Skip to content

Commit 6bdfff6

Browse files
committed
fix mapdomain and
1 parent 74a1c1e commit 6bdfff6

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

src/common.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export fromroots,
66
coeffs,
77
degree,
88
domain,
9-
scale_to_domain,
9+
mapdomain,
1010
order,
1111
hasnan,
1212
roots,
@@ -57,12 +57,13 @@ fromroots(A::AbstractMatrix{T}; var::SymbolLike = :x) where {T <: Number} = from
5757
fit(x, y; [weights], deg=length(x) - 1, var=:x)
5858
fit(::Type{<:AbstractPolynomial}, x, y; [weights], deg=length(x)-1, var=:x)
5959
60-
Fit the given data as a polynomial type with the given degree. Uses linear least squares. When weights are given, as either a `Number`, `Vector` or `Matrix`, will use weighted linear least squares. The default polynomial type is [`Polynomial`](@ref).
60+
Fit the given data as a polynomial type with the given degree. Uses linear least squares. When weights are given, as either a `Number`, `Vector` or `Matrix`, will use weighted linear least squares. The default polynomial type is [`Polynomial`](@ref). This will automatically scale your data to the [`domain`](@ref) of the polynomial type using [`mapdomain`](@ref)
6161
"""
6262
function fit(P::Type{<:AbstractPolynomial},
6363
x::AbstractVector{T},
6464
y::AbstractVector{T};
6565
weights = nothing, deg::Integer = length(x) - 1, var = :x) where {T <: Number}
66+
x = mapdomain(P, x)
6667
vand = vander(P, x, deg)
6768
if weights !== nothing
6869
coeffs = _wlstsq(vand, y, weights)
@@ -310,7 +311,8 @@ domain(::Type{<:AbstractPolynomial})
310311
domain(::P) where {P <: AbstractPolynomial} = domain(P)
311312

312313
"""
313-
scale_to_domain(::Type{<:AbstractPolynomial}, x)
314+
mapdomain(::Type{<:AbstractPolynomial}, x::AbstractArray)
315+
mapdomain(::AbstractPolynomial, x::AbstractArray)
314316
315317
Given values of x that are assumed to be unbounded (-∞, ∞), return values rescaled to the domain of the given polynomial.
316318
@@ -319,16 +321,20 @@ Given values of x that are assumed to be unbounded (-∞, ∞), return values re
319321
julia> x = -10:10
320322
-10:10
321323
322-
julia> scale_to_domain(ChebyshevT, x)
324+
julia> mapdomain(ChebyshevT, x)
323325
-1.0:0.1:1.0
324326
325327
```
326328
"""
327-
function scale_to_domain(P::Type{<:AbstractPolynomial}, x)
329+
function mapdomain(P::Type{<:AbstractPolynomial}, x::AbstractArray)
328330
d = domain(P)
329-
x_scaled = x .* (last(d) - first(d)) / (last(x) - first(x))
331+
x = collect(x)
332+
x_zerod = x .- minimum(x)
333+
x_scaled = x_zerod .* (last(d) - first(d)) ./ maximum(x_zerod)
334+
x_scaled .+= first(d)
335+
return x_scaled
330336
end
331-
scale_to_domain(::P, x) where {P <: AbstractPolynomial} = scale_to_domain(P, x)
337+
mapdomain(::P, x::AbstractArray) where {P <: AbstractPolynomial} = mapdomain(P, x)
332338

333339
#=
334340
indexing
@@ -437,7 +443,7 @@ Polynomial(4.0 - 6.0*x + 2.0*x^2)
437443
438444
```
439445
"""
440-
function Base.gcd(p1::AbstractPolynomial{T}, p2::AbstractPolynomial{S}) where {T, S}
446+
function Base.gcd(p1::AbstractPolynomial{T}, p2::AbstractPolynomial{S}) where {T,S}
441447
r₀, r₁ = promote(p1, p2)
442448
iter = 1
443449
itermax = length(r₁)

src/polynomials/Poly.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Base.convert(::Type{<:Polynomial}, p::Poly{T}) where {T} = Polynomial(p.coeffs,
2828
Base.convert(::Type{Polynomial{T}}, p::Poly{S}) where {T,S} = Polynomial(T.(p.coeffs), p.var)
2929

3030
domain(::Type{<:Poly}) = Interval(-Inf, Inf)
31-
scale_to_domain(::Type{<:Poly}, x) = x
31+
mapdomain(::Type{<:Poly}, x::AbstractArray) = x
3232

3333
function (p::Poly{T})(x::S) where {T,S}
3434
R = promote_type(T, S)

src/polynomials/Polynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ end
4040
@register Polynomial
4141

4242
domain(::Type{<:Polynomial}) = Interval(-Inf, Inf)
43-
scale_to_domain(::Type{<:Polynomial}, x) = x
43+
mapdomain(::Type{<:Polynomial}, x::AbstractArray) = x
4444

4545
function (p::Polynomial{T})(x::S) where {T,S}
4646
R = promote_type(T, S)

test/ChebyshevT.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ end
112112
@test res target
113113
end
114114

115+
@testset "Mapdomain" begin
116+
x = -30:20
117+
mx = mapdomain(ChebyshevT, x)
118+
@test extrema(mx) == (-1, 1)
119+
120+
x = 0.5:0.01:0.6
121+
mx = mapdomain(ChebyshevT, x)
122+
@test extrema(mx) == (-1, 1)
123+
end
124+
115125
@testset "Arithmetic" begin
116126
# multiplication
117127
c1 = ChebyshevT([1, 2, 3])

test/Polynomial.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ using LinearAlgebra
2020
@test all([-200, -0.3, 1, 48.2] .∈ domain(p))
2121
end
2222

23+
@testset "Mapdomain" begin
24+
x = -30:20
25+
mx = mapdomain(Polynomial, x)
26+
@test mx == x
27+
28+
x = 0.5:0.01:0.6
29+
mx = mapdomain(Polynomial, x)
30+
@test mx == x
31+
end
32+
2333
@testset "Other Construction" begin
2434
# Leading 0s
2535
p = Polynomial([1, 2, 0, 0])
@@ -164,7 +174,7 @@ end
164174
@test maximum(abs_error) <= 0.03
165175

166176
# Test weighted
167-
for W in [1, ones(size(xs)), diagm(0=>ones(size(xs)))]
177+
for W in [1, ones(size(xs)), diagm(0 => ones(size(xs)))]
168178
p = fit(Polynomial, xs, ys, weights = W, deg = 2)
169179
@test p.(xs) y_fit
170180
end
@@ -213,16 +223,16 @@ end
213223
@test p == Polynomial([6, -5, 1])
214224
@test roots(p) r
215225

216-
@test roots(p0)==roots(p1)==roots(pNULL)==[]
226+
@test roots(p0) == roots(p1) == roots(pNULL) == []
217227
@test roots(Polynomial([0,1,0])) == [0.0]
218228
r = roots(Polynomial([0,1,0]))
219229

220230
@test roots(p2) == [-1]
221231
a_roots = copy(pN.coeffs)
222-
@test all(map(abs,sort(roots(fromroots(a_roots))) - sort(a_roots)) .< 1e6)
232+
@test all(map(abs, sort(roots(fromroots(a_roots))) - sort(a_roots)) .< 1e6)
223233
@test length(roots(p5)) == 4
224234
@test roots(pNULL) == []
225-
@test sort(roots(pR)) == [1//2, 3//2]
235+
@test sort(roots(pR)) == [1 // 2, 3 // 2]
226236

227237
A = [1 0; 0 1]
228238
p = fromroots(Polynomial, A)
@@ -426,7 +436,7 @@ end
426436
p = Polynomial([1.0, 2.0, 3.0])
427437
@test sprint(show, p) == "Polynomial(1.0 + 2.0*x + 3.0*x^2)"
428438

429-
p = Polynomial([1+1im, -2im])
439+
p = Polynomial([1 + 1im, -2im])
430440
@test sprint(show, p) == "Polynomial((1 + 1im) - 2im*x)"
431441

432442
p = Polynomial{Rational}([1, 4])
@@ -438,15 +448,15 @@ end
438448
@test repr(p) == "Polynomial(1.0 + 2.0*x + 3.0*x^2 + 1.0*x^3)"
439449
p = Polynomial([1, im])
440450
@test repr(p) == "Polynomial(1 + im*x)"
441-
p = Polynomial([1+im, 1-im, -1+im, -1 - im])# minus signs
451+
p = Polynomial([1 + im, 1 - im, -1 + im, -1 - im])# minus signs
442452
@test repr(p) == "Polynomial((1 + 1im) + (1 - 1im)*x - (1 - 1im)*x^2 - (1 + 1im)*x^3)"
443-
p = Polynomial([1.0, 0 + NaN*im, NaN, Inf, 0 - Inf*im]) # handle NaN or Inf appropriately
453+
p = Polynomial([1.0, 0 + NaN * im, NaN, Inf, 0 - Inf * im]) # handle NaN or Inf appropriately
444454
@test repr(p) == "Polynomial(1.0 + NaN*im*x + NaN*x^2 + Inf*x^3 - Inf*im*x^4)"
445455

446456
p = Polynomial([1,2,3])
447457

448458
@test repr("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
449-
p = Polynomial([1//2, 2//3, 1])
459+
p = Polynomial([1 // 2, 2 // 3, 1])
450460
@test repr("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
451461

452462
# customized printing with printpoly
@@ -456,20 +466,20 @@ end
456466
return String(take!(buf))
457467
end
458468
@test printpoly_to_string(Polynomial([1,2,3], "y")) == "1 + 2*y + 3*y^2"
459-
@test printpoly_to_string(Polynomial([1,2,3], "y"), descending_powers=true) == "3*y^2 + 2*y + 1"
460-
@test printpoly_to_string(Polynomial([2, 3, 1], :z), descending_powers=true, offset=-2) == "1 + 3*z^-1 + 2*z^-2"
461-
@test printpoly_to_string(Polynomial([-1, 0, 1], :z), offset=-1, descending_powers=true) == "z - z^-1"
469+
@test printpoly_to_string(Polynomial([1,2,3], "y"), descending_powers = true) == "3*y^2 + 2*y + 1"
470+
@test printpoly_to_string(Polynomial([2, 3, 1], :z), descending_powers = true, offset = -2) == "1 + 3*z^-1 + 2*z^-2"
471+
@test printpoly_to_string(Polynomial([-1, 0, 1], :z), offset = -1, descending_powers = true) == "z - z^-1"
462472
end
463473

464474
@testset "Plotting" begin
465475
p = fromroots([-1, 1]) # x^2 - 1
466476
r = -2:0.04:2
467-
rec = apply_recipe(Dict{Symbol, Any}(), p)
468-
@test getfield(rec[1], 1) == Dict{Symbol, Any}(:label => "-1 + x^2")
477+
rec = apply_recipe(Dict{Symbol,Any}(), p)
478+
@test getfield(rec[1], 1) == Dict{Symbol,Any}(:label => "-1 + x^2")
469479
@test rec[1].args == (r, p.(r))
470480

471481
r = -1:0.02:1
472-
rec = apply_recipe(Dict{Symbol, Any}(), p, -1, 1)
473-
@test getfield(rec[1], 1) == Dict{Symbol, Any}(:label => "-1 + x^2")
482+
rec = apply_recipe(Dict{Symbol,Any}(), p, -1, 1)
483+
@test getfield(rec[1], 1) == Dict{Symbol,Any}(:label => "-1 + x^2")
474484
@test rec[1].args == (r, p.(r))
475485
end

0 commit comments

Comments
 (0)