Comments (2)
The primary intended workflow with QuantiPhy is:
- Use QuantiPhy to convert numbers with units and scale factors given as strings into Quantity objects where any scale factor provided has been evaluated with the resulting number being stored in its base units. So, Quantity('1 ns') is stored as 1e-9 's'.
- Perform any needed computation that involves these numbers. These computation generally result in the units being lost. For example:
>>> tstart = Quantity('25 ps')
>>> tstop = Quantity('1.025 ns')
>>> tdelta = tstop - tstart
>>> tdelta
1e-09
- Convert quantities to strings for output.
>>> print(Quantity(tdelta, 's'))
1 ns
So a fundamental assumption is that the except when reading and writing numbers, the value is always represented in its base units, in this case, seconds.
I don't really understand your motivation for wanting the value in picoseconds, so let me give two possible answers.
First I will assume that internally you need picoseconds in order to perform a calculation. In that case you can simply do the following:
>>> mytime = Quantity('1 ns')
>>> mytime_in_ps = 1e12 * mytime
Or, if you want the code to be more explicit, you can use the following:
>>> mytime_in_ps = mytime / Quantity('1 ps')
Notice that mytime_in_ps is represented as a float. You generally only want to use a QuantiPhy's quantity when representing numbers in their base units, otherwise you can end up with weird results like:
>>> print(Quantity(1000, units='ps'))
1 kps
That explains why I did not use the scale feature of QuantiPhy, as it would have ended up with a quantity with base units of 'ps'. You can do this if you want, but then you should avoid outputting numbers with scale factors. So for example, you can use:
>>> mytime = Quantity('1 ns', scale=(1e12, 'ps'))
>>> print(mytime)
1 kps
>>> mytime.real
1000
>>> mytime.units
'ps'
>>> print(mytime.fixed())
1000 ps
The scale factor is a scale by value rather than a scale to value, so it is simply multiplied by the specified number to get the final result. So in this case '1 ns' is converted to 1e-9 's', which is then multiplied by 1e12 to get the final result in 'ps'.
The other possibility that I can think of is that the internal computations are performed in seconds and you simply want to convert the results into picoseconds upon output. This is the more common scenario but it does not appear to be what you want. Anyway, just in case, here it is:
>>> mytime = Quantity('1 ns')
>>> print(mytime.render(scale=(1e12, 'ps')))
1 kps
In this case scale converts the result to picoseconds, but you should also suppress the use of SI scale factors:
>>> print(mytime.fixed(scale=(1e12, 'ps')))
1000 ps
>>> print(mytime.render(scale=(1e12, 'ps'), form='eng'))
1e3 ps
from quantiphy.
Hi @KenKundert ,
Many thanks for the excellent feedback!
I don't really understand your motivation for wanting the value in picoseconds,
Ah yes, sorry about that.
Basically, I started experting with some libraries for VCD files; one of the key parts of these files is the timescale statement:
$timescale 1ps $end
So, the timescale is specified as a string. Now, some libraries default to "ps" timescale, some to "ns" timescale - so if I want to convert from one to the other, and avoid resampling, I want to find out how many picoseconds is, say, "100 ns". So I throught - instead of me trying to write my own converter, I might as well try to see is there is some units library - and I found Quantiphy. And it works great for me in this case - I was just unclear on how things are supposed to work.
The scale factor is a scale by value rather than a scale to value, so it is simply multiplied by the specified number to get the final result
Excellent - thanks for this, this is the critical piece of information that I was missing. And thanks also for the great examples. I guess I can close this issue now ...
from quantiphy.
Related Issues (20)
- Scaling Problem when entering Quantity HOT 2
- Upload wheels to PyPI HOT 1
- Consider removing setup_requires from setup.py HOT 3
- Consider using mu (µ) character in render method HOT 8
- Consider permissive license? HOT 1
- prec='full' fails HOT 3
- translate to Russian Cyrillic HOT 5
- setup.py no longer lists test requirements HOT 2
- Question on use HOT 3
- add type hints to allow static code analysis using mypy HOT 4
- Added to conda-forge HOT 4
- Quantity returns the wrong value HOT 10
- meter is not recognized HOT 2
- Doc is not working HOT 2
- Adding Packages to Spack
- DeprecationWarning when running py.test HOT 2
- Adding new unicode character for micro: "µ" as an alternative to μ HOT 2
- Unitless scales HOT 4
- Custom radix HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quantiphy.