示例#1
0
        public void TestConvertParsleyToEmgu()
        {
            MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(new double[] { 1.0f, 2.0f, 3.0f });
            Emgu.CV.Structure.MCvPoint3D32f       f = v.ToEmguF();
            Assert.AreEqual(1.0, f.x);
            Assert.AreEqual(2.0, f.y);
            Assert.AreEqual(3.0, f.z);

            Emgu.CV.Structure.MCvPoint3D64f d = v.ToEmgu();
            Assert.AreEqual(1.0, d.x);
            Assert.AreEqual(2.0, d.y);
            Assert.AreEqual(3.0, d.z);

            double[,] data = new double[2, 3] {
                { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }
            };

            MathNet.Numerics.LinearAlgebra.Matrix m = MathNet.Numerics.LinearAlgebra.Matrix.Create(data);
            Emgu.CV.Matrix <double> m2 = m.ToEmgu();

            Assert.AreEqual(data[0, 0], m2[0, 0]);
            Assert.AreEqual(data[0, 1], m2[0, 1]);
            Assert.AreEqual(data[0, 2], m2[0, 2]);
            Assert.AreEqual(data[1, 0], m2[1, 0]);
            Assert.AreEqual(data[1, 1], m2[1, 1]);
            Assert.AreEqual(data[1, 2], m2[1, 2]);
        }
示例#2
0
 public Vector(System.Collections.Generic.IEnumerable <int> enumerable)
 {
     InnerVector =
         MathNet.Numerics.LinearAlgebra.Vector <float> .Build.DenseOfEnumerable(
             System.Linq.Enumerable.Select(enumerable, (i => (float)i))
             );
 }
 /// <summary>
 /// Convert MCvPoint3D32f to MathNet.Numerics.LinearAlgebra.Vector
 /// </summary>
 /// <param name="p">MCvPoint3D32f to convert</param>
 /// <returns>MathNet.Numerics.LinearAlgebra.Vector</returns>
 public static MathNet.Numerics.LinearAlgebra.Vector ToParsley(this Emgu.CV.Structure.MCvPoint3D32f p) {
   MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(3);
   v[0] = p.x;
   v[1] = p.y;
   v[2] = p.z;
   return v;
 }
示例#4
0
 /// <summary>
 /// Convert System.Drawing.Point to MathNet.Numerics.LinearAlgebra.Vector
 /// </summary>
 /// <param name="p">System.Drawing.Point to convert</param>
 /// <returns>MathNet.Numerics.LinearAlgebra.Vector</returns>
 public static MathNet.Numerics.LinearAlgebra.Vector ToParsley(this System.Drawing.Point p)
 {
     MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(2);
     v[0] = p.X;
     v[1] = p.Y;
     return(v);
 }
        static void Main(string[] args)
        {
            Transform3D[] trns = new Transform3D[4];
            MathNet.Numerics.LinearAlgebra.Matrix <double> identity = MathNet.Numerics.LinearAlgebra.Matrix <double> .Build.Dense(3, 3);

            identity[0, 0] = 1;
            identity[1, 1] = 1;
            identity[2, 2] = 1;

            MathNet.Numerics.LinearAlgebra.Vector <double> v1 = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(3);

            v1[0] = 0;

            MathNet.Numerics.LinearAlgebra.Vector <double> v2 = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(3);

            v2[0] = 9;

            MathNet.Numerics.LinearAlgebra.Vector <double> v3 = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(3);

            v3[0] = 10;

            MathNet.Numerics.LinearAlgebra.Vector <double> v4 = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(3);

            v4[0] = 11;

            trns[0] = new Transform3D(identity, v1);
            trns[1] = new Transform3D(identity, v2);
            trns[2] = new Transform3D(identity, v3);
            trns[3] = new Transform3D(identity, v4);
            Candidate.initSums(42, 3.14, 2.72);
            Density     d        = new Density(); // finder, we need an instance for certain complicated reason
            Transform3D solution = d.Find(trns);

            Console.WriteLine(solution);
        }
   ToNonHomogeneous(this MathNet.Numerics.LinearAlgebra.Vector v) 
 {
   MathNet.Numerics.LinearAlgebra.Vector r = new MathNet.Numerics.LinearAlgebra.Vector(v.Length - 1);
   for (int i = 0; i < v.Length - 1; ++i) {
     r[i] = v[i];
   }
   return r;
 }
示例#7
0
 /// <summary>
 /// Convert MCvPoint3D32f to MathNet.Numerics.LinearAlgebra.Vector
 /// </summary>
 /// <param name="p">MCvPoint3D32f to convert</param>
 /// <returns>MathNet.Numerics.LinearAlgebra.Vector</returns>
 public static MathNet.Numerics.LinearAlgebra.Vector ToParsley(this Emgu.CV.Structure.MCvPoint3D32f p)
 {
     MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(3);
     v[0] = p.x;
     v[1] = p.y;
     v[2] = p.z;
     return(v);
 }
示例#8
0
        public void TestConvertSystemToParsley()
        {
            System.Drawing.PointF p = new System.Drawing.PointF(1.0f, 2.0f);
            MathNet.Numerics.LinearAlgebra.Vector v = p.ToParsley();

            Assert.AreEqual(1.0, v[0]);
            Assert.AreEqual(2.0, v[1]);
        }
 ToNonHomogeneous(this MathNet.Numerics.LinearAlgebra.Vector v)
 {
     MathNet.Numerics.LinearAlgebra.Vector r = new MathNet.Numerics.LinearAlgebra.Vector(v.Length - 1);
     for (int i = 0; i < v.Length - 1; ++i)
     {
         r[i] = v[i];
     }
     return(r);
 }
示例#10
0
        private static MathNet.Numerics.LinearAlgebra.Vector <double> Multiply2(MathNet.Numerics.LinearAlgebra.Matrix <double> matrix, MathNet.Numerics.LinearAlgebra.Vector <double> rightSide)
        {
            MathNet.Numerics.LinearAlgebra.VectorBuilder <double> build = MathNet.Numerics.LinearAlgebra.Vector <double> .Build;
            MathNet.Numerics.LinearAlgebra.Vector <double>        ret   = build.SameAs(matrix, rightSide, matrix.RowCount);

            Transformer.DoMultiply(matrix, rightSide, ret);

            return(ret);
        }
示例#11
0
文件: NN.cs 项目: kazuCF/CNN
        public DenseVector ToDense(MathNet.Numerics.LinearAlgebra.Vector <double> a)
        {
            DenseVector c = new DenseVector(a.Count);

            for (int i = 0; i < a.Count; i++)
            {
                c[i] = a[i];
            }
            return(c);
        }
示例#12
0
        private static QuantityBase MultiplyVectorMatrix(QuantityBase left, QuantityBase right)
        {
            Quantity <MathNet.Numerics.LinearAlgebra.Vector <double> > leftQ  = (Quantity <MathNet.Numerics.LinearAlgebra.Vector <double> >)left;
            Quantity <MathNet.Numerics.LinearAlgebra.Matrix <double> > rightQ = (Quantity <MathNet.Numerics.LinearAlgebra.Matrix <double> >)right;

            IUnit unit = MultiplyUnits(left, right);

            MathNet.Numerics.LinearAlgebra.Vector <double> vector = leftQ.Value * rightQ.Value;
            return(new Quantity <MathNet.Numerics.LinearAlgebra.Vector <double> >(vector, unit));
        }
   ToHomogeneous(this MathNet.Numerics.LinearAlgebra.Vector v, double w) 
 {
   MathNet.Numerics.LinearAlgebra.Vector r = 
     new MathNet.Numerics.LinearAlgebra.Vector(v.Length + 1);
   for (int i = 0; i < v.Length; ++i) {
     r[i] = v[i];
   }
   r[r.Length - 1] = w;
   return r;
 }
 ToHomogeneous(this MathNet.Numerics.LinearAlgebra.Vector v, double w)
 {
     MathNet.Numerics.LinearAlgebra.Vector r =
         new MathNet.Numerics.LinearAlgebra.Vector(v.Length + 1);
     for (int i = 0; i < v.Length; ++i)
     {
         r[i] = v[i];
     }
     r[r.Length - 1] = w;
     return(r);
 }
示例#15
0
 private static void FormOutput(MathNet.Numerics.LinearAlgebra.Vector <double> solution, Equations.Nonlinear.NonlinearEquationDescription system, ref List <string> outputList)
 {
     for (int i = 0; i < solution.Count; i++)
     {
         outputList.Add(system.VariableNames[i] + " = " + solution[i].ToString());
     }
     double[] x = solution.ToArray();
     for (int i = 0; i < system.Equations.Count; i++)
     {
         outputList.Add($"F{i}(X) = {system.Equations[i].Execute(x)}");
     }
 }
示例#16
0
        private void creatMatrixForLSI()//func for LSI
        {
            createWordsMap();
            createDocsMap();
            int rows = m_wordsMap.Count;
            int cols = m_docsMap.Count;

            double[,] A = new double[rows, cols];
            double[,] q = new double[rows, 1];
            foreach (string word in m_wordsMap.Keys) //create A matrix
            {
                long          ptr        = m_Dictionary[word];
                string        rec        = findPostingRec(@"C:\Users\Daniel\Documents\Visual Studio 2015\Projects\IR\IR\bin\Debug\t\PostingF.txt", ptr);
                List <string> docsOfword = new List <string>();
                string[]      sRec       = rec.Split(' ');
                for (int i = 0; i < sRec.Length; i++)
                {
                    if (sRec[i].EndsWith("#"))
                    {
                        docsOfword.Add(sRec[i].Substring(0, sRec[i].Length - 1));
                    }
                }
                int Arow = m_wordsMap[word];
                foreach (string doc in docsOfword)
                {
                    int Acol = m_docsMap[doc];
                    A[Arow, Acol] = 1;
                }

                q[Arow, 0] = 1; //set q vector
            }
            var Amatrix = DenseMatrix.OfArray(A);
            var svd     = Amatrix.Svd(true);
            var D_T     = svd.VT;
            var T       = svd.U;
            var S       = svd.W;
            int k       = 1;
            var D_Tk    = D_T.SubMatrix(0, k, 0, D_T.ColumnCount);

            var Tk       = T.SubMatrix(0, T.RowCount, 0, k);
            var Sk       = S.SubMatrix(0, k, 0, k);
            var Sk_inver = Sk.Inverse();

            m_Dk_T      = D_Tk;
            m_Sk_invers = Sk_inver;
            m_Tk        = Tk;

            var Qmatrix = DenseMatrix.OfArray(q);
            var q_T     = Qmatrix.Transpose();
            var qMatrix = q_T * m_Tk * m_Sk_invers;

            m_q = qMatrix.Row(0);
        }
示例#17
0
        public static void initSums(double xSize, double ySize, double zSize)
        {
            t0 = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(new double[] { xSize / 2, ySize / 2, zSize / 2 });

            tpSum          = new double[3]; // we only need the diagonal
            tpSum[0]       = xSize * xSize * xSize * ySize * zSize / 12;
            tpSum[1]       = xSize * ySize * ySize * ySize * zSize / 12;
            tpSum[2]       = xSize * ySize * zSize * zSize * zSize / 12;
            dpsum          = tpSum[0] + tpSum[1] + tpSum[2];
            n              = xSize * ySize * zSize;
            invn           = 1.0 / n;
            Density.radius = Math.Sqrt(xSize * xSize + ySize * ySize + zSize * zSize) / 4;
        }
示例#18
0
        public static void PrintImage(MathNet.Numerics.LinearAlgebra.Vector <double> image, int row)
        {
            int column = image.Count / row;

            for (int y = 0; y < row; y++)
            {
                for (int x = 0; x < column; x++)
                {
                    Console.Write(GrayScale[(int)((GrayScale.Length - 1) * (1 - image[y * column + x]))]);
                }
                Console.WriteLine();
            }
        }
