MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: I am having some problem manipulating mathematica

  • To: mathgroup at
  • Subject: [mg91650] Re: I am having some problem manipulating mathematica
  • From: Bob Hanlon <hanlonr at>
  • Date: Sun, 31 Aug 2008 04:30:48 -0400 (EDT)
  • Reply-to: hanlonr at

expr = {a*Sin[y*x]*x^2 - x^3 + Cos[Tan[x + y] + b*(y/Exp[c*x])], 
   c*x^2 + Log[d*(x*y)]};

Separate the expressions into terms

expr2 = List @@@ expr

{{-x^3, Cos[(b*y)/E^(c*x) + 
         Tan[x + y]], a*x^2*Sin[x*y]}, 
   {c*x^2, Log[d*x*y]}}

f@@@expr  is shorthand for  Apply[f,expr,{1}]

Remove jagged edges of expr2

expr3 = PadRight[expr2]

{{-x^3, Cos[(b*y)/E^(c*x) + 
         Tan[x + y]], a*x^2*Sin[x*y]}, 
   {c*x^2, Log[d*x*y], 0}}

coef = Array[p, Dimensions[expr3]]

{{p[1, 1], p[1, 2], p[1, 3]}, 
   {p[2, 1], p[2, 2], p[2, 3]}}

Sum the rows

expr4 = Total /@ (coef*expr3)

{(-p[1, 1])*x^3 + a*p[1, 3]*
       Sin[x*y]*x^2 + 
     Cos[(b*y)/E^(c*x) + Tan[x + y]]*
       p[1, 2], c*p[2, 1]*x^2 + 
     Log[d*x*y]*p[2, 2]}

Put all of this into a Module

myFunction[x_List, p_Symbol] := Module[
  {expr = List @@@ x},
  expr = PadRight[expr];
  Total /@ (Array[p, Dimensions[expr]]*expr)]

Verify that myFunction produces the same results

expr4 == myFunction[expr, p]


I recommend that you use coefficients of the form p[i, j]; however, if this is unacceptable, you can use a replacement such as

expr5 = expr4 /. p[i_, j_] :> 
   ToExpression["p$" <> ToString[i] <> 
     "$" <> ToString[j]]

{(-p$1$1)*x^3 + a*p$1$3*Sin[x*y]*
       x^2 + p$1$2*Cos[(b*y)/E^(c*x) + 
           Tan[x + y]], c*p$2$1*x^2 + 

Bob Hanlon

---- pratip <pratip.official at> wrote: 

Hallo everybody,

I am having a small problem while dealing with the parts of a Mathematica expression. Here I give an example.
take the LHS of a  system of equations
Hence the expression is
Now take the first expression
There we can see the three first level terms
separated with {+,-,+}
Now how to Count how many such parts exists in general?
Because I want to get every single term multiplied with different symbols. Here we have three terms in the first expression so we must get
Hence I am trying to write a function that is defined like this

and works like this
I don't think it is too difficult for people of your skills. But I don't know a very nice way to do it when the starting equation system can have any number of equations and LHS of every equation can have any number of first level terms combined with "+" or "-". It might also occur that some equations will have just a single term LHS e.g "x^2==0".

So that was the primary problem. It will be even better (actually that is the real thing I am looking for) if it is possible to extend this operation on every higher part level recursively. That means to multiply part of a part with symbols like before. For example if we have the following equation
Cos[Tan[x+y]+b*(y/Exp[c*x])]==0 we have



Down is the explanation of this above notation

*****What is i*****
The meaning of the symbol p$i$j$k is as follows
"i" denotes the number of main expression p occurs. Here we had only one expression so all i is equal to one. If we had two expressions some p will be like "p$2$j$k".

*****What is j*****
"j" denotes the level in the expression where p is getting multiplied. In the above example there are three parameter that occurs in the second level namely {p$1$2$1,p$1$2$2,p$1$2$3}.

*****What is k*****
"k" is meant for counting the p that occurs in a fixed level in a fixed expression.

I finally re grate that my message may seem too long for many of you though the question in it is not that much challenging at least mathematically. But I hope some of you might have a nice answer. My skill in Mathematica seems to be not enough to deal with this problem.I am feeling kind of hopeless.


  • Prev by Date: Re: light-weight PC to run Mathematica?
  • Previous by thread: Debugger