|
| 1 | +"""Quadratic form operation declarations.""" |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from jacobian._models import StrictModel |
| 7 | +from jacobian.catalog._examples import example |
| 8 | +from jacobian.catalog.models import MathTool, OperationExample |
| 9 | +from jacobian.math.quadratic_forms._models import ( |
| 10 | + DiscriminantRequest, |
| 11 | + DiscriminantResult, |
| 12 | + EvaluationRequest, |
| 13 | + EvaluationResult, |
| 14 | + SignatureRequest, |
| 15 | + SignatureResult, |
| 16 | +) |
| 17 | +from jacobian.math.quadratic_forms._operations import ( |
| 18 | + compute_discriminant, |
| 19 | + compute_signature, |
| 20 | + evaluate_form, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _op[RequestT: StrictModel, ResultT: StrictModel]( |
| 25 | + operation_id: str, |
| 26 | + title: str, |
| 27 | + description: str, |
| 28 | + request_model: type[RequestT], |
| 29 | + result_model: type[ResultT], |
| 30 | + operation: Callable[[RequestT], ResultT], |
| 31 | + *tags: str, |
| 32 | + examples: tuple[OperationExample, ...] = (), |
| 33 | +) -> MathTool[RequestT, ResultT]: |
| 34 | + return MathTool( |
| 35 | + operation_id=operation_id, |
| 36 | + version="1", |
| 37 | + title=title, |
| 38 | + description=description, |
| 39 | + request_type=request_model, |
| 40 | + result_type=result_model, |
| 41 | + run=operation, |
| 42 | + tags=tags, |
| 43 | + examples=examples, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +_FORM_2D = {"matrix": [[1, 0], [0, 1]]} |
| 48 | + |
| 49 | +TOOLS: tuple[MathTool[Any, Any], ...] = ( |
| 50 | + _op( |
| 51 | + "quadratic_form.evaluate.compute", |
| 52 | + "Evaluate a quadratic form q(x) = x^T A x", |
| 53 | + "Compute the exact integer value q(x) = x^T A x for an " |
| 54 | + "integral quadratic form (symmetric matrix) and integer vector.", |
| 55 | + EvaluationRequest, |
| 56 | + EvaluationResult, |
| 57 | + evaluate_form, |
| 58 | + "algebra", |
| 59 | + "quadratic-form", |
| 60 | + "exact", |
| 61 | + examples=( |
| 62 | + example( |
| 63 | + "identity_2d_at_3_4", |
| 64 | + "Evaluate x^T I x at (3, 4); " |
| 65 | + "the matrix must be symmetric and the vector length must match.", |
| 66 | + {"form": _FORM_2D, "vector": [3, 4]}, |
| 67 | + ), |
| 68 | + ), |
| 69 | + ), |
| 70 | + _op( |
| 71 | + "quadratic_form.discriminant.compute", |
| 72 | + "Compute the discriminant det(A) of a quadratic form", |
| 73 | + "Compute the exact determinant of the symmetric matrix " |
| 74 | + "representing the quadratic form, using SymPy for exact " |
| 75 | + "integer matrix computation.", |
| 76 | + DiscriminantRequest, |
| 77 | + DiscriminantResult, |
| 78 | + compute_discriminant, |
| 79 | + "algebra", |
| 80 | + "quadratic-form", |
| 81 | + "exact", |
| 82 | + examples=( |
| 83 | + example( |
| 84 | + "identity_2d_discriminant", |
| 85 | + "Compute det(I_2) = 1; the matrix must be symmetric.", |
| 86 | + {"form": _FORM_2D}, |
| 87 | + ), |
| 88 | + ), |
| 89 | + ), |
| 90 | + _op( |
| 91 | + "quadratic_form.signature.compute", |
| 92 | + "Compute the signature/inertia of a quadratic form", |
| 93 | + "Compute the inertia (n_positive, n_negative, n_zero) of a " |
| 94 | + "quadratic form using SymPy eigenvalue computation, with " |
| 95 | + "definiteness classification.", |
| 96 | + SignatureRequest, |
| 97 | + SignatureResult, |
| 98 | + compute_signature, |
| 99 | + "algebra", |
| 100 | + "quadratic-form", |
| 101 | + "exact", |
| 102 | + examples=( |
| 103 | + example( |
| 104 | + "identity_2d_signature", |
| 105 | + "Compute the signature of I_2; the matrix must be symmetric.", |
| 106 | + {"form": _FORM_2D}, |
| 107 | + ), |
| 108 | + ), |
| 109 | + ), |
| 110 | +) |
| 111 | + |
| 112 | +__all__ = ["TOOLS"] |
0 commit comments