# Norm, normalize

**URL:** https://discourse.julialang.org/t/norm-normalize/1818
**Category:** Internals & Design
**Created:** [February 1, 2017, 12:41pm UTC](https://discourse.julialang.org/t/norm-normalize/1818 "2017-02-01T12:41:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [February 1, 2017, 12:41pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/1 "2017-02-01T12:41:50Z")

</div>

I just realized, that `normalize` only has methods for `AbstractVectors`.

```julia
julia> methods(normalize)
# 2 methods for generic function "normalize":
normalize(v::AbstractArray{T,1} where T) in Base.LinAlg at linalg/generic.jl:1294
normalize(v::AbstractArray{T,1} where T, p::Real) in Base.LinAlg at linalg/generic.jl:1294

```

And `norm` is only defined for vectors and matrices. Mathematically the `p` norm is reasonable for  
arrays of any dimension, so to me it seems both of these should be defined for `AbstractArray`.

Is there a good reason for limiting these definitions?

---

<div class="post-metadata">

### Author: ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)
#### Post date: [February 1, 2017, 3:18pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/2 "2017-02-01T15:18:47Z")

</div>

It is usually not ambiguous how people would like to normalize a vector whereas I think that would be the case for matrices. We could choose a default but I’m not sure that there is as much a demand for that compared to how frequently people normalize vectors.

The `norm` function for matrices is the operator norm where the operator is applied with `*` on a `Vector`. We don’t define an operator for arrays of higher dimension so I think it is fine **not** to define a `norm` in that case. Notice that the elementwise norm function is called `vecnorm` and works for arrays of all dimensions and even iterators.

---

<div class="post-metadata">

### Author: ![jrevels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jrevels/32/10393_2.png) [@jrevels](https://discourse.julialang.org/u/jrevels)
#### Post date: [February 1, 2017, 4:04pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/3 "2017-02-01T16:04:36Z")

</div>

It seems to me that the most consistent thing would be to define `normalize!(x::T, p::Real)` for all `T` accepted by `norm(x::T, p::Real)`, and define `vecnormalize!` for the normalization by the Frobenius norm.

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [February 1, 2017, 8:51pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/4 "2017-02-01T20:51:35Z")

</div>

Would the proposed functionality produce the projection to the Lp unit ball? Or just divide each element by the norm? My impression is that these two concepts are equivalent for the L2 norm, but aren’t generally equivalent.

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [February 1, 2017, 9:07pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/5 "2017-02-01T21:07:00Z")

</div>

You are right, they differ. I prefer division. I often want to normalize the largest entry of an array to be one. This corresponds to division by `Inf` norm. Projection on the other hand is a quite useless operation in case of the `Inf` norm:  
`clamp.(arr, -1, 1)`

---

<div class="post-metadata">

### Author: ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)
#### Post date: [February 1, 2017, 9:45pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/6 "2017-02-01T21:45:48Z")

</div>

Notice that you can simply do

```julia
scale!(A, inv(vecnorm(A, Inf)))

```

to achieve that which might be sufficient and not much longer than `vecnormalize!(A, Inf)`. The `normalize!` function has the convenience of returning the norm and then it handles some corner cases.

---

<div class="post-metadata">

### Author: ![jrevels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jrevels/32/10393_2.png) [@jrevels](https://discourse.julialang.org/u/jrevels)
#### Post date: [February 1, 2017, 9:46pm UTC](https://discourse.julialang.org/t/norm-normalize/1818/7 "2017-02-01T21:46:11Z")

</div>

Elementwise division by the norm seems the most predicable behavior to me, but maybe that’s just my background…

If it’s worth anything, it seems that Mathematica defines `Normalize[expr, f]` to do elementwise division by the norm calculated via `f` when `expr` is a matrix (the matrix example is under “Generalizations and Extensions”):

> **[Normalize—Wolfram Language Documentation](https://reference.wolfram.com/language/ref/Normalize.html)**
>
> Normalize\[v\] gives the normalized form of a vector v. Normalize\[z\] gives the normalized form of a complex number z. Normalize\[expr, f\] normalizes with respect to the norm function f.
