Here you can find the source of norm(double[] vector, int n)
Parameter | Description |
---|---|
vector | a parameter |
n | a parameter |
public static double norm(double[] vector, int n)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from ww w .ja v a 2 s . c o m*/ * This method takes the Ln vector norm of the vector according to the formula: * Norm = [E(i=0 to vector.length) |vector[i]|^n]^(1.0/n) * @param vector * @param n * @return the vector norm of the given vector to the nth degree */ public static double norm(double[] vector, int n) { double norm = 0.0; //Go through each component for (int i = 0; i < vector.length; ++i) { double inner = 1.0; double comp = Math.abs(vector[i]); //Get the appropriate power of the component for (int dim = 0; dim < n; ++dim) inner *= comp; //Add the |comp|^n to our sum norm += inner; } //Take the appropriate root, don't waste the method call if //n is 1. if (n != 1) norm = Math.pow(norm, 1.0 / n); return norm; } }