📄 program.cs
字号:
x[i][j - j0] = data[r[i]][j];
}
}
return X;
}
/// <summary>
/// 复制矩阵
/// </summary>
/// <returns>矩阵的复制</returns>
public Matrix Clone()
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j];
}
}
return X;
}
/// <summary>
/// 转置矩阵
/// </summary>
/// <returns>返回转置矩阵</returns>
public Matrix Transpose()
{
Matrix X = new Matrix(columns, rows);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[j][i] = data[i][j];
}
}
return X;
}
/// <summary>
/// 负运算
/// </summary>
/// <returns>返回负运算的结果</returns>
public static Matrix Negate(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("参数无效");
}
int rows = value.Rows;
int columns = value.Columns;
double[][] data = value.Array;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = -data[i][j];
}
}
return X;
}
/// <summary>
/// 负运算操作符重载
/// </summary>
public static Matrix operator -(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("参数无效");
}
return Negate(value);
}
/// <summary>
/// 相等运算符重载
/// </summary>
public static bool operator ==(Matrix left, Matrix right)
{
return Equals(left, right);
}
/// <summary>
/// 不相等运算符重载
/// </summary>
public static bool operator !=(Matrix left, Matrix right)
{
return !Equals(left, right);
}
/// <summary>
/// 矩阵相加
/// </summary>
/// <param name="left">矩阵1</param>
/// <param name="right">矩阵2</param>
/// <returns>矩阵1和矩阵2的和</returns>
public static Matrix Add(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
if (right == null)
{
throw new ArgumentNullException("参数2无效");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
if ((rows != right.Rows) || (columns != right.Columns))
{
throw new ArgumentException("矩阵不相符");
}
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] + right[i, j];
}
}
return X;
}
/// <summary>
/// 加法运算符重载
/// </summary>
public static Matrix operator +(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
if (right == null)
{
throw new ArgumentNullException("参数2无效");
}
return Add(left, right);
}
/// <summary>
/// 减法
/// </summary>
/// <param name="left">被减矩阵</param>
/// <param name="right">减数矩阵</param>
/// <returns>差</returns>
public static Matrix Subtract(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
if (right == null)
{
throw new ArgumentNullException("参数2无效");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
if ((rows != right.Rows) || (columns != right.Columns))
{
throw new ArgumentException("矩阵不相符");
}
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] - right[i, j];
}
}
return X;
}
/// <summary>
/// 减法运算符重载
/// </summary>
public static Matrix operator -(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
if (right == null)
{
throw new ArgumentNullException("参数2无效");
}
return Subtract(left, right);
}
/// <summary>
/// 乘法
/// </summary>
/// <param name="left">乘数矩阵1</param>
/// <param name="right">乘数</param>
/// <returns>积</returns>
public static Matrix Multiply(Matrix left, double right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] * right;
}
}
return X;
}
/// <summary>
/// 乘法运算符重载
/// </summary>
public static Matrix operator *(Matrix left, double right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
return Multiply(left, right);
}
/// <summary>
/// 乘法
/// </summary>
/// <param name="left">乘数矩阵1</param>
/// <param name="right">乘数矩阵2</param>
/// <returns>积</returns>
public static Matrix Multiply(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
if (right == null)
{
throw new ArgumentNullException("参数2无效");
}
int rows = left.Rows;
double[][] data = left.Array;
if (right.Rows != left.columns)
{
throw new ArgumentException("矩阵维数不符");
}
int columns = right.Columns;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
int size = left.columns;
double[] column = new double[size];
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < size; k++)
{
column[k] = right[k, j];
}
for (int i = 0; i < rows; i++)
{
double[] row = data[i];
double s = 0;
for (int k = 0; k < size; k++)
{
s += row[k] * column[k];
}
x[i][j] = s;
}
}
return X;
}
/// <summary>
/// 乘数运算符重载
/// </summary>
public static Matrix operator *(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("参数1无效");
}
if (right == null)
{
throw new ArgumentNullException("参数2无效");
}
return Multiply(left, right);
}
/// <summary>
/// 随机产生矩阵元素
/// </summary>
/// <param name="rows">行数</param>
/// <param name="columns">列数</param>
/// <returns>矩阵</returns>
public static Matrix Random(int rows, int columns)
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = random.NextDouble();
}
}
return X;
}
/// <summary>
/// 重载ToString方法
/// </summary>
/// <returns>矩阵字符串输出</returns>
public override string ToString()
{
using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
writer.Write(this.data[i][j] + " ");
}
writer.WriteLine();
}
return writer.ToString();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -