## [Swig-devel] [MATLAB] How to handle "ans" output?

 [Swig-devel] [MATLAB] How to handle "ans" output? From: Joel Andersson - 2014-08-19 10:28:49 Attachments: Message as HTML ```Hi! I've finally managed to generate a (still incomplete) MATLAB interface to our optimization framework CasADi! There is still a long way to go, but I would already say thanks especially to Kris for great contributions to the module! There is one thing I find particularly annoying when playing around with CasADi from MATLAB; the fact that the "ans" argument in MATLAB is not generated when you want it. A function such as arcsinh in CasADi is now wrapped as follows: function varargout = function_name(varargin) [varargout{1:nargout}] = casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); end The problem is that if you type "asinh(x)" in MATLAB, nargout will be zero, so nothing will be returned from the wrapper. To get an "ans" argument, you need to replace "nargout" with 1, or more generally with max(nargout,1): function varargout = function_name(varargin) [varargout{1:max(1,nargout)}] = casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); end My proposed resolution is to always return at least one output whenever any overload of the function has any outputs arguments. This means that if you have two overloaded functions, one without outputs and another with outputs, a dummy output (0-by-0 matrix) would be returned from the overload with no outputs: void foo(double a); // no outputs, but dummy output [] returned int foo(double a, double b); // outputs Does this sound reasonable? In a MATLAB script, you'd put a semicolon after foo(a) anyway, preventing the creation of []. Best regards, Joel ```

 [Swig-devel] [MATLAB] How to handle "ans" output? From: Joel Andersson - 2014-08-19 10:28:49 Attachments: Message as HTML ```Hi! I've finally managed to generate a (still incomplete) MATLAB interface to our optimization framework CasADi! There is still a long way to go, but I would already say thanks especially to Kris for great contributions to the module! There is one thing I find particularly annoying when playing around with CasADi from MATLAB; the fact that the "ans" argument in MATLAB is not generated when you want it. A function such as arcsinh in CasADi is now wrapped as follows: function varargout = function_name(varargin) [varargout{1:nargout}] = casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); end The problem is that if you type "asinh(x)" in MATLAB, nargout will be zero, so nothing will be returned from the wrapper. To get an "ans" argument, you need to replace "nargout" with 1, or more generally with max(nargout,1): function varargout = function_name(varargin) [varargout{1:max(1,nargout)}] = casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); end My proposed resolution is to always return at least one output whenever any overload of the function has any outputs arguments. This means that if you have two overloaded functions, one without outputs and another with outputs, a dummy output (0-by-0 matrix) would be returned from the overload with no outputs: void foo(double a); // no outputs, but dummy output [] returned int foo(double a, double b); // outputs Does this sound reasonable? In a MATLAB script, you'd put a semicolon after foo(a) anyway, preventing the creation of []. Best regards, Joel ```
 Re: [Swig-devel] [MATLAB] How to handle "ans" output? From: Ian Bell - 2014-08-19 18:43:12 Attachments: Message as HTML ```I experienced this exact issue, and while it wasn't the end of the world that it didn't directly output anything to screen (helpful for debugging), it would have been nice to have had the info that all was well. I think your proposed solution makes sense to me. I think it is a more common use case to have a function that returns something as compared to a void, unless you are passing by reference I guess. So making that the default seems logical to me. Ian On Tue, Aug 19, 2014 at 12:28 PM, Joel Andersson wrote: > Hi! > > I've finally managed to generate a (still incomplete) MATLAB interface to > our optimization framework CasADi! There is still a long way to go, but I > would already say thanks especially to Kris for great contributions to the > module! > > There is one thing I find particularly annoying when playing around with > CasADi from MATLAB; the fact that the "ans" argument in MATLAB is not > generated when you want it. > > A function such as arcsinh in CasADi is now wrapped as follows: > function varargout = function_name(varargin) > [varargout{1:nargout}] = > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > end > > The problem is that if you type "asinh(x)" in MATLAB, nargout will be > zero, so nothing will be returned from the wrapper. To get an "ans" > argument, you need to replace "nargout" with 1, or more generally with > max(nargout,1): > function varargout = function_name(varargin) > [varargout{1:max(1,nargout)}] = > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > end > > My proposed resolution is to always return at least one output whenever > any overload of the function has any outputs arguments. This means that if > you have two overloaded functions, one without outputs and another with > outputs, a dummy output (0-by-0 matrix) would be returned from the overload > with no outputs: > > void foo(double a); // no outputs, but dummy output [] returned > int foo(double a, double b); // outputs > > Does this sound reasonable? In a MATLAB script, you'd put a semicolon > after foo(a) anyway, preventing the creation of []. > > Best regards, > Joel > > > > ------------------------------------------------------------------------------ > > _______________________________________________ > Swig-devel mailing list > Swig-devel@... > https://lists.sourceforge.net/lists/listinfo/swig-devel > > ```
 Re: [Swig-devel] [MATLAB] How to handle "ans" output? From: Kris Thielemans - 2014-08-19 18:51:32 ```From: Joel Andersson Sent: 19 August 2014 11:29 > > There is one thing I find particularly annoying when playing around with CasADi > from MATLAB; the fact that the "ans" argument in MATLAB is not generated > when you want it. > Yes, this is quite annoying, I agree. > A function such as arcsinh in CasADi is now wrapped as follows: > function varargout = function_name(varargin) > [varargout{1:nargout}] = > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > end > > The problem is that if you type "asinh(x)" in MATLAB, nargout will be zero, so > nothing will be returned from the wrapper. To get an "ans" argument, you need > to replace "nargout" with 1, or more generally with max(nargout,1): > function varargout = function_name(varargin) > [varargout{1:max(1,nargout)}] = > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > end > > My proposed resolution is to always return at least one output whenever any > overload of the function has any outputs arguments. This means that if you > have two overloaded functions, one without outputs and another with outputs, a > dummy output (0-by-0 matrix) would be returned from the overload with no > outputs: > > void foo(double a); // no outputs, but dummy output [] returned > int foo(double a, double b); // outputs > > Does this sound reasonable? In a MATLAB script, you'd put a semicolon after > foo(a) anyway, preventing the creation of []. > I don't find it 100% satisfactory (it's not entirely clean, and people might not automatically put the semicolon if the function shouldn't return anything) but it's definitely a better solution than what we have at present. Also, the above example is somewhat contrived (anyone making an API like that is asking for confusion) as normally the overloading happens based on the input arguments, but I guess we have to cover corner cases as well. I have no clear idea how you'd implement it though. When wrapping the code, you'd need to know which functions return void, and if so, return [], no? If so, I guess we could just as well modify the matlab class as well? (ok, that'd seems to mean we need to transport a lot of complication into the matlab code which we don't have now, so probably not a good idea). Another idea: As opposed to returning [], return a special object (e.g. SwigVoid). Once we do that, a few different options: - set its display properties to the empty string. So effectively you want see it. (hmmm) - let the matlab wrapper detect that. Something like this (not checked!) function varargout= swig_return(varargin) ( If (nargout==0 && nargin==1 && class(varargin{1}, 'SwigVoid')) return; else varargout = varargin; return; ) function varargout = function_name(varargin) [varargout{1:nargout}] = swig_return(... casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:})); end if we then make swig_return a method of SwigRef, it could work. Or indeed, make a method swig_call_wrapper that does this all in one go... not sure if this can work though Kris ```
 Re: [Swig-devel] [MATLAB] How to handle "ans" output? From: Joel Andersson - 2014-08-19 19:31:35 Attachments: Message as HTML ```Hi! After some thinking, I would like to modify my suggestion: It should be possible to check if _all_ the overloads of a particular (member) function has at least one output argument. And for those functions, enforce an "ans" output. That is, if a function has two overloads, and at least one of them void, no "would be produced": void foo(double a, double b); double foo(double a); // No "ans" produced! But if _all_ overloads have non-void returns, it an "ans" will be produced: double foo(double a, double b); // "ans" produced double foo(double a); // "ans "produced I think the normal situation is that all the overloads are either non-void or void and this will be covered by this. It will also not add any noticeable overhead or larger wrapper files. Nor do we have to worry about strange "[]" returns from void functions. What say you? Joel P.S.: I could take a shot at the implementation, if we agree on a design. 2014-08-19 20:51 GMT+02:00 Kris Thielemans : > From: Joel Andersson Sent: 19 August 2014 11:29 > > > > There is one thing I find particularly annoying when playing around with > CasADi > > from MATLAB; the fact that the "ans" argument in MATLAB is not generated > > when you want it. > > > > Yes, this is quite annoying, I agree. > > > A function such as arcsinh in CasADi is now wrapped as follows: > > function varargout = function_name(varargin) > > [varargout{1:nargout}] = > > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > > end > > > > The problem is that if you type "asinh(x)" in MATLAB, nargout will be > zero, so > > nothing will be returned from the wrapper. To get an "ans" argument, you > need > > to replace "nargout" with 1, or more generally with max(nargout,1): > > function varargout = function_name(varargin) > > [varargout{1:max(1,nargout)}] = > > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > > end > > > > My proposed resolution is to always return at least one output whenever > any > > overload of the function has any outputs arguments. This means that if > you > > have two overloaded functions, one without outputs and another with > outputs, a > > dummy output (0-by-0 matrix) would be returned from the overload with no > > outputs: > > > > void foo(double a); // no outputs, but dummy output [] returned > > int foo(double a, double b); // outputs > > > > Does this sound reasonable? In a MATLAB script, you'd put a semicolon > after > > foo(a) anyway, preventing the creation of []. > > > > I don't find it 100% satisfactory (it's not entirely clean, and people > might not automatically put the semicolon if the function shouldn't return > anything) but it's definitely a better solution than what we have at > present. Also, the above example is somewhat contrived (anyone making an > API like that is asking for confusion) as normally the overloading happens > based on the input arguments, but I guess we have to cover corner cases as > well. > > I have no clear idea how you'd implement it though. When wrapping the > code, you'd need to know which functions return void, and if so, return [], > no? If so, I guess we could just as well modify the matlab class as well? > (ok, that'd seems to mean we need to transport a lot of complication into > the matlab code which we don't have now, so probably not a good idea). > > Another idea: > As opposed to returning [], return a special object (e.g. SwigVoid). Once > we do that, a few different options: > - set its display properties to the empty string. So effectively you want > see it. (hmmm) > - let the matlab wrapper detect that. Something like this (not checked!) > > function varargout= swig_return(varargin) > ( > If (nargout==0 && nargin==1 && class(varargin{1}, 'SwigVoid')) > return; > else > varargout = varargin; > return; > ) > > function varargout = function_name(varargin) > [varargout{1:nargout}] = swig_return(... > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:})); > end > > if we then make swig_return a method of SwigRef, it could work. Or indeed, > make a method swig_call_wrapper that does this all in one go... > > not sure if this can work though > > Kris > > -- -- Joel Andersson, PhD Ptge. Busquets 11-13, atico 3 E-08940 Cornella de Llobregat, Spain Home: +34-93-6034011 Mobile: +32-486-672874 (Belgium) / +34-63-4408800 (Spain) / +46-707-360512 (Sweden) ```
 Re: [Swig-devel] [MATLAB] How to handle "ans" output? From: Joel Andersson - 2014-08-20 12:48:06 Attachments: Message as HTML ```Hi! I've taken a shot at fixing this issue as outlined above. I've committed this to my branch and made a pull request to Kris' branch. It appears to work fine now (did some testing on the generated wrapper for CasADi), but unfortunately I wasn't able to run the test suite (haven't got that to work here). Best regards! Joel 2014-08-19 21:31 GMT+02:00 Joel Andersson : > Hi! > > After some thinking, I would like to modify my suggestion: It should be > possible to check if _all_ the overloads of a particular (member) function > has at least one output argument. And for those functions, enforce an "ans" > output. > > That is, if a function has two overloads, and at least one of them void, > no "would be produced": > > void foo(double a, double b); > double foo(double a); // No "ans" produced! > > But if _all_ overloads have non-void returns, it an "ans" will be produced: > > double foo(double a, double b); // "ans" produced > double foo(double a); // "ans "produced > > I think the normal situation is that all the overloads are either non-void > or void and this will be covered by this. It will also not add any > noticeable overhead or larger wrapper files. Nor do we have to worry about > strange "[]" returns from void functions. > > What say you? > Joel > > P.S.: I could take a shot at the implementation, if we agree on a design. > > 2014-08-19 20:51 GMT+02:00 Kris Thielemans : > > From: Joel Andersson Sent: 19 August 2014 11:29 >> > >> > There is one thing I find particularly annoying when playing around >> with CasADi >> > from MATLAB; the fact that the "ans" argument in MATLAB is not generated >> > when you want it. >> > >> >> Yes, this is quite annoying, I agree. >> >> > A function such as arcsinh in CasADi is now wrapped as follows: >> > function varargout = function_name(varargin) >> > [varargout{1:nargout}] = >> > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); >> > end >> > >> > The problem is that if you type "asinh(x)" in MATLAB, nargout will be >> zero, so >> > nothing will be returned from the wrapper. To get an "ans" argument, >> you need >> > to replace "nargout" with 1, or more generally with max(nargout,1): >> > function varargout = function_name(varargin) >> > [varargout{1:max(1,nargout)}] = >> > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); >> > end >> > >> > My proposed resolution is to always return at least one output whenever >> any >> > overload of the function has any outputs arguments. This means that if >> you >> > have two overloaded functions, one without outputs and another with >> outputs, a >> > dummy output (0-by-0 matrix) would be returned from the overload with no >> > outputs: >> > >> > void foo(double a); // no outputs, but dummy output [] returned >> > int foo(double a, double b); // outputs >> > >> > Does this sound reasonable? In a MATLAB script, you'd put a semicolon >> after >> > foo(a) anyway, preventing the creation of []. >> > >> >> I don't find it 100% satisfactory (it's not entirely clean, and people >> might not automatically put the semicolon if the function shouldn't return >> anything) but it's definitely a better solution than what we have at >> present. Also, the above example is somewhat contrived (anyone making an >> API like that is asking for confusion) as normally the overloading happens >> based on the input arguments, but I guess we have to cover corner cases as >> well. >> >> I have no clear idea how you'd implement it though. When wrapping the >> code, you'd need to know which functions return void, and if so, return [], >> no? If so, I guess we could just as well modify the matlab class as well? >> (ok, that'd seems to mean we need to transport a lot of complication into >> the matlab code which we don't have now, so probably not a good idea). >> >> Another idea: >> As opposed to returning [], return a special object (e.g. SwigVoid). Once >> we do that, a few different options: >> - set its display properties to the empty string. So effectively you want >> see it. (hmmm) >> - let the matlab wrapper detect that. Something like this (not checked!) >> >> function varargout= swig_return(varargin) >> ( >> If (nargout==0 && nargin==1 && class(varargin{1}, 'SwigVoid')) >> return; >> else >> varargout = varargin; >> return; >> ) >> >> function varargout = function_name(varargin) >> [varargout{1:nargout}] = swig_return(... >> casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:})); >> end >> >> if we then make swig_return a method of SwigRef, it could work. Or >> indeed, make a method swig_call_wrapper that does this all in one go... >> >> not sure if this can work though >> >> Kris >> >> > > > -- > -- > Joel Andersson, PhD > Ptge. Busquets 11-13, atico 3 > E-08940 Cornella de Llobregat, Spain > Home: +34-93-6034011 > Mobile: +32-486-672874 (Belgium) / +34-63-4408800 (Spain) / > +46-707-360512 > (Sweden) > > -- -- Joel Andersson, PhD Ptge. Busquets 11-13, atico 3 E-08940 Cornella de Llobregat, Spain Home: +34-93-6034011 Mobile: +32-486-672874 (Belgium) / +34-63-4408800 (Spain) / +46-707-360512 (Sweden) ```
 Re: [Swig-devel] [MATLAB] How to handle "ans" output? From: Kris Thielemans - 2014-08-20 21:21:06 ```Hi Joel This works for me as well, so I've merged it in. thanks! > -----Original Message----- > From: Joel Andersson [mailto:j.a.e.andersson@...] > Sent: 20 August 2014 13:48 > To: Kris Thielemans > Cc: swig-devel@... > Subject: Re: [Swig-devel] [MATLAB] How to handle "ans" output? > > Hi! > > I've taken a shot at fixing this issue as outlined above. I've committed this to my > branch and made a pull request to Kris' branch. It appears to work fine now > (did some testing on the generated wrapper for CasADi), but unfortunately I > wasn't able to run the test suite (haven't got that to work here). > > Best regards! > Joel > > > > > 2014-08-19 21:31 GMT+02:00 Joel Andersson : > > > Hi! > > After some thinking, I would like to modify my suggestion: It should be > possible to check if _all_ the overloads of a particular (member) function has at > least one output argument. And for those functions, enforce an "ans" output. > > That is, if a function has two overloads, and at least one of them void, > no "would be produced": > > void foo(double a, double b); > double foo(double a); // No "ans" produced! > > But if _all_ overloads have non-void returns, it an "ans" will be > produced: > > double foo(double a, double b); // "ans" produced > double foo(double a); // "ans "produced > > I think the normal situation is that all the overloads are either non-void > or void and this will be covered by this. It will also not add any noticeable > overhead or larger wrapper files. Nor do we have to worry about strange "[]" > returns from void functions. > > What say you? > Joel > > P.S.: I could take a shot at the implementation, if we agree on a design. > > 2014-08-19 20:51 GMT+02:00 Kris Thielemans > : > > > From: Joel Andersson Sent: 19 August 2014 11:29 > > > > > There is one thing I find particularly annoying when playing > around with CasADi > > from MATLAB; the fact that the "ans" argument in MATLAB is > not generated > > when you want it. > > > > > Yes, this is quite annoying, I agree. > > > > A function such as arcsinh in CasADi is now wrapped as > follows: > > function varargout = function_name(varargin) > > [varargout{1:nargout}] = > > > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > > end > > > > The problem is that if you type "asinh(x)" in MATLAB, nargout > will be zero, so > > nothing will be returned from the wrapper. To get an "ans" > argument, you need > > to replace "nargout" with 1, or more generally with > max(nargout,1): > > function varargout = function_name(varargin) > > [varargout{1:max(1,nargout)}] = > > > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:}); > > end > > > > My proposed resolution is to always return at least one > output whenever any > > overload of the function has any outputs arguments. This > means that if you > > have two overloaded functions, one without outputs and > another with outputs, a > > dummy output (0-by-0 matrix) would be returned from the > overload with no > > outputs: > > > > void foo(double a); // no outputs, but dummy output [] > returned > > int foo(double a, double b); // outputs > > > > Does this sound reasonable? In a MATLAB script, you'd put a > semicolon after > > foo(a) anyway, preventing the creation of []. > > > > > I don't find it 100% satisfactory (it's not entirely clean, and > people might not automatically put the semicolon if the function shouldn't > return anything) but it's definitely a better solution than what we have at > present. Also, the above example is somewhat contrived (anyone making an API > like that is asking for confusion) as normally the overloading happens based on > the input arguments, but I guess we have to cover corner cases as well. > > I have no clear idea how you'd implement it though. When > wrapping the code, you'd need to know which functions return void, and if so, > return [], no? If so, I guess we could just as well modify the matlab class as > well? (ok, that'd seems to mean we need to transport a lot of complication into > the matlab code which we don't have now, so probably not a good idea). > > Another idea: > As opposed to returning [], return a special object (e.g. > SwigVoid). Once we do that, a few different options: > - set its display properties to the empty string. So effectively > you want see it. (hmmm) > - let the matlab wrapper detect that. Something like this (not > checked!) > > function varargout= swig_return(varargin) > ( > If (nargout==0 && nargin==1 && class(varargin{1}, > 'SwigVoid')) > return; > else > varargout = varargin; > return; > > ) > > function varargout = function_name(varargin) > > [varargout{1:nargout}] = swig_return(... > > casadi_coreMATLAB_wrap(1532,'SX_arcsinh',self,varargin{:})); > end > > if we then make swig_return a method of SwigRef, it could > work. Or indeed, make a method swig_call_wrapper that does this all in one > go... > > not sure if this can work though > > Kris > > > > > > > > -- > -- > Joel Andersson, PhD > Ptge. Busquets 11-13, atico 3 > E-08940 Cornella de Llobregat, Spain > Home: +34-93-6034011 > Mobile: +32-486-672874 (Belgium) / +34-63- > 4408800 (Spain) / +46-707-360512 > (Sweden) > > > > > > > -- > -- > Joel Andersson, PhD > Ptge. Busquets 11-13, atico 3 > E-08940 Cornella de Llobregat, Spain > Home: +34-93-6034011 > Mobile: +32-486-672874 (Belgium) / +34-63-4408800 (Spain) / +46-707-360512 > (Sweden) > ```