/** @file ncsymbol.cpp
*
* Implementation of GiNaC's symbolic objects - non-commutative. */
/*
* GiNaC Copyright (C) 2011 Jan Rheinländer
*
* 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
*/
// Copied from symbol.cpp and modified a bit
#include "ncsymbol.h"
#include "ginac/lst.h"
#include "ginac/archive.h"
//#include "ginac/tostring.h"
//#include "ginac/utils.h"
//#include "ginac/hash_seed.h"
#include "ginac/inifcns.h"
#include "ginac/expairseq.h"
#include "ginac/relational.h"
#include "ginac/pseries.h"
#include <map>
#include <stdexcept>
#include <string>
namespace GiNaC {
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(ncsymbol, basic,
print_func<print_context>(&ncsymbol::do_print).
print_func<print_latex>(&ncsymbol::do_print_latex).
print_func<print_tree>(&ncsymbol::do_print_tree).
print_func<print_python_repr>(&ncsymbol::do_print_python_repr))
//////////
// default constructor
//////////
ncsymbol::ncsymbol() : serial(next_serial++), name(""), TeX_name("")
{
setflag(status_flags::evaluated | status_flags::expanded);
}
//////////
// other constructors
//////////
// public
ncsymbol::ncsymbol(const std::string & initname) : serial(next_serial++),
name(initname), TeX_name("")
{
setflag(status_flags::evaluated | status_flags::expanded);
}
ncsymbol::ncsymbol(const std::string & initname, const std::string & texname) :
serial(next_serial++), name(initname), TeX_name(texname)
{
setflag(status_flags::evaluated | status_flags::expanded);
}
//////////
// archiving
//////////
/** Read object from archive_node. */
void ncsymbol::read_archive(const archive_node &n, lst &sym_lst)
{
inherited::read_archive(n, sym_lst);
serial = next_serial++;
std::string tmp_name;
n.find_string("name", tmp_name);
// If ncsymbol is in sym_lst, return the existing ncsymbol
for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
if (is_a<ncsymbol>(*it) && (ex_to<ncsymbol>(*it).name == tmp_name)) {
*this = ex_to<ncsymbol>(*it);
return;
}
}
name = tmp_name;
if (!n.find_string("TeXname", TeX_name))
TeX_name = std::string("");
setflag(status_flags::evaluated | status_flags::expanded);
setflag(status_flags::dynallocated);
sym_lst.append(*this);
}
/** Archive the object. */
void ncsymbol::archive(archive_node &n) const
{
inherited::archive(n);
// XXX: we should not archive anonymous ncsymbols.
if (!name.empty())
n.add_string("name", name);
if (!TeX_name.empty())
n.add_string("TeX_name", TeX_name);
}
//////////
// functions overriding virtual functions from base classes
//////////
/** Return default TeX name for ncsymbol. This recognizes some greek letters. */
static const std::string& get_default_TeX_name(const std::string& name);
// public
void ncsymbol::do_print(const print_context & c, unsigned level) const
{
if (!name.empty())
c.s << name;
else
c.s << "ncsymbol" << serial;
}
void ncsymbol::do_print_latex(const print_latex & c, unsigned level) const
{
if (!TeX_name.empty())
c.s << TeX_name;
else if (!name.empty())
c.s << get_default_TeX_name(name);
else
c.s << "ncsymbol" << serial;
}
void ncsymbol::do_print_tree(const print_tree & c, unsigned level) const
{
c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
<< ", serial=" << serial
<< std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
<< ", domain=" << get_domain() << ", non-commutative composite"
<< std::endl;
}
void ncsymbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
{
c.s << class_name() << "('";
if (!name.empty())
c.s << name;
else
c.s << "ncsymbol" << serial;
if (!TeX_name.empty())
c.s << "','" << TeX_name;
c.s << "')";
}
bool ncsymbol::info(unsigned inf) const
{
switch (inf) {
//case info_flags::symbol:
case info_flags::polynomial:
case info_flags::integer_polynomial:
case info_flags::cinteger_polynomial:
case info_flags::rational_polynomial:
case info_flags::crational_polynomial:
case info_flags::rational_function:
case info_flags::expanded:
return true;
case info_flags::real:
return get_domain() == domain::real || get_domain() == domain::positive;
case info_flags::positive:
case info_flags::nonnegative:
return get_domain() == domain::positive;
case info_flags::has_indices:
return false;
}
return inherited::info(inf);
}
// Copied from pseries.cpp
ex ncsymbol::series(const relational & r, int order, unsigned options) const
{
epvector seq;
const ex point = r.rhs();
GINAC_ASSERT(is_a<ncsymbol>(r.lhs()));
if (this->is_equal_same_type(ex_to<ncsymbol>(r.lhs()))) {
if (order > 0 && !point.is_zero())
seq.push_back(expair(point, ex(numeric(0))));
if (order > 1)
seq.push_back(expair(ex(numeric(1)), ex(numeric(1))));
else
seq.push_back(expair(Order(ex(numeric(1))), numeric(order)));
} else
seq.push_back(expair(*this, ex(numeric(0))));
return pseries(r, seq);
}
// Copied from normal.cpp
ex ncsymbol::normal(exmap & repl, exmap & rev_lookup, int level) const
{
return (new lst(*this, ex(numeric(1))))->setflag(status_flags::dynallocated);
}
// Copied from normal.cpp
ex ncsymbol::to_polynomial(exmap & repl) const
{
return *this;
}
// Copied from normal.cpp
ex ncsymbol::to_rational(exmap & repl) const
{
return *this;
}
ex ncsymbol::conjugate() const
{
return conjugate_function(*this).hold();
}
ex ncsymbol::real_part() const
{
return real_part_function(*this).hold();
}
ex ncsymbol::imag_part() const
{
return imag_part_function(*this).hold();
}
bool ncsymbol::is_polynomial(const ex & var) const
{
return true;
}
// protected
/** Implementation of ex::diff() for single differentiation of a ncsymbol.
* It returns 1 or 0.
*
* @see ex::diff */
/*ex ncsymbol::derivative(const symbol & s) const
{
if (compare_same_type(s))
return _ex0;
else
return _ex1;
}*/
int ncsymbol::compare_same_type(const basic & other) const
{
GINAC_ASSERT(is_a<ncsymbol>(other));
const ncsymbol *o = static_cast<const ncsymbol *>(&other);
if (serial==o->serial) return 0;
return serial < o->serial ? -1 : 1;
}
bool ncsymbol::is_equal_same_type(const basic & other) const
{
GINAC_ASSERT(is_a<ncsymbol>(other));
const ncsymbol *o = static_cast<const ncsymbol *>(&other);
return serial==o->serial;
}
/*unsigned ncsymbol::calchash() const
{
unsigned seed = make_hash_seed(typeid(*this));
hashvalue = golden_ratio_hash(seed ^ serial);
setflag(status_flags::hash_calculated);
return hashvalue;
}*/
//////////
// virtual functions which can be overridden by derived classes
//////////
// none
//////////
// non-virtual functions in this class
//////////
/** Return default TeX name for ncsymbol. This recognizes some greek letters. */
static const std::string& get_default_TeX_name(const std::string& name)
{
static std::map<std::string, std::string> standard_names;
static bool names_initialized = false;
if (!names_initialized) {
standard_names["alpha"] = std::string("\\alpha");
standard_names["beta"] = std::string("\\beta");;
standard_names["gamma"] = std::string("\\gamma");;
standard_names["delta"] = std::string("\\delta");;
standard_names["epsilon"] = std::string("\\epsilon");
standard_names["varepsilon"] = std::string("\\varepsilon");
standard_names["zeta"] = std::string("\\zeta");
standard_names["eta" ] = std::string("\\eta" );
standard_names["theta"] = std::string("\\theta");
standard_names["vartheta"] = std::string("\\vartheta");
standard_names["iota"] = std::string("\\iota");
standard_names["kappa"] = std::string("\\kappa");
standard_names["lambda"] = std::string("\\lambda");
standard_names["mu"] = std::string("\\mu");
standard_names["nu"] = std::string("\\nu");
standard_names["xi"] = std::string("\\xi");
standard_names["omicron"] = std::string("\\omicron");
standard_names["pi"] = std::string("\\pi");
standard_names["varpi"] = std::string("\\varpi");
standard_names["rho"] = std::string("\\rho");
standard_names["varrho"] = std::string("\\varrho");
standard_names["sigma"] = std::string("\\sigma");
standard_names["varsigma"] = std::string("\\varsigma");
standard_names["tau"] = std::string("\\tau");
standard_names["upsilon"] = std::string("\\upsilon");
standard_names["phi"] = std::string("\\phi");
standard_names["varphi"] = std::string("\\varphi");
standard_names["chi"] = std::string("\\chi");
standard_names["psi"] = std::string("\\psi");
standard_names["omega"] = std::string("\\omega");
standard_names["Gamma"] = std::string("\\Gamma");
standard_names["Delta"] = std::string("\\Delta");
standard_names["Theta"] = std::string("\\Theta");
standard_names["Lambda"] = std::string("\\Lambda");
standard_names["Xi"] = std::string("\\Xi");
standard_names["Pi"] = std::string("\\Pi");
standard_names["Sigma"] = std::string("\\Sigma");
standard_names["Upsilon"] = std::string("\\Upsilon");
standard_names["Phi"] = std::string("\\Phi");
standard_names["Psi"] = std::string("\\Psi");
standard_names["Omega"] = std::string("\\Omega");
names_initialized = true;
}
std::map<std::string, std::string>::const_iterator it = standard_names.find(name);
if (it != standard_names.end())
return it->second;
else
return name;
}
GINAC_BIND_UNARCHIVER(ncsymbol);
//////////
// static member variables
//////////
// private
unsigned ncsymbol::next_serial = 0;
} // namespace GiNaC