elementary API

The elementary package is the facade for the analytic traits of arithmetic that Dual[T] implements. It re-exports Dual, Sqrt, SqrtChecked, Exponential, Logarithmic, Trigonometric and Constants. Import it when you write generic code against these traits and want to differentiate it. The derivative rules are listed on the dual API.

Source: src/elementary/alias.mbt.

Importing

import {
  "Luna-Flow/autodiff/elementary",
}

Re-exported type

Dual

The dual number type.

pub using @dual {type Dual}

Analytic traits

TraitMethodsTangent on Dual[T]
Sqrtsqrtb/(2a)b/(2\sqrt a)
SqrtCheckedsqrt_checkedb/(2a)b/(2\sqrt a), checked
Exponentialexp, exp2b eab\,e^a, b 2aln⁡2b\,2^a\ln 2
Logarithmicln, log2, log10b/ab/a, b/(aln⁡2)b/(a\ln 2), b/(aln⁡10)b/(a\ln 10)
Trigonometricsin, cos, tanbcos⁡ab\cos a, −bsin⁡a-b\sin a, b/cos⁡2ab/\cos^2 a
Constantspi, tau, e00

Sqrt

Unchecked square root.

pub using @arithmetic {trait Sqrt}

SqrtChecked

Square root returning Result[Self, ArithmeticError]; see the checked API.

pub using @arithmetic {trait SqrtChecked}

Exponential

exp and exp2. The Dual[T] instance needs T : Exponential + Logarithmic + IntegralHomomorphism + Mul, because exp2 uses ln⁡2\ln 2.

pub using @arithmetic {trait Exponential}

Logarithmic

ln, log2 and log10. The Dual[T] instance needs T : Logarithmic + IntegralHomomorphism + Mul + Div.

pub using @arithmetic {trait Logarithmic}

Trigonometric

sin, cos and tan. The Dual[T] instance needs T : Trigonometric + Mul + Neg + Div.

pub using @arithmetic {trait Trigonometric}

Constants

π\pi, τ=2π\tau = 2\pi and ee; on Dual[T] they are constants with tangent zero.

pub using @arithmetic {trait Constants}
fn[T : @elementary.Trigonometric + @elementary.Exponential + Mul] damped(x : T) -> T {
  @elementary.Exponential::exp(x) * @elementary.Trigonometric::sin(x)
}

test "analytic traits on dual numbers" {
  let y = damped(@elementary.Dual::variable(0.0))
  assert_eq(y.value(), 0.0)
  assert_eq(y.tangent(), 1.0) // e^0 (sin 0 + cos 0)
  let tau : @elementary.Dual[Double] = @elementary.Constants::tau()
  assert_eq(tau.tangent(), 0.0)
}