Ask Your Question
1

Tensor slicing

asked 2019-04-19 23:06:02 +0100

ljp gravatar image

I wonder if there is any way to access tensor elements by a slicing operation, so that parts of the tensor can be set by another tensor or the other way around. Another useful thing would be the ability to display only the slice of interest of a tensor.
At the moment I'm just looping over the components using the irange of the Manifold.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-04-23 17:33:01 +0100

dan_fulea gravatar image

The following code sets via slicing a value for a tensor:

import pprint
import random

M = Manifold(4, 'A^4')
c.<x,y,z,t> = M.chart()
cf = c.frame()

R = range(4)
data = [ [ [ ( random.choice([-10..10]) if i < j and j < k else 0 )
             for k in R ]
           for j in R ]
         for i in R ]
print "Using the following data..."
pprint.pprint(data)

print "Initializing a tensor field with the above constant data..."
t = M.tensor_field(1, 2, name='t')
t[cf, : ] = data
print "t is:"
print t
print t.display()

(The data was constructed to be sparse, so that the print can be followed easily.) Results this time:

Using the following data...
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
Initializing a tensor field with the above constant data...
t is:
Tensor field t of type (1,2) on the 4-dimensional differentiable manifold A^4
t = -9 d/dx*dy*dz + 4 d/dx*dy*dt + 6 d/dx*dz*dt + 5 d/dy*dz*dt

Now we may want to reset some part of the data... It was simpler for me to do this in terms of the variable data used above, its slicing objects are rather easy to handle:

sage: data
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
sage: data[0:1]
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [0, 0, 0, 0]]]
sage: data[0:1] = [[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [11, 222, 3333, 44444]]]
sage: data
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [11, 222, 3333, 44444]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
sage: t[cf, :] = data
sage: t.display()
t = -9 d/dx*dy*dz + 4 d/dx*dy*dt + 6 d/dx*dz*dt + 11 d/dx*dt*dx + 222 d/dx*dt*dy 
    + 3333 d/dx*dt*dz + 44444 d/dx*dt*dt + 5 d/dy*dz*dt
sage:

(Output was manually adjusted to fit in page...)

Also: One can set some data, or just use some existing element, then at some point extract the data as a "multilist", change this one via slicing, and set the new value inside the tensor:

sage: data
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [11, 222, 3333, 44444]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
sage: t[cf, :] = data
sage: extracteddata = t[cf, :]
sage: extracteddata[0:1][0:2]
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [11, 222, 3333, 44444]]]
sage: extracteddata[0:1][0:2][0]
[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [11, 222, 3333, 44444]]
sage: extracteddata[0:1][0:2][0][-1]
[11, 222, 3333, 44444]
sage: extracteddata[0:1][0:2][0][-1] = [99, 88, 77, 66]
sage: t[cf, :]
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [11, 222, 3333, 44444]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
sage: t[cf, :] = extracteddata
sage: t[cf, :]
[[[0, 0, 0, 0], [0, 0, -9, 4], [0, 0, 0, 6], [99, 88, 77, 66]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-04-19 23:06:02 +0100

Seen: 322 times

Last updated: Apr 23 '19