Perform operation on subset of vector in rust? - vector

I'm very new to Rust, and I come from C++ land. I'm trying to use the experimental Vec::partition_at_index function. I'm trying to call this function on a certain range of indices of my vector, but have it still modify the original vector (I'm implementing a version of quicksort). Is there a way this can be done?
I also noticed Iterator::partition_in_place. Is this more what I should going for? Can the iterator version be used to operate on a subset of values?
If there are C++ folks hanging out here, I'm looking for the behavior of std::partition, which can operate on an iterator range.

Related

How do I declare an array in R?

I'm trying to declare an array in R, something logically equivalent to the following Java code:
Object[][] array = new Object[6][32]
After I declare this array, I plan to loop over the indices and assign values to them.
I am not familiar with what you are planning on doing in R, but loops are generally not recommended. I would say this is especially true when you don't know the length of the output.
You might want to find a "vectorized" solution first and if not, then using something in the apply family might also be helpful
Disclaimer: I am certain there is more nuance to this discussion based on what I have read, so I don't want to claim to be an expert on this subject.

Replace for loop with vectorized call of a function returning multiple values

I have the following function: problema_firma_emprestimo(r,w,r_emprestimo,posicao,posicao_banco), where all input are scalars.
This function return three different matrix, using
return demanda_k_emprestimo,demanda_l_emprestimo,lucro_emprestimo
I need to run this function for a series of values of posicao_banco that are stored in a vector.
I'm doing this using a for loop, because I need three separate matrix with each of them storing one of the three outputs of the function, and the first dimension of each matrix corresponds to the index of posicao_banco. My code for this part is:
demanda_k_emprestimo = zeros(num_bancos,na,ny);
demanda_l_emprestimo = similar(demanda_k_emprestimo);
lucro_emprestimo = similar(demanda_k_emprestimo);
for i in eachindex(posicao_bancos)
demanda_k_emprestimo[i,:,:] , demanda_l_emprestimo[i,:,:] , lucro_emprestimo[i,:,:] = problema_firma_emprestimo(r,w,r_emprestimo[i],posicao,posicao_bancos[i]);
end
Is there a fast and clean way of doing this using vectorized functions? Something like problema_firma_emprestimo.(r,w,r_emprestimo[i],posicao,posicao_bancos) ? When I do this, I got a tuple with the result, but I can't find a good way of unpacking the answer.
Thanks!
Unfortunately, it's not easy to use broadcasting here, since then you will end up with output that is an array of tuples, instead of a tuple of arrays. I think a loop is a very good approach, and has no performance penalty compared to broadcasting.
I would suggest, however, that you organize your output array dimensions differently, so that i indexes into the last dimension instead of the first:
for i in eachindex(posicao_bancos)
demanda_k_emprestimo[:, :, i] , ...
end
This is because Julia arrays are column major, and this way the output values are filled into the output arrays in the most efficient way. You could also consider making the output arrays into vectors of matrices, instead of 3D arrays.
On a side note: since you are (or should be) creating an MWE for the sake of the people answering, it would be better if you used shorter and less confusing variable names. In particular for people who don't understand Portuguese (I'm guessing), your variable names are super long, confusing and make the code visually dense. Telling the difference between demanda_k_emprestimo and demanda_l_emprestimo at a glance is hard. The meaning of the variables are not important either, so it's better to just call them A and B or X and Y, and the functions foo or something.

Is a STL vector pf pointers to vectors my best option?

I currently have a ton of vectors all set to have 1200 items, which is overkill, but could be used. So I don't have to recode a lot of stuff, what is the best way to create and iterate through a list of these vectors and resize them all as needed? (they all are equal in size)
One of my options is to create a pointer (after the fact) to each vector, and then create a vector of these pointers which can be iterated to resize. Another option is to create the vectors in the first place as pointers instead of objects. This seems like it would be a lot of work, and I currently have a lot of code where I use the vector objects.
Are there other options?
Fred E.
In C++, std::vector is just a wrapper around a raw data pointer. The vector itself stores data in the heap. So you can store vectors in a vector as objects, or you can store pointers to them.
The easiest way is to store vectors in a vector as objects, but you must carefully monitor how you pass this outer vector into functions. You should always pass it by reference or pass a pointer to it, so as not to trigger copying all nested vectors. On the other hand, such a vector will automatically and correctly remove nested vectors when its destructor fires.
The vector of pointers to vectors has its advantages and disadvantages. On the one hand, when a function is called inaccurately with this vector as a parameter, only pointers will be copied. On the other hand, accessing vectors through a pointer affects performance. In addition, when storing pointers to vectors, you must take care to delete them yourself.
If your vectors are created and deleted elsewhere and you only need to iterate through them and modify them in your code, then obviously the vector of pointers to vectors is your best choice.

Can we use apply function along with some user defined function or if/while loops in R to conditionally work it on selective rows?

I know that while and if functions in R are not vectorised. while and if functions help us selectively work on some rows based on some condition. I also know that the apply function in R is used to apply over the columns and hence it operates on all rows of columns that we wish to put apply on. Can I use apply() along with user defined functions and/or with while/if loop to conditionally use it over some rows rather than all rows as apply function usually does.
Note :- This core issue here is to bypass the drawback on non-vectorization of while/if loops in R.
You can supply user defined functions to apply using an argument FUN = function(x) user_defined_function(x) {}. And apply is "vectorized" in sense that as argument it accept vectors, not scalars (but its implementation is heavily using for and if loops, type apply without arguments in your console). So for and apply are of the same perfomance.
However you can break the execution of user defined function throwing exception with stop and wrapping in tryCatch it is a non-recommended technique (it influences environements, call stacks, scopes etc., make debugging difficult and lead to errors which are difficult to identify).
Better to use for and if and very often it is the most easiest and effective way (to write a recursive function, taking in consideration that (tail) recursion is not really optimized for R, or fully refactor your algorithm quite difficult and time consuming).

What is the Julia's best approximation to R objects' attributes?

I store important metadata in R objects as attributes. I want to migrate my workflow to Julia and I am looking for a way to represent at least temporarily the attributes as something accessible by Julia. Then I can start thinking about extending the RData package to fill this data structure with actual objects' attributes.
I understand, that annotating with things like label or unit in DataFrame - I think the most important use for object' attributes - is probably going to be implemented in the DataFrames package some time (https://github.com/JuliaData/DataFrames.jl/issues/35). But I am asking about about more general solution, that doesn't depend on this specific use case.
For anyone interested, here is a related discussion in the RData package
In Julia it is ideomatic to define your own types - you'd simply make fields in the type to store the attributes. In R, the nice thing about storing things as attributes is that they don't affect how the type dispatches - e.g. adding metadata to a Vector doesn't make it stop behaving like a Vector. In julia, that approach is a little more complicated - you'd have to define the AbstractVector interface for your type https://docs.julialang.org/en/latest/manual/interfaces/#man-interface-array-1 to have it behave like a Vector.
In essence, this means that the workflow solutions are a little different - e.g. often the attribute metadata in R is used to associate metadata to an object when it's returned from a function. An easy way to do something similar in Julia is to have the function return a tuple and assign the result to a tuple:
function ex()
res = rand(5)
met = "uniformly distributed random numbers"
res, met
end
result, metadata = ex()
I don't think there are plans to implement attributes like in R.

Resources