I am using trinket.io, an online IDE for python3. I currently do not have access to any other IDEs for download. I am trying to create a numpy array using numerous iterative and conditional statements. When I print each list, I receive the correct results that I am expecting but when I append it to the array, it results in very weird results.
This is my code:
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
x = Symbol('x')
y = Function('f')(x)
#Equation is ay_(n-1) + by_n + cy_(n+1) = d
a = 3
b = 4
c = 6
d = 4
#Final row equation: ey_(n-2)+fy_(n-1)+gy_(n) = d
e = 5
f = 3
g = 7
#Timestep
h = 0.2
#Initialize variables
y_0 = 0
final_point = 1
array = []
row = []
i = 1
array_row_size = int(final_point / h)
array_column_size = array_row_size + 1
for i in range(1,array_row_size+1):
row.clear()
if i == 1:
row.append(b)
row.append(c)
for j in range(2, array_column_size-1):
row.append(0)
row.append(d)
elif i == 2:
row.append(a)
row.append(b)
row.append(c)
for j in range(3, array_column_size-1):
row.append(0)
row.append(d)
elif i < (array_column_size - 1) and i>2:
for j in range(2,i):
row.append(0)
row.append(a)
row.append(b)
row.append(c)
for j in range(i+1, array_column_size-1):
row.append(0)
row.append(d)
else:
for j in range(1, array_column_size-3):
row.append(0)
row.append(e)
row.append(f)
row.append(g)
row.append(d)
array.append(row)
print("Row:", row)
print("Array", array)
i += 1
#print(array)
and these are the results:
Row: [4, 6, 0, 0, 0, 4]
Array [[4, 6, 0, 0, 0, 4]]
Row: [3, 4, 6, 0, 0, 4]
Array [[3, 4, 6, 0, 0, 4], [3, 4, 6, 0, 0, 4]]
Row: [0, 3, 4, 6, 0, 4]
Array [[0, 3, 4, 6, 0, 4], [0, 3, 4, 6, 0, 4], [0, 3, 4, 6, 0, 4]]
Row: [0, 0, 3, 4, 6, 4]
Array [[0, 0, 3, 4, 6, 4], [0, 0, 3, 4, 6, 4], [0, 0, 3, 4, 6, 4], [0, 0, 3, 4, 6, 4]]
Row: [0, 0, 5, 3, 7, 4]
Array [[0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4]]
This is obviously very different that expected since I would expect that each row would be different in the array whereas in each iteration, all the rows are being updated and they are the same.
When you append the row
list to the array, you are not creating a copy of the row
at that moment, you are adding a reference to the same row
list. Every time you modify row
in the loop you are modifying the same underlying list that all the elements in your array
are pointing to.
You have to create a new row
list in each iteration and then appending a copy of it to the array
, each element in the array
will be a distinct list containing the values calculated in that specific iteration.
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
x = Symbol('x')
y = Function('f')(x)
# Equation is ay_(n-1) + by_n + cy_(n+1) = d
a = 3
b = 4
c = 6
d = 4
# Final row equation: ey_(n-2)+fy_(n-1)+gy_(n) = d
e = 5
f = 3
g = 7
# Timestep
h = 0.2
# Initialize variables
y_0 = 0
final_point = 1
array = []
row = []
i = 1
array_row_size = int(final_point / h)
array_column_size = array_row_size + 1
for i in range(1, array_row_size + 1):
row = [] # Create a new empty list for each row
if i == 1:
row.append(b)
row.append(c)
for j in range(2, array_column_size - 1):
row.append(0)
row.append(d)
elif i == 2:
row.append(a)
row.append(b)
row.append(c)
for j in range(3, array_column_size - 1):
row.append(0)
row.append(d)
elif i < (array_column_size - 1) and i > 2:
for j in range(2, i):
row.append(0)
row.append(a)
row.append(b)
row.append(c)
for j in range(i + 1, array_column_size - 1):
row.append(0)
row.append(d)
else:
for j in range(1, array_column_size - 3):
row.append(0)
row.append(e)
row.append(f)
row.append(g)
row.append(d)
array.append(row[:]) # or row.copy() to append a copy of the row
print("Row:", row)
print("Array", array)
i += 1
#print(array)
Output
Row: [4, 6, 0, 0, 0, 4]
Array [[4, 6, 0, 0, 0, 4]]
Row: [3, 4, 6, 0, 0, 4]
Array [[4, 6, 0, 0, 0, 4], [3, 4, 6, 0, 0, 4]]
Row: [0, 3, 4, 6, 0, 4]
Array [[4, 6, 0, 0, 0, 4], [3, 4, 6, 0, 0, 4], [0, 3, 4, 6, 0, 4]]
Row: [0, 0, 3, 4, 6, 4]
Array [[4, 6, 0, 0, 0, 4], [3, 4, 6, 0, 0, 4], [0, 3, 4, 6, 0, 4], [0, 0, 3, 4, 6, 4]]
Row: [0, 0, 5, 3, 7, 4]
Array [[4, 6, 0, 0, 0, 4], [3, 4, 6, 0, 0, 4], [0, 3, 4, 6, 0, 4], [0, 0, 3, 4, 6, 4], [0, 0, 5, 3, 7, 4]]