basic.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Eduard Valeyev
19  * Department of Chemistry, Virginia Tech
20  *
21  * util.h
22  * May 20, 2013
23  *
24  */
25 
26 #ifndef TILEDARRAY_MATH_LINALG_BASIC_H__INCLUDED
27 #define TILEDARRAY_MATH_LINALG_BASIC_H__INCLUDED
28 
29 #include "TiledArray/dist_array.h"
31 
33 
34 // freestanding adaptors for DistArray needed by solvers like DIIS
35 
36 template <typename Tile, typename Policy>
38  const DistArray<Tile, Policy>& a2) {
40  a1(vars) = a1(vars) * a2(vars);
41 }
42 
43 template <typename Tile, typename Policy, typename S>
44 inline void scale(DistArray<Tile, Policy>& a, S scaling_factor) {
45  using numeric_type = typename DistArray<Tile, Policy>::numeric_type;
47  a(vars) = numeric_type(scaling_factor) * a(vars);
48 }
49 
50 template <typename Tile, typename Policy>
51 inline void zero(DistArray<Tile, Policy>& a) {
52  scale(a, 0);
53 }
54 
55 template <typename Tile, typename Policy, typename S>
56 inline void axpy(DistArray<Tile, Policy>& y, S alpha,
57  const DistArray<Tile, Policy>& x) {
58  using numeric_type = typename DistArray<Tile, Policy>::numeric_type;
60  y(vars) = y(vars) + numeric_type(alpha) * x(vars);
61 }
62 
63 } // namespace TiledArray::math::linalg
64 
65 namespace Eigen {
66 
67 // freestanding adaptors for Eigen::MatrixBase needed by solvers like DIIS
68 
69 template <typename Derived>
70 inline void vec_multiply(Eigen::MatrixBase<Derived>& a1,
71  const Eigen::MatrixBase<Derived>& a2) {
72  a1.array() *= a2.array();
73 }
74 
75 template <typename Derived, typename S>
76 inline void scale(Eigen::MatrixBase<Derived>& a, S scaling_factor) {
77  using numeric_type = typename Eigen::MatrixBase<Derived>::value_type;
78  a.array() *= numeric_type(scaling_factor);
79 }
80 
81 template <typename Derived>
82 inline void zero(Eigen::MatrixBase<Derived>& a) {
83  a = Derived::Zero(a.rows(), a.cols());
84 }
85 
86 template <typename Derived, typename S>
87 inline void axpy(Eigen::MatrixBase<Derived>& y, S alpha,
88  const Eigen::MatrixBase<Derived>& x) {
89  using numeric_type = typename Eigen::MatrixBase<Derived>::value_type;
90  y.array() += numeric_type(alpha) * x.array();
91 }
92 
93 template <typename Derived>
94 inline auto dot(const Eigen::MatrixBase<Derived>& l,
95  const Eigen::MatrixBase<Derived>& r) {
96  return l.adjoint().dot(r);
97 }
98 
99 template <typename Derived>
100 inline auto inner_product(const Eigen::MatrixBase<Derived>& l,
101  const Eigen::MatrixBase<Derived>& r) {
102  return l.dot(r);
103 }
104 
105 template <typename Derived>
106 inline auto norm2(const Eigen::MatrixBase<Derived>& m) {
107  return m.template lpNorm<2>();
108 }
109 
110 } // namespace Eigen
111 
112 #endif // TILEDARRAY_MATH_LINALG_BASIC_H__INCLUDED
std::string dummy_annotation(unsigned int n_outer_size, unsigned int n_inner_size=0)
Definition: annotation.h:35
Forward declarations.
Definition: foreach.h:34
detail::numeric_type< Tile >::type numeric_type
Definition: dist_array.h:69
auto dot(const Eigen::MatrixBase< Derived > &l, const Eigen::MatrixBase< Derived > &r)
Definition: basic.h:94
void zero(Eigen::MatrixBase< Derived > &a)
Definition: basic.h:82
void scale(DistArray< Tile, Policy > &a, S scaling_factor)
Definition: basic.h:44
auto rank(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1617
auto norm2(const Eigen::MatrixBase< Derived > &m)
Definition: basic.h:106
Forward declarations.
Definition: dist_array.h:57
void scale(Eigen::MatrixBase< Derived > &a, S scaling_factor)
Definition: basic.h:76
void vec_multiply(DistArray< Tile, Policy > &a1, const DistArray< Tile, Policy > &a2)
Definition: basic.h:37
void vec_multiply(Eigen::MatrixBase< Derived > &a1, const Eigen::MatrixBase< Derived > &a2)
Definition: basic.h:70
void axpy(Eigen::MatrixBase< Derived > &y, S alpha, const Eigen::MatrixBase< Derived > &x)
Definition: basic.h:87
void zero(DistArray< Tile, Policy > &a)
Definition: basic.h:51
void axpy(DistArray< Tile, Policy > &y, S alpha, const DistArray< Tile, Policy > &x)
Definition: basic.h:56
auto inner_product(const Eigen::MatrixBase< Derived > &l, const Eigen::MatrixBase< Derived > &r)
Definition: basic.h:100