Warnings FIBMAT(n)

Messages in english

Modérateur : xcasadmin

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Warnings FIBMAT(n)

Message par compsystems » sam. déc. 22, 2018 7:34 pm

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

parisse
Messages : 5740
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Warnings FIBMAT(n)

Message par parisse » dim. déc. 23, 2018 12:46 pm

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.

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Re: Warnings FIBMAT(n)

Message par compsystems » dim. déc. 23, 2018 7:50 pm

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]

parisse
Messages : 5740
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Warnings FIBMAT(n)

Message par parisse » lun. déc. 24, 2018 5:59 pm

There is no possible confusion with function definition if you *read* a variable.

Répondre