You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(tuples (sort (fn (x y) (< (last car.x) (last car.y))) lis) ; if there is more than one index: collect into tuples of the rightmost index and list the answers of rec on each tuple
48
+
(len:keep [is (last car._) 0] lis))))) ;
49
+
50
+
(rec key-val-lis))))
51
+
52
+
(def ident-matrix (rank size (o table? nil))
53
+
(let M (mat-to-table (zeros (n-of rank size)))
54
+
(for i 0 (- size 1)
55
+
(= (M (n-of size i)) 1))
56
+
(if table? M
57
+
(table-to-mat M))))
40
58
41
59
(def matrix-multiply (a b)
42
-
"multiplies the matrix a by the matrix b (N.B. Rank one row vectors need to be in the form ((a b c d ...)) /NOT/ (a b c d), i.e. initiated with dims=(1 n) not (n))"
60
+
"multiplies the matrix a by the matrix b (N.B. Rank one row vectors need to be in the form ((a b c d ...)) /NOT/ (a b c d), i.e. initiated with dims=(1 n) not (n))"
using gaussian elimination and returns a list of x's (N.B. not efficient for large sparce matrices)"
62
80
(zap flat rhs)
63
-
(withs (N (if (no (is len.mat (len car.mat))) (err "mat must be a square matrix, use co-effs of 0 for equations which dont have a certain variable in them")
64
-
len.mat)
65
-
tmp (list)
66
-
MAX 0
67
-
i 0 j 0 k 0
68
-
X (zeros N))
69
-
(for i 0 (- N 1) (push (join mat.i (cons rhs.i ())) tmp))
70
-
(let M rev.tmp
71
-
;elimination step
72
-
(loop (= i 0) (<= i (- N 1)) (++ i)
73
-
(= MAX i)
74
-
(loop (= j (+ i 1)) (<= j (- N 1)) (++ j)
75
-
(if (> (abs:elt M (j i)) (abs:elt M (MAX i))) (= MAX j)))
76
-
(if (isnt MAX i) (swap M.i M.MAX))
77
-
(loop (= j (+ i 1)) (<= j (- N 1)) (++ j)
78
-
(loop (= k N) (>= k i) (-- k)
79
-
(-- (elt M (j k)) (/ (* (elt M (i k)) (elt M (j i))) (elt M (i i))))))) ;;end of elimination step
80
-
;;substitution
81
-
(loop (= j (- N 1)) (>= j 0) (-- j)
82
-
(= tmp 0.0)
83
-
(loop (= k (+ j 1)) (<= k(- N 1)) (++ k) (++ tmp (* (elt M (j k)) X.k)))
84
-
(= X.j (/ (- (elt M (j N)) tmp) (elt M (j j))))))
85
-
X))
86
-
81
+
(if (acons mat)
82
+
(withs (N (if (no (is len.mat (len car.mat))) (err "mat must be a square matrix, use co-effs of 0 for equations which dont have a certain variable in them")
83
+
len.mat)
84
+
tmp (list)
85
+
MAX 0
86
+
i 0 j 0 k 0
87
+
X (zeros N))
88
+
(for i 0 (- N 1) (push (join mat.i (cons rhs.i ())) tmp))
89
+
(let M rev.tmp
90
+
;;elimination step
91
+
(loop (= i 0) (<= i (- N 1)) (++ i)
92
+
(= MAX i)
93
+
(loop (= j (+ i 1)) (<= j (- N 1)) (++ j)
94
+
(if (> (abs:elt M (i j)) (abs:elt M (i MAX))) (= MAX j)))
95
+
(if (isnt MAX i) (swap M.i M.MAX))
96
+
(loop (= j (+ i 1)) (<= j (- N 1)) (++ j)
97
+
(loop (= k N) (>= k i) (-- k)
98
+
(-- (elt M (k j)) (/ (* (elt M (k i)) (elt M (i j))) (elt M (i i))))))) ;;end of elimination step
99
+
;;substitution
100
+
(loop (= j (- N 1)) (>= j 0) (-- j)
101
+
(= tmp 0.0)
102
+
(loop (= k (+ j 1)) (<= k(- N 1)) (++ k) (++ tmp (* (elt M (k j)) X.k)))
103
+
(= X.j (/ (- (elt M (N j)) tmp) (elt M (j j))))))
104
+
X)
105
+
(do (zap table-to-mat mat)
106
+
(withs (N (if (no (is len.mat (len car.mat))) (err "mat must be a square matrix, use co-effs of 0 for equations which dont have a certain variable in them")
107
+
len.mat)
108
+
tmp (list)
109
+
MAX 0
110
+
i 0 j 0 k 0
111
+
X (zeros N))
112
+
(for i 0 (- N 1) (push (join mat.i (cons rhs.i ())) tmp))
113
+
(let M (mat-to-table rev.tmp)
114
+
;;elimination step
115
+
(loop (= i 0) (<= i (- N 1)) (++ i)
116
+
(= MAX i)
117
+
(loop (= j (+ i 1)) (<= j (- N 1)) (++ j)
118
+
(if (> (abs:M (list i j)) (abs:M (list i MAX))) (= MAX j)))
119
+
(if (isnt MAX i) (let temp ()
120
+
(for a 0 N
121
+
(= temp (M (list a i)))
122
+
(= (M (list a i)) (M (list a MAX)))
123
+
(= (M (list a MAX)) temp))))
124
+
(loop (= j (+ i 1)) (<= j (- N 1)) (++ j)
125
+
(loop (= k N) (>= k i) (-- k)
126
+
(-- (M (list k j)) (/ (* (M (list k i)) (M (list i j))) (M (list i i))))))) ;;end of elimination step
127
+
;;substitution
128
+
(loop (= j (- N 1)) (>= j 0) (-- j)
129
+
(= tmp 0.0)
130
+
(loop (= k (+ j 1)) (<= k(- N 1)) (++ k) (++ tmp (* (M (list k j)) X.k)))
0 commit comments