13 #include <gsl/gsl_errno.h>
15 #include "GSLUtility.h"
16 #include "IException.h"
24 GSLUtility *GSLUtility::_instance = 0;
35 GSLUtility::GSLUtility() {
36 gsl_set_error_handler(handler);
70 gsl_vector *GSLUtility::vector(
size_t n,
bool zero)
const {
72 return (gsl_vector_calloc(n));
75 return (gsl_vector_alloc(n));
93 gsl_matrix *GSLUtility::matrix(
size_t n1,
size_t n2,
bool zero)
const {
95 return (gsl_matrix_calloc(n1, n2));
98 return (gsl_matrix_alloc(n1, n2));
114 gsl_matrix *GSLUtility::identity(
size_t n1,
size_t n2)
const {
115 gsl_matrix *i = gsl_matrix_alloc(n1, n2);
116 gsl_matrix_set_identity(i);
125 void GSLUtility::setIdentity(gsl_matrix *m)
const {
126 gsl_matrix_set_identity(m);
141 void GSLUtility::free(gsl_vector *v)
const {
157 void GSLUtility::free(gsl_matrix *m)
const {
171 GSLUtility::GSLVector GSLUtility::gslToGSL(
const gsl_vector *v)
const {
174 for(
size_t i = 0 ; i < n ; i++) {
175 Nv[i] = gsl_vector_get(v, i);
189 GSLUtility::GSLMatrix GSLUtility::gslToGSL(
const gsl_matrix *m)
const {
190 size_t nrows = Rows(m);
191 size_t ncols = Columns(m);
192 GSLMatrix Nm(nrows, ncols);
193 for(
size_t i = 0 ; i < nrows ; i++) {
194 for(
size_t j = 0 ; j < ncols ; j++) {
195 Nm[i][j] = gsl_matrix_get(m, i, j);
212 gsl_vector *GSLUtility::GSLTogsl(
const GSLUtility::GSLVector &v,
213 gsl_vector *gv)
const {
215 gv = gsl_vector_alloc(v.dim());
217 else if(size(gv) != (
size_t) v.dim()) {
219 mess <<
"Size of NL vector (" << v.dim() <<
") not same as GSL vector ("
226 for(
int i = 0 ; i < v.dim() ; i++) {
227 gsl_vector_set(gv, i, v[i]);
242 gsl_matrix *GSLUtility::GSLTogsl(
const GSLUtility::GSLMatrix &m,
243 gsl_matrix *gm)
const {
245 gm = gsl_matrix_alloc(m.dim1(), m.dim2());
247 else if((Rows(gm) != (
size_t) m.dim1()) &&
248 (Columns(gm) != (
size_t) m.dim2())) {
250 mess <<
"Size of NL matrix (" << m.dim1() <<
"," << m.dim2()
251 <<
") not same as GSL matrix (" << Rows(gm) <<
"," << Columns(gm)
258 for(
int i = 0 ; i < m.dim1() ; i++) {
259 for(
int j = 0 ; j < m.dim2() ; j++) {
260 gsl_matrix_set(gm, i, j, m[i][j]);
267 size_t GSLUtility::Rows(
const gsl_matrix *m)
const {
272 size_t GSLUtility::Columns(
const gsl_matrix *m)
const {
277 size_t GSLUtility::Columns(
const GSLMatrix &m)
const {
282 size_t GSLUtility::Rows(
const GSLMatrix &m)
const {
287 size_t GSLUtility::size(
const gsl_vector *v)
const {
292 size_t GSLUtility::size(
const gsl_matrix *m)
const {
293 return (Rows(m) * Columns(m));
307 void GSLUtility::check(
int gsl_status,
const char *src,
int line)
const {
308 if(gsl_status != GSL_SUCCESS) {
309 string msg =
"GSL error occured: " + string(gsl_strerror(gsl_status));
310 throw IException(IException::Programmer, msg.c_str(), src, line);
331 void GSLUtility::handler(
const char *reason,
const char *file,
int line,
334 mess <<
"GSLError (" << gsl_errno <<
") -> " << reason;
335 throw IException(IException::Programmer, mess.str().c_str(),