Python Forum
How to generate double arrays by two lists using np.zero - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to generate double arrays by two lists using np.zero (/thread-42080.html)



How to generate double arrays by two lists using np.zero - Safinazsalem - May-06-2024

I have these two lists:

X=np.arange(0,50,0.1)
Y=np.arange(10,20,1)

How to generate a doubles array from these two lists by np.zeros_like()

Then how to make a counter on this array using enumerate()


I tired something like:
import numpy as np
import scipy.special
from scipy import integrate
from scipy.special import kn
import matplotlib.pyplot as plt
import math
import time, sys
from itertools import count

X=np.arange(0,50,0.1)
Y=np.arange(10,20,1)

res = np.zeros_like((x, y))
for i, (va, val) in enumerate(zip(va, val))
y= va+val
res[i]=y
return res 
But it gives an error: UnboundLocalError: local variable ‘y’ referenced before assignment


RE: How to generate double arrays by two lists using np.zero - deanhystad - May-06-2024

I have no idea what you are trying to do. Do you just want to make a 2D array that is X wide and Y tall?
res = np.zeros((Y.shape[0], X.shape[0]))
This is a mess.
for i, (va, val) in enumerate(zip(va, val))
y= va+val
res[i]=y
return res 
Indentation is wrong for a for loop. You can only use return inside a function. va and val have not been assigned a value before it is used.

It is confusing using va and val on both sides of this assignment.
for i, (va, val) in enumerate(zip(va, val))
I have no idea what va and val are initially, but they are something else after the for loop is done.

This seems wrong. I don't know for sure because I don't know what va and val are.
res[i]=y
This could be correct if va and val are lists of lists or 2D arrays, but it makes no sense to initialize res to zeros and then replace all the rows. Why not do this:
res = va + val
But there must be a reason you initialize res to zero, without providing more information I don't think you can expect much help.


RE: How to generate double arrays by two lists using np.zero - Safinazsalem - May-07-2024

Hi, Thanks for your reply.

The value of res that I have mentioned is an example.

What indeed I try to do is triple integral a function f(t1,t2,t3,a,k) over t1,t2 and t3 while perform a loop on the values of the variables a and k .

The integration limits of t1 and t2 is from 0 to a:
( with a tends to zero)

The integration limits of t3 is from 0 to infinity

In the next code I could only perform a single loop on k . You notice that I generate a single dimensional array:
X = (0,50,0.1)

I appreciate if you help me like in the question above, if i generated also a range by :
Y= np.arange(10,20,1)

How to generate 2Dim array then make counter on it by enumerate() and do the integral accordingly. I think in this case F(X) will be F(X,Y).


from scipy import integrate
from scipy.special import kn
import matplotlib.pyplot as plt
import math
import time, sys
 
 
H=4.10061*10**-5;ti=-100*H;end=-H;step=H;a=0;
 
f = lambda t1, t2,t3, k: t1 + t2 +k +  a +t3
X = np.arange(0,50,0.1)
 
g=float('inf')
#plot(X,f(X))
 
def F(x):
    res = np.zeros_like(x)
    for i,val in enumerate(x):
        y,err = integrate.tplquad(f, 0, a,lambda x: 0, lambda x: a,lambda x,y: 0, lambda x,y: g,args=(val,))
        res[i]=y/(math.exp(H*a)**2)
    return res
 
plt.plot(X,F(X))



RE: How to generate double arrays by two lists using np.zero - deanhystad - May-07-2024

What is the second loop? Is it a? Is X the values for k? Do you want something like this?
 def func(t1, t2, t3, a, k):
    """Function goes here."""
k_values = np.arange(k_start, k_end, k_increment)
a_values = np.arange(a_start, a_end, a_increment)
res = np.zeros((len(k_values), len(a_values)))

for a_index, a in enumerate(a_values):
    for k_index, k in enumerate(k_values):
        y, err = integrate.tplquad(
            func,
            0, a,
            lambda x: 0, lambda x: a,
            lambda x, y: 0, lambda x, y: float("inf"),
            args=(a, k)
        res[a_index, k_index] = y / (math.exp(H*a)**2)



RE: How to generate double arrays by two lists using np.zero - Safinazsalem - May-11-2024

Hi, thanks so much for the reply.

Yes, that what I try to do. But how to plot the function versus (k_values ) ?

Also in the res[a_index, k_index] how to show that the value of (a) tends to very small values or zero.