-2

I need to add threads to this code but it doesn't work. The teacher gave advice to create a list that stores the parameters to use, but I can't figure out how to do it

a = 1
b = 3
E = 0.07

def f(x):
    return x**4

def quad(left, right, fleft, fright, lr_area):
    mid = (left + right)/2
    fmid = f(mid)
    l_area = ((fleft + fmid)*(mid - left)/2)
    r_area = ((fmid + fright)*(right - mid)/2)
    if abs((l_area + r_area) - lr_area) > E:
        l_area = quad(left, mid, fleft, fmid, l_area)
        r_area = quad(mid, right, fmid, fright, r_area)

    return l_area + r_area

S = quad(a, b, f(a), f(b), (f(a) + f(b))*(b-a)/2)

print (S)

on your advice, I tried not to touch the recursion and solve the problem through streams, but I get the wrong answer. new code below

    import threading
    import time
    
    a = 1
    b = 3
    E = 0.07
    
    def f(x):
        return x**4
    
    
    
    def quad(left, right, fleft, fright, lr_area):
        mid = (left + right)/2
        fmid = f(mid)
        l_area = ((fleft + fmid)*(mid - left)/2)
        r_area = ((fmid + fright)*(right - mid)/2)
        atribute_list = []
        if abs((l_area + r_area) - lr_area) > E:
            l_area1 = threading.Thread(target=quad, args=(left, mid, fleft, fmid, l_area,))
            r_area1 = threading.Thread(target=quad, args=(mid, right, fmid, fright, r_area,))
    
            l_area1.start()
            r_area1.start()
    
            l_area1.join()
            r_area1.join()
            time.sleep(1)
            print(atribute_list)
    
        return atribute_list
    
    S = quad(a, b, f(a), f(b), (f(a) + f(b))*(b-a)/2)
    
    print(S)
  • Does this answer your question? [How to use threading in Python?](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) – ILS Nov 12 '22 at 14:03
  • Where do you want to add multithreading? I can see that `quad()` is a recursive function, so if you [want to apply multithreading you may need to change your algorithm](https://stackoverflow.com/a/14541585/11806050) – Thaiminhpv Nov 12 '22 at 14:11
  • Stack Overflow is not a place to answer such question and do the work for you, consider to specify your question. – Fabian Nov 12 '22 at 14:15
  • Thanks for the idea to remove the recursion, I would not have guessed because I am new to Python. To solve the function, recursion was used to divide the trapezoid into 2 parts and calculate its area, but I need to make sure that threads are created that calculate the area of ​​the trapezoid. I tried to do something, but everything stops after 1 recalculation, and then, as the teacher said, the program stops because my threads do not see the new parameters of the divided trapezoids – Иван Шакура Nov 12 '22 at 15:32
  • Re, "I tried to do something, but everything stops after 1 recalculation..." OK, so, instead of showing us the code that works, show the code that _doesn't_ work—tell us what you expected it to do, tell us what it did instead that you don't understand—and maybe somebody here can help you to understand what went wrong. – Solomon Slow Nov 12 '22 at 18:06
  • Re, "Thanks for the idea to remove the recursion..." You should consult your instructor before you do that. Your `quad` function obviously is meant to perform numeric integration of the function `f`, but the exercise probably isn't meant to teach numerical methods: It probably is meant to teach you something about threads. Getting rid of the recursion means, getting rid of the divide-and-conquer aspect of the algorithm. That might defeat your instructor's purpose. Don't change the program so much that the instructor no longer recognizes it. – Solomon Slow Nov 12 '22 at 18:14
  • yes yes yes i did it. I spent 5 hours continuously thinking only about the code and 2 energy cans and I did it. – Иван Шакура Nov 12 '22 at 23:10

1 Answers1

0

here is my code if anyone is interested

import threading

a = 1
b = 3
E = 0.07
st = int(input("Write number: "))


def f(x):
    return x**st

lparametrs_list = [a, b, f(a), f(b), (f(a) + f(b))*(b-a)/2]

rparametrs_list = [a, b, f(a), f(b), (f(a) + f(b))*(b-a)/2]


def llist_updater(l_list):
    lparametrs_list.clear()
    for i in l_list:
        lparametrs_list.append(i)

    return lparametrs_list


def rlist_updater(r_list):
    rparametrs_list.clear()
    for i in r_list:
        rparametrs_list.append(i)

    return rparametrs_list


def quad(left, right, fleft, fright, lr_area):
    mid = (left + right)/2
    fmid = f(mid)
    l_area = ((fleft + fmid)*(mid - left)/2)
    r_area = ((fmid + fright)*(right - mid)/2)
    if abs((l_area + r_area) - lr_area) > E:
        l_list = [left, mid, fleft, fmid, l_area]
        lthr = threading.Thread(target=llist_updater, args=(l_list,))
        lthr.start()
        lthr.join()
        l_area = quad(*lparametrs_list)
        r_list = [mid, right, fmid, fright, r_area]
        rthr = threading.Thread(target=rlist_updater, args=(r_list,))
        rthr.start()
        rthr.join()
        r_area = quad(*rparametrs_list)

    return l_area + r_area
S = quad(a, b, f(a), f(b), (f(a) + f(b))*(b-a)/2)

print(S)