2
$\begingroup$

I'm having trouble with numerically evaluating some functions. The problem essentially comes down to

Log[Quantity[3, "Meters"]] - Log[Quantity[2, "Kilometers"]]

The above code doesn't evaluate to a numerical answer even though it should since $$ \log(3\ {\rm m}) - \log(2\ {\rm km}) = \log \left(\frac{3\ {\rm m}}{2\ {\rm km}} \right)=\log\left(\frac 3{2000}\right)$$ Is there a way to get mathematica to automatically combine the logs of unitful quantities?

EDIT:

Note the following:

Log[ Quantity[3, "Meters" ] / Quantity[2, "Kilometers"] ]
(* -Log[2000/3] *)

Simplify[ Log[a] - Log[b], b > 0]
(* Log[a/b] *)

Simplify[ Log[ Quantity[3, "Meters"] ] - Log[ Quantity[2, "Kilometers"] ] ]
(* Log[ 3 m ] - Log[ 2 km ] *)
$\endgroup$
6
  • $\begingroup$ Log[QuantityMagnitude@Quantity[3, "Meters"]] - Log[QuantityMagnitude@Quantity[2, "Kilometers"]] // N could be a workaround. $\endgroup$
    – Syed
    Commented Nov 23, 2021 at 5:37
  • 3
    $\begingroup$ @Syed - You need a UnitConvert, i.e., Log[QuantityMagnitude@Quantity[3, "Meters"]] - Log[QuantityMagnitude@UnitConvert@Quantity[2, "Kilometers"]] // N $\endgroup$
    – Bob Hanlon
    Commented Nov 23, 2021 at 5:46
  • $\begingroup$ Log[Quantity[3, "Meters"]] - Log[Quantity[2, "Kilometers"]] /. Log[a_] - Log[b_] :> Log[a/b] $\endgroup$
    – Bob Hanlon
    Commented Nov 23, 2021 at 5:51
  • $\begingroup$ Completely missed that units were different. Thanks @BobHanlon $\endgroup$
    – Syed
    Commented Nov 23, 2021 at 5:53
  • 1
    $\begingroup$ Log[ Quantity[3, "Meters"] ] is undefined. The first example is unrelated because Quantity[3, "Meters"]/Quantity[2, "Kilometers"] evaluates to a number. So the question is comparing the logarithm of a number with logarithms of distances. $\endgroup$
    – Michael E2
    Commented Apr 30, 2022 at 22:02

0

Browse other questions tagged or ask your own question.