commit c8491fa069f85c73e9957ba60df5a941239ce496
Author: Enrico Forestieri <[email protected]>
Date:   Thu May 1 12:21:19 2025 +0200

    Improve input for maxima
    
    Maxima and Maple share many constructs and it is probably for
    this reason that sometimes a MapleStream is used instead of a
    MaximaStream. However this has some unfortunate consequences
    because in some cases the output from maxima would depend on
    the particular way an input is built. For example, using maxima,
    $\sin(\pi)/1=0$ but $\frac{\sin(\pi)}{1}=\sin\pi$. See also the
    example file attached to #13178 for other more complex examples.
    
    By adding a boolean member to MapleStream we can discriminate
    when the input is meant for Maxima, instead, and act consequently.
---
 src/mathed/InsetMath.cpp       |  2 +-
 src/mathed/InsetMathDelim.cpp  |  2 +-
 src/mathed/InsetMathDiff.cpp   |  2 ++
 src/mathed/InsetMathExFunc.cpp |  2 +-
 src/mathed/InsetMathExInt.cpp  | 15 +++++++++++----
 src/mathed/InsetMathMatrix.cpp | 25 ++++++++++++++++++++-----
 src/mathed/InsetMathSymbol.cpp |  4 +++-
 src/mathed/MathStream.h        |  5 ++++-
 8 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/src/mathed/InsetMath.cpp b/src/mathed/InsetMath.cpp
index 5dee0c31ba..00d411d857 100644
--- a/src/mathed/InsetMath.cpp
+++ b/src/mathed/InsetMath.cpp
@@ -190,7 +190,7 @@ void InsetMath::maple(MapleStream & os) const
 
 void InsetMath::maxima(MaximaStream & os) const
 {
-       MapleStream ns(os.os());
+       MapleStream ns(os.os(), true);
        maple(ns);
 }
 
diff --git a/src/mathed/InsetMathDelim.cpp b/src/mathed/InsetMathDelim.cpp
index f81b8e1f16..95fcf2a19b 100644
--- a/src/mathed/InsetMathDelim.cpp
+++ b/src/mathed/InsetMathDelim.cpp
@@ -153,7 +153,7 @@ void InsetMathDelim::maple(MapleStream & os) const
 {
        if (isAbs()) {
                if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
-                       os << "linalg[det](" << cell(0) << ')';
+                       os << (os.maxima() ? "determinant(" : "linalg[det](") 
<< cell(0) << ')';
                else
                        os << "abs(" << cell(0) << ')';
        }
diff --git a/src/mathed/InsetMathDiff.cpp b/src/mathed/InsetMathDiff.cpp
index ffc3bedb4e..cdcac8a164 100644
--- a/src/mathed/InsetMathDiff.cpp
+++ b/src/mathed/InsetMathDiff.cpp
@@ -64,6 +64,8 @@ void InsetMathDiff::maple(MapleStream & os) const
                if (idx != 0)
                        os << ',';
                os << cell(idx);
+               if (idx != 0 && os.maxima())
+                       os << ",1";
        }
        os << ')';
 }
diff --git a/src/mathed/InsetMathExFunc.cpp b/src/mathed/InsetMathExFunc.cpp
index d3314380c0..126d6adec4 100644
--- a/src/mathed/InsetMathExFunc.cpp
+++ b/src/mathed/InsetMathExFunc.cpp
@@ -63,7 +63,7 @@ docstring InsetMathExFunc::name() const
 void InsetMathExFunc::maple(MapleStream & os) const
 {
        if (name_ == "det")
-               os << "linalg[det](" << cell(0) << ')';
+               os << (os.maxima() ? "determinant(" : "linalg[det](") << 
cell(0) << ')';
        else
                os << name_ << '(' << cell(0) << ')';
 }
diff --git a/src/mathed/InsetMathExInt.cpp b/src/mathed/InsetMathExInt.cpp
index 7217bc3e37..8e9e641302 100644
--- a/src/mathed/InsetMathExInt.cpp
+++ b/src/mathed/InsetMathExInt.cpp
@@ -75,7 +75,7 @@ void InsetMathExInt::draw(PainterInfo &, int, int) const
 void InsetMathExInt::maple(MapleStream & os) const
 {
        if (symbol_ == "int" || symbol_ == "intop")
-               os << "int(";
+               os << (os.maxima() ? "integrate(" : "int(");
        else
                os << symbol_ << '(';
 
@@ -84,9 +84,16 @@ void InsetMathExInt::maple(MapleStream & os) const
        else
                os << '1';
        os << ',' << cell(1);
-       if (hasScripts())
-               os << '=' << cell(2) << ".." << cell(3);
-       os << ')';
+       if (os.maxima()) {
+               if (hasScripts())
+                       os << cell(1) << ',' << cell(2) << ',' << cell(3) << 
')';
+               else
+                       os << cell(1) << ')';
+       } else {
+               if (hasScripts())
+                       os << '=' << cell(2) << ".." << cell(3);
+               os << ')';
+       }
 }
 
 
diff --git a/src/mathed/InsetMathMatrix.cpp b/src/mathed/InsetMathMatrix.cpp
index 54a68bd558..fd7b368186 100644
--- a/src/mathed/InsetMathMatrix.cpp
+++ b/src/mathed/InsetMathMatrix.cpp
@@ -46,11 +46,26 @@ void InsetMathMatrix::normalize(NormalStream & os) const
 
 void InsetMathMatrix::maple(MapleStream & os) const
 {
-       os << "matrix(" << int(nrows()) << ',' << int(ncols()) << ",[";
-       for (idx_type idx = 0; idx < nargs(); ++idx) {
-               if (idx)
-                       os << ',';
-               os << cell(idx);
+       if (os.maxima()) {
+               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 << ']';
+               }
+       } else {
+               os << "matrix(" << int(nrows()) << ',' << int(ncols()) << ",[";
+               for (idx_type idx = 0; idx < nargs(); ++idx) {
+                       if (idx)
+                               os << ',';
+                       os << cell(idx);
+               }
        }
        os << "])";
 }
diff --git a/src/mathed/InsetMathSymbol.cpp b/src/mathed/InsetMathSymbol.cpp
index 108156f27d..d32ea41c77 100644
--- a/src/mathed/InsetMathSymbol.cpp
+++ b/src/mathed/InsetMathSymbol.cpp
@@ -128,7 +128,9 @@ void InsetMathSymbol::maple(MapleStream & os) const
        if (name() == "cdot")
                os << '*';
        else if (name() == "infty")
-               os << "infinity";
+               os << (os.maxima() ? "inf" : "infinity");
+       else if (name() == "pi" && os.maxima())
+               os << "%pi";
        else
                os << name();
 }
diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h
index b2f538ad6e..daddfe9f28 100644
--- a/src/mathed/MathStream.h
+++ b/src/mathed/MathStream.h
@@ -710,12 +710,15 @@ NormalStream & operator<<(NormalStream &, int);
 class MapleStream {
 public:
        ///
-       explicit MapleStream(odocstream & os) : os_(os) {}
+       explicit MapleStream(odocstream & os, bool maxima = false) : os_(os), 
maxima_(maxima) {}
        ///
        odocstream & os() { return os_; }
+       bool maxima() { return maxima_; }
 private:
        ///
        odocstream & os_;
+       ///
+       bool maxima_;
 };
 
 
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to