0

Here is the problem:

I have 3 states (vectors in 3D), that I would like to pair up (e.g. (a,a), (a,b), (a,c), (b,a), (b,b), ...) into elements of a 3X3 matrix.

In a more abstract way, I have a set of 3 arrays with 3 entries, and I would like to produce a 3x3 array where the entries themselves are an array of 2 of the original arrays. (A lot of nesting).

For the 3 cartesian basis vectors this would look like: a = (1,0,0), b = (0,1,0), c = (0,0,1)

and the matrix would be:

M = | {(1,0,0),(1,0,0)} {(1,0,0),(0,1,0)} {(1,0,0),(0,0,1)} |
    | {(0,1,0),(1,0,0)} {(0,1,0),(0,1,0)} {(0,1,0),(0,0,1)} |
    | {(0,0,1),(1,0,0)} {(0,0,1),(0,1,0)} {(0,0,1),(0,0,1)} |

# I appreciate I have formatted the matrix in code rather than LaTeX
# but I can't find the functionality to do this on StackOverflow

so the matrix has elements that are arrays of 2 entries, and the entries of those arrays are my 3D vectors (arrays with 3 entries).

The code that I am using to try to implement this is:

import numpy as np

a= np.array([0,0,0])
b= np.array([0,0,1])
c= np.array([0,2,0])

states = np.array([a,b,c])
def cycler(states):
    matrix = np.zeros((3,3))
    
    for x in range(0,3):
        for y in range(0,3):
            matrix[x,y]= np.array([states[x],states[y] ])

    return matrix

cycler(states)

However, when I run this code, I am getting an error on the line:

matrix[x,y]= np.array([states[x],states[y] ])

with the error message:

ValueError: setting an array element with a sequence.

As a bit of python newbie I'd really appreciate a bit of help to allow me to create the matrix of pairs of vectors! (For context, my state vectors a, b, c are not the basis vectors, and I will then later be writing a function that will read the elements of the matrix, using the array of the 2 3D states in a further calculation.)

1
  • self explanatory "setting an array element with a sequence." Commented Oct 10, 2020 at 16:44

1 Answer 1

1

Setting matrix = np.zeros((3,3),dtype=object) resolves your problem, though I would recommend you rather use a 4d-array of dimensions 3x3x2x3.

Sign up to request clarification or add additional context in comments.

Comments

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.