/** @file function.cpp * * Implementation of class of symbolic functions. */ /* * This file was generated automatically by function.py. * Please do not modify it directly, edit function.cppy instead! * function.py options: maxargs=@maxargs@ * * GiNaC Copyright (C) 1999-2017 Johannes Gutenberg University Mainz, Germany * * 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 2 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "function.h" #include "operators.h" #include "fderivative.h" #include "ex.h" #include "lst.h" #include "symmetry.h" #include "print.h" #include "power.h" #include "archive.h" #include "inifcns.h" #include "utils.h" #include "hash_seed.h" #include "remember.h" #include #include #include #include #include namespace GiNaC { ////////// // helper class function_options ////////// function_options::function_options() { initialize(); } function_options::function_options(std::string const & n, std::string const & tn) { initialize(); set_name(n, tn); } function_options::function_options(std::string const & n, unsigned np) { initialize(); set_name(n, std::string()); nparams = np; } function_options::~function_options() { // nothing to clean up at the moment } void function_options::initialize() { set_name("unnamed_function", "\\mbox{unnamed}"); nparams = 0; eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = expand_f = derivative_f = expl_derivative_f = power_f = series_f = nullptr; info_f = nullptr; evalf_params_first = true; use_return_type = false; eval_use_exvector_args = false; evalf_use_exvector_args = false; conjugate_use_exvector_args = false; real_part_use_exvector_args = false; imag_part_use_exvector_args = false; expand_use_exvector_args = false; derivative_use_exvector_args = false; expl_derivative_use_exvector_args = false; power_use_exvector_args = false; series_use_exvector_args = false; print_use_exvector_args = false; info_use_exvector_args = false; use_remember = false; functions_with_same_name = 1; symtree = 0; } function_options & function_options::set_name(std::string const & n, std::string const & tn) { name = n; if (tn==std::string()) TeX_name = "\\mbox{"+name+"}"; else TeX_name = tn; return *this; } function_options & function_options::latex_name(std::string const & tn) { TeX_name = tn; return *this; } // the following lines have been generated for max. @maxargs@ parameters +++ for method, N in [ (f, N) for f in methods[0:-1] for N in range(1, maxargs + 1)]: function_options & function_options::@method@_func(@method@_funcp_@N@ e) { test_and_set_nparams(@N@); @method@_f = @method@_funcp(e); return *this; } --- // end of generated lines +++ for method in methods[0:-1]: function_options & function_options::@method@_func(@method@_funcp_exvector e) { @method@_use_exvector_args = true; @method@_f = @method@_funcp(e); return *this; } --- // end of generated lines function_options & function_options::set_return_type(unsigned rt, const return_type_t* rtt) { use_return_type = true; return_type = rt; if (rtt != nullptr) return_type_tinfo = *rtt; else return_type_tinfo = make_return_type_t(); return *this; } function_options & function_options::do_not_evalf_params() { evalf_params_first = false; return *this; } function_options & function_options::remember(unsigned size, unsigned assoc_size, unsigned strategy) { use_remember = true; remember_size = size; remember_assoc_size = assoc_size; remember_strategy = strategy; return *this; } function_options & function_options::overloaded(unsigned o) { functions_with_same_name = o; return *this; } function_options & function_options::set_symmetry(const symmetry & s) { symtree = s; return *this; } void function_options::test_and_set_nparams(unsigned n) { 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 // be caught anyhow std::cerr << "WARNING: " << name << "(): number of parameters (" << n << ") differs from number set before (" << nparams << ")" << std::endl; } } void function_options::set_print_func(unsigned id, print_funcp f) { if (id >= print_dispatch_table.size()) print_dispatch_table.resize(id + 1); print_dispatch_table[id] = f; } /** This can be used as a hook for external applications. */ unsigned function::current_serial = 0; GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq) ////////// // default constructor ////////// // public function::function() : serial(0) { } ////////// // other constructors ////////// // public function::function(unsigned ser) : serial(ser) { } // the following lines have been generated for max. @maxargs@ parameters +++ for N in range(1, maxargs + 1): function::function(unsigned ser, @seq('const ex & param%(n)d', N)@) : exprseq{@seq('param%(n)d', N)@}, serial(ser) { } --- function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser) { // Force re-evaluation even if the exprseq was already evaluated // (the exprseq copy constructor copies the flags) clearflag(status_flags::evaluated); } function::function(unsigned ser, const exvector & v) : exprseq(v), serial(ser) { } function::function(unsigned ser, exvector && v) : exprseq(std::move(v)), serial(ser) { } ////////// // archiving ////////// /** Construct object from archive_node. */ void function::read_archive(const archive_node& n, lst& sym_lst) { inherited::read_archive(n, sym_lst); // Find serial number by function name std::string s; if (n.find_string("name", s)) { unsigned int ser = 0; for (auto & it : registered_functions()) { if (s == it.name) { serial = ser; return; } ++ser; } throw (std::runtime_error("unknown function '" + s + "' in archive")); } else throw (std::runtime_error("unnamed function in archive")); } /** Archive the object. */ void function::archive(archive_node &n) const { inherited::archive(n); GINAC_ASSERT(serial < registered_functions().size()); n.add_string("name", registered_functions()[serial].name); } GINAC_BIND_UNARCHIVER(function); ////////// // functions overriding virtual functions from base classes ////////// // public void function::print(const print_context & c, unsigned level) const { GINAC_ASSERT(serial &pdt = opt.print_dispatch_table; // Dynamically dispatch on print_context type const print_context_class_info *pc_info = &c.get_class_info(); next_context: unsigned id = pc_info->options.get_id(); if (id >= pdt.size() || pdt[id] == nullptr) { // Method not found, try parent print_context class const print_context_class_info *parent_pc_info = pc_info->get_parent(); if (parent_pc_info) { pc_info = parent_pc_info; goto next_context; } // Method still not found, use default output if (is_a(c)) { c.s << std::string(level, ' ') << class_name() << " " << opt.name << " @" << this << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec << ", nops=" << nops() << std::endl; unsigned delta_indent = static_cast(c).delta_indent; for (size_t i=0; i(c)) { // Print function name in lowercase std::string lname = opt.name; size_t num = lname.size(); for (size_t i=0; i(c)) { c.s << opt.TeX_name; printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence()); } else { c.s << opt.name; printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence()); } } else { // Method found, call it current_serial = serial; if (opt.print_use_exvector_args) ((print_funcp_exvector)pdt[id])(seq, c); else switch (opt.nparams) { // the following lines have been generated for max. @maxargs@ parameters +++ for N in range(1, maxargs + 1): case @N@: ((print_funcp_@N@)(pdt[id]))(@seq('seq[%(n)d]', N, 0)@, c); break; --- // end of generated lines default: throw(std::logic_error("function::print(): invalid nparams")); } } } ex function::eval() const { if (flags & status_flags::evaluated) { return *this; } GINAC_ASSERT(serial 1 && !(opt.symtree.is_zero())) { exvector v = seq; GINAC_ASSERT(is_a(opt.symtree)); int sig = canonicalize(v.begin(), ex_to(opt.symtree)); if (sig != std::numeric_limits::max()) { // Something has changed while sorting arguments, more evaluations later if (sig == 0) return _ex0; return ex(sig) * thiscontainer(std::move(v)); } } if (opt.eval_f==nullptr) { return this->hold(); } bool use_remember = opt.use_remember; ex eval_result; if (use_remember && lookup_remember_table(eval_result)) { return eval_result; } current_serial = serial; if (opt.eval_use_exvector_args) eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq); else switch (opt.nparams) { // the following lines have been generated for max. @maxargs@ parameters +++ for N in range(1, maxargs + 1): case @N@: eval_result = ((eval_funcp_@N@)(opt.eval_f))(@seq('seq[%(n)d]', N, 0)@); break; --- // end of generated lines default: throw(std::logic_error("function::eval(): invalid nparams")); } if (use_remember) { store_remember_table(eval_result); } return eval_result; } ex function::evalf() const { GINAC_ASSERT(serialeval_ncmul(v); } unsigned function::calchash() const { unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial); for (size_t i=0; iop(i).gethash(); } if (flags & status_flags::evaluated) { setflag(status_flags::hash_calculated); hashvalue = v; } return v; } ex function::thiscontainer(const exvector & v) const { return function(serial, v); } ex function::thiscontainer(exvector && v) const { return function(serial, std::move(v)); } /** Implementation of ex::series for functions. * \@see ex::series */ ex function::series(const relational & r, int order, unsigned options) const { GINAC_ASSERT(serial(other)); const function & o = static_cast(other); if (serial != o.serial) return serial < o.serial ? -1 : 1; else return exprseq::compare_same_type(o); } bool function::is_equal_same_type(const basic & other) const { GINAC_ASSERT(is_a(other)); const function & o = static_cast(other); if (serial != o.serial) return false; else return exprseq::is_equal_same_type(o); } bool function::match_same_type(const basic & other) const { GINAC_ASSERT(is_a(other)); const function & o = static_cast(other); return serial == o.serial; } unsigned function::return_type() const { GINAC_ASSERT(serialreturn_type(); } } return_type_t function::return_type_tinfo() const { GINAC_ASSERT(serial(); else return seq.begin()->return_type_tinfo(); } } ////////// // new virtual functions which can be overridden by derived classes ////////// // none ////////// // non-virtual functions in this class ////////// // protected ex function::pderivative(unsigned diff_param) const // partial differentiation { GINAC_ASSERT(serial(*this, power_param).setflag(status_flags::evaluated); } ex function::expand(unsigned options) const { GINAC_ASSERT(serial & function::registered_functions() { static std::vector rf = std::vector(); return rf; } bool function::lookup_remember_table(ex & result) const { return remember_table::remember_tables()[this->serial].lookup_entry(*this,result); } void function::store_remember_table(ex const & result) const { remember_table::remember_tables()[this->serial].add_entry(*this,result); } // public unsigned function::register_new(function_options const & opt) { size_t same_name = 0; for (auto & i : registered_functions()) { if (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) { unsigned serial = 0; for (auto & it : function::registered_functions()) { if (it.get_name() == name && it.get_nparams() == nparams) return serial; ++serial; } throw (std::runtime_error("no function '" + name + "' with " + std::to_string(nparams) + " parameters defined")); } /** Return the print name of the function. */ std::string function::get_name() const { GINAC_ASSERT(serial