Ensign
Low-rank framework for multi-core CPUs and GPUs
Loading...
Searching...
No Matches
tensor.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <lr/lr.hpp>
9
10namespace Ensign {
11
12
14namespace Tensor {
15
16template <size_t m, size_t d, class T>
18{
19 std::array<Index, d> shape{input.shape()};
20 std::array<Index, d - 1> cols_shape, vec_index_cols;
21 remove_element(std::begin(shape), std::end(shape), std::begin(cols_shape), m);
22 std::vector<Index> vec_index(d, 0);
23 Index i, j;
24
25 assert(shape[m] == output.shape()[1] && prod(cols_shape) == output.shape()[0]);
26
27 for (auto const& el : input) {
28 i = vec_index[m];
29 remove_element(std::begin(vec_index), std::end(vec_index),
30 std::begin(vec_index_cols), m);
31 j = IndexFunction::vec_index_to_comb_index(std::begin(vec_index_cols),
32 std::end(vec_index_cols),
33 std::begin(cols_shape));
34 output(j, i) = el;
35 IndexFunction::incr_vec_index(std::begin(shape), std::begin(vec_index),
36 std::end(vec_index));
37 }
38}
39
40template <>
43template <>
46template <>
49
50template <size_t m, size_t d, class T>
52{
53 std::array<Index, d> shape{output.shape()};
54 std::array<Index, d - 1> cols_shape, vec_index_cols;
55 remove_element(std::begin(shape), std::end(shape), std::begin(cols_shape), m);
56 std::vector<Index> vec_index(d, 0);
57 Index i, j;
58
59 assert(shape[m] == input.shape()[1] && prod(cols_shape) == input.shape()[0]);
60
61 for (auto& el : output) {
62 i = vec_index[m];
63 remove_element(std::begin(vec_index), std::end(vec_index),
64 std::begin(vec_index_cols), m);
65 j = IndexFunction::vec_index_to_comb_index(std::begin(vec_index_cols),
66 std::end(vec_index_cols),
67 std::begin(cols_shape));
68 el = input(j, i);
69 IndexFunction::incr_vec_index(std::begin(shape), std::begin(vec_index),
70 std::end(vec_index));
71 }
72}
73
74template <>
77template <>
80template <>
83
84template <class T>
86 const Index n_basisfunctions, const T weight,
87 const Ensign::Matrix::blas_ops& blas)
88{
89 Index rows = input.shape()[0];
90 Index cols = input.shape()[1];
91
92 assert(n_basisfunctions <= cols);
93
94 std::default_random_engine generator(1234);
95 std::normal_distribution<double> distribution(0.0, 1.0);
96
97 for (Index k = n_basisfunctions; k < cols; ++k) {
98 for (Index i = 0; i < rows; ++i) {
99 input(i, k) = distribution(generator);
100 }
101 }
102
103 multi_array<T, 2> R({cols, cols});
104
105 Ensign::orthogonalize gs(&blas);
106 gs(input, R, weight);
107
108 for (Index j = n_basisfunctions; j < cols; ++j) {
109 for (Index i = 0; i < cols; ++i) {
110 R(i, j) = T(0.0);
111 }
112 }
113 return R;
114}
115
116} // namespace Tensor
117
118}
ptrdiff_t Index
Definition common.hpp:83
Provides two-factor (i.e. matrix) low-rank types and their operations.
Index vec_index_to_comb_index(InputIt first, InputIt last, InputItInt first_int)
Definition index.hpp:19
void incr_vec_index(InputIt first, OutputIt d_first, OutputIt d_last)
Definition index.hpp:43
void tensorize(const multi_array< T, 2 > &input, multi_array< T, d > &output)
Definition tensor.hpp:51
void matricize(const multi_array< T, d > &input, multi_array< T, 2 > &output)
Definition tensor.hpp:17
void tensorize< 2, 3, double >(const multi_array< double, 2 > &input, multi_array< double, 3 > &output)
void matricize< 2, 3, double >(const multi_array< double, 3 > &input, multi_array< double, 2 > &output)
void tensorize< 1, 3, double >(const multi_array< double, 2 > &input, multi_array< double, 3 > &output)
void matricize< 1, 3, double >(const multi_array< double, 3 > &input, multi_array< double, 2 > &output)
void matricize< 0, 3, double >(const multi_array< double, 3 > &input, multi_array< double, 2 > &output)
void tensorize< 0, 3, double >(const multi_array< double, 2 > &input, multi_array< double, 3 > &output)
Core namespace for Ensign's numerical algorithms and data structures.
Definition common.hpp:96
void remove_element(InputIt first, InputIt last, OutputIt d_first, const Index idx)
Definition utility.hpp:95
T prod(array< T, d > a)
Definition utility.hpp:14
Provides matrix and vector operations with optional GPU execution.
Definition matrix.hpp:47
Dense d-dimensional array stored contiguously in column-major order (i.e.
Definition storage.hpp:24
array< Index, d > shape() const
Definition storage.hpp:244
Orthogonalizes Q and returns the corresponding upper triangular matrix R.
Definition lr.hpp:164