Matrix subset according to type of input
Hi Jos,
This addresses #2344
It changes the behavior of index to accommodate for scalar indices and the size of the output depends on the input.
I'm still doing some tests and reviewing what documentation needs to change, but it all seems to work according to: https://github.com/josdejong/mathjs/issues/2344#issuecomment-1648501803
A = [1,2,3; 4,5,6]
A[2,3] --> 6
A[[1,2],[2,3]] --> [2,3; 5,6]
A[[2],[2,3]] --> [[5, 6]]
A[2,[2,3]] --> [5,6]
A[[1,2],[2]] --> [2; 5] // which is the same as [[2], [5]]
A[[1,2],2] --> [2,5]
A[[2], [2]] --> [[5]]
A[2, [2]] --> [5]
A[[2], 2] --> [5]
A[[],[]] --> []
A[1,[]] --> []
A[[],1] --> []
I think it's mostly ready for review.
Just wanted to review the following,
The elements of an index are called ranges like index(range1, range2) but the ranges are not always Range. Sometimes ranges are number, string, bigNumber, Array, Matrix, Array of Booleans, Matrix of Booleans or Range.
So I was thinking if in the documentation and maybe in the code to find a more general name for the elements of an index like index(indexElement1, indexElement2).
As a reference in numpy it's more like you slice with indices and there is no index class. So you slice A[index1, index2]
https://mathjs.org/docs/reference/functions/index.html
The code looks really neat, thanks David!
Some thoughts:
-
This is a breaking change, therefore I've changed this PR to merge into the
v15branch, and listed it at #3453. -
So I was thinking if in the documentation and maybe in the code to find a more general name for the elements of an index like
index(indexElement1, indexElement2).Maybe we can call the arguments
index(dim1, dim2, ...)? -
Does this PR have impact on the performance of
subset? -
I'm a bit concerned about how hugely breaking this change is. Existing code will probably still work but now return differently dimensioned matrices, which can give subtle and hard to debug errors. Can we somehow help people migrate, or detect "old" usage and warn the user? Or make it backward compatible with a config flag?
Thanks for your review Jos,
- Ok.
- Good idea! that aligns better with the rest of the code base.
- Makes a few of the tests slightly faster within the margin of error, but the main speed benefit might be the Matrix creation during the creation of Index, which is outside the scope of the subset benchmark. I will add some tests including the Index creation.
- I agree it's a big change. I'm not sure how to proceed. I'll give it some thought.
Regarding the benchmarks, I said something wrong. The current benchmarks already take into account the index creation.
The main benefit is the index creation, if tested on its own it's about twice as fast. But if tested on slicing as I said it's slightly faster.
So this task was already done.
Regarding the new behavior, I think it's possible to do the config flag, what would be a good name for it? Like config.legacy ?
Thanks for testing the performance David, most important is that there is not a sudden detoriation. Little performance improvements are of course always welcome :)
Regarding the new behavior, I think it's possible to do the config flag, what would be a good name for it? Like
config.legacy?
Great to hear a config flag is most likely possible. I think we should give it a name specific for this very subset/index refactoring. Something like config.legacyIndex or config.backwardCompatibleIndex? I think I like config.legacyIndex more of these two but no strong feelings.
Thanks Jos!
It might be too complicated to change the behavior of Index so I would like to try first to change subset with config.legacySubset flag. I will try this first and report back for your review.
Hi, this is still a work in progress but I think the overall implementation is working. Next steps are:
- add tests for
subsetwithconfig.legacySubset sylvester,rowandcolumndepend onsubset, ~~thus they should save currentconfig.legacySubset, change it to false and restore its original value at the end.~~- change documentation.
I think it's mostly done, the only thing left is changing the documentation.
Hi, I included the changes in documentation and it's ready for review.
Thanks Jos, for your kind words and thorough review.
Will address these topics and report back in the following days.
Hi, the topics are addressed in general, please let me know if the chapter of migration with this new behavior should be more in detail or with a different focus.
Published now in v15.0.0, thanks again David.