2

Do you know why I cannot store in a double the result from a vector multiplication ?

double A = rowvec({1,3,4})*vec({5,6,7});

It gives "no suitable conversion function from "const arma::Glue"... to "const double" exists.

Still that matrix vector multiplication gives a double. How can I get around ?

Thank you!

  • 3
    Use [as_scalar0](http://arma.sourceforge.net/docs.html#as_scalar) to convert a 1x1 matrix to a scalar. For example: `double A = as_scalar( rowvec({1,3,4})*vec({5,6,7}) );` – hbrerkere May 22 '17 at 01:25

1 Answers1

1

The result of the product is an expression template called arma::Glue which can be converted to a 1x1 matrix. To do this inline and assign it to a double evaluate it explicitly using .eval() and take the only element which is (0,0).

#include <armadillo>

int main() {
  using arma::rowvec;
  using arma::vec;
  double A = (rowvec({1,3,4})*vec({5,6,7})).eval()(0,0);
};

N.B.: Did you mean dot(a,b)?

#include <armadillo>

int main() {
  using arma::rowvec;
  using arma::vec;
  using arma::dot;
  double A = dot(rowvec({1,3,4}), vec({5,6,7}));
};
Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • 5
    It's better to use [as_scalar()](http://arma.sourceforge.net/docs.html#as_scalar) in this case. Use of [.eval()](http://arma.sourceforge.net/docs.html#eval_member) is not really recommended as it can degrade performance. – hbrerkere May 22 '17 at 01:27