GithubHelp home page GithubHelp logo

complexnumber's Introduction

ComplexNumber

A JavaScript complex number class that allows you to do most of the core mathematical operations with complex numbers, including addition, subtraction, multiplication, and getting the modulus of a complex number.


properties

/* The real part of the complex number
 * 
 * @type Number
 */
real: 0,

/* The imaginary part of the complex number
 * 
 * @type Number
 */
imaginary: 0,

A complex number is made up of two parts: a real and an imaginary parts. In the complex number 3 + 5i, for example, the real part is 3 and the imaginary part 5.

Creating a ComplexNumber is done by passing in two parameters: 1) real part and 2) imaginary part:

var complex = new ComplexNumber(3, 5);

complex.toString(); //returns "3 + 5i"

methods

The methods include mathematical operations (add(), sub(), mult(), mod()) and a toString() function that gives the string representation of the complex number (e.g. 3 + 5i)

add

Adds two complex numbers together by adding the real parts and the complex parts.

/**
 * The add operation which sums the real and complex parts separately
 * 
 * @param ==> 	If there is one argument, assume it's a ComplexNumber
 * 				If there are two arguments, assume the first is the real part and the second is the imaginary part
 * 
 * @return ComplexNumber
 */
add: function() {
    if(arguments.length == 1)
        return new ComplexNumber(this.real + arguments[0].real, this.imaginary + arguments[0].imaginary);
    else
        return new ComplexNumber(this.real + arguments[0], this.imaginary + arguments[1]);
},

Example usage:

var complex1 = new ComplexNumber(3, 5),
	complex2 = new ComplexNumber(1, 2),
	complexSum = complex1.add(complex2);

complexSum.toString(); //returns "4 + 7i"

sub

Subtracts a complex number from another by finding the difference between the real parts and the complex parts.

/**
 * The subtract operation which subtracts the real and complex parts from one another separately
 * 
 * @param ==>   If there is one argument, assume it's a ComplexNumber
 * 			    If there are two arguments, assume the first is the real part and the second is the imaginary part
 * 
 * @return ComplexNumber
 */
sub: function() {
    if(arguments.length == 1)
        return new ComplexNumber(this.real - arguments[0].real, this.imaginary - arguments[0].imaginary);
    else
        return new ComplexNumber(this.real - arguments[0], this.imaginary - arguments[1]);
},

Example usage:

var complex1 = new ComplexNumber(3, 5),
	complex2 = new ComplexNumber(1, 2),
	complexDiff = complex1.sub(complex2);

complexDiff.toString(); //returns "2 + 3i"

mult

Multiplies a complex number with another. Given complex numbers A and B, the result of their multiplication is: [(realA * realB) - (imaginaryA * imaginaryB)] + [(realA * imaginaryB) + (imaginaryA * realB)]*i.

/**
 * The multiplication operation which multiplies two complex numbers
 * 
 * @param ==> 	If there is one argument, assume it's a ComplexNumber
 * 				If there are two, assume the first is the real part and the second is the imaginary part
 * 
 * @return ComplexNumber
 */
mult: function() {
    var multiplier = arguments[0];

    if(arguments.length != 1)
        multiplier = new ComplexNumber(arguments[0], arguments[1]);

    return new ComplexNumber(this.real * multiplier.real - this.imaginary * multiplier.imaginary, 
							this.real * multiplier.imaginary + this.imaginary * multiplier.real);
},

Example usage:

var complex1 = new ComplexNumber(3, 5),
	complex2 = new ComplexNumber(1, 2),
	complexMult = complex1.mult(complex2);

complexMult.toString();
//returns "-7 + 11i" .... [(3 * 1) - (5 * 2)] + [(3 * 2) + (5 * 1)] * i  =  (3 - 10) + (6 + 5)i  =  -7 + 11i

mod

Returns the modulus of a complex number. The modulus is defined as the square root of the real part squared plus the imaginary part squared. This basically turns the complex number into a purely real number.

/**
 * The modulus of a complex number
 * 
 * @return number
 */
mod: function() {
    return Math.sqrt(this.real * this.real + this.imaginary * this.imaginary);
},

Example usage:

var complex = new ComplexNumber(3, 4);

complex.mod(); //returns 5 (3^2=9 and 4^2=16. 9 + 16 = 25. Square root of 25 is 5. 

toString

Returns the string representation of a complex number (e.g. "3 + 5i")

/**
 * The string representation of a complex number (i.e. 4 + 3i)
 * 
 * @return String
 */
toString: function() {
    return this.real + " + " + this.imaginary + "i";
}

Example usage:

var complex = new ComplexNumber(3, 4);

complex.toString(); //returns "3 + 4i" 

complexnumber's People

Contributors

janhartigan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.