For comparison, this works for SX (but SX is unusable for my huge variables). If the same could work for MX, I could actually use max() -
Octave__18:14:04_0005833_9> x=SX.sym('x', 1,2)
x =
[[x_0, x_1]]
Octave__18:17:13_0005833_10> y = max(x)*x(1)
y =
(fmax(fmax(-inf,x_0),x_1)*x_0)
Octave__18:17:23_0005833_11> z = y.gradient(x)
z =
@1=-inf, @2=fmax(@1,x_0), @3=(x_1<=@2), [[(fmax(@2,x_1)+((!(x_0<=@1))*(@3*x_0))), ((!@3)*x_0)]]
Octave__18:17:29_0005833_12> x=SX.sym('x', 1,2)
x =
[[x_0, x_1]]
Octave__18:17:31_0005833_13> y = max(x)*x(1)
y =
(fmax(fmax(-inf,x_0),x_1)*x_0)
Octave__18:17:32_0005833_14> z = y.gradient(x)
z =
@1=-inf, @2=fmax(@1,x_0), @3=(x_1<=@2), [[(fmax(@2,x_1)+((!(x_0<=@1))*(@3*x_0))), ((!@3)*x_0)]]
Octave__18:17:35_0005833_15> f = Function('f',{x},{z})
f =
f:(i0[1x2])->(o0[1x2]) SXFunction
Octave__18:17:44_0005833_16> f([1 3])
ans =
[[3, 1]]
Octave__18:17:49_0005833_17> f([3 1])
ans =
[[6, 0]]