5
$\begingroup$

I would like to see how many times (and where) the number "91" appear in a set of formulae.

I thought of using Position[expr , 91 , Infinity], but it seems not to be working in this simple example:

Position[{Log[Sqrt[1 + I Sqrt[-1 + (4 mm)/91]]]}, 91, ∞]

which gives

{}

Why?

$\endgroup$
  • 1
    $\begingroup$ At first glance this feels like it could be a bug. Report to WRI Support to see what they say. $\endgroup$ – Edmund Feb 21 at 12:22
  • 1
    $\begingroup$ @Edmund. If you look at the FullForm of this expression, the 91 is embedded inside Rational[4,91], and it seems like the pattern matcher is treating anything that looks like Rational[_,_] as atomic (which might or might not be intended behavior). Indeed: Cases[Rational[4, 91], 91, Infinity] evaluates to {}. The work-around of Alexei's below is a reasonable hack, although annoying that it must be done. $\endgroup$ – march Feb 21 at 18:11
  • $\begingroup$ Interestingly, Cases[Rational[x, 91], 91, Infinity] evaluates to {91}, indicating that the pattern matcher sees actual numbers in the form Rational[_, _] as atomic, but if the expression is symbolic, it does not. $\endgroup$ – march Feb 21 at 18:15
  • $\begingroup$ @march. Rational[x, 91] is not an atomic expression, so it is treated differently from Rational[4, 91] which is atomic. $\endgroup$ – m_goldberg Feb 21 at 22:10
4
$\begingroup$

Try this:

expr = Log[Sqrt[1 + I Sqrt[-1 + (4 mm)/91]]];
Position[expr, f_[x_, 91] | f_[91, x_]]

(*  {{1, 1, 2, 2, 1, 2, 1}}   *)

Have fun!

$\endgroup$
4
$\begingroup$
expr = {Log[Sqrt[1 + I Sqrt[-1 + (4 mm)/91]]]};

If you look at the FullForm of the expression, you see that 91 appears only inside Rational

expr // FullForm

enter image description here

Since Rational is an atomic element (Atomic Elements of Expressions), the 91 is not accessible by Position. However, you can see the position of the Rational containing 91 as shown by Alexei.

Position[expr, Rational[_, 91], ∞]

(* {{1, 1, 1, 2, 2, 1, 2, 1}} *)

There are no parts to an atomic expression.

Rational[4, 91][[-1]]

enter image description here

$\endgroup$
1
$\begingroup$

An alternative pattern to search through the atoms:

Position[Log[Sqrt[1 + I Sqrt[-1 + (4 mm)/91]]], _?AtomQ[___, 91, ___] | 91]

{{1, 1, 2, 2, 1, 2, 1}}

Also

Position[Log[Sqrt[1 + I Sqrt[-1 + (4 mm)/91]]], 
 TypeSystem`$AtomPattern[___, 91, ___] | 91]

{{1, 1, 2, 2, 1, 2, 1}}

$\endgroup$
0
$\begingroup$

Another approach would be to change the expression into a string and then locate the string "91" within that. For your example:

expr = {Log[Sqrt[1 + I Sqrt[-1 + (4 mm)/91]]]}; 
StringPosition[ToString[expr], "91"]
{{94, 95}}

Which shows that 91 occurs in the final two positions of the string.

$\endgroup$
  • $\begingroup$ While this works, it's rather counter-intuitive that 9 and 1 are the final two characters in the string for that expression. A naive expectation might be that ] and } would be the final two characters. $\endgroup$ – High Performance Mark Feb 21 at 17:26
  • $\begingroup$ Counterintuitive or not... StringLength[ToString[expr]] is 95. $\endgroup$ – bill s Feb 21 at 21:30

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.