C# 向量上的乘法矩阵
我需要将矩阵乘以向量,然后打印结果。有整数向量和整数矩阵两类。输出时,仅显示第一个元素。你能告诉我错误是什么吗?下面我将展示我所做的*操作符的重载、显示矩阵的方法以及显示矩阵的确切方式C# 向量上的乘法矩阵,c#,matrix,operator-overloading,matrix-multiplication,C#,Matrix,Operator Overloading,Matrix Multiplication,我需要将矩阵乘以向量,然后打印结果。有整数向量和整数矩阵两类。输出时,仅显示第一个元素。你能告诉我错误是什么吗?下面我将展示我所做的*操作符的重载、显示矩阵的方法以及显示矩阵的确切方式 公共类向量//类向量 { 受保护的整数[]整数数组; 保护单元尺寸; 保护整数编码错误; 受保护的静态uint num_vec; 公共向量机() { codeError=1; 尺寸=1; IntArray=新整数[大小]; IntArray[0]=0; num_vec++; } 公共矢量(uint n)// {
公共类向量//类向量
{
受保护的整数[]整数数组;
保护单元尺寸;
保护整数编码错误;
受保护的静态uint num_vec;
公共向量机()
{
codeError=1;
尺寸=1;
IntArray=新整数[大小];
IntArray[0]=0;
num_vec++;
}
公共矢量(uint n)//
{
codeError=1;
尺寸=n;
IntArray=新整数[大小];
对于(int i=0;iIntArray;
}
////////////////////////////////////////////////////////
公共类MatrixInt//类MatrixInt
{
受保护的整数[,]整数数组;
保护整数n,m;
保护整数编码错误;
受保护的静态整数;
公共矩阵()
{
n=1;m=1;
IntArray=新整数[n,m];
IntArray[0,0]=0;
num_m++;
}
公共矩阵(整数r,整数c)
{
n=r;m=c;
IntArray=新整数[n,m];
对于(int i=0;ipublic class VectorInt//class VectorInt
{
protected int[] IntArray;
protected uint size;
protected int codeError;
protected static uint num_vec;
public VectorInt()
{
codeError = 1;
size = 1;
IntArray = new int[size];
IntArray[0] = 0;
num_vec++;
}
public VectorInt(uint n)//конструктор з одним параметром
{
codeError = 1;
size = n;
IntArray = new int[size];
for (int i = 0; i < n; i++)
{
IntArray[i] = 0;
}
num_vec++;
}
public VectorInt(uint n, int b)
{
codeError = 1;
size = n;
IntArray = new int[size];
for (int i = 0; i < n; i++)
{
IntArray[i] = b;
}
num_vec++;
}
~VectorInt()
{
num_vec--;
Console.WriteLine("Dipsosed");
}
public void Input()
{
Console.WriteLine("\nSize of vector: ");
size = uint.Parse(Console.ReadLine());
IntArray = new int[size];
for (int i = 0; i < size; i++)
{
Console.WriteLine("Element {0}: ", i);
Console.WriteLine(" v [ {0} ] = {1} ", i, IntArray[i] = int.Parse(Console.ReadLine()));
}
}
public void Output()
{
Console.WriteLine("\nMatrix");
for (int i = 0; i < n; i++)//n-amount of rows
{
for (int j = 0; j < m; j++)//m-amount of columns
{
Console.Write(" m [ {0}{1} ] = {2} ", i, j, IntArray[i, j]);
}
Console.WriteLine();
}
}
public uint Size
{
get
{
return (uint)IntArray.Length;
}
}
public int[] NewIntArray
{
get => IntArray;
}
////////////////////////////////////////////////////////
public class MatrixInt//class MatrixInt
{
protected int[,] IntArray;
protected int n, m;
protected int codeError;
protected static int num_m;
public MatrixInt()
{
n = 1; m = 1;
IntArray = new int[n, m];
IntArray[0, 0] = 0;
num_m++;
}
public MatrixInt(int r, int c)
{
n = r; m = c;
IntArray = new int[n, m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
IntArray[i, j] = 0;
}
num_m++;
}
public MatrixInt(int r, int c, int b)
{
n = r; m = c;
IntArray = new int[n, m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
IntArray[i, j] = b;
}
num_m++;
}
~MatrixInt()
{
num_m--;
Console.WriteLine("Disposed");
}
public void Input()
{
Console.WriteLine("Number of rows: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Number of columns: ");
m = int.Parse(Console.ReadLine());
IntArray = new int[n, m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.WriteLine("Element {0}{1}: ", i, j);
Console.WriteLine(" m [ {0}{1} ] = {2} ", i, j, IntArray[i, j] = int.Parse(Console.ReadLine()));
}
}
}
public void Output()
{
Console.WriteLine("\nMatrix");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(" m [ {0}{1} ] = {2} ", i, j, IntArray[i, j]);
}
Console.WriteLine();
}
}
public int Rows
{
get
{
return n;
}
}
public int Columns
{
get
{
return m;
}
}
/////////////////////////////////////////////////////////////
public static MatrixInt operator *(MatrixInt obj, VectorInt obj1)
{
MatrixInt obj3 = new MatrixInt();
if (obj.Columns != obj1.NewIntArray.Length)
{
throw new Exception("Error!");
}
obj3.IntArray = new int[obj.Rows, 1];
for (int i = 0; i < obj.Rows; i++)
{
obj3.IntArray[i, 0] = 0;
for (int j = 0; j < obj.Columns; j++)
{
obj3.IntArray[i, 0] += obj.IntArray[i, j] * obj1.NewIntArray[j];
//Console.WriteLine(" m [ {0}{1} ] = {2} ", i, j, obj3.IntArray[i, 0]);
}
}
return obj3;
}
////////////////////////////////////////////////////////
MatrixInt obj = new MatrixInt();
VectorInt obj1 = new VectorInt();
obj.Input();
obj.Output();
obj1.Input();
obj1.Output();
obj *= obj1;
obj.Output();