@adaskothebeast/splines

中文文档
6.0.0发布于 2026/11/02AGPL-3.0-or-laterTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4888

类型脚本的 Spline 插值

中文使用文档
文档目录17 节
  1. 为什么?
  2. * 安装
  3. QQ 快速启动
  4. 计算
  5. Monotone Cubic 赫米特人国际化
  6. 自然立方体 Spline
  7. • API
  8. 输入格式
  9. 方法
  10. 选项
  11. 输入验证出错( E)
  12. v6.0.0中的新内容
  13. 我该用哪种算法?
  14. 示例:平滑有声信号
  15. 例:热力学——蒸发压到温度
  16. 许可证
  17. 捐款

*

npm version NPM Downloads @adaskothebeast/splines License: AGPL v3 TypeScript CodeFactor GitHub Actions Workflow Status Sonar Tests Sonar Coverage Quality Gate Status Bugs Code Smells Coverage Duplicated Lines (%) Lines of Code Reliability Rating Security Rating Maintainability Rating Technical Debt Vulnerabilities

轻量级,零依赖相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相相生相生相生相生相生相生相生相生相相生相生相生相生相生相生相生相生相生相.

平滑曲线通过您的数据点——不需要博士学位.


为什么?

永远需要:

  • 平滑出传感器数据 没有失去趋势?
  • 键框之间的动画 自然运动?
  • 填补时间序列中的空白 数据智能化?
  • 创建平滑图表 从零散的数据点?
  • 插入颜色、位置或任何数值 顺利吗?

线性插值看起来是机器人 单体内插会野生地振荡(龙格现象) (中文(简体) ). Splines)给你两个世界中最好的 ——实际通过您数据点的平滑曲线.

本库为您提供 ~3KB( 最小化) 中可制取的 Spline 插值, 包括:

  • ✅ 零依赖关系 - 只是数学
  • ✅ 类型脚本第一 ——全型安全
  • ✅ 两个算法 ——选择正确的数据工具
  • ✅ 树能震动 - 只捆绑你用的东西
  • ✅ 测试良好 综合测试套房

* 安装

npm install @adaskothebeast/splines
yarn add @adaskothebeast/splines
pnpm add @adaskothebeast/splines

QQ 快速启动

import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';

// Your data points (x, y pairs)
const data = [
  [0, 0],
  [1, 2],
  [2, 3],
  [3, 5],
  [4, 4],
];

// Create the spline
const spline = new MonotoneCubicHermitInterpolation(data);

// Interpolate at any x value
spline.interpolate(1.5);  // → ~2.5 (smooth value between points)
spline.interpolate(2.7);  // → ~4.1

计算

Monotone Cubic 赫米特人国际化

最佳服务: 永远不应超标的数据(传感器读取、百分比、概率)

import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';

const spline = new MonotoneCubicHermitInterpolation([
  [0, 0],
  [1, 100],
  [2, 100],  // flat section
  [3, 50],
]);

// Guarantees: if your data goes 0 → 100, interpolated values stay in [0, 100]
// No overshooting, no undershooting

为什么用它?

  • 保守单调——如果数据上升,插值上升
  • 没有振荡和振动的文物
  • 适合物理限制的数据

📚 维基百科:莫诺通立方插图


自然立方体 Spline

最佳服务: 平滑曲线,其中过度射击是可以接受的(动画,可视化)

import { NaturalSpline } from '@adaskothebeast/splines';

const spline = new NaturalSpline([
  [0, 0],
  [1, 2],
  [2, 0],
  [3, 2],
]);

// Produces the smoothest possible curve (minimizes curvature)
// May overshoot at peaks/valleys — that's often desirable for animation

// Note: values outside the data range are clamped to the nearest endpoint

为什么用它?

  • 最大平滑——连续的第一和第二个衍生工具
  • 可视化的自然外观曲线
  • CAD 和图形中使用的经典算法

📚 学习更多: 自然立方体 Spline


• API

输入格式

两种算法都接受数据为tuple或 NumberPair 对象 :

// Tuple format (recommended)
const data: [number, number][] = [[0, 0], [1, 2], [2, 4]];

// Object format
import { NumberPair } from '@adaskothebeast/splines';
const data = [new NumberPair(0, 0), new NumberPair(1, 2), new NumberPair(2, 4)];

方法

const spline = new MonotoneCubicHermitInterpolation(data);

// Interpolate at any x value
const y = spline.interpolate(x);

选项

两种算法都接受可选 options 参数 :

type OutOfRangeStrategy = 'clamp' | 'extrapolate' | 'throw';

type SplineOptions = {
  outOfRange?: OutOfRangeStrategy;
};
import { NaturalSpline } from '@adaskothebeast/splines';

const spline = new NaturalSpline(data, { outOfRange: 'extrapolate' });

注释:

  • 数据由 x 值自动排序
  • MonotoneCubicHermiteInterpolation 默认为 outOfRange: 'extrapolate'
  • NaturalSpline 默认为 outOfRange: 'clamp'
  • 若为 outOfRange: 'throw', (中文(简体) ), interpolate(x) 丢出一个 RangeError 用于数据范围以外的x
  • NaturalSpline 通过线性插值支持2分( 3+ 点使用天然立方丝线)
  • 空数组会出错
  • X 值必须是有限度的,在排序后必须严格增加(不支持重复的 x 值)

输入验证出错( E)

构造器验证前面的输入点并扔出一个 SplineInputError 带有 SplineErrorCode 当提供无效数据时:

import {
  NaturalSpline,
  SplineErrorCode,
  SplineInputError,
  validateInput,
} from '@adaskothebeast/splines';

try {
  new NaturalSpline([
    [1, 10],
    [1, 20], // duplicate x
  ]);
} catch (err) {
  if (err instanceof SplineInputError) {
    console.error(err.code); // SplineErrorCode.NonIncreasingX ('NON_INCREASING_X')
  }
}
错误代码( E)SplineErrorCode(中文(简体) )数值条件
EmptyInput'EMPTY_INPUT'输入数组为空
InvalidFormat'INVALID_FORMAT'输入不是一个 [number, number] 二联或 { x, y } 双对
NonFinite'NON_FINITE'任意 x 或者说 y 值为 NaN, (中文(简体) ), Infinity,或 -Infinity
NonIncreasingX'NON_INCREASING_X'复制 x 排序后值存在

v6.0.0中的新内容

  • 可配置的外出行为( )SplineOptions(中文(简体) ): 传出 { outOfRange: 'clamp' | 'extrapolate' | 'throw' } 改为 NaturalSpline (违约) 'clamp')或(或) MonotoneCubicHermiteInterpolation (违约) 'extrapolate') (中文(简体) ).
  • 严格输入验证( E)SplineInputError 打开 SplineErrorCode(中文(简体) ): 构造器和导出的 validateInput 编号 : normalizeAndValidate 帮助者验证输入是非空的、有限度的,并严格增加了 x 排序后值, 扔出类型 SplineInputError 实例.
  • NaturalSpline 精确和 2- 点支持数字 :
    • 固定的三相解析器边界处理,所以所有相线段都精确地通过每个结点.
    • 构建时添加直接线性插值支持 NaturalSpline 和两分.
  • 业绩优化数字 :
    • 将内部数字数组替换为 Float64Array 示线系数的缓冲.
    • 添加一个快速路径, 当输入点已被命令时跳过索引排序 x。 。 。 .
  • 子路径导出 & 现代工具链数字 :
    • 从两者中导出验证和选项类型/帮助 @adaskothebeast/splines 和亚路径入站点(@adaskothebeast/splines/NaturalSpline, (中文(简体) ), @adaskothebeast/splines/MonotoneCubicHermiteInterpolation) (中文(简体) ).
    • 将构建和测试工具链升级到Nx 24, TypeScript 6和Node 26.

我该用哪种算法?

