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
35
39
41typedef Eigen::Matrix<nec_float,3,1> nec_3vector;
42typedef Eigen::Matrix<nec_complex,3,1> nec_c3vector;
43
44inline void vector_fill(complex_array& x, int64_t start, int64_t N, const nec_complex& y) {
45 x.fill(start, N, y);
46}
47
48
49inline nec_complex cplx_00() {
50 static nec_complex _cplx00(0.0,0.0); return _cplx00;
51}
52
53inline nec_complex cplx_01() {
54 static nec_complex _cplx01(0.0,1.0); return _cplx01;
55}
56
57inline nec_complex cplx_10() {
58 static nec_complex _cplx10(1.0,0.0); return _cplx10;
59}
60
61inline nec_complex cplx_11() {
62 static nec_complex _cplx11(1.0,1.0); return _cplx11;
63}
64
65
66inline nec_complex cplx_exp(const nec_float& x) {
67 return nec_complex(cos(x),sin(x));
68}
69
70
71inline nec_float pi() {
72 static nec_float _pi = 3.1415926536; return _pi;
73}
74
75inline nec_float two_pi() {
76 static nec_float _tmp = 2.0 * pi(); return _tmp;
77}
78
79inline nec_float four_pi() {
80 static nec_float _tmp = 4.0 * pi(); return _tmp;
81}
82
83inline nec_float pi_two() {
84 static nec_float _tmp = pi() / 2.0; return _tmp;
85}
86
87inline nec_float sqrt_pi() { // was SP from common.h
88 static nec_float _tmp = sqrt(pi()); return _tmp;
89}
90
91
92inline nec_complex two_pi_j() {
93 static nec_complex _tmp(0.0,two_pi()); return _tmp;
94}
95
96
97
98inline nec_float rad_to_degrees(nec_float in_radians) {
99 static nec_float _rad_to_deg = 360.0 / (2 * pi()); // 57.29577951
100 return in_radians * _rad_to_deg;
101}
102
103inline nec_float degrees_to_rad(nec_float in_degrees) {
104 static nec_float _deg_to_rad = (2 * pi()) / 360.0;
105 return in_degrees * _deg_to_rad;
106}
107
110inline nec_complex deg_polar(nec_float r, nec_float theta) {
111 return std::polar(r, degrees_to_rad(theta));
112}
113
114
117inline nec_float arg_degrees(nec_complex z) {
118 return rad_to_degrees(std::arg(z));
119}
120
121
124inline nec_float atgn2( nec_float x, nec_float y) {
125 if ((0.0 == y) && (0.0 == x))
126 return 0.0;
127
128 return( std::atan2(y, x) );
129}
130
131
133inline nec_float db10( nec_float x ) {
134 if ( x < 1.0e-20 )
135 return( -999.99 );
136
137 return( 10.0 * log10(x) );
138}
139
141inline nec_float from_db10( nec_float x ) {
142 if ( x < -99.9 )
143 return( 0.0 );
144
145 return( std::pow(10,x / 10.0));
146}
147
148
150inline nec_float db20( nec_float x ) {
151 if ( x < 1.0e-20 )
152 return( -999.99 );
153
154 return( 20.0 * log10(x) );
155}
156
157
158inline nec_float norm(const nec_float x, const nec_float y) {
159 return std::sqrt(x*x + y*y);
160}
161
162inline nec_float norm2(const nec_float x, const nec_float y, const nec_float z) {
163 return (x*x + y*y + z*z);
164}
165
166inline nec_float norm(const nec_float x, const nec_float y, const nec_float z) {
167 return std::sqrt(norm2(x,y,z));
168}
169
171inline nec_float normL1(const nec_float x, const nec_float y, const nec_float z) {
172 return std::fabs(x) + std::fabs(y) + std::fabs(z);
173}
174
175
176
177
179inline nec_float norm(const nec_3vector& v) {
180 return v.norm();
181}
182
184inline nec_float normL1(const nec_3vector& v) {
185 return normL1(v(0), v(1), v(2));
186}
A Safe Array class backed by Eigen for SIMD-accelerated operations.
Definition safe_array.h:47
Definition safe_array.h:258