# How to drop Infinity AND 'Indeterminate' elements from an equation?

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.

• So you mean something like eq/.s_Plus:>Select[s,FreeQ[λ]]? Commented Jan 8, 2023 at 0:07
• @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. Commented Jan 8, 2023 at 0:17
• It helps if you show what the desired output should be from the given input. Commented Jan 8, 2023 at 0:36
• @Nasser: -0.905818 is the result I obtain from the equation by manually dropping the infinity terms. Lukas's result is -0.859759. Commented Jan 8, 2023 at 0:42

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:

Impose the condition that $$\lambda$$ is zero

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


with the output

Observe that Mathematica yields Indeterminate and not Infinity

Then, we proceed as

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


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

• 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. Commented Jan 8, 2023 at 2:04
• @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 :-)
– bmf
Commented Jan 8, 2023 at 2:07
• Excellent! Thanks for your and Nasser's help. My model works very nicely generating the desired outputs. Commented Jan 8, 2023 at 2:40

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.]}    *)

(*    -0.905818    *)

• A very compact answer. Earlier, I did not understand the code but now I see the value. Thanks. Commented Jan 9, 2023 at 15:10
• 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. Commented Jan 11, 2023 at 23:56
• 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. Commented Jan 12, 2023 at 7:27
• 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? Commented Jan 12, 2023 at 17:23
• Try Union[Level[eq, {-2}]] but it depends a lot on your variables. Commented Jan 12, 2023 at 17:52

-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


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

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


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

• 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. Commented Jan 8, 2023 at 0:56
• @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. Commented Jan 8, 2023 at 0:59
• 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. Commented Jan 8, 2023 at 1:07

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`