https://github.com/alexprengere/product
Cartesian product with objective function
https://github.com/alexprengere/product
math product python
Last synced: 9 months ago
JSON representation
Cartesian product with objective function
- Host: GitHub
- URL: https://github.com/alexprengere/product
- Owner: alexprengere
- License: apache-2.0
- Created: 2015-12-06T15:51:27.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-03-14T11:03:27.000Z (over 3 years ago)
- Last Synced: 2025-01-14T10:18:52.924Z (11 months ago)
- Topics: math, product, python
- Language: Python
- Size: 14.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# product
Cartesian product with an objective function to minimize.
By default, the sum of returned elements.
```python
>>> from product import product
>>> for t in product([[0, 1, 2], [0, 1, 2]]):
... print(t)
(0, 0)
(0, 1)
(1, 0)
(0, 2)
(1, 1)
(2, 0)
(1, 2)
(2, 1)
(2, 2)
```
Now let's try with `sqrt` as objective function.
Note that `(1, 1)` is now after `(0, 2)` and `(2, 0)`.
```python
>>> from math import sqrt
>>> for t in product([[0, 1, 2], [0, 1, 2]], key=sqrt):
... print(t)
(0, 0)
(0, 1)
(1, 0)
(0, 2)
(2, 0)
(1, 1)
(1, 2)
(2, 1)
(2, 2)
```
Change that to the square.
Note that `(1, 1)` is now before `(0, 2)` and `(2, 0)`.
```python
>>> for t in product([[0, 1, 2], [0, 1, 2]], key=lambda i: i**2):
... print(t)
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(0, 2)
(2, 0)
(1, 2)
(2, 1)
(2, 2)
```