用C#定义一个方块矩阵类,里面包含矩阵的基本成员,如矩阵本身的数据,计算矩阵的行列式,特征值,特征向量、矩阵转置、求逆矩阵等方法,实现两个矩乘法的方法(可选),要求计算矩阵行列式用静态方法,其他用动态方法。 在Main()函数中,创建三个矩阵对象,并调用相关方法计算该矩阵的各种值。
时间: 2023-06-27 19:08:18 浏览: 178
以下是C#定义方块矩阵类的示例代码:
```csharp
using System;
class Matrix
{
private int[,] data; // 存储矩阵数据
private int size; // 矩阵大小
// 构造方法
public Matrix(int size)
{
this.size = size;
this.data = new int[size, size];
}
// 矩阵转置
public void Transpose()
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
int temp = data[i, j];
data[i, j] = data[j, i];
data[j, i] = temp;
}
}
}
// 求逆矩阵
public Matrix Inverse()
{
Matrix result = new Matrix(size);
int[,] identity = new int[size, size];
for (int i = 0; i < size; i++)
{
identity[i, i] = 1;
}
for (int i = 0; i < size; i++)
{
int max = i;
for (int j = i + 1; j < size; j++)
{
if (Math.Abs(data[j, i]) > Math.Abs(data[max, i]))
{
max = j;
}
}
if (max != i)
{
for (int j = 0; j < size; j++)
{
int temp = data[i, j];
data[i, j] = data[max, j];
data[max, j] = temp;
temp = identity[i, j];
identity[i, j] = identity[max, j];
identity[max, j] = temp;
}
}
for (int j = i + 1; j < size; j++)
{
int factor = data[j, i] / data[i, i];
for (int k = 0; k < size; k++)
{
data[j, k] -= factor * data[i, k];
identity[j, k] -= factor * identity[i, k];
}
}
}
for (int i = size - 1; i >= 0; i--)
{
for (int j = i - 1; j >= 0; j--)
{
int factor = data[j, i] / data[i, i];
for (int k = 0; k < size; k++)
{
data[j, k] -= factor * data[i, k];
identity[j, k] -= factor * identity[i, k];
}
}
}
for (int i = 0; i < size; i++)
{
int factor = data[i, i];
for (int j = 0; j < size; j++)
{
data[i, j] /= factor;
identity[i, j] /= factor;
result[i, j] = identity[i, j];
}
}
return result;
}
// 计算行列式
public static int Determinant(Matrix matrix)
{
int result = 1;
int sign = 1;
int[,] data = (int[,])matrix.data.Clone();
for (int i = 0; i < matrix.size; i++)
{
int max = i;
for (int j = i + 1; j < matrix.size; j++)
{
if (Math.Abs(data[j, i]) > Math.Abs(data[max, i]))
{
max = j;
}
}
if (max != i)
{
for (int j = 0; j < matrix.size; j++)
{
int temp = data[i, j];
data[i, j] = data[max, j];
data[max, j] = temp;
}
sign *= -1;
}
if (data[i, i] == 0)
{
return 0;
}
result *= data[i, i];
for (int j = i + 1; j < matrix.size; j++)
{
int factor = data[j, i] / data[i, i];
for (int k = i + 1; k < matrix.size; k++)
{
data[j, k] -= factor * data[i, k];
}
}
}
return result * sign;
}
// 计算特征值和特征向量
public void Eigen(out int[] eigenValues, out int[,] eigenVectors)
{
eigenValues = new int[size];
eigenVectors = new int[size, size];
int[,] data = (int[,])this.data.Clone();
for (int i = 0; i < size; i++)
{
eigenVectors[i, i] = 1;
}
while (true)
{
int maxI = 0;
int maxJ = 1;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (Math.Abs(data[i, j]) > Math.Abs(data[maxI, maxJ]))
{
maxI = i;
maxJ = j;
}
}
}
if (data[maxI, maxJ] == 0)
{
break;
}
int aii = data[maxI, maxI];
int ajj = data[maxJ, maxJ];
int aij = data[maxI, maxJ];
int det = aii * ajj - aij * aij;
int sqrt = (int)Math.Sqrt(det);
int lambda1 = (aii + ajj + sqrt) / 2;
int lambda2 = (aii + ajj - sqrt) / 2;
if (Math.Abs(lambda1 - aii) < Math.Abs(lambda2 - aii))
{
eigenValues[maxI] = lambda1;
}
else
{
eigenValues[maxI] = lambda2;
}
for (int i = 0; i < size; i++)
{
int vi = data[i, maxI];
int vj = data[i, maxJ];
data[i, maxI] = (vi * ajj - vj * aij) / det;
data[i, maxJ] = (vj * aii - vi * aij) / det;
}
for (int j = 0; j < size; j++)
{
int vi = eigenVectors[j, maxI];
int vj = eigenVectors[j, maxJ];
eigenVectors[j, maxI] = (vi * ajj - vj * aij) / det;
eigenVectors[j, maxJ] = (vj * aii - vi * aij) / det;
}
}
}
// 计算矩阵乘法
public static Matrix operator *(Matrix m1, Matrix m2)
{
if (m1.size != m2.size)
{
throw new ArgumentException("矩阵大小不一致");
}
Matrix result = new Matrix(m1.size);
for (int i = 0; i < m1.size; i++)
{
for (int j = 0; j < m1.size; j++)
{
for (int k = 0; k < m1.size; k++)
{
result[i, j] += m1[i, k] * m2[k, j];
}
}
}
return result;
}
// 索引器
public int this[int i, int j]
{
get { return data[i, j]; }
set { data[i, j] = value; }
}
}
```
在Main()函数中,创建三个矩阵对象,并调用相关方法计算该矩阵的各种值的示例代码:
```csharp
static void Main(string[] args)
{
Matrix m1 = new Matrix(3);
m1[0, 0] = 1;
m1[0, 1] = 2;
m1[0, 2] = 3;
m1[1, 0] = 4;
m1[1, 1] = 5;
m1[1, 2] = 6;
m1[2, 0] = 7;
m1[2, 1] = 8;
m1[2, 2] = 9;
Matrix m2 = new Matrix(3);
m2[0, 0] = 9;
m2[0, 1] = 8;
m2[0, 2] = 7;
m2[1, 0] = 6;
m2[1, 1] = 5;
m2[1, 2] = 4;
m2[2, 0] = 3;
m2[2, 1] = 2;
m2[2, 2] = 1;
Matrix m3 = new Matrix(3);
m3[0, 0] = 1;
m3[0, 1] = 2;
m3[0, 2] = 3;
m3[1, 0] = 2;
m3[1, 1] = 5;
m3[1, 2] = 6;
m3[2, 0] = 3;
m3[2, 1] = 6;
m3[2, 2] = 9;
Console.WriteLine($"m1的行列式为{Matrix.Determinant(m1)}");
int[] eigenValues;
int[,] eigenVectors;
m1.Eigen(out eigenValues, out eigenVectors);
Console.WriteLine("m1的特征值为:");
for (int i = 0; i < eigenValues.Length; i++)
{
Console.WriteLine(eigenValues[i]);
}
Console.WriteLine("m1的特征向量为:");
for (int i = 0; i < m1.size; i++)
{
for (int j = 0; j < m1.size; j++)
{
Console.Write($"{eigenVectors[i, j]} ");
}
Console.WriteLine();
}
m1.Transpose();
Console.WriteLine("m1的转置为:");
for (int i = 0; i < m1.size; i++)
{
for (int j = 0; j < m1.size; j++)
{
Console.Write($"{m1[i, j]} ");
}
Console.WriteLine();
}
Matrix m1Inverse = m1.Inverse();
Console.WriteLine("m1的逆矩阵为:");
for (int i = 0; i < m1.size; i++)
{
for (int j = 0; j < m1.size; j++)
{
Console.Write($"{m1Inverse[i, j]} ");
}
Console.WriteLine();
}
Matrix m1m2 = m1 * m2;
Console.WriteLine("m1和m2的乘积为:");
for (int i = 0; i < m1.size; i++)
{
for (int j = 0; j < m1.size; j++)
{
Console.Write($"{m1m2[i, j]} ");
}
Console.WriteLine();
}
Matrix m1m3 = m1 * m3;
Console.WriteLine("m1和m3的乘积为:");
for (int i = 0; i < m1.size; i++)
{
for (int j = 0; j < m1.size; j++)
{
Console.Write($"{m1m3[i, j]} ");
}
Console.WriteLine();
}
}
```
阅读全文