numrs/ops/elementwise/binary/
add.rs

1use crate::array::{Array, DTypeValue};
2use crate::llo::ElementwiseKind;
3use anyhow::Result;
4
5/// Elementwise add with automatic type promotion
6/// 
7/// Returns an Array with the promoted dtype. The return type defaults to f32
8/// unless type promotion determines otherwise.
9/// 
10/// # Examples
11/// ```
12/// use numrs::{Array, ops};
13/// 
14/// let a = Array::new(vec![3], vec![1.0, 2.0, 3.0]);
15/// let b = Array::new(vec![3], vec![4.0, 5.0, 6.0]);
16/// let c = ops::add(&a, &b).unwrap();
17/// assert_eq!(c.data, vec![5.0, 7.0, 9.0]);
18/// ```
19#[inline(always)]
20pub fn add<T1, T2>(a: &Array<T1>, b: &Array<T2>) -> Result<Array>
21where
22    T1: DTypeValue,
23    T2: DTypeValue,
24{
25    // Internally uses DynArray for type promotion, then converts to Array
26    let dyn_result = crate::ops::promotion_wrappers::binary_promoted(a, b, ElementwiseKind::Add, "add")?;
27    dyn_result.into_typed()
28}