CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Views: 418346
1
2
2 OpenMath functionality in GAP
3
4
5
2.1 Viewing OpenMath representation of an object
6
7
2.1-1 OMPrint
8
9
OMPrint( obj )  function
10
11
OMPrint writes the default XML OpenMath encoding of GAP object obj to the
12
standard output.
13
14
One can try it with different GAP objects to see if they can be converted to
15
OpenMath and learn how their OpenMath representation looks like. Here we
16
show the encoding for lists of integers and rationals:
17
18
 Example 
19

20
gap> OMPrint( [ 1, 1/2 ] ); 
21
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
22
 <OMA>
23
 <OMS cd="list1" name="list"/>
24
 <OMI>1</OMI>
25
 <OMA>
26
 <OMS cd="nums1" name="rational"/>
27
 <OMI>1</OMI>
28
 <OMI>2</OMI>
29
 </OMA>
30
 </OMA>
31
</OMOBJ>
32

33

34
35
Strings are encoded using <OMSTR> tags:
36
37
 Example 
38

39
gap> OMPrint( "This is a string" );
40
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
41
 <OMSTR>This is a string</OMSTR>
42
</OMOBJ>
43

44

45
46
Cyclotomics may be encoded in different ways dependently on their
47
properties:
48
49
 Example 
50

51
gap> OMPrint( 1-2*E(4) ); 
52
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
53
 <OMA>
54
 <OMS cd="complex1" name="complex_cartesian"/>
55
 <OMI>1</OMI>
56
 <OMI>-2</OMI>
57
 </OMA>
58
</OMOBJ>
59
gap> OMPrint(E(3)); 
60
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
61
 <OMA>
62
 <OMS cd="arith1" name="plus"/>
63
 <OMA>
64
 <OMS cd="arith1" name="times"/>
65
 <OMI>1</OMI>
66
 <OMA>
67
 <OMS cd="algnums" name="NthRootOfUnity"/>
68
 <OMI>3</OMI>
69
 <OMI>1</OMI>
70
 </OMA>
71
 </OMA>
72
 </OMA>
73
</OMOBJ>
74

75

76
77
Various encodings may be used for various types of groups:
78
79
 Example 
80

81
gap> OMPrint( Group( (1,2) ) );
82
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
83
 <OMA>
84
 <OMS cd="permgp1" name="group"/>
85
 <OMS cd="permutation1" name="right_compose"/>
86
 <OMA>
87
 <OMS cd="permut1" name="permutation"/>
88
 <OMI>2</OMI>
89
 <OMI>1</OMI>
90
 </OMA>
91
 </OMA>
92
</OMOBJ>
93
gap> OMPrint( Group( [ [ [ 1, 2 ],[ 0, 1 ] ] ] ) );
94
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
95
 <OMA>
96
 <OMS cd="group1" name="group_by_generators"/>
97
 <OMA>
98
 <OMS cd="linalg2" name="matrix"/>
99
 <OMA>
100
 <OMS cd="linalg2" name="matrixrow"/>
101
 <OMI>1</OMI>
102
 <OMI>2</OMI>
103
 </OMA>
104
 <OMA>
105
 <OMS cd="linalg2" name="matrixrow"/>
106
 <OMI>0</OMI>
107
 <OMI>1</OMI>
108
 </OMA>
109
 </OMA>
110
 </OMA>
111
</OMOBJ>
112
gap> OMPrint( FreeGroup( 2 ) ); 
113
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
114
 <OMA>
115
 <OMS cd="fpgroup1" name="free_groupn"/>
116
 <OMI>2</OMI>
117
 </OMA>
118
</OMOBJ>
119

120

121
122
Producing OpenMath representation of polynomials, one may get a warning:
123
124
 Example 
125

126
gap> x:=Indeterminate(Rationals,"x");; y:=Indeterminate(Rationals,"y");;
127
gap> OMPrint(x^2+y);
128
#I Warning : polynomial will be printed using its default ring 
129
#I because the default OpenMath polynomial ring is not specified 
130
#I or it is not contained in the default OpenMath polynomial ring.
131
#I You may ignore this or call SetOpenMathDefaultPolynomialRing to fix it.
132
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
133
 <OMA>
134
 <OMS cd="polyd1" name="DMP"/>
135
 <OMA id="polyring9qiY2oOaiITWUORb" >
136
 <OMS cd="polyd1" name="poly_ring_d"/>
137
 <OMS cd="setname1" name="Q"/>
138
 <OMI>2</OMI>
139
 </OMA>
140
 <OMA>
141
 <OMS cd="polyd1" name="SDMP"/>
142
 <OMA>
143
 <OMS cd="polyd1" name="term"/>
144
 <OMI>1</OMI>
145
 <OMI>0</OMI>
146
 <OMI>1</OMI>
147
 </OMA>
148
 <OMA>
149
 <OMS cd="polyd1" name="term"/>
150
 <OMI>1</OMI>
151
 <OMI>2</OMI>
152
 <OMI>0</OMI>
153
 </OMA>
154
 </OMA>
155
 </OMA>
156
</OMOBJ>
157

158

159
160
Indeed, now when another polynomial will be printed, it will belong to a
161
ring with a different identifier (despite GAP will be able to perform
162
arithmetical operations on these polynomials like when they belong to the
163
same ground ring):
164
165
 Example 
166

167
gap> OMPrint(x+1);
168
#I Warning : polynomial will be printed using its default ring 
169
#I because the default OpenMath polynomial ring is not specified 
170
#I or it is not contained in the default OpenMath polynomial ring.
171
#I You may ignore this or call SetOpenMathDefaultPolynomialRing to fix it.
172
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
173
 <OMA>
174
 <OMS cd="polyd1" name="DMP"/>
175
 <OMA id="polyring0LqlkhnCyLldcoBl" >
176
 <OMS cd="polyd1" name="poly_ring_d_named"/>
177
 <OMS cd="setname1" name="Q"/>
178
 <OMV name="x"/>
179
 </OMA>
180
 <OMA>
181
 <OMS cd="polyd1" name="SDMP"/>
182
 <OMA>
183
 <OMS cd="polyd1" name="term"/>
184
 <OMI>1</OMI>
185
 <OMI>1</OMI>
186
 </OMA>
187
 <OMA>
188
 <OMS cd="polyd1" name="term"/>
189
 <OMI>1</OMI>
190
 <OMI>0</OMI>
191
 </OMA>
192
 </OMA>
193
 </OMA>
194
</OMOBJ>
195

196

197
198
Thus, the warning means that it is not guaranteed that the polynomial ring
199
will have the same identifier <OMA id="polyring..." > when another or same
200
polynomial from this ring will be printed next time. If this may constitute
201
a problem, for example, if a list of polynomial is being exchanged with
202
another system and it is crucial that all of them will belong to the same
203
ring, then such ring must be created explicitly and then
204
SetOpenMathDefaultPolynomialRing must be called:
205
206
 Example 
207

208
gap> x:=Indeterminate(Rationals,"x");; y:=Indeterminate(Rationals,"y");;
209
gap> R:=PolynomialRing(Rationals,[x,y]);;
210
gap> SetOpenMathDefaultPolynomialRing(R);
211
gap> OMPrint(x^2+y);
212
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
213
 <OMA>
214
 <OMS cd="polyd1" name="DMP"/>
215
 <OMA id="polyring9eNcBGFHXkjl2kWh" >
216
 <OMS cd="polyd1" name="poly_ring_d"/>
217
 <OMS cd="setname1" name="Q"/>
218
 <OMI>2</OMI>
219
 </OMA>
220
 <OMA>
221
 <OMS cd="polyd1" name="SDMP"/>
222
 <OMA>
223
 <OMS cd="polyd1" name="term"/>
224
 <OMI>1</OMI>
225
 <OMI>0</OMI>
226
 <OMI>0</OMI>
227
 </OMA>
228
 <OMA>
229
 <OMS cd="polyd1" name="term"/>
230
 <OMI>1</OMI>
231
 <OMI>0</OMI>
232
 <OMI>0</OMI>
233
 </OMA>
234
 </OMA>
235
 </OMA>
236
</OMOBJ>
237

238

239
240
Now we can see that both polynomials belong to the ring with the same
241
identifier, and the OpenMath representation of the 2nd polynomial properly
242
reflects that it belongs to a polynomial ring with two variables.
243
244
 Example 
245

246
gap> OMPrint(x+1); 
247
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
248
 <OMA>
249
 <OMS cd="polyd1" name="DMP"/>
250
 <OMR href="#polyring9eNcBGFHXkjl2kWh" />
251
 <OMA>
252
 <OMS cd="polyd1" name="SDMP"/>
253
 <OMA>
254
 <OMS cd="polyd1" name="term"/>
255
 <OMI>1</OMI>
256
 <OMI>0</OMI>
257
 <OMI>0</OMI>
258
 </OMA>
259
 <OMA>
260
 <OMS cd="polyd1" name="term"/>
261
 <OMI>1</OMI>
262
 <OMI>0</OMI>
263
 <OMI>0</OMI>
264
 </OMA>
265
 </OMA>
266
 </OMA>
267
</OMOBJ> 
268

269

270
271
2.1-2 OMString
272
273
OMString( obj )  function
274
275
OMString returns a string with the default XML OpenMath encoding of GAP
276
object obj. If used with the noomobj option, then initial and final <OMOBJ>
277
tags will be omitted.
278
279
 Example 
280

281
gap> OMString(42);
282
"<OMOBJ xmlns=\"http://www.openmath.org/OpenMath\" version=\"2.0\"> <OMI>42</OMI> </OMOBJ>"
283
gap> OMString([1,2]:noomobj); 
284
"<OMA> <OMS cd=\"list1\" name=\"list\"/> <OMI>1</OMI> <OMI>2</OMI> </OMA>"
285

286

287
288
289
2.2 Reading OpenMath code from streams and strings
290
291
2.2-1 OMGetObject
292
293
OMGetObject( stream )  function
294
295
stream is an input stream (see InputTextFile (Reference: InputTextFile),
296
InputTextUser (Reference: InputTextUser), InputTextString (Reference:
297
InputTextString), InputOutputLocalProcess (Reference:
298
InputOutputLocalProcess), InputOutputTCPStream (SCSCP: InputOutputTCPStream
299
(for client)), InputOutputTCPStream (SCSCP: InputOutputTCPStream (for
300
server))) with an OpenMath object on it. OMGetObject takes precisely one
301
object off stream and returns it as a GAP object. Both XML and binary
302
OpenMath encoding are supported: autodetection is used.
303
304
This may be used to retrieve objects from a file. In the following example
305
we demonsrate reading the same content in binary and XML formats using the
306
test files supplied with the package (the package autodetects whether binary
307
or XML encoding is used):
308
309
 Example 
310

311
gap> txml:=Filename(DirectoriesPackageLibrary("openmath","tst"),"test3.omt");; 
312
gap> tbin:=Filename(DirectoriesPackageLibrary("openmath","tst"),"test3.bin");; 
313
gap> xstream := InputTextFile( txml );; bstream := InputTextFile( tbin );; 
314
gap> x:=OMGetObject(xstream); y:=OMGetObject(bstream);
315
912873912381273891
316
912873912381273891
317
gap> x:=OMGetObject(xstream); y:=OMGetObject(bstream);
318
E(4)
319
E(4)
320
gap> CloseStream(xstream);CloseStream(bstream);
321

322

323
324
To paste an OpenMath object directly into standard input execute the
325
following command in GAP:
326
327
 Example 
328

329
gap> s:= InputTextUser();; g := OMGetObject(s); CloseStream(s);
330
gap> 
331

332

333
334
For XML OpenMath, this function requires that the GAP package GAPDoc is
335
available.
336
337
2.2-2 EvalOMString
338
339
EvalOMString( omstr )  function
340
341
This function is an analog of EvalString (Reference: EvalString). Its
342
argument omstr must be a string containing a single OpenMath object.
343
EvalOMString will return the GAP object represented by omstr.
344
345
If omstr contains more OpenMath objects, the rest will be ignored.
346
347
 Example 
348

349
gap> s:="<OMOBJ><OMS cd=\"setname1\" name=\"Z\"/></OMOBJ>";;
350
gap> EvalOMString(s);
351
Integers
352
gap> G:=SL(2,5);; G=EvalOMString(OMString(G));
353
true
354

355

