1 страниц (3 вхождений)
[DEV] Substitutions in expressions - Сообщения
#1 Опубликовано: 11.07.2017 21:54:09
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.
Sumtest.sm (10 КиБ) скачан 30 раз(а).

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.
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 (10 КиБ) скачан 30 раз(а).
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#2 Опубликовано: 12.07.2017 12:43:25
Hello Martin,
what about this?
proof of concept:
![2017-07-12 17_46_25-SMath Studio - [Page1_].png](/ru-RU/file/QWCRD6/2017-07-12-17_46_25-SMath-Studio---_Page1___png)
WroteI 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?
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:
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 пользователям понравился этот пост
Martin Kraska 12.07.2017 13:15:00
#3 Опубликовано: 12.07.2017 15:22:22
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
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
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 страниц (3 вхождений)
-
Новые сообщения
-
Нет новых сообщений