Math Grader
Answer checker API that uses sympy to simplify expressions and check for equality.
Call grade_answer(given_answer: str, ground_truth: str).
are_equal_under_sympy(ground_truth_normalized: str, given_normalized: str) -> bool
Check if two expressions are mathematically equivalent using sympy.
Subtracts the two expressions and simplifies the result. If the simplified difference equals zero, the expressions are equivalent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ground_truth_normalized
|
str
|
The normalized ground truth expression. |
required |
given_normalized
|
str
|
The normalized given expression to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the expressions are mathematically equivalent, False otherwise. |
Source code in pita/utils/grading_utils/math/math_grader.py
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 | |
count_unknown_letters_in_expr(expr: str) -> int
Count the number of unknown letters in an expression.
Removes known mathematical function names (sqrt, frac) and counts the remaining alphabetic characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
str
|
A mathematical expression string. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of distinct unknown alphabetic characters in the expression. |
Source code in pita/utils/grading_utils/math/math_grader.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
grade_answer(given_answer: str, ground_truth: str) -> bool
Grade a given answer against the ground truth.
The answer will be considered correct if: (a) it normalizes to the same string as the ground truth answer OR (b) sympy can simplify the difference between the expressions to 0
Special handling for: - Tuples and intervals (must match structure and order) - Fractions (must be in reduced form) - Integers (must be exact matches, no decimal equivalents)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
given_answer
|
str
|
The answer to grade. |
required |
ground_truth
|
str
|
The correct answer to compare against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the answer is correct, False otherwise. |
Source code in pita/utils/grading_utils/math/math_grader.py
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 | |
should_allow_eval(expr: str) -> bool
Determine if an expression is safe to evaluate with sympy.
Checks for potentially problematic patterns that might cause sympy to hang or fail, including too many unknown variables and known bad patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
str
|
A mathematical expression string. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the expression is safe to evaluate, False otherwise. |
Source code in pita/utils/grading_utils/math/math_grader.py
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 | |
split_tuple(expr: str) -> List[str]
Split the elements in a tuple or interval.
Handles well-formatted commas in large numbers while splitting tuple elements. Recognizes tuples by their bracketing characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
str
|
A string representing a tuple, interval, or single value. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
A list of string elements. If the expression is a tuple/interval, |
List[str]
|
returns the individual elements; otherwise returns a list containing |
List[str]
|
the original expression. |
Source code in pita/utils/grading_utils/math/math_grader.py
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 | |