StrReverse (CadenaDeTexto)
Sino danos más pistas ...
Saludos, nanus
"Dacar" <d_carp...@abadindustrial.com> escribió en el mensaje
news:O4p1y8BSCHA.2080@tkmsftngp11...
Creo que no se refiere a eso, nanus, sino a un algoritmo de matriz inversa
pero de una
matriz "algebraica"... todo ese rollo del calculo del determinante de una
matriz etc... :P
"nanus" <na...@ono.com> escribió en el mensaje
news:OtiDhXCSCHA.3500@tkmsftngp12...
"Patricia González Conde" <mariamisa...@hotmail.com> escribió en el
mensaje news:eGjvctCSCHA.2080@tkmsftngp11...
nanus <na...@ono.com> escribió en el mensaje de noticias
Oz6As3CSCHA.3968@tkmsftngp12...
¿Qué problema tienes en el cálculo de la inversa? Representación de la
matriz, método, cálculo.....
Un saludo
Dacar <d_carp...@abadindustrial.com> escribió en el mensaje de noticias
O3WSl8CSCHA.1740@tkmsftngp08...
Nota: el libro en cuestión es una maravilla
Calcular adjuntos y determinantes no veo que sea muy complicado, o soy muy
optimista....
Como soy una vaga, estoy esperando a ver si Eduardo Olaz (seguro que lo
tiene resuelto, conociéndole) aparece, pero me está picando el
gusanillo.....
Un saludo
David Girona López <tec...@velesoft.com> escribió en el mensaje de noticias
u5byLcDSCHA.1640@tkmsftngp12...
Invertir una matriz 2x2, 3x3 es algo TRIVIAL, pero invertir cualquier matriz
cuadrada tiene miga.
En el libro biene el algoritmo implementado en C y explicado a las mil
maravillas, y si lo ves te darás cuenta que el uso de punteros es "lo
complicado", es decir, implementar la artilleria para poder poner en marcha
el algoritmo de inversión de matrices es lo 'chungo', y mas si uno está
viciado con vb.
Saludos
Eva Etxebeste <eetxebesteTOYHAS...@hotmail.com> escribió en el
mensaje de noticias OGibjBDSCHA.3736@tkmsftngp11...
El algoritmo, una vez elegido el método, para calcular la inversa de una
matriz 2x2 o NxN es el mismo. Evidentemente, si nos salimos de madre con las
dimensiones tendremos problemas de memoria, eso es de cajón.
El manejo de punteros sí que puede ser el escollo, sobre todo si no estás
habituado a manejarlos. Y, si estás viciado con VB, los punteros no son el
pan nuestro de cada día. La "artillería" a montar para manejar punteros es
siempre la misma (fíjate en otros ejemplo que tengas en el libro) y cuando
trabajas con ellos, te juro que te envicias y después los echas de menos.
Un saludo
David Girona López <tec...@velesoft.com> escribió en el mensaje de noticias
OIWTqlDSCHA.4380@tkmsftngp08...
No recuerdo los algoritmos especiales y tampoco me ha llegado (por más que
intento sincronizar la cuenta) asi que no puedo ayudarte pero bueno que se
le va ha hacer mira en algún libro de algoritmia...
Desconozco ABSOLUTAMENTE TODO, únicamente se que quiere invertir una matriz.
En el tratamiento de matrices y en el caso de la inversión existen varias
formas de atacar el problema, Y NO TODAS SON VÁLIDAS, dependiendo del tamaño
de la matriz, exisnten métodos MUY eficientes enfrente de otros que son
IMPRACTICABLES, con el matiz añadido que el método de inversión de una
matriz 3x3 (por ejemplo) que todos aprendimos en la 'escuela' no SIRVE para
una matriz nxn.
En segundo lugar, y en relación a VB y el uso de punteros del algoritmo que
le he aconsejado. En VB lo va a tener chungo para trasladar ese algoritmo
(en realidad el algoritmo es 'simple', pero el uso de la artilleria de los
punteros que hace ese algoritmo, se lo va a poner 'jodido'). Esto bien en
relación a la primera cuestión, no es lo mismo implementar la inevrsión de
una matriz de dimensión FIJA (el sabe que es 16x16 y santas pascuas) que uno
que haya de manejar la incertidumbre de la dimensión (creo que no he decha
nada nuevo que no sea QUE PROBLEMA ES GENERALIZAR UN PROBLEMA).
Repito que en el libro en cuestión está la explicación, pseudocódigo,el
código fuente,... pero insito que llevarselo a VB no es obvio, no digo que
sea imposible, pero que la traducción no es de perogrullo.
Hará unos años yo tuve el mismo problema (pero en JAVA) teve que invertir
una matriz y acabo de encontrar un ejemplo que prové (este es el algoritmo
del LIBRO):
#if defined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define NR_END 1
#define FREE_ARG char*
void nrerror(char error_text[])
/* Numerical Recipes standard error handler */
{
fprintf(stderr,"Numerical Recipes run-time error...\n");
fprintf(stderr,"%s\n",error_text);
fprintf(stderr,"...now exiting to system...\n");
exit(1);
}
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
int *ivector(long nl, long nh)
/* allocate an int vector with subscript range v[nl..nh] */
{
int *v;
v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
if (!v) nrerror("allocation failure in ivector()");
return v-nl+NR_END;
}
unsigned char *cvector(long nl, long nh)
/* allocate an unsigned char vector with subscript range v[nl..nh] */
{
unsigned char *v;
v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned
char)));
if (!v) nrerror("allocation failure in cvector()");
return v-nl+NR_END;
}
unsigned long *lvector(long nl, long nh)
/* allocate an unsigned long vector with subscript range v[nl..nh] */
{
unsigned long *v;
v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
if (!v) nrerror("allocation failure in lvector()");
return v-nl+NR_END;
}
double *dvector(long nl, long nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
double *v;
v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
if (!v) nrerror("allocation failure in dvector()");
return v-nl+NR_END;
}
float **matrix(long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
double **dmatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
int **imatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
int **m;
/* allocate pointers to rows */
m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
long newrl, long newcl)
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
{
long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
float **m;
/* allocate array of pointers to rows */
m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in submatrix()");
m += NR_END;
m -= newrl;
/* set pointers to rows */
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
and ncol=nch-ncl+1. The routine should be called with the address
&a[0][0] as the first argument. */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in convert_matrix()");
m += NR_END;
m -= nrl;
/* set pointers to rows */
m[nrl]=a-ncl;
for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long
ndh)
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
float ***t;
/* allocate pointers to pointers to rows */
t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
if (!t) nrerror("allocation failure 1 in f3tensor()");
t += NR_END;
t -= nrl;
/* allocate pointers to rows and set pointers to them */
t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
t[nrl] += NR_END;
t[nrl] -= ncl;
/* allocate rows and set pointers to them */
t[nrl][ncl]=(float *)
malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
t[nrl][ncl] += NR_END;
t[nrl][ncl] -= ndl;
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
for(i=nrl+1;i<=nrh;i++) {
t[i]=t[i-1]+ncol;
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
}
/* return pointer to array of pointers to rows */
return t;
}
void free_vector(float *v, long nl, long nh)
/* free a float vector allocated with vector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_ivector(int *v, long nl, long nh)
/* free an int vector allocated with ivector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_cvector(unsigned char *v, long nl, long nh)
/* free an unsigned char vector allocated with cvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_lvector(unsigned long *v, long nl, long nh)
/* free an unsigned long vector allocated with lvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_dvector(double *v, long nl, long nh)
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
/* free a float matrix allocated by matrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
/* free an int matrix allocated by imatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
/* free a submatrix allocated by submatrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
/* free a matrix allocated by convert_matrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh)
/* free a float f3tensor allocated by f3tensor() */
{
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
free((FREE_ARG) (t[nrl]+ncl-NR_END));
free((FREE_ARG) (t+nrl-NR_END));
}
#else /* ANSI */
/* traditional - K&R */
#include <stdio.h>
#define NR_END 1
#define FREE_ARG char*
void nrerror(error_text)
char error_text[];
/* Numerical Recipes standard error handler */
{
void exit();
fprintf(stderr,"Numerical Recipes run-time error...\n");
fprintf(stderr,"%s\n",error_text);
fprintf(stderr,"...now exiting to system...\n");
exit(1);
}
float *vector(nl,nh)
long nh,nl;
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
int *ivector(nl,nh)
long nh,nl;
/* allocate an int vector with subscript range v[nl..nh] */
{
int *v;
v=(int *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(int)));
if (!v) nrerror("allocation failure in ivector()");
return v-nl+NR_END;
}
unsigned char *cvector(nl,nh)
long nh,nl;
/* allocate an unsigned char vector with subscript range v[nl..nh] */
{
unsigned char *v;
v=(unsigned char *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(unsigned
char)));
if (!v) nrerror("allocation failure in cvector()");
return v-nl+NR_END;
}
unsigned long *lvector(nl,nh)
long nh,nl;
/* allocate an unsigned long vector with subscript range v[nl..nh] */
{
unsigned long *v;
v=(unsigned long *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(long)));
if (!v) nrerror("allocation failure in lvector()");
return v-nl+NR_END;
}
double *dvector(nl,nh)
long nh,nl;
/* allocate a double vector with subscript range v[nl..nh] */
{
double *v;
v=(double *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(double)));
if (!v) nrerror("allocation failure in dvector()");
return v-nl+NR_END;
}
float **matrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((unsigned int)((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
double **dmatrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((unsigned int)((nrow+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((unsigned
int)((nrow*ncol+NR_END)*sizeof(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
int **imatrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
int **m;
/* allocate pointers to rows */
m=(int **) malloc((unsigned int)((nrow+NR_END)*sizeof(int*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(int *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(int)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
float **a;
long newcl,newrl,oldch,oldcl,oldrh,oldrl;
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
{
long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
float **m;
/* allocate array of pointers to rows */
m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in submatrix()");
m += NR_END;
m -= newrl;
/* set pointers to rows */
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **convert_matrix(a,nrl,nrh,ncl,nch)
float *a;
long nch,ncl,nrh,nrl;
/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
and ncol=nch-ncl+1. The routine should be called with the address
&a[0][0] as the first argument. */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in convert_matrix()");
m += NR_END;
m -= nrl;
/* set pointers to rows */
m[nrl]=a-ncl;
for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float ***f3tensor(nrl,nrh,ncl,nch,ndl,ndh)
long nch,ncl,ndh,ndl,nrh,nrl;
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
float ***t;
/* allocate pointers to pointers to rows */
t=(float ***) malloc((unsigned int)((nrow+NR_END)*sizeof(float**)));
if (!t) nrerror("allocation failure 1 in f3tensor()");
t += NR_END;
t -= nrl;
/* allocate pointers to rows and set pointers to them */
t[nrl]=(float **) malloc((unsigned
int)((nrow*ncol+NR_END)*sizeof(float*)));
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
t[nrl] += NR_END;
t[nrl] -= ncl;
/* allocate rows and set pointers to them */
t[nrl][ncl]=(float *) malloc((unsigned
int)((nrow*ncol*ndep+NR_END)*sizeof(float)));
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
t[nrl][ncl] += NR_END;
t[nrl][ncl] -= ndl;
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
for(i=nrl+1;i<=nrh;i++) {
t[i]=t[i-1]+ncol;
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
}
/* return pointer to array of pointers to rows */
return t;
}
void free_vector(v,nl,nh)
float *v;
long nh,nl;
/* free a float vector allocated with vector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_ivector(v,nl,nh)
int *v;
long nh,nl;
/* free an int vector allocated with ivector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_cvector(v,nl,nh)
long nh,nl;
unsigned char *v;
/* free an unsigned char vector allocated with cvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_lvector(v,nl,nh)
long nh,nl;
unsigned long *v;
/* free an unsigned long vector allocated with lvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_dvector(v,nl,nh)
double *v;
long nh,nl;
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_matrix(m,nrl,nrh,ncl,nch)
float **m;
long nch,ncl,nrh,nrl;
/* free a float matrix allocated by matrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_dmatrix(m,nrl,nrh,ncl,nch)
double **m;
long nch,ncl,nrh,nrl;
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_imatrix(m,nrl,nrh,ncl,nch)
int **m;
long nch,ncl,nrh,nrl;
/* free an int matrix allocated by imatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_submatrix(b,nrl,nrh,ncl,nch)
float **b;
long nch,ncl,nrh,nrl;
/* free a submatrix allocated by submatrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_convert_matrix(b,nrl,nrh,ncl,nch)
float **b;
long nch,ncl,nrh,nrl;
/* free a matrix allocated by convert_matrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_f3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
float ***t;
long nch,ncl,ndh,ndl,nrh,nrl;
/* free a float f3tensor allocated by f3tensor() */
{
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
free((FREE_ARG) (t[nrl]+ncl-NR_END));
free((FREE_ARG) (t+nrl-NR_END));
}
#endif /* ANSI */
/*******************************************************************/
/* EJEMPLO DE QUE EL PROCEDIMIENTO DE INVERSION FUNCIONA */
/* LA INVERSA DE LA MATRIZ DEFINIDA ES: */
/*************/
/* 2 8 6 */
/* 4 2 -2 */
/* 3 -1 1 */
/*************/
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#define NRANSI
#include <nrutil.h>
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
void main ()
{
int *indxc,*indxr,*ipiv;
int i,icol,irow,j,k,l,ll;
float big,dum,pivinv,temp;
int n=3, m=1;
float a[3][3]=
{
{ 0.0, 1.0/10.0, 1.0/5.0 },
{ 1.0/14.0, 4.0/35.0, -1.0/5.0 },
{ 1.0/14.0, -13.0/70.0, 1.0/5.0 }
};
float b[3][1]={
{ 1.0},
{ 2.0},
{ 3.0},
};
clrscr();
printf("PROYECTO FIN DE CARRERA\n\n");
printf("EJEMPLO PARA COMPROBAR QUE EL PROCEDIMIENTO DE INVERSION
FUNCIONA\n\n");
printf("MATRIZ A INVERTIR: \n\n");
printf(" 0.0, 1.0/10.0, 1.0/5.0\n");
printf(" 1.0/14.0, 4.0/35.0, -1.0/5.0\n");
printf(" 1.0/14.0, -13.0/70.0, 1.0/5.0\n\n");
indxc=ivector(0,n-1);
indxr=ivector(0,n-1);
ipiv=ivector(0,n-1);
for (j=0;j<n;j++) ipiv[j]=0;
for (i=0;i<n;i++) {
big=0.0;
for (j=0;j<n;j++)
if (ipiv[j] != 1)
for (k=0;k<n;k++) {
if (ipiv[k] == 0) {
if (fabs(a[j][k]) >= big) {
big=fabs(a[j][k]);
irow=j;
icol=k;
}
} else if (ipiv[k] > 1) nrerror("gaussj: Singular Matrix-1");
}
++(ipiv[icol]);
if (irow != icol) {
for (l=0;l<n;l++) SWAP(a[irow][l],a[icol][l])
/* for (l=0;l<m;l++) SWAP(b[irow][l],b[icol][l])*/
}
indxr[i]=irow;
indxc[i]=icol;
if (a[icol][icol] == 0.0) nrerror("gaussj: Singular Matrix-2");
pivinv=1.0/a[icol][icol];
a[icol][icol]=1.0;
for (l=0;l<n;l++) a[icol][l] *= pivinv;
/* for (l=0;l<m;l++) b[icol][l] *= pivinv; */
for (ll=0;ll<n;ll++)
if (ll != icol) {
dum=a[ll][icol];
a[ll][icol]=0.0;
for (l=0;l<n;l++) a[ll][l] -= a[icol][l]*dum;
/* for (l=0;l<m;l++) b[ll][l] -= b[icol][l]*dum; */
}
}
for (l=n-1;l>=0;l--) {
if (indxr[l] != indxc[l])
for (k=0;k<n;k++)
SWAP(a[k][indxr[l]],a[k][indxc[l]]);
}
printf("INVERSA DE LA MATRIZ:\n\n");
for (i=0;i<n;i++){
printf("\n");
for (j=0;j<n;j++){
printf("FILA %i",i);
printf(" COLUMNA %i",j);
printf(" =%f",a[i][j]);
printf("\n");
}
}
free_ivector(ipiv,0,n-1);
free_ivector(indxr,0,n-1);
free_ivector(indxc,0,n-1);
}
#undef SWAP
#undef NRANSI
T.S.U. Mario Osorio <HotB...@cantv.net> escribió en el mensaje de noticias
#ihuo3HSCHA.4472@tkmsftngp09...
> ĄĄEsa es mi Eva!!
>
>
--
Saludos:
Francisco J. García Aguado
"Eva Etxebeste" <eetxebesteTOYHAS...@hotmail.com> escribió en
el mensaje news:ePrmjjHSCHA.3648@tkmsftngp11...
> A ver David, no nos sulfuremos que no llegamos a nada y Dacar sigue
> esperando su inversa... Fíjate que yo no hablé en ningún momento de
matrices
> de dimensión fija ni de 2x2 ó 3x3 que nos permitirían aplicar la regla de
> Sarrus que no todos hemos aprendido, ni falta que hace. Si en algo soy
> asquerosilla es a la hora de generalizar.
>
> Si mi memoria no me falla (que suele hacerlo frecuentemente) el producto
de
> una matriz por su inversa es la matriz unidad. De aquí (y de alguna otra
> cosilla) después de unos "sencillos" pasos, se llega a que la matriz
inversa
> a una A dada está definido por: Adj(A)/ (Determinante de A). Y cada
elemento
> de la adjunta se obtiene hallando los adjuntos de la original y
> trasponiéndolos.
>
> Adjunto un pequeño ejemplo, hecho deprisa y corriendo, pero que creo que
le
> servirá a Dacar. Supone que la matriz es cuadrada, mira si es regular y
> calcula determinante e inversa. El código de cálculo está en un módulo
> aparte, la hoja es solo para poder ver el resultado con cierta comodidad.
> Por razones obvias, la presentación la he limitado a matrices 10x10.
>
> Un saludo
>
> P.D.: Es lentujo, poco eficiente y tendrá errores. Si alguien se siente
con
> ánimos de meter mano ar bisho, me encantaría recibir el código corregido.
>
>
> David Girona López <tec...@velesoft.com> escribió en el mensaje de
noticias
> uIzfouFSCHA.1668@tkmsftngp13...
Búho <pacoQU...@nemo.es> escribió en el mensaje de noticias
uBWX#IJSCHA.3620@tkmsftngp08...
Con una matriz de 100 x 100 apenas tarda 1 segundo en calcular su inversa.
Le he puesto un generador que carga la matriz.
Creo que será fácil de adaptar para lo que tú la quieres.
________________________________________________________
' Filas y columnas de la matriz
Const conFilCol As Long = 6
Dim Matriz(conFilCol - 1, conFilCol - 1) As Double
Dim MatrizA(conFilCol - 1, conFilCol - 1) As Double
Public Sub Prueba()
CargarMatriz conFilCol
Debug.Print "Matriz Inversa Ejemplo"
MatrizInversa Matriz, MatrizA, conFilCol
ImprimirMatriz MatrizA, "Inversa", conFilCol, conFilCol
End Sub
Public Sub CargarMatriz(n)
Dim i As Long, j As Long
' Crea una matriz de n filas y columnas
' con valores enteros entre 1 y 10
Randomize Timer
For i = 0 To n - 1
For j = 0 To n - 1
Matriz(i, j) = Int(Rnd * 10) + 1
Next j
Next i
End Sub
Public Sub MatrizInversa( _
Matriz() As Double, _
Inversa() As Double, _
n As Long)
' MatrizInversa Calcula la Inversa de una matriz
' Parámetros:
' Matriz = Matriz() a ser invertida
' n = numero de filas o columnas de la Matriz
' Salida:
' Inversa() = La Matriz invertida
Dim i As Long, j As Long, k As Long, l As Long
Dim div As Double
' Pone Inversa() como una matriz identidad
MatrizIdentidad Inversa, n
For i = 0 To n - 1
div = Matriz(i, i)
For j = 0 To n - 1
Matriz(i, j) = Matriz(i, j) / div
Inversa(i, j) = Inversa(i, j) / div
Next j
For k = 0 To n - 1
div = Matriz(k, i)
If k <> i Then
For l = 0 To n - 1
Matriz(k, l) = Matriz(k, l) - div * Matriz(i, l)
Inversa(k, l) = Inversa(k, l) - div * Inversa(i, l)
Next l
End If
Next k
Next i
End Sub
Public Sub ImprimirMatriz( _
ByRef Matriz() As Double, _
NombreMatriz As String, _
Filas As Long, _
Columnas As Long)
' ImprimirMatriz visualiza Matriz()
' en la ventana de depuración
' Parámetros:
' Matriz() = Matriz que se va a mostrar
' Filas = numero de filas en Matriz()
' Columnas = numero de columnas en Matriz()
Dim i, j As Long
Dim strTabuladores As String
Dim strMostrar As String
Debug.Print ""
Debug.Print " La Matriz " & NombreMatriz & " es:"
strTabuladores = Chr$(9) & Chr$(9) & Chr$(9)
strMostrar = strMostrar & strTabuladores & CStr(0)
strTabuladores = strTabuladores & Chr$(9) & Chr$(9)
For j = 1 To Columnas - 1
strMostrar = strMostrar & strTabuladores & CStr(j)
Next j
Debug.Print strMostrar
strMostrar = ""
strTabuladores = Chr$(9)
For i = 0 To Filas - 1
strMostrar = strMostrar & CStr(i) & strTabuladores
For j = 0 To Columnas - 1
strMostrar = strMostrar & CStr(Matriz(i, j)) & strTabuladores
Next j
Debug.Print strMostrar
strMostrar = ""
Next i
Debug.Print " "
End Sub
Public Sub MatrizIdentidad( _
ByRef Matriz() As Double, _
n As Long)
' MatrizIdentidad crea una Matriz Identidad
' Parámetros:
' n = numero de filas y columnas de la Matriz
' Salida:
' Matriz() = Matriz Identidad
Dim i As Long
MatrizCero Matriz, n, n ' Pone a 0 todos los elementos
For i = 0 To n - 1 ' Cada elemento de la diagonal
Matriz(i, i) = 1 ' lo pone a 1
Next
End Sub
Public Sub MatrizCero( _
ByRef Matriz() As Double, _
Filas As Long, _
Columnas As Long)
' MatrizCero crea una Matriz con todos los elementos a 0
' Parámetros:
' Filas = numero de filas en Matriz()
' Columnas = número de columnas en Matriz()
' salida:
' Matriz = zero Matriz to be created
Dim i, j As Long
For i = 0 To Filas - 1 ' cada elemento de la matriz
For j = 0 To Columnas - 1
Matriz(i, j) = 0 ' lo pone a 0
Next j
Next i
End Sub
Saludos desde la calle Estafeta de Pamplona
Eduardo
"Dacar" <d_carp...@abadindustrial.com> escribió en el mensaje
news:O4p1y8BSCHA.2080@tkmsftngp11...
Eduardo Olaz <edu...@olaz.net> escribió en el mensaje de noticias
eTeqnQKSCHA.4088@tkmsftngp09...
Eva Etxebeste <eetxebesteTOYHAS...@hotmail.com> escribió en el
mensaje de noticias e6R9E2JSCHA.3252@tkmsftngp12...
No es mío, es de úna página de un inglés, o americano; página que no
recuerdo cuál era.
Como pretendía usarlo, lo traduje "al cristiano"
Dejé una línea en inglés para recordármelo.
De todas formas Eva, no soy un bicho raro, ni el típico "programador loco"
que se dedique a parir o coleccionar algoritmos raros.
Por ejemplo este algoritmo lo tengo por una circunstancia concreta.
Saludos ;)))
Eduardo
Pero ambos sabemos que el cálculo de matrices y determinantes entra en juego
en mil y un áreas. Yo tengo una calculadorita que me resuelve el tema, pero
mientras pensaba en el tema se me vino a la cabeza que quedaría muy elegante
construirse una clase, y ahí se me materializó la cara del Sr. Eduardo Olaz.
Así de simple, asociación de ideas.
Eduardo Olaz <edu...@olaz.net> escribió en el mensaje de noticias
eXUdgIPSCHA.2400@tkmsftngp13...
Reto aceptado
Me pondré en ello.
Saludos:
Eduardo
"David Girona López" <tec...@velesoft.com> escribió en el mensaje
news:u5byLcDSCHA.1640@tkmsftngp12...
"Manuel Romero" <mrom...@hotmail.com> escribió en el mensaje
news:#z5lrV7SCHA.3720@tkmsftngp08...