[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"Jb3QNeTLa5":3},"(This is 100% vibe coded by AI). I did it to show the PVS dev the current sota.\n\n# LeanDidax\n\nA pedagogical implementation of automatic differentiation in Lean 4, inspired by JAX's Autodidax tutorial.\n\nFor a comprehensive overview of the project and its components, see the [Summary Document](SUMMARY.md).\n\n## Overview\n\nLeanDidax is an automatic differentiation library written in Lean 4. It provides both forward-mode and reverse-mode autodiff capabilities, allowing users to compute derivatives of complex mathematical functions efficiently.\n\nThe library is inspired by the [JAX Autodidax tutorial](https://docs.jax.dev/en/latest/autodidax.html), which explains how to build a simple automatic differentiation system from scratch.\n\n## Features\n\n- **Forward-mode autodiff**: Efficient computation of derivatives using dual numbers\n- **Reverse-mode autodiff**: Efficient computation of gradients using computational graphs\n- **Operator overloading**: Use standard mathematical notation with autodiff types\n- **Vectorized operations**: Support for batched operations and the `vmap` transformation\n- **Control flow primitives**: Differentiable conditionals with `cond`, `select`, and `switch`\n- **Custom derivative rules**: Define specialized derivatives for functions\n- **Rich function library**: Support for common mathematical functions:\n  - Basic operations: +, -, *, /\n  - Trigonometric functions: sin, cos, tan\n  - Hyperbolic functions: sinh, cosh, tanh\n  - Exponential and logarithmic functions: exp, log\n  - Non-differentiable functions with special handling: abs, relu\n  - Activation functions for neural networks: sigmoid\n- **Higher-order differentiation functions**: grad, valueAndGrad, jvp\n\n## Installation\n\nAdd LeanDidax to your Lean project by adding it as a dependency in your `lakefile.lean`:\n\n```lean\nrequire LeanDidax from git \"https://github.com/yourusername/LeanDidax\" @ \"main\"\n```\n\n## Usage\n\n### Basic Example: Forward-Mode Autodiff\n\n```lean\nimport LeanDidax2.Basic\n\nopen LeanDidax2\n\n-- Define a function to differentiate\ndef f (x : Value Float) : Value Float :=\n  x * x + 2 * x + 1\n\n-- Compute the value and derivative at x=3\n#eval \n  let x := seed 3.0  -- Create a variable with tangent=1.0\n  let result := f x\n  (s!\"Function value at x=3: {result.primal}\",\n   s!\"Derivative at x=3: {result.tangent}\")\n```\n\n### Reverse-Mode Autodiff Example\n\n```lean\nimport LeanDidax2.Autodiff\n\nopen LeanDidax2\nopen LeanDidax2.ReverseMode\n\n-- Define a function using a computational graph\ndef polynomial (x : Node) : Node :=\n  let x2 := Node.Mul x x            -- x^2\n  let two := Node.Leaf 2.0\n  let twoX := Node.Mul two x        -- 2*x\n  let sum1 := Node.Add x2 twoX      -- x^2 + 2*x\n  let one := Node.Leaf 1.0\n  Node.Add sum1 one                 -- x^2 + 2*x + 1\n\n-- Compute the gradient at x=3\n#eval\n  let x := 3.0\n  let xNode := Node.Leaf x\n  let polyGraph := polynomial xNode\n  let gradResults := backward polyGraph 1.0\n  let grad := match gradResults.find? (fun pair => pair.1 == x) with\n              | some (_, cotangent) => cotangent\n              | none => 0.0\n  (s!\"Gradient of f(x) = x^2 + 2x + 1 at x=3: {grad}\")\n```\n\n### Vectorized Operations with vmap\n\n```lean\nimport LeanDidax2.Batch\n\nopen LeanDidax2\nopen LeanDidax2.Batch\n\n-- Define a function\ndef f (x : Value Float) : Value Float := \n  x * x + 2 * x + 1\n\n-- Apply it to multiple inputs at once\n#eval\n  let inputs := #[1.0, 2.0, 3.0, 4.0, 5.0]\n  let results := vmap f inputs\n  for i in [:inputs.size] do\n    IO.println s!\"{inputs[i]!}: value = {results[i]!.primal}, derivative = {results[i]!.tangent}\"\n```\n\n### Custom Derivative Rules\n\n```lean\nimport LeanDidax2.CustomRules\n\nopen LeanDidax2\nopen LeanDidax2.CustomRules\n\n-- Define a function with a custom derivative rule\ndef f (x : Float) : Float := x * x * x * Float.sin x\ndef df (x : Float) : Float := 3 * x * x * Float.sin x + x * x * x * Float.cos x\n\n-- Use the custom rule\n#eval\n  let x := 2.0\n  let result := defCustomFn \"cubic_sine\" f df (seed x)\n  (s!\"Value: {result.primal}\", s!\"Derivative: {result.tangent}\")\n```\n\n### Differentiable Control Flow\n\n```lean\nimport LeanDidax2.ControlFlow\n\nopen LeanDidax2\nopen LeanDidax2.ControlFlow\n\n-- Use differentiable conditionals\n#eval\n  let x := seed 2.0\n  let result := cond (x.primal > 0.0)\n    (fun _ => x * x)         -- x^2 when x > 0\n    (fun _ => x * -1.0)      -- -x when x ≤ 0\n  (s!\"Value: {result.primal}\", s!\"Derivative: {result.tangent}\")\n```\n\n## Implementation Details\n\n### Value Type\n\nThe core of LeanDidax is the `Value` type, which tracks both the primal value and its tangent (derivative) information:\n\n```lean\nstructure Value (α : Type) where\n  primal : α\n  tangent : α := primal\n```\n\n### Forward-Mode Autodiff\n\nForward-mode autodiff works by propagating tangent values alongside primal values through each operation. For example, the multiplication rule:\n\n```lean\ndef mul [Mul α] [Add α] (x y : Value α) : Value α :=\n  { primal := x.primal * y.primal,\n    tangent := x.tangent * y.primal + x.primal * y.tangent }\n```\n\n### Reverse-Mode Autodiff\n\nReverse-mode autodiff builds a computational graph and uses backward propagation to compute gradients:\n\n```lean\ninductive Node\n  | Leaf (value : Float)\n  | Add (left right : Node)\n  | Mul (left right : Node)\n  | ...\n```\n\n### Control Flow Primitives\n\nThe library supports differentiable control flow:\n\n```lean\ndef cond {α : Type} [Zero α] \n  (pred : Bool) \n  (trueBranch : Unit → Value α) \n  (falseBranch : Unit → Value α) : Value α :=\n  if pred then trueBranch () else falseBranch ()\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n",1781732221638]