MCPcopy Create free account
hub / github.com/cosdata/cosdata / main

Function main

org/src/project_to_dim.rs:115–230  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

113
114
115fn main() {
116 let mut rng = thread_rng();
117 let uniform = Uniform::new(-1.0, 1.0);
118
119 // Create two random source vectors of 320 dimensions
120 let a: Vec<f64> = (0..320).map(|_| uniform.sample(&mut rng)).collect();
121 let b: Vec<f64> = (0..320).map(|_| uniform.sample(&mut rng)).collect();
122
123 // Calculate and print cosine similarity between original A and B
124 let similarity_ab_original = cosine_similarity(&a, &b);
125 println!("Cosine similarity between original A and B: {:.6}", similarity_ab_original);
126
127 // Number of samples and target dimensions
128 let num_vectors = 100;
129 let target_dimensions = 16;
130
131 // Generate perturbed versions of A
132 let normal = Normal::new(0.0, 0.25).unwrap();
133 let perturbed_vectors: Vec<Vec<f64>> = (0..num_vectors)
134 .map(|_| a.iter().map(|&x| x * (1.0 + normal.sample(&mut rng))).collect())
135 .collect();
136
137 // Print summary of perturbations
138 println!("Summary of perturbations:");
139 for (i, perturbed) in perturbed_vectors.iter().take(5).enumerate() {
140 let diff: Vec<f64> = perturbed.iter().zip(a.iter()).map(|(p, a)| p - a).collect();
141 println!("\nPerturbed vector {}:", i + 1);
142 println!(" Mean difference: {:.6}", diff.iter().sum::<f64>() / diff.len() as f64);
143 println!(" Max difference: {:.6}", diff.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)));
144 println!(" Min difference: {:.6}", diff.iter().fold(f64::INFINITY, |a, &b| a.min(b)));
145 }
146
147 // Project source A and B
148 let a_source_projected = project_vector_to_x(&a, target_dimensions);
149 let b_projected = project_vector_to_x(&b, target_dimensions);
150
151 // Calculate and print cosine similarity between projected A and B
152 let similarity_ab_source = cosine_similarity(&a_source_projected, &b_projected);
153 println!("\nProjected A: {:?}", a_source_projected);
154 println!("\nProjected B: {:?}", b_projected);
155 println!("\nCosine similarity between projected A and B: {:.6}", similarity_ab_source);
156
157 // Calculate and print indices for projected A and B
158 let (index_a, alt_indices_a) = make_index(&a_source_projected);
159 let (index_b, alt_indices_b) = make_index(&b_projected);
160 println!("Index for projected A: {}", index_a);
161 println!("Alternative indices for A: {:?}", alt_indices_a);
162 println!("Index for projected B: {}", index_b);
163 println!("Alternative indices for B: {:?}", alt_indices_b);
164
165 // Main loop
166 let mut similarities_ab = Vec::new();
167 let mut similarities_aa = Vec::new();
168 let mut main_index_counts = HashMap::new();
169 let mut alt_index_counts = HashMap::new();
170
171 for a_perturbed in &perturbed_vectors {
172 let a_perturbed_projected = project_vector_to_x(a_perturbed, target_dimensions);

Callers

nothing calls this directly

Calls 5

cosine_similarityFunction · 0.70
project_vector_to_xFunction · 0.70
make_indexFunction · 0.70
iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected