0
$\begingroup$

I have the following expression:

x Quantity[y, ("Milliwatts"*"Seconds"^2)/"Joules"]

If I try to convert this to the SI unit, I would expect to get

Quantity[(x y)/1000, "Seconds"]

but it in fact returns unevaluated:

UnitConvert[x Quantity[y, ("Milliwatts"*"Seconds"^2)/"Joules"]]

From what I can tell, UnitConvert only works with Quantity expressions, and Mathematica is reluctant to treat x as dimensionless and multiply it inside the Quantity as part of the Quantity's value.

However, the following works no better:

UnitConvert[Quantity[x, "DimensionlessUnit"] Quantity[y, ("Milliwatts"*"Seconds"^2)/"Joules"]]

I could do a replacement rule like this:

Quantity[x_, "DimensionlessUnit"] Quantity[y_, z_] :> Quantity[x y, z]

which appears to do the job, but I don't see why it's necessary. It also seems like a rather crude solution.

I should mention that this is Mathematica 9.0.1, and I understand that Units have been somewhat overhauled in Mathematica 10, but that's not really an option for me at the moment.

Is it possible to force Quantity to include prefactors and assume they are numeric quantities?

$\endgroup$
1
  • $\begingroup$ Not that I am aware of, but try UnitConvert /@ (x Quantity[ y, ("Milliwatts"*"Seconds"^2)/"Joules"]) /. UnitConvert[a_] -> a $\endgroup$
    – Jinxed
    Commented Mar 9, 2015 at 8:08

1 Answer 1

1
$\begingroup$

Easiest and straightforward may be to define a unit conversion function like so:

uc[expr_, targetunit___]:=UnitConvert[#,targetunit]&/@expr/.UnitConvert[a_,b___]->a

Usage:

uc[x Quantity[y, ("Milliwatts"*"Seconds"^2)/"Joules"]]
(* x (Quantity[y/1000, "Seconds"]) *)

or giving a desired target unit:

uc[x Quantity[y, ("Milliwatts"*"Seconds"^2)/"Joules"], "Hours"]
(* x (Quantity[y/3600000, "Hours"]) *)
$\endgroup$
2
  • $\begingroup$ Thanks for the answer, and it works beautifully. I have a related but distinct question that I will post shortly. $\endgroup$
    – Widjet
    Commented Mar 11, 2015 at 3:08
  • $\begingroup$ @Widjet: You are most welcome! $\endgroup$
    – Jinxed
    Commented Mar 11, 2015 at 6:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.