
(require (quote calc-ext))

(defun calc-Need-calc-alg nil (byte-code "" [nil] 1))

(defun calc-alg-evaluate (arg) (interactive "p") (byte-code "\"" [nil calc-do (lambda nil (byte-code ">? !!#))" [calc-simplify-mode math-simplify-only nil arg (none num) calc-modify-simplify-mode calc-enter-result 1 "dsmp" calc-top] 6)) 115] 3))

(defun calc-modify-simplify-mode (arg) (byte-code "!U ĉ !Y ƉW$ 	C" [arg calc-simplify-mode math-abs 2 alg 3 ext 0] 4))

(defun calc-simplify nil (interactive) (byte-code "\"" [nil calc-do (lambda nil (byte-code ">? !!#)" [calc-simplify-mode (none num) calc-enter-result 1 "simp" math-simplify calc-top-n] 6)) 528] 3))

(defun calc-simplify-extended nil (interactive) (byte-code "\"" [nil calc-do (lambda nil (byte-code ">? !!#)" [calc-simplify-mode (none num) calc-enter-result 1 "esmp" math-simplify-extended calc-top-n] 6)) 746] 3))

(defun calc-expand-formula (arg) (interactive "p") (byte-code "\"" [nil calc-do (lambda nil (byte-code ">? !V! !)0 !!/ )#))" [calc-simplify-mode math-simplify-only nil arg math-expand-formulas t top (none num) calc-modify-simplify-mode calc-enter-result 1 "expf" 0 calc-top-n math-expand-formula] 8)) 982] 3))

(defun calc-factor (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "  Ă #" [arg calc-unary-op "fctr" calc-is-hyperbolic calcFunc-factors calcFunc-factor] 5)) 1332] 3))

(defun calc-expand (n) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "!D !C\"#" [n calc-enter-result 1 "expa" append calcFunc-expand calc-top-n prefix-numeric-value] 8)) 1545] 3))

(defun calc-collect (&optional var) (interactive "sCollect terms involving: ") (byte-code "\"" [nil calc-do (lambda nil (byte-code "\" \" ? !B#= !!=1 8\"!E#)" [var equal "" "$" calc-enter-result 2 "clct" calcFunc-collect calc-top-list-n math-read-expr car-safe error "Bad format in expression: %s" 1 calc-top-n] 14)) 1769] 3))

(defun calc-apart (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "#" [arg calc-unary-op "aprt" calcFunc-apart] 4)) 2143] 3))

(defun calc-normalize-rat (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "#" [arg calc-unary-op "nrat" calcFunc-nrat] 4)) 2309] 3))

(defun calc-poly-gcd (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "#" [arg calc-binary-op "pgcd" calcFunc-pgcd] 4)) 2482] 3))

(defun calc-poly-div (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "
# 
?R !\":> @>?; @=8 8=; !A =?O \"\"R !" [calc-poly-div-remainder nil arg calc-binary-op "pdiv" calcFunc-pdiv calc-clear-command-flag clear-message calc-record "prem" (bigpos bigneg) float 1 0 math-zerop message "(Remainder was %s)" math-format-flat-expr "(No remainder)"] 9)) 2651] 3))

(defun calc-poly-rem (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "#" [arg calc-binary-op "prem" calcFunc-prem] 4)) 3090] 3))

(defun calc-poly-div-rem (arg) (interactive "P") (byte-code "\"" [nil calc-do (lambda nil (byte-code "  # #" [arg calc-is-hyperbolic calc-binary-op "pdvr" calcFunc-pdivide calcFunc-pdivrem] 6)) 3259] 3))

(defun calc-substitute (&optional oldname newname) (interactive "sSubstitute old: ") (byte-code "\"" [nil calc-do (lambda nil (byte-code "\" \" ?- !!!Ή = Q\"\"N \"N ?_ !!͉ ;j !k 	!=| 	8\"!; ! != 8\"\" !
	##," [old new num expr oldname newname unread-command-char nil 1 equal "" "$" calc-top-n 2 3 read-string "Substitute old: " ", new: " math-read-expr car-safe error "Bad format in expression: %s" math-expr-contains "No occurrences found." calc-enter-result "sbst" math-expr-subst] 26)) 3484] 3))

(defun calc-has-rules (name) (byte-code "!: @> " [name calc-var-value (vec calcFunc-assign calcFunc-condition)] 3))

(defun math-recompile-eval-rules nil (byte-code "!	 !\"!" [math-eval-rules-cache math-eval-rules-cache-other nil math-eval-rules-cache-tag calc-has-rules var-EvalRules math-compile-rewrites (var EvalRules var-EvalRules) assq calc-var-value] 6))

(defun math-expand-formula (expr) (byte-code ":/ @9/ @N @N/ @A\")	!@=?. 	)" [expr res math-expand-formulas t calc-user-defn math-expandable apply car-safe] 4))

(defun math-beforep (a b) (byte-code ":?
 @> 	:? 	@>D 	\"
=@ 
=@ 	\"?@ !>G	!>GV)	\"O Â\"Z Ă\"e Â	\"p Ă:?z @> 	!= 	! 	8\" 	8\"U Ă Â Ă	:? 	@> != ! 8	\" Ă Â Â@= 	@= ! 	!p8	8\"
=Ăl
=Âl8>	8>&Ăl8>3	8>:Âl8	8\"=MĂl
=WÂl8>d	8>kĂl):?z@>?	:?	@>?=?:?@>@=	@=8!	8!\"	:?	@>?	@=:?@>@	@=A	A@	@\"È	?@	@\"@!	@!\"" [a b comp nil t (bigpos bigneg frac float) (bigpos bigneg frac float) math-compare -1 0 equal car-safe (bigneg nil bigpos frac float) (bigneg nil bigpos frac float) (neg (var inf var-inf)) (neg (var inf var-inf)) (var inf var-inf) (var inf var-inf) (bigpos bigneg frac float) intv math-intv-constp math-beforep 2 (bigpos bigneg frac float) 1 (2 3) (0 1) (0 1) (2 3) 3 (0 2) (1 3) (bigpos bigneg frac float cplx polar hms date sdev intv mod) (bigpos bigneg frac float cplx polar hms date sdev intv mod) (bigpos bigneg frac float cplx polar hms date sdev intv mod) var string-lessp symbol-name (bigpos bigneg frac float cplx polar) (bigpos bigneg frac float cplx polar)] 29))

(defun math-simplify-extended (a) (byte-code "
!)" [math-living-dangerously t a math-simplify] 2))

(fset (quote calcFunc-esimplify) (symbol-function (quote math-simplify-extended)))

(defun math-simplify (top-expr) (byte-code ":!  ! \" !\" . !. $
k 	
!!!
	g 
	@#
	A	N ) >?s 	
!	 	@\"	A	 
!\"?) 
t ),
" [math-simplifying t top-only calc-simplify-mode simp-rules math-living-dangerously math-simplifying-units math-integrating res r top-expr append calc-has-rules var-AlgSimpRules ((var AlgSimpRules var-AlgSimpRules)) var-ExtSimpRules ((var ExtSimpRules var-ExtSimpRules)) var-UnitSimpRules ((var UnitSimpRules var-UnitSimpRules)) var-IntegSimpRules ((var IntegSimpRules var-IntegSimpRules)) nil math-simplify-step math-normalize (nil) math-rewrite (neg (var inf var-inf)) (none num) equal] 16))

(fset (quote calcFunc-simplify) (symbol-function (quote math-simplify)))

(defun math-simplify-step (a) (byte-code ":?
 @> Y 
 @>  ( @A\"B	@9V 	@NU J @	\"F 	\"U A8 )	)" [a aa top-only handler (bigpos bigneg frac float cplx polar hms date mod var) (calcFunc-quote calcFunc-condition calcFunc-evalto) mapcar math-simplify-step math-simplify equal funcall] 6))

(defun math-need-std-simps nil (byte-code "" [nil] 1))

(math-defsimplify (+ -) (math-simplify-plus))

(defun math-simplify-plus nil (byte-code "8!>. 88:? 88@>. 8:?- 8@>?^ 8@AA88\"8@\"8AA	\"8
\"* @= 8:?s 8@> 8:? 8@>? 8AA8\"A	\")8!> 88@=@=% AA\"\"AA\"8 8@=%AA\"\"A\"+" [expr x op aa aaa temp t nil car-safe 1 (+ -) 2 (bigpos bigneg frac float cplx polar) (bigpos bigneg frac float cplx polar) setcar + (bigpos bigneg frac float cplx polar) (bigpos bigneg frac float cplx polar) (+ -) math-combine-sum - 0] 18))

(math-defsimplify * (math-simplify-times))

(defun math-simplify-times nil (byte-code "8!=D 888\"A 8\"( 88\"A 8A88\"8A
\")t 88\"t 8\"^ 8\"t 8AA8\"A
\")8!8:? 8@> 8! AA8\"\"A8\"\"8!=  88% A\"A\" 8\"8 8%A\"AA\"8!=,88>C888\"88\"D-" [expr t x aa aaa temp safe scalar nil car-safe 2 * math-beforep 1 math-known-scalarp setcar (bigpos bigneg frac) math-common-constant-factor math-cancel-common-factor math-mul math-combine-prod frac (1 -1) math-div] 32))

(math-defsimplify / (math-simplify-divide))

(defun math-simplify-divide nil (byte-code "	A	@= 	8:? 	8@>?! 	8! 	@=C 	8:?B 	8@>?K 	8!!=` 8=` ? 	A8	8\"\"	AA	8\"\"! 	@\" 	8\"  \"=? 	A	8\"\"	AA	8\"\"! 	@\" 	8\"@!= 	8\",	8A@!=8\"A	AA$AA 	AA$	8AA@!=T8\"JA	AA
$AA-	AA
$	-" [np expr nover nil nn n op calc-tweak-eqn-table t / 2 (bigpos bigneg frac float) math-common-constant-factor 1 (bigpos bigneg frac float) car-safe frac setcar math-mul math-cancel-common-factor math-negp assq math-frac-gcd math-known-scalarp * math-simplify-divisor] 32))

(defun math-simplify-divisor (np dp nover dover) (byte-code "@!=) 	@A
$@8\"& 	@AA
?$ @=N 	@!>M @>H =M )U 	@! 	@!
	@	!=  		A\" 	8\"	AAq  	\"." [dp np nover dover t expr signs math-living-dangerously n d dd temp op safe scalar car-safe / math-simplify-divisor math-known-scalarp 1 math-possible-signs (1 4) (calcFunc-eq calcFunc-neq) 5 math-numberp nil * math-simplify-one-divisor] 14))

(defun math-simplify-one-divisor (np dp) (byte-code "	@
@%K @>?3 
@!3 @\"3 8\"	A \"B \"
\"  ? @= 
@!= 
@8:?u 
@8@> 		@
@8D\"\"

@8\"" [temp np dp nover dover t expr op calc-tweak-eqn-table math-combine-prod (/ calcFunc-eq calcFunc-neq) math-known-negp assq setcar 1 math-div / car-safe calcFunc-sqrt (bigpos bigneg) math-mul] 15))

(defun math-common-constant-factor (expr) (byte-code ":?
 @>7 :? @>' >?$ !4 !!4 ! @>c 8!8!	_ 
_ 	
\"=?_ 	* @>r 8! @= 8! 8:? 8@> 8!E" [expr f1 f2 (bigpos bigneg frac float) (bigpos bigneg frac) (0 1 -1) math-abs math-ratp math-to-simple-fraction math-common-constant-factor (+ - cplx sdev) 1 2 math-frac-gcd (* polar) / (bigpos bigneg) frac] 14))

(defun math-cancel-common-factor (expr val) (byte-code "!># A8	\"\"AA8	\"\"= !=9 8	\"8\"= 	\"" [expr val car-safe (+ - cplx sdev) setcar math-cancel-common-factor 1 2 * math-mul math-div] 11))

(defun math-frac-gcd (a b) (byte-code ":\" @>? @= 8= !% =, 	 	:N 	@>?K 	@=H 	8=K 	!Q 	=X  :?b @>o 	:?o 	@>y 	\" :? @> E	:? 	@> 	E8	8\"8	8\"\"" [a b (bigpos bigneg) float 1 0 math-zerop (bigpos bigneg) (bigpos bigneg) (bigpos bigneg) math-gcd (bigpos bigneg) frac (bigpos bigneg) math-make-frac 2] 10))

(math-defsimplify % (math-simplify-mod))

(defun math-simplify-mod nil (byte-code "8:? 8@>#8:Z 8@=W 8@=?W 8@>R 88:H 88@=O 88VW 8!_ 8V#8!	 	@! 	@8\"U? 	8	8\"	@8\"E8E\"	 	8\"? 	8! 8! 	88\" 
\"? 
	8
\"	8\"	@
\")E8
\"EE\"8\"\"		8	8\"8!\"	!	@\"\"," [expr lin t1 t2 t3 calc-prefer-frac t 2 (bigpos bigneg frac float) bigpos bigneg (frac float) 1 0 math-posp math-is-linear nil math-negp math-compare -1 % + math-mul math-mod math-equal-int math-num-integerp calcFunc-gcd * math-div math-known-integerp] 21))

(math-defsimplify (calcFunc-eq calcFunc-neq calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq) (if (= (length expr) 3) (math-simplify-ineq)))

(defun math-simplify-ineq nil (byte-code "	A@!>& 
AA	AA
@=$
A 	AA	A=$ 	AB!	@=X =K ͆U >U ς 	@=s =f φp >p ͂ 	@= = ͆ > ς 	@= = ͆ > ς 	@= = φ > ͂ 	@= = φ > ͆ 	)*" [np expr n nil signs car-safe (+ -) math-simplify-add-term - math-simplify-divide math-possible-signs calcFunc-eq 2 1 (1 4 5) 0 calcFunc-neq (1 4 5) calcFunc-lt (2 4 6) calcFunc-gt 4 (1 2 3) calcFunc-leq (1 2 3) calcFunc-geq (2 4 6)] 8))

(defun math-simplify-add-term (np dp minus lplain) (byte-code "@! @@!> ȉ8	@=% 
D !	=d 	U !W \"AA\" \"AA@={ !} \"A 	% 
 	? !	= 	 ! \"\" \"!\"-" [np rplain t n d dd temp dp nil minus lplain math-vectorp car-safe (+ -) math-combine-sum 2 + math-looks-negp setcar math-neg 0] 21))

(math-defsimplify calcFunc-sin (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arcsin)) (nth 1 (nth 1 expr))) (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-sin) (math-neg (nth 1 expr))))) (and (eq calc-angle-mode (quote rad)) (let ((n (math-linear-in (nth 1 expr) (quote (var pi var-pi))))) (and n (math-known-sin (car n) (nth 1 n) 120 0)))) (and (eq calc-angle-mode (quote deg)) (let ((n (math-integer-plus (nth 1 expr)))) (and n (math-known-sin (car n) (nth 1 n) (quote (frac 2 3)) 0)))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arccos)) (list (quote calcFunc-sqrt) (math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arctan)) (math-div (nth 1 (nth 1 expr)) (list (quote calcFunc-sqrt) (math-add 1 (math-sqr (nth 1 (nth 1 expr))))))) (let ((m (math-should-expand-trig (nth 1 expr)))) (and m (integerp (car m)) (let ((n (car m)) (a (nth 1 m))) (list (quote +) (list (quote *) (list (quote calcFunc-sin) (list (quote *) (1- n) a)) (list (quote calcFunc-cos) a)) (list (quote *) (list (quote calcFunc-cos) (list (quote *) (1- n) a)) (list (quote calcFunc-sin) a))))))))

