org.apache.mahout.math
Class AbstractVector

java.lang.Object
  extended by org.apache.mahout.math.AbstractVector
All Implemented Interfaces:
java.lang.Cloneable, Vector
Direct Known Subclasses:
AbstractMatrix.TransposeViewVector, DenseVector, RandomAccessSparseVector, SequentialAccessSparseVector, VectorView

public abstract class AbstractVector
extends java.lang.Object
implements Vector

Implementations of generic capabilities like sum of elements and dot products


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.mahout.math.Vector
Vector.Element
 
Field Summary
protected  double lengthSquared
           
protected  int size
           
 
Constructor Summary
protected AbstractVector()
           
protected AbstractVector(java.lang.String name)
           
protected AbstractVector(java.lang.String name, int size)
           
 
Method Summary
 void addTo(Vector v)
          Add the elements to the other vector and results are stored in that vector.
 double aggregate(BinaryFunction aggregator, UnaryFunction map)
          Examples speak louder than words: aggregate(plus, pow(2)) is another way to say getLengthSquared(), aggregate(max, abs) is norm(Double.POSITIVE_INFINITY).
 double aggregate(Vector other, BinaryFunction aggregator, BinaryFunction combiner)
          Generalized inner product - take two vectors, iterate over them both, using the combiner to combine together (and possibly map in some way) each pair of values, which are then aggregated with the previous accumulated value in the combiner.
 java.lang.String asFormatString()
           
 Vector assign(BinaryFunction f, double y)
          Apply the function to each element of the receiver, using the y value as the second argument of the BinaryFunction
 Vector assign(double value)
          Assign the value to all elements of the receiver
 Vector assign(double[] values)
          Assign the values to the receiver
 Vector assign(UnaryFunction function)
          Apply the function to each element of the receiver
 Vector assign(Vector other)
          Assign the other matrix values to the receiver
 Vector assign(Vector other, BinaryFunction function)
          Apply the function to each element of the receiver and the corresponding element of the other argument
 Vector clone()
          Return a copy of the recipient
 Matrix cross(Vector other)
          Return the cross product of the receiver and the other vector
static Vector decodeVector(java.lang.String formattedString)
          Decodes a point from its string representation.
 Vector divide(double x)
          Return a new matrix containing the values of the recipient divided by the argument
 double dot(Vector x)
          Return the dot product of the recipient and the argument
 double dotSelf()
           
static boolean equivalent(Vector left, Vector right)
          Compare whether two Vector implementations have the same elements, regardless of the implementation and name.
 double get(int index)
          Return the value at the given index
 double get(java.lang.String label)
          Return the value at the index defined by the label
 double getDistanceSquared(Vector v)
          Get the square of the distance between this vector and the other vector.
 java.util.Map<java.lang.String,java.lang.Integer> getLabelBindings()
          Return a map of the current label bindings of the receiver
 double getLengthSquared()
          Return the sum of squares of all elements in the vector.
 java.lang.String getName()
          Vectors may have a name associated with them, which makes them easy to identify
 int hashCode()
           
protected abstract  Matrix matrixLike(int rows, int columns)
          Subclasses must override to return an appropriately sparse or dense result
 double maxValue()
           
 int maxValueIndex()
           
 Vector minus(Vector x)
          Return a new matrix containing the element by element difference of the recipient and the argument
 double norm(double power)
          Return the k-norm of the vector.
 Vector normalize()
          Return a new matrix containing the normalized (L_2 norm) values of the recipient
 Vector normalize(double power)
          Return a new Vector containing the normalized (L_power norm) values of the recipient.
 Vector plus(double x)
          Return a new matrix containing the sum of each value of the recipient and the argument
 Vector plus(Vector x)
          Return a new matrix containing the element by element sum of the recipient and the argument
 void set(int index, double value)
          Set the value at the given index
 void set(java.lang.String label, double value)
          Set the value at the index that is mapped to the label
 void set(java.lang.String label, int index, double value)
          Set the value at the index and add the label to the bindings
 void setLabelBindings(java.util.Map<java.lang.String,java.lang.Integer> bindings)
          Sets a map of label bindings in the receiver
 void setName(java.lang.String name)
          Set a name for this vector.
 int size()
          Return the cardinality of the recipient (the maximum number of values)
static boolean strictEquivalence(Vector left, Vector right)
          Compare whether two Vector implementations are the same, including the underlying implementation.
 Vector times(double x)
          Return a new matrix containing the product of each value of the recipient and the argument
 Vector times(Vector x)
          Return a new matrix containing the element-wise product of the recipient and the argument
 Vector viewPart(int offset, int length)
          Return a new matrix containing the subset of the recipient
 double zSum()
          Return the sum of all the elements of the receiver
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.mahout.math.Vector
getElement, getNumNondefaultElements, getQuick, iterateAll, iterateNonZero, like, like, setQuick
 

Field Detail

size

protected int size

lengthSquared

protected double lengthSquared
Constructor Detail

AbstractVector

protected AbstractVector()

AbstractVector

protected AbstractVector(java.lang.String name)

AbstractVector

protected AbstractVector(java.lang.String name,
                         int size)
Method Detail

aggregate

public double aggregate(BinaryFunction aggregator,
                        UnaryFunction map)
Description copied from interface: Vector
Examples speak louder than words: aggregate(plus, pow(2)) is another way to say getLengthSquared(), aggregate(max, abs) is norm(Double.POSITIVE_INFINITY). To sum all of the postive values, aggregate(plus, max(0)).

Specified by:
aggregate in interface Vector
Parameters:
aggregator - used to combine the current value of the aggregation with the result of map.apply(nextValue)
map - a function to apply to each element of the vector in turn before passing to the aggregator
Returns:
the final aggregation

aggregate

public double aggregate(Vector other,
                        BinaryFunction aggregator,
                        BinaryFunction combiner)
Description copied from interface: Vector

Generalized inner product - take two vectors, iterate over them both, using the combiner to combine together (and possibly map in some way) each pair of values, which are then aggregated with the previous accumulated value in the combiner.

Example: dot(other) could be expressed as aggregate(other, Plus, Times), and kernelized inner products (which are symmetric on the indices) work similarly.

Specified by:
aggregate in interface Vector
Parameters:
other - a vector to aggregate in combination with
Returns:
the final aggregation

matrixLike

protected abstract Matrix matrixLike(int rows,
                                     int columns)
Subclasses must override to return an appropriately sparse or dense result

Parameters:
rows - the row cardinality
columns - the column cardinality
Returns:
a Matrix

viewPart

public Vector viewPart(int offset,
                       int length)
Description copied from interface: Vector
Return a new matrix containing the subset of the recipient

Specified by:
viewPart in interface Vector
Parameters:
offset - an int offset into the receiver
length - the cardinality of the desired result
Returns:
a new Vector

clone

public Vector clone()
Description copied from interface: Vector
Return a copy of the recipient

Specified by:
clone in interface Vector
Overrides:
clone in class java.lang.Object
Returns:
a new Vector

divide

public Vector divide(double x)
Description copied from interface: Vector
Return a new matrix containing the values of the recipient divided by the argument

Specified by:
divide in interface Vector
Parameters:
x - a double value
Returns:
a new Vector

dot

public double dot(Vector x)
Description copied from interface: Vector
Return the dot product of the recipient and the argument

Specified by:
dot in interface Vector
Parameters:
x - a Vector
Returns:
a new Vector

dotSelf

public double dotSelf()

get

public double get(int index)
Description copied from interface: Vector
Return the value at the given index

Specified by:
get in interface Vector
Parameters:
index - an int index
Returns:
the double at the index

minus

public Vector minus(Vector x)
Description copied from interface: Vector
Return a new matrix containing the element by element difference of the recipient and the argument

Specified by:
minus in interface Vector
Parameters:
x - a Vector
Returns:
a new Vector

normalize

public Vector normalize()
Description copied from interface: Vector
Return a new matrix containing the normalized (L_2 norm) values of the recipient

Specified by:
normalize in interface Vector
Returns:
a new Vector

normalize

public Vector normalize(double power)
Description copied from interface: Vector
Return a new Vector containing the normalized (L_power norm) values of the recipient.

