Design question: N-dimensional tensors

  • Thread starter Martin Magnusson
  • Start date
M

Martin Magnusson

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.
 
V

Victor Bazarov

Martin said:
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.

One way would be recursive templates similar to

template<class T, unsigned dim> class tensor {
tensor<T, dim-1> *storage;
...
T operator()(...) const; // to access an element
// the trick is to implement the ...
// conversion and passing it along to
// the 'storage' accessor function
// Not hard once you think about it.
};

template<class T> class tensor<T,1> { // partial specialisation
T *storage;
...
T operator()(unsigned i) const { return storage; }
};
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.

Have you tried looking on the 'Net? I bet people have already come up
with something not just fitting your needs but even efficient and very
likely nice and portable...

I am not trying to discourage you from taking a trip across the ocean to
discover America again. If you think you need to prove it to yourself
that you can do it or work on those rowing or sailing skills of yours,
it's not such a bad idea to actually take a long voyage. It's just that
there are other projects waiting to be implemented and either nobody or
very few people are working on them now. Perhaps you should try getting
your feet wet with those instead of doing something that has been done
over and over again...

Just a thought.

V
 
S

Steven T. Hatton

Martin said:
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
 
E

E. Robert Tisdale

Martin said:
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 C++ Tensor class is available via anonymous ftp from

`ftp.cs.ucla.edu'.

It uses a recursive template class definition for N-dimensional arrays.
It is an experimental class
which is not ready for practical application.

Enjoy, E. Robert Tisdale

P.S. I hope the following notes will be helpful.

unix% ftp ftp.cs.ucla.edu
Name (ftp.cs.ucla.edu:your_login_ID): anonymous
Password: your_login_ID
ftp> cd pub
ftp> binary
ftp> get Tensor.tar.Z
ftp> bye
unix% uncompress Tensor.tar.Z
unix% tar xvf Tensor.tar
unix% cd Tensor
unix% make
 
L

Lionel B

Martin said:
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.
Have a look at Blitz++ http://www.oonumerics.org/blitz/
 
M

Martin Magnusson

John said:

Thanks a lot - that does exactly what I need. All the other
implementations I found were less general, either they had simply
implemented a set of Tensor2, Tensor3, Tensor4... or would require a
specific number of arguments for access, which would still make it
impossible to write dimension-independent code. I really should have
checked Boost first thing.

/ martin
 
G

Gerhard Wesp

Hi Martin,
Is there a good way to do this, ensuring constant-time access?
Constant-time insertion and deletion wouldn't be necessary.

Hmmm... Access will be at least O( N ) where N is the dimension.

From the discussion so far I reckon that this is fixed at compile
time for your application.

If not, in cpp-lib I have a ``table'' template, which is a tensor with
dynamic dimension (as opposed to boost::multi_array where it must be
given at compile-time AFAIK).

It comes with std-like interface and a facility to read
arbitrary-dimensional tables from ASCII files in a variety of formats.

See http://www.cosy.sbg.ac.at/~gwesp/ .

Cheers
-Gerhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,970
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top