Debian Codegraph @

src/atlas/projection/Jacobian.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <array>
14 #include <cmath>
15 #include <initializer_list>
16 #include <iostream>
17 #include <string>
18
19 #include "eckit/geometry/Point2.h"
20
21 //---------------------------------------------------------------------------------------------------------------------
22
23 namespace atlas {
24
25 using Point2 = eckit::geometry::Point2;
26
27 //---------------------------------------------------------------------------------------------------------------------
28 namespace projection {
29
30 class Jacobian : public std::array<std::array<double, 2>, 2> {
31 public:
32 using std::array<std::array<double, 2>, 2>::array;
33
34 static Jacobian identity() { return Jacobian{1., 0., 0., 1.}; }
35
36 Jacobian() = default;
37
38 Jacobian(double j00, double j01, double j10, double j11) {
39 (*this)[0][0] = j00;
40 (*this)[0][1] = j01;
41 (*this)[1][0] = j10;
42 (*this)[1][1] = j11;
43 }
44
45 Jacobian(std::initializer_list<std::initializer_list<double>> list):
46 Jacobian{*(list.begin()->begin()), *(list.begin()->begin() + 1), *((list.begin() + 1)->begin()),
47 *((list.begin() + 1)->begin() + 1)} {}
48
49 Jacobian operator-(const Jacobian& jac) const {
50 return Jacobian{(*this)[0][0] - jac[0][0], (*this)[0][1] - jac[0][1], (*this)[1][0] - jac[1][0],
51 (*this)[1][1] - jac[1][1]};
52 }
53
54 Jacobian operator+(const Jacobian& jac) const {
55 return Jacobian{(*this)[0][0] + jac[0][0], (*this)[0][1] + jac[0][1], (*this)[1][0] + jac[1][0],
56 (*this)[1][1] + jac[1][1]};
57 }
58
59 Jacobian operator*(double a) const {
60 return Jacobian{(*this)[0][0] * a, (*this)[0][1] * a, (*this)[1][0] * a, (*this)[1][1] * a};
61 }
62
63 Jacobian operator*(const Jacobian& jac) const {
64 return Jacobian{(*this)[0][0] * jac[0][0] + (*this)[0][1] * jac[1][0],
65 (*this)[0][0] * jac[0][1] + (*this)[0][1] * jac[1][1],
66 (*this)[1][0] * jac[0][0] + (*this)[1][1] * jac[1][0],
67 (*this)[1][0] * jac[0][1] + (*this)[1][1] * jac[1][1]};
68 }
69
70 Point2 operator*(const Point2& x) const {
71 return Point2{(*this)[0][0] * x[0] + (*this)[0][1] * x[1], (*this)[1][0] * x[0] + (*this)[1][1] * x[1]};
72 }
73
74 double norm() const {
75 return std::sqrt((*this)[0][0] * (*this)[0][0] + (*this)[0][1] * (*this)[0][1] + (*this)[1][0] * (*this)[1][0] +
76 (*this)[1][1] * (*this)[1][1]);
77 }
78
79 double determinant() const { return (*this)[0][0] * (*this)[1][1] - (*this)[0][1] * (*this)[1][0]; }
80
81 Jacobian inverse() const {
82 return Jacobian{(*this)[1][1], -(*this)[0][1], -(*this)[1][0], (*this)[0][0]} * (1. / determinant());
83 }
84
85 Jacobian transpose() const { return Jacobian{(*this)[0][0], (*this)[1][0], (*this)[0][1], (*this)[1][1]}; }
86
87 double dx_dlon() const { return (*this)[JDX][JDLON]; }
88 double dy_dlon() const { return (*this)[JDY][JDLON]; }
89 double dx_dlat() const { return (*this)[JDX][JDLAT]; }
90 double dy_dlat() const { return (*this)[JDY][JDLAT]; }
91
92 double dlon_dx() const { return (*this)[JDLON][JDX]; }
93 double dlon_dy() const { return (*this)[JDLON][JDY]; }
94 double dlat_dx() const { return (*this)[JDLAT][JDX]; }
95 double dlat_dy() const { return (*this)[JDLAT][JDY]; }
96
97 friend std::ostream& operator<<(std::ostream& os, const Jacobian& jac) {
98 os << jac[0][0] << " " << jac[0][1] << "\n" << jac[1][0] << " " << jac[1][1];
99 return os;
100 }
101
102 private:
103 enum
104 {
105 JDX = 0,
106 JDY = 1
107 };
108 enum
109 {
110 JDLON = 0,
111 JDLAT = 1
112 };
113 };
114
115 //---------------------------------------------------------------------------------------------------------------------
116
117 } // namespace projection
118 } // namespace atlas