Skip to content

Implement (Scalar +-*/ Array) #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/arith/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ where

macro_rules! arith_scalar_func {
($rust_type: ty, $op_name:ident, $fn_name: ident) => {
// Implement (&Array<T> op_name rust_type)
impl<'f, T> $op_name<$rust_type> for &'f Array<T>
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
Expand All @@ -685,6 +686,7 @@ macro_rules! arith_scalar_func {
}
}

// Implement (Array<T> op_name rust_type)
impl<T: HasAfEnum> $op_name<$rust_type> for Array<T>
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
Expand All @@ -698,6 +700,34 @@ macro_rules! arith_scalar_func {
$fn_name(&self, &temp, false)
}
}

// Implement (rust_type op_name &Array<T>)
impl<'f, T> $op_name<&'f Array<T>> for $rust_type
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
$rust_type: HasAfEnum + ImplicitPromote<T>,
<$rust_type as ImplicitPromote<T>>::Output: HasAfEnum,
{
type Output = Array<<$rust_type as ImplicitPromote<T>>::Output>;

fn $fn_name(self, rhs: &'f Array<T>) -> Self::Output {
$fn_name(&self, rhs, false)
}
}

// Implement (rust_type op_name Array<T>)
impl<T> $op_name<Array<T>> for $rust_type
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
$rust_type: HasAfEnum + ImplicitPromote<T>,
<$rust_type as ImplicitPromote<T>>::Output: HasAfEnum,
{
type Output = Array<<$rust_type as ImplicitPromote<T>>::Output>;

fn $fn_name(self, rhs: Array<T>) -> Self::Output {
$fn_name(&self, &rhs, false)
}
}
};
}

Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions tests/scalar_arith.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use ::arrayfire::*;

#[allow(non_snake_case)]
#[test]
fn check_scalar_arith() {
let dims = Dim4::new(&[5, 5, 1, 1]);
let A = randu::<f32>(dims);
let s: f32 = 2.0;
let scalar_as_lhs = s * &A;
let scalar_as_rhs = &A * s;
let C = constant(s, dims);
let no_scalars = A * C;
let scalar_res_comp = eq(&scalar_as_lhs, &scalar_as_rhs, false);
let res_comp = eq(&scalar_as_lhs, &no_scalars, false);
let scalar_res = all_true_all(&scalar_res_comp);
let res = all_true_all(&res_comp);

assert_eq!(scalar_res.0, res.0);
}