示例#1
0
        /**
         * 实现矩阵的乘法
         *
         * @param other - 与指定矩阵相乘的矩阵
         * @return Matrix型,指定矩阵与other相乘之积
         * @如果矩阵的行/列数不匹配,则会抛出异常
         */
        public Matrix Multiply(Matrix other)
        {
            // 首先检查行列数是否符合要求
            if (numColumns != other.GetNumRows())
            {
                throw new Exception("矩阵的行/列数不匹配。");
            }

            // ruct the object we are going to return
            Matrix result = new Matrix(numRows, other.GetNumColumns());

            // 矩阵乘法,即
            //
            // [A][B][C]   [G][H]     [A*G + B*I + C*K][A*H + B*J + C*L]
            // [D][E][F] * [I][J] =   [D*G + E*I + F*K][D*H + E*J + F*L]
            //             [K][L]
            //
            double value;

            for (int i = 0; i < result.GetNumRows(); ++i)
            {
                for (int j = 0; j < other.GetNumColumns(); ++j)
                {
                    value = 0.0;
                    for (int k = 0; k < numColumns; ++k)
                    {
                        value += GetElement(i, k) * other.GetElement(k, j);
                    }

                    result.SetElement(i, j, value);
                }
            }

            return(result);
        }
示例#2
0
        /**
         * 判断矩阵否相等
         *
         * @param other - 用于比较的矩阵
         * @return bool 型,两个矩阵相等则为true,否则为false
         */
        public override bool Equals(object other)
        {
            Matrix matrix = other as Matrix;

            if (matrix == null)
            {
                return(false);
            }

            // 首先检查行列数是否相等
            if (numColumns != matrix.GetNumColumns() || numRows != matrix.GetNumRows())
            {
                return(false);
            }

            for (int i = 0; i < numRows; ++i)
            {
                for (int j = 0; j < numColumns; ++j)
                {
                    if (Math.Abs(GetElement(i, j) - matrix.GetElement(i, j)) > eps)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#3
0
 /**
  * 拷贝构造函数
  *
  * @param other - 源矩阵
  */
 public Matrix(Matrix other)
 {
     numColumns = other.GetNumColumns();
     numRows    = other.GetNumRows();
     Init(numRows, numColumns);
     SetData(other.elements);
 }
示例#4
0
        /**
         * 给矩阵赋值
         *
         * @param other - 用于给矩阵赋值的源矩阵
         * @return Matrix型,阵与other相等
         */
        public Matrix SetValue(Matrix other)
        {
            if (other != this)
            {
                Init(other.GetNumRows(), other.GetNumColumns());
                SetData(other.elements);
            }

            // finally return a reference to ourselves
            return(this);
        }
示例#5
0
        /**
         * 实现矩阵的减法
         *
         * @param other - 与指定矩阵相减的矩阵
         * @return Matrix型,指定矩阵与other相减之差
         * @如果矩阵的行/列数不匹配,则会抛出异常
         */
        public Matrix Subtract(Matrix other)
        {
            if (numColumns != other.GetNumColumns() ||
                numRows != other.GetNumRows())
            {
                throw new Exception("矩阵的行/列数不匹配。");
            }

            // 构造结果矩阵
            Matrix result = new Matrix(this);           // 拷贝构造

            // 进行减法操作
            for (int i = 0; i < numRows; ++i)
            {
                for (int j = 0; j < numColumns; ++j)
                {
                    result.SetElement(i, j, result.GetElement(i, j) - other.GetElement(i, j));
                }
            }

            return(result);
        }
示例#6
0
        /**
         * 实现矩阵的加法
         *
         * @param other - 与指定矩阵相加的矩阵
         * @return Matrix型,指定矩阵与other相加之和
         * @如果矩阵的行/列数不匹配,则会抛出异常
         */
        public Matrix Add(Matrix other)
        {
            // 首先检查行列数是否相等
            if (numColumns != other.GetNumColumns() ||
                numRows != other.GetNumRows())
            {
                throw new Exception("矩阵的行/列数不匹配。");
            }

            // 构造结果矩阵
            Matrix result = new Matrix(this);           // 拷贝构造

            // 矩阵加法
            for (int i = 0; i < numRows; ++i)
            {
                for (int j = 0; j < numColumns; ++j)
                {
                    result.SetElement(i, j, result.GetElement(i, j) + other.GetElement(i, j));
                }
            }

            return(result);
        }