Programowanie w systemie UNIX/GSL

InstalacjaEdytuj

  • ze źródeł
  • skompilowane pakiety

pakietyEdytuj

W Ubuntu 14.04 są pakiety:[1]

  • gsl-bin: GNU Scientific Library (GSL) -- binary package
  • libgsl0-dbg: GNU Scientific Library (GSL) -- debug symbols package
  • libgsl0-dev: GNU Scientific Library (GSL) -- development package
  • libgsl0ldbl: GNU Scientific Library (GSL) -- library package

Instalacja

 sudo apt-get install libgsl0ldbl
 sudo apt-get install libgsl0-dev


Sprawdzamy biblioteki:

gsl-config --libs

Otrzymujemy informacje o katalogu i opcjach linkera:

-L/usr/lib -lgsl -lgslcblas -lm


Inna metoda: [2]

sudo apt-get install gsl-bin
sudo apt-get -y install libgsl-dev

Pierwszy programEdytuj

Poniższy przykładowy program oblicza wartość funkcji Bessela[3] dla argumentu 5[4]:

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main(void)
{
  double x = 5.0;
  double y = gsl_sf_bessel_J0(x);
  printf("J0(%g) = %.18e\n", x, y);
  return 0;
}

KompilacjaEdytuj

ręcznaEdytuj

gcc -L/usr/lib -lgsl -lgslcblas -lm example.c

z użyciem gsl-configEdytuj

gcc $(gsl-config --cflags) example.c $(gsl-config --libs)

makeEdytuj

Opis [5]

Wynik pracy programuEdytuj

Wynik powinien być poprawny dla podwójnej precyzji:


J0(5) = -1.775967713143382920e-01

SilniaEdytuj

W formie naturalnejEdytuj

#include <stdio.h>
#include <gsl/gsl_sf.h>


// gcc -I/usr/include/gsl/ -L/usr/local/lib/ -lgsl -lgslcblas -lm f.c
// http://www.gnu.org/software/gsl/manual/html_node/Special-Function-Usage.html#Special-Function-Usage
// The natural form returns only the value of the function and can be used directly in mathematical expressions.

int main(void)
{
  unsigned int n = 100;
  double result;

  result = gsl_sf_fact(n);


 
  printf("factorial( %d ) = %.0f\n", n,result);
  

  return 0;
}


Wynik działania:

./a.out
factorial( 100 ) = 933262154439441509656467047959538825784009703731840988310128895405822272385704312950661130
89288327277825849664006524270554535976289719382852181865895959724032


W formie wychwytującej błędyEdytuj

#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf.h>


// gcc -I/usr/include/gsl/ -L/usr/local/lib/ -lgsl -lgslcblas -lm f.c
// gcc -I/usr/include/gsl/ -L/usr/lib/ -lgsl -lgslcblas -lm f.c
// factorial
// http://www.gnu.org/software/gsl/manual/html_node/Special-Function-Usage.html#Special-Function-Usage
// The error-handling function
// http://www.gnu.org/software/gsl/manual/html_node/Special-Functions-Examples.html#Special-Functions-Examples
/*




*/
 
int main(void)
{
  unsigned int n = 100;
  int status; 
  gsl_sf_result result;

  status = gsl_sf_fact_e (n, &result);


  printf("status  = %s\n", gsl_strerror(status));
  printf("factorial( %2d ) = %f\n", n,result.val);
  printf("+/- % .18f\n",result.err); 

  return 0;
}


Wynik:

./a.out
status  = success
factorial( 100 ) =    933262154439441509656467047959538825784009703731840988310128895405822272385704312950661130
89288327277825849664006524270554535976289719382852181865895959724032
+/-  4144516527479786869998377376409676501474209436767641660238087976812259116180322817471642386
      5792481641279576393802186138583881092629521428905984.000000000000000000

Znajdowanie zer równańEdytuj

Wielowymiarowe metody[6]

Aproksymacja wielomianowaEdytuj

/*
https://rosettacode.org/wiki/Polynomial_regression
Polynomial regression

Find an approximating polynomial of known degree for a given data.

Example: For input data:

x = {0,  1,  2,  3,  4,  5,  6,   7,   8,   9,   10};
y = {1,  6,  17, 34, 57, 86, 121, 162, 209, 262, 321};

The approximating polynomial is: 3x^2 + 2x + 1
Here, the polynomial's coefficients are (3, 2, 1).

one can check it online: https://arachnoid.com/polysolve/

=========================================

gcc -Wall -I/usr/local/include  -c p.c
gcc -L/usr/local/lib p.o -lgsl -lgslcblas -lm

./a.out
1.000000
2.000000
3.000000

*/

#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_multifit.h> // 
#include <stdbool.h>
#include <math.h>


// http://www.gnu.org/software/gsl/manual/html_node/Fitting-Examples.html here] helped alot to code this quickly):



#define NP 11
double x[] = {0,  1,  2,  3,  4,  5,  6,   7,   8,   9,   10};
double y[] = {1,  6,  17, 34, 57, 86, 121, 162, 209, 262, 321};

#define DEGREE 3
double coeff[DEGREE];


bool polynomialfit(int obs, int degree, 
		   double *dx, double *dy, double *store) /* n, p */
{
  gsl_multifit_linear_workspace *ws;
  gsl_matrix *cov, *X;
  gsl_vector *y, *c;
  double chisq;

  int i, j;

  X = gsl_matrix_alloc(obs, degree);
  y = gsl_vector_alloc(obs);
  c = gsl_vector_alloc(degree);
  cov = gsl_matrix_alloc(degree, degree);

  for(i=0; i < obs; i++) {
    for(j=0; j < degree; j++) {
      gsl_matrix_set(X, i, j, pow(dx[i], j));
    }
    gsl_vector_set(y, i, dy[i]);
  }

  ws = gsl_multifit_linear_alloc(obs, degree);
  gsl_multifit_linear(X, y, c, cov, &chisq, ws);

  /* store result ... */
  for(i=0; i < degree; i++)
  {
    store[i] = gsl_vector_get(c, i);
  }

  gsl_multifit_linear_free(ws);
  gsl_matrix_free(X);
  gsl_matrix_free(cov);
  gsl_vector_free(y);
  gsl_vector_free(c);
  return true; /* we do not "analyse" the result (cov matrix mainly)
		  to know if the fit is "good" */
}

int main()
{
  int i;

  polynomialfit(NP, DEGREE, x, y, coeff);
  for(i=0; i < DEGREE; i++) {
    printf("%lf\n", coeff[i]);
  }
  return 0;
}

ODEEdytuj

OdnośnikiEdytuj

  1. Ask Ubuntu : Install GNU Scientific library (GSL) on Ubuntu 14.04 via terminal
  2. askubuntu question :how-can-i-run-an-c-c-program-that-use-gnu-scientific-library-gsl
  3. Funkcje Bessela w wikipedii
  4. An Example Program – GNU Scientific Library – Reference Manual
  5. Getting started with GSL by Darren Wilkinson
  6. gsl: Example-programs-for-Multidimensional-Root-finding