Common features

Below we list some common features of Symbolica.

Expanding

Symbolica can expand an expression:

from symbolica import Expression
x = Expression.var('x')
e = (1+x)^2
print(e.expand())

yielding

x^2+2x+1

Collecting

A coefficient list in a variable can be obtained:

from symbolica import Expression
x, y = Expression.vars('x', 'y')
e = 5*x + x * y + x**2 + 5

l = e.coefficient_list(x)
for key, value in l:
    print(key, value)

yielding

x^2 1
x y+5
1 5

You can also collect in a variable:

from symbolica import Expression
x, y = Expression.vars('x', 'y')
e = 5*x + x * y + x**2 + 5

print(e.collect(x))

yielding

x^2+x*(y+5)+5

You can also provide a user-defined function to map the key and one to map the coefficient. For example:

from symbolica import Expression

x, y = Expression.vars('x', 'y')
var, coeff = Expression.funs('var', 'coeff')

e = 5*x + x * y + x**2 + 5

e = e.collect(x, key_map=lambda x: var(x), coeff_map=lambda x: coeff(x))
print(e)

yielding var(1)*coeff(5)+var(x)*coeff(y+5)+var(x^2)*coeff(1)

Derivatives

Symbolica can derive expressions with built-in and user-defined functions:

from symbolica import Expression
x = Expression.var('x')

print(Expression.parse("(1+2*x)^(5+x)").derivative(x))
print(Expression.parse("log(2*x) + exp(3*x) + sin(4*x) + cos(y*x)").derivative(x))
print(Expression.parse("f(x^2,x)").derivative(x))
print(Expression.parse("der(0,1,f(x,x^3))").derivative(x))

yielding

(2*x+1)^(x+5)*log(2*x+1)+2*(x+5)*(2*x+1)^(x+4)
2*(2*x)^-1+3*exp(3*x)+4*cos(4*x)-y*sin(x*y)
der(0,1,f(x^2,x))+2*x*der(1,0,f(x^2,x))
der(1,1,f(x,x^3))+3*x^2*der(0,2,f(x,x^3))

The built-in der function keeps counters of the number of derivatives per argument position.

Series expansion

Symbolica can also produce the Taylor series:

from symbolica import Expression

x, y = Expression.vars('x', 'y')
f = Expression.fun('f')

e = 2* x**2 * y + f(x)
e = e.taylor_series(x, 0, 2)

print(e)

Solving linear system

Symbolica can solve linear systems:

from symbolica import Expression

x, y, c = Expression.vars('x', 'y', 'c')
f = Expression.fun('f')

x_r, y_r = Expression.solve_linear_system(
    [f(c)*x + y + c, y + c**2], [x, y])
print('x =', x_r, ', y =', y_r)
x = (-c+c^2)*f(c)^-1 , y = -c^2