- my ($seq_template,$n)=@_;
- my ($res,$N);
-
- $res='';
- for ($N=1; $N<=$n; $N++) {
- $res .= eval('"' . $seq_template . '"');
- if ($N!=$n) {
- $res .= ', ';
- }
- }
- return $res;
+ my ($seq_template,$n)=@_;
+ my ($res,$N);
+
+ $res='';
+ for ($N=1; $N<=$n; $N++) {
+ $res .= eval('"' . $seq_template . '"');
+ if ($N!=$n) {
+ $res .= ', ';
+ }
+ }
+ return $res;
- my ($template,$seq_template1,$seq_template2,$from,$to)=@_;
- my ($res,$N,$SEQ);
-
- $res='';
- for ($N=$from; $N<=$to; $N++) {
- $SEQ1=generate_seq($seq_template1,$N);
- $SEQ2=generate_seq($seq_template2,$N);
- $res .= eval('"' . $template . '"');
- $SEQ1=''; # to avoid main::SEQ1 used only once warning
- $SEQ2=''; # same as above
- }
- return $res;
+ my ($template,$seq_template1,$seq_template2,$from,$to)=@_;
+ my ($res,$N,$SEQ);
+
+ $res='';
+ for ($N=$from; $N<=$to; $N++) {
+ $SEQ1=generate_seq($seq_template1,$N);
+ $SEQ2=generate_seq($seq_template2,$N);
+ $res .= eval('"' . $template . '"');
+ $SEQ1=''; # to avoid main::SEQ1 used only once warning
+ $SEQ2=''; # same as above
+ }
+ return $res;
- my ($template,$seq_template1,$seq_template2)=@_;
- return generate_from_to($template,$seq_template1,$seq_template2,1,$maxargs);
+ my ($template,$seq_template1,$seq_template2)=@_;
+ return generate_from_to($template,$seq_template1,$seq_template2,1,$maxargs);
}
$declare_function_macro_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE';
#define DECLARE_FUNCTION_1P(NAME) \
extern const unsigned function_index_##NAME; \
inline GiNaC::function NAME(const GiNaC::ex & p1) { \
}
$declare_function_macro_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE';
#define DECLARE_FUNCTION_1P(NAME) \
extern const unsigned function_index_##NAME; \
inline GiNaC::function NAME(const GiNaC::ex & p1) { \
}
#define DECLARE_FUNCTION_2P(NAME) \
extern const unsigned function_index_##NAME; \
inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::ex & p2) { \
}
#define DECLARE_FUNCTION_2P(NAME) \
extern const unsigned function_index_##NAME; \
inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::ex & p2) { \
#define DECLARE_FUNCTION_${N}P(NAME) \\
extern const unsigned function_index_##NAME; \\
inline GiNaC::function NAME(${SEQ1}) { \\
#define DECLARE_FUNCTION_${N}P(NAME) \\
extern const unsigned function_index_##NAME; \\
inline GiNaC::function NAME(${SEQ1}) { \\
#define DECLARE_FUNCTION_1P(NAME) \
extern const unsigned function_index_##NAME; \
inline function NAME(const ex & p1) { \
#define DECLARE_FUNCTION_1P(NAME) \
extern const unsigned function_index_##NAME; \
inline function NAME(const ex & p1) { \
}
#define DECLARE_FUNCTION_2P(NAME) \
extern const unsigned function_index_##NAME; \
inline function NAME(const ex & p1, const ex & p2) { \
}
#define DECLARE_FUNCTION_2P(NAME) \
extern const unsigned function_index_##NAME; \
inline function NAME(const ex & p1, const ex & p2) { \
#define DECLARE_FUNCTION_${N}P(NAME) \\
extern const unsigned function_index_##NAME; \\
inline function NAME(${SEQ1}) { \\
#define DECLARE_FUNCTION_${N}P(NAME) \\
extern const unsigned function_index_##NAME; \\
inline function NAME(${SEQ1}) { \\
- <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
- case ${N}:
- eval_result=((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
- break;
+ <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
+ case ${N}:
+ eval_result=((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
+ break;
- <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
- case ${N}:
- return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
- break;
+ <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
+ case ${N}:
+ return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
+ break;
- <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
- case ${N}:
- return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
- break;
+ <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
+ case ${N}:
+ return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
+ break;
- <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
- case ${N}:
- try {
- res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
- } catch (do_taylor) {
- res = basic::series(r, order, options);
- }
- return res;
- break;
+ <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
+ case ${N}:
+ try {
+ res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
+ } catch (do_taylor) {
+ res = basic::series(r, order, options);
+ }
+ return res;
+ break;
#define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
const unsigned function_index_##NAME= \\
#define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
const unsigned function_index_##NAME= \\
#else // ndef NO_NAMESPACE_GINAC
#define REGISTER_FUNCTION(NAME,OPT) \\
const unsigned function_index_##NAME= \\
#else // ndef NO_NAMESPACE_GINAC
#define REGISTER_FUNCTION(NAME,OPT) \\
const unsigned function_index_##NAME= \\
#define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
const unsigned function_index_##NAME= \\
#define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
const unsigned function_index_##NAME= \\
- function_options();
- function_options(std::string const & n, std::string const & tn=std::string());
- ~function_options();
- void initialize(void);
- function_options & set_name(std::string const & n, std::string const & tn=std::string());
+ function_options();
+ function_options(std::string const & n, std::string const & tn=std::string());
+ ~function_options();
+ void initialize(void);
+ function_options & set_name(std::string const & n, std::string const & tn=std::string());
// the following lines have been generated for max. ${maxargs} parameters
$eval_func_interface
$evalf_func_interface
$derivative_func_interface
$series_func_interface
// end of generated lines
// the following lines have been generated for max. ${maxargs} parameters
$eval_func_interface
$evalf_func_interface
$derivative_func_interface
$series_func_interface
// end of generated lines
- function_options & set_return_type(unsigned rt, unsigned rtt=0);
- function_options & do_not_evalf_params(void);
- function_options & remember(unsigned size, unsigned assoc_size=0,
- unsigned strategy=remember_strategies::delete_never);
- function_options & overloaded(unsigned o);
- void test_and_set_nparams(unsigned n);
- std::string get_name(void) const { return name; }
- unsigned get_nparams(void) const { return nparams; }
+ function_options & set_return_type(unsigned rt, unsigned rtt=0);
+ function_options & do_not_evalf_params(void);
+ function_options & remember(unsigned size, unsigned assoc_size=0,
+ unsigned strategy=remember_strategies::delete_never);
+ function_options & overloaded(unsigned o);
+ void test_and_set_nparams(unsigned n);
+ std::string get_name(void) const { return name; }
+ unsigned get_nparams(void) const { return nparams; }
- // end of generated lines
- function(unsigned ser, const exprseq & es);
- function(unsigned ser, const exvector & v, bool discardable=0);
- function(unsigned ser, exvector * vp); // vp will be deleted
+ // end of generated lines
+ function(unsigned ser, const exprseq & es);
+ function(unsigned ser, const exvector & v, bool discardable=0);
+ function(unsigned ser, exvector * vp); // vp will be deleted
- basic * duplicate() const;
- void printraw(std::ostream & os) const;
- void print(std::ostream & os, unsigned upper_precedence=0) const;
- void printtree(std::ostream & os, unsigned indent) const;
- void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence=0) const;
- ex expand(unsigned options=0) const;
- ex eval(int level=0) const;
- ex evalf(int level=0) const;
- ex series(const relational & r, int order, unsigned options = 0) const;
- ex thisexprseq(const exvector & v) const;
- ex thisexprseq(exvector * vp) const;
+ basic * duplicate() const;
+ void printraw(std::ostream & os) const;
+ void print(std::ostream & os, unsigned upper_precedence=0) const;
+ void printtree(std::ostream & os, unsigned indent) const;
+ void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence=0) const;
+ ex expand(unsigned options=0) const;
+ ex eval(int level=0) const;
+ ex evalf(int level=0) const;
+ ex series(const relational & r, int order, unsigned options = 0) const;
+ ex thisexprseq(const exvector & v) const;
+ ex thisexprseq(exvector * vp) const;
- ex derivative(const symbol & s) const;
- int compare_same_type(const basic & other) const;
- bool is_equal_same_type(const basic & other) const;
- unsigned return_type(void) const;
- unsigned return_type_tinfo(void) const;
-
- // new virtual functions which can be overridden by derived classes
- // none
-
- // non-virtual functions in this class
+ ex derivative(const symbol & s) const;
+ int compare_same_type(const basic & other) const;
+ bool is_equal_same_type(const basic & other) const;
+ unsigned return_type(void) const;
+ unsigned return_type_tinfo(void) const;
+
+ // new virtual functions which can be overridden by derived classes
+ // none
+
+ // non-virtual functions in this class
- ex pderivative(unsigned diff_param) const; // partial differentiation
- static std::vector<function_options> & registered_functions(void);
- bool lookup_remember_table(ex & result) const;
- void store_remember_table(ex const & result) const;
+ ex pderivative(unsigned diff_param) const; // partial differentiation
+ static std::vector<function_options> & registered_functions(void);
+ bool lookup_remember_table(ex & result) const;
+ void store_remember_table(ex const & result) const;
- static unsigned register_new(function_options const & opt);
- static unsigned find_function(const std::string &name, unsigned nparams);
- unsigned getserial(void) const {return serial;}
-
+ static unsigned register_new(function_options const & opt);
+ static unsigned find_function(const std::string &name, unsigned nparams);
+ unsigned getserial(void) const {return serial;}
+
- set_name("unnamed_function","\\\\operatorname{unnamed}");
- nparams=0;
- eval_f=evalf_f=derivative_f=series_f=0;
- evalf_params_first=true;
- use_return_type=false;
- use_remember=false;
- functions_with_same_name=1;
+ set_name("unnamed_function","\\\\operatorname{unnamed}");
+ nparams=0;
+ eval_f=evalf_f=derivative_f=series_f=0;
+ evalf_params_first=true;
+ use_return_type=false;
+ use_remember=false;
+ functions_with_same_name=1;
- if (nparams==0) {
- nparams=n;
- } else if (nparams!=n) {
- // we do not throw an exception here because this code is
- // usually executed before main(), so the exception could not
- // caught anyhow
- std::cerr << "WARNING: number of parameters ("
- << n << ") differs from number set before ("
- << nparams << ")" << std::endl;
- }
+ if (nparams==0) {
+ nparams=n;
+ } else if (nparams!=n) {
+ // we do not throw an exception here because this code is
+ // usually executed before main(), so the exception could not
+ // caught anyhow
+ std::cerr << "WARNING: number of parameters ("
+ << n << ") differs from number set before ("
+ << nparams << ")" << std::endl;
+ }
- debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
- tinfo_key = TINFO_function;
+ debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
+ tinfo_key = TINFO_function;
/** Construct object from archive_node. */
function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
{
/** Construct object from archive_node. */
function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
{
- debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
-
- // Find serial number by function name
- std::string s;
- if (n.find_string("name", s)) {
- unsigned int ser = 0;
- std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
- while (i != iend) {
- if (s == i->name) {
- serial = ser;
- return;
- }
- i++; ser++;
- }
- throw (std::runtime_error("unknown function '" + s + "' in archive"));
- } else
- throw (std::runtime_error("unnamed function in archive"));
+ debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
+
+ // Find serial number by function name
+ std::string s;
+ if (n.find_string("name", s)) {
+ unsigned int ser = 0;
+ std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
+ while (i != iend) {
+ if (s == i->name) {
+ serial = ser;
+ return;
+ }
+ i++; ser++;
+ }
+ throw (std::runtime_error("unknown function '" + s + "' in archive"));
+ } else
+ throw (std::runtime_error("unnamed function in archive"));
- os << registered_functions()[serial].name;
- printseq(os,'(',',',')',exprseq::precedence,function::precedence);
+ os << registered_functions()[serial].name;
+ printseq(os,'(',',',')',exprseq::precedence,function::precedence);
- debugmsg("function printtree",LOGLEVEL_PRINT);
-
- GINAC_ASSERT(serial<registered_functions().size());
-
- os << std::string(indent,' ') << "function "
- << registered_functions()[serial].name
- << ", hash=" << hashvalue
- << " (0x" << std::hex << hashvalue << std::dec << ")"
- << ", flags=" << flags
- << ", nops=" << nops() << std::endl;
- for (unsigned i=0; i<nops(); ++i) {
- seq[i].printtree(os,indent+delta_indent);
- }
- os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
+ debugmsg("function printtree",LOGLEVEL_PRINT);
+
+ GINAC_ASSERT(serial<registered_functions().size());
+
+ os << std::string(indent,' ') << "function "
+ << registered_functions()[serial].name
+ << ", hash=" << hashvalue
+ << " (0x" << std::hex << hashvalue << std::dec << ")"
+ << ", flags=" << flags
+ << ", nops=" << nops() << std::endl;
+ for (unsigned i=0; i<nops(); ++i) {
+ seq[i].printtree(os,indent+delta_indent);
+ }
+ os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
- exvector::const_iterator it = seq.begin();
- exvector::const_iterator itend = seq.end();
- while (it != itend) {
- it->bp->printcsrc(os, type, 0);
- it++;
- if (it != itend)
- os << ",";
- }
- os << ")";
+ exvector::const_iterator it = seq.begin();
+ exvector::const_iterator itend = seq.end();
+ while (it != itend) {
+ it->bp->printcsrc(os, type, 0);
+ it++;
+ if (it != itend)
+ os << ",";
+ }
+ os << ")";
- // end of generated lines
- default:
- throw(std::logic_error("function::eval(): invalid nparams"));
- }
- if (use_remember) {
- store_remember_table(eval_result);
- }
- return eval_result;
+ // end of generated lines
+ default:
+ throw(std::logic_error("function::eval(): invalid nparams"));
+ }
+ if (use_remember) {
+ store_remember_table(eval_result);
+ }
+ return eval_result;
- GINAC_ASSERT(serial<registered_functions().size());
-
- exvector eseq=evalfchildren(level);
-
- if (registered_functions()[serial].evalf_f==0) {
- return function(serial,eseq).hold();
- }
- switch (registered_functions()[serial].nparams) {
- // the following lines have been generated for max. ${maxargs} parameters
+ GINAC_ASSERT(serial<registered_functions().size());
+
+ exvector eseq=evalfchildren(level);
+
+ if (registered_functions()[serial].evalf_f==0) {
+ return function(serial,eseq).hold();
+ }
+ switch (registered_functions()[serial].nparams) {
+ // the following lines have been generated for max. ${maxargs} parameters
}
/** Implementation of ex::series for functions.
* \@see ex::series */
ex function::series(const relational & r, int order, unsigned options = 0) const
{
}
/** Implementation of ex::series for functions.
* \@see ex::series */
ex function::series(const relational & r, int order, unsigned options = 0) const
{
- GINAC_ASSERT(serial<registered_functions().size());
-
- if (registered_functions()[serial].series_f==0) {
- return basic::series(r, order);
- }
- ex res;
- switch (registered_functions()[serial].nparams) {
- // the following lines have been generated for max. ${maxargs} parameters
+ GINAC_ASSERT(serial<registered_functions().size());
+
+ if (registered_functions()[serial].series_f==0) {
+ return basic::series(r, order);
+ }
+ ex res;
+ switch (registered_functions()[serial].nparams) {
+ // the following lines have been generated for max. ${maxargs} parameters
- ex result;
-
- 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;
- for (unsigned i=0; i!=seq.size(); i++) {
- arg_diff = seq[i].diff(s);
- // We apply the chain rule only when it makes sense. This is not
- // just for performance reasons but also to allow functions to
- // throw when differentiated with respect to one of its arguments
- // without running into trouble with our automatic full
- // differentiation:
- if (!arg_diff.is_zero())
- result += pderivative(i)*arg_diff;
- }
- }
- return result;
+ ex result;
+
+ 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;
+ for (unsigned i=0; i!=seq.size(); i++) {
+ arg_diff = seq[i].diff(s);
+ // We apply the chain rule only when it makes sense. This is not
+ // just for performance reasons but also to allow functions to
+ // throw when differentiated with respect to one of its arguments
+ // without running into trouble with our automatic full
+ // differentiation:
+ if (!arg_diff.is_zero())
+ result += pderivative(i)*arg_diff;
+ }
+ }
+ return result;
- GINAC_ASSERT(serial<registered_functions().size());
-
- if (registered_functions()[serial].derivative_f==0) {
- return Derivative(*this, lst(diff_param));
- }
- switch (registered_functions()[serial].nparams) {
- // the following lines have been generated for max. ${maxargs} parameters
+ GINAC_ASSERT(serial<registered_functions().size());
+
+ if (registered_functions()[serial].derivative_f==0) {
+ return Derivative(*this, lst(diff_param));
+ }
+ switch (registered_functions()[serial].nparams) {
+ // the following lines have been generated for max. ${maxargs} parameters
- unsigned same_name=0;
- for (unsigned i=0; i<registered_functions().size(); ++i) {
- if (registered_functions()[i].name==opt.name) {
- same_name++;
- }
- }
- if (same_name>=opt.functions_with_same_name) {
- // we do not throw an exception here because this code is
- // usually executed before main(), so the exception could not
- // caught anyhow
- std::cerr << "WARNING: function name " << opt.name
- << " already in use!" << std::endl;
- }
- registered_functions().push_back(opt);
- if (opt.use_remember) {
- remember_table::remember_tables().
- push_back(remember_table(opt.remember_size,
- opt.remember_assoc_size,
- opt.remember_strategy));
- } else {
- remember_table::remember_tables().push_back(remember_table());
- }
- return registered_functions().size()-1;
+ unsigned same_name=0;
+ for (unsigned i=0; i<registered_functions().size(); ++i) {
+ if (registered_functions()[i].name==opt.name) {
+ same_name++;
+ }
+ }
+ if (same_name>=opt.functions_with_same_name) {
+ // we do not throw an exception here because this code is
+ // usually executed before main(), so the exception could not
+ // caught anyhow
+ std::cerr << "WARNING: function name " << opt.name
+ << " already in use!" << std::endl;
+ }
+ registered_functions().push_back(opt);
+ if (opt.use_remember) {
+ remember_table::remember_tables().
+ push_back(remember_table(opt.remember_size,
+ opt.remember_assoc_size,
+ opt.remember_strategy));
+ } else {
+ remember_table::remember_tables().push_back(remember_table());
+ }
+ 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 std::string &name, unsigned nparams)
{
}
/** Find serial number of function by name and number of parameters.
* Throws exception if function was not found. */
unsigned function::find_function(const std::string &name, unsigned nparams)
{
- std::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"));
+ std::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"));