Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline mkraska  
#1 Posted : 12 July 2017 03:54:09(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,986
Germany

Was thanked: 1124 time(s) in 721 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).
Sumtest.png
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline Davide Carpi  
#2 Posted : 12 July 2017 18:43:25(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,647
Man
Italy
Location: Italy

Was thanked: 1329 time(s) in 875 post(s)
Hello Martin,

Originally Posted by: mkraska Go to Quoted Post
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:
2017-07-12 17_46_25-SMath Studio - [Page1_].png

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
thanks 1 user thanked Davide Carpi for this useful post.
on 12/07/2017(UTC)
Offline mkraska  
#3 Posted : 12 July 2017 21:22:22(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,986
Germany

Was thanked: 1124 time(s) in 721 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
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Users browsing this topic
Guest
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.