public Matrice DefineMatriceP(List<Vise> ListVise) { P = new Matrice(ListVise.Count * 2, ListVise.Count * 2); for (int i = 0; i < ListVise.Count * 2; i++) { for (int j = 0; j < ListVise.Count * 2; j++) { if (i == j) { if (i <= ListVise.Count) { P[i, j] = 1 / calc_preci_priori(B[i, 1]); } else { P[i, j] = 1 / calc_preci_priori_angle(B[i, 1]); } } else P[i, j] = 0; } } return (P); }
// Function returns the copy of this matrix public Matrice Duplicate() { Matrice matrix = new Matrice(rows, cols); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i, j] = mat[i, j]; return matrix; }
private static void SafeACopytoC(Matrice A, int xa, int ya, Matrice C, int size) { for (int i = 0; i < size; i++) // rows for (int j = 0; j < size; j++) // cols { C[i, j] = 0; if (xa + j < A.cols && ya + i < A.rows) C[i, j] += A[ya + i, xa + j]; } }
// Smart matrix multiplication private static Matrice StrassenMultiply(Matrice A, Matrice B) { if (A.cols != B.rows) throw new MException("Wrong dimension of matrix!"); Matrice R; int msize = Math.Max(Math.Max(A.rows, A.cols), Math.Max(B.rows, B.cols)); if (msize < 32) { R = ZeroMatrix(A.rows, B.cols); for (int i = 0; i < R.rows; i++) for (int j = 0; j < R.cols; j++) for (int k = 0; k < A.cols; k++) R[i, j] += A[i, k] * B[k, j]; return R; } int size = 1; int n = 0; while (msize > size) { size *= 2; n++; }; int h = size / 2; Matrice[,] mField = new Matrice[n, 9]; /* * 8x8, 8x8, 8x8, ... * 4x4, 4x4, 4x4, ... * 2x2, 2x2, 2x2, ... * . . . */ int z; for (int i = 0; i < n - 4; i++) // rows { z = (int)Math.Pow(2, n - i - 1); for (int j = 0; j < 9; j++) mField[i, j] = new Matrice(z, z); } SafeAplusBintoC(A, 0, 0, A, h, h, mField[0, 0], h); SafeAplusBintoC(B, 0, 0, B, h, h, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 1], 1, mField); // (A11 + A22) * (B11 + B22); SafeAplusBintoC(A, 0, h, A, h, h, mField[0, 0], h); SafeACopytoC(B, 0, 0, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 2], 1, mField); // (A21 + A22) * B11; SafeACopytoC(A, 0, 0, mField[0, 0], h); SafeAminusBintoC(B, h, 0, B, h, h, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 3], 1, mField); //A11 * (B12 - B22); SafeACopytoC(A, h, h, mField[0, 0], h); SafeAminusBintoC(B, 0, h, B, 0, 0, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 4], 1, mField); //A22 * (B21 - B11); SafeAplusBintoC(A, 0, 0, A, h, 0, mField[0, 0], h); SafeACopytoC(B, h, h, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 5], 1, mField); //(A11 + A12) * B22; SafeAminusBintoC(A, 0, h, A, 0, 0, mField[0, 0], h); SafeAplusBintoC(B, 0, 0, B, h, 0, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 6], 1, mField); //(A21 - A11) * (B11 + B12); SafeAminusBintoC(A, h, 0, A, h, h, mField[0, 0], h); SafeAplusBintoC(B, 0, h, B, h, h, mField[0, 1], h); StrassenMultiplyRun(mField[0, 0], mField[0, 1], mField[0, 1 + 7], 1, mField); // (A12 - A22) * (B21 + B22); R = new Matrice(A.rows, B.cols); // result /// C11 for (int i = 0; i < Math.Min(h, R.rows); i++) // rows for (int j = 0; j < Math.Min(h, R.cols); j++) // cols R[i, j] = mField[0, 1 + 1][i, j] + mField[0, 1 + 4][i, j] - mField[0, 1 + 5][i, j] + mField[0, 1 + 7][i, j]; /// C12 for (int i = 0; i < Math.Min(h, R.rows); i++) // rows for (int j = h; j < Math.Min(2 * h, R.cols); j++) // cols R[i, j] = mField[0, 1 + 3][i, j - h] + mField[0, 1 + 5][i, j - h]; /// C21 for (int i = h; i < Math.Min(2 * h, R.rows); i++) // rows for (int j = 0; j < Math.Min(h, R.cols); j++) // cols R[i, j] = mField[0, 1 + 2][i - h, j] + mField[0, 1 + 4][i - h, j]; /// C22 for (int i = h; i < Math.Min(2 * h, R.rows); i++) // rows for (int j = h; j < Math.Min(2 * h, R.cols); j++) // cols R[i, j] = mField[0, 1 + 1][i - h, j - h] - mField[0, 1 + 2][i - h, j - h] + mField[0, 1 + 3][i - h, j - h] + mField[0, 1 + 6][i - h, j - h]; return R; }
// Function solves Ax = v in confirmity with solution vector "v" public Matrice SolveWith(Matrice v) { if (rows != cols) throw new MException("The matrix is not square!"); if (rows != v.rows) throw new MException("Wrong number of results in solution vector!"); if (L == null) MakeLU(); Matrice b = new Matrice(rows, 1); for (int i = 0; i < rows; i++) b[i, 0] = v[pi[i], 0]; // switch two items in "v" due to permutation matrix Matrice z = SubsForth(L, b); Matrice x = SubsBack(U, z); return x; }
private static void AplusBintoC(Matrice A, int xa, int ya, Matrice B, int xb, int yb, Matrice C, int size) { for (int i = 0; i < size; i++) // rows for (int j = 0; j < size; j++) C[i, j] = A[ya + i, xa + j] + B[yb + i, xb + j]; }
// Power matrix to exponent public static Matrice Power(Matrice m, int pow) { if (pow == 0) return IdentityMatrix(m.rows, m.cols); if (pow == 1) return m.Duplicate(); if (pow == -1) return m.Invert(); Matrice x; if (pow < 0) { x = m.Invert(); pow *= -1; } else x = m.Duplicate(); Matrice ret = IdentityMatrix(m.rows, m.cols); while (pow != 0) { if ((pow & 1) == 1) ret *= x; x *= x; pow >>= 1; } return ret; }
// Function for LU decomposition public void MakeLU() { if (!IsSquare()) throw new MException("The matrix is not square!"); L = IdentityMatrix(rows, cols); U = Duplicate(); pi = new int[rows]; for (int i = 0; i < rows; i++) pi[i] = i; double p = 0; double pom2; int k0 = 0; int pom1 = 0; for (int k = 0; k < cols - 1; k++) { p = 0; for (int i = k; i < rows; i++) // find the row with the biggest pivot { if (Math.Abs(U[i, k]) > p) { p = Math.Abs(U[i, k]); k0 = i; } } if (p == 0) // throw new MException("The matrix is singular!"); pom1 = pi[k]; pi[k] = pi[k0]; pi[k0] = pom1; // switch two rows in permutation matrix for (int i = 0; i < k; i++) { pom2 = L[k, i]; L[k, i] = L[k0, i]; L[k0, i] = pom2; } if (k != k0) detOfP *= -1; for (int i = 0; i < cols; i++) // Switch rows in U { pom2 = U[k, i]; U[k, i] = U[k0, i]; U[k0, i] = pom2; } for (int i = k + 1; i < rows; i++) { L[i, k] = U[i, k] / U[k, k]; for (int j = k; j < cols; j++) U[i, j] = U[i, j] - L[i, k] * U[k, j]; } } }
public static Matrice Add(Matrice m1, Matrice m2) { if (m1.rows != m2.rows || m1.cols != m2.cols) throw new MException("Matrices must have the same dimensions!"); Matrice r = new Matrice(m1.rows, m1.cols); for (int i = 0; i < r.rows; i++) for (int j = 0; j < r.cols; j++) r[i, j] = m1[i, j] + m2[i, j]; return r; }
// Function parses the matrix from string public static Matrice Parse(string ps) { string s = NormalizeMatrixString(ps); string[] rows = Regex.Split(s, "\r\n"); string[] nums = rows[0].Split(' '); Matrice matrix = new Matrice(rows.Length, nums.Length); try { for (int i = 0; i < rows.Length; i++) { nums = rows[i].Split(' '); for (int j = 0; j < nums.Length; j++) matrix[i, j] = double.Parse(nums[j]); } } catch (FormatException ) { throw new MException("Wrong input format!"); } return matrix; }
public Matrice DefineMatriceA(List<gps> Observation, List<Vise> ListVise) { A = new Matrice(ListVise.Count * 2, Observation.Count); for (int i = 0; i < ListVise.Count * 2; i++) { for (int j = 0; j < Observation.Count; j+=2) { B[i, j] = (ListVise[i].st1.Position.Y - Observation[j].Position.Y) / Return_Distance(ListVise[i].st1.Position.X, Observation[j].Position.X, ListVise[i].st1.Position.Y, Observation[j].Position.Y); B[i, j + 1] = (ListVise[i].st1.Position.X - Observation[j].Position.X) / Return_Distance(ListVise[i].st1.Position.X, Observation[j].Position.X, ListVise[i].st1.Position.Y, Observation[j].Position.Y); // B[i, j] = (ListVise[i].st2.Position.Y - Observation[j].Position.Y) / Return_Distance(ListVise[i].st2.Position.X, Observation[j].Position.X, ListVise[i].st2.Position.Y, Observation[j].Position.Y); // B[i, j + 1] = (ListVise[i].st2.Position.X - Observation[j].Position.X) / Return_Distance(ListVise[i].st2.Position.X, Observation[j].Position.X, ListVise[i].st2.Position.Y, Observation[j].Position.Y); } } return A; }
// Region Func calc public Matrice testfunct() { // X = new Matrice(PointMesuré.Count + gpslist.Count, PointMesuré.Count); //-----> Define Matrice A (envoyé les elem) A = DefineMatriceA(PointMesuré, AllVise); // -----> Define matrice colonne B; B = DefineMatriceB(AllVise); // -----> Define matrice Pondération P; P = DefineMatriceP(AllVise); // logique de calcul --> X = Matrice.StupidMultiply(Matrice.StupidMultiply(Matrice.Transpose(A), P), A); N = X; suite_des_calcules(N); X = X.Invert();// Multiplication X = Matrice.StupidMultiply(X, Matrice.Transpose(A)); X = Matrice.StupidMultiply(X, P); X = Matrice.StupidMultiply(X, B); X1 = X; Tmp = Matrice.Add(X, -X1); return Tmp; }
public void suite_des_calcules(Matrice N) { int DegreLiberte; double chideuxval; double a; double b; N = N.Invert(); DegreLiberte = (AllVise.Count() * 2) - (PointMesuré.Count() * 2); chideuxval = chideux[DegreLiberte]; a = Math.Sqrt(chideuxval) * chideuxval; b = Math.Sqrt(chideuxval) * chideuxval; // matrice diagonal + matrice triangulaire sup + matrice tirangulaire inf // On peut avoir le determinant ? // a et b }
public Matrice GetCol(int k) { Matrice m = new Matrice(rows, 1); for (int i = 0; i < rows; i++) m[i, 0] = mat[i, k]; return m; }
// Function generates the random matrix public static Matrice RandomMatrix(int iRows, int iCols, int dispersion) { Random random = new Random(); Matrice matrix = new Matrice(iRows, iCols); for (int i = 0; i < iRows; i++) for (int j = 0; j < iCols; j++) matrix[i, j] = random.Next(-dispersion, dispersion); return matrix; }
// Function returns the inverted matrix public Matrice Invert() { if (L == null) MakeLU(); Matrice inv = new Matrice(rows, cols); for (int i = 0; i < rows; i++) { Matrice Ei = Matrice.ZeroMatrix(rows, 1); Ei[i, 0] = 1; Matrice col = SolveWith(Ei); inv.SetCol(col, i); } return inv; }
// Stupid matrix multiplication public static Matrice StupidMultiply(Matrice m1, Matrice m2) { if (m1.cols != m2.rows) throw new MException("Wrong dimensions of matrix!"); Matrice result = ZeroMatrix(m1.rows, m2.cols); for (int i = 0; i < result.rows; i++) for (int j = 0; j < result.cols; j++) for (int k = 0; k < m1.cols; k++) result[i, j] += m1[i, k] * m2[k, j]; return result; }
public void SetCol(Matrice v, int k) { for (int i = 0; i < rows; i++) mat[i, k] = v[i, 0]; }
// Function solves Ax = b for A as an upper triangular matrix public static Matrice SubsBack(Matrice A, Matrice b) { if (A.L == null) A.MakeLU(); int n = A.rows; Matrice x = new Matrice(n, 1); for (int i = n - 1; i > -1; i--) { x[i, 0] = b[i, 0]; for (int j = n - 1; j > i; j--) x[i, 0] -= A[i, j] * x[j, 0]; x[i, 0] = x[i, 0] / A[i, i]; } return x; }
private static void ACopytoC(Matrice A, int xa, int ya, Matrice C, int size) { for (int i = 0; i < size; i++) // rows for (int j = 0; j < size; j++) C[i, j] = A[ya + i, xa + j]; }
// Function solves Ax = b for A as a lower triangular matrix public static Matrice SubsForth(Matrice A, Matrice b) { if (A.L == null) A.MakeLU(); int n = A.rows; Matrice x = new Matrice(n, 1); for (int i = 0; i < n; i++) { x[i, 0] = b[i, 0]; for (int j = 0; j < i; j++) x[i, 0] -= A[i, j] * x[j, 0]; x[i, 0] = x[i, 0] / A[i, i]; } return x; }
// Multiplication by constant n private static Matrice Multiply(double n, Matrice m) { Matrice r = new Matrice(m.rows, m.cols); for (int i = 0; i < m.rows; i++) for (int j = 0; j < m.cols; j++) r[i, j] = m[i, j] * n; return r; }
// Matrix transpose, for any rectangular matrix public static Matrice Transpose(Matrice m) { Matrice t = new Matrice(m.cols, m.rows); for (int i = 0; i < m.rows; i++) for (int j = 0; j < m.cols; j++) t[j, i] = m[i, j]; return t; }
private static void SafeAplusBintoC(Matrice A, int xa, int ya, Matrice B, int xb, int yb, Matrice C, int size) { for (int i = 0; i < size; i++) // rows for (int j = 0; j < size; j++) // cols { C[i, j] = 0; if (xa + j < A.cols && ya + i < A.rows) C[i, j] += A[ya + i, xa + j]; if (xb + j < B.cols && yb + i < B.rows) C[i, j] += B[yb + i, xb + j]; } }
// Function generates the zero matrix public static Matrice ZeroMatrix(int iRows, int iCols) { Matrice matrix = new Matrice(iRows, iCols); for (int i = 0; i < iRows; i++) for (int j = 0; j < iCols; j++) matrix[i, j] = 0; return matrix; }
// A * B into C, level of recursion, matrix field // function for square matrix 2^N x 2^N private static void StrassenMultiplyRun(Matrice A, Matrice B, Matrice C, int l, Matrice[,] f) { int size = A.rows; int h = size / 2; if (size < 32) { for (int i = 0; i < C.rows; i++) for (int j = 0; j < C.cols; j++) { C[i, j] = 0; for (int k = 0; k < A.cols; k++) C[i, j] += A[i, k] * B[k, j]; } return; } AplusBintoC(A, 0, 0, A, h, h, f[l, 0], h); AplusBintoC(B, 0, 0, B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 1], l + 1, f); // (A11 + A22) * (B11 + B22); AplusBintoC(A, 0, h, A, h, h, f[l, 0], h); ACopytoC(B, 0, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 2], l + 1, f); // (A21 + A22) * B11; ACopytoC(A, 0, 0, f[l, 0], h); AminusBintoC(B, h, 0, B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 3], l + 1, f); //A11 * (B12 - B22); ACopytoC(A, h, h, f[l, 0], h); AminusBintoC(B, 0, h, B, 0, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 4], l + 1, f); //A22 * (B21 - B11); AplusBintoC(A, 0, 0, A, h, 0, f[l, 0], h); ACopytoC(B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 5], l + 1, f); //(A11 + A12) * B22; AminusBintoC(A, 0, h, A, 0, 0, f[l, 0], h); AplusBintoC(B, 0, 0, B, h, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 6], l + 1, f); //(A21 - A11) * (B11 + B12); AminusBintoC(A, h, 0, A, h, h, f[l, 0], h); AplusBintoC(B, 0, h, B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 7], l + 1, f); // (A12 - A22) * (B21 + B22); /// C11 for (int i = 0; i < h; i++) // rows for (int j = 0; j < h; j++) // cols C[i, j] = f[l, 1 + 1][i, j] + f[l, 1 + 4][i, j] - f[l, 1 + 5][i, j] + f[l, 1 + 7][i, j]; /// C12 for (int i = 0; i < h; i++) // rows for (int j = h; j < size; j++) // cols C[i, j] = f[l, 1 + 3][i, j - h] + f[l, 1 + 5][i, j - h]; /// C21 for (int i = h; i < size; i++) // rows for (int j = 0; j < h; j++) // cols C[i, j] = f[l, 1 + 2][i - h, j] + f[l, 1 + 4][i - h, j]; /// C22 for (int i = h; i < size; i++) // rows for (int j = h; j < size; j++) // cols C[i, j] = f[l, 1 + 1][i - h, j - h] - f[l, 1 + 2][i - h, j - h] + f[l, 1 + 3][i - h, j - h] + f[l, 1 + 6][i - h, j - h]; }
public Matrice DefineMatriceB(List<Vise> ListVise) { B = new Matrice(ListVise.Count * 2, 1); for (int i = 0; i < ListVise.Count * 2; i+=2) { if (i <= ListVise.Count) { B[i, 1] = Return_Distance(ListVise[i].st1.Position.X, ListVise[i].st2.Position.X, ListVise[i].st1.Position.Y, ListVise[i].st2.Position.Y) - Return_Distance(ListVise[i].st1.Position.X, ListVise[i].st2.Position.X, ListVise[i].st1.Position.Y, ListVise[i].st2.Position.Y); B[i + 1, 1] = Return_Distance(ListVise[i].st2.Position.X, ListVise[i].st1.Position.X, ListVise[i].st2.Position.Y, ListVise[i].st1.Position.Y) - Return_Distance(ListVise[i].st2.Position.X, ListVise[i].st1.Position.X, ListVise[i].st2.Position.Y, ListVise[i].st1.Position.Y); } else { B[i, 1] = Return_Angles(ListVise[i].st1.Position.X, ListVise[i].st2.Position.X, ListVise[i].st1.Position.Y, ListVise[i].st2.Position.Y, 0) - Return_Angles(ListVise[i].st1.Position.X, ListVise[i].st2.Position.X, ListVise[i].st1.Position.Y, ListVise[i].st2.Position.Y, 0); B[i + 1, 1] = Return_Angles(ListVise[i].st2.Position.X, ListVise[i].st1.Position.X, ListVise[i].st2.Position.Y, ListVise[i].st1.Position.Y, 0) - Return_Angles(ListVise[i].st2.Position.X, ListVise[i].st1.Position.X, ListVise[i].st2.Position.Y, ListVise[i].st1.Position.Y, 0 ); } } return B; }