Addtional Result in Programming Line Function - Is it possible to show more than one result when using the line (programming) function? - Сообщения
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 раз(а).
Also, you can use eval(line(...)).
Best regards.
Alvaro.
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.
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?
WroteThanks, 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:
Best regards.
Alvaro.
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
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?
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.
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 раз(а).
Maths Algo Style First UTILITIES Copy.sm (35 КиБ) скачан 30 раз(а).
Maths Algo(x,y,u,v,code).sm (54 КиБ) скачан 29 раз(а).
Blatt22.sm (5 КиБ) скачан 35 раз(а).
Blatt22 ver 2.sm (18 КиБ) скачан 35 раз(а).
Blatt22 ver 2.pdf (82 КиБ) скачан 36 раз(а).
Best regards.
Alvaro.
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.
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.
WroteThe 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.
WroteWroteThe 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
WroteJean,
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.
WroteThanks 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 раз(а).
WroteGetting 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.
-
Новые сообщения
-
Нет новых сообщений