diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/data/FloatVector.java')
-rw-r--r-- | src/de/lmu/ifi/dbs/elki/data/FloatVector.java | 135 |
1 files changed, 41 insertions, 94 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/data/FloatVector.java b/src/de/lmu/ifi/dbs/elki/data/FloatVector.java index 6943eb8a..e34e97d9 100644 --- a/src/de/lmu/ifi/dbs/elki/data/FloatVector.java +++ b/src/de/lmu/ifi/dbs/elki/data/FloatVector.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.data; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2011 + Copyright (C) 2012 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -28,11 +28,13 @@ import java.nio.ByteBuffer; import java.util.Iterator; import java.util.List; -import de.lmu.ifi.dbs.elki.math.linearalgebra.Matrix; import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector; import de.lmu.ifi.dbs.elki.persistent.ByteArrayUtil; import de.lmu.ifi.dbs.elki.persistent.ByteBufferSerializer; -import de.lmu.ifi.dbs.elki.utilities.Util; +import de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.ArrayAdapter; +import de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.ArrayLikeUtil; +import de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.NumberArrayAdapter; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer; /** * A FloatVector is to store real values approximately as float values. @@ -41,6 +43,11 @@ import de.lmu.ifi.dbs.elki.utilities.Util; */ public class FloatVector extends AbstractNumberVector<FloatVector, Float> implements ByteBufferSerializer<FloatVector> { /** + * Static factory instance + */ + public static final FloatVector STATIC = new FloatVector(new float[0], true); + + /** * Keeps the values of the float vector */ private float[] values; @@ -100,9 +107,9 @@ public class FloatVector extends AbstractNumberVector<FloatVector, Float> implem * @param columnMatrix a matrix of one column */ public FloatVector(Vector columnMatrix) { - values = new float[columnMatrix.getRowDimensionality()]; + values = new float[columnMatrix.getDimensionality()]; for(int i = 0; i < values.length; i++) { - values[i] = (float) columnMatrix.get(i, 0); + values[i] = (float) columnMatrix.get(i); } } @@ -143,75 +150,7 @@ public class FloatVector extends AbstractNumberVector<FloatVector, Float> implem @Override public Vector getColumnVector() { - return new Vector(Util.convertToDoubles(values)); - } - - @Override - public Matrix getRowVector() { - return new Matrix(new double[][] { Util.convertToDoubles(values) }); - } - - @Override - public FloatVector plus(FloatVector fv) { - if(fv.getDimensionality() != this.getDimensionality()) { - throw new IllegalArgumentException("Incompatible dimensionality: " + this.getDimensionality() + " - " + fv.getDimensionality() + "."); - } - float[] values = new float[this.values.length]; - for(int i = 0; i < values.length; i++) { - values[i] = this.values[i] + fv.getValue(i + 1); - } - return new FloatVector(values, true); - } - - @Override - public FloatVector minus(FloatVector fv) { - if(fv.getDimensionality() != this.getDimensionality()) { - throw new IllegalArgumentException("Incompatible dimensionality: " + this.getDimensionality() + " - " + fv.getDimensionality() + "."); - } - float[] values = new float[this.values.length]; - for(int i = 0; i < values.length; i++) { - values[i] = this.values[i] - fv.getValue(i + 1); - } - return new FloatVector(values, true); - } - - /** - * Provides the scalar product (inner product) of this and the given - * FloatVector. - * - * @param f the FloatVector to compute the scalar product for - * @return the scalar product (inner product) of this and the given - * FloatVector - */ - @Override - public Float scalarProduct(FloatVector f) { - if(this.getDimensionality() != f.getDimensionality()) { - throw new IllegalArgumentException("Incompatible dimensionality: " + this.getDimensionality() + " - " + f.getDimensionality() + "."); - } - float result = 0.0f; - for(int i = 0; i < this.getDimensionality(); i++) { - result += this.values[i] * f.values[i]; - } - return result; - } - - @Override - public FloatVector nullVector() { - return new FloatVector(new float[this.values.length], true); - } - - @Override - public FloatVector negativeVector() { - return multiplicate(-1); - } - - @Override - public FloatVector multiplicate(double k) { - float[] values = new float[this.values.length]; - for(int i = 0; i < values.length; i++) { - values[i] = (float) (this.values[i] * k); - } - return new FloatVector(values, true); + return new Vector(ArrayLikeUtil.toPrimitiveDoubleArray(values, ArrayLikeUtil.FLOATARRAYADAPTER)); } @Override @@ -226,30 +165,24 @@ public class FloatVector extends AbstractNumberVector<FloatVector, Float> implem return featureLine.toString(); } - /** - * @return a new FloatVector with the specified values - */ - @Override - public FloatVector newInstance(Vector values) { - return new FloatVector(values); - } - - /** - * @return a new FloatVector with the specified values - */ - @Override - public FloatVector newInstance(double[] values) { - return new FloatVector(Util.convertToFloat(values)); - } - @Override - public FloatVector newInstance(Float[] values) { - return new FloatVector(values); + public <A> FloatVector newFeatureVector(A array, ArrayAdapter<Float, A> adapter) { + int dim = adapter.size(array); + float[] values = new float[dim]; + for(int i = 0; i < dim; i++) { + values[i] = adapter.get(array, i); + } + return new FloatVector(values, true); } @Override - public FloatVector newInstance(List<Float> values) { - return new FloatVector(values); + public <A> FloatVector newNumberVector(A array, NumberArrayAdapter<?, A> adapter) { + int dim = adapter.size(array); + float[] values = new float[dim]; + for(int i = 0; i < dim; i++) { + values[i] = adapter.getFloat(array, i); + } + return new FloatVector(values, true); } @Override @@ -281,4 +214,18 @@ public class FloatVector extends AbstractNumberVector<FloatVector, Float> implem public int getByteSize(FloatVector vec) { return ByteArrayUtil.SIZE_SHORT + ByteArrayUtil.SIZE_FLOAT * vec.getDimensionality(); } + + /** + * Parameterization class + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + public static class Parameterizer extends AbstractParameterizer { + @Override + protected FloatVector makeInstance() { + return STATIC; + } + } }
\ No newline at end of file |