Units in the electronic context

50 views
Skip to first unread message

Thiago Costa de Paiva

unread,
Aug 9, 2015, 9:28:34 PM8/9/15
to sy...@googlegroups.com
Hello guys

I use python (notebook, sympy, pyspice and matplotlib are a very good
team) to design some electronic projects allowing me to have almost
everything in only one place.

I tried for a while to play with the units but I was unable to go
further. Today I asked a question [0] in Stack Overflow that confirmed
that it was not possible.

[0] http://stackoverflow.com/questions/31906377/volts-as-volts-in-sympy

Is there a way to not have the units as the most basic ones (SI) but
according to the electronic context itself, like:

ohm * ampere -> volt
volt / ampere -> ohm
volt / ohm -> ampere
ampere * volt -> watt
... and so on.

Thank you for your excellent work.

Best Regards,
Thiago

--
Thiago Costa de Paiva

Aaron Meurer

unread,
Aug 9, 2015, 10:45:04 PM8/9/15
to sy...@googlegroups.com
Have you looked at the new sympy.physics.unitsystems module? It should be more flexible than the sympy.physics.units module.

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 post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/20150809195655.GA1508%40reco.
For more options, visit https://groups.google.com/d/optout.

Thiago Costa de Paiva

unread,
Aug 9, 2015, 10:51:55 PM8/9/15
to sy...@googlegroups.com
Thank you for your suggestion, Aaron. I tried to look at it, but I was
not able to find the path to the solution. Do you have a tip?

Thiago


Em 2015/08/09 21:44:41, Aaron Meurer escreveu:
> Have you looked at the new sympy.physics.unitsystems module? It should be more
> flexible than the sympy.physics.units module.
>
> Aaron Meurer
>
> On Sun, Aug 9, 2015 at 2:56 PM, Thiago Costa de Paiva <[1]tec...@tecepe.eng.br>
> wrote:
>
> Hello guys
>
> I use python (notebook, sympy, pyspice and matplotlib are a very good
> team) to design some electronic projects allowing me to have almost
> everything in only one place.
>
> I tried for a while to play with the units but I was unable to go
> further. Today I asked a question [0] in Stack Overflow that confirmed
> that it was not possible.
>
> [0] [2]http://stackoverflow.com/questions/31906377/volts-as-volts-in-sympy
>
> Is there a way to not have the units as the most basic ones (SI) but
> according to the electronic context itself, like:
>
> ohm * ampere -> volt
> volt / ampere -> ohm
> volt / ohm -> ampere
> ampere * volt -> watt
> ... and so on.
>
> Thank you for your excellent work.
>
> Best Regards,
> Thiago
>
> --
> Thiago Costa de Paiva
>
> --
> 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 [3]sympy+un...@googlegroups.com.
> To post to this group, send email to [4]sy...@googlegroups.com.
> Visit this group at [5]http://groups.google.com/group/sympy.
> To view this discussion on the web visit [6]https://groups.google.com/d/
> msgid/sympy/20150809195655.GA1508%40reco.
> For more options, visit [7]https://groups.google.com/d/optout.
>
>
> --
> 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 [8]sympy+un...@googlegroups.com.
> To post to this group, send email to [9]sy...@googlegroups.com.
> Visit this group at [10]http://groups.google.com/group/sympy.
> To view this discussion on the web visit [11]https://groups.google.com/d/msgid/
> sympy/
> CAKgW%3D6KQFkCY410dkNfa%3D7n8jLh7gvQcR%2B6cQ3t9GRMLgLZspg%40mail.gmail.com.
> For more options, visit [12]https://groups.google.com/d/optout.
>
> References:
>
> [1] mailto:tec...@tecepe.eng.br
> [2] http://stackoverflow.com/questions/31906377/volts-as-volts-in-sympy
> [3] mailto:sympy%2Bunsu...@googlegroups.com
> [4] mailto:sy...@googlegroups.com
> [5] http://groups.google.com/group/sympy
> [6] https://groups.google.com/d/msgid/sympy/20150809195655.GA1508%40reco
> [7] https://groups.google.com/d/optout
> [8] mailto:sympy+un...@googlegroups.com
> [9] mailto:sy...@googlegroups.com
> [10] http://groups.google.com/group/sympy
> [11] https://groups.google.com/d/msgid/sympy/CAKgW%3D6KQFkCY410dkNfa%3D7n8jLh7gvQcR%2B6cQ3t9GRMLgLZspg%40mail.gmail.com?utm_medium=email&utm_source=footer
> [12] https://groups.google.com/d/optout

--
Thiago Costa de Paiva
FSF member: 11963
Linux User: 565327

Thiago Costa de Paiva

unread,
Aug 14, 2015, 4:44:35 PM8/14/15
to sy...@googlegroups.com
OK, so I have a work around here:

from sympy.physics import units as u

class MyVars:
def __init__(self):
'''create a dict of units of interest'''
self.my_units = {}
self.my_units[u.V] = 'V'
self.my_units[u.W] = 'W'
self.my_units[u.A] = 'A'
self.my_units[u.Hz] = 'Hz'
self.my_units[u.percent] = '\%'
self.my_units[u.F] = 'F'
self.my_units[u.s] = 's'

def identify_unit(self, value):
'''
Identify the unit from ones in the list of interest
and then split the value in a number and the new unit.
Here "value" is a standard "sympy + sympy.physics.units" var.
Return None if the unit received is not in the list.
'''
for my_unit in self.my_units.keys():
aux = value.as_coefficient(my_unit)
if aux:
if aux.is_number:
return aux, self.units[unit]
return None

def change_factor(self, value):
'''
value is in the form got from "identify_unit" above:
value[0]: value itself;
value[1]: unit from the list of interest.
Here, I adjust the value and unit in case of M (mega),
k (kilo), m (milli) and \mu (micro), and return it.
'''
aux = abs(value)
if aux >= u.mega:
return value / (10 ** 6), "M" + value[1]
elif aux >= u.kilo:
return value[0] / (10 ** 3), "k" + value[1]
elif aux >= 1:
return value[0], value[1]
elif aux >= u.milli:
return value[0] * (10 ** 3), "m" + value[1]
elif aux >= u.micro:
return value[0] * (10 ** 6), "\mu{}" + value[1]

So, that is the solution that I am using right now. It is far away from a
complete and useful set of helper functions and units. However, if you think
that it has potentials to be part of the Sympy project, just guide me through
the process and let me know what more is needed.

Cheers,
Thiago
Reply all
Reply to author
Forward
0 new messages