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).
I wrote up this code sample to test out the math here:
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?
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:
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:
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.