Martin Magnusson wrote:
I want to create a templated class (templated over the number of
dimensions) of N-dimensional tensors. With N=1 you get a vector, N=2
you get an ordinary matrix, N=3 you get a 3D tensor, etc.
Is there a good way to do this, ensuring constant-time access?
Constant-time insertion and deletion wouldn't be necessary.
I suppose that hash_map with an N-dimensional vector as its key_value
would do the job, but it would of course not be as efficient as
implementing specialized classes for the dimensionalities I actually need.
The only guarantee I will make about the code you will find here is that it
is far less that perfect. It represents my second Foray into the real of
C++ templates, and I was experimenting and learning as I hacked on the
code. There may also be files not included in what is on the site. They
are not essential to the functionality of the code. It does not provide a
complete tensor implementation, but does show one way of generating
indices. I may have gone overboard with the use of loop unrolling, but
what can I say? I was trying to explore the language features.
http://www.globalsymmetry.com/gs-home/images/sth/tmath/
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell