C# double[,] 与 List<List<double>>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1712346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
double[,] vs. List<List<double>>
提问by karl.r
I have code for a matrix multiplication lab using double[,] and I wanted to compare it with an implementation using a List< List< double>>
我有一个使用 double[,] 的矩阵乘法实验室代码,我想将它与使用 List< List< double>> 的实现进行比较
public static Matrix operator *(Matrix a, Matrix b)
{
if (a.Width != b.Height)
{
throw new InvalidOperationException();
}
double[,] result = new double[a.Height, b.Width];
for (int i = 0; i < a.Height; i++)
{
for (int j = 0; j < b.Width; j++)
{
for (int k = 0; k < a.Width; k++)
result[i, j] += a[i, k] * b[k, j];
}
}
return new Matrix(result);
}
'result' here has the right data:
'result' 这里有正确的数据:
Input matrix A:
输入矩阵A:
1.000 2.000 3.000 1.000
1.000 2.000 3.000 1.000
2.000 3.000 3.000 1.000
2.000 3.000 3.000 1.000
Input matrix B:
输入矩阵 B:
1.000 0.000 0.000 0.000
1.000 0.000 0.000 0.000
0.000 1.000 0.000 0.000
0.000 1.000 0.000 0.000
0.000 0.000 1.000 0.000
0.000 0.000 1.000 0.000
2.000 3.000 0.000 1.000
2.000 3.000 0.000 1.000
Matrix product A*B
矩阵乘积 A*B
3.000 5.000 3.000 1.000
3.000 5.000 3.000 1.000
4.000 6.000 3.000 1.000
4.000 6.000 3.000 1.000
Changing it to Lists...
将其更改为列表...
public List<List<double>> matrix;
public double this[int x, int y]
{
get { return matrix[x][y]; }
set { matrix[x][y] = value; }
}
public static Matrix operator *(Matrix a, Matrix b)
{
if (a.Width != b.Height)
{
throw new InvalidOperationException();
}
Matrix result = new Matrix(a.Height, b.Width);
for (int i = 0; i < a.Height; i++)
{
for (int j = 0; j < b.Width; j++)
{
for (int k = 0; k < a.Width; k++)
result[i, j] += a[i, k] * b[k, j];
}
}
return result;
}
Using the same data the result is now:
使用相同的数据,现在的结果是:
[7] [11] [6] [2]
[7] [11] [6] [2]
[7] [11] [6] [2]
[7] [11] [6] [2]
EDIT: constructor is:
编辑:构造函数是:
public Matrix(int height, int width)
{
List<List<double>> points = new List<List<double>>();
List<double> row = new List<double>();
for (int i = 0; i < width; i++)
{
row.Add(0.0d);
}
for (int i = 0; i < height; i++)
{
points.Add(row);
}
matrix = points;
}
it looks like it works fine, everythign is initialized to 0.0
看起来它工作正常,everythign 被初始化为 0.0
My question would be why does the math change between the 2 ways of storing the values.
我的问题是为什么数学会在两种存储值的方式之间发生变化。
采纳答案by Reed Copsey
The problem is in your matrix contructor. You're setting every "row" in "points" to the same instance.
问题出在您的矩阵构造函数中。您将“点”中的每一“行”设置为同一个实例。
Try changing your constructor to:
尝试将您的构造函数更改为:
public Matrix(int height, int width)
{
List<List<double>> points = new List<List<double>>(height); // Might as well set the default capacity...
for (int j = 0; j < height; j++)
{
List<double> row = new List<double>(width);
for (int i = 0; i < width; i++)
{
row.Add(0.0d);
}
points.Add(row);
}
matrix = points;
}
That being said, for a matrix, unless you're trying to implement sparse matrices, a multi-dimensional array makes much more sense. Using a list of lists would make more sense only if you wanted to allow the lists to grow. In your case, you know the sizes in advance, so using arrays is probably a better option.
话虽如此,对于矩阵,除非您尝试实现稀疏矩阵,否则多维数组更有意义。仅当您希望允许列表增长时,使用列表列表才更有意义。在您的情况下,您事先知道大小,因此使用数组可能是更好的选择。
回答by Dmitry Osinovskiy
Well, constructor is wrong. This is because all rows in your matrix points to one object. When you change data in one row, really you change data in all of your rows.
Correct it this way.
好吧,构造函数是错误的。这是因为矩阵中的所有行都指向一个对象。当您更改一行中的数据时,实际上您更改了所有行中的数据。
用这种方法纠正。
public Matrix(int height, int width)
{
List<List<double>> points = new List<List<double>>();
for (int i = 0; i < height; i++)
{
List<double> row = new List<double>();
for (int i = 0; i < width; i++)
{
row.Add(0.0d);
}
points.Add(row);
}
matrix = points;
}