altered: transpose
This commit is contained in:
@@ -31,6 +31,44 @@ int main() {
|
|||||||
int out[] = { 1,2,3,4 };
|
int out[] = { 1,2,3,4 };
|
||||||
matrix<int> tmp(2, 2, out);
|
matrix<int> tmp(2, 2, out);
|
||||||
tmp = left * right;
|
tmp = left * right;
|
||||||
cout << "times: " << endl << tmp << endl;
|
cout << "times: " << endl << tmp << endl << endl;
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>еط<D0B5>dz<EFBFBD><C7B3><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3>ᴴ<EFBFBD><E1B4B4><EFBFBD>µĶ<C2B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD><EFBFBD>double delete֮<65><D6AE>
|
||||||
|
cout << "transpose: " << endl << tmp.transpose() << endl;
|
||||||
|
cout << "transpose left: " << endl << left.transpose() << endl;
|
||||||
|
cout << "transpose right: " << endl << right.transpose() << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
left:
|
||||||
|
1 2 3
|
||||||
|
4 5 6
|
||||||
|
|
||||||
|
right:
|
||||||
|
1 2
|
||||||
|
3 4
|
||||||
|
5 6
|
||||||
|
|
||||||
|
left(1, 1): 1
|
||||||
|
right(2, 1): 3
|
||||||
|
|
||||||
|
times:
|
||||||
|
22 28
|
||||||
|
49 64
|
||||||
|
|
||||||
|
|
||||||
|
transpose:
|
||||||
|
22 49
|
||||||
|
28 64
|
||||||
|
|
||||||
|
transpose left:
|
||||||
|
1 4
|
||||||
|
2 5
|
||||||
|
3 6
|
||||||
|
|
||||||
|
transpose right:
|
||||||
|
1 3 5
|
||||||
|
2 4 6
|
||||||
|
*/
|
@@ -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) const;
|
matrix<T> operator*(const matrix<T>& x) const;
|
||||||
matrix<T>& operator+=(const matrix<T>& x);
|
matrix<T>& operator+=(const matrix<T>& x);
|
||||||
|
matrix<T>& transpose();
|
||||||
template <class T>
|
template <class T>
|
||||||
friend std::ostream& operator<<(std::ostream& os, const matrix<T>& x);
|
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) {
|
if (rows < 0 || cols < 0) {
|
||||||
throw std::out_of_range("Matrix subscript out of range");
|
throw std::out_of_range("Matrix subscript out of range");
|
||||||
}
|
}
|
||||||
|
|
||||||
int sum = rows * cols;
|
int sum = rows * cols;
|
||||||
element = new T[sum];
|
element = new T[sum];
|
||||||
for (int i = 0; i < sum; i++) {
|
if (x) {
|
||||||
element[i] = x[i];
|
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>
|
template <class T>
|
||||||
@@ -146,3 +151,18 @@ std::ostream& operator<<(std::ostream& os, const matrix<T>& x) {
|
|||||||
}
|
}
|
||||||
return os;
|
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