# GPU Adding-Doubling Algorithm for Analysis of Optical Spectral Images
## 2 Materials and Methods
### Preliminaries of Adding Doubling (AD) Algorithm
#### 2.1.7 Adding Boundary Layers  
Finally, boundary conditions are included as an additional layer when there is a refractive index mismatch between the layer and the surrounding medium. Since the boundary is flat, the Fresnel equations can be used:  
\[ \begin{split} r_{s}&=\left(\frac{n_{1}\
u_{1}-n_{2 }\
u_{2}}{n_{1}\
u_{1}+n_{2}\
u_{2}}\right)^{2}\\ r_{p}&=\left(\frac{n_{1}\
u_{2}-n_{2}\
u_{1}}{n_{1} \
u_{2}+n_{2}\
u_{1}}\right)\\ r&=\frac{r_{s}+r_{p}}{2}\\ t&=1-r\end{split}  \tag{22}\]  
As we are dealing with nonpolarized light, we simply average the reflectivities for the \(s\) and \(p\) polarizations. Before calculating the reflectance and transmittance values, it is necessary to determine the critical angle where total reflection starts:  
\[v_{c}=\sqrt{1-\left(\frac{n_{1}}{n_{2}}\right)^{2}} \tag{23}\]For angles smaller than the critical angle, the cosines outside layer \(\
u_{1}\) are calculated from the cosines inside layer \(\
u_{2}\) using [PERSON]'s refraction law:  
\[\
u_{1}=\sqrt{\left(1-\left(\frac{n_{2}}{n_{1}}\sqrt{1-\
u_{2}^{2}}\right)^{2} \right)} \tag{24}\]  
The boundary matrices are then formulated as follows:  
\[ \begin{split} R_{i,j}^{01}&=\begin{cases}r(\
u_{2}) \delta_{i,j},&\
u_{2}>\
u_{c}\\ \delta_{i,j},&\
u_{2}\leq\
u_{c}\end{cases}\\ \mathbf{R}^{01}&=\mathbf{R}^{10}\\ \mathbf{T}^{01}&=\mathbf{1}-\mathbf{R}^{01}\\ \mathbf{T}^{10}&=\mathbf{T}^{01}\end{cases}\end{split}  \tag{25}\]  
The reflection \(r(\
u_{i})\) is the unpolarized Fresnel reflection function, while the Kronecker delta implies matrices \(\mathbf{R}^{01}\), \(\mathbf{R}^{10}\), \(\mathbf{T}^{10}\), and \(\mathbf{T}^{01}\) are diagonal, which means that light is specularly reflected at an angle equal to the incident angle [12]. Figure 2 depicts a boundary between medium 0 and 1 with different refraction indices. The upper boundary is added to the layer, and the corresponding reflectance and transmittance matrices are calculated in the following way:  
\[ \begin{split}\gamma_{1}&=\left(\mathbf{I}-\mathbf{R }^{10}\mathbf{R}_{t}\right)^{-1}\\ \mathbf{T}^{02}&=\mathbf{T}_{t}\gamma_{1}\mathbf{T}^{01 }\\ \mathbf{R}^{20}&=\mathbf{T}_{t}\gamma_{1}\mathbf{R}^{10 }\mathbf{T}_{t}+\mathbf{R}_{t}\\ \gamma_{2}&=\left(\mathbf{I}-\mathbf{R}_{t}\mathbf{R}^{10 }\right)^{-1}\\ \mathbf{T}^{20}&=\mathbf{T}^{10}\gamma_{2}\mathbf{T}_{t}\\ \mathbf{R}^{02}&=\mathbf{T}^{10}\gamma_{2}\mathbf{R}_{t} \mathbf{T}^{01}\end{split}  \tag{26}\]  
To incorporate the bottom boundary layer, different cases can be specified. In the first case, when the bottom layer is specular, the reflectance matrix \(\mathbf{R}_{L}\) equals:  
\[\mathbf{R}_{L}= \begin{bmatrix}R_{0}&0&\dots&0\\ 0&R_{0}&\ddots&0\\ \vdots&\ddots&\ddots&\vdots\\ 0&0&\dots&R_{0}\end{bmatrix}  \tag{27}\]  
where \(\mathbf{R}_{0}\) stands for the surface reflectance. In this scenario, only the reflectance matrix at the sample surface is relevant, while the other matrices are 0. Figure 2: Scheme of a boundary between medium 0 and 1 with different refraction indices. The reflectances \(R\) and transmittances \(T\) for both media are included. In the second case, when the bottom layer is Lambertian, the reflectance matrix is calculated as follows:  
\[\mathbf{R}_{L}=2R_{0} \begin{bmatrix}v_{1}w_{1}&v_{2}w_{2}&\cdots\\ \vdots&\ddots&\vdots\\ v_{1}w_{1}&\cdots&v_{m}w_{m}\end{bmatrix}  \tag{28}\]  
For the third case, when the bottom layer is assumed to be the same as the external medium, we can employ previously calculated reflectance and transmittance matrices from Equation (25). The reflectance matrices \(\mathbf{R^{30}}\) and \(\mathbf{R^{63}}\), as well as the transmittance matrices \(\mathbf{T^{50}}\) and \(\mathbf{T^{03}}\), are determined as follows:  
\[