Skip to content

Commit

Permalink
Add asserts (#115)
Browse files Browse the repository at this point in the history
* Add asserts

* Type cast literals

* asserts for indexing vectors

* include assert

* Fix accessing elements outside of slice
  • Loading branch information
kamilritz authored and Julian Kent committed Dec 9, 2019
1 parent 2f63981 commit 4f3565d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type" FORCE)
if(TESTING)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE)
else()
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type" FORCE)
endif()
message(STATUS "set build type to ${CMAKE_BUILD_TYPE}")
endif()

Expand Down
20 changes: 20 additions & 0 deletions matrix/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,21 @@ class Matrix

inline Type operator()(size_t i, size_t j) const
{
assert(i >= 0);
assert(i < M);
assert(j >= 0);
assert(j < N);

return _data[i][j];
}

inline Type &operator()(size_t i, size_t j)
{
assert(i >= 0);
assert(i < M);
assert(j >= 0);
assert(j < N);

return _data[i][j];
}

Expand Down Expand Up @@ -470,6 +480,11 @@ class Matrix

inline void swapRows(size_t a, size_t b)
{
assert(a >= 0);
assert(a < M);
assert(b >= 0);
assert(b < M);

if (a == b) {
return;
}
Expand All @@ -485,6 +500,11 @@ class Matrix

inline void swapCols(size_t a, size_t b)
{
assert(a >= 0);
assert(a < N);
assert(b >= 0);
assert(b < N);

if (a == b) {
return;
}
Expand Down
27 changes: 21 additions & 6 deletions matrix/Slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,30 @@ class Slice {
_data(const_cast<Matrix<Type, M, N>*>(data)) {
static_assert(P <= M, "Slice rows bigger than backing matrix");
static_assert(Q <= N, "Slice cols bigger than backing matrix");
assert(x0 >= 0);
assert(x0 + P <= M);
assert(y0 >= 0);
assert(y0 + Q <= N);
}

Type operator()(size_t i, size_t j) const
{
assert(i >= 0);
assert(i < P);
assert(j >= 0);
assert(j < Q);

return (*_data)(_x0 + i, _y0 + j);
}

Type &operator()(size_t i, size_t j)

{
assert(i >= 0);
assert(i < P);
assert(j >= 0);
assert(j < Q);

return (*_data)(_x0 + i, _y0 + j);
}

Expand Down Expand Up @@ -97,23 +112,23 @@ class Slice {
return Slice<Type, R, S, M, N>(x0 + _x0, y0 + _y0, _data);
}

void copyTo(Type dst[M*N]) const
void copyTo(Type dst[P*Q]) const
{
const Slice<Type, P, Q, M, N> &self = *this;

for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
for (size_t i = 0; i < P; i++) {
for (size_t j = 0; j < Q; j++) {
dst[i*N+j] = self(i, j);
}
}
}

void copyToColumnMajor(Type dst[M*N]) const
void copyToColumnMajor(Type dst[P*Q]) const
{
const Slice<Type, P, Q, M, N> &self = *this;

for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
for (size_t i = 0; i < P; i++) {
for (size_t j = 0; j < Q; j++) {
dst[i+(j*M)] = self(i, j);
}
}
Expand Down
40 changes: 34 additions & 6 deletions matrix/SquareMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
void uncorrelateCovariance(size_t first)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
Vector<Type, Width> diag_elements = self.slice<Width, Width>(first, first).diag();
self.uncorrelateCovarianceSetVariance(first, diag_elements);
Expand All @@ -143,10 +147,14 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
void uncorrelateCovarianceSetVariance(size_t first, const Vector<Type, Width> &vec)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
// zero rows and columns
self.slice<Width, M>(first, 0) = 0;
self.slice<M, Width>(0, first) = 0;
self.slice<Width, M>(first, 0) = Type(0);
self.slice<M, Width>(0, first) = Type(0);

// set diagonals
unsigned vec_idx = 0;
Expand All @@ -159,10 +167,14 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
void uncorrelateCovarianceSetVariance(size_t first, Type val)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
// zero rows and columns
self.slice<Width, M>(first, 0) = 0;
self.slice<M, Width>(0, first) = 0;
self.slice<Width, M>(first, 0) = Type(0);
self.slice<M, Width>(0, first) = Type(0);

// set diagonals
for (size_t idx = first; idx < first+Width; idx++) {
Expand All @@ -174,11 +186,15 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
void makeBlockSymmetric(size_t first)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
if(Width>1) {
for (size_t row_idx = first+1; row_idx < first+Width; row_idx++) {
for (size_t col_idx = first; col_idx < row_idx; col_idx++) {
Type tmp = self(row_idx,col_idx) + (self(col_idx,row_idx) - self(row_idx,col_idx)) / 2;
Type tmp = self(row_idx,col_idx) + (self(col_idx,row_idx) - self(row_idx,col_idx)) / Type(2);
self(row_idx,col_idx) = tmp;
self(col_idx,row_idx) = tmp;
}
Expand All @@ -190,11 +206,15 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
void makeRowColSymmetric(size_t first)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
self.makeBlockSymmetric<Width>(first);
for (size_t row_idx = first; row_idx < first+Width; row_idx++) {
for (size_t col_idx = 0; col_idx < first; col_idx++) {
Type tmp = self(row_idx,col_idx) + (self(col_idx,row_idx) - self(row_idx,col_idx)) / 2;
Type tmp = self(row_idx,col_idx) + (self(col_idx,row_idx) - self(row_idx,col_idx)) / Type(2);
self(row_idx,col_idx) = tmp;
self(col_idx,row_idx) = tmp;
}
Expand All @@ -210,6 +230,10 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
bool isBlockSymmetric(size_t first, const Type eps = 1e-8f)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
if(Width>1) {
for (size_t row_idx = first+1; row_idx < first+Width; row_idx++) {
Expand All @@ -227,6 +251,10 @@ class SquareMatrix : public Matrix<Type, M, M>
template <size_t Width>
bool isRowColSymmetric(size_t first, const Type eps = 1e-8f)
{
static_assert(Width <= M, "Width bigger than matrix");
assert(first >= 0);
assert(first + Width <= M);

SquareMatrix<Type, M> &self = *this;
for (size_t row_idx = first; row_idx < first+Width; row_idx++) {
for (size_t col_idx = 0; col_idx < first; col_idx++) {
Expand Down
6 changes: 6 additions & 0 deletions matrix/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ class Vector : public Matrix<Type, M, 1>

inline Type operator()(size_t i) const
{
assert(i >= 0);
assert(i < M);

const MatrixM1 &v = *this;
return v(i, 0);
}

inline Type &operator()(size_t i)
{
assert(i >= 0);
assert(i < M);

MatrixM1 &v = *this;
return v(i, 0);
}
Expand Down
1 change: 1 addition & 0 deletions matrix/math.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <assert.h>
#include "stdlib_imports.hpp"
#ifdef __PX4_QURT
#include "dspal_math.h"
Expand Down

0 comments on commit 4f3565d

Please sign in to comment.