Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/opti/linopt.cpp
3206 views
1
#include <mystdlib.h>
2
#include <myadt.hpp>
3
4
#include <linalg.hpp>
5
#include "opti.hpp"
6
7
namespace netgen
8
{
9
10
void LinearOptimize (const DenseMatrix & a, const Vector & b,
11
const Vector & c, Vector & x)
12
13
{
14
int i1, i2, i3, j;
15
DenseMatrix m(3), inv(3);
16
Vector rs(3), hx(3), res(a.Height()), res2(3);
17
double f, fmin;
18
int nrest;
19
20
if (a.Width() != 3)
21
{
22
cerr << "LinearOptimize only implemented for 3 unknowns" << endl;
23
return;
24
}
25
26
fmin = 1e10;
27
x = 0;
28
nrest = a.Height();
29
for (i1 = 1; i1 <= nrest; i1++)
30
for (i2 = i1 + 1; i2 <= nrest; i2++)
31
for (i3 = i2 + 1; i3 <= nrest; i3++)
32
{
33
for (j = 1; j <= 3; j++)
34
{
35
m.Elem(1, j) = a.Get(i1, j);
36
m.Elem(2, j) = a.Get(i2, j);
37
m.Elem(3, j) = a.Get(i3, j);
38
}
39
40
rs.Elem(1) = b.Get(i1);
41
rs.Elem(2) = b.Get(i2);
42
rs.Elem(3) = b.Get(i3);
43
44
if (fabs (m.Det()) < 1e-12) continue;
45
46
CalcInverse (m, inv);
47
inv.Mult (rs, hx);
48
49
a.Residuum (hx, b, res);
50
// m.Residuum (hx, rs, res2);
51
f = c * hx;
52
53
/*
54
testout -> precision(12);
55
(*testout) << "i = (" << i1 << "," << i2 << "," << i3
56
<< "), f = " << f << " x = " << x << " res = " << res
57
<< " resmin = " << res.Min()
58
<< " res2 = " << res2 << " prod = " << prod << endl;
59
*/
60
61
62
double rmin = res.Elem(1);
63
for (int hi = 2; hi <= res.Size(); hi++)
64
if (res.Elem(hi) < rmin) rmin = res.Elem(hi);
65
66
if ( (f < fmin) && rmin >= -1e-8)
67
{
68
fmin = f;
69
x = hx;
70
}
71
}
72
}
73
}
74
75