Skip to main content

simplifying expressions - Log of products to sum of logs


I have a log of products: $$\log \left(\prod _{i=1}^n \left( g(a(i,i)) \prod _{k=1}^i f(a(i,k))\right)\right)$$


That I turn into a sum of logs (I know everything involved is nice enough): $$\sum _{i=1}^n \left(\sum _{k=1}^i \log (f(a(i,k)))+\log (g(a(i,i)))\right)$$


I had to use a custom rule for this:


Log[Product[g[a[i, i]] Product[f[a[i, k]], {k, 1, i}], {i, 1, n}]] //.
{Log[Product[expr_, range_]] :> Sum[Log[expr], range],

Log[Times[a_, b_]] :> Log[a] + Log[b],
Log[Power[a_, n_]] :> n Log[a]}

Some observations:




FullSimplify does nothing, even with assumptions:


FullSimplify[
Log[Product[g[a[i, i]] Product[f[a[i, k]], {k, 1, i}], {i, 1, n}]],
{a[_, _] > 0 && g[_] > 0 && a[_, _] > 0 && f[_] > 0}]




Not even with penalty for Product does it do anything:


FullSimplify[
Log[Product[a[i], {i, 1, n}]],
a[_] > 0,
ComplexityFunction ->
(20 Count[#, _Product, Infinity] + LeafCount[#] &)]



But for Times it works:



FullSimplify[
Log[a b c],
a > 0 && b > 0 && c > 0,
ComplexityFunction ->
(20 Count[#, _Times, Infinity] + LeafCount[#] &)]
(* Log[a] + Log[b] + Log[c] *)

Unless I use a[i]


FullSimplify[
Log[a[1] a[2] a[3] ],

a[_] > 0 , (* a[1]>0 && a[2]>0 && a[3]>0 works *)
ComplexityFunction -> (20 Count[#, _Times, Infinity] + LeafCount[#] &)]



Is there a more general way to do this that does not rely on making up a bunch of manual transformations?



Answer



I don't think there is any in-built way of doing this with Mathematica using FullSimplify etc, other than as you have suggested. These issues arise quite often when solving symbolic maximum likelihood estimator problems, so we added this functionality years ago into the mathStatica add-on for Mathematica, which is done by activating SuperLog, which enhances Mathematica's Log function to make such conversions automatically. Since your example is a bit more unusual than the norm, I thought I'd just try it out ... Here's the syntax:


enter image description here


A more typical problem would be something like:


enter image description here



Comments