juhasch / physicalquantities Goto Github PK
View Code? Open in Web Editor NEWA Python module to allow calculations using physical quantities
License: Other
A Python module to allow calculations using physical quantities
License: Other
Comparisons like <
, >
, and ==
are not implemented.
a = [1,2,3] * pq.Q(1,'m')
a[1] = a[2]
a
returns [1, 3.0 m, 3] m
Does not work with Python3 and Partly Python2
1/s
displayed as s/1
because of wrong division function ?Hello,
Your work is very useful to me. Thank you.
After reading the docs I continue to don't understand how to define more units.
Please help me to define new physical quantities.
For instance 1 L (0.001 m^3):
it gives an error:
36 add_composite_unit('L', '0.001*m**3','Litre')
NameError: name 'Litre' is not defined
I understand it, but where to define Litre?
thank you very much in advance
Filipe
Now:
In [55]: a = 1m
In [56]: a.
a.Gm a.any_to a.convert a.km a.pm a.to
a.Mm a.autoscale a.copy a.m a.sin a.um
a.Tm a.base a.cos a.mm a.sqrt
a.am a.cm a.fm a.nm a.tan
A = 100 cm**2
-> A= 10000 cm^2
but
A= 100 m**2
-> A = 100 m^2
When trying to convert between incompatible units one would expect UnitError
to be raised, instead e.g. PhysicalQuantity(1, 'm').to('kg')
raises AttributeError("PhysicalUnit instance has no attribute 'unit'")
. This is caused by line 453 in Unit.py, introduced by b1f54a1.
>>> PhysicalQuantity(1, 'm').to('kg')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/PhysicalQuantities/Quantity.py", line 565, in to
value = convertvalue(self.value, self.unit, unit)
File "/usr/lib/python2.7/site-packages/PhysicalQuantities/Unit.py", line 58, in convertvalue
(factor, offset) = src_unit.conversion_tuple_to(target_unit)
File "/usr/lib/python2.7/site-packages/PhysicalQuantities/Unit.py", line 453, in conversion_tuple_to
raise UnitError('Incompatible unit for conversion %s' % other.unit)
AttributeError: PhysicalUnit instance has no attribute 'unit'
@juhasch, how about just going back to raise UnitError('Incompatible units')
as in e10ab54?
!uname -a
Darwin Mac132 18.7.0 Darwin Kernel Version 18.7.0: Mon Feb 10 21:08:45 PST 2020; root:xnu-4903.278.28~1/RELEASE_X86_64 x86_64
pip_freeze.txt
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-726c0454b5bc> in <module>
----> 1 get_ipython().run_line_magic('load_ext', 'PhysicalQuantities.ipython')
2 import PhysicalQuantities.constants as q
3 import PhysicalQuantities.imperial
4 from IPython.display import display, Math, Latex
5 from sympy import *
/Applications/SageMath/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
2315 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2316 with self.builtin_trap:
-> 2317 result = fn(*args, **kwargs)
2318 return result
2319
<decorator-gen-64> in load_ext(self, module_str)
/Applications/SageMath/local/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
185 # but it's overkill for just that one bit of state.
186 def magic_deco(arg):
--> 187 call = lambda f, *a, **k: f(*a, **k)
188
189 if callable(arg):
/Applications/SageMath/local/lib/python3.7/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)
31 if not module_str:
32 raise UsageError('Missing module name.')
---> 33 res = self.shell.extension_manager.load_extension(module_str)
34
35 if res == 'already loaded':
/Applications/SageMath/local/lib/python3.7/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
85 dir=compress_user(self.ipython_extension_dir)))
86 mod = sys.modules[module_str]
---> 87 if self._call_load_ipython_extension(mod):
88 self.loaded.add(module_str)
89 else:
/Applications/SageMath/local/lib/python3.7/site-packages/IPython/core/extensions.py in _call_load_ipython_extension(self, mod)
132 def _call_load_ipython_extension(self, mod):
133 if hasattr(mod, 'load_ipython_extension'):
--> 134 mod.load_ipython_extension(self.shell)
135 return True
136
/Applications/SageMath/local/lib/python3.7/site-packages/PhysicalQuantities/ipython.py in load_ipython_extension(ip)
62 global __transformer, within_comment
63 within_comment = False
---> 64 ip.input_transformer_manager.logical_line_transforms.insert(0, __transformer)
65 ip.user_ns['pq'] = PhysicalQuantities.q
66
AttributeError: 'TransformerManager' object has no attribute 'logical_line_transforms'
a = pq.Q(1, 's**3*m**2')
leads to:
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
a.to('1/m)'
-> m/1
is displayed, although Unit should be the same
a = 1 / 1 m
b = a.to('1/m')
print a,b
print a.unit.names, b.unit.names
> 1.0 1/m 1.0 m/1
> {'m': -1} {'1': -1, 'm': 1}
I don't think comparison is working.
For example, if I run the following code:
def test_comparison():
a = pq.Q(1e-3, 'm')
b = pq.Q(1, 'mm')
return(a == b)
test_comparison()
I get
False
I am using pq.version 0.2 in iPython 2.2.0.
I can get what I want by doing:
return(a.base.value == b.base.value)
With PhysicalQuantities-0.9.1
in IPython, I get:
>>> (-0.00032C).autoscale
/home/foo/.local/lib/python3.6/site-packages/PhysicalQuantities/quantity.py:580: RuntimeWarning: invalid value encountered in log10
n = np.log10(b.value)
-0.00032 C
Hi,
The link for the documentation notebook:
http://nbviewer.ipython.org/github/juhasch/PhysicalQuantities/blob/master/doc/Index.ipynb
is redirected to:
nbviewer.jupyter.org/github/juhasch/PhysicalQuantities/blob/master/doc/Index.ipynb
But, this page apparently does not exist (404 error).
Is there some other way to access this documentation?
Thanks,
David
The package appears to be hosted on PyPi here:
https://pypi.python.org/pypi/physicalquantities/0.6.
However, attempting to install with pip
fails:
pip install --user PhysicalQuantities
Full log:
------------------------------------------------------------
/usr/bin/pip run on Wed Jan 27 22:18:28 2016
Downloading/unpacking PhysicalQuantities
Getting page https://pypi.python.org/simple/PhysicalQuantities/
URLs to search for versions for PhysicalQuantities:
* https://pypi.python.org/simple/PhysicalQuantities/
Analyzing links from page https://pypi.python.org/simple/physicalquantities/
Could not find any downloads that satisfy the requirement PhysicalQuantities
Cleaning up...
No distributions at all found for PhysicalQuantities
Exception information:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 290, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1178, in prepare_files
url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
File "/usr/lib/python2.7/dist-packages/pip/index.py", line 277, in find_requirement
raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for PhysicalQuantities
Thanks for the cool library!
It would be nice to have a function that can parse a PhysicalQuantity from a string.
For usage outside ipython, it would be nice if this was available under a short name, such as the already existing q()
.
A quick mock-up which just splits at whitespace:
from PhysicalQuantities import PhysicalQuantity, _Quantity, q
_Quantity.__call__ = lambda s, x: PhysicalQuantity(float(x.split()[0]), x.split()[1])
print(q("1 m") / q("1 s")) # gives 1.0 m/s
It would also be useful for parsing string data loaded from files or input by user.
2 m**2
gets expanded to 4 m^2
instead of 2 m^2
xQ = np.array([1, 3, 0]) * 1 m
xq = np.array([3, 2, 0]) * 1 m
ra=np.linalg.norm(xQ-xq)
ra**3
TypeError: unsupported operand type(s) for *: 'int' and 'dict'
The line below throws out an error because it's trying to use pi as a unit, and it is not using np.pi as you've changed others to.
addunit('Bohr', '4*pi*eps0*hbar**2/me/e0**2', 'Bohr radius')
Error:
NameError: name 'pi' is not defined
Also, it looks like hartree is the the same.
a=0 m
b = 0 dB
Result:
a = 0 m ; b =
Don't blindly assume Sympy is installed.
sqrt(1 m**2)
returns TypeError: unorderable types: PhysicalQuantity() < int()
4 m**-1
returns 0.25 1/m
instead of 4 1/m
.
To be able to set individual elements of a numpy array or a list
setitem needs to be defined.
With PhysicalQuantities-0.9.1
in IPython, I get:
>>> 8.85e-12F/1m
8.85e-12 F*m
Still not solved:
2 m2 -> 2 m^2
2 mm2 -> 4 mm^2
Make it easier to get real/imag part of a complex number with unit:
a = (1 + 1j) * 1 Ohm
a.real
Seems overriding np.sqrt creates problems with numba.
use dBQuantity(10*np.log10(3),'dB',islog=True)
instead of dBUnit(10*np.log10(x),'dB',islog=True)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.