[Scilab-users] Multiple files of functions with the same name

classic Classic list List threaded Threaded
13 messages Options
Llelan D. Llelan D.
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Multiple files of functions with the same name

A common task I've coded is to run multiple implementations of the same
function head-to-head to profile their effectiveness. An example would
be a competition in which each of two implementations provide a response
for each match. The usual way in other interpreted languages is to have
the implementers write a source file implementing the same function
prototype (same name, arguments, and return type). You load each one in
their own namespace and run each for each match.

Is there a way to load the functions in two .sci files so that the
second does not clobber the first. I can find no system for qualifying
function names or creating namespaces in Scilab. The files can not be
run in-place (without persisting in the session) since the exec()
function only loads the functions and does not run any. Using statements
outside of any function does no good as additional function definitions
might be required in the file to implement their algorithm.

I've thought of using exec() on one file, qualifying the loaded function
names with <unique competitor name>_<function name> with assignments and
clearing the original function variables (which works manually), and
doing the same for the second file but I can find no way of getting a
list of functions loaded by the last exec().

I'm either missing something really obvious or am stymied. Any help
would be greatly appreciated.
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Hello,

Le 03/07/2014 04:14, Llelan D. a écrit :

> A common task I've coded is to run multiple implementations of the
> same function head-to-head to profile their effectiveness. An example
> would be a competition in which each of two implementations provide a
> response for each match. The usual way in other interpreted languages
> is to have the implementers write a source file implementing the same
> function prototype (same name, arguments, and return type). You load
> each one in their own namespace and run each for each match.
>
> Is there a way to load the functions in two .sci files so that the
> second does not clobber the first.

Let say that you have 2 functions written in Scilab language, with the
same name "fun(..)" and same or distinct profiles (possible calling
sequences). If each one is declared in a specific library -- say lib1
and lib2 --, then you can call each one separately: lib1.fun(...) will
call the first implementation, and lib2.fun(..) the second one ; etc. I
think -- but this must be checked -- that after calling lib1.fun(..),
this implementation will be loaded in memory, replacing any other
previous loaded implementation of fun(..). So, if then you call simply
fun(..), still the lib1 implementation will be run.

HTH
Samuel

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Le 03/07/2014 09:14, Samuel Gougeon a écrit :

>
> Let say that you have 2 functions written in Scilab language, with the
> same name "fun(..)" and same or distinct profiles (possible calling
> sequences). If each one is declared in a specific library -- say lib1
> and lib2 --, then you can call each one separately: lib1.fun(...) will
> call the first implementation, and lib2.fun(..) the second one ; etc.
> I think -- but this must be checked -- that after calling
> lib1.fun(..), this implementation will be loaded in memory, replacing
> any other previous loaded implementation of fun(..). So, if then you
> call simply fun(..), still the lib1 implementation will be run.
May i add that if you wish to make actual all the functions of a scilab
library, just load the library. This will replace in memory all previous
functions with identical names.

Samuel

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Antoine Monmayrant Antoine Monmayrant
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

In reply to this post by Llelan D.
On 07/03/2014 04:14 AM, Llelan D. wrote:

> A common task I've coded is to run multiple implementations of the
> same function head-to-head to profile their effectiveness. An example
> would be a competition in which each of two implementations provide a
> response for each match. The usual way in other interpreted languages
> is to have the implementers write a source file implementing the same
> function prototype (same name, arguments, and return type). You load
> each one in their own namespace and run each for each match.
>
> Is there a way to load the functions in two .sci files so that the
> second does not clobber the first. I can find no system for qualifying
> function names or creating namespaces in Scilab. The files can not be
> run in-place (without persisting in the session) since the exec()
> function only loads the functions and does not run any. Using
> statements outside of any function does no good as additional function
> definitions might be required in the file to implement their algorithm.
>
> I've thought of using exec() on one file, qualifying the loaded
> function names with <unique competitor name>_<function name> with
> assignments and clearing the original function variables (which works
> manually), and doing the same for the second file but I can find no
> way of getting a list of functions loaded by the last exec().
>
> I'm either missing something really obvious or am stymied. Any help
> would be greatly appreciated.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
>

I am not sure that I truly understand your problem, but maybe what
follows can be helpful if you can run your two implementation sequentially:
- functions are first class citizens in scilab which mean you can copy
them like normal variables:
myfunc=sin;
disp("myfunc(pi/2)="+string(myfunc(%pi/2)));
myfunc=cos;
disp("myfunc(pi/2)="+string(myfunc(%pi/2)));

Could this be used to solve your problem by renaming the implementation
to test on the fly?

Antoine
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Alain LAMY Alain LAMY
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Hi,

Here's another suggestion, in case:

Say that function f(x) is defined in the file "file1.sce".
and that f(x) is also defined in "file2.sce".

If you load the functions:
exec("file1.sce")
f1 = f;
exec("file2.sce")
f2 = f;

then you have the 2 functions in memory with different names.
You could then rename them as required: f = f2; ...

You could also store the functions in a list, or use execstr
to manage function names: execstr("f"+str+"=f");

I hope that helps.

Stefan Du Rietz Stefan Du Rietz
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Hello,

Why not ordinary function files?

exec("fun1.sci")
exec("fun2.sci")

And then rename them when needed:

f = fun1;
...
f= fun2;
...

Regards
Stefan


On 2014-07-07 00:16, Alain LAMY wrote:
--------------------

> Hi,
>
> Here's another suggestion, in case:
>
> Say that function f(x) is defined in the file "file1.sce".
> and that f(x) is also defined in "file2.sce".
>
> If you load the functions:
> exec("file1.sce")
> f1 = f;
> exec("file2.sce")
> f2 = f;
>
> then you have the 2 functions in memory with different names.
> You could then rename them as required: f = f2; ...
>
> You could also store the functions in a list, or use execstr
> to manage function names: execstr("f"+str+"=f");
>
> I hope that helps.
>
>
>
>
>
> --
> View this message in context: http://mailinglists.scilab.org/Scilab-users-Multiple-files-of-functions-with-the-same-name-tp4030814p4030832.html
> Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
>

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Llelan D. Llelan D.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

In reply to this post by Antoine Monmayrant
Thank you all for your kind replies. Samuel has come up with what seems
to be the only workable solution using the namespaces provided by
library variables (I'll explain after a couple of points and an example).

----------
The rest of the replies have to do with either loading and running each
implementation sequentially, or manually renaming the loaded function
variables.

As I explained in the OP:
* Both functions MUST be resident without clashing so they both can be
run. Constantly reloading, re-interpreting, and recompiling each
function for what may be millions of times per match is pathetically
inefficient.
* The .sci files might not only implement the specified function
prototype, they might also implement many support functions. Since the
implementation files might be written by 3rd parties (such as in a
tournament), the support function names can not be known beforehand. So,
it is impossible to know what to rename after an exec() is done, and
I've never found a way to list what functions are loaded by an exec().

As to the confusion of what is being done: This is a description of a
generic process that can be applied to a great variety of algorithm
profiling and tournament situations. The overall products that might use
this are not important. The ability to have functions with the same name
and prototype implemented in different .sci files resident in the same
session is.

However, I'll give you a concrete example:
Some years ago, a tournament was held for response algorithms to the
Iterated Prisoner's Dilemma (see http://www.prisoners-dilemma.com for
details). Competitors would each implement a set of "personalities" each
of which implements a "responseString = response(iGame, lastScore)"
prototype. The tournament software expects to see in the tournament
directory a set of directories each named for a competitor. In each
competitor directory is a set of personality directories each of which
holds the implementation of the response prototype function and possibly
many other support functions. While the competitor names are registered
unique, the personality and support function names might not be. Each
competitor is paired against each other competitor where each
personality of one competitor is played against each personality of the
other 20 times. The average score for each personality and competitor is
accumulated and the competitor(s) with the highest average score wins.
With hundreds of competitors, some using dozens of personalities and
many with the same names but different implementations, the number of
games can easily run into the hundreds of thousands.

In this scenario, the personalities should not be constantly reloaded,
re-interpreted, and re-compiled for each and every call. The whole
tournament would be uselessly bogged down.

----------
The library solution proposed by Samuel can deal with this since the
loaded library functions can be qualified with the library variable
name. For example: Two libraries are created in directories testLib1 and
testLib2, each of which implements the "y = test(x)" prototype. After
using the lib() function to load both to two variables called testLib1
and testLib2, you can call either testLib1.test(2) or testLib2.test(2)
without having to reload anything. There is a little annoying magic
where the variable "test" will be assigned to whatever library was last
loaded but, if you do not use that variable, there is no problem, unless
you run into variable space overruns (I do wish there was a way to not
do that).

