-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Allow any vector in pickRandom function #1976
Allow any vector in pickRandom function #1976
Conversation
Thanks Konrad for picking this up 👍 We should think through what behavior we want. The current solution assumes an array, and picks a random element from the array. In mathjs you often have say a 2d matrix or array, and I think it makes more sense though to pick an element from the matrix instead of returning a row of the matrix. For example: // create a 2x2 array
const array = [
[1, 2],
[3, 4]
]
math.pickRandom(array)
// currently returns a "row": [1, 2] or [3, 4]
// I would expect it to return a value 1, 2, 3, or 4 I also noticed an unrelated bug that is already in the existing implementation: if (length === 0) {
return []
} else if (number >= length) {
return number > 1 ? possibles : possibles[0]
} This causes input like |
@josdejong I tried to implement the behavior described in #1974 which was to pick any element from the array. So that's what I did, but it sure makes sense to behave differently in case of matrices. |
Yes indeed. I think this should be easy to achieve, by doing |
I used |
It could be that we need to change some tests. |
@josdejong
There is a test described as |
The flattening looks good, thanks 👍 You're right about changing the behavior of |
Improvement is now available in |
Hmm, so should I reopen this? |
? |
You didn't merge that |
😱 I'm not sure what went wrong there, sorry! I really thought I had pressed the merge button. |
Will fix this coverage today |
I think the code coverage change is false alarm |
Will merge now - for real 😉 |
Thanks :) |
I'm glad you saw that I made a mistake here, thanks again! |
Available now in |
Hi, I accidentally commented on the wrong issue before. I just wanted to say that sometimes the behaviour of returning a vector instead of a value is what I prefer. For example, when I store a set of points in a list, each point represented as 2-dim vector, I expect pickRandom to return a random point, not the position of a random point along a random axis. Perhaps we could allow some property to distinguish n-dim arrays from lists of (n-1)-dim arrays, like NumPy or Julia already do it. |
Oh, I didn't realize that math.js already distinguishes between arrays and matrices. Wouldn't it make sense then that pickRandom applied to a matrix returns a random entry (not random vector) but applied to array returns random element (no matter what type)? |
Hm, I think that would be confusing too. So far, the behavior of all functions is consistent independent of whether you're using an Array or a Matrix. Using either an Array or a Matrix is just a different "jacket" around the same data. |
…introduce new option `elementWise`, always return `n` number of random picks (see also #1976).
@fweth @josdejong but it's working like that now after I added matrix flattening |
Yes I know, I hadn't expected this could impact how the function was used in practice. |
New option is published now in |
Hello, let me know if anything is wrong :)
Fixes #1974