]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
kernel: move recusrive-hashcode to math and add test
[factor.git] / core / math / math.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel kernel.private ;
4 IN: math
5
6 BUILTIN: fixnum ;
7 BUILTIN: bignum ;
8 BUILTIN: float ;
9
10 PRIMITIVE: bits>double ( n -- x )
11 PRIMITIVE: bits>float ( n -- x )
12 PRIMITIVE: double>bits ( x -- n )
13 PRIMITIVE: float>bits ( x -- n )
14
15 <PRIVATE
16 PRIMITIVE: bignum* ( x y -- z )
17 PRIMITIVE: bignum+ ( x y -- z )
18 PRIMITIVE: bignum- ( x y -- z )
19 PRIMITIVE: bignum-bit? ( x n -- ? )
20 PRIMITIVE: bignum-bitand ( x y -- z )
21 PRIMITIVE: bignum-bitnot ( x -- y )
22 PRIMITIVE: bignum-bitor ( x y -- z )
23 PRIMITIVE: bignum-bitxor ( x y -- z )
24 PRIMITIVE: bignum-gcd ( x y -- z )
25 PRIMITIVE: bignum-log2 ( x -- n )
26 PRIMITIVE: bignum-mod ( x y -- z )
27 PRIMITIVE: bignum-shift ( x y -- z )
28 PRIMITIVE: bignum/i ( x y -- z )
29 PRIMITIVE: bignum/mod ( x y -- z w )
30 PRIMITIVE: bignum< ( x y -- ? )
31 PRIMITIVE: bignum<= ( x y -- ? )
32 PRIMITIVE: bignum= ( x y -- ? )
33 PRIMITIVE: bignum> ( x y -- ? )
34 PRIMITIVE: bignum>= ( x y -- ? )
35 PRIMITIVE: bignum>fixnum ( x -- y )
36 PRIMITIVE: bignum>fixnum-strict ( x -- y )
37 PRIMITIVE: both-fixnums? ( x y -- ? )
38 PRIMITIVE: fixnum* ( x y -- z )
39 PRIMITIVE: fixnum*fast ( x y -- z )
40 PRIMITIVE: fixnum+ ( x y -- z )
41 PRIMITIVE: fixnum+fast ( x y -- z )
42 PRIMITIVE: fixnum- ( x y -- z )
43 PRIMITIVE: fixnum-bitand ( x y -- z )
44 PRIMITIVE: fixnum-bitnot ( x -- y )
45 PRIMITIVE: fixnum-bitor ( x y -- z )
46 PRIMITIVE: fixnum-bitxor ( x y -- z )
47 PRIMITIVE: fixnum-fast ( x y -- z )
48 PRIMITIVE: fixnum-mod ( x y -- z )
49 PRIMITIVE: fixnum-shift ( x y -- z )
50 PRIMITIVE: fixnum-shift-fast ( x y -- z )
51 PRIMITIVE: fixnum/i ( x y -- z )
52 PRIMITIVE: fixnum/i-fast ( x y -- z )
53 PRIMITIVE: fixnum/mod ( x y -- z w )
54 PRIMITIVE: fixnum/mod-fast ( x y -- z w )
55 PRIMITIVE: fixnum< ( x y -- ? )
56 PRIMITIVE: fixnum<= ( x y -- z )
57 PRIMITIVE: fixnum> ( x y -- ? )
58 PRIMITIVE: fixnum>= ( x y -- ? )
59 PRIMITIVE: fixnum>bignum ( x -- y )
60 PRIMITIVE: fixnum>float ( x -- y )
61 PRIMITIVE: float* ( x y -- z )
62 PRIMITIVE: float+ ( x y -- z )
63 PRIMITIVE: float- ( x y -- z )
64 PRIMITIVE: float-u< ( x y -- ? )
65 PRIMITIVE: float-u<= ( x y -- ? )
66 PRIMITIVE: float-u> ( x y -- ? )
67 PRIMITIVE: float-u>= ( x y -- ? )
68 PRIMITIVE: float/f ( x y -- z )
69 PRIMITIVE: float< ( x y -- ? )
70 PRIMITIVE: float<= ( x y -- ? )
71 PRIMITIVE: float= ( x y -- ? )
72 PRIMITIVE: float> ( x y -- ? )
73 PRIMITIVE: float>= ( x y -- ? )
74 PRIMITIVE: float>bignum ( x -- y )
75 PRIMITIVE: float>fixnum ( x -- y )
76 PRIVATE>
77
78 GENERIC: >fixnum ( x -- n ) foldable
79 GENERIC: >bignum ( x -- n ) foldable
80 GENERIC: >integer ( x -- n ) foldable
81 GENERIC: >float ( x -- y ) foldable
82 GENERIC: integer>fixnum ( x -- y ) foldable
83 GENERIC: integer>fixnum-strict ( x -- y ) foldable
84
85 GENERIC: numerator ( a/b -- a )
86 GENERIC: denominator ( a/b -- b )
87 GENERIC: >fraction ( a/b -- a b )
88
89 GENERIC: real-part ( z -- x )
90 GENERIC: imaginary-part ( z -- y )
91
92 MATH: number= ( x y -- ? ) foldable
93
94 M: object number= 2drop f ;
95
96 MATH: <  ( x y -- ? ) foldable
97 MATH: <= ( x y -- ? ) foldable
98 MATH: >  ( x y -- ? ) foldable
99 MATH: >= ( x y -- ? ) foldable
100
101 MATH: unordered? ( x y -- ? ) foldable
102 MATH: u<  ( x y -- ? ) foldable
103 MATH: u<= ( x y -- ? ) foldable
104 MATH: u>  ( x y -- ? ) foldable
105 MATH: u>= ( x y -- ? ) foldable
106
107 M: object unordered? 2drop f ;
108
109 MATH: +   ( x y -- z ) foldable
110 MATH: -   ( x y -- z ) foldable
111 MATH: *   ( x y -- z ) foldable
112 MATH: /   ( x y -- z ) foldable
113 MATH: /f  ( x y -- z ) foldable
114 MATH: /i  ( x y -- z ) foldable
115 MATH: mod ( x y -- z ) foldable
116
117 MATH: /mod ( x y -- z w ) foldable
118
119 MATH: bitand ( x y -- z ) foldable
120 MATH: bitor  ( x y -- z ) foldable
121 MATH: bitxor ( x y -- z ) foldable
122 GENERIC#: shift 1 ( x n -- y ) foldable
123 GENERIC: bitnot ( x -- y ) foldable
124 GENERIC#: bit? 1 ( x n -- ? ) foldable
125
126 GENERIC: abs ( x -- y ) foldable
127
128 <PRIVATE
129
130 GENERIC: (log2) ( x -- n ) foldable
131
132 PRIVATE>
133
134 : recursive-hashcode ( n obj quot -- code )
135     pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
136
137 ERROR: log2-expects-positive x ;
138
139 : log2 ( x -- n )
140     dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
141
142 : zero? ( x -- ? ) 0 number= ; inline
143 : 2/ ( x -- y ) -1 shift ; inline
144 : sq ( x -- y ) dup * ; inline
145 : neg ( x -- -x ) -1 * ; inline
146 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
147 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
148 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
149 : 2^ ( n -- 2^n ) 1 swap shift ; inline
150 : even? ( n -- ? ) 1 bitand zero? ; inline
151 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
152
153 GENERIC: neg? ( x -- ? )
154
155 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
156     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
157
158 : when-zero ( ... n quot: ( ... -- ... x ) -- ... x ) [ ] if-zero ; inline
159
160 : unless-zero ( ... n quot: ( ... n -- ... ) -- ... ) [ ] swap if-zero ; inline
161
162 : until-zero ( ... n quot: ( ... x -- ... y ) -- ... ) [ dup zero? ] swap until drop ; inline
163
164 UNION: integer fixnum bignum ;
165
166 TUPLE: ratio
167     { numerator integer read-only }
168     { denominator integer read-only } ;
169
170 UNION: rational integer ratio ;
171
172 M: rational neg? 0 < ; inline
173
174 UNION: real rational float ;
175
176 TUPLE: complex
177     { real real read-only }
178     { imaginary real read-only } ;
179
180 UNION: number real complex ;
181
182 GENERIC: recip ( x -- y )
183
184 M: number recip 1 swap / ; inline
185
186 : rect> ( x y -- z )
187     ! Note: an imaginary 0.0 should still create a complex
188     dup 0 = [ drop ] [ complex boa ] if ; inline
189
190 GENERIC: >rect ( z -- x y )
191
192 M: real >rect 0 ; inline
193
194 M: complex >rect [ real-part ] [ imaginary-part ] bi ; inline
195
196 <PRIVATE
197
198 : (gcd) ( b a x y -- a d )
199     swap [
200         nip
201     ] [
202         [ /mod [ over * swapd - ] dip ] keep (gcd)
203     ] if-zero ; inline recursive
204
205 PRIVATE>
206
207 : gcd ( x y -- a d )
208     [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; inline
209
210 MATH: simple-gcd ( x y -- d ) foldable
211
212 <PRIVATE
213
214 : fixnum-gcd ( x y -- d ) { fixnum fixnum } declare gcd nip ;
215
216 PRIVATE>
217
218 M: fixnum simple-gcd fixnum-gcd ; inline
219
220 M: bignum simple-gcd bignum-gcd ; inline
221
222 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
223
224 GENERIC: fp-special? ( x -- ? )
225 GENERIC: fp-nan? ( x -- ? )
226 GENERIC: fp-qnan? ( x -- ? )
227 GENERIC: fp-snan? ( x -- ? )
228 GENERIC: fp-infinity? ( x -- ? )
229 GENERIC: fp-nan-payload ( x -- bits )
230 GENERIC: fp-sign ( x -- ? )
231
232 M: object fp-special? drop f ; inline
233 M: object fp-nan? drop f ; inline
234 M: object fp-qnan? drop f ; inline
235 M: object fp-snan? drop f ; inline
236 M: object fp-infinity? drop f ; inline
237
238 : <fp-nan> ( payload -- nan )
239     0x7ff0000000000000 bitor bits>double ; inline
240
241 GENERIC: next-float ( m -- n )
242 GENERIC: prev-float ( m -- n )
243
244 : next-power-of-2 ( m -- n )
245     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
246
247 : power-of-2? ( n -- ? )
248     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
249
250 : align ( m w -- n )
251     1 - [ + ] keep bitnot bitand ; inline
252
253 : each-integer-from ( ... i n quot: ( ... i -- ... ) -- ... )
254     2over < [
255         [ nip call ] 3keep
256         [ 1 + ] 2dip each-integer-from
257     ] [
258         3drop
259     ] if ; inline recursive
260
261 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
262     [ 0 ] 2dip each-integer-from ; inline
263
264 : times ( ... n quot: ( ... -- ... ) -- ... )
265     [ drop ] prepose each-integer ; inline
266
267 : find-integer-from ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
268     2over < [
269         [ nip call ] 3keep roll
270         [ 2drop ]
271         [ [ 1 + ] 2dip find-integer-from ] if
272     ] [
273         3drop f
274     ] if ; inline recursive
275
276 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
277     [ 0 ] 2dip find-integer-from ; inline
278
279 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
280     over 0 < [
281         2drop f
282     ] [
283         [ call ] 2keep rot [
284             drop
285         ] [
286             [ 1 - ] dip find-last-integer
287         ] if
288     ] if ; inline recursive
289
290 : all-integers-from? ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
291     2over < [
292         [ nip call ] 3keep roll
293         [ [ 1 + ] 2dip all-integers-from? ]
294         [ 3drop f ] if
295     ] [
296         3drop t
297     ] if ; inline recursive
298
299 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
300     [ 0 ] 2dip all-integers-from? ; inline