org.retro.neural
Class Vector

java.lang.Object
  extended byorg.retro.neural.Vector

public class Vector
extends java.lang.Object


Field Summary
protected  FieldElement[] entries
           
 
Constructor Summary
Vector(FieldElement[] theEntries)
           
Vector(int length)
           
 
Method Summary
 Vector add(FieldElement scalar)
          Returns the sum of this Vector and a scalar.
 Vector add(Vector anotherVector)
          Returns the sum of this Vector and another.
 void addReplace(FieldElement scalar)
          Adds a scalar to this Vector.
 void addReplace(Vector anotherVector)
          Adds another vector to this Vector.
 Vector and(Vector anotherVector)
          Returns the logical AND of this Vector with another.
 Vector apply(FieldElement scalar, DyadicOperator fun)
          Returns the result of applying a specified function to the elements of a this Vector a scalar.
 Vector apply(MonadicOperator fun)
          Returns the result of applying a specified function to every element of this Vector.
 Vector apply(Vector anotherVector, DyadicOperator fun)
          Returns the result of applying a specified function to the elements of this Vector and another.
 void applyReplace(FieldElement scalar, DyadicOperator fun)
          Sets this Vector to the result of applying a specified function to elements of this Vector and a scalar.
 void applyReplace(MonadicOperator fun)
          Sets this Vector to the result of applying a specified function to every element of this Vector.
 void applyReplace(Vector anotherVector, DyadicOperator fun)
          Sets this Vector to the result of applying a specified function to elements of this Vector and another's.
 Vector arrayMultiply(Vector anotherVector)
          Returns the element-wise product of this Vector and another.
 Vector copy()
          Returns a deep copy of this Vector.
 FieldElement cosine(Vector anotherVector)
          Returns the cosine between this Vector and another.
 Matrix cross(Vector anotherVector)
          Returns the cross-product of this Vector and another.
 Vector divide(FieldElement scalar)
          Returns this Vector divided by a scalar.
 void divideReplace(FieldElement scalar)
          Divides this Vector by a scalar.
 Vector eq(FieldElement scalar)
          Returns a Vector containing ones where this Vector's elements are equal to a scalar, and zeros elsewhere.
 Vector eq(Vector anotherVector)
          Returns a Vector containing ones where this Vector's elements are equal to those of another Vectors, and zeros elsewhere.
 boolean equals(Vector anotherVector)
          Tests two Vectors for equality.
 int[] find(FieldElement scalar)
          Returns indices where vector equals scalar argument.
 Vector ge(FieldElement scalar)
          Returns a Vector containing ones where this Vector's elements are greater than or equal to a scalar, and zeros elsewhere.
 Vector ge(Vector anotherVector)
          Returns a Vector containing ones where this Vector's elements are greater than or equal to those of another Vectors, and zeros elsewhere.
 FieldElement getEntry(int index)
          Gets the entry of a certain index.
 Vector gt(FieldElement scalar)
          Returns a Vector containing ones where this Vector's elements are greater than a scalar, and zeros elsewhere.
 Vector gt(Vector anotherVector)
          Returns a Vector containing ones where this Vector's elements are greater than those of another Vectors, and zeros elsewhere.
 FieldElement L1Norm()
          Returns the L1 norm of this Vector.
 Vector le(FieldElement scalar)
          Returns a Vector containing ones where this Vector's elements are less than or equal to a scalar, and zeros elsewhere.
 Vector le(Vector anotherVector)
          Returns a Vector containing ones where this Vector's elements are less than or equal to those of another Vectors, and zeros elsewhere.
 int length()
          Returns the length of the Vector.
 Vector lt(FieldElement scalar)
          Returns a Vector containing ones where this Vector's elements are less than a scalar, and zeros elsewhere.
 Vector lt(Vector anotherVector)
          Returns a Vector containing ones where this Vector's elements are less than those of another Vectors, and zeros elsewhere.
 FieldElement max()
          Computes the largest value of any element in this Vector.
 FieldElement mean()
          Computes the mean over the elements of this Vector.
 FieldElement min()
          Computes the smallest value of any element in this Vector.
 Vector multiply(FieldElement scalar)
          Returns the product of this Vector and a scalar.
 Vector multiply(Matrix theMatrix)
          Returns the product of this Vector and a Matrix.
 FieldElement multiply(Vector anotherVector)
          Returns the standard inner (dot) product of this Vector and another.
 void multiplyReplace(FieldElement scalar)
          Multiplies this Vector by a scalar.
 void multiplyReplace(Vector anotherVector)
          Multiplies this Vector element-wise by another.
 Vector ne(FieldElement scalar)
          Returns a Vector containing ones where this Vector's elements are not equal to a scalar, and zeros elsewhere.
 Vector ne(Vector anotherVector)
          Returns a Vector containing ones where this Vector's elements are not equal to those of another Vector, and zeros elsewhere.
 Vector not()
          Returns the logical negation of this Vector.
 FieldElement nycDist(Vector anotherVector)
          Returns the Manhattan distance (L1 norm of differences) between this Vector and another.
 Vector or(Vector anotherVector)
          Returns the logical OR of this Vector with another.
 Matrix repmat(int m)
          Replicates this Vector as a Matrix.
protected  Vector safe_diff(Vector anotherVector, java.lang.String method)
           
 void setAll(FieldElement newEntry)
          Sets all entries to a FieldElement.
 void setEntry(int index, FieldElement newEntry)
          Sets an entry to a FieldElement at a certain index.
 Vector sort()
          Returns a copy of this Vector, sorted in ascending order.
 Vector subtract(FieldElement scalar)
          Returns this Vector subtracted by a scalar.
 Vector subtract(Vector anotherVector)
          Returns the result of subtracting another vector from this.
 void subtractReplace(FieldElement scalar)
          Subtracts a scalar from this Vector.
 void subtractReplace(Vector anotherVector)
          Subtracts another Vector from this.
 FieldElement sum()
          Computes the sum over the elements of this Vector.
 void swapEntries(int index1, int index2)
          Swaps two Entries of a Vector.
 Matrix toMatrix()
          Returns a new 1xN Matrix made from the N elements of this Vector.
 java.lang.String toString()
          Returns a String representation of this Vector
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

entries

protected FieldElement[] entries
Constructor Detail

Vector

public Vector(int length)

Vector

public Vector(FieldElement[] theEntries)
       throws InvalidOperationException
Method Detail

length

public int length()
Returns the length of the Vector.

Returns:
the length

L1Norm

public FieldElement L1Norm()
Returns the L1 norm of this Vector.

Returns:
L1 norm of this Vector

nycDist

public FieldElement nycDist(Vector anotherVector)
                     throws InvalidOperationException
Returns the Manhattan distance (L1 norm of differences) between this Vector and another.

Parameters:
anotherVector -
Returns:
Manhattan distance between this and anotherVector
Throws:
InvalidOperationException - if Vectors have unequal lengths

cosine

public FieldElement cosine(Vector anotherVector)
                    throws InvalidOperationException
Returns the cosine between this Vector and another. Cosine is the dot product of the vectors, divided by the product of their lengths.

Parameters:
anotherVector -
Returns:
cosine between this and anotherVector
Throws:
InvalidOperationException - if Vectors have unequal lengths

getEntry

public FieldElement getEntry(int index)
                      throws InvalidOperationException
Gets the entry of a certain index. First index is 1.

Returns:
the entry
Throws:
InvalidOperationException - if the index is out of bounds

setEntry

public void setEntry(int index,
                     FieldElement newEntry)
Sets an entry to a FieldElement at a certain index.

Parameters:
index -
newEntry -
Throws:
InvalidOperationException - if the index is invalide

toMatrix

public Matrix toMatrix()
Returns a new 1xN Matrix made from the N elements of this Vector.

Returns:
the Matrix

divide

public Vector divide(FieldElement scalar)
Returns this Vector divided by a scalar.

Parameters:
scalar -
Returns:
the divided Vector

divideReplace

public void divideReplace(FieldElement scalar)
Divides this Vector by a scalar.

Parameters:
scalar -

multiply

public Vector multiply(FieldElement scalar)
Returns the product of this Vector and a scalar.

Parameters:
scalar -
Returns:
the multiplied Vector

arrayMultiply

public Vector arrayMultiply(Vector anotherVector)
                     throws InvalidOperationException
Returns the element-wise product of this Vector and another.

Parameters:
anotherVector -
Returns:
this .* anotherVector
Throws:
InvalidOperationException - if the vectors have unequal lengths

multiplyReplace

public void multiplyReplace(FieldElement scalar)
Multiplies this Vector by a scalar.

Parameters:
scalar -

multiplyReplace

public void multiplyReplace(Vector anotherVector)
Multiplies this Vector element-wise by another.

Throws:
InvalidOperationException - if the vectors have unequal lengths

add

public Vector add(FieldElement scalar)
Returns the sum of this Vector and a scalar.

Parameters:
scalar -
Returns:
the add Vector

add

public Vector add(Vector anotherVector)
           throws InvalidOperationException
