Help with matrix transpose

This is a discussion on Help with matrix transpose within the C Programming forums, part of the General Programming Boards category; hi i have a program that compiles and works it scans points from a txt file (that i attached) into ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    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;
    }
    Attached Files Attached Files

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    thanx alot...
    would u kno how to multiply A by At by any chance?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    do you have a formula? have you tried to program it? Could you post your attemp result?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    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;
    }

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    thank you so much u helped me alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  4. Help!! Tips on Matrix Calculator Program please!
    By skanxalot in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2002, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21