From 5da91a41a57433008145066ea40e139d012ee9fb Mon Sep 17 00:00:00 2001 From: Zakhar Khomenkov Date: Tue, 25 Apr 2023 14:52:41 +0300 Subject: [PATCH] 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