# how to convert a bundle of if statements into a simple one.

hi i am just trying to create a matrix depending on some specific conditions for which i designed these codes which work fine but i want to concise these codes.

EEV1 = Log[EV1]/(2*\[A]);
EEV2 = Log[EV2]/(2*\[A]);
EEV3 = Log[EV3]/(2*\[A]);
EEV4 = Log[EV4]/(2*\[A]);
If[Im[EEV1] > 0 && Im[EEV2] > 0 && Im[EEV3] < 0 && Im[EEV4] < 0,
Fpmat = Transpose[{EVEC1, EVEC2}] ];
If[Im[EEV1] < 0 && Im[EEV2] > 0 && Im[EEV3] > 0 && Im[EEV4] < 0,
Fpmat = Transpose[{EVEC2, EVEC3}] ];
If[Im[EEV1] < 0 && Im[EEV2] < 0 && Im[EEV3] > 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC3, EVEC4}] ];
If[Im[EEV1] > 0 && Im[EEV2] < 0 && Im[EEV3] > 0 && Im[EEV4] < 0,
Fpmat = Transpose[{EVEC1, EVEC3}] ];
If[Im[EEV1] > 0 && Im[EEV2] < 0 && Im[EEV3] < 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC1, EVEC4}] ];
If[Im[EEV1] < 0 && Im[EEV2] > 0 && Im[EEV3] < 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC2, EVEC4}] ];

If[Im[EEV1] > 0 && Im[EEV2] > 0 && Im[EEV3] > 0 && Im[EEV4] < 0,
Fpmat = Transpose[{EVEC1, EVEC2}] ];
If[Im[EEV1] > 0 && Im[EEV2] > 0 && Im[EEV3] < 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC1, EVEC2}] ];
If[Im[EEV1] < 0 && Im[EEV2] > 0 && Im[EEV3] > 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC2, EVEC3}] ];
If[Im[EEV1] > 0 && Im[EEV2] < 0 && Im[EEV3] > 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC1, EVEC3}] ];
If[Im[EEV1] > 0 && Im[EEV2] > 0 && Im[EEV3] > 0 && Im[EEV4] > 0,
Fpmat = Transpose[{EVEC1, EVEC2}] ];

If[Im[EEV1] < 0 && Im[EEV2] < 0 && Im[EEV3] < 0 && Im[EEV4] < 0,
Print["choose another value of variable"] Break[]];
If[Im[EEV1] > 0 && Im[EEV2] < 0 && Im[EEV3] < 0 && Im[EEV4] < 0,
Print["choose another value of variable"] Break[]];
If[Im[EEV1] < 0 && Im[EEV2] > 0 && Im[EEV3] < 0 && Im[EEV4] < 0,
Print["choose another value of variable"] Break[]];
If[Im[EEV1] < 0 && Im[EEV2] < 0 && Im[EEV3] > 0 && Im[EEV4] < 0,
Print["choose another value of variable"] Break[]];
If[Im[EEV1] < 0 && Im[EEV2] < 0 && Im[EEV3] < 0 && Im[EEV4] > 0,
Print["choose another value of variable"] Break[]];


let me see who can help me. Thanks in advance.

• Use Which? – MarcoB Apr 6 '16 at 16:19
• i just want to reduce the size, by using which its size remains same. – M.M Umber Apr 6 '16 at 16:20
• The logic of your problem sets the size of your conditions, not the syntax. You could see if logically you can exclude some cases. For instance, would there be a single condition that would make any combinations of value containing that condition unacceptable? That could be tested separately as a catch-all condition. Is there a simple combination of choices describing an operation (e.g. an even/odd number of positive imaginary parts, etc)? Can you describe the logic of your choices? – MarcoB Apr 6 '16 at 16:31
• EEV1,EEV2,EEV3 and EEV4 represent the eigenvector of 4 different matrices. if the imaginary part of these matrices eigenvector are positive than there eigenvalues crossponding to positive imaginary valued matrix will be used as a new matrix. – M.M Umber Apr 6 '16 at 16:39

You could do :

 Fpmat =
Switch[Sign[Im[{EEV1,EEV2,EEV3,EEV4}]],
{1, 1, _, _}, Transpose[{EVEC1, EVEC2}],
{1, -1, 1, _}, Transpose[{EVEC1, EVEC3}],
{1, -1, -1, 1}, Transpose[{EVEC1, EVEC4}],
{-1, 1, 1, _}, Transpose[{EVEC2, EVEC3}],
{-1, 1, -1, 1}, Transpose[{EVEC2, EVEC4}],
{-1, -1, 1, 1}, Transpose[{EVEC3, EVEC4}],
{_, _, _, _}, Print["choose another value of variable"]; Fpmat
]


The first case that matches is applied, then Mathematica exit the Switch

The case of equality to 0 may not be what you want.

The number of different actions that can result from the various conditions is much smaller than the number of conditions. What you have to do is collect all the conditions leading to identical actions, and then combine these conditions with Or (written usually as ||). That's all there is to it. I'll leave the typing to you.

f[{ 1,  1,  _,  _}] := Transpose[{EVEC1, EVEC2}]
f[{-1,  1,  1,  _}] := Transpose[{EVEC2, EVEC3}]
f[{ 1, -1,  1,  _}] := Transpose[{EVEC1, EVEC3}]
f[{-1, -1,  1,  1}] := Transpose[{EVEC3, EVEC4}];
f[{ 1, -1, -1,  1}] := Transpose[{EVEC1, EVEC4}];
f[{-1,  1, -1,  1}] := Transpose[{EVEC2, EVEC4}];
f[_] := Print["choose another value of variable"]


Then use:

f[Sign[Im[{EEV1, EEV2, EEV3, EEV4}]]]