Hot answers tagged

7

Here are a couple of options. Given: poly = Array[# a[#] x^# &, 5] (* x a[1] + 2 x^2 a[2] + 3 x^3 a[3] + 4 x^4 a[4] + 5 x^5 a[5] *) We can do c = CoefficientRules[Expand[poly], x] c /. HoldPattern[a_ -> b_] :> (a -> b^2) (* {{5} -> 5 a[5], {4} -> 4 a[4], {3} -> 3 a[3], {2} -> 2 a[2], {1} -> a[1]} *) (* {{5} -> 25 a[5]^2, ...


6

If you need to get involved with a function of index j, you might find MapIndexed helpful. For example, if you want to drop j th element in the jth sublist, you can do MapIndexed[Drop[#1, #2] &, tab] Here #2 is your {j}, and you can feed it to your function. Be aware of the difference of {j} and j in Drop.


6

I haven't revised this code for 5 years but think it should do what you need for a): sortableTable[Transpose@inTable, Alignment -> {{Left,Right}, Center}] and for b) sortableTable[Transpose@inTable, Alignment -> {{Right, "."}, Center}] Clear[sortableTable]; Options[sortableTable] = Flatten[{TableHeadings -> {None, None}, Options[Grid]}]; ...


4

f = # -> #2^2 & @@@ # &; poly = Plus @@ Array[# a[#] x^# &, 5]; c = CoefficientRules[poly, x] {{5} -> 5 a[5], {4} -> 4 a[4], {3} -> 3 a[3], {2} -> 2 a[2], {1} -> a[1]} f@c {{5} -> 25 a[5]^2, {4} -> 16 a[4]^2, {3} -> 9 a[3]^2, {2} -> 4 a[2]^2, {1} -> a[1]^2}


4

The first thing you should do is to forget about using For-loops to manipulate lists. If you feel more comfortable with For-loop type thinking then you might use Table[Drop[tab[[i]], -2], {i, Length[data]}] But if you are willing to learn a bit functional coding, I'd recommend Drop[#, -2] & /@ tab


4

If we are to drop the first 2 elements of tab[[1]], the last 2 of tab[[2]], the first 6 of tab[[3]] and the last 2 of tab[[4]] make use of MapThread, e.g. MapThread[Drop, {tab, {2, -2, 6, -2}}] {{0.287282, 0.29287, 0.349432}, {0.335918, 0.320225, 0.306177, 0.294094, 0.28371, 0.274858, 0.267383, 0.260873, 0.255252, 0.247822, 0.245063}, {0.548742, ...


3

For anything but the simplest of graphics objects, always avoid Animate and use ListAnimate instead. The difference is that ListAnimate works on a pre-defined list of images to create an animation. All the rendering is done beforehand. With Animate, it attempts to do the rendering on the fly, when you are moving the slider. So this will make the ...


3

When dealing with a list of rules ({a->b, c->d, ...}) you might also be interested in converting it first into an Association, which allow efficient treatment of its elements and with some more simple syntax. For example in your case: taking @March example: poly = Array[# a[#] x^# &, 5] (* x a[1] + 2 x^2 a[2] + 3 x^3 a[3] + 4 x^4 a[4] + 5 x^5 ...


3

Do not use x as both a dependent variable in NDSolve and an index in Table. Instead, try, Table[Solu[-a, -1, 1 + a, .3, 50], {a, 0, 2}] (* {{7.19845*10^-22, -0.102113, 0.102113, 3.31597, 3.31597, 3.26406}, {-0.10725, -0.10725, 0.214501, 3.33333, 3.16227, 3.16227}, {-67.8836, -67.7287, 135.612, 3.29475, 0.133514, 0.162306}} *)


3

This should be much more efficient for anything beyond small lists: (Tr[Tally[#1~Join~#2][[;; Length@#1, 2]]] - Length[#1]) &[listA, listB]


1

The previous answers seem to have been addressing a rather different (or much more generalized) sort of problem, because as I see this now, we have the following problem statement: len = 10; (* let's have 10 to be specific, but this is an arbitrary positive integer *) max = 10^3; (* maximum value of numbers in listB *) listA = Range[len]; listB = ...


1

Many thanks to @J.M. and @Artes I found out that the iterator 0.1 in the Table function was making this artifact, which is also version dependent. @J. M.'s solution worked perfectly: Table[N[alpha[y1, d1]], {d1, 1/10, 5, 1/10}, {y1, 0, d1, 1/10}]


1

The three solutions provided in comments are all perfectly viable, and their performance is comparable as well: nmax = 3000; Select[Table[{n, Abs[2^n - 51]}, {n, 0, 3000}], PrimeQ[#[[2]]] &][[All, 1]]; // RepeatedTiming Select[Range[3000], PrimeQ[2^# - 51] &]; // RepeatedTiming Pick[#, PrimeQ[2^# - 51]] &@Range[3000]; // RepeatedTiming ...


1

Look up the difference between Append and AppendTo Append doesn't reassign the value of the List, while AppendTo does.



Only top voted, non community-wiki answers of a minimum length are eligible