calc-stuff.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ;;; calc-stuff.el --- miscellaneous functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; This file is autoloaded from calc-ext.el.
  19. (require 'calc-ext)
  20. (require 'calc-macs)
  21. (defun calc-num-prefix (n)
  22. "Use the number at the top of stack as the numeric prefix for the next command.
  23. With a prefix, push that prefix as a number onto the stack."
  24. (interactive "P")
  25. (calc-wrapper
  26. (if n
  27. (calc-enter-result 0 "" (prefix-numeric-value n))
  28. (let ((num (calc-top 1)))
  29. (if (math-messy-integerp num)
  30. (setq num (math-trunc num)))
  31. (or (integerp num)
  32. (error "Argument must be a small integer"))
  33. (calc-pop-stack 1)
  34. (setq prefix-arg num)
  35. (message "%d-" num))))) ; a (lame) simulation of the real thing...
  36. (defun calc-more-recursion-depth (n)
  37. (interactive "P")
  38. (calc-wrapper
  39. (if (calc-is-inverse)
  40. (calc-less-recursion-depth n)
  41. (let ((n (if n (prefix-numeric-value n) 2)))
  42. (if (> n 1)
  43. (setq max-specpdl-size (* max-specpdl-size n)
  44. max-lisp-eval-depth (* max-lisp-eval-depth n))))
  45. (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))))
  46. (defun calc-less-recursion-depth (n)
  47. (interactive "P")
  48. (let ((n (if n (prefix-numeric-value n) 2)))
  49. (if (> n 1)
  50. (setq max-specpdl-size
  51. (max (/ max-specpdl-size n) 600)
  52. max-lisp-eval-depth
  53. (max (/ max-lisp-eval-depth n) 200))))
  54. (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))
  55. (defvar calc-which-why nil)
  56. (defvar calc-last-why-command nil)
  57. (defun calc-explain-why (why &optional more)
  58. (if (eq (car why) '*)
  59. (setq why (cdr why)))
  60. (let* ((pred (car why))
  61. (arg (nth 1 why))
  62. (msg (cond ((not pred) "Wrong type of argument")
  63. ((stringp pred) pred)
  64. ((eq pred 'integerp) "Integer expected")
  65. ((eq pred 'natnump)
  66. (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
  67. "Integer expected"
  68. "Nonnegative integer expected"))
  69. ((eq pred 'posintp)
  70. (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
  71. "Integer expected"
  72. "Positive integer expected"))
  73. ((eq pred 'fixnump)
  74. (if (and arg (Math-integerp arg))
  75. "Small integer expected"
  76. "Integer expected"))
  77. ((eq pred 'fixnatnump)
  78. (if (and arg (Math-natnump arg))
  79. "Small integer expected"
  80. (if (and arg (Math-objvecp arg)
  81. (not (Math-integerp arg)))
  82. "Integer expected"
  83. "Nonnegative integer expected")))
  84. ((eq pred 'fixposintp)
  85. (if (and arg (Math-integerp arg) (Math-posp arg))
  86. "Small integer expected"
  87. (if (and arg (Math-objvecp arg)
  88. (not (Math-integerp arg)))
  89. "Integer expected"
  90. "Positive integer expected")))
  91. ((eq pred 'posp) "Positive number expected")
  92. ((eq pred 'negp) "Negative number expected")
  93. ((eq pred 'nonzerop) "Nonzero number expected")
  94. ((eq pred 'realp) "Real number expected")
  95. ((eq pred 'anglep) "Real number expected")
  96. ((eq pred 'hmsp) "HMS form expected")
  97. ((eq pred 'datep)
  98. (if (and arg (Math-objectp arg)
  99. (not (Math-realp arg)))
  100. "Real number or date form expected"
  101. "Date form expected"))
  102. ((eq pred 'numberp) "Number expected")
  103. ((eq pred 'scalarp) "Number expected")
  104. ((eq pred 'vectorp) "Vector or matrix expected")
  105. ((eq pred 'numvecp) "Number or vector expected")
  106. ((eq pred 'matrixp) "Matrix expected")
  107. ((eq pred 'square-matrixp)
  108. (if (and arg (math-matrixp arg))
  109. "Square matrix expected"
  110. "Matrix expected"))
  111. ((eq pred 'objectp) "Number expected")
  112. ((eq pred 'constp) "Constant expected")
  113. ((eq pred 'range) "Argument out of range")
  114. (t (format "%s expected" pred))))
  115. (punc ": ")
  116. (calc-can-abbrev-vectors t))
  117. (while (setq why (cdr why))
  118. (and (car why)
  119. (setq msg (concat msg punc (if (stringp (car why))
  120. (car why)
  121. (math-format-flat-expr (car why) 0)))
  122. punc ", ")))
  123. (message "%s%s" msg (if more " [w=more]" ""))))
  124. (defun calc-why ()
  125. (interactive)
  126. (if (not (eq this-command last-command))
  127. (if (eq last-command calc-last-why-command)
  128. (setq calc-which-why (cdr calc-why))
  129. (setq calc-which-why calc-why)))
  130. (if calc-which-why
  131. (progn
  132. (calc-explain-why (car calc-which-why) (cdr calc-which-why))
  133. (setq calc-which-why (cdr calc-which-why)))
  134. (if calc-why
  135. (progn
  136. (message "(No further explanations available)")
  137. (setq calc-which-why calc-why))
  138. (message "No explanations available"))))
  139. ;; The following caches are declared in other files, but are
  140. ;; reset here.
  141. (defvar math-lud-cache) ; calc-mtx.el
  142. (defvar math-log2-cache) ; calc-bin.el
  143. (defvar math-radix-digits-cache) ; calc-bin.el
  144. (defvar math-radix-float-cache-tag) ; calc-bin.el
  145. (defvar math-random-cache) ; calc-comb.el
  146. (defvar math-max-digits-cache) ; calc-bin.el
  147. (defvar math-integral-cache) ; calcalg2.el
  148. (defvar math-units-table) ; calc-units.el
  149. (defvar math-decls-cache-tag) ; calc-arith.el
  150. (defvar math-format-date-cache) ; calc-forms.el
  151. (defvar math-holidays-cache-tag) ; calc-forms.el
  152. (defun calc-flush-caches (&optional inhibit-msg)
  153. (interactive "P")
  154. (calc-wrapper
  155. (setq math-lud-cache nil
  156. math-log2-cache nil
  157. math-radix-digits-cache nil
  158. math-radix-float-cache-tag nil
  159. math-random-cache nil
  160. math-max-digits-cache nil
  161. math-integral-cache nil
  162. math-units-table nil
  163. math-decls-cache-tag nil
  164. math-eval-rules-cache-tag t
  165. math-format-date-cache nil
  166. math-holidays-cache-tag t)
  167. (mapc (function (lambda (x) (set x -100))) math-cache-list)
  168. (unless inhibit-msg
  169. (message "All internal calculator caches have been reset"))))
  170. ;;; Conversions.
  171. (defun calc-clean (n)
  172. (interactive "P")
  173. (calc-slow-wrapper
  174. (calc-with-default-simplification
  175. (let ((func (if (calc-is-hyperbolic) 'calcFunc-clean 'calcFunc-pclean)))
  176. (calc-enter-result 1 "cln"
  177. (if n
  178. (let ((n (prefix-numeric-value n)))
  179. (list func
  180. (calc-top-n 1)
  181. (if (<= n 0)
  182. (+ n calc-internal-prec)
  183. n)))
  184. (list func (calc-top-n 1))))))))
  185. (defun calc-clean-num (num)
  186. (interactive "P")
  187. (calc-clean (- (if num
  188. (prefix-numeric-value num)
  189. (if (and (>= last-command-event ?0)
  190. (<= last-command-event ?9))
  191. (- last-command-event ?0)
  192. (error "Number required"))))))
  193. (defvar math-chopping-small nil)
  194. (defun calcFunc-clean (a &optional prec) ; [X X S] [Public]
  195. (if prec
  196. (cond ((Math-messy-integerp prec)
  197. (calcFunc-clean a (math-trunc prec)))
  198. ((or (not (integerp prec))
  199. (< prec 3))
  200. (calc-record-why "*Precision must be an integer 3 or above")
  201. (list 'calcFunc-clean a prec))
  202. ((not (Math-objvecp a))
  203. (list 'calcFunc-clean a prec))
  204. (t (let ((calc-internal-prec prec)
  205. (math-chopping-small t))
  206. (calcFunc-clean (math-normalize a)))))
  207. (cond ((eq (car-safe a) 'polar)
  208. (let ((theta (math-mod (nth 2 a)
  209. (if (eq calc-angle-mode 'rad)
  210. (math-two-pi)
  211. 360))))
  212. (math-neg
  213. (math-neg
  214. (math-normalize
  215. (list 'polar
  216. (calcFunc-clean (nth 1 a))
  217. (calcFunc-clean theta)))))))
  218. ((memq (car-safe a) '(vec date hms))
  219. (cons (car a) (mapcar 'calcFunc-clean (cdr a))))
  220. ((memq (car-safe a) '(cplx mod sdev intv))
  221. (math-normalize (cons (car a) (mapcar 'calcFunc-clean (cdr a)))))
  222. ((eq (car-safe a) 'float)
  223. (if math-chopping-small
  224. (if (or (> (nth 2 a) (- calc-internal-prec))
  225. (Math-lessp (- calc-internal-prec) (calcFunc-xpon a)))
  226. (if (and (math-num-integerp a)
  227. (math-lessp (calcFunc-xpon a) calc-internal-prec))
  228. (math-trunc a)
  229. a)
  230. 0)
  231. a))
  232. ((Math-objectp a) a)
  233. ((math-infinitep a) a)
  234. (t (list 'calcFunc-clean a)))))
  235. (defun calcFunc-pclean (a &optional prec)
  236. (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec)))
  237. a))
  238. (defun calcFunc-pfloat (a)
  239. (math-map-over-constants 'math-float a))
  240. (defun calcFunc-pfrac (a &optional tol)
  241. (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol)))
  242. a))
  243. ;; The variable math-moc-func is local to math-map-over-constants,
  244. ;; but is used by math-map-over-constants-rec, which is called by
  245. ;; math-map-over-constants.
  246. (defvar math-moc-func)
  247. (defun math-map-over-constants (math-moc-func expr)
  248. (math-map-over-constants-rec expr))
  249. (defun math-map-over-constants-rec (expr)
  250. (cond ((or (Math-primp expr)
  251. (memq (car expr) '(intv sdev)))
  252. (or (and (Math-objectp expr)
  253. (funcall math-moc-func expr))
  254. expr))
  255. ((and (memq (car expr) '(^ calcFunc-subscr))
  256. (eq math-moc-func 'math-float)
  257. (= (length expr) 3)
  258. (Math-integerp (nth 2 expr)))
  259. (list (car expr)
  260. (math-map-over-constants-rec (nth 1 expr))
  261. (nth 2 expr)))
  262. (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr))))))
  263. (provide 'calc-stuff)
  264. ;;; calc-stuff.el ends here