Hi Shikai Luo,

I agree with the previous mail from John D Lamb, use C++11 whenever possible.

I appended a minimal example for some usage of gsl stuff, e.g. using unique_ptr, STL, etc.

Best wishes,

    Klaus.


On 16.02.2014 21:56, John D Lamb wrote:
On 16/02/14 16:54, Shikai Luo wrote:
I was just wondering how GSL decide the stride when I allocate memory
for a gsl_vector?

You decide the stride. If you use gsl_vector_alloc() the stride is 1. But if you use, for example gsl_vector_subvector_with_stride() you can choose a different stride.

I need to use STL generic algorithms on gsl vector and matrix, so I need
the stride to be 1,

You could try some C++ wrappers I wrote for gsl, http://ccgsl.sourceforge.net/ These cover most of gsl-1.16 except things like sorting, which STL provides. These are just headers so you could extract the files you need for gsl::vector and gsl::matrix. These are thoroughly tested and allow you to do stuff like:

using namespace std;
using namespace gsl;

vector v( 7 );
for( size_t i = 0; i < v.size(); ++i )
  v[i] = sqrt( i );

or even, with a gsl::matrix M and C++11,

for( size_t i { 0 }; i < M.size2(); ++i )
  auto col { M.col( i ) };
  for( auto e : col )
    cout << e << " ";
  cout << endl;

Note that the stride doesn’t matter here because the vector class has an iterator that works with any stride.

Alternatively, try Andrew Steiner’s
http://o2scl.sourceforge.net/
which has similar functionality.


#include <algorithm>
#include <gsl/gsl_sort.h>
#include <gsl/gsl_vector.h>
#include <iostream>
#include <memory>
#include <numeric>
#include <vector>

int main()
{
	/* gsl vector */
	unsigned int vector_size = 3;

	std::unique_ptr<gsl_vector,void(*)(gsl_vector*)>
		v( gsl_vector_alloc( vector_size ), gsl_vector_free );

	for(unsigned int i = 0; i < vector_size; ++i)
		gsl_vector_set( v.get(), i, i );

	for(unsigned int i = 0; i < vector_size; ++i)
		std::cout << gsl_vector_get( v.get(), i ) << std::endl;

	std::cout << std::endl;

	/* c++11 vector */
	std::vector<double> vec = { 2, -1, 0 };

	gsl_sort( vec.data(), 1, vec.size() );

	for(double x : vec)
		std::cout << x << std::endl;

	std::cout << std::endl;

	/* using STL */
	std::cout << std::max( vec.front(), vec.back() ) << std::endl;

	return 0;
}

<<attachment: huthmacher.vcf>>

Reply via email to