Menu Close

Matrices dispersas

Una matriz es dispersa si la mayoriá de sus elementos son ceros. Por ejemplo, la primera de las siguientes matrices es dispersa y la segunda no lo es

   ( 0 0 4 )   ( 0 1 4 )
   ( 0 5 0 )   ( 0 5 1 )
   ( 0 0 0 )

Usando la librería Data.Matrix, las anteriores matrices se pueden definir por

   ej1, ej2 :: Matrix Int
   ej1 = fromList 3 3 [0,0,4,0,5,0,0,0,0]
   ej2 = fromList 2 3 [0,1,4,0,5,1]

La dispersión de una matriz es el cociente entre el número de ceros de la matriz y el producto de sus números de filas y de columnas.

Definir las siguientes funciones

   dispersion :: (Num a, Eq a) => Matrix a -> Double
   esDispersa :: (Num a, Eq a) => Matrix a -> Bool

tales que

  • (dispersion p) es la dispersión de la matriz p. Por ejemplo,
     dispersion ej1              ==  0.7777777777777778
     dispersion ej2              ==  0.3333333333333333
     dispersion (fmap (+1) ej1)  ==  0.0
     dispersion (identity 3)     ==  0.6666666666666666
     dispersion (zero 9 9)       ==  1.0
  • (esDispersa p) se verifica si la matriz p es dispersa. Por ejemplo,
     esDispersa ej1              ==  True
     esDispersa ej2              ==  False
     esDispersa (fmap (+1) ej1)  ==  False
     esDispersa (identity 3)     ==  True
     esDispersa (zero 9 9)       ==  True

Soluciones

import Data.Matrix (Matrix, fromList, nrows, ncols, toList)
 
ej1, ej2 :: Matrix Int
ej1 = fromList 3 3 [0,0,4,0,5,0,0,0,0]
ej2 = fromList 2 3 [0,1,4,0,5,1]
 
dispersion :: (Num a, Eq a) => Matrix a -> Double
dispersion p =
  fi nCeros / (fi nrows * fi ncols)
  where fi f = (fromIntegral . f) p
 
-- (nCeros p) es el número de ceros de la matriz p. Por ejemplo,
--    nCeros ej1  ==  7
--    nCeros ej2  ==  2
nCeros :: (Num a, Eq a) => Matrix a -> Int
nCeros p = length (filter (== 0) (toList p))
 
-- La función anterior se puede definir sin argumentos:
nCeros2 :: (Num a, Eq a) => Matrix a -> Int
nCeros2 = length . filter (== 0) . toList
 
esDispersa :: (Num a, Eq a) => Matrix a -> Bool
esDispersa p = dispersion p > 0.5
 
-- La función anterior se puede definir sin argumentos:
esDispersa2 :: (Num a, Eq a) => Matrix a -> Bool
esDispersa2 = (> 0.5) . dispersion
Inicial

10 soluciones de “Matrices dispersas

  1. jaibengue
    import Data.Matrix
     
    ej1, ej2 :: Matrix Int
    ej1 = fromList 3 3 [0,0,4,0,5,0,0,0,0]
    ej2 = fromList 2 3 [0,1,4,0,5,1]
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p = (sum[1 | i <- [1..r], j <- [1..c], p ! (i,j) == 0]) / fromIntegral(r*c)
      where r = nrows p
            c = ncols p
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa p = dispersion p > 0.5
  2. alerodrod5
     
    import Data.Matrix
    import Data.List (genericLength)
    posiciones0 ::(Num a, Eq a) => Matrix a -> [(Int,Int)]
    posiciones0 m =[(i,j) | i <- [1..r], j <- [1..c], m ! (i,j) == 0]
      where r = nrows m
            c = ncols m
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion m = genericLength (posiciones0 m)/ fromIntegral ((nrows m)*(ncols m))  
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa m = dispersion m > 0.5
  3. albcarcas1
     
    import Data.Matrix (toLists)
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p = sum[1 | x <- concat(toLists p), x == 0] / fromIntegral((nrows p)*(ncols p))
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa p = dispersion p > 0.5
  4. jorcatote
    import Data.Matrix
    import Data.List (genericLength)
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion = (/) <$> foldl (acc x -> if x == 0 then acc + 1 else acc) 0 
                 <*> genericLength . lista
      where lista a = [a ! (i,j) | i <- [1..nrows a], j <- [1..ncols a]]
     
    esDispersa :: (Num a,Eq a) => Matrix a -> Bool
    esDispersa = (<) 0.5 . dispersion
  5. angruicam1
    import Data.List   (genericLength)
    import Data.Matrix (Matrix, fromList, identity,
                        zero, ncols, nrows, toList)
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p =
      genericLength (filter (== 0) (toList p)) / fromIntegral (m*n)
      where m = nrows p
            n = ncols p
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa = (> 0.5) . dispersion
  6. esppercab

    import Data.Matrix
    import Data.List

    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p = genericLength [1| i <- [1..a], j <- [1..b], (getElem i j p) == 0]/fromIntegral (a*b)
        where a = nrows p
              b = ncols p
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa p = dispersion p > 0.5
  7. antgongar

    Se puede definir dispersion sin usar argumentos

    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion = (/) <$> (fromIntegral . nCeros) <*>
                         (fromIntegral . ((*) <$> nrows <*> ncols))
  8. menvealer
     
    import Data.Matrix (Matrix, fromList, ncols, nrows, toList)
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p = fromIntegral(length (filter (== 0)(toList p))) / fromIntegral (nrows p * ncols p)
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa p = length (filter (== 0) (toList p)) > length (filter (/= 0) (toList p))
  9. carbremor
    import Data.List
    import Data.Matrix
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p  = genericLength (filter (== 0) (toList p)) / genericLength (toList p)
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa p | dispersion p > 0.5    = True
                 | otherwise             = False
     
     
    ej1, ej2 :: Matrix Int
    ej1 = fromList 3 3 [0,0,4,0,5,0,0,0,0]
    ej2 = fromList 2 3 [0,1,4,0,5,1]
     
    -- Ejemplos
    -- λ> dispersion ej1
    -- 0.7777777777777778
    -- (0.01 secs, 101,176 bytes)
     
    -- λ> dispersion ej2
    -- 0.3333333333333333
    -- (0.01 secs, 97,328 bytes)
     
    -- λ> esDispersa ej1
    -- True
    -- (0.01 secs, 81,176 bytes)
     
    -- λ> esDispersa ej2
    -- False
    -- (0.01 secs, 77,200 bytes)
  10. anadebla
     
     
    import Data.Matrix as M
    import Data.List
     
     
    dispersion :: (Num a, Eq a) => Matrix a -> Double
    dispersion p = (ceros p)/ fromIntegral (filasXcolumnas p)
     
     
    ceros p = genericLength (filter (== 0) (toList p))
     
     
    filasXcolumnas p =  (nrows p) * (ncols p)
     
     
    esDispersa :: (Num a, Eq a) => Matrix a -> Bool
    esDispersa p = dispersion p > 0.5
     
     
    ej1 = fromList 3 3 [0,0,4,0,5,0,0,0,0]
     
    ej2 = fromList 2 3 [0,1,4,0,5,1]

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.