Mathematical Introduction


Mathematical machines

Computers are mathematical machines. Their basic operations involve elementary logic and arithmetic very similar to what you learned as a child.

Programming languages tend to expand on the mathematical aspects of computers, making programs take on a look that is very similar to the mathematics that you study in a course on elementary algebra. It is important to enbrace the mathematical nature of algorithms and computation. That is how you will achieve simplicity and elegance.

Our approach is to take advantage of your mathematical training to expose some key ideas of algorithms and programming in a familiar and simple setting. This chapter begins by looking at mathematical expressions. In this chapter, we are not using a programming language. The notation is typical mathematical notation. The next chapter introduces programming language notation.


Expressions, precedence and associativity

An expression produces a value. For example, expression 3 + 4 has value 7 and expression 5(10+2) has value 60. You evaluate an expression to get its value.

How expressions are evaluated is determined by operator precedence and associativity. Precedence tells you that you do multiplications before additions. For example, to compute the value of 2 + 3x, you do the multiplication 3x first, then add 2. Associativity tells you that additions and subtractions are done from left to right. For example, 5 - 2 - 2 = 1, since you do the first subtraction first. Parentheses are used to override the precedence and associativity rules. For example, expression 10 - (4 - 1) has value 7.

To compute the value of an expression, work your way from smaller expressions to larger ones. You can show an evaluation step by step by replacing one subexpression by its value, then do another subexpression, and so on until you have the answer. At each step, choose any subexpression that involves just one operator applied to two known values. In the following example, the part that is about to be computed is underlined and shown in red.

  3(5 + 4(2 + 9)) 
    = 3(5 + 4(11))
    = 3(5 + 44)
    = 3(49)
    = 147


Variables and substitution

When an expression contains variables such as x and y, you can only tell what its value is if someone tells you values for the variables. Once you know a variable's value, you can substitute that value for the variable. For example, substituting 40 for x in expression 2(x + 9) yields expression 2(40 + 9) and substituting 8 for y in expression y + 1 yields expression 8 + 1. Notice that substitution is not the same as evaluation. After doing a substitution that yields expression 8 + 1, you can continue and do an evaluation, yielding 9. We prefer to keep the two steps separate.

Sometimes, when you do a substitution, you need to add parentheses to ensure that the expression is read correctly. For example, if you substitute 35 for z in expression 2z, you get 2(35), not 235. You can always add parentheses if you want to. But you typically leave them off when they are not needed.

Substitution probably seems like a very simple concept, and it is. But it is also a surprisingly important one for computing. We will see examples of substitutions in several places while studying fundamental concepts of computation and programming.

One important rule about expressions tells you something about what kinds of expressions you are allowed to write. The rule is:

You can substitute any expression for a variable. What you get is always an allowed expression. You will often need to add parentheses to preserve the expression's structure, so substitute a parenthesized expression.
For example, you know that (3y + 1) is an expression, and 2x + 8 is another one. You can replace x by expression (3y + 1), yielding another expression 2(3y + 1) + 8. You can keep replacing variables by other expressions, building up larger and larger expressions.


Functions

You have seen function notation such as f(x), meaning "the value produced by function f when run on argument x." We will use that notation, allowing a variety of different functions.

Since we will want to be able to type expressions on a keyboard, notations such as the radical notation (the funny sign that means square root) are awkward. So we use notation sqrt(x) to indicate the square root of x. For example, sqrt(49) = 7.


Problems

  1. [solve] Show a step-by-step evaluation of expression (3)(5) + 9.

  2. [solve] Show a step-by-step evaluation of expression (2 + 5)(8 - 2 - (3 + 1)).

  3. [solve] What expression do you get if you substitute 500 for x in expression (x - 9)(40)? Just do the substitution. Do not also evaluate.

  4. [solve] What expression do you get if you substitute 500 for x in expression 3x - 2? Just do the substitution. Do not also evaluate.

  5. [solve] What expression do you get if you substitute (y + 1) for x in expression 3x + 2? Just do the substitution. Do not also evaluate.

  6. [solve] What expression do you get if you substitute (7(y + 2) - 5) for x in expression 3x2 + 2x? Just do the substitution. Do not also evaluate.


Summary

An expression is something that you can evaluate to get a value, by performing operations such as operators (addition, subtraction, multiplication, etc.) and functions.

Follow precedence and associativity rules. Parentheses force you to do the operations in a particular order.

Substitution of a value or another expression for a variable is an important concept in computation.

In general, substituting an expression for a variable in another expression gives you a larger expression.


Review

An algorithm is a clear and precise set of instructions telling how to solve a computational problem.

Working out algorithms requires an eye for detail. You can develop an eye for detail with practice.

Our focus is on finding simple and elegant algorithms.