473,468 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Design question: N-dimensional tensors

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.
Jul 22 '05 #1
6 1804
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.
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]; }
};
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
Jul 22 '05 #2
>
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...


boost multi_array class does this

http://www.boost.org/libs/multi_array/doc/index.html

john
Jul 22 '05 #3
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

Jul 22 '05 #4
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.

Have a look at Blitz++ http://www.oonumerics.org/blitz/

--
Lionel B

Jul 22 '05 #5
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.

John Harrison wrote: boost multi_array class does this

http://www.boost.org/libs/multi_array/doc/index.html


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
Jul 22 '05 #6
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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.