假设建议
传感器数据(温度、压力)木乃伊立方体赫米特语
进度栏,百分比木乃伊立方体赫米特语
动画键框天然螺纹
平滑图要么,要么,要么,要么,要么
财务数据木乃伊立方体赫米特语
类似贝塞尔的曲线天然螺纹

拇指规则: 如果过度射击会是一个错误,使用莫诺内. 如果过度射击增加了自然运动,则使用自然运动.


示例:平滑有声信号

import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';

// Sparse measurements
const measurements = [
  [0, 20.1],
  [5, 22.3],
  [10, 25.8],
  [15, 24.2],
  [20, 26.5],
];

const spline = new MonotoneCubicHermitInterpolation(measurements);

// Generate smooth curve with 100 points
const smoothCurve = [];
for (let x = 0; x <= 20; x += 0.2) {
  smoothCurve.push({ x, y: spline.interpolate(x) });
}

例:热力学——蒸发压到温度

倒置蒸汽台数据以从饱和蒸汽压得到温度. 标准表格给你 T → P但有时你需要 P → T。 。 。 .

import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';

// Water saturation vapor pressure data (from steam tables)
// Format: [Pressure in kPa, Temperature in °C]
const vaporPressureToTemp = [
  [0.6113, 0],      // Triple point
  [1.2281, 10],
  [2.3392, 20],
  [4.2470, 30],
  [7.3849, 40],
  [12.352, 50],
  [19.946, 60],
  [31.201, 70],
  [47.414, 80],
  [70.182, 90],
  [101.33, 100],    // Boiling point at 1 atm
  [143.38, 110],
  [198.67, 120],
];

// Create inverse lookup: Pressure → Temperature
const pressureToTempSpline = new MonotoneCubicHermitInterpolation(vaporPressureToTemp);

// Now you can find saturation temperature for any pressure
pressureToTempSpline.interpolate(101.33);  // → 100°C (boiling point at 1 atm)
pressureToTempSpline.interpolate(50);      // → ~81.3°C
pressureToTempSpline.interpolate(20);      // → ~60.1°C

// Useful for:
// - HVAC calculations
// - Psychrometric charts
// - Boiler/steam system design
// - Dew point calculations

为什么这里是莫诺恩立方体赫米特? 活体压与温度严格为单调相通——使用单调相通插能保证物理上有意义的结果(没有负压,没有温度倒置).


许可证

AGPL-3.0- 或- 后期 亚当·"Adasko The BeAsT" 普鲁辛斯基


捐款

问题和公关欢迎! 这是一个小型的,专注的图书馆——让我们保持这样.

git clone https://github.com/AdaskoTheBeAsT/Splines-TypeScript.git
cd Splines-TypeScript
yarn install
yarn test

版本与发布记录

@adaskothebeast/splines 共发布 19 个版本,首次发布于 2022/05/22;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.0latest2026/11/026 天前
5.0.02025/12/0111 个月前
4.0.02023/12/242 年前
3.0.02023/11/132 年前
2.1.42023/10/233 年前
2.1.32023/10/233 年前

关于 @adaskothebeast/splines 的常见问题

@adaskothebeast/splines 是什么?

类型脚本的 Spline 插值

如何安装 @adaskothebeast/splines?

在项目目录运行 npm install @adaskothebeast/splines,或使用 pnpm add / yarn add / bun add。

@adaskothebeast/splines 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adaskothebeast/splines 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@adaskothebeast/splines 可以商用吗?

@adaskothebeast/splines 使用 AGPL-3.0-or-later 许可。最严格的 Copyleft:除分发外,通过网络向用户提供基于它的服务也需要开源对应源码。闭源 SaaS 使用前务必评估。以上为概括,不构成法律意见,请以许可原文为准。

@adaskothebeast/splines 的下载量有多少?

根据 npm 官方下载统计,@adaskothebeast/splines 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,888 次,日均约 163 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adaskothebeast/splines 还在维护吗?

最新版本 6.0.0 发布于 2026/11/02(6 天前);至今共发布 19 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/05/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。