Timeline for Quickest way to test whether array is constant
Current License: CC BY-SA 4.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 22, 2020 at 14:24 | vote | accept | Gert | ||
Sep 29, 2020 at 21:51 | history | became hot network question | |||
Sep 29, 2020 at 18:00 | history | tweeted | twitter.com/StackMma/status/1311002819776966657 | ||
Sep 29, 2020 at 16:45 | answer | added | Henrik Schumacher | timeline score: 6 | |
Sep 29, 2020 at 15:41 | comment | added | Carl Woll | @HenrikSchumacher You should add the MinMax approach as an answer, it is faster than the other answers. | |
Sep 29, 2020 at 15:13 | comment | added | Michael E2 |
@HenrikSchumacher I suspect the OP neglected the point because of the use of DeleteDuplicates and the question about "equal elements." I raised it, so that OP might clarify which criterion is desired. (One might want to use Chop or Threshold on max - min in the case of floats depending on how array is calculated.)
|
|
Sep 29, 2020 at 15:08 | answer | added | kglr | timeline score: 9 | |
Sep 29, 2020 at 15:07 | comment | added | Michael E2 |
What is the maximum depth of your arrays? Length[DeleteDuplicates[array]] assumes it's depth 1 (a "flat" or non-nested list). MinMax[array] will work with arrays of any depth (vectors, matrices, tensors...).
|
|
Sep 29, 2020 at 14:46 | comment | added | Henrik Schumacher | @MichaelE2 Good point. Admittedly, I had only the (machine) integer case in mind. | |
Sep 29, 2020 at 14:29 | comment | added | Michael E2 |
@Henrik has a good idea but "duplicate" and "equal" mean different things in Mathematica, especially for floating-point numbers and expressions like array = {1 + Sqrt[3], Sqrt[4 + 2 Sqrt[3]]} . An alternative to check duplicates is 0 == Subtract @@ MinMax[array] .
|
|
Sep 29, 2020 at 14:28 | answer | added | Sjoerd Smit | timeline score: 9 | |
Sep 29, 2020 at 14:00 | comment | added | Bob Hanlon |
Your Length[ DeleteDuplicates[ array ] ] == 1 does not test whether array is constant but rather whether its rows are equal. This is equivalent to Equal @@ array . To test whether array is constant you could use Equal @@ Flatten[array]
|
|
Sep 29, 2020 at 13:54 | comment | added | Henrik Schumacher |
Equal@@MinMax[array] might be a bit faster...
|
|
Sep 29, 2020 at 13:47 | history | asked | Gert | CC BY-SA 4.0 |