File size: 15,782 Bytes
d06fa57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | """Expression nodes for the SRHarness symbolic engine."""
from __future__ import annotations
from copy import deepcopy
from dataclasses import dataclass, field
from collections.abc import Iterator
from typing import TYPE_CHECKING, Any, Mapping
if TYPE_CHECKING:
from .optimize import FitResult
class Expression:
"""Base class of every symbolic expression node."""
def __add__(self, other: Any) -> Expression:
return Binary("+", self, as_expression(other))
def __radd__(self, other: Any) -> Expression:
return Binary("+", as_expression(other), self)
def __sub__(self, other: Any) -> Expression:
return Binary("-", self, as_expression(other))
def __rsub__(self, other: Any) -> Expression:
return Binary("-", as_expression(other), self)
def __mul__(self, other: Any) -> Expression:
return Binary("*", self, as_expression(other))
def __rmul__(self, other: Any) -> Expression:
return Binary("*", as_expression(other), self)
def __truediv__(self, other: Any) -> Expression:
return Binary("/", self, as_expression(other))
def __rtruediv__(self, other: Any) -> Expression:
return Binary("/", as_expression(other), self)
def __pow__(self, other: Any) -> Expression:
return Binary("**", self, as_expression(other))
def __rpow__(self, other: Any) -> Expression:
return Binary("**", as_expression(other), self)
def __neg__(self) -> Expression:
return Unary("-", self)
def __getitem__(self, index: Index | tuple[Index, ...] | str) -> Expression:
if isinstance(index, tuple):
indices = tuple(as_index(item) for item in index)
else:
indices = (as_index(index),)
return Indexed(self, indices)
def evaluate(
self,
values: Mapping[str, Any] | None = None,
*,
parameters: Mapping[str, Any] | None = None,
time: Any = None,
delay_resolver: Any = None,
num_nodes: int | None = None,
) -> Any:
"""Evaluate this expression with NumPy values.
Args:
values: Values keyed by symbol name, or a context exposing ``data``
and optional ``num_nodes`` metadata.
parameters: Fitted parameter values keyed by parameter name.
time: Optional sample times.
delay_resolver: Optional callback that resolves delayed values.
num_nodes: Explicit node count for indexed expressions.
Returns:
The evaluated scalar or array.
"""
from .context import resolve_context
from .evaluation import evaluate
values, _, num_nodes = resolve_context(values, num_nodes=num_nodes)
return evaluate(
self,
values=values,
parameters=parameters,
time=time,
delay_resolver=delay_resolver,
num_nodes=num_nodes,
)
eval = evaluate
@property
def operands(self) -> tuple[Expression, ...]:
"""Child expressions, exposed as an immutable tuple.
Returns:
The direct child nodes in expression order.
"""
from .tree import children
return children(self)
def iter_preorder(self) -> Iterator[Expression]:
"""Yield this node followed by its descendants.
Yields:
Expression nodes in preorder.
"""
from .tree import iter_preorder
yield from iter_preorder(self)
def iter_postorder(self) -> Iterator[Expression]:
"""Yield descendants followed by this node.
Yields:
Expression nodes in postorder.
"""
from .tree import iter_postorder
yield from iter_postorder(self)
def copy(self) -> Expression:
"""Return an independent copy.
Returns:
A deep copy of this expression.
"""
return deepcopy(self)
def replace(self, old: Expression, new: Expression) -> Expression:
"""Return a tree in which the exact *old* node is replaced by *new*.
Args:
old: Existing expression node to replace.
new: Replacement expression node.
Returns:
A copied expression tree with matching nodes replaced.
"""
from .tree import replace
return replace(self, old, new)
def to_str(
self,
*,
latex: bool = False,
number_format: str = "",
) -> str:
"""Render the expression as plain text or LaTeX.
Args:
latex: Whether to render LaTeX notation.
number_format: Format specification for numeric literals.
Returns:
The rendered expression.
"""
from .render import render
return render(self, latex=latex, number_format=number_format)
def to_tree(self, *, number_format: str = "") -> str:
"""Render a compact preorder tree for diagnostics.
Args:
number_format: Format specification for numeric literals.
Returns:
A multiline representation of the expression tree.
"""
from .tree import children
lines = [self.to_str(number_format=number_format)]
def visit(node: Expression, prefix: str, connector: str) -> None:
lines.append(f"{prefix}{connector}{node.to_str(number_format=number_format)}")
values = children(node)
continuation = " " if connector == "└ " else "│ "
for index, child in enumerate(values):
last = index == len(values) - 1
visit(child, prefix + continuation, "└ " if last else "├ ")
values = children(self)
for index, child in enumerate(values):
visit(child, "", "└ " if index == len(values) - 1 else "├ ")
return "\n".join(lines)
def __len__(self) -> int:
return sum(1 for _ in self.iter_preorder())
def fit(
self,
values: Mapping[str, Any],
target: Any = None,
*,
initial: Mapping[str, Any] | None = None,
method: str = "BFGS",
options: Mapping[str, Any] | None = None,
num_nodes: int | None = None,
) -> FitResult:
"""Fit named and grouped parameters against a target array.
Args:
values: Values keyed by symbol name, or a context exposing ``data``,
``target``, and optional ``num_nodes`` metadata.
target: Target name or target values. Omit when supplied by context.
initial: Optional initial parameter values.
method: Optimization method name.
options: Optional optimizer settings.
num_nodes: Explicit node count for indexed expressions.
Returns:
The fitted expression, parameter values, predictions, and loss.
"""
from .context import resolve_context
from .optimize import fit
values, target, num_nodes = resolve_context(values, target, num_nodes)
if target is None:
raise ValueError("A target array or a context target is required for fitting.")
return fit(
self, values, target, initial=initial, method=method,
options=options, num_nodes=num_nodes,
)
def fold_constants(self) -> Expression:
"""Return a copy with closed numerical subexpressions evaluated.
Returns:
A simplified expression with closed numeric branches folded.
"""
from .analysis import fold_constants
return fold_constants(self)
def count_parameters(
self,
values: Mapping[str, Any] | None = None,
*,
parameters: Mapping[str, Any] | None = None,
) -> int:
"""Count independent fitted values represented by this expression.
Args:
values: Values keyed by symbol name.
parameters: Fitted parameter values keyed by parameter name.
Returns:
The number of independent scalar parameter values.
"""
from .analysis import count_parameters
return count_parameters(self, values, parameters=parameters)
def __str__(self) -> str:
from .render import render
return render(self)
@dataclass(frozen=True, slots=True)
class Number(Expression):
"""Fixed numeric literal."""
value: int | float
@dataclass(frozen=True, slots=True)
class Symbol(Expression):
"""Named input symbol with an optional bound value."""
name: str
value: Any = field(default=None, compare=False, repr=False)
@dataclass(frozen=True, slots=True)
class Parameter(Expression):
"""Named scalar parameter optimized during fitting."""
name: str
value: float | None = field(default=None, compare=False)
@dataclass(frozen=True, slots=True)
class GroupedParameter(Expression):
"""Parameter with one fitted value per category."""
by: Expression
name: str | None = None
value: Mapping[Any, float] | None = field(default=None, compare=False)
default: float | None = field(default=None, compare=False)
@dataclass(frozen=True, slots=True)
class Index:
"""Symbolic relation index."""
name: str
def __str__(self) -> str:
return self.name
@dataclass(frozen=True, slots=True)
class Unary(Expression):
"""Unary expression node."""
operator: str
operand: Expression
@dataclass(frozen=True, slots=True)
class Binary(Expression):
"""Binary expression node."""
operator: str
left: Expression
right: Expression
@dataclass(frozen=True, slots=True)
class Function(Expression):
"""Named function-call expression node."""
name: str
arguments: tuple[Expression, ...]
@dataclass(frozen=True, slots=True)
class Indexed(Expression):
"""Expression annotated with symbolic indices."""
base: Expression
indices: tuple[Index, ...]
@dataclass(frozen=True, slots=True)
class Reduction(Expression):
"""Sum reduction with an optional relation binder."""
indices: tuple[Index, ...]
operand: Expression
relation: Expression | None = None
@dataclass(frozen=True, slots=True)
class Gather(Expression):
"""Weighted collection of a structural expression on relation entries."""
relation: Expression
operand: Expression
@dataclass(frozen=True, slots=True)
class Aggregate(Expression):
"""Convenience aggregation node lowered to indexed syntax."""
relation: Expression
operand: Expression
@dataclass(frozen=True, slots=True)
class RelationLift(Expression):
"""Convenience source or target projection used inside an aggregation."""
role: str
operand: Expression
relation: Expression | None = None
def as_expression(value: Any) -> Expression:
"""Convert a numeric literal or expression into an expression node.
Args:
value: Existing expression or numeric literal.
Returns:
The corresponding expression node.
"""
if isinstance(value, Expression):
return value
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise TypeError(f"Expected an expression or number, got {type(value).__name__}.")
return Number(value)
Variable = Symbol
def as_index(value: Index | str) -> Index:
"""Convert an index name into an index node.
Args:
value: Existing index or valid Python identifier.
Returns:
The corresponding symbolic index.
"""
if isinstance(value, Index):
return value
if isinstance(value, str) and value.isidentifier():
return Index(value)
raise TypeError(f"Expected an index name, got {value!r}.")
def param(name: str, value: float | None = None) -> Parameter:
"""Create a named scalar parameter.
Args:
name: Parameter name shared by all matching occurrences.
value: Optional initial or fixed value.
Returns:
A symbolic scalar parameter.
"""
return Parameter(name, value)
def grouped_param(
by: Expression,
*,
name: str | None = None,
value: Mapping[Any, float] | None = None,
default: float | None = None,
) -> GroupedParameter:
"""Create a parameter with one fitted value per category.
Args:
by: Symbol or expression containing category labels.
name: Optional parameter-map name.
value: Optional initial values keyed by category.
default: Value used for categories absent from ``value``.
Returns:
A category-dependent parameter expression.
"""
return GroupedParameter(by, name=name, value=value, default=default)
def function(name: str, *arguments: Any) -> Function:
"""Create a supported symbolic function call.
Args:
name: Function name recognized by the evaluator.
*arguments: Function operands.
Returns:
A symbolic function node.
"""
return Function(name, tuple(as_expression(argument) for argument in arguments))
def reduction(
indices: Index | tuple[Index, ...],
operand: Any,
relation: Any = None,
) -> Reduction:
"""Create an indexed sum reduction.
Args:
indices: Symbolic indices.
operand: Expression being reduced or transformed.
relation: Relation expression that binds symbolic indices.
Returns:
A symbolic reduction node.
"""
if not isinstance(indices, tuple):
indices = (indices,)
return Reduction(
tuple(as_index(index) for index in indices),
as_expression(operand),
None if relation is None else as_expression(relation),
)
def gather(relation: Any, operand: Any) -> Gather:
"""Collect structural values at the nonzero entries of a relation.
Args:
relation: Indexed relation or relation-valued expression.
operand: Structural expression evaluated at relation coordinates.
Returns:
A relation-aligned symbolic field.
"""
return Gather(as_expression(relation), as_expression(operand))
def aggr(relation: Any, operand: Any = None) -> Expression:
"""Build and lower target-wise graph aggregation syntax.
Args:
relation: Edge relation, or a product containing it when ``operand`` is omitted.
operand: Message expression to aggregate by target node.
Returns:
The equivalent canonical indexed reduction.
"""
from .desugar import desugar
from .parser import _split_aggregation
if operand is None:
relation, operand = _split_aggregation(as_expression(relation))
return desugar(Aggregate(as_expression(relation), as_expression(operand)))
def targ(*arguments: Any) -> RelationLift:
"""Project node values onto relation targets inside ``aggr``.
Args:
*arguments: Either ``value`` or ``relation, value``.
Returns:
A target projection used by aggregation desugaring.
"""
if len(arguments) == 1:
return RelationLift("target", as_expression(arguments[0]))
if len(arguments) == 2:
return RelationLift("target", as_expression(arguments[1]), as_expression(arguments[0]))
raise TypeError("targ expects targ(value) or targ(relation, value).")
def sour(*arguments: Any) -> RelationLift:
"""Project node values onto relation sources inside ``aggr``.
Args:
*arguments: Either ``value`` or ``relation, value``.
Returns:
A source projection used by aggregation desugaring.
"""
if len(arguments) == 1:
return RelationLift("source", as_expression(arguments[0]))
if len(arguments) == 2:
return RelationLift("source", as_expression(arguments[1]), as_expression(arguments[0]))
raise TypeError("sour expects sour(value) or sour(relation, value).")
|