Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

2 Pages12>
Options
Go to last post Go to first unread
Offline Davide Carpi  
#1 Posted : 18 October 2012 12:58:43(UTC)
Davide Carpi


Rank: Advanced Member

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

Was thanked: 1120 time(s) in 731 post(s)
Hi,

I think would be useful a clear(variableName1,variableName2,...) function.


regards,

w3b5urf3r

Edited by user 18 October 2012 22:51:57(UTC)  | Reason: Not specified

Davide Carpi attached the following image(s):
SMath Studio - [Page1]_2.png
If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 3 users thanked Davide Carpi for this useful post.
on 18/10/2012(UTC),  on 19/10/2012(UTC),  on 19/10/2012(UTC)
Offline cobanml  
#2 Posted : 25 October 2012 00:19:05(UTC)
cobanml

Rank: Newbie

Groups: Registered
Joined: 25/10/2012(UTC)
Posts: 1
Location: Canada

Best would be a clear fonction that clear every thing used above
I am sure it is very easy to get for the next Smath
Offline mkraska  
#3 Posted : 25 October 2012 02:21:38(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
I'd propose to use the assignment operator for clearing variables, i.e. something like

a:#

a:?

a:undefined

BTW: Currently the question mark is a valid variable name, at least you can assign values to it and you can use it. You just have to paste it from the clipboard, typing does not work.

Best regards, Martin
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
thanks 1 user thanked mkraska for this useful post.
on 25/10/2012(UTC)
Offline Rising Eagle  
#4 Posted : 14 December 2012 06:09:26(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Haven't posted in years. Not since there was a requirement to create a user name. I resisted for a long time, but now I must speak.

There already is a way to clear a global variable already present in smath.

All you have to do is use the line function. To be whimsical about it, the line function is like a space-warp (or is it time-warp or space-time-warp). In any event (this is a pun related to black hole event-horizons, btw), in this space-warp, the local environment is shielded from the global environment where all global variables are defined. Within the line function environment, no variables really exists from the global arena except their shadows: the variable label and their global values (as defined prior to the line function). Inside the line function, all variables are local (any assignment to any variable name creates a brand new local variable) or are global variables treated as local variables. An assignment to a global variable actually creates a new local variable of the same name that supersedes the global variable by the same name and this local variable is now used locally in all that follows within the line function environment. Therefore, assignments to global variables redefine locally and those assignments do not survive outside the line function environment in the global environment where the global variable reasserts itself and the local variable no longer exists.

The magic occurs when passing calculated results out through the very last instruction in the line function. All variables in the last line that have a global counterpart and are unchanged locally are passed out as a fully refreshed, pristine, uninitialized variable without the full substitution rules enforced. They are passed out as var parameters (by reference), not as val parameters, and so any values they may have already associated with them from the global environment will be incorporated into the calculated result. Variables created locally within the line function environment or global variables that are locally changed within the line function environment that are present in the last instruction are passed as val parameters and their values are funneled into the calculated value that passes out of the line function. The receiving variable in the global arena inherits a dependency on each and every global variable (i.e., any variables that are not changed in any way locally) mentioned in the last instruction, though any values assigned to those variables in the global environment prior to the line function are funneled into the current value passed.

Now, there is a quirk in the last instruction of the line function. If a global variable is present without being involved in any arithmetic, it is stripped of any globally assigned value and so the receiving variable gets a fully uninitiated reset as a result.

So, with that explanation, here is the very simple syntax for resetting/clearing a global variable:

b2 := |b2

so when you see the definition of b2 in the global arena, you get b2 -> b2

or

a1 := |b2

so when you see the definition of a2, you get a1 -> b2

That's it. There it is. How simple. Note the single line line function on the right. Very simple.

I do have one related request, however. There is currently no way to show all variable dependencies that still exist in a given variable. The dynamic assistance only shows what is calculated by -> and explicit dependencies are not seen in the -> calculation because any variables with a value substituted in will be substituted in the calculated value of the interrogation's -> output string.

eg., a b c
a := 3
b := a
b -> 3
which is correct as b is not dependent on a, however

b := a
a := 3
b -> 3
is not correct because b is dependent on a and if a changes, so will b

This makes debugging hard and causes a lot of confusion when making even simple math sheets. I request some feature that will identify any explicit dependencies that a variable may have. In this example, a syntax of b: a would be useful, not b: 3. If more than one variable, how about a simple string b: a,... . If there is hierarchical dependency, show the dependency tree.


For all concerned, I still use 0.89 build 3931. I hope the behavior of the line function hasn't changed or my entire explanation would be wrong.

Edited by user 14 December 2012 07:51:15(UTC)  | Reason: Not specified

thanks 3 users thanked Rising Eagle for this useful post.
on 14/12/2012(UTC),  on 14/12/2012(UTC),  on 15/12/2012(UTC)
Offline omorr  
#5 Posted : 14 December 2012 14:30:55(UTC)
omorr


Rank: Administration

Groups: Registered, Administrators, Advanced Member
Joined: 23/06/2009(UTC)
Posts: 1,731
Man
Serbia

Was thanked: 311 time(s) in 265 post(s)
Hello Rising Eagle,

Thank you for your post and all of your comments. Thank you for the very useful observation about line() function as well. It seems that you are right and that the line() function behavior did not change - see the attached picture. It could be the most simple way to make a variable undefined.

Regards,
Radovan
omorr attached the following image(s):
clearvar.png
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
Offline mkraska  
#6 Posted : 15 December 2012 04:50:29(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
Hello Rising Eagle,

thank you for sharing your observations of the line() behaviour. It will take me a while to understand them. I wonder if you could summarize in a similar way the behaviour of function definitions, in particular the interaction of formal parameters in the definition with the global name space. I had severe problems with using formal parameter names locally if parameters of the same name existed globally. Up to now I consider that a bug as the name of local variables must not matter. I feel being forced to use odd variable names inside definitions like x_ or x# without any good reason. At least I am not able to explain to my students, why their ordinary programming knowledge does not apply here.

Best regards, Martin Kraska.

Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
Offline Rising Eagle  
#7 Posted : 15 December 2012 06:22:14(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Here is the short answer concerning the behavior of functions for smath 0.89 build 3931, other versions may differ:

The only local variables in a function are the ones declared in the argument/parameter list. All others, whether defined in the global environment prior to the function definition or defined by assignment within the function definition are global. All global variables are accessible within the function definition and may be read from or assigned to within the function definition.

The names of the parameters/local variables are not reserved. If a declared parameter/local var is assigned to, the assigned value will be available in the global environment subsequent to the function definition. If the parameter's name is not currently in the global environment when it is assigned to in the function definition, a global variable of the same name is created and accessible in the global environment subsequent to the definition.

The function definition is active - the function need not be called in order for assignments within its definition to take effect in the subsequent global environment.

When the function is called, global variables placed in the argument list are called by value and they assign both the value and the type (matrix, real, boolean, etc) to substitute in for the local parameters in the function definition. Again, if the local parameters are assigned to, they will take effect in the global environment after the function call. If the global variables in the argument list or any other global variable name is assigned to within the function call, the assignments will take effect in the subsequent global environment.

Test these rules to verify them. Reply with any contradictions or other rules of the road you find. We should get a good guide together of such behaviors for all to refer to.
thanks 1 user thanked Rising Eagle for this useful post.
on 16/12/2012(UTC)
Offline mkraska  
#8 Posted : 15 December 2012 12:49:59(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
Hello Rising Eagle,

thanks again for the explanations. I guess this discussion shall get us quite some steps forward in understanding SMath. Seems that we are left with some sort of reverse engineering.

Now here is the first attempt to challenge your explanation for the line function:
I understood that local definitions of names that exist globally will only affect the global variable if that name appears in the last line of the line function.

In the attached example, the global name b is assigned to in the line block but not used in the last line. Still the global value is changed. I see a contradiction between my understanding of your explanation and my observation of what SMath does.

Due to operator ambiguity (please, Andrey, give us back the -> ) I have to mention that all evaluations are set to numeric by default. BTW that is a field of broad discussion in the forum, still with lack of thorough insight (at least by me).

Best regards, Martin Kraska

Edited by user 15 December 2012 14:08:49(UTC)  | Reason: Not specified

mkraska attached the following image(s):
line1.PNG
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
Offline Davide Carpi  
#9 Posted : 15 December 2012 13:02:11(UTC)
Davide Carpi


Rank: Advanced Member

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

Was thanked: 1120 time(s) in 731 post(s)
Originally Posted by: mkraska Go to Quoted Post
Hello Rising Eagle,

thanks again for the explanations. I guess this discussion shall get us quite some steps forward in understanding SMath. Seems that we are left with some sort of reverse engineering.

Now here is the first attempt to challenge your explanation for the line function:
I understood that local definitions of names that exist globally will only affect the global variable if that name appears in the last line of the line function.

In the attached example, the global name b is assigned to in the line block but not used in the last line. Still the global value is changed. I see a contradiction between my understanding of your explanation and my observation of what SMath does.

Due to operator ambiguity (please, Andrey, give us back the ->Wink I have to mention that all evaluations are set to numeric by default. BTW that is a field of broad discussion in the forum, still with lack of thorough insight (at least by me).

Best regards, Martin Kraska


I think that a line placed freely in the sheet (without assignment) has not be thought as a separate space but only as a "visual aggregator of instructions" (or, at least, sometimes I use it in that way...)
If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
Offline Rising Eagle  
#10 Posted : 15 December 2012 13:48:02(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Hah. Very good. This is how we uncover the quirks.

I have never used the line function as a free floater in calculations, only as an alignment framework for sequential derivations and proofs. However, I have reproduced your result. Clearly, if the line function is not on the RHS of an assignment operator, it doesn't have local variables and its activity is fully in the global environment. That means all substitution properties for variables that apply in the global space will apply identically to the expressions specified within the free-floating line function.

When used as the RHS of an assignment operator, however, its locality is maintained and the previous explanation still holds.
Offline mkraska  
#11 Posted : 15 December 2012 18:24:56(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
Here are some tests with the last line of blocks being assignments. Seems that in local namespaces these assignments are ignored and the block just returns the RHS of that assignment. Thus the assignment target is not transferred to outer namespace even if it explicitely appears in the last line of the block.

Also, for me it looks like that function definitions are inactive as long as the function is not called.


File Attachment(s):
Deep thought.sm (20kb) downloaded 30 time(s).
mkraska attached the following image(s):
Deep thought.png
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
thanks 1 user thanked mkraska for this useful post.
on 16/12/2012(UTC)
Offline Rising Eagle  
#12 Posted : 15 December 2012 22:18:27(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Originally Posted by: mkraska Go to Quoted Post
Here are some tests with the last line of blocks being assignments. Seems that in local namespaces these assignments are ignored and the block just returns the RHS of that assignment. Thus the assignment target is not transferred to outer namespace even if it explicitely appears in the last line of the block.

Also, for me it looks like that function definitions are inactive as long as the function is not called.





I might not have been complete in my explanation of the last line of the line function's behavior and therefore must clarify its behavior to remove any unresolved confusion. The last line funnels an expression, not to the general global environment, but to the LHS of an assignment operator in which the line function is the RHS. In your examples 4 and 5 you express a := b. The value of local variable b (not local b itself, nor the value of global variable b, nor global b itself) transfers to the global d. This is correct behavior as intended by the design of the line function in smath. The assignment to a in the last line is treated the same as in any other line in the line function and is therefore a simple assignment to local a, not global a. This again is correct, intended, and consistent behavior.

As you pointed out previously, a free floating line function does not have local variables, so the above behavior does not apply.

In all your examples 1-5, the behavior of the line function is correctly predicted by the rules we have learned from our prior experiences and our recent observations of its free floating use cases.

In examples 6 and 7, the functions d and e(a) are fully and completely defined by a line function on the RHS. If there is activity inside the line functions, it is all executed on local variables and cannot be ascertained by interrogation in the global environment. If we had the ability to use -> or = within the line function environment and have the calculated values show up from within the line function, we could tell. In the end, it doesn't really matter if they are active locally or not. What matters is the global effect. The effect on the global environment is what we must be able to detect and understand. Coming from within a line function that completely makes up the RHS of a function definition, the behaviors of examples 6 and 7 in terms of the effect on the global environment are exactly what we expect.

Function definitions in general do not necessarily contain line functions. Without that shroud, the operations taking place within the function definition are active in the global environment, are subject to the context of activity and variable substitutions prior to the definition, and effect activity and substitutions coming subsequent to the definition. Functions may be complex and contain parts that are globally active and other parts hidden by line functions. But the rules apply consistently to the individual pieces and make a predictable whole. I failed to mention this in my previous explanation.

As for your example 8. Finally, somebody has seen the face of god and brought the answer to the nature of the universe back to us mere mortals. With the mystery now unveiled, the stars will burn out one by one, eventually leaving a dark and physically lifeless, but fully spiritual universal existence in its wake.

Thank you for the insightful discussion and lucid examples so far. I look forward to more.

I must say, I am impressed by the caliber of the members of the smath community and the remarkable advancements in its development and applications over the past few years. My hat's off to all of you.

Edited by user 15 December 2012 22:40:45(UTC)  | Reason: Not specified

thanks 2 users thanked Rising Eagle for this useful post.
on 16/12/2012(UTC),  on 16/12/2012(UTC)
Offline mkraska  
#13 Posted : 16 December 2012 03:26:05(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
Hello Rising Eagle,

I feel a little bit stretched between the wish to understand what happens in SMath and the frustration of finding it hard to understand.

For 99% of my personal use of SMath I can live well with just not using features that I do not understand or whom I do not trust. On the other hand I feel very uncomfortable recommending SMath to my students (of mechanical engineering) without being able to clearly name the concepts and limitations. What I want to give them is a reliable referenc manual including programming features. Due to lack of such documentation I am forced to guess the rules from forum discussions and own experience, write them down and challenge them by examples. There is always the danger of blaming SMath of being buggy just by failing to understand the beauty of a particular concept or of not mentioning really big traps.

Topics, where the ice is particularly thin for me are:
- predicting the behaviour of symbolic evaluation. So far I just recommend my students not to use it (just use numeric evaluation).
- clearly stating what the effects of using eval() inside and outside of definitions and line blocks are.
- explaining why it matters what formal parameter names are used inside a function definition. I was teached that these names must not matter except that they might shadow existing global variables.
- what is the concept of active definitions and where is the benefit of having them. What is the benefit of making line blocks behaving special in that respect.
- why is the imaginary unit i still defined even if I use i as loop counter? Didn't I then redefine the symbol?
ok, let's leave the remainder for next x-mas.

Did you obtain your remarkable understanding of SMath by reverse engineering (trial and error) or by first hand information or just by identifying concepts that are common and trivial to you?

Now let's go on playing. I am going to challenge the statement: "Function definitions are active by default (i.e. their body is executed at defintion time with perhaps undefined formal parameter values) unless the top level body statement is a line block."

I shall use the trace() function for examination what is when executed (ascertaining activity) and what current variable values are

Here is a function without line block in it:

f1(n):for(i,range(1,n),trace(i))

Having this definition in the sheet and enforcing calculation of the sheet does not generate any output to the output window.

When calling the function with an instruction like

f1(3)=i@#

then SMath complains about f1 having no return value. This does not retain it from displaying the result "i" and from executing the iteration statement, leading to the following output:

i = 1
i = 2
i = 3

My observation says that the challenged statement cannot be true. I would replace it by "The right hand side of variable or function assignments is only executed if the assignment target (the name) is evaluated" (regardless if it contains or is completely built of line blocks).

Best reagards, Martin Kraska

Edited by user 16 December 2012 03:28:24(UTC)  | Reason: Not specified

Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
thanks 1 user thanked mkraska for this useful post.
on 16/12/2012(UTC)
Offline Rising Eagle  
#14 Posted : 16 December 2012 14:53:21(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Originally Posted by: mkraska Go to Quoted Post


- predicting the behaviour of symbolic evaluation. So far I just recommend my students not to use it (just use numeric evaluation).
- clearly stating what the effects of using eval() inside and outside of definitions and line blocks are.
- explaining why it matters what formal parameter names are used inside a function definition. I was teached that these names must not matter except that they might shadow existing global variables.
- what is the concept of active definitions and where is the benefit of having them. What is the benefit of making line blocks behaving special in that respect.
- why is the imaginary unit i still defined even if I use i as loop counter? Didn't I then redefine the symbol?
ok, let's leave the remainder for next x-mas.

Did you obtain your remarkable understanding of SMath by reverse engineering (trial and error) or by first hand information or just by identifying concepts that are common and trivial to you?

Now let's go on playing. I am going to challenge the statement: "Function definitions are active by default (i.e. their body is executed at defintion time with perhaps undefined formal parameter values) unless the top level body statement is a line block."

I shall use the trace() function for examination what is when executed (ascertaining activity) and what current variable values are

Here is a function without line block in it:

f1(n):for(i,range(1,n),trace(i))

Having this definition in the sheet and enforcing calculation of the sheet does not generate any output to the output window.

When calling the function with an instruction like

f1(3)=i@#

then SMath complains about f1 having no return value. This does not retain it from displaying the result "i" and from executing the iteration statement, leading to the following output:

i = 1
i = 2
i = 3

My observation says that the challenged statement cannot be true. I would replace it by "The right hand side of variable or function assignments is only executed if the assignment target (the name) is evaluated" (regardless if it contains or is completely built of line blocks).

Best reagards, Martin Kraska


My understanding is the same as for you. Experience, observation, reading about it online, mixed with my training and experience in math, software programming, and uses of other software math tools. I create general rules that fit the information extracted from all these sources. Since my use of smath is not fully comprehensive, there will be cases where smath behavior deviates from my understanding. For this limitation in my understanding (and related limitations in my explanations), I apologize. As well, subtle, but often critical details that are inadvertently taken for granted in my thinking may be left out of explanations. Alas, this is a folly of communication as to how much you can assume is shared knowledge between sender and receiver. The less the sender takes for granted, the more protracted the explanations become. Nobody wants to write an encyclopedia and nobody wants to read one. This back and forth is now uncovering new ground which calls for updates to my thinking to account for new cases that I have not considered or explored before. So let us not hesitate to explore the terrain.

I will assume smath has a finite logical set of rules in the algorithms it uses to parse and construct formulas. However, its different modes (numeric, symbolic), myriad structures (function definitions, line functions, for loops, etc), myriad operators (->, :=, +, -, ^, etc) make the set of rules complex and the effort to reverse engineer protracted.

Some quick answers:
symbolic evaluation - do you mean what are the rules for substitution to construct formulas that are defined or how does it do algebraic manipulations to simplify expressions? In the former, I believe I understand the basics, but now see from your examples that there are structures I have not used extensively enough to have seen the deeper patterns. We have already discussed some cases and we will get a couple more tonight. In the latter, I find the smath algebraic simplification quite lacking, even disappointing. I try not to use it without proving that its results are correct. If you refer to -> vs =, my understanding is that = only works if all variable dependencies in an expression have a numerical value assigned to them so that a number can be calculated. -> allows variable dependencies with no numerical value assigned to it and the display string will show those unvalued variables in relation to numbers already calculable in the rest of the expression.
eval() - never use it. Never needed to in my work. From reading, it forces immediate numerical substitution and calculation and cannot work if one or more variables do not yet have a numerical value assigned.
formal parameters - this is the basis of our current discussion. We will uncover their secrets.
imaginary unit - don't know full explanation. In your for loop, the i is a local variable and does not exist as a global variable, therefore, it cannot supersede the default imaginary number i. When global i is assigned anything other than the imaginary i in the global environment, the i becomes a global variable of a new type and the new definition of i with the new type that goes with it does supersede the default imaginary unit i.
active definitions - this is my term. I refer to function definitions of math formulas in the conventional sense without the use of line functions or other programming structures. I do not think of the line function when I think of a math function with parameters; this combination is a programming extension offered by smath. This combination is important to explore, however, and your list of examples begins that process, though, I don't believe any of them actually contradict my understanding of the two separate structures when I think of how they would behave when used together. My two original explanations assumed math functions with parameters and line functions as mutually exclusive and separate concepts. In your thinking, they are fundamentally linked. To me, for example, a math function is f(x) := x^2 +1.

I mean active in the sense that if you embed assignments, e.g., f(x) := (m := x)^2 +1, then the global variable m is assigned the symbolic substitution of global x which shows up subsequent to the definition. The formula, on the other hand, uses the local value supplied by the function argument parameter (into a local variable x) from the function call. Consider:
m->m, x->x
f(x) := (m := x)^2 + 1
m->x, x->x
see how the embedded assignment assigned global x to global m and this is just the definition. Also
m->m, x->22
f(x) := (m := x)^2 + 1
m->22, x->22
again see the global assignment, even though the function has never been called. This is what I mean by active definition. The evaluation of x^2+1 cannot, of course, be evaluated numerically since there is no value for the local x substituted in with a function definition. Evaluation only occurs when parameter is supplied in a call to f(x).

A subtlety here is that when smath parses the definition, it recognizes the global assignment instruction and that it is independent of the formula and takes the action right away and removes it from the formula used to calculate f(x) when f(x) is called subsequently so that the global assignment is never called again.
m:=4, x:=15
f(3)=10
m:=4, x:=15

In your for loop/trace example, the i is a local variable and no parameter argument is supplied to occupy local variable n, so the loop cannot execute in the definition. In fact, smath goes further and makes it a syntax error if a numerical value is directly used in the for loop range in this example of a function definition (this syntax error does not occur if the for loop is free floating, but only when it is in the RHS of an assignment operator).

Now consider a more advanced function:
m->m, j->j, p->p, q->q, r->r
s2(p,q,r) := †if (m:=†r)>3, (p:=†q+p)^3+q^2+r+p+m+j, else 3
m->r, p->q+p
dynamic assist shows the function defined as s2(p,q,r)->if (r>3), 2*r+p+j+(p+q)^3+q^2, else 3
Here, from the definition provided, smath performs several operations. The embedded global assignments occur for m and for p which take effect in the global environment subsequent to the definition. See from the formula how smath constructs the function and it has been stripped of these assignments. These global assignments occur only during the definition of the function and are not carried out during any call to the function. Any algebraic and/or numerical simplifications and optimizations that smath is able to do in advance for the function occurs immediately when it parses and reconstructs the formula in the definition. These are all part of the active definition feature of smath. Recap: each assignment occurs in two ways. The assignment is interpreted as a global operation with global variables and values. Global operations occur only during definition of formula and not in subsequent calls to the function. Each assignment is also interpreted as a local operation with local variables and values; their effect (and any other possible optimizations) on the formula expression is applied immediately by smath during formula definition. The algorithm for making local substitutions is basically sequential in order of their appearance in the definition syntax and applied to subsequent instances of the variable to be substituted in the formula definition. This algorithm in smath is flawed if circular assignment definitions are attempted.

A circular example for you to play with:
m->m, j->j, p->p, q->q, r->r
s2(p,q,r) := †if (q:=†r+p)>3, (p:=†p+q)^3+q^2+r+p+m+j, else p:=3
p->6+r, q->6+2*r, r->r
dynamic assistance shows: s2(p,q,r) := †if (6+2*r)>3, 3+r+m+j+(6+r)^3+(3+r)^2, else 3

After playing with this function, you will see that if there is no circular references in the assignment statements, the substitutions are sequential and make sense. Circular references cause screwy results from the smath parser, however.

If you still have questions about the line function (a facility for writing mini linearly sequential computer programs within smath and which may be used to define complicated functions) please ask. I'm not sure what is the cause of any confusion.
Offline mkraska  
#15 Posted : 16 December 2012 17:59:02(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
Thank you again for the interesting contribution. Responding to all of the aspects will take some time, therefore I just take some of them.

Originally Posted by: Rising Eagle Go to Quoted Post
In your for loop/trace example, the i is a local variable and no parameter argument is supplied to occupy local variable n, so the loop cannot execute in the definition. In fact, smath goes further and makes it a syntax error if a numerical value is directly used in the for loop range in this example of a function definition (this syntax error does not occur if the for loop is free floating, but only when it is in the RHS of an assignment operator).

If the formal argument n has a value or not does not affect the definition. You see that in the first section of the attached example. Still the loop is only executed if the function is called.

Thanks for explaining the term "active definition". My understanding is now: "A definition is active if embedded assignment operations are executed at definition time". Correct?

BTW, I never would have guessed that "for" or perhaps also other flow control structures create their own name space. At least, the iterator variable is local as long as it is not used in embedded assignments. To my taste this is one more time too much implicit scoping in SMath.

Originally Posted by: Rising Eagle Go to Quoted Post
However, its different modes (numeric, symbolic), myriad structures (function definitions, line functions, for loops, etc), myriad operators (->, :=, +, -, ^, etc) make the set of rules complex and the effort to reverse engineer protracted.

Perhaps we have to add also differences in the program versions, as I fail in reproducing some of your observations.

Originally Posted by: Rising Eagle Go to Quoted Post
symbolic evaluation - do you mean what are the rules for substitution to construct formulas that are defined or how does it do algebraic manipulations to simplify expressions?

Worst of all is the lack of the -> operator in recent SMath versions. The operator is available but is displayed as "=". Thus you cannot judge the evaluation mode by just looking at the doc. There have been discussions on what the effects of symbolic/numeric settings for functions at definition time and at call time are. Our recent discussion has really shed some light on that as well, might be worth revisiting those old examples.

File Attachment(s):
for loop and function.sm (14kb) downloaded 28 time(s).
mkraska attached the following image(s):
for loop and function.png
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
Offline omorr  
#16 Posted : 16 December 2012 18:45:59(UTC)
omorr


Rank: Administration

Groups: Registered, Administrators, Advanced Member
Joined: 23/06/2009(UTC)
Posts: 1,731
Man
Serbia

Was thanked: 311 time(s) in 265 post(s)
Hello Rising Eagle, mkraska
Originally Posted by: Rising Eagle Go to Quoted Post
Now consider a more advanced function:
m->m, j->j, p->p, q->q, r->r
s2(p,q,r) := †if (m:=†r)>3, (p:=†q+p)^3+q^2+r+p+m+j, else 3
m->r, p->q+p
dynamic assist shows the function defined as s2(p,q,r)->if (r>3), 2*r+p+j+(p+q)^3+q^2, else 3

Thank you again for these posts and your patience to follow all these issues. Martin (mkraska) and others pointed out our frustration by not understanding how all these things actually work.
Rising Eagle - I hope you would not mind if I suggest you to use MATH tags to represent the SMath regions and/or to use attached pictures - screenshots (see the attached picture, please). It would be much easier to follow your discussion. As you could see, it seems that I did not manage to reproduce your above example presented in simple text. It might be due to your (or mine) mistake or due to the SMath version difference - I used the recent one.

I also agree with Martin that excluding the visibility of "->" operator (symbolic equal to) in recent SMath versions in fact causes more confusion, in spite of its visual effect to represent it as ordinary equal to "=".

Regards,
Radovan

Edited by user 16 December 2012 18:59:35(UTC)  | Reason: Not specified

omorr attached the following image(s):
RE-example1.png
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
Offline Rising Eagle  
#17 Posted : 17 December 2012 05:16:58(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Martin,

I was afraid of this difference in versions being a problem with our discussion. Unfortunately, this is the very reason I have not upgraded to newer versions. I did not want my extensive work based on 0.89 to show errors. It would be a huge problem to try to debug them all to make them work properly in newer versions.

There clearly are differences in the handling of global vs local in our two different versions. Some are feature changes and some are bug fixes implemented in the advancement from my version to yours. I hope none of the differences are caused by the introduction of new bugs into the newer versions.

You are on the right track with your understanding of my use of the term active definition. However, the assignments are only a part of it. smath doesn't just handle embedded assignments during a definition, it also parses the RHS, assembles local and global variables, does numeric and symbol substitutions, applies some optimizations, constructs a sequence of operations on the local and global variables, and generates a mini computer subroutine that represents the function. We must understand all this activity, because much of it (often in unexpected ways) can affect the global environment subsequent to the definition. That is why I use the term active definition.

The algorithms for doing all these things are different depending on the specific structures that are used and how they are combined in an expression. The goal of smath is to generate a subroutine that behaves as we would expect in a mathematical perspective that is analogous to what a mathematician might create with pen and paper. The problem is that the possibilities are so vast that even when things work exactly as we want them too, surprising effects can occur that confuse us. For example, in a global sheet, an assignment in the beginning of the sheet to a variable name may have been forgotten and we reuse the same variable later on with unexpected results. Also, the handling of global and local spaces differs among the many structures and even among the many usage possibilities of each structure. To gain the benefit of the power and potential of smath, we must spend much time learning about its structures, how it represents expressions, and get our thinking in sync with it so we can create expressions that are handled in exactly the way we want and get result that are in tune with what we are looking for.

Radovan,

I surely would like to use the math tags. However, with my version (a known bug), the copy does not correctly represent an expression in its text string format that I can paste into the forum posting editor. Since I don't know the exact ascii syntax of complicated expressions that smath generates, I am at a loss. I will make some time soon and study how to provide file attachments, screen shots, and live smath embedding into my posts. It will probably have to wait till after the holidays.

Also, thank you for trying out the if statement. I don't believe you have made any mistakes. My belief is that the algorithm for parsing has been updated for the better. The circular nature of the original expression makes it difficult to say what is a correct parsing. I'm not even sure there is a correct parsing, just one that follows a set of consistent rules and with results that allow us to understand how and why the reconstructed expression is created. In this case, I think I like your versions result better than my own. I can even accept that for the embedded assignment p := p + q, the parser saw it was circular since global p is not numerically initialized and chose to ignore it leaving global p undefined. I can live with that ground rule. It also seems the parser initialized global m to the value global r and left the local m in the algebraic formula unchanged. Again, this is a ground rule I can live with. This parser behavior is easy to understand and allows us to create predictably behaving formulas (at least in this case).

Martin,

I have some more information about how the for loop behaves, at least in my version. I also looked into the issue with the imaginary number and discovered some interesting things, including some mishandling by smath.

For loop:
In my brief explanation in my previous post, I stated the index variable is local. I spoke too soon. Using i as the index caused its own quirky and misleading behavior. It looks like index handling is more complicated and its behavior using i is just a further complication. i is a reserved character, so we will talk about that later and not use it in this discussion. My latest exploration in my version shows the following behavior:

Using the for loop as RHS in an assignment operation:
Behavior upon definition:
- the LHS must have parens and at least one parameter for smath to use the for loop, or it won't create the definition
- the parameter must be that of the first value in the range
- additional parameters are permitted, but not necessary
- on the RHS, the index variable must be a single, naked variable, no embedded assignment is allowed
- the index variable has no local or global effects at definition time (it is inactive)
- the first and second range variables may be assigned to other variables and the assignment is active
- such assignments use global values the range vars had prior to the definition and assign to global vars with subsequent effect
- the body of the for loop has no local or global effects at definition time (it is inactive)
- embedded assignments are allowed in the body, but are inactive at definition time
Behavior upon call:
- a check is made that the number of parameters matches that from the definition, won't execute if there is mismatch
- parameter for index var is ignored completely by the index var
- value of global var with same name as index var is ignored
- in my version, the index var is global and its final index value has global effect after execution
- assignments to index var embedded in range vars are superseded by the index assignment
- assignments to index var in body of for loop become final value (from last loop iteration) in the global environment
- in yours, it only is global if used in the body of the for loop as shown in your example
- the first range var takes value of its parameter
- the second range var takes value of its parameter if it exists, if not, takes value from the global variable
- both range variables are strictly local vars unless assigned to, in which case their values are copied into global counterparts
- embedded assignments of the range vars are carried out
- if range vars don't have integer values execution is halted
- if range vars have values that allow body execution, it proceeds
- any embedded global assignments are carried out

Use of the for loop as free floating expression holds no surprises and behaves as we would expect with all global variables.

smath sometimes does not execute a call to the function after an editing change. F9 must be hit to recalculate. Don't know why. So when investigating, sometimes we examine expressions that are considered syntax errors and smath can be confused. It will mislead the investigation if F9 is not hit and we make note of a result that isn't real because smath forgot to recalculate. FYI. We must always make sure a recalculation is made after an edit in order to be sure that we are seeing the real effects of the edit on smath's behavior.

Imaginary number:
- imaginary i is reserved and smath confuses itself as explained below
- if reassigned in global environment i := 5, smath redefines i as integer 5 thereafter in the sheet correctly
- dynamic assistance will show as imaginary anywhere on the sheet before the reassignment and i -> 5 afterwards
- if used as an index in a free floating for loop (with no global reassignment prior), i will be reassigned properly
- HOWEVER, the dynamic assistance shows that if the for loop is RHS of an assignment, i is MISHANDLED
- i is not reassigned after function definition, this is correct
- i should be reassigned after function call is executed and this works correctly
- BUG: any i typed in anywhere on the sheet before or after for loop call is assumed by smath to be imaginary
- sometimes, doing so can cause other i's present after the function call to revert to imaginary
- sometimes moving an expression i-> will cause all i's after function call to revert to imaginary
- this only occurs when the change triggers smath to recalculate
- but only when smath forgets to recalculate the function call when it recalculates
- maybe smath is only recalculating below the i-> and if i-> is below the function call, the call isn't recalculated
- when the page is recalculated, the for loop's reassignment takes over again for all i's present after the for loop
- the for loop may not be the only example of this behavior and we should identify where it occurs
- BUG: is smath only sometimes recalculated and if so, why and why not?
- is function call sometimes not included, if so, why and why not?
- is this behavior a bug or feature?
- what are the rules for recalculation and what should I look for to make sure those rules are being properly followed?
-

Edited by user 17 December 2012 21:24:13(UTC)  | Reason: Not specified

Offline kilele  
#18 Posted : 17 December 2012 16:14:46(UTC)
kilele


Rank: Advanced Member

Groups: Registered
Joined: 30/03/2011(UTC)
Posts: 392

Was thanked: 132 time(s) in 113 post(s)
Originally Posted by: Rising Eagle Go to Quoted Post

imaginary i is reserved and smath confuses itself as explained below


suggestion: use the unicode i with macron

ī:=sqrt(-1)

After this assignment Smath suggests ī whenever the user presses i

Edited by user 18 December 2012 00:19:53(UTC)  | Reason: Not specified

kilele attached the following image(s):
imaginary.png
Offline mkraska  
#19 Posted : 17 December 2012 17:48:06(UTC)
mkraska


Rank: Advanced Member

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

Was thanked: 807 time(s) in 516 post(s)
Rising Eagle,

Thank you again for sharing the results of your reverse engineering research. The length of the posts in this topic seem to follow an exponential law. I am asking myself if we are not in a situation where we use SMath to solve problems that we would not have without it.

For me, the right balance between joy and frustration is controled by how deep one digs. Therefore I try to be pragmatic and just limit the search to structures and procedures that we definitly would have a benefit from, should they work reliably.

Things like embedding assignments whereever SMath fails to complain, are beyond what I want to practice and teach. I admit that such experiments are good challenges for assumed rules, but I would try to limit the use cases.

Top level requirement is that things should look and work like handwritten formulas and algorithms on paper. So what are the expectations to handwritten code?
- Variable scoping should be controlled by entities like function or procedures with well defined input and output rules.
- flow control statements like while, if, for, line shpuld not interfere with variable scoping.
- Visual representation and internal handling must allow to tell imaginary unit from variable i. A math program should allow for safe use of i as variable in loops without spoiling complex calculus.
- Everything must be unambiguous, such that you can reproduce everything from printout. An example is the difference between name indices and adress indices. These can be identified by the amount of space. This is not always (never, so to say) obvious to the uninitiated but safe once explained. We had more serious issues with sums and their arguments.

Some of these expectations are still not possible to meet. Others are possible to meet by choice of the autor of the sheet. Best case is where SMath enforces the sheets to meet these expectations by complaining in case of non-safe usage (this sometimes happens in matlaCool.

The final result of reverse engineering should be a minimal set of survival rules. Examples:
"Never use i as variable name"
"Never use symbolic optimization"
"Never use 3D diagrams"
"Never rely on local scope of variables"
"If in doubt (or better always) press the re-calculate button"
"If the formula could be misunderstood, set braces, even if SMath does not require this"
"Never embed assignment operations except in the body of control statements"

The better the program becomes the less such rules are required.



Martin Kraska

Pre-configured portable distribution of SMath Studio: https://en.smath.info/wi...th%20with%20Plugins.ashx
Offline Rising Eagle  
#20 Posted : 23 December 2012 14:17:00(UTC)
Rising Eagle


Rank: Member

Groups: Registered
Joined: 14/12/2012(UTC)
Posts: 23
Location: Neverland

Was thanked: 15 time(s) in 9 post(s)
Originally Posted by: kilele Go to Quoted Post
Originally Posted by: Rising Eagle Go to Quoted Post

imaginary i is reserved and smath confuses itself as explained below


suggestion: use the unicode i with macron

ī:=sqrt(-1)

After this assignment Smath suggests ī whenever the user presses i


This is good information. Once the preassigned value of a default symbol is assigned on the smath sheet to another symbol, the default symbol can be reassigned in any other way and used as desired. Definitely good to know.

I have an alternative to your suggested reassignment: i is the imaginary unit. As a unit, it should be treated as such and assigned to a blue character. Just type:

'i := i


PS - I would like to use italics. I could use a cut and paste from a character map, but I would rather use a ctl-key that works in the same way the ctl-g key transforms a roman letter to a greek letter. If anyone knows how, please reply. It's interesting, the blue i shows up in DA as an italic, but as a regular roman character on the smath sheet.

Edited by user 23 December 2012 14:18:39(UTC)  | Reason: Not specified

Rising Eagle attached the following image(s):
Screenshot.png
thanks 1 user thanked Rising Eagle for this useful post.
on 23/12/2012(UTC)
Users browsing this topic
Similar Topics
Prevent element assignment to line() statements or provide a real clear() function (Feature Requests)
by mkraska 22/06/2013 10:15:47(UTC)
2 Pages12>
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.