On Sun, Jun 21, 2009 at 8:34 PM, Bryan Bishop<[email protected]> wrote:
> On Sun, Jun 21, 2009 at 8:21 PM, Ondrej Certik<[email protected]> wrote:
>> On Sun, Jun 21, 2009 at 7:07 PM, Bryan Bishop<[email protected]> wrote:
>>> On Sun, Jun 21, 2009 at 7:20 PM, Ondrej Certik<[email protected]> wrote:
>>>> On Sun, Jun 21, 2009 at 5:43 PM, Bryan Bishop<[email protected]> wrote:
>>>>> On Sun, Jun 21, 2009 at 6:13 PM, Ondrej Certik<[email protected]> wrote:
>>>>>> On Sun, Jun 21, 2009 at 4:27 PM, Bryan Bishop<[email protected]> wrote:
>>>>>>> I'd like to be able to differentiate with respect to a unit (like from
>>>>>>> physics.units.Unit). But the problem is that physics.units.Unit isn't
>>>>>>> inheriting from Symbol. I wonder why this is. Was this a design
>>>>>>> decision? I think I might be able to convert it into a derived class
>>>>>>> of Symbol, but I was wondering if anyone wanted to yell at me first or
>>>>>>> offer suggestions or something. :-)
>>>>>>
>>>>>> I don't know any reason right now -- just try if it works and if so,
>>>>>> please send us a patch fixing it and a test.
>>>>>
>>>>> Maybe I'll write some unit tests of what the expected functionality
>>>>> should be, and get back with you.
>>>>
>>>> That'd be awesome. I forgot to ask why you need it. :)
>>>
>>> I am working on a symbolic equation extraction algorithm in python
>>> (it's under the name "skdb"). Somewhat related to nodal analysis in
>>> electric circuit theory, or "linear graph theory", or bond graphs,
>>> which is used to come up with state equations in mechatronics or other
>>> multi-domain systems (thus lots of different unit combination
>>> possibilities). In some cases, components in the system graph provide
>>> differential equations which need to be manipulated symbolically. So,
>>> being able to differentiate with respect to a unit would be useful.
>>> Also retaining all of the functionality of the usual Symbol objects.
>>
>> I don't understand it much, but why don't you treat everything
>> symbolically and use units only at the very end of the calculation? It
>> seems to me, that differentiating with respect to "meters" makes only
>> sense to recover the "number", e..g
>>
>> diff(5*meter, meter)  == 5
>>
>> But that can be easily fixed but enhancing our units module.
>
> Just leaving it for the end of the calculation would be pointless
> since there are some symbolic operations that happen in the mean time
> when you're doing the differentiation in the first place. A few months
> ago when I was looking into this I considered the idea of adding the
> "pq" library from pypi as a dependency, and make a member variable in
> Symbol for a pq unit. But when I did this, it turned out that there
> were so many functions that I had to update that it didn't look like a
> good idea. Maybe that's the approach I should take?
>
> pq ("physical-quantities") is found over at:
> http://packages.python.org/quantities/user/tutorial.html

Okay, I have written some unit tests to help explain what I am going on about.

http://paste.blixt.org/106593

or as a backup:
http://adl.serveftp.org/sympy-units.py

(and it's also attached)

- Bryan
http://heybryan.org/
1 512 203 0507

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

#!/usr/bin/python
# sympy units shelltrance
# Bryan Bishop ([email protected]) http://heybryan.org/
# 2009-06-21

from sympy import Symbol
from sympy.physics. import * # units.m
import unittest

class test_sympy_units(unittest.TestCase):
        def test_integration(self):
                t = Symbol("t") * units.s
                self.assertTrue(
                
                # integral of acceleration w.r.t time (t) is velocity
                acceleration = Symbol("a") * units.m * units.s**(-2)
                velocity = integrate(acceleration,t)
                
                self.assertTrue(velocity.units == (units.m/units.s))

        def test_basic_stuff(self):
                # specify units when instantiating the symbol
                t = Symbol("t",units.s)
                # check that the units are correct
                self.assertTrue(t.units==units.s)

                # specifying units elsehow.
                mass = Symbol("m") * units.kg
                self.assertTrue(mass.units==units.kg)
                self.assertTrue(mass.units==units.g*1000)

        def test_multiplication(self):
                # J = kg * m**2 * s**(-2) = N * m
                # N = kg * m * s**(-2)
                mass = Symbol("m1") * units.kg
                force = Symbol("f1") * units.s**(-2)*units.kg*units.m
                energy = mass*force
                self.assertTrue(energy.units==units.J)

        def test_addition(self):
                # the fun one?
                mass = Symbol("m2") * units.kg
                energy = Symbol("e1") * units.J
                self.assertRaises(mass*energy)
                

if __name__ == '__main__':
        unittest.main()

Reply via email to