Esempio n. 1
0
        public static void squareConjugate(CMatrixRMaj mat)
        {
            int index     = 2;
            int rowStride = mat.getRowStride();
            int indexEnd  = rowStride;

            for (int i = 0;
                 i < mat.numRows;
                 i++, index += (i + 1) * 2, indexEnd += rowStride)
            {
                mat.data[index - 1] = -mat.data[index - 1];

                int indexOther = (i + 1) * rowStride + i * 2;
                for (; index < indexEnd; index += 2, indexOther += rowStride)
                {
                    float real = mat.data[index];
                    float img  = mat.data[index + 1];

                    mat.data[index]          = mat.data[indexOther];
                    mat.data[index + 1]      = -mat.data[indexOther + 1];
                    mat.data[indexOther]     = real;
                    mat.data[indexOther + 1] = -img;
                }
            }
        }
        //@Override
        public override void invert(CMatrixRMaj A_inv)
        {
            float[]     vv = decomp._getVV();
            CMatrixRMaj LU = decomp.getLU();

            if (A_inv.numCols != LU.numCols || A_inv.numRows != LU.numRows)
            {
                throw new ArgumentException("Unexpected matrix dimension");
            }

            int n = A.numCols;

            float[] dataInv    = A_inv.data;
            int     strideAinv = A_inv.getRowStride();

            for (int j = 0; j < n; j++)
            {
                // don't need to change inv into an identity matrix before hand
                Array.Clear(vv, 0, n * 2);
                vv[j * 2]     = 1;
                vv[j * 2 + 1] = 0;

                decomp._solveVectorInternal(vv);
//            for( int i = 0; i < n; i++ ) dataInv[i* n +j] = vv[i];
                int index = j * 2;
                for (int i = 0; i < n; i++, index += strideAinv)
                {
                    dataInv[index]     = vv[i * 2];
                    dataInv[index + 1] = vv[i * 2 + 1];
                }
            }
        }
Esempio n. 3
0
        /**
         * Computes the quality of a triangular matrix, where the quality of a matrix
         * is defined in {@link LinearSolverDense#quality()}.  In
         * this situation the quality is the magnitude of the product of
         * each diagonal element divided by the magnitude of the largest diagonal element.
         * If all diagonal elements are zero then zero is returned.
         *
         * @return the quality of the system.
         */
        public static float qualityTriangular(CMatrixRMaj T)
        {
            int N = Math.Min(T.numRows, T.numCols);

            float max = elementDiagMaxMagnitude2(T);

            if (max == 0.0f)
            {
                return(0.0f);
            }

            max = (float)Math.Sqrt(max);
            int rowStride = T.getRowStride();

            float qualityR = 1.0f;
            float qualityI = 0.0f;

            for (int i = 0; i < N; i++)
            {
                int index = i * rowStride + i * 2;

                float real      = T.data[index] / max;
                float imaginary = T.data[index] / max;

                float r   = qualityR * real - qualityI * imaginary;
                float img = qualityR * imaginary + real * qualityI;

                qualityR = r;
                qualityI = img;
            }

            return((float)Math.Sqrt(qualityR * qualityR + qualityI * qualityI));
        }
Esempio n. 4
0
        /**
         * Sets all the diagonal elements equal to one and everything else equal to zero.
         * If this is a square matrix then it will be an identity matrix.
         *
         * @param mat A square matrix.
         */
        public static void setIdentity(CMatrixRMaj mat)
        {
            int width = mat.numRows < mat.numCols ? mat.numRows : mat.numCols;

            Array.Clear(mat.data, 0, mat.getDataLength());

            int index  = 0;
            int stride = mat.getRowStride();

            for (int i = 0; i < width; i++, index += stride + 2)
            {
                mat.data[index] = 1;
            }
        }
Esempio n. 5
0
        /**
         * A straight forward transpose.  Good for small non-square matrices.
         *
         * @param A Original matrix.  Not modified.
         * @param A_tran Transposed matrix.  Modified.
         */
        public static void standard(CMatrixRMaj A, CMatrixRMaj A_tran)
        {
            int index         = 0;
            int rowStrideTran = A_tran.getRowStride();
            int rowStride     = A.getRowStride();

            for (int i = 0; i < A_tran.numRows; i++)
            {
                int index2 = i * 2;

                int end = index + rowStrideTran;
                while (index < end)
                {
                    A_tran.data[index++] = A.data[index2];
                    A_tran.data[index++] = A.data[index2 + 1];
                    index2 += rowStride;
                }
            }
        }
Esempio n. 6
0
        //@Override
        public override void solve(CMatrixRMaj b, CMatrixRMaj x)
        {
            if (b.numCols != x.numCols || b.numRows != numRows || x.numRows != numCols)
            {
                throw new ArgumentException("Unexpected matrix size");
            }

            int bnumCols = b.numCols;
            int bstride  = b.getRowStride();

            float[] dataB = b.data;
            float[] dataX = x.data;

            float[] vv = decomp._getVV();

//        for( int j = 0; j < numCols; j++ ) {
//            for( int i = 0; i < this.numCols; i++ ) vv[i] = dataB[i*numCols+j];
//            decomp._solveVectorInternal(vv);
//            for( int i = 0; i < this.numCols; i++ ) dataX[i*numCols+j] = vv[i];
//        }
            for (int j = 0; j < bnumCols; j++)
            {
                int index = j * 2;
                for (int i = 0; i < numRows; i++, index += bstride)
                {
                    vv[i * 2]     = dataB[index];
                    vv[i * 2 + 1] = dataB[index + 1];
                }
                decomp._solveVectorInternal(vv);
                index = j * 2;
                for (int i = 0; i < numRows; i++, index += bstride)
                {
                    dataX[index]     = vv[i * 2];
                    dataX[index + 1] = vv[i * 2 + 1];
                }
            }
        }
Esempio n. 7
0
        /**
         * <p>
         * Returns the magnitude squared of the complex element along the diagonal with the largest magnitude<br>
         * <br>
         * Max{ |a<sub>ij</sub>|^2 } for all i and j<br>
         * </p>
         *
         * @param a A matrix. Not modified.
         * @return The max magnitude squared
         */
        public static float elementDiagMaxMagnitude2(CMatrixRMaj a)
        {
            int size = Math.Min(a.numRows, a.numCols);

            int   rowStride = a.getRowStride();
            float max       = 0;

            for (int i = 0; i < size; i++)
            {
                int index = i * rowStride + i * 2;

                float real      = a.data[index];
                float imaginary = a.data[index + 1];

                float m = real * real + imaginary * imaginary;

                if (m > max)
                {
                    max = m;
                }
            }

            return(max);
        }