目录

Using mathPrediction for Mathematical Prediction

Starting from API version 26.0.0, FAST Kit provides the mathPrediction math prediction module. It models and predicts sequence data based on historical sampling data and is suitable for real-time prediction scenarios such as gesture tracking, animation curve prediction, and motion trajectory estimation.

The mathPrediction math prediction module provides intelligent sequence prediction (predictIndex) capabilities. It receives an array of sampling points containing indexes and timestamps, and uses built-in FAST Kit algorithms to predict the index value at the next time step. The core principle is to use time series analysis techniques to perform trend fitting and extrapolation on input sampling points, thereby predicting future states.

Use Cases

The mathPrediction module is applicable to the following scenarios:

Development Procedure

  1. Before calling the predictIndex function, ensure that the mathPrediction module has been correctly imported and the passed** IndexSample** array contains at least two valid sampling points.

  2. Import the mathPrediction module.

  3. Construct the IndexSample API array and add at least two sampling points.

  4. Call the predictIndex function to perform prediction.

  5. Obtain the returned prediction result.

  6. Handle exceptions. For example, throw an exception and log the error when the number of sampling points is less than two.

  7. The timestamps of sampling points must be monotonically increasing. Otherwise, the prediction result may be inaccurate.

import { hilog } from '@kit.PerformanceAnalysisKit';
import { mathPrediction } from '@kit.FASTKit';

const DOMAIN = 0x0000;

function mathPredictionTest(): void {
  // Construct an array that contains at least two sampling points to predict the next index value.
  let samples: mathPrediction.IndexSample[] = [
    // First sampling point: The initial index is 0 and the timestamp is 0.
    { index: 0, timestamp: 0 },
    // Second sampling point: The index is 10 and the timestamp is 100.
    { index: 10, timestamp: 100 },
    // Third sampling point: The index is 20 and the timestamp is 200.
    { index: 20, timestamp: 200 }
  ];
  try {
    // Call the prediction function to obtain the next index value.
    const predicted = mathPrediction.predictIndex(samples);
    hilog.info(DOMAIN, 'testTag', 'PredictionFunction predicted=%{public}d', predicted);
  } catch (err) {
    // Capture and record the error code and error information when the prediction fails.
    let code = (err as BusinessError).code;
    let message = (err as BusinessError).message;
    hilog.error(DOMAIN, 'testTag', 'PredictionFunction failed, code: %{public}d, message: %{public}s', code, message);
  }
}