https://github.com/const-ae/einsum
Einstein Summation for Arrays in R
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (16.6%) to scientific vocabulary
Last synced: 10 months ago
·
JSON representation
Repository
Einstein Summation for Arrays in R
Basic Info
- Host: GitHub
- Owner: const-ae
- License: other
- Language: R
- Default Branch: master
- Homepage: https://const-ae.github.io/einsum/
- Size: 89.8 KB
Statistics
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
- Releases: 2
Created about 6 years ago
· Last pushed almost 3 years ago
Metadata Files
Readme
Changelog
License
README.Rmd
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(1)
```
# einsum
[](https://github.com/const-ae/einsum/actions)
[](https://app.codecov.io/gh/const-ae/einsum?branch=master)
[Einstein summation](https://en.wikipedia.org/wiki/Einstein_notation) is a concise mathematical notation that
implicitly sums over repeated indices of n-dimensional arrays. Many ordinary
matrix operations (e.g., transpose, matrix multiplication, scalar product, 'diag()', trace, etc.)
can be written using Einstein notation. The notation is particularly convenient for
expressing operations on arrays with more than two dimensions because the
respective operators ('tensor products') might not have a standardized name.
## Installation
You can install the package from [CRAN](https://CRAN.R-project.org/package=einsum) with:
``` r
install.packages("einsum")
```
or if you want to use the development version from [GitHub](https://github.com/einsum):
``` r
# install.packages("devtools")
devtools::install_github("const-ae/einsum")
```
## Example
Load the package:
```{r}
library(einsum)
```
Let's make two matrices:
```{r}
mat1 <- matrix(rnorm(n = 8 * 4), nrow = 8, ncol = 4)
mat2 <- matrix(rnorm(n = 4 * 4), nrow = 4, ncol = 4)
```
We can use `einsum()` to calculate the matrix product
```{r}
einsum("ij, jk -> ik", mat1, mat2)
```
which produces the same as the standard matrix multiplication
```{r}
mat1 %*% mat2
```
The matrix multiplication example is straightforward, and there is little benefit of using the Einstein notation over the more familiar matrix product expression. Furthermore, 'einsum' is a lot slower.
However, 'einsum' truly shines when working with more than 2-dimensional arrays, where it can be difficult to figure out the correct kind of tensor product:
```{r}
# Make three n-dimensional arrays
arr1 <- array(rnorm(3 * 9 * 2), dim = c(3, 9, 2))
arr2 <- array(rnorm(2 * 5), dim = c(2, 5))
arr3 <- array(rnorm(9 * 3), dim = c(9, 3))
# Sum across axes a, b, and c
einsum("abc, cd, ba -> d", arr1, arr2, arr3)
```
The equivalent expression using tensor products (which are not intuitive) would look like this:
```{r}
tensor::tensor(tensor::tensor(arr1, arr2, alongA = 3, alongB = 1), arr3, alongA = c(2,1), alongB = c(1, 2))
```
If you need to do the same computation repeatedly, you can use `einsum_generator()`, which generates and compiles an efficient C++ function for that calculation (to see the function code, set `compile_function=FALSE`). It can take a few seconds to compile the function, but the returned function can be one or two orders of magnitude faster than `einsum()`.
```{r}
# einsum_generator returns a function
array_prod <- einsum_generator("abc, cd, ba -> d")
array_prod(arr1, arr2, arr3)
```
```{r}
bench::mark(
tensor = tensor::tensor(tensor::tensor(arr1, arr2, alongA = 3, alongB = 1),
arr3, alongA = c(2,1), alongB = c(1, 2)),
einsum = einsum("abc, cd, ba -> d", arr1, arr2, arr3),
einsum_generator = array_prod(arr1, arr2, arr3)
)
```
Lastly, you can also generate C++ code if you need an efficient implementation of some function, which you could (with proper credit) for example paste into your R package:
```{r}
# The C++ code underlying the tensor product
cat(einsum_generator("abc, cd, ba -> d", compile_function = FALSE))
```
# Credit
This package is inspired by the [einsum](https://numpy.org/doc/stable/reference/generated/numpy.einsum.html) function in NumPy.
Owner
- Name: Constantin
- Login: const-ae
- Kind: user
- Location: Heidelberg, Germany
- Company: EMBL
- Website: https://twitter.com/const_ae
- Repositories: 64
- Profile: https://github.com/const-ae
PhD Student, Biostats, R
GitHub Events
Total
- Watch event: 2
Last Year
- Watch event: 2
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- DanielBonnery (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- cran 398 last-month
- Total docker downloads: 683
- Total dependent packages: 2
- Total dependent repositories: 3
- Total versions: 2
- Total maintainers: 1
cran.r-project.org: einsum
Einstein Summation
- Homepage: https://const-ae.github.io/einsum/
- Documentation: http://cran.r-project.org/web/packages/einsum/einsum.pdf
- License: MIT + file LICENSE
-
Latest release: 0.1.2
published almost 3 years ago
Rankings
Dependent packages count: 13.7%
Dependent repos count: 16.5%
Stargazers count: 19.3%
Forks count: 21.0%
Average: 21.7%
Docker downloads count: 23.1%
Downloads: 36.2%
Maintainers (1)
Last synced:
11 months ago
Dependencies
DESCRIPTION
cran
- Rcpp * imports
- glue * imports
- mathjaxr * imports
- covr * suggests
- testthat * suggests
.github/workflows/R-CMD-check.yaml
actions
- actions/cache v1 composite
- actions/checkout v2 composite
- actions/upload-artifact master composite
- r-lib/actions/setup-pandoc master composite
- r-lib/actions/setup-r master composite
.github/workflows/pkgdown.yaml
actions
- actions/cache v1 composite
- actions/checkout v2 composite
- r-lib/actions/setup-pandoc master composite
- r-lib/actions/setup-r master composite