From 3214d3b0dcfca15845e87d24a592d483ff483ad2 Mon Sep 17 00:00:00 2001 From: john verzani Date: Mon, 14 Aug 2023 17:19:14 -0400 Subject: [PATCH] Issue 504b (#513) * get docs working * odd error * odd error 2 * odd error 2 * simplify difference * push change to POW * version bump * run doctest fix=true * intercept sqrt, cbrt --- Project.toml | 2 +- docs/src/Tutorial/basic_operations.md | 2 +- src/lambdify.jl | 11 ++++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index ee23e36..c7d110b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "SymPy" uuid = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6" -version = "1.1.9" +version = "1.1.10" [deps] CommonEq = "3709ef60-1bee-4518-9f2f-acd86f176c50" diff --git a/docs/src/Tutorial/basic_operations.md b/docs/src/Tutorial/basic_operations.md index 907a4a6..d06fcac 100644 --- a/docs/src/Tutorial/basic_operations.md +++ b/docs/src/Tutorial/basic_operations.md @@ -515,7 +515,7 @@ julia> ex = sin(x)^2 + x^2 x + sin (x) julia> body = convert(Expr, ex) -:(SymPy.__POW__(x, 2) + SymPy.__POW__(sin(x), 2)) +:(x ^ 2 + sin(x) ^ 2) julia> syms = Symbol.(free_symbols(ex)) 1-element Vector{Symbol}: diff --git a/src/lambdify.jl b/src/lambdify.jl index 50a0726..c2c1b7b 100644 --- a/src/lambdify.jl +++ b/src/lambdify.jl @@ -42,15 +42,15 @@ __ZERO__(xs...) = 0 # not quite a match; NaN not θ(0) when evaluated at 0 w/o second argument __HEAVISIDE__ = (a...) -> (a[1] < 0 ? 0 : (a[1] > 0 ? 1 : (length(a) > 1 ? a[2] : NaN))) __POW__(x, y::Int) = Base.literal_pow(^, x, Val(y)) # otherwise -__POW__(a,b) = (a)^(b) +__POW__(a,b) = (@show a, b; (a)^(b)) # __SYMPY__ALL__, fn_map = Dict( "Add" => :+, "Sub" => :-, "Mul" => :*, # :(SymPy.__PROD__) "Div" => :/, - # "Pow" => :^, - "Pow" => :(SymPy.__POW__), + "Pow" => :^, + #"Pow" => :(SymPy.__POW__), "re" => :real, "im" => :imag, "Abs" => :abs, @@ -134,6 +134,11 @@ function walk_expression(ex; values=Dict(), fns=Dict()) return walk_expression.(Introspection.args(ex), values=values, fns=fns) elseif fn == "Indexed" return Expr(:ref, [walk_expression(a, values=values, fns=fns) for a in Introspection.args(ex)]...) + elseif fn == "Pow" + a, b = Introspection.args(ex) + b == 1//2 && return Expr(:call, :sqrt, walk_expression(a, values=values, fns=fns)) + b == 1//3 && return Expr(:call, :cbrt, walk_expression(a, values=values, fns=fns)) + return Expr(:call, :^, [walk_expression(aᵢ, values=values, fns=fns) for aᵢ in (a,b)]...) elseif haskey(vals_map, fn) return vals_map[fn] end