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

Feature request: dims keyword for norm #40058

Closed
wesselb opened this issue Mar 16, 2021 · 2 comments
Closed

Feature request: dims keyword for norm #40058

wesselb opened this issue Mar 16, 2021 · 2 comments

Comments

@wesselb
Copy link

wesselb commented Mar 16, 2021

In some cases, it would be really handy to be able to do this:

julia> a = randn(5, 100);

julia> norm(a, dims=1)  # Doesn't work
ERROR: MethodError: no method matching norm(::Matrix{Float64}; dims=2)

julia> sqrt.(sum(abs2, a, dims=1))  # Intended result (in this case, with `p=2`)
1×100 Matrix{Float64}:
 1.26534  0.763155  2.55044  2.38645  1.69696    1.71206  2.61463  2.81637  1.82003  1.69302

As a more advanced example, for a tensor a of shape (m, n, k), norm(a, dims=(2, 3)) would compute norm(a[i, :, :]) for all i and return something of shape (m, 1, 1).

@martinholters
Copy link
Member

julia> mapslices(norm, a; dims=1)
1×100 Matrix{Float64}:
 1.89751  3.06913  2.61767  3.51301    1.91144  0.557424  1.73838

Or mapslices(norm, a; dims=(2,3)) for the second case.

Not sure we need to add this to norm directly?

@mcabbott
Copy link
Contributor

Duplicate of JuliaLang/LinearAlgebra.jl#697, BTW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants