]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
Factor source files should not be executable
[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 math.private ;
4 IN: math
5
6 GENERIC: >fixnum ( x -- n ) foldable
7 GENERIC: >bignum ( x -- n ) foldable
8 GENERIC: >integer ( x -- n ) foldable
9 GENERIC: >float ( x -- y ) foldable
10
11 GENERIC: numerator ( a/b -- a )
12 GENERIC: denominator ( a/b -- b )
13
14 GENERIC: real-part ( z -- x )
15 GENERIC: imaginary-part ( z -- y )
16
17 MATH: number= ( x y -- ? ) foldable
18
19 M: object number= 2drop f ;
20
21 MATH: <  ( x y -- ? ) foldable
22 MATH: <= ( x y -- ? ) foldable
23 MATH: >  ( x y -- ? ) foldable
24 MATH: >= ( x y -- ? ) foldable
25
26 MATH: unordered? ( x y -- ? ) foldable
27 MATH: u<  ( x y -- ? ) foldable
28 MATH: u<= ( x y -- ? ) foldable
29 MATH: u>  ( x y -- ? ) foldable
30 MATH: u>= ( x y -- ? ) foldable
31
32 M: object unordered? 2drop f ;
33
34 MATH: +   ( x y -- z ) foldable
35 MATH: -   ( x y -- z ) foldable
36 MATH: *   ( x y -- z ) foldable
37 MATH: /   ( x y -- z ) foldable
38 MATH: /f  ( x y -- z ) foldable
39 MATH: /i  ( x y -- z ) foldable
40 MATH: mod ( x y -- z ) foldable
41
42 MATH: /mod ( x y -- z w ) foldable
43
44 MATH: bitand ( x y -- z ) foldable
45 MATH: bitor  ( x y -- z ) foldable
46 MATH: bitxor ( x y -- z ) foldable
47 GENERIC# shift 1 ( x n -- y ) foldable
48 GENERIC: bitnot ( x -- y ) foldable
49 GENERIC# bit? 1 ( x n -- ? ) foldable
50
51 GENERIC: abs ( x -- y ) foldable
52
53 <PRIVATE
54
55 GENERIC: (log2) ( x -- n ) foldable
56
57 PRIVATE>
58
59 ERROR: log2-expects-positive x ;
60
61 : log2 ( x -- n )
62     dup 0 <= [
63         log2-expects-positive
64     ] [
65         (log2)
66     ] if ; inline
67
68 : zero? ( x -- ? ) 0 number= ; inline
69 : 2/ ( x -- y ) -1 shift ; inline
70 : sq ( x -- y ) dup * ; inline
71 : neg ( x -- -x ) -1 * ; inline
72 : recip ( x -- y ) 1 swap / ; inline
73 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
74 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
75 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
76 : 2^ ( n -- 2^n ) 1 swap shift ; inline
77 : even? ( n -- ? ) 1 bitand zero? ;
78 : odd? ( n -- ? ) 1 bitand 1 number= ;
79
80 : if-zero ( n quot1 quot2 -- )
81     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
82
83 : when-zero ( n quot -- ) [ ] if-zero ; inline
84
85 : unless-zero ( n quot -- ) [ ] swap if-zero ; inline
86
87 UNION: integer fixnum bignum ;
88
89 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
90
91 UNION: rational integer ratio ;
92
93 UNION: real rational float ;
94
95 TUPLE: complex { real real read-only } { imaginary real read-only } ;
96
97 UNION: number real complex ;
98
99 : fp-bitwise= ( x y -- ? ) [ double>bits ] bi@ = ; inline
100
101 GENERIC: fp-special? ( x -- ? )
102 GENERIC: fp-nan? ( x -- ? )
103 GENERIC: fp-qnan? ( x -- ? )
104 GENERIC: fp-snan? ( x -- ? )
105 GENERIC: fp-infinity? ( x -- ? )
106 GENERIC: fp-nan-payload ( x -- bits )
107 GENERIC: fp-sign ( x -- ? )
108
109 M: object fp-special? drop f ; inline
110 M: object fp-nan? drop f ; inline
111 M: object fp-qnan? drop f ; inline
112 M: object fp-snan? drop f ; inline
113 M: object fp-infinity? drop f ; inline
114
115 : <fp-nan> ( payload -- nan )
116     HEX: 7ff0000000000000 bitor bits>double ; inline
117
118 GENERIC: next-float ( m -- n )
119 GENERIC: prev-float ( m -- n )
120
121 : next-power-of-2 ( m -- n )
122     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
123
124 : power-of-2? ( n -- ? )
125     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
126
127 : align ( m w -- n )
128     1 - [ + ] keep bitnot bitand ; inline
129
130 <PRIVATE
131
132 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
133
134 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
135
136 : iterate-step ( i n quot -- i n quot )
137     #! Apply quot to i, keep i and quot, hide n.
138     [ nip call ] 3keep ; inline
139
140 : iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
141
142 PRIVATE>
143
144 : (each-integer) ( i n quot: ( i -- ) -- )
145     [ iterate-step iterate-next (each-integer) ]
146     [ 3drop ] if-iterate? ; inline recursive
147
148 : (find-integer) ( i n quot: ( i -- ? ) -- i )
149     [
150         iterate-step
151         [ [ ] ] 2dip
152         [ iterate-next (find-integer) ] 2curry bi-curry if
153     ] [ 3drop f ] if-iterate? ; inline recursive
154
155 : (all-integers?) ( i n quot: ( i -- ? ) -- ? )
156     [
157         iterate-step
158         [ iterate-next (all-integers?) ] 3curry
159         [ f ] if
160     ] [ 3drop t ] if-iterate? ; inline recursive
161
162 : each-integer ( n quot -- )
163     iterate-prep (each-integer) ; inline
164
165 : times ( n quot -- )
166     [ drop ] prepose each-integer ; inline
167
168 : find-integer ( n quot -- i )
169     iterate-prep (find-integer) ; inline
170
171 : all-integers? ( n quot -- ? )
172     iterate-prep (all-integers?) ; inline
173
174 : find-last-integer ( n quot: ( i -- ? ) -- i )
175     over 0 < [
176         2drop f
177     ] [
178         [ call ] 2keep rot [
179             drop
180         ] [
181             [ 1 - ] dip find-last-integer
182         ] if
183     ] if ; inline recursive