ch.akuhn.matrix

## Class SparseVector

• ```public class SparseVector
extends Vector```
A sparse vector
Author:

• ### Nested classes/interfaces inherited from class ch.akuhn.matrix.Vector

`Vector.Entry`
• ### Constructor Summary

Constructors
Modifier Constructor and Description
`protected ` `SparseVector(double[] values)`
`protected ` `SparseVector(int size)`
` ` ```SparseVector(int size, int capacity)```
Construct with the given length and capacity
• ### Method Summary

All Methods
Modifier and Type Method and Description
`double` ```add(int key, double value)```
Add the given value to the value at the given index
`double` `dot(Vector x)`
Returns the dot/scalar product.
`Iterable<Vector.Entry>` `entries()`
Iterates over all entries.
`boolean` `equals(Object obj)`
`boolean` `equals(SparseVector v)`
Test for equality
`boolean` ```equals(Vector v, double epsilon)```
Test for equality
`double` `get(int key)`
Get the value at the given index
`int` `hashCode()`
`boolean` `isUsed(int key)`
Test if an index has a set value
`int[]` `keys()`
`double` ```put(int key, double value)```
Set the value at an index
`void` `resizeTo(int newSize)`
Resize the vector
`void` ```scaleAndAddTo(double a, Vector y)```
y = y + a*`this`.
`int` `size()`
`Vector` `times(double scalar)`
Multiply by a constant, creating a new vector
`Vector` `timesEquals(double scalar)`
Multiply by a constant in-place
`void` `trim()`
Trim the underlying dense arrays to compact space and save memory
`int` `used()`
Returns number of non-zero-valued entries.
`double[]` `values()`
• ### Methods inherited from class ch.akuhn.matrix.Vector

`applyCentering, copy, dense, density, from, mean, norm, sparse, storeOn, sum, toString, unwrap, wrap`
• ### Methods inherited from class java.lang.Object

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

• #### SparseVector

`protected SparseVector(double[] values)`
• #### SparseVector

`protected SparseVector(int size)`
• #### SparseVector

```public SparseVector(int size,
int capacity)```
Construct with the given length and capacity
Parameters:
`size` - the length of the vector
`capacity` - the number of expected non-zero elements
• ### Method Detail

```public double add(int key,
double value)```
Description copied from class: `Vector`
Add the given value to the value at the given index
Overrides:
`add` in class `Vector`
Returns:
the new value
• #### entries

`public Iterable<Vector.Entry> entries()`
Description copied from class: `Vector`
Iterates over all entries. Some vectors omit zero-valued entries.
Overrides:
`entries` in class `Vector`
Returns:
value and index of each entry.
• #### equals

`public boolean equals(Object obj)`
Overrides:
`equals` in class `Object`
• #### equals

`public boolean equals(SparseVector v)`
Test for equality
Parameters:
`v` -
Returns:
true if equal; false otherwise
• #### get

`public double get(int key)`
Description copied from class: `Vector`
Get the value at the given index
Specified by:
`get` in class `Vector`
Returns:
the value
• #### hashCode

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

`public boolean isUsed(int key)`
Test if an index has a set value
Parameters:
`key` - the index
Returns:
true if index has an associated value
• #### put

```public double put(int key,
double value)```
Description copied from class: `Vector`
Set the value at an index
Specified by:
`put` in class `Vector`
Returns:
the new value
• #### resizeTo

`public void resizeTo(int newSize)`
Resize the vector
Parameters:
`newSize` - new size
• #### size

`public int size()`
Specified by:
`size` in class `Vector`
Returns:
the length of the vector
• #### used

`public int used()`
Description copied from class: `Vector`
Returns number of non-zero-valued entries.
Overrides:
`used` in class `Vector`
Returns:
a positive integer.
• #### trim

`public void trim()`
Trim the underlying dense arrays to compact space and save memory
• #### dot

`public double dot(Vector x)`
Description copied from class: `Vector`
Returns the dot/scalar product.
Overrides:
`dot` in class `Vector`
Returns:
the dot product

```public void scaleAndAddTo(double a,
Vector y)```
Description copied from class: `Vector`
y = y + a*`this`.
Overrides:
`scaleAndAddTo` in class `Vector`
• #### equals

```public boolean equals(Vector v,
double epsilon)```
Description copied from class: `Vector`
Test for equality
Specified by:
`equals` in class `Vector`
Returns:
true if the same; false otherwise
• #### times

`public Vector times(double scalar)`
Description copied from class: `Vector`
Multiply by a constant, creating a new vector
Specified by:
`times` in class `Vector`
Returns:
the vector
• #### timesEquals

`public Vector timesEquals(double scalar)`
Description copied from class: `Vector`
Multiply by a constant in-place
Specified by:
`timesEquals` in class `Vector`
Returns:
this
• #### keys

`public int[] keys()`
Returns:
the current keys
• #### values

`public double[] values()`
Returns:
the current values