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
please help
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;
}