[ 
https://issues.apache.org/jira/browse/NUMBERS-156?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17344579#comment-17344579
 ] 

Alex Herbert commented on NUMBERS-156:
--------------------------------------

Since we are investigating extended precision summation then could you add this 
method that reuses the LinearCombination class:
{code:java}
public static double value(double[] v) {
    // Find the magnitude limits ignoring zero
    double max = 0;
    double min = Double.POSITIVE_INFINITY;
    for (int i = 0; i < v.length; i++) {
        final double x = Math.abs(v[i]);
        if (x > max) {
            max = x;
        } else if (x < min && x != 0) {
            min = x;
        }
    }
    // Edge case
    if (max == 0) {
        return 0;
    }
    // Use scaling if required
    double[] x = v;
    double rescale = 1;
    if (max > 0x1.0p500) {
        // Too big so scale down
        x = x.clone();
        for (int i = 0; i < x.length; i++) {
            x[i] *= 0x1.0p-600;
        }
        rescale = 0x1.0p600; 
    } else if (min < 0x1.0p-500) {
        // Too small so scale up
        x = x.clone();
        for (int i = 0; i < x.length; i++) {
            x[i] *= 0x1.0p600;
        }
        rescale = 0x1.0p-600; 
    }
    return Math.sqrt(LinearCombination.value(x, x)) * rescale;
}
{code}
This method differs from the Kahan summation in that the product x^2 of each 
term is computed in extended precision. The sum is then computed in extended 
precision. The method could be optimised since LinearCombination will not know 
that the two input vectors are the same and will split each term, effectively 
treating x*x as x*y. If it knew x==y then some steps could be combined/removed. 
But the above code should work for a proof-of-concept in computing the result 
with greater precision.

> 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)

Reply via email to