Sums all array components then puts to power n. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Sums all array components then puts to power n.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] data = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        int n = 2;
        System.out.println(getPowerN(data, n));
    }//w  w w  .  ja  v a 2s.  co  m

    /**
     * Sums all components then puts to power n.
     * This is the direct computation of the polynomial expansion
     */
    //also see MathUtils.polynomialExpansion()
    public static double getPowerN(double[] data, int n) {
        double ans = 0.0;
        for (int k = 0; k < data.length; k++) {
            ans += data[k];
        }
        return Math.pow(ans, n);
    }
}

Related Tutorials