Exemplo n.º 1
0
        /// <summary>
        ///  Accord.NET
        ///   Computes the product A*B of matrix <c>A</c> and diagonal matrix <c>B</c>.
        /// </summary>
        ///
        /// <param name="a">The left matrix <c>A</c>.</param>
        /// <param name="diagonal">The diagonal vector of right matrix <c>B</c>.</param>
        /// <param name="result">The matrix <c>R</c> to store the product <c>R = A*B</c>
        ///   of the given matrices <c>A</c> and <c>B</c>.</param>
        ///
        public static double[,] DotWithDiagonal(this double[,] a, double[] diagonal, double[,] result)
        {
#if DEBUG
            var C = Dot(a.To <double[, ]>(), MatrixEx.Diagonal(diagonal.To <double[]>()));
#endif
            int rows = a.Rows();

            unsafe
            {
                fixed(double *ptrA = a)
                fixed(double *ptrR = result)
                {
                    double *A = ptrA;
                    double *R = ptrR;

                    for (int i = 0; i < rows; i++)
                    {
                        for (int j = 0; j < diagonal.Length; j++)
                        {
                            *R++ = (double)((double)(*A++) * (double)diagonal[j]);
                        }
                    }
                }
            }
#if DEBUG
            if (!MatrixEx.IsEqual(C, result.To <double[, ]>(), 1e-4))
            {
                throw new Exception();
            }
#endif
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Accord.NET
        ///   Computes the product <c>A*B'</c> of matrix <c>A</c> and transpose of <c>B</c>.
        /// </summary>
        ///
        /// <param name="a">The left matrix <c>A</c>.</param>
        /// <param name="b">The transposed right matrix <c>B</c>.</param>
        ///
        /// <returns>The product <c>A*B'</c> of the given matrices <c>A</c> and <c>B</c>.</returns>
        ///
        public static double[,] DotWithTransposed(this double[,] a, double[,] b, double[,] result)
        {
#if DEBUG
            if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows())
            {
                throw new Exception("Dimension mismatch!");
            }
            //
            var C = MatrixEx.Dot(a.To <double[, ]>(), b.Transpose().To <double[, ]>());
#endif
            int n = a.Columns();
            int m = a.Rows();
            int p = b.Rows();

            unsafe
            {
                fixed(double *A = a)
                fixed(double *B = b)
                fixed(double *R = result)
                {
                    double *pr = R;

                    for (int i = 0; i < m; i++)
                    {
                        double *pb = B;
                        for (int j = 0; j < p; j++, pr++)
                        {
                            double *pa = A + n * i;

                            double s = (double)0;
                            for (int k = 0; k < n; k++)
                            {
                                s += (double)((double)(*pa++) * (double)(*pb++));
                            }
                            *pr = (double)s;
                        }
                    }
                }
            }
#if DEBUG
            if (!MatrixEx.IsEqual(C, result.To <double[, ]>(), 1e-4))
            {
                throw new Exception();
            }
#endif
            return(result);
        }