math-extern: enable handling of simple sums

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3009 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-11-12 14:37:43 +00:00
parent 94aa4ea661
commit fb16a10b8e
3 changed files with 126 additions and 31 deletions

View File

@ -5,8 +5,8 @@
#include "math_symbolinset.h" #include "math_symbolinset.h"
MathExIntInset::MathExIntInset() MathExIntInset::MathExIntInset(string const & name)
: int_(new MathSymbolInset("int")) : symbol_(name)
{} {}
@ -16,9 +16,9 @@ MathInset * MathExIntInset::clone() const
} }
void MathExIntInset::differential(MathArray const & ar) void MathExIntInset::index(MathArray const & ar)
{ {
diff_ = ar; index_ = ar;
} }
@ -34,9 +34,15 @@ void MathExIntInset::scripts(MathAtom const & at)
} }
void MathExIntInset::symbol(MathAtom const & at) MathAtom & MathExIntInset::scripts()
{ {
int_ = at; return scripts_;
}
void MathExIntInset::symbol(string const & symbol)
{
symbol_ = symbol;
} }
@ -49,12 +55,12 @@ bool MathExIntInset::hasScripts() const
void MathExIntInset::normalize(NormalStream & os) const void MathExIntInset::normalize(NormalStream & os) const
{ {
os << "[int "; os << '[' << symbol_.c_str() << ' ';
if (hasScripts()) if (hasScripts())
os << scripts_.nucleus(); os << scripts_.nucleus();
else else
os << "{}"; os << "{}";
os << ' ' << core_ << ' ' << diff_ << ']'; os << ' ' << core_ << ' ' << index_ << ']';
} }
@ -72,12 +78,12 @@ void MathExIntInset::draw(Painter &, int, int) const
void MathExIntInset::maplize(MapleStream & os) const void MathExIntInset::maplize(MapleStream & os) const
{ {
os << int_.nucleus() << '('; os << symbol_.c_str() << '(';
if (core_.size()) if (core_.size())
os << core_; os << core_;
else else
os << '1'; os << '1';
os << ',' << diff_; os << ',' << index_;
if (hasScripts()) { if (hasScripts()) {
MathScriptInset * p = scripts_->asScriptInset(); MathScriptInset * p = scripts_->asScriptInset();
os << '=' << p->down().data_ << ".." << p->up().data_; os << '=' << p->down().data_ << ".." << p->up().data_;
@ -88,20 +94,20 @@ void MathExIntInset::maplize(MapleStream & os) const
void MathExIntInset::mathmlize(MathMLStream & os) const void MathExIntInset::mathmlize(MathMLStream & os) const
{ {
MathSymbolInset * sym = new MathSymbolInset(symbol_.c_str());
if (hasScripts()) if (hasScripts())
scripts_->asScriptInset()->mathmlize(int_.nucleus(), os); scripts_->asScriptInset()->mathmlize(sym, os);
else else
int_->mathmlize(os); sym->mathmlize(os);
delete sym;
os << core_ << "<mo> &InvisibleTimes; </mo>" os << core_ << "<mo> &InvisibleTimes; </mo>"
<< MTag("mrow") << "<mo> &DifferentialD; </mo>" << MTag("mrow") << "<mo> &DifferentialD; </mo>"
<< diff_ << ETag("mrow"); << index_ << ETag("mrow");
} }
void MathExIntInset::write(WriteStream & os) const void MathExIntInset::write(WriteStream &) const
{ {
if (hasScripts()) lyxerr << "should not happen\n";
os << scripts_.nucleus();
os << core_ << "d" << diff_;
} }

View File

@ -10,19 +10,19 @@
class MathExIntInset : public MathInset { class MathExIntInset : public MathInset {
public: public:
/// ///
MathExIntInset(); explicit MathExIntInset(string const & name_);
/// ///
MathInset * clone() const; MathInset * clone() const;
/// ///
void differential(MathArray const &); void index(MathArray const &);
/// ///
void core(MathArray const &); void core(MathArray const &);
/// ///
void scripts(MathAtom const &); void scripts(MathAtom const &);
/// ///
void symbol(MathAtom const &); MathAtom & scripts();
/// ///
bool hasScripts() const; void symbol(string const &);
/// ///
void metrics(MathMetricsInfo const & st) const; void metrics(MathMetricsInfo const & st) const;
/// ///
@ -38,12 +38,16 @@ public:
void write(WriteStream & os) const; void write(WriteStream & os) const;
private: private:
/// ///
MathAtom int_; bool hasScripts() const;
///
string symbol_;
/// ///
MathAtom scripts_; MathAtom scripts_;
/// ///
MathArray core_; MathArray core_;
/// ///
MathArray diff_; MathArray index_;
}; };
#endif #endif

View File

@ -3,6 +3,7 @@
// information" from the unstructered layout-oriented stuff in an // information" from the unstructered layout-oriented stuff in an
// MathArray. // MathArray.
#include <algorithm>
#include "math_charinset.h" #include "math_charinset.h"
#include "math_deliminset.h" #include "math_deliminset.h"
@ -111,9 +112,10 @@ string extractString(MathInset * p)
// define a function for tests // define a function for tests
typedef bool TestItemFunc(MathInset *); typedef bool TestItemFunc(MathInset *);
// define a function for replacing pa // define a function for replacing subexpressions
typedef MathInset * ReplaceArgumentFunc(const MathArray & ar); typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
// search end of nested sequence // search end of nested sequence
MathArray::iterator endNestSearch( MathArray::iterator endNestSearch(
MathArray::iterator it, MathArray::iterator it,
@ -293,7 +295,7 @@ void extractIntegrals(MathArray & ar)
continue; continue;
// create a proper inset as replacement // create a proper inset as replacement
MathExIntInset * p = new MathExIntInset; MathExIntInset * p = new MathExIntInset("int");
// collect scripts // collect scripts
MathArray::iterator st = it + 1; MathArray::iterator st = it + 1;
@ -305,26 +307,109 @@ void extractIntegrals(MathArray & ar)
} }
// use the atom behind the 'd' as differential // use the atom behind the 'd' as differential
MathArray diff; MathArray ind;
if (jt + 1 != ar.end()) { if (jt + 1 != ar.end()) {
diff.push_back(*(jt + 1)); ind.push_back(*(jt + 1));
ar.erase(it + 1, jt + 2); ++jt;
} else {
ar.erase(it + 1, jt + 1);
} }
p->differential(diff); ar.erase(it + 1, jt + 1);
p->index(ind);
(*it).reset(p); (*it).reset(p);
} }
lyxerr << "\nIntegrals to: " << ar << "\n"; lyxerr << "\nIntegrals to: " << ar << "\n";
} }
//
// search sums
//
bool sumSymbolTest(MathInset * p)
{
return p->asSymbolInset() && p->asSymbolInset()->name() == "sum";
}
bool equalSign(MathInset * p)
{
return extractString(p) == "=";
}
bool equalSign1(MathAtom const & at)
{
return equalSign(at.nucleus());
}
// replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
// assume 'extractDelims' ran before
void extractSums(MathArray & ar)
{
// we need at least two items...
if (ar.size() <= 1)
return;
lyxerr << "\nSums from: " << ar << "\n";
for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
MathArray::iterator it = ar.begin() + i;
// is this a sum name?
if (!sumSymbolTest(it->nucleus()))
continue;
// create a proper inset as replacement
MathExIntInset * p = new MathExIntInset("sum");
// collect scripts
MathArray::iterator st = it + 1;
if (st != ar.end() && (*st)->asScriptInset()) {
p->scripts(*st);
++st;
// try to figure out the summation index from the subscript
MathScriptInset * script = p->scripts()->asScriptInset();
if (script->hasDown()) {
MathArray & ar = script->down().data_;
MathArray::iterator it =
std::find_if(ar.begin(), ar.end(), &equalSign1);
if (it != ar.end()) {
// we found a '=', use everything in front of that as index,
// and everything behind as start value
p->index(MathArray(ar.begin(), it));
ar.erase(ar.begin(), it + 1);
} else {
// use everything as summation index
p->index(ar);
p->scripts().reset(0);
}
}
}
// use the atom behind the script as core
MathArray ind;
if (st != ar.end()) {
MathArray core;
core.push_back(*st);
p->core(core);
++st;
}
ar.erase(it + 1, st);
(*it).reset(p);
}
lyxerr << "\nSums to: " << ar << "\n";
}
void extractStructure(MathArray & ar) void extractStructure(MathArray & ar)
{ {
extractMatrices(ar); extractMatrices(ar);
extractDelims(ar); extractDelims(ar);
extractFunctions(ar); extractFunctions(ar);
extractIntegrals(ar); extractIntegrals(ar);
extractSums(ar);
extractStrings(ar); extractStrings(ar);
} }