Matriz
Biblioteca que permite crear matrices Algebraicas y realizar operaciones con estas
Loading...
Searching...
No Matches
Matriz Class Reference

#include <Matriz.hpp>

Public Member Functions

 Matriz (int m=3, int n=3)
 Constructor.
 Matriz (const Matriz &v)
 Constructor de copias.
Matrizoperator= (const Matriz &v)
 Operador de asignaci�n. Copia una matriz en otra matriz.
 ~Matriz ()
 Destructor.
void Capturar ()
 Captura los elementos de una matriz por teclado.
void Redimensionar (unsigned int nuevo_m, unsigned int nuevo_n)
 Obtiene el n�mero de filas de una matriz.
void Imprimir () const
 Imprime una matriz en pantalla.
int ObtenerNumRen () const
 Obtiene el n�mero de filas de una matriz.
int ObtenerNumCol () const
 Obtiene el n�mero de columnas de una matriz.
Matriz operator+ (const Matriz &v) const
 Operador para sumar dos matrices.
Matriz operator- (const Matriz &v) const
 Operador para restar dos matrices.
Matriz operator* (const Matriz &v) const
 Operador para multiplicar dos matrices.
Matriz operator* (double escalar) const
 Operador para multiplicar una matriz por un escalar.
Matriz Transpuesta () const
 Calcula la transpuesta de una matriz.
tipo Determinante () const
 Calcula el determinante de una matriz.
Matriz Cofactores () const
 Calcula la matriz de cofactores de una matriz.
Matriz Inversa () const
 Calcula la matriz inversa de una matriz.

Private Member Functions

void EstablecerDim (int m, int n)

Private Attributes

unsigned int m
unsigned int n
tipo ** componente = nullptr

Friends

std::ostream & operator<< (std::ostream &out, const Matriz &v)
 Funci�n amiga para la sobrecarga del operador de inserci�n.
std::istream & operator>> (std::istream &in, Matriz &v)
 Funci�n amiga para la sobrecarga del operador de extracci�n.
Matriz operator* (double escalar, const Matriz &v)
 Funci�n amiga para la sobrecarga del operador de multiplicaci�n por escalar.

Detailed Description

Permite manipular matrices y realizar diversas operaciones matriciales, incluyendo suma, resta, multiplicaci�n, multiplicaci�n por un escalar, determinante, inversa, cofactores, transposici�n y redimensionamiento.

Note
Esta clase se dise�� para manejar matrices de tipo long double.

Definition at line 18 of file Matriz.hpp.

Constructor & Destructor Documentation

◆ Matriz() [1/2]

Matriz::Matriz ( int m = 3,
int n = 3 )
explicit

Constructor.

Versi�n 1: Constructor de una matriz de 3x3 por omisi�n.

Versi�n 2: Constructor de una matriz de las dimensiones indicadas por m y n.

Parameters
mN�mero de filas de la matriz.
nN�mero de columnas de la matriz.
Precondition
m y n deben ser n�meros positivos.
Exceptions
constchar * La matriz no puede ser creada o las dimensiones no son positivas.

Definition at line 26 of file Matriz.cpp.

26 {
28 try {
29 componente = new tipo*[m];
30 componente[0] = new tipo[m * n];
31 for (int i = 1; i < m; ++i)
32 componente[i] = componente[0] + i * n;
33
34 // Inicializar la matriz con ceros
35 for (int i = 0; i < m; ++i) {
36 for (int j = 0; j < n; ++j) {
37 componente[i][j] = 0;
38 }
39 }
40 } catch(std::bad_alloc &) {
41 throw "No es posible construir una Matriz";
42 }
43}
long double tipo
Definition Matriz.hpp:16
unsigned int m
Definition Matriz.hpp:179
unsigned int n
Definition Matriz.hpp:179
tipo ** componente
Definition Matriz.hpp:180
void EstablecerDim(int m, int n)
Definition Matriz.cpp:17

◆ Matriz() [2/2]

Matriz::Matriz ( const Matriz & v)

Constructor de copias.

Parameters
vLa matriz a copiar.
Exceptions
constchar * La matriz copia no puede ser creada.

Definition at line 47 of file Matriz.cpp.

47 : m(v.m), n(v.n), componente(nullptr) {
48 try {
49 componente = new tipo*[m];
50 componente[0] = new tipo[m * n];
51 for (unsigned int i = 1; i < m; ++i)
52 componente[i] = componente[0] + i * n;
53
54 // Copiar los elementos de v a la matriz actual
55 for (unsigned int i = 0; i < m; ++i) {
56 for (unsigned int j = 0; j < n; ++j) {
57 componente[i][j] = v.componente[i][j];
58 }
59 }
60 } catch(std::bad_alloc &) {
61 throw "No es posible construir una Matriz";
62 }
63}

◆ ~Matriz()

Matriz::~Matriz ( )

Destructor.

Definition at line 95 of file Matriz.cpp.

95 {
96 delete[] componente[0];
97 delete[] componente;
98 componente = nullptr;
99}

Member Function Documentation

◆ Capturar()

void Matriz::Capturar ( )

Captura los elementos de una matriz por teclado.

Definition at line 101 of file Matriz.cpp.

102{
103 for(unsigned int i = 0 ; i < m ; ++i){
104 for(unsigned int j = 0; j<n; ++j){
105 cin >> componente[i][j];
106 }
107 }
108
109}

◆ Cofactores()

Matriz Matriz::Cofactores ( ) const

Calcula la matriz de cofactores de una matriz.

Returns
La matriz de cofactores.
Exceptions
constchar * La matriz no es cuadrada o la matriz de cofactores no puede ser calculada.

Definition at line 235 of file Matriz.cpp.

236{
237 if(n!=m) throw "Dimensiones incorrectas para el la matriz de cofactores";
238 Matriz cofactores(n, n);
239 Matriz submatriz(n-1, n-1);
240 for (unsigned int i = 0; i < n; ++i) {
241 for (unsigned int j = 0; j < n; ++j) {
242 int submatriz_fila = 0;
243 for (unsigned int k = 0; k < n; ++k) {
244 if (k != i) {
245 int submatriz_columna = 0;
246 for (unsigned int l = 0; l < n; ++l) {
247 if (l != j) {
248 submatriz.componente[submatriz_fila][submatriz_columna] = componente[k][l];
249 ++submatriz_columna;
250 }
251 }
252 ++submatriz_fila;
253 }
254 }
255
256 // Calcula el cofactor como el determinante de la submatriz con signo alternante
257 int signo = (i + j) % 2 == 0 ? 1 : -1;
258 cofactores.componente[i][j] = signo * submatriz.Determinante();
259 }
260 }
261 return cofactores;
262}
Matriz(int m=3, int n=3)
Constructor.
Definition Matriz.cpp:26

◆ Determinante()

tipo Matriz::Determinante ( ) const

Calcula el determinante de una matriz.

Returns
El determinante de la matriz.
Exceptions
constchar * La matriz no es cuadrada o el determinante no puede ser calculado.

Definition at line 201 of file Matriz.cpp.

202{
203 if(n!=m) throw "Dimensiones incorrectas para el determinanate";
204 if (n == 1) {
205 return componente[0][0];
206 } else {
207 tipo det = 0;
208 Matriz submatriz(n - 1, n - 1);
209
210 for (unsigned int i = 0; i < n; ++i) {
211 // Obtiene la submatriz eliminando la primera fila y la columna i
212 int submatriz_fila = 0;
213 for (unsigned int j = 1; j < n; ++j) {
214 int submatriz_columna = 0;
215 for (unsigned int k = 0; k < n; ++k) {
216 if (k != i) {
217 submatriz.componente[submatriz_fila][submatriz_columna] = componente[j][k];
218 ++submatriz_columna;
219 }
220 }
221 ++submatriz_fila;
222 }
223
224 // Calcula el cofactor como el determinante de la submatriz con signo alternante
225 int signo = (i % 2 == 0) ? 1 : -1;
226 tipo cofactor = submatriz.Determinante();
227 det += signo * componente[0][i] * cofactor;
228 }
229 return det;
230 }
231
232}

◆ EstablecerDim()

void Matriz::EstablecerDim ( int m,
int n )
private

Definition at line 17 of file Matriz.cpp.

18{
19 if (m < 1 || n < 1)throw "Valor fuera de rango";
20 this->n = n;
21 this->m = m;
22}

◆ Imprimir()

void Matriz::Imprimir ( ) const

Imprime una matriz en pantalla.

Definition at line 111 of file Matriz.cpp.

112{
113 cout << left;
114 cout << "┌";
115 for(unsigned int j = 0; j < n ; ++j) cout << "\t";
116 cout << "┐" << endl;
117
118 for(unsigned int i = 0 ; i < m ; ++i){
119 cout << "│";
120 for(unsigned int j = 0; j < n ; ++j) {
121 if (componente[i][j] - static_cast<int>(componente[i][j]) !=0 || componente[i][j] - round(componente[i][j]) !=0 ) {
122 cout << fixed << setprecision(2) << componente[i][j] << "\t" ;
123 } else {
124 cout << componente[i][j] << "\t" ;
125 }
126 }
127 cout << "│";
128 cout << endl;
129 }
130
131 cout << "└";
132 for(unsigned int j = 0; j < n ; ++j) cout << "\t";
133 cout << "┘";
134
135 cout << endl << endl;
136}

◆ Inversa()

Matriz Matriz::Inversa ( ) const

Calcula la matriz inversa de una matriz.

Returns
La matriz inversa.
Exceptions
constchar * La matriz no es cuadrada, singular o la matriz inversa no puede ser calculada.

Definition at line 264 of file Matriz.cpp.

265{
266 if(n!=m) throw "Dimensiones incorrectas para el la matriz inversa";
267 Matriz inversa(n, n);
268 tipo determinante = this->Determinante();
269
270 if (abs(determinante) < 1e-10) {
271 throw "No existe la inversa de esta matriz";
272 }
273
274 Matriz cofactores(n, n);
275 cofactores = this->Cofactores();
276
277 for (unsigned int i = 0; i < n; ++i) {
278 for (unsigned int j = 0; j < n; ++j) {
279 inversa.componente[i][j] = cofactores.componente[j][i] / determinante;
280 }
281 }
282
283 return inversa;
284
285}
tipo Determinante() const
Calcula el determinante de una matriz.
Definition Matriz.cpp:201
Matriz Cofactores() const
Calcula la matriz de cofactores de una matriz.
Definition Matriz.cpp:235

◆ ObtenerNumCol()

int Matriz::ObtenerNumCol ( ) const

Obtiene el n�mero de columnas de una matriz.

Returns
El n�mero de columnas de la matriz.

Definition at line 143 of file Matriz.cpp.

144{
145 return n;
146}

◆ ObtenerNumRen()

int Matriz::ObtenerNumRen ( ) const

Obtiene el n�mero de filas de una matriz.

Returns
El n�mero de filas de la matriz.

Definition at line 138 of file Matriz.cpp.

139{
140 return m;
141}

◆ operator*() [1/2]

Matriz Matriz::operator* ( const Matriz & v) const

Operador para multiplicar dos matrices.

Parameters
vLa matriz a multiplicar.
Returns
La matriz resultante de la multiplicaci�n.
Precondition
Las matrices a multiplicar deben tener las mismas dimensiones.
Exceptions
constchar * Las matrices a multiplicar tienen dimensiones incompatibles o la matriz resultante no puede ser creada.

Definition at line 321 of file Matriz.cpp.

322{
323 if (m != v.m || n != v.n)
324 throw "Dimensiones incompatibles para sumar";
325
326 Matriz s(m, n);
327
328 for (unsigned int i = 0; i < m; i++) {
329 for (unsigned int j = 0; j < n; j++) {
330 s.componente[i][j] = componente[i][j] * v.componente[i][j];
331 }
332 }
333
334 return s;
335}

◆ operator*() [2/2]

Matriz Matriz::operator* ( double escalar) const

Operador para multiplicar una matriz por un escalar.

Parameters
escalarEl n�mero por el que se multiplica la matriz.
Returns
La matriz resultante de la multiplicaci�n por escalar.
Exceptions
constchar * La matriz resultante no puede ser creada.

Definition at line 337 of file Matriz.cpp.

338{
339 Matriz s(m, n);
340 for (unsigned int i = 0; i < m; i++) {
341 for (unsigned int j = 0; j < n; j++) {
342 s.componente[i][j] = componente[i][j] * escalar;
343 }
344 }
345
346 return s;
347}

◆ operator+()

Matriz Matriz::operator+ ( const Matriz & v) const

Operador para sumar dos matrices.

Parameters
vLa matriz a sumar.
Returns
La matriz resultante de la suma.
Precondition
Las matrices a sumar deben tener las mismas dimensiones.
Exceptions
constchar * Las matrices a sumar tienen dimensiones incompatibles o la matriz resultante no puede ser creada.

Definition at line 289 of file Matriz.cpp.

290{
291 if (m != v.m || n != v.n)
292 throw "Dimensiones incompatibles para sumar";
293
294 Matriz s(m, n);
295
296 for (unsigned int i = 0; i < m; i++) {
297 for (unsigned int j = 0; j < n; j++) {
298 s.componente[i][j] = componente[i][j] + v.componente[i][j];
299 }
300 }
301
302 return s;
303}

◆ operator-()

Matriz Matriz::operator- ( const Matriz & v) const

Operador para restar dos matrices.

Parameters
vLa matriz a restar.
Returns
La matriz resultante de la resta.
Precondition
Las matrices a restar deben tener las mismas dimensiones.
Exceptions
constchar * Las matrices a restar tienen dimensiones incompatibles o la matriz resultante no puede ser creada.

Definition at line 305 of file Matriz.cpp.

306{
307 if (m != v.m || n != v.n)
308 throw "Dimensiones incompatibles para sumar";
309
310 Matriz s(m, n);
311
312 for (unsigned int i = 0; i < m; i++) {
313 for (unsigned int j = 0; j < n; j++) {
314 s.componente[i][j] = componente[i][j] - v.componente[i][j];
315 }
316 }
317
318 return s;
319}

◆ operator=()

Matriz & Matriz::operator= ( const Matriz & v)

Operador de asignaci�n. Copia una matriz en otra matriz.

Parameters
vLa matriz a copiar.
Returns
La matriz copia. Permite la aplicaci�n en cascada del operador.
Exceptions
constchar * La matriz copia no puede ser creada.

Definition at line 66 of file Matriz.cpp.

66 {
67 if (this == &v) return *this;
68 try {
69 delete[] componente[0];
70 delete[] componente;
71 componente = nullptr;
72
73 m = v.m;
74 n = v.n;
75
76 componente = new tipo*[m];
77 componente[0] = new tipo[m * n];
78 for (unsigned int i = 1; i < m; ++i) {
79 componente[i] = componente[0] + i * n;
80 }
81
82 for (unsigned int i = 0; i < m; ++i) {
83 for (unsigned int j = 0; j < n; ++j) {
84 componente[i][j] = v.componente[i][j];
85 }
86 }
87 } catch (std::bad_alloc &) {
88 throw "No es posible construir una Matriz";
89 }
90
91 return *this;
92}

◆ Redimensionar()

void Matriz::Redimensionar ( unsigned int nuevo_m,
unsigned int nuevo_n )

Obtiene el n�mero de filas de una matriz.

Returns
El n�mero de filas de la matriz.

Definition at line 148 of file Matriz.cpp.

149{
150 if (nuevo_m < 1 || nuevo_n < 1) {
151 throw "Valor fuera de rango para las dimensiones de la matriz";
152 }
153 if (nuevo_m == m && nuevo_n == n) {
154 return;
155 }
156 try {
157 tipo** nueva_componente = new tipo*[nuevo_m];
158 nueva_componente[0] = new tipo[nuevo_m * nuevo_n];
159 for (unsigned int i = 1; i < nuevo_m; ++i) {
160 nueva_componente[i] = nueva_componente[0] + i * nuevo_n;
161 }
162
163 for (unsigned int i = 0; i < nuevo_m; ++i) {
164 for (unsigned int j = 0; j < nuevo_n; ++j) {
165 nueva_componente[i][j] = 0;
166 }
167 }
168 // Copiar los valores de la matriz original que siguen siendo v�lidos
169 for (unsigned int i = 0; i < std::min(m, nuevo_m); ++i) {
170 for (unsigned int j = 0; j < std::min(n, nuevo_n); ++j) {
171 nueva_componente[i][j] = componente[i][j];
172 }
173 }
174
175 // Liberar la memoria de la matriz original
176 delete[] componente[0];
177 delete[] componente;
178
179 // Asignar la nueva matriz redimensionada
180 componente = nueva_componente;
181 m = nuevo_m;
182 n = nuevo_n;
183
184 } catch (std::bad_alloc &) {
185 throw "No es posible construir una Matriz";
186 }
187}

◆ Transpuesta()

Matriz Matriz::Transpuesta ( ) const

Calcula la transpuesta de una matriz.

Returns
La matriz transpuesta.

Definition at line 190 of file Matriz.cpp.

191{
192 Matriz s(n, m); // Crear una matriz con dimensiones transpuestas (n, m)
193 for (unsigned int i = 0; i < m; i++) {
194 for (unsigned int j = 0; j < n; j++) {
195 s.componente[j][i] = componente[i][j]; // Asignar elementos transpuestos
196 }
197 }
198 return s;
199}

◆ operator*

Matriz operator* ( double escalar,
const Matriz & v )
friend

Funci�n amiga para la sobrecarga del operador de multiplicaci�n por escalar.

Permite multiplicar una matriz por un escalar.

Parameters
escalarEl n�mero por el que se multiplica la matriz.
vLa matriz a multiplicar.
Returns
La matriz resultante de la multiplicaci�n por escalar.

Definition at line 390 of file Matriz.cpp.

391{
392 Matriz s(v.m, v.n);
393 for (unsigned int i = 0; i < v.m; i++) {
394 for (unsigned int j = 0; j < v.n; j++) {
395 s.componente[i][j] = escalar * v.componente[i][j];
396 }
397 }
398
399 return s;
400}

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const Matriz & v )
friend

Funci�n amiga para la sobrecarga del operador de inserci�n.

Permite imprimir una matriz mediante un flujo de salida.

Parameters
outEl flujo de salida.
vLa matriz a imprimir.
Returns
El flujo de salida. Permite la aplicaci�n en cascada del operador.

Definition at line 351 of file Matriz.cpp.

352{
353 out << left;
354 out << "┌";
355 for(unsigned int j = 0; j < v.n ; ++j) out << "\t";
356 out << "┐" << endl;
357
358 for(unsigned int i = 0 ; i < v.m ; ++i){
359 out << "│";
360 for(unsigned int j = 0; j < v.n ; ++j) {
361 if (v.componente[i][j] - static_cast<int>(v.componente[i][j]) !=0 || v.componente[i][j] - round(v.componente[i][j]) !=0 ) {
362 out << fixed << setprecision(2) << v.componente[i][j] << "\t" ;
363 } else {
364 out << v.componente[i][j] << "\t" ;
365 }
366 }
367 out << "│";
368 out << endl;
369 }
370
371 out << "└";
372 for(unsigned int j = 0; j < v.n ; ++j) out << "\t";
373 out << "┘";
374
375 out << endl << endl;
376
377 return out;
378}

◆ operator>>

std::istream & operator>> ( std::istream & in,
Matriz & v )
friend

Funci�n amiga para la sobrecarga del operador de extracci�n.

Permite capturar una matriz mediante un flujo de entrada.

Parameters
inEl flujo de entrada.
vLa matriz a capturar.
Returns
El flujo de entrada. Permite la aplicaci�n en cascada del operador.

Definition at line 380 of file Matriz.cpp.

381{
382 for(unsigned int i = 0 ; i < v.m ; ++i){
383 for(unsigned int j = 0; j<v.n; ++j){
384 in >>v.componente[i][j];
385 }
386 }
387 return in;
388}

Member Data Documentation

◆ componente

tipo** Matriz::componente = nullptr
private

Definition at line 180 of file Matriz.hpp.

◆ m

unsigned int Matriz::m
private

Definition at line 179 of file Matriz.hpp.

◆ n

unsigned int Matriz::n
private

Definition at line 179 of file Matriz.hpp.


The documentation for this class was generated from the following files: