Bill Allombert on Sat, 17 Oct 2015 23:27:46 +0200

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Vector of args

On Sat, Oct 17, 2015 at 09:11:51PM +0200, Josef Eschgfaeller wrote:
> Is there a better way to define evalf?
> Want to use f, not "f".

Please see this report
and the GIT branch bill-call.

> t_word unites several strings (or letters)
> to one string.
> --------------------------------------------------
> \\ f(v1,...) if v=[v1,...].
> evalf (f,v) = {v=Vec(Str(v)); v[1]=""; v[#v]="";
> v=t_word(v,,""); eval(Strprintf("(%s)(%s)",f,v))}

There is no need to convert f and v to character strings,
you can do

\\generate the string "v[1],...,v[n]"
genargs(n)= concat(vector(n+1,i,if(i==1,"",i==2,"v[1]",Str(",v[",i-1,"]"))))

\\ evaluate "f(v[1],...,v[n])" after setting f and v 
evalf(f,v)= eval(Str("f(",genargs(#v),")"))

But if you do not need to handle large number of arguments,
you can simply do:

evalf (f,v) = if(#v==0, f(),
                 #v==1, f(v[1]),
                 #v==2, f(v[1],v[2]),
                 #v==3, f(v[1],v[2],v[3]),
                 #v==4, f(v[1],v[2],v[3],v[4]));