Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double[,] vs. List<List<double>> [closed]

Tags:

c#

I have code for a matrix multiplication lab using double[,] and I wanted to compare it with an implementation using a 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:

Input matrix A:

1.000 2.000 3.000 1.000

2.000 3.000 3.000 1.000

Input matrix B:

1.000 0.000 0.000 0.000

0.000 1.000 0.000 0.000

0.000 0.000 1.000 0.000

2.000 3.000 0.000 1.000

Matrix product A*B

3.000 5.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]

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

My question would be why does the math change between the 2 ways of storing the values.

like image 954
karl.r Avatar asked Sep 10 '26 02:09

karl.r


1 Answers

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.

like image 110
Reed Copsey Avatar answered Sep 11 '26 14:09

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!