Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a 4d matrix full of zeros in python, numpy

I am trying to create a 4 dimensional matrix in python using the following code;

import numpy as np
rho=np.zeros(2,2,2,2)

But I get the following error;

    rho=np.zeros(2,2,2,2)
TypeError: function takes at most 3 arguments (4 given)

This seems to work in matlab, but not here. Any help would be appreciated, Thanks!

like image 252
Joe Bowen Avatar asked Oct 15 '25 16:10

Joe Bowen


1 Answers

Instead of passing 4 arguments, pass one argument, a tuple of four elements:

>>> rho=np.zeros((2,2,2,2))
>>> rho
array([[[[ 0.,  0.],
         [ 0.,  0.]],

        [[ 0.,  0.],
         [ 0.,  0.]]],


       [[[ 0.,  0.],
         [ 0.,  0.]],

        [[ 0.,  0.],
         [ 0.,  0.]]]])
>>> rho.shape
(2, 2, 2, 2)

The call signature is zeros(shape, dtype=float, order='C'), and so it's trying to interpret the first 2 as the shape, the second 2 as the type, the third 2 as the storage order, and then it doesn't know what to do with the last 2.

like image 187
DSM Avatar answered Oct 17 '25 05:10

DSM