2009-09-12 23:20:13 -04:00
|
|
|
! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2016-03-19 15:19:25 -04:00
|
|
|
USING: kernel kernel.private ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: math
|
|
|
|
|
2013-03-10 15:04:34 -04:00
|
|
|
BUILTIN: fixnum ;
|
|
|
|
BUILTIN: bignum ;
|
|
|
|
BUILTIN: float ;
|
|
|
|
|
2015-06-25 21:02:03 -04:00
|
|
|
PRIMITIVE: bits>double ( n -- x )
|
|
|
|
PRIMITIVE: bits>float ( n -- x )
|
|
|
|
PRIMITIVE: double>bits ( x -- n )
|
|
|
|
PRIMITIVE: float>bits ( x -- n )
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
PRIMITIVE: bignum* ( x y -- z )
|
|
|
|
PRIMITIVE: bignum+ ( x y -- z )
|
|
|
|
PRIMITIVE: bignum- ( x y -- z )
|
|
|
|
PRIMITIVE: bignum-bit? ( x n -- ? )
|
|
|
|
PRIMITIVE: bignum-bitand ( x y -- z )
|
|
|
|
PRIMITIVE: bignum-bitnot ( x -- y )
|
|
|
|
PRIMITIVE: bignum-bitor ( x y -- z )
|
|
|
|
PRIMITIVE: bignum-bitxor ( x y -- z )
|
|
|
|
PRIMITIVE: bignum-gcd ( x y -- z )
|
|
|
|
PRIMITIVE: bignum-log2 ( x -- n )
|
|
|
|
PRIMITIVE: bignum-mod ( x y -- z )
|
|
|
|
PRIMITIVE: bignum-shift ( x y -- z )
|
|
|
|
PRIMITIVE: bignum/i ( x y -- z )
|
|
|
|
PRIMITIVE: bignum/mod ( x y -- z w )
|
|
|
|
PRIMITIVE: bignum< ( x y -- ? )
|
|
|
|
PRIMITIVE: bignum<= ( x y -- ? )
|
|
|
|
PRIMITIVE: bignum= ( x y -- ? )
|
|
|
|
PRIMITIVE: bignum> ( x y -- ? )
|
|
|
|
PRIMITIVE: bignum>= ( x y -- ? )
|
|
|
|
PRIMITIVE: bignum>fixnum ( x -- y )
|
|
|
|
PRIMITIVE: bignum>fixnum-strict ( x -- y )
|
|
|
|
PRIMITIVE: both-fixnums? ( x y -- ? )
|
|
|
|
PRIMITIVE: fixnum* ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum*fast ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum+ ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum+fast ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum- ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-bitand ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-bitnot ( x -- y )
|
|
|
|
PRIMITIVE: fixnum-bitor ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-bitxor ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-fast ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-mod ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-shift ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum-shift-fast ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum/i ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum/i-fast ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum/mod ( x y -- z w )
|
|
|
|
PRIMITIVE: fixnum/mod-fast ( x y -- z w )
|
|
|
|
PRIMITIVE: fixnum< ( x y -- ? )
|
|
|
|
PRIMITIVE: fixnum<= ( x y -- z )
|
|
|
|
PRIMITIVE: fixnum> ( x y -- ? )
|
|
|
|
PRIMITIVE: fixnum>= ( x y -- ? )
|
|
|
|
PRIMITIVE: fixnum>bignum ( x -- y )
|
|
|
|
PRIMITIVE: fixnum>float ( x -- y )
|
|
|
|
PRIMITIVE: float* ( x y -- z )
|
|
|
|
PRIMITIVE: float+ ( x y -- z )
|
|
|
|
PRIMITIVE: float- ( x y -- z )
|
|
|
|
PRIMITIVE: float-u< ( x y -- ? )
|
|
|
|
PRIMITIVE: float-u<= ( x y -- ? )
|
|
|
|
PRIMITIVE: float-u> ( x y -- ? )
|
|
|
|
PRIMITIVE: float-u>= ( x y -- ? )
|
|
|
|
PRIMITIVE: float/f ( x y -- z )
|
|
|
|
PRIMITIVE: float< ( x y -- ? )
|
|
|
|
PRIMITIVE: float<= ( x y -- ? )
|
|
|
|
PRIMITIVE: float= ( x y -- ? )
|
|
|
|
PRIMITIVE: float> ( x y -- ? )
|
|
|
|
PRIMITIVE: float>= ( x y -- ? )
|
|
|
|
PRIMITIVE: float>bignum ( x -- y )
|
|
|
|
PRIMITIVE: float>fixnum ( x -- y )
|
|
|
|
PRIVATE>
|
|
|
|
|
2008-06-12 06:49:46 -04:00
|
|
|
GENERIC: >fixnum ( x -- n ) foldable
|
|
|
|
GENERIC: >bignum ( x -- n ) foldable
|
|
|
|
GENERIC: >integer ( x -- n ) foldable
|
2007-09-20 18:09:08 -04:00
|
|
|
GENERIC: >float ( x -- y ) foldable
|
2012-09-16 10:46:22 -04:00
|
|
|
GENERIC: integer>fixnum ( x -- y ) foldable
|
|
|
|
GENERIC: integer>fixnum-strict ( x -- y ) foldable
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
GENERIC: numerator ( a/b -- a )
|
|
|
|
GENERIC: denominator ( a/b -- b )
|
2015-07-19 18:59:40 -04:00
|
|
|
GENERIC: >fraction ( a/b -- a b )
|
2008-06-28 03:36:20 -04:00
|
|
|
|
|
|
|
GENERIC: real-part ( z -- x )
|
|
|
|
GENERIC: imaginary-part ( z -- y )
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
MATH: number= ( x y -- ? ) foldable
|
2007-10-14 20:38:23 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: object number= 2drop f ;
|
|
|
|
|
|
|
|
MATH: < ( x y -- ? ) foldable
|
|
|
|
MATH: <= ( x y -- ? ) foldable
|
|
|
|
MATH: > ( x y -- ? ) foldable
|
|
|
|
MATH: >= ( x y -- ? ) foldable
|
2009-09-12 23:20:13 -04:00
|
|
|
|
2009-09-10 23:45:18 -04:00
|
|
|
MATH: unordered? ( x y -- ? ) foldable
|
2009-09-12 23:20:13 -04:00
|
|
|
MATH: u< ( x y -- ? ) foldable
|
|
|
|
MATH: u<= ( x y -- ? ) foldable
|
|
|
|
MATH: u> ( x y -- ? ) foldable
|
|
|
|
MATH: u>= ( x y -- ? ) foldable
|
2009-09-10 23:45:18 -04:00
|
|
|
|
|
|
|
M: object unordered? 2drop f ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
MATH: + ( x y -- z ) foldable
|
|
|
|
MATH: - ( x y -- z ) foldable
|
|
|
|
MATH: * ( x y -- z ) foldable
|
|
|
|
MATH: / ( x y -- z ) foldable
|
2008-04-28 22:26:31 -04:00
|
|
|
MATH: /f ( x y -- z ) foldable
|
2007-09-20 18:09:08 -04:00
|
|
|
MATH: /i ( x y -- z ) foldable
|
|
|
|
MATH: mod ( x y -- z ) foldable
|
|
|
|
|
|
|
|
MATH: /mod ( x y -- z w ) foldable
|
|
|
|
|
|
|
|
MATH: bitand ( x y -- z ) foldable
|
|
|
|
MATH: bitor ( x y -- z ) foldable
|
|
|
|
MATH: bitxor ( x y -- z ) foldable
|
2017-06-01 14:58:58 -04:00
|
|
|
GENERIC#: shift 1 ( x n -- y ) foldable
|
2007-09-20 18:09:08 -04:00
|
|
|
GENERIC: bitnot ( x -- y ) foldable
|
2017-06-01 14:58:58 -04:00
|
|
|
GENERIC#: bit? 1 ( x n -- ? ) foldable
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-28 22:26:31 -04:00
|
|
|
GENERIC: abs ( x -- y ) foldable
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
GENERIC: (log2) ( x -- n ) foldable
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-08-12 00:09:02 -04:00
|
|
|
ERROR: log2-expects-positive x ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: log2 ( x -- n )
|
2015-08-13 19:13:05 -04:00
|
|
|
dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-28 22:26:31 -04:00
|
|
|
: zero? ( x -- ? ) 0 number= ; inline
|
2012-09-10 20:59:03 -04:00
|
|
|
: 2/ ( x -- y ) -1 shift ; inline
|
2008-04-18 17:51:09 -04:00
|
|
|
: sq ( x -- y ) dup * ; inline
|
2009-05-21 19:49:22 -04:00
|
|
|
: neg ( x -- -x ) -1 * ; inline
|
2008-04-26 00:12:44 -04:00
|
|
|
: sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
|
2009-05-01 20:58:24 -04:00
|
|
|
: ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
|
2009-02-02 14:43:54 -05:00
|
|
|
: rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
|
2007-09-20 18:09:08 -04:00
|
|
|
: 2^ ( n -- 2^n ) 1 swap shift ; inline
|
2010-11-16 06:13:15 -05:00
|
|
|
: even? ( n -- ? ) 1 bitand zero? ; inline
|
|
|
|
: odd? ( n -- ? ) 1 bitand 1 number= ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2016-07-28 13:41:02 -04:00
|
|
|
GENERIC: neg? ( x -- ? )
|
2011-11-23 22:51:05 -05:00
|
|
|
|
2010-03-05 03:21:10 -05:00
|
|
|
: if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
|
2009-08-14 15:27:23 -04:00
|
|
|
[ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
|
|
|
|
|
2017-05-03 21:13:37 -04:00
|
|
|
: when-zero ( ... n quot: ( ... -- ... x ) -- ... x ) [ ] if-zero ; inline
|
2009-08-14 15:27:23 -04:00
|
|
|
|
2017-05-03 21:13:37 -04:00
|
|
|
: unless-zero ( ... n quot: ( ... n -- ... ) -- ... ) [ ] swap if-zero ; inline
|
2009-08-14 15:27:23 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
UNION: integer fixnum bignum ;
|
|
|
|
|
2015-07-30 12:41:58 -04:00
|
|
|
TUPLE: ratio
|
|
|
|
{ numerator integer read-only }
|
|
|
|
{ denominator integer read-only } ;
|
2009-04-30 01:27:35 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
UNION: rational integer ratio ;
|
|
|
|
|
2011-11-26 18:37:58 -05:00
|
|
|
M: rational neg? 0 < ; inline
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
UNION: real rational float ;
|
|
|
|
|
2015-07-30 12:41:58 -04:00
|
|
|
TUPLE: complex
|
|
|
|
{ real real read-only }
|
|
|
|
{ imaginary real read-only } ;
|
2009-04-30 01:27:35 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
UNION: number real complex ;
|
|
|
|
|
2012-09-02 12:47:31 -04:00
|
|
|
GENERIC: recip ( x -- y )
|
|
|
|
|
|
|
|
M: number recip 1 swap / ; inline
|
|
|
|
|
2015-07-30 12:41:58 -04:00
|
|
|
: rect> ( x y -- z )
|
|
|
|
! Note: an imaginary 0.0 should still create a complex
|
|
|
|
dup 0 = [ drop ] [ complex boa ] if ; inline
|
|
|
|
|
|
|
|
GENERIC: >rect ( z -- x y )
|
|
|
|
|
|
|
|
M: real >rect 0 ; inline
|
|
|
|
|
|
|
|
M: complex >rect [ real-part ] [ imaginary-part ] bi ; inline
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: (gcd) ( b a x y -- a d )
|
|
|
|
swap [
|
|
|
|
nip
|
|
|
|
] [
|
|
|
|
[ /mod [ over * swapd - ] dip ] keep (gcd)
|
|
|
|
] if-zero ; inline recursive
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: gcd ( x y -- a d )
|
|
|
|
[ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; inline
|
|
|
|
|
2016-03-19 15:19:25 -04:00
|
|
|
MATH: simple-gcd ( x y -- d ) foldable
|
2015-07-30 12:41:58 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2016-03-19 15:19:25 -04:00
|
|
|
: fixnum-gcd ( x y -- d ) { fixnum fixnum } declare gcd nip ;
|
2015-07-30 12:41:58 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2016-03-19 15:19:25 -04:00
|
|
|
M: fixnum simple-gcd fixnum-gcd ; inline
|
2015-07-30 12:41:58 -04:00
|
|
|
|
2016-03-19 15:19:25 -04:00
|
|
|
M: bignum simple-gcd bignum-gcd ; inline
|
2015-07-30 12:41:58 -04:00
|
|
|
|
2012-07-21 13:22:44 -04:00
|
|
|
: fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
|
2009-05-09 19:17:30 -04:00
|
|
|
|
2009-05-09 10:49:31 -04:00
|
|
|
GENERIC: fp-special? ( x -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
GENERIC: fp-nan? ( x -- ? )
|
2009-05-09 10:49:31 -04:00
|
|
|
GENERIC: fp-qnan? ( x -- ? )
|
|
|
|
GENERIC: fp-snan? ( x -- ? )
|
|
|
|
GENERIC: fp-infinity? ( x -- ? )
|
|
|
|
GENERIC: fp-nan-payload ( x -- bits )
|
2009-09-12 17:24:07 -04:00
|
|
|
GENERIC: fp-sign ( x -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-20 04:55:19 -04:00
|
|
|
M: object fp-special? drop f ; inline
|
|
|
|
M: object fp-nan? drop f ; inline
|
|
|
|
M: object fp-qnan? drop f ; inline
|
|
|
|
M: object fp-snan? drop f ; inline
|
|
|
|
M: object fp-infinity? drop f ; inline
|
2008-09-03 02:35:03 -04:00
|
|
|
|
2009-05-09 10:49:31 -04:00
|
|
|
: <fp-nan> ( payload -- nan )
|
2011-11-23 21:49:33 -05:00
|
|
|
0x7ff0000000000000 bitor bits>double ; inline
|
2009-05-09 10:49:31 -04:00
|
|
|
|
2009-08-20 04:55:19 -04:00
|
|
|
GENERIC: next-float ( m -- n )
|
|
|
|
GENERIC: prev-float ( m -- n )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-07 20:44:49 -05:00
|
|
|
: next-power-of-2 ( m -- n )
|
2009-05-01 20:58:24 -04:00
|
|
|
dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-17 13:22:24 -04:00
|
|
|
: power-of-2? ( n -- ? )
|
2009-05-01 20:58:24 -04:00
|
|
|
dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
|
2008-04-17 13:22:24 -04:00
|
|
|
|
|
|
|
: align ( m w -- n )
|
2009-05-01 20:58:24 -04:00
|
|
|
1 - [ + ] keep bitnot bitand ; inline
|
2007-10-14 20:38:23 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-12-22 06:41:01 -05:00
|
|
|
: iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-13 06:06:28 -05:00
|
|
|
: if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: iterate-step ( i n quot -- i n quot )
|
2015-09-08 19:15:10 -04:00
|
|
|
! Apply quot to i, keep i and quot, hide n.
|
2009-02-05 04:29:59 -05:00
|
|
|
[ nip call ] 3keep ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2013-03-04 20:32:11 -05:00
|
|
|
: iterate-rot ( ? i n quot -- i n quot ? )
|
|
|
|
[ rot ] dip swap ; inline
|
|
|
|
|
2009-05-01 20:58:24 -04:00
|
|
|
: iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2010-03-05 03:21:10 -05:00
|
|
|
: (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
|
2007-09-20 18:09:08 -04:00
|
|
|
[ iterate-step iterate-next (each-integer) ]
|
2008-07-18 20:22:59 -04:00
|
|
|
[ 3drop ] if-iterate? ; inline recursive
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2013-03-04 20:32:11 -05:00
|
|
|
: (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
2013-03-04 20:32:11 -05:00
|
|
|
iterate-step iterate-rot
|
|
|
|
[ 2drop ] [ iterate-next (find-integer) ] if
|
2008-07-18 20:22:59 -04:00
|
|
|
] [ 3drop f ] if-iterate? ; inline recursive
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-03-05 03:21:10 -05:00
|
|
|
: (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
2013-03-04 20:32:11 -05:00
|
|
|
iterate-step iterate-rot
|
|
|
|
[ iterate-next (all-integers?) ] [ 3drop f ] if
|
2008-07-18 20:22:59 -04:00
|
|
|
] [ 3drop t ] if-iterate? ; inline recursive
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-05-13 17:30:19 -04:00
|
|
|
: each-integer ( ... n quot: ( ... i -- ... ) -- ... )
|
2007-09-20 18:09:08 -04:00
|
|
|
iterate-prep (each-integer) ; inline
|
|
|
|
|
2010-05-13 17:30:19 -04:00
|
|
|
: times ( ... n quot: ( ... -- ... ) -- ... )
|
2008-04-26 00:12:44 -04:00
|
|
|
[ drop ] prepose each-integer ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2015-09-30 00:25:11 -04:00
|
|
|
: find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
|
2007-09-20 18:09:08 -04:00
|
|
|
iterate-prep (find-integer) ; inline
|
|
|
|
|
2010-05-13 17:30:19 -04:00
|
|
|
: all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
iterate-prep (all-integers?) ; inline
|
|
|
|
|
2015-09-30 00:25:11 -04:00
|
|
|
: find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
|
2007-09-20 18:09:08 -04:00
|
|
|
over 0 < [
|
|
|
|
2drop f
|
|
|
|
] [
|
2009-02-05 04:29:59 -05:00
|
|
|
[ call ] 2keep rot [
|
2007-09-20 18:09:08 -04:00
|
|
|
drop
|
|
|
|
] [
|
2009-05-01 20:58:24 -04:00
|
|
|
[ 1 - ] dip find-last-integer
|
2007-09-20 18:09:08 -04:00
|
|
|
] if
|
2008-07-18 20:22:59 -04:00
|
|
|
] if ; inline recursive
|