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 */ 15 function Complex(real, imaginary) { 16 this.x = real; // The real part of the number 17 this.y = imaginary; // The imaginary part of the number 18 } 19 20 /* 21 * The second step in defining a class is defining its instance 22 * methods (and possibly other properties) in the prototype object 23 * of the constructor. Any properties defined in this object will 24 * be inherited by all instances of the class. Note that instance 25 * methods operate implicitly on the this keyword. For many methods, 26 * no other arguments are needed. 27 */ 28 29 // Return the magnitude of a complex number. This is defined 30 // as its distance from the origin (0,0) of the complex plane. 31 Complex.prototype.magnitude = function( ) { 32 return Math.sqrt(this.x*this.x + this.y*this.y); 33 }; 34 35 // Return a complex number that is the negative of this one. 36 Complex.prototype.negative = function( ) { 37 return new Complex(-this.x, -this.y); 38 }; 39 40 // Convert a Complex object to a string in a useful way. 41 // This is invoked when a Complex object is used as a string. 42 Complex.prototype.toString = function( ) { 43 return "{" + this.x + "," + this.y + "}"; 44 }; 45 46 // Return the real portion of a complex number. This function 47 // is invoked when a Complex object is treated as a primitive value. 48 Complex.prototype.valueOf = function( ) { return this.x; } 49 50 /* 51 * The third step in defining a class is to define class methods, 52 * constants, and any needed class properties as properties of the 53 * constructor function itself (instead of as properties of the 54 * prototype object of the constructor). Note that class methods 55 * do not use the this keyword: they operate only on their arguments. 56 */ 57 58 //Add two complex numbers and return the result. 59 Complex.add = function (a, b) { 60 return new Complex(a.x + b.x, a.y + b.y); 61 }; 62 63 // Subtract one complex number from another. 64 Complex.subtract = function (a, b) { 65 return new Complex(a.x - b.x, a.y - b.y); 66 }; 67 68 // Multiply two complex numbers and return the product. 69 Complex.multiply = function(a, b) { 70 return new Complex(a.x * b.x - a.y * b.y, 71 a.x * b.y + a.y * b.x); 72 }; 73 74 // Here are some useful predefined complex numbers. 75 // They are defined as class properties, where they can be used as 76 // "constants." (Note, though, that they are not actually read-only.) 77 Complex.zero = new Complex(0,0); 78 Complex.one = new Complex(1,0); 79 Complex.i = new Complex(0,1);