[R] Matrix Operations

2010-11-27 Thread Romildo Martins
 Hello,

Given the matrices LSPs and services, need to generate the matrix
(mtraffic) as follows:

1. Browse all lines of LSPs
2. Collect the value of bw (matrix services) in accordance with id2
3. Sum bw in mtraffic (line = source) (column = destiny)

Thanks!
Romildo Martins



**
example01
**
 lsps
 id source destiny    id2  caminho order
1  1      4       2          5       0     0
2  2      6      10          6       0     0
3  3      2       6          6       0     0
4  4      2       6          8       0     0
 services
id2  bw prio pen fator order
1  1 128    5  32  0.25     0
2  2  64    5  16  0.25     0
3  3  64    5  64  1.00     0
4  4  16    5  16  1.00     0
5  5  32    1 192  6.00     0
6  6  64    1 512  8.00     0
7  7 512    3 128  0.25     0
8  8  32    7  32  1.00     0
 mtraffic
   2 4  6 10
2   0 0 96  0
4  32 0  0  0
6   0 0  0 64
10  0 0  0  0


***
example02
***
 lsps
 id source destiny     id2   caminho order
1  1      2       4          7       0     0
2  2      6      10          4       0     0
3  3      6       4          5       0     0
4  4      6       4          8       0     0
 services
id2  bw prio pen fator order
1  1 128    5  32  0.25     0
2  2  64    5  16  0.25     0
3  3  64    5  64  1.00     0
4  4  16    5  16  1.00     0
5  5  32    1 192  6.00     0
6  6  64    1 512  8.00     0
7  7 512    3 128  0.25     0
8  8  32    7  32  1.00     0
 mtraffic
    2   4   6 10
 2  0 512 0  0
 4  0   0  0  0
 6  0  64 0 16
10 0   0 0  0
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix Operations

2010-11-27 Thread Wu Gong

Hi Romildo,

Merge two table by id2 first, then reshape to the wide format, sum by source
at last. Hope it helps.

### Data simulation
lsp.text -  id source destiny id2   caminho order
1  1  2   4  7   0 0
2  2  6  10  4   0 0
3  3  6   4  5   0 0
4  4  6   4  8   0 0 
lsp - read.table(textConnection(lsp.text),header=TRUE)
services.text - id2  bw prio pen fator order
1  1 1285  32  0.25 0
2  2  645  16  0.25 0
3  3  645  64  1.00 0
4  4  165  16  1.00 0
5  5  321 192  6.00 0
6  6  641 512  8.00 0
7  7 5123 128  0.25 0
8  8  327  32  1.00 0 
services - read.table(textConnection(services.text),header=TRUE)

## Delete unwanted columns and combine two table into one table by id2
mtraffic.1  - merge(lsp[,c(2,3,4)],services[,c(1,2)],by=id2)

## Reshape table to wide style
library(reshape)
mtraffic.2 - cast(mtraffic.1, source+id2 ~ destiny,fill=0)

## Sum by category(source)
colnames(mtraffic.2) -
c(colnames(mtraffic.2)[1:2],paste(D,colnames(mtraffic.2)[-c(1:2)],sep=))
mtraffic - aggregate(. ~ source, data=mtraffic.2[,-2],FUN=sum)



-
A R learner.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Matrix-Operations-tp3061550p3061773.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Matrix operations

2010-06-28 Thread Dmitrij Kudriavcev
Hello

I have a quick question.

I need to compute matrix in R, like A - t(X) %*% solve(V) %*% X, where X is
a vector and V is a matrix

This code works, but now i want to optimize it. I have try:

A - crossprod(X, solve(V)) %*% X

Is there another, better way?

WBR
Dima

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix operations

2010-06-28 Thread Bill.Venables
Since X is a vector, then

A - sum(X, solve(V, X)) 

is probably slightly better here.

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Dmitrij Kudriavcev
Sent: Tuesday, 29 June 2010 12:29 PM
To: r-help@r-project.org
Subject: [R] Matrix operations

Hello

I have a quick question.

I need to compute matrix in R, like A - t(X) %*% solve(V) %*% X, where X is
a vector and V is a matrix

This code works, but now i want to optimize it. I have try:

A - crossprod(X, solve(V)) %*% X

Is there another, better way?

WBR
Dima

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix operations

2010-06-28 Thread Bill.Venables
Oops.  Try

A - sum(X * solve(V, X))

(too fast!) 

-Original Message-
From: Venables, Bill (CMIS, Cleveland) 
Sent: Tuesday, 29 June 2010 1:05 PM
To: 'Dmitrij Kudriavcev'; 'r-help@r-project.org'
Subject: RE: [R] Matrix operations

