International Journal of Computer Applications (0975 – 8887) Volume 46– No.3, May 2012

Applications of Symbolic computation in C++ Programming Language

Satabaldiyev Askar Bakytzhanuly Latuta Konstantin Nikolayevich SuleymanDemirel University SuleymanDemirel University Almaty, Kazakhstan Almaty, Kazakhstan

ABSTRACT symbolic form, as opposed to numeric computation that deals This paper contains the brief information about symbolic with computation techniques. The location of symbolic approximations of specific numerical quantities expressed by computation within Computer Language classification is those symbols. The software that implements symbolic defined. Programming languages (PL) can be divided into two calculations might be user for symbolic differentiation, main areas: imperative PL and declarative PL. Declarative PL substitution one expression into another, finding polynomial, is generally used for logical and functional programming (ex: etc...generally for every computation with mathematical terms PROLOG, LISP, ML, Haskell,…) Symbolic computation for well-known algorithms. Symbolic computations are more techniques are commonly derived from both, logical and exact rather than numeric solutions. The reason is that functional programming. numeric solutions are more approximate and they are oriented Imperative programming languages are practically used for for specific cases, but symbolic computations are more numerical computations. Some of well-known examples of general. Because of this, symbolic computation methods may imperative PL’s are C, C++, Java, MATLAB, etc… But for need to take long time and resources to solve problems. last few years, the programming languages mentioned above Numeric computation methods are much faster. Some have been oriented to solve problems symbolically. problems can be better solved using symbolical way, while The general idea of this paper is to provide the other can be solved better in a numerical way[11]. implementation of well-known mathematical problems In Computer Science there exist many programming symbolically solved on the basis of C++ programming languages, which are used for numerical applications as language. It follows some typical examples to illustrate the solution to problems. properties of symbolic C++. Programming languages (PL) can be divided into two main areas: imperative PL and declarative PL. Declarative PL is generally used for logical and functional programming (ex: General Terms PROLOG, LISP, ML, Haskell,…) Symbolic computation Programming languages, symbolic computation, logical techniques are commonly derived from both, logical and programming, functional programming functional programming. Imperative programming languages are practically used for numerical computations. Some of well-known examples of Keywords imperative PL are C, C++, Java, MATLAB, etc… But for last Symbolic computation, C++, “symbolic++.h” few years, the programming languages mentioned above have been oriented to solve problems symbolically. 1. INTRODUCTION General classification of programming languages Symbolic computation relates to algorithms and software for is illustrated in Figure 1 manipulating mathematical expressions and equations in

Programming languages(PL)

Declarative PL Imperative PL

Logical PL PROLOG, Functional PL ALGOL, C, C++ LISP ML, Haskell, ... Java, MATLAB

Symbolic computation techniques

Figure1: Classification of programming languages

6 International Journal of Computer Applications (0975 – 8887) Volume 46– No.3, May 2012

Symbolic computation techniques (SCT) are created from There are not much publications about symbolic C++. There is knowledge logical and functional programming techniques. only one textbook and some papers [7],[8],[9] Some examples of SCT open-source, cost free tools are given in the following list: Axiom, Bergmann, Calc, CoCoA, There is an extensive library for symbolic C++, namely DoCon, DCAS, Eigenmath, Franklin Math, FriCAS, GAP, “GiNaC”[10], which is widely used for projects with symbolic etc… C++. The following examples illustrate the basic idea of symbolic computation applying the core library Also there are more than 30 commercial SCT tools. Detailed “symbolicc++.h”, without using extensive “GiNaC” library. information can be found in reference [1], [2], [3], [4], [5], [6].

• Header files 1 • Declaration of symbolic variables 2 • Symbolic computations 3 • Output results 4

Figure 2: General procedure for symbolic C++ program 2. APPLICATION OF SYMBOLIC C++ 2.4 Series

This chapter demonstrates the application of symbolic C++ Table 4. List of functions, descriptions and source codes programming for the most common mathematical subjects, for examples with series such as Vector Algebra, Algebra, Polynomials, and Series. Function Description Source code 2.1 Vector Algebra Taylor series expansion taylor.cpp

Table 1.List of functions, descriptions and source codes for 2.5 Source codes and output examples from vector algebra Function Description Source code 2.5.1 Vector addition and subtraction + Vector addition and veca.cpp // "veca.cpp" - subtraction // Vector addition and subtraction | Scalar product sp.cpp #include % Cross product cp.cpp #include "symbolicc++.h" Gradient grad.cpp using namespace std; Divergence diverge.cpp int main(void){ // 1. Block: Declaration of symbolic variables 2.2 Algebra Symbolic x("x", 2), y("y", 2), z("z", 2), s("s", 2);

Table 2.List of functions, descriptions and source codes for // 2. Block: Computations examples from algebra z = (x + y); Function Description Source code s = (x - y); // 3. Block: Output the results Df Differentiation diff.cpp cout<< "Addition z = " << z <

} Table 3.List of functions, descriptions and source codes for examples with polynomials /* Sample output: Function Description Source code Addition z = Diff Multivariable poly.cpp [x0+y0] polynomial [x1+y1] differentiation

Legendre Solution of Legendre legendre.cpp Subtraction s = differential equations [x0-y0] [x1-y1] */

7 International Journal of Computer Applications (0975 – 8887) Volume 46– No.3, May 2012

This symbolic computation gives directly the results of vector // Gradient example addition and subtraction for the given vectors #include 2.5.2 Scalar product #include "symbolicc++.h" using namespace std; // "sp.cpp"

// Scalar product int main(void){

// 1. Block: Declaration of symbolic variables #include Symbolic x("x"), y("y"), z("z"); #include "symbolicc++.h" Symbolic f = x*x+y*y+z*z; using namespace std; Symbolic res("res", 3);

// 2. Block: Computations int main(void){ res(0) = df(f,x); // 1. Block: Declaration of symbolic variables res(1) = df(f,y); Symbolic x("x", 2), y("y", 2), z("z", 2); res(2) = df(f,z);

// 2. Block: Computations // 3. Block: Output the results z = (x|y); cout<< "Gradient : "<< res <

// 3. Block: Output the results system("PAUSE"); cout<< "z = " << z <

} system("PAUSE"); /* return 0; Sample output: } Gradient : /* [2*x] Sample output: [2*y] z = x0*y0+x1*y1 [2*z] */ */ This symbolic computation gives directly the result of scalar This symbolic computation gives directly the result of product for the given expression gradient for the given expression

2.5.3 Cross product // "cp.cpp" 2.5.5 Divergence // Cross product // "diverge.cpp" // Divergence #include #include "symbolicc++.h" #include using namespace std; #include "symbolicc++.h" using namespace std; int main(void){ // 1. Block: Declaration of symbolic variables int main(void){ Symbolic x("x", 3), y("y", 3), z("z", 3); // 1. Block: Declaration of symbolic variables Symbolic x("x"), y("y"), z("z"); // 2. Block: Computations Symbolic f = x*x+y*y+z*z; z = (x % y); // 2. Block: Computations // 3. Block: Output the results Symbolic res = df(f,x) + df(f,y) + df(f,z); cout<< "z = " << z <

2.5.6 Differentiation 2.5.4 Gradient // "diff.cpp" // "grad.cpp" // Differentiation

8 International Journal of Computer Applications (0975 – 8887) Volume 46– No.3, May 2012

int main(void){ #include // 1. Block: Declaration of symbolic variables #include "symbolicc++.h" Multinomial a("a"); Multinomial b("b"); using namespace std; Multinomial x("x"); int main(void){ Multinomial p = a*(x^3) + b*(x^2) + 1; // 1. Block: Declaration of symbolic variables // 2. Block: Computations Symbolic x("x"); Symbolic y = sin(x) + cos(x); Multinomial res = Diff(p, "x"); // 2. Block: Computations Symbolic res = df(y,x); // 3. Block: Output the results cout<< "Diff(p,x) => "<< res < (3)ax^2 + (2)bx Sample output: */ Differentiation : res = cos(x)-sin(x) This symbolic computation gives directly the result of */ multivariable polynomial differentiation for the given The general terms after differentiation with symbolic computation are multinomial shown. 2.5.9 Solution of Legendre differential equations 2.5.7 Integration // legendre.cpp // "diff.cpp" // Legendre polynomial // Integration #include #include #include "symbolicc++.h" #include "symbolicc++.h" #include "legendre.h" using namespace std; using namespace std; int main(void){ int main(void){ int n=5; // 1. Block: Declaration of symbolic variables Symbolic x("x"); Symbolic a("a"), t("t"), ; Legendre P(n,x); Symbolic f = exp(-a * t); // Calculate the first few Legendre polynomials // 2. Block: Computations cout<< "P(0) = " << P < 0 return 0; 2.5.8 Multivariable polynomial differentiation } // "poly.cpp" // Multivariable polynomial differentiation /* Sample output: #include P(0) = 1 #include "symbolicc++.h" P(1) = x #include "multinomial.h" P(2) = 3/2*x^(2)-1/2 P(3) = 5/2*x^(3)-3/2*x using namespace std; P(4) = 35/8*x^(4)-15/4*x^(2)+3/8 P(5) = 63/8*x^(5)-35/4*x^(3)+15/8*x

9 International Journal of Computer Applications (0975 – 8887) Volume 46– No.3, May 2012

*/ 0 This symbolic computation gives directly the result of Taylor */ series expansion for the given series. This symbolic computation shows the first few Legendre 3. CONCLUSION polynomials This paper gives brief information about symbolic computation techniques. The location of symbolic 2.5.10 Taylor series expansion computation within programming language is illustrated. // taylor.cpp Nowadays, imperative programming languages, such as C++, // Taylor are generally used for numerical computations. The approach described in this paper can give the key idea to expand #include solution techniques. #include "symbolicc++.h" Accordingly, symbolic computation techniques can be expanded to solve any sort of problems. using namespace std; The general idea of this paper is to provide the implementation of well-known mathematical problems int factorial(int N){ symbolically solved on the basis of C++ programming int result=1; language. The properties of symbolic C++ have been for(int i=2;i<=N;i++) result *= i; illustrated using typical examples. return result; } 4. ACKNOWLEDGMENT

The authors would like to express gratitude to the research int main(void){ department of the University of Technology (em. Niyazi Ari, int i, j, n=3; Prof. Dr. sch. Techn. ETH) Zurich, Switzerland. Symbolic u("u"), x("x"), result; Symbolic u0("",n), y("",n); 5. REFERENCES u = u[x]; [1] Comparison of systems u0(0) = u*u+x; http://en.wikipedia.org/wiki/Comparison_of_computer_a for(j=1;j

http://www.mat.univie.ac.at/~slc/divers/software.html // substitution of initial conditions for(i=1;i0;j--) u0(i) = u0(i)[y(j)==u0(j-1)]; http://orms.mfo.de/about for(i=0;i

/* [11] “Applications of Symbolic computation in MATLAB” by Sample output Zhaparov M.K., Guvercin S. u(x) = x+3/2*x^(2)+4/3*x^(3)+1

10