Giter Club home page Giter Club logo

autodiff's Introduction

AutoDiff

Single variable automatic differentiation in Rust, with support for:

  • Basic arithmetic: $u + v$, $u - v$, $u*v$, $\frac{u}{v}$
  • Power: $u^n$, $\sqrt{u}$
  • Exponentation: $e^u$
  • Trigonometry: $\sin{u}$ and $\cos{u}$
  • Inverse trigonometry: $\arctan{u}$
  • Logarithm: $\ln{u}$
  • Composition: $u \circ v$

Usage

Create a function with operators and methods

For example: $f(x) = \frac{x^3}{2} + \sin{2x}$

use autodiff::X;

let f = X.pow(3.0) / 2.0 + (2.0 * X).sin();

Compute the value of the function and its derivative

For example: $f(3)$ and $f'(3)$

use autodiff::Fn; // Import the Fn trait to use .eval()

let (value, derivative) = f.eval(3.0);

println!("f(3)  = {value}");      // 13.220585
println!("f'(3) = {derivative}"); // 15.420341

Do cool things with the derivative like finding the local minima/maxima

For example: $f(x) = \frac{4x^4}{5} - \frac{3x^3}{2} - x^2 + 2x + \frac{5}{2}$

let f = 0.8 * X.pow(4.0) - 1.5 * X.pow(3.0) - X.pow(2.0) + 2.0 * X + 2.5;

let mut input = 0.0;

for _ in 0..100 {
    let (_, grad) = f.eval(input);
    
    input -= grad * 0.1;
}

let (output, _) = f.eval(input);
println!("a local minima of f(x) is f({input}) = {output}");

TODO

  • Multiple variables
  • High order derivative
  • Visual examples

License

This project is licensed under the MIT License.

autodiff's People

Contributors

ziap avatar

Stargazers

Phat Dang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.