矩阵乘法计算脚本代码(C#)
来源:互联网 发布:长沙软件外包 编辑:程序博客网 时间:2024/05/17 08:51
代码:
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(); }}运行截图:
0 0
- 矩阵乘法计算脚本代码(C#)
- 矩阵乘法(C语言)
- 高精度乘法计算 poj1001 Exponentiation C代码
- C++计算矩阵乘法
- 转置矩阵的分块并行乘法(C语言实现),计算矩阵C[rawn][rawn]=A[rawm][rawn]'*B[rawm][rawn],子块大小为S*T,其算法实现原理参加本代码的附件。
- DP—矩阵链乘法(代码)
- 矩阵快速乘法---代码
- 一个矩阵乘法代码
- C语言矩阵乘法(指针实现)
- C语言矩阵乘法
- 矩阵乘法计算量估算
- 矩阵乘法的并行计算
- 常用矩阵计算C语言代码
- C语言 乘法计算
- calc 计算 题解(矩阵乘法优化动态规划)
- java 多线程并行计算之矩阵乘法(星星笔记)
- 华为OJ(矩阵乘法计算量估计)
- 【codevs1287】矩阵乘法(矩阵乘法)
- coderforce#382div2
- 宽高的整理
- ftp传送文件
- sql之left join、right join、inner join的区别
- WEB架构师成长系列索引
- 矩阵乘法计算脚本代码(C#)
- Android异步处理二:使用AsyncTask异步更新UI界面
- 文章标题
- 吕旭军:如何打造区块链数字资产交易?
- JMS之activeMQ使用介绍(一)
- iOS 合并多张图片并返回base64
- linux后台运行程序(secureCRT断掉或关闭后继续运行程序)
- 文本表示方法
- Docker快速安装部署