## performance – sum of multiple piecewise functions python

suppose I have a few lists

``````b_wi=((1,2,3,4),(6,7,8,9,10,11)) #b_wi is a subset of x
f_wi=((5,4,2,7,9),(5,4,3,7,2,3,4))
x=((1,2,3,4,5,6,7,8,9,2,5,3),(1,24,36,42,35,6,7,8,91,2,5,3))
#the following two are step functions formed by the above lists.

'''
F1 = f_wi(0)(0) if x< b_wi(0)(0) ;
f_wi(0)(1) if  x< b_wi(0)(0) <=x< b_wi(0)(1);
...;
f_wi(0)(-1) if x>= b_wi(1)(-1)

F2 = f_wi(1)(0) if x< b_wi(1)(0) ;
f_wi(1)(1) if  x< b_wi(1)(0) <=x< b_wi(1)(1);
...;
f_wi(1)(-1) if x>= b_wi(1)(-1)
'''
``````

Now I want to get max (F1+F2) and the corresponding interval. I did some searching and found this : Evaluate sum of step functions

However, since the length of intervals is not the same for these step functions, I cannot apply the solution in the link directly. Instead, I did this:

``````import numpy as np
from pandas.core.common import flatten
def func(x,b,f):
return f(np.searchsorted(b,x,side='right'))

intval= np.unique(list(flatten(b_wi)))
x=np.concatenate(((-10000),(intval(:-1)+intval(1:))/2,(10000)))  #b_wi is a subset of x. That is why I can use this.
a=np.zeros((len(x)))
for b, f in zip(b_wi,f_wi):
a=a+ func(x,b,np.asarray(f))
print(a/2)
``````

Now I get can get the maximum of (F1+F2) using

``````np.amax(a)
``````

and I can get the interval as well.

This is just a simple example I used to illustrate my question. My actual lists are longer than these and there are 100000 step functions. Since I ‘flatten’ ‘b_wi’ in order to find the corresponding interval, the length of ‘intval’ becomes too large. Hence, my method is too slow. Does anyone know how I could speed it up? I feel like I am using the wrong method. Thank you very much.

## differential equations – Piecewise functions and ODEs

I had a ODE involving `Piecewise` functions, which mathematica solved to give (in input form)

`{{Bph -> Function({r}, -((2*((-j0)*Pi*r^2 - a^2*j0*Pi*UnitStep(-a + r) + j0*Pi*r^2*UnitStep(-a + r)))/r))}}`.

Now I want to use this in solving another equation, say `mhseqn2`, which doesn’t involve any other `Piecewise` stuff, in which this function `Bph` appears as a coefficient. I had named the above out put as `sol1`, hence ran `mhseqn2 //. sol1((1)) // Simplify`. The output did not involve something `Piecewise` or the `UnitStep` function, but was

`(Derivative(1)(p)(r) == 0 && a < r) || (2*j0^2*Pi*r == Derivative(1)(p)(r) && a > r)`,

where `p` is the function I have to solve the differential equation for.

My question is how can this be converted to something `DSolve` understands, like a piecewise function. In this case the answer can be eyeballed, but what about something more complicated?

## ds.dynamical systems – On topological entropy of continuous piecewise linear maps

Let $$f:(0,1) to (0,1)$$ be a continuous piecewise linear function with $$k$$ pieces (say the turning points are $$c_1,…c_{k}$$ and slopes $$s_1,…,s_k$$). I am curious to see if $$c_1,…,c_k$$ and $$s_1,…,s_k$$ are chosen uniformly at random from (0,1), if $$f$$ will have topological entropy positive with probability $$p>0.$$ Can you suggest maybe some related literature on this? I have read papers on the field but none of them deals with “random” functions.

## How to rigorously prove from set theory that piecewise functions exist?

Suppose we have two functions $$f$$ and $$g$$ from the set $$S$$ into the set $$T$$. Suppose further that we have subsets $$S_1$$ and $$S_2$$ of $$S$$ which are disjoint and whose union is $$S$$. How does one rigorously prove, from ZFC set theory, that there is a unique function $$h$$ which agrees with $$f$$ on $$S_1$$ and agrees with $$g$$ on $$S_2$$?

## reference request – Embedding theorems for fractional Sobolev spaces \$W^{s,p}(Gamma)\$ where \$Gamma\$ is closed piecewise \$C^1\$ curve in \$Bbb R^2\$

I am interested in embedding theorems for the fractional Sobolev space $$W^{s,p}(Gamma)$$ where $$Gamma$$ is closed piecewise $$C^1$$ curve in $$Bbb R^2$$ such as the boundary of a triangle or rectangle. What are basic results for this? Also, is there some $$p$$ for which $$W_{s,p}(Gamma)subset C(Gamma)?$$

Let’s use $$Gamma=partial Omega$$ where $$Omega$$ is a square as our basic example.
This is supposedly a smooth manifold as it is homemorphic to a circle, is that right? So we can define $$W^{s,p}(Gamma)$$ as Brezis mentions in the comments of Chapter 9 in his functional analysis books.

## piecewise – Expressing \$f(x)=-x ;;; -2leq x leq 0\f(x)=x ;;; 0

Can we express functions such as:

$$f(x)=-x quad -2leq x leq 0\f(x)=x quad 0< x leq 2\f(x+4)=f(x)$$

On Mathematica? I tried something with piecewise functions but it didn’t work. Namely, I couldn’t make the last condition. I tried:

``````f(x_) := Piecewise({{-x, -2 <= x <= 0 }, {x, 0 < x <= 2 }})
f(x_ + 4) := f(x)
``````

## plotting – Different kind of PlotStyle via Piecewise

I have a `Piecewise`. For vertical line I have to add `Exclusions -> None`.

``````    a1 = 1;
b1 = 2;
c1 = 2;
L(x_) := (x - a1)/(b1 - a1);
R(x_) := (c1 - x)/(c1 - b1);
A(x_) = Simplify(Piecewise({{0, x < a1}, {L(x), a1 <= x <= b1}, {R(x), Inequality(b1, LessEqual, x, Less, c1)}, {0, x >= c1}}, 0));
p1 = Plot(A(x), {x, a1, c1 + 0.1}, PlotStyle -> {Black, Dashed}, Exclusions -> None)
``````

What are the other options available? Can I make dashed to `*` or `o`.

## Finding probabilities in a piece-wise distribution function

Suppose I have a cumulative distribution function,
enter image description here

How do I find P(0.25<X<1) since the given distribution function is not differentiable at points like x=0.5 ?

## Modeling a piece-wise objective function for a linear program?

I am attempting to design a linear program where I optimize the amount of money I make by selling good $$A$$. Selling units of $$A$$ below a threshold $$t$$ results in an income of $$a$$ dollars per unit of $$A$$ and selling quantities greater than that threshold $$t$$ results in an income of $$b$$ dollars per unit.

How might I model this as a linear objective function?

## how to plot the amplitude and phase spectrum of Fourier Transform of periodic function Abs[Sin[t]] and corresponding non-periodic piecewise function

the two function

``````x(t_) := Piecewise({{Sin(t) , 0 <= t <= Pi}}) # will get frequency function X(w)
xhat(t_) := RealAbs(Sin(t)) # will get fourier coeff ck
``````

I want to show the amplitude-frequency relations graph plotted as in Fig.D.2 between periodic function `xhat(t_)` and corresponding non-periodic function `x(t_)` Amplitude and Phase of a Periodic Signal (Fourier Series)

how to plot the amplitude and phase spectrum of a Fourier Transform in this specific pattern?

similar answers does not work for me.