This notebook is licenced under CC BY-SA 4.0.

FriCAS Tutorial (Infinite Structures)

Ralf Hemmecke <ralf@hemmecke.org>

Sources at Github.

In [1]:
)set message type off
)set output algebra off
setFormat!(FormatMathJax)$JFriCASSupport
)set message type on
In [2]:
)version
Value = "FriCAS a9422d32eb6f6b03d98ac6adfc9bf05ec0f5e8bd compiled at Sa 03 Apr 2021 00:29:13 CEST"

Stream

An important data structure is a stream. A stream can be seen as an infinite list. But it is a lazy data structure. Only finitely many elements are ever computed. And the stream knows how to compute more elements.

The simplest way to create a stream is by a comprehension scheme with an open segment.

In [2]:
[i^2 for i in 10..]
Out[2]:
\[ \left[100, 121, 144, 169, 196, 225, 256, 289, 324, 361, \ldots \right] \]

Stream comprehension also works with such-that clauses.

In [3]:
s:=[i for i in 1.. | even? i]
Out[3]:
\[ \left[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, \ldots \right] \]
In [4]:
[x for x in s | x::PrimeField(3) = 0]
Out[4]:
\[ \left[6, 12, 18, 24, 30, 36, 42, 48, 54, 60, \ldots \right] \]

FriCAS lets you also create infinite structures of infinite structures.

In [5]:
t :=[[i/j for j in 1..] for i in 1..]
Out[5]:
\[ \left[\left[1, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}, \frac{1}{5}, \frac{1}{6}, \frac{1}{7}, \frac{1}{8}, \frac{1}{9}, \frac{1}{10}, \ldots \right], \left[2, 1, \frac{2}{3}, \frac{1}{2}, \frac{2}{5}, \frac{1}{3}, \frac{2}{7}, \frac{1}{4}, \frac{2}{9}, \frac{1}{5}, \ldots \right], \left[3, \frac{3}{2}, 1, \frac{3}{4}, \frac{3}{5}, \frac{1}{2}, \frac{3}{7}, \frac{3}{8}, \frac{1}{3}, \frac{3}{10}, \ldots \right], \left[4, 2, \frac{4}{3}, 1, \frac{4}{5}, \frac{2}{3}, \frac{4}{7}, \frac{1}{2}, \frac{4}{9}, \frac{2}{5}, \ldots \right], \left[5, \frac{5}{2}, \frac{5}{3}, \frac{5}{4}, 1, \frac{5}{6}, \frac{5}{7}, \frac{5}{8}, \frac{5}{9}, \frac{1}{2}, \ldots \right], \left[6, 3, 2, \frac{3}{2}, \frac{6}{5}, 1, \frac{6}{7}, \frac{3}{4}, \frac{2}{3}, \frac{3}{5}, \ldots \right], \left[7, \frac{7}{2}, \frac{7}{3}, \frac{7}{4}, \frac{7}{5}, \frac{7}{6}, 1, \frac{7}{8}, \frac{7}{9}, \frac{7}{10}, \ldots \right], \left[8, 4, \frac{8}{3}, 2, \frac{8}{5}, \frac{4}{3}, \frac{8}{7}, 1, \frac{8}{9}, \frac{4}{5}, \ldots \right], \left[9, \frac{9}{2}, 3, \frac{9}{4}, \frac{9}{5}, \frac{3}{2}, \frac{9}{7}, \frac{9}{8}, 1, \frac{9}{10}, \ldots \right], \left[10, 5, \frac{10}{3}, \frac{5}{2}, 2, \frac{5}{3}, \frac{10}{7}, \frac{5}{4}, \frac{10}{9}, 1, \ldots \right], \ldots \right] \]

Accessing respective values is as usual with the dot notation.

In [6]:
t.500.39
Out[6]:
\[ \frac{500}{39} \]

Primes can be created by selecting numbers that fulfill the prime? predicate.

In [7]:
primes := [i for i in 2.. | prime? i]
Out[7]:
\[ \left[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, \ldots \right] \]
In [8]:
twinPrimes := [[p,p+2] for p in primes | prime?(p+2)]
Out[8]:
\[ \left[\left[3, 5\right], \left[5, 7\right], \left[11, 13\right], \left[17, 19\right], \left[29, 31\right], \left[41, 43\right], \left[59, 61\right], \left[71, 73\right], \left[101, 103\right], \left[107, 109\right], \ldots \right] \]

Stream is the underlying data structure for power series. In fact there are three different domains: UnivariateTaylorSeries, UnivariateLaurentSeries, UnivariatePuiseuxSeries.

The most general series, Puiseux series, allows fractional powers.

In [9]:
series(sqrt sin(x), x=0)
Out[9]:
\[ {x}^{\frac{1}{2}}-\frac{1}{12}\, {x}^{\frac{5}{2}}+\frac{1}{1440}\, {x}^{\frac{9}{2}}+O\left({x}^{6}\right) \]

Laurent series are series that can have a finite number of negative powers.

In [10]:
l:=laurent(1/(x*log(x)), x=1)
Out[10]:
\[ {\left(x-1\right)}^{-1}-\frac{1}{2}+\frac{5}{12}\, \left(x-1\right)-\frac{3}{8}\, {\left(x-1\right)}^{2}+\frac{251}{720}\, {\left(x-1\right)}^{3}-\frac{95}{288}\, {\left(x-1\right)}^{4}+\frac{19087}{60480}\, {\left(x-1\right)}^{5}-\frac{5257}{17280}\, {\left(x-1\right)}^{6}+\frac{1070017}{3628800}\, {\left(x-1\right)}^{7}-\frac{25713}{89600}\, {\left(x-1\right)}^{8}+\frac{26842253}{95800320}\, {\left(x-1\right)}^{9}+O\left({\left(x-1\right)}^{10}\right) \]
In [11]:
order(l)
Out[11]:
\[ -1 \]
In [12]:
taylor(cos(x), x=0)
Out[12]:
\[ 1-\frac{1}{2}\, {x}^{2}+\frac{1}{24}\, {x}^{4}-\frac{1}{720}\, {x}^{6}+\frac{1}{40320}\, {x}^{8}-\frac{1}{3628800}\, {x}^{10}+O\left({x}^{11}\right) \]
In [14]:
x := series 'x;
sin x
Out[14]:
\[ x-\frac{1}{6}\, {x}^{3}+\frac{1}{120}\, {x}^{5}-\frac{1}{5040}\, {x}^{7}+\frac{1}{362880}\, {x}^{9}-\frac{1}{39916800}\, {x}^{11}+O\left({x}^{12}\right) \]

We can check whether certain expansions agree. Of course, the vanishing of this series is only computed up to a certain order.

In [15]:
z := 2*sin(x)*cos(x) - sin(2*x)
Out[15]:
\[ O\left({x}^{21}\right) \]
In [16]:
coefficient(z,30)
Out[16]:
\[ 0 \]

You can change how many elements of the stream you want to have computed. The default is 10.

In [17]:
)set stream calc 20
In [17]:
sin x
Out[17]:
\[ x-\frac{1}{6}\, {x}^{3}+\frac{1}{120}\, {x}^{5}-\frac{1}{5040}\, {x}^{7}+\frac{1}{362880}\, {x}^{9}-\frac{1}{39916800}\, {x}^{11}+\frac{1}{6227020800}\, {x}^{13}-\frac{1}{1307674368000}\, {x}^{15}+\frac{1}{355687428096000}\, {x}^{17}-\frac{1}{121645100408832000}\, {x}^{19}+\frac{1}{51090942171709440000}\, {x}^{21}+O\left({x}^{22}\right) \]