Mathjs: Adding .row() and .column() functions

Created on 19 Jun 2018  路  8Comments  路  Source: josdejong/mathjs

I'm working on some matrix maths that requires row and column extraction, but the docs don't mention any function for doing something like let row = matrix.row(3); or let col = matrix.column(1);.

Would it be possible to add those functions?

feature help wanted

Most helpful comment

Yes agree! It's just to give you and others an easy solution.

Anyone interested in implementing functions row and column ? If so just drop a message here and we can discuss the details.

All 8 comments

Sounds useful.

Note that this is already easy to when using the expression parser:

A=[1, 2, 3; 4, 5, 6; 7, 8, 9]

A[2,:]            # [[4, 5, 6]]
squeeze(A[1, :])  # [4, 5, 6]

A[:, 1]           # [[1], [4], [7]]
squeeze(A[:, 1])  # [1, 4, 7]

absolutely, but when you just need to work with matrixes in a JS lib that's going to live on a page, you try to weed out as much as possible, so not using the expression parser means rollup can throw it away and save a considerable amount of space.

(My code's already borderline offensively large for loading on a webpage, so anything I can avoid putting on top is worth avoiding putting on top)

Yes agree! It's just to give you and others an easy solution.

Anyone interested in implementing functions row and column ? If so just drop a message here and we can discuss the details.

Hi Jos.
If you still want help to implement these functions, please let me know.

@SzechuanSage yes definitely! Thanks for your offer, would be great if you can contribute.

I have looked through the code and I have some questions.

The DenseMatrix and SparseMatrix support >2 dimensions. Both of those classes also contain a function called swapRows. This function throws an error if the number of matrix dimensions is > 2.

If the new functions are to be developed, should they follow the same validation as swapRows()?

Row and column are very specific to 2D arrays. I expect anyone who uses 3D arrays (or higher dimensions) would not think in rows and columns. Perhaps row() and column() could be generalized to line(), which would get a single dimension from an n-dimensional array where the remaining n-1 dimensions have an index specified. If that were the case I would be inclined to still have row() and column() as wrappers for line(). Do you want row() and column() as @Pomax requested or do you want a more general line() with row() and column() as wrappers?

I notice in swapRows, if validateIndex() is called first, it would eliminate the need for the first if-test.

If the new functions are to be developed, should they follow the same validation as swapRows()?

Yes please add some validation checks inside the new methods so we get useful errors (and no vague JS errors like "cannot invoke blabla on undefined").

Do you want row() and column() as @Pomax requested or do you want a more general line() with row() and column() as wrappers?

I like you're idea of line but I'm not sure if there is actual need for it. I expect this to be much more work than just row() and column(), so I would say let's start simple with a 2D implementation and look into a more generic and powerful line() as soon as the need arises. But if you really prefer to go for the generic approach I'm ok with that, I don't have a very strong opinion in this regard.

Feel free to do improvements in swapRows if you see opportunities :)

I think we forgot to close this issue, the functions are already implemented a while ago.

Was this page helpful?
0 / 5 - 0 ratings