Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

python - Is it possible to create a variable and expect the if statement to be about a variable within the variable?

Sorry, my question is worded weirdly, I know. I'm working on a code for the trapezoidal rule. My function is CTR(N,a,b,f). At one point I have to find N such that the value of another function is 4. So my code looks like this

for N in range (1,2000):
    if ((CTR(2*N,0,np.sqrt(np.pi/2),func3)-CTR(N,0,np.sqrt(np.pi/2),func3)) / 
        (CTR(4*N,0,np.sqrt(np.pi/2),func3)-CTR(2*N,0,np.sqrt(np.pi/2),func3)) == 4):
        print("N = ", N)

This wasn't so bad to type out but now I have to do this for another problem which involves me writing out variations of my CTR code 16 times. Is it possible for me to do something like this:

Th1 = CTR(2*N,0,np.sqrt(np.pi/2),func3)
Th2 = CTR(N,0,np.sqrt(np.pi/2),func3)
Th3 = CTR(4*N,0,np.sqrt(np.pi/2),func3)

for N in range (1,2000):
    if((Th2-Th1 / Th4 - Th2) == 4):
    print("N = ", N)

I figure it didn't work when I tried because there is no N in the function. However, still wondering if there's some way to simplify my code.

question from:https://stackoverflow.com/questions/65868684/is-it-possible-to-create-a-variable-and-expect-the-if-statement-to-be-about-a-va

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue

When defining Th1, Th2, and Th3, their expressions are evaluated and their value is known. Changing N won't anything about it, since they've already been evaluated with a value of N (in your second code you were indeed supposed to define N before all four operators are defined).


Possible solution

What you can do though is wrap those expressions in a function, which would receive N as input. I'm assuming of course that func3 as already been defined in the current scope.

def Ths(N):
    Th1 = CTR(2*N, 0, np.sqrt(np.pi/2), func3)
    Th2 = CTR(N, 0, np.sqrt(np.pi/2), func3)
    Th3 = CTR(4*N, 0, np.sqrt(np.pi/2), func3)
    return Th1, Th2, Th3

This way you can call Ths with a value of N to compute the associated triplet Th1(N), Th2(N), and Th3(N).

for N in range (1,2000):
    if ((Th1 - Th2) / (Th3 - Th1)) == 4:
        return N 

Functional approach

It seems you are working with more than just Th1, Th2 and Th3. Also with more than one function. In your post you showed (Th2-Th1) / (Th4 - Th2). You would take a functional approach to solving this.

First define all your Thi as lambdas:

Th1 = lambda N: CTR(2*N, 0, np.sqrt(np.pi/2), func3)
Th2 = lambda N: CTR(N, 0, np.sqrt(np.pi/2), func3)
Th3 = lambda N: CTR(4*N, 0, np.sqrt(np.pi/2), func3)
Th4 = lambda N: CTR(2*N, 0, np.sqrt(np.pi/4), func3) # some other Thi

Then combine those operators:

Fc1 = lambda N: (Th1(N) - Th2(N)) / (Th3(N) - Th1(N))
Fc2 = lambda N: (Th2(N) - Th1(N)) / (Th4(N) - Th2(N))

If your functions all have the same shape, you could even create a helper to create these:

def make_func(a, b, c, d):
    return lambda N: (a(N) - b(N)) / (c(N) - d(N))

This way you can define Fc1 and Fc2 very easily with make_fc(Th1, Th2, Th3, Th1) and make_fc(Th2, Th1, Th4, Th2). Notice how I'm not calling Th1, Th2, ... but passing Th1, Th2, ... to make_func.


Finally create a wrapper to test your functions:

def test(fc, N_max=2000, value=4):
   for N in range(1, N_max):
       if fc(N) == 4:
           return N

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...