MOA 12.03
Real Time Analytics for Data Streams
core/DoubleVector.java
Go to the documentation of this file.
00001 /*
00002  *    DoubleVector.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
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 package moa.core;
00021 
00022 import moa.AbstractMOAObject;
00023 
00030 public class DoubleVector extends AbstractMOAObject {
00031 
00032     private static final long serialVersionUID = 1L;
00033 
00034     protected double[] array;
00035 
00036     public DoubleVector() {
00037         this.array = new double[0];
00038     }
00039 
00040     public DoubleVector(double[] toCopy) {
00041         this.array = new double[toCopy.length];
00042         System.arraycopy(toCopy, 0, this.array, 0, toCopy.length);
00043     }
00044 
00045     public DoubleVector(DoubleVector toCopy) {
00046         this(toCopy.getArrayRef());
00047     }
00048 
00049     public int numValues() {
00050         return this.array.length;
00051     }
00052 
00053     public void setValue(int i, double v) {
00054         if (i >= this.array.length) {
00055             setArrayLength(i + 1);
00056         }
00057         this.array[i] = v;
00058     }
00059 
00060     public void addToValue(int i, double v) {
00061         if (i >= this.array.length) {
00062             setArrayLength(i + 1);
00063         }
00064         this.array[i] += v;
00065     }
00066 
00067     public void addValues(DoubleVector toAdd) {
00068         addValues(toAdd.getArrayRef());
00069     }
00070 
00071     public void addValues(double[] toAdd) {
00072         if (toAdd.length > this.array.length) {
00073             setArrayLength(toAdd.length);
00074         }
00075         for (int i = 0; i < toAdd.length; i++) {
00076             this.array[i] += toAdd[i];
00077         }
00078     }
00079 
00080     public void subtractValues(DoubleVector toSubtract) {
00081         subtractValues(toSubtract.getArrayRef());
00082     }
00083 
00084     public void subtractValues(double[] toSubtract) {
00085         if (toSubtract.length > this.array.length) {
00086             setArrayLength(toSubtract.length);
00087         }
00088         for (int i = 0; i < toSubtract.length; i++) {
00089             this.array[i] -= toSubtract[i];
00090         }
00091     }
00092 
00093     public void addToValues(double toAdd) {
00094         for (int i = 0; i < this.array.length; i++) {
00095             this.array[i] = this.array[i] + toAdd;
00096         }
00097     }
00098 
00099     public void scaleValues(double multiplier) {
00100         for (int i = 0; i < this.array.length; i++) {
00101             this.array[i] = this.array[i] * multiplier;
00102         }
00103     }
00104 
00105     // returns 0.0 for values outside of range
00106     public double getValue(int i) {
00107         return ((i >= 0) && (i < this.array.length)) ? this.array[i] : 0.0;
00108     }
00109 
00110     public double sumOfValues() {
00111         double sum = 0.0;
00112         for (double element : this.array) {
00113             sum += element;
00114         }
00115         return sum;
00116     }
00117 
00118     public int maxIndex() {
00119         int max = -1;
00120         for (int i = 0; i < this.array.length; i++) {
00121             if ((max < 0) || (this.array[i] > this.array[max])) {
00122                 max = i;
00123             }
00124         }
00125         return max;
00126     }
00127 
00128     public void normalize() {
00129         scaleValues(1.0 / sumOfValues());
00130     }
00131 
00132     public int numNonZeroEntries() {
00133         int count = 0;
00134         for (double element : this.array) {
00135             if (element != 0.0) {
00136                 count++;
00137             }
00138         }
00139         return count;
00140     }
00141 
00142     public double minWeight() {
00143         if (this.array.length > 0) {
00144             double min = this.array[0];
00145             for (int i = 1; i < this.array.length; i++) {
00146                 if (this.array[i] < min) {
00147                     min = this.array[i];
00148                 }
00149             }
00150             return min;
00151         }
00152         return 0.0;
00153     }
00154 
00155     public double[] getArrayCopy() {
00156         double[] aCopy = new double[this.array.length];
00157         System.arraycopy(this.array, 0, aCopy, 0, this.array.length);
00158         return aCopy;
00159     }
00160 
00161     public double[] getArrayRef() {
00162         return this.array;
00163     }
00164 
00165     protected void setArrayLength(int l) {
00166         double[] newArray = new double[l];
00167         int numToCopy = this.array.length;
00168         if (numToCopy > l) {
00169             numToCopy = l;
00170         }
00171         System.arraycopy(this.array, 0, newArray, 0, numToCopy);
00172         this.array = newArray;
00173     }
00174 
00175     public void getSingleLineDescription(StringBuilder out) {
00176         getSingleLineDescription(out, numValues());
00177     }
00178 
00179     public void getSingleLineDescription(StringBuilder out, int numValues) {
00180         out.append("{");
00181         for (int i = 0; i < numValues; i++) {
00182             if (i > 0) {
00183                 out.append("|");
00184             }
00185             out.append(StringUtils.doubleToString(getValue(i), 3));
00186         }
00187         out.append("}");
00188     }
00189 
00190     @Override
00191     public void getDescription(StringBuilder sb, int indent) {
00192         getSingleLineDescription(sb);
00193     }
00194 }
 All Classes Namespaces Files Functions Variables Enumerations