Skip to content

Commit

Permalink
reorganize, refactor, upgrade to v0.7-beta
Browse files Browse the repository at this point in the history
	modified:   .travis.yml
	modified:   LICENSE.md
	modified:   REQUIRE
	modified:   appveyor.yml
	modified:   src/ZChop.jl
	modified:   test/runtests.jl
  • Loading branch information
jlapeyre committed Jul 3, 2018
1 parent c218dbd commit 7d25559
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 55 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ os:
- linux
- osx
julia:
- 0.6
- nightly
notifications:
email: false
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The ZChop.jl package is licensed under the MIT "Expat" License:

> Copyright (c) 2014: John Lapeyre.
> Copyright (c) 2014-2017: John Lapeyre.
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
julia 0.6
Compat
julia 0.7-beta
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
63 changes: 43 additions & 20 deletions src/ZChop.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
# Faster to not load precompiled
# __precompile__()

"""
The `ZChop` module provides the functions `zchop` and `zchop!`, which replace
numbers that are close to zero with zero. These functions act recursivley on
collections.
"""
module ZChop

export zchop, zchop!

const zeps = 1e-14

zchop(x::T, eps=zeps) where {T<:Real} =
abs(x) > convert(T,eps) ? x : zero(T)

zchop(x::T, eps=zeps) where {T<:Integer} =
abs(x) > eps ? x : zero(T)
"""
zeps::Float64
zchop(x::T, eps=zeps) where {T<:Complex} =
complex(zchop(real(x),eps),zchop(imag(x),eps))

zchop(a::T, eps=zeps) where {T<:AbstractArray} =
(b = similar(a); @inbounds for i in 1:length(a) b[i] = zchop(a[i],eps) end ; b)

zchop!(a::T, eps=zeps) where {T<:AbstractArray} =
(@inbounds for i in 1:length(a) a[i] = zchop(a[i],eps) end ; a)
The default lower threshold for a number to be replaced by zero.
"""
const zeps = 1e-14

zchop(x::T,eps=zeps) where {T<:Union{AbstractString,Char}} = x
zchop(x::T,eps=zeps) where {T<:Irrational} = zchop(float(x),eps)
zchop(x::Expr,eps=zeps) = Expr(x.head,zchop(x.args)...)
zchop(x,eps) = applicable(start,x) ? map((x)->zchop(x,eps),x) : x
zchop(x) = applicable(start,x) ? map(zchop,x) : x
"""
zchop(x::T, eps::Real = zeps)
Replace `x` by zero if `abs(x) < eps`. `zchop` acts recursively on
mappable objects. Objects that cannot be sensibly compared to a real
number are passed unaltered.
"""
zchop(x::T, eps::Real = zeps) where {T<:Real} = abs(x) > eps ? x : zero(T)
zchop(x::T, eps::Real = zeps) where {T<:Complex} =
complex(zchop(real(x), eps), zchop(imag(x), eps))
# Following is not type stable
zchop(x::Irrational, eps::Real = zeps) = zchop(float(x), eps)
# Do not iterate over strings
zchop(x::Union{AbstractString,AbstractChar}, eps::Real = zeps) = x

"""
zchop!(x::T, eps::Real = zeps)
Perform `zchop` in place.
"""
function zchop!(a::AbstractArray, eps::Real = zeps)
@inbounds for i in firstindex(a):lastindex(a)
a[i] = zchop(a[i], eps)
end
return a
end

zchop(a::AbstractArray, eps::Real = zeps) = zchop!(deepcopy(a), eps)
zchop(x::Expr, eps::Real = zeps) = Expr(x.head, zchop(x.args)...)

zchop(x::T, eps::Real) where {T} = Base.isiterable(T) ? map((x)->zchop(x, eps), x) : x
zchop(x::T) where {T} = Base.isiterable(T) ? map(zchop, x) : x

end # module ZChop
54 changes: 25 additions & 29 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
using Compat

using ZChop
using Compat.Test

const mystdout = isdefined(Main, :stdout) ? stdout : STDOUT

z1 = complex(1,1e-15)
zz1 = complex(1,0.0)
z2 = complex(1e-15,1)
zz2 = complex(0.0,1)
za = [z1,z2]
zza = [zz1,zz2]
using Test

@test zchop(1.0) == 1.0
@test zchop(1e-15) == 0.0

z1 = complex(1, 1e-15)
zz1 = complex(1, 0.0)
@test zchop(z1) == zz1

z2 = complex(1e-15, 1)
zz2 = complex(0.0, 1)
@test zchop(z2) == zz2

za = [z1, z2]
zza = [zz1, zz2]
@test zchop(za) == zza
t = (za,za)
@test zchop( t ) == (zza,zza)
t = (za, za)
@test zchop(t) == (zza, zza)
@test zchop( [za za] ) == [zza zza]
@test zchop(BigFloat[1e-15,1]) == BigFloat[0,1]

@test zchop(BigFloat[1e-15, 1]) == BigFloat[0, 1]
if Int != Int32 @test zchop(1//10^15) == 0//1 end
@test zchop([1e-16,"cat"]) == [ 0.0, "cat"]
@test zchop([1e-16, "cat"]) == [ 0.0, "cat"]
r = r"cat"
@test zchop(r) == r
@test zchop(1) == 1
@test zchop(1,3) == 0
@test zchop(1, 3) == 0
@test zchop(Int) == Int
@test zchop([true,false]) == [true,false]
@test zchop([true, false]) == [true, false]
@test zchop('x') == 'x'
@test zchop(mystdout) == mystdout
@test zchop(stdout) == stdout
@test zchop(Base.pi) == float(Base.pi)
@test zchop(Base.pi,4) == 0.0
@test zchop(Base.pi, 4) == 0.0
@test zchop(BigInt(1)) == BigInt(1)
@test zchop(BigInt(1),3) == BigInt(0)
m = [[1.0,2.0,1e-16] [3.0,4.0,5.0]]
@test zchop(m) == [[1.0,2.0,0.0] [3.0,4.0,5.0]]
@test zchop(BigInt(1), 3) == BigInt(0)
m = [[1.0, 2.0, 1e-16] [3.0, 4.0, 5.0]]
@test zchop(m) == [[1.0, 2.0, 0.0] [3.0, 4.0, 5.0]]
@test zchop(:a) == :a
@test zchop(:(1+1)) == :(1+1)
@test zchop(:(1+1e-16)) == :(1+0.0)
@test zchop(:( "cat" + [1e-16,2.0] )) == :( "cat" + [0.0,2.0] )
@test zchop(:( "cat" + [1e-16, 2.0] )) == :( "cat" + [0.0, 2.0] )

let b = collect(1:100) * 1e-15, c = zchop(b)
let b = collect(1:5) * 1e-15, c = zchop(b)
zchop!(b)
@test c == b
end

# Tuples
@test zchop((1e-15,1,1)) == (0.0,1,1)

@test zchop((1e-15, 1, 1)) == (0.0, 1, 1)

@test_throws MethodError zchop()

0 comments on commit 7d25559

Please sign in to comment.