Skip to content

Commit

Permalink
Add _isthinzero as alias of iszero
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenet committed Dec 12, 2023
1 parent 5f568d8 commit 933817b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
65 changes: 31 additions & 34 deletions src/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,41 +265,38 @@ function fixorder(a::HomogeneousPolynomial, b::HomogeneousPolynomial)
end


# Finds the first non zero entry; extended to Taylor1
function Base.findfirst(a::Taylor1{T}) where {T<:Number}
first = findfirst(x->!iszero(x), a.coeffs)
isnothing(first) && return -1
return first-1
end
# Finds the last non-zero entry; extended to Taylor1
function Base.findlast(a::Taylor1{T}) where {T<:Number}
last = findlast(x->!iszero(x), a.coeffs)
isnothing(last) && return -1
return last-1
end
## _isthinzero
"""
_isthinzero(x)
# Finds the first non zero entry; extended to HomogeneousPolynomial
function Base.findfirst(a::HomogeneousPolynomial{T}) where {T<:Number}
first = findfirst(x->!iszero(x), a.coeffs)
isa(first, Nothing) && return -1
return first
end
function Base.findfirst(a::TaylorN{T}) where {T<:Number}
first = findfirst(x->!iszero(x), a.coeffs)
isa(first, Nothing) && return -1
return first-1
end
# Finds the last non-zero entry; extended to HomogeneousPolynomial
function Base.findlast(a::HomogeneousPolynomial{T}) where {T<:Number}
last = findlast(x->!iszero(x), a.coeffs)
isa(last, Nothing) && return -1
return last
end
# Finds the last non-zero entry; extended to TaylorN
function Base.findlast(a::TaylorN{T}) where {T<:Number}
last = findlast(x->!iszero(x), a.coeffs)
isa(last, Nothing) && return -1
return last-1
Generic wrapper to function `iszero`, which allows using the correct
function for `Interval`s
"""
_isthinzero(x) = iszero(x)


## findfirst, findlast
for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
# Finds the first non zero entry
@eval function Base.findfirst(a::$T{T}) where {T<:Number}
first = findfirst(x->!_isthinzero(x), a.coeffs)
isnothing(first) && return -1
if $T == HomogeneousPolynomial
return first
else
return first-1
end
end
# Finds the last non-zero entry
@eval function Base.findlast(a::$T{T}) where {T<:Number}
last = findlast(x->!_isthinzero(x), a.coeffs)
isnothing(last) && return -1
if $T == HomogeneousPolynomial
return last
else
return last-1
end
end
end


Expand Down
20 changes: 10 additions & 10 deletions src/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function pretty_print(a::Taylor1)
bigO = bigOnotation[end] ?
string("+ 𝒪(", var, superscriptify(a.order+1), ")") :
string("")
# iszero(a) && return string(space, z, space, bigO)
TS._isthinzero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
for i in eachindex(a)
Expand All @@ -50,14 +50,14 @@ function pretty_print(a::Taylor1{T}) where {T<:NumberNotSeries}
bigO = bigOnotation[end] ?
string("+ 𝒪(", var, superscriptify(a.order+1), ")") :
string("")
iszero(a) && return string(space, z, space, bigO)
TS._isthinzero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
for i in eachindex(a)
monom::String = i==0 ? string("") : i==1 ? string(" ", var) :
string(" ", var, superscriptify(i))
@inbounds c = a[i]
iszero(c) && continue
TS._isthinzero(c) && continue
cadena = numbr2str(c, ifirst)
strout = string(strout, cadena, monom, space)
ifirst = false
Expand All @@ -73,14 +73,14 @@ function pretty_print(a::Taylor1{T} where {T <: AbstractSeries{S}}) where {S<:Nu
bigO = bigOnotation[end] ?
string("+ 𝒪(", var, superscriptify(a.order+1), ")") :
string("")
iszero(a) && return string(space, z, space, bigO)
TS._isthinzero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
for i in eachindex(a)
monom::String = i==0 ? string("") : i==1 ? string(" ", var) :
string(" ", var, superscriptify(i))
@inbounds c = a[i]
iszero(c) && continue
TS._isthinzero(c) && continue
cadena = numbr2str(c, ifirst)
ccad::String = i==0 ? cadena : ifirst ? string("(", cadena, ")") :
string(cadena[1:2], "(", cadena[3:end], ")")
Expand All @@ -94,7 +94,7 @@ end
function pretty_print(a::HomogeneousPolynomial{T}) where {T<:Number}
z = zero(a[1])
space = string(" ")
iszero(a) && return string(space, z)
TS._isthinzero(a) && return string(space, z)
strout::String = homogPol2str(a)
strout
end
Expand All @@ -105,12 +105,12 @@ function pretty_print(a::TaylorN{T}) where {T<:Number}
bigO::String = bigOnotation[end] ?
string(" + 𝒪(‖x‖", superscriptify(a.order+1), ")") :
string("")
iszero(a) && return string(space, z, space, bigO)
TS._isthinzero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
for ord in eachindex(a)
pol = a[ord]
iszero(pol) && continue
TS._isthinzero(pol) && continue
cadena::String = homogPol2str( pol )
strsgn = (ifirst || ord == 0 || cadena[2] == '-') ?
string("") : string(" +")
Expand Down Expand Up @@ -141,7 +141,7 @@ function homogPol2str(a::HomogeneousPolynomial{T}) where {T<:Number}
end
end
@inbounds c = a[pos]
iszero(c) && continue
TS._isthinzero(c) && continue
cadena = numbr2str(c, ifirst)
strout = string(strout, cadena, monom, space)
ifirst = false
Expand Down Expand Up @@ -170,7 +170,7 @@ function homogPol2str(a::HomogeneousPolynomial{Taylor1{T}}) where {T<:Number}
end
end
@inbounds c = a[pos]
iszero(c) && continue
TS._isthinzero(c) && continue
cadena = numbr2str(c, ifirst)
ccad::String = (pos==1 || ifirst) ? string("(", cadena, ")") :
string(cadena[1:2], "(", cadena[3:end], ")")
Expand Down

0 comments on commit 933817b

Please sign in to comment.