本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Single.DenseMatrix类的典型用法代码示例。如果您正苦于以下问题:C# DenseMatrix类的具体用法?C# DenseMatrix怎么用?C# DenseMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DenseMatrix类属于MathNet.Numerics.LinearAlgebra.Single命名空间,在下文中一共展示了DenseMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Bmp2Pattern
/// <summary>
/// Converts a given bitmap into an input pattern of the network.
/// </summary>
/// <param name="inputBmp"></param>
/// <returns>a Row*Col X 1 matrix containing the bitmap pixel</returns>
public static Matrix<float> Bmp2Pattern(string path)
{
System.Drawing.Bitmap inputBmp = new Bitmap(path);
Matrix<float> result = new DenseMatrix(inputBmp.Width * inputBmp.Height, 1);
for(int row = 0; row < inputBmp.Height; row++)
for (int col = 0; col < inputBmp.Width; col++)
{
result[(row * inputBmp.Width) + col, 0] = (Convert.ToSingle(inputBmp.GetPixel(row, col).R) + Convert.ToSingle(inputBmp.GetPixel(row, col).G) + Convert.ToSingle(inputBmp.GetPixel(row, col).B)) / (3.0f * 255.0f);
}
inputBmp.Dispose();
return result;
}
示例2: CanCheckRankOfSquareSingular
public void CanCheckRankOfSquareSingular([Values(10, 50, 100)] int order)
{
var A = new DenseMatrix(order, order);
A[0, 0] = 1;
A[order - 1, order - 1] = 1;
for (var i = 1; i < order - 1; i++)
{
A[i, i - 1] = 1;
A[i, i + 1] = 1;
A[i - 1, i] = 1;
A[i + 1, i] = 1;
}
var factorEvd = A.Evd();
Assert.AreEqual(factorEvd.Determinant, 0);
Assert.AreEqual(factorEvd.Rank, order - 1);
}
示例3: CanCheckRankOfSquareSingular
public void CanCheckRankOfSquareSingular(int order)
{
var matrixA = new DenseMatrix(order, order);
matrixA[0, 0] = 1;
matrixA[order - 1, order - 1] = 1;
for (var i = 1; i < order - 1; i++)
{
matrixA[i, i - 1] = 1;
matrixA[i, i + 1] = 1;
matrixA[i - 1, i] = 1;
matrixA[i + 1, i] = 1;
}
var factorEvd = matrixA.Evd();
Assert.AreEqual(factorEvd.Determinant, 0);
Assert.AreEqual(factorEvd.Rank, order - 1);
}
示例4: CanAddSparseMatricesBothWays
public void CanAddSparseMatricesBothWays()
{
var m1 = new SparseMatrix(1, 3);
var m2 = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } });
var sum1 = m1 + m2;
var sum2 = m2 + m1;
Assert.IsTrue(sum1.Equals(m2));
Assert.IsTrue(sum1.Equals(sum2));
var sparseResult = new SparseMatrix(1, 3);
sparseResult.Add(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1));
sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } });
sparseResult.Add(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1));
sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } });
m1.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1));
sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } });
sparseResult.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(2*sum1));
var denseResult = new DenseMatrix(1, 3);
denseResult.Add(m2, denseResult);
Assert.IsTrue(denseResult.Equals(sum1));
denseResult = DenseMatrix.OfArray(new float[,] {{0, 1, 1}});
denseResult.Add(m1, denseResult);
Assert.IsTrue(denseResult.Equals(sum1));
var m3 = DenseMatrix.OfArray(new float[,] {{0, 1, 1}});
var sum3 = m1 + m3;
var sum4 = m3 + m1;
Assert.IsTrue(sum3.Equals(m3));
Assert.IsTrue(sum3.Equals(sum4));
}
示例5: vanillaLogCov
Evd evd; //factorization storage
#endregion Fields
#region Constructors
public vanillaLogCov(float[,] matrix1, float[,] matrix2)
{
//deep copy the covariances
//densematrix allocates new memory for the matrix
cov1 = new DenseMatrix(Covariance(matrix1));
cov2 = new DenseMatrix(Covariance(matrix2));
evd = cov1.Evd();
diag = evd.D();
//could make this a function but thats for later cleanup.... < . <
//should be square matrix so matrix lengths dont matter
for (int i = 0; i < diag.RowCount; i++)
{
diag[i, i] = (float)Math.Log(Math.Abs((double)diag[i, i]));
}
logcov1 = evd.EigenVectors() * diag * evd.EigenVectors().Transpose();
evd = cov2.Evd();
diag = evd.D();
//could make this a function but thats for later cleanup.... < . <
//should be square matrix so matrix lengths dont matter
for (int i = 0; i < diag.RowCount; i++)
{
diag[i, i] = (float)Math.Log(Math.Abs((double)diag[i, i]));
}
logcov2 = evd.EigenVectors() * diag * evd.EigenVectors().Transpose();
//compute distance
distance = 0; //initialize to empty
for (int i = 0; i < logcov2.RowCount; i++)
{
for (int j = 0; j < logcov2.RowCount; j++)
{
distance += (float)Math.Pow(logcov1[i, j] - logcov2[i, j], 2);
}
}
distance = (float)Math.Sqrt(distance);
}
示例6: OfDiagonalVector
/// <summary>
/// Create a new dense matrix with the diagonal as a copy of the given vector.
/// This new matrix will be independent from the vector.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfDiagonalVector(Vector<float> diagonal)
{
var m = new DenseMatrix(diagonal.Count, diagonal.Count);
m.SetDiagonal(diagonal);
return m;
}
示例7: OfDiagonalArray
/// <summary>
/// Create a new dense matrix with the diagonal as a copy of the given array.
/// This new matrix will be independent from the array.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfDiagonalArray(float[] diagonal)
{
var m = new DenseMatrix(diagonal.Length, diagonal.Length);
m.SetDiagonal(diagonal);
return m;
}
示例8: 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 DenseMatrix OuterProduct(DenseVector u, DenseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(
0,
u.Count,
i =>
{
for (var j = 0; j < v.Count; j++)
{
matrix.At(i, j, u._values[i] * v._values[j]);
}
});
return matrix;
}
示例9: ToColumnMatrix
/// <summary>
/// Create a matrix based on this vector in column form (one single column).
/// </summary>
/// <returns>This vector as a column matrix.</returns>
public override Matrix<float> ToColumnMatrix()
{
var matrix = new DenseMatrix(_length, 1);
for (var i = 0; i < _values.Length; i++)
{
matrix.At(i, 0, _values[i]);
}
return matrix;
}
示例10: Transpose
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
/// <returns>The transpose of this matrix.</returns>
public override Matrix<float> Transpose()
{
var ret = new DenseMatrix(_columnCount, _rowCount);
for (var j = 0; j < _columnCount; j++)
{
var index = j * _rowCount;
for (var i = 0; i < _rowCount; i++)
{
ret._data[(i * _columnCount) + j] = _data[index + i];
}
}
return ret;
}
示例11: CanComputeSVDFactorizationOfWideMatrixWithWorkArray
public void CanComputeSVDFactorizationOfWideMatrixWithWorkArray()
{
var matrix = _matrices["Wide2x3"];
var a = new float[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, a, a.Length);
var s = new float[matrix.RowCount];
var u = new float[matrix.RowCount*matrix.RowCount];
var vt = new float[matrix.ColumnCount*matrix.ColumnCount];
var work = new float[100];
Control.LinearAlgebraProvider.SingularValueDecomposition(true, a, matrix.RowCount, matrix.ColumnCount, s, u, vt, work);
var w = new DenseMatrix(matrix.RowCount, matrix.ColumnCount);
for (var index = 0; index < s.Length; index++)
{
w[index, index] = s[index];
}
var mU = new DenseMatrix(matrix.RowCount, matrix.RowCount, u);
var mV = new DenseMatrix(matrix.ColumnCount, matrix.ColumnCount, vt);
var result = mU*w*mV;
AssertHelpers.AlmostEqualRelative(matrix[0, 0], result[0, 0], 5);
AssertHelpers.AlmostEqualRelative(matrix[1, 0], result[1, 0], 5);
AssertHelpers.AlmostEqualRelative(matrix[0, 1], result[0, 1], 5);
AssertHelpers.AlmostEqualRelative(matrix[1, 1], result[1, 1], 5);
AssertHelpers.AlmostEqualRelative(matrix[0, 2], result[0, 2], 5);
AssertHelpers.AlmostEqualRelative(matrix[1, 2], result[1, 2], 5);
}
示例12: CanComputeQRFactorWideMatrix
public void CanComputeQRFactorWideMatrix()
{
var matrix = _matrices["Wide2x3"];
var r = new float[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, r, r.Length);
var tau = new float[3];
var q = new float[matrix.RowCount*matrix.RowCount];
Control.LinearAlgebraProvider.QRFactor(r, matrix.RowCount, matrix.ColumnCount, q, tau);
var mr = new DenseMatrix(matrix.RowCount, matrix.ColumnCount, r).UpperTriangle();
var mq = new DenseMatrix(matrix.RowCount, matrix.RowCount, q);
var a = mq*mr;
for (var row = 0; row < matrix.RowCount; row++)
{
for (var col = 0; col < matrix.ColumnCount; col++)
{
AssertHelpers.AlmostEqualRelative(matrix[row, col], a[row, col], 5);
}
}
}
示例13: CanSolveUsingThinQRTallMatrixUsingWorkArray
public void CanSolveUsingThinQRTallMatrixUsingWorkArray()
{
var matrix = _matrices["Tall3x2"];
var a = new float[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, a, a.Length);
var b = new[] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
var x = new float[matrix.ColumnCount*2];
var work = new float[matrix.RowCount*matrix.ColumnCount];
Control.LinearAlgebraProvider.QRSolve(a, matrix.RowCount, matrix.ColumnCount, b, 2, x, work, QRMethod.Thin);
NotModified(3, 2, a, matrix);
var mb = new DenseMatrix(matrix.RowCount, 2, b);
var test = (matrix.Transpose()*matrix).Inverse()*matrix.Transpose()*mb;
AssertHelpers.AlmostEqualRelative(test[0, 0], x[0], 5);
AssertHelpers.AlmostEqualRelative(test[1, 0], x[1], 5);
AssertHelpers.AlmostEqualRelative(test[0, 1], x[2], 5);
AssertHelpers.AlmostEqualRelative(test[1, 1], x[3], 5);
}
示例14: CanSolveUsingThinQRSquareMatrixUsingWorkArray
public void CanSolveUsingThinQRSquareMatrixUsingWorkArray()
{
var matrix = _matrices["Square3x3"];
var a = new float[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, a, a.Length);
var b = new[] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
var x = new float[matrix.ColumnCount*2];
var work = new float[matrix.RowCount*matrix.ColumnCount];
Control.LinearAlgebraProvider.QRSolve(a, matrix.RowCount, matrix.ColumnCount, b, 2, x, work, QRMethod.Thin);
NotModified(3, 3, a, matrix);
var mx = new DenseMatrix(matrix.ColumnCount, 2, x);
var mb = matrix*mx;
AssertHelpers.AlmostEqualRelative(mb[0, 0], b[0], 5);
AssertHelpers.AlmostEqualRelative(mb[1, 0], b[1], 5);
AssertHelpers.AlmostEqualRelative(mb[2, 0], b[2], 5);
AssertHelpers.AlmostEqualRelative(mb[0, 1], b[3], 5);
AssertHelpers.AlmostEqualRelative(mb[1, 1], b[4], 4);
AssertHelpers.AlmostEqualRelative(mb[2, 1], b[5], 4);
}
示例15: CanSolveUsingSVDTallMatrixOnFactoredMatrix
public void CanSolveUsingSVDTallMatrixOnFactoredMatrix()
{
var matrix = _matrices["Tall3x2"];
var a = new float[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, a, a.Length);
var s = new float[matrix.ColumnCount];
var u = new float[matrix.RowCount*matrix.RowCount];
var vt = new float[matrix.ColumnCount*matrix.ColumnCount];
Control.LinearAlgebraProvider.SingularValueDecomposition(true, a, matrix.RowCount, matrix.ColumnCount, s, u, vt);
var b = new[] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
var x = new float[matrix.ColumnCount*2];
Control.LinearAlgebraProvider.SvdSolveFactored(matrix.RowCount, matrix.ColumnCount, s, u, vt, b, 2, x);
var mb = new DenseMatrix(matrix.RowCount, 2, b);
var test = (matrix.Transpose()*matrix).Inverse()*matrix.Transpose()*mb;
AssertHelpers.AlmostEqualRelative(test[0, 0], x[0], 5);
AssertHelpers.AlmostEqualRelative(test[1, 0], x[1], 5);
AssertHelpers.AlmostEqualRelative(test[0, 1], x[2], 5);
AssertHelpers.AlmostEqualRelative(test[1, 1], x[3], 5);
}