ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
splinalg.hpp
Go to the documentation of this file.
1/*
2 * $Id$
3 * Copyright (C) 2010, 2018, 2019, 2020 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_SPLINALG_HPP
21#define CCGSL_SPLINALG_HPP
22
23#include<cmath>
24#include<gsl/gsl_splinalg.h>
25#include"vector.hpp"
26#include"spmatrix.hpp"
27
28namespace gsl {
32 namespace splinalg {
36 class itersolve {
37 public:
41 gsl_splinalg_itersolve_type const*& gmres = gsl_splinalg_itersolve_gmres;
46 ccgsl_pointer = 0;
47 count = 0; // initially nullptr will do
48 }
49 // Refines random access container
50 // Refines assignable
57 explicit itersolve( gsl_splinalg_itersolve_type const* T, size_t const n,
58 size_t const m = 0){
59 ccgsl_pointer = gsl_splinalg_itersolve_alloc( T, n, m );
60 // just plausibly we could allocate itersolve but not count
61 try { count = new size_t; } catch( std::bad_alloc& e ){
62 // try to tidy up before rethrowing
63 gsl_splinalg_itersolve_free( ccgsl_pointer );
64 throw e;
65 }
66 *count = 1; // initially there is just one reference to ccgsl_pointer
67 }
74 explicit itersolve( gsl_splinalg_itersolve* v ){
75 ccgsl_pointer = v;
76 // just plausibly we could fail to allocate count: no further action needed.
77 count = new size_t;
78 *count = 1; // initially there is just one reference to ccgsl_pointer
79 }
80 // copy constructor
86 count = v.count; if( count != 0 ) ++*count; }
87 // assignment operator
93 // first, possibly delete anything pointed to by this
94 if( count == 0 or --*count == 0 ){
95 if( ccgsl_pointer != 0 ) gsl_splinalg_itersolve_free( ccgsl_pointer );
96 delete count;
97 } // Then copy
98 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count;
99 return *this;
100 }
101 // destructor
106 if( count == 0 or --*count == 0 ){
107 // could have allocated null pointer
108 if( ccgsl_pointer != 0 ) gsl_splinalg_itersolve_free( ccgsl_pointer );
109 delete count;
110 }
111 }
112#ifdef __GXX_EXPERIMENTAL_CXX0X__
118 std::swap( count, v.count );
119 v.ccgsl_pointer = nullptr;
120 }
127 itersolve( std::move( v ) ).swap( *this );
128 return *this;
129 }
130#endif
131 // Refines equality comparable
132 // == operator
139 bool operator==( itersolve const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
140 // != operator
147 bool operator!=( itersolve const& v ) const { return not operator==( v ); }
148 // Refines forward container
149 // Refines less than comparable
150 // operator<
159 bool operator<( itersolve const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
160 // operator>
169 bool operator>( itersolve const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
170 // operator<=
179 bool operator<=( itersolve const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
180 // operator>=
189 bool operator>=( itersolve const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
194 bool empty() const { return ccgsl_pointer == 0; }
195 // swap() --- should work even if sizes don't match
201 void swap( itersolve& v ){
202 std::swap( ccgsl_pointer, v.ccgsl_pointer );
203 std::swap( count, v.count );
204 }
205 private:
209 gsl_splinalg_itersolve* ccgsl_pointer;
213 size_t* count;
214 public:
215 // shared reference functions
220 gsl_splinalg_itersolve* get() const { return ccgsl_pointer; }
226 bool unique() const { return count != 0 and *count == 1; }
231 size_t use_count() const { return count == 0 ? 0 : *count; }
237#ifdef __GXX_EXPERIMENTAL_CXX0X__
238 explicit
239#endif
240 operator bool() const { return ccgsl_pointer != 0; }
241 public:
246 char const* itersolve_name() const { return gsl_splinalg_itersolve_name( get() ); }
256 int itersolve_iterate( spmatrix const& A, vector const& b, double const tol, vector& x ){
257 return gsl_splinalg_itersolve_iterate( A.get(), b.get(), tol, x.get(), get() ); }
262 double itersolve_normr() const { return gsl_splinalg_itersolve_normr( get() ); }
263 };
264 }
265}
266
267#endif
Workspace for sparse iterative solvers.
Definition: splinalg.hpp:36
itersolve(gsl_splinalg_itersolve_type const *T, size_t const n, size_t const m=0)
The default constructor creates a new itersolve with n elements.
Definition: splinalg.hpp:57
bool operator>=(itersolve const &v) const
A container needs to define an ordering for sorting.
Definition: splinalg.hpp:189
char const * itersolve_name() const
C++ version of gsl_splinalg_itersolve_name().
Definition: splinalg.hpp:246
itersolve(itersolve const &v)
The copy constructor.
Definition: splinalg.hpp:85
itersolve & operator=(itersolve const &v)
The assignment operator.
Definition: splinalg.hpp:92
int itersolve_iterate(spmatrix const &A, vector const &b, double const tol, vector &x)
C++ version of gsl_splinalg_itersolve_iterate().
Definition: splinalg.hpp:256
~itersolve()
The destructor only deletes the pointers if count reaches zero.
Definition: splinalg.hpp:105
gsl_splinalg_itersolve * get() const
Get the gsl_splinalg_itersolve_itersolve.
Definition: splinalg.hpp:220
bool unique() const
Find if this is the only object sharing the gsl_splinalg_itersolve_itersolve.
Definition: splinalg.hpp:226
bool empty() const
Find if the itersolve is empty.
Definition: splinalg.hpp:194
size_t use_count() const
Find how many itersolve objects share this pointer.
Definition: splinalg.hpp:231
gsl_splinalg_itersolve * ccgsl_pointer
The shared pointer.
Definition: splinalg.hpp:209
itersolve(gsl_splinalg_itersolve *v)
Could construct from a gsl_splinalg_itersolve_itersolve.
Definition: splinalg.hpp:74
bool operator<(itersolve const &v) const
A container needs to define an ordering for sorting.
Definition: splinalg.hpp:159
size_t * count
The shared reference count.
Definition: splinalg.hpp:213
bool operator==(itersolve const &v) const
Two itersolve are identically equal if their elements are identical.
Definition: splinalg.hpp:139
gsl_splinalg_itersolve_type const *& gmres
This is a type that can be passed to itersolve()
Definition: splinalg.hpp:41
itersolve & operator=(itersolve &&v)
Move operator.
Definition: splinalg.hpp:126
itersolve()
The default constructor is only really useful for assigning to.
Definition: splinalg.hpp:45
itersolve(itersolve &&v)
Move constructor.
Definition: splinalg.hpp:117
bool operator<=(itersolve const &v) const
A container needs to define an ordering for sorting.
Definition: splinalg.hpp:179
bool operator>(itersolve const &v) const
A container needs to define an ordering for sorting.
Definition: splinalg.hpp:169
bool operator!=(itersolve const &v) const
Two itersolve are different if their elements are not identical.
Definition: splinalg.hpp:147
void swap(itersolve &v)
Swap two itersolve objects.
Definition: splinalg.hpp:201
double itersolve_normr() const
C++ version of gsl_splinalg_itersolve_normr().
Definition: splinalg.hpp:262
This class handles sparse matrix objects as shared handles.
Definition: spmatrix.hpp:38
double get(size_t const i, size_t const j) const
C++ version of gsl_spmatrix_get().
Definition: spmatrix.hpp:402
This class handles vector objects as shared handles.
Definition: vector.hpp:74
gsl_vector * get()
Get the gsl_vector.
Definition: vector.hpp:1320
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
double b(int order, double qq)
C++ version of gsl_sf_mathieu_b().
Definition: sf_mathieu.hpp:298
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34