New Factor release

Slava Pestov — 2004-06-08 06:13:53

Hi all,

I uploaded a new Factor release, 0.59 at www.jedit.org/factor/.

Several years ago I wrote a simple symbolic algebra program with complex
numbers and complex-valued elementary functions. I adopted the complex
number code to Factor with minimal effort.

There is a new document describing this, www.jedit.org/factor/math.txt.

Apart from complex number support, this release has the usual bug fixes
and cleanups.

Here is pretty much the entire set of words for working with complex
numbers. Yes, I'm showing them here because I think they're pretty neat
:-) (The definitions of the real-valued fsin, fcos, etc are not shown
here, and can be found in Factor.jar, factor/math/real-math.factor).

Since one of the goals of Factor is to be useful as a desktop
calculator, lease let me know if you find the math features useful, and
what I could do to improve them.

: conjugate ( z -- z* )
>rect neg rect> ;

: arg ( z -- arg )
#! Compute the complex argument.
>rect swap fatan2 ; inline

: abs ( z -- abs )
#! Compute the complex absolute value.
>rect mag2 ; inline

: >polar ( z -- abs arg )
dup abs swap arg ; inline

: cis ( theta -- cis )
dup fcos swap fsin rect> ;

: polar> ( abs arg -- z )
cis * ; inline

: exp >rect swap fexp swap polar> ;
: log >polar swap flog swap rect> ;

: sqrt ( z -- sqrt )
>polar dup pi = [
drop fsqrt 0 swap rect>
] [
swap fsqrt swap 2 / polar>
] ifte ;

: ^mag ( w abs arg -- magnitude )
[ [ >rect swap ] dip swap fpow ] dip rot * fexp / ;

: ^theta ( w abs arg -- theta )
[ [ >rect ] dip flog * swap ] dip * + ;

: ^ ( z w -- z^w )
swap >polar 3dup ^theta [ ^mag ] dip polar> ;

: cos ( z -- cos )
>rect 2dup
fcosh swap fcos * -rot
fsinh swap fsin neg * rect> ;

: sec cos recip ;

: cosh ( z -- cosh )
>rect 2dup
fcos swap fcosh * -rot
fsin swap fsinh * rect> ;

: sech cosh recip ;

: sin ( z -- sin )
>rect 2dup
fcosh swap fsin * -rot
fsinh swap fcos * rect> ;

: cosec sin recip ;

: sinh ( z -- sinh )
>rect 2dup
fcos swap fsinh * -rot
fsin swap fcosh * rect> ;

: cosech sinh recip ;

: tan dup sin swap cos / ;
: tanh dup sinh swap cosh / ;
: cot dup cos swap sin / ;
: coth dup cosh swap sinh / ;

: acosh dup sq pred sqrt + log ;
: asech recip acosh ;
: asinh dup sq succ sqrt + log ;
: acosech recip asinh ;
: atanh dup succ swap pred neg / log 2 / ;
: acoth recip atanh ;
: <=1 ( x -- ? ) dup complex? [ drop f ] [ abs 1 <= ] ifte ;
: asin dup <=1 [ fasin ] [ i * asinh -i * ] ifte ;
: acos dup <=1 [ facos ] [ asin pi/2 swap - ] ifte ;
: atan dup <=1 [ fatan ] [ i * atanh i * ] ifte ;
: asec recip acos ;
: acosec recip asin ;
: acot recip atan ;