About PrintAs and how to make customized operators look nice

115 views
Skip to first unread message

alej...@gmail.com

unread,
Jan 3, 2023, 2:21:58 PM1/3/23
to xAct Tensor Computer Algebra
Hi all!

My questions are about output formats in xAct and how to make customized operators compatible with them.

Usually, when I try to define some nice-looking symbol in Mathematica, I use the Format command. For example,

Format[AA]:= "A"

so whenever you write an input involving the lengthy name "AA", in the output you just see "A" instead of "AA". Let's assume that the output I get is lengthy and I am not interested in using "%", but only in a couple of terms of the output. If I naively copy those terms with my mouse and paste them in a new cell, the result I get is "A" instead of the name of the original variable "AA".

This is very nicely treated in xAct, in which you can copy from the output, which is in a form made of format boxes, and paste the actual commands. In xAct, the equivalent of Format is (at least for some basic purposes)  PrintAs[AA]:= "A" for the head AA of a certain tensor.

At this point, I have two questions (the first one is probably not about xAct but Mathematica in general):

1) How does xAct (or PrintAs) deal with this clipboard issue I mentioned? Is there an easy way to translate this property to a general Mathematica notebook? (maybe an option for Format I am missing, or maybe using alternatives like MakeBoxes[AA, StandardForm]:=...). I have been searching in the general Mathematica documentation but I have not found anything, and since xAct does exactly what I want, that's why I post this question here :)

2) My second question is related to defining an operator, let's call it "Op", that we want it to look nice.

Let me put this in a specific context. I am currently studying junction conditions and I want to introduce the "discontinuity of" operator that is usually denoted with a pair of brackets. If we define a manifold with indices {a,b,c...} and a metric with covariant derivative Dg, such an operator would act, for example, as

Op[RicciDg[-a,-b]]

I want this to be a tensor with exactly the same symmetries and properties as RicciDg. I managed to avoid xAct warnings by introducing the lines:

Op /: FindIndices[Op[A_]] := FindIndices[Evaluate[A]]
Op /: SymmetryOf[Op[A_]] := SymmetryOf[A]
Op /: ToCanonical[Op[A_]] := Op[ToCanonical[A]]

(Of course, one can be even more refined and add a "?" command checking that A is an xAct tensor and so on...)

So far so good. Now, my question is that I want it to look as a pair of brackets:

Op[RicciDg[-a,-b]]  ->   Output as (schematically):  [ R[nabla]_{ab}   ]

Is there a simple way to use the nice properties of PrintAs to achieve this? (preserving the feature I mentioned in my first question).


Thanks a lot, and happy 2023!
Alejandro

TB

unread,
Jan 3, 2023, 2:57:44 PM1/3/23
to xAct Tensor Computer Algebra
Hi!
This problem can be solved in many different ways.
1) xAct makes copying of the output work (most of the time) using interpretation boxes. So for instance if one would like to make IntExpr[integrand,0] display as an integral but still be copyable one can for instance write
MakeBoxes[IntExpr[bulk_, 0], StandardForm] := xAct`xTensor`Private`interpretbox[IntExpr[bulk, 0], RowBox[{SubscriptBox["\[Integral]", "\[CapitalOmega]"], MakeBoxes[bulk, StandardForm], "d", SubscriptBox["\[Mu]", "\[CapitalOmega]"]}]];
You could try something similar for your operator if you like.

2) If your operator does not affect the index structure, you can define your operator as an InertHead and set the PrintAs to whatever you want.
DefInertHead[MyOp, PrintAs -> "A"]

Then you can use it as
MyOp[delta[-a, b]]

3) You can define an operator as something that takes a tensor head and gives a new tensor head, i.e. without indices.
xTensorQ[MyTensorOp[LL_?xTensorQ]] ^= True;
SlotsOfTensor[MyTensorOp[LL_?xTensorQ]] ^:= SlotsOfTensor[LL];
SymmetryGroupOfTensor[MyTensorOp[LL_?xTensorQ]] ^:=  SymmetryGroupOfTensor[LL];
PrintAs[MyTensorOp[LL_?xTensorQ]] ^:= RowBox[{"A", PrintAs[LL]}];

You might want to add some more things too, but these are the most important ones.
This kind of operator you can use like
MyTensorOp[delta][-a, b]


If you want the printout to be just a pair of brackets around the tensor with indices, the simplest way would be
DefInertHead[Op, PrintAs -> ""]

Regards
Thomas

alej...@gmail.com

unread,
Jan 25, 2023, 10:15:15 AM1/25/23
to xAct Tensor Computer Algebra
Hi all!

First I would like to thank Thomas for the suggestions. I have been recently playing a bit with these different options. The last two work pretty well.  Regarding the first one, suppose I define some operator acting on tensor expressions, like  Op[T[-a] V[-b]], ..., with some format, let's say, between curly brackets:

MakeBoxes[Op[A_], StandardForm] :=  xAct`xTensor`Private`interpretbox[Op[A], RowBox[{"{",  MakeBoxes[A, StandardForm], " } " }]]

There are a couple of issues I wanted to share with you:

1) Interestingly, in all the outputs, the operator appears between an additional pair of parenthesis, namely,

In: 2 Op[V[-a]] + W[-a]

Out: 2 ( {V_a } )  + W_a

It doesn't disappear after Simplify or things like that. Any ideas on how to eliminate it? I undestand that Mathematica includes it probably for a good reason, but I don't know what is behind that.

2) I am trying to find, as I mentioned in my previous email, a few lines to avoid xact warnings. I am using


Op /: FindIndices[Op[A_]] := FindIndices[Evaluate[A]]
Op /: SymmetryOf[Op[A_]] := SymmetryOf[A]
Op /: ToCanonical[Op[A_]] := Op[ToCanonical[A]]

Unprotect[ToCanonical];
ToCanonical[Op[A_]*B_] :=  Op [ToCanonical[A]] ToCanonical[B]
Protect[{ToCanonical}];

However I still get the warning "Unknown expression not canonicalized: Op[V[a]]" when I try for instance

In: Dg[-b][Op[V[a]]]    (here Dg is the Levi-Civita derivative of a certain metric)

In principle, one can ignore the warning, but the thing is that in these types of terms, the operator is removed from the output! Namely,

In:   Dg[-b][Op[V[a]]]
Out: Dg[-b][V[a]]    (+ warning)

I tried with

ToCanonical[Dg[a_][Op[A_]]] := Dg[a_][ Op [ToCanonical[A]] ]

but the problem remains.


Thanks!
Alejandro


TB

unread,
Jan 25, 2023, 11:00:43 AM1/25/23
to xAct Tensor Computer Algebra
Hi!

1)
When I try the following code in Mathematica 13.2 under Windows 10
<< xAct`xTensor`
$DefInfoQ = False;
SetOptions[AutomaticRules, Verbose -> False];
DefManifold[M4, 4, {a, b, c, d, f, p, q, r, m, l, h, j, n, t, s}]
DefMetric[{1, 3, 0}, g[-a, -b], CD]
DefTensor[V[-a], M4]
DefTensor[W[-a], M4]
MakeBoxes[Op[A_], StandardForm] :=
 xAct`xTensor`Private`interpretbox[Op[A],
  RowBox[{"{", MakeBoxes[A, StandardForm], " } "}]]

2 Op[V[-a]] + W[-a]

I only get an output like
2 {V_a} + W_a

I do not get an extra pair of (), so I have no idea why you got it. Check InputForm or FullForm on it to see something else was added.

2) If you want to use ToCanonical you have to stick to the kind of objects xAct natively handles like xTensor, InertHead etc.
ToCanonical decomposes your expression completely and makes another representation of your expression before it does the actual canonicalization.
Therefore, it will be very cumbersome to add another kind of object it does not recognize.

You can tell xAct that some object belongs to one of the recognized classes with InertHeadQ[Op]^=True etc, but in your case Op works like an InertHead, so I would recommend

DefInertHead[Op]

If you then want a special output you can change it by stopping the default, setting up your style, and starting the default for all other InertHead operators with

xTensorFormStop[InertHead]
MakeBoxes[Op[A_], StandardForm] :=
 xAct`xTensor`Private`interpretbox[Op[A],
  RowBox[{"{", MakeBoxes[A, StandardForm], " } "}]]
xTensorFormStart[InertHead]


Regards
Thomas

alej...@gmail.com

unread,
Jan 26, 2023, 11:31:34 AM1/26/23
to xAct Tensor Computer Algebra
 Hi Thomas,

Many thanks! Your second point is exactly what I was looking for. That makes everything compatible with xAct without introducing ad hoc commands.

Regarding the misterious parenthesis I have to apologize because I forgot to mention an important part: I wrote the symbol in color!

You can try for example with this slight modification of your code:


<< xAct`xTensor`
$DefInfoQ = False;
SetOptions[AutomaticRules, Verbose -> False];
DefManifold[M4, 4, {a, b, c, d, f, p, q, r, m, l, h, j, n, t, s}]
DefMetric[{1, 3, 0}, g[-a, -b], CD]
DefTensor[V[-a], M4]
DefTensor[W[-a], M4]
DefInertHead[Op]


xTensorFormStop[InertHead]
MakeBoxes[Op[A_], StandardForm] :=
 xAct`xTensor`Private`interpretbox[Op[A], RowBox[
{"\!\(\*StyleBox[\"{\",\nFontColor->RGBColor[0, 0, 1]]\)",
    MakeBoxes[A, StandardForm], 
"\!\(\*StyleBox[\"}\",\nFontColor->RGBColor[0, 0, 1]]\)"}]]
xTensorFormStart[InertHead]

Now 2 Op[V[-a]] + W[-a]  gives  2 ({V_a}) + W_a

Weird, isn't it?

All the best,
Alejandro

Thomas Bäckdahl

unread,
Jan 26, 2023, 11:43:14 AM1/26/23
to xa...@googlegroups.com
Hi!

Yes, it was strange that you got an extra pair of parenthesis.
Here you are already in Box mode so you don't need complicated strings with \! etc.

I tried this instead

MakeBoxes[Op[A_], StandardForm] :=
 xAct`xTensor`Private`interpretbox[Op[A],
  RowBox[{StyleBox["{", FontColor -> RGBColor[0, 0, 1]],
    MakeBoxes[A, StandardForm],
    StyleBox["}", FontColor -> RGBColor[0, 0, 1]]}]]

For me it seems to do the trick.

Regards
Thomas
--
You received this message because you are subscribed to the Google Groups "xAct Tensor Computer Algebra" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xact+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xact/9e97b481-0e68-4136-aeed-b943b96e24dcn%40googlegroups.com.

alej...@gmail.com

unread,
Jan 26, 2023, 2:53:43 PM1/26/23
to xAct Tensor Computer Algebra
Hi!

Yes! For me too. I hope people find these posts useful to customize objects in xAct.

Many thanks!

Best,
Alejandro

Reply all
Reply to author
Forward
0 new messages