Skip to content

Commit

Permalink
Itex (#241)
Browse files Browse the repository at this point in the history
* bump recipes base

* scale vfield

* fix itex (closes #240), other cleanup in display.jl
  • Loading branch information
jverzani committed Sep 19, 2018
1 parent 0b1c6e1 commit 5fb0e7b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.7.0
PyCall 1.7.1
RecipesBase 0.2.3
RecipesBase 0.5.0
SpecialFunctions 0.3.4
12 changes: 5 additions & 7 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Examples
```
@vars x
import Base.Docs.doc
doc(sin(x)) #
doc(sin(x)) #
doc(sympy[:sin]) # explicit module lookup
doc(SymPy.mpmath[:hypercomb]) # explicit module lookup
doc(Poly(x^2,x), :coeffs) # coeffs is an object method of the poly instance
Expand Down Expand Up @@ -43,9 +43,7 @@ latex(s::SymbolicObject, args...; kwargs...) = sympy_meth(:latex, s, args...; k
"create basic printed output"
function jprint(x::SymbolicObject)
out = PyCall.pycall(pybuiltin("str"), String, PyObject(x))
if occursin(r"\*\*", out)
out = replace(out, "**" => "^")
end
out = replace(out, r"\*\*" => "^")
out
end
jprint(x::AbstractArray) = map(jprint, x)
Expand All @@ -58,8 +56,8 @@ Base.show(io::IO, s::Sym) = print(io, jprint(s))

## text/plain
show(io::IO, ::MIME"text/plain", s::SymbolicObject) = print(io, sympy["pretty"](s))
show(io::IO, ::MIME"text/latex", x::Sym) = print(io, latex(x, mode="equation*"))

show(io::IO, ::MIME"text/latex", x::Sym) = print(io, latex(x, mode="equation*", itex=true))
function show(io::IO, ::MIME"text/latex", x::AbstractArray{Sym})
function toeqnarray(x::Vector{Sym})
a = join([latex(x[i]) for i in 1:length(x)], "\\\\")
Expand All @@ -76,7 +74,7 @@ end
## Pretty print dicts
function show(io::IO, ::MIME"text/latex", d::Dict{T,S}) where {T<:Sym, S<:Any}
Latex(x::Sym) = latex(x)
Latex(x) = sprint(Base.showcompact, x)
Latex(x) = sprint(io -> show(IOContext(io, :compact => true), x))

out = "\\begin{equation*}\\begin{cases}"
for (k,v) in d
Expand All @@ -89,4 +87,4 @@ end


## Convert SymPy symbol to Julia expression
convert(::Type{Expr}, x::SymbolicObject) = parse(jprint(x))
convert(::Type{Expr}, x::SymbolicObject) = Meta.parse(jprint(x))
40 changes: 18 additions & 22 deletions src/permutations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ id = Permutation(0:10)
```
Cycle notation can more compactly describe a permuation, it can be passed in as a container of cycles specified through tuples or vectors:
```
p = Permutation([(0,2), (1,3)])
```
Expand All @@ -71,7 +71,7 @@ constructor with values separated by commas and the "call" method for
`i -> j` is created (also the notation `i^p` returns this`) *but* if
more than one argument is given, a cycle is created and multiplied on
the *right* by `p`, so that the above becomes `(0,2) * (1,3)`.
Here are two permutations forming the symmetries of square, naturally represented in the two ways:
```
Expand All @@ -81,11 +81,11 @@ rotate = Permutation([1,2,3,0]) # or Permutation(0,1,2,3) in cycle notation
Operations on permutations include:
* a function call, `p(i)` to recover `j` where `i -> j`, also `i^p`.
* a function call, `p(i)` to recover `j` where `i -> j`, also `i^p`.
* `*` for multiplication. The convention is `(p*q)(i) = q(p(i))` or with the `^` notation: `i^(p*q) = (i^p)^q`.
* `+` for multiplication when `p` and `q` commute, where a check on commuting is performed.
* `inv` for the inverse permutation.
* `/`, where `p/q` is `p * inv(q)`.
* `/`, where `p/q` is `p * inv(q)`.
* `p^n` for powers. We have `inv(p) = p^(-1)` and `p^order(p)` is the identity.
* `p^q` for conjugate, defined by `inv(q) * p * q`.
Expand All @@ -99,7 +99,7 @@ flip^2 # the identity
wheres a rotation is not (as it has order 4)
```
rotate * rotate
rotate * rotate
order(rotate)
```
Expand All @@ -122,10 +122,10 @@ We can see this is the correct mapping `1 -> 3` with
We can check that `flip` and `rotate^2` do commute:
```
id = Permutation(3) # (n) is the identify
id = Permutation(3) # (n) is the identify
commutator(flip, rotate^2) == id
```
```
The conjugate for flip and rotate does the inverse of the flip, then rotates, then flips:
```
Expand All @@ -136,12 +136,12 @@ This is different than `flip^rotate`. As `flip` commutes with `rotate^2` this wi
```
(rotate^2)^flip
```
```
!!! Differences:
There is no support for the `Cycle` class
There is no support for the `Cycle` class
"""
function Permutation(x; kwargs...)
if typeof(x) <: UnitRange
Expand Down Expand Up @@ -218,7 +218,7 @@ for meth in permutations_new_functions
end
eval(Expr(:export, meth))
end



## Base methods of the object
Expand Down Expand Up @@ -303,7 +303,7 @@ end

_unflatten_cyclic_form(m::Matrix) = [m[i,:] for i in 1:size(m)[1]]
_unflatten_cyclic_form(m) = m

function cyclic_form(p::SymPermutation)
m = PyCall.PyObject(p)[:cyclic_form]
_unflatten_cyclic_form(m)
Expand Down Expand Up @@ -331,12 +331,12 @@ Some pre-defined groups are built-in:
* `DihedralGroup`: Group formed by a flip and rotation
* AlternativeGroup: Subgroup of S_n of even elements
* AbelianGroup: Returns the direct product of cyclic groups with the given orders.
Differences:
* use `collect(generate(G))` in place of `list(G.generate())`
"""
PermutationGroup(args...; kwargs...) = SymPy.combinatorics[:perm_groups][:PermutationGroup](args...; kwargs...)
export PermutationGroup
Expand Down Expand Up @@ -366,7 +366,7 @@ SymPy.elements(gp::SymPy.SymPermutationGroup) = [a for a in PyCall.PyObject(gp)[
random_element(gp, ...)
A random group element. Alias to `sympy"random"`.
"""
"""
random_element(gp::SymPy.SymPermutationGroup, args...) = object_meth(gp, :random, args...)

# base_methods
Expand All @@ -387,7 +387,7 @@ end

# new methods
permutation_group_methods = (#:baseswap,
:base,
#:base,
:center,
:centralizer,
:commutator,
Expand Down Expand Up @@ -479,7 +479,7 @@ permutation_group_properties = (:basic_orbits,
:generators,
:max_div,
:transitivity_degree

)
for prop in permutation_group_properties
prop_name = string(prop)
Expand All @@ -494,10 +494,6 @@ for prop in permutation_group_properties
end


if !(VERSION >= v"1.0.0")
import Base: base
end

base(ex::SymPermutationGroup) = PyCall.PyObject(ex)[:base]
export base

Expand Down
20 changes: 13 additions & 7 deletions src/plot_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,32 @@ surface(-5:5, -5:5, 25 - x^2 - y^2)
* a vectorfield plot can (inefficiently but directly) be produced following this example:
```
function vfieldplot(fx, fy; xlim=(-5,5), ylim=(-5,5), n=7)
function vfieldplot(fx, fy; xlim=(-5,5), ylim=(-5,5), n=8)
xs = range(xlim[1], stop=xlim[2], length=n)
ys = range(ylim[1], stop=ylim[2], length=n)
ys = range(ylim[1], stop=ylim[2], length=n)
us = vec([x for x in xs, y in ys])
vs = vec([y for x in xs, y in ys])
fxs = vec([fx(x,y) for x in xs, y in ys])
fys = vec([fy(x,y) for x in xs, y in ys])
quiver(us, vs, quiver=(fxs, fys))
mxs = maximum(abs.(filter(!isinf, filter(!isnan, fxs))))
mys = maximum(abs.(filter(!isinf, filter(!isnan, fys))))
d = 1/2 * max((xlim[2]-xlim[1])/mxs, (ylim[2]-ylim[1])/mys) / n
quiver(us, vs, quiver=(fxs.*d, fys.*d))
end
fx = (x + y) / sqrt(x^2 + y^2)
fy = (x - y) / sqrt(x^2 + y^2)
vfieldplot(fx, fy)
```
```
* To plot two or more functions at once, the style `plot([ex1, ex2], a, b)` does not work. Rather, use
`plot(ex1, a, b); plot!(ex2)`, as in:
```
@vars x
plot(sin(x), 0, 2pi)
Expand Down Expand Up @@ -185,15 +191,15 @@ export VectorField

xlims = get(plotattributes,:xlims, (-5,5))
ylims = get(plotattributes, :ylims, (-5,5))

xs = repeat(range(xlims[1], stop=xlims[2], length=n), inner=(n,))
ys = repeat(range(ylims[1], stop=ylims[2], length=n), outer=(n,))

us, vs = broadcast(F.fx, xs, ys), broadcast(F.fy, xs, ys)

delta = min((xlims[2]-xlims[1])/n, (ylims[2]-ylims[1])/n)
m = maximum([norm([u,v]) for (u,v) in zip(us, vs)])

lambda = delta / m

x := xs
Expand Down

0 comments on commit 5fb0e7b

Please sign in to comment.