pspline 5.0.5 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -5
  3. data/README.md +44 -43
  4. data/Rakefile +6 -6
  5. data/bin/console +14 -14
  6. data/bin/setup +8 -8
  7. data/ext/pspline/basis.cpp +394 -351
  8. data/ext/pspline/example/exbspline.rb +57 -57
  9. data/ext/pspline/example/excspline.rb +57 -57
  10. data/ext/pspline/example/exdspline.rb +55 -55
  11. data/ext/pspline/example/exfspline.rb +44 -44
  12. data/ext/pspline/example/exfspline1.rb +40 -40
  13. data/ext/pspline/example/exfspline2.rb +68 -68
  14. data/ext/pspline/example/exfspline3.rb +64 -64
  15. data/ext/pspline/example/exmspline.rb +68 -68
  16. data/ext/pspline/example/expspline.rb +29 -29
  17. data/ext/pspline/example/expspline1.rb +29 -29
  18. data/ext/pspline/example/expspline2.rb +47 -47
  19. data/ext/pspline/example/exqspline.rb +31 -31
  20. data/ext/pspline/example/exqspline1.rb +31 -31
  21. data/ext/pspline/example/exqspline2.rb +50 -50
  22. data/ext/pspline/example/exqspline3.rb +51 -51
  23. data/ext/pspline/example/exqspline4.rb +35 -35
  24. data/ext/pspline/example/exrspline.rb +34 -34
  25. data/ext/pspline/example/exrspline1.rb +34 -34
  26. data/ext/pspline/example/exrspline2.rb +44 -44
  27. data/ext/pspline/example/exsspline.rb +35 -35
  28. data/ext/pspline/example/exsspline1.rb +35 -35
  29. data/ext/pspline/example/extspline.rb +54 -54
  30. data/ext/pspline/extconf.rb +7 -7
  31. data/ext/pspline/fft.cpp +27 -552
  32. data/ext/pspline/include/basis/basis.h +145 -137
  33. data/ext/pspline/include/basis/fft.h +188 -152
  34. data/ext/pspline/include/basis/fft_complex.h +215 -0
  35. data/ext/pspline/include/basis/fft_real.h +625 -0
  36. data/ext/pspline/include/basis/gabs.h +35 -0
  37. data/ext/pspline/include/basis/marray_class_ext.h +568 -0
  38. data/ext/pspline/include/basis/marray_ext.h +100 -0
  39. data/ext/pspline/include/basis/matrix_luc_ext.h +300 -0
  40. data/ext/pspline/include/basis/matrix_lud_ext.h +298 -0
  41. data/ext/pspline/include/basis/poly.h +454 -0
  42. data/ext/pspline/include/basis/poly_array.h +1030 -1568
  43. data/ext/pspline/include/basis/pspline.h +806 -642
  44. data/ext/pspline/include/basis/real.h +526 -0
  45. data/ext/pspline/include/basis/real_inline.h +442 -0
  46. data/ext/pspline/include/basis/spline.h +83 -0
  47. data/ext/pspline/include/basis/uspline.h +251 -210
  48. data/ext/pspline/include/basis/util.h +122 -656
  49. data/ext/pspline/include/bspline.h +71 -377
  50. data/ext/pspline/include/bspline_Config.h +8 -2
  51. data/ext/pspline/include/real_config.h +3 -0
  52. data/ext/pspline/pspline.cpp +1236 -1038
  53. data/ext/pspline/real.cpp +1607 -0
  54. data/ext/pspline/real_const.cpp +585 -0
  55. data/lib/pspline.rb +71 -71
  56. data/lib/pspline/version.rb +1 -1
  57. data/pspline.gemspec +25 -25
  58. metadata +17 -5
  59. data/ext/pspline/plotsub.cpp +0 -139
  60. data/ext/pspline/util.cpp +0 -483
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8dacedf68daad7cf3e2b1e3f8aa1e59ccce2408d
4
- data.tar.gz: 948d586d9d991821a7e0d640ad9ef63fd98fc074
3
+ metadata.gz: 1f7cd8119c7e0de6c4f6a72d40a7a40c170835ec
4
+ data.tar.gz: e8a52f742dc9fb6ead4d2676ebee54f2e12a2cda
5
5
  SHA512:
