max(): not defined for MX?

451 views
Skip to first unread message

Dave Goel

unread,
Mar 30, 2018, 9:59:11 PM3/30/18
to CasADi

max(): not defined for MX?

I notice that max() now works for within matrices, and gets the maximum value within a matrix (right?). However, it seems that it only works for SX, not for MX. Is this a bug or intentional? Is it easy to fix?  I can only use MX in my code because SX would take many days to define, and would run out of memory.


x = MX('x', 2,1);
y = x.max;
f = Function('f', {x}, {y});
f([1;2])

error: casadiMEX: .../casadi/core/mx_node.cpp:291: 'eval' not defined for class N6casadi4MMaxE
error: called from
    call at line 735 column 28
    paren at line 1435 column 13
    subsref at line 1416 column 29

casadi: 3.4.0_for_octave_4.2.1

Joris Gillis

unread,
Apr 3, 2018, 3:17:28 AM4/3/18
to CasADi
Intentional only in the sense that there were higher priorities to focus on,
easy to fix.
https://github.com/casadi/casadi/issues/2043

Best,
  Joris

Dave Goel

unread,
Apr 3, 2018, 3:53:30 AM4/3/18
to Joris Gillis, CasADi
Hi Joris,

Ah, thanks for that. 

So, it seems that the mmax node did get added for MX, per #2043. But, when you f() on it later, you end up with the "eval not defined" thing. 
The same works on SX, though.




--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/casadi-users/5516d82b-373c-448e-ae45-f64a6c53c1ae%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Joris Gillis

unread,
Apr 3, 2018, 3:57:06 AM4/3/18
to CasADi
Are you using source builds? I can quickly include the functionality which you want (anything else besides evaluation?)

Best,
  Joris

Dave Goel

unread,
Apr 3, 2018, 3:58:43 AM4/3/18
to Joris Gillis, CasADi
I would love to include source builds, but last time I tried, I was stuck in some kind of dependency hell.
Should I try compiling again?

Just the eval. And, thanks so much!
Dave

 

Dave Goel

unread,
Apr 23, 2018, 6:16:29 PM4/23/18
to CasADi
Joris,

Many thanks for allowing function definitions for max over MX.

You did ask if I needed anything else beside eval. I apologize for not being clear. I am now stuck at the gradient step, as below. Is it possible to allow gradients as well?

Octave__18:13:59_0005833_7> x=MX.sym('x',1,2);
Octave__18:14:01_0005833_8> y = max(x)*x(1)
y =
(max(x)*x[0])
Octave__18:14:03_0005833_9> z = y.gradient(x)
error: casadiMEX: Error in MX::gradient at .../casadi/core/generic_matrix.hpp:1192:
Error in MX::jtimes at .../casadi/core/generic_matrix.hpp:1181:
Error in MX::reverse at .../casadi/core/mx.cpp:1676:
Error in MXFunction::ad_reverse at .../casadi/core/mx_function.cpp:997:
Error in MX::ad_reverse for node of type N6casadi4MMaxE at .../casadi/core/mx.cpp:1860:
.../casadi/core/mx_node.cpp:311: 'ad_reverse' not defined for class N6casadi4MMaxE
error: called from
    gradient at line 608 column 27
    subsref at line 18 column 30

To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users...@googlegroups.com.

Dave Goel

unread,
Apr 23, 2018, 6:20:17 PM4/23/18
to CasADi
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]]
Reply all
Reply to author
Forward
0 new messages