Skip to content

Commit fd31083

Browse files
committed
Variable, vars -> Sym, syms
1 parent 70b2909 commit fd31083

File tree

12 files changed

+78
-77
lines changed

12 files changed

+78
-77
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ SymbolicUtils.jl provides various utilities for symbolic computing.
44

55
[![Build Status](https://travis-ci.org/JuliaSymbolics/SymbolicUtils.jl.svg?branch=master)](https://travis-ci.com/github/JuliaSymbolics/SymbolicUtils.jl) [![Coverage Status](https://coveralls.io/repos/github/JuliaSymbolics/SymbolicUtils.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaSymbolics/SymbolicUtils.jl?branch=master)
66

7-
## Variables and expressions
7+
## Symbols and expressions
88

9-
Symbolic variables can be created using the `@vars` macro:
9+
Symbols can be created using the `@syms` macro:
1010

1111
```julia
1212
julia> using SymbolicUtils
1313

14-
julia> @vars a::Integer b c d x::Real y::Number
14+
julia> @syms a::Integer b c d x::Real y::Number
1515
(a, b, c, d, x, y)
1616
```
1717

18-
This macro also defines the Julia variables of the same name and each is set to the its respective symbolic variable object.
18+
This macro also defines the Julia variables of the same name and each is set to the its respective symbolic object.
1919

20-
The associated type `T` in the `@vars a::T` syntax, called `symtype` of the variable, is the type the value of the variable is supposed to be of. These types may determine the rules of symbolic simplification.
20+
The associated type `T` in the `@syms a::T` syntax, called `symtype` of the symbol, is the type the value of the symbol is supposed to be of. These types may determine the rules of symbolic simplification.
2121

22-
Arithmetic and math functions are defined on variables and return `Term` objects which represent function call expressions.
22+
Arithmetic and math functions are defined on symbols and return `Term` objects which represent function call expressions.
2323

24-
Variables can be defined to behave like functions. Both the input and output types for the function can be specified. Any application to that function will only admit either values of those types or symbolic variables of the same `symtype`.
24+
Symbols can be defined to behave like functions. Both the input and output types for the function can be specified. Any application to that function will only admit either values of those types or symbols of the same `symtype`.
2525

2626
```julia
27-
julia> @vars f(x) g(x::Real, y::Real)::Real
27+
julia> @syms f(x) g(x::Real, y::Real)::Real
2828
(f(::Number)::Number, g(::Real, ::Real)::Real)
2929

3030
julia> f(c)
@@ -67,7 +67,7 @@ _Example:_
6767
Simple rule to turn any `sin` into `cos`:
6868

6969
```julia
70-
julia> @vars a b c
70+
julia> @syms a b c
7171
(a, b, c)
7272

7373
julia> r = @rule sin(~x) => cos(~x)
@@ -153,7 +153,7 @@ is equivalent to `f(x, y, z, u, v, w)` and commutative if the order of arguments
153153
SymbolicUtils has a special `@acrule` macro meant for rules on functions which are associate
154154
and commutative such as addition and multiplication of real and complex numbers.
155155
```julia
156-
julia> @vars x y
156+
julia> @syms x y
157157
(x, y)
158158

159159
julia> acr = @acrule((~y)^(~n) * ~y => (~y)^(~n+1))
@@ -169,7 +169,7 @@ Rules are applied to an entire term, they do not see sub-terms
169169
```julia
170170
julia> using SymbolicUtils
171171

172-
julia> @vars x y
172+
julia> @syms x y
173173
(x, y)
174174

175175
julia> r = @rule sin(~x) => cos(~x)
@@ -233,15 +233,15 @@ Called only if `istree(x)` is `true`. Part of the API required
233233
for `simplify` to work. Other required methods are `operation` and `istree`
234234

235235
#### `to_symbolic(x::S)`
236-
Convert your variable type to a `SymbolicUtils.Variable`. Suppose you have
236+
Convert your variable type to a `SymbolicUtils.Sym`. Suppose you have
237237
```julia
238238
struct MySymbol
239239
s::Symbol
240240
end
241241
```
242242
which could represent any type symbolically, then you would define
243243
```julia
244-
SymbolicUtils.to_symbolic(s::MySymbol) = SymbolicUtils.Variable(s.s)
244+
SymbolicUtils.to_symbolic(s::MySymbol) = SymbolicUtils.Sym(s.s)
245245
```
246246

247247
### Optional
@@ -283,12 +283,12 @@ julia> ex = 1 + (:x - 2)
283283
How can we use SymbolicUtils.jl to convert `ex` to `(-)(:x, 1)`? We simply implement `istree`,
284284
`operation`, `arguments` and `to_symbolic` and we'll be off to the races:
285285
```julia
286-
using SymbolicUtils: Variable, istree, operation, arguments, to_symbolic
286+
using SymbolicUtils: Sym, istree, operation, arguments, to_symbolic
287287

288288
SymbolicUtils.istree(ex::Expr) = ex.head == :call
289289
SymbolicUtils.operation(ex::Expr) = ex.args[1]
290290
SymbolicUtils.arguments(ex::Expr) = ex.args[2:end]
291-
SymbolicUtils.to_symbolic(s::Symbol) = Variable(s)
291+
SymbolicUtils.to_symbolic(s::Symbol) = Sym(s)
292292

293293
julia> simplify(ex)
294294
(-1 + x)
@@ -298,7 +298,7 @@ Term{Any}
298298
f: + (function of type typeof(+))
299299
arguments: Array{Any}((2,))
300300
1: Int64 -1
301-
2: Variable{Any}
301+
2: Sym{Any}
302302
name: Symbol x
303303
```
304304
this thing returns a `Term{Any}`, but it's not hard to convert back to `Expr`:
@@ -327,7 +327,7 @@ Term{Real}
327327
f: + (function of type typeof(+))
328328
arguments: Array{Any}((2,))
329329
1: Int64 -1
330-
2: Variable{Real}
330+
2: Sym{Real}
331331
name: Symbol x
332332
```
333333
and now all our analysis is able to figure out that the `Term`s are `Number`s.

src/SymbolicUtils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ else
2828
end
2929

3030

31-
export @vars, term, @fun, showraw
31+
export @syms, term, @fun, showraw
3232
include("types.jl")
3333

3434

src/methods.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ for f in diadic
88
T::Type{<:Number},
99
S::Type{<:Number}) = promote_type(T, S)
1010

11-
for T in [Variable, Term]
12-
for S in [Variable, Term]
11+
for T in [Sym, Term]
12+
for S in [Sym, Term]
1313
@eval (::$(typeof(f)))(a::$T, b::$S) = term($f, a, b)
1414
end
1515
@eval begin

src/rule_dsl.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ _Example:_
5656
Simple rule to turn any `sin` into `cos`:
5757
5858
```julia
59-
julia> @vars a b c
59+
julia> @syms a b c
6060
(a, b, c)
6161
6262
julia> r = @rule sin(~x) => cos(~x)
@@ -176,7 +176,7 @@ Base.show(io::IO, acr::ACRule) = print(io, "ACRule(", acr.rule, ")")
176176

177177
function (acr::ACRule)(term)
178178
r = Rule(acr)
179-
if term isa Variable
179+
if term isa Sym
180180
r(term)
181181
else
182182
f = operation(term)

src/simplify.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _isone(x::Number) = isone(x)
2121
<(a::Number, b::Symbolic) = true
2222

2323
arglength(a) = length(arguments(a))
24-
function <(a::Variable, b::Term)
24+
function <(a::Sym, b::Term)
2525
args = arguments(b)
2626
if length(args) === 2
2727
n1, n2 = !isnumber(args[1]) , !isnumber(args[2])
@@ -54,7 +54,7 @@ function <ₑ(a::Variable, b::Term)
5454
end
5555
end
5656

57-
<(a::Symbolic, b::Variable) = !(b <ₑ a)
57+
<(a::Symbolic, b::Sym) = !(b <ₑ a)
5858

5959
function <(a::Symbol, b::Symbol)
6060
# Enforce the order [+,-,\,/,^,*]
@@ -75,7 +75,7 @@ function <ₑ(a::Symbol, b::Symbol)
7575
end
7676
end
7777

78-
<(a::Variable, b::Variable) = a.name < b.name
78+
<(a::Sym, b::Sym) = a.name < b.name
7979
<(a::T, b::S) where {T, S} = T===S ? isless(a, b) : nameof(T) < nameof(S)
8080

8181
function <(a::Term, b::Term)

src/types.jl

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,56 +92,57 @@ Base.:(==)(a::Symbolic, b::Symbolic) = a === b || isequal(a,b)
9292

9393
#--------------------
9494
#--------------------
95-
#### Variables
95+
#### Syms
9696
#--------------------
9797
"""
98-
Variable{T}(name::Symbol)
98+
Sym{T}(name::Symbol)
9999
100100
A named variable of type `T`. Type `T` can be `FnType{X,Y}` which
101101
means the variable is a function with the type signature X -> Y where
102102
`X` is a tuple type of arguments and `Y` is any type.
103103
"""
104-
struct Variable{T} <: Symbolic{T}
104+
struct Sym{T} <: Symbolic{T}
105105
name::Symbol
106106
end
107107

108-
Variable(x) = Variable{symtype(x)}(x)
108+
const Variable = Sym # old name
109+
Sym(x) = Sym{symtype(x)}(x)
109110

110-
Base.nameof(v::Variable) = v.name
111+
Base.nameof(v::Sym) = v.name
111112

112-
Base.:(==)(a::Variable, b::Variable) = a === b
113+
Base.:(==)(a::Sym, b::Sym) = a === b
113114

114-
Base.:(==)(::Variable, ::Symbolic) = false
115+
Base.:(==)(::Sym, ::Symbolic) = false
115116

116-
Base.:(==)(::Symbolic, ::Variable) = false
117+
Base.:(==)(::Symbolic, ::Sym) = false
117118

118-
Base.isequal(v1::Variable, v2::Variable) = isequal(v1.name, v2.name)
119+
Base.isequal(v1::Sym, v2::Sym) = isequal(v1.name, v2.name)
119120

120-
Base.show(io::IO, v::Variable) = print(io, v.name)
121+
Base.show(io::IO, v::Sym) = print(io, v.name)
121122

122123
#---------------------------
123124
#---------------------------
124125
#### Function-like variables
125126
#---------------------------
126127

127-
# Maybe don't even need a new type, can just use Variable{FnType}
128+
# Maybe don't even need a new type, can just use Sym{FnType}
128129
struct FnType{X<:Tuple,Y} end
129130

130-
(f::Variable{<:FnType})(args...) = term(f, args...)
131+
(f::Sym{<:FnType})(args...) = term(f, args...)
131132

132-
function (f::Variable)(args...)
133-
error("Variable $f of type $F are not callable. " *
134-
"Use @vars $f(var1, var2,...) to create it as a callable. " *
133+
function (f::Sym)(args...)
134+
error("Sym $f of type $F are not callable. " *
135+
"Use @syms $f(var1, var2,...) to create it as a callable. " *
135136
"See ?@fun for more options")
136137
end
137138

138139
"""
139-
`promote_symtype(f::Variable{FnType{X,Y}}, arg_symtypes...)`
140+
`promote_symtype(f::Sym{FnType{X,Y}}, arg_symtypes...)`
140141
141142
The resultant type of applying variable `f` to arugments of symtype `arg_symtypes...`.
142143
if the arguments are of the wrong type then this function will error.
143144
"""
144-
function promote_symtype(f::Variable{FnType{X,Y}}, args...) where {X, Y}
145+
function promote_symtype(f::Sym{FnType{X,Y}}, args...) where {X, Y}
145146
nrequired = fieldcount(X)
146147
ngiven = nfields(args)
147148

@@ -159,39 +160,39 @@ function promote_symtype(f::Variable{FnType{X,Y}}, args...) where {X, Y}
159160
end
160161

161162
"""
162-
@vars <lhs_expr>[::T1] <lhs_expr>[::T2]...
163+
@syms <lhs_expr>[::T1] <lhs_expr>[::T2]...
163164
164165
For instance:
165166
166-
@vars foo::Real bar baz(x, y::Real)::Complex
167+
@syms foo::Real bar baz(x, y::Real)::Complex
167168
168169
Create one or more variables. `<lhs_expr>` can be just a symbol in which case
169170
it will be the name of the variable, or a function call in which case a function-like
170-
variable which has the same name as the function being called. The Variable type, or
171-
in the case of a function-like Variable, the output type of calling the function
171+
variable which has the same name as the function being called. The Sym type, or
172+
in the case of a function-like Sym, the output type of calling the function
172173
can be set using the `::T` syntax.
173174
174175
# Examples:
175176
176-
- `@vars foo bar::Real baz::Int` will create
177+
- `@syms foo bar::Real baz::Int` will create
177178
variable `foo` of symtype `Number` (the default), `bar` of symtype `Real`
178179
and `baz` of symtype `Int`
179-
- `@vars f(x) g(y::Real, x)::Int h(a::Int, f(b))` creates 1-arg `f` 2-arg `g`
180+
- `@syms f(x) g(y::Real, x)::Int h(a::Int, f(b))` creates 1-arg `f` 2-arg `g`
180181
and 2 arg `f`. The second argument to `h` must be a one argument function-like
181182
variable. So, `h(1, g)` will fail and `h(1, f)` will work.
182183
"""
183-
macro vars(xs...)
184+
macro syms(xs...)
184185
defs = map(xs) do x
185186
n, t = _name_type(x)
186-
:($(esc(n)) = Variable{$(esc(t))}($(Expr(:quote, n))))
187+
:($(esc(n)) = Sym{$(esc(t))}($(Expr(:quote, n))))
187188
end
188189

189190
Expr(:block, defs...,
190191
:(tuple($(map(x->esc(_name_type(x).name), xs)...))))
191192
end
192193

193-
function vars_syntax_error()
194-
error("Incorrect @vars syntax. Try `@vars x::Real y::Complex g(a) f(::Real)::Real` for instance.")
194+
function syms_syntax_error()
195+
error("Incorrect @syms syntax. Try `@syms x::Real y::Complex g(a) f(::Real)::Real` for instance.")
195196
end
196197

197198
function _name_type(x)
@@ -212,11 +213,11 @@ function _name_type(x)
212213
elseif x isa Expr && x.head === :call
213214
return _name_type(:($x::Number))
214215
else
215-
vars_syntax_error()
216+
syms_syntax_error()
216217
end
217218
end
218219

219-
function Base.show(io::IO, f::Variable{<:FnType{X,Y}}) where {X,Y}
220+
function Base.show(io::IO, f::Sym{<:FnType{X,Y}}) where {X,Y}
220221
print(io, f.name)
221222
argrepr = join(map(t->"::"*string(t), X.parameters), ", ")
222223
print(io, "(", argrepr, ")")

test/basics.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
using SymbolicUtils: Variable, FnType, Term, symtype
1+
using SymbolicUtils: Sym, FnType, Term, symtype
22
using SymbolicUtils
33
using Test
44

5-
@testset "@vars" begin
5+
@testset "@syms" begin
66
let
7-
@vars a b::Float64 f(::Real) g(p, h(q::Real))::Int
7+
@syms a b::Float64 f(::Real) g(p, h(q::Real))::Int
88

9-
@test a isa Variable{Number}
9+
@test a isa Sym{Number}
1010
@test a.name === :a
1111

12-
@test b isa Variable{Float64}
12+
@test b isa Sym{Float64}
1313
@test b.name === :b
1414

15-
@test f isa Variable{FnType{Tuple{Real}, Number}}
15+
@test f isa Sym{FnType{Tuple{Real}, Number}}
1616
@test f.name === :f
1717

18-
@test g isa Variable{FnType{Tuple{Number, FnType{Tuple{Real}, Number}}, Int}}
18+
@test g isa Sym{FnType{Tuple{Number, FnType{Tuple{Real}, Number}}, Int}}
1919
@test g.name === :g
2020

2121
@test f(b) isa Term
@@ -30,7 +30,7 @@ using Test
3030
end
3131

3232
@testset "methods test" begin
33-
@vars w::Complex z::Complex a::Real b::Real x
33+
@syms w::Complex z::Complex a::Real b::Real x
3434

3535
@test w + z == Term{Complex}(+, [w, z])
3636
@test z + a == Term{Number}(+, [z, a])

test/fuzz.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ using SymbolicUtils: showraw
55

66
const leaf_funcs = [()->100*randn(),
77
()->rand(-100:100),
8-
()->rand(@vars a b c d e f),
9-
()->rand(@vars a b c d e f)]
8+
()->rand(@syms a b c d e f),
9+
()->rand(@syms a b c d e f)]
1010

1111
const fns = vcat(1 .=> SymbolicUtils.monadic,
1212
2 .=> vcat(SymbolicUtils.diadic, fill(+, 5), [-,-], fill(*, 5)),
@@ -16,7 +16,7 @@ const fns = vcat(1 .=> SymbolicUtils.monadic,
1616
function gen_rand_expr(inputs; leaf_prob=0.92, depth=0, min_depth=1, max_depth=5)
1717
if depth > max_depth || (min_depth <= depth && rand() < leaf_prob)
1818
leaf = rand(leaf_funcs)()
19-
if leaf isa SymbolicUtils.Variable
19+
if leaf isa SymbolicUtils.Sym
2020
push!(inputs, leaf)
2121
end
2222
return leaf

0 commit comments

Comments
 (0)