SOPT
Sparse OPTimisation
gradient_operator.h
Go to the documentation of this file.
1 #ifndef SOPT_GRADIENT_OPERATOR_H
2 #define SOPT_GRADIENT_OPERATOR_H
3 
4 #include "sopt/config.h"
6 #include "sopt/logging.h"
7 
10 template <typename T>
12  if (x.size() < 2) return Vector<T>::Zero(x.size());
13  Vector<T> output = Vector<T>::Zero(x.size());
14  output.segment(0, x.size() - 1) = x.segment(1, x.size() - 1) - x.segment(0, x.size() - 1);
15  return output;
16 }
18 template <typename T>
20  Vector<T> output = Vector<T>::Zero(x.size());
21  output.segment(0, x.size() - 1) -= x.segment(0, x.size() - 1);
22  if (output.size() > 2) output.segment(1, x.size() - 1) = x.segment(0, x.size() - 1);
23  return output;
24 }
26 template <typename T>
27 Vector<T> diff2d(const Vector<T> &x, const t_int rows, const t_int cols) {
28  Matrix<T> output = Matrix<T>::Zero(rows, 2 * cols);
29  const Matrix<T> &input_image = Matrix<T>::Map(x.data(), rows, cols);
30  for (Eigen::Index i(0); i < rows; i++)
31  output.block(0, 0, rows, cols).row(i) = diff<T>(input_image.row(i));
32  for (Eigen::Index i(0); i < cols; i++)
33  output.block(0, cols, rows, cols).col(i) = diff<T>(input_image.col(i));
34  return Vector<T>::Map(output.data(), output.size());
35 }
37 template <typename T>
39  const Matrix<T> &input_image = Matrix<T>::Map(x.data(), rows, 2 * cols);
41  for (Eigen::Index i(0); i < rows; i++)
42  output.row(i) += diff_adjoint<T>(input_image.block(0, 0, rows, cols).row(i));
43  for (Eigen::Index i(0); i < cols; i++)
44  output.col(i) += diff_adjoint<T>(input_image.block(0, cols, rows, cols).col(i));
45  return Vector<T>::Map(output.data(), output.size());
46 }
47 template <typename T>
50  [rows, cols](Vector<T> &out, Vector<T> const &x) {
51  assert(x.size() == rows * cols * 2);
52  out = diff2d_adjoint(x, rows, cols) / 2.;
53  assert(out.size() == rows * cols);
54  },
55  {{0, 1, static_cast<t_int>(rows * cols)}},
56  [rows, cols](Vector<T> &out, Vector<T> const &x) {
57  assert(x.size() == rows * cols);
58  out = diff2d(x, rows, cols) / 2.;
59  assert(out.size() == 2 * rows * cols);
60  },
61  {{0, 1, static_cast<t_int>(2 * rows * cols)}});
62 }
63 } // namespace sopt::gradient_operator
64 
65 #endif
Joins together direct and indirect operators.
t_uint rows
t_uint cols
Vector< T > diff2d(const Vector< T > &x, const t_int rows, const t_int cols)
Numerical derivative of 2d image.
Vector< T > diff_adjoint(const Vector< T > &x)
Numerical derivative adjoint of 1d vector.
Vector< T > diff(const Vector< T > &x)
Numerical derivative of 1d vector.
LinearTransform< Vector< T > > gradient_operator(const t_int rows, const t_int cols)
Vector< T > diff2d_adjoint(const Vector< T > &x, const t_int rows, const t_int cols)
Numerical derivative adjoint of 2d image.
int t_int
Root of the type hierarchy for signed integers.
Definition: types.h:13
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A matrix of a given type.
Definition: types.h:29