Ejemplo n.º 1
0
 public Matrix(Matrix matrix)
 {
     rows = matrix.x.GetLength(0);
     cols = matrix.x.Length / rows;
     x = new MNode[rows, cols];
     for (int i = 0; i < rows; i++)
     {
         for (int j = 0; j < cols; j++)
         {
             x[i, j] = new MNode();
             x[i, j].Copyfrom(matrix.x[i, j]);
         }
     }
 }
Ejemplo n.º 2
0
 private static double GetMarixMultValue(Matrix src, int row, Matrix des, int col)
 {
     double res = 0;
     for (int i = 0; i < src.Rows; i++)
     {
         res += src.x[row, i].Value * des.x[i, col].Value;
     }
     return res;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 矩阵的重定形
        /// </summary>
        /// <param name="marix">待定形矩阵</param>
        /// <param name="row">期望行数</param>
        /// <param name="col">期望列数</param>
        /// <returns>重定形之后的矩阵</returns>
        public static Matrix ReShape(Matrix matrix, int row, int col)
        {
            if (matrix.Rows * matrix.Cols != col * row)
            {
                System.Console.WriteLine("col * row isn't coincide to the array length");
                return null;
            }

            Matrix mat = new Matrix(col, row);
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    int r = ((i * (col - 1)) + j) / matrix.Cols;
                    int c = ((i * (col - 1)) + j) % matrix.Cols;
                    mat.x[i, j] = matrix.x[r, c];
                }
            }
            return mat;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 内部使用的矩阵生成式
 /// </summary>
 /// <param name="s"></param>
 /// <param name="value"></param>
 /// <param name="nodes"></param>
 /// <returns></returns>
 private static Matrix GenerateMatrix(int s, ArrayList value, MNode[,] nodes)
 {
     Matrix m = new Matrix(s, s);
     int x = 0;
     for (int i = 0; i < m.rows; i++)
     {
         for (int j = 0; j < m.cols; x++, j++)
         {
             m.x[i, j].Value = double.Parse(value[x].ToString());
             m.x[i, j].EName = nodes[i, j].EName;
             m.x[i, j].SName = nodes[i, j].SName;
         }
     }
     return m;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 从src到des的矩阵拷贝
 /// </summary>
 /// <param name="src">源</param>
 /// <param name="des">目的阵</param>
 public static void Copy(Matrix des, Matrix src)
 {
     for (int i = 0; i < des.Rows; i++)
     {
         for (int j = 0; j < des.Cols; j++)
         {
             des.x[i, j].Copyfrom(src.x[i, j]);
         }
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="matrix"></param>
 /// <returns></returns>
 //private MNode[] MatrixToArray(Matrix matrix)
 //{
 //    MNode[] array = new MNode[matrix.Rows * matrix.Cols];
 //    int m = 0;
 //    for (int i = 0; i < matrix.Rows; i++)
 //    {
 //        for (int j = 0; j < matrix.Cols; j++)
 //        {
 //            array[m++] = matrix.x[i, j];
 //        }
 //    }
 //    return array;
 //}
 /// <summary>
 /// 
 /// </summary>
 /// <param name="array"></param>
 /// <returns></returns>
 public static Matrix ArrayToMatrix(MNode[] array, int col, int row)
 {
     if (array.Length != col * row)
     {
         System.Console.WriteLine("col * row isn't coincide to the array length");
         return null;
     }
     Matrix matrix = new Matrix(col, row);
     int m = 0;
     for (int i = 0; i < row; i++)
     {
         for (int j = 0; j < col; j++)
         {
             matrix.x[i, j] = array[m++];
         }
     }
     return matrix;
 }
Ejemplo n.º 7
0
 public static Matrix operator /(Matrix src, long n)
 {
     Matrix result = new Matrix(src);
     for (int i = 0; i < src.Rows; i++)
     {
         for (int j = 0; j < src.Cols; j++)
         {
             result.x[i, j].Value = src.x[i, j].Value / n;
         }
     }
     return result;
 }
Ejemplo n.º 8
0
 public static Matrix operator -(Matrix src, Matrix des)
 {
     Matrix result = new Matrix(src);
     for (int i = 0; i < src.Rows; i++)
     {
         for (int j = 0; j < src.Cols; j++)
         {
             result.x[i, j] = src.x[i, j] - des.x[i, j];
         }
     }
     return result;
 }
Ejemplo n.º 9
0
        public static Matrix operator *(Matrix src, Matrix des)
        {
            Matrix result = new Matrix(src);
            // 对于前置矩阵的每一行
            for (int i = 0; i < src.Rows; i++)
            {
                // 对于后置矩阵的每一列
                for (int j = 0; j < src.Cols; j++)
                {
                    result.x[i, j].SName = src.x[i, j].SName;
                    result.x[i, j].EName = src.x[i, j].EName;

                    result.x[i, j].Value = GetMarixMultValue(src, i, des, j);
                }
            }
            return result;
        }