Python’s SymPy module is really cool

So I was just browsing some code, and I came across a cool module I’d never seen before: SymPy

Basically, SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS).

What is symbolic mathematics?

Symbolic computation deals with the computation of mathematical objects symbolically. This means that the mathematical objects are represented exactly, not approximately, and mathematical expressions with unevaluated variables are left in symbolic form. Symbolic computation is handling non-numerical values, this means symbols like in algebra. Variables are defined as

In simple word, “Variables are defined as Symbols in Symbolic Computation instead of defining variables as numerical values ”

This will be more clear from an example from SymPy official documentation.

Let us define a symbolic expression, representing the mathematical expression x+2xy+2y.

>>> from sympy import symbols
>>> x, y = symbols('x y')
>>> expr = x + 2*y
>>> expr
x + 2*y

Instead of evaluating to something by convention, the expression remains as just, x+2*y

>>> x*expr
x*(x + 2*y)

Here, we might have expected x(x+2y) to transform into x^2+2xy, but instead, we see that the expression was left alone. This is a common theme in SymPy.

 

The Power of Symbolic Computation

The real power of a symbolic computation system such as SymPy is the ability to do all sorts of computations symbolically.

SymPy can simplify expressions, compute derivatives, integrals, and limits, solve equations, work with matrices, and much, much more, and do it all symbolically. It includes modules for plotting, printing (like 2D pretty printed output of math formulas,), code generation, physics, statistics, combinatorics, number theory, geometry, logic, and more.

Examples from official SymPy tutorial

Solve x^2 – 2 = 0

>>> solve(x**2 - 2, x)
[-√2, √2
Compute  sin(x2)d
>>> integrate(sin(x**2), (x, -oo, oo))
√2⋅√π
─────
  2

Installation

To install SymPy run:

sudo pip install SymPy

If you already have Anaconda and want to update SymPy to the latest version, use:

conda update sympy

After installation, it is best to verify that your freshly-installed SymPy works. To do this, start up Python and import the SymPy libraries:

$ python
>>> from sympy import *

From here, execute some simple SymPy statements like the ones below:

>>> x = Symbol('x')
>>> limit(sin(x)/x, x, 0)
1
>>> integrate(1/x, x)
log(x)
 I am looking forward to using this library for mathematical computation for my IoT projects.

Currently, I am going through the tutorial and documentation to get familiar with using the software.

References:

Github: Introduction to contribution

Tutorial: http://docs.sympy.org/latest/tutorial/index.html

Documentation: http://docs.sympy.org/

Download: Releases 

 

Advertisements

Free software movement & Hacker culture

I will start this post with a quote by Kushal Das in between the session yesterday.

“The history is important, Learn about it!”

We had a session on IRC channel dgplug last night and the topic was “History of hacking and free software movement”.

The session started with this simple recent tweet by Gnome and a few replies

In this session, Kushal gave an inspiring talk on this topic, He told us about

  • TX-O computer at MIT lab,
  • How ‘Hacker Ethic’ word came into existence
  • The rise of Free Software Movement.
  • PDP – I computer
  • Richard Stallman
  • Founding FSF
  • Launching GNU
  • Open Source Initiative (OSI) by Bruce Perens and Eric S. Raymond.

Whenever we use Linux, or an open source software, Everything that we see today in Open source world, It all started with Free Software movement, which is about the freedom of users.

He also suggested us some books and resources to read about “History of hacking and free software movement”

  1. Free as in Freedom
  2. Hackers: Heroes of Computer Revolution
  3. A documentary “Revolution OS”

You can check IRC logs of dgplug here.