ukoethe / vigra

a generic C++ library for image analysis
http://ukoethe.github.io/vigra/
Other
407 stars 191 forks source link

Math operators not overloaded for MultiArray #470

Closed pacman128 closed 5 years ago

pacman128 commented 5 years ago

The documentation (https://ukoethe.github.io/vigra/doc-release/vigra/group__MultiMathModule.html#details) states that the basic math operations are overloaded for the MultiArray types. However, the example code in the documentation does not compile:

#include <vigra/multi_array.hxx>

void test()
{
  using namespace vigra::multi_math;
  vigra::MultiArray<2,float> i(vigra::Shape2(100,100)), j(vigra::Shape2(100,100));

  vigra::MultiArray<2, float> h  = i + 4.0 * j;
  h += (i.transpose() - j) / 2.0;
}

$ g++ -c vtest3.cpp vtest3.cpp: In function ‘void test()’: vtest3.cpp:8:44: error: no match for ‘operator’ (operand types are ‘double’ and ‘vigra::MultiArray<2u, float>’) vigra::MultiArray<2, float> h = i + 4.0 j; ...

as the compiler is unable to find any operator overloads for +, -, * and /. There are +=, ... methods defined for MultiArrayView and the Matrix type does have all the math overloads.

Even the Matrix version of the above code doesn't work as it's written. I had to use float (not double) constants for it to compile:

#include <vigra/linear_algebra.hxx>

void test()
{
  vigra::Matrix<float> i(100,100), j(100,100);

  //vigra::Matrix<float> h  = 4.0 * j; Doesn't work with double constant 
  vigra::Matrix<float> h  = i + 4.0f * j;
  h += (i.transpose() - j) / 2.0f;
}

Using float constants did not have any effect for the MultiArray code, even h = i + j fails for it.

I see this problem on the Vigra version that is provided for Centos 7 which is 1.9.0 and the current version 1.11.1.

pacman128 commented 5 years ago

The problem was with my code. I didn't include multi_math.hxx. The following code does compile:

#include <vigra/multi_array.hxx>
#include <vigra/multi_math.hxx>   // This is required

void test()
{
  using namespace vigra::multi_math;

  vigra::MultiArray<2,float> i(vigra::Shape2(100,100)), j(vigra::Shape2(100,100));

  vigra::MultiArray<2, float> h  = i + 4.0 * j;
  h += (i.transpose() - j) / 2.0;
}

The documentation could make this clearer.