Since X is a vector, then

A - sum(X, solve(V, X)) 

is probably slightly better here.

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Dmitrij Kudriavcev
Sent: Tuesday, 29 June 2010 12:29 PM
To: r-help@r-project.org
Subject: [R] Matrix operations

Hello

I have a quick question.

I need to compute matrix in R, like A - t(X) %*% solve(V) %*% X, where X is
a vector and V is a matrix

This code works, but now i want to optimize it. I have try:

A - crossprod(X, solve(V)) %*% X

Is there another, better way?

WBR
Dima

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix operations

2010-06-28 Thread Dmitrij Kudriavcev
But isn't it change multiplication order?

WBR
Dima

2010/6/29 bill.venab...@csiro.au

 Since X is a vector, then

 A - sum(X, solve(V, X))

 is probably slightly better here.

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of Dmitrij Kudriavcev
 Sent: Tuesday, 29 June 2010 12:29 PM
 To: r-help@r-project.org
 Subject: [R] Matrix operations

 Hello

 I have a quick question.

 I need to compute matrix in R, like A - t(X) %*% solve(V) %*% X, where X
 is
 a vector and V is a matrix

 This code works, but now i want to optimize it. I have try:

 A - crossprod(X, solve(V)) %*% X

 Is there another, better way?

 WBR
 Dima

 [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] matrix operations on grobs and grid units

2009-09-19 Thread baptiste auguie
Dear list,

As a minimal test of a more complex grid layout, I'm trying to find a
clean and efficient way to arrange text grobs in a rectangular layout.
The labels may be expressions, or text with a fontsize different of
the default, which means that the cell sizes should probably be
calculated using grobWidth() and grobHeight() as opposed to simpler
stringWidth() and stringHeight(). (Correct?).

The input of this function is a vector of labels, which are arranged
into a matrix layout. Below is my current version, followed by a few
questions.

e = expression(alpha,testing very large width, hat(beta),
integral(f(x)*dx, a, b))

rowMax.units - function(u, nrow){ # rowMax with a fake matrix of units
 matrix.indices - matrix(seq_along(u), nrow=nrow)
 do.call(unit.c, lapply(seq(1, nrow), function(ii) {
  max(u[matrix.indices[ii, ]])
 }))
}

colMax.units - function(u, ncol){ # colMax with a fake matrix of units
 matrix.indices - matrix(seq_along(u), ncol=ncol)
 do.call(unit.c, lapply(seq(1, ncol), function(ii) {
  max(u[matrix.indices[, ii]])
 }))
}


makeTableGrobs - function(e, ncol, nrow,
   just = c(center, center),
   gpar.text = gpar(col=black, cex=1),
   gpar.fill = gpar(fill = grey95, col=white, lwd=1.5)) {

 n - length(e) # number of labels

stopifnot(!n%%2) # only rectangular layouts

 if(missing(ncol)  missing(nrow)){
 nm - n2mfrow(n)  # pretty default layout
 ncol = nm[1]
 nrow = nm[2]
}

makeOneLabel - function(label.ind){
textGrob(label=e[label.ind], gp=gpar.text,
name=paste(cells-label-,label.ind, sep=))
}

lg - lapply(seq_along(e), makeOneLabel) # list of grobs
wg - lapply(lg, grobWidth) # list of grob widths
hg - lapply(lg, grobHeight) # list of grob heights

widths.all - do.call(unit.c, wg)
heights.all - do.call(unit.c, hg)

widths - colMax.units(widths.all, ncol)
heights - rowMax.units(heights.all, nrow)

   gcells = frameGrob(name=table.cells, vp = cells,
   layout = grid.layout(nrow, ncol, just=just,
   widths = widths,
   heights = heights) )

   label.ind - 1 # index running for the vector of labels

   for (ii in seq(1, ncol, 1)) {
   for (jj in seq(1, nrow, 1)) {

   gcells = placeGrob(gcells, rectGrob(gp=gpar.fill,
name=paste(cells-fill-r,ii, -c,jj,sep=)),
row=jj, col=ii)

  text.grob.ij = textGrob(label=e[label.ind],
gp=gpar.text, name=paste(cells-label-r,ii,
-c,jj,sep=))

   gcells = placeGrob(gcells, text.grob.ij, row=jj, col=ii)

   label.ind - label.ind + 1
   }
   }

gList( gcells)
}

# tests
vp = viewport(name=cells)
grid.draw(gTree(children=makeTableGrobs(e), childrenvp=vp))
grid.newpage()
grid.draw(gTree(children=makeTableGrobs(e, 1, 4), childrenvp=vp))
grid.newpage()
grid.draw(gTree(children=makeTableGrobs(e, 4, 1), childrenvp=vp))

