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"
38 #include "hash_seed.h"
50 // helper class function_options
53 function_options::function_options()
58 function_options::function_options(std::string const & n, std::string const & tn)
64 function_options::function_options(std::string const & n, unsigned np)
67 set_name(n, std::string());
71 function_options::~function_options()
73 // nothing to clean up at the moment
76 void function_options::initialize()
78 set_name("unnamed_function", "\\\\mbox{unnamed}");
80 eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = expand_f
81 = derivative_f = expl_derivative_f = power_f = series_f = nullptr;
83 evalf_params_first = true;
84 use_return_type = false;
85 eval_use_exvector_args = false;
86 evalf_use_exvector_args = false;
87 conjugate_use_exvector_args = false;
88 real_part_use_exvector_args = false;
89 imag_part_use_exvector_args = false;
90 expand_use_exvector_args = false;
91 derivative_use_exvector_args = false;
92 expl_derivative_use_exvector_args = false;
93 power_use_exvector_args = false;
94 series_use_exvector_args = false;
95 print_use_exvector_args = false;
96 info_use_exvector_args = false;
98 functions_with_same_name = 1;
102 function_options & function_options::set_name(std::string const & n,
103 std::string const & tn)
106 if (tn==std::string())
107 TeX_name = "\\\\mbox{"+name+"}";
113 function_options & function_options::latex_name(std::string const & tn)
119 // the following lines have been generated for max. @maxargs@ parameters
120 +++ for method, N in [ (f, N) for f in methods[0:-1] for N in range(1, maxargs + 1)]:
121 function_options & function_options::@method@_func(@method@_funcp_@N@ e)
123 test_and_set_nparams(@N@);
124 @method@_f = @method@_funcp(e);
128 // end of generated lines
130 +++ for method in methods[0:-1]:
131 function_options & function_options::@method@_func(@method@_funcp_exvector e)
133 @method@_use_exvector_args = true;
134 @method@_f = @method@_funcp(e);
139 // end of generated lines
141 function_options & function_options::set_return_type(unsigned rt, const return_type_t* rtt)
143 use_return_type = true;
146 return_type_tinfo = *rtt;
148 return_type_tinfo = make_return_type_t<function>();
152 function_options & function_options::do_not_evalf_params()
154 evalf_params_first = false;
158 function_options & function_options::remember(unsigned size,
163 remember_size = size;
164 remember_assoc_size = assoc_size;
165 remember_strategy = strategy;
169 function_options & function_options::overloaded(unsigned o)
171 functions_with_same_name = o;
175 function_options & function_options::set_symmetry(const symmetry & s)
181 void function_options::test_and_set_nparams(unsigned n)
185 } else if (nparams!=n) {
186 // we do not throw an exception here because this code is
187 // usually executed before main(), so the exception could not
189 std::cerr << "WARNING: " << name << "(): number of parameters ("
190 << n << ") differs from number set before ("
191 << nparams << ")" << std::endl;
195 void function_options::set_print_func(unsigned id, print_funcp f)
197 if (id >= print_dispatch_table.size())
198 print_dispatch_table.resize(id + 1);
199 print_dispatch_table[id] = f;
202 /** This can be used as a hook for external applications. */
203 unsigned function::current_serial = 0;
206 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
209 // default constructor
214 function::function() : serial(0)
219 // other constructors
224 function::function(unsigned ser) : serial(ser)
228 // the following lines have been generated for max. @maxargs@ parameters
229 +++ for N in range(1, maxargs + 1):
230 function::function(unsigned ser, @seq('const ex & param%(n)d', N)@)
231 : exprseq{@seq('param%(n)d', N)@}, serial(ser)
236 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
239 // Force re-evaluation even if the exprseq was already evaluated
240 // (the exprseq copy constructor copies the flags)
241 clearflag(status_flags::evaluated);
244 function::function(unsigned ser, const exvector & v)
245 : exprseq(v), serial(ser)
249 function::function(unsigned ser, exvector && v)
250 : exprseq(std::move(v)), serial(ser)
258 /** Construct object from archive_node. */
259 void function::read_archive(const archive_node& n, lst& sym_lst)
261 inherited::read_archive(n, sym_lst);
262 // Find serial number by function name
264 if (n.find_string("name", s)) {
265 unsigned int ser = 0;
266 for (auto & it : registered_functions()) {
273 throw (std::runtime_error("unknown function '" + s + "' in archive"));
275 throw (std::runtime_error("unnamed function in archive"));
278 /** Archive the object. */
279 void function::archive(archive_node &n) const
281 inherited::archive(n);
282 GINAC_ASSERT(serial < registered_functions().size());
283 n.add_string("name", registered_functions()[serial].name);
286 GINAC_BIND_UNARCHIVER(function);
289 // functions overriding virtual functions from base classes
294 void function::print(const print_context & c, unsigned level) const
296 GINAC_ASSERT(serial<registered_functions().size());
297 const function_options &opt = registered_functions()[serial];
298 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
300 // Dynamically dispatch on print_context type
301 const print_context_class_info *pc_info = &c.get_class_info();
304 unsigned id = pc_info->options.get_id();
305 if (id >= pdt.size() || pdt[id] == nullptr) {
307 // Method not found, try parent print_context class
308 const print_context_class_info *parent_pc_info = pc_info->get_parent();
309 if (parent_pc_info) {
310 pc_info = parent_pc_info;
314 // Method still not found, use default output
315 if (is_a<print_tree>(c)) {
317 c.s << std::string(level, ' ') << class_name() << " "
318 << opt.name << " @" << this
319 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
320 << ", nops=" << nops()
322 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
323 for (size_t i=0; i<seq.size(); ++i)
324 seq[i].print(c, level + delta_indent);
325 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
327 } else if (is_a<print_csrc>(c)) {
329 // Print function name in lowercase
330 std::string lname = opt.name;
331 size_t num = lname.size();
332 for (size_t i=0; i<num; i++)
333 lname[i] = tolower(lname[i]);
335 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
337 } else if (is_a<print_latex>(c)) {
339 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
342 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
347 // Method found, call it
348 current_serial = serial;
349 if (opt.print_use_exvector_args)
350 ((print_funcp_exvector)pdt[id])(seq, c);
351 else switch (opt.nparams) {
352 // the following lines have been generated for max. @maxargs@ parameters
353 +++ for N in range(1, maxargs + 1):
355 ((print_funcp_@N@)(pdt[id]))(@seq('seq[%(n)d]', N, 0)@, c);
358 // end of generated lines
360 throw(std::logic_error("function::print(): invalid nparams"));
365 ex function::eval(int level) const
368 // first evaluate children, then we will end up here again
369 return function(serial,evalchildren(level));
372 GINAC_ASSERT(serial<registered_functions().size());
373 const function_options &opt = registered_functions()[serial];
375 // Canonicalize argument order according to the symmetry properties
376 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
378 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
379 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
380 if (sig != std::numeric_limits<int>::max()) {
381 // Something has changed while sorting arguments, more evaluations later
384 return ex(sig) * thiscontainer(v);
388 if (opt.eval_f==nullptr) {
392 bool use_remember = opt.use_remember;
394 if (use_remember && lookup_remember_table(eval_result)) {
397 current_serial = serial;
398 if (opt.eval_use_exvector_args)
399 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
401 switch (opt.nparams) {
402 // the following lines have been generated for max. @maxargs@ parameters
403 +++ for N in range(1, maxargs + 1):
405 eval_result = ((eval_funcp_@N@)(opt.eval_f))(@seq('seq[%(n)d]', N, 0)@);
408 // end of generated lines
410 throw(std::logic_error("function::eval(): invalid nparams"));
413 store_remember_table(eval_result);
418 ex function::evalf(int level) const
420 GINAC_ASSERT(serial<registered_functions().size());
421 const function_options &opt = registered_functions()[serial];
423 // Evaluate children first
425 if (level == 1 || !(opt.evalf_params_first))
427 else if (level == -max_recursion_level)
428 throw(std::runtime_error("max recursion level reached"));
430 eseq.reserve(seq.size());
432 for (auto & it : seq) {
433 eseq.push_back(it.evalf(level));
437 if (opt.evalf_f==0) {
438 return function(serial,eseq).hold();
440 current_serial = serial;
441 if (opt.evalf_use_exvector_args)
442 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
443 switch (opt.nparams) {
444 // the following lines have been generated for max. @maxargs@ parameters
445 +++ for N in range(1, maxargs + 1):
447 return ((evalf_funcp_@N@)(opt.evalf_f))(@seq('eseq[%(n)d]', N, 0)@);
449 // end of generated lines
451 throw(std::logic_error("function::evalf(): invalid nparams"));
455 * This method is defined to be in line with behaviour of function::return_type()
457 ex function::eval_ncmul(const exvector & v) const
459 // If this function is called then the list of arguments is non-empty
460 // and the first argument is non-commutative, see function::return_type()
461 return seq.begin()->eval_ncmul(v);
464 unsigned function::calchash() const
466 unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial);
467 for (size_t i=0; i<nops(); i++) {
469 v ^= this->op(i).gethash();
472 if (flags & status_flags::evaluated) {
473 setflag(status_flags::hash_calculated);
479 ex function::thiscontainer(const exvector & v) const
481 return function(serial, v);
484 ex function::thiscontainer(exvector && v) const
486 return function(serial, std::move(v));
489 /** Implementation of ex::series for functions.
490 * \@see ex::series */
491 ex function::series(const relational & r, int order, unsigned options) const
493 GINAC_ASSERT(serial<registered_functions().size());
494 const function_options &opt = registered_functions()[serial];
496 if (opt.series_f==0) {
497 return basic::series(r, order);
500 current_serial = serial;
501 if (opt.series_use_exvector_args) {
503 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
504 } catch (do_taylor) {
505 res = basic::series(r, order, options);
509 switch (opt.nparams) {
510 // the following lines have been generated for max. @maxargs@ parameters
511 +++ for N in range(1, maxargs + 1):
514 res = ((series_funcp_@N@)(opt.series_f))(@seq('seq[%(n)d]', N, 0)@, r, order, options);
515 } catch (do_taylor) {
516 res = basic::series(r, order, options);
520 // end of generated lines
522 throw(std::logic_error("function::series(): invalid nparams"));
525 /** Implementation of ex::conjugate for functions. */
526 ex function::conjugate() const
528 GINAC_ASSERT(serial<registered_functions().size());
529 const function_options & opt = registered_functions()[serial];
531 if (opt.conjugate_f==0) {
532 return conjugate_function(*this).hold();
535 if (opt.conjugate_use_exvector_args) {
536 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
539 switch (opt.nparams) {
540 // the following lines have been generated for max. @maxargs@ parameters
541 +++ for N in range(1, maxargs + 1):
543 return ((conjugate_funcp_@N@)(opt.conjugate_f))(@seq('seq[%(n)d]', N, 0)@);
545 // end of generated lines
547 throw(std::logic_error("function::conjugate(): invalid nparams"));
550 /** Implementation of ex::real_part for functions. */
551 ex function::real_part() const
553 GINAC_ASSERT(serial<registered_functions().size());
554 const function_options & opt = registered_functions()[serial];
556 if (opt.real_part_f==0)
557 return basic::real_part();
559 if (opt.real_part_use_exvector_args)
560 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
562 switch (opt.nparams) {
563 // the following lines have been generated for max. @maxargs@ parameters
564 +++ for N in range(1, maxargs + 1):
566 return ((real_part_funcp_@N@)(opt.real_part_f))(@seq('seq[%(n)d]', N, 0)@);
568 // end of generated lines
570 throw(std::logic_error("function::real_part(): invalid nparams"));
573 /** Implementation of ex::imag_part for functions. */
574 ex function::imag_part() const
576 GINAC_ASSERT(serial<registered_functions().size());
577 const function_options & opt = registered_functions()[serial];
579 if (opt.imag_part_f==0)
580 return basic::imag_part();
582 if (opt.imag_part_use_exvector_args)
583 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
585 switch (opt.nparams) {
586 // the following lines have been generated for max. @maxargs@ parameters
587 +++ for N in range(1, maxargs + 1):
589 return ((imag_part_funcp_@N@)(opt.imag_part_f))(@seq('seq[%(n)d]', N, 0)@);
591 // end of generated lines
593 throw(std::logic_error("function::imag_part(): invalid nparams"));
596 /** Implementation of ex::info for functions. */
597 bool function::info(unsigned inf) const
599 GINAC_ASSERT(serial<registered_functions().size());
600 const function_options & opt = registered_functions()[serial];
603 return basic::info(inf);
606 if (opt.info_use_exvector_args) {
607 return ((info_funcp_exvector)(opt.info_f))(seq, inf);
610 switch (opt.nparams) {
611 // the following lines have been generated for max. @maxargs@ parameters
612 +++ for N in range(1, maxargs + 1):
614 return ((info_funcp_@N@)(opt.info_f))(@seq('seq[%(n)d]', N, 0)@, inf);
616 // end of generated lines
618 throw(std::logic_error("function::info(): invalid nparams"));
623 /** Implementation of ex::diff() for functions. It applies the chain rule,
624 * except for the Order term function.
626 ex function::derivative(const symbol & s) const
631 // Explicit derivation
632 result = expl_derivative(s);
636 size_t num = seq.size();
637 for (size_t i=0; i<num; i++) {
638 arg_diff = seq[i].diff(s);
639 // We apply the chain rule only when it makes sense. This is not
640 // just for performance reasons but also to allow functions to
641 // throw when differentiated with respect to one of its arguments
642 // without running into trouble with our automatic full
644 if (!arg_diff.is_zero())
645 result += pderivative(i)*arg_diff;
651 int function::compare_same_type(const basic & other) const
653 GINAC_ASSERT(is_a<function>(other));
654 const function & o = static_cast<const function &>(other);
656 if (serial != o.serial)
657 return serial < o.serial ? -1 : 1;
659 return exprseq::compare_same_type(o);
662 bool function::is_equal_same_type(const basic & other) const
664 GINAC_ASSERT(is_a<function>(other));
665 const function & o = static_cast<const function &>(other);
667 if (serial != o.serial)
670 return exprseq::is_equal_same_type(o);
673 bool function::match_same_type(const basic & other) const
675 GINAC_ASSERT(is_a<function>(other));
676 const function & o = static_cast<const function &>(other);
678 return serial == o.serial;
681 unsigned function::return_type() const
683 GINAC_ASSERT(serial<registered_functions().size());
684 const function_options &opt = registered_functions()[serial];
686 if (opt.use_return_type) {
687 // Return type was explicitly specified
688 return opt.return_type;
690 // Default behavior is to use the return type of the first
691 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
693 return return_types::commutative;
695 return seq.begin()->return_type();
699 return_type_t function::return_type_tinfo() const
701 GINAC_ASSERT(serial<registered_functions().size());
702 const function_options &opt = registered_functions()[serial];
704 if (opt.use_return_type) {
705 // Return type was explicitly specified
706 return opt.return_type_tinfo;
708 // Default behavior is to use the return type of the first
709 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
711 return make_return_type_t<function>();
713 return seq.begin()->return_type_tinfo();
718 // new virtual functions which can be overridden by derived classes
724 // non-virtual functions in this class
729 ex function::pderivative(unsigned diff_param) const // partial differentiation
731 GINAC_ASSERT(serial<registered_functions().size());
732 const function_options &opt = registered_functions()[serial];
734 if (opt.derivative_f) {
735 // Invoke the defined derivative function.
736 current_serial = serial;
737 if (opt.derivative_use_exvector_args)
738 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
739 switch (opt.nparams) {
740 // the following lines have been generated for max. @maxargs@ parameters
741 +++ for N in range(1, maxargs + 1):
743 return ((derivative_funcp_@N@)(opt.derivative_f))(@seq('seq[%(n)d]', N, 0)@, diff_param);
745 // end of generated lines
748 // No derivative defined? Fall back to abstract derivative object.
749 return fderivative(serial, diff_param, seq);
752 ex function::expl_derivative(const symbol & s) const // explicit differentiation
754 GINAC_ASSERT(serial<registered_functions().size());
755 const function_options &opt = registered_functions()[serial];
757 if (opt.expl_derivative_f) {
758 // Invoke the defined explicit derivative function.
759 current_serial = serial;
760 if (opt.expl_derivative_use_exvector_args)
761 return ((expl_derivative_funcp_exvector)(opt.expl_derivative_f))(seq, s);
762 switch (opt.nparams) {
763 // the following lines have been generated for max. @maxargs@ parameters
764 +++ for N in range(1, maxargs + 1):
766 return ((expl_derivative_funcp_@N@)(opt.expl_derivative_f))(@seq('seq[%(n)d]', N, 0)@, s);
768 // end of generated lines
771 // There is no fallback for explicit deriviative.
772 throw(std::logic_error("function::expl_derivative(): explicit derivation is called, but no such function defined"));
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];
781 // Invoke the defined power function.
782 current_serial = serial;
783 if (opt.power_use_exvector_args)
784 return ((power_funcp_exvector)(opt.power_f))(seq, power_param);
785 switch (opt.nparams) {
786 // the following lines have been generated for max. @maxargs@ parameters
787 +++ for N in range(1, maxargs + 1):
789 return ((power_funcp_@N@)(opt.power_f))(@seq('seq[%(n)d]', N, 0)@, power_param);
791 // end of generated lines
794 // No power function defined? Fall back to returning a power object.
795 return (new GiNaC::power(*this, power_param))->setflag(status_flags::dynallocated |
796 status_flags::evaluated);
799 ex function::expand(unsigned options) const
801 GINAC_ASSERT(serial<registered_functions().size());
802 const function_options &opt = registered_functions()[serial];
805 // Invoke the defined expand function.
806 current_serial = serial;
807 if (opt.expand_use_exvector_args)
808 return ((expand_funcp_exvector)(opt.expand_f))(seq, options);
809 switch (opt.nparams) {
810 // the following lines have been generated for max. @maxargs@ parameters
811 +++ for N in range(1, maxargs + 1):
813 return ((expand_funcp_@N@)(opt.expand_f))(@seq('seq[%(n)d]', N, 0)@, options);
815 // end of generated lines
818 // No expand function defined? Return the same function with expanded arguments (if required)
819 if (options & expand_options::expand_function_args)
820 return inherited::expand(options);
822 return (options == 0) ? setflag(status_flags::expanded) : *this;
825 std::vector<function_options> & function::registered_functions()
827 static std::vector<function_options> rf = std::vector<function_options>();
831 bool function::lookup_remember_table(ex & result) const
833 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
836 void function::store_remember_table(ex const & result) const
838 remember_table::remember_tables()[this->serial].add_entry(*this,result);
843 unsigned function::register_new(function_options const & opt)
845 size_t same_name = 0;
846 for (size_t i=0; i<registered_functions().size(); ++i) {
847 if (registered_functions()[i].name==opt.name) {
851 if (same_name>=opt.functions_with_same_name) {
852 // we do not throw an exception here because this code is
853 // usually executed before main(), so the exception could not
855 std::cerr << "WARNING: function name " << opt.name
856 << " already in use!" << std::endl;
858 registered_functions().push_back(opt);
859 if (opt.use_remember) {
860 remember_table::remember_tables().
861 push_back(remember_table(opt.remember_size,
862 opt.remember_assoc_size,
863 opt.remember_strategy));
865 remember_table::remember_tables().push_back(remember_table());
867 return registered_functions().size()-1;
870 /** Find serial number of function by name and number of parameters.
871 * Throws exception if function was not found. */
872 unsigned function::find_function(const std::string &name, unsigned nparams)
875 for (auto & it : function::registered_functions()) {
876 if (it.get_name() == name && it.get_nparams() == nparams)
880 throw (std::runtime_error("no function '" + name + "' with " + std::to_string(nparams) + " parameters defined"));
883 /** Return the print name of the function. */
884 std::string function::get_name() const
886 GINAC_ASSERT(serial<registered_functions().size());
887 return registered_functions()[serial].name;