[scilab-Users] code which does not work
François Vogel
fvogelnew1 at free.fr
Wed Mar 18 23:26:11 CET 2009
Ivan Maximov said on 18/03/2009 17:52:
> krotov_csa.sci
> __________________________________________________________________________________________________________
> function [ef,u1,u2,u3,u4]=krotov_csa(x1,x2,x3,x4)
>
> u1=x1; u2=x2; u3=x3; u4=x4;
> t1=x1; t2=x2; t3=x3; t4=x4;
>
> PROP=uu;
> TE=uu;
> pause;
>
> for i=1:N,
> for j=1:csN,
> PROP=expm(-%i*dt*(Hij(:,:,j)+Hamilt+H1(u1(i),u2(i),u3(i),u4(i))));
> TE=PROP*U(:,:,j);
> U(:,:,j)=TE;
> end
> end
This looks very much like a bug of Scilab when inserting a matrix in
hypermatrices.
The problem is that after the first execution (i=1 and j=1) of:
> PROP=expm(-%i*dt*(Hij(:,:,j)+Hamilt+H1(u1(i),u2(i),u3(i),u4(i))));
> TE=PROP*U(:,:,j);
> U(:,:,j)=TE;
something goes wrong in U: size(U) becomes 4x4, i.e. U becomes a 4x4
matrix. It looses its third dimension.
The strange thing is that this does not happen whatever the context,
and this explains why it works sometimes with the debugger (but I saw
the problem also when debugging in Scipad). My guess is that global
variables are involved, and perhaps execution contexts of your several
exec instructions.
The problem can be easier seen when instrumenting the code a bit like
this in function krotov_csa lines 10 and below:
for i=1:N,
mprintf("i is: %d\n",i)
for j=1:csN,
mprintf("j is: %d\n",j)
PROP=expm(-%i*dt*(Hij(:,:,j)+Hamilt+H1(u1(i),u2(i),u3(i),u4(i))));
TE=PROP*U(:,:,j);
disp("BEFORE");disp(size(U));
U(:,:,j)=TE;
disp("AFTER");disp(size(U))
end
end
Now upon execution from Scilab shell:
-->exec main.sce;
START GENERAL
i is: 1
j is: 1
BEFORE
4. 4. 155.
AFTER
4. 4.
j is: 2
!--error 21
invalid index
My opinion is that Scilab finds out that U(:,:,j) is a matrix and no
longer an hypermatrix (since the third dimension is exactly 1), and in
this case it reduces the number of dimensions of U(:,:,j). The problem
is that then Scilab erroneously affects U(:,:,j) into U, reducing the
dimension of U in the process too.
I think that this deserves a bug report in the bugzilla:
http://bugzilla.scilab.org/
However it would help if you could trim down the problem to a simpler
script. Thanks in advance.
Francois
More information about the users
mailing list