static public matp[] genC(matp[] P) { matp[] C = new matp[4]; C[0] = opr.add(opr.sub(opr.add(P[0], P[3]), P[4]), P[6]); C[1] = opr.add(P[2], P[4]); C[2] = opr.add(P[1], P[3]); C[3] = opr.add(opr.sub(opr.add(P[0], P[2]), P[1]), P[5]); return C; }
static public matp[] genC(matp[] P) { matp[] C = new matp[4]; C[0] = opr.add(opr.sub(opr.add(P[0], P[3]), P[4]), P[6]); C[1] = opr.add(P[2], P[4]); C[2] = opr.add(P[1], P[3]); C[3] = opr.add(opr.sub(opr.add(P[0], P[2]), P[1]), P[5]); return(C); }
static public matp mulStrass(matp m1, matp m2,int n_min) { if (n_min > m1.n || n_min < 1) { Console.WriteLine("Wrong n-min !"); return new matp(1); } if (m1.n <= n_min) return opr.mul(m1, m2); matp[] P = genP(m1, m2, n_min); matp[] C = genC(P); matp rez = opr.remake(C); return rez; }
static public matp[] genP(matp m1, matp m2, int n_min) { matp[] m1bl = m1.section(); matp[] m2bl = m2.section(); matp[] P = new matp[7]; P[0] = mulStrass(opr.add(m1bl[0], m1bl[3]), opr.add(m2bl[0], m2bl[3]), n_min); P[1] = mulStrass(opr.add(m1bl[2], m1bl[3]), m2bl[0], n_min); P[2] = mulStrass(m1bl[0], opr.sub(m2bl[1], m2bl[3]), n_min); P[3] = mulStrass(m1bl[3], opr.sub(m2bl[2], m2bl[0]), n_min); P[4] = mulStrass(opr.add(m1bl[0], m1bl[1]), m2bl[3], n_min); P[5] = mulStrass(opr.sub(m1bl[2], m1bl[0]), opr.add(m2bl[0], m2bl[1]), n_min); P[6] = mulStrass(opr.sub(m1bl[1], m1bl[3]), opr.add(m2bl[2], m2bl[3]), n_min); return(P); }
static public matp remake(matp[] ms) { int n = ms[0].n * 2; matp mr = new matp(n); for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n / 2; j++) { mr.m[i, j] = ms[0].m[i, j]; mr.m[i, j + n / 2] = ms[1].m[i, j]; mr.m[i + n / 2, j] = ms[2].m[i, j]; mr.m[i + n / 2, j + n / 2] = ms[3].m[i, j]; } } return(mr); }
static public matp mul(matp m1, matp m2) { if (m1.n != m2.n) { Console.WriteLine("Wrong mul!"); return new matp(1); } int n = m1.n; matp rez = new matp(n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) rez.m[i, j] += (m1.m[i, k] * m2.m[k, j]); return rez; }
public matp[] section() { matp[] blocs = new matp[4]; for (int i = 0; i < 4; i++) blocs[i] = new matp(this.n / 2); for (int i = 0; i < n / 2; i++) for (int j = 0; j < n / 2; j++) { blocs[0].m[i, j] = this.m[i, j]; blocs[1].m[i, j] = this.m[i, j + n / 2]; blocs[2].m[i, j] = this.m[i + n / 2, j]; blocs[3].m[i, j] = this.m[i + n / 2, j + n / 2]; } return blocs; }
static public matp[] genP(matp m1, matp m2,int n_min) { matp[] m1bl = m1.section(); matp[] m2bl = m2.section(); matp[] P = new matp[7]; P[0] = mulStrass(opr.add(m1bl[0], m1bl[3]), opr.add(m2bl[0], m2bl[3]),n_min); P[1] = mulStrass(opr.add(m1bl[2], m1bl[3]), m2bl[0], n_min); P[2] = mulStrass(m1bl[0], opr.sub(m2bl[1], m2bl[3]), n_min); P[3] = mulStrass(m1bl[3], opr.sub(m2bl[2], m2bl[0]), n_min); P[4] = mulStrass(opr.add(m1bl[0], m1bl[1]), m2bl[3], n_min); P[5] = mulStrass(opr.sub(m1bl[2], m1bl[0]), opr.add(m2bl[0], m2bl[1]), n_min); P[6] = mulStrass(opr.sub(m1bl[1], m1bl[3]), opr.add(m2bl[2], m2bl[3]), n_min); return P; }
static public matp mulStrass(matp m1, matp m2, int n_min) { if (n_min > m1.n || n_min < 1) { Console.WriteLine("Wrong n-min !"); return(new matp(1)); } if (m1.n <= n_min) { return(opr.mul(m1, m2)); } matp[] P = genP(m1, m2, n_min); matp[] C = genC(P); matp rez = opr.remake(C); return(rez); }
static public matp sub(matp m1, matp m2) { if (m1.n != m2.n) { Console.WriteLine("Wrong sub!"); return(new matp(1)); } int n = m1.n; matp rez = new matp(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rez.m[i, j] = m1.m[i, j] - m2.m[i, j]; } } return(rez); }
static void Main(string[] args) { P1 p1 = new P1(); P2 p2 = new P2(p1.u); double[,] m1 = { { 1, 4, 2, 3 }, { 2, 4, 0, 1 }, { 2, 5, 8, 2 }, { 2, 1, 1, 11 } }; double[,] m2 = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; matp M1 = new matp(m1); M1.afis(); matp M2 = new matp(m2); M2.afis(); opr.mul(M1, M2).afis(); P3.mulStrass(M1, M2, 2).afis(); Console.WriteLine("\n\nDONE !"); Console.Read(); }
public matp[] section() { matp[] blocs = new matp[4]; for (int i = 0; i < 4; i++) { blocs[i] = new matp(this.n / 2); } for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n / 2; j++) { blocs[0].m[i, j] = this.m[i, j]; blocs[1].m[i, j] = this.m[i, j + n / 2]; blocs[2].m[i, j] = this.m[i + n / 2, j]; blocs[3].m[i, j] = this.m[i + n / 2, j + n / 2]; } } return(blocs); }
static public matp mul(matp m1, matp m2) { if (m1.n != m2.n) { Console.WriteLine("Wrong mul!"); return(new matp(1)); } int n = m1.n; matp rez = new matp(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { rez.m[i, j] += (m1.m[i, k] * m2.m[k, j]); } } } return(rez); }
static public matp add(matp m1, matp m2) { if (m1.n != m2.n) { Console.WriteLine("Wrong add!"); return new matp(1); } int n = m1.n; matp rez = new matp(n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) rez.m[i, j] = m1.m[i, j] + m2.m[i, j]; return rez; }
static public matp remake(matp[] ms) { int n = ms[0].n * 2; matp mr = new matp(n); for (int i = 0; i < n / 2; i++) for (int j = 0; j < n / 2; j++) { mr.m[i, j] = ms[0].m[i, j]; mr.m[i, j + n / 2] = ms[1].m[i, j]; mr.m[i + n / 2, j] = ms[2].m[i, j]; mr.m[i + n / 2, j + n / 2] = ms[3].m[i, j]; } return mr; }