@@ -29,8 +29,10 @@ import {
2929} from "./helper_statistical" ;
3030import {
3131 dichotomicSearch ,
32+ emptyDataErrorMessage ,
3233 inferFormat ,
3334 matrixMap ,
35+ noValidInputErrorMessage ,
3436 reduceNumbers ,
3537 reduceNumbersTextAs0 ,
3638 toBoolean ,
@@ -158,7 +160,7 @@ function centile(
158160 count ++ ;
159161 }
160162 } ) ;
161- assert ( count !== 0 , _t ( "[[FUNCTION_NAME]] has no valid input data." ) ) ;
163+ assert ( count !== 0 , noValidInputErrorMessage ) ;
162164
163165 if ( ! isInclusive ) {
164166 // 2nd argument must be between 1/(n+1) and n/(n+1) with n the number of data
@@ -542,8 +544,12 @@ export const FORECAST: AddFunctionDescription = {
542544 x : Arg ,
543545 dataY : Matrix < FunctionResultObject > ,
544546 dataX : Matrix < FunctionResultObject >
545- ) : number | Matrix < number > {
547+ ) {
546548 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
549+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
550+ return new NotAvailableError ( noValidInputErrorMessage ) ;
551+ }
552+
547553 return predictLinearValues (
548554 [ flatDataY ] ,
549555 [ flatDataX ] ,
@@ -586,7 +592,10 @@ export const GROWTH: AddFunctionDescription = {
586592 knownDataX : Matrix < FunctionResultObject > = [ [ ] ] ,
587593 newDataX : Matrix < FunctionResultObject > = [ [ ] ] ,
588594 b : Maybe < FunctionResultObject > = { value : true }
589- ) : Matrix < number > {
595+ ) {
596+ if ( knownDataY . length === 0 || knownDataY [ 0 ] . length === 0 ) {
597+ return new EvaluationError ( emptyDataErrorMessage ( "known_data_y" ) ) ;
598+ }
590599 return expM (
591600 predictLinearValues (
592601 logM ( toNumberMatrix ( knownDataY , "the first argument (known_data_y)" ) ) ,
@@ -613,11 +622,11 @@ export const INTERCEPT: AddFunctionDescription = {
613622 _t ( "The range representing the array or matrix of independent data." )
614623 ) ,
615624 ] ,
616- compute : function (
617- dataY : Matrix < FunctionResultObject > ,
618- dataX : Matrix < FunctionResultObject >
619- ) : number {
625+ compute : function ( dataY : Matrix < FunctionResultObject > , dataX : Matrix < FunctionResultObject > ) {
620626 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
627+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
628+ return new NotAvailableError ( noValidInputErrorMessage ) ;
629+ }
621630 const [ [ ] , [ intercept ] ] = fullLinearRegression ( [ flatDataX ] , [ flatDataY ] ) ;
622631 return intercept as number ;
623632 } ,
@@ -658,7 +667,7 @@ export const LARGE = {
658667 } ) ;
659668 const result = largests . shift ( ) ;
660669 if ( result === undefined ) {
661- return new EvaluationError ( _t ( "[[FUNCTION_NAME]] has no valid input data." ) ) ;
670+ return new EvaluationError ( noValidInputErrorMessage ) ;
662671 }
663672 if ( count < _n ) {
664673 return new EvaluationError (
@@ -702,7 +711,10 @@ export const LINEST: AddFunctionDescription = {
702711 dataX : Matrix < FunctionResultObject > = [ [ ] ] ,
703712 calculateB : Maybe < FunctionResultObject > = { value : true } ,
704713 verbose : Maybe < FunctionResultObject > = { value : false }
705- ) : ( number | string ) [ ] [ ] {
714+ ) {
715+ if ( dataY . length === 0 || dataY [ 0 ] . length === 0 ) {
716+ return new EvaluationError ( emptyDataErrorMessage ( "data_y" ) ) ;
717+ }
706718 return fullLinearRegression (
707719 toNumberMatrix ( dataX , "the first argument (data_y)" ) ,
708720 toNumberMatrix ( dataY , "the second argument (data_x)" ) ,
@@ -745,7 +757,10 @@ export const LOGEST: AddFunctionDescription = {
745757 dataX : Matrix < FunctionResultObject > = [ [ ] ] ,
746758 calculateB : Maybe < FunctionResultObject > = { value : true } ,
747759 verbose : Maybe < FunctionResultObject > = { value : false }
748- ) : ( number | string ) [ ] [ ] {
760+ ) {
761+ if ( dataY . length === 0 || dataY [ 0 ] . length === 0 ) {
762+ return new EvaluationError ( emptyDataErrorMessage ( "data_y" ) ) ;
763+ }
749764 const coeffs = fullLinearRegression (
750765 toNumberMatrix ( dataX , "the second argument (data_x)" ) ,
751766 logM ( toNumberMatrix ( dataY , "the first argument (data_y)" ) ) ,
@@ -773,10 +788,8 @@ export const MATTHEWS: AddFunctionDescription = {
773788 const flatX = dataX . flat ( ) ;
774789 const flatY = dataY . flat ( ) ;
775790 assertSameNumberOfElements ( flatX , flatY ) ;
776- if ( flatX . length === 0 ) {
777- return new EvaluationError (
778- _t ( "[[FUNCTION_NAME]] expects non-empty ranges for both parameters." )
779- ) ;
791+ if ( flatX . length === 0 || flatY . length === 0 ) {
792+ return new NotAvailableError ( noValidInputErrorMessage ) ;
780793 }
781794 const n = flatX . length ;
782795
@@ -1022,17 +1035,10 @@ export const MINIFS = {
10221035// -----------------------------------------------------------------------------
10231036// PEARSON
10241037// -----------------------------------------------------------------------------
1025- function pearson ( dataY : Matrix < FunctionResultObject > , dataX : Matrix < FunctionResultObject > ) : number {
1038+ function pearson ( dataY : Matrix < FunctionResultObject > , dataX : Matrix < FunctionResultObject > ) {
10261039 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
1027- if ( flatDataX . length === 0 ) {
1028- throw new EvaluationError (
1029- _t ( "[[FUNCTION_NAME]] expects non-empty ranges for both parameters." )
1030- ) ;
1031- }
1032- if ( flatDataX . length < 2 ) {
1033- throw new EvaluationError (
1034- _t ( "[[FUNCTION_NAME]] needs at least two values for both parameters." )
1035- ) ;
1040+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
1041+ return new NotAvailableError ( noValidInputErrorMessage ) ;
10361042 }
10371043 const n = flatDataX . length ;
10381044
@@ -1072,7 +1078,7 @@ export const PEARSON: AddFunctionDescription = {
10721078 compute : function (
10731079 dataY : Matrix < FunctionResultObject > ,
10741080 dataX : Matrix < FunctionResultObject >
1075- ) : number {
1081+ ) : number | NotAvailableError {
10761082 return pearson ( dataY , dataX ) ;
10771083 } ,
10781084 isExported : true ,
@@ -1169,8 +1175,11 @@ export const POLYFIT_COEFFS: AddFunctionDescription = {
11691175 dataX : Matrix < FunctionResultObject > ,
11701176 order : Maybe < FunctionResultObject > ,
11711177 intercept : Maybe < FunctionResultObject > = { value : true }
1172- ) : Matrix < number > {
1178+ ) {
11731179 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
1180+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
1181+ return new NotAvailableError ( noValidInputErrorMessage ) ;
1182+ }
11741183 return polynomialRegression (
11751184 flatDataY ,
11761185 flatDataX ,
@@ -1208,9 +1217,12 @@ export const POLYFIT_FORECAST: AddFunctionDescription = {
12081217 dataX : Matrix < FunctionResultObject > ,
12091218 order : Maybe < FunctionResultObject > ,
12101219 intercept : Maybe < FunctionResultObject > = { value : true }
1211- ) : Matrix < number > {
1220+ ) {
12121221 const _order = toNumber ( order , this . locale ) ;
12131222 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
1223+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
1224+ return new NotAvailableError ( noValidInputErrorMessage ) ;
1225+ }
12141226 const coeffs = polynomialRegression ( flatDataY , flatDataX , _order , toBoolean ( intercept ) ) . flat ( ) ;
12151227 return matrixMap ( toMatrix ( x ) , ( xij ) =>
12161228 evaluatePolynomial ( coeffs , toNumber ( xij , this . locale ) , _order )
@@ -1336,7 +1348,11 @@ export const RSQ: AddFunctionDescription = {
13361348 dataY : Matrix < FunctionResultObject > ,
13371349 dataX : Matrix < FunctionResultObject >
13381350 ) : number {
1339- return Math . pow ( pearson ( dataX , dataY ) , 2.0 ) ;
1351+ const value = pearson ( dataY , dataX ) ;
1352+ if ( value instanceof Error ) {
1353+ throw value ;
1354+ }
1355+ return Math . pow ( value as number , 2.0 ) ;
13401356 } ,
13411357 isExported : true ,
13421358} ;
@@ -1356,11 +1372,11 @@ export const SLOPE: AddFunctionDescription = {
13561372 _t ( "The range representing the array or matrix of independent data." )
13571373 ) ,
13581374 ] ,
1359- compute : function (
1360- dataY : Matrix < FunctionResultObject > ,
1361- dataX : Matrix < FunctionResultObject >
1362- ) : number {
1375+ compute : function ( dataY : Matrix < FunctionResultObject > , dataX : Matrix < FunctionResultObject > ) {
13631376 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
1377+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
1378+ return new NotAvailableError ( noValidInputErrorMessage ) ;
1379+ }
13641380 const [ [ slope ] ] = fullLinearRegression ( [ flatDataX ] , [ flatDataY ] ) ;
13651381 return slope as number ;
13661382 } ,
@@ -1401,7 +1417,7 @@ export const SMALL = {
14011417 } ) ;
14021418 const result = largests . pop ( ) ;
14031419 if ( result === undefined ) {
1404- return new EvaluationError ( _t ( "[[FUNCTION_NAME]] has no valid input data." ) ) ;
1420+ return new EvaluationError ( noValidInputErrorMessage ) ;
14051421 }
14061422 if ( count < _n ) {
14071423 return new EvaluationError (
@@ -1428,11 +1444,11 @@ export const SPEARMAN: AddFunctionDescription = {
14281444 _t ( "The range representing the array or matrix of independent data." )
14291445 ) ,
14301446 ] ,
1431- compute : function (
1432- dataX : Matrix < FunctionResultObject > ,
1433- dataY : Matrix < FunctionResultObject >
1434- ) : number {
1447+ compute : function ( dataX : Matrix < FunctionResultObject > , dataY : Matrix < FunctionResultObject > ) {
14351448 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
1449+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
1450+ return new NotAvailableError ( noValidInputErrorMessage ) ;
1451+ }
14361452 const n = flatDataX . length ;
14371453
14381454 const order = flatDataX . map ( ( e , i ) => [ e , flatDataY [ i ] ] ) ;
@@ -1577,11 +1593,11 @@ export const STEYX: AddFunctionDescription = {
15771593 _t ( "The range representing the array or matrix of independent data." )
15781594 ) ,
15791595 ] ,
1580- compute : function (
1581- dataY : Matrix < FunctionResultObject > ,
1582- dataX : Matrix < FunctionResultObject >
1583- ) : number {
1596+ compute : function ( dataY : Matrix < FunctionResultObject > , dataX : Matrix < FunctionResultObject > ) {
15841597 const { flatDataX, flatDataY } = filterAndFlatData ( dataY , dataX ) ;
1598+ if ( flatDataX . length === 0 || flatDataY . length === 0 ) {
1599+ return new NotAvailableError ( noValidInputErrorMessage ) ;
1600+ }
15851601 const data = fullLinearRegression ( [ flatDataX ] , [ flatDataY ] , true , true ) ;
15861602 return data [ 1 ] [ 2 ] as number ;
15871603 } ,
@@ -1620,7 +1636,10 @@ export const TREND: AddFunctionDescription = {
16201636 knownDataX : Matrix < FunctionResultObject > = [ [ ] ] ,
16211637 newDataX : Matrix < FunctionResultObject > = [ [ ] ] ,
16221638 b : Maybe < FunctionResultObject > = { value : true }
1623- ) : Matrix < number > {
1639+ ) {
1640+ if ( knownDataY . length === 0 || knownDataY [ 0 ] . length === 0 ) {
1641+ return new EvaluationError ( emptyDataErrorMessage ( "known_data_y" ) ) ;
1642+ }
16241643 return predictLinearValues (
16251644 toNumberMatrix ( knownDataY , "the first argument (known_data_y)" ) ,
16261645 toNumberMatrix ( knownDataX , "the second argument (known_data_x)" ) ,
0 commit comments