Developer documentation
Version 3.0.3-105-gd3941f44
mat2vec.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __connectome_mat2vec_h__
18#define __connectome_mat2vec_h__
19
20
21#include <stdint.h>
22
23#include "types.h"
24
25#include "connectome/connectome.h"
26
27
28
29
30namespace MR {
31 namespace Connectome {
32
33
34
35 class Mat2Vec
37
38 public:
39 Mat2Vec (const node_t i) : dim (i) { }
40
41 uint64_t operator() (const node_t i, const node_t j) const
42 {
43 assert (i < dim);
44 assert (j < dim);
45 const uint64_t i64 (i);
46 const uint64_t j64 (j);
47 if (i < j)
48 return j64 + (uint64_t(dim) * i64) - ((i64 * (i64+1)) / 2);
49 else
50 return i64 + (uint64_t(dim) * j64) - ((j64 * (j64+1)) / 2);
51 }
52
53 std::pair<node_t, node_t> operator() (const uint64_t i) const
54 {
55 static const uint64_t temp = 2*dim+1;
56 static const uint64_t temp_sq = temp * temp;
57 const uint64_t row = std::floor ((temp - std::sqrt(temp_sq - (8*i))) / 2);
58 const uint64_t col = i - (uint64_t(dim)*row) + ((row * (row+1))/2);
59 assert (row < dim);
60 assert (col < dim);
61 return std::make_pair (node_t(row), node_t(col));
62 }
63
64 node_t mat_size() const { return dim; }
65 uint64_t vec_size() const { return (uint64_t(dim) * (uint64_t(dim)+1) / 2); }
66
67 // Complete Matrix->Vector and Vector->Matrix conversion
68 template <class MatType, class VecType>
69 VecType& M2V (const MatType&, VecType&) const;
70 template <class VecType, class MatType>
71 MatType& V2M (const VecType&, MatType&) const;
72
73 // Convenience functions to avoid having to pre-define the output class
74 template <class MatType>
75 vector_type M2V (const MatType&) const;
76 template <class VecType>
77 matrix_type V2M (const VecType&) const;
78
79
80 private:
81 const node_t dim;
82
83 };
84
85
86
87 template <class MatType, class VecType>
88 VecType& Mat2Vec::M2V (const MatType& m, VecType& v) const
89 {
90 assert (m.rows() == m.cols());
91 assert (m.rows() == dim);
92 v.resize (vec_size());
93 for (size_t index = 0; index != vec_size(); ++index) {
94 const std::pair<node_t, node_t> row_col = (*this) (index);
95 v[index] = m (row_col.first, row_col.second);
96 }
97 return v;
98 }
99
100 template <class VecType, class MatType>
101 MatType& Mat2Vec::V2M (const VecType& v, MatType& m) const
102 {
103 assert (size_t (v.size()) == vec_size());
104 m.resize (dim, dim);
105 for (node_t row = 0; row != dim; ++row) {
106 for (node_t col = 0; col != dim; ++col)
107 m (row, col) = v[(*this) (row, col)];
108 }
109 return m;
110 }
111
112 template <class MatType>
114 {
115 vector_type v;
116 M2V (m, v);
117 return v;
118 }
119
120 template <class VecType>
122 {
123 matrix_type m;
124 V2M (v, m);
125 return m;
126 }
127
128
129
130 }
131}
132
133
134#endif
135
VecType & M2V(const MatType &, VecType &) const
Definition: mat2vec.h:88
MatType & V2M(const VecType &, MatType &) const
Definition: mat2vec.h:101
uint64_t operator()(const node_t i, const node_t j) const
Definition: mat2vec.h:41
node_t mat_size() const
Definition: mat2vec.h:64
Mat2Vec(const node_t i)
Definition: mat2vec.h:39
uint64_t vec_size() const
Definition: mat2vec.h:65
constexpr I floor(const T x)
template function with cast to different type
Definition: math.h:75
#define NOMEMALIGN
Definition: memory.h:22
uint32_t node_t
Definition: connectome.h:34
Eigen::Array< value_type, Eigen::Dynamic, Eigen::Dynamic > matrix_type
Definition: connectome.h:37
Eigen::Array< value_type, Eigen::Dynamic, 1 > vector_type
Definition: connectome.h:38
Eigen::Matrix< default_type, 3, 1 > VecType
Definition: search.h:57
Eigen::Matrix< default_type, 3, 3 > MatType
Definition: search.h:56
Definition: base.h:24
size_t index