-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easier way to retrieve matrix columns or rows? #230
Comments
Yes, the syntax for getting/setting subsets is terribly verbose. JavaScript doesn't have a colon operator like python :(. Using the (one-based) expression parser it's great, there you just do If you know a less verbose solution please let me know, I'm very interested in that! A few thoughts:
|
I will close this issue. The (JavaScript) API is still verbose, but I don't know of a better solution. |
Hi guys, I think this issue should be reopened - if nothing else, it seems like this sort of functionality should be included in the API for matrices. The actual helper functions can be very short, something like getRow = (M, i) => math.flatten(M.subset(math.index(i, math.range(0, M._size[0])))).toArray();
getColumn = (M, i) => math.flatten(M.subset(math.index(math.range(0, M._size[0]),i))).toArray(); Ideally, though, these would be functions attached to any matrix, so you could call something like let row = M.getRow(2); This code is perhaps not the best solution, but isolating a single row/column from a matrix is such a common numerical procedure that it really should have some first-class support. If it's possible for something like this to be added in a PR, just point me towards the appropriate files! |
Agree @dzackgarza , ok let's add these two functions. I like the shorter names I think we should at least create these two as standalone functions The functions are zero-based, and need a transform so they work with one-based indices in the expression parser. The Matrix methods are just zero-based. Would be great if you could contribute these functions @dzackgarza ! Docs and unit tests is probably the most work here, not the actual implementation. You could look at how concat.js and concat.transform.js is implemented, it has a last index argument too which is transformed in the expression parser. |
@dzackgarza in
|
This is already implemented as |
Did you try it out? 😄 |
good point though, this issue should be closed long time ago (the functions |
But they do indeed work differently than documented 😅️ // get a row
const d = [[1, 2], [3, 4]]
math.row(d, 1) // returns [3, 4]
// get a column
const d = [[1, 2], [3, 4]]
math.column(d, 1) // returns [2, 4] But in reality they return EDIT: For example |
hm have to double check that. |
The
I have a vague idea but can you give an example so I understand what you mean? |
I found out that I remembered the inconsistencies much worse than they actually were. Ah, those fake memories 😅️. If we fix the documentation of The only thing that really bugs me now is that |
It is important that the functions are consistent with each other. It makes sense to me to have 1d in -> 2d out. I've opened an issue for that: #1753. |
Turns out these weren't fake memories in the end. The math.dot( math.matrix([[1],[2]]), math.matrix([[1],[2]]) )
// RangeError: Vector expected |
Hi Jos,
Thanks for providing such a useful math library for JS. I'm considering MathJS for a current project, and would like some guidance retrieving rows and columns from a Matrix object. Let's say I have a matrix:
And, now I want to retrieve the last column as a 1-D array. After trying various approaches, I came up with this:
Admittedly, I may be spoiled from Python's NumPy syntax, but this seems like a lot of work. Is this the easiest/best way to access members of a Matrix in MathJS?
Thanks again,
Scott
The text was updated successfully, but these errors were encountered: