[
https://issues.apache.org/jira/browse/NUMBERS-156?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17335523#comment-17335523
]
Alex Herbert commented on NUMBERS-156:
--------------------------------------
This seems to combine the ideas of SafeNorm and exact scaling:
{code:java}
/**
* @param v Cartesian coordinates.
* @return the 2-norm of the vector.
*/
public static double value(double[] v) {
// Sum of big, normal and small numbers
double s1 = 0;
double s2 = 0;
double s3 = 0;
for (int i = 0; i < v.length; i++) {
final double x = Math.abs(v[i]);
if (x > 0x1.0p500) {
// Scale down big numbers
s1 += square(x * 0x1.0p-600);
} else if (x < 0x1.0p-500) {
// Scale up small numbers
s3 += square(x * 0x1.0p600);
} else {
// Unscaled
s2 += square(x);
}
}
// The highest sum is the significant component. Add the next significant.
if (s1 != 0) {
return Math.sqrt(s1 + s2 * 0x1.0p-600 * 0x1.0p-600) * 0x1.0p600;
} else if (s2 != 0) {
return Math.sqrt(s2 + s3 * 0x1.0p-600 * 0x1.0p-600);
}
return Math.sqrt(s3) * 0x1.0p-600;
}
/**
* Square the value.
*
* @param x the value
* @return x^2
*/
private static double square(double x) {
return x * x;
}
{code}
* Single pass algorithm
* Matches the simple sum of squares when values would not over/underflow
* Handles under/overflow
* Is invariant to scaling so that: s * norm( x ) == norm( sx )
This passes the SafeNorm unit test (which does not check handling of infinites
or NaN).
I would have to check what happens around the boundaries 2^500 and 2^-500 with
regards to scaled values, i.e. is this the correct way to add the two sums with
a magnitude change between them.
> SafeNorm 3D overload
> --------------------
>
> Key: NUMBERS-156
> URL: https://issues.apache.org/jira/browse/NUMBERS-156
> Project: Commons Numbers
> Issue Type: Improvement
> Reporter: Matt Juntunen
> Priority: Major
>
> We should create an overload of {{SafeNorm.value}} that accepts 3 arguments
> to potentially improve performance for 3D vectors.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)