-1

I'm currently writing a programme in which two arrays are defined and then undergo singular value decomposition followed by some other processes to simulate some physics. The arrays should be of dimension D² x D². I initially tried:

Ma, Mb = ( np.zeros([ pow(D, 2), pow(D,2)]) for i in range(2))

followed by

Ma = np . zeros ([ pow(D,2), pow(D,2)])
Mb = np . zeros ([ pow(D,2), pow(D,2)])

and finally

Ma , Mb = [np . zeros ([ pow(D,2), pow(D,2)])]*2

I've found that defining the arrays in different ways affects my final answer, with only the final way giving me the answer I expect. The first two options give me nonsensical answers. Are these different methods actually equivalent or am I missing something?

Edit:

The code that follows is:

for i in D_array:
    for j in D_array:
        for k in D_array:
            for l in D_array:
                Ma[k + D * j][i + D * l] = T[i][j][k][l]
                Mb[k + D * l][i + D * j] = T[i][j][k][l]

where T and D_array were previously defined. After this Ma and Mb undergo SVD,

tom10
  • 67,082
  • 10
  • 127
  • 137
  • it's very unusual to have ` ` between a module name and a function from it, though it's valid python. Most programmers will prefer `np.zeros` over `np . zeros`! Also `pow(D,2)==D**2`. – Marcus Müller Apr 09 '16 at 15:01
  • Define "nonsensical". What **are** you expecting? What is it that you get? – Marcus Müller Apr 09 '16 at 15:02
  • 2
    you are getting two references to the same object, very common problem – Tadhg McDonald-Jensen Apr 09 '16 at 15:02
  • http://stackoverflow.com/questions/6688223/python-list-multiplication-3-makes-3-lists-which-mirror-each-other-when is one example – Tadhg McDonald-Jensen Apr 09 '16 at 15:03
  • Please forgive my lack of knowledge, but what is `np`? – vmonteco Apr 09 '16 at 15:03
  • 1
    @vmonteco [Numpy](http://www.numpy.org/) – poke Apr 09 '16 at 15:04
  • 1
    @vmonteco in `numpy` examples, it's common to do `import numpy as np`. – Marcus Müller Apr 09 '16 at 15:04
  • @Marcus Müller I'm finding the heat capacity of a 2D Ising model. The final method gives me the correct heat capacity curve but not the other two (I end up with oscillating values of the order 10^30 rather than of the order 1). – Hans Schmuber Apr 09 '16 at 15:06
  • @user138901: obviously, I meant in a programming sense. You're still telling me "the last one works, the other ones don't, don't ask me about details". We need your cooperation to understand what you really need to do. – Marcus Müller Apr 09 '16 at 15:08
  • @vmonteco, usually a [google search of the questions tags and unknown term](https://www.google.com/webhp?hl=en#hl=en&q=python+numpy+np) can tell you what it means, for example the top hit for me had `import numpy as np` a bit down the page. – Tadhg McDonald-Jensen Apr 09 '16 at 15:09
  • @TadhgMcDonald-Jensen You're right. My bad. – vmonteco Apr 09 '16 at 15:10
  • @Marcus Müller The code that follows is just a series of for loops that define the elements of Ma and Mb. These are then decomposed. – Hans Schmuber Apr 09 '16 at 15:12
  • @user138901: if the 3rd initialization is what works then you almost certainly have something wrong in your Ising model code. Use one of the first two initializations and get your Ising code working first. It seems that now you're trying to get bad code working via a bad initialization, which is unlikely to lead to true happiness. – tom10 Apr 09 '16 at 15:20
  • Will take a look. Thanks for the help! – Hans Schmuber Apr 09 '16 at 15:21
  • I think your entire loop can be replaced with some combination of `np.transpose` and `.reshape(D**2, D**2)`, which will be O(1) instead of O(D^4). Is D_array just a `np.arange`? – Eric Apr 09 '16 at 16:56
  • Yes, it's an array of natural numbers from 0 to D-1. – Hans Schmuber Apr 12 '16 at 16:38

1 Answers1

4

The last way will have Ma is Mb return true. I find it surprising that that'd be the case you desire

Essentially the last one is equivalent to Mb = Ma = np . zeros ([ pow(D,2), pow(D,2)])

Demur Rumed
  • 421
  • 6
  • 17
  • So you're saying that whenever I edit `Ma`, then the same edit will occur to `Mb`? – Hans Schmuber Apr 09 '16 at 15:11
  • you may want to mention that the correct way to write it is with list comprehension like `[np . zeros ([ pow(D,2), pow(D,2)]) for _ in range(2)]` but that would probably be much less verbose then just writing the statement twice. – Tadhg McDonald-Jensen Apr 09 '16 at 15:11
  • @user138901, yes they point to the same object so it makes no difference which pointer you use to modify it. (the changes to `Ma` would occur to `Mb` as well) – Tadhg McDonald-Jensen Apr 09 '16 at 15:14