本文整理汇总了C#中SparseMatrix类的典型用法代码示例。如果您正苦于以下问题:C# SparseMatrix类的具体用法?C# SparseMatrix怎么用?C# SparseMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SparseMatrix类属于命名空间,在下文中一共展示了SparseMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CCSMatrix
public CCSMatrix(SparseMatrix matrix, bool transponse)
{
// get number of non-zero elements
m = matrix.ColumnSize;
n = matrix.RowSize;
int nnz = 0;
foreach (List<SparseMatrix.Element> col in matrix.Columns) nnz += col.Count;
// create temp arrays
rowIndex = new int[nnz];
colIndex = new int[n + 1];
values = new double[nnz];
// copy values to arrays
int index = 0;
int index2 = 0;
colIndex[0] = 0;
foreach (List<SparseMatrix.Element> row in matrix.Rows)
{
foreach (SparseMatrix.Element e in row)
{
rowIndex[index] = e.j;
values[index] = e.value;
index++;
}
colIndex[++index2] = index;
}
}
示例2: ComputeProbabilities
void ComputeProbabilities(IList<int> users)
{
foreach (int user_id in users)
{
// initialize counter variables
var user_class_counts = new int[ratings.Scale.Levels.Count];
var user_attribute_given_class_counts = new SparseMatrix<int>(ratings.Scale.Levels.Count, ItemAttributes.NumberOfColumns);
// count
foreach (int index in ratings.ByUser[user_id])
{
int item_id = ratings.Items[index];
int level_id = ratings.Scale.LevelID[ratings[index]];
user_class_counts[level_id]++;
foreach (int attribute_id in item_attributes.GetEntriesByRow(item_id))
user_attribute_given_class_counts[attribute_id, level_id]++;
}
// compute probabilities
float denominator = user_class_counts.Sum() + ClassSmoothing;
foreach (int level_id in ratings.Scale.LevelID.Values)
{
user_class_probabilities[user_id, level_id] = (user_class_counts[level_id] + ClassSmoothing) / denominator;
// TODO sparsify?
for (int attribute_id = 0; attribute_id < NumItemAttributes; attribute_id++)
user_attribute_given_class_probabilities[user_id][attribute_id, level_id]
= (user_attribute_given_class_counts[attribute_id, level_id] + AttributeSmoothing) / (NumItemAttributes + AttributeSmoothing);
}
}
}
示例3: BuildLaplaceCot
public SparseMatrix BuildLaplaceCot(TriMesh mesh)
{
int n = mesh.Vertices.Count;
SparseMatrix L = new SparseMatrix(n, n);
for (int i = 0; i < mesh.Faces.Count; i++)
{
int c1 = mesh.Faces[i].GetVertex(0).Index;
int c2 = mesh.Faces[i].GetVertex(1).Index;
int c3 = mesh.Faces[i].GetVertex(2).Index;
Vector3D v1 = mesh.Faces[i].GetVertex(0).Traits.Position;
Vector3D v2 = mesh.Faces[i].GetVertex(1).Traits.Position;
Vector3D v3 = mesh.Faces[i].GetVertex(2).Traits.Position;
double cot1 = (v2 - v1).Dot(v3 - v1) / (v2 - v1).Cross(v3 - v1).Length();
double cot2 = (v3 - v2).Dot(v1 - v2) / (v3 - v2).Cross(v1 - v2).Length();
double cot3 = (v1 - v3).Dot(v2 - v3) / (v1 - v3).Cross(v2 - v3).Length();
L.AddValueTo(c1, c2, -cot3 / 2); L.AddValueTo(c2, c1, -cot3 / 2);
L.AddValueTo(c2, c3, -cot1 / 2); L.AddValueTo(c3, c2, -cot1 / 2);
L.AddValueTo(c3, c1, -cot2 / 2); L.AddValueTo(c1, c3, -cot2 / 2);
}
for (int i = 0; i < n; i++)
{
double sum = 0;
foreach (SparseMatrix.Element e in L.Rows[i])
{
sum += e.value;
}
L.AddValueTo(i, i, -sum);
}
L.SortElement();
return L;
}
示例4: WriteMatrix
public void WriteMatrix(ref SparseMatrix sparseMatrix, string modelName)
{
string fileName = Path.GetFileNameWithoutExtension(modelName);
string path = GetPath() + fileName + ".matrix";
WriteMatrix(sparseMatrix, path);
}
示例5: TestFrobeniusNorm
public void TestFrobeniusNorm()
{
var matrix = new SparseMatrix<double>(5, 5);
Assert.AreEqual(0, matrix.FrobeniusNorm());
matrix[1, 1] = 5;
Assert.AreEqual(Math.Sqrt(25), matrix.FrobeniusNorm());
}
示例6: WriteSparseMatrix
/// <summary>Write a sparse matrix of integers to a StreamWriter object</summary>
/// <param name="writer">a <see cref="StreamWriter"/></param>
/// <param name="matrix">the matrix of doubles to write out</param>
static public void WriteSparseMatrix(this TextWriter writer, SparseMatrix<int> matrix)
{
writer.WriteLine(matrix.NumberOfRows + " " + matrix.NumberOfColumns);
foreach (var index_pair in matrix.NonEmptyEntryIDs)
writer.WriteLine(index_pair.Item1 + " " + index_pair.Item2 + " " + matrix[index_pair.Item1, index_pair.Item2].ToString());
writer.WriteLine();
}
示例7: TestFrobeniusNorm
[Test()] public void TestFrobeniusNorm()
{
var float_matrix = new SparseMatrix<float>(5, 5);
Assert.AreEqual(0, float_matrix.FrobeniusNorm());
float_matrix[1, 1] = 5;
Assert.AreEqual(Math.Sqrt(25), float_matrix.FrobeniusNorm());
}
示例8: Factorization
public void Factorization(SparseMatrix A)
{
TripletArraryData data = ConvertToTripletArrayData(A);
int rowCount = A.Rows.Count;
int columnCount = A.Columns.Count;
int nnz = data.nnz;
fixed (int* ri = data.rowIndex, ci = data.colIndex)
fixed (double* val = data.values)
{
switch (SolverType)
{
case EnumSolver.UmfpackLU:
solver = CreateSolverLUUMFPACK(rowCount, nnz, ri, ci, val);
break;
case EnumSolver.SuperLULU:
solver = CreateSolverLUSuperLU(rowCount, rowCount, nnz, ri, ci, val);
break;
case EnumSolver.CholmodCholesky:
solver = CreateSolverCholeskyCHOLMOD(rowCount, rowCount, nnz, nnz, ri, ci, val);
break;
case EnumSolver.SPQRLeastNormal:
solver = CreateSolverQRSuiteSparseQR(rowCount, columnCount, nnz, nnz, ri, ci, val);
break;
case EnumSolver.SPQRLeastSqure:
solver = CreateSolverQRSuiteSparseQR(rowCount, columnCount, nnz, nnz, ri, ci, val);
break;
}
}
if (solver == null) throw new Exception("Create Solver Fail");
}
示例9: LinerEquations
/// <summary>
/// 連立線形方程式を作成する
/// </summary>
/// <param name="count">未知数の数</param>
/// <param name="maxNonZeroCount">0でない要素の最大数</param>
public LinerEquations(int count, int maxNonZeroCount)
{
// 係数行列・未知数・右辺ベクトルを初期化
this.A = new SparseMatrix(count, maxNonZeroCount);
this.x = new double[count];
this.b = new double[count];
}
示例10: SolveLongMatrixThrowsArgumentException
public void SolveLongMatrixThrowsArgumentException()
{
var matrix = new SparseMatrix(3, 2);
Vector input = new DenseVector(3);
var solver = new BiCgStab();
Assert.Throws<ArgumentException>(() => solver.Solve(matrix, input));
}
示例11: TestIsSymmetric
[Test()] public void TestIsSymmetric()
{
var matrix1 = new SparseMatrix<double>(3, 5);
Assert.IsFalse(matrix1.IsSymmetric);
var matrix2 = new SparseMatrix<double>(5, 5);
Assert.IsFalse(matrix2.IsSymmetric);
}
示例12: SolveWideMatrixThrowsArgumentException
public void SolveWideMatrixThrowsArgumentException()
{
var matrix = new SparseMatrix(2, 3);
Vector input = new DenseVector(2);
var solver = new GpBiCg();
Assert.Throws<ArgumentException>(() => solver.Solve(matrix, input));
}
示例13: MultiplyByMatlab
public SparseMatrix MultiplyByMatlab(SparseMatrix A, SparseMatrix B)
{
IOHuiZhao.Instance.WriteMatrix(ref A, "abf_A.matrix");
IOHuiZhao.Instance.WriteMatrix(ref B, "abf_B.matrix");
SparseMatrix C = IOHuiZhao.Instance.ReadMatrix("AB.matrix");
return C;
}
示例14: CheckResult
/// <summary>
/// Check the result.
/// </summary>
/// <param name="preconditioner">Specific preconditioner.</param>
/// <param name="matrix">Source matrix.</param>
/// <param name="vector">Initial vector.</param>
/// <param name="result">Result vector.</param>
protected override void CheckResult(IPreConditioner preconditioner, SparseMatrix matrix, Vector vector, Vector result)
{
Assert.AreEqual(typeof(UnitPreconditioner), preconditioner.GetType(), "#01");
// Unit preconditioner is doing nothing. Vector and result should be equal
for (var i = 0; i < vector.Count; i++)
{
Assert.IsTrue(vector[i] == result[i], "#02-" + i);
}
}
示例15: ComputeEigensByLib
public Eigen ComputeEigensByLib(SparseMatrix sparse, int count)
{
SparseMatrixDouble ds = new SparseMatrixDouble(sparse);
Eigen eigen = ComputeEigensByLib(ds, 0.0, count);
return eigen;
}