[R-pkg-devel] How to get arbitrary precise inputs from R for an Rcpp package?

Vladimir Dergachev vo|ody@ @end|ng |rom m|nd@pr|ng@com
Fri Jul 19 07:17:44 CEST 2024



On Fri, 19 Jul 2024, Khue Tran wrote:

> I will need to run eigen() on a symmetric matrix, but I want to get arbitrary precise eigenvalues since we will need those eigenvalues for our further calculations. Does that make sense?

For a symmetric matrix there is a nice algorithm that is simple to 
implement and that converges fairly fast.

What you do is find the largest in absolute value off-diagonal entry.

The you construct 2x2 rotation matrix that zeros it: A --> R A R^-1, where 
R is a rotation matrix. The rotation matrix has mostly 1's on the main 
diagonal except for rows and columns of the large off-diagonal entry.

Repeat the procedure until the off-diagonal entries are as small as you 
want.

Because you are picking the largest value every time the algorithm is 
fairly stable.

The only disadvantage is that this algorithm plays havoc with sparse 
matrices by creating too many new non-zero entries, so for those you need 
to use a different method.

best

Vladimir Dergachev

> Best,
> Khue Tran
> 
> On Fri, Jul 19, 2024 at 12:14 PM Vladimir Dergachev <volodya using mindspring.com> wrote:
>
>       Do you need to run eigen() on an arbitrary matrix or symmetric one ?
>
>       best
>
>       Vladimir Dergachev
>
>       On Fri, 19 Jul 2024, Khue Tran wrote:
>
>       > Thank you Simon! This is very helpful! Regarding eigen, I found in the
>       > Boost library the following example for arbitrary precision matrix solver:
>       > https://github.com/boostorg/multiprecision/blob/develop/example/eigen_example.cpp.
>       > I am not sure if the precision is fully preserved throughout the process,
>       > but this example motivated me to try coding with the Boost library.
>       >
>       > Best,
>       > Khue Tran
>       >
>       > On Fri, Jul 19, 2024 at 9:50 AM Simon Urbanek <simon.urbanek using r-project.org>
>       > wrote:
>       >
>       >> Khue,
>       >>
>       >>
>       >>> On 19/07/2024, at 11:32 AM, Khue Tran <tran3 using kenyon.edu> wrote:
>       >>>
>       >>> Thank you for the suggestion, Denes, Vladimir, and Dirk. I have indeed
>       >>> looked into Rmpfr and while the package can interface GNU MPFR with R
>       >>> smoothly, as of right now, it doesn't have all the functions I need (ie.
>       >>> eigen for mpfr class) and when one input decimals, say 0.1 to mpfr(), the
>       >>> precision is still limited by R's default double precision.
>       >>>
>       >>
>       >>
>       >> Don't use doubles, use decimal fractions:
>       >>
>       >>> Rmpfr::mpfr(gmp::as.bigq(1,10), 512)
>       >> 1 'mpfr' number of precision  512   bits
>       >> [1]
>       >> 0.100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002
>       >>
>       >> As for eigen() - I'm not aware of an arbitrary precision solver, so I
>       >> think the inputs are your least problem - most tools out there use LAPACK
>       >> which doesn't support arbitrary precision so your input precision is likely
>       >> irrelevant in this case.
>       >>
>       >> Cheers,
>       >> Simon
>       >>
>       >>
>       >>
>       >>> Thank you for the note, Dirk. I will keep in mind to send any future
>       >>> questions regarding Rcpp to the Rcpp-devel mailing list. I understand
>       >> that
>       >>> the type used in the Boost library for precision is not one of the types
>       >>> supported by SEXP, so it will be more complicated to map between the cpp
>       >>> codes and R. Given Rmpfr doesn't provide all necessary mpfr calculations
>       >>> (and embarking on interfacing Eigen with Rmpfr is not a small task), does
>       >>> taking input as strings seem like the best option for me to get precise
>       >>> inputs?
>       >>>
>       >>> Sincerely,
>       >>> Khue
>       >>>
>       >>> On Fri, Jul 19, 2024 at 8:29 AM Dirk Eddelbuettel <edd using debian.org>
>       >> wrote:
>       >>>
>       >>>>
>       >>>> Hi Khue,
>       >>>>
>       >>>> On 19 July 2024 at 06:29, Khue Tran wrote:
>       >>>> | I am currently trying to get precise inputs by taking strings instead
>       >> of
>       >>>> | numbers then writing a function to decompose the string into a
>       >> rational
>       >>>> | with the denominator in the form of 10^(-n) where n is the number of
>       >>>> | decimal places. I am not sure if this is the only way or if there is a
>       >>>> | better method out there that I do not know of, so if you can think of
>       >> a
>       >>>> | general way to get precise inputs from users, it will be greatly
>       >>>> | appreciated!
>       >>>>
>       >>>> That is one possible way. The constraint really is that the .Call()
>       >>>> interface
>       >>>> we use for all [1] extensions to R only knowns SEXP types which map to a
>       >>>> small set of known types: double, int, string, bool, ...  The type used
>       >> by
>       >>>> the Boost library you are using is not among them, so you have to add
>       >> code
>       >>>> to
>       >>>> map back and forth. Rcpp makes that easier; it is still far from
>       >> automatic.
>       >>>>
>       >>>> R has packages such as Rmpfr interfacing GNU MPFR based on GMP. Maybe
>       >> that
>       >>>> is
>       >>>> good enough?  Also note that Rcpp has a dedicated (low volume and
>       >> friendly)
>       >>>> mailing list where questions such as this one may be better suited.
>       >>>>
>       >>>> Cheers, Dirk
>       >>>>
>       >>>> [1] A slight generalisation. There are others but they are less common /
>       >>>> not
>       >>>> recommended.
>       >>>>
>       >>>> --
>       >>>> dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org
>       >>>>
>       >>>
>       >>>       [[alternative HTML version deleted]]
>       >>>
>       >>> ______________________________________________
>       >>> R-package-devel using r-project.org mailing list
>       >>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>       >>>
>       >>
>       >>
>       >
>       >       [[alternative HTML version deleted]]
>       >
>       > ______________________________________________
>       > R-package-devel using r-project.org mailing list
>       > https://stat.ethz.ch/mailman/listinfo/r-package-devel
>       >
> 
> 
>


More information about the R-package-devel mailing list