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