Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/4005ca0acd6ba7732436 to your computer and use it in GitHub Desktop.
Save joseanpg/4005ca0acd6ba7732436 to your computer and use it in GitHub Desktop.
@Jose_A_Alonso Exercitium: Mínimo producto escalar
-- 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]
-----------------------------------------------------------------------------------------
-- 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]
-----------------------------------------------------------------------------------------
-- 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