UQTk: Uncertainty Quantification Toolkit  3.1.1
arraytools.h
Go to the documentation of this file.
1 /* =====================================================================================
2 
3  The UQ Toolkit (UQTk) version 3.1.1
4  Copyright (2021) NTESS
5  https://www.sandia.gov/UQToolkit/
6  https://github.com/sandialabs/UQTk
7 
8  Copyright 2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
9  Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10  retains certain rights in this software.
11 
12  This file is part of The UQ Toolkit (UQTk)
13 
14  UQTk is open source software: you can redistribute it and/or modify
15  it under the terms of BSD 3-Clause License
16 
17  UQTk is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  BSD 3 Clause License for more details.
21 
22  You should have received a copy of the BSD 3 Clause License
23  along with UQTk. If not, see https://choosealicense.com/licenses/bsd-3-clause/.
24 
25  Questions? Contact the UQTk Developers at <uqtk-developers@software.sandia.gov>
26  Sandia National Laboratories, Livermore, CA, USA
27 ===================================================================================== */
32 
33 #ifndef ARRAYTOOLS_H
34 #define ARRAYTOOLS_H
35 
36 #include <stdlib.h>
37 #include "Array1D.h"
38 #include "Array2D.h"
39 
41 template <typename T> void array1Dto2D(Array1D<T>& arr_1d,Array2D<T>& arr);
42 
44 template <typename T> void array2Dto1D(Array2D<T>& arr_2d,Array1D<T>& arr);
45 
47 template <typename T> void paste(Array1D<T>& arr1,Array1D<T>& arr2,Array2D<T>& arr);
48 
51 template <typename T> void generate_multigrid(Array2D<T>& multigrid,Array2D<T>& grid);
52 
55 
62 
66 void append(Array1D<int>& x, Array1D<int>& y);
67 
69 template <typename T> void transpose(Array2D<T> &x, Array2D<T> &xt);
70 
72 void flatten(Array2D<double>& arr_2, Array1D<double>& arr_1);
73 
78 
83 
85 void swap(Array1D<double>& arr,int i,int j);
86 
88 void swap(Array2D<double>& arr,int i,int j);
89 
91 double access(int nx, int ny, Array1D<double>& arr_1, int i, int j);
92 
94 template <typename T> void getRow(Array2D<T> &arr2d, int k, Array1D<T> &arr1d);
95 
97 template <typename T> void getCol(Array2D<T> &arr2d, int k, Array1D<T> &arr1d);
98 
100 template <typename T> void addVal(int n, T *arr1d, T val) ;
101 
103 template <typename T> void addVal(Array1D<T> &arr1d, T val) ;
104 
106 template <typename T> void addVal(Array2D<T> &arr2d, T val) ;
107 
109 template <typename T> void subVector(Array1D<T> &vector, Array1D<int> &ind, Array1D<T> &subvector);
110 
112 template <typename T> void subMatrix_row(Array2D<T> &matrix, Array1D<int> &ind, Array2D<T> &submatrix);
113 
115 template <typename T> void subMatrix_col(Array2D<T> &matrix, Array1D<int> &ind, Array2D<T> &submatrix);
116 
119 template <typename T> void matPvec(Array2D<T> &matrix, const Array1D<T> &rc, T alpha, char *RC);
120 
122 template <typename T> T maxVal(const Array1D<T>& vector, int *indx) ;
123 
126 
132 
134 void shell_sort (int *a, int n) ;
136 void shell_sort(Array1D<int>& array);
138 void shell_sort(Array1D<double>& array);
140 void shell_sort_col(Array2D<double>& array,int col,Array1D<int>& newInd, Array1D<int>& oldInd);
142 void shell_sort_all(Array2D<double>& array,Array1D<int>& newInd, Array1D<int>& oldInd);
144 void quicksort3(Array1D<double>& arr, int l, int r);
146 void quicksort3(Array2D<double>& arr,int left, int right,int col);
148 void quicksort3(Array2D<double>& arr,int left, int right);
149 
157 
160 template <typename T> void find(Array1D<T> &theta, T lmbda, string type, Array1D<int> &indx) ;
161 
164 void prodAlphaMatVec (Array2D<double>& A, Array1D<double>& x, double alpha, Array1D<double>& y) ;
167 void prodAlphaMatTVec(Array2D<double>& A, Array1D<double>& x, double alpha, Array1D<double>& y) ;
170 void prodAlphaMatMat(Array2D<double>& A, Array2D<double>& B, double alpha, Array2D<double>& C);
173 void prodAlphaMatTMat(Array2D<double>& A, Array2D<double>& B, double alpha, Array2D<double>& C) ;
175 void addVecAlphaVecPow(Array1D<double>& x, double alpha, Array1D<double>& y, int ip) ;
180 
181 
184 template <typename T> void delRow(Array2D<T>& A, int irow) ;
185 
188 template <typename T> void delCol(Array2D<T> &A, int icol) ;
189 
192 template <typename T> void delCol(Array1D<T> &x, int icol) ;
193 
202 void paddMatRow(Array2D<int>& A, Array1D<int>& x) ;
205 void paddMatCol(Array2D<int>& A, Array1D<int>& x) ;
208 void paddMatColScal(Array2D<double>& A, Array1D<double>& x, double scal) ;
209 
211 bool is_equal(Array1D<int>& a, Array1D<int>& b);
215 bool is_less(Array1D<int>& a, Array1D<int>& b);
218 
221 int vecIsInArray(Array1D<int>& vec, Array2D<int>& array);
222 
224 double select_kth(int k, Array1D<double>& arr);
225 
228 double logdeterm(Array2D<double>& mat);
229 
231 double trace(Array2D<double>& mat);
232 
235 
237 Array2D<double> diag(Array1D<double>& diagonal_array);
238 /**********************************************************
239 NEW ROUTINES - Kenny
240 ***********************************************************/
241 
244 
247 
249 Array2D<double> mtxdel(Array2D<double>&, int index, int dim);
250 
253 
256 
259 
262 
265 
268 
271 
274 
277 
280 
282 void scaleinplace(Array1D<double>&, double);
283 
285 void scaleinplace(Array1D<int>&, int);
286 
288 void scaleinplace(Array2D<double>&, double);
289 
291 void scaleinplace(Array2D<int>&, int);
292 
295 
298 
301 
304 
306 double norm(Array1D<double>&);
307 
310 
313 
316 
319 
322 
325 
328 
331 
334 
337 
340 
343 
346 
348 void printarray(Array1D<int>&);
349 
352 
354 void printarray(Array2D<int>&);
355 
356 
357 //---------------------------------------------------------------------------------------
358 #endif // ARRAYTOOLS_H
1D Array class for any type T
2D Array class for any type T
void prodAlphaMatMat(Array2D< double > &A, Array2D< double > &B, double alpha, Array2D< double > &C)
Returns , where 'A' and 'B' are 2D arrays and 'alpha' is a scalar. The 2D array 'C' has elements.
Definition: arraytools.cpp:1020
void QR(Array2D< double > &B, Array2D< double > &Q, Array2D< double > &R)
Computes the QR factorization of a 2D Array (need not be square)
Definition: arraytools.cpp:2227
void merge(Array2D< double > &x, Array2D< double > &y, Array2D< double > &xy)
Merge 2d double arrays (vertical stack)
Definition: arraytools.cpp:179
Array1D< double > Ainvb(Array2D< double > &A, Array1D< double > &b)
Solves linear system Ax=b, i.e. return A^(-1)*b where A is real, symmetric and positive definite.
Definition: arraytools.cpp:2177
void array1Dto2D(Array1D< T > &arr_1d, Array2D< T > &arr)
Store a given 1d array in a 2d array with a single second dimension.
Definition: arraytools.cpp:50
Array2D< double > dotT(Array2D< double > &, Array2D< double > &)
Returns the matrix matrix^T product.
Definition: arraytools.cpp:2104
Array2D< double > dotmult(Array2D< double > &A, Array2D< double > &B)
Returns the elementwise multiplication of two 2D Arrays.
Definition: arraytools.cpp:1903
void swap(Array1D< double > &arr, int i, int j)
Swap i-th and j-th elements of the array arr.
Definition: arraytools.cpp:357
void LSTSQ(Array2D< double > &A, Array1D< double > &b, Array1D< double > &x)
Least squares solution for overdetermined system. Note that A must be "taller than wide"....
Definition: arraytools.cpp:2195
void paddMatColScal(Array2D< double > &A, Array1D< double > &x, double scal)
Padds square 2D array 'A' with the elements of 'x' and 'scal' as follows: and .
Definition: arraytools.cpp:1313
void paste(Array1D< T > &arr1, Array1D< T > &arr2, Array2D< T > &arr)
Paste two 1d arrays of same size into a single 2d array with second dimension equal to two.
Definition: arraytools.cpp:86
void find(Array1D< T > &theta, T lmbda, string type, Array1D< int > &indx)
Return list of indices corresponding to elements of 1D array theta that are: larger ( type="gt" ),...
Definition: arraytools.cpp:923
void SVD(Array2D< double > &A, Array2D< double > &U, Array1D< double > &S, Array2D< double > &VT)
Computes the SVD calculation of a 2D Array (need not be square)
Definition: arraytools.cpp:2289
void setdiff_s(Array1D< int > &A, Array1D< int > &B, Array1D< int > &C)
Returns ( C=Elements of A that are not in B); C is sorted in ascending order.
Definition: arraytools.cpp:608
void setdiff(Array1D< int > &A, Array1D< int > &B, Array1D< int > &C)
Returns (C=Elements of A that are not in B); C is sorted in ascending order.
Definition: arraytools.cpp:588
void delCol(Array2D< T > &A, int icol)
Deletes column 'icol' from 2D array 'A'.
Definition: arraytools.cpp:1164
double logdeterm(Array2D< double > &mat)
Log-determinant of a real symmetric positive-definite matrix.
Definition: arraytools.cpp:1491
void matPvec(Array2D< T > &matrix, const Array1D< T > &rc, T alpha, char *RC)
Adds scaled row or column to all rows / columns of a matrix (double or int)
Definition: arraytools.cpp:525
void fold_1dto2d_rowfirst(Array1D< double > &x1, Array2D< double > &x2)
Fold a 1d array into a 2d array (double format), row first.
Definition: arraytools.cpp:318
int vecIsInArray(Array1D< int > &vec, Array2D< int > &array)
Checks if vec matches with any of the rows of array Returns the row number, or -1 if vec is not equal...
Definition: arraytools.cpp:1425
double dist_sq(Array1D< double > &x, Array1D< double > &y, Array1D< double > &w)
Weighted vector distance-squared.
Definition: arraytools.cpp:1994
Array2D< double > INV(Array2D< double > &A)
Returns the inverse of a square 2D Array.
Definition: arraytools.cpp:2129
void printarray(Array1D< double > &)
Prints 1D double Array to screen (alternative to for loop using cout)
Definition: arraytools.cpp:2320
bool is_less(Array1D< int > &a, Array1D< int > &b)
Checks if one 1d int array is less than another (by first element, then by second,...
Definition: arraytools.cpp:1377
void paddMatCol(Array2D< double > &A, Array1D< double > &x)
Padds 2D array 'A' with the column 'x'.
Definition: arraytools.cpp:1238
void delRow(Array2D< T > &A, int irow)
Deletes row 'irow' from 2D array 'A'.
Definition: arraytools.cpp:1135
double norm(Array1D< double > &)
Returns norm of 1D Array (Euclidean)
Definition: arraytools.cpp:1987
Array2D< double > AinvH(Array2D< double > &A, Array2D< double > &H)
Solves linear system AX=H, i.e. returns A^(-1)*H, where A is real, symmetric and positive definite.
Definition: arraytools.cpp:2159
void getCol(Array2D< T > &arr2d, int k, Array1D< T > &arr1d)
Retrieves column 'k' from 2D array 'arr2d' and returns it in 1D array 'arr1d'.
Definition: arraytools.cpp:407
void shell_sort_all(Array2D< double > &array, Array1D< int > &newInd, Array1D< int > &oldInd)
Sorts double array in ascending order according to first column, then second column breaks the tie,...
Definition: arraytools.cpp:729
void prodAlphaMatTMat(Array2D< double > &A, Array2D< double > &B, double alpha, Array2D< double > &C)
Returns , where 'A' and 'B' are 2D arrays and 'alpha' is a scalar. The 2D array 'C' has elements.
Definition: arraytools.cpp:1054
Array1D< double > subtract(Array1D< double > &, Array1D< double > &)
Returns subtraction of two 1D Arrays (must be of the same shape)
Definition: arraytools.cpp:1742
double prod_vecTmatvec(Array1D< double > &a, Array2D< double > &B, Array1D< double > &c)
Returns .
Definition: arraytools.cpp:1101
void quicksort3(Array1D< double > &arr, int l, int r)
Quick-sort with 3-way partitioning of array between indices l and r.
Definition: arraytools.cpp:775
void subMatrix_col(Array2D< T > &matrix, Array1D< int > &ind, Array2D< T > &submatrix)
Extracts from 'matrix' columns corresponding to indices 'ind' and returns them in 'submatrix' (double...
Definition: arraytools.cpp:500
void paddMatRow(Array2D< double > &A, Array1D< double > &x)
Padds 2D array 'A' with the row 'x'.
Definition: arraytools.cpp:1214
Array2D< double > diag(Array1D< double > &diagonal_array)
Returns a diagonal matrix with a given diagonal.
Definition: arraytools.cpp:1587
void getRow(Array2D< T > &arr2d, int k, Array1D< T > &arr1d)
Retrieves row 'k' from 2D array 'arr2d' and returns it in 1D array 'arr1d'.
Definition: arraytools.cpp:392
void intersect(Array1D< int > &A, Array1D< int > &B, Array1D< int > &C, Array1D< int > &iA, Array1D< int > &iB)
Finds common entries in 1D arrays 'A' and 'B' and returns them in 'C', sorted in ascending order....
Definition: arraytools.cpp:857
void array2Dto1D(Array2D< T > &arr_2d, Array1D< T > &arr)
Store a given 2d array with a single second dimension in a 1d array.
Definition: arraytools.cpp:65
void prodAlphaMatTVec(Array2D< double > &A, Array1D< double > &x, double alpha, Array1D< double > &y)
Returns , where 'A' is a 2D array, 'x' is 1D array of size and 'alpha' is a scalar....
Definition: arraytools.cpp:989
void addinplace(Array2D< double > &x, Array2D< double > &y)
Add two 2D Arrays in place. Summation is returned as x.
Definition: arraytools.cpp:1699
double trace(Array2D< double > &mat)
Trace of a matrix.
Definition: arraytools.cpp:1533
void fold_1dto2d_colfirst(Array1D< double > &x1, Array2D< double > &x2)
Fold a 1d array into a 2d array (double format), column first.
Definition: arraytools.cpp:337
void subtractinplace(Array2D< double > &x, Array2D< double > &y)
Subtract two 2D Arrays in place. Difference is returned as x.
Definition: arraytools.cpp:1786
Array2D< double > MatTMat(Array2D< double > &A)
Returns , where 'A' is a 2D array.
Definition: arraytools.cpp:1113
void shell_sort_col(Array2D< double > &array, int col, Array1D< int > &newInd, Array1D< int > &oldInd)
Sorts double array in ascending order according to a given column.
Definition: arraytools.cpp:694
void subVector(Array1D< T > &vector, Array1D< int > &ind, Array1D< T > &subvector)
Extracts from 'vector', elements corresponding to indices 'ind' and returns them in 'subvector' (doub...
Definition: arraytools.cpp:452
void shell_sort(int *a, int n)
Sorts integer array.
Definition: arraytools.cpp:629
void addVal(int n, T *arr1d, T val)
Adds 'val' to the first n elements of an array pointer (double or int)
Definition: arraytools.cpp:420
double access(int nx, int ny, Array1D< double > &arr_1, int i, int j)
Access element from 1D array 'arr_1'.
Definition: arraytools.cpp:382
T maxVal(const Array1D< T > &vector, int *indx)
Returns maximum value in 'vector' and its location in *indx (double or int)
Definition: arraytools.cpp:571
void scaleinplace(Array1D< double > &, double)
Multiply Array1D by double in place.
Definition: arraytools.cpp:1856
bool is_equal(Array1D< int > &a, Array1D< int > &b)
Checks if two 1d int arrays are equal.
Definition: arraytools.cpp:1339
void append(Array1D< double > &x, Array1D< double > &y)
Append array y to array x in place (double format)
Definition: arraytools.cpp:248
double evalLogMVN(Array1D< double > &x, Array1D< double > &mu, Array2D< double > &Sigma)
Evaluates the natural logarithm of a multivariate normal distribution.
Definition: arraytools.cpp:1545
Array1D< double > copy(Array1D< double > &)
Returns a copy of 1D array.
Definition: arraytools.cpp:1604
Array2D< double > Trans(Array2D< double > &)
Returns the transpose of a 2D Array.
Definition: arraytools.cpp:2008
void subMatrix_row(Array2D< T > &matrix, Array1D< int > &ind, Array2D< T > &submatrix)
Extracts from 'matrix' rows corresponding to indices 'ind' and returns them in 'submatrix' (double or...
Definition: arraytools.cpp:476
Array2D< double > dotdivide(Array2D< double > &A, Array2D< double > &B)
Returns the elementwise division of two 2D Arrays.
Definition: arraytools.cpp:1945
Array2D< double > mtxdel(Array2D< double > &, int index, int dim)
Deletes matrix columns or rows. Index specifies which column or row and dim = 1 deletes column,...
Definition: arraytools.cpp:1621
void prodAlphaMatVec(Array2D< double > &A, Array1D< double > &x, double alpha, Array1D< double > &y)
Returns , where 'A' is a 2D array, 'x' is 1D array of size and 'alpha' is a scalar....
Definition: arraytools.cpp:957
Array1D< double > scale(Array1D< double > &, double)
Returns 1D Arrays scaled by a double.
Definition: arraytools.cpp:1828
double select_kth(int k, Array1D< double > &arr)
Select the k-th smallest element of an array arr.
Definition: arraytools.cpp:1449
Array1D< double > add(Array1D< double > &, Array1D< double > &)
Add two 1D Arrays and returns sum (must be of the same shape)
Definition: arraytools.cpp:1653
void addVecAlphaVecPow(Array1D< double > &x, double alpha, Array1D< double > &y, int ip)
Implements , where 'x' and 'y' are 1D arrays with elements.
Definition: arraytools.cpp:1088
void flatten(Array2D< double > &arr_2, Array1D< double > &arr_1)
Unfold/flatten a 2d array into a 1d array (double format)
Definition: arraytools.cpp:299
void transpose(Array2D< T > &x, Array2D< T > &xt)
Transpose a 2d double or int array x and return the result in xt.
Definition: arraytools.cpp:279
double dot(Array1D< double > &, Array1D< double > &)
Returns the dot product of two 1D Arrays (must be of the same length)
Definition: arraytools.cpp:2024
void generate_multigrid(Array2D< T > &multigrid, Array2D< T > &grid)
Generates multigrid as a cartesian product of each column of grid.
Definition: arraytools.cpp:106
Definition: Array1D.h:472
Definition: Array1D.h:262
Stores data of any type T in a 1D array.
Definition: Array1D.h:61
Stores data of any type T in a 2D array.
Definition: Array2D.h:60
static double x1[]
Definition: gkpclib.cpp:36