Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lgamma() / lfactorial() not taking expressions #325

Open
lentinj opened this issue Sep 8, 2020 · 3 comments
Open

lgamma() / lfactorial() not taking expressions #325

lentinj opened this issue Sep 8, 2020 · 3 comments

Comments

@lentinj
Copy link
Contributor

lentinj commented Sep 8, 2020

Description:

Providing an expression argument lgamma(), amongst others, doesn't compile.

Reproducible Steps:

Try compiling the following example:

#include <TMB.hpp>

template<class Type>
Type objective_function<Type>::operator() () {
    DATA_VECTOR(x);

    // Works
    vector<Type> out1 = lgamma(x);
    REPORT(out1)

    // Doesn't compile!
    vector<Type> out2 = lgamma(x + 0);
    REPORT(out2)

    // Works
    auto lgamma_vec = [](vector<Type> x) -> vector<Type> {
        return lgamma(x);
    };
    vector<Type> out3 = lgamma_vec(x + 0);
    REPORT(out3)

    return 0;
}

The above code will only compile if the second example is commented out.

I (think!) this is due to function templates interacting badly with the lazy evaluation of x + 0. As it is handed one template expects to also return a "something + something" expression, which it can't do.

The bodge in the third case uses the same Type as the objective_function, so the lazy evaluation is no longer a problem.

Is there a better approach here I'm missing?

TMB Version:

> packageVersion("TMB")
[1] ‘1.7.18’

R Version:

> R.version.string
[1] "R version 3.6.3 (2020-02-29)"

Operating System:

$ uname -v
#1 SMP Debian 5.7.6-1 (2020-06-24)
@kaskr
Copy link
Owner

kaskr commented Sep 8, 2020

It's a known issue. An alternative 'fix' is:

vector<Type> out2 = lgamma(vector<Type>(x + 0));

I admit it would be nice if such casts weren't necessary.

@kaskr kaskr added the Wish list label Sep 8, 2020
@lentinj
Copy link
Contributor Author

lentinj commented Sep 10, 2020

Oops, yes that would have been the sensible thing to do. Apologies.

Happy to make a pull request, adding a note to the docs to save future head-scratching. The most useful place I can see is http://kaskr.github.io/adcomp/_book/Errors.html#compilation-errors - does this seem like a plan? I can't any specific docs on the vectorized lgamma, and it's not strictly an lgamma problem anyway.

@kaskr
Copy link
Owner

kaskr commented Sep 11, 2020

@lentinj

does this seem like a plan?

Yes - thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants