C++program
Matrix/CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(MatrixBuilder LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) add_subdirectory(src) add_subdirectory(testing)
Matrix/Makefile
#exe = exename #to be set on the command line build_dir = build all: cmake -E remove_directory $(build_dir) cmake -E make_directory $(build_dir) cmake -S . -B $(build_dir) cmake --build $(build_dir) --clean-first cmake -E copy $(build_dir)/$(exe) . cmake -E remove_directory $(build_dir)
Matrix/src/CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(Matrix LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) add_subdirectory(Matrix) add_subdirectory(Vector)
Matrix/src/Matrix/CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(MatrixMatrix LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) add_library(MatrixMatrix STATIC Matrix.cpp Matrix.h RowIterator.cpp RowIterator.h ConstRowIterator.cpp ConstRowIterator.h ColumnIterator.cpp ColumnIterator.h ConstColumnIterator.cpp ConstColumnIterator.h ) target_include_directories(MatrixMatrix PUBLIC .) target_link_libraries(MatrixMatrix PUBLIC MatrixVector) target_compile_options(MatrixMatrix PRIVATE -Wall -Wextra -Werror)
Matrix/src/Matrix/ColumnIterator.cpp
Matrix/src/Matrix/ColumnIterator.cpp
//
// Created by mfbut on 11/17/2019.
//
#include
"ColumnIterator.h"
#include
"ConstColumnIterator.h"
#include
"Matrix.h"
Matrix
::
ColumnIterator
::
ColumnIterator
(
Matrix
*
matrix
,
int
col
)
{
}
Matrix
::
ColumnIterator
::
operator
ConstColumnIterator
()
const
{
}
Matrix
::
ConstVectorRef
Matrix
::
ColumnIterator
::
operator
*
()
const
{
return
const_cast
<
const
Matrix
*>
(
matrix
)
->
colAt
(
col
);
}
Matrix
::
ColumnIterator
::
value_type
Matrix
::
ColumnIterator
::
operator
*
()
{
}
Matrix
::
ConstVectorRef
Matrix
::
ColumnIterator
::
operator
[](
int
offset
)
const
{
return
const_cast
<
const
Matrix
*>
(
matrix
)
->
colAt
(
col
+
offset
);
}
Matrix
::
ColumnIterator
::
value_type
Matrix
::
ColumnIterator
::
operator
[](
int
offset
)
{
}
Matrix
::
ColumnIterator
&
Matrix
::
ColumnIterator
::
operator
++
()
{
}
const
Matrix
::
ColumnIterator
Matrix
::
ColumnIterator
::
operator
++
(
int
)
{
}
Matrix
::
ColumnIterator
&
Matrix
::
ColumnIterator
::
operator
--
()
{
}
const
Matrix
::
ColumnIterator
Matrix
::
ColumnIterator
::
operator
--
(
int
)
{
}
Matrix
::
ColumnIterator
&
Matrix
::
ColumnIterator
::
operator
+=
(
int
amount
)
{
}
Matrix
::
ColumnIterator
Matrix
::
ColumnIterator
::
operator
+
(
int
amount
)
const
{
}
Matrix
::
ColumnIterator
&
Matrix
::
ColumnIterator
::
operator
-=
(
int
amount
)
{
}
Matrix
::
ColumnIterator
Matrix
::
ColumnIterator
::
operator
-
(
int
amount
)
const
{
}
Matrix
::
ColumnIterator
::
difference_type
Matrix
::
ColumnIterator
::
operator
-
(
const
ColumnIterator
&
rhs
)
{
}
bool
Matrix
::
ColumnIterator
::
operator
==
(
const
ColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ColumnIterator
::
operator
!=
(
const
ColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ColumnIterator
::
operator
<
(
const
ColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ColumnIterator
::
operator
<=
(
const
ColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ColumnIterator
::
operator
>
(
const
ColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ColumnIterator
::
operator
>=
(
const
ColumnIterator
&
rhs
)
const
{
}
Matrix/src/Matrix/ColumnIterator.h
// // Created by mfbut on 11/17/2019. // #ifndef ECS_36B_HOMEWORK_COLUMNITERATOR_H #define ECS_36B_HOMEWORK_COLUMNITERATOR_H #include <iterator> #include "ConstVectorRef.h" #include "VectorRef.h" namespace Matrix { class Matrix; class ConstColumnIterator; class ColumnIterator { public: //the iterator type tags using iterator_category = std::random_access_iterator_tag; using value_type = VectorRef; using difference_type = int; using pointer = const value_type*; using reference = const value_type&; //Create an column iterator over the specified matrix starting at the specified column ColumnIterator(Matrix* matrix, int col); ColumnIterator(const ColumnIterator& orig) = default; virtual ~ColumnIterator() = default; //return a ConstColumnIterator that is over the same matrix and at the same position explicit operator ConstColumnIterator() const; //get a reference to the column you are at ConstVectorRef operator* () const; value_type operator*(); //get a reference to a column offset past your current location ConstVectorRef operator[](int offset) const; value_type operator[](int offset); //move to the next column ColumnIterator& operator++(); //pre const ColumnIterator operator++(int); //post //move to the previous column ColumnIterator& operator--(); //pre const ColumnIterator operator--(int); //post //move forward amount columns ColumnIterator& operator+=(int amount); ColumnIterator operator+(int amount) const; //move backward amount columns ColumnIterator& operator-=(int amount); ColumnIterator operator-(int amount) const; //return the number of columns between yourself and rhs //for example if you were at column 10 and rhs was at column 7 //the difference would be 3 difference_type operator-(const ColumnIterator& rhs); //you and rhs are equal if you are over the same matrix and //at the same column bool operator==(const ColumnIterator& rhs) const; bool operator!=(const ColumnIterator& rhs) const; //are you at a column before rhs? bool operator<(const ColumnIterator& rhs) const; bool operator<=(const ColumnIterator& rhs) const; //are you at a column after rhs? bool operator>(const ColumnIterator& rhs) const; bool operator>=(const ColumnIterator& rhs) const; protected: Matrix* matrix; int col; }; } #endif //ECS_36B_HOMEWORK_COLUMNITERATOR_H
Matrix/src/Matrix/ConstColumnIterator.cpp
Matrix/src/Matrix/ConstColumnIterator.cpp
//
// Created by mfbut on 11/17/2019.
//
#include
"ConstColumnIterator.h"
#include
"Matrix.h"
Matrix
::
ConstColumnIterator
::
ConstColumnIterator
(
const
Matrix
*
matrix
,
int
col
)
{
}
Matrix
::
ConstColumnIterator
::
value_type
Matrix
::
ConstColumnIterator
::
operator
*
()
const
{
}
Matrix
::
ConstColumnIterator
::
value_type
Matrix
::
ConstColumnIterator
::
operator
[](
int
offset
)
const
{
}
Matrix
::
ConstColumnIterator
&
Matrix
::
ConstColumnIterator
::
operator
++
()
{
}
const
Matrix
::
ConstColumnIterator
Matrix
::
ConstColumnIterator
::
operator
++
(
int
)
{
}
Matrix
::
ConstColumnIterator
&
Matrix
::
ConstColumnIterator
::
operator
--
()
{
}
const
Matrix
::
ConstColumnIterator
Matrix
::
ConstColumnIterator
::
operator
--
(
int
)
{
}
Matrix
::
ConstColumnIterator
&
Matrix
::
ConstColumnIterator
::
operator
+=
(
int
amount
)
{
}
Matrix
::
ConstColumnIterator
Matrix
::
ConstColumnIterator
::
operator
+
(
int
amount
)
const
{
}
Matrix
::
ConstColumnIterator
&
Matrix
::
ConstColumnIterator
::
operator
-=
(
int
amount
)
{
}
Matrix
::
ConstColumnIterator
Matrix
::
ConstColumnIterator
::
operator
-
(
int
amount
)
const
{
}
Matrix
::
ConstColumnIterator
::
difference_type
Matrix
::
ConstColumnIterator
::
operator
-
(
const
ConstColumnIterator
&
rhs
)
{
}
bool
Matrix
::
ConstColumnIterator
::
operator
==
(
const
ConstColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstColumnIterator
::
operator
!=
(
const
ConstColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstColumnIterator
::
operator
<
(
const
ConstColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstColumnIterator
::
operator
<=
(
const
ConstColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstColumnIterator
::
operator
>
(
const
ConstColumnIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstColumnIterator
::
operator
>=
(
const
ConstColumnIterator
&
rhs
)
const
{
}
Matrix/src/Matrix/ConstColumnIterator.h
// // Created by mfbut on 11/17/2019. // #ifndef ECS_36B_HOMEWORK_CONSTCOLUMNITERATOR_H #define ECS_36B_HOMEWORK_CONSTCOLUMNITERATOR_H #include <iterator> #include "ConstVectorRef.h" namespace Matrix { class Matrix; class ConstColumnIterator { public: //the iterator type tags using iterator_category = std::random_access_iterator_tag; using value_type = ConstVectorRef; using difference_type = int; using pointer = const value_type*; using reference = const value_type&; //Create an column iterator over the specified Matrix starting at the specified column ConstColumnIterator(const Matrix* matrix, int col); ConstColumnIterator(const ConstColumnIterator& orig) = default; virtual ~ConstColumnIterator() = default; //return a reference to the column you are at value_type operator* () const; //return a reference to the column offset columns from your current position value_type operator[](int offset) const; //move to the next column ConstColumnIterator& operator++(); //pre const ConstColumnIterator operator++(int); //post //move to the previous column ConstColumnIterator& operator--(); //pre const ConstColumnIterator operator--(int); //post //move forward amount columns ConstColumnIterator& operator+=(int amount); ConstColumnIterator operator+(int amount) const; //move backward amount columns ConstColumnIterator& operator-=(int amount); ConstColumnIterator operator-(int amount) const; //return the number of columns between yourself and rhs //for example if you were at column 10 and rhs was at column 7 //the difference would be 3 difference_type operator-(const ConstColumnIterator& rhs); //you and rhs are equal if you are over the same matrix and //at the same column bool operator==(const ConstColumnIterator& rhs) const; bool operator!=(const ConstColumnIterator& rhs) const; //are you at a column before rhs? bool operator<(const ConstColumnIterator& rhs) const; bool operator<=(const ConstColumnIterator& rhs) const; //are you at a column after rhs? bool operator>(const ConstColumnIterator& rhs) const; bool operator>=(const ConstColumnIterator& rhs) const; protected: }; } #endif //ECS_36B_HOMEWORK_CONSTCOLUMNITERATOR_H
Matrix/src/Matrix/ConstRowIterator.cpp
Matrix/src/Matrix/ConstRowIterator.cpp
//
// Created by mfbut on 11/17/2019.
//
#include
"ConstRowIterator.h"
#include
"Matrix.h"
Matrix
::
ConstRowIterator
::
ConstRowIterator
(
const
Matrix
*
matrix
,
int
row
)
{
}
Matrix
::
ConstRowIterator
::
value_type
Matrix
::
ConstRowIterator
::
operator
*
()
const
{
}
Matrix
::
ConstRowIterator
::
value_type
Matrix
::
ConstRowIterator
::
operator
[](
int
offset
)
const
{
}
Matrix
::
ConstRowIterator
&
Matrix
::
ConstRowIterator
::
operator
++
()
{
}
const
Matrix
::
ConstRowIterator
Matrix
::
ConstRowIterator
::
operator
++
(
int
)
{
}
Matrix
::
ConstRowIterator
&
Matrix
::
ConstRowIterator
::
operator
--
()
{
}
const
Matrix
::
ConstRowIterator
Matrix
::
ConstRowIterator
::
operator
--
(
int
)
{
}
Matrix
::
ConstRowIterator
&
Matrix
::
ConstRowIterator
::
operator
+=
(
int
amount
)
{
}
Matrix
::
ConstRowIterator
Matrix
::
ConstRowIterator
::
operator
+
(
int
amount
)
const
{
}
Matrix
::
ConstRowIterator
&
Matrix
::
ConstRowIterator
::
operator
-=
(
int
amount
)
{
}
Matrix
::
ConstRowIterator
Matrix
::
ConstRowIterator
::
operator
-
(
int
amount
)
const
{
}
Matrix
::
ConstRowIterator
::
difference_type
Matrix
::
ConstRowIterator
::
operator
-
(
const
ConstRowIterator
&
rhs
)
{
}
bool
Matrix
::
ConstRowIterator
::
operator
==
(
const
ConstRowIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstRowIterator
::
operator
!=
(
const
ConstRowIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstRowIterator
::
operator
<
(
const
ConstRowIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstRowIterator
::
operator
<=
(
const
ConstRowIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstRowIterator
::
operator
>
(
const
ConstRowIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstRowIterator
::
operator
>=
(
const
ConstRowIterator
&
rhs
)
const
{
}
Matrix/src/Matrix/ConstRowIterator.h
// // Created by mfbut on 11/17/2019. // #ifndef ECS_36B_HOMEWORK_CONSTROWITERATOR_H #define ECS_36B_HOMEWORK_CONSTROWITERATOR_H #include <iterator> #include "ConstVectorRef.h" #include "Matrix.h" namespace Matrix { class Matrix; class ConstRowIterator { public: //the iterator type tags using iterator_category = std::random_access_iterator_tag; using value_type = ConstVectorRef; using difference_type = int; using pointer = const value_type*; using reference = const value_type&; //Create an row iterator over the specified Matrix starting at the specified row ConstRowIterator(const Matrix* matrix, int row); ConstRowIterator(const ConstRowIterator& orig) = default; virtual ~ConstRowIterator() = default; //return a reference to the row you are on value_type operator* () const; //return a reference to the row offset rows past your current position value_type operator[](int offset) const; //move forward one row ConstRowIterator& operator++(); //pre const ConstRowIterator operator++(int); //post //move backward one row ConstRowIterator& operator--(); //pre const ConstRowIterator operator--(int); //post //move forward amount rows ConstRowIterator& operator+=(int amount); ConstRowIterator operator+(int amount) const; //move backward rows ConstRowIterator& operator-=(int amount); ConstRowIterator operator-(int amount) const; //return the number of rows between yourself and rhs //for example if you were at row 10 and rhs was at row 7 //the difference would be 3 difference_type operator-(const ConstRowIterator& rhs); //you are equal to rhs if you are both over the same matrix //and you are on the same row bool operator==(const ConstRowIterator& rhs) const; bool operator!=(const ConstRowIterator& rhs) const; //are you at a row before rhs? bool operator<(const ConstRowIterator& rhs) const; bool operator<=(const ConstRowIterator& rhs) const; //are you at a row after rhs? bool operator>(const ConstRowIterator& rhs) const; bool operator>=(const ConstRowIterator& rhs) const; protected: }; } #endif //ECS_36B_HOMEWORK_CONSTROWITERATOR_H
Matrix/src/Matrix/Matrix.cpp
Matrix/src/Matrix/Matrix.cpp
//
// Created by mfbut on 11/14/2019.
//
#include
<
algorithm
>
#include
<
functional
>
#include
"Matrix.h"
Matrix
::
Matrix
::
Matrix
(
int
numRows
,
int
numCols
,
const
Matrix
::
Matrix
::
element_type
&
val
)
){
}
Matrix
::
Matrix
::
Matrix
(
int
numRows
,
int
numCols
)
{
}
Matrix
::
Matrix
::
Matrix
(
const
std
::
vector
<
std
::
vector
<
element_type
>>&
values
)
{
}
Matrix
::
Matrix
::
Matrix
(
const
std
::
vector
<
Vector
>&
values
)
{
}
Matrix
::
Matrix
::
Matrix
(
const
std
::
vector
<
ConstVectorRef
>&
values
)
{
}
Matrix
::
Matrix
::
Matrix
(
const
std
::
vector
<
VectorRef
>&
values
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
ident
(
int
dim
)
{
}
int
Matrix
::
Matrix
::
getNumRows
()
const
{
}
int
Matrix
::
Matrix
::
getNumCols
()
const
{
}
Matrix
::
Matrix
::
element_type
&
Matrix
::
Matrix
::
at
(
int
row
,
int
col
)
{
}
const
Matrix
::
Matrix
::
element_type
&
Matrix
::
Matrix
::
at
(
int
row
,
int
col
)
const
{
}
Matrix
::
VectorRef
Matrix
::
Matrix
::
rowAt
(
int
row
)
{
}
Matrix
::
ConstVectorRef
Matrix
::
Matrix
::
rowAt
(
int
row
)
const
{
}
Matrix
::
VectorRef
Matrix
::
Matrix
::
colAt
(
int
col
)
{
}
Matrix
::
ConstVectorRef
Matrix
::
Matrix
::
colAt
(
int
col
)
const
{
}
Matrix
::
VectorRef
Matrix
::
Matrix
::
operator
[](
int
row
)
{
}
Matrix
::
ConstVectorRef
Matrix
::
Matrix
::
operator
[](
int
row
)
const
{
}
Matrix
::
Matrix
::
const_iterator
Matrix
::
Matrix
::
begin
()
const
{
}
Matrix
::
Matrix
::
const_iterator
Matrix
::
Matrix
::
end
()
const
{
}
Matrix
::
Matrix
::
iterator
Matrix
::
Matrix
::
begin
()
{
}
Matrix
::
Matrix
::
iterator
Matrix
::
Matrix
::
end
()
{
}
Matrix
::
Matrix
::
const_row_iterator
Matrix
::
Matrix
::
rowBegin
()
const
{
}
Matrix
::
Matrix
::
const_row_iterator
Matrix
::
Matrix
::
rowEnd
()
const
{
;
}
Matrix
::
Matrix
::
row_iterator
Matrix
::
Matrix
::
rowBegin
()
{
}
Matrix
::
Matrix
::
row_iterator
Matrix
::
Matrix
::
rowEnd
()
{
}
Matrix
::
Matrix
::
const_column_iterator
Matrix
::
Matrix
::
colBegin
()
const
{
}
Matrix
::
Matrix
::
const_column_iterator
Matrix
::
Matrix
::
colEnd
()
const
{
}
Matrix
::
Matrix
::
column_iterator
Matrix
::
Matrix
::
colBegin
()
{
}
Matrix
::
Matrix
::
column_iterator
Matrix
::
Matrix
::
colEnd
()
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
-
()
const
{
}
Matrix
::
Matrix
&
Matrix
::
Matrix
::
operator
+=
(
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
+
(
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
&
Matrix
::
Matrix
::
operator
-=
(
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
-
(
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
&
Matrix
::
Matrix
::
operator
*=
(
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
*
(
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
&
Matrix
::
Matrix
::
operator
+=
(
const
element_type
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
+
(
const
element_type
&
rhs
)
const
{
}
Matrix
::
Matrix
&
Matrix
::
Matrix
::
operator
-=
(
const
element_type
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
-
(
const
element_type
&
rhs
)
const
{
}
Matrix
::
Matrix
&
Matrix
::
Matrix
::
operator
*=
(
const
element_type
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
Matrix
::
operator
*
(
const
element_type
&
rhs
)
const
{
}
Matrix
::
Matrix
Matrix
::
operator
+
(
const
Matrix
::
element_type
&
lhs
,
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
operator
-
(
const
Matrix
::
element_type
&
lhs
,
const
Matrix
&
rhs
)
{
}
Matrix
::
Matrix
Matrix
::
operator
*
(
const
Matrix
::
element_type
&
lhs
,
const
Matrix
&
rhs
)
{
}
Matrix/src/Matrix/Matrix.h
// // Created by mfbut on 11/14/2019. // #ifndef ECS_36B_HOMEWORK_MATRIX_H #define ECS_36B_HOMEWORK_MATRIX_H #include <vector> #include "Vector.h" #include "VectorRef.h" #include "ConstVectorRef.h" namespace Matrix { class RowIterator; class ConstRowIterator; class ColumnIterator; class ConstColumnIterator; class Matrix { public: using element_type = Vector::value_type; using value_type = Vector; using reference = VectorRef; using const_reference = ConstVectorRef; using pointer = value_type*; using const_pointer = const value_type*; using difference_type = int; using row_iterator = RowIterator; using const_row_iterator = ConstRowIterator; using column_iterator = ColumnIterator; using const_column_iterator = ConstColumnIterator; //by default we go through the rows using iterator = row_iterator; using const_iterator = const_row_iterator; Matrix(const Matrix& orig) = default; //create a numRows X numCols Matrix where all of the elements are val Matrix(int numRows, int numCols, const element_type& val); //create a numRows X numCols Matrix where all of the elements are 0 Matrix(int numRows, int numCols); //create a matrix with the specified values explicit Matrix(const std::vector<std::vector<element_type>>& values); explicit Matrix(const std::vector<Vector>& values); explicit Matrix(const std::vector<ConstVectorRef>& values); explicit Matrix(const std::vector<VectorRef>& values); virtual ~Matrix() = default; //return an identity matrix that is dim X dim big //in the identity matrix all of the elements are 0 except for the ones //on the main diagonal //an example 3 X 3 identity matrix //1 0 0 //0 1 0 //0 0 1 static Matrix ident(int dim); //return the number of rows int getNumRows() const; //return the number of columns int getNumCols() const; //element access element_type& at(int row, int col); const element_type& at(int row, int col) const; //get a reference to the specified row VectorRef rowAt(int row); ConstVectorRef rowAt(int row) const; //get a reference to the specified column VectorRef colAt(int col); ConstVectorRef colAt(int col) const; //get a reference to the specified row VectorRef operator[](int row); ConstVectorRef operator[](int row) const; //const iterators. go through the matrix by row const_iterator begin() const; const_iterator end() const; //iterators. go through the matrix by row iterator begin(); iterator end(); //explicit const row iterators const_row_iterator rowBegin() const; const_row_iterator rowEnd() const; //explicit row iterators row_iterator rowBegin(); row_iterator rowEnd(); //explicit const column iterators const_column_iterator colBegin() const; const_column_iterator colEnd() const; //explicit column iterators column_iterator colBegin(); column_iterator colEnd(); //negate Matrix operator-() const; //Matrix + Matrix Matrix& operator+=(const Matrix& rhs); Matrix operator+(const Matrix& rhs); //Matrix - Matrix Matrix& operator-=(const Matrix& rhs); Matrix operator-(const Matrix& rhs); //Matrix * Matrix Matrix& operator*=(const Matrix& rhs); Matrix operator*(const Matrix& rhs); //Matrix + scalar Matrix& operator+=(const element_type& rhs); Matrix operator+(const element_type& rhs) const; //Matrix - scalar Matrix& operator-=(const element_type& rhs); Matrix operator-(const element_type& rhs) const; //Matrix * scalar Matrix& operator*=(const element_type& rhs); Matrix operator*(const element_type& rhs) const; protected: }; //scalar + Matrix Matrix operator+(const Matrix::element_type& lhs, const Matrix& rhs); //scalar - Matrix Matrix operator-(const Matrix::element_type& lhs, const Matrix& rhs); //scalar * Matrix Matrix operator*(const Matrix::element_type& lhs, const Matrix& rhs); } #include "ConstRowIterator.h" #include "RowIterator.h" #include "ConstColumnIterator.h" #include "ColumnIterator.h" #endif //ECS_36B_HOMEWORK_MATRIX_H
Matrix/src/Matrix/RowIterator.cpp
Matrix/src/Matrix/RowIterator.cpp
//
// Created by mfbut on 11/17/2019.
//
#include
"RowIterator.h"
#include
"Matrix.h"
Matrix
::
RowIterator
::
RowIterator
(
Matrix
*
matrix
,
int
row
)
{
}
Matrix
::
RowIterator
::
operator
ConstRowIterator
()
const
{
}
Matrix
::
ConstVectorRef
Matrix
::
RowIterator
::
operator
*
()
const
{
//make sure that we call the const at method on matrix
return
const_cast
<
const
Matrix
*>
(
matrix
)
->
rowAt
(
row
);
}
Matrix
::
ConstVectorRef
Matrix
::
RowIterator
::
operator
[](
int
offset
)
const
{
//make sure that we call the const at method on matrix
return
const_cast
<
const
Matrix
*>
(
matrix
)
->
rowAt
(
row
+
offset
);
}
Matrix
::
RowIterator
::
value_type
Matrix
::
RowIterator
::
operator
*
()
{
}
Matrix
::
RowIterator
::
value_type
Matrix
::
RowIterator
::
operator
[](
int
offset
)
{
}
Matrix
::
RowIterator
&
Matrix
::
RowIterator
::
operator
++
()
{
}
const
Matrix
::
RowIterator
Matrix
::
RowIterator
::
operator
++
(
int
)
{
}
Matrix
::
RowIterator
&
Matrix
::
RowIterator
::
operator
--
()
{
}
const
Matrix
::
RowIterator
Matrix
::
RowIterator
::
operator
--
(
int
)
{
}
Matrix
::
RowIterator
&
Matrix
::
RowIterator
::
operator
+=
(
int
amount
)
{
}
Matrix
::
RowIterator
Matrix
::
RowIterator
::
operator
+
(
int
amount
)
const
{
}
Matrix
::
RowIterator
&
Matrix
::
RowIterator
::
operator
-=
(
int
amount
)
{
}
Matrix
::
RowIterator
Matrix
::
RowIterator
::
operator
-
(
int
amount
)
const
{
}
Matrix
::
RowIterator
::
difference_type
Matrix
::
RowIterator
::
operator
-
(
const
RowIterator
&
rhs
)
{
}
bool
Matrix
::
RowIterator
::
operator
==
(
const
RowIterator
&
rhs
)
const
{
}
bool
Matrix
::
RowIterator
::
operator
!=
(
const
RowIterator
&
rhs
)
const
{
}
bool
Matrix
::
RowIterator
::
operator
<
(
const
RowIterator
&
rhs
)
const
{
}
bool
Matrix
::
RowIterator
::
operator
<=
(
const
RowIterator
&
rhs
)
const
{
}
bool
Matrix
::
RowIterator
::
operator
>
(
const
RowIterator
&
rhs
)
const
{
}
bool
Matrix
::
RowIterator
::
operator
>=
(
const
RowIterator
&
rhs
)
const
{
}
Matrix/src/Matrix/RowIterator.h
// // Created by mfbut on 11/17/2019. // #ifndef ECS_36B_HOMEWORK_ROWITERATOR_H #define ECS_36B_HOMEWORK_ROWITERATOR_H #include <iterator> #include "VectorRef.h" #include "ConstVectorRef.h" namespace Matrix { class Matrix; class ConstRowIterator; class RowIterator { public: //the iterator type tags using iterator_category = std::random_access_iterator_tag; using value_type = VectorRef; using difference_type = int; using pointer = const value_type*; using reference = const value_type&; //create a RowIterator over the specified matrix starting at the specified row RowIterator(Matrix* matrix, int row); RowIterator(const RowIterator& orig) = default; virtual ~RowIterator() = default; //return a ConstRowIterator over the same matrix and at the same row explicit operator ConstRowIterator() const; //return a reference to the row that you are on ConstVectorRef operator* () const; value_type operator*(); //return a reference to the row offset past your current row ConstVectorRef operator[](int offset) const; value_type operator[](int offset); //move to the next row RowIterator& operator++(); //pre const RowIterator operator++(int); //post //move to the previous row RowIterator& operator--(); //pre const RowIterator operator--(int); //post //move forward amount rows RowIterator& operator+=(int amount); RowIterator operator+(int amount) const; //move backward amount rows RowIterator& operator-=(int amount); RowIterator operator-(int amount) const; //return the number of rows between yourself and rhs //for example if you were at row 10 and rhs was at row 7 //the difference would be 3 difference_type operator-(const RowIterator& rhs); //you are equal to rhs if you are both over the same matrix //and you are on the same row bool operator==(const RowIterator& rhs) const; bool operator!=(const RowIterator& rhs) const; //are you at a row before rhs? bool operator<(const RowIterator& rhs) const; bool operator<=(const RowIterator& rhs) const; //are you at a row after rhs bool operator>(const RowIterator& rhs) const; bool operator>=(const RowIterator& rhs) const; protected: Matrix* matrix; int row; }; } #endif //ECS_36B_HOMEWORK_ROWITERATOR_H
Matrix/src/Vector/BaseVector.cpp
Matrix/src/Vector/BaseVector.cpp
//
// Created by mfbut on 11/14/2019.
//
#include
<
functional
>
#include
<
algorithm
>
#include
"BaseVector.h"
#include
"Vector.h"
#include
"ConstVectorIterator.h"
#include
"VectorIterator.h"
#include
"Matrix.h"
Matrix
::
BaseVector
::
operator
Matrix
()
const
{
}
Matrix
::
VectorIterator
Matrix
::
BaseVector
::
begin
()
{
}
Matrix
::
VectorIterator
Matrix
::
BaseVector
::
end
()
{
}
Matrix
::
ConstVectorIterator
Matrix
::
BaseVector
::
begin
()
const
{
}
Matrix
::
ConstVectorIterator
Matrix
::
BaseVector
::
end
()
const
{
}
Matrix
::
ConstVectorIterator
Matrix
::
BaseVector
::
cbegin
()
const
{
}
Matrix
::
ConstVectorIterator
Matrix
::
BaseVector
::
cend
()
const
{
}
Matrix
::
BaseVector
&
Matrix
::
BaseVector
::
operator
+=
(
const
ConstBaseVector
&
rhs
)
{
}
Matrix
::
BaseVector
&
Matrix
::
BaseVector
::
operator
-=
(
const
ConstBaseVector
&
rhs
)
{
}
Matrix
::
BaseVector
&
Matrix
::
BaseVector
::
operator
+=
(
const
BaseVector
::
value_type
&
rhs
)
{
}
Matrix
::
BaseVector
&
Matrix
::
BaseVector
::
operator
-=
(
const
BaseVector
::
value_type
&
rhs
)
{
}
Matrix
::
BaseVector
&
Matrix
::
BaseVector
::
operator
*=
(
const
BaseVector
::
value_type
&
rhs
)
{
}
Matrix/src/Vector/BaseVector.h
// // Created by mfbut on 11/14/2019. // #ifndef ECS_36B_HOMEWORK_BASEVECTOR_H #define ECS_36B_HOMEWORK_BASEVECTOR_H #include "ConstBaseVector.h" namespace Matrix { class Vector; class VectorIterator; class ConstVectorIterator; class Matrix; class BaseVector : public virtual ConstBaseVector{ public: //the type tags using value_type = int; using reference = value_type&; using const_reference = const value_type&; using pointer = value_type*; using const_pointer = const value_type*; using difference_type = int; using iterator = VectorIterator; using const_iterator = ConstVectorIterator; virtual ~BaseVector() override = default; //return a 1 X N matrix with the same values as yourself explicit operator Matrix() const; //element access virtual const ConstBaseVector::value_type& at(int index) const override = 0; virtual value_type& at(int index) = 0; virtual const ConstBaseVector::value_type& operator[](int index) const override = 0; virtual value_type& operator[](int index) = 0; //iterators virtual VectorIterator begin(); virtual VectorIterator end(); virtual ConstVectorIterator begin() const override; virtual ConstVectorIterator end() const override; virtual ConstVectorIterator cbegin() const; virtual ConstVectorIterator cend() const; //vector += vector virtual BaseVector& operator+=(const ConstBaseVector& rhs); //vector -= vector virtual BaseVector& operator-=(const ConstBaseVector& rhs); //vector += scalar virtual BaseVector& operator+=(const value_type& rhs); //vector -= scalar virtual BaseVector& operator-=(const value_type& rhs); //vector *= scalar virtual BaseVector& operator*=(const value_type& rhs); }; } #include "ConstVectorIterator.h" #include "VectorIterator.h" #endif //ECS_36B_HOMEWORK_BASEVECTOR_H
Matrix/src/Vector/CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(MatrixVector LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) add_library(MatrixVector STATIC BaseVector.cpp BaseVector.h Vector.cpp Vector.h VectorIterator.cpp VectorIterator.h ConstVectorIterator.cpp ConstVectorIterator.h VectorRef.cpp VectorRef.h ConstBaseVector.cpp ConstBaseVector.h ConstVectorRef.cpp ConstVectorRef.h) target_include_directories(MatrixVector PUBLIC .) target_link_libraries(MatrixVector PUBLIC MatrixMatrix) target_compile_options(MatrixVector PRIVATE -Wall -Wextra -Werror)
Matrix/src/Vector/ConstBaseVector.cpp
Matrix/src/Vector/ConstBaseVector.cpp
//
// Created by mfbut on 11/17/2019.
//
#include
<
algorithm
>
#include
"Vector.h"
#include
"ConstBaseVector.h"
Matrix
::
ConstVectorIterator
Matrix
::
ConstBaseVector
::
begin
()
const
{
}
Matrix
::
ConstVectorIterator
Matrix
::
ConstBaseVector
::
end
()
const
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
-
()
const
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
+
(
const
ConstBaseVector
&
rhs
)
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
-
(
const
ConstBaseVector
&
rhs
)
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
*
(
const
ConstBaseVector
&
rhs
)
const
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
+
(
const
ConstBaseVector
::
value_type
&
rhs
)
const
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
-
(
const
ConstBaseVector
::
value_type
&
rhs
)
const
{
}
Matrix
::
Vector
Matrix
::
ConstBaseVector
::
operator
*
(
const
ConstBaseVector
::
value_type
&
rhs
)
const
{
}
bool
Matrix
::
ConstBaseVector
::
operator
==
(
const
ConstBaseVector
&
rhs
)
const
{
}
bool
Matrix
::
ConstBaseVector
::
operator
!=
(
const
ConstBaseVector
&
rhs
)
const
{
}
Matrix
::
Vector
Matrix
::
operator
+
(
const
ConstBaseVector
::
value_type
&
lhs
,
const
ConstBaseVector
&
rhs
)
{
}
Matrix
::
Vector
Matrix
::
operator
-
(
const
ConstBaseVector
::
value_type
&
lhs
,
const
ConstBaseVector
&
rhs
)
{
}
Matrix
::
Vector
Matrix
::
operator
*
(
const
ConstBaseVector
::
value_type
&
lhs
,
const
ConstBaseVector
&
rhs
)
{
}
Matrix/src/Vector/ConstBaseVector.h
// // Created by mfbut on 11/17/2019. // #ifndef ECS_36B_HOMEWORK_CONSTBASEVECTOR_H #define ECS_36B_HOMEWORK_CONSTBASEVECTOR_H namespace Matrix { class Vector; class ConstVectorIterator; class ConstBaseVector { public: //type tags using value_type = int; using reference = const value_type&; using const_reference = const value_type&; using pointer = const value_type*; using const_pointer = const value_type*; using difference_type = int; using iterator = ConstVectorIterator; using const_iterator = ConstVectorIterator; virtual ~ConstBaseVector() = default; virtual int size() const = 0; //element access virtual const value_type& at(int index) const = 0 ; virtual const value_type& operator[](int index) const = 0; //iterators virtual ConstVectorIterator begin() const; virtual ConstVectorIterator end() const; //negation virtual Vector operator-() const; //vector addition virtual Vector operator+(const ConstBaseVector& rhs); //vector subtraction virtual Vector operator-(const ConstBaseVector& rhs); //vector multiplication i.e. dot product virtual Vector operator*(const ConstBaseVector& rhs) const; //vector + scalar virtual Vector operator+(const value_type& rhs) const; //vector - scalar virtual Vector operator-(const value_type& rhs) const; //vector * scalar virtual Vector operator*(const value_type& rhs) const; //you are the same as rhs if you contain the same elements as rhs bool operator==(const ConstBaseVector& rhs) const; bool operator!=(const ConstBaseVector& rhs) const; }; //scalar + vector Vector operator+(const ConstBaseVector::value_type& lhs, const ConstBaseVector& rhs); //scalar + vector Vector operator-(const ConstBaseVector::value_type& lhs, const ConstBaseVector& rhs); //scalar * vector Vector operator*(const ConstBaseVector::value_type& lhs, const ConstBaseVector& rhs); } #include "ConstVectorIterator.h" #endif //ECS_36B_HOMEWORK_CONSTBASEVECTOR_H
Matrix/src/Vector/ConstVectorIterator.cpp
Matrix/src/Vector/ConstVectorIterator.cpp
//
// Created by mfbut on 11/14/2019.
//
#include
"ConstVectorIterator.h"
#include
"BaseVector.h"
Matrix
::
ConstVectorIterator
::
ConstVectorIterator
(
const
ConstBaseVector
*
vector
,
int
pos
){
}
const
Matrix
::
ConstVectorIterator
::
value_type
&
Matrix
::
ConstVectorIterator
::
operator
*
()
const
{
}
const
Matrix
::
ConstVectorIterator
::
value_type
&
Matrix
::
ConstVectorIterator
::
operator
[](
int
offset
)
const
{
}
Matrix
::
ConstVectorIterator
&
Matrix
::
ConstVectorIterator
::
operator
++
()
{
}
const
Matrix
::
ConstVectorIterator
Matrix
::
ConstVectorIterator
::
operator
++
(
int
)
{
}
Matrix
::
ConstVectorIterator
&
Matrix
::
ConstVectorIterator
::
operator
--
()
{
}
const
Matrix
::
ConstVectorIterator
Matrix
::
ConstVectorIterator
::
operator
--
(
int
)
{
}
Matrix
::
ConstVectorIterator
&
Matrix
::
ConstVectorIterator
::
operator
+=
(
int
amount
)
{
}
Matrix
::
ConstVectorIterator
Matrix
::
ConstVectorIterator
::
operator
+
(
int
amount
)
const
{
}
Matrix
::
ConstVectorIterator
&
Matrix
::
ConstVectorIterator
::
operator
-=
(
int
amount
)
{
}
Matrix
::
ConstVectorIterator
Matrix
::
ConstVectorIterator
::
operator
-
(
int
amount
)
const
{
}
Matrix
::
ConstVectorIterator
::
difference_type
Matrix
::
ConstVectorIterator
::
operator
-
(
const
ConstVectorIterator
&
rhs
)
{
}
bool
Matrix
::
ConstVectorIterator
::
operator
==
(
const
ConstVectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstVectorIterator
::
operator
!=
(
const
ConstVectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstVectorIterator
::
operator
<
(
const
ConstVectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstVectorIterator
::
operator
<=
(
const
ConstVectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstVectorIterator
::
operator
>
(
const
ConstVectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
ConstVectorIterator
::
operator
>=
(
const
ConstVectorIterator
&
rhs
)
const
{
}
Matrix/src/Vector/ConstVectorIterator.h
// // Created by mfbut on 11/14/2019. // #ifndef ECS_36B_HOMEWORK_CONSTVECTORITERATOR_H #define ECS_36B_HOMEWORK_CONSTVECTORITERATOR_H #include <iterator> #include "ConstBaseVector.h" namespace Matrix { class ConstBaseVector; class ConstVectorIterator { public: //the iterator type tags using iterator_category = std::random_access_iterator_tag; using value_type = ConstBaseVector::value_type; using difference_type = int; using pointer = const value_type*; using reference = const value_type&; //create a ConstVectorIterator over the specified vector starting at the specified position ConstVectorIterator(const ConstBaseVector* vector, int pos); ConstVectorIterator(const ConstVectorIterator& orig) = default; virtual ~ConstVectorIterator() = default; //return a reference to the element you are at const value_type& operator*() const; //return a reference to the element offset past your current position const value_type& operator[](int offset) const; //move to the next element ConstVectorIterator& operator++(); //pre const ConstVectorIterator operator++(int); //post //move to the previous element ConstVectorIterator& operator--(); //pre const ConstVectorIterator operator--(int); //post //move forward amount elements ConstVectorIterator& operator+=(int amount); ConstVectorIterator operator+(int amount) const; //move backward amount elements ConstVectorIterator& operator-=(int amount); ConstVectorIterator operator-(int amount) const; //return the number of elements between yourself and rhs //for example if you were at element 10 and rhs was at element 7 //the difference would be 3 difference_type operator-(const ConstVectorIterator& rhs); //you are equal to rhs if you are over the same vector //and at the same position bool operator==(const ConstVectorIterator& rhs) const; bool operator!=(const ConstVectorIterator& rhs) const; //are you at an element before rhs? bool operator<(const ConstVectorIterator& rhs) const; bool operator<=(const ConstVectorIterator& rhs) const; //are you at an element after rhs? bool operator>(const ConstVectorIterator& rhs) const; bool operator>=(const ConstVectorIterator& rhs) const; private: }; } #endif //ECS_36B_HOMEWORK_CONSTVECTORITERATOR_H
Matrix/src/Vector/ConstVectorRef.cpp
Matrix/src/Vector/ConstVectorRef.cpp
//
// Created by mfbut on 11/17/2019.
//
#include
"ConstVectorRef.h"
#include
"Matrix.h"
Matrix
::
ConstVectorRef
::
ConstVectorRef
(
const
ConstBaseVector
&
orig
)
{
elements
.
reserve
(
orig
.
size
());
for
(
auto
&
element
:
orig
){
//the const cast here allows us to remove the const from the const int*
//so that we can place the int* into our vector
//const_cast can be dangerous if used correctly but
//can be very helpful when applied correctly
//please don't use it in your own code until we cover it in class
//or you read up on it.
elements
.
push_back
(
const_cast
<
value_type
*>
(
&
element
));
}
}
Matrix
::
ConstVectorRef
::
ConstVectorRef
(
const
Matrix
&
matrix
,
int
col
)
{
elements
.
reserve
(
matrix
.
getNumCols
());
for
(
const
auto
&
row
:
matrix
){
//again with the const cast to discard the constness
elements
.
push_back
(
const_cast
<
value_type
*>
(
&
row
.
at
(
col
)));
}
}
int
Matrix
::
ConstVectorRef
::
size
()
const
{
}
const
Matrix
::
ConstBaseVector
::
value_type
&
Matrix
::
ConstVectorRef
::
at
(
int
index
)
const
{
}
const
Matrix
::
ConstBaseVector
::
value_type
&
Matrix
::
ConstVectorRef
::
operator
[](
int
index
)
const
{
}
Matrix/src/Vector/ConstVectorRef.h
// // Created by mfbut on 11/17/2019. // #ifndef ECS_36B_HOMEWORK_CONSTVECTORREF_H #define ECS_36B_HOMEWORK_CONSTVECTORREF_H #include <vector> #include "ConstBaseVector.h" namespace Matrix { class BaseVector; class Matrix; class ConstVectorRef : public virtual ConstBaseVector{ public: virtual ~ConstVectorRef() override = default; explicit ConstVectorRef(const ConstBaseVector& orig); //create a reference over the specified column in the given matrix ConstVectorRef(const Matrix& matrix, int col); //because this is a const reference we cannot change the value //that it refers to so we delete the operator= ConstVectorRef& operator=(ConstBaseVector& rhs) = delete; //return the number of elements in the vector you refer to virtual int size() const override; //return a reference to the element at the position you refer to virtual const value_type& at(int index) const override; virtual const value_type& operator[](int index) const override; protected: std::vector<value_type*> elements; }; } #endif //ECS_36B_HOMEWORK_CONSTVECTORREF_H
Matrix/src/Vector/Vector.cpp
Matrix/src/Vector/Vector.cpp
//
// Created by mfbut on 11/14/2019.
//
#include
"Vector.h"
Matrix
::
Vector
::
Vector
(
const
ConstBaseVector
&
orig
)
{
}
Matrix
::
Vector
::
Vector
(
int
numElements
,
const
BaseVector
::
value_type
&
value
)
{
}
Matrix
::
Vector
::
Vector
(
int
numElements
)
{
}
Matrix
::
Vector
::
Vector
(
const
std
::
vector
<
value_type
>&
values
)
{
}
Matrix
::
Vector
::
Vector
()
{
}
int
Matrix
::
Vector
::
size
()
const
{
}
Matrix
::
BaseVector
::
value_type
&
Matrix
::
Vector
::
at
(
int
index
)
{
}
const
Matrix
::
BaseVector
::
value_type
&
Matrix
::
Vector
::
at
(
int
index
)
const
{
}
Matrix
::
BaseVector
::
value_type
&
Matrix
::
Vector
::
operator
[](
int
index
)
{
}
const
Matrix
::
BaseVector
::
value_type
&
Matrix
::
Vector
::
operator
[](
int
index
)
const
{
}
Matrix
::
Vector
&
Matrix
::
Vector
::
operator
=
(
const
ConstBaseVector
&
rhs
)
{
}
Matrix/src/Vector/Vector.h
// // Created by mfbut on 11/14/2019. // #ifndef ECS_36B_HOMEWORK_VECTOR_H #define ECS_36B_HOMEWORK_VECTOR_H #include <vector> #include "BaseVector.h" #include "ConstBaseVector.h" namespace Matrix { class Vector : public BaseVector{ public: //create a vector that contains the same elements as in orig explicit Vector(const ConstBaseVector& orig); //create a vector that holds numElements and each element is value Vector(int numElements, const BaseVector::value_type& value); //create a vector that holds numElements 0's explicit Vector(int numElements); //create a vector that contains the specified values explicit Vector(const std::vector<value_type>& values); //create a 1 X 1 vector with the value 0 Vector(); virtual ~Vector() override = default; //make all of the elements in this vector the same //as what is in rhs. adjust the size of this vector if necessary Vector& operator=(const ConstBaseVector& rhs); //return the number of elements in the Vector virtual int size() const override; //return the element at the position virtual value_type& at(int index) override; virtual const value_type& at(int index) const override; virtual value_type& operator[](int index) override; virtual const value_type& operator[](int index) const override; private: }; } #endif //ECS_36B_HOMEWORK_VECTOR_H
Matrix/src/Vector/VectorIterator.cpp
Matrix/src/Vector/VectorIterator.cpp
//
// Created by mfbut on 11/14/2019.
//
#include
"VectorIterator.h"
#include
"ConstColumnIterator.h"
Matrix
::
VectorIterator
::
VectorIterator
(
BaseVector
*
vector
,
int
pos
)
{}
Matrix
::
VectorIterator
::
operator
ConstVectorIterator
()
const
{
}
Matrix
::
VectorIterator
::
value_type
&
Matrix
::
VectorIterator
::
operator
*
()
{
}
const
Matrix
::
VectorIterator
::
value_type
&
Matrix
::
VectorIterator
::
operator
*
()
const
{
}
const
Matrix
::
VectorIterator
::
value_type
&
Matrix
::
VectorIterator
::
operator
[](
int
offset
)
const
{
}
Matrix
::
VectorIterator
::
value_type
&
Matrix
::
VectorIterator
::
operator
[](
int
offset
)
{
}
Matrix
::
VectorIterator
&
Matrix
::
VectorIterator
::
operator
++
()
{
}
const
Matrix
::
VectorIterator
Matrix
::
VectorIterator
::
operator
++
(
int
)
{
}
Matrix
::
VectorIterator
&
Matrix
::
VectorIterator
::
operator
--
()
{
}
const
Matrix
::
VectorIterator
Matrix
::
VectorIterator
::
operator
--
(
int
)
{
}
Matrix
::
VectorIterator
&
Matrix
::
VectorIterator
::
operator
+=
(
int
amount
)
{
}
Matrix
::
VectorIterator
Matrix
::
VectorIterator
::
operator
+
(
int
amount
)
const
{
}
Matrix
::
VectorIterator
&
Matrix
::
VectorIterator
::
operator
-=
(
int
amount
)
{
}
Matrix
::
VectorIterator
Matrix
::
VectorIterator
::
operator
-
(
int
amount
)
const
{
}
Matrix
::
VectorIterator
::
difference_type
Matrix
::
VectorIterator
::
operator
-
(
const
VectorIterator
&
rhs
)
{
}
bool
Matrix
::
VectorIterator
::
operator
==
(
const
VectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
VectorIterator
::
operator
!=
(
const
VectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
VectorIterator
::
operator
<
(
const
VectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
VectorIterator
::
operator
<=
(
const
VectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
VectorIterator
::
operator
>
(
const
VectorIterator
&
rhs
)
const
{
}
bool
Matrix
::
VectorIterator
::
operator
>=
(
const
VectorIterator
&
rhs
)
const
{
}
Matrix/src/Vector/VectorIterator.h
// // Created by mfbut on 11/14/2019. // #ifndef ECS_36B_HOMEWORK_VECTORITERATOR_H #define ECS_36B_HOMEWORK_VECTORITERATOR_H #include <iterator> #include "BaseVector.h" namespace Matrix { class BaseVector; class ConstVectorIterator; class VectorIterator { public: //the iterator type tags using value_type = BaseVector::value_type; using difference_type = int; using pointer = value_type*; using reference = value_type&; using iterator_category = std::random_access_iterator_tag; //create an iterator over the given vector starting at the specified position VectorIterator(BaseVector* vector, int pos); virtual ~VectorIterator() = default; //conversion to a ConstVectorIterator explicit operator ConstVectorIterator() const; //return the element that you are at value_type& operator*(); const value_type& operator*() const; //return the element offset elements away from your current position value_type& operator[](int offset); const value_type& operator[](int offset) const; //go to the next element VectorIterator& operator++(); //pre const VectorIterator operator++(int); //post //go to the previous element VectorIterator& operator--(); //pre const VectorIterator operator--(int); //post //move amount elements forward VectorIterator& operator+=(int amount); VectorIterator operator+(int amount) const; //move amount elements backwards VectorIterator& operator-=(int amount); VectorIterator operator-(int amount) const; //return the number of elements between yourself and rhs //for example if you were at element 10 and rhs was at element 7 //the difference would be 3 difference_type operator-(const VectorIterator& rhs); //true if both this and rhs are over the same vector and at the same element bool operator==(const VectorIterator& rhs) const; bool operator!=(const VectorIterator& rhs) const; //are you at an element before rhs? bool operator<(const VectorIterator& rhs) const; bool operator<=(const VectorIterator& rhs) const; //are you at an element after rhs? bool operator>(const VectorIterator& rhs) const; bool operator>=(const VectorIterator& rhs) const; private: }; } #endif //ECS_36B_HOMEWORK_VECTORITERATOR_H
Matrix/src/Vector/VectorRef.cpp
Matrix/src/Vector/VectorRef.cpp
//
// Created by mfbut on 11/16/2019.
//
#include
"VectorRef.h"
#include
"Matrix.h"
Matrix
::
VectorRef
::
VectorRef
(
BaseVector
&
orig
)
{
}
Matrix
::
VectorRef
::
VectorRef
(
Matrix
&
matrix
,
int
col
)
{
}
int
Matrix
::
VectorRef
::
size
()
const
{
}
Matrix
::
BaseVector
::
value_type
&
Matrix
::
VectorRef
::
at
(
int
index
)
{
}
const
Matrix
::
BaseVector
::
value_type
&
Matrix
::
VectorRef
::
at
(
int
index
)
const
{
}
Matrix
::
BaseVector
::
value_type
&
Matrix
::
VectorRef
::
operator
[](
int
index
)
{
}
const
Matrix
::
BaseVector
::
value_type
&
Matrix
::
VectorRef
::
operator
[](
int
index
)
const
{
}
Matrix
::
VectorRef
&
Matrix
::
VectorRef
::
operator
=
(
const
BaseVector
&
rhs
)
{
}
Matrix/src/Vector/VectorRef.h
// // Created by mfbut on 11/16/2019. // #ifndef ECS_36B_HOMEWORK_VECTORREF_H #define ECS_36B_HOMEWORK_VECTORREF_H #include <vector> #include "BaseVector.h" #include "ConstVectorRef.h" namespace Matrix { class Matrix; class VectorRef : public ConstVectorRef, public BaseVector { public: //type tags using value_type = BaseVector::value_type; using reference = BaseVector::reference; using const_reference = BaseVector::const_reference; using pointer = BaseVector::pointer; using const_pointer = BaseVector::const_pointer; using difference_type = BaseVector::difference_type; using iterator = BaseVector::iterator; using const_iterator = BaseVector::const_iterator; VectorRef() = delete; virtual ~VectorRef() override = default; //create a reference to the given vector explicit VectorRef(BaseVector& orig); explicit VectorRef(const ConstBaseVector& orig) = delete; // make extra sure this doesn't get created //create a reference over the specified column in the given matrix VectorRef(Matrix& matrix, int col); VectorRef(const Matrix& matrix, int col) = delete; // make extra sure this doesn't get created //set the value of each element that we refer to //equal to the value at the corresponding position in rhs VectorRef& operator=(const BaseVector& rhs); //return the number of elements in the vector it refers to virtual int size() const override; //element access virtual value_type& at(int index) override; virtual const value_type& at(int index) const override; virtual value_type& operator[](int index) override; virtual const value_type& operator[](int index) const override; }; } #endif //ECS_36B_HOMEWORK_VECTORREF_H
Matrix/testing/CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(MatrixTesting LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) find_package(Threads REQUIRED) include(FetchContent) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG master ) FetchContent_GetProperties(googletest) if (NOT googletest_POPULATED) FetchContent_Populate(googletest) endif () add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}) add_subdirectory(Vector) add_subdirectory(Matrix) add_executable(MatrixTester test_main.cpp) target_link_libraries(MatrixTester VectorTesting MatrixTesting)
Matrix/testing/Matrix/CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(MatrixVectorTesting LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) add_library(MatrixTesting STATIC MatrixTesting.cpp MatrixTesting.h MatrixConstructorTests.cpp MatrixElementAccessTests.cpp MatrixArithmeticOperatorTests.cpp) target_link_libraries(MatrixTesting PUBLIC MatrixMatrix gtest gmock ) target_compile_options(MatrixTesting PRIVATE -Wall -Werror -Wextra) target_include_directories(MatrixTesting PUBLIC .)
Matrix/testing/Matrix/MatrixArithmeticOperatorTests.cpp
Matrix/testing/Matrix/MatrixArithmeticOperatorTests.cpp
//
// Created by mfbut on 11/21/2019.
//
#include
"MatrixTesting.h"
#include
"gmock/gmock.h"
TEST_F
(
MatrixTesting
,
MatrixOperatorNegate
)
{
using
namespace
::
testing
;
answer_type values
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type answer
{{
-
10
,
-
20
},
{
-
30
,
-
40
},
{
-
45
,
-
50
},
{
-
100
,
-
200
}};
Matrix
::
Matrix
mat1
(
values
);
ASSERT_TRUE
(
-
mat1
==
answer
);
ASSERT_TRUE
(
mat1
==
values
);
}
TEST_F
(
MatrixTesting
,
MatrixPlusEqualMatrix
)
{
using
namespace
::
testing
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type rhs
{{
100
,
200
},
{
300
,
400
},
{
500
,
600
},
{
700
,
800
}};
answer_type answer
{{
110
,
220
},
{
330
,
440
},
{
545
,
650
},
{
800
,
1000
}};
Matrix
::
Matrix
mat1
(
lhs
),
mat2
(
rhs
);
auto
&
ref
=
mat1
+=
mat2
;
ASSERT_TRUE
(
mat1
==
answer
);
ASSERT_EQ
(
&
mat1
,
&
ref
);
}
TEST_F
(
MatrixTesting
,
MatrixPlusMatrix
)
{
using
namespace
::
testing
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type rhs
{{
100
,
200
},
{
300
,
400
},
{
500
,
600
},
{
700
,
800
}};
answer_type answer
{{
110
,
220
},
{
330
,
440
},
{
545
,
650
},
{
800
,
1000
}};
Matrix
::
Matrix
mat1
(
lhs
),
mat2
(
rhs
);
auto sum
=
mat1
+
mat2
;
ASSERT_TRUE
(
sum
==
answer
);
}
TEST_F
(
MatrixTesting
,
MatrixMinusEqualMatrix
)
{
using
namespace
::
testing
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type rhs
{{
100
,
200
},
{
300
,
400
},
{
500
,
600
},
{
700
,
800
}};
answer_type answer
{{
-
90
,
-
180
},
{
-
270
,
-
360
},
{
-
455
,
-
550
},
{
-
600
,
-
600
}};
Matrix
::
Matrix
mat1
(
lhs
),
mat2
(
rhs
);
auto
&
ref
=
mat1
-=
mat2
;
ASSERT_TRUE
(
mat1
==
answer
);
ASSERT_EQ
(
&
mat1
,
&
ref
);
}
TEST_F
(
MatrixTesting
,
MatrixMinusMatrix
)
{
using
namespace
::
testing
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type rhs
{{
100
,
200
},
{
300
,
400
},
{
500
,
600
},
{
700
,
800
}};
answer_type answer
{{
-
90
,
-
180
},
{
-
270
,
-
360
},
{
-
455
,
-
550
},
{
-
600
,
-
600
}};
Matrix
::
Matrix
mat1
(
lhs
),
mat2
(
rhs
);
auto diff
=
mat1
-
mat2
;
ASSERT_TRUE
(
diff
==
answer
);
}
TEST_F
(
MatrixTesting
,
MatrixTimesEqualMatrix
)
{
using
namespace
::
testing
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type rhs
{{
100
,
200
,
300
},
{
400
,
500
,
600
}};
answer_type answer
{{
9000
,
12000
,
15000
},
{
19000
,
26000
,
33000
},
{
24500
,
34000
,
43500
},
{
90000
,
120000
,
150000
}};
Matrix
::
Matrix
mat1
(
lhs
),
mat2
(
rhs
);
auto
&
ref
=
mat1
*=
mat2
;
ASSERT_TRUE
(
mat1
==
answer
);
ASSERT_EQ
(
&
mat1
,
&
ref
);
}
TEST_F
(
MatrixTesting
,
MatrixTimesMatrix
)
{
using
namespace
::
testing
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
answer_type rhs
{{
100
,
200
,
300
},
{
400
,
500
,
600
}};
answer_type answer
{{
9000
,
12000
,
15000
},
{
19000
,
26000
,
33000
},
{
24500
,
34000
,
43500
},
{
90000
,
120000
,
150000
}};
Matrix
::
Matrix
mat1
(
lhs
),
mat2
(
rhs
);
auto prod
=
mat1
*
mat2
;
ASSERT_TRUE
(
prod
==
answer
);
}
TEST_F
(
MatrixTesting
,
MatrixPlusEqualScalar
)
{
using
namespace
::
testing
;
using
elem_type
=
Matrix
::
Matrix
::
element_type
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
elem_type rhs
(
100
);
answer_type answer
{{
110
,
120
},
{
130
,
140
},
{
145
,
150
},
{
200
,
300
}};
Matrix
::
Matrix
mat1
(
lhs
);
auto
&
ref
=
mat1
+=
rhs
;
ASSERT_TRUE
(
mat1
==
answer
);
ASSERT_EQ
(
&
mat1
,
&
ref
);
}
TEST_F
(
MatrixTesting
,
MatrixPlusScalar
)
{
using
namespace
::
testing
;
using
elem_type
=
Matrix
::
Matrix
::
element_type
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
elem_type rhs
(
100
);
answer_type answer
{{
110
,
120
},
{
130
,
140
},
{
145
,
150
},
{
200
,
300
}};
Matrix
::
Matrix
mat1
(
lhs
);
auto sum
=
mat1
+
rhs
;
ASSERT_TRUE
(
sum
==
answer
);
}
TEST_F
(
MatrixTesting
,
MatrixMinusEqualScalar
)
{
using
namespace
::
testing
;
using
elem_type
=
Matrix
::
Matrix
::
element_type
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
elem_type rhs
(
10
);
answer_type answer
{{
0
,
10
},
{
20
,
30
},
{
35
,
40
},
{
90
,
190
}};
Matrix
::
Matrix
mat1
(
lhs
);
auto
&
ref
=
mat1
-=
rhs
;
ASSERT_TRUE
(
mat1
==
answer
);
ASSERT_EQ
(
&
mat1
,
&
ref
);
}
TEST_F
(
MatrixTesting
,
MatrixMinusScalar
)
{
using
namespace
::
testing
;
using
elem_type
=
Matrix
::
Matrix
::
element_type
;
answer_type lhs
{{
10
,
20
},
{
30
,
40
},
{
45
,
50
},
{
100
,
200
}};
elem_type rhs
(
10
);
answer_type answer
{{
0
,
10
},
{
20
,
30
},
{
35
,
40
},
{
90
,
190
}};
Matrix
::
Matrix
mat1
(
lhs
);
auto diff
=
mat1
-
rhs
;
ASSERT_TRUE
(
diff
==
answer
);
}
TEST_F
(
MatrixTesting
,
MatrixTimesEqualScalar
)
{