4
$\begingroup$

I would like

x= 2 \[PlusMinus] Sqrt[3] 

to evaluate to

{x=2+Sqrt[3],x=2-Sqrt[3]} 

Including any number of plus or minus symbols, so

x=(2 \[PlusMinus] Sqrt[3]) \[PlusMinus] 3 

would evaluation to

{x=5+Sqrt[3],x=5-Sqrt[3],x=-1+Sqrt[3],x=-1-Sqrt[3]} 

Any ideas?

Thanks

$\endgroup$
0

2 Answers 2

3
$\begingroup$

Does this work in every case you can think of?

Flatten[{2 \[PlusMinus] Sqrt[3],a \[PlusMinus] 4, a \[PlusMinus] b \[PlusMinus] q}//.h__ \[PlusMinus] t__->{h+t,h-t}] 

which returns

{2+Sqrt[3],2-Sqrt[3],4+a,-4+a,a+b+q,a-b+q,a+b-q,a-b-q}] 

The h__ matches everything before \[PlusMinus], the t__ matches everything after, and the //. repeatedly turns any item that matches into two items until it is done and finally the Flatten discards any introduced { and }.

Preserving the x= in the output is a different issue and is trying to avoid the usual evaluation process. I don't see a simple way of doing that in every case. Perhaps someone else can show a clever way of doing that.

$\endgroup$
2
$\begingroup$
ClearAll[expandPlusMinus] expandPlusMinus = Flatten @* ReplaceAll[PlusMinus -> ({+##, # - #2} &)]; 

Examples:

2 ± Sqrt[3] // expandPlusMinus 
{2 + Sqrt[3], 2 - Sqrt[3]} 
(2 ± Sqrt[3]) ± 3 // expandPlusMinus 
{5 + Sqrt[3], 5 - Sqrt[3], -1 + Sqrt[3], -1 - Sqrt[3]} 
2 ± Sqrt[3] // expandPlusMinus // Map[PromptForm[x, #] &] 

enter image description here

(2 ± Sqrt[3]) ± 3 // expandPlusMinus // Map[PromptForm[x, #] &] 

enter image description here

Alternatively,

ClearAll[expandPlusMinus2] expandPlusMinus2 = Block[{PlusMinus = Flatten@Through[{Plus, Subtract}@##] &}, #] &; 

Examples:

2 ± Sqrt[3] // expandPlusMinus2 
{2 + Sqrt[3], 2 - Sqrt[3]} 
(2 ± Sqrt[3]) ± 3 // expandPlusMinus2 
{5 + Sqrt[3], 5 - Sqrt[3], -1 + Sqrt[3], -1 - Sqrt[3]} 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.