comparison lisp/calc/calcalg2.el @ 41271:fcd507927105

Change all toplevel `setq' forms to `defvar' forms, and move them before their first use. Use `when', `unless'. Remove trailing periods from error forms. Add description and headers suggested by Emacs Lisp coding conventions.
author Colin Walters <walters@gnu.org>
date Mon, 19 Nov 2001 07:43:43 +0000
parents 73f364fd8aaa
children 36c14bc6e7fb
comparison
equal deleted inserted replaced
41270:711f18abaf57 41271:fcd507927105
1 ;; Calculator for GNU Emacs, part II [calc-alg-2.el] 1 ;;; calcalg2.el --- more algebraic functions for Calc
2
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com. 4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
4 7
5 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
6 9
7 ;; GNU Emacs is distributed in the hope that it will be useful, 10 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor 11 ;; but WITHOUT ANY WARRANTY. No author or distributor
17 ;; supposed to have been given to you along with GNU Emacs so you 20 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a 21 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice 22 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies. 23 ;; and this notice must be preserved on all copies.
21 24
22 25 ;;; Commentary:
26
27 ;;; Code:
23 28
24 ;; This file is autoloaded from calc-ext.el. 29 ;; This file is autoloaded from calc-ext.el.
25 (require 'calc-ext) 30 (require 'calc-ext)
26 31
27 (require 'calc-macs) 32 (require 'calc-macs)
30 35
31 36
32 (defun calc-derivative (var num) 37 (defun calc-derivative (var num)
33 (interactive "sDifferentiate with respect to: \np") 38 (interactive "sDifferentiate with respect to: \np")
34 (calc-slow-wrapper 39 (calc-slow-wrapper
35 (and (< num 0) (error "Order of derivative must be positive")) 40 (when (< num 0)
41 (error "Order of derivative must be positive"))
36 (let ((func (if (calc-is-hyperbolic) 'calcFunc-tderiv 'calcFunc-deriv)) 42 (let ((func (if (calc-is-hyperbolic) 'calcFunc-tderiv 'calcFunc-deriv))
37 n expr) 43 n expr)
38 (if (or (equal var "") (equal var "$")) 44 (if (or (equal var "") (equal var "$"))
39 (setq n 2 45 (setq n 2
40 expr (calc-top-n 2) 46 expr (calc-top-n 2)
41 var (calc-top-n 1)) 47 var (calc-top-n 1))
42 (setq var (math-read-expr var)) 48 (setq var (math-read-expr var))
43 (if (eq (car-safe var) 'error) 49 (when (eq (car-safe var) 'error)
44 (error "Bad format in expression: %s" (nth 1 var))) 50 (error "Bad format in expression: %s" (nth 1 var)))
45 (setq n 1 51 (setq n 1
46 expr (calc-top-n 1))) 52 expr (calc-top-n 1)))
47 (while (>= (setq num (1- num)) 0) 53 (while (>= (setq num (1- num)) 0)
48 (setq expr (list func expr var))) 54 (setq expr (list func expr var)))
49 (calc-enter-result n "derv" expr)))) 55 (calc-enter-result n "derv" expr))))
590 (and (= (length expr) 3) 596 (and (= (length expr) 3)
591 (list 'calcFunc-subscr (nth 1 expr) 597 (list 'calcFunc-subscr (nth 1 expr)
592 (math-derivative (nth 2 expr))))))) 598 (math-derivative (nth 2 expr)))))))
593 599
594 600
595 601 (defvar math-integ-var '(var X ---))
596 602 (defvar math-integ-var-2 '(var Y ---))
597 603 (defvar math-integ-vars (list 'f math-integ-var math-integ-var-2))
598 (setq math-integ-var '(var X ---)) 604 (defvar math-integ-var-list (list math-integ-var))
599 (setq math-integ-var-2 '(var Y ---)) 605 (defvar math-integ-var-list-list (list math-integ-var-list))
600 (setq math-integ-vars (list 'f math-integ-var math-integ-var-2))
601 (setq math-integ-var-list (list math-integ-var))
602 (setq math-integ-var-list-list (list math-integ-var-list))
603 606
604 (defmacro math-tracing-integral (&rest parts) 607 (defmacro math-tracing-integral (&rest parts)
605 (list 'and 608 (list 'and
606 'trace-buffer 609 'trace-buffer
607 (list 'save-excursion 610 (list 'save-excursion
1702 '(var pi var-pi))))))) 1705 '(var pi var-pi)))))))
1703 1706
1704 1707
1705 1708
1706 1709
1710 (defvar math-tabulate-initial nil)
1711 (defvar math-tabulate-function nil)
1707 (defun calcFunc-table (expr var &optional low high step) 1712 (defun calcFunc-table (expr var &optional low high step)
1708 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf))) 1713 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf)))
1709 (or high (setq high low low 1)) 1714 (or high (setq high low low 1))
1710 (and (or (math-infinitep low) (math-infinitep high)) 1715 (and (or (math-infinitep low) (math-infinitep high))
1711 (not step) 1716 (not step)
1759 (and (not (and (equal low '(neg (var inf var-inf))) 1764 (and (not (and (equal low '(neg (var inf var-inf)))
1760 (equal high '(var inf var-inf)))) 1765 (equal high '(var inf var-inf))))
1761 (list low high)) 1766 (list low high))
1762 (and step (list step)))))) 1767 (and step (list step))))))
1763 1768
1764 (setq math-tabulate-initial nil)
1765 (setq math-tabulate-function nil)
1766
1767 (defun math-scan-for-limits (x) 1769 (defun math-scan-for-limits (x)
1768 (cond ((Math-primp x)) 1770 (cond ((Math-primp x))
1769 ((and (eq (car x) 'calcFunc-subscr) 1771 ((and (eq (car x) 'calcFunc-subscr)
1770 (Math-vectorp (nth 1 x)) 1772 (Math-vectorp (nth 1 x))
1771 (math-expr-contains (nth 2 x) var)) 1773 (math-expr-contains (nth 2 x) var))
1783 (t 1785 (t
1784 (while (setq x (cdr x)) 1786 (while (setq x (cdr x))
1785 (math-scan-for-limits (car x)))))) 1787 (math-scan-for-limits (car x))))))
1786 1788
1787 1789
1790 (defvar math-disable-sums nil)
1788 (defun calcFunc-sum (expr var &optional low high step) 1791 (defun calcFunc-sum (expr var &optional low high step)
1789 (if math-disable-sums (math-reject-arg)) 1792 (if math-disable-sums (math-reject-arg))
1790 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2))) 1793 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2)))
1791 (math-sum-rec expr var low high step))) 1794 (math-sum-rec expr var low high step)))
1792 (math-disable-sums t)) 1795 (math-disable-sums t))
1793 (math-normalize res))) 1796 (math-normalize res)))
1794 (setq math-disable-sums nil)
1795 1797
1796 (defun math-sum-rec (expr var &optional low high step) 1798 (defun math-sum-rec (expr var &optional low high step)
1797 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf))) 1799 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf)))
1798 (and low (not high) (setq high low low 1)) 1800 (and low (not high) (setq high low low 1))
1799 (let (t1 t2 val) 1801 (let (t1 t2 val)
1939 (let ((temp (or (car not-const) 1))) 1941 (let ((temp (or (car not-const) 1)))
1940 (while (setq not-const (cdr not-const)) 1942 (while (setq not-const (cdr not-const))
1941 (setq temp (list '* (car not-const) temp))) 1943 (setq temp (list '* (car not-const) temp)))
1942 temp))))) 1944 temp)))))
1943 1945
1946 (defvar math-sum-int-pow-cache (list '(0 1)))
1944 ;; Following is from CRC Math Tables, 27th ed, pp. 52-53. 1947 ;; Following is from CRC Math Tables, 27th ed, pp. 52-53.
1945 (defun math-sum-integer-power (pow) 1948 (defun math-sum-integer-power (pow)
1946 (let ((calc-prefer-frac t) 1949 (let ((calc-prefer-frac t)
1947 (n (length math-sum-int-pow-cache))) 1950 (n (length math-sum-int-pow-cache)))
1948 (while (<= n pow) 1951 (while (<= n pow)
1961 (setcar lin (math-sub 1 (math-mul n sum))) 1964 (setcar lin (math-sub 1 (math-mul n sum)))
1962 (setq math-sum-int-pow-cache 1965 (setq math-sum-int-pow-cache
1963 (nconc math-sum-int-pow-cache (list (nreverse new))) 1966 (nconc math-sum-int-pow-cache (list (nreverse new)))
1964 n (1+ n)))) 1967 n (1+ n))))
1965 (nth pow math-sum-int-pow-cache))) 1968 (nth pow math-sum-int-pow-cache)))
1966 (setq math-sum-int-pow-cache (list '(0 1)))
1967 1969
1968 (defun math-to-exponentials (expr) 1970 (defun math-to-exponentials (expr)
1969 (and (consp expr) 1971 (and (consp expr)
1970 (= (length expr) 2) 1972 (= (length expr) 2)
1971 (let ((x (nth 1 expr)) 1973 (let ((x (nth 1 expr))
2011 (list 'calcFunc-exp (nth 2 expr))) 2013 (list 'calcFunc-exp (nth 2 expr)))
2012 (t 2014 (t
2013 (cons (car expr) (mapcar 'math-to-exps (cdr expr)))))) 2015 (cons (car expr) (mapcar 'math-to-exps (cdr expr))))))
2014 2016
2015 2017
2018 (defvar math-disable-prods nil)
2016 (defun calcFunc-prod (expr var &optional low high step) 2019 (defun calcFunc-prod (expr var &optional low high step)
2017 (if math-disable-prods (math-reject-arg)) 2020 (if math-disable-prods (math-reject-arg))
2018 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2))) 2021 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2)))
2019 (math-prod-rec expr var low high step))) 2022 (math-prod-rec expr var low high step)))
2020 (math-disable-prods t)) 2023 (math-disable-prods t))
2021 (math-normalize res))) 2024 (math-normalize res)))
2022 (setq math-disable-prods nil)
2023 2025
2024 (defun math-prod-rec (expr var &optional low high step) 2026 (defun math-prod-rec (expr var &optional low high step)
2025 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf))) 2027 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf)))
2026 (and low (not high) (setq high '(var inf var-inf))) 2028 (and low (not high) (setq high '(var inf var-inf)))
2027 (let (t1 t2 t3 val) 2029 (let (t1 t2 t3 val)
2163 (calcFunc-table expr var low high))))) 2165 (calcFunc-table expr var low high)))))
2164 2166
2165 2167
2166 2168
2167 2169
2170 (defvar math-solve-ranges nil)
2168 ;;; Attempt to reduce lhs = rhs to solve-var = rhs', where solve-var appears 2171 ;;; Attempt to reduce lhs = rhs to solve-var = rhs', where solve-var appears
2169 ;;; in lhs but not in rhs or rhs'; return rhs'. 2172 ;;; in lhs but not in rhs or rhs'; return rhs'.
2170 ;;; Uses global values: solve-*. 2173 ;;; Uses global values: solve-*.
2171 (defun math-try-solve-for (lhs rhs &optional sign no-poly) 2174 (defun math-try-solve-for (lhs rhs &optional sign no-poly)
2172 (let (t1 t2 t3) 2175 (let (t1 t2 t3)
2309 (math-try-solve-for t1 rhs sign)) 2312 (math-try-solve-for t1 rhs sign))
2310 (t 2313 (t
2311 (calc-record-why "*No inverse known" lhs) 2314 (calc-record-why "*No inverse known" lhs)
2312 nil)))) 2315 nil))))
2313 2316
2314 (setq math-solve-ranges nil)
2315 2317
2316 (defun math-try-solve-prod () 2318 (defun math-try-solve-prod ()
2317 (cond ((eq (car lhs) '*) 2319 (cond ((eq (car lhs) '*)
2318 (cond ((not (math-expr-contains (nth 1 lhs) solve-var)) 2320 (cond ((not (math-expr-contains (nth 1 lhs) solve-var))
2319 (math-try-solve-for (nth 2 lhs) 2321 (math-try-solve-for (nth 2 lhs)
2654 (math-sub (math-add (math-mul sign1 (math-div r 2)) 2656 (math-sub (math-add (math-mul sign1 (math-div r 2))
2655 (math-solve-get-sign (math-div de 2))) 2657 (math-solve-get-sign (math-div de 2)))
2656 (math-div a 4)))) 2658 (math-div a 4))))
2657 nil t)) 2659 nil t))
2658 2660
2661 (defvar math-symbolic-solve nil)
2662 (defvar math-int-coefs nil)
2659 (defun math-poly-all-roots (var p &optional math-factoring) 2663 (defun math-poly-all-roots (var p &optional math-factoring)
2660 (catch 'ouch 2664 (catch 'ouch
2661 (let* ((math-symbolic-solve calc-symbolic-mode) 2665 (let* ((math-symbolic-solve calc-symbolic-mode)
2662 (roots nil) 2666 (roots nil)
2663 (deg (1- (length p))) 2667 (deg (1- (length p)))
2748 (if (eq solve-full t) 2752 (if (eq solve-full t)
2749 (list 'calcFunc-subscr 2753 (list 'calcFunc-subscr
2750 vec 2754 vec
2751 (math-solve-get-int 1 (1- (length orig-p)) 1)) 2755 (math-solve-get-int 1 (1- (length orig-p)) 1))
2752 vec)))))) 2756 vec))))))
2753 (setq math-symbolic-solve nil)
2754 2757
2755 (defun math-lcm-denoms (&rest fracs) 2758 (defun math-lcm-denoms (&rest fracs)
2756 (let ((den 1)) 2759 (let ((den 1))
2757 (while fracs 2760 (while fracs
2758 (if (eq (car-safe (car fracs)) 'frac) 2761 (if (eq (car-safe (car fracs)) 'frac)
2868 (math-add aa 2871 (math-add aa
2869 (let ((calc-symbolic-mode math-symbolic-solve)) 2872 (let ((calc-symbolic-mode math-symbolic-solve))
2870 (math-mul (math-sqrt (math-sub (math-sqr aa) 2873 (math-mul (math-sqrt (math-sub (math-sqr aa)
2871 rnd0)) 2874 rnd0))
2872 (if (math-negp xim) -1 1))))))))))) 2875 (if (math-negp xim) -1 1)))))))))))
2873 (setq math-int-coefs nil)
2874 2876
2875 ;;; The following routine is from Numerical Recipes, section 9.5. 2877 ;;; The following routine is from Numerical Recipes, section 9.5.
2876 (defun math-poly-laguerre-root (p x polish) 2878 (defun math-poly-laguerre-root (p x polish)
2877 (let* ((calc-prefer-frac nil) 2879 (let* ((calc-prefer-frac nil)
2878 (calc-symbolic-mode nil) 2880 (calc-symbolic-mode nil)