previous  next
1.2.18. Rings Inside User-Defined Functions
As mentioned earlier, user-defined functions cannot reference
(non-global) variables except those defined within the function or
passed as arguments.  However, functions can refer to rings via their
identifiers and use them as one would outside of a function.

When a function is called, it assumes the current ring and performs
operations in that ring.  One may define new rings which will exist
after the function returns, but one may not change the current ring
with the command 'Use'.  However, one may *temporarily* use a ring
with the command 'Using'.

To make functions more portable, it may be useful to refer to the
current ring not by its name but by using the command 'CurrentRing'.

Example

-- Example I.
-- Test uses the existing rings, R, S, and creates a new ring T.
-- While a (non-global) *variable* defined in a function will
-- automatically disappear, a ring (and its name) will not.
Use R ::= Q[x,y,z];
S ::= Q[a,b];
Define Test()
  PrintLn (x+y)^2;
  PrintLn S :: (a+b)^3;
  T ::= Z/(5)[t];
  I := T :: Ideal(t^2);
  Print I;
EndDefine;
Test();
x^2 + 2xy + y^2
S :: a^3 + 3a^2b + 3ab^2 + b^3
T :: Ideal(t^2)
-------------------------------
I;  -- the variable I was local to the function
ERROR: Undefined variable I
CONTEXT: I
-------------------------------
T;  -- The function created the ring T.  (Note: T is not a variable.)
Z/(5)[t]
-------------------------------

-- Example II.
-- The use of 'CurrentRing' within a function.
Define Poincare2(I)
  Return Poincare(CurrentRing()/I);
EndDefine;
Use R ::= Q[x,y];
Poincare2(Ideal(x^2,y^2));
(1 + 2x + x^2)
-------------------------------

-- Example III.
-- Creating a ring with a user-supplied name.  For more information,
-- see 'Var'.
Define Create(Var(R));
  Var(R) ::= Q[a,b];
EndDefine;
Create('K');
K;
Q[a,b]
-------------------------------
Create('myring');
Var('myring');
Q[a,b]
-------------------------------
Use Var('myring');  -- make myring current
-- Example IV.
-- A more complicated example, creating rings whose names are
-- automatically generated.  See 'NewId' and 'Var' for more
-- information. 
Define CreateRing(I)
  NewRingName := NewId();
  Var(NewRingName) ::= Q[x[1..I]],Lex;
  Return NewRingName;
EndDefine;
  
Use R ::= Q[x,y],DegRevLex;
Use S ::= Q[x,y,z],Lex;
N := 5;
For I := 1 To N Do
  RingName := CreateRing(I); -- RingName is a string
  Using Var(RingName) Do
    PrintLn Indets();
  EndUsing;
  -- Destroy Var(RingName); -- uncomment if you want to destroy the tmp
  -- ring
EndFor;

RingEnvs();
[x[1]]
[x[1], x[2]]
[x[1], x[2], x[3]]
[x[1], x[2], x[3], x[4]]
[x[1], x[2], x[3], x[4], x[5]]

-------------------------------
["Q", "Qt", "R", "S", "V#1", "V#3", "V#5", "V#7", "V#9", "Z"]
-------------------------------