@@ -12,9 +12,35 @@ Immutable is a bit of a misnomer, as using the `@set!` macro from `Setfield.jl`
12
12
"""
13
13
struct ImmutableDensePolynomial{B,T,X,N} <: AbstractDenseUnivariatePolynomial{B,T,X}
14
14
coeffs:: NTuple{N,T}
15
- function ImmutableDensePolynomial {B,T,X,N} (cs:: NTuple{N} ) where {B,N,T,X}
15
+ function ImmutableDensePolynomial {B,T,X,N} (cs:: NTuple{N,T } ) where {B,N,T,X}
16
16
new {B,T,Symbol(X),N} (cs)
17
17
end
18
+
19
+ # tuple with mismatched size/type
20
+ # need zero(T) defined if padding needed
21
+ function ImmutableDensePolynomial {B,T,X,N} (xs:: Tuple{Vararg} ) where {B,T,X,N}
22
+ M = length (xs)
23
+ N′ = findlast (! iszero, xs)
24
+ N < N′ && throw (ArgumentError (" Too many coefficients for type" ))
25
+ if N == N′ == M
26
+ cs = T .(xs)
27
+ elseif isnothing (N′)
28
+ cs = ntuple (i -> zero (T), Val (N))
29
+ else
30
+ cs = ntuple (i -> i <= N′ ? T (xs[i]) : zero (first (xs)), Val (N))
31
+ end
32
+ new {B,T,X,N} (cs)
33
+ end
34
+
35
+ # zero polynomial
36
+ function ImmutableDensePolynomial {B,T,X,N} (cs:: Tuple{} ) where {B,T,X,N}
37
+ cs = ntuple (i-> zero (T), Val (N))
38
+ new {B,T,Symbol(X),N} (cs)
39
+ end
40
+ function ImmutableDensePolynomial {B,T,X,0} (cs:: Tuple{} ) where {B,T,X}
41
+ new {B,T,Symbol(X),0} (())
42
+ end
43
+
18
44
end
19
45
20
46
ImmutableDensePolynomial {B,T,X,N} (check:: Type{Val{false}} , cs:: NTuple{N,T} ) where {B,N,T,X} =
@@ -23,33 +49,38 @@ ImmutableDensePolynomial{B,T,X,N}(check::Type{Val{false}}, cs::NTuple{N,T}) wher
23
49
ImmutableDensePolynomial {B,T,X,N} (check:: Type{Val{true}} , cs:: NTuple{N,T} ) where {B,N, T,X} =
24
50
ImmutableDensePolynomial {B,T,X,N} (cs)
25
51
26
- # tuple with mismatched size
27
- function ImmutableDensePolynomial {B,T,X,N} (xs:: NTuple{M,S} ) where {B,T,S,X,N,M}
28
- p = ImmutableDensePolynomial {B,S,X,M} (xs)
29
- convert (ImmutableDensePolynomial{B,T,X,N}, ImmutableDensePolynomial {B,T,X,M} (xs))
30
- end
31
-
32
52
# vector case with N
33
53
function ImmutableDensePolynomial {B,T,X,N} (xs:: AbstractVector{S} ) where {B,T,S,X,N}
34
54
ImmutableDensePolynomial {B,T,X,N} (ntuple (Base. Fix1 (getindex, xs), Val (N)))
35
- end
55
+ end
36
56
37
57
# constant
38
58
function ImmutableDensePolynomial {B,T,X,N} (c:: S ) where {B,T,X,N,S<: Scalar }
39
- if N == 0
40
- if iszero (c)
41
- throw (ArgumentError (" Can't create zero-length polynomial" ))
42
- else
43
- return zero (ImmutableDensePolynomial{B,T,X})
44
- end
45
- end
46
- cs = ntuple (i -> i == 1 ? T (c) : zero (T), Val (N))
47
- return ImmutableDensePolynomial {B,T,X,N} (cs)
59
+ return ImmutableDensePolynomial {B,T,X,N} ((c,))
48
60
end
49
- ImmutableDensePolynomial {B,T,X} (:: Val{false} , xs:: NTuple{N,S} ) where {B,T,S,X,N} = ImmutableDensePolynomial {B,T,X,N} (convert (NTuple{N,T}, xs))
50
- ImmutableDensePolynomial {B,T,X} (xs:: NTuple{N} ) where {B,T,X,N} = ImmutableDensePolynomial {B,T,X,N} (convert (NTuple{N,T}, xs))
51
- ImmutableDensePolynomial {B,T} (xs:: NTuple{N} , var:: SymbolLike = Var (:x )) where {B,T,N} = ImmutableDensePolynomial {B,T,Symbol(var),N} (xs)
52
- ImmutableDensePolynomial {B} (xs:: NTuple{N,T} , var:: SymbolLike = Var (:x )) where {B,T,N} = ImmutableDensePolynomial {B,T,Symbol(var),N} (xs)
61
+
62
+ function ImmutableDensePolynomial {B,T,X} (:: Val{false} , xs:: Tuple{S,Vararg{S}} ) where {B,T,S,X}
63
+ N = length (xs)
64
+ ImmutableDensePolynomial {B,T,X,N} (convert (NTuple{N,T}, xs))
65
+ end
66
+
67
+ ImmutableDensePolynomial {B,T,X} (xs:: NTuple{N} ) where {B,T,X,N} =
68
+ ImmutableDensePolynomial {B,T,X,N} (convert (NTuple{N,T}, xs))
69
+
70
+ ImmutableDensePolynomial {B,T,X} (xs:: Tuple{} ) where {B,T,X} =
71
+ ImmutableDensePolynomial {B,T,X,0} (())
72
+
73
+ ImmutableDensePolynomial {B,T} (xs:: NTuple{N} , var:: SymbolLike = Var (:x )) where {B,T,N} =
74
+ ImmutableDensePolynomial {B,T,Symbol(var),N} (xs)
75
+
76
+ ImmutableDensePolynomial {B,T} (xs:: Tuple{} , var:: SymbolLike = Var (:x )) where {B,T} =
77
+ ImmutableDensePolynomial {B,T,Symbol(var),0} (xs)
78
+
79
+ ImmutableDensePolynomial {B} (xs:: Tuple{T,Vararg{T}} , var:: SymbolLike = Var (:x )) where {B,T} =
80
+ ImmutableDensePolynomial {B,T,Symbol(var),length(xs)} (xs)
81
+
82
+ ImmutableDensePolynomial {B} (xs:: Tuple{} , var:: SymbolLike = Var (:x )) where {B} =
83
+ ImmutableDensePolynomial {B,Float64,Symbol(var),0} (xs)
53
84
54
85
# abstract vector. Must eat order
55
86
ImmutableDensePolynomial {B,T,X} (:: Val{false} , xs:: AbstractVector , order:: Int = 0 ) where {B,T,X} =
@@ -80,18 +111,37 @@ Base.promote_rule(::Type{P}, ::Type{<:S}) where {S<:Number,B,T,X,N,P<:ImmutableD
80
111
81
112
function Base. convert (:: Type{<:ImmutableDensePolynomial{B,T,X,N}} ,
82
113
p:: ImmutableDensePolynomial{B,T′,X,N′} ) where {B,T,T′,X,N,N′}
83
- N′′ = findlast (! iszero, p)
84
- isnothing (N′′) && return zero (ImmutableDensePolynomial{B,T,X,N})
85
- N < N′′ && throw (ArgumentError (" Wrong size" ))
86
- ImmutableDensePolynomial {B,T,X,N} (ntuple (i -> T (p[i- 1 ]), Val (N)))
114
+ ImmutableDensePolynomial {B,T,X,N} (p. coeffs)
87
115
end
88
116
89
- function Base. map (fn, p:: P , args... ) where {B,T,X, P<: ImmutableDensePolynomial{B,T,X} }
117
+ function Base. map (fn, p:: P , args... ) where {B,T,X,N, P<: ImmutableDensePolynomial{B,T,X,N } }
90
118
xs = map (fn, p. coeffs, args... )
91
119
R = eltype (xs)
92
- return ImmutableDensePolynomial {B,R,X} (xs)
120
+ return ImmutableDensePolynomial {B,R,X,N} (xs)
121
+ end
122
+
123
+ # map has a type instability
124
+ function scalar_mult (p:: P , c:: S ) where {B, S<: Scalar , T, X, N, P<: ImmutableDensePolynomial{B,T,X,N} }
125
+ R = Base. promote_op (* , T, S)
126
+ # cs = map(Base.Fix2(*,c), p.coeffs)
127
+ cs = p. coeffs .* (c,)
128
+
129
+ ImmutableDensePolynomial {B,R,X,N} (cs)
130
+ end
131
+
132
+ function scalar_mult (c:: S , p:: P ) where {B, S<: Scalar , T, X, N, P<: ImmutableDensePolynomial{B,T,X,N} }
133
+ R = Base. promote_op (* , T, S)
134
+ # cs = map(Base.Fix1(*,c), p.coeffs)
135
+ cs = (c,) .* p. coeffs
136
+ ImmutableDensePolynomial {B,R,X,N} (cs)
93
137
end
94
138
139
+ # scalar mult faster with 0 default
140
+ scalar_mult (p:: ImmutableDensePolynomial{B,T,X,0} , c:: S ) where {B,T,X,S<: Scalar } = zero (ImmutableDensePolynomial{B,promote_type (T,S),X,0 })
141
+ scalar_mult (c:: S , p:: ImmutableDensePolynomial{B,T,X,0} ) where {B,T,X,S<: Scalar } = zero (ImmutableDensePolynomial{B,promote_type (T,S),X,0 })
142
+
143
+
144
+
95
145
96
146
Base. copy (p:: ImmutableDensePolynomial ) = p
97
147
Base. similar (p:: ImmutableDensePolynomial , args... ) = p. coeffs
@@ -257,10 +307,6 @@ function _tuple_combine(op, p::ImmutableDensePolynomial{B,T,X,N}, q::ImmutableDe
257
307
end
258
308
259
309
260
- # scalar mult faster with 0 default
261
- scalar_mult (p:: ImmutableDensePolynomial{B,T,X,0} , c:: S ) where {B,T,X,S<: Scalar } = zero (ImmutableDensePolynomial{B,promote_type (T,S),X,0 })
262
- scalar_mult (c:: S , p:: ImmutableDensePolynomial{B,T,X,0} ) where {B,T,X,S<: Scalar } = zero (ImmutableDensePolynomial{B,promote_type (T,S),X,0 })
263
-
264
310
265
311
# # ---
266
312
# Padded vector combination of two homogeneous tuples assuming N ≥ M
0 commit comments