Skip to content

Commit 29c5bfd

Browse files
authored
Docfix (#467)
* adjust solve functions * fix docs * adjust test * work around doctest errors on CI * add rtoldefault for Sym
1 parent 0eda92a commit 29c5bfd

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed

docs/src/Tutorial/manipulation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ result in different output forms. For example
951951

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

954-
```jldoctest manipulation
954+
```
955955
julia> uexpr = UnevaluatedExpr(S.One*5/7)*UnevaluatedExpr(S.One*3/4)
956956
5/7⋅3/4
957957

docs/src/introduction.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,8 @@ julia> try solve(cos(x) - x) catch err "error" end # wrap command for doctest
851851
For such an equation, a numeric method would be needed, similar to the `Roots` package. For example:
852852

853853
```jldoctest introduction
854-
julia> nsolve(cos(x) - x, 1)
855-
0.7390851332151606416553120876738734040134117589007574649656806357732846548836
856-
854+
julia> nsolve(cos(x) - x, 1) ≈ 0.73908513321516064165
855+
true
857856
```
858857

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

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

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

1051+
Also, consistent with the interface from `Symbolics` the infix tilde, `~`, can be used for `Eq`.
1052+
1053+
10501054
Here is an alternative way of asking a previous question on a pair of linear equations:
10511055

10521056
```julia
1053-
julia> x, y = symbols("x,y", real=true)
1057+
julia> @syms x::real y::real
10541058
(x, y)
10551059

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

10611063
julia> d = solve(exs)
10621064
Dict{Any, Any} with 2 entries:
10631065
x => 60/17
10641066
y => -6/17
1065-
10661067
```
10671068

10681069
Here is one other way to express the same
10691070

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

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

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

2282-
```jldoctest introduction
2283+
```
22832284
julia> sympy.classify_ode(ex)
22842285
("separable", "1st_exact", "1st_power_series", "lie_group", "separable_Integral", "1st_exact_Integral")
22852286
22862287
```
22872288

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

2290-
```jldoctest introduction
2291+
```
22912292
julia> dsolve(ex, v(t)) |> string
22922293
"Eq(v(t), -α/tanh(log(exp(k*α*(C1 - 2*t)))/(2*m)))"
22932294

src/generic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Base.mod(x::SymbolicObject, args...)= Mod(x, args...)
3535
#Base.mod2pi
3636
#Base.fldmod
3737

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

3941
function Base.round(x::Sym; kwargs...)
4042
length(free_symbols(x)) > 0 && throw(ArgumentError("can't round a symbolic expression"))

src/mathfuns.jl

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,14 @@ Dict{Any, Any} with 2 entries:
130130
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.
131131
"""
132132
solve() = ()
133-
solve(V::Vector{T}, args...; kwargs...) where {T <: SymbolicObject} =
134-
sympy.solve(V, args...; kwargs...)
133+
135134

136135
"""
137136
nonlinsolve
138137
139138
Note: if passing variables in use a tuple (e.g., `(x,y)`) and *not* a vector (e.g., `[x,y]`).
140139
"""
141-
nonlinsolve(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} =
142-
sympy.nonlinsolve(V, args...; kwargs...)
143-
144-
linsolve(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} =
145-
sympy.linsolve(V, args...; kwargs...)
146-
linsolve(Ts::Tuple, args...; kwargs...) where {T <: SymbolicObject} =
147-
sympy.linsolve(Ts, args...; kwargs...)
148-
149-
nsolve(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} =
150-
sympy.nsolve(V, args...; kwargs...)
151-
nsolve(Ts::Tuple, args...; kwargs...) where {T <: SymbolicObject} =
152-
sympy.nsolve(Ts, args...; kwargs...)
153-
154-
140+
nonlinsolve()
155141

156142

157143
## dsolve allowing initial condiation to be specified
@@ -216,10 +202,8 @@ julia> @syms x() y() t g
216202
julia> ∂ = Differential(t)
217203
Differential(t)
218204
219-
julia> eqns = [∂(x(t)) ~ y(t), ∂(y(t)) ~ x(t)]
220-
2-element Vector{Sym}:
221-
Eq(Derivative(x(t), t), y(t))
222-
Eq(Derivative(y(t), t), x(t))
205+
julia> eqns = (∂(x(t)) ~ y(t), ∂(y(t)) ~ x(t))
206+
(Eq(Derivative(x(t), t), y(t)), Eq(Derivative(y(t), t), x(t)))
223207
224208
julia> dsolve(eqns)
225209
2-element Vector{Sym}:
@@ -277,7 +261,24 @@ lhs(x::SymbolicObject) = pycall_hasproperty(x, :lhs) ? x.lhs : x
277261

278262
export dsolve, rhs, lhs
279263

264+
## ----
265+
266+
## Add methods for "solve functions"
267+
for meth (:solve, :linsolve, :nonlinsolve, :nsolve, :dsolve)
268+
m = Symbol(meth)
269+
@eval begin
270+
($meth)(V::AbstractArray{T,N}, args...; kwargs...) where {T <: SymbolicObject, N} = sympy.$meth(V, args...; kwargs...)
271+
($meth)(Ts::NTuple{N,T}, args...; kwargs...) where {N, T <: SymbolicObject} =
272+
sympy.$meth(Ts, args...; kwargs...)
273+
($meth)(Ts::Tuple, args...; kwargs...) =
274+
sympy.$meth(Ts, args...; kwargs...)
275+
end
276+
end
277+
278+
279+
280280
## ---- deprecate ----
281+
281282
## used with ics=(u,0,1) style
282283
function _dsolve(eqn::Sym, args...; ics=nothing, kwargs...)
283284

test/test-ode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ using Test
4949
dsolve((u)(x) - a*u(x), u(x), ics=Dict(u(y0)=>y1)) # == Eq(u(x), y1 * exp(a*(x - y0)))
5050
dsolve(x*(u)(x) + x*u(x) + 1, u(x), ics=Dict(u(1) => 1))
5151
𝒂 = 2
52-
dsolve(((u)(x))^2 - 𝒂*u(x), u(x), ics=Dict(u(0) => 0))
52+
dsolve(((u)(x))^2 - 𝒂 * u(x), u(x), ics=Dict(u(0) => 0, (u)(0) => 0))
5353
dsolve(((u))(x) - 𝒂 * u(x), u(x), ics=Dict(u(0)=> 1, (u)(0) => 0))
5454

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

0 commit comments

Comments
 (0)