calcalg3.el 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. ;;; calcalg3.el --- more algebraic 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 calc-fit-s-shaped-logistic-curve "calc-nlfit" (arg))
  23. (declare-function calc-fit-bell-shaped-logistic-curve "calc-nlfit" (arg))
  24. (declare-function calc-fit-hubbert-linear-curve "calc-nlfit" (&optional sdv))
  25. (declare-function calc-graph-add-curve "calc-graph" (xdata ydata &optional zdata))
  26. (declare-function calc-graph-lookup "calc-graph" (thing))
  27. (declare-function calc-graph-set-styles "calc-graph" (lines points &optional yerr))
  28. (declare-function math-min-list "calc-arith" (a b))
  29. (declare-function math-max-list "calc-arith" (a b))
  30. (defun math-map-binop (binop args1 args2)
  31. "Apply BINOP to the elements of the lists ARGS1 and ARGS2"
  32. (if args1
  33. (cons
  34. (funcall binop (car args1) (car args2))
  35. (funcall 'math-map-binop binop (cdr args1) (cdr args2)))))
  36. (defun calc-find-root (var)
  37. (interactive "sVariable(s) to solve for: ")
  38. (calc-slow-wrapper
  39. (let ((func (if (calc-is-hyperbolic) 'calcFunc-wroot 'calcFunc-root)))
  40. (if (or (equal var "") (equal var "$"))
  41. (calc-enter-result 2 "root" (list func
  42. (calc-top-n 3)
  43. (calc-top-n 1)
  44. (calc-top-n 2)))
  45. (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
  46. (not (string-match "\\[" var)))
  47. (math-read-expr (concat "[" var "]"))
  48. (math-read-expr var))))
  49. (if (eq (car-safe var) 'error)
  50. (error "Bad format in expression: %s" (nth 1 var)))
  51. (calc-enter-result 1 "root" (list func
  52. (calc-top-n 2)
  53. var
  54. (calc-top-n 1))))))))
  55. (defun calc-find-minimum (var)
  56. (interactive "sVariable(s) to minimize over: ")
  57. (calc-slow-wrapper
  58. (let ((func (if (calc-is-inverse)
  59. (if (calc-is-hyperbolic)
  60. 'calcFunc-wmaximize 'calcFunc-maximize)
  61. (if (calc-is-hyperbolic)
  62. 'calcFunc-wminimize 'calcFunc-minimize)))
  63. (tag (if (calc-is-inverse) "max" "min")))
  64. (if (or (equal var "") (equal var "$"))
  65. (calc-enter-result 2 tag (list func
  66. (calc-top-n 3)
  67. (calc-top-n 1)
  68. (calc-top-n 2)))
  69. (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
  70. (not (string-match "\\[" var)))
  71. (math-read-expr (concat "[" var "]"))
  72. (math-read-expr var))))
  73. (if (eq (car-safe var) 'error)
  74. (error "Bad format in expression: %s" (nth 1 var)))
  75. (calc-enter-result 1 tag (list func
  76. (calc-top-n 2)
  77. var
  78. (calc-top-n 1))))))))
  79. (defun calc-find-maximum (var)
  80. (interactive "sVariable to maximize over: ")
  81. (calc-invert-func)
  82. (calc-find-minimum var))
  83. (defun calc-poly-interp (arg)
  84. (interactive "P")
  85. (calc-slow-wrapper
  86. (let ((data (calc-top 2)))
  87. (if (or (consp arg) (eq arg 0) (eq arg 2))
  88. (setq data (cons 'vec (calc-top-list 2 2)))
  89. (or (null arg)
  90. (error "Bad prefix argument")))
  91. (if (calc-is-hyperbolic)
  92. (calc-enter-result 1 "rati" (list 'calcFunc-ratint data (calc-top 1)))
  93. (calc-enter-result 1 "poli" (list 'calcFunc-polint data
  94. (calc-top 1)))))))
  95. ;; The variables calc-curve-nvars, calc-curve-varnames, calc-curve-model and calc-curve-coefnames are local to calc-curve-fit, but are
  96. ;; used by calc-get-fit-variables which is called by calc-curve-fit.
  97. (defvar calc-curve-nvars)
  98. (defvar calc-curve-varnames)
  99. (defvar calc-curve-model)
  100. (defvar calc-curve-coefnames)
  101. (defvar calc-curve-fit-history nil
  102. "History for calc-curve-fit.")
  103. (defun calc-curve-fit (arg &optional calc-curve-model
  104. calc-curve-coefnames calc-curve-varnames)
  105. (interactive "P")
  106. (calc-slow-wrapper
  107. (setq calc-aborted-prefix nil)
  108. (let ((func (if (calc-is-inverse) 'calcFunc-xfit
  109. (if (calc-is-hyperbolic) 'calcFunc-efit
  110. 'calcFunc-fit)))
  111. key (which 0)
  112. (nonlinear nil)
  113. (plot nil)
  114. n calc-curve-nvars temp data
  115. (homog nil)
  116. (msgs '( "(Press ? for help)"
  117. "1 = linear or multilinear"
  118. "2-9 = polynomial fits; i = interpolating polynomial"
  119. "p = a x^b, ^ = a b^x"
  120. "e = a exp(b x), x = exp(a + b x), l = a + b ln(x)"
  121. "E = a 10^(b x), X = 10^(a + b x), L = a + b log10(x)"
  122. "q = a + b (x-c)^2"
  123. "g = (a/b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)"
  124. "s = a/(1 + exp(b (x - c)))"
  125. "b = a exp(b (x - c))/(1 + exp(b (x - c)))^2"
  126. "o = (y/x) = a (1 - x/b)"
  127. "h prefix = homogeneous model (no constant term)"
  128. "P prefix = plot result"
  129. "' = alg entry, $ = stack, u = Model1, U = Model2")))
  130. (while (not calc-curve-model)
  131. (message
  132. "Fit to model: %s:%s%s"
  133. (nth which msgs)
  134. (if plot "P" " ")
  135. (if homog "h" ""))
  136. (setq key (read-char))
  137. (cond ((= key ?\C-g)
  138. (keyboard-quit))
  139. ((= key ??)
  140. (setq which (% (1+ which) (length msgs))))
  141. ((memq key '(?h ?H))
  142. (setq homog (not homog)))
  143. ((= key ?P)
  144. (if plot
  145. (setq plot nil)
  146. (let ((data (calc-top 1)))
  147. (if (or
  148. (calc-is-hyperbolic)
  149. (calc-is-inverse)
  150. (not (= (length data) 3)))
  151. (setq plot "Can't plot")
  152. (setq plot data)))))
  153. ((progn
  154. (if (eq key ?\$)
  155. (setq n 1)
  156. (setq n 0))
  157. (cond ((null arg)
  158. (setq n (1+ n)
  159. data (calc-top n)))
  160. ((or (consp arg) (eq arg 0))
  161. (setq n (+ n 2)
  162. data (calc-top n)
  163. data (if (math-matrixp data)
  164. (append data (list (calc-top (1- n))))
  165. (list 'vec data (calc-top (1- n))))))
  166. ((> (setq arg (prefix-numeric-value arg)) 0)
  167. (setq data (cons 'vec (calc-top-list arg (1+ n)))
  168. n (+ n arg)))
  169. (t (error "Bad prefix argument")))
  170. (or (math-matrixp data) (not (cdr (cdr data)))
  171. (error "Data matrix is not a matrix!"))
  172. (setq calc-curve-nvars (- (length data) 2)
  173. calc-curve-coefnames nil
  174. calc-curve-varnames nil)
  175. nil))
  176. ((= key ?1) ; linear or multilinear
  177. (calc-get-fit-variables calc-curve-nvars
  178. (1+ calc-curve-nvars) (and homog 0))
  179. (setq calc-curve-model
  180. (math-mul calc-curve-coefnames
  181. (cons 'vec (cons 1 (cdr calc-curve-varnames))))))
  182. ((and (>= key ?2) (<= key ?9)) ; polynomial
  183. (calc-get-fit-variables 1 (- key ?0 -1) (and homog 0))
  184. (setq calc-curve-model
  185. (math-build-polynomial-expr (cdr calc-curve-coefnames)
  186. (nth 1 calc-curve-varnames))))
  187. ((= key ?i) ; exact polynomial
  188. (calc-get-fit-variables 1 (1- (length (nth 1 data)))
  189. (and homog 0))
  190. (setq calc-curve-model
  191. (math-build-polynomial-expr (cdr calc-curve-coefnames)
  192. (nth 1 calc-curve-varnames))))
  193. ((= key ?p) ; power law
  194. (calc-get-fit-variables calc-curve-nvars
  195. (1+ calc-curve-nvars) (and homog 1))
  196. (setq calc-curve-model
  197. (math-mul
  198. (nth 1 calc-curve-coefnames)
  199. (calcFunc-reduce
  200. '(var mul var-mul)
  201. (calcFunc-map
  202. '(var pow var-pow)
  203. calc-curve-varnames
  204. (cons 'vec (cdr (cdr calc-curve-coefnames))))))))
  205. ((= key ?^) ; exponential law
  206. (calc-get-fit-variables calc-curve-nvars
  207. (1+ calc-curve-nvars) (and homog 1))
  208. (setq calc-curve-model
  209. (math-mul (nth 1 calc-curve-coefnames)
  210. (calcFunc-reduce
  211. '(var mul var-mul)
  212. (calcFunc-map
  213. '(var pow var-pow)
  214. (cons 'vec (cdr (cdr calc-curve-coefnames)))
  215. calc-curve-varnames)))))
  216. ((= key ?s)
  217. (setq nonlinear t)
  218. (setq calc-curve-model t)
  219. (require 'calc-nlfit)
  220. (calc-fit-s-shaped-logistic-curve func))
  221. ((= key ?b)
  222. (setq nonlinear t)
  223. (setq calc-curve-model t)
  224. (require 'calc-nlfit)
  225. (calc-fit-bell-shaped-logistic-curve func))
  226. ((= key ?o)
  227. (setq nonlinear t)
  228. (setq calc-curve-model t)
  229. (require 'calc-nlfit)
  230. (if (and plot (not (stringp plot)))
  231. (setq plot
  232. (list 'vec
  233. (nth 1 plot)
  234. (cons
  235. 'vec
  236. (math-map-binop 'calcFunc-div
  237. (cdr (nth 2 plot))
  238. (cdr (nth 1 plot)))))))
  239. (calc-fit-hubbert-linear-curve func))
  240. ((memq key '(?e ?E))
  241. (calc-get-fit-variables calc-curve-nvars
  242. (1+ calc-curve-nvars) (and homog 1))
  243. (setq calc-curve-model
  244. (math-mul (nth 1 calc-curve-coefnames)
  245. (calcFunc-reduce
  246. '(var mul var-mul)
  247. (calcFunc-map
  248. (if (eq key ?e)
  249. '(var exp var-exp)
  250. '(calcFunc-lambda
  251. (var a var-a)
  252. (^ 10 (var a var-a))))
  253. (calcFunc-map
  254. '(var mul var-mul)
  255. (cons 'vec (cdr (cdr calc-curve-coefnames)))
  256. calc-curve-varnames))))))
  257. ((memq key '(?x ?X))
  258. (calc-get-fit-variables calc-curve-nvars
  259. (1+ calc-curve-nvars) (and homog 0))
  260. (setq calc-curve-model
  261. (math-mul calc-curve-coefnames
  262. (cons 'vec (cons 1 (cdr calc-curve-varnames)))))
  263. (setq calc-curve-model (if (eq key ?x)
  264. (list 'calcFunc-exp calc-curve-model)
  265. (list '^ 10 calc-curve-model))))
  266. ((memq key '(?l ?L))
  267. (calc-get-fit-variables calc-curve-nvars
  268. (1+ calc-curve-nvars) (and homog 0))
  269. (setq calc-curve-model
  270. (math-mul calc-curve-coefnames
  271. (cons 'vec
  272. (cons 1 (cdr (calcFunc-map
  273. (if (eq key ?l)
  274. '(var ln var-ln)
  275. '(var log10
  276. var-log10))
  277. calc-curve-varnames)))))))
  278. ((= key ?q)
  279. (calc-get-fit-variables calc-curve-nvars
  280. (1+ (* 2 calc-curve-nvars)) (and homog 0))
  281. (let ((c calc-curve-coefnames)
  282. (v calc-curve-varnames))
  283. (setq calc-curve-model (nth 1 c))
  284. (while (setq v (cdr v) c (cdr (cdr c)))
  285. (setq calc-curve-model (math-add
  286. calc-curve-model
  287. (list '*
  288. (car c)
  289. (list '^
  290. (list '- (car v) (nth 1 c))
  291. 2)))))))
  292. ((= key ?g)
  293. (setq
  294. calc-curve-model
  295. (math-read-expr
  296. "(AFit / BFit sqrt(2 pi)) exp(-0.5 * ((XFit - CFit) / BFit)^2)")
  297. calc-curve-varnames '(vec (var XFit var-XFit))
  298. calc-curve-coefnames '(vec (var AFit var-AFit)
  299. (var BFit var-BFit)
  300. (var CFit var-CFit)))
  301. (calc-get-fit-variables 1 (1- (length calc-curve-coefnames))
  302. (and homog 1)))
  303. ((memq key '(?\$ ?\' ?u ?U))
  304. (let* ((defvars nil)
  305. (record-entry nil))
  306. (if (eq key ?\')
  307. (let* ((calc-dollar-values calc-arg-values)
  308. (calc-dollar-used 0)
  309. (calc-hashes-used 0))
  310. (setq calc-curve-model
  311. (calc-do-alg-entry "" "Model formula: "
  312. nil 'calc-curve-fit-history))
  313. (if (/= (length calc-curve-model) 1)
  314. (error "Bad format"))
  315. (setq calc-curve-model (car calc-curve-model)
  316. record-entry t)
  317. (if (> calc-dollar-used 0)
  318. (setq calc-curve-coefnames
  319. (cons 'vec
  320. (nthcdr (- (length calc-arg-values)
  321. calc-dollar-used)
  322. (reverse calc-arg-values))))
  323. (if (> calc-hashes-used 0)
  324. (setq calc-curve-coefnames
  325. (cons 'vec (calc-invent-args
  326. calc-hashes-used))))))
  327. (progn
  328. (setq calc-curve-model (cond ((eq key ?u)
  329. (calc-var-value 'var-Model1))
  330. ((eq key ?U)
  331. (calc-var-value 'var-Model2))
  332. (t (calc-top 1))))
  333. (or calc-curve-model (error "User model not yet defined"))
  334. (if (math-vectorp calc-curve-model)
  335. (if (and (memq (length calc-curve-model) '(3 4))
  336. (not (math-objvecp (nth 1 calc-curve-model)))
  337. (math-vectorp (nth 2 calc-curve-model))
  338. (or (null (nth 3 calc-curve-model))
  339. (math-vectorp (nth 3 calc-curve-model))))
  340. (setq calc-curve-varnames (nth 2 calc-curve-model)
  341. calc-curve-coefnames
  342. (or (nth 3 calc-curve-model)
  343. (cons 'vec
  344. (math-all-vars-but
  345. calc-curve-model
  346. calc-curve-varnames)))
  347. calc-curve-model (nth 1 calc-curve-model))
  348. (error "Incorrect model specifier")))))
  349. (or calc-curve-varnames
  350. (let ((with-y
  351. (eq (car-safe calc-curve-model) 'calcFunc-eq)))
  352. (if calc-curve-coefnames
  353. (calc-get-fit-variables
  354. (if with-y (1+ calc-curve-nvars) calc-curve-nvars)
  355. (1- (length calc-curve-coefnames))
  356. (math-all-vars-but
  357. calc-curve-model calc-curve-coefnames)
  358. nil with-y)
  359. (let* ((coefs (math-all-vars-but calc-curve-model nil))
  360. (vars nil)
  361. (n (-
  362. (length coefs)
  363. calc-curve-nvars
  364. (if with-y 2 1)))
  365. p)
  366. (if (< n 0)
  367. (error "Not enough variables in model"))
  368. (setq p (nthcdr n coefs))
  369. (setq vars (cdr p))
  370. (setcdr p nil)
  371. (calc-get-fit-variables
  372. (if with-y (1+ calc-curve-nvars) calc-curve-nvars)
  373. (length coefs)
  374. vars coefs with-y)))))
  375. (if record-entry
  376. (calc-record (list 'vec calc-curve-model
  377. calc-curve-varnames calc-curve-coefnames)
  378. "modl"))))
  379. (t (beep))))
  380. (unless nonlinear
  381. (let ((calc-fit-to-trail t))
  382. (calc-enter-result n (substring (symbol-name func) 9)
  383. (list func calc-curve-model
  384. (if (= (length calc-curve-varnames) 2)
  385. (nth 1 calc-curve-varnames)
  386. calc-curve-varnames)
  387. (if (= (length calc-curve-coefnames) 2)
  388. (nth 1 calc-curve-coefnames)
  389. calc-curve-coefnames)
  390. data))
  391. (if (consp calc-fit-to-trail)
  392. (calc-record (calc-normalize calc-fit-to-trail) "parm"))))
  393. (when plot
  394. (if (stringp plot)
  395. (message "%s" plot)
  396. (let ((calc-graph-no-auto-view t))
  397. (calc-graph-delete t)
  398. (calc-graph-add-curve
  399. (calc-graph-lookup (nth 1 plot))
  400. (calc-graph-lookup (nth 2 plot)))
  401. (unless (math-contains-sdev-p (nth 2 data))
  402. (calc-graph-set-styles nil nil)
  403. (calc-graph-point-style nil))
  404. (setq plot (cdr (nth 1 plot)))
  405. (setq plot
  406. (list 'intv
  407. 3
  408. (math-sub
  409. (math-min-list (car plot) (cdr plot))
  410. '(float 5 -1))
  411. (math-add
  412. '(float 5 -1)
  413. (math-max-list (car plot) (cdr plot)))))
  414. (calc-graph-add-curve (calc-graph-lookup plot)
  415. (calc-graph-lookup (calc-top-n 1)))
  416. (calc-graph-plot nil)))))))
  417. (defun calc-invent-independent-variables (n &optional but)
  418. (calc-invent-variables n but '(x y z t) "x"))
  419. (defun calc-invent-parameter-variables (n &optional but)
  420. (calc-invent-variables n but '(a b c d) "a"))
  421. (defun calc-invent-variables (num but names base)
  422. (let ((vars nil)
  423. (n num) (nn 0)
  424. var)
  425. (while (and (> n 0) names)
  426. (setq var (math-build-var-name (if (consp names)
  427. (car names)
  428. (concat base (int-to-string
  429. (setq nn (1+ nn)))))))
  430. (or (math-expr-contains (cons 'vec but) var)
  431. (setq vars (cons var vars)
  432. n (1- n)))
  433. (or (symbolp names) (setq names (cdr names))))
  434. (if (= n 0)
  435. (nreverse vars)
  436. (calc-invent-variables num but t base))))
  437. (defun calc-get-fit-variables (nv nc &optional defv defc with-y homog)
  438. (or (= nv (if with-y (1+ calc-curve-nvars) calc-curve-nvars))
  439. (error "Wrong number of data vectors for this type of model"))
  440. (if (integerp defv)
  441. (setq homog defv
  442. defv nil))
  443. (if homog
  444. (setq nc (1- nc)))
  445. (or defv
  446. (setq defv (calc-invent-independent-variables nv)))
  447. (or defc
  448. (setq defc (calc-invent-parameter-variables nc defv)))
  449. (let ((vars (read-string (format "Fitting variables (default %s; %s): "
  450. (mapconcat 'symbol-name
  451. (mapcar (function (lambda (v)
  452. (nth 1 v)))
  453. defv)
  454. ",")
  455. (mapconcat 'symbol-name
  456. (mapcar (function (lambda (v)
  457. (nth 1 v)))
  458. defc)
  459. ","))))
  460. (coefs nil))
  461. (setq vars (if (string-match "\\[" vars)
  462. (math-read-expr vars)
  463. (math-read-expr (concat "[" vars "]"))))
  464. (if (eq (car-safe vars) 'error)
  465. (error "Bad format in expression: %s" (nth 2 vars)))
  466. (or (math-vectorp vars)
  467. (error "Expected a variable or vector of variables"))
  468. (if (equal vars '(vec))
  469. (setq vars (cons 'vec defv)
  470. coefs (cons 'vec defc))
  471. (if (math-vectorp (nth 1 vars))
  472. (if (and (= (length vars) 3)
  473. (math-vectorp (nth 2 vars)))
  474. (setq coefs (nth 2 vars)
  475. vars (nth 1 vars))
  476. (error
  477. "Expected independent variables vector, then parameters vector"))
  478. (setq coefs (cons 'vec defc))))
  479. (or (= nv (1- (length vars)))
  480. (and (not with-y) (= (1+ nv) (1- (length vars))))
  481. (error "Expected %d independent variable%s" nv (if (= nv 1) "" "s")))
  482. (or (= nc (1- (length coefs)))
  483. (error "Expected %d parameter variable%s" nc (if (= nc 1) "" "s")))
  484. (if homog
  485. (setq coefs (cons 'vec (cons homog (cdr coefs)))))
  486. (if calc-curve-varnames
  487. (setq calc-curve-model (math-multi-subst calc-curve-model (cdr calc-curve-varnames) (cdr vars))))
  488. (if calc-curve-coefnames
  489. (setq calc-curve-model (math-multi-subst calc-curve-model (cdr calc-curve-coefnames) (cdr coefs))))
  490. (setq calc-curve-varnames vars
  491. calc-curve-coefnames coefs)))
  492. ;;; The following algorithms are from Numerical Recipes chapter 9.
  493. ;;; "rtnewt" with safety kludges
  494. (defvar var-DUMMY)
  495. (defun math-newton-root (expr deriv guess orig-guess limit)
  496. (math-working "newton" guess)
  497. (let* ((var-DUMMY guess)
  498. next dval)
  499. (setq next (math-evaluate-expr expr)
  500. dval (math-evaluate-expr deriv))
  501. (if (and (Math-numberp next)
  502. (Math-numberp dval)
  503. (not (Math-zerop dval)))
  504. (progn
  505. (setq next (math-sub guess (math-div next dval)))
  506. (if (math-nearly-equal guess (setq next (math-float next)))
  507. (progn
  508. (setq var-DUMMY next)
  509. (list 'vec next (math-evaluate-expr expr)))
  510. (if (Math-lessp (math-abs-approx (math-sub next orig-guess))
  511. limit)
  512. (math-newton-root expr deriv next orig-guess limit)
  513. (math-reject-arg next "*Newton's method failed to converge"))))
  514. (math-reject-arg next "*Newton's method encountered a singularity"))))
  515. ;;; Inspired by "rtsafe"
  516. (defun math-newton-search-root (expr deriv guess vguess ostep oostep
  517. low vlow high vhigh)
  518. (let ((var-DUMMY guess)
  519. (better t)
  520. pos step next vnext)
  521. (if guess
  522. (math-working "newton" (list 'intv 0 low high))
  523. (math-working "bisect" (list 'intv 0 low high))
  524. (setq ostep (math-mul-float (math-sub-float high low)
  525. '(float 5 -1))
  526. guess (math-add-float low ostep)
  527. var-DUMMY guess
  528. vguess (math-evaluate-expr expr))
  529. (or (Math-realp vguess)
  530. (progn
  531. (setq ostep (math-mul-float ostep '(float 6 -1))
  532. guess (math-add-float low ostep)
  533. var-DUMMY guess
  534. vguess (math-evaluate-expr expr))
  535. (or (math-realp vguess)
  536. (progn
  537. (setq ostep (math-mul-float ostep '(float 123456 -5))
  538. guess (math-add-float low ostep)
  539. var-DUMMY guess
  540. vguess nil))))))
  541. (or vguess
  542. (setq vguess (math-evaluate-expr expr)))
  543. (or (Math-realp vguess)
  544. (math-reject-arg guess "*Newton's method encountered a singularity"))
  545. (setq vguess (math-float vguess))
  546. (if (eq (Math-negp vlow) (setq pos (Math-posp vguess)))
  547. (setq high guess
  548. vhigh vguess)
  549. (if (eq (Math-negp vhigh) pos)
  550. (setq low guess
  551. vlow vguess)
  552. (setq better nil)))
  553. (if (or (Math-zerop vguess)
  554. (math-nearly-equal low high))
  555. (list 'vec guess vguess)
  556. (setq step (math-evaluate-expr deriv))
  557. (if (and (Math-realp step)
  558. (not (Math-zerop step))
  559. (setq step (math-div-float vguess (math-float step))
  560. next (math-sub-float guess step))
  561. (not (math-lessp-float high next))
  562. (not (math-lessp-float next low)))
  563. (progn
  564. (setq var-DUMMY next
  565. vnext (math-evaluate-expr expr))
  566. (if (or (Math-zerop vnext)
  567. (math-nearly-equal next guess))
  568. (list 'vec next vnext)
  569. (if (and better
  570. (math-lessp-float (math-abs (or oostep
  571. (math-sub-float
  572. high low)))
  573. (math-abs
  574. (math-mul-float '(float 2 0)
  575. step))))
  576. (math-newton-search-root expr deriv nil nil nil ostep
  577. low vlow high vhigh)
  578. (math-newton-search-root expr deriv next vnext step ostep
  579. low vlow high vhigh))))
  580. (if (or (and (Math-posp vlow) (Math-posp vhigh))
  581. (and (Math-negp vlow) (Math-negp vhigh)))
  582. (math-search-root expr deriv low vlow high vhigh)
  583. (math-newton-search-root expr deriv nil nil nil ostep
  584. low vlow high vhigh))))))
  585. ;;; Search for a root in an interval with no overt zero crossing.
  586. ;; The variable math-root-widen is local to math-find-root, but
  587. ;; is used by math-search-root, which is called (directly and
  588. ;; indirectly) by math-find-root.
  589. (defvar math-root-widen)
  590. (defun math-search-root (expr deriv low vlow high vhigh)
  591. (let (found)
  592. (if math-root-widen
  593. (let ((iters 0)
  594. (iterlim (if (eq math-root-widen 'point)
  595. (+ calc-internal-prec 10)
  596. 20))
  597. (factor (if (eq math-root-widen 'point)
  598. '(float 9 0)
  599. '(float 16 -1)))
  600. (prev nil) vprev waslow
  601. diff)
  602. (while (or (and (math-posp vlow) (math-posp vhigh))
  603. (and (math-negp vlow) (math-negp vhigh)))
  604. (math-working "widen" (list 'intv 0 low high))
  605. (if (> (setq iters (1+ iters)) iterlim)
  606. (math-reject-arg (list 'intv 0 low high)
  607. "*Unable to bracket root"))
  608. (if (= iters calc-internal-prec)
  609. (setq factor '(float 16 -1)))
  610. (setq diff (math-mul-float (math-sub-float high low) factor))
  611. (if (Math-zerop diff)
  612. (setq high (calcFunc-incr high 10))
  613. (if (math-lessp-float (math-abs vlow) (math-abs vhigh))
  614. (setq waslow t
  615. prev low
  616. low (math-sub low diff)
  617. var-DUMMY low
  618. vprev vlow
  619. vlow (math-evaluate-expr expr))
  620. (setq waslow nil
  621. prev high
  622. high (math-add high diff)
  623. var-DUMMY high
  624. vprev vhigh
  625. vhigh (math-evaluate-expr expr)))))
  626. (if prev
  627. (if waslow
  628. (setq high prev vhigh vprev)
  629. (setq low prev vlow vprev)))
  630. (setq found t))
  631. (or (Math-realp vlow)
  632. (math-reject-arg vlow 'realp))
  633. (or (Math-realp vhigh)
  634. (math-reject-arg vhigh 'realp))
  635. (let ((xvals (list low high))
  636. (yvals (list vlow vhigh))
  637. (pos (Math-posp vlow))
  638. (levels 0)
  639. (step (math-sub-float high low))
  640. xp yp var-DUMMY)
  641. (while (and (<= (setq levels (1+ levels)) 5)
  642. (not found))
  643. (setq xp xvals
  644. yp yvals
  645. step (math-mul-float step '(float 497 -3)))
  646. (while (and (cdr xp) (not found))
  647. (if (Math-realp (car yp))
  648. (setq low (car xp)
  649. vlow (car yp)))
  650. (setq high (math-add-float (car xp) step)
  651. var-DUMMY high
  652. vhigh (math-evaluate-expr expr))
  653. (math-working "search" high)
  654. (if (and (Math-realp vhigh)
  655. (eq (math-negp vhigh) pos))
  656. (setq found t)
  657. (setcdr xp (cons high (cdr xp)))
  658. (setcdr yp (cons vhigh (cdr yp)))
  659. (setq xp (cdr (cdr xp))
  660. yp (cdr (cdr yp))))))))
  661. (if found
  662. (if (Math-zerop vhigh)
  663. (list 'vec high vhigh)
  664. (if (Math-zerop vlow)
  665. (list 'vec low vlow)
  666. (if deriv
  667. (math-newton-search-root expr deriv nil nil nil nil
  668. low vlow high vhigh)
  669. (math-bisect-root expr low vlow high vhigh))))
  670. (math-reject-arg (list 'intv 3 low high)
  671. "*Unable to find a sign change in this interval"))))
  672. ;;; "rtbis" (but we should be using Brent's method)
  673. (defun math-bisect-root (expr low vlow high vhigh)
  674. (let ((step (math-sub-float high low))
  675. (pos (Math-posp vhigh))
  676. var-DUMMY
  677. mid vmid)
  678. (while (not (or (math-nearly-equal low
  679. (setq step (math-mul-float
  680. step '(float 5 -1))
  681. mid (math-add-float low step)))
  682. (progn
  683. (setq var-DUMMY mid
  684. vmid (math-evaluate-expr expr))
  685. (Math-zerop vmid))))
  686. (math-working "bisect" mid)
  687. (if (eq (Math-posp vmid) pos)
  688. (setq high mid
  689. vhigh vmid)
  690. (setq low mid
  691. vlow vmid)))
  692. (list 'vec mid vmid)))
  693. ;;; "mnewt"
  694. (defvar math-root-vars [(var DUMMY var-DUMMY)])
  695. (defun math-newton-multi (expr jacob n guess orig-guess limit)
  696. (let ((m -1)
  697. (p guess)
  698. p2 expr-val jacob-val next)
  699. (while (< (setq p (cdr p) m (1+ m)) n)
  700. (set (nth 2 (aref math-root-vars m)) (car p)))
  701. (setq expr-val (math-evaluate-expr expr)
  702. jacob-val (math-evaluate-expr jacob))
  703. (unless (and (math-constp expr-val)
  704. (math-constp jacob-val))
  705. (math-reject-arg guess "*Newton's method encountered a singularity"))
  706. (setq next (math-add guess (math-div (math-float (math-neg expr-val))
  707. (math-float jacob-val)))
  708. p guess p2 next)
  709. (math-working "newton" next)
  710. (while (and (setq p (cdr p) p2 (cdr p2))
  711. (math-nearly-equal (car p) (car p2))))
  712. (if p
  713. (if (Math-lessp (math-abs-approx (math-sub next orig-guess))
  714. limit)
  715. (math-newton-multi expr jacob n next orig-guess limit)
  716. (math-reject-arg nil "*Newton's method failed to converge"))
  717. (list 'vec next expr-val))))
  718. (defun math-find-root (expr var guess math-root-widen)
  719. (if (eq (car-safe expr) 'vec)
  720. (let ((n (1- (length expr)))
  721. (calc-symbolic-mode nil)
  722. (var-DUMMY nil)
  723. (jacob (list 'vec))
  724. p p2 m row)
  725. (unless (eq (car-safe var) 'vec)
  726. (math-reject-arg var 'vectorp))
  727. (unless (= (length var) (1+ n))
  728. (math-dimension-error))
  729. (setq expr (copy-sequence expr))
  730. (while (>= n (length math-root-vars))
  731. (let ((symb (intern (concat "math-root-v"
  732. (int-to-string
  733. (length math-root-vars))))))
  734. (setq math-root-vars (vconcat math-root-vars
  735. (vector (list 'var symb symb))))))
  736. (setq m -1)
  737. (while (< (setq m (1+ m)) n)
  738. (set (nth 2 (aref math-root-vars m)) nil))
  739. (setq m -1 p var)
  740. (while (setq m (1+ m) p (cdr p))
  741. (or (eq (car-safe (car p)) 'var)
  742. (math-reject-arg var "*Expected a variable"))
  743. (setq p2 expr)
  744. (while (setq p2 (cdr p2))
  745. (setcar p2 (math-expr-subst (car p2) (car p)
  746. (aref math-root-vars m)))))
  747. (unless (eq (car-safe guess) 'vec)
  748. (math-reject-arg guess 'vectorp))
  749. (unless (= (length guess) (1+ n))
  750. (math-dimension-error))
  751. (setq guess (copy-sequence guess)
  752. p guess)
  753. (while (setq p (cdr p))
  754. (or (Math-numberp (car guess))
  755. (math-reject-arg guess 'numberp))
  756. (setcar p (math-float (car p))))
  757. (setq p expr)
  758. (while (setq p (cdr p))
  759. (if (assq (car-safe (car p)) calc-tweak-eqn-table)
  760. (setcar p (math-sub (nth 1 (car p)) (nth 2 (car p)))))
  761. (setcar p (math-evaluate-expr (car p)))
  762. (setq row (list 'vec)
  763. m -1)
  764. (while (< (setq m (1+ m)) n)
  765. (nconc row (list (math-evaluate-expr
  766. (or (calcFunc-deriv (car p)
  767. (aref math-root-vars m)
  768. nil t)
  769. (math-reject-arg
  770. expr
  771. "*Formulas must be differentiable"))))))
  772. (nconc jacob (list row)))
  773. (setq m (math-abs-approx guess))
  774. (math-newton-multi expr jacob n guess guess
  775. (if (math-zerop m) '(float 1 3) (math-mul m 10))))
  776. (unless (eq (car-safe var) 'var)
  777. (math-reject-arg var "*Expected a variable"))
  778. (unless (math-expr-contains expr var)
  779. (math-reject-arg expr "*Formula does not contain specified variable"))
  780. (if (assq (car expr) calc-tweak-eqn-table)
  781. (setq expr (math-sub (nth 1 expr) (nth 2 expr))))
  782. (math-with-extra-prec 2
  783. (setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY)))
  784. (let* ((calc-symbolic-mode nil)
  785. (var-DUMMY nil)
  786. (expr (math-evaluate-expr expr))
  787. (deriv (calcFunc-deriv expr '(var DUMMY var-DUMMY) nil t))
  788. low high vlow vhigh)
  789. (and deriv (setq deriv (math-evaluate-expr deriv)))
  790. (setq guess (math-float guess))
  791. (if (and (math-numberp guess)
  792. deriv)
  793. (math-newton-root expr deriv guess guess
  794. (if (math-zerop guess) '(float 1 6)
  795. (math-mul (math-abs-approx guess) 100)))
  796. (if (Math-realp guess)
  797. (setq low guess
  798. high guess
  799. var-DUMMY guess
  800. vlow (math-evaluate-expr expr)
  801. vhigh vlow
  802. math-root-widen 'point)
  803. (if (eq (car guess) 'intv)
  804. (progn
  805. (or (math-constp guess) (math-reject-arg guess 'constp))
  806. (setq low (nth 2 guess)
  807. high (nth 3 guess))
  808. (if (memq (nth 1 guess) '(0 1))
  809. (setq low (calcFunc-incr low 1 high)))
  810. (if (memq (nth 1 guess) '(0 2))
  811. (setq high (calcFunc-incr high -1 low)))
  812. (setq var-DUMMY low
  813. vlow (math-evaluate-expr expr)
  814. var-DUMMY high
  815. vhigh (math-evaluate-expr expr)))
  816. (if (math-complexp guess)
  817. (math-reject-arg "*Complex root finder must have derivative")
  818. (math-reject-arg guess 'realp))))
  819. (if (Math-zerop vlow)
  820. (list 'vec low vlow)
  821. (if (Math-zerop vhigh)
  822. (list 'vec high vhigh)
  823. (if (and deriv (Math-numberp vlow) (Math-numberp vhigh))
  824. (math-newton-search-root expr deriv nil nil nil nil
  825. low vlow high vhigh)
  826. (if (or (and (Math-posp vlow) (Math-posp vhigh))
  827. (and (Math-negp vlow) (Math-negp vhigh))
  828. (not (Math-numberp vlow))
  829. (not (Math-numberp vhigh)))
  830. (math-search-root expr deriv low vlow high vhigh)
  831. (math-bisect-root expr low vlow high vhigh))))))))))
  832. (defun calcFunc-root (expr var guess)
  833. (math-find-root expr var guess nil))
  834. (defun calcFunc-wroot (expr var guess)
  835. (math-find-root expr var guess t))
  836. ;;; The following algorithms come from Numerical Recipes, chapter 10.
  837. (defvar math-min-vars [(var DUMMY var-DUMMY)])
  838. (defun math-min-eval (expr a)
  839. (if (Math-vectorp a)
  840. (let ((m -1))
  841. (while (setq m (1+ m) a (cdr a))
  842. (set (nth 2 (aref math-min-vars m)) (car a))))
  843. (setq var-DUMMY a))
  844. (setq a (math-evaluate-expr expr))
  845. (if (Math-ratp a)
  846. (math-float a)
  847. (if (eq (car a) 'float)
  848. a
  849. (math-reject-arg a 'realp))))
  850. (defvar math-min-or-max "minimum")
  851. ;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c).
  852. ;;; "mnbrak"
  853. (defun math-widen-min (expr a b)
  854. (let ((done nil)
  855. (iters 30)
  856. incr c va vb vc u vu r q ulim bc ba qr)
  857. (or b (setq b (math-mul a '(float 101 -2))))
  858. (setq va (math-min-eval expr a)
  859. vb (math-min-eval expr b))
  860. (if (math-lessp-float va vb)
  861. (setq u a a b b u
  862. vu va va vb vb vu))
  863. (setq c (math-add-float b (math-mul-float '(float 161803 -5)
  864. (math-sub-float b a)))
  865. vc (math-min-eval expr c))
  866. (while (and (not done) (math-lessp-float vc vb))
  867. (math-working "widen" (list 'intv 0 a c))
  868. (if (= (setq iters (1- iters)) 0)
  869. (math-reject-arg nil (format "*Unable to find a %s near the interval"
  870. math-min-or-max)))
  871. (setq bc (math-sub-float b c)
  872. ba (math-sub-float b a)
  873. r (math-mul-float ba (math-sub-float vb vc))
  874. q (math-mul-float bc (math-sub-float vb va))
  875. qr (math-sub-float q r))
  876. (if (math-lessp-float (math-abs qr) '(float 1 -20))
  877. (setq qr (if (math-negp qr) '(float -1 -20) '(float 1 -20))))
  878. (setq u (math-sub-float
  879. b
  880. (math-div-float (math-sub-float (math-mul-float bc q)
  881. (math-mul-float ba r))
  882. (math-mul-float '(float 2 0) qr)))
  883. ulim (math-add-float b (math-mul-float '(float -1 2) bc))
  884. incr (math-negp bc))
  885. (if (if incr (math-lessp-float b u) (math-lessp-float u b))
  886. (if (if incr (math-lessp-float u c) (math-lessp-float c u))
  887. (if (math-lessp-float (setq vu (math-min-eval expr u)) vc)
  888. (setq a b va vb
  889. b u vb vu
  890. done t)
  891. (if (math-lessp-float vb vu)
  892. (setq c u vc vu
  893. done t)
  894. (setq u (math-add-float c (math-mul-float '(float -161803 -5)
  895. bc))
  896. vu (math-min-eval expr u))))
  897. (if (if incr (math-lessp-float u ulim) (math-lessp-float ulim u))
  898. (if (math-lessp-float (setq vu (math-min-eval expr u)) vc)
  899. (setq b c vb vc
  900. c u vc vu
  901. u (math-add-float c (math-mul-float
  902. '(float -161803 -5)
  903. (math-sub-float b c)))
  904. vu (math-min-eval expr u)))
  905. (setq u ulim
  906. vu (math-min-eval expr u))))
  907. (setq u (math-add-float c (math-mul-float '(float -161803 -5)
  908. bc))
  909. vu (math-min-eval expr u)))
  910. (setq a b va vb
  911. b c vb vc
  912. c u vc vu))
  913. (if (math-lessp-float a c)
  914. (list a va b vb c vc)
  915. (list c vc b vb a va))))
  916. (defun math-narrow-min (expr a c intv)
  917. (let ((xvals (list a c))
  918. (yvals (list (math-min-eval expr a)
  919. (math-min-eval expr c)))
  920. (levels 0)
  921. (step (math-sub-float c a))
  922. (found nil)
  923. xp yp b)
  924. (while (and (<= (setq levels (1+ levels)) 5)
  925. (not found))
  926. (setq xp xvals
  927. yp yvals
  928. step (math-mul-float step '(float 497 -3)))
  929. (while (and (cdr xp) (not found))
  930. (setq b (math-add-float (car xp) step))
  931. (math-working "search" b)
  932. (setcdr xp (cons b (cdr xp)))
  933. (setcdr yp (cons (math-min-eval expr b) (cdr yp)))
  934. (if (and (math-lessp-float (nth 1 yp) (car yp))
  935. (math-lessp-float (nth 1 yp) (nth 2 yp)))
  936. (setq found t)
  937. (setq xp (cdr xp)
  938. yp (cdr yp))
  939. (if (and (cdr (cdr yp))
  940. (math-lessp-float (nth 1 yp) (car yp))
  941. (math-lessp-float (nth 1 yp) (nth 2 yp)))
  942. (setq found t)
  943. (setq xp (cdr xp)
  944. yp (cdr yp))))))
  945. (if found
  946. (list (car xp) (car yp)
  947. (nth 1 xp) (nth 1 yp)
  948. (nth 2 xp) (nth 2 yp))
  949. (or (if (math-lessp-float (car yvals) (nth 1 yvals))
  950. (and (memq (nth 1 intv) '(2 3))
  951. (let ((min (car yvals)))
  952. (while (and (setq yvals (cdr yvals))
  953. (math-lessp-float min (car yvals))))
  954. (and (not yvals)
  955. (list (nth 2 intv) min))))
  956. (and (memq (nth 1 intv) '(1 3))
  957. (setq yvals (nreverse yvals))
  958. (let ((min (car yvals)))
  959. (while (and (setq yvals (cdr yvals))
  960. (math-lessp-float min (car yvals))))
  961. (and (not yvals)
  962. (list (nth 3 intv) min)))))
  963. (math-reject-arg nil (format "*Unable to find a %s in the interval"
  964. math-min-or-max))))))
  965. ;;; "brent"
  966. (defun math-brent-min (expr prec a va x vx b vb)
  967. (let ((iters (+ 20 (* 5 prec)))
  968. (w x)
  969. (vw vx)
  970. (v x)
  971. (vv vx)
  972. (tol (list 'float 1 (- -1 prec)))
  973. (zeps (list 'float 1 (- -5 prec)))
  974. (e '(float 0 0))
  975. d u vu xm tol1 tol2 etemp p q r xv xw)
  976. (while (progn
  977. (setq xm (math-mul-float '(float 5 -1)
  978. (math-add-float a b))
  979. tol1 (math-add-float
  980. zeps
  981. (math-mul-float tol (math-abs x)))
  982. tol2 (math-mul-float tol1 '(float 2 0)))
  983. (math-lessp-float (math-sub-float tol2
  984. (math-mul-float
  985. '(float 5 -1)
  986. (math-sub-float b a)))
  987. (math-abs (math-sub-float x xm))))
  988. (if (= (setq iters (1- iters)) 0)
  989. (math-reject-arg nil (format "*Unable to converge on a %s"
  990. math-min-or-max)))
  991. (math-working "brent" x)
  992. (if (math-lessp-float (math-abs e) tol1)
  993. (setq e (if (math-lessp-float x xm)
  994. (math-sub-float b x)
  995. (math-sub-float a x))
  996. d (math-mul-float '(float 381966 -6) e))
  997. (setq xw (math-sub-float x w)
  998. r (math-mul-float xw (math-sub-float vx vv))
  999. xv (math-sub-float x v)
  1000. q (math-mul-float xv (math-sub-float vx vw))
  1001. p (math-sub-float (math-mul-float xv q)
  1002. (math-mul-float xw r))
  1003. q (math-mul-float '(float 2 0) (math-sub-float q r)))
  1004. (if (math-posp q)
  1005. (setq p (math-neg-float p))
  1006. (setq q (math-neg-float q)))
  1007. (setq etemp e
  1008. e d)
  1009. (if (and (math-lessp-float (math-abs p)
  1010. (math-abs (math-mul-float
  1011. '(float 5 -1)
  1012. (math-mul-float q etemp))))
  1013. (math-lessp-float (math-mul-float
  1014. q (math-sub-float a x)) p)
  1015. (math-lessp-float p (math-mul-float
  1016. q (math-sub-float b x))))
  1017. (progn
  1018. (setq d (math-div-float p q)
  1019. u (math-add-float x d))
  1020. (if (or (math-lessp-float (math-sub-float u a) tol2)
  1021. (math-lessp-float (math-sub-float b u) tol2))
  1022. (setq d (if (math-lessp-float xm x)
  1023. (math-neg-float tol1)
  1024. tol1))))
  1025. (setq e (if (math-lessp-float x xm)
  1026. (math-sub-float b x)
  1027. (math-sub-float a x))
  1028. d (math-mul-float '(float 381966 -6) e))))
  1029. (setq u (math-add-float x
  1030. (if (math-lessp-float (math-abs d) tol1)
  1031. (if (math-negp d)
  1032. (math-neg-float tol1)
  1033. tol1)
  1034. d))
  1035. vu (math-min-eval expr u))
  1036. (if (math-lessp-float vx vu)
  1037. (progn
  1038. (if (math-lessp-float u x)
  1039. (setq a u)
  1040. (setq b u))
  1041. (if (or (equal w x)
  1042. (not (math-lessp-float vw vu)))
  1043. (setq v w vv vw
  1044. w u vw vu)
  1045. (if (or (equal v x)
  1046. (equal v w)
  1047. (not (math-lessp-float vv vu)))
  1048. (setq v u vv vu))))
  1049. (if (math-lessp-float u x)
  1050. (setq b x)
  1051. (setq a x))
  1052. (setq v w vv vw
  1053. w x vw vx
  1054. x u vx vu)))
  1055. (list 'vec x vx)))
  1056. ;;; "powell"
  1057. (defun math-powell-min (expr n guesses prec)
  1058. (let* ((f1dim (math-line-min-func expr n))
  1059. (xi (calcFunc-idn 1 n))
  1060. (p (cons 'vec (mapcar 'car guesses)))
  1061. (pt p)
  1062. (ftol (list 'float 1 (- prec)))
  1063. (fret (math-min-eval expr p))
  1064. fp ptt fptt xit i ibig del diff res)
  1065. (while (progn
  1066. (setq fp fret
  1067. ibig 0
  1068. del '(float 0 0)
  1069. i 0)
  1070. (while (<= (setq i (1+ i)) n)
  1071. (setq fptt fret
  1072. res (math-line-min f1dim p
  1073. (math-mat-col xi i)
  1074. n prec)
  1075. p (let ((calc-internal-prec prec))
  1076. (math-normalize (car res)))
  1077. fret (nth 2 res)
  1078. diff (math-abs (math-sub-float fptt fret)))
  1079. (if (math-lessp-float del diff)
  1080. (setq del diff
  1081. ibig i)))
  1082. (math-lessp-float
  1083. (math-mul-float ftol
  1084. (math-add-float (math-abs fp)
  1085. (math-abs fret)))
  1086. (math-mul-float '(float 2 0)
  1087. (math-abs (math-sub-float fp
  1088. fret)))))
  1089. (setq ptt (math-sub (math-mul '(float 2 0) p) pt)
  1090. xit (math-sub p pt)
  1091. pt p
  1092. fptt (math-min-eval expr ptt))
  1093. (if (and (math-lessp-float fptt fp)
  1094. (math-lessp-float
  1095. (math-mul-float
  1096. (math-mul-float '(float 2 0)
  1097. (math-add-float
  1098. (math-sub-float fp
  1099. (math-mul-float '(float 2 0)
  1100. fret))
  1101. fptt))
  1102. (math-sqr-float (math-sub-float
  1103. (math-sub-float fp fret) del)))
  1104. (math-mul-float del
  1105. (math-sqr-float (math-sub-float fp fptt)))))
  1106. (progn
  1107. (setq res (math-line-min f1dim p xit n prec)
  1108. p (car res)
  1109. fret (nth 2 res)
  1110. i 0)
  1111. (while (<= (setq i (1+ i)) n)
  1112. (setcar (nthcdr ibig (nth i xi))
  1113. (nth i (nth 1 res)))))))
  1114. (list 'vec p fret)))
  1115. (defun math-line-min-func (expr n)
  1116. (let ((m -1))
  1117. (while (< (setq m (1+ m)) n)
  1118. (set (nth 2 (aref math-min-vars m))
  1119. (list '+
  1120. (list '*
  1121. '(var DUMMY var-DUMMY)
  1122. (list 'calcFunc-mrow '(var line-xi line-xi) (1+ m)))
  1123. (list 'calcFunc-mrow '(var line-p line-p) (1+ m)))))
  1124. (math-evaluate-expr expr)))
  1125. (defun math-line-min (f1dim line-p line-xi n prec)
  1126. (let* ((var-DUMMY nil)
  1127. (expr (math-evaluate-expr f1dim))
  1128. (params (math-widen-min expr '(float 0 0) '(float 1 0)))
  1129. (res (apply 'math-brent-min expr prec params))
  1130. (xi (math-mul (nth 1 res) line-xi)))
  1131. (list (math-add line-p xi) xi (nth 2 res))))
  1132. (defun math-find-minimum (expr var guess min-widen)
  1133. (let* ((calc-symbolic-mode nil)
  1134. (n 0)
  1135. (var-DUMMY nil)
  1136. (isvec (math-vectorp var))
  1137. g guesses)
  1138. (or (math-vectorp var)
  1139. (setq var (list 'vec var)))
  1140. (or (math-vectorp guess)
  1141. (setq guess (list 'vec guess)))
  1142. (or (= (length var) (length guess))
  1143. (math-dimension-error))
  1144. (while (setq var (cdr var) guess (cdr guess))
  1145. (or (eq (car-safe (car var)) 'var)
  1146. (math-reject-arg (car var) "*Expected a variable"))
  1147. (or (math-expr-contains expr (car var))
  1148. (math-reject-arg (car var)
  1149. "*Formula does not contain specified variable"))
  1150. (while (>= (1+ n) (length math-min-vars))
  1151. (let ((symb (intern (concat "math-min-v"
  1152. (int-to-string
  1153. (length math-min-vars))))))
  1154. (setq math-min-vars (vconcat math-min-vars
  1155. (vector (list 'var symb symb))))))
  1156. (set (nth 2 (aref math-min-vars n)) nil)
  1157. (set (nth 2 (aref math-min-vars (1+ n))) nil)
  1158. (if (math-complexp (car guess))
  1159. (setq expr (math-expr-subst expr
  1160. (car var)
  1161. (list '+ (aref math-min-vars n)
  1162. (list '*
  1163. (aref math-min-vars (1+ n))
  1164. '(cplx 0 1))))
  1165. guesses (let ((g (math-float (math-complex (car guess)))))
  1166. (cons (list (nth 2 g) nil nil)
  1167. (cons (list (nth 1 g) nil nil t)
  1168. guesses)))
  1169. n (+ n 2))
  1170. (setq expr (math-expr-subst expr
  1171. (car var)
  1172. (aref math-min-vars n))
  1173. guesses (cons (if (math-realp (car guess))
  1174. (list (math-float (car guess)) nil nil)
  1175. (if (and (eq (car-safe (car guess)) 'intv)
  1176. (math-constp (car guess)))
  1177. (list (math-mul
  1178. (math-add (nth 2 (car guess))
  1179. (nth 3 (car guess)))
  1180. '(float 5 -1))
  1181. (math-float (nth 2 (car guess)))
  1182. (math-float (nth 3 (car guess)))
  1183. (car guess))
  1184. (math-reject-arg (car guess) 'realp)))
  1185. guesses)
  1186. n (1+ n))))
  1187. (setq guesses (nreverse guesses)
  1188. expr (math-evaluate-expr expr))
  1189. (if (= n 1)
  1190. (let* ((params (if (nth 1 (car guesses))
  1191. (if min-widen
  1192. (math-widen-min expr
  1193. (nth 1 (car guesses))
  1194. (nth 2 (car guesses)))
  1195. (math-narrow-min expr
  1196. (nth 1 (car guesses))
  1197. (nth 2 (car guesses))
  1198. (nth 3 (car guesses))))
  1199. (math-widen-min expr
  1200. (car (car guesses))
  1201. nil)))
  1202. (prec calc-internal-prec)
  1203. (res (if (cdr (cdr params))
  1204. (math-with-extra-prec (+ calc-internal-prec 2)
  1205. (apply 'math-brent-min expr prec params))
  1206. (cons 'vec params))))
  1207. (if isvec
  1208. (list 'vec (list 'vec (nth 1 res)) (nth 2 res))
  1209. res))
  1210. (let* ((prec calc-internal-prec)
  1211. (res (math-with-extra-prec (+ calc-internal-prec 2)
  1212. (math-powell-min expr n guesses prec)))
  1213. (p (nth 1 res))
  1214. (vec (list 'vec)))
  1215. (while (setq p (cdr p))
  1216. (if (nth 3 (car guesses))
  1217. (progn
  1218. (nconc vec (list (math-normalize
  1219. (list 'cplx (car p) (nth 1 p)))))
  1220. (setq p (cdr p)
  1221. guesses (cdr guesses)))
  1222. (nconc vec (list (car p))))
  1223. (setq guesses (cdr guesses)))
  1224. (if isvec
  1225. (list 'vec vec (nth 2 res))
  1226. (list 'vec (nth 1 vec) (nth 2 res)))))))
  1227. (defun calcFunc-minimize (expr var guess)
  1228. (let ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
  1229. (math-min-or-max "minimum"))
  1230. (math-find-minimum (math-normalize expr)
  1231. (math-normalize var)
  1232. (math-normalize guess) nil)))
  1233. (defun calcFunc-wminimize (expr var guess)
  1234. (let ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
  1235. (math-min-or-max "minimum"))
  1236. (math-find-minimum (math-normalize expr)
  1237. (math-normalize var)
  1238. (math-normalize guess) t)))
  1239. (defun calcFunc-maximize (expr var guess)
  1240. (let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
  1241. (math-min-or-max "maximum")
  1242. (res (math-find-minimum (math-normalize (math-neg expr))
  1243. (math-normalize var)
  1244. (math-normalize guess) nil)))
  1245. (list 'vec (nth 1 res) (math-neg (nth 2 res)))))
  1246. (defun calcFunc-wmaximize (expr var guess)
  1247. (let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
  1248. (math-min-or-max "maximum")
  1249. (res (math-find-minimum (math-normalize (math-neg expr))
  1250. (math-normalize var)
  1251. (math-normalize guess) t)))
  1252. (list 'vec (nth 1 res) (math-neg (nth 2 res)))))
  1253. ;;; The following algorithms come from Numerical Recipes, chapter 3.
  1254. (defun calcFunc-polint (data x)
  1255. (or (math-matrixp data) (math-reject-arg data 'matrixp))
  1256. (or (= (length data) 3)
  1257. (math-reject-arg data "*Wrong number of data rows"))
  1258. (or (> (length (nth 1 data)) 2)
  1259. (math-reject-arg data "*Too few data points"))
  1260. (if (and (math-vectorp x) (or (math-constp x) math-expand-formulas))
  1261. (cons 'vec (mapcar (function (lambda (x) (calcFunc-polint data x)))
  1262. (cdr x)))
  1263. (or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp))
  1264. (math-with-extra-prec 2
  1265. (cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x
  1266. nil)))))
  1267. (put 'calcFunc-polint 'math-expandable t)
  1268. (defun calcFunc-ratint (data x)
  1269. (or (math-matrixp data) (math-reject-arg data 'matrixp))
  1270. (or (= (length data) 3)
  1271. (math-reject-arg data "*Wrong number of data rows"))
  1272. (or (> (length (nth 1 data)) 2)
  1273. (math-reject-arg data "*Too few data points"))
  1274. (if (and (math-vectorp x) (or (math-constp x) math-expand-formulas))
  1275. (cons 'vec (mapcar (function (lambda (x) (calcFunc-ratint data x)))
  1276. (cdr x)))
  1277. (or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp))
  1278. (math-with-extra-prec 2
  1279. (cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x
  1280. (cdr (cdr (cdr (nth 1 data)))))))))
  1281. (put 'calcFunc-ratint 'math-expandable t)
  1282. (defun math-poly-interp (xa ya x ratp)
  1283. (let ((n (length xa))
  1284. (dif nil)
  1285. (ns nil)
  1286. (xax nil)
  1287. (c (copy-sequence ya))
  1288. (d (copy-sequence ya))
  1289. (i 0)
  1290. (m 0)
  1291. y dy (xp xa) xpm cp dp temp)
  1292. (while (<= (setq i (1+ i)) n)
  1293. (setq xax (cons (math-sub (car xp) x) xax)
  1294. xp (cdr xp)
  1295. temp (math-abs (car xax)))
  1296. (if (or (null dif) (math-lessp temp dif))
  1297. (setq dif temp
  1298. ns i)))
  1299. (setq xax (nreverse xax)
  1300. ns (1- ns)
  1301. y (nth ns ya))
  1302. (if (math-zerop dif)
  1303. (list y 0)
  1304. (while (< (setq m (1+ m)) n)
  1305. (setq i 0
  1306. xp xax
  1307. xpm (nthcdr m xax)
  1308. cp c
  1309. dp d)
  1310. (while (<= (setq i (1+ i)) (- n m))
  1311. (if ratp
  1312. (let ((t2 (math-div (math-mul (car xp) (car dp)) (car xpm))))
  1313. (setq temp (math-div (math-sub (nth 1 cp) (car dp))
  1314. (math-sub t2 (nth 1 cp))))
  1315. (setcar dp (math-mul (nth 1 cp) temp))
  1316. (setcar cp (math-mul t2 temp)))
  1317. (if (math-equal (car xp) (car xpm))
  1318. (math-reject-arg (cons 'vec xa) "*Duplicate X values"))
  1319. (setq temp (math-div (math-sub (nth 1 cp) (car dp))
  1320. (math-sub (car xp) (car xpm))))
  1321. (setcar dp (math-mul (car xpm) temp))
  1322. (setcar cp (math-mul (car xp) temp)))
  1323. (setq cp (cdr cp)
  1324. dp (cdr dp)
  1325. xp (cdr xp)
  1326. xpm (cdr xpm)))
  1327. (if (< (+ ns ns) (- n m))
  1328. (setq dy (nth ns c))
  1329. (setq ns (1- ns)
  1330. dy (nth ns d)))
  1331. (setq y (math-add y dy)))
  1332. (list y dy))))
  1333. ;;; The following algorithms come from Numerical Recipes, chapter 4.
  1334. (defun calcFunc-ninteg (expr var lo hi)
  1335. (setq lo (math-evaluate-expr lo)
  1336. hi (math-evaluate-expr hi))
  1337. (or (math-numberp lo) (math-infinitep lo) (math-reject-arg lo 'numberp))
  1338. (or (math-numberp hi) (math-infinitep hi) (math-reject-arg hi 'numberp))
  1339. (if (math-lessp hi lo)
  1340. (math-neg (calcFunc-ninteg expr var hi lo))
  1341. (setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY)))
  1342. (let ((var-DUMMY nil)
  1343. (calc-symbolic-mode nil)
  1344. (calc-prefer-frac nil)
  1345. (sum 0))
  1346. (setq expr (math-evaluate-expr expr))
  1347. (if (equal lo '(neg (var inf var-inf)))
  1348. (let ((thi (if (math-lessp hi '(float -2 0))
  1349. hi '(float -2 0))))
  1350. (setq sum (math-ninteg-romberg
  1351. 'math-ninteg-midpoint expr
  1352. (math-float lo) (math-float thi) 'inf)
  1353. lo thi)))
  1354. (if (equal hi '(var inf var-inf))
  1355. (let ((tlo (if (math-lessp '(float 2 0) lo)
  1356. lo '(float 2 0))))
  1357. (setq sum (math-add sum
  1358. (math-ninteg-romberg
  1359. 'math-ninteg-midpoint expr
  1360. (math-float tlo) (math-float hi) 'inf))
  1361. hi tlo)))
  1362. (or (math-equal lo hi)
  1363. (setq sum (math-add sum
  1364. (math-ninteg-romberg
  1365. 'math-ninteg-midpoint expr
  1366. (math-float lo) (math-float hi) nil))))
  1367. sum)))
  1368. ;;; Open Romberg method; "qromo" in section 4.4.
  1369. ;; The variable math-ninteg-temp is local to math-ninteg-romberg,
  1370. ;; but is used by math-ninteg-midpoint, which is used by
  1371. ;; math-ninteg-romberg.
  1372. (defvar math-ninteg-temp)
  1373. (defun math-ninteg-romberg (func expr lo hi mode)
  1374. (let ((curh '(float 1 0))
  1375. (h nil)
  1376. (s nil)
  1377. (j 0)
  1378. (ss nil)
  1379. (prec calc-internal-prec)
  1380. (math-ninteg-temp nil))
  1381. (math-with-extra-prec 2
  1382. ;; Limit on "j" loop must be 14 or less to keep "it" from overflowing.
  1383. (or (while (and (null ss) (<= (setq j (1+ j)) 8))
  1384. (setq s (nconc s (list (funcall func expr lo hi mode)))
  1385. h (nconc h (list curh)))
  1386. (if (>= j 3)
  1387. (let ((res (math-poly-interp h s '(float 0 0) nil)))
  1388. (if (math-lessp (math-abs (nth 1 res))
  1389. (calcFunc-scf (math-abs (car res))
  1390. (- prec)))
  1391. (setq ss (car res)))))
  1392. (if (>= j 5)
  1393. (setq s (cdr s)
  1394. h (cdr h)))
  1395. (setq curh (math-div-float curh '(float 9 0))))
  1396. ss
  1397. (math-reject-arg nil (format "*Integral failed to converge"))))))
  1398. (defun math-ninteg-evaluate (expr x mode)
  1399. (if (eq mode 'inf)
  1400. (setq x (math-div '(float 1 0) x)))
  1401. (let* ((var-DUMMY x)
  1402. (res (math-evaluate-expr expr)))
  1403. (or (Math-numberp res)
  1404. (math-reject-arg res "*Integrand does not evaluate to a number"))
  1405. (if (eq mode 'inf)
  1406. (setq res (math-mul res (math-sqr x))))
  1407. res))
  1408. (defun math-ninteg-midpoint (expr lo hi mode) ; uses "math-ninteg-temp"
  1409. (if (eq mode 'inf)
  1410. (let ((math-infinite-mode t) temp)
  1411. (setq temp (math-div 1 lo)
  1412. lo (math-div 1 hi)
  1413. hi temp)))
  1414. (if math-ninteg-temp
  1415. (let* ((it3 (* 3 (car math-ninteg-temp)))
  1416. (math-working-step-2 (* 2 (car math-ninteg-temp)))
  1417. (math-working-step 0)
  1418. (range (math-sub hi lo))
  1419. (del (math-div range (math-float it3)))
  1420. (del2 (math-add del del))
  1421. (del3 (math-add del del2))
  1422. (x (math-add lo (math-mul '(float 5 -1) del)))
  1423. (sum '(float 0 0))
  1424. (j 0) temp)
  1425. (while (<= (setq j (1+ j)) (car math-ninteg-temp))
  1426. (setq math-working-step (1+ math-working-step)
  1427. temp (math-ninteg-evaluate expr x mode)
  1428. math-working-step (1+ math-working-step)
  1429. sum (math-add sum (math-add temp (math-ninteg-evaluate
  1430. expr (math-add x del2)
  1431. mode)))
  1432. x (math-add x del3)))
  1433. (setq math-ninteg-temp (list it3
  1434. (math-add (math-div (nth 1 math-ninteg-temp)
  1435. '(float 3 0))
  1436. (math-mul sum del)))))
  1437. (setq math-ninteg-temp (list 1 (math-mul
  1438. (math-sub hi lo)
  1439. (math-ninteg-evaluate
  1440. expr
  1441. (math-mul (math-add lo hi) '(float 5 -1))
  1442. mode)))))
  1443. (nth 1 math-ninteg-temp))
  1444. ;;; The following algorithms come from Numerical Recipes, chapter 14.
  1445. (defvar math-dummy-vars [(var DUMMY var-DUMMY)])
  1446. (defvar math-dummy-counter 0)
  1447. (defun math-dummy-variable ()
  1448. (if (= math-dummy-counter (length math-dummy-vars))
  1449. (let ((symb (intern (format "math-dummy-%d" math-dummy-counter))))
  1450. (setq math-dummy-vars (vconcat math-dummy-vars
  1451. (vector (list 'var symb symb))))))
  1452. (set (nth 2 (aref math-dummy-vars math-dummy-counter)) nil)
  1453. (prog1
  1454. (aref math-dummy-vars math-dummy-counter)
  1455. (setq math-dummy-counter (1+ math-dummy-counter))))
  1456. (defvar math-in-fit 0)
  1457. (defvar calc-fit-to-trail nil)
  1458. (defun calcFunc-fit (expr vars &optional coefs data)
  1459. (let ((math-in-fit 10))
  1460. (math-with-extra-prec 2
  1461. (math-general-fit expr vars coefs data nil))))
  1462. (defun calcFunc-efit (expr vars &optional coefs data)
  1463. (let ((math-in-fit 10))
  1464. (math-with-extra-prec 2
  1465. (math-general-fit expr vars coefs data 'sdev))))
  1466. (defun calcFunc-xfit (expr vars &optional coefs data)
  1467. (let ((math-in-fit 10))
  1468. (math-with-extra-prec 2
  1469. (math-general-fit expr vars coefs data 'full))))
  1470. ;; The variables math-fit-first-var, math-fit-first-coef and
  1471. ;; math-fit-new-coefs are local to math-general-fit, but are used by
  1472. ;; calcFunc-fitvar, calcFunc-fitparam and calcFunc-fitdummy
  1473. ;; (respectively), which are used by math-general-fit.
  1474. (defvar math-fit-first-var)
  1475. (defvar math-fit-first-coef)
  1476. (defvar math-fit-new-coefs)
  1477. (defun math-general-fit (expr vars coefs data mode)
  1478. (let ((calc-simplify-mode nil)
  1479. (math-dummy-counter math-dummy-counter)
  1480. (math-in-fit 1)
  1481. (extended (eq mode 'full))
  1482. (math-fit-first-coef math-dummy-counter)
  1483. math-fit-first-var
  1484. (plain-expr expr)
  1485. orig-expr
  1486. have-sdevs need-chisq chisq
  1487. (x-funcs nil)
  1488. (y-filter nil)
  1489. y-dummy
  1490. (coef-filters nil)
  1491. math-fit-new-coefs
  1492. (xy-values nil)
  1493. (weights nil)
  1494. (var-YVAL nil) (var-YVALX nil)
  1495. covar beta
  1496. n nn m mm v dummy p)
  1497. ;; Validate and parse arguments.
  1498. (or data
  1499. (if coefs
  1500. (setq data coefs
  1501. coefs nil)
  1502. (if (math-vectorp expr)
  1503. (if (memq (length expr) '(3 4))
  1504. (setq data vars
  1505. vars (nth 2 expr)
  1506. coefs (nth 3 expr)
  1507. expr (nth 1 expr))
  1508. (math-dimension-error))
  1509. (setq data vars
  1510. vars nil
  1511. coefs nil))))
  1512. (or (math-matrixp data) (math-reject-arg data 'matrixp))
  1513. (setq v (1- (length data))
  1514. n (1- (length (nth 1 data))))
  1515. (or (math-vectorp vars) (null vars)
  1516. (setq vars (list 'vec vars)))
  1517. (or (math-vectorp coefs) (null coefs)
  1518. (setq coefs (list 'vec coefs)))
  1519. (or coefs
  1520. (setq coefs (cons 'vec (math-all-vars-but expr vars))))
  1521. (or vars
  1522. (if (<= (1- (length coefs)) v)
  1523. (math-reject-arg coefs "*Not enough variables in model")
  1524. (setq coefs (copy-sequence coefs))
  1525. (let ((p (nthcdr (- (length coefs) v
  1526. (if (eq (car-safe expr) 'calcFunc-eq) 1 0))
  1527. coefs)))
  1528. (setq vars (cons 'vec (cdr p)))
  1529. (setcdr p nil))))
  1530. (or (= (1- (length vars)) v)
  1531. (= (length vars) v)
  1532. (math-reject-arg vars "*Number of variables does not match data"))
  1533. (setq m (1- (length coefs)))
  1534. (if (< m 1)
  1535. (math-reject-arg coefs "*Need at least one parameter"))
  1536. ;; Rewrite expr in terms of fitparam and fitvar, make into an equation.
  1537. (setq p coefs)
  1538. (while (setq p (cdr p))
  1539. (or (eq (car-safe (car p)) 'var)
  1540. (math-reject-arg (car p) "*Expected a variable"))
  1541. (setq dummy (math-dummy-variable)
  1542. expr (math-expr-subst expr (car p)
  1543. (list 'calcFunc-fitparam
  1544. (- math-dummy-counter math-fit-first-coef)))))
  1545. (setq math-fit-first-var math-dummy-counter
  1546. p vars)
  1547. (while (setq p (cdr p))
  1548. (or (eq (car-safe (car p)) 'var)
  1549. (math-reject-arg (car p) "*Expected a variable"))
  1550. (setq dummy (math-dummy-variable)
  1551. expr (math-expr-subst expr (car p)
  1552. (list 'calcFunc-fitvar
  1553. (- math-dummy-counter math-fit-first-var)))))
  1554. (if (< math-dummy-counter (+ math-fit-first-var v))
  1555. (setq dummy (math-dummy-variable))) ; dependent variable may be unnamed
  1556. (setq y-dummy dummy
  1557. orig-expr expr)
  1558. (or (eq (car-safe expr) 'calcFunc-eq)
  1559. (setq expr (list 'calcFunc-eq (list 'calcFunc-fitvar v) expr)))
  1560. (let ((calc-symbolic-mode nil))
  1561. ;; Apply rewrites to put expr into a linear-like form.
  1562. (setq expr (math-evaluate-expr expr)
  1563. expr (math-rewrite (list 'calcFunc-fitmodel expr)
  1564. '(var FitRules var-FitRules))
  1565. math-in-fit 2
  1566. expr (math-evaluate-expr expr))
  1567. (or (and (eq (car-safe expr) 'calcFunc-fitsystem)
  1568. (= (length expr) 4)
  1569. (math-vectorp (nth 2 expr))
  1570. (math-vectorp (nth 3 expr))
  1571. (> (length (nth 2 expr)) 1)
  1572. (= (length (nth 3 expr)) (1+ m)))
  1573. (math-reject-arg plain-expr "*Model expression is too complex"))
  1574. (setq y-filter (nth 1 expr)
  1575. x-funcs (vconcat (cdr (nth 2 expr)))
  1576. coef-filters (nth 3 expr)
  1577. mm (length x-funcs))
  1578. (if (equal y-filter y-dummy)
  1579. (setq y-filter nil))
  1580. ;; Build the (square) system of linear equations to be solved.
  1581. (setq beta (cons 'vec (make-list mm 0))
  1582. covar (cons 'vec (mapcar 'copy-sequence (make-list mm beta))))
  1583. (let* ((ptrs (vconcat (cdr data)))
  1584. (isigsq 1)
  1585. (xvals (make-vector mm 0))
  1586. (i 0)
  1587. j k xval yval sigmasqr wt covj covjk covk betaj lud)
  1588. (while (<= (setq i (1+ i)) n)
  1589. ;; Assign various independent variables for this data point.
  1590. (setq j 0
  1591. sigmasqr nil)
  1592. (while (< j v)
  1593. (aset ptrs j (cdr (aref ptrs j)))
  1594. (setq xval (car (aref ptrs j)))
  1595. (if (= j (1- v))
  1596. (if sigmasqr
  1597. (progn
  1598. (if (eq (car-safe xval) 'sdev)
  1599. (setq sigmasqr (math-add (math-sqr (nth 2 xval))
  1600. sigmasqr)
  1601. xval (nth 1 xval)))
  1602. (if y-filter
  1603. (setq xval (math-make-sdev xval
  1604. (math-sqrt sigmasqr))))))
  1605. (if (eq (car-safe xval) 'sdev)
  1606. (setq sigmasqr (math-add (math-sqr (nth 2 xval))
  1607. (or sigmasqr 0))
  1608. xval (nth 1 xval))))
  1609. (set (nth 2 (aref math-dummy-vars (+ math-fit-first-var j))) xval)
  1610. (setq j (1+ j)))
  1611. ;; Compute Y value for this data point.
  1612. (if y-filter
  1613. (setq yval (math-evaluate-expr y-filter))
  1614. (setq yval (symbol-value (nth 2 y-dummy))))
  1615. (if (eq (car-safe yval) 'sdev)
  1616. (setq sigmasqr (math-sqr (nth 2 yval))
  1617. yval (nth 1 yval)))
  1618. (if (= i 1)
  1619. (setq have-sdevs sigmasqr
  1620. need-chisq (or extended
  1621. (and (eq mode 'sdev) (not have-sdevs)))))
  1622. (if have-sdevs
  1623. (if sigmasqr
  1624. (progn
  1625. (setq isigsq (math-div 1 sigmasqr))
  1626. (if need-chisq
  1627. (setq weights (cons isigsq weights))))
  1628. (math-reject-arg yval "*Mixed error forms and plain numbers"))
  1629. (if sigmasqr
  1630. (math-reject-arg yval "*Mixed error forms and plain numbers")))
  1631. ;; Compute X values for this data point and update covar and beta.
  1632. (if (eq (car-safe xval) 'sdev)
  1633. (set (nth 2 y-dummy) (nth 1 xval)))
  1634. (setq j 0
  1635. covj covar
  1636. betaj beta)
  1637. (while (< j mm)
  1638. (setq wt (math-evaluate-expr (aref x-funcs j)))
  1639. (aset xvals j wt)
  1640. (setq wt (math-mul wt isigsq)
  1641. betaj (cdr betaj)
  1642. covjk (car (setq covj (cdr covj)))
  1643. k 0)
  1644. (while (<= k j)
  1645. (setq covjk (cdr covjk))
  1646. (setcar covjk (math-add (car covjk)
  1647. (math-mul wt (aref xvals k))))
  1648. (setq k (1+ k)))
  1649. (setcar betaj (math-add (car betaj) (math-mul wt yval)))
  1650. (setq j (1+ j)))
  1651. (if need-chisq
  1652. (setq xy-values (cons (append xvals (list yval)) xy-values))))
  1653. ;; Fill in symmetric half of covar matrix.
  1654. (setq j 0
  1655. covj covar)
  1656. (while (< j (1- mm))
  1657. (setq k j
  1658. j (1+ j)
  1659. covjk (nthcdr j (car (setq covj (cdr covj))))
  1660. covk (nthcdr j covar))
  1661. (while (< (setq k (1+ k)) mm)
  1662. (setq covjk (cdr covjk)
  1663. covk (cdr covk))
  1664. (setcar covjk (nth j (car covk))))))
  1665. ;; Solve the linear system.
  1666. (if mode
  1667. (progn
  1668. (setq covar (math-matrix-inv-raw covar))
  1669. (if covar
  1670. (setq beta (math-mul covar beta))
  1671. (if (math-zerop (math-abs beta))
  1672. (setq covar (calcFunc-diag 0 (1- (length beta))))
  1673. (math-reject-arg orig-expr "*Singular matrix")))
  1674. (or (math-vectorp covar)
  1675. (setq covar (list 'vec (list 'vec covar)))))
  1676. (setq beta (math-div beta covar)))
  1677. ;; Compute chi-square statistic if necessary.
  1678. (if need-chisq
  1679. (let (bp xp sum)
  1680. (setq chisq 0)
  1681. (while xy-values
  1682. (setq bp beta
  1683. xp (car xy-values)
  1684. sum 0)
  1685. (while (setq bp (cdr bp))
  1686. (setq sum (math-add sum (math-mul (car bp) (car xp)))
  1687. xp (cdr xp)))
  1688. (setq sum (math-sqr (math-sub (car xp) sum)))
  1689. (if weights (setq sum (math-mul sum (car weights))))
  1690. (setq chisq (math-add chisq sum)
  1691. weights (cdr weights)
  1692. xy-values (cdr xy-values)))))
  1693. ;; Convert coefficients back into original terms.
  1694. (setq math-fit-new-coefs (copy-sequence beta))
  1695. (let* ((bp math-fit-new-coefs)
  1696. (cp covar)
  1697. (sigdat 1)
  1698. (math-in-fit 3)
  1699. (j 0))
  1700. (and mode (not have-sdevs)
  1701. (setq sigdat (if (<= n mm)
  1702. 0
  1703. (math-div chisq (- n mm)))))
  1704. (if mode
  1705. (while (setq bp (cdr bp))
  1706. (setcar bp (math-make-sdev
  1707. (car bp)
  1708. (math-sqrt (math-mul (nth (setq j (1+ j))
  1709. (car (setq cp (cdr cp))))
  1710. sigdat))))))
  1711. (setq math-fit-new-coefs (math-evaluate-expr coef-filters))
  1712. (if calc-fit-to-trail
  1713. (let ((bp math-fit-new-coefs)
  1714. (cp coefs)
  1715. (vec nil))
  1716. (while (setq bp (cdr bp) cp (cdr cp))
  1717. (setq vec (cons (list 'calcFunc-eq (car cp) (car bp)) vec)))
  1718. (setq calc-fit-to-trail (cons 'vec (nreverse vec)))))))
  1719. ;; Substitute best-fit coefficients back into original formula.
  1720. (setq expr (math-multi-subst
  1721. orig-expr
  1722. (let ((n v)
  1723. (vec nil))
  1724. (while (>= n 1)
  1725. (setq vec (cons (list 'calcFunc-fitvar n) vec)
  1726. n (1- n)))
  1727. (setq n m)
  1728. (while (>= n 1)
  1729. (setq vec (cons (list 'calcFunc-fitparam n) vec)
  1730. n (1- n)))
  1731. vec)
  1732. (append (cdr math-fit-new-coefs) (cdr vars))))
  1733. ;; Package the result.
  1734. (math-normalize
  1735. (if extended
  1736. (list 'vec expr beta covar
  1737. (let ((p coef-filters)
  1738. (n 0))
  1739. (while (and (setq n (1+ n) p (cdr p))
  1740. (eq (car-safe (car p)) 'calcFunc-fitdummy)
  1741. (eq (nth 1 (car p)) n)))
  1742. (if p
  1743. coef-filters
  1744. (list 'vec)))
  1745. chisq
  1746. (if (and have-sdevs (> n mm))
  1747. (list 'calcFunc-utpc chisq (- n mm))
  1748. '(var nan var-nan)))
  1749. expr))))
  1750. (defun calcFunc-fitvar (x)
  1751. (if (>= math-in-fit 2)
  1752. (progn
  1753. (setq x (aref math-dummy-vars (+ math-fit-first-var x -1)))
  1754. (or (calc-var-value (nth 2 x)) x))
  1755. (math-reject-arg x)))
  1756. (defun calcFunc-fitparam (x)
  1757. (if (>= math-in-fit 2)
  1758. (progn
  1759. (setq x (aref math-dummy-vars (+ math-fit-first-coef x -1)))
  1760. (or (calc-var-value (nth 2 x)) x))
  1761. (math-reject-arg x)))
  1762. (defun calcFunc-fitdummy (x)
  1763. (if (= math-in-fit 3)
  1764. (nth x math-fit-new-coefs)
  1765. (math-reject-arg x)))
  1766. (defun calcFunc-hasfitvars (expr)
  1767. (if (Math-primp expr)
  1768. 0
  1769. (if (eq (car expr) 'calcFunc-fitvar)
  1770. (nth 1 expr)
  1771. (apply 'max (mapcar 'calcFunc-hasfitvars (cdr expr))))))
  1772. (defun calcFunc-hasfitparams (expr)
  1773. (if (Math-primp expr)
  1774. 0
  1775. (if (eq (car expr) 'calcFunc-fitparam)
  1776. (nth 1 expr)
  1777. (apply 'max (mapcar 'calcFunc-hasfitparams (cdr expr))))))
  1778. (defun math-all-vars-but (expr but)
  1779. (let* ((vars (math-all-vars-in expr))
  1780. (p but))
  1781. (while p
  1782. (setq vars (delq (assoc (car-safe p) vars) vars)
  1783. p (cdr p)))
  1784. (sort (mapcar 'car vars)
  1785. (function (lambda (x y) (string< (nth 1 x) (nth 1 y)))))))
  1786. ;; The variables math-all-vars-vars (the vars for math-all-vars) and
  1787. ;; math-all-vars-found are local to math-all-vars-in, but are used by
  1788. ;; math-all-vars-rec which is called by math-all-vars-in.
  1789. (defvar math-all-vars-vars)
  1790. (defvar math-all-vars-found)
  1791. (defun math-all-vars-in (expr)
  1792. (let ((math-all-vars-vars nil)
  1793. math-all-vars-found)
  1794. (math-all-vars-rec expr)
  1795. math-all-vars-vars))
  1796. (defun math-all-vars-rec (expr)
  1797. (if (Math-primp expr)
  1798. (if (eq (car-safe expr) 'var)
  1799. (or (math-const-var expr)
  1800. (if (setq math-all-vars-found (assoc expr math-all-vars-vars))
  1801. (setcdr math-all-vars-found (1+ (cdr math-all-vars-found)))
  1802. (setq math-all-vars-vars (cons (cons expr 1) math-all-vars-vars)))))
  1803. (while (setq expr (cdr expr))
  1804. (math-all-vars-rec (car expr)))))
  1805. (provide 'calcalg3)
  1806. ;;; calcalg3.el ends here