1

Is there a way to create a view for a numpy.ndarray that will only return specific items in a specific shape?

I'm working on a project with a material stress tensor matrix. I have created an ndarray sublcass that must maintain a 3x3 shape for its base. There is one module, however, that requires the tensor to be in Voigt notation. Unfortunately, this is not easily done by a simple reshape function because of the order of the entities in the matrix.

Notation Convention

I would like to be able to keep the single ndarray subclass and just create a separate view for the calculations that require this notation.

As of now, the best I've been able to come up with is creating a function that constructs and returns a new array from the instance's data property. It normally wouldn't be a big deal, but the calculations I need it for will need to be performed millions of times.

2
  • There appears to be something similar to this in the source code at this github page, maybe something you could use or adapt Commented Feb 26, 2019 at 22:12
  • that github script uses loops to iterate over array. I would keep hands off Commented Feb 26, 2019 at 22:30

1 Answer 1

1

you can pass list of indexes and extract only those values you are interested in

In this example, I create Eye matrix and from it I create View on diagonale

tensor = np.eye(3)

>>> diagonal_view = [i for i in range(3)], [i for i in range(3)]
>>> tensor[diagonal_view]
array([1., 1., 1.])

for your example in your matrix shape, you would want something like this

#             1. dimension , 2. dimension
voight_view = [0,1,2,1,2,0],[0,1,2,2,0,1] # voight notation # voight notation
>>> tensor[voight_view]
array([1., 1., 1., 0., 0., 0.])

In case you dont want reference, just use

array.copy()

But it seems that just pure assignment works too

new_array = tensor[voight_view]
Sign up to request clarification or add additional context in comments.

1 Comment

That's it! I didn't realize you could index like that. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.