0

I have a matrix G.

I want to make a block matrix

 G  Gz
 Gz G

in python where Gz is all 0 matrix with same size as G.

All my attempts are leading to crashes.

This is part of my code

Gz=[row[:] for row in G]
Gz=np.zeros
G = np.array(G).reshape(-1,nvar)
Gz= np.array(Gz).reshape(-1,nvar)

GGz=np.vstack(G,Gz)
GzG=np.vstack(Gz,G)
GG=np.hstack(GGz,GzG)

This is the error message I get :

ValueError                                Traceback (most recent call last)
<ipython-input-40-56968a80ac64> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', "G1 = []\nh1 = []\nG = 
[]\nh = []\n#print(G)\nfor i in range(n):\n    for j in range(n):\n        k 
= n*i + j\n       # print('hello0')\n       # print(n)\n       # 
print('hello1')\n       # print(i)\n       # print('hello2')\n       # 
print(j)\n       # print('hello3')\n       # print(k)\n       # exit()\n        
# -b_ij <= 0\n        G.append([0]*nvar)\n        G1.append([0]*nvar)\n\n        
#print(len(G))\n        #print(G)\n        #print('hello3')\n        G[-1] 
[k] = -1\n        G1[-1][k] = -1\n\n        #print(len(G))\n        
#print(G)\n        h.append([0])\n        h1.append([0])\n\n        # b_ij 
<= 1\n        G.append([0]*nvar)\n        G[-1][k] = 1\n        
h.append([1])\n        G1.append([0]*nvar)\n        G1[-1][k] = 1\n        
h1.append([1])\n        # u_i + v_j - b_ij <= 1\n        
G.append([0]*nvar)\n        G[-1][k] = -1\n        G[-1][n**2 + i] = 1\n        
G[-1][n**2 + n + j] = 1\n        h.append([1])\n        
G1.append([0]*nvar)\n        G1[-1][k] = -1\n        G1[-1][n**2 + i] = 1\n        

It continues as

   G1[-1][n**2 + n + j] = 1\n        h1.append([1])\n        # b_ij - u_i <= 
  0\n        G.append([0]*nvar)\n        G[-1][k] = 1\n        G[-1][n**2 + 
  i] 
  = -1\n        h.append([0])\n        G1.append([0]*nvar)\n        G1[-1] 
[k] 

It continues as

= 1\n        G1[-1][n**2 + i] = -1\n        h1.append([0])\n        # u_i 
 <= 
 1\n        G.append([0]*nvar)\n        G[-1][n**2 + i] = 1\n        
 h.append([1])\n        G1.append([0]*nvar)\n        G1[-1][n**2 + i] = 1\n        
 h1.append([1])\n        # b_ij - v_j <= 0\n        G.append([0]*nvar)\n        
 G[-1][k] = 1\n        G[-1][n**2 + n + j] = -1\n        h.append([0])\n        
 G1.append([0]*nvar)\n        G1[-1][k] = 1\n        G1[-1][n**2 + n + j] = 
 -1\n        h1.append([0])\n        # v_j <= 1\n        
  G.append([0]*nvar)\n        

It continues as

 G[-1][n**2 + n + j] = 1\n        h.append([1])\n        
 G1.append([0]*nvar)\n        G1[-1][n**2 + n + j] = 1\n        
 h1.append([1])\n        \n#print(h)\nGz=[row[:] for row in 
 G]\nGz=np.zeros\nG = np.array(G).reshape(-1,nvar)\nGz= 
 np.array(Gz).reshape(-1,nvar)\nh = np.array(h).reshape(-1,1)\nG1 = 
 np.array(G).reshape(-1,nvar)\nh1 = 
 np.array(h).reshape(-1,1)\n\nhh=np.vstack((h,h1))\nhh = 

It continues as

np.array(hh).reshape(-1,1)\nprint(G.shape)\n#print(Gz.shape)\n 
\n#GGz=np.vstack( 
G,Gz)\n#GzG=np.vstack(Gz,G)\n#GG=np.hstack((,np.vstack(Gz,G1)))\n\n#GG = 
np.array(G).reshape(-1,nvar)")

It continues as

C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in 
run_cell_magic(self, magic_name, line, cell)
2118             magic_arg_s = self.var_expand(line, stack_depth)
2119             with self.builtin_trap:
-> 2120                 result = fn(magic_arg_s, cell)
2121             return result
2122 

<decorator-gen-61> in time(self, line, cell, local_ns)

It continues as

C:\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
191     # but it's overkill for just that one bit of state.
192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
194 
195         if callable(arg):

It continues as

C:\Anaconda3\lib\site-packages\IPython\core\magics\execution.py in 
time(self, line, cell, local_ns)
1175         else:
1176             st = clock2()
-> 1177             exec(code, glob, local_ns)
1178             end = clock2()
1179             out = None

<timed exec> in <module>()

ValueError: total size of new array must be unchanged
Turbo
  • 77
  • 1
  • 10
  • You have so many missing parentheses in this code it's not even funny. Please post code you've actually tried running. And post the complete error you get, formatted properly. All in the question please: don't respond in comments. – Mad Physicist May 06 '18 at 14:27
  • While you're at it, please explain what each line of the code. What you've posted looks nonsensical enough to be a real mystery. – Mad Physicist May 06 '18 at 14:29
  • It looks to me like you're just looking for [scipy.linalg.block_diag](https://stackoverflow.com/questions/4154253/how-can-i-transform-blocks-into-a-blockdiagonal-matrix-numpy/4155943#4155943). Is that right? – DSM May 06 '18 at 14:45
  • @DSM yes I am also trying to vertically and horizontally stack in addition to block stack. – Turbo May 06 '18 at 14:47
  • I tried BlownhitherMa's code it still throws up. – Turbo May 06 '18 at 14:48

1 Answers1

1

np.zeros is a function that takes shape tuple and return matrix of zeros. Following your logic, it would be Gz=np.zeros(G.shape).
In addition to that, I would prefer to generated 4-tile matrix and assigns G to 2 of them.

# G = np.random.random((2, 2))
m, n = G.shape
GG = np.zeros((m * 2, n * 2))           # 4 tiles
GG[m:, n:] = G                          # assign left-top
GG[:m, :n] = G                          # assign right-bottom

print(GG)
# [[0.16518374 0.83519636 0.         0.        ]
#  [0.76530177 0.21199601 0.         0.        ]
#  [0.         0.         0.16518374 0.83519636]
#  [0.         0.         0.76530177 0.21199601]]
Blownhither Ma
  • 1,461
  • 8
  • 18
  • why does this not work? I am trying to stack two vectors. m1, n1 = h.shape hh = np.zeros((m1 * 2,n1)) # 2 tiles hh[m:, :] = h # assign left-top hh[:m, :] = h # assign right-bottom – Turbo May 06 '18 at 14:40
  • @Turbo The code in the comment use `m1` and `m` at the same time and it is confusing. I guess use `hh[m1:, :] = h` would help? – Blownhither Ma May 06 '18 at 14:43
  • does not work m1, n1 = h.shape hh = np.zeros((m1 * 2,n1)) # 2 tiles hh[m1:, :] = h # assign left-top hh[:m1, :] = h # assign right-bottom – Turbo May 06 '18 at 14:45
  • @Turbo Does it give errors or what is the desire result? ( By the way, why `np.zeros((m1 * 2,n1))` instead of `n1 * 2`? It doesn't help your original request – Blownhither Ma May 06 '18 at 14:48
  • I am also trying to horizontally and vertically stack in addition to diagonal stack. – Turbo May 06 '18 at 14:49
  • Your current code stacks two `h` which is equivalent to simply `np.vstack([h, h])`. And I do not see any error raised – Blownhither Ma May 06 '18 at 14:53
  • Do you know what this means? ValueError Traceback (most recent call last) in () 1 E = np.zeros((n**2,nvar)) ----> 2 E[:n**2,:n**2] = np.diag(np.ones(n**2)) 3 b = np.zeros(n**2).reshape(n**2,-1) 4 b[:n**2] = M.reshape(n**2,-1) 5 ValueError: could not broadcast input array from shape (225,225) into shape (225,15) – Turbo May 06 '18 at 14:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170472/discussion-between-blownhither-ma-and-turbo). – Blownhither Ma May 06 '18 at 14:57
  • Do you know how to print matrix size? len(A) does not help. – Turbo May 06 '18 at 15:27
  • `np.shape(matrix)` or `matrix.shape` if it is numpy matrix – Blownhither Ma May 06 '18 at 15:31
  • Would you also know this? AttributeError: 'cvxopt.base.matrix' object has no attribute 'astype' – Turbo May 06 '18 at 15:37
  • I try G = cop.matrix(G.astype(float)) here. – Turbo May 06 '18 at 15:39
  • @Turbo `np.array(G).astype(float)` because numpy has `astype` function while I'm not sure about `cvxopt` (chatroom preferred because this is not related to the original post – Blownhither Ma May 06 '18 at 15:40