Search

Search papers and lessons

Legendre Transform

Level 3 · 9 min read

The Legendre transform turns a convex function into a second function of its slope, built so the two describe the same curve from the other side.

On this page

  • Colour = topic mix
  • Optimization
  • Calculus
  • Linear algebra
  • Sign in to see your progress

TL;DR

A convex function can be described in two ways: by its value at each point, or by the lines that support it. The Legendre transform converts between these descriptions. For each slope pp, it records the largest gap between pxpx and f(x)f(x), and that record is a new function f∗(p)f^*(p). Applying the transform twice returns the original function when ff is convex and closed.

Where you'll see it: in convex optimization, where it produces dual problems and lower bounds; in classical mechanics, where it turns a Lagrangian into a Hamiltonian; and in statistical mechanics, where it links free energy to entropy.

Intuition

Describing a curve by its tangents

A convex function is a bowl. You can describe the bowl in the usual way, as a height at each horizontal position xx. But you can also describe it by the family of tangent lines that touch it. Each tangent line has a slope pp and a height where it crosses the vertical axis. If you know, for every slope, where the tangent with that slope sits, you know the whole bowl.

The Legendre transform takes that second description and turns it into a function. Feed in a slope pp, and it returns the negative of the intercept of the tangent with that slope. The bowl is recoverable from the same information, so no detail is lost.

flowchart LR
  F["f(x): height at each point"] -->|"tangent lines"| S["slope p and intercept"]
  S -->|"Legendre transform"| G["f*(p): one number per slope"]
  G -->|"transform again"| F

The picture for f(x)=x2/2f(x) = x^2/2 shows the idea. The curve is the bowl, the dashed line is the tangent at x=3x = 3, which has slope 33. The tangent crosses the vertical axis at −4.5-4.5, so the transform at slope 3 equals 4.54.5.

−1012345−50510xy
  • f(x) = x²/2
  • tangent at x = 3, slope 3
The tangent with slope 3 touches the bowl at x = 3 and crosses the vertical axis at -4.5. The Legendre transform at slope 3 is 4.5, the negative of that intercept.

Why it is useful

A minimization problem in one form can be hard, and the same problem seen through the transform can be easy. In optimization, the transform gives a lower bound on a problem that cannot be solved directly, and that bound can often be optimized. In mechanics, it changes which variables are used, from velocities to momenta, which makes some equations of motion simpler. The transform is the tool that switches between these views.

The math

Let f:R→Rf : \mathbb{R} \to \mathbb{R} be a Convex Function. Its Legendre transform (also called the convex conjugate) is

f∗(p)=sup⁡x∈R(p x−f(x)).f^*(p) = \sup_{x \in \mathbb{R}} \bigl( p\,x - f(x) \bigr).

Here pp is a slope, sup⁡\sup is the supremum, the least upper bound of the expression over all xx, and f∗f^* is a new function of pp. For each slope pp, the expression px−f(x)px - f(x) measures how far the line y=pxy = px lies above the graph of ff at xx. The transform takes the largest such gap.

Geometric meaning

The tangent to ff at a point x0x_0 with slope pp is the line y=f(x0)+p (x−x0)y = f(x_0) + p\,(x - x_0). Rearranged, this is y=px−(px0−f(x0))y = px - (px_0 - f(x_0)), so its intercept on the vertical axis is −(px0−f(x0))-(px_0 - f(x_0)). A line y=px−cy = px - c lies on or below the graph of ff exactly when c≥px−f(x)c \ge px - f(x) for every xx, that is, when c≥f∗(p)c \ge f^*(p). The best line of slope pp under the graph has c=f∗(p)c = f^*(p), so its intercept is −f∗(p)-f^*(p), and it is the tangent. The transform value is the negative of that intercept.

Derivative relation

If ff is differentiable and strictly convex, the supremum occurs where the derivative of px−f(x)px - f(x) is zero, which gives f′(x)=pf'(x) = p. Let x(p)x(p) be the point where f′(x)=pf'(x) = p. Then

f∗(p)=p x(p)−f(x(p)),df∗dp(p)=x(p).f^*(p) = p\,x(p) - f(x(p)), \qquad \frac{d f^*}{dp}(p) = x(p).

The derivative of the transform is the inverse of the derivative of ff. The slope of one function is the position of the other, which is why the two views carry the same information.

Recall that a Derivative is the slope of the curve at a point. The first-order condition above uses exactly that fact.

Involution

For a convex function that is also closed (its sublevel sets are closed), the transform is its own inverse:

f∗∗(x)=f(x).f^{**}(x) = f(x).

This is the Fenchel–Moreau theorem. Two representations, values and supporting lines, are therefore equivalent. Without convexity, the second transform gives the lower convex envelope of ff instead of ff itself.

Inequality

From the definition, for any xx and pp,

f(x)+f∗(p)≥p x.f(x) + f^*(p) \ge p\,x.

This is Fenchel's inequality. It holds because f∗(p)f^*(p) is at least px−f(x)px - f(x) by construction, rearranged. Equality holds exactly when p=f′(x)p = f'(x), which is the condition that the line touches the bowl at xx.

Worked examples

Example
Quadratic: the transform of x²/2 is p²/2

Take f(x)=x2/2f(x) = x^2/2. This function is convex.

  1. The expression to maximize is g(x)=px−x2/2g(x) = px - x^2/2.
  2. Differentiate: g′(x)=p−xg'(x) = p - x. Setting this to zero gives x=px = p.
  3. Substitute back: f∗(p)=p⋅p−p2/2=p2/2f^*(p) = p \cdot p - p^2/2 = p^2/2.
  4. Numerical check at p=3p = 3: g(x)=3x−x2/2g(x) = 3x - x^2/2 is largest at x=3x = 3, where g=9−4.5=4.5g = 9 - 4.5 = 4.5. The formula gives 9/2=4.59/2 = 4.5.
  5. Check the derivative relation: x(p)=px(p) = p, and ddp(p2/2)=p=x(p)\frac{d}{dp}(p^2/2) = p = x(p). It holds.
  6. Transform again: f∗∗(x)=sup⁡p(px−p2/2)f^{**}(x) = \sup_p (px - p^2/2) is attained at p=xp = x, giving x2−x2/2=x2/2=f(x)x^2 - x^2/2 = x^2/2 = f(x). The function is its own dual.

Interpretation: a quadratic bowl is self-dual. The slope-value description has the same shape as the original, so the transform does not change the curve's form.

Example
Exponential: the transform of eˣ is p ln p − p

Take f(x)=exf(x) = e^x, which is convex and differentiable.

  1. Maximize g(x)=px−exg(x) = px - e^x for p>0p > 0. Differentiate: g′(x)=p−exg'(x) = p - e^x.
  2. Set g′(x)=0g'(x) = 0: ex=pe^x = p, so x(p)=ln⁡px(p) = \ln p.
  3. Substitute: f∗(p)=pln⁡p−pf^*(p) = p \ln p - p.
  4. Numerical check at p=2p = 2: x=ln⁡2≈0.6931x = \ln 2 \approx 0.6931. Then g=2(0.6931)−2=−0.6137g = 2(0.6931) - 2 = -0.6137. The formula gives 2ln⁡2−2=−0.61372 \ln 2 - 2 = -0.6137.
  5. For p<0p < 0, px−expx - e^x grows without bound as xx goes to minus infinity, so f∗(p)=+∞f^*(p) = +\infty. For p=0p = 0, sup⁡(−ex)=0\sup(-e^x) = 0, approached as xx goes to minus infinity.
  6. The domain of f∗f^* is therefore p≥0p \ge 0. The transform of a function defined on all of the real line can have a restricted domain. Here exe^x is increasing everywhere, so no tangent has a negative slope, and no line of negative slope stays below the graph.

Interpretation: the transform records the slopes at which the exponential has tangent lines. Only nonnegative slopes occur, and the transform at each of them is pln⁡p−pp \ln p - p. This is the same expression that appears as negative entropy in information theory, and it is why exponential families and entropy are linked through conjugates.

