Page MenuHomec4science

legacy_types.cpp
No OneTemporary

File Metadata

Created
Tue, Apr 30, 20:39

legacy_types.cpp

/**
* @file
* @section LICENSE
*
* Copyright (©) 2016-2021 EPFL (École Polytechnique Fédérale de Lausanne),
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#include "legacy_types.hh"
/* -------------------------------------------------------------------------- */
namespace tamaas {
namespace legacy {
/* -------------------------------------------------------------------------- */
/* VectorProxy definitions */
/* -------------------------------------------------------------------------- */
template <typename T>
VectorProxy<T>::VectorProxy(T* mem, UInt n) : Proxy<T, 1>() {
this->data.wrapMemory(mem, n);
this->n[0] = n;
this->computeStrides();
}
/* -------------------------------------------------------------------------- */
template <typename T>
void VectorProxy<T>::mul(const MatrixProxy<T>& mat, const VectorProxy<T>& vec) {
auto n_mat = mat.sizes();
auto n_vec = vec.sizes();
TAMAAS_ASSERT(n_mat[1] == n_vec[0], "matrix and vector sizes do not match");
TAMAAS_ASSERT(n_mat[0] == this->n[0],
"matrix and result vector sizes do not match");
// No parallelism here
for (UInt i = 0; i < n_mat[0]; ++i) {
for (UInt j = 0; j < n_vec[0]; ++j) {
(*this)(i) += mat(i, j) * vec(j);
}
}
}
/* -------------------------------------------------------------------------- */
template <typename T>
T VectorProxy<T>::dot(const VectorProxy<T>& vec) {
TAMAAS_ASSERT(this->n[0] == vec.sizes()[0], "vector sizes do not match");
T res = 0;
// No parallelism here
for (UInt i = 0; i < this->n[0]; i++)
res += (*this)(i)*vec(i);
return res;
}
/* -------------------------------------------------------------------------- */
template <typename T>
T VectorProxy<T>::l2norm() const {
T res = 0;
// No parallelism here
for (UInt i = 0; i < this->n[0]; i++)
res += (*this)(i) * (*this)(i);
return sqrt(res);
}
/* -------------------------------------------------------------------------- */
/* MatrixProxy definitions */
/* -------------------------------------------------------------------------- */
template <typename T>
MatrixProxy<T>::MatrixProxy(T* mem, UInt n, UInt m) : Proxy<T, 2>() {
this->data.wrapMemory(mem, n * m);
this->n[0] = n;
this->n[1] = m;
this->computeStrides();
}
/* -------------------------------------------------------------------------- */
#define TYPE_INSTANCIATE(type) \
template class VectorProxy<type>; \
template class MatrixProxy<type>
TYPE_INSTANCIATE(Real);
TYPE_INSTANCIATE(UInt);
TYPE_INSTANCIATE(Complex);
TYPE_INSTANCIATE(Int);
#undef TYPE_INSTANCIATE
} // namespace legacy
} // namespace tamaas

Event Timeline