This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Custom scalars 2

Tags: None
(comma "," separated)
p0wertripper
Registered Member
Posts
6
Karma
0

Custom scalars 2

Sun May 12, 2013 7:23 pm
This is a follow up on this question: viewtopic.php?f=74&t=111094.

I am writing a class (e.g. class A) that contains and operates on a matrix with custom scalars (class B). The instances of class B, however, can be multiplied by doubles, i.e. class B contains relevant overloads. I would like to know how would it be possible to implement matrix (containing custom scalars) multiplication by doubles.

Here is a simple example of what I would like to do:

Code: Select all
Matrix<myscalar, 2, 1> C; //myscalar is a custom scalar class with relevant overloads
C(0).setValue(2);
C(1).setValue(3);
double e(5.0);
myscalar f(5.0);
C=f*C; // obviously, this works
C=e*C; // obviously, this gives a make error  "no match for operator*"


I presume one way to resolve the issue would be to introduce another overload for operator* in Eigen library. However, I am not sure how to approach the problem.
Another way would be to write a custom multiplication procedure that would run through the whole matrix and multiply each element individually. Obviously, this is a much easier solution, but it would defy (to some extent) the purpose of using Eigen matrix class for my application, as, I presume, such solution would be very inefficient comparing to the algorithms used by Eigen.

Basically the questions are:
1). How easy do you think it would be to introduce the multiplication by double for someone with little knowledge of Eigen classes from the developers perspective? Could you suggest how to do it?
2). Are there any simple workarounds that I have not thought of?
3). How much more inefficient would be the iterative multiplication for large matrices?

Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Custom scalars 2  Topic is solved

Sun May 12, 2013 8:41 pm
You can implement an overload of operator* and make it returns an Eigen expression type, e.g.:
Code: Select all

template<typename Derived>
CwiseUnaryOp<internal::scalar_multiple2_op<double,myscalar>, const Derived>
operator*(double& lhs, const DenseBase<Derived>& rhs) {
  return CwiseUnaryOp<internal::scalar_multiple2_op<double,myscalar>, const Derived>(
      internal::scalar_multiple2_op<double,myscalar>(lhs), rhs.derived());
}

You also need to specialize internal::scalar_product_traits for your type:
Code: Select all
namespace Eigen { namespace internal {
  template<> struct scalar_product_traits<double,myscalar> {
    enum { Defined = 1 };
    typedef myscalar ReturnType;
  };
}}

and don't forget the symmetric cases.
p0wertripper
Registered Member
Posts
6
Karma
0

Re: Custom scalars 2

Mon May 13, 2013 5:57 pm
Works well. Was not expecting to get a ready solution. Thank you very much.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]