-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
329 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
add_singleton(x::AbstractArray, ::Val{dim}) where {dim} | ||
Add an additional dimension `dim` of size 1 to array `x`. | ||
""" | ||
function add_singleton(x::AbstractArray, ::Val{dim}) where {dim} | ||
shape = ntuple(max(ndims(x) + 1, dim)) do i | ||
return i < dim ? size(x, i) : (i > dim ? size(x, i - 1) : 1) | ||
end | ||
return reshape(x, shape) | ||
end | ||
|
||
""" | ||
dot_matwise(x::AbstractArray, y::AbstractArray) | ||
Compute the inner product of all matrices in `x` and `y`. | ||
At least one of `x` and `y` has to be a matrix. | ||
""" | ||
dot_matwise(x::AbstractMatrix, y::AbstractMatrix) = dot(x, y) | ||
function dot_matwise(x::AbstractArray, y::AbstractMatrix) | ||
xmat = reshape(x, size(x, 1) * size(x, 2), :) | ||
return reshape(reshape(y, 1, :) * xmat, size(x)[3:end]) | ||
end | ||
dot_matwise(x::AbstractMatrix, y::AbstractArray) = dot_matwise(y, x) | ||
|
||
""" | ||
checksize2(x::AbstractVecOrMat, y::AbstractVecOrMat) | ||
Check if arrays `x` and `y` are compatible, then return a tuple of its broadcasted second | ||
dimension. | ||
""" | ||
checksize2(::AbstractVector, ::AbstractVector) = () | ||
function checksize2(μ::AbstractVecOrMat, ν::AbstractVecOrMat) | ||
size_μ_2 = size(μ, 2) | ||
size_ν_2 = size(ν, 2) | ||
if size_μ_2 > 1 && size_ν_2 > 1 && size_μ_2 != size_ν_2 | ||
throw(DimensionMismatch("size of source and target marginals is not compatible")) | ||
end | ||
return (max(size_μ_2, size_ν_2),) | ||
end | ||
|
||
""" | ||
checkbalanced(μ::AbstractVecOrMat, ν::AbstractVecOrMat) | ||
Check that source and target marginals `μ` and `ν` are balanced. | ||
""" | ||
function checkbalanced(μ::AbstractVector, ν::AbstractVector) | ||
sum(μ) ≈ sum(ν) || throw(ArgumentError("source and target marginals are not balanced")) | ||
return nothing | ||
end | ||
function checkbalanced(x::AbstractVecOrMat, y::AbstractVecOrMat) | ||
all(isapprox.(sum(x; dims=1), sum(y; dims=1))) || | ||
throw(ArgumentError("source and target marginals are not balanced")) | ||
return nothing | ||
end |
Oops, something went wrong.
dc4dd48
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
dc4dd48
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/38091
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: