| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 
 | /*
    SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com>
    SPDX-FileCopyrightText: 1996-2000 Bernd Johannes Wuebben <wuebben@kde.org>
    SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kcalc_stats.h"
//------------------------------------------------------------------------------
// Name: KStats
// Desc: constructor
//------------------------------------------------------------------------------
KStats::KStats() = default;
//------------------------------------------------------------------------------
// Name: ~KStats
// Desc: destructor
//------------------------------------------------------------------------------
KStats::~KStats() = default;
//------------------------------------------------------------------------------
// Name: clearAll
// Desc: empties the data set
//------------------------------------------------------------------------------
void KStats::clearAll()
{
    data_.clear();
}
//------------------------------------------------------------------------------
// Name: enterData
// Desc: adds an item to the data set
//------------------------------------------------------------------------------
void KStats::enterData(const KNumber &data)
{
    data_.push_back(data);
}
//------------------------------------------------------------------------------
// Name: clearLast
// Desc: removes the last item from the data set
//------------------------------------------------------------------------------
void KStats::clearLast()
{
    if (!data_.isEmpty()) {
        data_.pop_back();
    }
}
//------------------------------------------------------------------------------
// Name: sum
// Desc: calculates the SUM of all values in the data set
//------------------------------------------------------------------------------
KNumber KStats::sum() const
{
    KNumber result = KNumber::Zero;
    for (const KNumber &x : std::as_const(data_)) {
        result += x;
    }
    return result;
}
//------------------------------------------------------------------------------
// Name: median
// Desc: calculates the MEDIAN of all values in the data set
//------------------------------------------------------------------------------
KNumber KStats::median()
{
    KNumber result = KNumber::Zero;
    size_t index;
    unsigned int bound = count();
    if (bound == 0) {
        error_flag_ = true;
        return KNumber::Zero;
    }
    if (bound == 1)
        return data_.at(0);
    // need to copy data_-list, because sorting afterwards
    QList<KNumber> tmp_data(data_);
    std::sort(tmp_data.begin(), tmp_data.end());
    if (bound & 1) { // odd
        index = (bound - 1) / 2 + 1;
        result = tmp_data.at(index - 1);
    } else { // even
        index = bound / 2;
        result = ((tmp_data.at(index - 1)) + (tmp_data.at(index))) / KNumber(2);
    }
    return result;
}
//------------------------------------------------------------------------------
// Name: std_kernel
// Desc: calculates the STD Kernel of all values in the data set
//------------------------------------------------------------------------------
KNumber KStats::std_kernel()
{
    KNumber result = KNumber::Zero;
    const KNumber mean_value = mean();
    if (mean_value.type() != KNumber::TypeError) {
        for (const KNumber &x : std::as_const(data_)) {
            result += (x - mean_value) * (x - mean_value);
        }
    }
    return result;
}
//------------------------------------------------------------------------------
// Name: sum_of_squares
// Desc: calculates the SUM of all values in the data set (each squared)
//------------------------------------------------------------------------------
KNumber KStats::sum_of_squares() const
{
    KNumber result = KNumber::Zero;
    for (const KNumber &x : std::as_const(data_)) {
        result += (x * x);
    }
    return result;
}
//------------------------------------------------------------------------------
// Name: mean
// Desc: calculates the MEAN of all values in the data set
//------------------------------------------------------------------------------
KNumber KStats::mean()
{
    if (data_.isEmpty()) {
        error_flag_ = true;
        return KNumber::Zero;
    }
    return (sum() / KNumber(count()));
}
//------------------------------------------------------------------------------
// Name: std
// Desc: calculates the STANDARD DEVIATION of all values in the data set
//------------------------------------------------------------------------------
KNumber KStats::std()
{
    if (data_.isEmpty()) {
        error_flag_ = true;
        return KNumber::Zero;
    }
    return (std_kernel() / KNumber(count())).sqrt();
}
//------------------------------------------------------------------------------
// Name: sample_std
// Desc: calculates the SAMPLE STANDARD DEVIATION of all values in the data set
//------------------------------------------------------------------------------
KNumber KStats::sample_std()
{
    KNumber result = KNumber::Zero;
    if (count() < 2) {
        error_flag_ = true;
        return KNumber::Zero;
    }
    result = (std_kernel() / KNumber(count() - 1)).sqrt();
    return result;
}
//------------------------------------------------------------------------------
// Name: count
// Desc: returns the amount of values in the data set
//------------------------------------------------------------------------------
int KStats::count() const
{
    return data_.size();
}
//------------------------------------------------------------------------------
// Name: error
// Desc: returns the error state AND clears it
//------------------------------------------------------------------------------
bool KStats::error()
{
    bool value = error_flag_;
    error_flag_ = false;
    return value;
}
 |