Skip to content

Commit bcd9909

Browse files
committed
new data generation function: constant_like
function to create an array with same shape and type as input array filled with a constant
1 parent babec9d commit bcd9909

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/data/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,26 @@ pub fn replace_scalar(a: &mut Array, cond: &Array, b: f64) {
622622
HANDLE_ERROR(AfError::from(err_val));
623623
}
624624
}
625+
626+
/// Create an array filled with given constant retaining type/shape of another Array.
627+
///
628+
/// # Parameters
629+
///
630+
/// - `value` is the constant with which output Array is to be filled
631+
/// - `input` is the Array whose shape the output Array has to maintain
632+
///
633+
/// # Return Values
634+
///
635+
/// Array with given constant value and input Array's shape and similar internal data type.
636+
pub fn constant_like(value: f64, input: &Array) -> Array {
637+
let dims = input.dims();
638+
unsafe {
639+
let mut temp: i64 = 0;
640+
let err_val = af_constant(&mut temp as MutAfArray, value as c_double,
641+
dims.ndims() as c_uint,
642+
dims.get().as_ptr() as *const DimT,
643+
input.get_type() as c_int);
644+
HANDLE_ERROR(AfError::from(err_val));
645+
Array::from(temp)
646+
}
647+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod backend;
3232
pub use blas::{matmul, dot, transpose, transpose_inplace};
3333
mod blas;
3434

35-
pub use data::{constant, range, iota};
35+
pub use data::{constant, constant_like, range, iota};
3636
pub use data::{identity, diag_create, diag_extract, lower, upper};
3737
pub use data::{join, join_many, tile};
3838
pub use data::{reorder, shift, moddims, flat, flip};

0 commit comments

Comments
 (0)