矩阵乘法计算脚本代码(C#)

代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;


class Script
{
    public class Matrix
    {
        public List<List<int>> array = null;
        public int Row
        {
            get
            {
                return array.Count;
            }
        }
        public int cln
        {
            get
            {
                return array[0].Count;
            }
        }
        public Matrix(List<List<int>> d)
        {
            array = d;
        }
        public Matrix(int row, int cln)
        {
            array = new List<List<int>>(row);
            for (int i = 0; i < row; i++)
            {
                List<int> c = new List<int>();
                array.Add(c);
                for (int j = 0; j < cln; j++)
                {
                    array[i].Add(0);
                }
            }
        }


        static private int js(ref List<List<int>> b1, ref List<List<int>> b2, int i, int j)
        {
            int s = 0;


            for (int q = 0; q < b1[0].Count; q++)
            {
                s += b1[i][q] * b2[q][j];
            }
            return s;
        }
        static public Matrix operator *(Matrix m1, Matrix m2)
        {
            if (m1.cln != m2.Row)
            {
                throw new System.ArgumentOutOfRangeException("行列式不符合规范");
            }


            int row = m1.Row, cln = m2.cln;
            Matrix res = new Matrix(row, cln);


            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < cln; j++)
                {
                    res.array[i][j] = js(ref m1.array, ref m2.array, i, j);
                }
            }
            return res;
        }
        static public Matrix operator *(int l, Matrix m2)
        {
            int row = m2.Row, cln = m2.cln;
            Matrix res = new Matrix(row, cln);


            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < cln; j++)
                {
                    res.array[i][j] = l * m2.array[i][j];
                }
            }
            return res;
        }
        static public Matrix operator *(Matrix m2, int l)
        {
            int row = m2.Row, cln = m2.cln;
            Matrix res = new Matrix(row, cln);


            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < cln; j++)
                {
                    res.array[i][j] = l * m2.array[i][j];
                }
            }
            return res;
        }


        static public void Show(Matrix m)
        {
            for (int i = 0; i < m.Row; i++)
            {
                for (int j = 0; j < m.cln; j++)
                {
                    Console.Write("{0},", m.array[i][j]);
                }
                Console.WriteLine("\n");
            }
        }
        /// <summary>
        /// 矩阵转置操作
        /// </summary>
        /// <returns></returns>
        public void Transport()
        {
            int temp;
            for (int i = 0; i < this.cln; i++)
            {
                for (int j = 0; j < this.Row; j++)
                {
                    temp = this.array[i][j];
                    this.array[i][j] = this.array[j][i];
                    this.array[j][i] = temp;
                }
            }
        }
        /// <summary>
        /// 返回转置矩阵
        /// </summary>
        /// <param name="m">原矩阵</param>
        /// <returns>返原矩阵的转置矩阵</returns>
        static public Matrix TransportNew(Matrix m)
        {
            Matrix res = new Matrix(m.cln, m.Row);
            for (int i = 0; i < res.Row; i++)
            {
                for (int j = 0; j < res.cln; j++)
                {
                    res.array[i][j] = m.array[j][i];
                }
            }
            return res;
        }
        public void Show()
        {
            Matrix.Show(this);
        }
    }


    [STAThread]
    static public void Main(string[] args)
    {
        Matrix m1 = new Matrix(new List<List<int>>() { new List<int>() { 1, 2, 0 }, new List<int>() { 3, -1, 1 } });
        //Matrix m2 = new Matrix(new List<List<int>>() { new List<int>() { 4, 1, 0 }, new List<int>() { -1, 1, 3 }, new List<int>() { 2, 0, 1 }, new List<int>() { 1, 3, 4 } });
        //Matrix m1 = new Matrix(new List<List<int>>() { new List<int>() { -2, 4 }, new List<int>() { 1, -2 } });


        //Matrix m2 = new Matrix(new List<List<int>>() { new List<int>() { 1, 0 }, new List<int>() { 0, 1 } });
        //Matrix resM1M2 = m1 * m2;
        //Matrix resM2M1 = m2 * m1;
        //Matrix res3M1 = m1 * 3;
        Console.WriteLine("m1:\n");
        Matrix.Show(m1);


        Console.WriteLine("m1的转置矩阵:\n");
        Matrix m1T = Matrix.TransportNew(m1);
        Matrix.Show(m1T*m1);
        //Console.WriteLine("res M1*M2:\n");
        //Matrix.Show(resM1M2);
        //Console.WriteLine("res M2*M1:\n");
        //Matrix.Show(resM2M1);
        Console.ReadLine();
    }
}
运行截图:



猜你喜欢

转载自blog.csdn.net/snikeguo/article/details/53381345