Dropping higher order terms
- To: mathgroup at smc.vnet.net
- Subject: [mg4121] Dropping higher order terms
- From: Paul Abbott <paul at earwax.pd.uwa.edu.au>
- Date: Wed, 5 Jun 1996 01:38:53 -0400
- Organization: University of Western Australia
- Sender: owner-wri-mathgroup at wolfram.com
Regarding the question: I want to be able to drop higher order terms in an algebraic expression. As a simple example consider two matrices (mat1 = {{-1,a,b},{a,1,0},{b,0,1}}) // MatrixForm -1 a b a 1 0 b 0 1 (mat2 = {{1,0,c},{0,1,d},{c,d,1}}) // MatrixForm 1 0 c 0 1 d c d 1 where a, b, c, and d are small. The matrix product (prod = mat1 . mat2) // MatrixForm -1 + b c a + b d b - c + a d a 1 a c + d b + c d 1 + b c contains terms of first and second order in a, b, c, and d. How can I retain only the terms first order in in a, b, c, and d? A: Looking at FullForm[prod] List[List[Plus[-1, Times[b, c]], Plus[a, Times[b, d]], Plus[b, Times[-1, c], Times[a, d]]], List[a, 1, Plus[Times[a, c], d]], List[Plus[b, c], d, Plus[1, Times[b, c]]]] reveals that the terms you want to delete, say Times[b,c], are of the general form Times[__]. However, you do not want to delete terms like Times[-1,c] so you can use pattern-matching with Times[_Symbol,_Symbol..], (i.e., a product of two or more Mathematica symbols. The repetition operator .. indicates a pattern repeated one or more times) to retain only the terms first order in in a, b, c, and d: (prod /. Times[_Symbol,_Symbol..] :> 0) // MatrixForm -1 a b - c a 1 d b + c d 1 Here is a more complicated example: (mat1 . mat2 . mat1 // Expand) /. Times[_Symbol,_Symbol..] :> 0 2 2 2 2 {{1 + a + b , 0, -c + b c}, {0, 1 + a , d}, 2 2 {-c + b c, d, 1 + b }} You now want to delete all terms of the general form _^_ (i.e., powers). You can again use pattern-matching: (% /. _^_ :> 0) // MatrixForm 1 0 -c 0 1 d -c d 1 Combining these rules we have: firstorder = {Times[_Symbol,_Symbol..] :> 0, _^_ :> 0}; (mat1 . mat2 . mat1 . mat1 . mat2 // Expand) /. firstorder // MatrixForm -1 a b - 2 c a 1 2 d b + 2 c 2 d 1 _________________________________________________________________ Paul Abbott Department of Physics Phone: +61-9-380-2734 The University of Western Australia Fax: +61-9-380-1014 Nedlands WA 6907 paul at physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/Paul _________________________________________________________________ ==== [MESSAGE SEPARATOR] ====