Skip to content

list of operations that preserve attributes

2 messages · John C Nash, Brian Ripley

#
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
#
There is a description in the 'Blue Book' (Becker et al, 1988) of when 
attributes are supposed to be preserved, and R mostly conforms to it.

This is discussed somewhere relevant in the R documentation:  I would 
have to search for exactly where, so I leave that to you.
On Wed, 19 Oct 2011, John C Nash wrote: