[Rd] list of operations that preserve attributes

John C Nash nashjc at uottawa.ca
Wed Oct 19 21:42:11 CEST 2011


I've been trying to prepare some wrappers for optimization objective functions and
gradients that use try() to check for computable functions. I'm also trying to do scaling
as per optim() so that a number of methods that are available on CRAN and R-Forge can use
parameter and function scaling. This got me into minor trouble when I scaled a gradient or
Hessian and the "inadmissible" attribute I had created suddenly disappeared. When I
discovered why -- that some operations don't preserve attributes -- I started to look for
a list of which ops preserve attributes and which don't. The code snippet below shows that
matrix multiplication (%*%) does not, while multiplication by a vector (simple *) does.
I'm not really surprised that some ops don't preserve attributes, particularly those that
are binary like multiplication, but it might be nice to know which operations and special
functions do so.

rm(list=ls())
m<-matrix(1:4,nrow=2, ncol=2)
print(m)
attributes(m)
attr(m,"check")<-"***"
attributes(m)
bigm<-10*m
str(bigm)
bigm1<-diag(c(1,1))%*%m
str(bigm1)
bigm1<-c(1,2)*m
str(bigm1)
print(bigm1)
arraym<-as.array(m)
str(arraym)
tanm<-tan(m)
str(tanm)


Best,

John Nash



More information about the R-devel mailing list