/// <summary>
        /// Perform one iteration.
        /// </summary>
        ///
        public override sealed void Iteration()
        {
            int length = network.RBF.Length;

            var funcs = new IRadialBasisFunction[length];

            // Iteration over neurons and determine the necessaries
            for (int i = 0; i < length; i++)
            {
                IRadialBasisFunction basisFunc = network.RBF[i];

                funcs[i] = basisFunc;

                // This is the value that is changed using other training methods.
                // weights[i] =
                // network.Structure.Synapses[0].WeightMatrix.Data[i][j];
            }

            ObjectPair <double[][], double[][]> data = TrainingSetUtil
                                                       .TrainingToArray(Training);

            double[][] matrix = EngineArray.AllocateDouble2D(length, network.OutputCount);

            FlatToMatrix(network.Flat.Weights, 0, matrix);
            Error = SVD.Svdfit(data.A, data.B, matrix, funcs);
            MatrixToFlat(matrix, network.Flat.Weights, 0);
        }
Exemplo n.º 2
0
        public DeconvolutionSVD(IAlgebraLinear <MatrixType> algebra, IFunction <double, double> input_function, double[] signal_sample_times)
        {
            // build AIF matrix

            AMatrix <MatrixType> forward_matrix = null;
            SVD <MatrixType>     svd            = algebra.ComputeSVD(forward_matrix);
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method will calculate the SVD of Term Frequency Matrix of Stored Documents
        /// </summary>
        /// <returns></returns>
        public SVD CalculateSVD()
        {
            Matrix tempMatrix = new Matrix(_termWeightCorpus);
            SVD    tempsvd    = new SVD(tempMatrix);

            return(tempsvd);
        }
Exemplo n.º 4
0
        public Matrix3d OrthogonalFactorSVD()
        {
            SVD svd = new SVD(e, 3, 3);

            lastSVDIsFullRank = svd.FullRank;
            return(new Matrix3d(svd.OrthogonalFactor));
        }
Exemplo n.º 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testRecoverOrginal()
        public virtual void testRecoverOrginal()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.impl.matrix.MatrixAlgebra algebra = getAlgebra();
            MatrixAlgebra algebra = Algebra;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.linearalgebra.DecompositionResult result = getSVD().apply(A);
            DecompositionResult result = SVD.apply(A);

            assertTrue(result is SVDecompositionResult);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final SVDecompositionResult svd_result = (SVDecompositionResult) result;
            SVDecompositionResult svd_result = (SVDecompositionResult)result;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix u = svd_result.getU();
            DoubleMatrix u = svd_result.U;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix w = com.opengamma.strata.collect.array.DoubleMatrix.diagonal(com.opengamma.strata.collect.array.DoubleArray.copyOf(svd_result.getSingularValues()));
            DoubleMatrix w = DoubleMatrix.diagonal(DoubleArray.copyOf(svd_result.SingularValues));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix vt = svd_result.getVT();
            DoubleMatrix vt = svd_result.VT;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix a = (com.opengamma.strata.collect.array.DoubleMatrix) algebra.multiply(algebra.multiply(u, w), vt);
            DoubleMatrix a = (DoubleMatrix)algebra.multiply(algebra.multiply(u, w), vt);

            checkEquals(A, a);
        }
Exemplo n.º 6
0
    public Vector3 Solve(double svd_tol, int svd_sweeps, double pinv_tol)
    {
        if (QefData.numPoints == 0)
        {
            throw new ArgumentException("...");
        }

        massPoint  = QefData.massPoint;
        massPoint /= QefData.numPoints;
        SetAta();
        SetAtb();
        atb -= MatUtils.VMuSymmetric(ata, massPoint);
        x    = Vector3.Zero;
        double result = SVD.SolveSymmetric(ata, atb, out x, svd_tol, svd_sweeps, pinv_tol);

        if (double.IsNaN(result))
        {
            x = massPoint;
        }
        else
        {
            x += massPoint;
        }
        SetAtb();
        hasSolution = true;
        return(x);
    }
        private void btnShow_Click(object sender, EventArgs e)
        {
            SVD tempSVd = AlgorithmObject.CalculateSVD();

            txtSMatrix.Text = tempSVd.S.ToString("F3", " ", "\n|", "|" + Environment.NewLine, "|");
            txtUMatrix.Text = tempSVd.U.ToString("F3", "", "\n|", "|" + Environment.NewLine, "|");
            txtVMatrix.Text = tempSVd.V.ToString("F3", " ", "\n|", "|" + Environment.NewLine, "|");
        }
Exemplo n.º 8
0
        public Matrix3d InverseSVD()
        {
            SVD      svd = new SVD(e, 3, 3);
            Matrix3d inv = new Matrix3d(svd.Inverse);

            lastSVDIsFullRank = svd.FullRank;
            return(inv);
        }
        public ITransform GenerateTransform(IDataSet <double> data_set)
        {
            AMatrix <MatrixType> data       = algebra.Create(ToolsCollection.ConvertToArray2D(data_set.FeatureData));
            SVD <MatrixType>     svd        = algebra.ComputeSVD(data);
            AMatrix <MatrixType> projection = svd.VT.Transpose().GetColumns(0, destination_dimension_count);

            return(new DimensionReductionPCA <MatrixType>(data_set.DataContext, projection));
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();

            /*double[,] a1 = new double[,] { {0.53637763463723365, 0.028471325537409321, 0.66987587449600727 },
             * { 0.96396422198226872, 0.82154775123183976, 0.84203247765173783 },
             * { 0.30928549371160824, 0.36416072042852676, 0.19277459904215047 } };*/
            Matrix a = Matrix.RandomMatrix3();

            // Matrix a = new Matrix(a1);
            Console.WriteLine("Матрица A");
            PrintData(a.ToArray(), 3, 3);

            SVD svd = MatrixOperations.SVD(a, 1e-15);

            Console.WriteLine();
            Console.WriteLine("Матрица V");
            PrintData(svd.V.ToArray(), 3, 3);
            Console.WriteLine();
            Console.WriteLine("Матрица S");
            PrintData(svd.S.ToArray(), 3, 3);
            Console.WriteLine();
            Console.WriteLine("Матрица U");
            PrintData(svd.U.ToArray(), 3, 3);

            double result = svd.Error(a);

            Console.WriteLine();
            Console.WriteLine("Погрешность:" + result.ToString());

            /*Stopwatch stopwatch = Stopwatch.StartNew();
             * LibraryMethod(Random(1000, 1000), 1000, 1000);
             * stopwatch.Stop();
             * TimeSpan time = stopwatch.Elapsed;*/

            //Eigendecomp eigendecomp = EIG();

            /*List<Vector> vectors = new List<Vector>();
             * vectors.Add(new Vector(new double[] { 0, -80, 0 }));
             * vectors.Add(new Vector(new double[] { -69.3, 40, 0 }));
             * vectors.Add(new Vector(new double[] { 69.3, 40, 0 }));
             * vectors.Add(new Vector(new double[] { 0, 150, 0 }));
             * vectors.Add(new Vector(new double[] { 0, 200, 0 }));
             *
             * List<Matrix> matrices = SimpleExperiments.Experiment(vectors, new Vector(new double[] { 50, -30, 5 }), 1e-13);
             * Console.WriteLine("Матрица R");
             * PrintData(matrices[0].ToArray(), 3, 3);
             * Console.WriteLine("Матрица R'");
             * PrintData(matrices[1].ToArray(), 3, 3);
             *
             * Console.WriteLine();
             * Console.Write("Разница равномерных норм: ");
             * Console.WriteLine(Math.Abs(matrices[0].InfinityNorm() - matrices[1].InfinityNorm()).ToString());*/

            Console.ReadKey();
        }
Exemplo n.º 11
0
        public static void rot02(SMat3 m, double c, double s)
        {
            SVD.calcSymmetricGivensCoefficients(m.m00, m.m02, m.m22, out c, out s);
            var cc  = c * c;
            var ss  = s * s;
            var mix = 2 * c * s * m.m02;

            m.setSymmetric(cc * m.m00 - mix + ss * m.m22, c * m.m01 - s * m.m12, 0,
                           m.m11, s * m.m01 + c * m.m12, ss * m.m00 + mix + cc * m.m22);
        }
Exemplo n.º 12
0
    public static void Rot12(SMat3 m, double c, double s)
    {
        SVD.CalcSymmetricGivensCoefficients(m.m11, m.m12, m.m22, out c, out s);
        double cc  = c * c;
        double ss  = s * s;
        double mix = 2 * c * s * m.m12;

        m.SetSymmetric(m.m00, c * m.m01 - s * m.m02, s * m.m01 + c * m.m02,
                       cc * m.m11 - mix + ss * m.m22, 0, ss * m.m11 + mix + cc * m.m22);
    }
Exemplo n.º 13
0
    public static void rot12(SMat3 m, float c, float s)
    {
        SVD.calcSymmetricGivensCoefficients(m.m11, m.m12, m.m22, c, s);
        float cc  = c * c;
        float ss  = s * s;
        float mix = 2 * c * s * m.m12;

        m.setSymmetric(m.m00, c * m.m01 - s * m.m02, s * m.m01 + c * m.m02,
                       cc * m.m11 - mix + ss * m.m22, 0, ss * m.m11 + mix + cc * m.m22);
    }
Exemplo n.º 14
0
    public static void Rot01(SMat3 m, double c, double s)
    {
        SVD.CalcSymmetricGivensCoefficients(m.m00, m.m01, m.m11, out c, out s);
        double cc  = c * c;
        double ss  = s * s;
        double mix = 2 * c * s * m.m01;

        m.SetSymmetric(cc * m.m00 - mix + ss * m.m11, 0, c * m.m02 - s * m.m12,
                       ss * m.m00 + mix + cc * m.m11, s * m.m02 + c * m.m12, m.m22);
    }
Exemplo n.º 15
0
    public static void rot01(SMat3 m, float c, float s)
    {
        SVD.calcSymmetricGivensCoefficients(m.m00, m.m01, m.m11, c, s);
        float cc  = c * c;
        float ss  = s * s;
        float mix = 2 * c * s * m.m01;

        m.setSymmetric(cc * m.m00 - mix + ss * m.m11, 0, c * m.m02 - s * m.m12,
                       ss * m.m00 + mix + cc * m.m11, s * m.m02 + c * m.m12, m.m22);
    }
Exemplo n.º 16
0
        public Matrix4d Inverse()
        {
            SVD svd = new SVD(e, row_size, row_size);

            if (svd.State == false)
            {
                throw new ArithmeticException();
            }
            return(new Matrix4d(svd.Inverse));
        }
Exemplo n.º 17
0
        public void testSVD()
        {
            //BOOST_MESSAGE("Testing singular value decomposition...");

            setup();

            double tol = 1.0e-12;

            Matrix[] testMatrices = { M1, M2, M3, M4 };

            for (int j = 0; j < testMatrices.Length; j++)
            {
                // m >= n required (rows >= columns)
                Matrix A   = testMatrices[j];
                SVD    svd = new SVD(A);
                // U is m x n
                Matrix U = svd.U();
                // s is n long
                Vector s = svd.singularValues();
                // S is n x n
                Matrix S = svd.S();
                // V is n x n
                Matrix V = svd.V();

                for (int i = 0; i < S.rows(); i++)
                {
                    if (S[i, i] != s[i])
                    {
                        QAssert.Fail("S not consistent with s");
                    }
                }

                // tests
                Matrix U_Utranspose = Matrix.transpose(U) * U;
                if (norm(U_Utranspose - I) > tol)
                {
                    QAssert.Fail("U not orthogonal (norm of U^T*U-I = " + norm(U_Utranspose - I) + ")");
                }

                Matrix V_Vtranspose = Matrix.transpose(V) * V;
                if (norm(V_Vtranspose - I) > tol)
                {
                    QAssert.Fail("V not orthogonal (norm of V^T*V-I = " + norm(V_Vtranspose - I) + ")");
                }

                Matrix A_reconstructed = U * S * Matrix.transpose(V);
                if (norm(A_reconstructed - A) > tol)
                {
                    QAssert.Fail("Product does not recover A: (norm of U*S*V^T-A = " + norm(A_reconstructed - A) + ")");
                }
            }
        }
Exemplo n.º 18
0
        public Vector3 NullVector()
        {
            SVD svd = new SVD(e, 3, 3);

            for (int i = 1; i < 4; ++i)
            {
                if (Mathf.Abs(svd.w[i]) < 1e-7) // ==0
                {
                    return(new Vector3(svd.u[1, i], svd.u[2, i], svd.u[3, i]));
                }
            }
            return(new Vector3(0, 0, 0));
        }
Exemplo n.º 19
0
        public List <Matrix> LowRankApproximation()
        {
            SVD           tempSvd         = CalculateSVD();
            Matrix        afterRankU      = tempSvd.U.SubMatrix(0, tempSvd.U.Rows - 1, 0, RankKValue);
            Matrix        afterRankVH     = tempSvd.VH.SubMatrix(0, tempSvd.VH.Rows - 1, 0, RankKValue);
            Matrix        afterRankV      = tempSvd.V.SubMatrix(0, tempSvd.V.Rows - 1, 0, RankKValue);
            Matrix        afterRankS      = tempSvd.S.SubMatrix(0, RankKValue, 0, RankKValue);
            List <Matrix> reducedMatrices = new List <Matrix>();

            reducedMatrices.Add(afterRankS);
            reducedMatrices.Add(afterRankU);
            reducedMatrices.Add(afterRankV);
            reducedMatrices.Add(afterRankVH);
            return(reducedMatrices);
        }
Exemplo n.º 20
0
        public Vector3 SmallestEigenVector()
        {
            SVD   svd = new SVD(e, 3, 3);
            float min = float.MaxValue; int j = -1;

            for (int i = 1; i < 4; ++i)
            {
                if (svd.w[i] < min) // ==0
                {
                    min = svd.w[i];
                    j   = i;
                }
            }
            return(new Vector3(svd.u[1, j], svd.u[2, j], svd.u[3, j]));
        }
Exemplo n.º 21
0
        public Vector2 LargestEigenVector()
        {
            SVD   svd = new SVD(new float[] { a, b, c, d }, 2, 2);
            float max = -1; int j = -1;

            for (int i = 1; i < 3; ++i)
            {
                if (svd.w[i] > max)
                {
                    max = svd.w[i];
                    j   = i;
                }
            }
            return(new Vector2(svd.u[1, j], svd.u[2, j]));
        }
Exemplo n.º 22
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testInvert()
        public virtual void testInvert()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.impl.matrix.MatrixAlgebra algebra = getAlgebra();
            MatrixAlgebra algebra = Algebra;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final SVDecompositionResult result = getSVD().apply(A);
            SVDecompositionResult result = SVD.apply(A);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix ut = result.getUT();
            DoubleMatrix ut = result.UT;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix v = result.getV();
            DoubleMatrix v = result.V;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] sv = result.getSingularValues();
            double[] sv = result.SingularValues;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n = sv.length;
            int n = sv.Length;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] svinv = new double[n];
            double[] svinv = new double[n];
            for (int i = 0; i < n; i++)
            {
                if (sv[i] == 0.0)
                {
                    svinv[i] = 0.0;
                }
                else
                {
                    svinv[i] = 1.0 / sv[i];
                }
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix winv = com.opengamma.strata.collect.array.DoubleMatrix.diagonal(com.opengamma.strata.collect.array.DoubleArray.copyOf(svinv));
            DoubleMatrix winv = DoubleMatrix.diagonal(DoubleArray.copyOf(svinv));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix ainv = (com.opengamma.strata.collect.array.DoubleMatrix) algebra.multiply(algebra.multiply(v, winv), ut);
            DoubleMatrix ainv = (DoubleMatrix)algebra.multiply(algebra.multiply(v, winv), ut);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix identity = (com.opengamma.strata.collect.array.DoubleMatrix) algebra.multiply(A, ainv);
            DoubleMatrix identity = (DoubleMatrix)algebra.multiply(A, ainv);

            checkIdentity(identity);
        }
Exemplo n.º 23
0
        protected void ApplyDynamicConstraint(Particle pc)
        {
            var plist = new List <Particle>();

            plist.Add(pc);
            foreach (Particle p in this.g.GetNeighborVertices(pc))
            {
                plist.Add(p);
            }

            var A = new float3x3(0);//1,0,0,0,1,0,0,0,1);

            foreach (var p in plist)
            {
                var cx = UnityTools.Math.Operation.OuterProduct(p.predictPos, p.restPos);
                var m  = p.Mass;
                A += p.Ai + m * cx;
            }
            var C0      = this.C0(plist);
            var Ct      = this.Ct(plist);
            var MassSum = this.MassSum(plist);
            var cc      = UnityTools.Math.Operation.OuterProduct(Ct, C0);

            A -= MassSum * cc;

            var U = new float3x3(0);
            var d = new float3(0);
            var V = new float3x3(0);

            SVD.GetSVD3D(A, out U, out d, out V);

            var R = math.mul(U, math.transpose(V));


            foreach (var p in plist)
            {
                var goal  = math.mul(R, p.restPos - C0) + Ct;
                var delta = goal - p.predictPos;

                p.predictPos += delta * this.stiffness;
            }

            pc.predictRotation = new quaternion(R);
        }
Exemplo n.º 24
0
        public static List <Matrix> Experiment(List <Vector> vectors, Vector point, double eps)
        {
            List <Matrix> result    = new List <Matrix>();
            Random        rnd       = new Random();
            double        phi       = rnd.NextDouble() * 360 - 180;
            double        psi       = rnd.NextDouble() * 60 - 30;
            double        theta     = rnd.NextDouble() * 60 - 30;
            Matrix        rotMatrix = MatrixOperations.RotationMatrix(phi, theta, psi);

            result.Add(rotMatrix);

            Matrix E     = ComputeEMatrix(vectors, point);
            Matrix moveE = rotMatrix * E;

            SVD    svd = MatrixOperations.SVD(E * moveE.Transpose(), eps);
            Matrix R   = svd.U * svd.V.Transpose();

            result.Add(R);
            return(result);
        }
Exemplo n.º 25
0
Arquivo: Form1.cs Projeto: Hazber/OOP
        private void ПососатьКакToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*   if (SVD.ShowDialog() == DialogResult.Cancel)
             *     return;
             * string filename = SVD.FileName;
             * byte[] data = FileCreators[SVD.FilterIndex - 1].FileSave(Catalog);
             * using (FileStream fs = new FileStream(filename, FileMode.Create))
             * {
             *     fs.Write(data, 0, data.Length);
             * }*/

            if (SVD.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            string filename = SVD.FileName;

            byte[]     data       = FileCreators[SVD.FilterIndex - 1].FileSave(Catalog);
            PluginForm pluginForm = new PluginForm(data, filename);

            pluginForm.Show();
        }
Exemplo n.º 26
0
    public float solve(Vector3 outx, float svd_tol, int svd_sweeps, float pinv_tol)
    {
        if (data.numPoints == 0)
        {
            throw new ArgumentException("...");
        }

        massPoint.Set(data.massPoint_x, data.massPoint_y, data.massPoint_z);
        massPoint *= (1.0f / data.numPoints);
        setAta();
        setAtb();
        Vector3 tmpv;

        MatUtils.vmul_symmetric(out tmpv, ata, massPoint);
        atb = atb - tmpv;
        x   = Vector3.zero;
        float result = SVD.solveSymmetric(ata, atb, x, svd_tol, svd_sweeps, pinv_tol);

        x += massPoint * 1;
        setAtb();
        outx        = x;
        hasSolution = true;
        return(result);
    }
Exemplo n.º 27
0
Arquivo: SVD.cs Projeto: minikie/test
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SVD obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
Exemplo n.º 28
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testNullObjectMatrix()
        public virtual void testNullObjectMatrix()
        {
            SVD.apply((DoubleMatrix)null);
        }
        public double ErrorScore(List <MyVector2> points)
        {
            foreach (MyVector2 p in points)
            {
                mean += p;
            }

            mean /= points.Count;
            double W = points.Count;


            double[,] C = new double[2, 2];
            foreach (MyVector2 point in points)
            {
                C[0, 0] += (point.x - mean.x) * (point.x - mean.x);
                C[0, 1] += (point.x - mean.x) * (point.y - mean.y);

                C[1, 0] += (point.y - mean.y) * (point.x - mean.x);
                C[1, 1] += (point.y - mean.y) * (point.y - mean.y);
            }

            C[0, 0] /= W;
            C[0, 1] /= W;
            C[1, 0] /= W;
            C[1, 1] /= W;

            Matrix2d CM  = new Matrix2d(C);
            SVD      svd = new SVD(C);
            //svd.w - eigen value, start from 1
            //svd.u - eigen vector, start from 1

            int max = 1, min = 2;

            if (svd.w[max] < svd.w[min])
            {
                int temp = max;
                max = min;
                min = temp;
            }

            double    major     = 2 * Math.Sqrt(svd.w[max]);
            MyVector2 majoraxis = new MyVector2(svd.u[1, max], svd.u[2, max]);

            majoraxis = majoraxis.Normalize();
            double    minor     = 2 * Math.Sqrt(svd.w[min]);
            MyVector2 minoraxis = new MyVector2(svd.u[1, min], svd.u[2, min]);

            minoraxis = minoraxis.Normalize();

            majorendp   = mean + majoraxis * major;
            majorstartp = mean - majoraxis * major;
            minorendp   = mean + minoraxis * minor;
            minorstartp = mean - minoraxis * minor;

            double error = Math.Abs(W - 4 * Math.PI * Math.Sqrt(CM.Det()));

            error /= W;
            Console.WriteLine("Like a ellipse error: {0}", error); //10^-2 may be a good threshold

            return(error);
        }
Exemplo n.º 30
0
        public void testQRSolve()
        {
            // Testing QR solve...
            setup();

            double tol = 1.0e-12;
            MersenneTwisterUniformRng rng = new MersenneTwisterUniformRng(1234);
            Matrix bigM = new Matrix(50, 100, 0.0);

            for (int i = 0; i < Math.Min(bigM.rows(), bigM.columns()); ++i)
            {
                bigM[i, i] = i + 1.0;
            }
            Matrix[] testMatrices = { M1, M2,                   M3, Matrix.transpose(M3),
                                      M4, Matrix.transpose(M4), M5, I,                   M7,bigM, Matrix.transpose(bigM) };

            for (int j = 0; j < testMatrices.Length; j++)
            {
                Matrix A = testMatrices[j];
                Vector b = new Vector(A.rows());

                for (int k = 0; k < 10; ++k)
                {
                    for (int i = 0; i < b.Count; ++i)
                    {
                        b[i] = rng.next().value;
                    }
                    Vector x = MatrixUtilities.qrSolve(A, b, true);

                    if (A.columns() >= A.rows())
                    {
                        if (norm(A * x - b) > tol)
                        {
                            QAssert.Fail("A*x does not match vector b (norm = "
                                         + norm(A * x - b) + ")");
                        }
                    }
                    else
                    {
                        // use the SVD to calculate the reference values
                        int    n  = A.columns();
                        Vector xr = new Vector(n, 0.0);

                        SVD    svd       = new SVD(A);
                        Matrix V         = svd.V();
                        Matrix U         = svd.U();
                        Vector w         = svd.singularValues();
                        double threshold = n * Const.QL_EPSILON;

                        for (int i = 0; i < n; ++i)
                        {
                            if (w[i] > threshold)
                            {
                                double u    = 0;
                                int    zero = 0;
                                for (int kk = 0; kk < U.rows(); kk++)
                                {
                                    u += (U[kk, i] * b[zero++]) / w[i];
                                }

                                for (int jj = 0; jj < n; ++jj)
                                {
                                    xr[jj] += u * V[jj, i];
                                }
                            }
                        }

                        if (norm(xr - x) > tol)
                        {
                            QAssert.Fail("least square solution does not match (norm = "
                                         + norm(x - xr) + ")");
                        }
                    }
                }
            }
        }
Exemplo n.º 31
0
        public float[] SVDSingularMat()
        {
            SVD svd = new SVD(e, 3, 3);

            return(svd.w);
        }