Class SparseVector
Class defining a sparse vector.
Inherited Members
Namespace: BRIDGES.LinearAlgebra.Vectors
Assembly: BRIDGES.dll
Syntax
public sealed class SparseVector : Vector, IDotProduct<Vector, double>, INorm<Vector>, IMetric<Vector>, IGroupAction<Vector, double>, IDotProduct<SparseVector, double>, INorm<SparseVector>, IMetric<SparseVector>, IGroupAction<SparseVector, double>
Constructors
| Improve this Doc View SourceSparseVector(SparseVector)
Initialises a new instance of the SparseVector class from another SparseVector.
Declaration
public SparseVector(SparseVector other)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | other | SparseVector to deep copy. |
SparseVector(Int32, IEnumerable<Int32>, IEnumerable<Double>)
Initialises a new instance of the SparseVector class of given size, with given values.
Declaration
public SparseVector(int size, IEnumerable<int> rowIndices, IEnumerable<double> values)
Parameters
Type | Name | Description |
---|---|---|
Int32 | size | Number of components of the SparseVector. |
IEnumerable<Int32> | rowIndices | Row indices of the non-zero values of the SparseVector. |
IEnumerable<Double> | values | Non-zero values of the SparseVector. |
SparseVector(Int32)
Initialises a new instance of the SparseVector class of given size, containing only zeros.
Declaration
public SparseVector(int size)
Parameters
Type | Name | Description |
---|---|---|
Int32 | size | Number of components of the SparseVector. |
Properties
| Improve this Doc View SourceItem[Int32]
Gets or sets the component at a given index.
Declaration
public override double this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | Index of the component to get or set. |
Property Value
Type | Description |
---|---|
Double | The value of the component. |
Overrides
| Improve this Doc View SourceNonZerosCount
Gets the number of non-zero values in the current sparse vector.
Declaration
public int NonZerosCount { get; }
Property Value
Type | Description |
---|---|
Int32 |
Size
Number of component of this vector.
Declaration
public override int Size { get; }
Property Value
Type | Description |
---|---|
Int32 |
Overrides
Methods
| Improve this Doc View SourceAdd(SparseVector, DenseVector)
Computes the right addition of a SparseVector with a DenseVector.
Declaration
public static DenseVector Add(SparseVector left, DenseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector for the addition. |
DenseVector | right | Right DenseVector for the addition. |
Returns
Type | Description |
---|---|
DenseVector | The new DenseVector resulting from the addition. |
Add(SparseVector, SparseVector)
Computes the addition of two SparseVector.
Declaration
public static SparseVector Add(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector for the addition. |
SparseVector | right | Right SparseVector for the addition. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the addition. |
Add(SparseVector, Vector)
Computes the right addition of a DenseVector with a Vector.
Declaration
public static Vector Add(SparseVector left, Vector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left DenseVector for the addition. |
Vector | right | Right Vector for the addition. |
Returns
Type | Description |
---|---|
Vector | The new DenseVector resulting from the addition. |
Divide(SparseVector, Double)
Computes the scalar division of a SparseVector with a
Declaration
public static SparseVector Divide(SparseVector operand, double divisor)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | operand | SparseVector to divide. |
Double | divisor |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the scalar division. |
Equals(SparseVector)
Class defining a sparse vector.
Declaration
public bool Equals(SparseVector other)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | other |
Returns
Type | Description |
---|---|
Boolean |
Equals(Vector)
Class defining a sparse vector.
Declaration
public override bool Equals(Vector other)
Parameters
Type | Name | Description |
---|---|---|
Vector | other |
Returns
Type | Description |
---|---|
Boolean |
Overrides
| Improve this Doc View SourceEquals(Object)
Class defining a sparse vector.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj |
Returns
Type | Description |
---|---|
Boolean |
Overrides
| Improve this Doc View SourceGetEnumerator()
Class defining a sparse vector.
Declaration
public override IEnumerator<double> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<Double> |
Overrides
Remarks
This method iterates over both zero and non-zero values. To iterate only on non-zero values use NonZeros().
GetHashCode()
Class defining a sparse vector.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
Overrides
| Improve this Doc View SourceGramSchmidt(IEnumerable<SparseVector>)
Ortho-normalise the set of DenseVector using a Gram-Schimdt process.
Declaration
public static SparseVector[] GramSchmidt(IEnumerable<SparseVector> vectors)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<SparseVector> | vectors | Set of DenseVector to operate on. |
Returns
Type | Description |
---|---|
SparseVector[] | The ortho-normal set of DenseVector. |
Remarks
If the vectors are not linearly independent the number of vectors will change.
Multiply(SparseVector, Double)
Computes the right scalar multiplication of a SparseVector with a
Declaration
public static SparseVector Multiply(SparseVector operand, double factor)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | operand | SparseVector to multiply on the right. |
Double | factor |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the right scalar multiplication. |
Multiply(Double, SparseVector)
Computes the left scalar multiplication of a SparseVector with a
Declaration
public static SparseVector Multiply(double factor, SparseVector operand)
Parameters
Type | Name | Description |
---|---|---|
Double | factor | |
SparseVector | operand | SparseVector to multiply on the left. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the left scalar multiplication. |
NonZeros()
Returns an enumerator which reads through the non-zero components of the current SparseVector.
The
Declaration
public IEnumerable<(int rowIndex, double value)> NonZeros()
Returns
Type | Description |
---|---|
IEnumerable<(, )<Int32, Double>> | The enumerator of the SparseVector. |
Opposite(SparseVector)
Computes the opposite of the SparseVector.
Declaration
public static SparseVector Opposite(SparseVector operand)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | operand | SparseVector from which the opposite is computed. |
Returns
Type | Description |
---|---|
SparseVector | The SparseVector, opposite of the initial one. |
StandardVector(Int32, Int32)
Returns the unit SparseVector of a given size
, with one at the given row index
and zeros elsewhere.
Declaration
public static SparseVector StandardVector(int size, int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | size | Size of the SparseVector. |
Int32 | index | Index of the standard vector, i.e of the component equal to one. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector representing the standard vector. |
Subtract(SparseVector, DenseVector)
Computes the right subtraction of a SparseVector with a DenseVector.
Declaration
public static DenseVector Subtract(SparseVector left, DenseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to subtract. |
DenseVector | right | Right DenseVector to subtract with. |
Returns
Type | Description |
---|---|
DenseVector | The new DenseVector resulting from the subtraction. |
Subtract(SparseVector, SparseVector)
Computes the subtraction of two SparseVector.
Declaration
public static SparseVector Subtract(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to subtract. |
SparseVector | right | Right SparseVector to subtract with. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the subtraction. |
Subtract(SparseVector, Vector)
Computes the right subtraction of a DenseVector with a Vector.
Declaration
public static Vector Subtract(SparseVector left, Vector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left DenseVector to subtract. |
Vector | right | Right Vector to subtract with. |
Returns
Type | Description |
---|---|
Vector | The new DenseVector resulting from the subtraction. |
ToArray()
Translates this vector into its array representation.
Declaration
public override double[] ToArray()
Returns
Type | Description |
---|---|
Double[] | The array representing the vector. |
Overrides
| Improve this Doc View SourceToString()
Class defining a sparse vector.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String |
Overrides
| Improve this Doc View SourceTransposeMultiply(SparseVector, DenseVector)
Computes the multiplication between the transposed left SparseVector and the right DenseVector.
Declaration
public static double TransposeMultiply(SparseVector left, DenseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to transpose, then multiply. |
DenseVector | right | Right DenseVector to multiply. |
Returns
Type | Description |
---|---|
Double | The scalar value resulting from the multiplication. |
TransposeMultiply(SparseVector, SparseVector)
Computes the multiplication between the transposed left SparseVector and the right SparseVector.
Declaration
public static double TransposeMultiply(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to transpose, then multiply. |
SparseVector | right | Right SparseVector to multiply. |
Returns
Type | Description |
---|---|
Double | The scalar value resulting from the multiplication. |
TransposeMultiply(SparseVector, Vector)
Computes the multiplication between the transposed left SparseVector and the right Vector.
Declaration
public static double TransposeMultiply(SparseVector left, Vector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to transpose, then multiply. |
Vector | right | Right Vector to multiply. |
Returns
Type | Description |
---|---|
Double | The scalar value resulting from the multiplication. |
TryGetComponent(Int32, out Double)
Gets the components at the given index.
Declaration
public bool TryGetComponent(int index, out double val)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index of the component to get. |
Double | val | Value containing the component at the given index if it was found, zero otherwise. |
Returns
Type | Description |
---|---|
Boolean | true if the component was found, false otherwise. |
Zero(Int32)
Returns the neutral SparseVector for the addition.
Declaration
public static SparseVector Zero(int size)
Parameters
Type | Name | Description |
---|---|---|
Int32 | size | Number of component of the SparseVector. |
Returns
Type | Description |
---|---|
SparseVector | The SparseVector of the given size and with zeros on every coordinates. |
Operators
| Improve this Doc View SourceAddition(SparseVector, DenseVector)
Computes the right addition of a SparseVector with a DenseVector.
Declaration
public static DenseVector operator +(SparseVector left, DenseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector for the addition. |
DenseVector | right | Right DenseVector for the addition. |
Returns
Type | Description |
---|---|
DenseVector | The new DenseVector resulting from the addition. |
Addition(SparseVector, SparseVector)
Computes the addition of two SparseVector.
Declaration
public static SparseVector operator +(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector for the addition. |
SparseVector | right | Right SparseVector for the addition. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the addition. |
Addition(SparseVector, Vector)
Computes the right addition of a DenseVector with a Vector.
Declaration
public static Vector operator +(SparseVector left, Vector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left DenseVector for the addition. |
Vector | right | Right Vector for the addition. |
Returns
Type | Description |
---|---|
Vector | The new DenseVector resulting from the addition. |
Division(SparseVector, Double)
Computes the scalar division of a SparseVector with a
Declaration
public static SparseVector operator /(SparseVector operand, double divisor)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | operand | SparseVector to divide. |
Double | divisor |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the scalar division. |
Equality(SparseVector, SparseVector)
Evaluates whether two SparseVector are equal or not.
Declaration
public static bool operator ==(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector for the comparison. |
SparseVector | right | Right SparseVector for the comparison. |
Returns
Type | Description |
---|---|
Boolean | true if the two SparseVector are equal, false otherwise. |
Inequality(SparseVector, SparseVector)
Evaluates whether two SparseVector are different.
Declaration
public static bool operator !=(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector for the comparison. |
SparseVector | right | Right SparseVector for the comparison. |
Returns
Type | Description |
---|---|
Boolean | true if the two SparseVector are different, false otherwise. |
Multiply(SparseVector, Double)
Computes the right scalar multiplication of a SparseVector with a
Declaration
public static SparseVector operator *(SparseVector operand, double factor)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | operand | SparseVector to multiply on the right. |
Double | factor |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the right scalar multiplication. |
Multiply(Double, SparseVector)
Computes the left scalar multiplication of a SparseVector with a
Declaration
public static SparseVector operator *(double factor, SparseVector operand)
Parameters
Type | Name | Description |
---|---|---|
Double | factor | |
SparseVector | operand | SparseVector to multiply on the left. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the left scalar multiplication. |
Subtraction(SparseVector, DenseVector)
Computes the right subtraction of a SparseVector with a DenseVector.
Declaration
public static DenseVector operator -(SparseVector left, DenseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to subtract. |
DenseVector | right | Right DenseVector to subtract with. |
Returns
Type | Description |
---|---|
DenseVector | The new DenseVector resulting from the subtraction. |
Subtraction(SparseVector, SparseVector)
Computes the subtraction of two SparseVector.
Declaration
public static SparseVector operator -(SparseVector left, SparseVector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left SparseVector to subtract. |
SparseVector | right | Right SparseVector to subtract with. |
Returns
Type | Description |
---|---|
SparseVector | The new SparseVector resulting from the subtraction. |
Subtraction(SparseVector, Vector)
Computes the right subtraction of a DenseVector with a Vector.
Declaration
public static Vector operator -(SparseVector left, Vector right)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | left | Left DenseVector to subtract. |
Vector | right | Right Vector to subtract with. |
Returns
Type | Description |
---|---|
Vector | The new DenseVector resulting from the subtraction. |
UnaryNegation(SparseVector)
Computes the opposite of the SparseVector.
Declaration
public static SparseVector operator -(SparseVector operand)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | operand | SparseVector from which the opposite is computed. |
Returns
Type | Description |
---|---|
SparseVector | The SparseVector, opposite of the initial one. |
Explicit Interface Implementations
| Improve this Doc View SourceIDotProduct<SparseVector, Double>.DotProduct(SparseVector)
Computes the dot product of this element with another element.
Declaration
double IDotProduct<SparseVector, double>.DotProduct(SparseVector other)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | other |
Returns
Type | Description |
---|---|
Double | The value of the dot product of the two elements. |
IMetric<SparseVector>.DistanceTo(SparseVector)
Computes the distance of this element to another element.
Declaration
double IMetric<SparseVector>.DistanceTo(SparseVector other)
Parameters
Type | Name | Description |
---|---|---|
SparseVector | other |
Returns
Type | Description |
---|---|
Double | The value of the distance between the two elements. |
IGroupAction<SparseVector, Double>.Divide(Double)
Computes the right scalar division of the current element.
Declaration
SparseVector IGroupAction<SparseVector, double>.Divide(double divisor)
Parameters
Type | Name | Description |
---|---|---|
Double | divisor |
Returns
Type | Description |
---|---|
SparseVector | The new element resulting from the scalar dividion. |
IGroupAction<SparseVector, Double>.Multiply(Double)
Computes the right scalar multiplication of the current element.
Declaration
SparseVector IGroupAction<SparseVector, double>.Multiply(double factor)
Parameters
Type | Name | Description |
---|---|---|
Double | factor |
Returns
Type | Description |
---|---|
SparseVector | The new element resulting from the scalar multiplication. |