up previous next
 2.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 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.

 Example
 ``` Use R ::= QQ[x,y,z]; S ::= QQ[a,b]; Define Test() PrintLn (x+y)^2; PrintLn S :: (a+b)^3; T ::= ZZ/(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.) ZZ/(5)[t] ------------------------------- ```
Example II

The use of CurrentRing within a function.

 Example
 ``` Define Poincare2(I) Return Poincare(CurrentRing()/I); EndDefine; Use R ::= QQ[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 .

 Example
 ``` Define Create(Var(R)); Var(R) ::= QQ[a,b]; EndDefine; Create("K"); K; QQ[a,b] ------------------------------- Create("myring"); Var("myring"); QQ[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.

 Example
 ``` Define CreateRing(I) NewRingName := NewId(); Var(NewRingName) ::= QQ[x[1..I]], Lex; Return NewRingName; EndDefine; Use R ::= QQ[x,y], DegRevLex; Use S ::= QQ[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; [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]] ------------------------------- RingEnvs(); ["QQ", "QQt", "R", "S", "V#1", "V#3", "V#5", "V#7", "V#9", "ZZ"] ------------------------------- ```