Take a piecewise-linear function
$$f(x,y)=\begin{cases} ax+b \quad x\leq y\\ cx+d \quad x > y \end{cases}$$
I need to obtain an expression of $\mathbb{E}_{y}f(x,y)$, knowing that there are only finitely many $y$ values, say $y_1,\ldots,y_N$ and their own probabilities $p_1,\ldots,p_N$.
The expectation will itself be a piecewise-linear function. If $N$ is fairly small, say $2$ or $3$, I can manually do the calculations and obtain an expression of the expectation. However, as $N$ grows, this task soon becomes impractical. I could probably write my own code in e.g., Python, to do that, but I feel there is a chance I am reinventing the wheel. So, here is the question: Is there a piece of (possibly free) software that can give me the expression of $g(x) = \mathbb{E}_{y}f(x,y) = \sum_{i=1}^Np_if(x,y_i)$?
Here's a function in R that I wrote for $g(x)$: