mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* Add `.js` extension to source file imports * Specify package `exports` in `package.json` Specify package type as `commonjs` (It's good to be specific) * Move all compiled scripts into `lib` directory Remove ./number.js (You can use the compiled ones in `./lib/*`) Tell node that the `esm` directory is type `module` and enable tree shaking. Remove unused files from packages `files` property * Allow importing of package.json * Make library ESM first * - Fix merge conflicts - Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs` - Refactor unit tests to be able to run with plain nodejs (no transpiling) - Fix browser examples * Fix browser and browserstack tests * Fix running unit tests on Node 10 (which has no support for modules) * Fix node.js examples (those are still commonjs) * Remove the need for `browserBundle.cjs` * Generate minified bundle only * [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963) Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.** - [Release notes](https://github.com/bitinn/node-fetch/releases) - [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md) - [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Cleanup console.log * Add integration tests to test the entry points (commonjs/esm, full/number only) * Create backward compatibility error messages in the files moved/removed since v8 * Describe breaking changes in HISTORY.md * Bump karma from 5.2.1 to 5.2.2 (#1965) Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2. - [Release notes](https://github.com/karma-runner/karma/releases) - [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md) - [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
257 lines
8.4 KiB
JavaScript
257 lines
8.4 KiB
JavaScript
// test add
|
|
import assert from 'assert'
|
|
|
|
import math from '../../../../src/defaultInstance.js'
|
|
const add = math.add
|
|
|
|
// TODO: make unit tests independent of math
|
|
describe('add', function () {
|
|
describe('Array', function () {
|
|
it('should convert strings and add them element wise', function () {
|
|
assert.deepStrictEqual(add('2', ['3', '4']), [5, 6])
|
|
assert.deepStrictEqual(add(['2', '3'], '4'), [6, 7])
|
|
})
|
|
|
|
it('should add arrays correctly', function () {
|
|
const a2 = [[1, 2], [3, 4]]
|
|
const a3 = [[5, 6], [7, 8]]
|
|
const a4 = add(a2, a3)
|
|
assert.deepStrictEqual(a4, [[6, 8], [10, 12]])
|
|
})
|
|
|
|
it('should add 3 dimension arrays correctly', function () {
|
|
const a2 = [[[1, 1], [2, 2]], [[3, 3], [4, 4]]]
|
|
const a3 = [[[5, 5], [6, 6]], [[7, 7], [8, 8]]]
|
|
const a4 = add(a2, a3)
|
|
assert.deepStrictEqual(a4, [[[6, 6], [8, 8]], [[10, 10], [12, 12]]])
|
|
})
|
|
|
|
it('should add a scalar and an array correctly', function () {
|
|
assert.deepStrictEqual(add(2, [3, 4]), [5, 6])
|
|
assert.deepStrictEqual(add([3, 4], 2), [5, 6])
|
|
})
|
|
|
|
it('should add array and dense matrix correctly', function () {
|
|
const a = [1, 2, 3]
|
|
const b = math.matrix([3, 2, 1])
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.matrix([4, 4, 4]))
|
|
})
|
|
|
|
it('should add array and sparse matrix correctly', function () {
|
|
const a = [[1, 2, 3], [4, 5, 6]]
|
|
const b = math.sparse([[6, 5, 4], [3, 2, 1]])
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.matrix([[7, 7, 7], [7, 7, 7]]))
|
|
})
|
|
})
|
|
|
|
describe('DenseMatrix', function () {
|
|
it('should handle strings and matrices element wise', function () {
|
|
assert.deepStrictEqual(add('2', math.matrix(['3', '4'])), math.matrix([5, 6]))
|
|
assert.deepStrictEqual(add(math.matrix(['2', '3']), '4'), math.matrix([6, 7]))
|
|
})
|
|
|
|
it('should add matrices correctly', function () {
|
|
const a2 = math.matrix([[1, 2], [3, 4]])
|
|
const a3 = math.matrix([[5, 6], [7, 8]])
|
|
const a4 = add(a2, a3)
|
|
assert.ok(a4 instanceof math.Matrix)
|
|
assert.deepStrictEqual(a4.size(), [2, 2])
|
|
assert.deepStrictEqual(a4.valueOf(), [[6, 8], [10, 12]])
|
|
})
|
|
|
|
it('should add 3 dimension natrices correctly', function () {
|
|
const a2 = math.matrix([[[1, 1], [2, 2]], [[3, 3], [4, 4]]])
|
|
const a3 = math.matrix([[[5, 5], [6, 6]], [[7, 7], [8, 8]]])
|
|
const a4 = add(a2, a3)
|
|
assert.deepStrictEqual(a4, math.matrix([[[6, 6], [8, 8]], [[10, 10], [12, 12]]]))
|
|
})
|
|
|
|
it('should add a scalar and a matrix correctly', function () {
|
|
assert.deepStrictEqual(add(2, math.matrix([3, 4])), math.matrix([5, 6]))
|
|
assert.deepStrictEqual(add(math.matrix([3, 4]), 2), math.matrix([5, 6]))
|
|
})
|
|
|
|
it('should add matrix and array correctly', function () {
|
|
const a = math.matrix([1, 2, 3])
|
|
const b = [3, 2, 1]
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.matrix([4, 4, 4]))
|
|
})
|
|
|
|
it('should add dense and sparse matrices correctly', function () {
|
|
const a = math.matrix([[1, 2, 3], [1, 0, 0]])
|
|
const b = math.sparse([[3, 2, 1], [0, 0, 1]])
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.matrix([[4, 4, 4], [1, 0, 1]]))
|
|
})
|
|
|
|
it('should add matrices with a datatype correctly', function () {
|
|
const a2 = math.matrix([[1, 2], [3, 4]], 'dense', 'number')
|
|
const a3 = math.matrix([[5, 6], [7, 8]], 'dense', 'number')
|
|
const a4 = add(a2, a3)
|
|
assert.ok(a4 instanceof math.Matrix)
|
|
assert.strictEqual(a4._datatype, 'number')
|
|
assert.deepStrictEqual(a4.size(), [2, 2])
|
|
assert.deepStrictEqual(a4.valueOf(), [[6, 8], [10, 12]])
|
|
})
|
|
|
|
it('should add matrices with a datatype correctly', function () {
|
|
const a2 = math.matrix([math.bignumber(3), math.bignumber(4)], 'dense', 'BigNumber')
|
|
const a3 = math.matrix([math.bignumber(5), math.bignumber(6)], 'dense', 'BigNumber')
|
|
const a4 = add(a2, a3)
|
|
assert.ok(a4 instanceof math.Matrix)
|
|
assert.strictEqual(a4._datatype, 'BigNumber')
|
|
assert.deepStrictEqual(a4.size(), [2])
|
|
assert.deepStrictEqual(a4.valueOf(), [math.bignumber(8), math.bignumber(10)])
|
|
})
|
|
})
|
|
|
|
describe('SparseMatrix', function () {
|
|
it('should add matrices correctly', function () {
|
|
const a2 = math.matrix([[1, 2], [3, 4]], 'sparse')
|
|
const a3 = math.matrix([[5, -2], [7, -4]], 'sparse')
|
|
const a4 = add(a2, a3)
|
|
assert.ok(a4 instanceof math.Matrix)
|
|
assert.deepStrictEqual(a4, math.sparse([[6, 0], [10, 0]]))
|
|
})
|
|
|
|
it('should add a scalar and a matrix correctly', function () {
|
|
assert.deepStrictEqual(add(2, math.matrix([[3, 4], [5, 6]], 'sparse')), math.matrix([[5, 6], [7, 8]], 'dense'))
|
|
assert.deepStrictEqual(add(math.matrix([[3, 4], [5, 6]], 'sparse'), 2), math.matrix([[5, 6], [7, 8]], 'dense'))
|
|
})
|
|
|
|
it('should add matrix and array correctly', function () {
|
|
const a = math.matrix([[1, 2, 3], [1, 0, 0]], 'sparse')
|
|
const b = [[3, 2, 1], [0, 0, 1]]
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.matrix([[4, 4, 4], [1, 0, 1]]))
|
|
})
|
|
|
|
it('should add sparse and dense matrices correctly', function () {
|
|
const a = math.sparse([[1, 2, 3], [1, 0, 0]])
|
|
const b = math.matrix([[3, 2, 1], [0, 0, 1]])
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.matrix([[4, 4, 4], [1, 0, 1]]))
|
|
})
|
|
|
|
it('should add sparse and sparse matrices correctly', function () {
|
|
const a = math.sparse([[1, 2, 3], [1, 0, 0]])
|
|
const b = math.sparse([[3, 2, 1], [0, 0, 1]])
|
|
const c = add(a, b)
|
|
|
|
assert.ok(c instanceof math.Matrix)
|
|
assert.deepStrictEqual(c, math.sparse([[4, 4, 4], [1, 0, 1]]))
|
|
})
|
|
|
|
it('should add two pattern matrices correctly', function () {
|
|
const a = new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 0],
|
|
ptr: [0, 2, 3, 4],
|
|
size: [3, 3]
|
|
})
|
|
|
|
const b = new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 1],
|
|
ptr: [0, 3, 3, 4],
|
|
size: [3, 3]
|
|
})
|
|
|
|
const c = add(a, b)
|
|
|
|
assert.deepStrictEqual(
|
|
c,
|
|
new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 2, 0, 1],
|
|
ptr: [0, 3, 4, 6],
|
|
size: [3, 3]
|
|
}))
|
|
})
|
|
|
|
it('should add pattern and value matrices correctly', function () {
|
|
const a = new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 0],
|
|
ptr: [0, 2, 3, 4],
|
|
size: [3, 3]
|
|
})
|
|
|
|
const b = new math.SparseMatrix({
|
|
values: [1, 2, 3, 4],
|
|
index: [0, 1, 2, 1],
|
|
ptr: [0, 3, 3, 4],
|
|
size: [3, 3]
|
|
})
|
|
|
|
const c = add(a, b)
|
|
|
|
assert.deepStrictEqual(
|
|
c,
|
|
new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 2, 0, 1],
|
|
ptr: [0, 3, 4, 6],
|
|
size: [3, 3]
|
|
}))
|
|
})
|
|
|
|
it('should add value and pattern matrices correctly', function () {
|
|
const a = new math.SparseMatrix({
|
|
values: [1, 2, 3, 4],
|
|
index: [0, 1, 2, 0],
|
|
ptr: [0, 2, 3, 4],
|
|
size: [3, 3]
|
|
})
|
|
|
|
const b = new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 1],
|
|
ptr: [0, 3, 3, 4],
|
|
size: [3, 3]
|
|
})
|
|
|
|
const c = add(a, b)
|
|
|
|
assert.deepStrictEqual(
|
|
c,
|
|
new math.SparseMatrix({
|
|
values: undefined,
|
|
index: [0, 1, 2, 2, 0, 1],
|
|
ptr: [0, 3, 4, 6],
|
|
size: [3, 3]
|
|
}))
|
|
})
|
|
})
|
|
|
|
describe('multiple arguments', function () {
|
|
it('should add more than two arguments', function () {
|
|
assert.deepStrictEqual(add(2, 3, 4), 9)
|
|
assert.deepStrictEqual(add(2, 3, [5, 6]), [10, 11])
|
|
|
|
assert.deepStrictEqual(add([1, 1], [2, 2], [3, 3]), [6, 6])
|
|
assert.deepStrictEqual(add([1, 1], [2, 2], 3), [6, 6])
|
|
assert.deepStrictEqual(add([1, 1], 2, 3), [6, 6])
|
|
|
|
assert.deepStrictEqual(add(math.matrix([1, 1]), math.matrix([2, 2]), math.matrix([3, 3])), math.matrix([6, 6]))
|
|
assert.deepStrictEqual(add(math.matrix([1, 1]), math.matrix([2, 2]), 3), math.matrix([6, 6]))
|
|
assert.deepStrictEqual(add(math.matrix([1, 1]), 2, 3), math.matrix([6, 6]))
|
|
})
|
|
})
|
|
})
|