tractOptimalPowerMatricesPerState ::
  (Ord b, Ord node,
  Vec.Storage vec (vec (Maybe (Result a))),
  Vec.Storage vec (Maybe (Result a)),
  Vec.FromList vec) =>
  Map Idx.State (Map [b] (Maybe (a1, EnvResult node a))) ->
  [TopoIdx.Position node] ->
  Map (TopoIdx.Position node)
      (Map Idx.State (Sig.PSignal2 Vector vec (Maybe (Result a))))
extractOptimalPowerMatricesPerState m ppos =
  Map.map (Map.map ModUt.to2DMatrix)
  $ Map.fromList $ map (\p -> (p, Map.mapWithKey (f p) m)) ppos
  where f p st matrixMap = Map.map g matrixMap
          where pos = StateIdx.powerFromPosition st p
                g = join . fmap (StateQty.lookup pos . snd)


extractOptimalPowerMatricesPerState ::
  (Ord b, Ord node,
  Vec.Storage vec (vec (Maybe (Result a))),
  Vec.Storage vec (Maybe (Result a)),
  Vec.FromList vec) =>
  Map Idx.State (Map [b] (Maybe (a1, EnvResult node a))) ->
  [TopoIdx.Position node] ->
  Map (TopoIdx.Position node)
      (Map Idx.State (Sig.PSignal2 Vector vec (Maybe (Result a))))
extractOptimalPowerMatricesPerState m ppos =
  Map.map (Map.map ModUt.to2DMatrix)
  $ Map.fromList $ map (\p -> (p, Map.mapWithKey (f p) m)) ppos
  where f p st matrixMap = Map.map g matrixMap
          where pos = StateIdx.powerFromPosition st p
                g = join . fmap (StateQty.lookup pos . snd)
extractOptimalPowerMatricesPerState m ppos =
  Map.map (Map.map ModUt.to2DMatrix)
  $ Map.fromList $ map (\p -> (p, Map.mapWithKey (f p) m)) ppos
  where f p st matrixMap = Map.map g matrixMap
          where pos = StateIdx.powerFromPosition st p
                g = join . fmap (StateQty.lookup pos . snd)