User-defined Functions in SMath Studio

User-defined Functions in SMath Studio - A guide to the user-defined functions - Сообщения

#21 Опубликовано: 22.03.2016 23:21:29
Ruben Sidranski

Ruben Sidranski

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

Группа: User

Wrote

Wrote

Update ... In SMath --> Help --> Examples --> Handbooks? If so, the PDF file in post #1 did not appear as a download

Try this way:
SMath Menu --> Tools --> Plugins --> Extensions Manager --> Handbooks


Turns out it's the same location. Thank you
1 пользователям понравился этот пост
ioan92 23.03.2016 07:41:00
#22 Опубликовано: 23.03.2016 09:25:24
Jean Giraud

Jean Giraud

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

Группа: User

Wrote

When you say
"when a canvas variable is defined at function's definition, the value is stored..."
This really means:
"when a canvas variable is defined before the function's definition, the value is stored..."
before = above or left of the function (on the canvas)
Right?



You would install parameters up above the function definition in the case
parameters result from other specifics of the project, i.e: if parameters
result from some type of calculations. Otherwise, if the parameters are
"solid", put them below the definition at the point of usage, like near the
plot so you can play with the graph.
Words speak and may be confusing, image don't speak but verdict.

Jean

Forum Define Parametric.gif

1 пользователям понравился этот пост
Davide Carpi 23.03.2016 15:55:00
#23 Опубликовано: 23.03.2016 15:56:28
Davide Carpi

Davide Carpi

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

Группа: Moderator

Wrote

Davide,

When you say
"when a canvas variable is defined at function's definition, the value is stored..."
This really means:
"when a canvas variable is defined before the function's definition, the value is stored..."
before = above or left of the function (on the canvas)
Right?

Thanks


You're right, I'll use your formulation in next revision
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#24 Опубликовано: 23.03.2016 22:28:13
Ryan Freund

Ryan Freund

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

Группа: User

Sorry I should have shown with an example.

Another way to say it would be:

"when a canvas variable is already defined at the function's definition, the value is stored..."

I suppose any of those works. It's just that when you read the word "at" you think that it is being somehow defined in the function itself.

Anyway, Thanks for confirming.
#25 Опубликовано: 24.03.2016 00:09:31
Jean Giraud

Jean Giraud

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

Группа: User

No "value is stored" ... the algo is stored,
implicit of the associated argument. In this
example, the argument is β indexed, stored
in the algo stack.

Jean

Forum Define Scalar Function.gif


Integrate Scalar Express.sm (11 КиБ) скачан 57 раз(а).
#26 Опубликовано: 19.09.2016 15:54:10
Martin Kraska

Martin Kraska

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

Группа: Moderator

0.98.6103

It seems that certain changes happened in what is allowed as formal arguments in function definitions. I compare the pdf and the .sm file of the user function guide.

- dummy argument: empty string no more allowed, empty placeholder (type #) is accepted instead.
- unit names are now protected, they stick to their meaning no matter what value is used in the function call.

2016-09-19 20_49_06-SMath Studio Desktop - [Userfunctions.sm_].png
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#27 Опубликовано: 19.09.2016 17:53:59
Davide Carpi

Davide Carpi

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

Группа: Moderator

Thank you Martin,

yes these behaviors were unintentionally introduced by Andrey in 0.98, patched recently; I'll update this document ASAP (here and in the EM too)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#28 Опубликовано: 19.09.2016 17:59:56
Martin Kraska

Martin Kraska

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

Группа: Moderator

Wrote

Thank you Martin,

yes these behaviors were unintentionally introduced by Andrey in 0.98, patched recently; I'll update this document ASAP



I don't mind these changes to remain. I think the empty placeholder is much better visual clue to something that does not matter than the empty string (and requires less typing).

I also would suggest to allow leaving the placeholder empty if no input to the function is required. Just as with DocumentDirectory()
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#29 Опубликовано: 19.09.2016 22:39:07
Jean Giraud

Jean Giraud

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

Группа: User

Wrote

I think the empty placeholder is much better visual clue



Agreed 100% Martin ... 'x' makes "conventional sense" also.
Anything else => for the birds .
#30 Опубликовано: 21.09.2016 12:18:56
Martin Kraska

Martin Kraska

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

Группа: Moderator

Hello Davide, I refer to your tips in the guide to user-defined functions:

- if you are running NUMERICAL calculations inside the function:
     - if you don't want/need values from outside, you have to check that everything is defined
         inside the function  ("pure" function -> copy the function in an empty worksheet, run it 
         directly ->  shouldn't run if something is missing in the function)
     - if you use unknowns to get values from outside the function:
         1) document somewhere the dependancies (f.e. in the description)
         2) test it in an empty worksheet, define the dependacies and check the consequences of
               the loss of any single dependancy (A,B,C -> keep A and B, remove C, then keep A and C
               and remove B, etc..)

- if you are running SYMBOLICAL calculations inside the function, you may need to choose
    hardly predictable unknowns' names (such as x#, _x, _x_, #x, §x, ...) to avoid
    unintentional replacements from the canvas;


I know that using strange variable names within functions is common practice in SMath programming. However, somehow I have forgotten why we do this or at least fail to explain it.

Could you please give an example for an unintended replacement which has to be avoided by strange names?
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#31 Опубликовано: 21.09.2016 13:32:27
Davide Carpi

Davide Carpi

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

Группа: Moderator

Currently I'm reworking the document, I think that this section will be changed to be more clear.

Leaving aside [SS-111]/[SS-2327] (this is a bug, Andrey will work on it), issues are related to an existing variable on the parent level (commonly the canvas, but you can extend the logic to nested functions), and a variable with the same name in the function; cases in my knowledge:
  1. unintentional pass-by-reference: this is not really a problem if you pay attention to it; if you don't want to pay attention, you can use the "strange variables' names" approach (not really a best practice, but it is a way); this is the only case in which you can modify unintentionally external variables from inside the function.
    2016-09-21 18_28_16-SMath Studio Desktop - [name_intef_issue_symb.sm_].png

  2. Matrices/systems: if are not initialized within the procedure and you write only some elements or you change the sizes; this may affects the expected result.
    2016-09-21 18_28_39-SMath Studio Desktop - [name_intef_issue_symb.sm_].png

  3. Symbolic calculations: you assign a local unknown to a variable, then you do stuff like differentiation thinking that the variable contains an unknown; this may affects the expected result. If the worksheet is sharable or with complex logic, hardly predictable unknown names here are almost mandatory IMO (plus it might help to identify that is a generic unknown).
    2016-09-21 18_28_05-SMath Studio Desktop - [name_intef_issue_symb.sm_].png

    In the example above, this is the logic when you evaluate f(3):
    1. v1 stores 13 (3+2*5), because b=5 exists on the top level;
    2. v2 stores 3+2*c, because c doesn't exists anywhere;
    3. o1 stores 2 (expected value), because the order of computation: differentiation is made, then the assignments looks to replace unknowns from RHS to store it in the variable on the LHS;
    4. o2 stores 2 (expected value), because v2 contains the unknown c;
    5. o3 stores 0, because you are differentiating 13;


P.S. please note that the canvas' variables here are placed after functions' definitions, to remark that the logic applies when you evaluate the function.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 пользователям понравился этот пост
Martin Kraska 21.09.2016 17:06:00
#32 Опубликовано: 21.09.2016 17:21:44
Martin Kraska

Martin Kraska

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

Группа: Moderator

Wrote


  • Symbolic calculations: you assign a local unknown to a variable, then you do stuff like differentiation thinking that the variable contains an unknown; this may affects the expected result. If the worksheet is sharable or with complex logic, hardly predictable unknown names here are almost mandatory IMO (plus it might help to identify that is a generic unknown).
    2016-09-21 18_28_05-SMath Studio Desktop - [name_intef_issue_symb.sm_].png



  • Thank you Davide,

    this is exactly what I was looking for. Using Clear(), you can close the canvas-bypass for variables used inside the functions. This is just the symbolic version of initialization.
    As long as you don't use the internal names as arguments in the function call, you are safe.
    Thus, I have to admit that still the names of the internal variables matter.

    clear.sm (8 КиБ) скачан 48 раз(а).

    Clear.png
    Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
    #33 Опубликовано: 21.09.2016 18:46:35
    Jean Giraud

    Jean Giraud

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

    Группа: User

    I didn't put much effort trying to understand what f(x)
    should do. For sure it's not going to cut the mustard for
    Engineers. If I would have to QA such a mess, I would return
    red crossed. On the other hand, "Clear" does nor clear,
    it does not empty the variable/constant. It will clear pi for 1
    and it does not clear predefined items before maple symbolic evaluation.
    Smath clear is nothing compared to Mathcad that creates "inexistance".

    Forum Unknown Stuff.gif
    #34 Опубликовано: 21.09.2016 18:58:49
    Jean Giraud

    Jean Giraud

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

    Группа: User

    The compactum is very useful, but not always compatible.
    There are more instances of "not compatible".

    Compactum.gif
    #35 Опубликовано: 22.09.2016 00:42:19
    Jean Giraud

    Jean Giraud

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

    Группа: User

    ... those things you want to compact in some way must be tested
    on real applications. Bits and pieces are no proof. This document
    shows two cases of failure [1: the compactum, 2: Algo]

    Jean

    Spline Quadratic [Model].sm (89 КиБ) скачан 58 раз(а).

    #36 Опубликовано: 22.09.2016 05:47:40
    Davide Carpi

    Davide Carpi

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

    Группа: Moderator

    Wrote

    Using Clear(), you can close the canvas-bypass for variables used inside the functions. This is just the symbolic version of initialization.


    Yes, I guess it might be the best approach (probably the most clean even for occasional readers).

    Wrote

    On the other hand, "Clear" does nor clear,
    it does not empty the variable/constant. It will clear pi for 1
    and it does not clear predefined items before maple symbolic evaluation.
    Smath clear is nothing compared to Mathcad that creates "inexistance".


    You are right; this is because Clear() is not a built-in function, just rides a feature of line() wrt anything but functions.

    Two points to have a built-in clear()
    If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
    #37 Опубликовано: 22.09.2016 11:21:49
    Jean Giraud

    Jean Giraud

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

    Группа: User

    Wrote

    Two points to have a built-in clear()



    Yes Davide, but simpler than a "clear function", just like Mathcad:
    assign undefined ... a:= ... f(x):=

    In Mathcad, undefined would evaporate 'a'. If more paramaters were previously used,
    undefining all individually would render the symbolic "virgin" for expansion.
    That was Mathcad 11 and earlier versions, at least back to Mathcad 8 Pro.
    Alike evaporating parameters, Mathcad evaporates a function, f(x):=
    past undefining f(x):= 'f' can be re-used.

    In fact, we have it all naturally in Smath.

    1. Assume f(a,b,c,x) was a user function, now just define f(x):=ln(x)
    2. To clear parameters a,b,c ... just define a:=Shift+" [the blanking tool].

    Not completely proofed.

    Jean

    Maple coeffs NumDen [Companion].sm (18 КиБ) скачан 59 раз(а).

    Visit the end of the work sheet.

    #38 Опубликовано: 22.09.2016 11:58:25
    Martin Kraska

    Martin Kraska

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

    Группа: Moderator

    Wrote



    Yes Davide, but simpler than a "clear function", just like Mathcad:
    assign undefined ... a:= ... f(x):=




    I agree with the proposal to have a:# or f(1):# or f(#):x being understood as deleting a definition. That could even be understood by non-SMath people, although an explicit "undefined" would be less subject to interpretation.
    Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
    #39 Опубликовано: 22.09.2016 13:44:36
    Andrey Ivashov

    Andrey Ivashov

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

    Группа: Super Administrator

    Wrote

    Leaving aside [SS-111]/[SS-2327] (this is a bug, Andrey will work on it)


    Fixed. Thank you!
    1 пользователям понравился этот пост
    Davide Carpi 22.09.2016 19:30:00
    #40 Опубликовано: 23.09.2016 01:58:36
    Jean Giraud

    Jean Giraud

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

    Группа: User

    Wrote

    I agree with the proposal to have a:# or f(1):# or f(#):x being understood as deleting a definition. That could even be understood by non-SMath people, although an explicit "undefined" would be less subject to interpretation.



    Good suggestion except that the gyzma # is not accessible in Smath.
    Not directly accessible via the Shift 3 keyboard, neither via the
    extended Alt 035. Smath 5346, XP Home SP3.

    My western keyboard is OK, but ignored by Smath 5346.
    gyzma from keyboard # ... gyzma Alt 035 #

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