Program to Copy a Text File to another Location using Keyboard input

Question: I am writing a program to copy a text file to another file location from keyboard input and am having trouble. The file names are in the correct places when entered from the keyboard, but the content is not there. I have gotten one line to copy correctly into the next file.  At the moment, I am receiving a cannot find symbol error on line 41.
    import java.io.*;
    import java.util.*;

    public class lity
    {
     public static void main(String[] args)
     {
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Directory of file to be copied");
      String dir = keyboard.nextLine();
      System.out.println("Name of file to be copied");
      String file = keyboard.nextLine();
      File copied = new File(dir, file);

      BufferedReader in =new BufferedReader(new FileReader(copied));
      String str;

      while((str = in.readLine())!=null)
      {
       char[] n;
       n = str.toCharArray();
      }
      in.close();

      System.out.println("Path of pasted file");
      String pdir = keyboard.nextLine();
      File newdir = new File(pdir);

      if(!newdir.exists())
      {
       newdir.mkdir();
      }

      System.out.println("pasted file name");
      String pfile = keyboard.nextLine();
      File myfile = new File(newdir, pfile);

      FileWriter stream = new FileWriter(myfile);

      stream.write(n);
      stream.close();
      }

     }

new code
    import java.io.*;
    import java.util.*;

    public class CopyFile
    {
     public static void main(String[] args)
     {
      String s = " ";
      StringBuilder sb = new StringBuilder(s);

      Scanner keyboard = new Scanner(System.in);
      System.out.println("enter directory path of the file to be pasted:");
      String dirName = keyboard.next();
      System.out.println("enter file name of the file to be copied");
      String fileName = keyboard.next();
      try
      {
       File input = new File(dirName, fileName);

       Scanner ind = new Scanner(input);
       BufferedReader in = new BufferedReader(new FileReader(input));
       String str;

       try
       {
        while(ind.hasNextLine())
        {
         str = in.readLine();
         sb.append(str+"\n");
        }
        in.close();
        ind.close();
       }

       catch(IOException e)
       {
        System.out.println("IOException");
       }

       System.out.println("path of file to be pasted");
       String dir = keyboard.next();
       File myDir = new File(dir);

       if(!myDir.exists())
       {
        myDir.mkdir();
       }

       System.out.println("enter name of the file to be pasted");
       String mfile;
       mfile = keyboard.next();
       try
       {
        File myFile = new File(myDir, mfile);

        PrintWriter pw = new PrintWriter(new FileWriter(myFile));

        String nstr;
        nstr = sb.toString();
    char[] n = nstr.toCharArray();
        for(int d = 0;d < n.length; d++)
    {
     pw.write(n[d]);
    }
        pw.close();
       }
      catch(IOException e)
      {
       System.out.println(e);
      }
     }
       catch(FileNotFoundException e)
       {
        System.out.println(e);
       }
      }
     }

Comment: which line is line 41?
Comment: Yep, I figured that as I was reading through your code.
Answer: You're declaring the n variable locally inside of your loop, and then trying to use it elsewhere. To get the code to compile you would need to declare it in the main method or in the class.
Regardless, even if the code compiled, it would still fail since you're re-creating the char array with each loop where you read in from the file. Consider either using a StringBuilder to get all the Strings, either that or reading and writing at the same time in one loop.
Something like (in pseudo-code):
create StringBuilder
Open file to read in a Scanner or BufferedReader
Loop through text of file
  append each String from file into the StringBuilder
  You probably want to append "\n" with each line added
end loop
close the Scanner or BufferedReader

Open file to write and create a PrintWriter with it
write out StringBuilder's String using toString() into the file using PrintWriter
close the PrintWriter.

Comment: There is still nothing written in the file. What I have written is above.
Comment: @user3277602: your latest code does in fact work, except you probably need to append the `"\n"` to each line as I recommend above. Also there's no need to pass a space into the StringBuilder's constructor.
Comment: Done. formatting is odd at the bottom, but you get the idea.
Comment: @user3277602: don't use **both** a Scanner and a BufferedReader at the same time -- that makes no sense. It's one or the other.


Playing vinyl with Traktor Scratch Pro 2

Question: Currently I'm mixing manually using raw vinyl, but I'm looking to buy Traktor Scratch Pro 2 with time coded vinyls:

When I'm running through Traktor will I still be able to play raw vinyl without changing my setup?
Answer: Yes.
Just change the input switch of the mixer to "phono" whenever you want to play a proper vinyl, then change it back to "line" to play the files.
Obviously you can't use the software functions on the old school vinyl (effects, cue points, loops...), you have to stick to the hardware in your setup for beatmatching, effecting and so on, but it's still fun. :)
Answer: The phono inputs have corresponding outputs, and the circuit is labelled "thru" -- so it seems to me that having the Traktor in the path will make no difference whatsoever, when using conventional records.
Comment: I thought I'd leave reading the manual to you (they are downloadable on the Traktor site)
Comment: Cheers @slim, do you have any proof to back this up? Have you got first-hand experience?
Answer:
Native Instruments (creator of Traktor) provides a multicore cable in order to connect turntables correctly to the mixer so that the system can work properly in DVS or Analogeu mode. DVS mode is always set to line so just use the Line feed in the Mixers for DVS or Phono for analogue sound on the fly.
Multicore cables are not needed to achieve this but really do make setting the system easier and way faster than multiple cables.


Where do I release the soft pedal in Ravel's Jeux d'Eau?

Question: I've been working on Ravel's Jeux d'Eau, and the intended use of the soft pedal in the piece is not always clear to me. There are several spots where he indicates to use the soft pedal, but there isn't always a clear distinction as to where to release it.
For reference, here is the score I am working from.
On page 20 (as marked on the page), the 2nd from last measure indicates to use una corde (side note: does this differ from the 2 Ped marking?), and the following measure is marked 3 cordes. The two measures are similar, so the directive to play the first soft and the second louder is obvious here.
Compare that to the first measure of the piece which is marked 2 Ped, and no other pedal marking is found until the second page of the piece. Does this indicate to apply the soft pedal until where marked on the second page, does 2 Ped only apply to the current measure, or was Ravel simply leaving this up for interpretation?
Looking at page 21 (as marked), the 5th measure has a Ped marking on only the first two beats, but as the beats that follow repeat the same pattern, it's apparent that the intention is to apply the same pedaling to them. This leaves me wondering if there is some implicit directive that I am missing about when to release the soft pedal.
Also on page 21, the last measure indicates 3 cordes. We see this marking again in the 4th measure of page 25, yet there is no indication between the two to use the soft pedal. Could this be an oversight, or have I missed something that indicates otherwise?
Answer: Okay, I think firstly you are not interpreting the 2Ped markings correctly. According to several sources (for instance here on music.SE and here elsewhere on the web) Debussy and Ravel use 2Ped to ask the performer to use both the soft pedal and damper pedals.
However, this misunderstanding is understandable; this article discusses editorial differences between different versions of Jeux d'Eau (specifically one edited by Roger Nichols, in this case), with particular reference to the interpretation of 2ped markings. It tells us,

One puzzling detail in Jeux d'eau: Nichols takes Ravel's "2 Ped." to mean, not "two pedals" (i.e., both damper and soft pedals), but "soft pedal," and he thus replaces it with "1 Corde," which Ravel used for that purpose elsewhere in the same work.

Now, I don't think the article relates to your score of the piece, but it does highlight the possibility of misinterpretation.
Looking at the score you link to, let me suggest answers for your more specific questions.
Indeed, there is no other marking after the first bar, until the Ped marking in bar 15, suggesting that, yes, both pedals are used until this point. (Whether using the soft pedal in the previous double-forte bar is appropriate, I'm not sure; you'd have to ask a pianist - I'm not a pianist!)
On page 21, I agree that the Ped markings here are to be repeated; these would seem to refer to the damper pedal (as usual) and would be appropriate looking at the way the harmony moves. Implicitly, I would say you should still have the soft pedal down, as the marking 3 cordes is given at the end of the page.
Finally, you are absolutely right that there is no una corde or 2Ped marking between the two 3 cordes markings you mention. However, if there is supposed to be a return to una corde, or indeed 2Ped, it would seem sensible that this takes place at the 1er Mouvt marking. Although, this literally only means a return to the first tempo, it seems fair to also infer a return to the opening mood, and so the same pedal marking (although, yes, if so it should really be marked, and the music is not the same as the opening…)
Hopefully this answer gets you pointed in the right direction with regard to the pedalling in this piece; I'm sure some pianists will also respond to give you more detail, particularly about the bar-to-bar subtleties of using piano pedals.
Comment: Pianist or not, this is a fantastic, helpful and informative answer.
Answer: On a grand piano, you can play ff with the soft pedal down if you want. You get a different tone color by moving the hammers sideways so a different part of the hammer strikes the string, but the volume of sound doesn't change much if your fingers use the same amount of force with una corda and tre corde.
But that doesn't work on an upright piano, where the mechanism reduces the volume (by moving the hammers closer to the strings) but doesn't change the tone color.
And what you get on a digital piano depends very much on the particular model you are playing.


Is ProcessorContext.schedule thread-safe?

Question: I was wondering if ProcessorContext.schedule is thread-safe so that I can spawn new thread to execute the Punctuator callback?
Also, if a consumer consumes just 1 partition but we set num.stream.threads=2. Does this automatically spawn a new thread for the scheduler?
After trying it a bit I found the answer may be "no".
Then what's the recommended the way to make spawning new thread for scheduler thread-safe?
Answer: Registering a punctuation will not spawn a new thread. The number of used threads in determined by num.stream.threads configuration only. Hence, if you register a punctuation, it's executed on the same thread as the topology and thus it is thread safe.
If you configure more threads than available input topic partitions, some threads would not get any work assigned, and thus, they would not execute any punctuations.
Comment: Got it, thanks. Followup: Is that safe to spawn a new thread in punctuator that needs to access processor context?
Comment: Is there a way to make a heavy-load punctuator working in another thread and thus not blocking the consumer?
Comment: That is not possible.


systemd list-units cuts service names

Question: I want to get all service names which match a pattern.
This does not work:
systemctl list-units -t service --no-pager --no-legend| cut -d' ' -f1| grep mypattern

Long service names get cut.
Example:
fooo123@...abc-new.service

How can I list the whole unit name?
Answer: You need to use the --full option:

Do not ellipsize unit names and truncate unit descriptions in the output of list-units and list-jobs.

Here is the whole script:
systemctl list-units -t service --full| cut -d' ' -f1| grep mypattern | while read s; do systemctl status $s; done

Comment: Note that it might be important to also include a lot of other flags like `--no-legend` and `--plain` if you intend to use it in scripts. See [this question](https://serverfault.com/q/942128/549727). Also, ALWAYS test for your SPECIFIC version of systemd - some flags (in particular, `--quiet`) may not behave the same across different versions.
Comment: I aliased that long ago... `alias systemctl='systemctl -l'`
Comment: @MichaelHampton a recursive alias. I was not aware that this works. Great :-)

systemd list-units cuts service names


confused about acf/pacf of this stock index

Question: all
I'm trying to determine the AR and MA of a time series by looking at the ACF/PACF plots but they doesn't look like the classic examples of the textbooks.
it has 264 monthly values of a stock index from 2000 to 2022. The series is not stationary so I take a diff and then plot acf/pacf. As you can see the first and second lags of either plots are not significant but the third lag is. I don't know how to interpret this.

Thanks for your help! Edited just to add some extra info (commands are from R)
Regarding UNIT Roots:
forecast::ndiffs(x) gives me "1", so it should be safe that taking a first difference is ok
Regarding seasonality:
forecast::nsdiffs(x) gives "0"
A plot of the time series: plot(x)

after taking the first difference: plot(diff(x))

and finally a descomposition using stl(x)
Comment: While the third lag is significant... it does look like it could be just chance.
Comment: take the log before differencing
Comment: I did it. The ACF/PACF are the same with a different scale.
Comment: A hint of quarterly seasonality tends to show up in certain markets where managers need to report their holdings on a quarterly basis.  Often, a close look at the data shows unusual activity right at the end of each quarter.  Thus, the spike at lag 3 is no surprise and suggests exactly what to investigate for understanding the data further.
Answer: This could indicate the presence of seasonality in your data. The initial impression looks like your series could be a 3-period seasonal-AR or seasonal-MA process because the 3rd lag is significant, followed by a significant 6th lag in ACF. In PACF, 3rd lag is again significant (the 6th lag is high but insignificant).
The plot of the original variable can give more insight into the patterns of behaviour and seasonality, or if seasonality exists in data or not. Check for any continuous repeating patterns of peaks and valleys in the variable over time (in this example, the pattern might repeat every 3 periods/months). If the peaks are of similar height over time, it indicates additive seasonality. If the peaks are increasing in height with an increasing trend, it indicated multiplicative seasonality.
You could also try seasonal differencing with a lag of 3 periods (3 months in your case). Seasonal differencing will be enough if the seasonality is additive. If it's multiplicative seasonality, you can use the SARIMA model for estimation.
Comment: The point I am trying to get across is that additive seasonality absolutely does not warrant seasonal differencing. Looking at the ACF and PACF plots, it is also absolutely clear that seasonal differencing is unwarranted and would most likely be harmful.
Comment: First, if there were seasonality, you would see signs of it on all integer multiples of 3 (3, 6, 9, ...), but here you do not. Second, differencing a series that does not have a unit root is asking for trouble (keyword: *overdifferencing*).
Comment: There is evidence of the 3rd and 6th lag being significant. But that alone is not enough to conclude that seasonality exists. That is why I mentioned that there could be seasonality in the data, but needs further checking. It could indicate seasonal AR or MA terms since both AFC and PACF die out after the 3rd or 6th lag. Again, it needs further checking to make sure this is the case. Only ACF and PACF may not be enough and we might need to check the behaviour of original variables with time or even detrend it.
Comment: Seasonality may not always be visible along with all multiples (3, 6, 9 and so on here). It could die out after a few multiples in both ACF and PACF, or in any one of them.
Comment: OK, SMA(3) could make some sense here if we only looked at the ACF-PACF plots and ignored the fact these are stock returns which should normally be unpredictable (and the plots being completely in line with an assumption of white noise; recall that 5% of bars should stick out purely by chance). Yet there is no way to advocate for seasonal differencing based on the graphs. Also regarding *the 6th lag is high*: it only seems high on a scale from roughly -0.17 to +0.17 as in the graph. The actual value is about 0.1 which implies an $R^2$ of about 0.01. This is really low.
Comment: It is true that seasonality probably won't exist in stock returns, but we still have no idea about actual data. All we have are the ACF and PACF plots. We definitely need some more insights into the data to make an informed decision. About advocating seasonal differences, I mentioned that it should be applied if the data has an additive seasonality. We have to make sure that seasonality even exists first, using further analysis. I am advising on the possibilities that might be encountered after some more analysis.
Comment: We could deal with it using Seasonal AR and MA terms within the model.


Eigenvalue of linear operator

Question: Let $V=P_3(\mathbb{R})$ and $T$ is a linear operator such that $T(f(x))=xf'(x)+f''(x)-f(2)$.
Find the eigenvalues for $T$ and an ordered basis such that $[T]_B$ is a diagonal matrix.
Comment: What is $P_3$? What have you tried? Where are you stuck?
Comment: so I know the standard basis of P3(R) is (1,x,x^2,x^3) and based on the transform, I know xf'(x) will be (0,1,2,3) f''(x) is (2,6,0,0) and -f(2) is (-2,0,0,0) but this leaves me with a 4x3. I'm also not 100% I'm right on that.
Comment: What is $P_3(\mathbb{R})$? Cubic polynomials with real coefficients?
Comment: If so, you're in a finite-dimensional space... write down $T$ as a matrix, and compute the eigendecomposition.
Comment: my main problem is writing down T as a matrix in the first place, I know how to do it from there. I should've made that more clear.
Comment: Compute $T(f)$ for each $f$ in your standard basis. The results will give you the matrix for $T$ (with respect to that standard basis).
Answer: In a finite-dimensional Hilbert space, a linear operator is uniquely determined by its action on a basis and can be represented as a matrix in that basis. In particular, if your basis is $\{1, x, x^2, x^3\}$,
$$T(1) = 0 + 0 - 1 = (-1)1 + 0x + 0x^2 + 0x^3,$$
so
$$T(a+bx+cx^2+dx^3) = \left[\begin{array}{cccc}-1 & ? & ? &?\\0 & ? & ? &?\\0 & ? & ? &?\\0 & ? & ? &?\end{array}\right]\left[\begin{array}{c}a\\b\\c\\d\end{array}\right]\cdot\left[\begin{array}{c}1\\x\\x^2\\x^3\end{array}\right].$$
Can you fill in the rest of the columns?
Comment: Thank you so much! I got it now.
Comment: It becomes an upper triangle matrix so the det[A] is just the product of the diagonal right?
Comment: I haven't checked that $T$ is triangular, but if that is indeed the case, then yes, the determinant of a triangular matrix is the product of the diagonal entries.

Eigenvalue of linear operator


How small can affine sets be?

Question: Quick question: let $V$ be a closed subset in the Zariski topology on $k^n$ ($k$ algebraically closed), and let $U$ be an open subset of $V$.  Is it possible to find another open subset $U_1$ of $V$ such that $U_1$ is contained in $U$, and the subringed space structure on $U_1$ is affine (that is, $U_1$ is isomorphic in the category of ringed spaces to an affine variety)?  Here I'm not requiring varieties just to be irreducible.
I think the answer should be no, for example if you take $k$ in the Zariski topology, and any open set $U$ is just $k$ with finitely many points missing, I don't think you can find an open subset of $U$ that's affine (that is, I doubt any cofinite subset of $k$ besides $k$ itself is affine).
Comment: Affine open subsets form a base for the topology. Remember that if you have an affine variety $X$ and a regular function $f$ on $X$ then the open set of points of $X$ where $f$ does not vanish is also affine. The trick is to add a new variable $y$ and the equation $fy - 1$. This should be in any algebraic geometry book!
Answer: The answer, if I am not misreading your question, is yes. I do not understand your suggestion of $k$ (usually denoted by $\mathbb{A}^1_k$), which is affine and any open subset of it is also affine.

How small can affine sets be?


Lagrange Interpolation Basic Proof

Question: I have been given this question, and I am unsure what the intuition behind answering it would be, nor where to start:

Let $x_1 < x_2 < \dots < x_n$. Show that, for some given function $f$, the polynomial
$$P(x) = \sum_{k=1}^n f(x_k) L_k(x), \quad\text{where}\quad L_k(x) = \prod_{i=1\\i\neq k}^n \dfrac{x-x_i}{x_k-x_i}$$, satisfies $$P(x) = f(x) \quad \text{for} \quad x=x_1, x_2, \ldots, x_n.$$

How would one approach this question, and what does it intuitively show by answering it?
Comment: Show first $L_k(x_j)=0$ for $j\ne k$, then compute $L_k(x_k)=1$ and you are done.
Comment: @LutzLehmann How can I show that $L_k(x_j) = 0$ for $j \neq k$? Is this just quoting the definition of a Lagrange polynomial?
Comment: The product in the definition of $L_k$ gets a factor that is zero.
Comment: @LutzLehmann Would it be possible to construct an answer? My understanding of this topic is quite limited at the moment so I struggle to fully grasp what is going on.
Answer: You will find that $L_k(x_j)=\delta_{jk}$ with the Kronecker delta. The zeros originate with zeros of the linear factors in the defining product of $L_k(x)$, while the ones result from cancellation in the quotient factors of the product.
So in the end you compute $P(x_j)=\sum_k\delta_{kj}f(x_j)=f(x_j)$.
Comment: Because this simply means $P(x_j)=f(x_j)$ for $j=1,...,n$.
Comment: Thank you for the answer. Sorry if I am being stupid, but I still don't understand. I know that $L_k(x_j)$ will either take values of $0$ or $1$, as you mentioned with the Kronecker delta. But how does this prove that the satisfication is met of $P(x) = f(x)$ for $x = x_1, \dots , x_n$ ?

Lagrange Interpolation Basic Proof


Collection: Results on stopping times for Brownian motion (with drift)

Question: The aim of this question is to collect results on stopping times of Brownian motion (possibly with drift), with a focus on distributional properties:

distributions of stopping times (Laplace transform, moments,..)
distributional properties of the stopped process (computation/finiteness of moments, ...)

Many of the results, which I have in mind, are typical homework problems.

What is the motivation for such a collection?

There is a number of "classical" stopping times for Brownian motion, but unfortunately these stopping times don't have a specific name (apart from "exit time", "hitting time", ... - which is also not very specific), and this makes it hard to find results here on StackExchange. Sometimes, when I'm looking for a result, I know that it is somewhere here on MSE but I'm simply not able to find it. For other questions, which are asked very frequently in MSE, it is often difficult to find a good "old" answer.
In any case, I believe that it would be a benefit to make the knowledge easier to access - both for students (who are trying to solve their homework problems) as for the "teachers" (who are answering questions on MSE).
To make this list a helpful tool (e.g. for answering questions) please make sure to give a short but concise description of each result which you list in your answer.
Comment: Great idea. Maybe it's also a good idea to include citable references for each formula? I'm sure many of them can be found e.g. in the Handbook of Brownian Motion by Borodin and Salminen.
Comment: @MarsPlastic Sure, it's nice to have citable references but I don't really see the point in giving the references in this (big) list here. I think that the references should be rather part of the linked answers. It already starts with the problem that many of the results can be found in several books, so which one should I cite? The one I personally like? Several ones? This would make the list real bulky.
Comment: Well, any book would suffice and in the end that's the choice of whoever adds it to the list - I don't really see the problem there. It's a good point though, that references should rather be added in the linked answers (if someone wants to add them). I was just a suggestion anyway.
Answer: Below, $(X_t)_{t \geq 0}$ is either a Brownian motion (BM, for short) or a Brownian motion with drift. For each of the items in my list I will indicate for which process the corresponding result was obtained.

$\tau = \tau_a:=\inf\{t \geq 0; X_t = a\}$ for $a>0$.

Note: We have $\tau=\inf\{t \geq 0; X_t \geq a\}$ a.s. if $(X_t)_{t \geq 0}$ is a BM, see this answer.

(BM) $\tau_a = \tfrac{1}{c} \tau_{a \sqrt{c}}$ in distribution
(BM) $\mathbb{P}(\tau<\infty)=1$ (via martingale methods)
(BM with non-negative drift) $\mathbb{P}(\tau<\infty)=1$ (via law of iterated logarithm)
(BM with negative drift) Computation of $\mathbb{P}(\tau<\infty)$
(BM) $\mathbb{E}\tau = \infty$
(BM with positive drift) $\mathbb{E}(\tau^n)<\infty$ for all $n \in \mathbb{N}$ and computation of these moments
(BM with drift) density of the distribution of $\tau$
(BM) Laplace transform of $\tau$
(BM with positive drift) Laplace transform of $\tau$

$\tau= \inf\{t \geq 0; X_t \notin [a,b]\}$

(BM) $\mathbb{P}(\tau<\infty)=1$ (for $d$-dimensional BM)
(BM) Distribution of $X_{\tau}$
(BM with drift) Distribution of $X_{\tau}$ (i.e. computation of $\mathbb{P}(X_{\tau}=a)$ and $\mathbb{P}(X_{\tau}=b)$.)
(BM) Computation of $\mathbb{E}(\tau)$ (see this question for BM started at $X_0=x$)
(BM) Computation of $\mathbb{E}(\tau^2)$
(BM) $\mathbb{E}(\tau^p) < \infty$ for all $p \geq 1$
(BM; $a=b$) Laplace transform of $\tau$ (see also this question)
(BM) distribution of $\tau$

Hitting times for some curves

(BM) $\mathbb{E}\tau=\infty$ for $\tau := \inf\{t; B_t^2 \geq 1+t\}$ (see also this question)
(BM) $\mathbb{E}(\tau) =\tfrac{1}{2}$ for $\tau = \inf\{t; B_t^2 = 1-t\}$
(BM) $\mathbb{E}\tau<\infty$ for $\tau:=\inf\{t; |B_t| = \tfrac{1}{2}(1+\sqrt{1+t})\}$
(BM) $\mathbb{E}(\tau)=\infty$ for $\tau=\inf\{t; B_t \geq e^{-\lambda t}\}$

Random variables which are not stopping times

(BM) $\tau=\inf\{t; X_t = \max_{s \in [0,1]} X_s\}$ is not a stopping time (see also this question)
(BM) hitting time of an open set is not a stopping time with respect to the canonical filtration

Miscellaneous

(BM) $\mathbb{E}(\tau)$ for $\tau=\inf\{t; |X_t-x| \geq r\}$ where $X$ is a multi-dimensional Brownian motion
(BM) $\mathbb{E}(\tau)$ for $\tau=\inf\{t; \sup_{s \leq t} X_s \geq b, \inf_{s \leq t} X_s \leq a\}$ with $a<0<b$
(BM) Proof of Wald's identity (see also this question and this version for $\mathbb{E}(\sqrt{\tau})<\infty$)
Comment: @MarsPlastic Why do you think so? MSE is based on an question-answer-principle, right? This one clearly counts as an answer so why should I make it part of my question? Other people are free to post their own lists, independently from mine.
Comment: Have you considered doing something similar for stochastic integrals of functionals of BM wrt time or dBt?
Comment: Wouldn't it make more sense to put that in the opening post?
Comment: I do not oppose to you posting this as an answer. ;-) My point is that the overall claritiy would profit from collecting these links in the opening post, don't you think? Any further links provided in other users' answers might then be added as well.
Comment: @badatmath No, I haven't.
Answer: Let $b: [0,\infty ) \to \Bbb R$ a function and define $\tau_b := \inf \{t > 0 : X_t \geq b(t)\}$.

