src/utilities/data/Matrix.hpp
| 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 | #ifndef UTILITIES_DATA_MATRIX_HPP | ||
| 7 | #define UTILITIES_DATA_MATRIX_HPP | ||
| 8 | |||
| 9 | #include "Vector.hpp" | ||
| 10 | #include "../UtilitiesAPI.hpp" | ||
| 11 | #include "../core/Logger.hpp" | ||
| 12 | |||
| 13 | #include <boost/numeric/ublas/matrix.hpp> | ||
| 14 | #include <boost/numeric/ublas/io.hpp> | ||
| 15 | #include <boost/numeric/ublas/lu.hpp> | ||
| 16 | |||
| 17 | namespace openstudio { | ||
| 18 | |||
| 19 | /// Matrix | ||
| 20 | using Matrix = boost::numeric::ublas::matrix<double>; | ||
| 21 | |||
| 22 | ////////////////////////////////////////////////////////////////////////// | ||
| 23 | // Begin SWIG'able, copy and paste into Matrix.i | ||
| 24 | ////////////////////////////////////////////////////////////////////////// | ||
| 25 | |||
| 26 | /// new operators | ||
| 27 | |||
| 28 | UTILITIES_API bool operator==(const Matrix& lhs, const Matrix& rhs); | ||
| 29 | UTILITIES_API bool operator!=(const Matrix& lhs, const Matrix& rhs); | ||
| 30 | |||
| 31 | /// common methods | ||
| 32 | |||
| 33 | /// linear interpolation of the function v = f(x, y) at point xi, yi | ||
| 34 | /// assumes that x and y are strictly increasing | ||
| 35 | /// ti is the total seconds of the timestep interval; it is used for | ||
| 36 | /// AverageInterp and must be positive | ||
| 37 | UTILITIES_API double interp(const Vector& x, const Vector& y, const Matrix& v, double xi, double yi, InterpMethod interpMethod = LinearInterp, | ||
| 38 | ExtrapMethod extrapMethod = NoneExtrap, double ti = -9999.0); | ||
| 39 | |||
| 40 | /// linear interpolation of the function v = f(x, y) at points xi, yi | ||
| 41 | /// assumes that x and y are strictly increasing | ||
| 42 | /// ti is the total seconds of the timestep interval; it is used for | ||
| 43 | /// AverageInterp and must be positive | ||
| 44 | UTILITIES_API Vector interp(const Vector& x, const Vector& y, const Matrix& v, const Vector& xi, double yi, InterpMethod interpMethod = LinearInterp, | ||
| 45 | ExtrapMethod extrapMethod = NoneExtrap, double ti = -9999.0); | ||
| 46 | |||
| 47 | /// linear interpolation of the function v = f(x, y) at points xi, yi | ||
| 48 | /// assumes that x and y are strictly increasing | ||
| 49 | /// ti is the total seconds of the timestep interval; it is used for | ||
| 50 | /// AverageInterp and must be positive | ||
| 51 | UTILITIES_API Vector interp(const Vector& x, const Vector& y, const Matrix& v, double xi, const Vector& yi, InterpMethod interpMethod = LinearInterp, | ||
| 52 | ExtrapMethod extrapMethod = NoneExtrap, double ti = -9999.0); | ||
| 53 | |||
| 54 | /// linear interpolation of the function v = f(x, y) at points xi, yi | ||
| 55 | /// assumes that x and y are strictly increasing | ||
| 56 | /// ti is the total seconds of the timestep interval; it is used for | ||
| 57 | /// AverageInterp and must be positive | ||
| 58 | UTILITIES_API Matrix interp(const Vector& x, const Vector& y, const Matrix& v, const Vector& xi, const Vector& yi, | ||
| 59 | InterpMethod interpMethod = LinearInterp, ExtrapMethod extrapMethod = NoneExtrap, double ti = -9999.0); | ||
| 60 | |||
| 61 | /// matrix product | ||
| 62 | UTILITIES_API Matrix prod(const Matrix& lop, const Matrix& rop); | ||
| 63 | |||
| 64 | /// vector product | ||
| 65 | UTILITIES_API Vector prod(const Matrix& m, const Vector& v); | ||
| 66 | |||
| 67 | /// outer product | ||
| 68 | UTILITIES_API Matrix outerProd(const Vector& lhs, const Vector& rhs); | ||
| 69 | |||
| 70 | /// take the natural logarithm of Matrix elements, componentwise | ||
| 71 | UTILITIES_API Matrix log(const Matrix& v); | ||
| 72 | |||
| 73 | /// take the logarithm of Matrix elements with respect to base, componentwise | ||
| 74 | UTILITIES_API Matrix log(const Matrix& v, double base); | ||
| 75 | |||
| 76 | /// generates a M x N Matrix whose elements come from the uniform distribution on [a,b]. | ||
| 77 | UTILITIES_API Matrix randMatrix(double a, double b, unsigned M, unsigned N); | ||
| 78 | |||
| 79 | /// sum of all elements | ||
| 80 | UTILITIES_API double sum(const Matrix& matrix); | ||
| 81 | |||
| 82 | /// maximum of all elements | ||
| 83 | UTILITIES_API double maximum(const Matrix& matrix); | ||
| 84 | |||
| 85 | /// minimum of all elements | ||
| 86 | UTILITIES_API double minimum(const Matrix& matrix); | ||
| 87 | |||
| 88 | /// mean of all elements | ||
| 89 | UTILITIES_API double mean(const Matrix& matrix); | ||
| 90 | |||
| 91 | /// get the connected components from an NxN adjacency matrix (1.0 for i-j connected, 0.0 for i-j not connected) | ||
| 92 | UTILITIES_API std::vector<std::vector<unsigned>> findConnectedComponents(const Matrix& matrix); | ||
| 93 | |||
| 94 | // from the boost vault: | ||
| 95 | // The following code inverts the matrix input using LU-decomposition with backsubstitution of unit vectors. Reference: Numerical Recipes in C, 2nd ed., by Press, Teukolsky, Vetterling & Flannery. | ||
| 96 | /// Matrix inversion routine, using lu_factorize and lu_substitute in uBLAS to invert a matrix */ | ||
| 97 | template <class T> | ||
| 98 | 283671 | bool invert(const boost::numeric::ublas::matrix<T>& input, boost::numeric::ublas::matrix<T>& inverse) { | |
| 99 | |||
| 100 | // create a working copy of the input | ||
| 101 |
1/1✓ Branch 1 taken 283671 times.
|
283671 | boost::numeric::ublas::matrix<T> A(input); |
| 102 | |||
| 103 | // create a permutation matrix for the LU-factorization | ||
| 104 |
1/1✓ Branch 2 taken 283671 times.
|
283671 | boost::numeric::ublas::permutation_matrix<std::size_t> pm(A.size1()); |
| 105 | |||
| 106 | // perform LU-factorization | ||
| 107 |
1/1✓ Branch 1 taken 283671 times.
|
283671 | typename boost::numeric::ublas::matrix<T>::size_type res = boost::numeric::ublas::lu_factorize(A, pm); |
| 108 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 283653 times.
|
283671 | if (res != 0) { |
| 109 |
12/12✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
✓ Branch 10 taken 18 times.
✓ Branch 13 taken 18 times.
✓ Branch 16 taken 18 times.
✓ Branch 19 taken 18 times.
✓ Branch 22 taken 18 times.
✓ Branch 25 taken 18 times.
✓ Branch 28 taken 18 times.
✓ Branch 31 taken 18 times.
✓ Branch 34 taken 18 times.
|
54 | LOG_FREE(Info, "boost.ublas", |
| 110 | "boost::numeric::ublas::lu_factorize returned res = " << res << ", A = " << A << ", pm = " << pm << " for input = " << input); | ||
| 111 | 18 | return false; | |
| 112 | } | ||
| 113 | |||
| 114 | // create identity matrix of "inverse" | ||
| 115 |
2/2✓ Branch 2 taken 283653 times.
✓ Branch 5 taken 283653 times.
|
283653 | inverse.assign(boost::numeric::ublas::identity_matrix<T>(A.size1())); |
| 116 | |||
| 117 | // backsubstitute to get the inverse | ||
| 118 | try { | ||
| 119 |
1/1✓ Branch 1 taken 283653 times.
|
283653 | boost::numeric::ublas::lu_substitute(A, pm, inverse); |
| 120 | ✗ | } catch (std::exception& e) { | |
| 121 | ✗ | LOG_FREE(Info, "boost.ublas", "boost::numeric::ublas::lu_substitute threw exception '" << e.what() << "' for A = " << A << ", pm = " << pm); | |
| 122 | ✗ | return false; | |
| 123 | } | ||
| 124 | |||
| 125 | 283653 | return true; | |
| 126 | 283671 | } | |
| 127 | |||
| 128 | } // namespace openstudio | ||
| 129 | |||
| 130 | #endif //UTILITIES_DATA_MATRIX_HPP | ||
| 131 |