How to use the complex function from mathjs

Find comprehensive JavaScript mathjs.complex code examples handpicked from public code repositorys.

13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
var eulerAngles = {theta: null, phi: null, lambda: null, phase: null};
var coeff = math.pow(math.det(inputMatrix), -0.5);
eulerAngles.phase = -1 * math.complex(coeff).toPolar().phi;
var su_mat = math.multiply(coeff, inputMatrix);
eulerAngles.theta = 2 * math.atan2(math.abs(su_mat[1][0]), math.abs(su_mat[0][0]));
var phiplambda = 2 * math.complex(su_mat[1][1]).toPolar().phi;
var phimlamda = 2 * math.complex(su_mat[1][0]).toPolar().phi;
eulerAngles.phi = (phiplambda + phimlamda) / 2.0;
eulerAngles.lambda = (phiplambda - phimlamda) / 2.0;
return eulerAngles;
fork icon43
star icon207
watch icon14

+ 3159 other calls in file

31
32
33
34
35
36
37
38
39
mul: math.multiply,
div: math.divide,
mod,
reciprocal: z => math.divide(1, z),
component_mul: (z, alpha) => math.complex(alpha*z.re, alpha*z.im),
component_mul_prelog: (z, alpha) => math.complex(math.exp(alpha)*z.re, math.exp(alpha)*z.im),
real: math.re,
imag: math.im,
step: z => (z.re >= 0) ? 1 : 0,
fork icon8
star icon29
watch icon3

+ 34 other calls in file

176
177
178
179
180
181
182
183
184
185
        l,
        cmul(2, cpow(l, 5)),
        cmul(15, cpow(l, 9)),
        cmul(150, cpow(l, 13))
    );
    return cmul(clog(q), math.complex(0, -1/Math.PI));
}

function jacobi_reduce(z, k) {
    const tau = invert_tau(k);
fork icon8
star icon29
watch icon3

+ 19 other calls in file

68
69
70
71
72
73
74
75
76
77
78
79
80


    if (!isNaN(val)) {re = val; im = 0;}
    if (val[0] === 'number') {re = val[1]; im = val[2];}
    if (val[0] === 'constant') {re = constants[val[1]]; im = 0;}


    return math.complex(re, im);
}


function destructure(val) {
    if (val.re === undefined) {return ['number', val, 0];}
fork icon8
star icon28
watch icon0

+ 110 other calls in file

47
48
49
50
51
52
53
54
55
56

let _bes = q.map(val => BESSEL.besselj(val*h,0));
let bes = maths.chain(q).dotMultiply(h).dotMultiply(_bes).done();
let oros = maths.chain(jx).dotDivide(q).dotDivide(maths.dotPow(bes, 2)).done();

const j2 = maths.complex('i');
var dz = [];
for (var i = 0; i < this.f.length; i++) {
  var p2 = maths.sqrt(maths.chain(q)
                .dotMultiply(q)
fork icon2
star icon3
watch icon0

146
147
148
149
150
151
152
153
154
155
JavaScript没有内置复数的功能,但有一些库支持复数算法。例如, [mathjs](https://www.npmjs.com/package/mathjs):

```js
var math = require('mathjs')

var a = math.complex(3, -1)
//=> { re: 3, im: -1 }

var b = math.sqrt(-1)
//=> { re: 0, im: -1 }
fork icon0
star icon39
watch icon12

+ 11 other calls in file

105
106
107
108
109
110
111
112
113
114
// See https://samuelj.li/blog/2021-05-05-erf-expansion
function erf_large(z) {
    if (z.re < 0) {return erf_large(z.neg()).neg();}
    if (z.im < 0) {return math.conj(erf_large(math.conj(z)));}
    const TWO_SQRTPI = 1.1283791671;
    const W = math.complex(0.70710678118, 0.70710678118);
    const W_BAR = math.complex(0.70710678118, -0.70710678118);
    const rs = cmul(z, W_BAR);
    const r = rs.re;
    const s = rs.im;
fork icon8
star icon0
watch icon0

+ 175 other calls in file

0
1
2
3
4
5
6
7
8
const math = require('mathjs')
const PRECISION = 3;

class Ket {
    constructor(_amplitudes) {
        let vector = _amplitudes.map(a => new math.complex(a));

        if (!this.isUnitary(vector))
            throw `Ket must be unitary [${vector}]`;
fork icon2
star icon4
watch icon3

+ 3 other calls in file

31
32
33
34
35
36
37
38
39
}

const partOne = (input) => {
  let ship = {
    position: math.complex(0, 0),
    dir: math.complex(1, 0)
  }

  let [maxX, maxY, minX, minY] = [0, 0, 0, 0]
fork icon0
star icon1
watch icon1

+ 3 other calls in file

207
208
209
210
211
212
213
214
215

Math.js supports complex numbers.

```js
var a = math.complex(2, 3);     // 2 + 3i
var b = math.complex('4 - 2i'); // 4 - 2i
math.add(a, b);                 // 6 + i
math.sqrt(-4);                  // 2i
```
fork icon0
star icon0
watch icon0

+ 47 other calls in file

129
130
131
132
133
134
135
136
137
138
139
140
141
}






function drawJulia(canvas, real, imaginary) {
    var c = math.complex(real, imaginary);
    var ctx = canvas.getContext("2d");
    var width_px = canvas.width;
    let julia = julia_set(width_px, c, 150);
    for (j = 0; j < width_px; j++) {
fork icon0
star icon0
watch icon0

75
76
77
78
79
80
81
82
83
84
let square = [];
let row = [];
for (j = 0; j < width_px; j++) {
    row = [];
    for (i = 0; i < width_px; i++) {
        let point = math.complex(x+i*k_step, y+j*k_step);
        row[i] = point;
    }
    square[j] = row;
}
fork icon0
star icon0
watch icon0

431
432
433
434
435
436
437
438
439
440
  //console.log(r1, i1, r2, i2)
//}

let complex1 = math.complex(r1, i1);

let complex2 = math.complex(r2, i2);

let complexProduct = math.multiply(complex1, complex2);

//rp1.doublePtr(r, c)[0] = complexProduct.re.toFixed(4); // this was commented! (matFromArray)
fork icon0
star icon0
watch icon0

+ 11 other calls in file