SymPy,
SymEngine

and the interface!

Indian Institute of Technology, Bombay

by

What do I have for you?

  • Computer Algebra Systems
  • SymPy and SymEngine

Offering to the whole audience

Offering to the Pythonists (Yay!)

  • Python Wrappers to C/C++ code

Computer Algebra Systems and why do you need them fast?

SymPy

  • SymPy is a pure Python library for symbolic mathematics
  • Code - Simple, comprehensible and easily extensible
  • SymPy is standalone
>>> from sympy.integrals import laplace_transform
>>> from sympy.abc import t, s, a
>>> laplace_transform(t**a, t, s)
(s**(-a)*gamma(a + 1)/s, 0, -re(a) < 1)

This simple :o

Examples

>>> from sympy import *
>>> init_printing()
>>> x, t, z, nu = symbols('x t z nu')
>>> diff(sin(x)*exp(x), x)
 x           x       
ℯ ⋅sin(x) + ℯ ⋅cos(x)
>>> integrate(sin(x**2), (x, -oo, oo))
  ___   ___
╲╱ 2 ⋅╲╱ π 
───────────
     2     
>>> limit(sin(x)/x, x, 0)
1

Examples

>>> solve(x**2 - 2, x)
⎡   ___    ___⎤
⎣-╲╱ 2 , ╲╱ 2 ⎦
>>> y = Function('y')
>>> dsolve(Eq(y(t).diff(t, t) - y(t), exp(t)), y(t))
           -t   ⎛     t⎞  t
y(t) = C₂⋅ℯ   + ⎜C₁ + ─⎟⋅ℯ
                ⎝     2⎠
>>> Matrix([[1, 2], [2, 2]]).eigenvals()
⎧      ____         ____       ⎫
⎪3   ╲╱ 17        ╲╱ 17    3   ⎪
⎨─ + ──────: 1, - ────── + ─: 1⎬
⎪2     2            2      2   ⎪
⎩                              ⎭
>>> besselj(nu, z).rewrite(jn)
  ___   ___
╲╱ 2 ⋅╲╱ z ⋅jn(ν - 1/2, z)
──────────────────────────
            ___
          ╲╱ π

SymPy

Core capabilities|Polynomials |Calculus

         Solving equations |Combinatorics |Discrete math

          Matrices |Geometric Algebra| Geometry |Plotting |Physics 

Statistics| Cryptography| Parsing |Printing

Why SymEngine was born?

Python too slow!

but...

Lot of algorithms and modules implemented

Pythonic interface is awesome (\m/)

Need to speed up

hence,

Write the core in C++...
...and use Python wrappers to SymPy

SymEngine

  • SymEngine is a fast symbolic manipulation library, C++
  • SymEngine was previously called CSymPy
  • Standalone CAS
RCP<const Basic> x = symbol("x");
RCP<const Basic> a = x;
RCP<const Basic> c = integer(2);

a = add(a, mul(c, pow(x, integer(3))));

Examples

>>> from symengine import *
>>> x = Symbol("x")
>>> y = Symbol("y")
>>> z = Symbol("z")
>>> ((2*x+y)**2).expand() 
4*x**2 + 4*x*y + y**2
>>> ((2*x**2+3*y)**2).expand() 
4*x**4 + 12*x**2*y + 9*y**2
>>> ((1/(y*z) - y*z)*y*z).expand() 
1 - y**2*z**2

Arithmetic

Examples

>>> from symengine.lib.symengine_wrapper import *
>>> binomial(5, 2) 
10
>>> divides(5, 2)
False
>>> prime_factors(100)
[2, 2, 5, 5]
>>> prime_factor_multiplicities(90) 
{Integer(2): 1, Integer(3): 2, Integer(5): 1}
>>> Sieve.generate_primes(50) 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
>>> totient(-15) 
8
>>> legendre(14, 15) 
-1
>>> kronecker(9, 2) 
1

Number theory

Examples

>>> from symengine import * 
>>> A = DenseMatrix(2, 2, [1, 2, 3, 4])
>>> B = DenseMatrix(2, 2, [1, 0, 0, 1])
>>> A.det()
-2
>>> A.inv()
[-2, 1]
[3/2, -1/2]

>>> A + B
[2, 2]
[3, 5]

>>> A * B 
[1, 2]
[3, 4]

Matrix

SymEngine

Plans

* Write the core in C++
* SymEngine is hot swappable SymPy core

* Make Sage use SymEngine as it's symbolic engine

Eventually...

* Full support C, Julia, Ruby and many more wrappers

* Make algorithms as efficient as possible

* Core to multiple CAS

Python wrapper to C++ code

The Problem!

C++ function/file

Interface

Use it from Python

How SymEngine does it?

Cython

  • Cython is a blend of Python and C.
  • Improving Python performace
  • Wrapping or exposing libraries

Improving Python performance

Python

C/C++

def fib(n):
    a,b = 1,1
    for i in range(n):
        a, b = a+b, a
    return a 
int fib(int n)
{
    int tmp, i, a, b;
    a = b = 1;
    for(i=0; i<n; i++) {
        tmp = a; a += b; b = tmp;
    }
    return a;
} 
def fib(int n):
    cdef int i, a, b
    a,b = 1,1
    for i in range(n):
        a, b = a+b, a
    return a 

Cython

- 1X

- 80X

- 100X

Underlying magic

Cython source file
fib.pyx

C extension file

fib.c

Python Extension Module

fib.so

Let's try!

Wrapping code

cdef extern from "string.h":
    # Describe the interface for the functions used.
    int strlen(char *c)

def get_len(char *message):
    # strlen can now be used from Cython code (but not Python)…
    return strlen(message) 

Cython code

Call from Python

>>> import len_extern
>>> len_extern.strlen
Traceback (most recent call last):
AttributeError: 'module' object has no attribute 'strlen'
>>> len_extern.get_len("woohoo!") 

Let's try!!

Contributors

(in order of first commit)

Ondřej Čertík
Dale Lukas Peterson
Thilina Bandara Rathnayake
Christopher Dembia 
Julien Rioux
Sushant Hiray 
Thomas Hisch 
Vinzent Steinberg 
Isuru Fernando

Peter Brady 
Shivam Vats 
Sumith
Sanka Rasnayaka
Abinash Meher
Govind Sahai
AMiT Kumar
Francesco Biscani 
Connor Behan

Status and Timeline

  • Basic manipulations
  • Matrix module
  • Functions module
  • Number theory module
  • Polynomial module (WIP)

6 GSoC students in the past 2 years

Had the first beta release recently
v0.1.0

Projections and Milestones

Immediate 

  • Completing the Polynomial module
  • Series expansion
  • Pattern matching

Eventually

  • Assumptions
  • Multiple modules
  • Wrappers to Julia, MATLAB...
  • Have a huge and happy community :)

Summary

Symbolic Computation

SymPy

SymEngine

Cython

Parts of the talk have been inspired by Kurt W. Smith's SciPy 2015 talk

Hope you enjoyed!

GitHub: Sumith1896

Facebook: Sumith Kulal

Email: sumith1896@gmail.com

Get in touch :)

"The purpose of computation is insight,
not numbers."

                                              - Richard Hamming

Catch me at

Made with Slides.com