示例#19
0
        }//checked

        public static DenseMatrix getdiagboundary(DenseMatrix inFrame)
        {
            DenseMatrix output = new DenseMatrix(inFrame.RowCount, inFrame.ColumnCount);

            for (int diagIdx = -1 * inFrame.RowCount + 1; diagIdx <= inFrame.ColumnCount - 1; diagIdx++)
            {
                MathNet.Numerics.LinearAlgebra.Vector <float> tempDiag = diag(inFrame, diagIdx);
                DenseMatrix tempDiagSum = new DenseMatrix(1, tempDiag.Count);
                int         count       = 0;

                for (int ii = 0; ii < tempDiag.Count; ii++)
                {
                    if (tempDiag[ii] == 1)
                    {
                        count = count + 1;
                        tempDiagSum[0, ii] = count;
                    }
                    else
                    {
                        count = 0;
                    }
                }

                if (diagIdx <= 0)
                {
                    int offset = 0;
                    //iterate through diagonal
                    for (int i = -diagIdx; (i < inFrame.RowCount) && (offset < inFrame.ColumnCount); i++)
                    {
                        output[i, offset] = tempDiagSum[0, offset];
                        offset++;
                    }
                }
                else
                {
                    int offset = 0;
                    for (int i = diagIdx; (i < inFrame.ColumnCount) && (offset < inFrame.RowCount); i++)
                    {
                        output[offset, i] = tempDiagSum[0, offset];
                        offset++;
                    }
                }
            }


            return(output);
        }//checked
示例#20
0
        Vector3 computeNormal(MathNet.Numerics.LinearAlgebra.Vector <MathNet.Numerics.Complex> eigenVa, Matrix <float> eigenVe)
        {
            // Keep the smallest eigenvector as surface normal
            int   smallestId    = 0;
            float smallestValue = float.MaxValue;

            for (int j = 0; j < eigenVe.ColumnCount; j++)
            {
                float lambda = (float)eigenVa[j].Real;
                if (lambda < smallestValue)
                {
                    smallestId    = j;
                    smallestValue = lambda;
                }
            }

            var normalVector = eigenVe.Column(smallestId);

            return(ToVector3(normalVector));
        }
示例#21
0
        static List <Graphs.Vector> GetEigenVectorLayout(MathNet.Numerics.LinearAlgebra.Vector <double> x, MathNet.Numerics.LinearAlgebra.Vector <double> y)
        {
            var minX = double.MaxValue;
            var minY = double.MaxValue;
            var maxX = double.MinValue;
            var maxY = double.MinValue;

            for (int v = 0; v < x.Count; v++)
            {
                minX = Math.Min(minX, x[v]);
                minY = Math.Min(minY, y[v]);
                maxX = Math.Max(maxX, x[v]);
                maxY = Math.Max(maxY, y[v]);
            }

            var width  = maxX - minX;
            var height = maxY - minY;

            return(Enumerable.Range(0, x.Count).Select(v => new Graphs.Vector(0.1 + 0.7 * (x[v] - minX) / width, 0.1 + 0.7 * (y[v] - minY) / height)).ToList());
        }
示例#22
0
        }//checked

        /// <summary>
        /// return an array of diagonal values of input matrix given an offset
        /// </summary>
        /// <param name="inFrame"></param>
        /// <param name="diag_idx"></param>
        /// <returns></returns>
        public static MathNet.Numerics.LinearAlgebra.Vector <float> diag(DenseMatrix inFrame, int diag_idx)
        {
            ///Idea: Given offset, create new matrix from specified offset and get diagonal
            //Trying to mimic Matlab's function

            DenseMatrix newMatrix = null;

            if (diag_idx <= 0)
            {
                //Along length
                newMatrix = (DenseMatrix)inFrame.SubMatrix(-diag_idx, inFrame.RowCount + diag_idx, 0, inFrame.ColumnCount);
            }
            else if (diag_idx > 0)
            {
                newMatrix = (DenseMatrix)inFrame.SubMatrix(0, inFrame.RowCount, diag_idx, inFrame.ColumnCount - diag_idx);
            }
            MathNet.Numerics.LinearAlgebra.Vector <float> diagonals = newMatrix.Diagonal();

            return(diagonals);
        } //checked
示例#23
0
        private static MathNet.Numerics.LinearAlgebra.Matrix <double> PseudoInverse2(MathNet.Numerics.LinearAlgebra.Double.Matrix matrix)
        {
            MathNet.Numerics.LinearAlgebra.Factorization.Svd <double> svd = MathNet.Numerics.LinearAlgebra.Double.Factorization.UserSvd.Create(matrix, true);
            MathNet.Numerics.LinearAlgebra.Matrix <double>            w   = svd.W;
            MathNet.Numerics.LinearAlgebra.Vector <double>            s   = svd.S;
            double tolerance = 008 * svd.L2Norm * System.Math.Pow(2, -53);

            for (int i = 0; i < s.Count; i++)
            {
                s[i] = s[i] < tolerance ? 0 : 1 / s[i];
            }

            for (var i = 0; i < 008; i++)
            {
                double value = s.At(i);
                w.Storage.At(i, i, value);
            }

            MathNet.Numerics.LinearAlgebra.Matrix <double> ddd = svd.U * w * svd.VT;
            return(ddd.Transpose());
        }
    public void TestConvertParsleyToInterop() {
      MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(new double[] { 1.0, 2.0, 3.0 });
      double[] i = v.ToInterop();

      Assert.AreEqual(1.0, i[0]);
      Assert.AreEqual(2.0, i[1]);
      Assert.AreEqual(3.0, i[2]);

      double[,] data = new double[2, 3] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };

      MathNet.Numerics.LinearAlgebra.Matrix m = MathNet.Numerics.LinearAlgebra.Matrix.Create(data);
      double[,] m2 = m.ToInterop();


      Assert.AreEqual(data[0, 0], m2[0, 0]);
      Assert.AreEqual(data[0, 1], m2[0, 1]);
      Assert.AreEqual(data[0, 2], m2[0, 2]);
      Assert.AreEqual(data[1, 0], m2[1, 0]);
      Assert.AreEqual(data[1, 1], m2[1, 1]);
      Assert.AreEqual(data[1, 2], m2[1, 2]);
    }
示例#25
0
        public static Vector2 trilaterate2DLinear(List <BleNode> nodes, List <double> distances)
        {
            if (nodes.Count == 3 && distances.Count == 3)
            {
                MathNet.Numerics.LinearAlgebra.Vector <double> vA = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(new double[] { nodes[0].X, nodes[0].Y });

                double[] b = { 0, 0 };
                b[0] = 0.5 * (Math.Pow(distances[0], 2) - Math.Pow(distances[1], 2) + Math.Pow(getDistance(nodes[1], nodes[0]), 2));
                b[1] = 0.5 * (Math.Pow(distances[0], 2) - Math.Pow(distances[2], 2) + Math.Pow(getDistance(nodes[2], nodes[0]), 2));

                MathNet.Numerics.LinearAlgebra.Vector <double> vb = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(b);

                double[,] A = { { nodes[1].X - nodes[0].X, nodes[1].Y - nodes[0].Y }, { nodes[2].X - nodes[0].X, nodes[2].Y - nodes[0].Y } };

                MathNet.Numerics.LinearAlgebra.Matrix <double> mA = MathNet.Numerics.LinearAlgebra.Matrix <double> .Build.DenseOfArray(A);

                MathNet.Numerics.LinearAlgebra.Matrix <double> mAT = mA.Transpose();

                MathNet.Numerics.LinearAlgebra.Vector <double> x = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(2);

                double det = mA.Multiply(mAT).Determinant();
                if (det > 0.1)
                {
                    x = (mA.Transpose() * mA).Inverse() * (mA.Transpose() * vb);
                }
                else
                {
                    x = (((mA.Multiply(mAT)).Inverse()).Multiply(mAT)).Multiply(vb);
                }

                x.Add(vA);

                double[] coordinates = x.ToArray();

                Vector2 vector = new Vector2((float)coordinates[0], (float)coordinates[1]);
                return(vector);
            }

            return(new Vector2());
        }
示例#26
0
        internal Transform3D toTransform3D()
        {
            MathNet.Numerics.LinearAlgebra.Matrix <double> r = MathNet.Numerics.LinearAlgebra.Matrix <double> .Build.Dense(3, 3);

            MathNet.Numerics.LinearAlgebra.Vector <double> tr = MathNet.Numerics.LinearAlgebra.Vector <double> .Build.Dense(3);

            r[0, 0] = rot[0];
            r[0, 1] = rot[1];
            r[0, 2] = rot[2];
            r[1, 0] = rot[3];
            r[1, 1] = rot[4];
            r[1, 2] = rot[5];
            r[2, 0] = rot[6];
            r[2, 1] = rot[7];
            r[2, 2] = rot[8];
            var Rt0 = r * t0;

            tr[0] = t[0] - Rt0[0];
            tr[1] = t[1] - Rt0[1];
            tr[2] = t[2] - Rt0[2];
            return(new Transform3D(r, tr));
        }
示例#27
0
        public void TestConvertEmguToParsley()
        {
            Emgu.CV.Structure.MCvPoint3D32f       p = new Emgu.CV.Structure.MCvPoint3D32f(1.0f, 2.0f, 3.0f);
            MathNet.Numerics.LinearAlgebra.Vector v = p.ToParsley();

            Assert.AreEqual(1.0, v[0]);
            Assert.AreEqual(2.0, v[1]);
            Assert.AreEqual(3.0, v[2]);

            double[,] data = new double[2, 3] {
                { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }
            };

            Emgu.CV.Matrix <double> m = new Emgu.CV.Matrix <double>(data);
            MathNet.Numerics.LinearAlgebra.Matrix m2 = m.ToParsley();

            Assert.AreEqual(data[0, 0], m2[0, 0]);
            Assert.AreEqual(data[0, 1], m2[0, 1]);
            Assert.AreEqual(data[0, 2], m2[0, 2]);
            Assert.AreEqual(data[1, 0], m2[1, 0]);
            Assert.AreEqual(data[1, 1], m2[1, 1]);
            Assert.AreEqual(data[1, 2], m2[1, 2]);
        }
示例#28
0
        /// <summary>
        /// Find Homography.
        /// </summary>
        /// <param name="source"> The source Transformer. </param>
        /// <param name="destination"> The destination Transformer. </param>
        /// <returns> The homologous matrix. </returns>
        public static Matrix3x2 FindHomography(ITransformerLTRB source, ITransformerLTRB destination)
        {
            float x0 = source.LeftTop.X, x1 = source.RightTop.X, x2 = source.LeftBottom.X, x3 = source.RightBottom.X;
            float y0 = source.LeftTop.Y, y1 = source.RightTop.Y, y2 = source.LeftBottom.Y, y3 = source.RightBottom.Y;
            float u0 = destination.LeftTop.X, u1 = destination.RightTop.X, u2 = destination.LeftBottom.X, u3 = destination.RightBottom.X;
            float v0 = destination.LeftTop.Y, v1 = destination.RightTop.Y, v2 = destination.LeftBottom.Y, v3 = destination.RightBottom.Y;

            MathNet.Numerics.LinearAlgebra.Double.DenseMatrix matrix = MathNet.Numerics.LinearAlgebra.Double.DenseMatrix.OfArray(new double[008, 008]
            {
                { x0, y0, 1, 0, 0, 0, -u0 * x0, -u0 * y0 },
                { 0, 0, 0, x0, y0, 1, -v0 * x0, -v0 * y0 },
                { x1, y1, 1, 0, 0, 0, -u1 * x1, -u1 * y1 },
                { 0, 0, 0, x1, y1, 1, -v1 * x1, -v1 * y1 },
                { x3, y3, 1, 0, 0, 0, -u3 * x3, -u3 * y3 },
                { 0, 0, 0, x3, y3, 1, -v3 * x3, -v3 * y3 },
                { x2, y2, 1, 0, 0, 0, -u2 * x2, -u2 * y2 },
                { 0, 0, 0, x2, y2, 1, -v2 * x2, -v2 * y2 },
            });

            MathNet.Numerics.LinearAlgebra.Double.DenseVector vector = new MathNet.Numerics.LinearAlgebra.Double.DenseVector(new double[008]
            {
                u0, v0, u1, v1,
                u3, v3, u2, v2
            });

            MathNet.Numerics.LinearAlgebra.Matrix <double> PseudoInverse = Transformer.PseudoInverse2(matrix);

            MathNet.Numerics.LinearAlgebra.Vector <double> ret = Transformer.Multiply2(PseudoInverse, vector);

            return(new Matrix3x2
                   (
                       m11: (float)ret[0], m12: (float)ret[3],
                       m21: (float)ret[1], m22: (float)ret[4],
                       m31: (float)ret[2], m32: (float)ret[5]
                   ));
        }
