I'm not sure about your implementation, but the cosine coefficient of two vectors is equal to the normalized point product of these vectors.
a. b = a T b. , , .
TF * IDF term, document, , , .
, , , . , , .
, , , , .
TF * IDF:
, TF * IDF term-document a. a i, j, i - , j - . , (, , 1). , i, j= f i, j * D/d i, f i, j is i doc j, D - , d i - i .
- , b. b i, q i q. b i, q= f i, q, f i, q - i q. , .
, . a, b Frobenius norm.
, -, b . , ( ) . b T a. , .