Global vars

Messages in english

Modérateur : xcasadmin

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

Global vars

Message par compsystems » mer. sept. 06, 2017 3:35 pm

Hi, how can I save global variables, but when I run a program call again, these again do not lose their content

Thanks

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

Re: Global vars

Message par parisse » mer. sept. 06, 2017 3:55 pm

I don't understand what you mean.

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

Re: Global vars

Message par compsystems » mer. sept. 06, 2017 4:50 pm

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

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

Re: Global vars

Message par parisse » mer. sept. 06, 2017 5:17 pm

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.

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

Re: Global vars

Message par compsystems » mer. sept. 06, 2017 5:25 pm

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

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

Re: Global vars

Message par parisse » mer. sept. 06, 2017 6:37 pm

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.

frederic han
Messages : 1139
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: Global vars

Message par frederic han » jeu. sept. 07, 2017 10:43 am

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);
}:;

Répondre