views:

173

answers:

3

Hi all,

I need to represent the unit of Percent per second using the JScience.org's JSR 275 units and measures implementation. I am trying to do to the following:

Unit<Dimensionless> PERCENT_PER_SECOND = NonSI.PERCENT.divide(Si.SECOND).asType(Dimensionless.class)

but I am getting a ClassCastException when I try to do that.

The following works, but I'm not sure if there's a better way:

public interface PercentOverTime extends Quantity {}
public static Unit<PercentOverTime> PERCENT_PER_SECOND = new BaseUnit<PercentOverTime>("%/s");

Any thoughts? The closest I could find to this is the question on Cooking Measurements (which is how I saw how to define your own units).

A: 

Percent isn't a unit, but a scalar - so percent per second is only a scalar value per unit time, which doesn't make sense. It's like saying "3 per second". 3 what?

If you incorporate the unit of what you are measuring per unit time that will get you the correct unit.

mdma
I agree that it doesn't make much sense but that is the unit specified within the technical spec I am implementing. Perhaps I need to get that changed.
I82Much
I agree - it makes sense to have that as a unit you can later combine. But it's not dimensionless, so I'd look there. (I don't know the API - but I've worked with SI units.)
mdma
OK that explains it. I guess dimensionless / time != dimensionless; probably have to create a new quantity like I did.
I82Much
+2  A: 

It has units s^-1, or Hz (SI.HERTZ in JScience speak).

Or Unit<Frequency>.

spong
+1  A: 

I wrote up this code sample to test out the math here:

public void testUnit() {
    // define two points on a function from t -> %
    // the rate of change between these two points
    // should have unit %/t
    Measure<Double, Dimensionless> p0 = Measure.valueOf(50.0, NonSI.PERCENT);
    Measure<Double, Dimensionless> p1 = Measure.valueOf(20.0, NonSI.PERCENT);

    Measure<Double, Duration> timeDifference = Measure.valueOf(10.0, SI.SECOND);

    // JSR-275 has no Amount, so have to convert and do math ourselves
    // these doubles are percents
    double p0Raw = p0.doubleValue(NonSI.PERCENT);
    double p1Raw = p1.doubleValue(NonSI.PERCENT);

    // this duration is in seconds
    double timeDifferenceRaw = timeDifference.doubleValue(SI.SECOND);

    // this is the slope of the secant between the two points
    // so it should be the %/s we want
    double rateSecant = (p1Raw - p0Raw) / timeDifferenceRaw;

    // let's see what we get
    Measure<Double, ?> answer = Measure.valueOf(rateSecant,
                                                NonSI.PERCENT.divide(SI.SECOND));
    System.out.println(answer);
}

If your original function has time as the independent variable (e.g. as seconds) and a ratio as the independent variable (e.g. as a percent), then the derivative of this function with regard to time will still have time as the independent variable, but will have 'ratio per time' as the dependent.

Yes, ratios are dimensionless, so this is a little bit odd, but you could imagine a graph of the percent change day over day in a stock price and then a graph of the change in the percent change in a stock price day over day day over day.

So what does this print out?

-3.0 %/s

Which is what we expect the rate of change to be for a change from 50 to 20 percent over 10 seconds.

So your unit construction should look like:

    Unit<?> magicUnit = NonSI.PERCENT.divide(SI.SECOND);
    Dimension magicDimension = Dimension.NONE.divide(Dimension.TIME);

    System.out.println(magicUnit + " measures " + magicDimension + " ("
            + magicUnit.getDimension() + ")");

Indeed this prints %/s measures 1/[T] (1/[T]), as we expect.

So we have a Unit and Dimension and can make Measures. What is the Quantity we are measuring? The docs say this about Quantity:

Distinct quantities have usually different physical dimensions; although it is not required nor necessary, for example Torque and Energy have same dimension but are of different nature (vector for torque, scalar for energy).

So while Frequency would seem to be the correct Quantity, it doesn't really express the semantic quantity we seem to be discussing.

In closing, your first line of code doesn't work because in the included model 1/[T] measures the quantity Freqency, not the quantity Dimensionless. So if you don't want to make your own Quantity, use Unit. The Dimension you are looking for is None/Time, or %/second if you want the correct scalar multipliers in there. Finally, it's up to you whether you want to make your own Quantity, but it might be worthwhile if you're using this in a lot of places.

It would also be worthwhile to check out the latest developments in the JScience space since it seems like they decided Amount (with add, subtract, multiply, divide, pow, etc. methods) was needed. It would be really easy to do all this dimensional analysis with Amount. Just do a Percent Amount minus a Percent Amount and divide by a Seconds amount and it should do the units for you.

jasonmp85
Thank you for the in depth response
I82Much