-
Notifications
You must be signed in to change notification settings - Fork 59
New data generation functions and Neg trait implementation #94
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
Conversation
function to create an array with same shape and type as input array filled with a constant
Are there non templated versions of random number generators ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Just the one query.
type Output = Array; | ||
|
||
fn neg(self) -> Self::Output { | ||
constant_like(0.0, &self) - self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a benefit to this vs the following?
af::mul(&-1.0, &self);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are doing same number of operations (create a constant array and then do arithmetic operation of two arrays) under the hood except that the intrinsic operation is subtraction in the trait implementation where as the other version is doing multiplication. I think they would perform about the same - compiler may even optimise it for this particular cases (Multiplication by -1 and Subtraction from 0).
/// # Return Values | ||
/// | ||
/// Array with given constant value and input Array's shape and similar internal data type. | ||
pub fn constant_like(value: f64, input: &Array) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition
@arrayfire/rust-devel Please check the latest commit, it should cover all the missing gaps now. |
/// # Return Values | ||
/// | ||
/// Array filled with `value` and `dims` shape. | ||
pub fn create_constant_i64(value: i64, dims: Dim4) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see why you made these separate from the create_constant
[precision], however can we do something to merge them? I.e. maybe an Enum or something? Not too big a deal either way though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jramapuram How can we do this using enum ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of something like this: https://is.gd/taX9ac
/// # Return Values | ||
/// | ||
/// Array of `dims` shape and filed with given constant `value`. | ||
pub fn create_constant(real: f64, imag: f64, dims: Dim4, dtype: DType) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to remove all the create_
parts from these functions? I think it is obvious that someone wants to create a constant if they call the constant
function. It will also be more more consistent with the AF api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't have same function names. If we remove the create_
prefix then name clash will occur.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes forgot we already have that and need to specialize. See my gist for a possible solution: https://is.gd/taX9ac
/// # Return Values | ||
/// | ||
/// Array filled with `value` and `dims` shape. | ||
pub fn create_constant_u64(value: u64, dims: Dim4) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment w.r.t. create_
/// # Return Values | ||
/// Array | ||
#[allow(unused_mut)] | ||
pub fn create_range(dims: Dim4, seq_dim: i32, dtype: DType) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment w.r.t. create_
/// | ||
/// Identity matrix | ||
#[allow(unused_mut)] | ||
pub fn create_identity(dims: Dim4, dtype: DType) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment w.r.t. create_
/// | ||
/// Array | ||
#[allow(unused_mut)] | ||
pub fn create_iota(dims: Dim4, tdims: Dim4, dtype: DType) -> Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment w.r.t. create_
I found the need for constant a while ago. But since I don't really need use af;
use conv::{ConvUtil, Saturate};
/// Helper to return a constant value based on type
pub fn constant(dims: Dim4, aftype: DType, val: f32) -> Array {
match aftype
{
DType::F32 => af::constant(val, dims),
DType::F64 => af::constant(val.approx_as::<f64>().unwrap(), dims),
DType::C32 => af::constant(Complex::new(val, 0f32)
, dims),
DType::C64 => af::constant(Complex::new(val.approx_as::<f64>().unwrap(), 0f64)
, dims),
DType::B8 => {
if val > 0f32 {
af::constant(true, dims)
}else{
af::constant(false, dims)
}
},
DType::S32 => af::constant(val.approx_as::<i32>().saturate().unwrap(), dims),
DType::U32 => af::constant(val.approx_as::<u32>().saturate().unwrap(), dims),
DType::U8 => af::constant(val.approx_as::<u8>().saturate().unwrap(), dims),
DType::S64 => af::constant(val.approx_as::<i64>().saturate().unwrap(), dims),
DType::U64 => af::constant(val.approx_as::<u64>().saturate().unwrap(), dims),
DType::S16 => af::constant(val.approx_as::<i16>().saturate().unwrap(), dims),
DType::U16 => af::constant(val.approx_as::<u16>().saturate().unwrap(), dims),
}
} |
@jramapuram I don't like the |
@pavanky @jramapuram Feedback pls, i want to release 3.4.1 of this wrapper this week if possible. |
why won't the function shown here work: |
That version is sacrificing accuracy of
I don't think we can do that as a library. Edited: Added to that, we can't have another function with same name as |
@9prady9 accept |
I didn't check C++ API, but C API of arrayfire has different functions for Edited:
|
@9prady9 I wasn't talking about having multiple functions, but about the name (specifically the |
Yes, we can't have same names. |
For him it is working because he had them in different namespace. |
Sorry for the delay. Infrequent internet over the winter break. Will review in 2 days. |
@arrayfire/rust-devel Awaiting feedback. |
Added a simple way to use Enums in comments. Another possible (but have not tested myself) way is to use generics: http://stackoverflow.com/questions/26810793/how-can-i-create-an-is-prime-function-that-is-generic-over-various-integer-types See solution in that answer as well as comment in accepted answer. Not sure if the generics are robust enough to accept all numeric types yet though. |
* constant_t * range_t * range_t * iota_t
d202256
to
10a20d1
Compare
@jramapuram please check now. Using your suggestion, i have consolidated all the separate create_constant_* functions into a single function @pavanky please check now. I have renamed all create_* fns to have |
looks good 👍 |
Looks great! |
Fixes #93
Fixes #89