For tournament software, it is important to be able to require
competitors to submit implementation sources so that you can verify
nothing goes on that breaks the rules. Scilab allows you to create the
libraries without external utilities all in the same language. Setting
funcprot(2) makes sure that competitor implementations do not try to
futz with the environment used by the other competitor but still allows
the qualified libraries with functions of the same name to be loaded.
I've tested this and it works.

Thank you Samuel for your solution to this. I am greatly appreciative.

However – namespaces or qualified names are definitely needed in Scilab.

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
aweeks aweeks
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Hi "Llelan D.",

In your text below, see the line marked in red.  Would 'listfunctions()'  before and after the 'exec' do what you need?  (Apologies if that is too naive).

Adrian.

    Adrian Weeks
    Development Engineer


    HID Global,
    3 Cae Gwyrdd,
    Green Meadow Springs
    Business Park,
    Cardiff CF15 7AB,
    United Kingdom.
    +44 (0)29 20528523 (Office)

    [hidden email]
    www.hidglobal.com


Inactive hide details for "Llelan D." ---09/07/2014 19:17:15---Thank you all for your kind replies. Samuel has come up with wha"Llelan D." ---09/07/2014 19:17:15---Thank you all for your kind replies. Samuel has come up with what seems  to be the only workable sol


    From:

"Llelan D." <[hidden email]>

    To:

[hidden email]

    Date:

09/07/2014 19:17

    Subject:

Re: [Scilab-users] Multiple files of functions with the same name

    Sent by:

"users" <[hidden email]>




Thank you all for your kind replies. Samuel has come up with what seems
to be the only workable solution using the namespaces provided by
library variables (I'll explain after a couple of points and an example).

----------
The rest of the replies have to do with either loading and running each
implementation sequentially, or manually renaming the loaded function
variables.

As I explained in the OP:
* Both functions MUST be resident without clashing so they both can be
run. Constantly reloading, re-interpreting, and recompiling each
function for what may be millions of times per match is pathetically
inefficient.
* The .sci files might not only implement the specified function
prototype, they might also implement many support functions. Since the
implementation files might be written by 3rd parties (such as in a
tournament), the support function names can not be known beforehand. So,
it is impossible to know what to rename after an exec() is done, and
I've never found a way to list what functions are loaded by an exec().

As to the confusion of what is being done: This is a description of a
generic process that can be applied to a great variety of algorithm
profiling and tournament situations. The overall products that might use
this are not important. The ability to have functions with the same name
and prototype implemented in different .sci files resident in the same
session is.

However, I'll give you a concrete example:
Some years ago, a tournament was held for response algorithms to the
Iterated Prisoner's Dilemma (see
http://www.prisoners-dilemma.com for
details). Competitors would each implement a set of "personalities" each
of which implements a "responseString = response(iGame, lastScore)"
prototype. The tournament software expects to see in the tournament
directory a set of directories each named for a competitor. In each
competitor directory is a set of personality directories each of which
holds the implementation of the response prototype function and possibly
many other support functions. While the competitor names are registered
unique, the personality and support function names might not be. Each
competitor is paired against each other competitor where each
personality of one competitor is played against each personality of the
other 20 times. The average score for each personality and competitor is
accumulated and the competitor(s) with the highest average score wins.
With hundreds of competitors, some using dozens of personalities and
many with the same names but different implementations, the number of
games can easily run into the hundreds of thousands.

In this scenario, the personalities should not be constantly reloaded,
re-interpreted, and re-compiled for each and every call. The whole
tournament would be uselessly bogged down.

----------
The library solution proposed by Samuel can deal with this since the
loaded library functions can be qualified with the library variable
name. For example: Two libraries are created in directories testLib1 and
testLib2, each of which implements the "y = test(x)" prototype. After
using the lib() function to load both to two variables called testLib1
and testLib2, you can call either testLib1.test(2) or testLib2.test(2)
without having to reload anything. There is a little annoying magic
where the variable "test" will be assigned to whatever library was last
loaded but, if you do not use that variable, there is no problem, unless
you run into variable space overruns (I do wish there was a way to not
do that).

For tournament software, it is important to be able to require
competitors to submit implementation sources so that you can verify
nothing goes on that breaks the rules. Scilab allows you to create the
libraries without external utilities all in the same language. Setting
funcprot(2) makes sure that competitor implementations do not try to
futz with the environment used by the other competitor but still allows
the qualified libraries with functions of the same name to be loaded.
I've tested this and it works.

Thank you Samuel for your solution to this. I am greatly appreciative.

However – namespaces or qualified names are definitely needed in Scilab.

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users



_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Llelan D. Llelan D.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

@aweeks
I thought of that too. As documented, you should be able to get the list of loaded function names from the last "exec()" by subtracting a "listfunctions()" call prior to the "exec()" to one made after.

However, "listfunctions()" acts in some strange and undocumented ways. For example, if you start with a clean invocation of the interpreter console and run "listfunctions()", it lists the function "listfunctions" itself. If you do a "typeof(asinh)" and then a "listfunctions()", it will now list "! asinh typeof listfunctions !".

Until "listfunctions()" is fully and clearly documented, it seems useless in this context, or at least unreliable.
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Le 13/07/2014 17:29, Llelan D. a écrit :
> However, "listfunctions()" acts in some strange and undocumented ways. For
> example, if you start with a clean invocation of the interpreter console and
> run "listfunctions()", it lists the function "listfunctions" itself. If you
> do a "typeof(asinh)" and then a "listfunctions()", it will now list "! asinh
> typeof listfunctions !".
When an unknown symbol looking like the call to a function is met,
Scilab scans the list of functions registered in loaded libraries. If a
function name matches the symbol, it is loaded from the related library.
This means that every function registered in a loaded library is loaded
only when it is first called.
In your experiment, it is the case for typeof and asinh and selfly for
listfunctions. So, the answer from listfunctions is normal.

Samuel

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Llelan D. Llelan D.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

Actually, when first referenced and not just called since "asinh" was never called but only used as a function reference argument to "typeof()".

I was afraid it was something like a lazy-load scenario. It still makes it useless in trying to determine what functions were loaded during an "exec()" call. A minor point though since the library scenario has proved more useful. Also, the 24 character name limit destroys any attempt to manage qualifying names by renaming (sigh).
aweeks aweeks
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

In reply to this post by Llelan D.

Hi Llelan D,

Here's another thought (probably as naive as the first) - the functions 'whos' and more particularly 'who_user'. These seem to return a list of variables (including function names).
The behaviour of 'whos' is odd in that the return doesn't seem to be a variable (at least in my case),  'who_user' seems to work more as expected.

Good luck.

Adrian.

    Adrian Weeks
    Development Engineer


    HID Global,
    3 Cae Gwyrdd,
    Green Meadow Springs
    Business Park,
    Cardiff CF15 7AB,
    United Kingdom.
    +44 (0)29 20528523 (Office)

    [hidden email]
    www.hidglobal.com


Inactive hide details for "Llelan D." ---13/07/2014 16:30:21---@aweeks I thought of that too. As documented, you should be able"Llelan D." ---13/07/2014 16:30:21---@aweeks I thought of that too. As documented, you should be able to get the list of


    From:

"Llelan D." <[hidden email]>

    To:

[hidden email]

    Date:

13/07/2014 16:30

    Subject:

Re: [Scilab-users] Multiple files of functions with the same name

    Sent by:

"users" <[hidden email]>




@aweeks
I thought of that too. As documented, you should be able to get the list of
loaded function names from the last "exec()" by subtracting a
"listfunctions()" call prior to the "exec()" to one made after.

However, "listfunctions()" acts in some strange and undocumented ways. For
example, if you start with a clean invocation of the interpreter console and
run "listfunctions()", it lists the function "listfunctions" itself. If you
do a "typeof(asinh)" and then a "listfunctions()", it will now list "! asinh
typeof listfunctions !".

Until "listfunctions()" is fully and clearly documented, it seems useless in
this context, or at least unreliable.



--
View this message in context:
http://mailinglists.scilab.org/Scilab-users-Multiple-files-of-functions-with-the-same-name-tp4030814p4030901.html
Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users



_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Llelan D. Llelan D.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple files of functions with the same name

"whos" outputs a formatted display of the same information returned by "who" (name and memory size) and with type and size of each. No value is actually returned.

"who_user" only returns a list of variables created or loaded by the current user in the current session. So the first reference to a function in an already loaded library (like "asinh") will appear in the returned list.

All of them suffer the same lazy-load problem as "listfunctions", which follows since "whos", "who_user", and "listfunctions" all use "who" to collect the information they then filter and return or use.
See
  <Scilab installation directory>/modules/core/macros/whos.sci
  <Scilab installation directory>/modules/core/macros/who_user.sci
  <Scilab installation directory>/modules/functions/macros/listfunctions.sci