Conversion long/wide

Author

baptiste

Published

June 27, 2024

R utility functions used below.

Reading long-format T-matrix files

Some programs output T-matrices in long format, similar to the example below:

d <- read.table('data/tmat_Au20x40_Nmax3.tmat')
names(d) <- c('s','sp','l','lp','m','mp','Tr','Ti')
head(d)
  s sp l lp  m mp            Tr            Ti
1 1  1 1  1 -1 -1 -6.049214e-05 -4.266526e-04
2 1  1 1  1  0  0 -3.331557e-05 -3.932179e-04
3 1  1 1  1  1  1 -6.049214e-05 -4.266526e-04
4 1  1 1  3 -1 -1 -2.374705e-07 -1.995117e-06
5 1  1 1  3  0  0 -1.110299e-07 -1.278537e-06
6 1  1 1  3  1  1 -2.374705e-07 -1.995117e-06

Note that one advantage of this format is that only non-zero entries need to be provided, similar to a sparse matrix definition. With such data, it can be useful to add combined indices \(p\) (blocks) and \(q\) (full matrix), as illustrated below:

d <- tmatrix_combinedindex(d)
head(d)
  s sp l lp  m mp            Tr            Ti p pp q qp
1 1  1 1  1 -1 -1 -6.049214e-05 -4.266526e-04 1  1 1  1
2 1  1 1  1  0  0 -3.331557e-05 -3.932179e-04 2  2 2  2
3 1  1 1  1  1  1 -6.049214e-05 -4.266526e-04 3  3 3  3
4 1  1 1  3 -1 -1 -2.374705e-07 -1.995117e-06 1 11 1 11
5 1  1 1  3  0  0 -1.110299e-07 -1.278537e-06 2 12 2 12
6 1  1 1  3  1  1 -2.374705e-07 -1.995117e-06 3 13 3 13

Reading wide data and converting to long format

The function read_treams() reads a standard HDF5-stored T-matrix and returns it in long format, including the computation’s method_parameters fields.

d <- read_treams('data/SPH-DE~4.H5')
head(d)
  s sp l lp  m mp            Tr            Ti          mod p pp q qp domain_x
1 1  1 1  1 -1 -1 -5.907953e-05 -4.253829e-04 4.294659e-04 1  1 1  1      633
2 1  1 1  1  0  0 -3.230918e-05 -3.810847e-04 3.824519e-04 2  2 2  2      633
3 1  1 1  1  1  1 -5.907953e-05 -4.253829e-04 4.294659e-04 3  3 3  3      633
4 1  1 1  2 -1 -1  5.408062e-10 -4.834050e-09 4.864207e-09 1  5 1  5      633
5 1  1 1  2  0  0 -1.995470e-11 -3.206499e-09 3.206561e-09 2  6 2  6      633
6 1  1 1  2  1  1  5.408062e-10 -4.834050e-09 4.864207e-09 3  7 3  7      633
  domain_y domain_z fem_degree max_refinements maximumSideLength
1      633      633          2               1                13
2      633      633          2               1                13
3      633      633          2               1                13
4      633      633          2               1                13
5      633      633          2               1                13
6      633      633          2               1                13
  maximumSideLengthDomain precision
1                      13     1e-05
2                      13     1e-05
3                      13     1e-05
4                      13     1e-05
5                      13     1e-05
6                      13     1e-05

Convert to wide

If we wish to convert to a true matrix representation, the following function adds all the missing zeros and places the entries at the combined indices \((q,q')\),

w <- tmatrix_wide(d)
30 x 30 matrix of characters: 

                   [,1]              [,2] ...             [,30]
[1,]  -5.9e-05-4.3e-04i  0.0e+00+0.0e+00i ...  0.0e+00+0.0e+00i
[2,]   0.0e+00+0.0e+00i -3.2e-05-3.8e-04i ...  0.0e+00+0.0e+00i
[3,]   0.0e+00+0.0e+00i  0.0e+00+0.0e+00i ...  0.0e+00+0.0e+00i
...                 ...               ... ...               ...
[30,]  0.0e+00+0.0e+00i  0.0e+00+0.0e+00i ... -6.4e-09+1.7e-07i