Last active
February 6, 2020 19:48
-
-
Save gngdb/fb6e3d330af60474cd7cd92d214f4b85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"I want to produce the value at a specific index in the output of a cartesian product, without having to iterate my way to that value. As long as the arrays going into the cartesian product are the same length, this seems to be just a base conversion problem." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[0, 0],\n", | |
" [0, 1],\n", | |
" [0, 2],\n", | |
" [1, 0],\n", | |
" [1, 1],\n", | |
" [1, 2],\n", | |
" [2, 0],\n", | |
" [2, 1],\n", | |
" [2, 2]])\n" | |
] | |
} | |
], | |
"source": [ | |
"# concretely\n", | |
"x = torch.arange(3)\n", | |
"y = torch.arange(3)\n", | |
"prod = torch.cartesian_prod(x, y)\n", | |
"print(prod)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([1, 2])" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# I want to produce the value at index `i`, without computing the array `prod`\n", | |
"i = 5\n", | |
"prod[5]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"5 is 12 in base 3." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([3, 1])\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([1, 2])" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"b = 3**(1-torch.arange(2))\n", | |
"print(b)\n", | |
"(i//b)%3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Defining an arbitrarily large cartesian product:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Length of cartesian product: 13841287201\n" | |
] | |
} | |
], | |
"source": [ | |
"k, n = 7, 12\n", | |
"tensors = [torch.arange(k) for i in range(n)]\n", | |
"print(\"Length of cartesian product: \", k**n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# index element halfway through this cartesian product\n", | |
"i = (k**n)//2\n", | |
"b = k**((n-1) - torch.arange(n))\n", | |
"basek = (i//b)%k\n", | |
"basek" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor(0)" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(basek*b).sum() - i" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment