Eigen is a handy C++ library used for linear algebra and matrix multiplication. Another useful implementation it provides is its matrix manipulation.
This article will discuss how Eigen does matrix manipulation via nullary expressions. Our main focus will be on nullary expression and matrix manipulation via nullary expressions. We will also discuss implementing circulant matrices and indexing rows and columns here.
So let's get started!
Nullary Expressions
Before starting matrix manipulation via nullary expressions, we should first go through nullary expressions. The Nullary Expressions class represents a generic nullary operator expression. The main purpose of the class is to define procedural matrices. These matrices can be constant or random matrices. They are returned by the methods Ones(), Zero(), Constant(), Identity(), and Random().
NullaryOp must expose one of the following methods:
operator()(): If the procedural generation is independent of the coefficient entries (e.g., random numbers).
operator()(Index i): If the procedural generation is only applicable to vectors and is dependent on the coefficient index i (for e.g., linspace).
operator()(Index i, Index j): If the procedural generation is affected by matrix coordinates i, j (e.g., to generate a checkerboard with 0 and 1).
Circulant Matrices
A circulant matrix is a square matrix in which all row vectors have the same elements. And they have rotated one element to the right of the preceding row vector. This section will discuss making circulant matrices with matrix manipulation via nullary expressions.
We first create a template (generic function) of struct type name circulant_helper. Which have a scaler-type argument. It stores the number of rows and columns dynamically, i.e. on the compile time. The storage order will be in column-major.
This class represents a general nullary operator expression. It is the return type of the Ones(), Zero(), Constant(), Identity(), and Random() methods, and it is almost always used in this manner. This function will call circulant_functor and returns the value in a circulant order. The makeCirculant function uses the circulant_helper and circulant_functor to do row vice nullary operations.
This function will return the value in circulant order to the main function. The index size is initialised as a difference between rows and columns. If the index < 0, the value of the index will get continuously added to the m_vec.size();
Below is the implementation of the main function. Here we are initialising the vector with the desired value. Finally, we are calling the makeCirculant function.
int main()
{
Eigen::VectorXd vec(4);
vec << 1, 3, 5, 6;
Eigen::MatrixXd mat;
mat = makeCirculant(vec);
cout << mat << endl;
}
You can also try this code with Online C++ Compiler
The above program did matrix manipulation via nullary expression to make a circulant matrix. Here we create generic functions to do particular operations. The circulant_helper and circulant_functor functions store and generate nullary operator expressions. Afterwards, the makeCirculant function uses the circulant_helper and circulant_functor to do row vice nullary operations.
Try and compile by yourself with the help of online C++ Compiler for better understanding.
Indexing Rows and Columns
We use matrix manipulation via nullary expressions to match Matlab's indexing of a matrix using two vectors. It uses the vectors of indices that correspond to the rows and columns to be selected.
First, we will create a nullary_functor that stores references to the input matrix. The two arrays of indices and the necessary operator()(i,j). The order of storage will be row-major or column-major depending upon Flags&RowMajorBit. Afterwards, the indexing_functor function will be called.
Now we will create an indexing() function. The value passed are Array, rows and cols, creating the nullary expression. Finally, it will return the size of rows and columns and rows indices and column indices.
The above program indexes rows and columns using nullary operators. The nullary_functor function stores references to the input matrix. The order of storage will be row-order. Afterwards, the indexing() function will index rows, columns, rows indices and column indices.
What are some common operations that the Eigen Library can perform?
The Eigen's Library can be used to fulfil functions such as the definition of vectors and matrices. It can be used for various math operations such as vector add/subtract, dot and cross product.
What are the most common fields in which the Eigen Library is used?
Robotics and other real-world applications benefit from the Eigen Geometry Library. Eigen's Library is used to perform many advanced maths computations.
What is the conjugate of a matrix?
The conjugate of a matrix is obtained by replacing the elements of a matrix with their complex conjugate numbers.
What is OpenGL?
OpenGL is a cross-platform and cross-interface vector graphics rendering library. The OpenGL API is utilised to connect with the graphics processing unit. It is also used to achieve hardware-accelerated rendering.
What are the most common operations that the Eigen Library can perform?
The Eigen Library can be used to execute functions such as vector and matrix declaration. Eigen's Library can be used to execute some math operations such as vector solving, dot product, and cross product.
Conclusion
This article discussed matrix manipulation via nullary expressions. We discussed the nullary expression, circulant matrices, and indexing rows and columns. We hope this blog has helped you with matrix manipulation via nullary expressions. If you like to learn more, you can check out our articles: