0

I want to realize the matrix multiplication using Strassen method. Since my input matrix will not be 2^n *2^n all the time, for example, it could be 3*3 matrix, I need to fill the edge area with 0. But I came across a problem when I create new matrix A_new[][] for A[][], B_new[][] for B[][]. I can successfully generate the A_new[][] and B_new[][] for both square(2*2, etc.) or (3*3,etc.). But when I want to further use A_new[][] and B_new[][], which I created before, the code outside the "if" statement couldn't see it.

Is there some ways that I could make the code which is outside the "if" statement see it and process it?

Here are my code.

void strassen(int A[][N], int B[][N], int C[][N]) {
  if (N == 1) {
    C[0][0] = A[0][0] * B[0][0];
  }

  if (N > 1 && N % 2 != 0) {
    int A_new[N + 1][N + 1];
    int B_new[N + 1][N + 1];
    int i, j;
    for (i = 0; i < N + 1; i++) {
      if (i == N) {
        for (j = 0; j < N + 1; j++) {
          A_new[i][j] = 0;
          B_new[i][j] = 0;
        }

      } else {
        for (j = 0; j < N + 1; j++) {
          if (j == N) {
            A_new[i][j] = 0;
            B_new[i][j] = 0;
          } else {
            A_new[i][j] = A[i][j];
            B_new[i][j] = B[i][j];

          }

        }
      }
    }

    for (i = 0; i < N + 1; i++) {
      for (j = 0; j < N + 1; j++) {
        C[i][j] = 0;
      }
    }

  } else {
    int i, j;
    int A_new[N][N];
    int B_new[N][N];
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        A_new[i][j] = A[i][j];
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        B_new[i][j] = B[i][j];
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        C[i][j] = 0;
      }
    }
  }
mehdi
  • 340
  • 4
  • 17
SHANG ZHOU
  • 57
  • 1
  • 7
  • take them out of the if scope and move it to the upper scope. if(...){if scope}. vars are local to the scope they are declared in. so if you move their declarations prior to the first if statement they will be "alive" for all of the functions scope and not only in the if scope – H.cohen Jan 21 '19 at 19:43
  • I can't because outside the if statement, I am not sure about the matrix size, and if it can be divide by 2, the size will be N, if it can't, the size would be N+1. – SHANG ZHOU Jan 21 '19 at 19:46
  • you can define pointers ,and allocate memory on the heap using malloc. – H.cohen Jan 21 '19 at 19:49
  • by the way, you can "waist" memory and make them N+1 anyways. and if the size needs to be N, just use your inner mat. – H.cohen Jan 21 '19 at 19:55
  • 1
    I suspect the 1`} if (N > 1 && N % 2 != 0) {` should be `} else if (N > 1 && N % 2 != 0) {`. – chux - Reinstate Monica Jan 21 '19 at 19:56
  • Sorry, I am new to C , so I was trying to avoid the pointer and memory allocation to realize the function, but could you give me some source or example that I could read. BTW, is that means to realize the strassen method, we must use the pointer? – SHANG ZHOU Jan 21 '19 at 19:58
  • @chux I think the same an else if missing – bruno Jan 21 '19 at 19:59
  • Your answer is more formal, but it's not the key issue here I think – SHANG ZHOU Jan 21 '19 at 20:00
  • The problem you have here is where you allocate the memory. what I suggested and suggested in the answer below is to allocate N+1 matrix either way. if you do so- you don't HAVE to use all of it if you only need an N mat. – H.cohen Jan 21 '19 at 20:06
  • If one starts by implementing a matrix structure [along the lines here](https://stackoverflow.com/questions/34856555/how-to-work-on-a-sub-matrix-in-a-matrix-by-pointer), with views to existing matrices being first-class citizens, the implementation becomes much simpler. – Nominal Animal Jan 21 '19 at 20:34

1 Answers1

1

if you move int A_new[N + 1][N + 1]; and int B_new[N + 1][N + 1]; before the if you can use A and B in the two branches of the if without problem, and they are available after the if too of course. You do not use all the cells in the else branch but that has no consequences

  int A_new[N + 1][N + 1];
  int B_new[N + 1][N + 1];

  if (N > 1 && N % 2 != 0) {
    int i, j;
    for (i = 0; i < N + 1; i++) {
      if (i == N) {
        for (j = 0; j < N + 1; j++) {
          A_new[i][j] = 0;
          B_new[i][j] = 0;
        }

      } else {
        for (j = 0; j < N + 1; j++) {
          if (j == N) {
            A_new[i][j] = 0;
            B_new[i][j] = 0;
          } else {
            A_new[i][j] = A[i][j];
            B_new[i][j] = B[i][j];
          }
        }
      }
    }

    for (i = 0; i < N + 1; i++) {
      for (j = 0; j < N + 1; j++) {
        C[i][j] = 0;
      }
    }

  } else {
    int i, j;
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        A_new[i][j] = A[i][j];
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        B_new[i][j] = B[i][j];
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        C[i][j] = 0;
      }
    }
  }

As chux says in a remark you missed an else (or you just return if N == 1 ?) and the code can be

  int A_new[N + 1][N + 1];
  int B_new[N + 1][N + 1];

  if (N == 1) {
    C[0][0] = A[0][0] * B[0][0];
  }
  else if (N > 1 && N % 2 != 0) {
    int i, j;
    for (i = 0; i < N + 1; i++) {
      if (i == N) {
        for (j = 0; j < N + 1; j++) {
          A_new[i][j] = 0;
          B_new[i][j] = 0;
        }

      } else {
        for (j = 0; j < N + 1; j++) {
          if (j == N) {
            A_new[i][j] = 0;
            B_new[i][j] = 0;
          } else {
            A_new[i][j] = A[i][j];
            B_new[i][j] = B[i][j];
          }
        }
      }
    }

    for (i = 0; i < N + 1; i++) {
      for (j = 0; j < N + 1; j++) {
        C[i][j] = 0;
      }
    }

  } else {
    int i, j;
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        A_new[i][j] = A[i][j];
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        B_new[i][j] = B[i][j];
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        C[i][j] = 0;
      }
    }
  }
bruno
  • 32,421
  • 7
  • 25
  • 37
  • I can't because outside the if statement, I am not sure about the matrix size, and if it can be divide by 2, the size will be N, if it can't, the size would be N+1 – SHANG ZHOU Jan 21 '19 at 20:01
  • @SHANGZHOU while the matrix is enough large for all the cases there are no problem, just some cells will not be used, that all, that cannot be a problem – bruno Jan 21 '19 at 20:03