Skip to content

Repository files navigation

clarke-ad

Automatic differentiation for nonsmooth functions — abs, max, min, floor done right.

License: MIT Rust WASM

clarke.jesed.dev · GitHub


Standard AD gives garbage at kinks. Clarke-AD uses Clarke generalized gradients — convex hulls of subgradients — to handle abs/max/min/floor/ceil/signum honestly.

  standard AD:  d/dx |x| at x=0 → 0 (wrong!)
  clarke-ad:    ∂|x| at x=0     → [-1, 1] (correct subgradient set)

Install

curl -sSf https://clarke.jesed.dev/install.sh | sh

Demo

Try it live → clarke.jesed.dev

How it works

For a Lipschitz function f, the Clarke subgradient at x₀ is:

∂f(x₀) = conv{lim ∇f(xᵢ) : xᵢ → x₀, xᵢ ∉ Ωf}

At smooth points, this is just the derivative. At kinks (abs, max, floor), it's a convex set — the subgradient interval.

Use cases

Domain Application
Robotics Contact/impact mechanics (sign function)
Control Saturation, thresholds, argmin compositions
Optimization Nonsmooth objectives, subgradient methods
Simulation Piecewise-constant systems, switched dynamics
Machine learning ReLU networks, hinge loss, quantization

Architecture

clarke-ad/
├── crates/
│   ├── clarke-core/       # dual numbers, forward AD
│   ├── clarke-nonsmooth/  # Clarke subgradients, kink detection
│   ├── clarke-opt/        # subgradient, proximal methods
│   └── clarke-wasm/       # browser bridge
├── docs/math.md           # the theory
└── scripts/               # regression scripts

Build

cargo test                                    # unit tests
cargo build --release                         # optimized binary
cd ui && npm install && npm run dev           # local dev server

Tech stack

  • Rust — core AD engine with dual numbers
  • Clarke subgradients — convex hull of subgradients at kinks
  • wasm-bindgen — browser bridge
  • Vite — frontend build

References

  • Clarke, "Optimization and Nonsmooth Analysis" (1983).
  • Rockafellar, "Convex Analysis" (1970).
  • Shor, "Minimization Methods for Non-Differentiable Functions" (1985).

License

MIT — see LICENSE.

About

Automatic differentiation for nonsmooth functions — abs, max, min, floor done right with Clarke subgradients

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages