[Scilab-users] Anonymous functions

Pierre Vuillemin contact at pierre-vuillemin.fr
Tue Jan 17 11:46:11 CET 2017


I have actually implemented a similar solution (in the previous link) 
except that the data are stored in some list. This leads to anonymous 
functions that behaves as in Matlab, i.e. the data of the function is 
instantiated when the function is created.

In python, the value of the data 'a' is instantiated when the function 
is evaluated. Therefore, if the data 'a' changes, the function changes.

Here I could make something like

deff('y=f(x)','y = a*x')

but then the function will get the value of the variable 'a' of the 
current namespace, e.g.

a = 1
function outer(x)
  a = 2
  disp(f(1))
endfunction

f(1)     // will give 1
outer(1) // will give 2
a = 3
f(1)     // will give 3
outer(1) // will give 2

In this example, I would like f(1) to behave identically independently 
of its position.

Achieving that boils down to be able to refer to a specific variable in 
a specific namespace of Scilab (a kind of pointer I guess), but I'm not 
sure this is possible?


Le 17.01.2017 10:38, Serge Steer a écrit :
> A partial solution should be
> deff("y=f(x)","a="+sci2exp(a,0)+";y=a*x")
> 
> but it may cause numerical problem as a is formatted
> Regards
> 
> Serge Steer
> On 17/01/2017 08:23, Pierre Vuillemin wrote:
>> Hi all,
>> 
>> I am trying to reproduce the behaviour of anonymous function in Scilab 
>> (see the code in lambdaFun here: 
>> https://github.com/pivui/scilabTools).
>> 
>> At the moment, it works like in Matlab, for instance:
>> 
>> a = eye(3,3);
>> lambda 'f(x) = a*x' // the name of the lambda function will be f
>> a = rand(3,3)       // the value of 'a' is saved in the lambda 
>> function when it is created
>> f(ones(3,1))        // gives [1,1,1]'
>> 
>> I would like to emulate the behaviour of lambda function of python as 
>> well. In that case, 'a' should change when the initial variable 
>> changes, for instance
>> 
>> a = 2
>> f = lambda : a
>> def fun():
>>   a = 4
>>   return f
>> a = 3
>> f()   // should return 3
>> fun() // should return 3 -> it does not take the variable 'a' in the 
>> function namespace
>> 
>> I need to remember where the value or a is stored, I don't know if it 
>> is possible?
>> 
>> Regards,
>> 
>> Pierre
>> _______________________________________________
>> users mailing list
>> users at lists.scilab.org
>> http://lists.scilab.org/mailman/listinfo/users
>> 
> 
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users



More information about the users mailing list