Class SparseVector

• All Implemented Interfaces:
Serializable

```public class SparseVector
extends Vector```
A sparse vector represented by an indices array and a values array.
Serialized Form
• Constructor Summary

Constructors
Constructor and Description
`SparseVector()`
Construct an empty sparse vector with undetermined size.
`SparseVector(int n)`
Construct an empty sparse vector with determined size.
```SparseVector(int n, int[] indices, double[] values)```
Construct a sparse vector with the given indices and values.
```SparseVector(int n, Map<Integer,Double> kv)```
Construct a sparse vector with given indices to values map.
• Method Summary

All Methods
Modifier and Type Method and Description
`void` ```add(int i, double val)```
Add the i-th element of the vector by value "val".
`SparseVector` `append(double d)`
Create a new vector by adding an element to the end of the vector.
`SparseVector` `clone()`
`double` `dot(Vector other)`
Compute the dot product with another vector.
`boolean` `equals(Object o)`
`double` `get(int i)`
Get the i-th element of the vector.
`int[]` `getIndices()`
Get the indices array.
`double[]` `getValues()`
Get the values array.
`int` `hashCode()`
`VectorIterator` `iterator()`
Get the iterator of the vector.
`Vector` `minus(Vector vec)`
Create a new vector by subtracting another vector.
`void` `normalizeEqual(double p)`
Normalize the vector.
`double` `normInf()`
Return the Inf norm of the vector.
`double` `normL1()`
Return the L1 norm of the vector.
`double` `normL2()`
Return the L2 norm of the vector.
`double` `normL2Square()`
Return the square of L2 norm of the vector.
`int` `numberOfValues()`
Get number of values in this vector.
`DenseMatrix` `outer()`
Compute the outer product with itself.
`DenseMatrix` `outer(SparseVector other)`
Compute the outer product with another vector.
`Vector` `plus(Vector vec)`
Create a new vector by plussing another vector.
`SparseVector` `prefix(double d)`
Create a new vector by adding an element to the head of the vector.
`void` `removeZeroValues()`
Remove all zero values away from this vector.
`SparseVector` `scale(double d)`
Scale the vector by value "v" and create a new vector to store the result.
`void` `scaleEqual(double d)`
Scale the vector by value "v".
`void` ```set(int i, double val)```
Set the i-th element of the vector to value "val".
`void` `setSize(int n)`
Set the size of the vector.
`int` `size()`
Get the size of the vector.
`SparseVector` `slice(int[] indices)`
Slice the vector.
`void` ```standardizeEqual(double mean, double stdvar)```
Standardize the vector.
`DenseVector` `toDenseVector()`
Convert to a dense vector.
`String` `toString()`
• Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`
• Constructor Detail

• SparseVector

`public SparseVector()`
Construct an empty sparse vector with undetermined size.
• SparseVector

`public SparseVector(int n)`
Construct an empty sparse vector with determined size.
• SparseVector

```public SparseVector(int n,
int[] indices,
double[] values)```
Construct a sparse vector with the given indices and values.
Throws:
`IllegalArgumentException` - If size of indices array and values array differ.
`IllegalArgumentException` - If n >= 0 and the indices are out of bound.
• SparseVector

```public SparseVector(int n,
Map<Integer,Double> kv)```
Construct a sparse vector with given indices to values map.
Throws:
`IllegalArgumentException` - If n >= 0 and the indices are out of bound.
• Method Detail

• clone

`public SparseVector clone()`
Overrides:
`clone` in class `Object`
• prefix

`public SparseVector prefix(double d)`
Description copied from class: `Vector`
Create a new vector by adding an element to the head of the vector.
Specified by:
`prefix` in class `Vector`
• append

`public SparseVector append(double d)`
Description copied from class: `Vector`
Create a new vector by adding an element to the end of the vector.
Specified by:
`append` in class `Vector`
• getIndices

`public int[] getIndices()`
Get the indices array.
• getValues

`public double[] getValues()`
Get the values array.
• size

`public int size()`
Description copied from class: `Vector`
Get the size of the vector.
Specified by:
`size` in class `Vector`
• get

`public double get(int i)`
Description copied from class: `Vector`
Get the i-th element of the vector.
Specified by:
`get` in class `Vector`
• setSize

`public void setSize(int n)`
Set the size of the vector.
• numberOfValues

`public int numberOfValues()`
Get number of values in this vector.
• set

```public void set(int i,
double val)```
Description copied from class: `Vector`
Set the i-th element of the vector to value "val".
Specified by:
`set` in class `Vector`

```public void add(int i,
double val)```
Description copied from class: `Vector`
Add the i-th element of the vector by value "val".
Specified by:
`add` in class `Vector`
• toString

`public String toString()`
Overrides:
`toString` in class `Object`
• normL2

`public double normL2()`
Description copied from class: `Vector`
Return the L2 norm of the vector.
Specified by:
`normL2` in class `Vector`
• normL1

`public double normL1()`
Description copied from class: `Vector`
Return the L1 norm of the vector.
Specified by:
`normL1` in class `Vector`
• normInf

`public double normInf()`
Description copied from class: `Vector`
Return the Inf norm of the vector.
Specified by:
`normInf` in class `Vector`
• normL2Square

`public double normL2Square()`
Description copied from class: `Vector`
Return the square of L2 norm of the vector.
Specified by:
`normL2Square` in class `Vector`
• slice

`public SparseVector slice(int[] indices)`
Description copied from class: `Vector`
Slice the vector.
Specified by:
`slice` in class `Vector`
• plus

`public Vector plus(Vector vec)`
Description copied from class: `Vector`
Create a new vector by plussing another vector.
Specified by:
`plus` in class `Vector`
• minus

`public Vector minus(Vector vec)`
Description copied from class: `Vector`
Create a new vector by subtracting another vector.
Specified by:
`minus` in class `Vector`
• scale

`public SparseVector scale(double d)`
Description copied from class: `Vector`
Scale the vector by value "v" and create a new vector to store the result.
Specified by:
`scale` in class `Vector`
• scaleEqual

`public void scaleEqual(double d)`
Description copied from class: `Vector`
Scale the vector by value "v".
Specified by:
`scaleEqual` in class `Vector`
• removeZeroValues

`public void removeZeroValues()`
Remove all zero values away from this vector.
• dot

`public double dot(Vector other)`
Description copied from class: `Vector`
Compute the dot product with another vector.
Specified by:
`dot` in class `Vector`
• outer

`public DenseMatrix outer()`
Description copied from class: `Vector`
Compute the outer product with itself.
Specified by:
`outer` in class `Vector`
Returns:
The outer product matrix.
• outer

`public DenseMatrix outer(SparseVector other)`
Compute the outer product with another vector.
Returns:
The outer product matrix.
• toDenseVector

`public DenseVector toDenseVector()`
Convert to a dense vector.
• standardizeEqual

```public void standardizeEqual(double mean,
double stdvar)```
Description copied from class: `Vector`
Standardize the vector.
Specified by:
`standardizeEqual` in class `Vector`
• normalizeEqual

`public void normalizeEqual(double p)`
Description copied from class: `Vector`
Normalize the vector.
Specified by:
`normalizeEqual` in class `Vector`
• equals

`public boolean equals(Object o)`
Overrides:
`equals` in class `Object`
• hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`
• iterator

`public VectorIterator iterator()`
Description copied from class: `Vector`
Get the iterator of the vector.
Specified by:
`iterator` in class `Vector`