cds2.modeling
Symbolic mathematics: expression trees, differentiation, LaTeX export, equation solving.
Symbolic mathematics: expression trees, differentiation, LaTeX export.
Expression
Base class of the symbolic expression tree.
Source code in src/cds2/modeling.py
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 | |
Constant
Bases: Expression
A fixed numeric literal.
Source code in src/cds2/modeling.py
92 93 94 95 96 97 98 99 100 101 102 103 | |
Variable
Bases: Expression
A named free variable.
Source code in src/cds2/modeling.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
BinaryOp
Bases: Expression
Base for binary operators.
Source code in src/cds2/modeling.py
127 128 129 130 131 132 133 134 135 136 137 138 | |
Negate
Bases: Expression
Unary negation.
Source code in src/cds2/modeling.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
MathModel
Equation-centric modelling helper built on the expression tree.
Source code in src/cds2/modeling.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
residual(bindings)
Evaluate LHS - RHS under complete variable bindings.
Source code in src/cds2/modeling.py
557 558 559 560 | |
solve_equation(unknown, target_value, known, initial_guess=1.0)
Solve for unknown so that LHS equals target_value (Newton).
Source code in src/cds2/modeling.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
fit_parameters(parameters, observations, learning_rate=0.001, epochs=500)
Grid-free least-squares refinement of free parameters by GD.
Source code in src/cds2/modeling.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
symbol(name)
Shorthand factory for a named :class:Variable.
Source code in src/cds2/modeling.py
122 123 124 | |
diff(expression, variable)
Symbolic partial derivative of expression w.r.t. variable.
Source code in src/cds2/modeling.py
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 | |
integrate(expression, variable)
Symbolic indefinite integral of a polynomial expression w.r.t. variable.
Handles sums, constant factors, powers with numeric exponents (except
x**-1) and plain variables; anything else raises NotImplementedError.
The integration constant is omitted.
Source code in src/cds2/modeling.py
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 | |
polynomial_coefficients(expression, variable)
Coefficients (ascending powers) of the polynomial in variable.
Raises ValueError when the expression is not a polynomial in the
requested variable.
Source code in src/cds2/modeling.py
299 300 301 302 303 304 305 | |
solve_polynomial(lhs, rhs, unknown, real_only=False)
Roots of lhs = rhs treated as a polynomial in unknown.
Returns complex roots (ascending powers input); with real_only only
the real roots are returned, sorted ascending.
Source code in src/cds2/modeling.py
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 | |
simplify(expression, max_rounds=8)
Apply algebraic simplification rules until stable.
Source code in src/cds2/modeling.py
394 395 396 397 398 399 400 401 402 | |
to_string(expression)
Plain-text rendering used for structural comparison and printing.
Source code in src/cds2/modeling.py
471 472 473 474 475 476 477 478 479 480 481 482 | |
substitute(expression, replacements)
Replace variables with values or other expressions.
Source code in src/cds2/modeling.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
evaluate(expression, environment)
Numerically evaluate the expression under a variable binding.
Source code in src/cds2/modeling.py
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 | |
iter_postorder(expression)
Yield expression nodes depth-first, children before parents.
Source code in src/cds2/modeling.py
532 533 534 535 536 537 538 539 | |
to_latex(expression)
Render the expression as a LaTeX fragment.
Source code in src/cds2/modeling.py
542 543 544 | |