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
18using namespace std;
19
20namespace Isis {
21 namespace GSL {
22
24 GSLUtility *GSLUtility::_instance = 0;
25
38
39
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);
117 return (i);
118 }
119
125 void GSLUtility::setIdentity(gsl_matrix *m) const {
127 return;
128 }
129
141 void GSLUtility::free(gsl_vector *v) const {
143 return;
144 }
145
157 void GSLUtility::free(gsl_matrix *m) const {
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 << ")";
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 << ")";
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
GSLUtility Provides top level interface to the GNU GSL.
Definition GSLUtility.h:69
size_t Rows(const gsl_matrix *m) const
Returns number of rows in a GSL matrix.
void free(gsl_vector *v) const
Frees a GSL vector.
size_t size(const gsl_vector *v) const
Returns the size of a GSL vector.
GSLVector gslToGSL(const gsl_vector *v) const
Converts a GSL vector to a TNT-based vector.
void setIdentity(gsl_matrix *m) const
Initializes an existing GSL matrix to the identity matrix.
gsl_vector * GSLTogsl(const GSLVector &v, gsl_vector *gv=0) const
Converts TNT-based vector to GSL vector.
static GSLUtility * _instance
Singleton self-reference pointer.
Definition GSLUtility.h:123
static void handler(const char *reason, const char *file, int line, int gsl_errno)
Special GSL errror handler.
GSLUtility()
Contructs a GSLUtility object with an error handler.
void check(int gsl_status, const char *src=__FILE__, int line=__LINE__) const
Performs a check on GSL library function return status.
gsl_matrix * matrix(size_t n1, size_t n2, bool zero=false) const
Creates a GSL matrix.
static GSLUtility * getInstance()
Return a reference to the GSL (singleton) object.
size_t Columns(const gsl_matrix *m) const
Returns the number of coulumns in a GSL matrix.
gsl_vector * vector(size_t n, bool zero=false) const
Creates a GSL vector.
gsl_matrix * identity(size_t n1, size_t n2) const
Returns a GSL identity matrix of the specified size.
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.