Message Boards Message Boards

0
|
3581 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Collect[ ] function for vectors {a x, c x} --> x {a, c} ?

Posted 8 years ago

Hi,

I want to rearrange a term so that I can bring out a symbol (that exists in each component of the vector) out of a vector.

In a scalar term (tensor order #0), I get it successfully by the "Collect" function:

input: Collect[a x + c x, x]
output: (a + c) x

so, I am happy with this.

But when I tried this function at a vector (tensor order #1), it does not work the way I want:

input: Collect[{a x, c x}, x]
output: {a x, c x}

But the output, I want to have, shoud be: x {a, c}

Please can you help me? Maybe I forgot some parameters in the "Collect" function or I need a completely different function for my problem. I do not know.

Thanks. Matthias

Wolfram Mathematica Student Edition Version 9.0.1.0 Windows 8.1, 64 bit

POSTED BY: Matt Po
4 Replies
Posted 8 years ago

Thank you. I tried to commend them out (...). Is there any way to solve the task in Version 9?

In[1]:= x {1, 3}

Out[1]= {x, 3 x}

In[2]:= expr = {a x + b x^2, c x + d x + 3 x^2}

Out[2]= {a x + b x^2, c x + d x + 3 x^2}

In[3]:= expr = Factor /@ expr

Out[3]= {x (a + b x), x (c + d + 3 x)}

In[4]:= divisors = If[MatchQ[#, Times[_]], Cases[#, Times[x] :> x], {1}] & /@ expr

Out[4]= {{x, a + b x}, {x, c + d + 3 x}}

In[5]:= common = Times @@ Intersection @@ divisors

Out[5]= x

In[6]:= expr /= common

Out[6]= {a + b x, c + d + 3 x}

In

In

In[9]:= ?Inactivate

During evaluation of In[9]:= Information::notfound: Symbol Inactivate not found. >>

In[10]:= Factor[expr]

Out[10]= {a + b x, c + d + 3 x}

In[11]:= expr/Factor[expr]

Out[11]= {1, 1}

POSTED BY: Matt Po
Posted 8 years ago

Thank you for your answer. I have testet it, and the result is not the expected one (output #8). Please can you help me, thanks.

In[1]:= x {1, 3}

Out[1]= {x, 3 x}

In[2]:= expr = {a x + b x^2, c x + d x + 3 x^2}

Out[2]= {a x + b x^2, c x + d x + 3 x^2}

In[3]:= expr = Factor /@ expr

Out[3]= {x (a + b x), x (c + d + 3 x)}

In[4]:= divisors = If[MatchQ[#, Times[_]], Cases[#, Times[x] :> x], {1}] & /@ expr

Out[4]= {{x, a + b x}, {x, c + d + 3 x}}

In[5]:= common = Times @@ Intersection @@ divisors

Out[5]= x

In[6]:= expr /= common

Out[6]= {a + b x, c + d + 3 x}

In[7]:= expr = Inactivate[common expr]

Out[7]= Inactivate[{x (a + b x), x (c + d + 3 x)}]

In[8]:= expr // Activate

Out[8]= Activate[Inactivate[{x (a + b x), x (c + d + 3 x)}]]

POSTED BY: Matt Po

The problem is that you have version 9 which does not have Activate and Inactive. If you leave out In[7] and In[8]. You will have prefactor and the vector in the variables common and expr, respectively.

POSTED BY: Sander Huisman

One of the problems is that x{1,3} automatically gets expanded back into {x,3x}. You can of course 'freeze' this behavior using Hold, or other means:

Something like this could work (no guarantees):

expr = {a x + b x^2, c x + d x + 3 x^2};
expr = Factor /@ expr;
divisors = 
  If[MatchQ[#, Times[___]], Cases[#, Times[x__] :> x], {1}] & /@ 
   expr;
common = Times @@ Intersection @@ divisors;
expr /= common;
expr = Inactivate[common expr]
expr // Activate

giving:

x * {a + b x, c + d + 3 x}
{x (a + b x), x (c + d + 3 x)}
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