【问题标题】:Staggering matrix in pythonpython中的交错矩阵
【发布时间】:2023-04-04 02:41:02
【问题描述】:

我有一个矩阵 G。

我想做一个块矩阵

 G  Gz
 Gz G

在 python 中,Gz 是与 G 相同大小的全 0 矩阵。

我所有的尝试都导致崩溃。

这是我的代码的一部分

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)

这是我收到的错误消息:

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        

继续

   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] 

继续

= 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        

继续

 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 = 

继续

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)")

继续

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)

继续

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):

继续

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

【问题讨论】:

  • 你在这段代码中有这么多缺少的括号,这甚至都不好笑。请发布您实际尝试运行的代码。并发布您得到的完整错误,格式正确。所有问题都请:不要在 cmets 中回复。
  • 当你在做的时候,请解释一下每一行代码的含义。您发布的内容看起来荒谬到足以成为一个真正的谜。
  • 在我看来您只是在寻找scipy.linalg.block_diag。对吗?
  • @DSM 是的,除了块堆栈之外,我还尝试垂直和水平堆栈。
  • 我试过 BlownhitherMa 的代码,它仍然会抛出。

标签:
python
matrix