Attachment 'floating_point_and_stability.rst'

Download

Exact and float point computations - Stability and unstability in dynamics

There are a lot of different numbers in Sage. You will need to choose which kind of numbers you want to use depending on your computations. In this worksheet we will consider the following types of numbers:

Integers, rationals and floating point

To create an integer or a rational number, you just write it as you would do on a sheet of paper

sage: 2 + 3^5    # an integer
sage: 23 / 45    # a rational number

To create a floating point number, you need to add a dot

sage: 1.0
sage: 3.25 + 22.18

Contrarily to integers and rationals, a floating point number has limited precision. Compare the results of the two following cells

sage: 2^100 + 2^10 - 2^100
sage: 2.0^100.0 + 2.0^10.0 - 2.0^100.0

Can you explain the above behavior?

Exercise: Find the smallest power n so that 2^n + 1 - 2^n and 2.0^n + 1 - 2.0^n provide different answers.

If you have an object a and want to know what kind of number it is, you can use one of the functions parent or type. The former returns the set in which your object belongs while the second one returns the type of the object (in a computer programming sense).

sage: a = 2
sage: b = 3/2
sage: c = 5.0
sage: print parent(a)
sage: print parent(b)
sage: print parent(c)
sage: print type(a)
sage: print type(b)
sage: print type(c)

Maps of the interval: fixed points and iteration

Let us consider the map f4(x) = 4x(1 − x) from the interval [0, 1] to itself.

Exercise: Prove that f4 is surjective and plot it

Show that f4(3 ⁄ 4) = 3 ⁄ 4 (in other words, the point 3 ⁄ 4 is a fixed point of f4). What do you expect from the following two commands (you have to guess whether the answer will be True or False and you can then check your answer by executing the cell):

sage: s = 3.0 / 4.0
sage: 4 * s * (1 - s) == s

Now let f7 ⁄ 2(x) = (7)/(2)x(1 − x).

Prove that f7 ⁄ 2 is not surjective on [0, 1] and that 5 ⁄ 7 is a fixed point of f7 ⁄ 2.

Exercise: Is the following True or False? (you have to guess before executing the cell)

sage: s = 5.0 / 7.0
sage: 7.0 / 2.0 * s * (1.0 - s) == s

Exercise: Perform the same two computations as above with rational numbers instead of floating point.

On a computer a floating point number is a number of the form m 2n where m (the mantissa) and n (the exponent) have some fixed bounds. In particular, floating point numbers have finite precision. Computations with floating points numbers are inaccurate but very efficient.

Compare the following computation with rationals:

sage: s = 1
sage: for i in range(10):
....:     s = (s + 2/s) / 2
sage: print s
sage: print s.numerical_approx()

and the same computation with floating point numbers:

sage: s = 1.0
sage: for i in range(10):
....:     s = (s + 2.0 / s) / 2.0
....: print s

What can you say? What are these loops computing?

Exercise: Let us consider go back to the function f7 ⁄ 2(x) = 7 ⁄ 2x(1 − x) from the interval [0,1] to itself. Starting from x0 = 5 ⁄ 7 as a rational number, compute 100 iterations of the map f. Print the result as a rational number and get an approximation using the class function numerical_approx.

Redo the same iteration starting with the floating point number 5.0 / 7.0 instead. Print the result.

What do you conclude?

Symbolic expressions

A symbolic expression is created anytime you invoke a symbolic function on exact input. For example

sage: pi
sage: sqrt(2)

Exercise: What are the parent and type of the two above examples?

As for integers and rationals, you can use numerical_approx to obtain an approximation of your number

sage: print pi.numerical_approx()
sage: print sqrt(2).numerical_approx()

Iterating a map with symbolic expressions will give you more complicated expressions.

Exercise: Startinf from x_0 = sqrt(2) - 1 as a symbolic expression apply 10 times the map x\maspto4x(1 − x).

How many characters are there in this expression?

Symbolic expressions are useful to manipulate expression trees and apply simplification rules. However, most of the time this is not what you want to use.

Algebraic numbers

