-
Notifications
You must be signed in to change notification settings - Fork 63
/
FreeAssAlgebra.jl
253 lines (214 loc) · 7.05 KB
/
FreeAssAlgebra.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
###############################################################################
#
# FreeAssAlgebra.jl : free associative algebra R<x1,...,xn>
#
###############################################################################
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
coefficient_ring(R::FreeAssAlgebra{T}) where T <: RingElement = base_ring(R)
function is_domain_type(::Type{S}) where {T <: RingElement, S <: FreeAssAlgElem{T}}
return is_domain_type(T)
end
function is_exact_type(a::Type{S}) where {T <: RingElement, S <: FreeAssAlgElem{T}}
return is_exact_type(T)
end
###############################################################################
#
# String IO
#
###############################################################################
function _expressify_word!(prod::Expr, x, v::Vector)
j = -1
e = 0
for i in v
if j != i
if j > 0 && !iszero(e)
push!(prod.args, e == 1 ? x[j] : Expr(:call, :^, x[j], e))
end
e = 0
end
j = i
e += 1
end
if j > 0 && !iszero(e)
push!(prod.args, e == 1 ? x[j] : Expr(:call, :^, x[j], e))
end
end
function expressify(a::FreeAssAlgElem, x = symbols(parent(a)); context = nothing)
sum = Expr(:call, :+)
for (c, v) in zip(coefficients(a), exponent_words(a))
prod = Expr(:call, :*)
if !isone(c)
push!(prod.args, expressify(c, context = context))
end
_expressify_word!(prod, x, v)
push!(sum.args, prod)
end
return sum
end
@enable_all_show_via_expressify FreeAssAlgElem
function show(io::IO, ::MIME"text/plain", a::FreeAssAlgebra)
max_vars = 5 # largest number of variables to print
n = nvars(a)
print(io, "Free associative algebra")
print(io, " on ", ItemQuantity(nvars(a), "indeterminate"), " ")
if n > max_vars
join(io, symbols(a)[1:max_vars - 1], ", ")
println(io, ", ..., ", symbols(a)[n])
else
join(io, symbols(a), ", ")
println(io)
end
io = pretty(io)
print(io, Indent(), "over ", Lowercase(), base_ring(a))
print(io, Dedent())
end
function show(io::IO, a::FreeAssAlgebra)
if get(io, :supercompact, false)
# no nested printing
print(io, "Free associative algebra")
else
# nested printing allowed, preferably supercompact
io = pretty(io)
print(io, "Free associative algebra on ", ItemQuantity(nvars(a), "indeterminate"))
print(IOContext(io, :supercompact => true), " over ", Lowercase(), base_ring(a))
end
end
###############################################################################
#
# Basic Manipulation
#
###############################################################################
function coefficients(a::FreeAssAlgElem)
return Generic.MPolyCoeffs(a)
end
function terms(a::FreeAssAlgElem)
return Generic.MPolyTerms(a)
end
function monomials(a::FreeAssAlgElem)
return Generic.MPolyMonomials(a)
end
@doc raw"""
exponent_words(a::FreeAssAlgElem{T}) where T <: RingElement
Return an iterator for the exponent words of the given polynomial. To
retrieve an array of the exponent words, use `collect(exponent_words(a))`.
"""
function exponent_words(a::FreeAssAlgElem{T}) where T <: RingElement
return Generic.FreeAssAlgExponentWords(a)
end
function is_unit(a::FreeAssAlgElem{T}) where T
if is_constant(a)
return is_unit(leading_coefficient(a))
elseif is_domain_type(elem_type(coefficient_ring(a)))
return false
elseif length(a) == 1
return false
else
throw(NotImplementedError(:is_unit, a))
end
end
###############################################################################
#
# Hashing
#
###############################################################################
function Base.hash(x::FreeAssAlgElem{T}, h::UInt) where T <: RingElement
b = 0x6220ed52502c8d9f%UInt
for (c, e) in zip(coefficients(x), exponent_words(x))
b = xor(b, xor(hash(c, h), h))
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
b = xor(b, xor(hash(e, h), h))
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
end
return b
end
###############################################################################
#
# Evaluation
#
###############################################################################
@doc raw"""
evaluate(a::FreeAssAlgElem{T}, vals::Vector{U}) where {T <: RingElement, U <: NCRingElem}
Evaluate `a` by substituting in the array of values for each of the variables.
The evaluation will succeed if multiplication is defined between elements of
the coefficient ring of `a` and elements of `vals`.
The syntax `a(vals...)` is also supported.
# Examples
```jldoctest; setup = :(using AbstractAlgebra)
julia> R, (x, y) = free_associative_algebra(ZZ, ["x", "y"]);
julia> f = x*y - y*x
x*y - y*x
julia> S = matrix_ring(ZZ, 2);
julia> m1 = S([1 2; 3 4])
[1 2]
[3 4]
julia> m2 = S([0 1; 1 0])
[0 1]
[1 0]
julia> evaluate(f, [m1, m2])
[-1 -3]
[ 3 1]
julia> m1*m2 - m2*m1 == evaluate(f, [m1, m2])
true
julia> m1*m2 - m2*m1 == f(m1, m2)
true
```
"""
function evaluate(a::FreeAssAlgElem{T}, vals::Vector{U}) where {T <: RingElement, U <: NCRingElem}
length(vals) != nvars(parent(a)) && error("Number of variables does not match number of values")
R = base_ring(parent(a))
S = parent(one(R)*one(parent(vals[1])))
r = zero(S)
o = one(S)
for (c, v) in zip(coefficients(a), exponent_words(a))
r = addeq!(r, c*prod((vals[i] for i in v), init = o))
end
return r
end
function (a::FreeAssAlgElem{T})(val::U, vals::U...) where {T <: RingElement, U <: NCRingElem}
return evaluate(a, [val, vals...])
end
###############################################################################
#
# Random elements
#
###############################################################################
RandomExtensions.maketype(S::FreeAssAlgebra, _, _, _) = elem_type(S)
function RandomExtensions.make(S::FreeAssAlgebra,
term_range::AbstractUnitRange{Int},
exp_bound::AbstractUnitRange{Int}, vs...)
R = base_ring(S)
if length(vs) == 1 && elem_type(R) == Random.gentype(vs[1])
Make(S, term_range, exp_bound, vs[1])
else
Make(S, term_range, exp_bound, make(R, vs...))
end
end
function rand(rng::AbstractRNG, sp::SamplerTrivial{<:Make4{
<:NCRingElement, <:FreeAssAlgebra, <:AbstractUnitRange{Int}, <:AbstractUnitRange{Int}}})
S, term_range, exp_bound, v = sp[][1:end]
f = S()
g = gens(S)
R = base_ring(S)
isempty(g) && return S(rand(rng, v))
for i = 1:rand(rng, term_range)
term = S(1)
for j = 1:rand(rng, exp_bound)
term *= rand(g)
end
term *= rand(rng, v)
f += term
end
return f
end
function rand(rng::AbstractRNG, S::FreeAssAlgebra,
term_range::AbstractUnitRange{Int}, exp_bound::AbstractUnitRange{Int}, v...)
m = make(S, term_range, exp_bound, v...)
rand(rng, m)
end
function rand(S::FreeAssAlgebra, term_range, exp_bound, v...)
rand(GLOBAL_RNG, S, term_range, exp_bound, v...)
end