001package org.nasdanika.ai; 002 003import java.util.List; 004 005import reactor.core.publisher.Mono; 006 007public interface FloatVectorSimilarityComputer extends VectorSimilarityComputer<Float, Float> { 008 009 /** 010 * Calculates the cosine similarity. 011 */ 012 static FloatVectorSimilarityComputer COSINE_SIMILARITY_COMPUTER = new FloatVectorSimilarityComputer() { 013 014 @Override 015 public Mono<Float> computeAsync(List<Float> a, List<Float> b) { 016 float dot = 0.0f; 017 float nru = 0.0f; 018 float nrv = 0.0f; 019 for (int i = 0; i < a.size(); i++) { 020 dot += a.get(i) * b.get(i); 021 nru += a.get(i) * a.get(i); 022 nrv += b.get(i) * b.get(i); 023 } 024 025 return Mono.just(dot / (float)(Math.sqrt(nru) * Math.sqrt(nrv))); 026 } 027 }; 028 029// INNER_PRODUCT_SIMILARITY 030// EUCLIDEAN_SIMILARITY 031// CANBERRA_SIMILARITY 032// BRAY_CURTIS_SIMILARITY 033// CORRELATION_SIMILARITY 034// MANHATTAN_SIMILARITY 035 036}