Remove N^2 uniqueness checks in interpolators#2031
Merged
jodastephen merged 9 commits intomasterfrom Jul 29, 2019
Merged
Conversation
jodastephen
suggested changes
Jul 22, 2019
| */ | ||
| public static double[] noDuplicates(double[] argument, String name) { | ||
| notNull(argument, name); | ||
| if (argument.length > 0 && DoubleStream.of(argument).distinct().count() != argument.length) { |
Contributor
There was a problem hiding this comment.
I wouldn't have thought DoubleStream.of(argument).distinct().count() was that fast?
modules/collect/src/main/java/com/opengamma/strata/collect/DoubleArrayMath.java
Outdated
Show resolved
Hide resolved
|
|
||
| /** | ||
| * Return the array lengths if they are the same, otherwise throws an {@code IllegalArgumentException}. | ||
| * |
Contributor
There was a problem hiding this comment.
I would have just changed this to a // comment
| double[] yValuesSrt = new double[nDataPts]; | ||
|
|
||
| xValuesSrt = Arrays.copyOf(xValues, nDataPts); | ||
| double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts); |
Contributor
There was a problem hiding this comment.
I suspect xValues.clone() might be quicker in cases like this.
Contributor
Author
There was a problem hiding this comment.
Apparently .clone() may add a cast (since it's implicitly overriden from the Object::clone() method)
Arrays.copyOf() delegates to System.arrayCopy which is native, and doesn't involve a cast.
I'd have to benchmark to be sure.
Although https://stackoverflow.com/questions/12157300/clone-or-arrays-copyof says they are equivalent post JIT
| for (int j = i + 1; j < nDataPts; ++j) { | ||
| ArgChecker.isFalse(xValues[i] == xValues[j], "Data should be distinct"); | ||
| } | ||
| if (DoubleStream.of(xValues).distinct().count() != xValues.length) { |
jodastephen
approved these changes
Jul 29, 2019
…bleArrayMath.java Co-Authored-By: Stephen Colebourne <stephen@opengamma.com>
8391972 to
c618d63
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Not sure this is the best implementation - still seems a little boilerplatey.
Could definitely be streamlined with more ArgChecker methods, potentially ones that assert that a double array is sorted, unique, and doesn't contain NaN or infinity, but maybe that's overstepping what ArgChecker is responsible for.