1

So basically I have multiple arrays and I need to calculate something with these arrays. The problem is that some of these arrays sometimes equal zero and are divisors.

I want to solve this problem by filtering my array and saying something like "if r >= rs: print("0"), else: print(H)", but it doesn't work. I also tried using map function to say that if the radius r < 0.00001: result = 0.00001, else: result = r. I tried printing list(map(.....)), but it didn't work

def Max(r):
    if r < 0.00001:
      result = 0.00001
    else:
          result = r
    return(result)

# radius array (trying to apply Max to all r)
r22 = map(Max, zz[:, 1]) # zz is an odeint function defined before

def Hamiltonian(r, pt, pr, pphi): #all values are given in the code
H = (-((1-rs/r)*-1)(pt*2)/2 + (1-rs/r)(pr*2)/2 + (pphi2)/(2(r**2)))
return(H)

I got three error messages, "TypeError: unsupported operand type(s) for /: 'int' and 'map'", "TypeError: 'numpy.ndarray' object is not callable", and TypeError: unsupported operand type(s) for /: 'int' and 'list'. Does anyone know why? Ideally, I'd like H to automatically print 0 for all the radius = 0 and ignore the division by zero. Can anyone please help me??

1 Answer 1

1

Your "H formula" is not correctly written, some multiplier signs are missing I believe...

H = (-((1-rs/r)*-1)*(pt*2)/2 + (1-rs/r)*(pr*2)/2 + (pphi*2)/(2*(r**2)))

For the division, you can try to handle an exception? Something like:

def hamiltonian(r, pt, pr, pphi):
    while True:
        try:
            H = (-((1 - rs / r) * -1) * (pt * 2) / 2 + (1 - rs / r) * (pr * 2) / 2 + (pphi * 2) / (2 * (r ** 2)))
            return(H)
        except ZeroDivisionError:
            H = 0
            return(H)

print(hamiltonian(r, pt, pr, pphi))

check this to learn about handling of errors and exceptions

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answering so quick! Unfortunately I tried this method but I still got an error message "TypeError: 'numpy.ndarray' object is not callable", which basically tells me that I tried calling a numpy array as a function right? Do you have any idea how to fix this?
yep absolutely correct, can't believe I didn't see that.. sorry for bothering ;)
You are welcome, I edited the answer again and included the handling of the exception into your code. Make sure your formulas are right :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.