Sumith Kulal
Programming Languages, verification and synthesis.
Indian Institute of Technology, Bombay
by
What do I have for you?
Offering to the whole audience
Offering to the Pythonists (Yay!)
Computer Algebra Systems and why do you need them fast?
>>> 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
>>> 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
>>> 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)
──────────────────────────
___
╲╱ π
Core capabilities|Polynomials |Calculus
Solving equations |Combinatorics |Discrete math
Matrices |Geometric Algebra| Geometry |Plotting |Physics
Statistics| Cryptography| Parsing |Printing
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
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))));
>>> 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
>>> 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
>>> 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
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
The Problem!
C++ function/file
Interface
Use it from Python
Cython
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
Cython source file
fib.pyx
C extension file
fib.c
Python Extension Module
fib.so
Let's try!
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!!
(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
6 GSoC students in the past 2 years
Had the first beta release recently
v0.1.0
Immediate
Eventually
Symbolic Computation
SymPy
SymEngine
Cython
Parts of the talk have been inspired by Kurt W. Smith's SciPy 2015 talk
Get in touch :)
"The purpose of computation is insight,
not numbers."
- Richard Hamming
Catch me at
By Sumith Kulal
Presentation of the talk "SymPy, SymEngine and the interface!" delivered at Scientific Python Conference (SciPy) India 2015 on December 15th