# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1399923515 -7200
#      Mon May 12 21:38:35 2014 +0200
# Node ID 8a3361bfa4347f2fe4bda6ab778a2dfec54f24be
# Parent  5292e0614efcfba24c882bed3a0471b52785c979
interpolate function added

diff -r 5292e0614efc -r 8a3361bfa434 INDEX
--- a/INDEX	Mon Mar 10 08:58:56 2014 +0000
+++ b/INDEX	Mon May 12 21:38:35 2014 +0200
@@ -14,6 +14,7 @@
   Expression
   Function
   DirichletBC
+  interpolate
 Definition of the abstract Variational problem
   BilinearForm
   LinearForm
diff -r 5292e0614efc -r 8a3361bfa434 src/Makefile.in
--- a/src/Makefile.in	Mon Mar 10 08:58:56 2014 +0000
+++ b/src/Makefile.in	Mon May 12 21:38:35 2014 +0200
@@ -15,7 +15,8 @@
              plot_func.oct \
              plot_mesh.oct \
              SubSpace.oct \
-             feval.oct 
+             feval.oct \
+             interpolate.oct
 
 
 LIBS += -ldolfin
@@ -109,6 +110,12 @@
 feval.o: feval.cc function.h
 	$(MKOCTFILE) $(CPPFLAGS) -c feval.cc $(LDFLAGS) -o $@ -I.
 
+interpolate.oct: interpolate.o
+	$(MKOCTFILE) $(CPPFLAGS) interpolate.o -o $@ $(LDFLAGS) $(LIBS)
+
+interpolate.o: interpolate.cc function.h
+	$(MKOCTFILE) $(CPPFLAGS) -c interpolate.cc $(LDFLAGS) -o $@ -I.
+
 clean:
 	-rm -f *.o core octave-core *.oct *~ *.xml
 
diff -r 5292e0614efc -r 8a3361bfa434 src/interpolate.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/interpolate.cc	Mon May 12 21:38:35 2014 +0200
@@ -0,0 +1,65 @@
+/*
+ Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+
+ This program is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3 of the License, or (at your option) any later
+ version.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "function.h"
+
+DEFUN_DLD (interpolate, args, nargout, "-*- texinfo -*-\n\
+@deftypefn {Function File} @var{interpolated} =\
+interpolate (@var{f}, @var{g})\n\
+Interpolate Function @var{g} on @var{f}'s space. \n\
+@seealso{Function}\n\
+@end deftypefn")
+{
+
+  int nargin = args.length ();
+  octave_value retval;
+  
+  if (nargin < 2 || nargin > 2 || nargout > 1)
+    print_usage ();
+  else
+    {
+      if (! function_type_loaded)
+        {
+          function::register_type ();
+          function_type_loaded = true;
+          mlock ();
+        }
+
+      if (args(0).type_id () == function::static_type_id () &&
+          args(1).type_id () == function::static_type_id ())
+        {
+          const function & u0 =
+            static_cast<const function&> (args(0).get_rep ());
+          const function & u1 =
+            static_cast<const function&> (args(1).get_rep ());
+
+          if (!error_state)
+            {
+              boost::shared_ptr<dolfin::Function>
+                output (new dolfin::Function (u0.get_fun ()));
+              const dolfin::Function &
+                input = u1.get_fun ();
+
+              output->interpolate (input);
+              std::string name = u1.get_str ();
+
+              retval = new function (name, output);
+            }
+        }
+    }
+  return retval;
+}
# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1400339818 -7200
#      Sat May 17 17:16:58 2014 +0200
# Node ID a13b7d744b86ed4adbaa2c90f3bbbc5ac6c7fc47
# Parent  8a3361bfa4347f2fe4bda6ab778a2dfec54f24be
Added Python-like interpolate

diff -r 8a3361bfa434 -r a13b7d744b86 src/interpolate.cc
--- a/src/interpolate.cc	Mon May 12 21:38:35 2014 +0200
+++ b/src/interpolate.cc	Sat May 17 17:16:58 2014 +0200
@@ -15,13 +15,23 @@
  this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "coefficient.h"
 #include "function.h"
+#include "functionspace.h"
 
 DEFUN_DLD (interpolate, args, nargout, "-*- texinfo -*-\n\
-@deftypefn {Function File} @var{interpolated} =\
-interpolate (@var{f}, @var{g})\n\
-Interpolate Function @var{g} on @var{f}'s space. \n\
-@seealso{Function}\n\
+@deftypefn {Function File} @var{interp} = \
+interpolate (@var{Function}, @var{v})\n\
+interpolate (@var{v}, @var{FunctionSpace})\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\
+@seealso{Function, Expression, Constant, FunctionSpace}\n\
 @end deftypefn")
 {
 
@@ -39,27 +49,98 @@
           mlock ();
         }
 
-      if (args(0).type_id () == function::static_type_id () &&
-          args(1).type_id () == function::static_type_id ())
+      if (! coefficient_type_loaded)
+        {
+          coefficient::register_type ();
+          coefficient_type_loaded = true;
+          mlock ();
+        }
+
+      if (! functionspace_type_loaded)
+        {
+          functionspace::register_type ();
+          functionspace_type_loaded = true;
+          mlock ();
+        }
+
+      if (args(1).type_id () == functionspace::static_type_id ())
+        {
+          const functionspace & u1 =
+            static_cast<const functionspace&> (args(1).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 ())
+                {
+                  const function & u0 =
+                    static_cast<const function&> (args(0).get_rep ());
+
+                  if (! error_state)
+                    {
+                      output->interpolate (u0.get_fun ());
+                      std::string name = u0.get_str ();
+                      retval = new function (name, output);
+                    }
+                }
+              else if (args(0).type_id () == coefficient::static_type_id ())
+                {
+                  const coefficient & u0 =
+                    static_cast<const coefficient&> (args(0).get_rep ());
+
+                  if (! error_state)
+                    {
+                      output->interpolate (* u0.get_expr ());
+                      std::string name = u0.get_str ();
+                      retval = new function (name, output);
+                    }
+                }
+              else
+                error ("interpolate: invalid arguments");
+            }
+        }
+      else if (args(0).type_id () == function::static_type_id ())
         {
           const function & u0 =
             static_cast<const function&> (args(0).get_rep ());
-          const function & u1 =
-            static_cast<const function&> (args(1).get_rep ());
 
-          if (!error_state)
+          if (! error_state)
             {
               boost::shared_ptr<dolfin::Function>
                 output (new dolfin::Function (u0.get_fun ()));
-              const dolfin::Function &
-                input = u1.get_fun ();
 
-              output->interpolate (input);
-              std::string name = u1.get_str ();
+              if (args(1).type_id () == function::static_type_id ())
+                {
+                  const function & u1 =
+                    static_cast<const function&> (args(1).get_rep ());
 
-              retval = new function (name, output);
+                  if (! error_state)
+                    {
+                      output->interpolate (u1.get_fun ());
+                      std::string name = u1.get_str ();
+                      retval = new function (name, output);
+                    }
+                }
+              else if (args(1).type_id () == coefficient::static_type_id ())
+                {
+                  const coefficient & u1 =
+                    static_cast<const coefficient&> (args(1).get_rep ());
+
+                  if (! error_state)
+                    {
+                      output->interpolate (* u1.get_expr ());
+                      std::string name = u1.get_str ();
+                      retval = new function (name, output);
+                    }
+                }
+              else
+                error ("interpolate: invalid arguments");
             }
         }
+      else
+        error ("interpolate: invalid arguments");
     }
   return retval;
 }
