Exemplo n.º 1
0
        /// <summary>
        /// Computes a single singular vector for the given matrix, corresponding to the largest singular value.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="epsilon">The error margin.</param>
        /// <param name="max_iterations">The maximum number of iterations.</param>
        /// <returns>A singular vector, with dimension equal to number of columns of the matrix.</returns>
        public static double[] Decompose1D(double[,] matrix, double epsilon, int max_iterations)
        {
            int    n          = matrix.GetLength(1);
            int    iterations = 0;
            double mag;

            double[] lastIteration;
            double[] currIteration = RandomUnitVector(n);
            double[,] b = M.MultiplyGeneral(M.Transpose(matrix), matrix);
            do
            {
                lastIteration = V.Copy(currIteration);
                currIteration = M.MultiplyVector(b, lastIteration);
                currIteration = V.Scale(currIteration, 100);
                mag           = V.Magnitude(currIteration);
                if (mag > epsilon)
                {
                    currIteration = V.Scale(currIteration, 1 / mag);
                }

                iterations++;
            }while (V.Dot(lastIteration, currIteration) < 1 - epsilon && iterations < max_iterations);

            return(currIteration);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Computes the SVD for the given matrix, with singular values arranged from greatest to least.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="epsilon">The error margin.</param>
        /// <param name="max_iterations">The maximum number of iterations.</param>
        /// <returns>The SVD.</returns>
        public static (double[, ] U, double[] S, double[, ] V) Decompose(double[,] matrix, double epsilon, int max_iterations)
        {
            int m         = matrix.GetLength(0);
            int n         = matrix.GetLength(1);
            int numValues = Math.Min(m, n);

            // sigmas is be a diagonal matrix, hence only a vector is needed
            double[] sigmas = new double[numValues];
            double[,] us = new double[m, numValues];
            double[,] vs = new double[n, numValues];

            // keep track of progress
            double[,] remaining = M.Copy(matrix);

            // for each singular value
            for (int i = 0; i < numValues; i++)
            {
                // compute the v singular vector
                double[] v = Decompose1D(remaining, epsilon, max_iterations);
                double[] u = M.MultiplyVector(matrix, v);

                // compute the contribution of this pair of singular vectors
                double[,] contrib = V.OuterProduct(u, v);

                // extract the singular value
                double s = V.Magnitude(u);

                // v and u should be unit vectors
                if (s < epsilon)
                {
                    u = V.Zero(m);
                    v = V.Zero(n);
                }
                else
                {
                    u = V.Scale(u, 1 / s);
                }

                // save u, v and s into the result
                for (int j = 0; j < u.Length; j++)
                {
                    us[j, i] = u[j];
                }

                for (int j = 0; j < v.Length; j++)
                {
                    vs[j, i] = v[j];
                }

                sigmas[i] = s;

                // remove the contribution of this pair and compute the rest
                remaining = M.Subtract(remaining, contrib);
            }

            return(U : us, S : sigmas, V : vs);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computes a random unit vector.
        /// </summary>
        /// <param name="dimensions">The dimensions of the required vector.</param>
        /// <returns>The unit vector.</returns>
        public static double[] RandomUnitVector(int dimensions)
        {
            Random random = new Random();

            double[] result = new double[dimensions];
            for (int i = 0; i < dimensions; i++)
            {
                result[i] = 2 * random.NextDouble() - 1;
            }

            double magnitude = V.Magnitude(result);

            result = V.Scale(result, 1 / magnitude);
            return(result);
        }