![]() Specifying the size in the type also allows for more rigorous checking at compile-time. The compiler emits better (faster) code because it knows the size of the matrices and vectors. The use of fixed-size matrices and vectors has two advantages. Note that here the coefficients of vector v are directly set in the constructor, though the same syntax of the left example could be used too. Similarly, we have VectorXd on the left (arbitrary size) versus Vector3d on the right (fixed size). Because the type already encodes the size of the matrix, it is not necessary to specify the size in the constructor compare MatrixXd m(3,3) with Matrix3d m. ![]() The version in the right column is similar, except that the matrix is of type Matrix3d, which represents matrices of a fixed size (here 3-by-3). ![]() In the version in the left column, the matrix is of type MatrixXd which represents matrices of arbitrary size. Now look back at the second example program. The final line of the program multiplies the matrix m with the vector v and outputs the result. The one but last line uses the so-called comma-initializer, explained in Advanced initialization, to set all coefficients of the vector v to be as follows: Here, the vector v is created to contain 3 coefficients which are left uninitialized. This represents a (column) vector of arbitrary size. The next line of the main function introduces a new type: VectorXd. The function call MatrixXd::Constant(3,3,1.2) returns a 3-by-3 matrix expression having all coefficients equal to 1.2. The next line applies a linear mapping such that the values are between 10 and 110. The second example starts by declaring a 3-by-3 matrix m which is initialized using the Random() method with random values between -1 and 1. Concentrate on the left-hand program for now we will talk about the right-hand program later. Here is another example, which combines matrices with vectors. The final line outputs the matrix m to the standard output stream. The following three statements sets the other three entries. As usual in computer science, the index of the first index is 0, as opposed to the convention in mathematics that the first index is 1. You need to use round parentheses to refer to entries in the matrix. The statement m(0,0) = 3 sets the entry in the top-left corner to 3. The first line of the main function declares a variable of type MatrixXd and specifies that it is a matrix with 2 rows and 2 columns (the entries are not initialized). All classes and functions defined in this header file (and other Eigen header files) are in the Eigen namespace. The Eigen/Dense header file defines all member functions for the MatrixXd type and related types (see also the table of header files). ![]() See the quick reference guide for an overview of the different types you can use to represent a matrix. This represents a matrix of arbitrary size (hence the X in MatrixXd), in which every entry is a double (hence the d in MatrixXd). The Eigen header files define many types, but for simple applications it may be enough to use only the MatrixXd type. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |