# Help with matrix transpose

• 12-07-2006
sass208
Help with matrix transpose
hi i have a program that compiles and works
it scans points from a txt file (that i attached) into a 2D matrix and from this matrix i calculated and generated the matrix 'A' . Now i need to transpose this A matrix

i was given this subroutine in my linear_algebra.h library to find the transpose of a matrix
Code:

// Transposition.
void matrix_transpose(double m1[], double mr[], int h, int w)
{
int i, j;

for(i = 0; i < h; ++i)
for(j = 0; j < w; ++j)
mr[in2d(j, i, h)] = m1[in2d(i, j, w)];

return;
}

but it doesnt work....

I need to print the transpose of my A matrix

heres my program
and i attached the linear algebra library that i was provided which has the transpose subroutine in it
Code:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "linear_algebra.h"

int main(){
int n,i,j;
double **a;    /* this is the array of points name */
double **A;        /* this is the array of 2n x 9*/
double **At; /* this is the array of A transpose */
int num_rows; /* this variable will be used for the first  dimension */
int num_col; /* this variable will be used for the second dimension */

FILE * ifile;
ifile = fopen ("points.txt","r");
fscanf(ifile, "%d\n", &n);/*read in the number of points N.*/
printf("%d\n", n);

num_rows = n;
num_col = 6;

/*  allocate storage for an array of pointers */
a = malloc(num_rows * sizeof(double *));

/* for each pointer, allocate storage for an array of doubles */
for (i = 0; i < num_rows; i++) {
a[i] = malloc(num_col * sizeof(double));
}

/* assign the value to each element        */
for (i = 0; i < num_rows; i++) {
for (j = 0; j < num_col; j++) {
fscanf(ifile,"%lf ", &a[i][j]);
}
}

/*  malloc again for 2nd array */
A = malloc((2*n) * sizeof(double *));

for (i = 0; i < (2*n); i++) {
A[i] = malloc(9 * sizeof(double));
}

/* create a 2*n X 9 matrix*/
for (i = 0; i < num_rows; i++) {
A[2*i][0]=0;
A[2*i][1]=0;
A[2*i][2]=0;
A[2*i][3]=(-a[i][5])*a[i][0];
A[2*i][4]=(-a[i][5])*a[i][1];
A[2*i][5]=(-a[i][5])*a[i][2];
A[2*i][6]=(a[i][4])*a[i][0];
A[2*i][7]=(a[i][4])*a[i][1];
A[2*i][8]=(a[i][4])*a[i][2];

A[2*i+1][0]=(a[i][5])*a[i][0];
A[2*i+1][1]=(a[i][5])*a[i][1];
A[2*i+1][2]=(a[i][5])*a[i][2];
A[2*i+1][3]=0;
A[2*i+1][4]=0;
A[2*i+1][5]=0;
A[2*i+1][6]=(-a[i][3])*a[i][0];
A[2*i+1][7]=(-a[i][3])*a[i][1];
A[2*i+1][8]=(-a[i][3])*a[i][2];
}

/*  malloc again for 3rd array */
At = malloc((2*n) * sizeof(double *));

for (i = 0; i < (2*n); i++) {
At[i] = malloc(9 * sizeof(double));
}

for (i = 0; i < 2*n; i++) {
for (j = 0; j < 9; j++) {
printf("%6.4lf ", A[i][j]);
}
}

/* now for each pointer, free its array of dbles */
for (i = 0; i < num_rows; i++) {
free(a[i]);
}
/* now free the array of pointers */
free(a);

/* free for 2nd array */
for (i = 0; i < (n*2); i++) {
free(A[i]);
}

free(A);

/* free for 3rd array */
for (i = 0; i < (n*2); i++) {
free(At[i]);
}

free(At);

fclose(ifile);
return 0;
}

• 12-08-2006
vart
The given procedure works with linear array as if it stores the matrix
the function
in2d(j, i, h) just retreives the index in the linear array based on the row/col numbers and row width

In your case the Matrixes are stored in the 2-dimentional array
so you can just do
At[i][j] = A[j][i]
for all i,j needed
• 12-08-2006
sass208
thanx alot...
would u kno how to multiply A by At by any chance?
• 12-08-2006
vart
do you have a formula? have you tried to program it? Could you post your attemp result?
• 12-08-2006
sass208
this is the formula but it has the in2d again

Code:

/ Multiplication.
// Need (wm1 == hm2) && (hm1 == hmr) && (wm2 == wmr).
void matrix_mult(double m1[], double m2[], double mr[],
int hm1, int wm1, int wm2)
{
int i, j, k;
double sum;

for(i = 0; i < hm1; ++i)
for(j = 0; j < wm2; ++j) {
sum = 0;
for(k = 0; k < wm1; ++k)
sum += m1[in2d(i, k, wm1)] * m2[in2d(k, j, wm2)];

mr[in2d(i, j, wm2)] = sum;
}

return;
}

i tried to change the code like you did last time and i did this but it doesnt work

Code:

/*matrix multiplication*/

for(i = 0; i < 9; i++){
for(j = 0; j < 9 ; j++) {
for(k = 0; k < 2*n; k++){

W[i][j]+= (A[i][k])*(At[k][j]);
}
}
}

ok and heres my whole program...if you take out the multiplication it works so far

Code:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "linear_algebra.h"

int main(){
int n,i,j,k;
double **a;    /* this is the array of points name */
double **A;        /* this is the array of 2n x 9*/
double **At; /* this is the array of A transpose */
double **W; /*this is the 9 x 9 arrray*/
int num_rows; /* this variable will be used for the first  dimension */
int num_col; /* this variable will be used for the second dimension */

FILE * ifile;
ifile = fopen ("points.txt","r");
fscanf(ifile, "%d\n", &n);/*read in the number of points N.*/
printf("%d\n", n);

num_rows = n;
num_col = 6;

/*  allocate storage for an array of pointers */
a = malloc(num_rows * sizeof(double *));

/* for each pointer, allocate storage for an array of doubles */
for (i = 0; i < num_rows; i++) {
a[i] = malloc(num_col * sizeof(double));
}

/* assign the value to each element        */
for (i = 0; i < num_rows; i++) {
for (j = 0; j < num_col; j++) {
fscanf(ifile,"%lf ", &a[i][j]);
}
}

/*  malloc again for 2nd array */
A = malloc((2*n) * sizeof(double *));

for (i = 0; i < (2*n); i++) {
A[i] = malloc(9 * sizeof(double));
}

/* create a 2*n X 9 matrix*/
for (i = 0; i < num_rows; i++) {
A[2*i][0]=0;
A[2*i][1]=0;
A[2*i][2]=0;
A[2*i][3]=(-a[i][5])*a[i][0];
A[2*i][4]=(-a[i][5])*a[i][1];
A[2*i][5]=(-a[i][5])*a[i][2];
A[2*i][6]=(a[i][4])*a[i][0];
A[2*i][7]=(a[i][4])*a[i][1];
A[2*i][8]=(a[i][4])*a[i][2];

A[2*i+1][0]=(a[i][5])*a[i][0];
A[2*i+1][1]=(a[i][5])*a[i][1];
A[2*i+1][2]=(a[i][5])*a[i][2];
A[2*i+1][3]=0;
A[2*i+1][4]=0;
A[2*i+1][5]=0;
A[2*i+1][6]=(-a[i][3])*a[i][0];
A[2*i+1][7]=(-a[i][3])*a[i][1];
A[2*i+1][8]=(-a[i][3])*a[i][2];
}

/*  malloc again for 3rd array */
At = malloc((2*n) * sizeof(double *));

for (i = 0; i < (2*n); i++) {
At[i] = malloc(9 * sizeof(double));
}

/* matrix transpose*/
for (i = 0; i < 2*n; i++) {
for (j = 0; j < 9; j++) {
At[i][j]=A[j][i];

}
}

/*  malloc again for 4th array */
W = malloc((9) * sizeof(double *));

for (i = 0; i < (9); i++) {
W[i] = malloc(9 * sizeof(double));
}

/*matrix multiplication*/

for(i = 0; i < 9; i++){
for(j = 0; j < 9 ; j++) {
for(k = 0; k < 2*n; k++){

W[i][j]+= (A[i][k])*(At[k][j]);
}
}
}

for (i = 0; i < 9; i++) {
for (j = 0; j <9 ; j++) {
printf("%6.4lf ", W[i][j]);
}
}
/* now for each pointer, free its array of dbles */

for (i = 0; i < num_rows; i++) {
free(a[i]);
}
/* now free the array of pointers */
free(a);

/* free for 2nd array */
for (i = 0; i < (n*2); i++) {
free(A[i]);
}

free(A);

/* free for 3rd array */
for (i = 0; i < (9); i++) {
free(At[i]);
}

free(At);
/* free for 4th array */
for (i = 0; i < (9); i++) {
free(W[i]);
}

free(W);

fclose(ifile);
return 0;
}

• 12-09-2006
vart
W[i][j]+= (A[i][k])*(At[k][j]);
you should iitialize W[i][j] before entering the loop, otherwise the result will contain garbage
• 12-09-2006
sass208
thank you so much u helped me alot