2
$\begingroup$

I have obtained the following form as a part of a computation:

k (Quantity[0, "Kilohertz"]) + Quantity[1/500, "Kilohertz"]

This is typeset as

Here, k is an atom (representing an integer in my computation). Obviously I would like this to simplify to Quantity[2, "Hertz"], but I cannot figure out how to cause the term with k to vanish. Neither FullSimplify nor UnitSimplify have any effect here:

My best guess is that Mathematica doesn't want to assume things about k, but given that the magnitude of the unit is 0, it should still simplify away.

How can I simplify this as I would expect?

$\endgroup$
3
  • $\begingroup$ try Simplify[....,Assumptions->Element[k,Reals]] $\endgroup$
    – george2079
    Apr 5, 2016 at 2:45
  • $\begingroup$ @george2079 no dice. Same effect as without the Assumptions -- the form is untouched. $\endgroup$ Apr 5, 2016 at 4:11
  • $\begingroup$ Related: (34967) $\endgroup$
    – Mr.Wizard
    Jun 8, 2017 at 2:12

2 Answers 2

3
$\begingroup$

You can do this:

Unprotect[Quantity]
Quantity /: Times[Quantity[mag_, unit_], seq__] := 
Quantity[Times[mag, seq], unit]

This will leads to your desired result, and also works for symbolic Times in Quantity

$\endgroup$
2
  • 1
    $\begingroup$ That's a good idea. Is it wise to edit builtin rules like Quantity? Also, would Quantity[0, __] := 0 be a better rule or worse? (I don't know general conventions, thanks.) $\endgroup$ Apr 5, 2016 at 5:23
  • $\begingroup$ @thirtythreeforty your idea also "edits" the built-in function (requires Unprotect[ Quantity]) . Which to do depends on your need. $\endgroup$
    – vapor
    Apr 5, 2016 at 5:40
0
$\begingroup$

Had the same problem and solved it by simply using a replacement rule.

k (Quantity[0, "Kilohertz"]) + Quantity[1/500, "Kilohertz"]

UnitConvert[Out[1] /. Quantity[0, "Kilohertz"] -> 0, "Hertz"]

Here's what it looks like in a notebook:

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ People here generally like users to post code as Mathematica code instead of images or TeX, so they can copy-paste it. It makes it convenient. $\endgroup$
    – user9660
    Jun 5, 2016 at 8:35
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Jun 5, 2016 at 8:35
  • $\begingroup$ Louis, thanks for the formatting suggestion. $\endgroup$
    – T. Zilla
    Jun 5, 2016 at 17:37

Your Answer

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

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