Estructura matriz

Aquí se define la estructura matriz.

Características

Operaciones: suma, resta, multiplicación (matricial y escalar).

Propiedades: obtención de transpuesta, prueba para ver si la diagonal principal está conformada sólo del elemento “1”, prueba de simetría de la matriz, prueba de si la matriz es cuadrada o no.

Procedimientos: método de Jacobi.

================================ Código =================================

# include <iostream>
# include <cmath>

using namespace std;

struct matrix{

//================= Acceso a datos ==================
int nrows, ncols;
double **data;         //arreglo con la informacion

//numero de renglones
int rows(){
return nrows;
}
//numero de columnas
int cols(){
return ncols;
}

//constructor
matrix(int nrows, int ncols) : nrows(nrows), ncols(ncols){
data = new double*[nrows];
for (int i = 0; i < nrows; i++){
data[i] = new double[ncols];
}
}

//acceso mediante parentesis
double& operator ()(int i,int j){
return data[i][j];
}

//para inicializar la matriz
matrix init(){
matrix C(nrows, ncols);
for (int i = 0 ; i < nrows ; i++){
for (int j = 0; j < ncols; j++){
cout <<“[“<< i <<“,”<< j <<“]: “;
cin >> C(i,j);
}
}
return C;
}

//para copiar una matriz en otra
matrix copy(){
matrix C(nrows, ncols);

for (int i = 0; i < nrows; i++){
for (int j = 0; j < ncols; j++){
C(i,j) = data[i][j];
}
}
return C;
}

//para mostrar la matriz
void show(){
for (int i = 0; i < nrows; i++){
for (int j = 0; j < ncols; j++){
cout << data[i][j] << “\t”;
}
cout << endl;
}
}

// ================= Propiedades ===================

int diag(){                           //Para ver si la matriz tiene solo 1’s en la diagonal principal
if (nrows != ncols){                //si la matriz no es cuadrada
return -1;
}
else{                             //si si es
int prod=1;
for (int i=0; i<nrows; i++){
prod *= data[i][i];
}
if (prod==1) { return 1; } else{ return 0; }
}
}

int sym(){                            // para ver si la matriz es simetrica
if (nrows != ncols){ return -1;}  // si la matriz no es cuadrada
else{
int count = 0;  int k = (ncols-1)*(ncols)/2;
for (int i = 0; i < nrows-1; i++){
for (int j = i+1; j < ncols; j++){
if ( data[i][j] == data[j][i] ){ count++; }
}
}
if ( count == k ){ return 1;}   //si es simetrica
else{ return 0;}              //si no es simetrica
}
}

int square(){                         //para ver si la matriz es cuadrada
if (nrows != ncols){ return 0; }
else{ return 1; }
}

matrix t(){                           //para obtener la transpuesta
matrix C(ncols, nrows);
for (int i = 0; i < ncols; i++){
for (int j = 0; j < nrows; j++){
C(i,j) = data[j][i];
}
}
return C;
}

matrix dinv(){                          //diagonal inversa de la matriz
matrix C(ncols, nrows);
for (int i = 0; i < nrows; i++){
for (int j = 0 ; j < ncols; j++){
if (i == j){
C(i,j) = 1/data[i][i];
}
else{
C(i,j) = 0;
}
}
}
return C;
}

matrix R(){                             //parte no diagonal de la matriz
matrix C(nrows, ncols);

for (int i = 0; i < nrows; i++){
for (int j = 0; j < ncols; j++){
if (j != i){
C(i,j) = data[i][j];
}
else{
C(i,j) = 0;
}
}
}
return C;
}

// ================= Operaciones ===================

friend matrix operator +(matrix A, matrix B){ //suma de matrices
int m = A.rows(); int n = A.cols();
matrix C(m,n);

for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
C(i,j) = A(i,j) + B(i,j);
}
}
return C;
}

friend matrix operator -(matrix A, matrix B){ //resta de matrices
int m = A.rows(); int n = A.cols();
matrix C(m,n);

for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
C(i,j) = A(i,j) – B(i,j);
}
}
return C;
}

friend matrix operator -(matrix A){           //inversion de signo
int m = A.rows(); int n = A.cols();

for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
A(i,j) = – A(i,j);
}
}
return A;
}

friend matrix operator *(matrix A, matrix B){ //producto de matrices
int n = A.rows(); int m = B.cols();
matrix C(n,m);

for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
C(i,j) = 0;
for (int k = 0; k < A.cols(); k++){
C(i,j) += A(i,k) * B(k,j);
}
}
}
return C;
}

friend matrix operator *(double a, matrix B){ //escalar por matriz
int n = B.rows(); int m = B.cols();
matrix C(n,m);

for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
C(i,j) = a*B(i,j);
}
}
return C;
}

// ================= Procedimientos ===================

matrix Jacobi(matrix b){                      //Solucion de sistemas de ecuaciones lineales

int iter = 1; int n = ncols;

matrix A = copy();

matrix D = A.dinv();
matrix R = A.R();
matrix T = – ( D * R );
matrix C = D * b;
matrix x(n, 1);
matrix y(n, 1);
matrix error(0,0);

cout << endl << “Introducir vector aproximacion: ” << endl << endl;

x = x.init();

cout << endl << “Vector aproximacion: ” << endl << endl;
x.show();
cout <<endl;

do{
x = T*x + C;
y = (A*x) – b;
iter++;

error = (y.t())*y;

}while( abs(error(0,0)) > 1e-10 );

return x;
}
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s