Last active
August 29, 2015 14:14
-
-
Save joseanpg/4005ca0acd6ba7732436 to your computer and use it in GitHub Desktop.
@Jose_A_Alonso Exercitium: Mínimo producto escalar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://www.glc.us.es/~jalonso/exercitium/minimo-producto-escalar/ | |
import Data.List | |
----------------------------------------------------------------------------------------- | |
a <·> b = sum (zipWith (*) a b) | |
----------------------------------------------------------------------------------------- | |
menorProductoEscalar v1 v2 = | |
let p1 = permutations v1 | |
p2 = permutations v2 | |
alpha [] a = a | |
alpha (x:xs) a = beta a x p2 (alpha xs) | |
beta a x [] cont = cont a | |
beta a x (y:ys) cont = beta (min a (x <·> y)) x ys cont | |
in alpha p1 (v1 <·> v2) | |
----------------------------------------------------------------------------------------- | |
menorProductoEscalar' v1 v2 = | |
minimum [ x <·> y | x <- permutations v1, y <- permutations v2] | |
----------------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://www.glc.us.es/~jalonso/exercitium/minimo-producto-escalar/ | |
import Data.List | |
----------------------------------------------------------------------------------------- | |
a <·> b = sum (zipWith (*) a b) | |
----------------------------------------------------------------------------------------- | |
menorProductoEscalar v1 v2 = minimum [ x <·> v2 | x <- permutations v1] | |
----------------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://en.wikipedia.org/wiki/Rearrangement_inequality | |
import Data.List | |
a <·> b = sum (zipWith (*) a b) | |
menorProductoEscalar v1 v2 = | |
let v1crec = sortBy compare v1 -- sort | |
v2decr = sortBy (flip compare) v2 | |
in v1crec <·> v2decr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment