Generally speaking, silently treating vectors as scalars and vice versa
with implied broadcasting is a common source of subtle bugs, so this
behavior is unlikely to change. Eigen does support scalar*vector, if you
extract the single element as such.

On Tue, Jul 28, 2020 at 6:06 AM Mario Azcueta <[email protected]> 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 here
>
> https://stackoverflow.com/questions/63074198/c-eigen-treat-single-element-array-as-scalar-for-arithmetc-operations
>

Reply via email to