Thefaceofbo

GE (C)

Nov 10th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int ge_fw(float *, int, int, float *);
  5. int ge_bw(float *, int, int, float *);
  6.  
  7. int main(void) {
  8.    int rows,cols,x,y = 0;
  9.    float *i, *out, *outF = NULL;
  10.    rows = 3;
  11.    cols = 3;
  12.    i = (float *)malloc(sizeof(float)*rows*cols);
  13.    out = (float *)malloc(sizeof(float)*rows*cols);
  14.    outF = (float *)malloc(sizeof(float)*rows*cols);
  15.    /* Set vals for initial Matrix */
  16.    i[0] = 1;
  17.    i[1] = 1;
  18.    i[2] = 0;
  19.    i[3] = 1;
  20.    i[4] = 1;
  21.    i[5] = 4;
  22.    i[6] = 1;
  23.    i[7] = 1;
  24.    i[8] = 8;
  25.    ge_fw(i, rows, cols, out);
  26.    ge_bw(out, rows, cols, outF);
  27.    for (x=0; x < rows; x++) {
  28.       for (y=0; y < cols; y++) {
  29.          printf("%f ", i[x*cols + y]);
  30.       }
  31.       printf("\n");
  32.    }
  33.    printf("\n");
  34.    for (x=0; x < rows; x++) {
  35.       for (y=0; y < cols; y++) {
  36.          printf("%f ", outF[x*cols + y]);
  37.       }
  38.       printf("\n");
  39.    }
  40.    return 0;
  41. }
  42.  
  43. int ge_fw(float *u, int rows, int cols, float *m) {
  44.    int r,c,x,v = 0;
  45.    int toggle = 1; /* Toggle used twice to save an int (why not) */
  46.    float scale,temp = 0;
  47.    /* Error Check */
  48.    if ((u == NULL) || (m == NULL)) { printf("The array must be allocated for assignment"); return -1; }
  49.    /* Duplicate */
  50.    for (x=0; x<rows*cols; x++) {
  51.       m[x] = u[x];
  52.    }
  53.    /* Begin GE */
  54.    for (c=0; c < rows; c++) {
  55.       /* Swap rows if zero */
  56.       toggle = 1;
  57.       if (m[c*cols + c] == 0) {
  58.          toggle = 0;
  59.          for (x=c+1;x<rows;x++) {
  60.             if (m[x*cols + c] != 0) {
  61.                /* Execute the swap */
  62.                for (v=0; v<cols; v++) {
  63.                   temp = m[c*cols + v];
  64.                   m[c*cols + v] = m[x*cols + v];
  65.                   m[x*cols + v] = temp;
  66.                }
  67.                toggle = 1;
  68.                break;
  69.             }
  70.          }
  71.       }
  72.       if (toggle == 0) { continue; } /* Skip zeros column */
  73.       /* Assign values */
  74.       for (r=c+1;r<rows;r++) {
  75.          scale = m[r*cols + c]/m[c*cols + c];
  76.          for (v=c; v < cols; v++) {
  77.             m[r*cols + v] = m[r*cols + v] - scale*m[c*cols + v];
  78.          }
  79.       }
  80.       /* Check for zero rows */
  81.       for (r=c; r<rows; r++) {
  82.          toggle = 1;
  83.          for (x=0; x<cols; x++) {
  84.             if (m[r*cols + x] != 0) { toggle = 0; break; }
  85.          }
  86.          if (toggle == 1) {
  87.          /* Put zero row on bottom */
  88.             for (v=r*cols; v<(rows-1)*cols; v++) {
  89.                m[v] = m[v + cols];
  90.             }
  91.             for (v=(rows-1)*cols; v<rows*cols; v++) {
  92.                m[v] = 0;
  93.             }
  94.          }
  95.       }
  96.    }
  97.    return 0;
  98. }
  99.  
  100. int ge_bw(float *u, int rows, int cols, float *m) {
  101.    int r,c,x,v = 0;
  102.    int toggle = 1;
  103.    float scale,temp = 0;
  104.    /* Error Checking */
  105.    if ((u == NULL) || (m == NULL)) { printf("The array must be allocated for assignment"); return -1; }
  106.    /* Duplicate */
  107.    for (x=0; x<rows*cols; x++) {
  108.       m[x] = u[x];
  109.    }
  110.    /* Begin Reverse GE */
  111.    /* Check for zero rows */
  112.    for (r=c; r<rows; r++) {
  113.       toggle = 1;
  114.       for (x=0; x<cols; x++) {
  115.          if (m[r*cols + x] != 0) { toggle = 0; break; }
  116.       }
  117.       if (toggle == 1) {
  118.       /* Check Zero rows bottom */
  119.          break;
  120.       }
  121.    }
  122.    if (r >= rows) { r = rows - 1; }
  123.    for (c=r; c>=0; c--) {
  124.       for (x=c-1; x >= 0; x--) {
  125.          if (m[c*cols + c] == 0) { continue; }
  126.          scale = m[x*cols + c]/m[c*cols + c];
  127.          for (v=c; v<cols; v++) {
  128.             m[x*cols + v] = m[x*cols + v] - scale*m[c*cols + v];
  129.          }
  130.       }
  131.       if (m[c*cols + c] == 0) { continue; }
  132.       scale = 1/m[c*cols + c];
  133.       for (v=0; v<cols; v++) {
  134.          m[c*cols + v] = scale*m[c*cols + v];
  135.       }
  136.    }
  137.    /* Check for zero rows */
  138.    for (r=c; r<rows; r++) {
  139.       toggle = 1;
  140.       for (x=0; x<cols; x++) {
  141.          if (m[r*cols + x] != 0) { toggle = 0; break; }
  142.       }
  143.       if (toggle == 1) {
  144.       /* Put zero row on bottom */
  145.          for (v=r*cols; v<(rows-1)*cols; v++) {
  146.             m[v] = m[v + cols];
  147.          }
  148.          for (v=(rows-1)*cols; v<rows*cols; v++) {
  149.             m[v] = 0;
  150.          }
  151.       }
  152.    }
  153.    return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment