diff --git a/Vector_Matrix/.idea/Vector_Matrix.iml b/Combined/.idea/Vector_Matrix.iml similarity index 100% rename from Vector_Matrix/.idea/Vector_Matrix.iml rename to Combined/.idea/Vector_Matrix.iml diff --git a/Vector_Matrix/.idea/misc.xml b/Combined/.idea/misc.xml similarity index 100% rename from Vector_Matrix/.idea/misc.xml rename to Combined/.idea/misc.xml diff --git a/Vector_Matrix/.idea/modules.xml b/Combined/.idea/modules.xml similarity index 100% rename from Vector_Matrix/.idea/modules.xml rename to Combined/.idea/modules.xml diff --git a/Vector_Matrix/.idea/workspace.xml b/Combined/.idea/workspace.xml similarity index 100% rename from Vector_Matrix/.idea/workspace.xml rename to Combined/.idea/workspace.xml diff --git a/Vector_Matrix/CMakeLists.txt b/Combined/CMakeLists.txt similarity index 80% rename from Vector_Matrix/CMakeLists.txt rename to Combined/CMakeLists.txt index cf3e87f..e6fc3a2 100644 --- a/Vector_Matrix/CMakeLists.txt +++ b/Combined/CMakeLists.txt @@ -1,10 +1,10 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # The next lines represent a list of all the executables in the folder (chapter) # There is typically one executable per exercise. # As you solve exercises, you need to add them here. # The syntax is add_executable(executable_name source_file_name) -add_executable(Vector_Matrix main.cpp Vector.cpp Matrix.cpp Exception.cpp) +add_executable(COMBINED main.cpp Vector_Matrix.cpp LinSys.cpp) diff --git a/Combined/LinSys.cpp b/Combined/LinSys.cpp new file mode 100644 index 0000000..e35f8c1 --- /dev/null +++ b/Combined/LinSys.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +#include "LinSys.hpp" + +/************************************** +*************************************** +*************************************** +************* LINSYS CLASS ************ +*************************************** +*************************************** +**************************************/ + +// Overridden copy constructor +// Allocates memory for new matrix, and copies +// entries of other matrix into it + +Lin_Sys::Lin_Sys(const Lin_Sys &otherLin_Sys) { + /* Copying of Linear system*/ + mSize=otherLin_Sys.mSize; + mpA=otherLin_Sys.mpA; + mpb=otherLin_Sys.mpb; + +} + +Lin_Sys::Lin_Sys(Matrix A ,Vector b) { + mpA=A; + mpb=b; + mSize=A.NumberOfRows(); +} + +Lin_Sys::Lin_Sys(int size) { + mSize=size; + mpb=Vector(size); + mpA=Matrix(size); +} + + +Vector Lin_Sys::Solve (std::string method) { + +} \ No newline at end of file diff --git a/Combined/LinSys.hpp b/Combined/LinSys.hpp new file mode 100644 index 0000000..acfcf9c --- /dev/null +++ b/Combined/LinSys.hpp @@ -0,0 +1,25 @@ +#ifndef LINSYSHEADERDEF +#define LINSYSHEADERDEF + +#include "Vector_Matrix.hpp" +#include "string" + +/************************************** +*************************************** +******* LINEAR SYSTEM CLASS *********** +*************************************** +**************************************/ + +class Lin_Sys{ +private: +public: + int mSize; + Matrix mpA=Matrix(mSize); + Vector mpb=Vector(mSize); + Lin_Sys(const Lin_Sys& otherLin_Sys); + Lin_Sys(Matrix A, Vector b); + Lin_Sys(int size); + virtual Vector Solve(std::string method); +}; + +#endif \ No newline at end of file diff --git a/Combined/Vector_Matrix.cpp b/Combined/Vector_Matrix.cpp new file mode 100644 index 0000000..3b3b317 --- /dev/null +++ b/Combined/Vector_Matrix.cpp @@ -0,0 +1,603 @@ +#include +#include +#include +#include +#include "Vector_Matrix.hpp" +#include "LinSys.hpp" + + + /************************************** + *************************************** + *************************************** + ****** FUNCTION PROTOTYPE ************* + *************************************** + *************************************** + **************************************/ +double** AllocateMemory(int rows, int cols); + + +/************************************** +*************************************** +*************************************** +************* MATRIX CLASS ************ +*************************************** +*************************************** +*************************************** +*************************************** +**************************************/ + +// Overridden copy constructor +// Allocates memory for new matrix, and copies +// entries of other matrix into it + +Matrix::Matrix(const Matrix& otherMatrix) +{ + /* Copying of Matrix size*/ + mNumCols = otherMatrix.NumberOfColumns(); + mNumRows = otherMatrix.NumberOfRows(); + + /*Allocation of memory for copy of matrix*/ + mData=AllocateMemory(mNumRows,mNumCols) ; + + /* Copying of class data*/ + for (int i=0; i 0 && cols>0 )&& "Define positive integer values for both arguments defining the size of the matrix."); + mNumCols = cols; + mNumRows=rows; + mData=AllocateMemory(mNumRows,mNumCols) ; + + for (int i=0; i 0 && "\nDefine a positive integer value for the square matrix size."); + mNumCols = size; + mNumRows=size; + mData=AllocateMemory(mNumRows,mNumCols) ; + + for (int i=0; i=mNumRows){ + if (i_index==0){ + throw (Exception("Index i ","Index i of A(i,j) is out of bounds. (1-based indexing)")); + } else { + throw (Exception("Index j ","Index j of A(i,j) is out of bounds. (1-based indexing)")); + } + } else { + return i_val; + } + } catch (Exception& error){ + error.PrintDebug(); + std::cout<<"\n\nIndex error.\nSee error report and give alternative index value\n\n"; + int j; + std::cin>>j; + while (j<=0 || j>mNumRows){ + std::cout<<"\nIndexes are still out of bounds, give alternative values ...\n"; + std::cin>>j; + } + return j-1; + } +} + +// Read-only variant of [][] +// Note that this uses ‘zero-based’ indexing, +// and a check on the validity of the index + +double Matrix::Read(int i, int j) const +{ + return mData[CheckIndex(0,i)][CheckIndex(1,j)]; +} + + +// Overloading round brackets +// Note that this uses ‘one-based’ indexing, +// and a check on the validity of the index +double& Matrix::operator()(int i, int j) const +{ + return mData[CheckIndex(0,i-1)][CheckIndex(1,j-1)]; +} + +// Overloading the assignment operator +Matrix& Matrix::operator=(const Matrix& otherVector) +{ + assert((mNumRows == otherVector.mNumRows && mNumCols==otherVector.mNumCols) && "Matrices are of different sizes. Cant assign"); + + for (int i=0; i 0&& "Assign positive integer value for vector size."); + mSize = size; + mData = new double [mSize]; + for (int i=0; i=mSize){ + if (i_index==0){ + throw (Exception(" i","Index i of vector u[i] is out of bounds. (0-based indexing)")); + }else { + throw (Exception("Index i","Index i of vector u[i] is out of bounds. (1-based indexing)")); + } + } else { + return i_val; + } + } catch (Exception& error){ + error.PrintDebug(); + std::cout<<"\nIndex error. See error report and give an alternative index value\n\n"; + int j; + std::cin>>j; + while (j<=-1 || j>=mSize){ + std::cout<<"\nIndex is still out of bounds, give an alternative value ...\n"; + std::cin>>j; + } + return j; + } +} + +// Overloading square brackets +// Note that this uses ‘zero-based’ indexing, +// and a check on the validity of the index +double& Vector::operator[](int i) const +{ + return mData[CheckIndex(0,i)]; +} + +// Read-only variant of [] +// Note that this uses ‘zero-based’ indexing, +// and a check on the validity of the index + +double Vector::Read(int i) const +{ + return mData[CheckIndex(0,i)]; +} + + +// Overloading round brackets +// Note that this uses ‘one-based’ indexing, +// and a check on the validity of the index +double& Vector::operator()(int i) +{ + return mData[CheckIndex(1,i-1)]; +} + +// Overloading the assignment operator +Vector& Vector::operator=(const Vector& otherVector) +{ + assert(mSize == otherVector.mSize&& "Vectors are of different sizes. Cant assign."); + for (int i=0; i + +class Matrix; +class Vector; + +/************************************** +*************************************** +************* MATRIX CLASS ************ +*************************************** +**************************************/ + +class Matrix +{ +private: + double** mData; // data stored in matrix + int mNumRows; // Number of rows + int mNumCols; // Number of cols +public: + Matrix(const Matrix& otherMatrix); + Matrix(int rows, int cols); + Matrix(int size); + //~Matrix(); + int NumberOfRows() const; + int NumberOfColumns() const; +// read-only zero-based indexing + int CheckIndex(int index,int val) const ; + double Read(int i,int j) const; + double& operator()(int i, int j) const; // one-based indexing +// assignment + Matrix& operator=(const Matrix& otherMatrix); + Matrix operator+() const; // unary + + Matrix operator-() const; // unary - +// binary operations + Matrix operator+(const Matrix& B) const; // binary + + Matrix operator-(const Matrix& B) const; // binary - + Matrix operator*(const Matrix& B) const; // binary * + friend std::ostream& operator<<(std::ostream& output, const Matrix& B); +// scalar multiplication + Matrix operator *(double a) const; +// Vector multiplication + Vector operator*(const Vector& u) const; // binary * +// Determinant method + double Determinant(); + void ReduceMatrix(double** A,int size,int col, const Matrix &A_hat); + void FreeMemory(); +// Memory allocation + double** AllocateMemory(int rows, int cols) const; +}; + +/************************************** +*************************************** +************* VECTOR CLASS ************ +*************************************** +**************************************/ + +class Vector +{ +private: + double * mData; // data stored in vector + int mSize; // size of vector +public: + Vector(const Vector& otherVector); + Vector(int size); + ~Vector(); + int GetSize() const; + double& operator[](int i) const; // zero-based indexing +// read-only zero-based indexing + double Read(int i) const; + int CheckIndex(int i_index,int i_val) const ; + double& operator()(int i); // one-based indexing +// assignment + Vector& operator=(const Vector& otherVector); + Vector operator+() const; // unary + + Vector operator-() const; // unary - + Vector operator+(const Vector& v1) const; // binary + + Vector operator-(const Vector& v1) const; // binary - + Vector operator*(const Vector& v1) const; // binary * + Vector operator/(const Vector& v1) const; // binary / + friend std::ostream& operator<<(std::ostream& output, const Vector& v); +// scalar multiplication + Vector operator *(double a) const; +// Matrix multiplication + Vector operator*(const Matrix& A) const; // binary * +// p-norm method + double CalculateNorm(int p=2) const; +// declare length function as a friend + friend int length(const Vector& v); + +}; + +/************************************** +*************************************** +********* FRIEND FUNCTIONS ************ +*************************************** +**************************************/ + +// Prototype signature of TerminalPrint() friend function +void TerminalPrint(Matrix v,std::string msg) ; +// Prototype signature of length() friend function +int length(const Vector& v); +// Prototype signature of TerminalPrint() friend function +void TerminalPrint(Vector v,std::string msg) ; +void TerminalPrint(double v,std::string msg) ; +void TerminalPrint(int v,std::string msg) ; + +/************************************** +*************************************** +************ ERROR HANDLING************ +*************************************** +**************************************/ + +class Exception +{ +private: + std::string mTag, mProblem; +public: + Exception(std::string tagString, std::string probString); + void PrintDebug() const; +}; +#endif \ No newline at end of file diff --git a/Vector_Matrix/cmake-build-debug/CMakeCache.txt b/Combined/cmake-build-debug/CMakeCache.txt similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeCache.txt rename to Combined/cmake-build-debug/CMakeCache.txt diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeCCompiler.cmake b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeCCompiler.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeCCompiler.cmake rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeCCompiler.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeCXXCompiler.cmake b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeCXXCompiler.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeCXXCompiler.cmake rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeCXXCompiler.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_C.bin b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_C.bin similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_C.bin rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_C.bin diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_CXX.bin b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_CXX.bin similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_CXX.bin rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeDetermineCompilerABI_CXX.bin diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeSystem.cmake b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeSystem.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CMakeSystem.cmake rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CMakeSystem.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/CMakeCCompilerId.c b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/CMakeCCompilerId.c similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/CMakeCCompilerId.c rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/CMakeCCompilerId.c diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/a.out b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/a.out similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/a.out rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdC/a.out diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/CMakeCXXCompilerId.cpp b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/CMakeCXXCompilerId.cpp similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/CMakeCXXCompilerId.cpp rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/CMakeCXXCompilerId.cpp diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/a.out b/Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/a.out similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/a.out rename to Combined/cmake-build-debug/CMakeFiles/3.8.2/CompilerIdCXX/a.out diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake b/Combined/cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake rename to Combined/cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/CMakeOutput.log b/Combined/cmake-build-debug/CMakeFiles/CMakeOutput.log similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/CMakeOutput.log rename to Combined/cmake-build-debug/CMakeFiles/CMakeOutput.log diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Makefile.cmake b/Combined/cmake-build-debug/CMakeFiles/Makefile.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Makefile.cmake rename to Combined/cmake-build-debug/CMakeFiles/Makefile.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Makefile2 b/Combined/cmake-build-debug/CMakeFiles/Makefile2 similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Makefile2 rename to Combined/cmake-build-debug/CMakeFiles/Makefile2 diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/TargetDirectories.txt b/Combined/cmake-build-debug/CMakeFiles/TargetDirectories.txt similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/TargetDirectories.txt rename to Combined/cmake-build-debug/CMakeFiles/TargetDirectories.txt diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/CXX.includecache b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/CXX.includecache similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/CXX.includecache rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/CXX.includecache diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/DependInfo.cmake b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/DependInfo.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/DependInfo.cmake rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/DependInfo.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Exception.o b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Exception.o similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Exception.o rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Exception.o diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Matrix.o b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Matrix.o similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Matrix.o rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Matrix.o diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Vector.o b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Vector.o similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Vector.o rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/Vector.o diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/build.make b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/build.make similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/build.make rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/build.make diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/cmake_clean.cmake b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/cmake_clean.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/cmake_clean.cmake rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/cmake_clean.cmake diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.internal b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.internal similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.internal rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.internal diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.make b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.make similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.make rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/depend.make diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/flags.make b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/flags.make similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/flags.make rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/flags.make diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/link.txt b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/link.txt similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/link.txt rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/link.txt diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/main.o b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/main.o similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/main.o rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/main.o diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/progress.make b/Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/progress.make similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/progress.make rename to Combined/cmake-build-debug/CMakeFiles/Vector_Matrix.dir/progress.make diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/clion-environment.txt b/Combined/cmake-build-debug/CMakeFiles/clion-environment.txt similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/clion-environment.txt rename to Combined/cmake-build-debug/CMakeFiles/clion-environment.txt diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/clion-log.txt b/Combined/cmake-build-debug/CMakeFiles/clion-log.txt similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/clion-log.txt rename to Combined/cmake-build-debug/CMakeFiles/clion-log.txt diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/cmake.check_cache b/Combined/cmake-build-debug/CMakeFiles/cmake.check_cache similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/cmake.check_cache rename to Combined/cmake-build-debug/CMakeFiles/cmake.check_cache diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/feature_tests.bin b/Combined/cmake-build-debug/CMakeFiles/feature_tests.bin similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/feature_tests.bin rename to Combined/cmake-build-debug/CMakeFiles/feature_tests.bin diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/feature_tests.c b/Combined/cmake-build-debug/CMakeFiles/feature_tests.c similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/feature_tests.c rename to Combined/cmake-build-debug/CMakeFiles/feature_tests.c diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/feature_tests.cxx b/Combined/cmake-build-debug/CMakeFiles/feature_tests.cxx similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/feature_tests.cxx rename to Combined/cmake-build-debug/CMakeFiles/feature_tests.cxx diff --git a/Vector_Matrix/cmake-build-debug/CMakeFiles/progress.marks b/Combined/cmake-build-debug/CMakeFiles/progress.marks similarity index 100% rename from Vector_Matrix/cmake-build-debug/CMakeFiles/progress.marks rename to Combined/cmake-build-debug/CMakeFiles/progress.marks diff --git a/Vector_Matrix/cmake-build-debug/Makefile b/Combined/cmake-build-debug/Makefile similarity index 100% rename from Vector_Matrix/cmake-build-debug/Makefile rename to Combined/cmake-build-debug/Makefile diff --git a/Vector_Matrix/cmake-build-debug/Project.cbp b/Combined/cmake-build-debug/Project.cbp similarity index 100% rename from Vector_Matrix/cmake-build-debug/Project.cbp rename to Combined/cmake-build-debug/Project.cbp diff --git a/Vector_Matrix/cmake-build-debug/Vector_Matrix b/Combined/cmake-build-debug/Vector_Matrix similarity index 100% rename from Vector_Matrix/cmake-build-debug/Vector_Matrix rename to Combined/cmake-build-debug/Vector_Matrix diff --git a/Vector_Matrix/cmake-build-debug/cmake_install.cmake b/Combined/cmake-build-debug/cmake_install.cmake similarity index 100% rename from Vector_Matrix/cmake-build-debug/cmake_install.cmake rename to Combined/cmake-build-debug/cmake_install.cmake diff --git a/Combined/main.cpp b/Combined/main.cpp new file mode 100644 index 0000000..cdb2e15 --- /dev/null +++ b/Combined/main.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include "Vector_Matrix.hpp" +#include "LinSys.hpp" + + + +int main(int argc, char *argv[]){ + + // Define size of system + int size=3; + //Instances of vector class + Vector b(size); + Vector c(size); + //Value assignment + b[0]=0;b[1]=5;b[2]=10; + c[0]=10;c[1]=-9;c[2]=8; + //Use of function TerminalPrint(Vector u, string) + TerminalPrint(b,"Vecteur b :"); + TerminalPrint(c,"Vecteur c :"); + // Operators available for vector class : +, -, *, /, << (Check Vector_Matrix.hpp for details) + TerminalPrint(b+c,"Vecteur b+c :"); + + //Instances of matrix classes + Matrix A(size); + Matrix B(size); + //Value assignment (1 based indexing, like Matlab) + A(1,1)=1;A(1,2)=1;A(1,3)=2; + A(2,1)=3;A(2,2)=4;A(2,3)=5; + A(3,1)=6;A(3,2)=7;A(3,3)=8; + B(1,1)=7;B(1,2)=-4;B(1,3)=3; + B(2,1)=11;B(2,2)=8;B(2,3)=-8; + B(3,1)=-6;B(3,2)=2;B(3,3)=13; + + //Use of function TerminalPrint(Matrix A, string) + TerminalPrint(A*b,"Vecteur A*b :"); + TerminalPrint(B,"Matrice B :"); + // Operators available for Matrix class : +, -, *, << (Check Vector_Matrix.hpp for details) + TerminalPrint(A*B,"Matrices A*B :"); + // Determinant method for matrices + std::cout<<"Determinant : "< -#include "Exception.hpp" -//Constructor -Exception::Exception(std::string tagString, - std::string probString) -{ - mTag = tagString; - mProblem = probString; -} - -void Exception::PrintDebug() const -{ - std::cerr << "** Error ("< - -class Exception -{ -private: - std::string mTag, mProblem; -public: - Exception(std::string tagString, std::string probString); - void PrintDebug() const; -}; -#endif //EXCEPTIONDEF \ No newline at end of file diff --git a/Vector_Matrix/Matrix.cpp b/Vector_Matrix/Matrix.cpp deleted file mode 100644 index f797003..0000000 --- a/Vector_Matrix/Matrix.cpp +++ /dev/null @@ -1,288 +0,0 @@ -#include -#include -#include -#include -#include "Matrix.hpp" -#include "Exception.hpp" - -// Overridden copy constructor -// Allocates memory for new matrix, and copies -// entries of other matrix into it -double** AllocateMemory(int rows, int cols){ - /*Allocates memory for matrix A*/ - double** A; - A=new double* [rows]; - for (int i=0;i 0 && cols>0); - mNumCols = cols; - mNumRows=rows; - mData=AllocateMemory(mNumRows,mNumCols) ; - - for (int i=0; i 0); - mNumCols = size; - mNumRows=size; - mData=AllocateMemory(mNumRows,mNumCols) ; - - for (int i=0; i=mNumRows){ - throw (Exception("Index","Index is out of bounds")); - } else { - return i; - } - } catch (Exception& error){ - error.PrintDebug(); - std::cout<<"\nGive an alternative index value\n\n"; - int j; - std::cin>>j; - while (j<=-1 || j>=mNumRows){ - std::cout<<"\nIndex is still out of bounds, give an alternative value ...\n"; - std::cin>>j; - } - return j; - } -} - - - -// Read-only variant of [][] -// Note that this uses ‘zero-based’ indexing, -// and a check on the validity of the index - -double Matrix::Read(int i, int j) const -{ - return mData[CheckIndex(i)][CheckIndex(j)]; -} - - -// Overloading round brackets -// Note that this uses ‘one-based’ indexing, -// and a check on the validity of the index -double& Matrix::operator()(int i, int j) -{ - return mData[CheckIndex(i-1)][CheckIndex(j-1)]; -} - -// Overloading the assignment operator -Matrix& Matrix::operator=(const Matrix& otherVector) -{ - assert(mNumRows == otherVector.mNumRows && mNumCols==otherVector.mNumCols); - for (int i=0; i - -class Matrix -{ -private: - double** mData; // data stored in matrix - int mNumRows; // Number of rows - int mNumCols; // Number of cols -public: - Matrix(const Matrix& otherMatrix); - Matrix(int rows, int cols); - Matrix(int size); - ~Matrix(); - int NumberOfRows() const; - int NumberOfColumns() const; -// read-only zero-based indexing - int CheckIndex(int i) const ; - double Read(int i,int j) const; - double& operator()(int i, int j); // one-based indexing -// assignment - Matrix& operator=(const Matrix& otherMatrix); - Matrix operator+() const; // unary + - Matrix operator-() const; // unary - - Matrix operator+(const Matrix& B) const; // binary + - Matrix operator-(const Matrix& B) const; // binary - - Matrix operator*(const Matrix& B) const; // binary * - friend std::ostream& operator<<(std::ostream& output, const Matrix& B); -// scalar multiplication - Matrix operator *(double a) const; -// Determinant method - friend void ReduceMatrix(double** A,int size,int col, double** &A_hat); - void FreeMemory(double** A,int rows) const; - double Determinant(double** A, int size) const; -// Memory allocation - friend double** AllocateMemory(int rows, int cols); -}; - -// Prototype signature of TerminalPrint() friend function -void TerminalPrint(Matrix v,std::string msg) ; - -#endif \ No newline at end of file diff --git a/Vector_Matrix/Vector.cpp b/Vector_Matrix/Vector.cpp deleted file mode 100644 index e712b65..0000000 --- a/Vector_Matrix/Vector.cpp +++ /dev/null @@ -1,217 +0,0 @@ -#include -#include -#include -#include -#include "Vector.hpp" -#include "Exception.hpp" - -// Overridden copy constructor -// Allocates memory for new vector, and copies -// entries of other vector into it - -Vector::Vector(const Vector& otherVector) -{ - mSize = otherVector.GetSize(); - mData = new double [mSize]; - for (int i=0; i 0); - mSize = size; - mData = new double [mSize]; - for (int i=0; i=mSize){ - throw (Exception("Index","Index is out of bounds")); - } else { - return i; - } - } catch (Exception& error){ - error.PrintDebug(); - std::cout<<"\nGive an alternative index value\n\n"; - int j; - std::cin>>j; - while (j<=-1 || j>=mSize){ - std::cout<<"\nIndex is stille out of bounds, give an alternative value ...\n"; - std::cin>>j; - } - return j; - } -} - -// Overloading square brackets -// Note that this uses ‘zero-based’ indexing, -// and a check on the validity of the index -double& Vector::operator[](int i) -{ - return mData[CheckIndex(i)]; -} - -// Read-only variant of [] -// Note that this uses ‘zero-based’ indexing, -// and a check on the validity of the index - -double Vector::Read(int i) const -{ - return mData[CheckIndex(i)]; -} - - -// Overloading round brackets -// Note that this uses ‘one-based’ indexing, -// and a check on the validity of the index -double& Vector::operator()(int i) -{ - return mData[CheckIndex(i-1)]; -} - -// Overloading the assignment operator -Vector& Vector::operator=(const Vector& otherVector) -{ - assert(mSize == otherVector.mSize); - for (int i=0; i - -class Vector -{ -private: - double * mData; // data stored in vector - int mSize; // size of vector -public: - Vector(const Vector& otherVector); - Vector(int size); - ~Vector(); - int GetSize() const; - double& operator[](int i); // zero-based indexing -// read-only zero-based indexing - double Read(int i) const; - int CheckIndex(int i) const ; - double& operator()(int i); // one-based indexing -// assignment - Vector& operator=(const Vector& otherVector); - Vector operator+() const; // unary + - Vector operator-() const; // unary - - Vector operator+(const Vector& v1) const; // binary + - Vector operator-(const Vector& v1) const; // binary - - Vector operator*(const Vector& v1) const; // binary * - Vector operator/(const Vector& v1) const; // binary / - friend std::ostream& operator<<(std::ostream& output, const Vector& v); -// scalar multiplication - Vector operator *(double a) const; -// p-norm method - double CalculateNorm(int p=2) const; -// declare length function as a friend - friend int length(const Vector& v); - -}; - -// Prototype signature of length() friend function -int length(const Vector& v); -// Prototype signature of TerminalPrint() friend function -void TerminalPrint(Vector v,std::string msg) ; - -#endif \ No newline at end of file diff --git a/Vector_Matrix/main.cpp b/Vector_Matrix/main.cpp deleted file mode 100644 index aff953a..0000000 --- a/Vector_Matrix/main.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -#include "Vector.hpp" -#include "Matrix.hpp" - - -int main(int argc, char *argv[]){ - int size=3; - Vector b(size); - Vector c(size); - b[0]=0;b[1]=5;b[2]=10; - c[0]=10;c[1]=-9;c[2]=8; - TerminalPrint(b,"Vecteur b :"); - TerminalPrint(c,"Vecteur c :"); - TerminalPrint(b+c,"Vecteur b+c :"); - - Matrix A(size); - Matrix B(size); - A(1,1)=1;A(1,2)=1;A(1,3)=2; - A(2,1)=3;A(2,2)=4;A(2,3)=5; - A(3,1)=6;A(3,2)=7;A(3,3)=8; - - B(1,1)=7;B(1,2)=-4;B(1,3)=3; - B(2,1)=11;B(2,2)=8;B(2,3)=-8; - B(3,1)=-6;B(3,2)=2;B(3,3)=13; - - - TerminalPrint(A,"Matrice A :"); - TerminalPrint(B,"Matrice B :"); - TerminalPrint(A*B,"Matrices A*B :"); - - - return 0; -} \ No newline at end of file