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

Notification

Icon
Error

Login


2 Pages12>
Options
Go to last post Go to first unread
Offline P.J  
#1 Posted : 26 July 2023 02:53:53(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
I am using the Line (programming) function for a calculation.

Firstly, apologies, I cannot get the line function to display as maths in the forum so I have had to bodge it in with some text.

Say I have a line function like this:

a:=1
b:=2

|c:=b^2+2 . . = 7
|c
|a+c

Then I would like to be able to display the result of 'c' in the line function. The variable scope of c is limited to within the line function (which is good), but it means I cannot see the value.

So it would look something like this:

|c:=b^2+2 . . = 7
|c = 6
|a+c

I have attached a worksheet showing this in more detail.

Thanks in advance

Additional Programming Result.sm (15kb) downloaded 11 time(s).

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

Offline Razonar  
#2 Posted : 26 July 2023 03:27:27(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Hi. Maybe this is what you are looking for: "Defining a function as f(x) := 1*(| ... ) make all variables in line(...) as globals." from here.

Clipboard01.png

Also, you can use eval(line(...)).

Best regards.
Alvaro.
Offline mkraska  
#3 Posted : 26 July 2023 03:50:31(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)
Also, wrapping the block in eval() makes all variables global. This doesn't even require assignment or display operators.
In fact, you also could use other structures like lists { or matrices [] to collect definitions. Yet, only the line operator warrants a certain sequence of execution.

2023-07-26 02_46_42-SMath Solver - [Additional Programming Result.sm_].png

Edited by user 26 July 2023 03:53:58(UTC)  | Reason: Not specified

Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Offline P.J  
#4 Posted : 26 July 2023 04:12:55(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Thanks, Alvaro.

I wanted to keep the variable scope local; to reduce the possibility of errors I want to avoid reuse of global variable names / redefinition of global variables.

I could make the variables global using the methods you suggest and then delete them from scope with the Clear() function but:

- If the included sheet contains a lot of variables, which it probably will, then I would need to clear each of them individually (and make sure that I had found all of them)
- The Clear() function does not appear to work for variables that come from imported sheets. But I could assign it to something meaningless like x:=nul
- If a variable name from the included sheet matches one already in global scope then this would clear the global scope variable as well
- Less syntactically sugared

Is there a way to localise the scope of variables, other then the Line function?
Offline Razonar  
#5 Posted : 26 July 2023 04:37:06(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Originally Posted by: P.J Go to Quoted Post
Thanks, Alvaro.

I wanted to keep the variable scope local; to reduce the possibility of errors I want to avoid reuse of global variable names / redefinition of global variables.
...
Is there a way to localise the scope of variables, other then the Line function?


I don't know how to localize variables, but know how to "globalize" them. For a more elaborate selection about which variables you want outside of a function body, you can redefine inside it the functions arguments, like this:

Clipboard01.png

Best regards.
Alvaro.
Offline P.J  
#6 Posted : 26 July 2023 04:41:46(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Thanks Martin,

I could do the following:

1. In the included worksheet define an m x 2 matrix, where m is the number of outputted variables, and each row would be [ "x", x ]
2. Use a Line function to assign this matrix to a global variable
3. Assign the particular value of from the matrix I want to a global variable

The issues with this, none of which are terminal, are:

- The description of the variable in the matrix has to be text, rather than maths
- Does not work well with units, for example 1 kN would be shown as 1000.000 kg m / sec^2
- Again, not very syntactically sugared, particularly if giving the worksheet to somebody who is mathematically literate, but maybe not an SMath User
Offline P.J  
#7 Posted : 26 July 2023 04:56:15(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Thanks Alvaro,

Can I just check what you have done. I assume that above the maths you have shown in your post, you have separately defined xo:=5, y:=7 etc. Therefore xo, y and ao are global variable after f() because they were global beforehand? Or have I missed something?
Offline Razonar  
#8 Posted : 26 July 2023 05:36:58(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Originally Posted by: P.J Go to Quoted Post
... I assume that above the maths you have shown in your post, you have separately defined xo:=5, y:=7 etc. ...


Hi. Nope. What you see is the entire worksheet, there are not hidden code there, sorry for not having attached it, for laziness. xo, y & ao turns global only after calling f(xo,y,ao). Before they could have some value or be undefined.

That ability to modify the values of the arguments passed to a function, whether or not they are defined, is unique to SMath and is not present in maple, mathematica or matlab, at least not in a simple way.

additional result.sm (7kb) downloaded 9 time(s).

Best regards.
Alvaro.

Edited by user 26 July 2023 05:40:34(UTC)  | Reason: Not specified

Offline P.J  
#9 Posted : 26 July 2023 07:49:02(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Alvaro,

I didn't know you could pass variable by reference, rather than by value, into a function.

I think that is a solution to the problem. It also has the advantage that it can insert all the variables into global scope in one go.

For completeness I have added your method to the my example worksheet, and also upload a second worksheet ( "Section Properties" ) so the calculation can actually be done. The "path" variable should automatically point SMath at the directory where "Additional Programming Result 2.sm", so it will find "Section Properties.sm" providing they are both saved in the same directory. If not then the path variable can be adjusted to suit.


Alvaro, many thanks once again for your help with this

Additional Programming Result 2.sm (22kb) downloaded 18 time(s).
Section Properties.sm (4kb) downloaded 15 time(s).

Edited by user 26 July 2023 07:51:02(UTC)  | Reason: Marked as solved

Offline Jean Giraud  
#10 Posted : 26 July 2023 13:06:18(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Sanity checked Smath Solver 30405

Maths Algo Style First UTILITIES Copy.sm (36kb) downloaded 9 time(s).
Maths Algo(x,y,u,v,code).sm (55kb) downloaded 7 time(s).
Offline overlord  
#11 Posted : 26 July 2023 16:33:32(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,128
Turkey

Was thanked: 509 time(s) in 339 post(s)
I am trying to think simple.
Maybe sys() is what you looking for.

2023-07-26_16-32.png
Offline mkraska  
#12 Posted : 27 July 2023 00:55:23(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)
Casting the computations into block functions with no reference to context variables from inside removes any restrictions on variable names in the sheet. With a proper description such functions become self-documenting. This way you can create portable collections of such functions (and store them as snippets).

Blatt22.sm (6kb) downloaded 10 time(s).
2023-07-26 23_48_17-SMath Solver - [Blatt22_].png
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
thanks 3 users thanked mkraska for this useful post.
on 27/07/2023(UTC),  on 27/07/2023(UTC),  on 27/07/2023(UTC)
Offline Razonar  
#13 Posted : 27 July 2023 06:03:41(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Hi. Pushing Martin's solution one little step further.

Blatt22 ver 2.sm (19kb) downloaded 9 time(s).
Blatt22 ver 2.pdf (83kb) downloaded 11 time(s).

Best regards.
Alvaro.
Offline P.J  
#14 Posted : 27 July 2023 06:06:06(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Thanks Overlord,

The sys() function does exactly what I wanted in a really simple way. You can use an index on the result to pick out the single value you want, whilst having the others displayed.

Alvaro's solution is still also very useful, as it introduces a number of other possibilities as well.

I think I'm getting a really good toolkit to attack this problem, so thanks once again to both of you.
Offline P.J  
#15 Posted : 27 July 2023 06:31:35(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Thanks Martin,

Your implementation is simpler than mine. The reason I went for a slightly more complex version was to reduce the possibility of errors.

For example, if you have a function that computes the second moment of area of a box girder then you would be listing lost of dimensions in the function definition with similar names all in the same units, for example:

SecondMomentOfAreaBoxGirder(w.tf, t.tf, w.bf, t.bf, d.wb, t.wbr, t.wbl, a.wbr, a.wbl)

Getting one of these around the wrong way may have a significant effect on the result, but be hard to spot. If they are listed in the line function, as I have done, then the order is not important, so one way of generating errors is removed.

The reason I want to use included sheets for this is because some of the functions were becoming very long and a little unwieldy. For complex functions, I find having a worksheet become a function a simpler way to do things, but there are instances where a simple function would be easier.

As above, having more options is good. So thanks again, Martin.


Offline Jean Giraud  
#16 Posted : 27 July 2023 22:19:12(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: P.J Go to Quoted Post
The reason I want to use included sheets for this is because some of the functions were becoming very long and a little unwieldy.

The two attachments post 10 support include.
From there, at the working work sheet level,
algo code will be in-situ command line wrt applicable parameters.
Sanity the mutual parameters interaction. Vectorize as needed, export.
Offline P.J  
#17 Posted : 28 July 2023 02:52:52(UTC)
P.J


Rank: Member

Groups: Registered
Joined: 25/07/2023(UTC)
Posts: 11

Was thanked: 2 time(s) in 1 post(s)
Originally Posted by: Jean Giraud Go to Quoted Post
Originally Posted by: P.J Go to Quoted Post
The reason I want to use included sheets for this is because some of the functions were becoming very long and a little unwieldy.

The two attachments post 10 support include.
From there, at the working work sheet level,
algo code will be in-situ command line wrt applicable parameters.
Sanity the mutual parameters interaction. Vectorize as needed, export.


Jean,

I'm sorry, I don't understand what you are trying explain. Would you be able to provide some additional details, please?

Thanks,
Peter

Offline overlord  
#18 Posted : 28 July 2023 03:58:08(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,128
Turkey

Was thanked: 509 time(s) in 339 post(s)
Originally Posted by: P.J Go to Quoted Post
Jean,

I'm sorry, I don't understand what you are trying explain. Would you be able to provide some additional details, please?

Thanks,
Peter

Don't bother.
You are not first, you won't be last.
He is unofficial troll of this forum.
He is not here to help you.
Just to promote his useless samples.
Or to hijack other people's posts.
Nothing more.
Offline mkraska  
#19 Posted : 28 July 2023 11:44: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)
Originally Posted by: P.J Go to Quoted Post
Thanks Martin,

Your implementation is simpler than mine. The reason I went for a slightly more complex version was to reduce the possibility of errors.

For example, if you have a function that computes the second moment of area of a box girder then you would be listing lost of dimensions in the function definition with similar names all in the same units, for example:

SecondMomentOfAreaBoxGirder(w.tf, t.tf, w.bf, t.bf, d.wb, t.wbr, t.wbl, a.wbr, a.wbl)

Getting one of these around the wrong way may have a significant effect on the result, but be hard to spot. If they are listed in the line function, as I have done, then the order is not important, so one way of generating errors is removed.

The reason I want to use included sheets for this is because some of the functions were becoming very long and a little unwieldy. For complex functions, I find having a worksheet become a function a simpler way to do things, but there are instances where a simple function would be easier.

As above, having more options is good. So thanks again, Martin.




Here is a easy way to get a summary for procedural implementations of algorithms. You just insert a table region, write your function to the placeholder and do some format adjustments. In the example I just switched from fixed to variable width of table columns and set the font to arial.

Unfortunately, the decimal separator setting is not respected.

This procedure has full control over units in results display and can be extended to alvaros generalized approach (with then input and output flags being dynamic).

Blatt22.sm (13kb) downloaded 11 time(s).
Blatt22.png

Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Offline Jean Giraud  
#20 Posted : 28 July 2023 16:00:30(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: P.J Go to Quoted Post
Getting one of these around the wrong way may have a significant effect on the result, but be hard to spot.

That's what I have explained at the in-situ command line,
sanity check mutual compatible parameters
to avoid hard to spot gross mistakes.
BTW, include is more at the single user level.

Users browsing this topic
Guest
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.