{"id":3912,"date":"2018-03-28T08:57:21","date_gmt":"2018-03-28T06:57:21","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3912"},"modified":"2018-04-05T07:04:57","modified_gmt":"2018-04-05T05:04:57","slug":"operaciones-binarias-con-matrices","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/operaciones-binarias-con-matrices\/","title":{"rendered":"Operaciones binarias con matrices"},"content":{"rendered":"<p>Entre dos matrices de la misma dimensi\u00f3n se pueden aplicar distintas operaciones binarias entre los elementos en la misma posici\u00f3n. Por ejemplo, si a y b son las matrices<\/p>\n<pre lang=\"text\">\n   |3 4 6|     |1 4 2|\n   |5 6 7|     |2 1 2|\n<\/pre>\n<p>entonces a+b y a-b son, respectivamente<\/p>\n<pre lang=\"text\">\n   |4 8 8|     |2 0 4|\n   |7 7 9|     |3 5 5\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   opMatriz :: (Int -> Int -> Int) -> \n               Matriz Int -> Matriz Int -> Matriz Int\n<\/pre>\n<p>tal que (opMatriz f p q) es la matriz obtenida aplicando la operaci\u00f3n f entre los elementos de p y q de la misma posici\u00f3n. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> a = listArray ((1,1),(2,3)) [3,4,6,5,6,7]\n   \u03bb> b = listArray ((1,1),(2,3)) [1,4,2,2,1,2]\n   \u03bb> elems (opMatriz (+) a b)\n   [4,8,8,7,7,9]\n   \u03bb> elems (opMatriz max a b)\n   [3,4,6,5,6,7]\n   \u03bb> c = listArray ((1,1),(2,2)) [\"ab\",\"c\",\"d\",\"ef\"]\n   \u03bb> d = listArray ((1,1),(2,2)) [3,1,0,5]\n   \u03bb> elems (opMatriz menor c d)\n   [True,False,False,True]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Array\n\n-- 1\u00aa soluci\u00f3n\nopMatriz :: (a -> b -> c) ->\n            Array (Int,Int) a -> Array (Int,Int) b -> Array (Int,Int) c\nopMatriz f p q =\n  array (bounds p) [(k, f (p!k) (q!k)) | k <- indices p]\n          \n-- 2\u00aa soluci\u00f3n\nopMatriz2 :: (a -> b -> c) ->\n            Array (Int,Int) a -> Array (Int,Int) b -> Array (Int,Int) c\nopMatriz2 f p q = \n  listArray (bounds p) [f x y | (x,y) <- zip (elems p) (elems q)]\n\n-- 3\u00aa soluci\u00f3n\nopMatriz3 :: (a -> b -> c) ->\n            Array (Int,Int) a -> Array (Int,Int) b -> Array (Int,Int) c\nopMatriz3 f p q = \n  listArray (bounds p) (zipWith f (elems p) (elems q))\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Entre dos matrices de la misma dimensi\u00f3n se pueden aplicar distintas operaciones binarias entre los elementos en la misma posici\u00f3n. Por ejemplo, si a y b son las matrices |3 4 6| |1 4 2| |5 6 7| |2 1 2| entonces a+b y a-b son, respectivamente |4 8 8| |2 0 4| |7 7&#8230;<\/p>\n","protected":false},"author":1,"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":[5],"tags":[250,43,8,245,82,72,42,11,9,76],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3912"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=3912"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3912\/revisions"}],"predecessor-version":[{"id":3944,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3912\/revisions\/3944"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}