From e6ced1f7bbe3550bc26a8ddc82d0fd6897059814 Mon Sep 17 00:00:00 2001 From: john verzani Date: Sun, 29 Nov 2020 09:17:33 -0500 Subject: [PATCH] Issue sympy stats (#389) * adjust compat entry for RecipesBase; close #386 * add comment about import_from; bump version * version bump --- Project.toml | 4 ++-- docs/src/index.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index eaeca866..51444602 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "SymPy" uuid = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6" -version = "1.0.32" +version = "1.0.33" [deps] @@ -11,7 +11,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" [compat] PyCall = "1.91" -RecipesBase = "0.7, 1.0" +RecipesBase = "0.7, 0.8, 1.0, 1.1" SpecialFunctions = "0.8, 0.9, 0.10, 1.0" julia = "1.0" diff --git a/docs/src/index.md b/docs/src/index.md index 49e5a4f3..e34b9fd6 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -86,3 +86,36 @@ julia> out.doit() x ⋅(log(x) + 1) ``` +## Using other features of SymPy + +By design, along with methods defined for generic functions in `Julia`, only a select number of core SymPy functions are exported; others need to be qualified. Many can be found, as above, from the syntax `sympy.XXX`, where the `XXX` method from the underlying `sympy` module is used. Many more must be imported before being available. + +For example, the [Stats](https://docs.sympy.org/latest/modules/stats.html) module provides methods to support the concept of a random variable used in probability and statistics. The following shows how to import all the methods into a session: + +```jldoctest Stats +julia> SymPy.PyCall.pyimport_conda("sympy.stats", "sympy") +PyObject + +julia> SymPy.import_from(sympy.stats) + +julia> p = 1//2 +1//2 + +julia> @vars x integer=true positive=true +(x,) + +julia> pdf = p * (1-p)^(x-1); + +julia> D = DiscreteRV(x, pdf, set=sympy.S.Naturals) +x + +julia> E(D) +2 + +julia> P(D ≫ 3) +1/8 +``` + +The `import_from` function imports all the functions it can, creating methods specialized on their first argument being symbolic. In this case, `E` and `P` are used above without qualification. `Naturals`, above, is in a different sympy module, and hasn't been imported, so it must be qualified above. The `pyimport_conda` call of `PyCall` will import the module into the specific name, and if necessary install the underlying package. + +The above works well for interactive usage, but would cause problems were it included in package code, as the assignment within `pyimport_conda` occurs at run time. There are necessary workarounds.