view src/feval.cc @ 216:a28b50969020

New version of feval which accept a matrix or an array as points where the evaluation is done.
author gedeone-octave <marcovass89@hotmail.it>
date Wed, 05 Mar 2014 11:59:02 +0000
parents ca7eb016cf64
children b07ec30369ab
line wrap: on
line source

/*
 Copyright (C) 2013 Marco Vassallo <gedeone-octave@users.sourceforge.net>

 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 (feval, args, , "-*- texinfo -*-\n\
@deftypefn {Function File} {[@var{value}]} = \
feval (@var{function_name}, @var{Coordinate})\n\
Evaluate a function at a specific point of the domain and return the value. \n\
The input parameters are the function and the point where it has to\
be evaluated.\n\
The point can be either a vector or a matrix. If it is a matrix, each column\
represents a different point at which we evaluates the function.\n\
@seealso{Function}\n\
@end deftypefn")
{

  int nargin = args.length ();
  octave_value retval=0;
  
  if (nargin < 2 || nargin > 2)
    print_usage ();
  else
    {
      if (! function_type_loaded)
        {
          function::register_type ();
          function_type_loaded = true;
          mlock ();
        }

      if (args(0).type_id () == function::static_type_id ())
        {
          const function & fspo =
            static_cast<const function&> (args(0).get_rep ());
          Matrix point= args(1).matrix_value ();

          if (!error_state)
            {
              const boost::shared_ptr<const dolfin::Function> 
                & f = fspo.get_pfun ();

              if (point.rows () == 1)
                point = point.transpose ();

              dim_vector dims;
              dims.resize (2);
              dims(0) = f->value_dimension (0);
              dims(1) = point.cols ();
              Matrix res (dims);

              dim_vector dims_tmp;
              dims_tmp.resize (2);
              dims_tmp(0) = f->value_dimension (0);
              dims_tmp(1) = 1;
              Array<double> res_tmp (dims_tmp);

              for (uint i = 0; i < point.cols (); ++i)
                {
                  Array<double> point_tmp = point.column (i);
                  dolfin::Array<double> 
                    x (point_tmp.length (), point_tmp.fortran_vec ());
                  dolfin::Array<double> 
                    values (res_tmp.length (), res_tmp.fortran_vec ());
                  f->eval (values, x);
                  for (uint j = 0; j < res_tmp.length (); ++j)
                    res(i, j) = res_tmp (j);
                }
              retval = octave_value (res);
            }
        }
    }
  return retval;
}