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

Make isapprox error unless shapes match #56018

Open
jariji opened this issue Oct 6, 2024 · 5 comments
Open

Make isapprox error unless shapes match #56018

jariji opened this issue Oct 6, 2024 · 5 comments
Labels
arrays [a, r, r, a, y, s] maths Mathematical functions

Comments

@jariji
Copy link
Contributor

jariji commented Oct 6, 2024

I don't like this and I would like it to error

julia> [1; 2; 3;;]
3×1 Matrix{Int64}:
 1
 2
 3
julia> [1; 2; 3;;]  [1,2,3]
true

just like this does

julia> (x for x in 1:3)  [1,2,3]
ERROR: MethodError: no method matching isapprox(::Base.Generator{UnitRange{Int64}, typeof(identity)}, ::Vector{Int64})
@nsajko
Copy link
Contributor

nsajko commented Oct 6, 2024

Possible course of action: make a PR then request a PkgEval from NanoSoldier to see what will break.

@nsajko nsajko added arrays [a, r, r, a, y, s] maths Mathematical functions labels Oct 6, 2024
@nsajko
Copy link
Contributor

nsajko commented Oct 6, 2024

I would like it to error

Given that == returns false for array arguments of mismatched shape, perhaps [1; 2; 3;;] ≈ [1,2,3] should return false, too?

@jariji
Copy link
Contributor Author

jariji commented Oct 6, 2024

I think Matrix == Vector should error too. #40717

@nsajko
Copy link
Contributor

nsajko commented Oct 6, 2024

Yeah, but that's definitely for a breaking release.

@nsajko
Copy link
Contributor

nsajko commented Oct 9, 2024

Offending method:

# isapprox: approximate equality of arrays [like isapprox(Number,Number)]
# Supports nested arrays; e.g., for `a = [[1,2, [3,4]], 5.0, [6im, [7.0, 8.0]]]`
# `a ≈ a` is `true`.
function isapprox(x::AbstractArray, y::AbstractArray;
atol::Real=0,
rtol::Real=Base.rtoldefault(promote_leaf_eltypes(x),promote_leaf_eltypes(y),atol),
nans::Bool=false, norm::Function=norm)
d = norm(x - y)
if isfinite(d)
return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y)))
else
# Fall back to a component-wise approximate comparison
# (mapreduce instead of all for greater generality [#44893])
return mapreduce((a, b) -> isapprox(a, b; rtol=rtol, atol=atol, nans=nans), &, x, y)
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] maths Mathematical functions
Projects
None yet
Development

No branches or pull requests

2 participants