calc-vec.el 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. ;;; calc-vec.el --- vector 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. ;; Declare functions which are defined elsewhere.
  22. (declare-function math-read-expr-level "calc-aent" (exp-prec &optional exp-term))
  23. (defun calc-display-strings (n)
  24. (interactive "P")
  25. (calc-wrapper
  26. (message (if (calc-change-mode 'calc-display-strings n t t)
  27. "Displaying vectors of integers as quoted strings"
  28. "Displaying vectors of integers normally"))))
  29. (defun calc-pack (n)
  30. (interactive "P")
  31. (calc-wrapper
  32. (let* ((nn (if n 1 2))
  33. (mode (if n (prefix-numeric-value n) (calc-top-n 1)))
  34. (mode (if (and (Math-vectorp mode) (cdr mode)) (cdr mode)
  35. (if (integerp mode) mode
  36. (error "Packing mode must be an integer or vector of integers"))))
  37. (num (calc-pack-size mode))
  38. (items (calc-top-list num nn)))
  39. (calc-enter-result (+ nn num -1) "pack" (calc-pack-items mode items)))))
  40. (defun calc-pack-size (mode)
  41. (cond ((consp mode)
  42. (let ((size 1))
  43. (while mode
  44. (or (integerp (car mode)) (error "Vector of integers expected"))
  45. (setq size (* size (calc-pack-size (car mode)))
  46. mode (cdr mode)))
  47. (if (= size 0)
  48. (error "Zero dimensions not allowed")
  49. size)))
  50. ((>= mode 0) mode)
  51. (t (or (cdr (assq mode '((-3 . 3) (-13 . 1) (-14 . 3) (-15 . 6))))
  52. 2))))
  53. (defun calc-pack-items (mode items)
  54. (cond ((consp mode)
  55. (if (cdr mode)
  56. (let* ((size (calc-pack-size (cdr mode)))
  57. (len (length items))
  58. (new nil)
  59. p row)
  60. (while (> len 0)
  61. (setq p (nthcdr (1- size) items)
  62. row items
  63. items (cdr p)
  64. len (- len size))
  65. (setcdr p nil)
  66. (setq new (cons (calc-pack-items (cdr mode) row) new)))
  67. (calc-pack-items (car mode) (nreverse new)))
  68. (calc-pack-items (car mode) items)))
  69. ((>= mode 0)
  70. (cons 'vec items))
  71. ((= mode -3)
  72. (if (and (math-objvecp (car items))
  73. (math-objvecp (nth 1 items))
  74. (math-objvecp (nth 2 items)))
  75. (if (and (math-num-integerp (car items))
  76. (math-num-integerp (nth 1 items)))
  77. (if (math-realp (nth 2 items))
  78. (cons 'hms items)
  79. (error "Seconds must be real"))
  80. (error "Hours and minutes must be integers"))
  81. (math-normalize (list '+
  82. (list '+
  83. (if (eq calc-angle-mode 'rad)
  84. (list '* (car items)
  85. '(hms 1 0 0))
  86. (car items))
  87. (list '* (nth 1 items) '(hms 0 1 0)))
  88. (list '* (nth 2 items) '(hms 0 0 1))))))
  89. ((= mode -13)
  90. (if (math-realp (car items))
  91. (cons 'date items)
  92. (if (eq (car-safe (car items)) 'date)
  93. (car items)
  94. (if (math-objvecp (car items))
  95. (error "Date value must be real")
  96. (cons 'calcFunc-date items)))))
  97. ((memq mode '(-14 -15))
  98. (let ((p items))
  99. (while (and p (math-objvecp (car p)))
  100. (or (math-integerp (car p))
  101. (error "Components must be integers"))
  102. (setq p (cdr p)))
  103. (if p
  104. (cons 'calcFunc-date items)
  105. (list 'date (math-dt-to-date items)))))
  106. ((or (eq (car-safe (car items)) 'vec)
  107. (eq (car-safe (nth 1 items)) 'vec))
  108. (let* ((x (car items))
  109. (vx (eq (car-safe x) 'vec))
  110. (y (nth 1 items))
  111. (vy (eq (car-safe y) 'vec))
  112. (z nil)
  113. (n (1- (length (if vx x y)))))
  114. (and vx vy
  115. (/= n (1- (length y)))
  116. (error "Vectors must be the same length"))
  117. (while (>= (setq n (1- n)) 0)
  118. (setq z (cons (calc-pack-items
  119. mode
  120. (list (if vx (car (setq x (cdr x))) x)
  121. (if vy (car (setq y (cdr y))) y)))
  122. z)))
  123. (cons 'vec (nreverse z))))
  124. ((= mode -1)
  125. (if (and (math-realp (car items)) (math-realp (nth 1 items)))
  126. (cons 'cplx items)
  127. (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  128. (error "Components must be real"))
  129. (math-normalize (list '+ (car items)
  130. (list '* (nth 1 items) '(cplx 0 1))))))
  131. ((= mode -2)
  132. (if (and (math-realp (car items)) (math-anglep (nth 1 items)))
  133. (cons 'polar items)
  134. (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  135. (error "Components must be real"))
  136. (math-normalize (list '* (car items)
  137. (if (math-anglep (nth 1 items))
  138. (list 'polar 1 (nth 1 items))
  139. (list 'calcFunc-exp
  140. (list '*
  141. (math-to-radians-2
  142. (nth 1 items))
  143. (list 'polar
  144. 1
  145. (math-quarter-circle
  146. nil)))))))))
  147. ((= mode -4)
  148. (let ((x (car items))
  149. (sigma (nth 1 items)))
  150. (if (or (math-scalarp x) (not (math-objvecp x)))
  151. (if (or (math-anglep sigma) (not (math-objvecp sigma)))
  152. (math-make-sdev x sigma)
  153. (error "Error component must be real"))
  154. (error "Mean component must be real or complex"))))
  155. ((= mode -5)
  156. (let ((a (car items))
  157. (m (nth 1 items)))
  158. (if (and (math-anglep a) (math-anglep m))
  159. (if (math-posp m)
  160. (math-make-mod a m)
  161. (error "Modulus must be positive"))
  162. (if (and (math-objectp a) (math-objectp m))
  163. (error "Components must be real"))
  164. (list 'calcFunc-makemod a m))))
  165. ((memq mode '(-6 -7 -8 -9))
  166. (let ((lo (car items))
  167. (hi (nth 1 items)))
  168. (if (and (or (math-anglep lo) (eq (car lo) 'date)
  169. (not (math-objvecp lo)))
  170. (or (math-anglep hi) (eq (car hi) 'date)
  171. (not (math-objvecp hi))))
  172. (math-make-intv (+ mode 9) lo hi)
  173. (error "Components must be real"))))
  174. ((eq mode -10)
  175. (if (math-zerop (nth 1 items))
  176. (error "Denominator must not be zero")
  177. (if (and (math-integerp (car items)) (math-integerp (nth 1 items)))
  178. (math-normalize (cons 'frac items))
  179. (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  180. (error "Components must be integers"))
  181. (cons 'calcFunc-fdiv items))))
  182. ((memq mode '(-11 -12))
  183. (if (and (math-realp (car items)) (math-integerp (nth 1 items)))
  184. (calcFunc-scf (math-float (car items)) (nth 1 items))
  185. (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  186. (error "Components must be integers"))
  187. (math-normalize
  188. (list 'calcFunc-scf
  189. (list 'calcFunc-float (car items))
  190. (nth 1 items)))))
  191. (t
  192. (error "Invalid packing mode: %d" mode))))
  193. (defvar calc-unpack-with-type nil)
  194. (defun calc-unpack (mode)
  195. (interactive "P")
  196. (calc-wrapper
  197. (let ((calc-unpack-with-type t))
  198. (calc-pop-push-record-list 1 "unpk" (calc-unpack-item
  199. (and mode
  200. (prefix-numeric-value mode))
  201. (calc-top))))))
  202. (defun calc-unpack-type (item)
  203. (cond ((eq (car-safe item) 'vec)
  204. (1- (length item)))
  205. ((eq (car-safe item) 'intv)
  206. (- (nth 1 item) 9))
  207. (t
  208. (or (cdr (assq (car-safe item) '( (cplx . -1) (polar . -2)
  209. (hms . -3) (sdev . -4) (mod . -5)
  210. (frac . -10) (float . -11)
  211. (date . -13) )))
  212. (error "Argument must be a composite object")))))
  213. (defun calc-unpack-item (mode item)
  214. (cond ((not mode)
  215. (if (or (and (not (memq (car-safe item) '(frac float cplx polar vec
  216. hms date sdev mod
  217. intv)))
  218. (math-objvecp item))
  219. (eq (car-safe item) 'var))
  220. (error "Argument must be a composite object or function call"))
  221. (if (eq (car item) 'intv)
  222. (cdr (cdr item))
  223. (cdr item)))
  224. ((> mode 0)
  225. (let ((dims nil)
  226. type new row)
  227. (setq item (list item))
  228. (while (> mode 0)
  229. (setq type (calc-unpack-type (car item))
  230. dims (cons type dims)
  231. new (calc-unpack-item nil (car item)))
  232. (while (setq item (cdr item))
  233. (or (= (calc-unpack-type (car item)) type)
  234. (error "Inconsistent types or dimensions in vector elements"))
  235. (setq new (append new (calc-unpack-item nil (car item)))))
  236. (setq item new
  237. mode (1- mode)))
  238. (if (cdr dims) (setq dims (list (cons 'vec (nreverse dims)))))
  239. (cond ((eq calc-unpack-with-type 'pair)
  240. (list (car dims) (cons 'vec item)))
  241. (calc-unpack-with-type
  242. (append item dims))
  243. (t item))))
  244. ((eq calc-unpack-with-type 'pair)
  245. (let ((calc-unpack-with-type nil))
  246. (list mode (cons 'vec (calc-unpack-item mode item)))))
  247. ((= mode -3)
  248. (if (eq (car-safe item) 'hms)
  249. (cdr item)
  250. (error "Argument must be an HMS form")))
  251. ((= mode -13)
  252. (if (eq (car-safe item) 'date)
  253. (cdr item)
  254. (error "Argument must be a date form")))
  255. ((= mode -14)
  256. (if (eq (car-safe item) 'date)
  257. (math-date-to-dt (math-floor (nth 1 item)))
  258. (error "Argument must be a date form")))
  259. ((= mode -15)
  260. (if (eq (car-safe item) 'date)
  261. (append (math-date-to-dt (nth 1 item))
  262. (and (not (math-integerp (nth 1 item)))
  263. (list 0 0 0)))
  264. (error "Argument must be a date form")))
  265. ((eq (car-safe item) 'vec)
  266. (let ((x nil)
  267. (y nil)
  268. res)
  269. (while (setq item (cdr item))
  270. (setq res (calc-unpack-item mode (car item))
  271. x (cons (car res) x)
  272. y (cons (nth 1 res) y)))
  273. (list (cons 'vec (nreverse x))
  274. (cons 'vec (nreverse y)))))
  275. ((= mode -1)
  276. (if (eq (car-safe item) 'cplx)
  277. (cdr item)
  278. (if (eq (car-safe item) 'polar)
  279. (cdr (math-complex item))
  280. (if (Math-realp item)
  281. (list item 0)
  282. (error "Argument must be a complex number")))))
  283. ((= mode -2)
  284. (if (or (memq (car-safe item) '(cplx polar))
  285. (Math-realp item))
  286. (cdr (math-polar item))
  287. (error "Argument must be a complex number")))
  288. ((= mode -4)
  289. (if (eq (car-safe item) 'sdev)
  290. (cdr item)
  291. (list item 0)))
  292. ((= mode -5)
  293. (if (eq (car-safe item) 'mod)
  294. (cdr item)
  295. (error "Argument must be a modulo form")))
  296. ((memq mode '(-6 -7 -8 -9))
  297. (if (eq (car-safe item) 'intv)
  298. (cdr (cdr item))
  299. (list item item)))
  300. ((= mode -10)
  301. (if (eq (car-safe item) 'frac)
  302. (cdr item)
  303. (if (Math-integerp item)
  304. (list item 1)
  305. (error "Argument must be a rational number"))))
  306. ((= mode -11)
  307. (if (eq (car-safe item) 'float)
  308. (list (nth 1 item) (math-normalize (nth 2 item)))
  309. (error "Expected a floating-point number")))
  310. ((= mode -12)
  311. (if (eq (car-safe item) 'float)
  312. (list (calcFunc-mant item) (calcFunc-xpon item))
  313. (error "Expected a floating-point number")))
  314. (t
  315. (error "Invalid unpacking mode: %d" mode))))
  316. (defun calc-diag (n)
  317. (interactive "P")
  318. (calc-wrapper
  319. (calc-enter-result 1 "diag" (if n
  320. (list 'calcFunc-diag (calc-top-n 1)
  321. (prefix-numeric-value n))
  322. (list 'calcFunc-diag (calc-top-n 1))))))
  323. (defun calc-ident (n)
  324. (interactive "NDimension of identity matrix = ")
  325. (calc-wrapper
  326. (calc-enter-result 0 "idn" (if (eq n 0)
  327. '(calcFunc-idn 1)
  328. (list 'calcFunc-idn 1
  329. (prefix-numeric-value n))))))
  330. (defun calc-index (n &optional stack)
  331. (interactive "NSize of vector = \nP")
  332. (calc-wrapper
  333. (if (consp stack)
  334. (calc-enter-result 3 "indx" (cons 'calcFunc-index (calc-top-list-n 3)))
  335. (calc-enter-result 0 "indx" (list 'calcFunc-index
  336. (prefix-numeric-value n))))))
  337. (defun calc-build-vector (n)
  338. (interactive "NSize of vector = ")
  339. (calc-wrapper
  340. (calc-enter-result 1 "bldv" (list 'calcFunc-cvec
  341. (calc-top-n 1)
  342. (prefix-numeric-value n)))))
  343. (defun calc-cons (arg)
  344. (interactive "P")
  345. (calc-wrapper
  346. (if (calc-is-hyperbolic)
  347. (calc-binary-op "rcns" 'calcFunc-rcons arg)
  348. (calc-binary-op "cons" 'calcFunc-cons arg))))
  349. (defun calc-head (arg)
  350. (interactive "P")
  351. (calc-wrapper
  352. (if (calc-is-inverse)
  353. (if (calc-is-hyperbolic)
  354. (calc-unary-op "rtai" 'calcFunc-rtail arg)
  355. (calc-unary-op "tail" 'calcFunc-tail arg))
  356. (if (calc-is-hyperbolic)
  357. (calc-unary-op "rhed" 'calcFunc-rhead arg)
  358. (calc-unary-op "head" 'calcFunc-head arg)))))
  359. (defun calc-tail (arg)
  360. (interactive "P")
  361. (calc-invert-func)
  362. (calc-head arg))
  363. (defun calc-vlength (arg)
  364. (interactive "P")
  365. (calc-wrapper
  366. (if (calc-is-hyperbolic)
  367. (calc-unary-op "dims" 'calcFunc-mdims arg)
  368. (calc-unary-op "len" 'calcFunc-vlen arg))))
  369. (defun calc-arrange-vector (n)
  370. (interactive "NNumber of columns = ")
  371. (calc-wrapper
  372. (calc-enter-result 1 "arng" (list 'calcFunc-arrange (calc-top-n 1)
  373. (prefix-numeric-value n)))))
  374. (defun calc-vector-find (arg)
  375. (interactive "P")
  376. (calc-wrapper
  377. (let ((func (cons 'calcFunc-find (calc-top-list-n 2))))
  378. (calc-enter-result
  379. 2 "find"
  380. (if arg (append func (list (prefix-numeric-value arg))) func)))))
  381. (defun calc-subvector ()
  382. (interactive)
  383. (calc-wrapper
  384. (if (calc-is-inverse)
  385. (calc-enter-result 3 "rsvc" (cons 'calcFunc-rsubvec
  386. (calc-top-list-n 3)))
  387. (calc-enter-result 3 "svec" (cons 'calcFunc-subvec (calc-top-list-n 3))))))
  388. (defun calc-reverse-vector (arg)
  389. (interactive "P")
  390. (calc-wrapper
  391. (calc-unary-op "rev" 'calcFunc-rev arg)))
  392. (defun calc-mask-vector (arg)
  393. (interactive "P")
  394. (calc-wrapper
  395. (calc-binary-op "vmsk" 'calcFunc-vmask arg)))
  396. (defun calc-expand-vector (arg)
  397. (interactive "P")
  398. (calc-wrapper
  399. (if (calc-is-hyperbolic)
  400. (calc-enter-result 3 "vexp" (cons 'calcFunc-vexp (calc-top-list-n 3)))
  401. (calc-binary-op "vexp" 'calcFunc-vexp arg))))
  402. (defun calc-sort ()
  403. (interactive)
  404. (calc-slow-wrapper
  405. (if (calc-is-inverse)
  406. (calc-enter-result 1 "rsrt" (list 'calcFunc-rsort (calc-top-n 1)))
  407. (calc-enter-result 1 "sort" (list 'calcFunc-sort (calc-top-n 1))))))
  408. (defun calc-grade ()
  409. (interactive)
  410. (calc-slow-wrapper
  411. (if (calc-is-inverse)
  412. (calc-enter-result 1 "rgrd" (list 'calcFunc-rgrade (calc-top-n 1)))
  413. (calc-enter-result 1 "grad" (list 'calcFunc-grade (calc-top-n 1))))))
  414. (defun calc-histogram (n)
  415. (interactive "P")
  416. (unless (natnump n)
  417. (setq n (math-read-expr (read-string "Centers of bins: "))))
  418. (calc-slow-wrapper
  419. (if calc-hyperbolic-flag
  420. (calc-enter-result 2 "hist" (list 'calcFunc-histogram
  421. (calc-top-n 2)
  422. (calc-top-n 1)
  423. n))
  424. (calc-enter-result 1 "hist" (list 'calcFunc-histogram
  425. (calc-top-n 1)
  426. n)))))
  427. (defun calc-transpose (arg)
  428. (interactive "P")
  429. (calc-wrapper
  430. (calc-unary-op "trn" 'calcFunc-trn arg)))
  431. (defun calc-conj-transpose (arg)
  432. (interactive "P")
  433. (calc-wrapper
  434. (calc-unary-op "ctrn" 'calcFunc-ctrn arg)))
  435. (defun calc-cross (arg)
  436. (interactive "P")
  437. (calc-wrapper
  438. (calc-binary-op "cros" 'calcFunc-cross arg)))
  439. (defun calc-kron (arg)
  440. (interactive "P")
  441. (calc-wrapper
  442. (calc-binary-op "kron" 'calcFunc-kron arg)))
  443. (defun calc-remove-duplicates (arg)
  444. (interactive "P")
  445. (calc-wrapper
  446. (calc-unary-op "rdup" 'calcFunc-rdup arg)))
  447. (defun calc-set-union (arg)
  448. (interactive "P")
  449. (calc-wrapper
  450. (calc-binary-op "unio" 'calcFunc-vunion arg '(vec) 'calcFunc-rdup)))
  451. (defun calc-set-intersect (arg)
  452. (interactive "P")
  453. (calc-wrapper
  454. (calc-binary-op "intr" 'calcFunc-vint arg '(vec) 'calcFunc-rdup)))
  455. (defun calc-set-difference (arg)
  456. (interactive "P")
  457. (calc-wrapper
  458. (calc-binary-op "diff" 'calcFunc-vdiff arg '(vec) 'calcFunc-rdup)))
  459. (defun calc-set-xor (arg)
  460. (interactive "P")
  461. (calc-wrapper
  462. (calc-binary-op "xor" 'calcFunc-vxor arg '(vec) 'calcFunc-rdup)))
  463. (defun calc-set-complement (arg)
  464. (interactive "P")
  465. (calc-wrapper
  466. (calc-unary-op "cmpl" 'calcFunc-vcompl arg)))
  467. (defun calc-set-floor (arg)
  468. (interactive "P")
  469. (calc-wrapper
  470. (calc-unary-op "vflr" 'calcFunc-vfloor arg)))
  471. (defun calc-set-enumerate (arg)
  472. (interactive "P")
  473. (calc-wrapper
  474. (calc-unary-op "enum" 'calcFunc-venum arg)))
  475. (defun calc-set-span (arg)
  476. (interactive "P")
  477. (calc-wrapper
  478. (calc-unary-op "span" 'calcFunc-vspan arg)))
  479. (defun calc-set-cardinality (arg)
  480. (interactive "P")
  481. (calc-wrapper
  482. (calc-unary-op "card" 'calcFunc-vcard arg)))
  483. (defun calc-unpack-bits (arg)
  484. (interactive "P")
  485. (calc-wrapper
  486. (if (calc-is-inverse)
  487. (calc-unary-op "bpck" 'calcFunc-vpack arg)
  488. (calc-unary-op "bupk" 'calcFunc-vunpack arg))))
  489. (defun calc-pack-bits (arg)
  490. (interactive "P")
  491. (calc-invert-func)
  492. (calc-unpack-bits arg))
  493. (defun calc-rnorm (arg)
  494. (interactive "P")
  495. (calc-wrapper
  496. (calc-unary-op "rnrm" 'calcFunc-rnorm arg)))
  497. (defun calc-cnorm (arg)
  498. (interactive "P")
  499. (calc-wrapper
  500. (calc-unary-op "cnrm" 'calcFunc-cnorm arg)))
  501. (defun calc-mrow (n &optional nn)
  502. (interactive "NRow number: \nP")
  503. (calc-wrapper
  504. (if (consp nn)
  505. (calc-enter-result 2 "mrow" (cons 'calcFunc-mrow (calc-top-list-n 2)))
  506. (setq n (prefix-numeric-value n))
  507. (if (= n 0)
  508. (calc-enter-result 1 "getd" (list 'calcFunc-getdiag (calc-top-n 1)))
  509. (if (< n 0)
  510. (calc-enter-result 1 "rrow" (list 'calcFunc-mrrow
  511. (calc-top-n 1) (- n)))
  512. (calc-enter-result 1 "mrow" (list 'calcFunc-mrow
  513. (calc-top-n 1) n)))))))
  514. (defun calc-mcol (n &optional nn)
  515. (interactive "NColumn number: \nP")
  516. (calc-wrapper
  517. (if (consp nn)
  518. (calc-enter-result 2 "mcol" (cons 'calcFunc-mcol (calc-top-list-n 2)))
  519. (setq n (prefix-numeric-value n))
  520. (if (= n 0)
  521. (calc-enter-result 1 "getd" (list 'calcFunc-getdiag (calc-top-n 1)))
  522. (if (< n 0)
  523. (calc-enter-result 1 "rcol" (list 'calcFunc-mrcol
  524. (calc-top-n 1) (- n)))
  525. (calc-enter-result 1 "mcol" (list 'calcFunc-mcol
  526. (calc-top-n 1) n)))))))
  527. ;;;; Vectors.
  528. (defun calcFunc-mdims (m)
  529. (or (math-vectorp m)
  530. (math-reject-arg m 'vectorp))
  531. (cons 'vec (math-mat-dimens m)))
  532. ;;; Apply a function elementwise to vector A. [V X V; N X N] [Public]
  533. (defun math-map-vec (f a)
  534. (if (math-vectorp a)
  535. (cons 'vec (mapcar f (cdr a)))
  536. (funcall f a)))
  537. (defun math-dimension-error ()
  538. (calc-record-why "*Dimension error")
  539. (signal 'wrong-type-argument nil))
  540. ;;; Build a vector out of a list of objects. [Public]
  541. (defun calcFunc-vec (&rest objs)
  542. (cons 'vec objs))
  543. ;;; Build a constant vector or matrix. [Public]
  544. (defun calcFunc-cvec (obj &rest dims)
  545. (math-make-vec-dimen obj dims))
  546. (defun math-make-vec-dimen (obj dims)
  547. (if dims
  548. (if (natnump (car dims))
  549. (if (or (cdr dims)
  550. (not (math-numberp obj)))
  551. (cons 'vec (copy-sequence
  552. (make-list (car dims)
  553. (math-make-vec-dimen obj (cdr dims)))))
  554. (cons 'vec (make-list (car dims) obj)))
  555. (math-reject-arg (car dims) 'fixnatnump))
  556. obj))
  557. (defun calcFunc-head (vec)
  558. (if (and (Math-vectorp vec)
  559. (cdr vec))
  560. (nth 1 vec)
  561. (calc-record-why 'vectorp vec)
  562. (list 'calcFunc-head vec)))
  563. (defun calcFunc-tail (vec)
  564. (if (and (Math-vectorp vec)
  565. (cdr vec))
  566. (cons 'vec (cdr (cdr vec)))
  567. (calc-record-why 'vectorp vec)
  568. (list 'calcFunc-tail vec)))
  569. (defun calcFunc-cons (head tail)
  570. (if (Math-vectorp tail)
  571. (cons 'vec (cons head (cdr tail)))
  572. (calc-record-why 'vectorp tail)
  573. (list 'calcFunc-cons head tail)))
  574. (defun calcFunc-rhead (vec)
  575. (if (and (Math-vectorp vec)
  576. (cdr vec))
  577. (let ((vec (copy-sequence vec)))
  578. (setcdr (nthcdr (- (length vec) 2) vec) nil)
  579. vec)
  580. (calc-record-why 'vectorp vec)
  581. (list 'calcFunc-rhead vec)))
  582. (defun calcFunc-rtail (vec)
  583. (if (and (Math-vectorp vec)
  584. (cdr vec))
  585. (nth (1- (length vec)) vec)
  586. (calc-record-why 'vectorp vec)
  587. (list 'calcFunc-rtail vec)))
  588. (defun calcFunc-rcons (head tail)
  589. (if (Math-vectorp head)
  590. (append head (list tail))
  591. (calc-record-why 'vectorp head)
  592. (list 'calcFunc-rcons head tail)))
  593. ;;; Apply a function elementwise to vectors A and B. [O X O O] [Public]
  594. (defun math-map-vec-2 (f a b)
  595. (if (math-vectorp a)
  596. (if (math-vectorp b)
  597. (let ((v nil))
  598. (while (setq a (cdr a))
  599. (or (setq b (cdr b))
  600. (math-dimension-error))
  601. (setq v (cons (funcall f (car a) (car b)) v)))
  602. (if a (math-dimension-error))
  603. (cons 'vec (nreverse v)))
  604. (let ((v nil))
  605. (while (setq a (cdr a))
  606. (setq v (cons (funcall f (car a) b) v)))
  607. (cons 'vec (nreverse v))))
  608. (if (math-vectorp b)
  609. (let ((v nil))
  610. (while (setq b (cdr b))
  611. (setq v (cons (funcall f a (car b)) v)))
  612. (cons 'vec (nreverse v)))
  613. (funcall f a b))))
  614. ;;; "Reduce" a function over a vector (left-associatively). [O X V] [Public]
  615. (defun math-reduce-vec (f a)
  616. (if (math-vectorp a)
  617. (if (cdr a)
  618. (let ((accum (car (setq a (cdr a)))))
  619. (while (setq a (cdr a))
  620. (setq accum (funcall f accum (car a))))
  621. accum)
  622. 0)
  623. a))
  624. ;;; Reduce a function over the columns of matrix A. [V X V] [Public]
  625. (defun math-reduce-cols (f a)
  626. (if (math-matrixp a)
  627. (cons 'vec (math-reduce-cols-col-step f (cdr a) 1 (length (nth 1 a))))
  628. a))
  629. (defun math-reduce-cols-col-step (f a col cols)
  630. (and (< col cols)
  631. (cons (math-reduce-cols-row-step f (nth col (car a)) col (cdr a))
  632. (math-reduce-cols-col-step f a (1+ col) cols))))
  633. (defun math-reduce-cols-row-step (f tot col a)
  634. (if a
  635. (math-reduce-cols-row-step f
  636. (funcall f tot (nth col (car a)))
  637. col
  638. (cdr a))
  639. tot))
  640. (defun math-dot-product (a b)
  641. (if (setq a (cdr a) b (cdr b))
  642. (let ((accum (math-mul (car a) (car b))))
  643. (while (setq a (cdr a) b (cdr b))
  644. (setq accum (math-add accum (math-mul (car a) (car b)))))
  645. accum)
  646. 0))
  647. ;;; Return the number of elements in vector V. [Public]
  648. (defun calcFunc-vlen (v)
  649. (if (math-vectorp v)
  650. (1- (length v))
  651. (if (math-objectp v)
  652. 0
  653. (list 'calcFunc-vlen v))))
  654. ;;; Get the Nth row of a matrix.
  655. (defun calcFunc-mrow (mat n) ; [Public]
  656. (if (Math-vectorp n)
  657. (math-map-vec (function (lambda (x) (calcFunc-mrow mat x))) n)
  658. (if (and (eq (car-safe n) 'intv) (math-constp n))
  659. (calcFunc-subvec mat
  660. (math-add (nth 2 n) (if (memq (nth 1 n) '(2 3)) 0 1))
  661. (math-add (nth 3 n) (if (memq (nth 1 n) '(1 3)) 1 0)))
  662. (or (and (integerp (setq n (math-check-integer n)))
  663. (> n 0))
  664. (math-reject-arg n 'fixposintp))
  665. (or (Math-vectorp mat)
  666. (math-reject-arg mat 'vectorp))
  667. (or (nth n mat)
  668. (math-reject-arg n "*Index out of range")))))
  669. (defun calcFunc-subscr (mat n &optional m)
  670. (if (eq (car-safe mat) 'var) nil
  671. (setq mat (calcFunc-mrow mat n))
  672. (if m
  673. (if (math-num-integerp n)
  674. (calcFunc-mrow mat m)
  675. (calcFunc-mcol mat m))
  676. mat)))
  677. ;;; Get the Nth column of a matrix.
  678. (defun math-mat-col (mat n)
  679. (cons 'vec (mapcar (function (lambda (x) (elt x n))) (cdr mat))))
  680. (defun calcFunc-mcol (mat n) ; [Public]
  681. (if (Math-vectorp n)
  682. (calcFunc-trn
  683. (math-map-vec (function (lambda (x) (calcFunc-mcol mat x))) n))
  684. (if (and (eq (car-safe n) 'intv) (math-constp n))
  685. (if (math-matrixp mat)
  686. (math-map-vec (function (lambda (x) (calcFunc-mrow x n))) mat)
  687. (calcFunc-mrow mat n))
  688. (or (and (integerp (setq n (math-check-integer n)))
  689. (> n 0))
  690. (math-reject-arg n 'fixposintp))
  691. (or (Math-vectorp mat)
  692. (math-reject-arg mat 'vectorp))
  693. (or (if (math-matrixp mat)
  694. (and (< n (length (nth 1 mat)))
  695. (math-mat-col mat n))
  696. (nth n mat))
  697. (math-reject-arg n "*Index out of range")))))
  698. ;;; Remove the Nth row from a matrix.
  699. (defun math-mat-less-row (mat n)
  700. (if (<= n 0)
  701. (cdr mat)
  702. (cons (car mat)
  703. (math-mat-less-row (cdr mat) (1- n)))))
  704. (defun calcFunc-mrrow (mat n) ; [Public]
  705. (and (integerp (setq n (math-check-integer n)))
  706. (> n 0)
  707. (< n (length mat))
  708. (math-mat-less-row mat n)))
  709. ;;; Remove the Nth column from a matrix.
  710. (defun math-mat-less-col (mat n)
  711. (cons 'vec (mapcar (function (lambda (x) (math-mat-less-row x n)))
  712. (cdr mat))))
  713. (defun calcFunc-mrcol (mat n) ; [Public]
  714. (and (integerp (setq n (math-check-integer n)))
  715. (> n 0)
  716. (if (math-matrixp mat)
  717. (and (< n (length (nth 1 mat)))
  718. (math-mat-less-col mat n))
  719. (math-mat-less-row mat n))))
  720. (defun calcFunc-getdiag (mat) ; [Public]
  721. (if (math-square-matrixp mat)
  722. (cons 'vec (math-get-diag-step (cdr mat) 1))
  723. (calc-record-why 'square-matrixp mat)
  724. (list 'calcFunc-getdiag mat)))
  725. (defun math-get-diag-step (row n)
  726. (and row
  727. (cons (nth n (car row))
  728. (math-get-diag-step (cdr row) (1+ n)))))
  729. (defun math-transpose (mat) ; [Public]
  730. (let ((m nil)
  731. (col (length (nth 1 mat))))
  732. (while (> (setq col (1- col)) 0)
  733. (setq m (cons (math-mat-col mat col) m)))
  734. (cons 'vec m)))
  735. (defun calcFunc-trn (mat)
  736. (if (math-vectorp mat)
  737. (if (math-matrixp mat)
  738. (math-transpose mat)
  739. (math-col-matrix mat))
  740. (if (math-numberp mat)
  741. mat
  742. (math-reject-arg mat 'matrixp))))
  743. (defun calcFunc-ctrn (mat)
  744. (calcFunc-conj (calcFunc-trn mat)))
  745. (defun calcFunc-pack (mode els)
  746. (or (Math-vectorp els) (math-reject-arg els 'vectorp))
  747. (if (and (Math-vectorp mode) (cdr mode))
  748. (setq mode (cdr mode))
  749. (or (integerp mode) (math-reject-arg mode 'fixnump)))
  750. (condition-case err
  751. (if (= (calc-pack-size mode) (1- (length els)))
  752. (calc-pack-items mode (cdr els))
  753. (math-reject-arg els "*Wrong number of elements"))
  754. (error (math-reject-arg els (nth 1 err)))))
  755. (defun calcFunc-unpack (mode thing)
  756. (or (integerp mode) (math-reject-arg mode 'fixnump))
  757. (condition-case err
  758. (cons 'vec (calc-unpack-item mode thing))
  759. (error (math-reject-arg thing (nth 1 err)))))
  760. (defun calcFunc-unpackt (mode thing)
  761. (let ((calc-unpack-with-type 'pair))
  762. (calcFunc-unpack mode thing)))
  763. (defun calcFunc-arrange (vec cols) ; [Public]
  764. (setq cols (math-check-fixnum cols t))
  765. (if (math-vectorp vec)
  766. (let* ((flat (math-flatten-vector vec))
  767. (mat (list 'vec))
  768. next)
  769. (if (<= cols 0)
  770. (nconc mat flat)
  771. (while (>= (length flat) cols)
  772. (setq next (nthcdr cols flat))
  773. (setcdr (nthcdr (1- cols) flat) nil)
  774. (setq mat (nconc mat (list (cons 'vec flat)))
  775. flat next))
  776. (if flat
  777. (setq mat (nconc mat (list (cons 'vec flat)))))
  778. mat))))
  779. (defun math-flatten-vector (vec) ; [L V]
  780. (if (math-vectorp vec)
  781. (apply 'append (mapcar 'math-flatten-vector (cdr vec)))
  782. (list vec)))
  783. (defun calcFunc-vconcat (a b)
  784. (math-normalize (list '| a b)))
  785. (defun calcFunc-vconcatrev (a b)
  786. (math-normalize (list '| b a)))
  787. (defun calcFunc-append (v1 v2)
  788. (if (and (math-vectorp v1) (math-vectorp v2))
  789. (append v1 (cdr v2))
  790. (list 'calcFunc-append v1 v2)))
  791. (defun calcFunc-appendrev (v1 v2)
  792. (calcFunc-append v2 v1))
  793. ;;; Copy a matrix. [Public]
  794. (defun math-copy-matrix (m)
  795. (if (math-vectorp (nth 1 m))
  796. (cons 'vec (mapcar 'copy-sequence (cdr m)))
  797. (copy-sequence m)))
  798. ;;; Convert a scalar or vector into an NxN diagonal matrix. [Public]
  799. (defun calcFunc-diag (a &optional n)
  800. (and n (not (integerp n))
  801. (setq n (math-check-fixnum n)))
  802. (if (math-vectorp a)
  803. (if (and n (/= (length a) (1+ n)))
  804. (list 'calcFunc-diag a n)
  805. (if (math-matrixp a)
  806. (if (and n (/= (length (elt a 1)) (1+ n)))
  807. (list 'calcFunc-diag a n)
  808. a)
  809. (cons 'vec (math-diag-step (cdr a) 0 (1- (length a))))))
  810. (if n
  811. (cons 'vec (math-diag-step (make-list n a) 0 n))
  812. (list 'calcFunc-diag a))))
  813. (defun calcFunc-idn (a &optional n)
  814. (if n
  815. (if (math-vectorp a)
  816. (math-reject-arg a 'numberp)
  817. (calcFunc-diag a n))
  818. (if (integerp calc-matrix-mode)
  819. (calcFunc-idn a calc-matrix-mode)
  820. (list 'calcFunc-idn a))))
  821. (defun math-mimic-ident (a m)
  822. (if (math-square-matrixp m)
  823. (calcFunc-idn a (1- (length m)))
  824. (if (math-vectorp m)
  825. (if (math-zerop a)
  826. (cons 'vec (mapcar (function (lambda (x)
  827. (if (math-vectorp x)
  828. (math-mimic-ident a x)
  829. a)))
  830. (cdr m)))
  831. (math-dimension-error))
  832. (calcFunc-idn a))))
  833. (defun math-diag-step (a n m)
  834. (if (< n m)
  835. (cons (cons 'vec
  836. (nconc (make-list n 0)
  837. (cons (car a)
  838. (make-list (1- (- m n)) 0))))
  839. (math-diag-step (cdr a) (1+ n) m))
  840. nil))
  841. ;;; Create a vector of consecutive integers. [Public]
  842. (defun calcFunc-index (n &optional start incr)
  843. (if (math-messy-integerp n)
  844. (math-float (calcFunc-index (math-trunc n) start incr))
  845. (and (not (integerp n))
  846. (setq n (math-check-fixnum n)))
  847. (let ((vec nil))
  848. (if start
  849. (progn
  850. (if (>= n 0)
  851. (while (>= (setq n (1- n)) 0)
  852. (setq vec (cons start vec)
  853. start (math-add start (or incr 1))))
  854. (while (<= (setq n (1+ n)) 0)
  855. (setq vec (cons start vec)
  856. start (math-mul start (or incr 2)))))
  857. (setq vec (nreverse vec)))
  858. (if (>= n 0)
  859. (while (> n 0)
  860. (setq vec (cons n vec)
  861. n (1- n)))
  862. (let ((i -1))
  863. (while (>= i n)
  864. (setq vec (cons i vec)
  865. i (1- i))))))
  866. (cons 'vec vec))))
  867. ;;; Find an element in a vector.
  868. (defun calcFunc-find (vec x &optional start)
  869. (setq start (if start (math-check-fixnum start t) 1))
  870. (if (< start 1) (math-reject-arg start 'posp))
  871. (setq vec (nthcdr start vec))
  872. (let ((n start))
  873. (while (and vec (not (Math-equal x (car vec))))
  874. (setq n (1+ n)
  875. vec (cdr vec)))
  876. (if vec n 0)))
  877. ;;; Return a subvector of a vector.
  878. (defun calcFunc-subvec (vec start &optional end)
  879. (setq start (math-check-fixnum start t)
  880. end (math-check-fixnum (or end 0) t))
  881. (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  882. (let ((len (1- (length vec))))
  883. (if (<= start 0)
  884. (setq start (+ len start 1)))
  885. (if (<= end 0)
  886. (setq end (+ len end 1)))
  887. (if (or (> start len)
  888. (<= end start))
  889. '(vec)
  890. (setq vec (nthcdr start vec))
  891. (if (<= end len)
  892. (let ((chop (nthcdr (- end start 1) (setq vec (copy-sequence vec)))))
  893. (setcdr chop nil)))
  894. (cons 'vec vec))))
  895. ;;; Remove a subvector from a vector.
  896. (defun calcFunc-rsubvec (vec start &optional end)
  897. (setq start (math-check-fixnum start t)
  898. end (math-check-fixnum (or end 0) t))
  899. (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  900. (let ((len (1- (length vec))))
  901. (if (<= start 0)
  902. (setq start (+ len start 1)))
  903. (if (<= end 0)
  904. (setq end (+ len end 1)))
  905. (if (or (> start len)
  906. (<= end start))
  907. vec
  908. (let ((tail (nthcdr end vec))
  909. (chop (nthcdr (1- start) (setq vec (copy-sequence vec)))))
  910. (setcdr chop nil)
  911. (append vec tail)))))
  912. ;;; Reverse the order of the elements of a vector.
  913. (defun calcFunc-rev (vec)
  914. (if (math-vectorp vec)
  915. (cons 'vec (reverse (cdr vec)))
  916. (math-reject-arg vec 'vectorp)))
  917. ;;; Compress a vector according to a mask vector.
  918. (defun calcFunc-vmask (mask vec)
  919. (if (math-numberp mask)
  920. (if (math-zerop mask)
  921. '(vec)
  922. vec)
  923. (or (math-vectorp mask) (math-reject-arg mask 'vectorp))
  924. (or (math-constp mask) (math-reject-arg mask 'constp))
  925. (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  926. (or (= (length mask) (length vec)) (math-dimension-error))
  927. (let ((new nil))
  928. (while (setq mask (cdr mask) vec (cdr vec))
  929. (or (math-zerop (car mask))
  930. (setq new (cons (car vec) new))))
  931. (cons 'vec (nreverse new)))))
  932. ;;; Expand a vector according to a mask vector.
  933. (defun calcFunc-vexp (mask vec &optional filler)
  934. (or (math-vectorp mask) (math-reject-arg mask 'vectorp))
  935. (or (math-constp mask) (math-reject-arg mask 'constp))
  936. (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  937. (let ((new nil)
  938. (fvec (and filler (math-vectorp filler))))
  939. (while (setq mask (cdr mask))
  940. (if (math-zerop (car mask))
  941. (setq new (cons (or (if fvec
  942. (car (setq filler (cdr filler)))
  943. filler)
  944. (car mask)) new))
  945. (setq vec (cdr vec)
  946. new (cons (or (car vec) (car mask)) new))))
  947. (cons 'vec (nreverse new))))
  948. ;;; Compute the row and column norms of a vector or matrix. [Public]
  949. (defun calcFunc-rnorm (a)
  950. (if (and (Math-vectorp a)
  951. (math-constp a))
  952. (if (math-matrixp a)
  953. (math-reduce-vec 'math-max (math-map-vec 'calcFunc-cnorm a))
  954. (math-reduce-vec 'math-max (math-map-vec 'math-abs a)))
  955. (calc-record-why 'vectorp a)
  956. (list 'calcFunc-rnorm a)))
  957. (defun calcFunc-cnorm (a)
  958. (if (and (Math-vectorp a)
  959. (math-constp a))
  960. (if (math-matrixp a)
  961. (math-reduce-vec 'math-max
  962. (math-reduce-cols 'math-add-abs a))
  963. (math-reduce-vec 'math-add-abs a))
  964. (calc-record-why 'vectorp a)
  965. (list 'calcFunc-cnorm a)))
  966. (defun math-add-abs (a b)
  967. (math-add (math-abs a) (math-abs b)))
  968. ;;; Sort the elements of a vector into increasing order.
  969. (defun calcFunc-sort (vec) ; [Public]
  970. (if (math-vectorp vec)
  971. (cons 'vec (sort (copy-sequence (cdr vec)) 'math-beforep))
  972. (math-reject-arg vec 'vectorp)))
  973. (defun calcFunc-rsort (vec) ; [Public]
  974. (if (math-vectorp vec)
  975. (cons 'vec (nreverse (sort (copy-sequence (cdr vec)) 'math-beforep)))
  976. (math-reject-arg vec 'vectorp)))
  977. ;; The variable math-grade-vec is local to calcFunc-grade and
  978. ;; calcFunc-rgrade, but is used by math-grade-beforep, which is called
  979. ;; by calcFunc-grade and calcFunc-rgrade.
  980. (defvar math-grade-vec)
  981. (defun calcFunc-grade (math-grade-vec)
  982. (if (math-vectorp math-grade-vec)
  983. (let* ((len (1- (length math-grade-vec))))
  984. (cons 'vec (sort (cdr (calcFunc-index len)) 'math-grade-beforep)))
  985. (math-reject-arg math-grade-vec 'vectorp)))
  986. (defun calcFunc-rgrade (math-grade-vec)
  987. (if (math-vectorp math-grade-vec)
  988. (let* ((len (1- (length math-grade-vec))))
  989. (cons 'vec (nreverse (sort (cdr (calcFunc-index len))
  990. 'math-grade-beforep))))
  991. (math-reject-arg math-grade-vec 'vectorp)))
  992. (defun math-grade-beforep (i j)
  993. (math-beforep (nth i math-grade-vec) (nth j math-grade-vec)))
  994. ;;; Compile a histogram of data from a vector.
  995. (defun calcFunc-histogram (vec wts &optional n)
  996. (or n (setq n wts wts 1))
  997. (or (Math-vectorp vec)
  998. (math-reject-arg vec 'vectorp))
  999. (if (Math-vectorp wts)
  1000. (or (= (length vec) (length wts))
  1001. (math-dimension-error)))
  1002. (cond ((natnump n)
  1003. (let ((res (make-vector n 0))
  1004. (vp vec)
  1005. (wvec (Math-vectorp wts))
  1006. (wp wts)
  1007. bin)
  1008. (while (setq vp (cdr vp))
  1009. (setq bin (car vp))
  1010. (or (natnump bin)
  1011. (setq bin (math-floor bin)))
  1012. (and (natnump bin)
  1013. (< bin n)
  1014. (aset res bin
  1015. (math-add (aref res bin)
  1016. (if wvec (car (setq wp (cdr wp))) wts)))))
  1017. (cons 'vec (append res nil))))
  1018. ((Math-vectorp n) ;; n is a vector of midpoints
  1019. (let* ((bds (math-vector-avg n))
  1020. (res (make-vector (1- (length n)) 0))
  1021. (vp (cdr vec))
  1022. (wvec (Math-vectorp wts))
  1023. (wp wts)
  1024. num)
  1025. (while vp
  1026. (setq num (car vp))
  1027. (let ((tbds (cdr bds))
  1028. (i 0))
  1029. (while (and tbds (Math-lessp (car tbds) num))
  1030. (setq i (1+ i))
  1031. (setq tbds (cdr tbds)))
  1032. (aset res i
  1033. (math-add (aref res i)
  1034. (if wvec (car (setq wp (cdr wp))) wts))))
  1035. (setq vp (cdr vp)))
  1036. (cons 'vec (append res nil))))
  1037. (t
  1038. (math-reject-arg n "*Expecting an integer or vector"))))
  1039. ;;; Replace a vector [a b c ...] with a vector of averages
  1040. ;;; [(a+b)/2 (b+c)/2 ...]
  1041. (defun math-vector-avg (vec)
  1042. (let ((vp (sort (copy-sequence (cdr vec)) 'math-beforep))
  1043. (res nil))
  1044. (while (and vp (cdr vp))
  1045. (setq res (cons (math-div (math-add (car vp) (cadr vp)) 2) res)
  1046. vp (cdr vp)))
  1047. (cons 'vec (reverse res))))
  1048. ;;; Set operations.
  1049. (defun calcFunc-vunion (a b)
  1050. (if (Math-objectp a)
  1051. (setq a (list 'vec a))
  1052. (or (math-vectorp a) (math-reject-arg a 'vectorp)))
  1053. (if (Math-objectp b)
  1054. (setq b (list b))
  1055. (or (math-vectorp b) (math-reject-arg b 'vectorp))
  1056. (setq b (cdr b)))
  1057. (calcFunc-rdup (append a b)))
  1058. (defun calcFunc-vint (a b)
  1059. (if (and (math-simple-set a) (math-simple-set b))
  1060. (progn
  1061. (setq a (cdr (calcFunc-rdup a)))
  1062. (setq b (cdr (calcFunc-rdup b)))
  1063. (let ((vec (list 'vec)))
  1064. (while (and a b)
  1065. (if (math-beforep (car a) (car b))
  1066. (setq a (cdr a))
  1067. (if (Math-equal (car a) (car b))
  1068. (setq vec (cons (car a) vec)
  1069. a (cdr a)))
  1070. (setq b (cdr b))))
  1071. (nreverse vec)))
  1072. (calcFunc-vcompl (calcFunc-vunion (calcFunc-vcompl a)
  1073. (calcFunc-vcompl b)))))
  1074. (defun calcFunc-vdiff (a b)
  1075. (if (and (math-simple-set a) (math-simple-set b))
  1076. (progn
  1077. (setq a (cdr (calcFunc-rdup a)))
  1078. (setq b (cdr (calcFunc-rdup b)))
  1079. (let ((vec (list 'vec)))
  1080. (while a
  1081. (while (and b (math-beforep (car b) (car a)))
  1082. (setq b (cdr b)))
  1083. (if (and b (Math-equal (car a) (car b)))
  1084. (setq a (cdr a)
  1085. b (cdr b))
  1086. (setq vec (cons (car a) vec)
  1087. a (cdr a))))
  1088. (nreverse vec)))
  1089. (calcFunc-vcompl (calcFunc-vunion (calcFunc-vcompl a) b))))
  1090. (defun calcFunc-vxor (a b)
  1091. (if (and (math-simple-set a) (math-simple-set b))
  1092. (progn
  1093. (setq a (cdr (calcFunc-rdup a)))
  1094. (setq b (cdr (calcFunc-rdup b)))
  1095. (let ((vec (list 'vec)))
  1096. (while (or a b)
  1097. (if (and a
  1098. (or (not b)
  1099. (math-beforep (car a) (car b))))
  1100. (setq vec (cons (car a) vec)
  1101. a (cdr a))
  1102. (if (and a (Math-equal (car a) (car b)))
  1103. (setq a (cdr a))
  1104. (setq vec (cons (car b) vec)))
  1105. (setq b (cdr b))))
  1106. (nreverse vec)))
  1107. (let ((ca (calcFunc-vcompl a))
  1108. (cb (calcFunc-vcompl b)))
  1109. (calcFunc-vunion (calcFunc-vcompl (calcFunc-vunion ca b))
  1110. (calcFunc-vcompl (calcFunc-vunion a cb))))))
  1111. (defun calcFunc-vcompl (a)
  1112. (setq a (math-prepare-set a))
  1113. (let ((vec (list 'vec))
  1114. (prev '(neg (var inf var-inf)))
  1115. (closed 2))
  1116. (while (setq a (cdr a))
  1117. (or (and (equal (nth 2 (car a)) '(neg (var inf var-inf)))
  1118. (memq (nth 1 (car a)) '(2 3)))
  1119. (setq vec (cons (list 'intv
  1120. (+ closed
  1121. (if (memq (nth 1 (car a)) '(0 1)) 1 0))
  1122. prev
  1123. (nth 2 (car a)))
  1124. vec)))
  1125. (setq prev (nth 3 (car a))
  1126. closed (if (memq (nth 1 (car a)) '(0 2)) 2 0)))
  1127. (or (and (equal prev '(var inf var-inf))
  1128. (= closed 0))
  1129. (setq vec (cons (list 'intv (+ closed 1)
  1130. prev '(var inf var-inf))
  1131. vec)))
  1132. (math-clean-set (nreverse vec))))
  1133. (defun calcFunc-vspan (a)
  1134. (setq a (math-prepare-set a))
  1135. (if (cdr a)
  1136. (let ((last (nth (1- (length a)) a)))
  1137. (math-make-intv (+ (logand (nth 1 (nth 1 a)) 2)
  1138. (logand (nth 1 last) 1))
  1139. (nth 2 (nth 1 a))
  1140. (nth 3 last)))
  1141. '(intv 2 0 0)))
  1142. (defun calcFunc-vfloor (a &optional always-vec)
  1143. (setq a (math-prepare-set a))
  1144. (let ((vec (list 'vec)) (p a) (prev nil) b mask)
  1145. (while (setq p (cdr p))
  1146. (setq mask (nth 1 (car p))
  1147. a (nth 2 (car p))
  1148. b (nth 3 (car p)))
  1149. (and (memq mask '(0 1))
  1150. (not (math-infinitep a))
  1151. (setq mask (logior mask 2))
  1152. (math-num-integerp a)
  1153. (setq a (math-add a 1)))
  1154. (setq a (math-ceiling a))
  1155. (and (memq mask '(0 2))
  1156. (not (math-infinitep b))
  1157. (setq mask (logior mask 1))
  1158. (math-num-integerp b)
  1159. (setq b (math-sub b 1)))
  1160. (setq b (math-floor b))
  1161. (if (and prev (Math-equal (math-sub a 1) (nth 3 prev)))
  1162. (setcar (nthcdr 3 prev) b)
  1163. (or (Math-lessp b a)
  1164. (setq vec (cons (setq prev (list 'intv mask a b)) vec)))))
  1165. (setq vec (nreverse vec))
  1166. (math-clean-set vec always-vec)))
  1167. (defun calcFunc-vcard (a)
  1168. (setq a (calcFunc-vfloor a t))
  1169. (or (math-constp a) (math-reject-arg a "*Set must be finite"))
  1170. (let ((count 0))
  1171. (while (setq a (cdr a))
  1172. (if (eq (car-safe (car a)) 'intv)
  1173. (setq count (math-add count (math-sub (nth 3 (car a))
  1174. (nth 2 (car a))))))
  1175. (setq count (math-add count 1)))
  1176. count))
  1177. (defun calcFunc-venum (a)
  1178. (setq a (calcFunc-vfloor a t))
  1179. (or (math-constp a) (math-reject-arg a "*Set must be finite"))
  1180. (let* ((prev a) (this (cdr prev)) this-val next this-last)
  1181. (while this
  1182. (setq next (cdr this)
  1183. this-val (car this))
  1184. (if (eq (car-safe this-val) 'intv)
  1185. (progn
  1186. (setq this (cdr (calcFunc-index (math-add
  1187. (math-sub (nth 3 this-val)
  1188. (nth 2 this-val))
  1189. 1)
  1190. (nth 2 this-val))))
  1191. (setq this-last (last this))
  1192. (setcdr this-last next)
  1193. (setcdr prev this)
  1194. (setq prev this-last))
  1195. (setq prev this))
  1196. (setq this next)))
  1197. a)
  1198. (defun calcFunc-vpack (a)
  1199. (setq a (calcFunc-vfloor a t))
  1200. (if (and (cdr a)
  1201. (math-negp (if (eq (car-safe (nth 1 a)) 'intv)
  1202. (nth 2 (nth 1 a))
  1203. (nth 1 a))))
  1204. (math-reject-arg (nth 1 a) 'posp))
  1205. (let ((accum 0))
  1206. (while (setq a (cdr a))
  1207. (if (eq (car-safe (car a)) 'intv)
  1208. (if (equal (nth 3 (car a)) '(var inf var-inf))
  1209. (setq accum (math-sub accum
  1210. (math-power-of-2 (nth 2 (car a)))))
  1211. (setq accum (math-add accum
  1212. (math-sub
  1213. (math-power-of-2 (1+ (nth 3 (car a))))
  1214. (math-power-of-2 (nth 2 (car a)))))))
  1215. (setq accum (math-add accum (math-power-of-2 (car a))))))
  1216. accum))
  1217. (defun calcFunc-vunpack (a &optional w)
  1218. (or (math-num-integerp a) (math-reject-arg a 'integerp))
  1219. (if w (setq a (math-clip a w)))
  1220. (if (math-messy-integerp a) (setq a (math-trunc a)))
  1221. (let* ((calc-number-radix 2)
  1222. (calc-twos-complement-mode nil)
  1223. (neg (math-negp a))
  1224. (aa (if neg (math-sub -1 a) a))
  1225. (str (if (eq aa 0)
  1226. ""
  1227. (if (consp aa)
  1228. (math-format-bignum-binary (cdr aa))
  1229. (math-format-binary aa))))
  1230. (zero (if neg ?1 ?0))
  1231. (one (if neg ?0 ?1))
  1232. (len (length str))
  1233. (vec (list 'vec))
  1234. (pos (1- len)) pos2)
  1235. (while (>= pos 0)
  1236. (if (eq (aref str pos) zero)
  1237. (setq pos (1- pos))
  1238. (setq pos2 pos)
  1239. (while (and (>= pos 0) (eq (aref str pos) one))
  1240. (setq pos (1- pos)))
  1241. (setq vec (cons (if (= pos (1- pos2))
  1242. (- len pos2 1)
  1243. (list 'intv 3 (- len pos2 1) (- len pos 2)))
  1244. vec))))
  1245. (if neg
  1246. (setq vec (cons (list 'intv 2 len '(var inf var-inf)) vec)))
  1247. (math-clean-set (nreverse vec))))
  1248. (defun calcFunc-rdup (a)
  1249. (if (math-simple-set a)
  1250. (progn
  1251. (and (Math-objectp a) (setq a (list 'vec a)))
  1252. (or (math-vectorp a) (math-reject-arg a 'vectorp))
  1253. (setq a (sort (copy-sequence (cdr a)) 'math-beforep))
  1254. (let ((p a))
  1255. (while (cdr p)
  1256. (if (Math-equal (car p) (nth 1 p))
  1257. (setcdr p (cdr (cdr p)))
  1258. (setq p (cdr p)))))
  1259. (cons 'vec a))
  1260. (math-clean-set (math-prepare-set a))))
  1261. (defun math-prepare-set (a)
  1262. (if (Math-objectp a)
  1263. (setq a (list 'vec a))
  1264. (or (math-vectorp a) (math-reject-arg a 'vectorp))
  1265. (setq a (cons 'vec (sort (copy-sequence (cdr a)) 'math-beforep))))
  1266. (let ((p a) res)
  1267. ;; Convert all elements to non-empty intervals.
  1268. (while (cdr p)
  1269. (if (eq (car-safe (nth 1 p)) 'intv)
  1270. (if (math-intv-constp (nth 1 p))
  1271. (if (and (memq (nth 1 (nth 1 p)) '(0 1 2))
  1272. (Math-equal (nth 2 (nth 1 p)) (nth 3 (nth 1 p))))
  1273. (setcdr p (cdr (cdr p)))
  1274. (setq p (cdr p)))
  1275. (math-reject-arg (nth 1 p) 'constp))
  1276. (or (Math-anglep (nth 1 p))
  1277. (eq (car (nth 1 p)) 'date)
  1278. (equal (nth 1 p) '(var inf var-inf))
  1279. (equal (nth 1 p) '(neg (var inf var-inf)))
  1280. (math-reject-arg (nth 1 p) 'realp))
  1281. (setcar (cdr p) (list 'intv 3 (nth 1 p) (nth 1 p)))
  1282. (setq p (cdr p))))
  1283. ;; Combine redundant intervals.
  1284. (setq p a)
  1285. (while (cdr (cdr p))
  1286. (if (or (memq (setq res (math-compare (nth 3 (nth 1 p))
  1287. (nth 2 (nth 2 p))))
  1288. '(-1 2))
  1289. (and (eq res 0)
  1290. (memq (nth 1 (nth 1 p)) '(0 2))
  1291. (memq (nth 1 (nth 2 p)) '(0 1))))
  1292. (setq p (cdr p))
  1293. (setq res (math-compare (nth 3 (nth 1 p)) (nth 3 (nth 2 p))))
  1294. (setcdr p (cons (list 'intv
  1295. (+ (logand (logior (nth 1 (nth 1 p))
  1296. (if (Math-equal
  1297. (nth 2 (nth 1 p))
  1298. (nth 2 (nth 2 p)))
  1299. (nth 1 (nth 2 p))
  1300. 0))
  1301. 2)
  1302. (logand (logior (if (memq res '(1 0 2))
  1303. (nth 1 (nth 1 p)) 0)
  1304. (if (memq res '(-1 0 2))
  1305. (nth 1 (nth 2 p)) 0))
  1306. 1))
  1307. (nth 2 (nth 1 p))
  1308. (if (eq res 1)
  1309. (nth 3 (nth 1 p))
  1310. (nth 3 (nth 2 p))))
  1311. (cdr (cdr (cdr p))))))))
  1312. a)
  1313. (defun math-clean-set (a &optional always-vec)
  1314. (let ((p a) res)
  1315. (while (cdr p)
  1316. (if (and (eq (car-safe (nth 1 p)) 'intv)
  1317. (Math-equal (nth 2 (nth 1 p)) (nth 3 (nth 1 p))))
  1318. (setcar (cdr p) (nth 2 (nth 1 p))))
  1319. (setq p (cdr p)))
  1320. (if (and (not (cdr (cdr a)))
  1321. (eq (car-safe (nth 1 a)) 'intv)
  1322. (not always-vec))
  1323. (nth 1 a)
  1324. a)))
  1325. (defun math-simple-set (a)
  1326. (or (and (Math-objectp a)
  1327. (not (eq (car-safe a) 'intv)))
  1328. (and (Math-vectorp a)
  1329. (progn
  1330. (while (and (setq a (cdr a))
  1331. (not (eq (car-safe (car a)) 'intv))))
  1332. (null a)))))
  1333. ;;; Compute a right-handed vector cross product. [O O O] [Public]
  1334. (defun calcFunc-cross (a b)
  1335. (if (and (eq (car-safe a) 'vec)
  1336. (= (length a) 4))
  1337. (if (and (eq (car-safe b) 'vec)
  1338. (= (length b) 4))
  1339. (list 'vec
  1340. (math-sub (math-mul (nth 2 a) (nth 3 b))
  1341. (math-mul (nth 3 a) (nth 2 b)))
  1342. (math-sub (math-mul (nth 3 a) (nth 1 b))
  1343. (math-mul (nth 1 a) (nth 3 b)))
  1344. (math-sub (math-mul (nth 1 a) (nth 2 b))
  1345. (math-mul (nth 2 a) (nth 1 b))))
  1346. (math-reject-arg b "*Three-vector expected"))
  1347. (math-reject-arg a "*Three-vector expected")))
  1348. ;;; Compute a Kronecker product
  1349. (defun calcFunc-kron (x y &optional nocheck)
  1350. "The Kronecker product of objects X and Y.
  1351. The objects X and Y may be scalars, vectors or matrices.
  1352. The type of the result depends on the types of the operands;
  1353. the product of two scalars is a scalar,
  1354. of one scalar and a vector is a vector,
  1355. of two vectors is a vector.
  1356. of one vector and a matrix is a matrix,
  1357. of two matrices is a matrix."
  1358. (unless nocheck
  1359. (cond ((or (math-matrixp x)
  1360. (math-matrixp y))
  1361. (unless (math-matrixp x)
  1362. (setq x (if (math-vectorp x)
  1363. (list 'vec x)
  1364. (list 'vec (list 'vec x)))))
  1365. (unless (math-matrixp y)
  1366. (setq y (if (math-vectorp y)
  1367. (list 'vec y)
  1368. (list 'vec (list 'vec y))))))
  1369. ((or (math-vectorp x)
  1370. (math-vectorp y))
  1371. (unless (math-vectorp x)
  1372. (setq x (list 'vec x)))
  1373. (unless (math-vectorp y)
  1374. (setq y (list 'vec y))))))
  1375. (if (math-vectorp x)
  1376. (let (ret)
  1377. (dolist (v (cdr x))
  1378. (dolist (w (cdr y))
  1379. (setq ret (cons (calcFunc-kron v w t) ret))))
  1380. (cons 'vec (nreverse ret)))
  1381. (math-mul x y)))
  1382. ;; The variable math-rb-close is local to math-read-brackets, but
  1383. ;; is used by math-read-vector, which is called (directly and
  1384. ;; indirectly) by math-read-brackets.
  1385. (defvar math-rb-close)
  1386. ;; The next few variables are local to math-read-exprs in calc-aent.el
  1387. ;; and math-read-expr in calc-ext.el, but are set in functions they call.
  1388. (defvar math-exp-pos)
  1389. (defvar math-exp-str)
  1390. (defvar math-exp-old-pos)
  1391. (defvar math-exp-token)
  1392. (defvar math-exp-keep-spaces)
  1393. (defvar math-expr-data)
  1394. (defun math-read-brackets (space-sep math-rb-close)
  1395. (and space-sep (setq space-sep (not (math-check-for-commas))))
  1396. (math-read-token)
  1397. (while (eq math-exp-token 'space)
  1398. (math-read-token))
  1399. (if (or (equal math-expr-data math-rb-close)
  1400. (eq math-exp-token 'end))
  1401. (progn
  1402. (math-read-token)
  1403. '(vec))
  1404. (let ((save-exp-pos math-exp-pos)
  1405. (save-exp-old-pos math-exp-old-pos)
  1406. (save-exp-token math-exp-token)
  1407. (save-exp-data math-expr-data)
  1408. (vals (let ((math-exp-keep-spaces space-sep))
  1409. (if (or (equal math-expr-data "\\dots")
  1410. (equal math-expr-data "\\ldots"))
  1411. '(vec (neg (var inf var-inf)))
  1412. (catch 'syntax (math-read-vector))))))
  1413. (if (stringp vals)
  1414. (if space-sep
  1415. (let ((error-exp-pos math-exp-pos)
  1416. (error-exp-old-pos math-exp-old-pos)
  1417. vals2)
  1418. (setq math-exp-pos save-exp-pos
  1419. math-exp-old-pos save-exp-old-pos
  1420. math-exp-token save-exp-token
  1421. math-expr-data save-exp-data)
  1422. (let ((math-exp-keep-spaces nil))
  1423. (setq vals2 (catch 'syntax (math-read-vector))))
  1424. (if (and (not (stringp vals2))
  1425. (or (assoc math-expr-data '(("\\ldots") ("\\dots") (";")))
  1426. (equal math-expr-data math-rb-close)
  1427. (eq math-exp-token 'end)))
  1428. (setq space-sep nil
  1429. vals vals2)
  1430. (setq math-exp-pos error-exp-pos
  1431. math-exp-old-pos error-exp-old-pos)
  1432. (throw 'syntax vals)))
  1433. (throw 'syntax vals)))
  1434. (if (or (equal math-expr-data "\\dots")
  1435. (equal math-expr-data "\\ldots"))
  1436. (progn
  1437. (math-read-token)
  1438. (setq vals (if (> (length vals) 2)
  1439. (cons 'calcFunc-mul (cdr vals)) (nth 1 vals)))
  1440. (let ((exp2 (if (or (equal math-expr-data math-rb-close)
  1441. (equal math-expr-data ")")
  1442. (eq math-exp-token 'end))
  1443. '(var inf var-inf)
  1444. (math-read-expr-level 0))))
  1445. (setq vals
  1446. (list 'intv
  1447. (if (equal math-expr-data ")") 2 3)
  1448. vals
  1449. exp2)))
  1450. (if (not (or (equal math-expr-data math-rb-close)
  1451. (equal math-expr-data ")")
  1452. (eq math-exp-token 'end)))
  1453. (throw 'syntax "Expected `]'")))
  1454. (if (equal math-expr-data ";")
  1455. (let ((math-exp-keep-spaces space-sep))
  1456. (setq vals (cons 'vec (math-read-matrix (list vals))))))
  1457. (if (not (or (equal math-expr-data math-rb-close)
  1458. (eq math-exp-token 'end)))
  1459. (throw 'syntax "Expected `]'")))
  1460. (or (eq math-exp-token 'end)
  1461. (math-read-token))
  1462. vals)))
  1463. (defun math-check-for-commas (&optional balancing)
  1464. (let ((count 0)
  1465. (pos (1- math-exp-pos)))
  1466. (while (and (>= count 0)
  1467. (setq pos (string-match
  1468. (if balancing "[],[{}()<>]" "[],[{}()]")
  1469. math-exp-str (1+ pos)))
  1470. (or (/= (aref math-exp-str pos) ?,) (> count 0) balancing))
  1471. (cond ((memq (aref math-exp-str pos) '(?\[ ?\{ ?\( ?\<))
  1472. (setq count (1+ count)))
  1473. ((memq (aref math-exp-str pos) '(?\] ?\} ?\) ?\>))
  1474. (setq count (1- count)))))
  1475. (if balancing
  1476. pos
  1477. (and pos (= (aref math-exp-str pos) ?,)))))
  1478. (defun math-read-vector ()
  1479. (let* ((val (list (math-read-expr-level 0)))
  1480. (last val))
  1481. (while (progn
  1482. (while (eq math-exp-token 'space)
  1483. (math-read-token))
  1484. (and (not (eq math-exp-token 'end))
  1485. (not (equal math-expr-data ";"))
  1486. (not (equal math-expr-data math-rb-close))
  1487. (not (equal math-expr-data "\\dots"))
  1488. (not (equal math-expr-data "\\ldots"))))
  1489. (if (equal math-expr-data ",")
  1490. (math-read-token))
  1491. (while (eq math-exp-token 'space)
  1492. (math-read-token))
  1493. (let ((rest (list (math-read-expr-level 0))))
  1494. (setcdr last rest)
  1495. (setq last rest)))
  1496. (cons 'vec val)))
  1497. (defun math-read-matrix (mat)
  1498. (while (equal math-expr-data ";")
  1499. (math-read-token)
  1500. (while (eq math-exp-token 'space)
  1501. (math-read-token))
  1502. (setq mat (nconc mat (list (math-read-vector)))))
  1503. mat)
  1504. (provide 'calc-vec)
  1505. ;;; calc-vec.el ends here