5
$\begingroup$

This is a simple question but somehow after trying Delete, and Drop I still cannot get what I want. Given the following equation,

eq = (-0.818158 + 
0.725806 Log[0.683526 λ[1., 2.]] λ[1., 2.] + 
0.774194 Log[0.744189 λ[2., 1.]] λ[2., 1.] + 
0.774194 Log[0.744189 λ[2., 3.]] λ[2., 
 3.])/(0.951613 - 0.725806 (0.0444444 + λ[1., 2.]) - 
0.774194 (0.0208333 + λ[2., 1.] + λ[2., 3.]));

I want to drop from this equation all the terms that involve Lambda. (A term is an element between two arithmetic signs like + and -.) because the Lambda is equal to zero and taking Log[0] is infinite. So, I like to get rid of Infinity and have a real number as a solution.

Generally speaking, given a list of very large equations, I like to drop Lambda terms with zero from all the equations.

I think it is easier to drop all the terms that generate infinity.

$\endgroup$
4
  • 1
    $\begingroup$ So you mean something like eq/.s_Plus:>Select[s,FreeQ[λ]]? $\endgroup$
    – Lukas Lang
    Commented Jan 8, 2023 at 0:07
  • $\begingroup$ @Lukas Lang: Somehow your result is not equal to the result I obtain by simply dropping the lambda terms manually. I do not really know why? Is it possible to show the term remaining after dropping the lambda terms. This will let me know why I get two different results. $\endgroup$ Commented Jan 8, 2023 at 0:17
  • 1
    $\begingroup$ It helps if you show what the desired output should be from the given input. $\endgroup$
    – Nasser
    Commented Jan 8, 2023 at 0:36
  • $\begingroup$ @Nasser: -0.905818 is the result I obtain from the equation by manually dropping the infinity terms. Lukas's result is -0.859759. $\endgroup$ Commented Jan 8, 2023 at 0:42

4 Answers 4

6
$\begingroup$

I don't think that there's anything wrong with the answer by @Nasser and I have upvoted, but since there are some complains below is an alternative.

Logic:

  1. Split the equation to a List
  2. Evaluate all the terms of the aforementioned List for the value of $\lambda$ that causes the infinity
  3. Drop infinities
  4. Sum the elements

It works roughly like this:

With

eq = (-0.818158 + 
     0.725806 Log[0.683526 λ[1., 2.]] λ[1., 2.] + 
     0.774194 Log[0.744189 λ[2., 1.]] λ[2., 1.] + 
     0.774194 Log[0.744189 λ[2., 3.]] λ[2., 
       3.])/(0.951613 - 0.725806 (0.0444444 + λ[1., 2.]) - 
     0.774194 (0.0208333 + λ[2., 1.] + λ[2., 3.]));

Create the list

