3
$\begingroup$

In Mathematica numerical terms collect when their variables are equal.

.2 "SERC" + .7 "SERC"
(* 0.9 "SERC" *)

However, numerical Quantity objects do not collect.

Quantity[.2, "Megawatts"] "SERC" + Quantity[.7, "Megawatts"] "SERC"
(* "SERC" (Quantity[0.2, "Megawatts"]) + "SERC" (Quantity[0.7, "Megawatts"]) *)

I would expect these to collect into Quantity[.9, "Megawatts"] "SERC" but they do not. How can I make Quantity objects collect? My understanding is that they should collect in this instance.

$\endgroup$

1 Answer 1

5
$\begingroup$

This is weird behavior on the face of it. You can force them to collect explicitly:

Collect[Quantity[.2, "Megawatts"] "SERC" + Quantity[.7, "Megawatts"] "SERC", "SERC"]

(* Out: "SERC" Quantity[0.9, "Megawatts"] *)

... or you can use Simplify:

Simplify[Quantity[.2, "Megawatts"] "SERC" + Quantity[.7, "Megawatts"] "SERC"]

(*Out: SERC Quantity[0.9, "Megawatts"] *)

The latter will also make the necessary unit conversions:

Simplify[Quantity[2, "Megawatts"] "SERC" + Quantity[700, "Kilowatts"] "SERC"]

(* Out: "SERC" Quantity[2700, "Kilowatts"] *)
$\endgroup$
1
  • $\begingroup$ Collect seems to be a good fit (+1). $\endgroup$
    – Edmund
    Commented Jun 29, 2016 at 17:27

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.