Skip to main content

Linear Algebra routines for Python as implemented in the R language.

Project description

®️ rlinalg Stars

Linear Algebra routines for Python as implemented in the R language.

Actions Coverage License PyPI Bioconda Wheel Python Versions Python Implementations Source Mirror GitHub issues Docs Changelog Downloads

🗺️ Overview

The R Project for Statistical Computing provides an environment for using and developing statistical methods. Most of the array manipulation and linear algebra routines are implemented using LAPACK, which can be accessed in Python using SciPy and NumPy.

However, when trying to port and reproduce code from R in Python, one can notice differences in the implementation of several routines, in particular in the QR decomposition with pivoting enabled:

> mat <- t(matrix(seq_len(9), nrow=3))
> qr.Q(mat)
           [,1]       [,2]       [,3]
[1,] -0.1230915  0.9045340  0.4082483
[2,] -0.4923660  0.3015113 -0.8164966
[3,] -0.8616404 -0.3015113  0.4082483
>>> mat = numpy.arange(1, 10).reshape(3, 3)
>>> scipy.linalg.qr(mat, pivoting = True)[0]
array([[-0.2672612  0.8728716  0.4082483]
       [-0.5345225  0.2182179 -0.8164966]
       [-0.8017837 -0.4364358  0.4082483]])

The culprit here is the qr function from R not using LAPACK dgeqp3 by default, but a modified R-specific version of the LINPACK dqrdc routine (dqrdc2) that optimizes the pivoting strategy. This means that code using qr in R will behave differently than an equivalent Python using LAPACK, and there was (until now) no way to reproduce the R behaviour.

The rlinalg library provides linear algebra routines from R using the Fortran sources to allow reproducibility. It exposes an API similar to the scipy interface for similar functions (qr, kappa, lstsq), which can be used to get the same results as R:

>>> mat = numpy.arange(1, 10).reshape(3, 3)
>>> rlinalg.qr(mat).Q.round(7)
array([[-0.1230915  0.904534   0.4082483]
       [-0.492366   0.3015113 -0.8164966]
       [-0.8616404 -0.3015113  0.4082483]])

This library depends on NumPy, and on the BLAS libraries available on the system. It is available for all modern Python versions (3.7+). Building is done with Meson and requires a Fortran compiler when compiling from source.

🔧 Installing

Install the rlinalg package directly from PyPi which hosts universal wheels that can be installed with pip:

$ pip install rlinalg

💭 Feedback

⚠️ Issue Tracker

Found a bug? Have an enhancement request? Head over to the GitHub issue tracker if you need to report or ask something. If you are filing in on a bug, please include as much information as you can about the issue, and try to recreate the same bug in a simple, easily reproducible situation.

📋 Changelog

This project adheres to Semantic Versioning and provides a changelog in the Keep a Changelog format.

⚖️ License

This library is provided under the GNU General Public License v3.0 or later. It includes some code redistributed from the R language, which is licensed under the GNU General Public License v2.0 or later. Some tests were adapted from SciPy, which is developed under the BSD-3-clause license.

This project is in no way not affiliated, sponsored, or otherwise endorsed by the R project. It was developed by Martin Larralde during his PhD project at the Leiden University Medical Center in the Zeller lab.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rlinalg-0.1.0.tar.gz (15.9 MB view hashes)

Uploaded Source

Built Distributions

rlinalg-0.1.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (735.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (734.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (734.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (733.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (735.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (734.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (734.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (734.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (734.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

rlinalg-0.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (733.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page