111

Many computer science programs require two or three calculus classes.

I'm wondering, how and when is calculus used in computer science? The CS content of a degree in computer science tends to focus on algorithms, operating systems, data structures, artificial intelligence, software engineering, etc. Are there times when Calculus is useful in these or other areas of Computer Science?

Raphael
  • 73,212
  • 30
  • 182
  • 400
Victor
  • 1,289
  • 2
  • 9
  • 8

9 Answers9

125

I can think of a few courses that would need Calculus, directly. I have used bold face for the usually obligatory disciplines for a Computer Science degree, and italics for the usually optional ones.

  • Computer Graphics/Image Processing, and here you will also need Analytic Geometry and Linear Algebra, heavily! If you go down this path, you may also want to study some Differential Geometry (which has multivariate Calculus as a minimum prerequisite). But you'll need Calculus here even for very basic things: try searching for "Fourier Transform" or "Wavelets", for example -- these are two very fundamental tools for people working with images.
  • Optimization, non-linear mostly, where multivariate Calculus is the fundamental language used to develop everything. But even linear optimization benefits from Calculus (the derivative of the objective function is absolutely important)
  • Probability/Statistics. These cannot be seriously studied without multivariate Calculus.
  • Machine Learning, which makes heavy use of Statistics (and consequently, multivariate Calculus)
  • Data Science and related subjects, which also use lots of Statistics;
  • Robotics, where you will need to model physical movements of a robot, so you will need to know partial derivatives and gradients.
  • Discrete Math and Combinatorics (yes!, you may need Calculus for discrete counting!) -- if you get serious enough about generating functions, you'll need to know how to integrate and derivate certain formulas. And that is useful for Analysis of Algorithms (see the book by Sedgewick and Flajolet, "Analysis of Algorithms"). Similarly, Taylor Series and calculus can be useful in solving certain kinds of recurrence relations, which are used in algorithm analysis.
  • Analysis of Algorithms, where you use the notion of limit right from the start (see Landau notation, "little $o$" -- it's defined using a limit)

There may be others -- this is just off the top of my head.

And, besides that, one benefits indirectly from a Calculus course by learning how to reason and explain arguments with technical rigor. This is more valuable than students usually think.

Finally -- you will need Calculus in order to, well, interact with people from other Exact Sciences and Engineering. And it's not uncommon that a Computer Scientist needs to not only talk but also work together with a Physicist or an Engineer.

Jay
  • 1,359
  • 1
  • 9
  • 12
31

This is somewhat obscure, but calculus turns up in algebraic data types. For any given type, the type of its one-hole contexts is the derivative of that type. See this excellent talk for an overview of the whole subject. This is very technical terminology, so let's explain.

Algebraic Data Types

You may have come across tuples being referred to as product types (if not, it's because they are the cartesian product of two types). We're going to take this literally and use the notation:

$$a * b$$

To represent a tuple, where $a$ and $b$ are both types. Next, you may have come across sum types these are types which can be either one type, or another (known as unions, variants, or as the Either type (kinda) in Haskell). We're also going to take this one literally and use the notation:

$$a + b$$

These are named as they are because if a type $a$ has $N_a$ values and a type $b$ has $N_b$ values, then the type $a + b$ has $N_a + N_b$ values.

These types look like normal algebraic expressions and we can, in fact, manipulate them as such (to a point).

An Example

In functional languages a common definition of a list (given in Haskell here) is this:

data List a = Empty 
            | Cons a List

This says that a list is either empty or a tuple of a value and another list. Transforming that to algebraic notation, we get:

$$L(a) = 1 + a * L(a)$$

Where $1$ represents a type with one value (aka the unit type). By repeatedly inserting, we can evaluate this to get a definition for $L(a)$:

$$L(a) = 1 + a * L(a)$$ $$L(a) = 1 + a * (1 + a * L(a))$$ $$L(a) = 1 + a + a^2 * (1 + a * L(a))$$ $$L(a) = 1 + a + a^2 + a^3 * (1 + a * L(a))$$ $$L(a) = 1 + a + a^2 + a^3 + a^4 + a^5...$$

(Where $x^n$ is meant in the sense of repeated multiplication.)

This definition says then that a list is either unit, or a tuple of one item, or a tuple of two items, or of three etc, which is the definition of a list!

One-hole Contexts

Now on to one-hole contexts: a one-hole context is what you get when you 'take a value out' of a product type. Let's give an example:

For a simple 2-tuple which is homogeneous, $a^2$, if we take a value out, we just get a 1-tuple, $a$. But there are two different one-hole contexts of this type: namely the first and second values of the tuple. So since it is either of these we could write that it is $a + a$, which is, of course, $2a$. This is where the differentiation comes in to play. Let's confirm this with another example:

Taking a value out of a 3-tuple gives a 2-tuple, but there three different variants:

$$(a, a, \_)$$ $$(a, \_, a)$$ $$(\_, a, a)$$

Depending on where we put the hole. This gives us $3a^2$ which is indeed the derivative of $a^3$. There is a proof of this in general here.

For our final example, let's use a list:

If we take our original expression for a list:

$$L(a) = 1 + a * L(a)$$

We can rearrange to get:

$$L(a) = \frac{1}{1 - a}$$

(On the surface this may seem like nonsense, but if you take the taylor series of this result you get the definition we derived earlier.)

Now if we differentiate this, we get an interesting result:

$$\frac{\partial L(a)}{\partial a} = (L(a))^2$$

Thus one list has become a pair of lists. This in fact makes sense: the two lists produced correspond to the elements above and below the hole in the original list!

13

Numerical Methods. There exist cumbersome calculus problems that are unique to specific applications, and they need solutions faster than a human can practically solve without a program. Someone has to design an algorithm that will compute the solution. Isn't that the only thing that separates programmers from scientists?

Jonah Havel
  • 139
  • 4
13

Automation - Similar to robotics, automation can require quantifying a lot of human behavior.

Calculations - Finding solutions to proofs often requires calculus.

Visualizations - Utilizing advanced algorithms requires calculus such as cos, sine, pi, and e. Especially when you're calculating vectors, collision fields, and meshing.

Logistics and Risk analysis - Determining whether a task is possible, the risk involved, and possible rate of success.

Security - Most security can be performed without calculus; however, many people who want explanations prefer it in mathematical expressions.

AI - The basics of AI can be utilized without calculus; however, calculating advanced behavior, swarm intelligence/hive minds, and complex values based decision making.

Medical calculations - Visualizing most health data requires calculus such as an EKG reading.

Science & Engineering - When working with nearly any other scientific discipline requires calculus: Aerospace, Astrology, Biology, Chemistry, or Engineering.

Many people in programming can go their entire career without using calculus; however, it can prove invaluable if you're willing to do the work. For me it has been most effective in automation, logistics, and visualization. By identifying specific patterns, you can simply ignore the pattern, imitate the pattern, or develop a superior method all together.

LJones
  • 139
  • 4
7

The fact is that there's very little chance you'll ever use calculus. However, virtually every other scientific discipline DOES use calculus and you are working on a science degree. There are certain expectations of what a university science degree is supposed to mean and one of those things is that you know calculus. Even if you'll never use it.

It's okay if you do poorly in calculus, but make sure you put some effort into discrete mathematics. There are a lot of real-world programming problems where discrete math comes into play and ignorance of its principles can embarrass you in front of other coders.

Scott B
  • 97
  • 3
5

Many people already provided applications in CS. But sometimes you'll find Calculus when you least expect:

Regular-expression derivatives reexamined

If you know automata this pdf might be worth reading.

Schonfinkel
  • 1,493
  • 4
  • 13
  • 25
4

Some more specific examples:

  • Calculus is used to derive the delta rule, which is what allows some types of neural networks to 'learn'.
  • Calculus can be used to compute the Fourier transform of an oscillating function, very important in signal analysis.
  • Calculus is used all the time in computer graphics, which is a very active field as people continually discover new techniques. For a fundamental example check out Kajiya's rendering equation
  • Calculus is important in the field of computational geometry, investigate curve and surface modelling.
bfair
  • 143
  • 6
3

To these other excellent answers I add this point: rigor in testing.

In creating test cases for some applications I have had to make use of calculus to predict expected running times, memory sizes, and choose optimal parameters when tuning data structures. This includes understanding expected rounding error, etc.

While statistics is mentioned in other answers, I would like to specifically mention Monte-carlo algorithms, such as optimization algorithms and some frugal streaming algorithms that are based on Mathematical principles that include calculus.

Specific industries where I have worked where calculus was required include:

  • Finance (creating a trading platform)

  • Insurance (numerical integration of insurance policies in what-if scenarios to compute expected policy losses)

  • Logistics (optimizing the consolidation of transportation routes)

  • Signal processing

Paul Chernoch
  • 261
  • 1
  • 7
3

Calculus -- the integral portion -- is used directly in CS as a foundation for thinking about summation. If you work through any portion of Knuth's Concrete Mathematics section on summation, you will quickly recognize conventions common to calculus: understanding some of the continuous case gives you tools to consider the discrete.

Many of the uses of your CS study involve programming systems which monitor change, or in some cases, attempt to predict the future. The mathematics around those systems is rooted in differential equations and linear algebra, and differential equations are...calculus. There are teachers like Gibert Strang who advocate for moving more quickly into the differential equations part, but it is still a subset of calculus. When change depends on change in any system, it starts to be unstable (and stable) in ways which are both non-intuitive and very well understood. To understand why your sensible linear system is behaving in nonlinear ways, you either need the tools of calculus or you need to re-invent them for your problem space.

And finally, CS often requires reading and understanding the work of others, and calculus is the first exposure to a lot of shared vocabulary, convention, and history.

Jess
  • 31
  • 1