/usr/include/vmmlib/lapack_linear_least_squares.hpp is in libvmmlib-dev 1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 378 379 380 | #ifndef __VMML__VMMLIB_LAPACK_LINEAR_LEAST_SQUARES__HPP__
#define __VMML__VMMLIB_LAPACK_LINEAR_LEAST_SQUARES__HPP__
#include <vmmlib/matrix.hpp>
#include <vmmlib/vector.hpp>
#include <vmmlib/exception.hpp>
#include <vmmlib/lapack_types.hpp>
#include <vmmlib/lapack_includes.hpp>
#include <string>
/**
*
* this is a wrapper for the following lapack routines:
*
* xGELS
*
*
*/
namespace vmml
{
// XYYZZZ
// X = data type: S - float, D - double
// YY = matrix type, GE - general, TR - triangular
// ZZZ = function name
namespace lapack
{
//
//
// SGELS/DGELS
//
//
// parameter struct
template< typename float_t >
struct llsq_params_xgels
{
char trans; // 'N'->A, 'T'->Atransposed
lapack_int m; // number of rows, M >= 0
lapack_int n; // number of columns, N >= 0
lapack_int nrhs; // number of columns of B/X
float_t* a; // input A
lapack_int lda; // leading dimension of A (number of rows)
float_t* b; // input B, output X
lapack_int ldb; // leading dimension of b
float_t* work; // workspace
lapack_int lwork; // workspace size
lapack_int info; // 'return' value
friend std::ostream& operator << ( std::ostream& os,
const llsq_params_xgels< float_t >& p )
{
os
<< " m " << p.m
<< " n " << p.n
<< " nrhs " << p.nrhs
<< " lda " << p.lda
<< " ldb " << p.ldb
<< " lwork " << p.lwork
<< " info " << p.info
<< std::endl;
return os;
}
};
// call wrappers
#if 0
void dgels_(const char *trans, const int *M, const int *N, const int *nrhs,
double *A, const int *lda, double *b, const int *ldb, double *work,
const int * lwork, int *info);
#endif
template< typename float_t >
inline void
llsq_call_xgels( llsq_params_xgels< float_t >& p )
{
VMMLIB_ERROR( "not implemented for this type.", VMMLIB_HERE );
}
template<>
inline void
llsq_call_xgels( llsq_params_xgels< float >& p )
{
sgels_(
&p.trans,
&p.m,
&p.n,
&p.nrhs,
p.a,
&p.lda,
p.b,
&p.ldb,
p.work,
&p.lwork,
&p.info
);
}
template<>
inline void
llsq_call_xgels( llsq_params_xgels< double >& p )
{
dgels_(
&p.trans,
&p.m,
&p.n,
&p.nrhs,
p.a,
&p.lda,
p.b,
&p.ldb,
p.work,
&p.lwork,
&p.info
);
}
template< size_t M, size_t N, typename float_t >
struct linear_least_squares_xgels
{
bool compute(
const matrix< M, N, float_t >& A,
const vector< M, float_t >& B,
vector< N, float_t >& x );
linear_least_squares_xgels();
~linear_least_squares_xgels();
const lapack::llsq_params_xgels< float_t >& get_params(){ return p; };
matrix< M, N, float_t >& get_factorized_A() { return _A; }
protected:
matrix< M, N, float_t > _A;
vector< M, float_t > _b;
llsq_params_xgels< float_t > p;
};
template< size_t M, size_t N, typename float_t >
bool
linear_least_squares_xgels< M, N, float_t >::compute(
const matrix< M, N, float_t >& A,
const vector< M, float_t >& B,
vector< N, float_t >& x )
{
_A = A;
_b = B;
llsq_call_xgels( p );
// success
if ( p.info == 0 )
{
for( size_t index = 0; index < N; ++index )
{
x( index ) = _b( index );
}
return true;
}
if ( p.info < 0 )
{
VMMLIB_ERROR( "xGELS - invalid argument.", VMMLIB_HERE );
}
else
{
std::cout << "A\n" << A << std::endl;
std::cout << "B\n" << B << std::endl;
VMMLIB_ERROR( "least squares solution could not be computed.",
VMMLIB_HERE );
}
return false;
}
template< size_t M, size_t N, typename float_t >
linear_least_squares_xgels< M, N, float_t >::
linear_least_squares_xgels()
{
p.trans = 'N';
p.m = M;
p.n = N;
p.nrhs = 1;
p.a = _A.array;
p.lda = M;
p.b = _b.array;
p.ldb = M;
p.work = new float_t();
p.lwork = -1;
// workspace query
llsq_call_xgels( p );
p.lwork = static_cast< lapack_int > ( p.work[0] );
delete p.work;
p.work = new float_t[ p.lwork ];
}
template< size_t M, size_t N, typename float_t >
linear_least_squares_xgels< M, N, float_t >::
~linear_least_squares_xgels()
{
delete[] p.work;
}
//
//
// SGESV/DGESV
//
//
template< typename float_t >
struct llsq_params_xgesv
{
lapack_int n; // order of matrix A = M * N
lapack_int nrhs; // number of columns of B
float_t* a; // input A, output P*L*U
lapack_int lda; // leading dimension of A (for us: number of rows)
lapack_int* ipiv; // pivot indices, integer array of size N
float_t* b; // input b, output X
lapack_int ldb; // leading dimension of b
lapack_int info;
friend std::ostream& operator << ( std::ostream& os,
const llsq_params_xgesv< float_t >& p )
{
os
<< "n " << p.n
<< " nrhs " << p.nrhs
<< " lda " << p.lda
<< " ldb " << p.ldvt
<< " info " << p.info
<< std::endl;
return os;
}
};
#if 0
/* Subroutine */ int dgesv_(integer *n, integer *nrhs, doublereal *a, integer
*lda, integer *ipiv, doublereal *b, integer *ldb, integer *info);
#endif
template< typename float_t >
inline void
llsq_call_xgesv( llsq_params_xgesv< float_t >& p )
{
VMMLIB_ERROR( "not implemented for this type.", VMMLIB_HERE );
}
template<>
inline void
llsq_call_xgesv( llsq_params_xgesv< float >& p )
{
sgesv_(
&p.n,
&p.nrhs,
p.a,
&p.lda,
p.ipiv,
p.b,
&p.ldb,
&p.info
);
}
template<>
inline void
llsq_call_xgesv( llsq_params_xgesv< double >& p )
{
dgesv_(
&p.n,
&p.nrhs,
p.a,
&p.lda,
p.ipiv,
p.b,
&p.ldb,
&p.info
);
}
template< size_t M, size_t N, typename float_t >
struct linear_least_squares_xgesv
{
// computes x ( Ax = b ). x replaces b on output.
void compute(
matrix< N, N, float_t >& A,
matrix< N, M, float_t >& b
);
linear_least_squares_xgesv();
~linear_least_squares_xgesv();
const lapack::llsq_params_xgesv< float_t >& get_params() { return p; }
lapack::llsq_params_xgesv< float_t > p;
}; // struct lapack_linear_least_squares
template< size_t M, size_t N, typename float_t >
void
linear_least_squares_xgesv< M, N, float_t >::
compute(
matrix< N, N, float_t >& A,
matrix< N, M, float_t >& b
)
{
p.a = A.array;
p.b = b.array;
lapack::llsq_call_xgesv( p );
if ( p.info != 0 )
{
if ( p.info < 0 )
VMMLIB_ERROR( "invalid value in input matrix", VMMLIB_HERE );
else
VMMLIB_ERROR( "factor U is exactly singular, solution could not be computed.", VMMLIB_HERE );
}
}
template< size_t M, size_t N, typename float_t >
linear_least_squares_xgesv< M, N, float_t >::
linear_least_squares_xgesv()
{
p.n = N;
p.nrhs = M;
p.lda = N;
p.ldb = N;
p.ipiv = new lapack_int[ N ];
}
template< size_t M, size_t N, typename float_t >
linear_least_squares_xgesv< M, N, float_t >::
~linear_least_squares_xgesv()
{
delete[] p.ipiv;
}
} // namespace lapack
} // namespace vmml
#endif
|