37 #ifndef GMM_DENSE_SYLVESTER_H
38 #define GMM_DENSE_SYLVESTER_H
47 template <
typename MAT1,
typename MAT2,
typename MAT3>
48 void kron(
const MAT1 &m1,
const MAT2 &m2,
const MAT3 &m3_,
50 MAT3 &m3 =
const_cast<MAT3 &
>(m3_);
51 size_type m = mat_nrows(m1), n = mat_ncols(m1);
52 size_type l = mat_nrows(m2), k = mat_ncols(m2);
54 GMM_ASSERT2(mat_nrows(m3) == m*l && mat_ncols(m3) == n*k,
55 "dimensions mismatch");
60 gmm::copy(gmm::scaled(m2, m1(i,j)),
61 gmm::sub_matrix(m3, sub_interval(l*i, l),
62 sub_interval(k*j, k)));
64 gmm::add(gmm::scaled(m2, m1(i,j)),
65 gmm::sub_matrix(m3, sub_interval(l*i, l),
66 sub_interval(k*j, k)));
74 template <
typename MAT,
typename VECT>
75 colmatrix_to_vector(
const MAT &A, VECT &v, col_major) {
76 size_type m = mat_nrows(A), n = mat_ncols(A);
77 GMM_ASSERT2(m*n == vect_size(v),
"dimensions mismatch");
79 gmm::copy(mat_col(A, i), sub_vector(v, sub_interval(i*m, m)));
82 template <
typename MAT,
typename VECT>
83 colmatrix_to_vector(
const MAT &A, VECT &v, row_and_col)
84 { colmatrix_to_vector(A, v, col_major()); }
86 template <
typename MAT,
typename VECT>
87 colmatrix_to_vector(
const MAT &A, VECT &v, col_and_row)
88 { colmatrix_to_vector(A, v, col_major()); }
90 template <
typename MAT,
typename VECT>
91 colmatrix_to_vector(
const MAT &A, VECT &v, row_major) {
92 size_type m = mat_nrows(mat), n = mat_ncols(A);
93 GMM_ASSERT2(m*n == vect_size(v),
"dimensions mismatch");
95 gmm::copy(mat_row(A, i), sub_vector(v, sub_slice(i, n, m)));
98 template <
typename MAT,
typename VECT>
inline
99 colmatrix_to_vector(
const MAT &A,
const VECT &v_) {
100 VECT &v =
const_cast<VECT &
>(v_);
101 colmatrix_to_vector(A, v,
typename linalg_traits<MAT>::sub_orientation());
109 template <
typename MAT,
typename VECT>
110 vector_to_colmatrix(
const VECT &v, MAT &A, col_major) {
111 size_type m = mat_nrows(A), n = mat_ncols(A);
112 GMM_ASSERT2(m*n == vect_size(v),
"dimensions mismatch");
114 gmm::copy(sub_vector(v, sub_interval(i*m, m)), mat_col(A, i));
117 template <
typename MAT,
typename VECT>
118 vector_to_colmatrix(
const VECT &v, MAT &A, row_and_col)
119 { vector_to_colmatrix(v, A, col_major()); }
121 template <
typename MAT,
typename VECT>
122 vector_to_colmatrix(
const VECT &v, MAT &A, col_and_row)
123 { vector_to_colmatrix(v, A, col_major()); }
125 template <
typename MAT,
typename VECT>
126 vector_to_colmatrix(
const VECT &v, MAT &A, row_major) {
127 size_type m = mat_nrows(mat), n = mat_ncols(A);
128 GMM_ASSERT2(m*n == vect_size(v),
"dimensions mismatch");
130 gmm::copy(sub_vector(v, sub_slice(i, n, m)), mat_row(A, i));
133 template <
typename MAT,
typename VECT>
inline
134 vector_to_colmatrix(
const VECT &v,
const MAT &A_) {
135 MAT &A =
const_cast<MAT &
>(A_);
136 vector_to_colmatrix(v, A,
typename linalg_traits<MAT>::sub_orientation());
144 template <
typename MAT1,
typename MAT2,
typename MAT3,
typename MAT4 >
145 void sylvester(
const MAT1 &m1,
const MAT2 &m2,
const MAT3 &m3,
147 typedef typename linalg_traits<Mat>::value_type T;
149 MAT3 &m4 =
const_cast<MAT4 &
>(m4_);
150 size_type m = mat_nrows(m1), n = mat_ncols(m1);
151 size_type l = mat_nrows(m2), k = mat_ncols(m2);
153 GMM_ASSERT2(m == n && l == k && m == mat_nrows(m3) &&
154 l == mat_ncols(m3) && m == mat_nrows(m4) && l == mat_ncols(m4),
155 "dimensions mismatch");
157 gmm::dense_matrix<T> akronb(m*l, m*l);
158 gmm::dense_matrix<T> idm(m, m), idl(l,l);
159 gmm::copy(identity_matrix(), idm);
160 gmm::copy(identity_matrix(), idl);
161 std::vector<T> x(m*l), c(m*l);
163 kron(idl, m1, akronb);
164 kron(gmm::transposed(m2), idm, akronb,
false);
166 colmatrix_to_vector(m3, c);
168 vector_to_colmatrix(x, m4);