1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
std::cout <<"/////////////////Definition////////////////"<<std::endl; /* Definition of vectors and matrices in Eigen comes in the following form: Eigen::MatrixSizeType Eigen::VectorSizeType Eigen::ArraySizeType Size can be 2,3,4 for fixed size square matrices or X for dynamic size Type can be: i for integer, f for float, d for double, c for complex, cf for complex float, cd for complex double. */ // Vector3f is a fixed column vector of 3 floats: Eigen::Vector3f objVector3f; // RowVector2i is a fixed row vector of 3 integer: Eigen::RowVector2i objRowVector2i; // VectorXf is a column vector of size 10 floats: Eigen::VectorXf objv(10); Eigen::Matrix4d m; // 4x4 double Eigen::Matrix4cd objMatrix4cd; // 4x4 double complex //a is a 3x3 matrix, with a static float[9] array of uninitialized coefficients, Eigen::Matrix3f a; //b is a dynamic-size matrix whose size is currently 0x0, and whose array of coefficients hasn't yet been allocated at all. Eigen::MatrixXf b; //A is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients. Eigen::MatrixXf A(10, 15); //V is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients. Eigen::VectorXf V(30); //Template style definition // Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> &matrix Eigen::Matrix<double, 2, 3> my_matrix; my_matrix << 1, 2, 3, 4, 5, 6; // ArrayXf Eigen::Array<float, Eigen::Dynamic, 1> a1; // Array3f Eigen::Array<float, 3, 1> a2; // ArrayXXd Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> a3; // Array33d Eigen::Array<double, 3, 3> a4; Eigen::Matrix3d matrix_from_array = a4.matrix(); std::cout <<"///////////////////Initialization//////////////////"<< std::endl; Eigen::Matrix2d a_2d; a_2d.setRandom(); a_2d.setConstant(4.3); Eigen::MatrixXd identity=Eigen::MatrixXd::Identity(6,6); Eigen::MatrixXd zeros=Eigen::MatrixXd::Zero(3, 3); Eigen::ArrayXXf table(10, 4); table.col(0) = Eigen::ArrayXf::LinSpaced(10, 0, 90); std::cout <<"/////////////Matrix Coefficient Wise Operations///////////////////"<< std::endl; int i,j; std::cout << my_matrix << std::endl; std::cout << my_matrix.transpose()<< std::endl; std::cout<<my_matrix.minCoeff(&i, &j)<<std::endl; std::cout<<my_matrix.maxCoeff(&i, &j)<<std::endl; std::cout<<my_matrix.prod()<<std::endl; std::cout<<my_matrix.sum()<<std::endl; std::cout<<my_matrix.mean()<<std::endl; std::cout<<my_matrix.trace()<<std::endl; std::cout<<my_matrix.colwise().mean()<<std::endl; std::cout<<my_matrix.rowwise().maxCoeff()<<std::endl; std::cout<<my_matrix.lpNorm<2>()<<std::endl; std::cout<<my_matrix.lpNorm<Eigen::Infinity>()<<std::endl; std::cout<<(my_matrix.array()>0).all()<<std::endl;// if all elemnts are positive std::cout<<(my_matrix.array()>2).any()<<std::endl;//if any element is greater than 2 std::cout<<(my_matrix.array()>1).count()<<std::endl;// count the number of elements greater than 1 std::cout << my_matrix.array() - 2 << std::endl; std::cout << my_matrix.array().abs() << std::endl; std::cout << my_matrix.array().square() << std::endl; std::cout << my_matrix.array() * my_matrix.array() << std::endl; std::cout << my_matrix.array().exp() << std::endl; std::cout << my_matrix.array().log() << std::endl; std::cout << my_matrix.array().sqrt() << std::endl; std::cout <<"//////////////////Block Elements Access////////////////////"<< std::endl; //Block of size (p,q), starting at (i,j) matrix.block(i,j,p,q) Eigen::MatrixXf mat(4, 4); mat << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16; std::cout << "Block in the middle" << std::endl; std::cout << mat.block<2, 2>(1, 1) << std::endl; for (int i = 1; i <= 3; ++i) { std::cout << "Block of size " << i << "x" << i << std::endl; std::cout << mat.block(0, 0, i, i) << std::endl; } /////////////build matrix from vector, resizing matrix, dynamic size //////////////// Eigen::MatrixXd dynamicMatrix; int rows, cols; rows=3; cols=2; dynamicMatrix.resize(rows,cols); dynamicMatrix<<-1,7,3,4,5,1; //If you want a conservative variant of resize() which does not change the coefficients, use conservativeResize() dynamicMatrix.conservativeResize(dynamicMatrix.rows(), dynamicMatrix.cols()+1); dynamicMatrix.col(dynamicMatrix.cols()-1) = Eigen::Vector3d(1, 4, 0); dynamicMatrix.conservativeResize(dynamicMatrix.rows(), dynamicMatrix.cols()+1); dynamicMatrix.col(dynamicMatrix.cols()-1) = Eigen::Vector3d(5, -8, 6); /* you should expect this: 1 7 1 5 3 4 4 -8 5 1 0 6 */ std::cout<< dynamicMatrix<<std::endl; |