imate
C++/CUDA Reference
c_dense_affine_matrix_function.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3  * SPDX-License-Identifier: BSD-3-Clause
4  * SPDX-FileType: SOURCE
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the license found in the LICENSE.txt file in the root
8  * directory of this source tree.
9  */
10 
11 
12 // =======
13 // Headers
14 // =======
15 
17 #include <cassert> // assert
18 #include <cstddef> // NULL
19 
20 
21 // =============
22 // constructor 1
23 // =============
24 
27 
28 template <typename DataType>
30  const DataType* A_,
31  const FlagType A_is_row_major_,
32  const LongIndexType num_rows_,
33  const LongIndexType num_columns_):
34 
35  // Base class constructor
36  cAffineMatrixFunction<DataType>(num_rows_, num_columns_),
37 
38  // Initializer list
39  A(A_, num_rows_, num_columns_, A_is_row_major_)
40 {
41  // This constructor is called assuming B is identity
42  this->B_is_identity = true;
43 
44  // When B is identity, the eigenvalues of A+tB are known for any t
45  this->eigenvalue_relation_known = 1;
46 }
47 
48 
49 // =============
50 // constructor 2
51 // =============
52 
53 template <typename DataType>
55  const DataType* A_,
56  const FlagType A_is_row_major_,
57  const LongIndexType num_rows_,
58  const LongIndexType num_columns_,
59  const DataType* B_,
60  const FlagType B_is_row_major_):
61 
62  // Base class constructor
63  cAffineMatrixFunction<DataType>(num_rows_, num_columns_),
64 
65  // Initializer list
66  A(A_, num_rows_, num_columns_, A_is_row_major_),
67  B(B_, num_rows_, num_columns_, B_is_row_major_)
68 {
69  // Matrix B is assumed to be non-zero. Check if it is identity or generic
70  if (this->B.is_identity_matrix())
71  {
72  this->B_is_identity = true;
73  this->eigenvalue_relation_known = 1;
74  }
75 }
76 
77 
78 // ==========
79 // destructor
80 // ==========
81 
82 template <typename DataType>
84 {
85 }
86 
87 
88 // ===
89 // dot
90 // ===
91 
107 
108 template <typename DataType>
110  const DataType* vector,
111  DataType* product)
112 {
113  // Matrix A times vector
114  this->A.dot(vector, product);
115  LongIndexType min_vector_size;
116 
117  // Matrix B times vector to be added to the product
118  if (this->B_is_identity)
119  {
120  // Check parameter is set
121  assert((this->parameters != NULL) && "Parameter is not set.");
122 
123  // Find minimum of the number of rows and columns
124  min_vector_size = \
125  (this->num_rows < this->num_columns) ? \
126  this->num_rows : this->num_columns;
127 
128  // Adding input vector to product
129  this->_add_scaled_vector(vector, min_vector_size,
130  this->parameters[0], product);
131  }
132  else
133  {
134  // Check parameter is set
135  assert((this->parameters != NULL) && "Parameter is not set.");
136 
137  // Adding parameter times B times input vector to the product
138  this->B.dot_plus(vector, this->parameters[0], product);
139  }
140 }
141 
142 
143 // =============
144 // transpose dot
145 // =============
146 
163 
164 template <typename DataType>
166  const DataType* vector,
167  DataType* product)
168 {
169  // Matrix A times vector
170  this->A.transpose_dot(vector, product);
171  LongIndexType min_vector_size;
172 
173  // Matrix B times vector to be added to the product
174  if (this->B_is_identity)
175  {
176  // Check parameter is set
177  assert((this->parameters != NULL) && "Parameter is not set.");
178 
179  // Find minimum of the number of rows and columns
180  min_vector_size = \
181  (this->num_rows < this->num_columns) ? \
182  this->num_rows : this->num_columns;
183 
184  // Adding input vector to product
185  this->_add_scaled_vector(vector, min_vector_size,
186  this->parameters[0], product);
187  }
188  else
189  {
190  // Check parameter is set
191  assert((this->parameters != NULL) && "Parameter is not set.");
192 
193  // Adding "parameter * B * input vector" to the product
194  this->B.transpose_dot_plus(vector, this->parameters[0], product);
195  }
196 }
197 
198 
199 // ===============================
200 // Explicit template instantiation
201 // ===============================
202 
203 template class cDenseAffineMatrixFunction<float>;
Base class for affine matrix functions of one parameter.
cDenseAffineMatrixFunction(const DataType *A_, const FlagType A_is_row_major_, const LongIndexType num_rows_, const LongIndexType num_colums_)
Constructor. Matrix B is assumed to be the identity matrix.
virtual void dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
virtual void transpose_dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
FlagType eigenvalue_relation_known
int LongIndexType
Definition: types.h:60
int FlagType
Definition: types.h:68