1    	package acl2s.lib.parse.obj;
2    	
3    	import acl2s.lib.parse.ObjectNotSupported;
4    	import acl2s.lib.parse.IParseContext;
5    	import acl2s.lib.parse.ParseException;
6    	import acl2s.lib.parse.Parser;
7    	
8    	public class Complex extends Num {
9    		private static final long serialVersionUID = 2L;
10   	
11   		public final IntOrRat rpart;
12   		public final IntOrRat cpart;
13   		
14   		public Complex(IntOrRat r, IntOrRat c) {
15   			rpart = r; cpart = c;
16   		}
17   		
18   		public Num invert() {
19   			IntOrRat f = (IntOrRat) rpart.times(rpart).plus(cpart.times(cpart)).invert();
20   			return create((IntOrRat)rpart.times(f),
21   					      (IntOrRat)cpart.times(f).negate());
22   		}
23   	
24   		public Num negate() {
25   			return new Complex(rpart.negate(),cpart.negate());
26   		}
27   	
28   		public Num plus(Num n) {
29   			if (n instanceof Complex) {
30   				Complex c = (Complex) n;
31   				return create((IntOrRat)rpart.plus(c.rpart),(IntOrRat)cpart.plus(c.cpart));
32   			} else if (n instanceof Zero) {
33   				return this;
34   			} else {
35   				return create((IntOrRat)rpart.plus(n),cpart);
36   			}
37   		}
38   	
39   		public Num times(Num n) {
40   			if (n instanceof Zero) {
41   				return Zero.instance;
42   			}
43   			IntOrRat a = rpart;
44   			IntOrRat b = cpart;
45   			IntOrRat c, d;
46   			if (n instanceof Complex) {
47   				c = ((Complex)n).rpart;
48   				d = ((Complex)n).cpart;
49   			} else {
50   				c = (IntOrRat) n;
51   				d = Zero.instance;
52   			}
53   			
54   			return create((IntOrRat)(a.times(c).plus(b.times(d).negate())),
55   					      (IntOrRat)(b.times(c).plus(a.times(d))));
56   		}
57   	
58   		String computeString(IParseContext c) {
59   			return "#C(" + rpart.computeString(c) + " " + cpart.computeString(c) + ")";
60   		}
61   		
62   		public static Num parse(Parser p) throws ParseException {
63   			for(;;) { // not really a loop
64   				IntOrRat rpart,cpart;
65   				char c;
66   				if (p.pop() != '#') break;
67   				c = p.pop();
68   				if (c != 'c' && c != 'C') break;
Event example: Example of checking the function's return value.
Also see events: [check_return][example][example][example][example]
69   				if (p.pop() != '(') break;
70   				try {
71   					p.skipSpaceAndComments();
72   					rpart = IntOrRat.parse(p);
73   					p.skipSpaceAndComments();
74   					cpart = IntOrRat.parse(p);
75   					p.skipSpaceAndComments();
76   					if (p.pop() == ')') return create(rpart,cpart);
77   				} catch (ParseException pe) {
78   				}
79   				throw new ObjectNotSupported("Invalid format for complex number.");
80   			}
81   			throw new ParseException("Complex number expected, e.g. #C(1 2).");
82   		}
83   	
84   		public static Num create(IntOrRat rpart, IntOrRat cpart) {
85   			if (cpart instanceof Zero) {
86   				return rpart;
87   			} else {
88   				return new Complex(rpart,cpart);
89   			}
90   		}
91   		
92   		public boolean equals(Object o) {
93   			return (o instanceof Complex) &&
94   			this.rpart.equals(((Complex)o).rpart) &&
95   			this.cpart.equals(((Complex)o).cpart);
96   		}
97   	}