factor/basis/math/functions/functions.factor

394 lines
8.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2009-02-20 21:14:54 -05:00
USING: math kernel math.constants math.private math.bits
math.libm combinators fry math.order sequences ;
2007-09-20 18:09:08 -04:00
IN: math.functions
GENERIC: sqrt ( x -- y ) foldable
M: real sqrt
>float dup 0.0 <
[ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
: factor-2s ( n -- r s )
2015-09-08 19:15:10 -04:00
! factor an integer into 2^r * s
dup 0 = [ 1 ] [
2010-02-09 21:04:58 -05:00
[ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
] if ; inline
<PRIVATE
2012-08-31 18:10:48 -04:00
: (^fixnum) ( z w -- z^w )
[ 1 ] 2dip
[ dup zero? ] [
dup odd? [
[ [ * ] keep ] [ 1 - ] bi*
] when [ sq ] [ 2/ ] bi*
] until 2drop ; inline
: (^bignum) ( z w -- z^w )
make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
: (^n) ( z w -- z^w )
2012-08-31 18:10:48 -04:00
dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
GENERIC# ^n 1 ( z w -- z^w ) foldable
M: fixnum ^n (^n) ;
2012-08-31 18:10:48 -04:00
M: bignum ^n
[ factor-2s ] dip [ (^n) ] keep rot * shift ;
M: ratio ^n
2010-02-09 21:04:58 -05:00
[ >fraction ] dip '[ _ ^n ] bi@ / ;
M: float ^n (^n) ;
M: complex ^n (^n) ;
2007-09-20 18:09:08 -04:00
2008-09-02 03:02:05 -04:00
: integer^ ( x y -- z )
dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
2008-09-02 03:02:05 -04:00
PRIVATE>
2008-09-02 03:02:05 -04:00
: >float-rect ( z -- x y )
>rect [ >float ] bi@ ; inline
: >polar ( z -- abs arg )
>float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
2008-09-02 03:02:05 -04:00
2009-08-30 07:19:14 -04:00
: cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
2008-09-02 03:02:05 -04:00
: polar> ( abs arg -- z ) cis * ; inline
GENERIC: e^ ( x -- y )
2009-08-30 07:19:14 -04:00
M: float e^ fexp ; inline
2009-08-30 07:19:14 -04:00
M: real e^ >float e^ ; inline
2009-08-30 07:19:14 -04:00
M: complex e^ >rect [ e^ ] dip polar> ; inline
2009-08-30 07:19:14 -04:00
<PRIVATE
2008-09-02 03:02:05 -04:00
: ^mag ( w abs arg -- magnitude )
2009-08-30 07:19:14 -04:00
[ >float-rect swap ]
[ >float swap >float fpow ]
[ rot * e^ /f ]
2009-08-30 07:19:14 -04:00
tri* ; inline
2008-09-02 03:02:05 -04:00
: ^theta ( w abs arg -- theta )
[ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
2008-09-02 03:02:05 -04:00
: ^complex ( x y -- z )
swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
: real^? ( x y -- ? )
2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
: 0^ ( zero x -- z )
swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
2007-09-20 18:09:08 -04:00
2010-02-09 21:04:58 -05:00
: (^mod) ( x y n -- z )
[ make-bits 1 ] dip dup
'[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
: ^ ( x y -- z )
{
{ [ over zero? ] [ 0^ ] }
{ [ dup integer? ] [ integer^ ] }
2009-08-30 07:19:14 -04:00
{ [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
[ ^complex ]
} cond ; inline
: nth-root ( n x -- y ) swap recip ^ ; inline
2007-09-20 18:09:08 -04:00
: lcm ( a b -- c )
2016-03-19 15:19:25 -04:00
[ * ] 2keep simple-gcd /i ; foldable
2007-09-20 18:09:08 -04:00
2009-04-02 18:00:29 -04:00
: divisor? ( m n -- ? )
mod 0 = ; inline
2009-04-01 19:29:19 -04:00
ERROR: non-trivial-divisor n ;
2007-09-20 18:09:08 -04:00
: mod-inv ( x n -- y )
2009-01-23 19:20:47 -05:00
[ nip ] [ gcd 1 = ] 2bi
[ dup 0 < [ + ] [ nip ] if ]
[ non-trivial-divisor ] if ; foldable
2007-09-20 18:09:08 -04:00
: ^mod ( x y n -- z )
2010-02-09 21:04:58 -05:00
over 0 <
[ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
2007-09-20 18:09:08 -04:00
GENERIC: absq ( x -- y ) foldable
M: real absq sq ; inline
2007-09-20 18:09:08 -04:00
: ~abs ( x y epsilon -- ? )
[ - abs ] dip < ;
2007-09-20 18:09:08 -04:00
: ~rel ( x y epsilon -- ? )
[ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
2007-09-20 18:09:08 -04:00
: ~ ( x y epsilon -- ? )
{
{ [ dup zero? ] [ drop number= ] }
{ [ dup 0 < ] [ neg ~rel ] }
2008-04-11 13:56:48 -04:00
[ ~abs ]
2007-09-20 18:09:08 -04:00
} cond ;
: conjugate ( z -- z* ) >rect neg rect> ; inline
: arg ( z -- arg ) >float-rect swap fatan2 ; inline
2008-05-26 18:15:54 -04:00
: [-1,1]? ( x -- ? )
dup complex? [ drop f ] [ abs 1 <= ] if ; inline
: >=1? ( x -- ? )
dup complex? [ drop f ] [ 1 >= ] if ; inline
GENERIC: frexp ( x -- y exp )
M: float frexp
dup fp-special? [ dup zero? ] unless* [ 0 ] [
double>bits
[ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
[ -52 shift 0x7ff bitand 1022 - ] bi
] if ; inline
M: integer frexp
[ 0.0 0 ] [
dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
2011-11-23 21:49:33 -05:00
52 swap - shift 0x000f,ffff,ffff,ffff bitand
0.5 double>bits bitor bits>double
] [ 1 + ] bi [ * ] dip
] if-zero ; inline
2013-04-24 14:19:03 -04:00
DEFER: copysign
GENERIC# ldexp 1 ( x exp -- y )
M: float ldexp
over fp-special? [ over zero? ] unless* [ drop ] [
[ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
{
2013-04-24 14:19:03 -04:00
{ [ dup -1074 < ] [ drop 0 copysign ] }
{ [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
[
dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
[ -0x7ff0,0000,0000,0001 bitand ]
[ 1023 + 52 shift bitor bits>double ]
[ * ] tri*
]
} cond
] if ;
M: integer ldexp
2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
2008-09-02 03:02:05 -04:00
GENERIC: log ( x -- y )
2009-08-30 07:19:14 -04:00
M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
2007-09-20 18:09:08 -04:00
2009-08-30 07:19:14 -04:00
M: real log >float log ; inline
M: complex log >polar [ flog ] dip rect> ; inline
<PRIVATE
: most-negative-finite-float ( -- x )
2011-11-23 21:49:33 -05:00
-0x1.ffff,ffff,ffff,fp1023 >integer ; inline
2015-06-02 14:54:05 -04:00
: most-positive-finite-float ( -- x )
2011-11-23 21:49:33 -05:00
0x1.ffff,ffff,ffff,fp1023 >integer ; inline
2015-06-02 14:54:05 -04:00
2011-11-23 21:49:33 -05:00
CONSTANT: log-2 0x1.62e42fefa39efp-1
CONSTANT: log10-2 0x1.34413509f79ffp-2
2015-06-02 14:54:05 -04:00
: representable-as-float? ( x -- ? )
most-negative-finite-float
most-positive-finite-float between? ; inline
: (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
[ dup ] dip '[
2015-06-02 14:54:05 -04:00
dup representable-as-float?
[ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
] call ; inline
PRIVATE>
M: bignum log [ log ] log-2 (bignum-log) ;
GENERIC: log1+ ( x -- y )
M: object log1+ 1 + log ; inline
M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
2007-09-20 18:09:08 -04:00
: 10^ ( x -- y ) 10 swap ^ ; inline
GENERIC: log10 ( x -- y ) foldable
M: real log10 >float flog10 ; inline
M: complex log10 log 10 log / ; inline
M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
2008-11-29 04:49:24 -05:00
GENERIC: cos ( x -- y ) foldable
M: complex cos
>float-rect
[ [ fcos ] [ fcosh ] bi* * ]
[ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
2008-11-29 04:49:24 -05:00
2009-08-30 07:19:14 -04:00
M: float cos fcos ; inline
M: real cos >float cos ; inline
2007-09-20 18:09:08 -04:00
: sec ( x -- y ) cos recip ; inline
2008-11-29 04:49:24 -05:00
GENERIC: cosh ( x -- y ) foldable
M: complex cosh
>float-rect
[ [ fcosh ] [ fcos ] bi* * ]
[ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
2008-11-29 04:49:24 -05:00
2009-08-30 07:19:14 -04:00
M: float cosh fcosh ; inline
M: real cosh >float cosh ; inline
2007-09-20 18:09:08 -04:00
: sech ( x -- y ) cosh recip ; inline
2008-11-29 04:49:24 -05:00
GENERIC: sin ( x -- y ) foldable
M: complex sin
>float-rect
[ [ fsin ] [ fcosh ] bi* * ]
[ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
2008-11-29 04:49:24 -05:00
2009-08-30 07:19:14 -04:00
M: float sin fsin ; inline
M: real sin >float sin ; inline
2007-09-20 18:09:08 -04:00
: cosec ( x -- y ) sin recip ; inline
2008-11-29 04:49:24 -05:00
GENERIC: sinh ( x -- y ) foldable
2009-04-01 19:29:19 -04:00
M: complex sinh
>float-rect
[ [ fsinh ] [ fcos ] bi* * ]
[ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
2008-11-29 04:49:24 -05:00
2009-08-30 07:19:14 -04:00
M: float sinh fsinh ; inline
M: real sinh >float sinh ; inline
2007-09-20 18:09:08 -04:00
: cosech ( x -- y ) sinh recip ; inline
2008-11-29 04:49:24 -05:00
GENERIC: tan ( x -- y ) foldable
M: complex tan [ sin ] [ cos ] bi / ;
2009-08-30 07:19:14 -04:00
M: float tan ftan ; inline
M: real tan >float tan ; inline
2008-11-29 04:49:24 -05:00
GENERIC: tanh ( x -- y ) foldable
2007-09-20 18:09:08 -04:00
2008-11-29 04:49:24 -05:00
M: complex tanh [ sinh ] [ cosh ] bi / ;
2009-08-30 07:19:14 -04:00
M: float tanh ftanh ; inline
M: real tanh >float tanh ; inline
2007-09-20 18:09:08 -04:00
2008-05-26 18:15:54 -04:00
: cot ( x -- y ) tan recip ; inline
2007-09-20 18:09:08 -04:00
2008-05-26 18:15:54 -04:00
: coth ( x -- y ) tanh recip ; inline
2007-09-20 18:09:08 -04:00
2008-05-26 18:15:54 -04:00
: acosh ( x -- y )
2009-05-06 00:32:23 -04:00
dup sq 1 - sqrt + log ; inline
2007-09-20 18:09:08 -04:00
: asech ( x -- y ) recip acosh ; inline
2008-05-26 18:15:54 -04:00
: asinh ( x -- y )
2009-05-06 00:32:23 -04:00
dup sq 1 + sqrt + log ; inline
2007-09-20 18:09:08 -04:00
: acosech ( x -- y ) recip asinh ; inline
2008-05-26 18:15:54 -04:00
: atanh ( x -- y )
2009-05-06 00:32:23 -04:00
[ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
2007-09-20 18:09:08 -04:00
: acoth ( x -- y ) recip atanh ; inline
: i* ( x -- y ) >rect neg swap rect> ;
: -i* ( x -- y ) >rect swap neg rect> ;
: asin ( x -- y )
2009-08-30 07:19:14 -04:00
dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
2007-09-20 18:09:08 -04:00
: acos ( x -- y )
2015-06-02 14:54:05 -04:00
dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
2007-09-20 18:09:08 -04:00
2008-11-29 04:49:24 -05:00
GENERIC: atan ( x -- y ) foldable
2009-08-30 07:19:14 -04:00
M: complex atan i* atanh i* ; inline
M: float atan fatan ; inline
2008-11-29 04:49:24 -05:00
2009-08-30 07:19:14 -04:00
M: real atan >float atan ; inline
2007-09-20 18:09:08 -04:00
: asec ( x -- y ) recip acos ; inline
: acosec ( x -- y ) recip asin ; inline
: acot ( x -- y ) recip atan ; inline
: truncate ( x -- y ) dup 1 mod - ; inline
GENERIC: round ( x -- y )
M: integer round ; inline
M: ratio round
>fraction [ /mod abs 2 * ] keep >= [ dup 0 < -1 1 ? + ] when ;
M: float round dup sgn 2 /f + truncate ;
: floor ( x -- y )
2009-08-11 19:15:53 -04:00
dup 1 mod
2012-06-22 11:38:34 -04:00
[ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
: ceiling ( x -- y ) neg floor neg ; foldable
2009-05-05 14:55:00 -04:00
: floor-to ( x step -- y )
2009-08-11 19:15:53 -04:00
[ [ / floor ] [ * ] bi ] unless-zero ;
2009-05-05 14:55:00 -04:00
: lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
: roots ( x t -- seq )
[ [ log ] [ recip ] bi* * e^ ]
[ recip 2pi * 0 swap complex boa e^ ]
[ iota [ ^ * ] 2with map ] tri ;
: sigmoid ( x -- y ) neg e^ 1 + recip ; inline
GENERIC: signum ( x -- y )
M: real signum sgn ;
M: complex signum dup abs / ;
2012-06-01 11:24:35 -04:00
MATH: copysign ( x y -- x' )
2012-06-22 11:38:34 -04:00
M: real copysign >float copysign ;
2012-06-01 11:24:35 -04:00
M: float copysign
[ double>bits ] [ fp-sign ] bi*
[ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if
bits>double ;