Hi,
Clojure novice here.
Macbook 2.4ghz/4gram/osx10.5.4/java1.6
Clojure from svn last week
Jython 2.5a3
I've been exploring using the
jscience.org reference implementation of
JSR-275 to provide physical quantities support in Clojure. Generally,
things are working:
(import '(org.jscience.physics.amount Amount AmountFormat))
(import '(javax.measure.unit AlternateUnit BaseUnit CompoundUnit
DerivedUnit
Dimension NonSI ProductUnit SI SystemOfUnits
TransformedUnit Unit UnitFormat))
(import '(javax.measure Measure))
;;; Create an Amount by parsing string
(def amount1 (. Amount valueOf "234 mA"))
(def amount2 (. Amount valueOf "10 m"))
(def amt3 (. Amount valueOf "3 "))
;;; Create an Amount or Measure from long or double and a Unit
(def amount3 (. Amount valueOf 12.3 (. SI METER)))
(def amount4 (. Amount valueOf 100.0 (. NonSI POUND)))
(def amt-e2 (. Amount valueOf (long 33) (. NonSI FOOT)))
(def m1 (. Measure valueOf 100.0 (. NonSI POUND)))
(def m2 (. Measure valueOf 100 (. NonSI POUND)))
;;; Extract long or double value from a Measure with Unit conversion
(.doubleValue m1 (. SI KILOGRAM))
(.longValue m1 (. SI KILOGRAM))
(.doubleValue m2 (. SI KILOGRAM))
(.longValue m2 (. SI KILOGRAM))
;;; Chained access to nested stuff
(class (.. amt-e2 getUnit getDimension))
;;; Testing exactness ...
.isExact amt-e3)
(.getExactValue amt-e3)
(.isExact (.to amt-e3 (. SI METER)))
;;; Dividing and multiplying Amounts by Amounts
(.times amount4 amt3)
(def amt-e3 (.times (.divide amt-e2 (. Amount valueOf "11 ")) (.
Amount valueOf "2 ")))
;;; Dividing Amounts by long or double
(.divide amt-e3 33.2) ; Works (looks doubleish?)
(.divide amt-e3 33) ; Doesn't work (insufficiently
longish?)
java.lang.IllegalArgumentException: No matching method found: divide
java.lang.IllegalArgumentException: No matching method found: divide
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:71)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at user.eval__2453.invoke(Unknown Source)
at clojure.lang.Compiler.eval(Compiler.java:3850)
at clojure.lang.Repl.main(Repl.java:75)
(.divide amt-e3 (long 33)) ; Works
(.divide amt-e3 (double 33.2)) ; Works
;;; Multiplying Amounts by long or double
; Doesn't find appropriate overloaded method
(.times amt-e3 (double 12.3))
(.times amt-e3 12.3)
java.lang.ClassCastException: java.lang.Double cannot be cast to
org.jscience.physics.amount.Amount
java.lang.ClassCastException: java.lang.Double cannot be cast to
org.jscience.physics.amount.Amount
at org.jscience.physics.amount.Amount.times(Amount.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:82)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at user.eval__2381.invoke(Unknown Source)
at clojure.lang.Compiler.eval(Compiler.java:3850)
at clojure.lang.Repl.main(Repl.java:75)
=============================================================
Using jython all types of parameters work for both divide and times
=============================================================
>>> from org.jscience.physics.amount import Amount
>>> amt1 = Amount.valueOf("3.2 ft")
>>> amt2 = Amount.valueOf("23 ft")
>>> amt3 = Amount.valueOf("3 ")
>>> amt1.divide(amt2)
>>> amt1.divide(amt3)
>>> amt.divide(3.2)
>>> amt1.divide(3)
>>> amt1.times(amt2)
>>> amt1.times(amt3)
>>> amt2.times(3)
>>> amt1.times(3.2)
Anyone have a clue to offer me?