DGtal  1.4.2
LinearOperator.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file LinearOperator.ih
19  * @author Pierre Gueth (\c pierre.gueth@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2014/03/26
23  *
24  * Implementation of inline methods defined in LinearOperator.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // ----------------------- Standard services ------------------------------
35 
36 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
37 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::LinearOperator(ConstAlias<Calculus> _calculus)
38  : myContainer((&_calculus)->kFormLength(order_out, duality_out), (&_calculus)->kFormLength(order_in, duality_in)), myCalculus(&_calculus)
39 {
40  clear();
41 }
42 
43 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
44 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::LinearOperator(ConstAlias<Calculus> _calculus, const Container& _container)
45  : myContainer(_container), myCalculus(&_calculus)
46 {
47  ASSERT( myCalculus->kFormLength(order_out, duality_out) == myContainer.rows() );
48  ASSERT( myCalculus->kFormLength(order_in, duality_in) == myContainer.cols() );
49 }
50 
51 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
52 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>&
53 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::operator=(const LinearOperator& _operator)
54 {
55  myContainer = _operator.myContainer;
56  myCalculus = _operator.myCalculus;
57  return *this;
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 // Interface - public :
62 
63 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
64 void
65 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::clear()
66 {
67  myContainer.clear();
68 }
69 
70 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
71 void
72 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::selfDisplay(std::ostream& os) const
73 {
74  os << "[";
75  os << duality_in << " " << order_in << "-form => " << duality_out << " " << order_out << "-form";
76  os << " ";
77  os << "(" << myContainer.cols() << " => " << myContainer.rows() << ")";
78  os << "]";
79 }
80 
81 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
82 DGtal::LinearOperator<Calculus, order_out, duality_out, order_in, duality_in>
83 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::transpose() const
84 {
85  return TransposedLinearOperator(*myCalculus, myContainer.transpose());
86 }
87 
88 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
89 bool
90 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>::isValid() const
91 {
92  return myCalculus != NULL;
93 }
94 
95 ///////////////////////////////////////////////////////////////////////////////
96 // Implementation of inline functions //
97 
98 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
99 std::ostream&
100 DGtal::operator<<(std::ostream& out, const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& object)
101 {
102  object.selfDisplay(out);
103  return out;
104 }
105 
106 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
107 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>
108 DGtal::operator+(const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator_a, const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator_b)
109 {
110  ASSERT( linear_operator_a.myCalculus == linear_operator_b.myCalculus );
111  typedef DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out> OutputOperator;
112  return OutputOperator(*linear_operator_a.myCalculus, linear_operator_a.myContainer + linear_operator_b.myContainer);
113 }
114 
115 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
116 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>
117 DGtal::operator-(const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator_a, const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator_b)
118 {
119  ASSERT( linear_operator_a.myCalculus == linear_operator_b.myCalculus );
120  typedef DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out> OutputOperator;
121  return OutputOperator(*linear_operator_a.myCalculus, linear_operator_a.myContainer - linear_operator_b.myContainer);
122 }
123 
124 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
125 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>
126 DGtal::operator*(const typename Calculus::Scalar& scalar, const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator)
127 {
128  typedef DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out> OutputOperator;
129  return OutputOperator(*linear_operator.myCalculus, scalar * linear_operator.myContainer);
130 }
131 
132 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
133 DGtal::KForm<Calculus, order_out, duality_out>
134 DGtal::operator*(const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator, const DGtal::KForm<Calculus, order_in, duality_in>& input_form)
135 {
136  ASSERT( linear_operator.myCalculus == input_form.myCalculus );
137  return DGtal::KForm<Calculus, order_out, duality_out>(*linear_operator.myCalculus, linear_operator.myContainer * input_form.myContainer);
138 }
139 
140 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_fold, DGtal::Duality duality_fold, DGtal::Order order_out, DGtal::Duality duality_out>
141 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>
142 DGtal::operator*(const DGtal::LinearOperator<Calculus, order_fold, duality_fold, order_out, duality_out>& operator_left, const DGtal::LinearOperator<Calculus, order_in, duality_in, order_fold, duality_fold>& operator_right)
143 {
144  ASSERT( operator_right.myCalculus == operator_right.myCalculus );
145  typedef DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out> OutputOperator;
146  return OutputOperator(*operator_left.myCalculus, operator_left.myContainer * operator_right.myContainer);
147 }
148 
149 template <typename Calculus, DGtal::Order order_in, DGtal::Duality duality_in, DGtal::Order order_out, DGtal::Duality duality_out>
150 DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>
151 DGtal::operator-(const DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out>& linear_operator)
152 {
153  typedef DGtal::LinearOperator<Calculus, order_in, duality_in, order_out, duality_out> OutputOperator;
154  return OutputOperator(*linear_operator.myCalculus, -linear_operator.myContainer);
155 }
156 
157 // //
158 ///////////////////////////////////////////////////////////////////////////////