edu.wlu.cs.levy.SNARLI
Class SNVector

java.lang.Object
  |
  +--edu.wlu.cs.levy.SNARLI.SNVector

public class SNVector
extends java.lang.Object

This class provides glue code for vector operations in SNARLI. You do not need to call any of these methods to use SNARLI. This class is provided to show you the methods you need to implement if you want to use a different implementation for vectors.


Constructor Summary
SNVector(double[] a)
          Constructs a vector using a 1D array of double-precision floating-point values.
SNVector(int n)
          Constructs a vector of all zeros.
 
Method Summary
 void add(double s)
          Adds a scalar to this vector.
 void add(edu.wlu.cs.levy.SNARLI.SNVector x)
          Adds a vector to this vector.
static edu.wlu.cs.levy.SNARLI.SNVector add(edu.wlu.cs.levy.SNARLI.SNVector x, double s)
          Returns the sum of a vector and a scalar.
static edu.wlu.cs.levy.SNARLI.SNVector add(edu.wlu.cs.levy.SNARLI.SNVector x, edu.wlu.cs.levy.SNARLI.SNVector y)
          Returns the sum of two vectors.
 edu.wlu.cs.levy.SNARLI.SNVector and(edu.wlu.cs.levy.SNARLI.SNVector x)
          Returns the logical AND of this vector with another.
 void arrayMultiply(edu.wlu.cs.levy.SNARLI.SNVector x)
          Multiplies the elements of this vector by those of another.
static edu.wlu.cs.levy.SNARLI.SNVector arrayMultiply(edu.wlu.cs.levy.SNARLI.SNVector x, edu.wlu.cs.levy.SNARLI.SNVector y)
          Returns a vector containing the products of the elements of two vectors.
 edu.wlu.cs.levy.SNARLI.SNVector copy()
          Returns a deep copy of this vector.
 void divide(double s)
          Divides this vector by a scalar.
static edu.wlu.cs.levy.SNARLI.SNVector divide(edu.wlu.cs.levy.SNARLI.SNVector x, double s)
          Returns the quotient of a vector and a scalar.
 double eucDist(edu.wlu.cs.levy.SNARLI.SNVector x)
          Returns the Euclidean distance between this vector and another.
 int[] find(double s)
          Returns indices where vector equals scalar argument.
static edu.wlu.cs.levy.SNARLI.SNVector gaussianNoise(int n, java.util.Random r)
          Returns a vector of normally distributed random values.
 edu.wlu.cs.levy.SNARLI.SNVector ge(double s)
          Returns a vector containing ones where this vector's elements are greater than or equal to a scalar, and zeros elsewhere.
 double getValue(int i)
          Returns an indexed value from this vector.
 double[] getValues()
          Returns a 1D array of the values contained in this vector.
 edu.wlu.cs.levy.SNARLI.SNVector le(double s)
          Returns a vector containing ones where this vector's elements are less than or equal to a scalar, and zeros elsewhere.
 int length()
          Returns the length of this vector.
 double max()
          Returns the largest value of any element in this vector.
 double min()
          Returns the value of the smallest element of this vector.
 void multiply(double s)
          Multiplies this vector by a scalar.
 edu.wlu.cs.levy.SNARLI.SNVector multiply(edu.wlu.cs.levy.SNARLI.SNMatrix a)
          Returns the product of this Vector and a Matrix.
 void multiply(edu.wlu.cs.levy.SNARLI.SNVector x)
          Multiplies the elements of this vector by those of another vector.
static edu.wlu.cs.levy.SNARLI.SNVector multiply(edu.wlu.cs.levy.SNARLI.SNVector x, double s)
          Returns the product of a vector and a scalar.
 edu.wlu.cs.levy.SNARLI.SNVector ne(double s)
          Returns a vector containing ones where this vector's elements are not equal to a scalar, and zeros elsewhere.
 double nycDist(edu.wlu.cs.levy.SNARLI.SNVector x)
          Returns the Manhattan distance (L1 norm of differences) between this vector and another.
 edu.wlu.cs.levy.SNARLI.SNMatrix outer(edu.wlu.cs.levy.SNARLI.SNVector x)
          Returns the outer product of this vector and another.
 edu.wlu.cs.levy.SNARLI.SNMatrix repmat(int m)
          Replicates this vector as a matrix.
 void setAll(double s)
          Sets all entries to a scalar.
 void setValue(int i, double s)
          Sets an entry to a scalar at a certain index.
 edu.wlu.cs.levy.SNARLI.SNVector sort()
          Returns a copy of this vector, sorted in ascending order.
 void subtract(double s)
          Subtracts a scalar from this vector.
 void subtract(edu.wlu.cs.levy.SNARLI.SNVector x)
          Subtracts a vector from this vector.