See http://en.wikipedia.org/wiki/Lp_space

Technically, when 0 < power < 1, we don't have a norm, just a metric, but we'll overload this here.

Also supports power == 0 (number of non-zero elements) and power = Double.POSITIVE_INFINITY (max element). Again, see the Wikipedia page for more info

Specified by:
normalize in interface Vector
Parameters:
power - The power to use. Must be >= 0. May also be Double.POSITIVE_INFINITY. See the Wikipedia link for more on this.
Returns:
a new Vector

norm

public double norm(double power)
Description copied from interface: Vector
Return the k-norm of the vector.

See http://en.wikipedia.org/wiki/Lp_space

Technically, when 0 > power < 1, we don't have a norm, just a metric, but we'll overload this here. Also supports power == 0 (number of non-zero elements) and power = Double.POSITIVE_INFINITY (max element). Again, see the Wikipedia page for more info.

Specified by:
norm in interface Vector
Parameters:
power - The power to use.
See Also:
Vector.normalize(double)

getLengthSquared

public double getLengthSquared()
Description copied from interface: Vector
Return the sum of squares of all elements in the vector. Square root of this value is the length of the vector.

Specified by:
getLengthSquared in interface Vector

getDistanceSquared

public double getDistanceSquared(Vector v)
Description copied from interface: Vector
Get the square of the distance between this vector and the other vector.

Specified by:
getDistanceSquared in interface Vector

maxValue

public double maxValue()
Specified by:
maxValue in interface Vector
Returns:
The maximum value in the Vector

maxValueIndex

public int maxValueIndex()
Specified by:
maxValueIndex in interface Vector
Returns:
The index of the maximum value

plus

public Vector plus(double x)
Description copied from interface: Vector
Return a new matrix containing the sum of each value of the recipient and the argument

Specified by:
plus in interface Vector
Parameters:
x - a double
Returns:
a new Vector

plus

public Vector plus(Vector x)
Description copied from interface: Vector
Return a new matrix containing the element by element sum of the recipient and the argument

Specified by:
plus in interface Vector
Parameters:
x - a Vector
Returns:
a new Vector

addTo

public void addTo(Vector v)
Description copied from interface: Vector
Add the elements to the other vector and results are stored in that vector.

Specified by:
addTo in interface Vector

set

public void set(int index,
                double value)
Description copied from interface: Vector
Set the value at the given index

Specified by:
set in interface Vector
Parameters:
index - an int index into the receiver
value - a double value to set

times

public Vector times(double x)
Description copied from interface: Vector
Return a new matrix containing the product of each value of the recipient and the argument

Specified by:
times in interface Vector
Parameters:
x - a double argument
Returns:
a new Vector

times

public Vector times(Vector x)
Description copied from interface: Vector
Return a new matrix containing the element-wise product of the recipient and the argument

Specified by:
times in interface Vector
Parameters:
x - a Vector argument
Returns:
a new Vector

zSum

public double zSum()
Description copied from interface: Vector
Return the sum of all the elements of the receiver

Specified by:
zSum in interface Vector
Returns:
a double

assign

public Vector assign(double value)
Description copied from interface: Vector
Assign the value to all elements of the receiver

Specified by:
assign in interface Vector
Parameters:
value - a double value
Returns:
the modified receiver

assign

public Vector assign(double[] values)
Description copied from interface: Vector
Assign the values to the receiver

Specified by:
assign in interface Vector
Parameters:
values - a double[] of values
Returns:
the modified receiver

assign

public Vector assign(Vector other)
Description copied from interface: Vector
Assign the other matrix values to the receiver

Specified by:
assign in interface Vector
Parameters:
other - a Vector
Returns:
the modified receiver

assign

public Vector assign(BinaryFunction f,
                     double y)
Description copied from interface: Vector
Apply the function to each element of the receiver, using the y value as the second argument of the BinaryFunction

Specified by:
assign in interface Vector
Parameters:
f - a BinaryFunction to be applied
y - a double value to be argument to the function
Returns:
the modified receiver

assign

public Vector assign(UnaryFunction function)
Description copied from interface: Vector
Apply the function to each element of the receiver

Specified by:
assign in interface Vector
Parameters:
function - a UnaryFunction to apply
Returns:
the modified receiver

assign

public Vector assign(Vector other,
                     BinaryFunction function)
Description copied from interface: Vector
Apply the function to each element of the receiver and the corresponding element of the other argument

Specified by:
assign in interface Vector
Parameters:
other - a Vector containing the second arguments to the function
function - a BinaryFunction to apply
Returns:
the modified receiver

cross

public Matrix cross(Vector other)
Description copied from interface: Vector
Return the cross product of the receiver and the other vector

Specified by:
cross in interface Vector
Parameters:
other - another Vector
Returns:
a Matrix

decodeVector

public static Vector decodeVector(java.lang.String formattedString)
Decodes a point from its string representation.

Parameters:
formattedString - a formatted String produced by asFormatString. Note the payload remainder: it is optional, but can be present.
Returns:
the n-dimensional point

getName

public java.lang.String getName()
Description copied from interface: Vector
Vectors may have a name associated with them, which makes them easy to identify

Specified by:
getName in interface Vector
Returns:
The name, or null if one has not been set

setName

public void setName(java.lang.String name)
Description copied from interface: Vector
Set a name for this vector. Need not be unique in a set of Vectors, but probably is more useful if it is. In other words, Mahout does not check for uniqueness.

Specified by:
setName in interface Vector
Parameters:
name - The name

size

public int size()
Description copied from interface: Vector
Return the cardinality of the recipient (the maximum number of values)

Specified by:
size in interface Vector
Returns:
an int

asFormatString

public java.lang.String asFormatString()
Specified by:
asFormatString in interface Vector
Returns:
a formatted String suitable for output

equivalent

public static boolean equivalent(Vector left,
                                 Vector right)
Compare whether two Vector implementations have the same elements, regardless of the implementation and name. Two Vectors are equivalent if they have the same cardinality and all of their values are the same.

Does not compare Vector.getName().

Parameters:
left - The left hand Vector to compare
right - The right hand Vector
Returns:
true if the two Vectors have the same cardinality and the same values
See Also:
strictEquivalence(Vector, Vector), Vector#equals(Object)

strictEquivalence

public static boolean strictEquivalence(Vector left,
                                        Vector right)
Compare whether two Vector implementations are the same, including the underlying implementation. Two Vectors are the same if they have the same cardinality, same name and all of their values are the same.

Parameters:
left - The left hand Vector to compare
right - The right hand Vector
Returns:
true if the two Vectors have the same cardinality and the same values

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

get

public double get(java.lang.String label)
           throws IndexException,
                  UnboundLabelException
Description copied from interface: Vector
Return the value at the index defined by the label

Specified by:
get in interface Vector
Parameters:
label - a String label that maps to an index
Returns:
the double at the index
Throws:
IndexException - if the index is out of bounds
UnboundLabelException - if the label is unbound

getLabelBindings

public java.util.Map<java.lang.String,java.lang.Integer> getLabelBindings()
Description copied from interface: Vector
Return a map of the current label bindings of the receiver

Specified by:
getLabelBindings in interface Vector
Returns:
a Map

set

public void set(java.lang.String label,
                double value)
         throws IndexException,
                UnboundLabelException
Description copied from interface: Vector
Set the value at the index that is mapped to the label

Specified by:
set in interface Vector
Parameters:
label - a String label that maps to an index
value - the double value at the index
Throws:
IndexException
UnboundLabelException

setLabelBindings

public void setLabelBindings(java.util.Map<java.lang.String,java.lang.Integer> bindings)
Description copied from interface: Vector
Sets a map of label bindings in the receiver

Specified by:
setLabelBindings in interface Vector
Parameters:
bindings - a Map of label bindings

set

public void set(java.lang.String label,
                int index,
                double value)
         throws IndexException
Description copied from interface: Vector
Set the value at the index and add the label to the bindings

Specified by:
set in interface Vector
Parameters:
label - a String label that maps to an index
index - an int index
value - a double value
Throws:
IndexException


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.