This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix1d.cpp
118 lines (97 loc) · 2.11 KB
/
Matrix1d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "Matrix1d.h"
#include <algorithm>
#include <corecrt_math.h>
Matrix1d::Matrix1d(const size_t n) : matrix(new long double[n])
{
this->rows = 1;
this->cols = n;
}
Matrix1d::Matrix1d(const Matrix1d& matrix) : Matrix1d(matrix.Size())
{
for (size_t i = 0; i < this->Matrix::Size(); ++i)
{
this->matrix[i] = matrix.matrix[i];
}
}
Matrix1d::Matrix1d(const long double* arr, const size_t n) : Matrix1d(n)
{
for (size_t i = 0; i < n; ++i)
{
matrix[i] = arr[i];
}
}
void Matrix1d::Fill(const long double value)
{
for (size_t i = 0; i < this->cols; ++i)
{
this->matrix[i] = value;
}
}
Matrix1d Matrix1d::Abs() const
{
const auto matrix1d = Matrix1d(*this);
for (size_t i = 0; i < this->cols; ++i)
{
matrix1d.matrix[i] = fabsl(matrix1d.matrix[i]);
}
return Matrix1d{matrix1d};
}
Matrix1d Matrix1d::Trunc(const unsigned from, const unsigned to) const
{
const auto matrix1d = Matrix1d(to - from);
for (size_t i = from; i < to; ++i)
{
matrix1d.matrix[i - from] = this->matrix[i];
}
return Matrix1d{matrix1d};
}
/**
* Find maximum value in matrix and return index of it.
*
* @return index of maximum value in matrix.
*/
unsigned Matrix1d::IndexOfMax() const
{
unsigned index = 0;
for (size_t i = 1; i < this->cols; ++i)
{
if (this->matrix[i] > this->matrix[index])
{
index = i;
}
}
return index;
}
Matrix1d& Matrix1d::operator=(const Matrix1d& matrix1d)
{
if (this == &matrix1d)
{
return *this;
}
delete[] this->matrix;
this->rows = matrix1d.rows;
this->cols = matrix1d.cols;
this->matrix = new long double[matrix1d.Size()];
std::copy_n(matrix1d.matrix, matrix1d.Size(), this->matrix);
return *this;
}
Matrix1d& Matrix1d::operator=(Matrix1d&& matrix1d) noexcept
{
std::swap(this->cols, matrix1d.cols);
std::swap(this->rows, matrix1d.rows);
std::swap(this->matrix, matrix1d.matrix);
return *this;
}
Matrix1d Matrix1d::operator-(const Matrix1d& matrix1d) const
{
const auto result = Matrix1d(*this);
for (size_t i = 0; i < this->Size(); ++i)
{
result.matrix[i] = this->matrix[i] - matrix1d.matrix[i];
}
return Matrix1d{result};
}
Matrix1d::~Matrix1d()
{
delete[] matrix;
}