static edu.wlu.cs.levy.SNARLI.SNVector subtract(edu.wlu.cs.levy.SNARLI.SNVector x, double s)
          Returns the difference between a vector and a scalar.
static edu.wlu.cs.levy.SNARLI.SNVector subtract(edu.wlu.cs.levy.SNARLI.SNVector x, edu.wlu.cs.levy.SNARLI.SNVector y)
          Returns the difference between two vectors.
 double sum()
          Returns the sum of the elements of this vector.
 java.lang.String toString()
          Returns a String representation of this vector.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SNVector

public SNVector(int n)
Constructs a vector of all zeros.

Parameters:
n - vector length

SNVector

public SNVector(double[] a)
Constructs a vector using a 1D array of double-precision floating-point values.

Parameters:
a - the array of values
Method Detail

length

public int length()
Returns the length of this vector.

Returns:
length

getValue

public double getValue(int i)
                throws java.lang.RuntimeException
Returns an indexed value from this vector. First value has index 0.

Parameters:
i - the index
Returns:
Ith value
Throws:
java.lang.RuntimeException - if i is out of range [0, length-1]

add

public void add(double s)
Adds a scalar to this vector.

Parameters:
s - the scalar

subtract

public void subtract(double s)
Subtracts a scalar from this vector.

Parameters:
s - the scalar

multiply

public void multiply(double s)
Multiplies this vector by a scalar.

Parameters:
s - the scalar

divide

public void divide(double s)
Divides this vector by a scalar.

Parameters:
s - the scalar

add

public static edu.wlu.cs.levy.SNARLI.SNVector add(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                  double s)
Returns the sum of a vector and a scalar.

Parameters:
x - the vector
s - the scalar
Returns:
x + s

subtract

public static edu.wlu.cs.levy.SNARLI.SNVector subtract(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                       double s)
Returns the difference between a vector and a scalar.

Parameters:
x - the vector
s - the scalar
Returns:
x - s

multiply

public static edu.wlu.cs.levy.SNARLI.SNVector multiply(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                       double s)
Returns the product of a vector and a scalar.

Parameters:
x - the vector
s - the scalar
Returns:
x * s

divide

public static edu.wlu.cs.levy.SNARLI.SNVector divide(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                     double s)
Returns the quotient of a vector and a scalar.

Parameters:
x - the vector
s - the scalar
Returns:
x / s

add

public void add(edu.wlu.cs.levy.SNARLI.SNVector x)
         throws java.lang.RuntimeException
Adds a vector to this vector.

Parameters:
x - the other vector
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

subtract

public void subtract(edu.wlu.cs.levy.SNARLI.SNVector x)
              throws java.lang.RuntimeException
Subtracts a vector from this vector.

Parameters:
x - the other vector
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

multiply

public void multiply(edu.wlu.cs.levy.SNARLI.SNVector x)
              throws java.lang.RuntimeException
Multiplies the elements of this vector by those of another vector.

Parameters:
x - the other vector
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

add

public static edu.wlu.cs.levy.SNARLI.SNVector add(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                  edu.wlu.cs.levy.SNARLI.SNVector y)
                                           throws java.lang.RuntimeException
Returns the sum of two vectors.

Parameters:
x - one vector
y - the other vector
Returns:
x + y
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

subtract

public static edu.wlu.cs.levy.SNARLI.SNVector subtract(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                       edu.wlu.cs.levy.SNARLI.SNVector y)
                                                throws java.lang.RuntimeException
Returns the difference between two vectors.

Parameters:
x - one vector
y - the other vector
Returns:
x - y
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

arrayMultiply

public static edu.wlu.cs.levy.SNARLI.SNVector arrayMultiply(edu.wlu.cs.levy.SNARLI.SNVector x,
                                                            edu.wlu.cs.levy.SNARLI.SNVector y)
                                                     throws java.lang.RuntimeException
Returns a vector containing the products of the elements of two vectors.

