<?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>combine -- combine hash tables</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_hashing.html">next</a> | <a href="_merge_lp__Hash__Table_cm__Hash__Table_cm__Function_rp.html">previous</a> | <a href="_hashing.html">forward</a> | <a href="_merge_lp__Hash__Table_cm__Hash__Table_cm__Function_rp.html">backward</a> | <a href="_hash_sptables.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="_hash_sptables.html" title="">hash tables</a> > <a href="_combine.html" title="combine hash tables">combine</a></div> <hr/> <div><h1>combine -- combine hash tables</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>z = combine(x,y,f,g,h)</tt></div> </dd></dl> </div> </li> <li><div class="single">Inputs:<ul><li><span><tt>x</tt>, a hash table</span></li> <li><span><tt>y</tt>, a hash table of the same class as <tt>x</tt></span></li> <li><span><tt>f</tt>, a function of two variables to be used for combining a key of <tt>x</tt> with a key of <tt>y</tt> to make a new key for <tt>z</tt>.</span></li> <li><span><tt>g</tt>, a function of two variables to be used for combining a value of <tt>x</tt> with a value of <tt>y</tt> to make a new value for <tt>z</tt>.</span></li> <li><span><tt>h</tt>, a function of two variables to be used for combining two values returned by <tt>g</tt> when the corresponding keys returned by <tt>f</tt> turn out to be equal. Its first argument will be the value accumulated so far, and its second argument will be a value just provided by <tt>g</tt>.</span></li> </ul> </div> </li> <li><div class="single">Outputs:<ul><li><span><tt>z</tt>, a new hash table, of the same class as <tt>x</tt> and <tt>y</tt>, containing the pair <tt>f(p,q) => g(b,c)</tt> whenever <tt>x</tt> contains the pair <tt>p => b</tt> and <tt>y</tt> contains the pair <tt>q => c</tt>, except that <tt>h</tt> is used to combine values when two keys coincide. If <tt>f</tt> or <tt>g</tt> evaluates <a href="_continue.html" title="continue with the next iteration of a loop">continue</a>, then nothing is contributed to the resulting hash table. If <tt>h</tt> evaluates <a href="_continue.html" title="continue with the next iteration of a loop">continue</a>, then, at that point, the entry stored under the key <tt>f(p,q)</tt> in the hash table under construction is removed.</span></li> </ul> </div> </li> </ul> </div> <div class="single"><h2>Description</h2> <div>The function <tt>f</tt> is applied to every pair <tt>(p,q)</tt> where <tt>p</tt> is a key of <tt>x</tt> and <tt>q</tt> is a key of <tt>y</tt>. The number of times <tt>f</tt> is evaluated is thus the product of the number of keys in <tt>x</tt> and the number of keys in <tt>y</tt>.<p/> The function <tt>h</tt> should be an associative function, for otherwise the result may depend on internal details about the implementation of hash tables that affect the order in which entries are encountered. If <tt>f</tt>, <tt>g</tt>, and <tt>h</tt> are commutative functions as well, then the result <tt>z</tt> is a commutative function of <tt>x</tt> and <tt>y</tt>.<p/> The result is mutable if and only if <tt>x</tt> or <tt>y</tt> is.<p/> This function can be used for multiplying polynomials, where it can be used in code something like this:<pre> combine(x, y, monomialTimes, coeffTimes, coeffPlus)</pre> We illustrate that with a simple-minded implmentation of the free ring on the English alphabet, representing words as string and polynomials as hash tables that associate coefficients to words.<table class="examples"><tr><td><pre>i1 : Poly = new Type of HashTable o1 = Poly o1 : Type</pre> </td></tr> <tr><td><pre>i2 : p = new Poly from { "" => 1, "x" => 2, "y" => 3, "cat" => 5 } o2 = Poly{ => 1 } cat => 5 x => 2 y => 3 o2 : Poly</pre> </td></tr> <tr><td><pre>i3 : Poly * Poly := (p,q) -> combine(p,q,concatenate,times,plus);</pre> </td></tr> <tr><td><pre>i4 : p*p o4 = Poly{ => 1 } cat => 10 catcat => 25 catx => 10 caty => 15 x => 4 xcat => 10 xx => 4 xy => 6 y => 6 ycat => 15 yx => 6 yy => 9 o4 : Poly</pre> </td></tr> </table> </div> </div> <div class="single"><h2>See also</h2> <ul><li><span><a href="_merge_lp__Hash__Table_cm__Hash__Table_cm__Function_rp.html" title="merge hash tables">merge</a> -- merge hash tables</span></li> </ul> </div> </div> </body> </html>