Module peroxide::structure::ad

source ·
Expand description

Taylor mode forward automatic differentiation

§Important Features

  • Can automatic differentiate up to 2nd order (AD0 ~ AD2)
  • All AD are in stack (Guarantee high performance)
  • You can see AD via .print()

§Implemented Traits for AD

  • #[derive(Debug, Copy, Clone, PartialEq)]
  • std::fmt::Display
  • IntoIterator<Item = f64>
  • IntoIterator<Item = &f64>
  • IntoIterator<Item = &mut f64>
  • FromIterator<f64>
  • FromIterator<&f64>
  • Index, IndexMut
  • std::ops::{Neg, Add, Sub, Mul, Div}
  • peroxide_num::{PowOps, ExpLogOps, TrigOps}
  • peroxide::util::print::Printable

§Iterator of AD

There are three iterators.

  • ADIntoIter
  • ADIter<'a>
  • ADIterMut<'a>

Each implements DoubleEndedIterator, ExactSizeIterator also.

§Default Constructor

  • AD0(f64) : just constant
  • AD1(f64, f64) : 1st order AD
  • AD2(f64, f64, f64) : 2nd order AD

§Methods

  • empty(&self) -> AD
  • to_order(&self, n: usize) -> AD
  • iter(&self) -> ADIter{i}
  • iter_mut(&self) -> ADIterMut{i}
  • len(&self) -> usize
  • order(&self) -> usize
  • from_order(n: usize) -> AD
  • x(&self) -> f64
  • dx(&self) -> f64
  • ddx(&self) -> f64

§Implemented Operations

  • Add, Sub, Mul, Div
  • sin, cos, tan
  • sinh, cosh, tanh
  • sin_cos, sinh_cosh
  • exp, ln, log, log2, log10
  • powi, powf, sqrt, pow
  • asin, acos, atan
  • asinh, acosh, atanh

§Usage

§Construction

extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    // Declare x where x = 2
    let a = AD1(2f64, 1f64);
    // Declare x^2 where x = 2
    let b = AD2(4f64, 4f64, 2f64);
    // Convert AD1 -> AD2
    let c = a.to_order(2);
    // Zeros
    let d = AD::from_order(2);

    assert_eq!(c, AD2(2f64, 1f64, 0f64));
    assert_eq!(d, AD2(0f64, 0f64, 0f64));
}

§Operation

For every binary operation, it returns higher order AD (ex: AD1 + AD2 = AD2)

extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = AD1(2f64, 1f64);       // x        at x = 2
    let b = AD2(4f64, 4f64, 2f64); // x^2      at x = 2
    let c = a + b;                 // x^2 + x  at x = 2
    let d = a * b;                 // x^3      at x = 2
    let e = a / b;                 // 1/x      at x = 2
    assert_eq!(c, AD2(6f64, 5f64, 2f64));
    assert_eq!(d, AD2(8f64, 12f64, 12f64));
    assert_eq!(e, AD2(0.5, -0.25, 0.25));
}

Structs§

Enums§

Traits§