Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_SerialDenseHelpers.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef _TEUCHOS_SERIALDENSEHELPERS_HPP_
11#define _TEUCHOS_SERIALDENSEHELPERS_HPP_
12
20#include "Teuchos_Assert.hpp"
25
26#include "Teuchos_CommHelpers.hpp"
27#include "Teuchos_DefaultComm.hpp"
28#include "Teuchos_DefaultSerialComm.hpp"
29
30namespace Teuchos {
31
43template<typename OrdinalType, typename ScalarType>
46{
47 // Local variables.
48 // Note: dimemensions of W are obtained so we can compute W^T*A*W for either cases.
49 OrdinalType A_nrowcols = A.numRows(); // A is a symmetric matrix and is assumed square.
50 OrdinalType B_nrowcols = (ETranspChar[transw]!='N') ? W.numCols() : W.numRows();
51 OrdinalType W_nrows = (ETranspChar[transw]!='N') ? W.numRows() : W.numCols();
52 OrdinalType W_ncols = (ETranspChar[transw]!='N') ? W.numCols() : W.numRows();
53
54 bool isBUpper = B.upper();
55
56 // Check for consistent dimensions.
57 TEUCHOS_TEST_FOR_EXCEPTION( B_nrowcols != B.numRows(), std::out_of_range,
58 "Teuchos::symMatTripleProduct<>() : "
59 "Num Rows/Cols B (" << B.numRows() << ") inconsistent with W ("<< B_nrowcols << ")");
60 TEUCHOS_TEST_FOR_EXCEPTION( A_nrowcols != W_nrows, std::out_of_range,
61 "Teuchos::symMatTripleProduct<>() : "
62 "Num Rows/Cols A (" << A_nrowcols << ") inconsistent with W ("<< W_nrows << ")");
63
64 // Scale by zero, initialized B to zeros and return.
66 {
67 B.putScalar();
68 return;
69 }
70
71 // Workspace.
73
74 // BLAS class.
78
79 // Separate two cases because BLAS only supports symmetric matrix-matrix multply w/o transposes.
80 if (ETranspChar[transw]!='N') {
81 // Size AW to compute A*W
82 AW.shapeUninitialized(A_nrowcols,W_ncols);
83
84 // A*W
86
87 // B = W^T*A*W
88 if (isBUpper) {
89 for (OrdinalType j=0; j<B_nrowcols; ++j)
90 blas.GEMV( transw, W_nrows, j+1, one, W.values(), W.stride(), AW[j], 1, zero, &B(0,j), 1 );
91 }
92 else {
93 for (OrdinalType j=0; j<B_nrowcols; ++j)
94 blas.GEMV( transw, W_nrows, B_nrowcols-j, one, W[j], W.stride(), AW[j], 1, zero, &B(j,j), 1 );
95 }
96 }
97 else {
98 // Size AW to compute W*A
99 AW.shapeUninitialized(W_ncols, A_nrowcols);
100
101 // W*A
103
104 // B = W*A*W^T
105 if (isBUpper) {
106 for (OrdinalType j=0; j<B_nrowcols; ++j)
107 for (OrdinalType i=0; i<=j; ++i)
108 blas.GEMV( transw, 1, A_nrowcols, one, &AW(i,0), AW.stride(), &W(j,0), W.stride(), zero, &B(i,j), 1 );
109 }
110 else {
111 for (OrdinalType j=0; j<B_nrowcols; ++j)
112 for (OrdinalType i=j; i<B_nrowcols; ++i)
113 blas.GEMV( transw, 1, A_nrowcols, one, &AW(i,0), AW.stride(), &W(j,0), W.stride(), zero, &B(i,j), 1 );
114 }
115 }
116
117 return;
118}
119
129template<typename OrdinalType, typename ScalarType>
135
145template<typename OrdinalType, typename ScalarType>
147 const OrdinalType col,
149{
150 if (v.length() != A.numRows()) return false;
151
152 std::copy(v.values(),v.values()+v.length(),A[col]);
153
154 return true;
155}
156
162template <typename OrdinalType, typename ScalarType>
164{
166
167#ifdef HAVE_MPI
168 int mpiStarted = 0;
170 if (mpiStarted)
172 else
174#else
176#endif
177
178 const OrdinalType procRank = rank(*comm);
179
180 // Construct a separate serial dense matrix and synchronize it to get around
181 // input matrices that are subviews of a larger matrix.
183 if (procRank == 0)
184 newMatrix.random();
185 else
187
188 broadcast(*comm, 0, A.numRows()*A.numCols(), newMatrix.values());
189
190 // Assign the synchronized matrix to the input.
191 A.assign( newMatrix );
192}
193
194
205template<typename OrdinalType, typename ScalarType>
208 const OrdinalType kl, const OrdinalType ku,
209 const bool factorFormat)
210{
211 OrdinalType m = A->numRows();
212 OrdinalType n = A->numCols();
213
214 // Check that the new matrix is consistent.
215 TEUCHOS_TEST_FOR_EXCEPTION(A->values()==0, std::invalid_argument,
216 "SerialBandDenseSolver<T>::generalToBanded: A is an empty SerialDenseMatrix<T>!");
217 TEUCHOS_TEST_FOR_EXCEPTION(kl<0 || kl>m, std::invalid_argument,
218 "SerialBandDenseSolver<T>::generalToBanded: The lower bandwidth kl is invalid!");
219 TEUCHOS_TEST_FOR_EXCEPTION(ku<0 || ku>n, std::invalid_argument,
220 "SerialBandDenseSolver<T>::generalToBanded: The upper bandwidth ku is invalid!");
221
225
226 for (OrdinalType j = 0; j < n; j++) {
227 for (OrdinalType i=TEUCHOS_MAX(0,j-ku); i<=TEUCHOS_MIN(m-1,j+kl); i++) {
228 (*AB)(i,j) = (*A)(i,j);
229 }
230 }
231 return(AB);
232}
233
241template<typename OrdinalType, typename ScalarType>
244{
245
246 OrdinalType m = AB->numRows();
247 OrdinalType n = AB->numCols();
248 OrdinalType kl = AB->lowerBandwidth();
249 OrdinalType ku = AB->upperBandwidth();
250
251 // Check that the new matrix is consistent.
252 TEUCHOS_TEST_FOR_EXCEPTION(AB->values()==0, std::invalid_argument,
253 "SerialBandDenseSolver<T>::bandedToGeneral: AB is an empty SerialBandDenseMatrix<T>!");
254
256 for (OrdinalType j = 0; j < n; j++) {
257 for (OrdinalType i=TEUCHOS_MAX(0,j-ku); i<=TEUCHOS_MIN(m-1,j+kl); i++) {
258 (*A)(i,j) = (*AB)(i,j);
259 }
260 }
261 return(A);
262}
263
264} // namespace Teuchos
265
266#endif /* _TEUCHOS_SERIALDENSEHELPERS_HPP_ */
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::DataAccess Mode enumerable type.
Defines basic traits for the scalar field type.
Templated serial dense matrix class.
Templated serial dense matrix class.
Templated serial dense vector class.
Templated serial, dense, symmetric matrix class.
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
Smart reference counting pointer class for automatic garbage collection.
Teuchos::RCP< SerialBandDenseMatrix< OrdinalType, ScalarType > > generalToBanded(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A, const OrdinalType kl, const OrdinalType ku, const bool factorFormat)
A templated, non-member, helper function for converting a SerialDenseMatrix to a SerialBandDenseMatri...
Teuchos::RCP< SerialDenseMatrix< OrdinalType, ScalarType > > bandedToGeneral(const RCP< SerialBandDenseMatrix< OrdinalType, ScalarType > > &AB)
A templated, non-member, helper function for converting a SerialBandDenseMatrix to a SerialDenseMatri...
void randomSyncedMatrix(Teuchos::SerialDenseMatrix< OrdinalType, ScalarType > &A)
A templated, non-member, helper function for generating a random SerialDenseMatrix that is synchroniz...
bool setCol(const SerialDenseVector< OrdinalType, ScalarType > &v, const OrdinalType col, SerialDenseMatrix< OrdinalType, ScalarType > &A)
A templated, non-member, helper function for setting a SerialDenseMatrix column using a SerialDenseVe...
SerialDenseVector< OrdinalType, ScalarType > getCol(DataAccess CV, SerialDenseMatrix< OrdinalType, ScalarType > &A, const OrdinalType col)
A templated, non-member, helper function for viewing or copying a column of a SerialDenseMatrix as a ...
void symMatTripleProduct(ETransp transw, const ScalarType alpha, const SerialSymDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &W, SerialSymDenseMatrix< OrdinalType, ScalarType > &B)
A templated, non-member, helper function for computing the matrix triple-product: B = alpha*W^T*A*W o...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
This structure defines some basic traits for a scalar field type.
static T one()
Returns representation of one for this scalar type.
static T zero()
Returns representation of zero for this scalar type.