Re: [eigen] Treat single-element Array as scalar for arithmetc operations |

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] Treat single-element Array as scalar for arithmetc operations*From*: Adrien Escande <adrien.escande@xxxxxxxxx>*Date*: Wed, 29 Jul 2020 09:49:50 +0900*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=2bk+i6FETjIMO2L5feQ9Ajb9hXXTzueLK3q+16gyMWU=; b=bklco8qztiCUgxEhglesgXhdyHq207tBI/UeIQPdIN/9n84W7kmh05fBN+5RzpPYcO cd9AFXrO+B/zGRB/Ve5t6xAc70t73PvDwZrprmLuMYvsEX6mwpGHLcL8swS2+W8rdpGa i1dcKOVt0sbhVQ+eXzKbCTTz9c5msCikdsuRYxrVVMh92HiiCyh9AgJbI/x9qe7LiRJp o5vEI0bB9cS5ZSeMs2l01s8xYCCOrVPyAxWZsCFkPMRnF3E3k500IPJLbLdK8+HiqazP VyI00B+SJtSYXMkYuWGX803fk3lFdvr/Nwn7AGXh+YqHK49FpmiNPPuWSgJXajDcy8SI kCUQ==

It's difficult to know which way to go without being able to assert how much work it would be in your case.

Since you spoke about template functions, I assume you have a practical way to know at compile time if you have an array or a double. In this case and if you have problems with only a reasonable number of methods of ArrayXd, I think you should use templates and overcome the problems with free functions with overloads for ArrayXd and double. For example if you need to call ArrayXd::foo, you could create a function foo(const ArrayXd& a) {return a.foo();} and an overload foo(double) returning whatever is adequate.

Best regards,

Adrien

On Tue, Jul 28, 2020 at 10:05 PM Mario Azcueta <mazcueta@xxxxxxxxx> wrote:

Is there a way of adding/subtracting/multiplying two Eigen::ArrayXd arrays both when (a) they have dimension N, and (b) when one of them has dimension N and the other dimension 1?I'd ideally like that the array with size=1 is treated like a scalar for these operations. I'm somewhat trying to replicate the way Matlab behaves.As an example, I'd the code below to work both when a1 and a2 have the same size, as well as when one of them has size=1 and the other size=N:

ArrayXd add_arrays(const ArrayXd& a1, const ArrayXd& a2)

{

return a1 + a2;

}

Of course, this could be solved with an if statement, but I want to avoid that since my actual code would require to write a long function verifying all combinations of sizes (they're many args). Also, writing a template function gets me problems when trying to use some ArrayXd methods and I pass a double.I've looked into the EIGEN_ARRAYBASE_PLUGIN preprocessor which may seem like the way to extend this behavior, but I'm not sure this is the right way.. any advice will be welcome.Note: I've also post this question herehttps://stackoverflow.com/questions/63074198/c-eigen-treat-single-element-array-as-scalar-for-arithmetc-operations

**References**:**[eigen] Treat single-element Array as scalar for arithmetc operations***From:*Mario Azcueta

**Messages sorted by:**[ date | thread ]- Prev by Date:
**[eigen] How can we do in-place calculation for log(x)** - Next by Date:
**Re: [eigen] Treat single-element Array as scalar for arithmetc operations** - Previous by thread:
**[eigen] Treat single-element Array as scalar for arithmetc operations** - Next by thread:
**Re: [eigen] Treat single-element Array as scalar for arithmetc operations**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |