Skip to main content

optionstratlib_math/geometrics/operations/
traits.rs

1use crate::geometrics::MergeOperation;
2
3/// A trait that provides arithmetic operations for working with geometries,
4/// enabling merging and combining of curve data based on different operations.
5///
6/// This trait allows performing mathematical operations between geometric objects,
7/// particularly curves, to create new derived geometries. It supports various
8/// operations like addition, subtraction, multiplication, and more through the
9/// `MergeOperation` enum.
10///
11/// # Type Parameters
12/// - `Input`: The type of geometric object that can be merged and operated upon.
13///
14/// # Associated Types
15/// - `Error`: Represents the type of error that may occur during curve operations.
16///
17/// # Required Methods
18///
19/// ## `merge`
20/// Combines multiple geometries into a single curve based on a specified `MergeOperation`.
21///
22/// ### Parameters
23/// - `geometries`: A slice of references to the input geometries that need to be merged.
24/// - `operation`: The operation used to combine the geometries (e.g., addition, subtraction, etc.).
25///
26/// ### Returns
27/// - `Result<Input, Self::Error>`: Returns the resulting merged curve if successful,
28///   or an error of type `Self::Error` if the merge process fails.
29///
30/// ## `merge_with`
31/// Merges the current curve with another curve based on a specified `MergeOperation`.
32///
33/// ### Parameters
34/// - `other`: A reference to another curve to be merged.
35/// - `operation`: The operation used to combine the geometries (e.g., addition, subtraction, etc.).
36///
37/// ### Returns
38/// - `Result<Input, Self::Error>`: Returns the resulting merged curve if successful,
39///   or an error of type `Self::Error` if the merge process fails.
40///
41/// # Examples
42///
43/// ```rust
44/// use rust_decimal::Decimal;
45/// use optionstratlib_math::geometrics::{Arithmetic, GeometricObject, MergeOperation};
46/// use optionstratlib_math::curves::{Curve, Point2D};
47///
48/// let curve1 = Curve::from_vector(vec![
49///     Point2D::new(Decimal::ZERO, Decimal::ZERO),
50///     Point2D::new(Decimal::ONE, Decimal::ONE),
51/// ]);
52/// let curve2 = Curve::from_vector(vec![
53///     Point2D::new(Decimal::ZERO, Decimal::ONE),
54///     Point2D::new(Decimal::ONE, Decimal::TWO),
55/// ]);
56///
57/// // Merge two curves by adding their values
58/// let result_curve = Curve::merge(&[&curve1, &curve2], MergeOperation::Add);
59/// ```
60///
61/// # Notes
62/// - This trait is designed to be implemented for specific curve types which define how
63///   the merging will occur. The associated error type should capture and communicate
64///   any issues encountered during operations.
65/// - The implementation may need to handle cases where curves have different domains
66///   or sampling points.
67pub trait Arithmetic<Input> {
68    /// The error type returned when merging operations fail
69    type Error;
70
71    /// Combines multiple geometries into one using the specified merge operation.
72    ///
73    /// # Errors
74    ///
75    /// Returns the implementation's `Self::Error` when the input
76    /// slice is empty, when the selected `operation` is undefined
77    /// for the provided geometries, or when axis alignment fails.
78    fn merge(geometries: &[&Input], operation: MergeOperation) -> Result<Input, Self::Error>;
79
80    /// Merges the current curve with another curve using the specified merge operation.
81    ///
82    /// # Errors
83    ///
84    /// Same failure surface as `merge` applied to the two-element
85    /// collection `[self, other]`.
86    fn merge_with(&self, other: &Input, operation: MergeOperation) -> Result<Input, Self::Error>;
87}