Search code examples
c++boostmatrixicc

Multiplication of a matrix by scalar zero


I am working on a c++ project to do some computations. I am using the boost library to do these computations. I had a strange bug (results were not right or the program was freezing). When I checked what was happening, I realized that after creating a matrix of doubles and multiplying the matrix by 0 (to ensure that everything is equal to zero e.g.:

matrix<double> *A=new matrix<double>(10,100);
(*A)*=0.0;

My matrix was not always filled by strict 0 (this is inside a method that is called a LOT, the pointer is deleted correctly and I checked that I do not have any memory leaks), sometimes negative zeros (-0.0) or even NaNs!! Then I realized that using the method clear() will set all the values to the default (which is 0 for double). So even if I do have a solution, this is very peculiar! As usual, the question is: did I do something wrong (more likely) or is there a bug in the library (less likely)?

PS: I do use the intel compiler (version 2015)


Solution

  • As specified in Boost Libraries,

    matrix (size_type size1, size_type size2): Allocates an uninitialized matrix that holds size1 rows of size2 elements.
    

    So you are using a matrix not initialized, causing those strange errors. Create first a zero-matrix or an identity matrix:

    identity_matrix<double> matrix(3); //create a 3x3 identity matrix
    zero_matrix<double> matrix(3, 3);  //create a 3x3 zero matrix