Is there a way to print an Armadillo matrix in gdb?

前端 未结 5 1390
天命终不由人
天命终不由人 2020-12-11 03:41

I\'m using gdb to debug my c++ program. I\'m using the armadillo numerical library to define my matrices. I have an armadillo matrix defined like so:

mat A =         


        
5条回答
  •  攒了一身酷
    2020-12-11 04:23

    The question may be old, but stumbling over it made me find a solution for my own work.

    Due to the template-based nature of the Armadillo library you need to provide some helpers of your own:

    #include 
    #include 
    
    template
    void print_matrix(Matrix matrix) {
        matrix.print(std::cout);
    }
    
    //provide explicit instantiations of the template function for 
    //every matrix type you use somewhere in your program.
    template void print_matrix(arma::mat matrix);
    template void print_matrix(arma::cx_mat matrix);
    
    int main() {
        arma::mat matrix = arma::randu(10,10);
    
        return 0;
    }
    

    Now you can easily call print_matrix from within gdb:

    (gdb) call print_matrix >(matrix) 
       0.8402   0.4774   0.0163   0.5129   0.5267   0.5260   0.2383   0.5316   0.6879   0.9565
       0.3944   0.6289   0.2429   0.8391   0.7699   0.0861   0.9706   0.0393   0.1660   0.5886
       0.7831   0.3648   0.1372   0.6126   0.4002   0.1922   0.9022   0.4376   0.4401   0.6573
       0.7984   0.5134   0.8042   0.2960   0.8915   0.6632   0.8509   0.9318   0.8801   0.8587
       0.9116   0.9522   0.1567   0.6376   0.2833   0.8902   0.2667   0.9308   0.8292   0.4396
       0.1976   0.9162   0.4009   0.5243   0.3525   0.3489   0.5398   0.7210   0.3303   0.9240
       0.3352   0.6357   0.1298   0.4936   0.8077   0.0642   0.3752   0.2843   0.2290   0.3984
       0.7682   0.7173   0.1088   0.9728   0.9190   0.0200   0.7602   0.7385   0.8934   0.8148
       0.2778   0.1416   0.9989   0.2925   0.0698   0.4577   0.5125   0.6400   0.3504   0.6842
       0.5540   0.6070   0.2183   0.7714   0.9493   0.0631   0.6677   0.3540   0.6867   0.9110
    

    Thanks to tab completion you only need to actually type a few characters of print_matrix >.

提交回复
热议问题