Is this behavior of the Series function correct

While series expanding a function, I got an unexpected result, which seems like an error to me. I have boiled it down to this minimum (non) working example:

$  Assumptions = p \[Element] Reals && x \[Element] Reals; k = Conjugate[(x^2*(1 + I *p))^2 - x^2*(1 + I*p)]; Expand[FullSimplify[Series[k, {x, 0, 2}]]] Series[FullSimplify[k], {x, 0, 2}] // TraditionalForm 

This returns

-x^2 + I p x^2 + x^4 - 2 I p x^4 - p^2 x^4 

and

I (p+I) x^2+O(x^3) 

The second output, where I have first used FullSimplify returns the correct output. To my understanding, the first output is wrong. Have I found a bug, or is there something about the Series function I don’t understand.

How to symbolically manipulate the closed form series output from the easyFourier package?

EasyFourier by @xzczd is a nice package to obtain a Fourier series in closed form, e.g.

f = x^2 easyFourierTrigSeries[f, {x, -\[Pi], \[Pi]}, \[Infinity]] 

enter image description here

However, the output cannot be manipulated symbolically, at least I don’t know how to. For instance, differentiating the Fourier series is not possible like this:

D[easyFourierTrigSeries[f, {x, -\[Pi], \[Pi]}, \[Infinity]],x] 

enter image description here

What I would like to do is something comparable to the following, where I have copied the summand manually into a regular Mathematica Sum:

enter image description here

BTW: How do I downscale the size of pictures in stack exchange? Is there no easier way to insert Mathematica output? How can I insert complicated Mathematica input code in 2d-mode? (Thanks for hints to these "meta" questions, too.)

Visualizing convergence/divergence series

I was trying to visualize the idea of convergence/divergence of series using complex plane. I got this idea from “A First Course in Complex Analysis with Applications by Dennis Zill, Patrick Shanahan”. I used the command:

ComplexListPlot[Table[(0.9 I)^(n + 1)/n, {n, 1, 70, 0.01}],Joined -> True, PlotRange -> All,  PlotStyle -> {Blue, Thick}] 

to generate a plot as shown below:

enter image description here

Alternatively, one can use the command

ListLinePlot[Table[ReIm[(1.1 I)^(n + 1)/n], {n, 1, 70, 0.01}],   PlotRange -> All] 

to generate the same. This series is convergent as it kind of spirals down. If we change 0.9i to 1.1i, the plot changes to

enter image description here

which signifies that the series is divergent. What is want is that some sort direction as to which way the sequence goes, like these:

enter image description here enter image description here

How can this be achieved in mathematica?

Thanks in advance.

For a single party, how can I make Dead in Thay a “fast-paced assault” and not a drawn-out series of skirmishes?

I having been running adventure modules from Tales from the Yawning Portal for a single group of 3-6 players (i.e. I have 6 players, but sometimes as few as 3 can attend a session). Part of the reason for me running modules is to minimise necessary preparation time.

We recently finished playing the first adventure, Sunless Citadel, when I asked the players which adventure in the book they would like to do next. Upon seeing the massive awesome-looking map for the Doomvault in Dead in Thay (with 107 rooms, all containing encounters, divided into 9 mini-dungeons), they unanimously voted for that one. I warned them that it would take many sessions to complete, and they were fine with that. So I got all the characters to advance to Level 9, threw them some bonus loot, and dropped them into the Doomvault.

We are now three sessions in, and I have identified a few potential issues.

The lengthy preamble to the adventure says:

The incursion into the Doomvault is intended to be a fast-paced assault in which the characters have little time for typical rests. A few areas of the dungeon offer access to special magic that allows characters to gain the benefit of a rest.

However, our experience so far has been contrary to this. I provide a transcript of the game so far below:

  • In the first session, with 5 players, they fought through 3 rooms occupied by ordinary enemies, then one room occupied by a big (CR 10) boss monster. This took 40 minutes of in-game time.

  • In the second session, with 3 players (all casters), they took a long rest, had one encounter with two-and-a-half rooms’ worth of enemies (although a well-placed conjure woodland beings tipped the balance in their favour), then killed another big (CR 10) boss monster. After the rest, this took 1 hour of in-game time.

  • In the third session, with 3 players (2 casters), they took a short rest, killed a relatively strong monster, found a source of healing, fought two gorgons, returned to the gatehouse, twiddled their thumbs and restocked for what I declared to be 9 hours, took a long rest, returned to the dungeon, and fought a small room’s worth of enemies.

  • In the fourth session, with 4 players (2.5 casters), they had 3 combat encounters.

  • In the fifth session, with 3 players (2.5 casters), they dealt with a minor trap, had one combat encounter, took a long rest, had one large combat encounter, then took a short rest.

(For those unfamiliar with Dead in Thay, the gatehouse is an unmapped area through which the players entered the Doomvault and is under control of allies to the party. The module assumes that no encounters will happen in the gatehouse. I have thus assumed that it can function as a safe-room.)

So far, we have had 14 encounters, 3 long rests and 2 short rests. Over an in-game time of 49:20, the party has only spent 3:55 inside the dungeon, with the rest of the time sheltering in the gatehouse – far from a fast-paced assault! Typically, the call to take a long rest was done because the casters were out of spell slots, and with small party sizes the casters needed their spell slots for the party to deal enough damage to push through the encounters. While the players have found ways to recover hit-points within the Doomvault, nothing they have lets them recover spell slots besides a long rest.

I do not believe this to be a fault in the party (besides being too small half the time), but rather a characteristic of Dead in Thay. Page 84 of the DMG talks of the Adventuring Day, which is how many encounters a party can have between two long rests. For three level 9 PCs, an adventuring day contains 22,500 XP worth of encounters. Between the first two long rests, my three level 9 PCs had 31,900 XP worth of encounters, so it is hardly surprising that the party’s resources are so taxed. Between the second two long rests, we had 29,050 XP of encounters, mostly for 4 PCs, 2 lvl 9 and 2 lvl 10, which is around the right amount for an adventuring day.

If I had all 6 of my players, they could probably get further, but their adventuring day would still be much, much shorter than a full day. And because of this, and how a character can only benefit from a long rest once every 24 hours (PHB 186) (a rule I have already stretched a little), I anticipate that this will result in many more periods of inactivity in the gatehouse.

I am aware that the Thayans of the Doomvault can and will run some degree of preparations and repairs while the party is resting. However, the highly segregated nature of the Doomvault leads me to think that damage dealt to one section won’t draw that much attention from another section unless it is utterly catastrophic. And since access into and out of the Doomvault is being tightly controlled against the Thayans, their ability to replenish lost monsters is limited. The modifications the Thayans could make to the Doomvault during rests would be highly situational and rather limited, as far as I can tell, although maybe I’m just unimaginative.

Further reading about Dead in Thay online indicates that the adventure was originally designed for many groups of players to tackle simultaneously. This makes me fear that the Doomvault will become a long grind rather than a fast-paced assault for a single party.

On a related point, Dead in Thay includes a feature called ‘Alert Level’ which increases the difficulty of random encounters the longer players spend in the Doomvault; a feature intended to add to the feeling of a ‘fast-paced assault’. However, if the players only spend 2 hours a day inside the Doomvault, then the Alert Level never goes up by the rules as written, which is boring. I am considering having time spent in the gatehouse not decrease the Alert Level (but not increase it either), or possibly make Alert Level increase faster (1 per hour rather than 1 per 4 hours).

My question is this:
For a single party, how can I make Dead in Thay a “fast-paced assault” and not a drawn-out series of skirmishes?

Specifically, I would like to know how to

  • avoid the “15-minute working day” in a module densely packed with encounters (I am aware of several questions which handle this question generically; advice specific to Dead in Thay is thus preferable),
  • avoid burn-out in this mega-dungeon, and
  • make the Alert Level meaningful (although this is a minor point compared to the above two),

preferably without having to re-write the module.

An answer should preferably include your own experience in running Dead in Thay and how you tackled these issues (or if they are issues at all), although experience from other adventure modules of similar scale and density is acceptable.

Trouble with the numerical evaluation of a series

The series is $ \;S=\displaystyle{\sum_{n=0}^\infty 2^{-n+\sin(n\pi/5)}}$ .

Mathematica doesn’t find a closed form for Sum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}], so I tried both:

N[Sum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}], 100] NSum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}, WorkingPrecision -> 100] 

They return the same result:

2.621953360503001622580428627210775515701951638633391919349668089988053230383633286891962065806603221 

However, as I discovered later, this result is wrong. Indeed, the series is not difficult to evaluate, thanks to the periodicity of $ \sin(n\pi/5)$ :

$ $ S=\left(\sum_{n=0}^\infty2^{-10n}\right)\left(\sum_{n=0}^92^{-n+\sin(n\pi/5)}\right)=\frac{1}{1-2^{-10}}\sum_{n=0}^92^{-n+\sin(n\pi/5)}$ $

And N[1/(1 - 2^-10) Sum[2^(-n + Sin[n Pi/5]), {n, 0, 9}], 100] yields:

2.621953365022156800044269458734842788555304465923212022625632939238430813027293562013936284925892294 

So the initial sum only had 9 correct digits. It’s so bad that I am almost certain I am missing an important option, but I have no idea which (I tried playing with AccuracyGoal and PrecisionGoal, but it led me nowhere so far).

Any idea to get the numerical answer directly?

The same method as above can be used to evaluate $ \displaystyle{\sum_{n=0}^\infty 2^{-n+(-1)^n}}$ , and the sum is $ 3$ , however, Mathematica fails with a message that starts with NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections. So again I must be missing somthing important. The failure occurs with:

N[Sum[2^(-n + (-1)^n), {n, 0, Infinity}], 100] 

My guess would be that it fails for this one because the correct answer is an integer, and thus it’s impossible to give even a single correct digit since the sum could be 2.99999… or 3, but I’m not sure what really happens here, nor how to deal with this.

I’m not entirely new to Mathematica, but I have always found that numerical computations are difficult to handle with it. If someone can point to a good tutorial on this kind of problem or similar ones with NIntegrate, it would be really helpful!

Defining a function as a truncated taylors series

I am trying to numerically find the error of a truncated series. To do so I want to first define a function that is the truncated sum, I cannot seem to do that. All I am tried is not working.

f[x_]:=Normal[Series[ArcTan[x], {x, 0, 100}]] 

Does not work as f[1] evaluates $ x$ inside $ \arctan$ . I also tried

Normal[Series[ArcTan[x], {x, 0, 100}]] f[x_]:=% 

But that did not work. When I copy and paste the result of

Normal[Series[ArcTan[x], {x, 0, 100}]] 

It works fine. What am I doing wrong?

SQL – Sparse data in time series

[I wasn’t sure if this is the proper place to post SQL questions.] I have a public dataset of pharmaceutical prices. Any given drug gets a new price on some unpredictable day, and then the price remains that price until the next price change.

E.g.

drug            date          new_price acetaminophen   2020-01-09    0.25 oxycontin       2020-01-10    1.40 valaxirin       2020-02-10    2.34 oranicin        2020-02-11    1.54 acetaminophen   2020-02-12    1.47 

I have to do a variety of analytics e.g. "what was the price of acetaminophen on 2020-02-01?" Well that would of course be 0.25, but I need a way to figure that out in SQL. I have a variety of more complex queries, e.g. "list the ten cheapest drugs on a given date". So a solution I think needs to be generalized.

I realize that one possible solution would be to run a job that populates the database with prices for every day of the year, but I prefer not to solve the problem that way.