{"id":1322,"date":"2011-04-07T14:01:18","date_gmt":"2011-04-07T14:01:18","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/?p=1322"},"modified":"2011-04-28T06:04:02","modified_gmt":"2011-04-28T06:04:02","slug":"i1m2010-ejercicios-sobre-vectores-y-matrices-en-haskell","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/i1m2010-ejercicios-sobre-vectores-y-matrices-en-haskell\/","title":{"rendered":"I1M2010: Ejercicios sobre vectores y matrices en Haskell (1)"},"content":{"rendered":"<p>En la clase de hoy de <a href=\"http:\/\/www.cs.us.es\/~jalonso\/cursos\/i1m-10\">Inform\u00e1tica de 1\u00ba del Grado en Matem\u00e1ticas<\/a> hemos comenzado a comentar las soluciones a los ejercicios sobre vectores y matrices en Haskell de la <a href=\"https:\/\/www.glc.us.es\/~jalonso\/ejerciciosI1M2010\/index.php5\/Relaci%C3%B3n_28\">28\u00aa relaci\u00f3n<\/a>.<\/p>\n<p>Los ejercicios y su soluci\u00f3n se muestran a continuaci\u00f3n<!--more--><\/p>\n<pre lang=\"haskell\">\r\n-- ---------------------------------------------------------------------\r\n-- Introducci\u00f3n                                                       --\r\n-- ---------------------------------------------------------------------\r\n\r\n-- El objetivo de esta relaci\u00f3n es hacer ejercicios sobre vectores y\r\n-- matrices con el tipo de tablas de las tablas, definido en el m\u00f3dulo\r\n-- Data.Array y explicado en el tema 18 cuyas transparencias se\r\n-- encuentran en \r\n--    http:\/\/www.cs.us.es\/~jalonso\/cursos\/i1m-10\/temas\/tema-18t.pdf\r\n-- Adem\u00e1s, en algunos ejemplos de usan matrices con n\u00fameros racionales.\r\n-- En Haskell, el n\u00famero racional x\/y se representa por x%y. El TAD de\r\n-- los n\u00fameros racionales est\u00e1 definido en el m\u00f3dulo Data.Ratio.\r\n \r\n-- ---------------------------------------------------------------------\r\n-- Importaci\u00f3n de librer\u00edas                                           --\r\n-- ---------------------------------------------------------------------\r\n\r\nimport Data.Array\r\nimport Data.Ratio\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Tipos de los vectores y de las matrices                            --\r\n-- ---------------------------------------------------------------------\r\n\r\n-- Los vectores son tablas cuyos \u00edndices son n\u00fameros naturales.\r\ntype Vector a = Array Int a\r\n \r\n-- Las matrices son tablas cuyos \u00edndices son pares de n\u00fameros\r\n-- naturales. \r\ntype Matriz a = Array (Int,Int) a\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Operaciones b\u00e1sicas con matrices                                   --\r\n-- ---------------------------------------------------------------------\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 1. Definir la funci\u00f3n\r\n--    listaVector :: Num a => [a] -> Vector a\r\n-- tal que (listaVector xs) es el vector correspondiente a la lista\r\n-- xs. Por ejemplo, \r\n--    ghci> listaVector [3,2,5]\r\n--    array (1,3) [(1,3),(2,2),(3,5)]\r\n-- ---------------------------------------------------------------------\r\n\r\nlistaVector :: Num a => [a] -> Vector a\r\nlistaVector xs = listArray (1,n) xs\r\n    where n = length xs\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 2. Definir la funci\u00f3n\r\n--    listaMatriz :: Num a => [[a]] -> Matriz a\r\n-- tal que (listaMatriz xss) es la matriz cuyas filas son los elementos\r\n-- de xss. Por ejemplo,\r\n--    ghci> listaMatriz [[1,3,5],[2,4,7]]\r\n--    array ((1,1),(2,3)) [((1,1),1),((1,2),3),((1,3),5),\r\n--                         ((2,1),2),((2,2),4),((2,3),7)]\r\n-- ---------------------------------------------------------------------\r\n\r\nlistaMatriz :: Num a => [[a]] -> Matriz a\r\nlistaMatriz xss = listArray ((1,1),(m,n)) (concat xss)\r\n    where m = length xss\r\n          n = length (head xss)\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 3. Definir la funci\u00f3n\r\n--    numFilas :: Num a => Matriz a -> Int\r\n-- tal que (numFilas m) es el n\u00famero de filas de la matriz m. Por\r\n-- ejemplo,\r\n--    numFilas (listaMatriz [[1,3,5],[2,4,7]])  ==  2\r\n-- ---------------------------------------------------------------------\r\n\r\nnumFilas :: Num a => Matriz a -> Int\r\nnumFilas = fst . snd . bounds\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 4. Definir la funci\u00f3n\r\n--    numColumnas :: Num a => Matriz a -> Int\r\n-- tal que (numColumnas m) es el n\u00famero de columnas de la matriz\r\n-- m. Por ejemplo,\r\n--    numColumnas (listaMatriz [[1,3,5],[2,4,7]])  ==  3\r\n-- ---------------------------------------------------------------------\r\n\r\nnumColumnas:: Num a => Matriz a -> Int\r\nnumColumnas = snd . snd . bounds\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 5. Definir la funci\u00f3n\r\n--    dimension :: Num a => Matriz a -> (Int,Int)\r\n-- tal que (dimension m) es el n\u00famero de columnas de la matriz m. Por\r\n-- ejemplo, \r\n--    dimension (listaMatriz [[1,3,5],[2,4,7]])  ==  (2,3)\r\n-- ---------------------------------------------------------------------\r\n\r\ndimension :: Num a => Matriz a -> (Int,Int)\r\ndimension p = (numFilas p, numColumnas p)\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 6. Definir la funci\u00f3n\r\n--    separa :: Int -> [a] -> [[a]]\r\n-- tal que (separa n xs) es la lista obtenida separando los elementos de\r\n-- xs en grupos de n elementos (salvo el \u00faltimo que puede tener menos de\r\n-- n elementos). Por ejemplo, \r\n--    separa 3 [1..11]  ==  [[1,2,3],[4,5,6],[7,8,9],[10,11]]\r\n-- ---------------------------------------------------------------------\r\n\r\nsepara :: Int -> [a] -> [[a]]\r\nsepara _ [] = []\r\nsepara n xs = take n xs : separa n (drop n xs)\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 7. Definir la funci\u00f3n\r\n--    matrizLista :: Num a => Matriz a -> [[a]]\r\n-- tal que (matrizLista x) es la lista de las filas de la matriz x. Por\r\n-- ejemplo, \r\n--    ghci> let m = listaMatriz [[5,1,0],[3,2,6]]\r\n--    ghci> m\r\n--    array ((1,1),(2,3)) [((1,1),5),((1,2),1),((1,3),0),\r\n--                         ((2,1),3),((2,2),2),((2,3),6)]\r\n--    ghci> matrizLista m\r\n--    [[5,1,0],[3,2,6]]\r\n-- ---------------------------------------------------------------------\r\n\r\nmatrizLista :: Num a => Matriz a -> [[a]]\r\nmatrizLista p = separa (numColumnas p) (elems p)\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 8. Definir la funci\u00f3n\r\n--    vectorLista :: Num a => Vector a -> [a]\r\n-- tal que (vectorLista x) es la lista de los elementos del vector\r\n-- v. Por ejemplo, \r\n--    ghci> let v = listaVector [3,2,5]\r\n--    ghci> v\r\n--    array (1,3) [(1,3),(2,2),(3,5)]\r\n--    ghci> vectorLista v\r\n--    [3,2,5]\r\n-- ---------------------------------------------------------------------\r\n\r\nvectorLista :: Num a => Vector a -> [a]\r\nvectorLista = elems \r\n\r\n-- ---------------------------------------------------------------------\r\n-- Suma de matrices                                                   --\r\n-- ---------------------------------------------------------------------\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 9. Definir la funci\u00f3n\r\n--    sumaMatrices:: Num a => Matriz a -> Matriz a -> Matriz a\r\n-- tal que (sumaMatrices x y) es la suma de las matrices x e y. Por\r\n-- ejemplo, \r\n--    ghci> let m1 = listaMatriz [[5,1,0],[3,2,6]]\r\n--    ghci> let m2 = listaMatriz [[4,6,3],[1,5,2]]\r\n--    ghci> matrizLista (sumaMatrices m1 m2)\r\n--    [[9,7,3],[4,7,8]]\r\n-- ---------------------------------------------------------------------\r\n\r\nsumaMatrices:: Num a => Matriz a -> Matriz a -> Matriz a\r\nsumaMatrices p q = \r\n    array ((1,1),(m,n)) [((i,j),p!(i,j)+q!(i,j)) | \r\n                         i <- [1..m], j <- [1..n]]\r\n    where (m,n) = dimension p\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 10. Definir la funci\u00f3n\r\n--    filaMat :: Num a => Int -> Matriz a -> Vector a\r\n-- tal que (filaMat i p) es el vector correspondiente a la fila i-\u00e9sima\r\n-- de la matriz p. Por ejemplo,\r\n--    ghci> let p = listaMatriz [[5,1,0],[3,2,6],[4,5,7]]\r\n--    ghci> filaMat 2 p\r\n--    array (1,3) [(1,3),(2,2),(3,6)]\r\n--    ghci> vectorLista (filaMat 2 p)\r\n--    [3,2,6]\r\n-- ---------------------------------------------------------------------\r\n\r\nfilaMat :: Num a => Int -> Matriz a -> Vector a\r\nfilaMat i p = array (1,n) [(j,p!(i,j)) | j <- [1..n]]\r\n    where n = numColumnas p\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 11. Definir la funci\u00f3n\r\n--    columnaMat :: Num a => Int -> Matriz a -> Vector a\r\n-- tal que (columnaMat j p) es el vector correspondiente a la columna\r\n-- j-\u00e9sima de la matriz p. Por ejemplo,\r\n--    ghci> let p = listaMatriz [[5,1,0],[3,2,6],[4,5,7]]\r\n--    ghci> columnaMat 2 p\r\n--    array (1,3) [(1,1),(2,2),(3,5)]\r\n--    ghci> vectorLista (columnaMat 2 p)\r\n--    [1,2,5]\r\n-- ---------------------------------------------------------------------\r\n\r\ncolumnaMat :: Num a => Int -> Matriz a -> Vector a\r\ncolumnaMat j p = array (1,m) [(i,p!(i,j)) | i <- [1..m]]\r\n    where m = numFilas p\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Producto de matrices                                               --\r\n-- ---------------------------------------------------------------------\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 12. Definir la funci\u00f3n\r\n--    prodEscalar :: Num a => Vector a -> Vector a -> a\r\n-- tal que (prodEscalar v1 v2) es el producto escalar de los vectores v1\r\n-- y v2. Por ejemplo,\r\n--    ghci> let v = listaVector [3,1,10]\r\n--    ghci> prodEscalar v v\r\n--    110\r\n-- ---------------------------------------------------------------------\r\n\r\nprodEscalar :: Num a => Vector a -> Vector a -> a\r\nprodEscalar v1 v2 = \r\n    sum [i*j | (i,j) <- zip (elems v1) (elems v2)]\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 13. Definir la funci\u00f3n\r\n--    prodMatrices:: Num a => Matriz a -> Matriz a -> Matriz a\r\n-- tal que (prodMatrices p q) es el producto de las matrices p y q. Por\r\n-- ejemplo, \r\n--    ghci> let p = listaMatriz [[3,1],[2,4]]\r\n--    ghci> prodMatrices p p\r\n--    array ((1,1),(2,2)) [((1,1),11),((1,2),7),((2,1),14),((2,2),18)]\r\n--    ghci> matrizLista (prodMatrices p p)\r\n--    [[11,7],[14,18]]\r\n--    ghci> let q = listaMatriz [[7],[5]]\r\n--    ghci> prodMatrices p q\r\n--    array ((1,1),(2,1)) [((1,1),26),((2,1),34)]\r\n--    ghci> matrizLista (prodMatrices p q)\r\n--    [[26],[34]]\r\n-- ---------------------------------------------------------------------\r\n\r\nprodMatrices:: Num a => Matriz a -> Matriz a -> Matriz a\r\nprodMatrices p q = \r\n    array ((1,1),(m,n))\r\n          [((i,j), prodEscalar (filaMat i p) (columnaMat j q)) |\r\n           i <- [1..m], j <- [1..n]]\r\n    where m = numFilas p\r\n          n = numColumnas q\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Traspuestas y sim\u00e9tricas                                           --\r\n-- ---------------------------------------------------------------------\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 14. Definir la funci\u00f3n\r\n--    traspuesta :: Num a => Matriz a -> Matriz a\r\n-- tal que (traspuesta p) es la traspuesta de la matriz p. Por ejemplo,\r\n--    ghci> let p = listaMatriz [[5,1,0],[3,2,6]]\r\n--    ghci> traspuesta p\r\n--    array ((1,1),(3,2)) [((1,1),5),((1,2),3),\r\n--                         ((2,1),1),((2,2),2),\r\n--                         ((3,1),0),((3,2),6)]\r\n--    ghci> matrizLista (traspuesta p)\r\n--    [[5,3],[1,2],[0,6]]\r\n-- ---------------------------------------------------------------------\r\n\r\ntraspuesta :: Num a => Matriz a -> Matriz a\r\ntraspuesta p = \r\n    array ((1,1),(n,m))\r\n          [((i,j), p!(j,i)) | i <- [1..n], j <- [1..m]]\r\n    where (m,n) = dimension p\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 15. Definir la funci\u00f3n\r\n--    esCuadrada :: Num a => Matriz a -> Bool\r\n-- tal que (esCuadrada p) se verifica si la matriz p es cuadrada. Por\r\n-- ejemplo, \r\n--    ghci> let p = listaMatriz [[5,1,0],[3,2,6]]\r\n--    ghci> esCuadrada p\r\n--    False\r\n--    ghci> let q = listaMatriz [[5,1],[3,2]]\r\n--    ghci> esCuadrada q\r\n--    True\r\n-- ---------------------------------------------------------------------\r\n\r\nesCuadrada :: Num a => Matriz a -> Bool\r\nesCuadrada x = numFilas x == numColumnas x\r\n\r\n-- ---------------------------------------------------------------------\r\n-- Ejercicio 16. Definir la funci\u00f3n\r\n--    esSimetrica :: Num a => Matriz a -> Bool\r\n-- tal que (esSimetrica p) se verifica si la matriz p es sim\u00e9trica. Por\r\n-- ejemplo, \r\n--    ghci> let p = listaMatriz [[5,1,3],[1,4,7],[3,7,2]]\r\n--    ghci> esSimetrica p\r\n--    True\r\n--    ghci> let q = listaMatriz [[5,1,3],[1,4,7],[3,4,2]]\r\n--    ghci> esSimetrica q\r\n--    False\r\n-- ---------------------------------------------------------------------    \r\n\r\nesSimetrica :: Num a => Matriz a -> Bool\r\nesSimetrica x = x == traspuesta x\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En la clase de hoy de Inform\u00e1tica de 1\u00ba del Grado en Matem\u00e1ticas hemos comenzado a comentar las soluciones a los ejercicios sobre vectores y matrices en Haskell de la 28\u00aa relaci\u00f3n. Los ejercicios y su soluci\u00f3n se muestran a continuaci\u00f3n<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[133],"tags":[287],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/1322"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/comments?post=1322"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/1322\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/1322\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/media?parent=1322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/categories?post=1322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/tags?post=1322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}