MathGroup Archive 2001

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

Search the Archive

Re: Extract coefs. and exponents from a list . Package problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg30725] Re: Extract coefs. and exponents from a list . Package problem
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 8 Sep 2001 02:56:07 -0400 (EDT)
  • References: <9nal4a$nos$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Guillemo,
Two ways of making ExtractCoef work and a different code.

The problem with the given ExtractCoef is that on loading the package code,
t is created as
                            test`test`Private`t
whereas the inpput has Global`t.
Two ways round this

1) in the package write Global`t in place of t

BeginPackage["test`test`"];
ExtractCoef::usage="ExtractCoef[test]";
Begin["`Private`"];

ExtractCoef[list_]:=
Block[{f,g,a,b},f[a__]:={a};
g[b__]:={b};{(#1/.a_*Exp[b_*Global`t]\[Rule]a&)/@
f@@list,(#1/.a_*Exp[b_*Global`t]\[Rule]b&)/@g@@list}]

End[];
Protect[ExtractCoef];
EndPackage[];

list1= 2 Exp[-0.3 t]+0.2 Exp[-0.1 t]+0.4 Exp[-0.4 t];

ExtractCoef[list1]

        {{0.4,2,0.2},{-0.4,-0.3,-0.1}}

2) Input t from outside

In[1]:=
BeginPackage["test`test`"];
ExtractCoef::usage="ExtractCoef[test]";
Begin["`Private`"];

ExtractCoef[list_,t_]:=
Block[{f,g,a,b},f[a__]:={a};
g[b__]:={b};{(#1/.a_*Exp[b_*t]\[Rule]a&)/@
f@@list,(#1/.a_*Exp[b_*t]\[Rule]b&)/@g@@list}]

End[];
Protect[ExtractCoef];
EndPackage[];

ExtractCoef[list1,t]

        {{0.4,2,0.2},{-0.4,-0.3,-0.1}}

3) Different code - also allows summands with a coefficient and ones with no
Exp[.] part

ExtractCoef2[lst_]:=
Transpose[
Replace[({lst/.x_Plus:>List@@x}//Flatten),
{Exp[p_] c_. -> {p,c},c_->{0,c}},{1}
 ]/.t->1
]

list2= 2 + Exp[-0.3 t]+0.2 Exp[-0.1 t]+0.4 Exp[-0.4 t];


ExtractCoef2[list2]

        {{0,-0.4,-0.3,-0.1},{2,0.4,1,0.2}}
--
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

"Guillermo Sanchez" <guillerm at aida.usal.es> wrote in message
news:9nal4a$nos$1 at smc.vnet.net...
> Dear group
> (*I wish extract the coeffs and the exponents from expresion as follow*)
>
> list1=0.1 Exp[-0.3 t]+0.2 Exp[-0.1 t]+0.4 Exp[-0.4 t];
>
> (*This function works*)
>
> ExtractCoef1[list_]:=
>   Block[{f,g,a,b},f[a__]:={a};
>     g[b__]:={b};{(#1/.a_*Exp[b_*t]\[Rule]a&)/@
>         f@@list,(#1/.a_*Exp[b_*t]\[Rule]b&)/@g@@list}]
>
> ExtractCoef1[list1]
>
> (*out[]:{{0.4`,0.1`,0.2`},{-0.4`,-0.3`,-0.1`}}*)(*it works*)
>
> (*But, if I use this function inside a package it doesn`t work*)
>
> BeginPackage["test`test`"];
>
> ExtractCoef::usage="ExtractCoef[test]";
>
> Begin["`Private`"];
>
> ExtractCoef[list_]:=
>   Block[{f,g,a,b},f[a__]:={a};
>     g[b__]:={b};{(#1/.a_*Exp[b_*t]\[Rule]a&)/@
>         f@@list,(#1/.a_*Exp[b_*t]\[Rule]b&)/@g@@list}]
> End[];
> Protect[ExtractCoef];
> EndPackage[];
>
> ExtractCoef[list1](*It doesn´t work*)
>
> (* question 1: I wist fix this problem,any help?,
> question 2: Could any body build a expresion easier?*)
>






  • Prev by Date: Re: = or := ???
  • Next by Date: Re: JLink an animated gif
  • Previous by thread: Re: Extract coefs. and exponents from a list . Package problem
  • Next by thread: Trial and Error using mathematica.