Chapter 2. Type safety
Before we get started with the linear algebra and calculus, I want to talk quickly about types of objects. This is an important safeguard for the future in checking your work and auditing your understanding of a topic; a good instructor will point out, in your work, any time you make a type-error.
§2.1 [TEXT] Type errors
In mathematics, statements are usually either true or false. Examples of false statements7 include
However, it’s possible to write statements that are not merely false, but not even “grammatically correct”, such as the nonsense equations
To call these equations false is misleading. If your friend asked you whether , you would say “no”. But if your friend asked whether equals the identity matrix, the answer is a different kind of “no”; really, it’s “your question makes no sense”.
These three examples are type errors. This term comes from programming: most programming languages have different data types like integer, boolean, string, array, etc., and will usually8 prevent you from doing anything idiotic like adding a string to an array.
Objects in mathematics work in a really similar way. In the first weeks of 18.02, you will meet real numbers, vectors, and matrices; these are all different types of objects, and certain operations are defined (aka “allowed”) or undefined (aka “not allowed”) depending on the underlying types. Table Table 1 lists some common examples with vectors you’ve seen from precalculus.
| Operation | Notation | Input 1 | Input 2 | Output |
|---|---|---|---|---|
| Add/subtract | Scalar | Scalar | Scalar | |
| Add/subtract | -dim vector | -dim vector | -dim vector | |
| Add/subtract | matrix | matrix | matrix | |
| Multiply | or | Scalar | -dim vector | -dim vector |
| Multiply | or | Scalar | Scalar | Scalar |
| Multiply | or | matrix | matrix | matrix |
| Dot product | -dim vector | -dim vector | Scalar | |
| Cross product | -dim vector | -dim vector | -dim vector | |
| Length/mag. | Any vector | n/a | Scalar | |
| Determinant | Any square matrix | n/a | Scalar |
A common question at this point is how you are supposed to figure out whether a certain operation is allowed or not. For example, many students want to try and multiply two vectors together component-wise; why is
not a legal sentence? It seems like it would make sense.
The answer is that you don’t have to figure out — you are told; Table 1 isn’t something that you derive. That is, Table 1 consists of the definitions which you have been given.
(Or more sarcastically, it’s all just a social construct. Well, it’s bit more nuanced than that. Definitions aren’t judged by “correctness”; that doesn’t make sense; you are allowed to make up whatever definitions you want. Instead, definitions are judged by whether they are useful. Which is obviously subjective, but it’s less subjective than you might guess.)
§2.2 [TEXT] Why you should care
There are two action items to take away from this chapter.
§2.2.1 When learning a new object, examine its types first
What this means is that, every time you encounter a new kind of mathematical object or operation (e.g. partial derivative), the first thing you should do is ask what types are at play. This helps give you a sanity check on your understanding of the new concept.
We’ll use boxes like this throughout the textbook to do this:
§2.2.2 Whenever writing an equation, make sure the types check out
Practically, what’s really useful is that if you have a good handle on types, then it gives you a way to type-check your work. This is the analog of dimensional analysis from physics, where you know you messed up if some equation has on the left but on the right.
For example, if you are reading your work and you see something like
(1)
then you can immediately tell that there’s a mistake, because the two sides are incompatible — the left-hand side is a real number (scalar), but the right-hand side is a vector.
§2.3 [RECAP] Takeaways from type safety
- Throughout this book, every time you meet a new operation, make sure you know what types of objects it takes as input and which it takes as output.
- Whenever you write an equation, make sure it passes a type-check. You can catch a lot of errors like Equation 1 using type safety alone.
§2.4 [EXER] Exercises
Exercise 2.1. Let , , be vectors in . By using Table 1 (or skimming Section 4.1 briefly), determine whether each of the following expressions is a real number, a vector, or nonsense (type-error); there should be one of each.
- (here order of operations is before )
(The symbol confusingly can refer to three different things: grade-school multiplication, scalar multiplication, or the dot product.)
(The answer to this exercise is written in Chapter 42, and in general Part Lima contains solutions to all the exercises.)