public class SparseVector extends Object implements Vector, SparseStructure, Iterable<SparseVector.Entry>
Modifier and Type | Class and Description |
---|---|
static class |
SparseVector.Entry
This is an entry in a
SparseVector . |
static class |
SparseVector.Iterator
This wrapper class overrides the
Iterator.remove()
method to throw an exception when called. |
Constructor and Description |
---|
SparseVector(double... v)
Constructs a sparse vector from a
double[] . |
SparseVector(int size)
Constructs a sparse vector.
|
SparseVector(int size,
int[] indices,
double[] values)
Constructs a sparse vector.
|
SparseVector(int size,
List<SparseVector.Entry> entries)
Constructs a sparse vector.
|
SparseVector(SparseVector that)
Copy constructor.
|
SparseVector(Vector v)
Constructs a sparse vector from a vector.
|
Modifier and Type | Method and Description |
---|---|
Vector |
add(double c)
Add a constant to all entries in this vector.
|
SparseVector |
add(SparseVector that) |
Vector |
add(Vector that)
\(this + that\)
|
double |
angle(Vector that)
Measure the angle, \(\theta\), between
this and that . |
SparseVector |
deepCopy()
The implementation returns an instance created from
this by the copy
constructor of the class, or just this if the instance itself is
immutable. |
Vector |
divide(Vector that)
Divide
this by that , entry-by-entry. |
boolean |
equals(Object obj) |
double |
get(int index)
Get the value at position i.
|
int |
hashCode() |
double |
innerProduct(SparseVector that) |
double |
innerProduct(Vector that)
Inner product in the Euclidean space is the dot product.
|
Iterator<SparseVector.Entry> |
iterator() |
Vector |
leftMultiply(Matrix A)
Left multiplies a matrix.
|
Vector |
minus(double c)
Subtract a constant from all entries in this vector.
|
SparseVector |
minus(SparseVector that) |
Vector |
minus(Vector that)
\(this - that\)
|
SparseVector |
multiply(SparseVector that) |
SparseVector |
multiply(Vector that)
Multiply
this by that , entry-by-entry. |
int |
nNonZeros()
Get the number of non-zero entries in the structure.
|
double |
norm()
Compute the length or magnitude or Euclidean norm of a vector, namely, \(\|v\|\).
|
double |
norm(double p)
Gets the \(L^p\)-norm \(\|v\|_p\) of this vector.
|
SparseVector |
opposite()
Get the opposite of this vector.
|
Vector |
pow(double c)
Take the exponentiation of all entries in this vector, entry-by-entry.
|
SparseVector |
scaled(double c)
Scale this vector by a constant, entry-by-entry.
|
SparseVector |
scaled(Real c)
Scale this vector by a constant, entry-by-entry.
|
void |
set(int index,
double value)
Change the value of an entry in this vector.
|
int |
size()
Get the length of this vector.
|
double[] |
toArray()
Cast this vector into a 1D
double[] . |
String |
toString() |
SparseVector |
ZERO()
Get a 0-vector that has the same length as this vector.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public SparseVector(int size)
size
- the size of the vectorpublic SparseVector(int size, int[] indices, double[] values)
size
- the size of the vectorindices
- the indices of the non-zero valuesvalues
- the non-zero valuespublic SparseVector(int size, List<SparseVector.Entry> entries)
size
- the size of the vectorentries
- a list of sparse vector entriespublic SparseVector(double... v)
double[]
.v
- a vectorpublic SparseVector(Vector v)
v
- a vectorpublic SparseVector(SparseVector that)
that
- the vector to be copiedpublic int size()
Vector
public double get(int index)
Vector
public void set(int index, double value)
Vector
public Iterator<SparseVector.Entry> iterator()
iterator
in interface Iterable<SparseVector.Entry>
public Vector add(Vector that)
Vector
add
in interface Vector
add
in interface AbelianGroup<Vector>
that
- a vectorpublic SparseVector add(SparseVector that)
public Vector minus(Vector that)
Vector
minus
in interface Vector
minus
in interface AbelianGroup<Vector>
that
- a vectorpublic SparseVector minus(SparseVector that)
public SparseVector multiply(Vector that)
Vector
this
by that
, entry-by-entry.public SparseVector multiply(SparseVector that)
public Vector leftMultiply(Matrix A)
A
- the matrix on the leftpublic Vector divide(Vector that)
Vector
this
by that
, entry-by-entry.public Vector add(double c)
Vector
public Vector minus(double c)
Vector
public double innerProduct(Vector that)
Vector
innerProduct
in interface Vector
innerProduct
in interface HilbertSpace<Vector,Real>
that
- a vectorpublic double innerProduct(SparseVector that)
public Vector pow(double c)
Vector
public SparseVector scaled(double c)
Vector
vector.scaled(1. / vector.norm())
public SparseVector scaled(Real c)
Vector
vector.scaled(1. / vector.norm())
scaled
in interface Vector
scaled
in interface VectorSpace<Vector,Real>
c
- a constantpublic SparseVector opposite()
Vector
opposite
in interface Vector
opposite
in interface AbelianGroup<Vector>
public double norm()
Vector
norm
in interface Vector
norm
in interface BanachSpace<Vector,Real>
public double norm(double p)
Vector
Double.POSITIVE_INFINITY
), \(\|v\|_p =
\max|v_i|\).Double.NEGATIVE_INFINITY
), \(\|v\|_p =
\min|v_i|\).norm
in interface Vector
p
- p ≥ 1, or Double.POSITIVE_INFINITY
or
Double.NEGATIVE_INFINITY
public double angle(Vector that)
Vector
this
and that
.
That is,
\[
this \cdot that = \|this\| \times \|that\| \times \cos \theta
\]public SparseVector ZERO()
Vector
ZERO
in interface Vector
ZERO
in interface AbelianGroup<Vector>
public double[] toArray()
Vector
double[]
.public SparseVector deepCopy()
DeepCopyable
this
by the copy
constructor of the class, or just this
if the instance itself is
immutable.deepCopy
in interface Vector
deepCopy
in interface DeepCopyable
public int nNonZeros()
SparseStructure
nNonZeros
in interface SparseStructure
Copyright © 2010-2020 NM FinTech Ltd.. All Rights Reserved.