Equations

Asgard uses LEAN-style mathematical syntax for defining equations. This clean, functional notation supports arithmetic operations, integration, differentiation, and lambda calculus.

Syntax Overview

Operation Syntax Example
Integration int(term, var) int(f, x)
Differentiation diff(term, var) diff(f, x)
Lambda abstraction var. body abstraction x. x * x
Application apply(fn, arg) apply(f, 5)
Arithmetic +, -, *, /, ^ x + y * 2

Creating Equations

from gimle.asgard.equation.equation import Equation

# Simple equation
eq = Equation.from_string("x + y = z")

# Fundamental theorem of calculus
eq = Equation.from_string("diff(int(f, x), x) = f")

# Differential equation
eq = Equation.from_string("diff(y, t) = scalar(-0.1, y)")

print(eq)  # Output: diff(y, t) = scalar(-0.1, y)

Integration

Define integrals using the int(term, variable) syntax:

# Integrate f with respect to x
eq = Equation.from_string("int(f, x) = g")

# Nested integrals
eq = Equation.from_string("int(int(f, x), y) = g")

# Integration with arithmetic
eq = Equation.from_string("int(a + b, x) = int(a, x) + int(b, x)")

Differentiation

Define derivatives using the diff(term, variable) syntax:

# Differentiate f with respect to x
eq = Equation.from_string("diff(f, x) = g")

# Partial derivatives
eq = Equation.from_string("diff(diff(u, x), y) = 0")

# Chain with integration (fundamental theorem)
eq = Equation.from_string("diff(int(f, x), x) = f")

Lambda Calculus

Define anonymous functions using abstraction and application:

# Square function
eq = Equation.from_string("apply(abstraction x. x * x, 5) = 25")

# Nested abstractions (curried function)
eq = Equation.from_string("abstraction x. abstraction y. x + y")

# Function application
eq = Equation.from_string("apply(abstraction x. x + 1, y) = z")

Compiling to Circuits

Equations can be compiled to executable circuits:

from gimle.asgard.equation.equation import Equation
from gimle.asgard.compile.compiler import compile_equation_to_circuit

# Define the equation
eq = Equation.from_string("diff(int(f, x), x) = f")

# Compile to circuit (variable isolation is automatic)
circuit, metadata = compile_equation_to_circuit(eq)

print(circuit)
# Output: composition(register(x), deregister(x)) [1->1]

Differential Equations

Express ODEs and PDEs naturally:

# Simple ODE: dy/dt = -y (exponential decay)
eq = Equation.from_string("diff(y, t) = scalar(-1.0, y)")

# Exponential growth: dy/dt = ky
eq = Equation.from_string("diff(y, t) = k * y")

# Harmonic oscillator: d^2x/dt^2 = -omega^2 * x
eq = Equation.from_string("diff(diff(x, t), t) = scalar(-omega_squared, x)")

# Driven oscillator with damping
eq = Equation.from_string(
    "diff(diff(x, t), t) + 2 * zeta * omega * diff(x, t) + omega_squared * x = F"
)

Arithmetic Operations

Standard mathematical operators with familiar precedence:

# Basic operations
eq = Equation.from_string("x + y = z")
eq = Equation.from_string("a - b = c")
eq = Equation.from_string("2 * x = y")
eq = Equation.from_string("a / b = c")
eq = Equation.from_string("x ^ 2 = y")

# Compound expressions (use parentheses for grouping)
eq = Equation.from_string("(x + 2) * y = z")

Operator Precedence:

  1. ^ (exponentiation) - highest
  2. *, / (multiplication, division)
  3. +, - (addition, subtraction) - lowest

Identifiers and Numbers

Identifiers

# Single letter
eq = Equation.from_string("x = y")

# Multi-letter with underscores
eq = Equation.from_string("initial_value = final_value")

# With numbers (must start with letter)
eq = Equation.from_string("x1 + x2 = y")

Rules:

Numbers

# Integers
eq = Equation.from_string("x = 42")

# Decimals
eq = Equation.from_string("pi = 3.14159")

# Negative numbers
eq = Equation.from_string("rate = -0.05")

Next Steps