util.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_UTIL_H__INCLUDED
27 #define TILEDARRAY_MATH_LINALG_UTIL_H__INCLUDED
28 
29 #include <TiledArray/config.h>
30 #include "TiledArray/dist_array.h"
32 
34 
35 template<class A>
36 struct array_traits {
37  using scalar_type = typename A::scalar_type;
38  using numeric_type = typename A::numeric_type;
39  static const bool complex = !std::is_same_v<scalar_type, numeric_type>;
40  static_assert(std::is_same_v<numeric_type, typename A::element_type>,
41  "TA::linalg is only usable with a DistArray of scalar types");
42 };
43 
44 
45 template <typename Tile, typename Policy>
47  auto A_repl = A;
48  A_repl.make_replicated();
49  return array_to_eigen<Tile, Policy, Eigen::ColMajor>(A_repl);
50 }
51 
52 template <typename ContiguousTensor,
53  typename = std::enable_if_t<
54  TiledArray::detail::is_contiguous_tensor_v<ContiguousTensor>>>
55 auto make_matrix(const ContiguousTensor& A) {
57  static_assert(
58  std::is_same_v<numeric_type, typename ContiguousTensor::value_type>,
59  "TA::lapack::{cholesky*} are only usable with a ContiguousTensor of "
60  "scalar types");
61  TA_ASSERT(A.range().rank() == 1 || A.range().rank() == 2);
62  using colmajor_matrix_type = Eigen::Matrix<numeric_type, Eigen::Dynamic,
63  Eigen::Dynamic, Eigen::ColMajor>;
64  colmajor_matrix_type result(A.range().extent(0),
65  A.range().rank() == 2 ? A.range().extent(1) : 1);
66  constexpr const auto layout =
68  if (layout == TiledArray::OrdinalType::RowMajor) {
69  using rowmajor_matrix_type = Eigen::Matrix<numeric_type, Eigen::Dynamic,
70  Eigen::Dynamic, Eigen::RowMajor>;
71  auto result_map = Eigen::Map<const rowmajor_matrix_type>(
72  A.data(), result.rows(), result.cols());
73  result = result_map;
74  } else if constexpr (layout == TiledArray::OrdinalType::ColMajor) {
75  using rowmajor_matrix_type = Eigen::Matrix<numeric_type, Eigen::Dynamic,
76  Eigen::Dynamic, Eigen::ColMajor>;
77  auto result_map = Eigen::Map<const rowmajor_matrix_type>(
78  A.data(), result.rows(), result.cols());
79  result = result_map;
80  } else
81  abort();
82  return result;
83 }
84 
85 template <typename ContiguousTensor, typename Scalar, int ... Options,
86  typename = std::enable_if_t<
87  TiledArray::detail::is_contiguous_tensor_v<ContiguousTensor>>>
88 auto make_array(const Eigen::Matrix<Scalar, Options...>& A,
89  typename ContiguousTensor::range_type range = {}) {
91  static_assert(
92  std::is_same_v<numeric_type, typename ContiguousTensor::value_type>,
93  "TA::math::linalg is only usable with a ContiguousTensor of scalar types"
94  );
95  using range_type = typename ContiguousTensor::range_type;
96  if (range.rank() == 0)
97  range = range_type(A.rows(), A.cols());
98  else
99  TA_ASSERT(A.rows() * A.cols() == range.volume());
100  ContiguousTensor result(range);
101  auto result_map = eigen_map(result, A.rows(), A.cols());
102  result_map = A;
103  return result;
104 }
105 
106 template <typename Derived>
107 void zero_out_upper_triangle(Eigen::MatrixBase<Derived>& A) {
108  A.template triangularView<Eigen::StrictlyUpper>().setZero();
109 }
110 
111 } // namespace TiledArray::math::linalg
112 
113 #endif // TILEDARRAY_MATH_LINALG_UTIL_H__INCLUDED
typename TiledArray::detail::numeric_type< T >::type numeric_t
numeric_t<T> is an alias for numeric_type<T>::type
Definition: type_traits.h:730
Eigen::Map< const Eigen::Matrix< typename T::value_type, Eigen::Dynamic, Eigen::Dynamic, Storage >, Eigen::AutoAlign > eigen_map(const T &tensor, const std::size_t m, const std::size_t n)
Construct a const Eigen::Map object for a given Tensor object.
Definition: eigen.h:77
typename A::scalar_type scalar_type
Definition: util.h:37
ordinal trait specifies properties of the ordinal
Definition: type_traits.h:330
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
void zero_out_upper_triangle(Eigen::MatrixBase< Derived > &A)
Definition: util.h:107
Forward declarations.
Definition: dist_array.h:57
typename A::numeric_type numeric_type
Definition: util.h:38
auto make_array(const Eigen::Matrix< Scalar, Options... > &A, typename ContiguousTensor::range_type range={})
Definition: util.h:88
::Eigen::Matrix< T, ::Eigen::Dynamic, ::Eigen::Dynamic, Options > Matrix
Definition: blas.h:63
auto make_matrix(const DistArray< Tile, Policy > &A)
Definition: util.h:46