Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs-feliks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Feliks Kiszkurno
ogs-feliks
Commits
a9094651
Commit
a9094651
authored
7 months ago
by
Dmitri Naumov
Browse files
Options
Downloads
Patches
Plain Diff
[T/MaL] Add KahanSum test. Extend KahanSum I/F
parent
1b8ded47
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
MathLib/KahanSum.h
+38
-0
38 additions, 0 deletions
MathLib/KahanSum.h
Tests/MathLib/KahanSum.cpp
+71
-0
71 additions, 0 deletions
Tests/MathLib/KahanSum.cpp
with
109 additions
and
0 deletions
MathLib/KahanSum.h
+
38
−
0
View file @
a9094651
...
...
@@ -9,8 +9,21 @@
#pragma once
#include
<spdlog/fmt/bundled/ostream.h>
#include
<iomanip>
#include
<ostream>
#include
<range/v3/range/concepts.hpp>
#include
<range/v3/range/traits.hpp>
namespace
MathLib
{
class
KahanSum
;
}
template
<
>
struct
fmt
::
formatter
<
MathLib
::
KahanSum
>
:
fmt
::
ostream_formatter
{
};
namespace
MathLib
{
...
...
@@ -20,6 +33,31 @@ class KahanSum
public:
explicit
constexpr
KahanSum
(
double
const
value
=
0
)
:
value_
(
value
)
{}
explicit
constexpr
KahanSum
(
ranges
::
range
auto
const
&
range
)
:
value_
(
0
)
{
for
(
auto
const
v
:
range
)
{
*
this
+=
v
;
}
}
constexpr
KahanSum
operator
+
(
double
const
increment
)
const
{
KahanSum
result
=
*
this
;
return
result
+=
increment
;
}
constexpr
KahanSum
operator
-
(
double
const
increment
)
const
{
KahanSum
result
=
*
this
;
return
result
+=
-
increment
;
}
constexpr
KahanSum
&
operator
-=
(
double
const
increment
)
{
return
*
this
+=
-
increment
;
}
constexpr
KahanSum
&
operator
+=
(
double
const
increment
)
{
double
const
y
=
increment
-
correction_
;
...
...
This diff is collapsed.
Click to expand it.
Tests/MathLib/KahanSum.cpp
0 → 100644
+
71
−
0
View file @
a9094651
/**
* \file
* \copyright
* Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#include
"MathLib/KahanSum.h"
#include
<gtest/gtest.h>
#include
<range/v3/view/reverse.hpp>
#include
"Tests/AutoCheckTools.h"
using
namespace
MathLib
;
namespace
ac
=
autocheck
;
struct
NonNegativeDoubleListGenerator
{
using
result_type
=
std
::
vector
<
double
>
;
result_type
operator
()(
std
::
size_t
const
size
)
const
{
result_type
r
;
r
.
reserve
(
size
);
auto
positive_double_gen
=
ac
::
map
(
&
ac
::
absoluteValue
,
ac
::
generator
<
double
>
{});
std
::
generate_n
(
std
::
back_inserter
(
r
),
size
,
ac
::
fix
(
size
,
positive_double_gen
));
return
r
;
}
};
struct
MathLibKahanSumPropertyTest
:
public
::
testing
::
Test
{
ac
::
gtest_reporter
gtest_reporter
;
};
TEST_F
(
MathLibKahanSumPropertyTest
,
SortedSequences
)
{
auto
property
=
[](
const
std
::
vector
<
double
>&
sequence
)
{
// Sort to get an optimal behaviour for sum computation.
std
::
vector
<
double
>
sorted
{
sequence
};
std
::
sort
(
sorted
.
begin
(),
sorted
.
end
(),
[](
double
const
a
,
double
const
b
)
{
return
std
::
abs
(
a
)
<
std
::
abs
(
b
);
});
KahanSum
const
a
{
sorted
};
// Reverse sort yields worst sum behaviour resulting in maximum error.
KahanSum
const
b
{
sorted
|
ranges
::
views
::
reverse
};
if
(
a
()
==
b
())
{
return
true
;
}
// Testing the relative error to be within the theoretical limits. Keep
// in mind, that the sequence is constructed with non-negative numbers
// and the condition number is 1.
return
std
::
abs
(
a
()
-
b
())
/
((
a
()
+
b
())
/
2.
)
<
2.
*
std
::
numeric_limits
<
double
>::
epsilon
();
};
// The non-negative double list generator is used to keep the condition
// number, which is defined as sum|a_i|/|sum a_i|, equal to 1.
autocheck
::
check
<
std
::
vector
<
double
>>
(
property
,
1000
,
ac
::
make_arbitrary
(
NonNegativeDoubleListGenerator
{}),
gtest_reporter
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment