2006-03-18 23:28:55 Complex code test 1
NOTE: This page was written in the pre-Objective 2.0 days, when I was working on my own set of changes to Objective-C. My project has since been abandoned in favor of solutions made available both by Objective-C 2.0 and by F-Script.

An example of a single-file objective-c source for creating two complex number classes and testing them out (the MyComplex class isn't actually tested, just built).
( objc )
`  1  Complex *z = [Complex complexWithReal:-3.0 andImaginary:2.0];  2  Complex *y = [Complex complexWithMangitude:4.0 andAngle:35.0];  3    4  print "\n";  5  print "z = %s\n", [[z wesselDescription] cString];  6  print "  = %s\n", [[z phasorDescription] cString];  7  print "\n";  8    9  print "y = %s\n", [[y wesselDescription] cString]; 10  print "  = %s\n", [[y phasorDescription] cString]; 11  print "\n"; 12   13  [z multiplyBy:y]; 14   15  print "z * y = %s\n", [[z wesselDescription] cString]; 16  print "      = %s\n", [[z phasorDescription] cString]; 17  print "\n"; 18   19  class Complex { 20      #include "math.h" 21       22      float real, imaginary, magnitude, angle; 23       24      +(Complex *)complexWithReal:(float)x andImaginary:(float)y { 25          Complex *z = [Complex new]; 26          [z setReal:x andImaginary:y]; 27          return z; 28      } 29       30      +(Complex *)complexWithMangitude:(float)r andAngle:(float)theta { 31          Complex *z = [Complex new]; 32          [z setMagnitude:r andAngle:theta]; 33          return z; 34      } 35       36      -(void)setMagnitude:(float)r andAngle:(float)theta { 37          magnitude = r; 38          angle = theta; 39          real = r * cos( theta ); 40          imaginary = r * sin( theta ); 41      } 42       43      -(void)setReal:(float)x andImaginary:(float)y { 44          real = x; 45          imaginary = y; 46          magnitude = sqrt( x * x + y * y ); 47          angle = 180 * acos( real / magnitude ); 48      } 49       50      -(void)multiplyBy:(Complex *)z { 51          magnitude = magnitude * [z magnitude]; 52          angle = angle + [z angle]; 53      } 54       55      -(NSString *)wesselDescription { 56          return [NSString stringWithFormat:@"%f + %fi", real, imaginary ]; 57      } 58       59      -(NSString *)phasorDescription { 60          return [NSString stringWithFormat:@"%f < %f", magnitude, angle ]; 61      } 62       63      -(float)magnitude { 64          return magnitude; 65      } 66       67      -(float)angle { 68          return angle; 69      } 70  } 71   72  class MyComplex : Complex { 73      Complex *a; 74      NSString *name; 75       76      -(void)setName:(NSString *)newName { 77          [newName retain]; 78          name = newName; 79          [newName release]; 80      } 81       82      -(NSString *)name { 83          return name; 84      } 85  }`

which produces this set of files in a subdirectory:
`  1   8 -rwxr-xr-x    .compile  2   0 -rw-r--r--    .objcIncFile  3  56 -rwxr-xr-x    complex  4   8 -rw-r--r--    Complex.h  5   8 -rw-r--r--    Complex.m  6   8 -rw-r--r--    MyComplex.h  7   8 -rw-r--r--    MyComplex.m  8   8 -rw-r--r--    main.m`

And the complex file is compiled and ready to run. The output, for the curious, is: (the "<" notation is my text attempt at phasor notation, indicated the angle of the vector with respect to the x-axis.)
`  1  z = -3.000000 + 2.000000i  2    = 3.605551 < 459.646210  3    4  y = -3.614769 + -1.712731i  5    = 4.000000 < 35.000000  6    7  z * y = -3.000000 + 2.000000i  8        = 14.422205 < 494.646210`