|
| 1 | +"""Latin square 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.latin_squares_ops._models import ( |
| 10 | + LatinSquareCheckResult, |
| 11 | + LatinSquareRequest, |
| 12 | + LatinSquareTransposeResult, |
| 13 | + OrthogonalityRequest, |
| 14 | + OrthogonalityResult, |
| 15 | +) |
| 16 | +from jacobian.math.latin_squares_ops._operations import ( |
| 17 | + compute_latin_square_check, |
| 18 | + compute_latin_square_transpose, |
| 19 | + compute_orthogonality, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def _op[RequestT: StrictModel, ResultT: StrictModel]( |
| 24 | + operation_id: str, |
| 25 | + title: str, |
| 26 | + description: str, |
| 27 | + request_model: type[RequestT], |
| 28 | + result_model: type[ResultT], |
| 29 | + operation: Callable[[RequestT], ResultT], |
| 30 | + *tags: str, |
| 31 | + examples: tuple[OperationExample, ...] = (), |
| 32 | + version: str = "1", |
| 33 | +) -> MathTool[RequestT, ResultT]: |
| 34 | + return MathTool( |
| 35 | + operation_id=operation_id, |
| 36 | + version=version, |
| 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 | +TOOLS: tuple[MathTool[Any, Any], ...] = ( |
| 48 | + _op( |
| 49 | + "latin_square.check", |
| 50 | + "Check if a matrix is a Latin square", |
| 51 | + "Verify that each row and column contains every symbol 0..n-1 " |
| 52 | + "exactly once.", |
| 53 | + LatinSquareRequest, |
| 54 | + LatinSquareCheckResult, |
| 55 | + compute_latin_square_check, |
| 56 | + "latin-square", |
| 57 | + "verification", |
| 58 | + "exact", |
| 59 | + examples=( |
| 60 | + example( |
| 61 | + "z2_latin_square", |
| 62 | + "Check the 2x2 Latin square [[0,1],[1,0]].", |
| 63 | + { |
| 64 | + "square": { |
| 65 | + "order": 2, |
| 66 | + "cells": [[0, 1], [1, 0]], |
| 67 | + }, |
| 68 | + }, |
| 69 | + ), |
| 70 | + ), |
| 71 | + ), |
| 72 | + _op( |
| 73 | + "latin_square.orthogonality.check", |
| 74 | + "Check orthogonality of two Latin squares", |
| 75 | + "Check whether two Latin squares of the same order are orthogonal, " |
| 76 | + "i.e., all ordered pairs of entries are distinct.", |
| 77 | + OrthogonalityRequest, |
| 78 | + OrthogonalityResult, |
| 79 | + compute_orthogonality, |
| 80 | + "latin-square", |
| 81 | + "orthogonality", |
| 82 | + "exact", |
| 83 | + examples=( |
| 84 | + example( |
| 85 | + "orthogonal_z2", |
| 86 | + "Check orthogonality of [[0,1],[1,0]] and [[0,1],[1,0]].", |
| 87 | + { |
| 88 | + "square_a": { |
| 89 | + "order": 2, |
| 90 | + "cells": [[0, 1], [1, 0]], |
| 91 | + }, |
| 92 | + "square_b": { |
| 93 | + "order": 2, |
| 94 | + "cells": [[0, 1], [1, 0]], |
| 95 | + }, |
| 96 | + }, |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + _op( |
| 101 | + "latin_square.transpose.compute", |
| 102 | + "Transpose a Latin square", |
| 103 | + "Swap rows and columns of a Latin square.", |
| 104 | + LatinSquareRequest, |
| 105 | + LatinSquareTransposeResult, |
| 106 | + compute_latin_square_transpose, |
| 107 | + "latin-square", |
| 108 | + "transpose", |
| 109 | + "exact", |
| 110 | + examples=( |
| 111 | + example( |
| 112 | + "transpose_z2", |
| 113 | + "Transpose [[0,1],[1,0]].", |
| 114 | + { |
| 115 | + "square": { |
| 116 | + "order": 2, |
| 117 | + "cells": [[0, 1], [1, 0]], |
| 118 | + }, |
| 119 | + }, |
| 120 | + ), |
| 121 | + ), |
| 122 | + ), |
| 123 | +) |
| 124 | + |
| 125 | +__all__ = ["TOOLS"] |
0 commit comments