public static CMatrix operator +(CMatrix a, CMatrix b) { CMatrix ret = new CMatrix(a.Rows, a.Cols); for (int row = 0; row < a.Rows; row++) { for (int col = 0; col < a.Cols; col++) { ret.Set(row, col, a.Get(row, col) + b.Get(row, col)); } } return ret; }
public static CMatrix operator *(CMatrix a, CMatrix b) { CMatrix ret = new CMatrix(a.Rows, b.Cols); for (int row = 0; row < a.Rows; row++) { for (int col = 0; col < b.Cols; col++) { float sum = 0.0f; for (int i = 0; i < a.Cols; i++) { sum += a.Get(row, i) * b.Get(i, col); } ret.Set(row, col, sum); } } return ret; }
public static float MinimumEditDistance(string a, string b) { CMatrix matrix = new CMatrix(a.Length + 1, b.Length + 1); for (int i = 0; i <= a.Length; i++) { matrix.Set(i, 0, i); } for (int i = 0; i <= b.Length; i++) { matrix.Set(0, i, i); } for (int i = 1; i <= a.Length; i++) { for (int j = 1; j <= b.Length; j++) { float rightDiagonal = float.MaxValue; if (j < b.Length)//this is needed to avoid an out of bounds exception { rightDiagonal = matrix.Get(i - 1, j + 1); } if (a[i - 1] == b[j - 1]) { if (matrix.Get(i - 1, j - 1) < rightDiagonal) { matrix.Set(i, j, matrix.Get(i - 1, j - 1)); } else { matrix.Set(i, j, matrix.Get(i - 1, j + 1)); } } else { matrix.Set(i, j, Min3(matrix.Get(i - 1, j) + 1.0f, matrix.Get(i - 1, j - 1) + 1.0f, matrix.Get(i, j - 1) + 1.0f)); } } } //Console.WriteLine(matrix); //not required right now return matrix.Get(a.Length, b.Length); }