EMatrix
Functions
test_EMatrix.cpp File Reference
#include <iostream>
#include "EMatrix.h"
Include dependency graph for test_EMatrix.cpp:

Go to the source code of this file.

Functions

int main (void)
 

Detailed Description

This file is part of EMatrix, the C++ matrix library distribution. This project is licensed under the terms of the MIT license. The full text of the license file can be found in LICENSE.

Definition in file test_EMatrix.cpp.

Function Documentation

◆ main()

int main ( void  )

Begin and the very beginning.

Definition at line 15 of file test_EMatrix.cpp.

References ematrix::Matrix< tData, tRows, tCols >::cols(), cout_octave, ematrix::cross(), ematrix::det(), ematrix::diag(), ematrix::dot(), ematrix::Matrix< tData, tRows, tCols >::eye(), ematrix::inv(), ematrix::Matrix< tData, tRows, tCols >::load(), ematrix::Matrix< tData, tRows, tCols >::n(), ematrix::norm(), ematrix::octave(), ematrix::Matrix< tData, tRows, tCols >::ones(), ematrix::Matrix< tData, tRows, tCols >::pIJ(), ematrix::Matrix< tData, tRows, tCols >::randn(), ematrix::Matrix< tData, tRows, tCols >::rows(), ematrix::skew(), ematrix::trans(), ematrix::Matrix< tData, tRows, tCols >::u(), and ematrix::Matrix< tData, tRows, tCols >::zeros().

15  {
16 //! Begin and the very beginning.
17 
18  // Scoped to test [virtual ~Matrix ()], used gdb and cerr to verify
19  {
20  // Matrix ();
21  // void matalloc (size_t iRowIndex, size_t iColIndex);
23 
24  // These invoke compiler errors
25  //Matrix<double,0,0> A;
26  //Matrix<double,0,1> A;
27  //Matrix<double,1,0> A;
28  }
29 
30  {
31  // inline Matrix (const Matrix< tData, tRows, tCols > & R);
32  // std::ostream& operator << (std::ostream& s,const Matrix< tData, tRows, tCols >& A)
33  float a[2][3] = {{1.0,2.0,3.0},{4.0,5.0,6.0}};
34  Matrix<float,2,3> A(&a[0][0]);
36  B = B+B+A;
37  // Matrix<float,3,2> B=A; // Compiler error
38  cerr << B+B+A << endl;
39  }
40 
41  {
42  // Matrix (tData* tArray);
43  float a[2][3] = {{1.0,2.0,3.0},{4.0,5.0,6.0}};
44  Matrix<float,2,3> A(&a[0][0]);
45  cerr << A << endl;;
46  }
47 
48  {
49  // Matrix (const std::initializer_list<tData>& l);
50  Matrix<double,3,3> A = {1.,2.,3.,0.,0.,0.,0,0,0};
51  cerr << A << endl;;
52 
53  // Assertion fail, too many elements
54  //Matrix<double,3,3> B = {1.,2.,3.,0.,0.,0.,0,0,0,0};
55  // Assertion fail, too few elements
56  //Matrix<double,3,3> B = {1.,2.,3.,0.,0.,0.,0,0};
57  }
58 
59  /* Deprecated but tested
60  { // Matrix (const std::initializer_list<tData>& l);
61  Matrix<double, 1, 2 > X(2.81,3.14);
62  cerr << X;
63  Matrix<double, 1, 3 > Y(2.81,3.14,FLT_MAX);
64  cerr << Y;
65  }
66  */
67 
68  {
69  // inline Matrix (const Matrix< tData, tRows, tCols > & R);
70  Matrix<double,3,3> A = {1.2,2.2,3.2,4.,5.,0.,0,0,0};
73  B=A;
74  cerr << B << endl;
75  B=B;
76  cerr << B << endl;
77  // B=C; // Compiler error
78  }
79 
80  {
81  // inline const Matrix< tData, tRows, tCols > &operator = (const std::initializer_list<tData>& l);
83  A = {1.1,2.1,3.1,0.0,0.0,0.0};
84  cerr << A << endl;
85  // Assertion fail, too many elements
87  //B = {1.,2.,3.,0.,0.,0.,0,0,0,0};
88  // Assertion fail, too few elements
89  //B = {1.,2.,3.,0.,0.,0.,0,0};
90  }
91 
92  {
93  // load(tData* tArray)
94  double a[2][3] = {{1.1,2.1,3.1},{4.0,5.0,6.0}};
96  A.load(&a[0][0]);
97  cerr << A << endl;
98  }
99 
100  {
101  // inline tData * operator [] (size_t iRowIndex) const
102  // inline tData & operator () (size_t iRowIndex, size_t iColIndex) const;
103  // inline tData & operator () (size_t iIndex) const;
104  Matrix<double,3,2> A = {1,2,3,4,5,6};
105  A[0][0] = 7;
106  cerr << A[2][1] << endl;
107  cerr << A << endl;
108  // A[10][0] = 7; // assertion fail,
109  // cerr << A[11][2] << endl;
110  // cerr << A[1][12] << endl; // Not safe for col.
111 
112  A(1,1) = 8;
113  cerr << A(3,2) << endl;
114  cerr << A << endl;
115 
116  //cerr << A(1,12) << endl; // assertion fail,
117  //cerr << A(12,1) << endl;
118  //cerr << A(0,1) << endl;
119  //cerr << A(1,0) << endl;
120  //A(1,12) = 3.14;
121  //A(12,1) = 3.14;
122  //A(0, 1) = 3.14;
123  //A(1,-1) = 3.14;
124 
125  Matrix<double,6,1> V = {1,2,3,4,5,6};
126  V(1) = 8;
127  cerr << V << endl;
128  cerr << V(6) << endl;
129 
130  //V(12) = 3.14;// assertion fail,
131  //cerr << V(12) << endl;
132  //V(0) = 3.14;
133  //V << V(0) << endl;
134 
135  Matrix<double,1,6> U = {1,2,3,4,5,6};
136  U(1) = 8;
137  cerr << U << endl;
138  cerr << U(6) << endl;
139 
140  //U(12) = 3.14;// assertion fail,
141  //cerr << U(12) << endl;
142  //U(0) = 3.14;
143  //cerr << U(0) << endl;
144  }
145 
146  /* Deprecated but tested
147  { // inline ListInit<tData, tData* > operator = (tData x) {
148  Matrix<double,1,3> A;
149  A = 1.1,2.1;
150  cerr << A;
151  A = 1.1,2.1,3.1;
152  cerr << A;
153  A = 1.1,2.1,3.1;
154  cerr << A;
155  */
156 
157  {
158  // friend std::ostream& octave (std::ostream& s, const Matrix< std::complex<double> , tRows0, tCols0 >& A, const char* Aname) {
159  // friend std::ostream& octave (std::ostream& s, const Matrix< double , tRows0, tCols0 >& A, const char* Aname) {
160  double a[2][3] = {{1.40,2.40,3.40},{4.40,5.40,6.40}};
161  Matrix<double,2,3> A(&a[0][0]);
162 
163  cout_octave(A) << endl;
164 
165  Matrix< complex<double>,3,3 > ZA;
166  ZA[0][0] = complex<double>( 3.1,-1.8);
167  ZA[0][1] = complex<double>( 1.3, 0.2);
168  ZA[0][2] = complex<double>(-5.7,-4.3);
169  ZA[1][0] = complex<double>( 1.0, 0.0);
170  ZA[1][1] = complex<double>(-6.9, 3.2);
171  ZA[1][2] = complex<double>( 5.8, 2.2);
172  ZA[2][0] = complex<double>( 3.4,-4.0);
173  ZA[2][1] = complex<double>( 7.2, 2.9);
174  ZA[2][2] = complex<double>(-8.8, 3.2);
175  octave(cerr,ZA,"za") << endl;
176  Matrix< complex<double>,3,3 > ZAinv = inv(ZA);
177  //octave(cout,ZA,"za") << endl;
178  //cerr << trans(ZA) << endl;
179  //cerr << ZAinv << endl;
180  //cerr << det(ZA) << endl;
181  //cerr << det(trans(ZA)) << endl;
182  }
183 
184  {
185  // std::ostream& operator << (std::ostream& s,const Matrix< tData, tRows, tCols >& A)
186  // inline tData *pIJ (void) const {
187  float a[2][3] = {{1.40f,2.40f,3.40f},{4.40f,5.40f,6.40f}};
188  Matrix<float,2,3> A(&a[0][0]);
189  Matrix<float,2,3> B=A;
190  // Matrix<float,3,2> B=A; // Compiler error
191  cerr << "p " << cout.precision(10) << endl;
192  cerr << B << endl;
193 
194  float* ptr = A.pIJ();
195  cerr << ptr[4] << endl;
196  cerr << "rows = " << A.rows() << endl;
197  cerr << "cols = " << A.cols() << endl;
198  }
199 
200  {
201  // bool operator == (Matrix< tData, tRows, tCols > & R);
202  // bool operator != (Matrix< tData, tRows, tCols > & R);
203  // Matrix< tData, tRows, tCols > operator + ();
204  // Matrix< tData, tRows, tCols > operator - ();
205  // Matrix< tData, tRows, tCols > operator + (const Matrix< tData, tRows, tCols > & R)
206  // Matrix< tData, tRows, tCols > operator - (const Matrix< tData, tRows, tCols > & R)
207  Matrix<int,2,2> A = {1,2,3,4};
208  Matrix<int,2,2> B = {1,2,3,4};
209 
210  if( A == B ) cerr << "( A == B )" << endl;
211  else cerr << "( A != B )" << endl;
212  B(2,2) = 1;
213  if( A == B ) cerr << "( A == B )" << endl;
214  else cerr << "( A != B )" << endl;
215 
216  B(2,2) = 4;
217  if( A != B ) cerr << "( A != B )" << endl;
218  else cerr << "( A == B )" << endl;
219  B(2,2) = 1;
220  if( A != B ) cerr << "( A != B )" << endl;
221  else cerr << "( A == B )" << endl;
222 
223  Matrix<float,2,2> C = {1,2,3,4};
224  Matrix<float,2,2> D = (+C);
225 
226  if (C == D) cerr << "(C == D)" << endl;
227  else cerr << "(C != D)" << endl;
228 
229  cerr << -C + D << endl;
230  cerr << C - D << endl;
231 
232  }
233 
234  {
235  // Matrix< tData, tRows, tCols > operator * (const tData & scalar);
236  // friend Matrix< tData, tRows, tCols > operator * (const tData & scalar,const Matrix< tData, tRows, tCols > & R);
237  // Matrix< tData, tRows, tCols > operator / (const tData & scalar);
238  // Matrix< tData, tRows, tColsR > operator * (const Matrix< tData, tCols, tColsR >& R)
239 
240  Matrix<double,3,2> A = {1,2,3,4,5,6};
241  cerr << A*(-1.0) << endl;
242  cerr << -2.0*A << endl;
243  cerr << A/2.0 << endl;
244  // cerr << A/0.0 << endl; // assertion error
245 
246  Matrix<double,2,3> B = {1,2,3,4,5,6};
247  Matrix<double,2,3> C = {1,2,3,4,5,6};
248 
249  cerr << A*B << endl;
250  cerr << B*A << endl;
251  // cerr << B*C << endl; // compiler error
252  }
253 
254  {
255  // Matrix< tData, tRows, tCols > operator *= (const Matrix< tData, tRows, tCols > & R);
256  // Matrix< tData, tRows, tCols > operator /= (const Matrix< tData, tRows, tCols > & R);
257 
258  Matrix<double,2,3> B = {1,2,3,4,5,6};
259  Matrix<double,2,3> C = {1,2,3,4,5,6};
260  Matrix<double,3,2> D = {1,2,3,4,5,6};
261  cerr << (B*=C) << endl;
262  cerr << (B/=C) << endl;
263  //C(1,1) = 0;
264  //cout << (B/=C) << endl; // Divide by zero error.
265  //cout << (B*=D) << endl; // Compiler error
266  //cout << (B/=D) << endl; // Compiler error
267 
268  }
269 
270  {
271  // friend Matrix< tData0, tRowsT+tRowsB, tCols0 > operator & (const Matrix< tData0, tRowsT, tCols0 >& Top,
272  // const Matrix< tData0, tRowsB, tCols0 >& Bottom);
273  // friend Matrix< tData0, tRows0, tColsL+tColsR > operator | (const Matrix< tData0, tRows0, tColsL >& Left,
274  // const Matrix< tData0, tRows0, tColsR >& Right);
275 
276  Matrix<double,2,3> B = {1,2,3,4,5,6};
277  Matrix<double,2,3> C = {1,2,3,4,5,6};
278 
279  Matrix<double,4,3> D = (B&C);
280  Matrix<double,2,2> E = {1,2,3,4};
281 
282  cerr << (B&C) << endl;
283  cerr << (B|C) << endl;
284  cerr << (D & D) << endl;
285 
286  // D|B; compiler
287  // B|D; compiler
288  // B&E; compiler
289  // E&B; compiler
290  }
291 
292  {
293  // Matrix< tData, tRows, tCols > zeros( void );
294  // Matrix< tData, tRows, tCols > ones( void );
295  // Matrix< tData, tRows, tCols > eye( void );
296  // Matrix< tData, tRows, tCols > randn(void);
297 
298  Matrix<double,2,2> A = {1,2,3,4};
299  cerr << A << endl;
300  cerr << A.eye() << endl;
301 
302  // Matrix<double,3,2> B;
303  // cout << B.eye() << endl; // assertion error
304 
305  cerr << A.zeros() << endl;
306  cerr << A.ones() << endl;
308  cerr << n.randn() << endl;
309  }
310 
311  {
312  Matrix<double,2,3> B = {1,2,3,4,5,6};
313 
314  cerr << B << endl;
315  cerr << trans(B) << endl;
316 
317  Matrix< complex<double>,3,2> ZA;
318  ZA[0][0] = complex<double>( 3.1,-1.8);
319  ZA[0][1] = complex<double>( 1.3, 0.2);
320  ZA[0][2] = complex<double>(-5.7,-4.3);
321  ZA[1][0] = complex<double>( 1.0, 0.0);
322  ZA[1][1] = complex<double>(-6.9, 3.2);
323  ZA[1][2] = complex<double>( 5.8, 2.2);
324  cout_octave(ZA) << endl;
325  cout_octave(trans(ZA)) << endl;
326 
327  Matrix< complex<float>,3,2> ZB;
328  ZB[0][0] = complex<float>( 3.1f,-1.8f);
329  ZB[0][1] = complex<float>( 1.3f, 0.2f);
330  ZB[0][2] = complex<float>(-5.7f,-4.3f);
331  ZB[1][0] = complex<float>( 1.0f, 0.0f);
332  ZB[1][1] = complex<float>(-6.9f, 3.2f);
333  ZB[1][2] = complex<float>( 5.8f, 2.2f);
334  cout << ZB << endl;
335  cout << trans(ZB) << endl;
336 
337  }
338 
339  {
340  // friend Matrix< tData0, tRows0, 1 > diag( const Matrix< tData0, tRows0, tRows0 >& R ); // APD: tested
341  // friend Matrix< tData0, tRows0, tRows0 > diag( const Matrix< tData0, tRows0, 1 >& R ); // APD: tested
342  // friend Matrix< tData0, tCols0, tCols0 > diag( const Matrix< tData0, 1, tCols0 >& R ); // APD: tested
343 
344  Matrix<double,1,6> A = {1,2,3,4,5,6};
345  Matrix<double,6,1> B = {1,2,3,4,5,6};
346  Matrix<double,2,2> C = {1,2,3,4};
347  cerr << (diag(C)) << endl;
348 
349  cerr << diag(A) << endl;
350  cerr << diag(B) << endl;
351  }
352 
353  {
354  // friend Matrix< tData0, 3, 3 > skew( const Matrix< tData0, 3, 1 >& R );
355  // friend Matrix< tData0, 3, 1 > cross( const Matrix< tData0, 3, 1 >& L, const Matrix< tData0, 3, 1 >& R );
356  Matrix<double,3,1> B = {2,3,4};
357  Matrix<double,3,1> C = {1,2,3};
358  cerr << skew(C) << endl;
359 
360  Matrix<double,3,1> x = {1,0,0};
361  Matrix<double,3,1> y = {0,1,0};
362  cerr << cross(x,y) << endl;
363 
364  cerr << dot(B,C) << endl;
365 
366  cerr << B.n() << endl;
367  cerr << norm(B) << endl;
368 
369  cerr << B.u() << endl;
370  cerr << norm(B.u()) << endl;
371 
372  //Matrix<double,2,3> A = {1,2,3,4,5,6};
373  //cerr << A.n() << endl;
374  }
375 
376  {
377  // friend std::ostream& octave (std::ostream& s, const Matrix< std::complex<double> , tRows0, tCols0 >& A, const char* Aname) {
378  // friend std::ostream& octave (std::ostream& s, const Matrix< double , tRows0, tCols0 >& A, const char* Aname) {
379  double a[2][3] = {{1.40,2.40,3.40},{4.40,5.40,6.40}};
380  Matrix<double,2,3> A(&a[0][0]);
381 
382  cout_octave(A) << endl;
383 
384  Matrix< complex<double>,3,3 > ZA;
385  ZA[0][0] = complex<double>( 3.1,-1.8);
386  ZA[0][1] = complex<double>( 1.3, 0.2);
387  ZA[0][2] = complex<double>(-5.7,-4.3);
388  ZA[1][0] = complex<double>( 1.0, 0.0);
389  ZA[1][1] = complex<double>(-6.9, 3.2);
390  ZA[1][2] = complex<double>( 5.8, 2.2);
391  ZA[2][0] = complex<double>( 3.4,-4.0);
392  ZA[2][1] = complex<double>( 7.2, 2.9);
393  ZA[2][2] = complex<double>(-8.8, 3.2);
394  octave(cout,ZA,"za") << endl;
395  Matrix< complex<double>,3,3 > ZAinv = inv(ZA);
396  octave(cout,ZAinv,"za") << endl;
397 
398  cerr << det(ZA) << endl;
399  cerr << det(trans(ZA)) << endl;
400  }
401 
402  {
403  // friend std::ostream& octave (std::ostream& s, const Matrix< std::complex<double> , tRows0, tCols0 >& A, const char* Aname) {
404  // friend std::ostream& octave (std::ostream& s, const Matrix< double , tRows0, tCols0 >& A, const char* Aname) {
405  float a[2][3] = {{1.40f,2.40f,3.40f},{4.40f,5.40f,6.40f}};
406  Matrix<float,2,3> A(&a[0][0]);
407 
408  cout_octave(A) << endl;
409 
410  Matrix< complex<float>,3,3 > ZC;
411  ZC[0][0] = complex<float>( 3.1f,-1.8f);
412  ZC[0][1] = complex<float>( 1.3f, 0.2f);
413  ZC[0][2] = complex<float>(-5.7f,-4.3f);
414  ZC[1][0] = complex<float>( 1.0f, 0.0f);
415  ZC[1][1] = complex<float>(-6.9f, 3.2f);
416  ZC[1][2] = complex<float>( 5.8f, 2.2f);
417  ZC[2][0] = complex<float>( 3.4f,-4.0f);
418  ZC[2][1] = complex<float>( 7.2f, 2.9f);
419  ZC[2][2] = complex<float>(-8.8f, 3.2f);
420  octave(cout,ZC,"zc") << endl;
421  Matrix< complex<float>,3,3 > ZCinv = inv(ZC);
422  octave(cout,ZCinv,"zc") << endl;
423 
424  cerr << det(ZC) << endl;
425  cerr << det(trans(ZC)) << endl;
426  }
427 
428  {
429  Matrix< double, 3, 3 > A = {1, 1, 1, 2, 3, 4, 1, 3, 6};
430  Matrix< double, 3, 3 > B = {6, -3, 1, -8, 5, -2, 3, -2, 1};
431  cerr << inv(A) << endl;
432  cerr << inv(B) << endl;
433  cerr << det(A) << endl;
434  }
435 
436  {
437  Matrix< float, 3, 3 > A = {1, 1, 1, 2, 3, 4, 1, 3, 6};
438  Matrix< float, 3, 3 > B = {6, -3, 1, -8, 5, -2, 3, -2, 1};
439  cerr << inv(A) << endl;
440  cerr << inv(B) << endl;
441  cerr << det(A) << endl;
442 
443  //float a[2][3] = {{1.40,2.40,3.40},{4.40,5.40,6.40}};
444  //Matrix<float,2,3> C(&a[0][0]);
445  //cerr << det(C) << endl; // assertion error
446  }
447 
448  cerr << "Done!" << endl;
449 
450  return(0);
451 }
Matrix< tData, tRows, tCols > ones(void)
Definition: EMatrix.h:694
std::ostream & octave(std::ostream &s, const Matrix< std::complex< tData0 >, tRows0, tCols0 > &A, const char *Aname)
Definition: EMatrix.h:520
tData dot(const Matrix< tData, tRows, 1 > &L, const Matrix< tData, tRows, 1 > &R)
Definition: EMatrix.h:801
tData n(void) const
Definition: EMatrix.h:814
Matrix< tData, tRows, tCols > eye(void)
Definition: EMatrix.h:702
#define cout_octave(x)
Definition: EMatrix.h:26
Matrix< tData, 3, 1 > cross(const Matrix< tData, 3, 1 > &L, const Matrix< tData, 3, 1 > &R)
Definition: EMatrix.h:796
Matrix< tData, tRows, 1 > u(void) const
Definition: EMatrix.h:824
float det(const Matrix< float, tRows, tRows > &R)
Definition: EMatrix.h:926
Matrix< tData, tRows, tCols > zeros(void)
Definition: EMatrix.h:688
Matrix< float, tRows, tRows > inv(const Matrix< float, tRows, tRows > &R)
Definition: EMatrix.h:841
Matrix< tData, tRows, 1 > diag(const Matrix< tData, tRows, tRows > &R)
Definition: EMatrix.h:756
Matrix< tData, 3, 3 > skew(const Matrix< tData, 3, 1 > &R)
Definition: EMatrix.h:783
Matrix< tData, tRows, tCols > randn(void)
Definition: EMatrix.h:713
tData * pIJ(void) const
Definition: EMatrix.h:171
void load(const tData *tArray)
Definition: EMatrix.h:472
Matrix< tData, tCols, tRows > trans(const Matrix< tData, tRows, tCols > &R)
Definition: EMatrix.h:723
tData norm(const Matrix< tData, tRows, 1 > &R)
Definition: EMatrix.h:819