MOA 12.03
Real Time Analytics for Data Streams
|
00001 /* 00002 * DoubleVector.java 00003 * Copyright (C) 2010 RWTH Aachen University, Germany 00004 * @author Reidl ([email protected]) 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 * 00019 */ 00020 00021 package moa.clusterers.clustree.util; 00022 00023 public class DoubleVector { 00024 00031 public static void addVectors(double[] a1, double[] a2) { 00032 assert (a1 != null); 00033 assert (a2 != null); 00034 assert (a1.length == a2.length) : "Adding two arrays of different " 00035 + "length"; 00036 00037 for (int i = 0; i < a1.length; i++) { 00038 a1[i] += a2[i]; 00039 } 00040 } 00041 00047 public static double[] copyVector(double[] a) { 00048 final int length = a.length; 00049 double[] res = new double[length]; 00050 /*for (int i = 0; i < res.length; i++) { 00051 res[i] = a[i]; 00052 }*/ 00053 System.arraycopy(a, 0, res, 0, length); 00054 return res; 00055 } 00056 00061 public static void normalizeVector(double[] a) { 00062 double length = calculateLength(a); 00063 for (int i = 0; i < a.length; i++) { 00064 a[i] /= length; 00065 } 00066 } 00067 00073 public static void multiplyVector(double[] a, double f) { 00074 for (int i = 0; i < a.length; i++) { 00075 a[i] *= f; 00076 } 00077 } 00078 00083 public static double calculateLength(double[] a) { 00084 double length = 0.0; 00085 for (int i = 0; i < a.length; i++) { 00086 length += a[i] * a[i]; 00087 } 00088 return Math.sqrt(length); 00089 } 00090 00100 public static double calculateDistance(final double[] a1, final double[] a2) { 00101 assert (a1.length == a2.length); 00102 double distance = 0.0; 00103 int length = a1.length; 00104 for (int i = 0; i < length; i++) { 00105 double d = a1[i] - a2[i]; 00106 distance += d * d; 00107 } 00108 00109 return Math.sqrt(distance); 00110 } 00111 00112 00117 public static boolean isZero( double[] vector ) { 00118 for ( int i = 0; i < vector.length; i++ ) { 00119 if ( vector[i] != 0.0 ) 00120 return false; 00121 } 00122 return true; 00123 } 00124 00130 public static void addConstant(double[] vector, double c) { 00131 for (int i = 0; i < vector.length; i++) { 00132 vector[i] += c; 00133 } 00134 } 00135 }