Open Non-Contradiction opened 6 years ago
I just found that array
could be used instead of matrix
, and it allows generic code to go in. So maybe array
instead of Jmatrix
.
I wonder if we need to think of a pre-processor that will go through a code and look for the problem cases. Eventually we might be able to "translate" automatically. I can see a work process like:
To get
autodiffr
working in some cases, some new functions are created to use instead of the original ones. The issue is to keep record of these new functions and provide a space to discuss about them.cSums
,cMeans
,rSums
,rMeans
are created w.r.t the originalcolSums
,colMeans
,rowSums
,rowMeans
.These functions are equivalent to use of apply with FUN = mean or FUN = sum with appropriate margins, but are a lot faster. As they are written for speed, ....
So the only way to work with them seems to be defining new functions.na.rm
are not accepted.softmax <- function(x) sum(exp(x) / rSums(exp(x)))
diagm
are created to replace part ofdiag
.diag
working forjulia
matrices to extract the diagonal part, but it turns out to be quite hard to letdiag
working forjulia
vectors to create a matrix with the vector as diagonal. This is a general issue about matrix creation problem, and we can see similar problems appear multiple times.... And I think usingdiagm
anddiag
instead of onediag
is great and can avoid many potential issues.ncol
,nrow
andnames
.map
instead ofmapply
andsapply
.mapply
andsapply
are either internal-generic or non-generic at all, implementation a new function can really save the quirks in gettingmapply
orsapply
working, and can be more efficient and the usage are nearly the same asmapply
.mapply
accepts.function(x) sum(map(function(y) x[1] * y, x))
%m%
instead of%*%
.%*%
are only S4 generic but not S3 generic.%*%
in R.function(x) det(x[1,1] * solve(x %m% x) + x)
Jmatrix
instead ofmatrix(x, nrow, ncol)
.matrix
function is not generic in R, and I believe that the R implementation will have something like creation of a matrix, which is similar to the problem we have indiagm
.byrow
is also not accepted currently.function(x){a <- Jmatrix(x, length(x), 1); b <- Jmatrix(x, 1, length(x)); sum(log((1 + (a %m% b)) + a - b))}
Use
array(x, c(nrow, ncol))
instead ofmatrix(x, nrow, ncol))
array
is more friendly with generics thanmatrix
.mode
argument is ignored for now.function(x){ a <- array(x, c(length(x), 1)) b <- array(x, c(1, length(x))) sum(log((1 + (a %m% b)) + a - b)) }
zeros
andones
instead ofmatrix(0, nrow, ncol)
andmatrix(1, nrow, ncol)
.matrix
in R cannot create ajulia
matrix with the desired element type, which is quite a common practice in writingjulia
functions for the sake of automatic differentiation.x
as argument, should be able to accept more arguments to be really useful.function(x){y <- zeros(x); y[1] <- x[1]; y[1] <- y[1] * x[2]; y[2] <- y[2] * x[3]; y[3] <- sum(y); y}