summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/repmat.ipynb138
1 files changed, 138 insertions, 0 deletions
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}