Class ApproximateSparseVector
A one-dimensional vector of double values, optimised for the case where many of the elements share a common value (which need not be zero) within some tolerance.
Implements
Inherited Members
Namespace: Microsoft.ML.Probabilistic.Math
Assembly: Microsoft.ML.Probabilistic.dll
Syntax
[Serializable]
[DataContract]
public class ApproximateSparseVector : SparseVector, IReadOnlyList<double>, IReadOnlyCollection<double>, SettableToPower<Vector>, SettableToProduct<Vector>, SettableToProduct<Vector, Vector>, SettableToWeightedSum<Vector>, ISparseList<double>, ISparseEnumerable<double>, IList<double>, ICollection<double>, IEnumerable<double>, IEnumerable, ICloneable, CanSetAllElementsTo<double>, SettableTo<Vector>
Properties
CountTolerance
The maximum allowed count of vector elements not set to the common value.
Declaration
public int CountTolerance { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Remarks
This is ignored if <= 0 which is the default value.
Item[Int32]
Gets or sets an element at a given index.
Declaration
public override double this[int index] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index | The index of an element. |
Property Value
| Type | Description |
|---|---|
| Double | The element at a given index. |
Overrides
Tolerance
The tolerance at which vector element values are considered equal to the common value.
Declaration
public double Tolerance { get; }
Property Value
| Type | Description |
|---|---|
| Double |
Remarks
By default this tolerance is set to 0.000001.
Methods
Append(Vector)
Returns a new vector which appends a second vector to this vector
Declaration
public override Vector Append(Vector second)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | second | Second vector |
Returns
| Type | Description |
|---|---|
| Vector |
Overrides
Remarks
If the second vector is dense, or if the common values for the two vectors are different, then the result becomes dense
Append(Double)
Appends an item to a vector - returns a new sparse vector
Declaration
public override Vector Append(double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | item |
Returns
| Type | Description |
|---|---|
| Vector |
Overrides
Clone()
Clones this vector - return as a vector
Declaration
public override Vector Clone()
Returns
| Type | Description |
|---|---|
| Vector |
Overrides
Constant(Int32, Double)
Creates an approximate sparse vector of given length with elements all equal to a specified value
Declaration
public static ApproximateSparseVector Constant(int count, double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | count | Number of elements in vector |
| Double | value | value for each element |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
Constant(Int32, Double, Sparsity)
Creates an approximate sparse vector of given length with elements all equal to a specified value
Declaration
public static ApproximateSparseVector Constant(int count, double value, Sparsity s)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | count | Number of elements in vector |
| Double | value | value for each element |
| Sparsity | s | Sparsity |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
Contains(Double)
Returns true if the Vector contains the specified value up to tolerance
Declaration
public override bool Contains(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value | The value to test for |
Returns
| Type | Description |
|---|---|
| Boolean |
Overrides
Copy(Vector)
Creates an approximate sparse vector as a copy of another vector (which may not be sparse)
Declaration
public static ApproximateSparseVector Copy(Vector that)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | that | The source vector - can be dense or sparse |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
FromArray(Double[])
Constructs a sparse vector from a dense array.
Declaration
public static ApproximateSparseVector FromArray(params double[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | data | 1D array of elements. |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
Remarks
The array data is copied into new storage. The size of the vector is taken from the array.
FromArray(Int32, Double[], Int32)
Constructs a vector from part of an array.
Declaration
public static ApproximateSparseVector FromArray(int count, double[] data, int start)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | count | The number of elements in the vector. |
| Double[] | data | Storage for the vector elements. |
| Int32 | start | The starting index in the array for the vector elements. |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
Remarks
Throws an exception if Data is null, start < 0, or count < 0.
FromSparseValues(Int32, Double, Sparsity, List<ValueAtIndex<Double>>)
Constructs a sparse vector from a sorted list of sparse elements.
Declaration
[Construction(new string[]{"Count", "CommonValue", "Sparsity", "SparseValues"})]
public static ApproximateSparseVector FromSparseValues(int count, double commonValue, Sparsity s, List<ValueAtIndex<double>> sortedSparseValues)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | count | Count for result |
| Double | commonValue | Common value |
| Sparsity | s | Sparsity |
| List<ValueAtIndex<Double>> | sortedSparseValues | Sorted list of sparse elements |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
GetHashCode()
Gets a hash code for the instance.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| Int32 | The code. |
Overrides
IndexOf(Double)
Returns the index of the first occurence of the given value in the array. Returns -1 if the value is not in the array
Declaration
public override int IndexOf(double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | item | The item to check for |
Returns
| Type | Description |
|---|---|
| Int32 | Its index in the array |
Overrides
MaxDiff(Vector)
Returns the maximum absolute difference between this vector and another vector.
Declaration
public override double MaxDiff(Vector that)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | that | The second vector. |
Returns
| Type | Description |
|---|---|
| Double |
|
Overrides
Remarks
This routine is typically used instead of Equals, since Equals is susceptible to roundoff errors.
MaxDiff(Vector, Double)
Returns the maximum relative difference between this vector and another.
Declaration
public override double MaxDiff(Vector that, double rel)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | that | The second vector. |
| Double | rel | An offset to avoid division by zero. |
Returns
| Type | Description |
|---|---|
| Double |
|
Overrides
Remarks
This routine is typically used instead of Equals, since Equals is susceptible to roundoff errors.
Scale(Double)
Multiplies this vector by a scalar.
Declaration
public override void Scale(double scale)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | scale | The scalar. |
Overrides
Remarks
this receives the product. This method is a synonym for SetToProduct(this, scale)
SetSubvector(Int32, Vector)
Set a subvector of this sparse vector to another vector. The common value is not changed
Declaration
public override void SetSubvector(int startIndex, Vector that)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | startIndex | The index of the first element of this to copy to. |
| Vector | that | A vector whose length is at most |
Overrides
SetTo(ApproximateSparseVector)
Copies values from a sparse vector to this sparse vector.
Declaration
public void SetTo(ApproximateSparseVector that)
Parameters
| Type | Name | Description |
|---|---|---|
| ApproximateSparseVector | that |
SetTo(Vector)
Copies values from a Vector to this vector.
Declaration
public override void SetTo(Vector that)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | that |
Overrides
Remarks
The source vector can be dense, in which case the minimum value is used as the common value.
SetTo(IEnumerable<Double>)
Set this vector to a collection
Declaration
public override void SetTo(IEnumerable<double> that)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Double> | that |
Overrides
SetTo(IList<Double>, Double)
Copies values from a list of doubles which must have the same size as this vector, using the specified common value.
Declaration
public override void SetTo(IList<double> dlist, double commonValue)
Parameters
| Type | Name | Description |
|---|---|---|
| IList<Double> | dlist | List of doubles |
| Double | commonValue | Common value |
Overrides
SetTo(Double[])
Copies values from an array. The minimum value is used as the common value
Declaration
public override void SetTo(double[] values)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | values | An array whose length matches |
Overrides
SetToDiagonal(Matrix)
Sets this vector to the diagonal of a matrix.
Declaration
public override void SetToDiagonal(Matrix m)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | m | A matrix with Rows==Cols==this.Count. |
Overrides
SetToFunction(SparseVector, SparseVector, Func<Double, Double, Double>)
Sets the elements of this sparse vector to a function of the elements of two other sparse vectors
Declaration
public override SparseVector SetToFunction(SparseVector a, SparseVector b, Func<double, double, double> fun)
Parameters
| Type | Name | Description |
|---|---|---|
| SparseVector | a | The first vector |
| SparseVector | b | The second vector |
| Func<Double, Double, Double> | fun | The function which maps two doubles to a double |
Returns
| Type | Description |
|---|---|
| SparseVector |
Overrides
Remarks
Assumes the vectors are compatible
SetToFunction(SparseVector, Converter<Double, Double>)
Sets the elements of this sparse vector to a function of the elements of another sparse vector
Declaration
public override SparseVector SetToFunction(SparseVector that, Converter<double, double> fun)
Parameters
| Type | Name | Description |
|---|---|---|
| SparseVector | that | The other vector |
| Converter<Double, Double> | fun | The function which maps doubles to doubles |
Returns
| Type | Description |
|---|---|
| SparseVector |
Overrides
Remarks
Assumes the vectors are compatible
SetToFunctionInPlace(SparseVector, Func<Double, Double, Double>)
Sets the elements of this sparse vector to a function of the elements of this sparse vector and another sparse vectors x = fun(x,b)
Declaration
public override SparseVector SetToFunctionInPlace(SparseVector b, Func<double, double, double> fun)
Parameters
| Type | Name | Description |
|---|---|---|
| SparseVector | b | The second vector |
| Func<Double, Double, Double> | fun | The function which maps two doubles to a double |
Returns
| Type | Description |
|---|---|
| SparseVector |
Overrides
Remarks
Assumes the vectors are compatible
SetToSubarray(Double[], Int32)
Copies values from an array. The minimum is used as the common value
Declaration
public override void SetToSubarray(double[] values, int startIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | values | An array whose length is at least |
| Int32 | startIndex | The index of the first value in |
Overrides
SetToSubvector(SparseVector, Int32)
Copies values from a sparse vector. The common value is set to the common value from the source vector.
Declaration
public override void SetToSubvector(SparseVector that, int startIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| SparseVector | that | A vector whose length is at least |
| Int32 | startIndex | The index of the first value in |
Overrides
Subvector(Int32, Int32)
Create a subvector of this sparse vector.
Declaration
public override Vector Subvector(int startIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | startIndex | |
| Int32 | count |
Returns
| Type | Description |
|---|---|
| Vector |
Overrides
ToString(String, String)
String representation of vector with a specified format and delimiter
Declaration
public override string ToString(string format, string delimiter)
Parameters
| Type | Name | Description |
|---|---|---|
| String | format | |
| String | delimiter |
Returns
| Type | Description |
|---|---|
| String |
Overrides
Zero(Int32)
Creates an approximate sparse vector of given length with elements all 0.0
Declaration
public static ApproximateSparseVector Zero(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | count | Number of elements in vector |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
Zero(Int32, Sparsity)
Creates an approximate sparse vector of given length with elements all 0.0
Declaration
public static ApproximateSparseVector Zero(int count, Sparsity s)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | count | Number of elements in vector |
| Sparsity | s | Sparsity |
Returns
| Type | Description |
|---|---|
| ApproximateSparseVector |
Explicit Interface Implementations
ICloneable.Clone()
Clones this vector - return as an object
Declaration
object ICloneable.Clone()
Returns
| Type | Description |
|---|---|
| Object |