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

𝜋=165or2+2=5.

However, it’s possible to write statements that are not merely false, but not even “grammatically correct”, such as the nonsense equations

𝜋=(1001),(012345)=cos(67),det(511)≠2.

To call these equations false is misleading. If your friend asked you whether 2+2=5, you would say “no”. But if your friend asked whether 𝜋 equals the 2×2 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.

OperationNotationInput 1Input 2Output
Add/subtract𝑎±𝑏ScalarScalarScalar
Add/subtract𝐯±𝐰𝑑-dim vector𝑑-dim vector𝑑-dim vector
Add/subtract𝑀±𝑁𝑚×𝑛 matrix𝑚×𝑛 matrix𝑚×𝑛 matrix
Multiply𝑐𝐯 or 𝑐⋅𝐯Scalar𝑑-dim vector𝑑-dim vector
Multiply𝑎𝑏 or 𝑎⋅𝑏ScalarScalarScalar
Multiply𝑀𝑁 or 𝑀⋅𝑁𝑚×𝑛 matrix𝑛×𝑝 matrix𝑚×𝑝 matrix
Dot product𝐯⋅𝐰𝑑-dim vector𝑑-dim vectorScalar
Cross product𝐯×𝐰3-dim vector3-dim vector3-dim vector
Length/mag.|𝐯|Any vectorn/aScalar
Determinantdet𝐴Any square matrixn/aScalar
Table 1: Common linear algebra operations. For 18.02, “scalar” and “real number” are synonyms.
Digression

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

(23)(45)=?(815)

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:

Type signature
This is an example of a type signature box. When we want to make comments about the types of new objects, we’ll put them in boxes like 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 kg⋅meters⋅seconds−2 on the left but kg⋅meters⋅seconds−1 on the right.

For example, if you are reading your work and you see something like

|𝐯×𝐩|=9𝐩

(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

§2.4 [EXER] Exercises

Exercise 2.1. Let 𝐮, 𝐯, 𝐰 be vectors in ℝ3. 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.

(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.)

  1. 7Indiana Pi bill and 1984, respectively.
  2. 8JavaScript is a notable exception. In JavaScript, you may know that [] and {} are an empty array and an empty object, respectively. Then []+[] is the empty string, []+{} is the string ‘[object Object]’, {}+[] is 0, and {}+{} is NaN (not a number).