Home>

### python - i want to swap the first half and the second half of the array and put them in different arrays

Replacement of # array of the program g → 0 not included g2 → 0 included unit
Line 111
I want to insert the first 500 pieces of an array (iso0) with 1001 pieces after the second half 500 pieces.
(Put the function set at -500 ^ -120500 ^ -12 in time as a periodic function in the array of 0500 ^ -121000 ^ -12)
(Before conversion -500 ^ -12 ~ -1 ^ -12, 0, 1 ^ -12 ~ 500 ^ -12
(After conversion 0, 1 ^ -12 ~ 500 ^ -12, -500 ^ -12 ~ -1 ^ -12

Traceback (most recent call last):
File "C: \ Users \ oreka \ python \ 2jigen3.py", line 115, in
iso0g [i] = iso0 [i + 501]
TypeError: can only concatenate tuple (not "int") to tuple

Corresponding source code
``````import numpy as np
import math
import matplotlib.pyplot as plt
#Constant declaration
a = np.arange (-500.0e-12,600.0e-12,1.0e-12) # 500.0e-12 ~ 600.0e-12 time t (1100 elements)
a2 = np.arange (1,1000,1) # 1 to 1000 integers (used to assign lines with 3 elements in one line of k)
a3 = np.arange (-500.0e-12500.0e-12,1.0e-12) # -500.0e-12 to 500.0e-12 time t (1001 elements)
a4 = np.arange (0,1000.0e-12,1.0e-12) #Time for graph t
iso0g = np.arange (0,1001,1.0) # Insertion after sequence replacement
isog = np.arange (0,1001,1.0) # Insertion after array replacement
iso2g = np.arange (0,1001,1.0) # Insertion after sequence replacement
g = np.arange (0,500,1.0) # 0 not included
g2 = np.arange (501,1001,1.0) # 0 included
k = np.eye (1001) #k base (identity matrix (1001 × 1001))
A = 100
B = 217.0e-28
z = 30.0e + 3
T = 18016836.0e-18
dz = 15000
Creating #p (t, z) and dp/dz
p = pow (A * T, 2)/np.sqrt (pow (T, 4) + pow (B * z, 2)) * np.exp ((-1 * pow (T * a, 2))/(pow (T, 4) + pow (B * z, 2)))
#p (a3 size)
pa3 = pow (A * T, 2)/np.sqrt (pow (T, 4) + pow (B * z, 2)) * np.exp ((-1 * pow (T * a3, 2))/(pow (T, 4) + pow (B * z, 2)))
# p1a3 (t + dz)
p1a3 = pow (A * T, 2)/np.sqrt (pow (T, 4) + pow (B * (z + dz), 2)) * np.exp ((-1 * pow (T * a3,)) 2))/(pow (T, 4) + pow (B * (z + dz), 2)))
# p2a3 (t-dz)
p2a3 = pow (A * T, 2)/np.sqrt (pow (T, 4) + pow (B * (z --dz), 2)) * np.exp ((-1 * pow (T * a3,)) 2))/(pow (T, 4) + pow (B * (z --dz), 2)))
dpdz = pow (A * T * B, 2) * z * ((2 * pow (T * a3,2))/(pow (T, 4) + pow (B * z, 2)) ―― 1) * pow (pow (T, 4)
+ pow (B * z, 2), -1.5) * np.exp (-1 * pow (T * a3,2)/(pow (T, 4) + pow (B * z, 2)))
pp = (p1a3 --pa3)/dz # forward difference
pp2 = (p1a3 --p2a3)/2/dz # center difference
Transform the # dp/dz matrix into a vertical column
dpdz = dpdz.reshape ([1001,1])
pp = pp.reshape ([1001,1])
pp2 = pp2.reshape ([1001,1])
Repeatedly assign p to rows 2 to 1000 of #k (identity matrix (1001 x 1001)) * Rows with 3 elements in one rowfor i in a2:
k [i] [i-1] = p [i + 1] + p [i + 2]
k [i] [i] =-(p [i + 1] + 2 * p [i + 2] + p [i + 3])
k [i] [i + 1] = p [i + 2] + p [i + 3]
Substitute p in the 1st and 1001st lines of #k * A line with 2 elements in one line
k   =-(p  + 2 * p  + p )
k   = p  + p 
k   =-(p  + 2 * p  + p )
k   = p  + p 

