Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate with imaginary numbers in JavaScript?

Recently, I am trying to calculate using some equations that involve the imaginary number i in them. However, unlike e or π, there isn't any methods or native functions that will return i. A quick search in Google didn't get me any answer. Any ideas on how to achieve it?

function imaginary(){     return {         rational: this,         imaginary: "2i"  //magic code that does this     }; }; Number.prototype.imaginary = imaginary; 
like image 338
Derek 朕會功夫 Avatar asked Mar 14 '13 00:03

Derek 朕會功夫


People also ask

How do you calculate imaginary numbers?

An imaginary number is a real number multiplied by the imaginary unit i, which is defined by its property i2 = −1. The square of an imaginary number bi is −b2. For example, 5i is an imaginary number, and its square is −25.

How do you represent complex numbers in JavaScript?

js supports the creation, manipulation, and calculations with complex numbers. Support of complex numbers is powered by the library complex. js. In mathematics, a complex number is an expression of the form a + bi , where a and b are real numbers and i represents the imaginary number defined as i^2 = -1 .

How are imaginary numbers used in coding?

Imaginary numbers will be used to represent two dimensional variables where both dimensions are physically significant. A vector can do that (hence the "rotation part" of the answer), but "i" can be used in formula two represents 2 dimensions (like the static amplitude and phase information of a phasor).

Can you do calculations in JavaScript?

This is especially true when we are learning to program JavaScript (or any other language for that matter) — so much of what we do relies on processing numerical data, calculating new values, and so on, that you won't be surprised to learn that JavaScript has a full-featured set of math functions available.


2 Answers

The math.js library supports complex numbers, matrices, and more. The library is compatible with JavaScript's built-in Math library, so quite easy to use.

http://mathjs.org

You can just do things like:

math.i;                         // i math.sqrt(-4)                   // 2i var a = math.complex('2 + 3i'); // 2 + 3i var b = math.complex(4, 5);     // 4 + 5i math.add(a, b);                 // 6 + 8i math.multiply(a, b);            // -7 + 22i math.eval('e^(pi*i) + 1');      // ~0 // etc... 

Edit: note that math.js comes with an expression parser, which makes it more convenient to work with complex values and mathematical expressions:

math.eval('(2 + 3i) * (4 + 5i)'); // -7 + 22i 
like image 131
Jos de Jong Avatar answered Sep 25 '22 12:09

Jos de Jong


Assuming you really want complex numbers, and not just the imaginary component:

I would model a complex number just as you would model a 2D point, i.e. a pair of numbers.

Just as a point has x and y components, so a complex number has real and imaginary components. Both components can just be modeled with ordinary numeric types (int, float, etc.)

However, you will need to define new functionality for all of the mathematical operations.

Addition and subtraction of complex numbers works the same way as addition and subtraction of points - add the separate components to each other, don't mix them. For example:

(3+2i)+(5+4i) = (8+6i)

Multiplication works just like you learned in algebra when multiplying (a+b)*(c+d) = (ac+ad+bc+bd).

Except now you also have to remember that i*i = -1. So:

(a+bi)*(c+di) = (ac+adi+bci+bdii) = (ac-bd) + (ad+bc)i

For division and exponentiation, see http://en.wikipedia.org/wiki/Complex_number

like image 40
mbeckish Avatar answered Sep 25 '22 12:09

mbeckish