示例#29
0
        public void TestConvertParsleyToInterop()
        {
            MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(new double[] { 1.0, 2.0, 3.0 });
            double[] i = v.ToInterop();

            Assert.AreEqual(1.0, i[0]);
            Assert.AreEqual(2.0, i[1]);
            Assert.AreEqual(3.0, i[2]);

            double[,] data = new double[2, 3] {
                { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }
            };

            MathNet.Numerics.LinearAlgebra.Matrix m = MathNet.Numerics.LinearAlgebra.Matrix.Create(data);
            double[,] m2 = m.ToInterop();


            Assert.AreEqual(data[0, 0], m2[0, 0]);
            Assert.AreEqual(data[0, 1], m2[0, 1]);
            Assert.AreEqual(data[0, 2], m2[0, 2]);
            Assert.AreEqual(data[1, 0], m2[1, 0]);
            Assert.AreEqual(data[1, 1], m2[1, 1]);
            Assert.AreEqual(data[1, 2], m2[1, 2]);
        }
    public void TestConvertParsleyToEmgu() {
      MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(new double[] { 1.0f, 2.0f, 3.0f });
      Emgu.CV.Structure.MCvPoint3D32f f = v.ToEmguF();
      Assert.AreEqual(1.0, f.x);
      Assert.AreEqual(2.0, f.y);
      Assert.AreEqual(3.0, f.z);

      Emgu.CV.Structure.MCvPoint3D64f d = v.ToEmgu();
      Assert.AreEqual(1.0, d.x);
      Assert.AreEqual(2.0, d.y);
      Assert.AreEqual(3.0, d.z);

      double[,] data = new double[2, 3] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };

      MathNet.Numerics.LinearAlgebra.Matrix m = MathNet.Numerics.LinearAlgebra.Matrix.Create(data);
      Emgu.CV.Matrix<double> m2 = m.ToEmgu();

      Assert.AreEqual(data[0, 0], m2[0, 0]);
      Assert.AreEqual(data[0, 1], m2[0, 1]);
      Assert.AreEqual(data[0, 2], m2[0, 2]);
      Assert.AreEqual(data[1, 0], m2[1, 0]);
      Assert.AreEqual(data[1, 1], m2[1, 1]);
      Assert.AreEqual(data[1, 2], m2[1, 2]);
    }
示例#31
0
 /// <summary>
 /// Given a vector x, returns the derivative of x, elementwise
 /// </summary>
 /// <param name="x">the input vector x</param>
 /// <returns> elementwise application of the derivative of the relu function =  x > 0 ? 1 : 0.01</returns>
 public MathNet.Numerics.LinearAlgebra.Vector <double> CalculateDerivative(MathNet.Numerics.LinearAlgebra.Vector <double> x)
 {
     return(x.Map(s => s > 0 ? 1 : 0.01));
 }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //Input
            Matrix mL = null;

            DA.GetData(0, ref mL);

            //size of n x n matrix
            int n = mL.RowCount;


            //Number of eigenvalues/vectors to extract
            int eigsCount = n;

            if (Params.Input[1].SourceCount > 0)
            {
                DA.GetData(1, ref eigsCount);
            }

            if (eigsCount > n)
            {
                eigsCount = n;
            }
            else if (eigsCount < 1)
            {
                eigsCount = 1;
            }


            //Run calculation toggle
            bool run = false;

            DA.GetData(2, ref run);


            //Calculate

            //Create lists to store data
            List <double>     eigenValuesOutput       = new List <double>();
            DataTree <double> eigenVectorsOutput      = new DataTree <double>();
            Matrix            eigenVectorMatrixOutput = null;


            if (run && mL != null)
            {
                //Create matrix for MathNet eigendecomposition
                var matrix = MathNet.Numerics.LinearAlgebra.Matrix <double> .Build.Dense(n, n);

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        matrix[i, j] = mL[i, j];
                    }
                }

                //Eigendecomposition
                var evd = matrix.Evd(MathNet.Numerics.LinearAlgebra.Symmetricity.Symmetric);

                MathNet.Numerics.LinearAlgebra.Vector <Complex> eigsValComplex = evd.EigenValues;
                MathNet.Numerics.LinearAlgebra.Vector <Double>  eigsValReal    = eigsValComplex.Map(c => c.Real);
                MathNet.Numerics.LinearAlgebra.Matrix <double>  eigsVec        = evd.EigenVectors;


                //Convert data back to GH types

                //Eigenvalue list
                for (int i = 0; i < eigsCount; i++)
                {
                    eigenValuesOutput.Add(eigsValReal[i]);
                }

                //Eigenvectors
                eigenVectorMatrixOutput = new Matrix(n, eigsCount);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < eigsCount; j++)
                    {
                        double val = eigsVec[i, j];

                        GH_Path path = new GH_Path(j);
                        eigenVectorsOutput.Add(val, path);

                        eigenVectorMatrixOutput[i, j] = val;
                    }
                }
            }


            //Output
            DA.SetDataList(0, eigenValuesOutput);
            DA.SetDataTree(1, eigenVectorsOutput);
            DA.SetData(2, eigenVectorMatrixOutput);
        }
 /// <summary>
 /// Convert System.Drawing.Point to MathNet.Numerics.LinearAlgebra.Vector
 /// </summary>
 /// <param name="p">System.Drawing.Point to convert</param>
 /// <returns>MathNet.Numerics.LinearAlgebra.Vector</returns>
 public static MathNet.Numerics.LinearAlgebra.Vector ToParsley(this System.Drawing.Point p) {
   MathNet.Numerics.LinearAlgebra.Vector v = new MathNet.Numerics.LinearAlgebra.Vector(2);
   v[0] = p.X;
   v[1] = p.Y;
   return v;
 }
示例#34
0
 public Vector()
 {
     InnerVector = null;
 }
示例#35
0
 /// <summary>
 /// Given an input vector x, this function computes and returns max(x,0.01x) elementwise
 /// </summary>
 /// <param name="x">the inout vector x</param>
 /// <returns> max(x,0.01x) </returns>
 public MathNet.Numerics.LinearAlgebra.Vector <double> CalculateActivation(MathNet.Numerics.LinearAlgebra.Vector <double> x)
 {
     return(x.Map(s => s > 0 ? s : 0.01 * s));
 }
示例#36
0
文件: Util.cs 项目: jarroda/raytracer
 public static Vector3 ToVector(this MathNet.Numerics.LinearAlgebra.Vector <double> vector)
 => new Vector3((float)vector[0], (float)vector[1], (float)vector[2]);