S
Stefan Ram
More often then in my Java classes, the question is asked in
my C++ classes: »How does one define a matrix in C++?«
Well, one of the answers I give, is to use a library for
scientific computation, such as »Blitz«.
But maybe they want to see how to /implement/ a matrix in /C++/.
(I do not mean to implement using a class definition, but just
to create/read/write a matrix, and possibly do some math with it.
All without an elaborate definition of a matrix class.)
AFAIK, there is no predefined matrix type in C++. AFAIK, one
can build a matrix in several ways:
A - a two-dimensional array (not ragged) as in »double m[ 3 ][ 3 ]«,
B - a one-dimensional entity, with index »row * rowsize + col«, or
C - a one-dimensional entity of one dimensional entities (»ragged«).
In the last case, AFAIK, the most common candidates for the
type of said »entities« are arrays, ::std::vector, and ::std::array.
One also might consider mixtures :std::vector<::std::array>)
or less common types for row/cols, such as initializer lists.
To show to beginners how to implement a matrix in C++ with
simple operations (say: read, write, add, multiply) what
of the above means of implementation should be preferred?
On the web, it seems that solution A is preferred. I think
that this has the simplest allocation of memory by just
using »double a[ 3 ][ 3 ];«. When a matrix is defined as a
::std::vector of ::std::vector it seems not to be as easy to
allocate the memory for all the rows. On the other hand,
using plain old C arrays seem to be kind of old-fashioned,
maybe there is some better way to do this in today's C++?
my C++ classes: »How does one define a matrix in C++?«
Well, one of the answers I give, is to use a library for
scientific computation, such as »Blitz«.
But maybe they want to see how to /implement/ a matrix in /C++/.
(I do not mean to implement using a class definition, but just
to create/read/write a matrix, and possibly do some math with it.
All without an elaborate definition of a matrix class.)
AFAIK, there is no predefined matrix type in C++. AFAIK, one
can build a matrix in several ways:
A - a two-dimensional array (not ragged) as in »double m[ 3 ][ 3 ]«,
B - a one-dimensional entity, with index »row * rowsize + col«, or
C - a one-dimensional entity of one dimensional entities (»ragged«).
In the last case, AFAIK, the most common candidates for the
type of said »entities« are arrays, ::std::vector, and ::std::array.
One also might consider mixtures :std::vector<::std::array>)
or less common types for row/cols, such as initializer lists.
To show to beginners how to implement a matrix in C++ with
simple operations (say: read, write, add, multiply) what
of the above means of implementation should be preferred?
On the web, it seems that solution A is preferred. I think
that this has the simplest allocation of memory by just
using »double a[ 3 ][ 3 ];«. When a matrix is defined as a
::std::vector of ::std::vector it seems not to be as easy to
allocate the memory for all the rows. On the other hand,
using plain old C arrays seem to be kind of old-fashioned,
maybe there is some better way to do this in today's C++?