本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Double.SparseVector类的典型用法代码示例。如果您正苦于以下问题:C# SparseVector类的具体用法?C# SparseVector怎么用?C# SparseVector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SparseVector类属于MathNet.Numerics.LinearAlgebra.Double命名空间,在下文中一共展示了SparseVector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestVelocityOnPlane
public void TestVelocityOnPlane()
{
Frisbee.SimulationState st = new Frisbee.SimulationState
{
VX = 1,
VY = 1,
Theta = Math.PI / 4
};
Matrix<double> transformation =
new SparseMatrix(new [,]
{
{st.CosTheta, st.SinTheta*st.SinPhi, -st.SinTheta*st.CosPhi},
{0, st.CosPhi, st.SinPhi},
{st.SinTheta, -st.CosTheta*st.SinPhi, st.CosTheta*st.CosPhi}
});
SparseVector c3 = new SparseVector(transformation.Row(2));
SparseVector velocity = new SparseVector(new[] { st.VX, st.VY, st.VZ });
double velocityMagnitude = velocity.Norm(2);
double velocityC3 = velocity.DotProduct(c3);
Vector<double> vp = velocity.Subtract(c3.Multiply(velocityC3));
double vpMagnitude = vp.Norm(2);
}
示例2: Extract
public static SparseVector Extract(this SparseVector vector, int[] rows)
{
var extractedVector = new SparseVector(rows.Length);
for (int i = 0; i < rows.Length; i++)
{
extractedVector[i] = vector[rows[i]];
}
return extractedVector;
}
示例3: MeanRowVector
public static SparseVector MeanRowVector(this Matrix<double> matrix)
{
SparseVector meanVector = new SparseVector(matrix.RowCount);
for (int i = 0; i < matrix.RowCount; i++)
{
SparseVector row = new SparseVector(SparseVectorStorage<double>.OfVector(matrix.Row(i).Storage));
double elements = row.NonZerosCount;
if (elements > 0)
{
meanVector[i] = row.Sum() * (1 / elements);
}
else
{
meanVector[i] = 0;
}
}
return meanVector;
}
示例4: MeanColumnVector
public static SparseVector MeanColumnVector(this Matrix<double> matrix)
{
SparseVector meanVector = new SparseVector(matrix.ColumnCount);
for (int i = 0; i < matrix.ColumnCount; i++)
{
SparseVector column = new SparseVector(SparseVectorStorage<double>.OfVector(matrix.Column(i).Storage));
double elements = column.NonZerosCount;
if (elements > 0)
{
double sum = column.Sum();
meanVector[i] = sum*(1/elements);
}
else
{
meanVector[i] = 0;
}
}
return meanVector;
}
示例5: TryParse
/// <summary>
/// Converts the string representation of a real sparse vector to double-precision sparse vector equivalent.
/// A return value indicates whether the conversion succeeded or failed.
/// </summary>
/// <param name="value">
/// A string containing a real vector to convert.
/// </param>
/// <param name="formatProvider">
/// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information about value.
/// </param>
/// <param name="result">
/// The parsed value.
/// </param>
/// <returns>
/// If the conversion succeeds, the result will contain a complex number equivalent to value.
/// Otherwise the result will be <c>null</c>.
/// </returns>
public static bool TryParse(string value, IFormatProvider formatProvider, out SparseVector result)
{
bool ret;
try
{
result = Parse(value, formatProvider);
ret = true;
}
catch (ArgumentNullException)
{
result = null;
ret = false;
}
catch (FormatException)
{
result = null;
ret = false;
}
return ret;
}
示例6: OuterProduct
/// <summary>
/// Outer product of two vectors
/// </summary>
/// <param name="u">First vector</param>
/// <param name="v">Second vector</param>
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
/// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
public static Matrix<double> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new SparseMatrix(u.Count, v.Count);
for (var i = 0; i < u._storage.ValueCount; i++)
{
for (var j = 0; j < v._storage.ValueCount; j++)
{
matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
}
}
return matrix;
}
示例7: Negate
/// <summary>
/// Returns a negated vector.
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector Negate()
{
var result = new SparseVector(this.Count)
{
NonZeroValues = new double[this.NonZerosCount],
NonZeroIndices = new int[this.NonZerosCount],
NonZerosCount = this.NonZerosCount
};
Buffer.BlockCopy(this.NonZeroIndices, 0, result.NonZeroIndices, 0, this.NonZerosCount * Constants.SizeOfInt);
CommonParallel.For(
0,
this.NonZerosCount,
index => result.NonZeroValues[index] = -this.NonZeroValues[index]);
return result;
}
示例8: SparseVector
/// <summary>
/// Initializes a new instance of the <see cref="SparseVector"/> class by
/// copying the values from another.
/// </summary>
/// <param name="other">
/// The vector to create the new vector from.
/// </param>
public SparseVector(SparseVector other)
: this(other.Count)
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
NonZeroValues = new double[other.NonZerosCount];
NonZeroIndices = new int[other.NonZerosCount];
NonZerosCount = other.NonZerosCount;
Buffer.BlockCopy(other.NonZeroValues, 0, this.NonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(other.NonZeroIndices, 0, this.NonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt);
}
示例9: TensorMultiply
/// <summary>
/// Tensor Product (Outer) of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="OuterProduct"/>
public Matrix TensorMultiply(SparseVector v)
{
return OuterProduct(this, v);
}
示例10: CosineR
private static double CosineR(SparseVector Vector_a, SparseVector Vector_b)
{
return Vector_a.DotProduct(Vector_b) / (Vector_a.L2Norm() * Vector_b.L2Norm());
//return Distance.Cosine(R.Row(a).ToArray(), R.Row(b).ToArray());
}
示例11: CheckSparseMechanismByZeroMultiply
public void CheckSparseMechanismByZeroMultiply()
{
var vector = new SparseVector(10000);
// Add non-zero elements
vector[200] = 1.5;
vector[500] = 3.5;
vector[800] = 5.5;
vector[0] = 7.5;
// Multiply by 0
vector *= 0;
var storage = (SparseVectorStorage<double>) vector.Storage;
Assert.AreEqual(0, vector[200]);
Assert.AreEqual(0, vector[500]);
Assert.AreEqual(0, vector[800]);
Assert.AreEqual(0, vector[0]);
Assert.AreEqual(0, storage.ValueCount);
}
示例12: CheckSparseMechanismBySettingValues
public void CheckSparseMechanismBySettingValues()
{
var vector = new SparseVector(10000);
var storage = (SparseVectorStorage<double>) vector.Storage;
// Add non-zero elements
vector[200] = 1.5;
Assert.AreEqual(1.5, vector[200]);
Assert.AreEqual(1, storage.ValueCount);
vector[500] = 3.5;
Assert.AreEqual(3.5, vector[500]);
Assert.AreEqual(2, storage.ValueCount);
vector[800] = 5.5;
Assert.AreEqual(5.5, vector[800]);
Assert.AreEqual(3, storage.ValueCount);
vector[0] = 7.5;
Assert.AreEqual(7.5, vector[0]);
Assert.AreEqual(4, storage.ValueCount);
// Remove non-zero elements
vector[200] = 0;
Assert.AreEqual(0, vector[200]);
Assert.AreEqual(3, storage.ValueCount);
vector[500] = 0;
Assert.AreEqual(0, vector[500]);
Assert.AreEqual(2, storage.ValueCount);
vector[800] = 0;
Assert.AreEqual(0, vector[800]);
Assert.AreEqual(1, storage.ValueCount);
vector[0] = 0;
Assert.AreEqual(0, vector[0]);
Assert.AreEqual(0, storage.ValueCount);
}
示例13: CanScaleAVectorWhenSettingPreviousNonzeroElementsToZero
public void CanScaleAVectorWhenSettingPreviousNonzeroElementsToZero()
{
var vector = new SparseVector(20);
vector[10] = 1.0;
vector[11] = 2.0;
vector[11] = 0.0;
var scaled = new SparseVector(20);
vector.Multiply(3.0, scaled);
Assert.AreEqual(3.0, scaled[10]);
Assert.AreEqual(0.0, scaled[11]);
}
示例14: CanPointwiseMultiplySparseVector
public void CanPointwiseMultiplySparseVector()
{
var zeroArray = new[] {0.0, 1.0, 0.0, 1.0, 0.0};
var vector1 = SparseVector.OfEnumerable(Data);
var vector2 = SparseVector.OfEnumerable(zeroArray);
var result = new SparseVector(vector1.Count);
vector1.PointwiseMultiply(vector2, result);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(Data[i]*zeroArray[i], result[i]);
}
var resultStorage = (SparseVectorStorage<double>) result.Storage;
Assert.AreEqual(2, resultStorage.ValueCount);
}
示例15: CanDotProductOfTwoSparseVectors
public void CanDotProductOfTwoSparseVectors()
{
var vectorA = new SparseVector(10000);
vectorA[200] = 1;
vectorA[500] = 3;
vectorA[800] = 5;
vectorA[100] = 7;
vectorA[900] = 9;
var vectorB = new SparseVector(10000);
vectorB[300] = 3;
vectorB[500] = 5;
vectorB[800] = 7;
Assert.AreEqual(50.0, vectorA.DotProduct(vectorB));
}