math.functions: rename exp to e^ to be consistent with 2^ and 10^. update things.
parent
fee4d76b73
commit
f6398365bd
|
@ -156,7 +156,7 @@ MACRO: undo ( quot -- ) [undo] ;
|
||||||
\ undo 1 [ ] define-pop-inverse
|
\ undo 1 [ ] define-pop-inverse
|
||||||
\ map 1 [ [undo] '[ dup sequence? assure _ map ] ] define-pop-inverse
|
\ map 1 [ [undo] '[ dup sequence? assure _ map ] ] define-pop-inverse
|
||||||
|
|
||||||
\ exp \ log define-dual
|
\ e^ \ log define-dual
|
||||||
\ sq \ sqrt define-dual
|
\ sq \ sqrt define-dual
|
||||||
|
|
||||||
ERROR: missing-literal ;
|
ERROR: missing-literal ;
|
||||||
|
|
|
@ -70,8 +70,8 @@ IN: math.complex.tests
|
||||||
[ ] [ C{ 1 4 } coth drop ] unit-test
|
[ ] [ C{ 1 4 } coth drop ] unit-test
|
||||||
[ ] [ C{ 1 4 } cot drop ] unit-test
|
[ ] [ C{ 1 4 } cot drop ] unit-test
|
||||||
|
|
||||||
[ t ] [ 0.0 pi rect> exp C{ -1 0 } 1.0e-7 ~ ] unit-test
|
[ t ] [ 0.0 pi rect> e^ C{ -1 0 } 1.0e-7 ~ ] unit-test
|
||||||
[ t ] [ 0 pi rect> exp C{ -1 0 } 1.0e-7 ~ ] unit-test
|
[ t ] [ 0 pi rect> e^ C{ -1 0 } 1.0e-7 ~ ] unit-test
|
||||||
|
|
||||||
10 number-base [
|
10 number-base [
|
||||||
[ "C{ 1/2 2/3 }" ] [ C{ 1/2 2/3 } unparse ] unit-test
|
[ "C{ 1/2 2/3 }" ] [ C{ 1/2 2/3 } unparse ] unit-test
|
||||||
|
|
|
@ -49,7 +49,7 @@ ARTICLE: "power-functions" "Powers and logarithms"
|
||||||
"Squares:"
|
"Squares:"
|
||||||
{ $subsections sq sqrt }
|
{ $subsections sq sqrt }
|
||||||
"Exponential and natural logarithm:"
|
"Exponential and natural logarithm:"
|
||||||
{ $subsections exp cis log }
|
{ $subsections e^ cis log }
|
||||||
"Other logarithms:"
|
"Other logarithms:"
|
||||||
{ $subsections log1+ log10 }
|
{ $subsections log1+ log10 }
|
||||||
"Raising a number to a power:"
|
"Raising a number to a power:"
|
||||||
|
@ -108,13 +108,13 @@ HELP: align
|
||||||
{ $description "Outputs the least multiple of " { $snippet "w" } " greater than " { $snippet "m" } "." }
|
{ $description "Outputs the least multiple of " { $snippet "w" } " greater than " { $snippet "m" } "." }
|
||||||
{ $notes "This word will give an incorrect result if " { $snippet "w" } " is not a power of 2." } ;
|
{ $notes "This word will give an incorrect result if " { $snippet "w" } " is not a power of 2." } ;
|
||||||
|
|
||||||
HELP: exp
|
HELP: e^
|
||||||
{ $values { "x" number } { "y" number } }
|
{ $values { "x" number } { "y" number } }
|
||||||
{ $description "Exponential function, " { $snippet "y=e^x" } "." } ;
|
{ $description "Exponential function, " { $snippet "y=e^x" } "." } ;
|
||||||
|
|
||||||
HELP: frexp
|
HELP: frexp
|
||||||
{ $values { "x" number } { "y" float } { "exp" integer } }
|
{ $values { "x" number } { "y" float } { "exp" integer } }
|
||||||
{ $description "Break the number " { $snippet "x" } " into a normalized fraction " { $snippet "y" } " and an integral power of 2 " { $snippet "exp" } "." $nl "The function returns a number " { $snippet "y" } " in the interval [1/2, 1) or 0, and a number " { $snippet "exp" } " such that " { $snippet "x = y*(2**exp)" } "." } ;
|
{ $description "Break the number " { $snippet "x" } " into a normalized fraction " { $snippet "y" } " and an integral power of 2 " { $snippet "e^" } "." $nl "The function returns a number " { $snippet "y" } " in the interval [1/2, 1) or 0, and a number " { $snippet "exp" } " such that " { $snippet "x = y*(2**exp)" } "." } ;
|
||||||
|
|
||||||
HELP: log
|
HELP: log
|
||||||
{ $values { "x" number } { "y" number } }
|
{ $values { "x" number } { "y" number } }
|
||||||
|
@ -242,9 +242,9 @@ HELP: >polar
|
||||||
|
|
||||||
HELP: cis
|
HELP: cis
|
||||||
{ $values { "arg" "a real number" } { "z" "a complex number on the unit circle" } }
|
{ $values { "arg" "a real number" } { "z" "a complex number on the unit circle" } }
|
||||||
{ $description "Computes a point on the unit circle using Euler's formula for " { $snippet "exp(arg*i)" } "." } ;
|
{ $description "Computes a point on the unit circle using Euler's formula for " { $snippet "e^(arg*i)" } "." } ;
|
||||||
|
|
||||||
{ cis exp } related-words
|
{ cis e^ } related-words
|
||||||
|
|
||||||
HELP: polar>
|
HELP: polar>
|
||||||
{ $values { "abs" "a non-negative real number" } { "arg" real } { "z" number } }
|
{ $values { "abs" "a non-negative real number" } { "arg" real } { "z" number } }
|
||||||
|
@ -275,10 +275,6 @@ HELP: 10^
|
||||||
{ $values { "x" number } { "y" number } }
|
{ $values { "x" number } { "y" number } }
|
||||||
{ $description "Raises 10 to the power of " { $snippet "x" } ". If " { $snippet "x" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." } ;
|
{ $description "Raises 10 to the power of " { $snippet "x" } ". If " { $snippet "x" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." } ;
|
||||||
|
|
||||||
HELP: e^
|
|
||||||
{ $values { "x" number } { "y" number } }
|
|
||||||
{ $description "Raises " { $link e } " to the power of " { $snippet "x" } "." } ;
|
|
||||||
|
|
||||||
HELP: gcd
|
HELP: gcd
|
||||||
{ $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
|
{ $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
|
||||||
{ $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
|
{ $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
|
||||||
|
|
|
@ -72,11 +72,11 @@ CONSTANT: log10-factorial-1000 0x1.40f3593ed6f8ep11
|
||||||
[ 4.0 ] [ 10000.0 log10 ] unit-test
|
[ 4.0 ] [ 10000.0 log10 ] unit-test
|
||||||
[ $ log10-factorial-1000 t ] [ 1000 factorial [ log10 ] [ bignum? ] bi ] unit-test
|
[ $ log10-factorial-1000 t ] [ 1000 factorial [ log10 ] [ bignum? ] bi ] unit-test
|
||||||
|
|
||||||
[ t ] [ 1 exp e 1.e-10 ~ ] unit-test
|
[ t ] [ 1 e^ e 1.e-10 ~ ] unit-test
|
||||||
[ f ] [ 1 exp 0/0. 1.e-10 ~ ] unit-test
|
[ f ] [ 1 e^ 0/0. 1.e-10 ~ ] unit-test
|
||||||
[ f ] [ 0/0. 1 exp 1.e-10 ~ ] unit-test
|
[ f ] [ 0/0. 1 e^ 1.e-10 ~ ] unit-test
|
||||||
[ t ] [ 1.0 exp e 1.e-10 ~ ] unit-test
|
[ t ] [ 1.0 e^ e 1.e-10 ~ ] unit-test
|
||||||
[ t ] [ -1 exp e * 1.0 1.e-10 ~ ] unit-test
|
[ t ] [ -1 e^ e * 1.0 1.e-10 ~ ] unit-test
|
||||||
[ f ] [ 1/0. 1/0. 1.e-10 ~ ] unit-test
|
[ f ] [ 1/0. 1/0. 1.e-10 ~ ] unit-test
|
||||||
[ f ] [ 1/0. -1/0. 1.e-10 ~ ] unit-test
|
[ f ] [ 1/0. -1/0. 1.e-10 ~ ] unit-test
|
||||||
[ f ] [ 1/0. 0/0. 1.e-10 ~ ] unit-test
|
[ f ] [ 1/0. 0/0. 1.e-10 ~ ] unit-test
|
||||||
|
@ -241,6 +241,4 @@ CONSTANT: log10-factorial-1000 0x1.40f3593ed6f8ep11
|
||||||
|
|
||||||
{ t } [ 3 15 roots [ 15 ^ 3 .01 ~ ] all? ] unit-test
|
{ t } [ 3 15 roots [ 15 ^ 3 .01 ~ ] all? ] unit-test
|
||||||
|
|
||||||
{ t } [ 1 e^ e .0000000001 ~ ] unit-test
|
{ .5 } [ 0 sigmoid ] unit-test
|
||||||
{ 1 } [ 0 e^ ] unit-test
|
|
||||||
{ 1/2 } [ 0 sigmoid ] unit-test
|
|
||||||
|
|
|
@ -57,20 +57,20 @@ PRIVATE>
|
||||||
|
|
||||||
: polar> ( abs arg -- z ) cis * ; inline
|
: polar> ( abs arg -- z ) cis * ; inline
|
||||||
|
|
||||||
GENERIC: exp ( x -- y )
|
GENERIC: e^ ( x -- y )
|
||||||
|
|
||||||
M: float exp fexp ; inline
|
M: float e^ fexp ; inline
|
||||||
|
|
||||||
M: real exp >float exp ; inline
|
M: real e^ >float e^ ; inline
|
||||||
|
|
||||||
M: complex exp >rect [ exp ] dip polar> ; inline
|
M: complex e^ >rect [ e^ ] dip polar> ; inline
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: ^mag ( w abs arg -- magnitude )
|
: ^mag ( w abs arg -- magnitude )
|
||||||
[ >float-rect swap ]
|
[ >float-rect swap ]
|
||||||
[ >float swap >float fpow ]
|
[ >float swap >float fpow ]
|
||||||
[ rot * exp /f ]
|
[ rot * e^ /f ]
|
||||||
tri* ; inline
|
tri* ; inline
|
||||||
|
|
||||||
: ^theta ( w abs arg -- theta )
|
: ^theta ( w abs arg -- theta )
|
||||||
|
@ -223,8 +223,6 @@ M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
|
||||||
|
|
||||||
: 10^ ( x -- y ) 10 swap ^ ; inline
|
: 10^ ( x -- y ) 10 swap ^ ; inline
|
||||||
|
|
||||||
: e^ ( x -- y ) e swap ^ ; inline
|
|
||||||
|
|
||||||
GENERIC: log10 ( x -- y ) foldable
|
GENERIC: log10 ( x -- y ) foldable
|
||||||
|
|
||||||
M: real log10 >float flog10 ; inline
|
M: real log10 >float flog10 ; inline
|
||||||
|
@ -361,8 +359,8 @@ M: real atan >float atan ; inline
|
||||||
: lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
|
: lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
|
||||||
|
|
||||||
: roots ( x t -- seq )
|
: roots ( x t -- seq )
|
||||||
[ [ log ] [ recip ] bi* * exp ]
|
[ [ log ] [ recip ] bi* * e^ ]
|
||||||
[ recip 2pi * 0 swap complex boa exp ]
|
[ recip 2pi * 0 swap complex boa e^ ]
|
||||||
[ iota [ ^ * ] with with map ] tri ;
|
[ iota [ ^ * ] with with map ] tri ;
|
||||||
|
|
||||||
: sigmoid ( x -- y ) neg e^ 1 + recip ; inline
|
: sigmoid ( x -- y ) neg e^ 1 + recip ; inline
|
||||||
|
|
|
@ -69,7 +69,7 @@ HELP: fsinh
|
||||||
|
|
||||||
HELP: fexp
|
HELP: fexp
|
||||||
{ $values { "x" real } { "double" real } }
|
{ $values { "x" real } { "double" real } }
|
||||||
{ $description "Calls the exponential function (" { $snippet "y=e^x" } ") from the C standard library. User code should call " { $link exp } " instead." } ;
|
{ $description "Calls the exponential function (" { $snippet "y=e^x" } ") from the C standard library. User code should call " { $link e^ } " instead." } ;
|
||||||
|
|
||||||
HELP: flog
|
HELP: flog
|
||||||
{ $values { "x" real } { "double" real } }
|
{ $values { "x" real } { "double" real } }
|
||||||
|
|
|
@ -116,7 +116,7 @@ ERROR: too-many-samples seq n ;
|
||||||
(cos-random-float) (log-sqrt-random-float) * * + ;
|
(cos-random-float) (log-sqrt-random-float) * * + ;
|
||||||
|
|
||||||
: lognormal-random-float ( mean sigma -- n )
|
: lognormal-random-float ( mean sigma -- n )
|
||||||
normal-random-float exp ;
|
normal-random-float e^ ;
|
||||||
|
|
||||||
: exponential-random-float ( lambda -- n )
|
: exponential-random-float ( lambda -- n )
|
||||||
random-unit log neg swap / ;
|
random-unit log neg swap / ;
|
||||||
|
@ -149,7 +149,7 @@ ERROR: too-many-samples seq n ;
|
||||||
random-unit :> u2
|
random-unit :> u2
|
||||||
|
|
||||||
u1 1. u1 - / log ainv / :> v
|
u1 1. u1 - / log ainv / :> v
|
||||||
alpha v exp * :> x
|
alpha v e^ * :> x
|
||||||
u1 sq u2 * z!
|
u1 sq u2 * z!
|
||||||
bbb ccc v * + x - r!
|
bbb ccc v * + x - r!
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ ERROR: too-many-samples seq n ;
|
||||||
p 1.0 > [
|
p 1.0 > [
|
||||||
random-unit x alpha 1 - ^ >
|
random-unit x alpha 1 - ^ >
|
||||||
] [
|
] [
|
||||||
random-unit x neg exp >
|
random-unit x neg e^ >
|
||||||
] if
|
] if
|
||||||
] [
|
] [
|
||||||
random-unit b * p!
|
random-unit b * p!
|
||||||
|
@ -204,7 +204,7 @@ ERROR: too-many-samples seq n ;
|
||||||
0 :> c! 0 :> _f! ! initialize locals
|
0 :> c! 0 :> _f! ! initialize locals
|
||||||
[
|
[
|
||||||
random-unit {
|
random-unit {
|
||||||
[ 2. c - c * < ] [ 1. c - exp c * <= ]
|
[ 2. c - c * < ] [ 1. c - e^ c * <= ]
|
||||||
} 1|| not
|
} 1|| not
|
||||||
] [
|
] [
|
||||||
random-unit pi * cos :> z
|
random-unit pi * cos :> z
|
||||||
|
@ -252,7 +252,7 @@ ERROR: too-many-samples seq n ;
|
||||||
random-unit dup 1 swap - / log * + ;
|
random-unit dup 1 swap - / log * + ;
|
||||||
|
|
||||||
: power-random-float ( alpha -- n )
|
: power-random-float ( alpha -- n )
|
||||||
[ random-unit log exp 1 swap - ] dip recip ^ ;
|
[ random-unit log e^ 1 swap - ] dip recip ^ ;
|
||||||
|
|
||||||
{
|
{
|
||||||
{ [ os windows? ] [ "random.windows" require ] }
|
{ [ os windows? ] [ "random.windows" require ] }
|
||||||
|
|
|
@ -27,7 +27,7 @@ CONSTANT: gamma-p6
|
||||||
|
|
||||||
: gamma-lanczos6 ( x -- gamma[x] )
|
: gamma-lanczos6 ( x -- gamma[x] )
|
||||||
#! gamma(x) = gamma(x+1) / x
|
#! gamma(x) = gamma(x+1) / x
|
||||||
[ (gamma-lanczos6) exp ] keep / ;
|
[ (gamma-lanczos6) e^ ] keep / ;
|
||||||
|
|
||||||
: gammaln-lanczos6 ( x -- gammaln[x] )
|
: gammaln-lanczos6 ( x -- gammaln[x] )
|
||||||
#! log(gamma(x)) = log(gamma(x+1)) - log(x)
|
#! log(gamma(x)) = log(gamma(x+1)) - log(x)
|
||||||
|
@ -104,7 +104,7 @@ PRIVATE>
|
||||||
nip
|
nip
|
||||||
/
|
/
|
||||||
over /
|
over /
|
||||||
swap -1.0 * exp
|
swap -1.0 * e^
|
||||||
*
|
*
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ DERIVATIVE: abs
|
||||||
|
|
||||||
DERIVATIVE: sqrt [ sqrt 2 * / ]
|
DERIVATIVE: sqrt [ sqrt 2 * / ]
|
||||||
|
|
||||||
DERIVATIVE: exp [ exp * ]
|
DERIVATIVE: e^ [ e^ * ]
|
||||||
DERIVATIVE: log [ / ]
|
DERIVATIVE: log [ / ]
|
||||||
|
|
||||||
DERIVATIVE: sin [ cos * ]
|
DERIVATIVE: sin [ cos * ]
|
||||||
|
|
|
@ -45,7 +45,7 @@ MEMO: bernoulli ( p -- n )
|
||||||
even? [ "odd degrees of freedom" throw ] unless ;
|
even? [ "odd degrees of freedom" throw ] unless ;
|
||||||
|
|
||||||
: (chi2P) ( chi/2 df/2 -- p )
|
: (chi2P) ( chi/2 df/2 -- p )
|
||||||
[1,b) dupd n/v cum-product swap neg exp [ v*n sum ] keep + ;
|
[1,b) dupd n/v cum-product swap neg e^ [ v*n sum ] keep + ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue