Java Array Normalize norm(double[] a)

Here you can find the source of norm(double[] a)

Description

Calculates the euclidean norm of a vector.

License

Open Source License

Parameter

Parameter Description
a A vector.

Return

The euclidean norm of the vector.

Declaration

public static double norm(double[] a) 

Method Source Code

//package com.java2s;
/*/* www . j  av  a2 s.  c  om*/
 * This file is part of simple-cbir.
 *
 *  simple-cbir is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  simple-cbir is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with simple-cbir.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Diese Datei ist Teil von simple-cbir.
 *
 *  simple-cbir ist Freie Software: Sie k?nnen es unter den Bedingungen
 *  der GNU General Public License, wie von der Free Software Foundation,
 *  Version 3 der Lizenz oder (nach Ihrer Wahl) jeder sp?teren
 *  ver?ffentlichten Version, weiterverbreiten und/oder modifizieren.
 *
 *  simple-cbir wird in der Hoffnung, dass es n?tzlich sein wird, aber
 *  OHNE JEDE GEW?HELEISTUNG, bereitgestellt; sogar ohne die implizite
 *  Gew?hrleistung der MARKTF?HIGKEIT oder EIGNUNG F?R EINEN BESTIMMTEN ZWECK.
 *  Siehe die GNU General Public License f?r weitere Details.
 *
 *  Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
 *  Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Calculates the euclidean norm of a vector.
     * 
     * @param a
     *            A vector.
     * @return The euclidean norm of the vector.
     */
    public static double norm(double[] a) {
        double result = 0;
        for (int i = 0; i < a.length; i++)
            result += Math.pow(a[i], 2);
        result = Math.sqrt(result);
        return result;
    }

    /**
     * Calculates the weighted euclidean norm of a vector.
     * 
     * @param a
     *            A vector.
     * @param weights
     *            A weight array.
     * @return The weighted euclidean norm of the vector.
     */
    public static double norm(double[] a, double[] weights) {
        double result = 0;
        for (int i = 0; i < a.length; i++)
            result += weights[i] * Math.pow(a[i], 2);
        result = Math.sqrt(result);
        return result;
    }
}

Related

  1. norm(byte[] tab)
  2. norm(double[] a)
  3. norm(double[] a)
  4. norm(double[] array)
  5. norm(double[] data)