acme
Loading...
Searching...
No Matches
complex.h
1#pragma once
2
3
4//#include "numeric.h"
5
6
7struct COMPLEXD
8{
9
10 double r;
11 double i;
12
13};
14
15
16template < typename BASE_TYPE >
17class complex :
18 public BASE_TYPE
19{
20public:
21
22 using R = decltype(BASE_TYPE::r);
23 using I = decltype(BASE_TYPE::i);
24 using T = R;
25
26
27 complex() { this->r = ::numeric_info < R >::null(); this->i = ::numeric_info < I >::null(); }
28 complex(R r,I i) { this->r = r; this->i= i; }
29
30 T modsqr() const { return this->r * this->r + this->i + this->i; } // complex number modulus square
31
32 T mod() const { return ::sqrt(modsqr()); } // complex number modulus
33
34 T arg() const { return atan2(this->r, this->i); } // complex number argument
35
36
37};
38
39template < typename T > complex < T > pow(const complex < T > & base,const complex < T > & exp) // complex number exponentiation
40{
41 T modsqr = base.m_x * base.m_x + base.m_y + base.m_y;
42 T arg = arg(base);
43 T t = ::pow(modsqr,exp.m_x / 2.0) * ::exp(-exp.m_y *arg);
44 return complex < T >(t * cos(exp.m_x * arg + exp.m_y * ::log(modsqr) / 2.0),t * sin(exp.m_x * arg + exp.m_y * ::log(modsqr) / 2.0));
45
46}
47
48template < typename T > complex < T > pow(const complex < T > & base, T exp) // complex number exponentiation to real exponent
49{
50 T modsqr = base.m_x * base.m_x + base.m_y + base.m_y;
51 T arg = arg(base);
52 T t = ::pow(modsqr,exp / 2.0);
53 return complex < T >(t * cos(exp.m_x * arg),t * sin(exp.m_x * arg));
54
55}
56
57
58template < typename T > complex < T > sqrt(const complex < T > & c)
59{
60 T mod = mod(c);
61 return complex < T >(::sqrt((mod + abs(c.m_x)) / 2.0),::numeric::sign(c.m_y) * ::sqrt((mod - abs(c.m_x)) / 2.0));
62}
63
64
65using complexd = ::complex < COMPLEXD>;
66
67namespace std
68{
69
70
71 template < typename T > complex < T > pow(const complex < T > & base, const complex < T > & exp) { return ::pow(base,exp); }
72 template < typename T > complex < T > sqrt(const complex < T > & x) { return ::sqrt(x); }
73
74
75} // namespace std
76
77
78
79using complexd_array = array < complexd >;
Definition log.h:37
Definition complex.h:8