1setMethod('diag', 'bdsmatrix', function(x, nrow, ncol) {
2    if (class(x) != 'bdsmatrix') stop("Argument must be a bdsmatrix object")
3
4    d <- x@Dim
5    d3 <- sum(x@blocksize)
6    temp <- .C(Cbdsmatrix_index1,
7	       as.integer(length(x@blocksize)),
8	       as.integer(x@blocksize),
9	       as.integer(c(0,1,0)),
10	       as.integer(d3),
11	       as.integer(1:d3 -1),
12	       integer(1),
13	       indexb = integer(d3),
14	       integer(1))$indexb
15
16    if (length(x@rmat) > 0) {
17	temp2 <- seq(from=d3+1, by= d[2]+1, length= d[1] - d3)
18	c(x@blocks[temp], x@rmat[temp2])
19	}
20    else x@blocks[temp]
21    })
22
23setMethod("diag<-","bdsmatrix" ,function(x, value) {
24    if (class(x) != 'bdsmatrix') stop("Argument must be a bdsmatrix object")
25
26    d <- x@Dim
27    if (length(value) != d[1]) stop("Wrong length for diagonal")
28    d3 <- sum(x@blocksize)
29    temp <- .C(Cbdsmatrix_index1,
30	       as.integer(length(x@blocksize)),
31	       as.integer(x@blocksize),
32	       as.integer(c(0,1,0)),
33	       as.integer(d3),
34	       as.integer(1:d3 -1),
35	       integer(1),
36	       indexb = integer(d3),
37	       integer(1))$indexb
38    x@blocks[temp] <- value[1:d3]
39    if (length(x@rmat) > 0) {
40	temp2 <- seq(from=d3+1, by= d[2]+1, length= d[1] - d3)
41	x@rmat[temp2] <- value[-(1:d3)]
42	}
43    x
44    })
45
46