scientific notation in pretty print and latex

308 views
Skip to first unread message

Rajeev Singh

unread,
Feb 7, 2022, 5:55:47 AM2/7/22
to sy...@googlegroups.com
Dear group,

What would be the best way to get numbers in the form 10^{...} while
using pretty print or latex? The following hack works -

ten = symbols("10", positive=True)
eps, mu = 4*pi*ten**(-11), ten**(-5)

but is messing up pretty print when used with sympy.vector objects.
Couldn't find anything on this in the mail archives.

Best wishes,
Rajeev

gu...@uwosh.edu

unread,
Feb 7, 2022, 8:36:26 AM2/7/22
to sympy
I'm not sure of your exact use case, but maybe what I do when rounding numbers to match their errors will give you some ideas. https://github.com/gutow/round_using_error. In this case I end up determining the power of ten for the scientific notation using log base 10.

Jonathan

Rajeev Singh

unread,
Feb 8, 2022, 1:02:19 AM2/8/22
to sy...@googlegroups.com
Thanks Jonathan,

Perhaps the following example explains what I want to do. Using pretty print we get -

In [5]: Bx = 2e-4 * cos(1e5 * t) * sin(1e-3 * y)

In [6]: Bx
Out[6]: 0.0002⋅sin(0.001⋅y_C)⋅cos(100000.0⋅t)

In [7]: Bx = 2 * ten**(-4) * cos(ten**5 * t) * sin(ten**(-3) * y)

In [8]: Bx
Out[8]:
     ⎛y_C⎞    ⎛  5  ⎞
2⋅sin⎜───⎟⋅cos⎝10 ⋅t⎠
     ⎜  3⎟          
     ⎝10 ⎠          
─────────────────────
           4        
         10          

Is there a better way to achieve the last behaviour to improve readability in the terminal with pretty print?

Also using this hack messes up pretty print if used with sympy.vector objects, which is perhaps a bug.

Best wishes,
Rajeev
 


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/64c23a71-7773-42f1-b4a6-92e230849508n%40googlegroups.com.

gu...@uwosh.edu

unread,
Feb 8, 2022, 8:43:22 AM2/8/22
to sympy
Rajeev,

I have done a little work with the sympy print/rendering package. Unfortunately, this looks like something that needs to adapt those mechanisms. Initially, I would suggest a specialized wrapper that determines the proper powers and then sends an adapted expression to the pretty_print package. Depending on how the vector printing works you may have to send strings not specialized symbols. I am not sure the pretty_print code itself could be adapted to what you want and still be compatible with everything else.

Jonathan

Rajeev Singh

unread,
Feb 8, 2022, 11:18:26 AM2/8/22
to sy...@googlegroups.com
Thanks Jonathan, 

I guess I will stick with my approach for now. The behavior of sympy.vector may actually be a bug as this trick puts the unit vector in the middle of the expression -

In [6]: vecB = Bx * C.i

In [7]: vecB
Out[7]: 
⎛     ⎛y_C⎞    ⎛  5  ⎞⎞    
⎜2⋅sin⎜───⎟ i_C⋅cos⎝10 ⋅t⎠⎟
⎜     ⎜  3⎟           ⎟    
⎜     ⎝10 ⎠           ⎟    
⎜─────────────────────⎟    
⎜           4         ⎟    
⎝         10          ⎠    


Perhaps sympy may consider displaying large and small numbers using this notation, something like https://github.com/gutow/round_using_error, by default for latex and pretty print. I think mathematica does something similar. 

Best wishes,
Rajeev 


Alan Bromborsky

unread,
Feb 8, 2022, 2:35:45 PM2/8/22
to sy...@googlegroups.com

If you are using vectors you may want to look at galgebra (built on top of sympy ) -

https://galgebra.readthedocs.io/en/latest/

It can be used simply as a regular vector algebra/calculus library. 

Aaron Meurer

unread,
Feb 8, 2022, 7:23:55 PM2/8/22
to sy...@googlegroups.com
The min and max parameters to latex() control when a float is printed
in scientific format. The sstr printer also has the same options, but
it looks like the pretty printer does not (it should not be hard to
add them, though).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAABz-z-jD3GPB3jw0q%3D4xuG0VCmcw4NO8154H3CRYJz4Ws2Tpw%40mail.gmail.com.

Rajeev Singh

unread,
Feb 8, 2022, 11:04:31 PM2/8/22
to sy...@googlegroups.com
Thanks Alan and Aaron, 

I will go through galgebra and latex options carefully to get nicer looking output. Thanks again for the suggestions. 

Best wishes,
Rajeev 


Rajeev Singh

unread,
Feb 9, 2022, 12:01:23 AM2/9/22
to sy...@googlegroups.com
Dear group,

One more point in this context. Things work fine for unevaluated expressions -

vecE = (1/eps) * Integral(delop.cross(vecH), t)

\vec{E} = ⎛     ⌠                         ⎞    
          ⎜     ⎮      ⎛x_C⎞    ⎛  4  ⎞   ⎟ k_C
          ⎜     ⎮ 4⋅cos⎜───⎟⋅cos⎝10 ⋅t⎠   ⎟    
          ⎜     ⎮      ⎜  2⎟              ⎟    
          ⎜  11 ⎮      ⎝10 ⎠              ⎟    
          ⎜10  ⋅⎮ ───────────────────── dt⎟    
          ⎜     ⎮           10            ⎟    
          ⎝     ⌡                         ⎠    



vecE.doit()

\vec{E} = ⎛    6    ⎛  4  ⎞    ⎛x_C⎞⎞    
          ⎜4⋅10 ⋅sin⎝10 ⋅t⎠⋅cos⎜───⎟ k_C⎟
          ⎜                    ⎜  2⎟⎟    
          ⎝                    ⎝10 ⎠⎠    


I hope this would help to find the issue.

Best wishes,
Rajeev

gu...@uwosh.edu

unread,
Feb 9, 2022, 8:59:57 AM2/9/22
to sympy
Rajeev,
If I understand correctly, you are worried about the way functions, factors and symbols are ordered in an evaluated expression. Your example is the expected behavior. When an expression is evaluated, sympy has to decide how to order the symbols. The default ordering is alphabetical. With capital letters first. For example if I set p = n*R*T/V (ideal gas law) the display of the value of p will return: RTn/V as the sorting is done within the parts of the expression (numerator and denominator). I believe there are some options for adjusting this, but others will have to speak to that.

Jonathan

Rajeev Singh

unread,
Feb 9, 2022, 10:21:10 AM2/9/22
to sy...@googlegroups.com
Thanks Jonathan for the explanation. But I am not pointing out this behavior here. Part of the (multi-line) bracket goes to the right of the unit vector k_C in the last expression, which is a display bug specific to pretty print in the terminal.

Unfortunately the entire expression gets jumbled on the mobile. To see this problem please view the mail in a browser so that proper formatting is visible. 

Best wishes, 
Rajeev 


gu...@uwosh.edu

unread,
Feb 9, 2022, 12:31:44 PM2/9/22
to sympy
So you are not worried about the ordering of the symbols, but the fact that you are not getting the symbol for the basis vector outside of the outer brackets. I agree that is not standard convention, but as everything else in the expression is a scalar, I would consider the expression correct no matter where the basis vector appears. Can you find an example where it is really wrong, such as a matrix operation on the basis vector or the formal calculation of the dot product between two vectors? That would probably help isolate the problem. I suggest you file a bug report (https://github.com/sympy/sympy/issues) or if you can figure out how to fix it a pull request.

regards,
Jonathan

Aaron Meurer

unread,
Feb 9, 2022, 3:51:11 PM2/9/22
to sy...@googlegroups.com
It would help if you could include a complete block of code to
reproduce your expression. Just showing the final expression isn't
helpful because we don't know how the various variables are defined.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/44a85189-29f6-465e-889e-6b1a9109bc87n%40googlegroups.com.

Rajeev Singh

unread,
Feb 10, 2022, 4:44:29 AM2/10/22
to sy...@googlegroups.com
Dear Jonathan and Aaron,

I have created a new issue with a short complete example code and its output showing the behavior -

I hope it explains the printing bug I was trying to explain. Thanks for your patience. 

Best wishes,
Rajeev



Reply all
Reply to author
Forward
0 new messages