]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
primitives: Change PRIMITIVE: to check that the word is in that vocabulary and the...
[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 ;
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
88 GENERIC: real-part ( z -- x )
89 GENERIC: imaginary-part ( z -- y )
90
91 MATH: number= ( x y -- ? ) foldable
92
93 M: object number= 2drop f ;
94
95 MATH: <  ( x y -- ? ) foldable
96 MATH: <= ( x y -- ? ) foldable
97 MATH: >  ( x y -- ? ) foldable
98 MATH: >= ( x y -- ? ) foldable
99
100 MATH: unordered? ( x y -- ? ) foldable
101 MATH: u<  ( x y -- ? ) foldable
102 MATH: u<= ( x y -- ? ) foldable
103 MATH: u>  ( x y -- ? ) foldable
104 MATH: u>= ( x y -- ? ) foldable
105
106 M: object unordered? 2drop f ;
107
108 MATH: +   ( x y -- z ) foldable
109 MATH: -   ( x y -- z ) foldable
110 MATH: *   ( x y -- z ) foldable
111 MATH: /   ( x y -- z ) foldable
112 MATH: /f  ( x y -- z ) foldable
113 MATH: /i  ( x y -- z ) foldable
114 MATH: mod ( x y -- z ) foldable
115
116 MATH: /mod ( x y -- z w ) foldable
117
118 MATH: bitand ( x y -- z ) foldable
119 MATH: bitor  ( x y -- z ) foldable
120 MATH: bitxor ( x y -- z ) foldable
121 GENERIC# shift 1 ( x n -- y ) foldable
122 GENERIC: bitnot ( x -- y ) foldable
123 GENERIC# bit? 1 ( x n -- ? ) foldable
124
125 GENERIC: abs ( x -- y ) foldable
126
127 <PRIVATE
128
129 GENERIC: (log2) ( x -- n ) foldable
130
131 PRIVATE>
132
133 ERROR: log2-expects-positive x ;
134
135 : log2 ( x -- n )
136     dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
137
138 : zero? ( x -- ? ) 0 number= ; inline
139 : 2/ ( x -- y ) -1 shift ; inline
140 : sq ( x -- y ) dup * ; inline
141 : neg ( x -- -x ) -1 * ; inline
142 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
143 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
144 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
145 : 2^ ( n -- 2^n ) 1 swap shift ; inline
146 : even? ( n -- ? ) 1 bitand zero? ; inline
147 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
148
149 GENERIC: neg? ( x -- -x )
150
151 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
152     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
153
154 : when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline
155
156 : unless-zero ( ..a n quot: ( ..a n -- ..b ) -- ..b ) [ ] swap if-zero ; inline
157
158 UNION: integer fixnum bignum ;
159
160 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
161
162 UNION: rational integer ratio ;
163
164 M: rational neg? 0 < ; inline
165
166 UNION: real rational float ;
167
168 TUPLE: complex { real real read-only } { imaginary real read-only } ;
169
170 UNION: number real complex ;
171
172 GENERIC: recip ( x -- y )
173
174 M: number recip 1 swap / ; inline
175
176 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
177
178 GENERIC: fp-special? ( x -- ? )
179 GENERIC: fp-nan? ( x -- ? )
180 GENERIC: fp-qnan? ( x -- ? )
181 GENERIC: fp-snan? ( x -- ? )
182 GENERIC: fp-infinity? ( x -- ? )
183 GENERIC: fp-nan-payload ( x -- bits )
184 GENERIC: fp-sign ( x -- ? )
185
186 M: object fp-special? drop f ; inline
187 M: object fp-nan? drop f ; inline
188 M: object fp-qnan? drop f ; inline
189 M: object fp-snan? drop f ; inline
190 M: object fp-infinity? drop f ; inline
191
192 : <fp-nan> ( payload -- nan )
193     0x7ff0000000000000 bitor bits>double ; inline
194
195 GENERIC: next-float ( m -- n )
196 GENERIC: prev-float ( m -- n )
197
198 : next-power-of-2 ( m -- n )
199     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
200
201 : power-of-2? ( n -- ? )
202     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
203
204 : align ( m w -- n )
205     1 - [ + ] keep bitnot bitand ; inline
206
207 <PRIVATE
208
209 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
210
211 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
212
213 : iterate-step ( i n quot -- i n quot )
214     #! Apply quot to i, keep i and quot, hide n.
215     [ nip call ] 3keep ; inline
216
217 : iterate-rot ( ? i n quot -- i n quot ? )
218     [ rot ] dip swap ; inline
219
220 : iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
221
222 PRIVATE>
223
224 : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
225     [ iterate-step iterate-next (each-integer) ]
226     [ 3drop ] if-iterate? ; inline recursive
227
228 : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
229     [
230         iterate-step iterate-rot
231         [ 2drop ] [ iterate-next (find-integer) ] if
232     ] [ 3drop f ] if-iterate? ; inline recursive
233
234 : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
235     [
236         iterate-step iterate-rot
237         [ iterate-next (all-integers?) ] [ 3drop f ] if
238     ] [ 3drop t ] if-iterate? ; inline recursive
239
240 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
241     iterate-prep (each-integer) ; inline
242
243 : times ( ... n quot: ( ... -- ... ) -- ... )
244     [ drop ] prepose each-integer ; inline
245
246 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
247     iterate-prep (find-integer) ; inline
248
249 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
250     iterate-prep (all-integers?) ; inline
251
252 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
253     over 0 < [
254         2drop f
255     ] [
256         [ call ] 2keep rot [
257             drop
258         ] [
259             [ 1 - ] dip find-last-integer
260         ] if
261     ] if ; inline recursive