"CodeEquivalence" (Comparison Method)
Determine if two pieces of code are equivalent after transformations.
Details
- The "CodeEquivalence" method attempts to determine intentional equivalence by transforming expressions into a canonical representation.
- When equivalence can be determined by transformation, no evaluation is performed and the comparison is not based on the expression's output. In particular, this makes it possible to compare code that produces non-deterministic outputs (e.g. random values, dates, etc.).
- The "CodeEquivalence" method is directly based on the CodeEquivalenceUtilities paclet.
- For mathematical expressions, "CodeEquivalence" relies on the Simplify function to determine equivalence. For more control, use the specific math methods "ArithmeticResult", "CalculusResult", "PolynomialResult" and "AlgebraicValue".
Examples
open allclose allBasic Examples (1)
Create an AssessmentFunction for a coding question:
Scope (1)
The "CodeEquivalence" method supports the comparison of code with random outputs:
Compare with equivalent code for random numbers with an increasing range:
Applications (2)
Create a QuestionObject for a coding problem:
Create a QuestionObject for a coding problem:
Properties & Relations (2)
Symbol names need not match exactly. Undefined symbols will match any other unique undefined symbols:
The "CodeEquivalence" method can compare mathematical expressions:
Dedicated mathematical methods like "CalculusResult" also find the equivalence:
Code equivalence will treat expressions as code, where symbol names are arbitrary and any distinct symbols are considered equivalent:
Mathematical comparison methods require symbol names to match: