1 /** 2 * Complex.js: 3 * This file defines a Complex class to represent complex numbers. 4 * Recall that a complex number is the sum of a real number and an 5 * imaginary number and that the imaginary number i is the 6 * square root of -1. 7 */ 8 9 /** 10 * The first step in defining a class is defining the constructor 11 * function of the class. This constructor should initialize any 12 * instance properties of the object. These are the essential 13 * "state variables" that make each instance of the class different. 14 * @constructor 15 * @param real {Number} The real part 16 * @param imaginary {Number} The complex part 17 */ 18 function Complex(real, imaginary) { 19 this.x = real; // The real part of the number 20 this.y = imaginary; // The imaginary part of the number 21 } 22 23 /* 24 * The second step in defining a class is defining its instance 25 * methods (and possibly other properties) in the prototype object 26 * of the constructor. Any properties defined in this object will 27 * be inherited by all instances of the class. Note that instance 28 * methods operate implicitly on the this keyword. For many methods, 29 * no other arguments are needed. 30 */ 31 32 // Return the magnitude of a complex number. This is defined 33 // as its distance from the origin (0,0) of the complex plane. 34 Complex.prototype.magnitude = function( ) { 35 return Math.sqrt(this.x*this.x + this.y*this.y); 36 }; 37 38 // Return a complex number that is the negative of this one. 39 Complex.prototype.negative = function( ) { 40 return new Complex(-this.x, -this.y); 41 }; 42 43 // Convert a Complex object to a string in a useful way. 44 // This is invoked when a Complex object is used as a string. 45 Complex.prototype.toString = function( ) { 46 return "{" + this.x + "," + this.y + "}"; 47 }; 48 49 // Return the real portion of a complex number. This function 50 // is invoked when a Complex object is treated as a primitive value. 51 Complex.prototype.valueOf = function( ) { return this.x; } 52 53 /* 54 * The third step in defining a class is to define class methods, 55 * constants, and any needed class properties as properties of the 56 * constructor function itself (instead of as properties of the 57 * prototype object of the constructor). Note that class methods 58 * do not use the this keyword: they operate only on their arguments. 59 */ 60 61 /** 62 *Add two complex numbers and return the result. 63 *@param a {Complex} A complex number 64 *@param b {Complex} A second complex number 65 *@returns {Complex} The sum of a and b 66 */ 67 Complex.add = function (a, b) { 68 return new Complex(a.x + b.x, a.y + b.y); 69 }; 70 71 // Subtract one complex number from another. 72 Complex.subtract = function (a, b) { 73 return new Complex(a.x - b.x, a.y - b.y); 74 }; 75 76 // Multiply two complex numbers and return the product. 77 Complex.multiply = function(a, b) { 78 return new Complex(a.x * b.x - a.y * b.y, 79 a.x * b.y + a.y * b.x); 80 }; 81 82 // Here are some useful predefined complex numbers. 83 // They are defined as class properties, where they can be used as 84 // "constants." (Note, though, that they are not actually read-only.) 85 Complex.zero = new Complex(0,0); 86 Complex.one = new Complex(1,0); 87 Complex.i = new Complex(0,1);