ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
multilarge.hpp
Go to the documentation of this file.
1/*
2 * $Id$
3 * Copyright (C) 2010, 2018, 2019, 2020, 2021 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_MULTILARGE
21#define CCGSL_MULTILARGE
22
23#include<cmath>
24#include<gsl/gsl_multilarge.h>
25#include"matrix.hpp"
26#include"vector.hpp"
27
28namespace gsl {
32 namespace multilarge {
36 namespace linear {
40 typedef gsl_multilarge_linear_type type;
41 namespace {
45 type const* normal = gsl_multilarge_linear_normal;
49 type const* tsqr = gsl_multilarge_linear_tsqr;
50 }
54 class workspace {
55 public:
60 ccgsl_pointer = 0;
61 count = 0; // initially nullptr will do
62 }
68 explicit workspace( type const* T, size_t const p ){
69 ccgsl_pointer = gsl_multilarge_linear_alloc( T, p );
70 // just plausibly we could allocate workspace but not count
71 try { count = new size_t; } catch( std::bad_alloc& e ){
72 // try to tidy up before rethrowing
73 gsl_multilarge_linear_free( ccgsl_pointer );
74 throw e;
75 }
76 *count = 1; // initially there is just one reference to ccgsl_pointer
77 }
84 explicit workspace( gsl_multilarge_linear_workspace* v ){
85 ccgsl_pointer = v;
86 // just plausibly we could fail to allocate count: no further action needed.
87 count = new size_t;
88 *count = 1; // initially there is just one reference to ccgsl_pointer
89 }
90 // copy constructor
96 count = v.count; if( count != 0 ) ++*count; }
97 // assignment operator
103 // first, possibly delete anything pointed to by this
104 if( count == 0 or --*count == 0 ){
105 if( ccgsl_pointer != 0 ) gsl_multilarge_linear_free( ccgsl_pointer );
106 delete count;
107 } // Then copy
108 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count;
109 return *this;
110 }
111 // destructor
116 if( count == 0 or --*count == 0 ){
117 // could have allocated null pointer
118 if( ccgsl_pointer != 0 ) gsl_multilarge_linear_free( ccgsl_pointer );
119 delete count;
120 }
121 }
122#ifdef __GXX_EXPERIMENTAL_CXX0X__
128 std::swap( count, v.count );
129 v.ccgsl_pointer = nullptr;
130 }
137 workspace( std::move( v ) ).swap( *this );
138 return *this;
139 }
140#endif
141 // Refines equality comparable
142 // == operator
149 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
150 // != operator
157 bool operator!=( workspace const& v ) const { return not operator==( v ); }
158 // Refines forward container
159 // Refines less than comparable
160 // operator<
169 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
170 // operator>
179 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
180 // operator<=
189 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
190 // operator>=
199 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
204 bool empty() const { return ccgsl_pointer == 0; }
205 // swap() --- should work even if sizes don't match
211 void swap( workspace& v ){
212 std::swap( ccgsl_pointer, v.ccgsl_pointer );
213 std::swap( count, v.count );
214 }
215 private:
219 gsl_multilarge_linear_workspace* ccgsl_pointer;
223 size_t* count;
224 public:
225 // shared reference functions
230 gsl_multilarge_linear_workspace* get() const { return ccgsl_pointer; }
236 bool unique() const { return count != 0 and *count == 1; }
241 size_t use_count() const { return count == 0 ? 0 : *count; }
247#ifdef __GXX_EXPERIMENTAL_CXX0X__
248 explicit
249#endif
250 operator bool() const { return ccgsl_pointer != 0; }
251 public:
255 char const* name() const { return gsl_multilarge_linear_name( get() ); }
260 int reset(){ return gsl_multilarge_linear_reset( get() ); }
261 };
266 inline char const* name( workspace const& w ){
267 return gsl_multilarge_linear_name( w.get() ); }
273 inline int reset( workspace& w ){ return gsl_multilarge_linear_reset( w.get() ); }
281 inline int accumulate( matrix& X, vector& y, workspace& w ){
282 return gsl_multilarge_linear_accumulate( X.get(), y.get(), w.get() ); }
292 inline int solve( double const lambda, vector& c, double& rnorm, double& snorm,
293 workspace& w ){
294 return gsl_multilarge_linear_solve( lambda, c.get(), &rnorm, &snorm, w.get() ); }
301 inline int rcond( double& rcond, workspace& w ){
302 return gsl_multilarge_linear_rcond( &rcond, w.get() ); }
311 inline int lcurve( vector& reg_param, vector& rho, vector& eta, workspace& w ){
312 return gsl_multilarge_linear_lcurve( reg_param.get(), rho.get(), eta.get(), w.get() ); }
324 inline int wstdform1( vector const& L, matrix const& X, vector const& w, vector const& y,
325 matrix& Xs, vector& ys, workspace& work ){
326 return gsl_multilarge_linear_wstdform1( L.get(), X.get(), w.get(), y.get(), Xs.get(),
327 ys.get(), work.get() ); }
338 inline int stdform1( vector const& L, matrix const& X, vector const& y, matrix& Xs,
339 vector& ys, workspace& work ){
340 return gsl_multilarge_linear_stdform1( L.get(), X.get(), y.get(), Xs.get(), ys.get(),
341 work.get() ); }
348 inline int L_decomp( matrix& L, vector& tau ){
349 return gsl_multilarge_linear_L_decomp( L.get(), tau.get() ); }
362 inline int
363 wstdform2( matrix const& LQR, vector const& Ltau, matrix const& X, vector const& w,
364 vector const& y, matrix& Xs, vector& ys, workspace& work ){
365 return gsl_multilarge_linear_wstdform2( LQR.get(), Ltau.get(), X.get(), w.get(), y.get(),
366 Xs.get(), ys.get(), work.get() ); }
378 inline int stdform2( matrix const& LQR, vector const& Ltau, matrix const& X,
379 vector const& y, matrix& Xs, vector& ys, workspace& work ){
380 return gsl_multilarge_linear_stdform2( LQR.get(), Ltau.get(), X.get(), y.get(),
381 Xs.get(), ys.get(), work.get() ); }
389 inline int genform1( vector const& L, vector const& cs, vector& c, workspace& work ){
390 return gsl_multilarge_linear_genform1( L.get(), cs.get(), c.get(), work.get() ); }
400 inline int genform2( matrix const& LQR, vector const& Ltau, vector const& cs, vector& c,
401 workspace& work ){
402 return gsl_multilarge_linear_genform2( LQR.get(), Ltau.get(), cs.get(), c.get(),
403 work.get() ); }
409 inline matrix const
410 matrix_ptr( workspace const& work ){
411 gsl_matrix* m
412 = const_cast<gsl_matrix*>( gsl_multilarge_linear_matrix_ptr( work.get() ) );
414 result.wrap_gsl_matrix_without_ownership( m );
415 return result;
416 }
423 inline vector const rhs_ptr( workspace const& work ){
424 gsl_vector* v
425 = const_cast<gsl_vector*>( gsl_multilarge_linear_rhs_ptr( work.get() ) );
427 result.wrap_gsl_vector_without_ownership( v );
428 return result;
429 }
430 }
431 }
432}
433
434#endif
This class handles matrix objects as shared handles.
Definition: matrix.hpp:72
gsl_matrix * get()
Get the gsl_matrix.
Definition: matrix.hpp:1207
Workspace for solving systems.
Definition: multilarge.hpp:54
bool operator!=(workspace const &v) const
Two workspace are different if their elements are not identical.
Definition: multilarge.hpp:157
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: multilarge.hpp:179
void swap(workspace &v)
Swap two workspace objects.
Definition: multilarge.hpp:211
workspace & operator=(workspace &&v)
Move operator.
Definition: multilarge.hpp:136
bool unique() const
Find if this is the only object sharing the gsl_multilarge_linear_workspace.
Definition: multilarge.hpp:236
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
Definition: multilarge.hpp:149
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: multilarge.hpp:241
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: multilarge.hpp:169
bool empty() const
Find if the workspace is empty.
Definition: multilarge.hpp:204
workspace(gsl_multilarge_linear_workspace *v)
Could construct from a gsl_multilarge_linear_workspace*.
Definition: multilarge.hpp:84
int reset()
C++ version of gsl_multilarge_linear_reset().
Definition: multilarge.hpp:260
workspace(workspace &&v)
Move constructor.
Definition: multilarge.hpp:127
workspace(workspace const &v)
The copy constructor.
Definition: multilarge.hpp:95
size_t * count
The shared reference count.
Definition: multilarge.hpp:223
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: multilarge.hpp:189
workspace & operator=(workspace const &v)
The assignment operator.
Definition: multilarge.hpp:102
gsl_multilarge_linear_workspace * get() const
Get the gsl_multilarge_linear_workspace.
Definition: multilarge.hpp:230
workspace()
The default constructor is only really useful for assigning to.
Definition: multilarge.hpp:59
gsl_multilarge_linear_workspace * ccgsl_pointer
The shared pointer.
Definition: multilarge.hpp:219
workspace(type const *T, size_t const p)
The default constructor creates a new workspace with window length K.
Definition: multilarge.hpp:68
~workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: multilarge.hpp:115
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: multilarge.hpp:199
This class handles vector objects as shared handles.
Definition: vector.hpp:74
gsl_vector * get()
Get the gsl_vector.
Definition: vector.hpp:1320
int linear(double const *x, size_t const xstride, double const *y, size_t const ystride, size_t const n, double *c0, double *c1, double *cov00, double *cov01, double *cov11, double *sumsq)
C++ version of gsl_fit_linear().
Definition: fit.hpp:51
int accumulate(matrix &X, vector &y, workspace &w)
C++ version of gsl_multilarge_linear_accumulate().
Definition: multilarge.hpp:281
gsl_multilarge_linear_type type
Typedef for shorthand.
Definition: multilarge.hpp:40
int wstdform2(matrix const &LQR, vector const &Ltau, matrix const &X, vector const &w, vector const &y, matrix &Xs, vector &ys, workspace &work)
C++ version of gsl_multilarge_linear_wstdform2().
Definition: multilarge.hpp:363
int lcurve(vector &reg_param, vector &rho, vector &eta, workspace &w)
C++ version of gsl_multilarge_linear_lcurve().
Definition: multilarge.hpp:311
char const * name(workspace const &w)
Definition: multilarge.hpp:266
int L_decomp(matrix &L, vector &tau)
C++ version of gsl_multilarge_linear_L_decomp().
Definition: multilarge.hpp:348
int wstdform1(vector const &L, matrix const &X, vector const &w, vector const &y, matrix &Xs, vector &ys, workspace &work)
C++ version of gsl_multilarge_linear_wstdform1().
Definition: multilarge.hpp:324
int stdform1(vector const &L, matrix const &X, vector const &y, matrix &Xs, vector &ys, workspace &work)
C++ version of gsl_multilarge_linear_stdform1().
Definition: multilarge.hpp:338
int genform1(vector const &L, vector const &cs, vector &c, workspace &work)
Definition: multilarge.hpp:389
int rcond(double &rcond, workspace &w)
C++ version of gsl_multilarge_linear_rcond().
Definition: multilarge.hpp:301
matrix const matrix_ptr(workspace const &work)
C++ version of gsl_multilarge_linear_matrix_ptr().
Definition: multilarge.hpp:410
vector const rhs_ptr(workspace const &work)
C++ version of gsl_multilarge_linear_rhs_ptr().
Definition: multilarge.hpp:423
int solve(double const lambda, vector &c, double &rnorm, double &snorm, workspace &w)
C++ version of gsl_multilarge_linear_solve().
Definition: multilarge.hpp:292
int genform2(matrix const &LQR, vector const &Ltau, vector const &cs, vector &c, workspace &work)
C++ version of gsl_multilarge_linear_genform2().
Definition: multilarge.hpp:400
int reset(workspace &w)
C++ version of gsl_multilarge_linear_reset().
Definition: multilarge.hpp:273
int stdform2(matrix const &LQR, vector const &Ltau, matrix const &X, vector const &y, matrix &Xs, vector &ys, workspace &work)
C++ version of gsl_multilarge_linear_stdform2().
Definition: multilarge.hpp:378
double eta(double const s)
C++ version of gsl_sf_eta().
Definition: sf_zeta.hpp:181
gsl_sf_result result
Typedef for gsl_sf_result.
Definition: sf_result.hpp:30
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34