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? - Сообщения

#1 Опубликовано: 25.07.2023 20:53:53
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 КиБ) скачан 31 раз(а).
#2 Опубликовано: 25.07.2023 21:27:27
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: 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 Опубликовано: 25.07.2023 21:50:31
Martin Kraska

Martin Kraska

1222 сообщений из 2150 понравились пользователям.

Группа: 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 Опубликовано: 25.07.2023 22:12:55
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 Опубликовано: 25.07.2023 22:37:06
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: 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 Опубликовано: 25.07.2023 22:41:46
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 Опубликовано: 25.07.2023 22:56:15
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 Опубликовано: 25.07.2023 23:36:58
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: 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 КиБ) скачан 32 раз(а).

Best regards.
Alvaro.
#9 Опубликовано: 26.07.2023 01:49:02
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 КиБ) скачан 41 раз(а).
Section Properties.sm (3 КиБ) скачан 37 раз(а).
#10 Опубликовано: 26.07.2023 07:06:18
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Sanity checked Smath Solver 30405

Maths Algo Style First UTILITIES Copy.sm (35 КиБ) скачан 30 раз(а).
Maths Algo(x,y,u,v,code).sm (54 КиБ) скачан 29 раз(а).
#11 Опубликовано: 26.07.2023 10:33:32
overlord

overlord

549 сообщений из 1332 понравились пользователям.

Группа: Moderator

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

2023-07-26_16-32.png
#12 Опубликовано: 26.07.2023 18:55:23
Martin Kraska

Martin Kraska

1222 сообщений из 2150 понравились пользователям.

Группа: 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 КиБ) скачан 35 раз(а).
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 пользователям понравился этот пост
Oscar Campo 26.07.2023 18:58:00, NDTM Amarasekera 26.07.2023 22:51:00, Alvaro Diaz Falconi 27.07.2023 00:04:00
#13 Опубликовано: 27.07.2023 00:03:41
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: User

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

Blatt22 ver 2.sm (18 КиБ) скачан 35 раз(а).
Blatt22 ver 2.pdf (82 КиБ) скачан 36 раз(а).

Best regards.
Alvaro.
#14 Опубликовано: 27.07.2023 00:06:06
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 Опубликовано: 27.07.2023 00:31:35
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 Опубликовано: 27.07.2023 16:19:12
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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 Опубликовано: 27.07.2023 20:52:52
Peter Johns

Peter Johns

2 сообщений из 11 понравились пользователям.

Группа: 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 Опубликовано: 27.07.2023 21:58:08
overlord

overlord

549 сообщений из 1332 понравились пользователям.

Группа: 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 Опубликовано: 28.07.2023 05:44:09
Martin Kraska

Martin Kraska

1222 сообщений из 2150 понравились пользователям.

Группа: 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 КиБ) скачан 32 раз(а).
Blatt22.png

Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#20 Опубликовано: 28.07.2023 10:00:30
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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.

  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений