| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | ! Copyright (C) 2006, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | USING: accessors kernel kernel.private math math.private | 
					
						
							| 
									
										
										
										
											2008-12-08 15:58:00 -05:00
										 |  |  | math.libm math.functions arrays math.functions.private sequences | 
					
						
							|  |  |  | parser ;
 | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | IN: math.complex.private | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-27 17:26:39 -05:00
										 |  |  | M: real real-part ;
 | 
					
						
							|  |  |  | M: real imaginary-part drop 0 ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | M: complex real-part real>> ;
 | 
					
						
							|  |  |  | M: complex imaginary-part imaginary>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  | M: complex absq >rect [ sq ] bi@ + ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 2>rect ( x y -- xr yr xi yi )
 | 
					
						
							| 
									
										
										
										
											2008-11-30 23:21:37 -05:00
										 |  |  |     [ [ real-part ] bi@ ] | 
					
						
							|  |  |  |     [ [ imaginary-part ] bi@ ] 2bi ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | M: complex hashcode* | 
					
						
							|  |  |  |     nip >rect [ hashcode ] bi@ bitxor ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: complex equal? | 
					
						
							|  |  |  |     over complex? [ | 
					
						
							|  |  |  |         2>rect = [ = ] [ 2drop f ] if
 | 
					
						
							|  |  |  |     ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | M: complex number= | 
					
						
							|  |  |  |     2>rect number= [ number= ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-30 23:21:37 -05:00
										 |  |  | : *re ( x y -- xr*yr xi*ri ) 2>rect [ * ] 2bi@ ; inline
 | 
					
						
							|  |  |  | : *im ( x y -- xi*yr xr*yi ) 2>rect [ * swap ] dip * ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-30 23:21:37 -05:00
										 |  |  | M: complex + 2>rect [ + ] 2bi@ (rect>) ;
 | 
					
						
							|  |  |  | M: complex - 2>rect [ - ] 2bi@ (rect>) ;
 | 
					
						
							|  |  |  | M: complex * [ *re - ] [ *im + ] 2bi (rect>) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : complex/ ( x y -- r i m )
 | 
					
						
							| 
									
										
										
										
											2008-11-30 23:21:37 -05:00
										 |  |  |     [ [ *re + ] [ *im - ] 2bi ] keep absq ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-30 23:21:37 -05:00
										 |  |  | M: complex / complex/ tuck [ / ] 2bi@ (rect>) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: complex abs absq >float fsqrt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-30 23:21:37 -05:00
										 |  |  | M: complex sqrt >polar [ fsqrt ] [ 2.0 / ] bi* polar> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | IN: syntax | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : C{ \ } [ first2 rect> ] parse-literal ; parsing |