Skip to article frontmatterSkip to article content

Symbol

A Symbol is a GTSAM class used to create semantically meaningful keys (gtsam.Key) for variables. It combines a character (unsigned char) and an index (uint64_t) into a single 64-bit integer key. This allows for easy identification of variable types and their indices, e.g., ‘x’ for poses and ‘l’ for landmarks, like x0, x1, l0.

Open In Colab

import gtsam
from gtsam import Symbol

Initialization

A Symbol can be created by providing a character and an index. It can also be created by decoding an existing gtsam.Key (integer).

# Create Symbol 'x' with index 5
sym1 = Symbol('x', 5)
print(f"Symbol from char/index: {sym1.string()}")

# Get the underlying integer key
key1 = sym1.key()

# Reconstruct Symbol from the key
sym2 = Symbol(key1)
print(f"Symbol from key {key1}: {sym2.string()}")
Symbol from char/index: x5
Symbol from key 8646911284551352325: x5

Properties and Usage

You can access the character, index, and underlying integer key.

landmark_sym = Symbol('l', 10)

print(f"Symbol: {landmark_sym.string()}")
print(f"  Char: {landmark_sym.chr()}")
print(f"  Index: {landmark_sym.index()}")
print(f"  Key: {landmark_sym.key()}")

# Symbols are often used directly where Keys are expected in GTSAM functions,
# as they implicitly convert.
# e.g., values.insert(landmark_sym, gtsam.Point3(1,2,3))
Symbol: l10
  Char: 108
  Index: 10
  Key: 7782220156096217098

Shorthand Functions

GTSAM provides convenient shorthand functions X(j), L(j), etc., which are equivalent to gtsam.Symbol('x', j), gtsam.Symbol('l', j). To use these, first import with from gtsam import symbol_shorthand, then set up the variables you want to use with statements like X = symbol_shorthand.X.

from gtsam import symbol_shorthand

X = symbol_shorthand.X
L = symbol_shorthand.L

print(f"Symbol('x', 0).key() == X(0): {Symbol('x', 0).key() == X(0)}")
print(f"Symbol('l', 1).key() == L(1): {Symbol('l', 1).key() == L(1)}")
Symbol('x', 0).key() == X(0): True
Symbol('l', 1).key() == L(1): True