Displaying quantities in SI base units

50 views
Skip to first unread message

Teo

unread,
Nov 26, 2022, 2:13:06 AM11/26/22
to sympy
Hi all. I just started using Sympy this week. According to this video, when the following code was run, the units of ohm would be returned in SI base units.

import sympy.physics.units as u
u.ohm

However, when I tried, I got the symbol omega instead. Is there a method to obtain the same SI base units (kgm^2)/(A^2s^3)?

Thank you

Oscar Benjamin

unread,
Nov 28, 2022, 9:55:20 AM11/28/22
to sy...@googlegroups.com
You can do it like this:

In [1]: import sympy.physics.units as u

In [3]: u.convert_to(u.ohm, [u.kg,u.m,u.ampere,u.s])
Out[3]:
2
kilogram⋅meter
───────────────
2 3
ampere ⋅second

Probably there should be an easier way. It's also possible like this:

In [4]: u.convert_to(u.ohm, u.si.SI._base_units)
Out[4]:
2
kilogram⋅meter
───────────────
2 3
ampere ⋅second

In [5]: u.si.SI._base_units
Out[5]: (meter, kilogram, second, ampere, mole, candela, kelvin)

That's using _base_units which has a leading underscore indicating
that it should be considered "private". I don't see why it should be
considered private though...

Oscar

Teo

unread,
Nov 28, 2022, 3:44:35 PM11/28/22
to sympy
Thanks Oscar, that works. I can see why the current output is preferred by many, but the SI base units output is still useful for learners of Physics. My learners are young, so I hope there could be an easier way to achieve this.

gu...@uwosh.edu

unread,
Nov 28, 2022, 5:51:07 PM11/28/22
to sympy
Teo,

My solution is to treat units as positive valued symbols and skip the units tool in sympy. Then you just define things in the set of units you wish to use and force your students to think about converting among them. For some examples see the documentation for Algebra_with_Sympy. I then use the .subs() to substitute in conversion factors.

Jonathan

Chris Smith

unread,
Nov 29, 2022, 12:58:21 PM11/29/22
to sympy
I referenced this discussion in https://github.com/sympy/sympy/issues/23022

/c

Aaron Meurer

unread,
Nov 29, 2022, 6:10:03 PM11/29/22
to sy...@googlegroups.com
On Sat, Nov 26, 2022 at 12:13 AM Teo <pss....@gmail.com> wrote:
>
> Hi all. I just started using Sympy this week. According to this video, when the following code was run, the units of ohm would be returned in SI base units.

I didn't know about that video. I'm always happy to see more video
tutorial content for SymPy, and community created content in general.

>
> import sympy.physics.units as u
> u.ohm
>
> However, when I tried, I got the symbol omega instead. Is there a method to obtain the same SI base units (kgm^2)/(A^2s^3)?

The units module was rewritten since this video. You can see
https://docs.sympy.org/latest/modules/physics/units/index.html for a
description of the new module. The new module is designed to be
general enough to handle different unit systems, not just SI, which is
why it no longer automatically converts everything down to SI base
units.

Aaron Meurer

>
> Thank you
>
> --
> 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/e09ee812-50d0-4092-95e4-d22b513f4651n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages