Adding matrix rows with TMB vectors

250 views
Skip to first unread message

pthomp...@gmail.com

unread,
Mar 1, 2022, 4:59:19 PM3/1/22
to TMB Users
Hello, 

I'm trying to write up a model in TMB (I'm quite familiar with R, a little bit less familiar with TMB and C++) and having a bit of trouble. I think I'm missing something obvious, but I've looked a bit at previous questions as well as the docs and still can't figure it out. The problem I'm having can be simplified to the short example below: 

#include "TMB.hpp"
template<class Type>
Type objective_function<Type>::operator() ()
{
  // Data
  DATA_MATRIX( M );
 
  // Parameters
  PARAMETER_VECTOR( v );
 
  vector<Type> value = M.row(0) + v;
 
  return 0;
}

This piece of code doesn't compile because I can't add M.row(0) to v. I'm a bit confused as to why because M.row(0) should be stored as a vector<Type>, and I should theoretically be able to add two vector<Type>'s elementwise. Of course - I'm not suggesting that there is a bug - just wondering what I'm missing! I understand that obviously if these vectors are different lengths, this poses a problem, but figured that would show up as a runtime error - if I'm missing something let me know! What is the proper way to get "value" - is a for loop required?

Any input is appreciated,
Peter Thompson (he/him)

Anders Nielsen

unread,
Mar 2, 2022, 2:53:03 AM3/2/22
to pthomp...@gmail.com, TMB Users

Hi Peter, 


Something like this should work: 


  vector<Type> value = vector<Type>(M.row(0)) + v;


Cheers, 

Anders. 


From: tmb-...@googlegroups.com <tmb-...@googlegroups.com> on behalf of pthomp...@gmail.com <pthomp...@gmail.com>
Sent: Tuesday, March 1, 2022 10:59 PM
To: TMB Users
Subject: [TMB users] Adding matrix rows with TMB vectors
 
--
To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
---
You received this message because you are subscribed to the Google Groups "TMB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tmb-users/f764e612-5422-416d-a49d-1656cf8598fcn%40googlegroups.com.

pthomp...@gmail.com

unread,
Mar 2, 2022, 11:14:55 AM3/2/22
to TMB Users
Hi Anders, this worked perfectly! Thanks for the suggestion, I hadn't thought about casting.

Peter

mark...@gmail.com

unread,
Jul 14, 2022, 5:57:09 PM7/14/22
to TMB Users
Hi everyone,

I am having similar problems. Here is simple code with errors. vector*vector is supposed to be an element wise multiplication producing a vector (see https://kaskr.github.io/adcomp/matrix_arrays_8cpp-example.html

  matrix<Type> Scum(Nyears,Nyears);
  matrix<Type> Pcum(Nyears,Nyears);

      std::cout<< sum(vector<Type>(Scum.row(i)))<<std::endl;   //Compiles
      std::cout<< sum(vector<Type>(Scum.row(i))*vector<Type>(Pcum.row(i)))<<std::endl;   //no matching function for call to 'sum(const Eige::CwiseBinaryOp....

Any suggestions would be appreciated.

Thanks,

Mark

Kasper Kristensen

unread,
Jul 15, 2022, 5:15:36 AM7/15/22
to TMB Users
The confusion here is that a vector times vector becomes a new expression (not a vector). So, you have to explicitly cast to vector using something like

sum(vector<Type>(vector<Type>(Scum.row(i))*vector<Type>(Pcum.row(i))))

Alternatively you can choose to work with the expression templates and do

Scum.array().row(i).square().sum()

Note that 'array()' tells Eigen that you want to apply elementwise operations to your matrix rather than matrix operations.

mark...@gmail.com

unread,
Jul 15, 2022, 2:06:48 PM7/15/22
to TMB Users
Thanks Kasper,

That worked. It is kind of complicated, but I understand it. 

Regards,

Mark

Reply all
Reply to author
Forward
0 new messages