mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* chore: Rename `apply` to `mapSlices` This renaming conforms with the Julia name for the function formerly called `apply`, and allows it to be called from the expression parser. The previous name `apply` is kept as an alias for `mapSlices`, for backward compatibility. This commit implements an `alias` metadata property for function factories to facilitate the `apply` alias for `mapSlices`. As a separate bonus, this PR corrects several typos in function docs and removes now-passing doc tests from the list of "known failing" doc tests to get down to 45 known failures and 136 total issues in doc tests. (Most of the excess of 136 as compared to 45 are just due to roundoff error/slight inaccuracy of what the documentation claims the result will be and the actual result returned by mathjs. When the 45 are eliminated, a reasonable numeric tolerance can be decided on for doc testing and then the doc tests can be made binding rather than advisory. * refactor: changes per PR review --------- Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
import assert from 'assert'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
|
|
const sum = math.sum
|
|
const mapSlices = math.expression.transform.mapSlices
|
|
|
|
describe('mapSlices.transform', function () {
|
|
it('should apply a function to the rows of a matrix with one based indices', function () {
|
|
assert.deepStrictEqual(mapSlices([[1, 2], [3, 4]], 1, sum), [4, 6])
|
|
})
|
|
|
|
it('should apply a function to the columns of a matrix with one based indices', function () {
|
|
assert.deepStrictEqual(mapSlices([[1, 2], [3, 4]], 2, sum), [3, 7])
|
|
})
|
|
|
|
it('should throw an error if the dimension is below the range for one based indices', function () {
|
|
assert.throws(function () { mapSlices([[1, 2], [3, 4]], 0, sum) }, /Index out of range/)
|
|
})
|
|
|
|
it('should throw an error if the dimension is above the range for one based indices', function () {
|
|
assert.throws(function () { mapSlices([[1, 2], [3, 4]], 3, sum) }, /Index out of range/)
|
|
})
|
|
|
|
it('should be callable from the expression parser', function () {
|
|
assert.deepStrictEqual(
|
|
math.evaluate('mapSlices([[1, 2], [3, 4]], 2, sum)'),
|
|
math.evaluate('[3,7]')
|
|
)
|
|
})
|
|
})
|