summaryrefslogtreecommitdiff
path: root/modules/language/python/module/math.scm
blob: 4ad8d2fa97718e13355a4033880aac7154d11af4 (about) (plain)
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
(define-module (language python module math)
  #:use-module (language python for)
  #:use-module (language python try)
  #:use-module (language python exceptions)
  #:use-module (language python number)
  #:use-module (system foreign)
  #:use-module (rnrs bytevectors)
  
  #:export (ceil copysign fabs factorial floor fmod frexp fsum gcd isclose
		 isfinite isinf isnan modf trunc exp expm1 log log1p log2 log10
		 pow sqrt acos asin atan cos sin tan atan2 hypot pi
		 degrees radians acosh asinh atanh cosh sinh tanh
		 erf erfc gamma lgamma e tau inf nan))

(define ceil
  (lambda (x)
    ((@ (guile) inexac->exact)
     ((@ (guile) ceiling)
      x))))
  
(define (copysign x y)
  (let ((x ((@ (guile) abs) x)))
    (if (< y 0) (- x) x)))

(define (fabs x) ((@ (guile) abs) x))

(define (factorial x)
  (if (not (and (number? x) (integer? x)))
      (raise ValueError "Not an integer"))
  (if (< x 0)
      (raise ValueError "Negative integer"))

  (let lp ((x x))
    (if (= x 0)
	1
	(* x (lp (- x 1))))))

(define (floor x)
  ((@ (guile) inexac->exact)
   ((@ (guile) floor)
    x)))

(define (fmod x y) ((@ (guile) truncate-remainder) x y))

(define frexp
  (let ((f (pointer->procedure double
                               (dynamic-func "frexp" (dynamic-link))
                               (list double '*))))
    (lambda (x)
      (let* ((v  (make-bytevector 4))
	     (vp (bytevector->pointer v)))
	(list (f x) (bytevector-s32-ref v 0 (native-endianness)))))))

(define (fsum it)
  (for ((x : it)) ((s 0))
       (+ s x)
       #:final s))
       

(define gcd (@ (guile) gcd))

(define* (isclose a b #:key (rel_tol 1e-9) (abs_tol 0.0))
  (define e-abs (abs (- a b)))
  (if (< e-abs (max (* rel_tol (max (abs a) (abs b))) abs_tol))
      #t
      #f))

(define (isfinite x)
  (if (or (inf? x) (nan? x))
      #f
      #t))

(define isinf inf?)
(define isnan nan?)

(define (ldexp x i) (* x ((@ (guile) expt) 2 i)))

(define (modf x)
  (let* ((x1 (floor x))
	 (x2 (- x x1)))
    (values x2 x1)))

(define trunc py-trunc)

;; Power and logarithms
(define exp (@ (guile) exp))

(define expm1
  (let ((f (pointer->procedure double
                               (dynamic-func "expm1" (dynamic-link))
                               (list double))))
    (lambda (x) (f x))))

(define log (@ (guile) log))

(define log1p
  (let ((f (pointer->procedure double
                               (dynamic-func "log1p" (dynamic-link))
                               (list double))))

    (lambda (x) (f x))))

(define log2
  (let ((f (pointer->procedure double
                               (dynamic-func "log2" (dynamic-link))
                               (list double))))
    (lambda (x) (f x))))

(define log10 (@ (guile) log10))

(define pow expt)

(define sqrt (@ (guile) sqrt))

;; Trigs
(define acos (@ (guile) acos))
(define asin (@ (guile) asin))
(define atan (@ (guile) atan))
(define cos  (@ (guile) cos))
(define sin  (@ (guile) sin))
(define tan  (@ (guile) tan))

(define atan2
  (let ((f (pointer->procedure double
                               (dynamic-func "atan2" (dynamic-link))
                               (list double double))))
    (lambda (x y) (f x y))))

(define hypot
  (let ((f (pointer->procedure double
                               (dynamic-func "hypot" (dynamic-link))
                               (list double double))))

    (lambda (x y) (f x y))))


;; angular conversion
(define pi (* 4 (atan 1)))

(define degrees
  (let ((f (/ 360 (* 2 pi))))
    (lambda (x) (* f x))))

(define radians
  (let ((f (/ (* 2 pi) 360)))
    (lambda (x) (* f x))))

;; Hyperbolic funcitons
(define acosh (@ (guile) acosh))
(define asinh (@ (guile) asinh))
(define atanh (@ (guile) atanh))
(define cosh  (@ (guile) cosh))
(define sinh  (@ (guile) sinh))
(define tanh  (@ (guile) tanh))


;; Special functions
(define erf
  (let ((f (pointer->procedure double
                               (dynamic-func "erf" (dynamic-link))
                               (list double))))
    (lambda (x) (f x))))

(define erfc
  (let ((f (pointer->procedure double
                               (dynamic-func "erfc" (dynamic-link))
                               (list double))))
    (lambda (x) (f x))))

(define gamma
  (let ((f (pointer->procedure double
                               (dynamic-func "tgamma" (dynamic-link))
                               (list double))))

    (lambda (x)
      (if (integer? x)
	  (factorial (- x 1)) 
	  (f x)))))

(define lgamma
  (let ((f (pointer->procedure double
                               (dynamic-func "lgamma" (dynamic-link))
                               (list double))))
    (lambda (x)
      (f x))))

;; constants
(define e   (exp 1))
(define tau (* 2 pi))
(define inf ((@ (guile) inf)))
(define nan ((@ (guile) nan)))