ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
orthpoly.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <fvar.hpp>
12 
17 dmatrix orthpoly(int n,int deg)
18 {
19  dmatrix ocoff(0,deg,1,n);
20  double sum;
21  ocoff(0)=sqrt(double(n));
22  for (int is=1; is<=deg; is++)
23  {
24  for (int j=1; j<=n; j++)
25  {
26  ocoff(is,j)=pow(double(j),is);
27  }
28  }
29  for (int is=0; is<=deg; is++) /* L1000 */
30  {
31  for (int ik=0; ik<=is-1; ik++) /* L2000 */
32  {
33  sum=ocoff(is)*ocoff(ik);
34  ocoff(is)-=sum*ocoff(ik);
35  }
36  sum=norm2(ocoff(is));
37  ocoff(is)=ocoff(is)/sqrt(sum);
38  }
39  return trans(ocoff);
40 }
41 
46 dmatrix orthpoly(int n,int deg,int skip)
47 {
48  dmatrix ocoff(0,deg,1,n);
49  double sum;
50  ocoff(0)=sqrt(double(n));
51  for (int is=1; is<=deg; is++)
52  {
53  for (int j=1; j<=n; j++)
54  {
55  ocoff(is,j)=pow(double(j),is);
56  }
57  }
58  for (int is=0; is<=deg; is++) /* L1000 */
59  {
60  for (int ik=0; ik<=is-1; ik++) /* L2000 */
61  {
62  sum=ocoff(is)*ocoff(ik);
63  ocoff(is)-=sum*ocoff(ik);
64  }
65  sum=norm2(ocoff(is));
66  ocoff(is)=ocoff(is)/sqrt(sum);
67  }
68  return trans(ocoff.sub(skip,deg));
69 }
70 
75 dmatrix orthpoly_constant_begin(int n,int deg,int nconst)
76 {
77  dmatrix ocoff(0,deg,1,n);
78  double sum;
79  ocoff(0)=sqrt(double(n));
80  if (nconst>n-1)
81  {
82  cerr << "nconst too large in orthpoly_constant_begin"
83  << endl;
84  }
85  if (deg>n-nconst)
86  {
87  cerr << "deg too large in orthpoly_constant_begin"
88  << endl;
89  }
90  for (int is=1; is<=deg; is++)
91  {
92  if (nconst>1)
93  {
94  for (int j=1; j<=nconst; j++)
95  {
96  ocoff(is,j)=1.0;
97  }
98  for (int j=nconst+1; j<=n; j++)
99  {
100  ocoff(is,j)=pow(double(j-nconst+1),is);
101  }
102  }
103  else
104  {
105  for (int j=1; j<=n; j++)
106  {
107  ocoff(is,j)=pow(double(j),is);
108  }
109  }
110  }
111  for (int is=0; is<=deg; is++) /* L1000 */
112  {
113  for (int ik=0; ik<=is-1; ik++) /* L2000 */
114  {
115  sum=ocoff(is)*ocoff(ik);
116  ocoff(is)-=sum*ocoff(ik);
117  }
118  sum=norm2(ocoff(is));
119  ocoff(is)=ocoff(is)/sqrt(sum);
120  }
121 #ifdef DIAG
122  int ps=0;
123  if (ps)
124  {
125  dmatrix tmp(0,deg,0,deg);
126  for (int i=0;i<=deg;i++)
127  {
128  for (int j=0;j<=deg;j++)
129  {
130  tmp(i,j)=ocoff(i)*ocoff(j);
131  }
132  }
133  cout << tmp << endl;
134  }
135 #endif
136  return trans(ocoff);
137 }
138 
143 dmatrix orthpoly_constant_begin_end(int n,int deg,int nconst_begin,
144  int end_degree,int nconst_end)
145 {
146  dmatrix ocoff(0,deg,1,n);
147  double sum;
148  ocoff(0)=sqrt(double(n));
149  if (nconst_begin>n-1)
150  {
151  cerr << "nconst_begin too large in orthpoly_constant_begin"
152  << endl;
153  }
154  if (deg>n-nconst_begin)
155  {
156  cerr << "deg too large in orthpoly_constant_begin"
157  << endl;
158  }
159  for (int is=1; is<=deg; is++)
160  {
161  if (nconst_begin>1)
162  {
163  for (int j=1; j<=nconst_begin; j++)
164  {
165  ocoff(is,j)=1.0;
166  }
167  for (int j=nconst_begin+1; j<=n; j++)
168  {
169  int jj=j;
170  if (j>n-nconst_end+1 && is>=end_degree)
171  {
172  jj=n-nconst_end+1;
173  }
174  ocoff(is,j)=pow(double(jj-nconst_begin+1)/n,is);
175  }
176  }
177  else
178  {
179  for (int j=1; j<=n; j++)
180  {
181  int jj=j;
182  if (j>n-nconst_end+1 && is>=end_degree)
183  {
184  jj=n-nconst_end+1;
185  }
186  ocoff(is,j)=pow(double(jj)/n,is);
187  }
188  }
189  }
190  for (int is=0; is<=deg; is++) /* L1000 */
191  {
192  for (int ik=0; ik<=is-1; ik++) /* L2000 */
193  {
194  sum=ocoff(is)*ocoff(ik);
195  ocoff(is)-=sum*ocoff(ik);
196  }
197  sum=norm2(ocoff(is));
198  ocoff(is)=ocoff(is)/sqrt(sum);
199  }
200 #ifdef DIAG
201  int ps=0;
202  if (ps)
203  {
204  dmatrix tmp(0,deg,0,deg);
205  for (int i=0;i<=deg;i++)
206  {
207  for (int j=0;j<=deg;j++)
208  {
209  tmp(i,j)=ocoff(i)*ocoff(j);
210  }
211  }
212  cout << tmp << endl;
213  }
214 #endif
215  return trans(ocoff);
216 }
217 
223 {
224  dmatrix ocoff(1,n,1,n);
225  dmatrix ocoff1(1,n,1,n);
226  ocoff.initialize();
227  ocoff1.initialize();
228  for (int i=1; i<=n; i++)
229  {
230  for (int j=i; j<=n; j++)
231  {
232  ocoff(i,j)=1;
233  }
234  }
235  ocoff1=trans(ocoff);
236 
237  for (int i=1; i<=n; i++) /* L1000 */
238  {
239  for (int j=1; j<=i-1; j++) /* L2000 */
240  {
241  ocoff(i)-=(ocoff(i)*ocoff(j))*ocoff(j);
242  }
243  ocoff(i)/=norm(ocoff(i));
244  }
245  ocoff=trans(ocoff);
246 
247  cout << setw(10) << setprecision(4) << ocoff1 << endl << endl;
248 
249  dmatrix tmp1=(inv(ocoff1)*ocoff);
250  dvector a(1,n);
251  dvector b(1,n);
252 
253  for (int i=1; i<=n; i++) /* L1000 */
254  {
255  a(i)=tmp1(i,i);
256  }
257  b(1)=0.0;
258  for (int i=2; i<=n; i++) /* L1000 */
259  {
260  b(i)=tmp1(i-1,i);
261  }
262 
263  cout << a << endl << endl;
264  cout << b << endl << endl;
265 
266  cout << ocoff1*tmp1(1) << endl;
267  cout << ocoff1*tmp1(2) << endl;
268  cout << ocoff1 *tmp1(3) << endl;
269  cout << (ocoff1*tmp1(1)) * (ocoff1 *tmp1(3)) << endl;
270  cout << (ocoff1*tmp1(2)) * (ocoff1 *tmp1(3)) << endl;
271 
272  return ocoff1;
273 }
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
Vector of double precision numbers.
Definition: dvector.h:50
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
dmatrix sub(int, int)
Description not yet available.
Definition: dmat.cpp:47
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
dmatrix seldif_basis(int n)
Description not yet available.
Definition: orthpoly.cpp:222
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Definition: fvar.hpp:2819
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
dmatrix orthpoly(int n, int deg)
Description not yet available.
Definition: orthpoly.cpp:17
dmatrix orthpoly_constant_begin_end(int n, int deg, int nconst_begin, int enddeg, int nconst_end)
Description not yet available.
Definition: orthpoly.cpp:143
dmatrix orthpoly_constant_begin(int n, int deg, int nconst)
Description not yet available.
Definition: orthpoly.cpp:75
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17