Skip to main content

extends the existing functionality of `itertools.product` by variable limits.

Project description

Description

general_cartesian_product extends the existing functionality of itertools.product by variable limits. It's possible to create cartesian products like:

   i1           i2             i3
[1, 2, 3] x [1, ..., i1] x [0, ..., i2] = 
[
    [1, 1, 0]
    [1, 1, 1]
    [2, 1, 0]
    [2, 1, 1]
    [2, 2, 0]
    [2, 2, 1]
    [2, 2, 2]
    [3, 1, 0]
    [3, 1, 1]
    [3, 2, 0]
    [3, 2, 1]
    [3, 2, 2]
    [3, 3, 0]
    [3, 3, 1]
    [3, 3, 2]
    [3, 3, 3]
]

via this config file:

rules = {
    'i1': { 'start': 1, 'end': 3, },
    'i2': { 'start': 1, 'end': i1, },
    'i3': { 'end': i2, },
}

Obviously this example is quite useless, because its the same as [1,2,3]x[1,2,3]x[0,1,2,3] The whole thing becomes interesting if we have problems like this:

[1, 2, 3] x [1, ..., 3-i1] x [0, ..., 3-i2] = 
[
    [1, 1, 0]
    [1, 1, 1]
    [1, 1, 2]
    [1, 2, 0]
    [1, 2, 1]
    [2, 1, 0]
    [2, 1, 1]
    [2, 1, 2]
]

which is generated by this configuration:

rules = {
    'i1': {'start': 1, 'end': 3, },
    'i2': {'start': 1, 'end': 3-i1, },
    'i3': {'end': 3-i2, },
}

Limitations

Currently the only known limitation is that there must be always at least one constant limit. This means that the output of the cartesian product function will always be finite. To create 'infinite' generator like cartesian products a complete rewrite of this package is probably needed.

Installation

This package depends on sympy (or sage symbolic variables e.g.:i = var('i'))

pip install generalcartesianproduct sympy

Usage

sympy Example:

from sympy import symbols
from generalcartesianproduct.general_cartesian_product import *
i1, i2, u, v = symbols('i1,i2,u,v')

general_cartesian_product([i1, i2, i3])

sage Example:

from generalcartesianproduct.general_cartesian_product import *
m = 4
i1, i2, u, v = var('i1,i2,u,v')
rules = {
    'i1': { 'end': m, },
    'i2': { 'end': m, },
    'u':  { 'end': min_symbolic(m-i1, m-i2), },
    'v':  { 'end': min_symbolic(m-i1, m-i2)-u, }
}
cp = general_cartesian_product([i1, i2, i3], rules)
print(cp)

How to use the rules dictionary.

# simple example with only one dependency
rules = {
    'i1': { 'end': m, },
    'i2': { 'end': m-i1, },
}

TODO

  • better logging
  • add exceptions
  • make start, end accesable via kargs

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

GeneralCartesianProduct-0.1.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

GeneralCartesianProduct-0.1.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file GeneralCartesianProduct-0.1.1.tar.gz.

File metadata

  • Download URL: GeneralCartesianProduct-0.1.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for GeneralCartesianProduct-0.1.1.tar.gz
Algorithm Hash digest
SHA256 556864427ebd4f93d90a00deca273c40ae21634f90be5b1cda07cb8172dbf90f
MD5 fd206bacb75577b002b9835aeef12bc7
BLAKE2b-256 6df7a64696e2ff795c900b7238af7ab7f37d34a3bc80bf613c941201f925431c

See more details on using hashes here.

File details

Details for the file GeneralCartesianProduct-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: GeneralCartesianProduct-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for GeneralCartesianProduct-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b0bbc7154b09098a7735b6b77199b5203c29519c97a37690c656f9adee35b33c
MD5 b164378172943eed9a9c08f74771ca83
BLAKE2b-256 cf399d45f31225bb399f3713bc1654854b525fccd8bb395557d756b1406d3b34

See more details on using hashes here.

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