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
|
#include <iostream>
template<typename T, std::size_t r, std::size_t c>
struct Mat {
T arr[r][c];
void print_all() { //It's ok.
for (int i = 0; i < r ; i++) {
for (int j = 0; j < c ; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
}
int get_rows_count() const { //It's ok.
return r;
}
int get_columns_count() const { //It's ok.
return c;
}
T* get_row_arr(int row_index) { //It's ok.
T* arr_row = new T[c];
for (int i = 0; i < c; i++) {
arr_row[i] = arr[row_index][i];
}
return arr_row;
}
T* get_column_arr(int column_index) { //It's ok.
T* arr_column = new T[r];
for (int i = 0; i < r; i++) {
arr_column[i] = arr[column_index][i];
}
return arr_column;
}
void print_row(int rows_index) { //It's ok.
float* row = get_row_arr(rows_index);
for (int i = 0; i < c; i++) {
std::cout << *(row + i) << ' ';
}
std::cout << '\n';
delete row;
}
void print_column(int columns_index) { //It's ok.
float* column = get_column_arr(0);
for (int i = 0; i < 2; i++) {
std::cout << *(column + i) << '\n';
}
delete column;
}
Mat& operator+=(const Mat& right) { //It's ok.
for (int i = 0; i < r ; i++) {
for (int j = 0; j < c ; j++) {
arr[i][j] += right.arr[i][j];
}
}
return *this;
}
friend Mat operator+(Mat left, const Mat& right) { //It's ok.
left += right;
return left;
}
//takes left's row and right's column, produce result //1.
T dot_product(T* left, T* right) { //problem here how can I pass 2d array of T?
T sum = 0; //It may not be the same size. ex) 2x3 3x1 /// 2x2 2x2 .....
for (int i = 0; i < c; i++) { //only if left's number of columns == right's number of rows.
sum += *(left + i) + *(right + i);
}
}
//2.
Mat& operator*=(const Mat& right){ //problem here.
if(get_columns_count() != right.get_rows_count()) { //Current class is T<float, 2, 3>. however, Unlike matrix addition
//Matrix multiplication can take T class which has different row and column
} //ex) T<float, 2, 3> T<float, 3, 2> /// T<float, 2, 2> T<float, 2, 2>
for (int i = 0; i < r ; i++) {
for (int j = 0; j < c ; j++) {
arr[i][j] = dot_product(arr, right.arr);
}
}
return *this;
}
friend Mat operator*(Mat left, const Mat& right) {
left *= right;
return left;
}
};
int main() {
Mat<float, 2, 3> m; //It's ok.
m = { 1, 2, 1, 3, 7, 4}; //It's ok.
//3.
auto multiplication1 = m * m; //problem here. In this case I want the complier to tell me *operator with red underline
//Becasue 2x3 * 2x3 can not be multiplied.
std::cin.get();
}
|