-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
72 lines (54 loc) · 2.06 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
output:
github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
library(knitr)
library(pcg2)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
local({
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
x = paste(x, collapse = '.')
if (!grepl('\\.svg', x)) return(hook_plot(x, options))
# read the content of the svg image and write it out without <?xml ... ?>
paste0('<img src= "./', x, '">')
})
})
```
# pcg2
[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing)
[![Travis build status](https://travis-ci.org/natbprice/pcg2.svg?branch=master)](https://travis-ci.org/natbprice/pcg2)
There are several R packages that implement the preconditioned conjugate
gradients (pcg) method, but none of them allow for the passage of a function handle
in place of the matrix vector products A\*x. This functionality is available in Matlab
via the [`pcg`](https://www.mathworks.com/help/matlab/ref/pcg.html) function
and in Python via the [`scipy.sparse.linalg.cg`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.cg.html) and [`scipy.sparse.linalg.LinearOperator`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.LinearOperator.html) functions. This package is a simple implementation
of the pcg method allowing the user to pass a function handle in place of the
matrix vector product A\*x.
Other pcg packages in R:
* [pcg](https://cran.r-project.org/package=pcg)
* [Rlinsolve](https://cran.r-project.org/package=Rlinsolve)
* [cPCG](https://cran.r-project.org/package=cPCG)
## Installation
The **pcg2** package is currently only available from Github.
```{r, eval = FALSE}
devtools::install_github("natbprice/pcg2")
```
## Example
```{r}
A <- matrix(c(4, 1, 1, 3), nrow = 2)
b <- c(1, 2)
x0 <- c(2, 1)
Ax <- function(x) {
A %*% x
}
M <- matrix(c(4, 0, 0, 3), nrow = nrow(A))
pcg(Ax, b, M, x0)
```