$$ \newcommand{\Oof}[1]{\mathcal{O}(#1)} \newcommand{\F}{\boldsymbol{F}} \newcommand{\J}{\boldsymbol{J}} \newcommand{\x}{\boldsymbol{x}} \renewcommand{\c}{\boldsymbol{c}} $$

 

 

 

A Python program with a library function

Imagine you stand on a distance, say \( 10 \) m away, watching someone throwing a ball upwards. A straight line from you to the ball will then make an angle with the horizontal that increases and decreases as the ball goes up and down. Let us consider the ball at a particular moment in time, at which it has a height of \( 10 \) m.

What is the angle of the line then? Again, this could easily be done with a calculator, but we continue to address gentle mathematical problems when learning to program. Before thinking of writing a program, one should always formulate the algorithm, i.e., the recipe for what kind of calculations that must be performed. Here, if the ball is \( x \) m away and \( y \) m up in the air, it makes an angle \( \theta \) with the ground, where \( \tan\theta = y/x \). The angle is then \( \tan^{-1}(y/x) \).

Let us make a Python program for doing these calculations. We introduce names x and y for the position data \( x \) and \( y \), and the descriptive name angle for the angle \( \theta \). The program is stored in a file ball_angle_first_try.py:

x = 10             # Horizontal position
y = 10             # Vertical position

angle = atan(y/x)        

print (angle/pi)*180

Before we turn our attention to the running of this program, let us take a look at one new thing in the code. The line angle = atan(y/x), illustrates how the function atan, corresponding to \( \tan^{-1} \) in mathematics, is called with the ratio y/x as input parameter or argument. The atan function takes one argument, and the computed value is returned from atan. This means that where we see atan(y/x), a computation is performed (\( \tan^{-1}(y/x) \)) and the result "replaces" the text atan(y/x). This is actually no more magic than if we had written just y/x: then the computation of y/x would take place, and the result of that division would replace the text y/x. Thereafter, the result is assigned to the name angle on the left-hand side of =.

Note that the trigonometric functions, such as atan, work with angles in radians. The return value of atan must hence be converted to degrees, and that is why we perform the computation (angle/pi)*180. Two things happen in the print statement: first, the computation of (angle/pi)*180 is performed, resulting in a real number, and second, print prints that real number. Again, we may think that the arithmetic expression is replaced by its results and then print starts working with that result.

If we next execute ball_angle_first_try.py, we get an error message on the screen saying

NameError: name 'atan' is not defined
WARNING: Failure executing file: <ball_angle_first_try.py>

We have definitely run into trouble, but why? We are told that

name 'atan' is not defined

so apparently Python does not recognize this part of the code as anything familiar. On a pocket calculator the inverse tangent function is straightforward to use in a similar way as we have written in the code. In Python, however, this function has not yet been imported into the program. A lot of functionality is available to us in a program, but much more functionality exists in Python libraries, and to activate this functionality, we must explicitly import it. In Python, the atan function is grouped together with many other mathematical functions in the library called math. Such a library is referred to as a module in correct Python language. To get access to atan in our program we have to write

from math import atan

Inserting this statement at the top of the program and rerunning it, leads to a new problem: pi is not defined. The variable pi, representing \( \pi \), is also available in the math module, but it has to be imported too:

from math import atan, pi

It is tedious if you need quite some math functions and variables in your program, e.g., also sin, cos, log, exp, and so on. A quick way of importing everything in math at once, is

from math import *

We will often use this import statement and then get access to all common mathematical functions. This latter statement is inserted in a program named ball_angle.py:

from math import *

x = 10             # Horizontal position
y = 10             # Vertical position

angle = atan(y/x)        

print (angle/pi)*180

This program runs perfectly and produces 45.0 as output, as it should.

At first, it may seem cumbersome to have code in libraries, since you have to know which library to import to get the desired functionality. Having everything available anytime would be convenient, but this also means that you fill up the memory of your program with a lot of information that you rather would use for computations on big data. Python has so many libraries with so much functionality that one simply needs to import what is needed in a specific program.