-2

Hi I am new to rcpp and computing the inner product of two variables but getting an error "inner_product was not declared in this scope" for the following code:

#include <math.h>
#include <RcppCommon.h>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
// [[Rcpp::export]]
NumericVector polynomial_kernel(NumericVector x, NumericMatrix Y, double scale = 1, double offset = 
1, int d=1){

  int n = Y.nrow();
  NumericVector kernel(n);

  for (int j = 0; j < n; j++){
    NumericVector v = Y( j,_ );
    double crossProd =innerProduct(x,v);
    kernel[j]= pow((scale*crossProd+offset),2); 
  }
  return kernel;
}

Please help me to resolve this problem.

  • 4
    Where do you expect the function `inner_Product(Rcpp::NumericVector, Rcpp::NumericVector)` to come from? [`std::inner_product`](https://en.cppreference.com/w/cpp/algorithm/inner_product) has a different signature. BTW, is it `inner_Product` (code) or `inner_product` (error message)? Have you considered actually using Armadillo? – Ralf Stubner Nov 20 '19 at 06:31
  • Correction: it is an Rcpp function innerProduct(). I have made correction in code as well. However, I am getting the same error message. – sehrish sarfraz Nov 20 '19 at 09:23
  • 4
    The error message still mismatches your code. Please post the **actual** code and the **actual** error message. – Konrad Rudolph Nov 20 '19 at 09:28
  • the actual error is 'innerProduct' was not declared in this scope. The above code is what I am trying to run. Please let me know if there is some error in writing code. – sehrish sarfraz Nov 20 '19 at 09:35
  • 3
    And where do you expect the function `innerProduct(Rcpp::NumericVector, Rcpp::NumericVector)` to come from? AFAIK neither Rcpp nor RcppArmadillo provide it. And please [edit] your question to provide the actual error message. – Ralf Stubner Nov 20 '19 at 09:56

1 Answers1

3

Below is simpler, repaired version of your code that actually compiles. It uses Armadillo types for consistency, and instead of calling a non-existing "inner_product" routines computes the inner product of two vectors the standard way via multiplication.

#include <RcppArmadillo.h>      // also pulls in Rcpp.h amd cmath

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::vec polynomial_kernel(arma::vec x, arma::mat Y,
                            double scale = 1, double offset = 1, int d=1) {

  int n = Y.n_rows;
  arma::vec kernel(n);

  for (int j = 0; j < n; j++){
    arma::rowvec v = Y.row(j);
    double crossProd = arma::as_scalar(v * x);
    kernel[j] = std::pow((scale*crossProd+offset),2);
  }
  return kernel;
}

Your example was not a minimallyc complete verifiable example so I cannot show it any data you could have supplied with. On some made up data it seems to work:

R> set.seed(123)
R> polynomial_kernel(runif(4), matrix(rnorm(16),4))
         [,1]
[1,] 3.317483
[2,] 3.055690
[3,] 1.208345
[4,] 0.301834
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725