Newsgroups: comp.sources.misc
From: daveg@synaptics.com (David Gillespie)
Subject:  v24i074:  gnucalc - GNU Emacs Calculator, v2.00, Part26/56
Message-ID: <1991Oct31.072817.18326@sparky.imd.sterling.com>
X-Md4-Signature: 84c2fdc7279e8cdd578cc5b3fc54658f
Date: Thu, 31 Oct 1991 07:28:17 GMT
Approved: kent@sparky.imd.sterling.com

Submitted-by: daveg@synaptics.com (David Gillespie)
Posting-number: Volume 24, Issue 74
Archive-name: gnucalc/part26
Environment: Emacs
Supersedes: gmcalc: Volume 13, Issue 27-45

---- Cut Here and unpack ----
#!/bin/sh
# do not concatenate these parts, unpack them in order with /bin/sh
# file calc-rewr.el continued
#
if test ! -r _shar_seq_.tmp; then
	echo 'Please unpack part 1 first!'
	exit 1
fi
(read Scheck
 if test "$Scheck" != 26; then
	echo Please unpack part "$Scheck" next!
	exit 1
 else
	exit 0
 fi
) < _shar_seq_.tmp || exit 1
if test ! -f _shar_wnt_.tmp; then
	echo 'x - still skipping calc-rewr.el'
else
echo 'x - continuing file calc-rewr.el'
sed 's/^X//' << 'SHAR_EOF' >> 'calc-rewr.el' &&
X    (if (and (eq (car-safe varval) 'vec)
X	     (not (memq (car-safe old) '(nil schedule + -)))
X	     rules)
X	(progn
X	  (setcdr varval (cons (list 'calcFunc-assign
X				     (if (math-rwcomp-no-vars old)
X					 old
X				       (list 'calcFunc-quote old))
X				     new)
X			       (cdr varval)))
X	  (setcdr rules (cons (list (vector nil old)
X				    (list (list 'same 0 1)
X					  (list 'done new nil))
X				    nil nil)
X			      (cdr rules))))))
)
X
X
X
X
SHAR_EOF
echo 'File calc-rewr.el is complete' &&
chmod 0644 calc-rewr.el ||
echo 'restore of calc-rewr.el failed'
Wc_c="`wc -c < 'calc-rewr.el'`"
test 69210 -eq "$Wc_c" ||
	echo 'calc-rewr.el: original size 69210, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= calc-rules.el ==============
if test -f 'calc-rules.el' -a X"$1" != X"-c"; then
	echo 'x - skipping calc-rules.el (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting calc-rules.el (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'calc-rules.el' &&
;; Calculator for GNU Emacs, part II [calc-rules.el]
;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
X
;; This file is part of GNU Emacs.
X
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.
X
;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.
X
X
X
;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)
X
(require 'calc-macs)
X
(defun calc-Need-calc-rules () nil)
X
X
(defun calc-compile-rule-set (name rules)
X  (prog2
X   (message "Preparing rule set %s..." name)
X   (math-read-plain-expr rules t)
X   (message "Preparing rule set %s...done" name))
)
X
(defun calc-CommuteRules ()
X  "CommuteRules"
X  (calc-compile-rule-set
X   "CommuteRules" "[
iterations(1),
select(plain(a + b))		:=  select(plain(b + a)),
select(plain(a - b))		:=  select(plain((-b) + a)),
select(plain((1/a) * b))	:=  select(b / a),
select(plain(a * b))		:=  select(b * a),
select((1/a) / b)		:=  select((1/b) / a),
select(a / b)			:=  select((1/b) * a),
select((a^b) ^ c)		:=  select((a^c) ^ b),
select(log(a, b))		:=  select(1 / log(b, a)),
select(plain(a && b))		:=  select(b && a),
select(plain(a || b))		:=  select(b || a),
select(plain(a = b))		:=  select(b = a),
select(plain(a != b))		:=  select(b != a),
select(a < b)			:=  select(b > a),
select(a > b)			:=  select(b < a),
select(a <= b)			:=  select(b >= a),
select(a >= b)			:=  select(b <= a) ]")
)
X
(defun calc-JumpRules ()
X  "JumpRules"
X  (calc-compile-rule-set
X   "JumpRules" "[
iterations(1),
plain(select(x) = y)		:=  0 = select(-x) + y,
plain(a + select(x) = y)	:=  a = select(-x) + y,
plain(a - select(x) = y)	:=  a = select(x) + y,
plain(select(x) + a = y)	:=  a = select(-x) + y,
plain(a * select(x) = y)	:=  a = y / select(x),
plain(a / select(x) = y)	:=  a = select(x) * y,
plain(select(x) / a = y)	:=  1/a = y / select(x),
plain(a ^ select(2) = y)	:=  a = select(sqrt(y)),
plain(a ^ select(x) = y)	:=  a = y ^ select(1/x),
plain(select(x) ^ a = y)	:=  a = log(y, select(x)),
plain(log(a, select(x)) = y)	:=  a = select(x) ^ y,
plain(log(select(x), a) = y)	:=  a = select(x) ^ (1/y),
plain(y = select(x))		:=  y - select(x) = 0,
plain(y = a + select(x))	:=  y - select(x) = a,
plain(y = a - select(x))	:=  y + select(x) = a,
plain(y = select(x) + a)	:=  y - select(x) = a,
plain(y = a * select(x))	:=  y / select(x) = a,
plain(y = a / select(x))	:=  y * select(x) = a,
plain(y = select(x) / a)	:=  y / select(x) = 1/a,
plain(y = a ^ select(2))	:=  select(sqrt(y)) = a,
plain(y = a ^ select(x))	:=  y ^ select(1/x) = a,
plain(y = select(x) ^ a)	:=  log(y, select(x)) = a,
plain(y = log(a, select(x)))	:=  select(x) ^ y = a,
plain(y = log(select(x), a))	:=  select(x) ^ (1/y) = a ]")
)
X
(defun calc-DistribRules ()
X  "DistribRules"
X  (calc-compile-rule-set
X   "DistribRules" "[
iterations(1),
x * select(a + b)		:=  x*select(a) + x*b,
x * select(sum(a,b,c,d))	:=  sum(x*select(a),b,c,d),
x / select(a + b)		:=  1 / (select(a)/x + b/x),
select(a + b) / x		:=  select(a)/x + b/x,
sum(select(a),b,c,d) / x	:=  sum(select(a)/x,b,c,d),
x ^ select(a + b)		:=  x^select(a) * x^b,
x ^ select(sum(a,b,c,d))	:=  prod(x^select(a),b,c,d),
x ^ select(a * b)		:=  (x^a)^select(b),
x ^ select(a / b)		:=  (x^a)^select(1/b),
select(a + b) ^ n		:=  select(x)
X				    :: integer(n) :: n >= 2
X				    :: let(x, expandpow(a+b,n))
X				    :: quote(matches(x,y+z)),
select(a + b) ^ x		:=  a*select(a+b)^(x-1) + b*select(a+b)^(x-1),
select(a * b) ^ x		:=  a^x * select(b)^x,
select(prod(a,b,c,d)) ^ x	:=  prod(select(a)^x,b,c,d),
select(a / b) ^ x		:=  select(a)^x / b^x,
select(- a) ^ x			:=  (-1)^x * select(a)^x,
plain(-select(a + b))		:=  select(-a) - b,
plain(-select(sum(a,b,c,d)))    :=  sum(select(-a),b,c,d),
plain(-select(a * b))	        :=  select(-a) * b,
plain(-select(a / b))	        :=  select(-a) / b,
sqrt(select(a * b))		:=  sqrt(select(a)) * sqrt(b),
sqrt(select(prod(a,b,c,d)))	:=  prod(sqrt(select(a)),b,c,d),
sqrt(select(a / b))		:=  sqrt(select(a)) / sqrt(b),
sqrt(select(- a))		:=  sqrt(-1) sqrt(select(a)),
exp(select(a + b))		:=  exp(select(a)) / exp(-b) :: negative(b),
exp(select(a + b))		:=  exp(select(a)) * exp(b),
exp(select(sum(a,b,c,d)))	:=  prod(exp(select(a)),b,c,d),
exp(select(a * b))		:=  exp(select(a)) ^ b :: constant(b),
exp(select(a * b))		:=  exp(select(a)) ^ b,
exp(select(a / b))		:=  exp(select(a)) ^ (1/b),
ln(select(a * b))		:=  ln(select(a)) + ln(b),
ln(select(prod(a,b,c,d)))	:=  sum(ln(select(a)),b,c,d),
ln(select(a / b))		:=  ln(select(a)) - ln(b),
ln(select(a ^ b))		:=  ln(select(a)) * b,
log10(select(a * b))		:=  log10(select(a)) + log10(b),
log10(select(prod(a,b,c,d)))	:=  sum(log10(select(a)),b,c,d),
log10(select(a / b))		:=  log10(select(a)) - log10(b),
log10(select(a ^ b))		:=  log10(select(a)) * b,
log(select(a * b), x)		:=  log(select(a), x) + log(b,x),
log(select(prod(a,b,c,d)),x)	:=  sum(log(select(a),x),b,c,d),
log(select(a / b), x)		:=  log(select(a), x) - log(b,x),
log(select(a ^ b), x)		:=  log(select(a), x) * b,
log(a, select(b))		:=  ln(a) / select(ln(b)),
sin(select(a + b))		:=  sin(select(a)) cos(b) + cos(a) sin(b),
sin(select(2 a))		:=  2 sin(select(a)) cos(a),
sin(select(n a))		:=  2sin((n-1) select(a)) cos(a) - sin((n-2) a)
X				    :: integer(n) :: n > 2,
cos(select(a + b))		:=  cos(select(a)) cos(b) - sin(a) sin(b),
cos(select(2 a))		:=  2 cos(select(a))^2 - 1,
cos(select(n a))		:=  2cos((n-1) select(a)) cos(a) - cos((n-2) a)
X				    :: integer(n) :: n > 2,
tan(select(a + b))		:=  (tan(select(a)) + tan(b)) /
X				    (1 - tan(a) tan(b)),
tan(select(2 a))		:=  2 tan(select(a)) / (1 - tan(a)^2),
tan(select(n a))		:=  (tan((n-1) select(a)) + tan(a)) /
X				    (1 - tan((n-1) a) tan(a))
X				    :: integer(n) :: n > 2,
sinh(select(a + b))		:=  sinh(select(a)) cosh(b) + cosh(a) sinh(b),
cosh(select(a + b))		:=  cosh(select(a)) cosh(b) + sinh(a) sinh(b),
tanh(select(a + b))		:=  (tanh(select(a)) + tanh(b)) /
X				    (1 + tanh(a) tanh(b)),
x && select(a || b)		:=  (x && select(a)) || (x && b),
select(a || b) && x		:=  (select(a) && x) || (b && x),
! select(a && b)		:=  (!a) || (!b),
! select(a || b)		:=  (!a) && (!b) ]")
)
X
(defun calc-MergeRules ()
X  "MergeRules"
X  (calc-compile-rule-set
X   "MergeRules" "[
iterations(1),
X (x*opt(a)) + select(x*b)	:=  x * (a + select(b)),
X (x*opt(a)) - select(x*b)	:=  x * (a - select(b)),
sum(select(x)*a,b,c,d)		:=  x * sum(select(a),b,c,d),
X (a/x) + select(b/x)		:=  (a + select(b)) / x,
X (a/x) - select(b/x)		:=  (a - select(b)) / x,
sum(a/select(x),b,c,d)		:=  sum(select(a),b,c,d) / x,
X (a/opt(b)) + select(c/d)	:=  ((select(a)*d) + (b*c)) / (b*d),
X (a/opt(b)) - select(c/d)	:=  ((select(a)*d) - (b*c)) / (b*d),
X (x^opt(a)) * select(x^b)	:=  x ^ (a + select(b)),
X (x^opt(a)) / select(x^b)	:=  x ^ (a - select(b)),
select(x^a) / (x^opt(b))	:=  x ^ (select(a) - b),
prod(select(x)^a,b,c,d)		:=  x ^ sum(select(a),b,c,d),
select(x^a) / (x^opt(b))	:=  x ^ (select(a) - b),
X (a^x) * select(b^x)		:=  select((a * b) ^x),
X (a^x) / select(b^x)		:=  select((b / b) ^ x),
select(a^x) / (b^x)		:=  select((a / b) ^ x),
prod(a^select(x),b,c,d)		:=  select(prod(a,b,c,d) ^ x),
X (a^x) * select(b^y)		:=  select((a * b^(y-x)) ^x),
X (a^x) / select(b^y)		:=  select((b / b^(y-x)) ^ x),
select(a^x) / (b^y)		:=  select((a / b^(y-x)) ^ x),
select(x^a) ^ b			:=  x ^ select(a * b),
X (x^a) ^ select(b)		:=  x ^ select(a * b),
select(sqrt(a)) ^ b		:=  select(a ^ (b / 2)),
sqrt(a) ^ select(b)		:=  select(a ^ (b / 2)),
sqrt(select(a) ^ b)		:=  select(a ^ (b / 2)),
sqrt(a ^ select(b))		:=  select(a ^ (b / 2)),
sqrt(a) * select(sqrt(b))	:=  select(sqrt(a * b)),
sqrt(a) / select(sqrt(b))	:=  select(sqrt(a / b)),
select(sqrt(a)) / sqrt(b)	:=  select(sqrt(a / b)),
prod(select(sqrt(a)),b,c,d)	:=  select(sqrt(prod(a,b,c,d))),
exp(a) * select(exp(b))		:=  select(exp(a + b)),
exp(a) / select(exp(b))		:=  select(exp(a - b)),
select(exp(a)) / exp(b)		:=  select(exp(a - b)),
prod(select(exp(a)),b,c,d)	:=  select(exp(sum(a,b,c,d))),
select(exp(a)) ^ b		:=  select(exp(a * b)),
exp(a) ^ select(b)		:=  select(exp(a * b)),
ln(a) + select(ln(b))		:=  select(ln(a * b)),
ln(a) - select(ln(b))		:=  select(ln(a / b)),
select(ln(a)) - ln(b)		:=  select(ln(a / b)),
sum(select(ln(a)),b,c,d)	:=  select(ln(prod(a,b,c,d))),
b * select(ln(a))		:=  select(ln(a ^ b)),
select(b) * ln(a)		:=  select(ln(a ^ b)),
select(ln(a)) / ln(b)		:=  select(log(a, b)),
ln(a) / select(ln(b))		:=  select(log(a, b)),
select(ln(a)) / b		:=  select(ln(a ^ (1/b))),
ln(a) / select(b)		:=  select(ln(a ^ (1/b))),
log10(a) + select(log10(b))	:=  select(log10(a * b)),
log10(a) - select(log10(b))	:=  select(log10(a / b)),
select(log10(a)) - log10(b)	:=  select(log10(a / b)),
sum(select(log10(a)),b,c,d)	:=  select(log10(prod(a,b,c,d))),
b * select(log10(a))		:=  select(log10(a ^ b)),
select(b) * log10(a)		:=  select(log10(a ^ b)),
select(log10(a)) / log10(b)	:=  select(log(a, b)),
log10(a) / select(log10(b))	:=  select(log(a, b)),
select(log10(a)) / b		:=  select(log10(a ^ (1/b))),
log10(a) / select(b)		:=  select(log10(a ^ (1/b))),
log(a,x) + select(log(b,x))	:=  select(log(a * b,x)),
log(a,x) - select(log(b,x))	:=  select(log(a / b,x)),
select(log(a,x)) - log(b,x)	:=  select(log(a / b,x)),
sum(select(log(a,x)),b,c,d)	:=  select(log(prod(a,b,c,d),x)),
b * select(log(a,x))		:=  select(log(a ^ b,x)),
select(b) * log(a,x)		:=  select(log(a ^ b,x)),
select(log(a,x)) / log(b,x)	:=  select(log(a, b)),
log(a,x) / select(log(b,x))	:=  select(log(a, b)),
select(log(a,x)) / b		:=  select(log(a ^ (1/b),x)),
log(a,x) / select(b)		:=  select(log(a ^ (1/b),x)),
select(x && a) || (x && opt(b)) :=  x && (select(a) || b) ]")
)
X
(defun calc-NegateRules ()
X  "NegateRules"
X  (calc-compile-rule-set
X   "NegateRules" "[
iterations(1),
a + select(x)			:=  a - select(-x),
a - select(x)			:=  a + select(-x),
sum(select(x),b,c,d)		:=  -sum(select(-x),b,c,d),
a * select(x)			:=  -a * select(-x),
a / select(x)			:=  -a / select(-x),
select(x) / a			:=  -select(-x) / a,
prod(select(x),b,c,d)		:=  (-1)^(d-c+1) * prod(select(-x),b,c,d),
select(x) ^ n			:=  select(-x) ^ a :: integer(n) :: n%2 = 0,
select(x) ^ n			:=  -(select(-x) ^ a) :: integer(n) :: n%2 = 1,
select(x) ^ a			:=  (-select(-x)) ^ a,
a ^ select(x)			:=  (1 / a)^select(-x),
abs(select(x))			:=  abs(select(-x)),
i sqrt(select(x))		:=  -sqrt(select(-x)),
sqrt(select(x))			:=  i sqrt(select(-x)),
re(select(x))			:=  -re(select(-x)),
im(select(x))			:=  -im(select(-x)),
conj(select(x))			:=  -conj(select(-x)),
trunc(select(x))		:=  -trunc(select(-x)),
round(select(x))		:=  -round(select(-x)),
floor(select(x))		:=  -ceil(select(-x)),
ceil(select(x))			:=  -floor(select(-x)),
ftrunc(select(x))		:=  -ftrunc(select(-x)),
fround(select(x))		:=  -fround(select(-x)),
ffloor(select(x))		:=  -fceil(select(-x)),
fceil(select(x))		:=  -ffloor(select(-x)),
exp(select(x))			:=  1 / exp(select(-x)),
sin(select(x))			:=  -sin(select(-x)),
cos(select(x))			:=  cos(select(-x)),
tan(select(x))			:=  -tan(select(-x)),
arcsin(select(x))		:=  -arcsin(select(-x)),
arccos(select(x))		:=  4 arctan(1) - arccos(select(-x)),
arctan(select(x))		:=  -arctan(select(-x)),
sinh(select(x))			:=  -sinh(select(-x)),
cosh(select(x))			:=  cosh(select(-x)),
tanh(select(x))			:=  -tanh(select(-x)),
arcsinh(select(x))		:=  -arcsinh(select(-x)),
arctanh(select(x))		:=  -arctanh(select(-x)),
select(x) = a			:=  select(-x) = -a,
select(x) != a			:=  select(-x) != -a,
select(x) < a			:=  select(-x) > -a,
select(x) > a			:=  select(-x) < -a,
select(x) <= a			:=  select(-x) >= -a,
select(x) >= a			:=  select(-x) <= -a,
a < select(x)			:=  -a > select(-x),
a > select(x)			:=  -a < select(-x),
a <= select(x)			:=  -a >= select(-x),
a >= select(x)			:=  -a <= select(-x),
select(x)			:=  -select(-x) ]")
)
X
(defun calc-InvertRules ()
X  "InvertRules"
X  (calc-compile-rule-set
X   "InvertRules" "[
iterations(1),
a * select(x)			:=  a / select(1/x),
a / select(x)			:=  a * select(1/x),
select(x) / a			:=  1 / (select(1/x) a),
prod(select(x),b,c,d)		:=  1 / prod(select(1/x),b,c,d),
abs(select(x))			:=  1 / abs(select(1/x)),
sqrt(select(x))			:=  1 / sqrt(select(1/x)),
ln(select(x))			:=  -ln(select(1/x)),
log10(select(x))		:=  -log10(select(1/x)),
log(select(x), a)		:=  -log(select(1/x), a),
log(a, select(x))		:=  -log(a, select(1/x)),
arctan(select(x))               :=  simplify(2 arctan(1))-arctan(select(1/x)),
select(x) = a			:=  select(1/x) = 1/a,
select(x) != a			:=  select(1/x) != 1/a,
select(x) < a			:=  select(1/x) > 1/a,
select(x) > a			:=  select(1/x) < 1/a,
select(x) <= a			:=  select(1/x) >= 1/a,
select(x) >= a			:=  select(1/x) <= 1/a,
a < select(x)			:=  1/a > select(1/x),
a > select(x)			:=  1/a < select(1/x),
a <= select(x)			:=  1/a >= select(1/x),
a >= select(x)			:=  1/a <= select(1/x),
select(x)			:=  1 / select(1/x) ]")
)
X
X
(defun calc-FactorRules ()
X  "FactorRules"
X  (calc-compile-rule-set
X   "FactorRules" "[
thecoefs(x, [z, a+b, c]) := thefactors(x, [d x + d a/c, (c/d) x + (b/d)])
X        :: z = a b/c :: let(d := pgcd(pcont(c), pcont(b))),
thecoefs(x, [z, a, c]) := thefactors(x, [(r x + a/(2 r))^2])
X        :: z = (a/2)^2/c :: let(r := esimplify(sqrt(c)))
X        :: !matches(r, sqrt(rr)),
thecoefs(x, [z, 0, c]) := thefactors(x, [rc x + rz, rc x - rz])
X        :: negative(z)
X        :: let(rz := esimplify(sqrt(-z))) :: !matches(rz, sqrt(rzz))
X        :: let(rc := esimplify(sqrt(c))) :: !matches(rc, sqrt(rcc)),
thecoefs(x, [z, 0, c]) := thefactors(x, [rz + rc x, rz - rc x])
X        :: negative(c)
X        :: let(rz := esimplify(sqrt(z))) :: !matches(rz, sqrt(rzz))
X        :: let(rc := esimplify(sqrt(-c))) :: !matches(rc, sqrt(rcc))
X ]")
)
;;(setq var-FactorRules 'calc-FactorRules)
X
X
(defun calc-IntegAfterRules ()
X  "IntegAfterRules"
X  (calc-compile-rule-set
X   "IntegAfterRules" "[
X opt(a) ln(x) + opt(b) ln(y) := 2 a esimplify(arctanh(x-1))
X     :: a + b = 0 :: nrat(x + y) = 2 || nrat(x - y) = 2,
X a * (b + c) := a b + a c :: constant(a)
X ]")
)
X
;;(setq var-IntegAfterRules 'calc-IntegAfterRules)
X
X
(defun calc-FitRules ()
X  "FitRules"
X  (calc-compile-rule-set
X   "FitRules" "[
X
schedule(1,2,3,4),
iterations(inf),
X
phase(1),
e^x  		:=  exp(x),
x^y		:=  exp(y ln(x))  :: !istrue(constant(y)),
x/y		:=  x fitinv(y),
fitinv(x y)	:=  fitinv(x) fitinv(y),
exp(a) exp(b)	:=  exp(a + b),
a exp(b)	:=  exp(ln(a) + b)  :: !hasfitvars(a),
fitinv(exp(a))  :=  exp(-a),
ln(a b)		:=  ln(a) + ln(b),
ln(fitinv(a))	:=  -ln(a),
log10(a b)	:=  log10(a) + log10(b),
log10(fitinv(a)) := -log10(a),
log(a,b)	:=  ln(a)/ln(b),
ln(exp(a))	:=  a,
a*(b+c)		:=  a*b + a*c,
(a+b)^n		:=  x  :: integer(n) :: n >= 2
X		       :: let(x, expandpow(a+b,n))
X		       :: quote(matches(x,y+z)),
X
phase(1,2),
fitmodel(y = x)   :=  fitmodel(0, y - x),
fitmodel(y, x+c)  :=  fitmodel(y-c, x)  :: !hasfitparams(c),
fitmodel(y, x c)  :=  fitmodel(y/c, x)  :: !hasfitparams(c),
fitmodel(y, x/(c opt(d)))  :=  fitmodel(y c, x/d)  :: !hasfitparams(c),
fitmodel(y, apply(f,[x]))  :=  fitmodel(yy, x)
X			       :: hasfitparams(x)
X			       :: let(FTemp() = yy,
X			              solve(apply(f,[FTemp()]) = y,
X					    FTemp())),
fitmodel(y, apply(f,[x,c]))  :=  fitmodel(yy, x)
X				 :: !hasfitparams(c)
X				 :: let(FTemp() = yy,
X				        solve(apply(f,[FTemp(),c]) = y,
X					      FTemp())),
fitmodel(y, apply(f,[c,x]))  :=  fitmodel(yy, x)
X				 :: !hasfitparams(c)
X				 :: let(FTemp() = yy,
X				        solve(apply(f,[c,FTemp()]) = y,
X					      FTemp())),
X
phase(2,3),
fitmodel(y, x)              :=  fitsystem(y, [], [], fitpart(1,1,x)),
fitpart(a,b,plain(x + y))   :=  fitpart(a,b,x) + fitpart(a,b,y),
fitpart(a,b,plain(x - y))   :=  fitpart(a,b,x) + fitpart(-a,b,y),
fitpart(a,b,plain(-x))	    :=  fitpart(-a,b,x),
fitpart(a,b,x opt(c))	    :=  fitpart(a,x b,c)  :: !hasfitvars(x),
fitpart(a,x opt(b),c)	    :=  fitpart(x a,b,c)  :: !hasfitparams(x),
fitpart(a,x y + x opt(z),c) :=	fitpart(a,x*(y+z),c),
fitpart(a,b,c)		    :=  fitpart2(a,b,c),
X
phase(3),
fitpart2(a1,b1,x) + fitpart2(a2,b2,x)  :=  fitpart(1, a1 b1 + a2 b2, x),
fitpart2(a1,x,c1) + fitpart2(a2,x,c2)  :=  fitpart2(1, x, a1 c1 + a2 c2),
X
phase(4),
fitinv(x)  	:=  1 / x,
exp(x + ln(y))  :=  y exp(x),
exp(x ln(y))	:=  y^x,
ln(x) + ln(y)	:=  ln(x y),
ln(x) - ln(y)	:=  ln(x/y),
x*y + x*z	:=  x*(y+z),
fitsystem(y, xv, pv, fitpart2(a,fitparam(b),c) + opt(d))
X		:=  fitsystem(y, rcons(xv, a c),
X      		              rcons(pv, fitdummy(b) = fitparam(b)), d)
X		    :: b = vlen(pv)+1,
fitsystem(y, xv, pv, fitpart2(a,b,c) + opt(d))
X		:=  fitsystem(y, rcons(xv, a c),
X			      rcons(pv, fitdummy(vlen(pv)+1) = b), d),
fitsystem(y, xv, pv, 0)  :=  fitsystem(y, xv, cons(fvh,fvt))
X			     :: !hasfitparams(xv)
X			     :: let(cons(fvh,fvt),
X				    solve(pv, table(fitparam(j), j, 1,
X						    hasfitparams(pv)))),
fitparam(n) = x  :=  x ]")
)
X
SHAR_EOF
chmod 0644 calc-rules.el ||
echo 'restore of calc-rules.el failed'
Wc_c="`wc -c < 'calc-rules.el'`"
test 17425 -eq "$Wc_c" ||
	echo 'calc-rules.el: original size 17425, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= calc-sel-2.el ==============
if test -f 'calc-sel-2.el' -a X"$1" != X"-c"; then
	echo 'x - skipping calc-sel-2.el (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting calc-sel-2.el (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'calc-sel-2.el' &&
;; Calculator for GNU Emacs, part II [calc-sel-2.el]
;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
X
;; This file is part of GNU Emacs.
X
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.
X
;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.
X
X
X
;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)
X
(require 'calc-macs)
X
(defun calc-Need-calc-sel-2 () nil)
X
X
(defun calc-commute-left (arg)
X  (interactive "p")
X  (if (< arg 0)
X      (calc-commute-right (- arg))
X    (calc-wrapper
X     (calc-preserve-point)
X     (let ((num (max 1 (calc-locate-cursor-element (point))))
X	   (reselect calc-keep-selection))
X       (if (= arg 0) (setq arg nil))
X       (while (or (null arg) (>= (setq arg (1- arg)) 0))
X	 (let* ((entry (calc-top num 'entry))
X		(expr (car entry))
X		(sel (calc-auto-selection entry))
X		parent new)
X	   (or (and sel
X		    (consp (setq parent (calc-find-assoc-parent-formula
X					 expr sel))))
X	       (error "No term is selected"))
X	   (if (and calc-assoc-selections
X		    (assq (car parent) calc-assoc-ops))
X	       (let ((outer (calc-find-parent-formula parent sel)))
X		 (if (eq sel (nth 2 outer))
X		     (setq new (calc-replace-sub-formula
X				parent outer
X				(cond
X				 ((memq (car outer)
X					(nth 1 (assq (car-safe (nth 1 outer))
X						     calc-assoc-ops)))
X				  (let* ((other (nth 2 (nth 1 outer)))
X					 (new (calc-build-assoc-term
X					       (car (nth 1 outer))
X					       (calc-build-assoc-term
X						(car outer)
X						(nth 1 (nth 1 outer))
X						sel)
X					       other)))
X				    (setq sel (nth 2 (nth 1 new)))
X				    new))
X				 ((eq (car outer) '-)
X				  (calc-build-assoc-term
X				   '+
X				   (setq sel (math-neg sel))
X				   (nth 1 outer)))
X				 ((eq (car outer) '/)
X				  (calc-build-assoc-term
X				   '*
X				   (setq sel (calcFunc-div 1 sel))
X				   (nth 1 outer)))
X				 (t (calc-build-assoc-term
X				     (car outer) sel (nth 1 outer))))))
X		   (let ((next (calc-find-parent-formula parent outer)))
X		     (if (not (and (consp next)
X				   (eq outer (nth 2 next))
X				   (eq (car next) (car outer))))
X			 (setq new nil)
X		       (setq new (calc-build-assoc-term
X				  (car next)
X				  sel
X				  (calc-build-assoc-term
X				   (car next) (nth 1 next) (nth 2 outer)))
X			     sel (nth 1 new)
X			     new (calc-replace-sub-formula
X				  parent next new))))))
X	     (if (eq (nth 1 parent) sel)
X		 (setq new nil)
X	       (let ((p (nthcdr (1- (calc-find-sub-formula parent sel))
X				(setq new (copy-sequence parent)))))
X		 (setcar (cdr p) (car p))
X		 (setcar p sel))))
X	   (if (null new)
X	       (if arg
X		   (error "Term is already leftmost")
X		 (or reselect
X		     (calc-pop-push-list 1 (list expr) num '(nil)))
X		 (setq arg 0))
X	     (calc-pop-push-record-list
X	      1 "left"
X	      (list (calc-replace-sub-formula expr parent new))
X	      num
X	      (list (and (or (not (eq arg 0)) reselect)
X			 sel)))))))))
)
X
(defun calc-commute-right (arg)
X  (interactive "p")
X  (if (< arg 0)
X      (calc-commute-left (- arg))
X    (calc-wrapper
X     (calc-preserve-point)
X     (let ((num (max 1 (calc-locate-cursor-element (point))))
X	   (reselect calc-keep-selection))
X       (if (= arg 0) (setq arg nil))
X       (while (or (null arg) (>= (setq arg (1- arg)) 0))
X	 (let* ((entry (calc-top num 'entry))
X		(expr (car entry))
X		(sel (calc-auto-selection entry))
X		parent new)
X	   (or (and sel
X		    (consp (setq parent (calc-find-assoc-parent-formula
X					 expr sel))))
X	       (error "No term is selected"))
X	   (if (and calc-assoc-selections
X		    (assq (car parent) calc-assoc-ops))
X	       (let ((outer (calc-find-parent-formula parent sel)))
X		 (if (eq sel (nth 1 outer))
X		     (setq new (calc-replace-sub-formula
X				parent outer
X				(if (memq (car outer)
X					  (nth 2 (assq (car-safe (nth 2 outer))
X						       calc-assoc-ops)))
X				    (let ((other (nth 1 (nth 2 outer))))
X				      (calc-build-assoc-term
X				       (car outer)
X				       other
X				       (calc-build-assoc-term
X					(car (nth 2 outer))
X					sel
X					(nth 2 (nth 2 outer)))))
X				  (let ((new (cond
X					      ((eq (car outer) '-)
X					       (calc-build-assoc-term
X						'+
X						(math-neg (nth 2 outer))
X						sel))
X					      ((eq (car outer) '/)
X					       (calc-build-assoc-term
X						'*
X						(calcFunc-div 1 (nth 2 outer))
X						sel))
X					      (t (calc-build-assoc-term
X						  (car outer)
X						  (nth 2 outer)
X						  sel)))))
X				    (setq sel (nth 2 new))
X				    new))))
X		   (let ((next (calc-find-parent-formula parent outer)))
X		     (if (not (and (consp next)
X				   (eq outer (nth 1 next))))
X			 (setq new nil)
X		       (setq new (calc-build-assoc-term
X				  (car outer)
X				  (calc-build-assoc-term
X				   (car next) (nth 1 outer) (nth 2 next))
X				  sel)
X			     sel (nth 2 new)
X			     new (calc-replace-sub-formula
X				  parent next new))))))
X	     (if (eq (nth (1- (length parent)) parent) sel)
X		 (setq new nil)
X	       (let ((p (nthcdr (calc-find-sub-formula parent sel)
X				(setq new (copy-sequence parent)))))
X		 (setcar p (nth 1 p))
X		 (setcar (cdr p) sel))))
X	   (if (null new)
X	       (if arg
X		   (error "Term is already rightmost")
X		 (or reselect
X		     (calc-pop-push-list 1 (list expr) num '(nil)))
X		 (setq arg 0))
X	     (calc-pop-push-record-list
X	      1 "rght"
X	      (list (calc-replace-sub-formula expr parent new))
X	      num
X	      (list (and (or (not (eq arg 0)) reselect)
X			 sel)))))))))
)
X
(defun calc-build-assoc-term (op lhs rhs)
X  (cond ((and (eq op '+) (or (math-looks-negp rhs)
X			     (and (eq (car-safe rhs) 'cplx)
X				  (math-negp (nth 1 rhs))
X				  (eq (nth 2 rhs) 0))))
X	 (list '- lhs (math-neg rhs)))
X	((and (eq op '-) (or (math-looks-negp rhs)
X			     (and (eq (car-safe rhs) 'cplx)
X				  (math-negp (nth 1 rhs))
X				  (eq (nth 2 rhs) 0))))
X	 (list '+ lhs (math-neg rhs)))
X	((and (eq op '*) (and (eq (car-safe rhs) '/)
X			      (or (math-equal-int (nth 1 rhs) 1)
X				  (equal (nth 1 rhs) '(cplx 1 0)))))
X	 (list '/ lhs (nth 2 rhs)))
X	((and (eq op '/) (and (eq (car-safe rhs) '/)
X			      (or (math-equal-int (nth 1 rhs) 1)
X				  (equal (nth 1 rhs) '(cplx 1 0)))))
X	 (list '/ lhs (nth 2 rhs)))
X	(t (list op lhs rhs)))
)
X
(defun calc-sel-unpack ()
X  (interactive)
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (expr (car entry))
X	  (sel (or (calc-auto-selection entry) expr)))
X     (or (and (not (math-primp sel))
X	      (= (length sel) 2))
X	 (error "Selection must be a function of one argument"))
X     (calc-pop-push-record-list 1 "unpk"
X				(list (calc-replace-sub-formula
X				       expr sel (nth 1 sel)))
X				num
X				(list (and reselect (nth 1 sel))))))
)
X
(defun calc-sel-isolate ()
X  (interactive)
X  (calc-slow-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (expr (car entry))
X	  (sel (or (calc-auto-selection entry) (error "No selection")))
X	  (eqn sel)
X	  soln)
X     (while (and (or (consp (setq eqn (calc-find-parent-formula expr eqn)))
X		     (error "Selection must be a member of an equation"))
X		 (not (assq (car eqn) calc-tweak-eqn-table))))
X     (setq soln (math-solve-eqn eqn sel calc-hyperbolic-flag))
X     (or soln
X	 (error "No solution found"))
X     (setq soln (calc-encase-atoms
X		 (if (eq (not (calc-find-sub-formula (nth 2 eqn) sel))
X			 (eq (nth 1 soln) sel))
X		     soln
X		   (list (nth 1 (assq (car soln) calc-tweak-eqn-table))
X			 (nth 2 soln)
X			 (nth 1 soln)))))
X     (calc-pop-push-record-list 1 "isol"
X				(list (calc-replace-sub-formula
X				       expr eqn soln))
X				num
X				(list (and reselect sel)))
X     (calc-handle-whys)))
)
X
(defun calc-sel-commute (many)
X  (interactive "P")
X  (let ((calc-assoc-selections nil))
X    (calc-rewrite-selection "CommuteRules" many "cmut"))
X  (calc-set-mode-line)
)
X
(defun calc-sel-jump-equals (many)
X  (interactive "P")
X  (calc-rewrite-selection "JumpRules" many "jump")
)
X
(defun calc-sel-distribute (many)
X  (interactive "P")
X  (calc-rewrite-selection "DistribRules" many "dist")
)
X
(defun calc-sel-merge (many)
X  (interactive "P")
X  (calc-rewrite-selection "MergeRules" many "merg")
)
X
(defun calc-sel-negate (many)
X  (interactive "P")
X  (calc-rewrite-selection "NegateRules" many "jneg")
)
X
(defun calc-sel-invert (many)
X  (interactive "P")
X  (calc-rewrite-selection "InvertRules" many "jinv")
)
X
SHAR_EOF
chmod 0644 calc-sel-2.el ||
echo 'restore of calc-sel-2.el failed'
Wc_c="`wc -c < 'calc-sel-2.el'`"
test 9143 -eq "$Wc_c" ||
	echo 'calc-sel-2.el: original size 9143, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= calc-sel.el ==============
if test -f 'calc-sel.el' -a X"$1" != X"-c"; then
	echo 'x - skipping calc-sel.el (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting calc-sel.el (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'calc-sel.el' &&
;; Calculator for GNU Emacs, part II [calc-sel.el]
;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
X
;; This file is part of GNU Emacs.
X
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.
X
;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.
X
X
X
;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)
X
(require 'calc-macs)
X
(defun calc-Need-calc-sel () nil)
X
X
;;; Selection commands.
X
(defun calc-select-here (num &optional once keep)
X  (interactive "P")
X  (calc-wrapper
X   (calc-prepare-selection)
X   (let ((found (calc-find-selected-part))
X	 (entry calc-selection-cache-entry))
X     (or (and keep (nth 2 entry))
X	 (progn
X	   (if once (progn
X		      (setq calc-keep-selection nil)
X		      (message "(Selection will apply to next command only)")))
X	   (calc-change-current-selection 
X	    (if found
X		(if (and num (> (setq num (prefix-numeric-value num)) 0))
X		    (progn
X		      (while (and (>= (setq num (1- num)) 0)
X				  (not (eq found (car entry))))
X			(setq found (calc-find-assoc-parent-formula
X				     (car entry) found)))
X		      found)
X		  (calc-grow-assoc-formula (car entry) found))
X	      (car entry)))))))
)
X
(defun calc-select-once (num)
X  (interactive "P")
X  (calc-select-here num t)
)
X
(defun calc-select-here-maybe (num)
X  (interactive "P")
X  (calc-select-here num nil t)
)
X
(defun calc-select-once-maybe (num)
X  (interactive "P")
X  (calc-select-once num t t)
)
X
(defun calc-select-additional ()
X  (interactive)
X  (calc-wrapper
X   (let (calc-keep-selection)
X     (calc-prepare-selection))
X   (let ((found (calc-find-selected-part))
X	 (entry calc-selection-cache-entry))
X     (calc-change-current-selection
X      (if found
X	  (let ((sel (nth 2 entry)))
X	    (if sel
X		(progn
X		  (while (not (or (eq sel (car entry))
X				  (calc-find-sub-formula sel found)))
X		    (setq sel (calc-find-assoc-parent-formula
X			       (car entry) sel)))
X		  sel)
X	      (calc-grow-assoc-formula (car entry) found)))
X	(car entry)))))
)
X
(defun calc-select-more (num)
X  (interactive "P")
X  (calc-wrapper
X   (calc-prepare-selection)
X   (let ((entry calc-selection-cache-entry))
X     (if (nth 2 entry)
X	 (let ((sel (nth 2 entry)))
X	   (while (and (not (eq sel (car entry)))
X		       (>= (setq num (1- (prefix-numeric-value num))) 0))
X	     (setq sel (calc-find-assoc-parent-formula (car entry) sel)))
X	   (calc-change-current-selection sel))
X       (calc-select-here num))))
)
X
(defun calc-select-less (num)
X  (interactive "p")
X  (calc-wrapper
X   (calc-prepare-selection)
X   (let ((found (calc-find-selected-part))
X	 (entry calc-selection-cache-entry))
X     (calc-change-current-selection 
X      (and found
X	   (let ((sel (nth 2 entry))
X		 old index op)
X	     (while (and sel
X			 (not (eq sel found))
X			 (>= (setq num (1- num)) 0))
X	       (setq old sel
X		     index (calc-find-sub-formula sel found))
X	       (and (setq sel (and index (nth index old)))
X		    calc-assoc-selections
X		    (setq op (assq (car-safe sel) calc-assoc-ops))
X		    (memq (car old) (nth index op))
X		    (setq num (1+ num))))
X	     sel)))))
)
X
(defun calc-select-part (num)
X  (interactive "P")
X  (or num (setq num (- last-command-char ?0)))
X  (calc-wrapper
X   (calc-prepare-selection)
X   (let ((sel (calc-find-nth-part (or (nth 2 calc-selection-cache-entry)
X				      (car calc-selection-cache-entry))
X				  num)))
X     (if sel
X	 (calc-change-current-selection sel)
X       (error "%d is not a valid sub-formula index" num))))
)
X
(defun calc-find-nth-part (expr num)
X  (if (and calc-assoc-selections
X	   (assq (car-safe expr) calc-assoc-ops))
X      (let (op)
X	(calc-find-nth-part-rec expr))
X    (if (eq (car-safe expr) 'intv)
X	(and (>= num 1) (<= num 2) (nth (1+ num) expr))
X      (and (not (Math-primp expr)) (>= num 1) (< num (length expr))
X	   (nth num expr))))
)
X
(defun calc-find-nth-part-rec (expr)   ; uses num, op
X  (or (if (and (setq op (assq (car-safe (nth 1 expr)) calc-assoc-ops))
X	       (memq (car expr) (nth 1 op)))
X	  (calc-find-nth-part-rec (nth 1 expr))
X	(and (= (setq num (1- num)) 0)
X	     (nth 1 expr)))
X      (if (and (setq op (assq (car-safe (nth 2 expr)) calc-assoc-ops))
X	       (memq (car expr) (nth 2 op)))
X	  (calc-find-nth-part-rec (nth 2 expr))
X	(and (= (setq num (1- num)) 0)
X	     (nth 2 expr))))
)
X
(defun calc-select-next (num)
X  (interactive "p")
X  (if (< num 0)
X      (calc-select-previous (- num))
X    (calc-wrapper
X     (calc-prepare-selection)
X     (let* ((entry calc-selection-cache-entry)
X	    (sel (nth 2 entry)))
X       (if sel
X	   (progn
X	     (while (>= (setq num (1- num)) 0)
X	       (let* ((parent (calc-find-parent-formula (car entry) sel))
X		     (p parent)
X		     op)
X		 (and (eq p t) (setq p nil))
X		 (while (and (setq p (cdr p))
X			     (not (eq (car p) sel))))
X		 (if (cdr p)
X		     (setq sel (or (and calc-assoc-selections
X					(setq op (assq (car-safe (nth 1 p))
X						       calc-assoc-ops))
X					(memq (car parent) (nth 2 op))
X					(nth 1 (nth 1 p)))
X				   (nth 1 p)))
X		   (if (and calc-assoc-selections
X			    (setq op (assq (car-safe parent) calc-assoc-ops))
X			    (consp (setq p (calc-find-parent-formula
X					    (car entry) parent)))
X			    (eq (nth 1 p) parent)
X			    (memq (car p) (nth 1 op)))
X		       (setq sel (nth 2 p))
X		     (error "No \"next\" sub-formula")))))
X	     (calc-change-current-selection sel))
X	 (if (Math-primp (car entry))
X	     (calc-change-current-selection (car entry))
X	   (calc-select-part num))))))
)
X
(defun calc-select-previous (num)
X  (interactive "p")
X  (if (< num 0)
X      (calc-select-next (- num))
X    (calc-wrapper
X     (calc-prepare-selection)
X     (let* ((entry calc-selection-cache-entry)
X	    (sel (nth 2 entry)))
X       (if sel
X	   (progn
X	     (while (>= (setq num (1- num)) 0)
X	       (let* ((parent (calc-find-parent-formula (car entry) sel))
X		      (p (cdr-safe parent))
X		      (prev nil)
X		      op)
X		 (if (eq (car-safe parent) 'intv) (setq p (cdr p)))
X		 (while (and (not (eq (car p) sel))
X			     (setq prev (car p)
X				   p (cdr p))))
X		 (if prev
X		     (setq sel (or (and calc-assoc-selections
X					(setq op (assq (car-safe prev)
X						       calc-assoc-ops))
X					(memq (car parent) (nth 1 op))
X					(nth 2 prev))
X				   prev))
X		   (if (and calc-assoc-selections
X			    (setq op (assq (car-safe parent) calc-assoc-ops))
X			    (consp (setq p (calc-find-parent-formula
X					    (car entry) parent)))
X			    (eq (nth 2 p) parent)
X			    (memq (car p) (nth 2 op)))
X		       (setq sel (nth 1 p))
X		     (error "No \"previous\" sub-formula")))))
X	     (calc-change-current-selection sel))
X	 (if (Math-primp (car entry))
X	     (calc-change-current-selection (car entry))
X	   (let ((len (if (and calc-assoc-selections
X			       (assq (car (car entry)) calc-assoc-ops))
X			  (let (op (num 0))
X			    (calc-find-nth-part-rec (car entry))
X			    (- 1 num))
X			(length (car entry)))))
X	     (calc-select-part (- len num))))))))
)
X
(defun calc-find-parent-formula (expr part)
X  (cond ((eq expr part) t)
X	((Math-primp expr) nil)
X	(t
X	 (let ((p expr) res)
X	   (while (and (setq p (cdr p))
X		       (not (setq res (calc-find-parent-formula
X				       (car p) part)))))
X	   (and p
X		(if (eq res t) expr res)))))
)
X
X
(defun calc-find-assoc-parent-formula (expr part)
X  (calc-grow-assoc-formula expr (calc-find-parent-formula expr part))
)
X
(defun calc-grow-assoc-formula (expr part)
X  (if calc-assoc-selections
X      (let ((op (assq (car-safe part) calc-assoc-ops)))
X	(if op
X	    (let (new)
X	      (while (and (consp (setq new (calc-find-parent-formula
X					    expr part)))
X			  (memq (car new)
X				(nth (calc-find-sub-formula new part) op)))
X		(setq part new))))
X	part)
X    part)
)
X
(defun calc-find-sub-formula (expr part)
X  (cond ((eq expr part) t)
X	((Math-primp expr) nil)
X	(t
X	 (let ((num 1))
X	   (while (and (setq expr (cdr expr))
X		       (not (calc-find-sub-formula (car expr) part)))
X	     (setq num (1+ num)))
X	   (and expr num))))
)
X
(defun calc-unselect (num)
X  (interactive "P")
X  (calc-wrapper
X   (calc-prepare-selection num)
X   (calc-change-current-selection nil))
)
X
(defun calc-clear-selections ()
X  (interactive)
X  (calc-wrapper
X   (let ((limit (calc-stack-size))
X	 (n 1))
X     (while (<= n limit)
X       (if (calc-top n 'sel)
X	   (progn
X	     (calc-prepare-selection n)
X	     (calc-change-current-selection nil)))
X       (setq n (1+ n))))
X   (calc-clear-command-flag 'position-point))
)
X
(defun calc-show-selections (arg)
X  (interactive "P")
X  (calc-wrapper
X   (calc-preserve-point)
X   (setq calc-show-selections (if arg
X				  (> (prefix-numeric-value arg) 0)
X				(not calc-show-selections)))
X   (let ((p calc-stack))
X     (while (and p
X		 (or (null (nth 2 (car p)))
X		     (equal (car p) calc-selection-cache-entry)))
X       (setq p (cdr p)))
X     (or (and p
X	      (let ((calc-selection-cache-default-entry
X		     calc-selection-cache-entry))
X		(calc-do-refresh)))
X	 (and calc-selection-cache-entry
X	      (let ((sel (nth 2 calc-selection-cache-entry)))
X		(setcar (nthcdr 2 calc-selection-cache-entry) nil)
X		(calc-change-current-selection sel)))))
X   (message (if calc-show-selections
X		"Displaying only selected part of formulas"
X	      "Displaying all but selected part of formulas")))
)
X
(defun calc-preserve-point ()
X  (or (looking-at "\\.\n+\\'")
X      (progn
X	(setq calc-final-point-line (+ (count-lines (point-min) (point))
X				       (if (bolp) 1 0))
X	      calc-final-point-column (current-column))
X	(calc-set-command-flag 'position-point)))
)
X
(defun calc-enable-selections (arg)
X  (interactive "P")
X  (calc-wrapper
X   (calc-preserve-point)
X   (setq calc-use-selections (if arg
X				 (> (prefix-numeric-value arg) 0)
X			       (not calc-use-selections)))
X   (calc-set-command-flag 'renum-stack)
X   (message (if calc-use-selections
X		"Commands operate only on selected sub-formulas"
X	      "Selections of sub-formulas have no effect")))
)
X
(defun calc-break-selections (arg)
X  (interactive "P")
X  (calc-wrapper
X   (calc-preserve-point)
X   (setq calc-assoc-selections (if arg
X				   (<= (prefix-numeric-value arg) 0)
X				 (not calc-assoc-selections)))
X   (message (if calc-assoc-selections
X		"Selection treats a+b+c as a sum of three terms"
X	      "Selection treats a+b+c as (a+b)+c")))
)
X
(defun calc-prepare-selection (&optional num)
X  (or num (setq num (calc-locate-cursor-element (point))))
X  (setq calc-selection-true-num num
X	calc-keep-selection t)
X  (or (> num 0) (setq num 1))
X  ;; (if (or (< num 1) (> num (calc-stack-size)))
X  ;;     (error "Cursor must be positioned on a stack element"))
X  (let* ((entry (calc-top num 'entry))
X	 ww w)
X    (or (equal entry calc-selection-cache-entry)
X	(progn
X	  (setcar entry (calc-encase-atoms (car entry)))
X	  (setq calc-selection-cache-entry entry
X		calc-selection-cache-num num
X		calc-selection-cache-comp
X		(let ((math-comp-tagged t))
X		  (math-compose-expr (car entry) 0))
X		calc-selection-cache-offset
X		(+ (car (math-stack-value-offset calc-selection-cache-comp))
X		   (length calc-left-label)
X		   (if calc-line-numbering 4 0))))))
X  (calc-preserve-point)
)
(setq calc-selection-cache-entry nil)
X
;;; The following ensures that no two subformulas will be "eq" to each other!
(defun calc-encase-atoms (x)
X  (if (or (not (consp x))
X	  (equal x '(float 0 0)))
X      (list 'cplx x 0)
X    (calc-encase-atoms-rec x)
X    x)
)
X
(defun calc-encase-atoms-rec (x)
X  (or (Math-primp x)
X      (progn
X	(if (eq (car x) 'intv)
X	    (setq x (cdr x)))
X	(while (setq x (cdr x))
X	  (if (or (not (consp (car x)))
X		  (equal (car x) '(float 0 0)))
X	      (setcar x (list 'cplx (car x) 0))
X	    (calc-encase-atoms-rec (car x))))))
)
X
(defun calc-find-selected-part ()
X  (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset))
X	 toppt
X	 (lcount 0)
X	 (spaces 0)
X	 (math-comp-sel-vpos (save-excursion
X			       (beginning-of-line)
X			       (let ((line (point)))
X				 (calc-cursor-stack-index
X				  calc-selection-cache-num)
X				 (setq toppt (point))
X				 (while (< (point) line)
X				   (forward-line 1)
X				   (setq spaces (+ spaces
X						   (current-indentation))
X					 lcount (1+ lcount)))
X				 (- lcount (math-comp-ascent
X					    calc-selection-cache-comp) -1))))
X	 (math-comp-sel-cpos (- (point) toppt calc-selection-cache-offset
X				spaces lcount))
X	 (math-comp-sel-tag nil))
X    (and (>= math-comp-sel-hpos 0)
X	 (> calc-selection-true-num 0)
X	 (math-composition-to-string calc-selection-cache-comp 1000000))
X    (nth 1 math-comp-sel-tag))
)
X
(defun calc-change-current-selection (sub-expr)
X  (or (eq sub-expr (nth 2 calc-selection-cache-entry))
X      (let ((calc-prepared-composition calc-selection-cache-comp)
X	    (buffer-read-only nil)
X	    top)
X	(calc-set-command-flag 'renum-stack)
X	(setcar (nthcdr 2 calc-selection-cache-entry) sub-expr)
X	(calc-cursor-stack-index calc-selection-cache-num)
X	(setq top (point))
X	(calc-cursor-stack-index (1- calc-selection-cache-num))
X	(delete-region top (point))
X	(let ((calc-selection-cache-default-entry calc-selection-cache-entry))
X	  (insert (math-format-stack-value calc-selection-cache-entry)
X		  "\n"))))
)
X
(defun calc-top-selected (&optional n m)
X  (and calc-any-selections
X       calc-use-selections
X       (progn
X	 (or n (setq n 1))
X	 (or m (setq m 1))
X	 (calc-check-stack (+ n m -1))
X	 (let ((top (nthcdr (+ m calc-stack-top -1) calc-stack))
X	       (sel nil))
X	   (while (>= (setq n (1- n)) 0)
X	     (if (nth 2 (car top))
X		 (setq sel (if sel t (nth 2 (car top)))))
X	     (setq top (cdr top)))
X	   sel)))
)
X
(defun calc-replace-sub-formula (expr old new)
X  (setq new (calc-encase-atoms new))
X  (calc-replace-sub-formula-rec expr)
)
X
(defun calc-replace-sub-formula-rec (expr)
X  (cond ((eq expr old) new)
X	((Math-primp expr) expr)
X	(t
X	 (cons (car expr)
X	       (mapcar 'calc-replace-sub-formula-rec (cdr expr)))))
)
X
(defun calc-sel-error ()
X  (error "Illegal operation on sub-formulas")
)
X
(defun calc-replace-selections (n vals m)
X  (if (calc-top-selected n m)
X      (let ((num (length vals)))
X	(calc-preserve-point)
X	(cond
X	 ((= n num)
X	  (let* ((old (calc-top-list n m 'entry))
X		 (new nil)
X		 (sel nil)
X		 val)
X	    (while old
X	      (if (nth 2 (car old))
X		  (setq val (calc-encase-atoms (car vals))
X			new (cons (calc-replace-sub-formula (car (car old))
X							    (nth 2 (car old))
X							    val)
X				  new)
X			sel (cons val sel))
X		(setq new (cons (car vals) new)
X		      sel (cons nil sel)))
X	      (setq vals (cdr vals)
X		    old (cdr old)))
X	    (calc-pop-stack n m t)
X	    (calc-push-list (nreverse new)
X			    m (and calc-keep-selection (nreverse sel)))))
X	 ((= num 1)
X	  (let* ((old (calc-top-list n m 'entry))
X		 more)
X	    (while (and old (not (nth 2 (car old))))
X	      (setq old (cdr old)))
X	    (setq more old)
X	    (while (and (setq more (cdr more)) (not (nth 2 (car more)))))
X	    (and more
X		 (calc-sel-error))
X	    (calc-pop-stack n m t)
X	    (if old
X		(let ((val (calc-encase-atoms (car vals))))
X		  (calc-push-list (list (calc-replace-sub-formula
X					 (car (car old))
X					 (nth 2 (car old))
X					 val))
X				  m (and calc-keep-selection (list val))))
X	      (calc-push-list vals))))
X	 (t (calc-sel-error))))
X    (calc-pop-stack n m t)
X    (calc-push-list vals m))
)
(setq calc-keep-selection t)
X
(defun calc-delete-selection (n)
X  (let ((entry (calc-top n 'entry)))
X    (if (nth 2 entry)
X	(if (eq (nth 2 entry) (car entry))
X	    (progn
X	      (calc-pop-stack 1 n t)
X	      (calc-push-list '(0) n))
X	  (let ((parent (calc-find-parent-formula (car entry) (nth 2 entry)))
X		(repl nil))
X	    (calc-preserve-point)
X	    (calc-pop-stack 1 n t)
X	    (cond ((or (memq (car parent) '(* / %))
X		       (and (eq (car parent) '^)
X			    (eq (nth 2 parent) (nth 2 entry))))
X		   (setq repl 1))
X		  ((memq (car parent) '(vec calcFunc-min calcFunc-max)))
X		  ((and (assq (car parent) calc-tweak-eqn-table)
X			(= (length parent) 3))
X		   (setq repl 'del))
X		  (t
X		   (setq repl 0)))
X	    (cond
X	     ((eq repl 'del)
X	      (calc-push-list (list
X			       (calc-normalize
X				(calc-replace-sub-formula
X				 (car entry)
X				 parent
X				 (if (eq (nth 2 entry) (nth 1 parent))
X				     (nth 2 parent)
X				   (nth 1 parent)))))
X			      n))
X	     (repl
X	      (calc-push-list (list
X			       (calc-normalize
X				(calc-replace-sub-formula (car entry)
X							  (nth 2 entry)
X							  repl)))
X			      n))
X	     (t
X	      (calc-push-list (list
X			       (calc-normalize
X				(calc-replace-sub-formula (car entry)
X							  parent
X							  (delq (nth 2 entry)
X								(copy-sequence
X								 parent)))))
X			      n)))))
X      (calc-pop-stack 1 n t)))
)
X
(defun calc-roll-down-with-selections (n m)
X  (let ((vals (append (calc-top-list m 1)
X		      (calc-top-list (- n m) (1+ m))))
X	(sels (append (calc-top-list m 1 'sel)
X		      (calc-top-list (- n m) (1+ m) 'sel))))
X    (calc-pop-push-list n vals 1 sels))
)
X
(defun calc-roll-up-with-selections (n m)
X  (let ((vals (append (calc-top-list (- n m) 1)
X		      (calc-top-list m (- n m -1))))
X	(sels (append (calc-top-list (- n m) 1 'sel)
X		      (calc-top-list m (- n m -1) 'sel))))
X    (calc-pop-push-list n vals 1 sels))
)
X
(defun calc-auto-selection (entry)
X  (or (nth 2 entry)
X      (progn
X	(and (boundp 'reselect) (setq reselect nil))
X	(calc-prepare-selection)
X	(calc-grow-assoc-formula (car entry) (calc-find-selected-part))))
)
X
(defun calc-copy-selection ()
X  (interactive)
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (entry (calc-top num 'entry)))
X     (calc-push (or (calc-auto-selection entry) (car entry)))))
)
X
(defun calc-del-selection ()
X  (interactive)
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (entry (calc-top num 'entry))
X	  (sel (calc-auto-selection entry)))
X     (setcar (nthcdr 2 entry) (and (not (eq sel (car entry))) sel))
X     (calc-delete-selection num)))
)
X
(defun calc-enter-selection ()
X  (interactive)
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (expr (car entry))
X	  (sel (or (calc-auto-selection entry) expr))
X	  alg)
X     (let ((calc-dollar-values (list sel))
X	   (calc-dollar-used 0))
X       (setq alg (calc-do-alg-entry "" "Replace selection with: "))
X       (and alg
X	    (progn
X	      (setq alg (calc-encase-atoms (car alg)))
X	      (calc-pop-push-record-list 1 "repl"
X					 (list (calc-replace-sub-formula
X						expr sel alg))
X					 num
X					 (list (and reselect alg))))))
X     (calc-handle-whys)))
)
X
(defun calc-edit-selection ()
X  (interactive)
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (expr (car entry))
X	  (sel (or (calc-auto-selection entry) expr))
X	  alg)
X     (let ((str (math-showing-full-precision
X		 (math-format-nice-expr sel (screen-width)))))
X       (calc-edit-mode (list 'calc-finish-selection-edit
X			     num (list 'quote sel) reselect))
X       (insert str "\n"))))
X  (calc-show-edit-buffer)
)
X
(defun calc-finish-selection-edit (num sel reselect)
X  (let ((buf (current-buffer))
X	(str (buffer-substring (point) (point-max)))
X	(start (point)))
X    (switch-to-buffer calc-original-buffer)
X    (let ((val (math-read-expr str)))
X      (if (eq (car-safe val) 'error)
X	  (progn
X	    (switch-to-buffer buf)
X	    (goto-char (+ start (nth 1 val)))
X	    (error (nth 2 val))))
X      (calc-wrapper
X       (calc-preserve-point)
X       (if disp-trail
X	   (calc-trail-display 1 t))
X       (setq val (calc-encase-atoms (calc-normalize val)))
X       (let ((expr (calc-top num 'full)))
X	 (if (calc-find-sub-formula expr sel)
X	     (calc-pop-push-record-list 1 "edit"
X					(list (calc-replace-sub-formula
X					       expr sel val))
X					num
X					(list (and reselect val)))
X	   (calc-push val)
X	   (error "Original selection has been lost"))))))
)
X
(defun calc-sel-evaluate (arg)
X  (interactive "p")
X  (calc-slow-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (sel (or (calc-auto-selection entry) (car entry))))
X     (calc-with-default-simplification
X      (let ((math-simplify-only nil))
X	(calc-modify-simplify-mode arg)
X	(let ((val (calc-encase-atoms (calc-normalize sel))))
X	  (calc-pop-push-record-list 1 "jsmp"
X				     (list (calc-replace-sub-formula
X					    (car entry) sel val))
X				     num
X				     (list (and reselect val))))))
X     (calc-handle-whys)))
)
X
(defun calc-sel-expand-formula (arg)
X  (interactive "p")
X  (calc-slow-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (sel (or (calc-auto-selection entry) (car entry))))
X     (calc-with-default-simplification
X      (let ((math-simplify-only nil))
X	(calc-modify-simplify-mode arg)
X	(let* ((math-expand-formulas (> arg 0))
X	       (val (calc-normalize sel))
X	       top)
X	  (and (<= arg 0)
X	       (setq top (math-expand-formula val))
X	       (setq val (calc-normalize top)))
X	  (setq val (calc-encase-atoms val))
X	  (calc-pop-push-record-list 1 "jexf"
X				     (list (calc-replace-sub-formula
X					    (car entry) sel val))
X				     num
X				     (list (and reselect val))))))
X     (calc-handle-whys)))
)
X
(defun calc-sel-mult-both-sides (no-simp &optional divide)
X  (interactive "P")
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (expr (car entry))
X	  (sel (or (calc-auto-selection entry) expr))
X	  (func (car-safe sel))
X	  alg lhs rhs)
X     (setq alg (calc-with-default-simplification
X		(car (calc-do-alg-entry ""
X					(if divide
X					    "Divide both sides by: "
X					  "Multiply both sides by: ")))))
X     (and alg
X	  (progn
X	    (if (and (or (eq func '/)
X			 (assq func calc-tweak-eqn-table))
X		     (= (length sel) 3))
X		(progn
X		  (or (memq func '(/ calcFunc-eq calcFunc-neq))
X		      (if (math-known-nonposp alg)
X			  (progn
X			    (setq func (nth 1 (assq func
X						    calc-tweak-eqn-table)))
X			    (or (math-known-negp alg)
X				(message "Assuming this factor is nonzero")))
X			(or (math-known-posp alg)
X			    (if (math-known-nonnegp alg)
X				(message "Assuming this factor is nonzero")
X			      (message "Assuming this factor is positive")))))
X		  (setq lhs (list (if divide '/ '*) (nth 1 sel) alg)
X			rhs (list (if divide '/ '*) (nth 2 sel) alg))
X		  (or no-simp
X		      (progn
X			(setq lhs (math-simplify lhs)
X			      rhs (math-simplify rhs))
X			(and (eq func '/)
X			     (or (Math-equal (nth 1 sel) 1)
X				 (Math-equal (nth 1 sel) -1)
X				 (and (memq (car-safe (nth 2 sel)) '(+ -))
X				      (memq (car-safe alg) '(+ -))))
X			     (setq rhs (math-expand-term rhs)))))
X		  (setq alg (calc-encase-atoms
X			     (calc-normalize (list func lhs rhs)))))
X	      (setq rhs (list (if divide '* '/) sel alg))
X	      (or no-simp
X		  (setq rhs (math-simplify rhs)))
X	      (setq alg (calc-encase-atoms
X			 (calc-normalize (if divide
X					     (list '/ rhs alg)
X					   (list '* alg rhs))))))
X	    (calc-pop-push-record-list 1 (if divide "div" "mult")
X				       (list (calc-replace-sub-formula
X					      expr sel alg))
X				       num
X				       (list (and reselect alg)))))
X     (calc-handle-whys)))
)
X
(defun calc-sel-div-both-sides (no-simp)
X  (interactive "P")
X  (calc-sel-mult-both-sides no-simp t)
)
X
(defun calc-sel-add-both-sides (no-simp &optional subtract)
X  (interactive "P")
X  (calc-wrapper
X   (calc-preserve-point)
X   (let* ((num (max 1 (calc-locate-cursor-element (point))))
X	  (reselect calc-keep-selection)
X	  (entry (calc-top num 'entry))
X	  (expr (car entry))
X	  (sel (or (calc-auto-selection entry) expr))
X	  (func (car-safe sel))
X	  alg lhs rhs)
X     (setq alg (calc-with-default-simplification
X		(car (calc-do-alg-entry ""
X					(if subtract
X					    "Subtract from both sides: "
X					  "Add to both sides: ")))))
X     (and alg
X	  (progn
X	    (if (and (assq func calc-tweak-eqn-table)
X		     (= (length sel) 3))
X		(progn
X		  (setq lhs (list (if subtract '- '+) (nth 1 sel) alg)
X			rhs (list (if subtract '- '+) (nth 2 sel) alg))
X		  (or no-simp
X		      (setq lhs (math-simplify lhs)
X			    rhs (math-simplify rhs)))
X		  (setq alg (calc-encase-atoms
X			     (calc-normalize (list func lhs rhs)))))
X	      (setq rhs (list (if subtract '+ '-) sel alg))
X	      (or no-simp
X		  (setq rhs (math-simplify rhs)))
X	      (setq alg (calc-encase-atoms
X			 (calc-normalize (list (if subtract '- '+) alg rhs)))))
X	    (calc-pop-push-record-list 1 (if subtract "sub" "add")
X				       (list (calc-replace-sub-formula
X					      expr sel alg))
SHAR_EOF
true || echo 'restore of calc-sel.el failed'
fi
echo 'End of  part 26'
echo 'File calc-sel.el is continued in part 27'
echo 27 > _shar_seq_.tmp
exit 0
exit 0 # Just in case...
-- 
Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
Sterling Software, IMD           UUCP:     uunet!sparky!kent
Phone:    (402) 291-8300         FAX:      (402) 291-4362
Please send comp.sources.misc-related mail to kent@uunet.uu.net.
