]> git.lyx.org Git - features.git/commitdiff
parse octave output as matrix if necessary
authorAndré Pönitz <poenitz@gmx.net>
Wed, 7 Nov 2001 13:15:59 +0000 (13:15 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Wed, 7 Nov 2001 13:15:59 +0000 (13:15 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2974 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/Makefile.am
src/mathed/formula.C
src/mathed/math_arrayinset.C
src/mathed/math_arrayinset.h
src/mathed/math_exfuncinset.C [new file with mode: 0644]
src/mathed/math_exfuncinset.h [new file with mode: 0644]
src/mathed/math_factory.C
src/mathed/math_funcliminset.C
src/mathed/math_funcliminset.h
src/mathed/math_rootinset.C
src/mathed/math_rootinset.h

index 17977d84b4f28548eb0417ea036c1fd4bdae6e8f..081ea869186187a25cadfaef65c02befde1c0388 100644 (file)
@@ -43,6 +43,8 @@ libmathed_la_SOURCES = \
        math_diminset.h \
        math_dotsinset.C \
        math_dotsinset.h \
+       math_exfuncinset.C \
+       math_exfuncinset.h \
        math_factory.C \
        math_factory.h \
        math_fracinset.C \
index fcd4fec9924a53e4b887659a3467b6fa8e051d7e..a3cb00613fe9ff7acb32b93a9ec38222089c2822 100644 (file)
@@ -24,6 +24,8 @@
 #include "math_cursor.h"
 #include "math_parser.h"
 #include "math_charinset.h"
+#include "math_arrayinset.h"
+#include "math_deliminset.h"
 #include "lyx_main.h"
 #include "BufferView.h"
 #include "gettext.h"
@@ -63,7 +65,7 @@ namespace {
        }
 
 
-       string pipeThroughMaple(string const & extra, MathArray const & ar)
+       MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
        {
                string header = 
                        "readlib(latex):\n"
@@ -101,12 +103,35 @@ namespace {
                }
 
                string full = "latex(" +  extra + '(' + expr + "));";
-               string res = captureOutput("maple -q", header + full + trailer);
+               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)
+       {
+               string out = captureOutput("octave -q", ar.octavize());
+               if (out.size() > 6) // remove 'ans = '
+                       out = out.substr(6);
+
+               // parse output as matrix or single number
+               MathAtom at(new MathArrayInset(out));
+               MathArrayInset const * mat = at.nucleus()->asArrayInset();
+               MathArray res;
+               if (mat->ncols() == 1 && mat->nrows() == 1)
+                       res.push_back(mat->cell(0));
+               else {
+                       res.push_back(MathAtom(new MathDelimInset("(", ")")));
+                       res.back()->cell(0).push_back(at);
+               }
+               return res;
+       }
 
 
        MathArray pipeThroughExtern(string const & arg, MathArray const & ar)
@@ -118,40 +143,30 @@ namespace {
                if (extra.empty())
                        extra = "noextra";      
 
-               MathArray res;
-
-               if (lang == "octave") {
-
-                       string out = captureOutput("octave -q", ar.octavize());
-                       if (out.size() > 6)
-                               out = out.substr(6);
-                       mathed_parse_cell(res, out);
-
-               } else if (lang == "maple") {
+               if (lang == "octave")
+                       return pipeThroughOctave(extra, ar);
 
-                       mathed_parse_cell(res, pipeThroughMaple(extra, ar));
+               if (lang == "maple")
+                       return pipeThroughMaple(extra, ar);
 
-               } else {
+               // create normalized expression
+               ostringstream os;
+               os << "[" << extra << ' ';
+               ar.writeNormal(os); 
+               os << "]";
+               string data = os.str().c_str();
 
-                       // create normalized expression
-                       ostringstream os;
-                       os << "[" << extra << ' ';
-                       ar.writeNormal(os); 
-                       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);
-                       mathed_parse_cell(res, out);
+               // 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;
        }
 
@@ -425,6 +440,7 @@ void InsetFormula::handleExtern(const string & arg)
                mathcursor->selGet(ar);
                lyxerr << "use selection: " << ar << "\n";
        } else {
+               mathcursor->end();
                mathcursor->stripFromLastEqualSign();
                ar = mathcursor->cursor().cell();
                mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
index 4cd48211ec1f3a13b88123791236cf19463fabc1..b2a3fbee52b73e5e2f95cc424dc35716fb961c91 100644 (file)
@@ -3,7 +3,9 @@
 #endif
 
 #include "math_arrayinset.h"
+#include "math_parser.h"
 #include "support/LOstream.h"
+#include "Lsstream.h"
 
 
 MathArrayInset::MathArrayInset(int m, int n)
@@ -16,6 +18,31 @@ MathArrayInset::MathArrayInset(int m, int n, char valign, string const & halign)
 {}
 
 
+MathArrayInset::MathArrayInset(string const & str)
+       : MathGridInset(1, 1)
+{
+       vector< vector<string> > dat;
+       istringstream is(str);
+       while (is) {
+               string line;
+               getline(is, line);
+               istringstream ls(line);
+               typedef std::istream_iterator<string> iter;
+               vector<string> v = vector<string>(iter(ls), iter());
+               if (v.size())
+                       dat.push_back(v);
+       }
+
+       for (row_type row = 1; row < dat.size(); ++row)
+               addRow(0);
+       for (col_type col = 1; col < dat[0].size(); ++col)
+               addCol(0);
+       for (row_type row = 0; row < dat.size(); ++row)
+               for (col_type col = 0; col < dat[row].size(); ++col)
+                       mathed_parse_cell(cell(index(row, col)), dat[row][col]);
+}
+
+
 MathInset * MathArrayInset::clone() const
 {
        return new MathArrayInset(*this);
index e80acf6d0a8a33447741f9e99af77ffcf8d640cc..13f2ee5736548020ad6d0773904ab553790b4d1c 100644 (file)
@@ -15,6 +15,8 @@ public:
        MathArrayInset(int m, int n);
        ///
        MathArrayInset(int m, int n, char valign, string const & halign);
+       /// convienience constructor from whitespace/newline seperated data
+       MathArrayInset(string const & str);
        ///
        MathInset * clone() const;
        ///
diff --git a/src/mathed/math_exfuncinset.C b/src/mathed/math_exfuncinset.C
new file mode 100644 (file)
index 0000000..162792e
--- /dev/null
@@ -0,0 +1,55 @@
+#include "math_exfuncinset.h"
+#include "support.h"
+#include "debug.h"
+#include "support/LOstream.h"
+
+
+using std::ostream;
+
+
+MathExFuncInset::MathExFuncInset(string const & name)
+       : MathNestInset(1), name_(name)
+{}
+
+
+MathInset * MathExFuncInset::clone() const
+{
+       return new MathExFuncInset(*this);
+}
+
+
+void MathExFuncInset::write(MathWriteInfo & os) const
+{
+       os << '\\' << name_ << '{';
+       cell(0).write(os);
+       os << '}';
+}
+
+
+void MathExFuncInset::writeNormal(ostream & os) const
+{
+       os << "[" << name_ << ' ';
+       cell(0).writeNormal(os);
+       os << "] ";
+}
+
+
+void MathExFuncInset::metrics(MathMetricsInfo const & mi) const
+{
+       mi_ = mi;
+       mathed_string_dim(LM_TC_TEXTRM, mi_, name_, ascent_, descent_, width_);
+       lyxerr << "should not happen\n";
+}
+
+
+void MathExFuncInset::draw(Painter & pain, int x, int y) const
+{  
+       drawStr(pain, LM_TC_TEXTRM, mi_, x, y, name_);
+       lyxerr << "should not happen\n";
+}
+
+
+string MathExFuncInset::octavize() const
+{
+       return name_ + '(' + cell(0).octavize() + ')';
+}
diff --git a/src/mathed/math_exfuncinset.h b/src/mathed/math_exfuncinset.h
new file mode 100644 (file)
index 0000000..3f434f6
--- /dev/null
@@ -0,0 +1,33 @@
+// -*- C++ -*-
+#ifndef MATH_EXFUNCINSET_H
+#define MATH_EXFUNCINSET_H
+
+#include "math_nestinset.h"
+
+// f(x) in one block (as opposed to 'f','(','x',')' or 'f','x')
+// for interfacing external programs
+
+class MathExFuncInset : public MathNestInset {
+public:
+       ///
+       explicit MathExFuncInset(string const & name);
+       ///
+       MathInset * clone() const;
+       ///
+       void write(MathWriteInfo & os) const;
+       ///
+       void writeNormal(std::ostream &) const;
+       ///
+       void metrics(MathMetricsInfo const & st) const;
+       ///
+       void draw(Painter &, int x, int y) const;
+       ///
+       string octavize() const;
+
+private:
+       ///
+       string const name_;
+       ///
+       mutable MathMetricsInfo mi_;
+};
+#endif
index 10f0082e764158594b56f65a7d7f48a95584d6d6..a23ea6e197b4685b27482ec9b25ed1fc0d02439e 100644 (file)
@@ -26,7 +26,7 @@ MathAtom createMathInset(latexkeys const * l)
 {
        switch (l->token) {
        case LM_TK_FUNCLIM:
-               return MathAtom(new MathFuncLimInset(l));
+               return MathAtom(new MathFuncLimInset(l->name));
        case LM_TK_SPECIAL:
                return MathAtom(new MathSpecialCharInset(l->id));
        case LM_TK_SYM:
index 5af941b386eabf61ea9a57ede571f7708a39878e..472b6e0807279267cc3379f9d313a66ba3ac61b6 100644 (file)
@@ -1,13 +1,12 @@
 #include "math_funcliminset.h"
-#include "mathed/math_parser.h"
-#include "mathed/support.h"
+#include "support.h"
 #include "support/LOstream.h"
 
 
 using std::ostream;
 
-MathFuncLimInset::MathFuncLimInset(const latexkeys * l)
-       : sym_(l)
+MathFuncLimInset::MathFuncLimInset(string const & name)
+       : name_(name)
 {}
 
 
@@ -25,24 +24,24 @@ bool MathFuncLimInset::isScriptable() const
 
 void MathFuncLimInset::write(MathWriteInfo & os) const
 {
-       os << '\\' << sym_->name << ' ';
+       os << '\\' << name_ << ' ';
 }
 
 
 void MathFuncLimInset::writeNormal(ostream & os) const
 {
-       os << "[" << sym_->name << "] ";
+       os << "[" << name_ << "] ";
 }
 
 
 void MathFuncLimInset::metrics(MathMetricsInfo const & mi) const
 {
        mi_ = mi;
-       mathed_string_dim(LM_TC_TEXTRM, mi_, sym_->name, ascent_, descent_, width_);
+       mathed_string_dim(LM_TC_TEXTRM, mi_, name_, ascent_, descent_, width_);
 }
 
 
 void MathFuncLimInset::draw(Painter & pain, int x, int y) const
 {  
-       drawStr(pain, LM_TC_TEXTRM, mi_, x, y, sym_->name);
+       drawStr(pain, LM_TC_TEXTRM, mi_, x, y, name_);
 }
index 0d4e8dcc8517d996754e3448812cdd20c8c367b6..86736136ac306faa9275b551303a23a2a61ebda1 100644 (file)
@@ -4,15 +4,13 @@
 
 #include "math_diminset.h"
 
-struct latexkeys;
-
 // "normal" symbols that don't take limits and don't grow in displayed
 // formulae
 
 class MathFuncLimInset : public MathDimInset {
 public:
        ///
-       explicit MathFuncLimInset(latexkeys const *);
+       explicit MathFuncLimInset(string const & name);
        ///
        MathInset * clone() const;
        ///
@@ -28,7 +26,7 @@ public:
 
 private:
        ///
-       latexkeys const * sym_;
+       string const name_;
        ///
        mutable MathMetricsInfo mi_;
 };
index d7dbad49431ce5a1034326669a5eb5e081d4d362..cc143bef377f4247350e8a8cd1049eb7f7d47f11 100644 (file)
@@ -92,3 +92,9 @@ bool MathRootInset::idxDown(int & idx, int & pos) const
        pos = 0;
        return true;
 }
+
+
+string MathRootInset::octavize() const
+{
+       return "root(" + cell(1).octavize() + ',' + cell(0).octavize() + ')';
+}
index 95174881c759533b815f0b6214b3c6f9adc5063a..e85e074c2ed6ad212891e1eb287f5a669cfe519d 100644 (file)
@@ -43,6 +43,8 @@ public:
        bool idxUp(int & idx, int & pos) const;
        ///
        bool idxDown(int & idx, int & pos) const;
+       ///
+       string octavize() const;
 };
 
 #endif