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

util.array

# start with a matrix x <- matrix(1:12,ncol=3) # pay attention to the following when # writing examples that test for identity! identical(1*x,x) # FALSE
[1] FALSE
# create two matrices that are multiples of the first a <- 1*x b <- 2*a # these both have two dimensions of lengths 4 and 3 dim(a) # 4 3
[1] 4 3
# combine them to make an array with three dimensions c <- list2array(list(a,b)) # the third dimension has length 2 dim(c) # 4 3 2
[1] 4 3 2
# the first slice of the third dimension == a stopifnot(identical( slice(c,3), a )) # the second slice of the third dimension == b stopifnot(identical( slice(c,3,2), b )) # 'slice' works just like the bracket operator c11 <- slice(c,1) c12 <- slice(c,1,2) c21 <- slice(c,2,1) c212 <- slice(c,2,1:2) stopifnot(identical( c11, c[1,,] )) stopifnot(identical( c12, c[2,,] )) stopifnot(identical( c21, c[,1,] )) stopifnot(identical( c212, c[,1:2,] )) # let us replace part of the array d <- slice(c,3,2,value=a) # now the second slice of the third dimension == a stopifnot(identical( slice(d,3,2), a )) # and the sum across the third dimension == b stopifnot(identical( dimSums(d,3), b )) # taking the sum removes that dimension dim(d) # 4 3 2
[1] 4 3 2
dim(dimSums(d,1)) # 3 2
[1] 3 2
dim(dimSums(d,2)) # 4 2
[1] 4 2
dim(dimSums(d,3)) # 4 3
[1] 4 3
# working with an 'affinity' object ## Don't show: data(thermo)
thermo$obigt: 1809 aqueous, 3368 total species
## End(Don't show) 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 3095 -80 gas H+ 0 1 0 0 0 1 3 -7 aq
species("alanine")
CO2 H2O NH3 H2S O2 H+ ispecies logact state name 1 3 2 1 0 -3 0 1504 -3 aq alanine
a1 <- affinity(O2=c(-80,-60)) # at pH=7
energy.args: temperature is 25 C energy.args: pressure is Psat energy.args: variable 1 is log_f(O2) at 128 values from -80 to -60 subcrt: 7 species at 298.15 K and 1 bar (wet)
a2 <- affinity(O2=c(-80,-60),pH=c(0,14,7))
energy.args: temperature is 25 C energy.args: pressure is Psat energy.args: variable 1 is log_f(O2) at 128 values from -80 to -60 energy.args: variable 2 is pH at 7 values from 0 to 14 subcrt: 7 species at 298.15 K and 1 bar (wet)
# in the 2nd dimension (pH) get the 4th slice (pH=7) a3 <- slice.affinity(a2,2,4) stopifnot(all.equal(a1$values,a3$values))


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