1
$\begingroup$

I just started learning Mathematica and I'm not sure why a function I defined doesn't work.

So I'm trying to write a function that takes a list of vectors and eliminates all zero vectors (with respect to an inner product). I manage to do it the following way:

    noZeros1[list_List, innerp_] := DeleteCases[list, x_ /; innerp[x, x] == 0.] 

It works fine if I try it:

In: noZeros1[{{1, 1}, {0, 0}, {1, 0}, {0, 0}, {1, 2}, {0, 0}}, Dot]
Out: {{1, 1}, {1, 0}, {1, 2}}

However, I also tried the following approach:

noZeros[list_List, innerp_] :=list //. {a___, x_, b___} :> {a, b} /; innerp[x, x] == 0.

If I try that:

In: noZeros[{{1, 1}, {0, 0}, {1, 0}, {0, 0}, {1, 2}, {0, 0}}, Dot]
Out: {{1, 1}, {1, 0}, {0, 0}, {1, 2}, {0, 0}}

It only deletes the first zero vector it encounters. This is weird to me, because it has the same structure as the following function:

maxima[list_List] :=list //. {a___, x_, y_, b___} /; y <= x :> {a, x, b};

Which is a small function that rewrites a list eliminating elements that are smaller than the previous ones. That's essentially what I'm trying to do: a small function that rewrites a list eliminating elements that satisfy a condition.

$\endgroup$
2
  • 2
    $\begingroup$ What version are you using, and did you try to restart the kernel? For me, your noZeros function works as expected $\endgroup$ – Lukas Lang Jun 17 at 15:36
  • 1
    $\begingroup$ @LukasLang You're right. I restarted the kernel and now it works fine. The function probably had another rule associated with it. $\endgroup$ – IchVerlore Jun 17 at 15:40