tensor4all_tcicore/lib.rs
1#![warn(missing_docs)]
2//! TCI Core infrastructure
3//!
4//! Shared foundation for tensor cross interpolation algorithms.
5//!
6//! This crate provides three categories of functionality:
7//!
8//! - **Matrix cross interpolation**: [`MatrixLUCI`] (LU-based), [`MatrixACA`]
9//! (adaptive cross approximation), and [`RrLU`] (rank-revealing LU
10//! decomposition). All CI types implement the [`AbstractMatrixCI`] trait.
11//! - **Cached function evaluation**: [`CachedFunction`] wraps expensive
12//! multi-index functions with thread-safe memoization and automatic key
13//! type selection (up to 1024 bits by default, extensible via [`CacheKey`]).
14//! - **Utility types**: [`IndexSet`] for bidirectional pivot management,
15//! [`Matrix`] for dense row-major matrices, and [`Scalar`] for numeric
16//! scalar requirements.
17//!
18//! Higher-level crates (`tensor4all-tensorci`, `tensor4all-quanticstci`) build
19//! on these primitives.
20//!
21//! # Examples
22//!
23//! Cross-interpolate a rank-2 matrix:
24//!
25//! ```
26//! use tensor4all_tcicore::{AbstractMatrixCI, MatrixLUCI, from_vec2d};
27//!
28//! let m = from_vec2d(vec![
29//! vec![1.0_f64, 2.0, 3.0],
30//! vec![4.0, 5.0, 6.0],
31//! vec![7.0, 8.0, 9.0],
32//! ]);
33//!
34//! let ci = MatrixLUCI::from_matrix(&m, None).unwrap();
35//! assert!(ci.rank() <= 3);
36//! assert!(ci.rank() >= 1);
37//!
38//! // Reconstruction matches original at pivot positions
39//! for &i in ci.row_indices() {
40//! for &j in ci.col_indices() {
41//! assert!((ci.evaluate(i, j) - m[[i, j]]).abs() < 1e-10);
42//! }
43//! }
44//! ```
45//!
46//! Cache an expensive function:
47//!
48//! ```
49//! use tensor4all_tcicore::CachedFunction;
50//!
51//! let cf = CachedFunction::new(
52//! |idx: &[usize]| (idx[0] as f64) * (idx[1] as f64),
53//! &[3, 4],
54//! ).unwrap();
55//!
56//! assert_eq!(cf.eval(&[2, 3]), 6.0);
57//! assert_eq!(cf.num_evals(), 1);
58//!
59//! // Second call hits cache
60//! assert_eq!(cf.eval(&[2, 3]), 6.0);
61//! assert_eq!(cf.num_cache_hits(), 1);
62//! ```
63
64pub mod cached_function;
65pub mod error;
66pub mod indexset;
67pub mod matrix;
68mod matrix_luci;
69pub mod matrixaca;
70pub mod matrixlu;
71pub mod matrixluci;
72pub mod scalar;
73pub mod traits;
74
75pub use self::matrixluci::{
76 CandidateMatrixSource, CrossFactors, DenseFaerLuKernel, DenseMatrixSource, DenseOwnedMatrix,
77 LazyBlockRookKernel, LazyMatrixSource, MatrixLuciError, PivotKernel, PivotKernelOptions,
78 PivotSelectionCore,
79};
80pub use self::matrixluci::{Result as MatrixLuciResult, Scalar as MatrixLuciScalar};
81pub use cached_function::cache_key::CacheKey;
82pub use cached_function::error::CacheKeyError;
83pub use cached_function::index_int::IndexInt;
84pub use cached_function::CachedFunction;
85pub use error::{MatrixCIError, Result};
86pub use indexset::{IndexSet, LocalIndex, MultiIndex};
87pub use matrix::{from_vec2d, Matrix};
88pub use matrix_luci::MatrixLUCI;
89pub use matrixaca::MatrixACA;
90pub use matrixlu::{rrlu, rrlu_inplace, RrLU, RrLUOptions};
91pub use scalar::Scalar;
92pub use traits::AbstractMatrixCI;