(BM) Let $\alpha >0 , \beta \geq 0, \gamma > -\beta^2/4$.
$b (t) = \frac{\alpha}2 - \frac t \alpha \log \left( \frac \beta 2 + \sqrt{\frac{\beta^2}4 +\gamma e^{-\alpha^2/t}}\right)$
then $\Bbb P (\tau_b \in dt) = \frac1 {\sqrt{2\pi t^3}}\left( e^{-b(t)^2/(2t)}  - \frac\beta 2 e^{-(b(t)-\alpha)^2/(2t)}\right)dt$ (Daniels (1969))

(reflected BM: $X_t = \vert B_t\vert$) Let $\theta ,a >0$.
$b(t) = \frac t \theta \cosh^{-1}\left(a \exp\left(\frac{\theta^2}{2t}\right)\right)$, for $t>0$ such that $1< a \exp\left(\frac{\theta^2}{2t}\right)$
then  $\Bbb P (\tau_b \in dt) = \frac{\theta}{2^{3/2}} \frac{\sinh\left(b(t)\theta/t \right)}{a \exp\left(\theta^2/(2t)\right)} \frac1{\sqrt{2\pi}} e^{- b(t)^2/(2t^2)} dt$ (Lerche, 1986, Chapt.1, Exp. 3)

(reflected BM: $X_t = \vert B_t\vert$) Let $a>0$.
$b(t) = \sqrt{t\log\left( \frac{a^2}t\right)}$ for $0<t<a^2$
then $\Bbb P (\tau_b \in dt) = \frac{b(t)}{2t^{3/2}}\frac1{\sqrt{2\pi}} e^{- b(t)^2/(2t^2)}dt$ (Lerche, 1986, Chapt.1, Exp. 4)

(BM with start in $B_0 =x<0$) Let $\alpha > 0$
For $b(t) = \alpha t^2$ we have
$\Bbb P (\tau_b \in dt) = 2\left(\alpha \left(\frac1{2\alpha^2}\right)^{1/3}\right)^2 \sum_{k=0}^\infty \exp \left( -\mu_k t - \frac 2 3\alpha^2 t^3\right) \frac{\text{Ai}\left(\lambda_k - 2 \alpha \left(\frac1{2\alpha^2}\right)^{1/3} \vert x\vert \right)}{\text{Ai}'(\lambda_k)} dt$, where $\text{Ai}$ is the Airy function of the first kind and $\lambda_k$, $k\in\Bbb N_0$ are the zeros of $\text{Ai}$ on the negative half line and $\mu_k = - \lambda_k /\left(\frac1{2\alpha^2}\right)^{1/3}$. (Salminen, 1988)

The following is merely related, but maybe interesting:
Generally, if $\xi >0$ is a random variable, for $X_t = \vert B_t \vert $, one can always find a lower semicontiuous function $b:(0,\infty) \to [-\infty, \infty]$ such that $\tau_b \overset{d}= \xi$ (equality in distribution). In other words, every probability distribution on $(0,\infty)$ can be realized as first-passage time of (reflected) Brownian motion (Anulova, 1980).
Comment: The book A. Borodin, P. Salminen *Handbook of Brownian Motion* contains on more than 300 pages nothing else than such formulas. Totally impressive. There will be no MSE post ever than can top them.


Iframe rotatório em JavaScript

Question: Gostaria de corrigir o seguinte script para ficar da maneira que eu preciso.

Eu queria saber se tem alguma maneira do  ficar dentro do próprio script, não abaixo do , algo que use document.write dentro do script e um window.onload = random_iframe por exemplo. Para ficar somente algo dentro do .
Answer: usei jquery para fazer o load no iframe veja:

function randomIframe(obj){
var ends = new Array();

ends[0] = "http://www.terra.com.br";
ends[1] = "http://www.uol.com.br";
ends[2] = "http://www.bol.com.br";
ends[3] = "http://www.cade.com.br";
ends[4] = "http://www.baixaki.com.br";
var i = Math.round(Math.random()*ends.length-1);
obj.location.replace(ends[i]);

}
$(document).ready(function () {

    $("#publicidade").load(randomIframe(this));

});

Se você quiser que ele abra no tamanho especificado pelo seu iframe, use o atributo src para colocar o caminho de um html que abrirá toda a página iframe, exemplo :

// Pagina que contem o iframe

<!DOCTYPE html>

      <title>Exemplo</title>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">

// Pagina que carregará todo site

<!DOCTYPE html>

      <title>Exemplo</title>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">

Comment: Ótima solução, porém os sites não estão abrindo dentro do iframe, a página inteira é redirecionada... Além disso, é possivel deixar o iframe dentro do primeiro script com o "document.write"? Por exemplo document.write('<iframe name="publicid....) Obrigado
Comment: editei, salva em um html e testa, lembrando que são 2 paginas uma que vai carregar tudo e outra que contem o iframe com a resolução desejada.

Iframe rotatório em JavaScript


Azure Bing Web search fails with Query search

Question: I am using the Odata4j. When i try to execute for simple Web search with my query, getting following exeception at last line of the code:
PS: Please voteup if you find this helpful.Thanks
java.lang.RuntimeException: Expected status OK, found Bad Request. Server response:
Parameter: Query is not of type String
    at org.odata4j.jersey.consumer.ODataJerseyClient.doRequest(ODataJerseyClient.java:165)

This is my code:
ODataConsumer consumer = ODataConsumers
                .newBuilder("https://api.datamarket.azure.com/Bing/Search/v1/")
                .setClientBehaviors(OClientBehaviors.basicAuth("accountKey", "My account key here"))
                .build();

  System.out.println(consumer.getServiceRootUri()+consumer.toString());

  OQueryRequest<OEntity> oQueryRequest = consumer.getEntities("Web").custom("Query", "Search text criteria");

    System.out.println("oRequest"+oQueryRequest);

        Enumerable<OEntity> entities  = oQueryRequest.execute();

Answer: Not much into Java, but I've just got the same error with the implementation for Node.js, and the problem was that I forgot to add single quotes around the query. It should be something like:
...&Query='stackoverflow'

so after the URL encoding we have something like:
...&Query=%27stackoverflow%27

At least that worked for me.
Comment: Btw, if someone needs the implementation for node.js, you can use this module: www.npmjs.com/package/node-bing-api
Comment: Works for me too. Thank you!


sqlite shift rowid in multiple records

Question: Hello i have an sqlite db with many records like 10540 record they are ordered by creation time , i want to shift like a record in the middle and like to do it automatically
for example :
select * from table1 where id >= 8521;
UPDATE Table1 SET id = id +1 ;

does not work i get Error: Result: UNIQUE constraint failed:
so i want to shift up all records from 8521 to the last record and get place in the 8520 place for example so i can insert my record in that place of table .
even the
id = select max(id)+1

does not work how can i increment the id from last record to the needed record so i can put a place in the records db
Comment: Is the column `id` autoincrement?
Comment: This is a curious thing to want to do. If you can tell us why you want to do that we may be able to suggest a better solution.
Comment: yes the id is autoincrement , let's say the user bob wrote something at id = 1 then user john wrote another thing at id = 2 etc etc ... user x wrote at id = 5411 ,
i want to edit the id = 5410 and put john before x without taking any record place. for example if john wrote at id = 5410 and forgot to save or something and he tells the db admin so i can edit for him the db and put him at that place
Comment: i know i can do it manually by setting records id from 8521 to the last record to NULL and then use the reindex statement and insert a line with id of 8520 to the table but it's not handy
Comment: If you want to order rows by creation time, put the creation timestamp in a column and use that with ORDER BY. Don't try to make the rowid do something it isn't meant for
Answer: A simple update statement would fail, as it would try to create duplicate values in the primary key.
What you can do is this:
First update the column to the negatives of the values they should have:
update table1
set id = -(id + 1)
where id > 8520;

Now there are no duplicates and you just need to update again to the positive values:
update table1
set id = -id
where id < 0;

This will do the trick, but any kind of updating the primary key is not a recommended practice
Comment: thanks for your answer it worked . i do not do this all the time i only edit when asked to do so and help people thanks for help

sqlite shift rowid in multiple records


Invoke static method dynamically without using reflection

Question: Say I have something like so:
public class Entity<T> {

  public Class<T> model;

  public Entity(Class<T> m){
    this.model = m;
  }

}

so we use it like:
var ent = new Entity<String>(String.class);

but I can't call:
ent.model.format()

or
ent.model.join()

etc. None of the static methods are available in this case. Is there a way to call these static methods without using reflection?
Comment: are you sure it shouldn't be `T model`?
Comment: Why do you want to do this though? If you know the type at compile time, you could just call a static method normally with `String.join`. If you don't know the type at compile time, the compiler wouldn't know if `T` has a member called `join` at runtime anyway.
Comment: No, but method handles are preety fast.
Comment: Nah it's `Class` not `T`, look at the assignment in the constructor
Comment: In this case I am passing a Class around instead of an instance around. I would like to call the static methods on the Class, but I can't look them up without reflection, seems like
Answer: Seems like you want to bind the type to the attribute in your Entity class, for which you can ideally follow the approach as :
class Entity<T> {
    public T model;
    public Entity(T m) {
        this.model = m;
    }
}

which can then be instantiated as :
var ent = new Entity<>(""); // T is inferred based on the attribute type in the c'tor call
// or also  explicitly specified as
var ent = new Entity<String>(""); //redundant though

and then use it further as :
ent.model.format(""); // in this example 'model' is a 'String' with empty value ""

Comment: @rakim it would be possible to rewrite the code to work with a `String.class` instead of a `String`; in fact, it's even possible to rewrite it in a way that you don't need to pass an argument to the constructor at all. The remaining question still is, why? The `Entity` is entirely irrelevant to what you're doing, only the compile-time type of the variable matters.  E.g., you could also write `String s = null;  s.format("");` or `Optional o = Optional.empty(); o.orElse(null).format("…");`. If it is about hiding the variable's type, using `var o = Optional.empty();` would do.
Comment: @rakim You shall try to explain better in the question then why you want to do that.
Comment: yeah, I suppose format is being called like `(new String()).format` not the static method `String.format()`...right?
Comment: In the example shared (and as per the code in question), yes. Though a better way would certainly have been `String.format` for that.
Comment: yeah I upvoted, but I don't think this is an affirmative answer to the OP. OP wants to pass in `String.class` not `"a string"` into the constructor.


Fixing an object $Y$ defines a contravariant functor $\mathcal{C}^{op} \rightarrow$ Sets

Question: Say we fix an object $Y$, and then consider the assignment $(\mathcal{C}^{op})_0 \rightarrow$ Sets that sends $X$ to $\hom_{\mathcal{C}}(X, Y)$. How can I show that this defines a contravariant functor $\mathcal{C}^{op} \rightarrow$ Sets? In other words, I'm being asked to define the morphisms.
Is the point just that we can fix $Y$, then that $\hom_{\mathcal{C}}(X, Y) = \hom_{\mathcal{C}^{op}}(Y, X)$? Perhaps I'm misunderstanding the question.
Comment: What is $(\mathcal{C}^{\text{op}})_0$?
Comment: $(\mathcal{C}^{op})_0 = \mathcal{C}_0$, or the objects of $\mathcal{C}$.
Answer: One important point:  The assignment $X \to \mathrm{Hom}_{\mathcal{C}}(X,Y)$ defines a (covariant) functor $\mathcal{C}^{op} \to \mathbf{Sets}$, that is, a contravariant functor $\mathcal{C} \to \mathbf{Sets}$.  Think about how we may map the arrow $X \stackrel{f}{\to} X'$ to an appropriate arrow (which we will call $f^*$):
$$
f^* : \mathrm{Hom}_{\mathcal{C}}(X',Y) \to \mathrm{Hom}_{\mathcal{C}}(X,Y).
$$
In other words, given a typical morphism $X' \stackrel{g}{\to} Y$, can you think of a way to use $f$ to produce a morphism $X \stackrel{f^*(g)}{\to} Y$ ?  Once you make what amounts to the most straighforward or obvious choice, you may verify that the axioms for a functor are satisfied.
Hope this helps!
Comment: Recall, the functor goes from $\mathcal{C}^{op}$ to $\mathbf{Sets}$.... So you that means you should show $F(g \circ f) = F(f) \circ F(g)$, where $\circ$ is composition in $\mathcal{C}$.
Comment: So I was thinking to do precomposition, but I keep getting stuck trying to prove that $F(g \circ f) = F(g) \circ F(f)$. Doesn't something go wrong here?

$F(g \circ f) = h \circ g \circ f$ but $F(g) \circ F(f) = h \circ f \circ g$.
Comment: If you still are unsure, draw an arrow diagram.  And if you're still stuck after that, I'd be happy to expand on my answer above.
Comment: Is it just the following:

$F(g \circ f)(h) = (g \circ f)^*(h) = h \circ (g \circ f) = f^* \circ g^*(h) = F(f) \circ F(g) (h)$?
Comment: $F(g \circ f)(h) = h \circ (g \circ f) = (h \circ g) \circ f = F(g)(h) \circ f = F(f)( F(g)(h) ) = (F(f) \circ F(g))(h)$.  Therefore, since $h$ was arbitrary, $F(g \circ f) = F(f) \circ F(g)$.


core utilisation in c#

Question: I've written a program that reads through a WAV file and gathers various data then does certain calculations and outputs whatever useful information I need (the end point is a voice discriminator). This all happens individually for each file in a separate class object, only needing a different input string for each, so I figured I could quite easily thread up the application and I could have it running four times faster or so.
This I've done, and it seems to work nicely. However, when I came to time the application (threaded vs non-threaded) I only gained about 3 seconds over a minute to work through ~3600 files. My best guess for this is file I/O and I'll see greater improvements with all tests implemented, but that's not what I'm interested in particularly.
With Task Manager open on Win7 both versions of the application were showing similar activity on all four (virtual) cores on my i3 machine which then all dropped to minimum on completion.
So my question: Does c# compiler, and Visual Studio in particular, already optimise for multiple cores? and if not, have I missed something fundamental?
Answer: You're looking for the TPL; the Task Parallel Library.
Specifically, you can use Parallel.ForEach statement to process your files.
See here: http://msdn.microsoft.com/en-us/library/dd460720.aspx
Answer: It's difficult to answer your question without more detail but if you otherwise implemented the various thread functions correctly and use an appropriate number of threads for the hardware you're running your code on, your CPU utilization should be good in a threaded implementation.
If you don't use multiple threads for processing, your program will use a single CPU / single core - the compiler doesn't rearrange your code for multiple threads if that's what you're asking.
If you see the same kind of CPU utilization for both single-threaded and multithreaded code, there could be a number of reasons for that (just guessing since I don't know your code):

You use too few / too many threads
Disk I/O is soo much that all gains from threads are washed out
You have so many other processes / threads running on your system that they interfere with your measurement

There may be a number of other reasons but as Allan Elder's answer mentions, you can give the Parallels library a try - it'll try to pick an optimal number of threads for your code based on various factors. It makes using threads a lot simpler since the boilerplate code to hook up threads is done for you.
When I did similar processing on image files, using multiple threads cut my processing time down by 40% (although I loaded most images into memory - they weren't streaming so I/O wasn't as much of a factor). Core utilization was also much better with the multithreaded approach in my case - it was 8 cores vs. 1 core.
Comment: I just turned on all my tests and turned off the log files and timed again: 20 seconds vs 35. So yeah, my performance was drowning in disk I/O as expected. I'd already established most of this, but thanks for putting it so clearly. I mostly wanted to check I wasn't missing anything.

core utilisation in c#


What exactly is the relationship between significance tests and frequentism?

Question: Frequentism is very roughly the view that the probability of some attribute or event is identical to its actual relative frequency or hypothetical long-run, relative frequency.
My question: what is the relationship of significance tests in statistics to Frequentism?
(By "significance tests" I mean the general procedure of rejecting the null hypothesis H if the p-value of H and the observed test statistic is below the significance level alpha). Significance tests seem to me to be at the core of classical statics, which is usually strongly linked to Frequentism.
So, are significance tests meaningless/unjustifiable if Frequentism is false and if not, why?
Comment: Hypothesis tests do not rely on a frequentist interpretation of probability for their validity.  They do, however, avoid using Bayesian assumptions and that might be why some people associate these tests with frequentist approaches.
Comment: @whuber Usually I find myself in full agreement with your comments, However, in this case your point is not clear to me, I wonder if you can clarify. It is my understanding that the frequentist interpretation of p-values and hypothesis tests is what many find attractive.
Comment: David Cox in his "Principles of Statistical Inference" book (p.197) when writing positively about Frequentist analyses states: "The implications of data are examined using measuring techniques such as confidence limits and significance tests calibrated, as are other measuring instruments, indirectly by the hypothetical consequences of their repeated use".
Comment: @Graham That reads like a frequentist interpretation, but not a frequentist derivation or justification.  The decision-theoretic justification (based on optimizing the risk function for a binary loss) does not require any specific interpretation of probability.  (Bayesians object to classical hypothesis testing not because they interpret probability differently, but because they insist that the parameter must have a probability distribution.) From that perspective, the Cox quotation sounds only like a heuristic statement of a law of large numbers.
Comment: OK, you are drawing a distinction between frequentist interpretation and frequentist justification. However, if the desired frequentist interpretation was not present, Neyman-Pearson would not be interested in hypothesis tests, and Cox would not have been interested in significance tests. Sure, you can do Bayesian hypothesis testing but that is a different topic. Neyman-Pearson were optimizing across alternative tests that all had frequentist interpretability as I understand it.
Answer: When I say that a difference is "statistically significant at the 95% confidence level" (or that "p<.05") what that really means is...
"If, in reality, the difference in question were actually zero, and I had repeated this exact same analysis 100 different times, each time on a different random sample of the same size drawn from the same population, then only 5 of those 100 analyses would have (incorrectly) found a difference as large or larger than the one I actually observed."
So you can see that the concept of "taking repeated samples" is built right into the idea of statistical significance (and null hypothesis testing). In other words, saying something is statistically significant is really just making a claim about what would happen if you repeated the analysis you just did a large number of times.
It doesn't really make sense to worry about whether Frequentism is "true" or not. Frequentism and Bayesianism are two philosophical ways to think about this fuzzy concept called probability, but there is no way to ever prove that one of them is right or wrong. Of course, people often argue about which is more useful in particular contexts. But it is true that standard statistical tests, p values, and confidence intervals all presuppose a frequentist view of statistics.
Comment: Technically, your initial characterization is incorrect: it comes across as interpreting a probability as the actual number of occurrences.  The concept of taking repeated samples is built into this mischaracterization, but it's not a part of the theory of hypothesis testing.


Python BeautifulSoup resources

Question: So, hello. Python begginer and fairly new here as well. I didn't want to do this actually but I can't seem to find any answer anywhere. So I simply (or so I thought) wanna scrape this site to get a random word. I can't seem to find an efficient way on what tags to use in order to filter the html code. Any suggestions or good resources would be really appreciated! In addition, here's my code:
url = 'https://www.randomlists.com/random-words?dup=false&qty=1'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
review_text = soup.find_all(class_='support')
print(review_text)
>> []

I keep changing the find_all() argument but I can't figure out the right one
Comment: The word is loaded using JS and you cannot scrape that using bs4. Try using Selenium instead: https://stackoverflow.com/q/49939123/8878627
Answer: First of all, try to find some basic tutorials on youtube to learn how can scrap basic websites and how everything works overall.
And after that, I recommend to you start studying this book and practice.
Comment: You think I haven't? None really explains how to choose the tags, they just know because they have pre-coded the program! I;ll check the book tho, thanks
Comment: Consider using https://selectorgadget.com/ to choose the right tags!
Answer: Gonna drop a couple resources:
Course: https://automatetheboringstuff.com/chapter11/
YouTube: https://youtu.be/GjKQ6V_ViQE
And https://youtu.be/HiOtQMcI5wg
Personal projects:
https://github.com/0sergio-hash/Meal-Plan-scraping-project/blob/a884078eb119ab7ac5d113a8bdab494caad3db05/Meal%20Plan%20Project.ipynb
And
https://github.com/0sergio-hash/Amazon-Web-Scraping-project/blob/main/Amazon%20Web%20Scraper%20Project.ipynb
Second project follows along with the second YouTube video with some slight modifications
Answer: The random word is generated on the page with JavaScript. In another word, when your browser sends a request to server, and get the HTML (initial DOM), CSS and JavaScript files as response. Your browser will execute JavaScript, and insert element (random world) into HTML (now is modified DOM).
When you use requests.get(url) , you will get the HTML (initial DOM), and you cannot scrape the random word (because it does not exist) !
Therefore, in order to get the HTML (modified DOM), you have to scrape the page after the JavaScript is executed.
There are many solutions for this, please refer to this post.

PS. How to verify the random word is generated by JavaScript?
Disable JavaScript in your browser, and reload the page, you will not see the random word.
Comment: Now it makes sense, that's why looking at the source code I couldn't find the block for generating the word as it was in the 'Inspect'... Thanks mate. Now that I have the other post, this is solved
Yeah... And I just did disable JavaScript for a momment and word is now shown

Python BeautifulSoup resources


Preservation of negativity of eigenvalues of the Jacobian.matrix under coordinate change.

Question: Let $U$, $V$ be open sets in $\mathbb{R}^m$, and $\phi : U \rightarrow V$ be a diffeomorphism. Denote the inverse map by $\psi$. Let $f: U \rightarrow \mathbb{R}^m$ smooth.
Define a map $g:V \rightarrow \mathbb{R}^m$ as $g(x) := D \phi (\psi (x)) f(\psi(x))$, where $D\phi$ denotes the Jacobian matrix.
Suppose that $f(x_0)=0$ for some $x_0 \in U$, so that $g(\phi(x_0))=0$ by definition.
Then the following holds.

All the eigenvalues of $Df (x_0)$ has negative real parts if and only if the same condition holds for $Dg(\phi(x_0))$.

Proof: It is well known that $x_0$ is an exponentially stable equilibrium of the ODE $\dot x = f(x)$ if and only if $Df (x_0)$ has eigenvalues with negative real parts. The claim follows immediately from this equivalence.
Question: Is there a proof of the assertion without using any ODE theory?
Answer: Yes. Write down the product rule and chain rule and use two facts: $f(x_0)=0$ gets rid of the horrible term; and $D\phi(\psi(x)) = (D\psi(x))^{-1}$. Then you get the fact that $Dg(\phi(x_0)) = P^{-1}Df(x_0)P$ for $P=D\psi(x_0)$. Thus, the two matrices are similar (i.e., represent the same linear map) and, in particular, have the same eigenvalues.


Tamanho máximo para o nome de stored procedures, fields, etc ... no Firebird

Question: Pesquisei em várias fontes e não encontrei resposta definitiva para esta questão.
Creio que isso seja uma questão pertinente pois, baseados nisso, podemos
criar os nomes dos objetos de banco de dados vislumbrando estes limites.
Por exemplo, suponha que eu tenha uma stored procedure com o nome SPD_MINHA_STORED_PROCEDURE_REVOLUCIONARIA_PROJETADA_PARA_SALVAR_O_MUNDO.
Essa sequência tem 72 caracteres. Suponha que, eu crie, outra procedure
com o nome
SPD_MINHA_STORED_PROCEDURE_REVOLUCIONARIA_PROJETADA_PARA_SALVAR_A_GALAXIA.
Agora suponha que, o Firebird entenda e considere apenas os 30 primeiros
caracteres (que no caso do nome destas procedures, são iguais).
Quando eu invocar estas stored procedures, terei problemas pois, se o
Firebird entende e considera apenas os 30 primeiros caracteres do nome,
como vou saber qual procedure está sendo chamada?
Quais os limites para os nomes dos objetos de banco de dados informasse os mesmos nas relações
abaixo:
FireBird Versão 2.5.5
Stored Procedures -> Limite do nome = ?
Table -> Limite do nome = ?
View -> Limite do nome = ?
Trigger -> Limite do nome = ?
Exception -> Limite do nome = ?
Nome de variáveis/parâmetros de stored's -> Limite do nome = ?

Comment: Alguma das respostas resolveu o seu problema? Acha que pode aceitar uma delas? Se ainda não o fez, veja o [tour] como fazer isso. Você ajudandaria a comunidade identificando a melhor solução para você. Só pode aceitar uma delas, mas pode votar em qualquer coisa no site todo (se tiver pontuação suficiente).
Answer: Realmente não achei documentação oficial. O limite do identificador no Firebird 2.5 é 30 caracteres (ou 31 segundo algumas fontes). Apesar de não ser oficial, é o que diz várias respostas em fóruns e outros sites na internet. Não importa onde o identificador é usado, então em todos os casos acima se atenha a estes limites para não ter problemas. Parece que irão aumentar no 4.0.
Se tiver dúvida se isto está correto sugiro criar dois identificadores para o mesmo tipo de objeto dentro de DB com um tamanho maior onde a diferença entre os nomes ocorra apenas nos caracteres que superam o limite, os caracteres dentro do limite devem ser iguais, isto causaria um conflito de nomes. Vai diminuindo, o limite que não causar conflito é o limite da implementação.
Coloquei no GitHub para referência futura.
Comment: bigown, já pensei em fazer isso. Na verdade, dada a falta de documentação a respeito, será a única maneira de descobrir o limite.
Pra piorar a situação, estou usando o componente SimpleDataset, e tenho duas procedures com nomes grandes. Quando dou um add em um simple dataset, ele carrega os campos do outro dataset, justamente pelo problema de limitação de campos. E olha que, neste caso, a coincidência de nome já para no décimo sexto caracter ....
Ou seja, se tiver duas sp cujos nomes sejam iguais até o 16 caracter, já tenho um problema ....
Mas valeu, vou ver o que fazer.
Answer: Existem dois tipos de identificadores: regulares e delimitados. Eles tem regras diferentes de uso. Identificadores delimitados só estão disponíveis no dialeto 3.
Mas falando apenas sobre limite, o limite para ambos identificadores é 31 caracteres.
Isso está na documentação/manuais/materiais para referência. Veja o seguinte link:
http://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-structure-identifiers.html


GSL: Error reporting

Question: I want to use the GSL for integration
http://www.gnu.org/software/gsl/manual/html_node/Numerical-Integration.html
However, I find no convenient way how the integrated function
(the function f in the example http://www.gnu.org/software/gsl/manual/html_node/Numerical-integration-examples.html)
can report an error to the integrator. I want to integrate a function which itself results from an integration that could fail. This is my sample program
<code>#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
    GSL_ERROR("test error",GSL_FAILURE);
    return 0.0;
}

int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error);

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);

gsl_integration_workspace_free (w);

return 0;
}
</code>
resulting in the output
status          = 0
status          = -1
I think the integrator should rather stop and return my error code.  How can I achieve this?
Thank you very much for your help!!!
2011-04-27: I also tried this variant, after Brian Gough told me,
<code>#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
    GSL_ERROR("test error",GSL_FAILURE);
    return GSL_NAN;
}

int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error);

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);

gsl_integration_workspace_free (w);

return 0;
}
</code>
it did not help either. I will now fill out a bug report.
Answer: Thanks to Xuebin Wu from the GSL Mailing list the problem is solved:
Hi,
GSL_ERROR itself is a macro, it looks like
<code>      gsl_error (reason, __FILE__, __LINE__, gsl_errno);
      return gsl_errno;
</code>
The function already returns before you return NAN, because GSL_ERROR
has been called. Turning the handler off just let the first line do
nothing. The default error handler abort the program after printing
error message.
I do not think it is a bug. Maybe you can write your own error handler
to solve your problem. For example, you can use "goto" to jump out of
gsl_integration_qags, or set some global variable to indicate the
integration result is incorrect.
PS: I believe this macro is what you need,
Macro: GSL_ERROR_VAL (reason, gsl_errno, value)
This macro is the same as GSL_ERROR but returns a user-defined value
of value instead of an error code. It can be used for mathematical
functions that return a floating point value.
The following example shows how to return a NaN at a mathematical
singularity using the GSL_ERROR_VAL macro,
<code> if (x == 0)
   {
     GSL_ERROR_VAL("argument lies on singularity",
                   GSL_ERANGE, GSL_NAN);
   }
</code>
So I adjusted the code according to
<code>#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
//  return GSL_NAN;
    GSL_ERROR_VAL ("argument lies on singularity", GSL_ERANGE, GSL_NAN);
}

int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error);

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);

gsl_integration_workspace_free (w);

return 0;
}
</code>
and everything works as expected...
Answer: A bit hackish, but I'd probably have your function store some flag.  When it encounters an error it sets the flag and returns zero for all subsequent evaluations.  Then, after you've integrated it you can check this flag to see if the result is valid.
Comment: Hello Rhys, thank you for the hint, thats the way a colleague does it. I think it would be better (also concerning speed) to really stop the evaluation. Currently I compile the GSL with the -fexception  CFLAG and use C++ exception handling. However, normally the GSL does not support exceptions and they are C++, so I searched for a more direct way.
Answer: What about to write a wrapper for the function which returns pointer to a structure, containing function results and error status ? Or if you use c++, this encapsulation can be made with use of objects ....
Comment: Hello David, I just want that gsl_integration_qags stops due to the error reported in f...

GSL: Error reporting


Get timestamp of one month ago in PostgreSQL

Question: I have a PostgreSQL database in which one table rapidly grows very large (several million rows every month or so) so I'd like to periodically archive the contents of that table into a separate table.
I'm intending to use a cron job to execute a .sql file nightly to archive all rows that are older than one month into the other table.
I have the query working fine, but I need to know how to dynamically create a timestamp of one month prior.
The time column is stored in the format 2013-10-27 06:53:12 and I need to know what to use in an SQL query to build a timestamp of exactly one month prior. For example, if today is October 27, 2013, I want the query to match all rows where time < 2013-09-27 00:00:00
Comment: "The time column is stored in the format 2013-10-27 06:53:12" - you should use a [`timestamp` column](http://www.postgresql.org/docs/current/interactive/datatype-datetime.html) for that, not a formatted string, as it will make things like this a lot easier and more efficient.
Comment: "30 days" is more precise than "one month", and "exactly one month" is essentially meaningless. In SQL, if you subtract exactly one month from 2013-03-28, 03-29, 03-30, and 03-31, and you get the same answer: 2013-02-28. Does that matter?
Comment: @IMSoP Unfortunately, I don't have control over the database structure. I'm running a Quassel IRC Core, and having 18 million rows in the backlog table results in the client loading very slowly, so I need to archive all but recent rows in order to maintain client stability.
Answer: Question was answered by a friend in IRC:
'now'::timestamp - '1 month'::interval
Having the timestamp return 00:00:00 wasn't terrible important, so this works for my intentions.
Comment: DateTime functions: https://www.postgresql.org/docs/current/static/functions-datetime.html
Comment: note that the "friendly" version of this is `CURRENT_DATE - cast('1 months' as interval)`
Answer: select date_trunc('day', NOW() - interval '1 month')

This query will return date one month ago from now and round time to 00:00:00.
Comment: Note that this will return an actual timestamp; from the wording of the question, the actual column has a string, so you will need to cast it to compare: `WHERE CAST("time" as timestamp) < date_trunc('day', now() - interval '1 month')`
Answer: When you need to query for the data of previous month, then you need to query for the respective date column having month values as (current_month-1).
SELECT *
FROM {table_name}
WHERE {column_name} >= date_trunc('month', current_date-interval '1' month)
    AND {column_name} < date_trunc('month', current_date)

The first condition of where clause will search the date greater than the first day (00:00:00 Day 1 of Previous Month)of previous month and second clause will search for the date less than the first day of current month(00:00:00 Day 1 of Current Month).
This will includes all the results where date lying in previous month.


File share recommendations for many CAD files

Question: Description of problem / environment
We currently have around 100 GB of CAD files (90k files, 6k directories) stored in a couple of Subversion repositories. It seems an unnecessary hassle / burden to keep this much binary data in Subversion. It's also a burden for people to check in new files as they need to add & checkout a directory before they can commit. The only "advantage", being able to just right click and "update", has the penalty of 2 copies of each file being stored (how svn works), and being very slow. There is no meaningful version history to the files - i.e., the CAD files are not modified further one they are added, or if so, in this particular case it is not data we care about - only the current, latest state, or HEAD...thus exporting the data out of SVN is straightforward. Editing the files is not really part of workflow and is more likely to be accidental, and it involves 5+ CAD systems so I'm not sure a "PLM" type system would really be ideal or warranted.
The current environment of the file server is Windows Server 2003 - that will likely change in 6 months time (either to server 2008 R2 + big RAID 6, or a NAS, probably server 2008 R2 involved either way)
Due to the sheer size, no one really checks out all the parts (or even a given directory) very often and there is already a read-only network share that updates itself once a day from Subversion. The auto-update process breaks all the time (svn working copy gets dirty or in a bad state and needs to be cleaned). That is how the majority of users access these parts so they are fairly used to accessing from share already, it's primarily a change to how parts get added.
What new workflow options are there? Am I missing anything?
I'd like to update our workflow for dealing with CAD files. The current on the table consideration is going to a straight windows network share. Ideally maintaining this read-only behavior, but obviously people need a place to dump new files and have them be added to the share. If the network share becomes the primary source of the data it will be important that people aren't opening, editing, and saving the files all the time. I suppose the importance of that is debatable, but generally if editing, the contract is they copy them to their PC so the "main" copy of a given file isn't modified for everyone else.
Is it not worth the hassle trying to separate adding files from accessing them? (to maintain read-only access of the share)
Setting the share to write but not modify isn't necessarily an option (if maintaining read-only is a core requirement), as CAD systems like Pro/ENGINEER Take CAD file XYZ.prt, and each save increment a number... Eg. XYZ.prt.1, XYZ.prt.2, etc, which will result in many copies if people are accidentally saving to the share.
So far I have a hazy idea that I can script something to handle a writable "drop box" that copies to the share, and for example...denies zip files, and refuses to overwrite any files. This leaves me with the manual duty of deleting any files (occasionally necessary, but rare - or could be given to a select group of users). Maybe despite its imperfection Subversion isn't terrible...I'm looking for some other opinions here. What I don't want is to change everyone's workflow only to make the situation more work for me or the users (30-40 users).
Comment: "There is no version history to the files" - huh?  SVN isn't capable of doing anything except to keep the full version history.
Comment: Obviously SVN keeps a history (I am well versed in that), but the CAD files are never modified (i.e. modification to a file checked in), only new files added. Any history that does exist in svn is not history we actually care about, only the current, newest state, or HEAD. This is not a typical use of SVN, which is one reason I'd prefer to move the CAD files out of it. I'll update my wording
Comment: So to clarify, your users have a tool on their desktops (svn client) that they used for editing / uploading these files, and you separately have a read-only share where they can grab a (reasonably?) up to date copy of the current set of files.  Is that an accurate description of where you are now?
Comment: I'm not totally clear on the workflow you want: people check out a file, work on it locally w/ whatever CAD program they have, then save the modified file back?  And at that point you don't care about the previous version?  But until the new version "appears" back in the repository (SVN or new system), you want the previous version to be there for others to look at?  Is that right?
Comment: And what happens now if multiple people try to work on the same file at once?  I assume SVN can't do anything to merge your CAD files...
Comment: @Ward, People don't actually work on the CAD files - think of it more as analyzing them for our customers. Editing isn't normal workflow, so "checking out" and "committing a change" doesn't really happen much. All users care about is being able to open files, and add them easily. There isn't generally multiple people modifying a given file since people aren't generally modifying the files.
Comment: @Slartibartfast - Yes - except primarily to add new files or check out existing files, not generally modify (but I may need to provide the ability to do that). As the share has grown, the predominantly access files from the read-only share that updates periodically.
Comment: @Ward Regarding merging, yes I don't think SVN would provide useful merging of binary CAD files. Fortunately multiple people editing a file doesn't really happen
Answer: I should wait for your response to my comments, but...
How about:

A read-only share for your files.
A writable share with the same directory structure.
When someone needs to update a file, they "check it out" from the read-only share - that is, they make a copy of it locally.  (The limitation of what I'm about to say next is that there isn't a check-out process...)
They work on the file locally, any temp files are only on their hard drive.
When they're done updating the file, they copy it back to the correct directory on the writable share.
Using one of the many file copy tools (rsync, SecondCopy, whatever), at whatever interval you want, files are copied from the writable directories to the corresponding read-only directory.  The new version of the file will over-write the previous one, or you could keep versions at that point if you want to.

As I said, there's no actual check-out in this system, it doesn't deal with two or more people working on the same file at the same time.  I guess the collision resolution could make use of the fact that people would have a local copy of their work (at least for a while) to fall back on.
Comment: Thanks - even those simple steps are helping me solidify in my head what needs to happen.
Answer: I fail to understand why you are using a version control system when you are clearly not using it as such. Right now you are not getting the benefits of version control, yet are still paying for it in resources used.
Given your description I suggest using conventional file/folder shares instead. Why make it any more complicated than it needs to be? Structure is most easily established using clear and sensible folder naming and layering. I believe your users will not only adapt to it quickly but will thank you for the change.
Comment: It was setup this way many years ago, it was likely laziness because we are a software house and use Subversion for our source control (where it obviously matters a great deal!). There is zero reason to be using SVN for CAD files you aren't modifying, which is why I want to switch. I'm just kind of sanity checking that I'm making things simpler and not shooting myself in the foot Based on current read-only share behavior


Odds that a doctor has moved given that a doctor with that name is active at his postal code

Question: OK. I have a complex math problem at work that I've kind of gone down the rabbit hole with.
Assume: there is a background "doctor stops working" rate at $5\%/10$ years ($P(DS)$)
Assume: there is a background "new doctor with that name moved there" rate at $1\%/10$ years ($P(ND)$)
Also: $A$ means "this record is active", and it must be $100\%$ as we wouldn't be asking the question if the record were not active.
These are example values that I'll try to find otherwise. (I'm guessing finding good values for these will be a question on its own).
So, here's a situation. We know that a doctor filed a report 10 years ago. A doctor with that name and postal code filed a report today. What are the odds that he is the same person?
The reason that I'm having difficulty here is because I'm getting into circular logic. I can't use the $5\%$ background rate for a doctor's leaving, because the record is active. if we were to assume that nobody new ever came, it would have to be him, so the odds would be $100\%$. So I really need to find $P(DS\mid A)$. But $A = 1$, so that means that $P(DS\mid A)$ is $P(DS)$. Am I wrong? I tried other formulations where $P(DS\mid A)$ is predicated on $P(ND)$ but that's gone nowhere. Any hint as to how to proceed or even if this problem is solvable would be great.
Perhaps $A \neq 1$ - do I need to find the background rate of $A$, and I'm really using $A\mid(DS \cup ND)$?
Assuming that $A\neq 1$ by default, and it should be the background rate: $A$ would then be $(DS \cup ND)$. Does this not mean that I'm in circular logic again?
Comment: You seem to have a pretty good grip on the notion of conditional probability.  What makes you think that narrowing consideration of outcomes to those which agree with the "given" information constitutes "circular logic"?
Comment: I edited the format, but there are important things yet to change in this question so it can be properly answered. First, you are confusing *random variables*, *events* and the *probability of an event*. Take into account, for example, that when you say $A=1$, what you really mean, I guess, is $P(A)=1$. Make some effort making it clearer and I'll do my best to anser your question ;)
Answer: Some of your constants seem a little wonky (a $1/20$ chance of retiring every $10$ years works out to an average career of $200$ years), and the problem set-up seems a little askew, but I'll make some assumptions and see if they work for you.
Your doctor's name, say, is John Doe.  Let's suppose that existing Dr John Doe's retire annually at rate $\mu$, and new Dr John Doe's appear annually at rate $\lambda$.  That is, the number $k$ of Dr John Doe's has a time-dependent distribution governed by the differential equations
$$
\frac{d}{dt} P_k(t) = \lambda P_{k-1}(t)+(k+1)\mu P_{k+1}(t)-(\lambda+k\mu)P_k(t) \qquad k > 0
$$
$$
\frac{d}{dt} P_0(t) = \mu P_1(t) - \lambda P_0(t)
$$
If the distribution is stationary, and you know the number $k_0$ of Dr John Doe's today, you can take advantage of the fact that this is a pure birth-death process and is therefore reversible.  The arrival and retirement of Dr John Doe's is independent, so we consider the Dr John Doe who reported today separately from everyone else.  Start with the distribution $P_k(0) = 1$ if $k = k_0-1$, $P_k(0) = 0$ otherwise.  Find the distribution $P_k(10)$.  We also let $p_0 = e^{-10\mu}$ be the probability that the Dr John Doe who reported today was working ten years ago.  Then the conditional probability that the Dr John Doe who reported today was the same one who reported ten years ago, given that he worked ten years ago and there were $k$ other Dr John Doe's working at that time, is
$$
P(\text{same Dr Doe} \mid \text{working ten years ago, $k$ others})
    = \frac{1}{k+1}
$$
Since it can't be the same Dr John Doe if he wasn't working ten years ago, we can write
$$
P(\text{same Dr Doe} \mid \text{$k$ others}) = \frac{p_0}{k+1}
$$
and then unconditioning, we get
$$
P(\text{same Dr Doe}) = p_0 \sum_{k=0}^\infty \frac{P_k(10)}{k+1}
$$
The analytical solution for $P_k(10)$ is probably pretty messy, involving some kind of Bessel function.
Comment: The 1%/10 years thing was a typo, I meant it to be 1%/1 year. This is all pretty confusing to me but it looks solid, I'll keep looking it over until I get it. Thanks!


Open source soft modem that runs on Linux?

Question: Can anyone recommend an open source soft modem (a software only emulation of a modem card), that runs on Linux?
Preferably, this will be implemented in C/C++
Comment: Is this programming-related?  I'm having a hard time deciding.
Answer: take a look at iaxmodem
Comment: I've used it with hylafax to send faxes over an asterisk machine (see [fax server tutorial][1]).
But the quality wasn't as good as expected and it doesn't worked reliably. There are suggestions on the net that this combination only works reliably if the iaxmodem is installed on the same machine as the asterisk server. But that wasn't possible in my case.

  [1]: http://www.the-asterisk-book.com/unstable/faxserver-mit-iaxmodem-und-hylafax.html
Comment: I had actually come across iaxmodem in my search, but wanted to see if there were other opinions - or recommendations. +1 for suggesting it though. Incidentally, do you have any experience of using it?


What is the correct way to set cache contexts on custom blocks?

Question: I have run into an issue where a block that should be unique per page isn't for logged-out users.  The issue is a custom block plugin I have on a views search page that contains custom filters (sort of like a custom replacement for exposed filters. the block placed through /admin/structure/block).
Based on what I have learned about Drupal 8, I added the cache contexts to my build array:
  public function build() {

    $search_form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\SearchForm');
    return [
      'search_form' => $search_form,
      '#cache' => ['contexts' => ['url.path', 'url.query_args']]
    ];

  }

But it seems this must be incorrect because when logged-out, the block would get cached on the first view, and when the url changed, it wasn't showing a new version of the block.
I thought it might be the view page that was causing the issue, but even when I turned caching off on the view page, the issue remained.
I was able to fix the issue several ways, for example, by using a preprocess_block hook:
function mymodule_preprocess_block__mycustomsearchblock(&$variables) {
  $variables['#cache']['contexts'][] = 'url.path';
  $variables['#cache']['contexts'][] = 'url.query_args';
}

But it bothered me I couldn't just put the cache contexts into my block's build array.
Since my block extends BlockBase, I decided to try out the getCacheContexts() method, especially since I saw some modules within core are doing it this way.
  public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts(), ['url.path', 'url.query_args']);
  }

This is also fixed the issue, but interestingly, when I output the variables in the preprocess block function, these are not showing in $variables['#cache']['contexts'], but they do show in $variables['elements']['#cache']['contexts']
array:5 [▼
  0 => "languages:language_interface"
  1 => "theme"
  2 => "url.path"
  3 => "url.query_args"
  4 => "user.permissions"
]

I'm trying to figure out how this works, and why it wasn't working from the build function.
Looking at /core/modules/block/src/BlockViewBuilder.php at the viewMultiple() function, it looks like it pulls the cache tags from the entity and the plugin:
'contexts' => Cache::mergeContexts(
  $entity->getCacheContexts(),
  $plugin->getCacheContexts()
),

