# Turning a Piecewise Function into a Single Continuous Expression

I'm trying to construct a function that contains two parts: one, $g(a,b)$, coming into effect when $a\lt b$, and the other, $h(a,b)$, when $b \lt a$.

The problem is that $a=b$ is a valid possibility which I do not know how to handle, even though

if $a=b$, then $g(a,b)=h(a,b)$ and $g'(a,b)=h'(a,b)$.

So there is a real result at $a=b$, but I don't know how to get it out of the equation. there must be a mathematical way to properly express this.

currently I have

f(a,b) = $$\frac{(a-b)-\sqrt{(a-b)^2}}{2(a-b)}g(a,b) + \frac{\sqrt{(a-b)^2}+(a-b)}{2(a-b)}h(a,b).$$ which clearly doesn't cut it, because at $a=b$ the result is some weird specie of infinity.

And I am still working on figuring out $g(a,b)$ and $h(a,b)$, so I can't provide you with an example at this point.

So to recap: How do you join parts of functions into one continuous arithmetic function? (without input logic)

"you can'd do that" is a valid answer, but I'd like to know why.

-
And you don't want to use a piecewise function? You want just a single expression? What kind of functions are you allowed to use? Any problem with $\max$ and $\min$? –  Arturo Magidin Jul 24 '12 at 2:38
You are essentially looking for a formula such so that $$i(a,b)=\left\{\begin{array}{ll}1 &\text{if }a\leq b\\0&\text{if }a\gt b\end{array}\right.$$Then you can define your function as $i(a,b)g(a,b) + (1-i(a,b))h(a,b)$. But $i$ will not be continuous. –  Arturo Magidin Jul 24 '12 at 2:43
Functions defined that way are called "piecewise defined functions" (or "piece-wise") –  Arturo Magidin Jul 24 '12 at 5:43
–  joriki Jul 24 '12 at 6:43
BTW: what @Arturo was doing in his second comment is in fact an instance of the so-called Iverson bracket, but in slightly different notation. Raymond, in his answer, tells you how you can express your Iverson bracket in terms of the unit step or sign functions. –  Ｊ. Ｍ. Jul 24 '12 at 10:34

Consider the Heaviside step function defined this way (other variants for $H(0)$ exist) :

$$H(x)=\begin{cases} x<0&\ 0\\[8pt] x=0&\frac 12\\[8pt] x>0&\ 1\\[8pt] \end{cases}$$

(you may too write $H$ as a $\rm{sign}$ function : $\ H(x)=\dfrac {1+\rm{sign}(x)}2$) then :

$$\boxed{\displaystyle f(a,b)= g(a,b)\cdot H(b-a)+ h(a,b)\cdot H(a-b)}$$

(with value $\dfrac{g(a,a)+h(a,a)}2$ for $a=b$ of course)

To get a 'smoothed' result you may replace $H$ by a Sigmoid function : $\ \displaystyle S(x)=\frac 1{1+e^{-x}}\$ or other variants.

Since $\ H(x)=\lim_{n->\infty} S(n\cdot x)\$ you may replace $H(x)$ by something like $S(n\cdot x)$ with any $n \gg 1$ to get a smooth transition!

Let's illustrate this with an example : $x\mapsto x^2$ for negative $x$ and $x\mapsto \cos(10 x)$ for positive $x$ illustrated with $n=1000$ and $n=100$ :

f(t)=t^2/(1+exp(1000*t))+cos(10*t)/(1+exp(-1000*t)


f(t)=t^2/(1+exp(100*t))+cos(10*t)/(1+exp(-100*t)


Hoping it helped,

-
that's pretty darn badass dude. it's not perfect but i can adapt. many thanks. –  user35945 Jul 24 '12 at 19:52
@user35945: glad it helped! One of the possible problems is exponential overflow or underflow (i.e. the resulting function should be tested for the minimal an maximal value allowed). –  Raymond Manzoni Jul 24 '12 at 20:40

If it is acceptable for you to write $$g(a,b)=u\left(\frac{a+b}{2}\right)+(b-a)\cdot g_*(a,b)\\ h(a,b)=u\left(\frac{a+b}{2}\right)+(b-a)\cdot h_*(a,b)$$ then you can write $$f(a,b)=u\left(\frac{a+b}{2}\right)+\frac{(b-a)-|b-a|}{2}\cdot g_*(a,b)+\frac{(b-a)+|b-a|}{2}\cdot h_*(a,b)$$

On the other hand, if you have to consider $g,h$ as opaque functions, it is impossible to find continuous $\varphi,\psi$ such that $$f(a,b)=\varphi(a,b)g(a,b)+\psi(a,b)h(a,b)$$ (this is because necessarily $\varphi(a,b)=1$ when $a<b$ and $0$ when $a>b$, creating a discontinuity).

In particular, if $\varphi,\psi$ are constructed from constants, the variables $a,b$, operators like $+,-,\times,/$ and continuous functions like $\exp,\log,\sin,\cos,\tan,\sqrt{\cdot},|\cdot|$ (paying attention to their domain of definition), then they are necessarily continuous.

-