private void Compute(Matrix symmetricMatrix)
        {
            var tmpDiag = new SparseVector(size);

            Blas.Default.Copy(_diagonal, tmpDiag);
            var    tmpAccumulate = new SparseVector(size);
            Matrix s             = symmetricMatrix.Clone();

            for (int ite = 1; ite <= MaxIterations; ite++)
            {
                // abs sum of upper triangel
                double sum = 0.0;
                for (int j = 0; j < size - 1; j++)
                {
                    for (int k = j + 1; k < size; k++)
                    {
                        sum += Math.Abs(s[j, k]);
                    }
                }

                if (sum == 0.0)
                {
                    return;
                }

                // To speed up computation a threshold is introduced to
                // make sure it is worthy to perform the Jacobi rotation
                double threshold;
                if (ite < 5)
                {
                    threshold = 0.2 * sum / (size * size);
                }
                else
                {
                    threshold = 0.0;
                }

                // sweep upper triangle
                for (int j = 0; j < size - 1; j++)
                {
                    for (int k = j + 1; k < size; k++)
                    {
                        double smll = Math.Abs(s[j, k]);
                        if (ite > 5 &&
                            smll < EpsPrec * Math.Abs(_diagonal.Data[j]) &&
                            smll < EpsPrec * Math.Abs(_diagonal.Data[k]))
                        {
                            s[j, k] = 0.0;
                        }
                        else if (Math.Abs(s[j, k]) > threshold)
                        {
                            double heig = _diagonal.Data[k] - _diagonal.Data[j];
                            double tang;
                            if (smll < EpsPrec * Math.Abs(heig))
                            {
                                tang = s[j, k] / heig;
                            }
                            else
                            {
                                double beta = 0.5 * heig / s[j, k];
                                tang = 1.0 / (Math.Abs(beta) + Math.Sqrt(1.0 + beta * beta));
                                if (beta < 0.0)
                                {
                                    tang = -tang;
                                }
                            }
                            double cosin = 1.0 / Math.Sqrt(1.0 + tang * tang);
                            double sine  = tang * cosin;
                            double rho   = sine / (1.0 + cosin);
                            heig = tang * s[j, k];

                            tmpAccumulate.Data[j] -= heig;
                            tmpAccumulate.Data[k] += heig;
                            _diagonal.Data[j]     -= heig;
                            _diagonal.Data[k]     += heig;
                            s[j, k] = 0.0;

                            // perform rotation on upper triangle
                            int l = 0;
                            for (   ; l < j; l++)
                            {
                                JacobiRotate(s, rho, sine, l, j, l, k);
                            }
                            for (++l; l < k; l++)
                            {
                                JacobiRotate(s, rho, sine, j, l, l, k);
                            }
                            for (++l; l < size; l++)
                            {
                                JacobiRotate(s, rho, sine, j, l, k, l);
                            }

                            for (l = 0; l < size; l++)
                            {
                                JacobiRotate(_eigenVectors, rho, sine, l, j, l, k);
                            }
                        }
                    }
                }
                // tmpDiag.Add(tmpAccumulate);
                // diagonal = tmpDiag.Clone();
                // tmpAccumulate.Fill(0.0);
                for (int j = 0; j < size; j++)
                {
                    tmpDiag.Data[j]      += tmpAccumulate.Data[j];
                    _diagonal.Data[j]     = tmpDiag.Data[j];
                    tmpAccumulate.Data[j] = 0.0;
                }
            }
            throw new ApplicationException("TODO: Too many iterations reached");
        }
示例#2
0
        /// <summary>LU Decomposition</summary>
        /// <param name="A">  Rectangular matrix
        /// </param>
        /// <returns>     Structure to access L, U and piv.
        /// </returns>
		
        public LUDecomposition(Matrix A)
        {
            // Use a "left-looking", dot-product, Crout/Doolittle algorithm.
			
            _lu = A.Clone();
            _piv = new int[m];
            for (int i = 0; i < m; i++)
            {
                _piv[i] = i;
            }
            _pivsign = 1;
            //double[] LUrowi;
            var LUcolj = new double[m];
			
            // Outer loop.
			
            for (int j = 0; j < n; j++)
            {
				
                // Make a copy of the j-th column to localize references.
				
                for (int i = 0; i < m; i++)
                {
                    LUcolj[i] = _lu[i, j];
                }
				
                // Apply previous transformations.
				
                for (int i = 0; i < m; i++)
                {
                    //LUrowi = LU[i];
					
                    // Most of the time is spent in the following dot product.
					
                    int kmax = Math.Min(i, j);
                    double s = 0.0;
                    for (int k = 0; k < kmax; k++)
                    {
                        s += _lu[i,k] * LUcolj[k];
                    }
					
                    _lu[i,j] = LUcolj[i] -= s;
                }
				
                // Find pivot and exchange if necessary.
				
                int p = j;
                for (int i = j + 1; i < m; i++)
                {
                    if (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p]))
                    {
                        p = i;
                    }
                }
                if (p != j)
                {
                    for (int k = 0; k < n; k++)
                    {
                        double t = _lu[p, k]; _lu[p, k] = _lu[j, k]; _lu[j, k] = t;
                    }
                    int k2 = _piv[p]; _piv[p] = _piv[j]; _piv[j] = k2;
                    _pivsign = - _pivsign;
                }
				
                // Compute multipliers.
				
                if (j < m & _lu[j, j] != 0.0)
                {
                    for (int i = j + 1; i < m; i++)
                    {
                        _lu[i, j] /= _lu[j, j];
                    }
                }
            }
        }
