Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int ge_fw(float *, int, int, float *);
- int ge_bw(float *, int, int, float *);
- int main(void) {
- int rows,cols,x,y = 0;
- float *i, *out, *outF = NULL;
- rows = 3;
- cols = 3;
- i = (float *)malloc(sizeof(float)*rows*cols);
- out = (float *)malloc(sizeof(float)*rows*cols);
- outF = (float *)malloc(sizeof(float)*rows*cols);
- /* Set vals for initial Matrix */
- i[0] = 1;
- i[1] = 1;
- i[2] = 0;
- i[3] = 1;
- i[4] = 1;
- i[5] = 4;
- i[6] = 1;
- i[7] = 1;
- i[8] = 8;
- ge_fw(i, rows, cols, out);
- ge_bw(out, rows, cols, outF);
- for (x=0; x < rows; x++) {
- for (y=0; y < cols; y++) {
- printf("%f ", i[x*cols + y]);
- }
- printf("\n");
- }
- printf("\n");
- for (x=0; x < rows; x++) {
- for (y=0; y < cols; y++) {
- printf("%f ", outF[x*cols + y]);
- }
- printf("\n");
- }
- return 0;
- }
- int ge_fw(float *u, int rows, int cols, float *m) {
- int r,c,x,v = 0;
- int toggle = 1; /* Toggle used twice to save an int (why not) */
- float scale,temp = 0;
- /* Error Check */
- if ((u == NULL) || (m == NULL)) { printf("The array must be allocated for assignment"); return -1; }
- /* Duplicate */
- for (x=0; x<rows*cols; x++) {
- m[x] = u[x];
- }
- /* Begin GE */
- for (c=0; c < rows; c++) {
- /* Swap rows if zero */
- toggle = 1;
- if (m[c*cols + c] == 0) {
- toggle = 0;
- for (x=c+1;x<rows;x++) {
- if (m[x*cols + c] != 0) {
- /* Execute the swap */
- for (v=0; v<cols; v++) {
- temp = m[c*cols + v];
- m[c*cols + v] = m[x*cols + v];
- m[x*cols + v] = temp;
- }
- toggle = 1;
- break;
- }
- }
- }
- if (toggle == 0) { continue; } /* Skip zeros column */
- /* Assign values */
- for (r=c+1;r<rows;r++) {
- scale = m[r*cols + c]/m[c*cols + c];
- for (v=c; v < cols; v++) {
- m[r*cols + v] = m[r*cols + v] - scale*m[c*cols + v];
- }
- }
- /* Check for zero rows */
- for (r=c; r<rows; r++) {
- toggle = 1;
- for (x=0; x<cols; x++) {
- if (m[r*cols + x] != 0) { toggle = 0; break; }
- }
- if (toggle == 1) {
- /* Put zero row on bottom */
- for (v=r*cols; v<(rows-1)*cols; v++) {
- m[v] = m[v + cols];
- }
- for (v=(rows-1)*cols; v<rows*cols; v++) {
- m[v] = 0;
- }
- }
- }
- }
- return 0;
- }
- int ge_bw(float *u, int rows, int cols, float *m) {
- int r,c,x,v = 0;
- int toggle = 1;
- float scale,temp = 0;
- /* Error Checking */
- if ((u == NULL) || (m == NULL)) { printf("The array must be allocated for assignment"); return -1; }
- /* Duplicate */
- for (x=0; x<rows*cols; x++) {
- m[x] = u[x];
- }
- /* Begin Reverse GE */
- /* Check for zero rows */
- for (r=c; r<rows; r++) {
- toggle = 1;
- for (x=0; x<cols; x++) {
- if (m[r*cols + x] != 0) { toggle = 0; break; }
- }
- if (toggle == 1) {
- /* Check Zero rows bottom */
- break;
- }
- }
- if (r >= rows) { r = rows - 1; }
- for (c=r; c>=0; c--) {
- for (x=c-1; x >= 0; x--) {
- if (m[c*cols + c] == 0) { continue; }
- scale = m[x*cols + c]/m[c*cols + c];
- for (v=c; v<cols; v++) {
- m[x*cols + v] = m[x*cols + v] - scale*m[c*cols + v];
- }
- }
- if (m[c*cols + c] == 0) { continue; }
- scale = 1/m[c*cols + c];
- for (v=0; v<cols; v++) {
- m[c*cols + v] = scale*m[c*cols + v];
- }
- }
- /* Check for zero rows */
- for (r=c; r<rows; r++) {
- toggle = 1;
- for (x=0; x<cols; x++) {
- if (m[r*cols + x] != 0) { toggle = 0; break; }
- }
- if (toggle == 1) {
- /* Put zero row on bottom */
- for (v=r*cols; v<(rows-1)*cols; v++) {
- m[v] = m[v + cols];
- }
- for (v=(rows-1)*cols; v<rows*cols; v++) {
- m[v] = 0;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment