2002-09-11 09:14:57 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// This file contains most of the magic that extracts "context
|
|
|
|
// information" from the unstructered layout-oriented stuff in an
|
|
|
|
// MathArray.
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
#include <config.h>
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2002-04-24 17:07:42 +00:00
|
|
|
#include "math_amsarrayinset.h"
|
2002-06-18 15:44:30 +00:00
|
|
|
#include "math_arrayinset.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
#include "math_charinset.h"
|
|
|
|
#include "math_deliminset.h"
|
2001-11-13 18:33:48 +00:00
|
|
|
#include "math_diffinset.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
#include "math_exfuncinset.h"
|
2001-11-09 14:48:57 +00:00
|
|
|
#include "math_exintinset.h"
|
2001-11-13 18:33:48 +00:00
|
|
|
#include "math_fracinset.h"
|
2002-07-12 11:21:21 +00:00
|
|
|
#include "math_liminset.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
#include "math_matrixinset.h"
|
|
|
|
#include "math_mathmlstream.h"
|
2002-06-18 15:44:30 +00:00
|
|
|
#include "math_numberinset.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
#include "math_scriptinset.h"
|
|
|
|
#include "math_stringinset.h"
|
2001-11-09 14:23:44 +00:00
|
|
|
#include "math_symbolinset.h"
|
2002-05-30 07:09:54 +00:00
|
|
|
#include "math_unknowninset.h"
|
2002-06-18 15:44:30 +00:00
|
|
|
#include "math_parser.h"
|
2001-11-15 14:14:37 +00:00
|
|
|
#include "Lsstream.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
#include "debug.h"
|
2002-06-18 15:44:30 +00:00
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/systemcall.h"
|
|
|
|
#include "support/filetools.h"
|
2002-10-28 17:15:19 +00:00
|
|
|
#include "support/lstrings.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
#include <algorithm>
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
using std::ostream;
|
|
|
|
using std::istringstream;
|
|
|
|
using std::find_if;
|
2002-06-18 15:44:30 +00:00
|
|
|
using std::endl;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
ostream & operator<<(ostream & os, MathArray const & ar)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2002-03-21 17:42:56 +00:00
|
|
|
NormalStream ns(os);
|
2001-11-09 12:01:10 +00:00
|
|
|
ns << ar;
|
2001-11-09 10:44:24 +00:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// define a function for tests
|
2002-08-08 16:44:06 +00:00
|
|
|
typedef bool TestItemFunc(MathAtom const &);
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// define a function for replacing subexpressions
|
2002-08-09 10:22:35 +00:00
|
|
|
typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:42:56 +00:00
|
|
|
// try to extract a super/subscript
|
2001-11-15 14:14:37 +00:00
|
|
|
// modify iterator position to point behind the thing
|
|
|
|
bool extractScript(MathArray & ar,
|
|
|
|
MathArray::iterator & pos, MathArray::iterator last)
|
|
|
|
{
|
|
|
|
// nothing to get here
|
|
|
|
if (pos == last)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// is this a scriptinset?
|
|
|
|
if (!(*pos)->asScriptInset())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// it is a scriptinset, use it.
|
|
|
|
ar.push_back(*pos);
|
|
|
|
++pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// try to extract an "argument" to some function.
|
|
|
|
// returns position behind the argument
|
|
|
|
MathArray::iterator extractArgument(MathArray & ar,
|
|
|
|
MathArray::iterator pos, MathArray::iterator last, string const & = "")
|
|
|
|
{
|
|
|
|
// nothing to get here
|
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
// something deliminited _is_ an argument
|
|
|
|
if ((*pos)->asDelimInset()) {
|
|
|
|
ar.push_back(*pos);
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// always take the first thing, no matter what it is
|
|
|
|
ar.push_back(*pos);
|
|
|
|
|
|
|
|
// go ahead if possible
|
|
|
|
++pos;
|
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
// if the next item is a subscript, it most certainly belongs to the
|
|
|
|
// thing we have
|
2001-11-15 14:14:37 +00:00
|
|
|
extractScript(ar, pos, last);
|
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// but it might be more than that.
|
|
|
|
// FIXME: not implemented
|
|
|
|
//for (MathArray::iterator it = pos + 1; it != last; ++it) {
|
|
|
|
// // always take the first thing, no matter
|
|
|
|
// if (it == pos) {
|
|
|
|
// ar.push_back(*it);
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// returns sequence of char with same code starting at it up to end
|
|
|
|
// it might be less, though...
|
2002-06-18 15:44:30 +00:00
|
|
|
string charSequence
|
|
|
|
(MathArray::const_iterator it, MathArray::const_iterator end)
|
2001-11-15 14:14:37 +00:00
|
|
|
{
|
2002-06-18 15:44:30 +00:00
|
|
|
string s;
|
2002-05-30 07:09:54 +00:00
|
|
|
for (; it != end && (*it)->asCharInset(); ++it)
|
|
|
|
s += (*it)->getChar();
|
2002-06-18 15:44:30 +00:00
|
|
|
return s;
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
void extractStrings(MathArray & ar)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2001-11-09 12:01:10 +00:00
|
|
|
//lyxerr << "\nStrings from: " << ar << "\n";
|
2001-11-15 14:14:37 +00:00
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i) {
|
2002-06-18 15:44:30 +00:00
|
|
|
if (!ar[i]->asCharInset())
|
2001-11-15 14:14:37 +00:00
|
|
|
continue;
|
2002-06-18 15:44:30 +00:00
|
|
|
string s = charSequence(ar.begin() + i, ar.end());
|
2002-08-08 16:08:11 +00:00
|
|
|
ar[i] = MathAtom(new MathStringInset(s));
|
2002-06-18 15:44:30 +00:00
|
|
|
ar.erase(i + 1, i + s.size());
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2001-11-09 12:01:10 +00:00
|
|
|
//lyxerr << "\nStrings to: " << ar << "\n";
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void extractMatrices(MathArray & ar)
|
|
|
|
{
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nMatrices from: " << ar << "\n";
|
2002-04-24 17:07:42 +00:00
|
|
|
// first pass for explicitly delimited stuff
|
2002-08-09 10:22:35 +00:00
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i) {
|
|
|
|
if (!ar[i]->asDelimInset())
|
|
|
|
continue;
|
|
|
|
MathArray const & arr = ar[i]->asDelimInset()->cell(0);
|
|
|
|
if (arr.size() != 1)
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!arr.front()->asGridInset())
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
2002-08-09 10:22:35 +00:00
|
|
|
ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
|
2002-04-24 17:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// second pass for AMS "pmatrix" etc
|
2002-08-14 10:44:48 +00:00
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i)
|
2002-08-09 10:22:35 +00:00
|
|
|
if (ar[i]->asAMSArrayInset())
|
2002-08-14 10:44:48 +00:00
|
|
|
ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nMatrices to: " << ar << "\n";
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2001-11-09 12:01:10 +00:00
|
|
|
// convert this inset somehow to a string
|
2002-08-08 16:44:06 +00:00
|
|
|
bool extractString(MathAtom const & at, string & str)
|
2001-11-09 12:01:10 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
if (at->getChar()) {
|
|
|
|
str = string(1, at->getChar());
|
2001-11-15 14:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2002-08-08 16:44:06 +00:00
|
|
|
if (at->asStringInset()) {
|
|
|
|
str = at->asStringInset()->str();
|
2001-11-15 14:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// convert this inset somehow to a number
|
|
|
|
bool extractNumber(MathArray const & ar, int & i)
|
2002-03-25 12:11:25 +00:00
|
|
|
{
|
2002-06-18 15:44:30 +00:00
|
|
|
istringstream is(charSequence(ar.begin(), ar.end()).c_str());
|
2002-03-25 12:11:25 +00:00
|
|
|
is >> i;
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
bool extractNumber(MathArray const & ar, double & d)
|
2001-11-15 14:14:37 +00:00
|
|
|
{
|
2002-06-18 15:44:30 +00:00
|
|
|
istringstream is(charSequence(ar.begin(), ar.end()).c_str());
|
|
|
|
is >> d;
|
2001-11-15 14:14:37 +00:00
|
|
|
return is;
|
2001-11-09 12:01:10 +00:00
|
|
|
}
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2001-11-09 12:01:10 +00:00
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testString(MathAtom const & at, const string & str)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2001-11-15 14:14:37 +00:00
|
|
|
string s;
|
2002-08-08 16:44:06 +00:00
|
|
|
return extractString(at, s) && str == s;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
2001-11-09 14:23:44 +00:00
|
|
|
|
2001-11-12 14:37:43 +00:00
|
|
|
|
2001-11-09 14:23:44 +00:00
|
|
|
// search end of nested sequence
|
2001-11-09 14:48:57 +00:00
|
|
|
MathArray::iterator endNestSearch(
|
2001-11-09 14:23:44 +00:00
|
|
|
MathArray::iterator it,
|
|
|
|
MathArray::iterator last,
|
|
|
|
TestItemFunc testOpen,
|
|
|
|
TestItemFunc testClose
|
|
|
|
)
|
|
|
|
{
|
|
|
|
for (int level = 0; it != last; ++it) {
|
2002-08-08 16:44:06 +00:00
|
|
|
if (testOpen(*it))
|
2001-11-09 14:23:44 +00:00
|
|
|
++level;
|
2002-08-08 16:44:06 +00:00
|
|
|
if (testClose(*it))
|
2001-11-09 14:23:44 +00:00
|
|
|
--level;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// replace nested sequences by a real Insets
|
|
|
|
void replaceNested(
|
|
|
|
MathArray & ar,
|
|
|
|
TestItemFunc testOpen,
|
|
|
|
TestItemFunc testClose,
|
|
|
|
ReplaceArgumentFunc replaceArg
|
|
|
|
)
|
|
|
|
{
|
2001-11-09 10:44:24 +00:00
|
|
|
// use indices rather than iterators for the loop because we are going
|
|
|
|
// to modify the array.
|
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i) {
|
2001-11-09 14:23:44 +00:00
|
|
|
// check whether this is the begin of the sequence
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!testOpen(ar[i]))
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
|
|
|
|
2001-11-09 14:23:44 +00:00
|
|
|
// search end of sequence
|
2002-08-09 10:22:35 +00:00
|
|
|
MathArray::iterator it = ar.begin() + i;
|
2001-11-09 14:48:57 +00:00
|
|
|
MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
|
2001-11-09 12:01:10 +00:00
|
|
|
if (jt == ar.end())
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// replace the original stuff by the new inset
|
2002-08-09 10:22:35 +00:00
|
|
|
ar[i] = replaceArg(MathArray(it + 1, jt));
|
2002-10-28 09:21:40 +00:00
|
|
|
ar.erase(it + 1, jt + 1);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-11-09 10:44:24 +00:00
|
|
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// split scripts into seperate super- and subscript insets. sub goes in
|
2002-03-21 17:42:56 +00:00
|
|
|
// front of super...
|
2001-11-15 14:14:37 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
void splitScripts(MathArray & ar)
|
|
|
|
{
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nScripts from: " << ar << "\n";
|
2001-11-15 14:14:37 +00:00
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i) {
|
|
|
|
// is this script inset?
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!ar[i]->asScriptInset())
|
2001-11-15 14:14:37 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// no problem if we don't have both...
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!ar[i]->asScriptInset()->hasUp())
|
|
|
|
continue;
|
|
|
|
if (!ar[i]->asScriptInset()->hasDown())
|
2001-11-15 14:14:37 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// create extra script inset and move superscript over
|
2002-08-09 10:22:35 +00:00
|
|
|
MathScriptInset * p = ar[i].nucleus()->asScriptInset();
|
|
|
|
MathScriptInset * q = new MathScriptInset(true);
|
2002-07-30 13:56:02 +00:00
|
|
|
std::swap(q->up(), p->up());
|
2001-11-15 14:14:37 +00:00
|
|
|
p->removeScript(true);
|
|
|
|
|
|
|
|
// insert new inset behind
|
|
|
|
++i;
|
2002-03-21 17:42:56 +00:00
|
|
|
ar.insert(i, MathAtom(q));
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nScripts to: " << ar << "\n";
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-15 15:14:27 +00:00
|
|
|
//
|
|
|
|
// extract exp(...)
|
|
|
|
//
|
|
|
|
|
|
|
|
void extractExps(MathArray & ar)
|
|
|
|
{
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nExps from: " << ar << "\n";
|
2001-11-15 15:14:27 +00:00
|
|
|
for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
|
|
|
|
// is this 'e'?
|
2002-08-09 10:22:35 +00:00
|
|
|
if (ar[i]->getChar() != 'e')
|
2001-11-15 15:14:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// we need an exponent but no subscript
|
2002-08-09 10:22:35 +00:00
|
|
|
MathScriptInset const * sup = ar[i + 1]->asScriptInset();
|
2001-11-15 15:14:27 +00:00
|
|
|
if (!sup || sup->hasDown())
|
|
|
|
continue;
|
|
|
|
|
2002-06-25 13:19:50 +00:00
|
|
|
// create a proper exp-inset as replacement
|
2002-08-09 10:22:35 +00:00
|
|
|
ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
|
|
|
|
ar.erase(i + 1);
|
2001-11-15 15:14:27 +00:00
|
|
|
}
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nExps to: " << ar << "\n";
|
2001-11-15 15:14:27 +00:00
|
|
|
}
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
|
2002-06-25 13:19:50 +00:00
|
|
|
//
|
|
|
|
// extract det(...) from |matrix|
|
|
|
|
//
|
|
|
|
void extractDets(MathArray & ar)
|
|
|
|
{
|
|
|
|
//lyxerr << "\ndet from: " << ar << "\n";
|
|
|
|
for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
|
2002-08-09 07:01:17 +00:00
|
|
|
MathDelimInset const * del = (*it)->asDelimInset();
|
2002-06-25 13:19:50 +00:00
|
|
|
if (!del)
|
|
|
|
continue;
|
|
|
|
if (!del->isAbs())
|
|
|
|
continue;
|
2002-08-08 16:08:11 +00:00
|
|
|
*it = MathAtom(new MathExFuncInset("det", del->cell(0)));
|
2002-06-25 13:19:50 +00:00
|
|
|
}
|
|
|
|
//lyxerr << "\ndet to: " << ar << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
//
|
|
|
|
// search numbers
|
|
|
|
//
|
|
|
|
|
|
|
|
bool isDigitOrSimilar(char c)
|
|
|
|
{
|
|
|
|
return ('0' <= c && c <= '9') || c == '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns sequence of digits
|
|
|
|
string digitSequence
|
|
|
|
(MathArray::const_iterator it, MathArray::const_iterator end)
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
for (; it != end && (*it)->asCharInset(); ++it) {
|
|
|
|
if (!isDigitOrSimilar((*it)->getChar()))
|
|
|
|
break;
|
|
|
|
s += (*it)->getChar();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void extractNumbers(MathArray & ar)
|
|
|
|
{
|
|
|
|
//lyxerr << "\nNumbers from: " << ar << "\n";
|
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i) {
|
|
|
|
if (!ar[i]->asCharInset())
|
|
|
|
continue;
|
|
|
|
if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string s = digitSequence(ar.begin() + i, ar.end());
|
|
|
|
|
2002-08-08 16:08:11 +00:00
|
|
|
ar[i] = MathAtom(new MathNumberInset(s));
|
2002-06-18 15:44:30 +00:00
|
|
|
ar.erase(i + 1, i + s.size());
|
|
|
|
}
|
|
|
|
//lyxerr << "\nNumbers to: " << ar << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-09 14:48:57 +00:00
|
|
|
//
|
|
|
|
// search deliminiters
|
|
|
|
//
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testOpenParan(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "(");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testCloseParan(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, ")");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-09 10:22:35 +00:00
|
|
|
MathAtom replaceDelims(const MathArray & ar)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-09 10:22:35 +00:00
|
|
|
return MathAtom(new MathDelimInset("(", ")", ar));
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// replace '('...')' sequences by a real MathDelimInset
|
2001-11-13 18:33:48 +00:00
|
|
|
void extractDelims(MathArray & ar)
|
|
|
|
{
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nDelims from: " << ar << "\n";
|
2001-11-15 14:14:37 +00:00
|
|
|
replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nDelims to: " << ar << "\n";
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 14:48:57 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// search well-known functions
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2001-11-09 12:01:10 +00:00
|
|
|
// replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
|
2001-11-09 10:44:24 +00:00
|
|
|
// assume 'extractDelims' ran before
|
|
|
|
void extractFunctions(MathArray & ar)
|
|
|
|
{
|
|
|
|
// we need at least two items...
|
2002-07-12 11:21:21 +00:00
|
|
|
if (ar.size() < 2)
|
2001-11-09 10:44:24 +00:00
|
|
|
return;
|
|
|
|
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nFunctions from: " << ar << "\n";
|
2001-11-15 09:51:57 +00:00
|
|
|
for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
|
2001-11-09 10:44:24 +00:00
|
|
|
MathArray::iterator it = ar.begin() + i;
|
2001-11-15 14:14:37 +00:00
|
|
|
MathArray::iterator jt = it + 1;
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2001-11-15 09:51:57 +00:00
|
|
|
string name;
|
2001-11-15 14:14:37 +00:00
|
|
|
// is it a function?
|
2002-05-30 07:09:54 +00:00
|
|
|
if ((*it)->asUnknownInset()) {
|
2001-11-15 14:14:37 +00:00
|
|
|
// it certainly is if it is well known...
|
2002-08-05 16:21:19 +00:00
|
|
|
name = (*it)->name();
|
2002-03-21 17:42:56 +00:00
|
|
|
} else {
|
2001-11-15 09:51:57 +00:00
|
|
|
// is this a user defined function?
|
2001-11-15 14:14:37 +00:00
|
|
|
// it it probably not, if it doesn't have a name.
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!extractString(*it, name))
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
2001-11-15 14:14:37 +00:00
|
|
|
// it is not if it has no argument
|
2001-11-09 10:44:24 +00:00
|
|
|
if (jt == ar.end())
|
|
|
|
continue;
|
2001-11-15 14:14:37 +00:00
|
|
|
// guess so, if this is followed by
|
|
|
|
// a DelimInset with a single item in the cell
|
2002-08-09 07:01:17 +00:00
|
|
|
MathDelimInset const * del = (*jt)->asDelimInset();
|
2001-11-15 14:14:37 +00:00
|
|
|
if (!del || del->cell(0).size() != 1)
|
|
|
|
continue;
|
|
|
|
// fall trough into main branch
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// do we have an exponent like in
|
|
|
|
// 'sin' '^2' 'x' -> 'sin(x)' '^2'
|
|
|
|
MathArray exp;
|
|
|
|
extractScript(exp, jt, ar.end());
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// create a proper inset as replacement
|
2001-11-15 09:51:57 +00:00
|
|
|
MathExFuncInset * p = new MathExFuncInset(name);
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// jt points to the "argument". Get hold of this.
|
|
|
|
MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
|
2001-11-09 10:44:24 +00:00
|
|
|
|
|
|
|
// replace the function name by a real function inset
|
2002-08-08 16:08:11 +00:00
|
|
|
*it = MathAtom(p);
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// remove the source of the argument from the array
|
2001-11-15 14:14:37 +00:00
|
|
|
ar.erase(it + 1, st);
|
|
|
|
|
|
|
|
// re-insert exponent
|
|
|
|
ar.insert(i + 1, exp);
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nFunctions to: " << ar << "\n";
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-11-09 10:44:24 +00:00
|
|
|
|
|
|
|
|
2001-11-09 14:48:57 +00:00
|
|
|
//
|
|
|
|
// search integrals
|
|
|
|
//
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testSymbol(MathAtom const & at, string const & name)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return at->asSymbolInset() && at->asSymbolInset()->name() == name;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testIntSymbol(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testSymbol(at, "int");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testIntegral(MathAtom const & at)
|
2002-07-30 17:51:19 +00:00
|
|
|
{
|
|
|
|
return
|
2002-08-08 16:44:06 +00:00
|
|
|
testIntSymbol(at) ||
|
|
|
|
( at->asScriptInset()
|
|
|
|
&& at->asScriptInset()->nuc().size()
|
|
|
|
&& testIntSymbol(at->asScriptInset()->nuc().back()) );
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testIntDiff(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "d");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
|
|
|
|
// assume 'extractDelims' ran before
|
|
|
|
void extractIntegrals(MathArray & ar)
|
|
|
|
{
|
|
|
|
// we need at least three items...
|
2002-07-12 11:21:21 +00:00
|
|
|
if (ar.size() < 3)
|
2001-11-09 14:23:44 +00:00
|
|
|
return;
|
|
|
|
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nIntegrals from: " << ar << "\n";
|
2001-11-15 14:14:37 +00:00
|
|
|
for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
|
2001-11-09 14:23:44 +00:00
|
|
|
MathArray::iterator it = ar.begin() + i;
|
|
|
|
|
|
|
|
// search 'd'
|
|
|
|
MathArray::iterator jt =
|
2002-07-30 17:51:19 +00:00
|
|
|
endNestSearch(it, ar.end(), testIntegral, testIntDiff);
|
2001-11-09 14:23:44 +00:00
|
|
|
|
2001-11-09 18:02:20 +00:00
|
|
|
// something sensible found?
|
|
|
|
if (jt == ar.end())
|
|
|
|
continue;
|
|
|
|
|
2002-07-30 17:51:19 +00:00
|
|
|
// is this a integral name?
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testIntegral(*it))
|
2002-07-30 17:51:19 +00:00
|
|
|
continue;
|
2001-11-09 16:27:44 +00:00
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// core ist part from behind the scripts to the 'd'
|
2002-07-30 17:51:19 +00:00
|
|
|
MathExIntInset * p = new MathExIntInset("int");
|
|
|
|
|
|
|
|
// handle scripts if available
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testIntSymbol(*it)) {
|
|
|
|
p->cell(2) = (*it)->asScriptInset()->down();
|
|
|
|
p->cell(3) = (*it)->asScriptInset()->up();
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
|
|
|
p->cell(0) = MathArray(it + 1, jt);
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// use the "thing" behind the 'd' as differential
|
2001-11-13 18:33:48 +00:00
|
|
|
MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// remove used parts
|
|
|
|
ar.erase(it + 1, tt);
|
2002-08-08 16:08:11 +00:00
|
|
|
*it = MathAtom(p);
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nIntegrals to: " << ar << "\n";
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-12 14:37:43 +00:00
|
|
|
//
|
|
|
|
// search sums
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
bool testEqualSign(MathAtom const & at)
|
2001-11-12 14:37:43 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "=");
|
2001-11-12 14:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testSumSymbol(MathAtom const & p)
|
2002-07-30 17:51:19 +00:00
|
|
|
{
|
|
|
|
return testSymbol(p, "sum");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testSum(MathAtom const & at)
|
2002-07-30 17:51:19 +00:00
|
|
|
{
|
|
|
|
return
|
2002-08-08 16:44:06 +00:00
|
|
|
testSumSymbol(at) ||
|
|
|
|
( at->asScriptInset()
|
|
|
|
&& at->asScriptInset()->nuc().size()
|
|
|
|
&& testSumSymbol(at->asScriptInset()->nuc().back()) );
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
2001-11-12 14:37:43 +00:00
|
|
|
|
|
|
|
// replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
|
|
|
|
// assume 'extractDelims' ran before
|
|
|
|
void extractSums(MathArray & ar)
|
|
|
|
{
|
|
|
|
// we need at least two items...
|
2002-07-12 11:21:21 +00:00
|
|
|
if (ar.size() < 2)
|
2001-11-12 14:37:43 +00:00
|
|
|
return;
|
|
|
|
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nSums from: " << ar << "\n";
|
2002-07-12 11:21:21 +00:00
|
|
|
for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
|
2001-11-12 14:37:43 +00:00
|
|
|
MathArray::iterator it = ar.begin() + i;
|
|
|
|
|
|
|
|
// is this a sum name?
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!testSum(ar[i]))
|
2001-11-12 14:37:43 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// create a proper inset as replacement
|
|
|
|
MathExIntInset * p = new MathExIntInset("sum");
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// collect lower bound and summation index
|
2002-08-09 10:22:35 +00:00
|
|
|
MathScriptInset const * sub = ar[i]->asScriptInset();
|
2002-07-30 17:51:19 +00:00
|
|
|
if (sub && sub->hasDown()) {
|
|
|
|
// try to figure out the summation index from the subscript
|
2002-08-09 07:01:17 +00:00
|
|
|
MathArray const & ar = sub->down();
|
|
|
|
MathArray::const_iterator xt =
|
2002-07-30 17:51:19 +00:00
|
|
|
find_if(ar.begin(), ar.end(), &testEqualSign);
|
|
|
|
if (xt != ar.end()) {
|
|
|
|
// we found a '=', use everything in front of that as index,
|
|
|
|
// and everything behind as lower index
|
|
|
|
p->cell(1) = MathArray(ar.begin(), xt);
|
|
|
|
p->cell(2) = MathArray(xt + 1, ar.end());
|
|
|
|
} else {
|
|
|
|
// use everything as summation index, don't use scripts.
|
|
|
|
p->cell(1) = ar;
|
|
|
|
}
|
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// collect upper bound
|
2002-07-30 17:51:19 +00:00
|
|
|
if (sub && sub->hasUp())
|
2002-08-02 14:29:42 +00:00
|
|
|
p->cell(3) = sub->up();
|
2001-11-12 14:37:43 +00:00
|
|
|
|
2002-07-30 17:51:19 +00:00
|
|
|
// use something behind the script as core
|
|
|
|
MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ar.erase(it + 1, tt);
|
2002-08-08 16:08:11 +00:00
|
|
|
*it = MathAtom(p);
|
2001-11-12 14:37:43 +00:00
|
|
|
}
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nSums to: " << ar << "\n";
|
2001-11-12 14:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 16:27:06 +00:00
|
|
|
//
|
|
|
|
// search differential stuff
|
|
|
|
//
|
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// tests for 'd' or '\partial'
|
2001-11-15 14:14:37 +00:00
|
|
|
bool testDiffItem(MathAtom const & at)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "d");
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
bool testDiffArray(MathArray const & ar)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2001-11-15 14:14:37 +00:00
|
|
|
return ar.size() && testDiffItem(ar.front());
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testDiffFrac(MathAtom const & at)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return
|
|
|
|
at->asFracInset()
|
|
|
|
&& testDiffArray(at->asFracInset()->cell(0))
|
|
|
|
&& testDiffArray(at->asFracInset()->cell(1));
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 16:27:06 +00:00
|
|
|
void extractDiff(MathArray & ar)
|
|
|
|
{
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nDiffs from: " << ar << "\n";
|
2001-11-15 09:51:57 +00:00
|
|
|
for (MathArray::size_type i = 0; i < ar.size(); ++i) {
|
2001-11-13 18:33:48 +00:00
|
|
|
MathArray::iterator it = ar.begin() + i;
|
|
|
|
|
|
|
|
// is this a "differential fraction"?
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testDiffFrac(*it))
|
2001-11-13 18:33:48 +00:00
|
|
|
continue;
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2002-08-09 07:01:17 +00:00
|
|
|
MathFracInset const * f = (*it)->asFracInset();
|
2001-11-13 18:33:48 +00:00
|
|
|
if (!f) {
|
|
|
|
lyxerr << "should not happen\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a proper diff inset
|
2001-11-15 14:14:37 +00:00
|
|
|
MathDiffInset * diff = new MathDiffInset;
|
2001-11-13 18:33:48 +00:00
|
|
|
|
2001-11-15 09:51:57 +00:00
|
|
|
// collect function, let jt point behind last used item
|
2002-03-21 17:42:56 +00:00
|
|
|
MathArray::iterator jt = it + 1;
|
|
|
|
//int n = 1;
|
2002-08-09 07:01:17 +00:00
|
|
|
MathArray const & numer = f->cell(0);
|
2002-06-18 15:44:30 +00:00
|
|
|
if (numer.size() > 1 && numer[1]->asScriptInset()) {
|
2001-11-15 09:51:57 +00:00
|
|
|
// this is something like d^n f(x) / d... or d^n / d...
|
2001-11-15 14:14:37 +00:00
|
|
|
// FIXME
|
2002-03-21 17:42:56 +00:00
|
|
|
//n = 1;
|
|
|
|
if (numer.size() > 2)
|
2001-11-15 14:14:37 +00:00
|
|
|
diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
else
|
2001-11-15 14:14:37 +00:00
|
|
|
jt = extractArgument(diff->cell(0), jt, ar.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
} else {
|
|
|
|
// simply d f(x) / d... or d/d...
|
2002-03-21 17:42:56 +00:00
|
|
|
if (numer.size() > 1)
|
2001-11-15 14:14:37 +00:00
|
|
|
diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
else
|
2001-11-15 14:14:37 +00:00
|
|
|
jt = extractArgument(diff->cell(0), jt, ar.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
}
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// collect denominator parts
|
2002-08-09 07:01:17 +00:00
|
|
|
MathArray const & denom = f->cell(1);
|
|
|
|
for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
|
2001-11-15 14:14:37 +00:00
|
|
|
// find the next 'd'
|
2002-08-09 07:01:17 +00:00
|
|
|
MathArray::const_iterator et
|
|
|
|
= find_if(dt + 1, denom.end(), &testDiffItem);
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// point before this
|
2002-08-09 07:01:17 +00:00
|
|
|
MathArray::const_iterator st = et - 1;
|
|
|
|
MathScriptInset const * script = (*st)->asScriptInset();
|
2001-11-15 14:14:37 +00:00
|
|
|
if (script && script->hasUp()) {
|
|
|
|
// things like d.../dx^n
|
|
|
|
int mult = 1;
|
2002-08-02 14:29:42 +00:00
|
|
|
if (extractNumber(script->up(), mult)) {
|
2002-02-16 15:59:55 +00:00
|
|
|
//lyxerr << "mult: " << mult << endl;
|
2001-11-15 14:14:37 +00:00
|
|
|
for (int i = 0; i < mult; ++i)
|
|
|
|
diff->addDer(MathArray(dt + 1, st));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// just d.../dx
|
|
|
|
diff->addDer(MathArray(dt + 1, et));
|
2001-11-15 09:51:57 +00:00
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
dt = et;
|
2001-11-15 09:51:57 +00:00
|
|
|
}
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ar.erase(it + 1, jt);
|
2002-08-08 16:08:11 +00:00
|
|
|
*it = MathAtom(diff);
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
2002-02-15 14:07:09 +00:00
|
|
|
//lyxerr << "\nDiffs to: " << ar << "\n";
|
2001-11-13 16:27:06 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 08:26:41 +00:00
|
|
|
|
2002-07-12 11:21:21 +00:00
|
|
|
//
|
|
|
|
// search limits
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
bool testRightArrow(MathAtom const & at)
|
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testSymbol(at, "to") || testSymbol(at, "rightarrow");
|
2002-07-12 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
|
|
|
|
// assume 'extractDelims' ran before
|
|
|
|
void extractLims(MathArray & ar)
|
|
|
|
{
|
|
|
|
// we need at least three items...
|
|
|
|
if (ar.size() < 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//lyxerr << "\nLimits from: " << ar << "\n";
|
|
|
|
for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
|
|
|
|
MathArray::iterator it = ar.begin() + i;
|
|
|
|
|
|
|
|
// is this a limit function?
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testSymbol(*it, "lim"))
|
2002-07-12 11:21:21 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// the next one must be a subscript (without superscript)
|
2002-08-09 07:01:17 +00:00
|
|
|
MathScriptInset const * sub = (*(it + 1))->asScriptInset();
|
2002-07-12 11:21:21 +00:00
|
|
|
if (!sub || !sub->hasDown() || sub->hasUp())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// and it must contain a -> symbol
|
2002-08-09 07:01:17 +00:00
|
|
|
MathArray const & s = sub->down();
|
|
|
|
MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
|
2002-07-12 11:21:21 +00:00
|
|
|
if (st == s.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// the -> splits the subscript int x and x0
|
2002-07-30 13:56:02 +00:00
|
|
|
MathArray x = MathArray(s.begin(), st);
|
|
|
|
MathArray x0 = MathArray(st + 1, s.end());
|
2002-07-12 11:21:21 +00:00
|
|
|
|
|
|
|
// use something behind the script as core
|
|
|
|
MathArray f;
|
|
|
|
MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ar.erase(it + 1, tt);
|
2002-08-08 16:08:11 +00:00
|
|
|
|
|
|
|
// create a proper inset as replacement
|
|
|
|
*it = MathAtom(new MathLimInset(f, x, x0));
|
2002-07-12 11:21:21 +00:00
|
|
|
}
|
|
|
|
//lyxerr << "\nLimits to: " << ar << "\n";
|
|
|
|
}
|
|
|
|
|
2001-11-16 08:26:41 +00:00
|
|
|
|
2001-11-13 16:27:06 +00:00
|
|
|
//
|
|
|
|
// combine searches
|
|
|
|
//
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
void extractStructure(MathArray & ar)
|
|
|
|
{
|
2002-08-14 10:44:48 +00:00
|
|
|
//lyxerr << "\nStructure from: " << ar << "\n";
|
2002-07-30 17:51:19 +00:00
|
|
|
extractIntegrals(ar);
|
|
|
|
extractSums(ar);
|
2001-11-15 14:14:37 +00:00
|
|
|
splitScripts(ar);
|
2002-06-18 15:44:30 +00:00
|
|
|
extractNumbers(ar);
|
2001-11-09 10:44:24 +00:00
|
|
|
extractMatrices(ar);
|
|
|
|
extractDelims(ar);
|
|
|
|
extractFunctions(ar);
|
2002-06-25 13:19:50 +00:00
|
|
|
extractDets(ar);
|
2001-11-13 16:27:06 +00:00
|
|
|
extractDiff(ar);
|
2001-11-15 15:14:27 +00:00
|
|
|
extractExps(ar);
|
2002-07-12 11:21:21 +00:00
|
|
|
extractLims(ar);
|
2001-11-12 13:09:26 +00:00
|
|
|
extractStrings(ar);
|
2002-08-14 10:44:48 +00:00
|
|
|
//lyxerr << "\nStructure to: " << ar << "\n";
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void write(MathArray const & dat, WriteStream & wi)
|
|
|
|
{
|
|
|
|
MathArray ar = dat;
|
2001-11-09 12:01:10 +00:00
|
|
|
extractStrings(ar);
|
2002-08-05 10:27:40 +00:00
|
|
|
wi.firstitem() = true;
|
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
|
2002-07-30 13:56:02 +00:00
|
|
|
(*it)->write(wi);
|
2002-08-05 10:27:40 +00:00
|
|
|
wi.firstitem() = false;
|
|
|
|
}
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 12:01:10 +00:00
|
|
|
void normalize(MathArray const & ar, NormalStream & os)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2001-11-09 12:01:10 +00:00
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->normalize(os);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void octavize(MathArray const & dat, OctaveStream & os)
|
|
|
|
{
|
|
|
|
MathArray ar = dat;
|
|
|
|
extractStructure(ar);
|
2002-07-30 13:56:02 +00:00
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->octavize(os);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void maplize(MathArray const & dat, MapleStream & os)
|
|
|
|
{
|
|
|
|
MathArray ar = dat;
|
|
|
|
extractStructure(ar);
|
2002-07-30 13:56:02 +00:00
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->maplize(os);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-28 17:15:19 +00:00
|
|
|
void maximize(MathArray const & dat, MaximaStream & os)
|
|
|
|
{
|
|
|
|
MathArray ar = dat;
|
|
|
|
extractStructure(ar);
|
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->maximize(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-01 11:17:14 +00:00
|
|
|
void mathematicize(MathArray const & dat, MathematicaStream & os)
|
|
|
|
{
|
|
|
|
MathArray ar = dat;
|
|
|
|
extractStructure(ar);
|
2002-07-30 13:56:02 +00:00
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->mathematicize(os);
|
2002-07-01 11:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
void mathmlize(MathArray const & dat, MathMLStream & os)
|
|
|
|
{
|
|
|
|
MathArray ar = dat;
|
|
|
|
extractStructure(ar);
|
|
|
|
if (ar.size() == 0)
|
|
|
|
os << "<mrow/>";
|
|
|
|
else if (ar.size() == 1)
|
2002-08-08 17:11:30 +00:00
|
|
|
os << ar.front();
|
2001-11-09 10:44:24 +00:00
|
|
|
else {
|
|
|
|
os << MTag("mrow");
|
2002-07-30 13:56:02 +00:00
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->mathmlize(os);
|
2001-11-09 10:44:24 +00:00
|
|
|
os << ETag("mrow");
|
|
|
|
}
|
|
|
|
}
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
string captureOutput(string const & cmd, string const & data)
|
|
|
|
{
|
2002-10-28 17:15:19 +00:00
|
|
|
string command = "echo '" + data + "' | " + cmd;
|
|
|
|
lyxerr << "calling: " << command << endl;
|
|
|
|
cmd_ret const ret = RunCommand(command);
|
|
|
|
return ret.second;
|
|
|
|
}
|
|
|
|
|
2002-10-28 21:24:06 +00:00
|
|
|
string::size_type get_matching_brace(string const & str, string::size_type i)
|
|
|
|
{
|
|
|
|
int count = 1;
|
|
|
|
string::size_type n = str.size();
|
|
|
|
while (i < n) {
|
|
|
|
i = str.find_first_of("{}", i+1);
|
|
|
|
if (i == string::npos) return i;
|
|
|
|
if (str[i] == '{')
|
|
|
|
++count;
|
|
|
|
else
|
|
|
|
--count;
|
|
|
|
if (count == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type get_matching_brace_back(string const & str, string::size_type i)
|
|
|
|
{
|
|
|
|
int count = 1;
|
|
|
|
while (i > 0) {
|
|
|
|
i = str.find_last_of("{}", i-1);
|
|
|
|
if (i == string::npos) return i;
|
|
|
|
if (str[i] == '}')
|
|
|
|
++count;
|
|
|
|
else
|
|
|
|
--count;
|
|
|
|
if (count == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return string::npos;
|
|
|
|
}
|
2002-10-28 17:15:19 +00:00
|
|
|
|
|
|
|
MathArray pipeThroughMaxima(string const &, MathArray const & ar)
|
|
|
|
{
|
|
|
|
ostringstream os;
|
|
|
|
MaximaStream ms(os);
|
|
|
|
ms << ar;
|
|
|
|
string expr = os.str().c_str();
|
|
|
|
string const header = "SIMPSUM:true;";
|
|
|
|
|
|
|
|
string out;
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
// try to fix missing '*' the hard way
|
|
|
|
//
|
|
|
|
// > echo "2x;" | maxima
|
|
|
|
// ...
|
|
|
|
// (C1) Incorrect syntax: x is not an infix operator
|
|
|
|
// 2x;
|
|
|
|
// ^
|
|
|
|
//
|
|
|
|
lyxerr << "checking expr: '" << expr << "'\n";
|
|
|
|
string full = header + "tex(" + expr + ");";
|
|
|
|
out = captureOutput("maxima", full);
|
|
|
|
|
|
|
|
// leave loop if expression syntax is probably ok
|
|
|
|
if (out.find("Incorrect syntax") == string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// search line with "Incorrect syntax"
|
|
|
|
istringstream is(out.c_str());
|
|
|
|
string line;
|
|
|
|
while (is) {
|
|
|
|
getline(is, line);
|
|
|
|
if (line.find("Incorrect syntax") != string::npos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2nd next line is the one with caret
|
|
|
|
getline(is, line);
|
|
|
|
getline(is, line);
|
|
|
|
string::size_type pos = line.find('^');
|
|
|
|
lyxerr << "found caret at pos: '" << pos << "'\n";
|
|
|
|
if (pos == string::npos || pos < 4)
|
|
|
|
break; // caret position not found
|
|
|
|
pos -= 4; // skip the "tex(" part
|
|
|
|
if (expr[pos] == '*')
|
|
|
|
break; // two '*' in a row are definitely bad
|
|
|
|
expr.insert(pos, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<string> tmp = getVectorFromString(out, "$$");
|
|
|
|
if (tmp.size() < 2)
|
|
|
|
return MathArray();
|
|
|
|
|
|
|
|
out = subst(tmp[1],"\\>", "");
|
|
|
|
lyxerr << "out: '" << out << "'\n";
|
2002-10-28 21:24:06 +00:00
|
|
|
|
|
|
|
// Ugly code that tries to make the result prettier
|
|
|
|
|
|
|
|
string::size_type i = out.find("\\mathchoice");
|
|
|
|
while (i != string::npos) {
|
|
|
|
string::size_type j = get_matching_brace(out, i + 12);
|
|
|
|
string::size_type k = get_matching_brace(out, j + 1);
|
|
|
|
k = get_matching_brace(out, k + 1);
|
|
|
|
k = get_matching_brace(out, k + 1);
|
|
|
|
string mid = out.substr(i + 13,j - i - 13);
|
|
|
|
if (mid.find("\\over") != string::npos)
|
|
|
|
mid = '{' + mid + '}';
|
|
|
|
out = out.substr(0,i)
|
|
|
|
+ mid
|
|
|
|
+ out.substr(k + 1);
|
|
|
|
//lyxerr << "out: " << out << endl;
|
|
|
|
i = out.find("\\mathchoice", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = out.find("\\over");
|
|
|
|
while (i != string::npos) {
|
|
|
|
string::size_type j = get_matching_brace_back(out, i - 1);
|
|
|
|
if (j == string::npos || j == 0) break;
|
|
|
|
string::size_type k = get_matching_brace(out, i + 5);
|
|
|
|
if (k == string::npos || k + 1 == out.size()) break;
|
|
|
|
out = out.substr(0,j - 1)
|
|
|
|
+ "\\frac"
|
|
|
|
+ out.substr(j,i - j)
|
|
|
|
+ out.substr(i + 5,k - i - 4)
|
|
|
|
+ out.substr(k + 2);
|
|
|
|
//lyxerr << "out: " << out << endl;
|
|
|
|
i = out.find("\\over", i + 4);
|
|
|
|
}
|
2002-10-28 17:15:19 +00:00
|
|
|
MathArray res;
|
|
|
|
mathed_parse_cell(res, out);
|
|
|
|
return res;
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
|
|
|
|
{
|
|
|
|
string header = "readlib(latex):\n";
|
|
|
|
|
|
|
|
// remove the \\it for variable names
|
|
|
|
//"#`latex/csname_font` := `\\it `:"
|
|
|
|
header +=
|
|
|
|
"`latex/csname_font` := ``:\n";
|
|
|
|
|
|
|
|
// export matrices in (...) instead of [...]
|
|
|
|
header +=
|
|
|
|
"`latex/latex/matrix` := "
|
|
|
|
"subs(`[`=`(`, `]`=`)`,"
|
|
|
|
"eval(`latex/latex/matrix`)):\n";
|
|
|
|
|
|
|
|
// replace \\cdots with proper '*'
|
|
|
|
header +=
|
|
|
|
"`latex/latex/*` := "
|
|
|
|
"subs(`\\,`=`\\cdot `,"
|
|
|
|
"eval(`latex/latex/*`)):\n";
|
|
|
|
|
|
|
|
// remove spurious \\noalign{\\medskip} in matrix output
|
|
|
|
header +=
|
|
|
|
"`latex/latex/matrix`:= "
|
|
|
|
"subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
|
|
|
|
"eval(`latex/latex/matrix`)):\n";
|
|
|
|
|
|
|
|
//"#`latex/latex/symbol` "
|
|
|
|
// " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
|
|
|
|
|
|
|
|
string trailer = "quit;";
|
|
|
|
ostringstream os;
|
|
|
|
MapleStream ms(os);
|
|
|
|
ms << ar;
|
|
|
|
string expr = os.str().c_str();
|
|
|
|
lyxerr << "ar: '" << ar << "'\n";
|
2002-06-25 13:19:50 +00:00
|
|
|
lyxerr << "ms: '" << os.str() << "'\n";
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
// try to fix missing '*' the hard way by using mint
|
|
|
|
//
|
|
|
|
// ... > echo "1A;" | mint -i 1 -S -s -q
|
|
|
|
// on line 1: 1A;
|
|
|
|
// ^ syntax error -
|
|
|
|
// Probably missing an operator such as * p
|
|
|
|
//
|
|
|
|
lyxerr << "checking expr: '" << expr << "'\n";
|
|
|
|
string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
|
|
|
|
if (out.empty())
|
|
|
|
break; // expression syntax is ok
|
|
|
|
istringstream is(out.c_str());
|
|
|
|
string line;
|
|
|
|
getline(is, line);
|
|
|
|
if (line.find("on line") != 0)
|
|
|
|
break; // error message not identified
|
|
|
|
getline(is, line);
|
|
|
|
string::size_type pos = line.find('^');
|
|
|
|
if (pos == string::npos || pos < 15)
|
|
|
|
break; // caret position not found
|
|
|
|
pos -= 15; // skip the "on line ..." part
|
|
|
|
if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
|
|
|
|
break; // two '*' in a row are definitely bad
|
|
|
|
expr.insert(pos, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
string full = "latex(" + extra + '(' + expr + "));";
|
|
|
|
string out = captureOutput("maple -q", header + full + trailer);
|
|
|
|
|
|
|
|
// change \_ into _
|
|
|
|
|
|
|
|
//
|
|
|
|
MathArray res;
|
|
|
|
mathed_parse_cell(res, out);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray pipeThroughOctave(string const &, MathArray const & ar)
|
|
|
|
{
|
|
|
|
ostringstream os;
|
|
|
|
OctaveStream vs(os);
|
|
|
|
vs << ar;
|
|
|
|
string expr = os.str().c_str();
|
|
|
|
string out;
|
|
|
|
|
|
|
|
lyxerr << "pipe: ar: '" << ar << "'\n";
|
|
|
|
lyxerr << "pipe: expr: '" << expr << "'\n";
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
//
|
|
|
|
// try to fix missing '*' the hard way
|
|
|
|
// parse error:
|
|
|
|
// >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
|
|
|
|
// ^
|
|
|
|
//
|
|
|
|
lyxerr << "checking expr: '" << expr << "'\n";
|
|
|
|
out = captureOutput("octave -q 2>&1", expr);
|
|
|
|
lyxerr << "checking out: '" << out << "'\n";
|
|
|
|
|
|
|
|
// leave loop if expression syntax is probably ok
|
|
|
|
if (out.find("parse error:") == string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// search line with single caret
|
|
|
|
istringstream is(out.c_str());
|
|
|
|
string line;
|
|
|
|
while (is) {
|
|
|
|
getline(is, line);
|
|
|
|
lyxerr << "skipping line: '" << line << "'\n";
|
|
|
|
if (line.find(">>> ") != string::npos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// found line with error, next line is the one with caret
|
|
|
|
getline(is, line);
|
|
|
|
string::size_type pos = line.find('^');
|
|
|
|
lyxerr << "caret line: '" << line << "'\n";
|
|
|
|
lyxerr << "found caret at pos: '" << pos << "'\n";
|
|
|
|
if (pos == string::npos || pos < 4)
|
|
|
|
break; // caret position not found
|
|
|
|
pos -= 4; // skip the ">>> " part
|
|
|
|
if (expr[pos] == '*')
|
|
|
|
break; // two '*' in a row are definitely bad
|
|
|
|
expr.insert(pos, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out.size() < 6)
|
|
|
|
return MathArray();
|
|
|
|
|
|
|
|
// remove 'ans = '
|
|
|
|
out = out.substr(6);
|
|
|
|
|
|
|
|
// parse output as matrix or single number
|
|
|
|
MathAtom at(new MathArrayInset("array", out));
|
2002-08-09 10:22:35 +00:00
|
|
|
MathArrayInset const * mat = at->asArrayInset();
|
2002-06-18 15:44:30 +00:00
|
|
|
MathArray res;
|
|
|
|
if (mat->ncols() == 1 && mat->nrows() == 1)
|
2002-07-30 13:56:02 +00:00
|
|
|
res.append(mat->cell(0));
|
2002-06-18 15:44:30 +00:00
|
|
|
else {
|
|
|
|
res.push_back(MathAtom(new MathDelimInset("(", ")")));
|
2002-08-09 07:01:17 +00:00
|
|
|
res.back().nucleus()->cell(0).push_back(at);
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray pipeThroughExtern(string const & lang, string const & extra,
|
|
|
|
MathArray const & ar)
|
|
|
|
{
|
|
|
|
if (lang == "octave")
|
|
|
|
return pipeThroughOctave(extra, ar);
|
|
|
|
|
2002-10-28 17:15:19 +00:00
|
|
|
if (lang == "maxima")
|
|
|
|
return pipeThroughMaxima(extra, ar);
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
if (lang == "maple")
|
|
|
|
return pipeThroughMaple(extra, ar);
|
|
|
|
|
|
|
|
// create normalized expression
|
|
|
|
ostringstream os;
|
|
|
|
NormalStream ns(os);
|
|
|
|
os << "[" << extra << ' ';
|
|
|
|
ns << ar;
|
|
|
|
os << "]";
|
|
|
|
string data = os.str().c_str();
|
|
|
|
|
|
|
|
// search external script
|
|
|
|
string file = LibFileSearch("mathed", "extern_" + lang);
|
|
|
|
if (file.empty()) {
|
|
|
|
lyxerr << "converter to '" << lang << "' not found\n";
|
|
|
|
return MathArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
// run external sript
|
|
|
|
string out = captureOutput(file, data);
|
|
|
|
MathArray res;
|
|
|
|
mathed_parse_cell(res, out);
|
|
|
|
return res;
|
|
|
|
}
|