Giter Site home page Giter Site logo

Comments (2)

KenKundert avatar KenKundert commented on May 23, 2024

The primary intended workflow with QuantiPhy is:

  1. 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'.
  2. 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
  1. 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.

sdbbs avatar sdbbs commented on May 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.