On Tuesday, 26 May 2015 09:29:05 UTC+3, fl wrote:
> On Monday, May 25, 2015 at 9:18:08 PM UTC-7, Ian Collins wrote:
> > fl wrote:
> > > On Monday, May 25, 2015 at 5:18:44 PM UTC-7, Ian Collins wrote:
> > >> fl wrote:
> > >>> I get a piece of code which is useful for my project. The code
> > >>> is incomplete.
> > >>> Thus, I want to modify it to work for me. The problem is from
> > >>> a matrix usage.
> > >>> It has the following snippet:
> > >>>
> > >>> matrix state(6,1);
> > >>>
> > >>> /*initializes the state*/
> > >>> state[0][0]=0.1;
> > >>>
> > >>>
> > >>> The matrix is known as two dimension, which looks just like a
> > >>> two dimension array. Is it a pseudo code on the state variable?
> > >>> If it is the real code, I don't know how to define the class to
> > >>> make it like a two dimensional array.
> > >>
> > >> constexpr size_t X = some size;
> > >> constexpr size_t Y = some size;
> > >>
> > >> using Matrix = std::array<std::array<double,Y>,X>;
> > >>
> > >> Matrix matrix;
> > >
> > > I have tried this:
> > >
> > > Matrix = std::array<std::array<double,6>,6>;
> >
> > Why did you omit the "using"? It wasn't prose, it's a C++ keyword!
>
> Excuse me. I did use using at first, but it still can't pass build
> with MSVC. Now, it is found that MSVC does not support c++11.
'std::array' is a simple template mostly copied from 'boost::array' and
it was certainly present in C++ library of MSVC 2010.
> Whether below line is equivalent to typedef in MSVC 2010?
>
> using Matrix = std::array<std::array<double,Y>,X>;
Yes. It is just easier to read than:
typedef std::array<std::array<double,Y>,X> Matrix;
Why you start a new project with 3 generations old compiler?
> Now I find that a two-dimensional array is similar to a Matrix in the
> project.
What you mean by a two-dimensional array? You mean raw array? It is
rather tricky to work with raw arrays in C++ since those decay to
pointer of first element on slightest provocation (that means almost
always when you try to use one for anything). It is therefore
advisable to encapsulate raw array to a class to make it bit more
manageable (and that is what std::array does).
> My new question is whether a two-dimensional array can be
> redefined as a Matrix in order to support matrix multiplication in the
> following (A, P, Q are nXn Matrix, ' is transpose operation)?
>
>
> p = A * P * A' + Q;
No. There are no such thing as post-fix operator ' in C++ and there
are no such thing as matrix operations in C++ and also you can't make
operators (or functions) that return raw arrays in C++. Raw arrays
can't be passed or returned by value (as legacy from C). You should
read a good beginner level book about C++; you won't get anywhere by
asking these novice level questions in forums.
When we need to do linear algebra with matrices in C++ then we usually
use something outside of standard C++ for example some linear algebra
library (Armadillo, Eigen or Boost.UBLAS). These already contain
classes for representing matrices so you should not invent your own
types of matrices but simply use theirs.