2018-11-20 15:37:46 +00:00
|
|
|
;;; calc-alg.el --- algebraic functions for Calc -*- lexical-binding:t -*-
|
2001-11-19 07:44:56 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 1990-1993, 2001-2024 Free Software Foundation, Inc.
|
2001-11-19 07:44:56 +00:00
|
|
|
|
|
|
|
;; Author: David Gillespie <daveg@synaptics.com>
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 03:16:00 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2007-03-19 20:59:53 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:16:00 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2007-03-19 20:59:53 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2007-03-19 20:59:53 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
;;; Commentary:
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
;;; Code:
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;; This file is autoloaded from calc-ext.el.
|
|
|
|
|
2004-11-30 17:00:26 +00:00
|
|
|
(require 'calc-ext)
|
2001-11-06 18:59:06 +00:00
|
|
|
(require 'calc-macs)
|
|
|
|
|
|
|
|
;;; Algebra commands.
|
|
|
|
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar math-simplify-only)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-alg-evaluate (arg)
|
|
|
|
(interactive "p")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-with-default-simplification
|
|
|
|
(let ((math-simplify-only nil))
|
|
|
|
(calc-modify-simplify-mode arg)
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-enter-result 1 "dsmp" (calc-top 1))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar calc-simplify-mode)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-modify-simplify-mode (arg)
|
|
|
|
(if (= (math-abs arg) 2)
|
|
|
|
(setq calc-simplify-mode 'alg)
|
|
|
|
(if (>= (math-abs arg) 3)
|
|
|
|
(setq calc-simplify-mode 'ext)))
|
|
|
|
(if (< arg 0)
|
2001-11-14 09:00:01 +00:00
|
|
|
(setq calc-simplify-mode (list calc-simplify-mode))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-simplify ()
|
|
|
|
(interactive)
|
|
|
|
(calc-slow-wrapper
|
2009-08-24 18:43:50 +00:00
|
|
|
(let ((top (calc-top-n 1)))
|
|
|
|
(if (calc-is-inverse)
|
2009-09-19 21:11:40 +00:00
|
|
|
(setq top
|
2009-08-24 18:43:50 +00:00
|
|
|
(let ((calc-simplify-mode nil))
|
|
|
|
(math-normalize (math-trig-rewrite top)))))
|
|
|
|
(if (calc-is-hyperbolic)
|
2009-09-19 21:11:40 +00:00
|
|
|
(setq top
|
2009-08-24 18:43:50 +00:00
|
|
|
(let ((calc-simplify-mode nil))
|
|
|
|
(math-normalize (math-hyperbolic-trig-rewrite top)))))
|
|
|
|
(calc-with-default-simplification
|
|
|
|
(calc-enter-result 1 "simp" (math-simplify top))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-simplify-extended ()
|
|
|
|
(interactive)
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-with-default-simplification
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-enter-result 1 "esmp" (math-simplify-extended (calc-top-n 1))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar math-expand-formulas)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-expand-formula (arg)
|
|
|
|
(interactive "p")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-with-default-simplification
|
|
|
|
(let ((math-simplify-only nil))
|
|
|
|
(calc-modify-simplify-mode arg)
|
2003-02-04 13:24:35 +00:00
|
|
|
(calc-enter-result 1 "expf"
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (> arg 0)
|
|
|
|
(let ((math-expand-formulas t))
|
|
|
|
(calc-top-n 1))
|
|
|
|
(let ((top (calc-top-n 1)))
|
|
|
|
(or (math-expand-formula top)
|
2001-11-14 09:00:01 +00:00
|
|
|
top))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-factor (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-unary-op "fctr" (if (calc-is-hyperbolic)
|
|
|
|
'calcFunc-factors 'calcFunc-factor)
|
2001-11-14 09:00:01 +00:00
|
|
|
arg)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-expand (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 1 "expa"
|
|
|
|
(append (list 'calcFunc-expand
|
|
|
|
(calc-top-n 1))
|
2001-11-14 09:00:01 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2005-10-28 03:52:38 +00:00
|
|
|
;;; Write out powers (a*b*...)^n as a*b*...*a*b*...
|
2005-11-14 20:12:56 +00:00
|
|
|
(defun calcFunc-powerexpand (expr)
|
|
|
|
(math-normalize (math-map-tree 'math-powerexpand expr)))
|
2005-10-28 03:52:38 +00:00
|
|
|
|
2005-11-14 20:12:56 +00:00
|
|
|
(defun math-powerexpand (expr)
|
2005-10-28 03:52:38 +00:00
|
|
|
(if (eq (car-safe expr) '^)
|
2005-11-14 20:12:56 +00:00
|
|
|
(let ((n (nth 2 expr)))
|
|
|
|
(cond ((and (integerp n)
|
|
|
|
(> n 0))
|
|
|
|
(let ((i 1)
|
|
|
|
(a (nth 1 expr))
|
|
|
|
(prod (nth 1 expr)))
|
|
|
|
(while (< i n)
|
|
|
|
(setq prod (math-mul prod a))
|
|
|
|
(setq i (1+ i)))
|
|
|
|
prod))
|
|
|
|
((and (integerp n)
|
|
|
|
(< n 0))
|
|
|
|
(let ((i -1)
|
|
|
|
(a (math-pow (nth 1 expr) -1))
|
|
|
|
(prod (math-pow (nth 1 expr) -1)))
|
|
|
|
(while (> i n)
|
|
|
|
(setq prod (math-mul a prod))
|
|
|
|
(setq i (1- i)))
|
|
|
|
prod))
|
|
|
|
(t
|
|
|
|
expr)))
|
2005-10-28 03:52:38 +00:00
|
|
|
expr))
|
|
|
|
|
2005-11-14 20:12:56 +00:00
|
|
|
(defun calc-powerexpand ()
|
2005-10-28 03:52:38 +00:00
|
|
|
(interactive)
|
|
|
|
(calc-slow-wrapper
|
2005-11-14 20:12:56 +00:00
|
|
|
(calc-enter-result 1 "pexp"
|
|
|
|
(calcFunc-powerexpand (calc-top-n 1)))))
|
2005-10-28 03:52:38 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-collect (&optional var)
|
|
|
|
(interactive "sCollect terms involving: ")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(if (or (equal var "") (equal var "$") (null var))
|
|
|
|
(calc-enter-result 2 "clct" (cons 'calcFunc-collect
|
|
|
|
(calc-top-list-n 2)))
|
|
|
|
(let ((var (math-read-expr var)))
|
|
|
|
(if (eq (car-safe var) 'error)
|
|
|
|
(error "Bad format in expression: %s" (nth 1 var)))
|
|
|
|
(calc-enter-result 1 "clct" (list 'calcFunc-collect
|
|
|
|
(calc-top-n 1)
|
2001-11-14 09:00:01 +00:00
|
|
|
var))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-apart (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-unary-op "aprt" 'calcFunc-apart arg)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-normalize-rat (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-unary-op "nrat" 'calcFunc-nrat arg)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-poly-gcd (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-binary-op "pgcd" 'calcFunc-pgcd arg)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar calc-poly-div-remainder)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-poly-div (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(let ((calc-poly-div-remainder nil))
|
|
|
|
(calc-binary-op "pdiv" 'calcFunc-pdiv arg)
|
|
|
|
(if (and calc-poly-div-remainder (null arg))
|
|
|
|
(progn
|
|
|
|
(calc-clear-command-flag 'clear-message)
|
|
|
|
(calc-record calc-poly-div-remainder "prem")
|
|
|
|
(if (not (Math-zerop calc-poly-div-remainder))
|
|
|
|
(message "(Remainder was %s)"
|
|
|
|
(math-format-flat-expr calc-poly-div-remainder 0))
|
|
|
|
(message "(No remainder)")))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-poly-rem (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-binary-op "prem" 'calcFunc-prem arg)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-poly-div-rem (arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(if (calc-is-hyperbolic)
|
|
|
|
(calc-binary-op "pdvr" 'calcFunc-pdivide arg)
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-binary-op "pdvr" 'calcFunc-pdivrem arg))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-substitute (&optional oldname newname)
|
|
|
|
(interactive "sSubstitute old: ")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(let (old new (num 1) expr)
|
|
|
|
(if (or (equal oldname "") (equal oldname "$") (null oldname))
|
|
|
|
(setq new (calc-top-n 1)
|
|
|
|
old (calc-top-n 2)
|
|
|
|
expr (calc-top-n 3)
|
|
|
|
num 3)
|
|
|
|
(or newname
|
|
|
|
(progn (calc-unread-command ?\C-a)
|
|
|
|
(setq newname (read-string (concat "Substitute old: "
|
|
|
|
oldname
|
|
|
|
", new: ")
|
|
|
|
oldname))))
|
|
|
|
(if (or (equal newname "") (equal newname "$") (null newname))
|
|
|
|
(setq new (calc-top-n 1)
|
|
|
|
expr (calc-top-n 2)
|
|
|
|
num 2)
|
|
|
|
(setq new (if (stringp newname) (math-read-expr newname) newname))
|
|
|
|
(if (eq (car-safe new) 'error)
|
|
|
|
(error "Bad format in expression: %s" (nth 1 new)))
|
|
|
|
(setq expr (calc-top-n 1)))
|
|
|
|
(setq old (if (stringp oldname) (math-read-expr oldname) oldname))
|
|
|
|
(if (eq (car-safe old) 'error)
|
|
|
|
(error "Bad format in expression: %s" (nth 1 old)))
|
|
|
|
(or (math-expr-contains expr old)
|
2001-11-19 07:44:56 +00:00
|
|
|
(error "No occurrences found")))
|
2001-11-14 09:00:01 +00:00
|
|
|
(calc-enter-result num "sbst" (math-expr-subst expr old new)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun calc-has-rules (name)
|
|
|
|
(setq name (calc-var-value name))
|
|
|
|
(and (consp name)
|
|
|
|
(memq (car name) '(vec calcFunc-assign calcFunc-condition))
|
2001-11-14 09:00:01 +00:00
|
|
|
name))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
;; math-eval-rules-cache and math-eval-rules-cache-other are
|
|
|
|
;; declared in calc.el, but are used here by math-recompile-eval-rules.
|
|
|
|
(defvar math-eval-rules-cache)
|
|
|
|
(defvar math-eval-rules-cache-other)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-recompile-eval-rules ()
|
|
|
|
(setq math-eval-rules-cache (and (calc-has-rules 'var-EvalRules)
|
|
|
|
(math-compile-rewrites
|
|
|
|
'(var EvalRules var-EvalRules)))
|
|
|
|
math-eval-rules-cache-other (assq nil math-eval-rules-cache)
|
2001-11-14 09:00:01 +00:00
|
|
|
math-eval-rules-cache-tag (calc-var-value 'var-EvalRules)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; Try to expand a formula according to its definition.
|
|
|
|
(defun math-expand-formula (expr)
|
|
|
|
(and (consp expr)
|
|
|
|
(symbolp (car expr))
|
|
|
|
(or (get (car expr) 'calc-user-defn)
|
|
|
|
(get (car expr) 'math-expandable))
|
|
|
|
(let ((res (let ((math-expand-formulas t))
|
|
|
|
(apply (car expr) (cdr expr)))))
|
|
|
|
(and (not (eq (car-safe res) (car expr)))
|
2001-11-14 09:00:01 +00:00
|
|
|
res))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; True if A comes before B in a canonical ordering of expressions. [P X X]
|
|
|
|
(defun math-beforep (a b) ; [Public]
|
|
|
|
(cond ((and (Math-realp a) (Math-realp b))
|
|
|
|
(let ((comp (math-compare a b)))
|
|
|
|
(or (eq comp -1)
|
|
|
|
(and (eq comp 0)
|
|
|
|
(not (equal a b))
|
|
|
|
(> (length (memq (car-safe a)
|
* lisp/calc/calc.el: Take advantage of native bignums.
Remove redundant :group args.
(calc-trail-mode): Use inhibit-read-only.
(math-bignum-digit-length, math-bignum-digit-size)
(math-small-integer-size): Delete constants.
(math-normalize): Use native bignums.
(math-bignum, math-bignum-big): Delete functions.
(math-make-float): The mantissa can't be a calc bignum any more.
(math-neg, math-scale-left, math-scale-right, math-scale-rounding)
(math-add, math-sub, math-mul, math-idivmod, math-quotient)
(math-format-number, math-read-number, math-read-number-simple):
Don't bother handling calc bignums.
(math-div10-bignum, math-scale-left-bignum, math-scale-right-bignum)
(math-add-bignum, math-sub-bignum, math-mul-bignum, math-mul-bignum-digit)
(math-div-bignum, math-div-bignum-digit, math-div-bignum-big)
(math-div-bignum-part, math-div-bignum-try, math-format-bignum)
(math-format-bignum-decimal, math-read-bignum): Delete functions.
(math-numdigs): Don't presume that native ints are small enough to use
a slow algorithm.
* lisp/calc/calc-aent.el (calc-do-quick-calc):
* lisp/calc/calc-vec.el (calcFunc-vunpack):
* lisp/calc/calc-alg.el (math-beforep): Don't bother handling calc bignums.
* lisp/calc/calc-bin.el (math-bignum-logb-digit-size)
(math-bignum-digit-power-of-two): Remove constants.
(calcFunc-and, math-binary-arg, calcFunc-or, calcFunc-xor)
(calcFunc-diff, calcFunc-not, math-clip, math-format-twos-complement):
Use Emacs's builtin bignums.
(math-and-bignum, math-or-bignum, math-xor-bignum, math-diff-bignum)
(math-not-bignum, math-clip-bignum)
(math-format-bignum-radix, math-format-bignum-binary)
(math-format-bignum-octal, math-format-bignum-hex): Delete functions.
(math-format-binary): Fix old copy&paste error.
* lisp/calc/calc-comb.el (calc-prime-factors): Adjust for unused arg.
(math-prime-test): math-fixnum is now the identity.
* lisp/calc/calc-ext.el: Require cl-lib.
(math-oddp): Use cl-oddp. Don't bother with calc bignums.
(math-integerp, math-natnump, math-ratp, math-realp, math-anglep)
(math-numberp, math-scalarp, math-vectorp, math-objvecp, math-primp)
(math-num-natnump, math-objectp, math-check-integer, math-compare):
Don't bother handling calc bignums.
(math-check-fixnum): Use fixnump.
(math-fixnum, math-fixnum-big, math-bignum-test): Remove functions.
(math--format-integer-fancy): Rename from math-format-bignum-fancy.
Adjust for internal bignums.
* lisp/calc/calc-funcs.el (calcFunc-besJ): Use cl-isqrt.
* lisp/calc/calc-macs.el (Math-zerop, Math-integer-negp)
(Math-integer-posp, Math-negp, Math-posp, Math-integerp)
(Math-natnump, Math-ratp, Math-realp, Math-anglep, Math-numberp)
(Math-scalarp, Math-vectorp, Math-objectp, Math-objvecp)
(Math-integer-neg, Math-primp, Math-num-integerp):
Don't bother handling calc bignums.
(Math-bignum-test): Delete function.
* lisp/calc/calc-math.el (math-use-emacs-fn): Remove unused `fx`.
(math-isqrt, math-sqrt): Use cl-isqrt. Don't bother handling calc bignums.
(math-isqrt-bignum, math-isqrt-bignum-iter, math-isqrt-small):
Delete function.
* lisp/calc/calc-misc.el (math-fixnump, math-fixnatnump): Use fixnump.
(math-evenp): Use cl-evenp.
(math-zerop, math-negp, math-posp, math-div2): Don't bother handling
calc bignums.
(math-div2-bignum): Delete function.
2019-06-26 03:05:11 +00:00
|
|
|
'(nil frac float)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(length (memq (car-safe b)
|
* lisp/calc/calc.el: Take advantage of native bignums.
Remove redundant :group args.
(calc-trail-mode): Use inhibit-read-only.
(math-bignum-digit-length, math-bignum-digit-size)
(math-small-integer-size): Delete constants.
(math-normalize): Use native bignums.
(math-bignum, math-bignum-big): Delete functions.
(math-make-float): The mantissa can't be a calc bignum any more.
(math-neg, math-scale-left, math-scale-right, math-scale-rounding)
(math-add, math-sub, math-mul, math-idivmod, math-quotient)
(math-format-number, math-read-number, math-read-number-simple):
Don't bother handling calc bignums.
(math-div10-bignum, math-scale-left-bignum, math-scale-right-bignum)
(math-add-bignum, math-sub-bignum, math-mul-bignum, math-mul-bignum-digit)
(math-div-bignum, math-div-bignum-digit, math-div-bignum-big)
(math-div-bignum-part, math-div-bignum-try, math-format-bignum)
(math-format-bignum-decimal, math-read-bignum): Delete functions.
(math-numdigs): Don't presume that native ints are small enough to use
a slow algorithm.
* lisp/calc/calc-aent.el (calc-do-quick-calc):
* lisp/calc/calc-vec.el (calcFunc-vunpack):
* lisp/calc/calc-alg.el (math-beforep): Don't bother handling calc bignums.
* lisp/calc/calc-bin.el (math-bignum-logb-digit-size)
(math-bignum-digit-power-of-two): Remove constants.
(calcFunc-and, math-binary-arg, calcFunc-or, calcFunc-xor)
(calcFunc-diff, calcFunc-not, math-clip, math-format-twos-complement):
Use Emacs's builtin bignums.
(math-and-bignum, math-or-bignum, math-xor-bignum, math-diff-bignum)
(math-not-bignum, math-clip-bignum)
(math-format-bignum-radix, math-format-bignum-binary)
(math-format-bignum-octal, math-format-bignum-hex): Delete functions.
(math-format-binary): Fix old copy&paste error.
* lisp/calc/calc-comb.el (calc-prime-factors): Adjust for unused arg.
(math-prime-test): math-fixnum is now the identity.
* lisp/calc/calc-ext.el: Require cl-lib.
(math-oddp): Use cl-oddp. Don't bother with calc bignums.
(math-integerp, math-natnump, math-ratp, math-realp, math-anglep)
(math-numberp, math-scalarp, math-vectorp, math-objvecp, math-primp)
(math-num-natnump, math-objectp, math-check-integer, math-compare):
Don't bother handling calc bignums.
(math-check-fixnum): Use fixnump.
(math-fixnum, math-fixnum-big, math-bignum-test): Remove functions.
(math--format-integer-fancy): Rename from math-format-bignum-fancy.
Adjust for internal bignums.
* lisp/calc/calc-funcs.el (calcFunc-besJ): Use cl-isqrt.
* lisp/calc/calc-macs.el (Math-zerop, Math-integer-negp)
(Math-integer-posp, Math-negp, Math-posp, Math-integerp)
(Math-natnump, Math-ratp, Math-realp, Math-anglep, Math-numberp)
(Math-scalarp, Math-vectorp, Math-objectp, Math-objvecp)
(Math-integer-neg, Math-primp, Math-num-integerp):
Don't bother handling calc bignums.
(Math-bignum-test): Delete function.
* lisp/calc/calc-math.el (math-use-emacs-fn): Remove unused `fx`.
(math-isqrt, math-sqrt): Use cl-isqrt. Don't bother handling calc bignums.
(math-isqrt-bignum, math-isqrt-bignum-iter, math-isqrt-small):
Delete function.
* lisp/calc/calc-misc.el (math-fixnump, math-fixnatnump): Use fixnump.
(math-evenp): Use cl-evenp.
(math-zerop, math-negp, math-posp, math-div2): Don't bother handling
calc bignums.
(math-div2-bignum): Delete function.
2019-06-26 03:05:11 +00:00
|
|
|
'(nil frac float))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
((equal b '(neg (var inf var-inf))) nil)
|
|
|
|
((equal a '(neg (var inf var-inf))) t)
|
|
|
|
((equal a '(var inf var-inf)) nil)
|
|
|
|
((equal b '(var inf var-inf)) t)
|
|
|
|
((Math-realp a)
|
|
|
|
(if (and (eq (car-safe b) 'intv) (math-intv-constp b))
|
|
|
|
(if (or (math-beforep a (nth 2 b)) (Math-equal a (nth 2 b)))
|
|
|
|
t
|
|
|
|
nil)
|
|
|
|
t))
|
|
|
|
((Math-realp b)
|
|
|
|
(if (and (eq (car-safe a) 'intv) (math-intv-constp a))
|
|
|
|
(if (math-beforep (nth 2 a) b)
|
|
|
|
t
|
|
|
|
nil)
|
|
|
|
nil))
|
|
|
|
((and (eq (car a) 'intv) (eq (car b) 'intv)
|
|
|
|
(math-intv-constp a) (math-intv-constp b))
|
|
|
|
(let ((comp (math-compare (nth 2 a) (nth 2 b))))
|
|
|
|
(cond ((eq comp -1) t)
|
|
|
|
((eq comp 1) nil)
|
|
|
|
((and (memq (nth 1 a) '(2 3)) (memq (nth 1 b) '(0 1))) t)
|
|
|
|
((and (memq (nth 1 a) '(0 1)) (memq (nth 1 b) '(2 3))) nil)
|
|
|
|
((eq (setq comp (math-compare (nth 3 a) (nth 3 b))) -1) t)
|
|
|
|
((eq comp 1) nil)
|
|
|
|
((and (memq (nth 1 a) '(0 2)) (memq (nth 1 b) '(1 3))) t)
|
|
|
|
(t nil))))
|
|
|
|
((not (eq (not (Math-objectp a)) (not (Math-objectp b))))
|
|
|
|
(Math-objectp a))
|
|
|
|
((eq (car a) 'var)
|
|
|
|
(if (eq (car b) 'var)
|
2014-06-26 07:13:13 +00:00
|
|
|
(string-lessp (nth 1 a) (nth 1 b))
|
2001-11-06 18:59:06 +00:00
|
|
|
(not (Math-numberp b))))
|
|
|
|
((eq (car b) 'var) (Math-numberp a))
|
|
|
|
((eq (car a) (car b))
|
|
|
|
(while (and (setq a (cdr a) b (cdr b)) a
|
|
|
|
(equal (car a) (car b))))
|
|
|
|
(and b
|
|
|
|
(or (null a)
|
|
|
|
(math-beforep (car a) (car b)))))
|
2014-06-26 07:13:13 +00:00
|
|
|
(t (string-lessp (car a) (car b)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar math-living-dangerously)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-14 09:00:01 +00:00
|
|
|
(defsubst math-simplify-extended (a)
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((math-living-dangerously t))
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-simplify a)))
|
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defalias 'calcFunc-esimplify #'math-simplify-extended)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2009-08-24 18:43:50 +00:00
|
|
|
;;; Rewrite the trig functions in a form easier to simplify.
|
|
|
|
(defun math-trig-rewrite (fn)
|
|
|
|
"Rewrite trigonometric functions in terms of sines and cosines."
|
|
|
|
(cond
|
|
|
|
((not (consp fn))
|
|
|
|
fn)
|
|
|
|
((eq (car-safe fn) 'calcFunc-sec)
|
|
|
|
(list '/ 1 (cons 'calcFunc-cos (math-trig-rewrite (cdr fn)))))
|
|
|
|
((eq (car-safe fn) 'calcFunc-csc)
|
|
|
|
(list '/ 1 (cons 'calcFunc-sin (math-trig-rewrite (cdr fn)))))
|
|
|
|
((eq (car-safe fn) 'calcFunc-tan)
|
|
|
|
(let ((newfn (math-trig-rewrite (cdr fn))))
|
|
|
|
(list '/ (cons 'calcFunc-sin newfn)
|
|
|
|
(cons 'calcFunc-cos newfn))))
|
|
|
|
((eq (car-safe fn) 'calcFunc-cot)
|
|
|
|
(let ((newfn (math-trig-rewrite (cdr fn))))
|
|
|
|
(list '/ (cons 'calcFunc-cos newfn)
|
|
|
|
(cons 'calcFunc-sin newfn))))
|
|
|
|
(t
|
2018-11-20 15:37:46 +00:00
|
|
|
(mapcar #'math-trig-rewrite fn))))
|
2009-08-24 18:43:50 +00:00
|
|
|
|
|
|
|
(defun math-hyperbolic-trig-rewrite (fn)
|
|
|
|
"Rewrite hyperbolic functions in terms of sinhs and coshs."
|
|
|
|
(cond
|
|
|
|
((not (consp fn))
|
|
|
|
fn)
|
|
|
|
((eq (car-safe fn) 'calcFunc-sech)
|
|
|
|
(list '/ 1 (cons 'calcFunc-cosh (math-hyperbolic-trig-rewrite (cdr fn)))))
|
|
|
|
((eq (car-safe fn) 'calcFunc-csch)
|
|
|
|
(list '/ 1 (cons 'calcFunc-sinh (math-hyperbolic-trig-rewrite (cdr fn)))))
|
|
|
|
((eq (car-safe fn) 'calcFunc-tanh)
|
|
|
|
(let ((newfn (math-hyperbolic-trig-rewrite (cdr fn))))
|
|
|
|
(list '/ (cons 'calcFunc-sinh newfn)
|
|
|
|
(cons 'calcFunc-cosh newfn))))
|
|
|
|
((eq (car-safe fn) 'calcFunc-coth)
|
|
|
|
(let ((newfn (math-hyperbolic-trig-rewrite (cdr fn))))
|
|
|
|
(list '/ (cons 'calcFunc-cosh newfn)
|
|
|
|
(cons 'calcFunc-sinh newfn))))
|
|
|
|
(t
|
2018-11-20 15:37:46 +00:00
|
|
|
(mapcar #'math-hyperbolic-trig-rewrite fn))))
|
2009-08-24 18:43:50 +00:00
|
|
|
|
2009-09-19 21:11:40 +00:00
|
|
|
;; math-top-only is local to math-simplify, but is used by
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
;; math-simplify-step, which is called by math-simplify.
|
|
|
|
(defvar math-top-only)
|
|
|
|
|
2017-05-18 05:47:06 +00:00
|
|
|
(defun calc-input-angle-units (input)
|
|
|
|
(cond ((math-expr-contains input '(var deg var-deg)) 'deg)
|
|
|
|
((math-expr-contains input '(var rad var-rad)) 'rad)
|
|
|
|
((math-expr-contains input '(var hms var-hms)) 'hms)
|
|
|
|
(t nil)))
|
|
|
|
|
2012-07-29 18:51:16 +00:00
|
|
|
;; math-normalize-error is declared in calc.el.
|
|
|
|
(defvar math-normalize-error)
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar math-simplifying)
|
|
|
|
(defvar calc-angle-mode)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-simplify (top-expr)
|
|
|
|
(let ((math-simplifying t)
|
2017-05-18 05:47:06 +00:00
|
|
|
(calc-angle-mode (if (calc-input-angle-units top-expr)
|
|
|
|
'rad
|
|
|
|
calc-angle-mode))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(math-top-only (consp calc-simplify-mode))
|
2001-11-06 18:59:06 +00:00
|
|
|
(simp-rules (append (and (calc-has-rules 'var-AlgSimpRules)
|
|
|
|
'((var AlgSimpRules var-AlgSimpRules)))
|
|
|
|
(and math-living-dangerously
|
|
|
|
(calc-has-rules 'var-ExtSimpRules)
|
|
|
|
'((var ExtSimpRules var-ExtSimpRules)))
|
|
|
|
(and math-simplifying-units
|
|
|
|
(calc-has-rules 'var-UnitSimpRules)
|
|
|
|
'((var UnitSimpRules var-UnitSimpRules)))
|
|
|
|
(and math-integrating
|
|
|
|
(calc-has-rules 'var-IntegSimpRules)
|
|
|
|
'((var IntegSimpRules var-IntegSimpRules)))))
|
|
|
|
res)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(if math-top-only
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((r simp-rules))
|
|
|
|
(setq res (math-simplify-step (math-normalize top-expr))
|
|
|
|
calc-simplify-mode '(nil)
|
|
|
|
top-expr (math-normalize res))
|
|
|
|
(while r
|
|
|
|
(setq top-expr (math-rewrite top-expr (car r)
|
|
|
|
'(neg (var inf var-inf)))
|
|
|
|
r (cdr r))))
|
|
|
|
(calc-with-default-simplification
|
|
|
|
(while (let ((r simp-rules))
|
|
|
|
(setq res (math-normalize top-expr))
|
2012-07-29 18:51:16 +00:00
|
|
|
(if (not math-normalize-error)
|
|
|
|
(progn
|
|
|
|
(while r
|
|
|
|
(setq res (math-rewrite res (car r))
|
|
|
|
r (cdr r)))
|
|
|
|
(not (equal top-expr (setq res (math-simplify-step res)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq top-expr res)))))
|
2001-11-14 09:00:01 +00:00
|
|
|
top-expr)
|
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defalias 'calcFunc-simplify #'math-simplify)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
;; The following has a "bug" in that if any recursive simplifications
|
|
|
|
;; occur only the first handler will be tried; this doesn't really
|
|
|
|
;; matter, since math-simplify-step is iterated to a fixed point anyway.
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-simplify-step (a)
|
|
|
|
(if (Math-primp a)
|
|
|
|
a
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(let ((aa (if (or math-top-only
|
2001-11-06 18:59:06 +00:00
|
|
|
(memq (car a) '(calcFunc-quote calcFunc-condition
|
|
|
|
calcFunc-evalto)))
|
|
|
|
a
|
2018-11-20 15:37:46 +00:00
|
|
|
(cons (car a) (mapcar #'math-simplify-step (cdr a))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (symbolp (car aa))
|
|
|
|
(let ((handler (get (car aa) 'math-simplify)))
|
|
|
|
(and handler
|
|
|
|
(while (and handler
|
|
|
|
(equal (setq aa (or (funcall (car handler) aa)
|
|
|
|
aa))
|
|
|
|
a))
|
|
|
|
(setq handler (cdr handler))))))
|
2001-11-14 09:00:01 +00:00
|
|
|
aa)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
2004-11-30 17:00:26 +00:00
|
|
|
(defmacro math-defsimplify (funcs &rest code)
|
2018-11-20 15:37:46 +00:00
|
|
|
"Define the simplification code for functions FUNCS.
|
|
|
|
Code can refer to the expression to simplify via lexical variable `expr'
|
|
|
|
and should return the simplified expression to use (or nil)."
|
|
|
|
(declare (indent 1) (debug (sexp body)))
|
2011-05-23 17:57:17 +00:00
|
|
|
(cons 'progn
|
2021-04-05 15:28:05 +00:00
|
|
|
(mapcar (lambda (func)
|
|
|
|
`(put ',func 'math-simplify
|
|
|
|
(nconc
|
|
|
|
(get ',func 'math-simplify)
|
|
|
|
(list
|
|
|
|
(lambda (expr) ,@code)))))
|
2011-05-23 17:57:17 +00:00
|
|
|
(if (symbolp funcs) (list funcs) funcs))))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-defsimplify (+ -)
|
2018-11-20 15:37:46 +00:00
|
|
|
(cond ((and (memq (car-safe (nth 1 expr)) '(+ -))
|
|
|
|
(Math-numberp (nth 2 (nth 1 expr)))
|
|
|
|
(not (Math-numberp (nth 2 expr))))
|
|
|
|
(let ((x (nth 2 expr))
|
|
|
|
(op (car expr)))
|
|
|
|
(setcar (cdr (cdr expr)) (nth 2 (nth 1 expr)))
|
|
|
|
(setcar expr (car (nth 1 expr)))
|
|
|
|
(setcar (cdr (cdr (nth 1 expr))) x)
|
|
|
|
(setcar (nth 1 expr) op)))
|
|
|
|
((and (eq (car expr) '+)
|
|
|
|
(Math-numberp (nth 1 expr))
|
|
|
|
(not (Math-numberp (nth 2 expr))))
|
|
|
|
(let ((x (nth 2 expr)))
|
|
|
|
(setcar (cdr (cdr expr)) (nth 1 expr))
|
|
|
|
(setcar (cdr expr) x))))
|
|
|
|
(let ((aa expr)
|
2001-11-06 18:59:06 +00:00
|
|
|
aaa temp)
|
|
|
|
(while (memq (car-safe (setq aaa (nth 1 aa))) '(+ -))
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (setq temp (math-combine-sum (nth 2 aaa) (nth 2 expr)
|
2009-09-19 21:11:40 +00:00
|
|
|
(eq (car aaa) '-)
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car expr) '-) t))
|
2001-11-06 18:59:06 +00:00
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar (cdr (cdr expr)) temp)
|
|
|
|
(setcar expr '+)
|
2001-11-06 18:59:06 +00:00
|
|
|
(setcar (cdr (cdr aaa)) 0)))
|
|
|
|
(setq aa (nth 1 aa)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (setq temp (math-combine-sum aaa (nth 2 expr)
|
|
|
|
nil (eq (car expr) '-) t))
|
2001-11-06 18:59:06 +00:00
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar (cdr (cdr expr)) temp)
|
|
|
|
(setcar expr '+)
|
2001-11-06 18:59:06 +00:00
|
|
|
(setcar (cdr aa) 0)))
|
2018-11-20 15:37:46 +00:00
|
|
|
expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify *
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (eq (car-safe (nth 2 expr)) '*)
|
|
|
|
(and (math-beforep (nth 1 (nth 2 expr)) (nth 1 expr))
|
|
|
|
(or (math-known-scalarp (nth 1 expr) t)
|
|
|
|
(math-known-scalarp (nth 1 (nth 2 expr)) t))
|
|
|
|
(let ((x (nth 1 expr)))
|
|
|
|
(setcar (cdr expr) (nth 1 (nth 2 expr)))
|
|
|
|
(setcar (cdr (nth 2 expr)) x)))
|
|
|
|
(and (math-beforep (nth 2 expr) (nth 1 expr))
|
|
|
|
(or (math-known-scalarp (nth 1 expr) t)
|
|
|
|
(math-known-scalarp (nth 2 expr) t))
|
|
|
|
(let ((x (nth 2 expr)))
|
|
|
|
(setcar (cdr (cdr expr)) (nth 1 expr))
|
|
|
|
(setcar (cdr expr) x))))
|
|
|
|
(let ((aa expr)
|
2001-11-06 18:59:06 +00:00
|
|
|
aaa temp
|
2018-11-20 15:37:46 +00:00
|
|
|
(safe t) (scalar (math-known-scalarp (nth 1 expr))))
|
|
|
|
(if (and (Math-ratp (nth 1 expr))
|
|
|
|
(setq temp (math-common-constant-factor (nth 2 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar (cdr (cdr expr))
|
|
|
|
(math-cancel-common-factor (nth 2 expr) temp))
|
|
|
|
(setcar (cdr expr) (math-mul (nth 1 expr) temp))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(while (and (eq (car-safe (setq aaa (nth 2 aa))) '*)
|
|
|
|
safe)
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (setq temp (math-combine-prod (nth 1 expr)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(nth 1 aaa) nil nil t))
|
2001-11-06 18:59:06 +00:00
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar (cdr expr) temp)
|
2001-11-06 18:59:06 +00:00
|
|
|
(setcar (cdr aaa) 1)))
|
|
|
|
(setq safe (or scalar (math-known-scalarp (nth 1 aaa) t))
|
|
|
|
aa (nth 2 aa)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (and (setq temp (math-combine-prod aaa (nth 1 expr) nil nil t))
|
2001-11-06 18:59:06 +00:00
|
|
|
safe)
|
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar (cdr expr) temp)
|
2001-11-06 18:59:06 +00:00
|
|
|
(setcar (cdr (cdr aa)) 1)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (and (eq (car-safe (nth 1 expr)) 'frac)
|
|
|
|
(memq (nth 1 (nth 1 expr)) '(1 -1)))
|
|
|
|
(math-div (math-mul (nth 2 expr)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(nth 2 (nth 1 expr)))
|
|
|
|
expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify /
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-simplify-divide expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defvar math--simplify-divide-expr)
|
|
|
|
|
|
|
|
(defun math-simplify-divide (expr)
|
|
|
|
(let ((np (cdr expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
(nover nil)
|
2018-11-20 15:37:46 +00:00
|
|
|
(nn (and (or (eq (car expr) '/)
|
|
|
|
(not (Math-realp (nth 2 expr))))
|
|
|
|
(math-common-constant-factor (nth 2 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
n op)
|
|
|
|
(if nn
|
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setq n (and (or (eq (car expr) '/)
|
|
|
|
(not (Math-realp (nth 1 expr))))
|
|
|
|
(math-common-constant-factor (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (and (eq (car-safe nn) 'frac) (eq (nth 1 nn) 1) (not n))
|
2018-11-20 15:37:46 +00:00
|
|
|
(unless (and (eq (car-safe expr) 'calcFunc-eq)
|
|
|
|
(eq (car-safe (nth 1 expr)) 'var)
|
|
|
|
(not (math-expr-contains (nth 2 expr)
|
|
|
|
(nth 1 expr))))
|
|
|
|
(setcar (cdr expr)
|
|
|
|
(math-mul (nth 2 nn) (nth 1 expr)))
|
|
|
|
(setcar (cdr (cdr expr))
|
|
|
|
(math-cancel-common-factor (nth 2 expr) nn))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (and (math-negp nn)
|
2018-11-20 15:37:46 +00:00
|
|
|
(setq op (assq (car expr) calc-tweak-eqn-table)))
|
|
|
|
(setcar expr (nth 1 op))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (and n (not (eq (setq n (math-frac-gcd n nn)) 1)))
|
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar (cdr expr)
|
|
|
|
(math-cancel-common-factor (nth 1 expr) n))
|
|
|
|
(setcar (cdr (cdr expr))
|
|
|
|
(math-cancel-common-factor (nth 2 expr) n))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (and (math-negp n)
|
2018-11-20 15:37:46 +00:00
|
|
|
(setq op (assq (car expr)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
calc-tweak-eqn-table)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(setcar expr (nth 1 op))))))))
|
|
|
|
(let ((math--simplify-divide-expr expr)) ;For use in math-simplify-divisor
|
|
|
|
(if (and (eq (car-safe (car np)) '/)
|
|
|
|
(math-known-scalarp (nth 2 expr) t))
|
|
|
|
(progn
|
|
|
|
(setq np (cdr (nth 1 expr)))
|
|
|
|
(while (eq (car-safe (setq n (car np))) '*)
|
|
|
|
(and (math-known-scalarp (nth 2 n) t)
|
|
|
|
(math-simplify-divisor (cdr n) (cdr (cdr expr)) nil t))
|
|
|
|
(setq np (cdr (cdr n))))
|
|
|
|
(math-simplify-divisor np (cdr (cdr expr)) nil t)
|
|
|
|
(setq nover t
|
|
|
|
np (cdr (cdr (nth 1 expr))))))
|
|
|
|
(while (eq (car-safe (setq n (car np))) '*)
|
|
|
|
(and (math-known-scalarp (nth 2 n) t)
|
|
|
|
(math-simplify-divisor (cdr n) (cdr (cdr expr)) nover t))
|
|
|
|
(setq np (cdr (cdr n))))
|
|
|
|
(math-simplify-divisor np (cdr (cdr expr)) nover t)
|
|
|
|
expr)))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
|
|
|
|
;; The variables math-simplify-divisor-nover and math-simplify-divisor-dover
|
|
|
|
;; are local variables for math-simplify-divisor, but are used by
|
|
|
|
;; math-simplify-one-divisor.
|
|
|
|
(defvar math-simplify-divisor-nover)
|
|
|
|
(defvar math-simplify-divisor-dover)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defun math-simplify-divisor (np dp nover dover)
|
2001-11-06 18:59:06 +00:00
|
|
|
(cond ((eq (car-safe (car dp)) '/)
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-simplify-divisor np (cdr (car dp))
|
2018-11-20 15:37:46 +00:00
|
|
|
nover dover)
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (math-known-scalarp (nth 1 (car dp)) t)
|
|
|
|
(math-simplify-divisor np (cdr (cdr (car dp)))
|
2018-11-20 15:37:46 +00:00
|
|
|
nover (not dover))))
|
|
|
|
((or (or (eq (car math--simplify-divide-expr) '/)
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((signs (math-possible-signs (car np))))
|
|
|
|
(or (memq signs '(1 4))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (memq (car math--simplify-divide-expr)
|
|
|
|
'(calcFunc-eq calcFunc-neq))
|
2001-11-06 18:59:06 +00:00
|
|
|
(eq signs 5))
|
|
|
|
math-living-dangerously)))
|
|
|
|
(math-numberp (car np)))
|
2004-11-18 16:30:27 +00:00
|
|
|
(let (d
|
2009-09-19 21:11:40 +00:00
|
|
|
(safe t)
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-simplify-divisor-nover nover)
|
|
|
|
(math-simplify-divisor-dover dover)
|
2004-11-18 16:30:27 +00:00
|
|
|
(scalar (math-known-scalarp (car np))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(while (and (eq (car-safe (setq d (car dp))) '*)
|
|
|
|
safe)
|
|
|
|
(math-simplify-one-divisor np (cdr d))
|
|
|
|
(setq safe (or scalar (math-known-scalarp (nth 1 d) t))
|
|
|
|
dp (cdr (cdr d))))
|
|
|
|
(if safe
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-simplify-one-divisor np dp))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-simplify-one-divisor (np dp)
|
2009-09-19 21:11:40 +00:00
|
|
|
(let ((temp (math-combine-prod (car np) (car dp) math-simplify-divisor-nover
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
math-simplify-divisor-dover t))
|
|
|
|
op)
|
2009-09-19 21:11:40 +00:00
|
|
|
(if temp
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(progn
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (not (memq (car math--simplify-divide-expr)
|
|
|
|
'(/ calcFunc-eq calcFunc-neq)))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(math-known-negp (car dp))
|
2018-11-20 15:37:46 +00:00
|
|
|
(setq op (assq (car math--simplify-divide-expr)
|
|
|
|
calc-tweak-eqn-table))
|
|
|
|
(setcar math--simplify-divide-expr (nth 1 op)))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(setcar np (if math-simplify-divisor-nover (math-div 1 temp) temp))
|
|
|
|
(setcar dp 1))
|
2009-09-19 21:11:40 +00:00
|
|
|
(and math-simplify-divisor-dover (not math-simplify-divisor-nover)
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car math--simplify-divide-expr) '/)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(eq (car-safe (car dp)) 'calcFunc-sqrt)
|
|
|
|
(Math-integerp (nth 1 (car dp)))
|
|
|
|
(progn
|
|
|
|
(setcar np (math-mul (car np)
|
|
|
|
(list 'calcFunc-sqrt (nth 1 (car dp)))))
|
|
|
|
(setcar dp (nth 1 (car dp))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-common-constant-factor (expr)
|
|
|
|
(if (Math-realp expr)
|
|
|
|
(if (Math-ratp expr)
|
|
|
|
(and (not (memq expr '(0 1 -1)))
|
|
|
|
(math-abs expr))
|
|
|
|
(if (math-ratp (setq expr (math-to-simple-fraction expr)))
|
|
|
|
(math-common-constant-factor expr)))
|
|
|
|
(if (memq (car expr) '(+ - cplx sdev))
|
|
|
|
(let ((f1 (math-common-constant-factor (nth 1 expr)))
|
|
|
|
(f2 (math-common-constant-factor (nth 2 expr))))
|
|
|
|
(and f1 f2
|
|
|
|
(not (eq (setq f1 (math-frac-gcd f1 f2)) 1))
|
|
|
|
f1))
|
|
|
|
(if (memq (car expr) '(* polar))
|
|
|
|
(math-common-constant-factor (nth 1 expr))
|
|
|
|
(if (eq (car expr) '/)
|
|
|
|
(or (math-common-constant-factor (nth 1 expr))
|
|
|
|
(and (Math-integerp (nth 2 expr))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list 'frac 1 (math-abs (nth 2 expr))))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-cancel-common-factor (expr val)
|
|
|
|
(if (memq (car-safe expr) '(+ - cplx sdev))
|
|
|
|
(progn
|
|
|
|
(setcar (cdr expr) (math-cancel-common-factor (nth 1 expr) val))
|
|
|
|
(setcar (cdr (cdr expr)) (math-cancel-common-factor (nth 2 expr) val))
|
|
|
|
expr)
|
|
|
|
(if (eq (car-safe expr) '*)
|
|
|
|
(math-mul (math-cancel-common-factor (nth 1 expr) val) (nth 2 expr))
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-div expr val))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-frac-gcd (a b)
|
|
|
|
(if (Math-zerop a)
|
|
|
|
b
|
|
|
|
(if (Math-zerop b)
|
|
|
|
a
|
|
|
|
(if (and (Math-integerp a)
|
|
|
|
(Math-integerp b))
|
|
|
|
(math-gcd a b)
|
|
|
|
(and (Math-integerp a) (setq a (list 'frac a 1)))
|
|
|
|
(and (Math-integerp b) (setq b (list 'frac b 1)))
|
|
|
|
(math-make-frac (math-gcd (nth 1 a) (nth 1 b))
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-gcd (nth 2 a) (nth 2 b)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar calc-prefer-frac)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-defsimplify %
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (Math-realp (nth 2 expr))
|
|
|
|
(Math-posp (nth 2 expr))
|
|
|
|
(let ((lin (math-is-linear (nth 1 expr)))
|
|
|
|
t1)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and lin
|
|
|
|
(or (math-negp (car lin))
|
2018-11-20 15:37:46 +00:00
|
|
|
(not (Math-lessp (car lin) (nth 2 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(list '%
|
|
|
|
(list '+
|
|
|
|
(math-mul (nth 1 lin) (nth 2 lin))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-mod (car lin) (nth 2 expr)))
|
|
|
|
(nth 2 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and lin
|
|
|
|
(not (math-equal-int (nth 1 lin) 1))
|
|
|
|
(math-num-integerp (nth 1 lin))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-num-integerp (nth 2 expr))
|
|
|
|
(setq t1 (calcFunc-gcd (nth 1 lin) (nth 2 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(not (math-equal-int t1 1))
|
|
|
|
(list '*
|
|
|
|
t1
|
|
|
|
(list '%
|
|
|
|
(list '+
|
|
|
|
(math-mul (math-div (nth 1 lin) t1)
|
|
|
|
(nth 2 lin))
|
|
|
|
(let ((calc-prefer-frac t))
|
|
|
|
(math-div (car lin) t1)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div (nth 2 expr) t1))))
|
|
|
|
(and (math-equal-int (nth 2 expr) 1)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-known-integerp (if lin
|
|
|
|
(math-mul (nth 1 lin) (nth 2 lin))
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 expr)))
|
2001-11-14 09:00:01 +00:00
|
|
|
(if lin (math-mod (car lin) 1) 0))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify (calcFunc-eq calcFunc-neq calcFunc-lt
|
|
|
|
calcFunc-gt calcFunc-leq calcFunc-geq)
|
2018-11-20 15:37:46 +00:00
|
|
|
(if (= (length expr) 3)
|
|
|
|
(math-simplify-ineq expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defun math-simplify-ineq (expr)
|
|
|
|
(let ((np (cdr expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
n)
|
|
|
|
(while (memq (car-safe (setq n (car np))) '(+ -))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-simplify-add-term (cdr (cdr n)) (cdr (cdr expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
(eq (car n) '-) nil)
|
|
|
|
(setq np (cdr n)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-simplify-add-term np (cdr (cdr expr)) nil
|
|
|
|
(eq np (cdr expr)))
|
|
|
|
(math-simplify-divide expr)
|
|
|
|
(let ((signs (math-possible-signs (cons '- (cdr expr)))))
|
|
|
|
(or (cond ((eq (car expr) 'calcFunc-eq)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and (eq signs 2) 1)
|
|
|
|
(and (memq signs '(1 4 5)) 0)))
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq (car expr) 'calcFunc-neq)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and (eq signs 2) 0)
|
|
|
|
(and (memq signs '(1 4 5)) 1)))
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq (car expr) 'calcFunc-lt)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and (eq signs 1) 1)
|
|
|
|
(and (memq signs '(2 4 6)) 0)))
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq (car expr) 'calcFunc-gt)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and (eq signs 4) 1)
|
|
|
|
(and (memq signs '(1 2 3)) 0)))
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq (car expr) 'calcFunc-leq)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and (eq signs 4) 0)
|
|
|
|
(and (memq signs '(1 2 3)) 1)))
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq (car expr) 'calcFunc-geq)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (and (eq signs 1) 0)
|
|
|
|
(and (memq signs '(2 4 6)) 1))))
|
2018-11-20 15:37:46 +00:00
|
|
|
expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-simplify-add-term (np dp minus lplain)
|
|
|
|
(or (math-vectorp (car np))
|
|
|
|
(let ((rplain t)
|
2018-11-20 15:37:46 +00:00
|
|
|
n d temp)
|
2001-11-06 18:59:06 +00:00
|
|
|
(while (memq (car-safe (setq n (car np) d (car dp))) '(+ -))
|
|
|
|
(setq rplain nil)
|
|
|
|
(if (setq temp (math-combine-sum n (nth 2 d)
|
|
|
|
minus (eq (car d) '+) t))
|
|
|
|
(if (or lplain (eq (math-looks-negp temp) minus))
|
|
|
|
(progn
|
|
|
|
(setcar np (setq n (if minus (math-neg temp) temp)))
|
|
|
|
(setcar (cdr (cdr d)) 0))
|
|
|
|
(progn
|
|
|
|
(setcar np 0)
|
|
|
|
(setcar (cdr (cdr d)) (setq n (if (eq (car d) '+)
|
|
|
|
(math-neg temp)
|
|
|
|
temp))))))
|
|
|
|
(setq dp (cdr d)))
|
|
|
|
(if (setq temp (math-combine-sum n d minus t t))
|
|
|
|
(if (or lplain
|
|
|
|
(and (not rplain)
|
|
|
|
(eq (math-looks-negp temp) minus)))
|
|
|
|
(progn
|
|
|
|
(setcar np (setq n (if minus (math-neg temp) temp)))
|
|
|
|
(setcar dp 0))
|
|
|
|
(progn
|
|
|
|
(setcar np 0)
|
2001-11-14 09:00:01 +00:00
|
|
|
(setcar dp (setq n (math-neg temp)))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-sin
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-sin (math-neg (nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (eq calc-angle-mode 'rad)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and n
|
|
|
|
(math-known-sin (car n) (nth 1 n) 120 0))))
|
|
|
|
(and (eq calc-angle-mode 'deg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-integer-plus (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and n
|
|
|
|
(math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt (math-sub 1 (math-sqr
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
|
|
|
|
(math-div (nth 1 (nth 1 expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-add 1 (math-sqr
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr)))))))
|
|
|
|
(let ((m (math-should-expand-trig (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and m (integerp (car m))
|
|
|
|
(let ((n (car m)) (a (nth 1 m)))
|
|
|
|
(list '+
|
|
|
|
(list '* (list 'calcFunc-sin (list '* (1- n) a))
|
|
|
|
(list 'calcFunc-cos a))
|
|
|
|
(list '* (list 'calcFunc-cos (list '* (1- n) a))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list 'calcFunc-sin a))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-cos
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (math-looks-negp (nth 1 expr))
|
|
|
|
(list 'calcFunc-cos (math-neg (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (eq calc-angle-mode 'rad)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and n
|
|
|
|
(math-known-sin (car n) (nth 1 n) 120 300))))
|
|
|
|
(and (eq calc-angle-mode 'deg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-integer-plus (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and n
|
|
|
|
(math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-div 1
|
|
|
|
(list 'calcFunc-sqrt
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-add 1
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sqr (nth 1 (nth 1 expr)))))))
|
|
|
|
(let ((m (math-should-expand-trig (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and m (integerp (car m))
|
|
|
|
(let ((n (car m)) (a (nth 1 m)))
|
|
|
|
(list '-
|
|
|
|
(list '* (list 'calcFunc-cos (list '* (1- n) a))
|
|
|
|
(list 'calcFunc-cos a))
|
|
|
|
(list '* (list 'calcFunc-sin (list '* (1- n) a))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list 'calcFunc-sin a))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2005-02-15 19:23:46 +00:00
|
|
|
(math-defsimplify calcFunc-sec
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(list 'calcFunc-sec (math-neg (nth 1 expr))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq calc-angle-mode 'rad)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and n
|
2019-07-10 17:24:58 +00:00
|
|
|
(let ((s (math-known-sin (car n) (nth 1 n) 120 300)))
|
|
|
|
(and s (math-div 1 s))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq calc-angle-mode 'deg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-integer-plus (nth 1 expr))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and n
|
2019-07-10 17:24:58 +00:00
|
|
|
(let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300)))
|
|
|
|
(and s (math-div 1 s))))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-div
|
2005-02-15 19:23:46 +00:00
|
|
|
1
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-div
|
2005-02-15 19:23:46 +00:00
|
|
|
1
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
|
2005-02-15 19:23:46 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-add 1
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sqr (nth 1 (nth 1 expr))))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-csc
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-csc (math-neg (nth 1 expr)))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq calc-angle-mode 'rad)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and n
|
2019-07-10 17:24:58 +00:00
|
|
|
(let ((s (math-known-sin (car n) (nth 1 n) 120 0)))
|
|
|
|
(and s (math-div 1 s))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq calc-angle-mode 'deg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-integer-plus (nth 1 expr))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and n
|
2019-07-10 17:24:58 +00:00
|
|
|
(let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0)))
|
|
|
|
(and s (math-div 1 s))))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
|
|
|
|
(math-div 1 (nth 1 (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-div
|
2005-02-15 19:23:46 +00:00
|
|
|
1
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt (math-sub 1 (math-sqr
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr)))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
|
2005-02-15 19:23:46 +00:00
|
|
|
(math-div (list 'calcFunc-sqrt
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-add 1 (math-sqr
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr)))))
|
|
|
|
(nth 1 (nth 1 expr))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-should-expand-trig (x &optional hyperbolic)
|
|
|
|
(let ((m (math-is-multiple x)))
|
|
|
|
(and math-living-dangerously
|
|
|
|
m (or (and (integerp (car m)) (> (car m) 1))
|
|
|
|
(equal (car m) '(frac 1 2)))
|
|
|
|
(or math-integrating
|
|
|
|
(memq (car-safe (nth 1 m))
|
|
|
|
(if hyperbolic
|
|
|
|
'(calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh)
|
|
|
|
'(calcFunc-arcsin calcFunc-arccos calcFunc-arctan)))
|
|
|
|
(and (eq (car-safe (nth 1 m)) 'calcFunc-ln)
|
|
|
|
(eq hyperbolic 'exp)))
|
2001-11-14 09:00:01 +00:00
|
|
|
m)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-known-sin (plus n mul off)
|
|
|
|
(setq n (math-mul n mul))
|
|
|
|
(and (math-num-integerp n)
|
|
|
|
(setq n (math-mod (math-add (math-trunc n) off) 240))
|
|
|
|
(if (>= n 120)
|
|
|
|
(and (setq n (math-known-sin plus (- n 120) 1 0))
|
|
|
|
(math-neg n))
|
|
|
|
(if (> n 60)
|
|
|
|
(setq n (- 120 n)))
|
|
|
|
(if (math-zerop plus)
|
|
|
|
(and (or calc-symbolic-mode
|
|
|
|
(memq n '(0 20 60)))
|
|
|
|
(cdr (assq n
|
|
|
|
'( (0 . 0)
|
|
|
|
(10 . (/ (calcFunc-sqrt
|
|
|
|
(- 2 (calcFunc-sqrt 3))) 2))
|
|
|
|
(12 . (/ (- (calcFunc-sqrt 5) 1) 4))
|
|
|
|
(15 . (/ (calcFunc-sqrt
|
|
|
|
(- 2 (calcFunc-sqrt 2))) 2))
|
|
|
|
(20 . (/ 1 2))
|
|
|
|
(24 . (* (^ (/ 1 2) (/ 3 2))
|
|
|
|
(calcFunc-sqrt
|
|
|
|
(- 5 (calcFunc-sqrt 5)))))
|
|
|
|
(30 . (/ (calcFunc-sqrt 2) 2))
|
|
|
|
(36 . (/ (+ (calcFunc-sqrt 5) 1) 4))
|
|
|
|
(40 . (/ (calcFunc-sqrt 3) 2))
|
|
|
|
(45 . (/ (calcFunc-sqrt
|
|
|
|
(+ 2 (calcFunc-sqrt 2))) 2))
|
|
|
|
(48 . (* (^ (/ 1 2) (/ 3 2))
|
|
|
|
(calcFunc-sqrt
|
|
|
|
(+ 5 (calcFunc-sqrt 5)))))
|
|
|
|
(50 . (/ (calcFunc-sqrt
|
|
|
|
(+ 2 (calcFunc-sqrt 3))) 2))
|
|
|
|
(60 . 1)))))
|
|
|
|
(cond ((eq n 0) (math-normalize (list 'calcFunc-sin plus)))
|
|
|
|
((eq n 60) (math-normalize (list 'calcFunc-cos plus)))
|
2001-11-14 09:00:01 +00:00
|
|
|
(t nil))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-tan
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-tan (math-neg (nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (eq calc-angle-mode 'rad)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and n
|
|
|
|
(math-known-tan (car n) (nth 1 n) 120))))
|
|
|
|
(and (eq calc-angle-mode 'deg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-integer-plus (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and n
|
|
|
|
(math-known-tan (car n) (nth 1 n) '(frac 2 3)))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
|
|
|
|
(math-div (nth 1 (nth 1 expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-div (list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))
|
|
|
|
(nth 1 (nth 1 expr))))
|
|
|
|
(let ((m (math-should-expand-trig (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and m
|
|
|
|
(if (equal (car m) '(frac 1 2))
|
|
|
|
(math-div (math-sub 1 (list 'calcFunc-cos (nth 1 m)))
|
|
|
|
(list 'calcFunc-sin (nth 1 m)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div (list 'calcFunc-sin (nth 1 expr))
|
|
|
|
(list 'calcFunc-cos (nth 1 expr))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2005-02-15 19:23:46 +00:00
|
|
|
(math-defsimplify calcFunc-cot
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-cot (math-neg (nth 1 expr)))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq calc-angle-mode 'rad)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and n
|
2019-07-10 17:24:58 +00:00
|
|
|
(let ((tn (math-known-tan (car n) (nth 1 n) 120)))
|
|
|
|
(and tn (math-div 1 tn))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq calc-angle-mode 'deg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(let ((n (math-integer-plus (nth 1 expr))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and n
|
2019-07-10 17:24:58 +00:00
|
|
|
(let ((tn (math-known-tan (car n) (nth 1 n) '(frac 2 3))))
|
|
|
|
(and tn (math-div 1 tn))))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
|
2005-02-15 19:23:46 +00:00
|
|
|
(math-div (list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))
|
|
|
|
(nth 1 (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
|
|
|
|
(math-div (nth 1 (nth 1 expr))
|
2005-02-15 19:23:46 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
|
|
|
|
(math-div 1 (nth 1 (nth 1 expr))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-known-tan (plus n mul)
|
|
|
|
(setq n (math-mul n mul))
|
|
|
|
(and (math-num-integerp n)
|
|
|
|
(setq n (math-mod (math-trunc n) 120))
|
|
|
|
(if (> n 60)
|
|
|
|
(and (setq n (math-known-tan plus (- 120 n) 1))
|
|
|
|
(math-neg n))
|
|
|
|
(if (math-zerop plus)
|
|
|
|
(and (or calc-symbolic-mode
|
|
|
|
(memq n '(0 30 60)))
|
|
|
|
(cdr (assq n '( (0 . 0)
|
|
|
|
(10 . (- 2 (calcFunc-sqrt 3)))
|
|
|
|
(12 . (calcFunc-sqrt
|
|
|
|
(- 1 (* (/ 2 5) (calcFunc-sqrt 5)))))
|
|
|
|
(15 . (- (calcFunc-sqrt 2) 1))
|
|
|
|
(20 . (/ (calcFunc-sqrt 3) 3))
|
|
|
|
(24 . (calcFunc-sqrt
|
|
|
|
(- 5 (* 2 (calcFunc-sqrt 5)))))
|
|
|
|
(30 . 1)
|
|
|
|
(36 . (calcFunc-sqrt
|
|
|
|
(+ 1 (* (/ 2 5) (calcFunc-sqrt 5)))))
|
|
|
|
(40 . (calcFunc-sqrt 3))
|
|
|
|
(45 . (+ (calcFunc-sqrt 2) 1))
|
|
|
|
(48 . (calcFunc-sqrt
|
|
|
|
(+ 5 (* 2 (calcFunc-sqrt 5)))))
|
|
|
|
(50 . (+ 2 (calcFunc-sqrt 3)))
|
|
|
|
(60 . (var uinf var-uinf))))))
|
|
|
|
(cond ((eq n 0) (math-normalize (list 'calcFunc-tan plus)))
|
|
|
|
((eq n 60) (math-normalize (list '/ -1
|
|
|
|
(list 'calcFunc-tan plus))))
|
2001-11-14 09:00:01 +00:00
|
|
|
(t nil))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-sinh
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-sinh (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
|
2001-11-06 18:59:06 +00:00
|
|
|
math-living-dangerously
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub (math-sqr (nth 1 (nth 1 expr))) 1)))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
|
2001-11-06 18:59:06 +00:00
|
|
|
math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div (nth 1 (nth 1 expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
|
|
|
|
(let ((m (math-should-expand-trig (nth 1 expr) t)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and m (integerp (car m))
|
|
|
|
(let ((n (car m)) (a (nth 1 m)))
|
|
|
|
(if (> n 1)
|
|
|
|
(list '+
|
|
|
|
(list '* (list 'calcFunc-sinh (list '* (1- n) a))
|
|
|
|
(list 'calcFunc-cosh a))
|
|
|
|
(list '* (list 'calcFunc-cosh (list '* (1- n) a))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list 'calcFunc-sinh a)))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-cosh
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (math-looks-negp (nth 1 expr))
|
|
|
|
(list 'calcFunc-cosh (math-neg (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
|
2001-11-06 18:59:06 +00:00
|
|
|
math-living-dangerously
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-add (math-sqr (nth 1 (nth 1 expr))) 1)))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
|
2001-11-06 18:59:06 +00:00
|
|
|
math-living-dangerously
|
|
|
|
(math-div 1
|
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
|
|
|
|
(let ((m (math-should-expand-trig (nth 1 expr) t)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and m (integerp (car m))
|
|
|
|
(let ((n (car m)) (a (nth 1 m)))
|
|
|
|
(if (> n 1)
|
|
|
|
(list '+
|
|
|
|
(list '* (list 'calcFunc-cosh (list '* (1- n) a))
|
|
|
|
(list 'calcFunc-cosh a))
|
|
|
|
(list '* (list 'calcFunc-sinh (list '* (1- n) a))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list 'calcFunc-sinh a)))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-tanh
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-tanh (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
|
2001-11-06 18:59:06 +00:00
|
|
|
math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div (nth 1 (nth 1 expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-add (math-sqr (nth 1 (nth 1 expr))) 1))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
|
2001-11-06 18:59:06 +00:00
|
|
|
math-living-dangerously
|
|
|
|
(math-div (list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub (math-sqr (nth 1 (nth 1 expr))) 1))
|
|
|
|
(nth 1 (nth 1 expr))))
|
|
|
|
(let ((m (math-should-expand-trig (nth 1 expr) t)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and m
|
|
|
|
(if (equal (car m) '(frac 1 2))
|
|
|
|
(math-div (math-sub (list 'calcFunc-cosh (nth 1 m)) 1)
|
|
|
|
(list 'calcFunc-sinh (nth 1 m)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div (list 'calcFunc-sinh (nth 1 expr))
|
|
|
|
(list 'calcFunc-cosh (nth 1 expr))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2005-02-15 19:23:46 +00:00
|
|
|
(math-defsimplify calcFunc-sech
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(list 'calcFunc-sech (math-neg (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-div
|
2005-02-15 19:23:46 +00:00
|
|
|
1
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-add (math-sqr (nth 1 (nth 1 expr))) 1))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div 1 (nth 1 (nth 1 expr))) 1)
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-csch
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-csch (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div 1 (nth 1 (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-div
|
2005-02-15 19:23:46 +00:00
|
|
|
1
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub (math-sqr (nth 1 (nth 1 expr))) 1))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
|
|
|
(math-div (list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))
|
|
|
|
(nth 1 (nth 1 expr))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-coth
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-coth (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
|
|
|
(math-div (list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-add (math-sqr (nth 1 (nth 1 expr))) 1))
|
|
|
|
(nth 1 (nth 1 expr))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div (nth 1 (nth 1 expr))
|
2005-02-15 19:23:46 +00:00
|
|
|
(list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-sub (math-sqr (nth 1 (nth 1 expr))) 1))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
|
2005-02-15 19:23:46 +00:00
|
|
|
math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-div 1 (nth 1 (nth 1 expr))))))
|
2005-02-15 19:23:46 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-defsimplify calcFunc-arcsin
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-arcsin (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (nth 1 expr) 1)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-quarter-circle t))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (equal (nth 1 expr) '(frac 1 2))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-div (math-half-circle t) 6))
|
|
|
|
(and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car-safe (nth 1 expr)) 'calcFunc-sin)
|
|
|
|
(nth 1 (nth 1 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car-safe (nth 1 expr)) 'calcFunc-cos)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-sub (math-quarter-circle t)
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-arccos
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (nth 1 expr) 0)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-quarter-circle t))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (nth 1 expr) -1)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-half-circle t))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (equal (nth 1 expr) '(frac 1 2))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-div (math-half-circle t) 3))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (equal (nth 1 expr) '(frac -1 2))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-div (math-mul (math-half-circle t) 2) 3))
|
|
|
|
(and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car-safe (nth 1 expr)) 'calcFunc-cos)
|
|
|
|
(nth 1 (nth 1 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car-safe (nth 1 expr)) 'calcFunc-sin)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-sub (math-quarter-circle t)
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-arctan
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-arctan (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (nth 1 expr) 1)
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-div (math-half-circle t) 4))
|
|
|
|
(and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(eq (car-safe (nth 1 expr)) 'calcFunc-tan)
|
|
|
|
(nth 1 (nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-arcsinh
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-arcsinh (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-sinh)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-realp (nth 1 (nth 1 expr))))
|
|
|
|
(nth 1 (nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-arccosh
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-cosh)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-realp (nth 1 (nth 1 expr))))
|
|
|
|
(nth 1 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-arctanh
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-arctanh (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-tanh)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-realp (nth 1 (nth 1 expr))))
|
|
|
|
(nth 1 (nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-simplify-sqrt expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defun math-simplify-sqrt (expr)
|
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'frac)
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-div (list 'calcFunc-sqrt
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-mul (nth 1 (nth 1 expr))
|
|
|
|
(nth 2 (nth 1 expr))))
|
|
|
|
(nth 2 (nth 1 expr))))
|
|
|
|
(let ((fac (if (math-objectp (nth 1 expr))
|
|
|
|
(math-squared-factor (nth 1 expr))
|
|
|
|
(math-common-constant-factor (nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and fac (not (eq fac 1))
|
|
|
|
(math-mul (math-normalize (list 'calcFunc-sqrt fac))
|
|
|
|
(math-normalize
|
|
|
|
(list 'calcFunc-sqrt
|
2009-09-19 21:11:40 +00:00
|
|
|
(math-cancel-common-factor
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 expr) fac))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) '-)
|
|
|
|
(math-equal-int (nth 1 (nth 1 expr)) 1)
|
|
|
|
(eq (car-safe (nth 2 (nth 1 expr))) '^)
|
|
|
|
(math-equal-int (nth 2 (nth 2 (nth 1 expr))) 2)
|
|
|
|
(or (and (eq (car-safe (nth 1 (nth 2 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
'calcFunc-sin)
|
|
|
|
(list 'calcFunc-cos
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 (nth 2 (nth 1 expr))))))
|
|
|
|
(and (eq (car-safe (nth 1 (nth 2 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
'calcFunc-cos)
|
|
|
|
(list 'calcFunc-sin
|
2009-09-19 21:11:40 +00:00
|
|
|
(nth 1 (nth 1 (nth 2
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 expr))))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) '-)
|
|
|
|
(math-equal-int (nth 2 (nth 1 expr)) 1)
|
|
|
|
(eq (car-safe (nth 1 (nth 1 expr))) '^)
|
|
|
|
(math-equal-int (nth 2 (nth 1 (nth 1 expr))) 2)
|
|
|
|
(and (eq (car-safe (nth 1 (nth 1 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
'calcFunc-cosh)
|
|
|
|
(list 'calcFunc-sinh
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 (nth 1 (nth 1 expr)))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) '+)
|
|
|
|
(let ((a (nth 1 (nth 1 expr)))
|
|
|
|
(b (nth 2 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (or (and (math-equal-int a 1)
|
2018-11-20 15:37:46 +00:00
|
|
|
(setq a b b (nth 1 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-equal-int b 1))
|
|
|
|
(eq (car-safe a) '^)
|
|
|
|
(math-equal-int (nth 2 a) 2)
|
|
|
|
(or (and (eq (car-safe (nth 1 a)) 'calcFunc-sinh)
|
|
|
|
(list 'calcFunc-cosh (nth 1 (nth 1 a))))
|
2005-02-15 19:23:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 a)) 'calcFunc-csch)
|
|
|
|
(list 'calcFunc-coth (nth 1 (nth 1 a))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (eq (car-safe (nth 1 a)) 'calcFunc-tan)
|
|
|
|
(list '/ 1 (list 'calcFunc-cos
|
2005-02-15 19:23:46 +00:00
|
|
|
(nth 1 (nth 1 a)))))
|
|
|
|
(and (eq (car-safe (nth 1 a)) 'calcFunc-cot)
|
|
|
|
(list '/ 1 (list 'calcFunc-sin
|
2001-11-06 18:59:06 +00:00
|
|
|
(nth 1 (nth 1 a)))))))))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) '^)
|
2001-11-06 18:59:06 +00:00
|
|
|
(list '^
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))
|
|
|
|
(math-div (nth 2 (nth 1 expr)) 2)))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-sqrt)
|
|
|
|
(list '^ (nth 1 (nth 1 expr)) (math-div 1 4)))
|
|
|
|
(and (memq (car-safe (nth 1 expr)) '(* /))
|
|
|
|
(list (car (nth 1 expr))
|
|
|
|
(list 'calcFunc-sqrt (nth 1 (nth 1 expr)))
|
|
|
|
(list 'calcFunc-sqrt (nth 2 (nth 1 expr)))))
|
|
|
|
(and (memq (car-safe (nth 1 expr)) '(+ -))
|
|
|
|
(not (math-any-floats (nth 1 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((f (calcFunc-factors (calcFunc-expand
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (math-vectorp f)
|
|
|
|
(or (> (length f) 2)
|
|
|
|
(> (nth 2 (nth 1 f)) 1))
|
|
|
|
(let ((out 1) (rest 1) (sums 1) fac pow)
|
|
|
|
(while (setq f (cdr f))
|
|
|
|
(setq fac (nth 1 (car f))
|
|
|
|
pow (nth 2 (car f)))
|
|
|
|
(if (> pow 1)
|
|
|
|
(setq out (math-mul out (math-pow
|
|
|
|
fac (/ pow 2)))
|
|
|
|
pow (% pow 2)))
|
|
|
|
(if (> pow 0)
|
|
|
|
(if (memq (car-safe fac) '(+ -))
|
|
|
|
(setq sums (math-mul-thru sums fac))
|
|
|
|
(setq rest (math-mul rest fac)))))
|
|
|
|
(and (not (and (eq out 1) (memq rest '(1 -1))))
|
|
|
|
(math-mul
|
|
|
|
out
|
|
|
|
(list 'calcFunc-sqrt
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-mul sums rest))))))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Rather than factoring x into primes, just check for the first ten primes.
|
|
|
|
(defun math-squared-factor (x)
|
|
|
|
(if (Math-integerp x)
|
|
|
|
(let ((prsqr '(4 9 25 49 121 169 289 361 529 841))
|
|
|
|
(fac 1)
|
|
|
|
res)
|
|
|
|
(while prsqr
|
|
|
|
(if (eq (cdr (setq res (math-idivmod x (car prsqr)))) 0)
|
|
|
|
(setq x (car res)
|
|
|
|
fac (math-mul fac (car prsqr)))
|
|
|
|
(setq prsqr (cdr prsqr))))
|
2001-11-14 09:00:01 +00:00
|
|
|
fac)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-exp
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-simplify-exp (nth 1 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-simplify-exp (x)
|
|
|
|
(or (and (eq (car-safe x) 'calcFunc-ln)
|
|
|
|
(nth 1 x))
|
|
|
|
(and math-living-dangerously
|
|
|
|
(or (and (eq (car-safe x) 'calcFunc-arcsinh)
|
|
|
|
(math-add (nth 1 x)
|
|
|
|
(list 'calcFunc-sqrt
|
|
|
|
(math-add (math-sqr (nth 1 x)) 1))))
|
|
|
|
(and (eq (car-safe x) 'calcFunc-arccosh)
|
|
|
|
(math-add (nth 1 x)
|
|
|
|
(list 'calcFunc-sqrt
|
|
|
|
(math-sub (math-sqr (nth 1 x)) 1))))
|
|
|
|
(and (eq (car-safe x) 'calcFunc-arctanh)
|
|
|
|
(math-div (list 'calcFunc-sqrt (math-add 1 (nth 1 x)))
|
|
|
|
(list 'calcFunc-sqrt (math-sub 1 (nth 1 x)))))
|
|
|
|
(let ((m (math-should-expand-trig x 'exp)))
|
|
|
|
(and m (integerp (car m))
|
|
|
|
(list '^ (list 'calcFunc-exp (nth 1 m)) (car m))))))
|
|
|
|
(and calc-symbolic-mode
|
|
|
|
(math-known-imagp x)
|
|
|
|
(let* ((ip (calcFunc-im x))
|
|
|
|
(n (math-linear-in ip '(var pi var-pi)))
|
|
|
|
s c)
|
|
|
|
(and n
|
|
|
|
(setq s (math-known-sin (car n) (nth 1 n) 120 0))
|
|
|
|
(setq c (math-known-sin (car n) (nth 1 n) 120 300))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list '+ c (list '* s '(var i var-i))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-ln
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-exp)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-realp (nth 1 (nth 1 expr))))
|
|
|
|
(nth 1 (nth 1 expr)))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) '^)
|
|
|
|
(equal (nth 1 (nth 1 expr)) '(var e var-e))
|
2001-11-06 18:59:06 +00:00
|
|
|
(or math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-realp (nth 2 (nth 1 expr))))
|
|
|
|
(nth 2 (nth 1 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and calc-symbolic-mode
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-negp (nth 1 expr))
|
|
|
|
(math-add (list 'calcFunc-ln (math-neg (nth 1 expr)))
|
2002-10-09 16:00:16 +00:00
|
|
|
'(* (var pi var-pi) (var i var-i))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and calc-symbolic-mode
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-imagp (nth 1 expr))
|
|
|
|
(let* ((ip (calcFunc-im (nth 1 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(ips (math-possible-signs ip)))
|
|
|
|
(or (and (memq ips '(4 6))
|
|
|
|
(math-add (list 'calcFunc-ln ip)
|
|
|
|
'(/ (* (var pi var-pi) (var i var-i)) 2)))
|
|
|
|
(and (memq ips '(1 3))
|
|
|
|
(math-sub (list 'calcFunc-ln (math-neg ip))
|
2001-11-14 09:00:01 +00:00
|
|
|
'(/ (* (var pi var-pi) (var i var-i)) 2))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify ^
|
|
|
|
(or (and math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (eq (car-safe (nth 1 expr)) '^)
|
2001-11-06 18:59:06 +00:00
|
|
|
(list '^
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))
|
|
|
|
(math-mul (nth 2 expr)
|
|
|
|
(nth 2 (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-sqrt)
|
2001-11-06 18:59:06 +00:00
|
|
|
(list '^
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr))
|
|
|
|
(math-div (nth 2 expr) 2)))
|
|
|
|
(and (memq (car-safe (nth 1 expr)) '(* /))
|
|
|
|
(list (car (nth 1 expr))
|
|
|
|
(list '^ (nth 1 (nth 1 expr))
|
|
|
|
(nth 2 expr))
|
|
|
|
(list '^ (nth 2 (nth 1 expr))
|
|
|
|
(nth 2 expr))))))
|
|
|
|
(and (math-equal-int (nth 1 expr) 10)
|
|
|
|
(eq (car-safe (nth 2 expr)) 'calcFunc-log10)
|
|
|
|
(nth 1 (nth 2 expr)))
|
|
|
|
(and (equal (nth 1 expr) '(var e var-e))
|
|
|
|
(math-simplify-exp (nth 2 expr)))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-exp)
|
2001-11-06 18:59:06 +00:00
|
|
|
(not math-integrating)
|
2018-11-20 15:37:46 +00:00
|
|
|
(list 'calcFunc-exp (math-mul (nth 1 (nth 1 expr))
|
|
|
|
(nth 2 expr))))
|
|
|
|
(and (equal (nth 1 expr) '(var i var-i))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-imaginary-i)
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-num-integerp (nth 2 expr))
|
|
|
|
(let ((x (math-mod (math-trunc (nth 2 expr)) 4)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(cond ((eq x 0) 1)
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq x 1) (nth 1 expr))
|
2001-11-06 18:59:06 +00:00
|
|
|
((eq x 2) -1)
|
2018-11-20 15:37:46 +00:00
|
|
|
((eq x 3) (math-neg (nth 1 expr))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and math-integrating
|
2018-11-20 15:37:46 +00:00
|
|
|
(integerp (nth 2 expr))
|
|
|
|
(>= (nth 2 expr) 2)
|
|
|
|
(or (and (eq (car-safe (nth 1 expr)) 'calcFunc-cos)
|
|
|
|
(math-mul (math-pow (nth 1 expr)
|
|
|
|
(- (nth 2 expr) 2))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-sub 1
|
|
|
|
(math-sqr
|
|
|
|
(list 'calcFunc-sin
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr)))))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-cosh)
|
|
|
|
(math-mul (math-pow (nth 1 expr)
|
|
|
|
(- (nth 2 expr) 2))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-add 1
|
|
|
|
(math-sqr
|
|
|
|
(list 'calcFunc-sinh
|
2018-11-20 15:37:46 +00:00
|
|
|
(nth 1 (nth 1 expr)))))))))
|
|
|
|
(and (eq (car-safe (nth 2 expr)) 'frac)
|
|
|
|
(Math-ratp (nth 1 expr))
|
|
|
|
(Math-posp (nth 1 expr))
|
|
|
|
(if (equal (nth 2 expr) '(frac 1 2))
|
|
|
|
(list 'calcFunc-sqrt (nth 1 expr))
|
|
|
|
(let ((flr (math-floor (nth 2 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and (not (Math-zerop flr))
|
2018-11-20 15:37:46 +00:00
|
|
|
(list '* (list '^ (nth 1 expr) flr)
|
|
|
|
(list '^ (nth 1 expr)
|
|
|
|
(math-sub (nth 2 expr) flr)))))))
|
|
|
|
(and (eq (math-quarter-integer (nth 2 expr)) 2)
|
|
|
|
(let ((temp (math-simplify-sqrt expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and temp
|
2018-11-20 15:37:46 +00:00
|
|
|
(list '^ temp (math-mul (nth 2 expr) 2)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-log10
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (eq (car-safe (nth 1 expr)) '^)
|
|
|
|
(math-equal-int (nth 1 (nth 1 expr)) 10)
|
2001-11-06 18:59:06 +00:00
|
|
|
(or math-living-dangerously
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-known-realp (nth 2 (nth 1 expr))))
|
|
|
|
(nth 2 (nth 1 expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(math-defsimplify calcFunc-erf
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-neg (list 'calcFunc-erf (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-conj)
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-conj
|
2018-11-20 15:37:46 +00:00
|
|
|
(list 'calcFunc-erf (nth 1 (nth 1 expr)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(math-defsimplify calcFunc-erfc
|
2018-11-20 15:37:46 +00:00
|
|
|
(or (and (math-looks-negp (nth 1 expr))
|
|
|
|
(math-sub 2 (list 'calcFunc-erfc (math-neg (nth 1 expr)))))
|
|
|
|
(and (eq (car-safe (nth 1 expr)) 'calcFunc-conj)
|
2009-09-19 21:11:40 +00:00
|
|
|
(list 'calcFunc-conj
|
2018-11-20 15:37:46 +00:00
|
|
|
(list 'calcFunc-erfc (nth 1 (nth 1 expr)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun math-linear-in (expr term &optional always)
|
|
|
|
(if (math-expr-contains expr term)
|
|
|
|
(let* ((calc-prefer-frac t)
|
|
|
|
(p (math-is-polynomial expr term 1)))
|
|
|
|
(and (cdr p)
|
|
|
|
p))
|
2001-11-14 09:00:01 +00:00
|
|
|
(and always (list expr 0))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-multiple-of (expr term)
|
|
|
|
(let ((p (math-linear-in expr term)))
|
|
|
|
(and p
|
|
|
|
(math-zerop (car p))
|
2001-11-14 09:00:01 +00:00
|
|
|
(nth 1 p))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-14 09:00:01 +00:00
|
|
|
; not perfect, but it'll do
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-integer-plus (expr)
|
|
|
|
(cond ((Math-integerp expr)
|
|
|
|
(list 0 expr))
|
|
|
|
((and (memq (car expr) '(+ -))
|
|
|
|
(Math-integerp (nth 1 expr)))
|
|
|
|
(list (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr)))
|
|
|
|
(nth 1 expr)))
|
|
|
|
((and (memq (car expr) '(+ -))
|
|
|
|
(Math-integerp (nth 2 expr)))
|
|
|
|
(list (nth 1 expr)
|
|
|
|
(if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr)))))
|
2001-11-14 09:00:01 +00:00
|
|
|
(t nil)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-is-linear (expr &optional always)
|
|
|
|
(let ((offset nil)
|
|
|
|
(coef nil))
|
|
|
|
(if (eq (car-safe expr) '+)
|
|
|
|
(if (Math-objectp (nth 1 expr))
|
|
|
|
(setq offset (nth 1 expr)
|
|
|
|
expr (nth 2 expr))
|
|
|
|
(if (Math-objectp (nth 2 expr))
|
|
|
|
(setq offset (nth 2 expr)
|
|
|
|
expr (nth 1 expr))))
|
|
|
|
(if (eq (car-safe expr) '-)
|
|
|
|
(if (Math-objectp (nth 1 expr))
|
|
|
|
(setq offset (nth 1 expr)
|
|
|
|
expr (math-neg (nth 2 expr)))
|
|
|
|
(if (Math-objectp (nth 2 expr))
|
|
|
|
(setq offset (math-neg (nth 2 expr))
|
|
|
|
expr (nth 1 expr))))))
|
|
|
|
(setq coef (math-is-multiple expr always))
|
|
|
|
(if offset
|
|
|
|
(list offset (or (car coef) 1) (or (nth 1 coef) expr))
|
|
|
|
(if coef
|
2001-11-14 09:00:01 +00:00
|
|
|
(cons 0 coef)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-is-multiple (expr &optional always)
|
|
|
|
(or (if (eq (car-safe expr) '*)
|
|
|
|
(if (Math-objectp (nth 1 expr))
|
|
|
|
(list (nth 1 expr) (nth 2 expr)))
|
|
|
|
(if (eq (car-safe expr) '/)
|
|
|
|
(if (and (Math-objectp (nth 1 expr))
|
|
|
|
(not (math-equal-int (nth 1 expr) 1)))
|
|
|
|
(list (nth 1 expr) (math-div 1 (nth 2 expr)))
|
|
|
|
(if (Math-objectp (nth 2 expr))
|
|
|
|
(list (math-div 1 (nth 2 expr)) (nth 1 expr))
|
|
|
|
(let ((res (math-is-multiple (nth 1 expr))))
|
|
|
|
(if res
|
|
|
|
(list (car res)
|
|
|
|
(math-div (nth 2 (nth 1 expr)) (nth 2 expr)))
|
|
|
|
(setq res (math-is-multiple (nth 2 expr)))
|
|
|
|
(if res
|
|
|
|
(list (math-div 1 (car res))
|
|
|
|
(math-div (nth 1 expr)
|
|
|
|
(nth 2 (nth 2 expr)))))))))
|
|
|
|
(if (eq (car-safe expr) 'neg)
|
|
|
|
(list -1 (nth 1 expr)))))
|
|
|
|
(if (Math-objvecp expr)
|
|
|
|
(and (eq always 1)
|
|
|
|
(list expr 1))
|
2003-02-04 13:24:35 +00:00
|
|
|
(and always
|
2001-11-14 09:00:01 +00:00
|
|
|
(list 1 expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-lin (expr &optional var)
|
|
|
|
(if var
|
|
|
|
(let ((res (math-linear-in expr var t)))
|
|
|
|
(or res (math-reject-arg expr "Linear term expected"))
|
|
|
|
(list 'vec (car res) (nth 1 res) var))
|
|
|
|
(let ((res (math-is-linear expr t)))
|
|
|
|
(or res (math-reject-arg expr "Linear term expected"))
|
2001-11-14 09:00:01 +00:00
|
|
|
(cons 'vec res))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-linnt (expr &optional var)
|
|
|
|
(if var
|
|
|
|
(let ((res (math-linear-in expr var)))
|
|
|
|
(or res (math-reject-arg expr "Linear term expected"))
|
|
|
|
(list 'vec (car res) (nth 1 res) var))
|
|
|
|
(let ((res (math-is-linear expr)))
|
|
|
|
(or res (math-reject-arg expr "Linear term expected"))
|
2001-11-14 09:00:01 +00:00
|
|
|
(cons 'vec res))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-islin (expr &optional var)
|
|
|
|
(if (and (Math-objvecp expr) (not var))
|
|
|
|
0
|
|
|
|
(calcFunc-lin expr var)
|
2001-11-14 09:00:01 +00:00
|
|
|
1))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-islinnt (expr &optional var)
|
|
|
|
(if (Math-objvecp expr)
|
|
|
|
0
|
|
|
|
(calcFunc-linnt expr var)
|
2001-11-14 09:00:01 +00:00
|
|
|
1))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Simple operations on expressions.
|
|
|
|
|
2002-07-09 09:57:34 +00:00
|
|
|
;;; Return number of occurrences of thing in expr, or nil if none.
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-expr-contains-count (expr thing)
|
|
|
|
(cond ((equal expr thing) 1)
|
|
|
|
((Math-primp expr) nil)
|
|
|
|
(t
|
|
|
|
(let ((num 0))
|
|
|
|
(while (setq expr (cdr expr))
|
|
|
|
(setq num (+ num (or (math-expr-contains-count
|
|
|
|
(car expr) thing) 0))))
|
|
|
|
(and (> num 0)
|
2001-11-14 09:00:01 +00:00
|
|
|
num)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-expr-contains (expr thing)
|
|
|
|
(cond ((equal expr thing) 1)
|
|
|
|
((Math-primp expr) nil)
|
|
|
|
(t
|
|
|
|
(while (and (setq expr (cdr expr))
|
|
|
|
(not (math-expr-contains (car expr) thing))))
|
2001-11-14 09:00:01 +00:00
|
|
|
expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Return non-nil if any variable of thing occurs in expr.
|
|
|
|
(defun math-expr-depends (expr thing)
|
|
|
|
(if (Math-primp thing)
|
|
|
|
(and (eq (car-safe thing) 'var)
|
|
|
|
(math-expr-contains expr thing))
|
|
|
|
(while (and (setq thing (cdr thing))
|
|
|
|
(not (math-expr-depends expr (car thing)))))
|
2001-11-14 09:00:01 +00:00
|
|
|
thing))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Substitute all occurrences of old for new in expr (non-destructive).
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
|
|
|
|
;; The variables math-expr-subst-old and math-expr-subst-new are local
|
|
|
|
;; for math-expr-subst, but used by math-expr-subst-rec.
|
|
|
|
(defvar math-expr-subst-old)
|
|
|
|
(defvar math-expr-subst-new)
|
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defun math-expr-subst (expr old new)
|
|
|
|
(let ((math-expr-subst-old old)
|
|
|
|
(math-expr-subst-new new))
|
|
|
|
(math-expr-subst-rec expr)))
|
2001-11-14 09:00:01 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defalias 'calcFunc-subst #'math-expr-subst)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-expr-subst-rec (expr)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(cond ((equal expr math-expr-subst-old) math-expr-subst-new)
|
2001-11-06 18:59:06 +00:00
|
|
|
((Math-primp expr) expr)
|
|
|
|
((memq (car expr) '(calcFunc-deriv
|
|
|
|
calcFunc-tderiv))
|
|
|
|
(if (= (length expr) 2)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(if (equal (nth 1 expr) math-expr-subst-old)
|
|
|
|
(append expr (list math-expr-subst-new))
|
2001-11-06 18:59:06 +00:00
|
|
|
expr)
|
|
|
|
(list (car expr) (nth 1 expr)
|
|
|
|
(math-expr-subst-rec (nth 2 expr)))))
|
|
|
|
(t
|
|
|
|
(cons (car expr)
|
2018-11-20 15:37:46 +00:00
|
|
|
(mapcar #'math-expr-subst-rec (cdr expr))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Various measures of the size of an expression.
|
|
|
|
(defun math-expr-weight (expr)
|
|
|
|
(if (Math-primp expr)
|
|
|
|
1
|
|
|
|
(let ((w 1))
|
|
|
|
(while (setq expr (cdr expr))
|
|
|
|
(setq w (+ w (math-expr-weight (car expr)))))
|
2001-11-14 09:00:01 +00:00
|
|
|
w)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-expr-height (expr)
|
|
|
|
(if (Math-primp expr)
|
|
|
|
0
|
|
|
|
(let ((h 0))
|
|
|
|
(while (setq expr (cdr expr))
|
|
|
|
(setq h (max h (math-expr-height (car expr)))))
|
2001-11-14 09:00:01 +00:00
|
|
|
(1+ h))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Polynomial operations (to support the integrator and solve-for).
|
|
|
|
|
|
|
|
(defun calcFunc-collect (expr base)
|
|
|
|
(let ((p (math-is-polynomial expr base 50 t)))
|
|
|
|
(if (cdr p)
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-build-polynomial-expr (mapcar #'math-normalize p) base)
|
2008-11-03 02:16:35 +00:00
|
|
|
(car p))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; If expr is of the form "a + bx + cx^2 + ...", return the list (a b c ...),
|
2009-09-19 21:11:40 +00:00
|
|
|
;;; else return nil if not in polynomial form. If "loose" (math-is-poly-loose),
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
;;; coefficients may contain x, e.g., sin(x) + cos(x) x^2 is a loose polynomial in x.
|
|
|
|
|
2009-09-19 21:11:40 +00:00
|
|
|
;; These variables are local to math-is-polynomial, but are used by
|
|
|
|
;; math-is-poly-rec.
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(defvar math-is-poly-degree)
|
|
|
|
(defvar math-is-poly-loose)
|
2010-10-14 03:14:38 +00:00
|
|
|
(defvar math-var)
|
2019-12-26 18:37:10 +00:00
|
|
|
(defvar math-poly-base-variable)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defun math-is-polynomial (expr var &optional degree loose)
|
|
|
|
(let* ((math-poly-base-variable (if loose
|
|
|
|
(if (eq loose 'gen) var '(var XXX XXX))
|
2001-11-06 18:59:06 +00:00
|
|
|
math-poly-base-variable))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-var var)
|
|
|
|
(math-is-poly-loose loose)
|
|
|
|
(math-is-poly-degree degree)
|
2001-11-06 18:59:06 +00:00
|
|
|
(poly (math-is-poly-rec expr math-poly-neg-powers)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(and (or (null degree)
|
|
|
|
(<= (length poly) (1+ degree)))
|
2001-11-14 09:00:01 +00:00
|
|
|
poly)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-is-poly-rec (expr negpow)
|
|
|
|
(math-poly-simplify
|
2010-10-14 03:14:38 +00:00
|
|
|
(or (cond ((or (equal expr math-var)
|
2001-11-06 18:59:06 +00:00
|
|
|
(eq (car-safe expr) '^))
|
|
|
|
(let ((pow 1)
|
|
|
|
(expr expr))
|
2010-10-14 03:14:38 +00:00
|
|
|
(or (equal expr math-var)
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq pow (nth 2 expr)
|
|
|
|
expr (nth 1 expr)))
|
|
|
|
(or (eq math-poly-mult-powers 1)
|
|
|
|
(setq pow (let ((m (math-is-multiple pow 1)))
|
|
|
|
(and (eq (car-safe (car m)) 'cplx)
|
|
|
|
(Math-zerop (nth 1 (car m)))
|
|
|
|
(setq m (list (nth 2 (car m))
|
|
|
|
(math-mul (nth 1 m)
|
|
|
|
'(var i var-i)))))
|
|
|
|
(and (if math-poly-mult-powers
|
|
|
|
(equal math-poly-mult-powers
|
|
|
|
(nth 1 m))
|
|
|
|
(setq math-poly-mult-powers (nth 1 m)))
|
2010-10-14 03:14:38 +00:00
|
|
|
(or (equal expr math-var)
|
2001-11-06 18:59:06 +00:00
|
|
|
(eq math-poly-mult-powers 1))
|
|
|
|
(car m)))))
|
|
|
|
(if (consp pow)
|
|
|
|
(progn
|
|
|
|
(setq pow (math-to-simple-fraction pow))
|
|
|
|
(and (eq (car-safe pow) 'frac)
|
|
|
|
math-poly-frac-powers
|
2010-10-14 03:14:38 +00:00
|
|
|
(equal expr math-var)
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq math-poly-frac-powers
|
|
|
|
(calcFunc-lcm math-poly-frac-powers
|
|
|
|
(nth 2 pow))))))
|
|
|
|
(or (memq math-poly-frac-powers '(1 nil))
|
|
|
|
(setq pow (math-mul pow math-poly-frac-powers)))
|
|
|
|
(if (integerp pow)
|
|
|
|
(if (and (= pow 1)
|
2010-10-14 03:14:38 +00:00
|
|
|
(equal expr math-var))
|
2001-11-06 18:59:06 +00:00
|
|
|
(list 0 1)
|
|
|
|
(if (natnump pow)
|
2010-10-14 03:14:38 +00:00
|
|
|
(let ((p1 (if (equal expr math-var)
|
2001-11-06 18:59:06 +00:00
|
|
|
(list 0 1)
|
|
|
|
(math-is-poly-rec expr nil)))
|
|
|
|
(n pow)
|
|
|
|
(accum (list 1)))
|
|
|
|
(and p1
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(or (null math-is-poly-degree)
|
|
|
|
(<= (* (1- (length p1)) n) math-is-poly-degree))
|
2001-11-06 18:59:06 +00:00
|
|
|
(progn
|
|
|
|
(while (>= n 1)
|
|
|
|
(setq accum (math-poly-mul accum p1)
|
|
|
|
n (1- n)))
|
|
|
|
accum)))
|
|
|
|
(and negpow
|
|
|
|
(math-is-poly-rec expr nil)
|
|
|
|
(setq math-poly-neg-powers
|
|
|
|
(cons (math-pow expr (- pow))
|
|
|
|
math-poly-neg-powers))
|
|
|
|
(list (list '^ expr pow))))))))
|
|
|
|
((Math-objectp expr)
|
|
|
|
(list expr))
|
|
|
|
((memq (car expr) '(+ -))
|
|
|
|
(let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
|
|
|
|
(and p1
|
|
|
|
(let ((p2 (math-is-poly-rec (nth 2 expr) negpow)))
|
|
|
|
(and p2
|
|
|
|
(math-poly-mix p1 1 p2
|
|
|
|
(if (eq (car expr) '+) 1 -1)))))))
|
|
|
|
((eq (car expr) 'neg)
|
2018-11-20 15:37:46 +00:00
|
|
|
(mapcar #'math-neg (math-is-poly-rec (nth 1 expr) negpow)))
|
2001-11-06 18:59:06 +00:00
|
|
|
((eq (car expr) '*)
|
|
|
|
(let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
|
|
|
|
(and p1
|
|
|
|
(let ((p2 (math-is-poly-rec (nth 2 expr) negpow)))
|
|
|
|
(and p2
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(or (null math-is-poly-degree)
|
2009-09-19 21:11:40 +00:00
|
|
|
(<= (- (+ (length p1) (length p2)) 2)
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
math-is-poly-degree))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-poly-mul p1 p2))))))
|
|
|
|
((eq (car expr) '/)
|
2010-10-14 03:14:38 +00:00
|
|
|
(and (or (not (math-poly-depends (nth 2 expr) math-var))
|
2001-11-06 18:59:06 +00:00
|
|
|
(and negpow
|
|
|
|
(math-is-poly-rec (nth 2 expr) nil)
|
|
|
|
(setq math-poly-neg-powers
|
|
|
|
(cons (nth 2 expr) math-poly-neg-powers))))
|
|
|
|
(not (Math-zerop (nth 2 expr)))
|
|
|
|
(let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
|
Don't quote lambdas with 'function' in calc/*.el
* lisp/calc/calc-aent.el (calc-do-quick-calc)
(calc-do-calc-eval, math-build-parse-table):
* lisp/calc/calc-alg.el (math-polynomial-base):
* lisp/calc/calc-alg.el (math-is-poly-rec):
* lisp/calc/calc-arith.el (calcFunc-scf):
* lisp/calc/calc-arith.el (math-ceiling, math-round):
* lisp/calc/calc-arith.el (math-trunc-fancy, math-floor-fancy):
* lisp/calc/calc-ext.el (calc-init-extensions, calc-reset)
(calc-refresh-top, calc-z-prefix-help, calc-binary-op-fancy)
(calc-unary-op-fancy):
* lisp/calc/calc-forms.el (math-make-mod):
* lisp/calc/calc-frac.el (calcFunc-frac):
* lisp/calc/calc-funcs.el (calcFunc-euler):
* lisp/calc/calc-help.el (calc-full-help):
* lisp/calc/calc-lang.el (c, pascal, fortran, tex, latex, eqn)
(yacas, maxima, giac, math, maple):
* lisp/calc/calc-macs.el (calc-wrapper, calc-slow-wrapper):
* lisp/calc/calc-map.el (calc-get-operator, calcFunc-mapeqr)
(calcFunc-reducea, calcFunc-rreducea, calcFunc-reduced)
(calcFunc-rreduced, calcFunc-outer):
* lisp/calc/calc-misc.el (another-calc, calc-do-handle-whys):
* lisp/calc/calc-mode.el (calc-save-modes):
* lisp/calc/calc-mtx.el (math-col-matrix, math-mul-mat-vec):
* lisp/calc/calc-poly.el (math-sort-terms, math-poly-div-list)
(math-mul-list, math-sort-poly-base-list)
(math-partial-fractions):
* lisp/calc/calc-prog.el (calc-user-define-formula):
* lisp/calc/calc-rewr.el (math-rewrite, math-compile-patterns)
(math-compile-rewrites, math-parse-schedule)
(math-rwcomp-pattern):
* lisp/calc/calc-store.el (calc-var-name-map, calc-let)
(calc-permanent-variable, calc-insert-variables):
* lisp/calc/calc-stuff.el (calc-flush-caches, calcFunc-pclean)
(calcFunc-pfrac):
* lisp/calc/calc-units.el (math-build-units-table)
(math-decompose-units):
* lisp/calc/calc-vec.el (calcFunc-mrow, math-mat-col)
(calcFunc-mcol, math-mat-less-col, math-mimic-ident):
* lisp/calc/calc-yank.el (calc-edit):
* lisp/calc/calc.el
(calc-mode-var-list-restore-default-values)
(calc-mode-var-list-restore-saved-values, calc-mode, calc-quit):
* lisp/calc/calccomp.el (math-compose-expr)
(math-compose-matrix, math-vector-to-string): Don't quote lambdas with
'function'.
2020-11-17 01:51:30 +00:00
|
|
|
(mapcar (lambda (x) (math-div x (nth 2 expr)))
|
2001-11-06 18:59:06 +00:00
|
|
|
p1))))
|
|
|
|
((and (eq (car expr) 'calcFunc-exp)
|
2010-10-14 03:14:38 +00:00
|
|
|
(equal math-var '(var e var-e)))
|
|
|
|
(math-is-poly-rec (list '^ math-var (nth 1 expr)) negpow))
|
2001-11-06 18:59:06 +00:00
|
|
|
((and (eq (car expr) 'calcFunc-sqrt)
|
|
|
|
math-poly-frac-powers)
|
|
|
|
(math-is-poly-rec (list '^ (nth 1 expr) '(frac 1 2)) negpow))
|
|
|
|
(t nil))
|
2010-10-14 03:14:38 +00:00
|
|
|
(and (or (not (math-poly-depends expr math-var))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
math-is-poly-loose)
|
2001-11-06 18:59:06 +00:00
|
|
|
(not (eq (car expr) 'vec))
|
2001-11-14 09:00:01 +00:00
|
|
|
(list expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Check if expr is a polynomial in var; if so, return its degree.
|
|
|
|
(defun math-polynomial-p (expr var)
|
|
|
|
(cond ((equal expr var) 1)
|
|
|
|
((Math-primp expr) 0)
|
|
|
|
((memq (car expr) '(+ -))
|
|
|
|
(let ((p1 (math-polynomial-p (nth 1 expr) var))
|
|
|
|
p2)
|
|
|
|
(and p1 (setq p2 (math-polynomial-p (nth 2 expr) var))
|
|
|
|
(max p1 p2))))
|
|
|
|
((eq (car expr) '*)
|
|
|
|
(let ((p1 (math-polynomial-p (nth 1 expr) var))
|
|
|
|
p2)
|
|
|
|
(and p1 (setq p2 (math-polynomial-p (nth 2 expr) var))
|
|
|
|
(+ p1 p2))))
|
|
|
|
((eq (car expr) 'neg)
|
|
|
|
(math-polynomial-p (nth 1 expr) var))
|
|
|
|
((and (eq (car expr) '/)
|
|
|
|
(not (math-poly-depends (nth 2 expr) var)))
|
|
|
|
(math-polynomial-p (nth 1 expr) var))
|
|
|
|
((and (eq (car expr) '^)
|
|
|
|
(natnump (nth 2 expr)))
|
|
|
|
(let ((p1 (math-polynomial-p (nth 1 expr) var)))
|
|
|
|
(and p1 (* p1 (nth 2 expr)))))
|
|
|
|
((math-poly-depends expr var) nil)
|
2001-11-14 09:00:01 +00:00
|
|
|
(t 0)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-poly-depends (expr var)
|
|
|
|
(if math-poly-base-variable
|
|
|
|
(math-expr-contains expr math-poly-base-variable)
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-expr-depends expr var)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
;; The variables math-poly-base-const-ok and math-poly-base-pred are
|
|
|
|
;; local to math-polynomial-base, but are used by math-polynomial-base-rec.
|
|
|
|
(defvar math-poly-base-const-ok)
|
|
|
|
(defvar math-poly-base-pred)
|
|
|
|
|
2018-11-20 15:37:46 +00:00
|
|
|
(defun math-polynomial-base (top-expr &optional pred)
|
|
|
|
"Find the variable (or sub-expression) which is the base of polynomial expr."
|
|
|
|
(let ((math-poly-base-pred
|
Don't quote lambdas with 'function' in calc/*.el
* lisp/calc/calc-aent.el (calc-do-quick-calc)
(calc-do-calc-eval, math-build-parse-table):
* lisp/calc/calc-alg.el (math-polynomial-base):
* lisp/calc/calc-alg.el (math-is-poly-rec):
* lisp/calc/calc-arith.el (calcFunc-scf):
* lisp/calc/calc-arith.el (math-ceiling, math-round):
* lisp/calc/calc-arith.el (math-trunc-fancy, math-floor-fancy):
* lisp/calc/calc-ext.el (calc-init-extensions, calc-reset)
(calc-refresh-top, calc-z-prefix-help, calc-binary-op-fancy)
(calc-unary-op-fancy):
* lisp/calc/calc-forms.el (math-make-mod):
* lisp/calc/calc-frac.el (calcFunc-frac):
* lisp/calc/calc-funcs.el (calcFunc-euler):
* lisp/calc/calc-help.el (calc-full-help):
* lisp/calc/calc-lang.el (c, pascal, fortran, tex, latex, eqn)
(yacas, maxima, giac, math, maple):
* lisp/calc/calc-macs.el (calc-wrapper, calc-slow-wrapper):
* lisp/calc/calc-map.el (calc-get-operator, calcFunc-mapeqr)
(calcFunc-reducea, calcFunc-rreducea, calcFunc-reduced)
(calcFunc-rreduced, calcFunc-outer):
* lisp/calc/calc-misc.el (another-calc, calc-do-handle-whys):
* lisp/calc/calc-mode.el (calc-save-modes):
* lisp/calc/calc-mtx.el (math-col-matrix, math-mul-mat-vec):
* lisp/calc/calc-poly.el (math-sort-terms, math-poly-div-list)
(math-mul-list, math-sort-poly-base-list)
(math-partial-fractions):
* lisp/calc/calc-prog.el (calc-user-define-formula):
* lisp/calc/calc-rewr.el (math-rewrite, math-compile-patterns)
(math-compile-rewrites, math-parse-schedule)
(math-rwcomp-pattern):
* lisp/calc/calc-store.el (calc-var-name-map, calc-let)
(calc-permanent-variable, calc-insert-variables):
* lisp/calc/calc-stuff.el (calc-flush-caches, calcFunc-pclean)
(calcFunc-pfrac):
* lisp/calc/calc-units.el (math-build-units-table)
(math-decompose-units):
* lisp/calc/calc-vec.el (calcFunc-mrow, math-mat-col)
(calcFunc-mcol, math-mat-less-col, math-mimic-ident):
* lisp/calc/calc-yank.el (calc-edit):
* lisp/calc/calc.el
(calc-mode-var-list-restore-default-values)
(calc-mode-var-list-restore-saved-values, calc-mode, calc-quit):
* lisp/calc/calccomp.el (math-compose-expr)
(math-compose-matrix, math-vector-to-string): Don't quote lambdas with
'function'.
2020-11-17 01:51:30 +00:00
|
|
|
(or pred (lambda (base)
|
|
|
|
(math-polynomial-p
|
|
|
|
top-expr base)))))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(or (let ((math-poly-base-const-ok nil))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-polynomial-base-rec top-expr))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(let ((math-poly-base-const-ok t))
|
2018-11-20 15:37:46 +00:00
|
|
|
(math-polynomial-base-rec top-expr)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-polynomial-base-rec (mpb-expr)
|
|
|
|
(and (not (Math-objvecp mpb-expr))
|
|
|
|
(or (and (memq (car mpb-expr) '(+ - *))
|
|
|
|
(or (math-polynomial-base-rec (nth 1 mpb-expr))
|
|
|
|
(math-polynomial-base-rec (nth 2 mpb-expr))))
|
|
|
|
(and (memq (car mpb-expr) '(/ neg))
|
|
|
|
(math-polynomial-base-rec (nth 1 mpb-expr)))
|
|
|
|
(and (eq (car mpb-expr) '^)
|
|
|
|
(math-polynomial-base-rec (nth 1 mpb-expr)))
|
|
|
|
(and (eq (car mpb-expr) 'calcFunc-exp)
|
|
|
|
(math-polynomial-base-rec '(var e var-e)))
|
(calc-poly-div): Made calc-poly-div-remainder a local variable.
(math-eval-rules-cache, math-eval-rules-cache-other): Declared them.
(math-top-only): New variable
(math-simplify, math-simplify-step): Replace variable top-only by
declared variable math-top-only.
(math-simplify-expr): Declared it.
Replaced argument expr in all calls of math-defsimplify by
math-simplify-expr.
(math-simplify-plus, math-simplify-times, math-simplify-divide)
(math-simplify-divisor, math-simplify-one-divisor)
(math-simplify-mod, math-simplify-ineq, math-simplify-sqrt)
(math-simplify-pow): Replaced variable expr by declared variable
math-simplify-expr.
(math-simplify-divisor): Removed local variables temp and op.
(math-simplify-one-divisor): Made temp and op local variables.
(math-simplify-divisor-nover, math-simplify-divisor-dover): New
variables.
(math-simplify-divisor, math-simplify-one-divisor): Use declared
variables.
(math-expr-subst-new, math-expr-subst-old): New variables.
(math-expr-subst, math-expr-subst-rec): Use declared variables.
(math-is-poly-degree, math-is-poly-loose): New variables.
(math-is-polynomial, math-is-poly-rec): Use declared variables.
(math-poly-base-const-ok, math-poly-base-pred): New variables.
(math-polynomial-base, math-polynomial-base-rec): Use declared
variables.
2004-11-17 19:21:57 +00:00
|
|
|
(and (or math-poly-base-const-ok (math-expr-contains-vars mpb-expr))
|
|
|
|
(funcall math-poly-base-pred mpb-expr)
|
2001-11-14 09:00:01 +00:00
|
|
|
mpb-expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-expr-contains-vars (expr)
|
2018-11-20 15:37:46 +00:00
|
|
|
"Return non-nil if expr refers to any variables."
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (eq (car-safe expr) 'var)
|
|
|
|
(and (not (Math-primp expr))
|
|
|
|
(progn
|
|
|
|
(while (and (setq expr (cdr expr))
|
|
|
|
(not (math-expr-contains-vars (car expr)))))
|
2001-11-14 09:00:01 +00:00
|
|
|
expr))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-poly-simplify (p)
|
2018-11-20 15:37:46 +00:00
|
|
|
"Simplify a polynomial in list form by stripping off high-end zeros.
|
|
|
|
This always leaves the constant part, i.e., nil->nil and non-nil->non-nil."
|
2001-11-06 18:59:06 +00:00
|
|
|
(and p
|
|
|
|
(if (Math-zerop (nth (1- (length p)) p))
|
|
|
|
(let ((pp (copy-sequence p)))
|
|
|
|
(while (and (cdr pp)
|
|
|
|
(Math-zerop (nth (1- (length pp)) pp)))
|
|
|
|
(setcdr (nthcdr (- (length pp) 2) pp) nil))
|
|
|
|
pp)
|
2001-11-14 09:00:01 +00:00
|
|
|
p)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Compute ac*a + bc*b for polynomials in list form a, b and
|
|
|
|
;;; coefficients ac, bc. Result may be unsimplified.
|
|
|
|
(defun math-poly-mix (a ac b bc)
|
|
|
|
(and (or a b)
|
|
|
|
(cons (math-add (math-mul (or (car a) 0) ac)
|
|
|
|
(math-mul (or (car b) 0) bc))
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-poly-mix (cdr a) ac (cdr b) bc))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-poly-zerop (a)
|
|
|
|
(or (null a)
|
2001-11-14 09:00:01 +00:00
|
|
|
(and (null (cdr a)) (Math-zerop (car a)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-poly-mul (a b)
|
2018-11-20 15:37:46 +00:00
|
|
|
"Multiply two polynomials in list form."
|
2001-11-06 18:59:06 +00:00
|
|
|
(and a b
|
|
|
|
(math-poly-mix b (car a)
|
2001-11-14 09:00:01 +00:00
|
|
|
(math-poly-mul (cdr a) (cons 0 b)) 1)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-build-polynomial-expr (p var)
|
2018-11-20 15:37:46 +00:00
|
|
|
"Build an expression from a polynomial list."
|
2001-11-06 18:59:06 +00:00
|
|
|
(if p
|
|
|
|
(if (Math-numberp var)
|
|
|
|
(math-with-extra-prec 1
|
|
|
|
(let* ((rp (reverse p))
|
|
|
|
(accum (car rp)))
|
|
|
|
(while (setq rp (cdr rp))
|
|
|
|
(setq accum (math-add (car rp) (math-mul accum var))))
|
|
|
|
accum))
|
|
|
|
(let* ((rp (reverse p))
|
|
|
|
(n (1- (length rp)))
|
2018-11-20 15:37:46 +00:00
|
|
|
(accum (math-mul (car rp) (math-pow var n))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(while (setq rp (cdr rp))
|
|
|
|
(setq n (1- n))
|
|
|
|
(or (math-zerop (car rp))
|
|
|
|
(setq accum (list (if (math-looks-negp (car rp)) '- '+)
|
|
|
|
accum
|
|
|
|
(math-mul (if (math-looks-negp (car rp))
|
|
|
|
(math-neg (car rp))
|
|
|
|
(car rp))
|
|
|
|
(math-pow var n))))))
|
|
|
|
accum))
|
2001-11-14 09:00:01 +00:00
|
|
|
0))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun math-to-simple-fraction (f)
|
|
|
|
(or (and (eq (car-safe f) 'float)
|
|
|
|
(or (and (>= (nth 2 f) 0)
|
|
|
|
(math-scale-int (nth 1 f) (nth 2 f)))
|
|
|
|
(and (integerp (nth 1 f))
|
|
|
|
(> (nth 1 f) -1000)
|
|
|
|
(< (nth 1 f) 1000)
|
|
|
|
(math-make-frac (nth 1 f)
|
|
|
|
(math-scale-int 1 (- (nth 2 f)))))))
|
2001-11-14 09:00:01 +00:00
|
|
|
f))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2004-11-30 17:00:26 +00:00
|
|
|
(provide 'calc-alg)
|
|
|
|
|
2001-11-14 09:00:01 +00:00
|
|
|
;;; calc-alg.el ends here
|