A Representation of Piecewise Functions with the and its Application to Finding the Antiderivative of the of Functions

Mugilan Ganesan July 14, 2021

Abstract In this paper, we will describe a structure for the closed-form represen- tation of real-valued piecewise functions via the sign function. One possi- ble use case of this representation is calculating the indefinite integral of the absolute value of real functions. This has applications in automating repeated integration and improving the capabilities of existing symbolic computation libraries.

1 The Sign Function

The sign function sgn is a piecewise constant function that returns the sign of a .  −1, x < 0  sgn (x) = 0, x = 0 (1) 1, x > 0 It can alternatively be defined as ( |x| , x 6= 0 sgn (x) = x (2) 0, x = 0 Despite being a discontinuous function, it can be smoothly approximated by sigmoid functions, such as the error function. Now that we have defined the sign function, we will demonstrate its use in the representation of real-valued piecewise functions.

1 2 Representing Piecewise Functions

Suppose we would like to represent a real piecewise function F defined over all real numbers and composed of n subfunctions fi such that  f1 (x) , −∞ < x < α1  f (x) , α < x < α  2 1 2  . F (x) = . (3)  f (x) , α < x < α  n−1 n−2 n−1  fn (x) , αn−1 < x < ∞

Furthermore, we define the value of F at a sub-domain endpoint αi to be f (α ) + f (α ) F (α ) = i i i+1 i (4) i 2 By recognizing that  2, α < x < α  i i+1 sgn(x − αi) + sgn(αi+1 − x) = 1, x = αi or x = αi+1 (5) 0, otherwise The function F can be represented in closed-form as

n 1 X F (x) = f (x) (sgn (x − α ) + sgn (α − x)) (6) 2 k k−1 k k=1

It is necessary to define α0 = −∞ and αn = +∞. This is merely a matter of notation to always ensure that sgn(x − α0) = sgn(αn − x) = 1. For an example of equation (6), consider a function G satisfying the constraints of F and defined as ( g1(x) = x + 2, −∞ < x ≤ 2 G(x) = 2 (7) g2(x) = x , 2 < x < ∞ By applying equation (6), we can represent G as

2 1 X G(x) = g (x) (sgn (x − α ) + sgn (α − x)) 2 k k−1 k k=1 1 = [g (x)(sgn(x − α ) + sgn(2 − x)) + g (x)(sgn(x − 2) + sgn(a − x))] 2 1 0 2 2 1 + sgn(2 − x) sgn(x − 2) + 1 = (x + 2) + x2 2 2

2 3 The Absolute Value Function

The absolute value function |x| can be defined as ( x, x ≥ 0 |x| = (8) −x, x < 0 Given an arbitrary continuous function f, our goal is to find a function abs equivalent to |f(x)|. A key observation is to focus on the points at which the sign of f changes. More precisely, we can say that the sign of f changes at x = s if ∃ > 0 such that

f(x)f(y) < 0, ∀x ∈ (s − , s) , ∀y ∈ (s, s + ) (9)

As a rule of thumb for continuous functions, these points are the zeroes of odd multiplicity. The sign of f will not alternate at cusps or zeroes of even multiplicity. Let us now denote r as the set of all unique values satisfying condition (9). Our function abs can therefore be written as a piecewise function.

 c f1(x) = (−1) f(x), −∞ < x < r1  f (x) = (−1)c+1f(x), r < x < r  2 1 2  . abs(f(x)) = . (10)  f (x) = (−1)c+(n−2)f(x), r < x < r  n−1 n−2 n−1  c+(n−1) fn(x) = (−1) f(x), rn−1 < x < ∞

The subfunctions fi are explicitly defined as

c+(i−1) fi(x) = (−1) f(x) (11)

The constant c is an offset to fix the sign of the function before the first sign change. Alternatively stated, it depends upon whether f (x) is positive or neg- ative when x < r1. Given a constant a < r1, c can be computed as ( 0, f(a) > 0 1 − sgn (f (a)) c = = (12) 1, f(a) < 0 2

For abs to equal |f(x)|, they must also be equivalent at the endpoints ri. If the sign of a f changes at x = ri, it follows from Bolzano’s theorem that f(ri) = 0. Thus, the following condition must hold

abs(f(ri)) = |f(ri)| = 0 (13) If we define abs at the endpoints to satisfy equation (4), we find that

3 f (r ) + f (r ) abs(f(r )) = i i i+1 i i 2 (−1)c+i−1f(r ) + (−1)c+if(r ) = i i 2 = 0 = |f(ri)|

Thus the condition holds. Defining abs(f(ri)) in the form of equation (4) also allows it to be represented in a closed-form through equation (6).

n−1 (−1)c (1 − (−1)n) X abs(f(x)) = f (x) + (−1)c (−1)k sgn (x − r ) f (x) (14) 2 k k=1 4 Integrating the Absolute Value Function

Equation (14) is more easily subject to integration than |f(x)|. However, inte- grating it requires us to know the antiderivative of sgn (x − a) f(x) with respect to x. Lemma 1. The antiderivative of sgn (x − a) f (x) is the function F Z x F (x) = sgn (x − a) f (y) dy (15) a Proof. By the first fundamental theorem of calculus, the antiderivative F can be expressed as a definite integral. Z x F (x) + C = sgn (y − a) f (y) dy 0 Z a Z x = sgn (y − a) f (y) dy + sgn (y − a) f (y) dy 0 a We know that sgn(x−a) = sgn(b−a), ∀x ∈ (a, b] and sgn(x−a) = sgn(c−a), ∀x ∈ [c, a). In these domains, sgn(x − a) is a constant as it only changes value once at x = a. Z a Z x = sgn (0 − a) f (y) dy + sgn (x − a) f (y) dy 0 a Z x = sgn (x − a) f (y) dy + C a

4 By applying this result, we can determine that the integration of both sides returns Z (−1)c (1 − (−1)n) Z abs(f(x)) dx = f (x) dx 2 n−1 Z x c X k + (−1) (−1) sgn (x − rk) f (x) dx (16) k=1 rk 5 Examples of Antiderivatives

By using the aforementioned relation that expresses the indefinite integral of the absolute value of a function, a wide range of antiderivatives can be computed. For example, consider | ln x|. ( f (x) = (−1) ln x, 0 < x ≤ r = 1 | ln x| = 1 1 (17) f2(x) = ln x, 1 < x < ∞

In this situation, c = 1 and n = 2. The singular of sign change is r1 = 1.

Z (−1)c (1 − (−1)n) Z | ln x|dx = ln xdx 2 n−1 Z x c X k + (−1) (−1) sgn (x − rk) ln xdx k=1 rk = sgn(x − 1)[(x ln x − x) − (1 ln 1 − 1)] = sgn(x − 1)(x ln x) − sgn(x − 1)(x − 1) (18)

We know that |x| = x sgn(x). Furthermore, we can recognize that sgn(x − 1) = sgn(ln x). Therefore, we can conclude that Z | ln x|dx = x| ln x| − |x − 1| + C (19)

A more interesting example is sin x which has an infinite number of zeroes. Z j x k l x m |sin (x)| dx = − sgn (sin (x)) cos (x) + + + C (20) π π By manipulating the axial symmetry and periodicity of its roots, it can be expressed in terms of the floor and ceiling functions.

6 Conclusion

Current methods of automatically computing the antiderivative of functions rely on inadequate assumptions. One such assumption is that the function has a single zero at x = 0, which requires the domain of the function to be restricted

5 artificially. The proposed method provides the function’s actual antiderivative over its original domain. The proposed method also provides an algorithmic approach to the problem of computing antiderivatives. While the zeroes of functions may be difficult to find, preexisting numerical methods could be used to approximate them. These approximations could then be used in place of the true zeroes to provide an approximation of the antiderivative. Not only can this approach be automated, it is also more efficient for com- puting multiple definite integrals of absolute values. Rather than numerically integrating the function at the expense of high computational costs, this method provides a reusable closed-form. Once an antiderivative is computed, it can be reused in constant time. This method also opens up the possibility of automating repeated integration of absolute values. This can be used to compute the antiderivative of multiple integrals, such as volume integrals, which can improve the efficiency of their storage and calculation.

6