SymPy – Installation ”; Previous Next SymPy has one important prerequisite library named mpmath. It is a Python library for real and complex floating-point arithmetic with arbitrary precision. However, Python”s package installer PIP installs it automatically when SymPy is installed as follows − pip install sympy Other Python distributions such as Anaconda, Enthought Canopy, etc., may have SymPy already bundled in it. To verify, you can type the following in the Python prompt − >>> import sympy >>> sympy.__version__ And you get the below output as the current version of sympy − ”1.5.1” Source code of SymPy package is available at https://github.com/sympy/sympy. Print Page Previous Next Advertisements ”;
Category: sympy
SymPy – Substitution
SymPy – Substitution ”; Previous Next One of the most basic operations to be performed on a mathematical expression is substitution. The subs() function in SymPy replaces all occurrences of first parameter with second. >>> from sympy.abc import x,a >>> expr=sin(x)*sin(x)+cos(x)*cos(x) >>> expr The above code snippet gives an output equivalent to the below expression − $sin^2(x)+cos^2(x)$ >>> expr.subs(x,a) The above code snippet gives an output equivalent to the below expression − $sin^2(a)+cos^2(a)$ This function is useful if we want to evaluate a certain expression. For example, we want to calculate values of following expression by substituting a with 5. >>> expr=a*a+2*a+5 >>> expr The above code snippet gives an output equivalent to the below expression − $a^2 + 2a + 5$ expr.subs(a,5) The above code snippet gives the following output − 40 >>> from sympy.abc import x >>> from sympy import sin, pi >>> expr=sin(x) >>> expr1=expr.subs(x,pi) >>> expr1 The above code snippet gives the following output − 0 This function is also used to replace a subexpression with another subexpression. In following example, b is replaced by a+b. >>> from sympy.abc import a,b >>> expr=(a+b)**2 >>> expr1=expr.subs(b,a+b) >>> expr1 The above code snippet gives an output equivalent to the below expression − $(2a + b)^2$ Print Page Previous Next Advertisements ”;
SymPy – Introduction
SymPy – Introduction ”; Previous Next SymPy is a Python library for performing symbolic computation. It is a computer algebra system (CAS) that can be used either as a standalone application, as a library to other applications. Its live session is also available at https://live.sympy.org/. Since it is a pure Python library, it can be used as interactive mode and as a programmatic application. SymPy has now become a popular symbolic library for the scientific Python ecosystem. SymPy has a wide range of features applicable in the field of basic symbolic arithmetic, calculus, algebra, discrete mathematics, quantum physics, etc. SymPy is capable of formatting the results in variety of formats including LaTeX, MathML, etc. SymPy is distributed under New BSD License. A team of developers led by Ondřej Čertík and Aaron Meurer published first version of SymPy in 2007. Its current version is 1.5.1. Some of the areas of applications of SymPy are − Polynomials Calculus Discrete maths Matrices Geometry Plotting Physics Statistics Combinatorics Print Page Previous Next Advertisements ”;
SymPy – evalf() function
SymPy – evalf() function ”; Previous Next This function evaluates a given numerical expression upto a given floating point precision upto 100 digits. The function also takes subs parameter a dictionary object of numerical values for symbols. Consider following expression >>> from sympy.abc import r >>> expr=pi*r**2 >>> expr The above code snippet gives an output equivalent to the below expression − $Pi{r^2}$ To evaluate above expression using evalf() function by substituting r with 5 >>> expr.evalf(subs={r:5}) The above code snippet gives the following output − 78.5398163397448 By default, floating point precision is upto 15 digits which can be overridden by any number upto 100. Following expression is evaluated upto 20 digits of precision. >>> expr=a/b >>> expr.evalf(20, subs={a:100, b:3}) The above code snippet gives the following output − 33.333333333333333333 Print Page Previous Next Advertisements ”;
SymPy – Lambdify() function
SymPy – Lambdify() function ”; Previous Next The lambdify function translates SymPy expressions into Python functions. If an expression is to be evaluated over a large range of values, the evalf() function is not efficient. lambdify acts like a lambda function, except it converts the SymPy names to the names of the given numerical library, usually NumPy. By default, lambdify on implementations in the math standard library. >>> expr=1/sin(x) >>> f=lambdify(x, expr) >>> f(3.14) The above code snippet gives the following output − 627.8831939138764 The expression might have more than one variables. In that case, first argument to lambdify() function is a list of variables, followed by the expression to be evaluated. >>> expr=a**2+b**2 >>> f=lambdify([a,b],expr) >>> f(2,3) The above code snippet gives the following output − 13 However, to leverage numpy library as numerical backend, we have to define the same as an argument for lambdify() function. >>> f=lambdify([a,b],expr, “numpy”) We use two numpy arrays for two arguments a and b in the above function. The execution time is considerably fast in case of numpy arrays. >>> import numpy >>> l1=numpy.arange(1,6) >>> l2=numpy.arange(6,11) >>> f(l1,l2) The above code snippet gives the following output − array([ 37, 53, 73, 97, 125], dtype=int32) Print Page Previous Next Advertisements ”;
SymPy – Symbolic Computation
SymPy – Symbolic Computation ”; Previous Next Symbolic computation refers to development of algorithms for manipulating mathematical expressions and other mathematical objects. Symbolic computation integrates mathematics with computer science to solve mathematical expressions using mathematical symbols. A Computer Algebra System (CAS) such as SymPy evaluates algebraic expressions exactly (not approximately) using the same symbols that are used in traditional manual method. For example, we calculate square root of a number using Python”s math module as given below − >>> import math >>> print (math.sqrt(25), math.sqrt(7)) The output for the above code snippet is as follows − 5.0 2.6457513110645907 As you can see, square root of 7 is calculated approximately. But in SymPy square roots of numbers that are not perfect squares are left unevaluated by default as given below − >>> import sympy >>> print (sympy.sqrt(7)) The output for the above code snippet is as follows − sqrt(7) It is possible to simplify and show result of expression symbolically with the code snippet below − >>> import math >>> print (math.sqrt(12)) The output for the above code snippet is as follows − 3.4641016151377544 You need to use the below code snippet to execute the same using sympy − ##sympy output >>> print (sympy.sqrt(12)) And the output for that is as follows − 2*sqrt(3) SymPy code, when run in Jupyter notebook, makes use of MathJax library to render mathematical symbols in LatEx form. It is shown in the below code snippet − >>> from sympy import * >>> x=Symbol (”x”) >>> expr = integrate(x**x, x) >>> expr On executing the above command in python shell, following output will be generated − Integral(x**x, x) Which is equivalent to $int mathrm{x}^{x},mathrm{d}x$ The square root of a non-perfect square can be represented by Latex as follows using traditional symbol − >>> from sympy import * >>> x=7 >>> sqrt(x) The output for the above code snippet is as follows − $sqrt7$ A symbolic computation system such as SymPy does all sorts of computations (such as derivatives, integrals, and limits, solve equations, work with matrices) symbolically. SymPy package has different modules that support plotting, printing (like LATEX), physics, statistics, combinatorics, number theory, geometry, logic, etc. Print Page Previous Next Advertisements ”;