/*============================================================================ Escribir un programa que utilice funciones para calcular las siguientes operaciones matriciales: - La lectura (uso obligatorio de arrays dinámicos) e impresión de una matriz (valores reales, precisión 3 decimales). - Cálculo del producto de los valores de la diagonal principal y la suma de los valores de la diagonal secundaria para una matriz A (m x n). - Cálculo de las siguientes normas para una matriz A (m x n): A1 Ainf A2 - Cálculo del producto matricial de dos matrices (m x p y p x n). ============================================================================*/ #include #include #include using namespace std; float **read_matrix(int &, int &); void write_matrix(int , int, float **); float diagp(int , float **); float diags(int , float **); float norma1(int , int, float **); float normainf(int , int, float **); float normaf(int , int, float **); float **multmatrix( int , int , int , float **, float **); int main() { float **matrix, **matrix1, **matrix2; int m, p, m1, n; matrix=read_matrix(m, p); write_matrix(m, p, matrix); if (m==p) { cout << "Producto diag. principal: " << diagp(m,matrix) << endl; cout << "Suma diag. secundaria: " << diags(m,matrix) << endl; } cout << "Norma 1: " << norma1(m, p, matrix) << endl; cout << "Norma inf: " << normainf(m, p, matrix) << endl; cout << "Norma F: " << normaf(m, p, matrix) << endl; matrix1=read_matrix(m1, n); write_matrix(m1, n, matrix1); if (m1==n) { cout << "Producto diag. principal: " << diagp(m1,matrix1) << endl; cout << "Suma diag. secundaria: " << diags(m1,matrix1) << endl; } cout << "Norma 1: " << norma1(m1, n, matrix1) << endl; cout << "Norma inf: " << normainf(m1, n, matrix1) << endl; cout << "Norma F: " << normaf(m1, n, matrix1) << endl; if (p==m1) { cout << "\nMatrix Multiplication " << endl; matrix2 = multmatrix(m,p,n,matrix,matrix1); write_matrix(m, n, matrix2); } return 0; } float **read_matrix(int &row, int &col) { float **matrix=nullptr; cout << "Enter the number of rows and columns for the matrix: "; cin >> row >> col ; matrix = new float *[row]; // matrix creation for (int i = 0; i < row; i++) matrix[i] = new float [col]; cout << "Enter the element for the matrix: " << endl; for (int i=0; i < row; i++) for (int j=0; j < col; j++) { cout << "Row " << (i+1) << " Col " << (j+1) << " : "; cin >> matrix[i][j]; } return matrix; } void write_matrix(int row, int col, float **matrix) { cout << "\nMatrix:" << endl; cout << setprecision(3) << fixed; for (int i=0; i < row; i++) { for (int j=0; j < col; j++) cout << matrix[i][j] << " "; cout << endl ; } cout << endl ; } float ** multmatrix( int m, int p, int n, float **matrix1, float **matrix2) { float **matrixr; matrixr = new float *[m]; // matrix creation for (int i = 0; i < m; i++) matrixr[i] = new float [n]; for (int i = 0; i < m;i++) for (int j = 0; j < n; j++){ matrixr[i][j] = 0; for (int k = 0; k < p; k++) matrixr[i][j] += matrix1[i][k]*matrix2[k][j]; } return matrixr; } float diagp(int row, float **matrix) { float prod = 1; for (int i=0; i < row; i++) prod *= matrix[i][i]; return prod; } float diags(int row, float **matrix) { float suma = 0; for (int i=0; i < row; i++) suma += matrix[i][row-1-i]; return suma; } float norma1(int row, int col, float **matrix) { float *vec, suma, maxi; vec = new float [col]; for (int j=0; j < col; j++) { suma = 0; for (int i=0; i < row; i++) suma += fabs(matrix[i][j]); vec[j]=suma; } maxi = vec[0]; for (int j=0; j < col; j++) { if (vec[j] > maxi) maxi = vec[j]; } return maxi; } float normainf(int row, int col, float **matrix) { float *vec, suma, maxi; vec = new float [row]; for (int i=0; i < row; i++) { suma = 0; for (int j=0; j < col; j++) suma += fabs(matrix[i][j]); vec[i]=suma; } maxi = vec[0]; for (int i=0; i < row; i++) { if (vec[i] > maxi) maxi = vec[i]; } return maxi; } float normaf(int row, int col, float **matrix) { float suma = 0; for (int i=0; i < row; i++) for (int j=0; j < col; j++) suma += pow(matrix[i][j],2); return sqrt(suma); }