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 gu...@uwosh.edu 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 da...@dbailey.co.uk > 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 sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/550f5fae-6891-4f6e-bb30-91fc4998427an%40googlegroups.com.