@@ -6,111 +6,124 @@ function Base.:(*)(x::PolyVar{true}, y::PolyVar{true})
66 Monomial {true} (x > y ? [x,y] : [y,x], [1 ,1 ])
77 end
88end
9- function multiplyvar (v:: Vector{PolyVar{true}} , x:: PolyVar{true} )
10- i = findfirst (w -> w <= x , v)
9+ function multiplyvar (v:: Vector{PolyVar{true}} , x:: PolyVar{true} , z )
10+ i = findfirst (Base . Fix2 ( <= , x) , v)
1111 if (i != nothing && i > 0 ) && v[i] == x
12- multiplyexistingvar (v, x, i )
12+ copy (v), multiplyexistingvar (i, z )
1313 else
14- insertvar (v, x, (i == nothing || i == 0 ) ? length (v)+ 1 : i)
14+ j = (i == nothing || i == 0 ) ? length (v)+ 1 : i
15+ insertvar (v, x, j), insertvar (z, x, j)
1516 end
1617end
1718function Base.:(* )(x:: PolyVar{true} , y:: Monomial{true} )
18- w, updatez = multiplyvar (y. vars, x)
19- Monomial {true} (w, updatez (y . z) )
19+ w, z = multiplyvar (y. vars, x, y . z )
20+ Monomial {true} (w, z )
2021end
2122Base.:(* )(y:: MonomialVector{true} , x:: PolyVar{true} ) = x * y
2223function Base.:(* )(x:: PolyVar{true} , y:: MonomialVector{true} )
23- w, updatez = multiplyvar (y. vars, x)
24- MonomialVector {true} (w, updatez .(y . Z) )
24+ w, Z = multiplyvar (y. vars, x, y . Z )
25+ MonomialVector {true} (w, Z )
2526end
26- function multdivmono! (output_variables:: Vector{PolyVar{true}} ,
27- v:: Vector{PolyVar{true}} , x:: Monomial{true} , op)
27+ function _operate_exponents_to! (output:: Vector{Int} , op:: F , z1:: Vector{Int} , z2:: Vector{Int} ) where {F<: Function }
28+ @. output = op (z1, z2)
29+ return
30+ end
31+ function _operate_exponents_to! (output:: Vector{Int} , op:: F , z1:: Vector{Int} , z2:: Vector{Int} , n) where {F<: Function }
32+ resize! (output, n)
33+ @. output = op (z1, z2)
34+ return
35+ end
36+ function _operate_exponents_to! (output:: Vector{Int} , op:: F , z1:: Vector{Int} , z2:: Vector{Int} , n, maps) where {F<: Function }
37+ resize! (output, n)
38+ I = maps[1 ]; i = 1 ; lI = length (I)
39+ J = maps[2 ]; j = 1 ; lJ = length (J)
40+ while i <= lI || j <= lJ
41+ if i > lI || (j <= lJ && J[j] < I[i])
42+ output[J[j]] = op (0 , z2[j])
43+ j += 1
44+ elseif j > lJ || (i <= lI && I[i] < J[j])
45+ output[I[i]] = op (z1[I[i]], 0 )
46+ i += 1
47+ else
48+ @assert I[i] == J[j]
49+ output[I[i]] = op (z1[I[i]], z2[j])
50+ i += 1
51+ j += 1
52+ end
53+ end
54+ return
55+ end
56+ # Not used yet
57+ # function _operate_exponents!(op::F, Z::Vector{Vector{Int}}, z2::Vector{Int}, args::Vararg{Any,N}) where {F<:Function,N}
58+ # return Vector{Int}[_operate_exponents!(op, z, z2, args...) for z in Z]
59+ # end
60+ function multdivmono! (output, output_variables:: Vector{PolyVar{true}} ,
61+ v:: Vector{PolyVar{true}} , x:: Monomial{true} , op, z)
2862 if v == x. vars
2963 if output_variables == v
30- updatez! = (output, z) -> begin
31- @. output = op (z, x. z)
32- return
33- end
64+ _operate_exponents_to! (output, op, z, x. z)
3465 else
3566 resize! (output_variables, length (v))
3667 copyto! (output_variables, v)
37- updatez! = (output, z) -> begin
38- resize! (output, length (output_variables))
39- @. output = op (z, x. z)
40- return
41- end
68+ _operate_exponents_to! (output, op, z, x. z, length (output_variables))
4269 end
4370 else
4471 maps = mergevars_to! (output_variables, [v, x. vars])
45- n = length (v)
46- updatez! = (output, z) -> begin
47- resize! (output, length (output_variables))
48- I = maps[1 ]; i = 1 ; lI = length (I)
49- J = maps[2 ]; j = 1 ; lJ = length (J)
50- while i <= lI || j <= lJ
51- if i > lI || (j <= lJ && J[j] < I[i])
52- output[J[j]] = op (0 , x. z[j])
53- j += 1
54- elseif j > lJ || (i <= lI && I[i] < J[j])
55- output[I[i]] = op (z[I[i]], 0 )
56- i += 1
57- else
58- @assert I[i] == J[j]
59- output[I[i]] = op (z[I[i]], x. z[j])
60- i += 1
61- j += 1
62- end
63- end
72+ _operate_exponents_to! (output, op, z, x. z, length (output_variables), maps)
73+ end
74+ return
75+ end
76+ function _operate_exponents (op:: F , z1:: Vector{Int} , z2:: Vector{Int} ) where {F<: Function }
77+ return op .(z1, z2)
78+ end
79+ function _operate_exponents (op:: F , z1:: Vector{Int} , z2:: Vector{Int} , n, maps) where {F<: Function }
80+ newz = zeros (Int, n)
81+ I = maps[1 ]; i = 1 ; lI = length (I)
82+ J = maps[2 ]; j = 1 ; lJ = length (J)
83+ while i <= lI || j <= lJ
84+ if i > lI || (j <= lJ && J[j] < I[i])
85+ newz[J[j]] = op (0 , z2[j])
86+ j += 1
87+ elseif j > lJ || (i <= lI && I[i] < J[j])
88+ newz[I[i]] = op (z1[i], 0 )
89+ i += 1
90+ else
91+ @assert I[i] == J[j]
92+ newz[I[i]] = op (z1[i], z2[j])
93+ i += 1
94+ j += 1
6495 end
6596 end
66- return updatez!
97+ return newz
6798end
68- function multdivmono (v:: Vector{PolyVar{true}} , x:: Monomial{true} , op)
99+ function _operate_exponents (op:: F , Z:: Vector{Vector{Int}} , z2:: Vector{Int} , args:: Vararg{Any,N} ) where {F<: Function ,N}
100+ return Vector{Int}[_operate_exponents (op, z, z2, args... ) for z in Z]
101+ end
102+ function multdivmono (v:: Vector{PolyVar{true}} , x:: Monomial{true} , op, z)
69103 if v == x. vars
70104 w = copy (v)
71- updatez = z -> op .( z, x. z)
105+ z_new = _operate_exponents (op, z, x. z)
72106 else
73107 w, maps = mergevars ([v, x. vars])
74- updatez = z -> begin
75- newz = zeros (Int, length (w))
76- I = maps[1 ]; i = 1 ; lI = length (I)
77- J = maps[2 ]; j = 1 ; lJ = length (J)
78- while i <= lI || j <= lJ
79- if i > lI || (j <= lJ && J[j] < I[i])
80- newz[J[j]] = op (0 , x. z[j])
81- j += 1
82- elseif j > lJ || (i <= lI && I[i] < J[j])
83- newz[I[i]] = op (z[i], 0 )
84- i += 1
85- else
86- @assert I[i] == J[j]
87- newz[I[i]] = op (z[i], x. z[j])
88- i += 1
89- j += 1
90- end
91- end
92- newz
93- end
108+ z_new = _operate_exponents (op, z, x. z, length (w), maps)
94109 end
95- w, updatez
110+ return w, z_new
96111end
97112function MP. mapexponents_to! (output:: Monomial{true} , f:: Function , x:: Monomial{true} , y:: Monomial{true} )
98- updatez! = multdivmono! (output. vars, x. vars, y, f)
99- updatez! (output. z, x. z)
100- return x
113+ multdivmono! (output. z, output. vars, x. vars, y, f, x. z)
114+ return output
101115end
102116function MP. mapexponents! (f:: Function , x:: Monomial{true} , y:: Monomial{true} )
103- updatez! = multdivmono! (x. vars, x. vars, y, f)
104- updatez! (x. z, x. z)
117+ multdivmono! (x. z, x. vars, x. vars, y, f, x. z)
105118 return x
106119end
107120function MP. mapexponents (f:: Function , x:: Monomial{true} , y:: Monomial{true} )
108- w, updatez = multdivmono (x. vars, y, f)
109- Monomial {true} (w, updatez (x . z) )
121+ w, z = multdivmono (x. vars, y, f, x . z )
122+ return Monomial {true} (w, z )
110123end
111124function Base.:(* )(x:: Monomial{true} , y:: MonomialVector{true} )
112- w, updatez = multdivmono (y. vars, x, + )
113- MonomialVector {true} (w, updatez .(y . Z) )
125+ w, Z = multdivmono (y. vars, x, + , y . Z )
126+ return MonomialVector {true} (w, Z )
114127end
115128Base.:(* )(y:: MonomialVector{true} , x:: Monomial{true} ) = x * y
116129Base.:(* )(x:: Monomial{true} , y:: PolyVar{true} ) = y * x
0 commit comments