Typed matrix dimensions? #3071
Unanswered
AlexMouton
asked this question in
Q&A
Replies: 3 comments
-
That's an interesting question. There is no provisioning in const math = require('mathjs')
// define a new type, which checks whether a value is a Matrix with size 3x2
math.typed.types.push({name:'Matrix3x2', test: function (x) {
if (!x || !x.isMatrix) {
return false
}
const size = x.size()
return size.length === 2 && size[0] === 3 && size[1] === 2
}})
// create a typed function using this new data type
const myFunction = math.typed('myFunction', {
'Matrix3x2, Matrix3x2': function (a, b) {
return math.add(a, b)
}
})
const a1 = math.matrix([[1,2], [3,4], [5,6]])
const b1 = math.matrix([[7,8], [9,10], [11,12]])
const result1 = myFunction(a1, b1)
console.log(result1.toString()) // [[8, 10], [12, 14], [16, 18]]
const a2 = math.matrix([[1,2], [3,4], [5,6]])
const b2 = math.eye(3, 3)
const result2 = myFunction(a2, b2)
// TypeError: Unexpected type of argument in function myFunction
// (expected: Matrix3x2, actual: Matrix, index: 1) Would something like that work for you? |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think so. Ill give it a try. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Since this was a question and was answered, moving to Q&A. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi Jos, et al.
Is there a way for me to enforce the dimensionality of a matrix in a typed function?
I am interested in specifying vectors/positions specifically, and would rather not have to check those repeatedly.
I see theres a hot new version, so excuse me if this is now dated.
Thanks
Alex Mouton
Beta Was this translation helpful? Give feedback.
All reactions