Professional Documents
Culture Documents
Ex 19
Ex 19
#include <vector>
class Matrix {
private:
std::vector<std::vector<double>> data;
public:
// Constructors and other methods...
Matrix result;
if (rows == cols) {
// Square matrix, create a column vector of the main diagonal
elements
result.data.resize(rows, std::vector<double>(1));
for (int i = 0; i < rows; ++i) {
result.data[i][0] = mv.data[i][i];
}
} else if (cols == 1) {
// Column vector, create a square diagonal matrix
result.data.resize(rows, std::vector<double>(rows, 0.0));
for (int i = 0; i < rows; ++i) {
result.data[i][i] = mv.data[i][0];
}
} else {
// Non-square matrix or row vector, return an empty matrix
std::cerr << "Error: Unsupported matrix for diag function." <<
std::endl;
}
return result;
}
int main() {
// Test cases
Matrix squareMatrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Matrix columnVector{{1}, {2}, {3}};
Matrix rowVector{{1, 2, 3}};
return 0;
}