Julia - Understanding LinearMaps example - julia

I am trying to wrap my head around the LinearMaps.jl package. In their documentation they have only two examples and I am stuck at the first one.
# Define a linear map from a matrix and one from a function (?)
A = LinearMap(rand(10, 10))
B = LinearMap(cumsum, reverse∘cumsum∘reverse, 10)
# Possible operations
3.0A + 2B
A + I
A*B'
[A B; B A]
kron(A, B)
How does LinearMap() work when its argument(s) are functions? For instance the cumsum() function doesn't work on its own as it requires the dims argument, so I am completely puzzled as to what this is doing.

Related

Is there a way to differentiate a function containing a sum or a product?

I have the following equation:
and I'm trying to generate the analytic derivative .
I know you can use deriv() and D() for an expression , but I cannot seem to figure out how to actually implement a sum or a product notation into an expression.
partial/incomplete answer
The Deriv package offers a more robust (and extensible) alternative to the base R D and deriv functions, and appears to know about sum() already. prod() will be difficult, though (see below).
A very simple example:
library(Deriv)
Deriv(~ sum(b*x), "b")
## sum(x)
A slightly more complex answer that sort-of works:
Deriv(~ sum(rep(a, length(x)) + b*x), c("a","b"))
## c(a = sum(rep(x = 1, length(x))), b = sum(x))
Note here that sum(a+b*x) doesn't work (returns 1) for the derivative with respect to a, for reasons described in ?Deriv (search for "rep(" in the page): the rep() is needed to help Deriv sort out scalar/vector definitions. It's too bad that it can't simplify sum(rep(x=1, length(x))) to length(x) but ...
Trying
Deriv( ~ exp(sum(a+b*x))/prod(1+exp(a+b*x)))
gives an error
Could not retrieve body of 'prod()'
You might be able to add a rule for products to the derivatives table, but it will be tricky since prod() takes a ... argument. Let's try defining our own function Prod() which takes a single argument x (I think this is the right generalization of the product rule but didn't think about it too carefully.)
Prod <- function(x) product(x)
drule[["Prod"]] <- alist(for(i in 1:length(x)) { .dx[i]*Prod(x[-i]) })
Deriv(~Prod(beta*x), "x"))
Unsurprisingly (to me), this doesn't work: the result is 0 ... (the basic problem is that using .dx[i] to denote the derivative of x[i] doesn't work in the machinery).
I don't know of a way to solve this in R; if I had this problem (depending on more context, which I don't know), I might see if I could find a framework for automatic differentiation (rather than symbolic differentiation). Unfortunately most of the existing tools for autodiff in R use backends in C++ or Julia (e.g. see here (C++ + Rcpp + CppAD), here (Julia), the TMB package (C++/CppAD/user-friendly extensions). There's an ancient pure-R github project radx but it looks too incomplete to use ... (FWIW autodiffr requires a Julia installation but doesn't actually require you to write any Julia code, AFAICS ...)

Cumulative Integration Options With Julia

I have two 1-D arrays in which I would like to calculate the approximate cumulative integral of 1 array with respect to the scalar spacing specified by the 2nd array. MATLAB has a function called cumtrapz that handles this scenario. Is there something similar that I can try within Julia to accomplish the same thing?
The expected result is another 1-D array with the integral calculated for each element.
There is a numerical integration package for Julia (see the link) that defines cumul_integrate(X, Y) and uses the trapezoidal rule by default.
If this package didn't exist, though, you could easily write the function yourself and have a very efficient implementation out of the box because the loop does not come with a performance penalty.
Edit: Added an #assert to check matching vector dimensions and fixed a typo.
function cumtrapz(X::T, Y::T) where {T <: AbstractVector}
# Check matching vector length
#assert length(X) == length(Y)
# Initialize Output
out = similar(X)
out[1] = 0
# Iterate over arrays
for i in 2:length(X)
out[i] = out[i-1] + 0.5*(X[i] - X[i-1])*(Y[i] + Y[i-1])
end
# Return output
out
end

parameter passing mechanism in R

The following function is used to multiply a sequence 1:x by y
f1<-function(x,y){return (lapply(1:x, function(a,b) b*a, b=y))}
Looks like a is used to represent the element in the sequence 1:x, but I do not know how to understand this parameter passing mechanism. In other OO languages, like Java or C++, there have call by reference or call by value.
Short answer: R is call by value. Long answer: it can do both.
Call By Value, Lazy Evaluation, and Scoping
You'll want to read through: the R language definition for more details.
R mostly uses call by value but this is complicated by its lazy evaluation:
So you can have a function:
f <- function(x, y) {
x * 3
}
If you pass in two big matrixes to x and y, only x will be copied into the callee environment of f, because y is never used.
But you can also access variables in parent environments of f:
y <- 5
f <- function(x) {
x * y
}
f(3) # 15
Or even:
y <- 5
f <- function() {
x <- 3
g <- function() {
x * y
}
}
f() # returns function g()
f()() # returns 15
Call By Reference
There are two ways for doing call by reference in R that I know of.
One is by using Reference Classes, one of the three object oriented paradigms of R (see also: Advanced R programming: Object Oriented Field Guide)
The other is to use the bigmemory and bigmatrix packages (see The bigmemory project). This allows you to create matrices in memory (underlying data is stored in C), returning a pointer to the R session. This allows you to do fun things like accessing the same matrix from multiple R sessions.
To multiply a vector x by a constant y just do
x * y
The (some prefix)apply functions works very similar to each other, you want to map a function to every element of your vector, list, matrix and so on:
x = 1:10
x.squared = sapply(x, function(elem)elem * elem)
print(x.squared)
[1] 1 4 9 16 25 36 49 64 81 100
It gets better with matrices and data frames because you can now apply a function over all rows or columns, and collect the output. Like this:
m = matrix(1:9, ncol = 3)
# The 1 below means apply over rows, 2 would mean apply over cols
row.sums = apply(m, 1, function(some.row) sum(some.row))
print(row.sums)
[1] 12 15 18
If you're looking for a simple way to multiply a sequence by a constant, definitely use #Fernando's answer or something similar. I'm assuming you're just trying to determine how parameters are being passed in this code.
lapply calls its second argument (in your case function(a, b) b*a) with each of the values of its first argument 1, 2, ..., x. Those values will be passed as the first parameter to the second argument (so, in your case, they will be argument a).
Any additional parameters to lapply after the first two, in your case b=y, are passed to the function by name. So if you called your inner function fxn, then your invocation of lapply is making calls like fxn(1, b=4), fxn(2, b=4), .... The parameters are passed by value.
You should read the help of lapply to understand how it works. Read this excellent answer to get and a good explanation of different xxpply family functions.
From the help of laapply:
lapply(X, FUN, ...)
Here FUN is applied to each elementof X and ... refer to:
... optional arguments to FUN.
Since FUN has an optional argument b, We replace the ... by , b=y.
You can see it as a syntax sugar and to emphasize the fact that argument b is optional comparing to argument a. If the 2 arguments are symmetric maybe it is better to use mapply.

Passing constant arguments to a multidimensional rootfinder in Sage

I have a five-dimensional rootfinding problem I'd like to solve from within a Sage notebook, but the functions I wish to solve depend on other parameters that shouldn't be varied during the rootfinding. Figuring out how to set up a call to, say, scipy.optimize.newton_krylov has got me stumped. So let's say I have (with a,b,c,d,e the parameters I want to vary, F1,F2,F3,F4,F5 the five expressions I which to solve to be equal to F1Val,F2Val,F3Val,F4Val,F5Val, values I already know, and posVal another known parameter)
def func(a, b, c, d, e, F1Val, F2Val, F3Val, F4Val, F5Val, posVal):
F1.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F2.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F3.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F4.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F5.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
return (F1-F1Val, F2-F2Val, F3-F3Val, F4-F4Val, F5-F5Val)
and now I want to pass this to a rootfinding function to yield func = (0,0,0,0,0). I want to pass an initial guess (a0, b0, c0, d0, e0) vector and a set of arguments (F1Val, F2Val, F3Val, F4Val, F5Val, posVal) for the evaluation, but I can't figure out how to do this. Is there a standard technique for this sort of thing? The multidimensional rootfinders in scipy seem to be lacking the args=() variable that the 1D rootfinders offer.
Best,
-user2275987
Well, I'm still not sure how to actually employ the Newton-Raphson method here, but using fsolve works, for functions that accept a vector of variables and a vector of constant arguments. I'm reproducing my proof of concept here
def tstfunc(xIn, constIn):
x = xIn[0]
y = xIn[1]
a = constIn[0]
b = constIn[1]
out = [x+2*y+a]
out.append(a*x*y+b)
return out
from scipy.optimize import fsolve
ans = fsolve(tstfunc, x0=[1,1], args=[0.3, 2.1])
print ans

Manipulating expressions in R

I am looking for a way to create an expression that is the product of two given expressions. For example, suppose I have
e1 <- expression(a+b*x)
e2 <- expression(c+d*x)
Now I want to create programatically the expression (e1)*(e2):
expression((a+b*x)*(c+d*x))
Background
I am writing a model fitting function. The model has two pieces that are user-defined. I need to be able to "handle" them separately, and then create a combined expression and "handle" it as one model. "Handling" involves taking numeric derivatives, and the deriv function wants expressions as an input.
I don't deal with this too often but something like this seems to be working
e1 <- expression(a + b*x)
e2 <- expression(c + d*x)
substitute(expression(e1*e2), list(e1 = e1[[1]], e2 = e2[[1]]))
# expression((a + b * x) * (c + d * x))
Try this:
e1 <- quote(a+b*x) # or expression(*)[[1]]
e2 <- quote(c+d*x)
substitute(e1 * e2, list(e1=e1, e2=e2))
It's probably overkill in your case, but the Ryacas package can be nice for performing more complicated symbolic manipulations of this sort:
library(Ryacas)
yacas(expression(e1*e2))$text
# expression((a + b * x) * (c + d * x))
Also, instead of using substitute(), you can construct the same expression in base R like this:
as.expression(as.call(list(as.symbol("*"), e1[[1]], e2[[1]])))
# expression((a + b * x) * (c + d * x))
Explanatory note: One initially confusing aspect of dealing with expression objects is that they are really lists of language objects -- even when (as is often the case) those lists contain just one object. For example, in your question, both e1 and e2 are length 1 lists containing a single call object each.
Working from the inside out, the code above:
Extracts the two call objects using [[1]]
Uses as.call() to constructs a new call that is the product of the two call objects.
Finally, wraps the resultant call back up as the expression object that you want.

Resources