4
$\begingroup$

I am having a problem with keeping the parentheses enclosing a dot product in HoldForm[]. I want to keep them in my equations since they indicate important grouping information. To prevent MMA removing the parentheses, I placed the whole expression in a HoldForm[], but MMA ignored that and still deleted them, which annoyed me quite a lot.

Example 1: The expression I was trying to input and what I got:

enter image description here

HoldForm[(a . b) (c . d) == Subscript[a, j] Subscript[b, j] Subscript[c, k] Subscript[d, k]] 

enter image description here

Example 2: The expression I was trying to input and what I got:

enter image description here

HoldForm[u + (a . b) v == a . a (b . v) a] 

enter image description here

Example 3: The expression I was trying to input and what I got:

enter image description here

HoldForm[a\[Cross](b\[Cross]c) == (a . c) b - (a . b) c] 

enter image description here

So I was wondering, how can I make MMA preserve the parentheses around a certain expression like dot product I placed in HoldForm[]?

$\endgroup$
6
  • $\begingroup$ Have you seen PrecedenceForm? $\endgroup$ Commented Jun 5, 2022 at 11:31
  • $\begingroup$ Thanks @LukasLang. I had never heard of it until saw your suggestion. I read the documentation just now and found it useful. However, there seems no method or function to find the precedence levels specified by integers of a certain operation, despite few of the them are listed in the notes for Infix. If there is a way to find integer-specified precedence levels for any operator, then solution you suggested would be perfect enough. $\endgroup$ Commented Jun 5, 2022 at 13:05
  • 1
    $\begingroup$ You can use the (undocumented for some reason) function Precedence to get the numbers, e.g. Precedence[Times] gives 400. $\endgroup$ Commented Jun 5, 2022 at 13:17
  • 1
    $\begingroup$ Posting code is nicer than posting images. $\endgroup$ Commented Jun 5, 2022 at 16:29
  • $\begingroup$ Thanks @MichaelE2 for your suggestion. I have made an addendum of code to my original post. Hope it looks nicer now. $\endgroup$ Commented Jun 6, 2022 at 3:33

1 Answer 1

6
$\begingroup$

Putting my comment into an answer

You can use PrecedenceForm to get what you want:

HoldForm[PrecedenceForm[a.b, 350] PrecedenceForm[c.d, 350] == Subscript[a,i] Subscript[b,i] Subscript[c,k] Subscript[d,k]] 

enter image description here

The precedence of 350 just needs to be lower than the precedence of Times (so that a.b needs parentheses, similar to how a+b would need paretheses since its precedence is lower than that of Times). To get the precedence, Precedence (undocumented) can be used:

Precedence[Times] (* 400. *) 
$\endgroup$
1
  • $\begingroup$ Thanks Lukas! No wonder I did not find the method to get the precedence integers, as you said it turns out to be undocumented. Since that can be obtained throughPrecedence[], the solution you suggested is perfect enough! I have accepted it ! $\endgroup$ Commented Jun 6, 2022 at 3:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.