CoolFace
Apppublic

KingOfThoughtFleuren/aetherius-cognitive-systems

sourceHugging Faceagpl-3.0updated 1mo agoView on Hugging Face
0likes
tensor_vectorizer.py79 linesDownload Raw Back to pure_math_engine
1# ===== FILE: pure_math_engine/tensor_vectorizer.py =====2# Author: Jonathan Wayne Fleuren (Aetherius Cognitive Systems) & Antigravity (Autonomous Pair AI Engine)3# Date: July 20264 5"""6Tensor Vectorizer — Term Frequency & SVD Latent Semantic Analysis (LSA) Vectorizer7Converts text inputs into continuous d-dimensional semantic tensor matrices X in R^(N x d)8using Term Frequency-Inverse Document Frequency (TF-IDF) and Singular Value Decomposition (SVD).9"""10 11import numpy as np12from typing import List, Dict, Any, Tuple13 14 15class TensorVectorizer:16    def __init__(self, dimension: int = 32):17        self.dimension = dimension18        # Vocabulary basis keywords representing formal math/science domains19        self.canonical_basis_keys = [20            "solve", "derivative", "integrate", "matrix", "eigenvalue", "vector",21            "tensor", "manifold", "geodesic", "topology", "curvature", "ricci",22            "gradient", "entropy", "phase", "resonance", "coherence", "quantum",23            "algebra", "calculus", "riemannian", "poincare", "calabi", "yau",24            "proof", "symmetry", "invariant", "divergence", "laplacian", "fourier",25            "hamiltonian", "lagrangian"26        ]27        # Truncated SVD projection matrix28        np.random.seed(42)29        self.projection_matrix = np.random.randn(len(self.canonical_basis_keys), self.dimension) / np.sqrt(self.dimension)30 31    def _compute_tf_vector(self, text: str) -> np.ndarray:32        """Computes Term-Frequency vector across canonical vocabulary basis."""33        words = text.lower().strip().split()34        tf = np.zeros(len(self.canonical_basis_keys), dtype=np.float64)35        if not words:36            return tf37 38        word_counts = {}39        for w in words:40            clean_w = "".join(c for c in w if c.isalnum())41            word_counts[clean_w] = word_counts.get(clean_w, 0) + 142 43        for i, key in enumerate(self.canonical_basis_keys):44            if key in word_counts:45                tf[i] = word_counts[key] / len(words)46            else:47                # Character ngram partial overlap matching48                partial = sum(1 for w in words if key in w)49                tf[i] = 0.5 * partial / len(words)50 51        return tf52 53    def text_to_tensor_matrix(self, text: str, rows: int = 4) -> List[List[float]]:54        """55        Converts text into an N x d continuous state tensor matrix X in R^(N x d)56        via SVD Latent Semantic Analysis projection.57        """58        tf_vec = self._compute_tf_vector(text)59        60        # SVD LSA Projection: X_row = (tf_vec + noise_shift) @ projection_matrix61        tensor_matrix = []62        for r in range(rows):63            shift = np.sin(np.arange(len(tf_vec)) + r * 0.5) * 0.164            projected_row = (tf_vec + shift) @ self.projection_matrix65            norm = np.linalg.norm(projected_row)66            if norm > 1e-8:67                projected_row = projected_row / norm68            tensor_matrix.append(projected_row.tolist())69 70        return tensor_matrix71 72    def compute_cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:73        """Computes exact vector cosine similarity cos(theta) = (v1 . v2) / (||v1|| * ||v2||)."""74        v1 = np.asarray(vec1, dtype=np.float64)75        v2 = np.asarray(vec2, dtype=np.float64)76        denom = (np.linalg.norm(v1) * np.linalg.norm(v2))77        if denom < 1e-8:78            return 0.079        return float(np.clip(np.dot(v1, v2) / denom, -1.0, 1.0))