ODE solving with vectors

58 views
Skip to first unread message

Rachel L

unread,
Mar 21, 2017, 9:14:53 AM3/21/17
to stan-...@googlegroups.com
I'm trying to solve a model based on a ODE. However, in order to make my ODE work within stan, everything is being calculated using vectors. When I attempt to compile, I get the following error: 
stan/lib/stan_math/stan/math/prim/arr/functor/coupled_ode_system.hpp:98:15: error: no match for ‘operator=’ (operand types are ‘std::vector<double>’ and ‘Eigen::Matrix<double, -1, 1>’)
         dy_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
               ^
In file included from /usr/include/c++/5/vector:69:0,
                 from stan/lib/stan_math/stan/math/memory/stack_alloc.hpp:12,
                 from stan/lib/stan_math/stan/math/rev/core/autodiffstackstorage.hpp:4,
                 from stan/lib/stan_math/stan/math/rev/core.hpp:4,
                 from stan/lib/stan_math/stan/math/rev/mat.hpp:4,
                 from stan/lib/stan_math/stan/math.hpp:4,
                 from stan/src/stan/model/model_header.hpp:4,
                 from ./examples/platelets/platelets.hpp:3,
                 from <command-line>:0:
/usr/include/c++/5/bits/vector.tcc:167:5: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = double; _Alloc = std::allocator<double>]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/5/bits/vector.tcc:167:5: note:   no known conversion for argument 1 from ‘Eigen::Matrix<double, -1, 1>’ to ‘const std::vector<double>&’
make/models:10: recipe for target 'examples/platelets/platelets' failed"

I interpret this error that I can't return a vector to use with stan's ODE solvers. If so, is there a way to convert a vector into an acceptable data type, or should I just rewrite the model using different data types?
platelets.stan

Michael Betancourt

unread,
Mar 21, 2017, 9:19:27 AM3/21/17
to stan-...@googlegroups.com
We’ll need lots more detail to offer any help:

What interface are you using (or are you using the math library directly)?
What operating system are you using?
What compiler are you using?

It will also help to see the entire program that your’ trying to compile.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bob Carpenter

unread,
Mar 21, 2017, 12:55:20 PM3/21/17
to stan-...@googlegroups.com
It always helps to include your Stan code, otherwise we have
to guess. See below for guess.

> On Mar 21, 2017, at 9:14 AM, Rachel L <space...@gmail.com> wrote:
>
> I'm trying to solve a model based on a ODE. However, in order to make my ODE work within stan, everything is being calculated using vectors. When I attempt to compile, I get the following error:
> stan/lib/stan_math/stan/math/prim/arr/functor/coupled_ode_system.hpp:98:15: error: no match for ‘operator=’ (operand types are ‘std::vector<double>’ and ‘Eigen::Matrix<double, -1, 1>’)
> dy_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
> ^
> In file included from /usr/include/c++/5/vector:69:0,
> from stan/lib/stan_math/stan/math/memory/stack_alloc.hpp:12,
> from stan/lib/stan_math/stan/math/rev/core/autodiffstackstorage.hpp:4,
> from stan/lib/stan_math/stan/math/rev/core.hpp:4,
> from stan/lib/stan_math/stan/math/rev/mat.hpp:4,
> from stan/lib/stan_math/stan/math.hpp:4,
> from stan/src/stan/model/model_header.hpp:4,
> from ./examples/platelets/platelets.hpp:3,
> from <command-line>:0:
> /usr/include/c++/5/bits/vector.tcc:167:5: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = double; _Alloc = std::allocator<double>]
> vector<_Tp, _Alloc>::
> ^
> /usr/include/c++/5/bits/vector.tcc:167:5: note: no known conversion for argument 1 from ‘Eigen::Matrix<double, -1, 1>’ to ‘const std::vector<double>&’

You're trying to assign a vector type in Stan to something
that requires a real[] (array of reals). They're not the same
and there's a chapter in the manual outlining the differences.

- Bob

Rachel L

unread,
Mar 21, 2017, 3:32:17 PM3/21/17
to Stan users mailing list
Code has been uploaded.

Rachel L

unread,
Mar 21, 2017, 3:33:40 PM3/21/17
to Stan users mailing list
I'm on Ubunutu, 16.04. I believe I'm using the math library directly-I'm just calling make from the command line on the model file.

Bob Carpenter

unread,
Mar 21, 2017, 3:45:21 PM3/21/17
to stan-...@googlegroups.com

> On Mar 21, 2017, at 3:32 PM, Rachel L <space...@gmail.com> wrote:
>
> Code has been uploaded.

Where can we find it?

You shouldn't be getting this bug at all (seeing raw compile
problems from C++), but we need the program to reproduce it
and help you diagnose how to fix the underlying problem.

- Bob

Rachel L

unread,
Mar 22, 2017, 8:38:11 AM3/22/17
to Stan users mailing list
It's attached to the original post.

Michael Betancourt

unread,
Mar 22, 2017, 9:43:01 AM3/22/17
to stan-...@googlegroups.com
Google groups often strips attachments of non-standard filetypes —
there’s definitely nothing attached on any of the previous messages
in this thread.

It sounds like you’re using CmdStan, in which case it would be
most helpful if you could send the full Stan model as a text file.

Rachel L

unread,
Mar 22, 2017, 9:46:12 AM3/22/17
to Stan users mailing list
Trying to attach it as a .txt.
platelets.txt

Michael Betancourt

unread,
Mar 22, 2017, 10:56:20 AM3/22/17
to stan-...@googlegroups.com
Thanks. The problem is that the ODE function has to be defined
as an array of reals, not a vector. If you change

“vector Balances” to “real[] Balances”

and

“vector[11] dxdd” to "real dxdt[11]”

and then change Line 156 to a loop then it compiles correctly.

Bob — we should be able to check for this type mismatch in the parser,
no?
> <platelets.txt>

Rachel L

unread,
Mar 23, 2017, 9:55:40 AM3/23/17
to Stan users mailing list
Thanks! That change did it, the model now successfully complies. 
Reply all
Reply to author
Forward
0 new messages