Skip to content

Commit

Permalink
Showing 5 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/src/Tutorial/manipulation.md
Original file line number Diff line number Diff line change
@@ -951,7 +951,7 @@ result in different output forms. For example

* The printing support is through `show`, but we can use SymPy's:

```jldoctest manipulation
```
julia> uexpr = UnevaluatedExpr(S.One*5/7)*UnevaluatedExpr(S.One*3/4)
5/7⋅3/4
27 changes: 14 additions & 13 deletions docs/src/introduction.md
Original file line number Diff line number Diff line change
@@ -851,9 +851,8 @@ julia> try solve(cos(x) - x) catch err "error" end # wrap command for doctest
For such an equation, a numeric method would be needed, similar to the `Roots` package. For example:

```jldoctest introduction
julia> nsolve(cos(x) - x, 1)
0.7390851332151606416553120876738734040134117589007574649656806357732846548836
julia> nsolve(cos(x) - x, 1) ≈ 0.73908513321516064165
true
```

Though it can't solve everything, the `solve` function can also solve
@@ -867,7 +866,9 @@ julia> @syms a::real, b::real, c::real
julia> p = a*x^2 + b*x + c
2
a⋅x + b⋅x + c
```

```
julia> solve(p, x)
2-element Vector{Sym}:
(-b + sqrt(-4*a*c + b^2))/(2*a)
@@ -894,7 +895,7 @@ julia> solveset(p, x)
If the `x` value is not given, `solveset` will error and `solve` will try to find a
solution over all the free variables:

```jldoctest introduction
```
julia> solve(p)
1-element Vector{Dict{Any, Any}}:
Dict(a => -(b*x + c)/x^2)
@@ -1047,28 +1048,28 @@ julia> solve(x ⩵ 1)
```

Also, consistent with the interface from `Symbolics` the infix tilde, `~`, can be used for `Eq`.


Here is an alternative way of asking a previous question on a pair of linear equations:

```julia
julia> x, y = symbols("x,y", real=true)
julia> @syms x::real y::real
(x, y)

julia> exs = [2x+3y 6, 3x-4y 12] ## Using \Equal[tab]
2-element Vector{Sym}:
2x + 3y = 6
3x - 4y = 12
julia> exs = (2x+3y ~ 6, 3x-4y ~ 12)
(Eq(2*x + 3*y, 6), Eq(3*x - 4*y, 12))

julia> d = solve(exs)
Dict{Any, Any} with 2 entries:
x => 60/17
y => -6/17

```

Here is one other way to express the same

```jldoctest introduction
julia> Eq.( [2x+3y,3x-4y], [6,12]) |> solve == d
julia> Eq.( (2x+3y,3x-4y), (6,12)) |> solve == d
true
```

@@ -2279,15 +2280,15 @@ m⋅──(v(t))

We can "classify" this ODE with the method `classify_ode` function.

```jldoctest introduction
```
julia> sympy.classify_ode(ex)
("separable", "1st_exact", "1st_power_series", "lie_group", "separable_Integral", "1st_exact_Integral")
```

It is linear, but not solvable. Proceeding with `dsolve` gives:

```jldoctest introduction
```
julia> dsolve(ex, v(t)) |> string
"Eq(v(t), -α/tanh(log(exp(k*α*(C1 - 2*t)))/(2*m)))"
2 changes: 2 additions & 0 deletions src/generic.jl
Original file line number Diff line number Diff line change
@@ -35,6 +35,8 @@ Base.mod(x::SymbolicObject, args...)= Mod(x, args...)
#Base.mod2pi
#Base.fldmod

# so we can compare numbers with ≈
Base.rtoldefault(::Type{<:SymbolicObject}) = eps()

function Base.round(x::Sym; kwargs...)
length(free_symbols(x)) > 0 && throw(ArgumentError("can't round a symbolic expression"))
41 changes: 21 additions & 20 deletions src/mathfuns.jl
Original file line number Diff line number Diff line change
@@ -130,28 +130,14 @@ Dict{Any, Any} with 2 entries:
A very nice example using `solve` is a [blog](https://newptcai.github.io/euclidean-plane-geometry-with-julia.html) entry on [Napolean's theorem](https://en.wikipedia.org/wiki/Napoleon%27s_theorem) by Xing Shi Cai.
"""
solve() = ()
solve(V::Vector{T}, args...; kwargs...) where {T <: SymbolicObject} =
sympy.solve(V, args...; kwargs...)


"""
nonlinsolve
Note: if passing variables in use a tuple (e.g., `(x,y)`) and *not* a vector (e.g., `[x,y]`).
"""
nonlinsolve(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} =
sympy.nonlinsolve(V, args...; kwargs...)

linsolve(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} =
sympy.linsolve(V, args...; kwargs...)
linsolve(Ts::Tuple, args...; kwargs...) where {T <: SymbolicObject} =
sympy.linsolve(Ts, args...; kwargs...)

nsolve(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} =
sympy.nsolve(V, args...; kwargs...)
nsolve(Ts::Tuple, args...; kwargs...) where {T <: SymbolicObject} =
sympy.nsolve(Ts, args...; kwargs...)


nonlinsolve()


## dsolve allowing initial condiation to be specified
@@ -216,10 +202,8 @@ julia> @syms x() y() t g
julia> ∂ = Differential(t)
Differential(t)
julia> eqns = [∂(x(t)) ~ y(t), ∂(y(t)) ~ x(t)]
2-element Vector{Sym}:
Eq(Derivative(x(t), t), y(t))
Eq(Derivative(y(t), t), x(t))
julia> eqns = (∂(x(t)) ~ y(t), ∂(y(t)) ~ x(t))
(Eq(Derivative(x(t), t), y(t)), Eq(Derivative(y(t), t), x(t)))
julia> dsolve(eqns)
2-element Vector{Sym}:
@@ -277,7 +261,24 @@ lhs(x::SymbolicObject) = pycall_hasproperty(x, :lhs) ? x.lhs : x

export dsolve, rhs, lhs

## ----

## Add methods for "solve functions"
for meth (:solve, :linsolve, :nonlinsolve, :nsolve, :dsolve)
m = Symbol(meth)
@eval begin
($meth)(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} = sympy.$meth(V, args...; kwargs...)
($meth)(Ts::NTuple{N,T}, args...; kwargs...) where {N, T <: SymbolicObject} =
sympy.$meth(Ts, args...; kwargs...)
($meth)(Ts::Tuple, args...; kwargs...) =
sympy.$meth(Ts, args...; kwargs...)
end
end



## ---- deprecate ----

## used with ics=(u,0,1) style
function _dsolve(eqn::Sym, args...; ics=nothing, kwargs...)

2 changes: 1 addition & 1 deletion test/test-ode.jl
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ using Test
dsolve((u)(x) - a*u(x), u(x), ics=Dict(u(y0)=>y1)) # == Eq(u(x), y1 * exp(a*(x - y0)))
dsolve(x*(u)(x) + x*u(x) + 1, u(x), ics=Dict(u(1) => 1))
𝒂 = 2
dsolve(((u)(x))^2 - 𝒂*u(x), u(x), ics=Dict(u(0) => 0))
dsolve(((u)(x))^2 - 𝒂 * u(x), u(x), ics=Dict(u(0) => 0, (u)(0) => 0))
dsolve(((u))(x) - 𝒂 * u(x), u(x), ics=Dict(u(0)=> 1, (u)(0) => 0))

F, G, K = SymFunction("F, G, K")

0 comments on commit 29c5bfd

Please sign in to comment.