So that explains why adding a getCacheContexts() method to my block plugin adds the contexts to my block.  Also, looking at the preRender method in the same class, it looks like it doesn't use the cache array in the blocks build function, which confuses me, as it seems the way to add caching in Drupal 8 is to add a #cache element to render elements.
So my question is,
1) Are cache contexts added directly on the array in a block plugin ignored?
2) If so, is there a way around that, do we need to add it to a child element of the build array?
3) If context added directly is ignored, is adding a getCacheContexts() the way to go for block plugins in custom modules?
Comment: Do you use the standard block template or a custom one? See http://drupal.stackexchange.com/questions/217884/drupal-8-custom-block-class-with-custom-template-cache-issue
Comment: 1) No, your block content is actually a level down and should be merged in later. 2) Not needed because 1, 3) Implementing getCacheContexts() can be easier/cleaner but should not be required.  You explicitly mention anonymous users, are you sure that it also doesn't affect normal authenticated users? Does the problem go away if you disable dynamic_page_cache? Something strange must happen if it only affects anon users, as the internal page cache always varies by url/query args anyway.
Comment: Disabling dynamic page cache doesn't fix the issue.
Comment: Hm, It might be a problem with the fact that your top level element doesn't contain anything except #cache. Have you tried to simply set #cache within you form? It's the form that needs to vary by those, and since cache tags bubble up, that should just work. And if you ever use your form in a different block or other place, it should also just work there.
Comment: I quickly hacked SyndicateBlock and used this build() method: https://gist.github.com/Berdir/33a31b1e98caf080dae78adb731dba4c. Placing that on my site works just fine, the cache contexts are visible in cache_render table and the correct request URI is displayed. Can you try the same? Seems to me like something very strange is going on on your site
Comment: Yes, I tried just setting the cache on the form, and it didn't seem to bubble up.
Comment: These are the cids in the database: | entity_view:block:mycustomsearchblock:[languages:language_interface]=en:[route]=view.search_mysearch.page_1814a228561dab070a994db6febbbb64e81da19239ae0c74e505d53bc8e7516c8:[theme]=my_theme:[user.permAW4OHFPMmpl5de7Qao6TypMcD2NyiYUiy9HYhA0wwKo |
| entity_view:block:mycustomsearchblock:[languages:language_interface]=en:[theme]=my_theme:[user.permissions]=833bbf5a9ebc9243a1b579395499248a5a3736a756acebc3d3c41eb7666757d7
Comment: when I visit another page when logged out, even with dynamic page cache disabled, no new cids show up and the blocks are stale, not showing the filters in the url.
Comment: Thanks, with the simpler form in your gist, everything works as expected.  I'm going to add elements back until it breaks to see if I can locate the issue.
Comment: I was just noticing that.  It seems to be related to the custom template.  I'm using a custom template block--mycustomtemplate.html.twig If I output elements individually in the template, it breaks the uniqueness per page.  {{ content.foo }} in the custom template, and it won't work, {{ content }} and it does
Comment: I have updated the question with an answer based on your suggestions.  Thank you for all the help.
Answer: In most cases, you just set the cache context directly on the render array you return in your build() method.
I have finally found what my issue was, with the help of @Berdir and @4k4.  If you are using a custom template, such as block--myblock.html.twig and you output the variables individually, such as {{ content.foo }} instead of all at the same time like {{ content }}, it ignores your cache contexts passed directly into your block build array, when logged out.  See What is the correct way to set cache contexts on custom blocks?
So, to answer the original question:
1) Cache contexts passed directly into a custom block plugin are sometimes ignored.  You can test this by altering the SyndicateBlock, and then creating a custom template in your theme block--syndicate.html.php in which you output the variables individually like this:
{% block content %}
  {{ content.foo }}
{% endblock %}

As you change the url arguments, you'll see the block doesn't respect the cache context.
Now if you change it output all the content as a piece, it works:
{% block content %}
  {{ content }}
{% endblock %}

Now, it respects the cache context, and the block is unique per page.
2)  For now, to work around this, you can just output what's in your block in it's own template.
 public function build() {

    $search_form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\SearchForm');
    return [
      '#theme' => 'mycustomtemplate',
      '#search_form' => $search_form,
      '#cache' => ['contexts' => ['url.path', 'url.query_args']]
    ];

  }

This circumvents the block module's esoteric caching exceptions and your form is now unique per page when logged out.
3) Should you create your own theme template to fix this, or just add a method for getCacheContexts() in your custom Block plugin?  It's better to create a new theme template, rather than add a getCacheContexts() method that overrides the natural order of bubbling up of cache contexts, and may break metadata deeper in your build array.
Comment: This is a very good summary of the problem. But I think the conclusion in  3) is problematic, because you not only break that your own cache metadata can bubble up, but also that which may be deeper inside of the render array and you may not be aware of.
Comment: So you would suggest creating a new theme template?  To preserve clear bubbling?
Comment: Yes, use the block template only to add things to the outside. Build the inside of the block in build(). Use custom templates for this or use render elements, like a table or a core template like links.
Comment: OK, I'll update the answer.
Comment: Ugh, I didn't realize that Twig's drilling in would ignore the cacheable metadata. This might mean that we need to use our own custom method in the end to drill in (which renders the twig extension useless) so that we preserve metadata while going down into lower levels.

Good find!
Answer: For anyone else who finds this...
The reason that rendering content (or content|without()) works is that there is an element in the render array content['#cache'] that contains all of the cacheable metadata for the content.
If you do not allow this to be rendered in twig, either content or {{'#cache': content['#cache']|render }}, the page does not know it has cacheable metadata (eg. it never bubbles up).
Sounds like you are not doing custom twig though. If you are using something like Varnish, that could also be a culprit for anonymous users.
Answer: I also ran into this issue and the workaround I came up with was to create a new block_content variable based on a filtered version of the main content variable excluding any custom fields I want to render manually:
{% set block_content = content|without('field_mycustomfield', 'field_mycustomfield2') %}

Then instead of rendering the "content.body" variable directly later, I call:
{{ block_content }}

If you want to render every field individually, you can just keep adding them to the "without" filter so that rendering block_content doesn't do anything except fix caching.
Answer: When using cache contexts for anonymous users, be sure to disable the internal page cache module, as mentioned in cache context documentation.
It took me while to figure this out as well :-)
Answer: The easier method to achieve this is by declaring and defining the getCacheContexts() method

  public function build() {

    $search_form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\SearchForm');
    return [
      'search_form' => $search_form
    ];

  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    // If you need to redefine the Max Age for that block
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return ['url.path', 'url.query_args'];
  }

Check out the CacheableDependency documentation, it should contains everything you need ;)
Comment: This doesn't work anymore the way blocks are rendered now, see https://drupal.stackexchange.com/questions/288881/problem-setting-user-specific-cache-tags-on-custom-block-with-user-context


Is there a difference between ignorability and strong ignorability?

Question: The whole idea around ignorability is still leaving me a bit confused. I did read this post from the site: Strong ignorability: confusion on the relationship between outcomes and treatment. It had made things a bit clearer. But I'm trying to figure out what is the difference between ignorability and strong ignorability. It seems all the literature discusses strong ignorability as an assumption that we check in theory to determine the "quality" (there is probably a better word to describe it that is more poignant, but I'm not sure what) of our experiment, but I don't see anything on just ignorability.  Does it have a mathematical formulation like strong ignorability's $(Y_0,Y_1) \perp \!\!\! \perp  T|X$? This is all new to me and feeling a bit overwhelming....
Answer: Unless there is a specific reference in which you saw "ignorability" defined as something different from "strong ignorability," these terms are conventionally used interchangeably. Both are referring to the assumption that $(Y_0,Y_1) \perp \!\!\! \perp  T|X$.
For what it's worth, the term "unconfoundedness" is also typically used interchangeably with these terms: all three terms typically all refer to the same assumption that treatment is independent of potential outcomes (conditional on $X$).
Comment: In Rosenbaum & Rubin (1983), strong ignorability is defined to contrast with ignorability. I personally am unable to parse the difference between the two.
Comment: I found the difference out as per my professor. At least "symbolically" strong ignorability is characterized as $P(T| y^0, y^1, X) = P(T|X)$. Whereas ignorability is $P(T| y^0, y^1, X) = P(T)$. Where $T$ is treatment, $y^i$ are the potential outcomes, $X$ are the covariates.
Answer: I will give a response from the epidemiological side of causal inference, which uses a slightly different terminology ('exchangeable' rather than 'ignorable') with some subtle differences. Citing from What If (Hernán & Robins, 2023, p. 28, emphasis mine):

Rubin (1974, 1978) extended Neyman's theory for randomized experiments
to observational studies. Rosenbaum and Rubin (1983) referred to the combination of exchangeability and positivity as weak ignorability, and to the combination of full exchangeability (see Technical Point 2.1) and positivity as strong ignorability

Then, from Technical point 2.1 (p. 15, emphasis mine), we can see that:

Formally, let $\mathcal{A} = \{a, a', a'',  \dots \}$ denote the set of all treatment values present in the population, and $Y^{\mathcal{A}} = \{Y^a, Y^{a'}, Y^{a''}, \dots\}$ the set of all counterfactual outcomes. Randomization makes $Y^{\mathcal{A}}⊥A$. We refer to this joint independence as full exchangeability. [...] For a continuous outcome, exchangeability $Y^{\mathcal{a}}⊥A$ implies mean exchangeability $\operatorname{E}[Y^a | A = a'] = \operatorname{E}[Y^a]$ but mean exchangeability does not imply exchangeability because distributional parameters other than the mean (e.g., variance) may not be independent of treatment.

In other words, the distinction between weak ignorability and strong ignorability boils down to whether we are assuming mean exchangeability or full exchangeability (which I deem slightly more informative terms).
It is no surprise this distinction is hard to grasp since the bulk of most applications and expositions of causal inference problems is only concerned with average causal effects, so we end up erasing the distinction and just talking about ignorability or exchangeability per se. At the same time, causal assumptions from Rubin's terminology involve more than a single property (especially in the case of SUTVA), so that also does not help with clarity.


Deletar posição de array

Question: Boa tarde
Tenho um app de pedidos, registro os itens dentro de um array
quando preciso deletar um item do pedido, de alguma forma o tamanho do array não diminui
por exemplo cadastrei 3 itens e exclui um, o tamanho do array ainda fica 3 e se eu não adicionar mais um item pra completar, o app trava.
já tentei algumas coisas:
int size = pedido.itens().size -1;
pedidoList.remove(lastindex)
Continua dando problemas. o que poderia ser?
Comment: ```pedido.itens()``` e ```pedidoList``` referenciam o mesmo objeto?
Comment: Olá! pedido.itens é uma instâcia da classe pedidos que acessa o atributo itens que é o arraylist. pedidoList é um outro arrayList da classe Pedidos que eu uso pra listar os pedidos no recycler view. não sei se deu pra entender...
Answer: Olá,
Acontece que você coloca o valor atual da quantidade de pedidos dentro da variável  size e após isso, ela não é alterada, para corrigir, você pode chamar ele novamente:
int size = pedido.itens().size -1;

pedidoList.remove(lastindex);

size = pedido.itens().size -1;

Mas, desta forma que está, você poderia criar o size após remover o item, já que você não utiliza a variável size antes de chamar o método remove.
Comment: Boa noite. Me desculpe, não entendi muito bem! eu poderia usar dessa forma? de diminuir menos 1 do tamanho do array mas teria que chamar depois de remover o item, certo?

Deletar posição de array


Sort vs SortWithLimit in snowflake query profile

Question: what is the difference between the execution of sort and sortwithlimit?
How order by works with limit?
Answer: The combination of ORDER BY and LIMIT is implemented as a special operator, shown as "SortWithLimit" in the query profile.
This operator does not spill to disk when processing more than a certain amount of data and therefore can run out of memory.
Details: https://community.snowflake.com/s/article/Out-of-memory-error-caused-by-LIMIT-and-or-OFFSET-clause