Help calling up a specfic answer

Hello folks. I have a question for you.
I am using Mathmatica 9 and some of the work I do yields multiple answers. For the sake of simplicity lets assume it yields one real and one complex or imaginary answer.

For example:
2x and 5x+3i

Now is there a way in the following lines I can reference each one of those parts of the answer individually?

Such as If answer 1 (which would be 2x) then do whatever
If answer 2 (which would be 5x+3i) then do something else.

I know this is a vague example, but I need it for a large number of applications so I generalized it.

Thanks in advance!

Quick suggestion.

Change the list of values into a list of tests.

In[1]:= tests = Thread[ ans == {2 x , 5 x + 3 I}]
Out[1]= {ans == 2 x, ans == 3 I + 5 x}

Make a list of the functions to evaluate.

In[2]:= funs = {f1[arg], f2[arg]};

Pair them.

In[3]:= af = Transpose[{tests, funs}]
Out[3]= {{ans == 2 x, f1[arg]}, {ans == 3 I + 5 x, f2[arg]}}

Add a “none of the above” case.

In[4]:= af = Join[af, {{True, 0}}]
Out[4]= {{ans == 2 x, f1[arg]}, {ans == 3 I + 5 x, f2[arg]}, {True, 0}}

Create the Which for choosing which function to run.

In[5]:= ?Which 
Which[test1 , value1 , test2 , value2 , ... ]
    evaluates each of the testi  in turn, returning the value of the valuei
      corresponding to the first one that yields True.  

In[6]:= Apply[Which, Flatten[af]]
Out[6]= Which[ans == 2 x, f1[arg], ans == 3 I + 5 x, f2[arg], True, 0]

The functions should be defined with

f1[q_?NumericQ]:=

so they do not evaluate until passed a numerical argument.

Find below a simple program I wrote to teach my son arithmetics. It has the functionality you ask about. After evaluation it is called by the evaluation of a line with the variable play. Hope this helps.

right := 0;
wrong := 0;

