Since Armadillo (afaik) doesn't have a triangular solver, I'd like to use the LAPACK triangular solver available in dtrtrs
. I have looked at the following two (first, second) SO threads and pieced something together, but it isn't working.
I have created a fresh package using RStudio while also enabling RcppArmadillo. I have a header file header.h
:
#include <RcppArmadillo.h>
#ifdef ARMA_USE_LAPACK
#if !defined(ARMA_BLAS_CAPITALS)
#define arma_dtrtrs dtrtrs
#else
#define arma_dtrtrs DTRTRS
#endif
#endif
extern "C" {
void arma_fortran(arma_dtrtrs)(char* UPLO, char* TRANS, char* DIAG, int* N, int* NRHS,
double* A, int* LDA, double* B, int* LDB, int* INFO);
}
int trtrs(char uplo, char trans, char diag, int n, int nrhs, double* A, int lda, double* B, int ldb);
static int trisolve(const arma::mat &in_A, const arma::mat &in_b, arma::mat &out_x);
which essentially is the answer to the first linked question, with also a wrapper function and the main function. The meat of the functions go in trisolve.cpp
and is as follows:
#include "header.h"
int trtrs(char uplo, char trans, char diag, int n, int nrhs, double* A, int lda, double* B, int ldb) {
int info = 0;
wrapper_dtrtrs_(&uplo, &trans, &diag, &n, &nrhs, A, &lda, B, &ldb, &info);
return info;
}
static int trisolve(const arma::mat &in_A, const arma::mat &in_b, arma::mat &out_x) {
size_t rows = in_A.n_rows;
size_t cols = in_A.n_cols;
double *A = new double[rows*cols];
double *b = new double[in_b.size()];
//Lapack has column-major order
for(size_t col=0, D1_idx=0; col<cols; ++col)
{
for(size_t row = 0; row<rows; ++row)
{
// Lapack uses column major format
A[D1_idx++] = in_A(row, col);
}
b[col] = in_b(col);
}
for(size_t row = 0; row<rows; ++row)
{
b[row] = in_b(row);
}
int info = trtrs('U', 'N', 'N', cols, 1, A, rows, b, rows);
for(size_t col=0; col<cols; col++) {
out_x(col)=b[col];
}
delete[] A;
delete[] b;
return 0;
}
// [[Rcpp::export]]
arma::mat RtoRcpp(arma::mat A, arma::mat b) {
arma::uword n = A.n_rows;
arma::mat x = arma::mat(n, 1, arma::fill::zeros);
int info = trisolve(A, b, x);
return x;
}
There are (at least) two problems for me:
- When trying to compile, I get:
conflicting types for 'dtrtrs_'
from the header file. However, I don't see what is wrong with the inputs (and this is literally copied from the second linked thread). - Not unsurprisingly,
wrapper_dtrtrts_
is not correct. But from what I can tell from Armadillo'scompiler_setup.hpp
,arma_fortran
should create a function calledwrapper_dtrtrs_
for me. What is the name I should use here in the maincpp
file?