Skip to main content
GameDev.net gamedev.net
🔒 Locked

Matrix solving program

Started by gritchie Oct 4, 2012 at 2:01 AM 0 replies 1.3k views
Original Post
gritchie
gritchie
Hello everyone.

I was starting to make a program to solve the determinant of any nxn matrix.(just for the practice) I was wanting to use a class so I could practice using them. My question is should I use a data field for the size of the matrix and were would I start making my array (which i think would be the best way to make a matrix). I know I need to declare my array, I'm just not sure exactly where I should do that.

Thanks
alvaro
alvaro
"Solving" a matrix or a determinant doesn't mean anything. You can solve a system of linear equations (which can be expressed in matrix form) and you can compute a determinant. Precise language does matter for math, so try to learn to use the way people express these things.


This is how I would start a Matrix class:
#include <vector>

class Matrix {
int size;
std::vector<double> m;

public:
Matrix(int size) : size(size), m(size*size) {
}

double &operator()(int row, int col) {
return m[row * size + col];
}

double operator()(int row, int col) const {
return m[row * size + col];
}
};


If the size of the matrix will be known at compile time, you have the option of using templates instead:
template <int size>
class Matrix {
double m[size*size];

public:
double &operator()(int row, int col) {
return m[row * size + col];
}

double operator()(int row, int col) const {
return m[row * size + col];
}
};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.