magico 0.0.2 → 0.0.2.1
raw patch · 2 files changed
+17/−21 lines, 2 filesdep ~comfort-arraydep ~lapack
Dependency ranges changed: comfort-array, lapack
Files
- magico.cabal +4/−4
- src/Main.hs +13/−17
magico.cabal view
@@ -1,5 +1,5 @@ Name: magico-Version: 0.0.2+Version: 0.0.2.1 Synopsis: Compute solutions for Magico puzzle Description: Compute solutions for Magico puzzle:@@ -14,7 +14,7 @@ Cabal-Version: >=1.10 Source-Repository this- Tag: 0.0.2+ Tag: 0.0.2.1 Type: darcs Location: http://hub.darcs.net/thielema/magico @@ -25,8 +25,8 @@ Executable magico Main-is: Main.hs Build-Depends:- lapack >=0.2.2 && <0.3,- comfort-array >=0.3.1 && <0.4,+ lapack >=0.3 && <0.4,+ comfort-array >=0.4 && <0.5, transformers >=0.3 && <0.6, utility-ht >=0.0.11 && <0.1, base >=4.5 && <5
src/Main.hs view
@@ -11,14 +11,14 @@ import qualified Numeric.LAPACK.Matrix as Matrix import qualified Numeric.LAPACK.Vector as Vector import Numeric.LAPACK.Matrix ((===))-import Numeric.LAPACK.Vector (Vector)+import Numeric.LAPACK.Vector (Vector, (+++)) import Numeric.LAPACK.Format ((##)) import qualified Data.Array.Comfort.Boxed as BoxedArray import qualified Data.Array.Comfort.Storable as Array import qualified Data.Array.Comfort.Shape as Shape import Data.Array.Comfort.Storable ((!))-import Data.Array.Comfort.Shape ((:+:)((:+:)))+import Data.Array.Comfort.Shape ((:+:)) import Foreign.Storable (Storable) @@ -43,22 +43,21 @@ type Index2 = (Index, Index) squareShape :: SquareShape-squareShape = (Shape.Enumeration, Shape.Enumeration)+squareShape = Shape.static type SumsShape = (():+:()) :+: (Shape.Enumeration Index :+: Shape.Enumeration Index) sumsShape :: SumsShape-sumsShape = (():+:()) :+: (Shape.Enumeration :+: Shape.Enumeration)+sumsShape = Shape.static sums :: (Storable a) => a -> a -> (a,a,a) -> (a,a,a) -> SumsVector a sums diag0 diag1 horiz vert = let vec3 (x,y,z) = Vector.fromList Shape.Enumeration [x,y,z]- x<+>y = Vector.append x y- in (Vector.singleton diag0 <+> Vector.singleton diag1)- <+>- (vec3 horiz <+> vec3 vert)+ in (Vector.singleton diag0 +++ Vector.singleton diag1)+ ++++ (vec3 horiz +++ vec3 vert) indices :: Triple Index@@ -83,7 +82,7 @@ fullMatrix = Matrix.fromRowArray squareShape $ fmap- (\is -> Array.sample squareShape (\j -> if Fold.elem j is then 1 else 0))+ (Array.fromAssociations 0 squareShape . map (flip (,) 1) . Fold.toList) sumIndices isIntegerVector :: Solution Double -> Bool@@ -117,14 +116,14 @@ ranks = BoxedArray.map (rank . extendedMatrix) $ BoxedArray.indices squareShape nullspace ::- ExtMatrix Double -> Matrix.General Matrix.ZeroInt SquareShape Double+ ExtMatrix Double -> Matrix.General Matrix.ShapeInt SquareShape Double nullspace mat = let (_vr,sigma,vl) = Singular.decompose mat rnk = length $ takeWhile (>1e-7) $ Vector.toList sigma in Matrix.dropRows rnk $ Matrix.mapHeight (Shape.ZeroBased . Shape.size) $ Matrix.fromFull vl -masks :: Board (Matrix.General Matrix.ZeroInt SquareShape Double)+masks :: Board (Matrix.General Matrix.ShapeInt SquareShape Double) masks = BoxedArray.map (nullspace . extendedMatrix) $ BoxedArray.indices squareShape @@ -155,16 +154,13 @@ -} solve :: (Index2, Int) -> SumsVector Int -> [Solution Int] solve (givenI2, given) ss =- let vec =- Vector.append- (Array.map fromIntegral ss)- (Vector.singleton $ fromIntegral given)- mat = extendedMatrix givenI2+ let mat = extendedMatrix givenI2 in filter (all (>=0) . Vector.toList) $ map (Array.map round) $ integerSolutions (Fold.maximum $ Vector.toList ss) (null1 mat) $ Matrix.unliftColumn MatrixShape.ColumnMajor- (snd . Singular.leastSquaresMinimumNormRCond 1e-7 mat) vec+ (snd . Singular.leastSquaresMinimumNormRCond 1e-7 mat) $+ Array.map fromIntegral $ ss +++ Vector.singleton given printSolution :: Solution Int -> IO () printSolution sol = do