-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnion_trig.cpp
More file actions
323 lines (255 loc) · 9.14 KB
/
nion_trig.cpp
File metadata and controls
323 lines (255 loc) · 9.14 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright 2023 Marcus Dante Liebenthal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef NION_TRIG_CPP
#define NION_TRIG_CPP
#include "nion_trig.hpp"
/*******************************************
* NION HYPERBOLIC TRIGONOMETRIC FUNCTIONS *
********************************************/
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> sinh(const nion<T,N> &z) {
// get polar form of nion
nion<T,N> i = z.imag();
// make unit vector
T i_abs = i.abs();
T i_norm = sqrt(i_abs);
// calculate scalars
T e_z = exp(z.elem_[0]) / 2.0l;
T e_mz = exp(-z.elem_[0]) / 2.0l;
// compute denorm_min
T denorm_min = T(0); // default value
if constexpr (std::is_arithmetic_v<T>) {
denorm_min = std::numeric_limits<T>::denorm_min(); // if T is arithmetic, use its denorm_min
}
// compute exponential of nion
if (i_abs <= denorm_min) {
nion<T,N> sin_nion = i * ((e_z + e_mz) * sin(i_norm));
sin_nion += cos(i_norm) * (e_z - e_mz);
return sin_nion;
} else {
nion<T,N> sin_nion = i * ((e_z + e_mz) * sin(i_norm) / i_norm);
sin_nion += cos(i_norm) * (e_z - e_mz);
return sin_nion;
}
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> cosh(const nion<T,N> &z) {
// get polar form of nion
nion<T,N> i = z.imag();
// make unit vector
T i_abs = i.abs();
T i_norm = sqrt(i_abs);
// calculate scalars
T e_z = exp(z.elem_[0]) / 2.0l;
T e_mz = exp(-z.elem_[0]) / 2.0l;
// compute denorm_min
T denorm_min = T(0); // default value
if constexpr (std::is_arithmetic_v<T>) {
denorm_min = std::numeric_limits<T>::denorm_min(); // if T is arithmetic, use its denorm_min
}
// compute exponential of nion
if (i_abs <= denorm_min) {
nion<T,N> cos_nion = i * ((e_z - e_mz) * sin(i_norm));
cos_nion += cos(i_norm) * (e_z + e_mz);
return cos_nion;
} else {
nion<T,N> cos_nion = i * ((e_z - e_mz) * sin(i_norm) / i_norm);
cos_nion += cos(i_norm) * (e_z + e_mz);
return cos_nion;
}
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> tanh(const nion<T,N> &z) {
return (exp(z*2.0l) - 1.0l) / (exp(z*2.0l) + 1.0l);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> coth(const nion<T,N> &z) {
return tanh(z).inv();
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> sech(const nion<T,N> &z) {
return cosh(z).inv();
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> csch(const nion<T,N> &z) {
return sinh(z).inv();
}
/********************************
* NION TRIGONOMETRIC FUNCTIONS *
*********************************/
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> sin(const nion<T,N> &z) {
// get the polar form of the nion
T r = real(z);
nion<T,N> i = imag(z);
// make unit vector
T i_abs = i.abs();
T i_norm = sqrt(i_abs);
// compute denorm_min
T denorm_min = T(0); // default value
if constexpr (std::is_arithmetic_v<T>) {
denorm_min = std::numeric_limits<T>::denorm_min(); // if T is arithmetic, use its denorm_min
}
// compute the sine of the nion
if (i_abs <= denorm_min)
return i * (sinh(i_norm) * cos(r)) + sin(r) * cosh(i_norm);
else
return i * (sinh(i_norm) * cos(r) / i_norm) + sin(r) * cosh(i_norm);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> cos(const nion<T,N> &z) {
// get the polar form of the nion
T r = real(z);
nion<T,N> i = imag(z);
// make unit vector
T i_abs = i.abs();
T i_norm = sqrt(i_abs);
// compute denorm_min
T denorm_min = T(0); // default value
if constexpr (std::is_arithmetic_v<T>) {
denorm_min = std::numeric_limits<T>::denorm_min(); // if T is arithmetic, use its denorm_min
}
// compute the cosine of the nion
if (i_abs <= denorm_min)
return -i * (sinh(i_norm) * sin(r)) + cos(r) * cosh(i_norm);
else
return -i * (sinh(i_norm) * sin(r) / i_norm) + cos(r) * cosh(i_norm);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> tan(const nion<T,N> &z) {
// get the polar form of the nion
T r = real(z);
nion<T,N> i = imag(z);
// make unit vector
T i_abs = i.abs();
T i_norm = sqrt(i_abs);
// compute denorm_min
T denorm_min = T(0); // default value
if constexpr (std::is_arithmetic_v<T>) {
denorm_min = std::numeric_limits<T>::denorm_min(); // if T is arithmetic, use its denorm_min
}
// compute the tangent of the nion
if (i_norm <= denorm_min)
return (tan(r) + i * tanh(i_norm)) / (1 - i * (tan(r) * tanh(i_norm)));
else
return (tan(r) + i * (tanh(i_norm) / i_norm)) / (1 - i * (tan(r) / i_norm * tanh(i_norm)));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> cot(const nion<T,N> &z) {
return tan(z).inv();
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> sec(const nion<T,N> &z) {
return cos(z).inv();
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> csc(const nion<T,N> &z) {
return sin(z).inv();
}
/***************************************************
* NION INVERSE HYPERBOLIC TRIGONOMETRIC FUNCTIONS *
****************************************************/
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> asinh(const nion<T,N> &z) {
return log(z + sqrt(sqr(z) + 1.0l));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> acosh(const nion<T,N> &z) {
// compute the inverse hyperbolic cosine of the nion
return 2 * log(sqrt((z-1) * 0.5l) + sqrt((z+1.0l) * 0.5l));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> atanh(const nion<T,N> &z) {
return (log(1.0l + z) - log(1.0l - z)) * 0.5l;
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> acoth(const nion<T,N> &z) {
return (log(1.0l + inv(z)) - log(1.0l - inv(z))) * 0.5l;
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> asech(const nion<T,N> &z) {
return log(sqrt(1.0l/sqr(z) - 1.0l) + inv(z));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> acsch(const nion<T,N> &z) {
return log(sqrt(1.0l + 1.0l/sqr(z)) + inv(z));
}
/****************************************
* NION INVERSE TRIGONOMETRIC FUNCTIONS *
*****************************************/
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> atri(const nion<T,N> &z, const nion<T,N> &a, const nion<T,N> &b, const nion<T,N> &c){
// get the polar form of the nion
auto [z_abs, i_abs, i] = z.polar();
return -i * log((a + b * i)/c);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> asin(const nion<T,N> &z) {
// get the polar form of the nion
T r = real(z);
auto [mag2, phase2, i] = z.polar(); // decompose the nion into its polar form
T phase = sqrt(phase2); // compute the norm of the imaginary part
// return i * asinh(-i*r + phase);
// compute the inv sine of the nion
return -i * log(sqrt(1.0l - sqr(z)) + (i * r) - phase);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> acos(const nion<T,N> &z) {
return M_PI_2 - asin(z);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> atan(const nion<T,N> &z) {
// get the polar form of the nion
T r = real(z);
nion<T,N> i = z.imag();
// make unit vector
T i_abs = i.abs();
T i_norm = sqrt(i_abs);
// compute denorm_min
T denorm_min = T(0); // default value
if constexpr (std::is_arithmetic_v<T>) {
denorm_min = std::numeric_limits<T>::denorm_min(); // if T is arithmetic, use its denorm_min
}
if (i_abs > denorm_min)
i /= i_norm;
// compute the inv tangent of the nion:
T one = 1;
return 0.5l * i * (-log((one - i_norm) + (i * r) ) + log((one + i_norm) + (i * -r) ));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> acot(const nion<T,N> &z) {
return M_PI_2 - atan(z);
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> asec(const nion<T,N> &z) {
return acos(inv(z));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> acsc(const nion<T,N> &z) {
return asin(inv(z));
}
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> atan2(const nion<T,N> &y, const nion<T,N> &x) {
return atan(y / x);
}
/***************************
* NION GAMMA FUNCTION *
**************************/
template<arith_ops T, std::size_t N>
constexpr inline nion<T,N> gamma(const nion<T,N> &z) {
// compute the gamma function of the nion
return exp(-z) * sqrt(inv(z))
* pow(1.0l / (12.0l * z - inv(10.0l * z)) + z, z)
* sqrt(2.0l * M_PI);
}
#endif // NION_TRIG_CPP