Skip to content

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

Merged
merged 3 commits into from
Jan 10, 2017

Conversation

9prady9
Copy link
Member

@9prady9 9prady9 commented Dec 15, 2016

Fixes #93
Fixes #89

function to create an array with same shape and type as input array filled with a constant
@pavanky
Copy link
Member

pavanky commented Dec 15, 2016

Are there non templated versions of random number generators ?

Copy link
Member

@jramapuram jramapuram left a 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
Copy link
Member

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);

Copy link
Member Author

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition

@9prady9
Copy link
Member Author

9prady9 commented Dec 17, 2016

@arrayfire/rust-devel Please check the latest commit, it should cover all the missing gaps now.

@9prady9 9prady9 changed the title Constant_like and Neg trait New data generation functions and Neg trait implementation Dec 18, 2016
@9prady9 9prady9 requested a review from jramapuram December 19, 2016 07:57
/// # Return Values
///
/// Array filled with `value` and `dims` shape.
pub fn create_constant_i64(value: i64, dims: Dim4) -> Array {
Copy link
Member

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.

Copy link
Member Author

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 ?

Copy link
Member

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 {
Copy link
Member

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

Copy link
Member Author

@9prady9 9prady9 Dec 20, 2016

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.

Copy link
Member

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 {
Copy link
Member

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 {
Copy link
Member

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 {
Copy link
Member

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 {
Copy link
Member

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_

@jramapuram
Copy link
Member

I found the need for constant a while ago. But since I don't really need U64_MAX & I64_MAX I approximated it with the following [using conv crate]:

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),
  }
}

@9prady9 9prady9 removed the request for review from pavanky December 20, 2016 10:06
@9prady9
Copy link
Member Author

9prady9 commented Dec 20, 2016

@jramapuram I don't like the create_ prefix either, but i couldn't figure out a way to keep both earlier versions of the data generation functions and the new ones with same function names. Even if i mark a function as deprecated, the new function cannot have the same name.

@9prady9
Copy link
Member Author

9prady9 commented Jan 3, 2017

@pavanky @jramapuram Feedback pls, i want to release 3.4.1 of this wrapper this week if possible.

@pavanky
Copy link
Member

pavanky commented Jan 3, 2017

@jramapuram @9prady9

why won't the function shown here work:
#94 (comment)

@9prady9
Copy link
Member Author

9prady9 commented Jan 3, 2017

That version is sacrificing accuracy of u64 and i64 by accepting the constant value as an f32

pub fn constant(dims: Dim4, aftype: DType, val: f32)

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 constant, hence the type suffixes.

@pavanky
Copy link
Member

pavanky commented Jan 3, 2017

@9prady9 accept f64 and have special version called constant_long or something. This is what we do for the C and C++ versions anyway.

@9prady9
Copy link
Member Author

9prady9 commented Jan 3, 2017

I didn't check C++ API, but C API of arrayfire has different functions for u64, i64 and rest of the types.

Edited:

  • af_constant_ulong
  • af_constant_long
  • af_constant_complex and af_constant for test of the types

@pavanky
Copy link
Member

pavanky commented Jan 3, 2017

@9prady9 I wasn't talking about having multiple functions, but about the name (specifically the create_ parts). Will @jramapuram's code causing a conflict?

@9prady9
Copy link
Member Author

9prady9 commented Jan 3, 2017

Yes, we can't have same names.

@9prady9
Copy link
Member Author

9prady9 commented Jan 3, 2017

For him it is working because he had them in different namespace.

@jramapuram
Copy link
Member

Sorry for the delay. Infrequent internet over the winter break. Will review in 2 days.

@9prady9
Copy link
Member Author

9prady9 commented Jan 9, 2017

@arrayfire/rust-devel Awaiting feedback.

@jramapuram
Copy link
Member

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
@9prady9 9prady9 force-pushed the constant_like_and_Negtrait branch from d202256 to 10a20d1 Compare January 10, 2017 16:09
@9prady9
Copy link
Member Author

9prady9 commented Jan 10, 2017

@jramapuram please check now. Using your suggestion, i have consolidated all the separate create_constant_* functions into a single function constant_t with the suffix _t suggesting that it takes additional type parameter (that's the shortest name i could come up with that is different from constant).

@pavanky please check now. I have renamed all create_* fns to have _t suffix now.

@pavanky
Copy link
Member

pavanky commented Jan 10, 2017

looks good 👍

@jramapuram
Copy link
Member

Looks great!

@9prady9 9prady9 merged commit 78a0353 into arrayfire:devel Jan 10, 2017
@9prady9 9prady9 deleted the constant_like_and_Negtrait branch January 10, 2017 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants