Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to do a[a>0]-=1 in rust version? #347

Answered by 9prady9
3togo asked this question in Q&A
Discussion options

How to do a[a>0]-=1 in rust version?

You must be logged in to vote

One way would be the following:

let non_negs = a > 0;
let res = non_negs * (a - 1) + (1 - non_negs) * a;

Indexing functions like lookup and eval macro can also be used I think but I believe the above version is better in terms of performance because there is no random lookup involved and all reads/writes to GPU memory are coalesced.

Replies: 1 comment 3 replies

Comment options

One way would be the following:

let non_negs = a > 0;
let res = non_negs * (a - 1) + (1 - non_negs) * a;

Indexing functions like lookup and eval macro can also be used I think but I believe the above version is better in terms of performance because there is no random lookup involved and all reads/writes to GPU memory are coalesced.

You must be logged in to vote
3 replies
Comment options

I try to implement your suggested answer as follow:

let non_negs = gt(&a, &(0.0 as f32), false);
let a = &non_negs * (&a - 1.0 as f32) + (1 - &non_negs) * &a ;

However, I need to use "gt" instead of ">"
If I use ">", the compiler will complain that ">" cannot be applied to type '&arrayfire::Array<f32>' rustc(E0369)
let non_negs = &a > 0.0 as f32;

Comment options

Yes, the operator isn't implemented for default move semantics of rust, it accepts only a reference object.
Is this let non_negs = &a > 0.0 as f32; snippet also not working ?

Comment options

Never mind, my mistake - it has been a while I touched this section of rust wrapper. Rust doesn't have operator trait for comparison - not in the way arrayfire comparison results are reported. Hence that code doesn't work. Please use gt function.

Answer selected by 3togo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /