I have a function called 'symbolic_a0(array, T)' and the objective of that function is to create a piecewise function from the array that is given. The way that the function works is the following:
- Create the symbols.
- Create an empty Piecewise function.
- Iterate the array and fill the empty piecewise function.
- In this case do a symbolic integration of the piecewise function.
But when I try to run the code, the line "piecewise_expr = smp.Piecewise()" gives me the next error:
At least one (expr, cond) pair expected.
import numpy as np
import sympy as smp
def symbolic_a0(array, T):
t = smp.symbols('t', real=True)
piecewise_expr = smp.Piecewise() # Create an empty piecewise function.
# Build the Piecewise function from the array
for segment in array:
equation_lambda_numpy = lambda t: eval(segment["equation"].get()) # Lambda function with numpy expressions
equation_sympy = smp.sympify(equation_lambda_numpy(t))
lower_bound = int(segment["lower_bound"].get())
upper_bound = int(segment["upper_bound"].get())
piecewise_expr += (equation_sympy, (t, lower_bound, upper_bound))
return smp.integrate(piecewise_expr, (t, 0, T))
The array that is given:
piecewise_data = [
{"equation": 0, "lower_bound": -2, "upper_bound": -1},
{"equation": 1, "lower_bound": -1, "upper_bound": 1},
{"equation": 0, "lower_bound": 1, "upper_bound": 2},
]
How do I get rid of this error?
The things that I already tried:
piecewise_expr = smp.Piecewise(None, None).- Giving it a dummy function, but I don't want to use it in my calculation.
Make a list of arguments for the
Piecewiseand create thePiecewisewhen you are done:Note use of
rto create relational from the symbols and bounds.Piecewiseargs are (expr, condition) tuples.