Message Boards Message Boards

0
|
7393 Views
|
9 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Using ReplaceAll (/.) with complex variables?

Posted 8 years ago

I have hopefully a simple question. (I'm using home version of Mathematica 7)

If define a simple complex matrix

test = {{a + Ib, b + Ic}, {d + Ie, e + If}}

and do a simple replacement

test /. {e -> 1, f -> 2}

How come the complex parts using the symbols e and f are not replaced? The above yields

{{a + Ib, b + Ic}, {d + Ie, 1 + If}}

If I do something simple

test2 = a + I b

test2 /. {a -> 4, b -> 2}

Yields: 4 + 2 I

as expected, but if I call up the [[2,2]] element of the array above and try the ReplaceAll it doesn't work

test6 = test[[2, 2]]

Yields: e + If

but

test6 /. {e -> 3, f -> 5}    

Yields: 3 + If

So how come the complex part doesn't get replaced? How can I do this replacement?

POSTED BY: Paul Hillman
9 Replies
Posted 8 years ago

Without any disrespect, whats the fuzz? The OP's replacements does seem to work (provided there is a space left between I and the variable symbol):

In[1]:= test = {{a + I b, b + I c}, {d + I e, e + I f}}

Out[1]= {{a + I b, b + I c}, {d + I e, e + I f}}

In[2]:= test /. {e -> 1, f -> 2}

Out[2]= {{a + I b, b + I c}, {I + d, 1 + 2 I}}
POSTED BY: Hans Milton
Posted 8 years ago

Hint:

Ie

is a single symbol.

I*e

or

I e

is the product of two things.

2I

or

2*I

or

2 I

also happen to be the product of two things.

That should be enough.

POSTED BY: Bill Simpson

This is not the whole story. ReplaceAll (and related functions) use the FullForm for pattern matching, and to replace a part of the complex number one needs to use this form. It is true that 3 + 4 I is atomic but one may use the head of this expression to get into its parts

FullForm[3 + 4 I]

returns

Complex[3,4]

therefore

3+ 4 I /. Complex[x_,4]:> Complex[x,12]

returns

3 + 12 I

as expected

Sorry about the coloring, don't really know how that happened. a,b,c,d,e,f were suppose to be unknown symbols.

POSTED BY: Paul Hillman

You could've noticed it from the coloring already; unknown symbols are blue (or green inside e.g. Module) (a,b,c,d,e,f), known symbols are black (I)....

Note that you can't use replace(all) on complex numbers:

3 + 4 I /. 4 -> 5

as they are treated as an 'atom', so it will just return 3 + 4I.

POSTED BY: Sander Huisman

Thanks Bill, for some reason I thought that I was reserved, so Ie and If would be a product to two things.

POSTED BY: Paul Hillman
Posted 8 years ago

Your thinking that I is reserved is correct, but that isn't sufficient to override the principle that anything beginning with a letter followed by any number of letters and digits will be considered to be one symbol. This kind of problem shows up again and again and again and again...

POSTED BY: Bill Simpson

Because you wrote it correctly. Compare the inputs (and the coloring).

{{a + Ib, b + Ic}, {d + Ie, e + If}}
{{a + I b, b + I c}, {d + I e, e + I f}}
POSTED BY: Sander Huisman

That is not your fault, that is caused because you need a space between symbols:

If
Ie

versus

I f
I e
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract