How to calculate with imaginary numbers in JavaScript?

Asked
Active3 hr before
Viewed126 times

7 Answers

numberscalculate
90%

Complex numbers in JavaScript,Assuming you really want complex numbers, and not just the imaginary component:, About , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

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...
load more v
88%

Returns a JSON representation of the complex number, with signature {mathjs: 'Complex', re: number, im: number}. Used when serializing a complex number, see Serialization.,Returns a complex number that is inverse of the current complex number.,Revive a complex number from a JSON object. Accepts An object {mathjs: 'Complex', re: number, im: number}, where the property mathjs is optional. Used when deserializing a complex number, see Serialization.,Get the vector representation of the current complex number. Returns an array of size 2.

math.complex(re: number): Complex
math.complex(re: number, im: number): Complex
math.complex(complex: Complex): Complex
math.complex({
   re: Number,
   im: Number
}): Complex
math.complex({
   r: number,
   phi: number
}): Complex
math.complex({
   abs: number,
   arg: number
}): Complex
math.complex(str: string): Complex
load more v
72%

Previous: Write a JavaScript function to calculate Lanczos approximation gamma. Next: Write a JavaScript program to subtract two complex numbers.,Write a JavaScript program to add two complex numbers. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, that satisfies the equation i2 = −1. In this expression, a is the real part and b is the imaginary part of the complex number.,Vue - JavaScript Framework,Angular - JavaScript Framework

HTML Code:

<!DOCTYPE html>
<html>

<head>
   <meta charset="utf-8">
   <title>JavaScript function to add two complex numbers</title>
</head>

<body>

</body>

</html>

JavaScript Code:

function Complex(real, imaginary) {
   this.real = 0;
   this.imaginary = 0;
   this.real = (typeof real === 'undefined') ? this.real : parseFloat(real);
   this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary);
}
Complex.transform = function(num) {
   var complex;
   complex = (num instanceof Complex) ? num : complex;
   complex = (typeof num === 'number') ? new Complex(num, 0) : num;
   return complex;
};

function display_complex(re, im) {
   if (im === '0') return '' + re;
   if (re === 0) return '' + im + 'i';
   if (im < 0) return '' + re + im + 'i';
   return '' + re + '+' + im + 'i';
}

function complex_num_add(first, second) {
   var num1, num2;
   num1 = Complex.transform(first);
   num2 = Complex.transform(second);
   var real = num1.real + num2.real;
   var imaginary = num1.imaginary + num2.imaginary;
   return display_complex(real, imaginary);
}
var a = new Complex(2, -7);
var b = new Complex(4, 3);
console.log(complex_num_add(a, b));

Sample Output:

6 - 4 i
load more v
65%

function Complex(real, imaginary) {
   this.real = 0;
   this.imaginary = 0;
   this.real = (typeof real === 'undefined') ? this.real : parseFloat(real);
   this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary);
}
Complex.transform = function(num) {
   var complex;
   complex = (num instanceof Complex) ? num : complex;
   complex = (typeof num === 'number') ? new Complex(num, 0) : num;
   return complex;
};

function display_complex(re, im) {
   if (im === '0') return '' + re;
   if (re === 0) return '' + im + 'i';
   if (im < 0) return '' + re + im + 'i';
   return '' + re + '+' + im + 'i';
}

function complex_num_add(first, second) {
   var num1, num2;
   num1 = Complex.transform(first);
   num2 = Complex.transform(second);
   var real = num1.real + num2.real;
   var imaginary = num1.imaginary + num2.imaginary;
   return display_complex(real, imaginary);
}
var a = new Complex(2, -7);
var b = new Complex(4, 3);
console.log(complex_num_add(a, b));
75%

Complex.js is a well tested JavaScript library to work with complex number arithmetic in JavaScript. It implements every elementary complex number manipulation function and the API is intentionally similar to Fraction.js. Furthermore, it's the basis of Polynomial.js and Math.js., A complex numbers library , A complex numbers library ,A complex euler number instance

let Complex = require('complex.js');

let c = new Complex("99.3+8i");
c.mul({
   re: 3,
   im: 9
}).div(4.9).sub(3, 2);
load more v
40%

0
22%

Complex numbers have a wide vareity of applications, like calculation of AC voltage, representation of rotations in 2D, Discrete Fourier Transform, complex geometry and even Riemann hypothesis involve complex numbers. That's why I have written complex.js to make work with complex numbers an easy task in JavaScript.,The library is well tested and contains a lot of functions, which try to overcome numerical imprecision and are implemented with performance in mind. To make the API more robust, every operation returns a new instance of the complex class. New JavaScript engines do a really good job on this and immutable functions are really fast nowadays.,Complex numbers sound complex, but in fact they are the most natural numbers - even if the name is a bit misleading. When extending the natural numbers to integers, to rational numbers, to real numbers and finally to complex numbers; it's the first field in which every operation is well defined.,Sorry, comments are closed for this article. Contact me if you want to leave a note.

Simple Complex Numbers Calculation

var Complex = require('complex.js');

var c = new Complex("99.3+8i");
c.mul({
   re: 3,
   im: 9
}).div(4.9).sub(3, 2);

Other "numbers-calculate" queries related to "How to calculate with imaginary numbers in JavaScript?"