From maechler at stat.math.ethz.ch  Fri Aug 26 18:11:02 2011
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Fri, 26 Aug 2011 18:11:02 +0200
Subject: [Rd] matrix bands
In-Reply-To: <4E579AB3.8040305@dmu.dk>
References: <4E57823F.8010407@dmu.dk>
	<20055.36019.732209.391727@stat.math.ethz.ch>
	<4E579AB3.8040305@dmu.dk>
Message-ID: <20055.50582.165120.831467@stat.math.ethz.ch>

>>>>> Jeremy David Silver <jds at dmu.dk>
>>>>>     on Fri, 26 Aug 2011 15:08:03 +0200 writes:

    > Thanks for the suggestion, Martin!  I looked at both
    > Matrix::band and Matrix::bandSparse. Maybe I misunderstand the
    > help pages and the examples, but from what I can see neither of
    > them provides the functionality I was looking for.

    > For the getter version of the function I was looking for, I
    > can't use Matrix::band (by extracting the non-zero elements)
    > because this would require the assumption that the specified
    > band is entirely non-zero, e.g.:

>     band3 <- function(x,n){
>        x <- Matrix::band(x,n,n)
>        x[x!=0] ## works for a dense matrix, but not if band n has zeroes
>     }

> As for the setter version of the function I was looking for, it should 
> set the values of a specified band in an existing matrix to be the 
> vector of values provided. As far as I understand, Matrix::bandSparse 
> constructs a matrix rather than modifying the values of an existing matrix.

Yes, you are right.  The current bandSparse() doesn't do that...
and while you could use constructions like  
    diag(A[,-1]) <- dd
there will probably better (more efficient) alternatives.

> The functions included in my last post filled this gap. If Matrix::band 
> and Matrix::bandSparse can set/get bands in this way, I would like to 
> see how this can be done. If not, then perhaps they could be extended to 
> achieve this functionality.

That's a good suggestion, and I'll look at it,
further I'll look at your examples of "getting" ... 
and at the moment I'm still a bit curious why, i.e., to what end
/ in what application you need them [ rather than  band(*,.,.) ].
We may e-talk about this off-public if you want.

Martin


> On 2011-08-26 14:08, Martin Maechler wrote:
> >>>>>> Jeremy David Silver<jds at dmu.dk>
> >>>>>>      on Fri, 26 Aug 2011 13:23:43 +0200 writes:
> >      >  Dear R developers, I was looking for a function analogous
> >      >  to base::diag() for getting and setting bands of a
> >      >  matrix. The closest I could find was Matrix::band(), but
> >      >  this was not exactly what I wanted for two
> >      >  reasons. Firstly, Matrix::band() returns a matrix rather
> >      >  than just the specified band.  Secondly, Matrix::band()
> >      >  cannot be used for setting the values for a matrix band.
> >
> > Yes, but did you not look at  help(band)
> > or not look carefully enough ?
> >
> > --->
> >
> >    See Also:
> >
> >         ?bandSparse? for the _construction_ of a banded sparse matrix
> >         directly from its non-zero diagonals.
> >
> >
> >
> >      >  Setting or getting a matrix band could be of interest for
> >      >  sufficiently many users that you might consider including
> >      >  it in base R.  Alternatively, something like this could be
> >      >  included in the Matrix package.
> >
> > well, see above and let us know if you see anything lacking in
> > bandSparse().
> > Till now we haven't got much feedback about it, and there may
> > well be room for improvement.
> >
> > Martin Maechler, ETH Zurich  and co- maintainer("Matrix")



[Rd] matrix bands
Martin Maechler