pub enum AdValue<T> {
Primal(T),
Forward {
primal: T,
tangent: T,
},
Reverse {
primal: T,
node: NodeId,
tape: TapeId,
tangent: Option<T>,
},
}Expand description
Generic AD value that can wrap any user-defined payload type T.
This is the primary extension point of the crate.
§Examples
use ad_tensors_rs::{AdMode, AdValue, NodeId, TapeId};
let primal = AdValue::primal(3.0_f64);
assert_eq!(primal.mode(), AdMode::Primal);
let dual = AdValue::forward(3.0_f64, 1.0_f64);
assert_eq!(dual.mode(), AdMode::Forward);
let tracked = AdValue::reverse(3.0_f64, NodeId(1), TapeId(9), None);
assert_eq!(tracked.mode(), AdMode::Reverse);Variants§
Primal(T)
Primal-only value.
Forward
Forward-mode value and tangent.
Reverse
Reverse-mode value with graph metadata.
Implementations§
Source§impl<T> AdValue<T>
impl<T> AdValue<T>
Sourcepub fn primal(value: T) -> Self
pub fn primal(value: T) -> Self
Creates a primal-only value.
§Examples
use ad_tensors_rs::AdValue;
let x = AdValue::primal(2_i32);
assert!(matches!(x, AdValue::Primal(2)));Sourcepub fn forward(primal: T, tangent: T) -> Self
pub fn forward(primal: T, tangent: T) -> Self
Creates a forward-mode value.
§Examples
use ad_tensors_rs::AdValue;
let x = AdValue::forward(2.0_f64, 1.0_f64);
assert!(matches!(x, AdValue::Forward { .. }));Sourcepub fn reverse(
primal: T,
node: NodeId,
tape: TapeId,
tangent: Option<T>,
) -> Self
pub fn reverse( primal: T, node: NodeId, tape: TapeId, tangent: Option<T>, ) -> Self
Creates a reverse-mode value.
§Examples
use ad_tensors_rs::{AdValue, NodeId, TapeId};
let x = AdValue::reverse(2.0_f64, NodeId(3), TapeId(5), Some(0.1));
assert!(matches!(x, AdValue::Reverse { .. }));Sourcepub fn mode(&self) -> AdMode
pub fn mode(&self) -> AdMode
Returns the AD mode.
§Examples
use ad_tensors_rs::{AdMode, AdValue};
let x = AdValue::forward(1.0_f64, 1.0_f64);
assert_eq!(x.mode(), AdMode::Forward);Sourcepub fn primal_ref(&self) -> &T
pub fn primal_ref(&self) -> &T
Returns a reference to the primal payload.
§Examples
use ad_tensors_rs::AdValue;
let x = AdValue::forward(10_i32, 1_i32);
assert_eq!(x.primal_ref(), &10);Sourcepub fn primal_mut(&mut self) -> &mut T
pub fn primal_mut(&mut self) -> &mut T
Returns a mutable reference to the primal payload.
§Examples
use ad_tensors_rs::AdValue;
let mut x = AdValue::primal(1_i32);
*x.primal_mut() = 7;
assert_eq!(x.primal_ref(), &7);Sourcepub fn tangent_ref(&self) -> Option<&T>
pub fn tangent_ref(&self) -> Option<&T>
Returns a reference to tangent payload when available.
§Examples
use ad_tensors_rs::AdValue;
let x = AdValue::forward(2.0_f64, 3.0_f64);
assert_eq!(x.tangent_ref(), Some(&3.0));Sourcepub fn node_id(&self) -> Option<NodeId>
pub fn node_id(&self) -> Option<NodeId>
Returns reverse-mode node id when available.
§Examples
use ad_tensors_rs::{AdValue, NodeId, TapeId};
let x = AdValue::reverse(1.0_f64, NodeId(4), TapeId(6), None);
assert_eq!(x.node_id(), Some(NodeId(4)));Trait Implementations§
Source§impl<T: Clone> Differentiable for AdValue<T>
impl<T: Clone> Differentiable for AdValue<T>
impl<T> StructuralPartialEq for AdValue<T>
Auto Trait Implementations§
impl<T> Freeze for AdValue<T>where
T: Freeze,
impl<T> RefUnwindSafe for AdValue<T>where
T: RefUnwindSafe,
impl<T> Send for AdValue<T>where
T: Send,
impl<T> Sync for AdValue<T>where
T: Sync,
impl<T> Unpin for AdValue<T>where
T: Unpin,
impl<T> UnwindSafe for AdValue<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more