vermaseren / form

The FORM project for symbolic manipulation of very big expressions
GNU General Public License v3.0
982 stars 118 forks source link

Improving format mathematica? #460

Closed vsht closed 4 months ago

vsht commented 8 months ago

Dear FORM developers,

I guess that all FORM users exporting their results to Mathematica are aware of the issue that happens when naively trying to save expression with scalar products, e.g.

Auto V p;
Auto S s;

L exp = s1*p1.p2 + s2*p3.p4^2 + s3*p3.p4^3;

.sort 
Format Mathematica;
print;
.end

This yields

 p1 . p2*s1 + p3 . p4^2*s2 + p3 . p4^3*s3

which Mathematica happens to interpret as

Plus[Times[s1,Dot[p1,p2]],Times[s2,Dot[p3,Power[p4,2]]],Times[s3,Dot[p3,Power[p4,3]]]]

as compared to the correct version

      p1 . p2*s1 + ( p3 . p4)^2*s2 + (p3 . p4)^3*s3

yielding

Plus[Times[s1,Dot[p1,p2]],Times[s2,Power[Dot[p3,p4],2]],Times[s3,Power[Dot[p3,p4],3]]]

I know that one could do something like

repeat;
id v1?.v2? = sp(v1,v2);
id (v1?.v2?)^(-1) = pow(sp(v1,v2),-1);
endrepeat;

to circumvent the issue, but perhaps it could be nice if FORM could produce the right expressions right away?

Cheers, Vlad

tueda commented 8 months ago

Maybe, we could always enclose dot products in parentheses, even when not needed. This may be easier to implement than enclosing or not enclosing dot products in parentheses depending on context.

For reference, operator precedence in Mathematica:

https://reference.wolfram.com/language/tutorial/OperatorInputForms.html

The binary operator . (Dot) has a lower precedence than the binary operator ^ (Power).

vermaseren commented 8 months ago

A bit confusing. What does it do with 3.2^4 ? (floating point 3.2) Or do you have to write that also as (3.2)^4 ?

On 21 Oct 2023, at 11:19, Takahiro Ueda @.***> wrote:

Maybe, we could always enclose dot products in parentheses, even when not needed. This may be easier to implement than enclosing or not enclosing dot products in parentheses depending on context.

For reference, operator precedence in Mathematica:

https://reference.wolfram.com/language/tutorial/OperatorInputForms.html

The binary operator . (Dot) has a lower precedence than the binary operator ^ (Power).

— Reply to this email directly, view it on GitHub https://github.com/vermaseren/form/issues/460#issuecomment-1773728963, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJPCETQE3NGNNBA6JXPLWDYAOHS3AVCNFSM6AAAAAA6JBIYZ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZTG4ZDQOJWGM. You are receiving this because you are subscribed to this thread.

tueda commented 8 months ago

I guess Mathematica treats 3.2 as an "atom". So, 3.2^4 is fine.

Indeed,

3.2^a

gives

Power[3.2`, a]

and

p.q^a

gives

Dot[p, Power[q, a]]
vermaseren commented 8 months ago

I do not understand why they do it this way. What else can the . operator do? Maybe it was planned to use it for data structs, but then it should also bind tighter.

On 21 Oct 2023, at 13:43, Takahiro Ueda @.***> wrote:

I guess Mathematica treats 3.2 as an "atom". So, 3.2^4 is fine.

Indeed,

3.2^a gives

Power[3.2`, a] and

p.q^a gives

Dot[p, Power[q, a]] — Reply to this email directly, view it on GitHub https://github.com/vermaseren/form/issues/460#issuecomment-1773766053, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJPCEVIJRC7MGUBF222KD3YAOYOHAVCNFSM6AAAAAA6JBIYZ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZTG43DMMBVGM. You are receiving this because you commented.

tueda commented 8 months ago

The dot operator perfectly works with vectors/tensors in a reasonable way.

For example, the Mathematica code

{1, 2}.{3, 4}^2

gives

41

This is the same as other languages, like Python:

import numpy as np

a = np.array([1, 2])
b = np.array([3, 4])

a@b**2

which also gives 41, not 121.

I think an expression $p \cdot q^2$ in math or physics papers would be understood as $p \cdot (q^2)$.