#pragma once template static bool solve_linear_sys(T solution[], T* mat, int n, bool inplace); // Gaussian elimination, n*(n+1) martix, O(n^3) time. // m is array of ptr to each row template static bool solve_linear_sys(T solution[], T* mat[], int n, bool inplace = false) { if(!inplace) { int sz1 = sizeof(T) * (n+1); int sz = sz1 * n; T * mat1 = (T*) malloc(sz); T * p = mat1; for(int k=0; k pivot) { pivot = t; r = i; } } if(pivot == 0) { return false; } if(r != k) { T* p = mat[r]; mat[r] = mat[k]; mat[k] = p; } // eliminate pivot = mat[k][k]; for(int i=k+1; i=0; k--) { T c = 0; for(int j = k+1; j static bool solve_linear_sys(T solution[], T* mat, int n, bool inplace = false) { if(!inplace) { int sz = sizeof(T) * n * (n+1); T * mat1 = (T*) malloc(sz); memcpy(mat1, mat, sz); mat = mat1; } T ** m = (T**)malloc(sizeof(T*) * n); for(int i=0; i