Allora ho provato due versioni entrambe su Ubuntu, la prima calcola tutto nel main e funziona perfettamente:
codice:
#include <stdio.h>
# define VECTOR float*
#define MATRIX float*
int main() {
MATRIX Xy;
MATRIX Trasp;
int i,j;
int m=2,n=3;
Xy=calloc(m*(n+1),sizeof(float));;
Trasp=calloc(m*(n+1),sizeof(float));;
for (i = 0; i < m; i++) {
for (j = 0; j < n+1; j++) {
printf("el[%d][%d]= ", i,j);
fflush(stdout);
scanf("\%ff",&Xy[i*(n+1)+j]);
}
printf("\n");
}
for (i = 0; i < m; i++) {
for (j = 0; j < n+1; j++) {
printf("el[%d][%d]=%ff ", i,j,Xy[i*(n+1)+j]);
}
printf("\n");
}
for (i = 0; i < n+1; i++) {
for (j = 0; j < m; j++) {
Trasp[i*(m)+j]=Xy[j*(n+1)+i];
}
printf("\n");
}
for (i = 0; i < n+1; i++) {
for (j = 0; j < m; j++) {
printf("el[%d][%d]=%ff ", i,j,Trasp[i*(m)+j]);
}
printf("\n");
}
return 0;
}
con output corretto:
el[0][0]=4.000000f el[0][1]=5.000000f el[0][2]=6.000000f el[0][3]=7.000000f
el[1][0]=2.000000f el[1][1]=3.000000f el[1][2]=4.000000f el[1][3]=5.000000f
el[0][0]=4.000000f el[0][1]=7.000000f
el[1][0]=4.000000f el[1][1]=5.000000f
el[2][0]=2.000000f el[2][1]=5.000000f
el[3][0]=6.000000f el[3][1]=3.000000f
invece se utilizzo un metodo esterno, come segue:
codice:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <xmmintrin.h>
#define MATRIX float*
#define VECTOR float*
//float inversa(int n, MATRIX c, MATRIX inversa);
void trasposta(MATRIX Xy,MATRIX T,int n, int m);
void* get_block(int size, int elements) {
return _mm_malloc(elements * size, 16);
}
void free_block(void* p) {
_mm_free(p);
}
MATRIX alloc_matrix(int rows, int cols) {
return (MATRIX) get_block(sizeof(float),rows*cols);
}
void dealloc_matrix(MATRIX mat) {
free_block(mat);
}
float frand() {
float r = (float) rand();
return r / RAND_MAX;
}
MATRIX random_input(int m, int n) {
int i, j;
MATRIX A = alloc_matrix(m,n+1);
float x, y, e;
float* beta = calloc(n,sizeof(float));
for (i = 0; i < n; i++)
beta[i] = frand();
e = frand()*0.2;
for (i = 0; i < m; i++) {
y = 0;
for (j = 0; j < n; j++) {
if (j < n-1)
x = frand();
else
x = 1.0;
A[i*(n+1)+j] = x;
y += x*beta[j];
}
A[i*(n+1)+n] = y*(1+frand()*e-e/2);
}
free(beta);
return A;
}
void trasposta(MATRIX Xy,MATRIX T,int n, int m)
{
int i,j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
T[i*(m)+j]=Xy[j*(n)+i];
}
printf("\n");
}
}
int main() {
MATRIX Xy;
MATRIX Trasp;
int i,j;
int m=2,n=3;
Xy=calloc(m*(n+1),sizeof(float));;
Trasp=calloc(m*(n+1),sizeof(float));
for (i = 0; i < m; i++) {
for (j = 0; j < n+1; j++) {
printf("el[%d][%d]= ", i,j);
fflush(stdout);
scanf("\%ff",&Xy[i*(n+1)+j]);
}
printf("\n");
}
for (i = 0; i < m; i++) {
for (j = 0; j < n+1; j++) {
printf("el[%d][%d]=%ff ", i,j,Xy[i*(n+1)+j]);
}
printf("\n");
}
trasposta(Xy,Trasp,m,n+1);
for (i = 0; i < n+1; i++) {
for (j = 0; j < m; j++) {
printf("el[%d][%d]=%ff ", i,j,Trasp[i*(m)+j]);
}
printf("\n");
}
return 0;
}
come risultato esce qualcosa di sballato del tipo:
el[0][0]=1.000000f el[0][1]=2.000000f el[0][2]=3.000000f el[0][3]=4.000000f
el[1][0]=5.000000f el[1][1]=6.000000f el[1][2]=7.000000f el[1][3]=8.000000f
el[0][0]=1.000000f el[0][1]=3.000000f
el[1][0]=0.000000f el[1][1]=0.000000f
el[2][0]=2.000000f el[2][1]=4.000000f
el[3][0]=0.000000f el[3][1]=0.000000f
Che cosa sbaglio?!?!
PS: vi prego di ignorare i primi metodi come alloc_matrix o random_input sono dei metodi di supporto che mi servono piu avanti