« first day (2951 days earlier)      last day (47 days later) » 

5:32 AM
Funny, LaplaceTransform[1//Sin, 1, 1] evaluates to 1/2.
(Tested on Wolfram cloud. )
 
 
7 hours later…
12:19 PM
I can't seem to think well this morning, and I can neither remember nor figure out why the following pattern doesn't match:
f[a_, {x_, y_} | (y : _?NumericQ : 3), z___] := {{a}, {x}, {y}, {z}}
f[1, "string"]
(I was expecting {{1}, {}, {3}, {"string"}} as output.)
 
12:59 PM
@Mr.Wizard Looks like Alternatives can't match an empty sequence if it's only due to Optional for some reason - but I would have also expected this to work, since Optional expressions wrapped in something else do work fine, e.g.:
g[{}] /. g[{x_} | {y_ : 3}] :> {{x}, {y}}
(* {{}, {3}} *)

g[] /. g[x_ | y_ : 3] :> {{x}, {y}}
(* g[] *)
What does work is if you wrap the Optional around the Alternatives expression:
Clear@f
f[a_, Optional[{x_, y_} | y_?NumericQ, 3], z___] := {{a}, {x}, {y}, {z}}
f[1, "string"]
(* {{1}, {}, {3}, {"string"}} *)
(This will only work if the default value doesn't match any of the other alternatives however)
Can anyone reproduce the following behavior of Rectangle in a GraphicsComplex with Dynamic coordinates that change from ImageScaled to something else?
DynamicModule[{dragging = False, start}, EventHandler[
  Graphics[
   {EdgeForm@Gray,
    FaceForm@None, GraphicsComplex[
     Dynamic@If[dragging,
       {start, MousePosition["Graphics"]},
       {ImageScaled@{-0.1, -0.1}, ImageScaled@{-0.1, -0.1}}
       ],
     {Rectangle[1, 2], Line@{1, 2}}
     ]
    },
   Frame -> True,
   PlotRange -> 1
   ],
  {
   "MouseDown" :> (dragging = True; start = MousePosition["Graphics"]),
   "MouseUp" :> (dragging = False)
   }
  ]
 ]
The Line and Rectangle should both have the same end/corner points - but for some reason, the Rectangle "remembers" the ImageScaled offset
Any ideas how to work around this that are not "build the rectangle manually using Line"?
Nevermind, just found a solution: If the coordinates are wrapped in Offset (e.g. Offset[{0,0}, start]), it works as intended
 
@LukasLang Thank you. :-)
@LukasLang Reproduced in v10.1 as well, for what it's worth.
 
1:19 PM
@Mr.Wizard thanks, good to know it's not just me :)
 
I am trying hard to remember if I've seen a pattern like Optional[{x_, y_} | y_?NumericQ, 3] before; I don't believe it would have occurred to me to try that as it's not the way I thought about Optional working. Thanks for the solution in any case, and something to think about.
 
1:42 PM
@Mr.Wizard I was trying to find the place where I have first seen it, but I was unsuccessful - it is mentioned a few times on this site however, e.g in this question, where you have probably even seen it
 
@LukasLang I must be blind; where is that form used in that Q&A?
 
@Mr.Wizard I was referring to this piece in the quote at the top of your answer: f[args:{x_, y_}:{1,2}] - here, the values of x and y are also bound to the default of the outer optional. It's not exactly the same as in your case with Alternatives, but this is what made me think of the workaround at least
 
@LukasLang The Alternatives aspect is the part that surprised me. Clearly I need to try to understand this whole mechanism better, and/or as well as I did before. Again, thank you.
 
@Mr.Wizard Ah, then I misunderstood what surprised you. Here is how I think of it, maybe it helps you as well: When the default value of Optional is used, the pattern in the first argument is matched against that default value, and pattern variables are bound to the corresponding parts, e.g. for Optional[{x_, y_} | y_?NumericQ, 3], something like the following is used to get the values of x and y: 3 /. {x_, y_} | y_?NumericQ :> {{x}, {y}}
 
2:03 PM
@LukasLang Okay, I can see how that might be, and it's a good insight. I don't know why but I've never thought about Optional working like this.
@LukasLang For the sake of testing your model how might this edge case behavior be implemented?
ClearAll[f]
f[x : _?NumericQ : "foo"] := x
f[]
 
2:17 PM
@Mr.Wizard As far as I can tell, this is a special case for pattern names bound to the entire Optional expression, which seems to have been added around version 10.2, see this question
But you are right, it doesn't work nicely with my explanation above. Note that inner pattern variables are left empty in such cases, as would be expected (it's impossible to assign them in a meaningful way after all):
ClearAll[f]
f[x : {y_?NumericQ} : "foo"] := {{x}, {y}}
f[]
(* {{"foo"}, {}} *)
 
@LukasLang Do you think the model is just "foo" /. (x : (y_?NumericQ | _)) :> {{x}, {y}} for example, with the | _ being the v10.1 addition?
 
@Mr.Wizard Yes, that seems to capture the semantics indeed
 
2:35 PM
Bye for now. Thanks again.
 

« first day (2951 days earlier)      last day (47 days later) »