// This file is part of PLINK 1.9, copyright (C) 2005-2024 Shaun Purcell,
// Christopher Chang.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
#include "plink_common.h"
#include "plink_matrix.h"
#ifndef NOLAPACK
# ifndef __APPLE__
void xerbla_(void) {} // fix static linking error
# endif
#endif
static inline double SQR(const double a) {
return a * a;
}
double pythag(const double a, const double b) {
// PLINK stats.cpp pythag().
double absa,absb;
absa=fabs(a);
absb=fabs(b);
if (absa > absb) return absa*sqrt(1.0+SQR(absb/absa));
else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+SQR(absa/absb)));
}
#ifdef NOLAPACK
#ifdef __cplusplus
static inline double SIGN(const double &a, const double &b) {
// PLINK helper.h SIGN() template specialized to doubles.
return (b >= 0)? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
}
#else
static inline double SIGN(const double a, const double b) {
// PLINK helper.h SIGN() template specialized to doubles.
return (b >= 0)? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
}
#endif
int32_t svdcmp_c(int32_t m, double* a, double* w, double* v) {
// C port of PLINK stats.cpp svdcmp().
// now thread-safe.
double* rv1 = &(w[(uint32_t)m]);
int32_t n = m;
int32_t flag;
int32_t l = 0; // suppress compile warning
int32_t i,its,j,jj,k,nm;
double anorm,c,f,g,h,s,scale,x,y,z;
double temp;
g=scale=anorm=0.0;
for (i=0;i=0;i--) {
if (i < n-1) {
if (g != 0.0) {
for (j=l;j=0;i--) {
l=i+1;
g=w[i];
for (j=l;j=0;k--) {
for (its=0;its<30;its++) {
flag=1;
for (l=k;l>=0;l--) {
nm=l-1;
temp=fabs(rv1[l])+anorm;
if (temp == anorm) {
flag=0;
break;
}
temp=fabs(w[nm])+anorm;
if (temp == anorm) break;
}
if (flag) {
c=0.0;
s=1.0;
for (i=l;i dbl_1d_buf
// v -> dbl_2d_buf
const double eps = 1e-24;
int32_t i;
int32_t j;
int32_t k;
i = svdcmp_c(dim, matrix, dbl_1d_buf, dbl_2d_buf);
if (i == -1) {
return -1;
} else if (!i) {
return 1;
}
// Look for singular values
double wmax = 0;
for (i=0; i wmax ? dbl_1d_buf[i] : wmax;
}
double wmin = wmax * eps;
for (i=0; i 0) {
return 1;
}
dgecon_(&cc, &dim, matrix, &dim, &norm, &rcond, dbl_2d_buf, &(int_1d_buf[dim]), &info);
if (rcond < MATRIX_SINGULAR_RCOND) {
return 1;
}
dgetri_(&dim, matrix, &dim, int_1d_buf, dbl_2d_buf, &lwork, &info);
return 0;
}
#endif // !NOLAPACK
void col_major_matrix_multiply(__CLPK_integer row1_ct, __CLPK_integer col2_ct, __CLPK_integer common_ct, double* inmatrix1, double* inmatrix2, double* outmatrix) {
#ifdef NOLAPACK
uintptr_t row1_ct_l = row1_ct;
uintptr_t col2_ct_l = col2_ct;
uintptr_t common_ct_l = common_ct;
uintptr_t row_idx;
uintptr_t col_idx;
uintptr_t com_idx;
double* dptr;
double dxx;
// not optimized
for (col_idx = 0; col_idx < col2_ct_l; col_idx++) {
for (row_idx = 0; row_idx < row1_ct_l; row_idx++) {
dxx = 0;
dptr = &(inmatrix2[col_idx * common_ct]);
for (com_idx = 0; com_idx < common_ct_l; com_idx++) {
dxx += (*dptr++) * inmatrix1[com_idx * row1_ct_l + row_idx];
}
*outmatrix++ = dxx;
}
}
#else
# ifndef USE_CBLAS_XGEMM
char blas_char = 'N';
double dyy = 1;
double dzz = 0;
dgemm_(&blas_char, &blas_char, &row1_ct, &col2_ct, &common_ct, &dyy, inmatrix1, &row1_ct, inmatrix2, &common_ct, &dzz, outmatrix, &row1_ct);
# else
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, row1_ct, col2_ct, common_ct, 1.0, inmatrix1, row1_ct, inmatrix2, common_ct, 0.0, outmatrix, row1_ct);
# endif // USE_CBLAS_XGEMM
#endif // !NOLAPACK
}
void col_major_fmatrix_multiply(__CLPK_integer row1_ct, __CLPK_integer col2_ct, __CLPK_integer common_ct, float* inmatrix1, float* inmatrix2, float* outmatrix) {
#ifdef NOLAPACK
uintptr_t row1_ct_l = row1_ct;
uintptr_t col2_ct_l = col2_ct;
uintptr_t common_ct_l = common_ct;
uintptr_t row_idx;
uintptr_t col_idx;
uintptr_t com_idx;
float* fptr;
float fxx;
// not optimized
for (col_idx = 0; col_idx < col2_ct_l; col_idx++) {
for (row_idx = 0; row_idx < row1_ct_l; row_idx++) {
fxx = 0;
fptr = &(inmatrix2[col_idx * common_ct]);
for (com_idx = 0; com_idx < common_ct_l; com_idx++) {
fxx += (*fptr++) * inmatrix1[com_idx * row1_ct_l + row_idx];
}
*outmatrix++ = fxx;
}
}
#else
# ifndef USE_CBLAS_XGEMM
char blas_char = 'N';
float fyy = 1;
float fzz = 0;
sgemm_(&blas_char, &blas_char, &row1_ct, &col2_ct, &common_ct, &fyy, inmatrix1, &row1_ct, inmatrix2, &common_ct, &fzz, outmatrix, &row1_ct);
# else
cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, row1_ct, col2_ct, common_ct, 1.0, inmatrix1, row1_ct, inmatrix2, common_ct, 0.0, outmatrix, row1_ct);
# endif // USE_CBLAS_XGEMM
#endif // !NOLAPACK
}
// Todo: replace these with cache-oblivious, or at least -friendlier,
// algorithms.
void transpose_copy(uintptr_t old_maj, uintptr_t new_maj, double* old_matrix, double* new_matrix) {
double* dptr;
uintptr_t new_maj_idx;
uintptr_t old_maj_idx;
for (new_maj_idx = 0; new_maj_idx < new_maj; new_maj_idx++) {
dptr = &(old_matrix[new_maj_idx]);
for (old_maj_idx = 0; old_maj_idx < old_maj; old_maj_idx++) {
*new_matrix++ = dptr[old_maj_idx * new_maj];
}
}
}
void transpose_copy_float(uintptr_t old_maj, uintptr_t new_maj, uint32_t new_maj_max, float* old_matrix, float* new_matrix) {
// new_maj = in-memory stride of old_matrix rows
// new_maj_max = actual number of rows in new_matrix
// (distinction is necessary for SSE alignment)
float* fptr;
uintptr_t new_maj_idx;
uintptr_t old_maj_idx;
for (new_maj_idx = 0; new_maj_idx < new_maj_max; new_maj_idx++) {
fptr = &(old_matrix[new_maj_idx]);
for (old_maj_idx = 0; old_maj_idx < old_maj; old_maj_idx++) {
*new_matrix++ = fptr[old_maj_idx * new_maj];
}
}
}