calc-mtx.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. ;;; calc-mtx.el --- matrix functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; This file is autoloaded from calc-ext.el.
  19. (require 'calc-ext)
  20. (require 'calc-macs)
  21. (defun calc-mdet (arg)
  22. (interactive "P")
  23. (calc-slow-wrapper
  24. (calc-unary-op "mdet" 'calcFunc-det arg)))
  25. (defun calc-mtrace (arg)
  26. (interactive "P")
  27. (calc-slow-wrapper
  28. (calc-unary-op "mtr" 'calcFunc-tr arg)))
  29. (defun calc-mlud (arg)
  30. (interactive "P")
  31. (calc-slow-wrapper
  32. (calc-unary-op "mlud" 'calcFunc-lud arg)))
  33. ;;; Coerce row vector A to be a matrix. [V V]
  34. (defun math-row-matrix (a)
  35. (if (and (Math-vectorp a)
  36. (not (math-matrixp a)))
  37. (list 'vec a)
  38. a))
  39. ;;; Coerce column vector A to be a matrix. [V V]
  40. (defun math-col-matrix (a)
  41. (if (and (Math-vectorp a)
  42. (not (math-matrixp a)))
  43. (cons 'vec (mapcar (function (lambda (x) (list 'vec x))) (cdr a)))
  44. a))
  45. ;;; Multiply matrices A and B. [V V V]
  46. (defun math-mul-mats (a b)
  47. (let ((mat nil)
  48. (cols (length (nth 1 b)))
  49. row col ap bp accum)
  50. (while (setq a (cdr a))
  51. (setq col cols
  52. row nil)
  53. (while (> (setq col (1- col)) 0)
  54. (setq ap (cdr (car a))
  55. bp (cdr b)
  56. accum (math-mul (car ap) (nth col (car bp))))
  57. (while (setq ap (cdr ap) bp (cdr bp))
  58. (setq accum (math-add accum (math-mul (car ap) (nth col (car bp))))))
  59. (setq row (cons accum row)))
  60. (setq mat (cons (cons 'vec row) mat)))
  61. (cons 'vec (nreverse mat))))
  62. (defun math-mul-mat-vec (a b)
  63. (cons 'vec (mapcar (function (lambda (row)
  64. (math-dot-product row b)))
  65. (cdr a))))
  66. (defun calcFunc-tr (mat) ; [Public]
  67. (if (math-square-matrixp mat)
  68. (math-matrix-trace-step 2 (1- (length mat)) mat (nth 1 (nth 1 mat)))
  69. (math-reject-arg mat 'square-matrixp)))
  70. (defun math-matrix-trace-step (n size mat sum)
  71. (if (<= n size)
  72. (math-matrix-trace-step (1+ n) size mat
  73. (math-add sum (nth n (nth n mat))))
  74. sum))
  75. ;;; Matrix inverse and determinant.
  76. (defun math-matrix-inv-raw (m)
  77. (let ((n (1- (length m))))
  78. (if (<= n 3)
  79. (let ((det (math-det-raw m)))
  80. (and (not (math-zerop det))
  81. (math-div
  82. (cond ((= n 1) 1)
  83. ((= n 2)
  84. (list 'vec
  85. (list 'vec
  86. (nth 2 (nth 2 m))
  87. (math-neg (nth 2 (nth 1 m))))
  88. (list 'vec
  89. (math-neg (nth 1 (nth 2 m)))
  90. (nth 1 (nth 1 m)))))
  91. ((= n 3)
  92. (list 'vec
  93. (list 'vec
  94. (math-sub (math-mul (nth 3 (nth 3 m))
  95. (nth 2 (nth 2 m)))
  96. (math-mul (nth 3 (nth 2 m))
  97. (nth 2 (nth 3 m))))
  98. (math-sub (math-mul (nth 3 (nth 1 m))
  99. (nth 2 (nth 3 m)))
  100. (math-mul (nth 3 (nth 3 m))
  101. (nth 2 (nth 1 m))))
  102. (math-sub (math-mul (nth 3 (nth 2 m))
  103. (nth 2 (nth 1 m)))
  104. (math-mul (nth 3 (nth 1 m))
  105. (nth 2 (nth 2 m)))))
  106. (list 'vec
  107. (math-sub (math-mul (nth 3 (nth 2 m))
  108. (nth 1 (nth 3 m)))
  109. (math-mul (nth 3 (nth 3 m))
  110. (nth 1 (nth 2 m))))
  111. (math-sub (math-mul (nth 3 (nth 3 m))
  112. (nth 1 (nth 1 m)))
  113. (math-mul (nth 3 (nth 1 m))
  114. (nth 1 (nth 3 m))))
  115. (math-sub (math-mul (nth 3 (nth 1 m))
  116. (nth 1 (nth 2 m)))
  117. (math-mul (nth 3 (nth 2 m))
  118. (nth 1 (nth 1 m)))))
  119. (list 'vec
  120. (math-sub (math-mul (nth 2 (nth 3 m))
  121. (nth 1 (nth 2 m)))
  122. (math-mul (nth 2 (nth 2 m))
  123. (nth 1 (nth 3 m))))
  124. (math-sub (math-mul (nth 2 (nth 1 m))
  125. (nth 1 (nth 3 m)))
  126. (math-mul (nth 2 (nth 3 m))
  127. (nth 1 (nth 1 m))))
  128. (math-sub (math-mul (nth 2 (nth 2 m))
  129. (nth 1 (nth 1 m)))
  130. (math-mul (nth 2 (nth 1 m))
  131. (nth 1 (nth 2 m))))))))
  132. det)))
  133. (let ((lud (math-matrix-lud m)))
  134. (and lud
  135. (math-lud-solve lud (calcFunc-idn 1 n)))))))
  136. (defun calcFunc-det (m)
  137. (if (math-square-matrixp m)
  138. (math-with-extra-prec 2 (math-det-raw m))
  139. (if (and (eq (car-safe m) 'calcFunc-idn)
  140. (or (math-zerop (nth 1 m))
  141. (math-equal-int (nth 1 m) 1)))
  142. (nth 1 m)
  143. (math-reject-arg m 'square-matrixp))))
  144. ;; The variable math-det-lu is local to math-det-raw, but is
  145. ;; used by math-det-step, which is called by math-det-raw.
  146. (defvar math-det-lu)
  147. (defun math-det-raw (m)
  148. (let ((n (1- (length m))))
  149. (cond ((= n 1)
  150. (nth 1 (nth 1 m)))
  151. ((= n 2)
  152. (math-sub (math-mul (nth 1 (nth 1 m))
  153. (nth 2 (nth 2 m)))
  154. (math-mul (nth 2 (nth 1 m))
  155. (nth 1 (nth 2 m)))))
  156. ((= n 3)
  157. (math-sub
  158. (math-sub
  159. (math-sub
  160. (math-add
  161. (math-add
  162. (math-mul (nth 1 (nth 1 m))
  163. (math-mul (nth 2 (nth 2 m))
  164. (nth 3 (nth 3 m))))
  165. (math-mul (nth 2 (nth 1 m))
  166. (math-mul (nth 3 (nth 2 m))
  167. (nth 1 (nth 3 m)))))
  168. (math-mul (nth 3 (nth 1 m))
  169. (math-mul (nth 1 (nth 2 m))
  170. (nth 2 (nth 3 m)))))
  171. (math-mul (nth 3 (nth 1 m))
  172. (math-mul (nth 2 (nth 2 m))
  173. (nth 1 (nth 3 m)))))
  174. (math-mul (nth 1 (nth 1 m))
  175. (math-mul (nth 3 (nth 2 m))
  176. (nth 2 (nth 3 m)))))
  177. (math-mul (nth 2 (nth 1 m))
  178. (math-mul (nth 1 (nth 2 m))
  179. (nth 3 (nth 3 m))))))
  180. (t (let ((lud (math-matrix-lud m)))
  181. (if lud
  182. (let ((math-det-lu (car lud)))
  183. (math-det-step n (nth 2 lud)))
  184. 0))))))
  185. (defun math-det-step (n prod)
  186. (if (> n 0)
  187. (math-det-step (1- n) (math-mul prod (nth n (nth n math-det-lu))))
  188. prod))
  189. ;;; This returns a list (LU index d), or nil if not possible.
  190. ;;; Argument M must be a square matrix.
  191. (defvar math-lud-cache nil)
  192. (defun math-matrix-lud (m)
  193. (let ((old (assoc m math-lud-cache))
  194. (context (list calc-internal-prec calc-prefer-frac)))
  195. (if (and old (equal (nth 1 old) context))
  196. (cdr (cdr old))
  197. (let* ((lud (catch 'singular (math-do-matrix-lud m)))
  198. (entry (cons context lud)))
  199. (if old
  200. (setcdr old entry)
  201. (setq math-lud-cache (cons (cons m entry) math-lud-cache)))
  202. lud))))
  203. (defun math-lud-pivot-check (a)
  204. "Determine a useful value for checking the size of potential pivots
  205. in LUD decomposition."
  206. (cond ((eq (car-safe a) 'mod)
  207. (if (and (math-integerp (nth 1 a))
  208. (math-integerp (nth 2 a))
  209. (eq (math-gcd (nth 1 a) (nth 2 a)) 1))
  210. 1
  211. 0))
  212. (t
  213. (math-abs-approx a))))
  214. ;;; Numerical Recipes section 2.3; implicit pivoting omitted.
  215. (defun math-do-matrix-lud (m)
  216. (let* ((lu (math-copy-matrix m))
  217. (n (1- (length lu)))
  218. i (j 1) k imax sum big
  219. (d 1) (index nil))
  220. (while (<= j n)
  221. (setq i 1
  222. big 0
  223. imax j)
  224. (while (< i j)
  225. (math-working "LUD step" (format "%d/%d" j i))
  226. (setq sum (nth j (nth i lu))
  227. k 1)
  228. (while (< k i)
  229. (setq sum (math-sub sum (math-mul (nth k (nth i lu))
  230. (nth j (nth k lu))))
  231. k (1+ k)))
  232. (setcar (nthcdr j (nth i lu)) sum)
  233. (setq i (1+ i)))
  234. (while (<= i n)
  235. (math-working "LUD step" (format "%d/%d" j i))
  236. (setq sum (nth j (nth i lu))
  237. k 1)
  238. (while (< k j)
  239. (setq sum (math-sub sum (math-mul (nth k (nth i lu))
  240. (nth j (nth k lu))))
  241. k (1+ k)))
  242. (setcar (nthcdr j (nth i lu)) sum)
  243. (let ((dum (math-lud-pivot-check sum)))
  244. (if (Math-lessp big dum)
  245. (setq big dum
  246. imax i)))
  247. (setq i (1+ i)))
  248. (if (> imax j)
  249. (setq lu (math-swap-rows lu j imax)
  250. d (- d)))
  251. (setq index (cons imax index))
  252. (let ((pivot (nth j (nth j lu))))
  253. (if (math-zerop pivot)
  254. (throw 'singular nil)
  255. (setq i j)
  256. (while (<= (setq i (1+ i)) n)
  257. (setcar (nthcdr j (nth i lu))
  258. (math-div (nth j (nth i lu)) pivot)))))
  259. (setq j (1+ j)))
  260. (list lu (nreverse index) d)))
  261. (defun math-swap-rows (m r1 r2)
  262. (or (= r1 r2)
  263. (let* ((r1prev (nthcdr (1- r1) m))
  264. (row1 (cdr r1prev))
  265. (r2prev (nthcdr (1- r2) m))
  266. (row2 (cdr r2prev))
  267. (r2next (cdr row2)))
  268. (setcdr r2prev row1)
  269. (setcdr r1prev row2)
  270. (setcdr row2 (cdr row1))
  271. (setcdr row1 r2next)))
  272. m)
  273. (defun math-lud-solve (lud b &optional need)
  274. (if lud
  275. (let* ((x (math-copy-matrix b))
  276. (n (1- (length x)))
  277. (m (1- (length (nth 1 x))))
  278. (lu (car lud))
  279. (col 1)
  280. i j ip ii index sum)
  281. (while (<= col m)
  282. (math-working "LUD solver step" col)
  283. (setq i 1
  284. ii nil
  285. index (nth 1 lud))
  286. (while (<= i n)
  287. (setq ip (car index)
  288. index (cdr index)
  289. sum (nth col (nth ip x)))
  290. (setcar (nthcdr col (nth ip x)) (nth col (nth i x)))
  291. (if (null ii)
  292. (or (math-zerop sum)
  293. (setq ii i))
  294. (setq j ii)
  295. (while (< j i)
  296. (setq sum (math-sub sum (math-mul (nth j (nth i lu))
  297. (nth col (nth j x))))
  298. j (1+ j))))
  299. (setcar (nthcdr col (nth i x)) sum)
  300. (setq i (1+ i)))
  301. (while (>= (setq i (1- i)) 1)
  302. (setq sum (nth col (nth i x))
  303. j i)
  304. (while (<= (setq j (1+ j)) n)
  305. (setq sum (math-sub sum (math-mul (nth j (nth i lu))
  306. (nth col (nth j x))))))
  307. (setcar (nthcdr col (nth i x))
  308. (math-div sum (nth i (nth i lu)))))
  309. (setq col (1+ col)))
  310. x)
  311. (and need
  312. (math-reject-arg need "*Singular matrix"))))
  313. (defun calcFunc-lud (m)
  314. (if (math-square-matrixp m)
  315. (or (math-with-extra-prec 2
  316. (let ((lud (math-matrix-lud m)))
  317. (and lud
  318. (let* ((lmat (math-copy-matrix (car lud)))
  319. (umat (math-copy-matrix (car lud)))
  320. (n (1- (length (car lud))))
  321. (perm (calcFunc-idn 1 n))
  322. i (j 1))
  323. (while (<= j n)
  324. (setq i 1)
  325. (while (< i j)
  326. (setcar (nthcdr j (nth i lmat)) 0)
  327. (setq i (1+ i)))
  328. (setcar (nthcdr j (nth j lmat)) 1)
  329. (while (<= (setq i (1+ i)) n)
  330. (setcar (nthcdr j (nth i umat)) 0))
  331. (setq j (1+ j)))
  332. (while (>= (setq j (1- j)) 1)
  333. (let ((pos (nth (1- j) (nth 1 lud))))
  334. (or (= pos j)
  335. (setq perm (math-swap-rows perm j pos)))))
  336. (list 'vec perm lmat umat)))))
  337. (math-reject-arg m "*Singular matrix"))
  338. (math-reject-arg m 'square-matrixp)))
  339. (provide 'calc-mtx)
  340. ;;; calc-mtx.el ends here