All checks were successful
/ test (push) Successful in 18s
The Vector type is simply a generic type for built-in JavaScript Arrays. The parameter type is the type of all of the entries of the Array. The Vector type also supports inhomogeneous arrays by using the special type `Unknown` as the argument type, but note that when computing with inhomogeneous arrays, method dispatch must be performed separately for every entry in a calculation, making all operations considerably slower than on homogeneous Vector instances. Note also that arithmetic operations on nested Vectors, e.g. `Vector(Vector(NumberT))` are defined so as to interpret such entities as ordinary matrices, represented in row-major format (i.e., the component `Vector(NumberT)` items of such an entity are the _rows_ of the matrix. Reviewed-on: #28 Co-authored-by: Glen Whitney <glen@studioinfinity.org> Co-committed-by: Glen Whitney <glen@studioinfinity.org>
33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
import assert from 'assert'
|
|
import math from '#nanomath'
|
|
import {ReturnTyping} from '#core/Type.js'
|
|
|
|
describe('number arithmetic', () => {
|
|
it('supports basic operations', () => {
|
|
assert.strictEqual(math.abs(-Infinity), Infinity)
|
|
assert(isNaN(math.norm(NaN)))
|
|
assert.strictEqual(math.normsq(-2), 4)
|
|
assert.strictEqual(math.add(2, 2), 4)
|
|
assert.strictEqual(math.divide(6, 4), 1.5)
|
|
assert.strictEqual(math.cbrt(-8), -2)
|
|
assert.strictEqual(math.invert(-4), -0.25)
|
|
assert.strictEqual(math.multiply(4, 1.5), 6)
|
|
assert.strictEqual(math.negate(Infinity), -Infinity)
|
|
assert.strictEqual(math.subtract(4, 2), 2)
|
|
assert.strictEqual(math.quotient(7, 3), 2)
|
|
})
|
|
it('takes square root of numbers appropriately', () => {
|
|
assert(isNaN(math.sqrt(NaN)))
|
|
assert.strictEqual(math.sqrt(4), 2)
|
|
assert.deepStrictEqual(math.sqrt(-4), math.complex(0, 2))
|
|
math.config.returnTyping = ReturnTyping.conservative
|
|
assert(isNaN(math.sqrt(NaN)))
|
|
assert.strictEqual(math.sqrt(4), 2)
|
|
assert(isNaN(math.sqrt(-4)))
|
|
math.config.returnTyping = ReturnTyping.full
|
|
assert(isNaN(math.sqrt(NaN)))
|
|
assert.deepStrictEqual(math.sqrt(4), math.complex(2, 0))
|
|
assert.deepStrictEqual(math.sqrt(-4), math.complex(0, 2))
|
|
math.config.returnTyping = ReturnTyping.free
|
|
})
|
|
})
|