1
votes

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

1 Answers

1
votes

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