{
"cells": [
{
"cell_type": "markdown",
"id": "d62088a0",
"metadata": {},
"source": [
"$\\textbf{Lead Author: Adele Myers}$\n",
"\n",
"Inspired by: Guigui, Miolane, Pennec, 2022. Introduction to Riemannian Geometry and Geometric Statistics: from basic theory to implementation with Geomstats."
]
},
{
"cell_type": "markdown",
"id": "fc0913fc",
"metadata": {},
"source": [
"Note: Before you start to read this notebook, run the following code. This will import packages that will allow later code to run."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ac5b2719",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Using numpy backend\n"
]
}
],
"source": [
"import geomstats.backend as gs\n",
"\n",
"gs.random.seed(2020)"
]
},
{
"cell_type": "markdown",
"id": "ff5b438a",
"metadata": {},
"source": [
"# 1. Introduction"
]
},
{
"cell_type": "markdown",
"id": "1f4b7295",
"metadata": {},
"source": [
"Welcome to the `Manifold` class notebook! In this notebook, we will discuss manifolds and the `Manifold` class in Geomstats. Geomstats is a software which analyzes data on manifolds, so naturally the `Manifold` class is quite important.\n",
"\n",
"You will learn:\n",
"\n",
"1. What is a manifold?\n",
"2. Why do we care about manifolds?\n",
"3. What does the `Manifold` class do?\n",
"4. How is the `Manifold` class structured?\n",
" 1. What is an open set?\n",
" 2. What is a closed set?\n",
" 3. What is a tangent space?\n",
" "
]
},
{
"cell_type": "markdown",
"id": "48ac2a24",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "4083a61a",
"metadata": {},
"source": [
"# 2. What is a Manifold?"
]
},
{
"cell_type": "markdown",
"id": "4a60222e",
"metadata": {},
"source": [
"$\\textbf{Intuition:}$ When you are first learning, it can be a helpful starting point to think of a manifold as a surface. This surface can have any dimension and any shape as long as it is smooth (in the sense of being continuous and differentiable). (This is not a particularly precise definition, but it can be helpful starting point for building intuition.) For example, a hypersphere is a two dimensional manifold, and we will often use this manifold in examples. "
]
},
{
"cell_type": "markdown",
"id": "a5af7df5",
"metadata": {},
"source": [
"## 2.1 Precise Mathematical Definition"
]
},
{
"cell_type": "markdown",
"id": "486860d6",
"metadata": {},
"source": [
"A manifold is a set of points that satisfy a specific set of constraints. More specifically, a nonempty subset M $\\subseteq \\mathbb{R}^{N}$ is a d-dimensional manifold if and only if ANY of the following conditions hold:\n",
"\n",
"(1) (Local parametrization) For every $p \\in M$, there are two open subsets $V \\subseteq \\mathbb{R}^{d}$ and $U \\subseteq \\mathbb{R}^{N}$ with $p \\in U$ and $0 \\in V$. There is also a smooth function $f: V \\to \\mathbb{R}^{N}$ such that $f(0) = p$, where $f$ is a homeomorphism between V and $U \\cap M$, and $f$ is an immersion at 0.\n",
"\n",
"(2) (Local implicit function) For every $p \\in M$, there exists an open set $U \\in \\mathbb{R}^{N}$ and a smooth map $f: U \\to \\mathbb{R}^{N-d}$ that is a submersion at p, such that $U \\cap M = f^{-1}$({0}).\n",
"\n",
"(3) (Local Graph) For every $x \\in M$, there exists an open neighborhood $U \\subseteq \\mathbb{R}^{N}$ of $x$, a neighborhood $V \\subseteq \\mathbb{R}^{d}$ of 0 and a smooth map $f: V \\to \\mathbb{R}^{N-d}$ such that $U \\cap M = graph(f)^{2}$"
]
},
{
"cell_type": "markdown",
"id": "09f06979",
"metadata": {},
"source": [
"## 2.2 Imprecise \"Layman's Terms\" Definition:"
]
},
{
"cell_type": "markdown",
"id": "aa47546e",
"metadata": {},
"source": [
"A $\\textbf{manifold}$ is a set of points that satisfy a specific set of constraints, and these constraints are given by the mathematical definition above. That means that if a set of points satisfies any one of the conditions, then it is a manifold. However, these conditions can be hard to digest. We can translate the above mathematical conditions into three different ways of describing manifolds:\n",
"\n",
"1) (Local Parametrization) a manifold is a topological space that locally resembles Euclidean space near each point. For example, consider a two dimensional grid. We would not be able to deform this grid to have the shape of a sphere under any circumstance, but at each $\\textbf{local}$ point on the manifold, we can approximate the space around the point with a Euclidean grid.\n",
"\n",
"2) (Local Implicit Function) a manifold can be understood as the set of points that verify a constraint defined by an implicit equation, given by the function $f$. (see hypersphere example)\n",
"\n",
"3) (Local Graph) a manifold can be understood as a d-dimensional surface described by d variables, i.e. by the \"graph\" of a smooth function $f: (x_1,...,x_d) \\to f(x_1,...,x_d)$. This local graph cannot be applied globally (to the whole manifold) because a function $f$ must have only one output per set of inputs, and if we were to try to describe the whole manifold with such a graph, then many manifolds would have more than one output for one set of inputs."
]
},
{
"cell_type": "markdown",
"id": "84e49d7c",
"metadata": {},
"source": [
"The figure below provides a visualization of each of these definitions. The first figure is a visualization of \"local parametrization\", the second is a visualization of \"local implicit function\" and the third is a visualization of \"local graph\":"
]
},
{
"cell_type": "markdown",
"id": "5f1386d3",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "bf243b6d",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "73978259",
"metadata": {},
"source": [
"Each one of these definitions of a manifold is important because each one gives us an additional way to describe a manifold. This is useful because some manifolds are much easier described (or computationally implemented) by one of these definitions than the others. For example, any three of these definitions can be used to describe a hypersphere, but a hypersphere is most easily implemented using definition (2)."
]
},
{
"cell_type": "markdown",
"id": "d1a6966f",
"metadata": {},
"source": [
"## 2.3 Hypersphere example:"
]
},
{
"cell_type": "markdown",
"id": "e3a40d00",
"metadata": {},
"source": [
"Here, we will consider how to prove that a hypersphere is a manifold using the second condition in the definition of a manifold above (2)."
]
},
{
"cell_type": "markdown",
"id": "c61b4485",
"metadata": {},
"source": [
"A $\\textbf{hypersphere}$ is any of a set of objects (n-dimensional spheres) resulting from the generalization of a one-dimensional circle and a two-dimensional sphere. The dimension of the manifold is n and is equal to the number of degrees of freedom of a point moving on In n-dimensional space, a hypersphere is the set of all points that are a given distance (called the radius) from a given point (called the center). For example, a 2-dimensional hypersphere in 3-dimensions describes all the points in 3D space that lie on the surface of a sphere. In other words, a hypersphere describes all of the points that lie in three dimensions that have two degrees of freedom $(\\phi, \\theta)$."
]
},
{
"cell_type": "markdown",
"id": "ab26c3ae",
"metadata": {},
"source": [
"$\\textbf{How do we know that a hypersphere is a manifold?}$ Well, we know from the definition of a hypersphere that a hypersphere is the set of all points that are a given distance from the center of your coordinate system. For example in 3-dimensions, the surface $S$ of a sphere with radius 1 can be described by the relation: \n",
"\n",
"$|x|^{2} = 1$\n",
"\n",
"This simply says that all the points must be a distance of 1 away from the center. (see above figure (b) and take a = 1).\n",
"\n",
"We can now define a function \n",
"\n",
"$f(x) = |x|^{2} - 1$\n",
"\n",
"We know that this function $f$ will always equal zero for all points that lie on the surface $S$ because in order for a point to fall on the surface $S$ the condition $|x|^{2} = 1$ must be true. In other words,\n",
"\n",
"$x \\in S \\iff f(x) = 0$\n",
"\n",
"which tells us that \n",
"\n",
"$x \\in S \\iff x \\in f^{-1}$({0})\n",
"\n",
"This last line matches the definition of a manifold: $M = f^{-1}$({0}) where $M$ is the set of points $x$ that satisfy the condition $|x|^{2} = 1$. Therefore, the set of points that satisfy the condition $|x|^{2} = 1$ form a manifold."
]
},
{
"cell_type": "markdown",
"id": "6427ab8a",
"metadata": {},
"source": [
"## 2.4 More examples of manifolds:"
]
},
{
"cell_type": "markdown",
"id": "deb3926b",
"metadata": {},
"source": [
"A hypersphere is one type of manifold, but there are many other types of manifolds that are commonly used and seen in nature. We will show a few examples here to help you build intuition about what a manifold is and what a manifold can look like."
]
},
{
"cell_type": "markdown",
"id": "52f14f02",
"metadata": {},
"source": [
"For example, a torus (the surface of a donut) is a manifold."
]
},
{
"cell_type": "markdown",
"id": "f2074740",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "c614f981",
"metadata": {},
"source": [
"A klein bottle (the surface of the shape shown below) is also a manifold."
]
},
{
"cell_type": "markdown",
"id": "f25a4b02",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "2c07e9b9",
"metadata": {},
"source": [
"Perhaps a more familiar example for those in physics: Minkowski space (or Minkowski spacetime) is a combination of three-dimensional Euclidean space and time into a four-dimensional manifold (where x, y, z, and time are the dimensions of the space). This manifold is theorized to take one of three forms. A 4-dimensional manifold of: (a) flat curvature, (b) positive curvature, or (c) negative curvature. "
]
},
{
"cell_type": "markdown",
"id": "ad68e5ce",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "1252dfcb",
"metadata": {},
"source": [
"# 3. Why do we care about manifolds?"
]
},
{
"cell_type": "markdown",
"id": "b70fa3dc",
"metadata": {},
"source": [
"$\\textbf{Manifolds are important because data in nature \"naturally falls on manifolds\"}$, and as we discussed in the introduction section, knowing the manifold that a data set belongs to may give you more predictive power and a better understanding of the data's evolution. \n",
"\n",
"$\\textbf{What does it mean for data to \"naturally fall on a manifold\"?}$ In nature, data are often subject to constraints, and these constraints force the data to lie on manifolds. For example, consider the position of cities on the earth."
]
},
{
"cell_type": "markdown",
"id": "5ec06e21",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "ab1ca213",
"metadata": {},
"source": [
"The cities are subject to the following constraints: 1) they cannot fly above the surface of the earth because gravity holds them down and 2) they cannot sink down into the earth because the surface of the earth holds them up. Therefore, they are constrained to move (or not move) on the surface of a 3-dimensional sphere. This space that they are confined to exist in should sound familiar-- it is the space of a 2-dimensional hypersphere!"
]
},
{
"cell_type": "markdown",
"id": "74e0c2fa",
"metadata": {},
"source": [
"Other data falls on manifolds in similar ways: the data is subject to certain constraints, which forces it to fall on a manifold."
]
},
{
"cell_type": "markdown",
"id": "d55f5fec",
"metadata": {},
"source": [
"# 4. What does the `Manifold` class do?"
]
},
{
"cell_type": "markdown",
"id": "cdce00c5",
"metadata": {},
"source": [
"The `Manifold` class describes different types of manifolds. The `Manifold` class and its subclasses implement methods that establish the properties of different types of manifolds, and the manifolds that exist under these subclasses inherit the properties implemented in their respective subclasses.\n",
"\n",
"Note: The words \"class\", \"subclass\", \"methods\" refer to [object oriented programming](https://www.educative.io/blog/object-oriented-programming)."
]
},
{
"cell_type": "markdown",
"id": "c9888ad8",
"metadata": {},
"source": [
"The manifold class also provides ways of checking whether a vector is $\\textit{tangent}$ to a manifold and ways of projecting a vector to the $\\textit{tangent space}$ of a manifold. In order to better describe what this means, we will now define $\\textit{tangent vectors}$ and $\\textit{tangent spaces}$."
]
},
{
"cell_type": "markdown",
"id": "b80dde22",
"metadata": {},
"source": [
"## 4.1 Tangent Vectors"
]
},
{
"cell_type": "markdown",
"id": "705a6c52",
"metadata": {},
"source": [
"Here we will provide a brief reminder of tangent vectors as a way to lay the groundwork for discussing tangent spaces.\n",
"\n",
"A $\\textbf{tangent vector}$ is a vector that is \"tangent\" to a curve or surface at a given point. When a vector is \"tangent\" to a curve, this means that the vector has the same slope as the curve does at that point."
]
},
{
"cell_type": "markdown",
"id": "93cf1288",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "3bc23ab0",
"metadata": {},
"source": [
"## 4.2 Tangent Spaces"
]
},
{
"cell_type": "markdown",
"id": "17f31482",
"metadata": {},
"source": [
"Now we will introduce the concept of a \"tangent space\". The tangent space at a certain point on a manifold is comprised of all of the possible tangent vectors that exist at that point. For example, if you are considering the tangent space on a curve, then the only possible tangent vectors are tangent vectors that point forward and backward along the line (a). However, if you are considering the tangent space at a point on a surface, then the tangent vectors can point forward, backward, left, right, and everywhere in between, and the set of all tangent vectors forms a plane (b)."
]
},
{
"cell_type": "markdown",
"id": "5265a8ab",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "42a741ef",
"metadata": {},
"source": [
"Thus, the tangent space of a 1-dimensional manifold (curve) is also one dimensional, and the tangent space of a 2-dimensional manifold (a 2-dimensional surface) is also 2-dimensional.\n",
"\n",
"Similarly, for every n-dimensional manifold, there exists an n-dimensional tangent space at each point on the manifold, and the tangent space is comprised of all possible tangent vectors on that manifold."
]
},
{
"cell_type": "markdown",
"id": "13e555d6",
"metadata": {},
"source": [
"# 5. How is the `Manifold` class structured?"
]
},
{
"cell_type": "markdown",
"id": "0981e6e3",
"metadata": {},
"source": [
"The hierarchical structure of the classes inheriting from the `Manifold` parent class is as follows (this Figure is a courtesy of Nicolas Guigui):"
]
},
{
"cell_type": "markdown",
"id": "348717a4",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "815af4cb",
"metadata": {},
"source": [
"As discussed in the previous section, one of the primary purposes of the `Manifold` class is to hold information about various types of manifolds. Rules that are universally true for all manifolds are implemented in methods in the parent class `Manifold`. Rules that are true for some types of manifolds are implemented in the subclasses of `Manifold`: `LevelSet`, `VectorSpaceOpenSet`, `FiberBundle`, `ProductManifold`, `VectorSpace`, `MatrixLieAlgebra`, and `MatrixLieGroup`. Specific types of manifolds are described in methods within these subclasses.\n",
"\n",
"In this notebook, we will focus on describing the subclasses pertinent to the geometry module of geomstats: `LevelSet`, `VectorSpaceOpenSet`, `ProductManifold` and `VectorSpace`.\n",
"\n",
"In the following subsections, we will discuss the methods and ideas implemented in the parent class and its subclasses."
]
},
{
"cell_type": "markdown",
"id": "36dc5f07",
"metadata": {},
"source": [
"## 5.1 The Parent Class: `Manifold`"
]
},
{
"cell_type": "markdown",
"id": "578faea8",
"metadata": {},
"source": [
"The Manifold parent class is an abstract base class which provides the minimal skeleton of attributes and methods expected in its subclasses. Note that the methods of the abstract parent class are declared, but they contain no implementation, and they are overridden by the subclasses. The properties that are declared in the `Manifold` class are properties that all types of manifold must possess. For example, the following methods and attributes are implemented in `Manifold`:\n",
"\n",
"1. `dim`: $\\textit{attribute}$. the dimension of the manifold. \"How many coordinates are necessary to fully describe the manifold?\"\n",
"2. `belongs()`: $\\textit{method}$. evaluates whether a given element belongs to that manifold\n",
"3. `is_tangent()`: $\\textit{method}$. evaluates whether a given vector is a tangent vector at a given point \n",
"4. `random_point()`: $\\textit{method}$. generates a random point that lies on the manifold"
]
},
{
"cell_type": "markdown",
"id": "cb8c11e1",
"metadata": {},
"source": [
"While the abstract methods in Manifold do not contain any implementation, the methods of the subclasses of `Manifold`, such as the `Hypersphere` subclass, $\\textit{are}$ implemented and can be run. We will now exemplify this in the following section"
]
},
{
"cell_type": "markdown",
"id": "138bbba4",
"metadata": {},
"source": [
"### 5.1.1 Examples of Using `Manifold`'s Attributes and Methods in the Subclass: `Hypersphere`"
]
},
{
"cell_type": "markdown",
"id": "c875f005",
"metadata": {},
"source": [
"$\\textbf{Attributes:}$"
]
},
{
"cell_type": "markdown",
"id": "823b2bce",
"metadata": {},
"source": [
"`dim` : If we build a hypersphere of dimension 2 with the following code, we can check that `sphere.dim` gives back 2. Run the following code to verify this:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "23a48d29",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The dimension of the sphere is 2\n"
]
}
],
"source": [
"from geomstats.geometry.hypersphere import Hypersphere\n",
"\n",
"sphere = Hypersphere(dim=2)\n",
"\n",
"print(f\"The dimension of the sphere is {sphere.dim}\")"
]
},
{
"cell_type": "markdown",
"id": "6325d9ed",
"metadata": {},
"source": [
"$\\textbf{Methods:}$"
]
},
{
"cell_type": "markdown",
"id": "f9bf4f6e",
"metadata": {},
"source": [
"`belongs` : We can re-use the sphere we just built (called \"sphere\") and verify that the point (0, 0, 1) belongs to that sphere (it is the north pole). Run the following code to verify this using Geomstats."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ad91b2fa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import geomstats.backend as gs\n",
"\n",
"sphere.belongs(gs.array([0, 0, 1]))"
]
},
{
"cell_type": "markdown",
"id": "7bcfcd2a",
"metadata": {},
"source": [
"`is_tangent` : The vector (1, 1, 0 ) is tangent to the sphere at the north pole, since it does not have a vertical component (last component is equal to 0). Run the following code to verify this using Geomstats."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f080a042",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sphere.is_tangent(vector=gs.array([1, 1, 0]), base_point=gs.array([0, 0, 1]))"
]
},
{
"cell_type": "markdown",
"id": "12764fbf",
"metadata": {},
"source": [
"`random_point` : Now, we will use `random_point` to generate a random point, and then we will use `belongs` to prove that this random point belongs to the sphere."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d89557c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from geomstats.geometry.hypersphere import Hypersphere\n",
"\n",
"rp = Hypersphere.random_point(sphere)\n",
"\n",
"sphere.belongs(rp)"
]
},
{
"cell_type": "markdown",
"id": "cc797840",
"metadata": {},
"source": [
"### 5.1.2 The Full `Manifold` Class Code"
]
},
{
"cell_type": "markdown",
"id": "2fde983a",
"metadata": {},
"source": [
"You can see all of the methods in the `Manifold` parent class by running the following code. Observe the abstract methods denoted with \"@abc.abstractmethod\" that do not contain any implementation, but serve as template for the subclasses to overwrite. The code of `Manifold` can also be found [here](https://github.com/geomstats/geomstats/blob/main/geomstats/geometry/manifold.py)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5a27e4f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class Manifold(abc.ABC):\n",
" r\"\"\"Class for manifolds.\n",
"\n",
" Parameters\n",
" ----------\n",
" dim : int\n",
" Dimension of the manifold.\n",
" shape : tuple of int\n",
" Shape of one element of the manifold.\n",
" Optional, default : None.\n",
" default_coords_type : str, {\\'intrinsic\\', \\'extrinsic\\', etc}\n",
" Coordinate type.\n",
" Optional, default: 'intrinsic'.\n",
" equip : bool\n",
" If True, equip space with default metric.\n",
"\n",
" Attributes\n",
" ----------\n",
" point_ndim : int\n",
" Dimension of point array.\n",
" default_point_type : str\n",
" Point type: \"vector\" or \"matrix\"\n",
" \"\"\"\n",
"\n",
" def __init__(\n",
" self,\n",
" dim,\n",
" shape,\n",
" default_coords_type=\"intrinsic\",\n",
" equip=True,\n",
" ):\n",
" geomstats.errors.check_integer(dim, \"dim\")\n",
"\n",
" if not isinstance(shape, tuple):\n",
" raise ValueError(\"Expected a tuple for the shape argument.\")\n",
"\n",
" self.dim = dim\n",
" self.shape = shape\n",
" self.default_coords_type = default_coords_type\n",
"\n",
" self.point_ndim = len(self.shape)\n",
" if self.point_ndim == 1:\n",
" # TODO: remove default point type\n",
" self.default_point_type = \"vector\"\n",
" elif self.point_ndim == 2:\n",
" self.default_point_type = \"matrix\"\n",
" else:\n",
" self.default_point_type = \"other\"\n",
"\n",
" if equip:\n",
" self.equip_with_metric()\n",
"\n",
" def equip_with_metric(self, Metric=None, **metric_kwargs):\n",
" if Metric is None:\n",
" out = self._default_metric()\n",
" if isinstance(out, tuple):\n",
" Metric, kwargs = out\n",
" kwargs.update(metric_kwargs)\n",
" metric_kwargs = kwargs\n",
" else:\n",
" Metric = out\n",
"\n",
" self.metric = Metric(self, **metric_kwargs)\n",
"\n",
" @abc.abstractmethod\n",
" def belongs(self, point, atol=gs.atol):\n",
" \"\"\"Evaluate if a point belongs to the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., *point_shape]\n",
" Point to evaluate.\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" belongs : array-like, shape=[...,]\n",
" Boolean evaluating if point belongs to the manifold.\n",
" \"\"\"\n",
"\n",
" @abc.abstractmethod\n",
" def is_tangent(self, vector, base_point, atol=gs.atol):\n",
" \"\"\"Check whether the vector is tangent at base_point.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point on the manifold.\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" is_tangent : bool\n",
" Boolean denoting if vector is a tangent vector at the base point.\n",
" \"\"\"\n",
"\n",
" @abc.abstractmethod\n",
" def to_tangent(self, vector, base_point):\n",
" \"\"\"Project a vector to a tangent space of the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point on the manifold.\n",
"\n",
" Returns\n",
" -------\n",
" tangent_vec : array-like, shape=[..., *point_shape]\n",
" Tangent vector at base point.\n",
" \"\"\"\n",
"\n",
" @abc.abstractmethod\n",
" def random_point(self, n_samples=1, bound=1.0):\n",
" \"\"\"Sample random points on the manifold according to some distribution.\n",
"\n",
" If the manifold is compact, preferably a uniform distribution will be used.\n",
"\n",
" Parameters\n",
" ----------\n",
" n_samples : int\n",
" Number of samples.\n",
" Optional, default: 1.\n",
" bound : float\n",
" Bound of the interval in which to sample for non compact manifolds.\n",
" Optional, default: 1.\n",
"\n",
" Returns\n",
" -------\n",
" samples : array-like, shape=[..., *point_shape]\n",
" Points sampled on the manifold.\n",
" \"\"\"\n",
"\n",
" def regularize(self, point):\n",
" \"\"\"Regularize a point to the canonical representation for the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., dim]\n",
" Point.\n",
"\n",
" Returns\n",
" -------\n",
" regularized_point : array-like, shape=[..., *point_shape]\n",
" Regularized point.\n",
" \"\"\"\n",
" return gs.copy(point)\n",
"\n",
" def random_tangent_vec(self, base_point, n_samples=1):\n",
" \"\"\"Generate random tangent vec.\n",
"\n",
" Parameters\n",
" ----------\n",
" n_samples : int\n",
" Number of samples.\n",
" Optional, default: 1.\n",
" base_point : array-like, shape={[n_samples, *point_shape], [*point_shape,]}\n",
" Point.\n",
"\n",
" Returns\n",
" -------\n",
" tangent_vec : array-like, shape=[..., *point_shape]\n",
" Tangent vec at base point.\n",
" \"\"\"\n",
" if (\n",
" n_samples > 1\n",
" and base_point.ndim > len(self.shape)\n",
" and n_samples != len(base_point)\n",
" ):\n",
" raise ValueError(\n",
" \"The number of base points must be the same as the \"\n",
" \"number of samples, when the number of base points is different from 1.\"\n",
" )\n",
" batch_size = () if n_samples == 1 else (n_samples,)\n",
" return self.to_tangent(\n",
" gs.random.normal(size=batch_size + self.shape), base_point\n",
" )\n"
]
}
],
"source": [
"import inspect\n",
"\n",
"from geomstats.geometry.manifold import Manifold\n",
"\n",
"for line in inspect.getsourcelines(Manifold)[0]:\n",
" line = line.replace(\"\\n\", \"\")\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"id": "7ce847b2",
"metadata": {},
"source": [
"## 5.2 `VectorSpaceOpenSet`"
]
},
{
"cell_type": "markdown",
"id": "3bb824a3",
"metadata": {},
"source": [
"Earlier in the notebook, we were able to say that a set of points is a manifold if it satisfied one of three constraints. We also said that every manifold can be described by any three of these definitions, and the choice of definition is merely a question of which definition is most convenient. `VectorSpaceOpenSet` provides a way of describing manifolds with local parametrization, which was labeled (1) on our definition list.\n",
"\n",
"One such way to describe a manifold is with the concept of an $\\textbf{Open Set}$: a manifold is the open sets of a d-dimensional vector space, called $\\textbf{ambient space}$."
]
},
{
"cell_type": "markdown",
"id": "d6ba6ba0",
"metadata": {},
"source": [
"### 5.2.1 What is an Open Set?"
]
},
{
"cell_type": "markdown",
"id": "cf224618",
"metadata": {},
"source": [
"Intuitively, an open set is a group of numbers that does not include points on the boundary of whatever they are describing. For example, if you were to take the set of all points between a and b but $\\textbf{not}$ include the values a and b, this would be an open set (shown in figure a). If you were to take the set of all points between a and b and include the values a and b, this would be a closed set (shown in figure b)"
]
},
{
"cell_type": "markdown",
"id": "38f8dc93",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "822181bc",
"metadata": {},
"source": [
"The above examples showed open and closed sets in one dimension. Similarly, in two dimensions, open sets can be defined as sets which do not contain their boundaries. For example, the inside of the sphere, i.e. the ball without its boundary, is a manifold that is an open set. The image below shows an example of an open set (a) and a closed set (b) in two dimensions."
]
},
{
"cell_type": "markdown",
"id": "2991834b",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "8cde0af0",
"metadata": {},
"source": [
"### 5.2.2 What Methods are Implemented in `VectorSpaceOpenSet`?"
]
},
{
"cell_type": "markdown",
"id": "151d22b8",
"metadata": {},
"source": [
"If we know that a manifold is conveniently described as an open set, then some of the manifold's abstract methods can be rewritten in a specific form. For example, `VectorSpaceOpenSet` implements the methods:\n",
"\n",
"1. `projection()`: a method to project any d-dimensional vector to the manifold.\n",
"2. `is_tangent()`: checks whether the input vector is tangent at the input point.\n",
"3. `to_tangent()`: projects a vector to a tangent space of the manifold.\n",
"\n",
"Note that we do not (yet) specify which manifold we are talking about (whether it is a sphere or another surface), we are just saying that we are looking at some manifold that can be described as an open set"
]
},
{
"cell_type": "markdown",
"id": "40c86d59",
"metadata": {},
"source": [
"Run the code below to see the contents of the `VectorSpaceOpenSet` class."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "cd5f0071",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class VectorSpaceOpenSet(Manifold, abc.ABC):\n",
" \"\"\"Class for manifolds that are open sets of a vector space.\n",
"\n",
" In this case, tangent vectors are identified with vectors of the embedding\n",
" space.\n",
"\n",
" Parameters\n",
" ----------\n",
" embedding_space: VectorSpace\n",
" Embedding space that contains the manifold.\n",
" \"\"\"\n",
"\n",
" def __init__(self, embedding_space, shape=None, **kwargs):\n",
" # TODO: embedding space following `LevelSet`?\n",
" # TODO: should we default `dim` to embedding space?\n",
" self.embedding_space = embedding_space\n",
" if shape is None:\n",
" shape = embedding_space.shape\n",
" super().__init__(shape=shape, **kwargs)\n",
"\n",
" def is_tangent(self, vector, base_point=None, atol=gs.atol):\n",
" \"\"\"Check whether the vector is tangent at base_point.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point on the manifold.\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" is_tangent : bool\n",
" Boolean denoting if vector is a tangent vector at the base point.\n",
" \"\"\"\n",
" is_tangent = self.embedding_space.belongs(vector, atol)\n",
" if base_point is not None and base_point.ndim > vector.ndim:\n",
" return gs.broadcast_to(is_tangent, base_point.shape[: -self.point_ndim])\n",
" return is_tangent\n",
"\n",
" def to_tangent(self, vector, base_point=None):\n",
" \"\"\"Project a vector to a tangent space of the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point on the manifold.\n",
"\n",
" Returns\n",
" -------\n",
" tangent_vec : array-like, shape=[..., *point_shape]\n",
" Tangent vector at base point.\n",
" \"\"\"\n",
" tangent_vec = self.embedding_space.projection(vector)\n",
" if base_point is not None and base_point.ndim > vector.ndim:\n",
" return gs.broadcast_to(tangent_vec, base_point.shape)\n",
" return tangent_vec\n",
"\n",
" def random_point(self, n_samples=1, bound=1.0):\n",
" \"\"\"Sample random points on the manifold.\n",
"\n",
" Points are sampled from the embedding space using the distribution set\n",
" for that manifold and then projected to the manifold. As a result, this\n",
" is not a uniform distribution on the manifold itself.\n",
"\n",
" Parameters\n",
" ----------\n",
" n_samples : int\n",
" Number of samples.\n",
" Optional, default: 1.\n",
" bound : float\n",
" Bound of the interval in which to sample for the embedding space.\n",
" Optional, default: 1.\n",
"\n",
" Returns\n",
" -------\n",
" samples : array-like, shape=[..., *point_shape]\n",
" Points sampled on the hypersphere.\n",
" \"\"\"\n",
" sample = self.embedding_space.random_point(n_samples, bound)\n",
" return self.projection(sample)\n",
"\n",
" @abc.abstractmethod\n",
" def projection(self, point):\n",
" \"\"\"Project a point in embedding manifold on manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., *point_shape]\n",
" Point in embedding manifold.\n",
"\n",
" Returns\n",
" -------\n",
" projected : array-like, shape=[..., *point_shape]\n",
" Projected point.\n",
" \"\"\"\n"
]
}
],
"source": [
"import inspect\n",
"\n",
"from geomstats.geometry.base import VectorSpaceOpenSet\n",
"\n",
"for line in inspect.getsourcelines(VectorSpaceOpenSet)[0]:\n",
" line = line.replace(\"\\n\", \"\")\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"id": "8b35c4d4",
"metadata": {},
"source": [
"## 5.3 `LevelSet`"
]
},
{
"cell_type": "markdown",
"id": "4ba53871",
"metadata": {},
"source": [
"### 5.3.1 What is a Level Set?"
]
},
{
"cell_type": "markdown",
"id": "f2b6b387",
"metadata": {},
"source": [
"Another elementary class of manifolds are $\\textbf{Level Sets}$. A level set is the set of values $x$ for which a function f(x) is equal to a given constant. In other words, a level set is a set of curves for which the function describing a manifold is constant along that curve. \n",
"\n",
"In the same way that `VectorSpaceOpenSet` is an implementation of the first definition of a manifold (Local Parametrization), `LevelSet` is an implementation of the second definition of a manifold (Local Implicit Function). a level set is a set of points for which the function $f$ takes the exact same value. This value is called the \"level\", and does not need to be a scalar, it could also be a vector.\n",
"\n",
"For example, consider a hypersphere in three dimensional space. Each of the concentric spheres is a 2-dimensional manifold, each corresponding to a different level $(r1, r2,r3)$."
]
},
{
"cell_type": "markdown",
"id": "16bd9d4d",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "a93bf48d",
"metadata": {},
"source": [
"You can see [here](https://github.com/geomstats/geomstats/blob/20cccf598e94823ef8d7c0821b2968e6d29660c0/geomstats/geometry/hypersphere.py#L22) that the subclass `Hypersphere` is indeed implemented as a `LevelSet`."
]
},
{
"cell_type": "markdown",
"id": "e8608bea",
"metadata": {},
"source": [
"### 5.3.2 `LevelSet` in Geomstats"
]
},
{
"cell_type": "markdown",
"id": "b4c2afa0",
"metadata": {},
"source": [
"You can run the code below to see the contents of the `LevelSet` class. The methods of LevelSet are similar to the methods of VectorSpaceOpenSet , but their implementation is different. Recall from the (Local Implicit Function) definition, which `LevelSet` is an implementation of, that a manifold of this type must satisfy:\n",
"\n",
"\"For every $p \\in M$, there exists an open set $U \\in \\mathbb{R}^{N}$ and a smooth map $f: U \\to \\mathbb{R}^{N-d}$ that is a submersion at p, such that $U \\cap M = f^{-1}$({0}).\"\n",
"\n",
"`VectorSpaceOpenSet` is an implementation of the first manifold definition (Local Parametrization), and therefore need not follow the (Local Implicit Function) rule above. This is the reason that, `VectorSpaceOpenSet` methods and `LevelSet` methods are implemented differently. As an example of these different implementations: observe the implementation of the belongs methods in `LevelSet`. For a general level set, in order to verify if a point belongs to the level set, we should verify that the (Local Implicit Function) definition constraint is met, which is done with the line\n",
"\n",
"`constraint = gs.isclose(self.submersion(point), value, atol=atol)`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "48c28386",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class LevelSet(Manifold, abc.ABC):\n",
" \"\"\"Class for manifolds embedded in a vector space by a submersion.\n",
"\n",
" Parameters\n",
" ----------\n",
" default_coords_type : str, {'intrinsic', 'extrinsic', etc}\n",
" Coordinate type.\n",
" Optional, default: 'extrinsic'.\n",
" \"\"\"\n",
"\n",
" def __init__(self, default_coords_type=\"extrinsic\", shape=None, **kwargs):\n",
" self.embedding_space = self._define_embedding_space()\n",
"\n",
" if shape is None:\n",
" shape = self.embedding_space.shape\n",
"\n",
" super().__init__(default_coords_type=default_coords_type, shape=shape, **kwargs)\n",
"\n",
" @abc.abstractmethod\n",
" def _define_embedding_space(self):\n",
" \"\"\"Define embedding space of the manifold.\n",
"\n",
" Returns\n",
" -------\n",
" embedding_space : Manifold\n",
" Instance of Manifold.\n",
" \"\"\"\n",
"\n",
" @abc.abstractmethod\n",
" def submersion(self, point):\n",
" r\"\"\"Submersion that defines the manifold.\n",
"\n",
" :math:`\\mathrm{submersion}(x)=0` defines the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., *point_shape]\n",
"\n",
" Returns\n",
" -------\n",
" submersed_point : array-like\n",
" \"\"\"\n",
"\n",
" @abc.abstractmethod\n",
" def tangent_submersion(self, vector, point):\n",
" \"\"\"Tangent submersion.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" point : array-like, shape=[..., *point_shape]\n",
"\n",
" Returns\n",
" -------\n",
" submersed_vector : array-like\n",
" \"\"\"\n",
"\n",
" def belongs(self, point, atol=gs.atol):\n",
" \"\"\"Evaluate if a point belongs to the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., *point_shape]\n",
" Point to evaluate.\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" belongs : array-like, shape=[...,]\n",
" Boolean evaluating if point belongs to the manifold.\n",
" \"\"\"\n",
" belongs = self.embedding_space.belongs(point, atol)\n",
" if not gs.any(belongs):\n",
" return belongs\n",
"\n",
" submersed_point = self.submersion(point)\n",
"\n",
" n_batch = gs.ndim(point) - len(self.shape)\n",
" axis = tuple(range(-len(submersed_point.shape) + n_batch, 0))\n",
"\n",
" if gs.is_complex(submersed_point):\n",
" constraint = gs.isclose(submersed_point, 0.0 + 0.0j, atol=atol)\n",
" else:\n",
" constraint = gs.isclose(submersed_point, 0.0, atol=atol)\n",
"\n",
" if axis:\n",
" constraint = gs.all(constraint, axis=axis)\n",
"\n",
" return gs.logical_and(belongs, constraint)\n",
"\n",
" def is_tangent(self, vector, base_point, atol=gs.atol):\n",
" \"\"\"Check whether the vector is tangent at base_point.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point on the manifold.\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" is_tangent : bool\n",
" Boolean denoting if vector is a tangent vector at the base point.\n",
" \"\"\"\n",
" belongs = self.embedding_space.is_tangent(vector, base_point, atol)\n",
" if not gs.any(belongs):\n",
" return belongs\n",
"\n",
" submersed_vector = self.tangent_submersion(vector, base_point)\n",
"\n",
" n_batch = max(gs.ndim(base_point), gs.ndim(vector)) - len(self.shape)\n",
" axis = tuple(range(-len(submersed_vector.shape) + n_batch, 0))\n",
"\n",
" constraint = gs.isclose(submersed_vector, 0.0, atol=atol)\n",
" if axis:\n",
" constraint = gs.all(constraint, axis=axis)\n",
"\n",
" return gs.logical_and(belongs, constraint)\n",
"\n",
" def intrinsic_to_extrinsic_coords(self, point_intrinsic):\n",
" \"\"\"Convert from intrinsic to extrinsic coordinates.\n",
"\n",
" Parameters\n",
" ----------\n",
" point_intrinsic : array-like, shape=[..., *point_shape]\n",
" Point in the embedded manifold in intrinsic coordinates.\n",
"\n",
" Returns\n",
" -------\n",
" point_extrinsic : array-like, shape=[..., *embedding_space.point_shape]\n",
" Point in the embedded manifold in extrinsic coordinates.\n",
" \"\"\"\n",
" raise NotImplementedError(\"intrinsic_to_extrinsic_coords is not implemented.\")\n",
"\n",
" def extrinsic_to_intrinsic_coords(self, point_extrinsic):\n",
" \"\"\"Convert from extrinsic to intrinsic coordinates.\n",
"\n",
" Parameters\n",
" ----------\n",
" point_extrinsic : array-like, shape=[..., *embedding_space.point_shape]\n",
" Point in the embedded manifold in extrinsic coordinates,\n",
" i. e. in the coordinates of the embedding manifold.\n",
"\n",
" Returns\n",
" -------\n",
" point_intrinsic : array-lie, shape=[..., *point_shape]\n",
" Point in the embedded manifold in intrinsic coordinates.\n",
" \"\"\"\n",
" raise NotImplementedError(\"extrinsic_to_intrinsic_coords is not implemented.\")\n",
"\n",
" @abc.abstractmethod\n",
" def projection(self, point):\n",
" \"\"\"Project a point in embedding manifold on embedded manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., *embedding_space.point_shape]\n",
" Point in embedding manifold.\n",
"\n",
" Returns\n",
" -------\n",
" projected : array-like, shape=[..., *point_shape]\n",
" Projected point.\n",
" \"\"\"\n",
"\n",
" @abc.abstractmethod\n",
" def to_tangent(self, vector, base_point):\n",
" \"\"\"Project a vector to a tangent space of the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point on the manifold.\n",
"\n",
" Returns\n",
" -------\n",
" tangent_vec : array-like, shape=[..., *point_shape]\n",
" Tangent vector at base point.\n",
" \"\"\"\n"
]
}
],
"source": [
"import inspect\n",
"\n",
"from geomstats.geometry.base import LevelSet\n",
"\n",
"for line in inspect.getsourcelines(LevelSet)[0]:\n",
" line = line.replace(\"\\n\", \"\")\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"id": "14edde0c",
"metadata": {},
"source": [
"## 5.4 `VectorSpace`"
]
},
{
"cell_type": "markdown",
"id": "cd5fd5bd",
"metadata": {},
"source": [
"A $\\textit{vector space}$ is a special case of manifold where a vector exists at each point.\n",
"\n",
"Many operations defined on manifolds become simpler when the manifold is actually a vector space. Additionally, the ambient spaces used to define manifolds as open or closed sets are often vector spaces. Thus, we provide an implementation `VectorSpace`."
]
},
{
"cell_type": "markdown",
"id": "962d4ac2",
"metadata": {},
"source": [
"This class does not provide another way of describing a manifold. This class is an abstract class that makes sure that the ambient/embedding vector space is compatible with the methods that are called in `VectorSpaceOpenSet` and `LevelSet`.\n",
"\n",
"Actual manifolds are implemented as subclasses of this abstract method and must implement all the abstract methods defined in this class."
]
},
{
"cell_type": "markdown",
"id": "963954ba",
"metadata": {},
"source": [
"You can run the code below to see the contents of the `VectorSpace` class. Observe that, in a `VectorSpace` manifold, the manifold is comprised of vectors -- and more specifically, tangent vectors. Thus, in the implementation, we can see that in the `is_tangent()` method: in order to check that a vector is a tangent to the manifold, we only need to check that the vector belongs to the vector space."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "237b9252",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class VectorSpace(Manifold, abc.ABC):\n",
" \"\"\"Abstract class for vector spaces.\n",
"\n",
" Parameters\n",
" ----------\n",
" shape : tuple\n",
" Shape of the elements of the vector space. The dimension is the\n",
" product of these values by default.\n",
" \"\"\"\n",
"\n",
" def __init__(self, shape, dim=None, **kwargs):\n",
" if dim is None:\n",
" dim = math.prod(shape)\n",
" super().__init__(dim=dim, shape=shape, **kwargs)\n",
" self._basis = None\n",
"\n",
" def belongs(self, point, atol=gs.atol):\n",
" \"\"\"Evaluate if the point belongs to the vector space.\n",
"\n",
" This method checks the shape of the input point.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[.., *point_shape]\n",
" Point to test.\n",
" atol : float\n",
" Unused here.\n",
"\n",
" Returns\n",
" -------\n",
" belongs : array-like, shape=[...,]\n",
" Boolean evaluating if point belongs to the space.\n",
" \"\"\"\n",
" belongs = self.shape == point.shape[-self.point_ndim :]\n",
" shape = point.shape[: -self.point_ndim]\n",
" if belongs:\n",
" return gs.ones(shape, dtype=bool)\n",
" return gs.zeros(shape, dtype=bool)\n",
"\n",
" @staticmethod\n",
" def projection(point):\n",
" \"\"\"Project a point to the vector space.\n",
"\n",
" This method is for compatibility and returns `point`. `point` should\n",
" have the right shape,\n",
"\n",
" Parameters\n",
" ----------\n",
" point: array-like, shape[..., *point_shape]\n",
" Point.\n",
"\n",
" Returns\n",
" -------\n",
" point: array-like, shape[..., *point_shape]\n",
" Point.\n",
" \"\"\"\n",
" return gs.copy(point)\n",
"\n",
" def is_tangent(self, vector, base_point=None, atol=gs.atol):\n",
" \"\"\"Check whether the vector is tangent at base_point.\n",
"\n",
" Tangent vectors are identified with points of the vector space so\n",
" this checks the shape of the input vector.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point in the vector space.\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" is_tangent : array-like, shape=[...,]\n",
" Boolean denoting if vector is a tangent vector at the base point.\n",
" \"\"\"\n",
" belongs = self.belongs(vector, atol)\n",
" if base_point is not None and base_point.ndim > vector.ndim:\n",
" return gs.broadcast_to(belongs, base_point.shape[: -self.point_ndim])\n",
" return belongs\n",
"\n",
" def to_tangent(self, vector, base_point=None):\n",
" \"\"\"Project a vector to a tangent space of the vector space.\n",
"\n",
" This method is for compatibility and returns vector.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., *point_shape]\n",
" Vector.\n",
" base_point : array-like, shape=[..., *point_shape]\n",
" Point in the vector space\n",
"\n",
" Returns\n",
" -------\n",
" tangent_vec : array-like, shape=[..., *point_shape]\n",
" Tangent vector at base point.\n",
" \"\"\"\n",
" tangent_vec = self.projection(vector)\n",
" if base_point is not None and base_point.ndim > vector.ndim:\n",
" return gs.broadcast_to(tangent_vec, base_point.shape)\n",
" return tangent_vec\n",
"\n",
" def random_point(self, n_samples=1, bound=1.0):\n",
" \"\"\"Sample in the vector space with a uniform distribution in a box.\n",
"\n",
" Parameters\n",
" ----------\n",
" n_samples : int\n",
" Number of samples.\n",
" Optional, default: 1.\n",
" bound : float\n",
" Side of hypercube support of the uniform distribution.\n",
" Optional, default: 1.0\n",
"\n",
" Returns\n",
" -------\n",
" point : array-like, shape=[..., *point_shape]\n",
" Sample.\n",
" \"\"\"\n",
" size = self.shape\n",
" if n_samples != 1:\n",
" size = (n_samples,) + self.shape\n",
" point = bound * (gs.random.rand(*size) - 0.5) * 2\n",
" return point\n",
"\n",
" @property\n",
" def basis(self):\n",
" \"\"\"Basis of the vector space.\"\"\"\n",
" if self._basis is None:\n",
" self._basis = self._create_basis()\n",
" return self._basis\n",
"\n",
" @abc.abstractmethod\n",
" def _create_basis(self):\n",
" \"\"\"Create a canonical basis.\"\"\"\n"
]
}
],
"source": [
"import inspect\n",
"\n",
"from geomstats.geometry.base import VectorSpace\n",
"\n",
"for line in inspect.getsourcelines(VectorSpace)[0]:\n",
" line = line.replace(\"\\n\", \"\")\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"id": "79923105",
"metadata": {},
"source": [
"## 5.5 `ProductManifold`"
]
},
{
"cell_type": "markdown",
"id": "addb9c36",
"metadata": {},
"source": [
"New manifolds can also be created by composing existing manifolds. A product manifold combines two manifolds $M_1$ and $M_2$ to create a new manifold $M_3$, and more generally $N$ manifolds $M1, ..., M_N$ can be combined to create a new manifold $M$."
]
},
{
"cell_type": "markdown",
"id": "b7750cdf",
"metadata": {},
"source": [
"### 5.5.1 Example"
]
},
{
"cell_type": "markdown",
"id": "77ac9b35",
"metadata": {},
"source": [
"For example, we can create a product of two spheres as:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "fba010bc",
"metadata": {},
"outputs": [],
"source": [
"from geomstats.geometry.product_manifold import ProductManifold\n",
"\n",
"sphere1 = Hypersphere(dim=2)\n",
"\n",
"sphere2 = Hypersphere(dim=2)\n",
"\n",
"\n",
"product_of_two_spheres = ProductManifold([sphere1, sphere2])"
]
},
{
"cell_type": "markdown",
"id": "645c3d2e",
"metadata": {},
"source": [
"If we generate a random point on this product manifold, we will get 6 coordinates: the first 3 represent a point on the first sphere, and the last 3 represent a point on the second sphere:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3ffe5cbf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-0.3861891 , -0.52946814, -0.75532871],\n",
" [-0.14573203, 0.15373756, 0.97730596]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"product_of_two_spheres.random_point()"
]
},
{
"cell_type": "markdown",
"id": "8e7557bb",
"metadata": {},
"source": [
"You can run the code below to see the contents of the `ProductManifold` class."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f5009680",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class ProductManifold(_IterateOverFactorsMixins, Manifold):\n",
" \"\"\"Class for a product of manifolds M_1 x ... x M_n.\n",
"\n",
" In contrast to the classes NFoldManifold, Landmarks, or DiscretizedCurves,\n",
" the manifolds M_1, ..., M_n need not be the same, nor of\n",
" same dimension, but the list of manifolds needs to be provided.\n",
"\n",
" Parameters\n",
" ----------\n",
" factors : list\n",
" List of manifolds in the product.\n",
" default_point_type : {'auto', 'vector', 'matrix', 'other'}\n",
" Optional. Default value is 'auto', which will implement as 'vector' unless all\n",
" factors have the same shape. Vector representation gives the point as a 1-d\n",
" array. Matrix representation allows for a point to be represented by an array of\n",
" shape (n, dim), if each manifold has default_point_type 'vector' with shape\n",
" (dim,). 'other' will behave as `matrix` but for higher dimensions.\n",
" \"\"\"\n",
"\n",
" def __init__(self, factors, default_point_type=\"auto\", equip=True):\n",
" geomstats.errors.check_parameter_accepted_values(\n",
" default_point_type,\n",
" \"default_point_type\",\n",
" [\"auto\", \"vector\", \"matrix\", \"other\"],\n",
" )\n",
"\n",
" factors = tuple(factors)\n",
"\n",
" factor_dims = [factor.dim for factor in factors]\n",
" factor_default_coords_types = [factor.default_coords_type for factor in factors]\n",
"\n",
" dim = sum(factor_dims)\n",
"\n",
" shape = _find_product_shape(factors, default_point_type)\n",
"\n",
" if \"extrinsic\" in factor_default_coords_types:\n",
" default_coords_type = \"extrinsic\"\n",
" else:\n",
" default_coords_type = \"intrinsic\"\n",
"\n",
" if default_coords_type == \"extrinsic\":\n",
" factor_embedding_spaces = [\n",
" manifold.embedding_space\n",
" if hasattr(manifold, \"embedding_space\")\n",
" else manifold\n",
" for manifold in factors\n",
" ]\n",
" # TODO: need to revisit due to removal of scales\n",
" self.embedding_space = ProductManifold(factor_embedding_spaces)\n",
"\n",
" cum_index = (\n",
" gs.cumsum(factor_dims)[:-1]\n",
" if default_coords_type == \"intrinsic\"\n",
" else self.embedding_space._cum_index\n",
" )\n",
"\n",
" super().__init__(\n",
" factors=factors,\n",
" cum_index=cum_index,\n",
" pool_outputs=True,\n",
" has_mixed_fields=_has_mixed_fields(factors),\n",
" dim=dim,\n",
" shape=shape,\n",
" default_coords_type=default_coords_type,\n",
" equip=equip,\n",
" )\n",
"\n",
" def _default_metric(self):\n",
" return ProductRiemannianMetric\n",
"\n",
" def _pool_outputs_from_function(self, outputs):\n",
" \"\"\"Collect outputs for each product to be returned.\n",
"\n",
" If each element of the output is a boolean array of the same shape, test along\n",
" the list whether all elements are True and return a boolean array of the same\n",
" shape.\n",
"\n",
" Otherwise, if each element of the output has a shape compatible with points of\n",
" the corresponding factor, an attempt is made to map the list of points to a\n",
" point in the product by embed_to_product.\n",
"\n",
" Parameters\n",
" ----------\n",
" outputs : list\n",
" A list of outputs which must be pooled\n",
"\n",
" Returns\n",
" -------\n",
" pooled_output : array-like, shape {(...,), (..., self.shape)}\n",
" \"\"\"\n",
" # TODO: simplify after cleaning gs.squeeze\n",
" all_arrays = gs.all([gs.is_array(factor_output) for factor_output in outputs])\n",
" if (\n",
" all_arrays\n",
" and _all_equal([factor_output.shape for factor_output in outputs])\n",
" and gs.all([gs.is_bool(factor_output) for factor_output in outputs])\n",
" or (not all_arrays)\n",
" ):\n",
" outputs = gs.stack([gs.array(factor_output) for factor_output in outputs])\n",
" outputs = gs.all(outputs, axis=0)\n",
" return outputs\n",
"\n",
" try:\n",
" return self.embed_to_product(outputs)\n",
" except geomstats.errors.ShapeError:\n",
" raise RuntimeError(\n",
" \"Could not combine outputs - they are not points of the individual\"\n",
" \" factors.\"\n",
" )\n",
" except ValueError:\n",
" raise RuntimeError(\n",
" \"Could not combine outputs, probably because they could\"\n",
" \" not be concatenated or stacked.\"\n",
" )\n",
"\n",
" def belongs(self, point, atol=gs.atol):\n",
" \"\"\"Test if a point belongs to the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Point.\n",
" atol : float,\n",
" Tolerance.\n",
"\n",
" Returns\n",
" -------\n",
" belongs : array-like, shape=[...,]\n",
" Boolean evaluating if the point belongs to the manifold.\n",
" \"\"\"\n",
" belongs = self._iterate_over_factors(\"belongs\", {\"point\": point, \"atol\": atol})\n",
" return belongs\n",
"\n",
" def regularize(self, point):\n",
" \"\"\"Regularize the point into the manifold's canonical representation.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Point to be regularized.\n",
"\n",
" Returns\n",
" -------\n",
" regularized_point : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Point in the manifold's canonical representation.\n",
" \"\"\"\n",
" regularized_point = self._iterate_over_factors(\"regularize\", {\"point\": point})\n",
" return regularized_point\n",
"\n",
" def random_point(self, n_samples=1, bound=1.0):\n",
" \"\"\"Sample in the product space from the product distribution.\n",
"\n",
" The distribution used is the product of the distributions used by the\n",
" random_point methods of each individual factor manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" n_samples : int, optional\n",
" Number of samples.\n",
" bound : float\n",
" Bound of the interval in which to sample for non compact manifolds.\n",
" Optional, default: 1.\n",
"\n",
" Returns\n",
" -------\n",
" samples : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Points sampled from the manifold.\n",
" \"\"\"\n",
" samples = self._iterate_over_factors(\n",
" \"random_point\", {\"n_samples\": n_samples, \"bound\": bound}\n",
" )\n",
" return samples\n",
"\n",
" def random_tangent_vec(self, base_point, n_samples=1):\n",
" \"\"\"Sample on the tangent space from the product distribution.\n",
"\n",
" The distribution used is the product of the distributions used by the\n",
" random_tangent_vec methods of each individual factor manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" base_point : array-like, shape=[..., n, n]\n",
" Base point of the tangent space.\n",
" Optional, default: None.\n",
" n_samples : int\n",
" Number of samples.\n",
" Optional, default: 1.\n",
"\n",
" Returns\n",
" -------\n",
" samples : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Points sampled in the tangent space of the product manifold at base_point.\n",
" \"\"\"\n",
" samples = self._iterate_over_factors(\n",
" \"random_tangent_vec\", {\"base_point\": base_point, \"n_samples\": n_samples}\n",
" )\n",
" return samples\n",
"\n",
" def projection(self, point):\n",
" \"\"\"Project a point onto product manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" point : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Point in product manifold.\n",
"\n",
" Returns\n",
" -------\n",
" projected : array-like, shape=[..., {dim, embedding_space.dim,\n",
" [n_manifolds, dim_each]}]\n",
" Projected point.\n",
" \"\"\"\n",
" projected_point = self._iterate_over_factors(\"projection\", {\"point\": point})\n",
" return projected_point\n",
"\n",
" def to_tangent(self, vector, base_point):\n",
" \"\"\"Project a vector to a tangent space of the manifold.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., dim]\n",
" Vector.\n",
" base_point : array-like, shape=[..., dim]\n",
" Point on the manifold.\n",
"\n",
" Returns\n",
" -------\n",
" tangent_vec : array-like, shape=[..., dim]\n",
" Tangent vector at base point.\n",
"\n",
" Notes\n",
" -----\n",
" The tangent space of the product manifold is the direct sum of\n",
" tangent spaces.\n",
" \"\"\"\n",
" tangent_vec = self._iterate_over_factors(\n",
" \"to_tangent\", {\"base_point\": base_point, \"vector\": vector}\n",
" )\n",
" return tangent_vec\n",
"\n",
" def is_tangent(self, vector, base_point=None, atol=gs.atol):\n",
" \"\"\"Check whether the vector is tangent at base_point.\n",
"\n",
" The tangent space of the product manifold is the direct sum of\n",
" tangent spaces.\n",
"\n",
" Parameters\n",
" ----------\n",
" vector : array-like, shape=[..., dim]\n",
" Vector.\n",
" base_point : array-like, shape=[..., dim]\n",
" Point on the manifold.\n",
" Optional, default: None\n",
" atol : float\n",
" Absolute tolerance.\n",
" Optional, default: backend atol.\n",
"\n",
" Returns\n",
" -------\n",
" is_tangent : bool\n",
" Boolean denoting if vector is a tangent vector at the base point.\n",
" \"\"\"\n",
" is_tangent = self._iterate_over_factors(\n",
" \"is_tangent\", {\"base_point\": base_point, \"vector\": vector, \"atol\": atol}\n",
" )\n",
" return is_tangent\n"
]
}
],
"source": [
"import inspect\n",
"\n",
"from geomstats.geometry.product_manifold import ProductManifold\n",
"\n",
"for line in inspect.getsourcelines(ProductManifold)[0]:\n",
" line = line.replace(\"\\n\", \"\")\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"id": "9e913791",
"metadata": {},
"source": [
"# Conclusion"
]
},
{
"cell_type": "markdown",
"id": "7ff91bfd",
"metadata": {},
"source": [
"Key takeaways from this notebook:\n",
"\n",
"1. There are three ways to define a manifold, and each of these three definitions provide a different option for manifold implementation.\n",
"2. Learning to work with manifolds is useful because many datasets naturally lie on manifolds.\n",
"3. The `Manifold` class stores information about many types of manifolds. Each of the subclasses implements a different $\\textit{type}$ of manifold (for example, `LevelSet`), and specific manifolds (for example, `Hypersphere`) are implemented within these subclasses."
]
}
],
"metadata": {
"backends": [
"numpy",
"autograd"
],
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}