Python Forum
code running for more than an hour now, yet didn't get any result, what should I do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code running for more than an hour now, yet didn't get any result, what should I do?
#1
I'm trying to implement a triple integral Riemann sum. My single and double integral codes are working. I just added the for k in range (1,l+1):, l = int(zb/dz) and za+(k-1)*dz in the code below. It's not getting any error nor results. It's just running. It's been more than an hour now. What other ways/code would you suggest as a replacement of the code below? Using math library only and has to implement the function calculate(f, xa, xb, ya, yb, za, zb). I'm still new to this just for your information. Thank you for all the help.


dx = 0.002
dy = 0.002
dz = 0.002

def calculate(f, xa, xb, ya, yb, za, zb):
  n = int(xb/dx)
  m = int(yb/dy)
  l = int(zb/dz)
  t = dx*dy*dz
  total = 0
  for i in range (1,n+1):
    for j in range (1,m+1):
      for k in range (1,l+1):
        total += f(xa+(i-1)*dx, ya+(j-1)*dy, za+(k-1)*dz)*t
  return total

def p(x, y, z):
  return math.sin(x) + math.cos(y) + math.tan(z)

s = calculate(p, 0, 3, 0, 2, 0, 1)
print("{:.3f}".format(s))
Reply
#2
Why do you loop over range(1, i+1) and then subtract 1 from i?

This is not correct.
 n = int(xb/dx)
  m = int(yb/dy)
  l = int(zb/dz)
It is only correct if xa, ya and za are all zero.

I have no idea why it would take hours. I set dz = 1 and the program takes 1.3 seconds on my computer. I set dz = 0.2 and it took 5 times longer (5.3 seconds). I set dz = 0.02 and it took 10 times longer than that (50 seconds). I would expect dz = 0.002 should take 500 seconds. A long time to wait, but not hours.

For the function p this is a particularly inefficient way to compute the total. In your particular example cos(ya+(j-1)*dy) is computed for each iteration of the "k" loop. The value is the same each time. You are calling math.cos() 500 times more often then is needed, and you are calling math.sin() 500,000 times more often than is needed.
Reply
#3
If you take dx = dy = dz = 0.02 it should be 1000 times faster already.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code not running even without errors Azdaghost 2 207 Apr-25-2025, 07:35 PM
Last Post: Azdaghost
  python code not running Azdaghost 1 201 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 5 2,420 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  problem in running a code akbarza 7 2,363 Feb-14-2024, 02:57 PM
Last Post: snippsat
  the order of running code in a decorator function akbarza 2 1,360 Nov-10-2023, 08:09 AM
Last Post: akbarza
Question Sqlite3 how to know when cursor.execute didn't return anything ? SpongeB0B 2 1,632 Dec-18-2022, 06:13 PM
Last Post: deanhystad
  help me simple code result min and max number abrahimusmaximus 2 1,707 Nov-12-2022, 07:52 AM
Last Post: buran
  Code running many times nad not just one? korenron 4 2,228 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 6,774 Jul-01-2022, 02:51 PM
Last Post: maiya
  Can a program execute code in iPython shell and get result? deanhystad 3 2,772 Jun-17-2022, 03:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020