Re: format of "Solve" output
- To: mathgroup at smc.vnet.net
- Subject: [mg59963] Re: [mg59923] format of "Solve" output
- From: Ed Peschko <esp5 at mdssdev05.comp.pge.com>
- Date: Fri, 26 Aug 2005 04:54:12 -0400 (EDT)
- References: <200508190831.EAA27442@smc.vnet.net> <200508251034.GAA10188@smc.vnet.net> <430DCC3F.2070302@wolfram.com>
- Sender: owner-wri-mathgroup at wolfram.com
On Thu, Aug 25, 2005 at 08:48:47AM -0500, Daniel Lichtblau wrote: > Ed Peschko wrote: > >hey all, > > > >I had a simple question that I can't find a simple answer for in the docs: > > > >When you solve an equation using 'Solve', it comes out in the format: > > > >{{ x -> a+b, y -> 2 a + b}} > > > >However, when you try to transform the equations for use in FindInstance, > >say by using: > > > > > > Fold[And, 1, Apply[Equal, %25[[1]], {1}]] > > > >you get: > > > > "x" == "a" + "b" && "y" + 2 "a" + "b" > > > >In other words, variables lose their variableness. So, when you > >plug the results into, say, FindInstance it complains that "a" is not > >a variable. > > > >Is there any reason for this? And how can you turn the character strings > >back into variables? > > > >Thanks, > > > >Ed > > I do not understand exactly what you are asking (it is not clear, for > example, where you obtained character strings). But maybe below is the > soet of thing you wish to do. Hmm. Well, I figured out what was going on. I was trying to solve a numeric substitution cipher (eg. apple = 44, grape == 22, orange = 342, etc. what does grape equal), and I split up strings to become symbols, Only I forgot to make each character an actual symbol.. For some reason Solve is fine with this, it will solve for variables named "a". But FindInstance is not, so it barfs on the mismatch between the first and second argument. OutputFormat was masking this by making the "" around the characters invisible. Anyways, here's the code. The bug was in not having a Map around the conversion from Characters to Symbols. Ed eq[str_, amt_] := Module[ {lst}, lst = Map[Symbol, Characters[str]]; Plus[Sequence@@lst] == amt ]; char[str_] := Module[ {chars}, chars = Characters[str]; chars = Union[chars,chars]; Map[Symbol, chars] ]; aa = Solve[ { eq["barrow", 71], eq["newton", 70], eq["whiston", 104], eq["saunderson", 129], eq["colson", 51], eq["waring", 92], eq["milner", 58], eq["woodhouse", 108], eq["turton", 80], eq["airy", 46], eq["babbage", 84], eq["king", 45], eq["larmor", 58], eq["dirac", 52], eq["lighthill", 130] } ]; FindInstance[ Fold[And, True, Apply[Equal, aa[[1]], {1}]], char["barrownewtonwhistonsaundersoncolsonwaringmilnerwoodhouseturtonairybabbagekinglarmordiraclighthill"], Integers,1000 ]
- References:
- Nested Lists, Definition and Evaluation.
- From: "Alexander" <beginning.physst@mail.ru>
- format of "Solve" output
- From: Ed Peschko <esp5@mdssdev05.comp.pge.com>
- Nested Lists, Definition and Evaluation.