Implementation:Avhz RustQuant Vertex
| Knowledge Sources | |
|---|---|
| Domains | Automatic_Differentiation, Mathematics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
The Vertex struct and Arity enum define the fundamental node type in the computational graph used for reverse-mode automatic differentiation.
Description
The Vertex struct represents a single node in the computational graph. Each vertex stores two fixed-size arrays:
partials: [f64; 2]-- The partial derivatives of this operation with respect to its parent vertices. For a binary operation likex + y,partials[0]holds the partial with respect to the first parent andpartials[1]holds the partial with respect to the second.parents: [usize; 2]-- The indices of the parent vertices in the graph. These point back to the operands that produced this vertex.
All operations are modeled with a uniform binary layout (two parents, two partials). Unary operations (e.g., sin(x)) set the second partial to 0.0 and the second parent to 0. Nullary operations (e.g., input variables or constants) set both partials to 0.0 and both parents to 0. This uniform representation avoids the need for enum-based operation dispatch and allows the graph to be stored as a simple Vec<Vertex>.
The Arity enum classifies operations into three kinds:
Nullary-- No parents (input variables and constants)Unary-- One parent (e.g.,sin,exp,sqrt)Binary-- Two parents (e.g.,+,*,/)
The Arity enum is used by the graph's push() method to determine how to construct a new vertex, but it is not stored on the vertex itself.
The Vertex struct implements Clone, Copy, Debug, PartialEq, Eq, and Display, making it fully transparent and easy to work with in tests and debugging output.
Usage
Vertices are typically not constructed directly by users. They are created internally by the Graph::push() method when overloaded operators and mathematical functions are applied to Variable values. However, the constructors (new_binary, new_unary, new_nullary) and accessors (get_partials, get_parents) are public for use in testing, custom graph construction, and introspection of the computation graph.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_autodiff/src/vertex.rs
- Lines: 1-157
Signature
/// Struct defining the vertex of the computational graph.
#[derive(Clone, Copy, Debug)]
pub struct Vertex {
/// Array that contains the partial derivatives wrt to x and y.
pub partials: [f64; 2],
/// Array that contains the indices of the parent vertices.
pub parents: [usize; 2],
}
/// Enumeration for the operation type.
#[derive(Copy, Clone, Debug)]
pub enum Arity {
/// Nullary operation (e.g. a constant). No parents.
Nullary,
/// Unary operation (e.g. sin(x)). One parent.
Unary,
/// Binary operation (e.g. x + y). Two parents.
Binary,
}
impl Vertex {
/// Get the partials of the vertex.
pub const fn get_partials(&self) -> [f64; 2];
/// Get the parents of the vertex.
pub const fn get_parents(&self) -> [usize; 2];
/// Instantiate a new vertex from a binary operation.
pub const fn new_binary(
partial_x: f64, parent_x: usize,
partial_y: f64, parent_y: usize,
) -> Self;
/// Instantiate a new vertex from a unary operation.
pub const fn new_unary(partial_x: f64, parent_x: usize) -> Self;
/// Instantiate a new vertex from a nullary operation.
pub const fn new_nullary() -> Self;
}
Import
use RustQuant::autodiff::vertex::{Vertex, Arity};
I/O Contract
Inputs
new_binary
| Name | Type | Required | Description |
|---|---|---|---|
| partial_x | f64 |
Yes | Partial derivative with respect to the first parent. |
| parent_x | usize |
Yes | Graph index of the first parent vertex. |
| partial_y | f64 |
Yes | Partial derivative with respect to the second parent. |
| parent_y | usize |
Yes | Graph index of the second parent vertex. |
new_unary
| Name | Type | Required | Description |
|---|---|---|---|
| partial_x | f64 |
Yes | Partial derivative with respect to the parent. |
| parent_x | usize |
Yes | Graph index of the parent vertex. |
new_nullary
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | -- | -- | No arguments. Creates a vertex with zero partials and self-referencing parent indices (both 0).
|
Outputs
| Name | Type | Description |
|---|---|---|
| vertex | Vertex |
A vertex instance with partials and parents arrays set according to the operation arity. For unary operations, partials[1] = 0.0 and parents[1] = 0. For nullary operations, both arrays are [0.0, 0.0] and [0, 0] respectively.
|
Usage Examples
use RustQuant_autodiff::vertex::{Vertex, Arity};
// Create a binary vertex representing x + y
// where parent x is at index 0 and parent y is at index 1
// d/dx (x + y) = 1.0, d/dy (x + y) = 1.0
let v_add = Vertex::new_binary(1.0, 0, 1.0, 1);
assert_eq!(v_add.get_partials(), [1.0, 1.0]);
assert_eq!(v_add.get_parents(), [0, 1]);
// Create a unary vertex representing sin(x)
// where parent x is at index 0
// d/dx sin(x) = cos(x), e.g. cos(1.0) ~ 0.5403
let v_sin = Vertex::new_unary(0.5403, 0);
assert_eq!(v_sin.get_partials(), [0.5403, 0.0]);
assert_eq!(v_sin.get_parents(), [0, 0]);
// Create a nullary vertex for an input variable
let v_input = Vertex::new_nullary();
assert_eq!(v_input.get_partials(), [0.0, 0.0]);
assert_eq!(v_input.get_parents(), [0, 0]);
// Display a vertex
let v = Vertex::new_binary(1.0, 0, 2.0, 1);
println!("{}", v);
// Output: Vertex { partials: [1, 2], parents: [0, 1] }