OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
TpsMath.h
Go to the documentation of this file.
1#ifndef CLASSIC_TpsMath_HH
2#define CLASSIC_TpsMath_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: TpsMath.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Overloaded template functions:
13// Elementary functions acting on Tps<T> objects.
14//
15// ------------------------------------------------------------------------
16// Class category: Algebra
17// ------------------------------------------------------------------------
18//
19// $Date: 2000/03/27 09:32:32 $
20// $Author: fci $
21//
22// ------------------------------------------------------------------------
23
24#include "Algebra/Tps.h"
26#include <cmath>
27
28
29// Class Tps; global functions acting on Tps objects.
30// ------------------------------------------------------------------------
31
33template <class T> Tps<T> pow(const Tps<T> &x, int y);
34
36template <class T> Tps<T> sqrt(const Tps<T> &x);
37
39template <class T> Tps<T> sin(const Tps<T> &x);
40
42template <class T> Tps<T> cos(const Tps<T> &x);
43
45template <class T> Tps<T> tan(const Tps<T> &x);
46
48template <class T> Tps<T> cot(const Tps<T> &x);
49
51template <class T> Tps<T> sec(const Tps<T> &x);
52
54template <class T> Tps<T> csc(const Tps<T> &x);
55
57template <class T> Tps<T> exp(const Tps<T> &x);
58
60template <class T> Tps<T> log(const Tps<T> &x);
61
63template <class T> Tps<T> sinh(const Tps<T> &x);
64
66template <class T> Tps<T> cosh(const Tps<T> &x);
67
69template <class T> Tps<T> tanh(const Tps<T> &x);
70
71
72// Implementation
73// ------------------------------------------------------------------------
74
75template <class T>
76Tps<T> pow(const Tps<T> &x, int y) {
77 Tps<T> z(T(1));
78
79 if(y > 0) {
80 while(y-- > 0) z = z * x;
81 } else if(y < 0) {
82 Tps<T> t = x.inverse();
83 while(y++ < 0) z = z * t;
84 }
85
86 return z;
87}
88
89
90template <class T>
91Tps<T> sqrt(const Tps<T> &x) {
92 T aZero = x[0];
93 if(aZero <= 0.0) throw DomainError("sqrt(const Tps &)");
94
95 // Build Taylor series.
96 int cut = x.getTruncOrder();
97 T *series = new T[cut+1];
98 series[0] = sqrt(aZero);
99
100 for(int i = 1; i <= cut; i++) {
101 series[i] = (series[i-1] * double(3 - 2 * i)) / (aZero * double(2 * i));
102 }
103
104 Tps<T> z = x.Taylor(series, cut);
105 delete [] series;
106 return z;
107}
108
109
110template <class T>
111Tps<T> sin(const Tps<T> &x) {
112 T aZero = x[0];
113 int cut = x.getTruncOrder();
114 T *series = new T[cut+1];
115 series[0] = sin(aZero);
116 series[1] = cos(aZero);
117
118 for(int i = 2; i <= cut; i++) {
119 series[i] = - series[i-2] / double(i * (i - 1));
120 }
121
122 Tps<T> z = x.Taylor(series, cut);
123 delete [] series;
124 return z;
125}
126
127
128template <class T>
129Tps<T> cos(const Tps<T> &x) {
130 T aZero = x[0];
131 int cut = x.getTruncOrder();
132 T *series = new T[cut+1];
133 series[0] = cos(aZero);
134 series[1] = - sin(aZero);
135
136 for(int i = 2; i <= cut; i++) {
137 series[i] = - series[i-2] / double(i * (i - 1));
138 }
139
140 Tps<T> z = x.Taylor(series, cut);
141 delete [] series;
142 return z;
143}
144
145
146template <class T>
147Tps<T> tan(const Tps<T> &x) {
148 return sin(x) / cos(x);
149}
150
151
152template <class T>
153Tps<T> sec(const Tps<T> &x) {
154 return cos(x).inverse();
155}
156
157
158template <class T>
159Tps<T> csc(const Tps<T> &x) {
160 return sin(x).inverse();
161}
162
163
164template <class T>
165Tps<T> exp(const Tps<T> &x) {
166 T aZero = x[0];
167 int cut = x.getTruncOrder();
168 T *series = new T[cut+1];
169 series[0] = exp(aZero);
170
171 for(int i = 1; i <= cut; i++) {
172 series[i] = series[i-1] / double(i);
173 }
174
175 Tps<T> z = x.Taylor(series, cut);
176 delete [] series;
177 return z;
178}
179
180
181template <class T>
182Tps<T> log(const Tps<T> &x) {
183 T aZero = x[0];
184 if(aZero <= 0) throw DomainError("log(const Tps &)");
185
186 int cut = x.getTruncOrder();
187 double power;
188 T *series = new T[cut+1];
189 series[0] = log(aZero);
190 series[1] = power = T(1) / aZero;
191
192 for(int i = 2; i <= cut; i++) {
193 power = - power / aZero;
194 series[i] = power / double(i);
195 }
196
197 Tps<T> z = x.Taylor(series, cut);
198 delete [] series;
199 return z;
200}
201
202
203template <class T>
204Tps<T> sinh(const Tps<T> &x) {
205 T aZero = x[0];
206 int cut = x.getTruncOrder();
207 T *series = new T[cut+1];
208 series[0] = sinh(aZero);
209 series[1] = cosh(aZero);
210
211 for(int i = 2; i <= cut; i++) {
212 series[i] = series[i-2] / double(i * (i - 1));
213 }
214
215 Tps<T> z = x.Taylor(series, cut);
216 delete [] series;
217 return z;
218}
219
220
221template <class T>
222Tps<T> cosh(const Tps<T> &x) {
223 T aZero = x[0];
224 int cut = x.getTruncOrder();
225 T *series = new T[cut+1];;
226 series[0] = cosh(aZero);
227 series[1] = sinh(aZero);
228
229 for(int i = 2; i <= cut; i++) {
230 series[i] = series[i-2] / double(i * (i - 1));
231 }
232
233 Tps<T> z = x.Taylor(series, cut);
234 delete [] series;
235 return z;
236}
237
238
239template <class T>
240Tps<T> tanh(const Tps<T> &x) {
241 return sinh(x) / cosh(x);
242}
243
244#endif // CLASSIC_TpsMath_HH
Tps< T > log(const Tps< T > &x)
Natural logarithm.
Definition: TpsMath.h:182
Tps< T > sec(const Tps< T > &x)
Secant.
Definition: TpsMath.h:153
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
Tps< T > cot(const Tps< T > &x)
Cotangent.
Tps< T > cosh(const Tps< T > &x)
Hyperbolic cosine.
Definition: TpsMath.h:222
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > csc(const Tps< T > &x)
Cosecant.
Definition: TpsMath.h:159
Tps< T > tan(const Tps< T > &x)
Tangent.
Definition: TpsMath.h:147
Tps< T > exp(const Tps< T > &x)
Exponential.
Definition: TpsMath.h:165
Tps< T > sinh(const Tps< T > &x)
Hyperbolic sine.
Definition: TpsMath.h:204
Tps< T > tanh(const Tps< T > &x)
Hyperbolic tangent.
Definition: TpsMath.h:240
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
Truncated power series.
Definition: Tps.h:46
Tps< T > inverse(int order=truncOrder) const
Reciprocal value.
Definition: Tps.hpp:897
Tps< T > Taylor(const T series[], int n) const
Taylor series.
Definition: Tps.hpp:1014
int getTruncOrder() const
Get truncation order.
Definition: Tps.hpp:1037
Domain error exception.
Definition: DomainError.h:32