-1

I'm using Stroustrup's matrix.h implementation as I have a lot of matrix heavy computation to do. It will make life easier if I can just get the matrix populated!

I'm receiving a complex object with a matrix that is not known until received. Once it enters the method, I can get the row and column count, but I have to use a double i,j loop to pull the values since they are in a cpp17::any structure and I have to convert them using asNumber().

I declare it as follows as part of an object definition:

 Matrix<double,2> inputLayer;

In the code that instantiates the object, I have the following code:

int numRows = sourceSeries->rowCount();
int numColumns = sourceSeries->columnCount();
int i,j = 0;
    
for(i=0; i<numRows; i++){
    for(j=0;j<numColumns;j++) {
        // make sure you skip the header row in sourceSeries
      inputLayer[i][j] = asNumber(sourceSeries->data(i+1,j,ItemDataRole::Display));
    }
}

There is nothing like push_back() for the matrix template. The examples I can find in his books and on the web either pre-populate the matrix in the definition or create it from existing lists, which I won't have at this particular time.

Do I need to define a "new" double, receive the asNumber(), then set inputlayer[][] = the "new" double?

I'm hoping not to have to manage the memory like I can do with vectors that release when I go out of scope, which is why I was avoiding "new."

I'm using the boost frameworks as well and I'm wondering if I should try ublas version instead, or just get this one working.

Mark Travis
  • 687
  • 6
  • 5
  • 5
    If this is for real work as opposed to learning, use a library like Armadillo or Eigen, don't roll your own – Hong Ooi Oct 08 '21 at 19:32
  • 2
    Through no fault of stroustrup, if you want to do a lot of work with matrices, you should use a library like Eigen – AndyG Oct 08 '21 at 19:32
  • *"I have the following code"* -- why are you not done? What did you observe to lead you to the conclusion that this code is inadequate? – JaMiT Oct 08 '21 at 19:37
  • 1
    if memory management is all you're worried about, >=c++11 will do this for you. For instance, you can create a [`unique_ptr`](https://en.cppreference.com/w/cpp/memory/unique_ptr) of your `Matrix` and it will automatically be deallocated when it goes out of scope. – yano Oct 08 '21 at 19:37
  • Eigen over boost? This is a startup project I undertook 8 years ago and I'm trying to get it finished. I used to program c++ (and other languages) full-time years ago, but as you can see, I'm a bit rusty. This isn't my day job (yet.) Thanks for your responses!! – Mark Travis Oct 08 '21 at 19:53
  • @MarkTravis: Take a look at [Boost::uBLAS vs Eigen](https://stackoverflow.com/q/37221040/10077). – Fred Larson Oct 08 '21 at 20:19
  • @FredLarson, hong-ooi, andyG, Gold!! Thanks for the pointer to Eigen! – Mark Travis Oct 08 '21 at 20:31

1 Answers1

0

Thanks for the pointers to Eigen, that was so simple! Here's all I had to do:

In the header file:

#include "Eigen/Dense"

using namespace Eigen;

In the object definition of the header file:

    Matrix<double, Dynamic, Dynamic> inputLayer;

In the code where I need to read in the matrix:

int numRows = sourceSeries->rowCount();
int numColumns = sourceSeries->columnCount();
int i,j = 0;
MatrixXd inputLayer(numRows,numColumns);


for(i=0; i<numRows; i++){
    for(j=0;j<numColumns;j++) {
        // make sure you skip the header row in sourceSeries

        inputLayer(i,j) = asNumber(sourceSeries->data(i+1,j,ItemDataRole::Display));
       
    }
}

Sorry I had to waste so much time trying to get the other code to work, but at least I got real familiar with my debugger and the codebase again. Thanks everyone for the comments!

Mark Travis
  • 687
  • 6
  • 5
  • 1
    This might just be a hobby project, but it's bad practice to put `using namespace` in a header. See e.g. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice for some reasons why. – AVH Oct 08 '21 at 21:57
  • @AVH Good point. I'll move that over to the CPP file instead of the header. I can see the argument for short prefixes like std:: . I've got a couple of libraries that have complicated prefixes and it makes it a bit MORE readable covered by a namespace. Granted, the namespaces are declared in the CPP file. – Mark Travis Oct 09 '21 at 02:40
  • Yeah, I think this is actually a really good example of why you'd want the namespace in front of the type. In your original question you had a `"stroustrup"::Matrix`, and in the solution it's `Eigen::Matrix`. I find that keeping that explicit, makes it much quicker later on to figure out what how code works. – AVH Oct 09 '21 at 20:12