Skip to content

Commit

Permalink
add doctest to ci; version bump (JuliaPy#458)
Browse files Browse the repository at this point in the history
* add doctest to ci; version bump

* need pip?
* avoid doctest errors
* GKSwstype
  • Loading branch information
jverzani authored Feb 5, 2022
1 parent 3226ee1 commit 8b25bb9
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 87 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,26 @@ jobs:
- uses: codecov/codecov-action@v1
with:
file: lcov.info
docs:
name: Documentation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: '1'
- run: pip3 install sympy
- run: |
julia --project=docs -e '
using Pkg
Pkg.develop(PackageSpec(path=pwd()))
Pkg.instantiate()'
- run: |
julia --project=docs -e '
using Documenter: doctest
using SymPy
doctest(SymPy)'
- run: julia --project=docs docs/make.jl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
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.3"
version = "1.1.4"

[deps]
CommonEq = "3709ef60-1bee-4518-9f2f-acd86f176c50"
Expand Down
6 changes: 5 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ENV["PLOTS_TEST"] = "true"
ENV["GKSwstype"] = "100"


using Documenter
using SymPy

Expand All @@ -18,7 +22,7 @@ pages = [
"Solvers" => "Tutorial/solvers.md",
"Matrices" => "Tutorial/matrices.md",
"Advanced expression manipulation" => "Tutorial/manipulation.md"
],
],
"Reference/API" => "reference.md"
],
)
Expand Down
17 changes: 8 additions & 9 deletions docs/src/Tutorial/basic_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ julia> expr(x => 0)
```jldoctest basicoperations
julia> expr = x^y
y
x
x
julia> expr = expr(y => x^y)
⎛ y⎞
⎝x ⎠
x
x
julia> expr = expr(y => x^x)
⎛ ⎛ x⎞⎞
⎜ ⎝x ⎠⎟
⎝x ⎠
x
x
```

Expand Down Expand Up @@ -364,7 +364,7 @@ argument to `evalf`. Let's compute the first 100 digits of `\pi`.

##### In `Julia`:

```jldoctest basicoperations
```julia
julia> PI.evalf(100)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
```
Expand Down Expand Up @@ -414,7 +414,7 @@ user's discretion by setting the `chop` flag to True.

```jldoctest basicoperations
julia> _one = cos(Sym(1))^2 + sin(Sym(1))^2
2 2
2 2
cos (1) + sin (1)
julia> (_one - 1).evalf()
Expand Down Expand Up @@ -506,12 +506,12 @@ julia> fn.(a)

!!! note "Technical note"
The `lambdify` function converts a symbolic expression into a `Julia` expression, and then creates a function using `invokelatest` to avoid world age issues.

More performant functions can be produced using the following pattern:

```jldoctest basicoperations
julia> ex = sin(x)^2 + x^2
2 2
2 2
x + sin (x)
julia> body = convert(Expr, ex)
Expand Down Expand Up @@ -587,4 +587,3 @@ julia> fn(0)
!!! note "TODO"

Write an advanced numerics section

10 changes: 4 additions & 6 deletions docs/src/Tutorial/matrices.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ U factor:
julia> prod(diag(out.L)) * prod(diag(out.U))
⎛ 1⎞
x⋅⎜x - ─⎟
⎝ x⎠
⎝ x⎠
```

### RREF
Expand Down Expand Up @@ -1129,7 +1129,7 @@ expensive to calculate.

* note missing `b` is not needed with `Julia`:

```jldoctest matrices
```julia
julia> @syms lambda
(lambda,)

Expand All @@ -1138,13 +1138,12 @@ PurePoly(lambda**4 - 11*lambda**3 + 29*lambda**2 + 35*lambda - 150, lambda, doma

julia> factor(p) |> string
"PurePoly(lambda^4 - 11*lambda^3 + 29*lambda^2 + 35*lambda - 150, lambda, domain='ZZ')"
```


As an aside, we can get prettier output by adjusting how `lambda` should print, as follows:

```
```julia
julia> @syms lambda=>"λ"
(λ,)

Expand Down Expand Up @@ -1210,7 +1209,7 @@ julia> m = Sym[-2*cosh(q/3) exp(-q) 1; exp(q) -2*cosh(q/3) 1; 1 1 -2*cosh(q/3)]
3×3 Matrix{Sym}:
-2*cosh(q/3) exp(-q) 1
exp(q) -2*cosh(q/3) 1
1 1 -2*cosh(q/3)
1 1 -2*cosh(q/3)
julia> m.nullspace()
1-element Vector{Matrix{Sym}}:
Expand Down Expand Up @@ -1335,4 +1334,3 @@ SymPy issue tracker [#sympyissues-fn]_ to get detailed help from the community.
* [#mathematicazero-fn] How mathematica tests zero https://reference.wolfram.com/language/ref/PossibleZeroQ.html
* [#matlabzero-fn] How matlab tests zero https://www.mathworks.com/help/symbolic/mupad_ref/iszero.html
* [#sympyissues-fn] https://github.com/sympy/sympy/issues

9 changes: 2 additions & 7 deletions docs/src/Tutorial/solvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ julia> aug = [A b]
1 1 2 3
julia> linsolve(aug, (x,y,z)) # {(-y - 1, y, 2)};
┌ Warning: `vendor()` is deprecated, use `BLAS.get_config()` and inspect the output instead
│ caller = npyinitialize() at numpy.jl:67
└ @ PyCall ~/.julia/packages/PyCall/L0fLP/src/numpy.jl:67
```

Finally, linear equations are solved in `Julia` with the `\` (backslash) operator:
Expand Down Expand Up @@ -740,10 +738,7 @@ julia> dsolve(D(f)(x) - f(x), f(x), ics = Dict(f(0) => a)) |> string
To solve the simple harmonic equation, where two initial conditions are specified, we combine the tuple for each within another tuple:

```jldoctest solvers
julia> ics = Dict(f(0) => 1, D(f)(0) => 2)
Dict{Sym, Int64} with 2 entries:
f(0) => 1
Subs(Derivative(f(x), x), x, 0) => 2
julia> ics = Dict(f(0) => 1, D(f)(0) => 2);
julia> dsolve(D(D(f))(x) - f(x), f(x), ics=ics) |> string
"Eq(f(x), 3*exp(x)/2 - exp(-x)/2)"
Expand Down
67 changes: 20 additions & 47 deletions docs/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ julia> @syms xs[1:5]
(Sym[xs₁, xs₂, xs₃, xs₄, xs₅],)
julia> ys = [Sym("y$i") for i in 1:5]
julia> 5-element Vector{Sym}:
y₁
y₂
y₃
y₄
y₅
5-element Vector{Sym}:
y₁
y₂
Expand Down Expand Up @@ -236,18 +230,10 @@ This calling style will be equivalent to the last:
```jldoctest introduction
julia> ex(x=>1, y=>pi)
z + 1 + π
```

A straight call is also possble, where the order of the variables is determined by `free_symbols`:

```jldoctest introduction
julia> ex(1, pi)
x + 1 + π
```

This is useful for expressions of a single variable, but being more explicit through the use of paired values would be recommended.
A straight call is also possble, where the order of the variables is determined by `free_symbols`.
This is useful for expressions of a single variable, but being more explicit through the use of paired values is recommended.

## Conversion from symbolic to numeric

Expand Down Expand Up @@ -730,7 +716,7 @@ root. The `roots` function of SymPy does.

The output of calling `roots` will be a dictionary whose keys are the roots and values the multiplicity.

```jldoctest introduction
```julia
julia> roots(p)
Dict{Any, Any} with 7 entries:
-1 => 1
Expand All @@ -750,7 +736,7 @@ julia> p = x^5 - x + 1
5
x - x + 1
julia> roots(p)
julia> sympy.roots(p)
Dict{Any, Any}()
```
Expand Down Expand Up @@ -927,12 +913,10 @@ julia> exs = [2x+3y-6, 3x-4y-12]
2-element Vector{Sym}:
2⋅x + 3⋅y - 6
3⋅x - 4⋅y - 12
```

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


Expand Down Expand Up @@ -970,7 +954,7 @@ unknowns. When that is not the case, one can specify the variables to
solve for as a vector. In this example, we find a quadratic polynomial
that approximates $\cos(x)$ near $0$:

```jldoctest introduction
```julia
julia> a,b,c,h = symbols("a,b,c,h", real=true)
(a, b, c, h)

Expand Down Expand Up @@ -998,7 +982,7 @@ Dict{Any, Any} with 3 entries:
Again, a dictionary is returned. The polynomial itself can be found by
substituting back in for `a`, `b`, and `c`:

```jldoctest introduction
```julia
julia> quad_approx = p.subs(d); string(quad_approx)
"x^2*(-cos(h)/h^2 + cos(2*h)/(2*h^2) + 1/(2*h^2)) + x*(2*cos(h)/h - cos(2*h)/(2*h) - 3/(2*h)) + 1"

Expand All @@ -1010,7 +994,7 @@ Finally for `solve`, we show one way to re-express the polynomial $a_2x^2 + a_1x
as $b_2(x-c)^2 + b_1(x-c) + b_0$ using `solve` (and not, say, an
expansion theorem.)

```jldoctest introduction
```julia
julia> n = 3
3

Expand All @@ -1023,13 +1007,9 @@ julia> @syms as[1:3]
julia> @syms bs[1:3]
(Sym[bs₁, bs₂, bs₃],)

julia> p = sum([as[i+1]*x^i for i in 0:(n-1)])
2
2
julia> p = sum([as[i+1]*x^i for i in 0:(n-1)]);

julia> q = sum([bs[i+1]*(x-c)^i for i in 0:(n-1)])
2
2
julia> q = sum([bs[i+1]*(x-c)^i for i in 0:(n-1)]);

julia> solve(p-q, bs)
Dict{Any, Any} with 3 entries:
Expand Down Expand Up @@ -1069,7 +1049,7 @@ julia> solve(x ⩵ 1)

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

```jldoctest introduction
```julia
julia> x, y = symbols("x,y", real=true)
(x, y)

Expand All @@ -1079,13 +1059,6 @@ julia> exs = [2x+3y ⩵ 6, 3x-4y ⩵ 12] ## Using \Equal[tab]
3x - 4y = 12

julia> d = solve(exs)
/Users/verzani/.julia/conda/3/lib/python3.7/site-packages/sympy/matrices/repmatrix.py:102: SymPyDeprecationWarning:
non-Expr objects in a Matrix has been deprecated since SymPy 1.9. Use
list of lists, TableForm or some other data structure instead. See
https://github.com/sympy/sympy/issues/21497 for more info.
deprecated_since_version="1.9"
Dict{Any, Any} with 2 entries:
x => 60/17
y => -6/17
Expand Down Expand Up @@ -1246,15 +1219,15 @@ julia> limit(ex, x => a)

In a previous example, we defined `quad_approx`:

```jldoctest introduction
```julia
julia> quad_approx |> string
"x^2*(-cos(h)/h^2 + cos(2*h)/(2*h^2) + 1/(2*h^2)) + x*(2*cos(h)/h - cos(2*h)/(2*h) - 3/(2*h)) + 1"

```

The limit as `h` goes to $0$ gives `1 - x^2/2`, as expected:

```jldoctest introduction
```julia
julia> limit(quad_approx, h => 0)
2
x
Expand Down Expand Up @@ -1302,7 +1275,7 @@ f (generic function with 1 method)

A numeric attempt might be done along these lines:

```jldoctest introduction
```julia
julia> hs = [10.0^(-i) for i in 6:16]
11-element Vector{Float64}:
1.0e-6
Expand Down Expand Up @@ -1763,7 +1736,7 @@ julia> integrate(x^2, (x, 0, 1))

Tedious problems, such as those needing multiple integration-by-parts steps can be done easily:

```jldoctest introduction
```julia
julia> integrate(x^5*sin(x), x)
5 4 3 2
- x cos(x) + 5x sin(x) + 20x cos(x) - 60x sin(x) - 120xcos(x) + 120sin(x)
Expand All @@ -1776,7 +1749,7 @@ The SymPy tutorial says:
The tutorial gives the following example:

```jldoctest introduction
```julia
julia> ex = (x^4 + x^2*exp(x) - x^2 - 2*x*exp(x) - 2*x - exp(x))*exp(x)/((x - 1)^2*(x + 1)^2*(exp(x) + 1))
4 2 x 2 x x⎞ x
⎝x + x - x - 2x- 2x - ℯ ⎠
Expand All @@ -1788,7 +1761,7 @@ julia> ex = (x^4 + x^2*exp(x) - x^2 - 2*x*exp(x) - 2*x - exp(x))*exp(x)/((x - 1)

With indefinite integral:

```jldoctest introduction
```julia
julia> integrate(ex, x) |> string
"log(exp(x) + 1) + exp(x)/(x^2 - 1)"

Expand Down Expand Up @@ -2024,7 +1997,7 @@ julia> M = [1 x; x 1]
Construction of symbolic matrices can *also* be done through the `Matrix` constructor, which must be qualified. It is passed a vector or row vectors but any symbolic values *must* be converted into `PyObject`s:

```jldoctest introduction
julia> import PyCall: PyObject
julia> import SymPy.PyCall: PyObject
julia> A = sympy.Matrix([[1,PyObject(x)], [PyObject(y), 2]])
2×2 Matrix{Sym}:
Expand Down
Loading

0 comments on commit 8b25bb9

Please sign in to comment.