File indexing completed on 2024-05-19 11:21:23

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009-2012 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "maximaextensions.h"
0007 #include <QStringList>
0008 #include <KLocalizedString>
0009 
0010 #define MAXIMA_EXTENSION_CONSTRUCTORS(name) Maxima##name##Extension::Maxima##name##Extension(QObject* parent) : name##Extension(parent) {} \
0011                                      Maxima##name##Extension::~Maxima##name##Extension() {}
0012 
0013 //History Extension
0014 MAXIMA_EXTENSION_CONSTRUCTORS(History)
0015 
0016 QString MaximaHistoryExtension::lastResult()
0017 {
0018     return QLatin1String("%");
0019 }
0020 
0021 //Script
0022 MAXIMA_EXTENSION_CONSTRUCTORS(Script)
0023 
0024 QString MaximaScriptExtension::runExternalScript(const QString& file)
0025 {
0026     return QString::fromLatin1("batch(\"%1\")$").arg(file);
0027 }
0028 
0029 QString MaximaScriptExtension::scriptFileFilter()
0030 {
0031     return i18n("Maxima batch file (*.mac)");
0032 }
0033 
0034 QString MaximaScriptExtension::highlightingMode()
0035 {
0036     return QLatin1String("maxima");
0037 }
0038 
0039 QString MaximaScriptExtension::commentStartingSequence()
0040 {
0041     return QLatin1String("/* ");
0042 }
0043 
0044 QString MaximaScriptExtension::commentEndingSequence()
0045 {
0046     return QLatin1String(" */");
0047 }
0048 
0049 //CAS Extension
0050 MAXIMA_EXTENSION_CONSTRUCTORS(CAS)
0051 
0052 QString MaximaCASExtension::solve(const QStringList& equations, const QStringList& variables)
0053 {
0054     QString eqstr=QString::fromLatin1("[%1]").arg(equations.join(QLatin1String(",")));
0055 
0056     QString variablestr=QString::fromLatin1("[%1]").arg(variables.join(QLatin1String(",")));
0057 
0058     return QString::fromLatin1("solve(%1,%2);").arg(eqstr, variablestr);
0059 }
0060 
0061 QString MaximaCASExtension::simplify(const QString& expression)
0062 {
0063     return QString::fromLatin1("simplify(%1);").arg(expression);
0064 }
0065 
0066 QString MaximaCASExtension::expand(const QString& expression)
0067 {
0068     return QString::fromLatin1("expand(%1);").arg(expression);
0069 }
0070 
0071 //Calculus Extension
0072 MAXIMA_EXTENSION_CONSTRUCTORS(Calculus)
0073 
0074 QString MaximaCalculusExtension::limit(const QString& expression, const QString& variable, const QString& limit)
0075 {
0076     return QString::fromLatin1("limit(%1, %2=%3);").arg(expression, variable, limit);
0077 }
0078 
0079 QString MaximaCalculusExtension::differentiate(const QString& function,const QString& variable, int times)
0080 {
0081     return QString::fromLatin1("diff(%1, %2, %3);").arg(function, variable, QString::number(times));
0082 }
0083 
0084 QString MaximaCalculusExtension::integrate(const QString& function, const QString& variable)
0085 {
0086     return QString::fromLatin1("integrate(%1, %2);").arg(function, variable);
0087 }
0088 
0089 QString MaximaCalculusExtension::integrate(const QString& function,const QString& variable, const QString& left, const QString& right)
0090 {
0091     return QString::fromLatin1("integrate(%1, %2, %3, %4);").arg(function, variable, left, right);
0092 }
0093 
0094 //Linear Algebra Extension
0095 MAXIMA_EXTENSION_CONSTRUCTORS(LinearAlgebra)
0096 
0097 //Commands to create Vectors/Matrices
0098 QString MaximaLinearAlgebraExtension::createVector(const QStringList& entries, VectorType type)
0099 {
0100     QString list=entries.join(QLatin1String(","));
0101 
0102     if(type==Cantor::LinearAlgebraExtension::ColumnVector)
0103         return QString::fromLatin1("columnvector([%1]);").arg(list);
0104     else
0105         return QString::fromLatin1("rowvector([%1]);").arg(list);
0106 }
0107 
0108 QString MaximaLinearAlgebraExtension::createMatrix(const Matrix& matrix)
0109 {
0110     QString cmd=QLatin1String("matrix(");
0111     for (const QStringList& row : matrix)
0112     {
0113         cmd+=QLatin1Char('[');
0114         for (const QString& entry : row)
0115             cmd+=entry+QLatin1Char(',');
0116         cmd.chop(1);
0117         cmd+=QLatin1String("],");
0118     }
0119     cmd.chop(1);
0120     cmd+=QLatin1String(");");
0121 
0122     return cmd;
0123 }
0124 
0125 QString MaximaLinearAlgebraExtension::identityMatrix(int size)
0126 {
0127     return QString::fromLatin1("ident(%1);").arg(size);
0128 }
0129 
0130 //basic functions
0131 QString MaximaLinearAlgebraExtension::rank(const QString& matrix)
0132 {
0133     return QString::fromLatin1("rank(%1);").arg(matrix);
0134 }
0135 
0136 QString MaximaLinearAlgebraExtension::invertMatrix(const QString& matrix)
0137 {
0138     return QString::fromLatin1("invert(%1);").arg(matrix);
0139 }
0140 
0141 QString MaximaLinearAlgebraExtension::charPoly(const QString& matrix)
0142 {
0143     return QString::fromLatin1("charpoly(%1,x);").arg(matrix);
0144 }
0145 
0146 QString MaximaLinearAlgebraExtension::eigenVectors(const QString& matrix)
0147 {
0148     return QString::fromLatin1("eigenvectors(%1);").arg(matrix);
0149 }
0150 
0151 QString MaximaLinearAlgebraExtension::eigenValues(const QString& matrix)
0152 {
0153     return QString::fromLatin1("eigenvalues(%1);").arg(matrix);
0154 }
0155 
0156 //Plotting
0157 MAXIMA_EXTENSION_CONSTRUCTORS(Plot)
0158 
0159 QString MaximaPlotExtension::plotFunction2d(const QString& function, const QString& variable, const QString& left, const QString& right)
0160 {
0161     return QString::fromLatin1("plot2d(%1,[%2,%3,%4])").arg(function, variable, left, right);
0162 }
0163 
0164 QString MaximaPlotExtension::plotFunction3d(const QString& function, const VariableParameter& var1, const VariableParameter& var2)
0165 {
0166     const Interval& int1=var1.second;
0167     const Interval& int2=var2.second;
0168     return QString::fromLatin1("plot3d(%1,[%2,%3,%4],[%6,%7,%8])").arg(function,
0169                                                            var1.first, int1.first, int1.second,
0170                                                            var2.first, int2.first, int2.second);
0171 }
0172 
0173 //Variable Management
0174 MAXIMA_EXTENSION_CONSTRUCTORS(VariableManagement)
0175 
0176 QString MaximaVariableManagementExtension::addVariable(const QString& name, const QString& value)
0177 {
0178     return QString::fromLatin1("%1: %2").arg(name).arg(value);
0179 }
0180 
0181 QString MaximaVariableManagementExtension::setValue(const QString& name,const QString& value)
0182 {
0183     return QString::fromLatin1("%1: %2").arg(name).arg(value);
0184 }
0185 
0186 QString MaximaVariableManagementExtension::removeVariable(const QString& name)
0187 {
0188     return QString::fromLatin1("kill(%1)").arg(name);
0189 }
0190 
0191 QString MaximaVariableManagementExtension::saveVariables(const QString& fileName)
0192 {
0193     return QString::fromLatin1("save(\"%1\", values,functions)").arg(fileName);
0194 }
0195 
0196 QString MaximaVariableManagementExtension::loadVariables(const QString& fileName)
0197 {
0198     return QString::fromLatin1("load(\"%1\")").arg(fileName);
0199 }
0200 
0201 QString MaximaVariableManagementExtension::clearVariables()
0202 {
0203     return QLatin1String("kill(all)");
0204 }