113 lines
4.8 KiB
C++
113 lines
4.8 KiB
C++
#include "picout.h"
|
|
#include "pivector2d.h"
|
|
#include "Eigen/Dense"
|
|
|
|
typedef Eigen::MatrixXd PIMathMatrixd;
|
|
typedef Eigen::MatrixXf PIMathMatrixf;
|
|
typedef Eigen::MatrixXcf PIMathMatrixcf;
|
|
typedef Eigen::MatrixXcd PIMathMatrixcd;
|
|
|
|
template<typename Type>
|
|
inline PICout operator <<(PICout s, const Eigen::DenseCoeffsBase<Type, Eigen::ReadOnlyAccessors> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;}
|
|
template<typename Type>
|
|
inline PIVector<PIVector<typename Type::Scalar> > toVectors(const Eigen::DenseCoeffsBase<Type, Eigen::ReadOnlyAccessors> & m) {
|
|
PIVector<PIVector<typename Type::Scalar> > ret;
|
|
for(int i = 0; i < m.cols(); ++i)
|
|
ret << PIVector<typename Type::Scalar>(&(m(i*m.rows())), m.rows());
|
|
return ret;
|
|
}
|
|
|
|
template<typename Type>
|
|
inline Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> fromVectors(const PIVector<PIVector<Type> > & m) {
|
|
if (m.isEmpty()) return Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic>(0, 0);
|
|
if (m[0].isEmpty()) return Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic>(1, 0);
|
|
Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> ret(m[0].size(), m.size());
|
|
for(int i = 0; i < m.size_s(); ++i)
|
|
for(int j = 0; j < m[i].size_s(); ++j)
|
|
ret(j,i) = m[i][j];
|
|
return ret;
|
|
}
|
|
template<typename Type>
|
|
inline PIVector<typename Type::Scalar> toPlainVector(const Eigen::DenseCoeffsBase<Type, Eigen::ReadOnlyAccessors> & m) {
|
|
return PIVector<typename Type::Scalar>(&(m(0)), m.size());
|
|
}
|
|
|
|
|
|
int main() {
|
|
// Eigen::MatrixXd;
|
|
// PIMathMatrix<complexd> m1(2,2, PIVector<complexd>() << complexd(1.) << complexd(1.) << complexd(0.) << complexd(1.));
|
|
// PIMathMatrix<complexd> m2(2,3, PIVector<complexd>() << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.));
|
|
PIMathMatrixd m1(3,3);
|
|
m1 << 1,2,3,4,6,5,9,8,7;
|
|
// PIMathMatrix<double> m2(2,3, PIVector<double>() << 1. << 2. << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.));
|
|
// PIMathMatrix<double> m3(vv);
|
|
// piCout << m2 << PICoutManipulators::NewLine;
|
|
// piCout << (PIVector2D<complexd>)m2 << PICoutManipulators::NewLine;
|
|
// for (uint r = 0; r < m2.rows(); ++r) {
|
|
// for (uint c = 0; c < m2.cols(); ++c) {
|
|
//// piCout << m2[r][c];
|
|
// m2[r][c] = complexd(m2[r][c].real(), -(m2[r][c].imag()));
|
|
//// m2[r][c] = complexd();
|
|
|
|
// }
|
|
// }
|
|
piCout << m1 << PICoutManipulators::NewLine;
|
|
piCout << toPlainVector(m1);
|
|
// piCout << m2 << PICoutManipulators::NewLine;
|
|
// piCout << (PIVector2D<complexd>)m2 << PICoutManipulators::NewLine;
|
|
// piCout << m2.transposed() << PICoutManipulators::NewLine;
|
|
// piCout << hermitian(m2) << PICoutManipulators::NewLine;
|
|
// piCout << (m2*m1);
|
|
// PIMathVector<complexd> v(PIVector<complexd>() << complexd(1.) << complexd(2.));
|
|
// piCout << (m1*PIMathMatrix<complexd>::matrixCol(v));```
|
|
// piCout << m2;
|
|
// piCout << m3;
|
|
// bool ok;
|
|
PIMathMatrixd mi = m1.inverse();
|
|
piCout << mi;
|
|
piCout << PIMathMatrixd(mi * m1);
|
|
piCout << m1.row(0);
|
|
piCout << m1.row(1);
|
|
piCout << m1.row(2);
|
|
piCout << "";
|
|
piCout << m1.col(0);
|
|
piCout << m1.col(1);
|
|
piCout << m1.col(2);
|
|
//m2 = m2.transposed();
|
|
// piCout << m2 << PICoutManipulators::NewLine << hermitian(m2);
|
|
// piCout << PICoutManipulators::NewLine << hermitian(m2) * mi;
|
|
// piCout << PICoutManipulators::NewLine << m1 * m2 << PICoutManipulators::NewLine << m2 * m1;
|
|
// piCout << PICoutManipulators::NewLine << hermitian(m1*m2) << PICoutManipulators::NewLine << hermitian(m2) * hermitian(m1);
|
|
// PIVector2D<double> v(3,2);
|
|
// for (uint r = 0; r < v.rows(); ++r) {
|
|
// for (uint c = 0; c < v.cols(); ++c) {
|
|
// piCout << r*v.cols()+c+1;
|
|
// v[r][c] = r*v.cols()+c+1;
|
|
// }
|
|
// }
|
|
// piCout << v;
|
|
// for (uint c = 0; c < v.cols(); ++c) {
|
|
// for (uint r = 0; r < v.rows(); ++r) {
|
|
// piCout << r*v.cols()+c+1;
|
|
// v[r][c] = -v[r][c];
|
|
// }
|
|
// }
|
|
piCout << "-------" << PICoutManipulators::NewLine;
|
|
PIMathMatrixd v = m1.block(0,0, 3,2);
|
|
m1 = v;
|
|
// PIMathMatrixd v2 = m1.block(0,0, 2,3);
|
|
// PIMathMatrixd v2(2,3, m1.toPlainVector().resize(6));
|
|
// piCout << v << PICoutManipulators::NewLine;
|
|
piCout << m1 << PICoutManipulators::NewLine;
|
|
PIVector<PIVector<double> > vv = toVectors(m1);
|
|
piCout << vv << PICoutManipulators::NewLine;
|
|
piCout << fromVectors(vv) << PICoutManipulators::NewLine;
|
|
// piCout << toVectors(PIMathMatrixd(m1.transpose())) << PICoutManipulators::NewLine;
|
|
// PIMathMatrix<double> v2 = PIMathMatrix<double>(v.toVectors());
|
|
// piCout << v2 << PICoutManipulators::NewLine;
|
|
// piCout << v*v2 << PICoutManipulators::NewLine;
|
|
// piCout << v2*v << PICoutManipulators::NewLine;
|
|
return 0;
|
|
}
|
|
|