-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lisp
234 lines (205 loc) · 6.57 KB
/
util.lisp
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
(in-package :cl-user)
(defpackage zpcalc/util
(:use :cl)
(:export
#:id
#:mkstr
#:symb
#:symbol=
#:with-gensyms
#:compose
#:square
#:is-isquare
#:is-pow
#:log-exact
#:expt-exact
#:sqrt-exact
#:invpow-exact
#:->double
#:bool->int
#:make-keyword
#:falsy
#:truthy
#:alambda
#:aif
#:acond
#:package-designator
#:approx-equal
#:factorial
#:choose
#:permute
#:prime
#:primep
#:phi
#:fib))
(in-package :zpcalc/util)
(defun id (i) i)
(defun symbol= (a b)
(if (and (symbolp a) (symbolp b))
(string= (symbol-name a) (symbol-name b))
nil))
(defun square (x)
(* x x))
(defun is-isquare (x)
(= x (square (isqrt x))))
;; returns if x can be represented as an integer power of the base
(defun is-pow (x base)
(if (or (complexp x) (floatp x) (floatp base))
nil
(let ((approx (round (log x base))))
(if (= (expt base approx) x)
approx
nil))))
;; log, except tries to preserve exactness
;; log_base (num)
(defun log-exact (num base)
(let ((result (is-pow num base)))
(if result
result
(log (->double num) (->double base)))))
(defun bool->int (b)
(if b 1 0))
(defun ->double (x)
(typecase x
(complex (coerce x '(complex double-float)))
(t (coerce x 'double-float))))
(defun sqrt-exact (x)
(typecase x
(integer (if (and (plusp x)
(is-isquare x))
(isqrt x)
(sqrt (->double x))))
(rational (if (and (plusp x)
(is-isquare (numerator x))
(is-isquare (denominator x)))
(/ (isqrt (numerator x)) (isqrt (denominator x)))
(sqrt (->double x))))
(t (sqrt (->double x)))))
;; tries to preserve exactness of x ^ (1 / z)
;; need to check if y exists such that y^z = x
;; z = log_y (x)
;; y ^ z = x
(defun invpow-exact (x z)
(typecase x
(integer
(if (integerp z)
(let ((approx (expt x (/ 1 z))))
(if (= (expt (round approx) z) x)
(round approx)
(expt (->double x) (/ 1 (->double z)))))
(expt (->double x) (/ 1 (->double z)))))
(rational
(if (integerp z)
(let ((approx-num (expt (numerator x) (/ 1 z)))
(approx-den (expt (denominator x) (/ 1 z))))
(if (and (= (expt (round approx-num) z) (numerator x))
(= (expt (round approx-den) z) (denominator x)))
(/ (round approx-num) (round approx-den))
(expt (->double x) (/ 1 (->double z)))))
(expt (->double x) (/ 1 (->double z)))))
(t (expt (->double x) (/ 1 (->double z))))))
(defun expt-exact (a b)
(if (and (or (integerp a) (rationalp a)) (integerp b))
(expt a b)
(expt (->double a) (->double b))))
;; Taken from rosetta code for approximate equality
(defun approx-equal (float1 float2 &optional (threshold 0.000001))
"Determine whether float1 and float2 are equal; THRESHOLD is the
maximum allowable difference between normalized significands of floats
with the same exponent. The significands are scaled appropriately
before comparison for floats with different exponents."
(multiple-value-bind (sig1 exp1 sign1) (decode-float float1)
(multiple-value-bind (sig2 exp2 sign2) (decode-float float2)
(let ((cmp1 (float-sign sign1 (scale-float sig1 (floor (- exp1 exp2) 2))))
(cmp2 (float-sign sign2 (scale-float sig2 (floor (- exp2 exp1) 2)))))
(< (abs (- cmp1 cmp2)) threshold)))))
(defun falsy (x)
(zerop x))
(defun truthy (x)
(not (falsy x)))
(defun factorial (x)
(check-type x integer)
(if (< x 0)
(error "~S is an invalid argument for factorial" x))
(labels ((rec (x acc)
(if (<= x 1)
acc
(rec (1- x) (* x acc)))))
(rec x 1)))
;; binomial coefficient
;; aka number of ways to choose k items out of n
(defun choose (n k)
(if (< n k)
(error "~S and ~S are invalid arguments to choose" n k))
(/ (factorial n) (* (factorial k) (factorial (- n k)))))
;; number of ways to permute k items out of n
(defun permute (n k)
(if (< n k)
(error "~S and ~S are invalid arguments to permute" n k))
(/ (factorial n) (* (factorial (- n k)))))
;; nth fibonacci number using binet's formula
(defun fib (n)
(if (< n 0)
(error "~S is an invalid argument to fib" n))
(round (/ (- (expt (/ (+ 1 (sqrt 5.0d0)) 2.0d0) n)
(expt (/ (- 1 (sqrt 5.0d0)) 2.0d0) n)) (sqrt 5.0d0))))
;; totient function
;; copied algorithm from https://cp-algorithms.com/algebra/phi-function.html
(defun phi (n)
(if (<= n 0)
(error "~S is an invalid argument to totient" n))
(let ((result n)
(i 2))
(loop while (<= (* i i) n)
do (progn
(when (= 0 (mod n i))
(loop while (= 0 (mod n i))
do (progn
(setf n (truncate (/ n i)))
(decf result (truncate (/ result i))))))
(incf i)))
(when (> n 1)
(decf result (truncate (/ result n))))
result))
;; taken from StackOverflow
(defun make-keyword (name) (values (intern (string-upcase name) "KEYWORD")))
;; some utils taken from Paul Graham's On Lisp
(defun mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args) (princ a s))))
(defun symb (&rest args)
(values (intern (apply #'mkstr args))))
(defmacro with-gensyms (syms &body body)
`(let ,(mapcar #'(lambda (s) `(,s (gensym))) syms)
,@body))
(defun compose (&rest fns)
(destructuring-bind (fn1 . rest) (reverse fns)
#'(lambda (&rest args)
(reduce #'(lambda (v f) (funcall f v))
rest
:initial-value (apply fn1 args)))))
(defmacro alambda (parms &body body)
`(labels ((self ,parms ,@body))
#'self))
;; modified to be package-agnostic
(defmacro aif (test-form then-form &optional else-form)
(let ((it (intern (string 'it))))
`(let ((,it ,test-form))
(if ,it ,then-form ,else-form))))
;; modified - chained aif
(defmacro acond (&rest clauses)
(if (null clauses)
nil
(let ((cl1 (car clauses)))
`(aif ,(car cl1)
,@(cdr cl1)
(acond ,@(cdr clauses))))))
;; Returns a symbol's proper name and the package it should be located in
;; If no package designation (no dot), return nil for the package
;; Ex: (package-designator :hello.world) ==> (values :hello :world)
(defun package-designator (symbol)
(aif (position #\. (string symbol))
(values
(make-keyword (subseq (string symbol) 0 it))
(make-keyword (subseq (string symbol) (1+ it))))
(values nil symbol)))