Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

========== motivation ===========

Suppose, for example, I have an incoming expression like

 m = (3-I)x + 4(x y)/(Cos[y])

I want to find all the symbols in the expression ("x" and "y" in this case) without a priori knowledge of what symbols may appear in m.
Perhaps then I want to do something to the symbols, for example, transform each symbol #:

# -> u*#  

i.e. I want to obtain from m:

(3-I)u x + 4(u^2 x y)/(Cos[u y])  

In this case, I can take this result, expand in u and unitize to get:

Series[(3-I)u x + 4(u^2 x y)/(Cos[u y]), {u,0,1}]/.u->1  

a series expansion to first order in both x and y, where terms x^2, y^2, and x y are dropped.

==============================================================

This is just one example though, in general, I want to know how to find the symbols in an expression and do something to them.

I've tried things similar to

 test = Expand[# /. Not@NumericQ :> ReplaceAll[#, z -> u z]] &;  

 (5-3 I)x + 2 y^2 //test  

But this just returns the input unchanged, I would like it to return

 (5-3 I)u x + 2 u^2 y^2  

Any help?

share|improve this question
    
I feel that this question should be closed as a duplicate of (30038) but there is just enough wiggle room that I don't wish to act alone. If anyone agrees or disagrees please comment. –  Mr.Wizard Jul 2 at 6:55

4 Answers 4

m = (3 - I) x + 4 (x y)/(Cos[y]);
Variables@Level[m, {-1}]

(*{x, y}*)
share|improve this answer
    
nice use of Variables –  Nasser Jul 1 at 19:43
    
@Nasser it should look rather familiar. –  Mr.Wizard Jul 2 at 6:49

"...in general, I want to know how to find the symbols in an expression..."

m = (3 - I) x + 4 (x y)/(Cos[y]);

sym = DeleteDuplicates@Cases[m, _Symbol, Infinity]

{x, y}

"... and do something to them"

m /. MapThread[Rule, {sym, {u, v}}]

(3 - I) u + 4 u v Sec[v]

share|improve this answer
    
Great! Even m /. Function[{sym},MapThread[Rule, {sym,Symbol["x" <> ToString[#]] & /@ Range[Length@sym]}]][sym] –  acl Jul 1 at 19:16
m = (3 - I) x + 4 (x y)/(Cos[y Pi]);

Since

Head /@ {Pi, E, GoldenRatio}  (* and others *)

{Symbol, Symbol, Symbol}

Cases must test for more than just Symbol

m /. Thread[(var = Cases[m, _Symbol?(! NumericQ[#] &), Infinity] // Union) -> 
   u*var]

(3 - I)*u*x + 4*u^2*x*y* Sec[Pi*u*y]

share|improve this answer
    
+1, I think it safe to assume this is what the OP intended for symbol capture. –  rasher Jul 2 at 1:44
1  
I agree, you can separate them also by context since built in symbols have System` while x is Global` –  Kuba Jul 2 at 5:58
    
@BobHanlon Using Variables (like Algohi did) eliminates NumericQ –  eldo Jul 2 at 9:03
m = (3 - I) x + 4 (x  y)/Cos[y];
lev = Level[m, {-1}]
Union@Cases[lev, _Symbol]

(* {x, y} *)
share|improve this answer
    
.. just saw eldo post after hitting post.... This is a little different may be. In Mathematica, we should always try to come up with 10 different ways to solve something :) –  Nasser Jul 1 at 19:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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