Skip to content

Commit 2a03f8e

Browse files
authored
Merge pull request #37 from JuliaAlgebra/pretty-printing
Change printing of variables with indices
2 parents ede3633 + 0d7d8c2 commit 2a03f8e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
julia 0.7
2-
MultivariatePolynomials 0.2.0
3-
Reexport 0.2.0
2+
MultivariatePolynomials 0.2.1
3+
Reexport 0.2.0

src/var.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export polyvecvar
33

44

55
function polyarrayvar(::Type{PV}, prefix, indices...) where {PV}
6-
map(i -> PV("$(prefix)" * join(i, "_")), Iterators.product(indices...))
6+
map(i -> PV("$(prefix)[$(join(i, ","))]"), Iterators.product(indices...))
77
end
88

99
function buildpolyvar(::Type{PV}, var) where {PV}
@@ -43,19 +43,29 @@ end
4343

4444
struct PolyVar{C} <: AbstractVariable
4545
id::Int
46-
name::AbstractString
46+
name::String
47+
4748
function PolyVar{C}(name::AbstractString) where {C}
4849
# gensym returns something like Symbol("##42")
4950
# we first remove "##" and then parse it into an Int
5051
id = parse(Int, string(gensym())[3:end])
51-
new(id, name)
52+
new(id, convert(String, name))
5253
end
5354
end
5455

5556
Base.hash(x::PolyVar, u::UInt) = hash(x.id, u)
5657
Base.broadcastable(x::PolyVar) = Ref(x)
5758

5859
MP.name(v::PolyVar) = v.name
60+
function MP.name_base_indices(v::PolyVar)
61+
splits = split(v.name, r"[\[,\]]\s*", keepempty=false)
62+
if length(splits) == 1
63+
return v.name, Int[]
64+
else
65+
return splits[1], parse.(Int, splits[2:end])
66+
end
67+
end
68+
5969
MP.monomial(v::PolyVar) = Monomial(v)
6070
_vars(v::PolyVar) = [v]
6171

0 commit comments

Comments
 (0)