SkillAgentSearch skills...

Kotlingrad

🧩 Shape-Safe Symbolic Differentiation with Algebraic Data Types

Install / Use

/learn @breandan/Kotlingrad

README

<!--- @file:Suppress("ClassName") ---> <!--- @file:Suppress("PropertyName") --->

Kotlinβˆ‡: Type-safe Symbolic Differentiation for the JVM

Kotlin 1.6.20 Maven Central CI DOI

Kotlinβˆ‡ is a type-safe automatic differentiation framework written in Kotlin. It allows users to express differentiable programs with higher-dimensional data structures and operators. We attempt to restrict syntactically valid constructions to those which are algebraically valid and can be checked at compile-time. By enforcing these constraints in the type system, it eliminates certain classes of runtime errors that may occur during the execution of a differentiable program. Due to type-inference, most type declarations may be safely omitted by the end-user. Kotlinβˆ‡ strives to be expressive, safe, and notationally similar to mathematics.

Table of contents

Introduction

Inspired by Stalinβˆ‡, Autograd, DiffSharp, Myia, Nexus, Tangent, Lantern et al., Kotlinβˆ‡ attempts to port recent advancements in automatic differentiation (AD) to the Kotlin language. AD is useful for gradient descent and has a variety of applications in numerical optimization and machine learning. Our implementation adds a number of experimental ideas, including compile-time shape-safety, algebraic simplification and numerical stability checking with property-based testing. We aim to provide an algebraically-grounded implementation of AD for shape-safe tensor operations. Tensors in Kotlinβˆ‡ are represented as multidimensional arrays.

Features

Kotlinβˆ‡ currently supports the following features:

  • Arithmetical operations on scalars, vectors and matrices
  • Shape-safe vector and matrix algebra
  • Partial and higher-order differentiation on scalars
  • Property-based testing for numerical gradient checking
  • Recovery of symbolic derivatives from AD

Additionally, it aims to support:

All of these features are implemented without access to bytecode or special compiler tricks - just using higher-order functions and lambdas as shown in Lambda the Ultimate Backpropogator, embedded DSLs a la Lightweight Modular Staging, and ordinary generics. Please see below for a more detailed feature comparison.

Usage

Installation

Kotlinβˆ‡ is hosted on Maven Central. An example project is provided here.

Gradle

dependencies {
  implementation("ai.hypergraph:kotlingrad:0.4.7")
}

Maven

<dependency>
  <groupId>ai.hypergraph</groupId>
  <artifactId>kotlingrad</artifactId>
  <version>0.4.7</version>
</dependency>

Jupyter Notebook

To access Kotlinβˆ‡'s notebook support, use the following line magic:

@file:DependsOn("ai.hypergraph:kotlingrad:0.4.7")

For more information, explore the tutorial.

Notation

Kotlinβˆ‡ operators are higher-order functions, which take at most two inputs and return a single output, all of which are functions with the same numerical type, and whose shape is denoted using superscript in the rightmost column below.

| Math | Infix <sup>†</sup> | Prefix | Postfix<sup>‑</sup> | Operator Type Signature | |:------------------------------------------------------------------:|:-------------------------------:|:--------------------------------:|:-----------------------------------:|:-------------------------------------------------------------------------------:| | $$\mathbf{A}(\mathbf{B})$$<br>$$\mathbf{A}\circ\mathbf{B}$$ | a(b)<br>a of b | | | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{Ο€}, \texttt{b}: ℝ^{Ξ»} β†’ ℝ^{Ο„}) β†’ (ℝ^{Ξ»}→ℝ^{Ο€})$$ | | $$\mathbf{A}\pm\mathbf{B}$$ | a + b<br>a - b | plus(a, b)<br>minus(a, b) | | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{Ο€}, \texttt{b}: ℝ^{Ξ»} β†’ ℝ^{Ο€}) β†’ (ℝ^{?}→ℝ^{Ο€})$$ | | $$\mathbf{A}\mathbf{B}$$ | a * b<br>a.times(b) | times(a, b) | | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{mΓ—n}, \texttt{b}: ℝ^{Ξ»}→ℝ^{nΓ—p}) β†’ (ℝ^{?}→ℝ^{mΓ—p})$$ | | $$\frac{\mathbf{A}}{\mathbf{B}}$$<br>$$\mathbf{A}\mathbf{B}^{-1}$$ | a / b<br>a.div(b) | div(a, b) | | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{mΓ—n}, \texttt{b}: ℝ^{Ξ»}→ℝ^{pΓ—n}) β†’ (ℝ^{?}→ℝ^{mΓ—p})$$ | | $$\pm\mathbf{A}$$ | | -a<br>+a | a.neg()<br>a.pos() | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{Ο€}) β†’ (ℝ^{Ο„}→ℝ^{Ο€})$$ | | $$\sin{a}$$<br>$$\cos{a}$$<br>$$\tan{a}$$ | | sin(a)<br>cos(a)<br>tan(a) | a.sin()<br>a.cos()<br>a.tan() | $$(\texttt{a}: ℝ→ℝ) β†’ (ℝ→ℝ)$$ | | $$\ln{a}$$ | | ln(a)<br>log(a) | a.ln()<br>a.log() | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{mΓ—m}) β†’ (ℝ^{Ο„}→ℝ^{mΓ—m})$$ | | $$\log_{b}a$$ | a.log(b) | log(a, b) | | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{mΓ—m}, \texttt{b}: ℝ^{Ξ»}→ℝ^{mΓ—m}) β†’ (ℝ^{?}→ℝ)$$ | | $$\mathbf{A}^b$$ | a.pow(b) | pow(a, b) | | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{mΓ—m}, \texttt{b}: ℝ^{Ξ»}→ℝ) β†’ (ℝ^{?}→ℝ^{mΓ—m})$$ | | $$\sqrt{A}$$<br>$$\sqrt[3]{A}$$ | a.pow(1.0/2)<br>a.root(3) | sqrt(a)<br>cbrt(a) | a.sqrt()<br>a.cbrt() | $$(\texttt{a}: ℝ^{Ο„}→ℝ^{mΓ—m}) β†’ (ℝ^{Ο„}→ℝ^{mΓ—m})$$ | | $$\frac{da}{db},\frac{\partial{a}}{\partial{b}}$$ <br> $$D_b{a}$$ | a.d(b)<br>d(a) / d(b) | grad(a)[b] | | $$(\texttt{a}: C(ℝ^{Ο„}→ℝ)^{*}, \texttt{b}: C(ℝ^{Ξ»}→ℝ)) β†’ (ℝ^{?}→ℝ)$$ | | $$\nabla{a}$$ | | grad(a) | `a.

Related Skills

View on GitHub
GitHub Stars547
CategoryDevelopment
Updated1d ago
Forks23

Languages

Kotlin

Security Score

100/100

Audited on Mar 26, 2026

No findings