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:
NumericalDescriptionclass.
Two classes for representing numerical predicates:
- class HalfSpace(**data)#
Bases:
BasicPredicateThe
HalfSpaceclass is used to represent a half-space membership test.- Parameters:
space β The space of the predicate, which should be a tuple of two
Numbera β 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.
- class Segment(**data)#
Bases:
BasicPredicateRepresent 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
Numberx1 β 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, y1andx2, y2may 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.
- class NumericalDescription(**data)#
Bases:
DescriptionRepresent 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.