4
$\begingroup$

I have an expression that results in something like this:

Subscript[A, 1] + 
 Subscript[x, 
  3] (Subscript[A, 1] Subscript[k, 11] + {0, 0, 0}[Subscript[x, 1], 
     Subscript[x, 2]]

For simplicity's sake,

out:= A1+x3(A1K11+ {0,0,0}[x1,x2])

All numbers are subscripts. I know that {0,0,0}[x1,x2] =0 How can I tell that to Mathematica? there are multiple instances of the same in my program so a general fix would be ideal. Reiterating, I want to replace all instances of {0,0,0}[x1,xx2] with a zero

$\endgroup$
2
  • 1
    $\begingroup$ exp = Subscript[A, 1] + Subscript[x, 3] (Subscript[A, 1] Subscript[k, 11] + {0, 0, 0}[Subscript[x, 1], Subscript[x, 2]]); exp /. {0, 0, 0}[__] :> 0? $\endgroup$
    – kglr
    Dec 28, 2017 at 10:22
  • $\begingroup$ TIP: never use Subscript. $\endgroup$ Dec 28, 2017 at 16:18

2 Answers 2

5
$\begingroup$

In your case, you can match the {0,0,0} exactly. Therefore, you can use replacements or DeleteCases:

expr = Subscript[A, 1] + 
  Subscript[x, 
    3] (Subscript[A, 1] Subscript[k, 11] + {0, 0, 0}[Subscript[x, 1], 
      Subscript[x, 2]])

expr /. {0, 0, 0}[___] :> 0

DeleteCases[expr, {0, 0, 0}[___], Infinity]

If you want to generalize this, you just have to ask yourself, how should the pattern look. For instance, let's say you have cases like {0,0,1} and {1,0,3} and you want to delete them as well, then you can make your matching broader by using:

{_Integer, _Integer, _Integer}[___] :> 0

The rule above reads as follow: Replace everything with 0 that has a list of 3 integers in the front, followed by anything or nothing at all in brackets (that is what ___ stands for).

$\endgroup$
1
  • $\begingroup$ How can I use Replacements? I have a similar problem with a case that's {0,0,1} instead of {0,0,0}. I'd like to understand a general method. thanks $\endgroup$ Dec 28, 2017 at 13:05
3
$\begingroup$

Also

expr /. {0 ..} -> (0 &)  
% // TeXForm

$A_1 k_{11} x_3+A_1$

$\endgroup$
3
  • $\begingroup$ ... assuming, of course, that {0,0,0} appears only in the form {0,0,0}[somethings] in expr. $\endgroup$
    – kglr
    Dec 28, 2017 at 10:51
  • $\begingroup$ How would I do the same for {0,0,1}[ ___]? $\endgroup$ Dec 28, 2017 at 11:44
  • $\begingroup$ @Ashwin, you can use {0,0,1}->(0&) (again if {0,0,1} appears only as {0,01}[...]) $\endgroup$
    – kglr
    Dec 28, 2017 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.