| 1 |
// This file is part of PLINK 2.0, copyright (C) 2005-2022 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU General Public License as published by the Free |
| 6 |
// Software Foundation, either version 3 of the License, or (at your option) |
| 7 |
// any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 |
// more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
|
| 18 |
#include "plink2_matrix.h" |
| 19 |
|
| 20 |
#include <math.h> // fabs(), sqrt() |
| 21 |
|
| 22 |
#ifndef NOLAPACK |
| 23 |
# ifndef __APPLE__ |
| 24 |
|
| 25 |
# ifdef __cplusplus |
| 26 |
extern "C" { |
| 27 |
# endif |
| 28 |
|
| 29 |
# ifdef _WIN32 |
| 30 |
|
| 31 |
void dgemm_(char* transa, char* transb, int* m, int* n, int* k, |
| 32 |
double* alpha, double* a, int* lda, double* b, int* ldb, |
| 33 |
double* beta, double* c, int* ldc); |
| 34 |
|
| 35 |
void dgetrf_(__CLPK_integer* m, __CLPK_integer* n, |
| 36 |
__CLPK_doublereal* a, __CLPK_integer* lda, |
| 37 |
__CLPK_integer* ipiv, __CLPK_integer* info); |
| 38 |
|
| 39 |
void dsyrk_(char* uplo, char* trans, __CLPK_integer* n, __CLPK_integer* k, |
| 40 |
__CLPK_doublereal* alpha, __CLPK_doublereal* a, |
| 41 |
__CLPK_integer* lda, __CLPK_doublereal* beta, |
| 42 |
__CLPK_doublereal* c, __CLPK_integer* ldc); |
| 43 |
|
| 44 |
void dgemv_(char* trans, __CLPK_integer* m, __CLPK_integer* n, |
| 45 |
__CLPK_doublereal* alpha, __CLPK_doublereal* a, |
| 46 |
__CLPK_integer* lda, __CLPK_doublereal* x, __CLPK_integer* incx, |
| 47 |
__CLPK_doublereal* beta, __CLPK_doublereal* y, |
| 48 |
__CLPK_integer* incy); |
| 49 |
|
| 50 |
void sgemm_(char* transa, char* transb, int* m, int* n, int* k, |
| 51 |
float* alpha, float* a, int* lda, float* b, int* ldb, |
| 52 |
float* beta, float* c, int* ldc); |
| 53 |
|
| 54 |
void sgemv_(char* trans, int* m, int* n, float* alpha, float* a, int* lda, |
| 55 |
float* x, int* incx, float* beta, float* y, int* incy); |
| 56 |
|
| 57 |
void ssyrk_(char* uplo, char* trans, __CLPK_integer* n, __CLPK_integer* k, |
| 58 |
float* alpha, float* a, __CLPK_integer* lda, float* beta, |
| 59 |
float* c, __CLPK_integer* ldc); |
| 60 |
|
| 61 |
# else // Linux |
| 62 |
# ifndef USE_MKL |
| 63 |
int dgetrf_(__CLPK_integer* m, __CLPK_integer* n, |
| 64 |
__CLPK_doublereal* a, __CLPK_integer* lda, |
| 65 |
__CLPK_integer* ipiv, __CLPK_integer* info); |
| 66 |
|
| 67 |
int dgetri_(__CLPK_integer* n, __CLPK_doublereal* a, |
| 68 |
__CLPK_integer* lda, __CLPK_integer* ipiv, |
| 69 |
__CLPK_doublereal* work, __CLPK_integer* lwork, |
| 70 |
__CLPK_integer* info); |
| 71 |
|
| 72 |
double dlange_(char* norm, __CLPK_integer* m, __CLPK_integer* n, |
| 73 |
__CLPK_doublereal* a, __CLPK_integer* lda, |
| 74 |
__CLPK_doublereal* work); |
| 75 |
|
| 76 |
double dlansy_(char* norm, char* uplo, __CLPK_integer* n, |
| 77 |
__CLPK_doublereal* a, __CLPK_integer* lda, |
| 78 |
__CLPK_doublereal* work); |
| 79 |
|
| 80 |
int dgecon_(char* norm, __CLPK_integer* n, __CLPK_doublereal* a, |
| 81 |
__CLPK_integer* lda, __CLPK_doublereal* anorm, |
| 82 |
__CLPK_doublereal* rcond, __CLPK_doublereal* work, |
| 83 |
__CLPK_integer* iwork, __CLPK_integer* info); |
| 84 |
|
| 85 |
int dpocon_(char* uplo, __CLPK_integer* n, __CLPK_doublereal* a, |
| 86 |
__CLPK_integer* lda, __CLPK_doublereal* anorm, |
| 87 |
__CLPK_doublereal* rcond, __CLPK_doublereal* work, |
| 88 |
__CLPK_integer* iwork, __CLPK_integer* info); |
| 89 |
|
| 90 |
/* |
| 91 |
void dgesdd_(char* jobs, __CLPK_integer* m, __CLPK_integer* n, |
| 92 |
__CLPK_doublereal* a, __CLPK_integer* lda, __CLPK_doublereal* s, |
| 93 |
__CLPK_doublereal* u, __CLPK_integer* ldu, |
| 94 |
__CLPK_doublereal* vt, __CLPK_integer* ldvt, |
| 95 |
__CLPK_doublereal* work, __CLPK_integer* lwork, |
| 96 |
__CLPK_integer* iwork, __CLPK_integer* info); |
| 97 |
*/ |
| 98 |
|
| 99 |
void dgesvd_(char* jobu, char* jobvt, __CLPK_integer* m, __CLPK_integer* n, |
| 100 |
__CLPK_doublereal* a, __CLPK_integer* lda, __CLPK_doublereal* s, |
| 101 |
__CLPK_doublereal* u, __CLPK_integer* ldu, |
| 102 |
__CLPK_doublereal* vt, __CLPK_integer* ldvt, |
| 103 |
__CLPK_doublereal* work, __CLPK_integer* lwork, |
| 104 |
__CLPK_integer* info); |
| 105 |
|
| 106 |
int dsyevr_(char* jobz, char* range, char* uplo, __CLPK_integer* n, |
| 107 |
__CLPK_doublereal* a, __CLPK_integer* lda, __CLPK_doublereal* vl, |
| 108 |
__CLPK_doublereal* vu, __CLPK_integer* il, __CLPK_integer* iu, |
| 109 |
__CLPK_doublereal* abstol, __CLPK_integer* m, |
| 110 |
__CLPK_doublereal* w, __CLPK_doublereal* z, __CLPK_integer* ldz, |
| 111 |
__CLPK_integer* isuppz, __CLPK_doublereal* work, |
| 112 |
__CLPK_integer* lwork, __CLPK_integer* iwork, |
| 113 |
__CLPK_integer* liwork, __CLPK_integer* info); |
| 114 |
|
| 115 |
int dpotrf_(char* uplo, __CLPK_integer* n, __CLPK_doublereal* a, |
| 116 |
__CLPK_integer* lda, __CLPK_integer* info); |
| 117 |
|
| 118 |
|
| 119 |
void dpotrs_(char* uplo, __CLPK_integer* n, __CLPK_integer* nrhs, |
| 120 |
__CLPK_doublereal* a, __CLPK_integer* lda, __CLPK_doublereal* b, |
| 121 |
__CLPK_integer* ldb, __CLPK_integer* info); |
| 122 |
|
| 123 |
# ifndef USE_CBLAS_XGEMM |
| 124 |
void dgemm_(char* transa, char* transb, __CLPK_integer* m, __CLPK_integer* n, |
| 125 |
__CLPK_integer* k, __CLPK_doublereal* alpha, |
| 126 |
__CLPK_doublereal* a, __CLPK_integer* lda, __CLPK_doublereal* b, |
| 127 |
__CLPK_integer* ldb, __CLPK_doublereal* beta, |
| 128 |
__CLPK_doublereal* c, __CLPK_integer* ldc); |
| 129 |
|
| 130 |
void dgemv_(char* trans, __CLPK_integer* m, __CLPK_integer* n, |
| 131 |
__CLPK_doublereal* alpha, __CLPK_doublereal* a, |
| 132 |
__CLPK_integer* lda, __CLPK_doublereal* x, __CLPK_integer* incx, |
| 133 |
__CLPK_doublereal* beta, __CLPK_doublereal* y, |
| 134 |
__CLPK_integer* incy); |
| 135 |
|
| 136 |
void sgemm_(char* transa, char* transb, __CLPK_integer* m, __CLPK_integer* n, |
| 137 |
__CLPK_integer* k, float* alpha, float* a, __CLPK_integer* lda, |
| 138 |
float* b, __CLPK_integer* ldb, float* beta, float* c, |
| 139 |
__CLPK_integer* ldc); |
| 140 |
|
| 141 |
void sgemv_(char* trans, __CLPK_integer* m, __CLPK_integer* n, float* alpha, |
| 142 |
float* a, __CLPK_integer* lda, float* x, __CLPK_integer* incx, |
| 143 |
float* beta, float* y, __CLPK_integer* incy); |
| 144 |
|
| 145 |
void dsyrk_(char* uplo, char* trans, __CLPK_integer* n, __CLPK_integer* k, |
| 146 |
__CLPK_doublereal* alpha, __CLPK_doublereal* a, |
| 147 |
__CLPK_integer* lda, __CLPK_doublereal* beta, |
| 148 |
__CLPK_doublereal* c, __CLPK_integer* ldc); |
| 149 |
|
| 150 |
void ssyrk_(char* uplo, char* trans, __CLPK_integer* n, __CLPK_integer* k, |
| 151 |
float* alpha, float* a, __CLPK_integer* lda, float* beta, |
| 152 |
float* c, __CLPK_integer* ldc); |
| 153 |
# endif |
| 154 |
# endif // !USE_MKL |
| 155 |
# endif // Linux |
| 156 |
|
| 157 |
void xerbla_(void); |
| 158 |
# ifdef __cplusplus |
| 159 |
} // extern "C" |
| 160 |
# endif // __cplusplus |
| 161 |
void xerbla_(void) {} // fix static linking error |
| 162 |
# endif // not __APPLE__ |
| 163 |
|
| 164 |
#endif // !NOLAPACK |
| 165 |
|
| 166 |
|
| 167 |
#ifdef __cplusplus |
| 168 |
namespace plink2 { |
| 169 |
#endif |
| 170 |
|
| 171 |
intptr_t FirstInfOrNan(const double* vec, uintptr_t size) { |
| 172 |
// possible todo: vectorize this. Main loop could bitwise-or results from |
| 173 |
// several vector-comparisons at a time, use e.g. movemask to check for at |
| 174 |
// least one hit, then backtrack to identify first location. |
| 175 |
const uint64_t* vec_alias = R_CAST(const uint64_t*, vec); |
| 176 |
for (uintptr_t ulii = 0; ulii != size; ++ulii) { |
| 177 |
if ((vec_alias[ulii] & (0x7ffLLU << 52)) == (0x7ffLLU << 52)) { |
| 178 |
return S_CAST(intptr_t, ulii); |
| 179 |
} |
| 180 |
} |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
uint32_t LowerTriangularFirstInfOrNan(const double* matrix, uintptr_t dim, uintptr_t* row_idx_ptr, uintptr_t* col_idx_ptr) { |
| 185 |
const uint64_t* row_start = R_CAST(const uint64_t*, matrix); |
| 186 |
for (uintptr_t row_idx = 0; row_idx != dim; ++row_idx) { |
| 187 |
for (uintptr_t col_idx = 0; col_idx <= row_idx; ++col_idx) { |
| 188 |
if ((row_start[col_idx] & (0x7ffLLU << 52)) == (0x7ffLLU << 52)) { |
| 189 |
*row_idx_ptr = row_idx; |
| 190 |
*col_idx_ptr = col_idx; |
| 191 |
return 1; |
| 192 |
} |
| 193 |
} |
| 194 |
row_start = &(row_start[dim]); |
| 195 |
} |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
void ReflectMatrix(uint32_t dim, double* matrix) { |
| 200 |
const uintptr_t dim_p1l = dim + 1; |
| 201 |
double* write_row = matrix; |
| 202 |
for (uint32_t row_idx = 0; row_idx != dim; ++row_idx) { |
| 203 |
double* read_col_iter = &(matrix[dim_p1l * row_idx + dim]); |
| 204 |
for (uint32_t col_idx = row_idx + 1; col_idx != dim; ++col_idx) { |
| 205 |
write_row[col_idx] = *read_col_iter; |
| 206 |
read_col_iter = &(read_col_iter[dim]); |
| 207 |
} |
| 208 |
write_row = &(write_row[dim]); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void ReflectStridedMatrix(uint32_t dim, uint32_t stride, double* matrix) { |
| 213 |
const uintptr_t stride_p1l = stride + 1; |
| 214 |
double* write_row = matrix; |
| 215 |
for (uint32_t row_idx = 0; row_idx != dim; ++row_idx) { |
| 216 |
double* read_col_iter = &(matrix[stride_p1l * row_idx + stride]); |
| 217 |
for (uint32_t col_idx = row_idx + 1; col_idx != dim; ++col_idx) { |
| 218 |
write_row[col_idx] = *read_col_iter; |
| 219 |
read_col_iter = &(read_col_iter[stride]); |
| 220 |
} |
| 221 |
write_row = &(write_row[stride]); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void ReflectFmatrix(uint32_t dim, uint32_t stride, float* matrix) { |
| 226 |
const uintptr_t stride_p1l = stride + 1; |
| 227 |
float* write_row = matrix; |
| 228 |
for (uint32_t row_idx = 0; row_idx != dim; ++row_idx) { |
| 229 |
float* read_col_iter = &(matrix[stride_p1l * row_idx + stride]); |
| 230 |
for (uint32_t col_idx = row_idx + 1; col_idx != dim; ++col_idx) { |
| 231 |
write_row[col_idx] = *read_col_iter; |
| 232 |
read_col_iter = &(read_col_iter[stride]); |
| 233 |
} |
| 234 |
write_row = &(write_row[stride]); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void ReflectStridedMatrix0(uint32_t dim, uint32_t stride, double* matrix) { |
| 239 |
const uintptr_t stride_p1l = stride + 1; |
| 240 |
double* write_row = matrix; |
| 241 |
for (uint32_t row_idx = 0; row_idx != dim; ++row_idx) { |
| 242 |
double* read_col_iter = &(matrix[stride_p1l * row_idx + stride]); |
| 243 |
for (uint32_t col_idx = row_idx + 1; col_idx != dim; ++col_idx) { |
| 244 |
write_row[col_idx] = *read_col_iter; |
| 245 |
read_col_iter = &(read_col_iter[stride]); |
| 246 |
} |
| 247 |
ZeroDArr(stride - dim, &(write_row[dim])); |
| 248 |
write_row = &(write_row[stride]); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void ReflectFmatrix0(uint32_t dim, uint32_t stride, float* matrix) { |
| 253 |
const uintptr_t stride_p1l = stride + 1; |
| 254 |
float* write_row = matrix; |
| 255 |
for (uint32_t row_idx = 0; row_idx != dim; ++row_idx) { |
| 256 |
float* read_col_iter = &(matrix[stride_p1l * row_idx + stride]); |
| 257 |
for (uint32_t col_idx = row_idx + 1; col_idx != dim; ++col_idx) { |
| 258 |
write_row[col_idx] = *read_col_iter; |
| 259 |
read_col_iter = &(read_col_iter[stride]); |
| 260 |
} |
| 261 |
ZeroFArr(stride - dim, &(write_row[dim])); |
| 262 |
write_row = &(write_row[stride]); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
static inline double Sqr(const double aa) { |
| 267 |
return aa * aa; |
| 268 |
} |
| 269 |
|
| 270 |
double Pythag(const double a, const double b) { |
| 271 |
// PLINK stats.cpp Pythag(). |
| 272 |
double absa,absb; |
| 273 |
|
| 274 |
absa=fabs(a); |
| 275 |
absb=fabs(b); |
| 276 |
if (absa > absb) { |
| 277 |
return absa*sqrt(1.0+Sqr(absb/absa)); |
| 278 |
} |
| 279 |
return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+Sqr(absa/absb))); |
| 280 |
} |
| 281 |
|
| 282 |
#ifdef NOLAPACK |
| 283 |
static inline double MultiplyBySgn(double aa, double bb) { |
| 284 |
// PLINK helper.h MultiplyBySgn() template specialized to doubles. |
| 285 |
return (bb >= 0)? (aa >= 0 ? aa : -aa) : (aa >= 0 ? -aa : aa); |
| 286 |
} |
| 287 |
|
| 288 |
uint32_t SvdcmpC(int32_t m, double* a, double* w, double* v) { |
| 289 |
// C port of PLINK stats.cpp svdcmp(). |
| 290 |
// m: (in) number of rows/columns |
| 291 |
// a: (in/out) input matrix, becomes first part of decomposition |
| 292 |
// w: (out) singular values |
| 293 |
// v: (out) final part of decomposition |
| 294 |
// |
| 295 |
// Now thread-safe. |
| 296 |
double* rv1 = &(w[S_CAST(uint32_t, m)]); |
| 297 |
int32_t n = m; |
| 298 |
int32_t flag; |
| 299 |
int32_t l = 0; // suppress compile warning |
| 300 |
int32_t i,its,j,jj,k,nm; |
| 301 |
double anorm,c,f,g,h,s,scale,x,y,z; |
| 302 |
double temp; |
| 303 |
|
| 304 |
g=scale=anorm=0.0; |
| 305 |
for (i=0;i!=n;++i) { |
| 306 |
l=i+2; |
| 307 |
rv1[i]=scale*g; |
| 308 |
g=s=scale=0.0; |
| 309 |
if (i < m) { |
| 310 |
for (k=i;k!=m;++k) scale += fabs(a[k * m + i]); |
| 311 |
if (scale != 0.0) { |
| 312 |
for (k=i;k!=m;++k) { |
| 313 |
a[k * m + i] /= scale; |
| 314 |
s += a[k * m + i]*a[k * m + i]; |
| 315 |
} |
| 316 |
f=a[i * m + i]; |
| 317 |
g = -MultiplyBySgn(sqrt(s),f); |
| 318 |
h=f*g-s; |
| 319 |
a[i * m + i]=f-g; |
| 320 |
for (j=l-1;j!=n;++j) { |
| 321 |
for (s=0.0,k=i;k!=m;++k) s += a[k * m + i]*a[k * m + j]; |
| 322 |
f=s/h; |
| 323 |
for (k=i;k!=m;++k) a[k * m + j] += f*a[k * m + i]; |
| 324 |
} |
| 325 |
for (k=i;k!=m;++k) a[k * m + i] *= scale; |
| 326 |
} |
| 327 |
} |
| 328 |
w[i]=scale *g; |
| 329 |
g=s=scale=0.0; |
| 330 |
if (i+1 <= m && i+1 != n) { |
| 331 |
for (k=l-1;k!=n;++k) scale += fabs(a[i * m + k]); |
| 332 |
if (scale != 0.0) { |
| 333 |
for (k=l-1;k!=n;++k) { |
| 334 |
a[i * m + k] /= scale; |
| 335 |
s += a[i * m + k]*a[i * m + k]; |
| 336 |
} |
| 337 |
f=a[i * m + l-1]; |
| 338 |
g = -MultiplyBySgn(sqrt(s),f); |
| 339 |
h=f*g-s; |
| 340 |
a[i * m + l-1]=f-g; |
| 341 |
for (k=l-1;k!=n;++k) rv1[k]=a[i * m + k]/h; |
| 342 |
for (j=l-1;j!=m;++j) { |
| 343 |
for (s=0.0,k=l-1;k!=n;++k) s += a[j * m + k]*a[i * m + k]; |
| 344 |
for (k=l-1;k!=n;++k) a[j * m + k] += s*rv1[k]; |
| 345 |
} |
| 346 |
for (k=l-1;k!=n;++k) a[i * m + k] *= scale; |
| 347 |
} |
| 348 |
} |
| 349 |
anorm=MAXV(anorm,(fabs(w[i])+fabs(rv1[i]))); |
| 350 |
} |
| 351 |
for (i=n-1;i>=0;i--) { |
| 352 |
if (i < n-1) { |
| 353 |
if (g != 0.0) { |
| 354 |
for (j=l;j!=n;++j) |
| 355 |
v[j * m + i]=(a[i * m + j]/a[i * m + l])/g; |
| 356 |
for (j=l;j!=n;++j) { |
| 357 |
for (s=0.0,k=l;k!=n;++k) s += a[i * m + k]*v[k * m + j]; |
| 358 |
for (k=l;k!=n;++k) v[k * m + j] += s*v[k * m + i]; |
| 359 |
} |
| 360 |
} |
| 361 |
for (j=l;j!=n;++j) v[i * m + j]=v[j * m + i]=0.0; |
| 362 |
} |
| 363 |
v[i * m + i]=1.0; |
| 364 |
g=rv1[i]; |
| 365 |
l=i; |
| 366 |
} |
| 367 |
for (i=MINV(m,n)-1;i>=0;i--) { |
| 368 |
l=i+1; |
| 369 |
g=w[i]; |
| 370 |
for (j=l;j!=n;++j) a[i * m + j]=0.0; |
| 371 |
if (g != 0.0) { |
| 372 |
g=1.0/g; |
| 373 |
for (j=l;j!=n;++j) { |
| 374 |
for (s=0.0,k=l;k<m;++k) s += a[k * m + i]*a[k * m + j]; |
| 375 |
f=(s/a[i * m + i])*g; |
| 376 |
for (k=i;k!=m;++k) a[k * m + j] += f*a[k * m + i]; |
| 377 |
} |
| 378 |
for (j=i;j!=m;++j) a[j * m + i] *= g; |
| 379 |
} else for (j=i;j!=m;++j) a[j * m + i]=0.0; |
| 380 |
++a[i * m + i]; |
| 381 |
} |
| 382 |
for (k=n-1;k>=0;k--) { |
| 383 |
for (its=0;its!=30;++its) { |
| 384 |
flag=1; |
| 385 |
for (l=k;l>=0;l--) { |
| 386 |
nm=l-1; |
| 387 |
temp=fabs(rv1[l])+anorm; |
| 388 |
if (temp == anorm) { |
| 389 |
flag=0; |
| 390 |
break; |
| 391 |
} |
| 392 |
temp=fabs(w[nm])+anorm; |
| 393 |
if (temp == anorm) break; |
| 394 |
} |
| 395 |
if (flag) { |
| 396 |
c=0.0; |
| 397 |
s=1.0; |
| 398 |
for (i=l;i!=k+1;++i) { |
| 399 |
f=s*rv1[i]; |
| 400 |
rv1[i]=c*rv1[i]; |
| 401 |
temp = fabs(f)+anorm; |
| 402 |
if (temp == anorm) break; |
| 403 |
g=w[i]; |
| 404 |
h=Pythag(f,g); |
| 405 |
w[i]=h; |
| 406 |
h=1.0/h; |
| 407 |
c=g*h; |
| 408 |
s = -f*h; |
| 409 |
for (j=0;j!=m;++j) { |
| 410 |
y=a[j * m + nm]; |
| 411 |
z=a[j * m + i]; |
| 412 |
a[j * m + nm]=y*c+z*s; |
| 413 |
a[j * m + i]=z*c-y*s; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
z=w[k]; |
| 418 |
if (l == k) { |
| 419 |
if (z < 0.0) { |
| 420 |
w[k] = -z; |
| 421 |
for (j=0;j!=n;++j) v[j * m + k] = -v[j * m + k]; |
| 422 |
} |
| 423 |
break; |
| 424 |
} |
| 425 |
if (its == 29) |
| 426 |
return 0; // cannot converge: multi-collinearity? |
| 427 |
x=w[l]; |
| 428 |
nm=k-1; |
| 429 |
y=w[nm]; |
| 430 |
g=rv1[nm]; |
| 431 |
h=rv1[k]; |
| 432 |
f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y); |
| 433 |
g=Pythag(f,1.0); |
| 434 |
f=((x-z)*(x+z)+h*((y/(f+MultiplyBySgn(g,f)))-h))/x; |
| 435 |
c=s=1.0; |
| 436 |
for (j=l;j<=nm;j++) { |
| 437 |
i=j+1; |
| 438 |
g=rv1[i]; |
| 439 |
y=w[i]; |
| 440 |
h=s*g; |
| 441 |
g=c*g; |
| 442 |
z=Pythag(f,h); |
| 443 |
rv1[j]=z; |
| 444 |
c=f/z; |
| 445 |
s=h/z; |
| 446 |
f=x*c+g*s; |
| 447 |
g=g*c-x*s; |
| 448 |
h=y*s; |
| 449 |
y *= c; |
| 450 |
for (jj=0;jj!=n;++jj) { |
| 451 |
x=v[jj * m + j]; |
| 452 |
z=v[jj * m + i]; |
| 453 |
v[jj * m + j]=x*c+z*s; |
| 454 |
v[jj * m + i]=z*c-x*s; |
| 455 |
} |
| 456 |
z=Pythag(f,h); |
| 457 |
w[j]=z; |
| 458 |
if (z != 0.0) { |
| 459 |
z=1.0/z; |
| 460 |
c=f*z; |
| 461 |
s=h*z; |
| 462 |
} |
| 463 |
f=c*g+s*y; |
| 464 |
x=c*y-s*g; |
| 465 |
for (jj=0;jj!=m;++jj) { |
| 466 |
y=a[jj * m + j]; |
| 467 |
z=a[jj * m + i]; |
| 468 |
a[jj * m + j]=y*c+z*s; |
| 469 |
a[jj * m + i]=z*c-y*s; |
| 470 |
} |
| 471 |
} |
| 472 |
rv1[l]=0.0; |
| 473 |
rv1[k]=f; |
| 474 |
w[k]=x; |
| 475 |
} |
| 476 |
} |
| 477 |
return 1; |
| 478 |
} |
| 479 |
|
| 480 |
BoolErr InvertMatrix(int32_t dim, double* matrix, MatrixInvertBuf1* dbl_1d_buf, double* dbl_2d_buf) { |
| 481 |
// C port of PLINK stats.cpp's svd_inverse() function. |
| 482 |
assert(dim > 0); |
| 483 |
|
| 484 |
// w -> dbl_1d_buf |
| 485 |
// v -> dbl_2d_buf |
| 486 |
const double eps = 1e-24; |
| 487 |
// not unlikely(), matrix inversion failure can be common |
| 488 |
if (!SvdcmpC(dim, matrix, dbl_1d_buf, dbl_2d_buf)) { |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
// Look for singular values |
| 493 |
double wmax = 0; |
| 494 |
int32_t i; |
| 495 |
for (i=0; i!=dim; ++i) { |
| 496 |
wmax = dbl_1d_buf[i] > wmax ? dbl_1d_buf[i] : wmax; |
| 497 |
} |
| 498 |
double wmin = wmax * eps; |
| 499 |
for (i=0; i!=dim; ++i) { |
| 500 |
dbl_1d_buf[i] = dbl_1d_buf[i] < wmin ? 0 : (1 / dbl_1d_buf[i]); |
| 501 |
} |
| 502 |
|
| 503 |
int32_t j; |
| 504 |
for (i=0; i!=dim; ++i) { |
| 505 |
for (j=0; j!=dim; ++j) { |
| 506 |
matrix[i * dim + j] = matrix[i * dim + j] * dbl_1d_buf[j]; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
int32_t k; |
| 511 |
// [nxn].[t(v)] |
| 512 |
for (i=0; i!=dim; ++i) { |
| 513 |
ZeroDArr(dim, dbl_1d_buf); |
| 514 |
for (j=0; j!=dim; ++j) { |
| 515 |
for (k=0; k!=dim; ++k) { |
| 516 |
dbl_1d_buf[j] += matrix[i * dim + k] * dbl_2d_buf[j * dim + k]; |
| 517 |
} |
| 518 |
} |
| 519 |
for (j = 0; j != dim; ++j) { |
| 520 |
matrix[i * dim + j] = dbl_1d_buf[j]; |
| 521 |
} |
| 522 |
} |
| 523 |
for (i=1; i!=dim; ++i) { |
| 524 |
for(j=0; j!=i; ++j) { |
| 525 |
const double tmp = matrix[i * dim + j]; |
| 526 |
matrix[i * dim + j] = matrix[j * dim + i]; |
| 527 |
matrix[j * dim + i] = tmp; |
| 528 |
} |
| 529 |
} |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
BoolErr InvertStridedMatrixFirstHalf(int32_t dim, int32_t stride, double* matrix, MatrixInvertBuf1* dbl_1d_buf, double* dbl_2d_buf) { |
| 534 |
// Unfortunately, SvdcmpC() doesn't support a stride parameter. |
| 535 |
if (stride != dim) { |
| 536 |
const uint32_t dim_u = dim; |
| 537 |
const uintptr_t nbyte = dim_u * sizeof(double); |
| 538 |
const double* read_row = matrix; |
| 539 |
double* write_row = matrix; |
| 540 |
for (uint32_t row_idx = 1; row_idx != dim_u; ++row_idx) { |
| 541 |
read_row = &(read_row[S_CAST(uint32_t, stride)]); |
| 542 |
write_row = &(write_row[dim_u]); |
| 543 |
memmove(write_row, read_row, nbyte); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
return (!SvdcmpC(dim, matrix, dbl_1d_buf, dbl_2d_buf)); |
| 548 |
} |
| 549 |
|
| 550 |
BoolErr InvertFmatrixFirstHalf(int32_t dim, uint32_t stride, const float* matrix, double* half_inverted, MatrixInvertBuf1* dbl_1d_buf, double* dbl_2d_buf) { |
| 551 |
const float* read_row = matrix; |
| 552 |
double* write_row = half_inverted; |
| 553 |
for (uint32_t row_idx = 0; row_idx != S_CAST(uint32_t, dim); ++row_idx) { |
| 554 |
for (uint32_t col_idx = 0; col_idx != S_CAST(uint32_t, dim); ++col_idx) { |
| 555 |
write_row[col_idx] = S_CAST(double, read_row[col_idx]); |
| 556 |
} |
| 557 |
read_row = &(read_row[stride]); |
| 558 |
write_row = &(write_row[S_CAST(uint32_t, dim)]); |
| 559 |
} |
| 560 |
|
| 561 |
return (!SvdcmpC(dim, half_inverted, dbl_1d_buf, dbl_2d_buf)); |
| 562 |
} |
| 563 |
|
| 564 |
void InvertStridedMatrixSecondHalf(__CLPK_integer dim, __CLPK_integer stride, double* matrix, MatrixInvertBuf1* dbl_1d_buf, double* dbl_2d_buf) { |
| 565 |
// Look for singular values |
| 566 |
assert(dim > 0); |
| 567 |
const double eps = 1e-24; |
| 568 |
double wmax = 0; |
| 569 |
int32_t i; |
| 570 |
for (i=0; i!=dim; ++i) { |
| 571 |
wmax = dbl_1d_buf[i] > wmax ? dbl_1d_buf[i] : wmax; |
| 572 |
} |
| 573 |
double wmin = wmax * eps; |
| 574 |
for (i=0; i!=dim; ++i) { |
| 575 |
dbl_1d_buf[i] = dbl_1d_buf[i] < wmin ? 0 : (1 / dbl_1d_buf[i]); |
| 576 |
} |
| 577 |
|
| 578 |
int32_t j; |
| 579 |
for (i=0; i!=dim; ++i) { |
| 580 |
for (j=0; j!=dim; ++j) { |
| 581 |
matrix[i * dim + j] = matrix[i * dim + j] * dbl_1d_buf[j]; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
int32_t k; |
| 586 |
// [nxn].[t(v)] |
| 587 |
for (i=0; i!=dim; ++i) { |
| 588 |
ZeroDArr(dim, dbl_1d_buf); |
| 589 |
for (j=0; j!=dim; ++j) { |
| 590 |
for (k=0; k!=dim; ++k) { |
| 591 |
dbl_1d_buf[j] += matrix[i * dim + k] * dbl_2d_buf[j * dim + k]; |
| 592 |
} |
| 593 |
} |
| 594 |
for (j = 0; j != dim; ++j) { |
| 595 |
matrix[i * dim + j] = dbl_1d_buf[j]; |
| 596 |
} |
| 597 |
} |
| 598 |
// transpose, then fix stride |
| 599 |
for (i=1; i!=dim; ++i) { |
| 600 |
for(j=0; j!=i; ++j) { |
| 601 |
const double tmp = matrix[i * dim + j]; |
| 602 |
matrix[i * dim + j] = matrix[j * dim + i]; |
| 603 |
matrix[j * dim + i] = tmp; |
| 604 |
} |
| 605 |
} |
| 606 |
if ((stride != dim) && (dim > 1)) { |
| 607 |
const uint32_t dim_m1 = dim - 1; |
| 608 |
const uintptr_t nbyte = S_CAST(uint32_t, dim) * sizeof(double); |
| 609 |
double* read_row = &(matrix[dim_m1 * dim]); |
| 610 |
double* write_row = &(matrix[dim_m1 * stride]); |
| 611 |
for (uint32_t bot_row_idx = 0; bot_row_idx != dim_m1; ++bot_row_idx) { |
| 612 |
memmove(write_row, read_row, nbyte); |
| 613 |
read_row -= dim; |
| 614 |
write_row -= stride; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
void InvertFmatrixSecondHalf(__CLPK_integer dim, uint32_t stride, double* half_inverted, float* inverted_result, MatrixInvertBuf1* dbl_1d_buf, double* dbl_2d_buf) { |
| 620 |
// Look for singular values |
| 621 |
assert(dim > 0); |
| 622 |
const double eps = 1e-24; |
| 623 |
double wmax = 0; |
| 624 |
int32_t i; |
| 625 |
for (i=0; i!=dim; ++i) { |
| 626 |
wmax = dbl_1d_buf[i] > wmax ? dbl_1d_buf[i] : wmax; |
| 627 |
} |
| 628 |
double wmin = wmax * eps; |
| 629 |
for (i=0; i!=dim; ++i) { |
| 630 |
dbl_1d_buf[i] = dbl_1d_buf[i] < wmin ? 0 : (1 / dbl_1d_buf[i]); |
| 631 |
} |
| 632 |
|
| 633 |
int32_t j; |
| 634 |
for (i=0; i!=dim; ++i) { |
| 635 |
for (j=0; j!=dim; ++j) { |
| 636 |
half_inverted[i * dim + j] = half_inverted[i * dim + j] * dbl_1d_buf[j]; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
int32_t k; |
| 641 |
// [nxn].[t(v)] |
| 642 |
for (i=0; i!=dim; ++i) { |
| 643 |
ZeroDArr(dim, dbl_1d_buf); |
| 644 |
for (j=0; j!=dim; ++j) { |
| 645 |
for (k=0; k!=dim; ++k) { |
| 646 |
dbl_1d_buf[j] += half_inverted[i * dim + k] * dbl_2d_buf[j * dim + k]; |
| 647 |
} |
| 648 |
} |
| 649 |
for (j = 0; j != dim; ++j) { |
| 650 |
half_inverted[i * dim + j] = dbl_1d_buf[j]; |
| 651 |
} |
| 652 |
} |
| 653 |
inverted_result[0] = S_CAST(float, half_inverted[0]); |
| 654 |
for (i=1; i!=dim; ++i) { |
| 655 |
for(j=0; j!=i; ++j) { |
| 656 |
inverted_result[i * stride + j] = S_CAST(float, half_inverted[j * dim + i]); |
| 657 |
inverted_result[j * stride + i] = S_CAST(float, half_inverted[i * dim + j]); |
| 658 |
} |
| 659 |
inverted_result[i * stride + i] = S_CAST(float, half_inverted[i * dim + i]); |
| 660 |
} |
| 661 |
} |
| 662 |
#else // !NOLAPACK |
| 663 |
BoolErr InvertMatrix(__CLPK_integer dim, double* matrix, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 664 |
// InvertSymmdefMatrix() is noticeably faster in the symmetric |
| 665 |
// positive-semidefinite case. |
| 666 |
__CLPK_integer info; |
| 667 |
dgetrf_(&dim, &dim, matrix, &dim, int_1d_buf, &info); |
| 668 |
if (info) { |
| 669 |
return 1; |
| 670 |
} |
| 671 |
__CLPK_integer lwork = dim * dim; |
| 672 |
dgetri_(&dim, matrix, &dim, int_1d_buf, dbl_2d_buf, &lwork, &info); |
| 673 |
assert(info == 0); |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
BoolErr InvertMatrixChecked(__CLPK_integer dim, double* matrix, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 678 |
// This used to fall back on PLINK 1.07's SVD-based implementation when the |
| 679 |
// rcond estimate was too small, but in practice that just slowed things down |
| 680 |
// without meaningfully improving inversion of nonsingular matrices. So now |
| 681 |
// this just exits a bit earlier, while leaving the old "binary search for |
| 682 |
// the first row/column causing multicollinearity" logic to the caller. |
| 683 |
char cc = '1'; |
| 684 |
double norm = dlange_(&cc, &dim, &dim, matrix, &dim, dbl_2d_buf); |
| 685 |
__CLPK_integer info; |
| 686 |
dgetrf_(&dim, &dim, matrix, &dim, int_1d_buf, &info); |
| 687 |
if (info > 0) { |
| 688 |
return 1; |
| 689 |
} |
| 690 |
double rcond; |
| 691 |
dgecon_(&cc, &dim, matrix, &dim, &norm, &rcond, dbl_2d_buf, &(int_1d_buf[S_CAST(uint32_t, dim)]), &info); |
| 692 |
if (rcond < kMatrixSingularRcond) { |
| 693 |
return 1; |
| 694 |
} |
| 695 |
__CLPK_integer lwork = dim * dim; |
| 696 |
dgetri_(&dim, matrix, &dim, int_1d_buf, dbl_2d_buf, &lwork, &info); |
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
BoolErr InvertSymmdefMatrix(__CLPK_integer dim, double* matrix, __maybe_unused MatrixInvertBuf1* int_1d_buf, __maybe_unused double* dbl_2d_buf) { |
| 701 |
char uplo = 'U'; |
| 702 |
__CLPK_integer info; |
| 703 |
dpotrf_(&uplo, &dim, matrix, &dim, &info); |
| 704 |
if (info) { |
| 705 |
return 1; |
| 706 |
} |
| 707 |
dpotri_(&uplo, &dim, matrix, &dim, &info); |
| 708 |
assert(info == 0); |
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
BoolErr InvertSymmdefStridedMatrix(__CLPK_integer dim, __CLPK_integer stride, double* matrix, __maybe_unused MatrixInvertBuf1* int_1d_buf, __maybe_unused double* dbl_2d_buf) { |
| 713 |
char uplo = 'U'; |
| 714 |
__CLPK_integer info; |
| 715 |
dpotrf_(&uplo, &dim, matrix, &stride, &info); |
| 716 |
if (info) { |
| 717 |
return 1; |
| 718 |
} |
| 719 |
dpotri_(&uplo, &dim, matrix, &stride, &info); |
| 720 |
assert(info == 0); |
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
BoolErr InvertSymmdefMatrixChecked(__CLPK_integer dim, double* matrix, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 725 |
char cc = '1'; |
| 726 |
char uplo = 'U'; |
| 727 |
double norm = dlansy_(&cc, &uplo, &dim, matrix, &dim, dbl_2d_buf); |
| 728 |
__CLPK_integer info; |
| 729 |
dpotrf_(&uplo, &dim, matrix, &dim, &info); |
| 730 |
if (info > 0) { |
| 731 |
return 1; |
| 732 |
} |
| 733 |
double rcond; |
| 734 |
dpocon_(&uplo, &dim, matrix, &dim, &norm, &rcond, dbl_2d_buf, int_1d_buf, &info); |
| 735 |
if (rcond < kMatrixSingularRcond) { |
| 736 |
return 1; |
| 737 |
} |
| 738 |
dpotri_(&uplo, &dim, matrix, &dim, &info); |
| 739 |
return 0; |
| 740 |
} |
| 741 |
|
| 742 |
BoolErr InvertSymmdefMatrixFirstHalf(__CLPK_integer dim, __CLPK_integer stride, double* matrix, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 743 |
char cc = '1'; |
| 744 |
char uplo = 'U'; |
| 745 |
double norm = dlansy_(&cc, &uplo, &dim, matrix, &stride, dbl_2d_buf); |
| 746 |
__CLPK_integer info; |
| 747 |
dpotrf_(&uplo, &dim, matrix, &stride, &info); |
| 748 |
if (info > 0) { |
| 749 |
return 1; |
| 750 |
} |
| 751 |
double rcond; |
| 752 |
dpocon_(&uplo, &dim, matrix, &stride, &norm, &rcond, dbl_2d_buf, int_1d_buf, &info); |
| 753 |
return (rcond < kMatrixSingularRcond); |
| 754 |
} |
| 755 |
|
| 756 |
// quasi-bugfix (20 Sep 2017): give up on doing this with single-precision |
| 757 |
// numbers. Instead, convert to double-precision, then perform the usual |
| 758 |
// inversion, then downcode back to single-precision. |
| 759 |
BoolErr InvertFmatrixFirstHalf(__CLPK_integer dim, uint32_t stride, const float* matrix, double* half_inverted, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 760 |
const float* read_row = matrix; |
| 761 |
double* write_row = half_inverted; |
| 762 |
for (uint32_t row_idx = 0; row_idx != S_CAST(uint32_t, dim); ++row_idx) { |
| 763 |
// could use _mm256_cvtps_pd() here |
| 764 |
for (uint32_t col_idx = 0; col_idx != S_CAST(uint32_t, dim); ++col_idx) { |
| 765 |
write_row[col_idx] = S_CAST(double, read_row[col_idx]); |
| 766 |
} |
| 767 |
read_row = &(read_row[stride]); |
| 768 |
write_row = &(write_row[S_CAST(uint32_t, dim)]); |
| 769 |
} |
| 770 |
|
| 771 |
char cc = '1'; |
| 772 |
double norm = dlange_(&cc, &dim, &dim, half_inverted, &dim, dbl_2d_buf); |
| 773 |
__CLPK_integer info; |
| 774 |
dgetrf_(&dim, &dim, half_inverted, &dim, int_1d_buf, &info); |
| 775 |
if (info > 0) { |
| 776 |
return 1; |
| 777 |
} |
| 778 |
double rcond; |
| 779 |
dgecon_(&cc, &dim, half_inverted, &dim, &norm, &rcond, dbl_2d_buf, &(int_1d_buf[S_CAST(uint32_t, dim)]), &info); |
| 780 |
return (rcond < kMatrixSingularRcond); |
| 781 |
} |
| 782 |
|
| 783 |
BoolErr InvertSymmdefFmatrixFirstHalf(__CLPK_integer dim, uint32_t stride, float* matrix, double* half_inverted, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 784 |
const float* read_row = matrix; |
| 785 |
double* write_row = half_inverted; |
| 786 |
for (uint32_t row_idx = 0; row_idx != S_CAST(uint32_t, dim); ++row_idx) { |
| 787 |
// could use _mm256_cvtps_pd() here |
| 788 |
for (uint32_t col_idx = 0; col_idx <= row_idx; ++col_idx) { |
| 789 |
write_row[col_idx] = S_CAST(double, read_row[col_idx]); |
| 790 |
} |
| 791 |
read_row = &(read_row[stride]); |
| 792 |
write_row = &(write_row[S_CAST(uint32_t, dim)]); |
| 793 |
} |
| 794 |
|
| 795 |
char cc = '1'; |
| 796 |
char uplo = 'U'; |
| 797 |
double norm = dlansy_(&cc, &uplo, &dim, half_inverted, &dim, dbl_2d_buf); |
| 798 |
__CLPK_integer info; |
| 799 |
dpotrf_(&uplo, &dim, half_inverted, &dim, &info); |
| 800 |
if (info > 0) { |
| 801 |
return 1; |
| 802 |
} |
| 803 |
double rcond; |
| 804 |
dpocon_(&uplo, &dim, half_inverted, &dim, &norm, &rcond, dbl_2d_buf, int_1d_buf, &info); |
| 805 |
return (rcond < kMatrixSingularRcond); |
| 806 |
} |
| 807 |
|
| 808 |
void InvertFmatrixSecondHalf(__CLPK_integer dim, uint32_t stride, double* half_inverted, float* inverted_result, MatrixInvertBuf1* int_1d_buf, double* dbl_2d_buf) { |
| 809 |
__CLPK_integer lwork = dim * dim; |
| 810 |
__CLPK_integer info; |
| 811 |
dgetri_(&dim, half_inverted, &dim, int_1d_buf, dbl_2d_buf, &lwork, &info); |
| 812 |
const double* read_row = half_inverted; |
| 813 |
float* write_row = inverted_result; |
| 814 |
for (uint32_t row_idx = 0; row_idx != S_CAST(uint32_t, dim); ++row_idx) { |
| 815 |
// could use _mm256_cvtpd_ps() here |
| 816 |
for (uint32_t col_idx = 0; col_idx != S_CAST(uint32_t, dim); ++col_idx) { |
| 817 |
write_row[col_idx] = S_CAST(float, read_row[col_idx]); |
| 818 |
} |
| 819 |
read_row = &(read_row[S_CAST(uint32_t, dim)]); |
| 820 |
write_row = &(write_row[stride]); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
void InvertSymmdefFmatrixSecondHalf(__CLPK_integer dim, uint32_t stride, double* half_inverted, float* inverted_result, __maybe_unused MatrixInvertBuf1* int_1d_buf, __maybe_unused double* dbl_2d_buf) { |
| 825 |
char uplo = 'U'; |
| 826 |
__CLPK_integer info; |
| 827 |
dpotri_(&uplo, &dim, half_inverted, &dim, &info); |
| 828 |
const double* read_row = half_inverted; |
| 829 |
float* write_row = inverted_result; |
| 830 |
for (uint32_t row_idx = 0; row_idx != S_CAST(uint32_t, dim); ++row_idx) { |
| 831 |
// could use _mm256_cvtpd_ps() here |
| 832 |
for (uint32_t col_idx = 0; col_idx <= row_idx; ++col_idx) { |
| 833 |
write_row[col_idx] = S_CAST(float, read_row[col_idx]); |
| 834 |
} |
| 835 |
read_row = &(read_row[S_CAST(uint32_t, dim)]); |
| 836 |
write_row = &(write_row[stride]); |
| 837 |
} |
| 838 |
} |
| 839 |
#endif // !NOLAPACK |
| 840 |
|
| 841 |
void ColMajorMatrixMultiply(const double* inmatrix1, const double* inmatrix2, __CLPK_integer row1_ct, __CLPK_integer col2_ct, __CLPK_integer common_ct, double* outmatrix) { |
| 842 |
#ifdef NOLAPACK |
| 843 |
const uintptr_t row1_ct_l = row1_ct; |
| 844 |
const uintptr_t col2_ct_l = col2_ct; |
| 845 |
const uintptr_t common_ct_l = common_ct; |
| 846 |
// not optimized |
| 847 |
for (uintptr_t col_idx = 0; col_idx != col2_ct_l; ++col_idx) { |
| 848 |
for (uintptr_t row_idx = 0; row_idx != row1_ct_l; ++row_idx) { |
| 849 |
double cur_dotprod = 0.0; |
| 850 |
const double* dptr = &(inmatrix2[col_idx * common_ct]); |
| 851 |
for (uintptr_t com_idx = 0; com_idx != common_ct_l; ++com_idx) { |
| 852 |
cur_dotprod += (*dptr++) * inmatrix1[com_idx * row1_ct_l + row_idx]; |
| 853 |
} |
| 854 |
*outmatrix++ = cur_dotprod; |
| 855 |
} |
| 856 |
} |
| 857 |
#else |
| 858 |
# ifndef USE_CBLAS_XGEMM |
| 859 |
char blas_char = 'N'; |
| 860 |
double dyy = 1; |
| 861 |
double dzz = 0; |
| 862 |
dgemm_(&blas_char, &blas_char, &row1_ct, &col2_ct, &common_ct, &dyy, K_CAST(double*, inmatrix1), &row1_ct, K_CAST(double*, inmatrix2), &common_ct, &dzz, outmatrix, &row1_ct); |
| 863 |
# else |
| 864 |
// bugfix (30 Aug 2017): this fails on OS X when LDB > sqrt(2^31). |
| 865 |
// update: Windows does not have the same problem |
| 866 |
// update 2 (6 Sep 2017): the OS X failure seems to have been driven by 128k |
| 867 |
// thread stack size; going up to the usual 512k appears to solve the |
| 868 |
// problem |
| 869 |
// #ifdef LAPACK_ILP64 |
| 870 |
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, row1_ct, col2_ct, common_ct, 1.0, inmatrix1, row1_ct, inmatrix2, common_ct, 0.0, outmatrix, row1_ct); |
| 871 |
/* |
| 872 |
# else |
| 873 |
if (common_ct <= 46340) { |
| 874 |
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, row1_ct, col2_ct, common_ct, 1.0, inmatrix1, row1_ct, inmatrix2, common_ct, 0.0, outmatrix, row1_ct); |
| 875 |
return; |
| 876 |
} |
| 877 |
for (__CLPK_integer col_idx = 0; col_idx != col2_ct; ++col_idx) { |
| 878 |
cblas_dgemv(CblasColMajor, CblasNoTrans, row1_ct, common_ct, 1.0, inmatrix1, row1_ct, inmatrix2, 1, 0.0, outmatrix, 1); |
| 879 |
inmatrix2 = &(inmatrix2[(uint32_t)common_ct]); |
| 880 |
outmatrix = &(outmatrix[(uint32_t)row1_ct]); |
| 881 |
} |
| 882 |
# endif |
| 883 |
*/ |
| 884 |
# endif // USE_CBLAS_XGEMM |
| 885 |
#endif // !NOLAPACK |
| 886 |
} |
| 887 |
|
| 888 |
void ColMajorMatrixMultiplyStridedAddassign(const double* inmatrix1, const double* inmatrix2, __CLPK_integer row1_ct, __CLPK_integer stride1, __CLPK_integer col2_ct, __CLPK_integer stride2, __CLPK_integer common_ct, __CLPK_integer stride3, double beta, double* outmatrix) { |
| 889 |
// stride1 should be close to row1_ct |
| 890 |
// stride2 should be close to common_ct |
| 891 |
// output matrix uses stride3, which should be close to row1_ct |
| 892 |
#ifdef NOLAPACK |
| 893 |
const uintptr_t row1_ct_l = row1_ct; |
| 894 |
const uintptr_t col2_ct_l = col2_ct; |
| 895 |
const uintptr_t common_ct_l = common_ct; |
| 896 |
// not optimized, no beta == 0 special case |
| 897 |
for (uintptr_t col_idx = 0; col_idx != col2_ct_l; ++col_idx) { |
| 898 |
double* outmatrix_row_iter = &(outmatrix[col_idx * stride3]); |
| 899 |
for (uintptr_t row_idx = 0; row_idx != row1_ct_l; ++row_idx) { |
| 900 |
double cur_entry = 0.0; |
| 901 |
const double* col2_iter = &(inmatrix2[col_idx * stride2]); |
| 902 |
for (uintptr_t com_idx = 0; com_idx != common_ct_l; com_idx++) { |
| 903 |
cur_entry += (*col2_iter++) * inmatrix1[com_idx * stride1 + row_idx]; |
| 904 |
} |
| 905 |
*outmatrix_row_iter = (*outmatrix_row_iter) * beta + cur_entry; |
| 906 |
++outmatrix_row_iter; |
| 907 |
} |
| 908 |
} |
| 909 |
#else |
| 910 |
# ifndef USE_CBLAS_XGEMM |
| 911 |
char blas_char = 'N'; |
| 912 |
double alpha = 1; |
| 913 |
dgemm_(&blas_char, &blas_char, &row1_ct, &col2_ct, &common_ct, &alpha, K_CAST(double*, inmatrix1), &stride1, K_CAST(double*, inmatrix2), &stride2, &beta, outmatrix, &stride3); |
| 914 |
# else |
| 915 |
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, row1_ct, col2_ct, common_ct, 1.0, inmatrix1, stride1, inmatrix2, stride2, beta, outmatrix, stride3); |
| 916 |
# endif // USE_CBLAS_XGEMM |
| 917 |
#endif // !NOLAPACK |
| 918 |
} |
| 919 |
|
| 920 |
void ColMajorVectorMatrixMultiplyStrided(const double* in_dvec1, const double* inmatrix2, __CLPK_integer common_ct, __CLPK_integer stride2, __CLPK_integer col2_ct, double* out_dvec) { |
| 921 |
#ifdef NOLAPACK |
| 922 |
const uintptr_t col2_ct_l = col2_ct; |
| 923 |
const uintptr_t common_ct_l = common_ct; |
| 924 |
const uintptr_t stride_l = stride2; |
| 925 |
for (uintptr_t col_idx = 0; col_idx != col2_ct_l; ++col_idx) { |
| 926 |
double dxx = 0.0; |
| 927 |
const double* cur_col = &(inmatrix2[col_idx * stride_l]); |
| 928 |
for (uintptr_t row_idx = 0; row_idx != common_ct_l; ++row_idx) { |
| 929 |
dxx += in_dvec1[row_idx] * cur_col[row_idx]; |
| 930 |
} |
| 931 |
out_dvec[col_idx] = dxx; |
| 932 |
} |
| 933 |
#else |
| 934 |
# ifndef USE_CBLAS_XGEMM |
| 935 |
char trans = 'T'; |
| 936 |
__CLPK_integer incxy = 1; |
| 937 |
double dyy = 1; |
| 938 |
double dzz = 0; |
| 939 |
dgemv_(&trans, &common_ct, &col2_ct, &dyy, K_CAST(double*, inmatrix2), &stride2, K_CAST(double*, in_dvec1), &incxy, &dzz, out_dvec, &incxy); |
| 940 |
# else |
| 941 |
cblas_dgemv(CblasColMajor, CblasTrans, common_ct, col2_ct, 1.0, inmatrix2, stride2, in_dvec1, 1, 0.0, out_dvec, 1); |
| 942 |
# endif // USE_CBLAS_XGEMM |
| 943 |
#endif // !NOLAPACK |
| 944 |
} |
| 945 |
|
| 946 |
void ColMajorMatrixVectorMultiplyStrided(const double* inmatrix1, const double* in_dvec2, __CLPK_integer row1_ct, __CLPK_integer stride1, __CLPK_integer common_ct, double* out_dvec) { |
| 947 |
#ifdef NOLAPACK |
| 948 |
ZeroDArr(row1_ct, out_dvec); |
| 949 |
const uintptr_t row1_ct_l = row1_ct; |
| 950 |
const uintptr_t common_ct_l = common_ct; |
| 951 |
const uintptr_t stride_l = stride1; |
| 952 |
const double* col_iter = inmatrix1; |
| 953 |
for (uintptr_t common_idx = 0; common_idx != common_ct_l; ++common_idx) { |
| 954 |
const double dxx = in_dvec2[common_idx]; |
| 955 |
for (uintptr_t row1_idx = 0; row1_idx != row1_ct_l; ++row1_idx) { |
| 956 |
out_dvec[row1_idx] += col_iter[row1_idx] * dxx; |
| 957 |
} |
| 958 |
col_iter = &(col_iter[stride_l]); |
| 959 |
} |
| 960 |
#else |
| 961 |
# ifndef USE_CBLAS_XGEMM |
| 962 |
char trans = 'N'; |
| 963 |
__CLPK_integer incxy = 1; |
| 964 |
double dyy = 1; |
| 965 |
double dzz = 0; |
| 966 |
dgemv_(&trans, &row1_ct, &common_ct, &dyy, K_CAST(double*, inmatrix1), &stride1, K_CAST(double*, in_dvec2), &incxy, &dzz, out_dvec, &incxy); |
| 967 |
# else |
| 968 |
cblas_dgemv(CblasColMajor, CblasNoTrans, row1_ct, common_ct, 1.0, inmatrix1, stride1, in_dvec2, 1, 0.0, out_dvec, 1); |
| 969 |
# endif // USE_CBLAS_XGEMM |
| 970 |
#endif // !NOLAPACK |
| 971 |
} |
| 972 |
|
| 973 |
// er, should make this _addassign for consistency... |
| 974 |
void ColMajorFmatrixMultiplyStrided(const float* inmatrix1, const float* inmatrix2, __CLPK_integer row1_ct, __CLPK_integer stride1, __CLPK_integer col2_ct, __CLPK_integer stride2, __CLPK_integer common_ct, __CLPK_integer stride3, float* outmatrix) { |
| 975 |
#ifdef NOLAPACK |
| 976 |
const uintptr_t row1_ct_l = row1_ct; |
| 977 |
const uintptr_t col2_ct_l = col2_ct; |
| 978 |
const uintptr_t common_ct_l = common_ct; |
| 979 |
// not optimized |
| 980 |
for (uintptr_t col_idx = 0; col_idx != col2_ct_l; ++col_idx) { |
| 981 |
float* outmatrix_row_iter = &(outmatrix[col_idx * stride3]); |
| 982 |
for (uintptr_t row_idx = 0; row_idx != row1_ct_l; ++row_idx) { |
| 983 |
float cur_entry = 0.0; |
| 984 |
const float* col2_iter = &(inmatrix2[col_idx * stride2]); |
| 985 |
for (uintptr_t com_idx = 0; com_idx != common_ct_l; com_idx++) { |
| 986 |
cur_entry += (*col2_iter++) * inmatrix1[com_idx * stride1 + row_idx]; |
| 987 |
} |
| 988 |
*outmatrix_row_iter++ = cur_entry; |
| 989 |
} |
| 990 |
} |
| 991 |
#else |
| 992 |
# ifndef USE_CBLAS_XGEMM |
| 993 |
char blas_char = 'N'; |
| 994 |
float fyy = 1; |
| 995 |
float fzz = 0; |
| 996 |
sgemm_(&blas_char, &blas_char, &row1_ct, &col2_ct, &common_ct, &fyy, K_CAST(float*, inmatrix1), &stride1, K_CAST(float*, inmatrix2), &stride2, &fzz, outmatrix, &stride3); |
| 997 |
# else |
| 998 |
cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, row1_ct, col2_ct, common_ct, 1.0, inmatrix1, stride1, inmatrix2, stride2, 0.0, outmatrix, stride3); |
| 999 |
# endif // USE_CBLAS_XGEMM |
| 1000 |
#endif // !NOLAPACK |
| 1001 |
} |
| 1002 |
|
| 1003 |
void ColMajorFmatrixVectorMultiplyStrided(const float* inmatrix1, const float* in_fvec2, __CLPK_integer row1_ct, __CLPK_integer stride1, __CLPK_integer common_ct, float* out_fvec) { |
| 1004 |
#ifdef NOLAPACK |
| 1005 |
ZeroFArr(row1_ct, out_fvec); |
| 1006 |
const uintptr_t row1_ct_l = row1_ct; |
| 1007 |
const uintptr_t common_ct_l = common_ct; |
| 1008 |
const uintptr_t stride_l = stride1; |
| 1009 |
for (uintptr_t common_idx = 0; common_idx != common_ct_l; ++common_idx) { |
| 1010 |
const float fxx = in_fvec2[common_idx]; |
| 1011 |
const float* col_iter = &(inmatrix1[common_idx * stride_l]); |
| 1012 |
for (uintptr_t row1_idx = 0; row1_idx != row1_ct_l; ++row1_idx) { |
| 1013 |
out_fvec[row1_idx] += (*col_iter++) * fxx; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
#else |
| 1017 |
# ifndef USE_CBLAS_XGEMM |
| 1018 |
char trans = 'N'; |
| 1019 |
__CLPK_integer incxy = 1; |
| 1020 |
float fyy = 1; |
| 1021 |
float fzz = 0; |
| 1022 |
sgemv_(&trans, &row1_ct, &common_ct, &fyy, K_CAST(float*, inmatrix1), &stride1, K_CAST(float*, in_fvec2), &incxy, &fzz, out_fvec, &incxy); |
| 1023 |
# else |
| 1024 |
cblas_sgemv(CblasColMajor, CblasNoTrans, row1_ct, common_ct, 1.0, inmatrix1, stride1, in_fvec2, 1, 0.0, out_fvec, 1); |
| 1025 |
# endif // USE_CBLAS_XGEMM |
| 1026 |
#endif // !NOLAPACK |
| 1027 |
} |
| 1028 |
|
| 1029 |
void ColMajorFvectorMatrixMultiplyStrided(const float* in_fvec1, const float* inmatrix2, __CLPK_integer common_ct, __CLPK_integer stride2, __CLPK_integer col2_ct, float* out_fvec) { |
| 1030 |
#ifdef NOLAPACK |
| 1031 |
const uintptr_t col2_ct_l = col2_ct; |
| 1032 |
const uintptr_t common_ct_l = common_ct; |
| 1033 |
const uintptr_t stride_l = stride2; |
| 1034 |
for (uintptr_t col_idx = 0; col_idx != col2_ct_l; ++col_idx) { |
| 1035 |
float fxx = 0.0; |
| 1036 |
const float* cur_col = &(inmatrix2[col_idx * stride_l]); |
| 1037 |
for (uintptr_t row_idx = 0; row_idx != common_ct_l; ++row_idx) { |
| 1038 |
fxx += in_fvec1[row_idx] * cur_col[row_idx]; |
| 1039 |
} |
| 1040 |
out_fvec[col_idx] = fxx; |
| 1041 |
} |
| 1042 |
#else |
| 1043 |
# ifndef USE_CBLAS_XGEMM |
| 1044 |
char trans = 'T'; |
| 1045 |
__CLPK_integer incxy = 1; |
| 1046 |
float fyy = 1; |
| 1047 |
float fzz = 0; |
| 1048 |
sgemv_(&trans, &common_ct, &col2_ct, &fyy, K_CAST(float*, inmatrix2), &stride2, K_CAST(float*, in_fvec1), &incxy, &fzz, out_fvec, &incxy); |
| 1049 |
# else |
| 1050 |
cblas_sgemv(CblasColMajor, CblasTrans, common_ct, col2_ct, 1.0, inmatrix2, stride2, in_fvec1, 1, 0.0, out_fvec, 1); |
| 1051 |
# endif // USE_CBLAS_XGEMM |
| 1052 |
#endif // !NOLAPACK |
| 1053 |
} |
| 1054 |
|
| 1055 |
// Briefly experimented with trying to speed this up, didn't make any progress. |
| 1056 |
// Tried vDSP_mtransD() on macOS and mkl_domatcopy() on Linux, neither of those |
| 1057 |
// helped, so it's likely there's no progress to be made. |
| 1058 |
void MatrixTransposeCopy(const double* old_matrix, uint32_t old_maj, uint32_t new_maj, double* new_matrix_iter) { |
| 1059 |
for (uint32_t new_maj_idx = 0; new_maj_idx != new_maj; ++new_maj_idx) { |
| 1060 |
const double* old_matrix_col_iter = &(old_matrix[new_maj_idx]); |
| 1061 |
for (uint32_t old_maj_idx = 0; old_maj_idx != old_maj; ++old_maj_idx) { |
| 1062 |
*new_matrix_iter++ = *old_matrix_col_iter; |
| 1063 |
old_matrix_col_iter = &(old_matrix_col_iter[new_maj]); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
void FmatrixTransposeCopy(const float* old_matrix, uint32_t old_maj, uint32_t new_maj, uint32_t new_maj_max, float* new_matrix_iter) { |
| 1069 |
// new_maj = in-memory stride of old_matrix rows |
| 1070 |
// new_maj_max = actual number of rows in new_matrix |
| 1071 |
// (distinction is necessary for SSE alignment) |
| 1072 |
for (uint32_t new_maj_idx = 0; new_maj_idx != new_maj_max; ++new_maj_idx) { |
| 1073 |
const float* old_matrix_col_iter = &(old_matrix[new_maj_idx]); |
| 1074 |
for (uint32_t old_maj_idx = 0; old_maj_idx != old_maj; ++old_maj_idx) { |
| 1075 |
*new_matrix_iter++ = *old_matrix_col_iter; |
| 1076 |
old_matrix_col_iter = &(old_matrix_col_iter[new_maj]); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
/* |
| 1082 |
__CLPK_integer qr_square_factor_float_get_lwork(uint32_t dim) { |
| 1083 |
__CLPK_integer dim_i = (__CLPK_integer)dim; |
| 1084 |
__CLPK_integer sgeqrf_lwork = -1; |
| 1085 |
float work[1]; |
| 1086 |
__CLPK_integer info; |
| 1087 |
float dummy; |
| 1088 |
float dummy2; |
| 1089 |
sgeqrf_(&dim_i, &dim_i, &dummy, &dim_i, &dummy2, work, &sgeqrf_lwork, &info); |
| 1090 |
assert(info == 0); |
| 1091 |
sgeqrf_lwork = (__CLPK_integer)work[0]; |
| 1092 |
__CLPK_integer sorgqr_lwork = -1; |
| 1093 |
sorgqr_(&dim_i, &dim_i, &dim_i, &dummy, &dim_i, &dummy2, work, &sorgqr_lwork, &info); |
| 1094 |
assert(info == 0); |
| 1095 |
sorgqr_lwork = (__CLPK_integer)work[0]; |
| 1096 |
return MAXV(sgeqrf_lwork, sorgqr_lwork); |
| 1097 |
} |
| 1098 |
|
| 1099 |
BoolErr qr_square_factor_float(const float* input_matrix, uint32_t dim, uintptr_t stride, __CLPK_integer lwork, float* qq, float* r_determinant_ptr, float* tau_buf, float* work_buf) { |
| 1100 |
// only returns Q and, optionally, the product of R's diagonal entries (which |
| 1101 |
// should be the determinant of the original matrix). |
| 1102 |
// tau_buf should have space for dim entries |
| 1103 |
assert(dim > 0); |
| 1104 |
if (dim == stride) { |
| 1105 |
memcpy(qq, input_matrix, dim * ((uintptr_t)dim) * sizeof(float)); |
| 1106 |
} else { |
| 1107 |
for (uintptr_t col_idx = 0; col_idx != dim; ++col_idx) { |
| 1108 |
memcpy(&(qq[col_idx * dim]), &(input_matrix[col_idx * stride]), dim * sizeof(float)); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
__CLPK_integer dim_i = (__CLPK_integer)dim; |
| 1112 |
__CLPK_integer info; |
| 1113 |
sgeqrf_(&dim_i, &dim_i, qq, &dim_i, tau_buf, work_buf, &lwork, &info); |
| 1114 |
if (info != 0) { |
| 1115 |
return 1; |
| 1116 |
} |
| 1117 |
if (r_determinant_ptr) { |
| 1118 |
const uintptr_t dimp1 = dim + 1; |
| 1119 |
float prod = qq[0]; |
| 1120 |
for (uintptr_t col_idx = 1; col_idx != dim; ++col_idx) { |
| 1121 |
prod *= qq[col_idx * dimp1]; |
| 1122 |
} |
| 1123 |
*r_determinant_ptr = prod; |
| 1124 |
} |
| 1125 |
sorgqr_(&dim_i, &dim_i, &dim_i, qq, &dim_i, tau_buf, work_buf, &lwork, &info); |
| 1126 |
if (info != 0) { |
| 1127 |
return 1; |
| 1128 |
} |
| 1129 |
return 0; |
| 1130 |
} |
| 1131 |
*/ |
| 1132 |
|
| 1133 |
// A(A^T), where A is row-major; result is dim x dim |
| 1134 |
// ONLY UPDATES LOWER TRIANGLE OF result[]. |
| 1135 |
void MultiplySelfTranspose(const double* input_matrix, uint32_t dim, uint32_t col_ct, double* result) { |
| 1136 |
#ifdef NOLAPACK |
| 1137 |
for (uintptr_t row1_idx = 0; row1_idx != dim; ++row1_idx) { |
| 1138 |
const double* pred_row1 = &(input_matrix[row1_idx * col_ct]); |
| 1139 |
double* result_row = &(result[row1_idx * dim]); |
| 1140 |
for (uintptr_t row2_idx = 0; row2_idx <= row1_idx; ++row2_idx) { |
| 1141 |
const double* pred_row2 = &(input_matrix[row2_idx * col_ct]); |
| 1142 |
result_row[row2_idx] = DotprodD(pred_row1, pred_row2, col_ct); |
| 1143 |
} |
| 1144 |
} |
| 1145 |
#else |
| 1146 |
# ifndef USE_CBLAS_XGEMM |
| 1147 |
char uplo = 'U'; |
| 1148 |
char trans = 'T'; |
| 1149 |
__CLPK_integer tmp_n = dim; |
| 1150 |
__CLPK_integer tmp_k = col_ct; |
| 1151 |
double alpha = 1.0; |
| 1152 |
double beta = 0.0; |
| 1153 |
dsyrk_(&uplo, &trans, &tmp_n, &tmp_k, &alpha, K_CAST(double*, input_matrix), &tmp_k, &beta, result, &tmp_n); |
| 1154 |
# else |
| 1155 |
// see ColMajorMatrixMultiply() remarks; same OS X issue here. |
| 1156 |
// #ifdef LAPACK_ILP64 |
| 1157 |
cblas_dsyrk(CblasColMajor, CblasUpper, CblasTrans, dim, col_ct, 1.0, input_matrix, col_ct, 0.0, result, dim); |
| 1158 |
/* |
| 1159 |
# else |
| 1160 |
if (col_ct <= 46340) { |
| 1161 |
// bugfix (30 Aug 2017): for LDA > sqrt(2^31), this fails on OS X. |
| 1162 |
cblas_dsyrk(CblasColMajor, CblasUpper, CblasTrans, dim, col_ct, 1.0, input_matrix, col_ct, 0.0, result, dim); |
| 1163 |
return; |
| 1164 |
} |
| 1165 |
// this actually seems to be faster than dsyrk for small dim (at least on OS |
| 1166 |
// X), but that might change in the future so I won't "optimize" dispatch |
| 1167 |
for (uintptr_t row1_idx = 0; row1_idx != dim; ++row1_idx) { |
| 1168 |
const double* pred_row1 = &(input_matrix[row1_idx * col_ct]); |
| 1169 |
double* result_row = &(result[row1_idx * dim]); |
| 1170 |
for (uintptr_t row2_idx = 0; row2_idx <= row1_idx; ++row2_idx) { |
| 1171 |
const double* pred_row2 = &(input_matrix[row2_idx * col_ct]); |
| 1172 |
result_row[row2_idx] = DotprodD(pred_row1, pred_row2, col_ct); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
# endif |
| 1176 |
*/ |
| 1177 |
# endif |
| 1178 |
#endif |
| 1179 |
} |
| 1180 |
|
| 1181 |
void MultiplySelfTransposeStrided(const double* input_matrix, uint32_t dim, uint32_t col_ct, uint32_t stride, double* result) { |
| 1182 |
#ifdef NOLAPACK |
| 1183 |
for (uintptr_t row1_idx = 0; row1_idx != dim; ++row1_idx) { |
| 1184 |
const double* pred_row1 = &(input_matrix[row1_idx * stride]); |
| 1185 |
double* result_row = &(result[row1_idx * dim]); |
| 1186 |
for (uintptr_t row2_idx = 0; row2_idx <= row1_idx; ++row2_idx) { |
| 1187 |
const double* pred_row2 = &(input_matrix[row2_idx * stride]); |
| 1188 |
result_row[row2_idx] = DotprodD(pred_row1, pred_row2, col_ct); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
#else |
| 1192 |
# ifndef USE_CBLAS_XGEMM |
| 1193 |
char uplo = 'U'; |
| 1194 |
char trans = 'T'; |
| 1195 |
__CLPK_integer tmp_n = dim; |
| 1196 |
__CLPK_integer tmp_k = col_ct; |
| 1197 |
__CLPK_integer tmp_lda = stride; |
| 1198 |
double alpha = 1.0; |
| 1199 |
double beta = 0.0; |
| 1200 |
dsyrk_(&uplo, &trans, &tmp_n, &tmp_k, &alpha, K_CAST(double*, input_matrix), &tmp_lda, &beta, result, &tmp_n); |
| 1201 |
# else |
| 1202 |
cblas_dsyrk(CblasColMajor, CblasUpper, CblasTrans, dim, col_ct, 1.0, input_matrix, stride, 0.0, result, dim); |
| 1203 |
# endif |
| 1204 |
#endif |
| 1205 |
} |
| 1206 |
|
| 1207 |
void MultiplySelfTransposeStridedF(const float* input_matrix, uint32_t dim, uint32_t col_ct, uint32_t stride, float* result) { |
| 1208 |
#ifdef NOLAPACK |
| 1209 |
for (uintptr_t row1_idx = 0; row1_idx != dim; ++row1_idx) { |
| 1210 |
const float* pred_row1 = &(input_matrix[row1_idx * stride]); |
| 1211 |
float* result_row = &(result[row1_idx * dim]); |
| 1212 |
for (uintptr_t row2_idx = 0; row2_idx <= row1_idx; ++row2_idx) { |
| 1213 |
const float* pred_row2 = &(input_matrix[row2_idx * stride]); |
| 1214 |
result_row[row2_idx] = DotprodF(pred_row1, pred_row2, col_ct); |
| 1215 |
} |
| 1216 |
} |
| 1217 |
#else |
| 1218 |
# ifndef USE_CBLAS_XGEMM |
| 1219 |
char uplo = 'U'; |
| 1220 |
char trans = 'T'; |
| 1221 |
__CLPK_integer tmp_n = dim; |
| 1222 |
__CLPK_integer tmp_k = col_ct; |
| 1223 |
__CLPK_integer tmp_lda = stride; |
| 1224 |
float alpha = 1.0; |
| 1225 |
float beta = 0.0; |
| 1226 |
ssyrk_(&uplo, &trans, &tmp_n, &tmp_k, &alpha, K_CAST(float*, input_matrix), &tmp_lda, &beta, result, &tmp_n); |
| 1227 |
# else |
| 1228 |
cblas_ssyrk(CblasColMajor, CblasUpper, CblasTrans, dim, col_ct, 1.0, input_matrix, stride, 0.0, result, dim); |
| 1229 |
# endif |
| 1230 |
#endif |
| 1231 |
} |
| 1232 |
|
| 1233 |
void TransposeMultiplySelfIncr(double* input_part, uint32_t dim, uint32_t partial_row_ct, double* result) { |
| 1234 |
#ifdef NOLAPACK |
| 1235 |
// friends do not let friends use this implementation |
| 1236 |
const uintptr_t dim_l = dim; |
| 1237 |
const uintptr_t row_ct_l = partial_row_ct; |
| 1238 |
for (uintptr_t idx1 = 0; idx1 != dim_l; ++idx1) { |
| 1239 |
const double* col1 = &(input_part[idx1]); |
| 1240 |
double* write_iter = &(result[idx1 * dim_l]); |
| 1241 |
for (uintptr_t idx2 = 0; idx2 <= idx1; ++idx2) { |
| 1242 |
double cur_dotprod = *write_iter; |
| 1243 |
const double* col2 = &(input_part[idx2]); |
| 1244 |
for (uintptr_t row_idx = 0; row_idx != row_ct_l; ++row_idx) { |
| 1245 |
cur_dotprod += col1[row_idx * dim_l] * col2[row_idx * dim_l]; |
| 1246 |
} |
| 1247 |
*write_iter = cur_dotprod; |
| 1248 |
++write_iter; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
#else |
| 1252 |
# ifndef USE_CBLAS_XGEMM |
| 1253 |
char uplo = 'U'; |
| 1254 |
char trans = 'N'; |
| 1255 |
__CLPK_integer tmp_n = dim; |
| 1256 |
__CLPK_integer tmp_k = partial_row_ct; |
| 1257 |
double alpha = 1.0; |
| 1258 |
double beta = 1.0; |
| 1259 |
dsyrk_(&uplo, &trans, &tmp_n, &tmp_k, &alpha, input_part, &tmp_n, &beta, result, &tmp_n); |
| 1260 |
# else |
| 1261 |
cblas_dsyrk(CblasColMajor, CblasUpper, CblasNoTrans, dim, partial_row_ct, 1.0, input_part, dim, 1.0, result, dim); |
| 1262 |
# endif |
| 1263 |
#endif // !NOLAPACK |
| 1264 |
} |
| 1265 |
|
| 1266 |
#ifndef NOLAPACK |
| 1267 |
BoolErr GetSvdRectLwork(uint32_t major_ct, uint32_t minor_ct, __CLPK_integer* lwork_ptr) { |
| 1268 |
char jobu = 'S'; |
| 1269 |
char jobvt = 'O'; |
| 1270 |
__CLPK_integer tmp_m = minor_ct; |
| 1271 |
__CLPK_integer tmp_n = major_ct; |
| 1272 |
__CLPK_integer wkspace_size = -1; |
| 1273 |
double wkspace_size_d; |
| 1274 |
__CLPK_integer info; |
| 1275 |
dgesvd_(&jobu, &jobvt, &tmp_m, &tmp_n, nullptr, &tmp_m, nullptr, nullptr, &tmp_m, nullptr, &tmp_m, &wkspace_size_d, &wkspace_size, &info); |
| 1276 |
# ifdef LAPACK_ILP64 |
| 1277 |
if (unlikely(info)) { |
| 1278 |
return 1; |
| 1279 |
} |
| 1280 |
# else |
| 1281 |
if (unlikely(info || (wkspace_size_d > 2147483640.0))) { |
| 1282 |
return 1; |
| 1283 |
} |
| 1284 |
# endif |
| 1285 |
*lwork_ptr = RoundUpPow2(S_CAST(__CLPK_integer, wkspace_size_d), kCacheline / sizeof(double)); |
| 1286 |
return 0; |
| 1287 |
} |
| 1288 |
|
| 1289 |
IntErr SvdRect(uint32_t major_ct, uint32_t minor_ct, __CLPK_integer lwork, double* matrix, double* ss, double* vv, unsigned char* svd_rect_wkspace) { |
| 1290 |
double* work = R_CAST(double*, svd_rect_wkspace); |
| 1291 |
char jobu = 'S'; |
| 1292 |
char jobvt = 'O'; |
| 1293 |
__CLPK_integer tmp_m = minor_ct; |
| 1294 |
__CLPK_integer tmp_n = major_ct; |
| 1295 |
__CLPK_integer info; |
| 1296 |
dgesvd_(&jobu, &jobvt, &tmp_m, &tmp_n, matrix, &tmp_m, ss, vv, &tmp_m, nullptr, &tmp_m, work, &lwork, &info); |
| 1297 |
return S_CAST(IntErr, info); |
| 1298 |
} |
| 1299 |
|
| 1300 |
// dsyevr_ takes ~30% less time than dsyevd_ on OS X dev machine. todo: retest |
| 1301 |
// for Linux 64-bit MKL. |
| 1302 |
BoolErr GetExtractEigvecsLworks(uint32_t dim, uint32_t pc_ct, __CLPK_integer* lwork_ptr, __CLPK_integer* liwork_ptr, uintptr_t* wkspace_byte_ct_ptr) { |
| 1303 |
#ifndef LAPACK_ILP64 |
| 1304 |
if (unlikely(dim > 46340)) { |
| 1305 |
// (30 Dec 2024) maybe this still works on some systems despite matrix size |
| 1306 |
// being unrepresentable by __CLPK_integer? but it doesn't on macOS: |
| 1307 |
// https://groups.google.com/g/plink2-users/c/oteXlRFHdgk |
| 1308 |
return 1; |
| 1309 |
} |
| 1310 |
#endif |
| 1311 |
char jobz = 'V'; |
| 1312 |
char range = 'I'; |
| 1313 |
char uplo = 'U'; |
| 1314 |
__CLPK_integer tmp_n = dim; |
| 1315 |
__CLPK_integer il = dim + 1 - pc_ct; |
| 1316 |
__CLPK_integer iu = dim; |
| 1317 |
double abstol = -1.0; |
| 1318 |
__CLPK_integer lwork_dummy = -1; |
| 1319 |
|
| 1320 |
// defined to make macOS 13.3 happy. |
| 1321 |
double ignored_vl; |
| 1322 |
double ignored_vu; |
| 1323 |
__CLPK_integer ignored_m; |
| 1324 |
|
| 1325 |
double lwork_d; |
| 1326 |
__CLPK_integer liwork; |
| 1327 |
__CLPK_integer info; |
| 1328 |
dsyevr_(&jobz, &range, &uplo, &tmp_n, nullptr, &tmp_n, &ignored_vl, &ignored_vu, &il, &iu, &abstol, &ignored_m, nullptr, nullptr, &tmp_n, nullptr, &lwork_d, &lwork_dummy, &liwork, &lwork_dummy, &info); |
| 1329 |
#ifdef LAPACK_ILP64 |
| 1330 |
if (unlikely(info)) { |
| 1331 |
return 1; |
| 1332 |
} |
| 1333 |
#else |
| 1334 |
if (unlikely(info || (lwork_d > 2147483648.0 - (kCacheline / sizeof(double))))) { |
| 1335 |
return 1; |
| 1336 |
} |
| 1337 |
#endif |
| 1338 |
const __CLPK_integer lwork = RoundUpPow2(S_CAST(__CLPK_integer, lwork_d), kCacheline / sizeof(double)); |
| 1339 |
liwork = RoundUpPow2(liwork, kCacheline / sizeof(__CLPK_integer)); |
| 1340 |
*lwork_ptr = lwork; |
| 1341 |
*liwork_ptr = liwork; |
| 1342 |
*wkspace_byte_ct_ptr = lwork * sizeof(double) + liwork * sizeof(__CLPK_integer) + RoundUpPow2(2 * dim * sizeof(__CLPK_integer), kCacheline); |
| 1343 |
return 0; |
| 1344 |
} |
| 1345 |
|
| 1346 |
BoolErr ExtractEigvecs(uint32_t dim, uint32_t pc_ct, __CLPK_integer lwork, __CLPK_integer liwork, double* matrix, double* eigvals, double* reverse_eigvecs, unsigned char* extract_eigvecs_wkspace) { |
| 1347 |
char jobz = 'V'; |
| 1348 |
char range = 'I'; |
| 1349 |
char uplo = 'U'; |
| 1350 |
__CLPK_integer tmp_n = dim; |
| 1351 |
__CLPK_integer il = dim + 1 - pc_ct; |
| 1352 |
__CLPK_integer iu = dim; |
| 1353 |
double abstol = -1.0; |
| 1354 |
__CLPK_integer out_m; |
| 1355 |
double* work = R_CAST(double*, extract_eigvecs_wkspace); |
| 1356 |
__CLPK_integer* iwork = R_CAST(__CLPK_integer*, &(work[lwork])); |
| 1357 |
__CLPK_integer* isuppz = &(iwork[liwork]); |
| 1358 |
__CLPK_integer info; |
| 1359 |
// vl and vu may actually be referenced in some implementations |
| 1360 |
double dummy_d = 0.0; |
| 1361 |
dsyevr_(&jobz, &range, &uplo, &tmp_n, matrix, &tmp_n, &dummy_d, &dummy_d, &il, &iu, &abstol, &out_m, eigvals, reverse_eigvecs, &tmp_n, isuppz, work, &lwork, iwork, &liwork, &info); |
| 1362 |
// bug workaround (11 Feb 2023): on macOS, when there are NaNs in the matrix, |
| 1363 |
// info=0 is still returned but out_m=0. |
| 1364 |
// More generally, with the current interface, we want to return an error |
| 1365 |
// whenever the number of found eigenvalues is less than pc_ct. |
| 1366 |
return (S_CAST(uint32_t, out_m) != pc_ct) || (info != 0); |
| 1367 |
} |
| 1368 |
#endif // !NOLAPACK |
| 1369 |
|
| 1370 |
BoolErr invert_rank1_symm_start(const double* a_inv, const double* bb, __CLPK_integer orig_dim, double cc, double* __restrict ainv_b, double* k_recip_ptr) { |
| 1371 |
#ifdef NOLAPACK |
| 1372 |
const uintptr_t orig_dim_l = orig_dim; |
| 1373 |
const double* a_inv_iter = a_inv; |
| 1374 |
for (uintptr_t ulii = 0; ulii != orig_dim_l; ++ulii) { |
| 1375 |
ainv_b[ulii] = DotprodD(bb, a_inv_iter, orig_dim_l); |
| 1376 |
a_inv_iter = &(a_inv_iter[orig_dim_l]); |
| 1377 |
} |
| 1378 |
#else |
| 1379 |
# ifndef USE_CBLAS_XGEMM |
| 1380 |
char trans = 'N'; |
| 1381 |
__CLPK_integer incxy = 1; |
| 1382 |
double dyy = 1.0; |
| 1383 |
double dzz = 0.0; |
| 1384 |
dgemv_(&trans, &orig_dim, &orig_dim, &dyy, K_CAST(double*, a_inv), &orig_dim, K_CAST(double*, bb), &incxy, &dzz, ainv_b, &incxy); |
| 1385 |
# else |
| 1386 |
cblas_dgemv(CblasColMajor, CblasNoTrans, orig_dim, orig_dim, 1.0, a_inv, orig_dim, bb, 1, 0.0, ainv_b, 1); |
| 1387 |
# endif // USE_CBLAS_XGEMM |
| 1388 |
#endif |
| 1389 |
const double kk = cc - DotprodxD(bb, ainv_b, orig_dim); |
| 1390 |
if (fabs(kk) < kMatrixSingularRcond) { |
| 1391 |
return 1; |
| 1392 |
} |
| 1393 |
*k_recip_ptr = 1.0 / kk; |
| 1394 |
return 0; |
| 1395 |
} |
| 1396 |
|
| 1397 |
BoolErr InvertRank1Symm(const double* a_inv, const double* bb, __CLPK_integer orig_dim, uint32_t insert_idx, double cc, double* __restrict outmatrix, double* __restrict ainv_b_buf) { |
| 1398 |
double k_recip; |
| 1399 |
if (invert_rank1_symm_start(a_inv, bb, orig_dim, cc, ainv_b_buf, &k_recip)) { |
| 1400 |
return 1; |
| 1401 |
} |
| 1402 |
// [ A^{-1} + k_recip * outer_prod(ainv_b, ainv_b) -k_recip * ainv_b ] |
| 1403 |
// [ -k_recip * ainv_b k_recip ] |
| 1404 |
const uintptr_t orig_dim_l = orig_dim; |
| 1405 |
const uintptr_t final_dim = orig_dim_l + 1; |
| 1406 |
uintptr_t orig_row_idx = 0; |
| 1407 |
const double* a_inv_row = a_inv; |
| 1408 |
double* outmatrix_row = outmatrix; |
| 1409 |
for (; orig_row_idx != insert_idx; ++orig_row_idx) { |
| 1410 |
const double ainv_b_div_k = k_recip * ainv_b_buf[orig_row_idx]; |
| 1411 |
for (uintptr_t col_idx = 0; col_idx <= orig_row_idx; ++col_idx) { |
| 1412 |
outmatrix_row[col_idx] = a_inv_row[col_idx] + ainv_b_div_k * ainv_b_buf[col_idx]; |
| 1413 |
} |
| 1414 |
a_inv_row = &(a_inv_row[orig_dim_l]); |
| 1415 |
outmatrix_row = &(outmatrix_row[final_dim]); |
| 1416 |
} |
| 1417 |
for (uintptr_t col_idx = 0; col_idx != insert_idx; ++col_idx) { |
| 1418 |
outmatrix_row[col_idx] = -k_recip * ainv_b_buf[col_idx]; |
| 1419 |
} |
| 1420 |
outmatrix_row[insert_idx] = k_recip; |
| 1421 |
for (; orig_row_idx != orig_dim_l; ++orig_row_idx) { |
| 1422 |
outmatrix_row = &(outmatrix_row[final_dim]); |
| 1423 |
const double ainv_b_div_k = k_recip * ainv_b_buf[orig_row_idx]; |
| 1424 |
for (uintptr_t col_idx = 0; col_idx != insert_idx; ++col_idx) { |
| 1425 |
outmatrix_row[col_idx] = a_inv_row[col_idx] + ainv_b_div_k * ainv_b_buf[col_idx]; |
| 1426 |
} |
| 1427 |
outmatrix_row[insert_idx] = -ainv_b_div_k; |
| 1428 |
double* outmatrix_write_base = &(outmatrix_row[1]); |
| 1429 |
for (uintptr_t orig_col_idx = insert_idx; orig_col_idx <= orig_row_idx; ++orig_col_idx) { |
| 1430 |
outmatrix_write_base[orig_col_idx] = a_inv_row[orig_col_idx] + ainv_b_div_k * ainv_b_buf[orig_col_idx]; |
| 1431 |
} |
| 1432 |
a_inv_row = &(a_inv_row[orig_dim_l]); |
| 1433 |
} |
| 1434 |
return 0; |
| 1435 |
} |
| 1436 |
|
| 1437 |
BoolErr InvertRank1SymmDiag(const double* a_inv, const double* bb, __CLPK_integer orig_dim, double cc, double* __restrict outdiag, double* __restrict ainv_b_buf) { |
| 1438 |
double k_recip; |
| 1439 |
if (invert_rank1_symm_start(a_inv, bb, orig_dim, cc, ainv_b_buf, &k_recip)) { |
| 1440 |
return 1; |
| 1441 |
} |
| 1442 |
const uintptr_t orig_dim_l = orig_dim; |
| 1443 |
const uintptr_t orig_dim_p1 = orig_dim_l + 1; |
| 1444 |
for (uintptr_t ulii = 0; ulii != orig_dim_l; ++ulii) { |
| 1445 |
const double dxx = ainv_b_buf[ulii]; |
| 1446 |
outdiag[ulii] = a_inv[ulii * orig_dim_p1] + k_recip * dxx * dxx; |
| 1447 |
} |
| 1448 |
outdiag[orig_dim_l] = k_recip; |
| 1449 |
return 0; |
| 1450 |
} |
| 1451 |
|
| 1452 |
BoolErr InvertRank2SymmStart(const double* a_inv, const double* bb, __CLPK_integer orig_dim, __CLPK_integer b_stride, double d11, double d12, double d22, double* __restrict b_ainv, double* __restrict s_b_ainv, double* __restrict schur11_ptr, double* __restrict schur12_ptr, double* __restrict schur22_ptr) { |
| 1453 |
const uintptr_t orig_dim_l = orig_dim; |
| 1454 |
if (orig_dim) { |
| 1455 |
// (have confirmed that dgemm is better than per-row multiplies even for |
| 1456 |
// just 2 rows) |
| 1457 |
RowMajorMatrixMultiplyStrided(bb, a_inv, 2, b_stride, orig_dim, orig_dim, orig_dim, orig_dim, b_ainv); |
| 1458 |
|
| 1459 |
// Schur complement = (D - B A^{-1} B^T)^{-1} |
| 1460 |
if (orig_dim_l > kDotprodDThresh) { |
| 1461 |
d11 -= DotprodD(bb, b_ainv, orig_dim); |
| 1462 |
d12 -= DotprodD(bb, &(b_ainv[orig_dim_l]), orig_dim); |
| 1463 |
d22 -= DotprodD(&(bb[b_stride]), &(b_ainv[orig_dim_l]), orig_dim); |
| 1464 |
} else { |
| 1465 |
d11 -= DotprodDShort(bb, b_ainv, orig_dim); |
| 1466 |
d12 -= DotprodDShort(bb, &(b_ainv[orig_dim_l]), orig_dim); |
| 1467 |
d22 -= DotprodDShort(&(bb[b_stride]), &(b_ainv[orig_dim_l]), orig_dim); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
// [ a b ]^{-1} = [ d -b ] / (ad - b^2) |
| 1472 |
// [ b d ] [ -b a ] |
| 1473 |
const double det = d11 * d22 - d12 * d12; |
| 1474 |
if (fabs(det) < kMatrixSingularRcond) { |
| 1475 |
return 1; |
| 1476 |
} |
| 1477 |
const double det_recip = 1.0 / det; |
| 1478 |
const double schur11 = d22 * det_recip; |
| 1479 |
const double schur12 = -d12 * det_recip; |
| 1480 |
const double schur22 = d11 * det_recip; |
| 1481 |
for (uintptr_t col_idx = 0; col_idx != orig_dim_l; ++col_idx) { |
| 1482 |
const double b_ainv_1 = b_ainv[col_idx]; |
| 1483 |
const double b_ainv_2 = b_ainv[col_idx + orig_dim_l]; |
| 1484 |
s_b_ainv[col_idx] = schur11 * b_ainv_1 + schur12 * b_ainv_2; |
| 1485 |
s_b_ainv[col_idx + orig_dim_l] = schur12 * b_ainv_1 + schur22 * b_ainv_2; |
| 1486 |
} |
| 1487 |
*schur11_ptr = schur11; |
| 1488 |
*schur12_ptr = schur12; |
| 1489 |
*schur22_ptr = schur22; |
| 1490 |
return 0; |
| 1491 |
} |
| 1492 |
|
| 1493 |
BoolErr InvertRank2Symm(const double* a_inv, const double* bb, __CLPK_integer orig_dim, __CLPK_integer b_stride, uint32_t insert_idx, double d11, double d12, double d22, double* __restrict outmatrix, double* __restrict b_ainv_buf, double* __restrict s_b_ainv_buf) { |
| 1494 |
double schur11; |
| 1495 |
double schur12; |
| 1496 |
double schur22; |
| 1497 |
if (InvertRank2SymmStart(a_inv, bb, orig_dim, b_stride, d11, d12, d22, b_ainv_buf, s_b_ainv_buf, &schur11, &schur12, &schur22)) { |
| 1498 |
return 1; |
| 1499 |
} |
| 1500 |
// [ A^{-1} + A{-1}B^T S B A{-1} -A^{-1}B^T S ] |
| 1501 |
// [ -S B A{-1} S ] |
| 1502 |
const uintptr_t orig_dim_l = orig_dim; |
| 1503 |
const uintptr_t final_dim = orig_dim_l + 2; |
| 1504 |
uintptr_t orig_row_idx = 0; |
| 1505 |
const double* a_inv_row = a_inv; |
| 1506 |
const double* b_ainv_row2 = &(b_ainv_buf[orig_dim_l]); |
| 1507 |
const double* s_b_ainv_row2 = &(s_b_ainv_buf[orig_dim_l]); |
| 1508 |
double* outmatrix_row = outmatrix; |
| 1509 |
for (; orig_row_idx != insert_idx; ++orig_row_idx) { |
| 1510 |
const double b_ainv_1 = b_ainv_buf[orig_row_idx]; |
| 1511 |
const double b_ainv_2 = b_ainv_row2[orig_row_idx]; |
| 1512 |
for (uintptr_t col_idx = 0; col_idx <= orig_row_idx; ++col_idx) { |
| 1513 |
outmatrix_row[col_idx] = a_inv_row[col_idx] + b_ainv_1 * s_b_ainv_buf[col_idx] + b_ainv_2 * s_b_ainv_row2[col_idx]; |
| 1514 |
} |
| 1515 |
a_inv_row = &(a_inv_row[orig_dim_l]); |
| 1516 |
outmatrix_row = &(outmatrix_row[final_dim]); |
| 1517 |
} |
| 1518 |
for (uintptr_t col_idx = 0; col_idx != insert_idx; ++col_idx) { |
| 1519 |
outmatrix_row[col_idx] = -s_b_ainv_buf[col_idx]; |
| 1520 |
} |
| 1521 |
outmatrix_row[insert_idx] = schur11; |
| 1522 |
outmatrix_row = &(outmatrix_row[final_dim]); |
| 1523 |
for (uintptr_t col_idx = 0; col_idx != insert_idx; ++col_idx) { |
| 1524 |
outmatrix_row[col_idx] = -s_b_ainv_row2[col_idx]; |
| 1525 |
} |
| 1526 |
outmatrix_row[insert_idx] = schur12; |
| 1527 |
outmatrix_row[insert_idx + 1] = schur22; |
| 1528 |
for (; orig_row_idx != orig_dim_l; ++orig_row_idx) { |
| 1529 |
outmatrix_row = &(outmatrix_row[final_dim]); |
| 1530 |
const double b_ainv_1 = b_ainv_buf[orig_row_idx]; |
| 1531 |
const double b_ainv_2 = b_ainv_row2[orig_row_idx]; |
| 1532 |
for (uintptr_t col_idx = 0; col_idx != insert_idx; ++col_idx) { |
| 1533 |
outmatrix_row[col_idx] = a_inv_row[col_idx] + b_ainv_1 * s_b_ainv_buf[col_idx] + b_ainv_2 * s_b_ainv_row2[col_idx]; |
| 1534 |
} |
| 1535 |
outmatrix_row[insert_idx] = -s_b_ainv_buf[orig_row_idx]; |
| 1536 |
outmatrix_row[insert_idx + 1] = -s_b_ainv_row2[orig_row_idx]; |
| 1537 |
double* outmatrix_write_base = &(outmatrix_row[2]); |
| 1538 |
for (uintptr_t orig_col_idx = insert_idx; orig_col_idx <= orig_row_idx; ++orig_col_idx) { |
| 1539 |
outmatrix_write_base[orig_col_idx] = a_inv_row[orig_col_idx] + b_ainv_1 * s_b_ainv_buf[orig_col_idx] + b_ainv_2 * s_b_ainv_row2[orig_col_idx]; |
| 1540 |
} |
| 1541 |
a_inv_row = &(a_inv_row[orig_dim_l]); |
| 1542 |
} |
| 1543 |
return 0; |
| 1544 |
} |
| 1545 |
|
| 1546 |
BoolErr InvertRank2SymmDiag(const double* a_inv, const double* bb, __CLPK_integer orig_dim, double d11, double d12, double d22, double* __restrict outdiag, double* __restrict b_ainv_buf, double* __restrict s_b_ainv_buf) { |
| 1547 |
double schur11; |
| 1548 |
double schur12; |
| 1549 |
double schur22; |
| 1550 |
if (InvertRank2SymmStart(a_inv, bb, orig_dim, orig_dim, d11, d12, d22, b_ainv_buf, s_b_ainv_buf, &schur11, &schur12, &schur22)) { |
| 1551 |
return 1; |
| 1552 |
} |
| 1553 |
const uintptr_t orig_dim_l = orig_dim; |
| 1554 |
const uintptr_t orig_dim_p1 = orig_dim_l + 1; |
| 1555 |
const double* b_ainv_row2 = &(b_ainv_buf[orig_dim_l]); |
| 1556 |
const double* s_b_ainv_row2 = &(s_b_ainv_buf[orig_dim_l]); |
| 1557 |
for (uintptr_t ulii = 0; ulii != orig_dim_l; ++ulii) { |
| 1558 |
outdiag[ulii] = a_inv[ulii * orig_dim_p1] + b_ainv_buf[ulii] * s_b_ainv_buf[ulii] + b_ainv_row2[ulii] * s_b_ainv_row2[ulii]; |
| 1559 |
} |
| 1560 |
outdiag[orig_dim_l] = schur11; |
| 1561 |
outdiag[orig_dim_l + 1] = schur22; |
| 1562 |
return 0; |
| 1563 |
} |
| 1564 |
|
| 1565 |
// now assumes xtx_inv is predictors_pmaj * transpose on input |
| 1566 |
#ifdef NOLAPACK |
| 1567 |
BoolErr LinearRegressionInvMain(const double* xt_y_phenomaj, uint32_t predictor_ct, uint32_t pheno_ct, double* xtx_inv, double* fitted_coefs_phenomaj, MatrixInvertBuf1* mi_buf, double* dbl_2d_buf) { |
| 1568 |
if (InvertSymmdefMatrix(predictor_ct, xtx_inv, mi_buf, dbl_2d_buf)) { |
| 1569 |
return 1; |
| 1570 |
} |
| 1571 |
RowMajorMatrixMultiply(xt_y_phenomaj, xtx_inv, pheno_ct, predictor_ct, predictor_ct, fitted_coefs_phenomaj); |
| 1572 |
return 0; |
| 1573 |
} |
| 1574 |
#else |
| 1575 |
|
| 1576 |
BoolErr LinearRegressionInvMain(const double* xt_y_phenomaj, uint32_t predictor_ct, __CLPK_integer pheno_ct, double* xtx_inv, double* fitted_coefs_phenomaj) { |
| 1577 |
char uplo = 'U'; |
| 1578 |
__CLPK_integer tmp_n = predictor_ct; |
| 1579 |
__CLPK_integer info; |
| 1580 |
dpotrf_(&uplo, &tmp_n, xtx_inv, &tmp_n, &info); |
| 1581 |
if (info) { |
| 1582 |
return 1; |
| 1583 |
} |
| 1584 |
memcpy(fitted_coefs_phenomaj, xt_y_phenomaj, predictor_ct * pheno_ct * sizeof(double)); |
| 1585 |
dpotrs_(&uplo, &tmp_n, &pheno_ct, xtx_inv, &tmp_n, fitted_coefs_phenomaj, &tmp_n, &info); |
| 1586 |
assert(!info); |
| 1587 |
dpotri_(&uplo, &tmp_n, xtx_inv, &tmp_n, &info); |
| 1588 |
return (info != 0); |
| 1589 |
} |
| 1590 |
#endif // !NOLAPACK |
| 1591 |
|
| 1592 |
#ifdef __cplusplus |
| 1593 |
} // namespace plink2 |
| 1594 |
#endif |