Tensor<1, dim> initialization

30 views
Skip to first unread message

A.Z Ihsan

unread,
Jan 5, 2020, 8:15:49 AM1/5/20
to deal.II User Group

Hi there, 
just a small question. 

suppose we want to construct a rank-1 tensor in 3 dim with values (1, 1, 0) : 
Tensor<1, 3> b();

i read a documentation we can use array_type as a parameter construction, but still i aml confused how to use it. 
any idea how to use it?

BR,
Ihsan

Matthias Maier

unread,
Jan 5, 2020, 9:41:38 AM1/5/20
to dea...@googlegroups.com

On Sun, Jan 5, 2020, at 07:15 CST, "A.Z Ihsan" <jurna...@gmail.com> wrote:

> Tensor<1, 3> b();

If you write this then you declare a function "b" returning a
Tensor<1,3>. Declare the tensor without the parentheses:

Tensor<1, 3> b;

You can access individual elements of the tensor via operator[]:

b[0] = 1;
b[1] = 1;
b[2] = 0;

And you can do meaningful mathematical tensor operations. Including,
scaling a tensor

auto a = 5. * b;

Adding two tensors together:

auto c = a + b;

Taking the inner product:

auto k = a * b;

etc.

Best,
Matthias

A.Z Ihsan

unread,
Jan 6, 2020, 12:53:55 AM1/6/20
to deal.II User Group
Thank you Matthias. 
It works perfectly!

BR, 
Ihsan

Wolfgang Bangerth

unread,
Jan 6, 2020, 12:55:13 PM1/6/20
to dea...@googlegroups.com
On 1/5/20 6:15 AM, A.Z Ihsan wrote:
> just a small question.
>
> suppose we want to construct a rank-1 tensor in 3 dim with values (1, 1, 0) :
> Tensor<1, 3> b();

In addition the other answer, you can also initialize the tensor like this:
Tensor<1,3> t2({1., 1., 0.});
or
Tensor<1,3> t4{{1., 1., 0.}};

Best
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

Reply all
Reply to author
Forward
0 new messages