• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#编程建立泛型Matrix数据类型和对应处理方法

武飞扬头像
心网千结
帮助1

一、简介

        本文所描述的Matrix<T>是一个泛型,具有不同数值类型Matrix矩阵构造、新增、删除、查询、更改、深度复制、显示元素值、vector运算、扩展等功能。整体代码如下所示。

        另外,在编写Matrix<T>类时,为了调用Vector<T>类,对原先的Vector类代码,进行补充和修改。点击下载Matrix泛型dll库及介绍!

学新通

        注:Matrix表示矩阵类,matrix表示矩阵。

二、基本框架

2.1 建立Matrix<T>类

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
     
  6.  
    namespace XMU//.Base
  7.  
    {
  8.  
    public class Matrix<T> where T : struct
  9.  
    {
  10.  
     
  11.  
    }
  12.  
    }

        注:<T> where T : struct中,T表示泛型,是一个占位符,可以表示C#任何数据类型,包括自定义的类型,where T : struct 表示泛型的约束,约束泛型的类型为struct,及数值型。

2.2 Matrix成员

  1.  
    // 成员,存放数据
  2.  
    public T[,] _data;

2.3 Matrix属性

        Matrix只包含Row、Col、DataType(private)属性,分别表示矩阵的行数、列数、数据类型(int、double……)、数据类型的大小(eg:int型,对应sizeof(int))。

2.4 Matrix方法

        本博客撰写的Matrix类方法主要分为两种,一种是向量Matrix<T>的构造;一种是Matrix元素之间的操作,包括运算符( 、-、*、/、>、>=、<、<=、==、!=)重载、行/列提取、查询、扩展、求和、求最大值、求最小值、求均值、查找。

2.4.1 构造函数

        本文所写的Matrix构造函数有四种类型:构造空的matrix、根据指定维度构造全0或全1 matrix、根据数组构造matrix以及根据Vector构造matrix。

        为了便于显示Matrix元素,验证代码正确性,建议优先重写Tostring方法。

2.4.2 this[,]

        this[,] 可以根据中括号内的索引值,查询对应位置的元素。

2.4.3 Copy

        Copye方法可以在Matrix新增一个同类型的数或另一个Matrix,具体代码如下,运算符重载的基础,因此先介绍:

2.5 Matrix 运算符重载

2.5.1 正号

        " "正号运算符重载,返回一个全新的matrix矩阵,即每个元素乘一个 1。

函数原型:

public static Matrix<T> operator (Matrix<T> m1)

函数说明:用来重载正号运算符

名称

说明

m1

用来进行正号运算的matrix

2.5.2 -负号

        "-"正号运算符重载,返回一个全新的matrix矩阵,即每个元素乘一个-1。

函数原型:

public static Matrix<T> operator -(Matrix<T> m1)

函数说明:用来重载负号运算符

名称

说明

m1

用来进行负号运算的matrix

2.5.3 加法

        用于两个矩阵相加,有三种情况:矩阵 常数、常数 矩阵、矩阵 矩阵。

函数原型:

public static Matrix<T> operator (Matrix<T> m1, Matrix<T> m2)

public static Matrix<T> operator (Matrix<T> m1, T value)

public static Matrix<T> operator (T value, Matrix<T> m1)

函数说明:用来重载加法运算符

名称

说明

m1

加法左边的matrix

m2

加法右边的vector

value

用来与matrix相加的数

2.5.4 -减法

        同加法一样,用于两个矩阵相减,有三种形式:矩阵-常数、常数-矩阵、矩阵-矩阵,代码如下:

函数原型:

public static Matrix<T> operator -(Matrix<T> m1, Matrix<T> m2)

public static Matrix<T> operator -(Matrix<T> m1, T value)

public static Matrix<T> operator -(T value, Matrix<T> m1)

函数说明:用来重载减法运算符

名称

说明

v1

减法左边的matrix

v2

减法右边的matrix

value

用来与matrix相减的数

2.5.4 乘法

        同加减法一样,用于矩阵相乘,有三种情况:矩阵*常数、常数*矩阵、矩阵*矩阵。(注:矩阵点乘在后面讲解)。

函数原型:

public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2)

public static Matrix<T> operator *(Matrix<T> m1, T value)

public static Matrix<T> operator *(T value, Matrix<T> m1)

函数说明:用来重载乘法运算符

名称

说明

v1

乘法左边的matrix

v2

乘法右边的matrix

value

用来与matrix相乘的数

2.5.5 大于>

        大于>运算符用于比较常数与矩阵的比较,有三种情况:常数>矩阵、矩阵>常数、矩阵>矩阵。

        注:矩阵“>”必须与小于号“<”成对出现(重载)。

函数原型:

public static bool[,] operator >(Matrix<T> m1, T Value)

public static bool[,] operator >( T Value, Matrix<T> m1)

public static bool[,] operator >(Matrix<T> m1, Matrix<T> m2)

函数说明:用来比较矩阵与矩阵/数的大小

名称

说明

m1

matrix1

m2

matrix2

value

用来与matrix相比的数

2.5.6 小于<

        同大于号一样情况。

函数原型:

public static bool[,] operator <(Matrix<T> m1, T Value)

public static bool[,] operator <( T Value, Matrix<T> m1)

public static bool[,] operator <(Matrix<T> m1, Matrix<T> m2)

函数说明:用来比较矩阵与矩阵/数的大小

名称

说明

m1

matrix1

m2

matrix2

value

用来与matrix相比的数

2.5.7 大于等于

函数原型:

public static bool[,] operator >=(Matrix<T> m1, T Value)

public static bool[,] operator >=( T Value, Matrix<T> m1)

public static bool[,] operator >=(Matrix<T> m1, Matrix<T> m2)

函数说明:用来比较矩阵与矩阵/数的大小

名称

说明

m1

matrix1

m2

matrix2

value

用来与matrix相比的数

2.5.8小于等于

public static bool[,] operator <=(Matrix<T> m1, T Value)

public static bool[,] operator <=( T Value, Matrix<T> m1)

public static bool[,] operator <=(Matrix<T> m1, Matrix<T> m2)

函数说明:用来比较矩阵与矩阵/数的大小

名称

说明

m1

matrix1

m2

matrix2

value

用来与matrix相比的数

2.5.9 等于

函数原型:

public static bool[,] operator ==(Matrix<T> m1, T Value)

public static bool[,] operator ==( T Value, Matrix<T> m1)

public static bool[,] operator ==(Matrix<T> m1, Matrix<T> m2)

函数说明:用来比较矩阵与矩阵/数的大小

名称

说明

m1

matrix1

m2

matrix2

value

用来与matrix相比的数

2.5.10不等于

3.1.11 != 不等于

函数原型:

public static bool[,] operator !=(Matrix<T> m1, T Value)

public static bool[,] operator !=( T Value, Matrix<T> m1)

public static bool[,] operator !=(Matrix<T> m1, Matrix<T> m2)

函数说明:用来比较矩阵与矩阵/数的大小

名称

说明

m1

matrix1

m2

matrix2

value

用来与matrix相比的数

2.6 其它公共方法

2.6.1 GetRow

        获取矩阵某一行。

函数原型:

public Vector<T> GetRow(int rowIndex)

函数说明:该方法是用来在获取矩阵rowIndex对应的行。

名称

说明

rowIndex

用来提取的行索引

2.6.2 GetCol

        获取矩阵某一列。

函数原型:

public Vector<T> GetCol(int colIndex)

属性说明:该方法是用来在获取矩阵rowIndex对应的列。

名称

说明

rowIndex

用来提取的列索引

2.6.3 SetRow

        更改矩阵某一行数据。

函数原型:

public void SetRow(Vector<T> v, int rowIndex = 0)

函数说明:表示需要将v替换矩阵中rowIndex多对应的行。

名称

说明

v

需要替换的向量

rowIndex

表示需要替换的行,默认为0

2.6.4 SetCol

        更改矩阵某一列数据。

函数原型:

public void SetCol(Vector<T> v, int colIndex = 0)

函数说明:表示需要将v替换矩阵中rowIndex多对应的列。。

名称

说明

v

需要替换的向量

colIndex

表示需要替换的列,默认为0

2.6.5 Transposition

        将矩阵进行转置处理。

函数原型:

public Matrix<T> Transposition()

函数说明:表示矩阵的转置

2.5.6 ExtractRow

        根据多个int[]数组索引,提取矩阵对应的行。

函数原型:

public Matrix<T> ExtractRow(int[] indexs)

函数说明:根据int索引数组,提取矩阵的对应的所有行数。

名称

说明

indexs

int索引数组

2.5.7 ExtractCol

        根据多个int[]数组索引,提取矩阵对应的列。

函数原型:

public Matrix<T> ExtractCol(int[] indexs)

函数说明:根据int索引数组,提取矩阵的对应的所有列数。

名称

说明

indexs

int索引数组

2.5.8 DelRow

        根据int数、多个int[]数组索引,删除矩阵对应的行。

函数原型:

public Matrix<T> DelRow(params int[] indexs)

public Matrix<T> DelRow(int index)

函数说明:根据索引(数组),删除对应的行,得到一个新的矩阵。

名称

说明

indexs

需要删除的行索引数组

index

需要删除的行索引

2.5.9 DelCol

        根据int、多个int[]数组索引,删除矩阵对应的列。

函数原型:

public Matrix<T> DelCol(params int[] indexs)

ublic Matrix<T> DelRow(int index)

函数说明:根据索引(数组),删除对应的列,得到一个新的矩阵。

名称

说明

indexs

需要删除的列索引数组

index

需要删除的列索引

2.5.10 Unint

        与另一个矩阵进行行/列方向组合,默认行组合,默认rowUnion = true,表示行组合,反之,则表示列组合。

函数原型:

public Matrix<T> Unint(Matrix<T> m1, bool rowUnion = true)

函数说明:与另一个矩阵进行行/列方向组合,默认行组合

名称

说明

m1

需要组合的另一个矩阵

rowUnion

表示是否为行组合,默认为true;false表示列向组合

2.5.11 Repmat

        矩阵的行/列成倍数扩展,都默认为1倍,rowCopyX 表示行的倍数,colCopyX表示列的倍数。

函数原型:

public Matrix<T> Repmat(int rowCopyX = 1, int colCopyX = 1

函数说明:将矩阵按照行、列进行成倍数扩展

名称

说明

rowCopyX

行扩展的倍数,默认为1

colCopyX

列扩展的倍数,默认为1

2.5.12 Max

        求矩阵全域(maxType = 0)、每行(maxType = 1)、每列(maxType = 2)的最大值。

函数原型:

public Vector<T> Max(int maxType = 0)

函数说明:求全域最大值时,maxType=0(默认);求每行最大值时,maxType=1;求每列最大值时,maxType=2

名称

说明

maxType

求解最大值的类型,maxType = 0(默认)/1/2,分别表示全域、行、列方向最大值

2.5.13 Min

        求矩阵全域(minType = 0)、每行(minType = 1)、每列(minType = 2)的最小值。

函数原型:

public Vector<T> Min(int minType = 0)

函数说明:求全域最小值时,minType=0(默认);求每行最小值时,minType=1;求每列最小值时,minType=2

名称

说明

minType

求解最小值的类型,minType = 0(默认)/1/2,分别表示全域、行、列方向最小值

2.5.14 Sum

        求矩阵全域(sumType = 0)、每行(sumType = 1)、每列(sumType = 2)的和。

函数原型:

public Vector<T> Sum(int sumType = 0)

函数说明:求全域总和时,sumType=0(默认);求每行总和时,sumType=1;求每列总和时,sumType=2

名称

说明

sumType

求解总和的类型,sumType = 0(默认)/1/2,分别表示全域、行、列方向总和

2.5.15 Mean

        求矩阵全域(meanType = 0)、每行(meanType = 1)、每列(meanType = 2)的均值。

函数原型:

public Vector<T> Mean(int meanType = 0)

函数说明:求全域均值时,meanType=0(默认);求每行均值时,minType=1;求每列均值时,minType=2

名称

说明

meanType

求解均值的类型,meanType = 0(默认)/1/2,分别表示全域、行、列方向均值

 2.5.16 FindNumber

        求全域等于value的个数,meanType=0(默认);求每行等于value的个数,meanType=1;求每列等于value的个数,meanType=2。

函数原型:

public int[] FindNumber(T value, int findNumType = 0)

函数说明:根据findNumType的值,查找全域(findNumType=0)、每行(findNumType=1)或每列(findNumType=2)元素等于value的元素个数。

名称

说明

value

所需要查找的值

findNumType

寻找元素的方式,findNumType = 0(默认)/1/2,分别表示全域、行、列方向寻找

2.5.17 Find

        出所有元素等于value值的位置索引。

函数原型:

public int[,] Find(T value)

函数说明:查找全域元素等于value位置索引

名称

说明

value

需要查找的值

2.5.18 Data

        Data方法可以用来提取Matrix矩阵元素,或则说是量T 类型的Matrix转化T类型的数组。

函数原型:

public T[,] Data()

函数说明:将matrix元素提取出来,保存在T[,]数组上

三、验证

        调用的时候,将dll应用到工程文件的项目中,并添加:using XMU;

        可先下载dll库,里面包含了一个验证的program.cs文件。验证结果部分截图如下:

学新通

        代码暂不公开,需要的课下载封装的dll库,直接调用,若有不对或不足之处,敬请斧正。我将及时更新xmu.dll库。 

路漫漫其修远兮,吾将上下而求索!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfihjaa
系列文章
更多 icon
同类精品
更多 icon
继续加载