Skip to content

Commit 40cb7c6

Browse files
committed
Expand README.md with another example
1 parent 4d9c029 commit 40cb7c6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2025-11-22 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* README.md: Add a `Rcpp::cppFunction()` example
4+
15
2025-11-20 Dirk Eddelbuettel <edd@debian.org>
26

37
* DESCRIPTION (Version, Date): RcppArmadillo 15.2.2-0

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ precludes some more compile-time heavy features such as 'Rcpp Modules' which we
6262
the [Rcpp][rcpp] docs more details about 'Light', 'Lighter' and 'Lightest'. In the example above,
6363
the switch saves about 15% of total compilation time.
6464
65+
You can also experiment directly at the R prompt. The next example creates a one-liner to access the
66+
Armadillo `hess()` method to compute a [Hessenberg
67+
matrix](https://en.wikipedia.org/wiki/Hessenberg_matrix), a decomposition into an 'almost
68+
triangular' matrix. (We chose this example as base R does not have this functionality; it is a more
69+
interesting example than providing yet another QR, SVD, or Eigen decomposition though these are of
70+
course equally easily accessible from Armadillo.)
71+
72+
```r
73+
> library(Rcpp) # for cppFunction()
74+
> cppFunction("arma::mat hd(const arma::mat& m) { return hess(m); }", depends="RcppArmadillo")
75+
> set.seed(20251122)
76+
> m <- matrix(rnorm(49), 7, 7)
77+
> hd(m) # compute Hessenberg decom using the function we just created
78+
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
79+
[1,] 1.15745 1.64589 -0.809926 0.220417 -1.6050835 -0.947760 -0.972432
80+
[2,] 2.80695 -2.65613 0.119320 0.484858 -0.0877464 0.913440 -0.488142
81+
[3,] 0.00000 1.95335 -1.436409 1.879113 -0.1532293 -0.532077 -0.319914
82+
[4,] 0.00000 0.00000 -1.253221 -1.694414 0.2497111 1.233231 1.055706
83+
[5,] 0.00000 0.00000 0.000000 -3.333499 -1.4800146 -0.913523 -0.667435
84+
[6,] 0.00000 0.00000 0.000000 0.000000 -1.2050704 -0.294369 1.254402
85+
[7,] 0.00000 0.00000 0.000000 0.000000 0.0000000 0.324987 -1.504329
86+
>
87+
```
88+
89+
Note how we can once again access an Armadillo matrix via the `mat` class in the `arma`
90+
namespace. Here `mat` is a convenience shortcut for a matrix of type `double`, there is also `imat`
91+
for an integer matrix and more, see the Armadillo documentation. We tell `Rcpp::cppFunction()` about
92+
Armadillo via the `depends=` argument, it allows it use information the `RcppArmadillo` package
93+
provides about its header files etc.
94+
95+
The convenience of an automatic mapping from R directly into the Armadillo classes is a key
96+
advantage. The package also permits the standard 'const reference' calling semantic ensuring
97+
efficient transfer and signalling that the variable will not be altered.
98+
99+
65100
### Status
66101

67102
The package is mature yet under active development with releases to [CRAN][cran] about once every

0 commit comments

Comments
 (0)