Skip to content

Commit

Permalink
Add some span utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Oct 7, 2024
1 parent ef7d3a3 commit b512207
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/LinearIndependence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ function is_linearly_independent(V::Vector{T}) where {T}
return is_linearly_independent_with_relations(V)[1]
end

"""
is_linearly_independent(F::Field, V::Vector{T}) where {T} -> Bool
Checks whether the elements of `V` are linearly independent over `F`.
"""
function is_linearly_independent(F::Field, V::Vector{T}) where {T}
return is_linearly_independent_with_relations(F, V)[1]
end
Expand All @@ -24,11 +29,50 @@ function is_linearly_independent_with_relations(V::Vector{T}) where {T}
return nrows(M) == 0, M
end

"""
is_linearly_independent_with_relations(F::Field, V::Vector{T}) where {T}
Checks whether the elements of `V` are linearly independent over `F`.
This function returns a tuple `(is_independent, relations)` where `is_independent`
is a boolean indicating whether the elements are linearly independent and `relations`
is a matrix whose rows are the coefficients of the linear relations.
"""
function is_linearly_independent_with_relations(F::Field, V::Vector{T}) where {T}
M = kernel(_linear_independence_coeff_matrix(F, V); side=:left)
return nrows(M) == 0, M
end

"""
is_in_span(F::Field, x::T, V::Vector{T}) where {T} -> Bool
Checks whether `x` is in the `F`-span of `V`.
"""
function is_in_span(F::Field, x::T, V::Vector{T}) where {T}
cm = _linear_independence_coeff_matrix(F, [x; V])
return rank(cm[2:end, :]) == rank(cm)
end

"""
is_span_subset(F::Field, V::Vector{T}, W::Vector{T}) where {T} -> Bool
Checks whether the `F`-span of `V` is a subset of the `F`-span of `W`.
"""
function is_span_subset(F::Field, V::Vector{T}, W::Vector{T}) where {T}
cm = _linear_independence_coeff_matrix(F, [V; W])
return rank(cm[length(V)+1:end, :]) == rank(cm)
end

"""
is_span_equal(F::Field, V::Vector{T}, W::Vector{T}) where {T} -> Bool
Checks whether the `F`-span of `V` is equal to the `F`-span of `W`.
"""
function is_span_equal(F::Field, V::Vector{T}, W::Vector{T}) where {T}
cm = _linear_independence_coeff_matrix(F, [V; W])
return rank(cm[1:length(V), :]) == rank(cm[length(V)+1:end, :]) == rank(cm)
end

function _linear_independence_coeff_matrix(V::Vector{<:FieldElem})
@req length(V) > 0 "For empty vectors, the field needs to be specified"
return _linear_independence_coeff_matrix(parent(V[1]), V)
Expand Down
3 changes: 3 additions & 0 deletions src/PBWDeformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ export general_linear_lie_algebra
export inneighbor
export inneighbors
export is_crossing_free
export is_in_span
export is_linearly_independent
export is_linearly_independent_with_relations
export is_pbwdeformation
export is_span_equal
export is_span_subset
export isomorphic_module_with_simple_structure
export lookup_data
export lower_vertex, is_lower_vertex
Expand Down
105 changes: 105 additions & 0 deletions test/LinearIndependence-test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
@testset "LinearIndependence.jl tests" begin
@testset "is_linearly_independent(_with_relations)" begin
#TODO

end

@testset" is_in_span" begin
@test is_in_span(QQ, matrix(QQ, 2, 2, [1, 2, 3, 4]), [matrix(QQ, 2, 2, [1, 2, 3, 4])])

@test !is_in_span(QQ, matrix(QQ, 2, 2, [1, 2, 3, 4]), [matrix(QQ, 2, 2, [1, 1, 1, 1])])

@test is_in_span(
QQ,
matrix(QQ, 2, 2, [1, 2, 3, 4]),
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

@test !is_in_span(
QQ,
matrix(QQ, 2, 2, [1, 2, 3, 4]),
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
)

@test is_in_span(
QQ,
matrix(QQ, 2, 2, [1, 2, 3, 4]),
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

end

@testset "is_span_subset" begin
@test is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4])],
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

@test is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 2, 3, 5])],
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

@test is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 2, 3, 5])],
)

@test !is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
)

@test !is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

@test is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [2, 4, 6, 8])],
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
)

@test !is_span_subset(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [2, 4, 6, 8])],
)

end

@testset "is_span_equal" begin
@test !is_span_equal(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

@test !is_span_equal(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 1, 1, 1])],
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [2, 4, 6, 8])],
)

@test is_span_equal(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 2, 3, 5])],
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

@test is_span_equal(
QQ,
[matrix(QQ, 2, 2, [1, 2, 3, 4]), matrix(QQ, 2, 2, [1, 2, 3, 5]), matrix(QQ, 2, 2, [1, 2, 3, 6])],
[matrix(QQ, 2, 2, [1, 2, 3, 6]), matrix(QQ, 2, 2, [0, 0, 0, 1])],
)

end


end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Oscar.with_unicode(true) do
# short
include("ArcDiagram-test.jl")
include("DeformationBases-test.jl")
include("LinearIndependence-test.jl")
include("Pseudograph-test.jl")
include("SmashProductLie-test.jl")
include("SmashProductLieDeform-test.jl")
Expand Down

0 comments on commit b512207

Please sign in to comment.