28

Recently, I am trying to calculate using some equations with complex numbers. However, unlike e or π, there isn't any methods or native functions that will return i. A [quick search][1] 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;
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247

3 Answers3

25

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
const a = math.complex(`2 + 3i`); // 2 + 3i
const 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...

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
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Jos de Jong
  • 6,602
  • 3
  • 38
  • 58
  • 2
    mathjs doesn't support converting complex numbers to or from polar coordinates, unlike complex number libraries in most other languages. Perhaps look at https://npmjs.org/package/Complex if you need that. – MkV Dec 01 '13 at 09:13
  • 8
    mathjs now supports creating and converting complex numbers from polar coordinates, [see docs](https://github.com/josdejong/mathjs/blob/master/docs/datatypes/complex_numbers.md). – Jos de Jong May 04 '14 at 13:51
  • When I try `math.eval('e^(pi*i) + 1')` I get 1.2246467991473532e-16. This was in the node REPL on a phone using the `termux` app, though. – trysis Sep 22 '17 at 01:56
  • 3
    @trysis these are numerical round off errors, see docs: http://mathjs.org/docs/datatypes/numbers.html#roundoff-errors – Jos de Jong Sep 22 '17 at 07:57
  • hi, is there js libraries to compute things like that ? you input f(z)=(1−z)/(1+z), it gives you u=(1−x2−y2)/((1+x)+y2) and v=−2y/((1+x)2+y2) https://math.stackexchange.com/questions/2909044/separate-fz-into-real-and-imaginary-parts – Ch'nycos Sep 26 '22 at 22:10
13

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

mbeckish
  • 10,485
  • 5
  • 30
  • 55
-1

You don't really need a special JS library :

function convert2Rectangle(polarReal, polarAngle){
    var real = Math.cos(polarAngle*Math.PI/180)*polarReal
    var imag = Math.sin(polarAngle*Math.PI/180)*polarReal
    var RectNumber = {'real':real,'imag':imag}
    return RectNumber
}

function convert2Polar(rectReal, rectImag){
    var real = Math.sqrt(rectReal*rectReal + rectImag*rectImag) 
    var angle = Math.atan( real/rectReal )* (180/Math.PI)
    var PolarNumber = {'real':real,'angle':angle}
    return PolarNumber
}

var myPolar = convert2Polar(3,30)
console.log(myPolar.real)
console.log(myPolar.angle)

var myRect = convert2Rectangle(30.1496,84.317)
console.log(myRect.real)
console.log(myRect.imag)
RFPic
  • 1
  • 1