[Question]
  [
The [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity) of two vectors \$A\$ and \$B\$ is defined using their [dot product](https://en.wikipedia.org/wiki/Dot_product) and [magnitude](https://en.wikipedia.org/wiki/Magnitude_(mathematics)#Euclidean_vector_space) as:
\$\frac{A\cdot B}{\|A\|\|B\|}\$
Or in other terms
\$\frac{\sum\_{i=1}^nA\_iB\_i}{\sqrt{\sum\_{i=1}^nA\_i^2}\sqrt{\sum\_{i=1}^nB\_i^2}}\$
# Challenge
Given two nonempty, nonzero vectors containing only integers (can be taken as lists) calculate their cosine similarity.
* If their lengths are not equal, then the shorter vector should be padded on the right to length of the longer vector with zeroes.
Input and output may be done via any [reasonable method](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods).
Test cases below.
```
[1,2,3], [4,5,6] => ~0.974
[9, 62, 0, 3], [25, 3, 6] => ~0.25
[-7, 4, 9, 8], [16, 1, -2, 5] => ~-0.35
[82, -9], [7, 52, 3] => ~0.024
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest answer wins.
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
`*OInOtP/
```
Input as a pair of lists.
[Try it online](https://tio.run/##yy9OTMpM/f8/QcvfM8@/JED////oaEsdMyMdAx3jWJ1oI1MdYx2z2FgA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeX/BC3/yjz/kgD9/zr/o6OjDXWMdIxjdaJNdEx1zGJjdRSioy11zIx0DMCiRqY6xjBhXXMdEx1LHQugsKGZjqGOrpGOKUTGwkhH1xIobK5jCjIsNhYA).
**Explanation:**
```
`          # Push both lists of the (implicit) input-pair separately to the stack
 *         # Multiply the values at the same positions in the lists together
           # (discarding trailing items of unequal length input-lists)
  O        # Sum all these values together
   I       # Push the input-pair of lists again
    n      # Square each inner value
     O     # Sum the two inner lists
      t    # Take the square root of both sums
       P   # Take the product of this pair
        /  # Divide the earlier dot-product by this
           # (after which the result is output implicitly)
```
Here a minor equal-bytes alternative: `øPInOtP/O`. I haven't been able to find anything shorter unfortunately, but there are a lot of approaches for this at 1 or 2 bytes longer: [try it online](https://tio.run/##yy9OTMpM/f8/QcvfM8@/JEBfAQh0uA7vCIBw/SF8qHRZJUi@FsSHcv3h/LJKoAiC718MEYDwgSzPBK2jC/UO7QYpgQgALfGHCIGtCA1LUIABpeLUktIChbT8IoWS8nyFnPz84lSFzLyC0hLdnMzikmIgu7gkNTFFIT9NIVGhIDGzSCmGS8s/MuJRwyyYvbU6//9HR1vqmBnpGOgYx@pEG5nqGOuYxcYCAA).
[Answer]
# [Desmos](https://desmos.com/calculator), 42 bytes
This non-padding version miraculously works out:
```
f(a,b)=total(ab)/(a^2.totalb^2.total)^{.5}
```
[Try It On Desmos!](https://www.desmos.com/calculator/7e69ztsfva)
[Try It On Desmos! - Prettified](https://www.desmos.com/calculator/pb7kqinlej)
I originally had constructed an answer that was 123 bytes (around 3 times longer than with no padding!!!) long that did actually do the padding, but then I realized, to my complete surprise, that the test case outputs were matching on both my padding and no-padding answers (which is this 42 byte answer), even for test cases that I made up myself. This led me to dig a bit deeper into why this was the case, resulting in the explanation below.
### Explanation
The question essentially asks to pad the shorter list with zeros at the end to make both lists the same, and then multiply each element of the lists together and sum them, then divide it by the square root of the product of the sums of the elements of each of the two lists squared.
Now let's say that the unpadded, original input lists are `a=[a_1,a_2,...,a_A]` and `b=[b_1,b_2,...,b_B]`, where the capitalized version of the list represents the length of the list. WLOG, assume that `A<B`. Now let `c` and `d` be the padded versions of `a` and `b` respectively, meaning `c=[a_1,a_2,...,a_A,0,...,0]`, where there are `B-A` zeros, and `d=[b_1,b_2,...,b_B]`, which is `b` unchanged.
Now to explain my non-padding answer first:
In Desmos, every time an operation is applied to two lists, it will almost always clip the resulting list to the length of the shorter list out of the two lists. This includes multiplication (implicitly vectorized), which is used in this answer.
For the numerator of the formula given in the question, it is essentially equivalent to using vectorized multiplication on the two lists (which will multiply the lists element-by-element), and then taking the total sum of that vectorized product. We know that vectorized multiplication will clip the lists to the length of the shorter lists, which means that when we take the product `a*b`, we will get the result `[a_1 * b_1, a_2 * b_2,...,a_A * b_A]`, where it is clipped off at the shorter list `a`. Taking the total of this would give `a_1 * b_1 + a_2 * b_2 + ... + a_A * b_A`.
Wait! Now let's take a look at what happens when we take the product `c*d` and sum that list instead. Recall that `c=[a_1,a_2,...,a_A,0,...,0]` and `d=[b_1,b_2,...,b_B]`, so `c*d` would be `[a_1 * b_1, a_2 * b_2,...,a_A * b_A, 0 * b_(A+1),...,0 * b_B]`. Taking the sum of all those terms gives `a_1 * b_1 + a_2 * b_2 + ... + a_A * b_A`, due to all the zeros vanishing from the sum. But this is the exact same sum that we got without padding!!! This indicates that padding at least does not affect the final result of the numerator of the formula, so it is fine to take the sum without padding the two lists beforehand, as it will result in the same answer as with padding anyways.
Now let's consider what is happening in the denominator. In my answer with no padding, I am simply just multiplying each of the lists by themselves (i.e. squaring them), and then summing the resulting elements. So basically, we have that `a^2 = [(a_1)^2, (a_2)^2,...,(a_A)^2]` and `b^2 = [(b_1)^2, (b_2)^2,...,(b_B)^2]`, which means that their sums are `(a_1)^2 + (a_2)^2 + ... + (a_A)^2` and `(b_1)^2 + (b_2)^2 + ... + (b_B)^2` respectively.
But now consider the sums when there is padding involved. We then have that `c^2 = [(a_1)^2, (a_2)^2,...,(a_A)^2, 0^2,...,0^2]`, and `d^2` would be the same as `b^2` because as stated earlier, `d` is the same as `b` unchanged. So, the sum of `d^2` would obviously match that of `b^2`. But what about the sum of `c^2`? We have that the sum of `c^2` is `(a_1)^2 + (a_2)^2 + ... + (a_A)^2 + 0^2 + ... + 0^2`, but because all the zeros just vanish, the sum simplifies down to `(a_1)^2 + (a_2)^2 + ... + (a_A)^2`. Comparing this to the sum that we got with no padding, we can see that they are exactly the same!!!
Obviously, we have assumed that `A<B` at the very start of our proof. But what happens if `B>A`? Then simply just switch all the `a`'s and `b`'s, `A` and `B`'s, and also `c` and `d` and you will have the proof for the `B>A` case.
Lastly, what happens if `A=B`; in other words, what happens if they are both the same length? The clipping will shorten the longer list into the same length of the shorter list, but because they are the same length, this will not affect either of the lists. Similarly, padding will lengthen the shorter length to match the length of the longer list, but again, because the two lists are the exact same length, this won't affect either of the lists. Because both clipping and padding preserve the original lists, both of them will obviously result in the same calculations and therefore the same outputs as well.
Thus, by proving that the sums in both the numerator and denominator are the same with both padding (which is what the question is asking for) and with no padding (which is what is happening in my answer), my answer is valid, even though I am not actually doing any padding like the question asks for.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~60~~ ~~31~~ 27 bytes
```
Dot@@Normalize/@PadRight@#&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b73yW/xMHBL78oNzEnsypV3yEgMSUoMz2jxEFZ7X9AUWZeiUOaQ3W1hZGOgq5lrU61uY6CKZBtXFv7HwA "Wolfram Language (Mathematica) – Try It Online")
Many thanks to @att and @
polfosol ఠ\_ఠ ! They should have posted their answers (-\_-)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
ÆḊ€÷ƒḋ/
```
A monadic Link that accepts the pair of vectors and yields their cosine similarity.
**[Try it online!](https://tio.run/##y0rNyan8//9w28MdXY@a1hzefmzSwx3d@v@PTnq4c4bO4fasRw1zFGztFB41zNWM/P8/OjraUMdIxzhWRyHaRMdUxyw2VodLITraUkfBzEhHwUBHASxlZApkAIWgsrrmOgomOgpARRYgWUMzHQVDHQVdoAZTqAoLIFvXEiQJVGpqBDIGKBELAA "Jelly – Try It Online")**
### How?
```
ÆḊ€÷ƒḋ/ - Link: pair of lists, V
  €     - for each vector:
ÆḊ      -   get its norm
      / - reduce V by:
     ḋ  -   dot-product
    ƒ   - starting with the dot-product reduce the norms by:
   ÷    -   division
```
---
Here's a dyadic version, also at seven bytes:
```
,ÆḊ€÷ƒḋ
```
[Try it online!](https://tio.run/##y0rNyan8/1/ncNvDHV2PmtYc3n5s0sMd3f@PTnq4c4bO4eX6WY8a5ijY2ik8apirGfn/f3R0tKGOkY5xrI5CtImOqY5ZbKwOl0J0tKWOgpmRjoKBjgJYysgUyAAKQWV1zXUUTHQUgIosQLKGZjoKhjoKukANplAVFkC2riVIEqjU1AhkDFAiFgA)
[Answer]
# MATLAB, 37 32 bytes
-5 bytes thanks to [@loopywalt](https://codegolf.stackexchange.com/questions/259755/cosine-similarity-of-two-vectors/259849#comment572456_259849)
```
@(a,b)sum(diag(a'*b))/norm(a'*b)
```
Instead of padding `a` or `b` to the length of the other, or cropping it, which should be equivalent in the numerator, we take the outer product of the two vectors, then sum the diagonal (ie trace). This is equivalent to the dot product of the two vectors cropped to the shortest length.
Unfortunately, this doesn't work if the two vectors have different length, `trace` requires a square matrix:
```
@(a,b)trace(a'*b)/norm(a'*b)
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `s`, 7 bytes
```
*□²Ṡ√Π/
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJB4biLcyIsIiIsIirilqHCsuG5oOKIms6gLyIsIiIsIlsxLDIsM10sIFs0LDUsNl1cbls5LCA2MiwgMCwgM10sIFsyNSwgMywgNl1cblstNywgNCwgOSwgOF0sIFsxNiwgMSwgLTIsIDVdXG5bODIsIC05XSwgWzcsIDUyLCAzXSJd)
Link includes test cases. `ḋ` flag prints numbers as decimals (for convenience) rather than as rationals, but is not needed.
*-1 thanks to @lyxal*
#### Explanation
```
*□²Ṡ√Π/  # Implicit input
*        # Vectorised multiplication
 □²      # Input list squared
   Ṡ√    # Sum and square root
     Π   # Product
      /  # Divide
         # s flag sums the list
         # Implicit output
```
Old:
```
Þ•2(?²∑√)*/  # Implicit input
Þ•           # Dot product: seems to handle the padding with 0s
  2(    )    # Repeat twice:
    ?        #  Next input
     ²       #  Squared
      ∑      #  Summed
       √     #  Square rooted
         *   # Multiply both
          /  # Divide
             # Implicit output
```
[Answer]
# [Python](https://www.python.org) NumPy, 50 bytes
```
lambda a,b:(z:=a*b[:,1>0]).trace()/(z*z).sum()**.5
```
[Attempt This Online!](https://ato.pxeger.com/run?1=LY5BboMwEEX3nGKWNhpocDAFpPQijhWZtqhIxVjGLOAq3SBFiXql3iZ23NW8mXnzNT93s7qvSe_X_nS-La7P6j_2rcbuQ4HCriVbe1JpJ1os3g6S5s6q909CX8iWbjSfl5HQNM35_-XvMJrJOtDLaFZQM2iTJP1kQxQMGoQQBTI8SgRRIsdKBhINQsUQDgjPDeMe_Cgus1eEEsE7deiLCqFAyLzPo1B7zJqA3uQshEjZJgDGDtqRnmiT24tQEiN0ktL4777H-gA)
[Answer]
# [R](https://www.r-project.org), 40 bytes
```
\(x,y)sum(diag(z<-x%*%t(y)))/norm(z,"f")
```
[Attempt This Online!](https://ato.pxeger.com/run?1=Xc_NCoJAEAfwe08xGMJszJqurh-QPUkQYRkeVPAD1EMv0sUOnXqNXqK3adPI6jTL8P_9hz1fiv4W5WWSHbZlkobXuoq5_8ANNtSysk5xn-yO2K14oy_0ClvG2DLLixQ70mKNvfP3qQIjtEiQzQgidEiSyxjMIVzDyTQCz5n9RAMCVxCYBCMQUr3U7ssI-Uu4R-AQKOkPxHIJLAKuauSHcdOw_5yvAjwYiGqQ4nVyumIKZ_xL34_zCQ)
R port of [Cris Luengo's MATLAB answer](https://codegolf.stackexchange.com/a/259849/67312).
# [R](https://www.r-project.org), 48 bytes
```
\(x,y)(x%*%x*y%*%y)^-.5*(x*y)%*%!seq(!x)-seq(!y)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=Xc87DsIwDAbgnVO4QpXiyqnSkPQxlJMgGKoidQAEBSlduAhLGZi4BpfgNpgWUcoSx5G_38rlemjvxa6utuWqrjb57XRcy_SpFsJRg8L5ge-Chs8GlzK0geAOufXqci88h7KrDX7cY4gShYhI0wwJCmHIUowIU8jncFZhlpjJaDQjiDWBIuiBtnzjtx-j7ZjIhMAQsEw7EsUEEYHkGPtlUoWzP5fygMw6wglWv1cOW5Q2_V_atq8v)
A slight rearrangement and golf of [Dominic van Essen's answer](https://codegolf.stackexchange.com/a/259770/67312), posted at his request.
Works by maximizing operator precedence and vector recycling.
[Answer]
# JavaScript (ES6), 76 bytes
Expects `(a)(b)`.
```
a=>b=>[...a+b].reduce(t=>t+~~a[i]*~~b[i++]/h(...a)/h(...b),i=0,h=Math.hypot)
```
[Try it online!](https://tio.run/##Tc5NbsMgEAXgvU8xS6gHTPyTxKrwrsueALEAxw6uohA5tFI3vrqLa9cqGyT43pv5MF/m2Y7DI7C7v3RzL2cjGysbxTk3qdV87C6fbUeCbEI6TUYN@mWarBrSVGeOLIqut6U4SIFOvpvguPt@@EDnV5UAKFAHhByh0AiqRKgQjhr2E1@zDCbB61O58jqC6MUWyWOg2DM7z6tVsxNCbI2h86IPR4Q4j8WCSv9pJnix8XP8YPUiY676XevfLrCVi7xMdMJ7P76Z1hGiDILVFGQDrb8//a3jN38lPTGUWErp/AM "JavaScript (Node.js) – Try It Online")
### Commented
```
a =>             // outer function taking the 1st vector a[]
b =>             // inner function taking the 2nd vector b[]
[...a + b]       // build a large enough array to make sure that we
                 // iterate on all values of the largest vector
.reduce(t =>     // using t as an accumulator, for each entry in there:
  t +            //   add to t:
    ~~a[i]       //     either a[i] or 0
    * ~~b[i++]   //     multiplied by either b[i] or 0 (then increment i)
    / h(...a)    //     divided by hypot(a)
    / h(...b),   //     divided by hypot(b)
  i = 0,         //   start with t = i = 0
  h = Math.hypot //   h is an alias for Math.hypot
)                // end of reduce()
```
[Answer]
# [R](https://www.r-project.org), 52 bytes
*Edit: +2 bytes to fix when either vector is length-1, thanks to Giuseppe*
```
\(x,y,`?`=sum)?x*y*(seq(!x)==seq(!y))/(?x^2*?y^2)^.5
```
[Attempt This Online!](https://ato.pxeger.com/run?1=Xc9LDoIwEAbgvacY46YlU4TS8ljUXsQgCcGEBRpFErrxIm5w4cpreAlvYwUj4momk_n-yVyux-6e7-tyV2zqslK35rRl8VOsSYsGM52puqmobh3jkLo4kHlLleobQ-mS6DbljjYpp6krP_YxxpGc-MgxoAg5ESgxpBQWoFZw9twkErPJaoIQcgQPYQBc2s7OfgyXU8IiBIFgZdwTP0TwEZiNkV_GPDf4c7FdYElPbILk75PjFY-L4ZeuG-oL)
[Answer]
# TI-BASIC (TI-84), ~~43~~ 41 bytes
Padding is expensive.
```
:Prompt A,B
:max(dim(ʟA),dim(ʟB→dim(ʟA
:Ans→dim(ʟB
:sum(ʟAʟB/√(sum(ʟB²)sum(ʟA²
```
Example usage:
```
prgmCOSSIM
A=?{9,62,0,3}
B=?{25,3,6}
              .2531554274
```
For comparison:
## TI-BASIC (TI-84), no padding, ~~19~~ 17 bytes
Errors when the inputs aren't equal length. Takes the first input via `Ans`.
```
:Prompt A
:sum(ʟAAns/√(sum(Ans²)sum(ʟA²
```
Example usage:
```
{1,2,3}:prgmCOSSIM
A=?{4,5,6}
               .974631462
```
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), 17 bytes [SBCS](https://github.com/abrudz/SBCS)
```
↓∘↑+.×.÷.5*⍨1⊥¨×⍨
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=AwA&c=e9Q2@VHHjEdtE7X1Dk/XO7xdz1TrUe8Kw0ddSw@tODwdyAQA&f=LcyxEcAgDATB3FVcCJklkEAN0R49UBmYcfj7MzdIglIyqWJ4fgYpcOW9qEb5dc1GJeiHxRHWVOxeXc@I4w37Yhs&i=AwA&r=tryapl&l=apl-dyalog&m=train&n=f)
Lots of dots.. .
Input an array containing the two vectors.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 20 bytes
```
z₀×ᵐ+D&{^₂ᵐ+√}ᵐ×;D↔/
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/v@pRU8Ph6Q@3TtB2UauOe9TUBGI@6phVC6QPT7d2edQ2Rf///@hoCyMdhXjLWB2FaHMdBVMgxzg29n8EAA "Brachylog – Try It Online")
### Explanation
```
z₀×ᵐ+D&{^₂ᵐ+√}ᵐ×;D↔/
                      Input is a list of two lists representing the two vectors
z₀                    Zip the two vectors, clipping to the shorter length
  ×ᵐ                  Multiply each pair of corresponding values
    +                 Sum the products
     D                Save this value as D (for Dot product)
      &               Start over with the original list of lists
       {     }ᵐ       Do this to each vector:
        ^₂ᵐ             Square each of its elements
           +            Sum the squares
            √           Get the square root of the sum
               ×      Multiply the two results
                ;D    Pair with D from earlier
                  ↔   Reverse the list, putting D first
                   /  Divide
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), ~~13~~ 11 bytes
*-2 bytes thanks to FryAmTheEggman*
```
cF+s*MCQ.aM
```
[Try it online!](https://tio.run/##K6gsyfj/P9lNu1jL1zlQL9H3///oaAsjHQVdy1gdhWhzHQVTIMc4NhYA "Pyth – Try It Online")
### Explanation
```
cF+s*MCQ.aMQ    # implicitly add Q
                # implicitly assign Q = eval(input())
      CQ        # transpose the input, truncating to the shortest element
    *M          # map each element to its product
   s            # sum the elements
  +             # prepend this to
        .aMQ    # the input elements mapped to their norms
cF              # fold this over division
```
[Answer]
# [Pip](https://github.com/dloscutoff/pip) `-x`, 17 bytes
```
$*_MS Zg/RT$+*SQg
```
Takes the vectors as two command-line arguments, each one formatted as a Pip list. [Attempt This Online!](https://ato.pxeger.com/run?1=m724ILNgWbSSboVS7IKlpSVpuhYbVbTifYMVotL1g0JUtLWCA9Mh4lDp7dFK0RZG1rqWsUo6CkrR5tamRtbGsXDdAA)
### Explanation
We use the equivalent formula \$\sum\_{i=1}^n\frac{A\_i}{\sqrt{\sum\_{i=1}^nA\_i^2}}\frac{B\_i}{\sqrt{\sum\_{i=1}^nB\_i^2}}\$:
```
$*_MS Zg/RT$+*SQg
                   ; g is list of command-line args, evaluated (-x flag)
              SQg  ; Square each value in the list of two vectors
           $+*     ; Sum each squared vector
         RT        ; Take the square root of each sum
       g/          ; Divide the list of two vectors by that list of two magnitudes
      Z            ; Zip (transpose) the list of two vectors, pairing up corresponding
                   ; elements and trimming the longer vector to the length of the
                   ; shorter one (which, at the dot product stage, is equivalent to
                   ; right-padding the shorter one with zeros)
   MS              ; Map this function to each pair of values and sum the results:
$*_                ;   Take the product of the values
```
[Answer]
# [Python](https://www.python.org), 73 bytes
*-95 bytes thanks to `Neil`, -3 bytes thanks to `att` and -3 bytes thanks to `xnor`.*
This works despite the `zip` function stopping at the shorter sequence, since we right pad with `0`. Essentially, the elements of the longer sequence do not affect the calculation.
```
lambda a,b:d(a,b)/(d(a,a)*d(b,b))**.5;d=lambda*a:sum(map(int.__mul__,*a))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=LY9BDoJADEX3nqLLmUlBQQYBwwE8w0jIEEIkYYAgLLyAl3BDYvRO3sYOsGn_b19-2te3f4y3rp3fVXr9TGPlRL9Lo01RatBYJCWjyvfMds1FyQqyXAhXnst05YRO7pNhRvesbkc3z83U5DkKzfkW-Ky6waZB3YJSykMfjxmCClBimFmlYoTQRzggLBtfkqDRunROCAECMZH1XojgITjEyxWISDqxlURK34ZkWbID6Ac6iVXLE9s187z2Pw)
*P.S. I edited out the other versions of this answer made with `scipy`, `numpy` and `scikit-learn`, because they were significantly longer.*
[Answer]
# [Julia 1.0](http://julialang.org/), ~~68~~ ~~60~~ ~~44~~ 43 bytes
```
x^y=sum(prod,zip(x,y))
a\b=a^b/√(a^a*b^b)
```
[Try it online!](https://tio.run/##hc9LCoMwEAbgvaeYZSyJjbG@FkLv4QMS2oXFohiF2BP0DD1eL2InKRSE0mYRkmH45p/L3LUyNOtqmqXQ85UMY3@it3Yghi6@78lKFbJR@@f9QWQjd6pR/nqUWp/HCVotB@w3BEoIKYCgENWAp4LygH@I8Upq@wLgQZ5iUU59V/CAh773jckpJMhwJyEjLBFtGFf6zbAUW3AYapljwgQLGJGhHaPEeBD9ZzLsZrnb6L2UZWNhE33ScLFZan0B "Julia 1.0 – Try It Online")
* The power operator `^` is redefined for the intermediate function. This adds a few bytes where a square or square root is needed, but its high precedence in order of operations cuts down on parentheses used.
	+ [Documentation for operators](https://docs.julialang.org/en/v1/manual/mathematical-operations/)
	+ [Source code with all supported operators](https://github.com/JuliaLang/julia/blob/def2ddacc9a9b064d06c24d12885427fb0502465/src/julia-parser.scm)
* `zip` is used to accommodate vectors with different lengths.
	+ Otherwise, `x^y=sum(x.*y)` could be used.
Alternatively, [`LSHFunctions.cossim`](https://docs.juliahub.com/LSHFunctions/nLuMy/0.1.2/similarities/cosine/) could be adapted for this challenge.
* -8 bytes thanks to Giuseppe: rewrite with an improved formula
* -16 bytes (!!) thanks to Giuseppe: `sum` supports a calling function
* -1 byte thanks to MarcMush: replace `sqrt` with `√`
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~110~~ 61 bytes
saved 49 bytes thanks to the comment
```
@(A,B)A(1:(p=min(numel(A),numel(B))))*B(1:p)'/norm(A)/norm(B)
```
[Try it online!](https://tio.run/##XY9dC4IwFIbv/RW7a4tpbVPTQmjnb0QXEgmCX5gF/Xp754So3WznnOd5D@tvU/m6zxUrWBRF85lbScJydeRD0dYd757tveFWSP8ggbMlzAex2XX92GLmbxKzVYi5KMm0ZOZ6CmipY8kSyVLUj7qtm3Ksp7cbVNwCJSWCwGoH5qBg7ld56Wmo5t/W3gZL2tnGkeFBMqxCSObspadSybAjBJn8JBifgGQyLiF2dAYszJ28lMhL1o98xdiLWESxCOYP)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 9 bytes
```
Þ•∇"²Ṡ√Π/
```
Edit: Thunno ninja'd me but kinda want to leave my answer here as it (was) shorter
[Try it Online!](https://vyxal.pythonanywhere.com/#WyLhuIsiLCIiLCLDnuKAouKIh1wiwrLhuaDiiJrOoC8iLCIiLCJbOSwgNjIsIDAsIDNdXG5bMjUsIDMsIDZdIl0=)
**How it works:**
```
Þ•∇"²Ṡ√Π/
Þ•          Calculate the dot product
  ∇         Shift stack
   "        Pair the vectors
    ²Ṡ√     Square, take vectorised sum and square root
       Π/   Push product and divide
```
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), 20 bytes
```
Đ←Đ04Ș*Ʃ⇹²Ʃ↔+²Ʃ⇹↔*√/
```
[Try it online!](https://tio.run/##K6gs@f//yIRHbROOTDAwOTFD69jKR@07D20CUm1TtMF0@04gU@tRxyz9//@jdc11THQsdSxiuaINzXQMdXSNdExjAQ "Pyt – Try It Online")
```
Đ←Đ04Ș                       Gets stack set up for calculations
      *Ʃ                     A·B
        ⇹²Ʃ↔+²Ʃ⇹↔*√          ||A||*||B||
                   /         divide; implicit print
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~16~~ 14 bytes
```
Ｉ∕Σ×θη₂ΠＥ²ΣＸＡ²
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iOSOxKDk_MWfBgqWlJWm6FjejA4oy80o0nBOLSzRcMssyU1I1gktzNUIyc1OLNQp1FDI0NXUUggtLE4tSg_LzSzQCivJTSpNLNHwTCzSMgDJAtQH55alFGp55BaUlGkDFRppQYL2kOCm5GGrR8mgl3bIcpdjd0dGWOgpmQK0GOgrGsToK0UamQAZQKDYWohIA) Link is to verbose version of code. Explanation:
```
    θ           First input
   ×            Vectorised multiply by
     η          Second input
  Σ             Take the sum
 ∕              Divided by
         ²      Literal integer `2`
        Ｅ       Map over implicit range
            Ａ   Next input
           Ｘ    Vectorised raise to power
             ²  Literal integer `2`
          Σ     Take the sum
       Π        Take the product
      ₂         Take the square root
Ｉ               Cast to string
                Implicitly print
```
[Answer]
# [Factor](https://factorcode.org) + `math.similarity`, 17 bytes
```
cosine-similarity
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70sLTG5JL9owU2r0GBPP3crhYKi1JKSyoKizLwShdzEkgy94szczJzEosySSgVrLq5qBQsjBV1LhVqFagVzBVMjBWOF2qWlJWm6FhuT84sz81J1Eeoh4gv1IPSCBRAaAA)
[Answer]
# [Nibbles](http://golfscript.com/nibbles/index.html), 14.5 bytes (29 nibbles)
```
^/^+*100!$_*~*;~$+.$^$~$_-2
```
Nibbles only handles integers, and perhaps isn't the ideal language to calculate a value that will always lie in the range between `-1` and `1`.
So this program outputs the cosine similarity multiplied by 100: the extra code for this costs +3.5 bytes - without it, the program would be `/+!$_*^*;~$+.$^$~$_-2` (11 bytes), but can only output to an accuracy of zero decimal places, so either `1`, `0` or `-1`.
```
^/^+*100!$_*~*;~$+.$^$~$_-2
                                # First get the dot-product multiplied by 100:
        !                       #   zip together
         $_                     #   arg1 and arg2
           *                    #   by multiplication
    *100                        #   multiply each element by 100
   +                            #   and get the sum
  ^         ~                   # and then square the dot-product;
                                # Now get the squares of the product of the magnitudes:
              ;~$               #   apply to arg1 and save as function:
                  .$            #     map over each element:
                    ^$~         #       square
                 +              #     and sum the squared elements
                       $_       #   apply the same function to arg2
             *                  #   and multiply the two results together;
 /                              # Now divide the squared 100x dot-product
                                # by the squares of the product of the magnitudes
^                        -2     # and return the square-root of the result
```
[![enter image description here](https://i.stack.imgur.com/0esoD.png)](https://i.stack.imgur.com/0esoD.png)
[Answer]
# [Raku](https://raku.org/), 45 bytes
```
->\a,\b{sum(a Z*b)/sqrt sum(a Z*a)*sum b Z*b}
```
[Try it online!](https://tio.run/##NcxBCoMwFATQqwwixUhCTTRRQXOQVpEIzUppq@1CrGdPDeLqz7yB/3pMg3LjgotF7ZhuDG36df6OkcEt7sl1fk8fnN2QeI/o/bS52SwIwu6etBT74S1qjdXiF3ZbAPucEFUcAqmmqDJIKE3obiWUQHKwkEhPZzkylCi8cwUOJiCPqRBgpfcc0j8k7g8 "Perl 6 – Try It Online")
This is a straightforward translation of the cosine similarity formula. `x Z* y` zips two lists together with multiplication, and `sum` and `sqrt` are self-explanatory.
[Answer]
# [BQN](https://mlochbaum.github.io/BQN/), ~~20~~ 17 bytes
*-3 bytes thanks to [att](https://codegolf.stackexchange.com/users/81203/att)*
```
D←+´⊣×≠⊸↑
D÷×○√○D
```
The first line defines a helper function; the second line is the solution, an anonymous tacit function that takes two lists of numbers as left and right arguments. [Try it at BQN online!](https://mlochbaum.github.io/BQN/try.html#code=ROKGkCvCtOKKo8OX4omg4oq44oaRCkYg4oaQIETDt8OX4peL4oia4peLRAoKODLigL/CrzkgRiA34oC/NTLigL8z)
### Explanation
The helper function `D` calculates the dot product of two vectors (or, given one argument, the dot product of a vector with itself). The default behavior of `↑` turned out to be *very* convenient here.
```
D←+´⊣×≠⊸↑
D←         Assign this function to D:
      ≠⊸↑    Take (length of left arg) elements from (right arg)
               If left arg is shorter, trims right arg to that length
               If left arg is longer, pads right arg with zeros
    ⊣×       Multiply itemwise with left arg
  +´         Sum
D÷×○√○D
     ○D  Apply D to each argument (i.e. multiply it by itself and sum)
   ○√    Apply square root to each of those sums (magnitude of each vector)
  ×      Multiply those two results
D        Apply D to both arguments (i.e. get their dot product)
 ÷       Divide the dot product by the product of magnitudes
```
[Answer]
# [Arturo](https://arturo-lang.io), 69 bytes
```
$[a b][//∑map couple a b'p->∏p∏map@[a b]=>[sqrt∑map&'x->x^2]]
```
[Try it](http://arturo-lang.io/playground?5ojPMT)
[Answer]
# [Japt](https://github.com/ETHproductions/japt) v2.0a0, 13 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
í*V x÷NËx²¬Ã×
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=2.0a0&code=7SpWIHj3Tst4sqzD1w&input=Wy03LCA0LCA5LCA4XQpbMTYsIDEsIC0yLCA1XQ)
[Answer]
# [jq](https://stedolan.github.io/jq/), 68 bytes
```
(transpose|map((.[0]*.[1])?)|add)/(map(map(.*.)|add|sqrt)|.[0]*.[1])
```
[Try it online!](https://tio.run/##RYwxDsMgDEX3nMIjRA4NNJBk6kEsD0jpUqltEjJyd2pYOlj6eu/Jr6MUdZ3xk/ZveuZ33JUyNHJvyLJ@6By3Td9U5fVMbxrK6Tgvnf9lKUQWHd4ZgSb0GJg7ohUhOIQRoQnnZQhqbpgRJgRJlupsQLAIg@S@@UXWsFYloXf1BXc/ "jq – Try It Online")
`transpose` pads the arrays with `null`s, which we then simply remove with `?` when the multiplication fails, instead of replacing with zero.
[Answer]
# JavaScript, 175 bytes
```
(e,r)=>(l="length",p=(e,r)=>Array.from({...e,[l]:r[l]},e=>e??0),e[l]<r[l]?e=p(e,r):r=p(r,e),e[_="reduce"]((e,$,c)=>e+$*r[c],0)/(e[_]((e,r)=>e+r*r,0)*r[_]((e,r)=>e+r*r,0))**.5)
```
Try it:
```
c=
// start code
(e,r)=>(l="length",p=(e,r)=>Array.from({...e,[l]:r[l]},e=>e??0),e[l]<r[l]?e=p(e,r):r=p(r,e),e[_="reduce"]((e,$,c)=>e+$*r[c],0)/(e[_]((e,r)=>e+r*r,0)*r[_]((e,r)=>e+r*r,0))**.5)
// end code
console.log(c([1,2,3], [4,5,6]))
console.log(c([9, 62, 0, 3], [25, 3, 6]))
console.log(c([-7, 4, 9, 8], [16, 1, -2, 5]))
console.log(c([82, -9], [7, 52, 3]))
```
[Answer]
# [Uiua](https://uiua.org) [SBCS](https://tinyurl.com/Uiua-SBCS-Jan-14), ~~17~~ 16 bytes
```
÷/×√∩/+⍉×.⟜/×⬚0⊟
```
[Try it!](https://uiua.org/pad?src=0_8_0__ZiDihpAgw7cvw5fiiJriiKkvK-KNicOXLuKfnC_Dl-KsmjDiip8KCmYgWzEgMiAzXSBbNCA1IDZdCmYgWzkgNjIgMCAzXSBbMjUgMyA2XQpmIFvCrzcgNCA5IDhdIFsxNiAxIMKvMiA1XQpmIFs4MiDCrzldIFs3IDUyIDNdCg==)
]