mtransp.c 1020 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* mtransp.c
  2. *
  3. * Matrix transpose
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int n;
  10. * double A[n*n], T[n*n];
  11. *
  12. * mtransp( n, A, T );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. *
  19. * T[r][c] = A[c][r]
  20. *
  21. *
  22. * Transposes the n by n square matrix A and puts the result in T.
  23. * The output, T, may occupy the same storage as A.
  24. *
  25. *
  26. *
  27. */
  28. mtransp( n, A, T )
  29. int n;
  30. double *A, *T;
  31. {
  32. int i, j, np1;
  33. double *pAc, *pAr, *pTc, *pTr, *pA0, *pT0;
  34. double x, y;
  35. np1 = n+1;
  36. pA0 = A;
  37. pT0 = T;
  38. for( i=0; i<n-1; i++ ) /* row index */
  39. {
  40. pAc = pA0; /* next diagonal element of input */
  41. pAr = pAc + n; /* next row down underneath the diagonal element */
  42. pTc = pT0; /* next diagonal element of the output */
  43. pTr = pTc + n; /* next row underneath */
  44. *pTc++ = *pAc++; /* copy the diagonal element */
  45. for( j=i+1; j<n; j++ ) /* column index */
  46. {
  47. x = *pAr;
  48. *pTr = *pAc++;
  49. *pTc++ = x;
  50. pAr += n;
  51. pTr += n;
  52. }
  53. pA0 += np1; /* &A[n*i+i] for next i */
  54. pT0 += np1; /* &T[n*i+i] for next i */
  55. }
  56. *pT0 = *pA0; /* copy the diagonal element */
  57. }