Message Boards Message Boards

0
|
5256 Views
|
9 Replies
|
3 Total Likes
View groups...
Share
Share this post:

DatePlus[] error: not a recognized calendar increment specification

Why does this generate an error? Could this be the reason a function and/or plot that uses DatePlus[] this way doesn't work?

POSTED BY: Jay Gourley
9 Replies
Posted 2 years ago

Jay, I'm not sure why your recent posts aren't showing up on this page, but I received an email notification, so I think I know what you asked. I'll try to reproduce your questions faithfully.

  1. Is there a rule that predicts which functions work like Total[x+3] /. x->2 and which work like DatePlus[{2021,1,1},x] /. x->2? Or even some logic behind the difference?
  2. Am I right that by omission you told me there is no way to make the replacements work consistently?

So, Total[x+3] /. x->2 and DatePlus[{2021,1,1},x] /. x->2 are working the same with the exception that DatePlus generates an error message. Everything I described about the evaluation steps for DatePlus[{2021,1,1},x] /. x->2 applies exactly the same for Total[x+3] /. x->2. Don't be distracted by the error message if that's what's concerning you. The DatePlus example still produced an output. You can even suppress that message if you want to. To just suppress it for a particular evaluation, do it like this:

Quiet[DatePlus[{2021, 1, 10}, x] /. x -> -30]

To suppress it for the whole session, do it like this:

Off[DatePlus::inc]
  1. Is there a less roundabout way to express something like...
Grid[Table[
  With[{x = i}, {DatePlus[{2021, 1, 10}, x], 
    DatePlus[{2022, 1, 10}, x], DatePlus[{2023, 1, 10}, x]}], {i, 1, 
   5}]]

Yes, absolutely:

Grid[Table[
  DatePlus[{year, 1, 10}, inc], {inc, 1, 5}, {year, 2021, 2023}]]
POSTED BY: Eric Rimbey

Thanks, Eric. That's a big help.

POSTED BY: Jay Gourley
Posted 2 years ago

Jay, here's another way of thinking about it. Let's say you have an expression that has some hierarchy to it, like:

A[1, B["a"]]

What Mathematica will try to do is simplify/evaluate this expression according to rewrite rules (UpValues, DownValues, etc). What order should it do this in? Should it apply the rules for A first and then B, or the other way around? It seems intuitive that it should evaluate "inside out", and in fact this is what Mathematica typically tries to do. So, it looks for a rewrite rule for B, applies that, and then moves on to the rewrite rule for A.

Let's define A like this:

A[x_Integer, y_] := {1 + x, y, Head[y]}

Now let's evaluate A[1, B["a"]], and we should get {2, B["a"], B} But if we now define B like this:

B[x_String] := "z"

and re-evaluate A[1, B["a"]], we will get {2, "z", String}. So, B must have been evaluated first. If you evaluate A[1, B[1]], you'll get {2, B[1], B}, because our rewrite rules for B don't match an integer argument.

Okay, let's try ReplaceAll:

ReplaceAll[A[1, B[1]], 1 -> 5]

Evaluating that should give {2, B[5], B}. B[1] was evaluated first, but since there were no matching re-write rules, it just produced B[1], then the re-write rules for A were applied (giving {2, B[1], B}, and then ReplaceAll was applied, giving the final result.

Let's try

ReplaceAll[A[1, B["a"]], "a" -> 1]

This produces {2, "z", String}. If ReplaceAll had been applied first, then we would have seen a B[1] in the result.

Finally, let's try

ReplaceAll[A[1, B[1]], 1 -> "a"]

This produces {2, "z", B}. Since the ReplaceAll changed the expression, Mathematica does another round of evaluation, and the B["a"] that would have been there in an intermediate step gets evaluated to just "z" according to B's rewrite rules.

So, in your original question,

DatePlus[{2021,1,10},x]/.x->-30

everything inside the DatePlus expression gets looked at, and presumably there were no re-write rules for x, so Mathematica moved on to the full DatePlus expression. Since it can't evaluate it with a non-numeric value in the second position, it just returns the whole DatePlus expression unevaluated. However, DatePlus is designed to have the side effect of creating an error message in that case, so you see that error message appear during evaluation. We also must evaluate the expression x->-30, but this also cannot be simplified, so that bit remains as is. Next, we move on to the containing ReplaceAll expression. The replacement rule gets applied. Since this changed the expression, the new expression was re-evaluated, and this time DatePlus could perform the evaluation (without error).

Here's a fun thing to try. Evaluate the following lines:

x = 1;
DatePlus[{2021, 1, 10}, x] /. x -> -30

You should get {2021, -30, 11}. Walk through the evaluation manually, step by step, and you can see how we got that.

POSTED BY: Updating Name
Posted 2 years ago

I should have also mentioned that you can sort of interrupt this order of evaluation by using attributes HoldAll, HoldAllComplete, HoldRest, and the whole Hold* family of attributes.

POSTED BY: Eric Rimbey

Thanks, Eric. That helps a lot. 1. Is there a rule that predicts which functions work like

Total[x+3] /. x->2

and which work like?

DatePlus[{2021,1,1},x] /. x->2

Or even some logic behind the difference? 2. Am I right that by omission you told me there is no way to make the replacements work consistently? 3. Is there a less roundabout way to express something like,...

by using the trick suggested above by Sander Huisman?

POSTED BY: Jay Gourley
Posted 2 years ago

To see the evaluation steps

ReplaceAll[A[1, B[1]], 1 -> "a"] // Trace
POSTED BY: Rohit Namjoshi

DatePlus is expecting a number or a list as its second argument. The function DatePlus[{2021, 1, 10}, x] is coming back unevaluated and a warning is being printed. When you replace the x, then it is being evaluated correctly. Compare: StringJoin[{a, b}] /. {a -> "Hello", b -> "World"}

POSTED BY: W. Craig Carter

This doesn't work because at first it is left symbolic, and it tries to add x (symbolic) days and it left symbolic (with an error). It is then replaced, triggering another evaluation, and that works…

You can do this:

With[{x = -30}, DatePlus[{2021, 1, 10}, x]]

Or use Hold and ReleaseHold:

ReleaseHold[Hold[DatePlus[{2021, 1, 10}, x]] /. x -> -30]
POSTED BY: Sander Huisman

Thanks, Sander Huisman. Both solutions work. I would like to know more about the reason for the error. I didn't understand your explanation, possibly because I don't know what left symbolic means. Is this explained in documentation somewhere?

POSTED BY: Jay Gourley
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract