1 ;;; calc-rules.el --- rules for simplifying algebraic expressions in Calc
3 ;; Copyright (C) 1990-1993, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 (defun calc-compile-rule-set (name rules
)
34 (message "Preparing rule set %s..." name
)
35 (math-read-plain-expr rules t
)
36 (message "Preparing rule set %s...done" name
)))
38 (defun calc-CommuteRules ()
40 (calc-compile-rule-set
43 select(plain(a + b)) := select(plain(b + a)),
44 select(plain(a - b)) := select(plain((-b) + a)),
45 select(plain((1/a) * b)) := select(b / a),
46 select(plain(a * b)) := select(b * a),
47 select((1/a) / b) := select((1/b) / a),
48 select(a / b) := select((1/b) * a),
49 select((a^b) ^ c) := select((a^c) ^ b),
50 select(log(a, b)) := select(1 / log(b, a)),
51 select(plain(a && b)) := select(b && a),
52 select(plain(a || b)) := select(b || a),
53 select(plain(a = b)) := select(b = a),
54 select(plain(a != b)) := select(b != a),
55 select(a < b) := select(b > a),
56 select(a > b) := select(b < a),
57 select(a <= b) := select(b >= a),
58 select(a >= b) := select(b <= a) ]"))
60 (defun calc-JumpRules ()
62 (calc-compile-rule-set
65 plain(select(x) = y) := 0 = select(-x) + y,
66 plain(a + select(x) = y) := a = select(-x) + y,
67 plain(a - select(x) = y) := a = select(x) + y,
68 plain(select(x) + a = y) := a = select(-x) + y,
69 plain(a * select(x) = y) := a = y / select(x),
70 plain(a / select(x) = y) := a = select(x) * y,
71 plain(select(x) / a = y) := 1/a = y / select(x),
72 plain(a ^ select(2) = y) := a = select(sqrt(y)),
73 plain(a ^ select(x) = y) := a = y ^ select(1/x),
74 plain(select(x) ^ a = y) := a = log(y, select(x)),
75 plain(log(a, select(x)) = y) := a = select(x) ^ y,
76 plain(log(select(x), a) = y) := a = select(x) ^ (1/y),
77 plain(y = select(x)) := y - select(x) = 0,
78 plain(y = a + select(x)) := y - select(x) = a,
79 plain(y = a - select(x)) := y + select(x) = a,
80 plain(y = select(x) + a) := y - select(x) = a,
81 plain(y = a * select(x)) := y / select(x) = a,
82 plain(y = a / select(x)) := y * select(x) = a,
83 plain(y = select(x) / a) := y / select(x) = 1/a,
84 plain(y = a ^ select(2)) := select(sqrt(y)) = a,
85 plain(y = a ^ select(x)) := y ^ select(1/x) = a,
86 plain(y = select(x) ^ a) := log(y, select(x)) = a,
87 plain(y = log(a, select(x))) := select(x) ^ y = a,
88 plain(y = log(select(x), a)) := select(x) ^ (1/y) = a ]"))
90 (defun calc-DistribRules ()
92 (calc-compile-rule-set
95 x * select(a + b) := x*select(a) + x*b,
96 x * select(sum(a,b,c,d)) := sum(x*select(a),b,c,d),
97 x / select(a + b) := 1 / (select(a)/x + b/x),
98 select(a + b) / x := select(a)/x + b/x,
99 sum(select(a),b,c,d) / x := sum(select(a)/x,b,c,d),
100 x ^ select(a + b) := x^select(a) * x^b,
101 x ^ select(sum(a,b,c,d)) := prod(x^select(a),b,c,d),
102 x ^ select(a * b) := (x^a)^select(b),
103 x ^ select(a / b) := (x^a)^select(1/b),
104 select(a + b) ^ n := select(x)
105 :: integer(n) :: n >= 2
106 :: let(x, expandpow(a+b,n))
107 :: quote(matches(x,y+z)),
108 select(a + b) ^ x := a*select(a+b)^(x-1) + b*select(a+b)^(x-1),
109 select(a * b) ^ x := a^x * select(b)^x,
110 select(prod(a,b,c,d)) ^ x := prod(select(a)^x,b,c,d),
111 select(a / b) ^ x := select(a)^x / b^x,
112 select(- a) ^ x := (-1)^x * select(a)^x,
113 plain(-select(a + b)) := select(-a) - b,
114 plain(-select(sum(a,b,c,d))) := sum(select(-a),b,c,d),
115 plain(-select(a * b)) := select(-a) * b,
116 plain(-select(a / b)) := select(-a) / b,
117 sqrt(select(a * b)) := sqrt(select(a)) * sqrt(b),
118 sqrt(select(prod(a,b,c,d))) := prod(sqrt(select(a)),b,c,d),
119 sqrt(select(a / b)) := sqrt(select(a)) / sqrt(b),
120 sqrt(select(- a)) := sqrt(-1) sqrt(select(a)),
121 exp(select(a + b)) := exp(select(a)) / exp(-b) :: negative(b),
122 exp(select(a + b)) := exp(select(a)) * exp(b),
123 exp(select(sum(a,b,c,d))) := prod(exp(select(a)),b,c,d),
124 exp(select(a * b)) := exp(select(a)) ^ b :: constant(b),
125 exp(select(a * b)) := exp(select(a)) ^ b,
126 exp(select(a / b)) := exp(select(a)) ^ (1/b),
127 ln(select(a * b)) := ln(select(a)) + ln(b),
128 ln(select(prod(a,b,c,d))) := sum(ln(select(a)),b,c,d),
129 ln(select(a / b)) := ln(select(a)) - ln(b),
130 ln(select(a ^ b)) := ln(select(a)) * b,
131 log10(select(a * b)) := log10(select(a)) + log10(b),
132 log10(select(prod(a,b,c,d))) := sum(log10(select(a)),b,c,d),
133 log10(select(a / b)) := log10(select(a)) - log10(b),
134 log10(select(a ^ b)) := log10(select(a)) * b,
135 log(select(a * b), x) := log(select(a), x) + log(b,x),
136 log(select(prod(a,b,c,d)),x) := sum(log(select(a),x),b,c,d),
137 log(select(a / b), x) := log(select(a), x) - log(b,x),
138 log(select(a ^ b), x) := log(select(a), x) * b,
139 log(a, select(b)) := ln(a) / select(ln(b)),
140 sin(select(a + b)) := sin(select(a)) cos(b) + cos(a) sin(b),
141 sin(select(2 a)) := 2 sin(select(a)) cos(a),
142 sin(select(n a)) := 2sin((n-1) select(a)) cos(a) - sin((n-2) a)
143 :: integer(n) :: n > 2,
144 cos(select(a + b)) := cos(select(a)) cos(b) - sin(a) sin(b),
145 cos(select(2 a)) := 2 cos(select(a))^2 - 1,
146 cos(select(n a)) := 2cos((n-1) select(a)) cos(a) - cos((n-2) a)
147 :: integer(n) :: n > 2,
148 tan(select(a + b)) := (tan(select(a)) + tan(b)) /
150 tan(select(2 a)) := 2 tan(select(a)) / (1 - tan(a)^2),
151 tan(select(n a)) := (tan((n-1) select(a)) + tan(a)) /
152 (1 - tan((n-1) a) tan(a))
153 :: integer(n) :: n > 2,
154 cot(select(a + b)) := (cot(select(a)) cot(b) - 1) /
156 sinh(select(a + b)) := sinh(select(a)) cosh(b) + cosh(a) sinh(b),
157 cosh(select(a + b)) := cosh(select(a)) cosh(b) + sinh(a) sinh(b),
158 tanh(select(a + b)) := (tanh(select(a)) + tanh(b)) /
159 (1 + tanh(a) tanh(b)),
160 coth(select(a + b)) := (coth(select(a)) coth(b) + 1) /
162 x && select(a || b) := (x && select(a)) || (x && b),
163 select(a || b) && x := (select(a) && x) || (b && x),
164 ! select(a && b) := (!a) || (!b),
165 ! select(a || b) := (!a) && (!b) ]"))
167 (defun calc-MergeRules ()
169 (calc-compile-rule-set
172 (x*opt(a)) + select(x*b) := x * (a + select(b)),
173 (x*opt(a)) - select(x*b) := x * (a - select(b)),
174 sum(select(x)*a,b,c,d) := x * sum(select(a),b,c,d),
175 (a/x) + select(b/x) := (a + select(b)) / x,
176 (a/x) - select(b/x) := (a - select(b)) / x,
177 sum(a/select(x),b,c,d) := sum(select(a),b,c,d) / x,
178 (a/opt(b)) + select(c/d) := ((select(a)*d) + (b*c)) / (b*d),
179 (a/opt(b)) - select(c/d) := ((select(a)*d) - (b*c)) / (b*d),
180 (x^opt(a)) * select(x^b) := x ^ (a + select(b)),
181 (x^opt(a)) / select(x^b) := x ^ (a - select(b)),
182 select(x^a) / (x^opt(b)) := x ^ (select(a) - b),
183 prod(select(x)^a,b,c,d) := x ^ sum(select(a),b,c,d),
184 select(x^a) / (x^opt(b)) := x ^ (select(a) - b),
185 (a^x) * select(b^x) := select((a * b) ^x),
186 (a^x) / select(b^x) := select((b / b) ^ x),
187 select(a^x) / (b^x) := select((a / b) ^ x),
188 prod(a^select(x),b,c,d) := select(prod(a,b,c,d) ^ x),
189 (a^x) * select(b^y) := select((a * b^(y-x)) ^x),
190 (a^x) / select(b^y) := select((b / b^(y-x)) ^ x),
191 select(a^x) / (b^y) := select((a / b^(y-x)) ^ x),
192 select(x^a) ^ b := x ^ select(a * b),
193 (x^a) ^ select(b) := x ^ select(a * b),
194 select(sqrt(a)) ^ b := select(a ^ (b / 2)),
195 sqrt(a) ^ select(b) := select(a ^ (b / 2)),
196 sqrt(select(a) ^ b) := select(a ^ (b / 2)),
197 sqrt(a ^ select(b)) := select(a ^ (b / 2)),
198 sqrt(a) * select(sqrt(b)) := select(sqrt(a * b)),
199 sqrt(a) / select(sqrt(b)) := select(sqrt(a / b)),
200 select(sqrt(a)) / sqrt(b) := select(sqrt(a / b)),
201 prod(select(sqrt(a)),b,c,d) := select(sqrt(prod(a,b,c,d))),
202 exp(a) * select(exp(b)) := select(exp(a + b)),
203 exp(a) / select(exp(b)) := select(exp(a - b)),
204 select(exp(a)) / exp(b) := select(exp(a - b)),
205 prod(select(exp(a)),b,c,d) := select(exp(sum(a,b,c,d))),
206 select(exp(a)) ^ b := select(exp(a * b)),
207 exp(a) ^ select(b) := select(exp(a * b)),
208 ln(a) + select(ln(b)) := select(ln(a * b)),
209 ln(a) - select(ln(b)) := select(ln(a / b)),
210 select(ln(a)) - ln(b) := select(ln(a / b)),
211 sum(select(ln(a)),b,c,d) := select(ln(prod(a,b,c,d))),
212 b * select(ln(a)) := select(ln(a ^ b)),
213 select(b) * ln(a) := select(ln(a ^ b)),
214 select(ln(a)) / ln(b) := select(log(a, b)),
215 ln(a) / select(ln(b)) := select(log(a, b)),
216 select(ln(a)) / b := select(ln(a ^ (1/b))),
217 ln(a) / select(b) := select(ln(a ^ (1/b))),
218 log10(a) + select(log10(b)) := select(log10(a * b)),
219 log10(a) - select(log10(b)) := select(log10(a / b)),
220 select(log10(a)) - log10(b) := select(log10(a / b)),
221 sum(select(log10(a)),b,c,d) := select(log10(prod(a,b,c,d))),
222 b * select(log10(a)) := select(log10(a ^ b)),
223 select(b) * log10(a) := select(log10(a ^ b)),
224 select(log10(a)) / log10(b) := select(log(a, b)),
225 log10(a) / select(log10(b)) := select(log(a, b)),
226 select(log10(a)) / b := select(log10(a ^ (1/b))),
227 log10(a) / select(b) := select(log10(a ^ (1/b))),
228 log(a,x) + select(log(b,x)) := select(log(a * b,x)),
229 log(a,x) - select(log(b,x)) := select(log(a / b,x)),
230 select(log(a,x)) - log(b,x) := select(log(a / b,x)),
231 sum(select(log(a,x)),b,c,d) := select(log(prod(a,b,c,d),x)),
232 b * select(log(a,x)) := select(log(a ^ b,x)),
233 select(b) * log(a,x) := select(log(a ^ b,x)),
234 select(log(a,x)) / log(b,x) := select(log(a, b)),
235 log(a,x) / select(log(b,x)) := select(log(a, b)),
236 select(log(a,x)) / b := select(log(a ^ (1/b),x)),
237 log(a,x) / select(b) := select(log(a ^ (1/b),x)),
238 select(x && a) || (x && opt(b)) := x && (select(a) || b) ]"))
240 (defun calc-NegateRules ()
242 (calc-compile-rule-set
245 a + select(x) := a - select(-x),
246 a - select(x) := a + select(-x),
247 sum(select(x),b,c,d) := -sum(select(-x),b,c,d),
248 a * select(x) := -a * select(-x),
249 a / select(x) := -a / select(-x),
250 select(x) / a := -select(-x) / a,
251 prod(select(x),b,c,d) := (-1)^(d-c+1) * prod(select(-x),b,c,d),
252 select(x) ^ n := select(-x) ^ a :: integer(n) :: n%2 = 0,
253 select(x) ^ n := -(select(-x) ^ a) :: integer(n) :: n%2 = 1,
254 select(x) ^ a := (-select(-x)) ^ a,
255 a ^ select(x) := (1 / a)^select(-x),
256 abs(select(x)) := abs(select(-x)),
257 i sqrt(select(x)) := -sqrt(select(-x)),
258 sqrt(select(x)) := i sqrt(select(-x)),
259 re(select(x)) := -re(select(-x)),
260 im(select(x)) := -im(select(-x)),
261 conj(select(x)) := -conj(select(-x)),
262 trunc(select(x)) := -trunc(select(-x)),
263 round(select(x)) := -round(select(-x)),
264 floor(select(x)) := -ceil(select(-x)),
265 ceil(select(x)) := -floor(select(-x)),
266 ftrunc(select(x)) := -ftrunc(select(-x)),
267 fround(select(x)) := -fround(select(-x)),
268 ffloor(select(x)) := -fceil(select(-x)),
269 fceil(select(x)) := -ffloor(select(-x)),
270 exp(select(x)) := 1 / exp(select(-x)),
271 sin(select(x)) := -sin(select(-x)),
272 cos(select(x)) := cos(select(-x)),
273 tan(select(x)) := -tan(select(-x)),
274 sec(select(x)) := sec(select(-x)),
275 csc(select(x)) := -csc(select(-x)),
276 cot(select(x)) := -cot(select(-x)),
277 arcsin(select(x)) := -arcsin(select(-x)),
278 arccos(select(x)) := 4 arctan(1) - arccos(select(-x)),
279 arctan(select(x)) := -arctan(select(-x)),
280 sinh(select(x)) := -sinh(select(-x)),
281 cosh(select(x)) := cosh(select(-x)),
282 tanh(select(x)) := -tanh(select(-x)),
283 sech(select(x)) := sech(select(-x)),
284 csch(select(x)) := -csch(select(-x)),
285 coth(select(x)) := -coth(select(-x)),
286 arcsinh(select(x)) := -arcsinh(select(-x)),
287 arctanh(select(x)) := -arctanh(select(-x)),
288 select(x) = a := select(-x) = -a,
289 select(x) != a := select(-x) != -a,
290 select(x) < a := select(-x) > -a,
291 select(x) > a := select(-x) < -a,
292 select(x) <= a := select(-x) >= -a,
293 select(x) >= a := select(-x) <= -a,
294 a < select(x) := -a > select(-x),
295 a > select(x) := -a < select(-x),
296 a <= select(x) := -a >= select(-x),
297 a >= select(x) := -a <= select(-x),
298 select(x) := -select(-x) ]"))
300 (defun calc-InvertRules ()
302 (calc-compile-rule-set
305 a * select(x) := a / select(1/x),
306 a / select(x) := a * select(1/x),
307 select(x) / a := 1 / (select(1/x) a),
308 prod(select(x),b,c,d) := 1 / prod(select(1/x),b,c,d),
309 abs(select(x)) := 1 / abs(select(1/x)),
310 sqrt(select(x)) := 1 / sqrt(select(1/x)),
311 ln(select(x)) := -ln(select(1/x)),
312 log10(select(x)) := -log10(select(1/x)),
313 log(select(x), a) := -log(select(1/x), a),
314 log(a, select(x)) := -log(a, select(1/x)),
315 arctan(select(x)) := simplify(2 arctan(1))-arctan(select(1/x)),
316 select(x) = a := select(1/x) = 1/a,
317 select(x) != a := select(1/x) != 1/a,
318 select(x) < a := select(1/x) > 1/a,
319 select(x) > a := select(1/x) < 1/a,
320 select(x) <= a := select(1/x) >= 1/a,
321 select(x) >= a := select(1/x) <= 1/a,
322 a < select(x) := 1/a > select(1/x),
323 a > select(x) := 1/a < select(1/x),
324 a <= select(x) := 1/a >= select(1/x),
325 a >= select(x) := 1/a <= select(1/x),
326 select(x) := 1 / select(1/x) ]"))
329 (defun calc-FactorRules ()
331 (calc-compile-rule-set
333 thecoefs(x, [z, a+b, c]) := thefactors(x, [d x + d a/c, (c/d) x + (b/d)])
334 :: z = a b/c :: let(d := pgcd(pcont(c), pcont(b))),
335 thecoefs(x, [z, a, c]) := thefactors(x, [(r x + a/(2 r))^2])
336 :: z = (a/2)^2/c :: let(r := esimplify(sqrt(c)))
337 :: !matches(r, sqrt(rr)),
338 thecoefs(x, [z, 0, c]) := thefactors(x, [rc x + rz, rc x - rz])
340 :: let(rz := esimplify(sqrt(-z))) :: !matches(rz, sqrt(rzz))
341 :: let(rc := esimplify(sqrt(c))) :: !matches(rc, sqrt(rcc)),
342 thecoefs(x, [z, 0, c]) := thefactors(x, [rz + rc x, rz - rc x])
344 :: let(rz := esimplify(sqrt(z))) :: !matches(rz, sqrt(rzz))
345 :: let(rc := esimplify(sqrt(-c))) :: !matches(rc, sqrt(rcc))
347 ;;(setq var-FactorRules 'calc-FactorRules)
350 (defun calc-IntegAfterRules ()
352 (calc-compile-rule-set
354 opt(a) ln(x) + opt(b) ln(y) := 2 a esimplify(arctanh(x-1))
355 :: a + b = 0 :: nrat(x + y) = 2 || nrat(x - y) = 2,
356 a * (b + c) := a b + a c :: constant(a)
359 ;;(setq var-IntegAfterRules 'calc-IntegAfterRules)
362 (defun calc-FitRules ()
364 (calc-compile-rule-set
372 x^y := exp(y ln(x)) :: !istrue(constant(y)),
374 fitinv(x y) := fitinv(x) fitinv(y),
375 exp(a) exp(b) := exp(a + b),
376 a exp(b) := exp(ln(a) + b) :: !hasfitvars(a),
377 fitinv(exp(a)) := exp(-a),
378 ln(a b) := ln(a) + ln(b),
379 ln(fitinv(a)) := -ln(a),
380 log10(a b) := log10(a) + log10(b),
381 log10(fitinv(a)) := -log10(a),
382 log(a,b) := ln(a)/ln(b),
384 a*(b+c) := a*b + a*c,
385 (a+b)^n := x :: integer(n) :: n >= 2
386 :: let(x, expandpow(a+b,n))
387 :: quote(matches(x,y+z)),
390 fitmodel(y = x) := fitmodel(0, y - x),
391 fitmodel(y, x+c) := fitmodel(y-c, x) :: !hasfitparams(c),
392 fitmodel(y, x c) := fitmodel(y/c, x) :: !hasfitparams(c),
393 fitmodel(y, x/(c opt(d))) := fitmodel(y c, x/d) :: !hasfitparams(c),
394 fitmodel(y, apply(f,[x])) := fitmodel(yy, x)
397 solve(apply(f,[FTemp()]) = y,
399 fitmodel(y, apply(f,[x,c])) := fitmodel(yy, x)
402 solve(apply(f,[FTemp(),c]) = y,
404 fitmodel(y, apply(f,[c,x])) := fitmodel(yy, x)
407 solve(apply(f,[c,FTemp()]) = y,
411 fitmodel(y, x) := fitsystem(y, [], [], fitpart(1,1,x)),
412 fitpart(a,b,plain(x + y)) := fitpart(a,b,x) + fitpart(a,b,y),
413 fitpart(a,b,plain(x - y)) := fitpart(a,b,x) + fitpart(-a,b,y),
414 fitpart(a,b,plain(-x)) := fitpart(-a,b,x),
415 fitpart(a,b,x opt(c)) := fitpart(a,x b,c) :: !hasfitvars(x),
416 fitpart(a,x opt(b),c) := fitpart(x a,b,c) :: !hasfitparams(x),
417 fitpart(a,x y + x opt(z),c) := fitpart(a,x*(y+z),c),
418 fitpart(a,b,c) := fitpart2(a,b,c),
421 fitpart2(a1,b1,x) + fitpart2(a2,b2,x) := fitpart(1, a1 b1 + a2 b2, x),
422 fitpart2(a1,x,c1) + fitpart2(a2,x,c2) := fitpart2(1, x, a1 c1 + a2 c2),
426 exp(x + ln(y)) := y exp(x),
428 ln(x) + ln(y) := ln(x y),
429 ln(x) - ln(y) := ln(x/y),
430 x*y + x*z := x*(y+z),
431 fitsystem(y, xv, pv, fitpart2(a,fitparam(b),c) + opt(d))
432 := fitsystem(y, rcons(xv, a c),
433 rcons(pv, fitdummy(b) = fitparam(b)), d)
435 fitsystem(y, xv, pv, fitpart2(a,b,c) + opt(d))
436 := fitsystem(y, rcons(xv, a c),
437 rcons(pv, fitdummy(vlen(pv)+1) = b), d),
438 fitsystem(y, xv, pv, 0) := fitsystem(y, xv, cons(fvh,fvt))
440 :: let(cons(fvh,fvt),
441 solve(pv, table(fitparam(j), j, 1,
443 fitparam(n) = x := x ]"))
445 (provide 'calc-rules
)
447 ;;; calc-rules.el ends here