Skip to main content
Logo image

Section 5.4 Mathematical operations

As we’ve seen, PreFigure allows us to define some quantities, often using mathematical operations, and then reuse them. Here we summarize the set of mathematical operations that are possible. PreFigure is written in Python, and authors who are familiar with that language may be able to imagine what is possible.

Subsection 5.4.1 Math operations

PreFigure provides access to Python’s math module, which means that constants such as pi and e are available, as well as functions such as sin, cos, acos, atan, and atan2. By default, any trigonometric functions use radians, but the functions degrees and radians will convert back and forth.
Some operations from discrete math, such as comb(n,k) for \(n\choose k\text{,}\) are also available.
These functions are available without the math. prefix so that we could say, for instance, cos(pi/6). See the Python documentation
 1 
docs.python.org/3/library/math.html
for a full list of functions that are available.

Subsection 5.4.2 Vector operations

Vector operations may be defined on previously-defined lists or tuples. For instance, if we define u=(1,2) and v=(-2,4), we can later say u+v and -3*u. (Behind the scenes, any list or tuple that is defined is interpreted as a numpy array.)
Similarly, a \(2\times2\) matrix may be defined with
A = [[1,2],[2,1]]      
    
and then used to multiply a vector with A @ v.
Some additional operations include:
length
length(u) finds the length of the vector u.
normalize
normalize(u) gives a vector paralle to u and having unit length.
dot
dot(u, v) finds the dot product of u and v.
midpoint
midpoint(u, v) gives the midpoint of the line segment from u to v.
angle
angle(u) finds the angle, in radians, between the vector and the positive \(x\)-axis.

Subsection 5.4.3 List operations

It can be convenient to modify existing lists. For instance, to add an element a to an existing list, we could say append(list, a). This could be used, for instance, to implement Euler’s method, as shown in Figure 5.4.1. Of course, Subsection 3.8.5 provides an easier way to illustrate Euler’s method, but authors will undoubtedly find uses for the append function.
Figure 5.4.1. An implementation of Euler’s method
<diagram dimensions="(300,300)" margins="20">
  <coordinates bbox="(-1,-1,10,8)">
    <definition>f(t,y) = 0.1*y*(6-y)</definition>
    <definition>N=5</definition>
    <definition>h=10/N</definition>
    <definition>points = [[0,1]]</definition>
    <repeat parameter="k=1..N">
      <definition>t=points[-1][0]</definition>
      <definition>y=points[-1][1]</definition>
      <definition>points=append(points, [t+h, y+h*f(t,y)])</definition>
    </repeat>
    <grid-axes xlabel="t" ylabel="y"/>
    <slope-field function="f" stroke="orange"/>
    <polygon points="points" stroke="blue"/>

    <repeat parameter="k=0..N">
      <point p="points[k]"/>
    </repeat>
  </coordinates>
</diagram>
Listing 5.4.2. The PreFigure source for Figure 5.4.1.
If list=(a,b,c), then roll(list) will be the list (b,c,a). This can be useful for visiting each of the vertices in a polygon, for instance, as shown in Figure 5.4.3.
Figure 5.4.3. Adding angle markers to a polygon
<diagram dimensions="(300,300)" margins="5">
  <coordinates bbox="(0,0,10,10)">
    <definition>points = ((1,1),(2,6),(6,9),(9,5),(7,2))</definition>
    <grid-axes decorations="no"/>
    <repeat parameter="k=1..5">
      <angle-marker points="points"/>
      <definition>points=roll(points)</definition>
    </repeat>
    
    <polygon points="points" closed="yes" stroke="blue"/>
  </coordinates>
</diagram>
Listing 5.4.4. The PreFigure source for Figure 5.4.3.