Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Armadillo column matrix initialization ambiguous

I define an armadillo matrix and I try to initialize it via initialiser lists according to its manual.

It is fine unless I try to define a column matrix.

Why is it ambigious?

#include <armadillo>

int main()
{
    // ok : square matrix
    arma::mat A={{1.0,2.0},{3.0,4.5}};

    // ok: row matrix
    arma::mat B={3.5,4.0};

    // error: conversion from ‘<brace-enclosed initializer list>’ to 
    // ‘arma::mat {aka arma::Mat<double>}’ is ambiguous
    arma::mat C={{3.5},{4.0}};

    // ok: column matrix
    arma::mat D=arma::mat({3.5,4.0}).t();

    return 0;
}
like image 535
ar2015 Avatar asked Oct 19 '25 08:10

ar2015


1 Answers

It's a problem with the C++11/14 standard. Because there is only one element in each inner set, the standard essentially says that {{3.5},{4.0}} can also be interpreted as {3.5,4.0}. In other words, {3.5} can be implicitly converted to double(3.5). This leads to the ambiguity between the two constructors.

One possible solution is to use the Armadillo column vector constructor:

arma::mat C = arma::colvec( {3.5, 4.0} );
like image 96
mtall Avatar answered Oct 21 '25 22:10

mtall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!