9
$\begingroup$

fixed in 10.1 (windows)


I have run into some strange behavior while doing some pattern matching. First, this works as expected:

Exp[2 I u x] /. Exp[Complex[0, a_] u x] :> a
(* 2 *) 

However, when I replace x with Sin[x] I get:

Exp[2 I u Sin[x]] /. Exp[Complex[0, a_] u Sin[x]] :> a
(* E^(2 I u Sin[x]) *) 

Also, dropping the u returns normal behavior:

Exp[2 I Sin[x]] /. Exp[Complex[0, a_] Sin[x]] :> a
(* 2 *)

I don't have these problems when I'm not using Complex. This looks like a bug to me, but perhaps I am not understanding how Complex works. I do notice that Complex treats numbers and symbols differently:

AtomQ[Complex[0, 2]]
(* True *)

AtomQ[Complex[0, b]]
(* False *)

Any ideas?

BTW - I'm using 10.0.2 on Mac OS X 10.10.2

$\endgroup$
  • 1
    $\begingroup$ Since Daniel Lichtblau has added the 'bugs` tag to this question, I think we can take it that Wolfram Research will treat this as bug and we will see a fix for it a future release. $\endgroup$ – m_goldberg Mar 4 '15 at 1:36
  • 1
    $\begingroup$ I thought I had put in a comment but I guess I messed it up. Yes, definitely a bug. Here is a simple example that gives False when it should be True: MatchQ[ff[I*u*f[x]], ff[Complex[0,_]*u*f[x]]]. Change f[x] to b[x] in both places and it works correctly. $\endgroup$ – Daniel Lichtblau Mar 4 '15 at 14:57
4
$\begingroup$

I don't know why

Exp[2 I u Sin[x]] /. Exp[Complex[0, a_] u Sin[x]] :> a

doesn't work as expected, except that

MatchQ[Exp[2 I u Sin[x]], Exp[Complex[0, a_] u Sin[x]]]

gives

False

However, since the apparently more general

MatchQ[Exp[2 I u Sin[x]], Exp[Complex[0, a_] u_ v_]]

gives

True

you could use

Exp[2 I u Sin[x]] /. Exp[Complex[0, a_] u_ v_] :> a

2

Update

Using Trace to investigate further shows that using a single character function name in place of Sin[x] works

 Trace[MatchQ[Exp[2 I u b[x]], Exp[Complex[0, a_] u b[x]]]]
{
  {
    {{I, I}, 2 I u b[x], 2 I u b[x]}, Exp[2 I u b[x]], E^(2 I u b[x])}, 
    {{Complex[0, a_] u b[x], u b[x] Complex[0, a_]}, Exp[u b[x] Complex[0, a_]], 
        E^(u b[x] Complex[0, a_])},
    MatchQ[E^(2 I u b[x]), E^(u b[x] Complex[0, a_])], True
}

but apparently replacing Sin[x] with any multiple character function name will fail

Trace[MatchQ[Exp[2 I u ff[x]], Exp[Complex[0, a_] u ff[x]]]]
{
  {
    {{I, I}, 2 I u ff[x], 2 I u ff[x]}, Exp[2 I u ff[x]], E^(2 I u ff[x])}, 
    {{Complex[0, a_] u ff[x], u Complex[0, a_] ff[x]}, Exp[u Complex[0, a_] ff[x]],
        E^(u Complex[0, a_] ff[x])},
    MatchQ[E^(2 I u ff[x]), E^(u Complex[0,a_] ff[x])], False
}

These trace results seem inconsistent to me. I think you have found a bug in Mathematica's pattern matching.

$\endgroup$
  • $\begingroup$ Thanks for help. I found a couple other workarounds, including Exp[2 I u Sin[x]] /. Exp[a_Complex u Sin[x]] :> Im[a] $\endgroup$ – shopper Mar 3 '15 at 18:54
  • 1
    $\begingroup$ This seems like a bug to me too. $\endgroup$ – Mr.Wizard Mar 3 '15 at 23:26
3
$\begingroup$

This seems like a bug. Additional examples that would need to be explained if it is not:

foo[2 I u Sin[x]] /.
 foo[Complex[0, _] u Sin[x]] :> bar

foo[2 I u Sin[x]] /.
 foo[Complex[0, _] (p : u) Sin[x]] :> bar

foo[2 I u Sin[x]] /.
 foo[Complex[0, _] HoldPattern[u] Sin[x]] :> bar
foo[2 I u Sin[x]]

bar

bar

So the problem is unrelated to Exp. Since u does not evaluate to something else and it is not the head of an expression I think adding HoldPattern or p : should not affect the matching and yet they do.

$\endgroup$
  • $\begingroup$ I also thought it was probably a bug. Replacing the implicit Times by a user defined symbol with attribute Orderless also leads to a failed match. $\endgroup$ – Jacob Akkerboom Mar 4 '15 at 11:46
  • $\begingroup$ I was hoping for an answer by you or Simon Woods, as I was never quite comfortable with how Orderless works for pattern matching. $\endgroup$ – Jacob Akkerboom Mar 4 '15 at 11:47
1
$\begingroup$

This has been fixed in 10.1

Mathematica graphics


Mathematica graphics

code

Exp[2 I u x] /. Exp[Complex[0, a_] u x] :> a
Exp[2 I u Sin[x]] /. Exp[Complex[0, a_] u Sin[x]] :> a
foo[2 I u Sin[x]] /. foo[Complex[0, _] u Sin[x]] :> bar
foo[2 I u Sin[x]] /. foo[Complex[0, _] (p : u) Sin[x]] :> bar
foo[2 I u Sin[x]] /. foo[Complex[0, _] HoldPattern[u] Sin[x]] :> bar
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.