• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Single.DenseMatrix类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Single.DenseMatrix的典型用法代码示例。如果您正苦于以下问题:C# DenseMatrix类的具体用法?C# DenseMatrix怎么用?C# DenseMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



DenseMatrix类属于MathNet.Numerics.LinearAlgebra.Single命名空间,在下文中一共展示了DenseMatrix类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
        }
开发者ID:felix11,项目名称:MSP-Workshops,代码行数:20,代码来源:DataManipulation.cs


示例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);
        }
开发者ID:ulatekh,项目名称:mathnet-numerics,代码行数:17,代码来源:EvdTests.cs


示例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);
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:17,代码来源:EvdTests.cs


示例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));
        }
开发者ID:kityandhero,项目名称:mathnet-numerics,代码行数:39,代码来源:SparseMatrixTests.cs


示例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);
        }
开发者ID:Dig-Doug,项目名称:BU_KinectShowcase,代码行数:43,代码来源:vanillaLogCov.cs


示例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;
 }
开发者ID:EraYaN,项目名称:EV2020,代码行数:11,代码来源:DenseMatrix.cs


示例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;
 }
开发者ID:EraYaN,项目名称:EV2020,代码行数:11,代码来源:DenseMatrix.cs


示例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;
        }
开发者ID:cdrnet,项目名称:mathnet-numerics-native,代码行数:33,代码来源:DenseVector.cs


示例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;
        }
开发者ID:cdrnet,项目名称:mathnet-numerics-native,代码行数:14,代码来源:DenseVector.cs


示例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;
        }
开发者ID:cesardv,项目名称:mathnet-numerics,代码行数:18,代码来源:DenseMatrix.cs


示例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);
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:30,代码来源:LinearAlgebraProviderTests.cs


示例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);
                }
            }
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:22,代码来源:LinearAlgebraProviderTests.cs


示例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);
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:21,代码来源:LinearAlgebraProviderTests.cs


示例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);
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:23,代码来源:LinearAlgebraProviderTests.cs


示例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);
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:24,代码来源:LinearAlgebraProviderTests.cs


示例16: CanSolveForRandomMatrixWhenResultMatrixGiven

        public void CanSolveForRandomMatrixWhenResultMatrixGiven(int order)
        {
            var matrixA = Matrix<float>.Build.Random(order, order, 1);
            var matrixACopy = matrixA.Clone();
            var factorGramSchmidt = matrixA.GramSchmidt();

            var matrixB = Matrix<float>.Build.Random(order, order, 1);
            var matrixBCopy = matrixB.Clone();

            var matrixX = new DenseMatrix(order, order);
            factorGramSchmidt.Solve(matrixB, matrixX);

            // The solution X row dimension is equal to the column dimension of A
            Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);

            // The solution X has the same number of columns as B
            Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);

            var matrixBReconstruct = matrixA * matrixX;

            // Check the reconstruction.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1e-3);
                }
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }

            // Make sure B didn't change.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixBCopy[i, j], matrixB[i, j]);
                }
            }
        }
开发者ID:larzw,项目名称:mathnet-numerics,代码行数:47,代码来源:GramSchmidtTests.cs


示例17: 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;
        }
开发者ID:XiBeichuan,项目名称:hydronumerics,代码行数:18,代码来源:DenseMatrix.cs


示例18: CanComputeThinQRFactorTallMatrixWithWorkArray

        public void CanComputeThinQRFactorTallMatrixWithWorkArray()
        {
            var matrix = _matrices["Tall3x2"];
            var r = new float[matrix.ColumnCount*matrix.ColumnCount];
            var tau = new float[3];
            var q = new float[matrix.RowCount*matrix.ColumnCount];
            Array.Copy(matrix.Values, q, q.Length);

            var work = new float[matrix.ColumnCount*Control.BlockSize];
            Control.LinearAlgebraProvider.ThinQRFactor(q, matrix.RowCount, matrix.ColumnCount, r, tau, work);

            var mq = new DenseMatrix(matrix.RowCount, matrix.ColumnCount, q);
            var mr = new DenseMatrix(matrix.ColumnCount, matrix.ColumnCount, r);
            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);
                }
            }
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:22,代码来源:LinearAlgebraProviderTests.cs


示例19: Identity

        /// <summary>
        /// Initializes a square <see cref="DenseMatrix"/> with all zero's except for ones on the diagonal.
        /// </summary>
        /// <param name="order">the size of the square matrix.</param>
        /// <returns>A dense identity matrix.</returns>
        /// <exception cref="ArgumentException">
        /// If <paramref name="order"/> is less than one.
        /// </exception>
        public static DenseMatrix Identity(int order)
        {
            var m = new DenseMatrix(order);
            for (var i = 0; i < order; i++)
            {
                m._data[(i * order) + i] = 1.0f;
            }

            return m;
        }
开发者ID:cesardv,项目名称:mathnet-numerics,代码行数:18,代码来源:DenseMatrix.cs


示例20: CanMultiplyTallAndWideMatrices

        public void CanMultiplyTallAndWideMatrices()
        {
            var x = _matrices["Tall3x2"];
            var y = _matrices["Wide2x3"];
            var c = new DenseMatrix(x.RowCount, y.ColumnCount);

            Control.LinearAlgebraProvider.MatrixMultiply(x.Values, x.RowCount, x.ColumnCount, y.Values, y.RowCount, y.ColumnCount, c.Values);

            for (var i = 0; i < c.RowCount; i++)
            {
                for (var j = 0; j < c.ColumnCount; j++)
                {
                    AssertHelpers.AlmostEqualRelative(x.Row(i)*y.Column(j), c[i, j], 5);
                }
            }
        }
开发者ID:rmundy,项目名称:mathnet-numerics,代码行数:16,代码来源:LinearAlgebraProviderTests.cs



注:本文中的MathNet.Numerics.LinearAlgebra.Single.DenseMatrix类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Single.DenseVector类代码示例发布时间:2022-05-26
下一篇:
C# Solvers.Iterator类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap