3次スプラインの評価

C言語によるサンプルソースコード : 使用関数名:nag_1d_spline_evaluate (e02bbc)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 3次スプラインの評価 (C言語/C++)

Keyword: 3次, スプライン

概要

本サンプルは3次スプラインの評価を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについて3次スプラインの値を求めて出力します。

3次スプラインのデータ 

※本サンプルはnAG Cライブラリに含まれる関数 nag_1d_spline_evaluate() のExampleコードです。本サンプル及び関数の詳細情報は nag_1d_spline_evaluate のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はnag_1d_spline_evaluate のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

このデータをダウンロード
nag_1d_spline_evaluate (e02bbc) Example Program Data
  9
   4
     1.00
     1.00
     1.00
     1.00
     3.00
     6.00
     8.00
     9.00
     9.00
     9.00
     9.00
     1.00
     2.00
     4.00
     7.00
     6.00
     4.00
     3.00

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目にスプラインを計算する点の数(m)を指定しています。
  • 3行目にスプラインが定義される区間の数(ncap)を指定しています。
  • 4~14行目に拡張されたノット(lamda)を指定しています。
  • 15~21行目にBスプライン係数(c)を指定しています。

出力結果

(本関数の詳細はnag_1d_spline_evaluate のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

この出力例をダウンロード
nag_1d_spline_evaluate (e02bbc) Example Program Results
Augmented set of knots stored in spline.lamda:
    1.0000     1.0000     1.0000     1.0000     3.0000     6.0000
    8.0000     9.0000     9.0000     9.0000     9.0000

B-spline coefficients stored in spline.c

    1.0000     2.0000     4.0000     7.0000     6.0000     4.0000
    3.0000

      x         Value of cubic spline

    1.0000         1.0000
    2.0000         2.3779
    3.0000         3.6229
    4.0000         4.8327
    5.0000         5.8273
    6.0000         6.3571
    7.0000         6.1905
    8.0000         5.1667
    9.0000         3.0000

  • 2~4行目にspline.lamdaに格納された拡張ノットが出力されています。
  • 6~9行目にspline.cに格納されたBスプライン係数が出力されています。
  • 11~21行目に3次スプラインの値が出力されています。

ソースコード

(本関数の詳細はnag_1d_spline_evaluate のマニュアルページを参照)

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

このソースコードをダウンロード
/* nag_1d_spline_evaluate (e02bbc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nage02.h>

int main(void)
{
  Integer exit_status = 0, j, m, ncap, ncap7, r;
  Nag_Spline spline;
  double a, b, s, x;
  NagError fail;

  INIT_FAIL(fail);

  /* Initialize spline */
  spline.lamda = 0;
  spline.c = 0;

  printf("nag_1d_spline_evaluate (e02bbc) Example Program Results\n");
  scanf("%*[^\n]"); /* Skip heading in data file */
  while (scanf("%ld", &m) != EOF)
  {
    if (m <= 0) {
      printf("Invalid m.\n");
      exit_status = 1;
      return exit_status;
    }
    scanf("%ld", &ncap);
    ncap7 = ncap + 7;
    if (ncap > 0) {
      spline.n = ncap7;
      if (!(spline.c = nAG_ALLOC(ncap7, double)) ||
          !(spline.lamda = nAG_ALLOC(ncap7, double)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
    }
    else {
      printf("Invalid ncap.\n");
      exit_status = 1;
      return exit_status;
    }
    for (j = 0; j < ncap7; j++)
      scanf("%lf", &(spline.lamda[j]));
    for (j = 0; j < ncap + 3; j++)
      scanf("%lf", &(spline.c[j]));
    a = spline.lamda[3];
    b = spline.lamda[ncap + 3];
    printf("Augmented set of knots stored in spline.lamda:\n");
    for (j = 0; j < ncap7; j++)
      printf("%10.4f%s", spline.lamda[j],
             (j % 6 == 5 || j == ncap7 - 1) ? "\n" : " ");
    printf("\nB-spline coefficients stored in spline.c\n\n");
    for (j = 0; j < ncap + 3; j++)
      printf("%10.4f%s", spline.c[j],
             (j % 6 == 5 || j == ncap + 2) ? "\n" : " ");
    printf("\n      x         Value of cubic spline\n\n");
    for (r = 1; r <= m; ++r) {
      x = ((double) (m - r) * a + (double) (r - 1) * b) / (double) (m - 1);
      /* nag_1d_spline_evaluate (e02bbc).
       * Evaluation of fitted cubic spline, function only
       */
      nag_1d_spline_evaluate(x, &s, &spline, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_1d_spline_evaluate (e02bbc).\n%s\n",
               fail.message);
        exit_status = 1;
        goto END;
      }
      printf("%10.4f%15.4f\n", x, s);
    }
    nAG_FREE(spline.c);
    nAG_FREE(spline.lamda);
  }
END:
  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks