#๐ Passing reference to a numpy array into function
110 messages ยท Page 1 of 1 (latest)
@fiery turret
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You can't do this literally in Python. You'd need to return the new object, or put A into a mutable container.
I'd just return it.
OMG
NOW I HAVE TO REWRITE ALL MY CODE
nice scam by python
can i make a dictionary
from arrays?
This is supported by very few languages. Java and Javascript work the same way.
D2Q4 = np.zeros((SIZE + 1, SIZE + 1, 4))
D2Q4_new = D2Q4.copy()
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))
D2Q5_new = D2Q4.copy()
D2Q9 = np.zeros((SIZE + 1, SIZE + 1, 9))
D2Q9_new = D2Q4.copy()
i have 3 arrays
can i make something to iterate though them
!e
import numpy as np
A = np.zeros((3,3))
def modify(a):
a[:] = np.ones((3,3))
modify(A)
print(A)```
@harsh thunder :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [[1. 1. 1.]
002 | [1. 1. 1.]
003 | [1. 1. 1.]]
That's because [:] overwrites the previous contents of the list, which is different than reassigning the reference.
i don't understand
didn't know that, neat
Note that the two have different effects. Your original attempt tried to create a new array, which would have left other code with a reference to the old object unaffected. This way mutates the array, which will be reflected by everyone referencing the object.
So, just make sure you're careful of who has references if you're going to mutate the array.
well
def make_one_step():
main.D2Q4_new = D2Q4.copy()
for y in range (len(D2Q4)):
for x in range (len(D2Q4)):
for m in range(len(D2Q4[0][0])):
modify_Tm(x,y,m)
main.D2Q4 = D2Q4_new.copy()
thats a function i'm planning to pass my array to
since i have 3 arrays now
i don't wanna make 3 instances of this function
you'll want to pass the array in modify_Tm(main.D2Q4_new, x,y,m)
def operate(array, i, j, k):
array[i, j, k] *= 2
``` or whatever
now its also necessary to pass array further to modify_Tm function
def modify_Tm(x, y, m):
if m == 0:
flag1 = find_nearest_node(x-1)
flag2 = Tm(flag1, y, m)
flag3 = Tm_eq(flag1, y)
elif m == 1:
flag1 = find_nearest_node(y-1)
flag2 = Tm(x, flag1, m)
flag3 = Tm_eq(x, flag1)
elif m == 2:
flag1 = find_nearest_node(x+1)
flag2 = Tm(flag1, y, m)
flag3 = Tm_eq(flag1, y)
elif m == 3:
flag1 = find_nearest_node(y+1)
flag2 = Tm(x, flag1, m)
flag3 = Tm_eq(x, flag1)
elif m == 4:
flag2 = Tm(x, y, m)
flag3 = Tm_eq(x, y)
elif m == 5:
flagx = find_nearest_node(x - 1)
flagy = find_nearest_node(y - 1)
flag2 = Tm(flagx, flagy, m)
flag3 = Tm_eq(flagx, flagy)
elif m == 6:
flagx = find_nearest_node(x + 1)
flagy = find_nearest_node(y - 1)
flag2 = Tm(flagx, flagy, m)
flag3 = Tm_eq(flagx, flagy)
elif m == 7:
flagx = find_nearest_node(x + 1)
flagy = find_nearest_node(y + 1)
flag2 = Tm(flagx, flagy, m)
flag3 = Tm_eq(flagx, flagy)
elif m == 8:
flagx = find_nearest_node(x - 1)
flagy = find_nearest_node(y + 1)
flag2 = Tm(flagx, flagy, m)
flag3 = Tm_eq(flagx, flagy)
D2Q4_new[y][x][m] = flag2 - W * (flag2 - flag3)
how else could the modify_Tm function possibly have access to your local copy of the D2Q4 array?
That's the wettest if block ever
this is really bad practice for a number of reasons
โ
global_mutable_variable = "x"
do_work()
print("result:", global_mutable_variable)
โ
```py
result = do_work("x")
print("result:", result)
With the former, it is really difficult to understand how your functions work without first understanding the ENTIRE PROGRAM
nobody wants to have to understand the entire program just to fix one small issue
When your functions modify something outside of the function, that's called a "side effect" and they're really annoying
somtimes unavoidable, but in this case it certainly is
mhm so call modify_Tm(array, x, y, m)
dont use a nonlocal variable
also, this isn't a use-based subscription service, you don't have to use single letter variable names and abbreviations
like wtf is flagx, flagy, flag2, flag3, D2Q4, D2Q4_new, Tm, modify_Tm, Tm_eq, x, y, m, W
none of your variables make any sense, this is complete gibberish
(don't start explaining what they mean, please just use real names instead)
i have a feeling this is a rhetorical question but I'll answer it anyway
Based on the context, I have a feeling that your rows and columns have more inherit meaning than simply being a coordinate system (based on the name D2Q4). If that's not true, then names like x and y could be appropriate, but since these at least represent indices/locations, you could better express their meaning by doing x_index or simply ix (but most people won't do that). Alternatively, names like row and col make it really clear what the variables purposes are
how do you want to modify them, specifically?
there are many ways to slice numpy arrays using colons
so in my make one step function i'm modifying element by element through a loop
.
yep that's what you're doing
if main.D2Q4isn't being modified then that's because D2Q4_new in your modify_Tm refers to a different array than the main.D2Q4_new in your make_one_step
one of the many difficulties with debugging the โ kind of code
no your code already properly modifies an array
!e ```py
import numpy as np
D2Q4_new = np.zeros((2, 2, 2))
D2Q4_new[1][1][1] = 123
print(D2Q4_new)
@rain marsh :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [[[ 0. 0.]
002 | [ 0. 0.]]
003 |
004 | [[ 0. 0.]
005 | [ 0. 123.]]]
caz i'm not making 2 more instances of function
oh ๐คฆโโ๏ธ
okay so here's another one of the many problems with the โ
imagine you want to modify both "x", "y", and "z"
with the โ code, you have to do:
x = [1,2,3]
do_work_on_x()
y = [4,5,6]
do_work_on_y()
z = [7,8,9]
do_work_on_z()
print("results:", x, y, z)
``` where you have to create three separate functions
now consider the equivalent โ
code:
```py
x = do_work([1,2,3])
y = do_work([4,5,6])
z = do_work([7,8,9])
print("results:", x, y, z)
u want me to assign?
i was about to start assigneing before you gave me 2 dots thing
your question has nothing to do with the colon, :
your question is about how to make a function work with multiple different inputs
: is so good it modifies arrays
and I'm telling you that you do it like this
you pass in the variable as an argument
!e ```py
def modify(arr, index):
arr[index] = 123
return arr
print(modify([1,2,3], 0))
print(modify([4,5,6], 1))
print(modify([7,8,9], 2))
@rain marsh :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [123, 2, 3]
002 | [4, 123, 6]
003 | [7, 8, 123]
look at this, I used a single function to modify three different arrays
your function could be just as powerful if you stopped using globals
wait
D2Q4 = np.zeros((SIZE + 1, SIZE + 1, 4))
D2Q4_new = D2Q4.copy()
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))
D2Q5_new = D2Q4.copy()
D2Q9 = np.zeros((SIZE + 1, SIZE + 1, 9))
D2Q9_new = D2Q4.copy()
need to iterate though this
and also each of 3 arrays needs to be assosiated with its copy
would dictionary work
that goes hard
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.