12,078,259 members (46,985 online)
alternative version

82.2K views
27 bookmarked
Posted

# Tensor templates

, 26 Jun 2001
 Rate this:
A template class that enable you to make tensors with any dimensionality. Process convolution of any tensors and so on.

## Introduction

The `Tensor` class was written in 1997 during my study of templates (I found it examining my archives).

But I've not seen any such classes since. So I think it can be useful for students. The main reason I've wrote it - for the convolution of tensors with any dimensionality. Also you can get and use any sub-tensor using `operator[]`, for example can be written like

```Tensor2 qq,a1,a2,aa;
convolution(aa["li"],a2["lj"],qq["ij"]);
convolution(qq["kl"],a1["ki"],aa["li"]);```

## Using

The demo project shows usage of tensors. You can:

Declare and init tensors (you should specify dimension and indexes range).

```Tensor2 result2(2);
Tensor4 tt(3);  // 4 - dimensions indexes can be 0-2 or "1"-"3"
Tensor2 t1(2);       t1[0][0] = 1.0;
t1[1][0] = 2.0;
t1[0][1] = 3.0;
t1[1][1] = 4.0;
```

Convolute tensors - note that you can use different dimensions and one or more constants in indices.

```convolution(result5["2i"],t3["2"],t2["i3"]);
convolution(result6["ij"],t1["ja"],t2["ai"]);
```

Use some arithmetical operations.

```result3 = t1 + t2*2;
```

Print part or whole tensor.

```t4["ijkl"].printf(std::cout);
result5["1j"].printf(std::cout);
```

Average tensors.

```Tensor2 t[2];
double q[2];
t2.averaging(&t[0],q,2);
```

Invert 4-dimension tensor with indexes (0,1).

```Tensor4 t(2),ti(2);
ti = inverse(t);
```

## To Do list

It can be easy to implement some more operations with tensors (different multiplication and so). Please let me know, if you need some.

## Note

Make sure to check out the my web site which is more likely to have updates and betas: http://www.zmike.net

A list of licenses authors might use can be found here

## Share

 Web Developer Russian Federation
Mike has been programming in C/C++ for 11 years and Visual C++/MFC for 4 years. His background includes pure and applied mathematics, engineering and physics, and he is currently based in Moscow, Russia.

## You may also be interested in...

 First Prev Next
 linear algebra VeganFanatic9-Jun-10 16:22 VeganFanatic 9-Jun-10 16:22
 Introduction to Tensor Convolution Pho3NiX15-Aug-09 8:52 Pho3NiX 15-Aug-09 8:52
 Hi this idea of tensor convolution in intriguing for me. As you have mentioned most tensor library don't implement it. Also When I try to search for it google result are poluted by tons of research paper and I cannot find anything that would explain me what it is. FInally I have an paramtric Interpolation algorythm "recipe" that tell me to contract a Rank N tensor on N matrix like that For N=3 Q^i^j^k = sum_u ( sum_v ( sum_w ( M1_u^i * M2_v^j * M3_w^k * P_u_v_w ))) Is that a tensor convolution ? I'd also like to undestand how this thing work. Bassically M123 are matrix that represent the solution of the interpolation for eahc parameter separately And the convolution or whaever that thing is now represent the solution for all three parameters together
 General comment Darid Tromer25-Feb-08 0:08 Darid Tromer 25-Feb-08 0:08
 Re: General comment Mike Melnikov1-Mar-08 1:26 Mike Melnikov 1-Mar-08 1:26
 [Message Deleted] Danny Rodriguez27-Jan-08 10:41 Danny Rodriguez 27-Jan-08 10:41
 this is good! yulin1114-Jun-07 4:50 yulin11 14-Jun-07 4:50
 Tensors, great fun! ;) PedroMC11-Aug-05 0:48 PedroMC 11-Aug-05 0:48
 Re: Tensors, great fun! ;) yulin1114-Jun-07 4:52 yulin11 14-Jun-07 4:52
 thk u syit31515-May-04 5:37 syit315 15-May-04 5:37
 Well Done ! Colin Davies6-Jul-02 1:42 Colin Davies 6-Jul-02 1:42
 Re: Well Done ! Mike Melnikov10-Aug-02 4:11 Mike Melnikov 10-Aug-02 4:11
 I'm ashamed to ask but... Daniel Turini22-Dec-01 5:17 Daniel Turini 22-Dec-01 5:17
 Re: I'm ashamed to ask but... Mike Melnikov26-Dec-01 22:01 Mike Melnikov 26-Dec-01 22:01
 Re: I'm ashamed to ask but... Jerry Jeremiah14-Mar-06 14:56 Jerry Jeremiah 14-Mar-06 14:56
 Last Visit: 31-Dec-99 19:00     Last Update: 13-Feb-16 11:24 Refresh 1