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]:
absabs(x)abs(x)${\lvert{x}\rvert}$
acosacos(x)acos(x)${\arccos\left(x\right)}$
acoshacosh(x)acosh(x)${\cosh^{-1}\left(x\right)}$
asinasin(x)asin(x)${\arcsin\left(x\right)}$
asinhasinh(x)asinh(x)${\sinh^{-1}\left(x\right)}$
atanatan(x)atan(x)${\arctan\left(x\right)}$
atan2atan2(x)atan2(x)${\atan2\left(x\right)}$
atanhatanh(x)atanh(x)${\tanh^{-1}\left(x\right)}$
ceilceil(x)ceil(x)${\left\lceil{x}\right\rceil}$
copysigncopysign(x)copysign(x)${\copysign\left(x\right)}$
coscos(x)cos(x)${\cos\left(x\right)}$
coshcosh(x)cosh(x)${\cosh\left(x\right)}$
degreesdegrees(x)degrees(x)${x\cdot\frac{360}{2\pi}}$
erferf(x)erf(x)${\erf\left(x\right)}$
erfcerfc(x)erfc(x)${\erfc\left(x\right)}$
expexp(x)exp(x)${e^{x}}$
expm1expm1(x)expm1(x)${e^{x}-1}$
fabsfabs(x)fabs(x)${\lvert{x}\rvert}$
factorialfact(x)x!${x!}$
factorial2'factorialk'
floorfloor(x)floor(x)${\left\lfloor{x}\right\rfloor}$
fmodfmod(x)fmod(x)${\fmod\left(x\right)}$
frexpfrexp(x)frexp(x)${\frexp\left(x\right)}$
fsumfsum(x)fsum(x)${\fsum\left(x\right)}$
gammagamma(x)gamma(x)${\Gamma\left(x\right)}$
gcdgcd(x)gcd(x)${\gcd\left(x\right)}$
hypothypot(x)hypot(x)${\hypot\left(x\right)}$
iscloseisclose(x)isclose(x)${\isclose\left(x\right)}$
isfiniteisfinite(x)isfinite(x)${\isfinite\left(x\right)}$
isinfisinf(x)isinf(x)${\isinf\left(x\right)}$
isnanisnan(x)isnan(x)${\isnan\left(x\right)}$
ldexpldexp(x)ldexp(x)${\ldexp\left(x\right)}$
lgammalog(abs(gamma(x)))log(abs(gamma(x)))${\ln\lvert\Gamma\left({x}\rvert)\right)}$
loglog(x)log(x)${\ln\left(x\right)}$
log10log10(x)log10(x)${\log_{10}\left(x\right)}$
log1plog1p(x)log1p(x)${\ln\left(1-{x}\rvert)}$
log2log2(x)log2(x)${\log_2\left(x\right)}$
modfmodf(x)modf(x)${\modf\left(x\right)}$
powpow(x)pow(x)${\pow\left(x\right)}$
radiansradians(x)radians(x)${x\cdot\frac{2\pi}{360}}$
remainderremainder(x)remainder(x)${\remainder\left(x\right)}$
sinsin(x)sin(x)${\sin\left(x\right)}$
sinhsinh(x)sinh(x)${\sinh\left(x\right)}$
sqrtsqrt(x)sqrt(x)${\sqrt{x}}$
tantan(x)tan(x)${\tan\left(x\right)}$
tanhtanh(x)tanh(x)${\tanh\left(x\right)}$
trunctrunc(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}$