Multiline functions and functions as function arguments

Multiline functions and functions as function arguments - Messages

#21 Posted: 7/31/2010 4:19:51 AM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Hello Andrey,
Wrote


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 ...-


I was just playing with this feature in v0.89 in order to make some Wiki pages about it. Here is the example to the other users, just in case to clarify things.

Here is the function definition of "f" with two arguments- one of the argument is variable and another is function "F" (argumet function) - with single argument
[MATH lang=eng]f(x,F(1))←F(x+2)[/MATH]
On the LHS of the definition F(1) means that argument function F has one argument (I prefer this way) although it could be defined as F(dummy) where "dummy" might be whatever we like - it does not matter.
Let's put some variables and functions here
[MATH]a←6[/MATH]
[MATH lang=eng]x←mat(1,3,2,1)[/MATH]
[MATH]fun(y)←y[/MATH]
[MATH]fun1(z)←z+a[/MATH]
Now, few calls of function "f"
[MATH lang=eng]f(x,fun(xx))=mat(3,5,2,1)[/MATH]
[MATH lang=eng]f(x,fun1(xx))=mat(9,11,2,1)[/MATH]
It must be taken care that arguments of argument function "fun" and "fun1" have the following restrictions:
1. constants not allowed (like fun(5))
2. must be a variable different that the name of arguments in the function "f" (fun(x) not allowed here - x is the argument of "f" )
3. also the name of previously defined variable is not allowed (f(a) not allowed - variable "a" is defined above)

I hope this is Ok.

Regards
Radovan

P.S. I think this is not possible (it would be quite to much at the moment )
[MATH lang=eng]F(x,y,U(x,f(1)))←U(x,f(x))+2[/MATH]
Could you confirm this, please. This would be another restriction, but might be logical from the SMath implementation - not sure?
4. Argument function "U" in the argument list of "F" can not have another argument function in the argument list - "f(1)"
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#22 Posted: 8/8/2010 9:48:44 AM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Hello,

Here are some additional notes about argument functions.
Functions can be defined in a preferable way (IMHO)
[MATH lang=eng]Fun(a,b,f(2))←a+b+f(a,b)/2[/MATH]
Or with dummy arguments. Dummy arguments can be even with the same names. Here are some examples:
[MATH lang=eng]Fun_2(a,b,f(a_,a_))←a+b+f(a,b)/2[/MATH]
[MATH lang=eng]Fun_3(a,b,f(_1,_2))←a+b+f(a,b)/2[/MATH]
[MATH lang=eng]Fun_4(a,b,f(_,_))←a+b+f(a,b)/2[/MATH]
User can choose the most convenient way of defining these functions.
When calling, it is important to have in mind that (already mentioned)
1. argument functions must have variables as arguments
2. names from the argument list or previously defined variables are not allowed.
3. names should be different, contrary to the definition (SMath will not report an error here - It is allowed but pay attention on the result!)
Therefore, user can have its own naming conventions for these argument names - some unusul names used only in these cases. Here is the example by using underscore:
[MATH lang=eng]Fun_1(x,y,ff(_a,_b))=15[/MATH]
[MATH lang=eng]Fun_2(x,y,ff(_a,_b))=15[/MATH]
[MATH lang=eng]Fun_3(x,y,ff(a_,b_))=15[/MATH]
And my favorite one at the moment:
[MATH lang=eng]Fun_4(x,y,ff(_1,_2))=15[/MATH]

None of the variables _a, _b, a_, b_, _1, _2 were not previously defined

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#23 Posted: 4/21/2014 8:49:48 PM
Mike Kaganski

Mike Kaganski

184 likes in 434 posts.

Group: User

Hi,

I can't manage to use functions as arguments in following scenario:

f(g(1);x):=g(x)
g(x):=eval(x+x)
f(g(_);2)=Error: "_ Not Defined".

eval here disables using function as argument (yet another eval() deficiency! ). This is highly unfortunate, as my functions have to use evals to avoid overflow.

Another problem:

f(g(2);x;y):=g(x;y)
g(x;y):=x^y
f(g(_;_);1;2)=4
f(g(_1;_2);1;2)=1

(_, _1 and _2 are all uninitialized)

i.e. dummy argument names to function as argument affect the function expansion, in this specific case making it use y for both x and y! Of course, using different names makes things OK, but this is still a bug, because in the function definition the call is g(x,y), not g(y,y). I believe that the names of dummy arguments must be completely ignored (only tested for not being initialized), and repeating them should make no difference.

Edit: second problem is discussed in clause 3 of previous post #22. Still, I suppose that it should be fixed.
С уважением, Михаил Каганский
1 users liked this post
Radovan Omorjan 4/22/2014 3:16:00 AM
#24 Posted: 4/22/2014 3:30:29 AM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Thank you mikagenski for this post,

There is one thing that makes me sad sometimes . It is the date on some posts. For instance, when there is "Posted: 6 hours ago" following "Posted: 4 years ago". On the other hand, let's be positive. It is good that this still actually exists and I hope that we will see one day "Posted: 20 years ago". We could then remind ourselves how clever we were long time ago .

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#25 Posted: 4/22/2014 4:49:34 AM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Wrote

Hi,

I can't manage to use functions as arguments in following scenario:

f(g(1);x):=g(x)
g(x):=eval(x+x)
f(g(_);2)=Error: "_ Not Defined".

eval here disables using function as argument (yet another eval() deficiency! ). This is highly unfortunate, as my functions have to use evals to avoid overflow.


If eval() would return itself without evaluation if the numeric evaluation fails, this particular problem should be solved. Filed as proposal SS-123.
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 users liked this post
Davide Carpi 4/22/2014 10:28:00 AM
  • New Posts New Posts
  • No New Posts No New Posts