List @@ (eq // Rationalize // Expand)

output is:

list1

Impose the condition that $\lambda$ is zero

List @@ (eq // Rationalize // Expand) /. λ[_, _] -> 0

with the output

list2

Observe that Mathematica yields Indeterminate and not Infinity

Then, we proceed as

Select[List @@ (eq // Rationalize // Expand) /. λ[_, _] -> 0, 
 FreeQ[Indeterminate]]

res

If there were more finite numerical elements you could do a Total

Addendum: demonstrating what I mentioned at the end.

Consider the following equation, which is the original with the modification that I added a 3

eq = (-0.818158 + 
     0.725806 Log[0.683526 λ[1., 2.]] λ[1., 2.] + 
     0.774194 Log[0.744189 λ[2., 1.]] λ[2., 1.] + 
     0.774194 Log[0.744189 λ[2., 3.]] λ[2., 
       3.])/(0.951613 - 0.725806 (0.0444444 + λ[1., 2.]) - 
     0.774194 (0.0208333 + λ[2., 1.] + λ[2., 3.])) + 3;

Then

Total@(Select[
   List @@ (eq // Rationalize // Expand) /. λ[_, _] -> 0, 
   FreeQ[Indeterminate]])

gives back

res2

$\endgroup$
3
  • $\begingroup$ Yes, the process you described is what I was thinking but could not code it. That is exactly what I wanted to obtain. I noticed Indeterminate terms which I wanted to drop as well. So, your answer works well for me. I did not mean that Naser's answer is not good. It was too complicated for me to follow. I can follow your answer better. $\endgroup$ Commented Jan 8, 2023 at 2:04
  • $\begingroup$ @TugrulTemel glad I was able to help :-) Regarding the answer by Nasser, I just added the comment for clarity, in the sense that what he suggested does not break down, or cause any issues because I tried it. That was all :-) $\endgroup$
    – bmf
    Commented Jan 8, 2023 at 2:07
  • 1
    $\begingroup$ Excellent! Thanks for your and Nasser's help. My model works very nicely generating the desired outputs. $\endgroup$ Commented Jan 8, 2023 at 2:40
9
$\begingroup$

Are you looking for this limit?

Limit[eq, {λ[1., 2.] -> 0, λ[2., 1.] -> 0, λ[2., 3.] -> 0}]
(*    -0.905818    *)

With automatic generation of the list of $\lambda$ variables:

Λ = Union[Cases[eq, λ[___], ∞]]
(*    {λ[1., 2.], λ[2., 1.], λ[2., 3.]}    *)

Limit[eq, Thread[Λ -> 0]]
(*    -0.905818    *)
$\endgroup$
5
  • $\begingroup$ A very compact answer. Earlier, I did not understand the code but now I see the value. Thanks. $\endgroup$ Commented Jan 9, 2023 at 15:10
  • $\begingroup$ I think your answer above is not a general solution to my question because in your formulation Log[a lambda] will go to -Infinity; however, I want to prevent this to happen and hence to drop it from the equation. Basically, I want to drop all Indeterminate and Infinity terms from the equation because zero terms are irrelevant in my equation. $\endgroup$ Commented Jan 11, 2023 at 23:56
  • 1
    $\begingroup$ I was assuming that you are simply taking the limit of all $\lambda$ values going to zero, and making use of the limit $\lim_{x\to0}x\ln(x)=0$. This limit expresses your intent: the zero is “stronger” than the infinity and their product tends to zero. $\endgroup$
    – Roman
    Commented Jan 12, 2023 at 7:27
  • $\begingroup$ Thanks for the clarification. I tried your Code with lamda and game and alpha simultaneously`, but cannot get it right. Is there a way to expand the list of zero-parameters to include others in a one-line code? $\endgroup$ Commented Jan 12, 2023 at 17:23
  • 1
    $\begingroup$ Try Union[Level[eq, {-2}]] but it depends a lot on your variables. $\endgroup$
    – Roman
    Commented Jan 12, 2023 at 17:52
6
$\begingroup$

-0.905818 is the result I obtain from the equation by manually dropping the infinity terms.

In this case you could try

eq /. Log[_ * λ[_, _]] -> 1 /. λ[_, _] -> 0

Mathematica graphics

But if you want to keep the λ, and only remove the log terms, you can try

eq /. Log[_ * λ[_, _]] -> 1

Mathematica graphics

I do not know if this will work for all your expressions. I only tried it on the one you gave.

$\endgroup$
3
  • $\begingroup$ Your answer is good but not the solution I am after. My main problem is that some terms in the equation are infinite due to several reasons: either Log[0] or a term divided by zero. Instead of struggling to find those terms with Lamda, I simply drop the terms that lead to infinity. I think if I can drop the terms that lead to infinity, it will be an answer to my question. $\endgroup$ Commented Jan 8, 2023 at 0:56
  • $\begingroup$ @TugrulTemel may be you could then give additional examples of expressions and desired output of each in order to be able to come up with a general solution that works for all. Right now you only have one example and hard to make a general solution from that one example only. $\endgroup$
    – Nasser
    Commented Jan 8, 2023 at 0:59
  • $\begingroup$ Dropping all terms that involve infinity is a general solution. It really does not matter which example I give because the process of getting infinite is not relevant. The only relevant information is the fact that I end up with infinite terms, which I want to drop from the equation. $\endgroup$ Commented Jan 8, 2023 at 1:07
6
$\begingroup$

Here's a version of my original suggestion that gives the desired result:

Replace[eq, s : Except[_?AtomQ, _Plus] :> Select[s, FreeQ[λ]], All]
(* -0.905818 *)

The original version used ReplaceAll instead of Replace[...,All], which resulted in the λ terms being deleted at the outermost level, rather than at the innermost level (see also this question).

The other trick that is required is Except[_?AtomQ,_Plus] instead of simply _Plus. This is needed because _Plus also matches atoms such as 1 due to the OneIdentity attribute of Plus, which then breaks Select

$\endgroup$

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.