nec2++ 2.1.1
math_util.h
1#pragma once
2
3/*
4 Various Useful Math Utilities for nec2++
5
6 Copyright (C) 2004-2015 Timothy C.A. Molteno
7 tim@molteno.net
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*/
23
24#include "common.h"
25
26
30#include "safe_array.h"
31
36
40
42typedef Eigen::Matrix<nec_float,3,1> nec_3vector;
43typedef Eigen::Matrix<nec_complex,3,1> nec_c3vector;
44
45inline void vector_fill(complex_array& x, int64_t start, int64_t N, const nec_complex& y) {
46 x.fill(start, N, y);
47}
48
49
50inline nec_complex cplx_00() {
51 static nec_complex _cplx00(0.0,0.0); return _cplx00;
52}
53
54inline nec_complex cplx_01() {
55 static nec_complex _cplx01(0.0,1.0); return _cplx01;
56}
57
58inline nec_complex cplx_10() {
59 static nec_complex _cplx10(1.0,0.0); return _cplx10;
60}
61
62inline nec_complex cplx_11() {
63 static nec_complex _cplx11(1.0,1.0); return _cplx11;
64}
65
66
67inline nec_complex cplx_exp(const nec_float& x) {
68 return nec_complex(cos(x),sin(x));
69}
70
71
72inline nec_float pi() {
73 static nec_float _pi = 3.1415926536; return _pi;
74}
75
76inline nec_float two_pi() {
77 static nec_float _tmp = 2.0 * pi(); return _tmp;
78}
79
80inline nec_float four_pi() {
81 static nec_float _tmp = 4.0 * pi(); return _tmp;
82}
83
84inline nec_float pi_two() {
85 static nec_float _tmp = pi() / 2.0; return _tmp;
86}
87
88inline nec_float sqrt_pi() { // was SP from common.h
89 static nec_float _tmp = sqrt(pi()); return _tmp;
90}
91
92
93inline nec_complex two_pi_j() {
94 static nec_complex _tmp(0.0,two_pi()); return _tmp;
95}
96
97
98
99inline nec_float rad_to_degrees(nec_float in_radians) {
100 static nec_float _rad_to_deg = 360.0 / (2 * pi()); // 57.29577951
101 return in_radians * _rad_to_deg;
102}
103
104inline nec_float degrees_to_rad(nec_float in_degrees) {
105 static nec_float _deg_to_rad = (2 * pi()) / 360.0;
106 return in_degrees * _deg_to_rad;
107}
108
111inline nec_complex deg_polar(nec_float r, nec_float theta) {
112 return std::polar(r, degrees_to_rad(theta));
113}
114
115
118inline nec_float arg_degrees(nec_complex z) {
119 return rad_to_degrees(std::arg(z));
120}
121
122
125inline nec_float atgn2( nec_float x, nec_float y) {
126 if ((0.0 == y) && (0.0 == x))
127 return 0.0;
128
129 return( std::atan2(y, x) );
130}
131
132
134inline nec_float db10( nec_float x ) {
135 if ( x < 1.0e-20 )
136 return( -999.99 );
137
138 return( 10.0 * log10(x) );
139}
140
142inline nec_float from_db10( nec_float x ) {
143 if ( x < -99.9 )
144 return( 0.0 );
145
146 return( std::pow(10,x / 10.0));
147}
148
149
151inline nec_float db20( nec_float x ) {
152 if ( x < 1.0e-20 )
153 return( -999.99 );
154
155 return( 20.0 * log10(x) );
156}
157
158
159inline nec_float norm(const nec_float x, const nec_float y) {
160 return std::sqrt(x*x + y*y);
161}
162
163inline nec_float norm2(const nec_float x, const nec_float y, const nec_float z) {
164 return (x*x + y*y + z*z);
165}
166
167inline nec_float norm(const nec_float x, const nec_float y, const nec_float z) {
168 return std::sqrt(norm2(x,y,z));
169}
170
172inline nec_float normL1(const nec_float x, const nec_float y, const nec_float z) {
173 return std::fabs(x) + std::fabs(y) + std::fabs(z);
174}
175
176
177
178
180inline nec_float norm(const nec_3vector& v) {
181 return v.norm();
182}
183
185inline nec_float normL1(const nec_3vector& v) {
186 return normL1(v(0), v(1), v(2));
187}
A Safe Array class backed by Eigen for SIMD-accelerated operations.
Definition safe_array.h:47
Definition safe_array.h:258