Skip to content

Commit

Permalink
Fix some problems for comparisons between tfs (#622)
Browse files Browse the repository at this point in the history
* Use den/num vec and fix indexing

* Add tests that would have failed before

* Allow compare between poly of different lengths

* less allocations
  • Loading branch information
albheim authored Jan 28, 2022
1 parent afaf384 commit a9ad53d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/types/SisoTfTypes/SisoRational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,29 @@ function evalfr(f::SisoRational{T}, s::Number) where T
end
end

==(f1::SisoRational, f2::SisoRational) = (f1.num * f2.den[1] == f2.num * f1.den[1] && f1.den * f2.den[1] == f2.den * f1.den[1]) # NOTE: Not in analogy with how it's done for SisoZpk
function ==(f1::SisoRational, f2::SisoRational)
# Get representation of num/den so index access is correct
f1num, f1den = numvec(f1), denvec(f1)
f2num, f2den = numvec(f2), denvec(f2)
(f1num * f2den[1] == f2num * f1den[1] && f1den * f2den[1] == f2den * f1den[1]) # NOTE: Not in analogy with how it's done for SisoZpk
end

# We might want to consider alowing scaled num and den as equal
function isapprox(f1::SisoRational, f2::SisoRational; rtol::Real=sqrt(eps()), atol::Real=0)
isapprox(f1.num * f2.den[1], f2.num * f1.den[1], rtol=rtol, atol=atol) && isapprox(f1.den * f2.den[1], f2.den * f1.den[1], rtol=rtol, atol=atol)
# Get representation of num/den so index access is correct
f1num, f1den = numvec(f1), denvec(f1)
f2num, f2den = numvec(f2), denvec(f2)
if length(f1num) < length(f2num)
f1num = [zeros(length(f2num) - length(f1num)); f1num]
elseif length(f2num) < length(f1num)
f2num = [zeros(length(f1num) - length(f2num)); f2num]
end
if length(f1den) < length(f2den)
f1den = [zeros(length(f2den) - length(f1den)); f1den]
elseif length(f2den) < length(f1den)
f2den = [zeros(length(f1den) - length(f2den)); f2den]
end
isapprox(f1num * f2den[1], f2num * f1den[1], rtol=rtol, atol=atol) && isapprox(f1den * f2den[1], f2den * f1den[1], rtol=rtol, atol=atol)
end

+(f1::SisoRational, f2::SisoRational) = SisoRational(f1.num*f2.den + f2.num*f1.den, f1.den*f2.den)
Expand Down
5 changes: 5 additions & 0 deletions test/test_transferfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ z = tf("z", 0.005)
@test tf([1.0], [2.0,3.0]) == tf*[1.0], π*[2.0,3.0])
@test tf([1.0+2.0im], [2.0+im,3.0]) == tf((π+im)*[1+2.0im], (π+im)*[2.0+im,3.0])

# Test inequality
@test tf([1], [1]) != tf([2], [1])
@test tf([1.0], [1.0,0.0]) != tf([1.0], [2.0,0.0])
@test tf([1.0+2.0im], [2.0+im,3.0]) != tf([1+2.0im], [1.0+im,3.0])

# Test approximate equlity
# rtol should just be on the order of ϵ, no particular reason that exactly ϵ
# would work, but apparently it does
Expand Down

0 comments on commit a9ad53d

Please sign in to comment.