If you are using the unit system you probably have an idea of what unit a
given quantity should be represented, so there is no need to create dummy
place holders for units. That is what the units in SymPy are for:
>>> var('n P V T')
(n, P, V, T)
>>> from sympy.physics.units import *
>>> gas = Eq(n, P*V/R/T)
>>> rhs = gas.subs({P:3*atm, V:100*inch**3, T:200*K}).rhs
generic quantities:
>>> UnitSystem.get_unit_system("SI").get_dimensional_expr(rhs)
3 amount_of_substance*length *pressure ------------------------------------
energy
specific units and constants
>>> rhs
3 3*atmosphere*inch --------------------------- 2*kelvin*molar_gas_constant
a specific unit
>>> convert_to(rhs, mol)
0.299553802101669*mole
It might be instructive to try these 3 operations on R itself.
/c
On Friday, February 4, 2022 at 1:55:37 PM UTC-6 [email protected] wrote:
> I did not really say this, but this gets you all the automatic checking
> too. For example sympy will not add something with units of meters to
> something with units of seconds. I suspect this could be coupled to the
> units package by making sure that units behave as symbols.
>
> Jonathan
>
> On Friday, February 4, 2022 at 12:57:01 PM UTC-6 [email protected] wrote:
>
>> David,
>>
>> I too have found that the units package has been problematic for me to
>> use. My solution is the following.
>>
>> Units behave as positive numbers (real numbers) the way we use them in
>> the physical sciences. Thus I just define symbols for any units I want to
>> use as positive variables, for example: `var('J m kg s', positive = True)`.
>> Then I worry about conversions later. The only caveat is that you must make
>> sure not to use any of these variables for anything other than units. If I
>> want to define the speed of light I would do `c = 29979258*m/s` assuming I
>> do not want to use `c` for anything else. I have thought about pulling them
>> out of the physics.units package or scrapping them from NIST automatically,
>> but It hasn't been worth it to me.
>>
>> Here's a really simple example using my algebra_with_sympy package
>> <https://github.com/gutow/Algebra_with_Sympy> for ideal gas law and heat
>> capacity calculations. I could have defined some of the constants, but just
>> chose to copy and paste the numbers for the constants in for each
>> calculation. The second line of the numerical calculation of `n` also
>> illustrates using substitution to do unit conversions. If you just used
>> sympy all you would lose is the nice display of the algebraic equations.
>> [image: calc with units.png]
>> Jonathan
>> On Friday, February 4, 2022 at 11:30:02 AM UTC-6 [email protected]
>> wrote:
>>
>>> Dear Group,
>>>
>>> On the face of it, attaching appropriate physical units to variables
>>> sounds an excellent idea, but if, say, you import speed_of_light from
>>> sympy.physics.units, you have to extract an expression containing the
>>> speed and the physical units:
>>>
>>> sl=speed_of_light.convert_to(meter/second)
>>>
>>> This is OK, but in more complicated cases - e.g. the
>>> gravitational_constant - it would be nice to be able to extract the
>>> value with appropriate units without actually knowing the units in
>>> advance - just as if you looked it up in a handbook you would find
>>> something equivalent to
>>>
>>> 6.6743e-11*meter^2*newton/kilogram^2
>>>
>>> Also, if you just write an expression with speed_of_light, the result is
>>> confusing, because naively I thought speed_of_light would consist of an
>>> expression - 299792458*meter/second - but you can't use an expression of
>>> that sort at all (as far as I could see).
>>>
>>> Indeed it isn't clear why the class
>>> sympy.physics.units.quantities.Quantity exists.
>>>
>>> Am I correct that each of these named quantities have to be imported and
>>> subject to convert_to before they are any use in actual calculations?
>>> That didn't seem obvious from the documentation.
>>>
>>> I would have thought that introducing the units notation would mean that
>>> meaningless expressions would be faulted - checking the consistency of
>>> is a really useful operation.
>>>
>>> sl+3*meter
>>>
>>> 3*meter + 299792458*meter/second
>>>
>>> exp(-sl)
>>>
>>> exp(-299792458*meter/second)
>>>
>>> Neither of these fault, but I am wondering if there is a function to
>>> check potentially quite complicated expressions for consistency
>>> regarding units?
>>>
>>> Looking forward to reading your replies,
>>>
>>> David
>>>
>>>
>>>
>>>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/e656e96e-7faa-42f1-9801-fee682cbfcean%40googlegroups.com.