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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calculation of covariance matrix and return Matrix type
        /// </summary>
        /// <param name="Xi">arbitrary number of vectors </param>
        /// <returns></returns>
        public static double[,] CovMatrix(IList <double[]> Xi)
        {
            if (Xi == null || Xi.Count < 2)
            {
                throw new Exception("'data' cannot be null or less than 4 elements!");
            }
            //
            double[,] matrix = new double[Xi.Count, Xi.Count];
            //
            for (int i = 0; i < Xi.Count; i++)
            {
                for (int j = 0; j < Xi.Count; j++)
                {
                    if (i > j)
                    {
                        matrix[i, j] = matrix[j, i];
                    }
                    else if (i == j)
                    {
                        matrix[i, j] = VarianceOfS(Xi[i]);
                    }
                    else
                    {
                        matrix[i, j] = Covariance(Xi[i], Xi[j]);
                    }
                }
            }

            //inverse matrix
            try
            {
                var covMat = matrix.Invert();
                return(covMat);
            }
            catch
            {
                return(MatrixEx.Identity(Xi.Count, Xi.Count));
            }
        }