gtsam/gtsam/inference/doc/EdgeKey.ipynb

171 lines
4.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "edgekey_intro_md"
},
"source": [
"# EdgeKey"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "edgekey_desc_md"
},
"source": [
"An `EdgeKey` is a utility class in GTSAM used to encode a pair of 32-bit unsigned integers into a single 64-bit `gtsam.Key`. This can be useful for representing edges in a graph or other paired relationships where each element of the pair fits within 32 bits."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "edgekey_colab_md"
},
"source": [
"<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/inference/doc/EdgeKey.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "edgekey_pip_code",
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"%pip install --quiet gtsam-develop"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "edgekey_import_code"
},
"outputs": [],
"source": [
"import gtsam\n",
"from gtsam import EdgeKey"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "edgekey_init_md"
},
"source": [
"## Initialization\n",
"\n",
"An `EdgeKey` can be created by providing two 32-bit unsigned integers (`i` and `j`). It can also be created by decoding an existing `gtsam.Key` (integer), assuming it was encoded using the `EdgeKey` format."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "edgekey_create_code",
"outputId": "cdef1234-5678-90ab-cdef-1234567890ab"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"EdgeKey from (10, 20): {10, 20}\n",
"\n",
"EdgeKey from key 42949672980: {10, 20}\n",
"\n"
]
}
],
"source": [
"# Create EdgeKey from integers i=10, j=20\n",
"ekey1 = EdgeKey(10, 20)\n",
"print(f\"EdgeKey from (10, 20): {ekey1}\") # Uses __str__ which calls operator std::string\n",
"\n",
"# Get the underlying integer key\n",
"key1 = ekey1.key()\n",
"\n",
"# Reconstruct EdgeKey from the key\n",
"ekey2 = EdgeKey(key1)\n",
"print(f\"EdgeKey from key {key1}: {ekey2}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "edgekey_props_md"
},
"source": [
"## Properties and Usage\n",
"\n",
"You can access the original `i` and `j` values and the combined `Key`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "edgekey_access_code",
"outputId": "def12345-6789-0abc-def1-234567890abc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"EdgeKey: {123, 456}\n",
"\n",
" i: 123\n",
" j: 456\n",
" Key: 528280977864\n"
]
}
],
"source": [
"edge = EdgeKey(123, 456)\n",
"\n",
"print(f\"EdgeKey: {edge}\")\n",
"print(f\" i: {edge.i()}\")\n",
"print(f\" j: {edge.j()}\")\n",
"print(f\" Key: {edge.key()}\")"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "gtsam",
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}