Skip to content

Commit

Permalink
Fix #2412: let function diff return an empty matrix when the input co…
Browse files Browse the repository at this point in the history
…ntains only one element (#2422)

* Fix #2412: let function diff return an empty matrix when the input has only one element

* Undo changes in History in this fixme

* Add TypeScript definitions for src/utils/is.js (#2432)

This is a first step toward full publication of these functions,
that were already being exported by mathjs but had not yet
had the associated actions (documentation/available in 
parser/typed, etc.) Also, makes most of them into TypeScript
type guards, and adds Matrix as a constructor type. Resolved #2431.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>

* test: add two-dimensional test cases for diff of length 1

Co-authored-by: Chris Chudzicki <christopher.chudzicki@gmail.com>
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
  • Loading branch information
3 people committed May 12, 2022
1 parent 47c7166 commit cfcecde
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/function/matrix/diff.js
Expand Up @@ -110,9 +110,6 @@ export const createDiff = /* #__PURE__ */ factory(name, dependencies, ({ typed,
function _diff (arr) {
const result = []
const size = arr.length
if (size < 2) {
return arr
}
for (let i = 1; i < size; i++) {
result.push(_ElementDiff(arr[i - 1], arr[i]))
}
Expand Down
16 changes: 11 additions & 5 deletions test/unit-tests/function/matrix/diff.test.js
Expand Up @@ -21,18 +21,24 @@ const largeTestArrayDimension2 = [[[[1, 1, 1], [1, 1, 1]], [[-1, 1, 3], [3, 1, -
const largeTestArrayDimension3 = [[[[1, 1], [1, 1], [1, 1]], [[-1, -1], [1, 1], [-1, -1]], [[-3, -1], [-3, -1], [-3, -1]]], [[[4, 333], [12, -12], [111, -222]], [[1, 1], [2, 2], [1, 1]], [[-11, -11], [0, -31], [1, 1]]], [[[63, -61], [32, 27], [-36, -28]], [[-10, -1], [2, 1], [5, -4]], [[-30, -1], [-3, -1], [-99, -1]]]]

describe('diff', function () {
it('should return original array/matrix for less than 2 elements, with and without specified dimension', function () {
it('should return an empty array/matrix for less than 2 elements, with and without specified dimension', function () {
// With Dim = 0 specified
assert.deepStrictEqual(diff([], 0), [])
assert.deepStrictEqual(diff(matrix([]), 0), matrix([]))
assert.deepStrictEqual(diff([2], 0), [2])
assert.deepStrictEqual(diff(matrix([2]), 0), matrix([2]))
assert.deepStrictEqual(diff([2], 0), [])
assert.deepStrictEqual(diff(matrix([2]), 0), matrix([]))

// Without Dim = 0 specified
assert.deepStrictEqual(diff([]), [])
assert.deepStrictEqual(diff(matrix([])), matrix([]))
assert.deepStrictEqual(diff([2]), [2])
assert.deepStrictEqual(diff(matrix([2])), matrix([2]))
assert.deepStrictEqual(diff([2]), [])
assert.deepStrictEqual(diff(matrix([2])), matrix([]))

// Two-dimensional:
assert.deepStrictEqual(diff([[1, 3, 6, 10, 15]]), [])
assert.deepStrictEqual(diff([[1, 3, 6, 10, 15]], 1), [[2, 3, 4, 5]])
assert.deepStrictEqual(diff([[1], [3], [6], [10], [15]]), [[2], [3], [4], [5]])
assert.deepStrictEqual(diff([[1], [3], [6], [10], [15]], 1), [[], [], [], [], []])
})

it('should return difference between elements of a 1-dimensional array, with and without specified dimension', function () {
Expand Down

0 comments on commit cfcecde

Please sign in to comment.