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);
        }