COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
matrix.h
Go to the documentation of this file.
1 /********************************************************************
2 * Copyright (C) 2015 by Interstel Technologies, Inc.
3 * and Hawaii Space Flight Laboratory.
4 *
5 * This file is part of the COSMOS/core that is the central
6 * module for COSMOS. For more information on COSMOS go to
7 * <http://cosmos-project.com>
8 *
9 * The COSMOS/core software is licenced under the
10 * GNU Lesser General Public License (LGPL) version 3 licence.
11 *
12 * You should have received a copy of the
13 * GNU Lesser General Public License
14 * If not, go to <http://www.gnu.org/licenses/>
15 *
16 * COSMOS/core is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public License
18 * as published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * COSMOS/core is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * Refer to the "licences" folder for further information on the
27 * condititons and terms to use this software.
28 ********************************************************************/
29 
30 // dependencies: vector
31 #ifndef _MATH_MATRIX_H
32 #define _MATH_MATRIX_H
33 
34 #include "support/configCosmos.h"
35 #include "math/vector.h"
36 using namespace Cosmos::Math::Vectors;
37 
39 
41 struct rmatrix
42 {
43  rvector row[3];
44 
45  rmatrix() {}
46  rmatrix(rvector rv0, rvector rv1, rvector rv2) {
47  row[0] = rv0;
48  row[1] = rv1;
49  row[2] = rv2;
50  }
51 
53 
57  vector<rvector> v_row = vector<rvector>(row, row+sizeof(row)/sizeof(row[0]));
58  return json11::Json::object {
59  { "row" , v_row }
60  };
61  }
62 
64 
69  void from_json(const string& s) {
70  string error;
71  json11::Json parsed = json11::Json::parse(s,error);
72  if(error.empty()) {
73  auto p_row = parsed["row"].array_items();
74  for(size_t i = 0; i != p_row.size(); ++i) {
75  if(!p_row[i].is_null()) row[i].from_json(p_row[i].dump());
76  }
77  } else {
78  cerr<<"ERROR: <"<<error<<">"<<endl;
79  }
80  return;
81  }
82 
83 };
84 
85 std::ostream& operator << (std::ostream& out, const rmatrix& a);
86 std::ostream& operator << (std::ostream& out, const vector<rmatrix>& a);
87 std::istream& operator >> (std::istream& out, rmatrix& a);
88 
89 rvector operator * (rmatrix m, rvector v); // multiply matrix by vector operator
90 
91 
92 
94 
96 struct cmatrix
97 {
104 
106 
110  return json11::Json::object {
111  { "r1" , r1 },
112  { "r2" , r2 },
113  { "r3" , r3 }
114  };
115  }
116 
118 
123  void from_json(const string& s) {
124  string error;
125  json11::Json parsed = json11::Json::parse(s,error);
126  if(error.empty()) {
127  if(!parsed["r1"].is_null()) r1.from_json(parsed["r1"].dump());
128  if(!parsed["r2"].is_null()) r2.from_json(parsed["r2"].dump());
129  if(!parsed["r3"].is_null()) r3.from_json(parsed["r3"].dump());
130  } else {
131  cerr<<"ERROR: <"<<error<<">"<<endl;
132  }
133  return;
134  }
135 
136 
137 } ;
138 
139 std::ostream& operator << (std::ostream& out, const cmatrix& a);
140 std::istream& operator >> (std::istream& in, cmatrix& a);
141 
142 
144 struct matrix1d
145 {
147  double vector[4];
149  uint16_t cols;
150 } ;
151 
153 struct matrix2d
154 {
156  uint16_t rows;
158  uint16_t cols;
160  double array[4][4];
161 } ;
162 
163 
166 rvector rv_unskew(rmatrix matrix);
169 
172 //rmatrix rm_sqrt(rmatrix a);
173 rmatrix rm_eye();
174 rmatrix rm_zero();
177 rmatrix rm_smult(double a, rmatrix b);
181 rmatrix rm_change_around_x(double angle);
182 rmatrix rm_change_around_y(double angle);
183 rmatrix rm_change_around_z(double angle);
184 rmatrix rm_change_around(int axis, double angle);
186 rmatrix rm_from_cm(cmatrix matrix);
187 rmatrix rm_from_rv(rvector vector,int direction);
188 rmatrix rm_skew(rvector row1);
191 
192 double norm_rm(rmatrix a);
193 //double norm_rm2(rmatrix a);
194 
195 double trace_rm(rmatrix a);
196 //double trace_rm2(rmatrix a);
197 double determinant_rm(rmatrix a);
198 
199 
200 double norm_cm(cmatrix a);
201 double trace_cm(cmatrix a);
202 
205 //cmatrix cm_sqrt(cmatrix a);
206 cmatrix cm_eye();
207 cmatrix cm_zero();
210 cmatrix cm_smult(double a, cmatrix b);
214 cmatrix cm_change_around_x(double angle);
215 cmatrix cm_change_around_y(double angle);
216 cmatrix cm_change_around_z(double angle);
217 cmatrix cm_change_around(int axis, double angle);
218 cmatrix cm_from_rm(rmatrix matrix);
220 
222 double m2_determinant(matrix2d m);
223 matrix2d m2_zero(uint16_t rows, uint16_t cols);
224 matrix2d m2_eye(uint16_t rows);
225 matrix2d m2_cross(matrix2d matrix1, matrix2d matrix2);
226 matrix2d m2_smult(double number, matrix2d matrix);
227 matrix2d m2_sub(matrix2d matrix1, matrix2d matrix2);
228 matrix2d m2_add(matrix2d matrix1, matrix2d matrix2);
230 matrix1d m2_unskew(matrix2d matrix);
231 double m2_trace(matrix2d matrix);
232 matrix2d m2_mmult(matrix2d matrix1, matrix2d matrix2);
233 matrix1d m2_eig2x2(matrix2d matrix);
234 double m2_snorm2x2(matrix2d matrix);
235 matrix2d cv_to_m2(cvector vector,int direction);
236 matrix1d cv_to_m1(cvector vector);
238 matrix2d m1_to_m2(matrix1d vector,int direction);
239 matrix2d m2_from_rm(rmatrix matrix);
240 
241 matrix1d m1_zero(uint16_t cols);
242 matrix1d m1_smult(double number, matrix1d row);
243 matrix1d m1_mmult(matrix2d matrix, matrix1d row);
244 matrix1d m1_sub(matrix1d row1, matrix1d row2);
245 matrix1d m1_add(matrix1d row1, matrix1d row2);
246 matrix1d m1_cross(matrix1d row1, matrix1d row2);
249 double m1_norm(matrix1d matrix);
250 double m1_dot(matrix1d a, matrix1d b);
251 
252 namespace Cosmos {
253  namespace Math {
254  namespace Matrices {
255  class Matrix
256  {
257 
258  public:
260  explicit Matrix(Vector r00=Vector(), Vector r10=Vector(), Vector r20=Vector())
261  {
262  r0 = r00;
263  r1 = r10;
264  r2 = r20;
265  }
266 
267  explicit Matrix(rmatrix m0)
268  {
269  r0 = Vector(m0.row[0]);
270  r1 = Vector(m0.row[1]);
271  r2 = Vector(m0.row[2]);
272  }
273 
274  explicit Matrix(matrix2d m)
275  {
276  r0[0] = m.array[0][0];
277  r0[1] = m.array[0][1];
278  r0[2] = m.array[0][2];
279  r1[0] = m.array[1][0];
280  r1[1] = m.array[1][1];
281  r1[2] = m.array[1][2];
282  r2[0] = m.array[2][0];
283  r2[1] = m.array[2][1];
284  r2[2] = m.array[2][2];
285  }
286 
287  explicit Matrix(cmatrix m)
288  {
289  r0[0] = m.r1.x;
290  r0[1] = m.r1.y;
291  r0[2] = m.r1.z;
292  r1[0] = m.r2.x;
293  r1[1] = m.r2.y;
294  r1[2] = m.r2.z;
295  r2[0] = m.r3.x;
296  r2[1] = m.r3.y;
297  r2[2] = m.r3.z;
298  }
299 
301 
307  Matrix(Vector vector,int direction)
308  {
309  switch (direction)
310  {
311  case DIRECTION_COLUMN:
312  r0[0] = vector[0];
313  r1[0] = vector[1];
314  r2[0] = vector[2];
315  break;
316  default:
317  r0 = vector;
318  break;
319  }
320  }
321 
325 
327  {
328  Vector c;
329  c[0] = (*this)[0][0];
330  c[1] = (*this)[1][0];
331  c[2] = (*this)[2][0];
332  return c;
333  }
334 
336  {
337  Vector c;
338  c[0] = (*this)[0][1];
339  c[1] = (*this)[1][1];
340  c[2] = (*this)[2][0];
341  return c;
342  }
343 
345  {
346  Vector c;
347  c[0] = (*this)[0][2];
348  c[1] = (*this)[1][2];
349  c[2] = (*this)[2][2];
350  return c;
351  }
352 
353  Vector &operator [] (const int &index);
354 
355  Matrix &operator *= (const double scale);
356  Matrix operator * (const double scale) const;
357 // Matrix smult(const double scale) const;
358 
359  Vector operator * (const Vector v) const;
360 // Vector mmult(const Vector v) const;
361 
362  Matrix &operator *= (const Matrix &m);
363  Matrix operator * (const Matrix &m) const;
364  Matrix mmult(const Matrix &m) const;
365 
366  Matrix mult(const Matrix &b) const;
367 
368  Matrix &operator += (const Matrix &m);
369  Matrix operator + (const Matrix &m) const;
370 
371  Matrix &operator -= (const Matrix &m);
372  Matrix operator - (const Matrix &m) const;
373 
374  Vector diag();
375  Vector unskew();
376 
377  Matrix transpose();
378  Matrix square();
379  Matrix inverse();
380  Matrix change_between(Vector from, Vector to);
381  Matrix change_around_x(double angle);
382  Matrix change_around_y(double angle);
383  Matrix change_around_z(double angle);
384  Matrix change_around(int axis, double angle);
385  Matrix diag(Vector a);
386  Matrix skew(Vector row1);
387 
388  double determinant();
389 
391 
395  return json11::Json::object {
396  { "r0" , r0 },
397  { "r1" , r1 },
398  { "r2" , r2 }
399  };
400  }
401 
403 
408  void from_json(const string& s) {
409  string error;
410  json11::Json parsed = json11::Json::parse(s,error);
411  if(error.empty()) {
412  if(!parsed["r0"].is_null()) r0.from_json(parsed["r0"].dump());
413  if(!parsed["r1"].is_null()) r1.from_json(parsed["r1"].dump());
414  if(!parsed["r2"].is_null()) r2.from_json(parsed["r2"].dump());
415  } else {
416  cerr<<"ERROR: <"<<error<<">"<<endl;
417  }
418  return;
419  }
420  };
421 
422  Matrix operator * (const double scale, Matrix &v);
423  Matrix eye(double scale = 1.);
424  }
425  } // end of namespace Nath
426 } // end of namespace COSMOS
427 
428 #endif
rmatrix rm_from_rv(rvector vector, int direction)
rvector to rmatrix.
Definition: matrix.cpp:828
rvector rv_unskew(rmatrix matrix)
Unskew 3x3 row matrix.
Definition: matrix.cpp:880
double y
Y value.
Definition: vector.h:114
matrix1d m1_smult(double number, matrix1d row)
Multiply 1D matrix by a scalar.
Definition: matrix.cpp:979
Definition: agentclass.cpp:54
rmatrix rm_add(rmatrix a, rmatrix b)
rmatrix addition. Sum of two rmatrix values.
Definition: matrix.cpp:269
std::map< std::string, Json > object
Definition: json11.hpp:88
matrix2d m2_eye(uint16_t rows)
Create 2D identity matrix.
Definition: matrix.cpp:1261
rmatrix rm_skew(rvector row1)
Create skew symmetric rmatrix from rvector.
Definition: matrix.cpp:857
uint16_t cols
Number of elements.
Definition: matrix.h:149
Matrix(rmatrix m0)
Definition: matrix.h:267
Definition: json11.hpp:79
cvector cv_diag(cmatrix a)
Matrix diagonal.
Definition: matrix.cpp:549
matrix2d m2_skew(matrix1d row1)
Create skew symmetric matrix2d from matrix1d.
Definition: matrix.cpp:1103
Matrix(Vector vector, int direction)
::Vector to ::Matrix.
Definition: matrix.h:307
double norm_cm(cmatrix a)
cmatrix norm. Calculates the Norm of the supplied cmatrix
Definition: matrix.cpp:493
void from_json(const string &s)
Set class contents from JSON string.
Definition: matrix.h:408
3x3 element cartesian matrix
Definition: matrix.h:96
rmatrix rm_change_around(int axis, double angle)
Rotation matrix for indicated axis.
Definition: matrix.cpp:397
3 element generic row vector
Definition: vector.h:53
int i
Definition: rw_test.cpp:37
matrix2d m2_smult(double number, matrix2d matrix)
Multiply 2D matrix by a scalar.
Definition: matrix.cpp:1281
rmatrix rm_zero()
Zero filled rmatrix.
Definition: matrix.cpp:125
double trace_rm(rmatrix a)
rmatrix Trace Calculates the trace of the supplied rmatrix.
Definition: matrix.cpp:154
rvector rv_diag(rmatrix a)
Matrix diagonal.
Definition: matrix.cpp:63
json11::Json to_json() const
Convert class contents to JSON object.
Definition: matrix.h:394
cmatrix cm_change_around(int axis, double angle)
Rotation matrix for indicated axis.
Definition: matrix.cpp:773
rmatrix rm_square(rmatrix a)
Square rmatrix.
Definition: matrix.cpp:317
cmatrix cm_mmult(cmatrix a, cmatrix b)
cmatrix Matrix Product
Definition: matrix.cpp:566
Definition: vector.cpp:1578
matrix2d m2_sub(matrix2d matrix1, matrix2d matrix2)
Subtract one matrix2d from another.
Definition: matrix.cpp:1332
cvector r1
Row 1.
Definition: matrix.h:99
matrix2d m2_transpose(matrix2d matrix)
Return transpose of a 2D matrix.
Definition: matrix.cpp:1358
matrix2d m2_diag(matrix1d row1)
Create diagonal matrix2d from matrix1d.
Definition: matrix.cpp:1131
double x
X value.
Definition: vector.h:112
double norm_rm(rmatrix a)
rmatrix norm. Calculates the Norm of the supplied rmatrix
Definition: matrix.cpp:137
Vector r2
Definition: matrix.h:324
matrix1d m1_mmult(matrix2d matrix, matrix1d row)
Multiply matrix1d by matrix2d.
Definition: matrix.cpp:1041
long b
Definition: jpegint.h:371
double m1_norm(matrix1d matrix)
Compute the Euclidean norm of a 1D matrix.
Definition: matrix.cpp:1215
rmatrix()
Definition: matrix.h:45
cmatrix cm_sub(cmatrix a, cmatrix b)
cmatrix subtraction. Subtract two cmatrix values.
Definition: matrix.cpp:662
double determinant_rm(rmatrix a)
Determinant of row column matrix.
Definition: matrix.cpp:942
Vector c1()
Definition: matrix.h:335
rmatrix rm_mult(rmatrix a, rmatrix b)
Element-wise rmatrix multiplication.
Definition: matrix.cpp:223
rmatrix rm_eye()
Identity rmatrix.
Definition: matrix.cpp:114
cmatrix cm_change_around_z(double angle)
Rotation matrix for Z axis.
Definition: matrix.cpp:757
double array[4][4]
Elements.
Definition: matrix.h:160
void dump(std::string &out) const
Vector eye(double scale)
Definition: vector.cpp:2068
void from_json(const string &s)
Set class contents from JSON string.
Definition: vector.h:783
double m2_trace(matrix2d matrix)
Calculate the trace of a 2D matrix.
Definition: matrix.cpp:1404
static Json parse(const std::string &in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
cmatrix cm_change_around_x(double angle)
Rotation matrix for X axis.
Definition: matrix.cpp:727
rmatrix rm_inverse(rmatrix m)
Inverse of rmatrix.
Definition: matrix.cpp:897
static void dump(NullStruct, string &out)
Definition: json11.cpp:53
Vector c0()
Definition: matrix.h:326
cmatrix cm_smult(double a, cmatrix b)
Scalar cmatrix multiplication.
Definition: matrix.cpp:615
uint16_t rows
Number of rows.
Definition: matrix.h:156
3x3 element generic matrix
Definition: matrix.h:41
cmatrix cm_diag(cvector a)
Diagonal cmatrix Creates an cmatrix whose diagonal is filled with the supplied cvector.
Definition: matrix.cpp:453
cmatrix cm_mult(cmatrix a, cmatrix b)
Element-wise cmatrix multiplication.
Definition: matrix.cpp:591
cmatrix cm_add(cmatrix a, cmatrix b)
cmatrix addition. Sum of two cmatrix values.
Definition: matrix.cpp:637
rmatrix rm_diag(rvector a)
Diagonal rmatrix Creates an rmatrix whose diagonal is filled with the supplied rvector.
Definition: matrix.cpp:97
Headers and definitions common to all COSMOS.
rmatrix rm_sub(rmatrix a, rmatrix b)
rmatrix subtraction. Subtract two rmatrix values.
Definition: matrix.cpp:294
rmatrix rm_change_around_x(double angle)
Rotation matrix for X axis.
Definition: matrix.cpp:358
const array & array_items() const
Definition: json11.cpp:284
cmatrix cm_eye()
Identity cmatrix.
Definition: matrix.cpp:470
matrix2d cv_to_m2(cvector vector, int direction)
Definition: matrix.cpp:1516
nxm element 2D matrix
Definition: matrix.h:153
void from_json(const string &s)
Set class contents from JSON string.
Definition: vector.h:150
matrix1d m2_eig2x2(matrix2d matrix)
Eigen values of a 2x2 square matrix.
Definition: matrix.cpp:1580
void from_json(const string &s)
Set class contents from JSON string.
Definition: matrix.h:69
rmatrix rm_change_around_y(double angle)
Rotation matrix for Y axis.
Definition: matrix.cpp:368
#define DIRECTION_COLUMN
Definition: math/constants.h:64
Matrix(Vector r00=Vector(), Vector r10=Vector(), Vector r20=Vector())
Constructor, initialize to zero.
Definition: matrix.h:260
double m1_dot(matrix1d a, matrix1d b)
matrix1d dot product
Definition: matrix.cpp:1085
Vector r0
Definition: matrix.h:322
matrix1d m1_cross(matrix1d row1, matrix1d row2)
matrix1d cross product
Definition: matrix.cpp:1067
Definition: eci2kep_test.cpp:33
matrix1d cv_to_m1(cvector vector)
Definition: matrix.cpp:1498
cmatrix cm_zero()
Zero filled cmatrix.
Definition: matrix.cpp:481
cmatrix cm_change_between_cv(cvector from, cvector to)
Create rotation matrix from 2 vectors.
Definition: mathlib.cpp:2407
matrix2d m2_inverse(matrix2d m)
Inverse of matrix2d.
Definition: matrix.cpp:1152
n element row matrix
Definition: matrix.h:144
Vector c2()
Definition: matrix.h:344
double z
Z value.
Definition: vector.h:116
cmatrix cm_from_rm(rmatrix matrix)
cmatrix from rmatrix
Definition: matrix.cpp:428
Vector operator*(const double scale, const Vector &v)
Reverse scalar product.
Definition: vector.cpp:1794
double trace_cm(cmatrix a)
cmatrix Trace Calculates the trace of the supplied cmatrix.
Definition: matrix.cpp:510
std::ostream & operator<<(std::ostream &out, const Vector &v)
Definition: vector.cpp:1983
rmatrix rm_smult(double a, rmatrix b)
Scalar rmatrix multiplication.
Definition: matrix.cpp:247
Vector r1
Definition: matrix.h:323
rmatrix rm_transpose(rmatrix a)
rmatrix Transpose. Calculate the transpose of the supplied rmatrix.
Definition: matrix.cpp:172
uint16_t cols
Number of elements.
Definition: matrix.h:158
matrix2d m2_mmult(matrix2d matrix1, matrix2d matrix2)
Matrix product.
Definition: matrix.cpp:1423
cmatrix cm_square(cmatrix a)
Square cmatrix.
Definition: matrix.cpp:685
rmatrix rm_mmult(rmatrix a, rmatrix b)
rmatrix Matrix Product
Definition: matrix.cpp:198
rmatrix rm_change_around_z(double angle)
Rotation matrix for Z axis.
Definition: matrix.cpp:382
rmatrix(rvector rv0, rvector rv1, rvector rv2)
Definition: matrix.h:46
Matrix(cmatrix m)
Definition: matrix.h:287
matrix2d m1_to_m2(matrix1d vector, int direction)
Matrix1d to matrix2d.
Definition: matrix.cpp:1546
Matrix(matrix2d m)
Definition: matrix.h:274
Definition: matrix.h:255
void from_json(const string &s)
Set class contents from JSON string.
Definition: matrix.h:123
cmatrix cm_change_around_y(double angle)
Rotation matrix for Y axis.
Definition: matrix.cpp:742
matrix2d m2_cross(matrix2d matrix1, matrix2d matrix2)
rmatrix rm_change_between_rv(rvector from, rvector to)
Create rotation matrix from 2 row vectors.
Definition: rotation.cpp:164
json11::Json to_json() const
Convert class contents to JSON object.
Definition: matrix.h:109
Vector Class.
Definition: vector.h:672
cmatrix cm_transpose(cmatrix a)
cmatrix Transpose. Calculate the transpose of the supplied cmatrix.
Definition: matrix.cpp:525
json11::Json to_json() const
Convert class contents to JSON object.
Definition: matrix.h:56
matrix2d m2_zero(uint16_t rows, uint16_t cols)
Create 2D zero matrix.
Definition: matrix.cpp:1233
rmatrix rm_from_cm(cmatrix matrix)
rmatrix from cmatrix
Definition: matrix.cpp:804
cvector cv_mmult(cmatrix a, cvector b)
Multiply cartesian vector by cartesian matrix.
Definition: matrix.cpp:80
matrix2d cm3x3_to_m2(cmatrix matrix)
Definition: matrix.cpp:1474
rvector rv_mmult(rmatrix a, rvector b)
Multiply rmatrix by rvector.
Definition: matrix.cpp:41
matrix1d m2_unskew(matrix2d matrix)
Unskew 3x3 2D matrix.
Definition: matrix.cpp:1384
std::istream & operator>>(std::istream &out, rmatrix &a)
Definition: matrix.cpp:1648
rvector row[3]
Definition: matrix.h:43
matrix1d m1_add(matrix1d row1, matrix1d row2)
Add one 1D matrix to another.
Definition: matrix.cpp:1000
matrix1d m1_zero(uint16_t cols)
Fill 1D matrix with zeros.
Definition: matrix.cpp:959
matrix1d m1_sub(matrix1d row1, matrix1d row2)
Subtract one 1D matrix from another.
Definition: matrix.cpp:1021
double m2_snorm2x2(matrix2d matrix)
Spectral norm of a 2x2 matrix.
Definition: matrix.cpp:1607
double m2_determinant(matrix2d m)
Determinant of a 2D matrix.
Definition: matrix.cpp:1189
cvector r3
Row 3.
Definition: matrix.h:103
3 element cartesian vector
Definition: vector.h:107
matrix2d m2_from_rm(rmatrix matrix)
rmatrix from rmatrix
Definition: matrix.cpp:1453
matrix2d m2_add(matrix2d matrix1, matrix2d matrix2)
Add one matrix2d to another.
Definition: matrix.cpp:1304
cvector r2
Row 2.
Definition: matrix.h:101
rmatrix rm_from_m2(matrix2d m)
rmatrix from matrix2d
Definition: matrix.cpp:921