diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index b6b3c4cc09..d40bf7117e 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -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 * preview.h (preview): don't pass grfx::GraphicPtr & anymore. diff --git a/src/mathed/math_deliminset.C b/src/mathed/math_deliminset.C index 7ec7c44fde..2a318c3179 100644 --- a/src/mathed/math_deliminset.C +++ b/src/mathed/math_deliminset.C @@ -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 { diff --git a/src/mathed/math_deliminset.h b/src/mathed/math_deliminset.h index d2c0d4d5ea..7835b95f07 100644 --- a/src/mathed/math_deliminset.h +++ b/src/mathed/math_deliminset.h @@ -43,6 +43,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void octavize(OctaveStream &) const; diff --git a/src/mathed/math_diffinset.C b/src/mathed/math_diffinset.C index 75d3852d19..bf30b4a7ce 100644 --- a/src/mathed/math_diffinset.C +++ b/src/mathed/math_diffinset.C @@ -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 { diff --git a/src/mathed/math_diffinset.h b/src/mathed/math_diffinset.h index 284c822a2e..a96388f776 100644 --- a/src/mathed/math_diffinset.h +++ b/src/mathed/math_diffinset.h @@ -25,6 +25,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void write(WriteStream & os) const; diff --git a/src/mathed/math_exfuncinset.C b/src/mathed/math_exfuncinset.C index 7e0b0fd91b..667b4461e6 100644 --- a/src/mathed/math_exfuncinset.C +++ b/src/mathed/math_exfuncinset.C @@ -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()); diff --git a/src/mathed/math_exfuncinset.h b/src/mathed/math_exfuncinset.h index afc15927c9..45692fe81c 100644 --- a/src/mathed/math_exfuncinset.h +++ b/src/mathed/math_exfuncinset.h @@ -25,6 +25,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void octavize(OctaveStream &) const; diff --git a/src/mathed/math_exintinset.C b/src/mathed/math_exintinset.C index e26a74e6b6..47a44edde6 100644 --- a/src/mathed/math_exintinset.C +++ b/src/mathed/math_exintinset.C @@ -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 { diff --git a/src/mathed/math_exintinset.h b/src/mathed/math_exintinset.h index 5ef2642c9d..7c3866df1d 100644 --- a/src/mathed/math_exintinset.h +++ b/src/mathed/math_exintinset.h @@ -26,6 +26,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void write(WriteStream & os) const; diff --git a/src/mathed/math_extern.C b/src/mathed/math_extern.C index f595b1b168..a8fc186a10 100644 --- a/src/mathed/math_extern.C +++ b/src/mathed/math_extern.C @@ -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; diff --git a/src/mathed/math_extern.h b/src/mathed/math_extern.h index 3747827375..933129c1a6 100644 --- a/src/mathed/math_extern.h +++ b/src/mathed/math_extern.h @@ -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 &); diff --git a/src/mathed/math_fracinset.C b/src/mathed/math_fracinset.C index 119c278774..092c5d19c1 100644 --- a/src/mathed/math_fracinset.C +++ b/src/mathed/math_fracinset.C @@ -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 { diff --git a/src/mathed/math_fracinset.h b/src/mathed/math_fracinset.h index 4f903d7c8b..63fd4b72f9 100644 --- a/src/mathed/math_fracinset.h +++ b/src/mathed/math_fracinset.h @@ -35,6 +35,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void octavize(OctaveStream &) const; /// void mathmlize(MathMLStream &) const; diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index 044ef6c1a0..5260a4c7df 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -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()); diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index 09a02384c1..e3fbf90a3f 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -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 diff --git a/src/mathed/math_mathmlstream.C b/src/mathed/math_mathmlstream.C index 13a105c109..7d5f9a9e3a 100644 --- a/src/mathed/math_mathmlstream.C +++ b/src/mathed/math_mathmlstream.C @@ -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; +} + + + ////////////////////////////////////////////////////////////////////// diff --git a/src/mathed/math_mathmlstream.h b/src/mathed/math_mathmlstream.h index 744156946e..715e08c88f 100644 --- a/src/mathed/math_mathmlstream.h +++ b/src/mathed/math_mathmlstream.h @@ -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 // diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index 47cb5deec8..c1cb90af0b 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -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(); diff --git a/src/mathed/math_scriptinset.h b/src/mathed/math_scriptinset.h index 6087d537ec..d87bebd1f2 100644 --- a/src/mathed/math_scriptinset.h +++ b/src/mathed/math_scriptinset.h @@ -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: diff --git a/src/mathed/math_spaceinset.C b/src/mathed/math_spaceinset.C index 9bd15cbd13..641549513e 100644 --- a/src/mathed/math_spaceinset.C +++ b/src/mathed/math_spaceinset.C @@ -87,6 +87,11 @@ void MathSpaceInset::maplize(MapleStream & os) const os << ' '; } +void MathSpaceInset::mathematicize(MathematicaStream & os) const +{ + os << ' '; +} + void MathSpaceInset::octavize(OctaveStream & os) const { diff --git a/src/mathed/math_spaceinset.h b/src/mathed/math_spaceinset.h index f25d049a04..7c16d969b2 100644 --- a/src/mathed/math_spaceinset.h +++ b/src/mathed/math_spaceinset.h @@ -33,6 +33,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void octavize(OctaveStream &) const; /// void write(WriteStream & os) const; diff --git a/src/mathed/math_sqrtinset.C b/src/mathed/math_sqrtinset.C index 02c3a780bc..4341d059b9 100644 --- a/src/mathed/math_sqrtinset.C +++ b/src/mathed/math_sqrtinset.C @@ -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 { diff --git a/src/mathed/math_sqrtinset.h b/src/mathed/math_sqrtinset.h index 9ce0c237a5..258d713ba7 100644 --- a/src/mathed/math_sqrtinset.h +++ b/src/mathed/math_sqrtinset.h @@ -33,6 +33,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void octavize(OctaveStream &) const; /// void mathmlize(MathMLStream &) const; diff --git a/src/mathed/math_streamstr.C b/src/mathed/math_streamstr.C index dae93991fc..b499e21c13 100644 --- a/src/mathed/math_streamstr.C +++ b/src/mathed/math_streamstr.C @@ -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; diff --git a/src/mathed/math_streamstr.h b/src/mathed/math_streamstr.h index 3238106e04..065c7c8c6a 100644 --- a/src/mathed/math_streamstr.h +++ b/src/mathed/math_streamstr.h @@ -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 diff --git a/src/mathed/math_stringinset.C b/src/mathed/math_stringinset.C index 2708266302..078ec542cb 100644 --- a/src/mathed/math_stringinset.C +++ b/src/mathed/math_stringinset.C @@ -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) { diff --git a/src/mathed/math_stringinset.h b/src/mathed/math_stringinset.h index e627bea1d9..f81d89b07a 100644 --- a/src/mathed/math_stringinset.h +++ b/src/mathed/math_stringinset.h @@ -35,6 +35,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void write(WriteStream & os) const; diff --git a/src/mathed/math_support.C b/src/mathed/math_support.C index cec237a5de..878f7d6871 100644 --- a/src/mathed/math_support.C +++ b/src/mathed/math_support.C @@ -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; diff --git a/src/mathed/math_symbolinset.C b/src/mathed/math_symbolinset.C index bdcfdb8a1d..761cebfa65 100644 --- a/src/mathed/math_symbolinset.C +++ b/src/mathed/math_symbolinset.C @@ -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) { diff --git a/src/mathed/math_symbolinset.h b/src/mathed/math_symbolinset.h index 2f7e0a0af9..7316332726 100644 --- a/src/mathed/math_symbolinset.h +++ b/src/mathed/math_symbolinset.h @@ -43,6 +43,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void octavize(OctaveStream &) const; diff --git a/src/mathed/math_unknowninset.C b/src/mathed/math_unknowninset.C index b7256c3d58..ab33efbb34 100644 --- a/src/mathed/math_unknowninset.C +++ b/src/mathed/math_unknowninset.C @@ -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 { diff --git a/src/mathed/math_unknowninset.h b/src/mathed/math_unknowninset.h index 2e9dd5b82b..36c26ba397 100644 --- a/src/mathed/math_unknowninset.h +++ b/src/mathed/math_unknowninset.h @@ -38,6 +38,8 @@ public: /// void maplize(MapleStream &) const; /// + void mathematicize(MathematicaStream &) const; + /// void mathmlize(MathMLStream &) const; /// void octavize(OctaveStream &) const;