1
$\begingroup$

I'm creating a plot of a family of lines using their endpoints and plotting it like so:

Graphics[Line[Table[{{x1, y1}, {x2, y2}}, {u, 0, 2 \[Pi], ustep}]]]

Where x1, y1, x2 and y2 have previously been defined in terms of u. The problem is that it is possible for this definition to produce an infinite result for a single pair of x and y. When this infinity is encountered, Mathematica complains and none of the lines are actually displayed. I'd like Mathematica to just glaze over the invalid one and display all the rest.

More details:

  • There is a condition on u which could be used to find the problem point. (Let's just say $f(u) = 0$ for simplicity and generality.)
  • I don't explicitly know a priori where this undefined value will occur in the table (or even that it will be present at all).

Is there any way to tell Mathematica to skip Table values based on some rule?

$\endgroup$
7
  • $\begingroup$ There is; it is called Pattern matching $\endgroup$
    – Sektor
    Commented Oct 22, 2014 at 4:48
  • $\begingroup$ I don't understand where/why you get infinity. Can you provide a small numerical example where this happens? I think that would help in order to give you a good answer. $\endgroup$
    – mickep
    Commented Oct 22, 2014 at 5:10
  • $\begingroup$ @mickep Take for example x1 = 1/u. When u=0, x1 is infinite. $\endgroup$
    – Roger Burt
    Commented Oct 22, 2014 at 5:15
  • $\begingroup$ Is there any way to tell Mathematica to skip Table values based on some rule? how about DeleteCases ? Example: DeleteCases[{1, 2, Infinity, 3, 4}, Infinity] gives {1, 2, 3, 4} or DeleteCases[{1, 2, Infinity, 3, 4, 1/0}, Infinity | ComplexInfinity] $\endgroup$
    – Nasser
    Commented Oct 22, 2014 at 5:33
  • $\begingroup$ Unfortunately, that doesn't work when the elements of the list are themselves lists. e.g. DeleteCases[{1, {2, Infinity}, 3,{ 4, 1/0}}, Infinity | ComplexInfinity] $\endgroup$
    – Roger Burt
    Commented Oct 22, 2014 at 5:48

1 Answer 1

2
$\begingroup$

I suppose you have something like this:

expr = Table[{{1/u, u^2}, {Sqrt[u], 1/Sin[u]}}, {u, 0, 2 Pi, Pi/4}]
{{{ComplexInfinity, 0}, {0, ComplexInfinity}}, {{4/π, π^2/16}, {Sqrt[π]/2, 
   Sqrt[2]}}, {{2/π, π^2/4}, {Sqrt[π/2], 1}}, {{4/(3 π), (9 π^2)/
   16}, {Sqrt[3 π]/2, Sqrt[2]}}, {{1/π, π^2}, {Sqrt[π], 
   ComplexInfinity}}, {{4/(5 π), (25 π^2)/16}, {Sqrt[5 π]/2, -Sqrt[2]}}, {{2/(
   3 π), (9 π^2)/4}, {Sqrt[(3 π)/2], -1}}, {{4/(7 π), (49 π^2)/
   16}, {Sqrt[7 π]/2, -Sqrt[2]}}, {{1/(2 π), 4 π^2}, {Sqrt[2 π], 
   ComplexInfinity}}}

And you want to remove the results that include ComplexInfinity. Then consider these:

Cases[expr, {{__?NumericQ} ..}]

Select[expr, MatrixQ[#, NumericQ] &]

Select[expr, FreeQ[#, ComplexInfinity] &]

Other ways to write the last one, assuming version 10 or later:

Select[expr, FreeQ[ComplexInfinity]]

expr ~Select~ FreeQ[ComplexInfinity]

expr // Select @ FreeQ @ ComplexInfinity
$\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.