English 中文(简体)
matrix and vector template classes in c++
原标题:
#include <array>

template <typename T>
class Vector4<T> {
    std::array<T, 4> _a; // or  T _a[4]; ?
};

template <typename T>
class Matrix4<T> {
    std::array<T, 16> _a; // or  T _a[16]; ?
    //Vector4<T> row0; // or should i use this instead
    //Vector4<T> row1; // it makes other code easier but how
    //Vector4<T> row2; // can i implement something like 
    //Vector4<T> row3; // std::array::data()?
};

thanks

edit: ya its for 3d game programming... so i will need more then boost matrix can provide anyway like rotate and translate and invert etc...

问题回答

The way you want to do it, would be

std::vector<std::vector<int> > my_matrix(4, std::vector<int>(4));

However, I would rather use ublas from boost if you want to handle matricies:

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}

The example is from their website http://www.boost.org/doc/libs/1_40_0/libs/numeric/ublas/doc/matrix.htm

I ve found Eigen to be the most straightforward of the C++ linear algebra libraries, and it contains templates for fixed and variable dimension vectors and matrices. Like Boost, it s a pure template "library" so there are no libs to build / include, but I find it to be more complete and significantly more performant than Boost s ublas.

There s already a standard std::vector class. If you need something specifically for linear algebra, I d suggest looking into boost ublas.

I don t think you would want std::array _a[4], as you will create 4 arrays with that.

Boost ublas is a great library, but is not optimized if all you want to is 4-D stuff. I would recommend keeping the two classes independent:

template <typename T>
class Vector4<T> {
    T _a[4];
};

template <typename T>
class Matrix4<T> {
    T _m[4][4];
};

You can always cast a pointer to a row (_m[i]) into a Vector4<> if needed. To get a column vector though you will need an adapter class like ublas uses.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签