# Function returning normalized vector and norm

**URL:** <https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [June 30, 2020, 5:22am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285 "2020-06-30T05:22:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 30, 2020, 5:22am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/1 "2020-06-30T05:22:09Z")

</div>

Does the standard library have a function equivalent to the following?

```julia
function norm_normalize(v, p = 2)
    n = norm(v,p)
    return n, v/n
end

```

If not, is there a reason why such a function does not exist?

And finally, if the function does not exist but you think that it should exist, what should the name be? The name `norm_normalize()` is modeled after functions like `reim()` or `sincos()`, but I think it’s a bit too wordy. Another option would be `normunit()`, which follows a similar idea and is shorter but which is also more unclear.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 30, 2020, 6:10am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/2 "2020-06-30T06:10:44Z")

</div>

I don’t know if it exists. But what is the purpose of this function? It seems trivial enough that I would write it out inline, or define my own local function.

I have a similar feeling about `reim`, I don’t really see the point of it. Perhaps it’s very commonly used?

`sincos` is different, since it does something highly non-trivial.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 30, 2020, 6:28am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/3 "2020-06-30T06:28:05Z")

</div>

> But what is the purpose of this function?

Convenience. It allows you to merge two to three lines into one and avoid introducing some temporary variables. This may sound trivial, but often these things appear in quite complicated mathematical algorithms where squeezing out every bit of conciseness really helps. Also, note that `LinearAlgebra` already has a `normalize()` function which is about as trivial as `norm_normalize()`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 30, 2020, 6:50am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/4 "2020-06-30T06:50:00Z")

</div>

How do you make it three lines?

Anyway, I’m afraid I don’t know of such a function.

(I’m a bit skeptical to `normalize` too.)

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 30, 2020, 6:58am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/5 "2020-06-30T06:58:43Z")

</div>

By making `v` the result of some computations (e.g. a matvec `A*x`) which you do not want to repeat.  
For example, `norm_normalize()` would allow you to reduce

```julia
v = A*x
n = norm(v)
v /= n

```

to

```julia
n,v = norm_normalize(A*x)

```

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [June 30, 2020, 7:21am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/7 "2020-06-30T07:21:58Z")

</div>

> [@DNF](#):
>
> (I’m a bit skeptical to `normalize` too.)

Why?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 30, 2020, 7:29am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/9 "2020-06-30T07:29:52Z")

</div>

Seems too trivial to merit it’s own name. It’s just `x./norm(x)`, so I’m used to either writing that, or, if I need to pass it along somewhere, making an anonymous function.

I don’t _mind_ it, it just seems like something that could be trimmed away. I see it has a companion, `normalize!` which does slightly more.

It’s a question, what is the threshold for something to be non-trivial enough or used enough that it is worthwhile. Anyway, `normalize` and this proposed new function won’t live in Base, so no big deal.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [June 30, 2020, 7:35am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/10 "2020-06-30T07:35:07Z")

</div>

If I looked at the [code for \_\_normalize](https://github.com/JuliaLang/julia/blob/44fa15b1502a45eac76c9017af94332d4557b251/stdlib/LinearAlgebra/src/generic.jl#L1597-L1603), there is more to it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 30, 2020, 7:38am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/11 "2020-06-30T07:38:10Z")

</div>

OK, good, then I guess it’s worthwhile.

Since `normalize` / `normalize!` turns out to be a bit more involved than expected, perhaps the proposed function is a bit more useful than I first thought.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 30, 2020, 9:15am UTC](https://discourse.julialang.org/t/function-returning-normalized-vector-and-norm/42285/12 "2020-06-30T09:15:50Z")

</div>

I am on the fence about this; it is occasionally useful but also quite easy to implement when needed, so I would tend to agree with @DNF here. Also,

```julia
a = norm(v)
v /= a

```

is not much more complicated — I don’t really mind naming interim values (`v = A*x`), which can actually make the code cleaner.

As pointed out by @mzaffalon, `normalize!` does have a clever trick to fix a numerical issue. But the solution could be just exposing that, eg with

```julia
a = norm(v, p)
v2 = normalize_with(v, a)

```

etc, as it may be useful in other contexts.
