How to use formula to calculate sum of sequence in R? - r

I'm wonder is there already some forumla function ready to ues, for example?
sum(seq(10)) = 55
It could use math formula by n to calculate for faster response. But I don't know what's the proper keyword to found out is there builtin formula or already exist in R reposotory?

Related

Julia - exponentiating a matrix returned from another function

I have functions f1 and f2 returning matrices m1 and m2, which are calculated using Diagonal, Tridiagonal, SymTridiagonal from LinearAlgebra package.
In a new function f3 I try computing
j = m1 - m2*im
m3 = exp(j)
but this gives a Method error on computation unless I use j=Matrix(m1-m2*im), saying that no matching method for exp(::LinearAlgebra.Tridiagonal ...)
My question is how can I do this computation in the most optimal way? I am a total beginner in Julia.
Unless you have a very special structure of j (i.e. if its exponential is sparse - which is unlikely) the best you can do AFAICT is to use a dense matrix as an input to exp:
m3 = LinearAlgebra.exp!([float(x) for x in Tridiagonal(dl, d, du)])
If you expect m3 to be sparse then I think currently there is no special algorithm implemented for that case in Julia.
Note that I use exp! to do operation in place and use a comprehension to make sure the argument to exp! is dense. As exp! expects LinearAlgebra.BlasFloat (that is Union{Complex{Float32}, Complex{Float64}, Float32, Float64}) I use float to make sure that elements of j are appropriately converted. Note that it might fail if you work with e.g. BigFloat or Float16 values - in this case you have to do an appropriate conversion to the expected types yourself.

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.

plot solution of fsolve() of parametric function

I have a function f(v,u) and I defined function
solutionf(u) := fsolve(f(v,u)=v);
I need to plot solutionf(u) depending on u but just
plot(solutionf(u), u = 0 .. 0.4e-1)
gives me an error
Error, (in fsolve) number of equations, 1, does not match number of variables, 2
However I can always take the value solutionf(x) at any x.
Is there simple way to plot this? Or I have to make own for loop over u, take value at every point and plot interploating values?
This is one of the most-often-asked Maple questions. Your error is caused by what is known as premature evaluation, the expression solutionf(u) being evaluated before u has been given a numeric value.
There are several ways to avoid premature evaluation. The simplest is probably to use forward single quotes:
plot('solutionf(u)', u= 0..0.4e-1);

IFFT function in 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).

Resources