From ac7c86da7430923dc8c97b69a631fdef1f5178ec Mon Sep 17 00:00:00 2001 From: Dekel Tsur Date: Mon, 28 Oct 2002 17:15:19 +0000 Subject: [PATCH] Maxima git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5535 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/ChangeLog | 2 + src/mathed/math_deliminset.C | 12 +++++ src/mathed/math_deliminset.h | 2 + src/mathed/math_exfuncinset.C | 9 ++++ src/mathed/math_exfuncinset.h | 2 + src/mathed/math_exintinset.C | 24 ++++++++-- src/mathed/math_exintinset.h | 2 + src/mathed/math_extern.C | 87 ++++++++++++++++++++++++++++++---- src/mathed/math_extern.h | 2 + src/mathed/math_inset.C | 7 +++ src/mathed/math_inset.h | 3 ++ src/mathed/math_mathmlstream.C | 39 ++++++++++++++- src/mathed/math_mathmlstream.h | 31 +++++++++++- src/mathed/math_matrixinset.C | 18 +++++++ src/mathed/math_matrixinset.h | 2 + src/mathed/math_rootinset.C | 6 +++ src/mathed/math_rootinset.h | 2 + src/mathed/math_streamstr.C | 7 +++ src/mathed/math_streamstr.h | 2 + src/mathed/math_symbolinset.C | 11 +++++ src/mathed/math_symbolinset.h | 2 + 21 files changed, 256 insertions(+), 16 deletions(-) diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index b2adf9411f..2847dec9bb 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,5 +1,7 @@ 2002-10-28 Dekel Tsur + * many files: Add support for Maxima. + * math_scriptinset.C (dispatch): Change handling of LFUN_MATH_LIMITS. * math_symbolinset.C (metrics): Compute em with the default font. diff --git a/src/mathed/math_deliminset.C b/src/mathed/math_deliminset.C index 0a27adbd9b..1b4b477049 100644 --- a/src/mathed/math_deliminset.C +++ b/src/mathed/math_deliminset.C @@ -141,6 +141,18 @@ void MathDelimInset::maplize(MapleStream & os) const os << left_ << cell(0) << right_; } +void MathDelimInset::maximize(MaximaStream & os) const +{ + if (isAbs()) { + if (cell(0).size() == 1 && cell(0).front()->asMatrixInset()) + os << "determinant(" << cell(0) << ")"; + else + os << "abs(" << cell(0) << ")"; + } + else + os << left_ << cell(0) << right_; +} + void MathDelimInset::mathematicize(MathematicaStream & os) const { diff --git a/src/mathed/math_deliminset.h b/src/mathed/math_deliminset.h index 3806b1ff7c..786c48529c 100644 --- a/src/mathed/math_deliminset.h +++ b/src/mathed/math_deliminset.h @@ -45,6 +45,8 @@ public: /// void maplize(MapleStream &) const; /// + void maximize(MaximaStream &) const; + /// void mathematicize(MathematicaStream &) const; /// void mathmlize(MathMLStream &) const; diff --git a/src/mathed/math_exfuncinset.C b/src/mathed/math_exfuncinset.C index 93cd53e79f..ba3afb7091 100644 --- a/src/mathed/math_exfuncinset.C +++ b/src/mathed/math_exfuncinset.C @@ -56,6 +56,15 @@ void MathExFuncInset::maplize(MapleStream & os) const } +void MathExFuncInset::maximize(MaximaStream & os) const +{ + if (name_ == "det") + os << "determinant(" << cell(0) << ')'; + else + os << name_ << '(' << cell(0) << ')'; +} + + string asMathematicaName(string const & name) { if (name == "sin") return "Sin"; diff --git a/src/mathed/math_exfuncinset.h b/src/mathed/math_exfuncinset.h index 7a9d13c945..232225fbac 100644 --- a/src/mathed/math_exfuncinset.h +++ b/src/mathed/math_exfuncinset.h @@ -29,6 +29,8 @@ public: /// void maplize(MapleStream &) const; /// + void maximize(MaximaStream &) const; + /// void mathematicize(MathematicaStream &) const; /// void mathmlize(MathMLStream &) const; diff --git a/src/mathed/math_exintinset.C b/src/mathed/math_exintinset.C index f39b8ba642..7175cba3e3 100644 --- a/src/mathed/math_exintinset.C +++ b/src/mathed/math_exintinset.C @@ -79,15 +79,31 @@ void MathExIntInset::maplize(MapleStream & os) const } +void MathExIntInset::maximize(MaximaStream & os) const +{ + if ( symbol_ == "int" ) + os << "integrate("; + else + os << symbol_ << '('; + + if (cell(0).size()) + os << cell(0) << ','; + else + os << '1' << ','; + if (hasScripts()) + os << cell(1) << ',' << cell(2) << ',' << cell(3) << ')'; + else + os << cell(1) << ')'; +} + void MathExIntInset::mathematicize(MathematicaStream & os) const { if ( symbol_ == "int" ) os << "Integrate["; + else if (symbol_ == "sum") + os << "Sum["; else - if (symbol_ == "sum") - os << "Sum["; - else - os << symbol_ << '['; + os << symbol_ << '['; if (cell(0).size()) os << cell(0) << ','; diff --git a/src/mathed/math_exintinset.h b/src/mathed/math_exintinset.h index 8e44465b7f..34e969e872 100644 --- a/src/mathed/math_exintinset.h +++ b/src/mathed/math_exintinset.h @@ -28,6 +28,8 @@ public: /// void normalize(NormalStream &) const; /// + void maximize(MaximaStream &) const; + /// void maplize(MapleStream &) const; /// void mathematicize(MathematicaStream &) const; diff --git a/src/mathed/math_extern.C b/src/mathed/math_extern.C index 26ed7bf987..89404fbcb6 100644 --- a/src/mathed/math_extern.C +++ b/src/mathed/math_extern.C @@ -31,6 +31,7 @@ #include "support/lyxlib.h" #include "support/systemcall.h" #include "support/filetools.h" +#include "support/lstrings.h" #include @@ -841,6 +842,15 @@ void maplize(MathArray const & dat, MapleStream & os) } +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); +} + + void mathematicize(MathArray const & dat, MathematicaStream & os) { MathArray ar = dat; @@ -873,15 +883,71 @@ namespace { string captureOutput(string const & cmd, string const & data) { - string outfile = lyx::tempName(string(), "mathextern"); - string full = "echo '" + data + "' | (" + cmd + ") > " + outfile; - lyxerr << "calling: " << full << endl; - Systemcall dummy; - dummy.startscript(Systemcall::Wait, full); - string out = GetFileContents(outfile); - lyx::unlink(outfile); - lyxerr << "result: '" << out << "'" << endl; - return out; + string command = "echo '" + data + "' | " + cmd; + lyxerr << "calling: " << command << endl; + cmd_ret const ret = RunCommand(command); + return ret.second; + } + + + 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 tmp = getVectorFromString(out, "$$"); + if (tmp.size() < 2) + return MathArray(); + + out = subst(tmp[1],"\\>", ""); + + lyxerr << "out: '" << out << "'\n"; + MathArray res; + mathed_parse_cell(res, out); + return res; } @@ -1039,6 +1105,9 @@ MathArray pipeThroughExtern(string const & lang, string const & extra, if (lang == "octave") return pipeThroughOctave(extra, ar); + if (lang == "maxima") + return pipeThroughMaxima(extra, ar); + if (lang == "maple") return pipeThroughMaple(extra, ar); diff --git a/src/mathed/math_extern.h b/src/mathed/math_extern.h index 9e2f23c8b5..31a2fba64b 100644 --- a/src/mathed/math_extern.h +++ b/src/mathed/math_extern.h @@ -9,6 +9,7 @@ class NormalStream; class MapleStream; +class MaximaStream; class MathematicaStream; class MathMLStream; class OctaveStream; @@ -18,6 +19,7 @@ class MathArray; void write(MathArray const &, WriteStream &); void normalize(MathArray const &, NormalStream &); void maplize(MathArray const &, MapleStream &); +void maximize(MathArray const &, MaximaStream &); void mathematicize(MathArray const &, MathematicaStream &); void mathmlize(MathArray const &, MathMLStream &); void octavize(MathArray const &, OctaveStream &); diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index 62cc723c08..2a6445205d 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -224,6 +224,13 @@ void MathInset::maplize(MapleStream & os) const } +void MathInset::maximize(MaximaStream & os) const +{ + MapleStream ns(os.os()); + maplize(ns); +} + + void MathInset::mathematicize(MathematicaStream & os) const { NormalStream ns(os.os()); diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index 0ac7f17bed..b847533983 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -68,6 +68,7 @@ class InsetRef; class NormalStream; class OctaveStream; class MapleStream; +class MaximaStream; class MathematicaStream; class MathMLStream; class WriteStream; @@ -270,6 +271,8 @@ public: virtual void normalize(NormalStream &) const; /// write content as something readable by Maple virtual void maplize(MapleStream &) const; + /// write content as something readable by Maxima + virtual void maximize(MaximaStream &) const; /// write content as something readable by Mathematica virtual void mathematicize(MathematicaStream &) const; /// write content as something resembling MathML diff --git a/src/mathed/math_mathmlstream.C b/src/mathed/math_mathmlstream.C index b440fdb39d..007c3936aa 100644 --- a/src/mathed/math_mathmlstream.C +++ b/src/mathed/math_mathmlstream.C @@ -174,7 +174,6 @@ void MathMLStream::cr() } - ////////////////////////////////////////////////////////////////////// @@ -216,6 +215,44 @@ MapleStream & operator<<(MapleStream & ms, int i) ////////////////////////////////////////////////////////////////////// +MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at) +{ + at->maximize(ms); + return ms; +} + + +MaximaStream & operator<<(MaximaStream & ms, MathArray const & ar) +{ + maximize(ar, ms); + return ms; +} + + +MaximaStream & operator<<(MaximaStream & ms, char const * s) +{ + ms.os() << s; + return ms; +} + + +MaximaStream & operator<<(MaximaStream & ms, char c) +{ + ms.os() << c; + return ms; +} + + +MaximaStream & operator<<(MaximaStream & ms, int i) +{ + ms.os() << i; + return ms; +} + + +////////////////////////////////////////////////////////////////////// + + MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at) { at->mathematicize(ms); diff --git a/src/mathed/math_mathmlstream.h b/src/mathed/math_mathmlstream.h index 9ed06a5e00..9e75152e08 100644 --- a/src/mathed/math_mathmlstream.h +++ b/src/mathed/math_mathmlstream.h @@ -160,8 +160,6 @@ NormalStream & operator<<(NormalStream &, char); NormalStream & operator<<(NormalStream &, int); - - // // Maple // @@ -191,6 +189,35 @@ MapleStream & operator<<(MapleStream &, char); MapleStream & operator<<(MapleStream &, int); +// +// Maxima +// + + +class MaximaStream { +public: + /// + explicit MaximaStream(std::ostream & os) : os_(os) {} + /// + std::ostream & os() { return os_; } +private: + /// + std::ostream & os_; +}; + + +/// +MaximaStream & operator<<(MaximaStream &, MathAtom const &); +/// +MaximaStream & operator<<(MaximaStream &, MathArray const &); +/// +MaximaStream & operator<<(MaximaStream &, char const *); +/// +MaximaStream & operator<<(MaximaStream &, char); +/// +MaximaStream & operator<<(MaximaStream &, int); + + // // Mathematica // diff --git a/src/mathed/math_matrixinset.C b/src/mathed/math_matrixinset.C index 330c3cef62..4cfedd59b4 100644 --- a/src/mathed/math_matrixinset.C +++ b/src/mathed/math_matrixinset.C @@ -43,6 +43,24 @@ void MathMatrixInset::maplize(MapleStream & os) const } +void MathMatrixInset::maximize(MaximaStream & os) const +{ + os << "matrix("; + for (row_type row = 0; row < nrows(); ++row) { + if (row) + os << ','; + os << '['; + for (col_type col = 0; col < ncols(); ++col) { + if (col) + os << ','; + os << cell(index(row, col)); + } + os << ']'; + } + os << ')'; +} + + void MathMatrixInset::mathmlize(MathMLStream & os) const { MathGridInset::mathmlize(os); diff --git a/src/mathed/math_matrixinset.h b/src/mathed/math_matrixinset.h index 9c796bfa27..47a0b714e4 100644 --- a/src/mathed/math_matrixinset.h +++ b/src/mathed/math_matrixinset.h @@ -30,6 +30,8 @@ public: /// void maplize(MapleStream &) const; /// + void maximize(MaximaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void octavize(OctaveStream &) const; diff --git a/src/mathed/math_rootinset.C b/src/mathed/math_rootinset.C index a73efbd4f4..9194bbbd28 100644 --- a/src/mathed/math_rootinset.C +++ b/src/mathed/math_rootinset.C @@ -89,6 +89,12 @@ bool MathRootInset::idxUpDown(idx_type & idx, pos_type & pos, bool up, int) cons } +void MathRootInset::maplize(MapleStream & os) const +{ + os << "(" << cell(1) << ")^(1/(" << cell(0) <<"))"; +} + + void MathRootInset::octavize(OctaveStream & os) const { os << "root(" << cell(1) << ',' << cell(0) <<')'; diff --git a/src/mathed/math_rootinset.h b/src/mathed/math_rootinset.h index e117ba19ab..b5c6bd526e 100644 --- a/src/mathed/math_rootinset.h +++ b/src/mathed/math_rootinset.h @@ -47,6 +47,8 @@ public: /// void mathmlize(MathMLStream &) const; /// + void maplize(MapleStream &) const; + /// void octavize(OctaveStream &) const; }; diff --git a/src/mathed/math_streamstr.C b/src/mathed/math_streamstr.C index c2c8062b33..a1cb386b6e 100644 --- a/src/mathed/math_streamstr.C +++ b/src/mathed/math_streamstr.C @@ -32,6 +32,13 @@ MapleStream & operator<<(MapleStream & ms, string const & s) } +MaximaStream & operator<<(MaximaStream & ms, string const & s) +{ + ms.os() << s; + return ms; +} + + MathematicaStream & operator<<(MathematicaStream & ms, string const & s) { ms.os() << s; diff --git a/src/mathed/math_streamstr.h b/src/mathed/math_streamstr.h index 72be3a0ca2..499de5e8ce 100644 --- a/src/mathed/math_streamstr.h +++ b/src/mathed/math_streamstr.h @@ -12,6 +12,7 @@ class WriteStream; class NormalStream; class MapleStream; +class MaximaStream; class MathematicaStream; class MathMLStream; class OctaveStream; @@ -23,6 +24,7 @@ class OctaveStream; WriteStream & operator<<(WriteStream & ws, string const & s); NormalStream & operator<<(NormalStream & ns, string const & s); MapleStream & operator<<(MapleStream & ms, string const & s); +MaximaStream & operator<<(MaximaStream & ms, string const & s); MathematicaStream & operator<<(MathematicaStream & ms, string const & s); MathMLStream & operator<<(MathMLStream & ms, string const & s); OctaveStream & operator<<(OctaveStream & os, string const & s); diff --git a/src/mathed/math_symbolinset.C b/src/mathed/math_symbolinset.C index 8463bc998d..5a2129324f 100644 --- a/src/mathed/math_symbolinset.C +++ b/src/mathed/math_symbolinset.C @@ -131,6 +131,17 @@ void MathSymbolInset::maplize(MapleStream & os) const os << name(); } +void MathSymbolInset::maximize(MaximaStream & os) const +{ + if (name() == "cdot") + os << '*'; + else if (name() == "infty") + os << "INF"; + else + os << name(); +} + + void MathSymbolInset::mathematicize(MathematicaStream & os) const { if ( name() == "pi") { os << "Pi"; return;} diff --git a/src/mathed/math_symbolinset.h b/src/mathed/math_symbolinset.h index 30fd215e84..8f59fbcfe2 100644 --- a/src/mathed/math_symbolinset.h +++ b/src/mathed/math_symbolinset.h @@ -47,6 +47,8 @@ public: /// void maplize(MapleStream &) const; /// + void maximize(MaximaStream &) const; + /// void mathematicize(MathematicaStream &) const; /// void mathmlize(MathMLStream &) const; -- 2.39.2