Hi
I want to write a little function that takes a vector of arbitrary
length "n" and returns a matrix of size n+1 by n+1.
I can't easily describe it, but the following function that works for
n=3 should convey what I'm trying to do:
f <- function(x){
matrix(c(
1 , 0 , 0 , 0,
x[1] , 1 , 0 , 0,
x[1]*x[2] , x[2] , 1 , 0,
x[1]*x[2]*x[3], x[2]*x[3], x[3], 1
),
4,4, byrow=T)
}
f(c(10,7,2))
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 10 1 0 0
[3,] 70 7 1 0
[4,] 140 14 2 1
>
As one goes down column "i", the entries get multiplied by successive
elements of x, starting with x[i], after the first "1"
As one goes along a row, one takes a product of the tail end of x,
until the zeroes kick in.
Am I missing some clever solution?
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
Maybe not elegant, but I guess this function does what you want:
f <- function( x ) {
n <- length( x ) + 1
m <- matrix( 0, nrow = n, ncol = n )
diag(m) <- 1
for( j in 1:( n-1 ) ) {
for( i in ( j+1 ):n ) {
m[ i, j ] <- prod( x[ j:(i-1) ] )
}
}
return( m )
}
Best wishes,
Arne
On Tuesday 12 July 2005 13:11, Robin Hankin wrote:> Hi
>
> I want to write a little function that takes a vector of arbitrary
> length "n" and returns a matrix of size n+1 by n+1.
>
> I can't easily describe it, but the following function that works for
> n=3 should convey what I'm trying to do:
>
>
> f <- function(x){
> matrix(c(
> 1 , 0 , 0 , 0,
> x[1] , 1 , 0 , 0,
> x[1]*x[2] , x[2] , 1 , 0,
> x[1]*x[2]*x[3], x[2]*x[3], x[3], 1
> ),
> 4,4, byrow=T)
> }
>
> f(c(10,7,2))
> [,1] [,2] [,3] [,4]
> [1,] 1 0 0 0
> [2,] 10 1 0 0
> [3,] 70 7 1 0
> [4,] 140 14 2 1
>
>
>
> As one goes down column "i", the entries get multiplied by
successive
> elements of x, starting with x[i], after the first "1"
>
> As one goes along a row, one takes a product of the tail end of x,
> until the zeroes kick in.
>
>
> Am I missing some clever solution?
>
>
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
> tel 023-8059-7743
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
--
Arne Henningsen
Department of Agricultural Economics
University of Kiel
Olshausenstr. 40
D-24098 Kiel (Germany)
Tel: +49-431-880 4445
Fax: +49-431-880 1397
ahenningsen at agric-econ.uni-kiel.de
http://www.uni-kiel.de/agrarpol/ahenningsen/
Robin Hankin wrote:
What about
foo <- function(a){
n <- length(a)
X <- diag(n+1)
X[lower.tri(X)] <- unlist(lapply(seq(n),
function(x) cumprod(c(1, a)[-seq(x)])))
X
}
foo(c(10,7,2))
Uwe Ligges
> Hi
>
> I want to write a little function that takes a vector of arbitrary
> length "n" and returns a matrix of size n+1 by n+1.
>
> I can't easily describe it, but the following function that works for
> n=3 should convey what I'm trying to do:
>
>
> f <- function(x){
> matrix(c(
> 1 , 0 , 0 , 0,
> x[1] , 1 , 0 , 0,
> x[1]*x[2] , x[2] , 1 , 0,
> x[1]*x[2]*x[3], x[2]*x[3], x[3], 1
> ),
> 4,4, byrow=T)
> }
>
> f(c(10,7,2))
> [,1] [,2] [,3] [,4]
> [1,] 1 0 0 0
> [2,] 10 1 0 0
> [3,] 70 7 1 0
> [4,] 140 14 2 1
> >
>
>
> As one goes down column "i", the entries get multiplied by
successive
> elements of x, starting with x[i], after the first "1"
>
> As one goes along a row, one takes a product of the tail end of x,
> until the zeroes kick in.
>
>
> Am I missing some clever solution?
>
>
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
> tel 023-8059-7743
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
On 7/12/05, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:> Hi > > I want to write a little function that takes a vector of arbitrary > length "n" and returns a matrix of size n+1 by n+1. > > I can't easily describe it, but the following function that works for > n=3 should convey what I'm trying to do: > > > f <- function(x){ > matrix(c( > 1 , 0 , 0 , 0, > x[1] , 1 , 0 , 0, > x[1]*x[2] , x[2] , 1 , 0, > x[1]*x[2]*x[3], x[2]*x[3], x[3], 1 > ), > 4,4, byrow=T) > } > > f(c(10,7,2)) > [,1] [,2] [,3] [,4] > [1,] 1 0 0 0 > [2,] 10 1 0 0 > [3,] 70 7 1 0 > [4,] 140 14 2 1 > > > > > As one goes down column "i", the entries get multiplied by successive > elements of x, starting with x[i], after the first "1" > > As one goes along a row, one takes a product of the tail end of x, > until the zeroes kick in.I have not checked this generally but at least for the 4x4 case its inverse is 0 except for 1s on the diagonal and -x on the subdiagonal. We can use diff on a diagonal matrix to give a matrix with a diagonal and superdiagonal and then massage that into the required form, invert and round -- leave off the rounding if the components of x are not known to be integer. round(solve(diag(4) - t(diff(diag(5))[,1:4])+diag(4) * c(0,x)))