356
357
358
2.3 Writing OpenMath code to streams
359
360
While it is possible to read OpenMath code directly from a stream, writing
361
OpenMath to streams uses a different setup. It requires special objects
362
called OpenMath writers, which encapsulate streams and may be viewed as
363
transducers accepting GAP objects and writing them to a stream in the XML or
364
binary OpenMath
365
366
Such setup makes it possible to re-use the same stream for both binary and
367
XML OpenMath communication, using different OpenMath writers in different
368
calls. It also allows to re-use most of the high-level code for GAP to
369
OpenMath conversion, having separate methods for generating binary and XML
370
OpenMath only for low-level output (OpenMath tags and basic objects). This
371
makes easier adding support to new mathematical objects and private content
372
dictionaries as described in Chapter 3 since it does not require changing
373
the low-level functionality.
374
375
2.3-1 IsOpenMathWriter
376
377
IsOpenMathWriter Category
378
IsOpenMathXMLWriter Category
379
IsOpenMathBinaryWriter Category
380
381
IsOpenMathWriteris a category for OpenMath writers. It has two
382
subcategories: IsOpenMathXMLWriter and IsOpenMathBinaryWriter.
383
384
2.3-2 OpenMathXMLWriter
385
386
OpenMathXMLWriter( s )  function
387
388
for a stream s, returns an object in the category IsOpenMathXMLWriter
389
(2.3-1).
390
391
2.3-3 OpenMathBinaryWriter
392
393
OpenMathBinaryWriter( s )  function
394
395
for a stream s, returns an object in the category OpenMathBinaryWriter.
396
397
2.3-4 OMPutObject
398
399
OMPutObject( stream, obj )  function
400
OMPutObjectNoOMOBJtags( stream, obj )  function
401
402
OMPutObject writes (appends) the XML OpenMath encoding of the GAP object obj
403
to output stream stream (see InputTextFile (Reference: InputTextFile),
404
OutputTextUser (Reference: OutputTextUser), OutputTextString (Reference:
405
OutputTextString), InputOutputTCPStream (SCSCP: InputOutputTCPStream (for
406
client)), InputOutputTCPStream (SCSCP: InputOutputTCPStream (for server))).
407
408
The second version does the same but without <OMOBJ> tags, what may be
409
useful for assembling complex OpenMath objects.
410
411
 Example 
412

413
gap> g := [[1,2],[1,0]];;
414
gap> t := "";
415
""
416
gap> s := OutputTextString(t, true);;
417
gap> w:=OpenMathXMLWriter( s );
418
<OpenMath XML writer to OutputTextString(0)>
419
gap> OMPutObject(w, g);
420
gap> CloseStream(s);
421
gap> Print(t);
422
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
423
 <OMA>
424
 <OMS cd="linalg2" name="matrix"/>
425
 <OMA>
426
 <OMS cd="linalg2" name="matrixrow"/>
427
 <OMI>1</OMI>
428
 <OMI>2</OMI>
429
 </OMA>
430
 <OMA>
431
 <OMS cd="linalg2" name="matrixrow"/>
432
 <OMI>1</OMI>
433
 <OMI>0</OMI>
434
 </OMA>
435
 </OMA>
436
</OMOBJ>
437

438

439
440
2.3-5 OMPlainString
441
442
OMPlainString( string )  function
443
444
OMPlainString wraps the string into a GAP object of a special kind called an
445
OpenMath plain string. Internally such object is represented as a string,
446
but OMPutObject (2.3-4) threat it in a different way: instead of converting
447
it into a <OMSTR> object, an OpenMath plain string will be plainly
448
substituted into the output (this explains its name) without decorating it
449
with <OMSTR> tags.
450
451
It is assumed that OpenMath plain string contains valid OpenMath code; no
452
actual validation is performed during its creation. Such functionality may
453
be useful to compose some OpenMath code at the GAP level to communicate it
454
to the other system, in particular, to send there symbols which are not
455
supported by GAP, for example:
456
457
 Example 
458

459
gap> s:=OMPlainString("<OMS cd=\"nums1\" name=\"pi\"/>");
460
<OMS cd="nums1" name="pi"/>
461
gap> OMPrint(s); 
462
<OMOBJ xmlns="http://www.openmath.org/OpenMath" version="2.0">
463
 <OMS cd="nums1" name="pi"/>
464
</OMOBJ>
465

466

467
468
469
2.4 Utilities
470
471
2.4-1 OMTestXML
472
473
OMTestXML( obj )  function
474
OMTest( obj )  function
475
476
Converts obj to XML OpenMath and back. Returns true if and only if obj is
477
unchanged (as a GAP object) by this operation. The OpenMath standard does
478
not stipulate that converting to and from OpenMath should be the identity
479
function so this is a useful diagnostic tool.
480
481
 Example 
482

483
gap> OMTestXML([[1..10],[1/2,2+E(4)],ZmodnZObj(2,6),(1,2),true,"string"]); 
484
true
485

486

487
488
OMTest is a synonym to OMTestXML
489
490
2.4-2 OMTestBinary
491
492
OMTestBinary( obj )  function
493
494
Converts obj to binary OpenMath and back. Returns true if and only if obj is
495
unchanged (as a GAP object) by this operation. The OpenMath standard does
496
not stipulate that converting to and from OpenMath should be the identity
497
function so this is a useful diagnostic tool.
498
499
 Example 
500

501
gap> OMTestBinary([[1..10],[1/2,2+E(4)],ZmodnZObj(2,6),(1,2),true,"string"]); 
502
true
503

504

505
506
507