math.size and Matrix.size are inconsistent
See original GitHub issueThe .size()
method on matrices always returns number[]
. The math.size(x)
method, which should be more robust, can take not only Matrix
instances, but also array matrices and scalars. For arrays it returns number[]
, but for matrices it returns an instance of Matrix
.
let a = [ [1], [2] ]
let m = math.matrix(a)
math.size(a) // [1,2]
m.size() // [1,2]
math.size(m) // Matrix([1,2])
This is unexpected and useless. Size is usefull when one can read the dimensions rightaway, with matrix, you have to convert it to array first.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Incompatible size matrix multiplication - Math Stack Exchange
There exists the Kronecker product of matrices, which allows for the multiplication of two matrices of any size.
Read more >The dimensions of the matrix are inconsistent when using the ...
The dimensions of the matrix are inconsistent. ... The following error states that there is a dimension mismatch problem while performing the arithmetics....
Read more >Consistent and inconsistent equations - Wikipedia
In contrast, a linear or non linear equation system is called inconsistent if there is no set of values for the unknowns that...
Read more >Inconsistent matrix parentheses size #150 - stipub/stixfonts
The size of pmatrix parentheses is very inconsistent both using XeLaTeX and pdfLaTeX. The size of the parentheses in the right matrix looks ......
Read more >Inconsistency thresholds for incomplete pairwise comparison ...
The 10% rule of acceptable inconsistency is extended to incomplete matrices. •. Random index is found to depend on matrix size and the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve been giving this a bit more thought. I think having
size
always return anArray
is the more practical, pragmatic approach, and that this outweighs having consistentinput -> output
.So let’s go for it and change
size
to always return an Array, schedule this forv7
ok?Ah, your’re right. Yes I had seen it coming too, but using it for real helps me weight the impact (for me and all current users with certain expectations). I’m sorry for the back-and-forth in this regard, but it still doesn’t feel “right” so I’m reluctant to make this change. I have the feeling that we’re going to mix the current API with a different type of API.
It’s an interesting idea to have
zeros
andones
become more “smart” and have them take the size of the input in case the input is a SparseMatrix or DenseMatrix. It doesn’t really appeal to me though, because of a couple of reasons:size
do an extra interpretation step in certain cases make the function less transparent to me. I like that it is simplydimensions in -> Matrix out
.zeros(denseMatrix)
could behave differently thanzeros(denseMatrix.toArray())
.I think this touches the core of our different way of looking at the API. The API of mathjs is designed around the simple rule “data type in -> same data type out”. Such a rule gives predictability and makes it easy to use the library. The intention also is to use functions and no class methods (functional API). If you’re doing calculations with BigNumbers, the results are in BigNumbers and should not suddenly switch to numbers. When calculating with plain Array, you get plain Array out, and not a DenseMatrix. And when working with DenseMatrices, you get DenseMatrices out and not suddenly switch to low level Arrays.
It may be good to take a step back and first get the bigger picture clear. You have some interesting ideas and a different view on things than me which is always good. Here an open question to you: if you could start from scratch, what API would you come up with for mathjs? Would you like to replace the “data type in -> same data type out” API with something totally different, and if so what?