Skip to content

Commit

Permalink
Added tests for broadcasted subset in the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Jul 16, 2023
1 parent c175020 commit 15eb7db
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/utils/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ export function broadcastTo (array, toSize) {
Asize = arraySize(A)
}

// stretches the arryas on each dimension to make them the same size
// stretches the array on each dimension to make it the same size as index
for (let dim = 0; dim < N; dim++) {
if (Asize[dim] < broadcastedSize[dim]) {
A = stretch(A, broadcastedSize[dim], dim)
Expand All @@ -785,6 +785,25 @@ export function broadcastTo (array, toSize) {
return A
}

/**
* Broadcasts arrays and returns the broadcasted arrays in an array
* @param {...Array | any} arrays
* @returns
*/
export function broadcastArrays (...arrays) {
if (arrays.length === 0) {
throw new Error('Insuficient number of argumnets in function broadcastArrays')
}
if (arrays.length === 1) {
return arrays[0]
}
const sizes = arrays.map(function (array) { return arraySize(array) })
const broadcastedSize = broadcastSizes(...sizes)
const broadcastedArrays = []
arrays.forEach(function (array) { broadcastedArrays.push(broadcastTo(array, broadcastedSize)) })
return broadcastedArrays
}

/**
* stretches a matrix up to a certain size in a certain dimension
* @param {Array} arrayToStretch
Expand Down
11 changes: 11 additions & 0 deletions test/unit-tests/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,17 @@ describe('parse', function () {
assert.deepStrictEqual(parseAndEval('a[2:end-1, 2:end-1]', scope), math.matrix([[2, 0], [9, 9]]))
})

it('should get and set broadcasted submatrices in the parser', function () {
const scope = {}
parseAndEval('A = [1, 2, 3, 4]', scope)
assert.deepStrictEqual(parseAndEval('A[A>2]', scope), math.matrix([3, 4]))
parseAndEval('A[A>2] = 20', scope)
assert.deepStrictEqual(scope.A, math.matrix([1, 2, 20, 20]))
parseAndEval('A = [1, 2, 3, 4]', scope)
parseAndEval('A[A > 2] = [15]', scope)
assert.deepStrictEqual(scope.A, math.matrix([1, 2, 15, 15]))
})

it('should merge nested matrices', function () {
const scope = {}
parseAndEval('a=[1,2;3,4]', scope)
Expand Down
16 changes: 15 additions & 1 deletion test/unit-tests/utils/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
broadcastTo,
concat,
checkBroadcastingRules,
stretch
stretch,
broadcastArrays
} from '../../../src/utils/array.js'

describe('util.array', function () {
Expand Down Expand Up @@ -644,4 +645,17 @@ describe('util.array', function () {
assert.throws(function () { checkBroadcastingRules([2, 2], [1, 2]) })
})
})

describe('broadcastArrays', function () {
it('should broadcast many arrays', function () {
assert.deepStrictEqual(broadcastArrays([1, 2], [3, 4]), [[1, 2], [3, 4]])
assert.deepStrictEqual(broadcastArrays([1, 2], [[3], [4]]), [[[1, 2], [1, 2]], [[3, 3], [4, 4]]])
assert.deepStrictEqual(broadcastArrays([1, 2], [[3], [4]], [5, 6]), [[[1, 2], [1, 2]], [[3, 3], [4, 4]], [[5, 6], [5, 6]]])
})
it('should throw an arryor when the broadcasting rules don\'t apply', function () {
assert.throws(function () { broadcastArrays([1, 2], [1, 2, 3]) })
assert.throws(function () { broadcastArrays([1, 2], [1, 2, 3], [4, 5]) })
assert.throws(function () { broadcastArrays([[1, 2], [1, 2]], [[1, 2, 3]]) })
})
})
})

0 comments on commit 15eb7db

Please sign in to comment.