Loïc Grenié on Mon, 19 Oct 2015 14:10:13 +0200

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

Re: Vector of args

On 2015-10-17 at 23:27 GMT+02:00 Bill Allombert wrote:
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]));

     Wouldn't it be possible to have evalf already included in GP? It
  does not look too difficult to implement (although I do not understand
  completely the code). I volunteer if needed.

     I've recently needed said functionality to find the minimum of a
  function of several variables.