sqrtm - throw an error for matrices with dimension greater than two (#1977)

This commit is contained in:
Konrad Linkowski 2020-09-30 12:37:32 +02:00 committed by GitHub
parent f3c4a90218
commit becab4099b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -87,6 +87,10 @@ export const createSqrtm = /* #__PURE__ */ factory(name, dependencies, ({ typed,
'(size: ' + format(size) + ')')
}
}
default:
// Multi dimensional array
throw new RangeError('Matrix must be at most two dimensional ' +
'(size: ' + format(size) + ')')
}
}
})

View File

@ -52,6 +52,13 @@ describe('sqrtm', function () {
assert.throws(function () { math.sqrtm([[1, 2, 3], [4, 5, 6]]) }, /Matrix must be square/)
})
it('should throw an error in case of matrices with dimension greater than two', function () {
const errorRegex = /Matrix must be at most two dimensional/
assert.throws(function () { math.sqrtm(math.zeros(1, 1, 1)) }, errorRegex)
assert.throws(function () { math.sqrtm(math.zeros(2, 2, 2)) }, errorRegex)
assert.throws(function () { math.sqrtm(math.zeros(3, 3, 3, 3)) }, errorRegex)
})
it('should LaTeX sqrtm', function () {
const expression = math.parse('sqrtm([[33, 24], [48, 57]])')
assert.strictEqual(expression.toTex(), '{\\begin{bmatrix}33&24\\\\48&57\\\\\\end{bmatrix}}^{\\frac{1}{2}}')