[Question]
  [
To *normalize* a vector is to scale it to a length of 1 ([a unit vector](https://en.wikipedia.org/wiki/Unit_vector)), whilst keeping the direction consistent.
For example, if we wanted to normalize a vector with 3 components, **u**, we would first find its length:
> 
> **|u| = sqrt(ux2 + uy2 + uz2)**
> 
> 
> 
...and then scale each component by this value to get a length 1 vector.
> 
> **û = u ÷ |u|**
> 
> 
> 
---
# The Challenge
Your task is to write a program or function which, given a non-empty list of signed integers, interprets it as a vector, and normalizes it. This should work for any number of dimensions, for example (test cases rounded to two decimal places):
```
[20]           -> [1]
[-5]           -> [-1]
[-3, 0]        -> [-1, 0]
[5.5, 6, -3.5] -> [0.62, 0.68, -0.40]
[3, 4, -5, -6] -> [0.32, 0.43, -0.54, -0.65]
[0, 0, 5, 0]   -> [0, 0, 1, 0]
```
**Rules:**
* You can assume the input list will:
	+ Have at least one non-zero element
	+ Only contain numbers within your language's standard floating point range
* Your output should be accurate to *at least* **two decimal places**. Returning "infinite precision" fractions / symbolic values is also allowed, if this is how your language internally stores the data.
* Submissions should be either a full program which performs I/O, or a function. Function submissions can either return a new list, or modify the given list in place.
* Builtin vector functions/classes are allowed. Additionally, if your language has a vector type which supports an arbitrary number of dimensions, you can take one of these as input.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") contest, so you should aim to achieve the shortest solution possible (in bytes).
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
### Code:
```
nOt/
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/z79E////aGMdBRMdBV1TIDaLBQA "05AB1E – Try It Online")
### Explanation
```
n     # Square each element of the input
 O    # Sum all elements
  t   # Take the square root of the sum
   /  # Divide each element by the square root of the sum
```
[Answer]
# JavaScript (ES6), 31 bytes
```
a=>a.map(n=>n/Math.hypot(...a))
```
### Test cases
```
let f =
a=>a.map(n=>n/Math.hypot(...a))
console.log(JSON.stringify(f([20]          ))) // -> [1]
console.log(JSON.stringify(f([-5]          ))) // -> [-1]
console.log(JSON.stringify(f([-3, 0]       ))) // -> [-1, 0]
console.log(JSON.stringify(f([5.5, 6, -3.5]))) // -> [0.62, 0.68, -0.40]
console.log(JSON.stringify(f([3, 4, -5, -6]))) // -> [0.32, 0.43, -0.54, -0.65]
console.log(JSON.stringify(f([0, 0, 5, 0]  ))) // -> [0, 0, 1, 0]
```
[Answer]
# Mathematica, 9 bytes
```
Normalize
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b73y@/KDcxJ7Mq9X9AUWZeiUNadLWRQW0sF5yna4rCM9ZRQJE21TPVUTDTUdA11kNRaKxjoqNrqqNrhixoANSso2AKNuI/AA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 8 bytes
```
%+/&.:*:
```
[Try it online!](https://tio.run/##y/r/P83WSkFVW19Nz0rLCshTMDLgSlOINwURxgogtqmeqYIZkKMHEjNWMAFKKsSbAdkGQGiqYAAA)
6 bytes `%|@j./` works [if the vector is at least 2-dimensional](https://codegolf.stackexchange.com/a/149169/74681).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~5~~ 3 bytes
```
÷ÆḊ
```
[Try it online!](https://tio.run/##y0rNyan8///w9sNtD3d0/f//P9pYR8FER0HXFIjNYgE), or see the [test suite](https://tio.run/##y0rNyan8///w9sNtD3d0/T/cfnhZkZGKyqOmNf//RxsZxOooROuagkljHQUw11TPVEfBTEdB11gPLAEUNwHygIK6ZiC@AVCdjoIpSDUA)
Saved 2 bytes thanks to miles!
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 13 bytes
```
@(x)x/norm(x)
```
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en999Bo0KzQj8vvygXyPifphFtrKNgoqOgawrEZrEKmv8B "Octave – Try It Online")
[Answer]
# C, ~~73~~ 70 bytes
*Thanks to @Christoph for saving a byte!*
```
s,i;f(v,n)float*v;{for(s=0;i++<n;)s+=*v**v++;for(;--i;)*--v/=sqrt(s);}
```
[Try it online!](https://tio.run/##bY7NboMwEITvPMUKqZJ/KeHv4vAkUQ6I1pElAil2fEG8eumCox4ge7J35tuZVt7adlmsMEoTL3qqu6FxzKtJDyOxdaoM5@deUctr5hnznKtVUVIaRZmU/rO2P6Mjlqp58YP5AvdtHdnOAPMCTO@gp9EUAU7IUOE9jEBW1WAKGAwBzg3dtHUeI4qaxB9JpiEW/mKuL/LxdJbEMf7mKFov3BvTk/@MLXptcbpcoYYpS@fAbc02QcAJ6Z09C3ZZ7u3ZW3v@sucCDgG4y45EEYgyKQVUAmSeHKIKAfkRLAOIV1GXSMtqD@KyOIJVAFOsKKB8U7QK2Lz8trprbnaR3f0P)
[Answer]
# Python, ~~47~~ 46 bytes
```
lambda v:[e/sum(e*e for e in v)**.5for e in v]
```
[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUaHMKjpVv7g0VyNVK1UhLb9IIVUhM0@hTFNLS88UwY39X1CUmVeikaYRbayjYKKjoGsKxGaxmpr/AQ "Python 3 – Try It Online")
[Answer]
# [Julia](http://julialang.org/), 9 bytes
```
normalize
```
[Try it online!](https://tio.run/##yyrNyUw0@/8/zTYvvyg3MSezKvV/QVFmXklOnkaaRrSRQaymJheSgK4puoCxjgK6IlM9Ux0FMx0FXWM9dOVA1SZACaC8rhmalAHQIB0FU4hx/wE "Julia 0.6 – Try It Online")
[Answer]
## [CJam](https://sourceforge.net/p/cjam), 9 bytes
```
{_:mhzf/}
```
[Try it online!](https://tio.run/##S85KzP1fWBf7vzreKjejKk2/9r@qVcH/aCODWK5oXVMQYawAYpvqmSqYKega64HEjBVMFHRNFXTNgGwDBQMFU6ASAA "CJam – Try It Online")
### Explanation
```
_    e# Duplicate input.
:mh  e# Fold hypothenuse-length over the vector. This gives the norm, unless the vector
     e# has only one component, in which case it just gives that component.
z    e# Abs. For the case of a single negative vector component.
f/   e# Divide each vector component by the norm.
```
[Answer]
# TI-Basic, 6 bytes
```
Ans/√(sum(Ans2
```
Run with `{1,2,3}:prgmNAME`, where `{1,2,3}` is the vector to be normalized.
Divides each element in the vector by the square root of the sum of the squares of its elements.
[Answer]
# [R](https://www.r-project.org/), 23 bytes
```
function(v)v/(v%*%v)^.5
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/T6NMs0xfo0xVS7VMM07P9H@aRrKGoaWOkYGm5n8A "R – Try It Online")
`v%*%v` computes the dot product of v with itself.  
The function will issue a warning for length 2 or greater vectors.
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 57 bytes
```
v->v.stream().map(x->x/v.stream().reduce(0d,Math::hypot))
```
[Try it online!](https://tio.run/##bVHBboMwDL33K6yekg2yah07lA5p0rTTeupx2iGF0IYBiYhBRVO/nQVI1dJNSiL5vWf72cl4w32lRZkl350stKoQMouxGmXO7sLZHyytyxilKv8lDVaCFz2l610uY4hzbgxsuCzhZwZgkKNF312N9Yc0uH5TVisibzskn8MI0peu8aPGFSWUFVyTox8dH66wSiR1LMgi8TYcD6vVodUKKe1C2815cE0bJRMorBNiG8ly//kFvNobOhgDQGGQPC4SGl5CP5iGy8SDqSJggcWe7fWXbKruxU89MSquKevWnqB/B/h0Wc3gchCNe2CMQXP2uG0NioKpGpm2IyB5rSreGoZqHIk01LW5VeYlmYMfwRzu4TYpZVzrvD0X46b/lb6UlQwYodTZPHW/ "Java (OpenJDK 8) – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 5 bytes
```
t2&|/
```
[Try it online!](https://tio.run/##y00syfn/v8RIrUb///9oYx0FEx0FXVMgNosFAA "MATL – Try It Online")
I'm not entirely sure this is the shortest way to do this. First, we duplicate the input, then select the second output type of `|` (which is either `abs`, `norm` or `determinant`). Finally, we divide the input by the norm.
Alternative for 7 bytes:
```
t2^sX^/
```
[Answer]
# [Haskell](https://www.haskell.org/), 29 bytes
```
f x=map(/sqrt(sum$(^2)<$>x))x
```
[Try it online!](https://tio.run/##JYs9D8IgGIT/yjt0gOQFSSsdGsti4ubkSNCQSD9iS2vBpP8eMS733F3uBhtebppS6mBvZ7uSQ3hvkYTPXJB7SU@F2ind02xHDy30Lp4XH52PAZRqIR@uDyDrNvoIHDqahWzOPqFp4BZz3QNToPVlWmw0hiatS8GFQc3kHxX@ILnEGlnFZQ4VHpFJZHX2AgXKPDFf "Haskell – Try It Online")
Or for 1 byte more pointfree: `map=<<flip(/).sqrt.sum.map(^2)`
[Answer]
# [Funky](https://github.com/TehFlaminTaco/Funky), 42 bytes
```
a=>(d=a::map)(c=>c/d(b=>b^2)::reduce@+^.5)
```
[Try it online!](https://tio.run/##SyvNy678n2b7P9HWTiPFNtHKKjexQFMj2dYuWT9FI8nWLinOSNPKqig1pTQ51UE7Ts9U839BUWZeiUaaRrWxjomOrqmOrlmtpuZ/AA "Funky – Try It Online")
[Answer]
# [Ohm v2](https://github.com/nickbclifford/Ohm), 5 bytes
```
D²Σ¬/
```
[Try it online!](https://tio.run/##y8/INfr/3@XQpnOLD63R//8/2lhHwURHQdcUiM1iAQ "Ohm v2 – Try It Online")
[Answer]
# C++ (gcc), 70 bytes
Input by `std::valarray<float>`. Overwrites the original vector.
```
#import<valarray>
int f(std::valarray<float>&a){a/=sqrt((a*a).sum());}
```
[Try it online!](https://tio.run/##bYzLCoMwEEXX5isGS8tM0XbRx8LXvwyxKQFN0hgLRfx2G4XuurhwuY8jncufUi7LTvfO@lC9uWPv@dMIbQIoHEJbFL@wUp3l0ByYJj7Xw8sHRD4ynYaxR6Jyjhgju7F9QCXjU9tGbJyetUESk0j@8aDlwFDDdMngmkF@i7rPpUgUrg2tznrAbQ0aiu1A4HxkK0z3CtIMdNzNyxc "C++ (gcc) – Try It Online")
[Answer]
# Common Lisp, 69 bytes
```
(lambda(v)(mapcar(lambda(x)(/ x(sqrt(loop as y in v sum(* y y)))))v))
```
[Try it online!](https://tio.run/##Ncg7EoAgEATRq0zmrIkkHmjVhCp@glJwetTAzl7vzpY0mLINFzjo1G@Hsgq9pl3zP5pwQWM580UXY4IWdNiAinJ7zi@6fFWRMdHAYIV5PR4)
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), ~~13~~ ~~12~~ 10 bytes
*1 byte saved thanks to @Adám*
*2 bytes saved thanks to @ngn*
```
⊢÷.5*⍨+.×⍨
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24RHXYsOb9cz1XrUu0JDW//wdCCtCZRRMDLgSlM4tN4UTBorgHimeqYKZiCeHkjUWMEEJA/EZgA)
**How?**
```
⊢  ÷  .5*⍨  +.  ×⍨
u  ÷    √   Σ   u²
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 4 bytes
```
²∑√/
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyLhuIsiLCIiLCLCsuKIkeKImi8iLCIiLCJbNS41LCA2LCAtMy41XSJd)
```
²    # Square each
 ∑   # Sum
  √  # Square root
   / # Divide by that
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~8~~ 7 bytes
```
´(Ṁ/√ṁ□
```
[Try it online!](https://tio.run/##yygtzv5/aFtusM6jpsb/h7ZoPNzZoP@oY9bDnY2Ppi38//9/dLSRQaxOtK4piDDWAbFN9Ux1zHR0jfVAYsY6Jjq6pjq6ZkC2gY6BjilQSSwA "Husk – Try It Online")
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 51+64=115 bytes
```
v=>v.Select(d=>d/Math.Sqrt(v.Select(x=>x*x).Sum()))
```
[Try it online!](https://tio.run/##pZJNT@MwEIbPya8YbjYk3tIPLmkiIQSrlQAhFbGH1R7SZAqWXHvxRyiq@tvLJCUVVL3tJda88jvP65lULq2MxW1wUj/D7N15XGbx10pcGaWw8tJoJ36iRiurgxu3Ur9mcaVK5@BhHTtfelnBTdDV9FY6P61NmCssEvh1rcMSbUlVLxZwb@yyVNLhE1GMhRy2TV40YoYtltV5Uf@4K/2LmL1az/b6Ki9WpysuZmHJOOfbLO7BjZE13JVSMx6v4@iKghuF4reVHikqsid6w@L9gMs0vsHXuLCGIWwSOKKfw4bz7H9aj5Jxkk6S9OI4YCBGw4S@41EC6UBMxt1xMdlxN/unzo1RcJz5rWPTacl3DK7@kYx1N6WmtGDRBeVpAYe9dnYuHk3bgFGEOJILYDsD/SFBkwon@b5nr/E4iiz6YDUsSuWwdS5oyUxqD5JQg4yO6WfA3kXa2RlZKVfH6dZ/OXefwD/yL6R7FFUcCjjHdNjSDnARDauXvA3YTm@z/QA "C# (.NET Core) – Try It Online")
+64 bytes for the `using System;using System.Collections.Generic;using System.Linq;`
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 94+13=107 bytes
```
v=>{var m=0d;foreach(var x in v)m+=x*x;for(int i=0;i<v.Length;)v[i++]/=Math.Sqrt(m);return v;}
```
[Try it online!](https://tio.run/##lVFNT4NAED3DrxhvYAHpl5ctTYyJXtSYNKmHpgcKU7vJsqu7C7Zp@tvrQKGaxosXlje7896bN5kJM6XxWBou32G2MxYL5v5G0b0SAjPLlTTRI0rUPLt48cTlJ3MzkRoDr3vX2NTyDB5KmU1yVa4ELpYBdH9TeFG6SAU3OCdapSGBY5VM91WqoUjinK3JUJptvLqwBS6h8otesr3e1jcelxZ4EjM@qaInlO92w/xqwXu95U3ynNpNNPvU1it8ptGWmprZ4cjczlSleA7PKZee7@5d556GUgKjN80t0hjozWm@9e7Coifxa7GEPQxyOATQoT4h32f/phnmwSgIx0F4@4ssjoaDgL6jYQBhHI1HzXE7PkkczhOslBLwN30XMVQN/skccPtBJcyboetcNZpSWIr@kuPUSoquw9fgnd61QcNVcmZqS77rOG3Q61QYrPtoS3BaE/HHjI5J66hbGNC6qJO8NCLN2u5WplVb8CWEZyFCPkyhj@GgFrtQcyiYrmR1iXVSh@M3 "C# (.NET Core) – Try It Online")
+13 bytes for `using System;`
The non-Linq approach
### DeGolfed
```
v=>{
    var m=0d;
    foreach (var x in v)
        m+=x*x;
    for (int i=0; i < v.Length;)
        v[i++] /= Math.Sqrt(m);
    return v;
}
```
[Answer]
# [Perl 5](https://www.perl.org/), 45 + 1 (`-a`) = 46 bytes
```
say$_/sqrt eval join'+',map"($_)**2",@F for@F
```
[Try it online!](https://tio.run/##K0gtyjH9/784sVIlXr@4sKhEIbUsMUchKz8zT11bXSc3sUBJQyVeU0vLSEnHwU0hLb/Iwe3/f2MFEwVdUwVds3/5BSWZ@XnF/3V9TfUMDA3@6yYCAA "Perl 5 – Try It Online")
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 10 bytes
9 bytes of code, +1 for `-p` flag.
```
g/RT$+g*g
```
Takes the vector as separate command-line arguments. [Try it online!](https://tio.run/##K8gs@P8/XT8oREU7XSv9////ugX/TfVM/5v91zXWMwUA "Pip – Try It Online")
### How it works
```
      g*g  Arglist, multiplied by itself itemwise
    $+     Sum
  RT       Square root
g/         Divide arglist itemwise by that scalar
           Result is autoprinted (-p flag to format as list)
```
[Answer]
# Pyth, 5 bytes
```
cR.aQ
```
Try it online: [Test Suite](https://pyth.herokuapp.com/?code=cR.aQ&test_suite=1&test_suite_input=%5B20%5D%0A%5B-5%5D%0A%5B-3%2C%200%5D%0A%5B5.5%2C%206%2C%20-3.5%5D%0A%5B3%2C%204%2C%20-5%2C%20-6%5D%0A%5B0%2C%200%2C%205%2C%200%5D&debug=0)
### Explanation:
```
cR.aQQ   implicit Q at the end
c        divide
 R   Q   each element of the input
  .aQ    by the L2 norm of the input vector
```
[Answer]
# [Perl 6](http://perl6.org), 25 bytes
```
{$_ »/»sqrt sum $_»²}
```
[Try it online!](https://tio.run/##HYo7DsIwFASvMooiKhwszHOTz1UiClwRATYUUZRL0brzxYxxs9LM7PPm7zYvKwfHmLd2JsVTiuHl34TPQjunmL577gnXlab0cWJzxe8N7uEZzno6Miipa6gknWBRpqvWcEEJyv5Bo5Hy6vMP "Perl 6 – Try It Online")
`$_`, the list argument to the function, is divided elementwise (`»/»`) by the square root of the sum of the squares of the elements (`»²`).
[Answer]
# Ruby, ~~39~~ 35 bytes
```
->v{v.map{|x|x/v.sum{|x|x*x}**0.5}}
```
-4 bytes thanks to G B.
[Answer]
# [Coconut](http://coconut-lang.org/), 40 bytes
```
x->map((/)$(?,sum(map(t->t*t,x))**.5),x)
```
I have no idea what I'm doing...
[Try it online!](https://tio.run/##HYqxCsMgGITn9ilu6KDya0NTMwRqHyTtUAKC0GhIDDj03a263N13d3OYgz9itnjglZM0y2dl7Mov7En7sbCKUZooIiXOhVCal5Bt2JDgPKZb9yZMUjftCQ210oSBIHvVhtLfC5VSDpW78iPo@h7Pp4Sfga3ydXusvm7Ox/wH "Coconut – Try It Online")
[Answer]
# [Thunno](https://github.com/Thunno/Thunno) `D`, \$ 5 \log\_{256}(96) \approx \$ 4.12 bytes
```
2^St/
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_abSSi1LsgqWlJWm6FkuN4oJL9CFsqNCCddHGOgomOgq6pkBsFgsRBQA)
#### Explanation
```
2^St/  # D flag duplicates the input
2^     # Square each
  S    # Sum this list
   t   # Push the square root
    /  # Divide the input by this number
```
]