geometor.model.utils

Provides utility functions for the Model class.

This module collects general-purpose utility functions used throughout the package. It includes tools for symbolic expression simplification (cleaning), point comparison and sorting, and logging setup.

Functions

clean_expr(expr) → sympy.Expr

Canonicalize a symbolic geometric coordinate without invoking sp.simplify.

spread(l1, l2) → sympy.Expr

Calculate the spread of two lines.

compare_points(pt1, pt2) → int

Compare two points for sorting.

point_value(pt) → tuple[float, float]

Get the numerical coordinates of a point.

sort_points(pts) → list[sympy.geometry.Point]

Sort a list of points.

log_init(name) → None

Initialize logging for a session.

print_log([txt]) → None

Print text to stdout and log it to the file.

elapsed(start_time) → str

Calculate and format the elapsed time since start.

Module Contents

geometor.model.utils.clean_expr(expr: object) → sympy.Expr

Canonicalize a symbolic geometric coordinate without invoking sp.simplify.

Applies a fast tiered pipeline:

  1. Immediate O(1) return for Python int and SymPy Rational / Integer.

  2. Fraction combination (sympy.together()), bounded polynomial expansion, and denominator rationalization (sympy.radsimp() with symbolic=False).

  3. Conditional sympy.sqrtdenest() only when nested radicals are actually present.

Parameters:

expr – A numeric value or SymPy expression.

Returns:

A simplified exact sympy.Expr.

geometor.model.utils.spread(l1: sympy.geometry.Line, l2: sympy.geometry.Line) → sympy.Expr

Calculate the spread of two lines.

The spread is a rational invariant of two lines, equivalent to the square of the sine of the angle between them. It is a fundamental concept in rational trigonometry.

Parameters:
  • l1 – The first line.

  • l2 – The second line.

Returns:

The spread as a symbolic expression.

geometor.model.utils.compare_points(pt1: sympy.geometry.Point, pt2: sympy.geometry.Point) → int

Compare two points for sorting.

This comparison function orders points primarily by their x-coordinates and secondarily by their y-coordinates. It returns 1 if pt1 > pt2, -1 if pt1 < pt2, and 0 if they are equal.

Parameters:
  • pt1 – The first point.

  • pt2 – The second point.

Returns:

An integer indicating the relative order (-1, 0, 1).

geometor.model.utils.point_value(pt: sympy.geometry.Point) → tuple[float, float]

Get the numerical coordinates of a point.

This helper extracts the floating-point values of a point’s x and y coordinates, suitable for use in sorting keys or numerical comparisons.

Parameters:

pt – The point to evaluate.

Returns:

A tuple of (x, y) floats.

geometor.model.utils.sort_points(pts: list[sympy.geometry.Point]) → list[sympy.geometry.Point]

Sort a list of points.

This function sorts a list of points using the point_value helper as the key.

Parameters:

pts – The list of points to sort.

Returns:

The sorted list of points.

geometor.model.utils.log_init(name: str) → None

Initialize logging for a session.

Sets up a log directory in the user’s home Sessions folder and configures basic logging to a ‘build.log’ file.

Parameters:

name – The name of the session/subdirectory to create.

geometor.model.utils.print_log(txt: str = '') → None

Print text to stdout and log it to the file.

Parameters:

txt – The text to print and log.

geometor.model.utils.elapsed(start_time: float) → str

Calculate and format the elapsed time since start.

Parameters:

start_time – The starting timestamp (from timer()).

Returns:

A string representation of the elapsed timedelta.