-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement equality and non-equality comparisons for matrices
Closes #14
- Loading branch information
1 parent
623335e
commit c7c5ebd
Showing
5 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* -*- C++ -*- ------------------------------------------------------------ | ||
@@COPYRIGHT@@ | ||
*-----------------------------------------------------------------------*/ | ||
/** @file | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef cml_matrix_comparison_h | ||
#define cml_matrix_comparison_h | ||
|
||
#include <cml/matrix/readable_matrix.h> | ||
|
||
namespace cml { | ||
|
||
/** Returns true if the elements of @c left are all equal to the elements | ||
* of @c right. | ||
*/ | ||
template<class Sub1, class Sub2> bool operator==( | ||
const readable_matrix<Sub1>& left, const readable_matrix<Sub2>& right); | ||
|
||
/** Returns true if some element of @c left is not equal to the same element | ||
* of @c right. | ||
*/ | ||
template<class Sub1, class Sub2> bool operator!=( | ||
const readable_matrix<Sub1>& left, const readable_matrix<Sub2>& right); | ||
|
||
} // namespace cml | ||
|
||
#define __CML_MATRIX_COMPARISON_TPP | ||
#include <cml/matrix/comparison.tpp> | ||
#undef __CML_MATRIX_COMPARISON_TPP | ||
|
||
#endif | ||
|
||
// ------------------------------------------------------------------------- | ||
// vim:ft=cpp:sw=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* -*- C++ -*- ------------------------------------------------------------ | ||
@@COPYRIGHT@@ | ||
*-----------------------------------------------------------------------*/ | ||
/** @file | ||
*/ | ||
|
||
#ifndef __CML_MATRIX_COMPARISON_TPP | ||
#error "matrix/comparison.tpp not included correctly" | ||
#endif | ||
|
||
#include <cml/matrix/size_checking.h> | ||
|
||
namespace cml { | ||
|
||
template<class Sub1, class Sub2> inline bool operator==( | ||
const readable_matrix<Sub1>& left, const readable_matrix<Sub2>& right | ||
) | ||
{ | ||
/* Possibly equal only if the same dimensions: */ | ||
if(left.size() != right.size()) return false; | ||
for(int i = 0; i < left.rows(); i ++) { | ||
for(int j = 0; j < left.cols(); j ++) { | ||
/**/ if(left(i, j) < right(i, j)) return false; // Strictly less. | ||
else if(right(i, j) < left(i, j)) return false; // Strictly greater. | ||
else continue; // Possibly equal. | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
template<class Sub1, class Sub2> inline bool operator!=( | ||
const readable_matrix<Sub1>& left, const readable_matrix<Sub2>& right | ||
) | ||
{ | ||
return !(left == right); | ||
} | ||
|
||
} // namespace cml | ||
|
||
// ------------------------------------------------------------------------- | ||
// vim:ft=cpp:sw=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* -*- C++ -*- ------------------------------------------------------------ | ||
@@COPYRIGHT@@ | ||
*-----------------------------------------------------------------------*/ | ||
/** @file | ||
*/ | ||
|
||
// Make sure the main header compiles cleanly: | ||
#include <cml/matrix/comparison.h> | ||
|
||
#include <cml/matrix/fixed.h> | ||
#include <cml/matrix/types.h> | ||
|
||
// For Catch: | ||
#include <cml/util/matrix_print.h> | ||
|
||
/* Testing headers: */ | ||
#include "catch_runner.h" | ||
|
||
CATCH_TEST_CASE("equal1") | ||
{ | ||
auto M = cml::matrix33d( | ||
1., 2., 3., | ||
1., 4., 9., | ||
1., 16., 25. | ||
); | ||
|
||
M.transpose(); | ||
auto expected = cml::matrix33d( | ||
1., 1., 1., | ||
2., 4., 16., | ||
3., 9., 25. | ||
); | ||
|
||
CATCH_CHECK(M == expected); | ||
} | ||
|
||
CATCH_TEST_CASE("not_equal1") | ||
{ | ||
auto M = cml::matrix33d( | ||
1., 2., 3., | ||
1., 4., 9., | ||
1., 16., 25. | ||
); | ||
|
||
M.transpose(); | ||
auto expected = cml::matrix33d( | ||
1., 1., 1., | ||
2., 4., 16., | ||
3., 9., 24. | ||
); | ||
|
||
CATCH_CHECK(M != expected); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// vim:ft=cpp:sw=2 |