Skip to content

Remove N^2 uniqueness checks in interpolators#2031

Merged
jodastephen merged 9 commits intomasterfrom
topic/faster-duplication-checks
Jul 29, 2019
Merged

Remove N^2 uniqueness checks in interpolators#2031
jodastephen merged 9 commits intomasterfrom
topic/faster-duplication-checks

Conversation

@wjnicholson
Copy link
Contributor

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.

*/
public static double[] noDuplicates(double[] argument, String name) {
notNull(argument, name);
if (argument.length > 0 && DoubleStream.of(argument).distinct().count() != argument.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't have thought DoubleStream.of(argument).distinct().count() was that fast?


/**
* Return the array lengths if they are the same, otherwise throws an {@code IllegalArgumentException}.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect xValues.clone() might be quicker in cases like this.

Copy link
Contributor Author

@wjnicholson wjnicholson Jul 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArgChecker?

@jodastephen jodastephen force-pushed the topic/faster-duplication-checks branch from 8391972 to c618d63 Compare July 29, 2019 16:49
@jodastephen jodastephen merged commit c79144e into master Jul 29, 2019
@delete-merged-branch delete-merged-branch bot deleted the topic/faster-duplication-checks branch July 29, 2019 16:53
@jodastephen jodastephen added this to the v2.6 milestone Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants