private void button3_Click(object sender, EventArgs e)
{
double[] arrDataA = new double[] { , };
double[,] arrDataB = new double[,]{ {,},
{,}
}; Matrix a = new Matrix(arrDataA);
Matrix b = new Matrix(arrDataA);
Matrix c;
c = a + b;
Console.WriteLine("A + B :" + c.ToString());
c = a - b;
Console.WriteLine("A - B :" + c.ToString());
Console.WriteLine("A == B :" + (a == b).ToString());
b = new Matrix(arrDataB);
Console.WriteLine("A == B :" + (a == b).ToString());
c = a * b;
Console.WriteLine("A * B :" + c.ToString());
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ProgressTest
{
/// <summary>
/// 矩阵类
/// </summary>
/// <remarks>
/// 孙继磊,2010-10-18
/// sun.j.l.studio@gmail.com
/// </remarks>
public sealed class Matrix
{
int row, column; //矩阵的行列数
double[,] data; //矩阵的数据 #region 构造函数
public Matrix(int rowNum, int columnNum)
{
row = rowNum;
column = columnNum;
data = new double[row, column];
}
public Matrix(double[,] members)
{
row = members.GetUpperBound() + ;
column = members.GetUpperBound() + ;
data = new double[row, column];
Array.Copy(members, data, row * column);
}
public Matrix(double[] vector)
{
row = ;
column = vector.GetUpperBound() + ;
data = new double[, column];
for (int i = ; i < vector.Length; i++)
{
data[, i] = vector[i];
}
}
#endregion #region 属性和索引器
public int rowNum { get { return row; } }
public int columnNum { get { return column; } } public double this[int r, int c]
{
get { return data[r, c]; }
set { data[r, c] = value; }
}
#endregion public override string ToString()
{
string strRet = "";
for(int i=;i<row;i++)
for (int j = ; j < column; j++)
{
strRet += data[i,j] + " , ";
}
return strRet;
}
#region 转置
/// <summary>
/// 将矩阵转置,得到一个新矩阵(此操作不影响原矩阵)
/// </summary>
/// <param name="input">要转置的矩阵</param>
/// <returns>原矩阵经过转置得到的新矩阵</returns>
public static Matrix transpose(Matrix input)
{
double[,] inverseMatrix = new double[input.column, input.row];
for (int r = ; r < input.row; r++)
for (int c = ; c < input.column; c++)
inverseMatrix[c, r] = input[r, c];
return new Matrix(inverseMatrix);
}
#endregion #region 得到行向量或者列向量
public Matrix getRow(int r)
{
if (r > row || r <= ) throw new Exception("没有这一行。");
double[] a = new double[column];
Array.Copy(data, column * (row - ), a, , column);
Matrix m = new Matrix(a);
return m;
}
public Matrix getColumn(int c)
{
if (c > column || c < ) throw new Exception("没有这一列。");
double[,] a = new double[row, ];
for (int i = ; i < row; i++)
a[i, ] = data[i, c];
return new Matrix(a);
}
#endregion #region 操作符重载 + - * / == !=
public static Matrix operator +(Matrix a, Matrix b)
{
if (a.row != b.row || a.column != b.column)
throw new Exception("矩阵维数不匹配。");
Matrix result = new Matrix(a.row, a.column);
for (int i = ; i < a.row; i++)
for (int j = ; j < a.column; j++)
result[i, j] = a[i, j] + b[i, j];
return result;
} public static Matrix operator -(Matrix a, Matrix b)
{
return a + b * (-);
} public static Matrix operator *(Matrix matrix, double factor)
{
Matrix result = new Matrix(matrix.row, matrix.column);
for (int i = ; i < matrix.row; i++)
for (int j = ; j < matrix.column; j++)
matrix[i, j] = matrix[i, j] * factor;
return matrix;
}
public static Matrix operator *(double factor, Matrix matrix)
{
return matrix * factor;
} //a 行元素 * b 列元素
//a 列数 == b行数
public static Matrix operator *(Matrix a, Matrix b)
{
if (a.column != b.row)
throw new Exception("矩阵维数不匹配。");
Matrix result = new Matrix(a.row, b.column);
for (int i = ; i < a.row; i++)
for (int j = ; j < b.column; j++)
for (int k = ; k < a.column; k++)
result[i, j] += a[i, k] * b[k, j]; return result;
}
public static bool operator ==(Matrix a, Matrix b)
{
if (object.Equals(a, b)) return true;
if (object.Equals(null, b))
return a.Equals(b);
return b.Equals(a);
}
public static bool operator !=(Matrix a, Matrix b)
{
return !(a == b);
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (!(obj is Matrix)) return false;
Matrix t = obj as Matrix;
if (row != t.row || column != t.column) return false;
return this.Equals(t, );
}
/// <summary>
/// 按照给定的精度比较两个矩阵是否相等
/// </summary>
/// <param name="matrix">要比较的另外一个矩阵</param>
/// <param name="precision">比较精度(小数位)</param>
/// <returns>是否相等</returns>
public bool Equals(Matrix matrix, int precision)
{
if (precision < ) throw new Exception("小数位不能是负数");
double test = Math.Pow(10.0, -precision);
if (test < double.Epsilon)
throw new Exception("所要求的精度太高,不被支持。");
for (int r = ; r < this.row; r++)
for (int c = ; c < this.column; c++)
if (Math.Abs(this[r, c] - matrix[r, c]) >= test)
return false; return true;
}
#endregion
}
}

C# 矩阵计算的更多相关文章

  1. [WebGL入门]十,矩阵计算和外部库

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外,鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家指 ...

  2. [图解tensorflow源码] 入门准备工作附常用的矩阵计算工具[转]

    [图解tensorflow源码] 入门准备工作 附常用的矩阵计算工具[转] Link: https://www.cnblogs.com/yao62995/p/5773142.html  tensorf ...

  3. C++ 矩阵计算库 :Eigen库

    Eigen http://eigen.tuxfamily.org/index.php?title=Main_Page 下载http://bitbucket.org/eigen/eigen/get/3. ...

  4. Eigen--简单的C++矩阵计算库

    晚上突然想写一段小C++程序,要用到矩阵求逆呀乘法呀之类的,所以找了一下有什么现成的可用的C++矩阵计算相关的库,发现有一大堆,在其中各种各样的配置,感觉比较麻烦.从方便性来说Eigen是最方便的了, ...

  5. opengles 矩阵计算

    总的变换矩阵: matrix = projection * view * model 模型矩阵: modelMatrix=translateMatrix * scaleMatrix * rotateM ...

  6. Scipy学习笔记 矩阵计算

    Scipy学习笔记 非本人原创  原链接 http://blog.sina.com.cn/s/blog_70586e000100moen.html 1.逆矩阵的求解 >>>impor ...

  7. Numpy中的矩阵计算

    矩阵初始化 支持matlab语句初始化,支持narray和array初始化. >>> import numpy as np >>> M = np.matrix(&q ...

  8. hdu 4920 Matrix multiplication (矩阵计算)

    题目链接 题意:给两个矩阵a, b, 计算矩阵a*b的结果对3取余. 分析:直接计算时间复杂度是O(n^3),会超时,但是下面第一个代码勉强可以水过,数据的原因. #include <iostr ...

  9. 基于visual Studio2013解决C语言竞赛题之1046矩阵计算

       题目 解决代码及点评 /************************************************************************/ /* 46 ...

随机推荐

  1. C#注册表常用操作

    1:加键 改值 Microsoft.Win32.RegistryKey Key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( @" ...

  2. (转) Tomcat部署Web应用方法总结

    原文:http://blog.csdn.net/yangxueyong/article/details/6130065 Tomcat部署Web应用方法总结 分类: Java web2011-01-11 ...

  3. mahout中kmeans算法和Canopy算法实现原理

    本文讲一下mahout中kmeans算法和Canopy算法实现原理. 一. Kmeans是一个很经典的聚类算法,我想大家都非常熟悉.虽然算法较为简单,在实际应用中却可以有不错的效果:其算法原理也决定了 ...

  4. 爬虫再探实战(三)———爬取动态加载页面——selenium

    自学python爬虫也快半年了,在目前看来,我面临着三个待解决的爬虫技术方面的问题:动态加载,多线程并发抓取,模拟登陆.目前正在不断学习相关知识.下面简单写一下用selenium处理动态加载页面相关的 ...

  5. GridView 分页方法

    要实现GrdView分页的功能. 操作如下: 1.更改GrdView控件的AllowPaging属性为true. 2.更改GrdView控件的PageSize属性为 任意数值(默认为10) 3.更改G ...

  6. HDU 1074

    http://acm.hdu.edu.cn/showproblem.php?pid=1074 每个任务有一个截止日期和完成时间,超过截止日期一天扣一分,问完成全部任务最少扣几分,并输出路径 最多15个 ...

  7. ASP.NET定制简单的错误处理页面

    通常Web应用程序在发布后,为了给用户一个友好界面和使用体验,都会在错误发生时跳转至一个自定义的错误页面,而不是ASP.net向用户暴露出来的详细的异常列表. 简单的错误处理页面可以通过web.con ...

  8. 用中文把玩Google开源的Deep-Learning项目word2vec

    google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算term之间的相似度,对term聚类等,该项目也支持phrase的自动识别,以及与t ...

  9. Debian 入门安装与配置1

    Debian 入门安装与配置1 最近安装了多个发行版本的Linux,包括Ubuntu.Fedora.Centos和Debian,发现只有Debian在界面和稳定性等综合特性上表现最优,自己也最喜欢,所 ...

  10. Think Python - Chapter 8 - Strings

    8.1 A string is a sequenceA string is a sequence of characters. You can access the characters one at ...