scpsolver.util
Class SparseVector

java.lang.Object
  extended by scpsolver.util.SparseVector
All Implemented Interfaces:
Matrix

public class SparseVector
extends java.lang.Object
implements Matrix

Implementation of a sparse vector

Author:
planatsc

Constructor Summary
SparseVector(double[] x)
          Creates a new vector with entries taken from the array
SparseVector(int[] index, double[] data)
           
SparseVector(int size, int nonzerosize)
          Constructs a new SparseVector of a given size and with a given starting capacity.
SparseVector(Matrix c)
          Copies the matrix c.
SparseVector(SparseVector v)
          Copy-constructor for the SparseVector
 
Method Summary
 SparseVector add(SparseVector v)
          Adds two sparse vectors
 SparseVector clone()
          Creates a deep copy of the vector
 double dot(double[] ydata)
          Calculates the dot product of this vector and the vector y
 double dot(SparseVector y)
          Calculates the dot product (or scalar product, inner product) of two vectors
 double dotdumb(SparseVector y)
          Calculates the dot product of this vector and the vector y
 boolean equals(java.lang.Object o)
          Compares two vectors.
 boolean equals(SparseVector v)
          Compares two SparseVectors
 double[] get()
          Returns the vector as a double array
 double get(int index)
          Gets the value at index inpired by http://cvs.berlios.de/cgi-bin/viewcvs.cgi/ressim/mtj/src/no/uib/cipr/matrix/sparse/SparseVector.java?
 double get(int row, int column)
          Gets the value of the vector at the position donated by row and column.
 int getColNum()
          Returns the number of columns of the vector.
 double[] getData()
           
 int[] getIndex()
           
 int getIndex(int ind)
          Sets the index of a sparse vector inpired by http://cvs.berlios.de/cgi-bin/viewcvs.cgi/ressim/mtj/src/no/uib/cipr/matrix/sparse/SparseVector.java?
 NonZeroElementIterator getNonZeroElementIterator()
           
 int getRowNum()
          Returns the number of rows of the vector.
 int getSize()
           
 int getUsed()
           
 void grow(int newsize, boolean bottom)
          Grows or shrinks the vector to the given size
 void grow(SparseVector v)
          Grows a vector with another vector
static void main(java.lang.String[] args)
           
 SparseVector plus(Matrix y)
          Adds a second vector to the vector
 void set(int in, double value)
          Sets the entry of the vector to a given value
 void set(int row, int column, double value)
          Sets a certain entry of the vector to the given value.
 void setData(double[] data)
           
 void setIndex(int[] index)
           
 void setSize(int size)
           
 void setUsed(int used)
           
 SparseVector times(double c)
          Multiplies the vector with a scalar
 Matrix times(Matrix multiplier)
          Multiplies the vector with a matrix
 java.lang.String toString()
          String representation of this sparse vector.
 Matrix transpose()
          Transposes a vector
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SparseVector

public SparseVector(int size,
                    int nonzerosize)
Constructs a new SparseVector of a given size and with a given starting capacity.

Parameters:
size - The actual size of the vector
nonzerosize - The starting capacity of the vector

SparseVector

public SparseVector(SparseVector v)
Copy-constructor for the SparseVector

Parameters:
v - The vector to be copied

SparseVector

public SparseVector(double[] x)
Creates a new vector with entries taken from the array

Parameters:
x - The entries of the new vector

SparseVector

public SparseVector(int[] index,
                    double[] data)

SparseVector

public SparseVector(Matrix c)
Copies the matrix c. Attention: this may only work if the matrix is a nx1 or a 1xn matrix.

Parameters:
c - The matrix to be copied
Method Detail

getData

public double[] getData()

setData

public void setData(double[] data)

getIndex

public int[] getIndex()

setIndex

public void setIndex(int[] index)

getUsed

public int getUsed()

setUsed

public void setUsed(int used)

getSize

public int getSize()

setSize

public void setSize(int size)

grow

public void grow(int newsize,
                 boolean bottom)
Grows or shrinks the vector to the given size

Parameters:
newsize - The new size of the vector
bottom - Boolean indicating wether the elements are added on the top or bottom

grow

public void grow(SparseVector v)
Grows a vector with another vector

Parameters:
v - The vector to be concatinated
bottom - Wether v is added before or after the vector

transpose

public Matrix transpose()
Transposes a vector

Specified by:
transpose in interface Matrix
Returns:
Returns a new transposed vector

clone

public SparseVector clone()
Creates a deep copy of the vector

Overrides:
clone in class java.lang.Object
Returns:
A deep copy of the vector

toString

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

Overrides:
toString in class java.lang.Object

getIndex

public int getIndex(int ind)
Sets the index of a sparse vector inpired by http://cvs.berlios.de/cgi-bin/viewcvs.cgi/ressim/mtj/src/no/uib/cipr/matrix/sparse/SparseVector.java?rev=HEAD&content-type=text/vnd.viewcvs-markup ;)

Parameters:
ind -
val -
Returns:

set

public void set(int in,
                double value)
Sets the entry of the vector to a given value

Parameters:
in - The entry of the vector to be altered
value - The new value

get

public double get(int index)
Gets the value at index inpired by http://cvs.berlios.de/cgi-bin/viewcvs.cgi/ressim/mtj/src/no/uib/cipr/matrix/sparse/SparseVector.java?rev=HEAD&content-type=text/vnd.viewcvs-markup ;)

Parameters:
index -
Returns:

get

public double[] get()
Returns the vector as a double array

Returns:
The representation of the vector as array

dot

public double dot(SparseVector y)
Calculates the dot product (or scalar product, inner product) of two vectors

Parameters:
y - The second vector for the dot product
Returns:
The dot product of this and the vector y

plus

public SparseVector plus(Matrix y)
Adds a second vector to the vector

Specified by:
plus in interface Matrix
Parameters:
y - The second vector
Returns:
this+y

dotdumb

public double dotdumb(SparseVector y)
Calculates the dot product of this vector and the vector y

Parameters:
y - The second vector for the dot product
Returns:
The dot product of this and the second vector

dot

public double dot(double[] ydata)
Calculates the dot product of this vector and the vector y

Parameters:
ydata - The second vector implicitly given as double array
Returns:
The dot product of this and y

times

public SparseVector times(double c)
Multiplies the vector with a scalar

Parameters:
c - The scalar to be multiplied with the vector
Returns:
Returns the vector multiplied with the scalar

get

public double get(int row,
                  int column)
Gets the value of the vector at the position donated by row and column. Note: if the vector is not a linevector, column must be equal to zero, otherwise an exception will be thrown. If the vector is a linevector on the other hand, the row must be equal to zero likewise.

Specified by:
get in interface Matrix
Parameters:
row - The row of the entry
column - The column of the entry
Returns:
The value at the entry

getColNum

public int getColNum()
Returns the number of columns of the vector. Note: if this vector is not a linevector the value is, of course, always 1.

Specified by:
getColNum in interface Matrix
Returns:
The number of columns

getRowNum

public int getRowNum()
Returns the number of rows of the vector. Note: if this vector is a linevector the value is, of course, always 1.

Specified by:
getRowNum in interface Matrix
Returns:
The number of rows of the vector

times

public Matrix times(Matrix multiplier)
Multiplies the vector with a matrix

Specified by:
times in interface Matrix
Parameters:
multiplier - The matrix with which the vector should be multiplied
Returns:
this*multiplier

set

public void set(int row,
                int column,
                double value)
Sets a certain entry of the vector to the given value.

Specified by:
set in interface Matrix

add

public SparseVector add(SparseVector v)
Adds two sparse vectors

Parameters:
v - The vector to be added
Returns:
The sum of the two vectors

equals

public boolean equals(java.lang.Object o)
Compares two vectors. Notice: if the Object o isn't a probable SparseVector, it can't be compared yet.

Overrides:
equals in class java.lang.Object
Parameters:
v - The vector to be compared to
Returns:
True, if the second vector provides the same values, false otherwise. Note that the vectors don't need to be identical in the data-arrays, so it's okay when one vector explicitly safes a zero argument and the other doesn't.

equals

public boolean equals(SparseVector v)
Compares two SparseVectors

Parameters:
v - The vector to be compared to
Returns:
True, if the second vector provides the same values, false otherwise. Note that the vectors don't need to be identical in the data-arrays, so it's okay when one vector explicitly safes a zero argument and the other doesn't.

main

public static void main(java.lang.String[] args)

getNonZeroElementIterator

public NonZeroElementIterator getNonZeroElementIterator()
Specified by:
getNonZeroElementIterator in interface Matrix