next up previous
Next: util.blast Up: CHNOSZ examples Previous: util.args

util.array

utl.rr>   # start with a matrix
utl.rr>   x <- matrix(1:12,ncol=3)

utl.rr>   # pay attention to the following when
utl.rr>   # writing examples that test for identity!
utl.rr>   identical(1*x,x)   # FALSE
[1] FALSE

utl.rr>   # create two matrices that are multiples of the first
utl.rr>   a <- 1*x

utl.rr>   b <- 2*a

utl.rr>   # these both have two dimensions of lengths 4 and 3

utl.rr>   dim(a)  # 4 3
[1] 4 3

utl.rr>   # combine them to make an array with three dimensions
utl.rr>   c <- list2array(list(a,b))

utl.rr>   # the third dimension has length 2
utl.rr>   dim(c)  # 4 3 2
[1] 4 3 2

utl.rr>   # the first slice of the third dimension == a
utl.rr>   stopifnot(identical( slice(c,3), a ))

utl.rr>   # the second slice of the third dimension == b
utl.rr>   stopifnot(identical( slice(c,3,2), b ))

utl.rr>   # 'slice' works just like the bracket operator
utl.rr>   c11 <- slice(c,1)

utl.rr>   c12 <- slice(c,1,2)

utl.rr>   c21 <- slice(c,2,1)

utl.rr>   c212 <- slice(c,2,1:2)

utl.rr>   stopifnot(identical( c11, c[1,,] ))

utl.rr>   stopifnot(identical( c12, c[2,,] ))

utl.rr>   stopifnot(identical( c21, c[,1,] ))

utl.rr>   stopifnot(identical( c212, c[,1:2,] ))

utl.rr>   # let us replace part of the array
utl.rr>   d <- slice(c,3,2,value=a)

utl.rr>   # now the second slice of the third dimension == a
utl.rr>   stopifnot(identical( slice(d,3,2), a ))

utl.rr>   # and the sum across the third dimension == b
utl.rr>   stopifnot(identical( dimSums(d,3), b ))

utl.rr>   # taking the sum removes that dimension
utl.rr>   dim(d)             # 4 3 2
[1] 4 3 2

utl.rr>   dim(dimSums(d,1))  # 3 2
[1] 3 2

utl.rr>   dim(dimSums(d,2))  # 4 2
[1] 4 2

utl.rr>   dim(dimSums(d,3))  # 4 3
[1] 4 3

utl.rr>   # working with an 'affinity' object
utl.rr>   ## Don't show: 
utl.rr> data(thermo)
thermo$obigt has 1800 aqueous, 2925 total species

utl.rr> ## End Don't show
utl.rr>   basis("CHNOS+")
    C H N O S Z ispecies logact state
CO2 1 0 0 2 0 0       69     -3    aq
H2O 0 2 0 1 0 0        1      0   liq
NH3 0 3 1 0 0 0       68     -4    aq
H2S 0 2 0 0 1 0       70     -7    aq
O2  0 0 0 2 0 0     2691    -80   gas
H+  0 1 0 0 0 1        3     -7    aq

utl.rr>   species("alanine")
  CO2 H2O NH3 H2S O2 H+ ispecies logact state    name
1   3   2   1   0 -3  0     1504     -3    aq alanine

utl.rr>   a1 <- affinity(O2=c(-80,-60))   # at pH=7
affinity: temperature is 25 C
energy.args: pressure is Psat
energy.args: variable 1 is O2 at 128 increments from -80 to -60 
subcrt: 7 species at 298.15 K and 1 bar (wet) 

utl.rr>   a2 <- affinity(O2=c(-80,-60),pH=c(0,14,7))
affinity: temperature is 25 C
energy.args: pressure is Psat
energy.args: variable 1 is O2 at 128 increments from -80 to -60 
energy.args: variable 2 is pH at 7 increments from 0 to 14 
subcrt: 7 species at 298.15 K and 1 bar (wet) 

utl.rr>   # in the 2nd dimension (pH) get the 4th slice (pH=7)
utl.rr>   a3 <- slice.affinity(a2,2,4)

utl.rr>   stopifnot(all.equal(a1$values,a3$values))


next up previous
Next: util.blast Up: CHNOSZ examples Previous: util.args