Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Alexander Lapshin
common
Commits
6c1c93b7
Commit
6c1c93b7
authored
Jun 08, 2020
by
Alexander Lapshin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
284fffc4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
3 deletions
+62
-3
xalgorithm.h
utils/xalgorithm.h
+62
-3
No files found.
utils/xalgorithm.h
View file @
6c1c93b7
...
@@ -2,9 +2,14 @@
...
@@ -2,9 +2,14 @@
#include <algorithm>
#include <algorithm>
#include <cmath>
#include <cmath>
#include <functional>
template
<
typename
T
>
template
<
typename
T
>
double
golden_section_search
(
const
T
&
f
,
double
a
,
double
b
,
double
tol
)
double
golden_section_search
(
const
T
&
f
,
double
a
,
double
b
,
const
double
time_accuracy
)
{
{
const
double
invphi
=
(
std
::
sqrt
(
5
.
0
)
-
1
)
/
2
.
0
;
const
double
invphi
=
(
std
::
sqrt
(
5
.
0
)
-
1
)
/
2
.
0
;
const
double
invphi2
=
(
3
-
std
::
sqrt
(
5
.
0
))
/
2
.
0
;
const
double
invphi2
=
(
3
-
std
::
sqrt
(
5
.
0
))
/
2
.
0
;
...
@@ -14,11 +19,11 @@ double golden_section_search(const T& f, double a, double b, double tol)
...
@@ -14,11 +19,11 @@ double golden_section_search(const T& f, double a, double b, double tol)
double
h
=
b
-
a
;
double
h
=
b
-
a
;
if
(
h
<=
t
ol
)
{
if
(
h
<=
t
ime_accuracy
)
{
return
(
a
+
b
)
/
2
.
0
;
return
(
a
+
b
)
/
2
.
0
;
}
}
const
int
n
=
(
int
)
ceil
(
std
::
log
(
tol
/
h
)
/
std
::
log
(
invphi
));
const
int
n
=
static_cast
<
int
>
(
ceil
(
std
::
log
(
time_accuracy
/
h
)
/
std
::
log
(
invphi
)
));
double
c
=
a
+
invphi2
*
h
;
double
c
=
a
+
invphi2
*
h
;
double
d
=
a
+
invphi
*
h
;
double
d
=
a
+
invphi
*
h
;
...
@@ -51,3 +56,57 @@ double golden_section_search(const T& f, double a, double b, double tol)
...
@@ -51,3 +56,57 @@ double golden_section_search(const T& f, double a, double b, double tol)
return
(
c
+
b
)
/
2
.
0
;
return
(
c
+
b
)
/
2
.
0
;
}
}
}
}
inline
double
golden_section_search_stdf
(
double
a
,
double
b
,
const
double
time_accuracy
,
const
std
::
function
<
double
(
const
double
&
)
>&
f
)
{
const
double
invphi
=
(
std
::
sqrt
(
5
.
0
)
-
1
)
/
2
.
0
;
const
double
invphi2
=
(
3
-
std
::
sqrt
(
5
.
0
))
/
2
.
0
;
a
=
std
::
min
(
a
,
b
);
b
=
std
::
max
(
a
,
b
);
double
h
=
b
-
a
;
if
(
h
<=
time_accuracy
)
{
return
(
a
+
b
)
/
2
.
0
;
}
const
int
n
=
static_cast
<
int
>
(
ceil
(
std
::
log
(
time_accuracy
/
h
)
/
std
::
log
(
invphi
)));
double
c
=
a
+
invphi2
*
h
;
double
d
=
a
+
invphi
*
h
;
double
yc
=
f
(
c
);
double
yd
=
f
(
d
);
for
(
int
k
=
0
;
k
<
n
-
1
;
k
++
)
{
if
(
yc
<
yd
)
{
b
=
d
;
d
=
c
;
yd
=
yc
;
h
=
invphi
*
h
;
c
=
a
+
invphi2
*
h
;
yc
=
f
(
c
);
}
else
{
a
=
c
;
c
=
d
;
yc
=
yd
;
h
=
invphi
*
h
;
d
=
a
+
invphi
*
h
;
yd
=
f
(
d
);
}
}
if
(
yc
<
yd
)
{
return
(
a
+
d
)
/
2
.
0
;
}
else
{
return
(
c
+
b
)
/
2
.
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment