## list manipulation – Evaluate parameters before function uses them

I want to construct a power series:

``````LP(s_) := With({x = 1, R = 10, n = 1},
Sum(cc(i + n, n, x, R) s^i, {i, 0, 20}))
``````

but it kinda takes a long time to compute the coefficients cc(…) although they depend on very quickly evaluated parameters b(…) (even for large i, n within 1s)

``````LoI(n_) := Array(k, n)
IB(r_, x_, R_) := {r, x, R}
cc(l_, n_, x_, R_) :=
Sum(Product(b(i, n, x, R)/i!, {i, LoI(n)})
KroneckerDelta(l, Sum(j, {j, LoI(n)})),
Evaluate( Apply(Sequence, (IB(#, 0, l) & /@ LoI(n)))))
``````

where

``````b(l_, n_, x_, R_) :=
With({a = 0.000001, m = 1},
NIntegrate(4 Pi r^2  m(a r Cos(r))^l, {r, x, R}))
``````

and V() is an elementary function of a single variable so the integral is quickly evaluated.

Can I somehow make Mathematica compute the b()’s beforehand and then use them to compute the cc()’s? Or does it just take a long time because the combinatorics of the multidimensional sum are complicated (theres a multinomial coefficient)?

It seems like the computational costs for cc(l,n) comes from larger n (small n and large l also goes very easily) so maybe its more about the combinatorics in the sum.

Does this function constantly re-evaluate the b()’s and waste time?

## list manipulation – Removing specific sublists

I have a program that generates a list of specific 3-tuples of integers. A typical output is as follows:

``````list = {{0, 0, 2}, {0, 1, 1}, {0, 2, 0}, {1, 0, 1}, {1, 1, 0}, {2, 0, 0}}
``````

I want to remove all 3-tuples `{x_,y_,z_}` where $$x$$ or $$y$$ (or both) are odd. The output in the example should be

``````cleanlist = {{0,0,2},{0,2,0},{2,0,0}}
``````

Note that 1 isn’t the only odd integer that can appear.

I tried using something like `cleanlist = DeleteCases(DeleteCases(list, {_OddQ,y_,z_}),{x_,_OddQ,z_})`, but unfortunately `OddQ` doesn’t behave like `Integer`.

Any help is appreciated

## list manipulation – Enumerating over matrix element values in Table

I have a $$N$$ dimensional array $$M$$, and a function $$f({M_{i}})$$ in terms of the array elements, where each matrix element $$M_{i}$$ can be 0 or 1. I’d like to construct a table

``````Table(f({M_{ij}}), {M_{1},0,1},{M_{2},0,1},...,{M_{N},0,1})
``````

For a given N, I can write down the code to construct the table, but for general N, it there a convenient way to write the above code? Namely, I’d like to keep N as an input variable.

## expression manipulation – Collect with linear combinations

``````expression = a x1 + (b^2)/3 y1 + z1 - a x2 - (b^2)/3 y2;
``````

To literally `Collect`, use a temporary change of variables :

``````Collect(expression /. {x1 -> v1 + x2, y1 -> v2 + y2},
{v1, v2}) /. {v1 -> x1 - x2, v2 -> y1 - y2}

(* a (x1 - x2) + 1/3 b^2 (y1 - y2) + z1 *)
``````

## plotting – Plots quality during manipulation

Mr. Wizard’s answer indeed solves the problem of the ugly display. What’s left to add is perhaps the more general question of why this seemingly trivial drawing problem is so slow in the first place.

The whole point of a `Manipulate` slider is, after all, that you want to see immediate effects in real time. And the difference to `ListAnimate` is that you want the real-time response to be a computed result, not in general a pre-rendered frame (because then we wouldn’t need `Manipulate` in the first place).

So one should investigate the factors that slow down the displayed object in your manipulate. In this case, it has to do with the fact that `ParametricPlot` and other plotting functions try to choose a clever set of plot points and then do an interpolation to draw a smooth line. But trying to be clever can cost you time.

So if you want to run your `Manipulate` on a slow computer and still want high quality, that will mean the response to the slider action will become very sluggish. The output will look nice at all times, but the user experience will be bad. So you’re between a rock and a hard place, unless you get rid of the time consuming step in the plot, i.e., the refinement and interpolation.

The solution then boils down to replacing `ParametricPlot` by `ListParametricPlot` (with lowest `InterpolationOrder -> 1` which is the default). This is actually what I do quite often with `DensityPlots`, too (replace by `ListDensityPlot` or even `ArrayPlot` or `Image`). It’s usually faster.

Here is what that would look like in the above example (keeping the over-wrapped angle variable just for consistency with the original):

``````Manipulate(
ListPolarPlot(
Table({t, Sin(nS*t)}, {t, -5 Pi, 5 Pi, Pi/(20 + 15 nS)}),
Joined -> True, InterpolationOrder -> 1), {nS, 1, 20, 1})
``````

You can leave out `InterpolationOrder -> 1`, but I put it in so you can compare what happens if it’s replaced by `InterpolationOrder -> 2`.

In choosing the number of points to generate in the `Table`, I used the number of expected lobes as a parameter (the higher `nS`, the more detail we need). For this example, this doesn’t give any speed advantage really, but I think it’s worth keeping in mind when your functions get more complicated. The explicit choice of plot points in this list plot is essentially part of the user-interface design, in that you decide how much lag you like to accept for an increase in quality.

Edit

Here is an example of a polar plot where `Manipulate` becomes really painful – using `PolarPlot` but with a different function than given in the question. It’s plotting the result of a numerical integration in “real time”:

``````Manipulate(
PolarPlot(NIntegrate(LaguerreL(nS, x), {x, 0, t}), {t, 0, 2 Pi},
PlotPoints -> ControlActive(10, 60)), {nS, 1, 20, 1})
``````

Trying this on my machine, I can (with some patience) drag the slider to the max, but then have to wait quite some time for the final interpolated curve to appear. Then I try to drag the slider back to the left, and Mathematica becomes unresponsive for several seconds, not even drawing the `ControlActive` version of the plot. The CPU usage sticks at 100%. And this despite the fact that I limited even the high-quality number of plot points to `60`.

Now delete the manipulate generated above and try the next one:

``````Manipulate(
ListPolarPlot(
Table(NIntegrate(LaguerreL(nS, x), {x, 0, t}), {t, 0,
2 Pi, Pi/30}), Joined -> True), {nS, 1, 20, 1})
``````

Again it takes just as much patience to drag the slider all the way to the right. The difference here is that I have used `ListPolarPlot` and therefore was able to forego any attempts at interpolation. This means that the plot looks more angular than in the previous example, even though the number of plot points is the same as above.

But at least the `Manipulate` now responds much faster when I try to drag the slider back down. I’m pushing the performance limits here, so one should add a disclaimer: save your work before trying this.

The point here is: `Manipulate` seems to be utterly useless for this admittedly contrived example unless we can turn off interpolation. To do so, I used the `ListPolarPlot` version because by default it does not attempt interpolation. This means the final curves won’t look entirely smooth, but (a) they look the same while dragging as they do when finished, and (b) the interface doesn’t go into a coma.

There may be ways of getting `ListPlot` to perform a better here, but as far as I know, `ListPolarPlot` is the most straightforward way to get the control I need here.

Edit 2

Another good way to speed up the `PolarPlot` without using `ListPolarPlot` is to add `MaxRecursion -> 0`. This suppresses the iterative refinement of the point set on which the function is evaluated – and the advantage is that you can combine it more easily with Mr. Wizard’s other suggestions.

Here are two closely related links that use both the `List...Plot` approach and the `MaxRecursion` approach. Either one can be superior, depending on the situation:

## list manipulation – Defining a directed graph with labeled edges step-by-step

Let `n` be a positive integer. I would like to define a directed graph `gr(n_,a_,b_)` (actually a bounded poset) as follows:

1. Level 0: we start with a vertex labeled by the list `{1,2,1,3,2,1,...,n,n-1,...,1}` (denoted by `g` for greatest element)
2. Level 1: we add vertices again labeled by lists of length $${n+1 choose 2}$$ that are created from `g` in two different ways:
• for every consecutive sequence in `g` of the form `k,k+1,k` we add a vertex labeled again by `g`, except that we substitute the sequence `k,k+1,k` by `k+1,k,k+1`. We attach the vertex `g` to every of the added vertices with a directed edge and label all of these edges with `a`.
• for every consecutive sequence in `g` of the form `k,l` with `|k-l|>1` we add a vertex labeled again by `g`, except that that we substitute the sequence `k,l` by `l,k`. We attach the vertex `g` to every of the added vertices with a directed edge and label all of these edges with `b`. It is forbidden to resubstitute `l,k` with `k,l` again in the next level (and only in the next level) otherwise this algorithm wouldn’t terminate.
3. we iterate the procedure until we reach the last level with exactly one vertex with label `{n,n-1,n-2,...,1,...n,n-1,n-2,n,n-1,n}` (the least element in the poset)

As an example, I drawed a picture of `gr(3,a,b)`: https://ibb.co/JnQpjqP

Actually, the graph itself is not important to me, I only need an output in the end from which I can extract the information how many `a`‘s and `b`‘s are on the path from `g` to every other vertex `v`.

Of course, I don’t expect you to give me a full solution to this problem. For starters I would just like to know if you think this is realizable in Mathematica and maybe a suggestion what kind of Mathematica functions is typically used for these type of problems.

## list manipulation – No need to use Alternatives to build alternative string models?

I am trying to match a list of strings with a pattern, in which there are several possibilities for the characters to match. To me, the following seems the most natural:

`````` StringMatchQ({"12", "23", "34", "45"}, ___ ~~ Alternatives @@ {"1", "4"} ~~ ___)
``````

which returns as expected:

{True, false, true, true}

However, I discovered that the following works too (and it is faster when the list is very long):

``````StringMatchQ({"12", "23", "34", "45"}, ___ ~~ {"1", "4"} ~~ ___)
``````

{True, false, true, true}

Why does it work? Why can we specify the different possibilities with a list rather than with alternatives ?.

I also discovered that we don't even need to properly evaluate Alternatives, the following also work:

`````` StringMatchQ({"12", "23", "34", "45"}, ___ ~~ Alternatives @ {"1", "4"} ~~ ___)
``````

{True, false, true, true}

even if the head of `List` is not replaced:

``````Alternatives@{"1", "4"}
``````

which gives the symbolic expression (not defined?):

Alternatives ({"1", "4"})

Why do these last two methods work and where are they documented?

## expression manipulation – Applying a function to coefficients and variables in Collect

I deal schematically with expressions of the form

``````expr = c(1) G(1) + c(3,8) G(3,8) + ... + c(3) G(1) + ...
``````

where the G are fixed objects and the c some disordered coefficients. I would like to collect this term by term in the G, by getting something of the form

``````Sum(d(i__) G(i__), {i, some_set})
``````

then perform operations on the ds and the Gs. If I just wanted to do something at DS, I think

``````Collect(expr, G(__), operation_d)
``````

would be the appropriate and probably the most effective function to use. Is there an (integrated) way to act simultaneously on the Gs too? I believe the process necessarily carries all of the required data during the collection process.

Of course, I can generate a list of relevant G's (Collect (expr, G (__), 1 &)) and a list of corresponding coefficients, then act on them and combine them. However, this divides the problem into two external rather than internal parts, and for the case that can be handled by simply Collect () in one step, this is certainly less effective. I'm basically wondering if there is something more "Mathematica-like" to do. Thank you!

Note: I know the related but not the same five-year question How to organize expression by symbols (like Collect), but apply different functions to each coefficient.

## python – Efficient manipulation of numpy arrays to convert an identity matrix to a permutation matrix

I want to be able to generate the permutation matrix which divides a 1D array of consecutive numbers (i.e. even, odd, even, odd, even, odd, …) into a 1D array where the first half is the same and the second half is the rating. So (pair1, odd1, pair2, odd2, pair3, odd3) goes to (pair1, pair2, pair3, odd1, odd2, odd3).

For example, with N = 6, the permutation matrix would be:

``````M = array((1, 0, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0),
(0, 0, 0, 0, 1, 0),
(0, 1, 0, 0, 0, 0),
(0, 0, 0, 1, 0, 0),
(0, 0, 0, 0, 0, 1))
``````

You can verify that by multiplying this by `M * array((0, 1, 2, 3, 4, 5)) = array((0, 2, 4, 1, 3, 5))`.

My pseudocode approach

(Full code below.) Here's the mathematically correct way to generate this:

``````I = NxN identity matrix
for i in (0:N-1):
if i < N/2:
shift the 1 in row i by 2*i to the right
if i >= N/2:
shift the 1 in row i by 2*(i - N/2)+1 to the right
``````

You can see how it works to generate M above.

Code (Python)

I am implementing the above pseudocode using the numpy array manipulation (this code can be copied and pasted):

``````import numpy as np

def permutation_matrix(N):
N_half = int(N/2) #This is done in order to not repeatedly do int(N/2) on each array slice
I = np.identity(N)
I_even, I_odd = I(:N_half), I(N_half:) #Split the identity matrix into the top and bottom half, since they have different shifting formulas

#Loop through the row indices
for i in range(N_half):
# Apply method to the first half
i_even = 2 * i #Set up the new (shifted) index for the 1 in the row
zeros_even = np.zeros(N) #Create a zeros array (will become the new row)
zeros_even(i_even) = 1. #Put the 1 in the new location
I_even(i) = zeros_even #Replace the row in the array with our new, shifted, row

# Apply method to the second half
i_odd = (2 * (i - N_half)) + 1
zeros_odd = np.zeros(N)
zeros_odd(i_odd) = 1.
I_odd(i) = zeros_odd

M = np.concatenate((I_even, I_odd), axis=0)

return M

N = 8
M = permutation_matrix(N)
print(M)

Output:
array(((1., 0., 0., 0., 0., 0., 0., 0.),
(0., 0., 1., 0., 0., 0., 0., 0.),
(0., 0., 0., 0., 1., 0., 0., 0.),
(0., 0., 0., 0., 0., 0., 1., 0.),
(0., 1., 0., 0., 0., 0., 0., 0.),
(0., 0., 0., 1., 0., 0., 0., 0.),
(0., 0., 0., 0., 0., 1., 0., 0.),
(0., 0., 0., 0., 0., 0., 0., 1.)))
``````

My problems

I have a feeling that there are more effective ways to do this. To summarize what I do for each matrix:

1. Loop through the rows

2. In each row, identify where the `1` must be moved to, call it `idx`

3. Create a separate zero array and insert a `1` in the index `idx`

4. Replace the row we are evaluating with our modified zero array

Is it necessary to divide the table in half?

Is there a Pythonic way to implement two different functions on two halves of the same array without dividing them?

Is there an approach where I can move the 1s without needing to create a separate array of zeros in memory?

Do I even have to run the lines?

Are there more efficient libraries than `numpy` for that?

## list manipulation – RSolveValue, Table and general programming

If I have the following code:

`````` temperature[q_] := RSolveValue[{b[n] == b[n - 1] - time, b[0] == 90}, b, n]; Table[{time, temperature[1000][Range[0, 3]]}, {time, {2, 3, 5}}]
``````

where the output entails the subtraction of the first time element by all the elements of `temperature[1000][Range[0, 3]]`. The output is: {{2, {90, 88, 86, 84}}, {3, {90, 87, 84, 81}}, {5, {90, 85, 80, 75}}}.

How do i change the code such that the subtraction is the first element of time subtracted by the first element of `temperature[1000][Range[0, 3]]` then the second time element being subtracted by `temperature[1000][Range[0, 3]]` and so on to give an output such as: `{90, 88, 85, 80}}`

PS: This question is an extension of another question asked here General question on RSolveValue and general programming