public static int ED(string needle, string haystack) { CMatrix m = new CMatrix(needle.Length + 1, haystack.Length + 1); for (int i = 0; i < m.Rows; i++) { m.Set(i, 0, i); } for (int i = 0; i < m.Cols; i++) { m.Set(0, i, i); } for (int row = 1; row < m.Rows; row++) { for (int col = 1; col < m.Cols; col++) { if (needle[row - 1] == haystack[col - 1]) { m.Set(row, col, m.Get(row - 1, col - 1)); } else { int min = (int) min3(m.Get(row, col - 1) , m.Get(row - 1, col - 1) , m.Get(row - 1, col)); m.Set(row, col, min + 1); } } } Console.WriteLine(m); return (int) m.Get(m.Rows - 1, m.Cols - 1); }
public static CMatrix operator +(CMatrix a, CMatrix b) { if ((a.Rows != b.Rows) || (a.Cols != b.Cols)) { throw new MatrixException("Rows or cols not equal, so can't add these matrices."); } 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) { if (a.Cols != b.Rows) { throw new MatrixException("Rows in a not equal to cols in b can't multiply these matrices."); } 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 CMatrix operator -(CMatrix a) { 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)); } } return ret; }
static void Main(string[] args) { CMatrix am = new CMatrix(10, 10); CMatrix bm = new CMatrix(10, 10); am.Identity(); bm.Identity(); Console.WriteLine(am); Console.WriteLine(bm); CMatrix cm = am + bm; Console.WriteLine(cm); CMatrix identity = new CMatrix(4, 4); identity.Identity(); CMatrix test = new CMatrix(4, 4); test.Set(0, 1, 10); test.Set(3, 1, 8); test.Set(2, 0, -5); Console.WriteLine(test); Console.WriteLine(identity); Console.WriteLine(test * identity); Console.WriteLine(test * test); CMatrix m1 = new CMatrix(3, 2); m1.Set(0, 0, 1); m1.Set(0, 1, 3); m1.Set(1, 0, 2); m1.Set(1, 1, 5); m1.Set(2, 0, 0); m1.Set(2, 1, 8); CMatrix m2 = new CMatrix(2, 4); m2.Set(0, 0, 1); m2.Set(0, 1, 2); m2.Set(0, 2, 3); m2.Set(0, 3, 4); m2.Set(1, 0, 2); m2.Set(1, 1, 1); m2.Set(1, 2, 3); m2.Set(1, 3, 0); CMatrix m3 = m1 * m2; Console.WriteLine(m3); string a = "ABDEFGHIJK"; string b = "XDEFGHXXK"; int ed = EditDistance.ED(a, b); Console.WriteLine(ed); CMatrix mm1 = new CMatrix(10, 10); CMatrix mm2 = new CMatrix(10, 5); try { Console.WriteLine(mm1 * mm2); Console.WriteLine(mm1 + mm2); } catch (IndexOutOfRangeException e) { } catch (MatrixException e) { Console.WriteLine("A matrix exception occured"); Console.WriteLine(e.StackTrace); } catch (Exception e) { Console.WriteLine("Some other exception occured"); Console.WriteLine(e.StackTrace); try { // Close the file } catch (Exception ee) { } } }