public static function cosineSimilarity(array $vector_a, array $vector_b,
		$length_a = null, $length_b = null){

		$sum = 0;
		$sum_a_sq = ($length_a === null ? 0.0 : (float) $length_a);
		$sum_b_sq = ($length_b === null ? 0.0 : (float) $length_b);

		foreach($vector_a as $key => $value){

			if(isset($vector_b[$key])) $sum += ($value * $vector_b[$key]);

			if($length_a === null) $sum_a_sq += pow($value, 2);
		}

		if($length_a === null) $sum_a_sq = sqrt($sum_a_sq);

		if($length_b === null){

			foreach($vector_b as $key => $value){

				$sum_b_sq += pow($value, 2);
			}

			$sum_b_sq = sqrt($sum_b_sq);
		}

		$division = $sum_b_sq * $sum_a_sq;

		if($division == 0){

			return 0;
		}
		else{

			$result = $sum / $division;

			return $result;
		}
	}