Geof Piroux's patch for Mathematica support

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4506 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2002-07-01 11:17:14 +00:00
parent 643ec3cf04
commit cd19a0b33b
32 changed files with 274 additions and 11 deletions

View File

@ -1,3 +1,7 @@
2002-07-01 Geof Piroux
* math_ex*.[Ch] et al: support for Mathematica as CAS backend
2002-06-28 Angus Leeming <leeming@lyx.org>
* preview.h (preview): don't pass grfx::GraphicPtr & anymore.

View File

@ -144,6 +144,22 @@ void MathDelimInset::maplize(MapleStream & os) const
os << left_ << cell(0) << right_;
}
void MathDelimInset::mathematicize(MathematicaStream & os) const
{
if (isAbs()) {
bool mat =
cell(0).size() == 1 && cell(0).begin()->nucleus()
&& cell(0).begin()->nucleus()->asMatrixInset();
if (mat)
os << "Det" << cell(0) << ']';
else
os << "Abs[" << cell(0) << ']';
}
else
os << left_ << cell(0) << right_;
}
void MathDelimInset::mathmlize(MathMLStream & os) const
{

View File

@ -43,6 +43,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void octavize(OctaveStream &) const;

View File

@ -55,6 +55,17 @@ void MathDiffInset::maplize(MapleStream & os) const
os << ')';
}
void MathDiffInset::mathematicize(MathematicaStream & os) const
{
os << "Dt[";
for (idx_type idx = 0; idx < nargs(); ++idx) {
if (idx != 0)
os << ',';
os << cell(idx);
}
os << ']';
}
void MathDiffInset::mathmlize(MathMLStream & os) const
{

View File

@ -25,6 +25,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void write(WriteStream & os) const;

View File

@ -51,6 +51,34 @@ void MathExFuncInset::maplize(MapleStream & os) const
}
string asMathematicaName(string const & name)
{
if (name == "sin") return "Sin";
if (name == "sinh") return "Sinh";
if (name == "arcsin") return "ArcSin";
if (name == "cos") return "Cos";
if (name == "cosh") return "Cosh";
if (name == "arcos") return "ArcCos";
if (name == "tan") return "Tan";
if (name == "tanh") return "Tanh";
if (name == "arctan") return "ArcTan";
if (name == "cot") return "Cot";
if (name == "coth") return "Coth";
if (name == "csc") return "Csc";
if (name == "sec") return "Sec";
if (name == "exp") return "Exp";
if (name == "log") return "Log";
if (name == "ln" ) return "Log";
return name;
}
void MathExFuncInset::mathematicize(MathematicaStream & os) const
{
os << asMathematicaName(name_) << '[' << cell(0) << ']';
}
void MathExFuncInset::mathmlize(MathMLStream & os) const
{
os << MTag(name_.c_str()) << cell(0) << ETag(name_.c_str());

View File

@ -25,6 +25,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void octavize(OctaveStream &) const;

View File

@ -74,6 +74,26 @@ void MathExIntInset::maplize(MapleStream & os) const
os << ')';
}
void MathExIntInset::mathematicize(MathematicaStream & os) const
{
if ( symbol_ == "int" )
os << "Integrate[";
else
if (symbol_ == "sum")
os << "Sum[";
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::mathmlize(MathMLStream & os) const
{

View File

@ -26,6 +26,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void write(WriteStream & os) const;

View File

@ -844,6 +844,24 @@ void maplize(MathArray const & dat, MapleStream & os)
}
void mathematicize(MathArray const & dat, MathematicaStream & os)
{
MathArray ar = dat;
extractStructure(ar);
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus();
if (it + 1 != ar.end()) {
if (MathScriptInset const * q = asScript(it)) {
q->mathematicize2(p, os);
++it;
continue;
}
}
p->mathematicize(os);
}
}
void mathmlize(MathArray const & dat, MathMLStream & os)
{
MathArray ar = dat;

View File

@ -5,6 +5,7 @@
class NormalStream;
class MapleStream;
class MathematicaStream;
class MathMLStream;
class OctaveStream;
class WriteStream;
@ -13,6 +14,7 @@ class MathArray;
void write(MathArray const &, WriteStream &);
void normalize(MathArray const &, NormalStream &);
void maplize(MathArray const &, MapleStream &);
void mathematicize(MathArray const &, MathematicaStream &);
void mathmlize(MathArray const &, MathMLStream &);
void octavize(MathArray const &, OctaveStream &);

View File

@ -89,12 +89,15 @@ void MathFracInset::normalize(NormalStream & os) const
os << cell(0) << ' ' << cell(1) << ']';
}
void MathFracInset::maplize(MapleStream & os) const
{
os << '(' << cell(0) << ")/(" << cell(1) << ')';
}
void MathFracInset::mathematicize(MathematicaStream & os) const
{
os << '(' << cell(0) << ")/(" << cell(1) << ')';
}
void MathFracInset::octavize(OctaveStream & os) const
{

View File

@ -35,6 +35,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void octavize(OctaveStream &) const;
///
void mathmlize(MathMLStream &) const;

View File

@ -240,6 +240,13 @@ void MathInset::maplize(MapleStream & os) const
}
void MathInset::mathematicize(MathematicaStream & os) const
{
NormalStream ns(os.os());
normalize(ns);
}
void MathInset::mathmlize(MathMLStream & os) const
{
NormalStream ns(os.os());

View File

@ -71,6 +71,7 @@ class MathXYMatrixInset;
class NormalStream;
class OctaveStream;
class MapleStream;
class MathematicaStream;
class MathMLStream;
class WriteStream;
class InfoStream;
@ -251,6 +252,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 Mathematica
virtual void mathematicize(MathematicaStream &) const;
/// write content as something resembling MathML
virtual void mathmlize(MathMLStream &) const;
/// write content as something readable by Octave

View File

@ -116,6 +116,48 @@ MapleStream & operator<<(MapleStream & ms, int i)
}
//////////////////////////////////////////////////////////////////////
MathematicaStream & operator<<(MathematicaStream & ms, MathInset const * p)
{
if (p)
p->mathematicize(ms);
else
lyxerr << "operator<<(MathematicaStream, NULL) called\n";
return ms;
}
MathematicaStream & operator<<(MathematicaStream & ms, MathArray const & ar)
{
mathematicize(ar, ms);
return ms;
}
MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
{
ms.os() << s;
return ms;
}
MathematicaStream & operator<<(MathematicaStream & ms, char c)
{
ms.os() << c;
return ms;
}
MathematicaStream & operator<<(MathematicaStream & ms, int i)
{
ms.os() << i;
return ms;
}
//////////////////////////////////////////////////////////////////////

View File

@ -126,6 +126,35 @@ MapleStream & operator<<(MapleStream &, char);
MapleStream & operator<<(MapleStream &, int);
//
// Mathematica
//
class MathematicaStream {
public:
///
explicit MathematicaStream(std::ostream & os) : os_(os) {}
///
std::ostream & os() { return os_; }
private:
///
std::ostream & os_;
};
///
MathematicaStream & operator<<(MathematicaStream &, MathInset const *);
///
MathematicaStream & operator<<(MathematicaStream &, MathArray const &);
///
MathematicaStream & operator<<(MathematicaStream &, char const *);
///
MathematicaStream & operator<<(MathematicaStream &, char);
///
MathematicaStream & operator<<(MathematicaStream &, int);
//
// Octave
//

View File

@ -409,6 +409,25 @@ void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
}
void MathScriptInset::mathematicize2(MathInset const * nuc, MathematicaStream & os) const
{
bool d = hasDown() && down().data_.size();
bool u = hasUp() && up().data_.size();
if (nuc)
if (d) //subscript only if nuc !
os << "Subscript[" << nuc;
else
os << nuc;
if (u)
os << "^(" << up().data_ << ")";
if (nuc)
if (d)
os << "," << down().data_ << "]";
}
void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
{
bool d = hasDown() && down().data_.size();

View File

@ -93,6 +93,7 @@ public:
virtual void normalize2(MathInset const * nuc, NormalStream & os) const;
virtual void octavize2(MathInset const * nuc, OctaveStream & os) const;
virtual void maplize2(MathInset const * nuc, MapleStream & os) const;
virtual void mathematicize2(MathInset const * nuc, MathematicaStream & os) const;
virtual void mathmlize2(MathInset const * nuc, MathMLStream & os) const;
public:

View File

@ -87,6 +87,11 @@ void MathSpaceInset::maplize(MapleStream & os) const
os << ' ';
}
void MathSpaceInset::mathematicize(MathematicaStream & os) const
{
os << ' ';
}
void MathSpaceInset::octavize(OctaveStream & os) const
{

View File

@ -33,6 +33,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void octavize(OctaveStream &) const;
///
void write(WriteStream & os) const;

View File

@ -73,12 +73,16 @@ void MathSqrtInset::normalize(NormalStream & os) const
os << "[sqrt " << cell(0) << ']';
}
void MathSqrtInset::maplize(MapleStream & os) const
{
os << "sqrt(" << cell(0) << ')';
}
void MathSqrtInset::mathematicize(MathematicaStream & os) const
{
os << "Sqrt[" << cell(0) << ']';
}
void MathSqrtInset::octavize(OctaveStream & os) const
{

View File

@ -33,6 +33,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void octavize(OctaveStream &) const;
///
void mathmlize(MathMLStream &) const;

View File

@ -28,6 +28,13 @@ MapleStream & operator<<(MapleStream & ms, string const & s)
}
MathematicaStream & operator<<(MathematicaStream & ms, string const & s)
{
ms.os() << s;
return ms;
}
MathMLStream & operator<<(MathMLStream & ms, string const & s)
{
ms.os() << s;

View File

@ -7,6 +7,7 @@
class WriteStream;
class NormalStream;
class MapleStream;
class MathematicaStream;
class MathMLStream;
class OctaveStream;
@ -17,6 +18,7 @@ class OctaveStream;
WriteStream & operator<<(WriteStream & ws, string const & s);
NormalStream & operator<<(NormalStream & ns, string const & s);
MapleStream & operator<<(MapleStream & 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);
#endif

View File

@ -46,7 +46,7 @@ void MathStringInset::normalize(NormalStream & os) const
void MathStringInset::maplize(MapleStream & os) const
{
if (/*code_ != LM_TC_VAR || */ str_.size() <= 1) {
if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
os << ' ' << str_ << ' ';
return;
}
@ -58,6 +58,12 @@ void MathStringInset::maplize(MapleStream & os) const
}
void MathStringInset::mathematicize(MathematicaStream & os) const
{
os << ' ' << str_ << ' ';
}
void MathStringInset::octavize(OctaveStream & os) const
{
if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {

View File

@ -35,6 +35,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void write(WriteStream & os) const;

View File

@ -539,19 +539,20 @@ LyXFont::FONT_SERIES const inh_series = LyXFont::INHERIT_SERIES;
LyXFont::FONT_SHAPE const inh_shape = LyXFont::INHERIT_SHAPE;
// mathnormal should be the first, otherwise the fallback fuerther down
// does not work
fontinfo fontinfos[] = {
{"mathnormal", inh_family,inh_series, LyXFont::UP_SHAPE, LColor::math},
{"mathbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
{"mathcal",LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
{"mathfrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
{"mathrm", LyXFont::ROMAN_FAMILY, inh_series, inh_shape, LColor::math},
{"mathsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::math},
{"cmex", LyXFont::CMEX_FAMILY, inh_series, inh_shape, LColor::math},
{"cmm", LyXFont::CMM_FAMILY, inh_series, inh_shape, LColor::math},
{"cmr", LyXFont::CMR_FAMILY, inh_series, inh_shape, LColor::math},
{"cmsy", LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
{"eufrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
{"mathbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
{"mathcal",LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
{"mathfrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
{"mathnormal", inh_family,inh_series, LyXFont::UP_SHAPE, LColor::math},
{"mathrm", LyXFont::ROMAN_FAMILY, inh_series, inh_shape, LColor::math},
{"mathsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::math},
{"msa", LyXFont::MSA_FAMILY, inh_series, inh_shape, LColor::math},
{"msb", LyXFont::MSB_FAMILY, inh_series, inh_shape, LColor::math},
{"textbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::black},
@ -593,7 +594,9 @@ fontinfo * searchFont(string const & name)
//lyxerr << "found '" << i << "'\n";
return fontinfos + i;
}
return searchFont("mathnormal");
// this should be mathnormal
return fontinfos;
//return searchFont("mathnormal");
}
@ -619,6 +622,7 @@ void fakeFont(string const & orig, string const & fake)
}
}
void augmentFont(LyXFont & font, string const & name)
{
static bool initialized = false;

View File

@ -121,6 +121,13 @@ void MathSymbolInset::maplize(MapleStream & os) const
os << name();
}
void MathSymbolInset::mathematicize(MathematicaStream & os) const
{
if ( name() == "pi") { os << "Pi"; return;}
if ( name() == "infty") { os << "Infinity"; return;}
os << name();
}
char const * MathMLtype(string const & s)
{

View File

@ -43,6 +43,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void octavize(OctaveStream &) const;

View File

@ -84,6 +84,11 @@ void MathUnknownInset::maplize(MapleStream & os) const
os << name_;
}
void MathUnknownInset::mathematicize(MathematicaStream & os) const
{
os << name_;
}
void MathUnknownInset::mathmlize(MathMLStream & os) const
{

View File

@ -38,6 +38,8 @@ public:
///
void maplize(MapleStream &) const;
///
void mathematicize(MathematicaStream &) const;
///
void mathmlize(MathMLStream &) const;
///
void octavize(OctaveStream &) const;