Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am recieving the error seen in the question title when attempting to run my code which attempts to perform some math and will later desire to plot some conditional functions.

My code is as follows

import math
import numpy as np
def f(λ,a,u,o1,o2):
    o = o1 if (λ <= u) else o2
    return a*math.exp((λ-u)^2/(-2*o^2))

x_lamda = np.arange(4000,8000,10)

Yx1 = f(x_lamda,1.056,5998,379,310)


As seen my function is f of λ which is a gaussian with a piecewise function for o which changes depending on if λ is below or above the mean (u). I can't seem to find anything online to fix this error in a particular case like this. How do you avoid this 'ambiguity' when making conditional expressions?
This is the output:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


What I have tried:

I have attempted to use the a.any and the a.all functions on the "λ <= u" part in particular but I am running into more errors.
I will also like to mention that I will be using the quad function later to find definite intergrals plus I will be attempting to plot these later. Will I be running into similar problems later? Will this call for a new approach?
Posted
Comments
Peter_in_2780 12-Sep-21 21:48pm    
I don't know python, but it appears to me that your x_lamda that you pass as first argument to your function is an array. Thus when it is referenced as λ in the comparison, you get the error.
How you fix it depends on what you are trying to do. Maybe you need to put a loop around calling the function and pass it scalars.
Ryan Peen 12-Sep-21 21:57pm    
Yes I just noticed this. I wanted it to calculate the function within the range for the given increments using the np.arange function. A loop would probably do this best. Thankyou :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900