Tue, 24 Dec 2013 11:48:39 -0800
8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013
Summary: Copyright year updated for files modified during 2013
Reviewed-by: twisti, iveresov
ysr@777 | 1 | /* |
mikael@6198 | 2 | * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. |
ysr@777 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ysr@777 | 4 | * |
ysr@777 | 5 | * This code is free software; you can redistribute it and/or modify it |
ysr@777 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ysr@777 | 7 | * published by the Free Software Foundation. |
ysr@777 | 8 | * |
ysr@777 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ysr@777 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ysr@777 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ysr@777 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ysr@777 | 13 | * accompanied this code). |
ysr@777 | 14 | * |
ysr@777 | 15 | * You should have received a copy of the GNU General Public License version |
ysr@777 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ysr@777 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ysr@777 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
ysr@777 | 22 | * |
ysr@777 | 23 | */ |
ysr@777 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 27 | #include "utilities/debug.hpp" |
stefank@2314 | 28 | #include "utilities/globalDefinitions.hpp" |
stefank@2314 | 29 | #include "utilities/numberSeq.hpp" |
ysr@777 | 30 | |
ysr@777 | 31 | AbsSeq::AbsSeq(double alpha) : |
ysr@777 | 32 | _num(0), _sum(0.0), _sum_of_squares(0.0), |
ysr@777 | 33 | _davg(0.0), _dvariance(0.0), _alpha(alpha) { |
ysr@777 | 34 | } |
ysr@777 | 35 | |
ysr@777 | 36 | void AbsSeq::add(double val) { |
ysr@777 | 37 | if (_num == 0) { |
ysr@777 | 38 | // if the sequence is empty, the davg is the same as the value |
ysr@777 | 39 | _davg = val; |
ysr@777 | 40 | // and the variance is 0 |
ysr@777 | 41 | _dvariance = 0.0; |
ysr@777 | 42 | } else { |
ysr@777 | 43 | // otherwise, calculate both |
ysr@777 | 44 | _davg = (1.0 - _alpha) * val + _alpha * _davg; |
ysr@777 | 45 | double diff = val - _davg; |
ysr@777 | 46 | _dvariance = (1.0 - _alpha) * diff * diff + _alpha * _dvariance; |
ysr@777 | 47 | } |
ysr@777 | 48 | } |
ysr@777 | 49 | |
ysr@777 | 50 | double AbsSeq::avg() const { |
ysr@777 | 51 | if (_num == 0) |
ysr@777 | 52 | return 0.0; |
ysr@777 | 53 | else |
ysr@777 | 54 | return _sum / total(); |
ysr@777 | 55 | } |
ysr@777 | 56 | |
ysr@777 | 57 | double AbsSeq::variance() const { |
ysr@777 | 58 | if (_num <= 1) |
ysr@777 | 59 | return 0.0; |
ysr@777 | 60 | |
ysr@777 | 61 | double x_bar = avg(); |
ysr@777 | 62 | double result = _sum_of_squares / total() - x_bar * x_bar; |
ysr@777 | 63 | if (result < 0.0) { |
ysr@777 | 64 | // due to loss-of-precision errors, the variance might be negative |
ysr@777 | 65 | // by a small bit |
ysr@777 | 66 | |
ysr@777 | 67 | // guarantee(-0.1 < result && result < 0.0, |
ysr@777 | 68 | // "if variance is negative, it should be very small"); |
ysr@777 | 69 | result = 0.0; |
ysr@777 | 70 | } |
ysr@777 | 71 | return result; |
ysr@777 | 72 | } |
ysr@777 | 73 | |
ysr@777 | 74 | double AbsSeq::sd() const { |
ysr@777 | 75 | double var = variance(); |
ysr@777 | 76 | guarantee( var >= 0.0, "variance should not be negative" ); |
ysr@777 | 77 | return sqrt(var); |
ysr@777 | 78 | } |
ysr@777 | 79 | |
ysr@777 | 80 | double AbsSeq::davg() const { |
ysr@777 | 81 | return _davg; |
ysr@777 | 82 | } |
ysr@777 | 83 | |
ysr@777 | 84 | double AbsSeq::dvariance() const { |
ysr@777 | 85 | if (_num <= 1) |
ysr@777 | 86 | return 0.0; |
ysr@777 | 87 | |
ysr@777 | 88 | double result = _dvariance; |
ysr@777 | 89 | if (result < 0.0) { |
ysr@777 | 90 | // due to loss-of-precision errors, the variance might be negative |
ysr@777 | 91 | // by a small bit |
ysr@777 | 92 | |
ysr@777 | 93 | guarantee(-0.1 < result && result < 0.0, |
ysr@777 | 94 | "if variance is negative, it should be very small"); |
ysr@777 | 95 | result = 0.0; |
ysr@777 | 96 | } |
ysr@777 | 97 | return result; |
ysr@777 | 98 | } |
ysr@777 | 99 | |
ysr@777 | 100 | double AbsSeq::dsd() const { |
ysr@777 | 101 | double var = dvariance(); |
ysr@777 | 102 | guarantee( var >= 0.0, "variance should not be negative" ); |
ysr@777 | 103 | return sqrt(var); |
ysr@777 | 104 | } |
ysr@777 | 105 | |
ysr@777 | 106 | NumberSeq::NumberSeq(double alpha) : |
ysr@777 | 107 | AbsSeq(alpha), _maximum(0.0), _last(0.0) { |
ysr@777 | 108 | } |
ysr@777 | 109 | |
ysr@777 | 110 | bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) { |
ysr@777 | 111 | for (int i = 0; i < n; ++i) { |
ysr@777 | 112 | if (parts[i] != NULL && total->num() != parts[i]->num()) |
ysr@777 | 113 | return false; |
ysr@777 | 114 | } |
ysr@777 | 115 | return true; |
ysr@777 | 116 | } |
ysr@777 | 117 | |
ysr@777 | 118 | void NumberSeq::add(double val) { |
ysr@777 | 119 | AbsSeq::add(val); |
ysr@777 | 120 | |
ysr@777 | 121 | _last = val; |
ysr@777 | 122 | if (_num == 0) { |
ysr@777 | 123 | _maximum = val; |
ysr@777 | 124 | } else { |
ysr@777 | 125 | if (val > _maximum) |
ysr@777 | 126 | _maximum = val; |
ysr@777 | 127 | } |
ysr@777 | 128 | _sum += val; |
ysr@777 | 129 | _sum_of_squares += val * val; |
ysr@777 | 130 | ++_num; |
ysr@777 | 131 | } |
ysr@777 | 132 | |
ysr@777 | 133 | |
ysr@777 | 134 | TruncatedSeq::TruncatedSeq(int length, double alpha): |
ysr@777 | 135 | AbsSeq(alpha), _length(length), _next(0) { |
zgu@3900 | 136 | _sequence = NEW_C_HEAP_ARRAY(double, _length, mtInternal); |
ysr@777 | 137 | for (int i = 0; i < _length; ++i) |
ysr@777 | 138 | _sequence[i] = 0.0; |
ysr@777 | 139 | } |
ysr@777 | 140 | |
brutisso@3641 | 141 | TruncatedSeq::~TruncatedSeq() { |
zgu@3900 | 142 | FREE_C_HEAP_ARRAY(double, _sequence, mtGC); |
brutisso@3641 | 143 | } |
brutisso@3641 | 144 | |
ysr@777 | 145 | void TruncatedSeq::add(double val) { |
ysr@777 | 146 | AbsSeq::add(val); |
ysr@777 | 147 | |
ysr@777 | 148 | // get the oldest value in the sequence... |
ysr@777 | 149 | double old_val = _sequence[_next]; |
ysr@777 | 150 | // ...remove it from the sum and sum of squares |
ysr@777 | 151 | _sum -= old_val; |
ysr@777 | 152 | _sum_of_squares -= old_val * old_val; |
ysr@777 | 153 | |
ysr@777 | 154 | // ...and update them with the new value |
ysr@777 | 155 | _sum += val; |
ysr@777 | 156 | _sum_of_squares += val * val; |
ysr@777 | 157 | |
ysr@777 | 158 | // now replace the old value with the new one |
ysr@777 | 159 | _sequence[_next] = val; |
ysr@777 | 160 | _next = (_next + 1) % _length; |
ysr@777 | 161 | |
ysr@777 | 162 | // only increase it if the buffer is not full |
ysr@777 | 163 | if (_num < _length) |
ysr@777 | 164 | ++_num; |
ysr@777 | 165 | |
ysr@777 | 166 | guarantee( variance() > -1.0, "variance should be >= 0" ); |
ysr@777 | 167 | } |
ysr@777 | 168 | |
ysr@777 | 169 | // can't easily keep track of this incrementally... |
ysr@777 | 170 | double TruncatedSeq::maximum() const { |
ysr@777 | 171 | if (_num == 0) |
ysr@777 | 172 | return 0.0; |
ysr@777 | 173 | double ret = _sequence[0]; |
ysr@777 | 174 | for (int i = 1; i < _num; ++i) { |
ysr@777 | 175 | double val = _sequence[i]; |
ysr@777 | 176 | if (val > ret) |
ysr@777 | 177 | ret = val; |
ysr@777 | 178 | } |
ysr@777 | 179 | return ret; |
ysr@777 | 180 | } |
ysr@777 | 181 | |
ysr@777 | 182 | double TruncatedSeq::last() const { |
ysr@777 | 183 | if (_num == 0) |
ysr@777 | 184 | return 0.0; |
ysr@777 | 185 | unsigned last_index = (_next + _length - 1) % _length; |
ysr@777 | 186 | return _sequence[last_index]; |
ysr@777 | 187 | } |
ysr@777 | 188 | |
ysr@777 | 189 | double TruncatedSeq::oldest() const { |
ysr@777 | 190 | if (_num == 0) |
ysr@777 | 191 | return 0.0; |
ysr@777 | 192 | else if (_num < _length) |
ysr@777 | 193 | // index 0 always oldest value until the array is full |
ysr@777 | 194 | return _sequence[0]; |
ysr@777 | 195 | else { |
ysr@777 | 196 | // since the array is full, _next is over the oldest value |
ysr@777 | 197 | return _sequence[_next]; |
ysr@777 | 198 | } |
ysr@777 | 199 | } |
ysr@777 | 200 | |
ysr@777 | 201 | double TruncatedSeq::predict_next() const { |
ysr@777 | 202 | if (_num == 0) |
ysr@777 | 203 | return 0.0; |
ysr@777 | 204 | |
ysr@777 | 205 | double num = (double) _num; |
ysr@777 | 206 | double x_squared_sum = 0.0; |
ysr@777 | 207 | double x_sum = 0.0; |
ysr@777 | 208 | double y_sum = 0.0; |
ysr@777 | 209 | double xy_sum = 0.0; |
ysr@777 | 210 | double x_avg = 0.0; |
ysr@777 | 211 | double y_avg = 0.0; |
ysr@777 | 212 | |
ysr@777 | 213 | int first = (_next + _length - _num) % _length; |
ysr@777 | 214 | for (int i = 0; i < _num; ++i) { |
ysr@777 | 215 | double x = (double) i; |
ysr@777 | 216 | double y = _sequence[(first + i) % _length]; |
ysr@777 | 217 | |
ysr@777 | 218 | x_squared_sum += x * x; |
ysr@777 | 219 | x_sum += x; |
ysr@777 | 220 | y_sum += y; |
ysr@777 | 221 | xy_sum += x * y; |
ysr@777 | 222 | } |
ysr@777 | 223 | x_avg = x_sum / num; |
ysr@777 | 224 | y_avg = y_sum / num; |
ysr@777 | 225 | |
ysr@777 | 226 | double Sxx = x_squared_sum - x_sum * x_sum / num; |
ysr@777 | 227 | double Sxy = xy_sum - x_sum * y_sum / num; |
ysr@777 | 228 | double b1 = Sxy / Sxx; |
ysr@777 | 229 | double b0 = y_avg - b1 * x_avg; |
ysr@777 | 230 | |
ysr@777 | 231 | return b0 + b1 * num; |
ysr@777 | 232 | } |
ysr@1521 | 233 | |
ysr@1521 | 234 | |
ysr@1521 | 235 | // Printing/Debugging Support |
ysr@1521 | 236 | |
ysr@1521 | 237 | void AbsSeq::dump() { dump_on(gclog_or_tty); } |
ysr@1521 | 238 | |
ysr@1521 | 239 | void AbsSeq::dump_on(outputStream* s) { |
ysr@1521 | 240 | s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f", |
ysr@1521 | 241 | _num, _sum, _sum_of_squares); |
ysr@1521 | 242 | s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f", |
ysr@1521 | 243 | _davg, _dvariance, _alpha); |
ysr@1521 | 244 | } |
ysr@1521 | 245 | |
ysr@1521 | 246 | void NumberSeq::dump_on(outputStream* s) { |
ysr@1521 | 247 | AbsSeq::dump_on(s); |
mikael@4668 | 248 | s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f", _last, _maximum); |
ysr@1521 | 249 | } |
ysr@1521 | 250 | |
ysr@1521 | 251 | void TruncatedSeq::dump_on(outputStream* s) { |
ysr@1521 | 252 | AbsSeq::dump_on(s); |
ysr@1521 | 253 | s->print_cr("\t\t _length = %d, _next = %d", _length, _next); |
ysr@1521 | 254 | for (int i = 0; i < _length; i++) { |
ysr@1521 | 255 | if (i%5 == 0) { |
ysr@1521 | 256 | s->cr(); |
ysr@1521 | 257 | s->print("\t"); |
ysr@1521 | 258 | } |
ysr@1521 | 259 | s->print("\t[%d]=%7.3f", i, _sequence[i]); |
ysr@1521 | 260 | } |
ysr@1521 | 261 | s->print_cr(""); |
ysr@1521 | 262 | } |