Returns the sum of this Vector and another.

Parameters:
anotherVector -
Returns:
this + anotherVector
Throws:
InvalidOperationException - if the vectors have unequal lengths

addReplace

public void addReplace(FieldElement scalar)
Adds a scalar to this Vector.

Parameters:
scalar -

addReplace

public void addReplace(Vector anotherVector)
Adds another vector to this Vector.

Throws:
InvalidOperationException - if the vectors have unequal lengths

subtract

public Vector subtract(FieldElement scalar)
Returns this Vector subtracted by a scalar.

Parameters:
scalar -
Returns:
the subtractd Vector

subtract

public Vector subtract(Vector anotherVector)
                throws InvalidOperationException
Returns the result of subtracting another vector from this.

Parameters:
anotherVector -
Returns:
this - anotherVector
Throws:
InvalidOperationException - if the vectors have unequal lengths

subtractReplace

public void subtractReplace(FieldElement scalar)
Subtracts a scalar from this Vector.

Parameters:
scalar -

subtractReplace

public void subtractReplace(Vector anotherVector)
Subtracts another Vector from this.

Throws:
InvalidOperationException - if the vectors have unequal lengths

multiply

public FieldElement multiply(Vector anotherVector)
                      throws InvalidOperationException
Returns the standard inner (dot) product of this Vector and another.

Parameters:
anotherVector -
Returns:
The inner (dot) product of the two Vectors.
Throws:
InvalidOperationException - if the other vector is null
InvalidOperationException - if the vectors have unequal lengths

equals

public boolean equals(Vector anotherVector)
Tests two Vectors for equality.

Parameters:
anotherVector -
Returns:
true if and only if the two Vectors equal in all entries.

swapEntries

public void swapEntries(int index1,
                        int index2)
Swaps two Entries of a Vector.

Parameters:
index1 - 1st swap partner
index2 - 2nd seap partner

toString

public java.lang.String toString()
Returns a String representation of this Vector


and

public Vector and(Vector anotherVector)
Returns the logical AND of this Vector with another. Elements of the result are 1 where both vectors are non-zero, and zero elsewhere.

Parameters:
anotherVector -
Returns:
Vector of 1's and 0's
Throws:
InvalidOperationException - if the vectors have unequal lengths

or

public Vector or(Vector anotherVector)
Returns the logical OR of this Vector with another. Elements of the result are 1 where both vectors are non-zero, and zero elsewhere.

Parameters:
anotherVector -
Returns:
Vector of 1's and 0's
Throws:
InvalidOperationException - if the vectors have unequal lengths

not

public Vector not()
Returns the logical negation of this Vector. Elements of the result are 1 where the vector is zero, and one elsewhere.

Returns:
Matrix of 1's and 0's

applyReplace

public void applyReplace(MonadicOperator fun)
Sets this Vector to the result of applying a specified function to every element of this Vector. New functions can be applied to a Vector by subclassing the abstract MonadicOperator class.

Parameters:
fun - the function to apply
Returns:
result of applying fun to this Vector

apply

public Vector apply(MonadicOperator fun)
Returns the result of applying a specified function to every element of this Vector. New functions can be applied to a Vector by subclassing the abstract MonadicOperator class.

Parameters:
fun - the function to apply
Returns:
result of applying fun to this Vector

applyReplace

public void applyReplace(Vector anotherVector,
                         DyadicOperator fun)
Sets this Vector to the result of applying a specified function to elements of this Vector and another's. New functions can be applied to a Vector by subclassing the abstract DyadicOperator class.

Parameters:
anotherVector -
fun - the function to apply
Returns:
result of applying fun to the two Vectors

apply

public Vector apply(Vector anotherVector,
                    DyadicOperator fun)
Returns the result of applying a specified function to the elements of this Vector and another. New functions can be applied to a Vector by subclassing the abstract DyadicOperator class.

Parameters:
anotherVector -
fun - the function to apply
Returns:
result of applying fun to the two Vectors

applyReplace

public void applyReplace(FieldElement scalar,
                         DyadicOperator fun)
Sets this Vector to the result of applying a specified function to elements of this Vector and a scalar. New functions can be applied to a Vector by subclassing the abstract DyadicOperator class.

Parameters:
scalar -
fun - the function to apply
Returns:
result of applying fun to this Vector and the scalar

apply

public Vector apply(FieldElement scalar,
                    DyadicOperator fun)
Returns the result of applying a specified function to the elements of a this Vector a scalar. New functions can be applied to a Vector by subclassing the abstract DyadicOperator class.

Parameters:
scalar -
fun - the function to apply
Returns:
result of applying fun to the Vector and scalar

copy

public Vector copy()
Returns a deep copy of this Vector.

Returns:
Vector copy

lt

public Vector lt(Vector anotherVector)
Returns a Vector containing ones where this Vector's elements are less than those of another Vectors, and zeros elsewhere.

Parameters:
anotherVector -
Returns:
Vector of ones and zeros
Throws:
InvalidOperationException - if the vectors have unequal lengths

lt

public Vector lt(FieldElement scalar)
Returns a Vector containing ones where this Vector's elements are less than a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Vector of ones and zeros

le

public Vector le(Vector anotherVector)
Returns a Vector containing ones where this Vector's elements are less than or equal to those of another Vectors, and zeros elsewhere.

Parameters:
anotherVector -
Returns:
Vector of ones and zeros
Throws:
InvalidOperationException - if the vectors have unequal lengths

le

public Vector le(FieldElement scalar)
Returns a Vector containing ones where this Vector's elements are less than or equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Vector of ones and zeros

gt

public Vector gt(Vector anotherVector)
Returns a Vector containing ones where this Vector's elements are greater than those of another Vectors, and zeros elsewhere.

Parameters:
anotherVector -
Returns:
Vector of ones and zeros
Throws:
InvalidOperationException - if the vectors have unequal lengths

gt

public Vector gt(FieldElement scalar)
Returns a Vector containing ones where this Vector's elements are greater than a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Vector of ones and zeros

ge

public Vector ge(Vector anotherVector)
Returns a Vector containing ones where this Vector's elements are greater than or equal to those of another Vectors, and zeros elsewhere.

Parameters:
anotherVector -
Returns:
Vector of ones and zeros
Throws:
InvalidOperationException - if the vectors have unequal lengths

ge

public Vector ge(FieldElement scalar)
Returns a Vector containing ones where this Vector's elements are greater than or equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Vector of ones and zeros

eq

public Vector eq(Vector anotherVector)
Returns a Vector containing ones where this Vector's elements are equal to those of another Vectors, and zeros elsewhere.

Parameters:
anotherVector -
Returns:
Vector of ones and zeros
Throws:
InvalidOperationException - if the vectors have unequal lengths

eq

public Vector eq(FieldElement scalar)
Returns a Vector containing ones where this Vector's elements are equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Vector of ones and zeros

ne

public Vector ne(Vector anotherVector)
Returns a Vector containing ones where this Vector's elements are not equal to those of another Vector, and zeros elsewhere.

Parameters:
anotherVector -
Returns:
Vector of ones and zeros
Throws:
InvalidOperationException - if the vectors have unequal lengths

ne

public Vector ne(FieldElement scalar)
Returns a Vector containing ones where this Vector's elements are not equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Vector of ones and zeros

sum

public FieldElement sum()
Computes the sum over the elements of this Vector.

Returns:
the sum

min

public FieldElement min()
Computes the smallest value of any element in this Vector.

Returns:
the smallest value

max

public FieldElement max()
Computes the largest value of any element in this Vector.

Returns:
the largest value

mean

public FieldElement mean()
Computes the mean over the elements of this Vector.

Returns:
the mean

find

public int[] find(FieldElement scalar)
Returns indices where vector equals scalar argument.

Parameters:
scalar -
Returns:
indices where vector equals scalar

setAll

public void setAll(FieldElement newEntry)
Sets all entries to a FieldElement.

Parameters:
newEntry - the FieldElement

repmat

public Matrix repmat(int m)
Replicates this Vector as a Matrix. Returns an m-by-n Matrix for a length-n Vector replicated m times.

Parameters:
m - number of times to replicate
Returns:
an m-by-n Matrix

sort

public Vector sort()
Returns a copy of this Vector, sorted in ascending order.

Returns:
sorted copy

multiply

public Vector multiply(Matrix theMatrix)
                throws InvalidOperationException
Returns the product of this Vector and a Matrix.

Parameters:
theMatrix -
Returns:
this * theMatrix
Throws:
InvalidOperationException - if the matrix is null
InvalidOperationException - if the inner dimensions mismatch

cross

public Matrix cross(Vector anotherVector)
             throws InvalidOperationException
Returns the cross-product of this Vector and another.

Parameters:
anotherVector -
Returns:
this^T X anotherVector
Throws:
InvalidOperationException - if the other Vector is null

safe_diff

protected Vector safe_diff(Vector anotherVector,
                           java.lang.String method)
                    throws InvalidOperationException
Throws:
InvalidOperationException