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
  • 72,336
  • 29
  • 179
  • 389
Victor
  • 1,289
  • 2
  • 9
  • 8
  • 6
    We don't have a strict policy for list questions, but there is a general dislike. Please note also this and this discussion; you might want to improve your question as to avoid the problems explained there. If you are not sure how to improve your question maybe we can help you in [chat]? – Raphael Apr 05 '16 at 12:14
  • 52
    You seem to make the common mistake of assuming that the content of every course has to be relevant (for every career path). Sometimes is just about training you how to think in certain ways. – Raphael Apr 05 '16 at 12:15
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. – Raphael Apr 08 '16 at 06:42
  • Many career paths involve a combination of disciplines. For example most quants use a mixture of CS, mathematics, statistics, economics and finance. This involves applications of stochastic calculus - which are taught on virtually all graduate Financial Engineering graduate degrees – FD_bfa May 13 '23 at 17:26

9 Answers9

124

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,349
  • 1
  • 9
  • 12
  • 38
    Perhaps you had a different experience, but I found calculus pretty useless for learning how to reason and explain arguments rigorously. It was taught by rote and pattern matching pretty much like high school algebra and geometry. On the other hand, it was the prerequisite to several higher math classes that did teach these skills, so I suppose it wasn't entirely useless. – tsleyson Apr 05 '16 at 06:17
  • 6
    I can totally relate to the very last point (indirect benefits). Working on programming languages theory, I rarely used calculus directly. Perhaps the most direct application was in probabilistic computational models (e.g. Plotkin&Jones probabilistic powerdomains). Yet, my calculus course was mostly about proving things, and this was very, very valuable. One or two calculus courses are IMHO needed in every serious CS program, along some more math (discrete math, logic, linear algebra, numerical analysis, ... and possibly categories, topology, algebra, ... ). – chi Apr 05 '16 at 10:51
  • 3
    Here's an example of how I needed calculus in computer graphics: Smooth interpolation functions will basically all be of the form f(0) = 0, f(1) = 1, f'(0) = f'(1) = 0, and you can add any other constraints you care about, for instance f'(0.5) = 1. A little while ago I used this to derive some different interpolation polynomials for smoothing images. – porglezomp Apr 06 '16 at 16:24
  • 3
    Robotics can probably be expanded to any kind of physics modelling (which I guess also covers CG, in terms of lighting, so let's call it kinetic physics modelling). This includes acceleration/velocity, bounces/springs/deformation, PID controllers, acoustics, gravitation... – metao Apr 07 '16 at 07:37
  • 1
    And of course computer vision, which is the other current fad. Probability is also pretty useful in the design of computer networks. Not strictly calculus, but some ideas of mathematical analysis which are often covered in a calculus course turn up in static analysis and abstract interpretation, which are used in automatic program verification and optimizing compilers. – Tobia Tesan Apr 08 '16 at 11:53
  • 1
    I think the last paragraph is the most important one. Getting the right mindset, reasoning skills and technical rigour is most important when talking to a entity that only understands technical/mathematical rigour. – Mindwin Remember Monica Apr 08 '16 at 14:17
  • 2
    I'll champion the indirectly point this way: better than any class they take before, Calculus teaches students they can't simply count the number of problems and estimate how much work is going to be involved. – candied_orange Apr 10 '16 at 21:33
30

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
  • 3
    Given the "list" nature of this question, every answer should attempt to give the full picture. Are you certain you want to claim that numerical methods are the only instance? – Raphael Apr 05 '16 at 12:16
  • Comments are not for extended discussion; this conversation has been moved to chat. – Raphael Apr 07 '16 at 08:46
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
    How are cos, sine, $\pi$ and $\mathrm{e}$ calculus? – David Richerby Apr 06 '16 at 04:34
  • 4
    Well, $\exp(x)$ can be defined to be the unique solution to $f(x)=f'(x)$ with $f(0)=1$, and similarly sin, cos can be defined as the unique pair of functions satisfying $f'(x)=g(x)$, $g'(x) = -f(x)$ with appropriate boundary conditions (I think $f(0)=0$ and $g(0)=1$ should work). – Chris Taylor Apr 07 '16 at 07:23
  • 2
    @DavidRicherby: Example: how do you implement those functions on, say, a microcontroller without an FPU? If you know some calculus, you immediately know a good answer: power series. – Nate Eldredge Apr 10 '16 at 05:55
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
  • 9
    Your first paragraph is completely wrong and bordering on conspiracy theory. There are plenty enough areas of computer science where calculus is useful (see the other answers for endless lists of them). Sure, it's possible to avoid all of those areas but it's very misleading to claim that dropping calculus will have zero impact beyond grades. – David Richerby Apr 05 '16 at 19:58
  • 4
    Depending on your degree program, you could complete a degree without ever using calculus, and I definitely think CS majors don't need as much of it as we get. But doing poorly in it will lock you out of some of the most interesting areas of computer science. There's plenty of time to be a web developer once you graduate; while in school, why not try to push yourself a little? – tsleyson Apr 06 '16 at 01:59
  • 3
    @tsleyson If you want to be a web developer, save the tution money and time needed to obtain a CS degree. – Raphael Apr 06 '16 at 08:37
  • @DavidRicherby There IS an unintended conspiracy in CS to push math. When the academic field of CS was emerging it was created by math majors who insisted math was fundamental to understanding computers. They pushed that view onto the next generation of educators and it continues to this day. This is how we end up with co-op students who think they have to rigorously prove their code works and can't write anything larger than a 1000 lines of code without descending into analysis paralysis. – Scott B Apr 06 '16 at 18:06
  • 8
    @ScottB You seem to be confusing computer science with programming. – David Richerby Apr 06 '16 at 18:34
  • 2
    @Raphael I was speaking against an attitude I saw while doing my degree that you could ignore the material unless not knowing it would "embarrass you in front of other coders" as the OP says. If you aspire to be a web developer, yeah, save your money, or spend it at a dev bootcamp. If you're doing a degree in CS, put in the effort to learn CS. I scrubbed out of academia and ended up a web developer, but I can at least say I actually learned some computer science in my degree. – tsleyson Apr 06 '16 at 19:02
  • @DavidRicherby I'm trying to push back against the attitude that CS - math = programming. I feel there's an unhealthy obsession with math in CS and it pushes away good programmers. – Scott B Apr 06 '16 at 19:24
  • @tsleyson I can't argue with you. I'm at the anti-math end of the spectrum in this discussion and you're rooted firmly in the middle-ground. Everything you've said is right. – Scott B Apr 06 '16 at 19:29
  • 3
    @ScottB Who's saying CS = math + programming? I myself have been advocating against this limited view for ages. But you have it backwards, too: mathematics are integral to CS, just like it is to physics. We need it, even if we don't want to practice it. (That said, this is not the place for this discussion. Please join us in [chat] if you want to continue.) – Raphael Apr 06 '16 at 19:49
  • 1
    @ScottB "I'm trying to push back against the attitude that CS - math = programming." That's exactly the attitude you're endorsing by saying that the math in CS drives away good programmers. It also seems to be a strawman, since I've not noticed anybody but you making that claim. – David Richerby Apr 06 '16 at 20:25
  • 2
    Let me recap. The OP questions the importance of calculus in comp sci, many people give the viewpoint that it is and I give the viewpoint that it's not. I believe discrete math, lin alg, stats and numerical analysis should be required for a comp sci degree, but calculus shouldn't. You can spend an entire career building operating systems, compilers and web browsers without ever blowing the dust off of Stewart. Calculus is useful but so are a lot of other non-required courses. It shouldn't be a CS requirement. – Scott B Apr 06 '16 at 23:56
  • 2
    Re CS != programming : programming is more like English lit and composition. It's art and poetry, it's leaning to use tools effectively. CS needs some programming in the same way that Journalism needs some touch-typing. It's not about programming. I assume that there are degrees for learning programming now, so learning CS instead if you really want engineering is a poor fit. – JDługosz Apr 07 '16 at 07:30
  • May I suggest just limiting your first claim in your answer? Seems like a better solution than just arguing about it in the comments. – djechlin Apr 10 '16 at 21:49
  • Never mind, I just don't agree with any of this answer... doing poorly in calculus tends to mean you didn't learn or become useful at complex manipulations of expressions or abstract proof and reasoning, both of which are critical to c.s. study as well as engineering/programming. – djechlin Apr 10 '16 at 21:50
  • What, in your opinion, is too much math in a comp sci degree? If you feel calculus is necessary then how much of it? Differential and integral right? What about multivariable and vector? Should Galois be part of comp sci? Should ring theory? Abstract reasoning ability will increase with each higher course, so why stop at calculus? – Scott B May 12 '16 at 04:49
  • Deepening my understanding of mathematics has greatly improved my understanding of both computer programming and computation in general. I am 100% for math-heavy CS degrees. I'm a student with a long way to go, for what it's worth. – Ryan Jun 10 '17 at 21:39
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.

Aristu
  • 1,483
  • 4
  • 13
  • 24
  • I don't see any differential calculus there; I see the word "derivative", but I don't see anything resembling traditional differential calculus. –  Apr 07 '16 at 18:16
  • 2
    It's called "formal derivative", and it is related to Calculus, in a way. You will also see this done with Generating Functions, some formulas related to discrete structures and other areas where you don't actually have a "smooth function". – Jay Apr 07 '16 at 23:58
  • @Jay : The important thing isn't the name. How does understanding calculus help with it? – Christian Apr 08 '16 at 11:31
  • 2
    It is explained in this Wikipedia page. The formal derivative is an operation on elements of analgebraic structure that contains polynomials, and it is formally "quite like" the usual rule for differentiating polynomials, however -- differently from what a student sees in Calculus -- the polynomials are not over reals; they may be polynomials over an arbitrary "ring" (another algebraic structure). And there are practical applications of the formal derivative -- I have seen at least one (Algebraic Cryptanalysis -- can't remember the details). – Jay Apr 08 '16 at 12:27
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
  • "Many of the uses of your CS study involve programming systems which monitor change, or in some cases, attempt to predict the future" -- I don't think this is representative of moust CS courses of studies. – Raphael Apr 10 '16 at 12:42