CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
FloatMatrix.h
1 #ifndef __FLOAT_MATRIX_H__
2 #define __FLOAT_MATRIX_H__
3 
4 /*LICENSE_START*/
5 /*
6  * Copyright (c) 2014, Washington University School of Medicine
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <vector>
32 #include "stdint.h"
33 #include "Vector3D.h"
34 
35 namespace cifti {
36 
38  {//needed to do [][] on a const FloatMatrix
39  const std::vector<float>& m_row;
40  ConstFloatMatrixRowRef();//disallow default construction, this contains a reference
41  public:
42  ConstFloatMatrixRowRef(const ConstFloatMatrixRowRef& right);//copy constructor
43  ConstFloatMatrixRowRef(const std::vector<float>& therow);
44  const float& operator[](const int64_t& index);//access element
45  friend class FloatMatrixRowRef;//so it can check if it points to the same row
46  };
47 
49  {//needed to ensure some joker doesn't call mymatrix[1].resize();, while still allowing mymatrix[1][2] = 5; and mymatrix[1] = mymatrix[2];
50  std::vector<float>& m_row;
51  FloatMatrixRowRef();//disallow default construction, this contains a reference
52  public:
53  FloatMatrixRowRef(FloatMatrixRowRef& right);//copy constructor
54  FloatMatrixRowRef(std::vector<float>& therow);
55  FloatMatrixRowRef& operator=(const FloatMatrixRowRef& right);//NOTE: copy row contents!
56  FloatMatrixRowRef& operator=(const ConstFloatMatrixRowRef& right);//NOTE: copy row contents!
57  FloatMatrixRowRef& operator=(const float& right);//NOTE: set all row values!
58  float& operator[](const int64_t& index);//access element
59  };
60 
64  {
65  std::vector<std::vector<float> > m_matrix;
66  bool checkDimensions() const;//put this inside asserts at the end of functions
67  public:
68  FloatMatrix() { };//to make the compiler happy
70  FloatMatrix(const std::vector<std::vector<float> >& matrixIn);
72  FloatMatrix(const int64_t& rows, const int64_t& cols);
73  FloatMatrixRowRef operator[](const int64_t& index);//allow direct indexing to rows
74  ConstFloatMatrixRowRef operator[](const int64_t& index) const;//allow direct indexing to rows while const
75  FloatMatrix& operator+=(const FloatMatrix& right);//add to
76  FloatMatrix& operator-=(const FloatMatrix& right);//subtract from
77  FloatMatrix& operator*=(const FloatMatrix& right);//multiply by
78  FloatMatrix& operator+=(const float& right);//add scalar to
79  FloatMatrix& operator-=(const float& right);//subtract scalar from
80  FloatMatrix& operator*=(const float& right);//multiply by scalar
81  FloatMatrix& operator/=(const float& right);//divide by scalar
82  FloatMatrix operator+(const FloatMatrix& right) const;//add
83  FloatMatrix operator-(const FloatMatrix& right) const;//subtract
84  FloatMatrix operator-() const;//negate
85  FloatMatrix operator*(const FloatMatrix& right) const;//multiply
86  bool operator==(const FloatMatrix& right) const;//compare
87  bool operator!=(const FloatMatrix& right) const;//anti-compare
89  FloatMatrix inverse() const;
93  FloatMatrix transpose() const;
95  void resize(const int64_t rows, const int64_t cols, const bool destructive = false);
97  static FloatMatrix zeros(const int64_t rows, const int64_t cols);
99  static FloatMatrix ones(const int64_t rows, const int64_t cols);
101  static FloatMatrix identity(const int64_t rows);
103  FloatMatrix getRange(const int64_t firstRow, const int64_t afterLastRow, const int64_t firstCol, const int64_t afterLastCol) const;
105  FloatMatrix concatHoriz(const FloatMatrix& right) const;
107  FloatMatrix concatVert(const FloatMatrix& bottom) const;
109  void getDimensions(int64_t& rows, int64_t& cols) const;
111  const std::vector<std::vector<float> >& getMatrix() const;
113  void getAffineVectors(Vector3D& xvec, Vector3D& yvec, Vector3D& zvec, Vector3D& offset) const;
115  int64_t getNumberOfRows() { return (int64_t)m_matrix.size(); }
118  {
119  if (m_matrix.size() == 0) return 0;
120  return (int64_t)m_matrix[0].size();
121  }
122  };
123 
124 }
125 
126 #endif //__FLOAT_MATRIX_H__
cifti::FloatMatrix::getMatrix
const std::vector< std::vector< float > > & getMatrix() const
get the matrix as a vector<vector>
Definition: FloatMatrix.cxx:255
cifti::FloatMatrix::getDimensions
void getDimensions(int64_t &rows, int64_t &cols) const
get the dimensions
Definition: FloatMatrix.cxx:197
cifti::Vector3D
Definition: Vector3D.h:37
cifti::FloatMatrixRowRef
Definition: FloatMatrix.h:49
cifti::ConstFloatMatrixRowRef
Definition: FloatMatrix.h:38
cifti::FloatMatrix
Definition: FloatMatrix.h:64
cifti::FloatMatrix::reducedRowEchelon
FloatMatrix reducedRowEchelon() const
return the reduced row echelon form
Definition: FloatMatrix.cxx:222
cifti::FloatMatrix::ones
static FloatMatrix ones(const int64_t rows, const int64_t cols)
return a matrix of ones
Definition: FloatMatrix.cxx:248
cifti::FloatMatrix::getNumberOfColumns
int64_t getNumberOfColumns()
get number of columns
Definition: FloatMatrix.h:117
cifti::FloatMatrix::concatHoriz
FloatMatrix concatHoriz(const FloatMatrix &right) const
return a matrix formed by concatenating right to the right of this
Definition: FloatMatrix.cxx:80
cifti::FloatMatrix::inverse
FloatMatrix inverse() const
return the inverse
Definition: FloatMatrix.cxx:108
cifti::FloatMatrix::getRange
FloatMatrix getRange(const int64_t firstRow, const int64_t afterLastRow, const int64_t firstCol, const int64_t afterLastCol) const
get the range of values from first until one before afterLast, as a new matrix
Definition: FloatMatrix.cxx:94
cifti::FloatMatrix::transpose
FloatMatrix transpose() const
return the transpose
Definition: FloatMatrix.cxx:234
cifti::FloatMatrix::getAffineVectors
void getAffineVectors(Vector3D &xvec, Vector3D &yvec, Vector3D &zvec, Vector3D &offset) const
separate 3x4 or 4x4 into Vector3Ds, throw on wrong dimensions
Definition: FloatMatrix.cxx:260
cifti::FloatMatrix::identity
static FloatMatrix identity(const int64_t rows)
return square identity matrix
Definition: FloatMatrix.cxx:101
cifti::FloatMatrix::resize
void resize(const int64_t rows, const int64_t cols, const bool destructive=false)
resize the matrix - keeps contents within bounds unless destructive is true (destructive is faster)
Definition: FloatMatrix.cxx:229
cifti::FloatMatrix::zeros
static FloatMatrix zeros(const int64_t rows, const int64_t cols)
return a matrix of zeros
Definition: FloatMatrix.cxx:241
cifti::FloatMatrix::getNumberOfRows
int64_t getNumberOfRows()
get number of rows
Definition: FloatMatrix.h:115
cifti
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:42
cifti::FloatMatrix::concatVert
FloatMatrix concatVert(const FloatMatrix &bottom) const
returns a matrix formed by concatenating bottom to the bottom of this
Definition: FloatMatrix.cxx:87