SoPlex
Toggle main menu visibility
Loading...
Searching...
No Matches
src
soplex
stablesum.h
Go to the documentation of this file.
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2
/* */
3
/* This file is part of the class library */
4
/* SoPlex --- the Sequential object-oriented simPlex. */
5
/* */
6
/* Copyright (c) 1996-2026 Zuse Institute Berlin (ZIB) */
7
/* */
8
/* Licensed under the Apache License, Version 2.0 (the "License"); */
9
/* you may not use this file except in compliance with the License. */
10
/* You may obtain a copy of the License at */
11
/* */
12
/* http://www.apache.org/licenses/LICENSE-2.0 */
13
/* */
14
/* Unless required by applicable law or agreed to in writing, software */
15
/* distributed under the License is distributed on an "AS IS" BASIS, */
16
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17
/* See the License for the specific language governing permissions and */
18
/* limitations under the License. */
19
/* */
20
/* You should have received a copy of the Apache-2.0 license */
21
/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22
/* */
23
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25
#ifndef _SOPLEX_STABLE_SUM_H_
26
#define _SOPLEX_STABLE_SUM_H_
27
#include <type_traits>
28
29
// #define SOPLEX_CHECK_STABLESUM // double check the stable sum computation
30
31
namespace
soplex
32
{
33
34
template
<
typename
T>
35
class
StableSum
36
{
37
typename
std::remove_const<T>::type
sum
;
38
39
public
:
40
StableSum
() :
sum
(0) {}
41
StableSum
(
const
T& init) :
sum
(init) {}
42
43
void
operator+=
(
const
T& input)
44
{
45
sum
+= input;
46
}
47
48
void
operator-=
(
const
T& input)
49
{
50
sum
-= input;
51
}
52
53
operator
typename
std::remove_const<T>::type()
const
54
{
55
return
sum
;
56
}
57
58
};
59
60
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
61
#pragma float_control(precise, on, push)
62
#endif
63
64
template
<>
65
class
StableSum
<double>
66
{
67
double
sum
= 0;
68
double
c
= 0;
69
#ifdef SOPLEX_CHECK_STABLESUM
70
double
checksum = 0;
71
#endif
72
73
public
:
74
StableSum
() =
default
;
75
StableSum
(
double
init) :
sum
(init),
c
(0) {}
76
77
void
operator+=
(
double
input)
78
{
79
#ifdef SOPLEX_CHECK_STABLESUM
80
checksum += input;
81
#endif
82
double
t =
sum
+ input;
83
double
z = t -
sum
;
84
double
y = (
sum
- (t - z)) + (input - z);
85
c
+= y;
86
87
sum
= t;
88
}
89
90
void
operator-=
(
double
input)
91
{
92
(*this) += -input;
93
}
94
95
operator
double()
const
96
{
97
#ifdef SOPLEX_CHECK_STABLESUM
98
99
if
(
spxAbs
(checksum - (
sum
+
c
)) >= 1e-6)
100
printf(
"stablesum viol: %g, rel: %g, checksum: %g\n"
,
spxAbs
(checksum - (
sum
+
c
)),
101
spxAbs
(checksum - (
sum
+
c
)) /
SOPLEX_MAX
(1.0,
SOPLEX_MAX
(
spxAbs
(checksum),
spxAbs
(
sum
+
c
))),
102
checksum);
103
104
assert(
spxAbs
(checksum - (
sum
+
c
)) < 1e-6);
105
#endif
106
return
sum
+
c
;
107
}
108
};
109
110
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
111
#pragma float_control(pop)
112
#endif
113
114
/// Output operator.
115
template
<
class
T >
116
std::ostream&
operator<<
(std::ostream& s,
const
StableSum<T>
& sum)
117
{
118
s << static_cast<T>(sum);
119
120
return
s;
121
}
122
123
}
124
125
#endif
soplex::StableSum< double >::sum
double sum
Definition
stablesum.h:67
soplex::StableSum< double >::c
double c
Definition
stablesum.h:68
soplex::StableSum< double >::operator-=
void operator-=(double input)
Definition
stablesum.h:90
soplex::StableSum< double >::operator+=
void operator+=(double input)
Definition
stablesum.h:77
soplex::StableSum< double >::StableSum
StableSum(double init)
Definition
stablesum.h:75
soplex::StableSum< double >::StableSum
StableSum()=default
soplex::StableSum
Definition
stablesum.h:36
soplex::StableSum::StableSum
StableSum(const T &init)
Definition
stablesum.h:41
soplex::StableSum::operator-=
void operator-=(const T &input)
Definition
stablesum.h:48
soplex::StableSum::sum
std::remove_const< T >::type sum
Definition
stablesum.h:37
soplex::StableSum::operator+=
void operator+=(const T &input)
Definition
stablesum.h:43
soplex::StableSum::StableSum
StableSum()
Definition
stablesum.h:40
soplex
Everything should be within this namespace.
soplex::operator<<
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
Definition
basevectors.h:1110
soplex::spxAbs
R spxAbs(R a)
Definition
spxdefines.h:409
SOPLEX_MAX
#define SOPLEX_MAX(x, y)
Definition
spxdefines.h:297
Generated by
1.17.0