src/utilities/data/Vector.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Vector.hpp" | ||
| 7 | |||
| 8 | #include "../core/Assert.hpp" | ||
| 9 | |||
| 10 | #include <random> | ||
| 11 | |||
| 12 | // this should all be moved to a utilities/core/Random.h | ||
| 13 | #include <boost/random/uniform_real.hpp> | ||
| 14 | #include <boost/random/variate_generator.hpp> | ||
| 15 | |||
| 16 | namespace openstudio { | ||
| 17 | |||
| 18 | 227 | Vector createVector(const std::vector<double>& values) { | |
| 19 | 227 | size_t n = values.size(); | |
| 20 | 227 | Vector result(n); | |
| 21 |
2/2✓ Branch 0 taken 3578054 times.
✓ Branch 1 taken 227 times.
|
3578281 | for (unsigned i = 0; i < n; ++i) { |
| 22 |
1/1✓ Branch 2 taken 3578054 times.
|
3578054 | result[i] = values[i]; |
| 23 | } | ||
| 24 | 227 | return result; | |
| 25 | ✗ | } | |
| 26 | |||
| 27 | 320 | Vector createVector(const std::vector<long>& values) { | |
| 28 | 320 | size_t n = values.size(); | |
| 29 | 320 | Vector result(n); | |
| 30 |
2/2✓ Branch 0 taken 3900953 times.
✓ Branch 1 taken 320 times.
|
3901273 | for (unsigned i = 0; i < n; ++i) { |
| 31 |
1/1✓ Branch 2 taken 3900953 times.
|
3900953 | result[i] = (double)(values[i]); |
| 32 | } | ||
| 33 | 320 | return result; | |
| 34 | ✗ | } | |
| 35 | |||
| 36 | 11696 | std::vector<double> toStandardVector(const Vector& values) { | |
| 37 | 11696 | size_t n = values.size(); | |
| 38 |
1/1✓ Branch 1 taken 11696 times.
|
11696 | std::vector<double> result(n); |
| 39 |
2/2✓ Branch 0 taken 1691032 times.
✓ Branch 1 taken 11696 times.
|
1702728 | for (size_t i = 0; i < n; ++i) { |
| 40 |
1/1✓ Branch 1 taken 1691032 times.
|
1691032 | result[i] = values[i]; |
| 41 | } | ||
| 42 | 11696 | return result; | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | 86165 | bool operator==(const Vector& lhs, const Vector& rhs) { | |
| 46 | 86165 | bool result = false; | |
| 47 |
1/2✓ Branch 2 taken 86165 times.
✗ Branch 3 not taken.
|
86165 | if (lhs.size() == rhs.size()) { |
| 48 |
2/2✓ Branch 1 taken 86165 times.
✓ Branch 4 taken 86165 times.
|
86165 | result = (norm_1(lhs - rhs) == 0); |
| 49 | } | ||
| 50 | 86165 | return result; | |
| 51 | } | ||
| 52 | |||
| 53 | 310 | bool operator!=(const Vector& lhs, const Vector& rhs) { | |
| 54 | 310 | return !(lhs == rhs); | |
| 55 | } | ||
| 56 | |||
| 57 | /// linear interpolation of the function y = f(x) at point xi | ||
| 58 | /// assumes that x is strictly increasing | ||
| 59 | 354629 | InterpInfo interpInfo(const Vector& x, double xi, double ti) { | |
| 60 | 354629 | size_t N = x.size(); | |
| 61 | |||
| 62 | InterpInfo result; | ||
| 63 | |||
| 64 |
2/2✓ Branch 1 taken 156 times.
✓ Branch 2 taken 354473 times.
|
354629 | if (x(0) == xi) { |
| 65 | 156 | result.ia = 0; | |
| 66 | 156 | result.ib = 0; | |
| 67 | 156 | result.wa = 1.0; | |
| 68 | 156 | result.wb = 0.0; | |
| 69 | 156 | result.ti = 1.0; | |
| 70 | 156 | result.extrapolated = false; | |
| 71 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 354437 times.
|
354473 | } else if (xi < x(0)) { |
| 72 | 36 | result.ia = 0; | |
| 73 | 36 | result.ib = 0; | |
| 74 | 36 | result.wa = 1.0; | |
| 75 | 36 | result.wb = 0.0; | |
| 76 | 36 | result.ti = 1.0; | |
| 77 | 36 | result.extrapolated = true; | |
| 78 |
2/2✓ Branch 1 taken 131 times.
✓ Branch 2 taken 354306 times.
|
354437 | } else if (x(N - 1) == xi) { |
| 79 | 131 | result.ia = N - 1; | |
| 80 | 131 | result.ib = N - 1; | |
| 81 | 131 | result.wa = 0.0; | |
| 82 | 131 | result.wb = 1.0; | |
| 83 | 131 | result.ti = 1.0; | |
| 84 | 131 | result.extrapolated = false; | |
| 85 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 354282 times.
|
354306 | } else if (xi > x(N - 1)) { |
| 86 | 24 | result.ia = N - 1; | |
| 87 | 24 | result.ib = N - 1; | |
| 88 | 24 | result.wa = 0.0; | |
| 89 | 24 | result.wb = 1.0; | |
| 90 | 24 | result.ti = 1.0; | |
| 91 | 24 | result.extrapolated = true; | |
| 92 | } else { | ||
| 93 | |||
| 94 | // ((xi > x(0)) && (xi < x(N-1))) | ||
| 95 | 354282 | result.extrapolated = false; | |
| 96 |
1/1✓ Branch 1 taken 354282 times.
|
354282 | Vector::const_iterator begin = x.begin(); |
| 97 |
2/2✓ Branch 1 taken 354282 times.
✓ Branch 4 taken 354282 times.
|
354282 | Vector::const_iterator it = std::lower_bound(begin, x.end(), xi); |
| 98 | |||
| 99 | 354282 | result.ia = (unsigned)(it - begin - 1); | |
| 100 | 354282 | result.ib = (unsigned)(it - begin); | |
| 101 |
2/2✓ Branch 0 taken 353634 times.
✓ Branch 1 taken 648 times.
|
354282 | if (ti < 0.0) { |
| 102 |
3/3✓ Branch 1 taken 353634 times.
✓ Branch 4 taken 353634 times.
✓ Branch 7 taken 353634 times.
|
353634 | result.wa = (x(result.ib) - xi) / (x(result.ib) - x(result.ia)); |
| 103 |
3/3✓ Branch 1 taken 353634 times.
✓ Branch 4 taken 353634 times.
✓ Branch 7 taken 353634 times.
|
353634 | result.wb = (xi - x(result.ia)) / (x(result.ib) - x(result.ia)); |
| 104 | } else { | ||
| 105 |
1/1✓ Branch 1 taken 648 times.
|
648 | result.wb = xi - x(result.ia); |
| 106 | 648 | result.wa = ti - result.wb; | |
| 107 |
3/4✓ Branch 0 taken 648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 641 times.
|
648 | if ((result.wb > 0.0) && (result.wb < ti)) { |
| 108 | 7 | result.ti = ti; | |
| 109 | } else { | ||
| 110 | 641 | result.wa = 0.0; | |
| 111 | 641 | result.wb = 1.0; | |
| 112 | 641 | result.ti = 1.0; | |
| 113 | } | ||
| 114 | } | ||
| 115 | 354282 | } | |
| 116 | |||
| 117 | 354629 | return result; | |
| 118 | } | ||
| 119 | |||
| 120 | /// linear interpolation of the function y = f(x) at point xi | ||
| 121 | /// assumes that x is strictly increasing | ||
| 122 | 354485 | double interp(const Vector& x, const Vector& y, double xi, InterpMethod interpMethod, ExtrapMethod extrapMethod, double ti) { | |
| 123 | |||
| 124 |
1/1✓ Branch 1 taken 354485 times.
|
354485 | size_t N = x.size(); |
| 125 | |||
| 126 | 354485 | double result = 0.0; | |
| 127 | |||
| 128 |
2/3✓ Branch 1 taken 354485 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 354485 times.
|
354485 | if (y.size() != N) { |
| 129 | ✗ | return result; | |
| 130 | } | ||
| 131 | |||
| 132 |
3/4✓ Branch 0 taken 648 times.
✓ Branch 1 taken 353837 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 648 times.
|
354485 | if (interpMethod == AverageInterp && ti <= 0.0) { |
| 133 | ✗ | LOG_FREE_AND_THROW("openstudio.Vector", "Value of ti must be positive when interpolating using the AverageInterp method."); | |
| 134 | } | ||
| 135 | |||
| 136 |
1/1✓ Branch 1 taken 354485 times.
|
354485 | InterpInfo info = interpInfo(x, xi, ti); |
| 137 | |||
| 138 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 354461 times.
|
354485 | if (info.extrapolated) { |
| 139 |
2/3✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
24 | switch (extrapMethod) { |
| 140 | 18 | case NoneExtrap: | |
| 141 | // set to zero | ||
| 142 | 18 | result = 0.0; | |
| 143 | 18 | break; | |
| 144 | 6 | case NearestExtrap: | |
| 145 | // pick closest point | ||
| 146 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
|
6 | result = (info.wa > info.wb ? y(info.ia) : y(info.ib)); |
| 147 | 6 | break; | |
| 148 | } | ||
| 149 | } else { | ||
| 150 |
5/6✓ Branch 0 taken 678 times.
✓ Branch 1 taken 648 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 353105 times.
✗ Branch 5 not taken.
|
354461 | switch (interpMethod) { |
| 151 | 678 | case LinearInterp: | |
| 152 | // linear interpolation | ||
| 153 |
2/2✓ Branch 1 taken 678 times.
✓ Branch 4 taken 678 times.
|
678 | result = info.wa * y(info.ia) + info.wb * y(info.ib); |
| 154 | 678 | break; | |
| 155 | 648 | case AverageInterp: | |
| 156 | // average interpolation | ||
| 157 |
2/2✓ Branch 1 taken 648 times.
✓ Branch 4 taken 648 times.
|
648 | result = (info.wa * y(info.ia) + info.wb * y(info.ib)) / info.ti; |
| 158 | 648 | break; | |
| 159 | 15 | case NearestInterp: | |
| 160 | // pick closest point | ||
| 161 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
✓ Branch 3 taken 6 times.
✓ Branch 6 taken 9 times.
|
15 | result = (info.wa > info.wb ? y(info.ia) : y(info.ib)); |
| 162 | 15 | break; | |
| 163 | 15 | case HoldLastInterp: | |
| 164 | // set to previous value | ||
| 165 |
1/1✓ Branch 1 taken 15 times.
|
15 | result = y(info.ia); |
| 166 | 15 | break; | |
| 167 | 353105 | case HoldNextInterp: | |
| 168 | // set to next value | ||
| 169 |
1/1✓ Branch 1 taken 353105 times.
|
353105 | result = y(info.ib); |
| 170 | 353105 | break; | |
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | 354485 | return result; | |
| 175 | } | ||
| 176 | |||
| 177 | /// linear interpolation of the function y = f(x) at points xi | ||
| 178 | /// assumes that x is strictly increasing | ||
| 179 | ✗ | Vector interp(const Vector& x, const Vector& y, const Vector& xi, InterpMethod interpMethod, ExtrapMethod extrapMethod, double ti) { | |
| 180 | |||
| 181 | ✗ | size_t N = x.size(); | |
| 182 | |||
| 183 | ✗ | Vector result(N); | |
| 184 | |||
| 185 | ✗ | if (y.size() != N) { | |
| 186 | ✗ | return result; | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | for (unsigned i = 0; i < N; ++i) { | |
| 190 | ✗ | result(i) = interp(x, y, xi(i), interpMethod, extrapMethod, ti); | |
| 191 | } | ||
| 192 | |||
| 193 | ✗ | return result; | |
| 194 | ✗ | } | |
| 195 | |||
| 196 | /// generates a Vector of N points randomly drawn between and including a and b. | ||
| 197 | 172 | Vector randVector(double a, double b, unsigned N) { | |
| 198 | // seed random number generator | ||
| 199 |
4/7✓ Branch 0 taken 72 times.
✓ Branch 1 taken 100 times.
✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 72 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
172 | static std::minstd_rand generator(42u); |
| 200 | |||
| 201 | // define distribution | ||
| 202 |
1/1✓ Branch 1 taken 172 times.
|
172 | boost::uniform_real<> dist(a, b); |
| 203 | |||
| 204 | // create a generator | ||
| 205 | 172 | boost::variate_generator<std::minstd_rand&, boost::uniform_real<>> uniformGenerator(generator, dist); | |
| 206 | |||
| 207 |
1/1✓ Branch 1 taken 172 times.
|
172 | Vector result(N); |
| 208 |
2/2✓ Branch 0 taken 1506720 times.
✓ Branch 1 taken 172 times.
|
1506892 | for (unsigned n = 0; n < N; ++n) { |
| 209 |
2/2✓ Branch 1 taken 1506720 times.
✓ Branch 4 taken 1506720 times.
|
1506720 | result(n) = uniformGenerator(); // draw from generator |
| 210 | } | ||
| 211 | |||
| 212 | 344 | return result; | |
| 213 | ✗ | } | |
| 214 | |||
| 215 | /// generates a Vector of N points linearly spaced between and including a and b. | ||
| 216 | 118 | Vector linspace(double a, double b, unsigned N) { | |
| 217 | 118 | double delta = (b - a) / (N - 1); | |
| 218 | |||
| 219 | 118 | Vector result(N); | |
| 220 |
2/2✓ Branch 0 taken 54461 times.
✓ Branch 1 taken 118 times.
|
54579 | for (unsigned n = 0; n < N; ++n) { |
| 221 |
1/1✓ Branch 1 taken 54461 times.
|
54461 | result(n) = a + n * delta; |
| 222 | } | ||
| 223 | |||
| 224 | 118 | return result; | |
| 225 | ✗ | } | |
| 226 | |||
| 227 | /// generates a Vector linearly spaced points starting at a and ending before or at b with interval delta. | ||
| 228 | 2 | Vector deltaSpace(double a, double b, double delta) { | |
| 229 | 2 | unsigned N = (unsigned)std::floor((b - a) / delta) + 1; | |
| 230 | |||
| 231 | 2 | Vector result(N); | |
| 232 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
|
22 | for (unsigned n = 0; n < N; ++n) { |
| 233 |
1/1✓ Branch 1 taken 20 times.
|
20 | result(n) = a + n * delta; |
| 234 | } | ||
| 235 | |||
| 236 | 2 | return result; | |
| 237 | ✗ | } | |
| 238 | |||
| 239 | /// generates a Vector of N points logarithmically spaced between and including base^a and base^b. | ||
| 240 | 3 | Vector logspace(double a, double b, unsigned N, double base) { | |
| 241 |
1/1✓ Branch 1 taken 3 times.
|
3 | Vector powers = linspace(a, b, N); |
| 242 | |||
| 243 |
1/1✓ Branch 1 taken 3 times.
|
3 | Vector result(N); |
| 244 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3 times.
|
36 | for (unsigned n = 0; n < N; ++n) { |
| 245 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
|
33 | result(n) = std::pow(base, powers(n)); |
| 246 | } | ||
| 247 | |||
| 248 | 6 | return result; | |
| 249 | 3 | } | |
| 250 | |||
| 251 | /// take the natural logarithm of elements of a Vector | ||
| 252 | ✗ | Vector log(const Vector& x) { | |
| 253 | ✗ | size_t N = x.size(); | |
| 254 | ✗ | Vector result(N); | |
| 255 | ✗ | for (unsigned i = 0; i < N; ++i) { | |
| 256 | ✗ | result(i) = std::log(x(i)); | |
| 257 | } | ||
| 258 | ✗ | return result; | |
| 259 | ✗ | } | |
| 260 | |||
| 261 | /// take the logarithm of elements of a Vector with certain base | ||
| 262 | 3 | Vector log(const Vector& x, double base) { | |
| 263 | 3 | double logBase = std::log(base); | |
| 264 | 3 | size_t N = x.size(); | |
| 265 | 3 | Vector result(N); | |
| 266 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (unsigned i = 0; i < N; ++i) { |
| 267 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | result(i) = std::log(x(i)) / logBase; |
| 268 | } | ||
| 269 | 3 | return result; | |
| 270 | ✗ | } | |
| 271 | |||
| 272 | /// compute the cumulative sum of a Vector | ||
| 273 | ✗ | Vector cumsum(const Vector& x, double runningSum) { | |
| 274 | ✗ | unsigned N = x.size(); | |
| 275 | ✗ | Vector result(N); | |
| 276 | ✗ | for (unsigned i = 0; i < N; ++i) { | |
| 277 | ✗ | runningSum += x(i); | |
| 278 | ✗ | result(i) = runningSum; | |
| 279 | } | ||
| 280 | ✗ | return result; | |
| 281 | ✗ | } | |
| 282 | |||
| 283 | /// dot product | ||
| 284 | 1 | double dot(const Vector& lhs, const Vector& rhs) { | |
| 285 | 1 | return boost::numeric::ublas::inner_prod(lhs, rhs); | |
| 286 | } | ||
| 287 | |||
| 288 | /// sum | ||
| 289 | 173 | double sum(const Vector& vector) { | |
| 290 | 173 | return boost::numeric::ublas::sum(vector); | |
| 291 | } | ||
| 292 | |||
| 293 | /// maximum | ||
| 294 | 7 | double maximum(const Vector& vector) { | |
| 295 | 7 | double max = 0; | |
| 296 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | if (!vector.empty()) { |
| 297 |
3/3✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
|
7 | max = *std::max_element(vector.begin(), vector.end()); |
| 298 | } | ||
| 299 | 7 | return max; | |
| 300 | } | ||
| 301 | |||
| 302 | /// minimum | ||
| 303 | 7 | double minimum(const Vector& vector) { | |
| 304 | 7 | double min = 0; | |
| 305 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | if (!vector.empty()) { |
| 306 |
3/3✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
|
7 | min = *std::min_element(vector.begin(), vector.end()); |
| 307 | } | ||
| 308 | 7 | return min; | |
| 309 | } | ||
| 310 | |||
| 311 | /// mean | ||
| 312 | 21 | double mean(const Vector& vector) { | |
| 313 | 21 | double avg = 0; | |
| 314 | 21 | size_t N = vector.size(); | |
| 315 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | if (N > 0) { |
| 316 | 21 | avg = sum(vector) / N; | |
| 317 | } | ||
| 318 | 21 | return avg; | |
| 319 | } | ||
| 320 | |||
| 321 | /// variance | ||
| 322 | 14 | double variance(const Vector& vector) { | |
| 323 | 14 | double result = 0; | |
| 324 | 14 | size_t N = vector.size(); | |
| 325 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (N > 0) { |
| 326 | 14 | double sumSquares = pow(boost::numeric::ublas::norm_2(vector), 2.0); | |
| 327 | 14 | result = sumSquares / N - pow(mean(vector), 2); | |
| 328 | } | ||
| 329 | 14 | return result; | |
| 330 | } | ||
| 331 | |||
| 332 | /// std dev | ||
| 333 | 7 | double stdDev(const Vector& vector) { | |
| 334 | 7 | return std::sqrt(variance(vector)); | |
| 335 | } | ||
| 336 | |||
| 337 | 2 | std::function<double(const Vector&)> sumVectorFunctor() { | |
| 338 | 2 | return {&sum}; | |
| 339 | } | ||
| 340 | |||
| 341 | 2 | std::function<double(const Vector&)> maximumVectorFunctor() { | |
| 342 | 2 | return {&maximum}; | |
| 343 | } | ||
| 344 | |||
| 345 | 2 | std::function<double(const Vector&)> minimumVectorFunctor() { | |
| 346 | 2 | return {&minimum}; | |
| 347 | } | ||
| 348 | |||
| 349 | 2 | std::function<double(const Vector&)> meanVectorFunctor() { | |
| 350 | 2 | return {&mean}; | |
| 351 | } | ||
| 352 | |||
| 353 | 2 | std::function<double(const Vector&)> varianceVectorFunctor() { | |
| 354 | 2 | return {&variance}; | |
| 355 | } | ||
| 356 | |||
| 357 | 2 | std::function<double(const Vector&)> stdDevVectorFunctor() { | |
| 358 | 2 | return {&stdDev}; | |
| 359 | } | ||
| 360 | |||
| 361 | 12 | double evaluateDoubleFromVectorFunctor(const std::function<double(const Vector&)>& functor, const Vector& vector) { | |
| 362 | 12 | return functor(vector); | |
| 363 | } | ||
| 364 | } // namespace openstudio | ||
| 365 |