Path: blob/devel/ElmerGUI/netgen/libsrc/opti/linopt.cpp
3206 views
#include <mystdlib.h>1#include <myadt.hpp>23#include <linalg.hpp>4#include "opti.hpp"56namespace netgen7{89void LinearOptimize (const DenseMatrix & a, const Vector & b,10const Vector & c, Vector & x)1112{13int i1, i2, i3, j;14DenseMatrix m(3), inv(3);15Vector rs(3), hx(3), res(a.Height()), res2(3);16double f, fmin;17int nrest;1819if (a.Width() != 3)20{21cerr << "LinearOptimize only implemented for 3 unknowns" << endl;22return;23}2425fmin = 1e10;26x = 0;27nrest = a.Height();28for (i1 = 1; i1 <= nrest; i1++)29for (i2 = i1 + 1; i2 <= nrest; i2++)30for (i3 = i2 + 1; i3 <= nrest; i3++)31{32for (j = 1; j <= 3; j++)33{34m.Elem(1, j) = a.Get(i1, j);35m.Elem(2, j) = a.Get(i2, j);36m.Elem(3, j) = a.Get(i3, j);37}3839rs.Elem(1) = b.Get(i1);40rs.Elem(2) = b.Get(i2);41rs.Elem(3) = b.Get(i3);4243if (fabs (m.Det()) < 1e-12) continue;4445CalcInverse (m, inv);46inv.Mult (rs, hx);4748a.Residuum (hx, b, res);49// m.Residuum (hx, rs, res2);50f = c * hx;5152/*53testout -> precision(12);54(*testout) << "i = (" << i1 << "," << i2 << "," << i355<< "), f = " << f << " x = " << x << " res = " << res56<< " resmin = " << res.Min()57<< " res2 = " << res2 << " prod = " << prod << endl;58*/596061double rmin = res.Elem(1);62for (int hi = 2; hi <= res.Size(); hi++)63if (res.Elem(hi) < rmin) rmin = res.Elem(hi);6465if ( (f < fmin) && rmin >= -1e-8)66{67fmin = f;68x = hx;69}70}71}72}737475