A constructor for a matrix class

Hi,
I'm trying to write my own matrix math library (I have practical uses for it, but it's mostly an educational exercise) but I can't find the right syntex for what I'm trying to do:

I want the library to have a Matrix class which you can either declare like that:

Matrix C = {{1,2,3}
            {4,5,6}};

Or like this:

Matrix C(2,3);

(and thanks to the stackoverflow users who helped me figure out exactly what I want :slight_smile: )

I've tried looking into initializer lists, but I must be looking in the wrong direction, because I can't find any straight forward explanation for how to do something like that.

does anyone know a simple example for a constructor that does that?

thanks!

does anyone know a simple example for a constructor that does that?

A class can have multiple constructors. What are each of your constructors supposed to do?

PaulS:
A class can have multiple constructors. What are each of your constructors supposed to do?

You're right- I only asked half the question :smiley:

I want the class to have members "int rows", "int cols" and "int _matrix[]"

so when declaring:

Matrix C={{1,2,3}{4,5,6}};

the constructor will create a Matrix object where: rows=2, cols=3, and _matrix is a 2D array of type int containing the above elements.
the rest of the constructors should be easy enough when I master this one (well.. I think they should)

I want the class to have members "int rows", "int cols" and "int _matrix[]"

so when declaring:

Matrix C={{1,2,3}{4,5,6}};

the constructor will create a Matrix object where: rows=2, cols=3, and _matrix is a 2D array of type int containing the above elements.

When declaring a 2D array, all but one of the dimensions must be defined. That goes for unction arguments, too. You could define a function that took an array of unknown size, as long as you also supply the sizes.

So, your constructor declaration could look like:

   Matrix(int nRows, int nCols, int **data);

The use would then be:

   Matrix C(2, 3, {{1,2,3}{4,5,6}});

PaulS:
When declaring a 2D array, all but one of the dimensions must be defined. That goes for unction arguments, too. You could define a function that took an array of unknown size, as long as you also supply the sizes.

So, your constructor declaration could look like:

   Matrix(int nRows, int nCols, int **data);

The use would then be:

   Matrix C(2, 3, {{1,2,3}{4,5,6}});

Thanks, that's exactly what I thought of using, but I was wandering:
a. if there was any way of using the '=' operator to declare an object (i.e. C(1,3)={1,2,3};), and
b. if there is any way at all to get the number of rows and columns without having the user supply it. I thought about using a constructor which get an unknown number of arrays of unknown sizes as arguments, so the declaration would look like:

Matrix C({1,2,3},{4,5,6},{7,8,9},...);

then the constructor will know how many arrays it got, and how many elements there are in each array, so it can build an array with (numArrays) rows, and (maxArrayLength) columns. does that make any sense? how do you build a constructor which gets an unknown number of arguments?

EDIT:
oh, and:
c. can you use templates in arduino (If it's possible I'd like the Matrix object to either be and array of int or double, depending on the user's input without writing multiple constructors)

Matrix C(2, 3, {{1,2,3}{4,5,6}});

This will not work, you cannot assign a brace-enclosed initializer list to a pointer. By definition the pointer has no storage to place the data, its just points to data somewhere else.

To use an '=' instead of a constructor, your object must be a POD object (no constructors, private/protected members, and no inherited bases).

However paul did correctly assert that you need to know the array sizes, you cannot just pass data and expect the compiler to fill in the blanks.

This is where your edit is useful as it can be done using templates.

The basic starting point is this:

template< int rows, int cols >
  struct Matrix{

   int data[rows][cols];
};

Now you can declare an object like this:

Matrix< 2, 3 > C = {{{1,2,3},{4,5,6}}};

If you want to have the class work with different types you can add a third template parameter:

template< int rows, int cols, typename T >
  struct Matrix{

   T data[rows][cols];
};

You can use variadic templates in the latest IDE (C++11 enabled) and it can assist with creating complex objects, but that is more of a meta-programming topic and not really something that is covered easily.

pYro_65:
This will not work, you cannot assign a brace-enclosed initializer list to a pointer. By definition the pointer has no storage to place the data, its just points to data somewhere else.

To use an '=' instead of a constructor, your object must be a POD object (no constructors, private/protected members, and no inherited bases).

However paul did correctly assert that you need to know the array sizes, you cannot just pass data and expect the compiler to fill in the blanks.

This is where your edit is useful as it can be done using templates.

The basic starting point is this:

template< int rows, int cols >

struct Matrix{

int data[rows][cols];
};




Now you can declare an object like this:



Matrix< 2, 3 > C = {{{1,2,3},{4,5,6}}};




If you want to have the class work with different types you can add a third template parameter:



template< int rows, int cols, typename T >
  struct Matrix{

T data[rows][cols];
};





You can use variadic templates in the latest IDE (C++11 enabled) and it can assist with creating complex objects, but that is more of a meta-programming topic and not really something that is covered easily.

Thanks! I'll try it out :slight_smile: