金子邦彦研究室プログラミングGSL の活用GSL (GNU Scientific Library) のベクトル,行列に関する機能

GSL (GNU Scientific Library) のベクトル,行列に関する機能

GSL のベクトル,行列に関する機能について,一部分を紹介する.

前準備

GSL のインストール

Windows での GSL のインストール(ソースコードを使用)(MSYS2,configure,make を利用): 別ページ »で説明している.

double の配列に関する最大値,最小値,整列(ソート)

【要点】

  1. MSYS2 MINGW64 を実行する

    スタートメニューで,「MSYS2」の下の「MSYS2 MINGW64」を選ぶ.

  2. プログラムの準備
    #include<stdio.h>
    #include<gsl/gsl_statistics.h>
    
    #define LEN 6
    
    int main(int argc, char** argv)
    {
      double data[LEN] = {10.5, 18.2, 10.3, 15.4, 16.2, 18.3};
    
      double max = gsl_stats_max( data, 1, LEN );
      double min = gsl_stats_min( data, 1, LEN );
      double mean = gsl_stats_mean( data, 1, LEN );
      double sd = gsl_stats_sd( data, 1, LEN );
    
      printf( "max: \t%f \n", max );
      printf( "min: \t%f \n", min );
      printf( "mean: \t%f \n", mean);
      printf( "sd: \t%f \n", sd );
    
      return 0;
    }
    
  3. ビルドして実行
    gcc -I"C:\gsl\include" -c -o a.o a.c
    gcc -L"C:\gsl\lib" -o a.exe a.o -lgsl -lgslcblas
    ./a.exe
    

    [image]

機能まとめ

関連する外部ページ

乱数発生器

【要点】

  1. MSYS2 MINGW64 を実行する

    スタートメニューで,「MSYS2」の下の「MSYS2 MINGW64」を選ぶ.

  2. プログラムの準備
    #include<stdio.h>
    
    // この3行は必須
    #include<gsl/gsl_rng.h>
    #include<gsl/gsl_randist.h>
    #include<gsl/gsl_statistics.h>
    
    
    #define LEN 100
    
    int main(int argc, char** argv)
    {
      double a[LEN];
      double b[LEN];
    
      gsl_rng_env_setup();
      gsl_rng_type *T = (gsl_rng_type *)gsl_rng_default;
      /* 乱数発生器 */
      gsl_rng *r = gsl_rng_alloc(T);
      /* システムクロックを使って乱数の初期値を設定 */
      gsl_rng_set (r, time (NULL));
    
      int i;
      for(i=0; i<LEN; i++) {
        a[i] =  gsl_rng_uniform(r); 
      }
      for(i=0; i<LEN; i++) {
        b[i] = gsl_ran_gaussian(r, 1.0);
      }
    
      for(i=0; i<LEN; i++) {
        printf( "%d, \t%7.5f, \t%7.5f \n",
            i, a[i], b[i] );
      }
    
      printf( "--, \t-------, \t%-------\n") ; 
      printf( "mean: \t%7.5f, \t%7.5f \n",
          gsl_stats_mean(a, 1, LEN), gsl_stats_mean(b, 1, LEN) ); 
      printf( "SD: \t%7.5f, \t%7.5f \n",
          gsl_stats_sd(a, 1, LEN), gsl_stats_sd(b, 1, LEN) );
    
      gsl_rng_free(r);
    }
    
  3. ビルドして実行
    gcc -I"C:\gsl\include" -c -o a.o a.c
    gcc -L"C:\gsl\lib" -o a.exe a.o -lgsl -lgslcblas
    ./a.exe
    

    [image]

要点まとめ

double 型の GSL ベクトル

【要点】

ベクトルは,同じデータ型のデータの並び. 変数 v に,double 型の要素 2, 3, 4, 1 のベクトル(要素がこの順で並ぶ)を格納したいときは, 次のように書く.

gsl_vector *v = gsl_vector_alloc(4); // gsl_vector_alloc() は,double 型の要素を持つ GSL ベクトルの確保.中身は初期化されない.
gsl_vector_set(v, 0, 2.0);
gsl_vector_set(v, 1, 3.0);
gsl_vector_set(v, 2, 4.0);
gsl_vector_set(v, 3, 1.0);
gsl_vector_free(v); // 領域の解放

下の1行は必須

#include<gsl/gsl_vector.h>

ベクトルの領域確保と解放

gsl_vector_double *v = gsl_vector_alloc(4); // gsl_vector_alloc() は,double 型の要素を持つ GSL ベクトルの確保.中身は初期化されない.
gsl_vector_free(v); // 領域の解放

ベクトルの組み立て

ベクトルの値の取得

ベクトルに関する2項演算

集約演算

ファイル読み出し,書き込み

関連する外部ページ

GSL 行列

【要点】

変数 M に,要素 1, 2, 3, 4 が入った2行2列の行列,つまり, 1 3 2 4 を格納したいときは,次のように書く. 「gsl_matrix_alloc(2, 2)」の最初の 2 が行数.次の 2 が列数.

gsl_matrix *M = gsl_matrix_alloc(2, 2); // gsl_matrix_alloc() は,double 型の要素の確保.中身は初期化されない.
gsl_matrix_set(M, 0, 0, 1.0);
gsl_matrix_set(M, 1, 0, 2.0);
gsl_matrix_set(M, 0, 1, 3.0);
gsl_matrix_set(M, 1, 1, 4.0);
gsl_matrix_free(M); // 領域の解放

または

double d = { 1.0, 3.0,
             2.0, 4.0 };
gsl_matrix_view MV = gsl_matrix_view_array( a, 2, 2 );
gsl_matrix v = &MV.matrix

下の1行は必須

#include<gsl/gsl_matrix.h>

ベクトルの領域確保と解放

gsl_matrix *M = gsl_matrix_alloc(2, 2); // gsl_matrix_alloc() は,double 型の要素の確保.中身は初期化されない.
gsl_matrix_free(M); // 領域の解放

行列の組み立て

行列の値の取得,ベクトル像の取得

ベクトル像は,ベクトルと同様に扱うことができる.

ファイル読み出し,書き込み

関連する外部ページ

BLAS を呼び出す機能

BLAS(Basic Linear Algebra Subprograms)とは,行列演算,ベクトル演算の機能(下記)をもったプログラム群. GSL には,BLAS の機能を呼び出す機能があります.

関連する外部ページ】:

固有値,固有ベクトル

【要点】

実数対称行列

実数非対称行列

[image]

[image]

関連する外部ページ