altered: transpose
This commit is contained in:
@@ -22,6 +22,7 @@ public:
|
||||
matrix<T> operator-(const matrix<T>& x) const;
|
||||
matrix<T> operator*(const matrix<T>& x) const;
|
||||
matrix<T>& operator+=(const matrix<T>& x);
|
||||
matrix<T>& transpose();
|
||||
template <class T>
|
||||
friend std::ostream& operator<<(std::ostream& os, const matrix<T>& x);
|
||||
|
||||
@@ -53,12 +54,16 @@ matrix<T>::matrix(int rows, int cols, T* x) : rows(rows), cols(cols){
|
||||
if (rows < 0 || cols < 0) {
|
||||
throw std::out_of_range("Matrix subscript out of range");
|
||||
}
|
||||
|
||||
|
||||
int sum = rows * cols;
|
||||
element = new T[sum];
|
||||
for (int i = 0; i < sum; i++) {
|
||||
element[i] = x[i];
|
||||
if (x) {
|
||||
for (int i = 0; i < sum; i++) element[i] = x[i];
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < sum; i++) element[i] = T();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -146,3 +151,18 @@ std::ostream& operator<<(std::ostream& os, const matrix<T>& x) {
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
matrix<T>& matrix<T>::transpose() {
|
||||
T* telement = new T[rows * cols];
|
||||
for (int i = 1; i <= rows; i++) {
|
||||
for (int j = 1; j <= cols; j++) {
|
||||
telement[(j - 1) * rows + i - 1] = (*this)(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
delete[] element;
|
||||
std::swap(rows, cols);
|
||||
element = telement;
|
||||
return *this;
|
||||
}
|
||||
|
Reference in New Issue
Block a user