Improve math.functions

db4
Slava Pestov 2008-05-26 17:15:54 -05:00
parent a77bbfc28e
commit 8af320a2c0
3 changed files with 67 additions and 25 deletions

View File

@ -39,6 +39,12 @@ IN: math.functions.tests
[ 0.0 ] [ 0 sin ] unit-test
[ 0.0 ] [ 0 asin ] unit-test
[ t ] [ 10 atan real? ] unit-test
[ f ] [ 10 atanh real? ] unit-test
[ t ] [ 10 asin sin 10 1.e-10 ~ ] unit-test
[ t ] [ -1 sqrt neg dup acos cos 1.e-10 ~ ] unit-test
[ 100 ] [ 100 100 gcd nip ] unit-test
[ 100 ] [ 1000 100 gcd nip ] unit-test
[ 100 ] [ 100 1000 gcd nip ] unit-test

View File

@ -125,74 +125,90 @@ M: real absq sq ;
M: number (^)
swap >polar 3dup ^theta >r ^mag r> polar> ;
: [-1,1]? ( x -- ? )
dup complex? [ drop f ] [ abs 1 <= ] if ; inline
: >=1? ( x -- ? )
dup complex? [ drop f ] [ 1 >= ] if ; inline
: exp ( x -- y ) >rect swap fexp swap polar> ; inline
: log ( x -- y ) >polar swap flog swap rect> ; inline
: cos ( x -- y )
>float-rect 2dup
fcosh swap fcos * -rot
fsinh swap fsin neg * rect> ; foldable
dup complex? [
>float-rect 2dup
fcosh swap fcos * -rot
fsinh swap fsin neg * rect>
] [ fcos ] if ; foldable
: sec ( x -- y ) cos recip ; inline
: cosh ( x -- y )
>float-rect 2dup
fcos swap fcosh * -rot
fsin swap fsinh * rect> ; foldable
dup complex? [
>float-rect 2dup
fcos swap fcosh * -rot
fsin swap fsinh * rect>
] [ fcosh ] if ; foldable
: sech ( x -- y ) cosh recip ; inline
: sin ( x -- y )
>float-rect 2dup
fcosh swap fsin * -rot
fsinh swap fcos * rect> ; foldable
dup complex? [
>float-rect 2dup
fcosh swap fsin * -rot
fsinh swap fcos * rect>
] [ fsin ] if ; foldable
: cosec ( x -- y ) sin recip ; inline
: sinh ( x -- y )
>float-rect 2dup
fcos swap fsinh * -rot
fsin swap fcosh * rect> ; foldable
dup complex? [
>float-rect 2dup
fcos swap fsinh * -rot
fsin swap fcosh * rect>
] [ fsinh ] if ; foldable
: cosech ( x -- y ) sinh recip ; inline
: tan ( x -- y ) dup sin swap cos / ; inline
: tan ( x -- y )
dup complex? [ dup sin swap cos / ] [ ftan ] if ; inline
: tanh ( x -- y ) dup sinh swap cosh / ; inline
: tanh ( x -- y )
dup complex? [ dup sinh swap cosh / ] [ ftanh ] if ; inline
: cot ( x -- y ) dup cos swap sin / ; inline
: cot ( x -- y ) tan recip ; inline
: coth ( x -- y ) dup cosh swap sinh / ; inline
: coth ( x -- y ) tanh recip ; inline
: acosh ( x -- y ) dup sq 1- sqrt + log ; inline
: acosh ( x -- y )
dup >=1? [ facosh ] [ dup sq 1- sqrt + log ] if ; inline
: asech ( x -- y ) recip acosh ; inline
: asinh ( x -- y ) dup sq 1+ sqrt + log ; inline
: asinh ( x -- y )
dup complex? [ dup sq 1+ sqrt + log ] [ fasinh ] if ; inline
: acosech ( x -- y ) recip asinh ; inline
: atanh ( x -- y ) dup 1+ swap 1- neg / log 2 / ; inline
: atanh ( x -- y )
dup [-1,1]? [ fatanh ] [ dup 1+ swap 1- neg / log 2 / ] if ; inline
: acoth ( x -- y ) recip atanh ; inline
: [-1,1]? ( x -- ? )
dup complex? [ drop f ] [ abs 1 <= ] if ; inline
: i* ( x -- y ) >rect neg swap rect> ;
: -i* ( x -- y ) >rect swap neg rect> ;
: asin ( x -- y )
dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
: acos ( x -- y )
dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
inline
: atan ( x -- y )
dup [-1,1]? [ >float fatan ] [ i* atanh i* ] if ; inline
dup complex? [ i* atanh i* ] [ fatan ] if ; inline
: asec ( x -- y ) recip acos ; inline

View File

@ -15,6 +15,18 @@ IN: math.libm
"double" "libm" "atan" { "double" } alien-invoke ;
foldable
: facosh ( x -- y )
"double" "libm" "acosh" { "double" } alien-invoke ;
foldable
: fasinh ( x -- y )
"double" "libm" "asinh" { "double" } alien-invoke ;
foldable
: fatanh ( x -- y )
"double" "libm" "atanh" { "double" } alien-invoke ;
foldable
: fatan2 ( x y -- z )
"double" "libm" "atan2" { "double" "double" } alien-invoke ;
foldable
@ -27,6 +39,10 @@ IN: math.libm
"double" "libm" "sin" { "double" } alien-invoke ;
foldable
: ftan ( x -- y )
"double" "libm" "tan" { "double" } alien-invoke ;
foldable
: fcosh ( x -- y )
"double" "libm" "cosh" { "double" } alien-invoke ;
foldable
@ -35,6 +51,10 @@ IN: math.libm
"double" "libm" "sinh" { "double" } alien-invoke ;
foldable
: ftanh ( x -- y )
"double" "libm" "tanh" { "double" } alien-invoke ;
foldable
: fexp ( x -- y )
"double" "libm" "exp" { "double" } alien-invoke ;
foldable