1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Each `Var` contains a reference-counted pointer to a
//! `Vari`, stored in memory arena.
use std::fmt;
pub use core::types::*;
pub use core::vari::*;
pub use std::rc::Rc;
use operations;

pub struct Var {
    pub vi_: Rc<*mut Vari>,
}

impl Var {
    pub fn new(vi: Rc<*mut Vari>) -> Var {
        Var { vi_: vi }
    }
    pub fn val(&self) -> Real {
        let vi: &Vari = self.vi_.clone().into();
        vi.val()
    }
    pub fn adj(&self) -> Real {
        let vi: &Vari = self.vi_.clone().into();
        vi.adj()
    }
    pub fn grad(&self) {
        operations::grad::grad(self.vi_.clone());
    }
    pub fn get_vari_refmut<'a>(&self) -> &'a mut Vari {
        self.vi_.clone().into()
    }
    pub fn get_vari_ref<'a>(&self) -> &'a Vari {
        self.vi_.clone().into()
    }
    pub fn set_zero_all_adjoints(&self) {
        let mem = self.get_vari_ref().mem();
        mem.borrow().set_zero_all_adjoints();
    }
}

impl fmt::Debug for Var {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Var {{ val: {}, adj: {} }}", self.val(), self.adj())
    }
}