sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.36k stars 462 forks source link

Metrology module (a different implementation of the units module) #9763

Open cousteaulecommandant opened 14 years ago

cousteaulecommandant commented 14 years ago

This is a module that implements physical units in a more convenient way than the already existing units module: instead of creating separate variables for each unit, it creates an object containing the value, the dimension (mass, length, time...) as a list, and the units as an expression, plus the conversion factor.

It includes an easy-to-use function to create units from a string, implements SI prefixes and has a nice LaTeX output.

TO DO: Add more units (there are currently only 74, plus 13 constants), review the documentation and code in case it doesn't follow the Sage style, modify sage/symbolic/all.py to include this module (adding "import metrology" causes Sage to throw exceptions when starting).

CC: @kini

Component: symbolics

Keywords: units, SI, metrology

Author: Javier Mora

Reviewer: Burcin Erocal

Issue created by migration from https://trac.sagemath.org/ticket/9763

cousteaulecommandant commented 14 years ago

Attachment: trac_9763_add_metrology.patch.gz

Creates the metrology.py file, but doesn't edit the corresponding all.py

cousteaulecommandant commented 14 years ago

The metrology.py file itself, in case it's preferred

cousteaulecommandant commented 14 years ago

Attachment: metrology.py.gz

Example of usage, Pi theorem

cousteaulecommandant commented 14 years ago
comment:1

Attachment: example_metrology.sage.gz

burcin commented 13 years ago
comment:3

This needs more work as stated in the ticket description and seen by the fact that not all functions included have doctests.

I don't think it makes sense to have 2 different units modules. I suppose the goal here is to replace the existing one at some point. Is the functionality here a clear extension of the existing one? Is there anything that the current module does that this will not be able to do? (I never used the units module.)

Besides the questions of functionality, here are a few things I noticed when I read the patch:

burcin commented 13 years ago

Reviewer: Burcin Erocal

cousteaulecommandant commented 13 years ago
comment:4

As burcin said, the idea was to have this as a replacement for the current units module, without removing the old one. (Another idea was to call it units2, but that looked kind of ugly)

Making Units derive from SageObject might be a good idea; however I didn't know much about Sage (nor do I now, actually) so I didn't consider this possibility. Maybe a Units ring could be made too.

The U() function was called this way so one could create units conveniently, like

from metrology import U
length = U("1 m")

However, it's not necessary to have this function on the top level namespace, and Python guidelines usually suggest something like

import metrology
length = metrology.U("1 m")

As for advantages/disadvantages, well, I don't remember them all, but here's a rough list:

Pros

Cons

cousteaulecommandant commented 13 years ago
comment:5

(Apparently I forgot the space before each "*", resulting on a bad formatting. Since comments cannot be edited, I'll put the points again here for better readability)

Pros

Cons