Doxygen 1.9.8
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
 
Loading...
Searching...
No Matches
tsgUtils.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, Miroslav Stoyanov
3 *
4 * This file is part of
5 * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
12 * and the following disclaimer in the documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND IMPLIED.
25 * THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY PATENT,
26 * COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE.
27 * THE USER ASSUMES RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING FROM OR ARISING OUT OF,
28 * IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
29 */
30
31#ifndef __TASMANIAN_SPARSE_GRID_UTILS_HPP
32#define __TASMANIAN_SPARSE_GRID_UTILS_HPP
33
46#include "tsgMathUtils.hpp"
47
56namespace TasGrid{
57
62namespace Utils{
63
65template<typename scalar_type>
66void transpose(long long M, long long N, scalar_type const A[], scalar_type B[]);
68template<typename scalar_type>
69std::vector<scalar_type> transpose(long long M, long long N, scalar_type const A[]){
70 std::vector<scalar_type> B(M * N);
71 transpose(M, N, A, B.data());
72 return B;
73}
74
81template<typename IntA, typename IntB>
82inline size_t size_mult(IntA a, IntB b){ return static_cast<size_t>(a) * static_cast<size_t>(b); }
83
91template<typename T, typename I>
92std::vector<typename std::remove_const<T>::type> copyArray(T* x, I size){
93 return (x == nullptr) ? std::vector<typename std::remove_const<T>::type>() :
94 std::vector<typename std::remove_const<T>::type>(x, x + static_cast<size_t>(size));
95}
96
104template<typename T>
105std::vector<T> mergeVectors(std::vector<std::vector<T>> const &vec){
106 size_t total_size = 0;
107 for(auto const &v : vec) total_size += v.size();
108 std::vector<T> result;
109 result.reserve(total_size);
110 for(auto const &v : vec) result.insert(result.end(), v.begin(), v.end());
111 return result;
112}
113
126template<typename T>
128public:
130 Wrapper2D(int stride_size, T *raw_data) : stride(static_cast<size_t>(stride_size)), data(raw_data){}
133
135 T* getStrip(int i){ return &(data[size_mult(i, stride)]); }
136
137private:
138 size_t stride;
139 T *data;
140};
141
146template<bool condition>
147using use_if = typename std::enable_if<condition, void>::type;
148
153template<typename T, typename U> T exchange(T& x, U new_x){
154 T old_x = x;
155 x = static_cast<T>(new_x);
156 return old_x;
157}
158
163template<typename T, typename... Args>
164std::unique_ptr<T> make_unique(Args&&... args){
165 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
166}
167
168}
169
170}
171
172#endif
Wraps around a C-style of an array and mimics 2D data-structure.
Definition tsgUtils.hpp:127
Wrapper2D(int stride_size, T *raw_data)
Wrap around raw_data with the given stride_size.
Definition tsgUtils.hpp:130
T * getStrip(int i)
Return a pointer to the i-th strip.
Definition tsgUtils.hpp:135
~Wrapper2D()
Default destructor, raw_data has to be deleted elsewhere.
Definition tsgUtils.hpp:132
std::unique_ptr< T > make_unique(Args &&... args)
Equivalent to C++14 make_unique.
Definition tsgUtils.hpp:164
std::vector< T > mergeVectors(std::vector< std::vector< T > > const &vec)
Takes a vector of vectors and returns a single contiguous vector.
Definition tsgUtils.hpp:105
size_t size_mult(IntA a, IntB b)
Converts two integer-like variables to size_t and returns the product..
Definition tsgUtils.hpp:82
std::vector< typename std::remove_const< T >::type > copyArray(T *x, I size)
Copies an array into a vector, returns empty vector if the input is nullpntr.
Definition tsgUtils.hpp:92
T exchange(T &x, U new_x)
Equivalent to C++14 exchange, but works with simpler types (int, double, float*).
Definition tsgUtils.hpp:153
typename std::enable_if< condition, void >::type use_if
Equivalent to C++14 enable_if_t<condition, void>
Definition tsgUtils.hpp:147
void transpose(long long M, long long N, scalar_type const A[], scalar_type B[])
Constructs the transpose of an M by N matrix A in column major format, result is stored in B (impleme...
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68
Math functions and constants.