Ask Your Question
1

Working with units

asked 2020-01-12 07:49:40 +0100

erikblue gravatar image

Hi,

I am just getting started with sage. I'm an electrical engineer. One thing that was really nice about Mathcad was letting me know if my units were messed up. I see there are electrical units types, like "units.electrical_potential.volt" but I'm struggling to see how to use it in a real application.

For example, a resistor, do I do something like this for 220 ohm resistor?

R = 220 * units.resistance.ohms

And what I'd really like to do is then define a voltage:

V = 1*units.electrical_potential.volt

And then be able to just go like this to get the current through the resistor:

V/R

Which would output something like this:

0.00455 * units.current.amp

Or better yet, just:

4.55mA

I don't mind building my own class to achieve this functionality. I also need to create my own units, for example noise spectral density such as V*sqrt(Hz).

Anyways, I really like what I see with Sage so far, I tried to use PTC Mathcad a while back and was pulling my hair out... Sage seems super intuitive and flexible, just being able to type things out in the notebook is great.

Thank you!

Erik

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2020-01-13 11:19:46 +0100

eric_g gravatar image

Do you know Pint?

As for any Python package, it should be easy to install it in SageMath by typing the following command in a terminal:

sage -pip install pint

NB: this requires SageMath 9.0 since the latest version of Pint works only with Python 3.

edit flag offensive delete link more

Comments

3

astropy also has a very powerful units and quantities package which took much of its original inspiration from Pint. Actually, lately they've been converging more and more again on shared functionality (thanks in part to recent updates to Numpy making propagation of units easier)--I wonder if one day it will be possible for them to converge, rather than maintaining separate projects. Though astropy.units does have some more advanced functionality specific to the astronomy community (though also useful for most any science).

Iguananaut gravatar imageIguananaut ( 2020-01-13 13:05:17 +0100 )edit

Ok, so I'm doing fine with pint with one exception: When I define a symbolic expression and then try to evaluate that expression using variables that have units. For example, I define a symbolic expression:

Vres(Vref,RES)=2*Vref/2^RES

And I try to evaluate it using a pint variable (with units I defined):

Vres(5*u.V, 20)

I get a type error:

TypeError: no canonical coercion from <class 'pint.quantity.build_quantity_class.<locals>.Quantity'> to Callable function ring with arguments (Vref, RES)

I guess this makes sense because how would the symbolic expression class know what to do with a pint type variable? Any ideas?

erikblue gravatar imageerikblue ( 2020-01-29 22:41:35 +0100 )edit
0

answered 2020-01-30 03:06:46 +0100

erikblue gravatar image

Ok, so I'm doing fine with pint with one exception: When I define a symbolic expression and then try to evaluate that expression using variables that have units. For example, I define a symbolic expression:

Vres(Vref,RES)=2*Vref/2^RES

And I try to evaluate it using a pint variable (with units I defined):

Vres(5*u.V, 20)

I get a type error:

TypeError: no canonical coercion from <class 'pint.quantity.build_quantity_class.<locals>.Quantity'> to Callable function ring with arguments (Vref, RES)

I guess this makes sense because how would the symbolic expression class know what to do with a pint type variable? Which brings me back to the built-in units with SageMath. With built-in "units" class, symbolic expressions are fine, but I can't add my own custom units so I'm stuck... I desperately need units like V, nV, uV, mV, and nV/sqrt(Hz).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-01-12 07:48:43 +0100

Seen: 1,072 times

Last updated: Jan 30 '20