public double dot ( SparseVector b ) { SparseVector a = this ; if ( a . N != b . N ) throw new IllegalArgumentException ( "Vector lengths disagree. " + a . N + " != " + b . N ) ; double sum = 0.0 ; // iterate over the vector with the fewest nonzeros if ( a . symbolTable . size ( ) <= b . symbolTable . size ( ) ) { for ( int i : a . symbolTable ) if ( b . symbolTable . contains ( i ) ) sum += a . get ( i ) * b . get ( i ) ; } else { for ( int i : b . symbolTable ) if ( a . symbolTable . contains ( i ) ) sum += a . get ( i ) * b . get ( i ) ; } return sum ; }
Calculates the dot product of this vector a with b