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 all 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ version = "0.1.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
julia="1"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
46 changes: 44 additions & 2 deletions src/DiffTests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DiffTests

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

#=
Expand Down Expand Up @@ -249,6 +250,43 @@ 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))

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
sparse_lmul(x::VecOrMat) = sparse(test_matrix(x)) * x
sp_utriag_lmul(x::VecOrMat) = UpperTriangular(sparse(test_matrix(x))) * x
sp_ltriag_lmul(x::VecOrMat) = LowerTriangular(sparse(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
sparse_ldiv(x::VecOrMat) = sparse(test_matrix(x)) \ x
sp_utriag_ldiv(x::VecOrMat) = UpperTriangular(sparse(test_matrix(x))) \ x
sp_ltriag_ldiv(x::VecOrMat) = LowerTriangular(sparse(test_matrix(x))) \ x

const VECTOR_TO_VECTOR_FUNCS = (diag_lmul, dense_lmul, utriag_lmul, ltriag_lmul,
sparse_lmul, sp_utriag_lmul, sp_ltriag_lmul,
diag_ldiv, utriag_ldiv, ltriag_ldiv,
sparse_ldiv, sp_utriag_ldiv, sp_ltriag_ldiv,)

######################
# f(x::Array)::Array #
######################
Expand All @@ -274,6 +312,10 @@ 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,
sparse_lmul, sp_utriag_lmul, sp_ltriag_lmul,
diag_ldiv, utriag_ldiv, ltriag_ldiv,
sparse_ldiv, sp_utriag_ldiv, sp_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