{"version":3,"sources":["../../../src/internals/helpers/math.ts"],"names":["cosineSimilarity","vecA","vecB","length","ValueError","dot","reduce","sum","value","index","magA","Math","sqrt","magB","cosineSimilarityMatrix","matrixA","matrixB","map","rowA","rowB"],"mappings":";;;;;;AAkBO,SAASA,gBAAAA,CAAiBC,MAAgBC,IAAc,EAAA;AAC7D,EAAID,IAAAA,IAAAA,CAAKE,MAAWD,KAAAA,IAAAA,CAAKC,MAAQ,EAAA;AAC/B,IAAM,MAAA,IAAIC,sBAAW,gCAAA,CAAA;AACvB;AAEA,EAAA,MAAMC,GAAMJ,GAAAA,IAAAA,CAAKK,MAAO,CAAA,CAACC,GAAKC,EAAAA,KAAAA,EAAOC,KAAUF,KAAAA,GAAAA,GAAMC,KAAQN,GAAAA,IAAAA,CAAKO,KAAAA,CAAAA,EAAQ,CAAA,CAAA;AAC1E,EAAA,MAAMC,IAAOC,GAAAA,IAAAA,CAAKC,IAAKX,CAAAA,IAAAA,CAAKK,MAAO,CAAA,CAACC,GAAKC,EAAAA,KAAAA,KAAUD,GAAMC,GAAAA,KAAAA,IAAS,CAAG,EAAA,CAAA,CAAA,CAAA;AACrE,EAAA,MAAMK,IAAOF,GAAAA,IAAAA,CAAKC,IAAKV,CAAAA,IAAAA,CAAKI,MAAO,CAAA,CAACC,GAAKC,EAAAA,KAAAA,KAAUD,GAAMC,GAAAA,KAAAA,IAAS,CAAG,EAAA,CAAA,CAAA,CAAA;AAErE,EAAIE,IAAAA,IAAAA,KAAS,CAAKG,IAAAA,IAAAA,KAAS,CAAG,EAAA;AAC5B,IAAM,MAAA,IAAIT,sBAAW,wCAAA,CAAA;AACvB;AACA,EAAA,OAAOC,OAAOK,IAAOG,GAAAA,IAAAA,CAAAA;AACvB;AAbgBb,MAAAA,CAAAA,gBAAAA,EAAAA,kBAAAA,CAAAA;AAeT,SAASc,sBAAAA,CAAuBC,SAAqBC,OAAmB,EAAA;AAC7E,EAAKD,IAAAA,CAAAA,OAAAA,CAAQ,CAAA,CAAIZ,EAAAA,MAAAA,IAAU,QAAQY,OAAQ,CAAA,CAAA,CAAIZ,EAAAA,MAAAA,IAAU,CAAI,CAAA,EAAA;AAC3D,IAAM,MAAA,IAAIC,sBAAW,gDAAA,CAAA;AACvB;AAEA,EAAA,OAAOW,OAAQE,CAAAA,GAAAA,CAAI,CAACC,IAAAA,KAASF,OAAQC,CAAAA,GAAAA,CAAI,CAACE,IAAAA,KAASnB,gBAAiBkB,CAAAA,IAAAA,EAAMC,IAAAA,CAAAA,CAAAA,CAAAA;AAC5E;AANgBL,MAAAA,CAAAA,sBAAAA,EAAAA,wBAAAA,CAAAA","file":"math.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ValueError } from \"@/errors.js\";\n\nexport function cosineSimilarity(vecA: number[], vecB: number[]): number {\n  if (vecA.length !== vecB.length) {\n    throw new ValueError(\"Vectors must have equal length\");\n  }\n  // (cos θ) = (A · B) / (|A| * |B|)\n  const dot = vecA.reduce((sum, value, index) => sum + value * vecB[index], 0);\n  const magA = Math.sqrt(vecA.reduce((sum, value) => sum + value ** 2, 0));\n  const magB = Math.sqrt(vecB.reduce((sum, value) => sum + value ** 2, 0));\n\n  if (magA === 0 || magB === 0) {\n    throw new ValueError(\"Vectors cannot not have zero magnitude\");\n  }\n  return dot / (magA * magB);\n}\n\nexport function cosineSimilarityMatrix(matrixA: number[][], matrixB: number[][]): number[][] {\n  if ((matrixA[0]?.length ?? 0) !== (matrixA[0]?.length ?? 0)) {\n    throw new ValueError(\"Matrices must have the same number of columns.\");\n  }\n\n  return matrixA.map((rowA) => matrixB.map((rowB) => cosineSimilarity(rowA, rowB)));\n}\n"]}