Skip to content

Commit f52173a

Browse files
authored
[NB] update differentiation of division (#14085)
- add proper operator typing for the power operator - the frontend treats multiplication equally for element and nen elementwise, but pow needs to have the correct operator - works towards #13984
1 parent bcebbc1 commit f52173a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

OMCompiler/Compiler/NBackEnd/Util/NBDifferentiate.mo

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ public
17851785
local
17861786
Expression exp1, exp2, diffExp1, diffExp2, e1, e2, e3, res;
17871787
Operator operator, addOp, mulOp, powOp;
1788-
Operator.SizeClassification sizeClass;
1788+
Operator.SizeClassification sizeClass, powSizeClass;
17891789

17901790
// Addition calculations (ADD, ADD_EW, ...)
17911791
// (f + g)' = f' + g'
@@ -1835,10 +1835,11 @@ public
18351835
(diffExp2, diffArguments) := differentiateExpression(exp2, diffArguments);
18361836
// create subtraction and multiplication operator from the size classification of original division operator
18371837
(_, sizeClass) := Operator.classify(operator);
1838+
// the frontend treats multiplication equally for element and nen elementwise, but pow needs to have the correct operator
1839+
powSizeClass := if Type.isArray(Expression.typeOf(exp2)) then NFOperator.SizeClassification.ARRAY_SCALAR else NFOperator.SizeClassification.SCALAR;
18381840
addOp := Operator.fromClassification((NFOperator.MathClassification.ADDITION, sizeClass), operator.ty);
18391841
mulOp := Operator.fromClassification((NFOperator.MathClassification.MULTIPLICATION, sizeClass), operator.ty);
1840-
powOp := Operator.fromClassification((NFOperator.MathClassification.POWER,
1841-
Operator.combineSizeClassification(sizeClass, NFOperator.SizeClassification.SCALAR)), operator.ty);
1842+
powOp := Operator.fromClassification((NFOperator.MathClassification.POWER, powSizeClass), operator.ty);
18421843
then (Expression.MULTARY(
18431844
{Expression.MULTARY(
18441845
{Expression.BINARY(exp1, mulOp, diffExp2)}, // fg'
@@ -1920,7 +1921,7 @@ public
19201921
list<Expression> inv_arguments, new_inv_arguments = {};
19211922
list<Expression> diff_arguments, diff_inv_arguments;
19221923
Operator operator, addOp, powOp;
1923-
Operator.SizeClassification sizeClass;
1924+
Operator.SizeClassification sizeClass, powSizeClass;
19241925

19251926
// Dash calculations (ADD, SUB, ADD_EW, SUB_EW, ...)
19261927
// NOTE: Multary always contains ADDITION
@@ -1966,9 +1967,10 @@ public
19661967
algorithm
19671968
// create addition and power operator
19681969
(_, sizeClass) := Operator.classify(operator);
1970+
// the frontend treats multiplication equally for element and nen elementwise, but pow needs to have the correct operator
1971+
powSizeClass := if Type.isArray(Expression.typeOf(listHead(inv_arguments))) then NFOperator.SizeClassification.ARRAY_SCALAR else NFOperator.SizeClassification.SCALAR;
19691972
addOp := Operator.fromClassification((NFOperator.MathClassification.ADDITION, sizeClass), operator.ty);
1970-
powOp := Operator.fromClassification((NFOperator.MathClassification.POWER,
1971-
Operator.combineSizeClassification(sizeClass, NFOperator.SizeClassification.SCALAR)), operator.ty);
1973+
powOp := Operator.fromClassification((NFOperator.MathClassification.POWER, powSizeClass), operator.ty);
19721974
// f'
19731975
(diff_arguments, diffArguments) := differentiateMultaryMultiplicationArgs(arguments, diffArguments, operator);
19741976
diff_enumerator := Expression.MULTARY(diff_arguments, {}, addOp);

0 commit comments

Comments
 (0)