From 0bf7f7fdacdebb2f251269375b890671afc5348c Mon Sep 17 00:00:00 2001 From: Zakhar Khomenkov Date: Sun, 23 Apr 2023 21:51:03 +0300 Subject: [PATCH 1/5] sorry --- operator_overloading/Main.cpp | 220 ++++++++++++++++++++++++++++- operator_overloading/Matrix.cpp | 198 +++++++++++++++++++++++++- operator_overloading/Matrix.h | 40 ++++-- operator_overloading/Vector.cpp | 183 ++++++++++++++++++++++++ operator_overloading/Vector.h | 54 ++++++- operator_overloading/exception.cpp | 11 ++ operator_overloading/exception.h | 15 ++ 7 files changed, 702 insertions(+), 19 deletions(-) create mode 100644 operator_overloading/exception.cpp create mode 100644 operator_overloading/exception.h diff --git a/operator_overloading/Main.cpp b/operator_overloading/Main.cpp index ab75584..20244f2 100644 --- a/operator_overloading/Main.cpp +++ b/operator_overloading/Main.cpp @@ -1,11 +1,221 @@ -#include #include "Vector.h" -#include "Matrix.h" - - +#include +#include "CSLRMatrx.h" int main() { - // TODO: Выполнить тестирование + using namespace std; + + cout << "constructor" << endl; + Vector vec1(2, 5); + cout << vec1 << endl << endl; + // + cout << "operation = " << endl; + Vector vec2(3, 6); + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + vec1 = vec2; + cout << "vec1=vec2 -> vec1 =" << vec1 << endl << endl; + // + cout << "operation += " << endl; + Vector vec3(3, 8.4); + cout << "vec1 =" << vec1 << endl; + cout << "vec3 =" << vec3 << endl; + vec1 += vec3; + cout << "vec1+=vec3 =" << vec1 << endl << endl; + // + cout << "operation -= " << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec3 =" << vec3 << endl; + vec2 -= vec3; + cout << "vec2-=vec3 =" << vec2 << endl << endl; + // + cout << "unary - " << endl; + Vector vec4 = -vec1; + cout << "vec1 =" << vec1 << endl; + cout << "vec4= -vec1 -> vec4 =" << vec4 << endl << endl; + // + cout << "unary + " << endl; + Vector vec5 = +vec4; + cout << "vec4 =" << vec1 << endl; + cout << "vec5= +vec4 -> vec5 =" << vec4 << endl << endl; + // + cout << "binary - " << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec1 - vec2 =" << vec1 - vec2 << endl << endl; + // + cout << "binary + " << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec1 + vec2 =" << vec1 + vec2 << endl << endl; + // + cout << "multiplication a vector by a number" << endl; + double number = 3; + cout << "vec1 =" << vec1 << endl; + cout << "scalar =" << number << endl; + cout << "vec1 *scalar =" << vec1 * number << endl << endl; + // + cout << "multiplication a number by a vector" << endl; + number = 5; + cout << "vec1 =" << vec1 << endl; + cout << "scalar =" << number << endl; + cout << "scalar * vec1 =" << number * vec1 << endl << endl; + // + cout << "dot product of vectors" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec1*vec2 =" << vec1 * vec2 << endl << endl; + // + cout << "input of vector and vector pointer" << endl; + cin >> vec1; + cout << "vec1 =" << vec1 << endl; + double* ptr = vec1; + cout << "vec1 pointer is " << ptr << endl << endl; + // + cout << "separate element of vector" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "2nd element of vec1 is " << vec1[1] << endl << endl; + // + cout << "method of vector dimension" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec1 dimension is " << vec1.dimension() << endl << endl; + // + cout << "method of vector lenghth" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec1 lenghth is " << vec1.length() << endl << endl; + // + double* v1 = new double[7]; + v1[0] = 9; + v1[1] = 11; + v1[2] = 10; + v1[3] = 9; + v1[4] = 12; + v1[5] = 8; + v1[6] = 8; + Vector vector(7, 5); + double* v2 = new double[9]; + v2[0] = 1; + v2[1] = 2; + v2[2] = 1; + v2[3] = 2; + v2[4] = 1; + v2[5] = 1; + v2[6] = 2; + v2[7] = 2; + v2[8] = 3; + double* v3 = new double[9]; + v3[0] = 2; + v3[1] = 1; + v3[2] = 2; + v3[3] = 3; + v3[4] = 1; + v3[5] = 4; + v3[6] = 1; + v3[7] = 2; + v3[8] = 5; + double* v4 = new double[8]; + v4[0] = 1; + v4[1] = 1; + v4[2] = 1; + v4[3] = 2; + v4[4] = 5; + v4[5] = 7; + v4[6] = 7; + v4[7] = 10; + CSLRMatrix mat1(v1, v2, v3, v4); + + cout << "mat1:" << endl; + cout << mat1 << endl; + // + cout << "multiplication a matrix by a number" << endl; + number = 3; + cout << "scalar =" << number << endl; + + cout << "mat1 *scalar =\n" << mat1 * number << endl << endl; + // + cout << "multiplication a number by a matrix" << endl; + number = 5; + cout << "scalar =" << number << endl; + cout << "scalar * mat1 =\n" << number * mat1 << endl << endl; + // + cout << "multiplication a matrix by a vector" << endl; + cout << "vector =" << vector << endl; + cout << "vector * mat1 = " << mat1 * vector << endl << endl; + // + double* ve1 = new double[4]; + ve1[0] = 1; + ve1[1] = 2; + ve1[2] = 3; + ve1[3] = 4; + + double* ve2 = new double[2]; + ve2[0] = 2; + ve2[1] = 5; + + + double* ve3 = new double[2]; + ve3[0] = 1; + ve3[1] = 2; + double* ve4 = new double[5]; + ve4[0] = 1; + ve4[1] = 1; + ve4[2] = 1; + ve4[3] = 1; + ve4[4] = 3; + CSLRMatrix mat2(ve1, ve2, ve3, ve4); + cout << "operation = " << endl; + cout << "mat1 =\n" << mat1 << endl; + cout << "mat2 =\n" << mat2 << endl; + mat2 = mat1; + cout << "mat2=mat1 -> mat2 =\n" << mat2 << endl << endl; + // + cout << "method of matrix size" << endl; + cout << "mat1 dimension is " << mat1.matsize() << endl << endl; + // + cout << "method of matrix's non-zero elements" << endl; + cout << "number of mat1 non-zero elements is " << mat1.nzn() << endl << endl; + // + // + // + // + cout << "final test" << endl; + Vector vec(7, 0); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + vec[3] = 4; + vec[4] = 5; + vec[5] = 6; + vec[6] = 7; + Vector result(vec); + cout << "vector1 =" << vec << endl; + Vector vect(7, 0); + vect[0] = 14; + vect[1] = 6; + vect[2] = 2; + vect[3] = 11; + vect[4] = 3; + vect[5] = 3; + vect[6] = 1; + cout << "vector2 =" << vect << endl; + Vector vecto(7, 0); + vecto[0] = 5; + vecto[1] = 6; + vecto[2] = 4; + vecto[3] = 19; + vecto[4] = 6; + vecto[5] = 20; + vecto[6] = 5; + cout << "vector3 =" << vecto << endl; + int a = 4; + cout << "number1 =" << a << endl; + int b = 10; + cout << "number2 =" << b << endl; + int c = 10; + cout << "number3 =" << c << endl; + cout << "matrix =" << endl << mat1 << endl; + result = -a * mat1 * (vec - b * vect) + vecto * c; + cout << " -number1 * matrix * (vector1 - number2 * vector2) + vector3 * number3" << endl << result << endl; return 0; } \ No newline at end of file diff --git a/operator_overloading/Matrix.cpp b/operator_overloading/Matrix.cpp index 569f06b..3b7c0c4 100644 --- a/operator_overloading/Matrix.cpp +++ b/operator_overloading/Matrix.cpp @@ -1 +1,197 @@ -#include "Matrix.h" +#include +#include "exception.h" +#include "Vector.h" +#include "CSLRMatrx.h" +#include +using namespace std; + +CSLRMatrix::CSLRMatrix(double* p_adiag, double* p_altr, double* p_jptr, double* p_iptr) : + adiag(new double[_msize(p_adiag) / sizeof(double)]), + altr(new double[_msize(p_altr) / sizeof(double)]), + jptr(new double[_msize(p_jptr) / sizeof(double)]), + iptr(new double[_msize(p_iptr) / sizeof(double)]) +{ + for (int i = 0; i < _msize(p_adiag) / sizeof(double); ++i) + { + adiag[i] = p_adiag[i]; + } + for (int i = 0; i < _msize(p_altr) / sizeof(double); ++i) + { + altr[i] = p_altr[i]; + jptr[i] = p_jptr[i]; + } + for (int i = 0; i < _msize(p_iptr) / sizeof(double); ++i) + { + iptr[i] = p_iptr[i]; + } +} +CSLRMatrix::CSLRMatrix(const CSLRMatrix& p_mat) : + adiag(new double[_msize(p_mat.adiag) / sizeof(double)]), + altr(new double[_msize(p_mat.altr) / sizeof(double)]), + jptr(new double[_msize(p_mat.jptr) / sizeof(double)]), + iptr(new double[_msize(p_mat.iptr) / sizeof(double)]) +{ + for (int i = 0; i < _msize(p_mat.adiag) / sizeof(double); ++i) + { + adiag[i] = p_mat.adiag[i]; + } + for (int i = 0; i < _msize(p_mat.altr) / sizeof(double); ++i) + { + altr[i] = p_mat.altr[i]; + jptr[i] = p_mat.jptr[i]; + } + for (int i = 0; i < _msize(p_mat.iptr) / sizeof(double); ++i) + { + iptr[i] = p_mat.iptr[i]; + } +} + + + + +CSLRMatrix::~CSLRMatrix() { + delete[] adiag; + delete[] altr; + delete[] iptr; + delete[] jptr; +} + +std::istream& operator>>(std::istream& is, CSLRMatrix& m) { + + printf("\nenter the elements that are on the main diagonal\n"); + for (int i = 0; i < _msize(m.adiag) / sizeof(double); i++) + is >> m.adiag[i]; + printf("\nenter nonzero matrix elements\n"); + for (int i = 0; i < _msize(m.altr) / sizeof(double); i++) + is >> m.altr[i]; + printf("\nin which columns are these elements\n"); + for (int i = 0; i < _msize(m.jptr) / sizeof(double); i++) + is >> m.jptr[i]; + printf("\nWith what position in the array of all non - zero elements does the i-th line begin matrices\n"); + for (int i = 0; i < _msize(m.iptr) / sizeof(double); i++) + is >> m.iptr[i]; + return is; +} +std::ostream& operator<<(std::ostream& os, const CSLRMatrix& mat) { + for (int i = 0; i < _msize(mat.adiag) / sizeof(double); i++) { + for (int j = 0; j < _msize(mat.adiag) / sizeof(double); j++) { + double val = 0; + if (i == j) + { + val = mat.adiag[i]; + } + else + { + for (int k = mat.iptr[i] - 1; k < mat.iptr[i + 1] - 1; k++) { + if (mat.jptr[k] - 1 == j) { + val = mat.altr[k]; + break; + } + } + for (int k = mat.iptr[j] - 1; k < mat.iptr[j + 1] - 1; k++) { + if (mat.jptr[k] - 1 == i) { + val = mat.altr[k]; + break; + } + } + } + os << setw(4) << val; + } + os << std::endl << std::endl; + } + return os; +} + +int CSLRMatrix::matsize() const +{ + return _msize(adiag) / sizeof(double); +} +int CSLRMatrix::nzn() const +{ + int ans = _msize(altr) / sizeof(double) * 2; + for (int i = 0; i < _msize(adiag) / sizeof(double); i++) + { + if (adiag[i] != 0) ans++; + } + return ans; +} + +const CSLRMatrix& CSLRMatrix:: operator=(const CSLRMatrix& m) { + if (this == &m) { return *this; } + else + { + if (_msize(adiag) / sizeof(double) != sizeof(m.adiag) / sizeof(double)) + { + delete[](*this).adiag; + delete[](*this).altr; + delete[](*this).jptr; + delete[](*this).iptr; + (*this).adiag = new double[_msize(m.adiag) / sizeof(double)]; + (*this).altr = new double[_msize(m.altr) / sizeof(double)]; + (*this).jptr = new double[_msize(m.jptr) / sizeof(double)]; + (*this).iptr = new double[_msize(m.iptr) / sizeof(double)]; + } + for (int i = 0; i < _msize(m.adiag) / sizeof(double); ++i) + { + adiag[i] = m.adiag[i]; + } + for (int i = 0; i < _msize(m.altr) / sizeof(double); ++i) + { + altr[i] = m.altr[i]; + jptr[i] = m.jptr[i]; + } + for (int i = 0; i < _msize(m.iptr) / sizeof(double); ++i) + { + iptr[i] = m.iptr[i]; + } + return *this; + } + +} + +Vector operator*(const CSLRMatrix& mat, const Vector& v) +{ + if (mat.matsize() != v.dimension()) + { + throw IncompatibleDimException{}; + } + Vector res(v); + for (int i = 0; i < mat.matsize(); i++) + { + res[i] = v[i] * mat.adiag[i]; + } + for (int i = 0; i < mat.matsize(); i++) + { + for (int j = mat.iptr[i] - 1; j < mat.iptr[i + 1] - 1; j++) + { + res[i] += v[mat.jptr[j] - 1] * mat.altr[j]; + res[mat.jptr[j] - 1] += v[i] * mat.altr[j]; + } + } + return res; +} + +CSLRMatrix operator*(const CSLRMatrix& m, double scalar) +{ + CSLRMatrix result(m.adiag, m.altr, m.jptr, m.iptr); + for (int i = 0; i < _msize(result.adiag) / sizeof(double); i++) + { + result.adiag[i] *= scalar; + } + for (int i = 0; i < _msize(result.altr) / sizeof(double); i++) + { + result.altr[i] *= scalar; + } + return result; +} + +CSLRMatrix operator*(double scalar, const CSLRMatrix& m) +{ + return m * scalar; +} + + + + + + diff --git a/operator_overloading/Matrix.h b/operator_overloading/Matrix.h index 0a5fefd..5bad1e2 100644 --- a/operator_overloading/Matrix.h +++ b/operator_overloading/Matrix.h @@ -1,17 +1,35 @@ #pragma once +#include +#include "exception.h" +#include +#include "Vector.h" +class Vector; +class CSLRMatrix { +private: -// TODO: Выбрать класс в соответствии с вариантом + double* adiag; + double* altr; + double* jptr; + double* iptr; + friend class Vector; +public: -//class CSRMatrix -//{ -//public: -// -//}; + CSLRMatrix(double* p_adiag, double* p_altr, double* p_jptr, double* p_iptr); + CSLRMatrix(const CSLRMatrix& p_mat); + ~CSLRMatrix(); -//class CSLRMatrix -//{ -//public: -// -//}; \ No newline at end of file + friend std::istream& operator>>(std::istream& is, CSLRMatrix& m); + friend std::ostream& operator<<(std::ostream& os, const CSLRMatrix& mat); + + int matsize() const; + int nzn() const; + + const CSLRMatrix& operator=(const CSLRMatrix& m); + + friend Vector operator*(const CSLRMatrix& mat, const Vector& v); + friend CSLRMatrix operator*(double scalar, const CSLRMatrix& m); + friend CSLRMatrix operator*(const CSLRMatrix& m, double scalar); + +}; diff --git a/operator_overloading/Vector.cpp b/operator_overloading/Vector.cpp index c29aab7..4bd3a4b 100644 --- a/operator_overloading/Vector.cpp +++ b/operator_overloading/Vector.cpp @@ -1,2 +1,185 @@ +#include +#include "exception.h" +#include #include "Vector.h" +#include "Matrix.h" + + + + +Vector::Vector(int n, double value) { + size = n; + data = new double[size]; + for (int i = 0; i < size; i++) { + data[i] = value; + } +} + + +Vector::Vector(const Vector& v) { + size = v.size; + data = new double[size]; + for (int i = 0; i < size; i++) { + data[i] = v.data[i]; + } +} + + +Vector::~Vector() { + delete[] data; +} + + +Vector& Vector:: operator=(const Vector& v) { + if (this != &v) { + delete[] data; + size = v.size; + data = new double[size]; + for (int i = 0; i < size; i++) { + data[i] = v.data[i]; + } + } + return *this; +} + + +Vector& Vector::operator+=(const Vector& v) { + if (size != v.size) { + throw IncompatibleDimException(); + } + for (int i = 0; i < size; i++) { + data[i] += v.data[i]; + } + return *this; +} + + +Vector& Vector::operator-=(const Vector& v) { + if (size != v.size) { + throw IncompatibleDimException(); + } + for (int i = 0; i < size; i++) { + data[i] -= v.data[i]; + } + return *this; +} + + +Vector Vector::operator+() const { + return *this; +} + + +Vector Vector::operator-() const { + Vector result(size); + for (int i = 0; i < size; i++) { + result.data[i] = -data[i]; + } + return result; +} + + +Vector operator+(const Vector& v1, const Vector& v2) { + if (v1.size != v2.size) { + throw IncompatibleDimException(); + } + Vector result(v1); + result += v2; + return result; +} + + +Vector operator-(const Vector& v1, const Vector& v2) { + if (v1.size != v2.size) { + throw IncompatibleDimException(); + } + Vector result(v1); + result -= v2; + return result; +} + + +double operator*(const Vector& v1, const Vector& v2) { + double result = 0.0; + for (int i = 0; i < v1.size; i++) { + result += v1.data[i] * v2.data[i]; + } + return result; +} + +Vector operator*(const Vector& v, double scalar) { + Vector result(v); + for (int i = 0; i < v.size; i++) { + result.data[i] *= scalar; + } + return result; +} + + +Vector operator*(double scalar, const Vector& v) { + return v * scalar; +} + + + + +double& Vector::operator[](int index) { + if (index < 0 || index >= size) { + throw OutOfRangeException(); + } + return data[index]; +} + + +const double& Vector::operator[](int index) const { + if (index < 0 || index >= size) { + throw OutOfRangeException(); + } + return data[index]; +} + + +std::ostream& operator<<(std::ostream& os, const Vector& v) { + os << "["; + for (int i = 0; i < v.size; i++) { + os << v.data[i]; + if (i < v.size - 1) { + os << ", "; + } + } + os << "]"; + return os; +} + + +std::istream& operator>>(std::istream& is, Vector& v) { + for (int i = 0; i < v.size; i++) { + is >> v.data[i]; + } + return is; +} + + +Vector::operator double* () { + return data; +} + + +double Vector::length() const { + double result = 0.0; + for (int i = 0; i < size; i++) { + result += data[i] * data[i]; + } + return sqrt(result); +} + + +int Vector::dimension() const { + return size; +} + + + + + diff --git a/operator_overloading/Vector.h b/operator_overloading/Vector.h index 3f6a4a4..7023164 100644 --- a/operator_overloading/Vector.h +++ b/operator_overloading/Vector.h @@ -1,9 +1,59 @@ #pragma once - - +#include +#include +#include +#include "Matrix.h" +class CSLRMatrix; class Vector { + public: + Vector(int n = 0, double value = 0.0); + + Vector(const Vector& v); + + ~Vector(); + + double& operator[](int index); + + const double& operator[](int index) const; + + Vector& operator=(const Vector& v); + + Vector& operator+=(const Vector& v); + Vector& operator-=(const Vector& v); + + Vector operator+() const; + Vector operator-() const; + + friend Vector operator+(const Vector& v1, const Vector& v2); + friend Vector operator-(const Vector& v1, const Vector& v2); + + friend double operator*(const Vector& v1, const Vector& v2); + friend Vector operator*(const Vector& v, double scalar); + friend Vector operator*(double scalar, const Vector& v); + + //friend Vector operator*(const CSLRMatrix& m, const Vector& v); + + + + friend std::ostream& operator<<(std::ostream& os, const Vector& v); + friend std::istream& operator>>(std::istream& is, Vector& v); + + operator double* (); + + double length() const; + int dimension() const; + +private: + int size; + double* data; + friend class CSLRMatrix; }; + + + + + diff --git a/operator_overloading/exception.cpp b/operator_overloading/exception.cpp new file mode 100644 index 0000000..9f61887 --- /dev/null +++ b/operator_overloading/exception.cpp @@ -0,0 +1,11 @@ +#include +#include "exception.h" +const char* OutOfRangeException::what() const +{ + return "Index is out of range"; +} + +const char* IncompatibleDimException::what() const +{ + return "Vectors have incompatible dimensions"; +} diff --git a/operator_overloading/exception.h b/operator_overloading/exception.h new file mode 100644 index 0000000..678b266 --- /dev/null +++ b/operator_overloading/exception.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include +class OutOfRangeException : public std::exception +{ +public: + const char* what() const override; +}; + +class IncompatibleDimException : public std::exception +{ +public: + const char* what() const override; +}; -- GitLab From 5da91a41a57433008145066ea40e139d012ee9fb Mon Sep 17 00:00:00 2001 From: Zakhar Khomenkov Date: Tue, 25 Apr 2023 14:52:41 +0300 Subject: [PATCH 2/5] v2 --- operator_overloading/Main.cpp | 439 +++++++++++++++-------------- operator_overloading/Matrix.cpp | 215 +++++++------- operator_overloading/Matrix.h | 28 +- operator_overloading/Vector.cpp | 5 - operator_overloading/Vector.h | 4 - operator_overloading/exception.cpp | 1 + 6 files changed, 358 insertions(+), 334 deletions(-) diff --git a/operator_overloading/Main.cpp b/operator_overloading/Main.cpp index 20244f2..3836a15 100644 --- a/operator_overloading/Main.cpp +++ b/operator_overloading/Main.cpp @@ -1,221 +1,236 @@ #include "Vector.h" #include -#include "CSLRMatrx.h" int main() { using namespace std; + try + { + cout << "constructor" << endl; + Vector vec1(2, 5); + cout << vec1 << endl << endl; + // + cout << "operation = " << endl; + Vector vec2(3, 6); + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + vec1 = vec2; + cout << "vec1=vec2 -> vec1 =" << vec1 << endl << endl; + // + cout << "operation += " << endl; + Vector vec3(3, 8.4); + cout << "vec1 =" << vec1 << endl; + cout << "vec3 =" << vec3 << endl; + vec1 += vec3; + cout << "vec1+=vec3 =" << vec1 << endl << endl; + // + cout << "operation -= " << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec3 =" << vec3 << endl; + vec2 -= vec3; + cout << "vec2-=vec3 =" << vec2 << endl << endl; + // + cout << "unary - " << endl; + Vector vec4 = -vec1; + cout << "vec1 =" << vec1 << endl; + cout << "vec4= -vec1 -> vec4 =" << vec4 << endl << endl; + // + cout << "unary + " << endl; + Vector vec5 = +vec4; + cout << "vec4 =" << vec1 << endl; + cout << "vec5= +vec4 -> vec5 =" << vec4 << endl << endl; + // + cout << "binary - " << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec1 - vec2 =" << vec1 - vec2 << endl << endl; + // + cout << "binary + " << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec1 + vec2 =" << vec1 + vec2 << endl << endl; + // + cout << "multiplication a vector by a number" << endl; + double number = 3; + cout << "vec1 =" << vec1 << endl; + cout << "scalar =" << number << endl; + cout << "vec1 *scalar =" << vec1 * number << endl << endl; + // + cout << "multiplication a number by a vector" << endl; + number = 5; + cout << "vec1 =" << vec1 << endl; + cout << "scalar =" << number << endl; + cout << "scalar * vec1 =" << number * vec1 << endl << endl; + // + cout << "dot product of vectors" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec2 =" << vec2 << endl; + cout << "vec1*vec2 =" << vec1 * vec2 << endl << endl; + // + cout << "input of vector and vector pointer" << endl; + cin >> vec1; + cout << "vec1 =" << vec1 << endl; + double* ptr = vec1; + cout << "vec1 pointer is " << ptr << endl << endl; + // + cout << "separate element of vector" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "2nd element of vec1 is " << vec1[1] << endl << endl; + // + cout << "method of vector dimension" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec1 dimension is " << vec1.dimension() << endl << endl; + // + cout << "method of vector lenghth" << endl; + cout << "vec1 =" << vec1 << endl; + cout << "vec1 lenghth is " << vec1.length() << endl << endl; + // + double* v1 = new double[7]; + v1[0] = 9; + v1[1] = 11; + v1[2] = 10; + v1[3] = 9; + v1[4] = 12; + v1[5] = 8; + v1[6] = 8; + Vector vector(7, 5); + double* v2 = new double[9]; + v2[0] = 1; + v2[1] = 2; + v2[2] = 1; + v2[3] = 2; + v2[4] = 1; + v2[5] = 1; + v2[6] = 2; + v2[7] = 2; + v2[8] = 3; + size_t* v3 = new size_t[9]; + v3[0] = 2; + v3[1] = 1; + v3[2] = 2; + v3[3] = 3; + v3[4] = 1; + v3[5] = 4; + v3[6] = 1; + v3[7] = 2; + v3[8] = 5; + size_t* v4 = new size_t[8]; + v4[0] = 1; + v4[1] = 1; + v4[2] = 1; + v4[3] = 2; + v4[4] = 5; + v4[5] = 7; + v4[6] = 7; + v4[7] = 10; + size_t a = 7; + size_t b = 9; + CSLRMatrix mat1(v1, v2, v3, v4, a, b); - cout << "constructor" << endl; - Vector vec1(2, 5); - cout << vec1 << endl << endl; - // - cout << "operation = " << endl; - Vector vec2(3, 6); - cout << "vec1 =" << vec1 << endl; - cout << "vec2 =" << vec2 << endl; - vec1 = vec2; - cout << "vec1=vec2 -> vec1 =" << vec1 << endl << endl; - // - cout << "operation += " << endl; - Vector vec3(3, 8.4); - cout << "vec1 =" << vec1 << endl; - cout << "vec3 =" << vec3 << endl; - vec1 += vec3; - cout << "vec1+=vec3 =" << vec1 << endl << endl; - // - cout << "operation -= " << endl; - cout << "vec2 =" << vec2 << endl; - cout << "vec3 =" << vec3 << endl; - vec2 -= vec3; - cout << "vec2-=vec3 =" << vec2 << endl << endl; - // - cout << "unary - " << endl; - Vector vec4 = -vec1; - cout << "vec1 =" << vec1 << endl; - cout << "vec4= -vec1 -> vec4 =" << vec4 << endl << endl; - // - cout << "unary + " << endl; - Vector vec5 = +vec4; - cout << "vec4 =" << vec1 << endl; - cout << "vec5= +vec4 -> vec5 =" << vec4 << endl << endl; - // - cout << "binary - " << endl; - cout << "vec1 =" << vec1 << endl; - cout << "vec2 =" << vec2 << endl; - cout << "vec1 - vec2 =" << vec1 - vec2 << endl << endl; - // - cout << "binary + " << endl; - cout << "vec1 =" << vec1 << endl; - cout << "vec2 =" << vec2 << endl; - cout << "vec1 + vec2 =" << vec1 + vec2 << endl << endl; - // - cout << "multiplication a vector by a number" << endl; - double number = 3; - cout << "vec1 =" << vec1 << endl; - cout << "scalar =" << number << endl; - cout << "vec1 *scalar =" << vec1 * number << endl << endl; - // - cout << "multiplication a number by a vector" << endl; - number = 5; - cout << "vec1 =" << vec1 << endl; - cout << "scalar =" << number << endl; - cout << "scalar * vec1 =" << number * vec1 << endl << endl; - // - cout << "dot product of vectors" << endl; - cout << "vec1 =" << vec1 << endl; - cout << "vec2 =" << vec2 << endl; - cout << "vec1*vec2 =" << vec1 * vec2 << endl << endl; - // - cout << "input of vector and vector pointer" << endl; - cin >> vec1; - cout << "vec1 =" << vec1 << endl; - double* ptr = vec1; - cout << "vec1 pointer is " << ptr << endl << endl; - // - cout << "separate element of vector" << endl; - cout << "vec1 =" << vec1 << endl; - cout << "2nd element of vec1 is " << vec1[1] << endl << endl; - // - cout << "method of vector dimension" << endl; - cout << "vec1 =" << vec1 << endl; - cout << "vec1 dimension is " << vec1.dimension() << endl << endl; - // - cout << "method of vector lenghth" << endl; - cout << "vec1 =" << vec1 << endl; - cout << "vec1 lenghth is " << vec1.length() << endl << endl; - // - double* v1 = new double[7]; - v1[0] = 9; - v1[1] = 11; - v1[2] = 10; - v1[3] = 9; - v1[4] = 12; - v1[5] = 8; - v1[6] = 8; - Vector vector(7, 5); - double* v2 = new double[9]; - v2[0] = 1; - v2[1] = 2; - v2[2] = 1; - v2[3] = 2; - v2[4] = 1; - v2[5] = 1; - v2[6] = 2; - v2[7] = 2; - v2[8] = 3; - double* v3 = new double[9]; - v3[0] = 2; - v3[1] = 1; - v3[2] = 2; - v3[3] = 3; - v3[4] = 1; - v3[5] = 4; - v3[6] = 1; - v3[7] = 2; - v3[8] = 5; - double* v4 = new double[8]; - v4[0] = 1; - v4[1] = 1; - v4[2] = 1; - v4[3] = 2; - v4[4] = 5; - v4[5] = 7; - v4[6] = 7; - v4[7] = 10; - CSLRMatrix mat1(v1, v2, v3, v4); + cout << "mat1:" << endl; + cout << mat1 << endl; + // + cout << "multiplication a matrix by a number" << endl; + number = 3; + cout << "scalar =" << number << endl; - cout << "mat1:" << endl; - cout << mat1 << endl; - // - cout << "multiplication a matrix by a number" << endl; - number = 3; - cout << "scalar =" << number << endl; - - cout << "mat1 *scalar =\n" << mat1 * number << endl << endl; - // - cout << "multiplication a number by a matrix" << endl; - number = 5; - cout << "scalar =" << number << endl; - cout << "scalar * mat1 =\n" << number * mat1 << endl << endl; - // - cout << "multiplication a matrix by a vector" << endl; - cout << "vector =" << vector << endl; - cout << "vector * mat1 = " << mat1 * vector << endl << endl; - // - double* ve1 = new double[4]; - ve1[0] = 1; - ve1[1] = 2; - ve1[2] = 3; - ve1[3] = 4; - - double* ve2 = new double[2]; - ve2[0] = 2; - ve2[1] = 5; - - - double* ve3 = new double[2]; - ve3[0] = 1; - ve3[1] = 2; - - double* ve4 = new double[5]; - ve4[0] = 1; - ve4[1] = 1; - ve4[2] = 1; - ve4[3] = 1; - ve4[4] = 3; - CSLRMatrix mat2(ve1, ve2, ve3, ve4); - cout << "operation = " << endl; - cout << "mat1 =\n" << mat1 << endl; - cout << "mat2 =\n" << mat2 << endl; - mat2 = mat1; - cout << "mat2=mat1 -> mat2 =\n" << mat2 << endl << endl; - // - cout << "method of matrix size" << endl; - cout << "mat1 dimension is " << mat1.matsize() << endl << endl; - // - cout << "method of matrix's non-zero elements" << endl; - cout << "number of mat1 non-zero elements is " << mat1.nzn() << endl << endl; - // - // - // - // - cout << "final test" << endl; - Vector vec(7, 0); - vec[0] = 1; - vec[1] = 2; - vec[2] = 3; - vec[3] = 4; - vec[4] = 5; - vec[5] = 6; - vec[6] = 7; - Vector result(vec); - cout << "vector1 =" << vec << endl; - Vector vect(7, 0); - vect[0] = 14; - vect[1] = 6; - vect[2] = 2; - vect[3] = 11; - vect[4] = 3; - vect[5] = 3; - vect[6] = 1; - cout << "vector2 =" << vect << endl; - Vector vecto(7, 0); - vecto[0] = 5; - vecto[1] = 6; - vecto[2] = 4; - vecto[3] = 19; - vecto[4] = 6; - vecto[5] = 20; - vecto[6] = 5; - cout << "vector3 =" << vecto << endl; - int a = 4; - cout << "number1 =" << a << endl; - int b = 10; - cout << "number2 =" << b << endl; - int c = 10; - cout << "number3 =" << c << endl; - cout << "matrix =" << endl << mat1 << endl; - result = -a * mat1 * (vec - b * vect) + vecto * c; - cout << " -number1 * matrix * (vector1 - number2 * vector2) + vector3 * number3" << endl << result << endl; + cout << "mat1 *scalar =\n" << mat1 * number << endl << endl; + // + cout << "multiplication a number by a matrix" << endl; + number = 5; + cout << "scalar =" << number << endl; + cout << "scalar * mat1 =\n" << number * mat1 << endl << endl; + // + cout << "multiplication a matrix by a vector" << endl; + cout << "vector =" << vector << endl; + cout << "vector * mat1 = " << mat1 * vector << endl << endl; + // + double* ve1 = new double[4]; + ve1[0] = 1; + ve1[1] = 2; + ve1[2] = 3; + ve1[3] = 4; + double* ve2 = new double[2]; + ve2[0] = 2; + ve2[1] = 5; + size_t* ve3 = new size_t[2]; + ve3[0] = 1; + ve3[1] = 2; + size_t* ve4 = new size_t[5]; + ve4[0] = 1; + ve4[1] = 1; + ve4[2] = 1; + ve4[3] = 1; + ve4[4] = 3; + size_t c = 4; + size_t d = 2; + CSLRMatrix mat2(ve1, ve2, ve3, ve4, c, d); + cout << "operation = " << endl; + cout << "mat1 =\n" << mat1 << endl; + cout << "mat2 =\n" << mat2 << endl; + mat2 = mat1; + cout << "mat2=mat1 -> mat2 =\n" << mat2 << endl << endl; + // + cout << "method of matrix size" << endl; + cout << "mat1 dimension is " << mat1.matsize() << endl << endl; + // + cout << "method of matrix's non-zero elements" << endl; + cout << "number of mat1 non-zero elements is " << mat1.nzn() << endl << endl; + // + // + cout << "final test" << endl; + Vector vec(7, 0); + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + vec[3] = 4; + vec[4] = 5; + vec[5] = 6; + vec[6] = 7; + Vector result(vec); + cout << "vector1 =" << vec << endl; + Vector vect(7, 0); + vect[0] = 14; + vect[1] = 6; + vect[2] = 2; + vect[3] = 11; + vect[4] = 3; + vect[5] = 3; + vect[6] = 1; + cout << "vector2 =" << vect << endl; + Vector vecto(7, 0); + vecto[0] = 5; + vecto[1] = 6; + vecto[2] = 4; + vecto[3] = 19; + vecto[4] = 6; + vecto[5] = 20; + vecto[6] = 5; + cout << "vector3 =" << vecto << endl; + int k1 = 4; + cout << "number1 =" << a << endl; + int k2 = 10; + cout << "number2 =" << b << endl; + int k3 = 10; + cout << "number3 =" << c << endl; + cout << "matrix =" << endl << mat1 << endl; + result = -k1 * mat1 * (vec - k2 * vect) + vecto * k3; + cout << " result = -number1 * matrix * (vector1 - number2 * vector2) + vector3 * number3" << endl << result << endl; + delete[] v1; + delete[] v2; + delete[] v3; + delete[] v4; + delete[] ve1; + delete[] ve2; + delete[] ve3; + delete[] ve4; + } + catch (OutOfRangeException& exception) + { + std::cout << exception.what(); + } + catch (IncompatibleDimException& exception) + { + std::cout << exception.what(); + } return 0; } \ No newline at end of file diff --git a/operator_overloading/Matrix.cpp b/operator_overloading/Matrix.cpp index 3b7c0c4..f046dab 100644 --- a/operator_overloading/Matrix.cpp +++ b/operator_overloading/Matrix.cpp @@ -1,96 +1,103 @@ #include #include "exception.h" #include "Vector.h" -#include "CSLRMatrx.h" #include using namespace std; -CSLRMatrix::CSLRMatrix(double* p_adiag, double* p_altr, double* p_jptr, double* p_iptr) : - adiag(new double[_msize(p_adiag) / sizeof(double)]), - altr(new double[_msize(p_altr) / sizeof(double)]), - jptr(new double[_msize(p_jptr) / sizeof(double)]), - iptr(new double[_msize(p_iptr) / sizeof(double)]) + +CSLRMatrix::CSLRMatrix(double* p_adiag, double* p_altr, size_t* p_jptr, size_t* p_iptr, size_t p_diag, size_t p_elem) : + m_adiag(new double[p_diag]), + m_diag(p_diag), + m_altr(new double[p_elem]), + m_elem(p_elem), + m_jptr(new size_t[p_elem]), + m_iptr(new size_t[p_diag + 1]) { - for (int i = 0; i < _msize(p_adiag) / sizeof(double); ++i) + for (int i = 0; i < p_diag; ++i) { - adiag[i] = p_adiag[i]; + m_adiag[i] = p_adiag[i]; } - for (int i = 0; i < _msize(p_altr) / sizeof(double); ++i) + for (int i = 0; i < p_elem; ++i) { - altr[i] = p_altr[i]; - jptr[i] = p_jptr[i]; + m_altr[i] = p_altr[i]; + m_jptr[i] = p_jptr[i]; } - for (int i = 0; i < _msize(p_iptr) / sizeof(double); ++i) + for (int i = 0; i < p_diag + 1; ++i) { - iptr[i] = p_iptr[i]; + m_iptr[i] = p_iptr[i]; } } + + CSLRMatrix::CSLRMatrix(const CSLRMatrix& p_mat) : - adiag(new double[_msize(p_mat.adiag) / sizeof(double)]), - altr(new double[_msize(p_mat.altr) / sizeof(double)]), - jptr(new double[_msize(p_mat.jptr) / sizeof(double)]), - iptr(new double[_msize(p_mat.iptr) / sizeof(double)]) + m_adiag(new double[p_mat.m_diag]), + m_diag(p_mat.m_diag), + m_altr(new double[p_mat.m_elem]), + m_elem(p_mat.m_elem), + m_jptr(new size_t[p_mat.m_elem]), + m_iptr(new size_t[p_mat.m_diag + 1]) { - for (int i = 0; i < _msize(p_mat.adiag) / sizeof(double); ++i) + for (int i = 0; i < p_mat.m_diag; ++i) { - adiag[i] = p_mat.adiag[i]; + m_adiag[i] = p_mat.m_adiag[i]; } - for (int i = 0; i < _msize(p_mat.altr) / sizeof(double); ++i) + for (int i = 0; i < p_mat.m_elem; ++i) { - altr[i] = p_mat.altr[i]; - jptr[i] = p_mat.jptr[i]; + m_altr[i] = p_mat.m_altr[i]; + m_jptr[i] = p_mat.m_jptr[i]; } - for (int i = 0; i < _msize(p_mat.iptr) / sizeof(double); ++i) + for (int i = 0; i < p_mat.m_diag + 1; ++i) { - iptr[i] = p_mat.iptr[i]; + m_iptr[i] = p_mat.m_iptr[i]; } } - - CSLRMatrix::~CSLRMatrix() { - delete[] adiag; - delete[] altr; - delete[] iptr; - delete[] jptr; + delete[] m_adiag; + delete[] m_altr; + delete[] m_iptr; + delete[] m_jptr; } -std::istream& operator>>(std::istream& is, CSLRMatrix& m) { + +std::istream& operator>>(std::istream& is, CSLRMatrix& p_mat) { printf("\nenter the elements that are on the main diagonal\n"); - for (int i = 0; i < _msize(m.adiag) / sizeof(double); i++) - is >> m.adiag[i]; + for (int i = 0; i < p_mat.m_diag; i++) + is >> p_mat.m_adiag[i]; printf("\nenter nonzero matrix elements\n"); - for (int i = 0; i < _msize(m.altr) / sizeof(double); i++) - is >> m.altr[i]; + for (int i = 0; i < p_mat.m_elem; i++) + is >> p_mat.m_altr[i]; printf("\nin which columns are these elements\n"); - for (int i = 0; i < _msize(m.jptr) / sizeof(double); i++) - is >> m.jptr[i]; + for (int i = 0; i < p_mat.m_elem; i++) + is >> p_mat.m_jptr[i]; printf("\nWith what position in the array of all non - zero elements does the i-th line begin matrices\n"); - for (int i = 0; i < _msize(m.iptr) / sizeof(double); i++) - is >> m.iptr[i]; + for (int i = 0; i < p_mat.m_diag+1; i++) + is >> p_mat.m_iptr[i]; return is; } -std::ostream& operator<<(std::ostream& os, const CSLRMatrix& mat) { - for (int i = 0; i < _msize(mat.adiag) / sizeof(double); i++) { - for (int j = 0; j < _msize(mat.adiag) / sizeof(double); j++) { + + +std::ostream& operator<<(std::ostream& os, const CSLRMatrix& p_mat) { + for (int i = 0; i < p_mat.m_diag; i++) { + for (int j = 0; j < p_mat.m_diag; j++) { double val = 0; if (i == j) { - val = mat.adiag[i]; + val = p_mat.m_adiag[i]; } else { - for (int k = mat.iptr[i] - 1; k < mat.iptr[i + 1] - 1; k++) { - if (mat.jptr[k] - 1 == j) { - val = mat.altr[k]; + for (int k = p_mat.m_iptr[i] - 1; k < p_mat.m_iptr[i + 1] - 1; k++) { + if (p_mat.m_jptr[k] - 1 == j) { + val = p_mat.m_altr[k]; break; } } - for (int k = mat.iptr[j] - 1; k < mat.iptr[j + 1] - 1; k++) { - if (mat.jptr[k] - 1 == i) { - val = mat.altr[k]; + for (int k = p_mat.m_iptr[j] - 1; k < p_mat.m_iptr[j + 1] - 1; k++) { + if (p_mat.m_jptr[k] - 1 == i) { + val = p_mat.m_altr[k]; break; } } @@ -102,92 +109,100 @@ std::ostream& operator<<(std::ostream& os, const CSLRMatrix& mat) { return os; } -int CSLRMatrix::matsize() const + +size_t CSLRMatrix::matsize() const { - return _msize(adiag) / sizeof(double); + return m_diag; } -int CSLRMatrix::nzn() const +size_t CSLRMatrix::nzn() const { - int ans = _msize(altr) / sizeof(double) * 2; - for (int i = 0; i < _msize(adiag) / sizeof(double); i++) + int ans = m_elem * 2; + for (int i = 0; i < m_diag; i++) { - if (adiag[i] != 0) ans++; + if (m_adiag[i] != 0) ans++; } return ans; } -const CSLRMatrix& CSLRMatrix:: operator=(const CSLRMatrix& m) { - if (this == &m) { return *this; } - else + +const CSLRMatrix& CSLRMatrix::operator=(const CSLRMatrix& p_mat) +{ + if (this == &p_mat) { - if (_msize(adiag) / sizeof(double) != sizeof(m.adiag) / sizeof(double)) - { - delete[](*this).adiag; - delete[](*this).altr; - delete[](*this).jptr; - delete[](*this).iptr; - (*this).adiag = new double[_msize(m.adiag) / sizeof(double)]; - (*this).altr = new double[_msize(m.altr) / sizeof(double)]; - (*this).jptr = new double[_msize(m.jptr) / sizeof(double)]; - (*this).iptr = new double[_msize(m.iptr) / sizeof(double)]; - } - for (int i = 0; i < _msize(m.adiag) / sizeof(double); ++i) - { - adiag[i] = m.adiag[i]; - } - for (int i = 0; i < _msize(m.altr) / sizeof(double); ++i) - { - altr[i] = m.altr[i]; - jptr[i] = m.jptr[i]; - } - for (int i = 0; i < _msize(m.iptr) / sizeof(double); ++i) - { - iptr[i] = m.iptr[i]; - } return *this; } - + if (m_diag != p_mat.m_diag) + { + delete[] m_adiag; + delete[] m_altr; + delete[] m_jptr; + delete[] m_iptr; + m_adiag = new double[p_mat.m_diag]; + m_altr = new double[p_mat.m_elem]; + m_jptr = new size_t[p_mat.m_elem]; + m_iptr = new size_t[p_mat.m_diag+ 1]; + m_diag = p_mat.m_diag; + m_elem = p_mat.m_elem; + } + for (int i = 0; i < p_mat.m_diag; ++i) + { + m_adiag[i] = p_mat.m_adiag[i]; + } + for (int i = 0; i < p_mat.m_elem; ++i) + { + m_altr[i] = p_mat.m_altr[i]; + m_jptr[i] = p_mat.m_jptr[i]; + } + for (int i = 0; i < p_mat.m_diag + 1; ++i) + { + m_iptr[i] = p_mat.m_iptr[i]; + } + return *this; } -Vector operator*(const CSLRMatrix& mat, const Vector& v) + + +Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec) { - if (mat.matsize() != v.dimension()) + if (p_mat.matsize() != p_vec.dimension()) { throw IncompatibleDimException{}; } - Vector res(v); - for (int i = 0; i < mat.matsize(); i++) + Vector res(p_vec); + for (int i = 0; i < p_mat.matsize(); i++) { - res[i] = v[i] * mat.adiag[i]; + res[i] = p_vec[i] * p_mat.m_adiag[i]; } - for (int i = 0; i < mat.matsize(); i++) + for (int i = 0; i < p_mat.matsize(); i++) { - for (int j = mat.iptr[i] - 1; j < mat.iptr[i + 1] - 1; j++) + for (int j = p_mat.m_iptr[i] - 1; j < p_mat.m_iptr[i + 1] - 1; j++) { - res[i] += v[mat.jptr[j] - 1] * mat.altr[j]; - res[mat.jptr[j] - 1] += v[i] * mat.altr[j]; + res[i] += p_vec[p_mat.m_jptr[j] - 1] * p_mat.m_altr[j]; + res[p_mat.m_jptr[j] - 1] += p_vec[i] * p_mat.m_altr[j]; } } return res; } -CSLRMatrix operator*(const CSLRMatrix& m, double scalar) + +CSLRMatrix operator*(const CSLRMatrix& p_mat, double scalar) { - CSLRMatrix result(m.adiag, m.altr, m.jptr, m.iptr); - for (int i = 0; i < _msize(result.adiag) / sizeof(double); i++) + CSLRMatrix result(p_mat); + for (int i = 0; i >(std::istream& is, CSLRMatrix& m); - friend std::ostream& operator<<(std::ostream& os, const CSLRMatrix& mat); + friend std::istream& operator>>(std::istream& is, CSLRMatrix& p_mat); + friend std::ostream& operator<<(std::ostream& os, const CSLRMatrix& p_mat); - int matsize() const; - int nzn() const; + size_t matsize() const; + size_t nzn() const; - const CSLRMatrix& operator=(const CSLRMatrix& m); + const CSLRMatrix& operator=(const CSLRMatrix& p_mat); - friend Vector operator*(const CSLRMatrix& mat, const Vector& v); - friend CSLRMatrix operator*(double scalar, const CSLRMatrix& m); - friend CSLRMatrix operator*(const CSLRMatrix& m, double scalar); + friend Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec); + friend CSLRMatrix operator*(double scalar, const CSLRMatrix& p_mat); + friend CSLRMatrix operator*(const CSLRMatrix& p_mat, double scalar); }; diff --git a/operator_overloading/Vector.cpp b/operator_overloading/Vector.cpp index 4bd3a4b..ff5d5bd 100644 --- a/operator_overloading/Vector.cpp +++ b/operator_overloading/Vector.cpp @@ -2,12 +2,9 @@ #include "exception.h" #include #include "Vector.h" - #include "Matrix.h" - - Vector::Vector(int n, double value) { size = n; data = new double[size]; @@ -122,8 +119,6 @@ Vector operator*(double scalar, const Vector& v) { } - - double& Vector::operator[](int index) { if (index < 0 || index >= size) { throw OutOfRangeException(); diff --git a/operator_overloading/Vector.h b/operator_overloading/Vector.h index 7023164..10b46a6 100644 --- a/operator_overloading/Vector.h +++ b/operator_overloading/Vector.h @@ -34,10 +34,6 @@ public: friend Vector operator*(const Vector& v, double scalar); friend Vector operator*(double scalar, const Vector& v); - //friend Vector operator*(const CSLRMatrix& m, const Vector& v); - - - friend std::ostream& operator<<(std::ostream& os, const Vector& v); friend std::istream& operator>>(std::istream& is, Vector& v); diff --git a/operator_overloading/exception.cpp b/operator_overloading/exception.cpp index 9f61887..f793928 100644 --- a/operator_overloading/exception.cpp +++ b/operator_overloading/exception.cpp @@ -5,6 +5,7 @@ const char* OutOfRangeException::what() const return "Index is out of range"; } + const char* IncompatibleDimException::what() const { return "Vectors have incompatible dimensions"; -- GitLab From 4f3ec3b0376189172ce4f7f3e1c8883a4ca31c7e Mon Sep 17 00:00:00 2001 From: Zakhar Khomenkov Date: Tue, 2 May 2023 10:02:47 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B2=20=D1=8D=D1=82=D0=BE=D1=82=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- operator_overloading/Matrix.cpp | 41 ++++---- operator_overloading/Matrix.h | 12 +-- operator_overloading/Vector.cpp | 145 ++++++++++++++--------------- operator_overloading/Vector.h | 32 +++---- operator_overloading/exception.cpp | 4 +- operator_overloading/exception.h | 5 +- 6 files changed, 116 insertions(+), 123 deletions(-) diff --git a/operator_overloading/Matrix.cpp b/operator_overloading/Matrix.cpp index f046dab..e508fe8 100644 --- a/operator_overloading/Matrix.cpp +++ b/operator_overloading/Matrix.cpp @@ -61,25 +61,25 @@ CSLRMatrix::~CSLRMatrix() { } -std::istream& operator>>(std::istream& is, CSLRMatrix& p_mat) { +std::istream& operator>>(std::istream& p_is, CSLRMatrix& p_mat) { printf("\nenter the elements that are on the main diagonal\n"); for (int i = 0; i < p_mat.m_diag; i++) - is >> p_mat.m_adiag[i]; + p_is >> p_mat.m_adiag[i]; printf("\nenter nonzero matrix elements\n"); for (int i = 0; i < p_mat.m_elem; i++) - is >> p_mat.m_altr[i]; + p_is >> p_mat.m_altr[i]; printf("\nin which columns are these elements\n"); for (int i = 0; i < p_mat.m_elem; i++) - is >> p_mat.m_jptr[i]; + p_is >> p_mat.m_jptr[i]; printf("\nWith what position in the array of all non - zero elements does the i-th line begin matrices\n"); for (int i = 0; i < p_mat.m_diag+1; i++) - is >> p_mat.m_iptr[i]; - return is; + p_is >> p_mat.m_iptr[i]; + return p_is; } -std::ostream& operator<<(std::ostream& os, const CSLRMatrix& p_mat) { +std::ostream& operator<<(std::ostream& p_os, const CSLRMatrix& p_mat) { for (int i = 0; i < p_mat.m_diag; i++) { for (int j = 0; j < p_mat.m_diag; j++) { double val = 0; @@ -102,11 +102,11 @@ std::ostream& operator<<(std::ostream& os, const CSLRMatrix& p_mat) { } } } - os << setw(4) << val; + p_os << setw(4) << val; } - os << std::endl << std::endl; + p_os << std::endl << std::endl; } - return os; + return p_os; } @@ -114,6 +114,8 @@ size_t CSLRMatrix::matsize() const { return m_diag; } + + size_t CSLRMatrix::nzn() const { int ans = m_elem * 2; @@ -161,7 +163,6 @@ const CSLRMatrix& CSLRMatrix::operator=(const CSLRMatrix& p_mat) } - Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec) { if (p_mat.matsize() != p_vec.dimension()) @@ -185,28 +186,22 @@ Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec) } -CSLRMatrix operator*(const CSLRMatrix& p_mat, double scalar) +CSLRMatrix operator*(const CSLRMatrix& p_mat, double p_scalar) { CSLRMatrix result(p_mat); for (int i = 0; i >(std::istream& is, CSLRMatrix& p_mat); - friend std::ostream& operator<<(std::ostream& os, const CSLRMatrix& p_mat); + friend std::istream& operator>>(std::istream& p_is, CSLRMatrix& p_mat); + friend std::ostream& operator<<(std::ostream& p_os, const CSLRMatrix& p_mat); size_t matsize() const; size_t nzn() const; @@ -31,7 +30,6 @@ public: const CSLRMatrix& operator=(const CSLRMatrix& p_mat); friend Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec); - friend CSLRMatrix operator*(double scalar, const CSLRMatrix& p_mat); - friend CSLRMatrix operator*(const CSLRMatrix& p_mat, double scalar); - -}; + friend CSLRMatrix operator*(double p_scalar, const CSLRMatrix& p_mat); + friend CSLRMatrix operator*(const CSLRMatrix& p_mat, double p_scalar); +}; \ No newline at end of file diff --git a/operator_overloading/Vector.cpp b/operator_overloading/Vector.cpp index ff5d5bd..2428174 100644 --- a/operator_overloading/Vector.cpp +++ b/operator_overloading/Vector.cpp @@ -5,59 +5,59 @@ #include "Matrix.h" -Vector::Vector(int n, double value) { - size = n; - data = new double[size]; - for (int i = 0; i < size; i++) { - data[i] = value; +Vector::Vector(int p_n, double p_value) { + m_size = p_n; + m_data = new double[m_size]; + for (int i = 0; i < m_size; i++) { + m_data[i] = p_value; } } -Vector::Vector(const Vector& v) { - size = v.size; - data = new double[size]; - for (int i = 0; i < size; i++) { - data[i] = v.data[i]; +Vector::Vector(const Vector& p_vec) { + m_size = p_vec.m_size; + m_data = new double[m_size]; + for (int i = 0; i < m_size; i++) { + m_data[i] = p_vec.m_data[i]; } } Vector::~Vector() { - delete[] data; + delete[] m_data; } -Vector& Vector:: operator=(const Vector& v) { - if (this != &v) { - delete[] data; - size = v.size; - data = new double[size]; - for (int i = 0; i < size; i++) { - data[i] = v.data[i]; +Vector& Vector:: operator=(const Vector& p_vec) { + if (this != &p_vec) { + delete[] m_data; + m_size = p_vec.m_size; + m_data = new double[m_size]; + for (int i = 0; i < m_size; i++) { + m_data[i] = p_vec.m_data[i]; } } return *this; } -Vector& Vector::operator+=(const Vector& v) { - if (size != v.size) { +Vector& Vector::operator+=(const Vector& p_vec) { + if (m_size != p_vec.m_size) { throw IncompatibleDimException(); } - for (int i = 0; i < size; i++) { - data[i] += v.data[i]; + for (int i = 0; i < m_size; i++) { + m_data[i] += p_vec.m_data[i]; } return *this; } -Vector& Vector::operator-=(const Vector& v) { - if (size != v.size) { +Vector& Vector::operator-=(const Vector& p_vec) { + if (m_size != p_vec.m_size) { throw IncompatibleDimException(); } - for (int i = 0; i < size; i++) { - data[i] -= v.data[i]; + for (int i = 0; i < m_size; i++) { + m_data[i] -= p_vec.m_data[i]; } return *this; } @@ -69,112 +69,107 @@ Vector Vector::operator+() const { Vector Vector::operator-() const { - Vector result(size); - for (int i = 0; i < size; i++) { - result.data[i] = -data[i]; + Vector result(m_size); + for (int i = 0; i < m_size; i++) { + result.m_data[i] = -m_data[i]; } return result; } -Vector operator+(const Vector& v1, const Vector& v2) { - if (v1.size != v2.size) { +Vector operator+(const Vector& p_vec_1, const Vector& p_vec_2) { + if (p_vec_1.m_size != p_vec_2.m_size) { throw IncompatibleDimException(); } - Vector result(v1); - result += v2; + Vector result(p_vec_1); + result += p_vec_2; return result; } -Vector operator-(const Vector& v1, const Vector& v2) { - if (v1.size != v2.size) { +Vector operator-(const Vector& p_vec_1, const Vector& p_vec_2) { + if (p_vec_1.m_size != p_vec_2.m_size) { throw IncompatibleDimException(); } - Vector result(v1); - result -= v2; + Vector result(p_vec_1); + result -= p_vec_2; return result; } -double operator*(const Vector& v1, const Vector& v2) { +double operator*(const Vector& p_vec_1, const Vector& p_vec_2) { double result = 0.0; - for (int i = 0; i < v1.size; i++) { - result += v1.data[i] * v2.data[i]; + for (int i = 0; i < p_vec_1.m_size; i++) { + result += p_vec_1.m_data[i] * p_vec_2.m_data[i]; } return result; } -Vector operator*(const Vector& v, double scalar) { - Vector result(v); - for (int i = 0; i < v.size; i++) { - result.data[i] *= scalar; +Vector operator*(const Vector& p_vec, double p_scalar) { + Vector result(p_vec); + for (int i = 0; i < p_vec.m_size; i++) { + result.m_data[i] *= p_scalar; } return result; } -Vector operator*(double scalar, const Vector& v) { - return v * scalar; +Vector operator*(double p_scalar, const Vector& p_vec) { + return p_vec * p_scalar; } -double& Vector::operator[](int index) { - if (index < 0 || index >= size) { +double& Vector::operator[](int p_index) { + if (p_index < 0 || p_index >= m_size) { throw OutOfRangeException(); } - return data[index]; + return m_data[p_index]; } -const double& Vector::operator[](int index) const { - if (index < 0 || index >= size) { +const double& Vector::operator[](int p_index) const { + if (p_index < 0 || p_index >= m_size) { throw OutOfRangeException(); } - return data[index]; + return m_data[p_index]; } -std::ostream& operator<<(std::ostream& os, const Vector& v) { - os << "["; - for (int i = 0; i < v.size; i++) { - os << v.data[i]; - if (i < v.size - 1) { - os << ", "; +std::ostream& operator<<(std::ostream& p_os, const Vector& p_vec) { + p_os << "["; + for (int i = 0; i < p_vec.m_size; i++) { + p_os << p_vec.m_data[i]; + if (i < p_vec.m_size - 1) { + p_os << ", "; } } - os << "]"; - return os; + p_os << "]"; + return p_os; } -std::istream& operator>>(std::istream& is, Vector& v) { - for (int i = 0; i < v.size; i++) { - is >> v.data[i]; +std::istream& operator>>(std::istream& p_is, Vector& p_vec) { + for (int i = 0; i < p_vec.m_size; i++) { + p_is >> p_vec.m_data[i]; } - return is; + return p_is; } -Vector::operator double* () { - return data; +Vector:: operator double* () { + return m_data; } double Vector::length() const { double result = 0.0; - for (int i = 0; i < size; i++) { - result += data[i] * data[i]; + for (int i = 0; i < m_size; i++) { + result += m_data[i] * m_data[i]; } return sqrt(result); } int Vector::dimension() const { - return size; -} - - - - - + return m_size; +} \ No newline at end of file diff --git a/operator_overloading/Vector.h b/operator_overloading/Vector.h index 10b46a6..969f8d9 100644 --- a/operator_overloading/Vector.h +++ b/operator_overloading/Vector.h @@ -9,42 +9,42 @@ class Vector public: - Vector(int n = 0, double value = 0.0); + explicit Vector(int n = 0, double value = 0.0); Vector(const Vector& v); ~Vector(); - double& operator[](int index); + double& operator[](int p_index); - const double& operator[](int index) const; + const double& operator[](int p_index) const; - Vector& operator=(const Vector& v); + Vector& operator=(const Vector& p_vec); - Vector& operator+=(const Vector& v); - Vector& operator-=(const Vector& v); + Vector& operator+=(const Vector& p_vec); + Vector& operator-=(const Vector& p_vec); Vector operator+() const; Vector operator-() const; - friend Vector operator+(const Vector& v1, const Vector& v2); - friend Vector operator-(const Vector& v1, const Vector& v2); + friend Vector operator+(const Vector& p_vec_1, const Vector& p_vec_2); + friend Vector operator-(const Vector& p_vec_1, const Vector& p_vec_2); - friend double operator*(const Vector& v1, const Vector& v2); - friend Vector operator*(const Vector& v, double scalar); - friend Vector operator*(double scalar, const Vector& v); + friend double operator*(const Vector& p_vec_1, const Vector& p_vec_2); + friend Vector operator*(const Vector& p_vec, double p_scalar); + friend Vector operator*(double p_scalar, const Vector& p_vec); - friend std::ostream& operator<<(std::ostream& os, const Vector& v); - friend std::istream& operator>>(std::istream& is, Vector& v); + friend std::ostream& operator<<(std::ostream& p_os, const Vector& p_vec); + friend std::istream& operator>>(std::istream& p_is, Vector& p_vec); - operator double* (); + explicit operator double* (); double length() const; int dimension() const; private: - int size; - double* data; + int m_size; + double* m_data; friend class CSLRMatrix; }; diff --git a/operator_overloading/exception.cpp b/operator_overloading/exception.cpp index f793928..8a542fa 100644 --- a/operator_overloading/exception.cpp +++ b/operator_overloading/exception.cpp @@ -1,5 +1,7 @@ #include #include "exception.h" + + const char* OutOfRangeException::what() const { return "Index is out of range"; @@ -9,4 +11,4 @@ const char* OutOfRangeException::what() const const char* IncompatibleDimException::what() const { return "Vectors have incompatible dimensions"; -} +} \ No newline at end of file diff --git a/operator_overloading/exception.h b/operator_overloading/exception.h index 678b266..b11e282 100644 --- a/operator_overloading/exception.h +++ b/operator_overloading/exception.h @@ -2,14 +2,17 @@ #include #include #include + + class OutOfRangeException : public std::exception { public: const char* what() const override; }; + class IncompatibleDimException : public std::exception { public: const char* what() const override; -}; +}; \ No newline at end of file -- GitLab From 91e26285ee0041a73d6fbbaf47b533c62ab55077 Mon Sep 17 00:00:00 2001 From: Zakhar Khomenkov Date: Tue, 2 May 2023 12:00:45 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B2=20=D1=8D=D1=82=D0=BE=D1=82=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- operator_overloading/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operator_overloading/Main.cpp b/operator_overloading/Main.cpp index 3836a15..eeb1238 100644 --- a/operator_overloading/Main.cpp +++ b/operator_overloading/Main.cpp @@ -69,7 +69,7 @@ int main() cout << "input of vector and vector pointer" << endl; cin >> vec1; cout << "vec1 =" << vec1 << endl; - double* ptr = vec1; + double* ptr = (double*)vec1; cout << "vec1 pointer is " << ptr << endl << endl; // cout << "separate element of vector" << endl; -- GitLab From bfc24e9c1ad665d77d486a27ea6edcd2005b039f Mon Sep 17 00:00:00 2001 From: Zakhar Khomenkov Date: Sat, 13 May 2023 18:17:44 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20move?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- operator_overloading/Main.cpp | 31 ++++++++++++++++++++------- operator_overloading/Matrix.cpp | 38 +++++++++++++++++++++++++++++++++ operator_overloading/Matrix.h | 3 +++ operator_overloading/Vector.cpp | 27 ++++++++++++++++++++--- operator_overloading/Vector.h | 7 ++++-- 5 files changed, 93 insertions(+), 13 deletions(-) diff --git a/operator_overloading/Main.cpp b/operator_overloading/Main.cpp index eeb1238..8ec7b63 100644 --- a/operator_overloading/Main.cpp +++ b/operator_overloading/Main.cpp @@ -6,15 +6,26 @@ int main() try { cout << "constructor" << endl; - Vector vec1(2, 5); - cout << vec1 << endl << endl; + Vector source(2, 5); + cout << source << endl << endl; // cout << "operation = " << endl; Vector vec2(3, 6); - cout << "vec1 =" << vec1 << endl; + cout << "source =" << source << endl; cout << "vec2 =" << vec2 << endl; - vec1 = vec2; - cout << "vec1=vec2 -> vec1 =" << vec1 << endl << endl; + source = vec2; + cout << "source=vec2 -> source =" << source << endl << endl; + // + cout << "move constructor" << endl; + Vector source1=move(source); + cout << "vec1=source -> vec1 =" << source1 << endl << endl; + // + cout << "operation move = " << endl; + Vector vec1(1, 1); + cout << "vec1 =" << vec1 << endl; + cout << "source1 =" << source1 << endl; + vec1 = move(source1); + cout << "vec1=source1 -> vec1 =" << vec1 << endl << endl; // cout << "operation += " << endl; Vector vec3(3, 8.4); @@ -124,10 +135,14 @@ int main() v4[7] = 10; size_t a = 7; size_t b = 9; - CSLRMatrix mat1(v1, v2, v3, v4, a, b); + CSLRMatrix source_m(v1, v2, v3, v4, a, b); - cout << "mat1:" << endl; - cout << mat1 << endl; + cout << "source_m:" << endl; + cout << source_m << endl; + // + cout << "move constructor" << endl; + CSLRMatrix mat1 = move(source_m); + cout << "mat1=source_m -> source_m1 =" << mat1 << endl << endl; // cout << "multiplication a matrix by a number" << endl; number = 3; diff --git a/operator_overloading/Matrix.cpp b/operator_overloading/Matrix.cpp index e508fe8..39e4c55 100644 --- a/operator_overloading/Matrix.cpp +++ b/operator_overloading/Matrix.cpp @@ -53,6 +53,23 @@ CSLRMatrix::CSLRMatrix(const CSLRMatrix& p_mat) : } +CSLRMatrix::CSLRMatrix(CSLRMatrix&& p_mat) : + m_adiag(p_mat.m_adiag), + m_diag(p_mat.m_diag), + m_altr(p_mat.m_altr), + m_elem(p_mat.m_elem), + m_jptr(p_mat.m_jptr), + m_iptr(p_mat.m_iptr) +{ + p_mat.m_adiag = nullptr; + p_mat.m_altr = nullptr; + p_mat.m_jptr = nullptr; + p_mat.m_iptr = nullptr; + p_mat.m_diag = 0; + p_mat.m_elem = 0; +} + + CSLRMatrix::~CSLRMatrix() { delete[] m_adiag; delete[] m_altr; @@ -163,6 +180,27 @@ const CSLRMatrix& CSLRMatrix::operator=(const CSLRMatrix& p_mat) } +CSLRMatrix& CSLRMatrix::operator=(CSLRMatrix&& p_mat) +{ + delete[] m_adiag; + delete[] m_altr; + delete[] m_jptr; + delete[] m_iptr; + m_adiag = p_mat.m_adiag; + m_altr = p_mat.m_altr; + m_jptr = p_mat.m_jptr; + m_iptr = p_mat.m_iptr; + m_diag = p_mat.m_diag; + m_elem = p_mat.m_elem; + p_mat.m_adiag = nullptr; + p_mat.m_altr = nullptr; + p_mat.m_jptr = nullptr; + p_mat.m_iptr = nullptr; + p_mat.m_diag = 0; + p_mat.m_elem = 0; + return *this; +} + Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec) { if (p_mat.matsize() != p_vec.dimension()) diff --git a/operator_overloading/Matrix.h b/operator_overloading/Matrix.h index fe20871..3edfb04 100644 --- a/operator_overloading/Matrix.h +++ b/operator_overloading/Matrix.h @@ -20,6 +20,8 @@ public: CSLRMatrix(double* p_adiag, double* p_altr, size_t* p_jptr, size_t* p_iptr, size_t p_diag, size_t p_elem); CSLRMatrix(const CSLRMatrix& p_mat); ~CSLRMatrix(); + CSLRMatrix(CSLRMatrix&& p_mat); + friend std::istream& operator>>(std::istream& p_is, CSLRMatrix& p_mat); friend std::ostream& operator<<(std::ostream& p_os, const CSLRMatrix& p_mat); @@ -28,6 +30,7 @@ public: size_t nzn() const; const CSLRMatrix& operator=(const CSLRMatrix& p_mat); + CSLRMatrix& operator=(CSLRMatrix&& p_mat); friend Vector operator*(const CSLRMatrix& p_mat, const Vector& p_vec); friend CSLRMatrix operator*(double p_scalar, const CSLRMatrix& p_mat); diff --git a/operator_overloading/Vector.cpp b/operator_overloading/Vector.cpp index 2428174..d37fbfb 100644 --- a/operator_overloading/Vector.cpp +++ b/operator_overloading/Vector.cpp @@ -14,15 +14,26 @@ Vector::Vector(int p_n, double p_value) { } -Vector::Vector(const Vector& p_vec) { - m_size = p_vec.m_size; - m_data = new double[m_size]; +Vector::Vector(const Vector& p_vec): + m_size ( p_vec.m_size), + m_data ( new double[m_size]) +{ + for (int i = 0; i < m_size; i++) { m_data[i] = p_vec.m_data[i]; } } +Vector::Vector(Vector&& p_vec): + m_size(p_vec.m_size), + m_data(p_vec.m_data) +{ + p_vec.m_data = nullptr; + p_vec.m_size = 0; +} + + Vector::~Vector() { delete[] m_data; } @@ -41,6 +52,16 @@ Vector& Vector:: operator=(const Vector& p_vec) { } +Vector& Vector:: operator=(Vector&& p_vec) { + delete[] m_data; + m_size = p_vec.m_size; + m_data = p_vec.m_data; + p_vec.m_data = nullptr; + p_vec.m_size = 0; + return *this; +} + + Vector& Vector::operator+=(const Vector& p_vec) { if (m_size != p_vec.m_size) { throw IncompatibleDimException(); diff --git a/operator_overloading/Vector.h b/operator_overloading/Vector.h index 969f8d9..639b844 100644 --- a/operator_overloading/Vector.h +++ b/operator_overloading/Vector.h @@ -9,17 +9,20 @@ class Vector public: - explicit Vector(int n = 0, double value = 0.0); + explicit Vector(int p_n = 0, double p_value = 0.0); - Vector(const Vector& v); + Vector(const Vector& p_vec); ~Vector(); + Vector(Vector&& p_vec); + double& operator[](int p_index); const double& operator[](int p_index) const; Vector& operator=(const Vector& p_vec); + Vector& operator=(Vector&& p_vec); Vector& operator+=(const Vector& p_vec); Vector& operator-=(const Vector& p_vec); -- GitLab