kin = np.linalg.inv (k) Inverse matrix kin of #k
b = dpdz/dz/B # creation and b (dpdz) creation
b2 = pp/dz/B # b2 (forward difference)
b3 = pp2/dz/B # b3 (center difference)

#For checking how many elements are contained in each array (shape)
"" "
print ("a")
print (a.shape)
print ("a2")
print (a2.shape)
print ("a3")
print (a3.shape)
print ("k")
print (k.shape)
print ("p")
print (p.shape)
print ("dpdz")
print (dpdz.shape)
print ("kin")
print (kin.shape)
"" "
# For confirmation of each matrix (shape)
np.set_printoptions (linewidth = 2000, edgeitems = 7, precision = 4, floatmode ='maxprec')
print ("k")
print (k)
print ("p")
print (p)
print ("dpdz")
print (dpdz)
print ("b")
print (b)
print ("kin")
print (kin)
Calculate φ from the formula # 3.13
iso0 = kin @ b # iso0 b = dpdz
iso0 = iso0.reshape ([1001,])
print ("iso0")
print (iso0.shape)print (iso0)
iso = kin @ b2 # iso b = forward difference
iso = iso.reshape ([1001,])
print ("iso")
print (iso.shape)
print (iso)
iso2 = kin @ b3 # iso2 b = center difference
iso2 = iso2.reshape ([1001,])
print ("iso2")
print (iso2.shape)
print (iso2)
#Swap array g → 0 not included g2 → 0 included
for i in enumerate (g):
iso0g [i] = iso0 [i + 501]
isog [i] = iso [i + 501]
iso2g [i] = iso2 [i + 501]
for i in g2:
iso0g [i] = iso0 [i-501]
isog [i] = iso [i-501]
iso2g [i] = iso2 [i-501]
#iso for confirmation
print ("iso0")
print (iso0g)
print (iso0g.shape)

#PLOt of each matrix
plt.figure (0)
plt.xlabel ("t")
plt.ylabel ("p (t, z)")
plt.plot (a, p)
plt.figure (1)
plt.xlabel ("t")
plt.ylabel ("b and pp")
plt.plot (a3, dpdz)
plt.plot (a3, pp)
plt.plot (a3, pp2)
plt.figure (2)
plt.xlabel ("t")
plt.ylabel ("φ")
plt.plot (a4, iso0g)
plt.plot (a4, isog)
plt.plot (a4, iso2g)
plt.show ()``````
What I tried

I fixed it when I got an error before (make it float type), but after that it does not work

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

• Answer # 1

If you just need to prepare a box to put the calculated result`numpy.empty ()`Is recommended.
You should be aware of what type will be included.

You can make one loop by switching the first half and the second half as follows. (Boundary handling is probably wrong in the originally written code)

``````iso0g = np.empty (1001, dtype ='float')
for i in range (len (iso0)):
iso0g [i] = iso0 [(i + 500)% len (iso0)]``````

In addition, numpy has a handy function to do this,`numpy.roll ()`With functions, you don't need to loop or prepare a box in advance.

``iso0g = np.roll (iso0, -500)``

• Answer # 2

Two corrections are required.

First of all, in the following, g and g2 need to be ints used in the loop, so adding .0 is not good for float.

``````g = np.arange (0,500,1.0) # 0 not included
g2 = np.arange (501,1001,1.0) # 0 included``````

Revised

``````g = np.arange (0,500,1) # 0 not included
g2 = np.arange (501,1001,1) # 0 included``````

Next, you don't need enumerate () in the following places. i becomes a tuple.

``````#Swap array g → 0 not included g2 → 0 included
for i in enumerate (g):``````

Revised

``````#Swap array g → 0 not included g2 → 0 included
for i in g:``````