mathjs/examples/complex_numbers.js
greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

68 lines
2.0 KiB
JavaScript

// complex numbers
// load math.js (using node.js)
const math = require('../index')
// create a complex number with a numeric real and complex part
console.log('create and manipulate complex numbers')
const a = math.complex(2, 3)
print(a) // 2 + 3i
// read the real and complex parts of the complex number
print(a.re) // 2
print(a.im) // 3
// clone a complex value
const clone = a.clone()
print(clone) // 2 + 3i
// adjust the complex value
a.re = 5
print(a) // 5 + 3i
// create a complex number by providing a string with real and complex parts
const b = math.complex('3-7i')
print(b) // 3 - 7i
console.log()
// perform operations with complex numbers
console.log('perform operations')
print(math.add(a, b)) // 8 - 4i
print(math.multiply(a, b)) // 36 - 26i
print(math.sin(a)) // -9.6541254768548 + 2.8416922956064i
// some operations will return a complex number depending on the arguments
print(math.sqrt(4)) // 2
print(math.sqrt(-4)) // 2i
console.log()
// create a complex number from polar coordinates
console.log('create complex numbers with polar coordinates')
const c = math.complex({ r: math.sqrt(2), phi: math.pi / 4 })
print(c) // 1 + i
// get polar coordinates of a complex number
const d = math.complex(3, 4)
console.log(d.abs(), d.arg()) // radius = 5, phi = 0.9272952180016122
console.log()
// comparision operations
// note that there is no mathematical ordering defined for complex numbers
// we can only check equality. To sort a list with complex numbers,
// the natural sorting can be used
console.log('\ncomparision and sorting operations')
console.log('equal', math.equal(a, b)) // returns false
const values = [a, b, c]
console.log('values:', math.format(values, 14)) // [5 + 3i, 3 - 7i, 1 + i]
math.sort(values, 'natural')
console.log('sorted:', math.format(values, 14)) // [1 + i, 3 - 7i, 5 + 3i]
/**
* Helper function to output a value in the console. Value will be formatted.
* @param {*} value
*/
function print (value) {
const precision = 14
console.log(math.format(value, precision))
}