Symbolic Math with Python
The opposite of differentiation is integration. Sympy provides support for both indefinite and definite integrals. You can integrate elementary functions with:
integrate(sin(x), x)
-cos(x)
You can integrate special functions too. For example:
integrate(exp(-x**2)*erf(x), x)
Definite integrals can be calculated by adding limits to the integration. If you integrate sin(x) from 0 to pi/2, you would use:
integrate(sin(x), (x, 0, pi/2))
1
Sympy also can handle some improper integrals. For example:
integrate(exp(x), (x, 0, oo))
1
Sometimes, equations are too complex to deal with analytically. In those
cases, you need to generate a series expansion and calculate an
approximation. Sympy provides the operator series to do this. For
example, if you wanted a fourth-order series expansion of cos(x) about 0,
you would use:
cos(x).series(x, 0, 4)
1 - (x**2)/2 + (x**4)/24
Sympy handles linear algebra through the use of the
Matrixclass. If you
are dealing with just numbers, you can use:
Matrix([[1,0], [0,1]])
If you want to, you can define the dimensions of your matrix explicitly. This would look like:
Matrix(2, 2, [1, 0, 0, 1])
You also can use symbolic variables in your matrices:
x = Symbol('x')
y = Symbol('y')
A = Matrix([[1,x], [y,1]])
Once a matrix is created, you can operate on it. There are functions to do dot products, cross products or calculate determinants. Vectors are simply matrices made of either one row or one column.
Doing all of these calculations is a bit of a waste if you can't print out
what you are doing in a form you can use. The most basic output is
generated with the print command. If you want to dress it up some, you can
use the pprint command. This command does some ASCII pretty-printing, using
ASCII characters to display things like integral signs. If you want to
generate output that you can use in a published article, you can make sympy
generate LaTeX output. This is done with the latex
function. Simply using
the plain function will generate generic LaTeX output. For example:
latex(x**2)
x^{2}
You can hand in modes, however, for special cases. If you wanted to generate inline LaTeX, you could use:
latex(x**2, mode='inline')
$x^{2}$
You can generate full LaTeX equation output with:
latex(x**2, mode='equation')
\begin{equation}x^{2}\end{equation}
To end, let's look at some gotchas that may crop up. The first thing to consider is the equal sign. A single equal sign is the assignment operator, while two equal signs are used for equality testing. Equality testing applies only to actual equality, not symbolic. So, testing the following will return false:
(x+1)**2 == x**2 + 2*x + 1
If you want to test whether two equations are equal, you
need to subtract one from the other, and through careful use of
expand, simplify and
trigsimp, see whether you end up with 0. Sympy
doesn't use the default Python int and float, because it provides more
control. If you have an expression that contains only numbers, the
default Python types are used. If you want to use the sympy data types, you
can use the function sympify(), or
S(). So, using Python data types, you
get:
6.2 -> 6.2000000000000002
Whereas the sympy data types give:
S(6.2) -> 6.20000000000000
Expressions are immutable in sympy. Any functions applied to them do not change the expressions themselves, but instead return new expressions.
This article touched on only the most basic elements of sympy. But, I hope you have seen that it can be very useful in doing scientific calculations. And by using the isympy console, you have the flexibility to do interactive scientific analysis and work. If some functionality isn't there yet, remember that it is under active development, and also remember that you always can chip in and offer to help out.
Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Tech Tip: Really Simple HTTP Server with Python
- BASH script to log IPs on public web server
3 hours 27 min ago - DynDNS
7 hours 3 min ago - Reply to comment | Linux Journal
7 hours 35 min ago - All the articles you talked
9 hours 59 min ago - All the articles you talked
10 hours 2 min ago - All the articles you talked
10 hours 3 min ago - myip
14 hours 28 min ago - Keeping track of IP address
16 hours 19 min ago - Roll your own dynamic dns
21 hours 32 min ago - Please correct the URL for Salt Stack's web site
1 day 44 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
It sound that this is a
It sound that this is a perfect combination between mobile phones and automotive, no doubt, android industries have begun to enter our life, and I have been thinking recently, if Obd2 auto diagnostic software can be integrated into mobile applications, it will greatly change our lives, and even bring about a revolution again. Now many shop began selling android Obd2 auto diagnostic software product, I was no exception, welcome to my shop to see what the latest android Obd2 auto diagnostic software products. car diagnostic tool
Formula correction
Liked the article: short, to the point and offering a useful introduction. A worthy mention regarding the pretty printing options is that there is also the MathML output option, for those wanting to publish online. But you could also load the sympy environment in the IPython notebook then and have IPython render your formulas with Mathjax.
Just wanted to point out a small typo for the integration of exp(x) too:
In [3]: integrate(exp(x), (x, 0, oo))
Out[3]: ∞
In [4]: integrate(exp(-x), (x, 0, oo))
Out[4]: 1