(math-defsimplify calcFunc-cos (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arccos)) (nth 1 (nth 1 expr))) (and (math-looks-negp (nth 1 expr)) (list (quote calcFunc-cos) (math-neg (nth 1 expr)))) (and (eq calc-angle-mode (quote rad)) (let ((n (math-linear-in (nth 1 expr) (quote (var pi var-pi))))) (and n (math-known-sin (car n) (nth 1 n) 120 300)))) (and (eq calc-angle-mode (quote deg)) (let ((n (math-integer-plus (nth 1 expr)))) (and n (math-known-sin (car n) (nth 1 n) (quote (frac 2 3)) 300)))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arcsin)) (list (quote calcFunc-sqrt) (math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arctan)) (math-div 1 (list (quote calcFunc-sqrt) (math-add 1 (math-sqr (nth 1 (nth 1 expr))))))) (let ((m (math-should-expand-trig (nth 1 expr)))) (and m (integerp (car m)) (let ((n (car m)) (a (nth 1 m))) (list (quote -) (list (quote *) (list (quote calcFunc-cos) (list (quote *) (1- n) a)) (list (quote calcFunc-cos) a)) (list (quote *) (list (quote calcFunc-sin) (list (quote *) (1- n) a)) (list (quote calcFunc-sin) a))))))))

(defun math-should-expand-trig (x &optional hyperbolic) (byte-code "	!
I I @! @V @\"I E 8!3 ˂4 >E 8!=E =I )" [m x math-living-dangerously math-integrating hyperbolic math-is-multiple integerp 1 equal (frac 1 2) car-safe (calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh) (calcFunc-arcsin calcFunc-arccos calcFunc-arctan) calcFunc-ln exp] 7))

(defun math-known-sin (plus n mul off) (byte-code "	\"!u !
\"\"u Y4 Z$1 !u V? Z!X M >U \"Au =f D!u =t D!u Ƈ" [n mul off plus calc-symbolic-mode t nil math-mul math-num-integerp math-mod math-add math-trunc 240 120 math-known-sin 1 0 math-neg 60 math-zerop (0 20 60) assq ((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)) math-normalize calcFunc-sin calcFunc-cos] 13))

(math-defsimplify calcFunc-tan (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arctan)) (nth 1 (nth 1 expr))) (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-tan) (math-neg (nth 1 expr))))) (and (eq calc-angle-mode (quote rad)) (let ((n (math-linear-in (nth 1 expr) (quote (var pi var-pi))))) (and n (math-known-tan (car n) (nth 1 n) 120)))) (and (eq calc-angle-mode (quote deg)) (let ((n (math-integer-plus (nth 1 expr)))) (and n (math-known-tan (car n) (nth 1 n) (quote (frac 2 3)))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arcsin)) (math-div (nth 1 (nth 1 expr)) (list (quote calcFunc-sqrt) (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arccos)) (math-div (list (quote calcFunc-sqrt) (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))) (nth 1 (nth 1 expr)))) (let ((m (math-should-expand-trig (nth 1 expr)))) (and m (if (equal (car m) (quote (frac 1 2))) (math-div (math-sub 1 (list (quote calcFunc-cos) (nth 1 m))) (list (quote calcFunc-sin) (nth 1 m))) (math-div (list (quote calcFunc-sin) (nth 1 expr)) (list (quote calcFunc-cos) (nth 1 expr))))))))

(defun math-known-tan (plus n mul) (byte-code "	\"!h !\"h V0 
Z#- !h 
!H = >E \"Ah =V 
D!h =g 
DE!h Ň" [n mul plus calc-symbolic-mode t nil math-mul math-num-integerp math-mod math-trunc 120 60 math-known-tan 1 math-neg math-zerop (0 30 60) assq ((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)) 0 math-normalize calcFunc-tan / -1] 14))

(math-defsimplify calcFunc-sinh (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arcsinh)) (nth 1 (nth 1 expr))) (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-sinh) (math-neg (nth 1 expr))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arccosh)) math-living-dangerously (list (quote calcFunc-sqrt) (math-sub (math-sqr (nth 1 (nth 1 expr))) 1))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arctanh)) math-living-dangerously (math-div (nth 1 (nth 1 expr)) (list (quote calcFunc-sqrt) (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))) (let ((m (math-should-expand-trig (nth 1 expr) t))) (and m (integerp (car m)) (let ((n (car m)) (a (nth 1 m))) (if (> n 1) (list (quote +) (list (quote *) (list (quote calcFunc-sinh) (list (quote *) (1- n) a)) (list (quote calcFunc-cosh) a)) (list (quote *) (list (quote calcFunc-cosh) (list (quote *) (1- n) a)) (list (quote calcFunc-sinh) a)))))))))

(math-defsimplify calcFunc-cosh (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arccosh)) (nth 1 (nth 1 expr))) (and (math-looks-negp (nth 1 expr)) (list (quote calcFunc-cosh) (math-neg (nth 1 expr)))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arcsinh)) math-living-dangerously (list (quote calcFunc-sqrt) (math-add (math-sqr (nth 1 (nth 1 expr))) 1))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arctanh)) math-living-dangerously (math-div 1 (list (quote calcFunc-sqrt) (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))) (let ((m (math-should-expand-trig (nth 1 expr) t))) (and m (integerp (car m)) (let ((n (car m)) (a (nth 1 m))) (if (> n 1) (list (quote +) (list (quote *) (list (quote calcFunc-cosh) (list (quote *) (1- n) a)) (list (quote calcFunc-cosh) a)) (list (quote *) (list (quote calcFunc-sinh) (list (quote *) (1- n) a)) (list (quote calcFunc-sinh) a)))))))))

(math-defsimplify calcFunc-tanh (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arctanh)) (nth 1 (nth 1 expr))) (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-tanh) (math-neg (nth 1 expr))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arcsinh)) math-living-dangerously (math-div (nth 1 (nth 1 expr)) (list (quote calcFunc-sqrt) (math-add (math-sqr (nth 1 (nth 1 expr))) 1)))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-arccosh)) math-living-dangerously (math-div (list (quote calcFunc-sqrt) (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))) (and m (if (equal (car m) (quote (frac 1 2))) (math-div (math-sub (list (quote calcFunc-cosh) (nth 1 m)) 1) (list (quote calcFunc-sinh) (nth 1 m))) (math-div (list (quote calcFunc-sinh) (nth 1 expr)) (list (quote calcFunc-cosh) (nth 1 expr))))))))

(math-defsimplify calcFunc-arcsin (or (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-arcsin) (math-neg (nth 1 expr))))) (and (eq (nth 1 expr) 1) (math-quarter-circle t)) (and (equal (nth 1 expr) (quote (frac 1 2))) (math-div (math-half-circle t) 6)) (and math-living-dangerously (eq (car-safe (nth 1 expr)) (quote calcFunc-sin)) (nth 1 (nth 1 expr))) (and math-living-dangerously (eq (car-safe (nth 1 expr)) (quote calcFunc-cos)) (math-sub (math-quarter-circle t) (nth 1 (nth 1 expr))))))

(math-defsimplify calcFunc-arccos (or (and (eq (nth 1 expr) 0) (math-quarter-circle t)) (and (eq (nth 1 expr) -1) (math-half-circle t)) (and (equal (nth 1 expr) (quote (frac 1 2))) (math-div (math-half-circle t) 3)) (and (equal (nth 1 expr) (quote (frac -1 2))) (math-div (math-mul (math-half-circle t) 2) 3)) (and math-living-dangerously (eq (car-safe (nth 1 expr)) (quote calcFunc-cos)) (nth 1 (nth 1 expr))) (and math-living-dangerously (eq (car-safe (nth 1 expr)) (quote calcFunc-sin)) (math-sub (math-quarter-circle t) (nth 1 (nth 1 expr))))))

(math-defsimplify calcFunc-arctan (or (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-arctan) (math-neg (nth 1 expr))))) (and (eq (nth 1 expr) 1) (math-div (math-half-circle t) 4)) (and math-living-dangerously (eq (car-safe (nth 1 expr)) (quote calcFunc-tan)) (nth 1 (nth 1 expr)))))

(math-defsimplify calcFunc-arcsinh (or (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-arcsinh) (math-neg (nth 1 expr))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-sinh)) (or math-living-dangerously (math-known-realp (nth 1 (nth 1 expr)))) (nth 1 (nth 1 expr)))))

(math-defsimplify calcFunc-arccosh (and (eq (car-safe (nth 1 expr)) (quote calcFunc-cosh)) (or math-living-dangerously (math-known-realp (nth 1 (nth 1 expr)))) (nth 1 (nth 1 expr))))

(math-defsimplify calcFunc-arctanh (or (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-arctanh) (math-neg (nth 1 expr))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-tanh)) (or math-living-dangerously (math-known-realp (nth 1 (nth 1 expr)))) (nth 1 (nth 1 expr)))))

(math-defsimplify calcFunc-sqrt (math-simplify-sqrt))

(defun math-simplify-sqrt nil (byte-code "8!= 8888\"D88\"8!2 8!7 8!	T 	=?T 	D!8	\"D!\")
8!= 88\" 88!= 888\" 888!= 8888D 888!= 8888D8!=	88\"	88!=	888\"	888!=	8888D8!=v8888\"2889\"u!=u8\"u8!=^88Du8!=u88DE*8!=8888\"E8!=88\"E8!>8@88D88DE8!>8!?8!!!GV88V	Ae@8@8		V>		\"\"\"	\"		Va	!>Y	\"a	\"=q>?\"D\"-)" [expr fac math-living-dangerously a b f out rest sums pow car-safe 1 frac math-div calcFunc-sqrt math-mul 2 math-objectp math-squared-factor math-common-constant-factor math-normalize math-cancel-common-factor - math-equal-int ^ calcFunc-sin calcFunc-cos calcFunc-cosh calcFunc-sinh + calcFunc-tan / 4 (* /) (+ -) math-any-floats calcFunc-factors calcFunc-expand math-vectorp nil math-pow % 0 (+ -) math-mul-thru (1 -1)] 51))

(defun math-squared-factor (x) (byte-code ":?
 @>< 	9 	@\"A=1 @
	@\"5 	A 
+" [x prsqr fac res (bigpos bigneg) (4 9 25 49 121 169 289 361 529 841) 1 nil math-idivmod 0 math-mul] 5))

(math-defsimplify calcFunc-exp (math-simplify-exp (nth 1 expr)))

(defun math-simplify-exp (x) (byte-code "!= 8 	} !=) 88!\"D\"} !=C 88!\"D\"} !=` 8\"D8\"D\"} \"
| 
@!| 
8D
@E)  ! !\" @8$ @8$ EE," [x math-living-dangerously m calc-symbolic-mode ip n s c car-safe calcFunc-ln 1 calcFunc-arcsinh math-add calcFunc-sqrt math-sqr calcFunc-arccosh math-sub calcFunc-arctanh math-div math-should-expand-trig exp integerp ^ calcFunc-exp math-known-imagp calcFunc-im math-linear-in (var pi var-pi) nil math-known-sin 120 0 300 + * (var i var-i)] 25))

(math-defsimplify calcFunc-ln (or (and (eq (car-safe (nth 1 expr)) (quote calcFunc-exp)) (or math-living-dangerously (math-known-realp (nth 1 (nth 1 expr)))) (nth 1 (nth 1 expr))) (and (eq (car-safe (nth 1 expr)) (quote ^)) (equal (nth 1 (nth 1 expr)) (quote (var e var-e))) (or math-living-dangerously (math-known-realp (nth 2 (nth 1 expr)))) (nth 2 (nth 1 expr))) (and calc-symbolic-mode (math-known-negp (nth 1 expr)) (math-add (list (quote calcFunc-ln) (math-neg (nth 1 expr))) (quote (var pi var-pi)))) (and calc-symbolic-mode (math-known-imagp (nth 1 expr)) (let* ((ip (calcFunc-im (nth 1 expr))) (ips (math-possible-signs ip))) (or (and (memq ips (quote (4 6))) (math-add (list (quote calcFunc-ln) ip) (quote (/ (* (var pi var-pi) (var i var-i)) 2)))) (and (memq ips (quote (1 3))) (math-sub (list (quote calcFunc-ln) (math-neg ip)) (quote (/ (* (var pi var-pi) (var i var-i)) 2)))))))))

(math-defsimplify ^ (math-simplify-pow))

(defun math-simplify-pow nil (byte-code "_ 	8!= 	88	8	88\"E_ 	8!=9 	88	8\"E_ 	8!>_ 	8@	88	8E	88	8EE6	8\"z 	8!=z 	886	8\" 	8!6	8!= 
? 	88	8\"D6	8\"   	8! 	8!\"= ǂ = 	8 = ܂ = 	8!)6
U	8!U	8YU	8!=0	8	8Z\"	88D!\"\"U	8!=U	8	8Z\"	88D!\"\"6	8!=	8:?p	8@>	8:	8@=	8@=?	8@>	88:	88@=	88V	8!	8V	8\"	8D	8!:@>?@=8=! =?	8E	8	8\"EE)6	8!=6 5	8\"E)" [math-living-dangerously expr math-integrating x flr temp car-safe 1 ^ math-mul 2 calcFunc-sqrt math-div (* /) math-equal-int 10 calcFunc-log10 equal (var e var-e) math-simplify-exp calcFunc-exp (var i var-i) math-imaginary-i math-num-integerp math-mod math-trunc 4 0 -1 3 math-neg integerp calcFunc-cos math-pow math-sub math-sqr calcFunc-sin calcFunc-cosh math-add calcFunc-sinh frac (bigpos bigneg frac) bigpos bigneg (frac float) math-posp (frac 1 2) math-floor (bigpos bigneg) float math-zerop * math-quarter-integer math-simplify-sqrt] 41))

(math-defsimplify calcFunc-log10 (and (eq (car-safe (nth 1 expr)) (quote ^)) (math-equal-int (nth 1 (nth 1 expr)) 10) (or math-living-dangerously (math-known-realp (nth 2 (nth 1 expr)))) (nth 2 (nth 1 expr))))

(math-defsimplify calcFunc-erf (or (and (math-looks-negp (nth 1 expr)) (math-neg (list (quote calcFunc-erf) (math-neg (nth 1 expr))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-conj)) (list (quote calcFunc-conj) (list (quote calcFunc-erf) (nth 1 (nth 1 expr)))))))

(math-defsimplify calcFunc-erfc (or (and (math-looks-negp (nth 1 expr)) (math-sub 2 (list (quote calcFunc-erfc) (math-neg (nth 1 expr))))) (and (eq (car-safe (nth 1 expr)) (quote calcFunc-conj)) (list (quote calcFunc-conj) (list (quote calcFunc-erfc) (nth 1 (nth 1 expr)))))))

(defun math-linear-in (expr term &optional always) (byte-code "	\" 	#A *    D" [expr term calc-prefer-frac t p always math-expr-contains math-is-polynomial 1 0] 5))

(defun math-multiple-of (expr term) (byte-code "	
\" @! 8)" [p expr term math-linear-in math-zerop 1] 4))

(defun math-integer-plus (expr) (byte-code ":?
 @> Dv @>( 8:?( 8@>D @=8 8= 8!8Dv @>Y 8:?Y 8@>u 8@=l 8q 8!Dv " [expr t nil (bigpos bigneg) 0 (+ -) 1 (bigpos bigneg) + 2 math-neg (+ -) (bigpos bigneg)] 5))

(defun math-is-linear (expr &optional always) (byte-code "!=F 8:? 8@>) 88C 8:?7 8@>C 88 != 8:?\\ 8@>m 88! 8:?{ 8@> 8!8\" 
@ 
8 E 
 
B*" [offset nil coef expr always car-safe + 1 (bigpos bigneg frac float cplx polar hms date sdev intv mod) 2 (bigpos bigneg frac float cplx polar hms date sdev intv mod) - (bigpos bigneg frac float cplx polar hms date sdev intv mod) math-neg (bigpos bigneg frac float cplx polar hms date sdev intv mod) math-is-multiple 0] 9))

(defun math-is-multiple (expr &optional always) (byte-code "!=# 8:? 8@>  88D != 8:?9 8@>C 8\"?S 88\"D 8:?a 8@>q 8\"8D 8!	 	@888\"D 8!	 	@\"888\"D) != 8D :? @> 
= D 
 D" [expr res always car-safe * 1 (bigpos bigneg frac float cplx polar hms date sdev intv mod) 2 / (bigpos bigneg frac float cplx polar hms date sdev intv mod) math-equal-int math-div (bigpos bigneg frac float cplx polar hms date sdev intv mod) math-is-multiple neg -1 (bigpos bigneg frac float cplx polar hms date sdev intv mod vec)] 15))

(defun calcFunc-lin (expr &optional var) (byte-code " 
#	 
\"	@	8F)1 
\"	, 
\"	B)" [var res expr t math-linear-in math-reject-arg "Linear term expected" vec 1 math-is-linear] 6))

(defun calcFunc-linnt (expr &optional var) (byte-code " 
\"	 
\"	@	8F)/ 
!	* 
\"	B)" [var res expr math-linear-in math-reject-arg "Linear term expected" vec 1 math-is-linear] 6))

(defun calcFunc-islin (expr &optional var) (byte-code ":?
 @> 	? Â 	\"Ň" [expr var (bigpos bigneg frac float cplx polar hms date sdev intv mod vec) 0 calcFunc-lin 1] 3))

(defun calcFunc-islinnt (expr &optional var) (byte-code ":?
 @> Â 	\"Ň" [expr var (bigpos bigneg frac float cplx polar hms date sdev intv mod vec) 0 calcFunc-linnt 1] 3))

(defun math-expr-contains-count (expr thing) (byte-code "	\" Ƃ? :? @> ? A6 @	\"/ \\ V> )" [expr thing nil t num equal 1 (bigpos bigneg frac float cplx polar hms date mod var) 0 math-expr-contains-count] 6))

(defun math-expr-contains (expr thing) (byte-code "	\" ł3 :? @> 3 A) @	\"?1  " [expr thing nil t equal 1 (bigpos bigneg frac float cplx polar hms date mod var) math-expr-contains] 4))

(defun math-expr-depends (expr thing) (byte-code ":?
 @> != 	\"3 A) 	@\"?1 ǈ " [thing expr (bigpos bigneg frac float cplx polar hms date mod var) car-safe var math-expr-contains math-expr-depends nil] 5))

(defun math-expr-subst (expr old new) (byte-code "!" [expr math-expr-subst-rec] 2))

(fset (quote calcFunc-subst) (symbol-function (quote math-expr-subst)))

(defun math-expr-subst-rec (expr) (byte-code "	\" 
U :? @> U @>M GU? 8	\"; 
C\"< J @88!EU @A\"B" [expr old new t equal (bigpos bigneg frac float cplx polar hms date mod var) (calcFunc-deriv calcFunc-tderiv) 2 1 append math-expr-subst-rec mapcar] 8))

(defun math-expr-weight (expr) (byte-code ":?
 @> Â) A& 	@!\\ 	)" [expr w (bigpos bigneg frac float cplx polar hms date mod var) 1 math-expr-weight] 4))

(defun math-expr-height (expr) (byte-code ":?
 @> Â* A& 	@!] 	T)" [expr h (bigpos bigneg frac float cplx polar hms date mod var) 0 math-expr-height] 4))

(defun calcFunc-collect (expr base) (byte-code "	
$A 
\"! 	)" [p expr base t math-is-polynomial 50 math-normalize math-build-polynomial-expr] 5))

(defun math-is-polynomial (expr var &optional degree loose) (byte-code "	 	= 
 Ȃ \"?% GTX) *" [math-poly-base-variable loose var poly expr math-poly-neg-powers degree gen (var XXX XXX) math-is-poly-rec] 3))

(defun math-is-poly-rec (expr negpow) (byte-code "	\" !=r	\"$ 88= 
\"@!= @8:j @8@>?g @8@=a @88=g @8!p @8= @88\"D 8\" 8 	\" = @)
: 
!
!=  	\" 
8\"> 
\"
!n
U 	\" Dn
!O	\"	D\"
C	K
?,GS\"
XKYH	\"	S/	+nn\"n
[\"Bn
EC*:?|@>C@>8\"8\"@=Ԃ$))@=8\"\"@=8\"8\"
?GG\\Z
X\"))@=8	\"?>>8\">8B8:m8@>?j8@=e88=j8!r8=?8\"\")@=	\"	8E\"@=8E\"ǆ	\"?@=?C!" [expr var pow math-poly-mult-powers m math-poly-frac-powers p1 nil n accum degree negpow math-poly-neg-powers p2 t loose math-poly-simplify equal car-safe ^ 1 2 math-is-multiple cplx (bigpos bigneg) float 0 math-zerop math-mul (var i var-i) math-to-simple-fraction frac calcFunc-lcm (1 nil) integerp natnump math-is-poly-rec * math-poly-mul math-pow (bigpos bigneg frac float cplx polar hms date sdev intv mod) (+ -) math-poly-mix + -1 neg mapcar math-neg / math-poly-depends (bigpos bigneg) (lambda (x) (byte-code "	8\"" [x expr math-div 2] 4)) calcFunc-exp (var e var-e) calcFunc-sqrt (frac 1 2) vec] 43))

(defun math-polynomial-p (expr var) (byte-code "	\" ǂ :? @> ɂ @>B 8	\"
> 8	\"> 
]* @=h 8	\"
d 8	\"d 
\\* @=x 8	\" @= 8	\"? 8	\" @= 8! 8	\"
 
8\") 	\" Ă ɇ" [expr var p1 p2 nil t equal 1 (bigpos bigneg frac float cplx polar hms date mod var) 0 (+ -) math-polynomial-p 2 * neg / math-poly-depends ^ natnump] 14))

(defun math-poly-depends (expr var) (byte-code " 	\" 	
\"" [math-poly-base-variable expr var math-expr-contains math-expr-depends] 4))

(defun math-polynomial-base (mpb-top-expr &optional mpb-pred) (byte-code " ŉ!) !)" [mpb-pred const-ok nil mpb-top-expr t (lambda (base) (byte-code "	\"" [mpb-top-expr base math-polynomial-p] 3)) math-polynomial-base-rec] 3))

(defun math-polynomial-base-rec (mpb-expr) (byte-code ":?
 @>?b @>\" 8!\" 8!b @>1 8!b @=@ 8!b @=M !b 	W !b 
\"b " [mpb-expr const-ok mpb-pred (bigpos bigneg frac float cplx polar hms date sdev intv mod vec) (+ - *) math-polynomial-base-rec 1 2 (/ neg) ^ calcFunc-exp (var e var-e) math-expr-contains-vars funcall] 9))

(defun math-expr-contains-vars (expr) (byte-code "!=, :? @>?, A\" @!?* ň " [expr car-safe var (bigpos bigneg frac float cplx polar hms date mod var) math-expr-contains-vars nil] 3))

(defun math-poly-simplify (p) (byte-code " GS8:: GS8@>?7 GS8@=0 GS88=7 GS8!A GS8= !	A 	GS	8: 	GS	8@>? 	GS	8@=y 	GS	88= 	GS	8! 	GS	8= 	GZ	\"\"H 	) " [p pp nil (bigpos bigneg) float 1 0 math-zerop copy-sequence (bigpos bigneg) setcdr nthcdr 2] 8))

(defun math-poly-mix (a ac b bc) (byte-code " 	% @ 
\"	@ \"\"A
	A$B" [a b ac bc math-add math-mul 0 math-poly-mix] 9))

(defun math-poly-zerop (a) (byte-code "?6 A?6 @:2 @@>?/ @@=+ @8=/ @!6 @=" [a (bigpos bigneg) float 1 0 math-zerop] 3))

(defun math-poly-mul (a b) (byte-code " 	 	@A	B\"$" [a b math-poly-mix math-poly-mul 0 1] 7))

(defun math-build-polynomial-expr (p var) (byte-code " 	:? 	@>: 
\\!@A2 @	\"\" *)! !GS@	\"\"A S@! @!l тm @!} @! @	\"\"EN , ԇ" [p var calc-internal-prec rp accum n term (bigpos bigneg frac float cplx polar) math-normalize 1 reverse math-add math-mul math-pow nil math-zerop math-looks-negp - + math-neg 0] 19))

(defun math-to-simple-fraction (f) (byte-code "!=? 8Y 88\"? 8!? 8V? 8W? 88[\"\"C " [f car-safe float 2 0 math-scale-int 1 integerp -1000 1000 math-make-frac] 9))
