galactic.algebras.convex.descriptions.numerical.hull.core#

galactic.algebras.convex.descriptions.numerical.hull.core module

Define classes for representing numerical descriptions and predicates.

One class for representing numerical descriptions:

Two classes for representing numerical predicates:

  • HalfSpace for predicates defined by a half-space

  • Segment for predicates defined by a segment

class HalfSpace(**data)#

Bases: BasicPredicate

The HalfSpace class is used to represent a half-space membership test.

Parameters:
  • space – The space of the predicate, which should be a tuple of two Number

  • a – The coefficient of the first variable in the half-space inequality.

  • b – The coefficient of the second variable in the half-space inequality.

  • c – The constant term in the half-space inequality.

Example

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, Number
>>> from galactic.algebras.convex.descriptions.numerical.hull.core import HalfSpace
>>> space = (
...     Number(components=(Component(expr="[0]"),), name="x"),
...     Number(components=(Component(expr="[1]"),), name="y"),
... )
>>> predicate = HalfSpace(space=space, a=1, b=2, c=3)
>>> predicate
<galactic.algebras.convex.descriptions.numerical.hull.core.HalfSpace object at 0x...>
>>> str(predicate)
'x + 2β‹…y + 3 ≀ 0'
>>> print(predicate(Item(value=[0, 1])))
False
>>> print(predicate(Item(value=[-2, -1])))
True

Warning

To reduce floating-point issues, the comparison uses a precision of 1e-12.

property template: str#

Get the template.

Returns:

The template

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Segment(**data)#

Bases: BasicPredicate

Represent a segment membership test.

The segment is defined by two endpoints (x1, y1) and (x2, y2).

Parameters:
  • space – The space of the predicate, which should be a tuple of two Number

  • x1 – The x-coordinate of the first endpoint of the segment.

  • y1 – The y-coordinate of the first endpoint of the segment.

  • x2 – The x-coordinate of the second endpoint of the segment.

  • y2 – The y-coordinate of the second endpoint of the segment.

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, Number
>>> from galactic.algebras.convex.descriptions.numerical.hull.core import Segment
>>> space = (
...     Number(components=(Component(expr="[0]"),), name="x"),
...     Number(components=(Component(expr="[1]"),), name="y"),
... )
>>> predicate = Segment(space=space, x1=1, y1=2, x2=3, y2=4)
>>> predicate
<galactic.algebras.convex.descriptions.numerical.hull.core.Segment object at 0x...>
>>> str(predicate)
'(x, y) ∈ Segment((1, 2), (3, 4))'
>>> print(predicate(Item(value=[2, 3])))
True
>>> print(predicate(Item(value=[0, 0])))
False
>>> print(predicate(Item(value=[1, 2])))
True
>>> print(predicate(Item(value=[3, 4])))
True

Warning

  • To reduce floating-point issues, the comparison uses a precision of 1e-12.

  • The endpoint coordinates are sorted internally. As a result, x1, y1 and x2, y2 may be reordered after initialization, while representing the same geometric segment. This canonical ordering is required so that hashing and equality are consistent for equivalent segments.

property template: str#

Get the template.

Returns:

The template

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class NumericalDescription(**data)#

Bases: Description

Represent a numerical description space.

It computes the descriptions of a collection of individuals by computing the convex hull of numerical predicates.

Parameters:

space (tuple[Number, Number] | tuple[Number]) – The space of characteristics, which can be one or two Number instances.

ALIAS#

The alias of the type in the resource registry.

Type:

ClassVar[str] = β€œnumerical.description.Numerical”

Example

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Number, Component
>>> from galactic.algebras.convex.descriptions.numerical.hull.core import (
...     NumericalDescription,
... )
>>> x = Number(components=(Component(expr="[0]"),), name="x")
>>> y = Number(components=(Component(expr="[1]"),), name="y")
>>> description = NumericalDescription(space=(x, y))
>>> predicates = description([])
>>> [str(attr) for attr in predicates]
['βŠ₯']
>>> predicates = description(
...     [
...         Item(value=[0, 0]),
...         Item(value=[1, 0]),
...         Item(value=[1, 1]),
...         Item(value=[0, 1]),
...     ]
... )
>>> [str(attr) for attr in predicates]
['-y ≀ 0', 'x - 1 ≀ 0', '-x ≀ 0', 'y - 1 ≀ 0']
>>> predicates = description([Item(value=[0, 1])])
>>> [str(attr) for attr in predicates]
['(x, y) = (0.0, 1.0)']
>>> predicates = description([Item(value=[1, 2]), Item(value=[3, 4])])
>>> [str(attr) for attr in predicates]
['(x, y) ∈ Segment((1, 2), (3, 4))']

Configuration

If you want to specify a numerical description in a TOML file, you can use the following syntax:

[components.my_component1]
"$type" = "core.Component"
expr = "value1"

[components.my_component2]
"$type" = "core.Component"
expr = "value2"

[characteristics.my_characteristic1]
"$type" = "core.characteristic.Number"
components = [
  {"$ref" = "components.my_component1"}
]
name = "my_name1"

[characteristics.my_characteristic2]
"$type" = "core.characteristic.Number"
components = [
  {"$ref" = "components.my_component2"}
]
name = "my_name2"

[descriptions.my_description]
"$type" = "numerical.description.Numerical"
space = [
  {"$ref" = "characteristics.my_characteristic1"},
  {"$ref" = "characteristics.my_characteristic2"}
]
Raises:

TypeError – If any element in the space is not an instance of Number or if the number of characteristics is not between the minimum and maximum allowed.

model_config: ClassVar[ConfigDict] = {'validate_assignment': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].