Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: update codegen tests #692

Merged
merged 3 commits into from
Jan 15, 2025

Conversation

AayushSabharwal
Copy link
Contributor

No description provided.

test/code.jl Outdated

@test toexpr(a^-2) == :($(/)(1, $(^)(a, 2)))
@test toexpr(a^-2, nanmath_st) == :($(/)(1, $(NaNMath.pow)(a, 2)))
@test toexpr(NaNMath.pow(a, -2)) == :($(NaNMath.pow)(a, -2))
@test toexpr(NaNMath.pow(a, -2), nanmath_st) == :($(NaNMath.pow)(a, -2))
@test toexpr(NaNMath.pow(a, -2)) == :($(NaNMath.pow)($(inv)(a), 2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaNMath.pow(a, -2) turns into a ^ (-2). Codegen adds the NaNMath.pow back in https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/code.jl#L141.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make it not do that if the exponent is an integer

src/code.jl Outdated
@@ -146,11 +146,13 @@ function function_to_expr(op::typeof(^), O, st)
return toexpr(Term(inv, Any[ex]), st)
else
args = Any[Term(inv, Any[ex]), -args[2]]
op = get(st.rewrites, :nanmath, false) ? op : NaNMath.pow
op = get(st.rewrites, :nanmath, false) || args[2] isa Integer ? op : NaNMath.pow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this at all, see #689? In any case, the existing check is wrong and should be

Suggested change
op = get(st.rewrites, :nanmath, false) || args[2] isa Integer ? op : NaNMath.pow
op = get(st.rewrites, :nanmath, false) === true && !(args[2] isa Integer) ? NaNMath.pow : op

src/code.jl Outdated
return toexpr(Term(op, args), st)
end
end
get(st.rewrites, :nanmath, false) === true || return nothing
if !get(st.rewrites, :nanmath, false) || args[2] isa Integer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit safer:

Suggested change
if !get(st.rewrites, :nanmath, false) || args[2] isa Integer
if get(st.rewrites, :nanmath, false) !== true || args[2] isa Integer

Additionally, I guess it would be more efficient to move this to the beginning of the function body?

@ChrisRackauckas ChrisRackauckas merged commit cecc0ca into JuliaSymbolics:master Jan 15, 2025
7 of 10 checks passed
@AayushSabharwal AayushSabharwal deleted the as/fix-tests branch January 15, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants