mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
* add placeholder for apply function * added apply function * add test coverage for apply utility * stylsitic name change in apply source code * stylistic format change to test * improved description of function parameters * moved the apply function to the public matrix functions * update location and reference of unit test * fixed function reference paths in apply for location in function * changed path to apply in apply.test * make apply a typed function, update unit test * added typing error test to coverage * remove apply.test.js from the utils test function * added transform function for apply * add unit test for apply.transform.js
37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
const assert = require('assert')
|
|
const math = require('../../../src/main')
|
|
|
|
const sum = math.sum
|
|
|
|
describe('apply', function () {
|
|
it('should apply a function to the rows of a matrix', function () {
|
|
assert.deepStrictEqual(math.apply([[1, 2], [3, 4]], 0, sum), [4, 6])
|
|
})
|
|
|
|
it('should apply a function to the columns of a matrix', function () {
|
|
assert.deepStrictEqual(math.apply([[1, 2], [3, 4]], 1, sum), [3, 7])
|
|
})
|
|
|
|
const inputMatrix = [ // this is a 4x3x2 matrix, full test coverage
|
|
[ [1, 2], [3, 4], [5, 6] ],
|
|
[ [7, 8], [9, 10], [11, 12] ],
|
|
[ [13, 14], [15, 16], [17, 18] ],
|
|
[ [19, 20], [21, 22], [23, 24] ]
|
|
]
|
|
it('should apply to the rows of a tensor', function () {
|
|
assert.deepStrictEqual(math.apply(inputMatrix, 2, sum), [[3, 7, 11], [15, 19, 23], [27, 31, 35], [39, 43, 47]])
|
|
})
|
|
|
|
it('should throw an error if the dimension is out of range', function () {
|
|
assert.throws(function () { math.apply([[1, 2], [3, 4]], 3, sum) }, /Index out of range/)
|
|
})
|
|
|
|
it('should throw an error if the dimension is not an integer', function () {
|
|
assert.throws(function () { math.apply([[1, 2], [3, 4]], [1, 2], sum) }, /Unexpected type of argument in function apply/)
|
|
})
|
|
|
|
it('should throw an error if the matrix, is not a matrix or array', function () {
|
|
assert.throws(function () { math.apply('[[1, 2], [3, 4]]', 0, sum) }, /Unexpected type of argument in function apply/)
|
|
})
|
|
})
|