2 unstable releases

0.2.0 Dec 6, 2018
0.1.0 Sep 25, 2017

#20 in #petgraph


Used in cagra-parser

MIT license

18KB
398 lines

cagra

docs.rs Build Status

Calculation Graph library


lib.rs:

cagra

Tiny CAlculation GRAph library

Features

  • No frontend and No backend.
  • Adjacency list graph based on petgraph
  • Serialization with serde

Examples

Create a graph for z = (x + y) - 2*x*y

use cagra::graph::*;

let mut g: Graph<f64> = Graph::new();
let x = g.variable("x", 1.0).unwrap();
let y = g.variable("y", 3.0).unwrap();
let x_y = g.add(x, y);
let xy  = g.mul(x, y);
let a   = g.constant(2.0);
let axy = g.mul(a, xy);
let sum = g.sub(x_y, axy);

let result = g.eval_value(sum).unwrap();
abs_diff_eq!(result, -2.0);

g.eval_deriv(sum);
let dx = g.get_deriv(x).unwrap();
let dy = g.get_deriv(y).unwrap();
abs_diff_eq!(dx, -5.0);
abs_diff_eq!(dy, -1.0);

Dependencies

~4.5MB
~88K SLoC