示例#3
0
        /// <summary>Construct the singular value decomposition.</summary>
        /// <remarks>Provides access to U, S and V.</remarks>
        /// <param name="Arg">Rectangular matrix</param>
        public SingularValueDecomposition(Matrix Arg)
        {
            transpose = (Arg.RowCount < Arg.ColumnCount);

            // Derived from LINPACK code.
            // Initialize.
            var A = Arg.Clone();

            if (transpose)
            {
                A.Transpose();
            }

            m = A.RowCount;
            n = A.ColumnCount;
            int nu = Math.Min(m, n);

            s = new double[Math.Min(m + 1, n)];
            U = new Matrix(m, nu);
            V = new Matrix(n, n);

            var        e     = new double[n];
            var        work  = new double[m];
            const bool wantu = true;
            const bool wantv = true;

            // Reduce A to bidiagonal form, storing the diagonal elements
            // in s and the super-diagonal elements in e.

            int nct = Math.Min(m - 1, n);
            int nrt = Math.Max(0, Math.Min(n - 2, m));

            for (int k = 0; k < Math.Max(nct, nrt); k++)
            {
                if (k < nct)
                {
                    // Compute the transformation for the k-th column and
                    // place the k-th diagonal in s[k].
                    // Compute 2-norm of k-th column without under/overflow.
                    s[k] = 0;
                    for (int i = k; i < m; i++)
                    {
                        s[k] = Fn.Hypot(s[k], A[i, k]);
                    }
                    if (s[k] != 0.0)
                    {
                        if (A[k, k] < 0.0)
                        {
                            s[k] = -s[k];
                        }
                        for (int i = k; i < m; i++)
                        {
                            A[i, k] /= s[k];
                        }
                        A[k, k] += 1.0;
                    }
                    s[k] = -s[k];
                }
                for (int j = k + 1; j < n; j++)
                {
                    if ((k < nct) & (s[k] != 0.0))
                    {
                        // Apply the transformation.

                        double t = 0;
                        for (int i = k; i < m; i++)
                        {
                            t += A[i, k] * A[i, j];
                        }
                        t = (-t) / A[k, k];
                        for (int i = k; i < m; i++)
                        {
                            A[i, j] += t * A[i, k];
                        }
                    }

                    // Place the k-th row of A into e for the
                    // subsequent calculation of the row transformation.

                    e[j] = A[k, j];
                }
                if (wantu & (k < nct))
                {
                    // Place the transformation in U for subsequent back
                    // multiplication.

                    for (int i = k; i < m; i++)
                    {
                        U[i, k] = A[i, k];
                    }
                }
                if (k >= nrt)
                {
                    continue;
                }
                // Compute the k-th row transformation and place the
                // k-th super-diagonal in e[k].
                // Compute 2-norm without under/overflow.
                e[k] = 0;
                for (int i = k + 1; i < n; i++)
                {
                    e[k] = Fn.Hypot(e[k], e[i]);
                }
                if (e[k] != 0.0)
                {
                    if (e[k + 1] < 0.0)
                    {
                        e[k] = -e[k];
                    }
                    for (int i = k + 1; i < n; i++)
                    {
                        e[i] /= e[k];
                    }
                    e[k + 1] += 1.0;
                }
                e[k] = -e[k];
                if ((k + 1 < m) & (e[k] != 0.0))
                {
                    // Apply the transformation.

                    for (int i = k + 1; i < m; i++)
                    {
                        work[i] = 0.0;
                    }
                    for (int j = k + 1; j < n; j++)
                    {
                        for (int i = k + 1; i < m; i++)
                        {
                            work[i] += e[j] * A[i, j];
                        }
                    }
                    for (int j = k + 1; j < n; j++)
                    {
                        double t = (-e[j]) / e[k + 1];
                        for (int i = k + 1; i < m; i++)
                        {
                            A[i, j] += t * work[i];
                        }
                    }
                }
                if (wantv)
                {
                    // Place the transformation in V for subsequent
                    // back multiplication.

                    for (int i = k + 1; i < n; i++)
                    {
                        V[i, k] = e[i];
                    }
                }
            }

            // Set up the final bidiagonal matrix or order p.

            int p = System.Math.Min(n, m + 1);

            if (nct < n)
            {
                s[nct] = A[nct, nct];
            }
            if (m < p)
            {
                s[p - 1] = 0.0;
            }
            if (nrt + 1 < p)
            {
                e[nrt] = A[nrt, p - 1];
            }
            e[p - 1] = 0.0;

            // If required, generate U.

            if (wantu)
            {
                for (int j = nct; j < nu; j++)
                {
                    for (int i = 0; i < m; i++)
                    {
                        U[i, j] = 0.0;
                    }
                    U[j, j] = 1.0;
                }
                for (int k = nct - 1; k >= 0; k--)
                {
                    if (s[k] != 0.0)
                    {
                        for (int j = k + 1; j < nu; j++)
                        {
                            double t = 0;
                            for (int i = k; i < m; i++)
                            {
                                t += U[i, k] * U[i, j];
                            }
                            t = (-t) / U[k, k];
                            for (int i = k; i < m; i++)
                            {
                                U[i, j] += t * U[i, k];
                            }
                        }
                        for (int i = k; i < m; i++)
                        {
                            U[i, k] = -U[i, k];
                        }
                        U[k, k] = 1.0 + U[k, k];
                        for (int i = 0; i < k - 1; i++)
                        {
                            U[i, k] = 0.0;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < m; i++)
                        {
                            U[i, k] = 0.0;
                        }
                        U[k, k] = 1.0;
                    }
                }
            }

            // If required, generate V.

            if (wantv)
            {
                for (int k = n - 1; k >= 0; k--)
                {
                    if ((k < nrt) & (e[k] != 0.0))
                    {
                        for (int j = k + 1; j < nu; j++)
                        {
                            double t = 0;
                            for (int i = k + 1; i < n; i++)
                            {
                                t += V[i, k] * V[i, j];
                            }
                            t = (-t) / V[k + 1, k];
                            for (int i = k + 1; i < n; i++)
                            {
                                V[i, j] += t * V[i, k];
                            }
                        }
                    }
                    for (int i = 0; i < n; i++)
                    {
                        V[i, k] = 0.0;
                    }
                    V[k, k] = 1.0;
                }
            }

            // Main iteration loop for the singular values.

            int    pp   = p - 1;
            int    iter = 0;
            double eps  = System.Math.Pow(2.0, -52.0);

            while (p > 0)
            {
                int k, kase;

                // Here is where a test for too many iterations would go.

                // This section of the program inspects for
                // negligible elements in the s and e arrays.  On
                // completion the variables kase and k are set as follows.

                // kase = 1     if s(p) and e[k-1] are negligible and k<p
                // kase = 2     if s(k) is negligible and k<p
                // kase = 3     if e[k-1] is negligible, k<p, and
                //              s(k), ..., s(p) are not negligible (qr step).
                // kase = 4     if e(p-1) is negligible (convergence).

                for (k = p - 2; k >= -1; k--)
                {
                    if (k == -1)
                    {
                        break;
                    }
                    if (Math.Abs(e[k]) > eps * (Math.Abs(s[k]) + Math.Abs(s[k + 1])))
                    {
                        continue;
                    }
                    e[k] = 0.0;
                    break;
                }
                if (k == p - 2)
                {
                    kase = 4;
                }
                else
                {
                    int ks;
                    for (ks = p - 1; ks >= k; ks--)
                    {
                        if (ks == k)
                        {
                            break;
                        }
                        double t = (ks != p?Math.Abs(e[ks]):0.0) + (ks != k + 1?Math.Abs(e[ks - 1]):0.0);
                        if (Math.Abs(s[ks]) > eps * t)
                        {
                            continue;
                        }
                        s[ks] = 0.0;
                        break;
                    }
                    if (ks == k)
                    {
                        kase = 3;
                    }
                    else if (ks == p - 1)
                    {
                        kase = 1;
                    }
                    else
                    {
                        kase = 2;
                        k    = ks;
                    }
                }
                k++;

                // Perform the task indicated by kase.

                switch (kase)
                {
                // Deflate negligible s(p).
                case 1:
                {
                    double f = e[p - 2];
                    e[p - 2] = 0.0;
                    for (int j = p - 2; j >= k; j--)
                    {
                        double t  = Fn.Hypot(s[j], f);
                        double cs = s[j] / t;
                        double sn = f / t;
                        s[j] = t;
                        if (j != k)
                        {
                            f        = (-sn) * e[j - 1];
                            e[j - 1] = cs * e[j - 1];
                        }
                        if (wantv)
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = cs * V[i, j] + sn * V[i, p - 1];
                                V[i, p - 1] = (-sn) * V[i, j] + cs * V[i, p - 1];
                                V[i, j]     = t;
                            }
                        }
                    }
                }
                break;

                // Split at negligible s(k).


                case 2:
                {
                    double f = e[k - 1];
                    e[k - 1] = 0.0;
                    for (int j = k; j < p; j++)
                    {
                        double t  = Fn.Hypot(s[j], f);
                        double cs = s[j] / t;
                        double sn = f / t;
                        s[j] = t;
                        f    = (-sn) * e[j];
                        e[j] = cs * e[j];
                        if (wantu)
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t           = cs * U[i, j] + sn * U[i, k - 1];
                                U[i, k - 1] = (-sn) * U[i, j] + cs * U[i, k - 1];
                                U[i, j]     = t;
                            }
                        }
                    }
                }
                break;

                // Perform one qr step.


                case 3:
                {
                    // Calculate the shift.

                    double scale = System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Abs(s[p - 1]), System.Math.Abs(s[p - 2])), System.Math.Abs(e[p - 2])), System.Math.Abs(s[k])), System.Math.Abs(e[k]));
                    double sp    = s[p - 1] / scale;
                    double spm1  = s[p - 2] / scale;
                    double epm1  = e[p - 2] / scale;
                    double sk    = s[k] / scale;
                    double ek    = e[k] / scale;
                    double b     = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
                    double c     = (sp * epm1) * (sp * epm1);
                    double shift = 0.0;
                    if ((b != 0.0) | (c != 0.0))
                    {
                        shift = System.Math.Sqrt(b * b + c);
                        if (b < 0.0)
                        {
                            shift = -shift;
                        }
                        shift = c / (b + shift);
                    }
                    double f = (sk + sp) * (sk - sp) + shift;
                    double g = sk * ek;

                    // Chase zeros.

                    for (int j = k; j < p - 1; j++)
                    {
                        double t  = Fn.Hypot(f, g);
                        double cs = f / t;
                        double sn = g / t;
                        if (j != k)
                        {
                            e[j - 1] = t;
                        }
                        f        = cs * s[j] + sn * e[j];
                        e[j]     = cs * e[j] - sn * s[j];
                        g        = sn * s[j + 1];
                        s[j + 1] = cs * s[j + 1];
                        if (wantv)
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = cs * V[i, j] + sn * V[i, j + 1];
                                V[i, j + 1] = (-sn) * V[i, j] + cs * V[i, j + 1];
                                V[i, j]     = t;
                            }
                        }
                        t        = Fn.Hypot(f, g);
                        cs       = f / t;
                        sn       = g / t;
                        s[j]     = t;
                        f        = cs * e[j] + sn * s[j + 1];
                        s[j + 1] = (-sn) * e[j] + cs * s[j + 1];
                        g        = sn * e[j + 1];
                        e[j + 1] = cs * e[j + 1];
                        if (!wantu || (j >= m - 1))
                        {
                            continue;
                        }
                        for (int i = 0; i < m; i++)
                        {
                            t           = cs * U[i, j] + sn * U[i, j + 1];
                            U[i, j + 1] = (-sn) * U[i, j] + cs * U[i, j + 1];
                            U[i, j]     = t;
                        }
                    }
                    e[p - 2] = f;
                    iter     = iter + 1;
                }
                break;

                // Convergence.


                case 4:
                {
                    // Make the singular values positive.

                    if (s[k] <= 0.0)
                    {
                        s[k] = (s[k] < 0.0?-s[k]:0.0);
                        if (wantv)
                        {
                            for (int i = 0; i <= pp; i++)
                            {
                                V[i, k] = -V[i, k];
                            }
                        }
                    }

                    // Order the singular values.

                    while (k < pp)
                    {
                        if (s[k] >= s[k + 1])
                        {
                            break;
                        }
                        double t = s[k];
                        s[k]     = s[k + 1];
                        s[k + 1] = t;
                        if (wantv && (k < n - 1))
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t = V[i, k + 1]; V[i, k + 1] = V[i, k]; V[i, k] = t;
                            }
                        }
                        if (wantu && (k < m - 1))
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t = U[i, k + 1]; U[i, k + 1] = U[i, k]; U[i, k] = t;
                            }
                        }
                        k++;
                    }
                    iter = 0;
                    p--;
                }
                break;
                }
            }

            // (vermorel) transposing the results if needed
            if (!transpose)
            {
                return;
            }
            // swaping U and V
            Matrix T = V;

            V = U;
            U = T;
        }