-
-
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
Matrix creation and conversion methods #2155
Conversation
The build fails because of this error:
It is caused by the fact that I use a generator function but the Babel's generator polyfill is not properly loaded, see this thread on SO. @josdejong I think I'll need your help with this one. |
[[13, 14], [15, 16]] | ||
]) | ||
expected = [] | ||
m.forEach((value, index) => { expected.push({ value, index }) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can rewrite this to:
const expected = m.map((value, index) => ({ value, index }))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
O no, nevermind, that returns a DenseMatrix
again
wow good busy @m93a 😎 I personally prefer a single PR for a single new feature instead of one huge PR adressing lots of things: it makes it much harder to review (and easy to overlook something), keeps new functionality stuck in the pipeline for a longer time (putting pressure on you to everything asap). Is it ok for you to just handle the first three checkboxes in this PR, and address the other checkboxes in separate PR's? Some feedbacks on your implementation:
|
Hm, the |
No problem!
I don't have many experiences with Babel (and those that I do have are mostly negative 😁️), however if there's something specific I can help with, let me know. |
af99ea3
to
f80e4fb
Compare
Thanks for the updates, and for starting separate topics for the separate ideas you have.
😂 yeah same here... I wish it would "just work" I was thinking about Do you have any real world examples of where you need to pull a matrix apart in rows/columns, and afterwards glue it together again? I want to be careful to implement features that have a clear real-world use, and make sure new features really align with real-world usage. In this case, we could go for the features |
Basis transformations are the first thing that comes to my mind – if you work in a basis uᵢ, therefore [u₁]ᵤ = (1,0,...), [u₂]ᵤ = (0,1,...), ..., and want to change to a basis vᵢ, the transformation matrix for vectors is made of columns [vᵢ]ᵤ. Conversely, if you have a transformation matrix and don't know the basis vectors, you can get them as the columns of the matrix. There are several other use cases, like extracting eigenvectors from the matrix that If you had three vectors (column matrices) const v1, v2, v3
let M = transpose(matrix([ flatten(v1.toArray()), flatten(v2.toArray()), flatten(v3.toArray()) ])) ... or something like that. The code is needlessly complicated, although the actual task is almost trivial. And it's definitely not self-documenting, I'd have a hard time understanding what it does even in my own codebase. Compare that to: let M = matrixFromColumns(v1, v2, v3) I couldn't think of an example where one would want to take the columns, do something with them and then recombine them into a matrix again – maybe if you have a transformation matrix and want to change the basis you're transforming into. However, as I pointed out, there are legit use cases for doing those things separately – either taking columns of a matrix, or constructing a matrix from columns. So far, the examples have been about columns. I think |
Thanks, you convinced me 😄 . I agree that having the tooling to pull matrices apart and put them together in various ways are basic needs which can make life easy. I think if we provide these methods for columns, we should indeed also offer them for for rows else the API is incomplete (even if the need may be less). Thinking about my earlier comment:
If we implement So I think this PR is ready to be merged functionally wise, only we still have to fix the Babel issues 😕 . |
Having
I tried adding |
We should not need |
I'm not sure I understand... We already have runtime dependencies, why should Initially, I was also skeptical about having a Babel as a dependency after what happened with Therefore I don't think there's anything wrong with having Babel Runtime as a dependency, apart from the obvious fact that it's one dependency more than we had before. EDIT: Those dependencies are “runtime dependencies” in the sense that when you compile your project for the browser, they will be bundled together with it. It's not like you have to do anything else than include |
You may be right, it may be not a problem at all. To me I guess adding Ok then will merge your PR now 👍 |
@m93a when testing I noticed one more thing that we may want to adjust: function |
Aaah, I hate “array in—array out, Matrix in—Matrix out” 😂️ But yes, you're probably right, it would be more consistent with the rest of the library if the return type were array for |
👍 lets keep it consistent for now then. We can open a separate topic to discuss the “array in -> array out, Matrix in -> Matrix out”, I would like to better understand what you dislike about it, and if you have any alternative ideas in this regard. |
Published in |
* made dense and sparse matrices iterable, fixed josdejong#1184 * added matrixFromFunction, fixes josdejong#2153 * added tests for matrixFromFunction * added matrixFromRows * added matrixFromColumns * added rows() and columns() for dense matrix * improved sparse documentation a tiny bit * fix linting issues * added matrixFromRow/Column to seealso of row and column * removed unnecessary duplication from matrixFromRows/Columns * added babel runtime Co-authored-by: Jos de Jong <[email protected]>
This PR brings several quality-of-life improvements to matrices.
DenseMatrix
andSparseMatrix
are now iterable, which fixes Make Matrix iterable (support for...of statement) #1184matrixFromFunction(size, fn)
, which fixes Feature Request: Array/Matrix From Function #2153matrixFromRows
andmatrixFromColumns
mat.rows()
andmat.columns()
now return arrays of vectors, see Add methods for iteration of matrix rows and columns #2177matrix
,DenseMatrix
andSparseMatrix
accept an iterable as datasparseFromDOK
,sparseFromLIL
,sparseFromCOO
,sparseFromCSR
,sparseFromCSC
(wiki), which fixes (not) Constructing sparse matrices on your own? #1166SparseMatrix
now has methodstoDOK
,toLIL
,toCOO
,toCSR
,toCSC
documentation ofSparseMatrix
is now less sparse, which fixes (not) Improve the docs for sparse matrices #1769The strikethrough points will be addressed in a followup PR.