You might want to perform exact computations on real numbers but integers and rationals are not enough. A field in between the rationals and the set of real numbers is the set of algebraic numbers. In Sage it is called AA. Elements of AA might be recognized because when not an exact rational they appear with a question mark at their right end

sage: AA(2)
sage: AA(2).sqrt()

Exercise: Check that the two numbers above are indeed elements of AA.

You can compare elements

sage: a = AA(2).sqrt()
sage: b = AA(3).sqrt()
sage: 213 * a < 174 * b

Taking square roots is not the only way to build elements from AA. The most universal way is to construct roots of polynomials (with coefficients in QQ or AA)

sage: x = polygen(QQ)
sage: p1 = x^3 - 3*x^2 + x - 1
sage: p2 = x^3 - x - 1
sage: r1 = p1.roots(AA)
sage: r2 = p2.roots(AA)
sage: print r1
sage: print r2
sage: a = r1[0][0]
sage: b = r2[0][0]
sage: y = polygen(AA)
sage: p = a * x^3 - b * x^2 + x - a*b
sage: p.roots(AA)[0][0]

Exercise: Construct the number (2/5)^(1/3) as an element of AA.

The advantage of AA is that it is very flexible. On the other hand it might be slow (even very slow). You can have faster numbers using number fields that are intermediate between rationals and real numbers. To construct a number field the basic syntax is as follows

sage: x = polygen(QQ)
sage: K = NumberField(x^3 - 2, 'cbrt2', embedding=AA(2)^(1/3))

Note that you have to explicitely embbed the number field in AA in order for comparison to work properly.

sage: a = K.gen()    # generator of the number field
sage: print cbrt2 > 1
sage: print cbrt2 < 2

---

Authors:
  • Vincent Delecroix
License:

