alex_s7
❔ Convolution logic
have to implement the convolution logic in C#. I have attached my code. I created a unit test but it does not pass.
public class SMatrix{
private int[,] matrix;
private int rows;
private int columns;
public SMatrix(int[,] m) {
matrix = m;
rows = m.GetLength(0);
columns = m.GetLength(1);
}
public SMatrix(int[] arr, int r, int c) {
matrix = new int[r, c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
matrix[i, j] = arr[i * c + j];
}
}
rows = r;
columns = c;
}
public int Rows {
get { return rows; }
}
public int Columns {
get { return columns; }
}
public int this[int i, int j] {
get { return matrix[i, j]; }
set { matrix[i, j] = value; }
}
public int[,] GetMatrix() {
return matrix;
}
public static SMatrix operator (SMatrix a, SMatrix b) {
int[,] result = new int[a.Rows - b.Rows + 1, a.Columns - b.Columns + 1];
for (int i = 0; i < result.GetLength(0); i++) {
for (int j = 0; j < result.GetLength(1); j++) {
for (int k = i; k < i + b.Rows; k++) {
for (int l = j; l < j + b.Columns; l++) {
result[i, j] += a[k, l] b[k - i, l - j];
}
}
}
}
return new SMatrix(result);
}
}
10 replies