Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4743

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>method -- make a new method function</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_printing_spto_spthe_spscreen.html">next</a> | <a href="_making_spa_spnew_spmethod_spfunction.html">previous</a> | forward | backward | <a href="_making_spa_spnew_spmethod_spfunction.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_making_spa_spnew_spmethod_spfunction.html" title="">making a new method function</a> > <a href="_method.html" title="make a new method function">method</a></div>
<hr/>
<div><h1>method -- make a new method function</h1>
<div class="single"><h2>Synopsis</h2>
<ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>f = method()</tt></div>
</dd></dl>
</div>
</li>
<li><div class="single">Outputs:<ul><li><span><tt>f</tt>, <span>an object of class <a href="___Method__Function.html" title="a type of method function">MethodFunction</a></span>, a new method function</span></li>
</ul>
</div>
</li>
<li><div class="single"><a href="_using_spfunctions_spwith_spoptional_spinputs.html">Optional inputs</a>:<ul><li><span><tt>TypicalValue => </tt><span><span>a <a href="___Type.html">type</a></span>, <span>default value Thing</span>, the type of the value returned by <tt>f</tt>, typically.  This information is used only to build documentation automatically, and is stored in the hash table <a href="_typical__Values.html" title="types of values returned by functions">typicalValues</a>.</span></span></li>
<li><span><tt>Options => </tt><span><span>a <a href="___List.html">list</a></span>, <span>default value null</span>, a list of options <tt>J => v</tt>, where <tt>J</tt> is the name of an optional argument for <tt>f</tt>, and <tt>v</tt> is its default value.  The list of options could be also replaced by the corresponding <a href="___Option__Table.html" title="the class of hash tables for optional arguments">OptionTable</a>.  Specifying <tt>true</tt> here indicates that option handling is done by the individual methods, while still allowing dispatching to methods based on types of arguments to be done.  The individual methods may have various sets of acceptable option names, possibly empty.  See <a href="_making_spnew_spfunctions_spwith_spoptional_sparguments.html" title="">making new functions with optional arguments</a>.</span></span></li>
<li><span><tt>Binary => </tt><span><span>a <a href="___Boolean.html">Boolean value</a></span>, <span>default value false</span>, whether the method is to be binary: for three arguments or more the result will be computed by calling binary methods installed for <tt>f</tt> with two arguments at a time.</span></span></li>
<li><span><tt>Dispatch => </tt><span><span>default value {Thing, Thing, Thing, Thing}</span>, the method for getting a list of types from the parameters; the value of this option should be <a href="___Thing.html" title="the class of all things">Thing</a> or <a href="___Type.html" title="the class of all types">Type</a> to indicate that a sequence should be regarded as a single argument, or, if the elements of a sequence are to be regarded as separate parameters, a list whose elements are <a href="___Thing.html" title="the class of all things">Thing</a> or <a href="___Type.html" title="the class of all types">Type</a>.  Parameters corresponding to <a href="___Thing.html" title="the class of all things">Thing</a> or to a position beyond the end of the list are dispatched according to their <a href="_class.html" title="class of an object">class</a>, whereas parameters corresponding to <a href="___Type.html" title="the class of all types">Type</a> are expected to be types (actually, hash tables) and are used directly in the search for methods; see <a href="_inheritance.html" title="">inheritance</a>.</span></span></li>
</ul>
</div>
</li>
</ul>
</div>
<div class="single"><h2>Description</h2>
<div><span>The code above creates a method function that takes up to four arguments, looking up the appropriate method according to the classes of the arguments, with inheritance.  To install a method for two arguments, (x,y), of classes X and Y, use code like this:</span><pre>     f(X,Y) := (x,y) -> ...</pre>
<span>where '...' represents the body of the function you wish to install.  The syntax for one or three arguments is analogous.  See <a href="__co_eq.html" title="assignment of method or new local variable">:=</a> for details.</span><table class="examples"><tr><td><pre>i1 : f = method()

o1 = f

o1 : MethodFunction</pre>
</td></tr>
<tr><td><pre>i2 : f ZZ := x -> -x;</pre>
</td></tr>
<tr><td><pre>i3 : f(ZZ,String) := (n,s) -> concatenate(n:s);</pre>
</td></tr>
<tr><td><pre>i4 : f(String,ZZ,String) := (s,n,t) -> concatenate(s," : ",toString n," : ",t);</pre>
</td></tr>
<tr><td><pre>i5 : f 44

o5 = -44</pre>
</td></tr>
<tr><td><pre>i6 : f(5,"abcd ")

o6 = abcd abcd abcd abcd abcd </pre>
</td></tr>
<tr><td><pre>i7 : f("foo",88,"bar")

o7 = foo : 88 : bar</pre>
</td></tr>
</table>
<p>In the following example we install a asymmetric method to illustrate the left-associative order of evaluation for a <em>binary</em> method function.</p>
<table class="examples"><tr><td><pre>i8 : p = method(Binary => true, TypicalValue => List)

o8 = p

o8 : CompiledFunctionClosure</pre>
</td></tr>
<tr><td><pre>i9 : p(ZZ,ZZ) := p(List,ZZ) := (i,j) -> {i,j}

o9 = {*Function[stdio:9:32-9:38]*}

o9 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i10 : p(1,2)

o10 = {1, 2}

o10 : List</pre>
</td></tr>
<tr><td><pre>i11 : p(1,2,3,4,5,6)

o11 = {{{{{1, 2}, 3}, 4}, 5}, 6}

o11 : List</pre>
</td></tr>
</table>
By default, at most four arguments (in a sequence) can be handled by a method function, and the types have to be considered separately when installing methods.  In this example, we define a method function that treats a sequence as a single argument, and we install a method for handling such arguments.<table class="examples"><tr><td><pre>i12 : g = method(Dispatch => Thing);</pre>
</td></tr>
<tr><td><pre>i13 : g ZZ := i -> -i;</pre>
</td></tr>
<tr><td><pre>i14 : g Sequence := S -> reverse S;</pre>
</td></tr>
<tr><td><pre>i15 : g 44

o15 = -44</pre>
</td></tr>
<tr><td><pre>i16 : g(3,4,5,6)

o16 = (6, 5, 4, 3)

o16 : Sequence</pre>
</td></tr>
</table>
Here we define a method whose first argument is to be a type.  It will convert its second argument to that type.<table class="examples"><tr><td><pre>i17 : h = method(Dispatch => {Type})

o17 = h

o17 : MethodFunction</pre>
</td></tr>
<tr><td><pre>i18 : h(QQ,ZZ) := (QQ,n) -> n/1;</pre>
</td></tr>
<tr><td><pre>i19 : h(RR,ZZ) := (RR,n) -> n + 0.;</pre>
</td></tr>
<tr><td><pre>i20 : h(ZZ,ZZ) := (ZZ,n) -> n;</pre>
</td></tr>
<tr><td><pre>i21 : h(ZZ,14)

o21 = 14</pre>
</td></tr>
<tr><td><pre>i22 : h(QQ,14)

o22 = 14

o22 : QQ</pre>
</td></tr>
<tr><td><pre>i23 : h(RR,14)

o23 = 14

o23 : RR (of precision 53)</pre>
</td></tr>
</table>
<p>In the next example we make a linear function of a single real variable whose coefficients are provided as optional arguments named Slope and Intercept, with default value 1.</p>
<table class="examples"><tr><td><pre>i24 : r = method(Options => {Slope => 1, Intercept => 1})

o24 = r

o24 : MethodFunctionWithOptions</pre>
</td></tr>
</table>
<p>The methods installed for this method function should be written in the form <tt>opts -> args -> (...)</tt>.  The argument <tt>opts</tt> will be assigned a hash table of type <a href="___Option__Table.html" title="the class of hash tables for optional arguments">OptionTable</a> containing the optional argument names and their current values.  For example, in the body of the function, the current value for the argument named <tt>b</tt> can be recovered with <tt>opts#b</tt>, or with <tt>opts.b</tt>, in case <tt>b</tt> is known to be a global symbol.  Be careful not to change the value of <tt>b</tt>, or the code will stop working; it would be a good idea to protect it.</p>
<table class="examples"><tr><td><pre>i25 : r RR := o -> x -> o.Slope * x + o.Intercept

o25 = {*Function[stdio:25:10-25:34]*}

o25 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i26 : r(5.)

o26 = 6

o26 : RR (of precision 53)</pre>
</td></tr>
<tr><td><pre>i27 : r(5.,Slope=>100)

o27 = 501

o27 : RR (of precision 53)</pre>
</td></tr>
</table>
<p>The default <a href="___Option__Table.html">option table</a> for <tt>r</tt> can be recovered with the function <a href="_options.html" title="get options">options</a>.  The options given to <a href="_method.html" title="make a new method function">method</a> can be recovered with <a href="_method__Options_lp__Function_rp.html" title="recover the options used when a method function was created">methodOptions</a>.</p>
<table class="examples"><tr><td><pre>i28 : options r

o28 = OptionTable{Intercept => 1}
                  Slope => 1

o28 : OptionTable</pre>
</td></tr>
<tr><td><pre>i29 : methodOptions r

o29 = OptionTable{Binary => false                         }
                  Dispatch => {Thing, Thing, Thing, Thing}
                  Options => {Slope => 1, Intercept => 1}
                  TypicalValue => Thing

o29 : OptionTable</pre>
</td></tr>
</table>
<p>In this example we defined a method function that leaves option processing to the individual methods.</p>
<table class="examples"><tr><td><pre>i30 : s = method(Options => true)

o30 = s

o30 : MethodFunctionWithOptions</pre>
</td></tr>
<tr><td><pre>i31 : s ZZ := { Slope => 17 } >> o -> x -> o.Slope * x

o31 = {*Function[/builddir/build/BUILD/Macaulay2-1.3.1-r10737/Macaulay2/m2/option.m2:6:19-8:33]*}

o31 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i32 : s RR := { Intercept => 11 } >> o -> x -> x + o.Intercept

o32 = {*Function[/builddir/build/BUILD/Macaulay2-1.3.1-r10737/Macaulay2/m2/option.m2:6:19-8:33]*}

o32 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i33 : s 100

o33 = 1700</pre>
</td></tr>
<tr><td><pre>i34 : s 1000.

o34 = 1011

o34 : RR (of precision 53)</pre>
</td></tr>
<tr><td><pre>i35 : options s</pre>
</td></tr>
<tr><td><pre>i36 : options(s,ZZ)

o36 = OptionTable{Slope => 17}

o36 : OptionTable</pre>
</td></tr>
<tr><td><pre>i37 : options(s,RR)

o37 = OptionTable{Intercept => 11}

o37 : OptionTable</pre>
</td></tr>
</table>
</div>
</div>
<div class="single"><h2>See also</h2>
<ul><li><span><a href="_methods.html" title="list methods">methods</a> -- list methods</span></li>
<li><span><a href="_specifying_sptypical_spvalues.html" title="">specifying typical values</a></span></li>
</ul>
</div>
</div>
</body>
</html>