1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup band_helper
//! @{
namespace band_helper
{
template<typename eT>
inline
bool
is_band(uword& out_KL, uword& out_KU, const Mat<eT>& A, const uword N_min)
{
arma_extra_debug_sigprint();
// NOTE: assuming that A has a square size
// NOTE: assuming that N_min is >= 4
const uword N = A.n_rows;
if(N < N_min) { return false; }
// first, quickly check bottom-left and top-right corners
const eT eT_zero = eT(0);
const eT* A_col0 = A.memptr();
const eT* A_col1 = A_col0 + N;
if( (A_col0[N-2] != eT_zero) || (A_col0[N-1] != eT_zero) || (A_col1[N-2] != eT_zero) || (A_col1[N-1] != eT_zero) ) { return false; }
const eT* A_colNm2 = A.colptr(N-2);
const eT* A_colNm1 = A_colNm2 + N;
if( (A_colNm2[0] != eT_zero) || (A_colNm2[1] != eT_zero) || (A_colNm1[0] != eT_zero) || (A_colNm1[1] != eT_zero) ) { return false; }
// if we reached this point, go through the entire matrix to work out number of subdiagonals and superdiagonals
const uword n_nonzero_threshold = (N*N)/4; // empirically determined
uword KL = 0; // number of subdiagonals
uword KU = 0; // number of superdiagonals
const eT* A_colptr = A.memptr();
for(uword col=0; col < N; ++col)
{
uword first_nonzero_row = col;
uword last_nonzero_row = col;
for(uword row=0; row < col; ++row)
{
if( A_colptr[row] != eT_zero ) { first_nonzero_row = row; break; }
}
for(uword row=(col+1); row < N; ++row)
{
last_nonzero_row = (A_colptr[row] != eT_zero) ? row : last_nonzero_row;
}
const uword L_count = last_nonzero_row - col;
const uword U_count = col - first_nonzero_row;
if( (L_count > KL) || (U_count > KU) )
{
KL = (std::max)(KL, L_count);
KU = (std::max)(KU, U_count);
const uword n_nonzero = N*(KL+KU+1) - (KL*(KL+1) + KU*(KU+1))/2;
// return as soon as we know that it's not worth analysing the matrix any further
if(n_nonzero > n_nonzero_threshold) { return false; }
}
A_colptr += N;
}
out_KL = KL;
out_KU = KU;
return true;
}
template<typename eT>
inline
bool
is_band_lower(uword& out_KD, const Mat<eT>& A, const uword N_min)
{
arma_extra_debug_sigprint();
// NOTE: assuming that A has a square size
// NOTE: assuming that N_min is >= 4
const uword N = A.n_rows;
if(N < N_min) { return false; }
// first, quickly check bottom-left corner
const eT eT_zero = eT(0);
const eT* A_col0 = A.memptr();
const eT* A_col1 = A_col0 + N;
if( (A_col0[N-2] != eT_zero) || (A_col0[N-1] != eT_zero) || (A_col1[N-2] != eT_zero) || (A_col1[N-1] != eT_zero) ) { return false; }
// if we reached this point, go through the bottom triangle to work out number of subdiagonals
const uword n_nonzero_threshold = ( N*N - (N*(N-1))/2 ) / 4; // empirically determined
uword KL = 0; // number of subdiagonals
const eT* A_colptr = A.memptr();
for(uword col=0; col < N; ++col)
{
uword last_nonzero_row = col;
for(uword row=(col+1); row < N; ++row)
{
last_nonzero_row = (A_colptr[row] != eT_zero) ? row : last_nonzero_row;
}
const uword L_count = last_nonzero_row - col;
if(L_count > KL)
{
KL = L_count;
const uword n_nonzero = N*(KL+1) - (KL*(KL+1))/2;
// return as soon as we know that it's not worth analysing the matrix any further
if(n_nonzero > n_nonzero_threshold) { return false; }
}
A_colptr += N;
}
out_KD = KL;
return true;
}
template<typename eT>
inline
bool
is_band_upper(uword& out_KD, const Mat<eT>& A, const uword N_min)
{
arma_extra_debug_sigprint();
// NOTE: assuming that A has a square size
// NOTE: assuming that N_min is >= 4
const uword N = A.n_rows;
if(N < N_min) { return false; }
// first, quickly check top-right corner
const eT eT_zero = eT(0);
const eT* A_colNm2 = A.colptr(N-2);
const eT* A_colNm1 = A_colNm2 + N;
if( (A_colNm2[0] != eT_zero) || (A_colNm2[1] != eT_zero) || (A_colNm1[0] != eT_zero) || (A_colNm1[1] != eT_zero) ) { return false; }
// if we reached this point, go through the entire matrix to work out number of superdiagonals
const uword n_nonzero_threshold = ( N*N - (N*(N-1))/2 ) / 4; // empirically determined
uword KU = 0; // number of superdiagonals
const eT* A_colptr = A.memptr();
for(uword col=0; col < N; ++col)
{
uword first_nonzero_row = col;
for(uword row=0; row < col; ++row)
{
if( A_colptr[row] != eT_zero ) { first_nonzero_row = row; break; }
}
const uword U_count = col - first_nonzero_row;
if(U_count > KU)
{
KU = U_count;
const uword n_nonzero = N*(KU+1) - (KU*(KU+1))/2;
// return as soon as we know that it's not worth analysing the matrix any further
if(n_nonzero > n_nonzero_threshold) { return false; }
}
A_colptr += N;
}
out_KD = KU;
return true;
}
template<typename eT>
inline
void
compress(Mat<eT>& AB, const Mat<eT>& A, const uword KL, const uword KU, const bool use_offset)
{
arma_extra_debug_sigprint();
// NOTE: assuming that A has a square size
// band matrix storage format
// http://www.netlib.org/lapack/lug/node124.html
// for ?gbsv, matrix AB size: 2*KL+KU+1 x N; band representation of A stored in rows KL+1 to 2*KL+KU+1 (note: fortran counts from 1)
// for ?gbsvx, matrix AB size: KL+KU+1 x N; band representaiton of A stored in rows 1 to KL+KU+1 (note: fortran counts from 1)
//
// the +1 in the above formulas is to take into account the main diagonal
const uword AB_n_rows = (use_offset) ? uword(2*KL + KU + 1) : uword(KL + KU + 1);
const uword N = A.n_rows;
AB.set_size(AB_n_rows, N);
if(A.is_empty()) { AB.zeros(); return; }
if(AB_n_rows == uword(1))
{
eT* AB_mem = AB.memptr();
for(uword i=0; i<N; ++i) { AB_mem[i] = A.at(i,i); }
}
else
{
AB.zeros(); // paranoia
for(uword j=0; j < N; ++j)
{
const uword A_row_start = (j > KU) ? uword(j - KU) : uword(0);
const uword A_row_endp1 = (std::min)(N, j+KL+1);
const uword length = A_row_endp1 - A_row_start;
const uword AB_row_start = (KU > j) ? (KU - j) : uword(0);
const eT* A_colptr = A.colptr(j) + A_row_start;
eT* AB_colptr = AB.colptr(j) + AB_row_start + ( (use_offset) ? KL : uword(0) );
arrayops::copy( AB_colptr, A_colptr, length );
}
}
}
template<typename eT>
inline
void
uncompress(Mat<eT>& A, const Mat<eT>& AB, const uword KL, const uword KU, const bool use_offset)
{
arma_extra_debug_sigprint();
const uword AB_n_rows = AB.n_rows;
const uword N = AB.n_cols;
arma_debug_check( (AB_n_rows != ((use_offset) ? uword(2*KL + KU + 1) : uword(KL + KU + 1))), "band_helper::uncompress(): detected inconsistency" );
A.zeros(N,N); // assuming there is no aliasing between A and AB
if(AB_n_rows == uword(1))
{
const eT* AB_mem = AB.memptr();
for(uword i=0; i<N; ++i) { A.at(i,i) = AB_mem[i]; }
}
else
{
for(uword j=0; j < N; ++j)
{
const uword A_row_start = (j > KU) ? uword(j - KU) : uword(0);
const uword A_row_endp1 = (std::min)(N, j+KL+1);
const uword length = A_row_endp1 - A_row_start;
const uword AB_row_start = (KU > j) ? (KU - j) : uword(0);
const eT* AB_colptr = AB.colptr(j) + AB_row_start + ( (use_offset) ? KL : uword(0) );
eT* A_colptr = A.colptr(j) + A_row_start;
arrayops::copy( A_colptr, AB_colptr, length );
}
}
}
template<typename eT>
inline
void
extract_tridiag(Mat<eT>& out, const Mat<eT>& A)
{
arma_extra_debug_sigprint();
// NOTE: assuming that A has a square size and is at least 2x2
const uword N = A.n_rows;
out.set_size(N, 3); // assuming there is no aliasing between 'out' and 'A'
if(N < 2) { return; }
eT* DL = out.colptr(0);
eT* DD = out.colptr(1);
eT* DU = out.colptr(2);
DD[0] = A[0];
DL[0] = A[1];
const uword Nm1 = N-1;
const uword Nm2 = N-2;
for(uword i=0; i < Nm2; ++i)
{
const uword ip1 = i+1;
const eT* data = &(A.at(i, ip1));
const eT tmp0 = data[0];
const eT tmp1 = data[1];
const eT tmp2 = data[2];
DL[ip1] = tmp2;
DD[ip1] = tmp1;
DU[i ] = tmp0;
}
const eT* data = &(A.at(Nm2, Nm1));
DL[Nm1] = 0;
DU[Nm2] = data[0];
DU[Nm1] = 0;
DD[Nm1] = data[1];
}
} // end of namespace band_helper
//! @}
|