Repository navigation
FIX: Fix lambdifying models that evaluate to have complex numbers - #599
Merged
bocklund merged 4 commits intoApr 12, 2025
Merged
Conversation
richardotis
approved these changes
Apr 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In PyCalphad we eventually represent all our exponents as floating points, even if they are actually integer values. This becomes troublesome because SymEngine evaluates small, negligible imaginary parts, as in the following example:
In this case, the imaginary part is not useful and causes an issue when we call
lambdifyin (build_functions) with the default argument thatreal=True. We probably haven't run into this before because our bases are typically symbolic (e.g.Tin power functions) and these are typically not evaluated to a complex number until the function is actually evaluated insidelambdify(where the imaginary part gets thrown away because we calllambdify(..., real=True)implicitly). Consequently, doing this replacement outside oflambdifycan't be a source of a regression becauselambdifyhas been throwing away any complex parts anyway.We end up doing this workaround to find complex numbers by
atomsand replace the imaginary unit with zero, which is working around SymEngine not having are(real) function and callingsubsorxreplacewith{I: 0.0}directly on the Model expression doesn't replace arbitrarily nested imaginary numbers.