| 
									
										
										
										
											2009-01-14 02:02:27 -05:00
										 |  |  | ! Copyright (C) 2006, 2009 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 | 
					
						
							| 
									
										
										
										
											2012-08-10 18:57:29 -04:00
										 |  |  | math.functions arrays math.functions.private sequences | 
					
						
							|  |  |  | sequences.private parser ;
 | 
					
						
							| 
									
										
										
										
											2015-06-30 11:51:07 -04:00
										 |  |  | IN: math.complex | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: real real-part ; inline
 | 
					
						
							|  |  |  | M: real imaginary-part drop 0 ; inline
 | 
					
						
							|  |  |  | M: complex real-part real>> ; inline
 | 
					
						
							|  |  |  | M: complex imaginary-part imaginary>> ; inline
 | 
					
						
							|  |  |  | M: complex absq >rect [ sq ] bi@ + ; inline
 | 
					
						
							|  |  |  | M: complex hashcode* nip >rect [ hashcode ] bi@ bitxor ; inline
 | 
					
						
							| 
									
										
										
										
											2009-02-19 01:56:30 -05:00
										 |  |  | : componentwise ( x y quot -- a b ) [ [ >rect ] bi@ ] dip bi-curry@ bi* ; inline
 | 
					
						
							|  |  |  | : complex= ( x y quot -- ? ) componentwise and ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: complex equal? over complex? [ [ = ] complex= ] [ 2drop f ] if ; inline
 | 
					
						
							|  |  |  | M: complex number= [ number= ] complex= ; inline
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | : complex-op ( x y quot -- z ) componentwise rect> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: complex + [ + ] complex-op ; inline
 | 
					
						
							|  |  |  | M: complex - [ - ] complex-op ; inline
 | 
					
						
							| 
									
										
										
										
											2009-02-19 01:56:30 -05:00
										 |  |  | : *re ( x y -- xr*yr xi*yi ) [ >rect ] bi@ [ * ] bi-curry@ bi* ; inline
 | 
					
						
							|  |  |  | : *im ( x y -- xi*yr xr*yi ) swap [ >rect ] bi@ swap [ * ] bi-curry@ bi* ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: complex * [ *re - ] [ *im + ] 2bi rect> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-02-19 01:56:30 -05:00
										 |  |  | : (complex/) ( x y -- r i m ) [ [ *re + ] [ *im - ] 2bi ] keep absq ; inline
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | : complex/ ( x y quot -- z ) [ (complex/) ] dip curry bi@ rect> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-17 23:32:21 -04:00
										 |  |  | M: complex / [ / ] complex/ ; inline
 | 
					
						
							|  |  |  | M: complex /f [ /f ] complex/ ; inline
 | 
					
						
							|  |  |  | M: complex /i [ /i ] complex/ ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-30 07:32:20 -04:00
										 |  |  | M: complex abs absq sqrt ; inline
 | 
					
						
							|  |  |  | M: complex sqrt >polar [ sqrt ] [ 2.0 / ] bi* polar> ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-30 11:57:20 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | IN: syntax | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 18:57:29 -04:00
										 |  |  | ERROR: malformed-complex obj ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parse-complex ( seq -- complex )
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     dup length 2 = [ first2-unsafe rect> ] [ malformed-complex ] if ;
 | 
					
						
							| 
									
										
										
										
											2012-08-10 18:57:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: C{ \ } [ parse-complex ] parse-literal ;
 | 
					
						
							| 
									
										
										
										
											2009-01-14 02:02:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | USE: prettyprint.custom | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: complex pprint* pprint-object ;
 | 
					
						
							|  |  |  | M: complex pprint-delims drop \ C{ \ } ;
 | 
					
						
							| 
									
										
										
										
											2012-08-10 18:57:29 -04:00
										 |  |  | M: complex >pprint-sequence >rect 2array ;
 |