Multiline functions and functions as function arguments

Multiline functions and functions as function arguments - Сообщения

#1 Опубликовано: 24.06.2010 07:28:29
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

24.02.2010
Wrote

just for the curiosity, tell me what are your plans concerning multiline functions and functions as function arguments, or some other kind of this ability. Very often I wish that was implemented in SMath (I commented these things on an "brainstorming" topic on the Forum ).I would just like to know what are your plans about it, nothing else.


Wrote

Regarding multiline functions. I also see, that this should be implemented to extend abilities to create functions and I plan to implement it near time.

Functions as function arguments not in my asap list, because I have no ideas of how to do this... I mean, that one of the important differences between Mathcad and SMath Studio is that functions and variables can be specified with the same names without covering:

f:=2;
f(f):=f^2;
f(f)=4;
According to this, I don't know how to enable this functionality... But I believe it possible, so if I'll understand how, I'll do.


Wrote

Yes, you are quite right. I was thinking about that but did not mentioned. I did not have any idea yet how to distinguish the function arguments,actually - how to make the difference between the variable and function name. I thought that the inclusion of text variables might lead to the solution. Then the problem would be how to distinguish the text constant from the function. I do not know?
Is there any possibility to have something like:

x:=5
y:=6
g(a,b):=a+sin(b)
f(x,y,h(x,y)):=x+y+h(x,y)
result:=f(x,y,g(x,y))
or
x:=5
y:=6
g(a,b):=a+sin(b)
f(x,y,"fun h(2)":=x+y+h(x,y)
result:=f(x,y,"fun g(2)"
Which means everything after "fun" (or something like that) must be a function name with number of arguments in parenthesis.
I do not think this could be implemented, but I believe you will find the best way.

Concerning functions, the SMath way gives improved functionality and greater flexibility comparing to Mathcad. I believe you will find the way to include functions as function arguments and multiline function.


Wrote

Great ideas! Not sure about using text to define functions in arguments, but the first variant is rather interesting! Also, I've combined both solutions you have mentioned and got this:

x:=5
y:=6
g(a,b):=a+sin(b)
f(x,y,h(2)):=x+y+h(x,y)
result:=f(x,y,g(x,y))
I think it is better, because f(x,y,h(x,y)) will cause misunderstandings about arguments (can we use the same arguments within main function's arguments and within the sub-function? If yes, then programs logic should be more complex, because in that case same names don't mean the same variables (?), otherwise constraints should be added (actually, I don't like constraints )). As I see, f(x,y,h(2)) also has some negative sides (mostly because f(2) looks like we try to evaluate function f using argument 2).

Will think about this...


Wrote

I am glad that I could be of any help. Your solution is very interesting. I am also not sure about it but I think that when you define a function like:
f(x,y,h(2)):=
you might use something else to note that this particular function has two arguments. Something not to be confused with calling the function with a constant, say
f(x,y,h(2args)):=
f(x,y,h(_2_)):=
f(x,y,h(%2)):=

whatever you find it suitable, do not know.

After that, the possibility of calling the function with:

result:=f(x,y,g(x,y)) is great


24.02.2010
Wrote

On my second thoughts, this might be the best solution . Fictive arguments in the function definition are declared fine and understandable:
f(x,y,h(2)):=
Because this is a function definition, hardly we can be confused. Fictive argument which contains (n) after its name, where n is an integer, could be a function with n fictive arguments. Anything else is a variable (numerical or symbolical). I might be wrong of course not seeing any other nagative sides, but the function arguments are not evaluated i.e. there could not be an expression as a function argument.


23.10.2010
Wrote

I remember about functions as function arguments and couple of days ago I started to implement it but have a problem - I still can't find a best way how to represent this feature for user. It's good enough for me, if definition for such functions will be:
[MATH]f(g(2);c)←g(1;2)+c[/MATH]
but I not really like the ways I see how this definitions can be used:

  1. [MATH]g(a,b)←a*sqrt(b)[/MATH]
    [MATH]f(g(a;b);3)=#[/MATH]
  2. [MATH]f(el(x;1)*sqrt(el(x;2));3)=#[/MATH]
  3. [MATH]f(function(a*sqrt(b);a;b);3)=#[/MATH]

I think that all this variants is not user-friendly enough. The most acceptable way, of course, is the first one, but it requires separate definition of the function before using it in argument... Maybe I don't see anything simplest?
...
If we will understand how make it possible for user, I promise, I will implement it as soon as I can. This days I have a time and possibilities to do this work.

#2 Опубликовано: 25.06.2010 20:29:08
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Work started! Here are my first results:


Regards.
1 пользователям понравился этот пост
Radovan Omorjan 26.06.2010 14:40:00
#3 Опубликовано: 26.06.2010 02:13:12
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

YES

Very good start Andrey!

Looking forward to seeing some test SMath version with these features.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#4 Опубликовано: 27.06.2010 18:37:05
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Strange behavior of Mathcad found:


Same situation in SMath Studio:


Any idea why Mathcad can't evaluate this properly?

P.S.: This is a test file I've made to check SMath Studio.
#5 Опубликовано: 28.06.2010 05:59:08
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Hello Andrey,

As far as I know, Mathcad does not use something like f(a,y):=b(a(x),x)+x in generall. Function arguments are only names (variale or function names). Function argument in the user defined function could not be "a(x)" - it must be just "a". Although, there are few exceptions. I do not use Mcad 14 but here is the image from an older Mcad version. However, It should be noted that there are different symbolic engines applied. Mcad 14 uses MuPad and older versions use Maple.
Please look at this image. It seems Ok.

But, if you change "v" into "v(x)" the results here is quite strange and sometimes unpredictible. I suppose this is because the Maple engine is involved here, and the problematic Mathcad use of Maple.

On the other hand, it seems that Mcad 14 can cope with it and I suppose that the problem is in y which is a constant not an undefined (symbolic) variable.

On the other hand SMath must work with the expression like v() pointing that this is a function with arguments (I hope I am right).
Regarding that those are just starting efforts, SMath example seems to me more understandable.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#6 Опубликовано: 28.06.2010 06:44:19
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Hello.

Ah, yes. You're right, it is because I call m(v(x),x)... it should be m(v,x). Thank you!

Regards.
#7 Опубликовано: 28.06.2010 07:12:34
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

You are welcome

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#8 Опубликовано: 28.06.2010 21:26:04
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Live binaries updated, so current realization of functional programming can be tested using SMath Studio Live.

[LIVE width=529 height=266]http://smath.info/live/?file=1269[/LIVE]

Regards.
#9 Опубликовано: 29.06.2010 03:06:13
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Hello,

Just testing and tried to accept the logic behind this
[LIVE width=510 height=629]http://smath.info/live/?file=1273[/LIVE]

Should it be this way?

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#10 Опубликовано: 29.06.2010 06:44:44
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Some more testing (very nice )
[LIVE width=523 height=292]http://smath.info/live/?file=1278[/LIVE]
But if I try:
G(5,f(x))—>
There will not be any result. It seems Live version does not like it, at the moment.
Andrey, If you can not reproduce this - it must be my computer fault.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#11 Опубликовано: 29.06.2010 07:30:55
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Thank you, Radovan! I see now at least two errors. Will fix them.

Regarding "This might be error 3-arguments function on the LHS?". Sorry, not sure I understand what you mean.

P.S.: Live really doesn't like G(5,f(x))—> It captures 100% of one of server's CPUs and doesn't allow any other request till 30 seconds... interesting...
#12 Опубликовано: 29.06.2010 10:21:20
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

You are welcome Andrey, of course
Wrote


Regarding "This might be error 3-arguments function on the LHS?". Sorry, not sure I understand what you mean.


I'll try to be more specific. If I understood well, there are two ways to work with fun as fun arguments.
1. If we use dummy arguments like this
F(a,b,c,f(x,y,z)):=f(a,b )+f(c,d)
2. This should be the same
F(a,b,c,f(3)):=f(a,b )+f(c,d)

"a,b,c" should be used from the argument list ond the LHS of the function definition, and "d" is from the worksheet (might be defined previously or not).
I think that the LHS of the function definition tell us that on the RHS should be a function named "f" with 3 (three) arguments "x,y,z" dummy arguments or "3" in number. But, as we could see, on the RHS there is function "f" but not with three but two fictive (dummy) arguments f(a,b ) and f(c,d). If I understood it well, this must be regarded as an error in defining the function "F". Actually, if I try to use "F" this way, it will report that f(#,#,#) does not exist. I hope I am right.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#13 Опубликовано: 29.06.2010 10:54:14
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

If I understood you well, then it is because of the important difference between SMath Studio and Mathcad - SMath Studio allows to create any number of the variables and functions with the same name if only these items has different signature (signature for every primitive in SMath Studio is its pair of name and number of arguments).

See these examples to understand how it works with functions in arguments:

[LIVE width=398 height=134]http://smath.info/live/?file=1282[/LIVE]

[LIVE width=398 height=197]http://smath.info/live/?file=1281[/LIVE]

Best regards, Andrey Ivashov.
#14 Опубликовано: 29.06.2010 11:50:58
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Yes, I understand that. I thought something like this
[LIVE width=251 height=294]http://smath.info/live/?file=1283[/LIVE]
It does not matter what the second argument of function "F" is - it is simply redundant in the definition. We can put whatever we like (I think). My doubt is - could this be regarded as an error in the definition -f(x,y,z) is not used - or simply ignored?

Hmm...I might have the same doubts some time ago about it, but might have forgotten - sorry. Anyway, no harm done - just a user have been mistaken.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#15 Опубликовано: 29.06.2010 12:10:28
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Ok, its clear now. This is a rather common thing - I think it is not regarding functions in arguments.
F.e.: [MATH lang=eng]f(a):5[/MATH]. Should we raise an error or not?

I believe that this should be possible for user to make such redundant arguments. Because some persons may want to define all arguments of the function before creating of the main function's logic or some similar case.

Regards.
#16 Опубликовано: 29.06.2010 12:45:47
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Yes, you are right. I agree with you . This might be quite useful.

I am now quite sure that you already mentioned this, but I forgot - sorry.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#17 Опубликовано: 29.06.2010 20:55:42
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Errors fixed, SMath Studio Live updated. Hint: click on the expression with error to read a message.

[LIVE width=276 height=215]http://smath.info/live/?file=1287[/LIVE]

Regards.
#18 Опубликовано: 30.06.2010 04:37:15
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Hello Andrey,

Some more testing.
[LIVE width=380 height=384]http://smath.info/live/?file=1290[/LIVE]
Did I overlook something, or this was not implemented yet?

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#19 Опубликовано: 30.06.2010 06:10:44
Andrey Ivashov

Andrey Ivashov

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

Группа: Super Administrator

Привет, Radovan.

When you call g(x,f(x)) SMath Studio substitute x wherever it appears in expression (it is a vector in your example, defined above), so to get the result you need just change it to something like g(x,f(someName)). But it is an error nevertheless - program should show a message about this issue like it does on g(x,f(3)).

Regards.
#20 Опубликовано: 30.06.2010 08:21:11
Radovan Omorjan

Radovan Omorjan

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

Группа: Moderator

Zdravo, Andrey

Yes, you are right. It did not came to my mind. I checked that out.
I just need to put say g(x,f(a)) and it will work. The variable "a" must not be previously defined - The result would be zero then - if it was a scalar, and vector or matrix of zeros - if it was a vector or matrix. As you said, this should be issued as error.

On the other hand if you use f(x):=sin(x) -the intrinsic function-the error will be reported when using g(x,f(x))= as
"Argument must be scalar"

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений