11 #ifndef GSL_VECTOR_WRAP_H_KJBLIB_UARIZONAVISION
12 #define GSL_VECTOR_WRAP_H_KJBLIB_UARIZONAVISION
14 #include <l/l_sys_sys.h>
21 #include "gsl/gsl_vector.h"
24 #warning "GNU GSL is absent, so GNU GSL wrapper will not work properly."
39 double&
at(
size_t n ) {
return p[n %
size]; }
40 const double&
at(
size_t n )
const {
return p[n %
size]; }
42 #define gsl_vector_alloc(x) (new gsl_vector(x))
52 #define gsl_vector_set(w,i,x) ((w)->at(i)=(x))
53 #define gsl_vector_get(w,i) ((w)->at(i))
57 #if ! defined( KJB_HAVE_ISNAN ) || ! defined( KJB_HAVE_FINITE )
58 #warning "This code tests for NAN and finite values but your system lacks"
59 #warning "this feature. Method Gsl_Vector::is_normal will always return true."
89 ETX_2( 00 == m_vec,
"Memory allocation failure for Gsl_Vector" );
93 void check_bounds(
size_t index )
const
102 if (
size() <= index )
129 for(
size_t iii = 0; iii < KVSZ; ++iii )
131 at( iii ) = kv.
at( iii );
145 GSL_ETX( gsl_vector_memcpy( m_vec, src.m_vec ) );
158 GSL_ETX( gsl_vector_memcpy( m_vec, &vec_to_be_copied ) );
169 if ( m_vec != src.m_vec )
173 GSL_ETX( gsl_vector_memcpy( m_vec, src.m_vec ) );
188 return gsl_vector_ptr( m_vec, 0 );
197 return gsl_vector_const_ptr( m_vec, 0 );
206 return gsl_vector_ptr( m_vec,
size() );
215 return gsl_vector_const_ptr( m_vec,
size() );
228 swap( m_vec, v.m_vec );
237 gsl_vector_free( m_vec );
245 template <
class Iterator >
252 for( Iterator cur = begin; cur !=
end; ++cur )
266 return m_vec ->
size;
292 double at(
size_t index )
const
296 check_bounds( index );
299 return m_vec ->
at( index );
305 double&
at(
size_t index )
309 check_bounds( index );
310 return *gsl_vector_ptr( m_vec, index );
312 return m_vec ->
at( index );
354 gsl_vector_set_all( m_vec, val );
365 gsl_vector_set_zero( m_vec );
381 GSL_ETX( gsl_vector_set_basis( m_vec, index ) );
406 for(
size_t iii = 0; iii <
size(); ++iii )
408 kv.
at( iii ) =
at( iii );
438 GSL_ETX( gsl_vector_mul( product.m_vec, that.m_vec ) );
450 for(
size_t iii = 0; iii <
size(); ++iii )
452 sum_vec +=
at( iii );
464 return this_x_that.
sum();
474 return sqrt(
dot( *
this ) );
496 for(
size_t iii = 0; iii <
size(); ++iii )
498 double x =
at( iii );
501 if ( isnand( x ) || ! finite( x ) )
518 GSL_ETX( gsl_vector_add( m_vec, that.m_vec ) );
540 GSL_ETX( gsl_vector_sub( m_vec, that.m_vec ) );
562 GSL_ETX( gsl_vector_scale( m_vec, scalar ) );
579 GSL_ETX( gsl_vector_reverse( m_vec ) );
589 return product *= scalar;
597 GSL_ETX( gsl_vector_add_constant( m_vec, scalar ) );
607 return vsum += scalar;
621 return vsum -= scalar;
637 return gsl_vector_max( m_vec );
647 return gsl_vector_min( m_vec );
663 return vector * scalar;
674 for(
size_t iii = 0; iii < v.
size(); ++iii )
676 os << v.
at( iii ) <<
'\n';
694 for(
size_t iii = 0; iii < v.
size(); ++iii )
Definition: gsl_vector.h:35
Gsl_Vector & set_basis(size_t index)
Init to the (j+1)th column of identity matrix, given argument j.
Definition: gsl_vector.h:377
size_t size
Definition: gsl_vector.h:36
~Gsl_Vector()
dtor releases the resources in a vector
Definition: gsl_vector.h:233
Gsl_Vector & operator+=(double scalar)
add scalar to each elt. of this vector, in-place
Definition: gsl_vector.h:594
double * p
Definition: gsl_vector.h:36
Gsl_Vector operator-(const Gsl_Vector &that) const
Return the difference of two vectors of the same size.
Definition: gsl_vector.h:547
double sum() const
Return the sum of the elements of the vector.
Definition: gsl_vector.h:447
Gsl_Vector & operator=(const Gsl_Vector &src)
assignment operator
Definition: gsl_vector.h:166
Gsl_Vector(Iterator begin, Iterator end)
ctor constructs from a pair of input iterators
Definition: gsl_vector.h:246
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
Gsl_Vector & set_all(double val)
set all vector members to a given value
Definition: gsl_vector.h:350
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
double & at(size_t n)
Definition: gsl_vector.h:39
const double * begin() const
Definition: gsl_vector.h:194
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Gsl_Vector operator-(double scalar) const
Return copy of this vector w/ a scalar subtracted from each elt.
Definition: gsl_vector.h:618
Gsl_Vector & ow_reverse()
Reverse the order of the elements of the vector, in place.
Definition: gsl_vector.h:576
#define KJB(x)
Definition: l_util.h:9
Gsl_Vector operator-() const
Return copy of this vector with every element negated.
Definition: gsl_vector.h:626
Value_type & at(int i)
Safely subscript vector, e.g., A.at(10), to get an lvalue.
Definition: m_vector.h:991
#define KJB_THROW(ex)
Definition: l_exception.h:46
Gsl_Vector & ow_multiply_vector_by_scalar(double scalar)
Scale by a real value, like kjb_c::ow_multiply_vector_by_scalar.
Definition: gsl_vector.h:559
Gsl_Vector operator+(const Gsl_Vector &that) const
Return the sum of this vector and another vector of same size.
Definition: gsl_vector.h:525
#define ASSERT(condition, message)
Definition: Assert.h:45
Gsl_Vector & operator*=(double scalar)
Scale by a real value, like ow_multiply_vector_by_scalar.
Definition: gsl_vector.h:569
double front() const
access the first element of the vector, returning an rvalue
Definition: gsl_vector.h:326
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
GSL utility stuff to help those using the C++ wrapper on GSL code.
Gsl_Vector(size_t dims)
ctor to build an uninitialized vector of a given size
Definition: gsl_vector.h:111
std::ostream & operator<<(std::ostream &os, const kjb::Gsl_Vector &v)
Print out a vector as a column of ASCII-rendered values + newlines.
Definition: gsl_vector.h:672
void swap(Gsl_Vector &v)
swap the contents of two vectors
Definition: gsl_vector.h:223
bool is_normal() const
Test whether all vector entries are "normal" values.
Definition: gsl_vector.h:490
#define gsl_vector_get(w, i)
Definition: gsl_vector.h:53
size_t dims(const Scene &scene, bool respect_changed=true, bool infer_head=true)
Computes the number of variables in this scene.
Definition: pt_scene.cpp:106
~gsl_vector()
Definition: gsl_vector.h:38
gsl_vector(size_t n)
Definition: gsl_vector.h:37
double * end()
Definition: gsl_vector.h:203
Gsl_Vector & operator-=(double scalar)
subtract scalar from each elt. of this vector, in-place
Definition: gsl_vector.h:611
Gsl_Vector & set_zero()
set all vector members to zero
Definition: gsl_vector.h:361
Gsl_Vector operator+(double scalar) const
Return copy of this vector with a scalar added to each elt.
Definition: gsl_vector.h:604
x
Definition: APPgetLargeConnectedEdges.m:100
size_t size() const
returns the size of the vector
Definition: gsl_vector.h:263
#define GSL_ETX(gsl_expr)
Definition: gsl_util.h:37
double dot(const Gsl_Vector &that) const
Return the dot product of two vectors.
Definition: gsl_vector.h:461
double at(size_t index) const
element access for the vector, returning an rvalue
Definition: gsl_vector.h:292
const double * end() const
Definition: gsl_vector.h:212
RAII wrapper for GSL vector objects.
Definition: gsl_vector.h:78
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
double max() const
return the maximum value in the vector
Definition: gsl_vector.h:633
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Gsl_Vector operator*(double scalar) const
Scale by a real value and return the result.
Definition: gsl_vector.h:586
double * begin()
Definition: gsl_vector.h:185
#define ETX_2(a, msg)
Definition: l_exception.h:78
double & back()
access the last element of the vector, returning an lvalue
Definition: gsl_vector.h:334
const double & at(size_t n) const
Definition: gsl_vector.h:40
Gsl_Vector(const gsl_vector &vec_to_be_copied)
ctor copies an unwrapped GSL vector, does NOT take ownership.
Definition: gsl_vector.h:153
Gsl_Vector & operator+=(const Gsl_Vector &that)
Add a vector of the same size to this vector, elementwise.
Definition: gsl_vector.h:511
double & at(size_t index)
element access for the vector, returning an lvalue
Definition: gsl_vector.h:305
#define gsl_vector_alloc(x)
Definition: gsl_vector.h:42
double & front()
access the first element of the vector, returning an lvalue
Definition: gsl_vector.h:318
double min() const
return the minimum value in the vector
Definition: gsl_vector.h:643
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
Gsl_Vector(const Gsl_Vector &src)
copy ctor
Definition: gsl_vector.h:140
kjb::Vector vec() const
Export contents in a KJB Vector (the C++ kind).
Definition: gsl_vector.h:403
Gsl_Vector & operator-=(const Gsl_Vector &that)
Subtract a vector of the same size from this vector, elementwise.
Definition: gsl_vector.h:533
Gsl_Vector(const kjb::Vector &kv)
conversion ctor makes a copy of a KJB vector
Definition: gsl_vector.h:123
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
double back() const
access the last element of the vector, returning an rvalue
Definition: gsl_vector.h:342
double l2_norm() const
Return the L2 norm (the Euclidean norm) of the vector.
Definition: gsl_vector.h:471
std::istream & operator>>(std::istream &is, kjb::Gsl_Vector &v)
Read in ASCII-rendered, WS-delimited contents to a presized vector.
Definition: gsl_vector.h:691
Gsl_Vector ew_multiply(const Gsl_Vector &that) const
Perform element-wise multiplcation of two vectors, same size.
Definition: gsl_vector.h:430