1 Pages (13 items)
[DEV] Add and remove definitions in the context - Messages
#1 Posted: 7/28/2017 7:05:44 PM
I am trying to solve the problem that I can't overwrite special functions in the Maxima plugin via the IPluginLowLevelEvaluation.ExpressionEvaluation interface, see SS-2401.
Obviously one can add definitions to the canvas from within a plugin function execution using something like this:
This replaces the built-in definition of int(4) with the one provided by the maxima plugin in the current downstream canvas.
The above solution was found by blindly poking around, assisted by the Core doc.
Questions:
- Can I generate the definition directly from the text representations?
- How can I remove the definition?
- It seems that the variable names used in the definitions matter. int() does not provide the same results as Int() in some cases.
- What plugin might be a good source of inspiration for the above questions?

Obviously one can add definitions to the canvas from within a plugin function execution using something like this:
Entry lhs = Entry.Create(TermsConverter.ToTerms("int(f,x,a,b)"
);
Entry rhs = Entry.Create(TermsConverter.ToTerms("Int(f,x,a,b)"
); ;
Entry def = context.AddDefinition(lhs, rhs);
This replaces the built-in definition of int(4) with the one provided by the maxima plugin in the current downstream canvas.
The above solution was found by blindly poking around, assisted by the Core doc.
Questions:
- Can I generate the definition directly from the text representations?
- How can I remove the definition?
- It seems that the variable names used in the definitions matter. int() does not provide the same results as Int() in some cases.
- What plugin might be a good source of inspiration for the above questions?
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#2 Posted: 7/28/2017 7:36:19 PM
Good trick 
You can split a plain text representation at the first definition symbol and generate the LHS & RHS parts; (C# string.Split() methods);
Store.RemoveAt(), Store.removeRange() and Store.Clear() are the methods (last if you know the Definition, first two if you record where they were placed;
I have to check.
probably the include plugin by Viacheslav

Wrote- Can I generate the definition directly from the text representations?
You can split a plain text representation at the first definition symbol and generate the LHS & RHS parts; (C# string.Split() methods);
Wrote- How can I remove the definition?
Store.RemoveAt(), Store.removeRange() and Store.Clear() are the methods (last if you know the Definition, first two if you record where they were placed;
Wrote- It seems that the variable names used in the definitions matter. int() does not provide the same results as Int() in some cases.
I have to check.
Wrote- What plugin might be a good source of inspiration for the above questions?
probably the include plugin by Viacheslav
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
Martin Kraska 7/29/2017 9:56:00 AM
#3 Posted: 7/28/2017 8:32:40 PM
Possible code:
Entry lhs = Entry.Create(TermsConverter.ToTerms("int(f,x,a,b)"
);
Entry rhs = Entry.Create(TermsConverter.ToTerms("Int(f,x,a,b)"
); ;
int index = context.Count;
Entry def = context.AddDefinition(lhs, rhs);
try
{
// do your stuff here
}
catch
{
// errors, they might happens
}
finally
{
// remove the definition, even if an error occourred
context.RemoveAt(index);
}
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#4 Posted: 7/29/2017 9:54:59 AM
Thanks, Davide.
The include plugin has just one occurance of AddDefinition.
The idea is now to obtain the index of the definition using Store.Contains(Entry,Index) and remove it with RemoveAt()
As to the dependence on variable names: It was mitigated by also doing preprocessing for the integration variable in Int(4), otherwise parameter replacement didn't seem to work. Usually, the integration variable is just a dummy name and should not require preprocessing...
Is it sensible to use strange variable names like x__ in the definitions as it is sometimes on the canvas?
The include plugin has just one occurance of AddDefinition.
The idea is now to obtain the index of the definition using Store.Contains(Entry,Index) and remove it with RemoveAt()
As to the dependence on variable names: It was mitigated by also doing preprocessing for the integration variable in Int(4), otherwise parameter replacement didn't seem to work. Usually, the integration variable is just a dummy name and should not require preprocessing...
Is it sensible to use strange variable names like x__ in the definitions as it is sometimes on the canvas?
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#5 Posted: 7/29/2017 12:47:22 PM
#6 Posted: 7/29/2017 8:52:21 PM
I implemented a new MaximaTakeover() function, which adds definitions to the context or removes them according to the given arguments. First tests seem very promising, such that the full functionality of the Maxima plugin can be re-established soon.
It turned out that something like
didn't do the job of deleting the definitions from the global context. The deletion actually worked (as per inspection in the debugger) but that was of only local scope. I had to resort to Clear().
It turned out that something like
int index;
if (context.Contains(lhs, out index) context.RemoveAt(index);
public static bool MaximaTakeoverF1(Term[][] args, ref Store context, ref Term[] result)
{
// List of available definitions
String[] Functions = new String[]
{
"Diff(f__)",
"Diff(f__,x__)",
"Diff(f__,x__,n__)",
"Int(f__,x__)",
"Int(f__,x__,a__,b__)",
"Lim(f__,x__,x0__)",
"Sum(f__,var__,a__,b__)",
"Det(m__)"
};
// Try to ensure maxima is running
ControlObjects.CommunicationInterface.GetMaxima();
// Check if Maxima is running and complain otherwise
if (ControlObjects.CommunicationInterface.GetMaxima().GetState() != ControlObjects.CommunicationInterface.GetMaxima().GetMaximaStateRunning())
{
result = TermsConverter.ToTerms(ControlObjects.CommunicationInterface.GetMaxima().GetState());
return true;
}
// normalize the arguments list to the first three characters of the string representation
// i.e. int, dif, lim,...
List<string> NormArgs = new List<string>() { };
foreach (Term[] arg in args)
{
string Arg = TermsConverter.ToString(arg);
if (Arg.Length > 3) NormArgs.Add(Arg.Substring(1,3));
}
// Loop over the available functions
string message = "";
foreach (String Function in Functions)
{
string LhsString = Function.ToLower();
Entry lhs = Entry.Create(TermsConverter.ToTerms(LhsString));
if (NormArgs.Contains(LhsString.Substring(0, 3)) || NormArgs.Contains("all"
)
{
// add definition
Entry rhs = Entry.Create(TermsConverter.ToTerms(Function)); ;
Entry def = context.AddDefinition(lhs, rhs);
// augment message
string MsgPart = LhsString.Split('('
[0] + "(), ";
if (!message.Contains(MsgPart)) message += MsgPart;
}
else
{
// remove definition
context.Clear(lhs);
}
}
// Make result string
result = TermsConverter.ToTerms(Symbols.StringChar + message + Symbols.StringChar);
return true;
}
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#7 Posted: 7/29/2017 9:19:33 PM
#8 Posted: 7/31/2017 1:08:43 PM
Hello Martin,
Interesting, though the algo style is native in Smath 5346, 6179
except the first release 6179 is missing Int(2) [indefinite].
I think Sum will work as well, at least it was 5346.
Cheers, Jean
Maths Algo Style [Diff, Int].sm (15 KiB) downloaded 33 time(s).
Interesting, though the algo style is native in Smath 5346, 6179
except the first release 6179 is missing Int(2) [indefinite].
I think Sum will work as well, at least it was 5346.
Cheers, Jean
Maths Algo Style [Diff, Int].sm (15 KiB) downloaded 33 time(s).
#9 Posted: 8/1/2017 11:26:43 AM
I'd like to provide a standard description to the definitions created in MaximaTakeover().
Currently, the descriptions simply show the definition (as can be seen in the previous post).
The idea is, to get the description of the Maxima function (Int(...) and add it as description to the generated definitions, perhaps with some adjustments (convert uppercase to lowercase in function names).
How would I do that?
Currently, the descriptions simply show the definition (as can be seen in the previous post).
The idea is, to get the description of the Maxima function (Int(...) and add it as description to the generated definitions, perhaps with some adjustments (convert uppercase to lowercase in function names).
How would I do that?
public static bool MaximaTakeoverF(Term[][] args, ref Store context, ref Term[] result)
{
// List of available definitions
String[] Functions = new String[]
{
"Diff(f__)",
"Diff(f__,x__)",
"Diff(f__,x__,n__)",
"Int(f__,x__)",
"Int(f__,x__,a__,b__)",
"Lim(f__,x__,x0__)",
"Sum(f__,var__,a__,b__)",
"Det(m__)"
};
// Try to ensure maxima is running
ControlObjects.CommunicationInterface.GetMaxima();
// Check if Maxima is running and complain otherwise
if (ControlObjects.CommunicationInterface.GetMaxima().GetState() != ControlObjects.CommunicationInterface.GetMaxima().GetMaximaStateRunning())
{
result = TermsConverter.ToTerms(ControlObjects.CommunicationInterface.GetMaxima().GetState());
return true;
}
// Generate a string with all args concatenated
var ArgString = "";
foreach (Term[] arg in args) ArgString = ArgString + TermsConverter.ToString((Decision.Preprocessing(arg, ref context)));
// Loop over the available functions
string message = "";
foreach (String Function in Functions)
{
string LhsString = Function.ToLower();
Entry lhs = Entry.Create(TermsConverter.ToTerms(LhsString));
if (ArgString.Contains(LhsString.Substring(0, 3)) || ArgString.Contains("all"
)
{
// add definition
Entry rhs = Entry.Create(TermsConverter.ToTerms(Function));
Entry def = context.AddDefinition(lhs, rhs);
// augment message
string MsgPart = LhsString.Split('('
[0] + "(), ";
if (!message.Contains(MsgPart)) message += MsgPart;
}
else
{
// remove definition
context.Clear(lhs);
}
}
// Make result string
if (message.Length == 0) message = "All functions handled by SMath";
else message = message.Substring(0, message.Length - 2) + " handled by Maxima";
result = TermsConverter.ToTerms(Symbols.StringChar + message + Symbols.StringChar);
return true;
}
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#10 Posted: 8/1/2017 2:23:20 PM
You can use the AddDefinition(Definition value) method; Definition has some constructor with the description argument
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
Martin Kraska 8/1/2017 7:01:00 PM
#11 Posted: 8/1/2017 8:06:09 PM
WroteYou can use the AddDefinition(Definition value) method; Definition has some constructor with the description argument
Thanks. Davide.
First, I tried a very compact way, but that didn't make it beyond the current math region.
// add definition
Entry rhs = Entry.Create(TermsConverter.ToTerms(Function));
context.AddDefinition(lhs, rhs);
// add description
context[context.Count-1].Description = "description of "+ LhsString ;
Then I used the approach proposed by you, unfortunately, the definition constructor requires a terms list to specify the arguments. I found no smart way to extract this list from the function definition strings. Thus the code looks ugly and bloated.
string name = LhsString.Split('('
[0];
string[] vars = Regex.Replace(LhsString, @".+\((.+)\)", "$1"
.Split(','
;
Term[] Tvars = new Term[1];
int i = 0;
foreach (string v in vars)
{
Tvars[i] = new Term(v, TermType.Operand, 0); i++;
}
context.AddDefinition(new Definition(name, Tvars, rhs, "description of "+name));
This approach didn't work either, I even could not see the descriptions being generated at all in the debugger.
And in the end, when trying to provide a description in a math region directly, this is ignored. For the moment I have to give up. This might be related to the target functions being special functions. Re-definition somehow slips through, re-description obviously not.
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#12 Posted: 8/2/2017 4:53:58 AM
Hello Martin,
Confirmed. I've looked at the core code and I know why, I'll try to make ii possible.
WroteThis approach didn't work either, I even could not see the descriptions being generated at all in the debugger.
Confirmed. I've looked at the core code and I know why, I'll try to make ii possible.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#13 Posted: 8/2/2017 7:26:29 AM
WroteConfirmed. I've looked at the core code and I know why, I'll try to make ii possible.
Sounds promising. There is more to report.
context[context.Count-1].Description = "description of "+ LhsString ;
This guy isn't just useless, it is also dangerous. It sweeps all but one signature variants for each function off the context. Just the versions with a single argument survive this seemingly harmless line of code.
BTW, working with the debugger and observing how the context changes during function calls provides great insight into how SMath works. It's real fun!
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 Pages (13 items)
-
New Posts
-
No New Posts