I have a Matrix class and I am trying to overload the multiplication sign.
It compiles without error and runs, however it does not display the matrix like it should. So I want to make sure the error is not in my overload operation.

public static Matrix operator *(Matrix lhs, Matrix rhs)
        {
            Matrix temp = new Matrix();
            temp.matrix = new double[rhs.getRow(), rhs.getCol()];
            for (int i = 0; i < lhs.getRow(); i++)
            {
                for (int j = 0; j < lhs.getCol(); j++)
                {
                    temp.matrix[i,0] += lhs.matrix[i,j] * rhs.matrix[j,0];
                }
            }
            return temp;
        }

This is my display function, so you can get an idea of what's going on

public string displayMatrix()
        {
            string display = "";
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    display += String.Format("{0,-15:00.00}", matrix[i, j]);
                }
                display += "\r\n";
            }
            return display;
        }

This is the code for the form to display it in the textbox

Matrix matrix_b = new Matrix();
matrix_b = matrix_A * matrix_s;
matrixBDisplay.Text = matrix_b.displayMatrix();

Thanks for any help :)

-Miss Vavazoom

Recommended Answers

All 9 Replies

Is the MultiLine property of your Textbox set to true?

And I don't no for sure any more(been a long time ago) but is a matrix multiplication not more something like

for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                for (int k = 0; k < N; k++)
                    C[i][j] += A[i][k] * B[k][j];

Correct me if I'm wrong on this.

Yes, that's correct. And my multiline is set to true for my textbox.

It still is not displaying in the textbox. I want to note that I have 2 other textboxes that are showing output of matrices using the display function I've created. I've corrected my algorithm to the following:

public static Matrix operator *(Matrix lhs, Matrix rhs)
        {
            Matrix temp = new Matrix();
            temp.matrix = new double[rhs.getRow(), rhs.getCol()];
            for (int i = 0; i < lhs.getRow(); i++)
                for (int j = 0; j < rhs.getCol(); j++)
                    for (int k = 0; k < lhs.getCol(); k++)
                        temp.matrix[i,j] += lhs.matrix[i,k] * rhs.matrix[k,j];
            return temp;
        }

Do you need to initialise your Matrix temp to contain all 0's before performing your sums? The += operator might cause issues if the double temp.matrix[i,j] hasn't been initialised properly.

Alright, I took your advice, and initialized the matrix to 0 like the following, but still, no success

public static Matrix operator *(Matrix lhs, Matrix rhs)
        {
            Matrix temp = new Matrix();
            temp.matrix = new double[lhs.getRow(), rhs.getCol()];
            for (int i = 0; i < temp.row; i++)
                for (int j = 0; j < temp.col; j++)
                    temp.matrix[i, j] = 0;


            for(int i = 0; i < lhs.getRow(); i++)
                for(int j = 0; j < rhs.getCol(); j++)
                    for(int k = 0; k < lhs.getCol(); k++)
                        temp.matrix[i,j] += lhs.matrix[i,k] * rhs.matrix[k,j];
            return temp;
        }
    }

I finally found some code from 3 years ago. I am in fact still "working" on this MatrixCalculator project.:icon_cheesygrin:
I did an override of the ToString method, the variables starting with an underscore a private fields of the class. Give it a try, don't knoow if it will change anything.

public override string ToString()
        {
            String s = "\n";
            for (int r = 0; r < this._row; r++)
            {
                s += "| ";
                for (int c = 0; c < this._col; c++)
                {
                    s += _Mat[r, c].ToString("0.0000").PadLeft(12);  
                }
                s += "|\n";
            }
            return s;
        }

01,23 01,23 01,23
01,23 01,23 01,23
01,23 01,23 01,23
This is what I get in a form with one multiline textbox and following code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string display = "";
            double d = 1.23;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    display += String.Format("{0,-15:00.00}", d);
                }
                display += "\r\n";
            }
            textBox1.Text = display;
        }
    }
}

Perhaps something with your matrix[i, j] variable in your displayMatrix method?
Try and set a breakpoint and see what the debugger tells you.

Hey, thanks so much for helping me out! I really appreciate it...

This is my first program in C#, I've coded in C++ and Java, which makes it an easy transition. However, I'm still getting used to the C# way. Do I put the overloaded toString method in my matrix class, or as a function in the form?

Thank you!!

It is nothing really. We are here to help each other. :)
Should put the overridden ToString method in the Matrix class.
That way you can say things like myMatrix.ToString();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.