The permanent of a (square) matrix, like the determinant is a polynomial in the entries of the matrix. Unlike the determinant, the signatures of the permutations are not taken into account making the permanent much more difficult to compute because decomposition methods cannot be used.
The permanent commonly appears in problems related to quantum mechanics, and the most common brute-force combinatorial method has time complexity
This library aims to solve the need for an efficient library that solves the permenent of a given matrix.
permanent.opt()
Compute the permanent of a matrix using the best algorithm for the shape of the given matrix.
Parameters:
matrix
:np.ndarray(M, N, dtype=(np.double|np.complex))
Returns:
permanent
:(np.double|np.complex)
- Permanent of matrix.
permanent.combinatoric()
Compute the permanent of a matrix combinatorically.
Formula:
Parameters:
matrix
:np.ndarray(M, N, dtype=(np.double|np.complex))
Returns:
permanent
:(np.double|np.complex)
- Permanent of matrix.
permanent.glynn()
Formula:
Additional Information:
The original formula has been generalized here to work with
This can be neatly fit into the original formula by extending the inner sums over
Parameters:
matrix
:np.ndarray(M, N, dtype=(np.double|np.complex))
Returns:
permanent
:(np.double|np.complex)
- Permanent of matrix.
permanent.ryser()
Formula:
Parameters:
matrix
:np.ndarray(M, N, dtype=(np.double|np.complex))
Returns:
permanent
:(np.double|np.complex)
- Permanent of matrix.
The permanent package allows you to solve the permanent of a given matrix using the optimal algorithm for your matrix dimensions. You can either use the pre-defined parameters or fine tune them to your machine.
-
Install Python on your machine. Depending on your operating system, the instructions may vary.
-
Install gcc on your machine. Depending on your operating system, the instructions may vary.
-
Create and activate a virtual environment for this project named
permanents
. One way to do this is with pip.pip install virtualenv virtualenv permanents
-
Activate the virtual environment.
source permanents/bin/activate
-
Install Sphinx and other dependencies.
pip install sphinx sphinx-rtd-theme sphinx-copybutton
-
Install Python dependencies.
pip install numpy pandas scikit-learn
-
(Optional) Install Pytest if you wish to run tests.
pip install pytest
Now that you have your environment set up and activated you are ready to compile the source code into an executable. Here you have two options - compile the code as is with the pre-defined parameters for algorithm swapping, or compile the code with machine specific tuning for algorithm swapping. Note that machine specific tuning will run a series of tests. This will take anywhere from 10 minutes to 1 hour depending on your system.
-
Compile the permanent code (natively for your CPU architecture).
make BUILD_NATIVE=1
Note: if using M1 architecture, or want a portable build, simply run the following.
make
-
(Optional) Run tests on the algorithms.
make test
-
Compile the website.
cd docs && make html
-
Load the website.
open build/html/index.html
-
Compile the permanent code with the
tuning
flag.make RUN_TUNING=1
Note: it will take some time to run the tuning tests on your machine.
-
(Optional) Run tests on the algorithms.
make test
-
Compile the website.
cd docs && make html
-
Load the website using your web browser.
<browser> build/html/index.html
The Makefile in this project is used to compile C and Python libraries and includes rules for installation, testing, and cleaning. Here's a breakdown of its sections:
- Variables:
CXX
,AR
,PYTHON
: Define compiler, archiver, and Python executable.CXXFLAGS
: Compiler flags including C++ version, warnings, debugging, optimization, and platform-specific options.
- Conditional Compilation:
-
ifeq ($(shell uname -s),Darwin)
: Additional flags for macOS. -
ifneq ($(BUILD_NATIVE),)
: Optimization flags if building for native architecture. -
ifneq ($(RUN_TUNING),)
: Flag for runtime tuning. -
ifeq ($(PREFIX),)
: Default installation prefix.
- Targets:
all
,c
,python
: Phony targets for building all, C, or Python libraries.install
: Installs C libraries and headerstest
: Runs tests using pytest.clean
: Removes generated files.
- File generation:
compile_flags.txt
: Generates compilation flags for clangd.src/tuning.h
: Generates tuning parameters header file.
- Compilation Rules:
permanent/permanent.so
: Compiles Python extension module.src/libpermanent.o
: Compiles object code.libpermanent.a
,libpermanent.so
: Compiles static and shared C libraries respectively.
This code is distributed under the GNU General Public License version 3 (GPLv3). See http://www.gnu.org/licenses/ for more information.
The following programs/libraries are required to compile this package:
- Python (≥3.6)
- gcc (≥11.4)
- Sphinx (≥7.2)
- sphinx-rtd-theme (≥2.0)
- NumPy (≥1.13)
- pandas (≥2.2)
- scikit-learn (≥1.4)
- Pytest (optional: to run tests)