34 std::cout <<
"unique" << std::endl;
37 std::cout <<
"ambiguous" << std::endl;
40 std::cout <<
"no solution" << std::endl;
43 std::cout <<
"solution exists" << std::endl;
50 return (A[0][0]*A[1][1]*A[2][2] +
51 A[0][1]*A[1][2]*A[2][0] +
52 A[0][2]*A[1][0]*A[2][1] -
53 A[0][0]*A[1][2]*A[2][1] -
54 A[0][1]*A[1][0]*A[2][2] -
55 A[0][2]*A[1][1]*A[2][0]);
61 return (a[0]*b[1]*
c[2] +
70template <
int S,
int T>
71inline void copy_mat(
double A[S][T],
double B[S][T]) {
72 for (
int i = 0; i < S; ++i) {
73 for (
int j = 0; j < T; ++j) {
79template <
int S,
int T>
81 std::cout.setf(std::ios::left, std::ios::internal);
82 for (
int i = 0; i < S; ++i) {
83 for (
int j = 0; j < T; ++j) {
84 printf (
"%8.2f ", A[i][j]);
86 std::cout << std::endl;;
91template <
int S,
int U,
int T>
92inline void multiply(
double A[S][U],
double B[U][T],
double res[S][T]) {
93 for (
int i = 0; i < S; ++i) {
94 for (
int j = 0; j < T; ++j) {
96 for (
int k = 0; k < U; ++k) {
97 sum += A[i][k] * B[k][j];
108template <
int S,
int T>
109inline void multiply(
double A[S][T],
double v[T],
double res[S]) {
110 for (
int i = 0; i < S; ++i) {
112 for (
int k = 0; k < T; ++k) {
113 sum += A[i][k] * v[k];
128template <
int S,
int T>
129static int find_pivot(
const double A[S][T],
unsigned int i, std::vector<int>
const &avoid_cols) {
135 for (
int j = 0; j < T; ++j) {
136 if (std::find(avoid_cols.begin(), avoid_cols.end(), j) != avoid_cols.end()) {
139 if (fabs(A[i][j]) > max) {
151template <
int S,
int T>
153 double piv = A[row][col];
155 for (
int k = 0; k < S; ++k) {
156 if (k == row)
continue;
157 for (
int l = 0; l < T; ++l) {
158 if (l == col)
continue;
159 A[k][l] -= A[k][col] * A[row][l] / piv;
163 for (
int k = 0; k < S; ++k) {
164 if (k == row)
continue;
168 for (
int l = 0; l < T; ++l) {
169 if (l == col)
continue;
179template <
int S,
int T>
180static std::vector<int>
gauss_jordan (
double A[S][T],
int avoid_col = -1) {
181 std::vector<int> cols_used;
182 if (avoid_col != -1) {
183 cols_used.push_back (avoid_col);
185 for (
int i = 0; i < S; ++i) {
187 int col = find_pivot<S,T>(A, i, cols_used);
188 cols_used.push_back(col);
195 gauss_jordan_step<S,T> (A, i, col);
197 if (avoid_col != -1) {
199 cols_used.erase(cols_used.begin());
205template <
int S,
int T>
206static double projectify (std::vector<int>
const &cols,
const double B[S][T],
const double x[T],
207 const int index,
const double val) {
208 double val_proj = 0.0;
211 for (
int i = 0; i < S; ++i) {
212 if (cols[i] == T-1) {
218 std::cerr <<
"Something is wrong. Rethink!!" << std::endl;
223 for (
int j = 0; j < T; ++j) {
224 if (j ==
index)
continue;
225 sp += B[
c][j] * x[j];
227 double mu = 1 - val * B[
c][
index];
228 if (fabs(mu) < 1E-6) {
229 std::cerr <<
"No solution since adapted value is too close to zero" << std::endl;
232 val_proj = sp*val/mu;
255 int index = -1,
double val = 0.0,
bool proj =
false) {
258 SysEq::copy_mat<S,T>(A,B);
259 std::vector<int> cols = gauss_jordan<S,T>(B,
index);
260 if (std::find(cols.begin(), cols.end(), -1) != cols.end()) {
268 std::vector<int>::iterator k;
269 for (
int j = 0; j < S; ++j) {
272 for (
int j = 0; j < T; ++j) {
273 k = std::find(cols.begin(), cols.end(), j);
274 if (k == cols.end()) {
280 double val_new = (
proj ? projectify<S,T>(cols, B, x,
index, val) : val);
292 SysEq::multiply<S,T>(B,x,
w);
293 for (
int j = 0; j < S; ++j) {
297 if (S + (
index == -1 ? 0 : 1) == T) {
static auto proj(Geom::Point const &p, int dim)
static double projectify(std::vector< int > const &cols, const double B[S][T], const double x[T], const int index, const double val)
double determinant3x3(double A[3][3])
static void gauss_jordan_step(double A[S][T], int row, int col)
static std::vector< int > gauss_jordan(double A[S][T], int avoid_col=-1)
void copy_mat(double A[S][T], double B[S][T])
SolutionKind gaussjord_solve(double A[S][T], double x[T], double v[S], int index=-1, double val=0.0, bool proj=false)
Solve the linear system of equations A * x = v where we additionally stipulate x[index] = val if inde...
void multiply(double A[S][U], double B[U][T], double res[S][T])
static int find_pivot(const double A[S][T], unsigned int i, std::vector< int > const &avoid_cols)
void print_mat(const double A[S][T])
void explain(SolutionKind sol)
double determinant3v(const double a[3], const double b[3], const double c[3])
double sum(const double alpha[16], const double &x, const double &y)