SMath Studio Forum
»
SMath Studio
»
Questions
»
[DEV] Substitutions in expressions
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1125 time(s) in 722 post(s)
|
In SMath, i can either be the imaginary unit or the name of a variable, e.g. if used as iteration variable in sums and products. The same holds for e (which, however, is not so common as variable name). Translation to maxima requires different treatment for both uses. Constants e and i are translated to %e and %i. Applying this to variable names results in wrong expressions. In the function Sum(), I know where to find the variable and can replace their name by something magic, thereby shadowing eventual use of i or e. I think that this is sensible because the user can't expect the imaginary unit to keep it's value if the same name is used as loop variable. Even if I would choose to issue an error message saying that i and e must not be used as variable names, I still have the problem of identifying, where e and i are constants and where they are used as variables in expressions of arbitrary complexity. Is there any chance to do that in an elegant way? In general, I would prefer the constants e and i being visually distinct from variable names e and i. An option would be to treat the constants as dimensionless units (see SS-48). Then life would be much easier. Code:
public static bool Sum(Term root, Term[][] args, ref Store context, ref Term[] result)
{
var loopVar = args[1][0].Text;
var loopVarMod = "LXAXUXF"+loopVar;
// replace the name of the loop variable with something strange to avoid misunderstanding as i or e or whatever in Maxima
for (int k = 0; k < args[0].Length; k++)
{
args[0][k].Text = args[0][k].Text.Replace(loopVar, loopVarMod);
}
// Do preprocessing and convert to string. No need to preprocess the loop variable.
string stringToMaxima = "sum(" +
TermsConverter.ToString(Decision.Preprocessing(args[0], ref context)) + ',' +
loopVarMod + ',' +
TermsConverter.ToString(Decision.Preprocessing(args[2], ref context)) + ',' +
TermsConverter.ToString(Decision.Preprocessing(args[3], ref context)) + ")";
// ready for standard processing
result = TermsConverter.ToTerms(ControlObjects.CommunicationInterface.SendAndReceive(stringToMaxima, ""));
return true;
}
Sumtest.sm (11kb) downloaded 8 time(s). |
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,648 Location: Italy Was thanked: 1331 time(s) in 876 post(s)
|
Hello Martin, Originally Posted by: mkraska I still have the problem of identifying, where e and i are constants and where they are used as variables in expressions of arbitrary complexity. Is there any chance to do that in an elegant way? what about this? Code:var i = new Entry("i");
var e = new Entry("e");
if (context.Contains(i))
result = new Entry("1"); // variable has user-defined value
else
result = new Entry("0"); // variable has default value
proof of concept: Edited by user 12 July 2017 18:49:45(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
1 user thanked Davide Carpi for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1125 time(s) in 722 post(s)
|
Hello Davide,
thank you for your example, this gives insight into the use of the Store object and Entry().
I have to check if i or e really make it into the context when they are used as loop variables in sums which smath can't handle (if NotDefinedException is thrown by the preprocessor). Only in that case, the context.Contains() test would be a solution, because the problem also arises when e and i aren't re-defined outside the current expression.
Martin |
|
|
|
|
SMath Studio Forum
»
SMath Studio
»
Questions
»
[DEV] Substitutions in expressions
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.