Skip to content

Commit

Permalink
chore: move existing Value to forward module
Browse files Browse the repository at this point in the history
  • Loading branch information
hidal00p committed Jul 16, 2024
1 parent ceeb205 commit 9fa6afc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/forward/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod value;
File renamed without changes.
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod backprop;
mod value;
mod forward;

use value::Value;
use forward::value as fwd;

fn f(x: Value) -> Value {
let a = Value::passive(3.0); // Passive variable
let b = Value::passive(2.0); // Passive variable
fn f(x: fwd::Value) -> fwd::Value {
let a = fwd::Value::passive(3.0); // Passive variable
let b = fwd::Value::passive(2.0); // Passive variable

// purely copying
a * x * x + b / x
Expand All @@ -20,14 +20,14 @@ fn univariate_example() {
* dfdx(x = 2.0) = 11.5
*/

let x = Value::new(2.0, 1.0); // Active variable
let x = fwd::Value::new(2.0, 1.0); // Active variable
let y = f(x);

assert_eq!(y.value, 13.0);
assert_eq!(y.der, 11.5);
}

fn g(x1: Value, x2: Value) -> Value {
fn g(x1: fwd::Value, x2: fwd::Value) -> fwd::Value {
return x1 * x2;
}

Expand All @@ -45,7 +45,7 @@ fn multivariate_example() {
*
* Note that this could be extended to R^n.
*/
let mut x = vec![Value::new(2.0, 0.0), Value::new(3.0, 0.0)]; // vector of arguments i.e. x = (x1, x2)
let mut x = vec![fwd::Value::new(2.0, 0.0), fwd::Value::new(3.0, 0.0)]; // vector of arguments i.e. x = (x1, x2)
let mut grad_g = vec![0.0; 2];
for i in 0..2 {
x[i].der = 1.0; // seed the desired partial
Expand Down

0 comments on commit 9fa6afc

Please sign in to comment.