]> www.ginac.de Git - ginac.git/blobdiff - ginac/function.pl
- expressions can now be read from streams; the input expression can contain
[ginac.git] / ginac / function.pl
index 988206e9265ebfb231c607b1d75eb3bd4a321525..31893b2685e46991b685b0c4095bb270612c6706 100755 (executable)
@@ -1,5 +1,3 @@
-#!/usr/bin/perl -w
-
 $maxargs=13;
 
 sub generate_seq {
@@ -155,7 +153,7 @@ $typedef_derivative_funcp=generate(
 'const ex &','');
 
 $typedef_series_funcp=generate(
-'typedef ex (* series_funcp_${N})(${SEQ1}, const symbol &, const ex &, int);'."\n",
+'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int);'."\n",
 'const ex &','');
 
 $eval_func_interface=generate('    function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
@@ -205,9 +203,9 @@ $series_switch_statement=generate(
     <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
     case ${N}:
         try {
-            res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},s,point,order);
+            res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order);
         } catch (do_taylor) {
-            res = basic::series(s, point, order);
+            res = basic::series(r, order);
         }
         return res;
         break;
@@ -479,7 +477,7 @@ public:
     ex expand(unsigned options=0) const;
     ex eval(int level=0) const;
     ex evalf(int level=0) const;
-    ex series(const symbol & s, const ex & point, int order) const;
+    ex series(const relational & r, int order) const;
     ex thisexprseq(const exvector & v) const;
     ex thisexprseq(exvector * vp) const;
 protected:
@@ -500,6 +498,7 @@ protected:
     void store_remember_table(ex const & result) const;
 public:
     static unsigned register_new(function_options const & opt);
+    static unsigned find_function(const string &name, unsigned nparams);
     unsigned getserial(void) const {return serial;}
     
 // member variables
@@ -572,6 +571,7 @@ $implementation=<<END_OF_IMPLEMENTATION;
 
 #include "function.h"
 #include "ex.h"
+#include "lst.h"
 #include "archive.h"
 #include "inifcns.h"
 #include "utils.h"
@@ -946,12 +946,12 @@ ex function::thisexprseq(exvector * vp) const
 
 /** Implementation of ex::series for functions.
  *  \@see ex::series */
-ex function::series(const symbol & s, const ex & point, int order) const
+ex function::series(const relational & r, int order) const
 {
     GINAC_ASSERT(serial<registered_functions().size());
 
     if (registered_functions()[serial].series_f==0) {
-        return basic::series(s, point, order);
+        return basic::series(r, order);
     }
     ex res;
     switch (registered_functions()[serial].nparams) {
@@ -972,9 +972,24 @@ ex function::derivative(const symbol & s) const
 {
     ex result;
     
-    if (serial==function_index_Order) {
+    if (serial == function_index_Order) {
         // Order Term function only differentiates the argument
         return Order(seq[0].diff(s));
+    } else if (serial == function_index_Derivative) {
+        // Inert derivative performs chain rule on the first argument only, and
+        // adds differentiation parameter to list (second argument)
+        GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
+        GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
+        ex fcn = seq[0];
+        ex arg_diff;
+        for (unsigned i=0; i!=fcn.nops(); i++) {
+            arg_diff = fcn.op(i).diff(s);
+            if (!arg_diff.is_zero()) {
+                lst new_lst = ex_to_lst(seq[1]);
+                new_lst.append(i);
+                result += arg_diff * Derivative(fcn, new_lst);
+            }
+        }
     } else {
         // Chain rule
         ex arg_diff;
@@ -1045,7 +1060,7 @@ ex function::pderivative(unsigned diff_param) const // partial differentiation
     GINAC_ASSERT(serial<registered_functions().size());
     
     if (registered_functions()[serial].derivative_f==0) {
-        return Derivative(*this, diff_param);
+        return Derivative(*this, lst(diff_param));
     }
     switch (registered_functions()[serial].nparams) {
         // the following lines have been generated for max. ${maxargs} parameters
@@ -1100,6 +1115,21 @@ unsigned function::register_new(function_options const & opt)
     return registered_functions().size()-1;
 }
 
+/** Find serial number of function by name and number of parameters.
+ *  Throws exception if function was not found. */
+unsigned function::find_function(const string &name, unsigned nparams)
+{
+    vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
+    unsigned serial = 0;
+    while (i != end) {
+        if (i->get_name() == name && i->get_nparams() == nparams)
+            return serial;
+        i++;
+        serial++;
+    }
+    throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
+}
+
 //////////
 // static member variables
 //////////