LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
Matrix3d.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2014-2015 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #ifndef LSST_SPHGEOM_MATRIX3D_H_
24 #define LSST_SPHGEOM_MATRIX3D_H_
25 
28 
29 #include <iosfwd>
30 
31 #include "Vector3d.h"
32 
33 
34 namespace lsst {
35 namespace sphgeom {
36 
38 class Matrix3d {
39 public:
41  Matrix3d() {}
42 
45  Matrix3d(double m00, double m01, double m02,
46  double m10, double m11, double m12,
47  double m20, double m21, double m22)
48  {
49  _c[0] = Vector3d(m00, m10, m20);
50  _c[1] = Vector3d(m01, m11, m21);
51  _c[2] = Vector3d(m02, m12, m22);
52  }
53 
56  explicit Matrix3d(Vector3d const & v) {
57  _c[0] = Vector3d(v.x(), 0.0, 0.0);
58  _c[1] = Vector3d(0.0, v.y(), 0.0);
59  _c[2] = Vector3d(0.0, 0.0, v.z());
60  }
61 
63  explicit Matrix3d(double s) {
64  _c[0] = Vector3d(s, 0.0, 0.0);
65  _c[1] = Vector3d(0.0, s, 0.0);
66  _c[2] = Vector3d(0.0, 0.0, s);
67  }
68 
69  bool operator==(Matrix3d const & m) const {
70  return _c[0] == m._c[0] &&
71  _c[1] == m._c[1] &&
72  _c[2] == m._c[2];
73  }
74 
75  bool operator!=(Matrix3d const & m) const {
76  return _c[0] != m._c[0] ||
77  _c[1] != m._c[1] ||
78  _c[2] != m._c[2];
79  }
80 
82  Vector3d getRow(int r) const {
83  return Vector3d(getColumn(0)(r), getColumn(1)(r), getColumn(2)(r));
84  }
85 
87  Vector3d const & getColumn(int c) const { return _c[c]; }
88 
91  double operator()(int r, int c) const { return getColumn(c)(r); }
92 
94  double inner(Matrix3d const & m) const {
95  Matrix3d p = cwiseProduct(m);
96  Vector3d sum = p._c[0] + p._c[1] + p._c[2];
97  return sum(0) + sum(1) + sum(2);
98  }
99 
102  double getSquaredNorm() const { return inner(*this); }
103 
105  double getNorm() const { return std::sqrt(getSquaredNorm()); }
106 
109  Vector3d operator*(Vector3d const & v) const {
110  return Vector3d(_c[0] * v(0) + _c[1] * v(1) + _c[2] * v(2));
111  }
112 
115  Matrix3d operator*(Matrix3d const & m) const {
116  Matrix3d r;
117  for (int i = 0; i < 3; ++i) { r._c[i] = this->operator*(m._c[i]); }
118  return r;
119  }
120 
122  Matrix3d operator+(Matrix3d const & m) const {
123  Matrix3d r;
124  for (int i = 0; i < 3; ++i) { r._c[i] = _c[i] + m._c[i]; }
125  return r;
126  }
127 
129  Matrix3d operator-(Matrix3d const & m) const {
130  Matrix3d r;
131  for (int i = 0; i < 3; ++i) { r._c[i] = _c[i] - m._c[i]; }
132  return r;
133  }
134 
136  Matrix3d cwiseProduct(Matrix3d const & m) const {
137  Matrix3d r;
138  for (int i = 0; i < 3; ++i) { r._c[i] = _c[i].cwiseProduct(m._c[i]); }
139  return r;
140  }
141 
143  Matrix3d transpose() const {
144  Matrix3d t;
145  t._c[0] = Vector3d(_c[0].x(), _c[1].x(), _c[2].x());
146  t._c[1] = Vector3d(_c[0].y(), _c[1].y(), _c[2].y());
147  t._c[2] = Vector3d(_c[0].z(), _c[1].z(), _c[2].z());
148  return t;
149  }
150 
152  Matrix3d inverse() const {
153  Matrix3d inv;
154  Matrix3d const & m = *this;
155  // Find the first column of Adj(m), the adjugate matrix of m.
156  Vector3d a0(m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2),
157  m(1, 2) * m(2, 0) - m(2, 2) * m(1, 0),
158  m(1, 0) * m(2, 1) - m(2, 0) * m(1, 1));
159  // Find 1.0/det(m), where the determinant of m is the dot product of
160  // the first row of m with the first column of Adj(m).
161  double rdet = 1.0 / (a0(0) * m(0,0) + a0(1) * m(0,1) + a0(2) * m(0,2));
162  // The inverse of m is Adj(m)/det(m); compute it column by column.
163  inv._c[0] = a0 * rdet;
164  inv._c[1] = Vector3d((m(0, 2) * m(2, 1) - m(2, 2) * m(0, 1)) * rdet,
165  (m(0, 0) * m(2, 2) - m(2, 0) * m(0, 2)) * rdet,
166  (m(0, 1) * m(2, 0) - m(2, 1) * m(0, 0)) * rdet);
167  inv._c[2] = Vector3d((m(0, 1) * m(1, 2) - m(1, 1) * m(0, 2)) * rdet,
168  (m(0, 2) * m(1, 0) - m(1, 2) * m(0, 0)) * rdet,
169  (m(0, 0) * m(1, 1) - m(1, 0) * m(0, 1)) * rdet);
170  return inv;
171  }
172 
173 private:
174  Vector3d _c[3];
175 };
176 
177 std::ostream & operator<<(std::ostream &, Matrix3d const &);
178 
179 }} // namespace lsst::sphgeom
180 
181 #endif // LSST_SPHGEOM_MATRIX3D_H_
double x
double z
Definition: Match.cc:44
int y
Definition: SpanSet.cc:49
int m
Definition: SpanSet.cc:49
This file declares a class for representing vectors in ℝ³.
A 3x3 matrix with real entries stored in double precision.
Definition: Matrix3d.h:38
Vector3d getRow(int r) const
getRow returns the r-th matrix row. Bounds are not checked.
Definition: Matrix3d.h:82
Matrix3d inverse() const
inverse returns the inverse of this matrix.
Definition: Matrix3d.h:152
bool operator!=(Matrix3d const &m) const
Definition: Matrix3d.h:75
Matrix3d operator+(Matrix3d const &m) const
The addition operator returns the sum of this matrix and m.
Definition: Matrix3d.h:122
Matrix3d(Vector3d const &v)
This constructor creates a diagonal matrix with diagonal components set to the components of v.
Definition: Matrix3d.h:56
bool operator==(Matrix3d const &m) const
Definition: Matrix3d.h:69
Vector3d const & getColumn(int c) const
getColumn returns the c-th matrix column. Bounds are not checked.
Definition: Matrix3d.h:87
Matrix3d()
This constructor creates a zero matrix.
Definition: Matrix3d.h:41
Matrix3d(double s)
This constructor returns the identity matrix scaled by s.
Definition: Matrix3d.h:63
double operator()(int r, int c) const
The function call operator returns the scalar at row r and column c.
Definition: Matrix3d.h:91
double getNorm() const
getNorm returns the L2 (Frobenius) norm of this matrix.
Definition: Matrix3d.h:105
double inner(Matrix3d const &m) const
inner returns the Frobenius inner product of this matrix with m.
Definition: Matrix3d.h:94
Matrix3d(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
This constructor creates a matrix from its components, where mij specifies the component for row i an...
Definition: Matrix3d.h:45
Vector3d operator*(Vector3d const &v) const
The multiplication operator returns the product of this matrix with vector v.
Definition: Matrix3d.h:109
Matrix3d transpose() const
transpose returns the transpose of this matrix.
Definition: Matrix3d.h:143
Matrix3d cwiseProduct(Matrix3d const &m) const
cwiseProduct returns the component-wise product of this matrix and m.
Definition: Matrix3d.h:136
Matrix3d operator-(Matrix3d const &m) const
The subtraction operator returns the difference between this matrix and m.
Definition: Matrix3d.h:129
double getSquaredNorm() const
getSquaredNorm returns the Frobenius inner product of this matrix with itself.
Definition: Matrix3d.h:102
Matrix3d operator*(Matrix3d const &m) const
The multiplication operator returns the product of this matrix with matrix m.
Definition: Matrix3d.h:115
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition: Vector3d.h:44
Vector3d cwiseProduct(Vector3d const &v) const
cwiseProduct returns the component-wise product of this vector and v.
Definition: Vector3d.h:150
double x() const
Definition: Vector3d.h:66
double y() const
Definition: Vector3d.h:68
double z() const
Definition: Vector3d.h:70
std::ostream & operator<<(std::ostream &, Angle const &)
Definition: Angle.cc:34
A base class for image defects.
T sqrt(T... args)