6
$\begingroup$

I must admit that I don't think this problem is really new. However, I haven't found a post dealing exactly with this aspect of the issue.

I am doing calculation using Quantities, but I have problems dealing with dimensionless quantities. Suppose you define:

h = Quantity[b, "m"]

Now I would like to multiply h by a dimensionless parameter a, so to obtain the equivalent of the expression:

Quantity[a b, "m"]

Simply multiplying by a doesn't work (as you can check by trying to evaluate QuantitymMagnitude[a h]); this is understandable, since a is just an undefined symbol and as such has "unknown" units. I though, however, that multiplying by Quantity[a,"DimensionlessUnit"] would do the trick. Nope (same check).

The only solution I have found is to use

Quantity[a QuantityMagnitude[h], QuantityUnit[h]]

Rather involved... any idea or consideration in regard to this? Is there a profound reason I am not seeing for not supporting dimensionless symbols?

$\endgroup$
3
  • 1
    $\begingroup$ related mathematica.stackexchange.com/questions/16794/… $\endgroup$
    – george2079
    Commented Feb 25, 2015 at 0:20
  • 2
    $\begingroup$ You are right, that post is related (and maybe I should have mentioned it explicitly). However, the answer does not address my problem (it only deals with the output). $\endgroup$ Commented Feb 25, 2015 at 1:10
  • 1
    $\begingroup$ @GiacomoCiani I don't see a problem with Quantity[a, "DimensionlessUnit"]*h, since applying QuantityMagnitude gives a b m, the same as one gets with QuantityMagnitude[Quantity[a QuantityMagnitude[h], QuantityUnit[h]]]. The only difference is that applying QuantityUnit gives "DimensionlessUnit" "Meters", instead of just "Meters", but that can be reduced to 1 "m" by applying UnitConvert. So what am I missing? $\endgroup$
    – theorist
    Commented Feb 12, 2017 at 20:38

1 Answer 1

3
$\begingroup$

You could try something like the following:

(A) Create a replacement Rule:

rule=a_*b_Quantity:>  MapAt[#*a&,b,1]

and then apply this rule:

a*Quantity[b, "Meters"] /. rule
(* Quantity[a b, "Meters"] *)

or (B) live more dangerously and Unprotect Quantity. After this you can use TagSetDelayed:

Unprotect[Quantity];
Quantity /: Times[a : Except[_Quantity], b_Quantity] := MapAt[#*a &, b, 1]

and now you don't need the rule anymore.

(1/c) Quantity[b, "Volts"]
(* Quantity[b/c, "Volts"] *)

What are the risks of Unprotecting Quantity? I am not sure, but if you start getting strange results/errors after doing this you likely know the culprit.

$\endgroup$
1
  • $\begingroup$ This works! Actually, I have a rather complicated expression and I had to modify the rule in a_Symbol*b_Quantity:> MapAt[#*a&,b,1], or it would mess up the result. I haven't figured out why yet (on simple examples your rule seem to work even if a is a Quantity). $\endgroup$ Commented Feb 26, 2015 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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