3 * Implementation of class of symbolic functions. */
6 * This file was generated automatically by function.py.
7 * Please do not modify it directly, edit function.cppy instead!
8 * function.py options: maxargs=@maxargs@
10 * GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "operators.h"
29 #include "fderivative.h"
39 #include "hash_seed.h"
51 // helper class function_options
54 function_options::function_options()
59 function_options::function_options(std::string const & n, std::string const & tn)
65 function_options::function_options(std::string const & n, unsigned np)
68 set_name(n, std::string());
72 function_options::~function_options()
74 // nothing to clean up at the moment
77 void function_options::initialize()
79 set_name("unnamed_function", "\\\\mbox{unnamed}");
81 eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = expand_f
82 = derivative_f = expl_derivative_f = power_f = series_f = 0;
84 evalf_params_first = true;
85 use_return_type = false;
86 eval_use_exvector_args = false;
87 evalf_use_exvector_args = false;
88 conjugate_use_exvector_args = false;
89 real_part_use_exvector_args = false;
90 imag_part_use_exvector_args = false;
91 expand_use_exvector_args = false;
92 derivative_use_exvector_args = false;
93 expl_derivative_use_exvector_args = false;
94 power_use_exvector_args = false;
95 series_use_exvector_args = false;
96 print_use_exvector_args = false;
97 info_use_exvector_args = false;
99 functions_with_same_name = 1;
103 function_options & function_options::set_name(std::string const & n,
104 std::string const & tn)
107 if (tn==std::string())
108 TeX_name = "\\\\mbox{"+name+"}";
114 function_options & function_options::latex_name(std::string const & tn)
120 // the following lines have been generated for max. @maxargs@ parameters
121 +++ for method, N in [ (f, N) for f in methods[0:-1] for N in range(1, maxargs + 1)]:
122 function_options & function_options::@method@_func(@method@_funcp_@N@ e)
124 test_and_set_nparams(@N@);
125 @method@_f = @method@_funcp(e);
129 // end of generated lines
131 +++ for method in methods[0:-1]:
132 function_options & function_options::@method@_func(@method@_funcp_exvector e)
134 @method@_use_exvector_args = true;
135 @method@_f = @method@_funcp(e);
140 // end of generated lines
142 function_options & function_options::set_return_type(unsigned rt, const return_type_t* rtt)
144 use_return_type = true;
147 return_type_tinfo = *rtt;
149 return_type_tinfo = make_return_type_t<function>();
153 function_options & function_options::do_not_evalf_params()
155 evalf_params_first = false;
159 function_options & function_options::remember(unsigned size,
164 remember_size = size;
165 remember_assoc_size = assoc_size;
166 remember_strategy = strategy;
170 function_options & function_options::overloaded(unsigned o)
172 functions_with_same_name = o;
176 function_options & function_options::set_symmetry(const symmetry & s)
182 void function_options::test_and_set_nparams(unsigned n)
186 } else if (nparams!=n) {
187 // we do not throw an exception here because this code is
188 // usually executed before main(), so the exception could not
190 std::cerr << "WARNING: " << name << "(): number of parameters ("
191 << n << ") differs from number set before ("
192 << nparams << ")" << std::endl;
196 void function_options::set_print_func(unsigned id, print_funcp f)
198 if (id >= print_dispatch_table.size())
199 print_dispatch_table.resize(id + 1);
200 print_dispatch_table[id] = f;
203 /** This can be used as a hook for external applications. */
204 unsigned function::current_serial = 0;
207 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
210 // default constructor
215 function::function() : serial(0)
220 // other constructors
225 function::function(unsigned ser) : serial(ser)
229 // the following lines have been generated for max. @maxargs@ parameters
230 +++ for N in range(1, maxargs + 1):
231 function::function(unsigned ser, @seq('const ex & param%(n)d', N)@)
232 : exprseq(@seq('param%(n)d', N)@), serial(ser)
237 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
240 // Force re-evaluation even if the exprseq was already evaluated
241 // (the exprseq copy constructor copies the flags)
242 clearflag(status_flags::evaluated);
245 function::function(unsigned ser, const exvector & v, bool discardable)
246 : exprseq(v,discardable), serial(ser)
250 function::function(unsigned ser, exvector && v)
251 : exprseq(std::move(v)), serial(ser)
259 /** Construct object from archive_node. */
260 void function::read_archive(const archive_node& n, lst& sym_lst)
262 inherited::read_archive(n, sym_lst);
263 // Find serial number by function name
265 if (n.find_string("name", s)) {
266 unsigned int ser = 0;
267 for (auto & it : registered_functions()) {
274 throw (std::runtime_error("unknown function '" + s + "' in archive"));
276 throw (std::runtime_error("unnamed function in archive"));
279 /** Archive the object. */
280 void function::archive(archive_node &n) const
282 inherited::archive(n);
283 GINAC_ASSERT(serial < registered_functions().size());
284 n.add_string("name", registered_functions()[serial].name);
287 GINAC_BIND_UNARCHIVER(function);
290 // functions overriding virtual functions from base classes
295 void function::print(const print_context & c, unsigned level) const
297 GINAC_ASSERT(serial<registered_functions().size());
298 const function_options &opt = registered_functions()[serial];
299 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
301 // Dynamically dispatch on print_context type
302 const print_context_class_info *pc_info = &c.get_class_info();
305 unsigned id = pc_info->options.get_id();
306 if (id >= pdt.size() || pdt[id] == nullptr) {
308 // Method not found, try parent print_context class
309 const print_context_class_info *parent_pc_info = pc_info->get_parent();
310 if (parent_pc_info) {
311 pc_info = parent_pc_info;
315 // Method still not found, use default output
316 if (is_a<print_tree>(c)) {
318 c.s << std::string(level, ' ') << class_name() << " "
319 << opt.name << " @" << this
320 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
321 << ", nops=" << nops()
323 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
324 for (size_t i=0; i<seq.size(); ++i)
325 seq[i].print(c, level + delta_indent);
326 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
328 } else if (is_a<print_csrc>(c)) {
330 // Print function name in lowercase
331 std::string lname = opt.name;
332 size_t num = lname.size();
333 for (size_t i=0; i<num; i++)
334 lname[i] = tolower(lname[i]);
336 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
338 } else if (is_a<print_latex>(c)) {
340 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
343 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
348 // Method found, call it
349 current_serial = serial;
350 if (opt.print_use_exvector_args)
351 ((print_funcp_exvector)pdt[id])(seq, c);
352 else switch (opt.nparams) {
353 // the following lines have been generated for max. @maxargs@ parameters
354 +++ for N in range(1, maxargs + 1):
356 ((print_funcp_@N@)(pdt[id]))(@seq('seq[%(n)d]', N, 0)@, c);
359 // end of generated lines
361 throw(std::logic_error("function::print(): invalid nparams"));
366 ex function::eval(int level) const
369 // first evaluate children, then we will end up here again
370 return function(serial,evalchildren(level));
373 GINAC_ASSERT(serial<registered_functions().size());
374 const function_options &opt = registered_functions()[serial];
376 // Canonicalize argument order according to the symmetry properties
377 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
379 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
380 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
381 if (sig != std::numeric_limits<int>::max()) {
382 // Something has changed while sorting arguments, more evaluations later
385 return ex(sig) * thiscontainer(v);
393 bool use_remember = opt.use_remember;
395 if (use_remember && lookup_remember_table(eval_result)) {
398 current_serial = serial;
399 if (opt.eval_use_exvector_args)
400 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
402 switch (opt.nparams) {
403 // the following lines have been generated for max. @maxargs@ parameters
404 +++ for N in range(1, maxargs + 1):
406 eval_result = ((eval_funcp_@N@)(opt.eval_f))(@seq('seq[%(n)d]', N, 0)@);
409 // end of generated lines
411 throw(std::logic_error("function::eval(): invalid nparams"));
414 store_remember_table(eval_result);
419 ex function::evalf(int level) const
421 GINAC_ASSERT(serial<registered_functions().size());
422 const function_options &opt = registered_functions()[serial];
424 // Evaluate children first
426 if (level == 1 || !(opt.evalf_params_first))
428 else if (level == -max_recursion_level)
429 throw(std::runtime_error("max recursion level reached"));
431 eseq.reserve(seq.size());
433 for (auto & it : seq) {
434 eseq.push_back(it.evalf(level));
438 if (opt.evalf_f==0) {
439 return function(serial,eseq).hold();
441 current_serial = serial;
442 if (opt.evalf_use_exvector_args)
443 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
444 switch (opt.nparams) {
445 // the following lines have been generated for max. @maxargs@ parameters
446 +++ for N in range(1, maxargs + 1):
448 return ((evalf_funcp_@N@)(opt.evalf_f))(@seq('eseq[%(n)d]', N, 0)@);
450 // end of generated lines
452 throw(std::logic_error("function::evalf(): invalid nparams"));
456 * This method is defined to be in line with behaviour of function::return_type()
458 ex function::eval_ncmul(const exvector & v) const
460 // If this function is called then the list of arguments is non-empty
461 // and the first argument is non-commutative, see function::return_type()
462 return seq.begin()->eval_ncmul(v);
465 unsigned function::calchash() const
467 unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial);
468 for (size_t i=0; i<nops(); i++) {
470 v ^= this->op(i).gethash();
473 if (flags & status_flags::evaluated) {
474 setflag(status_flags::hash_calculated);
480 ex function::thiscontainer(const exvector & v) const
482 return function(serial, v);
485 ex function::thiscontainer(exvector && v) const
487 return function(serial, std::move(v));
490 /** Implementation of ex::series for functions.
491 * \@see ex::series */
492 ex function::series(const relational & r, int order, unsigned options) const
494 GINAC_ASSERT(serial<registered_functions().size());
495 const function_options &opt = registered_functions()[serial];
497 if (opt.series_f==0) {
498 return basic::series(r, order);
501 current_serial = serial;
502 if (opt.series_use_exvector_args) {
504 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
505 } catch (do_taylor) {
506 res = basic::series(r, order, options);
510 switch (opt.nparams) {
511 // the following lines have been generated for max. @maxargs@ parameters
512 +++ for N in range(1, maxargs + 1):
515 res = ((series_funcp_@N@)(opt.series_f))(@seq('seq[%(n)d]', N, 0)@, r, order, options);
516 } catch (do_taylor) {
517 res = basic::series(r, order, options);
521 // end of generated lines
523 throw(std::logic_error("function::series(): invalid nparams"));
526 /** Implementation of ex::conjugate for functions. */
527 ex function::conjugate() const
529 GINAC_ASSERT(serial<registered_functions().size());
530 const function_options & opt = registered_functions()[serial];
532 if (opt.conjugate_f==0) {
533 return conjugate_function(*this).hold();
536 if (opt.conjugate_use_exvector_args) {
537 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
540 switch (opt.nparams) {
541 // the following lines have been generated for max. @maxargs@ parameters
542 +++ for N in range(1, maxargs + 1):
544 return ((conjugate_funcp_@N@)(opt.conjugate_f))(@seq('seq[%(n)d]', N, 0)@);
546 // end of generated lines
548 throw(std::logic_error("function::conjugate(): invalid nparams"));
551 /** Implementation of ex::real_part for functions. */
552 ex function::real_part() const
554 GINAC_ASSERT(serial<registered_functions().size());
555 const function_options & opt = registered_functions()[serial];
557 if (opt.real_part_f==0)
558 return basic::real_part();
560 if (opt.real_part_use_exvector_args)
561 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
563 switch (opt.nparams) {
564 // the following lines have been generated for max. @maxargs@ parameters
565 +++ for N in range(1, maxargs + 1):
567 return ((real_part_funcp_@N@)(opt.real_part_f))(@seq('seq[%(n)d]', N, 0)@);
569 // end of generated lines
571 throw(std::logic_error("function::real_part(): invalid nparams"));
574 /** Implementation of ex::imag_part for functions. */
575 ex function::imag_part() const
577 GINAC_ASSERT(serial<registered_functions().size());
578 const function_options & opt = registered_functions()[serial];
580 if (opt.imag_part_f==0)
581 return basic::imag_part();
583 if (opt.imag_part_use_exvector_args)
584 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
586 switch (opt.nparams) {
587 // the following lines have been generated for max. @maxargs@ parameters
588 +++ for N in range(1, maxargs + 1):
590 return ((imag_part_funcp_@N@)(opt.imag_part_f))(@seq('seq[%(n)d]', N, 0)@);
592 // end of generated lines
594 throw(std::logic_error("function::imag_part(): invalid nparams"));
597 /** Implementation of ex::info for functions. */
598 bool function::info(unsigned inf) const
600 GINAC_ASSERT(serial<registered_functions().size());
601 const function_options & opt = registered_functions()[serial];
604 return basic::info(inf);
607 if (opt.info_use_exvector_args) {
608 return ((info_funcp_exvector)(opt.info_f))(seq, inf);
611 switch (opt.nparams) {
612 // the following lines have been generated for max. @maxargs@ parameters
613 +++ for N in range(1, maxargs + 1):
615 return ((info_funcp_@N@)(opt.info_f))(@seq('seq[%(n)d]', N, 0)@, inf);
617 // end of generated lines
619 throw(std::logic_error("function::info(): invalid nparams"));
624 /** Implementation of ex::diff() for functions. It applies the chain rule,
625 * except for the Order term function.
627 ex function::derivative(const symbol & s) const
632 // Explicit derivation
633 result = expl_derivative(s);
637 size_t num = seq.size();
638 for (size_t i=0; i<num; i++) {
639 arg_diff = seq[i].diff(s);
640 // We apply the chain rule only when it makes sense. This is not
641 // just for performance reasons but also to allow functions to
642 // throw when differentiated with respect to one of its arguments
643 // without running into trouble with our automatic full
645 if (!arg_diff.is_zero())
646 result += pderivative(i)*arg_diff;
652 int function::compare_same_type(const basic & other) const
654 GINAC_ASSERT(is_a<function>(other));
655 const function & o = static_cast<const function &>(other);
657 if (serial != o.serial)
658 return serial < o.serial ? -1 : 1;
660 return exprseq::compare_same_type(o);
663 bool function::is_equal_same_type(const basic & other) const
665 GINAC_ASSERT(is_a<function>(other));
666 const function & o = static_cast<const function &>(other);
668 if (serial != o.serial)
671 return exprseq::is_equal_same_type(o);
674 bool function::match_same_type(const basic & other) const
676 GINAC_ASSERT(is_a<function>(other));
677 const function & o = static_cast<const function &>(other);
679 return serial == o.serial;
682 unsigned function::return_type() const
684 GINAC_ASSERT(serial<registered_functions().size());
685 const function_options &opt = registered_functions()[serial];
687 if (opt.use_return_type) {
688 // Return type was explicitly specified
689 return opt.return_type;
691 // Default behavior is to use the return type of the first
692 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
694 return return_types::commutative;
696 return seq.begin()->return_type();
700 return_type_t function::return_type_tinfo() const
702 GINAC_ASSERT(serial<registered_functions().size());
703 const function_options &opt = registered_functions()[serial];
705 if (opt.use_return_type) {
706 // Return type was explicitly specified
707 return opt.return_type_tinfo;
709 // Default behavior is to use the return type of the first
710 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
712 return make_return_type_t<function>();
714 return seq.begin()->return_type_tinfo();
719 // new virtual functions which can be overridden by derived classes
725 // non-virtual functions in this class
730 ex function::pderivative(unsigned diff_param) const // partial differentiation
732 GINAC_ASSERT(serial<registered_functions().size());
733 const function_options &opt = registered_functions()[serial];
735 // No derivative defined? Then return abstract derivative object
736 if (opt.derivative_f == nullptr)
737 return fderivative(serial, diff_param, seq);
739 current_serial = serial;
740 if (opt.derivative_use_exvector_args)
741 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
742 switch (opt.nparams) {
743 // the following lines have been generated for max. @maxargs@ parameters
744 +++ for N in range(1, maxargs + 1):
746 return ((derivative_funcp_@N@)(opt.derivative_f))(@seq('seq[%(n)d]', N, 0)@, diff_param);
748 // end of generated lines
750 throw(std::logic_error("function::pderivative(): no diff function defined"));
753 ex function::expl_derivative(const symbol & s) const // explicit differentiation
755 GINAC_ASSERT(serial<registered_functions().size());
756 const function_options &opt = registered_functions()[serial];
758 // No explicit derivative defined? Then this function shall not be called!
759 if (opt.expl_derivative_f == nullptr)
760 throw(std::logic_error("function::expl_derivative(): explicit derivation is called, but no such function defined"));
762 current_serial = serial;
763 if (opt.expl_derivative_use_exvector_args)
764 return ((expl_derivative_funcp_exvector)(opt.expl_derivative_f))(seq, s);
765 switch (opt.nparams) {
766 // the following lines have been generated for max. @maxargs@ parameters
767 +++ for N in range(1, maxargs + 1):
769 return ((expl_derivative_funcp_@N@)(opt.expl_derivative_f))(@seq('seq[%(n)d]', N, 0)@, s);
771 // end of generated lines
775 ex function::power(const ex & power_param) const // power of function
777 GINAC_ASSERT(serial<registered_functions().size());
778 const function_options &opt = registered_functions()[serial];
780 if (opt.power_f == nullptr)
781 return (new GiNaC::power(*this, power_param))->setflag(status_flags::dynallocated |
782 status_flags::evaluated);
784 current_serial = serial;
785 if (opt.power_use_exvector_args)
786 return ((power_funcp_exvector)(opt.power_f))(seq, power_param);
787 switch (opt.nparams) {
788 // the following lines have been generated for max. @maxargs@ parameters
789 +++ for N in range(1, maxargs + 1):
791 return ((power_funcp_@N@)(opt.power_f))(@seq('seq[%(n)d]', N, 0)@, power_param);
793 // end of generated lines
795 throw(std::logic_error("function::power(): no power function defined"));
798 ex function::expand(unsigned options) const
800 GINAC_ASSERT(serial<registered_functions().size());
801 const function_options &opt = registered_functions()[serial];
803 // No expand defined? Then return the same function with expanded arguments (if required)
804 if (opt.expand_f == nullptr) {
805 // Only expand arguments when asked to do so
806 if (options & expand_options::expand_function_args)
807 return inherited::expand(options);
809 return (options == 0) ? setflag(status_flags::expanded) : *this;
812 current_serial = serial;
813 if (opt.expand_use_exvector_args)
814 return ((expand_funcp_exvector)(opt.expand_f))(seq, options);
815 switch (opt.nparams) {
816 // the following lines have been generated for max. @maxargs@ parameters
817 +++ for N in range(1, maxargs + 1):
819 return ((expand_funcp_@N@)(opt.expand_f))(@seq('seq[%(n)d]', N, 0)@, options);
821 // end of generated lines
823 throw(std::logic_error("function::expand(): no expand of function defined"));
826 std::vector<function_options> & function::registered_functions()
828 static std::vector<function_options> rf = std::vector<function_options>();
832 bool function::lookup_remember_table(ex & result) const
834 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
837 void function::store_remember_table(ex const & result) const
839 remember_table::remember_tables()[this->serial].add_entry(*this,result);
844 unsigned function::register_new(function_options const & opt)
846 size_t same_name = 0;
847 for (size_t i=0; i<registered_functions().size(); ++i) {
848 if (registered_functions()[i].name==opt.name) {
852 if (same_name>=opt.functions_with_same_name) {
853 // we do not throw an exception here because this code is
854 // usually executed before main(), so the exception could not
856 std::cerr << "WARNING: function name " << opt.name
857 << " already in use!" << std::endl;
859 registered_functions().push_back(opt);
860 if (opt.use_remember) {
861 remember_table::remember_tables().
862 push_back(remember_table(opt.remember_size,
863 opt.remember_assoc_size,
864 opt.remember_strategy));
866 remember_table::remember_tables().push_back(remember_table());
868 return registered_functions().size()-1;
871 /** Find serial number of function by name and number of parameters.
872 * Throws exception if function was not found. */
873 unsigned function::find_function(const std::string &name, unsigned nparams)
876 for (auto & it : function::registered_functions()) {
877 if (it.get_name() == name && it.get_nparams() == nparams)
881 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
884 /** Return the print name of the function. */
885 std::string function::get_name() const
887 GINAC_ASSERT(serial<registered_functions().size());
888 return registered_functions()[serial].name;