-
-
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
subset of matrix should always return the same type (matrix) #2344
Comments
Have you tried the configuration option |
I just tried it but I see so, what we would like is: math.config({ predictable: false })
math.subset(math.matrix([1,2,3,4]), math.index(1)) // number 2
math.config({ predictable: true })
math.subset(math.matrix([1,2,3,4]), math.index(1)) // Matrix/Array containing one value 2 Anyone interested in making |
|
What "the user" wants depends on the use case and which user you're talking about 😄 . In a programmatic JavaScript environment it indeed makes sense to use two different, explicit methods The method So in the expression parser you do things like the following, where it is implicit whether you get back a value or a matrix:
You could argue to make an explicit API in the expression parser too, but this is how the mathematical world is used to work and it works like a charm. Similarly, a function like |
By checking other references I think the logic is more in line of the type of the input instead of the size of the index so that if every range in index comes from a scalar then it yields a scalar. A = [1, 2; 3, 4]
A[1, 2] # yields 2
A[1, [2]] # yields [2]
A[[1], 2] # yields [2]
A[1:1, 2] # yields [2]
A[1, 2:2] # yields [2]
A[2, 1:2] # yields [3, 4]
A[[2], 1:2] # yields [[3, 4]] I see also some differences on higher dimension arrays regarding if it comes from an array or comes from a range. Some of that shows promising capabilities but there is still utility in checking the size of the index. |
This issue came up in #2994, and I think there is a clear convention that could/should be adopted here that will eliminate any use of the "predictable" mechanism. That is to say, I would propose that indexing any dimension with a scalar eliminates that dimension, whereas indexing it with an array leaves that dimension in place, regardless of the length of the array. For example, if
This way, the type of the result is exactly predictable just from the types of the indices; it's compatible with boolean indexing as recently implemented; and I believe it covers the original poster's use-case. Thoughts? The change from current behavior is that for example all three of the last expressions currently return just 5. Conceptually, it would not be the count of entries that determines the type of the result, but just the types of the indices. There are some edge cases with empty vectors for indices:
but I think these things could be ironed out in some reasonable way, I don't think they detract from the basic proposal. |
Ow, that is a great idea! I love it. It definitely makes sense to me. |
It also makes sense to me. As a reference for the edge cases, in numpy it would be like this: A[[],[]] --> [] The same would apply for N dimensions: A[[],2,3] --> []
A[1,[],3] --> []
A[1,2,[]] --> []
A[1,[],[]] --> []
A[[],2,[]] --> []
A[[],[],3] --> []
A[[],[],[]] --> [] |
This is actually a duplicate of the much older #1484, but since the discussion here is more in-depth, I'm actually going to close the older one. |
Depending on the index the function subset returns a matrix or a scalar.
How can I set that the return type is always a matrix? Is there another function?
The text was updated successfully, but these errors were encountered: