Ben Chuanlong Du's Blog

It is never too late to learn.

Do Calculus Using SageMath

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Comments

  1. Sage includes everything (including SymPy) from the open source world that you might want to do mathematics. This includes many libraries that are useful for numerics, like octave.

  2. Sage includes a bit of a DSL on top of Python. For example, you can type 1/2 without wrapping the integer literals, and it will return a rational. x^2 gives x squared, not xor(x, 2). I'm not sure if it automatically defines variables for you by default. This means that things that you do in an interactive Sage session might not translate directly to a Python script. On the other hand, this can be useful for interactive use (btw, SymPy also has isympy -I that does some similar things).

Pre-defined Symbols

  • x (a pre-defined variable)
  • pi (a pre-defined constant for $\pi$)
  • e (a pre-defined constant for $e$)
  • oo (a pre-defined constant for positive infinity)
  • -oo (a pre-defined constant for negative infinity)
In [12]:
x
Out[12]:
x
In [13]:
n(x)

TypeErrorTraceback (most recent call last)
<ipython-input-13-8d1e76faaf38> in <module>()
----> 1 n(x)

/usr/lib/python2.7/dist-packages/sage/misc/functional.pyc in numerical_approx(x, prec, digits, algorithm)
   1385         return numerical_approx_generic(x, prec)
   1386     else:
-> 1387         return n(prec, algorithm=algorithm)
   1388 
   1389 n = numerical_approx

sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.numerical_approx (build/cythonized/sage/symbolic/expression.cpp:36107)()

TypeError: cannot evaluate symbolic expression numerically
In [1]:
pi
Out[1]:
pi
In [10]:
n(pi)
Out[10]:
3.14159265358979
In [2]:
e
Out[2]:
e
In [11]:
n(e)
Out[11]:
2.71828182845905
In [14]:
oo
Out[14]:
+Infinity
In [15]:
-oo
Out[15]:
-Infinity

Define a Variable

Define a variable u.

In [18]:
var("u")
Out[18]:
u

Define a Function

In [ ]:
f(x) = x^3 + 1
f
x |--> x^3 + 1
In [ ]:
f(2)
9

Limitation

In [ ]:
lim(f, x=1)
x |--> 2
In [ ]:
lim((x ^ 2 - 1) / (x - 1), x=1)
2
In [ ]:
lim(f, x=1, dir="-")
x |--> 2
In [ ]:
lim(f, x=1, dir="right")
x |--> 2

Integral

In [1]:
integral(cos(x), (x, 0, pi / 2))
Out[1]:
1

Entropy

Calculate Entropy of the exponential distribution with a density function $\frac{1}{\mu} e^{-\frac{x}{\mu}}$.

In [2]:
var("u")
assume(u>0)
f(x) = 1/u * e ^ (-x/u)
integral(
    -log(f(x)) * f(x), (x, 0, oo)
)
Out[2]:
log(u) + 1

The above results shows that the entropy of th exponential distribution might be negative. As a matter of fact, the entropy goes to $-\infty$ as the parameter $\mu$ goes to 0.

Cross-entropy

In [8]:
var("u m")
assume(u>0)
assume(m>0)
f(x) = 1/u * e ^ (-x/u)
g(x) = 1/m * e ^ (-x/m)
integral(
    -log(f(x)) * g(x), (x, 0, oo)
)
Out[8]:
u*(m*log(u)/u + m^2/u^2)/m
In [9]:
c(u, m) = u*(m*log(u)/u + m^2/u^2)/m
In [13]:
c2(u) = log(u) + 1/u
In [39]:
plot(c2(u), (u, 0, 200))
Out[39]:
In [ ]:
 

Comments