| 1 |
rtomiyasu |
25 |
/* |
| 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 |
|
|
static const NRMat<Int4> trans_mat3[3] |
| 157 |
|
|
= { |
| 158 |
|
|
put_reduct_mat(3, 0, 1), |
| 159 |
|
|
put_reduct_mat(3, 0, 2), |
| 160 |
|
|
put_reduct_mat(3, 1, 2), |
| 161 |
|
|
}; |
| 162 |
|
|
|
| 163 |
|
|
static const NRMat<Int4> trans_mat4[6] |
| 164 |
|
|
= { |
| 165 |
|
|
put_reduct_mat(4, 0, 1), |
| 166 |
|
|
put_reduct_mat(4, 0, 2), |
| 167 |
|
|
put_reduct_mat(4, 0, 3), |
| 168 |
|
|
put_reduct_mat(4, 1, 2), |
| 169 |
|
|
put_reduct_mat(4, 1, 3), |
| 170 |
|
|
put_reduct_mat(4, 2, 3) |
| 171 |
|
|
}; |
| 172 |
|
|
|
| 173 |
|
|
if( ISIZE == 3 ) return trans_mat3[ i*(3-i)/2 + j-1 ]; |
| 174 |
|
|
else return trans_mat4[ i*(5-i)/2 + j-1 ]; |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
|
| 178 |
|
|
inline NRMat<Int4> put_transform_matrix_rowNtoNplus1(const Int4& isize) |
| 179 |
|
|
{ |
| 180 |
|
|
assert(isize >= 0); |
| 181 |
|
|
NRMat<Int4> TransMat(isize+1,isize,0); |
| 182 |
|
|
for(Int4 k=0; k<isize; k++) TransMat[k][k] = 1; |
| 183 |
|
|
for(Int4 k=0; k<isize; k++) TransMat[isize][k] = -1; |
| 184 |
|
|
|
| 185 |
|
|
return TransMat; |
| 186 |
|
|
} |
| 187 |
|
|
|
| 188 |
|
|
|
| 189 |
|
|
inline const NRMat<Int4>& put_transform_matrix_row3to4() |
| 190 |
|
|
{ |
| 191 |
|
|
static const NRMat<Int4> mat43 = put_transform_matrix_rowNtoNplus1(3); |
| 192 |
|
|
return mat43; |
| 193 |
|
|
} |
| 194 |
|
|
|
| 195 |
|
|
|
| 196 |
|
|
inline NRMat<Int4> put_transform_matrix_row3to4(const NRMat<Int4>& rhs) |
| 197 |
|
|
{ |
| 198 |
|
|
assert( rhs.nrows() == 3 ); |
| 199 |
|
|
|
| 200 |
|
|
const Int4 icol = rhs.ncols(); |
| 201 |
|
|
|
| 202 |
|
|
NRMat<Int4> TransMat(4, icol); |
| 203 |
|
|
for(Int4 k=0; k<3; k++) |
| 204 |
|
|
{ |
| 205 |
|
|
for(Int4 k2=0; k2<icol; k2++) |
| 206 |
|
|
{ |
| 207 |
|
|
TransMat[k][k2] = rhs[k][k2]; |
| 208 |
|
|
} |
| 209 |
|
|
} |
| 210 |
|
|
for(Int4 k2=0; k2<icol; k2++) |
| 211 |
|
|
{ |
| 212 |
|
|
TransMat[3][k2] = -( rhs[0][k2] + rhs[1][k2] + rhs[2][k2] ); |
| 213 |
|
|
} |
| 214 |
|
|
|
| 215 |
|
|
return TransMat; |
| 216 |
|
|
} |
| 217 |
|
|
|
| 218 |
|
|
|
| 219 |
|
|
inline NRMat<Int4> put_transform_matrix_row4to3(const NRMat<Int4>& rhs) |
| 220 |
|
|
{ |
| 221 |
|
|
assert( rhs.nrows() == 4 ); |
| 222 |
|
|
|
| 223 |
|
|
const Int4 icol = rhs.ncols(); |
| 224 |
|
|
NRMat<Int4> TransMat(3, icol); |
| 225 |
|
|
for(Int4 k=0; k<3; k++) |
| 226 |
|
|
{ |
| 227 |
|
|
for(Int4 k2=0; k2<icol; k2++) |
| 228 |
|
|
{ |
| 229 |
|
|
TransMat[k][k2] = rhs[k][k2]; |
| 230 |
|
|
} |
| 231 |
|
|
} |
| 232 |
|
|
return TransMat; |
| 233 |
|
|
} |
| 234 |
|
|
|
| 235 |
|
|
|
| 236 |
|
|
template<class T> |
| 237 |
|
|
inline SymMat<T> put_sym_matrix_sizeNplus1toN(const SymMat<T>& rhs) |
| 238 |
|
|
{ |
| 239 |
|
|
assert( rhs.size() > 0 ); |
| 240 |
|
|
|
| 241 |
|
|
static const Int4 isize = rhs.size() - 1; |
| 242 |
|
|
SymMat<T> ans(isize); |
| 243 |
|
|
|
| 244 |
|
|
for(Int4 k=0; k<isize; k++) |
| 245 |
|
|
{ |
| 246 |
|
|
for(Int4 k2=k; k2<isize; k2++) |
| 247 |
|
|
{ |
| 248 |
|
|
ans(k,k2) = rhs(k,k2); |
| 249 |
|
|
} |
| 250 |
|
|
} |
| 251 |
|
|
return ans; |
| 252 |
|
|
} |
| 253 |
|
|
|
| 254 |
|
|
template<class T> |
| 255 |
|
|
inline SymMat<T> put_sym_matrix_sizeNtoNplus1(const SymMat<T>& rhs) |
| 256 |
|
|
{ |
| 257 |
|
|
static const Int4 isize = rhs.size(); |
| 258 |
|
|
SymMat<T> ans(isize+1); |
| 259 |
|
|
|
| 260 |
|
|
// Copy. |
| 261 |
|
|
for(Int4 k=0; k<isize; k++) |
| 262 |
|
|
{ |
| 263 |
|
|
for(Int4 k2=k; k2<isize; k2++) |
| 264 |
|
|
{ |
| 265 |
|
|
ans(k,k2) = rhs(k,k2); |
| 266 |
|
|
} |
| 267 |
|
|
} |
| 268 |
|
|
|
| 269 |
|
|
ans(isize,isize) = 0.0; |
| 270 |
|
|
for(Int4 k=0; k<isize; k++) |
| 271 |
|
|
{ |
| 272 |
|
|
ans(k, isize) = 0.0; |
| 273 |
|
|
for(Int4 k2=0; k2<isize; k2++) |
| 274 |
|
|
{ |
| 275 |
|
|
ans(k, isize) -= rhs(k, k2); |
| 276 |
|
|
} |
| 277 |
|
|
ans(isize,isize) -= ans(k, isize); |
| 278 |
|
|
} |
| 279 |
|
|
|
| 280 |
|
|
return ans; |
| 281 |
|
|
} |
| 282 |
|
|
|
| 283 |
|
|
#endif |