Code transcripts

Session 1

session_1.m2

  11+1
  2
  3k = ZZ/11
  4k = QQ
  51/2
  6k = RR
  7
  8k = QQ
  9R = k[x,y,z,w]
 10
 11x + y^2 - w*x
 12
 13I = ideal(x^2, y^2, z^2)
 14J = ideal(x*y*z)
 15
 16I + J
 17intersect(I,J)
 18
 19R/I  -- Ring of polynomials with x,y,z degree <= 1, w degree anything
 20
 21use (R/I) -- Ties variables to the ring R/I instead of R
 22x^3
 23
 24k
 25k = ideal(x+y+z)
 26k
 27k = symbol k -- To "deassign" a variable
 28
 29-- indexed variables
 30x = symbol x
 31R = QQ[x_0,x_1,x_2,x_3]
 32R = QQ[x_0..x_3]
 33vars R
 34
 35x_0
 36
 37R = symbol R
 38R = ring x_0
 39
 40-- order matters
 41restart
 42R = QQ[x,y,z]
 43I = ideal(x+y)
 44S = QQ[x,y]
 45J = ideal(x+y)
 46
 47J == I -- is I and J equal?
 48sub(J, R) == I
 49
 50use R
 51K = ideal(x+y+z)
 52sub(K, S) -- sub is a bit dangerous
 53
 54use R
 55phi = map(R, S, {x,y})
 56phi J -- equivalent to sub(J,R) in this case
 57
 58describe S
 59use S
 60psi = map(S,R,{x,y,0})
 61psi K -- equivalent to sub(K,S) in this case
 62
 63restart
 64R = QQ[x,y,a, MonomialOrder => Lex, Degrees => {{-1,2}, {2,3}, {4,5}}]
 65x > y
 66a > y
 67
 68x > a^102832
 69
 703:1
 71degree(x^2*y^3)
 72degree(x)
 73
 74describe R
 75
 76
 77-- finding things
 78viewHelp
 79viewHelp inverse
 80?inverse
 81apropos "inverse"
 82viewHelp ideal
 83
 84--
 85-- lists
 86l = { "abc", I, R }
 87l#0
 88l#1
 89l#2
 90
 91l | { hello, "hello" }
 92
 93s = ("abc", I, R)
 94a..t
 95(0,0)..(6,4) -- sequence
 96toList((0,0)..(6,4)) --list
 97
 98(0,0)..<(6,4) -- sequence
 990..<7
1000..(7-1)
101
102s = a..c
103
104x = symbol x
105s = x_(0,0)..x_(3,3)
106
107reverse(0..10)
108reverse(a..z)
109
110R = QQ[s]
111describe R

Session 2

session_2.m2

 1-- Best practices
 2restart
 3
 4f = i -> i+1
 5
 6f 7
 7f(5)
 8
 9g = (a,b,c) -> (a+2, b+3, c+7)
10g(1,7,-3)
11
12R = QQ[x,y]
13J = 17
14
15complicated = I -> (
16  J := radical I; -- J is local to the function scope, := 
17  if J == ideal(x,y) then return ideal(0_R);
18  return (J + ideal(x,y))
19)
20
21complicated(ideal(x^2+7))
22complicated(ideal(x,y))
23
24I = ideal(x,y)
25if I == ideal(x,y) then (
26  print("Hello!");
27  I
28)
29
30for i from 0 to 7 do ( print(i) )
31for i from 0 to 7 list ( i^2 )
32
33-- Don't
34l = {}
35for i from 0 to 7 do (
36  l = l | {i^2}
37)
38l
39---
40
41-- kind of OK, but
42for word in {"my", "name", "is"} do print word
43
44viewHelp apply
45apply(toList(0..7), i -> i^2)
46toList(0..7) / (i -> i^2) -- same as above
47
48R = QQ[x,y,z,w]
49I = ideal(x^2+y, y^2-x*z, w*x)
50prim_dec = primaryDecomposition I
51prim_dec / dim
52
53primaryDecomposition I / radical // unique -- equivalent to
54unique(apply(primaryDecomposition I, radical))
55
56unique({1,2,3,2,2})
57S = QQ[ subsets(4,2) / (i -> p_i) ]
58describe S
59
60gens gb I
61
62viewHelp forceGB
63
64viewHelp
65
66R = QQ[x,y, MonomialOrder => ]
67
68matrix{{1,2}}
69
70load "my_functions.m2"
71f(1)
72g(4)

BestPackage

