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

Issue 504b #513

Merged
merged 9 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/Tutorial/basic_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}:
Expand Down
29 changes: 12 additions & 17 deletions docs/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,16 +847,11 @@ julia> factor(expand(f*g)) |> println
Symbolic powers also can be used:

```jldoctest introduction
julia> @syms n
(n,)

julia> gcd(x^n - x^(2*n), x^n)
n
x
julia> @syms x::real, n::integer
(x, n)

julia> gcd(x^(n + 4), x^(n + 1) + 3*x^n)
n
x
julia> gcd(x^n - x^(2*n), x^n) |> println
x^n

julia> sympy.resultant(3*x^4 + 3*x^3 + x^2 - x - 2, x^3 - 3*x^2 + x + 5)
0
Expand Down Expand Up @@ -931,7 +926,7 @@ julia> factor(f, modulus=5) |> println

The expression `x^4 - 3x^2 + 1` is stored internally as other expressions are, using the expression tree to build up from the atoms. However, for polynomials, more efficient and advantageous representations are possible. The dense polynomial representation is possible by storing just the coefficients relative to a known basis. For example:

```jldoctest introduction
```julia
julia> f = x^4 - 2x^2 + 1
4 2
x - 2⋅x + 1
Expand Down Expand Up @@ -1713,7 +1708,7 @@ julia> [hs ys]
With a values appearing to approach $0$. However, in fact these values will ultimately head off to $\infty$:

```jldoctest introduction
julia> limit(j(x), x, 0, dir="+")
julia> limit(j(x), x => 0, dir="+")

```
Expand All @@ -1724,14 +1719,14 @@ julia> limit(j(x), x, 0, dir="+")
One *could* use limits to implement the definition of a derivative:

```jldoctest introduction
julia> @syms x, h
julia> @syms x::real, h::real
(x, h)

julia> j(x) = exp(x) * sin(x)
julia> j(x) = x * exp(x)
j (generic function with 1 method)

julia> limit((j(x+h) - j(x)) / h, h, 0) |> println
(sin(x) + cos(x))*exp(x)
julia> limit((j(x+h) - j(x)) / h, h => 0) |> println
x*exp(x) + exp(x)

```

Expand All @@ -1741,8 +1736,8 @@ The same derivative computed above by a limit could be found with:

```jldoctest introduction
julia> diff(j(x), x)
x x
ℯ ⋅sin(x) + ℯ ⋅cos(x)
x x
x⋅ℯ + ℯ

```

Expand Down
11 changes: 8 additions & 3 deletions src/lambdify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down