forked from emacs-mirror/emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc-math.el
2111 lines (1937 loc) · 68 KB
/
calc-math.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; calc-math.el --- mathematical functions for Calc
;; Copyright (C) 1990-1993, 2001-2020 Free Software Foundation, Inc.
;; Author: David Gillespie <[email protected]>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
;; This file is autoloaded from calc-ext.el.
(require 'cl-lib)
(require 'calc-ext)
(require 'calc-macs)
;;; Find out how many 9s in 9.9999... will give distinct Emacs floats,
;;; then back off by one.
(defvar math-emacs-precision
(let* ((n 1)
(x 9)
(xx (+ x (* 9 (expt 10 (- n))))))
(while (/= x xx)
(progn
(setq n (1+ n))
(setq x xx)
(setq xx (+ x (* 9 (expt 10 (- n)))))))
(1- n))
"The number of digits in an Emacs float.")
;;; Find the largest power of 10 which is an Emacs float,
;;; then back off by one so that any float d.dddd...eN
;;; is an Emacs float, for acceptable d.dddd....
(defvar math-largest-emacs-expt
(let ((x 1)
(pow 1e2))
;; The following loop is for efficiency; it should stop when
;; 10^(2x) is too large. This could be indicated by a range
;; error when computing 10^(2x) or an infinite value for 10^(2x).
(while (and
pow
(< pow 1.0e+INF))
(setq x (* 2 x))
(setq pow (condition-case nil
(expt 10.0 (* 2 x))
(error nil))))
;; The following loop should stop when 10^(x+1) is too large.
(setq pow (condition-case nil
(expt 10.0 (1+ x))
(error nil)))
(while (and
pow
(< pow 1.0e+INF))
(setq x (1+ x))
(setq pow (condition-case nil
(expt 10.0 (1+ x))
(error nil))))
(1- x))
"The largest exponent which Calc will convert to an Emacs float.")
(defvar math-smallest-emacs-expt
(let ((x -1))
(while (condition-case nil
(> (expt 10.0 x) 0.0)
(error nil))
(setq x (* 2 x)))
(setq x (/ x 2))
(while (condition-case nil
(> (expt 10.0 x) 0.0)
(error nil))
(setq x (1- x)))
(+ x 2))
"The smallest exponent which Calc will convert to an Emacs float.")
(defun math-use-emacs-fn (fn x)
"Use the native Emacs function FN to evaluate the Calc number X.
If this can't be done, return NIL."
(and
(<= calc-internal-prec math-emacs-precision)
(math-realp x)
(let* ((xpon (+ (nth 2 x) (1- (math-numdigs (nth 1 x))))))
(and (<= math-smallest-emacs-expt xpon)
(<= xpon math-largest-emacs-expt)
(condition-case nil
(math-read-number
(number-to-string
(funcall fn
(string-to-number
(let
((calc-number-radix 10)
(calc-twos-complement-mode nil)
(calc-float-format (list 'float calc-internal-prec))
(calc-group-digits nil)
(calc-point-char "."))
(math-format-number (math-float x)))))))
(error nil))))))
(defun calc-sqrt (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-inverse)
(calc-unary-op "^2" 'calcFunc-sqr arg)
(calc-unary-op "sqrt" 'calcFunc-sqrt arg))))
(defun calc-isqrt (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-inverse)
(calc-unary-op "^2" 'calcFunc-sqr arg)
(calc-unary-op "isqt" 'calcFunc-isqrt arg))))
(defun calc-hypot (arg)
(interactive "P")
(calc-slow-wrapper
(calc-binary-op "hypt" 'calcFunc-hypot arg)))
(defun calc-ln (arg)
(interactive "P")
(calc-invert-func)
(calc-exp arg))
(defun calc-log10 (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-ln arg))
(defun calc-log (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-inverse)
(calc-binary-op "alog" 'calcFunc-alog arg)
(calc-binary-op "log" 'calcFunc-log arg))))
(defun calc-ilog (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-inverse)
(calc-binary-op "alog" 'calcFunc-alog arg)
(calc-binary-op "ilog" 'calcFunc-ilog arg))))
(defun calc-lnp1 (arg)
(interactive "P")
(calc-invert-func)
(calc-expm1 arg))
(defun calc-exp (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(if (calc-is-inverse)
(calc-unary-op "lg10" 'calcFunc-log10 arg)
(calc-unary-op "10^" 'calcFunc-exp10 arg))
(if (calc-is-inverse)
(calc-unary-op "ln" 'calcFunc-ln arg)
(calc-unary-op "exp" 'calcFunc-exp arg)))))
(defun calc-expm1 (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-inverse)
(calc-unary-op "ln+1" 'calcFunc-lnp1 arg)
(calc-unary-op "ex-1" 'calcFunc-expm1 arg))))
(defun calc-pi ()
(interactive)
(calc-slow-wrapper
(if (calc-is-inverse)
(if (calc-is-hyperbolic)
(if calc-symbolic-mode
(calc-pop-push-record 0 "phi" '(var phi var-phi))
(calc-pop-push-record 0 "phi" (math-phi)))
(if calc-symbolic-mode
(calc-pop-push-record 0 "gmma" '(var gamma var-gamma))
(calc-pop-push-record 0 "gmma" (math-gamma-const))))
(if (calc-is-hyperbolic)
(if calc-symbolic-mode
(calc-pop-push-record 0 "e" '(var e var-e))
(calc-pop-push-record 0 "e" (math-e)))
(if calc-symbolic-mode
(calc-pop-push-record 0 "pi" '(var pi var-pi))
(calc-pop-push-record 0 "pi" (math-pi)))))))
(defun calc-sin (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(if (calc-is-inverse)
(calc-unary-op "asnh" 'calcFunc-arcsinh arg)
(calc-unary-op "sinh" 'calcFunc-sinh arg))
(if (calc-is-inverse)
(calc-unary-op "asin" 'calcFunc-arcsin arg)
(calc-unary-op "sin" 'calcFunc-sin arg)))))
(defun calc-arcsin (arg)
(interactive "P")
(calc-invert-func)
(calc-sin arg))
(defun calc-sinh (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-sin arg))
(defun calc-arcsinh (arg)
(interactive "P")
(calc-invert-func)
(calc-hyperbolic-func)
(calc-sin arg))
(defun calc-sec (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(calc-unary-op "sech" 'calcFunc-sech arg)
(calc-unary-op "sec" 'calcFunc-sec arg))))
(defun calc-sech (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-sec arg))
(defun calc-cos (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(if (calc-is-inverse)
(calc-unary-op "acsh" 'calcFunc-arccosh arg)
(calc-unary-op "cosh" 'calcFunc-cosh arg))
(if (calc-is-inverse)
(calc-unary-op "acos" 'calcFunc-arccos arg)
(calc-unary-op "cos" 'calcFunc-cos arg)))))
(defun calc-arccos (arg)
(interactive "P")
(calc-invert-func)
(calc-cos arg))
(defun calc-cosh (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-cos arg))
(defun calc-arccosh (arg)
(interactive "P")
(calc-invert-func)
(calc-hyperbolic-func)
(calc-cos arg))
(defun calc-csc (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(calc-unary-op "csch" 'calcFunc-csch arg)
(calc-unary-op "csc" 'calcFunc-csc arg))))
(defun calc-csch (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-csc arg))
(defun calc-sincos ()
(interactive)
(calc-slow-wrapper
(if (calc-is-inverse)
(calc-enter-result 1 "asnc" (list 'calcFunc-arcsincos (calc-top-n 1)))
(calc-enter-result 1 "sncs" (list 'calcFunc-sincos (calc-top-n 1))))))
(defun calc-tan (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(if (calc-is-inverse)
(calc-unary-op "atnh" 'calcFunc-arctanh arg)
(calc-unary-op "tanh" 'calcFunc-tanh arg))
(if (calc-is-inverse)
(calc-unary-op "atan" 'calcFunc-arctan arg)
(calc-unary-op "tan" 'calcFunc-tan arg)))))
(defun calc-arctan (arg)
(interactive "P")
(calc-invert-func)
(calc-tan arg))
(defun calc-tanh (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-tan arg))
(defun calc-arctanh (arg)
(interactive "P")
(calc-invert-func)
(calc-hyperbolic-func)
(calc-tan arg))
(defun calc-cot (arg)
(interactive "P")
(calc-slow-wrapper
(if (calc-is-hyperbolic)
(calc-unary-op "coth" 'calcFunc-coth arg)
(calc-unary-op "cot" 'calcFunc-cot arg))))
(defun calc-coth (arg)
(interactive "P")
(calc-hyperbolic-func)
(calc-cot arg))
(defun calc-arctan2 ()
(interactive)
(calc-slow-wrapper
(calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2)))))
(defun calc-conj (arg)
(interactive "P")
(calc-wrapper
(calc-unary-op "conj" 'calcFunc-conj arg)))
(defun calc-imaginary ()
(interactive)
(calc-slow-wrapper
(calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1)))))
(defun calc-to-degrees (arg)
(interactive "P")
(calc-wrapper
(calc-unary-op ">deg" 'calcFunc-deg arg)))
(defun calc-to-radians (arg)
(interactive "P")
(calc-wrapper
(calc-unary-op ">rad" 'calcFunc-rad arg)))
(defun calc-degrees-mode (arg)
(interactive "p")
(cond ((= arg 1)
(calc-wrapper
(calc-change-mode 'calc-angle-mode 'deg)
(message "Angles measured in degrees")))
((= arg 2) (calc-radians-mode))
((= arg 3) (calc-hms-mode))
(t (error "Prefix argument out of range"))))
(defun calc-radians-mode ()
(interactive)
(calc-wrapper
(calc-change-mode 'calc-angle-mode 'rad)
(message "Angles measured in radians")))
;;; Compute the integer square-root floor(sqrt(A)). A > 0. [I I] [Public]
;;; This method takes advantage of the fact that Newton's method starting
;;; with an overestimate always works, even using truncating integer division!
(defun math-isqrt (a)
(cond ((Math-zerop a) a)
((not (natnump a))
(math-reject-arg a 'natnump))
(t (cl-isqrt a))))
(defun calcFunc-isqrt (a)
(if (math-realp a)
(math-isqrt (math-floor a))
(math-floor (math-sqrt a))))
(defun math-zerop-bignum (a)
(and (eq (car a) 0)
(progn
(while (eq (car (setq a (cdr a))) 0))
(null a))))
(defun math-scale-bignum-digit-size (a n) ; [L L S]
(while (> n 0)
(setq a (cons 0 a)
n (1- n)))
a)
;;; Compute the square root of a number.
;;; [T N] if possible, else [F N] if possible, else [C N]. [Public]
(defun math-sqrt (a)
(or
(and (Math-zerop a) a)
(and (math-known-nonposp a)
(math-imaginary (math-sqrt (math-neg a))))
(and (integerp a)
(let ((sqrt (cl-isqrt a)))
(if (= (* sqrt sqrt) a)
sqrt
(if calc-symbolic-mode
(list 'calcFunc-sqrt a)
(math-sqrt-float (math-float a) (math-float sqrt))))))
(and (eq (car-safe a) 'frac)
(let* ((num-sqrt (cl-isqrt (nth 1 a)))
(num-exact (= (* num-sqrt num-sqrt) (nth 1 a)))
(den-sqrt (cl-isqrt (nth 2 a)))
(den-exact (= (* den-sqrt den-sqrt) (nth 2 a))))
(if (and num-exact den-exact)
(list 'frac num-sqrt den-sqrt)
(if calc-symbolic-mode
(if (or num-exact den-exact)
(math-div (if num-exact
num-sqrt (list 'calcFunc-sqrt (nth 1 a)))
(if den-exact
den-sqrt (list 'calcFunc-sqrt (nth 2 a))))
(list 'calcFunc-sqrt a))
(math-sqrt-float (math-float a)
(math-div (math-float num-sqrt) den-sqrt))))))
(and (eq (car-safe a) 'float)
(if calc-symbolic-mode
(if (= (% (nth 2 a) 2) 0)
(let ((res (cl-isqrt (nth 1 a))))
(if (= (* res res) (nth 1 a))
(math-make-float res (/ (nth 2 a) 2))
(signal 'inexact-result nil)))
(signal 'inexact-result nil))
(math-sqrt-float a)))
(and (eq (car-safe a) 'cplx)
(math-with-extra-prec 2
(let* ((d (math-abs a))
(imag (math-sqrt (math-mul (math-sub d (nth 1 a))
'(float 5 -1)))))
(list 'cplx
(math-sqrt (math-mul (math-add d (nth 1 a)) '(float 5 -1)))
(if (math-negp (nth 2 a)) (math-neg imag) imag)))))
(and (eq (car-safe a) 'polar)
(list 'polar
(math-sqrt (nth 1 a))
(math-mul (nth 2 a) '(float 5 -1))))
(and (eq (car-safe a) 'sdev)
(let ((sqrt (math-sqrt (nth 1 a))))
(math-make-sdev sqrt
(math-div (nth 2 a) (math-mul sqrt 2)))))
(and (eq (car-safe a) 'intv)
(not (math-negp (nth 2 a)))
(math-make-intv (nth 1 a) (math-sqrt (nth 2 a)) (math-sqrt (nth 3 a))))
(and (eq (car-safe a) '*)
(or (math-known-nonnegp (nth 1 a))
(math-known-nonnegp (nth 2 a)))
(math-mul (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a))))
(and (eq (car-safe a) '/)
(or (and (math-known-nonnegp (nth 2 a))
(math-div (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a))))
(and (math-known-nonnegp (nth 1 a))
(not (math-equal-int (nth 1 a) 1))
(math-mul (math-sqrt (nth 1 a))
(math-sqrt (math-div 1 (nth 2 a)))))))
(and (eq (car-safe a) '^)
(math-known-evenp (nth 2 a))
(math-known-realp (nth 1 a))
(math-abs (math-pow (nth 1 a) (math-div (nth 2 a) 2))))
(let ((inf (math-infinitep a)))
(and inf
(math-mul (math-sqrt (math-infinite-dir a inf)) inf)))
(progn
(calc-record-why 'numberp a)
(list 'calcFunc-sqrt a))))
(defalias 'calcFunc-sqrt 'math-sqrt)
(defun math-infinite-dir (a &optional inf)
(or inf (setq inf (math-infinitep a)))
(math-normalize (math-expr-subst a inf 1)))
(defun math-sqrt-float (a &optional guess) ; [F F F]
(if calc-symbolic-mode
(signal 'inexact-result nil)
(math-with-extra-prec 1 (math-sqrt-raw a guess))))
(defun math-sqrt-raw (a &optional guess) ; [F F F]
(if (not (Math-posp a))
(math-sqrt a)
(cond
((math-use-emacs-fn 'sqrt a))
(t
(if (null guess)
(let ((ldiff (- (math-numdigs (nth 1 a)) 6)))
(or (= (% (+ (nth 2 a) ldiff) 2) 0) (setq ldiff (1+ ldiff)))
(setq guess (math-make-float (cl-isqrt
(math-scale-int (nth 1 a) (- ldiff)))
(/ (+ (nth 2 a) ldiff) 2)))))
(math-sqrt-float-iter a guess)))))
(defun math-sqrt-float-iter (a guess) ; [F F F]
(math-working "sqrt" guess)
(let ((g2 (math-mul-float (math-add-float guess (math-div-float a guess))
'(float 5 -1))))
(if (math-nearly-equal-float g2 guess)
g2
(math-sqrt-float-iter a g2))))
;;; True if A and B differ only in the last digit of precision. [P F F]
(defun math-nearly-equal-float (a b)
(let ((ediff (- (nth 2 a) (nth 2 b))))
(cond ((= ediff 0) ;; Expanded out for speed
(setq ediff (math-add (Math-integer-neg (nth 1 a)) (nth 1 b)))
(or (eq ediff 0)
(and (not (consp ediff))
(< ediff 10)
(> ediff -10)
(= (math-numdigs (nth 1 a)) calc-internal-prec))))
((= ediff 1)
(setq ediff (math-add (Math-integer-neg (nth 1 b))
(math-scale-int (nth 1 a) 1)))
(and (not (consp ediff))
(< ediff 10)
(> ediff -10)
(= (math-numdigs (nth 1 b)) calc-internal-prec)))
((= ediff -1)
(setq ediff (math-add (Math-integer-neg (nth 1 a))
(math-scale-int (nth 1 b) 1)))
(and (not (consp ediff))
(< ediff 10)
(> ediff -10)
(= (math-numdigs (nth 1 a)) calc-internal-prec))))))
(defun math-nearly-equal (a b) ; [P N N] [Public]
(setq a (math-float a))
(setq b (math-float b))
(if (eq (car a) 'polar) (setq a (math-complex a)))
(if (eq (car b) 'polar) (setq b (math-complex b)))
(if (eq (car a) 'cplx)
(if (eq (car b) 'cplx)
(and (or (math-nearly-equal-float (nth 1 a) (nth 1 b))
(and (math-nearly-zerop-float (nth 1 a) (nth 2 a))
(math-nearly-zerop-float (nth 1 b) (nth 2 b))))
(or (math-nearly-equal-float (nth 2 a) (nth 2 b))
(and (math-nearly-zerop-float (nth 2 a) (nth 1 a))
(math-nearly-zerop-float (nth 2 b) (nth 1 b)))))
(and (math-nearly-equal-float (nth 1 a) b)
(math-nearly-zerop-float (nth 2 a) b)))
(if (eq (car b) 'cplx)
(and (math-nearly-equal-float a (nth 1 b))
(math-nearly-zerop-float a (nth 2 b)))
(math-nearly-equal-float a b))))
;;; True if A is nearly zero compared to B. [P F F]
(defun math-nearly-zerop-float (a b)
(or (eq (nth 1 a) 0)
(<= (+ (math-numdigs (nth 1 a)) (nth 2 a))
(1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec)))))
(defun math-nearly-zerop (a b) ; [P N R] [Public]
(setq a (math-float a))
(setq b (math-float b))
(if (eq (car a) 'cplx)
(and (math-nearly-zerop-float (nth 1 a) b)
(math-nearly-zerop-float (nth 2 a) b))
(if (eq (car a) 'polar)
(math-nearly-zerop-float (nth 1 a) b)
(math-nearly-zerop-float a b))))
;;; This implementation could be improved, accuracy-wise.
(defun math-hypot (a b)
(cond ((Math-zerop a) (math-abs b))
((Math-zerop b) (math-abs a))
((not (Math-scalarp a))
(if (math-infinitep a)
(if (math-infinitep b)
(if (equal a b)
a
'(var nan var-nan))
a)
(calc-record-why 'scalarp a)
(list 'calcFunc-hypot a b)))
((not (Math-scalarp b))
(if (math-infinitep b)
b
(calc-record-why 'scalarp b)
(list 'calcFunc-hypot a b)))
((and (Math-numberp a) (Math-numberp b))
(math-with-extra-prec 1
(math-sqrt (math-add (calcFunc-abssqr a) (calcFunc-abssqr b)))))
((eq (car-safe a) 'hms)
(if (eq (car-safe b) 'hms) ; this helps sdev's of hms forms
(math-to-hms (math-hypot (math-from-hms a 'deg)
(math-from-hms b 'deg)))
(math-to-hms (math-hypot (math-from-hms a 'deg) b))))
((eq (car-safe b) 'hms)
(math-to-hms (math-hypot a (math-from-hms b 'deg))))
(t nil)))
(defalias 'calcFunc-hypot 'math-hypot)
(defun calcFunc-sqr (x)
(math-pow x 2))
(defun math-nth-root (a n)
(cond ((= n 2) (math-sqrt a))
((Math-zerop a) a)
((Math-negp a) nil)
((Math-integerp a)
(let ((root (math-nth-root-integer a n)))
(if (car root)
(cdr root)
(and (not calc-symbolic-mode)
(math-nth-root-float (math-float a) n
(math-float (cdr root)))))))
((eq (car-safe a) 'frac)
(let* ((num-root (math-nth-root-integer (nth 1 a) n))
(den-root (math-nth-root-integer (nth 2 a) n)))
(if (and (car num-root) (car den-root))
(list 'frac (cdr num-root) (cdr den-root))
(and (not calc-symbolic-mode)
(math-nth-root-float
(math-float a) n
(math-div-float (math-float (cdr num-root))
(math-float (cdr den-root))))))))
((eq (car-safe a) 'float)
(and (not calc-symbolic-mode)
(math-nth-root-float a n)))
((eq (car-safe a) 'polar)
(let ((root (math-nth-root (nth 1 a) n)))
(and root (list 'polar root (math-div (nth 2 a) n)))))
(t nil)))
;; The variables math-nrf-n, math-nrf-nf and math-nrf-nfm1 are local
;; to math-nth-root-float, but are used by math-nth-root-float-iter,
;; which is called by math-nth-root-float.
(defvar math-nrf-n)
(defvar math-nrf-nf)
(defvar math-nrf-nfm1)
(defun math-nth-root-float (a math-nrf-n &optional guess)
(math-inexact-result)
(math-with-extra-prec 1
(let ((math-nrf-nf (math-float math-nrf-n))
(math-nrf-nfm1 (math-float (1- math-nrf-n))))
(math-nth-root-float-iter a (or guess
(math-make-float
1 (/ (+ (math-numdigs (nth 1 a))
(nth 2 a)
(/ math-nrf-n 2))
math-nrf-n)))))))
(defun math-nth-root-float-iter (a guess)
(math-working "root" guess)
(let ((g2 (math-div-float (math-add-float (math-mul math-nrf-nfm1 guess)
(math-div-float
a (math-ipow guess (1- math-nrf-n))))
math-nrf-nf)))
(if (math-nearly-equal-float g2 guess)
g2
(math-nth-root-float-iter a g2))))
;; The variable math-nri-n is local to math-nth-root-integer, but
;; is used by math-nth-root-int-iter, which is called by
;; math-nth-root-int.
(defvar math-nri-n)
(defun math-nth-root-integer (a math-nri-n &optional guess) ; [I I S]
(math-nth-root-int-iter a (or guess
(math-scale-int 1 (/ (+ (math-numdigs a)
(1- math-nri-n))
math-nri-n)))))
(defun math-nth-root-int-iter (a guess)
(math-working "root" guess)
(let* ((q (math-idivmod a (math-ipow guess (1- math-nri-n))))
(s (math-add (car q) (math-mul (1- math-nri-n) guess)))
(g2 (math-idivmod s math-nri-n)))
(if (Math-natnum-lessp (car g2) guess)
(math-nth-root-int-iter a (car g2))
(cons (and (equal (car g2) guess)
(eq (cdr q) 0)
(eq (cdr g2) 0))
guess))))
(defun calcFunc-nroot (x n)
(calcFunc-pow x (if (integerp n)
(math-make-frac 1 n)
(math-div 1 n))))
;;;; Transcendental functions.
;;; All of these functions are defined on the complex plane.
;;; (Branch cuts, etc. follow Steele's Common Lisp book.)
;;; Most functions increase calc-internal-prec by 2 digits, then round
;;; down afterward. "-raw" functions use the current precision, require
;;; their arguments to be in float (or complex float) format, and always
;;; work in radians (where applicable).
(defun math-to-radians (a) ; [N N]
(cond ((eq (car-safe a) 'hms)
(math-from-hms a 'rad))
((and (not math-simplifying-units)
(memq calc-angle-mode '(deg hms)))
(math-mul a (math-pi-over-180)))
(t a)))
(defun math-from-radians (a) ; [N N]
(cond ((and (not math-simplifying-units)
(eq calc-angle-mode 'deg))
(if (math-constp a)
(math-div a (math-pi-over-180))
(list 'calcFunc-deg a)))
((eq calc-angle-mode 'hms)
(math-to-hms a 'rad))
(t a)))
(defun math-to-radians-2 (a &optional force-symbolic) ; [N N]
(cond ((eq (car-safe a) 'hms)
(math-from-hms a 'rad))
((and (not math-simplifying-units)
(memq calc-angle-mode '(deg hms)))
(if (or calc-symbolic-mode force-symbolic)
(math-div (math-mul a '(var pi var-pi)) 180)
(math-mul a (math-pi-over-180))))
(t a)))
(defun math-from-radians-2 (a &optional force-symbolic) ; [N N]
(cond ((and (not math-simplifying-units)
(memq calc-angle-mode '(deg hms)))
(if (or calc-symbolic-mode force-symbolic)
(math-div (math-mul 180 a) '(var pi var-pi))
(math-div a (math-pi-over-180))))
(t a)))
;;; Sine, cosine, and tangent.
(defun calcFunc-sin (x) ; [N N] [Public]
(cond ((and (integerp x)
(if (eq calc-angle-mode 'deg)
(= (% x 90) 0)
(= x 0)))
(aref [0 1 0 -1] (math-mod (/ x 90) 4)))
((Math-scalarp x)
(math-with-extra-prec 2
(math-sin-raw (math-to-radians (math-float x)))))
((eq (car x) 'sdev)
(if (math-constp x)
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float (nth 1 x))))
(xs (math-to-radians (math-float (nth 2 x))))
(sc (math-sin-cos-raw xx)))
(math-make-sdev (car sc) (math-mul xs (cdr sc)))))
(math-make-sdev (calcFunc-sin (nth 1 x))
(math-mul (nth 2 x) (calcFunc-cos (nth 1 x))))))
((and (eq (car x) 'intv) (math-intv-constp x))
(calcFunc-cos (math-sub x (math-quarter-circle nil))))
((equal x '(var nan var-nan))
x)
(t (calc-record-why 'scalarp x)
(list 'calcFunc-sin x))))
(defun calcFunc-cos (x) ; [N N] [Public]
(cond ((and (integerp x)
(if (eq calc-angle-mode 'deg)
(= (% x 90) 0)
(= x 0)))
(aref [1 0 -1 0] (math-mod (/ x 90) 4)))
((Math-scalarp x)
(math-with-extra-prec 2
(math-cos-raw (math-to-radians (math-float x)))))
((eq (car x) 'sdev)
(if (math-constp x)
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float (nth 1 x))))
(xs (math-to-radians (math-float (nth 2 x))))
(sc (math-sin-cos-raw xx)))
(math-make-sdev (cdr sc) (math-mul xs (car sc)))))
(math-make-sdev (calcFunc-cos (nth 1 x))
(math-mul (nth 2 x) (calcFunc-sin (nth 1 x))))))
((and (eq (car x) 'intv) (math-intv-constp x))
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float x)))
(na (math-floor (math-div (nth 2 xx) (math-pi))))
(nb (math-floor (math-div (nth 3 xx) (math-pi))))
(span (math-sub nb na)))
(if (memq span '(0 1))
(let ((int (math-sort-intv (nth 1 x)
(math-cos-raw (nth 2 xx))
(math-cos-raw (nth 3 xx)))))
(if (eq span 1)
(if (math-evenp na)
(math-make-intv (logior (nth 1 x) 2)
-1
(nth 3 int))
(math-make-intv (logior (nth 1 x) 1)
(nth 2 int)
1))
int))
(list 'intv 3 -1 1)))))
((equal x '(var nan var-nan))
x)
(t (calc-record-why 'scalarp x)
(list 'calcFunc-cos x))))
(defun calcFunc-sincos (x) ; [V N] [Public]
(if (Math-scalarp x)
(math-with-extra-prec 2
(let ((sc (math-sin-cos-raw (math-to-radians (math-float x)))))
(list 'vec (cdr sc) (car sc)))) ; the vector [cos, sin]
(list 'vec (calcFunc-sin x) (calcFunc-cos x))))
(defun calcFunc-tan (x) ; [N N] [Public]
(cond ((and (integerp x)
(if (eq calc-angle-mode 'deg)
(= (% x 180) 0)
(= x 0)))
0)
((Math-scalarp x)
(math-with-extra-prec 2
(math-tan-raw (math-to-radians (math-float x)))))
((eq (car x) 'sdev)
(if (math-constp x)
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float (nth 1 x))))
(xs (math-to-radians (math-float (nth 2 x))))
(sc (math-sin-cos-raw xx)))
(if (and (math-zerop (cdr sc)) (not calc-infinite-mode))
(progn
(calc-record-why "*Division by zero")
(list 'calcFunc-tan x))
(math-make-sdev (math-div-float (car sc) (cdr sc))
(math-div-float xs (math-sqr (cdr sc)))))))
(math-make-sdev (calcFunc-tan (nth 1 x))
(math-div (nth 2 x)
(math-sqr (calcFunc-cos (nth 1 x)))))))
((and (eq (car x) 'intv) (math-intv-constp x))
(or (math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float x)))
(na (math-floor (math-div (math-sub (nth 2 xx)
(math-pi-over-2))
(math-pi))))
(nb (math-floor (math-div (math-sub (nth 3 xx)
(math-pi-over-2))
(math-pi)))))
(and (equal na nb)
(math-sort-intv (nth 1 x)
(math-tan-raw (nth 2 xx))
(math-tan-raw (nth 3 xx))))))
'(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
((equal x '(var nan var-nan))
x)
(t (calc-record-why 'scalarp x)
(list 'calcFunc-tan x))))
(defun calcFunc-sec (x)
(cond ((and (integerp x)
(eq calc-angle-mode 'deg)
(= (% x 180) 0))
(if (= (% x 360) 0)
1
-1))
((and (integerp x)
(eq calc-angle-mode 'rad)
(= x 0))
1)
((Math-scalarp x)
(math-with-extra-prec 2
(math-sec-raw (math-to-radians (math-float x)))))
((eq (car x) 'sdev)
(if (math-constp x)
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float (nth 1 x))))
(xs (math-to-radians (math-float (nth 2 x))))
(sc (math-sin-cos-raw xx)))
(if (and (math-zerop (cdr sc))
(not calc-infinite-mode))
(progn
(calc-record-why "*Division by zero")
(list 'calcFunc-sec x))
(math-make-sdev (math-div-float '(float 1 0) (cdr sc))
(math-div-float
(math-mul xs (car sc))
(math-sqr (cdr sc)))))))
(math-make-sdev (calcFunc-sec (nth 1 x))
(math-div
(math-mul (nth 2 x)
(calcFunc-sin (nth 1 x)))
(math-sqr (calcFunc-cos (nth 1 x)))))))
((and (eq (car x) 'intv)
(math-intv-constp x))
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float x)))
(na (math-floor (math-div (math-sub (nth 2 xx)
(math-pi-over-2))
(math-pi))))
(nb (math-floor (math-div (math-sub (nth 3 xx)
(math-pi-over-2))
(math-pi))))
(naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
(nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
(span (math-sub nbb naa)))
(if (not (equal na nb))
'(intv 3 (neg (var inf var-inf)) (var inf var-inf))
(let ((int (math-sort-intv (nth 1 x)
(math-sec-raw (nth 2 xx))
(math-sec-raw (nth 3 xx)))))
(if (eq span 1)
(if (math-evenp (math-div (math-add naa 1) 2))
(math-make-intv (logior (nth 1 int) 2)
1
(nth 3 int))
(math-make-intv (logior (nth 1 int) 1)
(nth 2 int)
-1))
int))))))
((equal x '(var nan var-nan))
x)
(t (calc-record-why 'scalarp x)
(list 'calcFunc-sec x))))
(defun calcFunc-csc (x)
(cond ((and (integerp x)
(eq calc-angle-mode 'deg)
(= (% (- x 90) 180) 0))
(if (= (% (- x 90) 360) 0)
1
-1))
((Math-scalarp x)
(math-with-extra-prec 2
(math-csc-raw (math-to-radians (math-float x)))))
((eq (car x) 'sdev)
(if (math-constp x)
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float (nth 1 x))))
(xs (math-to-radians (math-float (nth 2 x))))
(sc (math-sin-cos-raw xx)))
(if (and (math-zerop (car sc))
(not calc-infinite-mode))
(progn
(calc-record-why "*Division by zero")
(list 'calcFunc-csc x))
(math-make-sdev (math-div-float '(float 1 0) (car sc))
(math-div-float
(math-mul xs (cdr sc))
(math-sqr (car sc)))))))
(math-make-sdev (calcFunc-csc (nth 1 x))
(math-div
(math-mul (nth 2 x)
(calcFunc-cos (nth 1 x)))
(math-sqr (calcFunc-sin (nth 1 x)))))))
((and (eq (car x) 'intv)
(math-intv-constp x))
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float x)))
(na (math-floor (math-div (nth 2 xx) (math-pi))))
(nb (math-floor (math-div (nth 3 xx) (math-pi))))
(naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
(nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
(span (math-sub nbb naa)))
(if (not (equal na nb))
'(intv 3 (neg (var inf var-inf)) (var inf var-inf))
(let ((int (math-sort-intv (nth 1 x)
(math-csc-raw (nth 2 xx))
(math-csc-raw (nth 3 xx)))))
(if (eq span 1)
(if (math-evenp (math-div naa 2))
(math-make-intv (logior (nth 1 int) 2)
1
(nth 3 int))
(math-make-intv (logior (nth 1 int) 1)
(nth 2 int)
-1))
int))))))
((equal x '(var nan var-nan))
x)
(t (calc-record-why 'scalarp x)
(list 'calcFunc-csc x))))
(defun calcFunc-cot (x) ; [N N] [Public]
(cond ((and (integerp x)
(if (eq calc-angle-mode 'deg)
(= (% (- x 90) 180) 0)
(= x 0)))
0)
((Math-scalarp x)
(math-with-extra-prec 2
(math-cot-raw (math-to-radians (math-float x)))))
((eq (car x) 'sdev)
(if (math-constp x)
(math-with-extra-prec 2
(let* ((xx (math-to-radians (math-float (nth 1 x))))
(xs (math-to-radians (math-float (nth 2 x))))
(sc (math-sin-cos-raw xx)))