oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use crate::{Vector, VectorError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Sparse vector representation using a hash map for efficient storage
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SparseVector {
    /// Non-zero values indexed by their position
    pub values: HashMap<usize, f32>,
    /// Total dimensions of the vector
    pub dimensions: usize,
    /// Optional metadata
    pub metadata: Option<HashMap<String, String>>,
}

impl SparseVector {
    /// Create a new sparse vector from indices and values
    pub fn new(
        indices: Vec<usize>,
        values: Vec<f32>,
        dimensions: usize,
    ) -> Result<Self, VectorError> {
        if indices.len() != values.len() {
            return Err(VectorError::InvalidDimensions(
                "Indices and values must have same length".to_string(),
            ));
        }

        if let Some(&max_idx) = indices.iter().max() {
            if max_idx >= dimensions {
                return Err(VectorError::InvalidDimensions(format!(
                    "Index {max_idx} exceeds dimensions {dimensions}"
                )));
            }
        }

        let mut sparse_values = HashMap::new();
        for (idx, val) in indices.into_iter().zip(values) {
            if val != 0.0 {
                // Only store non-zero values
                sparse_values.insert(idx, val);
            }
        }

        Ok(Self {
            values: sparse_values,
            dimensions,
            metadata: None,
        })
    }

    /// Create sparse vector from dense vector
    pub fn from_dense(dense: &Vector) -> Self {
        let values = dense.as_f32();
        let mut sparse_values = HashMap::new();

        for (idx, &val) in values.iter().enumerate() {
            if val.abs() > f32::EPSILON {
                // Only store non-zero values
                sparse_values.insert(idx, val);
            }
        }

        Self {
            values: sparse_values,
            dimensions: dense.dimensions,
            metadata: dense.metadata.clone(),
        }
    }

    /// Convert to dense vector
    pub fn to_dense(&self) -> Vector {
        let mut values = vec![0.0; self.dimensions];

        for (&idx, &val) in &self.values {
            if idx < self.dimensions {
                values[idx] = val;
            }
        }

        let mut vec = Vector::new(values);
        vec.metadata = self.metadata.clone();
        vec
    }

    /// Get value at index
    pub fn get(&self, index: usize) -> f32 {
        self.values.get(&index).copied().unwrap_or(0.0)
    }

    /// Set value at index
    pub fn set(&mut self, index: usize, value: f32) -> Result<(), VectorError> {
        if index >= self.dimensions {
            return Err(VectorError::InvalidDimensions(format!(
                "Index {} exceeds dimensions {}",
                index, self.dimensions
            )));
        }

        if value.abs() > f32::EPSILON {
            self.values.insert(index, value);
        } else {
            self.values.remove(&index);
        }

        Ok(())
    }

    /// Get number of non-zero elements
    pub fn nnz(&self) -> usize {
        self.values.len()
    }

    /// Sparsity ratio (percentage of zero elements)
    pub fn sparsity(&self) -> f32 {
        let non_zero = self.nnz() as f32;
        let total = self.dimensions as f32;
        (total - non_zero) / total
    }

    /// Dot product with another sparse vector
    pub fn dot(&self, other: &SparseVector) -> Result<f32, VectorError> {
        if self.dimensions != other.dimensions {
            return Err(VectorError::DimensionMismatch {
                expected: self.dimensions,
                actual: other.dimensions,
            });
        }

        let mut sum = 0.0;

        // Only iterate over the smaller set of indices
        if self.values.len() <= other.values.len() {
            for (&idx, &val) in &self.values {
                if let Some(&other_val) = other.values.get(&idx) {
                    sum += val * other_val;
                }
            }
        } else {
            for (&idx, &val) in &other.values {
                if let Some(&self_val) = self.values.get(&idx) {
                    sum += val * self_val;
                }
            }
        }

        Ok(sum)
    }

    /// Compute cosine similarity with another sparse vector
    pub fn cosine_similarity(&self, other: &SparseVector) -> Result<f32, VectorError> {
        let dot = self.dot(other)?;
        let self_norm = self.l2_norm();
        let other_norm = other.l2_norm();

        if self_norm == 0.0 || other_norm == 0.0 {
            Ok(0.0)
        } else {
            Ok(dot / (self_norm * other_norm))
        }
    }

    /// Compute L2 norm
    pub fn l2_norm(&self) -> f32 {
        self.values.values().map(|v| v * v).sum::<f32>().sqrt()
    }

    /// Compute L1 norm
    pub fn l1_norm(&self) -> f32 {
        self.values.values().map(|v| v.abs()).sum()
    }

    /// Add another sparse vector
    pub fn add(&self, other: &SparseVector) -> Result<SparseVector, VectorError> {
        if self.dimensions != other.dimensions {
            return Err(VectorError::DimensionMismatch {
                expected: self.dimensions,
                actual: other.dimensions,
            });
        }

        let mut result = self.clone();

        for (&idx, &val) in &other.values {
            let new_val = result.get(idx) + val;
            result.set(idx, new_val)?;
        }

        Ok(result)
    }

    /// Subtract another sparse vector
    pub fn subtract(&self, other: &SparseVector) -> Result<SparseVector, VectorError> {
        if self.dimensions != other.dimensions {
            return Err(VectorError::DimensionMismatch {
                expected: self.dimensions,
                actual: other.dimensions,
            });
        }

        let mut result = self.clone();

        for (&idx, &val) in &other.values {
            let new_val = result.get(idx) - val;
            result.set(idx, new_val)?;
        }

        Ok(result)
    }

    /// Scale by scalar
    pub fn scale(&self, scalar: f32) -> SparseVector {
        let mut result = self.clone();

        for val in result.values.values_mut() {
            *val *= scalar;
        }

        result
    }

    /// Normalize to unit length
    pub fn normalize(&self) -> SparseVector {
        let norm = self.l2_norm();
        if norm > 0.0 {
            self.scale(1.0 / norm)
        } else {
            self.clone()
        }
    }
}

/// Compressed Sparse Row (CSR) format for efficient batch operations
#[derive(Debug, Clone, PartialEq)]
pub struct CSRMatrix {
    /// Non-zero values
    pub values: Vec<f32>,
    /// Column indices for each value
    pub col_indices: Vec<usize>,
    /// Row pointers (start index of each row in values/col_indices)
    pub row_ptrs: Vec<usize>,
    /// Shape of the matrix (rows, cols)
    pub shape: (usize, usize),
}

impl CSRMatrix {
    /// Create CSR matrix from sparse vectors
    pub fn from_sparse_vectors(vectors: &[SparseVector]) -> Result<Self, VectorError> {
        if vectors.is_empty() {
            return Ok(Self {
                values: Vec::new(),
                col_indices: Vec::new(),
                row_ptrs: vec![0],
                shape: (0, 0),
            });
        }

        let num_rows = vectors.len();
        let num_cols = vectors[0].dimensions;

        // Verify all vectors have same dimensions
        for (i, vec) in vectors.iter().enumerate() {
            if vec.dimensions != num_cols {
                return Err(VectorError::InvalidDimensions(format!(
                    "Vector {} has {} dimensions, expected {}",
                    i, vec.dimensions, num_cols
                )));
            }
        }

        let mut values = Vec::new();
        let mut col_indices = Vec::new();
        let mut row_ptrs = vec![0];

        for vec in vectors {
            // Sort by column index for CSR format
            let mut sorted_entries: Vec<_> = vec.values.iter().collect();
            sorted_entries.sort_by_key(|&(&idx, _)| idx);

            for (&idx, &val) in sorted_entries {
                values.push(val);
                col_indices.push(idx);
            }

            row_ptrs.push(values.len());
        }

        Ok(Self {
            values,
            col_indices,
            row_ptrs,
            shape: (num_rows, num_cols),
        })
    }

    /// Get a specific row as sparse vector
    pub fn get_row(&self, row: usize) -> Option<SparseVector> {
        if row >= self.shape.0 {
            return None;
        }

        let start = self.row_ptrs[row];
        let end = self.row_ptrs[row + 1];

        let mut values = HashMap::new();
        for i in start..end {
            values.insert(self.col_indices[i], self.values[i]);
        }

        Some(SparseVector {
            values,
            dimensions: self.shape.1,
            metadata: None,
        })
    }

    /// Matrix-vector multiplication
    pub fn multiply_vector(&self, vector: &SparseVector) -> Result<Vec<f32>, VectorError> {
        if self.shape.1 != vector.dimensions {
            return Err(VectorError::DimensionMismatch {
                expected: self.shape.1,
                actual: vector.dimensions,
            });
        }

        let mut result = vec![0.0; self.shape.0];

        for (row, result_val) in result.iter_mut().enumerate().take(self.shape.0) {
            let start = self.row_ptrs[row];
            let end = self.row_ptrs[row + 1];

            let mut sum = 0.0;
            for i in start..end {
                let col = self.col_indices[i];
                if let Some(&vec_val) = vector.values.get(&col) {
                    sum += self.values[i] * vec_val;
                }
            }
            *result_val = sum;
        }

        Ok(result)
    }

    /// Get memory usage in bytes
    pub fn memory_usage(&self) -> usize {
        self.values.len() * std::mem::size_of::<f32>()
            + self.col_indices.len() * std::mem::size_of::<usize>()
            + self.row_ptrs.len() * std::mem::size_of::<usize>()
    }

    /// Get sparsity of the matrix
    pub fn sparsity(&self) -> f32 {
        let total_elements = self.shape.0 * self.shape.1;
        let non_zero = self.values.len();
        (total_elements - non_zero) as f32 / total_elements as f32
    }
}

/// Coordinate (COO) format for easy construction
#[derive(Debug, Clone, PartialEq)]
pub struct COOMatrix {
    pub row_indices: Vec<usize>,
    pub col_indices: Vec<usize>,
    pub values: Vec<f32>,
    pub shape: (usize, usize),
}

impl COOMatrix {
    /// Create empty COO matrix
    pub fn new(rows: usize, cols: usize) -> Self {
        Self {
            row_indices: Vec::new(),
            col_indices: Vec::new(),
            values: Vec::new(),
            shape: (rows, cols),
        }
    }

    /// Add a value to the matrix
    pub fn add_value(&mut self, row: usize, col: usize, value: f32) -> Result<(), VectorError> {
        if row >= self.shape.0 || col >= self.shape.1 {
            return Err(VectorError::InvalidDimensions(format!(
                "Index ({}, {}) out of bounds for shape {:?}",
                row, col, self.shape
            )));
        }

        if value.abs() > f32::EPSILON {
            self.row_indices.push(row);
            self.col_indices.push(col);
            self.values.push(value);
        }

        Ok(())
    }

    /// Convert to CSR format
    pub fn to_csr(&self) -> CSRMatrix {
        // Sort by row, then column
        let mut entries: Vec<_> = (0..self.values.len())
            .map(|i| (self.row_indices[i], self.col_indices[i], self.values[i]))
            .collect();
        entries.sort_by_key(|&(r, c, _)| (r, c));

        let mut values = Vec::new();
        let mut col_indices = Vec::new();
        let mut row_ptrs = vec![0];

        let mut current_row = 0;
        for (row, col, val) in entries {
            while current_row < row {
                row_ptrs.push(values.len());
                current_row += 1;
            }
            values.push(val);
            col_indices.push(col);
        }

        while current_row < self.shape.0 {
            row_ptrs.push(values.len());
            current_row += 1;
        }

        CSRMatrix {
            values,
            col_indices,
            row_ptrs,
            shape: self.shape,
        }
    }
}

#[cfg(test)]
mod tests {
    type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
    use super::*;

    #[test]
    fn test_sparse_vector_creation() -> Result<()> {
        let indices = vec![0, 3, 7];
        let values = vec![1.0, 2.0, 3.0];
        let sparse = SparseVector::new(indices, values, 10)?;

        assert_eq!(sparse.get(0), 1.0);
        assert_eq!(sparse.get(3), 2.0);
        assert_eq!(sparse.get(7), 3.0);
        assert_eq!(sparse.get(5), 0.0);
        assert_eq!(sparse.nnz(), 3);
        assert_eq!(sparse.dimensions, 10);
        Ok(())
    }

    #[test]
    fn test_sparse_dense_conversion() {
        let dense = Vector::new(vec![0.0, 1.0, 0.0, 2.0, 0.0]);
        let sparse = SparseVector::from_dense(&dense);

        assert_eq!(sparse.nnz(), 2);
        assert_eq!(sparse.get(1), 1.0);
        assert_eq!(sparse.get(3), 2.0);

        let dense_back = sparse.to_dense();
        assert_eq!(dense_back.as_f32(), vec![0.0, 1.0, 0.0, 2.0, 0.0]);
    }

    #[test]
    fn test_sparse_operations() -> Result<()> {
        let sparse1 = SparseVector::new(vec![0, 2, 4], vec![1.0, 2.0, 3.0], 5)?;
        let sparse2 = SparseVector::new(vec![1, 2, 3], vec![4.0, 5.0, 6.0], 5)?;

        // Dot product
        let dot = sparse1.dot(&sparse2)?;
        assert_eq!(dot, 10.0); // Only index 2 overlaps: 2.0 * 5.0 = 10.0

        // Addition
        let sum = sparse1.add(&sparse2)?;
        assert_eq!(sum.get(0), 1.0);
        assert_eq!(sum.get(1), 4.0);
        assert_eq!(sum.get(2), 7.0);
        assert_eq!(sum.get(3), 6.0);
        assert_eq!(sum.get(4), 3.0);

        // Scaling
        let scaled = sparse1.scale(2.0);
        assert_eq!(scaled.get(0), 2.0);
        assert_eq!(scaled.get(2), 4.0);
        assert_eq!(scaled.get(4), 6.0);
        Ok(())
    }

    #[test]
    fn test_csr_matrix() -> Result<()> {
        let vectors = vec![
            SparseVector::new(vec![0, 2], vec![1.0, 2.0], 4)?,
            SparseVector::new(vec![1, 3], vec![3.0, 4.0], 4)?,
            SparseVector::new(vec![0, 1, 2], vec![5.0, 6.0, 7.0], 4)?,
        ];

        let csr = CSRMatrix::from_sparse_vectors(&vectors)?;

        assert_eq!(csr.shape, (3, 4));
        assert_eq!(csr.values.len(), 7);
        assert_eq!(csr.row_ptrs, vec![0, 2, 4, 7]);

        // Test row extraction
        let row1 = csr.get_row(1).expect("row 1 should exist");
        assert_eq!(row1.get(1), 3.0);
        assert_eq!(row1.get(3), 4.0);
        Ok(())
    }

    #[test]
    fn test_coo_to_csr() -> Result<()> {
        let mut coo = COOMatrix::new(3, 3);
        coo.add_value(0, 0, 1.0)?;
        coo.add_value(0, 2, 2.0)?;
        coo.add_value(1, 1, 3.0)?;
        coo.add_value(2, 0, 4.0)?;
        coo.add_value(2, 2, 5.0)?;

        let csr = coo.to_csr();
        assert_eq!(csr.values, vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        assert_eq!(csr.col_indices, vec![0, 2, 1, 0, 2]);
        assert_eq!(csr.row_ptrs, vec![0, 2, 3, 5]);
        Ok(())
    }
}