The implicit size eye() object should and could be replaced with a regular %eye=1:0*$:1 implicitlist object as a new Scilab predefined constant
Reported by Samuel GOUGEON (@sgougeon)
DESCRIPTION:
------------
eye() builds an object that is a very specific implicit size one:
--> size(eye())
ans =
-1. -1.
sometimes actually used as an implicitlist one (:) as in indexing (for instance in getcolor.sci)
--> m = grand(3,4,"uin",0,9)
m =
9. 5. 7. 6.
8. 3. 8. 5.
6. 3. 4. 0.
--> m(2,eye())
ans =
8. 3. 8. 5.
This object embeds some data:
--> m = eye()*%z;
--> m
m =
eye *
z
and its type is the one of the embedded data:
--> type(eye())
ans =
1.
--> type(eye()*%z)
ans =
2.
But objects of this type are poorly used (107 occurrences of eye() in 47 native scilab files)
and poorly supported.
By the way, some existing usages can directly be replaced, like
m(eye(),k) => m(:,k)
eye()+m => eye(m)+m // easy when m is a symbol, less handy when it is an expression.
-------------------------------
The added value of this specific object looks very poor with respect
to the complexity and specific processing it requires in the code.
-------------------------------
Is it possible to implement an easy-to-use equivalent?
======================================================
A straightforward idea is to build and use an object of the existing implicitlist type.
Let's do it:
%eye = 1:0*$:1 // That can be protected as a new Scilab constant
Because in normal usages of implicitsize objects bounds can be of $ but not the step,
the idea here is to use a special polynomial step as a tag.
Then, eye()*n
already exists and is correct:
--> %eye*2
ans =
2::2
Then, operators currently supporting eye() could be easily redefined.
For instance, for the addition:
function r = %s_a_ip(s,ip)
// s+ip
step = ip(2);
if type(step)==2 & varn(step)=="$" & degree(step)<=0 & ip(3)==ip(1) then
// ip is n:0$:n representing %eye*n
r = eye(s)*ip(1) + s
else
if size(s,"*")<>1 then
msg = _("%s: Argument #%d: Scalar (1 element) expected.\n")
error(msprintf(msg, "%s_a_ip", 1))
end
r=(s+ip(1)):ip(2):(s+ip(3))
end
endfunction
Then:
--> m
m =
9. 5. 7. 6.
8. 3. 8. 5.
6. 3. 4. 0.
--> m + 3*%eye
ans =
12. 5. 7. 6.
8. 6. 8. 5.
6. 3. 7. 0.
-------------------------
This would allow to manage only 1 implicit size type of object,
%eye being just a particular case of such an object.
-------------------------