Parameters:
x - one vector
y - the other vector
Returns:
x .* y
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

arrayMultiply

public void arrayMultiply(edu.wlu.cs.levy.SNARLI.SNVector x)
                   throws java.lang.RuntimeException
Multiplies the elements of this vector by those of another.

Parameters:
x - the other vector
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

getValues

public double[] getValues()
Returns a 1D array of the values contained in this vector.

Returns:
values

sum

public double sum()
Returns the sum of the elements of this vector.

Returns:
sum

min

public double min()
Returns the value of the smallest element of this vector.

Returns:
minimum value

eucDist

public double eucDist(edu.wlu.cs.levy.SNARLI.SNVector x)
               throws java.lang.RuntimeException
Returns the Euclidean distance between this vector and another.

Parameters:
x - the other vector
Returns:
dist(this, x)
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

repmat

public edu.wlu.cs.levy.SNARLI.SNMatrix 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 edu.wlu.cs.levy.SNARLI.SNVector sort()
Returns a copy of this vector, sorted in ascending order.

Returns:
sorted copy

find

public int[] find(double s)
Returns indices where vector equals scalar argument. Index of first value in any vector is 0.

Returns:
indices where vector equals scalar

nycDist

public double nycDist(edu.wlu.cs.levy.SNARLI.SNVector x)
               throws java.lang.RuntimeException
Returns the Manhattan distance (L1 norm of differences) between this vector and another.

Parameters:
x - the other vector
Returns:
Manhattan distance between this and anotherVector
Throws:
java.lang.RuntimeException - if Vectors have unequal lengths

gaussianNoise

public static edu.wlu.cs.levy.SNARLI.SNVector gaussianNoise(int n,
                                                            java.util.Random r)
Returns a vector of normally distributed random values. Values are taken from a Gaussian distribution with zero mean and standard deviation one.

Returns:
vector of normally distributed random values

outer

public edu.wlu.cs.levy.SNARLI.SNMatrix outer(edu.wlu.cs.levy.SNARLI.SNVector x)
Returns the outer product of this vector and another.

Parameters:
x - the other vector
Returns:
this^T X x
Throws:
java.lang.RuntimeException - if the other vector is null

copy

public edu.wlu.cs.levy.SNARLI.SNVector copy()
Returns a deep copy of this vector.

Returns:
vector copy

setAll

public void setAll(double s)
Sets all entries to a scalar.

Parameters:
s - the scalar

setValue

public void setValue(int i,
                     double s)
              throws java.lang.RuntimeException
Sets an entry to a scalar at a certain index.

Parameters:
i - index
s - scalar
Throws:
java.lang.RuntimeException - if the index is invalide

ne

public edu.wlu.cs.levy.SNARLI.SNVector ne(double s)
Returns a vector containing ones where this vector's elements are not equal to a scalar, and zeros elsewhere.

Parameters:
s - the scalar
Returns:
vector of ones and zeros

ge

public edu.wlu.cs.levy.SNARLI.SNVector ge(double s)
Returns a vector containing ones where this vector's elements are greater than or equal to a scalar, and zeros elsewhere.

Parameters:
s - the scalar
Returns:
vector of ones and zeros

le

public edu.wlu.cs.levy.SNARLI.SNVector le(double s)
Returns a vector containing ones where this vector's elements are less than or equal to a scalar, and zeros elsewhere.

Parameters:
s - the scalar
Returns:
vector of ones and zeros

max

public double max()
Returns the largest value of any element in this vector.

Returns:
the largest value

and

public edu.wlu.cs.levy.SNARLI.SNVector and(edu.wlu.cs.levy.SNARLI.SNVector x)
                                    throws java.lang.RuntimeException
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:
x - the other vector
Returns:
vector of 1's and 0's
Throws:
java.lang.RuntimeException - if the vectors have unequal lengths

multiply

public edu.wlu.cs.levy.SNARLI.SNVector multiply(edu.wlu.cs.levy.SNARLI.SNMatrix a)
                                         throws java.lang.RuntimeException
Returns the product of this Vector and a Matrix.

Parameters:
a - the matrix
Returns:
this * a
Throws:
InvalidOperationException - if the matrix is null
InvalidOperationException - if the inner dimensions mismatch
java.lang.RuntimeException

toString

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

Overrides:
toString in class java.lang.Object
Returns:
string representation