Skip to content

feat(math): add integral binary quadratic forms domain - #2113

Merged
morluto merged 4 commits into
mainfrom
feat/binary-quadratic-forms-1830
Aug 21, 2026
Merged

feat(math): add integral binary quadratic forms domain#2113
morluto merged 4 commits into
mainfrom
feat/binary-quadratic-forms-1830

Conversation

@morluto

@morluto morluto commented Aug 20, 2026

Copy link
Copy Markdown
Owner

Summary

Implement the primitive positive-definite integral binary quadratic form domain (issue #1830) with five exact operations.

Operations

Operation Description
Validate coefficients as a primitive positive-definite form with negative discriminant D ≡ 0 or 1 (mod 4), returning the discriminant and symmetric Gram matrix
Exact evaluation Q(x,y) = a·x² + b·xy + c·y² with primitive-pair status
Gauss reduction to canonical reduced form with SL₂(Z) witness matrix and step ledger
Decide proper equivalence by comparing canonical reduced representatives, returning the exact SL₂(Z) transformation witness
Enumerate all reduced primitive positive-definite classes of a given discriminant, returning the complete class set and class number h(D)

Design

  • Direct implementation: No external library needed. All computations use exact integer arithmetic.
  • Gauss reduction: Standard S and T transformations composed iteratively. The reduction terminates because the minimum coefficient decreases at each step. The complete SL₂(Z) witness is tracked through composition.
  • Proper equivalence: Two forms are properly equivalent iff they have the same discriminant and the same canonical reduced representative. The witness is constructed by composing the reduction witnesses.
  • Class enumeration: For a discriminant D, enumerate all (a,b,c) with |b| ≤ a ≤ c, b² - 4ac = D, gcd(a,b,c) = 1, and the reduced inequalities with tie-breaking.
  • Fail-closed binding: Each result model re-runs the native kernel to verify exactness.

Testing

  • 22 known-answer, boundary, and adversarial tests covering:
    • Check: primitive positive-definite, non-positive-definite, nonnegative discriminant, imprimitive
    • Evaluate: origin, (1,0), (2,3), wrong-value rejection
    • Reduce: already-reduced, [5,3,1]→[1,1,3], discriminant preservation, idempotency
    • Proper equivalence: self, different discriminants, equivalent forms, non-equivalent same-discriminant
    • Reduced classes: D=-3 (h=1), D=-4 (h=1), D=-23 (h=3), D=-20 (h=2), all classes verified reduced for D ∈ {-3,-4,-7,-8,-11,-15,-19,-20,-23,-43,-47,-163}

Issue

Closes #1838 — integral binary quadratic forms: reduction, proper-class, composition, and representation operations.

Continue this on Linzumi

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6d305f6a5e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/jacobian/math/integral_binary_quadratic_forms/_operations.py
Comment thread src/jacobian/math/integral_binary_quadratic_forms/_models.py Outdated
Comment thread src/jacobian/math/integral_binary_quadratic_forms/_models.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 37ad404a5e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/jacobian/math/integral_binary_quadratic_forms/_models.py
Comment thread src/jacobian/math/integral_binary_quadratic_forms/_models.py
Comment thread src/jacobian/math/hypergraphs/_operations.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fb2cc869cf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +143 to +148
if (
self.a == self.reduced_a
and self.b == self.reduced_b
and self.c == self.reduced_c
):
return self

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the matrix for already-reduced forms

When the input is already reduced, this early return skips both the determinant and transformation checks. For example, a result with identical original/reduced coefficients but matrix=((2, 0), (0, 2)) is accepted even though the advertised SL₂(Z) witness has determinant 4, so corrupted producer output can pass result validation; validate the matrix on this path as well.

AGENTS.md reference: AGENTS.md:L103-L114

Useful? React with 👍 / 👎.

Comment on lines +177 to +180
@model_validator(mode="after")
def bind_equivalence(self) -> Self:
if self.status == "PROPERLY_EQUIVALENT" and self.matrix is not None:
p, q = self.matrix[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind the equivalence status to the forms

When a result carries NOT_PROPERLY_EQUIVALENT, this condition performs no mathematical validation at all; even identical forms such as (1,1,1) and (1,1,1) are accepted with that false status. A producer regression would therefore escape result construction and expose an incorrect exact decision, so recompute or otherwise bind both status branches to the supplied forms.

AGENTS.md reference: AGENTS.md:L74-L77

Useful? React with 👍 / 👎.

Comment on lines +204 to +205
if self.class_number != len(self.classes):
raise ValueError("class_number must equal the number of classes")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Verify that the reduced class list is complete

This checks only that class_number matches the supplied list length, not that the list is the complete class set promised by the operation. For example, ReducedClassesResult(discriminant=-23, classes=(), class_number=0) validates even though the exact class number is 3, allowing truncated or empty producer output to revalidate as an exact result; bind the tuple against the complete enumeration.

AGENTS.md reference: AGENTS.md:L103-L114

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a03051bbf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +25 to +27
a: int
b: int
c: int

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a canonical form value across these operations

The form is represented here as three raw coefficients, again as form1/form2 tuples for equivalence, and as separate original/reduced fields in results. Consequently, even the serialized output of the check operation cannot be passed unchanged to evaluate or reduce because it contains extra status/result fields, forcing callers to reconstruct (a,b,c) manually. Introduce one domain-owned binary-quadratic-form value and use it consistently in producer results and consumer requests.

AGENTS.md reference: AGENTS.md:L95-L97

Useful? React with 👍 / 👎.

Comment on lines +25 to +27
a: int
b: int
c: int

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bound evaluation coefficients before computing

Unlike x and y and the other coefficient-bearing requests, these fields accept integers of arbitrary magnitude. A native or successfully decoded request containing a coefficient with millions of digits therefore reaches _evaluate, which performs multiplication and constructs an equally unbounded exact result on the synchronous execution path. Apply an explicit coefficient budget in this request model so accepted inputs, intermediate work, and outputs remain bounded.

AGENTS.md reference: AGENTS.md:L128-L135

Useful? React with 👍 / 👎.

Comment on lines +81 to +87
if self.status == "PRIMITIVE_POSITIVE_DEFINITE":
if self.a is None or self.b is None or self.c is None:
raise ValueError("accepted form must carry coefficients")
if self.discriminant is None:
raise ValueError("accepted form must carry discriminant")
if self.discriminant != self.b**2 - 4 * self.a * self.c:
raise ValueError("discriminant must be b^2 - 4ac")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the claimed positive-definite status

When status is PRIMITIVE_POSITIVE_DEFINITE, this validator checks only that the discriminant matches the coefficients; it never verifies a > 0, a negative discriminant, or primitivity. For example, status="PRIMITIVE_POSITIVE_DEFINITE", a=-1, b=0, c=1, discriminant=4 validates despite being indefinite, allowing an incorrect authoritative classification to revalidate as an exact result. Recheck the defining domain predicates in this branch.

AGENTS.md reference: AGENTS.md:L156-L157

Useful? React with 👍 / 👎.

…aluate, reduce, equivalence, and class enumeration

Implement the primitive positive-definite integral binary quadratic form
domain (issue #1830) with five exact operations:

- number_theory.binary_quadratic_form.check: validate coefficients as a
  primitive positive-definite form with negative discriminant D ≡ 0 or 1
  (mod 4), returning the discriminant and symmetric Gram matrix
- number_theory.binary_quadratic_form.evaluate: exact evaluation
  Q(x,y) = a*x^2 + b*x*y + c*y^2 with primitive-pair status
- number_theory.binary_quadratic_form.reduce: Gauss reduction to canonical
  reduced form with SL_2(Z) witness matrix and step ledger
- number_theory.binary_quadratic_form.proper_equivalence.decide: decide
  proper equivalence by comparing canonical reduced representatives,
  returning the exact SL_2(Z) transformation witness
- number_theory.binary_quadratic_form.reduced_classes.compute: enumerate
  all reduced primitive positive-definite classes of a given discriminant,
  returning the complete class set and class number h(D)

Each result model re-runs the native kernel to verify exactness
(fail-closed binding). All 22 known-answer, boundary, and adversarial
tests pass.
@morluto
morluto force-pushed the feat/binary-quadratic-forms-1830 branch from 4a03051 to dc80790 Compare August 21, 2026 01:53

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dc80790a78

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


@model_validator(mode="after")
def bind_result(self) -> Self:
if self.status == "PRIMITIVE_POSITIVE_DEFINITE":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind negative check results to their source form

When the status is NOT_IN_INITIAL_DOMAIN, the result omits the inspected coefficients and this validator performs no consistency check, so a result claiming that the valid form [1,1,1] is outside the domain can revalidate unchanged. Retain the source coefficients on this branch and verify the negative classification so the authoritative decision cannot be detached from its input.

AGENTS.md reference: AGENTS.md:L156-L157

Useful? React with 👍 / 👎.

reduced_b: int
reduced_c: int
matrix: tuple[tuple[int, int], tuple[int, int]]
steps: tuple[tuple[int, int, int, int, int, int, int, int, int, int], ...]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate every reduction step in the ledger

The advertised reduction certificate includes this step ledger, but result validation never checks that each tuple transforms its recorded source to its destination, that consecutive steps chain together, or that their product equals matrix. Consequently, replacing a producer's ledger with arbitrary ten-integer tuples still yields a valid exact result; replay these bounded steps and their composition during validation.

AGENTS.md reference: AGENTS.md:L156-L157

Useful? React with 👍 / 👎.

@morluto
morluto merged commit caf0438 into main Aug 21, 2026
26 checks passed
@morluto
morluto deleted the feat/binary-quadratic-forms-1830 branch August 21, 2026 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Finite-group representation theory] Add exact character tables, class functions, restriction, induction, and decomposition operations

1 participant