BestPackage.m2

  1newPackage(
  2  "BestPackage",
  3  Headline => "an amazing package",
  4  Version => "1.0"
  5)
  6
  7export {"bestMethod"}
  8
  9bestMethod = method()
 10bestMethod ZZ := String => i -> if i == 0 then "hello" else "bye"
 11
 12TEST ///
 13assert(bestMethod(0) == "hello")
 14///
 15
 16beginDocumentation()
 17
 18doc ///
 19Key
 20  BestPackage
 21Headline
 22  a great package
 23///
 24
 25
 26doc ///
 27Key
 28  bestMethod
 29  (bestMethod, ZZ)
 30Headline
 31  a great method, for integers
 32Usage
 33  greeting = bestMethod(i)
 34Inputs
 35  i:ZZ
 36    either 1 or something else
 37Outputs
 38  greeting:String
 39    a nice greeting
 40Description
 41  Text
 42    This method outputs a nice greeting based on input. The integer 0 gives a hello
 43  Example
 44    bestMethod(0)
 45  Text
 46    Any other input says goodbye
 47  Example
 48    bestMethod(123)
 49///
 50
 51end
 52
 53
 54restart
 55uninstallPackage "BestPackage"
 56installPackage "BestPackage"
 57viewHelp BestPackage
 58
 59needsPackage "BestPackage" -- load package
 60check BestPackage -- run all tests
 61
 62bestMethod(1)
 63bestMethod(0)
 64
 65viewHelp BestPackage
 66
 67-- for inspiration for package documentation
 68needsPackage "SimpleDoc"
 69viewHelp SimpleDoc
 70
 71
 72
 73
 74needsPackage "Graphs"
 75viewHelp Graphs
 76
 77R = QQ[x,y,z]
 78I = ideal(x^2)
 79amult I
 80
 81needsPackage "SimpleDoc"
 82viewHelp SimpleDoc
 83
 84
 85-- HashTable
 86ht = hashTable { h => "Hello", b => "Bye" }
 87
 88h
 89b
 90ht.h
 91ht.b
 92ht.akjhsdas
 93
 94ht
 95
 96ancestors class I
 97peek I
 98I.ring
 99peek I.cache
100
101f = i -> i+1
102f(7)
103f(7/2)
104f(I)
105
106g = method()
107g(7)
108
109g ZZ := ZZ => i -> i^2    -- <method name> <input type> := <output type> => i -> i^2
110g QQ := QQ => i -> i^2    -- <method name> <input type> := <output type> => i -> i^2
111g(7)
112g(1/2)
113g(I)
114
115g (QQ, QQ) := QQ => (i,j) -> i^2 + j^2   -- <method name> <input type> := <output type> => i -> i^2
116g(1/2, 3/4)
117
118
119h = method(Options => { Shift => 0 })
120h ZZ := ZZ => opts -> i -> i + 1 + opts.Shift
121
122h(7)
123h(7, Shift => 27)
124
125viewHelp newRing
126options newRing
127options h

Session 4

session_4.m2

 1restart
 2
 3ht = hashTable { 7 => "hello", b => "goodbye", QQ => "rational"}
 4
 5ht.b
 6ht#b
 7ht#7
 8ht#QQ
 9
10b = 123
11ht#b
12ht.b
13
14
15class QQ
16class {1,2,3}
17
18ancestors class {1,2,3}
19ancestors class (1,2,3)
20
21g = method()
22g Thing := i -> i + 1
23
24ancestors Ideal
25
26g(7)
27g(ideal 1)
28
29-------------
30
31-- trigger an error
32error "error is here"
33
34R = QQ[x,y]
35ideal(x)
36oo + ideal(y)
37ooo + ideal(y^2)
38oooo + ideal(y^3)
39
40viewHelp "gbTrace"
41gbTrace = 3
42
43R = QQ[x,y,z, MonomialOrder => Lex]
44I = ideal(x^9 + y^3 + z^2 - 1, x^7 + y^5 + z^3 - 1)
45gens gb I -- Ctrl + C to interrups
46
47R = QQ[x,y,z, MonomialOrder => Lex]
48ideal(x^3 + y^3 + z^2 - 1, x^7 + y^5 + z^3 - 1)
49gbTrace = 0
50elapsedTime gens gb I;
51
52
53oo
54o84
55o87
56
57gens R
58R_0
59R_1
60
610_R
621_R
63
64map(R^1,R^1,1)
65methods map
66viewHelp map
67
68x = symbol x;
69R = QQ[x_1,x_2]
70x_1
71
72f = n -> (
73  x := symbol x;
74  R := QQ[x_1..x_n];
75  ideal gens R
76)
77
78x_1
79f 3
80x_1
81
82restart
83R = QQ[x,y]
84x
85
86f = () -> QQ[x,y]; -- bad, this "uses" the newly created ring,
87                    -- messing up user variables
88
89f = () -> QQ( monoid [x,y]); -- fix, doesn't use the new ring
90
91f()
92
93x
94use R
95x