MathGroup Archive 1993

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

Search the Archive

Re:Use of AlgebraicRules

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Re:Use of AlgebraicRules
  • From: HAY at leicester.ac.uk
  • Date: Wed, 6 JAN 93 23:52:28 GMT

Ken Sale (Lawrence Livermore National Lab, sale1 at llnl.gov)   
Posted a query about using AlgebraicRules. Here is a package with an 
extension of AlgebraicRules that seems to be both more powerful and also
simpler to use - it gives the expected result on Ken's example ( see 
the second of the examples given below).

It is sometimes necessary to use Together to get appropriate symbols
together so that AlgebraicRulesExtended or AlgebraicRules can act as
required (see example 3).

Criticism, bug reports, comments and improvements will be welcome.

Allan Hayes
hay at leicester.ac.uk

********************************************************************
                        The Package
        This is set up for adding to the Algebra folder
*********************************************************************
(* :Title: Algebraic Rules Extended *)
(* :Author: Allan Hayes, hay at leicester.ac.uk *)
(* :Summary:
     An extension of the system function AlgebraicRules.
     It gives a rule AREx and can be used exactly like 
     AlgebraicRules (though the latter gives a list of rules). 
     
     But: 
     (1) it does not require the user to list all the symbols
     occurring in the expression to which AREx is applied.
     (2)It uses Map so as get at places that Replace alone 
     cannot reach. 
*)
(* :Context: Algebra`AlgebraicRulesExtended` *)
(* :Package Version: 1.1 *)
(* :Copyright: Copyright 1993 Allan Hayes *)
(* :History:Version 1.1 by Allan Hayes, January 1993 *)
(* :Keywords: Algebra, Simplification, Manipulation *)
(* :Mathematica Version: 2.1 *)


BeginPackage["Algebra`AlgebraicRulesExtended`"];

AlgebraicRulesExtended::usage = "
AlgebraicRulesExtended[eqns (,{x1,x2,...})]\n
generates a rule, AREx, for replacing earlier members 
of x1,x2,.. with later ones or other variables from eqns. 
As a first step in evaluation the given list is extended 
by appending the other symbols in eqns in default order.\n

AlgebraicRulesExtended[eqns] is equivalent to 
AlgebraicRulesExtended[eqns ,{}].\n
\n

Options:  
AppendVariables -> True.\n 

AppendVariables-> False turns off the extension of the list 
{x1,x2, ...}\n
\n

expr/.AREx applies these rules to all parts of expr that 
contain at least one of the symbols from {x1, x2, ...} extended 
to include all symbols in eqns unless AppendVariables is set 
to False.";
  
AppendVariables::usage = " AppendVariables is an option for

AlgebraicRulesExtended. The default is True; and at this setting
the given list of variables is extended by appending any other 
variables in eqns, in default order. With setting True no 
extension is made. This option may be used to restrict the 
search for replacements and so speed up the computation";



Begin["`Private`"];

Options[AlgebraicRulesExtended] = {AppendVariables -> True};

AlgebraicRulesExtended[eqns_, opts___Rule] := 
	AlgebraicRulesExtended[eqns, {},opts];
AlgebraicRulesExtended[eqns_, var_Symbol, opts___Rule] :=

	AlgebraicRulesExtended[eqns, {var}, opts];

AlgebraicRulesExtended[eqns_, vars_List, opts___Rule] :=
   Block[
      {x,ans, app},
		
      appendQ = 
          AppendVariables/.
             {opts}/.
                Options[AlgebraicRulesExtended];
			
      With[
         {prvars =
            If[appendQ === False, 
               vars,
               Join[
                  vars,
                  Complement[
                     Cases[eqns,s_Symbol, Infinity], 
                     vars
                  ]
               ]
            ]
         },
			
            With[
               {AM = 
                  Function[
                     rules,
                     (#/. rules)&
                  ][AlgebraicRules[eqns, prvars]]
               },                         
               x_ :> (*Map[ , Infinity, Heads->True] is much slower*)

                  With[  
                     {posn = 
                        Position[
                           x, 
                           p_/;
                              !FreeQ[
                                 p,
                                 Alternatives@@prvars

                              ]
                        ]
                     },
                     Off[AlgebraicRules::newv,General::ivar];
                     ans= MapAt[AM,x,posn];
                     On[AlgebraicRules::newv,General::ivar];
                     ans
                  ]
            ]
      ]	 
   ]	 
	
	
End[];          
Protect[AlgebraicRulesExtended, AppendVariables];
EndPackage[];  


(*
*******************************************************************
       Examples and Comparisons with AlgebraicRules
*******************************************************************

(1)
In[]
    eqns = { c1^2 + s1^2 == 1, c2^2 + s2^2 == 1, c3^2 + s3^2 == 1 };
    AREx1 = AlgebraicRulesExtended[eqns];
    Tan[s1^2 + a^c + c1^2]/(b(s2^4 +k + c2^4))/.AREx1
	
Out[]     
    Tan[1 + a^c]/(b + b*k - 2*b*s2^2 + 2*b*s2^4)
      
In[]      
			AR1 = AlgebraicRules[eqns];
			Tan[s1^2 + a^c + c1^2]/(b(s2^4 +k + c2^4))/.AR1

Out[]
			AlgebraicRules::newv: 
			        4         4
			   b (c2  + k + s2 ) involves variables not among 
			    {c1, s1, c2, s2, c3, s3}.
			AlgebraicRules::newv: 
			        c     2     2
			   Tan[a  + c1  + s1 ] involves variables not among 
			    {c1, s1, c2, s2, c3, s3}.
			                c
			General::ivar: a  is not a valid variable.
			
			Tan[a^c + c1^2 + s1^2]/(b + b*k - 2*b*s2^2 + 2*b*s2^4)


(2)

In[]
			a + b/d /. AlgebraicRulesExtended[d == g, {a,b,d,g}]
			
Out[]
   a + b/g
   
In[]
   a + b/d /. AlgebraicRules[d == g, {a,b,d,g}]
   
Out[]
                          b
			AlgebraicRules::newv: a + - involves variables not among {a, b, d, g}.
			                          d
			               1
			General::ivar: - is not a valid variable.
			               d
			a + b/d
			
(3)

In[] 
         AR2 = AlgebraicRules[{a+b == s, a b == p},{a,b,s,p}];
         1/a + 1/b/.AR2
Out[]                 
                      1   1
AlgebraicRules::newv: - + - involves variables not among {a, b, s, p}.
                      a   b
               1
General::ivar: - is not a valid variable.
               b
1/a + 1/b

BUT
In[]
         AR2 = AlgebraicRules[{a+b == s, a b == p},{a,b,s,p}]
         Together[1/a + 1/b]/.AR2
Out[]
         s/p
         
For more complicated expressions try  
	Map[Together, expr, Infinity, Heads->True]


*)





  • Prev by Date: Rational approximations
  • Next by Date: RDS.m in issue 3 of volume 1.
  • Previous by thread: Rational approximations
  • Next by thread: RDS.m in issue 3 of volume 1.