6
- metadata.gz: c61c3af1bfaed26c57d94e144ad9e6546a5e7a21e2b194d3a703f90640b2beafc87450e4752b1399cc452ecba6cf41e154dee27359912ef5977ba396946dd5d5
7
- data.tar.gz: 386924860c51388e0ddbd74fc6e95dca67dd771556c13b2d58500e3b833bb476003cd40b539bd496862012f11371e9ecd4e8a1999cb05a4bea2baaba376244bb
6
+ metadata.gz: 5905c1d7434b3ce52b8502f418ec027f63a02c0f21de53c584dc455b79fdee10db99e64f5597674712c926bd679be88971c308aa3b5aac40fb08efecc77f4378
7
+ data.tar.gz: 03acfcff0d95f0762c69cd4d1a16d0ae6a82d3dd189793bd24f6db6229cfdb8c0082f4b19eebdca608d16ef2c12e8672f3960dc11277057b27e677208667734a
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in pspline.gemspec
4
- gemspec
5
- gem "rake-compiler"
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pspline.gemspec
4
+ gemspec
5
+ gem "rake-compiler"
data/README.md CHANGED
@@ -1,43 +1,44 @@
1
- # Pspline
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pspline`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'pspline'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install pspline
20
-
21
- ## Usage
22
-
23
- See pspline.wiki at https://github.com/vsp2old/pspline .
24
-
25
- ## Revision history
26
-
27
- 5.0.0 Upload for test.
28
- 5.0.1 Upload for test.
29
- 5.0.2 Upload for release.
30
- 5.0.3 Add class PSPLINE::Rfft, class PSPLINE::Cfft.
31
- 5.0.4 Add method line_integral.
32
- 5.0.5 Add Multiple Bspline.
33
-
34
- ## Development
35
-
36
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
-
38
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
-
40
- ## Contributing
41
-
42
- Bug reports and pull requests are welcome on GitHub at https://github.com/vsp2old/pspline .
43
-
1
+ # Pspline
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pspline`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pspline'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pspline
20
+
21
+ ## Usage
22
+
23
+ See pspline.wiki at https://github.com/vsp2old/pspline .
24
+
25
+ ## Revision history
26
+
27
+ 5.0.0 Upload for test.
28
+ 5.0.1 Upload for test.
29
+ 5.0.2 Upload for release.
30
+ 5.0.3 Add class PSPLINE::Rfft, class PSPLINE::Cfft.
31
+ 5.0.4 Add method line_integral.
32
+ 5.0.5 Add Multiple Bspline.
33
+ 5.1.0 4 double precision arithmetic.
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vsp2old/pspline .
44
+
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rake/extensiontask"
3
-
4
- Rake::ExtensionTask.new "pspline" do |ext|
5
- ext.lib_dir = "lib/pspline"
6
- end
1
+ require "bundler/gem_tasks"
2
+ require "rake/extensiontask"
3
+
4
+ Rake::ExtensionTask.new "pspline" do |ext|
5
+ ext.lib_dir = "lib/pspline"
6
+ end
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "pspline"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pspline"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,351 +1,394 @@
1
- #include <cstring>
2
- #include <cstdlib>
3
- #include <cstddef>
4
- #include <cstdio>
5
- #include <cassert>
6
- #include "bspline_Config.h"
7
- #include "basis/util.h"
8
- #include "basis/basis.h"
9
-
10
- static const double eps = 1.0e-10;
11
-
12
- /*******************************************************************************
13
- de Boor-Cox の漸化式によるB−スプライン関数値の計算
14
- k: Bスプラインの階数(j+1)
15
- q: 節点の配列
16
- cox: Bスプラインの基底数
17
- t: データ点の座標(q[j] <= t <= q[cox])
18
- d: d > 0 ? スプライン階数 : 微分階数
19
- *******************************************************************************/
20
- template <typename T>
21
- int deboor(int k, T *q, int c, T t, T *b, int& kset, int d)
22
- {
23
- int h = c-1, i = k-1, j, K = d > 0 ? d : k, L = k + d;
24
- T a, q1, q2, dt;
25
-
26
- while (i < h) { if(q[i] <= t && t < q[i+1]) break; i++; }
27
- kset = i; if (q[h] <= t && t < q[c] + eps) kset = h;
28
- b[0] = 1;
29
- for (j = 1; j < K; ++j) {
30
- if (L) --L;
31
- b[j] = 0;
32
- for (i = 0; i < j; ++i) {
33
- h = j - i;
34
- if (L) {
35
- q1 = q[kset + h] - t;
36
- q2 = t - q[kset - i];
37
- dt = q1 + q2;
38
- a = 0; if (dt > 0.0) a = b[h-1] / dt;
39
- b[ h ] += a * q2; // 決定項
40
- b[h-1] = a * q1; // 準備項
41
- } else {
42
- dt = q[kset + h] - q[kset - i];
43
- a = 0; if (dt > 0.0) a = b[h-1] * (T)j / dt;
44
- b[ h ] += a; // 決定項
45
- b[h-1] = -a; // 準備項
46
- }
47
- }
48
- }
49
- return kset - j + 1;
50
- }
51
- /*******************************************************************************
52
- de Boor-Cox の漸化式によるB−スプライン関数値の計算
53
- K: Bスプラインの階数(j+1)
54
- q: 節点の配列
55
- cox: Bスプラインの基底数
56
- t: データ点の座標(q[j] <= t <= q[cox])
57
- J: 漸化式の適用回数
58
- L: (0 >= L >= -j) ? 微分階数
59
- *******************************************************************************/
60
- template<typename T>
61
- void deboor_cox(int K, T *q, int cox, T t, T *b, int &kset, int J, int L)
62
- {
63
- int M = J + L;
64
- int H = M + K + L;
65
- if (J == 0 && M >= 0) {
66
- int j = K - 1, c = cox - 1;
67
- for(int i=j;i<c;++i)if(q[i]<=t && t<q[i+1]){kset=i;break;}
68
- if(q[c]<=t && t<=q[cox]+eps){kset=c;}
69
- b[0] = 1;
70
- } else if (M >= 0 || H < 0) {
71
- int j = J - (M < 0 ? M + 1 : 0);
72
- T *b0 = &b[j+1];
73
- deboor_cox(K, q, cox, t, b0, kset, J-1, L+1);
74
- b[j] = 0.0;
75
- for(int i = 0; i < j; ++i) {
76
- int h = j - i;
77
- T a, q1, q2, dt;
78
- if (L >= 0) {
79
- q1 = q[kset + h] - t;
80
- q2 = t - q[kset - i];
81
- dt = q1 + q2;
82
- a = 0; if (dt > 0.0) a = b0[h-1] / dt;
83
- b[ h ] += a * q2;
84
- b[h-1] = a * q1;
85
- } else {
86
- dt = q[kset + h] - q[kset - i];
87
- a = 0; if (dt > 0.0) a = b0[h-1] * (T)j / dt;
88
- b[ h ] += a;
89
- b[h-1] = -a;
90
- }
91
- }
92
- }
93
- }
94
- /*******************************************************************************
95
- シェーンバーグ・ホイットニー条件を満たす節点の設定
96
- q0, ..., qj, qk, ..., qi, ..., qc-1, qc, ..., qc+j
97
- x0, ..., x0, | , ..., | , ..., | , xn-1, ..., xn-1
98
- (x0+xk)/2 (xi-k+xi)/2 (xn-k-1+xn-1)/2
99
- q : 節点の配列
100
- m : 節点配列の長さ (c + k)
101
- c : スプライン基底の個数
102
- n : データ点の個数
103
- k : スプライン階数 (k <= n)
104
- d : 境界条件数 (d < k)
105
- s : 周期
106
- x : データ点の配列(x == NULL: パラメトリックスプラインを示す)
107
- ms :
108
- *******************************************************************************/
109
- template<typename T>
110
- void setten(T *q, int m, int c, int n, int k, int d = 0, int s = 0, const T *x = NULL, const int *ms = NULL)
111
- {
112
- if (s == 0)
113
- for (int i = 0; i < m; i++)
114
- if (x) /* シェーンバーグ・ホイットニー条件 */
115
- q[i] = i < k ? x[0] : i < c ? (x[i-k] + x[i-d]) / 2.0 : x[n-1];
116
- else /* パラメトリック */
117
- q[i] = i < k ? 0 : i < c ? i - T(k+d) / 2.0 : n-1;
118
- else
119
- if (s > 0) /* 周期境界条件による節点の設定 */
120
- for (int i = 0; i < m; i++) {
121
- int j = i - k + 1;
122
- if (x)
123
- q[i] = j < 0 ? x[0] + x[j+s] - x[s] : j <= s ? x[j] : x[s] + x[j-s] - x[0];
124
- else
125
- q[i] = j;
126
- }
127
- else
128
- if (ms) {
129
- int k0 = k, n1, k1;
130
- while (s++) {
131
- n1 = *ms++; k1 = *ms++;
132
- setten(q, k0+n1-1, k0+n1-1, n1, k0, 0, 0, x);
133
- q += k0+n1-1; m -= k0+n1-1; c -= n1-1; n -= n1-1; k0 = k1; x += n1-1;
134
- } setten(q, m, c, n, k0, 0, 0, x);
135
- }
136
- }
137
- /*******************************************************************************
138
- 初期化(d == 0 ? 境界条件のないスプライン補間)
139
- n: データ点の個数
140
- x: データ点の座標 x[0,...,n-1]
141
- j: スプラインの次数(スプライン階数 K = j + 1)
142
- d: d >= 0 ? データ点に追加される境界条件数 : 周期境界条件(d = T - n - 1)
143
- T: 周期(境界条件数 = d + K)
144
- d > j : 多重節点の付加、m = {n1, k1, ... }
145
- *******************************************************************************/
146
- template <typename S>
147
- base_spline<S>::base_spline(int n, const S *x, int j, int d, int *m)
148
- {
149
- int s = 0;
150
- if (d > j) { s = j - d; d = 0; }
151
- // 周期境界条件フラグ
152
- shuki = d < 0 ? 1 : 0;
153
- // スプライン階数
154
- rank = j + 1;
155
- // スプライン次数
156
- jisu = j;
157
- // 周期(データ点の個数)
158
- imax = n + d + shuki;
159
- // Bスプライン関数基底の個数
160
- icox = imax + jisu * shuki;
161
- // 節点の個数
162
- maxq = icox + rank;
163
- // 節点の配列
164
- knots = new S[maxq];
165
- // 節点の配列を初期化
166
- setten(knots, maxq, icox, n, rank, d, s + imax * shuki, x, m);
167
- }
168
- /*******************************************************************************
169
- 初期化
170
- q: ノットベクトル
171
- c: スプライン関数基底の個数
172
- k: スプライン階数
173
- s: 周期指定
174
- *******************************************************************************/
175
- template <typename S>
176
- base_spline<S>::base_spline(const S *q, int k, int c, int s)
177
- {
178
- assert(s == 0 || s == 1);
179
- assert(k - (k-1)*s <= c);
180
- // 周期境界条件フラグ
181
- shuki = s;
182
- // スプライン階数
183
- rank = k;
184
- // スプライン次数
185
- jisu = k - 1;
186
- // 周期(データ点の個数)
187
- imax = c - jisu * shuki;
188
- // Bスプライン関数基底の個数
189
- icox = c;
190
- // 節点の個数
191
- maxq = icox + rank;
192
- // 節点の配列
193
- knots = new S[maxq];
194
- // 節点の配列を初期化
195
- for (int i = 0; i < maxq; i++) knots[i] = q[i];
196
- }
197
- // コピーコンストラクター
198
- template <typename S>
199
- base_spline<S>::base_spline(const base_spline& b)
200
- : imax(b.imax), icox(b.icox), rank(b.rank), jisu(b.jisu), shuki(b.shuki), maxq(b.maxq)
201
- {
202
- knots = new S[maxq];
203
- memcpy(knots, b.knots, maxq * sizeof(S));
204
- }
205
- // 代入オペレーター
206
- template <typename S>
207
- base_spline<S>& base_spline<S>::operator=(const base_spline& b)
208
- {
209
- if (this != &b) {
210
- delete[] knots;
211
- imax = b.imax; icox = b.icox; rank = b.rank;
212
- jisu = b.jisu; shuki = b.shuki; maxq = b.maxq;
213
- knots = new S[maxq];
214
- memcpy(knots, b.knots, maxq * sizeof(S));
215
- }
216
- return *this;
217
- }
218
- /*******************************************************************************
219
- Bスプライン関数値の計算 Bi,K(t): i = kset-jisu,...,kset
220
- t: データ点の座標値
221
- c,b: 結果を入れる配列(c:サイズ,b:配列へのポインタ)
222
- k: k > 0 ? Bi,k(x) : k == 0 ? Bi,rank(x) : k < 0 ? D-kBi,rank(x)
223
- *******************************************************************************/
224
- template <typename S>
225
- int base_spline<S>::basic(S t, int c, S* b, int k) const
226
- {
227
- int kset = 0, j = k > 0 ? k : rank;
228
- S bc[j];
229
- int ks = deboor<S>(rank, knots, icox, t, bc, kset, k);
230
- if (c <= j)
231
- for (int i=0;i<c;++i) b[i] = bc[ks < 0 ? i - ks : i];
232
- else
233
- for (int i=0;i<c;++i) b[i] = (ks <= i) && (i <= kset) ? bc[i - ks] : 0;
234
- return kset;
235
- }
236
- /*******************************************************************************
237
- 係数行列の構成
238
- *******************************************************************************/
239
- static void shuki_spline(int k, double *b, int n, double *bc)
240
- {
241
- int j = k - 1;
242
- int m = k / 2; /* m = Mjis = (Jisu+1)/2 = kai/2 */
243
- int m_1 = j - m;
244
- int i, o = n - m + 1;
245
- for (i = 0; i < m; i++) bc[i] = b[i+m_1] + b[i+m_1+n];
246
- for (i = m; i < o; i++) bc[i] = b[i+m_1];
247
- for (i = o; i < n; i++) bc[i] = b[i+m_1] + b[i+m_1-n];
248
- }
249
-
250
- template <typename S>
251
- void base_spline<S>::gyoretu(S **bd, int n, const S *x, const int *d) const
252
- {
253
- if (shuki == 0)
254
- for (int j = 0; j < n; j++)
255
- basic((x ? x[j] : j), imax, bd[j], (d ? -d[j] : 0));
256
- else {
257
- S *b = new S[icox]; /* 周期スプライン係数行列の計算 */
258
- for (int j = 0; j < n; j++) {
259
- basic((x ? x[j] : j), icox, b, (d ? -d[j] : 0));
260
- shuki_spline(rank, b, imax, bd[j]);
261
- }
262
- delete[] b;
263
- }
264
- }
265
-
266
- template <typename S>
267
- parray<S> base_spline<S>::basis(S t, int jbn) const
268
- {
269
- S data[rank];
270
- int kset, offset = deboor<S>(rank, knots, icox, t, data, kset, -jbn);
271
- parray<S> result(offset, rank, 1);
272
- S *p = *result; for (int i = 0; i < rank; ++i) p[i] = data[i];
273
- return result;
274
- }
275
-
276
- template <typename S>
277
- parray<S> base_spline<S>::bases(S t, int jbn) const
278
- {
279
- int N = 1; for (int i = 2; i <= rank; i++) N += i;
280
- S *bv = new S[N];
281
- int kset; deboor_cox<S>(rank, knots, icox, t, bv, kset, jisu);
282
- int offset = kset - jisu, k = jbn + 1;
283
- parray<S> data(offset, rank, k);
284
- S *mp = *data;
285
- for (int j = 0; j <= jbn; ++j) {
286
- if (j > 0) deboor_cox<S>(rank, knots, icox, t, bv, kset, rank-j, -rank);
287
- for(int i = 0; i < rank; ++i) *mp++ = bv[i];
288
- }
289
- delete[] bv;
290
- return data;
291
- }
292
- /*******************************************************************************
293
- 微分係数の計算
294
- *******************************************************************************/
295
- template <typename S> void base_spline<S>::bibun_keisu(int Jbn, int n, S** ad) const
296
- {
297
- S zk;
298
- S *a0, *a1, *q = knots;
299
-
300
- for (int j = 0; j < Jbn; j++) {
301
- a0 = ad[j];
302
- a1 = ad[j+1];
303
- a1[0] = 0.0;
304
- for (int i = 1; i < n; i++) {
305
- if ((zk = q[i+Jbn-j] - q[i]) != 0.0)
306
- a1[i] = (Jbn-j)*(a0[i] - a0[i-1])/zk;
307
- else
308
- a1[i] = 0.0;
309
- }
310
- }
311
- }
312
- /*******************************************************************************
313
- 積分係数の計算
314
- *******************************************************************************/
315
- static double qvalue(int m, double *q, int i, int j, int c)
316
- {
317
- int k = i - c + j;
318
- return ((k < m) ? q[k] : qvalue(m, q, k, j, c)) + q[c] - q[j];
319
- }
320
-
321
- #define QVALUE(i) ((i)<m ? q[i] : shuki ? qvalue(m,q,i,jisu,icox) : q[m-1])
322
-
323
- template <typename S> void base_spline<S>::sekibun_keisu(int Jsk, int n, S** ai) const
324
- {
325
- int ki = rank, m = Maxq();
326
- S *a0, *a1, *q = knots;
327
-
328
- for (int js = 1; js <= Jsk; js++) {
329
- a0 = ai[(js-1)%2];
330
- a1 = ai[js%2];
331
- a1[0] = a0[0] * (QVALUE(ki)-q[0])/ki;
332
- for (int j = 1; j < n; j++)
333
- a1[j] = a1[j-1] + a0[j]*(QVALUE(j+ki)-q[j])/ki;
334
- ki++;
335
- }
336
- }
337
-
338
- template <typename S> int base_spline<S>::Kset(S x) const
339
- {
340
- int L = 0; S *q = knots;
341
- for(int i = jisu; i < icox; ++i)
342
- if (q[i] <= x && x < q[i+1]){ L = i; break;}
343
- if(q[icox-1] <= x && x <= q[icox]+eps){ L = icox-1;}
344
- return L;
345
- }
346
-
347
-
348
-
349
-
350
- template class base_spline<double>;
351
-
1
+ #include "bspline.h"
2
+
3
+ /*******************************************************************************
4
+ de Boor-Cox の漸化式によるB−スプライン関数値の計算
5
+ k: Bスプラインの階数(j+1)
6
+ q: 節点の配列
7
+ c: Bスプラインの基底数
8
+ t: データ点の座標(q[j] <= t <= q[cox])
9
+ d: d > 0 ? スプライン階数 : 微分階数
10
+ *******************************************************************************/
11
+ template <typename P, typename T>
12
+ // P = varray<double>, T = poly<double>;
13
+ int deboor(int k, const P& q, int c, const T& t, P& b, int& kset, int d)
14
+ {
15
+ static const T eps(1.0e-10);
16
+
17
+ int h = c-1, i = k-1, j, K = d > 0 ? d : k, L = k + d;
18
+
19
+ while (i < h) { if(q[i] <= t && t < q[i+1]) break; i++; }
20
+ kset = i; if (q[h] <= t && t < q[c] + eps) kset = h;
21
+ b[0] = 1.0;
22
+ for (j = 1; j < K; ++j) {
23
+ if (L > 0) --L;
24
+ b[j] = 0.0;
25
+ for (i = 0; i < j; ++i) {
26
+ h = j - i;
27
+ T q1 = q[kset + h] - t;
28
+ T q2 = t - q[kset - i];
29
+ T dt = q1 + q2;
30
+ T a = 0.0; if (dt > 0.0) a = b[h-1] / dt;
31
+ if (L > 0) {
32
+ b[ h ] += a * q2; // 決定項
33
+ b[h-1] = a * q1; // 準備項
34
+ } else {
35
+ a *= j;
36
+ b[ h ] += a; // 決定項
37
+ b[h-1] = -a; // 準備項
38
+ }
39
+ }
40
+ }
41
+ return kset - j + 1;
42
+ }
43
+ /*******************************************************************************
44
+ de Boor-Cox の漸化式によるB−スプライン関数値の計算
45
+ K: Bスプラインの階数(j+1
46
+ q: 節点の配列
47
+ cox: Bスプラインの基底数
48
+ t: データ点の座標(q[j] <= t <= q[cox])
49
+ J: 漸化式の適用回数
50
+ L: (0 >= L >= -j) ? 微分階数
51
+ *******************************************************************************/
52
+ template<typename P, typename T>
53
+ // P = varray<double>, T = poly<double>;
54
+ void deboor_cox(int K, const P& q, int cox, const T& t, P& b, int &kset, int J, int L)
55
+ {
56
+ static const T eps(1.0e-10);
57
+
58
+ int I = J + L;
59
+ int H = I + K + L;
60
+ if (J == 0 && I >= 0) {
61
+ int j = K - 1, c = cox - 1;
62
+ for(int i=j;i<c;++i)if(q[i]<=t && t<q[i+1]){kset=i;break;}
63
+ if(q[c]<=t && t<=q[cox]+eps){kset=c;}
64
+ b[0] = 1.0;
65
+ } else if (I >= 0 || H < 0) {
66
+ int j = J - (I < 0 ? I + 1 : 0);
67
+ const P& b0 = b[j+1];
68
+ deboor_cox(K, q, cox, t, const_cast<P&>(b0), kset, J-1, L+1);
69
+ b[j] = 0.0;
70
+ for(int i = 0; i < j; ++i) {
71
+ int h = j - i;
72
+ T q1 = q[kset + h] - t;
73
+ T q2 = t - q[kset - i];
74
+ T dt = q1 + q2;
75
+ T a = 0.0; if (dt > 0.0) a = b0[h-1] / dt;
76
+ if (L >= 0) {
77
+ b[ h ] += a * q2;
78
+ b[h-1] = a * q1;
79
+ } else {
80
+ a *= j;
81
+ b[ h ] += a;
82
+ b[h-1] = -a;
83
+ }
84
+ }
85
+ }
86
+ }
87
+ /*******************************************************************************
88
+ シェーンバーグ・ホイットニー条件を満たす節点の設定
89
+ q0, ..., qj, qk, ..., qi, ..., qc-1, qc, ..., qc+j
90
+ x0, ..., x0, | , ..., | , ..., | , xn-1, ..., xn-1
91
+ (x0+xk)/2 (xi-k+xi)/2 (xn-k-1+xn-1)/2
92
+ q : 節点の配列
93
+ m : 節点配列の長さ (c + k)
94
+ c : スプライン基底の個数
95
+ n : データ点の個数
96
+ k : スプライン階数 (k <= n)
97
+ d : 境界条件数 (d < k)
98
+ s : 周期
99
+ x : データ点の配列(x == NULL: パラメトリックスプラインを示す)
100
+ ms :
101
+ *******************************************************************************/
102
+ template<typename T>
103
+ void setten(varray<T>& q, int m, int c, int n, int k, int d, int s, const varray<T>& x, const int *ms = NULL)
104
+ {
105
+ if (s == 0)
106
+ for (int i = 0; i < m; i++)
107
+ if ((T*)x) /* シェーンバーグ・ホイットニー条件 */
108
+ q[i] = i < k ? x[0] : i < c ? (x[i-k] + x[i-d]) / 2.0 : x[n-1];
109
+ else /* パラメトリック */
110
+ q[i] = i < k ? 0.0 : i < c ? real<T>(T(i)) - real<T>(T(k + d)) / 2.0 : real<T>(T(n - 1));
111
+ else
112
+ if (s > 0) /* 周期境界条件による節点の設定 */
113
+ for (int i = 0; i < m; i++) {
114
+ int j = i - k + 1;
115
+ if ((T*)x)
116
+ q[i] = j < 0 ? x[0] + x[j+s] - x[s] : j <= s ? x[j] : x[s] + x[j-s] - x[0];
117
+ else
118
+ q[i] = j;
119
+ }
120
+ else
121
+ if (ms) {
122
+ int k0 = k, k1, n0, n1;
123
+ varray<T> p(q), y(x);
124
+ while (s++) {
125
+ n1 = *ms++; k1 = *ms++; n0 = n1 + 1;
126
+ setten(p, n0, n0, n0, k0, 0, 0, y, NULL);
127
+ p = p[n0]; m -= n0; c -= n0; n -= n1; k0 = k1; y = y[n1];
128
+ }
129
+ setten(p, m, c, n, k0, c - n, 0, y, NULL);
130
+ }
131
+ }
132
+ /*******************************************************************************
133
+ 初期化(d == 0 ? 境界条件のないスプライン補間)
134
+ n: データ点の個数
135
+ x: データ点の座標 x[0,...,n-1]
136
+ j: スプラインの次数(スプライン階数 K = j + 1)
137
+ d: d >= 0 ? データ点に追加される境界条件数 : 周期境界条件(d = T - n - 1)
138
+ T: 周期(境界条件数 = d + K)
139
+ d > j : 多重節点の付加、m = {n1, k1, ... }
140
+ *******************************************************************************/
141
+ template <typename T>
142
+ base_spline<T>::base_spline(int n, const varray<T>& x, int j, int d, int *m)
143
+ {
144
+ int s = 0;
145
+ if (d > j) { s = j - d; d = 0; }
146
+ // 周期境界条件フラグ
147
+ shuki = d < 0 ? 1 : 0;
148
+ // スプライン階数
149
+ rank = j + 1;
150
+ // スプライン次数
151
+ jisu = j;
152
+ // 周期(データ点の個数)
153
+ imax = n + d + shuki;
154
+ // Bスプライン関数基底の個数
155
+ icox = imax + jisu * shuki;
156
+ // 節点の個数
157
+ maxq = icox + rank;
158
+ // 節点の配列
159
+ knots = new varray<T>(size_t(maxq), x.atom());
160
+ // 節点の配列を初期化
161
+ setten(*knots, maxq, icox, n, rank, d, s + imax * shuki, x, m);
162
+ }
163
+ /*******************************************************************************
164
+ 初期化
165
+ q: ノットベクトル
166
+ c: スプライン関数基底の個数
167
+ k: スプライン階数
168
+ s: 周期指定
169
+ *******************************************************************************/
170
+ template <typename T>
171
+ base_spline<T>::base_spline(const varray<T>& q, int k, int c, int s)
172
+ {
173
+ assert(s == 0 || s == 1);
174
+ assert(k - (k-1)*s <= c);
175
+ // 周期境界条件フラグ
176
+ shuki = s;
177
+ // スプライン階数
178
+ rank = k;
179
+ // スプライン次数
180
+ jisu = k - 1;
181
+ // 周期(データ点の個数)
182
+ imax = c - jisu * shuki;
183
+ // Bスプライン関数基底の個数
184
+ icox = c;
185
+ // 節点の個数
186
+ maxq = icox + rank;
187
+ // 節点の配列
188
+ knots = new varray<T>(size_t(maxq), q.atom());
189
+ // 節点の配列を初期化
190
+ for (int i = 0; i < maxq; i++) (*knots)[i] = q[i];
191
+ }
192
+ // コピーコンストラクター
193
+ template <typename T>
194
+ base_spline<T>::base_spline(const base_spline& b)
195
+ : imax(b.imax), icox(b.icox), rank(b.rank), jisu(b.jisu), shuki(b.shuki), maxq(b.maxq), knots(new varray<T>(*b.knots)) {}
196
+ // 代入オペレーター
197
+ template <typename T>
198
+ base_spline<T>& base_spline<T>::operator=(const base_spline& b)
199
+ {
200
+ if (this != &b) {
201
+ delete knots;
202
+ imax = b.imax; icox = b.icox; rank = b.rank; jisu = b.jisu; shuki = b.shuki; maxq = b.maxq;
203
+ knots = new varray<T>(*b.knots);
204
+ }
205
+ return *this;
206
+ }
207
+ /*******************************************************************************
208
+ Bスプライン関数値の計算 Bi,K(t): i = kset-jisu,...,kset
209
+ t: データ点の座標値
210
+ c,b: 結果を入れる配列(c:サイズ,b:配列へのポインタ)
211
+ j: j > 0 ? Bi,j(x) : j == 0 ? Bi,rank(x) : j < 0 ? D(-j)Bi,rank(x)
212
+ *******************************************************************************/
213
+ template <typename S>
214
+ int base_spline<S>::basic(const real<S>& t, int c, varray<S>& b, int j) const
215
+ {
216
+ int kset = 0, k = j > 0 ? j : rank;
217
+ assert(c >= k);
218
+ const varray<S>& bc = b[c - k];
219
+ int ks = deboor(rank, *knots, icox, t, const_cast<varray<S>&>(bc), kset, j);
220
+ if (c != k) {
221
+ int kmax = ((c - 1) == kset ? ks : c);
222
+ for (int i = 0; i < kmax; ++i)
223
+ b[i] = (ks <= i) && (i <= kset) ? bc[i - ks] : 0.0;
224
+ }
225
+ return ks;
226
+ }
227
+ /*******************************************************************************
228
+ 係数行列の構成
229
+ *******************************************************************************/
230
+ template <typename T>
231
+ static void shuki_spline(int k, const varray<T>& b, int n, varray<T>& bc)
232
+ {
233
+ int j = k - 1;
234
+ int m = k / 2; /* m = Mjis = (Jisu+1)/2 = kai/2 */
235
+ int m_1 = j - m;
236
+ int i, o = n - m + 1;
237
+ for (i = 0; i < m; i++) bc[i] = b[i+m_1] + b[i+m_1+n];
238
+ for (i = m; i < o; i++) bc[i] = b[i+m_1];
239
+ for (i = o; i < n; i++) bc[i] = b[i+m_1] + b[i+m_1-n];
240
+ }
241
+
242
+ template <typename T>
243
+ void base_spline<T>::gyoretu(marray<T>& bd, int n, const varray<T>& x, const int *d) const
244
+ {
245
+ if (shuki == 0)
246
+ for (int j = 0; j < n; j++) {
247
+ varray<T> bc = bd.row(j);
248
+ basic(( (T*)x ? x[j] : real<T>(T(j)) ), imax, bc, (d ? -d[j] : 0));
249
+ }
250
+ else {
251
+ varray<T> b(size_t(icox), x.atom()); /* 周期スプライン係数行列の計算 */
252
+ for (int j = 0; j < n; j++) {
253
+ basic(( (T*)x ? x[j] : real<T>(T(j)) ), icox, b, (d ? -d[j] : 0));
254
+ varray<T> bc = bd.row(j);
255
+ shuki_spline(rank, b, imax, bc);
256
+ }
257
+ }
258
+ }
259
+
260
+ template <typename T>
261
+ marray<T> base_spline<T>::gyoretu(const Int& a, const varray<T>& x, const int *d) const
262
+ {
263
+ size_t n = a[0];
264
+ marray<T> bd(n, n, a.atom());
265
+ gyoretu(bd, n, x, d);
266
+ return bd;
267
+ }
268
+
269
+ template <typename T>
270
+ parray<T> base_spline<T>::basis(const real<T>& t, int jbn) const
271
+ {
272
+ int h = rank - (jbn < 0 ? jbn : 0);
273
+ int j = jbn < 0 ? h : -jbn;
274
+ varray<T> data(size_t(h), t.atom());
275
+ int kset, offset = deboor(rank, *knots, icox, t, data, kset, j);
276
+ parray<T> result(offset, h, 1, t.atom());
277
+ varray<T> m = result[0];
278
+ for (int i = 0; i < h; ++i) m[i] = data[i];
279
+ return result;
280
+ }
281
+
282
+ template <typename T>
283
+ parray<T> base_spline<T>::bases(const real<T>& t, int jbn) const
284
+ {
285
+ int N = 1; for (int i = 2; i <= rank; i++) N += i;
286
+ varray<T> data(size_t(N), t.atom());
287
+ int kset; deboor_cox(rank, *knots, icox, t, data, kset, jisu);
288
+ int offset = kset - jisu, k = jbn + 1;
289
+ parray<T> result(offset, rank, k, t.atom());
290
+ for (int j = 0; j <= jbn; ++j) {
291
+ varray<T> m = result[j];
292
+ if (j > 0) deboor_cox(rank, *knots, icox, t, data, kset, rank-j, -rank);
293
+ for(int i = 0; i < rank; ++i) m[i] = data[i];
294
+ }
295
+ return result;
296
+ }
297
+
298
+ template <typename S>
299
+ parray<S> base_spline<S>::sekibun(const real<S>& x, int Jsk) const
300
+ {
301
+ assert(Jsk < 0);
302
+ const varray<S>& q = *knots;
303
+ int K = rank, J = K - 1, R = K - Jsk;
304
+ varray<S> B(size_t(R), x.atom());
305
+ int kset, ks = deboor(K, q, icox, x, B, kset, R);
306
+ int offset = kset - J;
307
+ parray<S> pc(offset, K, 1, x.atom());
308
+ varray<S> ac(size_t(kset+1), x.atom()), sum = pc[0];
309
+ for (int i = 0; i < K; ++i) {
310
+ int Jsc = -Jsk;
311
+ for (int j = 0; j <= kset; ++j)
312
+ ac[j] = 0.0;
313
+ ac[i+offset] = 1;
314
+ while(Jsc) {
315
+ for (int j = 0; j <= kset; ++j)
316
+ ac[j] = (j > 0 ? ac[j-1] : 0.0) + ac[j] * (q[j+(R-Jsc)] - q[j]) / S(R-Jsc);
317
+ Jsc--;
318
+ } sum[i] = 0.0;
319
+ for (int j = (ks >= 0 ? ks : 0); j <= kset; ++j)
320
+ sum[i] += ac[j] * B[j - ks];
321
+ }
322
+ return pc;
323
+ }
324
+ /*******************************************************************************
325
+ 微分係数の計算
326
+ *******************************************************************************/
327
+ template <typename S>
328
+ void base_spline<S>::bibun_keisu(int Jbn, marray<S>& ad) const
329
+ {
330
+ real<S> zk; int n = ad.cols();
331
+ const varray<S>& q = *knots;
332
+
333
+ for (int j = 0; j < Jbn; j++) {
334
+ varray<S> a0 = ad.row(j);
335
+ varray<S> a1 = ad.row(j+1);
336
+ a1[0] = 0.0;
337
+ for (int i = 1; i < n; i++) {
338
+ if ((zk = q[i+Jbn-j] - q[i]) != 0.0)
339
+ a1[i] = (Jbn-j)*(a0[i] - a0[i-1])/zk;
340
+ else
341
+ a1[i] = 0.0;
342
+ }
343
+ }
344
+ }
345
+ /*******************************************************************************
346
+ 積分係数の計算
347
+ *******************************************************************************/
348
+ template <typename S>
349
+ static real<S> qvalue(int m, const varray<S>& q, int i, int j, int c)
350
+ {
351
+ int k = i - c + j;
352
+ return ((k < m) ? q[k] : qvalue(m, q, k, j, c)) + q[c] - q[j];
353
+ }
354
+
355
+ #define QVALUE(i) ((i)<m ? q[i] : shuki ? qvalue(m,q,i,jisu,icox) : q[m-1])
356
+
357
+ template <typename S>
358
+ void base_spline<S>::sekibun_keisu(int Jsk, marray<S>& ai) const
359
+ {
360
+ int ki = rank, m = Maxq(), n = ai.cols();
361
+ const varray<S>& q = *knots;
362
+
363
+ assert(n <= icox);
364
+ for (int js = 1; js <= Jsk; js++) {
365
+ real<S> a0 = ai.row((js-1)%2);
366
+ real<S> a1 = ai.row(js%2);
367
+ for (int j = 0; j < n; j++)
368
+ a1[j] = (j > 0 ? a1[j-1] : 0.0) + a0[j]*(QVALUE(j+ki)-q[j])/ki;
369
+ ki++;
370
+ }
371
+ }
372
+
373
+ template <typename S>
374
+ int base_spline<S>::Kset(const real<S>& x) const
375
+ {
376
+ static const S eps = S(1.0e-10);
377
+
378
+ int L = 0;
379
+ const varray<S>& q = *knots;
380
+ for(int i = jisu; i < icox; ++i)
381
+ if (q[i] <= x && x < q[i+1]){ L = i; break;}
382
+ if(q[icox-1] <= x && x <= q[icox]+eps){ L = icox-1;}
383
+ return L;
384
+ }
385
+
386
+ template <typename S>
387
+ void base_spline<S>::print() const
388
+ {
389
+ printf("<K = %d, C = %d, I = %d, J = %d, S = %d\n", rank, icox, imax, jisu, shuki);
390
+ knots->print("%g>\n");
391
+ }
392
+
393
+ template class base_spline<double>;
394
+