Esempio n. 1
0
        /// <summary>
        /// Linear algebraic matrix-matrix multiplication; <tt>C = alpha * A x B + beta*C</tt>.
        /// </summary>
        /// <param name="b">
        /// The second source matrix.
        /// </param>
        /// <param name="c">
        /// The matrix where results are to be storedd Set this parameter to <tt>null</tt> to indicate that a new result matrix shall be constructed.
        /// </param>
        /// <param name="alpha">
        /// The alpha.
        /// </param>
        /// <param name="beta">
        /// The beta.
        /// </param>
        /// <param name="transposeA">
        /// Whether A must be transposed.
        /// </param>
        /// <param name="transposeB">
        /// Whether B must be transposed.
        /// </param>
        /// <returns>
        /// C (for convenience only).
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <tt>B.rows() != A.columns()</tt>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <tt>C.rows() != A.rows() || C.columns() != B.columns()</tt>.
        /// </exception>
        /// <exception cref="ArithmeticException">
        /// If <tt>A == C || B == C</tt>.
        /// </exception>
        public virtual DoubleMatrix2D ZMult(DoubleMatrix2D b, DoubleMatrix2D c, double alpha, double beta, bool transposeA, bool transposeB)
        {
            if (transposeA)
            {
                return(ViewDice().ZMult(b, c, alpha, beta, false, transposeB));
            }
            if (transposeB)
            {
                return(ZMult(b.ViewDice(), c, alpha, beta, false, false));
            }

            int m = Rows;
            int n = Columns;
            int p = b.Columns;

            if (c == null)
            {
                c = new DenseDoubleMatrix2D(m, p);
            }
            if (b.Rows != n)
            {
                throw new ArgumentOutOfRangeException("b", String.Format(Cern.LocalizedResources.Instance().Exception_Matrix2DInnerDimensionMustAgree, this, b));
            }
            if (c.Rows != m || c.Columns != p)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleResultMatrix, this, b, c));
            }
            if (this == c || b == c)
            {
                throw new ArithmeticException(Cern.LocalizedResources.Instance().Exception_MatricesMustNotBeIdentical);
            }

            for (int j = p; --j >= 0;)
            {
                for (int i = m; --i >= 0;)
                {
                    double s = 0;
                    for (int k = n; --k >= 0;)
                    {
                        s += this[i, k] * b[k, j];
                    }
                    c[i, j] = (alpha * s) + (beta * c[i, j]);
                }
            }

            return(c);
        }
Esempio n. 2
0
        /// <summary>
        /// Linear algebraic matrix-matrix multiplication; <tt>C = alpha * A x B + beta*C</tt>.
        /// </summary>
        /// <param name="b">
        /// The second source matrix.
        /// </param>
        /// <param name="c">
        /// The matrix where results are to be storedd Set this parameter to <tt>null</tt> to indicate that a new result matrix shall be constructed.
        /// </param>
        /// <param name="alpha">
        /// The alpha.
        /// </param>
        /// <param name="beta">
        /// The beta.
        /// </param>
        /// <param name="transposeA">
        /// Whether A must be transposed.
        /// </param>
        /// <param name="transposeB">
        /// Whether B must be transposed.
        /// </param>
        /// <returns>
        /// C (for convenience only).
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <tt>B.rows() != A.columns()</tt>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <tt>C.rows() != A.rows() || C.columns() != B.columns()</tt>.
        /// </exception>
        /// <exception cref="ArithmeticException">
        /// If <tt>A == C || B == C</tt>.
        /// </exception>
        public virtual DoubleMatrix2D ZMult(DoubleMatrix2D b, DoubleMatrix2D c, double alpha, double beta, bool transposeA, bool transposeB)
        {
            if (transposeA)
            {
                return(ViewDice().ZMult(b, c, alpha, beta, false, transposeB));
            }
            if (transposeB)
            {
                return(ZMult(b.ViewDice(), c, alpha, beta, false, false));
            }

            int m = Rows;
            int n = Columns;
            int p = b.Columns;

            if (c == null)
            {
                c = new DenseDoubleMatrix2D(m, p);
            }
            if (b.Rows != n)
            {
                throw new ArgumentOutOfRangeException("b", "Matrix2D inner dimensions must agree:" + this + ", " + b);
            }
            if (c.Rows != m || c.Columns != p)
            {
                throw new ArgumentException("Incompatible result matrix: " + this + ", " + b + ", " + c);
            }
            if (this == c || b == c)
            {
                throw new ArithmeticException("Matrices must not be identical");
            }

            for (int j = p; --j >= 0;)
            {
                for (int i = m; --i >= 0;)
                {
                    double s = 0;
                    for (int k = n; --k >= 0;)
                    {
                        s += this[i, k] * b[k, j];
                    }
                    c[i, j] = (alpha * s) + (beta * c[i, j]);
                }
            }

            return(c);
        }