I need to do a matrix * vector multiplication, which generates only a single number, and pass it to a vector (but it doesn't work in the commented code below). What I have so far is:
cpp code
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_MATRIX(U); // id x 2 matrix
DATA_MATRIX(Z); // n x 2 matrix
matrix<Type> Z1_m1 = matrix<Type>(Z.row(0))*vector<Type>(U.row(0));
REPORT(Z1_m1); // works
vector<Type> ZZ(6);
// ZZ(0) = matrix<Type>(Z.row(0))*vector<Type>(U.row(0)); // HOW TO FIX IT??
// (This is a small code, which the real application run inside a for loop, and the row indices will be given by for)
REPORT(ZZ);
return 0;
}
R code
require(TMB)
set.seed(232)
model_data = list(U = matrix(c(9,11,2,4), ncol = 2),
Z = matrix(c(1,2,3,4,5,6, rpois(6,2)), ncol=2))
model <- "mult"
compile(paste0(model, ".cpp"))
dyn.load(dynlib(model))
m1 = MakeADFun(data=model_data, parameters=list(),type="Fun",
checkParameterOrder=FALSE,DLL=model)
print(m1$report()) # Note: order of variables NOT the same as .cpp file
Any help?
I have already posted this question into TMB users group. If the solution appears first there, I will post it here.
I will post here my sollution after @Bob help in TMB Users group:
This is inefficient because it is an array element-wise multiplication. If it was a matrix multiplication would run faster.