実線形等式制約つき最小二乗(LSE)問題

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

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 実線形等式制約つき最小二乗(LSE)問題 (C言語/C++)

Keyword: 実線形等式制約, 最小二乗問題, LSE

概要

本サンプルは実線形等式制約つき最小二乗(LSE)問題を解くC言語によるサンプルプログラムです。 本サンプルは以下に示される実線形等式制約つき最小二乗(LSE)問題を解いて解を出力します。

実行列のデータ 

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

入力データ

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

このデータをダウンロード
nag_dgglse (f08zac) Example Program Data

  6      4      2           :Values of M, N and P

 -0.57  -1.28  -0.39   0.25
 -1.93   1.08  -0.31  -2.14
  2.30   0.24   0.40  -0.35
 -1.93   0.64  -0.66   0.08
  0.15   0.30   0.15  -2.13
 -0.02   1.03  -1.43   0.50 :End of matrix A

  1.00   0.00  -1.00   0.00
  0.00   1.00   0.00  -1.00 :End of matrix B

 -1.50
 -2.14
  1.23
 -0.54
 -1.68
  0.82                      :End of vector c

  0.00
  0.00                      :End of vector d

  • 1行目はタイトル行で読み飛ばされます。
  • 3行目に行列Aの行数(m)、行列Aと行列Bの列数(n)、行列Bの行数(p)を指定しています。
  • 5~10行目に行列Aの要素を指定しています。
  • 12~13行目に行列Bの要素を指定しています。
  • 15~20行目にベクトルCの要素を指定しています。
  • 12~13行目にベクトルDの要素を指定しています。

出力結果

(本関数の詳細はnag_dgglse のマニュアルページを参照)
1
2
3
4
5
6
7

この出力例をダウンロード
nag_dgglse (f08zac) Example Program Results

Constrained least squares solution
     0.4890      0.9975      0.4890      0.9975

Square root of the residual sum of squares
   2.51e-02

  • 4行目に制約つき最小二乗問題の解が出力されています。
  • 7行目に残差平方和の平方根が出力されています。

ソースコード

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

※本サンプルソースコードは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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

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

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagf16.h>

int main(void)
{
  /* Scalars */
  double rnorm;
  Integer i, j, m, n, p, pda, pdb;
  Integer exit_status = 0;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  double *a = 0, *b = 0, *c = 0, *d = 0, *x = 0;

#ifdef nAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I - 1]
#define B(I, J) b[(J-1)*pdb + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J - 1]
#define B(I, J) b[(I-1)*pdb + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_dgglse (f08zac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%ld%*[^\n] ", &m, &n, &p);

#ifdef nAG_COLUMN_MAJOR
  pda = m;
  pdb = p;
#else
  pda = n;
  pdb = n;
#endif

  /* Allocate memory */
  if (!(a = nAG_ALLOC(m * n, double)) ||
      !(b = nAG_ALLOC(p * n, double)) ||
      !(c = nAG_ALLOC(m, double)) ||
      !(d = nAG_ALLOC(p, double)) || !(x = nAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read A, B, C and D from data file */
  for (i = 1; i <= m; ++i) {
    for (j = 1; j <= n; ++j)
      scanf("%lf", &A(i, j));
  }
  scanf("%*[^\n] ");

  for (i = 1; i <= p; ++i) {
    for (j = 1; j <= n; ++j)
      scanf("%lf", &B(i, j));
  }
  scanf("%*[^\n] ");

  for (i = 1; i <= m; ++i)
    scanf("%lf", &c[i - 1]);
  scanf("%*[^\n] ");

  for (i = 1; i <= p; ++i)
    scanf("%lf", &d[i - 1]);
  scanf("%*[^\n] ");

  /* Solve the equality-constrained least squares problem    */
  /* minimize ||c - A*x|| (in the 2-norm) subject to B*x = D */
  nag_dgglse(order, m, n, p, a, pda, b, pdb, c, d, x, &fail);

  if (fail.code == NE_NOERROR) {
    /* Print least squares solution */
    printf("%s\n", "Constrained least squares solution");
    for (i = 1; i <= n; ++i)
      printf("%11.4f%s", x[i - 1], i % 7 == 0 || i == n ? "\n" : " ");

    /* Compute the square root of the residual sum of squares */
    nag_dge_norm(Nag_ColMajor, Nag_FrobeniusNorm, 1, m - n + p, &c[n - p], 1,
                 &rnorm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_dge_norm (f16rac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    printf("\nSquare root of the residual sum of squares\n");
    printf("%11.2e\n", rnorm);
  }
  else {
    printf("Error from nag_dgglse (f08zac).\n%s\n", fail.message);
    exit_status = 1;
  }

END:
  nAG_FREE(a);
  nAG_FREE(b);
  nAG_FREE(c);
  nAG_FREE(d);
  nAG_FREE(x);

  return exit_status;
}


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