UQTk: Uncertainty Quantification Toolkit 3.1.5
arraytools.h
Go to the documentation of this file.
1/* =====================================================================================
2
3 The UQ Toolkit (UQTk) version 3.1.5
4 Copyright (2024) NTESS
5 https://www.sandia.gov/UQToolkit/
6 https://github.com/sandialabs/UQTk
7
8 Copyright 2024 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 https://github.com/sandialabs/UQTk/discussions
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
41template <typename T> void array1Dto2D(Array1D<T>& arr_1d,Array2D<T>& arr);
42
44template <typename T> void array2Dto1D(Array2D<T>& arr_2d,Array1D<T>& arr);
45
47template <typename T> void paste(Array1D<T>& arr1,Array1D<T>& arr2,Array2D<T>& arr);
48
51template <typename T> void generate_multigrid(Array2D<T>& multigrid,Array2D<T>& grid);
52
55
62
67
69template <typename T> void transpose(Array2D<T> &x, Array2D<T> &xt);
70
72void flatten(Array2D<double>& arr_2, Array1D<double>& arr_1);
73
78
83
85void swap(Array1D<double>& arr,int i,int j);
86
88void swap(Array2D<double>& arr,int i,int j);
89
91double access(int nx, int ny, Array1D<double>& arr_1, int i, int j);
92
94double accessPythonHelper(int nx, int ny, Array1D<double>& arr_1, int i, int j);
95
97template <typename T> void getRow(Array2D<T> &arr2d, int k, Array1D<T> &arr1d);
98
100template <typename T> void getCol(Array2D<T> &arr2d, int k, Array1D<T> &arr1d);
101
103template <typename T> void addVal(int n, T *arr1d, T val) ;
104
106template <typename T> void addVal(Array1D<T> &arr1d, T val) ;
107
109template <typename T> void addVal(Array2D<T> &arr2d, T val) ;
110
112template <typename T> void subVector(Array1D<T> &vector, Array1D<int> &ind, Array1D<T> &subvector);
113
115template <typename T> void subMatrix_row(Array2D<T> &matrix, Array1D<int> &ind, Array2D<T> &submatrix);
116
118template <typename T> void subMatrix_col(Array2D<T> &matrix, Array1D<int> &ind, Array2D<T> &submatrix);
119
122template <typename T> void matPvec(Array2D<T> &matrix, const Array1D<T> &rc, T alpha, char *RC);
123
125template <typename T> T maxVal(const Array1D<T>& vector, int *indx) ;
126
129
135
137void shell_sort (int *a, int n) ;
139void shell_sort(Array1D<int>& array);
141void shell_sort(Array1D<double>& array);
143void shell_sort_col(Array2D<double>& array,int col,Array1D<int>& newInd, Array1D<int>& oldInd);
145void shell_sort_all(Array2D<double>& array,Array1D<int>& newInd, Array1D<int>& oldInd);
147void quicksort3(Array1D<double>& arr, int l, int r);
149void quicksort3(Array2D<double>& arr,int left, int right,int col);
151void quicksort3(Array2D<double>& arr,int left, int right);
152
160
163template <typename T> void find(Array1D<T> &theta, T lmbda, string type, Array1D<int> &indx) ;
164
167void prodAlphaMatVec (Array2D<double>& A, Array1D<double>& x, double alpha, Array1D<double>& y) ;
178void addVecAlphaVecPow(Array1D<double>& x, double alpha, Array1D<double>& y, int ip) ;
183
184
187template <typename T> void delRow(Array2D<T>& A, int irow) ;
188
191template <typename T> void delCol(Array2D<T> &A, int icol) ;
192
195template <typename T> void delCol(Array1D<T> &x, int icol) ;
196
211void paddMatColScal(Array2D<double>& A, Array1D<double>& x, double scal) ;
212
221
224int vecIsInArray(Array1D<int>& vec, Array2D<int>& array);
225
227double select_kth(int k, Array1D<double>& arr);
228
231double logdeterm(Array2D<double>& mat);
232
234double trace(Array2D<double>& mat);
235
238
240Array2D<double> diag(Array1D<double>& diagonal_array);
241/**********************************************************
242NEW ROUTINES - Kenny
243***********************************************************/
244
247
250
252Array2D<double> mtxdel(Array2D<double>&, int index, int dim);
253
256
259
262
265
268
271
274
277
280
283
285void scaleinplace(Array1D<double>&, double);
286
288void scaleinplace(Array1D<int>&, int);
289
291void scaleinplace(Array2D<double>&, double);
292
294void scaleinplace(Array2D<int>&, int);
295
298
301
304
307
309double norm(Array1D<double>&);
310
313
316
319
322
325
328
331
334
337
340
343
346
349
352
355
358
359
360//---------------------------------------------------------------------------------------
361#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:1025
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:2230
Array2D< double > MatTMat(Array2D< double > &A)
Returns , where 'A' is a 2D array.
Definition arraytools.cpp:1118
Array2D< double > Trans(Array2D< double > &)
Returns the transpose of a 2D Array.
Definition arraytools.cpp:2011
void merge(Array2D< double > &x, Array2D< double > &y, Array2D< double > &xy)
Merge 2d double arrays (vertical stack)
Definition arraytools.cpp:179
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:1624
Array2D< double > dotdivide(Array2D< double > &A, Array2D< double > &B)
Returns the elementwise division of two 2D Arrays.
Definition arraytools.cpp:1948
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:2162
double accessPythonHelper(int nx, int ny, Array1D< double > &arr_1, int i, int j)
Function written exclusively for Pybind11 to be able to load in the function access.
Definition arraytools.cpp:390
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:2107
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:2198
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:1316
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:928
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:2292
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:613
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:593
void delCol(Array2D< T > &A, int icol)
Deletes column 'icol' from 2D array 'A'.
Definition arraytools.cpp:1167
double logdeterm(Array2D< double > &mat)
Log-determinant of a real symmetric positive-definite matrix.
Definition arraytools.cpp:1494
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:530
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:1428
double dist_sq(Array1D< double > &x, Array1D< double > &y, Array1D< double > &w)
Weighted vector distance-squared.
Definition arraytools.cpp:1997
void printarray(Array1D< double > &)
Prints 1D double Array to screen (alternative to for loop using cout)
Definition arraytools.cpp:2323
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:1380
void paddMatCol(Array2D< double > &A, Array1D< double > &x)
Padds 2D array 'A' with the column 'x'.
Definition arraytools.cpp:1241
void delRow(Array2D< T > &A, int irow)
Deletes row 'irow' from 2D array 'A'.
Definition arraytools.cpp:1138
double norm(Array1D< double > &)
Returns norm of 1D Array (Euclidean)
Definition arraytools.cpp:1990
Array2D< double > diag(Array1D< double > &diagonal_array)
Returns a diagonal matrix with a given diagonal.
Definition arraytools.cpp:1590
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:412
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:734
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:1059
Array1D< double > add(Array1D< double > &, Array1D< double > &)
Add two 1D Arrays and returns sum (must be of the same shape)
Definition arraytools.cpp:1656
Array1D< double > copy(Array1D< double > &)
Returns a copy of 1D array.
Definition arraytools.cpp:1607
double prod_vecTmatvec(Array1D< double > &a, Array2D< double > &B, Array1D< double > &c)
Returns .
Definition arraytools.cpp:1106
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:780
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:505
void paddMatRow(Array2D< double > &A, Array1D< double > &x)
Padds 2D array 'A' with the row 'x'.
Definition arraytools.cpp:1217
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:397
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:862
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:994
void addinplace(Array2D< double > &x, Array2D< double > &y)
Add two 2D Arrays in place. Summation is returned as x.
Definition arraytools.cpp:1702
double trace(Array2D< double > &mat)
Trace of a matrix.
Definition arraytools.cpp:1536
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:1789
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:699
Array2D< double > dotmult(Array2D< double > &A, Array2D< double > &B)
Returns the elementwise multiplication of two 2D Arrays.
Definition arraytools.cpp:1906
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:457
void shell_sort(int *a, int n)
Sorts integer array.
Definition arraytools.cpp:634
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:425
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:576
Array1D< double > scale(Array1D< double > &, double)
Returns 1D Arrays scaled by a double.
Definition arraytools.cpp:1831
void scaleinplace(Array1D< double > &, double)
Multiply Array1D by double in place.
Definition arraytools.cpp:1859
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:2180
Array1D< double > subtract(Array1D< double > &, Array1D< double > &)
Returns subtraction of two 1D Arrays (must be of the same shape)
Definition arraytools.cpp:1745
bool is_equal(Array1D< int > &a, Array1D< int > &b)
Checks if two 1d int arrays are equal.
Definition arraytools.cpp:1342
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:1548
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:481
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:962
double select_kth(int k, Array1D< double > &arr)
Select the k-th smallest element of an array arr.
Definition arraytools.cpp:1452
Array2D< double > INV(Array2D< double > &A)
Returns the inverse of a square 2D Array.
Definition arraytools.cpp:2132
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:1093
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:2027
void generate_multigrid(Array2D< T > &multigrid, Array2D< T > &grid)
Generates multigrid as a cartesian product of each column of grid.
Definition arraytools.cpp:106
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