KADATH
mult_cos_1d.cpp
1 /*
2  Copyright 2017 Philippe Grandclement
3 
4  This file is part of Kadath.
5 
6  Kadath is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Kadath is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Kadath. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "base_spectral.hpp"
21 #include "headcpp.hpp"
22 #include "array.hpp"
23 
24 namespace Kadath {
25 int mult_cos_1d_pasprevu (Array<double>&) {
26  cout << "mult_cos_1d not implemented." << endl ;
27  abort() ;
28 }
29 
30 int mult_cos_1d_cossin (Array<double>& tab) {
31  assert (tab.get_ndim()==1) ;
32  int nr = tab.get_size(0) ;
33  assert (nr%2==0) ;
34  Array<double> res (nr) ;
35 
36  res.set(0) = 0.5*tab(2) ;
37  res.set(1) = 0 ;
38  res.set(2) = tab(0) + 0.5*tab(4) ;
39  res.set(3) = 0.5*tab(5) ;
40  for (int k=4; k<nr-2; k++)
41  res.set(k) = 0.5*(tab(2+k) + tab(k-2)) ;
42 
43  res.set(nr-2) = 0.5*tab(nr-4) ;
44  res.set(nr-1) = 0. ;
45 
46  tab = res ;
47  return COSSIN ;
48 }
49 
50 int mult_cos_1d_cos (Array<double>& tab) {
51  assert (tab.get_ndim()==1) ;
52  int nr = tab.get_size(0) ;
53 
54  Array<double> res (nr) ;
55 
56  res.set(0) = 0.5*tab(1) ;
57  res.set(1) = tab(0) + 0.5*tab(2);
58  for (int k=2; k<nr-1; k++)
59  // Sines
60  res.set(k) = 0.5*(tab(k-1) + tab(k+1)) ;
61  //res.set(nr-1) = 0.5*tab(nr-2) ;
62  res.set(nr-1) = 0 ;
63 
64  tab = res ;
65  return COS;
66 }
67 
68 int mult_cos_1d_sin (Array<double>& tab) {
69  assert (tab.get_ndim()==1) ;
70  int nr = tab.get_size(0) ;
71 
72  Array<double> res (nr) ;
73 
74  res.set(0) = 0. ;
75  res.set(1) = 0.5*tab(2) ;
76  for (int k=2; k<nr-1; k++)
77  // Sines
78  res.set(k) = 0.5*(tab(k-1) + tab(k+1)) ;
79  //res.set(nr-1) = 0.5*tab(nr-2) ;
80  res.set(nr-1) = 0 ;
81 
82  tab = res ;
83  return SIN ;
84 }
85 
86 int mult_cos_1d_cos_even (Array<double>& tab) {
87  assert (tab.get_ndim()==1) ;
88  int nr = tab.get_size(0) ;
89 
90  Array<double> res (nr) ;
91 
92  res.set(0) = tab(0) + 0.5*tab(1);
93  for (int k=1; k<nr-1; k++)
94  // Sines
95  res.set(k) = 0.5*(tab(k) + tab(k+1)) ;
96  //res.set(nr-1) = 0.5*tab(nr-1) ;
97  res.set(nr-1) = 0. ;
98 
99  tab = res ;
100  return COS_ODD ;
101 }
102 
103 int mult_cos_1d_cos_odd (Array<double>& tab) {
104  assert (tab.get_ndim()==1) ;
105  int nr = tab.get_size(0) ;
106 
107  Array<double> res (nr) ;
108 
109  res.set(0) = 0.5*tab(0) ;
110  for (int k=1; k<nr; k++)
111  // Sines
112  res.set(k) = 0.5*(tab(k-1) + tab(k)) ;
113 
114  tab = res ;
115  return COS_EVEN ;
116 }
117 
118 int mult_cos_1d_sin_even (Array<double>& tab) {
119  assert (tab.get_ndim()==1) ;
120  int nr = tab.get_size(0) ;
121 
122  Array<double> res (nr) ;
123 
124  res.set(0) = 0.5*tab(1) ;
125  for (int k=1; k<nr-1; k++)
126  // Sines
127  res.set(k) = 0.5*(tab(k) + tab(k+1)) ;
128  res.set(nr-1) = 0. ;
129 
130  tab = res ;
131  return SIN_ODD ;
132 }
133 
134 int mult_cos_1d_sin_odd (Array<double>& tab) {
135  assert (tab.get_ndim()==1) ;
136  int nr = tab.get_size(0) ;
137 
138  Array<double> res (nr) ;
139 
140  res.set(0) = 0. ;
141  for (int k=1; k<nr; k++)
142  // Sines
143  res.set(k) = 0.5*(tab(k-1) + tab(k)) ;
144 
145  tab = res ;
146  return SIN_EVEN ;
147 }
148 
149 int mult_cos_1d (int base, Array<double>& tab) {
150  static int (*mult_cos_1d[NBR_MAX_BASE])(Array<double>&) ;
151  static bool premier_appel = true ;
152 
153  // Premier appel
154  if (premier_appel) {
155  premier_appel = false ;
156 
157  for (int i=0; i<NBR_MAX_BASE; i++)
158  mult_cos_1d[i] = mult_cos_1d_pasprevu ;
159 
160  mult_cos_1d[COSSIN] = mult_cos_1d_cossin ;
161  mult_cos_1d[COS_EVEN] = mult_cos_1d_cos_even ;
162  mult_cos_1d[COS_ODD] = mult_cos_1d_cos_odd ;
163  mult_cos_1d[SIN_EVEN] = mult_cos_1d_sin_even ;
164  mult_cos_1d[SIN_ODD] = mult_cos_1d_sin_odd ;
165  mult_cos_1d[COS] = mult_cos_1d_cos ;
166  mult_cos_1d[SIN] = mult_cos_1d_sin ;
167  }
168 
169  return mult_cos_1d[base](tab) ;
170 }}