将两个矩阵细分,并用Rcpp计算内积?

时间:2019-02-15 21:56:13

标签: c++ rcpp

假设我有两个矩阵A和B,并且我想计算A [1,]和B [2,]的内积。使用R我可以编写这段代码

class Food(models.Model):
    name = models.CharField(max_length=100)
    grocery_aisle = models.ManytoManyField(Store, through='Location')

class Store(models.Model):
    name = models.CharField(max_length=100)
    aisles = { # Not really sure how to store this kind of information which would be different for each Store object.
        0: 'Produce Section',
        1: 'Aisle 1: bread and peanutbutter',
        2: 'Frozen Desserts',
        3: 'Pharmacy'
    }

class Location(models.Model):
    food = models.ForeignKey(Food, on_delete=models.CASCADE)
    store = models.ForeignKey(Store, on_delete=models.CASCADE)
    aisle = models.SmallIntegerField(choices=MEASUREMENT_CHOICES) # option descriptions depend on selected store object

我正在尝试学习如何使用set.seed(1982) A <- matrix(rnorm(12,2,1), ncol = 4) B <- matrix(rnorm(20,2,1), ncol = 4) a <- A[1,] b <- B[2,] a %*% b 19.63588 做同样的事情。我写了以下代码:

Rcpp

编译器告诉我它无法进行此转换:

#include <Rcpp.h>
#include <numeric>
using namespace Rcpp;


// [[Rcpp::export]]
double myInnerProduct(const NumericMatrix A,
                      const NumericMatrix B,
                      const int n,
                      const int i){
  std::vector<double> a = A(n,_) ;
  std::vector<double> b = B(i,_) ;
  double val = std::inner_product(a.begin(), a.end(), b.begin(), 0);
  return val;
}

/*** R
set.seed(1982)
A <- matrix(rnorm(12,2,1), ncol = 4)
B <- matrix(rnorm(20,2,1), ncol = 4)

a <- A[1,]
b <- B[2,]

a %*% b

myInnerProduct(A, B, 1, 2)
*/

我该如何解决?

0 个答案:

没有答案