File: extra-math.thb

package info (click to toggle)
theme-d 7.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,036 kB
  • sloc: lisp: 9,625; sh: 5,321; makefile: 715; ansic: 477
file content (385 lines) | stat: -rw-r--r-- 12,108 bytes parent folder | download | duplicates (2)
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
;; -*-theme-d-*-

;; Copyright (C) 2018, 2021 Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.


(define-body (standard-library extra-math)

  ;; We trust libthemedsupport so we can use unchecked-prim-proc here.

  (add-method fmod
    (unchecked-prim-proc fmod (<real> <real>) <real> pure))

  (add-method r-remainder
    (unchecked-prim-proc r-remainder (<real> <real>) <real> pure))

  (add-method fma
    (unchecked-prim-proc fma (<real> <real> <real>) <real> pure))

  (add-method fast-fma?
    (unchecked-prim-proc fast-fma? () <boolean> pure))

  (add-method fmin
    (unchecked-prim-proc fmin (<real> <real>) <real> pure))

  (add-method fmax
    (unchecked-prim-proc fmax (<real> <real>) <real> pure))

  (add-method fdim
    (unchecked-prim-proc fdim (<real> <real>) <real> pure))

  (add-method r-exp2
    (unchecked-prim-proc r-exp2 (<real>) <real> pure))

  (add-method r-expm1
    (unchecked-prim-proc r-expm1 (<real>) <real> pure))

  (add-method r-log2
    (unchecked-prim-proc r-log2 (<real>) <real> pure))

  (add-method r-log1p
    (unchecked-prim-proc r-log1p (<real>) <real> pure))

  (add-method logb
    (unchecked-prim-proc logb (<real>) <real> pure))

  (add-method ilogb
    (unchecked-prim-proc ilogb (<real>) <integer> pure))

  (add-method ilogb0
    (unchecked-prim-proc ilogb0 () <integer> pure))

  (add-method ilogbnan
    (unchecked-prim-proc ilogbnan () <integer> pure))

  (add-method r-cbrt
    (unchecked-prim-proc r-cbrt (<real>) <real> pure))

  (add-method r-hypot
    (unchecked-prim-proc r-hypot (<real> <real>) <real> pure))

  (add-method r-erf
    (unchecked-prim-proc r-erf (<real>) <real> pure))

  (add-method r-erfc
    (unchecked-prim-proc r-erfc (<real>) <real> pure))

  (add-method r-lgamma
    (unchecked-prim-proc r-lgamma (<real>) <real> pure))

  (add-method r-tgamma
    (unchecked-prim-proc r-tgamma (<real>) <real> pure))

  (add-method r-nearbyint
    (unchecked-prim-proc r-nearbyint (<real>) <real> pure))

  (add-method rint
    (unchecked-prim-proc rint (<real>) <real> pure))

  (add-method frexp
    (unchecked-prim-proc frexp (<real>) (:pair <real> <integer>) pure))

  (add-method ldexp
    (unchecked-prim-proc ldexp (<real> <integer>) <real> pure))

  (add-method modf
    (unchecked-prim-proc modf (<real>) (:pair <real> <real>) pure))

  (add-method r-nextafter
    (unchecked-prim-proc r-nextafter (<real> <real>) <real> pure))

  (add-method r-copysign
    (unchecked-prim-proc r-copysign (<real> <real>) <real> pure))

  (add-method fpclassify
    (unchecked-prim-proc fpclassify (<real>) <integer> pure))

  (add-method fpclassify-nan
    (unchecked-prim-proc fpclassify-nan () <integer> pure))

  (add-method fpclassify-infinite
    (unchecked-prim-proc fpclassify-infinite () <integer> pure))

  (add-method fpclassify-zero
    (unchecked-prim-proc fpclassify-zero () <integer> pure))

  (add-method fpclassify-subnormal
    (unchecked-prim-proc fpclassify-subnormal () <integer> pure))

  (add-method fpclassify-normal
    (unchecked-prim-proc fpclassify-normal () <integer> pure))

  (add-method r-isnormal?
    (unchecked-prim-proc r-isnormal? (<real>) <boolean> pure))

  (add-method r-signbit
    (unchecked-prim-proc r-signbit (<real>) <integer> pure))

  (define-simple-method r-log2-neg (((r <real>)) <complex> pure)
    (complex (r-log2 (r-abs r)) gl-r-pi/ln2))

  (define-simple-method i-cbrt (((i <integer>)) (:union <real> <integer>) pure)
    (let ((r-result (r-cbrt (integer->real i))))
      (if (integer-float? r-result) (real->integer r-result) r-result)))

  (define-simple-method rat-cbrt (((rat <rational>))
				  (:union <real> <rational> <integer>) pure)
    (let ((r-numer (r-cbrt (integer->real (numerator rat))))
	  (r-denom (r-cbrt (integer->real (denominator rat)))))
      (if (and (integer-float? r-numer) (integer-float? r-denom))
	  (let ((i-numer (real->integer r-numer))
		(i-denom (real->integer r-denom)))
	    (if (equal? (remainder i-numer i-denom) 0)
		(quotient i-numer i-denom)
		(rational i-numer i-denom)))
	  (/ r-numer r-denom))))

  (define-simple-virtual-method exp2 (((nr <number>)) <number> pure)
    (raise-simple 'exp2:dispatch-error))

  (define-simple-virtual-method exp2 (((nr <real-number>)) <real-number> pure)
    (raise-simple 'exp2:dispatch-error))

  (include-virtual-methods exp2 c-exp2)

  (include-virtual-methods exp2 r-exp2)

  (define-simple-virtual-method exp2 (((rat <rational>)) <real-number> pure)
    (let* ((rat-s (simplify-rational rat))
	   (i-numer (numerator rat-s))
	   (i-denom (denominator rat-s)))
      (if (equal? i-denom 1)
	  (i-expt 2 i-numer)
	  (r-exp2 (rational->real rat)))))

  (define-simple-virtual-method exp2 (((i <integer>)) <rational-number> pure)
    (i-expt 2 i))

  (define-simple-virtual-method expm1 (((nr <number>)) <number> pure)
    (raise-simple 'expm1:dispatch-error))

  (define-simple-virtual-method expm1 (((nr <real-number>)) <real-number> pure)
    (raise-simple 'expm1:dispatch-error))

  (define-simple-virtual-method expm1 (((cx <complex>)) <complex> pure)
    (- (c-exp cx) 1.0))

  (include-virtual-methods expm1 r-expm1)

  (define-simple-virtual-method expm1 (((rat <rational>))
				       (:union <real> <integer>)
				       pure)
    (if (rat-zero? rat)
	0
	(r-expm1 (rational->real rat))))

  (define-simple-virtual-method expm1 (((i <integer>)) (:union <real> <integer>)
				       pure)
    (if (equal? i 0)
	0
	(r-expm1 (integer->real i))))

  (define-simple-virtual-method log2 (((nr <number>)) <number> pure)
    (raise-simple 'log2:dispatch-error))

  (define-simple-virtual-method log2 (((cx <complex>)) <complex> pure)
    (let ((r-abs-value (c-abs cx))
	  (r-phase (c-angle cx)))
      (create <complex> (r-log2 r-abs-value) (/ r-phase gl-r-ln2))))

  (define-simple-virtual-method log2 (((r <real>)) (:union <real> <complex>)
				      pure)
    (if (>= r 0.0)
	(r-log2 r)
	(r-log2-neg r)))

  (define-simple-virtual-method log2 (((rat <rational>))
				      (:union <integer> <real> <complex>) pure)
    (cond
     ((rat-zero? rat) (raise-numerical-overflow 'log2))
     ((rat-one? rat) 0)
     ((> rat 0)
      (match-type (rat-log2-exact rat)
	((<null>) (r-log2 (rational->real rat)))
	((i2 <integer>) i2)))
     (else (r-log2-neg (rational->real rat)))))

  (define-simple-virtual-method log2 (((i <integer>))
				      (:union <integer> <real> <complex>) pure)
    (cond
     ((= i 0) (raise-numerical-overflow 'log2))
     ((= i 1) 0)
     ((> i 0)
      (match-type (i-log2-exact i)
	((<null>) (r-log2 (integer->real i)))
	((i2 <integer>) i2)))
     (else (r-log2-neg (integer->real i)))))

  (define-simple-virtual-method log1p (((nr <number>)) <number> pure)
    (raise-simple 'log1p:dispatch-error))

  (define-simple-virtual-method log1p (((cx <complex>)) <complex> pure)
    (c-log (+ 1 cx)))

  (define-simple-virtual-method log1p (((r <real>))
				       (:union <complex> <real>) pure)
    (cond
     ((> r -1.0) (r-log1p r))
     ((equal? r -1.0) (neg-inf))
     ((< r -1.0) (r-log-neg (+ 1.0 r)))
     (else (nan))))

  (define-simple-virtual-method log1p (((rat <rational>))
				       (:union <complex> <real> <integer>) pure)
    (cond
     ((> rat -1) (r-log1p (rational->real rat)))
     ((equal? rat -1) (raise-numerical-overflow 'log1p))
     ((< rat -1) (r-log-neg (rational->real (+ 1 rat))))
     (else
      ;; We should never arrive here.
      (raise-simple 'log1p:internal-error))))

  (define-simple-virtual-method log1p (((i <integer>))
				       (:union <complex> <real> <integer>) pure)
    (cond
     ((> i -1) (r-log1p (integer->real i)))
     ((equal? i -1) (raise-numerical-overflow 'log1p))
     ((< i -1) (r-log-neg (integer->real (+ 1 i))))
     (else
      ;; We should never arrive here.
      (raise-simple 'log1p:internal-error))))

  (define-simple-virtual-method cbrt (((nr <number>)) <number> pure)
    (raise-simple 'cbrt:dispatch-error))

  (define-simple-virtual-method cbrt (((r <real-number>)) <real-number> pure)
    (raise-simple 'cbrt:dispatch-error))

  (define-simple-virtual-method cbrt (((cx <complex>)) <complex> pure)
    (let ((r-abs (c-abs cx))
	  (r-angle (c-angle cx)))
      (make-polar (r-cbrt r-abs) (/ r-angle 3))))

  (include-virtual-methods cbrt r-cbrt)

  (include-virtual-methods cbrt rat-cbrt)

  (include-virtual-methods cbrt i-cbrt)

  (define-simple-virtual-method hypot (((r1 <real-number>) (r2 <real-number>))
				       <real-number> pure)
    (raise-simple 'hypot:dispatch-error))

  (include-virtual-methods hypot r-hypot)

  (define-simple-virtual-method hypot (((r1 <real>) (rat2 <rational>)) <real>
				       pure)
    (r-hypot r1 (rational->real rat2)))

  (define-simple-virtual-method hypot (((r1 <real>) (i2 <integer>)) <real> pure)
    (r-hypot r1 (integer->real i2)))

  (define-simple-virtual-method hypot (((rat1 <rational>) (r2 <real>)) <real>
				       pure)
    (r-hypot (rational->real rat1) r2))

  (define-simple-virtual-method hypot (((rat1 <rational>) (rat2 <rational>))
				       <real-number> pure)
    (rat-sqrt (+ (square rat1) (square rat2))))

  (define-simple-virtual-method hypot (((rat1 <rational>) (i2 <integer>))
				       <real-number> pure)
    (rat-sqrt (+ (square rat1) (square i2))))

  (define-simple-virtual-method hypot (((i1 <integer>) (r2 <real>)) <real> pure)
    (r-hypot (integer->real i1) r2))

  (define-simple-virtual-method hypot (((i1 <integer>) (rat2 <rational>))
				       <real-number> pure)
    (rat-sqrt (+ (square i1) (square rat2))))

  (define-simple-virtual-method hypot (((i1 <integer>) (i2 <integer>))
				       (:union <real> <integer>) pure)
    (i-sqrt (+ (square i1) (square i2))))

  (define-simple-virtual-method erf (((r <real-number>))
				     (:union <real> <integer>)
				     pure)
    (raise-simple 'erf:dispatch-error))

  (include-virtual-methods erf r-erf)

  (define-simple-virtual-method erf (((rat <rational>))
				     (:union <real> <integer>)
				     pure)
    (if (rat-zero? rat) 0 (r-erf (rational->real rat))))

  (define-simple-virtual-method erf (((i <integer>)) (:union <real> <integer>)
				     pure)
    (if (equal? i 0) 0 (r-erf (integer->real i))))

  (define-simple-virtual-method erfc (((r <real-number>))
				      (:union <real> <integer>)
				      pure)
    (raise-simple 'erfc:dispatch-error))

  (include-virtual-methods erfc r-erfc)

  (define-simple-virtual-method erfc (((rat <rational>))
				      (:union <real> <integer>)
				      pure)
    (if (rat-zero? rat) 0 (r-erfc (rational->real rat))))

  (define-simple-virtual-method erfc (((i <integer>)) (:union <real> <integer>)
				      pure)
    (if (equal? i 0) 1 (r-erfc (integer->real i))))

  (define-simple-virtual-method lgamma (((r <real-number>))
					(:union <real> <integer>)
					pure)
    (raise-simple 'lgamma:dispatch-error))

  (include-virtual-methods lgamma r-lgamma)

  (define-simple-virtual-method lgamma (((rat <rational>))
					(:union <real> <integer>)
					pure)
    (let* ((rat-s (simplify-rational rat))
	   (i-numer (numerator rat-s))
	   (i-denom (denominator rat-s)))
      (if (and (equal? i-denom 1) (or (equal? i-numer 1) (equal? i-numer 2)))
	  0
	  (r-lgamma (rational->real rat-s)))))

  (define-simple-virtual-method lgamma (((i <integer>))
					(:union <real> <integer>)
					pure)
    (if (or (equal? i 1) (equal? i 2))
	0
	(r-lgamma (integer->real i))))

  (define-simple-virtual-method tgamma (((r <real-number>))
					(:union <real> <integer>)
					pure)
    (raise-simple 'tgamma:dispatch-error))

  (include-virtual-methods tgamma r-tgamma)

  (define-simple-virtual-method tgamma (((rat <rational>))
					(:union <real> <integer>)
					pure)
    (let* ((rat-s (simplify-rational rat))
	   (i-numer (numerator rat-s))
	   (i-denom (denominator rat-s)))
      (if (and (equal? i-denom 1) (>= i-numer 1))
	  (factorial (- i-numer 1))
	  (r-tgamma (rational->real rat-s)))))

  (define-simple-virtual-method tgamma (((i <integer>))
					(:union <real> <integer>)
					pure)
    (if (>= i 1)
	(factorial (- i 1))
	(r-tgamma (integer->real i)))))