summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authormaxc01 <xingchen92@gmail.com>2015-10-07 13:48:26 +0800
committermaxc01 <xingchen92@gmail.com>2015-10-07 13:48:26 +0800
commita61af756ddca4544de5e4969edc73131f4fccdd1 (patch)
tree2ac1755695a42d3964208e0029e74d446f5c3bd8 /examples
parent0840304af1564fa86a6006d648450372f301a6c8 (diff)
parentc84a485f148063f6d0c23f016fe348ec94fb6b19 (diff)
Merge pull request #1 from albertoruiz/master
sync from albetoruiz/hmatrix
Diffstat (limited to 'examples')
-rw-r--r--examples/bool.hs46
-rw-r--r--examples/bool.ipynb1152
-rw-r--r--examples/devel/ej1/functions.c35
-rw-r--r--examples/devel/ej1/wrappers.hs44
-rw-r--r--examples/devel/ej2/functions.c24
-rw-r--r--examples/devel/ej2/wrappers.hs32
-rw-r--r--examples/devel/example/functions.c22
-rw-r--r--examples/devel/example/wrappers.hs45
-rw-r--r--examples/error.hs8
-rw-r--r--examples/inplace.hs4
-rw-r--r--examples/kalman.hs33
-rw-r--r--examples/lie.hs10
-rw-r--r--examples/minimize.hs9
-rw-r--r--examples/monadic.hs31
-rw-r--r--examples/multiply.hs22
-rw-r--r--examples/ode.hs2
-rw-r--r--examples/pca1.hs18
-rw-r--r--examples/pca2.hs17
-rw-r--r--examples/pinv.hs13
-rw-r--r--examples/pinv.ipynb722
-rw-r--r--examples/plot.hs4
-rw-r--r--examples/repmat.ipynb138
-rw-r--r--examples/root.hs6
-rw-r--r--examples/vector.hs31
24 files changed, 2197 insertions, 271 deletions
diff --git a/examples/bool.hs b/examples/bool.hs
index 679b8bf..ee85523 100644
--- a/examples/bool.hs
+++ b/examples/bool.hs
@@ -1,17 +1,25 @@
1-- vectorized boolean operations defined in terms of step or cond 1-- vectorized boolean operations defined in terms of step or cond
2 2
3{-# LANGUAGE FlexibleContexts #-}
4
3import Numeric.LinearAlgebra 5import Numeric.LinearAlgebra
4 6
5infix 4 .==., ./=., .<., .<=., .>=., .>. 7infix 4 .==., ./=., .<., .<=., .>=., .>.
6infixr 3 .&&. 8infixr 3 .&&.
7infixr 2 .||. 9infixr 2 .||.
8 10
9a .<. b = step (b-a) 11-- specialized for Int result
10a .<=. b = cond a b 1 1 0 12cond'
11a .==. b = cond a b 0 1 0 13 :: (Element t, Ord t, Container c I, Container c t)
12a ./=. b = cond a b 1 0 1 14 => c t -> c t -> c I -> c I -> c I -> c I
13a .>=. b = cond a b 0 1 1 15cond' = cond
14a .>. b = step (a-b) 16
17a .<. b = cond' a b 1 0 0
18a .<=. b = cond' a b 1 1 0
19a .==. b = cond' a b 0 1 0
20a ./=. b = cond' a b 1 0 1
21a .>=. b = cond' a b 0 1 1
22a .>. b = cond' a b 0 0 1
15 23
16a .&&. b = step (a*b) 24a .&&. b = step (a*b)
17a .||. b = step (a+b) 25a .||. b = step (a+b)
@@ -29,26 +37,22 @@ maxEvery a b = cond a b b b a
29 37
30clip a b x = cond y b y y b where y = cond x a a x x 38clip a b x = cond y b y y b where y = cond x a a x x
31 39
32disp = putStr . dispf 3 40eye n = ident n :: Matrix R
33
34eye n = ident n :: Matrix Double
35row = asRow . fromList :: [Double] -> Matrix Double
36col = asColumn . fromList :: [Double] -> Matrix Double
37 41
38m = (3><4) [1..] :: Matrix Double 42m = (3><4) [1..] :: Matrix R
39 43
40p = row [0,0,1,1] 44p = fromList [0,0,1,1] :: Vector I
41q = row [0,1,0,1] 45q = fromList [0,1,0,1] :: Vector I
42 46
43main = do 47main = do
44 print $ find (>6) m 48 print $ find (>6) m
45 disp $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] 49 disp 3 $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..]
46 disp $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)] 50 disp 3 $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)]
47 disp $ m .>=. 10 .||. m .<. 4 51 print $ m .>=. 10 .||. m .<. 4
48 (disp . fromColumns . map flatten) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q] 52 (print . fromColumns) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q]
49 print $ taut $ (p `imp` q ) `equiv` (no q `imp` no p) 53 print $ taut $ (p `imp` q ) `equiv` (no q `imp` no p)
50 print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q) 54 print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q)
51 disp $ clip 3 8 m 55 disp 3 $ clip 3 8 m
52 disp $ col [1..7] .<=. row [1..5] 56 print $ col [1..7] .<=. row [1..5]
53 disp $ cond (col [1..3]) (row [1..4]) m 50 (3*m) 57 print $ cond (col [1..3]) (row [1..4]) m 50 (3*m)
54 58
diff --git a/examples/bool.ipynb b/examples/bool.ipynb
new file mode 100644
index 0000000..abceeb4
--- /dev/null
+++ b/examples/bool.ipynb
@@ -0,0 +1,1152 @@
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# vectorized boolean operations"
8 ]
9 },
10 {
11 "cell_type": "code",
12 "execution_count": 1,
13 "metadata": {
14 "collapsed": true
15 },
16 "outputs": [],
17 "source": [
18 "import Numeric.LinearAlgebra\n",
19 ":ext FlexibleContexts"
20 ]
21 },
22 {
23 "cell_type": "markdown",
24 "metadata": {},
25 "source": [
26 "## pretty printing"
27 ]
28 },
29 {
30 "cell_type": "code",
31 "execution_count": 2,
32 "metadata": {
33 "collapsed": false,
34 "scrolled": true
35 },
36 "outputs": [],
37 "source": [
38 "import IHaskell.Display\n",
39 ":ext FlexibleInstances\n",
40 "\n",
41 "dec = 3\n",
42 "\n",
43 "dispBool = (\"\\n\"++) . format \" \" f\n",
44 " where\n",
45 " f 1 = \"\\\\top\"\n",
46 " f 0 = \"\\\\cdot\"\n",
47 "\n",
48 "instance IHaskellDisplay (Matrix I) where\n",
49 " display m = return $ Display [html (\"<p>$$\"++(latexFormat \"bmatrix\" . dispBool) m++\"$$</p>\")]\n",
50 "\n",
51 "instance IHaskellDisplay (Matrix C) where\n",
52 " display m = return $ Display [html (\"<p>$$\"++(latexFormat \"bmatrix\" . dispcf dec) m++\"$$</p>\")]\n",
53 "\n",
54 "instance IHaskellDisplay (Matrix R) where\n",
55 " display m = return $ Display [html (\"<p>$$\"++ (latexFormat \"bmatrix\" . dispf dec) m++\"$$</p>\")]"
56 ]
57 },
58 {
59 "cell_type": "markdown",
60 "metadata": {},
61 "source": [
62 "## definitions"
63 ]
64 },
65 {
66 "cell_type": "markdown",
67 "metadata": {},
68 "source": [
69 "vectorized operators defined in terms of `step` and `cond`"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 3,
75 "metadata": {
76 "collapsed": false
77 },
78 "outputs": [],
79 "source": [
80 "-- specialized for Int result\n",
81 "cond'\n",
82 " :: (Element t, Ord t, Container c I, Container c t)\n",
83 " => c t -> c t -> c I -> c I -> c I -> c I\n",
84 "cond' = cond"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": 4,
90 "metadata": {
91 "collapsed": false
92 },
93 "outputs": [],
94 "source": [
95 "infix 4 .==., ./=., .<., .<=., .>=., .>.\n",
96 "infixr 3 .&&.\n",
97 "infixr 2 .||.\n",
98 "\n",
99 "a .<. b = cond' a b 1 0 0\n",
100 "a .<=. b = cond' a b 1 1 0\n",
101 "a .==. b = cond' a b 0 1 0\n",
102 "a ./=. b = cond' a b 1 0 1\n",
103 "a .>=. b = cond' a b 0 1 1\n",
104 "a .>. b = cond' a b 0 0 1\n",
105 "\n",
106 "a .&&. b = step (a*b)\n",
107 "a .||. b = step (a+b)\n",
108 "no a = 1-a\n",
109 "xor a b = a ./=. b\n",
110 "equiv a b = a .==. b\n",
111 "imp a b = no a .||. b"
112 ]
113 },
114 {
115 "cell_type": "markdown",
116 "metadata": {},
117 "source": [
118 "other useful functions"
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": 5,
124 "metadata": {
125 "collapsed": true
126 },
127 "outputs": [],
128 "source": [
129 "taut x = minElement x == 1\n",
130 "\n",
131 "minEvery a b = cond a b a a b\n",
132 "maxEvery a b = cond a b b b a\n",
133 "\n",
134 "eye n = ident n :: Matrix R\n",
135 "\n",
136 "clip a b x = cond y b y y b\n",
137 " where\n",
138 " y = cond x a a x x"
139 ]
140 },
141 {
142 "cell_type": "markdown",
143 "metadata": {},
144 "source": [
145 "## examples"
146 ]
147 },
148 {
149 "cell_type": "code",
150 "execution_count": 6,
151 "metadata": {
152 "collapsed": false
153 },
154 "outputs": [
155 {
156 "data": {
157 "text/html": [
158 "<style>/*\n",
159 "Custom IHaskell CSS.\n",
160 "*/\n",
161 "\n",
162 "/* Styles used for the Hoogle display in the pager */\n",
163 ".hoogle-doc {\n",
164 " display: block;\n",
165 " padding-bottom: 1.3em;\n",
166 " padding-left: 0.4em;\n",
167 "}\n",
168 ".hoogle-code {\n",
169 " display: block;\n",
170 " font-family: monospace;\n",
171 " white-space: pre;\n",
172 "}\n",
173 ".hoogle-text {\n",
174 " display: block;\n",
175 "}\n",
176 ".hoogle-name {\n",
177 " color: green;\n",
178 " font-weight: bold;\n",
179 "}\n",
180 ".hoogle-head {\n",
181 " font-weight: bold;\n",
182 "}\n",
183 ".hoogle-sub {\n",
184 " display: block;\n",
185 " margin-left: 0.4em;\n",
186 "}\n",
187 ".hoogle-package {\n",
188 " font-weight: bold;\n",
189 " font-style: italic;\n",
190 "}\n",
191 ".hoogle-module {\n",
192 " font-weight: bold;\n",
193 "}\n",
194 ".hoogle-class {\n",
195 " font-weight: bold;\n",
196 "}\n",
197 "\n",
198 "/* Styles used for basic displays */\n",
199 ".get-type {\n",
200 " color: green;\n",
201 " font-weight: bold;\n",
202 " font-family: monospace;\n",
203 " display: block;\n",
204 " white-space: pre-wrap;\n",
205 "}\n",
206 "\n",
207 ".show-type {\n",
208 " color: green;\n",
209 " font-weight: bold;\n",
210 " font-family: monospace;\n",
211 " margin-left: 1em;\n",
212 "}\n",
213 "\n",
214 ".mono {\n",
215 " font-family: monospace;\n",
216 " display: block;\n",
217 "}\n",
218 "\n",
219 ".err-msg {\n",
220 " color: red;\n",
221 " font-style: italic;\n",
222 " font-family: monospace;\n",
223 " white-space: pre;\n",
224 " display: block;\n",
225 "}\n",
226 "\n",
227 "#unshowable {\n",
228 " color: red;\n",
229 " font-weight: bold;\n",
230 "}\n",
231 "\n",
232 ".err-msg.in.collapse {\n",
233 " padding-top: 0.7em;\n",
234 "}\n",
235 "\n",
236 "/* Code that will get highlighted before it is highlighted */\n",
237 ".highlight-code {\n",
238 " white-space: pre;\n",
239 " font-family: monospace;\n",
240 "}\n",
241 "\n",
242 "/* Hlint styles */\n",
243 ".suggestion-warning { \n",
244 " font-weight: bold;\n",
245 " color: rgb(200, 130, 0);\n",
246 "}\n",
247 ".suggestion-error { \n",
248 " font-weight: bold;\n",
249 " color: red;\n",
250 "}\n",
251 ".suggestion-name {\n",
252 " font-weight: bold;\n",
253 "}\n",
254 "</style><p>$$\\begin{bmatrix}\n",
255 "\\top & \\top & \\top & \\top & \\top\n",
256 "\\\\\n",
257 "\\cdot & \\top & \\top & \\top & \\top\n",
258 "\\\\\n",
259 "\\cdot & \\cdot & \\top & \\top & \\top\n",
260 "\\\\\n",
261 "\\cdot & \\cdot & \\cdot & \\top & \\top\n",
262 "\\\\\n",
263 "\\cdot & \\cdot & \\cdot & \\cdot & \\top\n",
264 "\\\\\n",
265 "\\cdot & \\cdot & \\cdot & \\cdot & \\cdot\n",
266 "\\\\\n",
267 "\\cdot & \\cdot & \\cdot & \\cdot & \\cdot\n",
268 "\\end{bmatrix}$$</p>"
269 ]
270 },
271 "metadata": {},
272 "output_type": "display_data"
273 }
274 ],
275 "source": [
276 "col [1..7] .<=. row [1..5]"
277 ]
278 },
279 {
280 "cell_type": "code",
281 "execution_count": 7,
282 "metadata": {
283 "collapsed": true
284 },
285 "outputs": [],
286 "source": [
287 "m = (3><4) [1..] :: Matrix R"
288 ]
289 },
290 {
291 "cell_type": "code",
292 "execution_count": 8,
293 "metadata": {
294 "collapsed": false
295 },
296 "outputs": [
297 {
298 "data": {
299 "text/html": [
300 "<style>/*\n",
301 "Custom IHaskell CSS.\n",
302 "*/\n",
303 "\n",
304 "/* Styles used for the Hoogle display in the pager */\n",
305 ".hoogle-doc {\n",
306 " display: block;\n",
307 " padding-bottom: 1.3em;\n",
308 " padding-left: 0.4em;\n",
309 "}\n",
310 ".hoogle-code {\n",
311 " display: block;\n",
312 " font-family: monospace;\n",
313 " white-space: pre;\n",
314 "}\n",
315 ".hoogle-text {\n",
316 " display: block;\n",
317 "}\n",
318 ".hoogle-name {\n",
319 " color: green;\n",
320 " font-weight: bold;\n",
321 "}\n",
322 ".hoogle-head {\n",
323 " font-weight: bold;\n",
324 "}\n",
325 ".hoogle-sub {\n",
326 " display: block;\n",
327 " margin-left: 0.4em;\n",
328 "}\n",
329 ".hoogle-package {\n",
330 " font-weight: bold;\n",
331 " font-style: italic;\n",
332 "}\n",
333 ".hoogle-module {\n",
334 " font-weight: bold;\n",
335 "}\n",
336 ".hoogle-class {\n",
337 " font-weight: bold;\n",
338 "}\n",
339 "\n",
340 "/* Styles used for basic displays */\n",
341 ".get-type {\n",
342 " color: green;\n",
343 " font-weight: bold;\n",
344 " font-family: monospace;\n",
345 " display: block;\n",
346 " white-space: pre-wrap;\n",
347 "}\n",
348 "\n",
349 ".show-type {\n",
350 " color: green;\n",
351 " font-weight: bold;\n",
352 " font-family: monospace;\n",
353 " margin-left: 1em;\n",
354 "}\n",
355 "\n",
356 ".mono {\n",
357 " font-family: monospace;\n",
358 " display: block;\n",
359 "}\n",
360 "\n",
361 ".err-msg {\n",
362 " color: red;\n",
363 " font-style: italic;\n",
364 " font-family: monospace;\n",
365 " white-space: pre;\n",
366 " display: block;\n",
367 "}\n",
368 "\n",
369 "#unshowable {\n",
370 " color: red;\n",
371 " font-weight: bold;\n",
372 "}\n",
373 "\n",
374 ".err-msg.in.collapse {\n",
375 " padding-top: 0.7em;\n",
376 "}\n",
377 "\n",
378 "/* Code that will get highlighted before it is highlighted */\n",
379 ".highlight-code {\n",
380 " white-space: pre;\n",
381 " font-family: monospace;\n",
382 "}\n",
383 "\n",
384 "/* Hlint styles */\n",
385 ".suggestion-warning { \n",
386 " font-weight: bold;\n",
387 " color: rgb(200, 130, 0);\n",
388 "}\n",
389 ".suggestion-error { \n",
390 " font-weight: bold;\n",
391 " color: red;\n",
392 "}\n",
393 ".suggestion-name {\n",
394 " font-weight: bold;\n",
395 "}\n",
396 "</style><p>$$\\begin{bmatrix}\n",
397 "1 & 2 & 3 & 4\n",
398 "\\\\\n",
399 "5 & 6 & 7 & 8\n",
400 "\\\\\n",
401 "9 & 10 & 11 & 12\n",
402 "\\end{bmatrix}$$</p>"
403 ]
404 },
405 "metadata": {},
406 "output_type": "display_data"
407 }
408 ],
409 "source": [
410 "m"
411 ]
412 },
413 {
414 "cell_type": "code",
415 "execution_count": 9,
416 "metadata": {
417 "collapsed": false
418 },
419 "outputs": [
420 {
421 "data": {
422 "text/html": [
423 "<style>/*\n",
424 "Custom IHaskell CSS.\n",
425 "*/\n",
426 "\n",
427 "/* Styles used for the Hoogle display in the pager */\n",
428 ".hoogle-doc {\n",
429 " display: block;\n",
430 " padding-bottom: 1.3em;\n",
431 " padding-left: 0.4em;\n",
432 "}\n",
433 ".hoogle-code {\n",
434 " display: block;\n",
435 " font-family: monospace;\n",
436 " white-space: pre;\n",
437 "}\n",
438 ".hoogle-text {\n",
439 " display: block;\n",
440 "}\n",
441 ".hoogle-name {\n",
442 " color: green;\n",
443 " font-weight: bold;\n",
444 "}\n",
445 ".hoogle-head {\n",
446 " font-weight: bold;\n",
447 "}\n",
448 ".hoogle-sub {\n",
449 " display: block;\n",
450 " margin-left: 0.4em;\n",
451 "}\n",
452 ".hoogle-package {\n",
453 " font-weight: bold;\n",
454 " font-style: italic;\n",
455 "}\n",
456 ".hoogle-module {\n",
457 " font-weight: bold;\n",
458 "}\n",
459 ".hoogle-class {\n",
460 " font-weight: bold;\n",
461 "}\n",
462 "\n",
463 "/* Styles used for basic displays */\n",
464 ".get-type {\n",
465 " color: green;\n",
466 " font-weight: bold;\n",
467 " font-family: monospace;\n",
468 " display: block;\n",
469 " white-space: pre-wrap;\n",
470 "}\n",
471 "\n",
472 ".show-type {\n",
473 " color: green;\n",
474 " font-weight: bold;\n",
475 " font-family: monospace;\n",
476 " margin-left: 1em;\n",
477 "}\n",
478 "\n",
479 ".mono {\n",
480 " font-family: monospace;\n",
481 " display: block;\n",
482 "}\n",
483 "\n",
484 ".err-msg {\n",
485 " color: red;\n",
486 " font-style: italic;\n",
487 " font-family: monospace;\n",
488 " white-space: pre;\n",
489 " display: block;\n",
490 "}\n",
491 "\n",
492 "#unshowable {\n",
493 " color: red;\n",
494 " font-weight: bold;\n",
495 "}\n",
496 "\n",
497 ".err-msg.in.collapse {\n",
498 " padding-top: 0.7em;\n",
499 "}\n",
500 "\n",
501 "/* Code that will get highlighted before it is highlighted */\n",
502 ".highlight-code {\n",
503 " white-space: pre;\n",
504 " font-family: monospace;\n",
505 "}\n",
506 "\n",
507 "/* Hlint styles */\n",
508 ".suggestion-warning { \n",
509 " font-weight: bold;\n",
510 " color: rgb(200, 130, 0);\n",
511 "}\n",
512 ".suggestion-error { \n",
513 " font-weight: bold;\n",
514 " color: red;\n",
515 "}\n",
516 ".suggestion-name {\n",
517 " font-weight: bold;\n",
518 "}\n",
519 "</style><p>$$\\begin{bmatrix}\n",
520 "3 & 3 & 3 & 4\n",
521 "\\\\\n",
522 "5 & 6 & 7 & 8\n",
523 "\\\\\n",
524 "8 & 8 & 8 & 8\n",
525 "\\end{bmatrix}$$</p>"
526 ]
527 },
528 "metadata": {},
529 "output_type": "display_data"
530 }
531 ],
532 "source": [
533 "clip 3 8 m"
534 ]
535 },
536 {
537 "cell_type": "code",
538 "execution_count": 10,
539 "metadata": {
540 "collapsed": false
541 },
542 "outputs": [
543 {
544 "data": {
545 "text/plain": [
546 "[(1,2),(1,3),(2,0),(2,1),(2,2),(2,3)]"
547 ]
548 },
549 "metadata": {},
550 "output_type": "display_data"
551 }
552 ],
553 "source": [
554 "find (>6) m"
555 ]
556 },
557 {
558 "cell_type": "code",
559 "execution_count": 11,
560 "metadata": {
561 "collapsed": false
562 },
563 "outputs": [
564 {
565 "data": {
566 "text/html": [
567 "<style>/*\n",
568 "Custom IHaskell CSS.\n",
569 "*/\n",
570 "\n",
571 "/* Styles used for the Hoogle display in the pager */\n",
572 ".hoogle-doc {\n",
573 " display: block;\n",
574 " padding-bottom: 1.3em;\n",
575 " padding-left: 0.4em;\n",
576 "}\n",
577 ".hoogle-code {\n",
578 " display: block;\n",
579 " font-family: monospace;\n",
580 " white-space: pre;\n",
581 "}\n",
582 ".hoogle-text {\n",
583 " display: block;\n",
584 "}\n",
585 ".hoogle-name {\n",
586 " color: green;\n",
587 " font-weight: bold;\n",
588 "}\n",
589 ".hoogle-head {\n",
590 " font-weight: bold;\n",
591 "}\n",
592 ".hoogle-sub {\n",
593 " display: block;\n",
594 " margin-left: 0.4em;\n",
595 "}\n",
596 ".hoogle-package {\n",
597 " font-weight: bold;\n",
598 " font-style: italic;\n",
599 "}\n",
600 ".hoogle-module {\n",
601 " font-weight: bold;\n",
602 "}\n",
603 ".hoogle-class {\n",
604 " font-weight: bold;\n",
605 "}\n",
606 "\n",
607 "/* Styles used for basic displays */\n",
608 ".get-type {\n",
609 " color: green;\n",
610 " font-weight: bold;\n",
611 " font-family: monospace;\n",
612 " display: block;\n",
613 " white-space: pre-wrap;\n",
614 "}\n",
615 "\n",
616 ".show-type {\n",
617 " color: green;\n",
618 " font-weight: bold;\n",
619 " font-family: monospace;\n",
620 " margin-left: 1em;\n",
621 "}\n",
622 "\n",
623 ".mono {\n",
624 " font-family: monospace;\n",
625 " display: block;\n",
626 "}\n",
627 "\n",
628 ".err-msg {\n",
629 " color: red;\n",
630 " font-style: italic;\n",
631 " font-family: monospace;\n",
632 " white-space: pre;\n",
633 " display: block;\n",
634 "}\n",
635 "\n",
636 "#unshowable {\n",
637 " color: red;\n",
638 " font-weight: bold;\n",
639 "}\n",
640 "\n",
641 ".err-msg.in.collapse {\n",
642 " padding-top: 0.7em;\n",
643 "}\n",
644 "\n",
645 "/* Code that will get highlighted before it is highlighted */\n",
646 ".highlight-code {\n",
647 " white-space: pre;\n",
648 " font-family: monospace;\n",
649 "}\n",
650 "\n",
651 "/* Hlint styles */\n",
652 ".suggestion-warning { \n",
653 " font-weight: bold;\n",
654 " color: rgb(200, 130, 0);\n",
655 "}\n",
656 ".suggestion-error { \n",
657 " font-weight: bold;\n",
658 " color: red;\n",
659 "}\n",
660 ".suggestion-name {\n",
661 " font-weight: bold;\n",
662 "}\n",
663 "</style><p>$$\\begin{bmatrix}\n",
664 "\\top & \\top & \\top & \\cdot\n",
665 "\\\\\n",
666 "\\cdot & \\cdot & \\cdot & \\cdot\n",
667 "\\\\\n",
668 "\\cdot & \\top & \\top & \\top\n",
669 "\\end{bmatrix}$$</p>"
670 ]
671 },
672 "metadata": {},
673 "output_type": "display_data"
674 }
675 ],
676 "source": [
677 "(m .>=. 10) .||. (m .<. 4)"
678 ]
679 },
680 {
681 "cell_type": "code",
682 "execution_count": 12,
683 "metadata": {
684 "collapsed": false
685 },
686 "outputs": [
687 {
688 "data": {
689 "text/html": [
690 "<style>/*\n",
691 "Custom IHaskell CSS.\n",
692 "*/\n",
693 "\n",
694 "/* Styles used for the Hoogle display in the pager */\n",
695 ".hoogle-doc {\n",
696 " display: block;\n",
697 " padding-bottom: 1.3em;\n",
698 " padding-left: 0.4em;\n",
699 "}\n",
700 ".hoogle-code {\n",
701 " display: block;\n",
702 " font-family: monospace;\n",
703 " white-space: pre;\n",
704 "}\n",
705 ".hoogle-text {\n",
706 " display: block;\n",
707 "}\n",
708 ".hoogle-name {\n",
709 " color: green;\n",
710 " font-weight: bold;\n",
711 "}\n",
712 ".hoogle-head {\n",
713 " font-weight: bold;\n",
714 "}\n",
715 ".hoogle-sub {\n",
716 " display: block;\n",
717 " margin-left: 0.4em;\n",
718 "}\n",
719 ".hoogle-package {\n",
720 " font-weight: bold;\n",
721 " font-style: italic;\n",
722 "}\n",
723 ".hoogle-module {\n",
724 " font-weight: bold;\n",
725 "}\n",
726 ".hoogle-class {\n",
727 " font-weight: bold;\n",
728 "}\n",
729 "\n",
730 "/* Styles used for basic displays */\n",
731 ".get-type {\n",
732 " color: green;\n",
733 " font-weight: bold;\n",
734 " font-family: monospace;\n",
735 " display: block;\n",
736 " white-space: pre-wrap;\n",
737 "}\n",
738 "\n",
739 ".show-type {\n",
740 " color: green;\n",
741 " font-weight: bold;\n",
742 " font-family: monospace;\n",
743 " margin-left: 1em;\n",
744 "}\n",
745 "\n",
746 ".mono {\n",
747 " font-family: monospace;\n",
748 " display: block;\n",
749 "}\n",
750 "\n",
751 ".err-msg {\n",
752 " color: red;\n",
753 " font-style: italic;\n",
754 " font-family: monospace;\n",
755 " white-space: pre;\n",
756 " display: block;\n",
757 "}\n",
758 "\n",
759 "#unshowable {\n",
760 " color: red;\n",
761 " font-weight: bold;\n",
762 "}\n",
763 "\n",
764 ".err-msg.in.collapse {\n",
765 " padding-top: 0.7em;\n",
766 "}\n",
767 "\n",
768 "/* Code that will get highlighted before it is highlighted */\n",
769 ".highlight-code {\n",
770 " white-space: pre;\n",
771 " font-family: monospace;\n",
772 "}\n",
773 "\n",
774 "/* Hlint styles */\n",
775 ".suggestion-warning { \n",
776 " font-weight: bold;\n",
777 " color: rgb(200, 130, 0);\n",
778 "}\n",
779 ".suggestion-error { \n",
780 " font-weight: bold;\n",
781 " color: red;\n",
782 "}\n",
783 ".suggestion-name {\n",
784 " font-weight: bold;\n",
785 "}\n",
786 "</style><p>$$\\begin{bmatrix}\n",
787 "50 & 2 & 3 & 4\n",
788 "\\\\\n",
789 "15 & 50 & 7 & 8\n",
790 "\\\\\n",
791 "27 & 30 & 50 & 12\n",
792 "\\end{bmatrix}$$</p>"
793 ]
794 },
795 "metadata": {},
796 "output_type": "display_data"
797 }
798 ],
799 "source": [
800 "cond (col [1..3]) (row [1..4]) m 50 (3*m)"
801 ]
802 },
803 {
804 "cell_type": "code",
805 "execution_count": 13,
806 "metadata": {
807 "collapsed": false
808 },
809 "outputs": [
810 {
811 "data": {
812 "text/plain": [
813 "(6><8)\n",
814 " [ 10, 7, 7, 7, 7, 7, 7, 7\n",
815 " , 7, 11, 7, 7, 7, 7, 7, 7\n",
816 " , 7, 7, 12, 7, 7, 7, 7, 7\n",
817 " , 7, 7, 7, 13, 7, 7, 7, 7\n",
818 " , 7, 7, 7, 7, 14, 7, 7, 7\n",
819 " , 7, 7, 7, 7, 7, 7, 7, 7 ]"
820 ]
821 },
822 "metadata": {},
823 "output_type": "display_data"
824 }
825 ],
826 "source": [
827 "assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] :: Matrix Z"
828 ]
829 },
830 {
831 "cell_type": "code",
832 "execution_count": 14,
833 "metadata": {
834 "collapsed": false
835 },
836 "outputs": [
837 {
838 "data": {
839 "text/html": [
840 "<style>/*\n",
841 "Custom IHaskell CSS.\n",
842 "*/\n",
843 "\n",
844 "/* Styles used for the Hoogle display in the pager */\n",
845 ".hoogle-doc {\n",
846 " display: block;\n",
847 " padding-bottom: 1.3em;\n",
848 " padding-left: 0.4em;\n",
849 "}\n",
850 ".hoogle-code {\n",
851 " display: block;\n",
852 " font-family: monospace;\n",
853 " white-space: pre;\n",
854 "}\n",
855 ".hoogle-text {\n",
856 " display: block;\n",
857 "}\n",
858 ".hoogle-name {\n",
859 " color: green;\n",
860 " font-weight: bold;\n",
861 "}\n",
862 ".hoogle-head {\n",
863 " font-weight: bold;\n",
864 "}\n",
865 ".hoogle-sub {\n",
866 " display: block;\n",
867 " margin-left: 0.4em;\n",
868 "}\n",
869 ".hoogle-package {\n",
870 " font-weight: bold;\n",
871 " font-style: italic;\n",
872 "}\n",
873 ".hoogle-module {\n",
874 " font-weight: bold;\n",
875 "}\n",
876 ".hoogle-class {\n",
877 " font-weight: bold;\n",
878 "}\n",
879 "\n",
880 "/* Styles used for basic displays */\n",
881 ".get-type {\n",
882 " color: green;\n",
883 " font-weight: bold;\n",
884 " font-family: monospace;\n",
885 " display: block;\n",
886 " white-space: pre-wrap;\n",
887 "}\n",
888 "\n",
889 ".show-type {\n",
890 " color: green;\n",
891 " font-weight: bold;\n",
892 " font-family: monospace;\n",
893 " margin-left: 1em;\n",
894 "}\n",
895 "\n",
896 ".mono {\n",
897 " font-family: monospace;\n",
898 " display: block;\n",
899 "}\n",
900 "\n",
901 ".err-msg {\n",
902 " color: red;\n",
903 " font-style: italic;\n",
904 " font-family: monospace;\n",
905 " white-space: pre;\n",
906 " display: block;\n",
907 "}\n",
908 "\n",
909 "#unshowable {\n",
910 " color: red;\n",
911 " font-weight: bold;\n",
912 "}\n",
913 "\n",
914 ".err-msg.in.collapse {\n",
915 " padding-top: 0.7em;\n",
916 "}\n",
917 "\n",
918 "/* Code that will get highlighted before it is highlighted */\n",
919 ".highlight-code {\n",
920 " white-space: pre;\n",
921 " font-family: monospace;\n",
922 "}\n",
923 "\n",
924 "/* Hlint styles */\n",
925 ".suggestion-warning { \n",
926 " font-weight: bold;\n",
927 " color: rgb(200, 130, 0);\n",
928 "}\n",
929 ".suggestion-error { \n",
930 " font-weight: bold;\n",
931 " color: red;\n",
932 "}\n",
933 ".suggestion-name {\n",
934 " font-weight: bold;\n",
935 "}\n",
936 "</style><p>$$\\begin{bmatrix}\n",
937 "1 & 0 & 3 & 0 & 0\n",
938 "\\\\\n",
939 "0 & 2 & 0 & 0 & 0\n",
940 "\\\\\n",
941 "0 & 0 & 1 & 0 & 0\n",
942 "\\\\\n",
943 "0 & 7 & 0 & 1 & 0\n",
944 "\\\\\n",
945 "0 & 0 & 0 & 0 & 1\n",
946 "\\end{bmatrix}$$</p>"
947 ]
948 },
949 "metadata": {},
950 "output_type": "display_data"
951 }
952 ],
953 "source": [
954 "accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)]"
955 ]
956 },
957 {
958 "cell_type": "code",
959 "execution_count": 15,
960 "metadata": {
961 "collapsed": true
962 },
963 "outputs": [],
964 "source": [
965 "p = fromList [0,0,1,1] :: Vector I\n",
966 "q = fromList [0,1,0,1] :: Vector I"
967 ]
968 },
969 {
970 "cell_type": "code",
971 "execution_count": 16,
972 "metadata": {
973 "collapsed": false
974 },
975 "outputs": [
976 {
977 "data": {
978 "text/html": [
979 "<style>/*\n",
980 "Custom IHaskell CSS.\n",
981 "*/\n",
982 "\n",
983 "/* Styles used for the Hoogle display in the pager */\n",
984 ".hoogle-doc {\n",
985 " display: block;\n",
986 " padding-bottom: 1.3em;\n",
987 " padding-left: 0.4em;\n",
988 "}\n",
989 ".hoogle-code {\n",
990 " display: block;\n",
991 " font-family: monospace;\n",
992 " white-space: pre;\n",
993 "}\n",
994 ".hoogle-text {\n",
995 " display: block;\n",
996 "}\n",
997 ".hoogle-name {\n",
998 " color: green;\n",
999 " font-weight: bold;\n",
1000 "}\n",
1001 ".hoogle-head {\n",
1002 " font-weight: bold;\n",
1003 "}\n",
1004 ".hoogle-sub {\n",
1005 " display: block;\n",
1006 " margin-left: 0.4em;\n",
1007 "}\n",
1008 ".hoogle-package {\n",
1009 " font-weight: bold;\n",
1010 " font-style: italic;\n",
1011 "}\n",
1012 ".hoogle-module {\n",
1013 " font-weight: bold;\n",
1014 "}\n",
1015 ".hoogle-class {\n",
1016 " font-weight: bold;\n",
1017 "}\n",
1018 "\n",
1019 "/* Styles used for basic displays */\n",
1020 ".get-type {\n",
1021 " color: green;\n",
1022 " font-weight: bold;\n",
1023 " font-family: monospace;\n",
1024 " display: block;\n",
1025 " white-space: pre-wrap;\n",
1026 "}\n",
1027 "\n",
1028 ".show-type {\n",
1029 " color: green;\n",
1030 " font-weight: bold;\n",
1031 " font-family: monospace;\n",
1032 " margin-left: 1em;\n",
1033 "}\n",
1034 "\n",
1035 ".mono {\n",
1036 " font-family: monospace;\n",
1037 " display: block;\n",
1038 "}\n",
1039 "\n",
1040 ".err-msg {\n",
1041 " color: red;\n",
1042 " font-style: italic;\n",
1043 " font-family: monospace;\n",
1044 " white-space: pre;\n",
1045 " display: block;\n",
1046 "}\n",
1047 "\n",
1048 "#unshowable {\n",
1049 " color: red;\n",
1050 " font-weight: bold;\n",
1051 "}\n",
1052 "\n",
1053 ".err-msg.in.collapse {\n",
1054 " padding-top: 0.7em;\n",
1055 "}\n",
1056 "\n",
1057 "/* Code that will get highlighted before it is highlighted */\n",
1058 ".highlight-code {\n",
1059 " white-space: pre;\n",
1060 " font-family: monospace;\n",
1061 "}\n",
1062 "\n",
1063 "/* Hlint styles */\n",
1064 ".suggestion-warning { \n",
1065 " font-weight: bold;\n",
1066 " color: rgb(200, 130, 0);\n",
1067 "}\n",
1068 ".suggestion-error { \n",
1069 " font-weight: bold;\n",
1070 " color: red;\n",
1071 "}\n",
1072 ".suggestion-name {\n",
1073 " font-weight: bold;\n",
1074 "}\n",
1075 "</style><p>$$\\begin{bmatrix}\n",
1076 "\\cdot & \\cdot & \\cdot & \\cdot & \\cdot & \\top & \\top\n",
1077 "\\\\\n",
1078 "\\cdot & \\top & \\cdot & \\top & \\top & \\cdot & \\top\n",
1079 "\\\\\n",
1080 "\\top & \\cdot & \\cdot & \\top & \\top & \\cdot & \\cdot\n",
1081 "\\\\\n",
1082 "\\top & \\top & \\top & \\top & \\cdot & \\top & \\top\n",
1083 "\\end{bmatrix}$$</p>"
1084 ]
1085 },
1086 "metadata": {},
1087 "output_type": "display_data"
1088 }
1089 ],
1090 "source": [
1091 "fromColumns [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q]"
1092 ]
1093 },
1094 {
1095 "cell_type": "code",
1096 "execution_count": 17,
1097 "metadata": {
1098 "collapsed": false
1099 },
1100 "outputs": [
1101 {
1102 "data": {
1103 "text/plain": [
1104 "True"
1105 ]
1106 },
1107 "metadata": {},
1108 "output_type": "display_data"
1109 }
1110 ],
1111 "source": [
1112 "taut $ (p `imp` q ) `equiv` (no q `imp` no p)"
1113 ]
1114 },
1115 {
1116 "cell_type": "code",
1117 "execution_count": 18,
1118 "metadata": {
1119 "collapsed": false
1120 },
1121 "outputs": [
1122 {
1123 "data": {
1124 "text/plain": [
1125 "False"
1126 ]
1127 },
1128 "metadata": {},
1129 "output_type": "display_data"
1130 }
1131 ],
1132 "source": [
1133 "taut $ xor p q `equiv` (p .&&. no q .||. no p .&&. q)"
1134 ]
1135 }
1136 ],
1137 "metadata": {
1138 "kernelspec": {
1139 "display_name": "Haskell",
1140 "language": "haskell",
1141 "name": "haskell"
1142 },
1143 "language_info": {
1144 "codemirror_mode": "ihaskell",
1145 "file_extension": ".hs",
1146 "name": "haskell",
1147 "version": "7.10.1"
1148 }
1149 },
1150 "nbformat": 4,
1151 "nbformat_minor": 0
1152}
diff --git a/examples/devel/ej1/functions.c b/examples/devel/ej1/functions.c
deleted file mode 100644
index 02a4cdd..0000000
--- a/examples/devel/ej1/functions.c
+++ /dev/null
@@ -1,35 +0,0 @@
1/* assuming row order */
2
3typedef struct { double r, i; } doublecomplex;
4
5#define DVEC(A) int A##n, double*A##p
6#define CVEC(A) int A##n, doublecomplex*A##p
7#define DMAT(A) int A##r, int A##c, double*A##p
8#define CMAT(A) int A##r, int A##c, doublecomplex*A##p
9
10#define AT(M,row,col) (M##p[(row)*M##c + (col)])
11
12/*-----------------------------------------------------*/
13
14int c_scale_vector(double s, DVEC(x), DVEC(y)) {
15 int k;
16 for (k=0; k<=yn; k++) {
17 yp[k] = s*xp[k];
18 }
19 return 0;
20}
21
22/*-----------------------------------------------------*/
23
24int c_diag(DMAT(m),DVEC(y),DMAT(z)) {
25 int i,j;
26 for (j=0; j<yn; j++) {
27 yp[j] = AT(m,j,j);
28 }
29 for (i=0; i<mr; i++) {
30 for (j=0; j<mc; j++) {
31 AT(z,i,j) = i==j?yp[i]:0;
32 }
33 }
34 return 0;
35}
diff --git a/examples/devel/ej1/wrappers.hs b/examples/devel/ej1/wrappers.hs
deleted file mode 100644
index a88f74b..0000000
--- a/examples/devel/ej1/wrappers.hs
+++ /dev/null
@@ -1,44 +0,0 @@
1{-# LANGUAGE ForeignFunctionInterface #-}
2
3-- $ ghc -O2 --make wrappers.hs functions.c
4
5import Numeric.LinearAlgebra
6import Data.Packed.Development
7import Foreign(Ptr,unsafePerformIO)
8import Foreign.C.Types(CInt)
9
10-----------------------------------------------------
11
12main = do
13 print $ myScale 3.0 (fromList [1..10])
14 print $ myDiag $ (3><5) [1..]
15
16-----------------------------------------------------
17
18foreign import ccall unsafe "c_scale_vector"
19 cScaleVector :: Double -- scale
20 -> CInt -> Ptr Double -- argument
21 -> CInt -> Ptr Double -- result
22 -> IO CInt -- exit code
23
24myScale s x = unsafePerformIO $ do
25 y <- createVector (dim x)
26 app2 (cScaleVector s) vec x vec y "cScaleVector"
27 return y
28
29-----------------------------------------------------
30-- forcing row order
31
32foreign import ccall unsafe "c_diag"
33 cDiag :: CInt -> CInt -> Ptr Double -- argument
34 -> CInt -> Ptr Double -- result1
35 -> CInt -> CInt -> Ptr Double -- result2
36 -> IO CInt -- exit code
37
38myDiag m = unsafePerformIO $ do
39 y <- createVector (min r c)
40 z <- createMatrix RowMajor r c
41 app3 cDiag mat (cmat m) vec y mat z "cDiag"
42 return (y,z)
43 where r = rows m
44 c = cols m
diff --git a/examples/devel/ej2/functions.c b/examples/devel/ej2/functions.c
deleted file mode 100644
index 4dcd377..0000000
--- a/examples/devel/ej2/functions.c
+++ /dev/null
@@ -1,24 +0,0 @@
1/* general element order */
2
3typedef struct { double r, i; } doublecomplex;
4
5#define DVEC(A) int A##n, double*A##p
6#define CVEC(A) int A##n, doublecomplex*A##p
7#define DMAT(A) int A##r, int A##c, double*A##p
8#define CMAT(A) int A##r, int A##c, doublecomplex*A##p
9
10#define AT(M,r,c) (M##p[(r)*sr+(c)*sc])
11
12int c_diag(int ro, DMAT(m),DVEC(y),DMAT(z)) {
13 int i,j,sr,sc;
14 if (ro==1) { sr = mc; sc = 1;} else { sr = 1; sc = mr;}
15 for (j=0; j<yn; j++) {
16 yp[j] = AT(m,j,j);
17 }
18 for (i=0; i<mr; i++) {
19 for (j=0; j<mc; j++) {
20 AT(z,i,j) = i==j?yp[i]:0;
21 }
22 }
23 return 0;
24}
diff --git a/examples/devel/ej2/wrappers.hs b/examples/devel/ej2/wrappers.hs
deleted file mode 100644
index 1c02a24..0000000
--- a/examples/devel/ej2/wrappers.hs
+++ /dev/null
@@ -1,32 +0,0 @@
1{-# LANGUAGE ForeignFunctionInterface #-}
2
3-- $ ghc -O2 --make wrappers.hs functions.c
4
5import Numeric.LinearAlgebra
6import Data.Packed.Development
7import Foreign(Ptr,unsafePerformIO)
8import Foreign.C.Types(CInt)
9
10-----------------------------------------------------
11
12main = do
13 print $ myDiag $ (3><5) [1..]
14
15-----------------------------------------------------
16-- arbitrary data order
17
18foreign import ccall unsafe "c_diag"
19 cDiag :: CInt -- matrix order
20 -> CInt -> CInt -> Ptr Double -- argument
21 -> CInt -> Ptr Double -- result1
22 -> CInt -> CInt -> Ptr Double -- result2
23 -> IO CInt -- exit code
24
25myDiag m = unsafePerformIO $ do
26 y <- createVector (min r c)
27 z <- createMatrix (orderOf m) r c
28 app3 (cDiag o) mat m vec y mat z "cDiag"
29 return (y,z)
30 where r = rows m
31 c = cols m
32 o = if orderOf m == RowMajor then 1 else 0
diff --git a/examples/devel/example/functions.c b/examples/devel/example/functions.c
new file mode 100644
index 0000000..67d3270
--- /dev/null
+++ b/examples/devel/example/functions.c
@@ -0,0 +1,22 @@
1
2typedef struct { double r, i; } doublecomplex;
3
4#define VEC(T,A) int A##n, T* A##p
5#define MAT(T,A) int A##r, int A##c, int A##Xr, int A##Xc, T* A##p
6
7#define AT(m,i,j) (m##p[(i)*m##Xr + (j)*m##Xc])
8#define TRAV(m,i,j) int i,j; for (i=0;i<m##r;i++) for (j=0;j<m##c;j++)
9
10
11int c_diag(MAT(double,m), VEC(double,y), MAT(double,z)) {
12 int k;
13 for (k=0; k<yn; k++) {
14 yp[k] = AT(m,k,k);
15 }
16 { TRAV(z,i,j) {
17 AT(z,i,j) = i==j?yp[i]:0;
18 }
19 }
20 return 0;
21}
22
diff --git a/examples/devel/example/wrappers.hs b/examples/devel/example/wrappers.hs
new file mode 100644
index 0000000..f4e0f0b
--- /dev/null
+++ b/examples/devel/example/wrappers.hs
@@ -0,0 +1,45 @@
1{-# LANGUAGE ForeignFunctionInterface #-}
2{-# LANGUAGE TypeOperators #-}
3{-# LANGUAGE GADTs #-}
4
5{-
6 $ ghc -O2 wrappers.hs functions.c
7 $ ./wrappers
8-}
9
10import Numeric.LinearAlgebra
11import Numeric.LinearAlgebra.Devel
12import System.IO.Unsafe(unsafePerformIO)
13import Foreign.C.Types(CInt(..))
14import Foreign.Ptr(Ptr)
15
16
17infixl 1 #
18a # b = apply a b
19{-# INLINE (#) #-}
20
21infixr 5 :>, ::>
22type (:>) t r = CInt -> Ptr t -> r
23type (::>) t r = CInt -> CInt -> CInt -> CInt -> Ptr t -> r
24type Ok = IO CInt
25
26-----------------------------------------------------
27
28x = (3><5) [1..]
29
30main = do
31 print x
32 print $ myDiag x
33 print $ myDiag (tr x)
34
35-----------------------------------------------------
36foreign import ccall unsafe "c_diag" cDiag :: Double ::> Double :> Double ::> Ok
37
38myDiag m = unsafePerformIO $ do
39 y <- createVector (min r c)
40 z <- createMatrix RowMajor r c
41 cDiag # m # y # z #| "cDiag"
42 return (y,z)
43 where
44 (r,c) = size m
45
diff --git a/examples/error.hs b/examples/error.hs
index 5efae7c..77467df 100644
--- a/examples/error.hs
+++ b/examples/error.hs
@@ -8,6 +8,7 @@ test x = catch
8 (print x) 8 (print x)
9 (\e -> putStrLn $ "captured ["++ show (e :: SomeException) ++"]") 9 (\e -> putStrLn $ "captured ["++ show (e :: SomeException) ++"]")
10 10
11
11main = do 12main = do
12 setErrorHandlerOff 13 setErrorHandlerOff
13 14
@@ -15,7 +16,8 @@ main = do
15 test $ 5 + (fst.exp_e) 1000 16 test $ 5 + (fst.exp_e) 1000
16 test $ bessel_zero_Jnu_e (-0.3) 2 17 test $ bessel_zero_Jnu_e (-0.3) 2
17 18
18 test $ (linearSolve 0 4 :: Matrix Double) 19 test $ (inv 0 :: Matrix Double)
19 test $ (linearSolve 5 (sqrt (-1)) :: Matrix Double) 20 test $ (linearSolveLS 5 (sqrt (-1)) :: Matrix Double)
21
22 putStrLn "Bye"
20 23
21 putStrLn "Bye" \ No newline at end of file
diff --git a/examples/inplace.hs b/examples/inplace.hs
index 574aa44..19f9bc9 100644
--- a/examples/inplace.hs
+++ b/examples/inplace.hs
@@ -1,7 +1,9 @@
1-- some tests of the interface for pure 1-- some tests of the interface for pure
2-- computations with inplace updates 2-- computations with inplace updates
3 3
4import Numeric.LinearAlgebra.HMatrix 4{-# LANGUAGE FlexibleContexts #-}
5
6import Numeric.LinearAlgebra
5import Numeric.LinearAlgebra.Devel 7import Numeric.LinearAlgebra.Devel
6 8
7import Data.Array.Unboxed 9import Data.Array.Unboxed
diff --git a/examples/kalman.hs b/examples/kalman.hs
index 7fbe3d2..9756aa0 100644
--- a/examples/kalman.hs
+++ b/examples/kalman.hs
@@ -1,17 +1,15 @@
1import Numeric.LinearAlgebra 1import Numeric.LinearAlgebra
2import Graphics.Plot 2import Graphics.Plot
3 3
4vector l = fromList l :: Vector Double 4f = fromLists
5matrix ls = fromLists ls :: Matrix Double 5 [[1,0,0,0],
6diagl = diag . vector 6 [1,1,0,0],
7 [0,0,1,0],
8 [0,0,0,1]]
7 9
8f = matrix [[1,0,0,0], 10h = fromLists
9 [1,1,0,0], 11 [[0,-1,1,0],
10 [0,0,1,0], 12 [0,-1,0,1]]
11 [0,0,0,1]]
12
13h = matrix [[0,-1,1,0],
14 [0,-1,0,1]]
15 13
16q = diagl [1,1,0,0] 14q = diagl [1,1,0,0]
17 15
@@ -25,13 +23,13 @@ type Measurement = Vector Double
25 23
26kalman :: System -> State -> Measurement -> State 24kalman :: System -> State -> Measurement -> State
27kalman (System f h q r) (State x p) z = State x' p' where 25kalman (System f h q r) (State x p) z = State x' p' where
28 px = f <> x -- prediction 26 px = f #> x -- prediction
29 pq = f <> p <> trans f + q -- its covariance 27 pq = f <> p <> tr f + q -- its covariance
30 y = z - h <> px -- residue 28 y = z - h #> px -- residue
31 cy = h <> pq <> trans h + r -- its covariance 29 cy = h <> pq <> tr h + r -- its covariance
32 k = pq <> trans h <> inv cy -- kalman gain 30 k = pq <> tr h <> inv cy -- kalman gain
33 x' = px + k <> y -- new state 31 x' = px + k #> y -- new state
34 p' = (ident (dim x) - k <> h) <> pq -- its covariance 32 p' = (ident (size x) - k <> h) <> pq -- its covariance
35 33
36sys = System f h q r 34sys = System f h q r
37 35
@@ -49,3 +47,4 @@ main = do
49 print $ fromRows $ take 10 (map sX xs) 47 print $ fromRows $ take 10 (map sX xs)
50 mapM_ (print . sP) $ take 10 xs 48 mapM_ (print . sP) $ take 10 xs
51 mplot (evolution 20 (xs,des)) 49 mplot (evolution 20 (xs,des))
50
diff --git a/examples/lie.hs b/examples/lie.hs
index db21ea8..4933df6 100644
--- a/examples/lie.hs
+++ b/examples/lie.hs
@@ -1,8 +1,8 @@
1-- The magic of Lie Algebra 1-- The magic of Lie Algebra
2 2
3import Numeric.LinearAlgebra 3{-# LANGUAGE FlexibleContexts #-}
4 4
5disp = putStrLn . dispf 5 5import Numeric.LinearAlgebra
6 6
7rot1 :: Double -> Matrix Double 7rot1 :: Double -> Matrix Double
8rot1 a = (3><3) 8rot1 a = (3><3)
@@ -58,8 +58,8 @@ main = do
58 exact = rot3 a <> rot1 b <> rot2 c 58 exact = rot3 a <> rot1 b <> rot2 c
59 lie = scalar a * g3 |+| scalar b * g1 |+| scalar c * g2 59 lie = scalar a * g3 |+| scalar b * g1 |+| scalar c * g2
60 putStrLn "position in the tangent space:" 60 putStrLn "position in the tangent space:"
61 disp lie 61 disp 5 lie
62 putStrLn "exponential map back to the group (2 terms):" 62 putStrLn "exponential map back to the group (2 terms):"
63 disp (expm lie) 63 disp 5 (expm lie)
64 putStrLn "exact position:" 64 putStrLn "exact position:"
65 disp exact 65 disp 5 exact
diff --git a/examples/minimize.hs b/examples/minimize.hs
index 19b2cb3..c27afc2 100644
--- a/examples/minimize.hs
+++ b/examples/minimize.hs
@@ -20,7 +20,7 @@ partialDerivative n f v = fst (derivCentral 0.01 g (v!!n)) where
20 g x = f (concat [a,x:b]) 20 g x = f (concat [a,x:b])
21 (a,_:b) = splitAt n v 21 (a,_:b) = splitAt n v
22 22
23disp = putStrLn . format " " (printf "%.3f") 23disp' = putStrLn . format " " (printf "%.3f")
24 24
25allMethods :: (Enum a, Bounded a) => [a] 25allMethods :: (Enum a, Bounded a) => [a]
26allMethods = [minBound .. maxBound] 26allMethods = [minBound .. maxBound]
@@ -29,22 +29,23 @@ test method = do
29 print method 29 print method
30 let (s,p) = minimize method 1E-2 30 [1,1] f [5,7] 30 let (s,p) = minimize method 1E-2 30 [1,1] f [5,7]
31 print s 31 print s
32 disp p 32 disp' p
33 33
34testD method = do 34testD method = do
35 print method 35 print method
36 let (s,p) = minimizeD method 1E-3 30 1E-2 1E-4 f df [5,7] 36 let (s,p) = minimizeD method 1E-3 30 1E-2 1E-4 f df [5,7]
37 print s 37 print s
38 disp p 38 disp' p
39 39
40testD' method = do 40testD' method = do
41 putStrLn $ show method ++ " with estimated gradient" 41 putStrLn $ show method ++ " with estimated gradient"
42 let (s,p) = minimizeD method 1E-3 30 1E-2 1E-4 f (gradient f) [5,7] 42 let (s,p) = minimizeD method 1E-3 30 1E-2 1E-4 f (gradient f) [5,7]
43 print s 43 print s
44 disp p 44 disp' p
45 45
46main = do 46main = do
47 mapM_ test [NMSimplex, NMSimplex2] 47 mapM_ test [NMSimplex, NMSimplex2]
48 mapM_ testD allMethods 48 mapM_ testD allMethods
49 testD' ConjugateFR 49 testD' ConjugateFR
50 mplot $ drop 3 . toColumns . snd $ minimizeS f [5,7] 50 mplot $ drop 3 . toColumns . snd $ minimizeS f [5,7]
51
diff --git a/examples/monadic.hs b/examples/monadic.hs
index 7c6e0f4..cf8aacc 100644
--- a/examples/monadic.hs
+++ b/examples/monadic.hs
@@ -1,35 +1,37 @@
1-- monadic computations 1-- monadic computations
2-- (contributed by Vivian McPhail) 2-- (contributed by Vivian McPhail)
3 3
4{-# LANGUAGE FlexibleContexts #-}
5
4import Numeric.LinearAlgebra 6import Numeric.LinearAlgebra
7import Numeric.LinearAlgebra.Devel
5import Control.Monad.State.Strict 8import Control.Monad.State.Strict
6import Control.Monad.Maybe 9import Control.Monad.Trans.Maybe
7import Foreign.Storable(Storable) 10import Foreign.Storable(Storable)
8import System.Random(randomIO) 11import System.Random(randomIO)
9 12
10------------------------------------------- 13-------------------------------------------
11 14
12-- an instance of MonadIO, a monad transformer 15-- an instance of MonadIO, a monad transformer
13type VectorMonadT = StateT Int IO 16type VectorMonadT = StateT I IO
14 17
15test1 :: Vector Int -> IO (Vector Int) 18test1 :: Vector I -> IO (Vector I)
16test1 = mapVectorM $ \x -> do 19test1 = mapVectorM $ \x -> do
17 putStr $ (show x) ++ " " 20 putStr $ (show x) ++ " "
18 return (x + 1) 21 return (x + 1)
19 22
20-- we can have an arbitrary monad AND do IO 23-- we can have an arbitrary monad AND do IO
21addInitialM :: Vector Int -> VectorMonadT () 24addInitialM :: Vector I -> VectorMonadT ()
22addInitialM = mapVectorM_ $ \x -> do 25addInitialM = mapVectorM_ $ \x -> do
23 i <- get 26 i <- get
24 liftIO $ putStr $ (show $ x + i) ++ " " 27 liftIO $ putStr $ (show $ x + i) ++ " "
25 put $ x + i 28 put $ x + i
26 29
27-- sum the values of the even indiced elements 30-- sum the values of the even indiced elements
28sumEvens :: Vector Int -> Int 31sumEvens :: Vector I -> I
29sumEvens = foldVectorWithIndex (\x a b -> if x `mod` 2 == 0 then a + b else b) 0 32sumEvens = foldVectorWithIndex (\x a b -> if x `mod` 2 == 0 then a + b else b) 0
30 33
31-- sum and print running total of evens 34-- sum and print running total of evens
32sumEvensAndPrint :: Vector Int -> VectorMonadT ()
33sumEvensAndPrint = mapVectorWithIndexM_ $ \ i x -> do 35sumEvensAndPrint = mapVectorWithIndexM_ $ \ i x -> do
34 when (i `mod` 2 == 0) $ do 36 when (i `mod` 2 == 0) $ do
35 v <- get 37 v <- get
@@ -38,7 +40,7 @@ sumEvensAndPrint = mapVectorWithIndexM_ $ \ i x -> do
38 liftIO $ putStr $ (show v') ++ " " 40 liftIO $ putStr $ (show v') ++ " "
39 41
40 42
41indexPlusSum :: Vector Int -> VectorMonadT () 43--indexPlusSum :: Vector I -> VectorMonadT ()
42indexPlusSum v' = do 44indexPlusSum v' = do
43 let f i x = do 45 let f i x = do
44 s <- get 46 s <- get
@@ -63,7 +65,7 @@ monoStep d = do
63 65
64isMonotoneIncreasing :: Vector Double -> Bool 66isMonotoneIncreasing :: Vector Double -> Bool
65isMonotoneIncreasing v = 67isMonotoneIncreasing v =
66 let res = evalState (runMaybeT $ (mapVectorM_ monoStep v)) (v @> 0) 68 let res = evalState (runMaybeT $ (mapVectorM_ monoStep v)) (v ! 0)
67 in case res of 69 in case res of
68 Nothing -> False 70 Nothing -> False
69 Just _ -> True 71 Just _ -> True
@@ -72,8 +74,8 @@ isMonotoneIncreasing v =
72------------------------------------------- 74-------------------------------------------
73 75
74-- | apply a test to successive elements of a vector, evaluates to true iff test passes for all pairs 76-- | apply a test to successive elements of a vector, evaluates to true iff test passes for all pairs
75successive_ :: Storable a => (a -> a -> Bool) -> Vector a -> Bool 77successive_ :: (Container Vector a, Indexable (Vector a) a) => (a -> a -> Bool) -> Vector a -> Bool
76successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ step (subVector 1 (dim v - 1) v))) (v @> 0) 78successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ step (subVector 1 (size v - 1) v))) (v ! 0)
77 where step e = do 79 where step e = do
78 ep <- lift $ get 80 ep <- lift $ get
79 if t e ep 81 if t e ep
@@ -81,8 +83,10 @@ successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ s
81 else (fail "successive_ test failed") 83 else (fail "successive_ test failed")
82 84
83-- | operate on successive elements of a vector and return the resulting vector, whose length 1 less than that of the input 85-- | operate on successive elements of a vector and return the resulting vector, whose length 1 less than that of the input
84successive :: (Storable a, Storable b) => (a -> a -> b) -> Vector a -> Vector b 86successive
85successive f v = evalState (mapVectorM step (subVector 1 (dim v - 1) v)) (v @> 0) 87 :: (Storable b, Container Vector s, Indexable (Vector s) s)
88 => (s -> s -> b) -> Vector s -> Vector b
89successive f v = evalState (mapVectorM step (subVector 1 (size v - 1) v)) (v ! 0)
86 where step e = do 90 where step e = do
87 ep <- get 91 ep <- get
88 put e 92 put e
@@ -90,7 +94,7 @@ successive f v = evalState (mapVectorM step (subVector 1 (dim v - 1) v)) (v @> 0
90 94
91------------------------------------------- 95-------------------------------------------
92 96
93v :: Vector Int 97v :: Vector I
94v = 10 |> [0..] 98v = 10 |> [0..]
95 99
96w = fromList ([1..10]++[10,9..1]) :: Vector Double 100w = fromList ([1..10]++[10,9..1]) :: Vector Double
@@ -116,3 +120,4 @@ main = do
116 print $ successive_ (>) v 120 print $ successive_ (>) v
117 print $ successive_ (>) w 121 print $ successive_ (>) w
118 print $ successive (+) v 122 print $ successive (+) v
123
diff --git a/examples/multiply.hs b/examples/multiply.hs
index 572961c..be8fa73 100644
--- a/examples/multiply.hs
+++ b/examples/multiply.hs
@@ -22,10 +22,10 @@ instance Container Vector t => Scaling t (Vector t) (Vector t) where
22instance Container Vector t => Scaling (Vector t) t (Vector t) where 22instance Container Vector t => Scaling (Vector t) t (Vector t) where
23 (â‹…) = flip scale 23 (â‹…) = flip scale
24 24
25instance Container Vector t => Scaling t (Matrix t) (Matrix t) where 25instance (Num t, Container Vector t) => Scaling t (Matrix t) (Matrix t) where
26 (â‹…) = scale 26 (â‹…) = scale
27 27
28instance Container Vector t => Scaling (Matrix t) t (Matrix t) where 28instance (Num t, Container Vector t) => Scaling (Matrix t) t (Matrix t) where
29 (â‹…) = flip scale 29 (â‹…) = flip scale
30 30
31 31
@@ -42,14 +42,14 @@ class Mul a b c | a b -> c, a c -> b, b c -> a where
42instance Product t => Mul (Vector t) (Vector t) t where 42instance Product t => Mul (Vector t) (Vector t) t where
43 (×) = udot 43 (×) = udot
44 44
45instance Product t => Mul (Matrix t) (Vector t) (Vector t) where 45instance (Numeric t, Product t) => Mul (Matrix t) (Vector t) (Vector t) where
46 (×) = mXv 46 (×) = (#>)
47 47
48instance Product t => Mul (Vector t) (Matrix t) (Vector t) where 48instance (Numeric t, Product t) => Mul (Vector t) (Matrix t) (Vector t) where
49 (×) = vXm 49 (×) = (<#)
50 50
51instance Product t => Mul (Matrix t) (Matrix t) (Matrix t) where 51instance (Numeric t, Product t) => Mul (Matrix t) (Matrix t) (Matrix t) where
52 (×) = mXm 52 (×) = (<>)
53 53
54 54
55--instance Scaling a b c => Contraction a b c where 55--instance Scaling a b c => Contraction a b c where
@@ -92,9 +92,9 @@ u = fromList [3,0,5]
92w = konst 1 (2,3) :: Matrix Double 92w = konst 1 (2,3) :: Matrix Double
93 93
94main = do 94main = do
95 print $ (scale s v <> m) `udot` v 95 print $ (scale s v <# m) `udot` v
96 print $ scale s v `udot` (m <> v) 96 print $ scale s v `udot` (m #> v)
97 print $ s * ((v <> m) `udot` v) 97 print $ s * ((v <# m) `udot` v)
98 print $ s ⋅ v × m × v 98 print $ s ⋅ v × m × v
99 print a 99 print a
100-- print (b == c) 100-- print (b == c)
diff --git a/examples/ode.hs b/examples/ode.hs
index dc6e0ec..4cf1673 100644
--- a/examples/ode.hs
+++ b/examples/ode.hs
@@ -43,7 +43,7 @@ vanderpol' mu = do
43 jac t (toList->[x,v]) = (2><2) [ 0 , 1 43 jac t (toList->[x,v]) = (2><2) [ 0 , 1
44 , -1-2*x*v*mu, mu*(1-x**2) ] 44 , -1-2*x*v*mu, mu*(1-x**2) ]
45 ts = linspace 1000 (0,50) 45 ts = linspace 1000 (0,50)
46 hi = (ts@>1 - ts@>0)/100 46 hi = (ts!1 - ts!0)/100
47 sol = toColumns $ odeSolveV (MSBDF jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts 47 sol = toColumns $ odeSolveV (MSBDF jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts
48 mplot sol 48 mplot sol
49 49
diff --git a/examples/pca1.hs b/examples/pca1.hs
index a11eba9..ad2214d 100644
--- a/examples/pca1.hs
+++ b/examples/pca1.hs
@@ -8,27 +8,25 @@ import Control.Monad(when)
8type Vec = Vector Double 8type Vec = Vector Double
9type Mat = Matrix Double 9type Mat = Matrix Double
10 10
11 11{-
12-- Vector with the mean value of the columns of a matrix 12-- Vector with the mean value of the columns of a matrix
13mean a = constant (recip . fromIntegral . rows $ a) (rows a) <> a 13mean a = constant (recip . fromIntegral . rows $ a) (rows a) <> a
14 14
15-- covariance matrix of a list of observations stored as rows 15-- covariance matrix of a list of observations stored as rows
16cov x = (trans xc <> xc) / fromIntegral (rows x - 1) 16cov x = (trans xc <> xc) / fromIntegral (rows x - 1)
17 where xc = x - asRow (mean x) 17 where xc = x - asRow (mean x)
18-}
18 19
19 20
20-- creates the compression and decompression functions from the desired number of components 21-- creates the compression and decompression functions from the desired number of components
21pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec) 22pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec)
22pca n dataSet = (encode,decode) 23pca n dataSet = (encode,decode)
23 where 24 where
24 encode x = vp <> (x - m) 25 encode x = vp #> (x - m)
25 decode x = x <> vp + m 26 decode x = x <# vp + m
26 m = mean dataSet 27 (m,c) = meanCov dataSet
27 c = cov dataSet 28 (_,v) = eigSH (trustSym c)
28 (_,v) = eigSH' c 29 vp = tr $ takeColumns n v
29 vp = takeRows n (trans v)
30
31norm = pnorm PNorm2
32 30
33main = do 31main = do
34 ok <- doesFileExist ("mnist.txt") 32 ok <- doesFileExist ("mnist.txt")
@@ -43,4 +41,4 @@ main = do
43 let (pe,pd) = pca 10 xs 41 let (pe,pd) = pca 10 xs
44 let y = pe x 42 let y = pe x
45 print y -- compressed version 43 print y -- compressed version
46 print $ norm (x - pd y) / norm x --reconstruction quality 44 print $ norm_2 (x - pd y) / norm_2 x --reconstruction quality
diff --git a/examples/pca2.hs b/examples/pca2.hs
index e7ea95f..892d382 100644
--- a/examples/pca2.hs
+++ b/examples/pca2.hs
@@ -1,5 +1,7 @@
1-- Improved PCA, including illustrative graphics 1-- Improved PCA, including illustrative graphics
2 2
3{-# LANGUAGE FlexibleContexts #-}
4
3import Numeric.LinearAlgebra 5import Numeric.LinearAlgebra
4import Graphics.Plot 6import Graphics.Plot
5import System.Directory(doesFileExist) 7import System.Directory(doesFileExist)
@@ -10,27 +12,27 @@ type Vec = Vector Double
10type Mat = Matrix Double 12type Mat = Matrix Double
11 13
12-- Vector with the mean value of the columns of a matrix 14-- Vector with the mean value of the columns of a matrix
13mean a = constant (recip . fromIntegral . rows $ a) (rows a) <> a 15mean a = konst (recip . fromIntegral . rows $ a) (rows a) <# a
14 16
15-- covariance matrix of a list of observations stored as rows 17-- covariance matrix of a list of observations stored as rows
16cov x = (trans xc <> xc) / fromIntegral (rows x - 1) 18cov x = (mTm xc) -- / fromIntegral (rows x - 1)
17 where xc = x - asRow (mean x) 19 where xc = x - asRow (mean x)
18 20
19 21
20type Stat = (Vec, [Double], Mat) 22type Stat = (Vec, [Double], Mat)
21-- 1st and 2nd order statistics of a dataset (mean, eigenvalues and eigenvectors of cov) 23-- 1st and 2nd order statistics of a dataset (mean, eigenvalues and eigenvectors of cov)
22stat :: Mat -> Stat 24stat :: Mat -> Stat
23stat x = (m, toList s, trans v) where 25stat x = (m, toList s, tr v) where
24 m = mean x 26 m = mean x
25 (s,v) = eigSH' (cov x) 27 (s,v) = eigSH (cov x)
26 28
27-- creates the compression and decompression functions from the desired reconstruction 29-- creates the compression and decompression functions from the desired reconstruction
28-- quality and the statistics of a data set 30-- quality and the statistics of a data set
29pca :: Double -> Stat -> (Vec -> Vec , Vec -> Vec) 31pca :: Double -> Stat -> (Vec -> Vec , Vec -> Vec)
30pca prec (m,s,v) = (encode,decode) 32pca prec (m,s,v) = (encode,decode)
31 where 33 where
32 encode x = vp <> (x - m) 34 encode x = vp #> (x - m)
33 decode x = x <> vp + m 35 decode x = x <# vp + m
34 vp = takeRows n v 36 vp = takeRows n v
35 n = 1 + (length $ fst $ span (< (prec'*sum s)) $ cumSum s) 37 n = 1 + (length $ fst $ span (< (prec'*sum s)) $ cumSum s)
36 cumSum = tail . scanl (+) 0.0 38 cumSum = tail . scanl (+) 0.0
@@ -46,7 +48,7 @@ test :: Stat -> Double -> Vec -> IO ()
46test st prec x = do 48test st prec x = do
47 let (pe,pd) = pca prec st 49 let (pe,pd) = pca prec st
48 let y = pe x 50 let y = pe x
49 print $ dim y 51 print $ size y
50 shdigit (pd y) 52 shdigit (pd y)
51 53
52main = do 54main = do
@@ -63,3 +65,4 @@ main = do
63 let st = stat xs 65 let st = stat xs
64 test st 0.90 x 66 test st 0.90 x
65 test st 0.50 x 67 test st 0.50 x
68
diff --git a/examples/pinv.hs b/examples/pinv.hs
index 7de50b8..6f093b4 100644
--- a/examples/pinv.hs
+++ b/examples/pinv.hs
@@ -1,20 +1,19 @@
1import Numeric.LinearAlgebra 1import Numeric.LinearAlgebra
2import Graphics.Plot
3import Text.Printf(printf) 2import Text.Printf(printf)
4 3
5expand :: Int -> Vector Double -> Matrix Double 4expand :: Int -> Vector R -> Matrix R
6expand n x = fromColumns $ map (x^) [0 .. n] 5expand n x = fromColumns $ map (x^) [0 .. n]
7 6
8polynomialModel :: Vector Double -> Vector Double -> Int 7polynomialModel :: Vector R -> Vector R -> Int
9 -> (Vector Double -> Vector Double) 8 -> (Vector R -> Vector R)
10polynomialModel x y n = f where 9polynomialModel x y n = f where
11 f z = expand n z <> ws 10 f z = expand n z #> ws
12 ws = expand n x <\> y 11 ws = expand n x <\> y
13 12
14main = do 13main = do
15 [x,y] <- (toColumns . readMatrix) `fmap` readFile "data.txt" 14 [x,y] <- toColumns <$> loadMatrix "data.txt"
16 let pol = polynomialModel x y 15 let pol = polynomialModel x y
17 let view = [x, y, pol 1 x, pol 2 x, pol 3 x] 16 let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
18 putStrLn $ " x y p 1 p 2 p 3" 17 putStrLn $ " x y p 1 p 2 p 3"
19 putStrLn $ format " " (printf "%.2f") $ fromColumns view 18 putStrLn $ format " " (printf "%.2f") $ fromColumns view
20 mplot view 19
diff --git a/examples/pinv.ipynb b/examples/pinv.ipynb
new file mode 100644
index 0000000..532b8d0
--- /dev/null
+++ b/examples/pinv.ipynb
@@ -0,0 +1,722 @@
1{
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "import Numeric.LinearAlgebra"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": 2,
17 "metadata": {
18 "collapsed": true
19 },
20 "outputs": [],
21 "source": [
22 "import IHaskell.Display"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 3,
28 "metadata": {
29 "collapsed": false
30 },
31 "outputs": [],
32 "source": [
33 ":ext FlexibleInstances\n",
34 "\n",
35 "dec = 3\n",
36 "\n",
37 "instance IHaskellDisplay (Matrix C) where\n",
38 " display m = return $ Display [html (\"<p>$$\"++(latexFormat \"bmatrix\" . dispcf dec) m++\"$$</p>\")]\n",
39 "\n",
40 "instance IHaskellDisplay (Matrix R) where\n",
41 " display m = return $ Display [html (\"<p>$$\"++ (latexFormat \"bmatrix\" . dispf dec) m++\"$$</p>\")]"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "execution_count": 4,
47 "metadata": {
48 "collapsed": true
49 },
50 "outputs": [],
51 "source": [
52 "import Graphics.SVG\n",
53 "data RawSVG = RawSVG String\n",
54 "instance IHaskellDisplay RawSVG where\n",
55 " display (RawSVG s) = return $ Display [html $ \"<div style=\\\"width:600px\\\">\"++ s ++ \"</div>\"]\n",
56 "\n",
57 "lplot = RawSVG . hPlot"
58 ]
59 },
60 {
61 "cell_type": "markdown",
62 "metadata": {},
63 "source": [
64 "# least squares polynomial model"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": 5,
70 "metadata": {
71 "collapsed": false
72 },
73 "outputs": [],
74 "source": [
75 "expand :: Int -> Vector R -> Matrix R\n",
76 "expand n x = fromColumns $ map (x^) [0 .. n]\n",
77 "\n",
78 "polynomialModel :: Vector R -> Vector R -> Int -> (Vector R -> Vector R)\n",
79 "polynomialModel x y n = f\n",
80 " where\n",
81 " f z = expand n z #> ws\n",
82 " ws = expand n x <\\> y"
83 ]
84 },
85 {
86 "cell_type": "code",
87 "execution_count": 6,
88 "metadata": {
89 "collapsed": true
90 },
91 "outputs": [],
92 "source": [
93 "[x,y] <- toColumns <$> loadMatrix \"data.txt\""
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": 7,
99 "metadata": {
100 "collapsed": false
101 },
102 "outputs": [
103 {
104 "data": {
105 "text/plain": [
106 "[0.9,2.1,3.1,4.0,4.9,6.1,7.0,7.9,9.1,10.2]"
107 ]
108 },
109 "metadata": {},
110 "output_type": "display_data"
111 },
112 {
113 "data": {
114 "text/plain": [
115 "[1.1,3.9,9.2,51.8,25.3,35.7,49.4,3.6,81.5,99.5]"
116 ]
117 },
118 "metadata": {},
119 "output_type": "display_data"
120 }
121 ],
122 "source": [
123 "x\n",
124 "y"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 8,
130 "metadata": {
131 "collapsed": false
132 },
133 "outputs": [
134 {
135 "data": {
136 "text/html": [
137 "<style>/*\n",
138 "Custom IHaskell CSS.\n",
139 "*/\n",
140 "\n",
141 "/* Styles used for the Hoogle display in the pager */\n",
142 ".hoogle-doc {\n",
143 " display: block;\n",
144 " padding-bottom: 1.3em;\n",
145 " padding-left: 0.4em;\n",
146 "}\n",
147 ".hoogle-code {\n",
148 " display: block;\n",
149 " font-family: monospace;\n",
150 " white-space: pre;\n",
151 "}\n",
152 ".hoogle-text {\n",
153 " display: block;\n",
154 "}\n",
155 ".hoogle-name {\n",
156 " color: green;\n",
157 " font-weight: bold;\n",
158 "}\n",
159 ".hoogle-head {\n",
160 " font-weight: bold;\n",
161 "}\n",
162 ".hoogle-sub {\n",
163 " display: block;\n",
164 " margin-left: 0.4em;\n",
165 "}\n",
166 ".hoogle-package {\n",
167 " font-weight: bold;\n",
168 " font-style: italic;\n",
169 "}\n",
170 ".hoogle-module {\n",
171 " font-weight: bold;\n",
172 "}\n",
173 ".hoogle-class {\n",
174 " font-weight: bold;\n",
175 "}\n",
176 "\n",
177 "/* Styles used for basic displays */\n",
178 ".get-type {\n",
179 " color: green;\n",
180 " font-weight: bold;\n",
181 " font-family: monospace;\n",
182 " display: block;\n",
183 " white-space: pre-wrap;\n",
184 "}\n",
185 "\n",
186 ".show-type {\n",
187 " color: green;\n",
188 " font-weight: bold;\n",
189 " font-family: monospace;\n",
190 " margin-left: 1em;\n",
191 "}\n",
192 "\n",
193 ".mono {\n",
194 " font-family: monospace;\n",
195 " display: block;\n",
196 "}\n",
197 "\n",
198 ".err-msg {\n",
199 " color: red;\n",
200 " font-style: italic;\n",
201 " font-family: monospace;\n",
202 " white-space: pre;\n",
203 " display: block;\n",
204 "}\n",
205 "\n",
206 "#unshowable {\n",
207 " color: red;\n",
208 " font-weight: bold;\n",
209 "}\n",
210 "\n",
211 ".err-msg.in.collapse {\n",
212 " padding-top: 0.7em;\n",
213 "}\n",
214 "\n",
215 "/* Code that will get highlighted before it is highlighted */\n",
216 ".highlight-code {\n",
217 " white-space: pre;\n",
218 " font-family: monospace;\n",
219 "}\n",
220 "\n",
221 "/* Hlint styles */\n",
222 ".suggestion-warning { \n",
223 " font-weight: bold;\n",
224 " color: rgb(200, 130, 0);\n",
225 "}\n",
226 ".suggestion-error { \n",
227 " font-weight: bold;\n",
228 " color: red;\n",
229 "}\n",
230 ".suggestion-name {\n",
231 " font-weight: bold;\n",
232 "}\n",
233 "</style><p>$$\\begin{bmatrix}\n",
234 "1.000 & 0.900 & 0.810\n",
235 "\\\\\n",
236 "1.000 & 2.100 & 4.410\n",
237 "\\\\\n",
238 "1.000 & 3.100 & 9.610\n",
239 "\\\\\n",
240 "1.000 & 4.000 & 16.000\n",
241 "\\\\\n",
242 "1.000 & 4.900 & 24.010\n",
243 "\\\\\n",
244 "1.000 & 6.100 & 37.210\n",
245 "\\\\\n",
246 "1.000 & 7.000 & 49.000\n",
247 "\\\\\n",
248 "1.000 & 7.900 & 62.410\n",
249 "\\\\\n",
250 "1.000 & 9.100 & 82.810\n",
251 "\\\\\n",
252 "1.000 & 10.200 & 104.040\n",
253 "\\end{bmatrix}$$</p>"
254 ]
255 },
256 "metadata": {},
257 "output_type": "display_data"
258 }
259 ],
260 "source": [
261 "expand 2 x"
262 ]
263 },
264 {
265 "cell_type": "code",
266 "execution_count": 9,
267 "metadata": {
268 "collapsed": true
269 },
270 "outputs": [],
271 "source": [
272 "pol = polynomialModel x y\n",
273 "view = [x, y, pol 1 x, pol 2 x, pol 3 x]"
274 ]
275 },
276 {
277 "cell_type": "code",
278 "execution_count": 10,
279 "metadata": {
280 "collapsed": false
281 },
282 "outputs": [
283 {
284 "data": {
285 "text/plain": [
286 " x y p 1 p 2 p 3"
287 ]
288 },
289 "metadata": {},
290 "output_type": "display_data"
291 },
292 {
293 "data": {
294 "text/plain": [
295 " 0.90 1.10 -3.41 7.70 -6.99\n",
296 " 2.10 3.90 6.83 9.80 15.97\n",
297 " 3.10 9.20 15.36 13.39 25.09\n",
298 " 4.00 51.80 23.04 18.05 28.22\n",
299 " 4.90 25.30 30.72 24.05 28.86\n",
300 " 6.10 35.70 40.96 34.16 29.68\n",
301 " 7.00 49.40 48.64 43.31 33.17\n",
302 " 7.90 3.60 56.32 53.82 41.60\n",
303 " 9.10 81.50 66.57 69.92 64.39\n",
304 "10.20 99.50 75.95 86.80 101.01"
305 ]
306 },
307 "metadata": {},
308 "output_type": "display_data"
309 }
310 ],
311 "source": [
312 "import Text.Printf\n",
313 "\n",
314 "putStrLn \" x y p 1 p 2 p 3\"\n",
315 "putStrLn $ format \" \" (printf \"%.2f\") $ fromColumns view"
316 ]
317 },
318 {
319 "cell_type": "code",
320 "execution_count": 11,
321 "metadata": {
322 "collapsed": false
323 },
324 "outputs": [],
325 "source": [
326 "t = linspace 100 (minElement x, maxElement x)"
327 ]
328 },
329 {
330 "cell_type": "code",
331 "execution_count": 12,
332 "metadata": {
333 "collapsed": false
334 },
335 "outputs": [
336 {
337 "data": {
338 "text/html": [
339 "<style>/*\n",
340 "Custom IHaskell CSS.\n",
341 "*/\n",
342 "\n",
343 "/* Styles used for the Hoogle display in the pager */\n",
344 ".hoogle-doc {\n",
345 " display: block;\n",
346 " padding-bottom: 1.3em;\n",
347 " padding-left: 0.4em;\n",
348 "}\n",
349 ".hoogle-code {\n",
350 " display: block;\n",
351 " font-family: monospace;\n",
352 " white-space: pre;\n",
353 "}\n",
354 ".hoogle-text {\n",
355 " display: block;\n",
356 "}\n",
357 ".hoogle-name {\n",
358 " color: green;\n",
359 " font-weight: bold;\n",
360 "}\n",
361 ".hoogle-head {\n",
362 " font-weight: bold;\n",
363 "}\n",
364 ".hoogle-sub {\n",
365 " display: block;\n",
366 " margin-left: 0.4em;\n",
367 "}\n",
368 ".hoogle-package {\n",
369 " font-weight: bold;\n",
370 " font-style: italic;\n",
371 "}\n",
372 ".hoogle-module {\n",
373 " font-weight: bold;\n",
374 "}\n",
375 ".hoogle-class {\n",
376 " font-weight: bold;\n",
377 "}\n",
378 "\n",
379 "/* Styles used for basic displays */\n",
380 ".get-type {\n",
381 " color: green;\n",
382 " font-weight: bold;\n",
383 " font-family: monospace;\n",
384 " display: block;\n",
385 " white-space: pre-wrap;\n",
386 "}\n",
387 "\n",
388 ".show-type {\n",
389 " color: green;\n",
390 " font-weight: bold;\n",
391 " font-family: monospace;\n",
392 " margin-left: 1em;\n",
393 "}\n",
394 "\n",
395 ".mono {\n",
396 " font-family: monospace;\n",
397 " display: block;\n",
398 "}\n",
399 "\n",
400 ".err-msg {\n",
401 " color: red;\n",
402 " font-style: italic;\n",
403 " font-family: monospace;\n",
404 " white-space: pre;\n",
405 " display: block;\n",
406 "}\n",
407 "\n",
408 "#unshowable {\n",
409 " color: red;\n",
410 " font-weight: bold;\n",
411 "}\n",
412 "\n",
413 ".err-msg.in.collapse {\n",
414 " padding-top: 0.7em;\n",
415 "}\n",
416 "\n",
417 "/* Code that will get highlighted before it is highlighted */\n",
418 ".highlight-code {\n",
419 " white-space: pre;\n",
420 " font-family: monospace;\n",
421 "}\n",
422 "\n",
423 "/* Hlint styles */\n",
424 ".suggestion-warning { \n",
425 " font-weight: bold;\n",
426 " color: rgb(200, 130, 0);\n",
427 "}\n",
428 ".suggestion-error { \n",
429 " font-weight: bold;\n",
430 " color: red;\n",
431 "}\n",
432 ".suggestion-name {\n",
433 " font-weight: bold;\n",
434 "}\n",
435 "</style><div style=\"width:600px\"><?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
436 "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='0 0 600 400' >\n",
437 "\n",
438 "<g style='text-anchor:middle'>\n",
439 "<text x='300.000' y='30.000' style='font-size:14.0px'> polynomial models </text>\n",
440 "\n",
441 "</g>\n",
442 "\n",
443 "<g style='fill:white; stroke:none; stroke-width:1.0'>\n",
444 "<rect x='70.000' y='50.000' width='490.000' height='300.000' />\n",
445 "\n",
446 "</g>\n",
447 "\n",
448 "<g style='stroke:black;stroke-width:0.1;stroke-dasharray:2'>\n",
449 "<path d = 'M 144.961 350.000 144.961 50.000 ' />\n",
450 "<path d = 'M 240.758 350.000 240.758 50.000 ' />\n",
451 "<path d = 'M 336.554 350.000 336.554 50.000 ' />\n",
452 "<path d = 'M 432.351 350.000 432.351 50.000 ' />\n",
453 "<path d = 'M 528.148 350.000 528.148 50.000 ' />\n",
454 "\n",
455 "<path d = 'M 70.000 317.304 560.000 317.304 ' />\n",
456 "<path d = 'M 70.000 279.118 560.000 279.118 ' />\n",
457 "<path d = 'M 70.000 240.932 560.000 240.932 ' />\n",
458 "<path d = 'M 70.000 202.745 560.000 202.745 ' />\n",
459 "<path d = 'M 70.000 164.559 560.000 164.559 ' />\n",
460 "<path d = 'M 70.000 126.373 560.000 126.373 ' />\n",
461 "<path d = 'M 70.000 88.186 560.000 88.186 ' />\n",
462 "<path d = 'M 70.000 50.000 560.000 50.000 ' />\n",
463 "\n",
464 "\n",
465 "</g>\n",
466 "\n",
467 "\n",
468 "<defs> <clipPath id='clip7000050000490000300000'>\n",
469 "<rect x='70.000' y='50.000' width='490.000' height='300.000' />\n",
470 "</clipPath> </defs>\n",
471 "<g clip-path='url(#clip7000050000490000300000)'>\n",
472 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
473 "<path d = 'M 92.273 277.018 149.751 271.672 197.649 261.552 240.758 180.215 283.866 230.812 341.344 210.955 384.453 184.798 427.561 272.244 485.039 123.509 537.727 89.141 ' />\n",
474 "\n",
475 "</g>\n",
476 "\n",
477 "<g style='fill:red; stroke:none; stroke-width:1.0'>\n",
478 "<circle cx='92.273' cy='277.018' r='3.000' />\n",
479 "<circle cx='149.751' cy='271.672' r='3.000' />\n",
480 "<circle cx='197.649' cy='261.552' r='3.000' />\n",
481 "<circle cx='240.758' cy='180.215' r='3.000' />\n",
482 "<circle cx='283.866' cy='230.812' r='3.000' />\n",
483 "<circle cx='341.344' cy='210.955' r='3.000' />\n",
484 "<circle cx='384.453' cy='184.798' r='3.000' />\n",
485 "<circle cx='427.561' cy='272.244' r='3.000' />\n",
486 "<circle cx='485.039' cy='123.509' r='3.000' />\n",
487 "<circle cx='537.727' cy='89.141' r='3.000' />\n",
488 "\n",
489 "\n",
490 "</g>\n",
491 "\n",
492 "\n",
493 "<g style='fill:none; stroke:blue; stroke-width:1.0'>\n",
494 "<path d = 'M 92.273 285.630 96.772 284.099 101.272 282.569 105.771 281.038 110.271 279.508 114.770 277.977 119.270 276.447 123.770 274.916 128.269 273.385 132.769 271.855 137.268 270.324 141.768 268.794 146.267 267.263 150.767 265.732 155.266 264.202 159.766 262.671 164.265 261.141 168.765 259.610 173.264 258.079 177.764 256.549 182.264 255.018 186.763 253.488 191.263 251.957 195.762 250.426 200.262 248.896 204.761 247.365 209.261 245.835 213.760 244.304 218.260 242.774 222.759 241.243 227.259 239.712 231.758 238.182 236.258 236.651 240.758 235.121 245.257 233.590 249.757 232.059 254.256 230.529 258.756 228.998 263.255 227.468 267.755 225.937 272.254 224.406 276.754 222.876 281.253 221.345 285.753 219.815 290.253 218.284 294.752 216.753 299.252 215.223 303.751 213.692 308.251 212.162 312.750 210.631 317.250 209.100 321.749 207.570 326.249 206.039 330.748 204.509 335.248 202.978 339.747 201.448 344.247 199.917 348.747 198.386 353.246 196.856 357.746 195.325 362.245 193.795 366.745 192.264 371.244 190.733 375.744 189.203 380.243 187.672 384.743 186.142 389.242 184.611 393.742 183.080 398.242 181.550 402.741 180.019 407.241 178.489 411.740 176.958 416.240 175.427 420.739 173.897 425.239 172.366 429.738 170.836 434.238 169.305 438.737 167.775 443.237 166.244 447.736 164.713 452.236 163.183 456.736 161.652 461.235 160.122 465.735 158.591 470.234 157.060 474.734 155.530 479.233 153.999 483.733 152.469 488.232 150.938 492.732 149.407 497.231 147.877 501.731 146.346 506.230 144.816 510.730 143.285 515.230 141.754 519.729 140.224 524.229 138.693 528.728 137.163 533.228 135.632 537.727 134.102 ' />\n",
495 "\n",
496 "</g>\n",
497 "\n",
498 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
499 "\n",
500 "\n",
501 "</g>\n",
502 "\n",
503 "\n",
504 "<g style='fill:none; stroke:green; stroke-width:1.0'>\n",
505 "<path d = 'M 92.273 264.421 96.772 264.272 101.272 264.094 105.771 263.889 110.271 263.655 114.770 263.393 119.270 263.103 123.770 262.786 128.269 262.440 132.769 262.066 137.268 261.663 141.768 261.233 146.267 260.775 150.767 260.288 155.266 259.774 159.766 259.231 164.265 258.661 168.765 258.062 173.264 257.435 177.764 256.780 182.264 256.097 186.763 255.386 191.263 254.647 195.762 253.880 200.262 253.084 204.761 252.261 209.261 251.409 213.760 250.530 218.260 249.622 222.759 248.686 227.259 247.722 231.758 246.730 236.258 245.710 240.758 244.662 245.257 243.586 249.757 242.482 254.256 241.349 258.756 240.189 263.255 239.000 267.755 237.784 272.254 236.539 276.754 235.266 281.253 233.966 285.753 232.637 290.253 231.280 294.752 229.894 299.252 228.481 303.751 227.040 308.251 225.571 312.750 224.073 317.250 222.548 321.749 220.994 326.249 219.412 330.748 217.802 335.248 216.165 339.747 214.499 344.247 212.805 348.747 211.082 353.246 209.332 357.746 207.554 362.245 205.747 366.745 203.913 371.244 202.050 375.744 200.160 380.243 198.241 384.743 196.294 389.242 194.319 393.742 192.316 398.242 190.285 402.741 188.226 407.241 186.139 411.740 184.024 416.240 181.880 420.739 179.709 425.239 177.509 429.738 175.281 434.238 173.026 438.737 170.742 443.237 168.430 447.736 166.090 452.236 163.722 456.736 161.326 461.235 158.902 465.735 156.449 470.234 153.969 474.734 151.460 479.233 148.924 483.733 146.359 488.232 143.766 492.732 141.145 497.231 138.496 501.731 135.819 506.230 133.114 510.730 130.381 515.230 127.620 519.729 124.831 524.229 122.013 528.728 119.168 533.228 116.294 537.727 113.392 ' />\n",
506 "\n",
507 "</g>\n",
508 "\n",
509 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
510 "\n",
511 "\n",
512 "</g>\n",
513 "\n",
514 "\n",
515 "<g style='fill:none; stroke:brown; stroke-width:1.0'>\n",
516 "<path d = 'M 92.273 292.464 96.772 287.916 101.272 283.574 105.771 279.435 110.271 275.493 114.770 271.744 119.270 268.182 123.770 264.803 128.269 261.602 132.769 258.574 137.268 255.714 141.768 253.018 146.267 250.480 150.767 248.096 155.266 245.861 159.766 243.770 164.265 241.818 168.765 240.000 173.264 238.312 177.764 236.749 182.264 235.305 186.763 233.976 191.263 232.757 195.762 231.644 200.262 230.630 204.761 229.713 209.261 228.886 213.760 228.145 218.260 227.485 222.759 226.901 227.259 226.389 231.758 225.943 236.258 225.558 240.758 225.230 245.257 224.955 249.757 224.726 254.256 224.540 258.756 224.391 263.255 224.274 267.755 224.185 272.254 224.120 276.754 224.072 281.253 224.037 285.753 224.010 290.253 223.987 294.752 223.963 299.252 223.933 303.751 223.891 308.251 223.833 312.750 223.755 317.250 223.651 321.749 223.517 326.249 223.347 330.748 223.137 335.248 222.882 339.747 222.578 344.247 222.218 348.747 221.799 353.246 221.316 357.746 220.763 362.245 220.137 366.745 219.431 371.244 218.642 375.744 217.764 380.243 216.792 384.743 215.723 389.242 214.550 393.742 213.269 398.242 211.875 402.741 210.364 407.241 208.730 411.740 206.969 416.240 205.075 420.739 203.044 425.239 200.872 429.738 198.552 434.238 196.081 438.737 193.454 443.237 190.665 447.736 187.710 452.236 184.584 456.736 181.283 461.235 177.800 465.735 174.132 470.234 170.274 474.734 166.220 479.233 161.966 483.733 157.507 488.232 152.839 492.732 147.956 497.231 142.853 501.731 137.526 506.230 131.970 510.730 126.180 515.230 120.151 519.729 113.879 524.229 107.358 528.728 100.583 533.228 93.550 537.727 86.254 ' />\n",
517 "\n",
518 "</g>\n",
519 "\n",
520 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
521 "\n",
522 "\n",
523 "</g>\n",
524 "\n",
525 "\n",
526 "<g style='fill:none; stroke:gray; stroke-width:1.0'>\n",
527 "<path d = 'M 92.273 277.015 96.772 108.390 101.272 6.475 105.771 -44.591 110.271 -58.019 114.770 -44.689 119.270 -13.449 123.770 28.619 128.269 75.953 132.769 124.297 137.268 170.498 141.768 212.329 146.267 248.336 150.767 277.694 155.266 300.082 159.766 315.578 164.265 324.561 168.765 327.634 173.264 325.547 177.764 319.145 182.264 309.311 186.763 296.934 191.263 282.868 195.762 267.912 200.262 252.787 204.761 238.125 209.261 224.460 213.760 212.222 218.260 201.737 222.759 193.228 227.259 186.824 231.758 182.561 236.258 180.396 240.758 180.214 245.257 181.837 249.757 185.041 254.256 189.561 258.756 195.107 263.255 201.373 267.755 208.048 272.254 214.828 276.754 221.419 281.253 227.554 285.753 232.993 290.253 237.531 294.752 241.006 299.252 243.296 303.751 244.329 308.251 244.075 312.750 242.556 317.250 239.835 321.749 236.021 326.249 231.260 330.748 225.734 335.248 219.654 339.747 213.254 344.247 206.785 348.747 200.506 353.246 194.677 357.746 189.553 362.245 185.374 366.745 182.357 371.244 180.689 375.744 180.519 380.243 181.953 384.743 185.046 389.242 189.798 393.742 196.149 398.242 203.976 402.741 213.095 407.241 223.253 411.740 234.137 416.240 245.374 420.739 256.537 425.239 267.148 429.738 276.695 434.238 284.634 438.737 290.410 443.237 293.466 447.736 293.268 452.236 289.317 456.736 281.178 461.235 268.503 465.735 251.055 470.234 228.741 474.734 201.640 479.233 170.040 483.733 134.469 488.232 95.733 492.732 54.957 497.231 13.618 501.731 -26.407 506.230 -62.807 510.730 -92.788 515.230 -113.039 519.729 -119.686 524.229 -108.253 528.728 -73.622 533.228 -9.996 537.727 89.136 ' />\n",
528 "\n",
529 "</g>\n",
530 "\n",
531 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
532 "\n",
533 "\n",
534 "</g>\n",
535 "\n",
536 "\n",
537 "\n",
538 "\n",
539 "</g>\n",
540 "\n",
541 "<g style='fill:none; stroke:black; stroke-width:1.5'>\n",
542 "<rect x='70.000' y='50.000' width='490.000' height='300.000' />\n",
543 "\n",
544 "</g>\n",
545 "\n",
546 "<g style='text-anchor:middle'>\n",
547 "<text x='144.961' y='366.000' style='font-size:12.0px'> 2 </text>\n",
548 "<text x='240.758' y='366.000' style='font-size:12.0px'> 4 </text>\n",
549 "<text x='336.554' y='366.000' style='font-size:12.0px'> 6 </text>\n",
550 "<text x='432.351' y='366.000' style='font-size:12.0px'> 8 </text>\n",
551 "<text x='528.148' y='366.000' style='font-size:12.0px'> 10 </text>\n",
552 "\n",
553 "<text x='315.000' y='382.000' style='font-size:12.0px'> </text>\n",
554 "\n",
555 "</g>\n",
556 "\n",
557 "<g style='text-anchor:end'>\n",
558 "<text x='62.000' y='320.304' style='font-size:12.0px'> -20 </text>\n",
559 "<text x='62.000' y='282.118' style='font-size:12.0px'> 0 </text>\n",
560 "<text x='62.000' y='243.932' style='font-size:12.0px'> 20 </text>\n",
561 "<text x='62.000' y='205.745' style='font-size:12.0px'> 40 </text>\n",
562 "<text x='62.000' y='167.559' style='font-size:12.0px'> 60 </text>\n",
563 "<text x='62.000' y='129.373' style='font-size:12.0px'> 80 </text>\n",
564 "<text x='62.000' y='91.186' style='font-size:12.0px'> 100 </text>\n",
565 "<text x='62.000' y='53.000' style='font-size:12.0px'> 120 </text>\n",
566 "\n",
567 "</g>\n",
568 "\n",
569 "<g style='text-anchor:middle'>\n",
570 "<g transform='matrix(1,0,0,1,30.0,199.99999999999997)'>\n",
571 "<g transform='rotate(-90.0)'>\n",
572 "<g transform='matrix(1,0,0,1,-30.0,-199.99999999999997)'>\n",
573 "<text x='30.000' y='200.000' style='font-size:12.0px'> </text>\n",
574 "\n",
575 "</g>\n",
576 "\n",
577 "\n",
578 "</g>\n",
579 "\n",
580 "\n",
581 "</g>\n",
582 "\n",
583 "\n",
584 "</g>\n",
585 "\n",
586 "\n",
587 "<g style='stroke:black; stroke-width:1'>\n",
588 "<path d = 'M 144.961 350.000 144.961 344.000 ' />\n",
589 "<path d = 'M 240.758 350.000 240.758 344.000 ' />\n",
590 "<path d = 'M 336.554 350.000 336.554 344.000 ' />\n",
591 "<path d = 'M 432.351 350.000 432.351 344.000 ' />\n",
592 "<path d = 'M 528.148 350.000 528.148 344.000 ' />\n",
593 "\n",
594 "<path d = 'M 70.000 317.304 79.800 317.304 ' />\n",
595 "<path d = 'M 70.000 279.118 79.800 279.118 ' />\n",
596 "<path d = 'M 70.000 240.932 79.800 240.932 ' />\n",
597 "<path d = 'M 70.000 202.745 79.800 202.745 ' />\n",
598 "<path d = 'M 70.000 164.559 79.800 164.559 ' />\n",
599 "<path d = 'M 70.000 126.373 79.800 126.373 ' />\n",
600 "<path d = 'M 70.000 88.186 79.800 88.186 ' />\n",
601 "<path d = 'M 70.000 50.000 79.800 50.000 ' />\n",
602 "\n",
603 "\n",
604 "</g>\n",
605 "\n",
606 "\n",
607 "\n",
608 "<g style='fill:#fcfcff;stroke:gray'>\n",
609 "<rect x='89.500' y='44.600' width='113.600' height='122.400' />\n",
610 "\n",
611 "</g>\n",
612 "\n",
613 "<defs> <clipPath id='clip995004460030800122400'>\n",
614 "<rect x='99.500' y='44.600' width='30.800' height='122.400' />\n",
615 "</clipPath> </defs>\n",
616 "<g clip-path='url(#clip995004460030800122400)'>\n",
617 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
618 "<path d = 'M 94.500 65.000 114.900 65.000 135.300 65.000 ' />\n",
619 "\n",
620 "</g>\n",
621 "\n",
622 "<g style='fill:red; stroke:none; stroke-width:1.0'>\n",
623 "<circle cx='94.500' cy='65.000' r='3.000' />\n",
624 "<circle cx='114.900' cy='65.000' r='3.000' />\n",
625 "<circle cx='135.300' cy='65.000' r='3.000' />\n",
626 "\n",
627 "\n",
628 "</g>\n",
629 "\n",
630 "\n",
631 "<g style='fill:none; stroke:blue; stroke-width:1.0'>\n",
632 "<path d = 'M 94.500 85.400 114.900 85.400 135.300 85.400 ' />\n",
633 "\n",
634 "</g>\n",
635 "\n",
636 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
637 "\n",
638 "\n",
639 "</g>\n",
640 "\n",
641 "\n",
642 "<g style='fill:none; stroke:green; stroke-width:1.0'>\n",
643 "<path d = 'M 94.500 105.800 114.900 105.800 135.300 105.800 ' />\n",
644 "\n",
645 "</g>\n",
646 "\n",
647 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
648 "\n",
649 "\n",
650 "</g>\n",
651 "\n",
652 "\n",
653 "<g style='fill:none; stroke:brown; stroke-width:1.0'>\n",
654 "<path d = 'M 94.500 126.200 114.900 126.200 135.300 126.200 ' />\n",
655 "\n",
656 "</g>\n",
657 "\n",
658 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
659 "\n",
660 "\n",
661 "</g>\n",
662 "\n",
663 "\n",
664 "<g style='fill:none; stroke:gray; stroke-width:1.0'>\n",
665 "<path d = 'M 94.500 146.600 114.900 146.600 135.300 146.600 ' />\n",
666 "\n",
667 "</g>\n",
668 "\n",
669 "<g style='fill:none; stroke:none; stroke-width:1.0'>\n",
670 "\n",
671 "\n",
672 "</g>\n",
673 "\n",
674 "\n",
675 "\n",
676 "</g>\n",
677 "\n",
678 "<text x='140.300' y='68.000' style='font-size:12.0px'> data </text>\n",
679 "<text x='140.300' y='88.400' style='font-size:12.0px'> degree 1 </text>\n",
680 "<text x='140.300' y='108.800' style='font-size:12.0px'> degree 2 </text>\n",
681 "<text x='140.300' y='129.200' style='font-size:12.0px'> degree 3 </text>\n",
682 "<text x='140.300' y='149.600' style='font-size:12.0px'> degree 9 </text>\n",
683 "\n",
684 "\n",
685 "\n",
686 "\n",
687 "</svg>\n",
688 "</div>"
689 ]
690 },
691 "metadata": {},
692 "output_type": "display_data"
693 }
694 ],
695 "source": [
696 "lplot\n",
697 " [ plotMark x y \"none\" 1 circles \"red\" 3 \"data\"\n",
698 " , plot t (pol 1 t) \"blue\" 1 \"degree 1\"\n",
699 " , plot t (pol 2 t) \"green\" 1 \"degree 2\"\n",
700 " , plot t (pol 3 t) \"brown\" 1 \"degree 3\"\n",
701 " , plot t (pol 9 t) \"gray\" 1 \"degree 9\"\n",
702 " , MarginX 0.05, Title \"polynomial models\", LegendPos 0.05 0.95, MaxY 120\n",
703 " ] "
704 ]
705 }
706 ],
707 "metadata": {
708 "kernelspec": {
709 "display_name": "Haskell",
710 "language": "haskell",
711 "name": "haskell"
712 },
713 "language_info": {
714 "codemirror_mode": "ihaskell",
715 "file_extension": ".hs",
716 "name": "haskell",
717 "version": "7.10.1"
718 }
719 },
720 "nbformat": 4,
721 "nbformat_minor": 0
722}
diff --git a/examples/plot.hs b/examples/plot.hs
index f950aa5..90643ed 100644
--- a/examples/plot.hs
+++ b/examples/plot.hs
@@ -16,5 +16,5 @@ cumdist x = 0.5 * (1+ erf (x/sqrt 2))
16main = do 16main = do
17 let x = linspace 1000 (-4,4) 17 let x = linspace 1000 (-4,4)
18 mplot [f x] 18 mplot [f x]
19 mplot [x, mapVector cumdist x, mapVector gaussianPDF x] 19 mplot [x, cmap cumdist x, cmap gaussianPDF x]
20 mesh (sombrero 40) \ No newline at end of file 20 mesh (sombrero 40)
diff --git a/examples/repmat.ipynb b/examples/repmat.ipynb
new file mode 100644
index 0000000..afa9706
--- /dev/null
+++ b/examples/repmat.ipynb
@@ -0,0 +1,138 @@
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# repmat"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "An alternative implementation of `repmat` using the new in-place tools."
15 ]
16 },
17 {
18 "cell_type": "code",
19 "execution_count": 1,
20 "metadata": {
21 "collapsed": false
22 },
23 "outputs": [],
24 "source": [
25 ":ext FlexibleContexts\n",
26 "\n",
27 "import Numeric.LinearAlgebra\n",
28 "import Numeric.LinearAlgebra.Devel"
29 ]
30 },
31 {
32 "cell_type": "code",
33 "execution_count": 2,
34 "metadata": {
35 "collapsed": true
36 },
37 "outputs": [],
38 "source": [
39 "m = (3><4)[1..] :: Matrix Z"
40 ]
41 },
42 {
43 "cell_type": "code",
44 "execution_count": 3,
45 "metadata": {
46 "collapsed": false
47 },
48 "outputs": [
49 {
50 "data": {
51 "text/plain": [
52 "(3><4)\n",
53 " [ 1, 2, 3, 4\n",
54 " , 5, 6, 7, 8\n",
55 " , 9, 10, 11, 12 ]"
56 ]
57 },
58 "metadata": {},
59 "output_type": "display_data"
60 }
61 ],
62 "source": [
63 "m"
64 ]
65 },
66 {
67 "cell_type": "code",
68 "execution_count": 4,
69 "metadata": {
70 "collapsed": true
71 },
72 "outputs": [],
73 "source": [
74 "import Control.Monad.ST"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": 5,
80 "metadata": {
81 "collapsed": false
82 },
83 "outputs": [],
84 "source": [
85 "rpmt m i j = runST $ do\n",
86 " x <- newUndefinedMatrix RowMajor dr dc\n",
87 " sequence_ [ setMatrix x a b m | a <- [0,r..dr], b <-[0,c..dc] ]\n",
88 " unsafeFreezeMatrix x\n",
89 " where\n",
90 " (r,c) = size m\n",
91 " dr = i*r\n",
92 " dc = j*c"
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": 6,
98 "metadata": {
99 "collapsed": false
100 },
101 "outputs": [
102 {
103 "data": {
104 "text/plain": [
105 "(6><12)\n",
106 " [ 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4\n",
107 " , 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8\n",
108 " , 9, 10, 11, 12, 9, 10, 11, 12, 9, 10, 11, 12\n",
109 " , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4\n",
110 " , 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8\n",
111 " , 9, 10, 11, 12, 9, 10, 11, 12, 9, 10, 11, 12 ]"
112 ]
113 },
114 "metadata": {},
115 "output_type": "display_data"
116 }
117 ],
118 "source": [
119 "rpmt m 2 3"
120 ]
121 }
122 ],
123 "metadata": {
124 "kernelspec": {
125 "display_name": "Haskell",
126 "language": "haskell",
127 "name": "haskell"
128 },
129 "language_info": {
130 "codemirror_mode": "ihaskell",
131 "file_extension": ".hs",
132 "name": "haskell",
133 "version": "7.10.1"
134 }
135 },
136 "nbformat": 4,
137 "nbformat_minor": 0
138}
diff --git a/examples/root.hs b/examples/root.hs
index 8546ff5..fa6e77a 100644
--- a/examples/root.hs
+++ b/examples/root.hs
@@ -9,7 +9,7 @@ test method = do
9 print method 9 print method
10 let (s,p) = root method 1E-7 30 (rosenbrock 1 10) [-10,-5] 10 let (s,p) = root method 1E-7 30 (rosenbrock 1 10) [-10,-5]
11 print s -- solution 11 print s -- solution
12 disp p -- evolution of the algorithm 12 disp' p -- evolution of the algorithm
13 13
14jacobian a b [x,y] = [ [-a , 0] 14jacobian a b [x,y] = [ [-a , 0]
15 , [-2*b*x, b] ] 15 , [-2*b*x, b] ]
@@ -18,9 +18,9 @@ testJ method = do
18 print method 18 print method
19 let (s,p) = rootJ method 1E-7 30 (rosenbrock 1 10) (jacobian 1 10) [-10,-5] 19 let (s,p) = rootJ method 1E-7 30 (rosenbrock 1 10) (jacobian 1 10) [-10,-5]
20 print s 20 print s
21 disp p 21 disp' p
22 22
23disp = putStrLn . format " " (printf "%.3f") 23disp' = putStrLn . format " " (printf "%.3f")
24 24
25main = do 25main = do
26 test Hybrids 26 test Hybrids
diff --git a/examples/vector.hs b/examples/vector.hs
deleted file mode 100644
index f531cbd..0000000
--- a/examples/vector.hs
+++ /dev/null
@@ -1,31 +0,0 @@
1-- conversion to/from Data.Vector.Storable
2-- from Roman Leshchinskiy "vector" package
3--
4-- In the future Data.Packed.Vector will be replaced by Data.Vector.Storable
5
6-------------------------------------------
7
8import Numeric.LinearAlgebra as H
9import Data.Packed.Development(unsafeFromForeignPtr, unsafeToForeignPtr)
10import Foreign.Storable
11import qualified Data.Vector.Storable as V
12
13fromVector :: Storable t => V.Vector t -> H.Vector t
14fromVector v = unsafeFromForeignPtr p i n where
15 (p,i,n) = V.unsafeToForeignPtr v
16
17toVector :: Storable t => H.Vector t -> V.Vector t
18toVector v = V.unsafeFromForeignPtr p i n where
19 (p,i,n) = unsafeToForeignPtr v
20
21-------------------------------------------
22
23v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7)
24
25w = subVector 2 3 (linspace 5 (0,1)) :: Vector Double
26
27main = do
28 print v
29 print $ fromVector v
30 print w
31 print $ toVector w