Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(formula): supplement LINEST/LOGEST/TREND formula (#3965)

wpxp123456ff2017042b47
diff --git a/packages/engine-formula/src/basics/math.ts b/packages/engine-formula/src/basics/math.ts
index 08b860bb91..7a86ebf845 100644
--- a/packages/engine-formula/src/basics/math.ts
+++ b/packages/engine-formula/src/basics/math.ts
@@ -138,6 +138,146 @@ export function matrixTranspose(matrix: number[][]): number[][] {
return matrix[0].map((_, colIndex) => matrix.map((row) => row[colIndex]));
}
+export function inverseMatrixByLUD(matrix: number[][]) {
+ const { smallPivotDetected, luMatrix, permutation } = performLUDecomposition(matrix);
+
+ if (smallPivotDetected) {
+ return null;
+ }
+
+ return transformMatrix(luMatrix, permutation);
+}
+
+function performLUDecomposition(matrix: number[][]) {
+ const decomposedMatrix = matrixTranspose(matrix);
+ const numRows = decomposedMatrix.length;
+ const numCols = decomposedMatrix[0].length;
+ let isRowSwap = true;
+ let smallPivotDetected = false;
+ const luMatrix = createMatrix(numRows, numCols, 0);
+ const permutation = new Array(numCols).fill(0).map((_, index) => index);
+
+ for (let c = 0; c < numCols; c++) {
+ for (let i = 0; i < c; i++) {
+ let value = decomposedMatrix[i][c];
+
+ for (let k = 0; k < i; k++) {
+ value -= luMatrix[i][k] * luMatrix[k][c];
+ }
+
+ luMatrix[i][c] = value;
+ }
+
+ let maxPivot = -Infinity;
+ let pivotRow = c;
+
+ for (let r = c; r < numRows; r++) {
+ let value = decomposedMatrix[r][c];
+
+ for (let k = 0; k < c; k++) {
+ value -= luMatrix[r][k] * luMatrix[k][c];
+ }
+
+ luMatrix[r][c] = value;
+
+ const absValue = Math.abs(value);
+
+ if (absValue > maxPivot) {
+ maxPivot = absValue;
+ pivotRow = r;
+ }
+ }
+
+ if (Math.abs(luMatrix[pivotRow][c]) < 1e-11) {
+ smallPivotDetected = true;
+ break;
+ }
+
+ if (pivotRow !== c) {
+ [luMatrix[c], luMatrix[pivotRow]] = [luMatrix[pivotRow], luMatrix[c]];
+ [decomposedMatrix[c], decomposedMatrix[pivotRow]] = [decomposedMatrix[pivotRow], decomposedMatrix[c]];
+ [permutation[c], permutation[pivotRow]] = [permutation[pivotRow], permutation[c]];
+ isRowSwap = !isRowSwap;
+ }
+
+ const pivotElement = luMatrix[c][c];
+
+ for (let r = c + 1; r < numRows; r++) {
+ luMatrix[r][c] /= pivotElement;
+ }
+ }
+
+ return {
+ rowSwap: isRowSwap,
+ smallPivotDetected,
+ luMatrix,
+ permutation,
+ };
+}
+
+function transformMatrix(inputMatrix: number[][], indices: number[]): number[][] {
+ const size = indices.length;
+ const identityMatrix = createMatrix(size, size, 0);
+
+ for (let i = 0; i < size; i++) {
+ identityMatrix[i][i] = 1;
+ }
+
+ const resultMatrix = createMatrix(size, size, 0);
+
+ for (let rowIndex = 0; rowIndex < size; rowIndex++) {
+ const index = indices[rowIndex];
+ for (let colIndex = 0; colIndex < size; colIndex++) {
+ resultMatrix[rowIndex][colIndex] = identityMatrix[index][colIndex];
+ }
+ }
+
+ for (let pivot = 0; pivot < size; pivot++) {
+ const currentRow = resultMatrix[pivot];
+
+ for (let row = pivot + 1; row < size; row++) {
+ const factor = inputMatrix[row][pivot];
+
+ for (let column = 0; column < size; column++) {
+ resultMatrix[row][column] -= currentRow[column] * factor;
+ }
+ }
+ }
+
+ for (let row = size - 1; row >= 0; row--) {
+ const currentRow = resultMatrix[row];
+ const pivotElement = inputMatrix[row][row];
+
+ for (let column = 0; column < size; column++) {
+ currentRow[column] /= pivotElement;
+ }
+
+ for (let upperRow = 0; upperRow < row; upperRow++) {
+ const upperFactor = inputMatrix[upperRow][row];
+
+ for (let upperCol = 0; upperCol < size; upperCol++) {
+ resultMatrix[upperRow][upperCol] -= currentRow[upperCol] * upperFactor;
+ }
+ }
+ }
+
+ return resultMatrix;
+}
+
+function createMatrix(rows: number, cols: number, initialValue: number): number[][] {
+ const matrix: number[][] = [];
+
+ for (let r = 0; r < rows; r++) {
+ matrix[r] = [];
+
+ for (let c = 0; c < cols; c++) {
+ matrix[r].push(initialValue);
+ }
+ }
+
+ return matrix;
+}
+
export function inverseMatrixByUSV(matrix: number[][]): number[][] | null {
const matrixUSV = getMatrixUSV(matrix);
diff --git a/packages/engine-formula/src/basics/statistical.ts b/packages/engine-formula/src/basics/statistical.ts
index 8af43db85c..0407491e8e 100644
--- a/packages/engine-formula/src/basics/statistical.ts
+++ b/packages/engine-formula/src/basics/statistical.ts
@@ -15,10 +15,12 @@
*/
import type { ArrayValueObject } from '../engine/value-object/array-value-object';
-import type { BaseValueObject, ErrorValueObject } from '../engine/value-object/base-value-object';
+import type { BaseValueObject } from '../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import { ErrorValueObject } from '../engine/value-object/base-value-object';
import { erf, erfcINV } from './engineering';
-import { calculateCombin, calculateFactorial } from './math';
+import { ErrorType } from './error-type';
+import { calculateCombin, calculateFactorial, calculateMmult, inverseMatrixByLUD, inverseMatrixByUSV, matrixTranspose } from './math';
export function betaCDF(x: number, alpha: number, beta: number): number {
if (x <= 0) {
@@ -777,3 +779,284 @@ export function getTwoArrayNumberValues(
noCalculate,
};
}
+
+// eslint-disable-next-line
+export function checkKnownsArrayDimensions(knownYs: BaseValueObject, knownXs?: BaseValueObject, newXs?: BaseValueObject) {
+ const knownYsRowCount = knownYs.isArray() ? (knownYs as ArrayValueObject).getRowCount() : 1;
+ const knownYsColumnCount = knownYs.isArray() ? (knownYs as ArrayValueObject).getColumnCount() : 1;
+
+ let knownXsRowCount = knownYsRowCount;
+ let knownXsColumnCount = knownYsColumnCount;
+
+ if (knownXs && !knownXs.isNull()) {
+ knownXsRowCount = knownXs.isArray() ? (knownXs as ArrayValueObject).getRowCount() : 1;
+ knownXsColumnCount = knownXs.isArray() ? (knownXs as ArrayValueObject).getColumnCount() : 1;
+
+ if (
+ (knownYsRowCount === 1 && (knownXsColumnCount !== knownYsColumnCount)) ||
+ (knownYsColumnCount === 1 && knownXsRowCount !== knownYsRowCount) ||
+ (knownYsRowCount !== 1 && knownYsColumnCount !== 1 && (knownXsRowCount !== knownYsRowCount || knownXsColumnCount !== knownYsColumnCount))
+ ) {
+ return {
+ isError: true,
+ errorObject: ErrorValueObject.create(ErrorType.REF),
+ };
+ }
+ }
+
+ if (newXs && !newXs.isNull()) {
+ const newXsRowCount = newXs.isArray() ? (newXs as ArrayValueObject).getRowCount() : 1;
+ const newXsColumnCount = newXs.isArray() ? (newXs as ArrayValueObject).getColumnCount() : 1;
+
+ if (
+ (knownYsRowCount === 1 && knownXsRowCount > 1 && newXsRowCount !== knownXsRowCount) ||
+ (knownYsColumnCount === 1 && knownXsColumnCount > 1 && newXsColumnCount !== knownXsColumnCount)
+ ) {
+ return {
+ isError: true,
+ errorObject: ErrorValueObject.create(ErrorType.REF),
+ };
+ }
+ }
+
+ return {
+ isError: false,
+ errorObject: null,
+ };
+}
+
+export function getKnownsArrayValues(array: BaseValueObject): number[][] | ErrorValueObject {
+ const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
+ const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
+
+ const values: number[][] = [];
+
+ for (let r = 0; r < rowCount; r++) {
+ values[r] = [];
+
+ for (let c = 0; c < columnCount; c++) {
+ const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
+
+ if (valueObject.isError() || valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ values[r].push(+valueObject.getValue());
+ }
+ }
+
+ return values;
+}
+
+export function getSerialNumbersByRowsColumns(rowCount: number, columnCount: number): number[][] {
+ const values: number[][] = [];
+
+ let n = 1;
+
+ for (let r = 0; r < rowCount; r++) {
+ values[r] = [];
+
+ for (let c = 0; c < columnCount; c++) {
+ values[r].push(n++);
+ }
+ }
+
+ return values;
+}
+
+export function getSlopeAndIntercept(knownXsValues: number[], knownYsValues: number[], constb: number, isExponentialTransform: boolean) {
+ let Y = knownYsValues;
+
+ if (isExponentialTransform) {
+ Y = knownYsValues.map((value) => Math.log(value));
+ }
+
+ let slope, intercept;
+
+ if (constb) {
+ ({ slope, intercept } = getSlopeAndInterceptOfConstbIsTrue(knownXsValues, Y));
+ } else {
+ ({ slope, intercept } = getSlopeAndInterceptOfConstbIsFalse(knownXsValues, Y));
+ }
+
+ if (isExponentialTransform) {
+ slope = Math.exp(slope);
+ intercept = Math.exp(intercept);
+ }
+
+ if (Number.isNaN(slope) && !constb) {
+ slope = 0;
+ }
+
+ return { slope, intercept, Y };
+}
+
+function getSlopeAndInterceptOfConstbIsTrue(knownXsValues: number[], knownYsValues: number[]) {
+ const n = knownYsValues.length;
+
+ let sumX = 0;
+ let sumY = 0;
+ let sumX2 = 0;
+ let sumXY = 0;
+
+ for (let i = 0; i < n; i++) {
+ sumX += knownXsValues[i];
+ sumY += knownYsValues[i];
+ sumX2 += knownXsValues[i] * knownXsValues[i];
+ sumXY += knownXsValues[i] * knownYsValues[i];
+ }
+
+ const temp = n * sumXY - sumX * sumY;
+ const slope = temp / (n * sumX2 - sumX * sumX);
+ const intercept = 1 / n * sumY - slope * (1 / n) * sumX;
+
+ return {
+ slope,
+ intercept,
+ };
+}
+
+function getSlopeAndInterceptOfConstbIsFalse(knownXsValues: number[], knownYsValues: number[]) {
+ const matrixX = [[...knownXsValues]];
+ const matrixY = [...knownYsValues];
+
+ let rowCount = matrixX.length;
+ let columnCount = matrixX[0].length;
+ let minCount = Math.min(rowCount, columnCount);
+ const newMatrix = new Array(minCount).fill(0);
+
+ for (let i = 0; i < minCount; i++) {
+ const matrixXRow = matrixX[i];
+
+ let sumSquare = 0;
+
+ for (let j = 0; j < columnCount; j++) {
+ sumSquare += matrixXRow[j] ** 2;
+ }
+
+ const value = matrixXRow[i] < 0 ? Math.sqrt(sumSquare) : -Math.sqrt(sumSquare);
+
+ newMatrix[i] = value;
+
+ if (value !== 0) {
+ matrixXRow[i] -= value;
+
+ for (let j = i + 1; j < rowCount; j++) {
+ let sum = 0;
+
+ for (let k = i; k < columnCount; k++) {
+ sum -= matrixX[j][k] * matrixXRow[k];
+ }
+
+ sum /= (value * matrixXRow[i]);
+
+ for (let k = i; k < columnCount; k++) {
+ matrixX[j][k] -= sum * matrixXRow[k];
+ }
+ }
+ }
+ }
+
+ rowCount = matrixX.length;
+ columnCount = matrixX[0].length;
+ minCount = Math.min(rowCount, columnCount);
+ const result = new Array(rowCount).fill(0);
+
+ for (let i = 0; i < minCount; i++) {
+ const matrixXRow = matrixX[i];
+
+ let sum = 0;
+
+ for (let j = 0; j < columnCount; j++) {
+ sum += matrixY[j] * matrixXRow[j];
+ }
+
+ sum /= (newMatrix[i] * matrixXRow[i]);
+
+ for (let j = 0; j < columnCount; j++) {
+ matrixY[j] += sum * matrixXRow[j];
+ }
+ }
+
+ for (let i = newMatrix.length - 1; i >= 0; i--) {
+ matrixY[i] /= newMatrix[i];
+
+ const temp = matrixY[i];
+ const matrixXRow = matrixX[i];
+
+ result[i] = temp;
+
+ for (let j = 0; j < i; j++) {
+ matrixY[j] -= temp * matrixXRow[j];
+ }
+ }
+
+ return {
+ slope: result[0],
+ intercept: 0,
+ };
+}
+
+export function getKnownsArrayCoefficients(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number, isExponentialTransform: boolean) {
+ const isOneRow = knownYsValues.length === 1 && knownYsValues[0].length > 1;
+
+ let Y = knownYsValues;
+
+ if (isExponentialTransform) {
+ Y = knownYsValues.map((row) => row.map((value) => Math.log(value)));
+ }
+
+ let X = knownXsValues;
+ let newX = newXsValues;
+
+ if (isOneRow) {
+ Y = matrixTranspose(Y);
+ X = matrixTranspose(X);
+ newX = matrixTranspose(newX);
+ }
+
+ if (constb) {
+ X = X.map((row) => [...row, 1]);
+ }
+
+ const XT = matrixTranspose(X);
+ const XTX = calculateMmult(XT, X);
+ const XTY = calculateMmult(XT, Y);
+
+ let XTXInverse = inverseMatrixByLUD(XTX);
+
+ if (!XTXInverse) {
+ XTXInverse = inverseMatrixByUSV(XTX);
+
+ if (!XTXInverse) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ let coefficients = calculateMmult(XTXInverse, XTY);
+
+ if (!constb) {
+ coefficients.push([0]);
+ }
+
+ coefficients = matrixTranspose(coefficients);
+
+ const pop = coefficients[0].pop() as number;
+
+ coefficients[0].reverse();
+ coefficients[0].push(pop);
+
+ if (isExponentialTransform) {
+ for (let i = 0; i < coefficients[0].length; i++) {
+ coefficients[0][i] = Math.exp(coefficients[0][i]);
+ }
+ }
+
+ return {
+ coefficients,
+ Y,
+ X,
+ newX,
+ XTXInverse,
+ };
+}
diff --git a/packages/engine-formula/src/functions/statistical/function-map.ts b/packages/engine-formula/src/functions/statistical/function-map.ts
index 0144e40745..aa1073cf67 100644
--- a/packages/engine-formula/src/functions/statistical/function-map.ts
+++ b/packages/engine-formula/src/functions/statistical/function-map.ts
@@ -63,6 +63,8 @@ import { HypgeomDist } from './hypgeom-dist';
import { Intercept } from './intercept';
import { Kurt } from './kurt';
import { Large } from './large';
+import { Linest } from './linest';
+import { Logest } from './logest';
import { LognormDist } from './lognorm-dist';
import { LognormInv } from './lognorm-inv';
import { Max } from './max';
@@ -110,6 +112,7 @@ import { TDistRt } from './t-dist-rt';
import { TInv } from './t-inv';
import { TInv2t } from './t-inv-2t';
import { TTest } from './t-test';
+import { Trend } from './trend';
import { Trimmean } from './trimmean';
import { VarP } from './var-p';
import { VarS } from './var-s';
@@ -169,6 +172,8 @@ export const functionStatistical = [
[Intercept, FUNCTION_NAMES_STATISTICAL.INTERCEPT],
[Kurt, FUNCTION_NAMES_STATISTICAL.KURT],
[Large, FUNCTION_NAMES_STATISTICAL.LARGE],
+ [Linest, FUNCTION_NAMES_STATISTICAL.LINEST],
+ [Logest, FUNCTION_NAMES_STATISTICAL.LOGEST],
[LognormDist, FUNCTION_NAMES_STATISTICAL.LOGNORM_DIST],
[LognormInv, FUNCTION_NAMES_STATISTICAL.LOGNORM_INV],
[Max, FUNCTION_NAMES_STATISTICAL.MAX],
@@ -216,6 +221,7 @@ export const functionStatistical = [
[TInv, FUNCTION_NAMES_STATISTICAL.T_INV],
[TInv2t, FUNCTION_NAMES_STATISTICAL.T_INV_2T],
[TTest, FUNCTION_NAMES_STATISTICAL.T_TEST],
+ [Trend, FUNCTION_NAMES_STATISTICAL.TREND],
[Trimmean, FUNCTION_NAMES_STATISTICAL.TRIMMEAN],
[VarP, FUNCTION_NAMES_STATISTICAL.VAR_P],
[VarS, FUNCTION_NAMES_STATISTICAL.VAR_S],
diff --git a/packages/engine-formula/src/functions/statistical/growth/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/growth/__tests__/index.spec.ts
index 398c2252f3..0771b9d459 100644
--- a/packages/engine-formula/src/functions/statistical/growth/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/statistical/growth/__tests__/index.spec.ts
@@ -113,7 +113,7 @@ describe('Test growth function', () => {
column: 0,
});
const result2 = testFunction.calculate(knownYs2);
- expect(getObjectValue(result2)).toStrictEqual(ErrorType.NAME);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
const knownYs3 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
@@ -205,6 +205,52 @@ describe('Test growth function', () => {
});
const result = testFunction.calculate(knownYs, knownXs);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4, 5],
+ [2, 3, 4, 5, 6],
+ [3, 4, 5, 6, 7],
+ [4, 5, 6, 7, 8],
+ ]),
+ rowCount: 4,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs, knownXs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ ]),
+ rowCount: 1,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ [2, 3, 4, 5],
+ [3, 4, 5, 6],
+ [4, 5, 6, 7],
+ [5, 6, 7, 8],
+ ]),
+ rowCount: 5,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs2, knownXs3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.NA);
});
it('NewXs length error', () => {
@@ -361,10 +407,24 @@ describe('Test growth function', () => {
const knownXs = ErrorValueObject.create(ErrorType.NAME);
const newXs = ErrorValueObject.create(ErrorType.NAME);
const result = testFunction.calculate(knownYs, knownXs);
- expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
const result2 = testFunction.calculate(knownYs, undefined, newXs);
- expect(getObjectValue(result2)).toStrictEqual(ErrorType.NAME);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 17, 18, ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs, knownXs2);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);
});
it('More test', () => {
@@ -497,32 +557,32 @@ describe('Test growth function', () => {
});
const result3 = testFunction.calculate(knownYs2, knownXs2, newXs3, constb);
expect(getObjectValue(result3)).toStrictEqual([
- [1.1161231740339033],
- [1.7617295898720449],
+ [1.1161231740339035],
+ [1.7617295898720453],
[2.780778340631825],
- [4.389282114679539],
- [6.928203230275548],
- [10.935729065914693],
- [17.2613542397969],
- [27.245952089325357],
- [43.00600607235712],
- [67.88225099390945],
- [107.14782470725646],
+ [4.38928211467954],
+ [6.92820323027555],
+ [10.935729065914696],
+ [17.261354239796905],
+ [27.245952089325364],
+ [43.00600607235713],
+ [67.88225099390954],
+ [107.14782470725653],
]);
const result4 = testFunction.calculate(knownYs2, knownXs2, newXs3, constb2);
expect(getObjectValue(result4)).toStrictEqual([
- [1.1161231740339057],
- [1.7617295898720433],
- [2.780778340631814],
- [4.389282114679508],
- [6.928203230275478],
- [10.935729065914547],
- [17.261354239796617],
- [27.245952089324824],
- [43.00600607235614],
- [67.88225099390776],
- [107.14782470725342],
+ [1.1161231740339097],
+ [1.761729589872056],
+ [2.7807783406318434],
+ [4.38928211467957],
+ [6.928203230275601],
+ [10.935729065914781],
+ [17.261354239797047],
+ [27.2459520893256],
+ [43.00600607235752],
+ [67.88225099391019],
+ [107.14782470725761],
]);
const knownYs3 = ArrayValueObject.create({
@@ -562,7 +622,7 @@ describe('Test growth function', () => {
});
const result5 = testFunction.calculate(knownYs3, knownXs3, newXs4, constb);
expect(getObjectValue(result5)).toStrictEqual([
- [1.1161231740339033, 1.7617295898720449, 2.780778340631825, 4.389282114679539, 6.928203230275548, 10.935729065914693, 17.2613542397969],
+ [1.1161231740339035, 1.7617295898720453, 2.780778340631825, 4.38928211467954, 6.92820323027555, 10.935729065914696, 17.261354239796905],
]);
});
});
diff --git a/packages/engine-formula/src/functions/statistical/growth/index.ts b/packages/engine-formula/src/functions/statistical/growth/index.ts
index d756d0144b..060aa1ae12 100644
--- a/packages/engine-formula/src/functions/statistical/growth/index.ts
+++ b/packages/engine-formula/src/functions/statistical/growth/index.ts
@@ -15,9 +15,9 @@
*/
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
import { ErrorType } from '../../../basics/error-type';
-import { calculateMmult, inverseMatrixByUSV, matrixTranspose } from '../../../basics/math';
+import { matrixTranspose } from '../../../basics/math';
+import { checkKnownsArrayDimensions, getKnownsArrayCoefficients, getKnownsArrayValues, getSerialNumbersByRowsColumns, getSlopeAndIntercept } from '../../../basics/statistical';
import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { BooleanValueObject } from '../../../engine/value-object/primitive-object';
@@ -29,21 +29,25 @@ export class Growth extends BaseFunction {
override maxParams = 4;
override calculate(knownYs: BaseValueObject, knownXs?: BaseValueObject, newXs?: BaseValueObject, constb?: BaseValueObject): BaseValueObject {
- const knownYsRowCount = knownYs.isArray() ? (knownYs as ArrayValueObject).getRowCount() : 1;
- const knownYsColumnCount = knownYs.isArray() ? (knownYs as ArrayValueObject).getColumnCount() : 1;
- const knownYsValues = this._getValues(knownYs, knownYsRowCount, knownYsColumnCount);
+ const { isError, errorObject } = checkKnownsArrayDimensions(knownYs, knownXs, newXs);
+
+ if (isError) {
+ return errorObject as ErrorValueObject;
+ }
+
+ const knownYsValues = getKnownsArrayValues(knownYs);
if (knownYsValues instanceof ErrorValueObject) {
return knownYsValues;
}
- const knownXsValues = this._getKnownXsValues(knownYsRowCount, knownYsColumnCount, knownXs);
+ const knownXsValues = this._getKnownXsValues(knownYsValues, knownXs);
if (knownXsValues instanceof ErrorValueObject) {
return knownXsValues;
}
- const newXsValues = this._getNewXsValues(knownXsValues, knownYsRowCount, knownYsColumnCount, newXs);
+ const newXsValues = this._getNewXsValues(knownXsValues, newXs);
if (newXsValues instanceof ErrorValueObject) {
return newXsValues;
@@ -68,290 +72,91 @@ export class Growth extends BaseFunction {
return this._getResult(knownYsValues, knownXsValues, newXsValues, constbValue);
}
- private _getResult(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constbValue: number): BaseValueObject {
+ private _getResult(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number): BaseValueObject {
if ((knownYsValues.length === 1 && knownXsValues.length > 1) || (knownYsValues[0].length === 1 && knownXsValues[0].length > 1)) {
- return this._getResultByMultipleVariables(knownYsValues, knownXsValues, newXsValues, constbValue);
- }
-
- return this._getResultBySimpleVariables(knownYsValues, knownXsValues, newXsValues, constbValue);
- }
-
- private _getResultByMultipleVariables(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constbValue: number): BaseValueObject {
- const isOneRow = knownYsValues.length === 1 && knownYsValues[0].length > 1;
-
- let logYs: number[][] = [];
- let X: number[][] = [];
-
- if (isOneRow) {
- logYs = matrixTranspose(knownYsValues).map((row) => row.map((value) => Math.log(value)));
- X = constbValue ? matrixTranspose(knownXsValues).map((row) => [...row, 1]) : matrixTranspose(knownXsValues);
- } else {
- logYs = knownYsValues.map((row) => row.map((value) => Math.log(value)));
- X = constbValue ? knownXsValues.map((row) => [...row, 1]) : knownXsValues;
- }
-
- const XT = matrixTranspose(X);
- const XTX = calculateMmult(XT, X);
- const XTY = calculateMmult(XT, logYs);
- const XTXInv = inverseMatrixByUSV(XTX);
-
- if (!XTXInv) {
- return ErrorValueObject.create(ErrorType.NA);
- }
-
- const coefficients = calculateMmult(XTXInv, XTY).map((row) => row[0]);
-
- let result: number[][] = [];
-
- if (isOneRow) {
- result = matrixTranspose(newXsValues).map((row) => {
- const newX = constbValue ? [...row, 1] : row;
- const logY = newX.reduce((acc, value, index) => acc + value * coefficients[index], 0);
- return [Math.exp(logY)];
- });
- result = matrixTranspose(result);
- } else {
- result = newXsValues.map((row) => {
- const newX = constbValue ? [...row, 1] : row;
- const logY = newX.reduce((acc, value, index) => acc + value * coefficients[index], 0);
- return [Math.exp(logY)];
- });
- }
-
- return ArrayValueObject.createByArray(result);
- }
+ if (knownYsValues.length === 1 && knownXsValues.length > 1) {
+ const count = constb ? knownXsValues.length + 1 : knownXsValues.length;
- private _getResultBySimpleVariables(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constbValue: number): BaseValueObject {
- const newXsRowCount = newXsValues.length;
- const newXsColumnCount = newXsValues[0].length;
-
- const knownYsValuesFlat = knownYsValues.flat();
- const knownXsValuesFlat = knownXsValues.flat();
- const newXsValuesFlat = newXsValues.flat();
- const logYs = knownYsValuesFlat.map((value) => Math.log(value));
-
- let slope, intercept;
-
- if (constbValue) {
- ({ slope, intercept } = this._getSlopeAndInterceptOfConstbIsTrue(knownXsValuesFlat, logYs));
- } else {
- ({ slope, intercept } = this._getSlopeAndInterceptOfConstbIsFalse(knownXsValuesFlat, logYs));
- }
-
- if (Number.isNaN(slope) && !constbValue) {
- slope = 0;
- }
-
- const result: number[][] = [];
+ if (count > knownYsValues[0].length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
- for (let r = 0; r < newXsRowCount; r++) {
- const row: number[] = [];
+ if (knownYsValues[0].length === 1 && knownXsValues[0].length > 1) {
+ const count = constb ? knownXsValues[0].length + 1 : knownXsValues[0].length;
- for (let c = 0; c < newXsColumnCount; c++) {
- const value = intercept * (slope ** newXsValuesFlat[r * newXsColumnCount + c]);
- row.push(value);
+ if (count > knownYsValues.length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
}
- result.push(row);
+ return this._getResultByMultipleVariables(knownYsValues, knownXsValues, newXsValues, constb);
}
- return ArrayValueObject.createByArray(result);
+ return this._getResultBySimpleVariables(knownYsValues, knownXsValues, newXsValues, constb);
}
- private _getSlopeAndInterceptOfConstbIsTrue(knownXsValuesFlat: number[], logYs: number[]) {
- const n = logYs.length;
+ private _getResultByMultipleVariables(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number): BaseValueObject {
+ const isOneRow = knownYsValues.length === 1 && knownYsValues[0].length > 1;
- let sumX = 0;
- let sumY = 0;
- let sumX2 = 0;
- let sumXY = 0;
+ const _coefficients = getKnownsArrayCoefficients(knownYsValues, knownXsValues, newXsValues, constb, true);
- for (let i = 0; i < n; i++) {
- sumX += knownXsValuesFlat[i];
- sumY += logYs[i];
- sumX2 += knownXsValuesFlat[i] * knownXsValuesFlat[i];
- sumXY += knownXsValuesFlat[i] * logYs[i];
+ if (_coefficients instanceof ErrorValueObject) {
+ return _coefficients;
}
- const temp = n * sumXY - sumX * sumY;
- const slope = temp / (n * sumX2 - sumX * sumX);
- const intercept = 1 / n * sumY - slope * (1 / n) * sumX;
-
- return {
- slope: Math.exp(slope),
- intercept: Math.exp(intercept),
- };
- }
+ const { coefficients, newX } = _coefficients;
- private _getSlopeAndInterceptOfConstbIsFalse(knownXsValuesFlat: number[], logYs: number[]) {
- const matrixX = [[...knownXsValuesFlat]];
- const matrixY = [...logYs];
+ const cl = coefficients[0].length;
+ const b = coefficients[0][cl - 1];
- let rowCount = matrixX.length;
- let columnCount = matrixX[0].length;
- let minCount = Math.min(rowCount, columnCount);
- const newMatrix = new Array(minCount).fill(0);
+ let result: number[][] = [];
- for (let i = 0; i < minCount; i++) {
- const matrixXRow = matrixX[i];
+ for (let i = 0; i < newX.length; i++) {
+ result[i] = [];
- let sumSquare = 0;
+ let value = b;
- for (let j = 0; j < columnCount; j++) {
- sumSquare += matrixXRow[j] ** 2;
+ for (let j = cl - 2; j >= 0; j--) {
+ value *= (coefficients[0][cl - 2 - j] ** newX[i][j]);
}
- const value = matrixXRow[i] < 0 ? Math.sqrt(sumSquare) : -Math.sqrt(sumSquare);
-
- newMatrix[i] = value;
-
- if (value !== 0) {
- matrixXRow[i] -= value;
-
- for (let j = i + 1; j < rowCount; j++) {
- let sum = 0;
-
- for (let k = i; k < columnCount; k++) {
- sum -= matrixX[j][k] * matrixXRow[k];
- }
-
- sum /= (value * matrixXRow[i]);
-
- for (let k = i; k < columnCount; k++) {
- matrixX[j][k] -= sum * matrixXRow[k];
- }
- }
- }
+ result[i].push(value);
}
- rowCount = matrixX.length;
- columnCount = matrixX[0].length;
- minCount = Math.min(rowCount, columnCount);
- const result = new Array(rowCount).fill(0);
-
- for (let i = 0; i < minCount; i++) {
- const matrixXRow = matrixX[i];
-
- let sum = 0;
-
- for (let j = 0; j < columnCount; j++) {
- sum += matrixY[j] * matrixXRow[j];
- }
-
- sum /= (newMatrix[i] * matrixXRow[i]);
-
- for (let j = 0; j < columnCount; j++) {
- matrixY[j] += sum * matrixXRow[j];
- }
+ if (isOneRow) {
+ result = matrixTranspose(result);
}
- for (let i = newMatrix.length - 1; i >= 0; i--) {
- matrixY[i] /= newMatrix[i];
+ return ArrayValueObject.createByArray(result);
+ }
- const temp = matrixY[i];
- const matrixXRow = matrixX[i];
+ private _getResultBySimpleVariables(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number): BaseValueObject {
+ const knownYsValuesFlat = knownYsValues.flat();
+ const knownXsValuesFlat = knownXsValues.flat();
- result[i] = temp;
+ const { slope, intercept } = getSlopeAndIntercept(knownXsValuesFlat, knownYsValuesFlat, constb, true);
- for (let j = 0; j < i; j++) {
- matrixY[j] -= temp * matrixXRow[j];
- }
- }
+ const result = newXsValues.map((row) => {
+ return row.map((value) => intercept * (slope ** value));
+ });
- return {
- slope: Math.exp(result[0]),
- intercept: Math.exp(0),
- };
+ return ArrayValueObject.createByArray(result);
}
- private _getKnownXsValues(knownYsRowCount: number, knownYsColumnCount: number, knownXs?: BaseValueObject): number[][] | ErrorValueObject {
+ private _getKnownXsValues(knownYsValues: number[][], knownXs?: BaseValueObject): number[][] | ErrorValueObject {
if (!knownXs || knownXs.isNull()) {
- return this._getNumberValuesByRowsColumns(knownYsRowCount, knownYsColumnCount);
- }
-
- if (knownXs.isError()) {
- return knownXs as ErrorValueObject;
- }
-
- const knownXsRowCount = knownXs.isArray() ? (knownXs as ArrayValueObject).getRowCount() : 1;
- const knownXsColumnCount = knownXs.isArray() ? (knownXs as ArrayValueObject).getColumnCount() : 1;
-
- if (
- (knownYsRowCount === 1 && knownXsColumnCount !== knownYsColumnCount) ||
- (knownYsColumnCount === 1 && knownXsRowCount !== knownYsRowCount) ||
- (knownYsRowCount !== 1 && knownYsColumnCount !== 1 && (knownXsRowCount !== knownYsRowCount || knownXsColumnCount !== knownYsColumnCount))
- ) {
- return ErrorValueObject.create(ErrorType.REF);
+ return getSerialNumbersByRowsColumns(knownYsValues.length, knownYsValues[0].length);
}
- return this._getValues(knownXs, knownXsRowCount, knownXsColumnCount);
+ return getKnownsArrayValues(knownXs);
}
- private _getNewXsValues(knownXsValues: number[][], knownYsRowCount: number, knownYsColumnCount: number, newXs?: BaseValueObject): number[][] | ErrorValueObject {
+ private _getNewXsValues(knownXsValues: number[][], newXs?: BaseValueObject): number[][] | ErrorValueObject {
if (!newXs || newXs.isNull()) {
return knownXsValues;
}
- if (newXs.isError()) {
- return newXs as ErrorValueObject;
- }
-
- const newXsRowCount = newXs.isArray() ? (newXs as ArrayValueObject).getRowCount() : 1;
- const newXsColumnCount = newXs.isArray() ? (newXs as ArrayValueObject).getColumnCount() : 1;
-
- if (
- (knownYsRowCount === 1 && knownXsValues.length > 1 && newXsRowCount !== knownXsValues.length) ||
- (knownYsColumnCount === 1 && knownXsValues[0].length > 1 && newXsColumnCount !== knownXsValues[0].length)
- ) {
- return ErrorValueObject.create(ErrorType.REF);
- }
-
- return this._getValues(newXs, newXsRowCount, newXsColumnCount);
- }
-
- private _getValues(variant: BaseValueObject, rowCount: number, columnCount: number): number[][] | ErrorValueObject {
- const values: number[][] = [];
-
- for (let r = 0; r < rowCount; r++) {
- values[r] = [];
-
- for (let c = 0; c < columnCount; c++) {
- const valueObject = variant.isArray() ? (variant as ArrayValueObject).get(r, c) as BaseValueObject : variant;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean()) {
- return ErrorValueObject.create(ErrorType.VALUE);
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- return ErrorValueObject.create(ErrorType.VALUE);
- }
-
- values[r].push(+value);
- }
- }
-
- return values;
- }
-
- private _getNumberValuesByRowsColumns(rowCount: number, columnCount: number): number[][] {
- const values: number[][] = [];
-
- let n = 1;
-
- for (let r = 0; r < rowCount; r++) {
- values[r] = [];
-
- for (let c = 0; c < columnCount; c++) {
- values[r].push(n++);
- }
- }
-
- return values;
+ return getKnownsArrayValues(newXs);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts
new file mode 100644
index 0000000000..3ba932cd5b
--- /dev/null
+++ b/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts
@@ -0,0 +1,421 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { describe, expect, it } from 'vitest';
+
+import { ErrorType } from '../../../../basics/error-type';
+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
+import { BooleanValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
+import { getObjectValue } from '../../../__tests__/create-function-test-bed';
+import { FUNCTION_NAMES_STATISTICAL } from '../../function-names';
+import { Linest } from '../index';
+
+describe('Test linest function', () => {
+ const testFunction = new Linest(FUNCTION_NAMES_STATISTICAL.LINEST);
+
+ describe('Linest', () => {
+ it('Value is normal', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [5],
+ [6],
+ [7],
+ [8],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1, -4],
+ [0, 0],
+ [1, 0],
+ [ErrorType.NUM, 2],
+ [5, 0],
+ ]);
+
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, constb2, stats);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [0.4022988505747127, 0],
+ [0.05935606660854279, ErrorType.NA],
+ [0.9386973180076628, 0.7829602926862714],
+ [45.937499999999986, 3],
+ [28.160919540229884, 1.8390804597701154],
+ ]);
+ });
+
+ it('Only knownYs value', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs);
+ expect(getObjectValue(result)).toStrictEqual([
+ [36445.71428571428, -23993.33333333333],
+ ]);
+ });
+
+ it('KnownYs is one column, and knownXs is multiple columns', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [14200],
+ [14400],
+ [15100],
+ [150000],
+ [13900],
+ [16900],
+ [12600],
+ [14290],
+ [16300],
+ [16900],
+ [14900],
+ ]),
+ rowCount: 11,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2310, 2, 2, 20],
+ [2333, 2, 2, 1.2],
+ [2356, 3, 1.5, 33],
+ [2379, 3, 2, 43],
+ [2402, 2, 3, 53],
+ [2425, 4, 2, 23],
+ [2448, 2, 1.5, 99],
+ [2471, 2, 2, 34],
+ [2494, 3, 3, 23],
+ [2517, 4, 4, 55],
+ [2540, 2, 3, 22],
+ ]),
+ rowCount: 11,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [204.08084300495466, -3407.546483120881, 12743.55283648931, -127.71202270661888, 303849.43123394996],
+ [651.0984780422234, 27243.518202120504, 20457.15431476776, 281.25469710804725, 634296.6471140139],
+ [0.10746482202373192, 49689.654678752704, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [0.18060602765382994, 6, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [1783709761.9833107, 14814370692.562143, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, constb2, stats);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [104.93150874658363, -10969.550675692437, 12875.375874994475, 6.253966373496326, 0],
+ [582.3580513522538, 20946.225096188875, 19296.67873890206, 28.224249026281957, ErrorType.NA],
+ [0.3786001300516361, 46875.144599683044, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [1.066222024870103, 7, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [9371149831.311636, 15380954268.688364, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+
+ const stats2 = BooleanValueObject.create(false);
+ const result3 = testFunction.calculate(knownYs, knownXs, constb, stats2);
+ expect(getObjectValue(result3)).toStrictEqual([
+ [204.08084300495466, -3407.546483120881, 12743.55283648931, -127.71202270661888, 303849.43123394996],
+ ]);
+ });
+
+ it('KnownYs is one row, and knownXs is multiple rows', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2310, 2, 2, 20, 14200],
+ ]),
+ rowCount: 1,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2333, 2, 2, 1.2, 14400],
+ [2356, 3, 1.5, 33, 15100],
+ [2379, 3, 2, 43, 150000],
+ [2402, 2, 3, 53, 13900],
+ ]),
+ rowCount: 4,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [0.2595190981297719, 0.0006299216330276636, 0.17259280062947369, 0.5480680786089209, -0.13484243565471843],
+ [0, 0, 0, 0, 0],
+ [0.9999999999999984, 0, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [ErrorType.NUM, 0, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [152301876.79999977, 2.446228251051695e-7, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, constb2, stats);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [0.2546042576432228, 0.0005859795492142439, 0.17634578794240952, 0.5493251606822014, 0],
+ [0.07725154448946374, 0.001052591394934137, 0.12712504469774474, 0.048004226190790644, ErrorType.NA],
+ [0.9999999998277429, 0.18882049421003608, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [1451318743.3340783, 1, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [206976507.96434683, 0.03565317903372227, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+ });
+
+ it('Stats value test', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [5],
+ [6],
+ [7],
+ [8],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(false);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1, -4],
+ ]);
+
+ const stats2 = StringValueObject.create('test');
+ const result2 = testFunction.calculate(knownYs, knownXs, constb, stats2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+
+ const constb2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [true],
+ ]),
+ rowCount: 1,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const stats3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs, knownXs, constb2, stats3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.NAME);
+ });
+
+ it('KnownXs length error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ [5],
+ ]),
+ rowCount: 5,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs, knownXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4, 5],
+ [2, 3, 4, 5, 6],
+ [3, 4, 5, 6, 7],
+ [4, 5, 6, 7, 8],
+ ]),
+ rowCount: 4,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs, knownXs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ ]),
+ rowCount: 1,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ [2, 3, 4, 5],
+ [3, 4, 5, 6],
+ [4, 5, 6, 7],
+ [5, 6, 7, 8],
+ ]),
+ rowCount: 5,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs2, knownXs3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.NA);
+ });
+
+ it('Value is error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, 220000],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 17, 18, ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs, knownXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 17, 18, 19],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs2, knownXs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+ });
+ });
+});
diff --git a/packages/engine-formula/src/functions/statistical/linest/index.ts b/packages/engine-formula/src/functions/statistical/linest/index.ts
new file mode 100644
index 0000000000..e76dcb9015
--- /dev/null
+++ b/packages/engine-formula/src/functions/statistical/linest/index.ts
@@ -0,0 +1,265 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
+import { ErrorType } from '../../../basics/error-type';
+import { checkKnownsArrayDimensions, getKnownsArrayCoefficients, getKnownsArrayValues, getSerialNumbersByRowsColumns, getSlopeAndIntercept } from '../../../basics/statistical';
+import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
+import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
+import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { BooleanValueObject } from '../../../engine/value-object/primitive-object';
+import { BaseFunction } from '../../base-function';
+
+export class Linest extends BaseFunction {
+ override minParams = 1;
+
+ override maxParams = 4;
+
+ override calculate(knownYs: BaseValueObject, knownXs?: BaseValueObject, constb?: BaseValueObject, stats?: BaseValueObject): BaseValueObject {
+ const { isError, errorObject } = checkKnownsArrayDimensions(knownYs, knownXs);
+
+ if (isError) {
+ return errorObject as ErrorValueObject;
+ }
+
+ const knownYsValues = getKnownsArrayValues(knownYs);
+
+ if (knownYsValues instanceof ErrorValueObject) {
+ return knownYsValues;
+ }
+
+ const knownXsValues = this._getKnownXsValues(knownYsValues, knownXs);
+
+ if (knownXsValues instanceof ErrorValueObject) {
+ return knownXsValues;
+ }
+
+ let _constb = constb ?? BooleanValueObject.create(true);
+
+ if (_constb.isArray()) {
+ _constb = (_constb as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ let _stats = stats ?? BooleanValueObject.create(false);
+
+ if (_stats.isArray()) {
+ _stats = (_stats as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ const { isError: _isError, errorObject: _errorObject, variants } = checkVariantsErrorIsStringToNumber(_constb, _stats);
+
+ if (_isError) {
+ return _errorObject as ErrorValueObject;
+ }
+
+ const [constbObject, statsObject] = variants as BaseValueObject[];
+
+ return this._getResult(knownYsValues, knownXsValues, +constbObject.getValue(), +statsObject.getValue());
+ }
+
+ private _getResult(knownYsValues: number[][], knownXsValues: number[][], constb: number, stats: number): BaseValueObject {
+ if ((knownYsValues.length === 1 && knownXsValues.length > 1) || (knownYsValues[0].length === 1 && knownXsValues[0].length > 1)) {
+ if (knownYsValues.length === 1 && knownXsValues.length > 1) {
+ const count = constb ? knownXsValues.length + 1 : knownXsValues.length;
+
+ if (count > knownYsValues[0].length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ if (knownYsValues[0].length === 1 && knownXsValues[0].length > 1) {
+ const count = constb ? knownXsValues[0].length + 1 : knownXsValues[0].length;
+
+ if (count > knownYsValues.length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ return this._getResultByMultipleVariables(knownYsValues, knownXsValues, constb, stats);
+ }
+
+ return this._getResultBySimpleVariables(knownYsValues, knownXsValues, constb, stats);
+ }
+
+ private _getResultByMultipleVariables(knownYsValues: number[][], knownXsValues: number[][], constb: number, stats: number): BaseValueObject {
+ const _coefficients = getKnownsArrayCoefficients(knownYsValues, knownXsValues, knownXsValues, constb, false);
+
+ if (_coefficients instanceof ErrorValueObject) {
+ return _coefficients;
+ }
+
+ const { coefficients, X, XTXInverse } = _coefficients;
+
+ let result: Array<Array<number | string>> = [];
+
+ if (stats) {
+ const _knownYsValues = knownYsValues.flat();
+ const n = _knownYsValues.length;
+ const meanY = !constb ? 0 : _knownYsValues.reduce((acc, value) => acc + value, 0) / n;
+
+ const k = XTXInverse.length;
+ const df = n - k;
+
+ const cl = coefficients[0].length;
+ const fillNa = new Array(cl - 2).fill(ErrorType.NA);
+ const b = coefficients[0][cl - 1];
+
+ const newY = [];
+
+ for (let i = 0; i < X.length; i++) {
+ let value = b;
+
+ for (let j = cl - 2; j >= 0; j--) {
+ value += coefficients[0][cl - 2 - j] * X[i][j];
+ }
+
+ newY.push(value);
+ }
+
+ let sstotal = 0;
+ let ssresid = 0;
+
+ for (let i = 0; i < n; i++) {
+ sstotal += (_knownYsValues[i] - meanY) ** 2;
+ ssresid += (_knownYsValues[i] - newY[i]) ** 2;
+ }
+
+ const ssreg = sstotal - ssresid;
+ const r2 = sstotal === 0 ? 0 : ssreg / sstotal;
+
+ const seList = [];
+
+ for (let i = k - 1; i >= 0; i--) {
+ const se = df > 0 ? Math.sqrt(ssresid / df * XTXInverse[i][i]) : 0;
+ seList.push(se);
+ }
+
+ // seb = #N/A when const is FALSE
+ if (constb) {
+ const seb = seList.shift();
+ seList.push(seb);
+ } else {
+ seList.push(ErrorType.NA);
+ }
+
+ const sey = df > 0 ? Math.sqrt(ssresid / df) : 0;
+ const F = df > 0 ? (ssreg / (cl - 1)) / (ssresid / df) : ErrorType.NUM;
+
+ result = [
+ coefficients[0], // [mn, mn-1, ..., m1, b]
+ [...seList], // [sen, sen-1, ..., se1, seb]
+ [r2, sey, ...fillNa], // [r2, sey]
+ [F, df, ...fillNa], // [F, df]
+ [ssreg, ssresid, ...fillNa], // [ssreg, ssresid]
+ ];
+ } else {
+ result = [
+ coefficients[0], // [mn, mn-1, ..., m1, b]
+ ];
+ }
+
+ return ArrayValueObject.createByArray(result);
+ }
+
+ private _getResultBySimpleVariables(knownYsValues: number[][], knownXsValues: number[][], constb: number, stats: number): BaseValueObject {
+ const knownYsValuesFlat = knownYsValues.flat();
+ const knownXsValuesFlat = knownXsValues.flat();
+ const { slope: m, intercept: b } = getSlopeAndIntercept(knownXsValuesFlat, knownYsValuesFlat, constb, false);
+
+ if (Number.isNaN(m)) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+
+ let result: Array<Array<number | string>> = [];
+
+ if (stats) {
+ const n = knownYsValues.length;
+
+ let meanY = 0;
+ let meanX = 0;
+ let df = n - 1;
+
+ if (constb) {
+ let sumY = 0;
+ let sumX = 0;
+
+ for (let i = 0; i < n; i++) {
+ sumY += knownYsValuesFlat[i];
+ sumX += knownXsValuesFlat[i];
+ }
+
+ meanY = sumY / n;
+ meanX = sumX / n;
+ df = n - 2;
+ }
+
+ let sstotal = 0;
+ let ssresid = 0;
+ let ssx = 0;
+
+ for (let i = 0; i < n; i++) {
+ sstotal += (knownYsValuesFlat[i] - meanY) ** 2;
+ ssresid += (knownYsValuesFlat[i] - (m * knownXsValuesFlat[i] + b)) ** 2;
+ ssx += (knownXsValuesFlat[i] - meanX) ** 2;
+ }
+
+ const ssreg = sstotal - ssresid;
+ const r2 = sstotal === 0 ? 0 : ssreg / sstotal;
+
+ let se = 0;
+ let seb: number | ErrorType = 0;
+ let sey = 0;
+ let F = 0;
+
+ if (df > 0) {
+ if (ssx > 0) {
+ se = Math.sqrt(ssresid / df / ssx);
+ seb = Math.sqrt(ssresid / df * (1 / n + meanX ** 2 / ssx));
+ }
+
+ sey = Math.sqrt(ssresid / df);
+ F = (ssreg / 1) / (ssresid / df);
+ }
+
+ // seb = #N/A when const is FALSE
+ if (!constb) {
+ seb = ErrorType.NA;
+ }
+
+ result = [
+ [m, b], // [m, b]
+ [se, seb], // [se, seb]
+ [r2, sey], // [r2, sey]
+ [F, df], // [F, df]
+ [ssreg, ssresid], // [ssreg, ssresid]
+ ];
+ } else {
+ result = [
+ [m, b], // [m, b]
+ ];
+ }
+
+ return ArrayValueObject.createByArray(result);
+ }
+
+ private _getKnownXsValues(knownYsValues: number[][], knownXs?: BaseValueObject): number[][] | ErrorValueObject {
+ if (!knownXs || knownXs.isNull()) {
+ return getSerialNumbersByRowsColumns(knownYsValues.length, knownYsValues[0].length);
+ }
+
+ return getKnownsArrayValues(knownXs);
+ }
+}
diff --git a/packages/engine-formula/src/functions/statistical/logest/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/logest/__tests__/index.spec.ts
new file mode 100644
index 0000000000..8798a58dbc
--- /dev/null
+++ b/packages/engine-formula/src/functions/statistical/logest/__tests__/index.spec.ts
@@ -0,0 +1,421 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { describe, expect, it } from 'vitest';
+
+import { ErrorType } from '../../../../basics/error-type';
+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
+import { BooleanValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
+import { getObjectValue } from '../../../__tests__/create-function-test-bed';
+import { FUNCTION_NAMES_STATISTICAL } from '../../function-names';
+import { Logest } from '../index';
+
+describe('Test logest function', () => {
+ const testFunction = new Logest(FUNCTION_NAMES_STATISTICAL.LOGEST);
+
+ describe('Logest', () => {
+ it('Value is normal', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [5],
+ [6],
+ [7],
+ [8],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1.5784365299976528, 0.11391384443661104],
+ [0.06522558747032602, 0.4301922951970874],
+ [0.9607604883077162, 0.1458488474560075],
+ [48.96903385760741, 2],
+ [1.0416637206477855, 0.0425437726084915],
+ ]);
+
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, constb2, stats);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [1.1409215862229016, 1],
+ [0.033475302155726666, ErrorType.NA],
+ [0.8379284156943257, 0.4415695626609612],
+ [15.510339198892915, 3],
+ [3.024262994397811, 0.5849510360057776],
+ ]);
+ });
+
+ it('Only knownYs value', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1.463275628116175, 22291.223298465353],
+ ]);
+ });
+
+ it('KnownYs is one column, and knownXs is multiple columns', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [14200],
+ [14400],
+ [15100],
+ [150000],
+ [13900],
+ [16900],
+ [12600],
+ [14290],
+ [16300],
+ [16900],
+ [14900],
+ ]),
+ rowCount: 11,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2310, 2, 2, 20],
+ [2333, 2, 2, 1.2],
+ [2356, 3, 1.5, 33],
+ [2379, 3, 2, 43],
+ [2402, 2, 3, 53],
+ [2425, 4, 2, 23],
+ [2448, 2, 1.5, 99],
+ [2471, 2, 2, 34],
+ [2494, 3, 3, 23],
+ [2517, 4, 4, 55],
+ [2540, 2, 3, 22],
+ ]),
+ rowCount: 11,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1.0022562631143823, 0.9559927432167645, 1.3220173520215017, 0.9979673263194307, 1251594.7569785423],
+ [0.01111894485530442, 0.4652432570026924, 0.3493511018583655, 0.004803045273340239, 10.832016475252525],
+ [0.12342791182815234, 0.8485606231387034, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [0.21121122864903766, 6, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [0.6083349157737983, 4.320330786849268, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, constb2, stats);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [0.9976750487463316, 0.6740699096298128, 1.330094504910363, 1.0041640514348724, 1],
+ [0.01104236602940002, 0.3971712658719937, 0.3658934383957033, 0.0005351733146462942, ErrorType.NA],
+ [0.9948092355194054, 0.8888217534708093, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [335.3872379814746, 7, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [1059.8291850403075, 5.530028766100469, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+
+ const stats2 = BooleanValueObject.create(false);
+ const result3 = testFunction.calculate(knownYs, knownXs, constb, stats2);
+ expect(getObjectValue(result3)).toStrictEqual([
+ [1.0022562631143823, 0.9559927432167645, 1.3220173520215017, 0.9979673263194307, 1251594.7569785423],
+ ]);
+ });
+
+ it('KnownYs is one row, and knownXs is multiple rows', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2310, 2, 2, 20, 14200],
+ ]),
+ rowCount: 1,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2333, 2, 2, 1.2, 14400],
+ [2356, 3, 1.5, 33, 15100],
+ [2379, 3, 2, 43, 150000],
+ [2402, 2, 3, 53, 13900],
+ ]),
+ rowCount: 4,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1.0324032624317834, 0.999873102005119, 1.0215735856682533, 0.950065530364825, 1.9506552207912204],
+ [0, 0, 0, 0, 0],
+ [1, 0, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [ErrorType.NUM, 0, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [67.25895433112856, 0, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, constb2, stats);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [1.0578548634220857, 1.000090838078794, 1.0027512055146457, 0.9441658222150167, 1],
+ [0.38279330040800214, 0.005215752470850025, 0.6299241749270339, 0.23786833387662884, ErrorType.NA],
+ [0.9945738822026658, 0.9356346289385562, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [45.82345607624384, 1, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ [160.4576424421718, 0.8754121588689897, ErrorType.NA, ErrorType.NA, ErrorType.NA],
+ ]);
+ });
+
+ it('Stats value test', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [5],
+ [6],
+ [7],
+ [8],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(false);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [1.5784365299976528, 0.11391384443661104],
+ ]);
+
+ const stats2 = StringValueObject.create('test');
+ const result2 = testFunction.calculate(knownYs, knownXs, constb, stats2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+
+ const constb2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [true],
+ ]),
+ rowCount: 1,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const stats3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs, knownXs, constb2, stats3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.NAME);
+ });
+
+ it('KnownXs length error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ [5],
+ ]),
+ rowCount: 5,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs, knownXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4, 5],
+ [2, 3, 4, 5, 6],
+ [3, 4, 5, 6, 7],
+ [4, 5, 6, 7, 8],
+ ]),
+ rowCount: 4,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs, knownXs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ ]),
+ rowCount: 1,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ [2, 3, 4, 5],
+ [3, 4, 5, 6],
+ [4, 5, 6, 7],
+ [5, 6, 7, 8],
+ ]),
+ rowCount: 5,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs2, knownXs3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.NA);
+ });
+
+ it('Value is error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, 220000],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 17, 18, ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs, knownXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 17, 18, 19],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs2, knownXs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+ });
+ });
+});
diff --git a/packages/engine-formula/src/functions/statistical/logest/index.ts b/packages/engine-formula/src/functions/statistical/logest/index.ts
new file mode 100644
index 0000000000..ec62db47a6
--- /dev/null
+++ b/packages/engine-formula/src/functions/statistical/logest/index.ts
@@ -0,0 +1,275 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
+import { ErrorType } from '../../../basics/error-type';
+import { checkKnownsArrayDimensions, getKnownsArrayCoefficients, getKnownsArrayValues, getSerialNumbersByRowsColumns, getSlopeAndIntercept } from '../../../basics/statistical';
+import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
+import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
+import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { BooleanValueObject } from '../../../engine/value-object/primitive-object';
+import { BaseFunction } from '../../base-function';
+
+export class Logest extends BaseFunction {
+ override minParams = 1;
+
+ override maxParams = 4;
+
+ override calculate(knownYs: BaseValueObject, knownXs?: BaseValueObject, constb?: BaseValueObject, stats?: BaseValueObject): BaseValueObject {
+ const { isError, errorObject } = checkKnownsArrayDimensions(knownYs, knownXs);
+
+ if (isError) {
+ return errorObject as ErrorValueObject;
+ }
+
+ const knownYsValues = getKnownsArrayValues(knownYs);
+
+ if (knownYsValues instanceof ErrorValueObject) {
+ return knownYsValues;
+ }
+
+ const knownXsValues = this._getKnownXsValues(knownYsValues, knownXs);
+
+ if (knownXsValues instanceof ErrorValueObject) {
+ return knownXsValues;
+ }
+
+ let _constb = constb ?? BooleanValueObject.create(true);
+
+ if (_constb.isArray()) {
+ _constb = (_constb as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ let _stats = stats ?? BooleanValueObject.create(false);
+
+ if (_stats.isArray()) {
+ _stats = (_stats as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ const { isError: _isError, errorObject: _errorObject, variants } = checkVariantsErrorIsStringToNumber(_constb, _stats);
+
+ if (_isError) {
+ return _errorObject as ErrorValueObject;
+ }
+
+ const [constbObject, statsObject] = variants as BaseValueObject[];
+
+ return this._getResult(knownYsValues, knownXsValues, +constbObject.getValue(), +statsObject.getValue());
+ }
+
+ private _getResult(knownYsValues: number[][], knownXsValues: number[][], constb: number, stats: number): BaseValueObject {
+ if ((knownYsValues.length === 1 && knownXsValues.length > 1) || (knownYsValues[0].length === 1 && knownXsValues[0].length > 1)) {
+ if (knownYsValues.length === 1 && knownXsValues.length > 1) {
+ const count = constb ? knownXsValues.length + 1 : knownXsValues.length;
+
+ if (count > knownYsValues[0].length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ if (knownYsValues[0].length === 1 && knownXsValues[0].length > 1) {
+ const count = constb ? knownXsValues[0].length + 1 : knownXsValues[0].length;
+
+ if (count > knownYsValues.length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ return this._getResultByMultipleVariables(knownYsValues, knownXsValues, constb, stats);
+ }
+
+ return this._getResultBySimpleVariables(knownYsValues, knownXsValues, constb, stats);
+ }
+
+ // eslint-disable-next-line max-lines-per-function
+ private _getResultByMultipleVariables(knownYsValues: number[][], knownXsValues: number[][], constb: number, stats: number): BaseValueObject {
+ const _coefficients = getKnownsArrayCoefficients(knownYsValues, knownXsValues, knownXsValues, constb, true);
+
+ if (_coefficients instanceof ErrorValueObject) {
+ return _coefficients;
+ }
+
+ const { coefficients, Y, X, XTXInverse } = _coefficients;
+
+ let result: Array<Array<number | string>> = [];
+
+ if (stats) {
+ const _knownYsValues = Y.flat();
+ const n = _knownYsValues.length;
+ const meanY = !constb ? 0 : _knownYsValues.reduce((acc, value) => acc + value, 0) / n;
+
+ const k = XTXInverse.length;
+ const df = n - k;
+
+ const cl = coefficients[0].length;
+ const fillNa = new Array(cl - 2).fill(ErrorType.NA);
+ const b = coefficients[0][cl - 1];
+
+ const newY = [];
+
+ for (let i = 0; i < X.length; i++) {
+ let value = b;
+
+ for (let j = cl - 2; j >= 0; j--) {
+ value *= coefficients[0][cl - 2 - j] ** X[i][j];
+ }
+
+ newY.push(Math.log(value));
+ }
+
+ let sstotal = 0;
+ let ssresid = 0;
+
+ for (let i = 0; i < n; i++) {
+ sstotal += (_knownYsValues[i] - meanY) ** 2;
+
+ if (!constb && !Number.isFinite(newY[i])) {
+ continue;
+ }
+
+ ssresid += (_knownYsValues[i] - newY[i]) ** 2;
+ }
+
+ if (!Number.isFinite(ssresid)) {
+ ssresid = 0;
+ }
+
+ const ssreg = sstotal - ssresid;
+ const r2 = sstotal === 0 ? 0 : ssreg / sstotal;
+
+ const seList = [];
+
+ for (let i = k - 1; i >= 0; i--) {
+ const se = df > 0 ? Math.sqrt(ssresid / df * XTXInverse[i][i]) : 0;
+ seList.push(se);
+ }
+
+ // seb = #N/A when const is FALSE
+ if (constb) {
+ const seb = seList.shift();
+ seList.push(seb);
+ } else {
+ seList.push(ErrorType.NA);
+ }
+
+ const sey = df > 0 ? Math.sqrt(ssresid / df) : 0;
+ const F = df > 0 ? (ssreg / (cl - 1)) / (ssresid / df) : ErrorType.NUM;
+
+ result = [
+ coefficients[0], // [mn, mn-1, ..., m1, b]
+ [...seList], // [sen, sen-1, ..., se1, seb]
+ [r2, sey, ...fillNa], // [r2, sey]
+ [F, df, ...fillNa], // [F, df]
+ [ssreg, ssresid, ...fillNa], // [ssreg, ssresid]
+ ];
+ } else {
+ result = [
+ coefficients[0], // [mn, mn-1, ..., m1, b]
+ ];
+ }
+
+ return ArrayValueObject.createByArray(result);
+ }
+
+ private _getResultBySimpleVariables(knownYsValues: number[][], knownXsValues: number[][], constb: number, stats: number): BaseValueObject {
+ const knownYsValuesFlat = knownYsValues.flat();
+ const knownXsValuesFlat = knownXsValues.flat();
+ const { slope: m, intercept: b, Y } = getSlopeAndIntercept(knownXsValuesFlat, knownYsValuesFlat, constb, true);
+
+ if (Number.isNaN(m)) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+
+ let result: Array<Array<number | string>> = [];
+
+ if (stats) {
+ const n = Y.length;
+
+ let meanY = 0;
+ let meanX = 0;
+ let df = n - 1;
+
+ if (constb) {
+ let sumY = 0;
+ let sumX = 0;
+
+ for (let i = 0; i < n; i++) {
+ sumY += Y[i];
+ sumX += knownXsValuesFlat[i];
+ }
+
+ meanY = sumY / n;
+ meanX = sumX / n;
+ df = n - 2;
+ }
+
+ let sstotal = 0;
+ let ssresid = 0;
+ let ssx = 0;
+
+ for (let i = 0; i < n; i++) {
+ sstotal += (Y[i] - meanY) ** 2;
+ ssresid += (Y[i] - Math.log(b * (m ** knownXsValuesFlat[i]))) ** 2;
+ ssx += (knownXsValuesFlat[i] - meanX) ** 2;
+ }
+
+ const ssreg = sstotal - ssresid;
+ const r2 = sstotal === 0 ? 0 : ssreg / sstotal;
+
+ let se = 0;
+ let seb: number | ErrorType = 0;
+ let sey = 0;
+ let F = 0;
+
+ if (df > 0) {
+ if (ssx > 0) {
+ se = Math.sqrt(ssresid / df / ssx);
+ seb = Math.sqrt(ssresid / df * (1 / n + meanX ** 2 / ssx));
+ }
+
+ sey = Math.sqrt(ssresid / df);
+ F = (ssreg / 1) / (ssresid / df);
+ }
+
+ // seb = #N/A when const is FALSE
+ if (!constb) {
+ seb = ErrorType.NA;
+ }
+
+ result = [
+ [m, b], // [m, b]
+ [se, seb], // [se, seb]
+ [r2, sey], // [r2, sey]
+ [F, df], // [F, df]
+ [ssreg, ssresid], // [ssreg, ssresid]
+ ];
+ } else {
+ result = [
+ [m, b], // [m, b]
+ ];
+ }
+
+ return ArrayValueObject.createByArray(result);
+ }
+
+ private _getKnownXsValues(knownYsValues: number[][], knownXs?: BaseValueObject): number[][] | ErrorValueObject {
+ if (!knownXs || knownXs.isNull()) {
+ return getSerialNumbersByRowsColumns(knownYsValues.length, knownYsValues[0].length);
+ }
+
+ return getKnownsArrayValues(knownXs);
+ }
+}
diff --git a/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts
new file mode 100644
index 0000000000..e0456b6fb1
--- /dev/null
+++ b/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts
@@ -0,0 +1,629 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { describe, expect, it } from 'vitest';
+
+import { ErrorType } from '../../../../basics/error-type';
+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
+import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
+import { BooleanValueObject } from '../../../../engine/value-object/primitive-object';
+import { getObjectValue } from '../../../__tests__/create-function-test-bed';
+import { FUNCTION_NAMES_STATISTICAL } from '../../function-names';
+import { Trend } from '../index';
+
+describe('Test trend function', () => {
+ const testFunction = new Trend(FUNCTION_NAMES_STATISTICAL.TREND);
+
+ describe('Trend', () => {
+ it('Value is normal', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, 220000],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [11, 1.2, 1.3, 14, 15, 16],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const newXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [17, 18],
+ ]),
+ rowCount: 1,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, newXs, constb);
+ expect(getObjectValue(result)).toStrictEqual([
+ [155404.53583526541, 162554.58675507212],
+ ]);
+ });
+
+ it('Only knownYs value', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs);
+ expect(getObjectValue(result)).toStrictEqual([
+ [12452.380952380954],
+ [48898.09523809524],
+ [85343.80952380951],
+ [121789.5238095238],
+ [158235.2380952381],
+ [194680.95238095237],
+ ]);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [ErrorType.NAME],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+
+ const knownYs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [null],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);
+
+ const knownYs4 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [true],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result4 = testFunction.calculate(knownYs4);
+ expect(getObjectValue(result4)).toStrictEqual(ErrorType.VALUE);
+
+ const knownYs5 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ ['test'],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result5 = testFunction.calculate(knownYs5);
+ expect(getObjectValue(result5)).toStrictEqual(ErrorType.VALUE);
+ });
+
+ it('KnownXs length error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ [5],
+ ]),
+ rowCount: 5,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs, knownXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4, 5],
+ [2, 3, 4, 5, 6],
+ [3, 4, 5, 6, 7],
+ [4, 5, 6, 7, 8],
+ ]),
+ rowCount: 4,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs, knownXs2);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ ]),
+ rowCount: 1,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ [2, 3, 4, 5],
+ [3, 4, 5, 6],
+ [4, 5, 6, 7],
+ [5, 6, 7, 8],
+ ]),
+ rowCount: 5,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs2, knownXs3);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.NA);
+ });
+
+ it('NewXs length error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2],
+ [2, 3],
+ [3, 4],
+ [4, 5],
+ ]),
+ rowCount: 4,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const newXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3],
+ [2, 3, 4],
+ [3, 4, 5],
+ [4, 5, 6],
+ ]),
+ rowCount: 4,
+ columnCount: 3,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(knownYs, knownXs, newXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+ });
+
+ it('Constb value test', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, 220000],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [11, 1.2, 1.3, 14, 15, 16],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const newXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [17, 18],
+ ]),
+ rowCount: 1,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(false);
+ const result = testFunction.calculate(knownYs, knownXs, newXs, constb);
+ expect(getObjectValue(result)).toStrictEqual([
+ [163575.8491131277, 173197.95788448816],
+ ]);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100],
+ [47300],
+ [69000],
+ [102000],
+ [150000],
+ [220000],
+ ]),
+ rowCount: 6,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result2 = testFunction.calculate(knownYs2, undefined, undefined, constb);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [30908.79120879121],
+ [61817.58241758242],
+ [92726.37362637362],
+ [123635.16483516483],
+ [154543.95604395604],
+ [185452.74725274724],
+ ]);
+
+ const constb2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ ['test', 1],
+ [2, ErrorType.NAME],
+ ]),
+ rowCount: 2,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs2, undefined, undefined, constb2);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);
+
+ const result4 = testFunction.calculate(knownYs2, undefined, undefined, constb);
+ expect(getObjectValue(result4)).toStrictEqual([
+ [30908.79120879121],
+ [61817.58241758242],
+ [92726.37362637362],
+ [123635.16483516483],
+ [154543.95604395604],
+ [185452.74725274724],
+ ]);
+ });
+
+ it('Value is error', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [33100, 47300, 69000, 102000, 150000, 220000],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ErrorValueObject.create(ErrorType.NAME);
+ const newXs = ErrorValueObject.create(ErrorType.NAME);
+ const result = testFunction.calculate(knownYs, knownXs);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+
+ const result2 = testFunction.calculate(knownYs, undefined, newXs);
+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
+
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 17, 18, ErrorType.NAME],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs, knownXs2);
+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);
+ });
+
+ it('More test', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2],
+ [2, 3],
+ ]),
+ rowCount: 2,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [3, 4],
+ [4, 5],
+ ]),
+ rowCount: 2,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const newXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [5, 6, 7, 8, 9, 10, 11],
+ ]),
+ rowCount: 1,
+ columnCount: 7,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, newXs, constb);
+ expect(getObjectValue(result)).toStrictEqual([
+ [3, 4, 5, 6, 7, 8, 9],
+ ]);
+
+ const newXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [5],
+ [6],
+ [7],
+ [8],
+ [9],
+ [10],
+ [11],
+ [12],
+ [13],
+ [14],
+ [15],
+ ]),
+ rowCount: 11,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(knownYs, knownXs, newXs2, constb2);
+ expect(getObjectValue(result2)).toStrictEqual([
+ [2.5757575757575757],
+ [3.090909090909091],
+ [3.606060606060606],
+ [4.121212121212121],
+ [4.636363636363637],
+ [5.151515151515151],
+ [5.666666666666666],
+ [6.181818181818182],
+ [6.696969696969697],
+ [7.212121212121212],
+ [7.727272727272727],
+ ]);
+
+ const knownYs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1],
+ [2],
+ [3],
+ [4],
+ ]),
+ rowCount: 4,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2],
+ [2, 3],
+ [3, 4],
+ [4, 5],
+ ]),
+ rowCount: 4,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const newXs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2],
+ [2, 3],
+ [3, 4],
+ [4, 5],
+ [5, 6],
+ [6, 7],
+ [7, 8],
+ [8, 9],
+ [9, 10],
+ [10, 11],
+ [11, 12],
+ ]),
+ rowCount: 11,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result3 = testFunction.calculate(knownYs2, knownXs2, newXs3, constb);
+ expect(getObjectValue(result3)).toStrictEqual([
+ [0.9999999999999902],
+ [1.9999999999999938],
+ [2.9999999999999973],
+ [4.000000000000001],
+ [5.000000000000004],
+ [6.000000000000008],
+ [7.0000000000000115],
+ [8.000000000000014],
+ [9.000000000000018],
+ [10.000000000000021],
+ [11.000000000000025],
+ ]);
+
+ const result4 = testFunction.calculate(knownYs2, knownXs2, newXs3, constb2);
+ expect(getObjectValue(result4)).toStrictEqual([
+ [1],
+ [2],
+ [3],
+ [4],
+ [5],
+ [6],
+ [7],
+ [8],
+ [9],
+ [10],
+ [11],
+ ]);
+
+ const knownYs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ ]),
+ rowCount: 1,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs3 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4],
+ [2, 3, 4, 5],
+ ]),
+ rowCount: 2,
+ columnCount: 4,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const newXs4 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4, 5, 6, 7],
+ [2, 3, 4, 5, 6, 7, 8],
+ ]),
+ rowCount: 2,
+ columnCount: 7,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result5 = testFunction.calculate(knownYs3, knownXs3, newXs4, constb);
+ expect(getObjectValue(result5)).toStrictEqual([
+ [0.9999999999999902, 1.9999999999999938, 2.9999999999999973, 4.000000000000001, 5.000000000000004, 6.000000000000008, 7.0000000000000115],
+ ]);
+ });
+ });
+});
diff --git a/packages/engine-formula/src/functions/statistical/trend/index.ts b/packages/engine-formula/src/functions/statistical/trend/index.ts
new file mode 100644
index 0000000000..2128b05a98
--- /dev/null
+++ b/packages/engine-formula/src/functions/statistical/trend/index.ts
@@ -0,0 +1,162 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
+import { ErrorType } from '../../../basics/error-type';
+import { matrixTranspose } from '../../../basics/math';
+import { checkKnownsArrayDimensions, getKnownsArrayCoefficients, getKnownsArrayValues, getSerialNumbersByRowsColumns, getSlopeAndIntercept } from '../../../basics/statistical';
+import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
+import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { BooleanValueObject } from '../../../engine/value-object/primitive-object';
+import { BaseFunction } from '../../base-function';
+
+export class Trend extends BaseFunction {
+ override minParams = 1;
+
+ override maxParams = 4;
+
+ override calculate(knownYs: BaseValueObject, knownXs?: BaseValueObject, newXs?: BaseValueObject, constb?: BaseValueObject): BaseValueObject {
+ const { isError, errorObject } = checkKnownsArrayDimensions(knownYs, knownXs, newXs);
+
+ if (isError) {
+ return errorObject as ErrorValueObject;
+ }
+
+ const knownYsValues = getKnownsArrayValues(knownYs);
+
+ if (knownYsValues instanceof ErrorValueObject) {
+ return knownYsValues;
+ }
+
+ const knownXsValues = this._getKnownXsValues(knownYsValues, knownXs);
+
+ if (knownXsValues instanceof ErrorValueObject) {
+ return knownXsValues;
+ }
+
+ const newXsValues = this._getNewXsValues(knownXsValues, newXs);
+
+ if (newXsValues instanceof ErrorValueObject) {
+ return newXsValues;
+ }
+
+ let _constb = constb ?? BooleanValueObject.create(true);
+
+ if (_constb.isArray()) {
+ _constb = (_constb as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ if (_constb.isString()) {
+ _constb = _constb.convertToNumberObjectValue();
+ }
+
+ if (_constb.isError()) {
+ return _constb;
+ }
+
+ const constbValue = +_constb.getValue();
+
+ return this._getResult(knownYsValues, knownXsValues, newXsValues, constbValue);
+ }
+
+ private _getResult(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number): BaseValueObject {
+ if ((knownYsValues.length === 1 && knownXsValues.length > 1) || (knownYsValues[0].length === 1 && knownXsValues[0].length > 1)) {
+ if (knownYsValues.length === 1 && knownXsValues.length > 1) {
+ const count = constb ? knownXsValues.length + 1 : knownXsValues.length;
+
+ if (count > knownYsValues[0].length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ if (knownYsValues[0].length === 1 && knownXsValues[0].length > 1) {
+ const count = constb ? knownXsValues[0].length + 1 : knownXsValues[0].length;
+
+ if (count > knownYsValues.length) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
+ }
+
+ return this._getResultByMultipleVariables(knownYsValues, knownXsValues, newXsValues, constb);
+ }
+
+ return this._getResultBySimpleVariables(knownYsValues, knownXsValues, newXsValues, constb);
+ }
+
+ private _getResultByMultipleVariables(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number): BaseValueObject {
+ const isOneRow = knownYsValues.length === 1 && knownYsValues[0].length > 1;
+
+ const _coefficients = getKnownsArrayCoefficients(knownYsValues, knownXsValues, newXsValues, constb, false);
+
+ if (_coefficients instanceof ErrorValueObject) {
+ return _coefficients;
+ }
+
+ const { coefficients, newX } = _coefficients;
+
+ const cl = coefficients[0].length;
+ const b = coefficients[0][cl - 1];
+
+ let result: number[][] = [];
+
+ for (let i = 0; i < newX.length; i++) {
+ result[i] = [];
+
+ let value = b;
+
+ for (let j = cl - 2; j >= 0; j--) {
+ value += coefficients[0][cl - 2 - j] * newX[i][j];
+ }
+
+ result[i].push(value);
+ }
+
+ if (isOneRow) {
+ result = matrixTranspose(result);
+ }
+
+ return ArrayValueObject.createByArray(result);
+ }
+
+ private _getResultBySimpleVariables(knownYsValues: number[][], knownXsValues: number[][], newXsValues: number[][], constb: number): BaseValueObject {
+ const knownYsValuesFlat = knownYsValues.flat();
+ const knownXsValuesFlat = knownXsValues.flat();
+
+ const { slope, intercept } = getSlopeAndIntercept(knownXsValuesFlat, knownYsValuesFlat, constb, false);
+
+ const result = newXsValues.map((row) => {
+ return row.map((value) => slope * value + intercept);
+ });
+
+ return ArrayValueObject.createByArray(result);
+ }
+
+ private _getKnownXsValues(knownYsValues: number[][], knownXs?: BaseValueObject): number[][] | ErrorValueObject {
+ if (!knownXs || knownXs.isNull()) {
+ return getSerialNumbersByRowsColumns(knownYsValues.length, knownYsValues[0].length);
+ }
+
+ return getKnownsArrayValues(knownXs);
+ }
+
+ private _getNewXsValues(knownXsValues: number[][], newXs?: BaseValueObject): number[][] | ErrorValueObject {
+ if (!newXs || newXs.isNull()) {
+ return knownXsValues;
+ }
+
+ return getKnownsArrayValues(newXs);
+ }
+}
diff --git a/packages/sheets-formula-ui/src/locale/function-list/statistical/en-US.ts b/packages/sheets-formula-ui/src/locale/function-list/statistical/en-US.ts
index 445baeb559..d8f47f7ad0 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/statistical/en-US.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/statistical/en-US.ts
@@ -836,8 +836,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: "known_y's", detail: 'The set of y-values you already know in the relationship y = m*x+b.' },
+ knownXs: { name: "known_x's", detail: 'The set of x-values you already know in the relationship y = m*x+b.' },
+ constb: { name: 'const', detail: 'A logical value specifying whether to force the constant b to equal 0.' },
+ stats: { name: 'stats', detail: 'A logical value specifying whether to return additional regression statistics.' },
},
},
LOGEST: {
@@ -850,8 +852,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: "known_y's", detail: 'The set of y-values you already know in the relationship y = b*m^x.' },
+ knownXs: { name: "known_x's", detail: 'The set of x-values you already know in the relationship y = b*m^x.' },
+ constb: { name: 'const', detail: 'A logical value specifying whether to force the constant b to equal 1.' },
+ stats: { name: 'stats', detail: 'A logical value specifying whether to return additional regression statistics.' },
},
},
LOGNORM_DIST: {
@@ -1557,8 +1561,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: "known_y's", detail: 'The set of y-values you already know in the relationship y = m*x+b.' },
+ knownXs: { name: "known_x's", detail: 'The set of x-values you already know in the relationship y = m*x+b.' },
+ newXs: { name: "new_x's", detail: 'Are new x-values for which you want TREND to return corresponding y-values.' },
+ constb: { name: 'const', detail: 'A logical value specifying whether to force the constant b to equal 0.' },
},
},
TRIMMEAN: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/statistical/ja-JP.ts b/packages/sheets-formula-ui/src/locale/function-list/statistical/ja-JP.ts
index fa85336e8a..4922946465 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/statistical/ja-JP.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/statistical/ja-JP.ts
@@ -811,8 +811,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '既知の y', detail: '既にわかっている y の値の系列であり、y = m*x+b という関係が成り立ちます。' },
+ knownXs: { name: '既知の x', detail: '既にわかっている x の値の系列であり、y = m*x+b という関係が成り立ちます。' },
+ constb: { name: 'b', detail: '定数 b を 0 にするかどうかを論理値で指定します。' },
+ stats: { name: '補正', detail: '回帰直線の補正項を追加情報として返すかどうかを論理値で指定します。' },
},
},
LOGEST: {
@@ -825,8 +827,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '既知の y', detail: '既にわかっている y の値の系列であり、y = b*m^x という関係が成り立ちます。' },
+ knownXs: { name: '既知の x', detail: '既にわかっている x の値の系列であり、y = b*m^x という関係が成り立ちます。' },
+ constb: { name: 'b', detail: '定数 b を 1 にするかどうかを論理値で指定します。' },
+ stats: { name: '補正', detail: '回帰直線の補正項を追加情報として返すかどうかを論理値で指定します。' },
},
},
LOGNORM_DIST: {
@@ -1520,8 +1524,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '既知の y', detail: '既にわかっている y の値の系列であり、y = m*x+b という関係が成り立ちます。' },
+ knownXs: { name: '既知の x', detail: '既にわかっている x の値の系列であり、y = m*x+b という関係が成り立ちます。' },
+ newXs: { name: '新しい x', detail: 'TREND 関数を利用して、対応する y の値を計算する新しい x の値を指定します。' },
+ constb: { name: 'b', detail: '定数 b を 0 にするかどうかを論理値で指定します。' },
},
},
TRIMMEAN: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/statistical/vi-VN.ts b/packages/sheets-formula-ui/src/locale/function-list/statistical/vi-VN.ts
index 2a31a872a3..7b66b7ef66 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/statistical/vi-VN.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/statistical/vi-VN.ts
@@ -758,6 +758,38 @@ export default {
k: { name: 'k', detail: 'Vị trí (tính từ lớn nhất) trong mảng hoặc phạm vi ô dữ liệu cần trả về.' },
},
},
+ LINEST: {
+ description: 'Trả về các tham số của xu hướng tuyến tính',
+ abstract: 'Trả về các tham số của xu hướng tuyến tính',
+ links: [
+ {
+ title: 'Hướng dẫn',
+ url: 'https://support.microsoft.com/vi-vn/office/linest-%E5%87%BD%E6%95%B0-84d7d0d9-6e50-4101-977a-fa7abf772b6d',
+ },
+ ],
+ functionParameter: {
+ knownYs: { name: 'dữ liệu đã biết_y', detail: 'Tập giá trị y mà bạn đã biết trong quan hệ y = m*x+b.' },
+ knownXs: { name: 'dữ liệu đã biết_x', detail: 'Tập giá trị x mà bạn đã biết trong quan hệ y = m*x+b.' },
+ constb: { name: 'b', detail: 'Một giá trị lô-gic cho biết có bắt buộc hằng số b phải bằng 0 hay không.' },
+ stats: { name: 'thống kê', detail: 'Giá trị lô-gic chỉ rõ có trả về các thống kê hồi quy bổ sung hay không.' },
+ },
+ },
+ LOGEST: {
+ description: 'Trả về các tham số của xu hướng hàm mũ',
+ abstract: 'Trả về các tham số của xu hướng hàm mũ',
+ links: [
+ {
+ title: 'Hướng dẫn',
+ url: 'https://support.microsoft.com/vi-vn/office/logest-%E5%87%BD%E6%95%B0-f27462d8-3657-4030-866b-a272c1d18b4b',
+ },
+ ],
+ functionParameter: {
+ knownYs: { name: 'dữ liệu đã biết_y', detail: 'Tập giá trị y mà bạn đã biết trong quan hệ y = b*m^x.' },
+ knownXs: { name: 'dữ liệu đã biết_x', detail: 'Tập giá trị x mà bạn đã biết trong quan hệ y = b*m^x.' },
+ constb: { name: 'b', detail: 'Một giá trị lô-gic cho biết có bắt buộc hằng số b phải bằng 1 hay không.' },
+ stats: { name: 'thống kê', detail: 'Giá trị lô-gic chỉ rõ có trả về các thống kê hồi quy bổ sung hay không.' },
+ },
+ },
LOGNORM_DIST: {
description: 'Trả về phân bố chuẩn lô-ga-rít của',
abstract: 'Trả về phân bố chuẩn lô-ga-rít của',
@@ -1451,6 +1483,22 @@ export default {
type: { name: 'loại Phép thử', detail: 'Loại Phép thử t cần thực hiện.' },
},
},
+ TREND: {
+ description: 'Trả về các giá trị theo xu hướng tuyến tính',
+ abstract: 'Trả về các giá trị theo xu hướng tuyến tính',
+ links: [
+ {
+ title: 'Hướng dẫn',
+ url: 'https://support.microsoft.com/vi-vn/office/trend-%E5%87%BD%E6%95%B0-e2f135f0-8827-4096-9873-9a7cf7b51ef1',
+ },
+ ],
+ functionParameter: {
+ knownYs: { name: 'dữ liệu đã biết_y', detail: 'Tập giá trị y mà bạn đã biết trong quan hệ y = m*x+b.' },
+ knownXs: { name: 'dữ liệu đã biết_x', detail: 'Tập giá trị x mà bạn đã biết trong quan hệ y = m*x+b.' },
+ newXs: { name: 'dữ liệu mới_x', detail: 'Là những giá trị x mới mà bạn muốn hàm TREND trả về tương ứng với các giá trị y.' },
+ constb: { name: 'b', detail: 'Một giá trị lô-gic cho biết có bắt buộc hằng số b phải bằng 0 hay không.' },
+ },
+ },
TRIMMEAN: {
description: 'Trả về trung bình của phần trong một tập dữ liệu.',
abstract: 'Trả về trung bình của phần trong một tập dữ liệu.',
diff --git a/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-CN.ts b/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-CN.ts
index 7c39a555b1..85af1b2170 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-CN.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-CN.ts
@@ -836,8 +836,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '已知数据_y', detail: '关系表达式 y = m*x+b 中已知的 y 值集合。' },
+ knownXs: { name: '已知数据_x', detail: '关系表达式 y = m*x+b 中已知的 x 值集合。' },
+ constb: { name: 'b', detail: '一个逻辑值,用于指定是否将常量 b 强制设为 0。' },
+ stats: { name: '统计', detail: '一个逻辑值,用于指定是否返回附加回归统计值。' },
},
},
LOGEST: {
@@ -850,8 +852,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '已知数据_y', detail: '关系表达式 y = b*m^x 中已知的 y 值集合。' },
+ knownXs: { name: '已知数据_x', detail: '关系表达式 y = b*m^x 中已知的 x 值集合。' },
+ constb: { name: 'b', detail: '一个逻辑值,用于指定是否将常量 b 强制设为 1。' },
+ stats: { name: '统计', detail: '一个逻辑值,用于指定是否返回附加回归统计值。' },
},
},
LOGNORM_DIST: {
@@ -1557,8 +1561,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '已知数据_y', detail: '关系表达式 y = m*x+b 中已知的 y 值集合。' },
+ knownXs: { name: '已知数据_x', detail: '关系表达式 y = m*x+b 中已知的 x 值集合。' },
+ newXs: { name: '新数据_x', detail: '需要 TREND 返回对应 y 值的新 x 值。' },
+ constb: { name: 'b', detail: '一个逻辑值,用于指定是否将常量 b 强制设为 0。' },
},
},
TRIMMEAN: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-TW.ts b/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-TW.ts
index 16ff5456e6..03fc2e0522 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-TW.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/statistical/zh-TW.ts
@@ -836,8 +836,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '已知資料_y', detail: '在 y = m*x+b 關係中一組已知的 y 值。' },
+ knownXs: { name: '已知資料_x', detail: '在 y = m*x+b 關係中一組已知的 x 值。' },
+ constb: { name: 'b', detail: '指定是否強迫常數 b 等於 0 的邏輯值。' },
+ stats: { name: '統計', detail: '指定是否要傳回額外迴歸統計值的邏輯值。' },
},
},
LOGEST: {
@@ -850,8 +852,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '已知資料_y', detail: '在 y = b*m^x 關係中一組已知的 y 值。' },
+ knownXs: { name: '已知資料_x', detail: '在 y = b*m^x 關係中一組已知的 x 值。' },
+ constb: { name: 'b', detail: '指定是否強迫常數 b 等於 1 的邏輯值。' },
+ stats: { name: '統計', detail: '指定是否要傳回額外迴歸統計值的邏輯值。' },
},
},
LOGNORM_DIST: {
@@ -1557,8 +1561,10 @@ export default {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ knownYs: { name: '已知資料_y', detail: '在 y = m*x+b 關係中一組已知的 y 值。' },
+ knownXs: { name: '已知資料_x', detail: '在 y = m*x+b 關係中一組已知的 x 值。' },
+ newXs: { name: '新資料_x', detail: '要 TREND 傳回對應 y 值的新 x 值。' },
+ constb: { name: 'b', detail: '指定是否強迫常數 b 等於 0 的邏輯值。' },
},
},
TRIMMEAN: {
diff --git a/packages/sheets-formula/src/services/function-list/statistical.ts b/packages/sheets-formula/src/services/function-list/statistical.ts
index afe9148c0c..ee7782974a 100644
--- a/packages/sheets-formula/src/services/function-list/statistical.ts
+++ b/packages/sheets-formula/src/services/function-list/statistical.ts
@@ -1426,17 +1426,31 @@ export const FUNCTION_LIST_STATISTICAL: IFunctionInfo[] = [
abstract: 'formula.functionList.LINEST.abstract',
functionParameter: [
{
- name: 'formula.functionList.LINEST.functionParameter.number1.name',
- detail: 'formula.functionList.LINEST.functionParameter.number1.detail',
- example: 'A1:A20',
+ name: 'formula.functionList.LINEST.functionParameter.knownYs.name',
+ detail: 'formula.functionList.LINEST.functionParameter.knownYs.detail',
+ example: 'B2:B7',
require: 1,
repeat: 0,
},
{
- name: 'formula.functionList.LINEST.functionParameter.number2.name',
- detail: 'formula.functionList.LINEST.functionParameter.number2.detail',
- example: 'A1:A20',
- require: 1,
+ name: 'formula.functionList.LINEST.functionParameter.knownXs.name',
+ detail: 'formula.functionList.LINEST.functionParameter.knownXs.detail',
+ example: 'A2:A7',
+ require: 0,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.LINEST.functionParameter.constb.name',
+ detail: 'formula.functionList.LINEST.functionParameter.constb.detail',
+ example: 'true',
+ require: 0,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.LINEST.functionParameter.stats.name',
+ detail: 'formula.functionList.LINEST.functionParameter.stats.detail',
+ example: 'true',
+ require: 0,
repeat: 0,
},
],
@@ -1448,17 +1462,31 @@ export const FUNCTION_LIST_STATISTICAL: IFunctionInfo[] = [
abstract: 'formula.functionList.LOGEST.abstract',
functionParameter: [
{
- name: 'formula.functionList.LOGEST.functionParameter.number1.name',
- detail: 'formula.functionList.LOGEST.functionParameter.number1.detail',
- example: 'A1:A20',
+ name: 'formula.functionList.LOGEST.functionParameter.knownYs.name',
+ detail: 'formula.functionList.LOGEST.functionParameter.knownYs.detail',
+ example: 'B2:B7',
require: 1,
repeat: 0,
},
{
- name: 'formula.functionList.LOGEST.functionParameter.number2.name',
- detail: 'formula.functionList.LOGEST.functionParameter.number2.detail',
- example: 'A1:A20',
- require: 1,
+ name: 'formula.functionList.LOGEST.functionParameter.knownXs.name',
+ detail: 'formula.functionList.LOGEST.functionParameter.knownXs.detail',
+ example: 'A2:A7',
+ require: 0,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.LOGEST.functionParameter.constb.name',
+ detail: 'formula.functionList.LOGEST.functionParameter.constb.detail',
+ example: 'true',
+ require: 0,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.LOGEST.functionParameter.stats.name',
+ detail: 'formula.functionList.LOGEST.functionParameter.stats.detail',
+ example: 'true',
+ require: 0,
repeat: 0,
},
],
@@ -2667,17 +2695,31 @@ export const FUNCTION_LIST_STATISTICAL: IFunctionInfo[] = [
abstract: 'formula.functionList.TREND.abstract',
functionParameter: [
{
- name: 'formula.functionList.TREND.functionParameter.number1.name',
- detail: 'formula.functionList.TREND.functionParameter.number1.detail',
- example: 'A1:A20',
+ name: 'formula.functionList.TREND.functionParameter.knownYs.name',
+ detail: 'formula.functionList.TREND.functionParameter.knownYs.detail',
+ example: 'B2:B7',
require: 1,
repeat: 0,
},
{
- name: 'formula.functionList.TREND.functionParameter.number2.name',
- detail: 'formula.functionList.TREND.functionParameter.number2.detail',
- example: 'A1:A20',
- require: 1,
+ name: 'formula.functionList.TREND.functionParameter.knownXs.name',
+ detail: 'formula.functionList.TREND.functionParameter.knownXs.detail',
+ example: 'A2:A7',
+ require: 0,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.TREND.functionParameter.newXs.name',
+ detail: 'formula.functionList.TREND.functionParameter.newXs.detail',
+ example: 'A9:A10',
+ require: 0,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.TREND.functionParameter.constb.name',
+ detail: 'formula.functionList.TREND.functionParameter.constb.detail',
+ example: 'true',
+ require: 0,
repeat: 0,
},
],