Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions gtsam/linear/doc/Factors.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e08a484b",
"metadata": {},
"source": [
"# Factors (Jacobian / Hessian / Regular)\n",
"\n",
"This notebook groups the various factor types (JacobianFactor, BinaryJacobianFactor, HessianFactor, RegularHessianFactor, RegularJacobianFactor) together, showing construction patterns and small numpy examples that illustrate each concept."
]
},
{
"cell_type": "markdown",
"id": "3bef0e5a",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/linear/doc/Factors.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,
"id": "bd07b6dc",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" import google.colab\n",
" %pip install --quiet gtsam\n",
"except ImportError:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b52f6b46",
"metadata": {},
"outputs": [],
"source": [
"import gtsam\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "4fc0b36e",
"metadata": {},
"source": [
"## JacobianFactor and BinaryJacobianFactor\n",
"\n",
"Jacobian factors represent linear rows; binary variants connect two variable blocks. We try to construct them and show a numpy least-squares example."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "605445f9",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" jf = gtsam.JacobianFactor()\n",
" bjf = gtsam.BinaryJacobianFactor()\n",
"except Exception as e:\n",
" jf = None\n",
" bjf = None\n",
" print('JacobianFactor/BinaryJacobianFactor not available in Python bindings:', e)\n",
"\n",
"# LS example (numpy)\n",
"J = np.array([[1.0, 2.0],[3.0,4.0]])\n",
"b = np.array([1.0,2.0])\n",
"x, residuals, rank, s = np.linalg.lstsq(J, b, rcond=None)\n",
"print('Least-squares (numpy):', x)\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "6bfe3cd8",
"metadata": {},
"source": [
"## HessianFactor and RegularHessianFactor\n",
"\n",
"Hessian factors work in information form. We try to construct them and show a small MAP solve via numpy."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6f411bd",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" hf = gtsam.HessianFactor()\n",
" rhf = gtsam.RegularHessianFactor()\n",
"except Exception as e:\n",
" hf = None\n",
" rhf = None\n",
" print('HessianFactor/RegularHessianFactor not available in Python bindings:', e)\n",
"\n",
"# MAP solve (numpy)\n",
"H = np.array([[4.0,0.0],[0.0,2.0]])\n",
"g = np.array([-2.0,-2.0])\n",
"x = np.linalg.solve(H, -g)\n",
"print('MAP estimate (numpy):', x)\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "e2e3d4a8",
"metadata": {},
"source": [
"## RegularJacobianFactor\n",
"\n",
"Regular Jacobian factors have block regular structure; we include a small numpy example for least-squares."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af4f4b9c",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" rjf = gtsam.RegularJacobianFactor()\n",
"except Exception as e:\n",
" rjf = None\n",
" print('RegularJacobianFactor not available in Python bindings:', e)\n",
"\n",
"# Example (numpy)\n",
"J = np.eye(2)\n",
"b = np.array([0.5,-0.5])\n",
"x, *_ = np.linalg.lstsq(J, b, rcond=None)\n",
"print('LS (numpy):', x)\n",
"x"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
222 changes: 222 additions & 0 deletions gtsam/linear/doc/GaussianOverview.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "65e5517c",
"metadata": {},
"source": [
"# Gaussian Overview\n",
"\n",
"This notebook combines the Gaussian-related notebooks (Bayes nets, Bayes trees, conditionals, densities, elimination trees, junction trees, ISAM, factors, and factor graphs) into one consolidated guide. It follows the style of the existing documentation notebooks and includes guarded Python examples that fall back to numpy where bindings are not available."
]
},
{
"cell_type": "markdown",
"id": "58a2fcf2",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/linear/doc/GaussianOverview.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,
"id": "5c13d0b9",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" import google.colab\n",
" %pip install --quiet gtsam\n",
"except ImportError:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1692f36a",
"metadata": {},
"outputs": [],
"source": [
"import gtsam\n",
"import numpy as np\n",
"import graphviz"
]
},
{
"cell_type": "markdown",
"id": "742a3727",
"metadata": {},
"source": [
"## GaussianFactor and GaussianFactorGraph\n",
"\n",
"`GaussianFactor` objects contribute linear rows to a system; `GaussianFactorGraph` collects them and can solve the resulting linear system. Below we show guarded construction and a small numpy example."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e861ce74",
"metadata": {},
"outputs": [],
"source": [
"# Guarded construction examples\n",
"try:\n",
" gf = gtsam.GaussianFactor()\n",
" gfg = gtsam.GaussianFactorGraph()\n",
"except Exception as e:\n",
" gf = None\n",
" gfg = None\n",
" print('GaussianFactor/GaussianFactorGraph not available in Python bindings:', e)\n",
"\n",
"# Numpy example: form and solve a small system\n",
"A = np.array([[3.0, -1.0], [-1.0, 2.0]])\n",
"b = np.array([1.0, 0.0])\n",
"x = np.linalg.solve(A, b)\n",
"print('Solve (numpy):', x)\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "8893299e",
"metadata": {},
"source": [
"## GaussianBayesNet and GaussianConditional\n",
"\n",
"A `GaussianBayesNet` is a sequence of `GaussianConditional`s (conditionals). We attempt to construct them if available and otherwise show how a conditional relates to block matrices."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83a1b87d",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" gbn = gtsam.GaussianBayesNet()\n",
" gc = gtsam.GaussianConditional()\n",
"except Exception as e:\n",
" gbn = None\n",
" gc = None\n",
" print('GaussianBayesNet/GaussianConditional not available in Python bindings:', e)\n",
"\n",
"# Numpy illustration: solve a small conditional block\n",
"A = np.array([[2.0, -1.0], [-1.0, 2.0]])\n",
"b = np.array([1.0, 0.0])\n",
"x = np.linalg.solve(A, b)\n",
"print('Example conditional solve (numpy):', x)\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "df4d9ff3",
"metadata": {},
"source": [
"## GaussianBayesTree and GaussianEliminationTree\n",
"\n",
"Bayes trees and elimination trees capture elimination structure for efficient inference. If bindings exist we construct them; otherwise we show a small elimination example using numpy solves."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5ea2b4e",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" gbt = gtsam.GaussianBayesTree()\n",
" getree = gtsam.GaussianEliminationTree()\n",
"except Exception as e:\n",
" gbt = None\n",
" getree = None\n",
" print('GaussianBayesTree/GaussianEliminationTree not available in Python bindings:', e)\n",
"\n",
"# Numpy elimination demo\n",
"A = np.array([[4.0, -1.0], [-1.0, 3.0]])\n",
"b = np.array([1.0, 2.0])\n",
"x = np.linalg.solve(A, b)\n",
"print('Elimination example (numpy):', x)\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "a39b1efa",
"metadata": {},
"source": [
"## GaussianDensity and GaussianJunctionTree\n",
"\n",
"`GaussianDensity` stores densities (unnormalized); `GaussianJunctionTree` supports junction-tree algorithms. Below we show a pdf evaluation and a linear solve example."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa40cc22",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" gd = gtsam.GaussianDensity()\n",
" gjt = gtsam.GaussianJunctionTree()\n",
"except Exception as e:\n",
" gd = None\n",
" gjt = None\n",
" print('GaussianDensity/GaussianJunctionTree not available in Python bindings:', e)\n",
"\n",
"# Numpy gaussian pdf example\n",
"x = np.array([0.1, -0.2])\n",
"mu = np.zeros(2)\n",
"Sigma = np.eye(2)\n",
"det = np.linalg.det(Sigma)\n",
"norm = 1.0 / (2*np.pi*np.sqrt(det))\n",
"val = norm * np.exp(-0.5 * (x-mu).T @ np.linalg.inv(Sigma) @ (x-mu))\n",
"print('Gaussian pdf (numpy):', val)\n",
"val"
]
},
{
"cell_type": "markdown",
"id": "28cc2dd0",
"metadata": {},
"source": [
"## GaussianISAM (incremental)\n",
"\n",
"`GaussianISAM` is an incremental solver. We try to construct it and otherwise show a simple incremental idea with numpy."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cce797dc",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" gisam = gtsam.GaussianISAM()\n",
"except Exception as e:\n",
" gisam = None\n",
" print('GaussianISAM not available in Python bindings:', e)\n",
"\n",
"# Numpy incremental illustration (single step)\n",
"A = np.array([[2.0, 0.0],[0.0,2.0]])\n",
"b = np.array([1.0, 1.0])\n",
"x = np.linalg.solve(A, b)\n",
"print('Incremental example (numpy):', x)\n",
"x"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading