Note that there are some explanatory texts on larger screens.

plurals
  1. POTroubles with matrix division
    primarykey
    data
    text
    <p>I had the idea to write a matrix division in Haskell as <code>Muliply A* Inverse B</code>.</p> <p>I wrote some code to do that, but the compiler had the idea to stop me. It shows the following error:</p> <pre><code>Invalid type signature :MatrixDivision :: Matrix-&gt; Matrix-&gt; Matrix at line 86:1 Should be of (variable)::(Type) </code></pre> <p>What do I wrong? This is the code:</p> <pre><code>import List type Vector = [Int] type Matrix = [Vector] --basic constructions for vectors zeroVector :: Int -&gt; Vector zeroVector n = replicate n 0 --basic operations for vectors dotProduct :: Vector -&gt; Vector -&gt; Int dotProduct v w = sum ( zipWith (*) v w ) vectorSum :: Vector -&gt; Vector -&gt; Vector vectorSum = zipWith (+) vectorScalarProduct :: Int -&gt; Vector -&gt; Vector vectorScalarProduct n vec = [ n * x | x &lt;- vec ] --basic constructions for matrices -- elemMatrix n i j v is the n-by-n elementary matrix with -- entry v in the (i,j) place elemMatrix :: Int -&gt; Int -&gt; Int -&gt; Int -&gt; Matrix elemMatrix n i j v = [ [ entry row column | column &lt;- [1..n] ] | row &lt;- [1..n] ] where entry x y | x == y = 1 | x == i &amp;&amp; y == j = v | otherwise = 0 idMatrix :: Int -&gt; Matrix idMatrix n = elemMatrix n 1 1 1 zeroMatrix :: Int -&gt; Int -&gt; Matrix zeroMatrix i j = replicate i (zeroVector j) --basic operations for matrices matrixSum :: Matrix -&gt; Matrix -&gt; Matrix matrixSum = zipWith vectorSum matrixScalarProduct :: Int -&gt; Matrix -&gt; Matrix matrixScalarProduct n m = [ vectorScalarProduct n row | row &lt;- m ] matrixProduct :: Matrix -&gt; Matrix -&gt; Matrix matrixProduct m n = [ map (dotProduct r) (transpose n) | r &lt;- m ] {- The determinant and inverse functions given here are only for examples of Haskell syntax. Efficient versions using row operations are implemented in RowOperations.hs .-} --determinant using cofactors remove :: Matrix -&gt; Int -&gt; Int -&gt; Matrix remove m i j | m == [] || i &lt; 1 || i &gt; numRows m || j &lt; 1 || j &gt; numColumns m = error "(i,j) out of range" | otherwise = transpose ( cut (transpose ( cut m i ) ) j ) determinant :: Matrix -&gt; Int determinant [] = error "determinant: 0-by-0 matrix" determinant [[n]] = n determinant m = sum [ (-1)^(j+1) * (head m)!!(j-1) * determinant (remove m 1 j) | j &lt;- [1..(numColumns m) ] ] --inverse cofactor :: Matrix -&gt; Int -&gt; Int -&gt; Int cofactor m i j = (-1)^(i+j) * determinant (remove m i j) cofactorMatrix :: Matrix -&gt; Matrix cofactorMatrix m = [ [ (cofactor m i j) | j &lt;- [1..n] ] | i &lt;- [1..n] ] where n = length m inverse :: Matrix -&gt; Matrix inverse m = transpose [ [ quot x ( determinant m) | x &lt;- row ] | row &lt;- (cofactorMatrix m) ] --Matrix Division MatrixDivision :: Matrix -&gt; Matrix -&gt; Matrix MatrixDivision m n = matrixProduct m inverse(n) </code></pre> <p>I wrote the full code to give more information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload