Page 1 sur 1
Warnings FIBMAT(n)
Publié : sam. déc. 22, 2018 7:34 pm
par compsystems
Code : Tout sélectionner
function FIBMAT(n)
var M1,k;
index:=1;
M1:=MAKEMAT(0,n+1,n+1);
M1(1,1):=1;
M1(2,1):=1;
M1(2,2):=1;
FOR k FROM 3 TO n+1 DO
M1(k):=row(M1,k-2)+row(M1,k-3);
M1(k,k):=M1(k-1,k-1)+M1(k-2,k-2);
END;
RETURN M1;
ffunction
FIBMAT(7)
[[1,0,0,0,0,0,0,0],
[1,1,0,0,0,0,0,0],
[2,1,2,0,0,0,0,0],
[3,2,2,3,0,0,0,0],
[5,3,4,3,5,0,0,0],
[8,5,6,6,5,8,0,0],
[13,8,10,9,10,8,13,0],
[21,13,16,15,15,16,13,21]]
session Xcas
Why the following warnings are thrown?
// Warning: M1, declared as global variable(s).
// Warning, duplicate argument name: k
Re: Warnings FIBMAT(n)
Publié : dim. déc. 23, 2018 12:46 pm
par parisse
M1(k,k):=M1(k-1,k-1)+M1(k-2,k-2);
may be confused with a function definition. Do not use () for index notation when you store in a list/matrix, if you want to start indices at 1, use [[ ]] instead.
Re: Warnings FIBMAT(n)
Publié : dim. déc. 23, 2018 7:50 pm
par compsystems
The following code no longer generates those warnings.
although in the compilation (translation) it shows that it uses () in the right part
M1[[k,k]] := M1(k-1,k-1)+M1(k-2,k-2); // Why?
Code : Tout sélectionner
function fibmat0(n)
var M1,k;
//index:=1;
M1:=makemat(0,n+1,n+1);
M1[[1,1]]:=1;
M1[[2,1]]:=1;
M1[[2,2]]:=1;
for k from 3 to n+1 do
M1[[k]]:=row(M1,k-2)+row(M1,k-3);
M1[[k,k]]:=M1[[k-1,k-1]]+M1[[k-2,k-2]];
end;
return M1;
ffunction
Code : Tout sélectionner
fibmat2(n):={
local M1,k;
M1:=makemat(0,n+1,n+1);
M1[[1,1]]:=1;
M1[[2,1]]:=1;
M1[[2,2]]:=1;
for (k:=3;k<=(n+1);k:=k+1) {
M1[[k]]:=row(M1,k-2)+row(M1,k-3);
M1[[k,k]] := M1[[k-1,k-1]] + M1[[k-2,k-2]];
};
return(M1);
}/code]
Re: Warnings FIBMAT(n)
Publié : lun. déc. 24, 2018 5:59 pm
par parisse
There is no possible confusion with function definition if you *read* a variable.