This works as expected, however I would like some advice before going
any further,

- because this layout seems quite common, would it make sense to
provide methods for the following objects? (i) a matrix of grobs; (ii)
a matrix of units; (iii) cbind, rbind, rowMax, colMax methods for a
matrix of units.

- is there a better, recommended way to achieve the same thing?
(examples would be great)

Any other comments are very welcome.

Best regards,

baptiste

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] matrix operations on grobs and grid units

2009-09-19 Thread baptiste auguie
A few amendments might make this improved code more readable,

e = expression(alpha,testing very large width, hat(beta),
integral(f(x)*dx, a, b))

library(grid)

rowMax.units - function(u, nrow){ # rowMax with a fake matrix of units
 matrix.indices - matrix(seq_along(u), nrow=nrow)
 do.call(unit.c, lapply(seq(1, nrow), function(ii) {
  max(u[matrix.indices[ii, ]])
 }))
}

colMax.units - function(u, ncol){ # colMax with a fake matrix of units
 matrix.indices - matrix(seq_along(u), ncol=ncol)
 do.call(unit.c, lapply(seq(1, ncol), function(ii) {
  max(u[matrix.indices[, ii]])
 }))
}


makeTableGrobs - function(e, ncol, nrow, equal.width = F, equal.height=F,
just = c(center, center),
       gpar.text = gpar(col=black, cex=1),
       gpar.fill = gpar(fill = grey95, col=white, lwd=1.5)) {

n - length(e) # number of labels

stopifnot(!n%%2) # only rectangular layouts

if(missing(ncol)  missing(nrow)){
nm - n2mfrow(n)      # pretty default layout
ncol = nm[1]
nrow = nm[2]
}

makeOneLabel - function(label.ind){
textGrob(label=e[label.ind], gp=gpar.text,
name=paste(cells-label-,label.ind, sep=))
}

makeOneCell - function(label.ind){
rectGrob(gp=gpar.fill, name=paste(cells-fill-,label.ind, sep=))
}

 lg - lapply(seq_along(e), makeOneLabel) # list of text grobs
 lf - lapply(seq_along(e), makeOneCell) # list of rect grobs

 wg - lapply(lg, grobWidth) # list of grob widths
 hg - lapply(lg, grobHeight) # list of grob heights

 widths.all - do.call(unit.c, wg) # all grob widths
 heights.all - do.call(unit.c, hg)    #all grob heights

 widths - colMax.units(widths.all, ncol) # all column widths
 heights - rowMax.units(heights.all, nrow) # all row heights

 if(equal.width)
   widths - rep(max(widths), length(widths))
 if(equal.height)
   heights - rep(max(heights), length(heights))

 gcells = frameGrob(name=table.cells, vp = cells,
   layout = grid.layout(nrow, ncol, just=just,
     widths = widths, heights = heights) )

 label.ind - 1   # index running accross labels

 for (ii in seq(1, ncol, 1)) {
   for (jj in seq(1, nrow, 1)) {

     gcells = placeGrob(gcells, lf[[label.ind]], row=jj, col=ii)
     gcells = placeGrob(gcells, lg[[label.ind]], row=jj, col=ii)

     label.ind - label.ind + 1
   }
 }

 gl = gList( gcells)

 gl
}

# tests
vp = viewport(name=cells)
g1 - gTree(children=makeTableGrobs(e), childrenvp=vp)
g2 - gTree(children=makeTableGrobs(e, 4, 1), childrenvp=vp)
g3 - gTree(children=makeTableGrobs(e, 1, 4), childrenvp=vp)
g4 - gTree(children=makeTableGrobs(e, equal.w=T), childrenvp=vp)
g5 - gTree(children=makeTableGrobs(e, equal.h=T), childrenvp=vp)
g6 - gTree(children=makeTableGrobs(e, equal.h=T, equal.w=T), childrenvp=vp)

source(http://gridextra.googlecode.com/svn-history/r21/trunk/R/arrange2.r;)
# wrapper around grid.layout and grid.draw
arrange2(g1, g2, g3, g4, g5, g6, main=Testing different fitting arrangements)


This works as expected, however I would like some advice before going
any further,

- because this layout seems quite common, would it make sense to
provide methods for the following objects? (i) a matrix of grobs; (ii)
a matrix of units; (iii) cbind, rbind, rowMax, colMax methods for a
matrix of units.

- is there a better, recommended way to achieve the same thing?
(examples would be great)

Any comments and suggestions are very welcome.

Best regards,

baptiste

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.