/// <inheritdoc/>
        /// <exception cref="NumberIsTooLargeException"> if <c>m</c> is an
        /// <c>OpenMapRealMatrix</c>, and the total number of entries of the product
        /// is larger than <c>Int32.MaxValue</c>.</exception>
        public new RealMatrix multiply(RealMatrix m)
        {
            try
            {
                return(multiply((OpenMapRealMatrix)m));
            }
            catch (InvalidCastException)
            {
                MatrixUtils.checkMultiplicationCompatible(this, m);

                int             outCols = m.getColumnDimension();
                BlockRealMatrix outp    = new BlockRealMatrix(rows, outCols);
                for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.MoveNext();)
                {
                    double value = iterator.Current.Value;
                    int    key   = iterator.Current.Key;
                    int    i     = key / columns;
                    int    k     = key % columns;
                    for (int j = 0; j < outCols; ++j)
                    {
                        outp.addToEntry(i, j, value * m.getEntry(k, j));
                    }
                }
                return(outp);
            }
        }
Ejemplo n.º 2
0
        /**
         * {@inheritDoc}
         *
         * @throws NumberIsTooLargeException if {@code m} is an
         * {@code OpenMapRealMatrix}, and the total number of entries of the product
         * is larger than {@code Integer.MAX_VALUE}.
         */
        public override RealMatrix multiply(RealMatrix m)
        {
            try {
                return(multiply((OpenMapRealMatrix)m));
            } catch (InvalidCastException cce) {
                //MatrixUtils.checkMultiplicationCompatible(this, m);

                int             outCols = m.getColumnDimension();
                BlockRealMatrix @out    = new BlockRealMatrix(rows, outCols);
                for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();)
                {
                    iterator.advance();
                    double value = iterator.value();
                    int    key   = iterator.key();
                    int    i     = key / columns;
                    int    k     = key % columns;
                    for (int j = 0; j < outCols; ++j)
                    {
                        @out.addToEntry(i, j, value * m.getEntry(k, j));
                    }
                }

                return(@out);
            }
        }
        public RealMatrix ComputeCorrelationMatrix(RealMatrix matrix)
        {
            CheckSufficientData(matrix);
            int        nVars     = matrix.getColumnDimension();
            RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);

            for (int i = 0; i < nVars; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    double corr = Correlation(matrix.getColumn(i), matrix.getColumn(j));
                    outMatrix.setEntry(i, j, corr);
                    outMatrix.setEntry(j, i, corr);
                }
                outMatrix.setEntry(i, i, 1.0D);
            }
            return(outMatrix);
        }
        public RealMatrix CovarianceToCorrelation(RealMatrix covarianceMatrix)
        {
            int        nVars     = covarianceMatrix.getColumnDimension();
            RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);

            for (int i = 0; i < nVars; i++)
            {
                double sigma = Math.Sqrt(covarianceMatrix.getEntry(i, i));
                outMatrix.setEntry(i, i, 1.0D);
                for (int j = 0; j < i; j++)
                {
                    double entry = covarianceMatrix.getEntry(i, j) / (sigma * Math.Sqrt(covarianceMatrix.getEntry(j, j)));
                    outMatrix.setEntry(i, j, entry);
                    outMatrix.setEntry(j, i, entry);
                }
            }
            return(outMatrix);
        }
Ejemplo n.º 5
0
        protected RealMatrix ComputeCovarianceMatrix(RealMatrix matrix, bool biasCorrected) //throws MathIllegalArgumentException
        {
            int        dimension = matrix.getColumnDimension();
            Variance   variance  = new Variance(biasCorrected);
            RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);

            for (int i = 0; i < dimension; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    double cov = GetCovarianve(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
                    outMatrix.setEntry(i, j, cov);
                    outMatrix.setEntry(j, i, cov);
                }
                outMatrix.setEntry(i, i, variance.Evaluate(matrix.getColumn(i)));
            }
            return(outMatrix);
        }
Ejemplo n.º 6
0
            /// <inheritdoc/>
            public RealMatrix solve(RealMatrix b)
            {
                int n = qrt.Length;
                int m = qrt[0].Length;

                if (b.getRowDimension() != m)
                {
                    throw new DimensionMismatchException(b.getRowDimension(), m);
                }
                if (!isNonSingular())
                {
                    throw new SingularMatrixException();
                }

                int columns   = b.getColumnDimension();
                int blockSize = BlockRealMatrix.BLOCK_SIZE;
                int cBlocks   = (columns + blockSize - 1) / blockSize;

                double[][] xBlocks = BlockRealMatrix.createBlocksLayout(n, columns);
                double[][] y       = new double[b.getRowDimension()][];
                double[]   alpha   = new double[blockSize];

                for (int kBlock = 0; kBlock < cBlocks; ++kBlock)
                {
                    int kStart = kBlock * blockSize;
                    int kEnd   = FastMath.min(kStart + blockSize, columns);
                    int kWidth = kEnd - kStart;

                    // get the right hand side vector
                    b.copySubMatrix(0, m - 1, kStart, kEnd - 1, y);

                    // apply Householder transforms to solve Q.y = b
                    for (int minor = 0; minor < FastMath.min(m, n); minor++)
                    {
                        double[] qrtMinor = qrt[minor];
                        double   factor   = 1.0 / (rDiag[minor] * qrtMinor[minor]);

                        for (int i = 0; i < kWidth; ++i)
                        {
                            alpha[i] = 0.0d;
                        }
                        for (int row = minor; row < m; ++row)
                        {
                            double   d    = qrtMinor[row];
                            double[] yRow = y[row];
                            for (int k = 0; k < kWidth; ++k)
                            {
                                alpha[k] += d * yRow[k];
                            }
                        }
                        for (int k = 0; k < kWidth; ++k)
                        {
                            alpha[k] *= factor;
                        }

                        for (int row = minor; row < m; ++row)
                        {
                            double   d    = qrtMinor[row];
                            double[] yRow = y[row];
                            for (int k = 0; k < kWidth; ++k)
                            {
                                yRow[k] += alpha[k] * d;
                            }
                        }
                    }

                    // solve triangular system R.x = y
                    for (int j = rDiag.Length - 1; j >= 0; --j)
                    {
                        int      jBlock = j / blockSize;
                        int      jStart = jBlock * blockSize;
                        double   factor = 1.0 / rDiag[j];
                        double[] yJ     = y[j];
                        double[] xBlock = xBlocks[jBlock * cBlocks + kBlock];
                        int      index  = (j - jStart) * kWidth;
                        for (int k = 0; k < kWidth; ++k)
                        {
                            yJ[k]          *= factor;
                            xBlock[index++] = yJ[k];
                        }

                        double[] qrtJ = qrt[j];
                        for (int i = 0; i < j; ++i)
                        {
                            double   rIJ = qrtJ[i];
                            double[] yI  = y[i];
                            for (int k = 0; k < kWidth; ++k)
                            {
                                yI[k] -= yJ[k] * rIJ;
                            }
                        }
                    }
                }

                return(new BlockRealMatrix(n, columns, xBlocks, false));
            }