Page 1 sur 1

Global vars

Publié : mer. sept. 06, 2017 3:35 pm
par compsystems
Hi, how can I save global variables, but when I run a program call again, these again do not lose their content

Thanks

Re: Global vars

Publié : mer. sept. 06, 2017 3:55 pm
par parisse
I don't understand what you mean.

Re: Global vars

Publié : mer. sept. 06, 2017 4:50 pm
par compsystems
solved by putting x: = 0 before the definition of the function.
but the compiler should detect that the x variable has been defined previously and not throw the warning message Warning: x, declared as global variable(s)

Code : Tout sélectionner

x:=0;

f():={
  x:=x+1; // global
  print(x);
}:;
// Parsing f
// Warning: x, declared as global variable(s) ...
// compiling f

f() [enter] returns 1
f() [enter] returns 2
f() [enter] returns 3

now

Code : Tout sélectionner

f1():={
  y:=y+1;
  print(y);
}:;
// Parsing f1
// Warning: y, declared as global variable(s). If symbolic variables are required, declare them as local and run purge
// compiling f1

f1() [enter]
"y: recursive definition Error: Bad Argument Value"

the trick for the variable to be symbolic is to add the following statement
local y; purge(y);

Code : Tout sélectionner

f1():={
  local y; purge(y);
  y:=y+1;
  print(y);
}:;
f1() [enter] y+1

but to be more explicit the code, I think that a new command is necessary Symbs id0,id2,...idn it's a great idea? :mrgreen:

Symbs y; replaces local y; purge(y);

Code : Tout sélectionner

f1():={
  Symbs y;
  y:=y+1;
  print(y);
}:;
f1() [enter] y+1

Code : Tout sélectionner

x:=0; // global
f():={
  x:=x+1;
  print(x);
}:;

f1():={
  local y; purge(y); // Symbs y
  y:=y+1;
  print(y);
}:;

f2():={
  local x; purge(x); // Symbs x
  x:=x+1;
  print(x);
}:;
f();f1();f2(); [enter]
1, y+1, x+1

f();f1();f2(); [enter]
2, y+1, x+1

f();f1();f2(); [enter]
3, y+1, x+1

Re: Global vars

Publié : mer. sept. 06, 2017 5:17 pm
par parisse
compsystems a écrit :solved by putting x: = 0 before the definition of the function.
but the compiler should detect that the x variable has been defined previously and not throw the warning message Warning: x, declared as global variable(s)
First the compiler should not check that x is assigned, because that may change later, second it is essential to throw a warning message, because functions should not use global variables, only local variables. Global variables are tolerable for symbolic variables, but it's better to use a symbolic local variable, as explained in the warning. If you really want a global variable, you can still declare it as global.

Re: Global vars

Publié : mer. sept. 06, 2017 5:25 pm
par compsystems
ok,

What do you think of an alternative command

local y; purge(y);
as
symbs y;

local y,z; purge(y,z); as symbs y,z;

other CAS as the matlab symbolic toolbox follows this syntax

Re: Global vars

Publié : mer. sept. 06, 2017 6:37 pm
par parisse
Don't forget that it's easy to make suggestions, but it's not as easy to implement them and I have limited ressources, I can not spend one or 2 hours per day implementing all your suggestions. So please try to sort your suggestions, and send only important ones, for example replacing local y; purge(y); by whatever y; is not very important since the functionnality is already available. The same applies for cosmetic UI changes.

Re: Global vars

Publié : jeu. sept. 07, 2017 10:43 am
par frederic han
NB, there are several way to do this.

for instance in giac simple quotes means the unevaluated form of the object (it is not the same as double quotes) so I often do this:

Code : Tout sélectionner

f1():={
  local y:='y';
  y:=y+1;
  print(y);
}:;
another way to do this:

Code : Tout sélectionner

f1():={
  local y; purge(y);
  y:=y+1;
  print(y);
}:;