CS2 Computational Thinking for Scientists - Lab 06

CS2 Computational Thinking for Scientists - Lab 06

In this lab, we will experiment with C++, Python and Mathematica programs involving numerical and algebraic computations. As usual, create a folder on your desktop to save the Python code you will be working with and writing. Run the following commands in order to make the IDLE know about the folder:
import os
os.chdir("/Users/koc/Desktop/abc")
Change the file path from above to the one you will be using.
Also, import the math and random modules:
import math, random
since we will be using them as well.

Single-Precision Arithmetic in C/C++

The first experiment will involve the datapath width of the computer you are using. We also need to make a quick study of the integer data types:
  • short int
  • int
  • long int
  • long long int
The code is written in C++, and you don't need to compile; however, I would like you to examine it to see how it works: wordsize.cpp. We will run the code and discover the word size of each data type, which happens to be 16, 32, 64, and 64 bits, respectively. To learn about these data types, you may refer to standard textbooks on C/C++, as well as, many helpful pages, for example, see Variables and Types.

Multi-Precision Arithmetic in Python

Python does not define short or long integer types. It has only integer type, which has unlimited precision. Run IDLE and experiment with the operations:
+, -, *, //, %, **
Question: Is there a limit on the size of the number you will obtain? Answer this question by computing recursively the powers of 2 powers of, using
2**2
2**(2**2)
2**(2**(2**2))
2**(2**(2**(2**2)))
..
Try to see how far you can go, and what happens as higher powers are computed:

Mathematical Functions in Python

The Python modules math and random provide many mathematical and random functions we can use. We will experiment with the following:
  • ceil, floor
  • pow
  • factorial
  • gamma
  • randint
  • choice
  • shuffle

Multi-Precision Arithmetic in Mathematica

Mathematica also has built-in multi-precision integer arithmetic. The arithmetic operation symbols are:
+, -, *, /, Mod[], ^ 
Perform a similar experiment with Mathematica about the size of numbers you can compute:
2^2
2^(2^2)
2^(2^(2^2))
2^(2^(2^(2^2)))
...

Number Theory with Mathematica

Mathematica is much more powerful, has myriad of functions that are built in. We would like experiment with the following:
PrimeQ[ .. ]
FactorInteger[ .. ]
Binomial[n,k]
Sum[k^2, {k,1,100}]
Sum[k^2, {k,1,n}]
..

Algebraic Computations with Mathematica

There are other functions available in Mathematica. For example, we can perform computations with polynomials. We will experiment with the following:
  • InterpolatingPolynomial
  • Expand
  • Factor
  • Derivative
  • Solve
  • MatrixForm
  • Det
  • DSolve
  • RSolve
  • Chebyshev
  • Bessel
  • ...

Report: This lab requires a report. Experiment with the described functions and programs, and write yours (in Python and Mathematica only) and submit your observations and conclusions about each one them.


The lab report (if required) is due 5pm, Friday Feb 27.