Tradeoffs and failure modes

The transform is only as useful as the assumptions behind it. Convexity is the main one. Outside that setting, the transform still exists, but it loses information.

SituationWhat goes wrongBetter choice
ff is not convexf∗∗f^{**} is the convex envelope, not ff; the bowl description is incompleteUse the convex hull explicitly, or accept the bound
ff is not strictly convexf′f' is flat somewhere, so the slope does not pin down a single pointUse subdifferentials instead of derivatives
f′f' is not invertibleSolving f′(x)=pf'(x) = p for x(p)x(p) is hard or ambiguousWork numerically, or change variables
ff takes the value +∞+\infty outside a setThe conjugate picks up that domain restrictionTrack the domain of f∗f^* explicitly

The main practical use is duality. The Fenchel inequality gives a bound that holds for every xx and pp, and optimizing over pp produces the tightest such bound. That is the core of Lagrangian Duality. The bound is a lower bound only: the dual value can be strictly below the primal optimum when the problem is not convex, and the gap has to be checked separately.

History

The transform is named after Adrien-Marie Legendre, who studied transformations of this kind in the late eighteenth and early nineteenth centuries, in work connected to differential equations and mechanics. Its mechanical use grew through the nineteenth century, when the Lagrangian and Hamiltonian formulations of mechanics were developed and the transform was the bridge between them.

The version used in modern convex analysis was built by Werner Fenchel, who worked on the convex conjugate in the late 1940s, and by later authors including Jean-Jacques Moreau and R. Tyrrell Rockafellar, who extended it to general convex functions in the 1960s and 1970s. The result caught on in optimization because duality turns difficult problems into bounds that can be computed, and the same mathematics later appeared in statistical mechanics and machine learning, where the log-partition function and entropy are conjugate to each other.

Check yourself

QuestionRecall: what is the definition of the Legendre transform f∗(p)f^*(p) of a convex function ff?Show answer
f∗(p)=sup⁡x(px−f(x))f^*(p) = \sup_x (px - f(x)). For each slope pp, it takes the largest value of the line pxpx minus the function, over all xx. Geometrically it is the negative of the intercept of the tangent to ff with slope pp.
QuestionComputation: find the Legendre transform of f(x)=2x2f(x) = 2x^2 and check it at p=4p = 4.Show answer
Maximize px−2x2px - 2x^2. The derivative p−4xp - 4x vanishes at x=p/4x = p/4. Substitute: p⋅p/4−2⋅p2/16=p2/4−p2/8=p2/8p \cdot p/4 - 2 \cdot p^2/16 = p^2/4 - p^2/8 = p^2/8. At p=4p = 4, x=1x = 1 and 4−2=24 - 2 = 2, which matches 16/8=216/8 = 2.
QuestionWhy: why does applying the transform twice return the original function only for convex functions?Show answer
The transform sees only the supporting lines of a function, and a line can only support a convex set from outside. A nonconvex function has dips that no supporting line touches, so the second transform fills them in and returns the convex envelope, the largest convex function below it. For a convex, closed function there are no such dips, and the supporting lines recover the function exactly.
QuestionApplied: you want a lower bound on f(x)+g(x)f(x) + g(x) over all xx. Use the Fenchel inequality f(x)+f∗(p)≥pxf(x) + f^*(p) \ge px for ff and for gg to get one.Show answer
The inequality gives f(x)≥px−f∗(p)f(x) \ge px - f^*(p) and g(x)≥−px−g∗(−p)g(x) \ge -px - g^*(-p) for every xx and pp. Adding them, the pxpx terms cancel: f(x)+g(x)≥−f∗(p)−g∗(−p)f(x) + g(x) \ge -f^*(p) - g^*(-p). The right side does not depend on xx, so it bounds the minimum from below, and maximizing it over pp gives the tightest bound. This is Fenchel duality, and it gives a bound without solving the original problem.

Sources