📄 matrix.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace NumericRcipes
{
public sealed class NRVec//向量类定义
{
private int n;
private double[] data;
public int Rows { get { return n; } }
public double this[int i] { get { return data[i]; } set { data[i] = value; } }
public NRVec() { }//构造空向量
public NRVec(int nn) { n = nn; data = new double[n]; }//构造一个长度为nn的向量
public NRVec(double a, int nn)//构造一个长度为nn,元素为a的向量
{
n = nn; data = new double[n];
for (int i = 0; i < n; i++) data[i] = a;
}
public NRVec(double[] a)//由一维数组构造一个向量
{
n = a.GetLength(0);
data = new double[n];
a.CopyTo(data, 0);
}
public NRVec(NRVec rhs)//复制rhs向量
{
n = rhs.n;
data = new double[n];
rhs.data.CopyTo(data, 0);
}
public override string ToString()//向量的字符串输出
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++)
sb.AppendFormat("{0:F4}\n", data[i]);
return sb.ToString();
}
public void AssignTo(NRVec vec)//将向量赋值给vec
{
vec.n = n;
vec.data = new double[vec.n];
data.CopyTo(vec.data, 0);
}
public void AssignFrom(double a)//将a赋给向量的每个元素
{
for (int i = 0; i < n; i++) data[i] = a;
}
public static explicit operator double[](NRVec vec)//显示转换向量为一维数组
{
double[] result = new double[vec.n];
vec.data.CopyTo(result, 0);
return result;
}
public static explicit operator NRVec(double[] array)//显示转换一维数组为向量
{
NRVec result = new NRVec(array);
return result;
}
}
public sealed class NRMatrix : Object //矩阵类的定义
{
#region 矩阵的数据成员
int rows; //记录矩阵的行数
int cols; //记录矩阵的列数
double[,] data; //矩阵元素的存储数组
public int Rows //获取矩阵的行数
{
get { return rows; }
}
public int Cols //获取矩阵的列数
{
get { return cols; }
}
public double this[int i, int j]//获取和设置矩阵第i行第j列的元素
{
get
{
if (i >= rows || j >= cols || i < 0 || j < 0)
throw (new NRException("行标或列标越界,不能获取元素!"));
return data[i, j];
}
set
{
if (i >= rows || j >= cols || i < 0 || j < 0)
throw (new NRException("行标或列标越界,不能设置元素!"));
data[i, j] = value;
}
}
#endregion
#region 矩阵的构造函数
public NRMatrix() { }//构造空矩阵
public NRMatrix(int rows, int cols) //构造一个rows×cols的零矩阵
{
if (rows <= 0 || cols <= 0) throw (new NRException("不能生成少于1行1列的矩阵!"));
this.rows = rows; this.cols = cols;
data = new double[rows, cols];
}
public NRMatrix(double[,] a)//二维数组构造矩阵
{
rows = a.GetLength(0);
cols = a.GetLength(1);
data = new double[rows, cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
data[i, j] = a[i, j];
}
public NRMatrix(NRVec vec)//由向量vec生成对角矩阵
{
rows = vec.Rows;
cols = rows;
data = new double[rows, cols];
for (int i = 0; i < rows; i++)
data[i, i] = vec[i];
}
public NRMatrix(NRMatrix a)//复制a矩阵
{
rows = a.rows;
cols = a.cols;
data = new double[rows, cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
data[i, j] = a[i, j];
}
#endregion
public void AssignTo(NRMatrix mat)//将矩阵赋值给矩阵mat
{
mat.rows = rows;
mat.cols = cols;
mat.data = new double[mat.rows, mat.cols];
for (int i = 0; i < mat.rows; i++)
for (int j = 0; j < mat.cols; j++)
mat.data[i, j] = data[i, j];
}
public override string ToString() //矩阵的字符串输出
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
sb.AppendFormat("{0:F4}\t", data[i, j]);
sb.AppendFormat("\r\n");
}
return sb.ToString();
}
#region 矩阵基本运算符重载
public override bool Equals(object o) //重载Equals,矩阵相等的比较
{
// 在处理之前确认调用对象object是矩阵。
if (o != null && o is NRMatrix)
{
if (rows == ((NRMatrix)o).rows && cols == ((NRMatrix)o).cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (this[i, j] != ((NRMatrix)o)[i, j]) return false;
return true;
}
else
return false;
}
return false;
}
public static bool operator ==(NRMatrix mat1, NRMatrix mat2) //矩阵相等的比较
{
return mat1.Equals(mat2);
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
public static bool operator !=(NRMatrix mat1, NRMatrix mat2) //矩阵不等的比较
{
return !mat1.Equals(mat2);
}
public static NRMatrix operator +(NRMatrix mat1, NRMatrix mat2) //矩阵的加法运算
{
if (mat1.rows != mat2.rows || mat1.cols != mat2.cols)
throw (new NRException("矩阵不同型,不能作加法!"));
NRMatrix result = new NRMatrix(mat1.rows, mat1.cols);
for (int i = 0; i < mat1.rows; i++)
for (int j = 0; j < mat1.cols; j++)
result[i, j] = mat1[i, j] + mat2[i, j];
return result;
}
public static NRMatrix operator -(NRMatrix mat1, NRMatrix mat2) //矩阵的减法运算
{
if (mat1.rows != mat2.rows || mat1.cols != mat2.cols)
throw (new NRException("矩阵不同型,不能作减法!"));
NRMatrix result = new NRMatrix(mat1.rows, mat1.cols);
for (int i = 0; i < mat1.rows; i++)
for (int j = 0; j < mat1.cols; j++)
result[i, j] = mat1[i, j] - mat2[i, j];
return result;
}
public static NRMatrix operator *(NRMatrix mat1, NRMatrix mat2) //矩阵的乘积运算
{
if (mat1.cols != mat2.rows) throw (new NRException("矩阵不相容,不能作矩阵乘法!"));
int M = mat1.rows, N = mat2.cols;
NRMatrix result = new NRMatrix(M, N);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
{
double sum = 0.0;
for (int k = 0; k < mat1.cols; k++)
sum += mat1[i, k] * mat2[k, j];
result[i, j] = sum;
}
return result;
}
public static NRMatrix operator *(double k, NRMatrix mat) //矩阵的数乘运算
{
NRMatrix result = new NRMatrix(mat.rows, mat.cols);
for (int i = 0; i < mat.rows; i++)
for (int j = 0; j < mat.cols; j++)
result[i, j] = k * mat[i, j];
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -