/usr/share/doc/libghc-maths-doc/html/src/Math-Algebra-NonCommutative-NCPoly.html is in libghc-maths-doc 0.4.8-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | <?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>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>Math/Algebra/NonCommutative/NCPoly.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>-- Copyright (c) David Amos, 2008. All rights reserved.</span>
<a name="line-2"></a>
<a name="line-3"></a><span class='hs-comment'>-- |A module providing a type for non-commutative polynomials.</span>
<a name="line-4"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Algebra</span><span class='hs-varop'>.</span><span class='hs-conid'>NonCommutative</span><span class='hs-varop'>.</span><span class='hs-conid'>NCPoly</span> <span class='hs-keyword'>where</span>
<a name="line-5"></a>
<a name="line-6"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>L</span>
<a name="line-7"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Algebra</span><span class='hs-varop'>.</span><span class='hs-conid'>Field</span><span class='hs-varop'>.</span><span class='hs-conid'>Base</span>
<a name="line-8"></a>
<a name="line-9"></a>
<a name="line-10"></a><span class='hs-comment'>-- (NON-COMMUTATIVE) MONOMIALS</span>
<a name="line-11"></a>
<a name="line-12"></a><a name="Monomial"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>Monomial</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>M</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>)</span>
<a name="line-13"></a>
<a name="line-14"></a><a name="instance%20Ord%20(Monomial%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Ord</span> <span class='hs-layout'>(</span><span class='hs-conid'>Monomial</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-15"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>ys</span><span class='hs-layout'>,</span><span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-16"></a><span class='hs-comment'>-- Glex ordering</span>
<a name="line-17"></a>
<a name="line-18"></a><a name="instance%20Show%20(Monomial%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Show</span> <span class='hs-layout'>(</span><span class='hs-conid'>Monomial</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-19"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>null</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"1"</span>
<a name="line-20"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>showPower</span> <span class='hs-layout'>(</span><span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>group</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-21"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>showPower</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>showVar</span> <span class='hs-varid'>v</span>
<a name="line-22"></a> <span class='hs-varid'>showPower</span> <span class='hs-varid'>vs</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>v</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>showVar</span> <span class='hs-varid'>v</span> <span class='hs-varop'>++</span> <span class='hs-str'>"^"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>vs</span><span class='hs-layout'>)</span>
<a name="line-23"></a> <span class='hs-varid'>showVar</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>filter</span> <span class='hs-layout'>(</span><span class='hs-varop'>/=</span> <span class='hs-chr'>'"'</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-24"></a><span class='hs-comment'>-- Taken from NonComMonomial - why don't we just use it directly</span>
<a name="line-25"></a>
<a name="line-26"></a><a name="instance%20Num%20(Monomial%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Num</span> <span class='hs-layout'>(</span><span class='hs-conid'>Monomial</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-27"></a> <span class='hs-conid'>M</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>*</span> <span class='hs-conid'>M</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>M</span> <span class='hs-layout'>(</span><span class='hs-varid'>xs</span> <span class='hs-varop'>++</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-28"></a> <span class='hs-varid'>fromInteger</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>M</span> <span class='hs-conid'>[]</span>
<a name="line-29"></a>
<a name="line-30"></a><a name="divM"></a><span class='hs-comment'>-- try to find l, r such that a = lbr</span>
<a name="line-31"></a><span class='hs-definition'>divM</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>divM'</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>where</span>
<a name="line-32"></a> <span class='hs-varid'>divM'</span> <span class='hs-varid'>ls</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-33"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>b</span> <span class='hs-varop'>`</span><span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>isPrefixOf</span><span class='hs-varop'>`</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span>
<a name="line-34"></a> <span class='hs-keyword'>then</span> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varop'>$</span> <span class='hs-varid'>reverse</span> <span class='hs-varid'>ls</span><span class='hs-layout'>,</span> <span class='hs-conid'>M</span> <span class='hs-varop'>$</span> <span class='hs-varid'>drop</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-35"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>divM'</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>ls</span><span class='hs-layout'>)</span> <span class='hs-varid'>rs</span>
<a name="line-36"></a> <span class='hs-varid'>divM'</span> <span class='hs-keyword'>_</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-37"></a>
<a name="line-38"></a>
<a name="line-39"></a><span class='hs-comment'>-- (NON-COMMUTATIVE) POLYNOMIALS</span>
<a name="line-40"></a>
<a name="line-41"></a><a name="NPoly"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>NPoly</span> <span class='hs-varid'>r</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>Monomial</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span><span class='hs-varid'>r</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>)</span>
<a name="line-42"></a>
<a name="line-43"></a><a name="instance%20Ord%20(NPoly%20r%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Ord</span> <span class='hs-varid'>r</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Ord</span> <span class='hs-layout'>(</span><span class='hs-conid'>NPoly</span> <span class='hs-varid'>r</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-44"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-varid'>us</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>us</span>
<a name="line-45"></a>
<a name="line-46"></a><a name="instance%20Show%20(NPoly%20r%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Show</span> <span class='hs-varid'>r</span><span class='hs-layout'>,</span> <span class='hs-conid'>Eq</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Show</span> <span class='hs-layout'>(</span><span class='hs-conid'>NPoly</span> <span class='hs-varid'>r</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-47"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"0"</span>
<a name="line-48"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-49"></a> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>showTerm</span> <span class='hs-varid'>ts</span>
<a name="line-50"></a> <span class='hs-keyword'>in</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>c</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'+'</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>cs</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span>
<a name="line-51"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>showTerm</span> <span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-52"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>show</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>of</span>
<a name="line-53"></a> <span class='hs-str'>"1"</span> <span class='hs-keyglyph'>-></span> <span class='hs-str'>"+"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>m</span>
<a name="line-54"></a> <span class='hs-str'>"-1"</span> <span class='hs-keyglyph'>-></span> <span class='hs-str'>"-"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>m</span>
<a name="line-55"></a> <span class='hs-comment'>-- cs@(x:_) -> (if x == '-' then cs else '+':cs) ++ (if m == 1 then "" else show m)</span>
<a name="line-56"></a> <span class='hs-varid'>cs</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>showCoeff</span> <span class='hs-varid'>cs</span> <span class='hs-varop'>++</span> <span class='hs-layout'>(</span><span class='hs-keyword'>if</span> <span class='hs-varid'>m</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span> <span class='hs-keyword'>then</span> <span class='hs-str'>""</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>show</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
<a name="line-57"></a> <span class='hs-varid'>showCoeff</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`elem`</span> <span class='hs-keyglyph'>[</span><span class='hs-chr'>'+'</span><span class='hs-layout'>,</span><span class='hs-chr'>'-'</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-varid'>cs</span>
<a name="line-58"></a> <span class='hs-keyword'>then</span> <span class='hs-str'>"+("</span> <span class='hs-varop'>++</span> <span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span> <span class='hs-varop'>++</span> <span class='hs-str'>")"</span>
<a name="line-59"></a> <span class='hs-keyword'>else</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>c</span> <span class='hs-varop'>==</span> <span class='hs-chr'>'-'</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span> <span class='hs-keyword'>else</span> <span class='hs-chr'>'+'</span><span class='hs-conop'>:</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span>
<a name="line-60"></a>
<a name="line-61"></a><a name="instance%20Num%20(NPoly%20r%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>r</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>r</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Num</span> <span class='hs-layout'>(</span><span class='hs-conid'>NPoly</span> <span class='hs-varid'>r</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-62"></a> <span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span> <span class='hs-varop'>+</span> <span class='hs-conid'>NP</span> <span class='hs-varid'>us</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-layout'>(</span><span class='hs-varid'>mergeTerms</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>us</span><span class='hs-layout'>)</span>
<a name="line-63"></a> <span class='hs-varid'>negate</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-comment'>-</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>ts</span>
<a name="line-64"></a> <span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span> <span class='hs-varop'>*</span> <span class='hs-conid'>NP</span> <span class='hs-varid'>us</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-varop'>$</span> <span class='hs-varid'>collect</span> <span class='hs-varop'>$</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>sortBy</span> <span class='hs-varid'>cmpTerm</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-varop'>*</span><span class='hs-varid'>h</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-varop'>*</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>ts</span><span class='hs-layout'>,</span> <span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-layout'>,</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>us</span><span class='hs-keyglyph'>]</span>
<a name="line-65"></a> <span class='hs-varid'>fromInteger</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-conid'>[]</span>
<a name="line-66"></a> <span class='hs-varid'>fromInteger</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>fromInteger</span> <span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>fromInteger</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-67"></a>
<a name="line-68"></a><a name="cmpTerm"></a><span class='hs-definition'>cmpTerm</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>b</span><span class='hs-layout'>,</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>compare</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>EQ</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>EQ</span><span class='hs-layout'>;</span> <span class='hs-conid'>GT</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>LT</span><span class='hs-layout'>;</span> <span class='hs-conid'>LT</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>GT</span> <span class='hs-comment'>-- in mpolys we put "larger" terms first</span>
<a name="line-69"></a>
<a name="line-70"></a><a name="mergeTerms"></a><span class='hs-comment'>-- inputs in descending order</span>
<a name="line-71"></a><span class='hs-definition'>mergeTerms</span> <span class='hs-layout'>(</span><span class='hs-varid'>t</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-layout'>,</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>us</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-72"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>cmpTerm</span> <span class='hs-varid'>t</span> <span class='hs-varid'>u</span> <span class='hs-keyword'>of</span>
<a name="line-73"></a> <span class='hs-conid'>LT</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>t</span> <span class='hs-conop'>:</span> <span class='hs-varid'>mergeTerms</span> <span class='hs-varid'>ts</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-conop'>:</span><span class='hs-varid'>us</span><span class='hs-layout'>)</span>
<a name="line-74"></a> <span class='hs-conid'>GT</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>u</span> <span class='hs-conop'>:</span> <span class='hs-varid'>mergeTerms</span> <span class='hs-layout'>(</span><span class='hs-varid'>t</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span> <span class='hs-varid'>us</span>
<a name="line-75"></a> <span class='hs-conid'>EQ</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>if</span> <span class='hs-varid'>e</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>mergeTerms</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>us</span> <span class='hs-keyword'>else</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-layout'>,</span><span class='hs-varid'>e</span><span class='hs-layout'>)</span> <span class='hs-conop'>:</span> <span class='hs-varid'>mergeTerms</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>us</span>
<a name="line-76"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>c</span> <span class='hs-varop'>+</span> <span class='hs-varid'>d</span>
<a name="line-77"></a><span class='hs-definition'>mergeTerms</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>us</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ts</span> <span class='hs-varop'>++</span> <span class='hs-varid'>us</span> <span class='hs-comment'>-- one of them is null</span>
<a name="line-78"></a>
<a name="line-79"></a><a name="collect"></a><span class='hs-definition'>collect</span> <span class='hs-layout'>(</span><span class='hs-varid'>t1</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>t2</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-layout'>,</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span>
<a name="line-80"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>g</span> <span class='hs-varop'>==</span> <span class='hs-varid'>h</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>collect</span> <span class='hs-varop'>$</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-varop'>+</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span>
<a name="line-81"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>collect</span> <span class='hs-varop'>$</span> <span class='hs-varid'>t2</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span>
<a name="line-82"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t1</span> <span class='hs-conop'>:</span> <span class='hs-varid'>collect</span> <span class='hs-layout'>(</span><span class='hs-varid'>t2</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span>
<a name="line-83"></a><span class='hs-definition'>collect</span> <span class='hs-varid'>ts</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ts</span>
<a name="line-84"></a>
<a name="line-85"></a>
<a name="line-86"></a><a name="instance%20Fractional%20(NPoly%20k%20v)"></a><span class='hs-comment'>-- Fractional instance so that we can enter fractional coefficients</span>
<a name="line-87"></a><a name="instance%20Fractional%20(NPoly%20k%20v)"></a><span class='hs-comment'>-- Only lets us divide by field elements (with unit monomial), not any other polynomials</span>
<a name="line-88"></a><a name="instance%20Fractional%20(NPoly%20k%20v)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Fractional</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Fractional</span> <span class='hs-layout'>(</span><span class='hs-conid'>NPoly</span> <span class='hs-varid'>k</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-89"></a> <span class='hs-varid'>recip</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-num'>1</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>recip</span> <span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-90"></a> <span class='hs-varid'>recip</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"NPoly.recip: only supported for (non-zero) constants"</span>
<a name="line-91"></a>
<a name="line-92"></a>
<a name="line-93"></a><span class='hs-comment'>-- SOME VARIABLES (INDETERMINATES)</span>
<a name="line-94"></a><span class='hs-comment'>-- The idea is that you define your own type of indeterminates as required, along the same lines as this</span>
<a name="line-95"></a>
<a name="line-96"></a><a name="Var"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>Var</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>X</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Y</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Z</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span><span class='hs-conid'>Ord</span><span class='hs-layout'>)</span>
<a name="line-97"></a>
<a name="line-98"></a><a name="instance%20Show%20Var"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-conid'>Var</span> <span class='hs-keyword'>where</span>
<a name="line-99"></a> <span class='hs-varid'>show</span> <span class='hs-conid'>X</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"x"</span>
<a name="line-100"></a> <span class='hs-varid'>show</span> <span class='hs-conid'>Y</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"y"</span>
<a name="line-101"></a> <span class='hs-varid'>show</span> <span class='hs-conid'>Z</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"z"</span>
<a name="line-102"></a>
<a name="line-103"></a><a name="var"></a><span class='hs-comment'>-- |Create a non-commutative variable for use in forming non-commutative polynomials.</span>
<a name="line-104"></a><span class='hs-comment'>-- For example, we could define x = var "x", y = var "y". Then x*y /= y*x.</span>
<a name="line-105"></a><span class='hs-definition'>var</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>NPoly</span> <span class='hs-varid'>k</span> <span class='hs-varid'>v</span>
<a name="line-106"></a><span class='hs-definition'>var</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-107"></a>
<a name="line-108"></a><a name="x"></a><span class='hs-definition'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>var</span> <span class='hs-conid'>X</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>NPoly</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Var</span>
<a name="line-109"></a><a name="y"></a><span class='hs-definition'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>var</span> <span class='hs-conid'>Y</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>NPoly</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Var</span>
<a name="line-110"></a><a name="z"></a><span class='hs-definition'>z</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>var</span> <span class='hs-conid'>Z</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>NPoly</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Var</span>
<a name="line-111"></a>
<a name="line-112"></a>
<a name="line-113"></a><span class='hs-comment'>-- DIVISION ALGORITHM</span>
<a name="line-114"></a>
<a name="line-115"></a><a name="lm"></a><span class='hs-definition'>lm</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>m</span>
<a name="line-116"></a><a name="lc"></a><span class='hs-definition'>lc</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>c</span>
<a name="line-117"></a><a name="lt"></a><span class='hs-definition'>lt</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-layout'>(</span><span class='hs-varid'>t</span><span class='hs-conop'>:</span><span class='hs-varid'>ts</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-118"></a>
<a name="line-119"></a><a name="quotRemNP"></a><span class='hs-comment'>-- given f, gs, find ls, rs, f' such that f = sum (zipWith3 (*) ls gs rs) + f', with f' not divisible by any g</span>
<a name="line-120"></a><span class='hs-definition'>quotRemNP</span> <span class='hs-varid'>f</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>/=</span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>quotRemNP'</span> <span class='hs-varid'>f</span> <span class='hs-layout'>(</span><span class='hs-varid'>replicate</span> <span class='hs-varid'>n</span> <span class='hs-layout'>(</span><span class='hs-num'>0</span><span class='hs-layout'>,</span><span class='hs-num'>0</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span>
<a name="line-121"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"quotRemNP: division by zero"</span>
<a name="line-122"></a> <span class='hs-keyword'>where</span>
<a name="line-123"></a> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>gs</span>
<a name="line-124"></a> <span class='hs-varid'>quotRemNP'</span> <span class='hs-num'>0</span> <span class='hs-layout'>(</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span><span class='hs-varid'>f'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span><span class='hs-varid'>f'</span><span class='hs-layout'>)</span>
<a name="line-125"></a> <span class='hs-varid'>quotRemNP'</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span><span class='hs-varid'>f'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>gs</span><span class='hs-layout'>,</span><span class='hs-conid'>[]</span><span class='hs-layout'>,</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span><span class='hs-varid'>f'</span><span class='hs-layout'>)</span>
<a name="line-126"></a> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-conop'>:</span><span class='hs-varid'>gs</span><span class='hs-layout'>,</span> <span class='hs-varid'>lrs'</span><span class='hs-layout'>,</span> <span class='hs-layout'>(</span><span class='hs-varid'>l</span><span class='hs-layout'>,</span><span class='hs-varid'>r</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span> <span class='hs-varid'>f'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-127"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>lm</span> <span class='hs-varid'>h</span> <span class='hs-varop'>`divM`</span> <span class='hs-varid'>lm</span> <span class='hs-varid'>g</span> <span class='hs-keyword'>of</span>
<a name="line-128"></a> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-varid'>l'</span><span class='hs-layout'>,</span><span class='hs-varid'>r'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>let</span> <span class='hs-varid'>l''</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>l'</span><span class='hs-layout'>,</span><span class='hs-varid'>lc</span> <span class='hs-varid'>h</span> <span class='hs-varop'>/</span> <span class='hs-varid'>lc</span> <span class='hs-varid'>g</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-129"></a> <span class='hs-varid'>r''</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>r'</span><span class='hs-layout'>,</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-130"></a> <span class='hs-varid'>h'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>h</span> <span class='hs-comment'>-</span> <span class='hs-varid'>l''</span> <span class='hs-varop'>*</span> <span class='hs-varid'>g</span> <span class='hs-varop'>*</span> <span class='hs-varid'>r''</span>
<a name="line-131"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>quotRemNP'</span> <span class='hs-varid'>h'</span> <span class='hs-layout'>(</span><span class='hs-varid'>reverse</span> <span class='hs-varid'>lrs'</span> <span class='hs-varop'>++</span> <span class='hs-layout'>(</span><span class='hs-varid'>l</span><span class='hs-varop'>+</span><span class='hs-varid'>l''</span><span class='hs-layout'>,</span><span class='hs-varid'>r</span><span class='hs-varop'>+</span><span class='hs-varid'>r''</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span> <span class='hs-varid'>f'</span><span class='hs-layout'>)</span>
<a name="line-132"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>gs</span><span class='hs-layout'>,</span><span class='hs-layout'>(</span><span class='hs-varid'>l</span><span class='hs-layout'>,</span><span class='hs-varid'>r</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>lrs'</span><span class='hs-layout'>,</span><span class='hs-varid'>lrs</span><span class='hs-layout'>,</span><span class='hs-varid'>f'</span><span class='hs-layout'>)</span>
<a name="line-133"></a> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-conid'>[]</span><span class='hs-layout'>,</span><span class='hs-varid'>lrs'</span><span class='hs-layout'>,</span><span class='hs-conid'>[]</span><span class='hs-layout'>,</span><span class='hs-varid'>f'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-134"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>lth</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lt</span> <span class='hs-varid'>h</span> <span class='hs-comment'>-- can't reduce lt h, so add it to the remainder and try to reduce the remaining terms</span>
<a name="line-135"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>quotRemNP'</span> <span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-comment'>-</span><span class='hs-varid'>lth</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>reverse</span> <span class='hs-varid'>lrs'</span><span class='hs-layout'>,</span> <span class='hs-varid'>f'</span><span class='hs-varop'>+</span><span class='hs-varid'>lth</span><span class='hs-layout'>)</span>
<a name="line-136"></a>
<a name="line-137"></a><a name="remNP"></a><span class='hs-comment'>-- It is only marginally (5-10%) more space/time efficient not to track the (lazily unevaluated) factors</span>
<a name="line-138"></a><span class='hs-definition'>remNP</span> <span class='hs-varid'>f</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>/=</span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>remNP'</span> <span class='hs-varid'>f</span> <span class='hs-num'>0</span>
<a name="line-139"></a><span class='hs-comment'>-- let result = remNP' f 0 in if result == remNP2 f gs then result else error ("remNP2 " ++ show f ++ " " ++ show gs)</span>
<a name="line-140"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"remNP: division by zero"</span>
<a name="line-141"></a> <span class='hs-keyword'>where</span>
<a name="line-142"></a> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>gs</span>
<a name="line-143"></a> <span class='hs-varid'>remNP'</span> <span class='hs-num'>0</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>f'</span>
<a name="line-144"></a> <span class='hs-varid'>remNP'</span> <span class='hs-varid'>h</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-varid'>gs</span> <span class='hs-varid'>f'</span>
<a name="line-145"></a> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-conop'>:</span><span class='hs-varid'>gs</span><span class='hs-layout'>)</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span>
<a name="line-146"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>lm</span> <span class='hs-varid'>h</span> <span class='hs-varop'>`divM`</span> <span class='hs-varid'>lm</span> <span class='hs-varid'>g</span> <span class='hs-keyword'>of</span>
<a name="line-147"></a> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-varid'>l'</span><span class='hs-layout'>,</span><span class='hs-varid'>r'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>let</span> <span class='hs-varid'>l''</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>l'</span><span class='hs-layout'>,</span><span class='hs-varid'>lc</span> <span class='hs-varid'>h</span> <span class='hs-varop'>/</span> <span class='hs-varid'>lc</span> <span class='hs-varid'>g</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-148"></a> <span class='hs-varid'>r''</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>r'</span><span class='hs-layout'>,</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-149"></a> <span class='hs-varid'>h'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>h</span> <span class='hs-comment'>-</span> <span class='hs-varid'>l''</span> <span class='hs-varop'>*</span> <span class='hs-varid'>g</span> <span class='hs-varop'>*</span> <span class='hs-varid'>r''</span>
<a name="line-150"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>remNP'</span> <span class='hs-varid'>h'</span> <span class='hs-varid'>f'</span>
<a name="line-151"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-varid'>gs</span> <span class='hs-varid'>f'</span>
<a name="line-152"></a> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span>
<a name="line-153"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>lth</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lt</span> <span class='hs-varid'>h</span> <span class='hs-comment'>-- can't reduce lt h, so add it to the remainder and try to reduce the remaining terms</span>
<a name="line-154"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>remNP'</span> <span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-comment'>-</span><span class='hs-varid'>lth</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>f'</span><span class='hs-varop'>+</span><span class='hs-varid'>lth</span><span class='hs-layout'>)</span>
<a name="line-155"></a>
<a name="line-156"></a><span class='hs-keyword'>infixl</span> <span class='hs-num'>7</span> <span class='hs-varop'>%%</span>
<a name="line-157"></a><a name="%25%25"></a><span class='hs-comment'>-- f %% gs = r where (_,r) = quotRemNP f gs</span>
<a name="line-158"></a><a name="f"></a><span class='hs-definition'>f</span> <span class='hs-varop'>%%</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>remNP</span> <span class='hs-varid'>f</span> <span class='hs-varid'>gs</span>
<a name="line-159"></a>
<a name="line-160"></a><a name="remNP2"></a><span class='hs-comment'>-- !! Not sure if the following is valid</span>
<a name="line-161"></a><span class='hs-comment'>-- The idea is to avoid dividing by lc g, because sometimes our coefficient ring is not a field</span>
<a name="line-162"></a><span class='hs-comment'>-- Passes all the knot theory tests</span>
<a name="line-163"></a><span class='hs-comment'>-- However, it may be that if we ever get a non-invertible element at the front, we are in trouble anyway</span>
<a name="line-164"></a><span class='hs-definition'>remNP2</span> <span class='hs-varid'>f</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>/=</span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>gs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>remNP'</span> <span class='hs-varid'>f</span> <span class='hs-num'>0</span>
<a name="line-165"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"remNP: division by zero"</span>
<a name="line-166"></a> <span class='hs-keyword'>where</span>
<a name="line-167"></a> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>gs</span>
<a name="line-168"></a> <span class='hs-varid'>remNP'</span> <span class='hs-num'>0</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>f'</span>
<a name="line-169"></a> <span class='hs-varid'>remNP'</span> <span class='hs-varid'>h</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-varid'>gs</span> <span class='hs-varid'>f'</span>
<a name="line-170"></a> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span><span class='hs-conop'>:</span><span class='hs-varid'>gs</span><span class='hs-layout'>)</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span>
<a name="line-171"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>lm</span> <span class='hs-varid'>h</span> <span class='hs-varop'>`divM`</span> <span class='hs-varid'>lm</span> <span class='hs-varid'>g</span> <span class='hs-keyword'>of</span>
<a name="line-172"></a> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-varid'>l'</span><span class='hs-layout'>,</span><span class='hs-varid'>r'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>let</span> <span class='hs-varid'>l''</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>l'</span><span class='hs-layout'>,</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- NP [(l',lc h / lc g)]</span>
<a name="line-173"></a> <span class='hs-varid'>r''</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>r'</span><span class='hs-layout'>,</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-174"></a> <span class='hs-varid'>lcg</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>inject</span> <span class='hs-layout'>(</span><span class='hs-varid'>lc</span> <span class='hs-varid'>g</span><span class='hs-layout'>)</span>
<a name="line-175"></a> <span class='hs-varid'>lch</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>inject</span> <span class='hs-layout'>(</span><span class='hs-varid'>lc</span> <span class='hs-varid'>h</span><span class='hs-layout'>)</span>
<a name="line-176"></a> <span class='hs-comment'>-- h' = h - l'' * g * r''</span>
<a name="line-177"></a> <span class='hs-varid'>h'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lcg</span> <span class='hs-varop'>*</span> <span class='hs-varid'>h</span> <span class='hs-comment'>-</span> <span class='hs-varid'>lch</span> <span class='hs-varop'>*</span> <span class='hs-varid'>l''</span> <span class='hs-varop'>*</span> <span class='hs-varid'>g</span> <span class='hs-varop'>*</span> <span class='hs-varid'>r''</span>
<a name="line-178"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>remNP'</span> <span class='hs-varid'>h'</span> <span class='hs-layout'>(</span><span class='hs-varid'>lcg</span> <span class='hs-varop'>*</span> <span class='hs-varid'>f'</span><span class='hs-layout'>)</span> <span class='hs-comment'>-- must multiply f' by lcg too (otherwise get incorrect results, eg tlBasis 4)</span>
<a name="line-179"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-varid'>gs</span> <span class='hs-varid'>f'</span>
<a name="line-180"></a> <span class='hs-varid'>divisionStep</span> <span class='hs-varid'>h</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span>
<a name="line-181"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>lth</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lt</span> <span class='hs-varid'>h</span> <span class='hs-comment'>-- can't reduce lt h, so add it to the remainder and try to reduce the remaining terms</span>
<a name="line-182"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>remNP'</span> <span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-comment'>-</span><span class='hs-varid'>lth</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>f'</span><span class='hs-varop'>+</span><span class='hs-varid'>lth</span><span class='hs-layout'>)</span>
<a name="line-183"></a>
<a name="line-184"></a>
<a name="line-185"></a><span class='hs-comment'>-- OTHER STUFF</span>
<a name="line-186"></a>
<a name="line-187"></a><a name="toMonic"></a><span class='hs-definition'>toMonic</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-188"></a><span class='hs-definition'>toMonic</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-189"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-varid'>ts</span>
<a name="line-190"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>d</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>-></span><span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>d</span><span class='hs-varop'>/</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>ts</span>
<a name="line-191"></a>
<a name="line-192"></a><a name="inject"></a><span class='hs-comment'>-- injection of field elements into polynomial ring</span>
<a name="line-193"></a><span class='hs-definition'>inject</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-conid'>[]</span>
<a name="line-194"></a><span class='hs-definition'>inject</span> <span class='hs-varid'>c</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>fromInteger</span> <span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>c</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-195"></a>
<a name="line-196"></a><a name="subst"></a><span class='hs-comment'>-- substitute terms for variables in an NPoly</span>
<a name="line-197"></a><span class='hs-comment'>-- eg subst [(x,a),(y,a+b),(z,c^2)] (x*y+z) -> a*(a+b)+c^2</span>
<a name="line-198"></a><span class='hs-definition'>subst</span> <span class='hs-varid'>vts</span> <span class='hs-layout'>(</span><span class='hs-conid'>NP</span> <span class='hs-varid'>us</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sum</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>inject</span> <span class='hs-varid'>c</span> <span class='hs-varop'>*</span> <span class='hs-varid'>substM</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>m</span><span class='hs-layout'>,</span><span class='hs-varid'>c</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>us</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>where</span>
<a name="line-199"></a> <span class='hs-varid'>substM</span> <span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>substV</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-200"></a> <span class='hs-varid'>substV</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span>
<a name="line-201"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>v'</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NP</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>M</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>in</span>
<a name="line-202"></a> <span class='hs-keyword'>case</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-varid'>v'</span> <span class='hs-varid'>vts</span> <span class='hs-keyword'>of</span>
<a name="line-203"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>t</span>
<a name="line-204"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-layout'>(</span><span class='hs-str'>"subst: no substitute supplied for "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>v'</span><span class='hs-layout'>)</span>
<a name="line-205"></a>
<a name="line-206"></a>
<a name="line-207"></a><span class='hs-comment'>-- INVERTIBLE</span>
<a name="line-208"></a><span class='hs-comment'>-- To support algebras which have invertible elements</span>
<a name="line-209"></a>
<a name="line-210"></a><a name="Invertible"></a><span class='hs-keyword'>class</span> <span class='hs-conid'>Invertible</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>where</span>
<a name="line-211"></a> <span class='hs-varid'>inv</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-212"></a>
<a name="line-213"></a><a name="%5e-"></a><span class='hs-definition'>x</span> <span class='hs-varop'>^-</span> <span class='hs-varid'>k</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>inv</span> <span class='hs-varid'>x</span> <span class='hs-varop'>^</span> <span class='hs-varid'>k</span>
</pre></body>
</html>
|