Skip to content

[FIX] evaluation: fix operation with empty matrices #6930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from

Conversation

anhe-odoo
Copy link
Contributor

Task Description

When using empty matrices for math operation (multiplication or inversion), as in the FORECAST formula, we got a traceback and the error message was not clear for the user. This commits aims to fix it by checking that the matrices used in theses operations are not empty and returning an understandable error if it's not the case.

Related Task

review checklist

  • feature is organized in plugin, or UI components
  • support of duplicate sheet (deep copy)
  • in model/core: ranges are Range object, and can be adapted (adaptRanges)
  • in model/UI: ranges are strings (to show the user)
  • undo-able commands (uses this.history.update)
  • multiuser-able commands (has inverse commands and transformations where needed)
  • new/updated/removed commands are documented
  • exportable in excel
  • translations (_t("qmsdf %s", abc))
  • unit tested
  • clean commented code
  • track breaking changes
  • doc is rebuild (npm run doc)
  • status is correct in Odoo

Task Description

When using empty matrices for math operation (multiplication or
inversion), as in the FORECAST formula, we got a traceback and the
error message was not clear for the user. This commits aims to fix
it by checking that the matrices used in theses operations are not
empty and returning an understandable error if it's not the case.

Related Task

Task: 5001658
@robodoo
Copy link
Collaborator

robodoo commented Aug 13, 2025

Pull request status dashboard

Comment on lines +36 to +37
if (M.length < 1 || M[0].length < 1) {
throw new Error(_t("Function [[FUNCTION_NAME]] invert matrix error, empty matrix"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is impossible for a user to provide an empty matrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was, at first, as the invertMatrix function is called in other part of the code than the INVERT_MATRIX formula (it's also used in the getLMS method). But as we first try to multiply two matrices before trying to invert the result, indeed it may be not needed anymore.
Shouldn't we let this part "in case of" one day we use this method in other part of the code, or keeping thins as a safety guard is not a good idea ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can keep the check, but remove the translation (it's useless work for translators if the string is not user-facing)

setCellContent(model, "A1", "=SLOPE(C2:C3, B2:B3)");
expect(getEvaluatedCell(model, "A1").value).toBe("#ERROR");
expect((getEvaluatedCell(model, "A1") as ErrorCell).error.message).toBe(
"Cannot multiply matrices: empty matrices cannot be multiplied."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the arguments should be validated in the compute with a proper error message telling which argument is faulty. Here the message is quite useless: it doesn't tell you which arg is wrong, and at first glance you're trying to compute the slope, but the error is mentioning matrix multiplication.

@@ -30,6 +33,10 @@ export function invertMatrix(M: Matrix<number>): {
// (b) Multiply a row by a scalar. This multiply the determinant by that scalar.
// (c) Add to a row a multiple of another row. This does not change the determinant.

if (M.length < 1 || M[0].length < 1) {
throw new Error(_t("Function [[FUNCTION_NAME]] invert matrix error, empty matrix"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user-facing errors should be EvaluationError

Suggested change
throw new Error(_t("Function [[FUNCTION_NAME]] invert matrix error, empty matrix"));
throw new EvaluationError(_t("Function [[FUNCTION_NAME]] invert matrix error, empty matrix"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants