File: chap8_mj.html

package info (click to toggle)
gap-utils 0.93-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,504 kB
  • sloc: xml: 2,167; javascript: 155; makefile: 105
file content (149 lines) | stat: -rw-r--r-- 10,004 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<title>GAP (Utils) - Chapter 8: Records</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content="GAPDoc2HTML" />
<link rel="stylesheet" type="text/css" href="manual.css" />
<script src="manual.js" type="text/javascript"></script>
<script type="text/javascript">overwriteStyle();</script>
</head>
<body class="chap8"  onload="jscontent()">


<div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a>  <a href="chap1_mj.html">1</a>  <a href="chap2_mj.html">2</a>  <a href="chap3_mj.html">3</a>  <a href="chap4_mj.html">4</a>  <a href="chap5_mj.html">5</a>  <a href="chap6_mj.html">6</a>  <a href="chap7_mj.html">7</a>  <a href="chap8_mj.html">8</a>  <a href="chap9_mj.html">9</a>  <a href="chap10_mj.html">10</a>  <a href="chap11_mj.html">11</a>  <a href="chap12_mj.html">12</a>  <a href="chapBib_mj.html">Bib</a>  <a href="chapInd_mj.html">Ind</a>  </div>

<div class="chlinkprevnexttop">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp;  <a href="chap0_mj.html#contents">[Contents]</a>&nbsp;  &nbsp;<a href="chap7_mj.html">[Previous Chapter]</a>&nbsp;  &nbsp;<a href="chap9_mj.html">[Next Chapter]</a>&nbsp;  </div>

<p id="mathjaxlink" class="pcenter"><a href="chap8.html">[MathJax off]</a></p>
<p><a id="X7AA1073C7E943DD7" name="X7AA1073C7E943DD7"></a></p>
<div class="ChapSects"><a href="chap8_mj.html#X7AA1073C7E943DD7">8 <span class="Heading">Records</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap8_mj.html#X82B3D1D583CDF0E5">8.1 <span class="Heading">Functions for records</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap8_mj.html#X84D82EB579B2ACCD">8.1-1 AssignGlobals</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap8_mj.html#X7E6207B47B9AA30C">8.2 <span class="Heading">Option records for functions</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap8_mj.html#X8322B9377CC590D2">8.2-1 OptionRecordWithDefaults</a></span>
</div></div>
</div>

<h3>8 <span class="Heading">Records</span></h3>

<p><a id="X82B3D1D583CDF0E5" name="X82B3D1D583CDF0E5"></a></p>

<h4>8.1 <span class="Heading">Functions for records</span></h4>

<p><a id="X84D82EB579B2ACCD" name="X84D82EB579B2ACCD"></a></p>

<h5>8.1-1 AssignGlobals</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AssignGlobals</code>( <var class="Arg">rec</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>This function has been transferred from package <strong class="pkg">RCWA</strong>.</p>

<p>It assigns the record components of <var class="Arg">rec</var> to global variables with the same names.</p>


<div class="example"><pre>

<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := rec( a := 1, b := 2, c := 3 );;                                      </span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AssignGlobals( r );</span>
The following global variables have been assigned:
[ "a", "b", "c" ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[a,b,c];</span>
[ 1, 2, 3 ]

</pre></div>

<p><a id="X7E6207B47B9AA30C" name="X7E6207B47B9AA30C"></a></p>

<h4>8.2 <span class="Heading">Option records for functions</span></h4>

<p><a id="X8322B9377CC590D2" name="X8322B9377CC590D2"></a></p>

<h5>8.2-1 OptionRecordWithDefaults</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; OptionRecordWithDefaults</code>( <var class="Arg">defaults</var>, <var class="Arg">useroptions</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>This functions has been transferred by Chris Jefferson from other packages. It simplifies the handling of records which are intended to be used for expressing configuration options. <var class="Arg">defaults</var> represents the "default record", and <var class="Arg">useroptions</var> lets the user give new values for values in <var class="Arg">defaults</var>.</p>

<p>The function returns a record with the same component names as <var class="Arg">defaults</var> and which has the same values as <var class="Arg">defaults</var>, except for those component names in <var class="Arg">useroptions</var>, where the values in <var class="Arg">useroptions</var> are used instead. An error is given if <var class="Arg">useroptions</var> contains any component names not in <var class="Arg">defaults</var>. If <var class="Arg">useroptions</var> is an empty list it is treated as an empty record, and if <var class="Arg">useroptions</var> is a list of length <span class="SimpleMath">\(1\)</span> containing a record, this record is used as <var class="Arg">useroptions</var>.</p>


<div class="example"><pre>

<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">defaults := rec( a := 1, b := 2, c := 3 );;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, rec( a := 6) );</span>
rec( a := 6, b := 2, c := 3 )
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, rec( b := 7, c := 8 ) );</span>
rec( a := 1, b := 7, c := 8 )
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, [ ] );</span>
rec( a := 1, b := 2, c := 3 )
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, [ rec( c := 8 ) ] );</span>
rec( a := 1, b := 2, c := 8 )
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, rec( d := 9 ) );</span>
Error, Unknown option: d
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, [ rec( b := 7 ), rec( c := 8 ) ] );</span>
Error, Too many arguments for function
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OptionRecordWithDefaults( defaults, [6,7,8] );</span>
Error, Too many arguments for function

</pre></div>

<p>This function is designed to support functions with optional arguments given as a variable record, of the form <code class="code">function(x,y,options...)</code>. In the following, very contrived, example function, <code class="code">PrintDimensions</code>, the defaults are given by the variable <code class="code">order</code> which takes values <code class="code">h</code>, <code class="code">w</code> and <code class="code">d</code> having default values <span class="SimpleMath">\(1\)</span>, <span class="SimpleMath">\(2\)</span> and <span class="SimpleMath">\(3\)</span>. If there is a second argument, then <code class="code">OptionRecordWithDefaults( order, arg[2] );</code> is used to cvhange the values. These three values then determine the order in which the three dimensions are printed using a <code class="code">SortParallel</code> command.</p>


<div class="example"><pre>

PrintDimensions := function( arg ) 
    local nargs, dim, order, V, L, len, K, i; 
    nargs := Length( arg ); 
    dim := [ arg[1]!.height, arg[1]!.width, arg[1]!.depth ]; 
    order := rec( h := 1, w := 2, d := 3 ); 
    V := [ "height", "width", "depth" ]; 
    if ( nargs &gt; 1 ) and IsRecord( arg[2] ) then 
        order := OptionRecordWithDefaults( order, arg[2] ); 
    fi; 
    L := [ order!.h, order!.w, order!.d ]; 
    len := Length( L );
    K := [ 1..len ]; 
    SortParallel( L, K ); 
    Print( "dimensions: " ); 
    Print( V[K[1]], " = ", dim[K[1]], ", " );
    Print( V[K[2]], " = ", dim[K[2]], ", " );
    Print( V[K[3]], " = ", dim[K[3]], "\n" );
end;;
</pre></div>

<p>In the example below the first call to <code class="code">PrintDimensions</code> has just one parameter, <code class="code">mydim</code>, so the default order is used. In the second call, alternate values for <code class="code">h</code>, <code class="code">w</code> and <code class="code">d</code> are given, causing the width to be printed first, and then the depth and height.</p>


<div class="example"><pre>

<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">mydim := rec( height := 45, width := 31, depth := 17 ); </span>
rec( depth := 17, height := 45, width := 31 )
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrintDimensions( mydim );</span>
dimensions: height = 45, width = 31, depth = 17
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrintDimensions( mydim, rec( h:=3, w:=1, d:=2 ) );</span>
dimensions: width = 31, depth = 17, height = 45

</pre></div>


<div class="chlinkprevnextbot">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp;  <a href="chap0_mj.html#contents">[Contents]</a>&nbsp;  &nbsp;<a href="chap7_mj.html">[Previous Chapter]</a>&nbsp;  &nbsp;<a href="chap9_mj.html">[Next Chapter]</a>&nbsp;  </div>


<div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a>  <a href="chap1_mj.html">1</a>  <a href="chap2_mj.html">2</a>  <a href="chap3_mj.html">3</a>  <a href="chap4_mj.html">4</a>  <a href="chap5_mj.html">5</a>  <a href="chap6_mj.html">6</a>  <a href="chap7_mj.html">7</a>  <a href="chap8_mj.html">8</a>  <a href="chap9_mj.html">9</a>  <a href="chap10_mj.html">10</a>  <a href="chap11_mj.html">11</a>  <a href="chap12_mj.html">12</a>  <a href="chapBib_mj.html">Bib</a>  <a href="chapInd_mj.html">Ind</a>  </div>

<hr />
<p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
</body>
</html>