play := Panel[DynamicModule[{a, b, c, d, q},
    Clear

;
    q = RandomInteger[{0, 1}];
            If[q == 0,
                 Clear[c, d, f];
                     c = RandomInteger[{1, 10}];
                                   d = RandomInteger[{1, c - 1}];
                             f = “?”;
     g1 := (Clear; f = c - d; right++(*;EmitSound[Sound[{SoundNote[
       “E”,0.5],SoundNote[“C”,0.5],SoundNote[“E”,0.5],SoundNote[“C”,
       0.5],SoundNote[“F”,0.5],SoundNote[“E”,0.5],SoundNote[“D”,1]}]]*));
     g2 := (Clear; f = “?”; wrong++(*;EmitSound[Sound[{SoundNote[
       “G”,0.2],SoundNote[“G”,0.2],SoundNote[“G”,0.2],SoundNote[“Eb”,
       7]}]]*));

(*cc=EmitSound[{Sound[SoundNote[“D”]]}];*)
     Column[{
               Row[{

Dynamic@Button[Style[1, 30], If[c - d == 1, g1, g2]],
                     Dynamic@Button[Style[2, 30], If[c - d == 2, g1, g2]],
                         Dynamic@Button[Style[3, 30], If[c - d == 3, g1, g2]],
                             Dynamic@Button[Style[4, 30], If[c - d == 4, g1, g2]],
                                 Dynamic@Button[Style[5, 30], If[c - d == 5, g1, g2]],
                                     Dynamic@Button[Style[6, 30], If[c - d == 6, g1, g2]],
                                         Dynamic@Button[Style[7, 30], If[c - d == 7, g1, g2]],
                                             Dynamic@Button[Style[8, 30], If[c - d == 8, g1, g2]],

Dynamic@Button[Style[9, 30], If[c - d == 9, g1, g2]],
         Dynamic@Button[Style[10, 30], If[c - d == 9, g1, g2]]

}],

Panel[Row[{Text[Style[c, 50]],

Text[Style[“-”, 50]],

Text[Style[d, 50]],
                                                                      \
      Text[Style[“=”, 50]],
                                                                      \
               Text[Style[Dynamic, 50]]}]],

Panel[
        Row[{Text[Style["??a??? ???: ", 20]],
          Text[Style[Dynamic

, 20, Bold, RGBColor\[0, 0.8, 0]]]}], ImageSize -> {250, 40}], Panel\[Row\[{Text\[Style\["????a??????? ???????: ", 20]], Text\[Style\[Dynamic, 20, Bold, RGBColor\[0.8, 0, 0]]]}], ImageSize -> {250, 40}], Panel\[ Row\[{Text\[ Style\["??????: ", 20]], If\[wrong <= 3, Text\[Style\["1", RGBColor\[0, 0.8, 0], Bold, 20]], If\[ right >= wrong, Text\[Style\["2", Blue, Bold, 20]], If\[ right \< wrong && right > 0, Text\[Style\["3", Purple, Bold, 20]], If\[wrong != 0 && right == 0, Text\[Style\["4", Red, Bold, 20]]]] ]] }], ImageSize -> {250, 40}] }, Alignment -> Center], Clear\[a, b, f]; a = RandomInteger\[{1, 9}]; b = RandomInteger\[{1, 9 - a}]; f = "?"; g1 := (Clear; f = a + b; right++(\*;EmitSound\[Sound\[{SoundNote\[ "E",0.5],SoundNote\["C",0.5],SoundNote\["E",0.5],SoundNote\["C", 0.5],SoundNote\["F",0.5],SoundNote\["E",0.5],SoundNote\["D",1]}]]\*)); g2 := (Clear; f = "?"; wrong++(\*;EmitSound\[Sound\[{SoundNote\[ "G",0.2],SoundNote\["G",0.2],SoundNote\["G",0.2],SoundNote\["Eb", 7]}]]\*)); (\*cc=EmitSound\[{Sound\[SoundNote\["C"]]}];\*) Column\[{ Row\[{ Dynamic@Button\[Style\[1, 30], If\[a + b == 1, g1, g2]], Dynamic@Button\[Style\[2, 30], If\[a + b == 2, g1, g2]], Dynamic@Button\[Style\[3, 30], If\[a + b == 3, g1, g2]], Dynamic@Button\[Style\[4, 30], If\[a + b == 4, g1, g2]], Dynamic@Button\[Style\[5, 30], If\[a + b == 5, g1, g2]], Dynamic@Button\[Style\[6, 30], If\[a + b == 6, g1, g2]], Dynamic@Button\[Style\[7, 30], If\[a + b == 7, g1, g2]], Dynamic@Button\[Style\[8, 30], If\[a + b == 8, g1, g2]], Dynamic@Button\[Style\[9, 30], If\[a + b == 9, g1, g2]], Dynamic@Button\[Style\[10, 30], If\[a + b == 10, g1, g2]] }], Panel\[Row\[{Text\[Style\[a, 50]], Text\[Style\["+", 50]], Text\[Style\[b, 50]], \ Text\[Style\["=", 50]], \ Text\[Style\[Dynamic, 50]]}]], Panel\[ Row\[{Text\[Style\["??a??????? ???????: ", 20]], Text\[Style\[Dynamic
, 20, Bold, RGBColor\[0, 0.8, 0]]]}], ImageSize -> {250, 40}], Panel\[ Row\[{Text\[Style\["????a??????? ???????: ", 20]], Text\[Style\[Dynamic, 20, Bold, RGBColor\[0.8, 0, 0]]]}], ImageSize -> {250, 40}], Panel\[ Row\[{Text\[ Style\["??????: ", 20]], If\[wrong <= 3, Text\[Style\["1", RGBColor\[0, 0.8, 0], Bold, 20]], If\[ right >= wrong, Text\[Style\["2", Blue, Bold, 20]], If\[ right \< wrong && right > 0, Text\[Style\["3", Purple, Bold, 20]], If\[wrong != 0 && right == 0, Text\[Style\["4", Red, Bold, 20]]]] ]] }], ImageSize -> {250, 40}] }, Alignment -> Center] ] ]];

Help calling up a specfic answer

Here is an example of the Switch function control structure, that looks at the pattern of the input then desides what to do based on the first match it finds

In[ ]:= result = 5 x + 3 I;
Switch[result,
 2 x, "Do 2x",
 5 x + 3 I, "Do 5x+3I",
 _, "Do something else"]

Out[ ]= "Do 5x+3I"

The example patterns are very specific. To help see how to build more general patterns, look at the FullForm of what you are trying to match.

In[ ]:= FullForm[5x+3I]
Out[ ]= Plus[Complex[0,3],Times[5,x]]

The example Switch patterns can be generalized this way

In[ ]:= result = 6 x + 10 I;
Switch[result,
 Times[_, x], "Do Real",
 Plus[Complex[0, _], Times[_, x]], "Do Complex",
 _, "Do something else"]

Out[ ]= "Do Complex"

Notice the Complex comes first in the pattern, but is in the second position in the result. ‘6 x + 10 I’ gets reordered into a standard form when Set to result. The patten under Plus will still match that form with Complex and Times positions reversed.

In[ ]:= MatchQ[3 I + 5 x, Plus[Times[_, x], Complex[0, _]]]
Out[ ]= True