Isis 3 Programmer Reference
GSLUtility.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include <string>
8 #include <vector>
9 #include <numeric>
10 #include <iostream>
11 #include <sstream>
12 
13 #include <gsl/gsl_errno.h>
14 
15 #include "GSLUtility.h"
16 #include "IException.h"
17 
18 using namespace std;
19 
20 namespace Isis {
21  namespace GSL {
22 
24  GSLUtility *GSLUtility::_instance = 0;
25 
35  GSLUtility::GSLUtility() {
36  gsl_set_error_handler(handler);
37  }
38 
39 
49  GSLUtility *GSLUtility::getInstance() {
50  if(_instance == 0) {
51  _instance = new GSLUtility();
52  }
53  return (_instance);
54  }
55 
56 
70  gsl_vector *GSLUtility::vector(size_t n, bool zero) const {
71  if(zero) {
72  return (gsl_vector_calloc(n));
73  }
74  else {
75  return (gsl_vector_alloc(n));
76  }
77  }
78 
93  gsl_matrix *GSLUtility::matrix(size_t n1, size_t n2, bool zero) const {
94  if(zero) {
95  return (gsl_matrix_calloc(n1, n2));
96  }
97  else {
98  return (gsl_matrix_alloc(n1, n2));
99  }
100  }
101 
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);
117  return (i);
118  }
119 
125  void GSLUtility::setIdentity(gsl_matrix *m) const {
126  gsl_matrix_set_identity(m);
127  return;
128  }
129 
141  void GSLUtility::free(gsl_vector *v) const {
142  gsl_vector_free(v);
143  return;
144  }
145 
157  void GSLUtility::free(gsl_matrix *m) const {
158  gsl_matrix_free(m);
159  return;
160  }
161 
171  GSLUtility::GSLVector GSLUtility::gslToGSL(const gsl_vector *v) const {
172  size_t n = size(v);;
173  GSLVector Nv(n);
174  for(size_t i = 0 ; i < n ; i++) {
175  Nv[i] = gsl_vector_get(v, i);
176  }
177  return (Nv);
178  }
179 
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);
196  }
197  }
198  return (Nm);
199  }
200 
201 
212  gsl_vector *GSLUtility::GSLTogsl(const GSLUtility::GSLVector &v,
213  gsl_vector *gv) const {
214  if(gv == 0) {
215  gv = gsl_vector_alloc(v.dim());
216  }
217  else if(size(gv) != (size_t) v.dim()) {
218  ostringstream mess;
219  mess << "Size of NL vector (" << v.dim() << ") not same as GSL vector ("
220  << gv->size << ")";
221  throw IException(IException::Programmer,
222  mess.str().c_str(),
223  _FILEINFO_);
224  }
225 
226  for(int i = 0 ; i < v.dim() ; i++) {
227  gsl_vector_set(gv, i, v[i]);
228  }
229  return (gv);
230  }
231 
242  gsl_matrix *GSLUtility::GSLTogsl(const GSLUtility::GSLMatrix &m,
243  gsl_matrix *gm) const {
244  if(gm == 0) {
245  gm = gsl_matrix_alloc(m.dim1(), m.dim2());
246  }
247  else if((Rows(gm) != (size_t) m.dim1()) &&
248  (Columns(gm) != (size_t) m.dim2())) {
249  ostringstream mess;
250  mess << "Size of NL matrix (" << m.dim1() << "," << m.dim2()
251  << ") not same as GSL matrix (" << Rows(gm) << "," << Columns(gm)
252  << ")";
253  throw IException(IException::Programmer,
254  mess.str().c_str(),
255  _FILEINFO_);
256  }
257 
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]);
261  }
262  }
263  return (gm);
264  }
265 
267  size_t GSLUtility::Rows(const gsl_matrix *m) const {
268  return (m->size1);
269  }
270 
272  size_t GSLUtility::Columns(const gsl_matrix *m) const {
273  return (m->size2);
274  }
275 
277  size_t GSLUtility::Columns(const GSLMatrix &m) const {
278  return (m.dim1());
279  }
280 
282  size_t GSLUtility::Rows(const GSLMatrix &m) const {
283  return (m.dim2());
284  }
285 
287  size_t GSLUtility::size(const gsl_vector *v) const {
288  return (v->size);
289  }
290 
292  size_t GSLUtility::size(const gsl_matrix *m) const {
293  return (Rows(m) * Columns(m));
294  }
295 
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);
311  }
312  return;
313  }
314 
331  void GSLUtility::handler(const char *reason, const char *file, int line,
332  int gsl_errno) {
333  ostringstream mess;
334  mess << "GSLError (" << gsl_errno << ") -> " << reason;
335  throw IException(IException::Programmer, mess.str().c_str(),
336  file, line);
337  }
338 
339  }
340 } // namespace ISIS::GSL
Isis::IException
Isis exception class.
Definition: IException.h:91
std
Namespace for the standard library.
Isis::GSL::GSLUtility
GSLUtility Provides top level interface to the GNU GSL.
Definition: GSLUtility.h:69
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16