CC BY-SA 3.0

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-08-12 22:01:42, 8.3 KB) [[attachment:CollatzConjecture.ipynb]]
  • [get | view] (2018-08-12 22:01:45, 100.9 KB) [[attachment:CollatzConjecture.pdf]]
  • [get | view] (2018-08-12 22:01:49, 4.8 KB) [[attachment:CollatzConjecture.rst]]
  • [get | view] (2018-08-12 22:01:53, 12.3 KB) [[attachment:Dictionaries-GraphTheory.ipynb]]
  • [get | view] (2018-08-12 22:01:56, 202.6 KB) [[attachment:Dictionaries-GraphTheory_Solutions.pdf]]
  • [get | view] (2018-08-12 22:02:00, 9.1 KB) [[attachment:Dictionaries-GraphTheory_Solutions.rst]]
  • [get | view] (2018-08-15 12:32:58, 16.0 KB) [[attachment:Fields-2018-flatsurf_and_surface_dynamics_demo.ipynb]]
  • [get | view] (2018-08-11 19:25:37, 15.0 KB) [[attachment:S_2_1.svg]]
  • [get | view] (2018-08-12 22:02:10, 13.4 KB) [[attachment:Strings-BWT.ipynb]]
  • [get | view] (2018-08-12 22:02:14, 72.4 KB) [[attachment:Strings-BWT.pdf]]
  • [get | view] (2018-08-12 22:02:17, 7.0 KB) [[attachment:Strings-BWT.rst]]
  • [get | view] (2018-08-11 19:28:42, 16.7 KB) [[attachment:chap1-first_steps.ipynb]]
  • [get | view] (2018-08-11 19:28:46, 103.1 KB) [[attachment:chap1-first_steps.pdf]]
  • [get | view] (2018-08-11 19:28:53, 7.8 KB) [[attachment:chap1-first_steps.rst]]
  • [get | view] (2018-08-11 19:29:05, 24.6 KB) [[attachment:chap1-first_steps_solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:09, 108.7 KB) [[attachment:chap1-first_steps_solutions.pdf]]
  • [get | view] (2018-08-11 19:29:12, 12.0 KB) [[attachment:chap1-first_steps_solutions.rst]]
  • [get | view] (2018-08-11 19:29:17, 30.8 KB) [[attachment:chap2-list_and_for.ipynb]]
  • [get | view] (2018-08-11 19:29:23, 110.0 KB) [[attachment:chap2-list_and_for.pdf]]
  • [get | view] (2018-08-11 19:29:26, 17.6 KB) [[attachment:chap2-list_and_for.rst]]
  • [get | view] (2018-08-11 19:29:32, 42.0 KB) [[attachment:chap2-list_and_for_Solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:35, 116.1 KB) [[attachment:chap2-list_and_for_Solutions.pdf]]
  • [get | view] (2018-08-11 19:29:38, 25.6 KB) [[attachment:chap2-list_and_for_Solutions.rst]]
  • [get | view] (2018-08-11 19:29:51, 21.1 KB) [[attachment:chap3-if-solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:58, 95.4 KB) [[attachment:chap3-if-solutions.pdf]]
  • [get | view] (2018-08-11 19:29:55, 9.0 KB) [[attachment:chap3-if-solutions.rst]]
  • [get | view] (2018-08-11 19:29:41, 11.8 KB) [[attachment:chap3-if.ipynb]]
  • [get | view] (2018-08-11 19:29:45, 91.6 KB) [[attachment:chap3-if.pdf]]
  • [get | view] (2018-08-11 19:29:48, 5.7 KB) [[attachment:chap3-if.rst]]
  • [get | view] (2018-08-11 19:30:02, 4.4 KB) [[attachment:chap4-functions.ipynb]]
  • [get | view] (2018-08-11 19:30:06, 83.1 KB) [[attachment:chap4-functions.pdf]]
  • [get | view] (2018-08-11 19:30:09, 2.3 KB) [[attachment:chap4-functions.rst]]
  • [get | view] (2018-08-11 19:30:20, 3.2 KB) [[attachment:chap5-while.ipynb]]
  • [get | view] (2018-08-11 19:30:14, 62.1 KB) [[attachment:chap5-while.pdf]]
  • [get | view] (2018-08-11 19:30:39, 1.5 KB) [[attachment:chap5-while.rst]]
  • [get | view] (2018-08-11 19:30:47, 4.0 KB) [[attachment:chap6-advanced_exercises.ipynb]]
  • [get | view] (2018-08-11 19:30:53, 69.7 KB) [[attachment:chap6-advanced_exercises.pdf]]
  • [get | view] (2018-08-11 19:31:00, 2.0 KB) [[attachment:chap6-advanced_exercises.rst]]
  • [get | view] (2018-08-12 22:02:04, 90.9 KB) [[attachment:euler.png]]
  • [get | view] (2018-08-13 21:52:57, 1.1 KB) [[attachment:flipper_nf_conversion.py]]
  • [get | view] (2018-08-15 12:18:55, 55.4 KB) [[attachment:flipper_tutorial.pdf]]
  • [get | view] (2018-08-12 21:56:57, 16.1 KB) [[attachment:floating_point_and_stability.ipynb]]
  • [get | view] (2018-08-12 21:57:01, 78.0 KB) [[attachment:floating_point_and_stability.pdf]]
  • [get | view] (2018-08-12 21:57:04, 6.6 KB) [[attachment:floating_point_and_stability.rst]]
  • [get | view] (2018-08-12 22:02:07, 17.6 KB) [[attachment:graph0.png]]
  • [get | view] (2018-08-11 23:44:38, 23.2 KB) [[attachment:intro.en.ipynb]]
  • [get | view] (2018-08-11 23:44:45, 117.5 KB) [[attachment:intro.en.pdf]]
  • [get | view] (2018-08-11 23:44:53, 12.5 KB) [[attachment:intro.en.rst]]
  • [get | view] (2018-08-12 23:25:35, 62.3 KB) [[attachment:logistic_orbit_interact.png]]
  • [get | view] (2018-08-11 19:48:34, 9.6 KB) [[attachment:random_walk.ipynb]]
  • [get | view] (2018-08-11 19:48:38, 87.9 KB) [[attachment:random_walk.pdf]]
  • [get | view] (2018-08-11 19:48:44, 5.7 KB) [[attachment:random_walk.rst]]
  • [get | view] (2018-08-15 19:25:46, 67.3 KB) [[attachment:real_and_complex_numbers.ipynb]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.