USGS

Isis 3.0 Object Programmers' Reference

Home

GSLUtility.cpp
Go to the documentation of this file.
1 
24 #include <string>
25 #include <vector>
26 #include <numeric>
27 #include <iostream>
28 #include <sstream>
29 
30 #include <gsl/gsl_errno.h>
31 
32 #include "GSLUtility.h"
33 #include "IException.h"
34 
35 using namespace std;
36 
37 namespace Isis {
38  namespace GSL {
39 
41  GSLUtility *GSLUtility::_instance = 0;
42 
52  GSLUtility::GSLUtility() {
53  gsl_set_error_handler(handler);
54  }
55 
56 
66  GSLUtility *GSLUtility::getInstance() {
67  if(_instance == 0) {
68  _instance = new GSLUtility();
69  }
70  return (_instance);
71  }
72 
73 
87  gsl_vector *GSLUtility::vector(size_t n, bool zero) const {
88  if(zero) {
89  return (gsl_vector_calloc(n));
90  }
91  else {
92  return (gsl_vector_alloc(n));
93  }
94  }
95 
110  gsl_matrix *GSLUtility::matrix(size_t n1, size_t n2, bool zero) const {
111  if(zero) {
112  return (gsl_matrix_calloc(n1, n2));
113  }
114  else {
115  return (gsl_matrix_alloc(n1, n2));
116  }
117  }
118 
131  gsl_matrix *GSLUtility::identity(size_t n1, size_t n2) const {
132  gsl_matrix *i = gsl_matrix_alloc(n1, n2);
133  gsl_matrix_set_identity(i);
134  return (i);
135  }
136 
142  void GSLUtility::setIdentity(gsl_matrix *m) const {
143  gsl_matrix_set_identity(m);
144  return;
145  }
146 
158  void GSLUtility::free(gsl_vector *v) const {
159  gsl_vector_free(v);
160  return;
161  }
162 
174  void GSLUtility::free(gsl_matrix *m) const {
175  gsl_matrix_free(m);
176  return;
177  }
178 
188  GSLUtility::GSLVector GSLUtility::gslToGSL(const gsl_vector *v) const {
189  size_t n = size(v);;
190  GSLVector Nv(n);
191  for(size_t i = 0 ; i < n ; i++) {
192  Nv[i] = gsl_vector_get(v, i);
193  }
194  return (Nv);
195  }
196 
206  GSLUtility::GSLMatrix GSLUtility::gslToGSL(const gsl_matrix *m) const {
207  size_t nrows = Rows(m);
208  size_t ncols = Columns(m);
209  GSLMatrix Nm(nrows, ncols);
210  for(size_t i = 0 ; i < nrows ; i++) {
211  for(size_t j = 0 ; j < ncols ; j++) {
212  Nm[i][j] = gsl_matrix_get(m, i, j);
213  }
214  }
215  return (Nm);
216  }
217 
218 
229  gsl_vector *GSLUtility::GSLTogsl(const GSLUtility::GSLVector &v,
230  gsl_vector *gv) const {
231  if(gv == 0) {
232  gv = gsl_vector_alloc(v.dim());
233  }
234  else if(size(gv) != (size_t) v.dim()) {
235  ostringstream mess;
236  mess << "Size of NL vector (" << v.dim() << ") not same as GSL vector ("
237  << gv->size << ")";
238  throw IException(IException::Programmer,
239  mess.str().c_str(),
240  _FILEINFO_);
241  }
242 
243  for(int i = 0 ; i < v.dim() ; i++) {
244  gsl_vector_set(gv, i, v[i]);
245  }
246  return (gv);
247  }
248 
259  gsl_matrix *GSLUtility::GSLTogsl(const GSLUtility::GSLMatrix &m,
260  gsl_matrix *gm) const {
261  if(gm == 0) {
262  gm = gsl_matrix_alloc(m.dim1(), m.dim2());
263  }
264  else if((Rows(gm) != (size_t) m.dim1()) &&
265  (Columns(gm) != (size_t) m.dim2())) {
266  ostringstream mess;
267  mess << "Size of NL matrix (" << m.dim1() << "," << m.dim2()
268  << ") not same as GSL matrix (" << Rows(gm) << "," << Columns(gm)
269  << ")";
270  throw IException(IException::Programmer,
271  mess.str().c_str(),
272  _FILEINFO_);
273  }
274 
275  for(int i = 0 ; i < m.dim1() ; i++) {
276  for(int j = 0 ; j < m.dim2() ; j++) {
277  gsl_matrix_set(gm, i, j, m[i][j]);
278  }
279  }
280  return (gm);
281  }
282 
284  size_t GSLUtility::Rows(const gsl_matrix *m) const {
285  return (m->size1);
286  }
287 
289  size_t GSLUtility::Columns(const gsl_matrix *m) const {
290  return (m->size2);
291  }
292 
294  size_t GSLUtility::Columns(const GSLMatrix &m) const {
295  return (m.dim1());
296  }
297 
299  size_t GSLUtility::Rows(const GSLMatrix &m) const {
300  return (m.dim2());
301  }
302 
304  size_t GSLUtility::size(const gsl_vector *v) const {
305  return (v->size);
306  }
307 
309  size_t GSLUtility::size(const gsl_matrix *m) const {
310  return (Rows(m) * Columns(m));
311  }
312 
324  void GSLUtility::check(int gsl_status, const char *src, int line) const {
325  if(gsl_status != GSL_SUCCESS) {
326  string msg = "GSL error occured: " + string(gsl_strerror(gsl_status));
327  throw IException(IException::Programmer, msg.c_str(), src, line);
328  }
329  return;
330  }
331 
348  void GSLUtility::handler(const char *reason, const char *file, int line,
349  int gsl_errno) {
350  ostringstream mess;
351  mess << "GSLError (" << gsl_errno << ") -> " << reason;
352  throw IException(IException::Programmer, mess.str().c_str(),
353  file, line);
354  }
355 
356  }
357 } // namespace ISIS::GSL