# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1400534313 -7200
#      Mon May 19 23:18:33 2014 +0200
# Node ID e5ab30b86283d13d0a0a61b1fcb859659e3ecb4f
# Parent  a13b7d744b86ed4adbaa2c90f3bbbc5ac6c7fc47
Changed order in interpolate signature

diff -r a13b7d744b86 -r e5ab30b86283 src/interpolate.cc
--- a/src/interpolate.cc	Sat May 17 17:16:58 2014 +0200
+++ b/src/interpolate.cc	Mon May 19 23:18:33 2014 +0200
@@ -21,16 +21,14 @@
 
 DEFUN_DLD (interpolate, args, nargout, "-*- texinfo -*-\n\
 @deftypefn {Function File} @var{interp} = \
-interpolate (@var{Function}, @var{v})\n\
-interpolate (@var{v}, @var{FunctionSpace})\n\
+interpolate (@var{name}, @var{u}, @var{v})\n\
 Interpolate a function on a FunctionSpace.\n\
-This function can be used in two ways:\n\
-@itemize @bullet\n\
-@item To interpolate a Function, Expression or Constant, \
-@var{v}, on the same FunctionSpace of a @var{Function}\n\
-@item To interpolate a Function, Expression or Constant, \
-@var{v}, on a given @var{FunctionSpace}\n\
-@end itemize\n\
+@var{u} is the Function, Expression or Constant \
+to interpolate. @var{v} may be a FunctionSpace \
+or a Function. In the latter case @var{u} is interpolated \
+on the same FunctionSpace where @var{v} is defined.\n\
+Note that @var{name} is optional: if not provided, \
+it will default to the one assigned to @var{u}.\n\
 @seealso{Function, Expression, Constant, FunctionSpace}\n\
 @end deftypefn")
 {
@@ -38,7 +36,7 @@
   int nargin = args.length ();
   octave_value retval;
   
-  if (nargin < 2 || nargin > 2 || nargout > 1)
+  if (nargin < 2 || nargin > 3 || nargout > 1)
     print_usage ();
   else
     {
@@ -63,37 +61,48 @@
           mlock ();
         }
 
-      if (args(1).type_id () == functionspace::static_type_id ())
+      octave_idx_type offset = 0;
+      std::string name;
+      if (args(0).is_string ())
+        {
+          name = args(0).string_value ();
+          offset = 1;
+        }
+
+      if (args(1+offset).type_id () == functionspace::static_type_id ())
         {
           const functionspace & u1 =
-            static_cast<const functionspace&> (args(1).get_rep ());
+            static_cast<const functionspace&> (args(1+offset).get_rep ());
 
           if (! error_state)
             {
               boost::shared_ptr<dolfin::Function>
                 output (new dolfin::Function (u1.get_pfsp ()));
 
-              if (args(0).type_id () == function::static_type_id ())
+              if (args(0+offset).type_id () == function::static_type_id ())
                 {
                   const function & u0 =
-                    static_cast<const function&> (args(0).get_rep ());
+                    static_cast<const function&> (args(0+offset).get_rep ());
 
                   if (! error_state)
                     {
                       output->interpolate (u0.get_fun ());
-                      std::string name = u0.get_str ();
+                      if (name.empty ())
+                        name = u0.get_str ();
                       retval = new function (name, output);
                     }
                 }
-              else if (args(0).type_id () == coefficient::static_type_id ())
+              else if (args(0+offset).type_id () ==
+                       coefficient::static_type_id ())
                 {
                   const coefficient & u0 =
-                    static_cast<const coefficient&> (args(0).get_rep ());
+                    static_cast<const coefficient&> (args(0+offset).get_rep ());
 
                   if (! error_state)
                     {
                       output->interpolate (* u0.get_expr ());
-                      std::string name = u0.get_str ();
+                      if (name.empty ())
+                        name = u0.get_str ();
                       retval = new function (name, output);
                     }
                 }
@@ -101,37 +110,40 @@
                 error ("interpolate: invalid arguments");
             }
         }
-      else if (args(0).type_id () == function::static_type_id ())
+      else if (args(1+offset).type_id () == function::static_type_id ())
         {
           const function & u0 =
-            static_cast<const function&> (args(0).get_rep ());
+            static_cast<const function&> (args(1+offset).get_rep ());
 
           if (! error_state)
             {
               boost::shared_ptr<dolfin::Function>
                 output (new dolfin::Function (u0.get_fun ()));
 
-              if (args(1).type_id () == function::static_type_id ())
+              if (args(0+offset).type_id () == function::static_type_id ())
                 {
                   const function & u1 =
-                    static_cast<const function&> (args(1).get_rep ());
+                    static_cast<const function&> (args(0+offset).get_rep ());
 
                   if (! error_state)
                     {
                       output->interpolate (u1.get_fun ());
-                      std::string name = u1.get_str ();
+                      if (name.empty ())
+                        name = u1.get_str ();
                       retval = new function (name, output);
                     }
                 }
-              else if (args(1).type_id () == coefficient::static_type_id ())
+              else if (args(0+offset).type_id () ==
+                       coefficient::static_type_id ())
                 {
                   const coefficient & u1 =
-                    static_cast<const coefficient&> (args(1).get_rep ());
+                    static_cast<const coefficient&> (args(0+offset).get_rep ());
 
                   if (! error_state)
                     {
                       output->interpolate (* u1.get_expr ());
-                      std::string name = u1.get_str ();
+                      if (name.empty ())
+                        name = u1.get_str ();
                       retval = new function (name, output);
                     }
                 }
