Skip to content

Commit 474bcde

Browse files
authored
add macro to define a variable (#470)
1 parent f98eef0 commit 474bcde

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/common.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export fromroots,
1414
integrate,
1515
derivative,
1616
variable,
17+
@variable,
1718
isintegral,
1819
ismonic
1920

@@ -865,6 +866,26 @@ variable(::Type{P}, var::SymbolLike) where {P <: AbstractPolynomial} = variable(
865866
variable(p::AbstractPolynomial, var = indeterminate(p)) = variable(typeof(p), var)
866867
variable(var::SymbolLike = :x) = variable(Polynomial{Int}, var)
867868

869+
#@variable x
870+
#@variable x::Polynomial
871+
#@variable x::Polynomial{t]
872+
macro variable(x)
873+
q = Expr(:block)
874+
if isa(x, Expr) && x.head == :(::)
875+
x, P = x.args
876+
push!(q.args, Expr(:(=), esc(x),
877+
Expr(:call, :variable, P, Expr(:quote, x))))
878+
else
879+
push!(q.args, Expr(:(=), esc(x),
880+
Expr(:call, :variable, Expr(:quote, x))))
881+
end
882+
push!(q.args, esc(x))
883+
884+
q
885+
end
886+
887+
888+
868889
# basis
869890
# var is a positional argument, not a keyword; can't deprecate so we do `_var; var=_var`
870891
# return the kth basis polynomial for the given polynomial type, e.g. x^k for Polynomial{T}

test/StandardBasis.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ isimmutable(::Type{<:ImmutablePolynomial}) = true
6363
## issue #452
6464
ps = (1, 1.0)
6565
P != FactoredPolynomial && @test eltype(P(ps)) == eltype(promote(ps...))
66+
## issue 464
67+
@variable z
68+
@test z^2 + 2 == Polynomial([2,0,1], :z)
6669
end
6770
end
6871
end

0 commit comments

Comments
 (0)