Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add f(x) = A*x, A\x #11

Merged
merged 6 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/DiffTests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DiffTests

using LinearAlgebra: det, norm, dot, tr
using LinearAlgebra: det, norm, dot, tr, Diagonal, LowerTriangular, UpperTriangular
using Statistics: mean

#=
Expand Down Expand Up @@ -249,6 +249,39 @@ end
const INPLACE_ARRAY_TO_ARRAY_FUNCS = (chebyquad!, brown_almost_linear!, trigonometric!,
mutation_test_1!, mutation_test_2!)

############################
# f(x::VecOrMat)::VecOrMat #
############################

diag_matrix(::Type{T}, n::Integer) where T<:Real =
Diagonal(LinRange(convert(T, 0.01), convert(T, 100.0), n))
diag_matrix(x::VecOrMat) = diag_matrix(Float64, size(x, 1))

hilbert_matrix(::Type{T}, n::Integer) where T<:Real =
[convert(T, inv(i + j - 1)) for i in 1:n, j in 1:n]
hilbert_matrix(x::VecOrMat) = hilbert_matrix(Float64, size(x, 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't seem to be used anywhere in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't seem to be used anywhere in this PR.

Yes, it doesn't. I was undecided which constant matrix to use, it looked like with this one the errors are higher (it is beyond my capability to track and possibly fix the source of that errors).
I can remove, if it is critical, but maybe it is better to keep it, anticipating the future extension of the test coverage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally think keeping unused code in the main branch should be avoided. Everything else looks fine though in case someone with merge rights cares about my opinion 🙂 .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this should be removed before we can merge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oxinabox done


lehmer_matrix(::Type{T}, n::Integer) where T<:Real =
[convert(T, min(i, j)/max(i, j)) for i in 1:n, j in 1:n]
lehmer_matrix(x::VecOrMat) = lehmer_matrix(Float64, size(x, 1))

test_matrix = lehmer_matrix

# left multiplication by a constant matrix
diag_lmul(x::VecOrMat) = diag_matrix(x) * x
dense_lmul(x::VecOrMat) = test_matrix(x) * x
utriag_lmul(x::VecOrMat) = UpperTriangular(test_matrix(x)) * x
ltriag_lmul(x::VecOrMat) = LowerTriangular(test_matrix(x)) * x

# left division by a constant matrix
diag_ldiv(x::VecOrMat) = diag_matrix(x) \ x
dense_ldiv(x::VecOrMat) = test_matrix(x) \ x
utriag_ldiv(x::VecOrMat) = UpperTriangular(test_matrix(x)) \ x
ltriag_ldiv(x::VecOrMat) = LowerTriangular(test_matrix(x)) \ x

const VECTOR_TO_VECTOR_FUNCS = (diag_lmul, dense_lmul, utriag_lmul, ltriag_lmul,
diag_ldiv, utriag_ldiv, ltriag_ldiv)

######################
# f(x::Array)::Array #
######################
Expand All @@ -274,6 +307,8 @@ const ARRAY_TO_ARRAY_FUNCS = (-, chebyquad, brown_almost_linear, trigonometric,
# f(::Matrix)::Matrix #
#######################

const MATRIX_TO_MATRIX_FUNCS = (inv,)
const MATRIX_TO_MATRIX_FUNCS = (inv,
diag_lmul, dense_lmul, utriag_lmul, ltriag_lmul,
diag_ldiv, utriag_ldiv, ltriag_ldiv)

end # module
8 changes: 6 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
@test isa(f(y), Array)
end

for f in DiffTests.VECTOR_TO_VECTOR_FUNCS
@test isa(f(y), Vector)
end

for f in DiffTests.MATRIX_TO_MATRIX_FUNCS
@test isa(f(A), Array)
@test isa(f(A), Matrix)
end

for f in DiffTests.BINARY_MATRIX_TO_MATRIX_FUNCS
@test isa(f(A, B), Array)
@test isa(f(A, B), Matrix)
end

# f! returns Nothing
Expand Down