# Goulib.expr¶

math expressions (see polynomials for more)

In [1]:
from Goulib.notebook import *
from Goulib.expr import *
from Goulib.table import Table
from math import pi,sin
from Goulib.math2 import sqrt


Expr can be created from:

• numbers
• text (python-like formula)

Expr has LaTeX representation in notebooks

In [2]:
Expr(3**5) # is evaluated before Expr is created

Out[2]:
${243}$
In [3]:
e=Expr(3)**Expr(5) # combine Expr essions
h('as LaTeX (default) :',e)
print('as math formula :',e)
print('as python code :',repr(e))
print('evaluated :',e())

as LaTeX (default) : ${3^5}$
as math formula : 3^5
as python code : 3**5
evaluated : 243

In [4]:
e=Expr('5**3+(-3^2)') # ^ and ** are both considered as power operator for clarity+compatibility
h('as LaTeX (default) :',e)
print('as math formula :',e)
print('as python code :',repr(e))
print('evaluated :',e())

as LaTeX (default) : ${5^3-3^2}$
as math formula : 5^3-3^2
as python code : 5**3-3**2
evaluated : 116


## Functions¶

In [5]:
e=Expr('x^2')
h('an Expr may contain variables :',e)
h('and be evaluated as a function : for x=2, ',e,'=',e(x=2))

an Expr may contain variables : ${x^2}$
and be evaluated as a function : for x=2, ${x^2}$ = 4

all functions defined in math module can be used:

In [6]:
functions=default_context.functions # functions known by default to Expr
t=Table()
for n in functions:
f=functions[n][0] # get the function itself
try:
e=Expr(f)
t.append([n,repr(e),str(e),e])
except Exception as e:
t.append([n,e])
t

Out[6]:
 abs abs(x) abs(x) ${\lvert{x}\rvert}$ acos acos(x) acos(x) ${\arccos\left(x\right)}$ acosh acosh(x) acosh(x) ${\cosh^{-1}\left(x\right)}$ asin asin(x) asin(x) ${\arcsin\left(x\right)}$ asinh asinh(x) asinh(x) ${\sinh^{-1}\left(x\right)}$ atan atan(x) atan(x) ${\arctan\left(x\right)}$ atan2 atan2(x) atan2(x) ${\atan2\left(x\right)}$ atanh atanh(x) atanh(x) ${\tanh^{-1}\left(x\right)}$ ceil ceil(x) ceil(x) ${\left\lceil{x}\right\rceil}$ copysign copysign(x) copysign(x) ${\copysign\left(x\right)}$ cos cos(x) cos(x) ${\cos\left(x\right)}$ cosh cosh(x) cosh(x) ${\cosh\left(x\right)}$ degrees degrees(x) degrees(x) ${x\cdot\frac{360}{2\pi}}$ erf erf(x) erf(x) ${\erf\left(x\right)}$ erfc erfc(x) erfc(x) ${\erfc\left(x\right)}$ exp exp(x) exp(x) ${e^{x}}$ expm1 expm1(x) expm1(x) ${e^{x}-1}$ fabs fabs(x) fabs(x) ${\lvert{x}\rvert}$ factorial fact(x) x! ${x!}$ factorial2 'factorialk' floor floor(x) floor(x) ${\left\lfloor{x}\right\rfloor}$ fmod fmod(x) fmod(x) ${\fmod\left(x\right)}$ frexp frexp(x) frexp(x) ${\frexp\left(x\right)}$ fsum fsum(x) fsum(x) ${\fsum\left(x\right)}$ gamma gamma(x) gamma(x) ${\Gamma\left(x\right)}$ gcd gcd(x) gcd(x) ${\gcd\left(x\right)}$ hypot hypot(x) hypot(x) ${\hypot\left(x\right)}$ isclose isclose(x) isclose(x) ${\isclose\left(x\right)}$ isfinite isfinite(x) isfinite(x) ${\isfinite\left(x\right)}$ isinf isinf(x) isinf(x) ${\isinf\left(x\right)}$ isnan isnan(x) isnan(x) ${\isnan\left(x\right)}$ ldexp ldexp(x) ldexp(x) ${\ldexp\left(x\right)}$ lgamma log(abs(gamma(x))) log(abs(gamma(x))) ${\ln\lvert\Gamma\left({x}\rvert)\right)}$ log log(x) log(x) ${\ln\left(x\right)}$ log10 log10(x) log10(x) ${\log_{10}\left(x\right)}$ log1p log1p(x) log1p(x) ${\ln\left(1-{x}\rvert)}$ log2 log2(x) log2(x) ${\log_2\left(x\right)}$ modf modf(x) modf(x) ${\modf\left(x\right)}$ pow pow(x) pow(x) ${\pow\left(x\right)}$ radians radians(x) radians(x) ${x\cdot\frac{2\pi}{360}}$ remainder remainder(x) remainder(x) ${\remainder\left(x\right)}$ sin sin(x) sin(x) ${\sin\left(x\right)}$ sinh sinh(x) sinh(x) ${\sinh\left(x\right)}$ sqrt sqrt(x) sqrt(x) ${\sqrt{x}}$ tan tan(x) tan(x) ${\tan\left(x\right)}$ tanh tanh(x) tanh(x) ${\tanh\left(x\right)}$ trunc trunc(x) trunc(x) ${\left\lfloor{x}\right\rfloor}$
In [7]:
e=Expr(sqrt) #(Expr(pi))+Expr(1/5)
h('as LaTeX (default) :',e)
print('as math formula :',e)
print('as python code :',repr(e))
print('evaluated :',e())

as LaTeX (default) : ${\sqrt{x}}$
as math formula : sqrt(x)
as python code : sqrt(x)
evaluated : sqrt(x)

In [8]:
e1=Expr('3*x+2') #a very simple expression defined from text
e1

Out[8]:
${3x+2}$
In [9]:
e1=Expr(lambda x:3*x+2) #the same expression defined from a lambda function
e1

Out[9]:
${3x+2}$
In [10]:
def f(x):
return 3*x+2
Expr(f) #the same expression defined from a regular (simple...) function

Out[10]:
${3x+2}$
In [11]:
e3=Expr(sqrt)(e1) #Expr can be composed
e3

Out[11]:
${\sqrt{3x+2}}$
In [12]:
print(e3(x=1)) # Expr can be evaluated as a function
print(e3((pi-4)/6)) #the x variable is implicit

2.23606797749979
1.2533141373155001

In [13]:
e1([-2,1,0,1,2]) # Expr can be evaluated at different x values at once

Out[13]:
[-4, 5, 2, 5, 8]
In [14]:
e3.plot()  # Expr can be also plotted. Note the X axis is automatically restricted to the definition domain

Out[14]:
In [15]:
Expr('1/x').plot(x=range(-100,100))

Out[15]:

## multivariable¶

In [16]:
e=Expr('(-b+sqrt(b^2-4*a*c))/(2*a)') #laTex is rendered with some simple simplifications
e

Out[16]:
${\frac{-b+\sqrt{b^2-4ac}}{2a}}$
In [17]:
e(a=1) # substitution doesn't work yet ...

Out[17]:
${\frac{-b+\sqrt{b^2-4ac}}{2a}}$

## complex¶

In [18]:
e=Expr("e**(i*pi)")
e

Out[18]:
${e^{ipi}}$
In [19]:
e() # should be -1 one day...

Out[19]:
${e^ipi}$