Addtional Result in Programming Line Function

Addtional Result in Programming Line Function - Is it possible to show more than one result when using the line (programming) function? - Messages

#1 Posted: 7/25/2023 8:53:53 PM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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 (14 KiB) downloaded 38 time(s).
#2 Posted: 7/25/2023 9:27:27 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

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.
#3 Posted: 7/25/2023 9:50:31 PM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

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
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#4 Posted: 7/25/2023 10:12:55 PM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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?
#5 Posted: 7/25/2023 10:37:06 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Wrote

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.
#6 Posted: 7/25/2023 10:41:46 PM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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
#7 Posted: 7/25/2023 10:56:15 PM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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?
#8 Posted: 7/25/2023 11:36:58 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Wrote

... 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 (6 KiB) downloaded 37 time(s).

Best regards.
Alvaro.
#9 Posted: 7/26/2023 1:49:02 AM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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 (21 KiB) downloaded 46 time(s).
Section Properties.sm (3 KiB) downloaded 40 time(s).
#10 Posted: 7/26/2023 7:06:18 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Sanity checked Smath Solver 30405

Maths Algo Style First UTILITIES Copy.sm (35 KiB) downloaded 36 time(s).
Maths Algo(x,y,u,v,code).sm (54 KiB) downloaded 33 time(s).
#11 Posted: 7/26/2023 10:33:32 AM
overlord

overlord

552 likes in 1332 posts.

Group: Moderator

I am trying to think simple.
Maybe sys() is what you looking for.

2023-07-26_16-32.png
#12 Posted: 7/26/2023 6:55:23 PM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

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 (5 KiB) downloaded 39 time(s).
2023-07-26 23_48_17-SMath Solver - [Blatt22_].png
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
3 users liked this post
Oscar Campo 7/26/2023 6:58:00 PM, NDTM Amarasekera 7/26/2023 10:51:00 PM, Alvaro Diaz Falconi 7/27/2023 12:04:00 AM
#13 Posted: 7/27/2023 12:03:41 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Hi. Pushing Martin's solution one little step further.

Blatt22 ver 2.sm (18 KiB) downloaded 39 time(s).
Blatt22 ver 2.pdf (82 KiB) downloaded 40 time(s).

Best regards.
Alvaro.
#14 Posted: 7/27/2023 12:06:06 AM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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.
#15 Posted: 7/27/2023 12:31:35 AM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

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:

[MATH lang:eng]SecondMomentOfAreaBoxGirder(w.tf, t.tf, w.bf, t.bf, d.wb, t.wbr, t.wbl, a.wbr, a.wbl)[/MATH]

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.


#16 Posted: 7/27/2023 4:19:12 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

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.
#17 Posted: 7/27/2023 8:52:52 PM
Peter Johns

Peter Johns

2 likes in 11 posts.

Group: User

Wrote

Wrote

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

#18 Posted: 7/27/2023 9:58:08 PM
overlord

overlord

552 likes in 1332 posts.

Group: Moderator

Wrote

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.
#19 Posted: 7/28/2023 5:44:09 AM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Wrote

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:

[MATH lang:eng]SecondMomentOfAreaBoxGirder(w.tf, t.tf, w.bf, t.bf, d.wb, t.wbr, t.wbl, a.wbr, a.wbl)[/MATH]

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 (12 KiB) downloaded 39 time(s).
Blatt22.png

Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#20 Posted: 7/28/2023 10:00:30 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

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.

  • New Posts New Posts
  • No New Posts No New Posts