Skip to content

Commit 92b36b0

Browse files
authored
Sympylite (#258)
* add lite version * edits * get many tests working, modify matrix code * fix matrix, dsolve, tests, examples, travis, appveyor * cleanup * priviledge a few sympy function calls; do not import all
1 parent f95c1c1 commit 92b36b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+15587
-6953
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.7
76
- 1.0
7+
- 1.1
88
- nightly
99
matrix:
1010
allow_failures:
@@ -22,5 +22,5 @@ notifications:
2222
before_install:
2323
#install mpmath to test functionallity
2424
- if [ $MPMATH = "true" ]; then julia -e 'using Pkg; Pkg.add("Conda"); using Conda; Conda.add("mpmath")';fi
25-
after_success:
25+
after_success:
2626
- julia -e 'using Pkg; Pkg.add("Coverage"); cd(Pkg.dir("SymPy")); using Coverage; Coveralls.submit(process_folder())'

README.md

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,68 @@ Windows: [![Build Status](https://ci.appveyor.com/api/projects/status/github/Jul
1010

1111

1212

13-
The `SymPy` package (`http://sympy.org/`) is a Python library for symbolic mathematics.
13+
SymPy (`http://sympy.org/`) is a Python library for symbolic mathematics.
1414

1515
With the excellent `PyCall` package of `julia`, one has access to the
16-
many features of `SymPy` from within a `Julia` session.
16+
many features of the SymPy library from within a `Julia` session.
1717

18-
This `SymPy` package provides a light interface for _some_ of the
19-
features of `SymPy` that makes working with `SymPy` objects a bit
18+
This `SymPy` package provides a light interface for the
19+
features of the SymPy library that makes working with SymPy objects a bit
2020
easier.
2121

2222
The [tutorial](examples/tutorial.md) provides an overview. It is
2323
viewable as an `IJulia` notebook
24-
[here](http://nbviewer.ipython.org/github/jverzani/SymPy.jl/blob/master/examples/tutorial.ipynb).
24+
[here](http://nbviewer.ipython.org/github/JuliaPy/SymPy.jl/blob/master/examples/tutorial.ipynb). In addition, most of the SymPy tutorial has the `julia` counterparts illustrated starting from [index.html](http://htmlpreview.github.io/?https://github.com/JuliaPy/SymPy.jl/blob/master/examples/index.html)
2525

2626
### Installation
2727

28-
To use this package, both `Python` and its `SymPy` library must be
28+
To use this package, both Python and its SymPy library must be
2929
installed on your system. If `PyCall` is installed using `Conda`
3030
(which is the default if no system `python` is found), then the
31-
underlying `SymPy` library will be installed via `Conda` when the
32-
package is first loaded. Otherwise, installing both `Python` and
33-
`SymPy` (which also requires `mpmath`) can be done by other means.
31+
underlying SymPy library will be installed via `Conda` when the
32+
package is first loaded. Otherwise, installing both Python and the
33+
SymPy library (which also requires mpmath) can be done by other means.
3434
In this case, the `Anaconda` distribution is suggested, as it provides a single
35-
installation of `Python` that includes `SymPy` and many other
35+
installation of Python that includes SymPy and many other
3636
scientific libraries that can be profitably accessed within `Julia`
37-
via `PyCall`. (Otherwise, install `Python` then download the `sympy`
37+
via `PyCall`. (Otherwise, install Python then download the SymPy
3838
library from https://github.com/sympy/sympy/releases and install.)
3939

4040
## The `PyCall` interface to `SymPy`
4141

4242
The only point to this package is that using `PyCall` to access
43-
`SymPy` is somewhat cumbersome. The following is how one would define
44-
a symbolic value `x`, take its sine, then evaluate at `pi`, say:
43+
SymPy is somewhat cumbersome. The following is how one would define
44+
a symbolic value `x`, take its sine, then evaluate the symboic
45+
expression for `x` equal `pi`, say:
4546

4647
```
4748
using PyCall
48-
sympy = pyimport("sympy")
49-
x = sympy.Symbol("x")
50-
y = sympy.sin(x)
51-
z = y.subs(x, sympy.pi)
52-
convert(Float64, z)
49+
sympy = pyimport("sympy") #
50+
x = sympy.Symbol("x") # PyObject x
51+
y = sympy.sin(x) # PyObject sin(x)
52+
z = y.subs(x, sympy.pi) # PyObject 0
53+
convert(Float64, z) # 0.0
5354
```
5455

55-
The `Symbol` and `sin` function of `SymPy` are found within the
56-
imported `sympy` object. They may be referenced with `Python`'s dot
57-
notation. Similarly, the `subs` method of the `y` object may be
58-
accessed with Python's dot nottation using PyCall's `getproperty`
59-
overload to call the method. The call above substitutes a value of
60-
`sympy.pi` for `x`. This leaves the object as a `PyObject` storing a
61-
number which can be brought back into `julia` through conversion, in
56+
57+
The `sympy` object imported on the second line provides the access to
58+
much of SymPy's functionality, allowing access to functions
59+
(`sympy.sin`), properties, modules (`sympy`), and classes
60+
(`sympy.Symbol`, `sympy.Pi`). The `Symbol` and `sin` operations are found
61+
within the imported `sympy` module and, as seen, are referenced with
62+
`Python`'s dot call syntax, as implemented in `PyCall` through a
63+
specialized `getproperty` method.
64+
65+
SymPy's functionality is also found through methods bound to
66+
an object of a certain class. The `subs` method of the `y` object is an
67+
example. Such methods are also accessed with Python's dot-call
68+
syntax. The call above substitutes a value of `sympy.pi` for the
69+
symbolic variable `x`. This leaves the object as a `PyObject` storing
70+
a number which can be brought back into `julia` through conversion, in
6271
this case through an explicit `convert` call.
6372

6473

65-
Alternatively, `PyCall` now has a `*` method, so this could be done with
74+
Alternatively, `PyCall` now has a `*` method, so the above could also be done with:
6675

6776
```
6877
x = sympy.Symbol("x")
@@ -71,17 +80,17 @@ z = sympy.sin(y)
7180
convert(Float64, z.subs(x, 1))
7281
```
7382

74-
With `SymPy` this gets replaced by a more `julia`n syntax:
83+
With the `SymPy` package this gets replaced by a more `julia`n syntax:
7584

7685
```
7786
using SymPy
7887
x = symbols("x") # or @vars x, Sym("x"), or Sym(:x)
7988
y = sin(pi*x)
80-
y(1) # Does subs(y, x, 1). Use y(x=>1) to be specific as to which symbol to substitute
89+
y(1) # Does y.subs(x, 1). Use y(x=>1) to be specific as to which symbol to substitute
8190
```
8291

8392
The object `x` we create is of type `Sym`, a simple proxy for the
84-
underlying `PyObject`. We then overload the familiar math functions so
93+
underlying `PyObject`. The package overloads the familiar math functions so
8594
that working with symbolic expressions can use natural `julia`
8695
idioms. The final result here is a symbolic value of `0`, which
8796
prints as `0` and not `PyObject 0`. To convert it into a numeric value
@@ -92,48 +101,63 @@ float conversion, only there is an attempt to preserve the variable type.
92101
`Julia`) is converted to the symbolic `PI`, but in general won't be if
93102
the math constant is coerced to a floating point value before it
94103
encounters a symbolic object. It is better to just use the symbolic
95-
value `PI`, an alias for `sympy.pi` used above. A similar comment
96-
applies for `e`, where `E` should be used.)
104+
value `PI`, an alias for `sympy.pi` used above.)
97105

98-
However, for some tasks the `PyCall` interface is still needed, as
99-
only a portion of the `SymPy` interface is exposed. To call an
100-
underlying SymPy method, the `getproperty` method is overloaded so
101-
that `ex.meth_name(...)` dispatches the method of the object and
102-
`sympy.meth_name(...)` calls a function in the SymPy module.
103106

104-
For example, we could have been more explicit and used:
107+
----
105108

106-
```
107-
y = sympy.sin(pi * x)
108-
y.subs(x, 1)
109-
```
110109

111-
As calling the underlying SymPy function is not difficult, the
112-
interface exposed through overloading `Julia`'s methods attempts to
113-
keep similar functionality to the familiar `Julia` method when there is
114-
a discrepancy between conventions.
110+
SymPy has a mix of function calls (as in `sin(x)`) and method calls
111+
(as in `y.subs(x,1)`). The function calls are from objects in the base
112+
`sympy` module. When the `SymPy` package is loaded, in addition to
113+
specialized methods for many generic `Julia` functions, such as `sin`,
114+
a priviledged set of the function calls in `sympy` are imported as
115+
generic functions narrowed on their first argument being a symbolic
116+
object, as constructed by `Sym` or `symbols`. (Calling
117+
`import_from(sympy)` will import all the function calls.)
118+
119+
The basic usage follows these points:
120+
121+
* generic methods from `Julia` and imported functions in the `sympy`
122+
namespace are called through `fn(object)`
123+
124+
* SymPy methods are called through Python's dot-call syntax:
125+
`object.fn(...)`
115126

116-
## Notes
127+
* Contructors, like `sympy.Symbol`, and other non-function calls from `sympy` are qualified
128+
with `sympy.Constructor(...)`. Such qualified calls are also useful
129+
when the first argument is not symbolic.
117130

118-
Some aspects of `SymPy` require more modern versions of `sympy` to be
119-
installed. For example, the matrix functions rely on features of
120-
`sympy` that are not exposed in the `sympy` installed with Ubuntu LTS
121-
12.04.
122131

123-
In that particular instance, calls such as
132+
So, these three calls are different,
124133

125134
```
126-
x = symbols("x")
127-
a = [x 1; 1 x]
128-
det(a)
135+
sin(1), sin(Sym(1)), sympy.sin(1)
129136
```
130137

131-
Can be replaced with
138+
The first involves no symbolic values. The second and third are
139+
related and return a symbolic value for `sin(1)`. The second
140+
dispatches on the symbolic argument `Sym(1)`, the third has no
141+
dispatch, but refers to a SymPy function from the `sympy` object. Its
142+
argument, `1`, is converted by `PyCall` into a Python object for the
143+
function to process.
144+
145+
In the initial example, slightly rewritten, we could have written:
132146

133147
```
134-
sympy.det(a)
148+
x = symbols("x")
149+
y = sin(pi*x)
150+
y.subs(x, 1)
135151
```
136152

137-
Similarly for `trace`, `eigenvects`, ... . Note these are `sympy`
138-
methods, not `Julia` methods that have been ported. (Hence,
139-
`eigenvects` and not `eigvecs`.)
153+
The first line calls a provided alias for `sympy.symbols` which is
154+
defined to allow a string (or a symbol) as an argument. The second,
155+
dispatches to `sympy.sin`, as `pi*x` is symbolic-- `x` is, and
156+
multiplication promotes to a symbolic value. The third line uses the
157+
dot-call syntax of `PyCall` to call the `subs` method of the symbolic
158+
`y` object.
159+
160+
161+
Not illustrated above, but classes and other objects from SymPy are
162+
not brought in by default, and can be accessed using qualification, as
163+
in `sympy.Function` (used to define symbolic functions).

REQUIRE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
julia 0.7.0
1+
julia 1.0.0
22
PyCall 1.90.0
33
RecipesBase 0.5.0
44
SpecialFunctions 0.3.4
5+
OffsetArrays 0.10.0

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
environment:
22
matrix:
3-
- julia_version: 0.7
43
- julia_version: 1.0
4+
- julia_version: 1.1
55
- julia_version: latest
66

77
platform:

examples/LICENSE-orig.txt

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
Copyright (c) 2006-2018 SymPy Development Team
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
a. Redistributions of source code must retain the above copyright notice,
9+
this list of conditions and the following disclaimer.
10+
b. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
c. Neither the name of SymPy nor the names of its contributors
14+
may be used to endorse or promote products derived from this software
15+
without specific prior written permission.
16+
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28+
DAMAGE.
29+
30+
--------------------------------------------------------------------------------
31+
32+
Patches taken from the Diofant project (https://github.com/diofant/diofant)
33+
are licensed as:
34+
35+
Copyright (c) 2006-2017 SymPy Development Team,
36+
2013-2017 Sergey B Kirpichev
37+
38+
All rights reserved.
39+
40+
Redistribution and use in source and binary forms, with or without
41+
modification, are permitted provided that the following conditions are met:
42+
43+
a. Redistributions of source code must retain the above copyright notice,
44+
this list of conditions and the following disclaimer.
45+
b. Redistributions in binary form must reproduce the above copyright
46+
notice, this list of conditions and the following disclaimer in the
47+
documentation and/or other materials provided with the distribution.
48+
c. Neither the name of Diofant, or SymPy nor the names of its contributors
49+
may be used to endorse or promote products derived from this software
50+
without specific prior written permission.
51+
52+
53+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56+
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
57+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
60+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
63+
DAMAGE.
64+
65+
--------------------------------------------------------------------------------
66+
67+
Submodule taken from the multipledispatch project (https://github.com/mrocklin/multipledispatch)
68+
are licensed as:
69+
70+
Copyright (c) 2014 Matthew Rocklin
71+
72+
All rights reserved.
73+
74+
Redistribution and use in source and binary forms, with or without
75+
modification, are permitted provided that the following conditions are met:
76+
77+
a. Redistributions of source code must retain the above copyright notice,
78+
this list of conditions and the following disclaimer.
79+
b. Redistributions in binary form must reproduce the above copyright
80+
notice, this list of conditions and the following disclaimer in the
81+
documentation and/or other materials provided with the distribution.
82+
c. Neither the name of multipledispatch nor the names of its contributors
83+
may be used to endorse or promote products derived from this software
84+
without specific prior written permission.
85+
86+
87+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90+
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
91+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
93+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
94+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
97+
DAMAGE.

0 commit comments

Comments
 (0)