Hello dear all
Look i am using the library Eigen, for Matrix and vector calculations.
when, i tried to generate a matrix using advance initialization i want to avoid the dynamic allocation, therefore what i just did is the, following.
Matrix<int, 20, 20, RowMajor> m_MDoF;
Declare the typename "int" matrix size, and the RowMajor. At this point everything looks quite good. However, when i tried to generate the matrix in a class does not seems ok.
Look, my matrix size is composed by a variable which is passed though the constructor, then, when i run the code anything works. What tried, was the use the maximum size of rows and maximum size of columns. like this
Matrix<int, Dynamic, Dynamic, RowMajor, 20, 20> m_MDoF;
and now everything works, BUT MUST NOTICE THAT I WAS FORCED TO PLACE A 20, 20 as a maximum in rows and columns.
Hence, i decided to replace the "20" by a variable that i passed through the constructor, and the code does not work.
One of my ideas that actually i already tested, was using class templates
1 2 3
|
template<int numberRows, int numberCols>
.....
Matrix<int, Dynamic, Dynamic, RowMajor, numberRows, numberCols> m_MDoF;
| |
everything works quite nice.
However, i was wondering if there is a possibility to avoid the dynamic memory allocation and templates.
This my code.
the main cpp file
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
|
#include <bits/stdc++.h>
#include <Eigen/LU>
#include "MDoF.h"
using std::cout;
using std::endl;
using Eigen::Matrix;
using Eigen::RowMajor;
using Eigen::ColMajor;
using Eigen::Dynamic;
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin); // file to make input data.
freopen("output.txt", "w", stdout); // file to store the output data.
#endif
/*--------------- MAIN VARIABLES ---------------*/
const unsigned int nNodes = 5; // number of nodes
const unsigned int nDofNode = 2; // number of DoF per node
const unsigned int nElements = 7; // number of elements
const unsigned int nNodesElement = 2; // number of nodes per element
/* --------------- INSTANTIATION --------------- */
/* ---------- VARIABLES INITIALIZATION ---------- */
MDoF mDoF(nNodes, nDofNode, nElements, nNodesElement);
return 0;
}
| |
This is my class using eigen library
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
|
#pragma once
#include <bits/stdc++.h>
#include <Eigen/LU>
using std::cout;
using std::endl;
using Eigen::Matrix;
using Eigen::RowMajor;
using Eigen::ColMajor;
using Eigen::Dynamic;
class MDoF
{
private:
const unsigned int m_nNodes; // (nRows)
const unsigned int m_nDofNode; // (nCols)
const unsigned int m_nElements; // (nRowsTwo)
const unsigned int m_nNodesElement; // (nColsTwo)
protected:
Matrix<int, Dynamic, Dynamic, RowMajor, 20, 20> m_MDoF;
public:
MDoF(const unsigned int nNodes, const unsigned int nDofNode, const unsigned int nElements, const unsigned int nNodesElement);
~MDoF();
};
MDoF::MDoF(const unsigned int nNodes, const unsigned int nDofNode, const unsigned int nElements, const unsigned int nNodesElement)
: m_nNodes(nNodes),
m_nDofNode(nDofNode),
m_nElements(nElements),
m_nNodesElement(nNodesElement)
{
m_MDoF.resize(m_nNodes, m_nDofNode);
m_MDoF.setZero();
cout << m_MDoF << endl;
}
MDoF::~MDoF()
{
}
| |
If you replace the 20 by m_nNodes and m_nDofNode the code does not work.