calc-map.el 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. ;;; calc-map.el --- higher-order functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2015 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-apply (&optional oper)
  22. (interactive)
  23. (calc-wrapper
  24. (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
  25. (nthcdr calc-stack-top calc-stack)))
  26. (calc-dollar-used 0)
  27. (oper (or oper (calc-get-operator "Apply"
  28. (if (math-vectorp (calc-top 1))
  29. (1- (length (calc-top 1)))
  30. -1))))
  31. (expr (calc-top-n (1+ calc-dollar-used))))
  32. (message "Working...")
  33. (calc-set-command-flag 'clear-message)
  34. (calc-enter-result (1+ calc-dollar-used)
  35. (concat (substring "apl" 0 (- 4 (length (nth 2 oper))))
  36. (nth 2 oper))
  37. (list 'calcFunc-apply
  38. (math-calcFunc-to-var (nth 1 oper))
  39. expr)))))
  40. (defun calc-reduce (&optional oper accum)
  41. (interactive)
  42. (calc-wrapper
  43. (let* ((nest (calc-is-hyperbolic))
  44. (rev (calc-is-inverse))
  45. (nargs (if (and nest (not rev)) 2 1))
  46. (calc-dollar-values (mapcar #'calc-get-stack-element
  47. (nthcdr calc-stack-top calc-stack)))
  48. (calc-dollar-used 0)
  49. (calc-mapping-dir (and (not accum) (not nest) ""))
  50. (oper (or oper (calc-get-operator
  51. (if nest
  52. (concat (if accum "Accumulate " "")
  53. (if rev "Fixed Point" "Nest"))
  54. (concat (if rev "Inv " "")
  55. (if accum "Accumulate" "Reduce")))
  56. (if nest 1 2)))))
  57. (message "Working...")
  58. (calc-set-command-flag 'clear-message)
  59. (calc-enter-result (+ calc-dollar-used nargs)
  60. (concat (substring (if nest
  61. (if rev "fxp" "nst")
  62. (if accum "acc" "red"))
  63. 0 (- 4 (length (nth 2 oper))))
  64. (nth 2 oper))
  65. (if nest
  66. (cons (if rev
  67. (if accum 'calcFunc-afixp 'calcFunc-fixp)
  68. (if accum 'calcFunc-anest 'calcFunc-nest))
  69. (cons (math-calcFunc-to-var (nth 1 oper))
  70. (calc-top-list-n
  71. nargs (1+ calc-dollar-used))))
  72. (list (if accum
  73. (if rev 'calcFunc-raccum 'calcFunc-accum)
  74. (intern (concat "calcFunc-"
  75. (if rev "r" "")
  76. "reduce"
  77. calc-mapping-dir)))
  78. (math-calcFunc-to-var (nth 1 oper))
  79. (calc-top-n (1+ calc-dollar-used))))))))
  80. (defun calc-accumulate (&optional oper)
  81. (interactive)
  82. (calc-reduce oper t))
  83. (defun calc-map (&optional oper)
  84. (interactive)
  85. (calc-wrapper
  86. (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
  87. (nthcdr calc-stack-top calc-stack)))
  88. (calc-dollar-used 0)
  89. (calc-mapping-dir "")
  90. (oper (or oper (calc-get-operator "Map")))
  91. (nargs (car oper)))
  92. (message "Working...")
  93. (calc-set-command-flag 'clear-message)
  94. (calc-enter-result (+ nargs calc-dollar-used)
  95. (concat (substring "map" 0 (- 4 (length (nth 2 oper))))
  96. (nth 2 oper))
  97. (cons (intern (concat "calcFunc-map" calc-mapping-dir))
  98. (cons (math-calcFunc-to-var (nth 1 oper))
  99. (calc-top-list-n
  100. nargs
  101. (1+ calc-dollar-used))))))))
  102. (defun calc-map-equation (&optional oper)
  103. (interactive)
  104. (calc-wrapper
  105. (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
  106. (nthcdr calc-stack-top calc-stack)))
  107. (calc-dollar-used 0)
  108. (oper (or oper (calc-get-operator "Map-equation")))
  109. (nargs (car oper)))
  110. (message "Working...")
  111. (calc-set-command-flag 'clear-message)
  112. (calc-enter-result (+ nargs calc-dollar-used)
  113. (concat (substring "map" 0 (- 4 (length (nth 2 oper))))
  114. (nth 2 oper))
  115. (cons (if (calc-is-inverse)
  116. 'calcFunc-mapeqr
  117. (if (calc-is-hyperbolic)
  118. 'calcFunc-mapeqp 'calcFunc-mapeq))
  119. (cons (math-calcFunc-to-var (nth 1 oper))
  120. (calc-top-list-n
  121. nargs
  122. (1+ calc-dollar-used))))))))
  123. (defvar calc-verify-arglist t)
  124. (defvar calc-mapping-dir nil)
  125. (defun calc-map-stack ()
  126. "This is meant to be called by calc-keypad mode."
  127. (interactive)
  128. (let ((calc-verify-arglist nil))
  129. (calc-unread-command ?\$)
  130. (calc-map)))
  131. (defun calc-outer-product (&optional oper)
  132. (interactive)
  133. (calc-wrapper
  134. (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
  135. (nthcdr calc-stack-top calc-stack)))
  136. (calc-dollar-used 0)
  137. (oper (or oper (calc-get-operator "Outer" 2))))
  138. (message "Working...")
  139. (calc-set-command-flag 'clear-message)
  140. (calc-enter-result (+ 2 calc-dollar-used)
  141. (concat (substring "out" 0 (- 4 (length (nth 2 oper))))
  142. (nth 2 oper))
  143. (cons 'calcFunc-outer
  144. (cons (math-calcFunc-to-var (nth 1 oper))
  145. (calc-top-list-n
  146. 2 (1+ calc-dollar-used))))))))
  147. (defun calc-inner-product (&optional mul-oper add-oper)
  148. (interactive)
  149. (calc-wrapper
  150. (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
  151. (nthcdr calc-stack-top calc-stack)))
  152. (calc-dollar-used 0)
  153. (mul-oper (or mul-oper (calc-get-operator "Inner (Mult)" 2)))
  154. (mul-used calc-dollar-used)
  155. (calc-dollar-values (if (> mul-used 0)
  156. (cdr calc-dollar-values)
  157. calc-dollar-values))
  158. (calc-dollar-used 0)
  159. (add-oper (or add-oper (calc-get-operator "Inner (Add)" 2))))
  160. (message "Working...")
  161. (calc-set-command-flag 'clear-message)
  162. (calc-enter-result (+ 2 mul-used calc-dollar-used)
  163. (concat "in"
  164. (substring (nth 2 mul-oper) 0 1)
  165. (substring (nth 2 add-oper) 0 1))
  166. (nconc (list 'calcFunc-inner
  167. (math-calcFunc-to-var (nth 1 mul-oper))
  168. (math-calcFunc-to-var (nth 1 add-oper)))
  169. (calc-top-list-n
  170. 2 (+ 1 mul-used calc-dollar-used)))))))
  171. (defconst calc-oper-keys '( ( ( ?+ 2 calcFunc-add )
  172. ( ?- 2 calcFunc-sub )
  173. ( ?* 2 calcFunc-mul )
  174. ( ?/ 2 calcFunc-div )
  175. ( ?^ 2 calcFunc-pow )
  176. ( ?| 2 calcFunc-vconcat )
  177. ( ?% 2 calcFunc-mod )
  178. ( ?\\ 2 calcFunc-idiv )
  179. ( ?! 1 calcFunc-fact )
  180. ( ?& 1 calcFunc-inv )
  181. ( ?n 1 calcFunc-neg )
  182. ( ?x user )
  183. ( ?z user )
  184. ( ?A 1 calcFunc-abs )
  185. ( ?J 1 calcFunc-conj )
  186. ( ?G 1 calcFunc-arg )
  187. ( ?Q 1 calcFunc-sqrt )
  188. ( ?N 2 calcFunc-min )
  189. ( ?X 2 calcFunc-max )
  190. ( ?F 1 calcFunc-floor )
  191. ( ?R 1 calcFunc-round )
  192. ( ?S 1 calcFunc-sin )
  193. ( ?C 1 calcFunc-cos )
  194. ( ?T 1 calcFunc-tan )
  195. ( ?L 1 calcFunc-ln )
  196. ( ?E 1 calcFunc-exp )
  197. ( ?B 2 calcFunc-log ) )
  198. ( ( ?F 1 calcFunc-ceil ) ; inverse
  199. ( ?R 1 calcFunc-trunc )
  200. ( ?Q 1 calcFunc-sqr )
  201. ( ?S 1 calcFunc-arcsin )
  202. ( ?C 1 calcFunc-arccos )
  203. ( ?T 1 calcFunc-arctan )
  204. ( ?L 1 calcFunc-exp )
  205. ( ?E 1 calcFunc-ln )
  206. ( ?B 2 calcFunc-alog )
  207. ( ?^ 2 calcFunc-nroot )
  208. ( ?| 2 calcFunc-vconcatrev ) )
  209. ( ( ?F 1 calcFunc-ffloor ) ; hyperbolic
  210. ( ?R 1 calcFunc-fround )
  211. ( ?S 1 calcFunc-sinh )
  212. ( ?C 1 calcFunc-cosh )
  213. ( ?T 1 calcFunc-tanh )
  214. ( ?L 1 calcFunc-log10 )
  215. ( ?E 1 calcFunc-exp10 )
  216. ( ?| 2 calcFunc-append ) )
  217. ( ( ?F 1 calcFunc-fceil ) ; inverse-hyperbolic
  218. ( ?R 1 calcFunc-ftrunc )
  219. ( ?S 1 calcFunc-arcsinh )
  220. ( ?C 1 calcFunc-arccosh )
  221. ( ?T 1 calcFunc-arctanh )
  222. ( ?L 1 calcFunc-exp10 )
  223. ( ?E 1 calcFunc-log10 )
  224. ( ?| 2 calcFunc-appendrev ) )))
  225. (defconst calc-a-oper-keys '( ( ( ?a 3 calcFunc-apart )
  226. ( ?b 3 calcFunc-subst )
  227. ( ?c 2 calcFunc-collect )
  228. ( ?d 2 calcFunc-deriv )
  229. ( ?e 1 calcFunc-esimplify )
  230. ( ?f 2 calcFunc-factor )
  231. ( ?g 2 calcFunc-pgcd )
  232. ( ?i 2 calcFunc-integ )
  233. ( ?m 2 calcFunc-match )
  234. ( ?n 1 calcFunc-nrat )
  235. ( ?r 2 calcFunc-rewrite )
  236. ( ?s 1 calcFunc-simplify )
  237. ( ?t 3 calcFunc-taylor )
  238. ( ?x 1 calcFunc-expand )
  239. ( ?M 2 calcFunc-mapeq )
  240. ( ?N 3 calcFunc-minimize )
  241. ( ?P 2 calcFunc-roots )
  242. ( ?R 3 calcFunc-root )
  243. ( ?S 2 calcFunc-solve )
  244. ( ?T 4 calcFunc-table )
  245. ( ?X 3 calcFunc-maximize )
  246. ( ?= 2 calcFunc-eq )
  247. ( ?\# 2 calcFunc-neq )
  248. ( ?< 2 calcFunc-lt )
  249. ( ?> 2 calcFunc-gt )
  250. ( ?\[ 2 calcFunc-leq )
  251. ( ?\] 2 calcFunc-geq )
  252. ( ?{ 2 calcFunc-in )
  253. ( ?! 1 calcFunc-lnot )
  254. ( ?& 2 calcFunc-land )
  255. ( ?\| 2 calcFunc-lor )
  256. ( ?: 3 calcFunc-if )
  257. ( ?. 2 calcFunc-rmeq )
  258. ( ?+ 4 calcFunc-sum )
  259. ( ?- 4 calcFunc-asum )
  260. ( ?* 4 calcFunc-prod )
  261. ( ?_ 2 calcFunc-subscr )
  262. ( ?\\ 2 calcFunc-pdiv )
  263. ( ?% 2 calcFunc-prem )
  264. ( ?/ 2 calcFunc-pdivrem ) )
  265. ( ( ?m 2 calcFunc-matchnot )
  266. ( ?M 2 calcFunc-mapeqr )
  267. ( ?S 2 calcFunc-finv ) )
  268. ( ( ?d 2 calcFunc-tderiv )
  269. ( ?f 2 calcFunc-factors )
  270. ( ?M 2 calcFunc-mapeqp )
  271. ( ?N 3 calcFunc-wminimize )
  272. ( ?R 3 calcFunc-wroot )
  273. ( ?S 2 calcFunc-fsolve )
  274. ( ?X 3 calcFunc-wmaximize )
  275. ( ?/ 2 calcFunc-pdivide ) )
  276. ( ( ?S 2 calcFunc-ffinv ) )))
  277. (defconst calc-b-oper-keys '( ( ( ?a 2 calcFunc-and )
  278. ( ?o 2 calcFunc-or )
  279. ( ?x 2 calcFunc-xor )
  280. ( ?d 2 calcFunc-diff )
  281. ( ?n 1 calcFunc-not )
  282. ( ?c 1 calcFunc-clip )
  283. ( ?l 2 calcFunc-lsh )
  284. ( ?r 2 calcFunc-rsh )
  285. ( ?L 2 calcFunc-ash )
  286. ( ?R 2 calcFunc-rash )
  287. ( ?t 2 calcFunc-rot )
  288. ( ?p 1 calcFunc-vpack )
  289. ( ?u 1 calcFunc-vunpack )
  290. ( ?D 4 calcFunc-ddb )
  291. ( ?F 3 calcFunc-fv )
  292. ( ?I 1 calcFunc-irr )
  293. ( ?M 3 calcFunc-pmt )
  294. ( ?N 2 calcFunc-npv )
  295. ( ?P 3 calcFunc-pv )
  296. ( ?S 3 calcFunc-sln )
  297. ( ?T 3 calcFunc-rate )
  298. ( ?Y 4 calcFunc-syd )
  299. ( ?\# 3 calcFunc-nper )
  300. ( ?\% 2 calcFunc-relch ) )
  301. ( ( ?F 3 calcFunc-fvb )
  302. ( ?I 1 calcFunc-irrb )
  303. ( ?M 3 calcFunc-pmtb )
  304. ( ?N 2 calcFunc-npvb )
  305. ( ?P 3 calcFunc-pvb )
  306. ( ?T 3 calcFunc-rateb )
  307. ( ?\# 3 calcFunc-nperb ) )
  308. ( ( ?F 3 calcFunc-fvl )
  309. ( ?M 3 calcFunc-pmtl )
  310. ( ?P 3 calcFunc-pvl )
  311. ( ?T 3 calcFunc-ratel )
  312. ( ?\# 3 calcFunc-nperl ) )))
  313. (defconst calc-c-oper-keys '( ( ( ?d 1 calcFunc-deg )
  314. ( ?r 1 calcFunc-rad )
  315. ( ?h 1 calcFunc-hms )
  316. ( ?f 1 calcFunc-float )
  317. ( ?F 1 calcFunc-frac ) )))
  318. (defconst calc-f-oper-keys '( ( ( ?b 2 calcFunc-beta )
  319. ( ?e 1 calcFunc-erf )
  320. ( ?g 1 calcFunc-gamma )
  321. ( ?h 2 calcFunc-hypot )
  322. ( ?i 1 calcFunc-im )
  323. ( ?j 2 calcFunc-besJ )
  324. ( ?n 2 calcFunc-min )
  325. ( ?r 1 calcFunc-re )
  326. ( ?s 1 calcFunc-sign )
  327. ( ?x 2 calcFunc-max )
  328. ( ?y 2 calcFunc-besY )
  329. ( ?A 1 calcFunc-abssqr )
  330. ( ?B 3 calcFunc-betaI )
  331. ( ?E 1 calcFunc-expm1 )
  332. ( ?G 2 calcFunc-gammaP )
  333. ( ?I 2 calcFunc-ilog )
  334. ( ?L 1 calcFunc-lnp1 )
  335. ( ?M 1 calcFunc-mant )
  336. ( ?Q 1 calcFunc-isqrt )
  337. ( ?S 1 calcFunc-scf )
  338. ( ?T 2 calcFunc-arctan2 )
  339. ( ?X 1 calcFunc-xpon )
  340. ( ?\[ 2 calcFunc-decr )
  341. ( ?\] 2 calcFunc-incr ) )
  342. ( ( ?e 1 calcFunc-erfc )
  343. ( ?E 1 calcFunc-lnp1 )
  344. ( ?G 2 calcFunc-gammaQ )
  345. ( ?L 1 calcFunc-expm1 ) )
  346. ( ( ?B 3 calcFunc-betaB )
  347. ( ?G 2 calcFunc-gammag) )
  348. ( ( ?G 2 calcFunc-gammaG ) )))
  349. (defconst calc-k-oper-keys '( ( ( ?b 1 calcFunc-bern )
  350. ( ?c 2 calcFunc-choose )
  351. ( ?d 1 calcFunc-dfact )
  352. ( ?e 1 calcFunc-euler )
  353. ( ?f 1 calcFunc-prfac )
  354. ( ?g 2 calcFunc-gcd )
  355. ( ?h 2 calcFunc-shuffle )
  356. ( ?l 2 calcFunc-lcm )
  357. ( ?m 1 calcFunc-moebius )
  358. ( ?n 1 calcFunc-nextprime )
  359. ( ?r 1 calcFunc-random )
  360. ( ?s 2 calcFunc-stir1 )
  361. ( ?t 1 calcFunc-totient )
  362. ( ?B 3 calcFunc-utpb )
  363. ( ?C 2 calcFunc-utpc )
  364. ( ?F 3 calcFunc-utpf )
  365. ( ?N 3 calcFunc-utpn )
  366. ( ?P 2 calcFunc-utpp )
  367. ( ?T 2 calcFunc-utpt ) )
  368. ( ( ?n 1 calcFunc-prevprime )
  369. ( ?B 3 calcFunc-ltpb )
  370. ( ?C 2 calcFunc-ltpc )
  371. ( ?F 3 calcFunc-ltpf )
  372. ( ?N 3 calcFunc-ltpn )
  373. ( ?P 2 calcFunc-ltpp )
  374. ( ?T 2 calcFunc-ltpt ) )
  375. ( ( ?b 2 calcFunc-bern )
  376. ( ?c 2 calcFunc-perm )
  377. ( ?e 2 calcFunc-euler )
  378. ( ?s 2 calcFunc-stir2 ) )))
  379. (defconst calc-s-oper-keys '( ( ( ?: 2 calcFunc-assign )
  380. ( ?= 1 calcFunc-evalto ) )))
  381. (defconst calc-t-oper-keys '( ( ( ?C 3 calcFunc-tzconv )
  382. ( ?D 1 calcFunc-date )
  383. ( ?I 2 calcFunc-incmonth )
  384. ( ?J 1 calcFunc-julian )
  385. ( ?M 1 calcFunc-newmonth )
  386. ( ?W 1 calcFunc-newweek )
  387. ( ?U 1 calcFunc-unixtime )
  388. ( ?Y 1 calcFunc-newyear ) )))
  389. (defconst calc-u-oper-keys '( ( ( ?C 2 calcFunc-vcov )
  390. ( ?G 1 calcFunc-vgmean )
  391. ( ?M 1 calcFunc-vmean )
  392. ( ?N 1 calcFunc-vmin )
  393. ( ?S 1 calcFunc-vsdev )
  394. ( ?X 1 calcFunc-vmax ) )
  395. ( ( ?C 2 calcFunc-vpcov )
  396. ( ?M 1 calcFunc-vmeane )
  397. ( ?S 1 calcFunc-vpsdev ) )
  398. ( ( ?C 2 calcFunc-vcorr )
  399. ( ?G 1 calcFunc-agmean )
  400. ( ?M 1 calcFunc-vmedian )
  401. ( ?S 1 calcFunc-vvar ) )
  402. ( ( ?M 1 calcFunc-vhmean )
  403. ( ?S 1 calcFunc-vpvar ) )))
  404. (defconst calc-v-oper-keys '( ( ( ?a 2 calcFunc-arrange )
  405. ( ?b 2 calcFunc-cvec )
  406. ( ?c 2 calcFunc-mcol )
  407. ( ?d 2 calcFunc-diag )
  408. ( ?e 2 calcFunc-vexp )
  409. ( ?f 2 calcFunc-find )
  410. ( ?h 1 calcFunc-head )
  411. ( ?k 2 calcFunc-cons )
  412. ( ?l 1 calcFunc-vlen )
  413. ( ?m 2 calcFunc-vmask )
  414. ( ?n 1 calcFunc-rnorm )
  415. ( ?p 2 calcFunc-pack )
  416. ( ?r 2 calcFunc-mrow )
  417. ( ?s 3 calcFunc-subvec )
  418. ( ?t 1 calcFunc-trn )
  419. ( ?u 1 calcFunc-unpack )
  420. ( ?v 1 calcFunc-rev )
  421. ( ?x 1 calcFunc-index )
  422. ( ?A 1 calcFunc-apply )
  423. ( ?C 1 calcFunc-cross )
  424. ( ?D 1 calcFunc-det )
  425. ( ?E 1 calcFunc-venum )
  426. ( ?F 1 calcFunc-vfloor )
  427. ( ?G 1 calcFunc-grade )
  428. ( ?H 2 calcFunc-histogram )
  429. ( ?I 2 calcFunc-inner )
  430. ( ?L 1 calcFunc-lud )
  431. ( ?M 0 calcFunc-map )
  432. ( ?N 1 calcFunc-cnorm )
  433. ( ?O 2 calcFunc-outer )
  434. ( ?R 1 calcFunc-reduce )
  435. ( ?S 1 calcFunc-sort )
  436. ( ?T 1 calcFunc-tr )
  437. ( ?U 1 calcFunc-accum )
  438. ( ?V 2 calcFunc-vunion )
  439. ( ?X 2 calcFunc-vxor )
  440. ( ?- 2 calcFunc-vdiff )
  441. ( ?^ 2 calcFunc-vint )
  442. ( ?~ 1 calcFunc-vcompl )
  443. ( ?# 1 calcFunc-vcard )
  444. ( ?: 1 calcFunc-vspan )
  445. ( ?+ 1 calcFunc-rdup ) )
  446. ( ( ?h 1 calcFunc-tail )
  447. ( ?s 3 calcFunc-rsubvec )
  448. ( ?G 1 calcFunc-rgrade )
  449. ( ?R 1 calcFunc-rreduce )
  450. ( ?S 1 calcFunc-rsort )
  451. ( ?U 1 calcFunc-raccum ) )
  452. ( ( ?e 3 calcFunc-vexp )
  453. ( ?h 1 calcFunc-rhead )
  454. ( ?k 2 calcFunc-rcons )
  455. ( ?H 3 calcFunc-histogram )
  456. ( ?R 2 calcFunc-nest )
  457. ( ?U 2 calcFunc-anest ) )
  458. ( ( ?h 1 calcFunc-rtail )
  459. ( ?R 1 calcFunc-fixp )
  460. ( ?U 1 calcFunc-afixp ) )))
  461. ;;; Return a list of the form (nargs func name)
  462. (defvar calc-get-operator-history nil
  463. "History for calc-get-operator.")
  464. (defun calc-get-operator (msg &optional nargs)
  465. (setq calc-aborted-prefix nil)
  466. (let ((inv nil) (hyp nil) (prefix nil) (forcenargs nil)
  467. done key oper (which 0)
  468. (msgs '( "(Press ? for help)"
  469. "+, -, *, /, ^, %, \\, :, &, !, |, Neg"
  470. "SHIFT + Abs, conJ, arG; maX, miN; Floor, Round; sQrt"
  471. "SHIFT + Inv, Hyp; Sin, Cos, Tan; Exp, Ln, logB"
  472. "Algebra + Simp, Esimp, Deriv, Integ, !, =, etc."
  473. "Binary + And, Or, Xor, Diff; l/r/t/L/R shifts; Not, Clip"
  474. "Conversions + Deg, Rad, HMS; Float; SHIFT + Fraction"
  475. "Functions + Re, Im; Hypot; Mant, Expon, Scale; etc."
  476. "Kombinatorics + Dfact, Lcm, Gcd, Choose; Random; etc."
  477. "Time/date + newYear, Incmonth, etc."
  478. "Vectors + Length, Row, Col, Diag, Mask, etc."
  479. "_ = mapr/reducea, : = mapc/reduced, = = reducer"
  480. "X or Z = any function by name; ' = alg entry; $ = stack")))
  481. (while (not done)
  482. (message "%s%s: %s: %s%s%s"
  483. msg
  484. (cond ((equal calc-mapping-dir "r") " rows")
  485. ((equal calc-mapping-dir "c") " columns")
  486. ((equal calc-mapping-dir "a") " across")
  487. ((equal calc-mapping-dir "d") " down")
  488. (t ""))
  489. (if forcenargs
  490. (format "(%d arg%s)"
  491. forcenargs (if (= forcenargs 1) "" "s"))
  492. (nth which msgs))
  493. (if inv "Inv " "") (if hyp "Hyp " "")
  494. (if prefix (concat (char-to-string prefix) "-") ""))
  495. (setq key (read-char))
  496. (if (>= key 128) (setq key (- key 128)))
  497. (cond ((memq key '(?\C-g ?q))
  498. (keyboard-quit))
  499. ((memq key '(?\C-u ?\e)))
  500. ((= key ??)
  501. (setq which (% (1+ which) (length msgs))))
  502. ((and (= key ?I) (null prefix))
  503. (setq inv (not inv)))
  504. ((and (= key ?H) (null prefix))
  505. (setq hyp (not hyp)))
  506. ((and (eq key prefix) (not (eq key ?v)))
  507. (setq prefix nil))
  508. ((and (memq key '(?a ?b ?c ?f ?k ?s ?t ?u ?v ?V))
  509. (null prefix))
  510. (setq prefix (downcase key)))
  511. ((and (eq key ?\=) (null prefix))
  512. (if calc-mapping-dir
  513. (setq calc-mapping-dir (if (equal calc-mapping-dir "r")
  514. "" "r"))
  515. (beep)))
  516. ((and (eq key ?\_) (null prefix))
  517. (if calc-mapping-dir
  518. (if (string-match "map$" msg)
  519. (setq calc-mapping-dir (if (equal calc-mapping-dir "r")
  520. "" "r"))
  521. (setq calc-mapping-dir (if (equal calc-mapping-dir "a")
  522. "" "a")))
  523. (beep)))
  524. ((and (eq key ?\:) (null prefix))
  525. (if calc-mapping-dir
  526. (if (string-match "map$" msg)
  527. (setq calc-mapping-dir (if (equal calc-mapping-dir "c")
  528. "" "c"))
  529. (setq calc-mapping-dir (if (equal calc-mapping-dir "d")
  530. "" "d")))
  531. (beep)))
  532. ((and (>= key ?0) (<= key ?9) (null prefix))
  533. (setq forcenargs (if (eq forcenargs (- key ?0)) nil (- key ?0)))
  534. (and nargs forcenargs (/= nargs forcenargs) (>= nargs 0)
  535. (error "Must be a %d-argument operator" nargs)))
  536. ((memq key '(?\$ ?\'))
  537. (let* ((math-arglist nil)
  538. (has-args nil)
  539. (record-entry nil)
  540. (expr (if (eq key ?\$)
  541. (progn
  542. (setq calc-dollar-used 1)
  543. (if calc-dollar-values
  544. (car calc-dollar-values)
  545. (error "Stack underflow")))
  546. (let* ((calc-dollar-values calc-arg-values)
  547. (calc-dollar-used 0)
  548. (calc-hashes-used 0)
  549. (func (calc-do-alg-entry "" "Function: " nil
  550. 'calc-get-operator-history)))
  551. (setq record-entry t)
  552. (or (= (length func) 1)
  553. (error "Bad format"))
  554. (if (> calc-dollar-used 0)
  555. (progn
  556. (setq has-args calc-dollar-used
  557. math-arglist (calc-invent-args has-args))
  558. (math-multi-subst (car func)
  559. (reverse math-arglist)
  560. math-arglist))
  561. (if (> calc-hashes-used 0)
  562. (setq has-args calc-hashes-used
  563. math-arglist (calc-invent-args has-args)))
  564. (car func))))))
  565. (if (eq (car-safe expr) 'calcFunc-lambda)
  566. (setq oper (list "$" (- (length expr) 2) expr)
  567. done t)
  568. (or has-args
  569. (progn
  570. (calc-default-formula-arglist expr)
  571. (setq record-entry t
  572. math-arglist (sort math-arglist 'string-lessp))
  573. (if calc-verify-arglist
  574. (setq math-arglist (read-from-minibuffer
  575. "Function argument list: "
  576. (if math-arglist
  577. (prin1-to-string math-arglist)
  578. "()")
  579. minibuffer-local-map
  580. t)))
  581. (setq math-arglist (mapcar (function
  582. (lambda (x)
  583. (list 'var
  584. x
  585. (intern
  586. (concat
  587. "var-"
  588. (symbol-name x))))))
  589. math-arglist))))
  590. (setq oper (list "$"
  591. (length math-arglist)
  592. (append '(calcFunc-lambda) math-arglist
  593. (list expr)))
  594. done t))
  595. (if record-entry
  596. (calc-record (nth 2 oper) "oper"))))
  597. ((setq oper (assq key (nth (if inv (if hyp 3 1) (if hyp 2 0))
  598. (if prefix
  599. (symbol-value
  600. (intern (format "calc-%c-oper-keys"
  601. prefix)))
  602. calc-oper-keys))))
  603. (if (eq (nth 1 oper) 'user)
  604. (let ((func (intern
  605. (completing-read "Function name: "
  606. obarray 'fboundp
  607. nil "calcFunc-"))))
  608. (if (or forcenargs nargs)
  609. (setq oper (list "z" (or forcenargs nargs) func)
  610. done t)
  611. (if (fboundp func)
  612. (let* ((defn (symbol-function func)))
  613. (and (symbolp defn)
  614. (setq defn (symbol-function defn)))
  615. (if (eq (car-safe defn) 'lambda)
  616. (let ((args (nth 1 defn))
  617. (nargs 0))
  618. (while (not (memq (car args) '(&optional
  619. &rest nil)))
  620. (setq nargs (1+ nargs)
  621. args (cdr args)))
  622. (setq oper (list "z" nargs func)
  623. done t))
  624. (error
  625. "Function is not suitable for this operation")))
  626. (message "Number of arguments: ")
  627. (let ((nargs (read-char)))
  628. (if (and (>= nargs ?0) (<= nargs ?9))
  629. (setq oper (list "z" (- nargs ?0) func)
  630. done t)
  631. (beep))))))
  632. (if (or (and (eq prefix ?v) (memq key '(?A ?I ?M ?O ?R ?U)))
  633. (and (eq prefix ?a) (eq key ?M)))
  634. (let* ((dir (cond ((and (equal calc-mapping-dir "")
  635. (string-match "map$" msg))
  636. (setq calc-mapping-dir "r")
  637. " rows")
  638. ((equal calc-mapping-dir "r") " rows")
  639. ((equal calc-mapping-dir "c") " columns")
  640. ((equal calc-mapping-dir "a") " across")
  641. ((equal calc-mapping-dir "d") " down")
  642. (t "")))
  643. (calc-mapping-dir (and (memq (nth 2 oper)
  644. '(calcFunc-map
  645. calcFunc-reduce
  646. calcFunc-rreduce))
  647. ""))
  648. (oper2 (calc-get-operator
  649. (format "%s%s, %s%s" msg dir
  650. (substring (symbol-name (nth 2 oper))
  651. 9)
  652. (if (eq key ?I) " (mult)" ""))
  653. (cdr (assq (nth 2 oper)
  654. '((calcFunc-reduce . 2)
  655. (calcFunc-rreduce . 2)
  656. (calcFunc-accum . 2)
  657. (calcFunc-raccum . 2)
  658. (calcFunc-nest . 2)
  659. (calcFunc-anest . 2)
  660. (calcFunc-fixp . 2)
  661. (calcFunc-afixp . 2))))))
  662. (oper3 (if (eq (nth 2 oper) 'calcFunc-inner)
  663. (calc-get-operator
  664. (format "%s%s, inner (add)" msg dir))
  665. '(0 0 0)))
  666. (args nil)
  667. (nargs (if (> (nth 1 oper) 0)
  668. (nth 1 oper)
  669. (car oper2)))
  670. (n nargs)
  671. (p calc-arg-values))
  672. (while (and p (> n 0))
  673. (or (math-expr-contains (nth 1 oper2) (car p))
  674. (math-expr-contains (nth 1 oper3) (car p))
  675. (setq args (nconc args (list (car p)))
  676. n (1- n)))
  677. (setq p (cdr p)))
  678. (setq oper (list "" nargs
  679. (append
  680. '(calcFunc-lambda)
  681. args
  682. (list (math-build-call
  683. (intern
  684. (concat
  685. (symbol-name (nth 2 oper))
  686. calc-mapping-dir))
  687. (cons (math-calcFunc-to-var
  688. (nth 1 oper2))
  689. (if (eq key ?I)
  690. (cons
  691. (math-calcFunc-to-var
  692. (nth 1 oper3))
  693. args)
  694. args))))))
  695. done t))
  696. (setq done t))))
  697. (t (beep))))
  698. (and nargs (>= nargs 0)
  699. (/= nargs (nth 1 oper))
  700. (error "Must be a %d-argument operator" nargs))
  701. (append (if forcenargs
  702. (cons forcenargs (cdr (cdr oper)))
  703. (cdr oper))
  704. (list
  705. (let ((name (concat (if inv "I" "") (if hyp "H" "")
  706. (if prefix (char-to-string prefix) "")
  707. (char-to-string key))))
  708. (if (> (length name) 3)
  709. (substring name 0 3)
  710. name))))))
  711. ;;; Convert a variable name (as a formula) into a like-looking function name.
  712. (defun math-var-to-calcFunc (f)
  713. (if (eq (car-safe f) 'var)
  714. (if (fboundp (nth 2 f))
  715. (nth 2 f)
  716. (intern (concat "calcFunc-" (symbol-name (nth 1 f)))))
  717. (if (memq (car-safe f) '(lambda calcFunc-lambda))
  718. f
  719. (math-reject-arg f "*Expected a function name"))))
  720. ;;; Convert a function name into a like-looking variable name formula.
  721. (defun math-calcFunc-to-var (f)
  722. (if (symbolp f)
  723. (let* ((func (or (cdr (assq f '( ( + . calcFunc-add )
  724. ( - . calcFunc-sub )
  725. ( * . calcFunc-mul )
  726. ( / . calcFunc-div )
  727. ( ^ . calcFunc-pow )
  728. ( % . calcFunc-mod )
  729. ( neg . calcFunc-neg )
  730. ( | . calcFunc-vconcat ) )))
  731. f))
  732. (base (if (string-match "\\`calcFunc-\\(.+\\)\\'"
  733. (symbol-name func))
  734. (math-match-substring (symbol-name func) 1)
  735. (symbol-name func))))
  736. (list 'var
  737. (intern base)
  738. (intern (concat "var-" base))))
  739. f))
  740. ;;; Expand a function call using "lambda" notation.
  741. (defun math-build-call (f args)
  742. (if (eq (car-safe f) 'calcFunc-lambda)
  743. (if (= (length args) (- (length f) 2))
  744. (math-multi-subst (nth (1- (length f)) f) (cdr f) args)
  745. (calc-record-why "*Wrong number of arguments" f)
  746. (cons 'calcFunc-call (cons (math-calcFunc-to-var f) args)))
  747. (if (and (eq f 'calcFunc-neg)
  748. (= (length args) 1))
  749. (list 'neg (car args))
  750. (let ((func (assq f '( ( calcFunc-add . + )
  751. ( calcFunc-sub . - )
  752. ( calcFunc-mul . * )
  753. ( calcFunc-div . / )
  754. ( calcFunc-pow . ^ )
  755. ( calcFunc-mod . % )
  756. ( calcFunc-vconcat . | ) ))))
  757. (if (and func (= (length args) 2))
  758. (cons (cdr func) args)
  759. (cons f args))))))
  760. ;;; Do substitutions in parallel to avoid crosstalk.
  761. ;; The variables math-ms-temp and math-ms-args are local to
  762. ;; math-multi-subst, but are used by math-multi-subst-rec, which
  763. ;; is called by math-multi-subst.
  764. (defvar math-ms-temp)
  765. (defvar math-ms-args)
  766. (defun math-multi-subst (expr olds news)
  767. (let ((math-ms-args nil)
  768. math-ms-temp)
  769. (while (and olds news)
  770. (setq math-ms-args (cons (cons (car olds) (car news)) math-ms-args)
  771. olds (cdr olds)
  772. news (cdr news)))
  773. (math-multi-subst-rec expr)))
  774. (defun math-multi-subst-rec (expr)
  775. (cond ((setq math-ms-temp (assoc expr math-ms-args))
  776. (cdr math-ms-temp))
  777. ((Math-primp expr) expr)
  778. ((and (eq (car expr) 'calcFunc-lambda) (> (length expr) 2))
  779. (let ((new (list (car expr)))
  780. (math-ms-args math-ms-args))
  781. (while (cdr (setq expr (cdr expr)))
  782. (setq new (cons (car expr) new))
  783. (if (assoc (car expr) math-ms-args)
  784. (setq math-ms-args (cons (cons (car expr) (car expr))
  785. math-ms-args))))
  786. (nreverse (cons (math-multi-subst-rec (car expr)) new))))
  787. (t
  788. (cons (car expr)
  789. (mapcar 'math-multi-subst-rec (cdr expr))))))
  790. (defun calcFunc-call (f &rest args)
  791. (setq args (math-build-call (math-var-to-calcFunc f) args))
  792. (if (eq (car-safe args) 'calcFunc-call)
  793. args
  794. (math-normalize args)))
  795. (defun calcFunc-apply (f args)
  796. (or (Math-vectorp args)
  797. (math-reject-arg args 'vectorp))
  798. (apply 'calcFunc-call (cons f (cdr args))))
  799. ;;; Map a function over a vector symbolically. [Public]
  800. (defun math-symb-map (f mode args)
  801. (let* ((func (math-var-to-calcFunc f))
  802. (nargs (length args))
  803. (ptrs (vconcat args))
  804. (vflags (make-vector nargs nil))
  805. (heads '(vec))
  806. (head nil)
  807. (vec nil)
  808. (i -1)
  809. (math-working-step 0)
  810. (math-working-step-2 nil)
  811. len cols obj expr)
  812. (if (eq mode 'eqn)
  813. (setq mode 'elems
  814. heads '(calcFunc-eq calcFunc-neq calcFunc-lt calcFunc-gt
  815. calcFunc-leq calcFunc-geq))
  816. (while (and (< (setq i (1+ i)) nargs)
  817. (not (math-matrixp (aref ptrs i)))))
  818. (if (< i nargs)
  819. (if (eq mode 'elems)
  820. (setq func (list 'lambda '(&rest x)
  821. (list 'math-symb-map
  822. (list 'quote f) '(quote elems) 'x))
  823. mode 'rows)
  824. (if (eq mode 'cols)
  825. (while (< i nargs)
  826. (if (math-matrixp (aref ptrs i))
  827. (aset ptrs i (math-transpose (aref ptrs i))))
  828. (setq i (1+ i)))))
  829. (setq mode 'elems))
  830. (setq i -1))
  831. (while (< (setq i (1+ i)) nargs)
  832. (setq obj (aref ptrs i))
  833. (if (and (memq (car-safe obj) heads)
  834. (or (eq mode 'elems)
  835. (math-matrixp obj)))
  836. (progn
  837. (aset vflags i t)
  838. (if head
  839. (if (cdr heads)
  840. (setq head (nth
  841. (aref (aref [ [0 1 2 3 4 5]
  842. [1 1 2 3 2 3]
  843. [2 2 2 1 2 1]
  844. [3 3 1 3 1 3]
  845. [4 2 2 1 4 1]
  846. [5 3 1 3 1 5] ]
  847. (- 6 (length (memq head heads))))
  848. (- 6 (length (memq (car obj) heads))))
  849. heads)))
  850. (setq head (car obj)))
  851. (if len
  852. (or (= (length obj) len)
  853. (math-dimension-error))
  854. (setq len (length obj))))))
  855. (or len
  856. (if (= nargs 1)
  857. (math-reject-arg (aref ptrs 0) 'vectorp)
  858. (math-reject-arg nil "At least one argument must be a vector")))
  859. (setq math-working-step-2 (1- len))
  860. (while (> (setq len (1- len)) 0)
  861. (setq expr nil
  862. i -1)
  863. (while (< (setq i (1+ i)) nargs)
  864. (if (aref vflags i)
  865. (progn
  866. (aset ptrs i (cdr (aref ptrs i)))
  867. (setq expr (nconc expr (list (car (aref ptrs i))))))
  868. (setq expr (nconc expr (list (aref ptrs i))))))
  869. (setq math-working-step (1+ math-working-step)
  870. vec (cons (math-normalize (math-build-call func expr)) vec)))
  871. (setq vec (cons head (nreverse vec)))
  872. (if (and (eq mode 'cols) (math-matrixp vec))
  873. (math-transpose vec)
  874. vec)))
  875. (defun calcFunc-map (func &rest args)
  876. (math-symb-map func 'elems args))
  877. (defun calcFunc-mapr (func &rest args)
  878. (math-symb-map func 'rows args))
  879. (defun calcFunc-mapc (func &rest args)
  880. (math-symb-map func 'cols args))
  881. (defun calcFunc-mapa (func arg)
  882. (if (math-matrixp arg)
  883. (math-symb-map func 'elems (cdr (math-transpose arg)))
  884. (math-symb-map func 'elems arg)))
  885. (defun calcFunc-mapd (func arg)
  886. (if (math-matrixp arg)
  887. (math-symb-map func 'elems (cdr arg))
  888. (math-symb-map func 'elems arg)))
  889. (defun calcFunc-mapeq (func &rest args)
  890. (if (and (or (equal func '(var mul var-mul))
  891. (equal func '(var div var-div)))
  892. (= (length args) 2))
  893. (if (math-negp (car args))
  894. (let ((func (nth 1 (assq (car-safe (nth 1 args))
  895. calc-tweak-eqn-table))))
  896. (and func (setq args (list (car args)
  897. (cons func (cdr (nth 1 args)))))))
  898. (if (math-negp (nth 1 args))
  899. (let ((func (nth 1 (assq (car-safe (car args))
  900. calc-tweak-eqn-table))))
  901. (and func (setq args (list (cons func (cdr (car args)))
  902. (nth 1 args))))))))
  903. (if (or (and (equal func '(var div var-div))
  904. (assq (car-safe (nth 1 args)) calc-tweak-eqn-table))
  905. (equal func '(var neg var-neg))
  906. (equal func '(var inv var-inv)))
  907. (apply 'calcFunc-mapeqr func args)
  908. (apply 'calcFunc-mapeqp func args)))
  909. (defun calcFunc-mapeqr (func &rest args)
  910. (setq args (mapcar (function (lambda (x)
  911. (let ((func (assq (car-safe x)
  912. calc-tweak-eqn-table)))
  913. (if func
  914. (cons (nth 1 func) (cdr x))
  915. x))))
  916. args))
  917. (apply 'calcFunc-mapeqp func args))
  918. (defun calcFunc-mapeqp (func &rest args)
  919. (if (or (and (memq (car-safe (car args)) '(calcFunc-lt calcFunc-leq))
  920. (memq (car-safe (nth 1 args)) '(calcFunc-gt calcFunc-geq)))
  921. (and (memq (car-safe (car args)) '(calcFunc-gt calcFunc-geq))
  922. (memq (car-safe (nth 1 args)) '(calcFunc-lt calcFunc-leq))))
  923. (setq args (cons (car args)
  924. (cons (list (nth 1 (assq (car (nth 1 args))
  925. calc-tweak-eqn-table))
  926. (nth 2 (nth 1 args))
  927. (nth 1 (nth 1 args)))
  928. (cdr (cdr args))))))
  929. (math-symb-map func 'eqn args))
  930. ;;; Reduce a function over a vector symbolically. [Public]
  931. (defun calcFunc-reduce (func vec)
  932. (if (math-matrixp vec)
  933. (let (expr row)
  934. (setq func (math-var-to-calcFunc func))
  935. (while (setq vec (cdr vec))
  936. (setq row (car vec))
  937. (while (setq row (cdr row))
  938. (setq expr (if expr
  939. (if (Math-numberp expr)
  940. (math-normalize
  941. (math-build-call func (list expr (car row))))
  942. (math-build-call func (list expr (car row))))
  943. (car row)))))
  944. (math-normalize expr))
  945. (calcFunc-reducer func vec)))
  946. (defun calcFunc-rreduce (func vec)
  947. (if (math-matrixp vec)
  948. (let (expr row)
  949. (setq func (math-var-to-calcFunc func)
  950. vec (reverse (cdr vec)))
  951. (while vec
  952. (setq row (reverse (cdr (car vec))))
  953. (while row
  954. (setq expr (if expr
  955. (math-build-call func (list (car row) expr))
  956. (car row))
  957. row (cdr row)))
  958. (setq vec (cdr vec)))
  959. (math-normalize expr))
  960. (calcFunc-rreducer func vec)))
  961. (defun calcFunc-reducer (func vec)
  962. (setq func (math-var-to-calcFunc func))
  963. (or (math-vectorp vec)
  964. (math-reject-arg vec 'vectorp))
  965. (let ((expr (car (setq vec (cdr vec)))))
  966. (if expr
  967. (progn
  968. (condition-case err
  969. (and (symbolp func)
  970. (let ((lfunc (or (cdr (assq func
  971. '( (calcFunc-add . math-add)
  972. (calcFunc-sub . math-sub)
  973. (calcFunc-mul . math-mul)
  974. (calcFunc-div . math-div)
  975. (calcFunc-pow . math-pow)
  976. (calcFunc-mod . math-mod)
  977. (calcFunc-vconcat .
  978. math-concat) )))
  979. func)))
  980. (while (cdr vec)
  981. (setq expr (funcall lfunc expr (nth 1 vec))
  982. vec (cdr vec)))))
  983. (error nil))
  984. (while (setq vec (cdr vec))
  985. (setq expr (math-build-call func (list expr (car vec)))))
  986. (math-normalize expr))
  987. (or (math-identity-value func)
  988. (math-reject-arg vec "*Vector is empty")))))
  989. (defun math-identity-value (func)
  990. (cdr (assq func '( (calcFunc-add . 0) (calcFunc-sub . 0)
  991. (calcFunc-mul . 1) (calcFunc-div . 1)
  992. (calcFunc-idiv . 1) (calcFunc-fdiv . 1)
  993. (calcFunc-min . (var inf var-inf))
  994. (calcFunc-max . (neg (var inf var-inf)))
  995. (calcFunc-vconcat . (vec))
  996. (calcFunc-append . (vec)) ))))
  997. (defun calcFunc-rreducer (func vec)
  998. (setq func (math-var-to-calcFunc func))
  999. (or (math-vectorp vec)
  1000. (math-reject-arg vec 'vectorp))
  1001. (if (eq func 'calcFunc-sub) ; do this in a way that looks nicer
  1002. (let ((expr (car (setq vec (cdr vec)))))
  1003. (if expr
  1004. (progn
  1005. (while (setq vec (cdr vec))
  1006. (setq expr (math-build-call func (list expr (car vec)))
  1007. func (if (eq func 'calcFunc-sub)
  1008. 'calcFunc-add 'calcFunc-sub)))
  1009. (math-normalize expr))
  1010. 0))
  1011. (let ((expr (car (setq vec (reverse (cdr vec))))))
  1012. (if expr
  1013. (progn
  1014. (while (setq vec (cdr vec))
  1015. (setq expr (math-build-call func (list (car vec) expr))))
  1016. (math-normalize expr))
  1017. (or (math-identity-value func)
  1018. (math-reject-arg vec "*Vector is empty"))))))
  1019. (defun calcFunc-reducec (func vec)
  1020. (if (math-matrixp vec)
  1021. (calcFunc-reducer func (math-transpose vec))
  1022. (calcFunc-reducer func vec)))
  1023. (defun calcFunc-rreducec (func vec)
  1024. (if (math-matrixp vec)
  1025. (calcFunc-rreducer func (math-transpose vec))
  1026. (calcFunc-rreducer func vec)))
  1027. (defun calcFunc-reducea (func vec)
  1028. (if (math-matrixp vec)
  1029. (cons 'vec
  1030. (mapcar (function (lambda (x) (calcFunc-reducer func x)))
  1031. (cdr vec)))
  1032. (calcFunc-reducer func vec)))
  1033. (defun calcFunc-rreducea (func vec)
  1034. (if (math-matrixp vec)
  1035. (cons 'vec
  1036. (mapcar (function (lambda (x) (calcFunc-rreducer func x)))
  1037. (cdr vec)))
  1038. (calcFunc-rreducer func vec)))
  1039. (defun calcFunc-reduced (func vec)
  1040. (if (math-matrixp vec)
  1041. (cons 'vec
  1042. (mapcar (function (lambda (x) (calcFunc-reducer func x)))
  1043. (cdr (math-transpose vec))))
  1044. (calcFunc-reducer func vec)))
  1045. (defun calcFunc-rreduced (func vec)
  1046. (if (math-matrixp vec)
  1047. (cons 'vec
  1048. (mapcar (function (lambda (x) (calcFunc-rreducer func x)))
  1049. (cdr (math-transpose vec))))
  1050. (calcFunc-rreducer func vec)))
  1051. (defun calcFunc-accum (func vec)
  1052. (setq func (math-var-to-calcFunc func))
  1053. (or (math-vectorp vec)
  1054. (math-reject-arg vec 'vectorp))
  1055. (let* ((expr (car (setq vec (cdr vec))))
  1056. (res (list 'vec expr)))
  1057. (or expr
  1058. (math-reject-arg vec "*Vector is empty"))
  1059. (while (setq vec (cdr vec))
  1060. (setq expr (math-build-call func (list expr (car vec)))
  1061. res (nconc res (list expr))))
  1062. (math-normalize res)))
  1063. (defun calcFunc-raccum (func vec)
  1064. (setq func (math-var-to-calcFunc func))
  1065. (or (math-vectorp vec)
  1066. (math-reject-arg vec 'vectorp))
  1067. (let* ((expr (car (setq vec (reverse (cdr vec)))))
  1068. (res (list expr)))
  1069. (or expr
  1070. (math-reject-arg vec "*Vector is empty"))
  1071. (while (setq vec (cdr vec))
  1072. (setq expr (math-build-call func (list (car vec) expr))
  1073. res (cons (list expr) res)))
  1074. (math-normalize (cons 'vec res))))
  1075. (defun math-nest-calls (func base iters accum tol)
  1076. (or (symbolp tol)
  1077. (if (math-realp tol)
  1078. (or (math-numberp base) (math-reject-arg base 'numberp))
  1079. (math-reject-arg tol 'realp)))
  1080. (setq func (math-var-to-calcFunc func))
  1081. (or (null iters)
  1082. (if (equal iters '(var inf var-inf))
  1083. (setq iters nil)
  1084. (progn
  1085. (if (math-messy-integerp iters)
  1086. (setq iters (math-trunc iters)))
  1087. (or (integerp iters) (math-reject-arg iters 'fixnump))
  1088. (or (not tol) (natnump iters) (math-reject-arg iters 'fixnatnump))
  1089. (if (< iters 0)
  1090. (let* ((dummy '(var DummyArg var-DummyArg))
  1091. (dummy2 '(var DummyArg2 var-DummyArg2))
  1092. (finv (math-solve-for (math-build-call func (list dummy2))
  1093. dummy dummy2 nil)))
  1094. (or finv (math-reject-arg nil "*Unable to find an inverse"))
  1095. (if (and (= (length finv) 2)
  1096. (equal (nth 1 finv) dummy))
  1097. (setq func (car finv))
  1098. (setq func (list 'calcFunc-lambda dummy finv)))
  1099. (setq iters (- iters)))))))
  1100. (math-with-extra-prec 1
  1101. (let ((value base)
  1102. (ovalue nil)
  1103. (avalues (list base))
  1104. (math-working-step 0)
  1105. (math-working-step-2 iters))
  1106. (while (and (or (null iters)
  1107. (>= (setq iters (1- iters)) 0))
  1108. (or (null tol)
  1109. (null ovalue)
  1110. (if (eq tol t)
  1111. (not (if (and (Math-numberp value)
  1112. (Math-numberp ovalue))
  1113. (math-nearly-equal value ovalue)
  1114. (Math-equal value ovalue)))
  1115. (if (math-numberp value)
  1116. (Math-lessp tol (math-abs (math-sub value ovalue)))
  1117. (math-reject-arg value 'numberp)))))
  1118. (setq ovalue value
  1119. math-working-step (1+ math-working-step)
  1120. value (math-normalize (math-build-call func (list value))))
  1121. (if accum
  1122. (setq avalues (cons value avalues))))
  1123. (if accum
  1124. (cons 'vec (nreverse avalues))
  1125. value))))
  1126. (defun calcFunc-nest (func base iters)
  1127. (math-nest-calls func base iters nil nil))
  1128. (defun calcFunc-anest (func base iters)
  1129. (math-nest-calls func base iters t nil))
  1130. (defun calcFunc-fixp (func base &optional iters tol)
  1131. (math-nest-calls func base iters nil (or tol t)))
  1132. (defun calcFunc-afixp (func base &optional iters tol)
  1133. (math-nest-calls func base iters t (or tol t)))
  1134. (defun calcFunc-outer (func a b)
  1135. (or (math-vectorp a) (math-reject-arg a 'vectorp))
  1136. (or (math-vectorp b) (math-reject-arg b 'vectorp))
  1137. (setq func (math-var-to-calcFunc func))
  1138. (let ((mat nil))
  1139. (while (setq a (cdr a))
  1140. (setq mat (cons (cons 'vec
  1141. (mapcar (function (lambda (x)
  1142. (math-build-call func
  1143. (list (car a)
  1144. x))))
  1145. (cdr b)))
  1146. mat)))
  1147. (math-normalize (cons 'vec (nreverse mat)))))
  1148. ;; The variables math-inner-mul-func and math-inner-add-func are
  1149. ;; local to calcFunc-inner, but are used by math-inner-mats,
  1150. ;; which is called by math-inner-mats.
  1151. (defvar math-inner-mul-func)
  1152. (defvar math-inner-add-func)
  1153. (defun calcFunc-inner (math-inner-mul-func math-inner-add-func a b)
  1154. (or (math-vectorp a) (math-reject-arg a 'vectorp))
  1155. (or (math-vectorp b) (math-reject-arg b 'vectorp))
  1156. (if (math-matrixp a)
  1157. (if (math-matrixp b)
  1158. (if (= (length (nth 1 a)) (length b))
  1159. (math-inner-mats a b)
  1160. (math-dimension-error))
  1161. (if (= (length (nth 1 a)) 2)
  1162. (if (= (length a) (length b))
  1163. (math-inner-mats a (list 'vec b))
  1164. (math-dimension-error))
  1165. (if (= (length (nth 1 a)) (length b))
  1166. (math-mat-col (math-inner-mats a (math-col-matrix b))
  1167. 1)
  1168. (math-dimension-error))))
  1169. (if (math-matrixp b)
  1170. (nth 1 (math-inner-mats (list 'vec a) b))
  1171. (calcFunc-reduce math-inner-add-func (calcFunc-map math-inner-mul-func a b)))))
  1172. (defun math-inner-mats (a b)
  1173. (let ((mat nil)
  1174. (cols (length (nth 1 b)))
  1175. row col ap bp accum)
  1176. (while (setq a (cdr a))
  1177. (setq col cols
  1178. row nil)
  1179. (while (> (setq col (1- col)) 0)
  1180. (setq row (cons (calcFunc-reduce math-inner-add-func
  1181. (calcFunc-map math-inner-mul-func
  1182. (car a)
  1183. (math-mat-col b col)))
  1184. row)))
  1185. (setq mat (cons (cons 'vec row) mat)))
  1186. (cons 'vec (nreverse mat))))
  1187. (provide 'calc-map)
  1188. ;;; calc-map.el ends here