From fac9a388e776297084f7dd03d244e071d5916724 Mon Sep 17 00:00:00 2001 From: dhern023 Date: Wed, 11 Aug 2021 15:22:29 -0400 Subject: [PATCH 1/2] init commit --- CMakeLists.txt | 1 + install_manifest.txt | 2 - scientific_computing/ComplexNumber.cpp | 155 +++++++++++++++++++++++++ scientific_computing/ComplexNumber.hpp | 39 +++++++ scientific_computing/SquareMatrix.cpp | 68 +++++++++++ scientific_computing/SquareMatrix.h | 42 +++++++ 6 files changed, 305 insertions(+), 2 deletions(-) delete mode 100644 install_manifest.txt create mode 100644 scientific_computing/ComplexNumber.cpp create mode 100644 scientific_computing/ComplexNumber.hpp create mode 100644 scientific_computing/SquareMatrix.cpp create mode 100644 scientific_computing/SquareMatrix.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2089dd5..e05b096 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ include_directories( file(GLOB all_SRCS ${PROJECT_SOURCE_DIR}/scientific_computing/*.h ${PROJECT_SOURCE_DIR}/scientific_computing/*.cpp + ${PROJECT_SOURCE_DIR}/scientific_computing/*.hpp ) include(FetchContent) diff --git a/install_manifest.txt b/install_manifest.txt deleted file mode 100644 index 7be8706..0000000 --- a/install_manifest.txt +++ /dev/null @@ -1,2 +0,0 @@ -/usr/local/lib/liblinear_algebra.a -/usr/local/lib/libnumerical_solvers.a \ No newline at end of file diff --git a/scientific_computing/ComplexNumber.cpp b/scientific_computing/ComplexNumber.cpp new file mode 100644 index 0000000..c8eafa9 --- /dev/null +++ b/scientific_computing/ComplexNumber.cpp @@ -0,0 +1,155 @@ +#include "ComplexNumber.hpp" + + +// Override default constructor +// Set real and imaginary parts to zero +ComplexNumber::ComplexNumber() +{ + mRealPart = 0.0; + mImaginaryPart = 0.0; +} + +// Constructor that sets complex number z=x+iy +ComplexNumber::ComplexNumber(double x, double y) +{ + mRealPart = x; + mImaginaryPart = y; +} + +// Method for computing the modulus of a +// complex number +double ComplexNumber::CalculateModulus() const +{ + return sqrt(mRealPart*mRealPart+ + mImaginaryPart*mImaginaryPart); +} + +// Method for computing the argument of a +// complex number +double ComplexNumber::CalculateArgument() const +{ + return atan2(mImaginaryPart, mRealPart); +} + +// Method for raising complex number to the power n +// using De Moivre's theorem - first complex +// number must be converted to polar form +ComplexNumber ComplexNumber::CalculatePower(double n) const +{ + double modulus = CalculateModulus(); + double argument = CalculateArgument(); + double mod_of_result = pow(modulus, n); + double arg_of_result = argument*n; + double real_part = mod_of_result*cos(arg_of_result); + double imag_part = mod_of_result*sin(arg_of_result); + ComplexNumber z(real_part, imag_part); + return z; +} + +// Overloading the = (assignment) operator +ComplexNumber& ComplexNumber:: + operator=(const ComplexNumber& z) +{ + mRealPart = z.mRealPart; + mImaginaryPart = z.mImaginaryPart; + return *this; +} + +// Overloading the unary - operator +ComplexNumber ComplexNumber::operator-() const +{ + ComplexNumber w; + w.mRealPart = -mRealPart; + w.mImaginaryPart = -mImaginaryPart; + return w; +} + +// Overloading the binary + operator +ComplexNumber ComplexNumber:: + operator+(const ComplexNumber& z) const +{ + ComplexNumber w; + w.mRealPart = mRealPart + z.mRealPart; + w.mImaginaryPart = mImaginaryPart + z.mImaginaryPart; + return w; +} + +// Overloading the binary - operator +ComplexNumber ComplexNumber:: + operator-(const ComplexNumber& z) const +{ + ComplexNumber w; + w.mRealPart = mRealPart - z.mRealPart; + w.mImaginaryPart = mImaginaryPart - z.mImaginaryPart; + return w; +} + +// Overloading the insertion << operator +std::ostream& operator<<(std::ostream& output, + const ComplexNumber& z) +{ + // Format as "(a + bi)" or as "(a - bi)" + output << "(" << z.mRealPart << " "; + if (z.mImaginaryPart >= 0.0) + { + output << "+ " << z.mImaginaryPart << "i)"; + } + else + { + // z.mImaginaryPart < 0.0 + // Replace + with minus sign + output << "- " << -z.mImaginaryPart << "i)"; + } + return output; +} + +//Code from Chapter06.tex line 986 save as ComplexNumber.cpp + +//Code from chapter6 exercises =========================================================== + +double ComplexNumber::GetRealPart() const +{ + return mRealPart; +} + +double ComplexNumber::GetImaginaryPart() const +{ + return mImaginaryPart; +} + +double RealPart(ComplexNumber& z) +{ + return z.mRealPart; +} + +double ImaginaryPart(ComplexNumber& z) +{ + return z.mImaginaryPart; +} + +ComplexNumber::ComplexNumber(const ComplexNumber& z) +{ + mRealPart = z.mRealPart; + mImaginaryPart = z.mImaginaryPart; +} + +ComplexNumber::ComplexNumber(double x) +{ + mRealPart = x; + mImaginaryPart = 0; +} + +ComplexNumber ComplexNumber::CalculateConjugate(const ComplexNumber& z) +{ + ComplexNumber w; + w.mRealPart = z.mRealPart; + w.mImaginaryPart = -z.mImaginaryPart; + return w; +} + +void ComplexNumber::SetToConjugate(ComplexNumber& z) +{ + z.mImaginaryPart = -z.mImaginaryPart; +} + +//std::vector< std::vector< ComplexNumber>> ComplexNumber::CalculateMatrixExponential(std::vector< std::vector< ComplexNumber>>); diff --git a/scientific_computing/ComplexNumber.hpp b/scientific_computing/ComplexNumber.hpp new file mode 100644 index 0000000..11568c8 --- /dev/null +++ b/scientific_computing/ComplexNumber.hpp @@ -0,0 +1,39 @@ +#ifndef COMPLEXNUMBERHEADERDEF +#define COMPLEXNUMBERHEADERDEF + +#include +#include +#include + +class ComplexNumber +{ +private: + double mRealPart; + double mImaginaryPart; +public: + ComplexNumber(); + ComplexNumber(double x, double y); + double CalculateModulus() const; + double CalculateArgument() const; + ComplexNumber CalculatePower(double n) const; + ComplexNumber& operator=(const ComplexNumber& z); + ComplexNumber operator-() const; + ComplexNumber operator+(const ComplexNumber& z) const; + ComplexNumber operator-(const ComplexNumber& z) const; + friend std::ostream& operator<<(std::ostream& output, + const ComplexNumber& z); + //Chapter 6 Exercises ================================== + double GetRealPart() const; //access parts + double GetImaginaryPart() const; + friend double RealPart(ComplexNumber& z); //friend to access parts + friend double ImaginaryPart(ComplexNumber& z); + ComplexNumber(const ComplexNumber& z); //overwrite copy constructor + ComplexNumber(double x); //real number as complex a + 0i + ComplexNumber CalculateConjugate(const ComplexNumber& z); //return conjugate + void SetToConjugate(ComplexNumber& z); //conjugate in-place + //std::vector< std::vector< ComplexNumber>> CalculateMatrixExponential(std::vector< std::vector< ComplexNumber>>); + +}; + +#endif +//Code from Chapter06.tex line 946 save as ComplexNumber.hpp diff --git a/scientific_computing/SquareMatrix.cpp b/scientific_computing/SquareMatrix.cpp new file mode 100644 index 0000000..fa63639 --- /dev/null +++ b/scientific_computing/SquareMatrix.cpp @@ -0,0 +1,68 @@ +#include "SquareMatrix.h" + +// Constructors +SquareMatrix::SquareMatrix() +{ + matrix = {{0.0}}; + size = 1; +} + +SquareMatrix::SquareMatrix(size_t size, double value_default) +{ + //construct size x size with default value + initialize_matrix(value_default); + size = size; +} + +SquareMatrix::SquareMatrix(std::vector< std::vector< double>> A) +{ + //check matrix is square + assert(A.size() == A[1].size()); + + matrix = A; + size = A[0].size(); +} + +// Operators +//overload assignment operator +//overload unary substraction operator +//overload binary add/substraction + +// Methods +double SquareMatrix::CalculateDeterminant(SquareMatrix square_matrix) const +{ + return matrices::determinant(square_matrix.matrix); +} + +// SquareMatrix SquareMatrix::CalculateInterse(SquareMatrix square_matrix) const +// { + // return +// } + +ScaleMatrix(SquareMatrix& square_matrix, double scalar) const; +{ + // Overwrites private matrix variable + square_matrix.matrix = vector_spaces::scalar_multiplication(square_matrix.matrix, scalar); +} + +ScaleMatrix(double scalar, SquareMatrix& square_matrix) const; +{ + // Overwrites private matrix variable + square_matrix.matrix = vector_spaces::scalar_multiplication(square_matrix.matrix, scalar); +} + +SquareMatrix ScaleMatrix(SquareMatrix square_matrix, double scalar) const; +{ + SquareMatrix square_matrix_other; + square_matrix_other.matrix = vector_spaces::scalar_multiplication(square_matrix.matrix, scalar); + square_matrix_other.size = square_matrix.size; + return square_matrix_other; +} + +SquareMatrix ScaleMatrix(double scalar, SquareMatrix square_matrix) const; +{ + SquareMatrix square_matrix_other; + square_matrix_other.matrix = vector_spaces::scalar_multiplication(square_matrix.matrix, scalar); + square_matrix_other.size = square_matrix.size; + return square_matrix_other; +} \ No newline at end of file diff --git a/scientific_computing/SquareMatrix.h b/scientific_computing/SquareMatrix.h new file mode 100644 index 0000000..a79921f --- /dev/null +++ b/scientific_computing/SquareMatrix.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +#include +#include +#include + +class SquareMatrix +{ +private: + std::vector< std::vector< double>> matrix; + size_t size; + + // Methods + void initialize_matrix(double value_default) + { + std::vector< std::vector< double>> matrix(size, std::vector< double>(size, value_default)); + } + + +public: + + // Constructors + SquareMatrix(); + SquareMatrix(size_t size, double value_default); //construct size x size with default value + SquareMatrix(std::vector< std::vector< double>> A); //Pass in matrix to constructor + + // Operators + //overload assignment operator + //overload unary substraction operator``` + //overload binary add/substraction` + + // Methods + double CalculateDeterminant(SquareMatrix square_matrix) const; + //SquareMatrix CalculateInterse(SquareMatrix square_matrix) const; + ScaleMatrix(SquareMatrix& square_matrix, double scalar) const; + ScaleMatrix(double scalar, SquareMatrix& square_matrix) const; + SquareMatrix ScaleMatrix(SquareMatrix square_matrix, double scalar) const; + SquareMatrix ScaleMatrix(double scalar, SquareMatrix square_matrix) const; +}; \ No newline at end of file From 405e513e3ce4cd86ec3f530388514c192ee0479f Mon Sep 17 00:00:00 2001 From: dhern023 Date: Wed, 11 Aug 2021 16:51:35 -0400 Subject: [PATCH 2/2] added overload assignment operators --- scientific_computing/SquareMatrix.cpp | 7 +++++++ scientific_computing/SquareMatrix.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/scientific_computing/SquareMatrix.cpp b/scientific_computing/SquareMatrix.cpp index fa63639..b7f1df2 100644 --- a/scientific_computing/SquareMatrix.cpp +++ b/scientific_computing/SquareMatrix.cpp @@ -25,6 +25,13 @@ SquareMatrix::SquareMatrix(std::vector< std::vector< double>> A) // Operators //overload assignment operator +SquareMatrix& SquareMatrix::operator=(const SquareMatrix& square_matrix) +{ + matrix = square_matrix.matrix; + size = square_matrix.size; + return *this; +} + //overload unary substraction operator //overload binary add/substraction diff --git a/scientific_computing/SquareMatrix.h b/scientific_computing/SquareMatrix.h index a79921f..dbcf404 100644 --- a/scientific_computing/SquareMatrix.h +++ b/scientific_computing/SquareMatrix.h @@ -28,7 +28,7 @@ class SquareMatrix SquareMatrix(std::vector< std::vector< double>> A); //Pass in matrix to constructor // Operators - //overload assignment operator + SquareMatrix& operator=(const SquareMatrix& square_matrix); //overload assignment operator //overload unary substraction operator``` //overload binary add/substraction`