MathGroup Archive 1999

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

Search the Archive

Re: Coefficients in expressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16801] Re: Coefficients in expressions
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 30 Mar 1999 02:35:10 -0500
  • References: <7da865$9gd@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Virgil Stokes <virgil.stokes at neuro.ki.se> wrote in message
news:7da865$9gd at smc.vnet.net...
> I have generated several expressions (equations) in the form of:
>
>    c1*x1 + c2*x1' + c3*x2 + c4*y1+...+a1*x1'+ a2*y1 + a5*x1 +... == f1*z1
+...
>
> where, the equations can be over 50 lines in length.
> I would like to have a procedure (say Coeffs[.]) that would
> allow me to specify as input the "names" in the equations. The
> output would be all the coefficients for that list of names.
> For example, for the above,
>
>   Coeffs[%,x1,x1',y1]
>
> would output,
>
>     x1: (c1+a5)
>  x1': (c2+a1)
>   y1: (c4+a2)
>
> -- V. Stokes


Virgil,
The following may be of  use (Mathematica in Education and Research Vol 7 No
2 Spring 1998, p41)

MatchedCoefficients[expr_, vars_] :=
  Module[{h}, Replace[Expand[Collect[expr, vars, h]],
     {e_Plus :> List @@ e, e_ :> {e}}] /.
    {(u_.)h[y_] -> {u, y}}]

expr = c1 x1 + c2 x1' + c3 x2 + c4 y1 +
    a1 x1' + a2 y1 + a5 x1;

MatchedCoefficients[expr, {x1, x1', x2, y1}]

{{x1, a5 + c1}, {x2, c3}, {y1, a2 + c4},
  {x1', a1 + c2}}

The exression is easily recovered:

Dot @@ Transpose[%]

(a5 + c1)x1 + c3 x2 + (a2 + c4)y1 +
  (a1 + c2)x1'

Allan


--
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565





  • Prev by Date: Re: Equation numbers
  • Next by Date: NonlinearFit Question
  • Previous by thread: Re: Coefficients in expressions
  • Next by thread: curve fiting