Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add some unicode function synonyms and infix operators #6582

Merged
merged 20 commits into from
May 5, 2014
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0625671
Add ⊗ as synonym for kron
jiahao Apr 19, 2014
2a26899
Add √ as synonym for sqrt
jiahao Apr 19, 2014
fae924a
Add obelus (÷) as synonym for div
jiahao Apr 19, 2014
67ce8b5
Add ∅ (empty set) as synonym for Set{Any}({})
jiahao Apr 19, 2014
01d98fe
Add ∈ as synonym for `in` and ∋ for containment
jiahao Apr 19, 2014
edf4e3d
Add ⋅ as synonym for `dot`
jiahao Apr 19, 2014
7369326
Add ⊆ as synonym for `issubset`
jiahao Apr 19, 2014
ad12241
More set relations ⊈, ⊂, ⊄, ⊊.
jiahao Apr 19, 2014
e81e2bb
Add ∩ and ∪ as synonyms for `intersect` and `union` resp.
jiahao Apr 19, 2014
73f65ed
Add △ as synonym for `symdiff`
jiahao Apr 19, 2014
6036e78
Add negated element containments ∉, ∌
jiahao Apr 19, 2014
672ca6e
More comparsion operators ≠ (!=), ≡ (===), ≢ (!==), ≤ (<=), ≥ (>=), .…
jiahao Apr 19, 2014
915ba49
Add double solidus (⫽) as synonym for rational division (//)
jiahao Apr 19, 2014
efcc36b
Adds ∣ (divides) and its negation ∤
jiahao Apr 19, 2014
971756a
Add × (cross product) as synonym for `cross`
jiahao Apr 19, 2014
3cd38a5
Remove ∅ (empty set) and undefines ⊂ and ⊄
jiahao Apr 19, 2014
4a98190
Changed operator precedence for ∩ and ∪. Undefine △.
jiahao Apr 22, 2014
a243cc6
Add ∨ and ∧, and circled and boxed arithmetic operators
jiahao Apr 22, 2014
e4fbbef
Merge branch 'master' of github.com:JuliaLang/julia into uniops-552
JeffBezanson May 3, 2014
970938e
update set of unicode operators and fix exports
JeffBezanson May 3, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
@@ -1337,6 +1337,7 @@ end
symdiff(a) = a
symdiff(a, b) = union(setdiff(a,b), setdiff(b,a))
symdiff(a, b, rest...) = symdiff(a, symdiff(b, rest...))
const △ = symdiff
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit skeptical about this one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this is the Laplacian operator

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the meaning of a unicode operator has to be utterly obvious and agreed-on to have a definition in Base.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with that.


_cumsum_type{T<:Number}(v::AbstractArray{T}) = typeof(+zero(T))
_cumsum_type(v) = typeof(v[1]+v[1])
28 changes: 28 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
@@ -187,34 +187,43 @@ export
# Operators
!,
!=,
≠,
!==,
≡,
≢,
$,
%,
&,
*,
+,
-,
.!=,
.≠,
.+,
.-,
.*,
./,
.<,
.<=,
.≤,
.==,
.>,
.>=,
.≥,
.\,
.^,
/,
//,
⫽,
<,
<:,
<<,
<=,
≤,
==,
>,
>=,
≥,
>>,
#.>>,
#.<<,
@@ -224,6 +233,10 @@ export
|,
~,
:,
⊗,
÷,
∣,
∤,
A_ldiv_B!,
A_ldiv_Bc,
A_ldiv_Bt,
@@ -439,6 +452,7 @@ export
unsigned,
widemul,
zero,
√,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for cbrt?

for angle?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to parse as a prefix operator.


# specfun
airy,
@@ -657,6 +671,8 @@ export
triu!,
triu,
vecnorm,
⋅,
×,

# sparse
etree,
@@ -743,6 +759,18 @@ export
union,
unique,
values,
∈,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for all?

∉,
∋,
∌,
⊆,
⊈,
⊂,
⊄,
⊊,
∩,
∪,
△,

# strings and text output
ascii,
2 changes: 2 additions & 0 deletions base/linalg.jl
Original file line number Diff line number Diff line change
@@ -115,6 +115,8 @@ export
tril!,
triu!,
vecnorm,
⋅,
×,

# Operators
\,
1 change: 1 addition & 0 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ end
scale!(s::Number, X::AbstractArray) = scale!(X, s)

cross(a::AbstractVector, b::AbstractVector) = [a[2]*b[3]-a[3]*b[2], a[3]*b[1]-a[1]*b[3], a[1]*b[2]-a[2]*b[1]]
const × = cross

triu(M::AbstractMatrix) = triu(M,0)
tril(M::AbstractMatrix) = tril(M,0)
1 change: 1 addition & 0 deletions base/linalg/matmul.jl
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ function dot(x::AbstractVector, y::AbstractVector)
s
end
dot(x::Number, y::Number) = conj(x) * y
const ⋅ = dot
Ac_mul_B(x::Vector, y::Vector) = [dot(x, y)]
At_mul_B{T<:Real}(x::Vector{T}, y::Vector{T}) = [dot(x, y)]
At_mul_B{T<:BlasComplex}(x::Vector{T}, y::Vector{T}) = [BLAS.dotu(x, y)]
3 changes: 2 additions & 1 deletion base/math.jl
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ export sin, cos, tan, sinh, cosh, tanh, asin, acos, atan,
besselj0, besselj1, besselj, bessely0, bessely1, bessely,
hankelh1, hankelh2, besseli, besselk, besselh,
beta, lbeta, eta, zeta, polygamma, invdigamma, digamma, trigamma,
erfinv, erfcinv
erfinv, erfcinv, √

import Base: log, exp, sin, cos, tan, sinh, cosh, tanh, asin,
acos, atan, asinh, acosh, atanh, sqrt, log2, log10,
@@ -279,6 +279,7 @@ sqrt(x::Float64) = box(Float64,sqrt_llvm(unbox(Float64,x)))
sqrt(x::Float32) = box(Float32,sqrt_llvm(unbox(Float32,x)))
sqrt(x::Real) = sqrt(float(x))
@vectorize_1arg Number sqrt
const √=sqrt

for f in (:ceil, :trunc, :significand) # :rint, :nearbyint
@eval begin
15 changes: 14 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
@@ -13,14 +13,19 @@ isequal(T::Type, S::Type) = typeseq(T, S)
isequal(x,y) = is(x,y)
==(x,y) = isequal(x,y)
!=(x,y) = !(x==y)
const ≠ = !=
const ≡ = is
!==(x,y) = !is(x,y)

const ≢ = !==
< (x,y) = isless(x,y)
> (x,y) = y < x
<=(x,y) = !(y < x)
const ≤ = <=
>=(x,y) = (y <= x)
const ≥ = >=
.> (x,y) = y.<x
.>=(x,y) = y.<=x
const .≥ = .>=

# this definition allows Number types to implement < instead of isless,
# which is more idiomatic:
@@ -90,6 +95,8 @@ end
.!=(x::Number,y::Number) = x!=y
.< (x::Real,y::Real) = x<y
.<=(x::Real,y::Real) = x<=y
const .≤ = .<=
const .≠ = .!=

# core << >> and >>> takes Int32 as second arg
<<(x,y::Integer) = x << convert(Int32,y)
@@ -110,6 +117,10 @@ fld{T<:Real}(x::T, y::T) = convert(T,round((x-mod(x,y))/y))
# operator alias
const % = rem
.%(x::Real, y::Real) = x%y
const ⊗ = kron
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you said kron was not such a great idea?

const ÷ = div
∣(x::Real, y::Real) = y%x==0 #x∣y = x divides y
∤(x::Real, y::Real) = !∣(x,y)

# mod returns in [0,y) whereas mod1 returns in (0,y]
mod1{T<:Real}(x::T, y::T) = y-mod(y-x,y)
@@ -396,6 +407,8 @@ export
|>,
<|,
~,
⊗,
÷,
colon,
hcat,
vcat,
1 change: 1 addition & 0 deletions base/rational.jl
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ function //(x::Complex, y::Complex)
yy = real(y*y')
complex(real(xy)//yy, imag(xy)//yy)
end
const ⫽ = //
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit marginal too since I'm not sure this is a particularly standard notation.


function show(io::IO, x::Rational)
if isinf(x)
4 changes: 4 additions & 0 deletions base/reduce.jl
Original file line number Diff line number Diff line change
@@ -121,6 +121,10 @@ function in(x, itr)
end
return false
end
const ∈ = in
∉(x, itr)=!∈(x, itr)
∋(itr, x)= ∈(x, itr)
∌(itr, x)=!∋(itr, x)

function contains(itr, x)
depwarn("contains(collection, item) is deprecated, use in(item, collection) instead", :contains)
5 changes: 5 additions & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ function union(s::Set, sets::Set...)
end
return u
end
const ∪ = union

intersect(s::Set) = copy(s)
function intersect(s::Set, sets::Set...)
@@ -64,6 +65,7 @@ function intersect(s::Set, sets::Set...)
end
return i
end
const ∩ = intersect

function setdiff(a::Set, b::Set)
d = copy(a)
@@ -85,6 +87,9 @@ function issubset(l, r)
end
return true
end
const ⊆ = issubset
⊊(l::Set, r::Set) = ⊆(l, r) && l!=r
⊈(l::Set, r::Set) = !⊆(l, r)

function unique(C)
out = Array(eltype(C),0)
8 changes: 4 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
@@ -7,13 +7,13 @@
; the way the lexer works, every prefix of an operator must also
; be an operator.
(-- -->)
(> < >= <= == === != !== |.>| |.<| |.>=| |.<=| |.==| |.!=| |.=| |.!| |<:| |>:|)
(> < >= <= == === != !== |.>| |.<| |.>=| |.≥| |.<=| |.≤| |.==| |.!=| |.≠| |.=| |.!| |<:| |>:| ∈ ∉ ∋ ∌ ⊆ ⊈ ⊂ ⊄ ⊊)
(|\|>| |<\||)
(: |..|)
(+ - |.+| |.-| |\|| $)
(+ - |.+| |.-| |\|| $ ∩ ∪ △)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't ∩ have the precedence of & and have the precedence of |?

(<< >> >>> |.<<| |.>>| |.>>>|)
(* / |./| % |.%| & |.*| |\\| |.\\|)
(// .//)
(* / |./| ÷ ∣ ∤ % ⋅ × |.%| & |.*| |\\| |.\\|)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why stop here? Aren't there a lot more Unicode symbols that could potentially be infix operators? e.g. , , , , , , , , , ?

We might not provide any methods for these in Base, but shouldn't they be available for the user?

(// .//)
(^ |.^|)
(|::|)
(|.|)))