-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganize, refactor, upgrade to v0.7-beta
modified: .travis.yml modified: LICENSE.md modified: REQUIRE modified: appveyor.yml modified: src/ZChop.jl modified: test/runtests.jl
- Loading branch information
Showing
6 changed files
with
70 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ os: | |
- linux | ||
- osx | ||
julia: | ||
- 0.6 | ||
- nightly | ||
notifications: | ||
email: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
julia 0.6 | ||
Compat | ||
julia 0.7-beta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |