| 1 |
/* |
| 2 |
* The MIT License |
| 3 |
|
| 4 |
Conograph (powder auto-indexing program) |
| 5 |
|
| 6 |
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in |
| 16 |
all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#ifndef _MATRIX_NBYN_HH_ |
| 28 |
#define _MATRIX_NBYN_HH_ |
| 29 |
|
| 30 |
#include"../RietveldAnalysisTypes.hh" |
| 31 |
#include"../utility_data_structure/SymMat.hh" |
| 32 |
#include"../utility_data_structure/nrutil_nr.hh" |
| 33 |
|
| 34 |
inline void put_complement_set3(const Int4& i, const Int4& j, Int4& k) |
| 35 |
{ |
| 36 |
static const Int4 dim = 3; |
| 37 |
|
| 38 |
assert( i != j ); |
| 39 |
assert( 0 <= i && i < dim ); |
| 40 |
assert( 0 <= j && j < dim ); |
| 41 |
|
| 42 |
bool flag_tray[dim] = {true, true, true}; |
| 43 |
flag_tray[i]=false; |
| 44 |
flag_tray[j]=false; |
| 45 |
|
| 46 |
for(Int4 n=0; n<dim; n++) |
| 47 |
{ |
| 48 |
if( flag_tray[n] ) |
| 49 |
{ |
| 50 |
k = n; |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
k = -1; // To remove a warning. |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
inline void put_complement_set4(const Int4& i, const Int4& j, Int4& k, Int4& l) |
| 59 |
{ |
| 60 |
static const Int4 dim = 4; |
| 61 |
|
| 62 |
assert( i != j ); |
| 63 |
assert( 0 <= i && i < dim ); |
| 64 |
assert( 0 <= j && j < dim ); |
| 65 |
|
| 66 |
bool flag_tray[dim] = {true, true, true, true}; |
| 67 |
flag_tray[i]=false; |
| 68 |
flag_tray[j]=false; |
| 69 |
|
| 70 |
Int4 tray[2]; |
| 71 |
for(Int4 n=0, index=0; index<2; n++) |
| 72 |
{ |
| 73 |
if( flag_tray[n] ) tray[index++] = n; |
| 74 |
} |
| 75 |
k = tray[0]; |
| 76 |
l = tray[1]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
// 0<=i,j<ISIZE, i!=j. |
| 81 |
inline NRMat<Int4> put_permutation_matrix(const Int4& ISIZE, const Int4& i, const Int4& j) |
| 82 |
{ |
| 83 |
NRMat<Int4> transmat(ISIZE,ISIZE,0); |
| 84 |
for(Int4 k=0; k<ISIZE; k++) transmat[k][k] = 1; |
| 85 |
transmat[i][i] = 0; |
| 86 |
transmat[j][j] = 0; |
| 87 |
transmat[i][j] = 1; |
| 88 |
transmat[j][i] = 1; |
| 89 |
|
| 90 |
return transmat; |
| 91 |
} |
| 92 |
// 0<=i<4, 0<=j<4, i!=j. |
| 93 |
inline const NRMat<Int4>& put_permutation_matrix_dim_4(const Int4& i, const Int4& j) |
| 94 |
{ |
| 95 |
const NRMat<Int4> *trans_mat2; |
| 96 |
|
| 97 |
// 2013.4.3 VIC Tamura. (For programs compiled with Visual Studio C++) |
| 98 |
#ifdef _OPENMP |
| 99 |
#pragma omp critical |
| 100 |
#endif |
| 101 |
{ |
| 102 |
static const NRMat<Int4> trans_mat2_[6] |
| 103 |
= { |
| 104 |
put_permutation_matrix(4, 0, 1), |
| 105 |
put_permutation_matrix(4, 0, 2), |
| 106 |
put_permutation_matrix(4, 0, 3), |
| 107 |
put_permutation_matrix(4, 1, 2), |
| 108 |
put_permutation_matrix(4, 1, 3), |
| 109 |
put_permutation_matrix(4, 2, 3) |
| 110 |
}; |
| 111 |
trans_mat2 = trans_mat2_; |
| 112 |
} |
| 113 |
|
| 114 |
assert( i < j ); |
| 115 |
|
| 116 |
return trans_mat2[ i*(5-i)/2 + j-1 ]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
// 0<=i,j<ISIZE, i!=j. |
| 121 |
static NRMat<Int4> put_reduct_mat(const Int4& ISIZE, const Int4& i, const Int4& j) |
| 122 |
{ |
| 123 |
assert( 3 <= ISIZE && ISIZE <= 4 ); |
| 124 |
if( ISIZE == 3 ) |
| 125 |
{ |
| 126 |
Int4 k; |
| 127 |
put_complement_set3(i, j, k); |
| 128 |
|
| 129 |
NRMat<Int4> transmat(3,3,0); |
| 130 |
transmat[i][i] = -1; |
| 131 |
transmat[j][j] = 1; |
| 132 |
transmat[k][k] = 1; transmat[k][i] = 2; |
| 133 |
|
| 134 |
return transmat; |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
Int4 k, l; |
| 139 |
put_complement_set4(i, j, k, l); |
| 140 |
|
| 141 |
NRMat<Int4> transmat(4,4,0); |
| 142 |
transmat[i][i] = -1; |
| 143 |
transmat[j][j] = 1; |
| 144 |
transmat[k][k] = 1; transmat[k][i] = 1; |
| 145 |
transmat[l][l] = 1; transmat[l][i] = 1; |
| 146 |
|
| 147 |
return transmat; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
inline const NRMat<Int4>& put_reduction_matrix(const Int4& ISIZE, const Int4& i, const Int4& j) |
| 153 |
{ |
| 154 |
assert( i < j ); |
| 155 |
assert( 3 <= ISIZE && ISIZE <= 4 ); |
| 156 |
|
| 157 |
// 2014.11.6 VIC Tamura. (For programs compiled with Visual Studio C++) |
| 158 |
const NRMat<Int4>* trans_mat3; |
| 159 |
const NRMat<Int4>* trans_mat4; |
| 160 |
#ifdef _OPENMP |
| 161 |
#pragma omp critical |
| 162 |
#endif |
| 163 |
{ |
| 164 |
static const NRMat<Int4> trans_mat3_[3] |
| 165 |
= { |
| 166 |
put_reduct_mat(3, 0, 1), |
| 167 |
put_reduct_mat(3, 0, 2), |
| 168 |
put_reduct_mat(3, 1, 2), |
| 169 |
}; |
| 170 |
static const NRMat<Int4> trans_mat4_[6] |
| 171 |
= { |
| 172 |
put_reduct_mat(4, 0, 1), |
| 173 |
put_reduct_mat(4, 0, 2), |
| 174 |
put_reduct_mat(4, 0, 3), |
| 175 |
put_reduct_mat(4, 1, 2), |
| 176 |
put_reduct_mat(4, 1, 3), |
| 177 |
put_reduct_mat(4, 2, 3) |
| 178 |
}; |
| 179 |
trans_mat3 = trans_mat3_; |
| 180 |
trans_mat4 = trans_mat4_; |
| 181 |
} |
| 182 |
|
| 183 |
if( ISIZE == 3 ) return trans_mat3[ i*(3-i)/2 + j-1 ]; |
| 184 |
else return trans_mat4[ i*(5-i)/2 + j-1 ]; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
inline NRMat<Int4> put_transform_matrix_rowNtoNplus1(const Int4& isize) |
| 189 |
{ |
| 190 |
assert(isize >= 0); |
| 191 |
NRMat<Int4> TransMat(isize+1,isize,0); |
| 192 |
for(Int4 k=0; k<isize; k++) TransMat[k][k] = 1; |
| 193 |
for(Int4 k=0; k<isize; k++) TransMat[isize][k] = -1; |
| 194 |
|
| 195 |
return TransMat; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
inline const NRMat<Int4>& put_transform_matrix_row3to4() |
| 200 |
{ |
| 201 |
static const NRMat<Int4> mat43 = put_transform_matrix_rowNtoNplus1(3); |
| 202 |
return mat43; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
inline NRMat<Int4> put_transform_matrix_row3to4(const NRMat<Int4>& rhs) |
| 207 |
{ |
| 208 |
assert( rhs.nrows() == 3 ); |
| 209 |
|
| 210 |
const Int4 icol = rhs.ncols(); |
| 211 |
|
| 212 |
NRMat<Int4> TransMat(4, icol); |
| 213 |
for(Int4 k=0; k<3; k++) |
| 214 |
{ |
| 215 |
for(Int4 k2=0; k2<icol; k2++) |
| 216 |
{ |
| 217 |
TransMat[k][k2] = rhs[k][k2]; |
| 218 |
} |
| 219 |
} |
| 220 |
for(Int4 k2=0; k2<icol; k2++) |
| 221 |
{ |
| 222 |
TransMat[3][k2] = -( rhs[0][k2] + rhs[1][k2] + rhs[2][k2] ); |
| 223 |
} |
| 224 |
|
| 225 |
return TransMat; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
inline NRMat<Int4> put_transform_matrix_row4to3(const NRMat<Int4>& rhs) |
| 230 |
{ |
| 231 |
assert( rhs.nrows() == 4 ); |
| 232 |
|
| 233 |
const Int4 icol = rhs.ncols(); |
| 234 |
NRMat<Int4> TransMat(3, icol); |
| 235 |
for(Int4 k=0; k<3; k++) |
| 236 |
{ |
| 237 |
for(Int4 k2=0; k2<icol; k2++) |
| 238 |
{ |
| 239 |
TransMat[k][k2] = rhs[k][k2]; |
| 240 |
} |
| 241 |
} |
| 242 |
return TransMat; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
template<class T> |
| 247 |
inline SymMat<T> put_sym_matrix_sizeNplus1toN(const SymMat<T>& rhs) |
| 248 |
{ |
| 249 |
assert( rhs.size() > 0 ); |
| 250 |
|
| 251 |
static const Int4 isize = rhs.size() - 1; |
| 252 |
SymMat<T> ans(isize); |
| 253 |
|
| 254 |
for(Int4 k=0; k<isize; k++) |
| 255 |
{ |
| 256 |
for(Int4 k2=k; k2<isize; k2++) |
| 257 |
{ |
| 258 |
ans(k,k2) = rhs(k,k2); |
| 259 |
} |
| 260 |
} |
| 261 |
return ans; |
| 262 |
} |
| 263 |
|
| 264 |
template<class T> |
| 265 |
inline SymMat<T> put_sym_matrix_sizeNtoNplus1(const SymMat<T>& rhs) |
| 266 |
{ |
| 267 |
static const Int4 isize = rhs.size(); |
| 268 |
SymMat<T> ans(isize+1); |
| 269 |
|
| 270 |
// Copy. |
| 271 |
for(Int4 k=0; k<isize; k++) |
| 272 |
{ |
| 273 |
for(Int4 k2=k; k2<isize; k2++) |
| 274 |
{ |
| 275 |
ans(k,k2) = rhs(k,k2); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
ans(isize,isize) = 0.0; |
| 280 |
for(Int4 k=0; k<isize; k++) |
| 281 |
{ |
| 282 |
ans(k, isize) = 0.0; |
| 283 |
for(Int4 k2=0; k2<isize; k2++) |
| 284 |
{ |
| 285 |
ans(k, isize) -= rhs(k, k2); |
| 286 |
} |
| 287 |
ans(isize,isize) -= ans(k, isize); |
| 288 |
} |
| 289 |
|
| 290 |
return ans; |
| 291 |
} |
| 292 |
|
| 293 |
#endif |