-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMathUtil.h
171 lines (142 loc) · 4.45 KB
/
MathUtil.h
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
//# MathUtil.h: Various mathematical operations on vectors and matrices.
//#
//# Copyright (C) 2013
//# ASTRON (Netherlands Institute for Radio Astronomy)
//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
//#
//# This file is part of the LOFAR software suite.
//# The LOFAR software suite is free software: you can redistribute it and/or
//# modify it under the terms of the GNU General Public License as published
//# by the Free Software Foundation, either version 3 of the License, or
//# (at your option) any later version.
//#
//# The LOFAR software suite is distributed in the hope that it will be useful,
//# but WITHOUT ANY WARRANTY; without even the implied warranty of
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//# GNU General Public License for more details.
//#
//# You should have received a copy of the GNU General Public License along
//# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
//#
//# $Id$
#ifndef LOFAR_STATIONRESPONSE_MATHUTIL_H
#define LOFAR_STATIONRESPONSE_MATHUTIL_H
// \file
// Various mathematical operations on vectors and matrices.
#include "Constants.h"
#include "Types.h"
namespace LOFAR
{
namespace StationResponse
{
// \addtogroup StationResponse
// @{
inline real_t dot(const vector3r_t &arg0, const vector3r_t &arg1)
{
return arg0[0] * arg1[0] + arg0[1] * arg1[1] + arg0[2] * arg1[2];
}
inline double norm(const vector3r_t &arg0)
{
return sqrt(dot(arg0, arg0));
}
inline vector3r_t operator*(real_t arg0, const vector3r_t arg1)
{
vector3r_t result = {{arg0 * arg1[0], arg0 * arg1[1], arg0 * arg1[2]}};
return result;
}
inline vector3r_t normalize(const vector3r_t &arg0)
{
return 1.0 / norm(arg0) * arg0;
}
inline vector2r_t cart2thetaphi(const vector3r_t &cart)
{
real_t r = sqrt(cart[0] * cart[0] + cart[1] * cart[1]);
vector2r_t thetaphi = {{Constants::pi_2 - atan2(cart[2], r), atan2(cart[1],
cart[0])}};
return thetaphi;
}
inline vector3r_t thetaphi2cart(const vector2r_t &thetaphi)
{
real_t r = sin(thetaphi[0]);
vector3r_t cart = {{r * cos(thetaphi[1]), r * sin(thetaphi[1]),
cos(thetaphi[0])}};
return cart;
}
// returns az, el, r.
inline vector3r_t cart2sph(const vector3r_t &cart)
{
real_t r = sqrt(cart[0] * cart[0] + cart[1] * cart[1]);
vector3r_t sph;
sph[0] = atan2(cart[1], cart[0]);
sph[1] = atan2(cart[2], r);
sph[2] = norm(cart);
return sph;
}
// expects az, el, r.
inline vector3r_t sph2cart(const vector3r_t &sph)
{
vector3r_t cart = {{sph[2] * cos(sph[1]) * cos(sph[0]), sph[2] * cos(sph[1])
* sin(sph[0]), sph[2] * sin(sph[1])}};
return cart;
}
inline matrix22c_t operator*(const matrix22c_t &arg0, const matrix22r_t &arg1)
{
matrix22c_t result;
result[0][0] = arg0[0][0] * arg1[0][0] + arg0[0][1] * arg1[1][0];
result[0][1] = arg0[0][0] * arg1[0][1] + arg0[0][1] * arg1[1][1];
result[1][0] = arg0[1][0] * arg1[0][0] + arg0[1][1] * arg1[1][0];
result[1][1] = arg0[1][0] * arg1[0][1] + arg0[1][1] * arg1[1][1];
return result;
}
inline vector3r_t cross(const vector3r_t &arg0, const vector3r_t &arg1)
{
vector3r_t result;
result[0] = arg0[1] * arg1[2] - arg0[2] * arg1[1];
result[1] = arg0[2] * arg1[0] - arg0[0] * arg1[2];
result[2] = arg0[0] * arg1[1] - arg0[1] * arg1[0];
return result;
}
inline vector3r_t operator+(const vector3r_t &arg0, const vector3r_t &arg1)
{
vector3r_t result = {{arg0[0] + arg1[0], arg0[1] + arg1[1],
arg0[2] + arg1[2]}};
return result;
}
inline vector3r_t operator-(const vector3r_t &arg0, const vector3r_t &arg1)
{
vector3r_t result = {{arg0[0] - arg1[0], arg0[1] - arg1[1],
arg0[2] - arg1[2]}};
return result;
}
inline matrix22c_t normalize(const raw_response_t &raw)
{
matrix22c_t response = {{ {{}}, {{}} }};
if(raw.weight[0] != 0.0)
{
response[0][0] = raw.response[0][0] / raw.weight[0];
response[0][1] = raw.response[0][1] / raw.weight[0];
}
if(raw.weight[1] != 0.0)
{
response[1][0] = raw.response[1][0] / raw.weight[1];
response[1][1] = raw.response[1][1] / raw.weight[1];
}
return response;
}
inline diag22c_t normalize(const raw_array_factor_t &raw)
{
diag22c_t af = {{}};
if(raw.weight[0] != 0.0)
{
af[0] = raw.factor[0] / raw.weight[0];
}
if(raw.weight[1] != 0.0)
{
af[1] = raw.factor[1] / raw.weight[1];
}
return af;
}
// @}
} //# namespace StationResponse
} //# namespace LOFAR
#endif