IFFT function in R? - r

In Matlab, there is an ifft function (Inverse fast Fourier transform) - details.
In particular, the following:
ifft(X,n,dim)
Which returns the inverse DFT of X across the dimension dim.
In R, there is a similar function apart of the signal package - details
However it only allows for the x input array, as follows:
ifft(x)
Question:
Is there any way to include the extra dimension, such as dim in the Matlab function, with R?
Thank you so much for taking a look at my question, very helpful.

Are you looking for ? mvfft (with inverse = TRUE)?
mvfft does the (inverse) FFT by columns, but you can reshape your data:
t for matrix transpose
aperm for an array extension of t (dimension permutation)
you can reshape your array to a matrix by dim<-
(package arrayhelpers has convenience functions for such conversion of an array into a matrix and back).

Related

Functions that takes and returns vector in R

I want to create a function to help me convert a vector containing different values of Ghanaian Cedi to Hungarian Forint (1 cedi = 57.06 forint). My function name is Currency; such that if I give the function a vector [1,2,3,4],where1,2,3,4 represents cedi, the function will return me Currency(1), Currency(2), Currency(3) ,Currency(4), which are forints.
I was thinking of using loop to create my function. Before that, I would like to know if there's any easier way to separate the vector?
Vectors are a native data type in R. A numeric value is actually a numeric vector with one element as far as R is concerned.
In other words, R works directly with vectors making loops or apply functions rarely useful for trivial operations. Multiplying by a constant is a standard vector operation.
The function below handles a vector just fine:
convert_currency <- function(cedi) {
cedi * 57.06
}
convert_currency(1:4)
#> [1] 57.06 114.12 171.18 228.24

Opposite of array_tree in purrr package?

I was wondering if there is a function that is the opposite of the array_tree function from the purrr package.
Specifically I would like a function (lets call it list_untree -better suggestions welcome) that will do the following. If a is a multidimensional array and m is some vector specifying the margins for array_tree.
l <- array_tree(a, margin=m)
a <- list_untree(l, margin=m)
How can I write something like this efficiently if it does not already exist.
For More Motivation:
I am particularly interested in doing computations on l using the map function and then plugging the result into the list_untree function.

Handling matrices using Brobdingnag package

I need to build a matrix with extremely small entries.
So far I realized that the fastest way to define the kind of matrix that I need is:
Define a vectorized function of coordinates:
func = function(m,n){...}
Combine every possible coordinate using outer:
matrix = outer(1:100,1:100,FUN=func)
Having to deal with extremely small numbers I work in func's environment using brob numbers, its output will therefore be of the same type of a brob:
typeof(func(0:100,0:100) )
[1] "S4"
If I directly plug two vectors 0:100 in my function func it returns a vector of brobs but if I try to use it with outer I get the error:
Error in outer(1:100, 1:100, FUN = func) : invalid first argument
I suppose this is because package Brobdingnag can somehow deal with vectors but not with matrices. Is it right? Is there any way to make it work?

Julia: methods and DataArrays.DataArray

I would like to write a function fun1 with a DataArrays.DataArray y as unique argument. y can be either an integer or a float (in vector or in matrix form).
I have tried to follow the suggestions I have found in stackoverflow (Functions that take DataArrays and Arrays as arguments in Julia) and in the official documentation (http://docs.julialang.org/en/release-0.5/manual/methods/). However, I couldn't write a code enought flexible to deal with the uncertainty around y.
I would like to have something like (but capable of handling numerical DataArrays.DataArray):
function fun1(y::Number)
println(y);
end
Any suggestion?
One options can be to define:
fun1{T<:Number}(yvec::DataArray{T}) = foreach(println,yvec)
Then,
using DataArrays
v = DataArray(rand(10))
w = DataArray(rand(1:10,10))
fun1(v)
#
# elements of v printed as Flaot64s
#
fun1(w)
#
# elements of w printed as Ints
#
A delicate but recurring point to note is the invariance of Julia parametric types which necessitate defining a parametric function. A look at the documentation regarding types should clarify this concept (http://docs.julialang.org/en/release-0.4/manual/types/#types).

How to do a mathematical sum in R?

I have the following mathematical formula that I want to program as efficiently as possible in R.
$\sum_{i=1}^{N}(x_i-\bar x)(y_i-\bar y)$
Let's say we have the following example data:
x = c(1,5,7,10,11)
y = c(2,4,8,9,12)
How can I easily get this sum with this data without making a separate function?
Isn't there a package or a function that can compute these mathematical sums?
Use the sum command and vectorized operations: sum((x-mean(x))*(y-mean(y)))
The key revelation here is that the sum function is just taking the sum over the argument (vector, matrix, whatever). In this case, it's sufficient to give it a vector, and in this case, the vector expression is a little more complicated than sum(z), but notice that (x-mean(x))*(y-mean(y)) evaluates to z, so the fact that the command is slightly ornate doesn't really change how the function works. This is true in many places, not just the sum command.

Resources