| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2004, 2007 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | USING: math kernel math.constants math.private | 
					
						
							| 
									
										
										
										
											2008-04-26 00:17:08 -04:00
										 |  |  | math.libm combinators math.order ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: math.functions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (rect>) ( x y -- z )
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  |     dup 0 = [ drop ] [ <complex> ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : rect> ( x y -- z )
 | 
					
						
							|  |  |  |     over real? over real? and [ | 
					
						
							|  |  |  |         (rect>) | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         "Complex number must have real components" throw
 | 
					
						
							|  |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: sqrt ( x -- y ) foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: real sqrt | 
					
						
							|  |  |  |     >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  | : each-bit ( n quot: ( ? -- ) -- )
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  |     over 0 = pick -1 = or [ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         2drop
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         2dup >r >r >r odd? r> call r> 2/ r> each-bit | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  |     ] if ; inline recursive
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ^n ( z w -- z^w )
 | 
					
						
							|  |  |  |     1 swap [ | 
					
						
							|  |  |  |         [ dupd * ] when >r sq r> | 
					
						
							|  |  |  |     ] each-bit nip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | : integer^ ( x y -- z )
 | 
					
						
							|  |  |  |     dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >rect ( z -- x y )
 | 
					
						
							|  |  |  |     [ real-part ] [ imaginary-part ] bi ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >float-rect ( z -- x y )
 | 
					
						
							|  |  |  |     >rect [ >float ] bi@ ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >polar ( z -- abs arg )
 | 
					
						
							|  |  |  |     >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ;
 | 
					
						
							|  |  |  |     inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : polar> ( abs arg -- z ) cis * ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ^mag ( w abs arg -- magnitude )
 | 
					
						
							|  |  |  |     >r >r >float-rect swap r> swap fpow r> rot * fexp /f ;
 | 
					
						
							|  |  |  |     inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ^theta ( w abs arg -- theta )
 | 
					
						
							|  |  |  |     >r >r >float-rect r> flog * swap r> * + ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ^complex ( x y -- z )
 | 
					
						
							|  |  |  |     swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : real^? ( x y -- ? )
 | 
					
						
							|  |  |  |     2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 0^ ( x -- z )
 | 
					
						
							|  |  |  |     dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ^ ( x y -- z )
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { [ over zero? ] [ nip 0^ ] } | 
					
						
							|  |  |  |         { [ dup integer? ] [ integer^ ] } | 
					
						
							|  |  |  |         { [ 2dup real^? ] [ fpow ] } | 
					
						
							|  |  |  |         [ ^complex ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (^mod) ( n x y -- z )
 | 
					
						
							|  |  |  |     1 swap [ | 
					
						
							|  |  |  |         [ dupd * pick mod ] when >r sq over mod r> | 
					
						
							|  |  |  |     ] each-bit 2nip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (gcd) ( b a x y -- a d )
 | 
					
						
							|  |  |  |     over zero? [ | 
					
						
							|  |  |  |         2nip
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         swap [ /mod >r over * swapd - r> ] keep (gcd) | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gcd ( x y -- a d )
 | 
					
						
							|  |  |  |     0 -rot 1 -rot (gcd) dup 0 < [ neg ] when ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : lcm ( a b -- c )
 | 
					
						
							|  |  |  |     [ * ] 2keep gcd nip /i ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : mod-inv ( x n -- y )
 | 
					
						
							|  |  |  |     tuck gcd 1 = [ | 
					
						
							|  |  |  |         dup 0 < [ + ] [ nip ] if
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2007-10-06 17:09:15 -04:00
										 |  |  |         "Non-trivial divisor found" throw
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ^mod ( x y n -- z )
 | 
					
						
							|  |  |  |     over 0 < [ | 
					
						
							|  |  |  |         [ >r neg r> ^mod ] keep mod-inv | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         -rot (^mod) | 
					
						
							|  |  |  |     ] if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: absq ( x -- y ) foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | M: real absq sq ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : ~abs ( x y epsilon -- ? )
 | 
					
						
							|  |  |  |     >r - abs r> < ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ~rel ( x y epsilon -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |     >r [ - abs ] 2keep [ abs ] bi@ + r> * < ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ~ ( x y epsilon -- ? )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ pick fp-nan? pick fp-nan? or ] [ 3drop f ] } | 
					
						
							|  |  |  |         { [ dup zero? ] [ drop number= ] } | 
					
						
							|  |  |  |         { [ dup 0 < ] [ ~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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | GENERIC: exp ( x -- y )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: real exp fexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: complex exp >rect swap fexp swap polar> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: log ( x -- y )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | M: complex log >polar swap flog swap rect> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : cos ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup complex? [ | 
					
						
							|  |  |  |         >float-rect 2dup
 | 
					
						
							|  |  |  |         fcosh swap fcos * -rot
 | 
					
						
							|  |  |  |         fsinh swap fsin neg * rect> | 
					
						
							|  |  |  |     ] [ fcos ] if ; foldable
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sec ( x -- y ) cos recip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : cosh ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup complex? [ | 
					
						
							|  |  |  |         >float-rect 2dup
 | 
					
						
							|  |  |  |         fcos swap fcosh * -rot
 | 
					
						
							|  |  |  |         fsin swap fsinh * rect> | 
					
						
							|  |  |  |     ] [ fcosh ] if ; foldable
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sech ( x -- y ) cosh recip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sin ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup complex? [ | 
					
						
							|  |  |  |         >float-rect 2dup
 | 
					
						
							|  |  |  |         fcosh swap fsin * -rot
 | 
					
						
							|  |  |  |         fsinh swap fcos * rect> | 
					
						
							|  |  |  |     ] [ fsin ] if ; foldable
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : cosec ( x -- y ) sin recip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sinh ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup complex? [ | 
					
						
							|  |  |  |         >float-rect 2dup
 | 
					
						
							|  |  |  |         fcos swap fsinh * -rot
 | 
					
						
							|  |  |  |         fsin swap fcosh * rect> | 
					
						
							|  |  |  |     ] [ fsinh ] if ; foldable
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : cosech ( x -- y ) sinh recip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  | : tan ( x -- y )
 | 
					
						
							|  |  |  |     dup complex? [ dup sin swap cos / ] [ ftan ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  | : tanh ( x -- y )
 | 
					
						
							|  |  |  |     dup complex? [ dup sinh swap cosh / ] [ ftanh ] if ; 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 )
 | 
					
						
							| 
									
										
										
										
											2008-06-07 08:22:09 -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 )
 | 
					
						
							| 
									
										
										
										
											2008-06-07 08:22:09 -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 )
 | 
					
						
							| 
									
										
										
										
											2008-06-07 08:22:09 -04:00
										 |  |  |     dup 1+ swap 1- neg / log 2 / ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : acoth ( x -- y ) recip atanh ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | : i* ( x -- y ) >rect neg swap rect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : -i* ( x -- y ) >rect swap neg rect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : asin ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : acos ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : atan ( x -- y )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 18:15:54 -04:00
										 |  |  |     dup complex? [ i* atanh i* ] [ fatan ] if ; 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
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : truncate ( x -- y ) dup 1 mod - ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : round ( x -- y ) dup sgn 2 / + truncate ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : floor ( x -- y )
 | 
					
						
							|  |  |  |     dup 1 mod dup zero?
 | 
					
						
							|  |  |  |     [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ceiling ( x -- y ) neg floor neg ; foldable
 |