2
$\begingroup$

is it possible to extract the symbolic variables from an array ?. For example:

s = {(1+0.1I)a*b*c,4*a*b*c,(0.5-2i)*j*k^3,0.88*b*d^2}

I would like to have another array that contains only the different symbolic terms, like this:

ss = {a*b*c,b*d^2,j*k^3}

After that, I would like to identify the position of each term

Thanks

$\endgroup$
5
$\begingroup$
Clear["Global`*"]

s = {(1 + 0.1 I) a*b*c, 4*a*b*c, (0.5 - 2 i)*j*k^3, 0.88*b*d^2};

ss = s /. {Complex[_, _] :> 1, Plus[_, __] :> 1,
    a_?NumericQ * z_ :> z} // Union

(* {a b c, b d^2, j k^3} *)

Position[s, #*_] & /@ ss

(* {{{1}, {2}}, {{4}}, {{3}}} *)
$\endgroup$
6
  • $\begingroup$ Thanks, it works perfectly. Could you explain a bit the code, I don't understand what to do after the "plus". $\endgroup$
    – Nico
    Mar 27 '20 at 19:06
  • $\begingroup$ a_?NumericQ * z_ :> z specifies that the product of a numeric factor (a) with an expression (z) is replaced by the just the expression, i.e., drop the numeric factor. It could also be written as _?NumericQ*z_ :> z, that is, the numeric factor does not need to be given a name since it does not appear on the RHS of the rule. Look in the documentation for ReplaceAll $\endgroup$
    – Bob Hanlon
    Mar 27 '20 at 19:18
  • $\begingroup$ Thanks so much, :D, And the last question if I wanted the second code to search exactly for the term, for example if I have {a, aa, ab} and I want to find the position of the term "a", which is {1}, do you know how this could be done?, I think this code does not discriminate the length of the expression $\endgroup$
    – Nico
    Mar 27 '20 at 19:20
  • $\begingroup$ Position[{a, aa, ab}, a] Read the documentation for Position $\endgroup$
    – Bob Hanlon
    Mar 27 '20 at 19:23
  • $\begingroup$ but If I put Position[{a, aa, ab}, a], print the three value. I use $\endgroup$
    – Nico
    Mar 27 '20 at 19:32

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.