Symbolic-Numeric Integration of Univariate Expressions based on Sparse Regression
Abstract
Most computer algebra systems (CAS) support symbolic integration as core functionality. The majority of the integration packages use a combination of heuristic algebraic and rule-based (integration table) methods. In this paper, we present a hybrid (symbolic-numeric) methodology to calculate the indefinite integrals of univariate expressions. The primary motivation for this work is to add symbolic integration functionality to a modern CAS (the symbolic manipulation packages of SciML, the Scientific Machine Learning ecosystem of the Julia programming language), which is mainly designed toward numerical and machine learning applications and has a different set of features than traditional CAS. The symbolic part of our method is based on the combination of candidate terms generation (borrowed from the Homotopy operators theory) with rule-based expression transformations provided by the underlying CAS. The numeric part is based on sparse-regression, a component of Sparse Identification of Nonlinear Dynamics (SINDy) technique. We show that this system can solve a large variety of common integration problems using only a few dozen basic integration rules.
1 Introduction
Symbolic integration is a core competency of most Computer Algebra Systems (CAS) and has numerous applications [1, 2, 3]. Since the advent of symbolic integration software in the 1960s [4, 5], the majority of the integration packages fall on a spectrum between algebraic methods - based on a combination of heuristics such as derivative-divide and integration-by-part methods to the Risch algorithm - and rule-based methods, incorporating thousands of specific integration rules (e.g., RUBI)[6, 7, 8]. In this paper, we develop a symbolic-numeric (hybrid) integration method, which is closer in spirit to the heuristic methods but takes a detour in numerical computation to simplify the intermediate steps.
JuliaSymbolics is a subset organization of SciML (an open source organization maintaining a collection of hundreds of scientific computing packages for the Julia programming language). Historically, SciML began as a group of ordinary differential equation (ODE) solvers [9]. Symbolic computation was added as an Embedded Domain Specific Language (eDSL) to ease the definition of ODE systems and to aid with automatic calculation of Jacobian and Hessian of such systems. With the expansion of the SciML ecosystem, the purely symbolic routines were decoupled from the ODE solvers and were collected under JuliaSymbolics [10]. Considering its history and origin, JuliaSymbolics is geared toward symbolic differentiation and numerical integration but lacks direct symbolic integration capabilities. Furthermore, SciML has grown to cover recent advances in scientific machine learning. As part of our method, we utilize one of the these new packages (DataDrivenDiffEq.jl) that implements data-driven differential equation structural estimation and identification [11].
SymbolicNumericIntegration.jl is an attempt to build a symbolic integration package by combining the symbolic manipulation and automatic differentiation capabilities of JuliaSymbolics with numerical routines such as sparse regression provided by DataDrivenDiffEq.jl.
2 The Overview of the Method
This section informally introduces the symbolic-numeric integration algorithm by a simple example. We want to find , where . We can integrate using the method of indeterminate coefficients. The main idea is to write the solution, i.e., , as a sum of multiple possible terms with unknown coefficients,
(1) |
where , for , are the unknown coefficients and , where , is the set of reasonable candidate terms (ansatz). For our example, a reasonable set of terms is . Of course, we need a better method to find than saying it should be a reasonable set! We will discuss this problem is details later (Section 3.2), but for now assume that an oracle provides . We have
(2) |
Differentiating with respect to ,
(3) |
By definition, ; therefore, . We obtain the following linear system,
(4) |
The solution to the linear the system is , , and . Therefore,
(5) |
As it should.
Note that the preceding calculations were essentially all symbolic. However, numerical computation becomes necessary to avoid relying on JuliaSymbolics to convert expressions into unique canonical forms. Identities like may be correctly applied in this case, but in general, according to Richardson’s theorem, the problem of finding canonical forms of transcendental expressions is undecided [12]. Another reason for using numerical computation is that the list of candidates may not be (and usually is not) linearly independent. Finding a linearly-independent subset of a set of expressions is facilitated using numerical methods (Section 4.3).
3 Symbolic Computation
3.1 The Main Integration Algorithm
The main symbolic-numeric integration algorithm is summarized in Algorithm 1. Let be the independent variable. For this paper, we assume that the input function to be integrated, , is a univariate expression of . Additionally, we assume that is well defined in a closed subset of the complex plane with only isolated poles.
The candidates generation algorithm (Section 3.2) produces a list of generator expressions, (by applying Equations 12, 13 and 15), where , , , and does not have a constant leading coefficient. Each is converted to a set of candidates, . According to Eq. 15, . We can ignore the constant leading coefficients because the final coefficients are calculated by the numerical part of the algorithm. Therefore, we can express s as equivalence classes of expressions where iff is a non-zero constant. Using this convention, we may write and .
For each and the corresponding , Algorithm 1 calls Algorithm 2 to find the anti-derivative of the input. If there is an acceptable solution, it returns; otherwise, Algorithm 1 fetches the next generator, , and tests the resulting candidate set, . If no solution is found after trying , it returns with a failure message. is a parameter specified by the user.
Algorithm 2 generates test points in complex plane, where is the number of the candidates, to create an -by- matrix and an -element vector filled, respectively, with the values of the derivatives of the candidate terms and the input function at the test points.
As discussed above, a potential complication at this stage is that the rows of may be linearly-dependent. We remedy this problem by finding the linearly-dependent rows and removing them from and (Section 4.3).
Using full-rank and , we find such that . As is discussed in Section 4.4, we cast the problem as an optimization task and use sparse regression to calculate . Finally, we put everything together and generate , the candidate anti-derivative of the input.
3.2 Candidates Generation
Candidate generation is the core of the Symbolic-Numeric integration algorithm. First, we describe an algorithm for a subset of expressions amenable to simple treatment and then expand to the general algorithm.
One key observation is that the form of the anti-derivative of some functions is similar to their derivative forms. For example, . The underlying reason is that these functions can be defined in term of the polynomials of the exponential function and its inverse, i.e., , , and ; therefore is closed under differentiation and integration. The functions with this property include the non-negative integer powers of , , , , and . Let’s define
(6) |
where . For these class of functions, we can obtain the candidate terms with repetitive differentiation. For example, let . We start with ( is the set of candidates), differentiate , and add the resulting terms to (ignoring the constant coefficients):
(7) |
Next, we need to differentiate each term in that was not previously processed. The first term, , gives back and the second term is equal to . Therefore, is the final answer, consistent with . The main idea is repetitive differentiation of the terms until we run out of new terms to process. Essentially, we are generating a closure of the input expression based on differentiation.
Every has a finite number of candidate terms; therefore, the preceding algorithm always terminates. We prove this for functions defined according to Eq. 6. After differentiation, each resulting term is also in the form of Eq. 6 and remains the same in all of them. In addition, remain the same (one power of converts to or vice versa). Similarly, is a constant. Therefore, the sum of the powers, , is a constant. Since for each there are only a finite number of possible expressions, we run out of new terms in a finite number of steps and the algorithm terminates.
In general, most integrands are not in and candidates cannot all be found by just repetitive differentiation; however, repetitive differentiation is still the backbone of the general algorithm. The essence of candidate generation is integration by parts,
(8) |
followed by repetitive differentiation. This process or something equivalent resides at the core of most symbolic integration packages. However, when applying this algorithm to complex expressions, multiple problems arise. At each step, there can be more than one possible split of the integrand into and . It is unknown a priori which split will result in the correct solution. Therefore, we need to explore all possibilities. Doing this naively can generate an exponential number of candidates. In fact, there is no guarantee that the process even terminates. Realizing that many intermediate expressions are shared among different paths, we can manage the number of candidates by keeping track of the intermediate results, similar to how memoization reduces the complexity of recursive algorithms. A top-down recursive algorithm with memoization usually has an equivalent bottom-up dynamic programming dual. This is the algorithmic basis of the approach discussed below.
A more systematic way to manage the intermediate expressions generated by repetitive integration by parts is provided by the continuous homotopy operators, which automate integration by parts and are useful in integrating exact expressions composed of multiple functions of independent variables and finding conservation laws in the system of differential equations [13, 14, 15]. Here, our goal is to borrow some of the machinery of the homotopy operators methodology to enhance the generation of candidate expressions. We continue with a brief and incomplete discussion of the homotopy operators technique.
Let be a vector of dependent variables of . Let be a function of and its partial derivatives with respect to . We define , and . The total derivative operator is defined as
(9) |
where is the maximum order of the partial derivatives of present in . The key to the homotopy operators technique is a method to invert , namely , where
(10) |
is the one-dimensional homotopy operator, is a dummy integration variable, and the notation means replacing with , with , and the same for other variables and partial derivatives in . Moreover, is defined as
(11) |
For the proof of these equations, refer to [13, 15]. However, we can make intuitive sense of these equations. Eq 11 has two main roles. First, it replaces (removed by the partial differentiation operator) with . This action is similar to integration by part, and, in fact, its proof is based on integration by parts. Second, it performs repetitive differentiation (by the sub-expression containing the powers of ), reminiscent of the method discussed above for functions in . Finally, the definite integral in Eq 10 on variable calculates the coefficients of the candidates.
In this paper, we use sparse regression to find the coefficients; therefore, we do not need Eq 10. Our focus is on Eq 11, but we cannot use it in its current form because the expected integrands are generally not in the form of . The key is to rewrite the integrand, , is a usable form,
(12) |
is a function that can be easily integrated (see Section 3.3), and . For example, if , then for and .
To find candidates, we follow the same process as in Eq 11 but customized for expressions conformant to Eq 12. However, we cannot simply apply Eq 11 because, for a general non-exact expression, may not be known or even bounded. Instead, we unroll the outer summation in Eq 11 to generate an ordered list of candidate generators, . We generate by integrating each in turn. We integrate the first factor of , i.e. , by multiplying by and split into and . Ignoring the constant factors, the second part can be written as . Considering that , we have (remember that is chosen to be easy to integrate). Therefore,
(13) |
In , 1 represents the constant of integration. For the term on the right, 1 is not strictly necessary but improves performance by moving terms from to . Next, we generate by repetitive differentiation similar to Eq 11. In addition, we introduce the powers of into the results by integration-by-parts, assuming and in Eq 8,
(14) |
Putting all together,
(15) |
3.3 Rule-Based Integration
Converting integrands to a form compatible with Eq. 12 and finding in Eq. 13 are facilitated by the term-rewriting and rule definition functionality of JuliaSymbolics, which includes a layered architectural design [16] to decouple term rewriting from other features, and leverages the Julia’s metaprogramming features that have been inherited from other languages in the Lisp family.
On the lowest level, TermInterface.jl and Metatheory.jl packages [17, 10, 16] provide an efficient expression rewriting engine and rule definition eDSL. Rules are written as regular Julia expressions, thus parsed into S-expressions and then efficiently compiled to callable functions that perform pattern matching and support advanced conditional selection [18, 10, 16]. JuliaSymbolics also supports the definition of equational (bidirectional) rewrite rules that can be executed through an e-graph rewriting engine (adopted from [19]), performing equality saturation.
We use JuliaSymbolics’s rewriting capabilities to find the integrals of . Nearly fifty rules are sufficient to cover elementary functions (exponential, logarithmic, trigonometric, hyperbolic, and their inverses). The following code snippets show a few example rules.
@rule integrate(^(~x, -1)) => log(~x) @rule integrate(^(~x, ~k)) => ^(~x, ~k+1) @rule integrate(^(sin(~x), ~k::is_neg)) => integrate(^(csc(~x), -~k)) @rule integrate(csc(~x)) => log(sin(~x)^-1 - cos(~x)*sin(~x)^-1)
4 Numerical Computation
4.1 Simplifying Rational Expressions
Processing rational expressions is one of the first steps in symbolic integration with a long and mathematically rich history (e.g., Hermite’s and Harowitz’ methods). In this paper, to stay with the general theme of our method, we use a symbolic-numeric (hybrid) algorithm to factor rational expressions based on finding the roots of the denominator (which is assumed to be a polynomial). Our method is broadly similar to the one proposed in [20]; however, to find the coefficients of the terms, we use sparse regression method (see below) instead of the method of residues.
4.2 Generating Random Points and Moving Toward the Poles
In line 2 of Algorithm 2, random number (test points) are generated in, , an open disk of radius centered at the origin ( is a parameter provided by the user). Therefore, should contain the majority of the unique poles of . Note that a function like has an infinity number of poles and should contain at least one of them. For the sparse regression to work properly, test points need to be near the poles of . We use a variant of the Newton-Raphson method to facilitate probing the poles. The standard Newton-Raphson method to find zeros of is the iteration , where superscript depicts an order index and not a power. The zero-finding algorithm can be transformed to a pole-finding one by changing the minus sign to plus. Therefore,
(18) |
is used by Algorithm 2 on line 4 to move the test points toward the poles.
4.3 Finding Linearly-Independent Subsets of the Candidates
Matrix in Algorithm 2 can be rank-deficient. In order to find acceptable regression solutions, we need to remove linearly-dependent columns from . This task is achieved by using pivoted QR algorithm [21]. In short, is decomposed to , where is an orthogonal rotation matrix, is an upper triangular matrix, and is the permutation matrix. We find the columns linearly dependent on other columns by locating small values on the diagonal of (here, small means having an absolute value less than a parameter , usually taken to be ) and convert to the set , used by Algorithm 2, that lists indices of linearly-dependent columns.
4.4 Sparse Regression
In Algorithm 2, we need to find such that . If is low dimensional, this can be done simply as (by construction, is a square matrix). However, this process has the drawback of tending to use all the terms, even those with numerically small coefficients, which obscures the results and differs from the expected answer to integration problems. We need a parsimonious (sparse) model such that has the minimum number of non-zero elements while still solves with an acceptable accuracy. We can achieve this by recasting the problem as an optimization problem to solve
(19) |
for , where , and is a regularization parameter. In this paper, we use the sequential thresholded least-squares (STLSQ) algorithm, which is a component of Sparse Identification of Nonlinear Dynamics (SINDy) technique [22] and uses -norm. This method has been chosen due to robust behaviour within the scope of many problems related to SINDy. However, other sparse regression algorithms, e.g., the least absolute shrinkage and selection operation (LASSO) using -norm [23], Elastic-Net or SR3 are also possible candidates. The sparse regression code is provided by DataDrivenDiffEq.jl [11].
5 Results
SymbolicNumericIntegration.jl is available at https://github.com/SciML/SymbolicNumericIntegration.jl. We tested the performance of the software using a collection of test integrals provided by RUBI based on classic calculus textbooks [8] (Table 1).
Source | Success | Failure | Total |
---|---|---|---|
Apostle [24] | 123 | 26 | 149 |
Hearn | 191 | 38 | 229 |
Stewart [25] | 220 | 77 | 297 |
Timofeev [26] | 136 | 126 | 262 |
We showcase the strengths and weaknesses of the symbolic-numeric integration algorithm with two examples (both from Apostle [24]). The algorithm can successfully solve the following integral,
(20) |
The reason for the success is that we can solve Eq. 20 after substitution , which transforms the integral to easily solvable . However, the integration algorithm does not explicitly apply the substitution. Instead, the way that the candidate generation algorithm (section 3.2) creates new candidates implicitly covers the substitution. On the other hand, the algorithm fails to solve the following simple integral,
(21) |
Here, there is no simple substitution that can solve the integral. The standard solution is obtained by expressing in term of the tangent of half-angle and then solve the resulting rational expression. While it is easy to explicitly add this procedure to the algorithm, it is not an automatic consequence of the candidate generation algorithm based on homotopy operators. However, the algorithm can solve the following two variants of the problem,
(22) |
because now the substitution works, and,
(23) |
since and the algorithm can implicitly apply the tangent of half-angle trick.
6 Conclusions
SymbolicNumericIntegration.jl provides a proof-of-concept that symbolic-numeric integration, based on a combination of symbolic candidates generation and numerical sparse regression, is able to solve many standard integration problems. Symbolic integration systems are tightly coupled to the CAS they are build on. JuliaSymbolics has strong simplification, rule-based rewriting, differentiation and code generation capabilities – all useful for the scientific machine learning workflow it is designed to support. We add the much needed feature of symbolic integration by building on the same strengths of this CAS. On the other hand, polynomials with rational or integer coefficients ( and ), which are the workhorse of most traditional CAS, currently lack robust algorithmic support. Therefore, SymbolicNumericIntegration.jl employs the strengths of JuliaSymbolics, as used in Eqs. 12, 13, and 15, and covers for its shortcomings by using numerical methods. We believe that the features of strong auto-differentiation and numerical routines combined with a relatively weak classic symbolic algebra are not unique to SciML ecosystem and are shared by many recent systems gearing toward machine learning and expect that the symbolic-numeric methodology developed in this paper can be applicable to these other systems.
Acknowledgements
This material is based upon work supported by the National Science Foundation under grant no. OAC-1835443, grant no. SII-2029670, grant no. ECCS-2029670, grant no. OAC-2103804, and grant no. PHY-2021825. We also gratefully acknowledge the U.S. Agency for International Development through Penn State for grant no. S002283-USAID. The information, data, or work presented herein was funded in part by the Advanced Research Projects Agency-Energy (ARPA-E), U.S. Department of Energy, under Award Number DE-AR0001211 and DE-AR0001222. We also gratefully acknowledge the U.S. Agency for International Development through Penn State for grant no. S002283-USAID. The views and opinions of authors expressed herein do not necessarily state or reflect those of the United States Government or any agency thereof. This material was supported by The Research Council of Norway and Equinor ASA through Research Council project "308817 - Digital wells for optimal production and drainage". Research was sponsored by the United States Air Force Research Laboratory and the United States Air Force Artificial Intelligence Accelerator and was accomplished under Cooperative Agreement Number FA8750-19-2-1000. The views and conclusions contained in this document are those of the authors and should not be interpreted as representing the official policies, either expressed or implied, of the United States Air Force or the U.S. Government. The U.S. Government is authorized to reproduce and distribute reprints for Government purposes notwithstanding any copyright notation herein.
References
- [1] Manuel Bronstein. Symbolic Integration I. Springer-Verlag, 2005.
- [2] Edmund A. Lamagna. Computer algebra concepts and techniques. CRC Press, 2018.
- [3] Jurgen Gerhard. Modern computer algebra: Third edition. Cambridge University Press, 2011.
- [4] SlagleJames R. A Heuristic Program that Solves Symbolic Integration Problems in Freshman Calculus. Journal of the ACM (JACM), 10(4):507–520, oct 1963.
- [5] Joel Moses. Symbolic Integration: The Stormy Decade. pages 427–440.
- [6] Robert H. Risch. The Problem of Integration in Finite Terms. Transactions of the American Mathematical Society, 139:167, may 1969.
- [7] Aaron Meurer, Christopher P. Smith, Mateusz Paprocki, Ondřej Čertík, Sergey B. Kirpichev, Matthew Rocklin, AMiT Kumar, Sergiu Ivanov, Jason K. Moore, Sartaj Singh, Thilina Rathnayake, Sean Vig, Brian E. Granger, Richard P. Muller, Francesco Bonazzi, Harsh Gupta, Shivam Vats, Fredrik Johansson, Fabian Pedregosa, Matthew J. Curry, Andy R. Terrel, Štěpán Roučka, Ashutosh Saboo, Isuru Fernando, Sumith Kulal, Robert Cimrman, and Anthony Scopatz. SymPy: symbolic computing in Python. PeerJ Computer Science, 3(1):e103, jan 2017.
- [8] Albert Rich, Patrick Scheibe, and Nasser Abbasi. Rule-based integration: An extensive system of symbolic integration rules. Journal of Open Source Software, 3(32):1073.
- [9] Christopher Rackauckas and Qing Nie. Differentialequations.jl–a performant and feature-rich ecosystem for solving differential equations in julia. Journal of Open Research Software, 5(1), 2017.
- [10] Shashi Gowda, Yingbo Ma, Alessandro Cheli, Maja Gwozdz, Viral B Shah, Alan Edelman, and Christopher Rackauckas. High-performance symbolic-numerics via multiple dispatch. arXiv preprint arXiv:2105.03949, 2021.
- [11] JuliusMartensen, Christopher Rackauckas, et al. Datadrivendiffeq.jl, July 2021.
- [12] Daniel Richardson. Some undecidable problems involving elementary functions of a real variable. Journal of Symbolic Logic, 33:514–520, 1 1969.
- [13] W. Hereman, B. Deconinck, and L. D. Poole. Continuous and Discrete Homotopy Operators: A Theoretical Approach made Concrete. oct 2005.
- [14] Bernard Deconinck and Michael Nivala. Symbolic integration using homotopy methods. Mathematics and Computers in Simulation, 80(4):825–836, dec 2009.
- [15] Douglas Poole and Willy Hereman. The homotopy operator method for symbolic integration by parts and inversion of divergences with applications. https://doi.org/10.1080/00036810903208155, 89(4):433–455, apr 2010.
- [16] Alessandro Cheli and Christopher Rackauckas. Automated code optimization with e-graphs, 2021.
- [17] Alessandro Cheli. Metatheory.jl: Fast and elegant algebraic computation in julia with extensible equality saturation. Journal of Open Source Software, 6(59):3078, 2021.
- [18] Yingbo Ma, Shashi Gowda, Ranjan Anantharaman, Chris Laughman, Viral Shah, and Chris Rackauckas. Modelingtoolkit: A composable graph transformation system for equation-based modeling, 2021.
- [19] Max Willsey, Chandrakana Nandi, Yisu Remy Wang, Oliver Flatt, Zachary Tatlock, and Pavel Panchekha. Egg: Fast and extensible equality saturation. Proc. ACM Program. Lang., 5(POPL), jan 2021.
- [20] NodaMatu-Tarow and MiyahiroEiichi. A hybrid approach for the integration of a rational function. Journal of Computational and Applied Mathematics, 40(3):259–268, jul 1992.
- [21] G. H. Golub and C. F. Van Loan. Matrix computations. Johns Hopkins University Press, 2013.
- [22] Steven L. Brunton, Joshua L. Proctor, J. Nathan Kutz, and William Bialek. Discovering governing equations from data by sparse identification of nonlinear dynamical systems. Proceedings of the National Academy of Sciences of the United States of America, 113(15):3932–3937, apr 2016.
- [23] Robert Tibshirani. Regression shrinkage and selection via the lasso. Journal of the Royal Statistical Society: Series B (Methodological), 58:267–288, 1 1996.
- [24] Tom M. Apostol. Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra. J. Wiley, 1967.
- [25] J. Stewart and R.S. Andre. Calculus. Mathematics Series. Brooks/Cole, 1991.
- [26] A. F. Timofeev. Integration of Functions. 1948.