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 properly use join_many? #344

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

join_many requires the use of Vec::<&Array<T>> to do the array joining. To do so, I need to create another vector Vec::<Array<T>> to store the arrays first before joining.

Can the array be joined from Vec::<Array<T>> directly without converting it to Vec::<&Array<T>>?

use arrayfire::*;
fn main() {
 let mut vs = Vec::<Array<i32>>::new();
 let mut v2s = Vec::<&Array<i32>>::new();
 for i in 0..10{
 let a = range::<i32>(dim4!(4), 0) * i;
 vs.push(a);
 }
 for i in 0..10{
 v2s.push(&vs[i]);
 }
 let v3 = join_many(1, v2s);
 print(&v3);
}
You must be logged in to vote

@3togo Perhaps join_many macro is what you are looking for ? It takes care of creating the vector out of Array references and passing it down to join function for you.

The reason most APIs of arrayfire-rust crate have Array& as arguments to enable reuse of the same memory for multiple operations. Otherwise, Array would be consumed by the first join_many and the original array can't be reused without a clone operation which is not free of cost always.

Replies: 1 comment 6 replies

Comment options

@3togo Perhaps join_many macro is what you are looking for ? It takes care of creating the vector out of Array references and passing it down to join function for you.

The reason most APIs of arrayfire-rust crate have Array& as arguments to enable reuse of the same memory for multiple operations. Otherwise, Array would be consumed by the first join_many and the original array can't be reused without a clone operation which is not free of cost always.

You must be logged in to vote
6 replies
Comment options

Not sure I follow what you intend to ? Can you show me a sample of syntax are looking for ?

Comment options

If I use join_many!, I could not enter the index as variable. I need to type something like below
let v3: Array = join_many![3;&vs[0],&vs[2],&vs[3]];
let v4: Array = join_many![3;v2s[0],v2s[2],v2s[3]];
It is ok as long as the len() is not very big. But if the len() is say 100, it is not practical to use join_many!

`use arrayfire::*;
fn main() {

let mut vs = Vec::<Array<i32>>::new();
let mut v2s = Vec::<&Array<i32>>::new();
for i in 0..10{
 let a = range::<i32>(dim4!(4), 0) * i;
 vs.push(a);
}
for i in 0..10{
 v2s.push(&vs[i]);
}
let v3: Array<i32> = join_many![3;&vs[0],&vs[2],&vs[3]];
let v4: Array<i32> = join_many![3;v2s[0],v2s[2],v2s[3]];
let v5 = join_many(1, v2s);
print(&v3);
print(&v4);
print(&v5);

}`

Comment options

Let's say you have 100 such Arrays and you want some random set among those to be joined. Wouldn't you have an array of indices of those arrays ? I would think they are in some form of vector or container. You would have to iterate over those indices to create another vector of Arrays and then pass that along to [join_many](https://arrayfire.org/arrayfire-rust/arrayfire/fn.join_many.html). I don't think it would be inefficient to iterate over 100 integers by any means, and also given that Array is light weight object and you will be storing references to existing Arrays, that shouldn't be inefficient either.

I hope I understood your scenario correctly.

Comment options

I end up creating a macro as below

#[macro_export]
macro_rules! join_many {
 ($dim:expr, $v:expr) => {{
 join_many($dim, $v.iter().collect_vec())
 }};
}
Comment options

I am not sure I why you would need such a macro. If you eventual usage wants to keep set of af::arrays in a vector and call join_many, you can do so by directly calling join_many function.

The join_many macro that the crate provides is to help in scenarios where user don't want to deal with the creation of vector to pass it down to join_many function.

If you already have a container, you might as well just call join_many without any macro right ?

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 によって変換されたページ (->オリジナル) /