Why can't I pass a shared pointer of ULARGE_INTEGER, but a ULARGE_INTEGER* without problems?

Question: Here is my problem:
std::shared_ptr<ULARGE_INTEGER> freeBytesAvailable = std::shared_ptr<ULARGE_INTEGER>(new ULARGE_INTEGER);
std::shared_ptr<ULARGE_INTEGER> totalNumberOfBytes = std::shared_ptr<ULARGE_INTEGER>(new ULARGE_INTEGER);
std::shared_ptr<ULARGE_INTEGER> totalNumberOfFreeBytes = std::shared_ptr<ULARGE_INTEGER>(new ULARGE_INTEGER);

ULARGE_INTEGER *test = new ULARGE_INTEGER;
ULARGE_INTEGER *test2 = new ULARGE_INTEGER;
ULARGE_INTEGER *test3 = new ULARGE_INTEGER;

if (GetDiskFreeSpaceEx(NULL, test3, test2, test))
{
    //do something
}

as you can see, I want to call GetDiskFreeSpaceEx, and as a parameter it expects a PULARGE_INTEGER, so a pointer to a ULARGE_INTEGER
If I am using test, test2 and test3 as parameter it works totally fine.
However, I want to learn about smart pointers and therefor try to include some in my code and use them.
Everytime I try to pass my smart pointers, I get a conversion error, so something has to be wrong with the data type, but I just can't figure out the problem.
Shouldn't both be pointing to a ULARGE_INTEGER?
Can someone help? My guess would be that I would have to kind of unpack the raw pointer out of the smart pointer wrapper, but I'm not sure how to do so.
Comment: When in doubt start with a [good reference](http://en.cppreference.com/w/cpp/memory/shared_ptr).
Answer: Shared pointers are really just objects encapsulating raw pointers. Hence, you can pass shared pointers as function parameters only if the called function accepts a shared pointer as a parameter! You cannot pass a raw pointer in lieu of a shared pointer and vice versa. However, there are instances where you may want to pass the underlying raw pointer as an argument to a function call. This is typically the case when you have to make a C- library call. In such cases, just extract the underlying raw pointer using the get() method on the shared pointer object.
So in your case, the call to GetDiskFreeSpaceEx should be:
GetDiskFreeSpaceEx(NULL,freeBytesAvailable.get(), totalNumberOfBytes.get(), totalNumberOfFreeBytes.get() )

that is assuming that the function takes these parameters in this order.
Answer: I found a solution, so I will write it down for anyone looking for a quick solution as well:
.get()

will return the raw pointer, so if you are trying to pass a smart pointer to a function expecting a pointer, just add it and you are good to go.


Show that $(3, 1+\sqrt{-5}) \neq \mathbb{Z}[\sqrt{-5}]$

Question: Show that it is not possible to find "integers" $x, y$ of this kind which satisfy the equation $3x-(1+\sqrt{-5})y=1$, even though $3$ and $1+\sqrt{-5}$ are "primes", and therefore their greatest common divisor is 1.
My effort: I let  $x$ be $a+b\sqrt{-5}$ and $y$ be $c+d\sqrt{-5}$ and then substituted this into $3x-(1+\sqrt{-5})y$.
I ended up with this being $(3a-c+5d)+\sqrt{-5}(3b-d-c)$, and for this to be $1$, I postulated that $(3a-c+5d)$ must equal $1$ and $\sqrt{-5}(3b-d-c)$ must equal $0$.
I end up with two equations with four unknowns, giving infinite solutions. What have I done wrong? Also, what is the significance of this question in terms of learning the difference between the natural numbers and "integers" of this kind?
Thanks.
Comment: You're looking for (regular) _integer_ values of $a,b,c,d$; you're right that there are infinite solutions over the reals (or rationals), but that doesn't guarantee that any of them have all four values integral. As a hint: what does your equation that $\sqrt{-5}(3b-d-c)=0$ say about $d+c\bmod 3$? What does the first equation say about $5d-c\bmod 3$? Can you see a way to relate these two statements and derive a contradiction?
Comment: @StevenStadnicki $3b - d - c = 0$ implies that $d+c$ must be $3b$, so a multiple of $3$, that is $d+c \equiv0\pmod 3$, which implies $c \equiv-d\pmod 3$. And then... for $3a-c+5d=1$ we can say that $c-5d=3a - 1$, so $c-5d \equiv-1\pmod 3$, which implies $c \equiv-1+5d\pmod 3$. Substituting the first equation for $c$ into the second, we get $-d \equiv-1+5d\pmod 3$, which implies $6d \equiv1\pmod 3$ which is a contradiction because $6d \equiv0\pmod 3$ ... is this right? If it is, then all that is said means that the assertion in OP is correct.
Comment: That looks like exactly the argument to me!
Comment: @StevenStadnicki You are right... I am so silly, the $a, b, c, d$ should be integer values so it is possible there are no solutions. Hmmm they introduce modular arithmetic in the *next* chapter of the book but I will try...
Answer: Steven's comment is a good way to finish your solution. Here's an approach that will help with understanding the significance of your question: Multiply both sides by $1 - \sqrt{-5}$ to get
$$3x - (1 + \sqrt{-5})y = 1 \quad\implies\quad 3(1 - \sqrt{-5})x - 6y = 1 - \sqrt{-5}$$
The left-hand side can be factored as $3[(1 - \sqrt{-5})x - 2y]$. For any choice of "integers" $x$ and $y$, the quantity in brackets will be an "integer" $a + b\sqrt{-5}$, so the left-hand side is $3$ times an "integer", i.e. $3a + 3b\sqrt{-5}$. The right-hand side isn't of this form, so the equation has no solution.
These "integers" are the elements of the ring $\mathbb{Z}[\sqrt{-5}] = \{a + b\sqrt{-5} : a, b \in \mathbb{Z}\}$. You can think of rings like $\mathbb{Z}[\sqrt{-5}]$ as analogous to the integers $\mathbb{Z}$ in that you can add/subtract/multiply two elements of the ring and get another element in the ring. But this question shows that Bézout's identity holds in $\mathbb{Z}$ but not in $\mathbb{Z}[\sqrt{-5}]$. What this means is that you can't think about primes and factorization the same way. The essential difference between $\mathbb{Z}$ and $\mathbb{Z}[\sqrt{-5}]$ is that prime factorization is unique in $\mathbb{Z}$ but not in $\mathbb{Z}[\sqrt{-5}]$; note that our solution above relied on the fact that $6$ can be factored two different ways: $$6 = 3 \cdot 2 = (1 + \sqrt{-5}) (1 - \sqrt{-5})$$ If you study algebraic number theory, you'll learn how to salvage this situation by defining primes in $\mathbb{Z}[\sqrt{-5}]$ in a slightly different way so that prime factorization is unique. This allows us to do number theory in rings other than $\mathbb{Z}$, which is interesting in its own right and also helps us solve many classical problems over the integers, for example determining which prime numbers are expressible in the form $x^2 + ny^2$.
Answer: Hint: $\,\ w := 1+\sqrt{-5}\,\Rightarrow\, w\bar w = \color{#7cf}6\,\Rightarrow\, \color{#c00}{(w,3)(\bar w,3)} = \color{#7cf}3(\color{#7cf}2,w,\bar w,3) = \color{#c00}{(3)},\,$ but if $\,(3,w)=(1)\,$ then conjugating yields $\,(3,\bar w)=(1)\,$ so $\,(w,3)(\bar w,3) = \color{#c00}{(1)},\,$ contra above (more detail here).
Similarly $\, \color{#0a0}{(w,2)(w,3)=(w)},\ \ \color{#90f}{(\bar w,2)(\bar w,3) = (\bar w)},\ \ (w,2)(\bar w,2) = (2)$
Using the above, the nonunique factorization $\, \color{#0a0}w\color{#90f}{\bar w} = 2\cdot\color{#c00}3\,$ of numbers can refined to a unique factorization of ideals using the below ideal analog of $\rm 4NT$ = Four Number theorem.
$$  \overbrace{\color{#0a0}{(w,2)(w,3)}}^{\textstyle (\color{#0a0}w)}\overbrace{(\color{#90f}{\bar w,2)(\bar w,3)}}^{\textstyle (\color{#90f}{\bar w})} \,=\, \overbrace{(w,2)(\bar w,2)}^{\textstyle (\color{c00}2)}\overbrace{\color{#c00}{(w,3)(\bar w,3)}}^{\textstyle (\color{#c00}3)}$$
If those ideals were all principal $(x,y) = (z)$ then we could read those ideals as number gcds $\gcd(x,y)\approx z\,$ and then the above refinement is just the usual Four Number Theorem (which is true in any UFD or GCD domain, and yields uniqueness of factorizations into irreducibles).
So the significance of the nonprincipality of $(3,w)$  is that it implies that we have to pass from number gcds to ideal gcds in order to regain unique factorization.
Note: the gcd of ideals $I,J\,$ is defined to be $\,I+J,\,$ so $\,(w,2) = (w)+(2)\,$ is the ideal gcd of $(w)$ and $(2),\,$ etc. The reason for this is that the ideal sum satisfies the universal property of the gcd in rings where contains = divides for ideals (e.g. PIDs and Dedekind domains), viz.
$$\begin{align} A\supseteq B,C &\iff A\supseteq B+C,\qquad\ \text{universal property of ideal sum}\\[.2em]
\rightsquigarrow\ \ A\ \mid\ B,C&\iff A\ \mid\  B+C,\qquad\ \text{when contains = divides}\\[.2em]
A\ \mid\ B,C&\iff A\ \mid \gcd(B,C),\ \ \ \ \text{universal property of gcd}\end{align}$$
as explained in the prior linked answer.


Multiple comparison in Multiple imputation

Question: I am wondering if it is appropriate to use the term "multiple comparison problem" when applied to multiple imputation. I know that the multiple comparison problem arises when we have one set of data and ask many questions about it. Is this theoretically the same thing as having multiple data sets, and asking the same question on each dataset?
The reason I ask is because I have a MI dataset, and want to run a log rank test on each of the 50 datasets, but I don't believe that running the test on each dataset and then pooling is valid (because of the multiple comparison problem).
Comment: What exactly do you impute? The group variable or something else?
Comment: Correct. In my original dataset, the survival time and censoring indicator are fully observed, but the treatment/group needs imputation. As well, in another instance, I have that same setup, but a variable is imputed that I subset on (ie coxph(Surv(time,cens)~treat,subset=(receptor==TRUE))
Answer: I found this which might be of interest.
On another hand, I would find it more meaningful to pool the estimates of the log-hazard ratio and obtain their pooled standard error according to Rubin's rules (available, for example, here), and then conduct a t-test for the pooled estimate.
For other practical purposes, I'd also report the range of the p-values obtained form the multiple imputation, if pooling the estimate proves to be too complicated.
Comment: I am currently doing the Wald like test as you suggested in the second paragraph. My logic is that we want the pooled log rank test (from kaplan meier curves), but since this is not normally distributed, we can run a cox model (equivalent with no ties), and then to a Wald test, since the score test is the log rank test, and the wald test is asymptotically equivalent to it. I'm just afraid that. Besides, pooling on $\chi^2$ [doesn't seem to work well](http://www.biomedcentral.com/1471-2288/9/57). I'm just afraid of the theoretical soundness of this method.
Comment: Why wouldn't you run Cox models, estimate a regression coefficient (log-hazard ratio) from every imputed data set. The estimate of this, because it is a maximum likelihood estimate, has an asymptotic normal distribution. Then you pool the estimates, obtain the pooled standard error, and use that?
Comment: Sorry, I don't think I explained it well. What I did was run a cox model on the treatment only (as a proxy for the kaplan meier curve, in order to get the log rank test), then pooled. But since we can't get a score (logrank) or LR test on this pooled data, I resorted to using the pooled estimate to do a Wald test. So I did do what you said (assuming in the last sentence, you mean "use that" to mean run a wald test). I'm just wondering what others will say about this...because after all, we do have access to the log rank test (but only on the individual dataset level).
Comment: I understand now. The log-rank test is essentially the score test for a binary variable. I'll have to think more about this. Let us know if you figure something out!


Why No Chain Rule With Derivative of Inverse Functions

Question: When using the formula to find the derivative of inverse functions [if g(x)=f^-1(x), then f'(x)=1/g'(f(x))], why is it that when using the formula to solve equations, for the bottom part of the equation, g'(f(x)), we only take the derivative of 'g(x)' and just input 'f(x)' for (x), without applying the chain rule (i.e. then multiplying it by f'(x)), esp. since in this formula we're deriving g'(f(x)) with respect to 'x', not 'f(x)'? Thanks.
Comment: Please use MathJax to improve the readability of this. It's impossible to decipher otherwise.
Comment: Can you give an example of when you used the formula in this way to solve an equation? I think it would make it clearer what you're misunderstanding
Answer: We do use the Chain Rule.
You start with $g(x) = f^{-1}(x)$. Assuming that $f'(x)\neq 0$ (at the point in question) to ensure the differentiability of $g$, we can then proceed. We have:
$$g(f(x)) = x.$$
Taking derivatives with respect to $x$ on both sides and using the Chain Rule, we get
$$g'(f(x))f'(x) = 1.$$
Since $f'(x)\neq 0$, then solving for $g'(f(x))$, we get
$$g'(f(x)) = \frac{1}{f'(x)}.$$
Now: set $y=f(x)$. Then $x = g(y)$. Plugging $y$ for $f(x)$ on the left, and $g(y)$ for $x$ on the right, we get
$$g'(y) = \frac{1}{f'(g(y))},\qquad \text{where }f(x)=y.$$
Comment: just a small remark: there is infact no need to assume $f'(x) \neq 0$, because the product being equal to 1 already implies both factors are non zero
Comment: @peek-a-boo: It's badly located, but the assumption is necessary; it is implicit in the assumption that $g=f^{-1}$ is differentiable.


Slick 3.3.3 not parsing mysql datetime

Question: Im using slick 3.3.3, and i have a table with columns setup with LocalDateTime (ive also tried OffsetDateTime)
I was told slick 3.3.3 fixes the issue but no matter what I do i still get
 Text '2023-09-20 21:20:21' could not be parsed at index 10
when trying to parse this column
    def startDate = column[Option[LocalDateTime]]("start_date")

I can see in the code they are assuming in the mysql profile iso8601String, and just letting LocalDateTime or OffsetDateTime parse it on its own, but none of these natively support the above string, which is how mysql naturally stores the string. I dont understand.
Ive also tried using their custom mappers myself but I equally dont understand how to set that up
    MappedColumnType.base[Option[OffsetDateTime], String](
      localDateTime => localDateTime.map(_.format(formatter)).orNull,
      timeStr => if (timeStr == null) None else Some(OffsetDateTime.parse(timeStr, formatter))
    )

Even if i feed the implicit directly into the column like so:
def startDate = column[Option[LocalDateTime]]("start_date", O.SqlType("datetime"))(dateTimeMappingOption)

It just doesn't trigger it. Im also not quite sure how it handles optionals, if im expected to make parsers for each version and their optional or if theres some way to do that automatically.
Help is appreciated.
Answer: Here you have a migration guide for slick 3.3.
In the case of MySql says:

slick.jdbc.MySQLProfile

Java Type
SQL Type
Example SQL Literal

java.time.LocalDateTime
TEXT
'2019-02-03T18:20:28.661'

java.time.OffsetDateTime
TEXT
'2019-02-03T18:20:28.661Z'

Which means, the data type of the columns in the table must be of type TEXT. If the columns can be null, you could define the column like this
def startDate = column[Option[LocalDateTime]]("start_date")

If this example is not enough, edit your post adding how you are creating the table in the database and also show the error message you got when you execute the code

Slick 3.3.3 not parsing mysql datetime


Fourier transforming a byte array

Question: I am not so proficient in Java, so please keep it quite simple. I will, though, try to understand everything you post. Here's my problem.
I have written code to record audio from an external microphone and store that in a .wav. Storing this file is relevant for archiving purposes. What I need to do is a FFT of the stored audio.
My approach to this was loading the wav file as a byte array and transforming that, with the problem that 1. There's a header in the way I need to get rid of, but I should be able to do that and 2. I got a byte array, but most if not all FFT algorithms I found online and tried to patch into my project work with complex / two double arrays.
I tried to work around both these problems and finally was able to plot my FFT array as a graph, when I found out it was just giving me back "0"s. The .wav file is fine though, I can play it back without problems. I thought maybe converting the bytes into doubles was the problem for me, so here's my approach to that (I know it's not pretty)
byte ByteArray[] = Files.readAllBytes(wav_path);
String s = new String(ByteArray);
double[] DoubleArray = toDouble(ByteArray);
// build 2^n array, fill up with zeroes
boolean exp = false;
int i = 0;
int pow = 0;
while (!exp) {
    pow = (int) Math.pow(2, i);
    if (pow > ByteArray.length) {
        exp = true;
    } else {
        i++;
    }
}
System.out.println(pow);
double[] Filledup = new double[pow];
for (int j = 0; j < DoubleArray.length; j++) {
    Filledup[j] = DoubleArray[j];
    System.out.println(DoubleArray[j]);
}
for (int k = DoubleArray.length; k < Filledup.length; k++) {
    Filledup[k] = 0;
}

This is the function I'm using to convert the byte array into a double array:
public static double[] toDouble(byte[] byteArray) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
    double[] doubles = new double[byteArray.length / 8];
    for (int i = 0; i < doubles.length; i++) {
        doubles[i] = byteBuffer.getDouble(i * 8);
    }
    return doubles;
}

The header still is in there, I know that, but that should be the smallest problem right now. I transformed my byte array to a double array, then filled up that array to the next power of 2 with zeroes, so that the FFT can actually work (it needs an array of 2^n values). The FFT algorithm I'm using gets two double arrays as input, one being the real, the other being the imaginary part. I read, that for this to work, I'd have to keep the imaginary array empty (but its length being the same as the real array).
Worth to mention: I'm recording with 44100 kHz, 16 bit and mono.
If necessary, I'll post the FFT I'm using.
If I try to print the values of the double array, I get kind of weird results:
...
-2.0311904060823147E236
-1.3309975624948503E241
1.630738286366793E-260
1.0682002560745842E-255
-5.961832069690704E197
-1.1476447092561027E164
-1.1008407401197794E217
-8.109566204271759E298
-1.6104556241572942E265
-2.2081172620352248E130
NaN
3.643749694745671E-217
-3.9085815506127892E202
-4.0747557114875874E149
...

I know that somewhere the problem lies with me overlooking something very simple I should be aware of, but I can't seem to find the problem. My question finally is: How can I get this to work?
Comment: The question is how you are transforming the byte value to a double value? This part of the code is not shown. Do you use https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#doubleValue-- ?
Comment: I'll include the function in the code above.
Comment: Your are talking about a header, is it part of the byte array? if this is the case, you have to skip the nb of bytes of this header before reading the doubles.
Comment: The source array is not "complex".  Most algorithms produce a "complex" output that includes both "real" and "imaginary" time-domain data, however.  It's common to combine the real and imaginary values into a "magnitude" number by taking the square root of the sum of the squares (or simply treat the sum of the squares as a "power" value).  You get half as many frequency "buckets" out as the number of time-domain values you fed in -- this is due to the "Nyquist frequency".
Comment: it still is part of the byte array, it should be the first 44 bytes. I can get rid of it immediately, but it should not affect the FFT signifcantly.
Comment: @Hot Licks I found some algorithms that use a "pseudo complex" type meaning an array of tuples to store the real and imaginary part in a single array. That's what I meant by complex source arrays.
Comment: Read more closely.  Often the complex data is returned in the same array that was used to input the "real" time-domain data, since the two arrays would be the same size.  Real and imaginary output values may be stored in alternating array elements or there may be N real values followed by N imaginary values.
Comment: That's true. Both real and imaginary values get stored in a single array in alternating elements. I can easily comment out the part where the FFT algorithm is filling in the imaginary information, and then get back just an array of appropriate length, but full of 0s.
Comment: What's the reason for the  / 8 and * 8 in your code? Don't tell me it's because a double is 8 bytes, that doesn't make sense. Or is the input really an array of doubles?
Answer:
There's a header in the way I need to get rid of […]

You need to use javax.sound.sampled.AudioInputStream to read the file if you want to "skip" the header. This is useful to learn anyway, because you would need the data in the header to interpret the bytes if you did not know the exact format ahead of time.

I'm recording with 44100 kHz, 16 bit and mono.

So, this almost certainly means the data in the file is encoded as 16-bit integers (short in Java nomenclature).
Right now, your ByteBuffer code makes the assumption that it's already 64-bit floating point and that's why you get strange results. In other words, you are reinterpreting the binary short data as if it were double.
What you need to do is read in the short data and then convert it to double.
For example, here's a rudimentary routine to do such as you're trying to do (supporting 8-, 16-, 32- and 64-bit signed integer PCM):
import javax.sound.sampled.*;
import javax.sound.sampled.AudioFormat.Encoding;
import java.io.*;
import java.nio.*;

static double[] readFully(File file)
throws UnsupportedAudioFileException, IOException {
    AudioInputStream in = AudioSystem.getAudioInputStream(file);
    AudioFormat     fmt = in.getFormat();

    byte[] bytes;
    try {
        if(fmt.getEncoding() != Encoding.PCM_SIGNED) {
            throw new UnsupportedAudioFileException();
        }

        // read the data fully
        bytes = new byte[in.available()];
        in.read(bytes);
    } finally {
        in.close();
    }

    int   bits = fmt.getSampleSizeInBits();
    double max = Math.pow(2, bits - 1);

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(fmt.isBigEndian() ?
        ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);

    double[] samples = new double[bytes.length * 8 / bits];
    // convert sample-by-sample to a scale of
    // -1.0 <= samples[i] < 1.0
    for(int i = 0; i < samples.length; ++i) {
        switch(bits) {
            case 8:  samples[i] = ( bb.get()      / max );
                     break;
            case 16: samples[i] = ( bb.getShort() / max );
                     break;
            case 32: samples[i] = ( bb.getInt()   / max );
                     break;
            case 64: samples[i] = ( bb.getLong()  / max );
                     break;
            default: throw new UnsupportedAudioFileException();
        }
    }

    return samples;
}

The FFT algorithm I'm using gets two double arrays as input, one being the real, the other being the imaginary part. I read, that for this to work, I'd have to keep the imaginary array empty (but its length being the same as the real array).

That's right. The real part is the audio sample array from the file, the imaginary part is an array of equal length, filled with 0's e.g.:
double[] realPart = mySamples;
double[] imagPart = new double[realPart.length];
myFft(realPart, imagPart);

More info... "How do I use audio sample data from Java Sound?"
Comment: That was incredibly helpful to understand the reading process from a wav file, thank you.
Answer: The samples in a wave file are not going to be already 8-byte doubles that can be directly copied as per your posted code.
You need to look up (partially from the WAVE header format and from the RIFF specification) the data type, format, length and endianess of the samples before converting them to doubles.
Try 2 byte little-endian signed integers as a likely possibility.

Fourier transforming a byte array


How to handle a "self defeating" prediction model?

Question: I was watching a presentation by an ML specialist from a major retailer, where they had developed a model to predict out of stock events.
Let's assume for a moment that over time, their model becomes very accurate, wouldn't that somehow be "self-defeating"? That is, if the model truly works well, then they will be able to anticipate out of stock events and avoid them, eventually getting to a point where they have little or no out of stock events at all. But then if that is the case, there won't be enough historical data to run their model on, or their model gets derailed, because the same causal factors that used to indicate a stock out event no longer do so.
What are the strategies for dealing with such a scenario?
Additionally, one could envision the opposite situation: For example a recommender system might become a "self-fulfilling prophecy" with an increase in sales of item pairs driven by the output of the recommender system, even if the two items aren't really that related.
It seems to me that both are results of a sort of feedback loop that occurs between the output of the predictor and the actions that are taken based on it. How can one deal with situations like this?
Comment: (+1) In some analogous situations involving higher education, people talk about a model "cannabalizing itself."  College officials, using models, award financial aid to achieve certain enrollment- and financial-aid-related goals, only to find that, as a result, eventually prospective students' enrollment decisions are less and less determined by or predictable from the financial aid award.
Comment: This question is hard to answer in general, as it depends quite a bit on the situation. In case of stockouts there are good solutions, but in case of recommenders there may simply not be a big problem if your model becomes a bit prescriptive.
Answer: If you are using a model to support decisions about intervening in a system, then logically, the model should seek to predict the outcome conditioned on a given intervention. Then separately, you should optimize to choose the intervention with the best expected outcome. You are not trying to predict your own intervention.
In this case, the model could predict demand (the variable you don't directly control) and this, in combination with the stocking choice, would result in having an out-of-stock event or not. The model should continue to be "rewarded" for predicting demand correctly since this is its job. Out-of-stock events will depend on this variable along with your stocking choice.
Comment: This is exactly how it is solved in practice. A black box model that would not provide understanding of the driving factors but just predict that a stockout would happen on wednesday would have very limited use if people cannot understand the assumptions. (With a key assumption being that the model is not in place).
Comment: It is a little bit more complex than that, because in the model they used, demand signal was only one factor. But they also used other factors, based on in store conditions, to account for situations where the stock was in the store but not on the shelf (For example in the backroom, or at one of the cash registers or service desks because the customer changed their mind at the last minute). For that, they were not using just demand but other causal drivers as well.
Comment: @DennisJaheruddin: On the other hand, make a model that can predict when the product will be out of stock despite a timely re-order and you can make a killing.
Comment: @Alex that complexity just amounts to a network of simple supply/demand mechanisms governing different locations, whether or not the model represents them explicitly. The model's objective is more accurately described as predicting stock levels, not demand, but that only becomes relevant if you're specifically considering there to be uncontrolled factors in both the supply and demand at the point in the network (the shelf) where the stock levels matter. Interventions like ordering more stock or having staff replenishing shelves more regularly still need to be factors in the model.
Comment: You might want to mention the possibility that demand varies with stock.
Answer: There are two possibilities by which an out-of-stock (OOS) detection model might self-derail:

The relationship between inputs and OOS might change over time. For instance, promotions might lead to higher OOS (promotional sales are harder to predict than regular sales, in part because not only average sales increase, but also the variance of sales, and "harder-to-predict" translates often into OOS), but the system and its users might learn this and lay in additional stock for promotions. After a while, the original relationship between promotions and OOS does not hold any more.
This is often called "model shift" or similar. You can overcome it by adapting your model. The most common way is to weight inputs differently, giving lower weight to older observations.

Even if the relationship between a predictor and OOS does not change, the predictor's distribution might. For instance, multiple days with zero sales of a particular stock keeping unit (SKU) might signal an OOS - but if the model performs well, then OOS might be reduced across the board, and there might simply not be as many sequences of zero sales.
Changes in the distribution of a predictor should not be a problem. Your model will simply output a lower probability of OOS.

In the end, you probably don't need to worry overmuch. There will never be zero OOS. Feedback mechanisms like the ones above do occur, but they will not work until OOS are completely eradicated.

Some pending OOS may simply not be avertable. "I have one unit on the shelf and will probably face a demand for five over the coming week, but the next delivery is only due a week from today."
Some OOS will be very hard to predict, even if they are avertable, if they had been known in time. "If we had known we would drop the pallet off the forklift and destroy all the product, we would have ordered another one."
Retailers do understand that they need to aim for a high service level, but that 100% are not achievable. People do come in and buy up your entire stock on certain products. This is hard to forecast (see above) and sufficiently rare that you do not want to fill up your shelves on the off chance this might happen. Compare Pareto's law: a service level of 80% (or even 90%) is pretty easy to achieve, but 99.9% is much harder. Some OOS are consciously allowed.
Something similar to Moore's law holds: the better ML becomes, the more expectations will increase, and the harder people will make life for the model. While OOS detection (and forecasting) algorithms improve, retailers are busy making our life more difficult.

For instance through variant proliferation. It's easier to detect OOS on four flavors of yoghurt than on twenty different flavors. Why? Because people don't eat five times as much yoghurt. Instead, pretty much unchanged total demand is now distributed across five times as many SKUs, and each SKU's stock is one fifth as high as before. The Long Tail is expanding, and signals are getting weaker.
Or by allowing mobile checkout using your own device. This may well lower psychological barriers to shoplifting, so system inventories will be even worse than they already are, and of course, system inventories are probably the best predictor for OOS, so if they are off, the model will deteriorate.

I happen to have been working in forecasting retail sales for over twelve years now, so I do have a bit of an idea about developments like this.

I may be pessimistic, but I think very similar effects are at work for other ML use cases than OOS detection. Or maybe this is not pessimism: it means that problems will likely never be "solved", so there will still be work for us even decades from now.
Comment: I especially agree with your last comment. The worst-case version of this scenario seems like the starting point for a full employment / no free lunch theorem. Which is what makes this an interesting question IMO!
Answer: Presumably you can track when restock events happen. Then it's just a matter of arithmetic to work out when the stock would be depleted had the model not been used to restock inventory.
This assumes that any positive stock level is independent of the level of sales. A commenter says that this assumption doesn't hold in reality. I don't know either way -- I don't work on retail data sets. But as a simplification, my proposed approach permits one to make inferences using counterfactual reasoning; whether or not this simplification is too unrealistic to give meaningful insight is up to you.
Comment: This helps us because it gives you a counterfactual probability of a stockout had the restock not occurred. OP is asking "how to deal with" the fact that a model that reduces occurrences of stock outs will not have as many occurrences of stockouts available in the raw data. My point is that you can make inferences about the counterfactual occurrence of stockouts, and use that as a surrogate. What kind of help did you want?
Comment: @Sycorax: You assume that purchaser behavior is not influenced by the number of items visible on the shelf.  That's an invalid assumption.  The influence may be weak, but it's not absent.
Comment: I guess I don't see how this answers the question. The problems as I understand them are: (a) After implementing the model in production, the optimal prediction function for stockouts is now completely different than it was before, because we changed the data distribution; (b) The better our model is, the more rare stockout events will become, and therefore the more difficult it will become to accurately predict them going forward. Knowing "when the stock would be depleted had the model not been used to restock inventory" is neither here nor there because the model is in production from now on
Comment: @JakeWestfall This type of analysis is called counterfactual reasoning. If you know the inventory at all times, and you know when it's restocked, then you can create a counterfactual that supposes restocking didn't occur: just subtract the restock from the inventory after the restock occurred. Now you have a time series that reflects the supposition you never restocked. Carry this time-series forward until stockout. Now you know when a stockout would have occurred without restocking. How does this counterfactual have a different data distribution?
Comment: I understand all that. What I don't understand is how this solves the issues raised in the OP. For example, suppose a strong predictor of stockout is whether it's the first day of a month (when many people get paid). Using our new model, we can now avoid these stockout events by preemptively ordering more units near the end of each month. So now "first day of the month" will no longer be a useful predictor of stockouts going forward. We could indeed compute the counterfactual probability of a stockout on the first of the month had we not preemptively ordered, but how exactly does this help us?
Comment: Wait-- "use those as a surrogate"? Are you suggesting we'd _fit the model to the simulated counterfactual_?? As for the kind of help the OP wants: in the stockout example this is rather a "first world problem" and a response of "you should be so lucky to have this problem" may be good enough. But the recommender system example in the OP seems more serious. Or consider a model to find the price that maximizes profit. We estimate the best price and then "set it and forget it." But now we can't detect if changing market conditions alter the optimal price, because we're no longer varying the price
Comment: These all have in common that we used the model predictions to make changes to the data generating process, thus changing the distribution of what's being modeled. I interpret this to be the core issue the OP is asking about. The specific examples highlight different potential problems that may arise from this, and it's unlikely that they can all have the same solution. But it's not clear (at least to me) how _any_ of these kind of problems are addressed by the sort of counterfactual calculation you described in your answer.
Comment: I take the question to be "how can you make predictions about the new distribution," not "how can you emulate the old distribution." I suppose in some cases, continuing to train the model on the old distribution will help with the new distribution. Are you saying that it's *guaranteed* to?
Comment: @senderle I'm just saying that stock outs can be easily modeled using counterfactuals. Not all problems are like stock outs, but this does address a question that OP asked.
Comment: @BenVoigt Ah! Thank you! At last, someone was able to **explain** why this model might not be realistic. I didn't know that people were influenced by stock levels on shelves. I suppose in an e-commerce setting where stock levels are not as visible, this effect would be smaller than a physical commerce setting.
Comment: @Sycorax: E-commerce settings sometimes point out stock levels.  I know when searching for plane tickets, quite a few sites will include a statement along the lines of "One 2 seats left at this price!"  Hard to confirm that's true, but there's reasoning behind that statement.  A very obvious case where stock levels affect a purchase decision is when there's a buy-1-get-1-free offer, but only one item remaining.
Comment: @BenVoigt Selling out seats on aircraft seems like a fundamentally different problem, since the airplane can't be "restocked" with additional seating.
Answer: Your scenario bears a lot of resemblance to the Lucas Critique in economics.  In machine learning, this is called "dataset shift".
You can overcome it, as @Sycorax says, by explicitly modeling it.
Answer: One thing to remember is that ML is an instrumental goal. Ultimately, we don't want to predict out of stock events, we want to prevent out of stock events. Predicting out of stock events is simply a means to that end. So as far as Type II errors are concerned, this isn't an issue. Either we continue to have OOSE, in which case we have data to train our model, or we don't, in which the problem that the model was created to address has been solved. What can be a problem is Type I errors. It's easy to fall into a Bear Patrol fallacy, where you have a system X that is built to prevent Y, you don't see Y, so you conclude that X prevents Y, and any attempts to shut X down are dismissed on the basis "But it's doing such a good job preventing Y!" Organizations can be locked into expensive programs because no one wants to risk that Y will come back, and it's difficult to find out whether X is really necessary without allowing that possibility.
It then becomes a trade-off of how much you're willing to occasionally engage in (according to your model) suboptimal behavior to get a control group. That's part of any active exploration: if you have a drug that you think is effective, you have to have a control group that isn't getting the drug to confirm that it is in fact effective.


Verificar se há assinaturas ativas com App Billing Android

Question: Olá, estou implementando assinaturas em meu app que servirá para remover os anúncios. Tenho uma activity que está realizando as compras e está funcionando.
Quando a compra é finalizada com sucesso eu salvo um código no shared preferences para me informar que há uma assinatura ativa. No entanto me surgiu uma dúvida, como a compra é realizada fora da activity principal, caso o usuário não renove a assinatura quando ela expirar o código salvo no shared preferences sempre vai estar como comprado.
Pensei em fazer uma nova conexão com o Billing na activity principal, assim toda a vez que o aplicativo fosse aberto seria checado se há assinatura ativa e faria um edit no código no shared preferences.
No entanto, fiquei meio confuso no sentido de saber quais partes do código do app billing utilizar para fazer essa checagem.
Como posso fazer essa verificação utilizando o menos possível de código?
Desde já eu agradeço.
Comment: Cada caso é um caso. Salvar somente no sharedpreference talvez não seja mesmo mesmo uma boa opção. Por exemplo em um app como spotify, se eu usar ele no tablet e no celular, eu teria que fazer 2 compras de assinaturas ou somente uma? Acredito que você deve armazenar essa informação externamente para que possa usar em diversos dispositivos. Mas claro, vai depender do seu plano de negócio.
Comment: Salvo se eu estiver enganado, se você compra alguma coisa em um determinado app e quer usar em dois dispositivos, acho que teria que utilizar a mesma conta da play store  em ambos. Obrigado pela resposta.
Answer: tive um caso parecido com isso e fiz assim ó:
<code>public class MainActivity ... {
    // ...
    private boolean mIsPremium = false;
    static final String SKU_PREMIUM = "premium";
    private IabHelper mHelper;
    private String payload = "";
    // ...
    void complain(String message) {
        alert("Error: " + message);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isEmulator = Build.MODEL.contains("google_sdk") || Build.MODEL.contains("Emulator") || Build.MODEL.contains("Android SDK");
        final String BASE64_KEY = "SUA CHAVE PUBLICA BASE_64";
        if (!isEmulator) {
            mHelper = new IabHelper(this, BASE64_KEY);
            mHelper.enableDebugLogging(true);
            Log.d(TAG, "Starting setup.");
            mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                public void onIabSetupFinished(IabResult result) {
                    Log.d(TAG, "Setup finished.");
                    if (!result.isSuccess()) {
                        complain("Problem setting up in-app billing: " + result);
                        return;
                    }
                    if (mHelper == null) return;
                    Log.d(TAG, "Setup successful. Querying inventory.");
                    mHelper.queryInventoryAsync(mGotInventoryListener);
                }
            });
        }
    }
    // ...
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_REQUEST) {
            if (mHelper == null)
                return;
            if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
                super.onActivityResult(requestCode, resultCode, data);
            } else {
                Log.d(TAG, "onActivityResult handled by IABUtil.");
            }
        }
    }
    // ...
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            Log.d(TAG, "Query inventory finished.");
            if (mHelper == null) return;
            if (result.isFailure()) {
                complain("Failed to query inventory: " + result);
                return;
            }
            Log.d(TAG, "Query inventory was successful.");
            Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
            mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
            Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
        }
    };

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

            if (mHelper == null) return;

            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                return;
            }

            Log.d(TAG, "Purchase successful.");

            if (purchase.getSku().equals(SKU_PREMIUM)) {
                // bought the premium upgrade!
                Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
                alert("Thank you for upgrading to premium!");
                mIsPremium = true;
                // aqui eu passo verdadeiro para a variável mIsPremium uma única vez e somente se o cara pagou mesmo
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }
    // ...
    boolean verifyDeveloperPayload(Purchase p) {
        payload = p.getDeveloperPayload();
        return true;
    }
}
</code>
No meu caso um app que adquire a versão premium, (qual ao seu remover ads), eu apenas verifico se o meu item na loja foi comprado e se sim, passo o valor "true" para a variável mIsPremium, depois quando eu vou chamar qualquer outra activity partindo da minha mainactivity eu passo o valor dessa variável mIsPremium em um Bundle para ler nessa activity que estou chamando.
Por esse metodo voce verifica direto na assinatura do usuário para o app especifico e ele ja faz o controle normalmente, sem precisar gravar isso nas preferences do aparelho.
Espero que resolva para voce, aqui funciona blzura, tirando os usuarios "nós cegos" que sempre dizem que pagaram e os reclamas não sumiram, mas todos sabemos que esses não pagaram nada...
Muita sorte ai
Comment: Obrigado pela resposta. Vou fazer uns testes aqui e retornarei.


Can someone explain the relation between random variables and continuity?

Question: For example, if we look at distributions with $P(T<5)$ and $P(T\le5)$, the two notations equal due to continuity. I don't understand how continuity is related to the reason these two notations are equal.
Comment: In a continuous distribution $T$ with density function $f(t)$, $\Pr(T=5)=\int_5^5 f(t)\,dt=0$.
Comment: If you have a continuous random variable, then $P(T<5)$ and $P(T \leq 5)$ are equal, but for a discrete random variable they may well be different.
Comment: My answer below explains why we need not assume there's a density function.
Answer: Optional prefatory remarks:
"Continuity" is a bit ambiguous, in that it can mean either of two things:

The cumulative distribution function is continuous; or
There is a probability density function.

The second point means that
$$\Pr(T\text{ is in some specified set of numbers}) = \int\limits_{\begin{array}{c} \text{that set} \\ \text{of numbers} \end{array}} f(t)\,dt,$$
where $f$ is the density function.
The second condition is sometimes called "absolute continuity".  Whenever it is true, the first condition---that the C.D.F. is continuous---is also true.  But there are some cases where the first condition is true but the second is not.  The so-called Cantor distribution is one of those.
But we really only need the first conditions---that the C.D.F. is continuous---in order to show that $\Pr(T<t)=\Pr(T\le t)$.
The actual answer begins here:
Observe that if $s<t$ then
$$
\begin{align}
\Pr(T \le t) & = \Pr(T\le s\text{ or }s<T\le t) \\[8pt]
& =\Pr(T\le s)+\Pr(s<T\le t),
\end{align}
$$
and therefore
$$
\begin{align}
\Pr(s<T\le t) & = \Pr(T \le t) - \Pr(T\le s) \\[8pt]
& = F(t) - F(s),
\end{align}
$$
where $F$ is the C.D.F.
Continuity of $F$ implies that you can make $F(t)-F(s)$ as close to $0$ as you want by making $s$ close enough to (but still less than) $t$.  Thus you can make $\Pr(s<T\le t)$ as close to $0$ as you want by pushing $s$ toward $t$ from below (or "from the left", if you think of the usual lay-out of the $t$-axis).  Since $\Pr(T=t)\le\Pr(s<T\le t)$, it follows that you can make $\Pr(T=t)$ as close to $0$ as you want by pushing $s$ toward $t$.  But $\Pr(T=t)$ doesn't change as $s$ moves toward $t$.  Consequently $\Pr(T=t)$ must be $0$.  Now notice that
$$
\begin{align}
\Pr(T\le t) & = \Pr(T<t\text{ or }T=t) \\[8pt]
& = \Pr(T<t)+\Pr(T=t).
\end{align}
$$
Since we've shown that $\Pr(T=t)$ is $0$, it follows that
$$
\Pr(T\le t) = \Pr(T<t).
$$


Java Hash Table giving NullPointerException when checking if string is not null

Question: I have a hash table that is implemented using a node array, where the node contains a string and a boolean to check if there was a string there before for when searching for an element after elements have been deleted. I am getting a nullpointerexception at a place that I don't understand how I possibly could, in this line:
while(T.getElement(place) != null)

This is my code to do the loop that is throwing the exception.
Get Element is here:
public String getElement(int index){
        if (index != lastProbed)
            probeCount++;
        lastProbed = index;
        return table[index].str;
}

And this is my node class:
class Node {

boolean hadStr;
String str;

public Node() {
    str = null;
    hadStr = false;
}

}

I don't understand how I could be getting a null pointer in a place where I am just checking if a string is null.
Comment: what happens if `table[index]` is null? then `return table[index].str;` will give you a NPE
Comment: That actually helps a lot, thank you! Is there a better way to solve this rather than making a for loop in the constructor to initialize every node in my array?
Answer: Why not
public String getElement(int index){
        if (index != lastProbed)
            probeCount++;
        lastProbed = index;
        if (table[index] != null) {
           return table[index].str;
        }
        return null;
}


If $\frac{p^{n+1}-1}{p-1} = 2^{a}$ then $n+1 = 2^{b}$

Question:
Let $p>2$ prime, $n$ positive integer. Knowing $\frac{p^{n+1}-1}{p-1} = 2^{a}$ for some $a\in \mathbb{N}$ show that $n+1$ is also a power of $2$.

To be honest I have no idea how to prove it. I know that $\frac{p^{n+1}-1}{p-1} = 1 + p + p^{2} + ... + p^{n}$ so $n$ has to be odd. First few examples that work are $p=3, n=1$; $p=7, n=1$; $p=31, n=1$ so maybe $n$ has to be equal to $1$?
Comment: Does this answer your question? [If $\sigma(p^m)=2^n$ for prime $p$,then $m=1$ and $n$ is prime](https://math.stackexchange.com/questions/748128/if-sigmapm-2n-for-prime-p-then-m-1-and-n-is-prime) - found using an [Approach0 search](https://approach0.xyz/search/?q=OR%20content%3A%24%5Cfrac%7Bp%5E%7Bn%2B1%7D-1%7D%7Bp-1%7D%3D2%5E%7Ba%7D%24%2C%20OR%20content%3A%24n%2B1%3D2%5Eb%24%2C%20OR%20content%3Aprime&p=1). Note that $\sigma(p^n)=1+p+\ldots +p^{n}=\frac{p^{n+1}-1}{p-1}$, as shown in the dup. question. As it asks to prove the equivalent of $n=1$ there, this shows $n+1=2$ is a power of $2$.
Answer: Suppose that $n+1$ is not a power of $2$. Then $n+1$ can be written $n+1 = kl$, where $k$ is an odd integer at least $3$. So on the one hand, $\frac{p^{n+1}-1}{p^l-1}=\frac{p^{kl}-1}{p^l-1}$ is an integer, and on the other hand, as $p-1$ divides $p^l-1$, it follows that $\frac{p^{n+1}-1}{p^l-1}$ divides $\frac{p^{n+1}-1}{p-1}$. So to show that $\frac{p^{n+1}-1}{p-1}$ cannot be a power of $2$ for $n+1$ as above, it suffices to show that $\frac{p^{kl}-1}{p^l-1}$ is an odd integer at least $3$ [make sure you see why]. But then for such $n+1$:
$$\frac{p^{lk}-1}{p^l-1}$$ $$= 1 + p^l + p^{2l} + \ldots +p^{(k-1)l}.$$
As the RHS is the sum of an odd number $k \ge 3$ of terms, it follows that $\frac{p^{lk}-1}{p^l-1}$ is indeed an odd integer at least $3$ for such $n+1$. So as noted above, this implies that $\frac{p^{n+1}-1}{p-1}$ cannot be a power of $2$ for such $n+1$, and so the result follows.


Google Search Console API, URL Inspection

Question: I want to use the Google Search Console API to check if an url is indexed.
<code>$client = new Google_Client();
$client->setAuthConfig( json_decode( $json, true ));
$scopes = ['https://www.googleapis.com/auth/webmasters','https://www.googleapis.com/auth/webmasters.readonly'];
$client->addScope($scopes);
$searchConsole = new Google_Service_SearchConsole($client);
$urlRequest = new \Google_Service_SearchConsole_InspectUrlIndexRequest();
$urlRequest->setInspectionUrl($url);
$urlRequest->setLanguageCode('fr');
$urlRequest->setSiteUrl('sc-domain:'.$url);
$urlIndex = new \Google_Service_SearchConsole_Resource_UrlInspectionIndex();
</code>
The problem is when I try to create an UrlInspectionIndex to make an inspect() with $urlRequest I get an Exception in the constructor of vendor/google/apiclient/src/Service/Resource.php:66
I don't know what those arguments are. Hope anyone would help me, thank you.
Answer: I got it to work like this:
<code>$service = new Google_Service_SearchConsole($client);
$query = new Google_Service_SearchConsole_InspectUrlIndexRequest();
$query->setSiteUrl("https://$domain/");
$query->setInspectionUrl("https://$domain/$page");
$results = $service->urlInspection_index->inspect($query);
</code>


Ruby on Rails: Check whether a word is matched to any attributes in database

Question: I am doing my Ruby on Rails project now. PostgreSQL is used as database.I have a table called pets, where stores all pets information(name, gender, age, etc.), which are shown as follows.
{Pet id: 1, name: "Jean", gender: "Female", pet_type: "Cat", age: "0", body_weight: 1, breed: "Abyssinian", created_at: "2015-04-26 23:50:01", updated_at: "2015-04-26 23:50:01"}
{Pet id: 2, ...}
...
{Pet id: 10, ...}
Now I am going to check whether there is a Abyssinian breed cat in table pets. If there is, return the Pet id, otherwise return "no such cat found".
Is there anyone know how to do this? Thanks in advance.
Comment: I tried @infused answer, output = Pet.find_by(breed: "Abyssinian", pet_type: "Cat") || "no such cat found". It will return the first pet where breed == "Abyssinian".
Answer: To find the first pet where breed == "Abyssinian":
pet = Pet.find_by(breed: "Abyssinian", pet_type: "Cat")

So, to combine that with your required logic:
pet = Pet.find_by(breed: "Abyssinian", pet_type: "Cat")
output = pet ? pet : "no such cat found"

You could simply this even further to:
output = Pet.find_by(breed: "Abyssinian", pet_type: "Cat") || "no such cat found"

Comment: you miss `pet_type: "cat"` I thik
Comment: Thanks @infused! I tried it and it works! I was wondering what I should do to get all pets whose breed is Abyssinian rather than the first pet?
Comment: To find them all use: `Pet.where(breed: "Abyssinian", pet_type: "Cat")`
Answer: Is "breed" a field in the pets table? If so, you could do something like:
if Pet.exists?( breed: "Abyssinian" )
  x = Pet.where( breed: "Abyssinian" )
else
  x = "No such cat found"
end

The danger in this is that there could be multiple Pets returned. If you want to find one and only one, you could do
if Pet.where( breed: "Abyssinian" ).count == 1

instead.
REVISION
@infused's answer is more succinct, I would encourage an implementation similar to his, assuming a breed column exists.
Comment: Thanks for your prompt answer! Breed is a field in the pets table. I was wondering whether there is a way to return all pets id whose breed is Abyssinian?


What are the odds of predicting a series of coin tosses to a certain percentage?

Question: So I'm stuck on a problem. It's related for some work I'm doing but I'll keep it simple here.
Here's the scenario: I flip a coin 11 times and I record the results without telling you what the results are. Now you have to try and predict what I tossed.
What are the odds that you will guess 8 out of those 11 tosses correctly?
Comment: Before solving this we need a bit more information. Do we assume the coin is fair? And do you care about the order of the tosses, that is if you tossed HTH and I guess HHT is that still considered correct?
Comment: @MaartenPunt yes the coin is fair, and the order matters.
Comment: (1) Do the 8 correct guesses need to be in sequence, or are any 8 out of the 11 positions fine? (2) Are you interested in *exactly* 8 correct guesses, or *at least* 8 correct guesses (i.e., would 9 correct guesses count or not)?
Comment: @StephanKolassa 1: the 8 can be in any sequence
2: I'm interested in *exactly* eight correct guesses.
Comment: https://en.wikipedia.org/wiki/Binomial_distribution
Answer: If your coin is fair and you are not interested in which 8 out of your 11 guesses are correct, then you are interested in

the number of successes in a sequence of $n$ independent experiments, each asking a yes–no question, and each with its own boolean-valued outcome

which is modeled by a binomial distribution, in your case with parameters $p=0.5$, $n=11$ and $k=8$. The probability you are interested in is given by the probability mass function:
$$ {n\choose k}p^k(1-p)^{n-k}$$
You can calculate this, or use built-in tables or probability density functions from your favorite statistical package. Finally, because I'm never sure I haven't made an error, you can simulate. If all three approaches give the same result, things are looking good. In R:
<code>> choose(11,8)*0.5^8*(1-0.5)^3
[1] 0.08056641
>
> dbinom(8,11,0.5)
[1] 0.08056641
>
> nn <- 1e6
> set.seed(1)
> sims <- matrix(runif(nn*11)<0.5,nrow=nn)
> sum(rowSums(sims)==8)/nn
[1] 0.080538
</code>
Comment: Given that the OP is asking for odds rather than probability shouldn't it be 0.08/0.92, that is roughly 9:100?
Comment: @MaartenPunt: good point, thanks. Considering that the OP accepted the answer, I assume (s)he either used "odds" in a non-technical sense, or did intend the technical sense and was able to work out the odds from the probability himself.


Extracting alt attribute from image into div (Gallery description purpose)

Question: So i have this mini gallery. This is how i want it to work:
img BIG IMAGE img
div description div
img thumbnails img
Everyting works fine except i don't know if i'm extracting alt value correctly into div. Code inc:
HTML SAMPLE:

Jquery:
$(document).ready(function(){

 $("h2").append('')

 $(".thumbs a").click(function(){

   var largePath = $(this).attr("href");
   var largeAlt = $(this).attr("title");

   var altText = $(this).attr("alt");

   $("#largeImg").attr({ src: largePath, alt: largeAlt });

   $("div#opis").html(altText);

   $("h2 em").html(" (" + largeAlt + ")"); return false;
 });

});

Thanks in advance!
Comment: And use english when naming your classes/ids or you will make other (god save non polish developers) who could work with your code cry - consistence is even more important - everything english or polish - your choice just be consistent.

Even if now you aren't working with foreign developers it's a good habit to practice from start. English is common language for all programmers.
Comment: When you find yourself writing `$(this)` repeatedly, you might consider doing it once and caching the result instead. *Every* call to `$(...)` results in several function calls and a memory allocation or two. Three calls in a click handler isn't really going to matter, but it's a bad habit to get into. Try `var $this = $(this);` then `$this.attr("href")`, `$this.attr("title")`, etc. FWIW. (If you're creating closures, you may want to clear that var at the end of your function, e.g. `$this = undefined;` -- unless, of course, it's useful to the closures.)
Answer: Using $(this).attr("alt") in that context is returning the alt attribute of the a tag, not the child img.
Try this:
var altText = $(this).find('img').attr('alt');


Recreating multidimensional tensor from one dimensional array

Question: I am trying to manually decode the output of a Yolo object detection ONNX model in C#. Netron describes the output as follows:
type: float32[1,3,80,80,19]

But in C# code the model output I receive is a single dimensional array:
 float[364800]

and 364800 = 1 * 3 * 80 * 80 * 19
My programming experience has been with VB.NET and a smattering of C#. I'm new to ML and object detection  and I don't have much experience working with tensors or Python, hence I am trying to build a solution in C#.
Can somebody point me in the right direction in reconstructing the multidimensional tensor array so I can iterate over the results? How would the data in the one dimensional array be stored?
Also I wondered if I might be doing it the hard way. If there is some kind of tensor manipulation tool in the .NET world I would be happy to know about it.
Thanks for any help!
Answer: If you call Tensor.Dimensions.Length you will get the float32[1,3,80,80,19] value. For iteration over the tensor and processing, here is an example of iterating to divide and then return a new tensor result:
    public static Tensor<float> DivideTensorByFloat(float[] data, float value, int[] dimensions)
    {
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = data[i] / value;
        }

        return CreateTensor(data, dimensions);
    }

    public static DenseTensor<T> CreateTensor<T>(T[] data, int[] dimensions)
    {
        var tensor = new DenseTensor<T>(data, dimensions);
        return tensor;
    }

    DivideTensorByFloat(Tensor.ToArray(), value, Tensor.Dimensions.ToArray());

Answer: You can use numpy to work with arrays in python.
install
pip install numpy

Inorder to convert from 1D to multi-dimentional array you can conver using the following way:
oned_array = np.arange(6)
oned_array:
array([0,1,2,3,4,5])

multi_dimn_array = oned_array .reshape((3,2))
multi_dimn_array :
array([[0, 1],
       [2, 3],
       [4, 5]])

Useful Resource:

Numpy
Scipy
Comment: Thanks but I'm not clear how this applies in C#, these are libraries in Python


Best practice to migrate Server 2003 x64 to Server 2008 x64

Question: We have 2 Domain Controllers running Windows Server 2003 R2 x64. DC1 has the FSMO roles and DC2 acts as a 'secondary' DC. Both are Global Catalog servers.
DC1 has these roles installed: Domain Controller, DNS Server, Application Server (IIS), File Server and Print Server.
DC2 has these roles installed: Domain Controller, DNS Server, Application Server (IIS) and File Server.
There is also a trust relationship between the current domain of these two DCs and a different one which is located in the same location.
What we want to achieve is to upgrade both servers to Windows Server 2008 R2 with a clean install process on both machines, not a direct upgrade from 2003 R2 to 2008 R2.
I am looking for the best practice to achieve that goal. Can you please help me understand the best way to achieve that?
Which server do I have to upgrade first? How can I preserve all of the settings and data from each role?
Answer: What I have noticed is that you are running file-services and IIS Services on a Domain Controller, this should not be done! I suggest you to separate those services, if possible IIS and file-services as well.
Furthermore have you thought about a Domain Migration to a single domain and separating your Sites via OUs / Sites&Services ?
About the DCs you could install and prmote them to a GC while the old masters stays available and integrate them into the Domain, next step would be moving the FSMO rule to the new master(s).
Afterwards you simple demote the old DCs to member Servers, in case of trouble there are very good KBs if demoting fails in technet.
Your settings should be preserved.
Feel free to contact me about detailed help in a private message
Comment: File Services is not used anymore, thanks for the suggestion.
The other domain in managed by another team and it isn't an option to migrate the two of them into one.
So, what are you saying is this: Demote DC2 to member server, install Server 2008 R2, transfer the FSMO Roles and after the replication do the same process for the DC1. Right?
Comment: First install the new DCs, then promote them, then transfer, then demote,
About team management you can use Active Directory delegation to delegate/customize the permissions on the specific OUs
Comment: But, we won't add any new DCs to the domain. Everything will happen on the same two existing servers.
Comment: And that is the bad idea IMHO...
Install 2 other DCs, then promote, then transfer.
You should prefer a clean installation for a DC, and not an upgraded one.


Are symmetry operations commutative or not?

Question: My book (Bishop, David m. Group Theory and Chemistry) states, that $C_3C_3^{-1} = E$ and $C_3^{-1}C_3 = E$.

After that it shows an non-commutative example:

Ok. But then they say that $(PQ)R = P(QR) = PQR$.
Actually, the operations are always applied from right to left, but here the sequence $Q, P, R$ and $R, Q, P$ is apparently the same. This is what confuses me a little, although it was shown before that it generally depends on the sequence of the linkage.
Comment: Yes, commutativity doesn't always hold but associativity is actually a requirement for a group operation.
Comment: Where are $P$,$Q$, and $R$ in your picture? I can't see them
Comment: After you ask a question here, if you get an acceptable answer, you should "accept" the answer by clicking the check mark $\checkmark$ next to it. This scores points for you and for the person who answered your question. You can find out more about accepting answers here: [How do I accept an answer?](http://meta.math.stackexchange.com/questions/3286/), [Why should we accept answers?](http://meta.math.stackexchange.com/questions/3399/), [What should I do if someone answers my question?](https://math.stackexchange.com/help/someone-answers).
Answer:
Are symmetry operations commutative or not?

No.
Consider the rotations and reflections of an equilateral triangle. It is not difficult to see that, if we label the vertices of the triangle by $1,2,$ and $3$, then the symmetries correspond to the group $S_3$ of permutations of three elements. (In fact, this can be made precise: the group $D_3$ of symmetries of an equilateral triangle is isomorphic to $S_3$.)
However, $(12)(23)=(312)$, whereas $(23)(12)=(132)\neq (312)$.
Thus $D_3$ is not abelian.
But all groups are associative, which is to say that for all $a,b,c\in G$, where $G$ is a group, we have
$$a(bc)=(ab)c.$$
Comment: The pictures deal with the product of two symmetries, so the question about associativity (placing parentheses) never comes up. In the first picture you have the product of a rotation and its inverse. That comes out the identity in either order. In the second picture you have the product of a reflection and a rotation. There the order matters. The product operation is not commutative. But it is associative.
Comment: You can't move the $b$ in front of the $a$ just be rearranging the parentheses. The parentheses tell  you the order in which to do the arithmetic operations, not the order in which the operands appear. Different orders of operations produce the same answer. Sometimes some orders are more efficient to compute than others.
Comment: Ok, so i can put the brackets how i want to. Two questions come to my mind, why use clamps at all then? And don't the brackets still change the sequence? a(bc)=(ab)c = bca = abc
Comment: A priori, @iwab, parentheses are needed, but it is not too difficult to show by induction that, due to associativity (being an axiom), we can place them anywhere without issue, and even omit them entirely. Some algebraic structures require parentheses, though, like some quasigroups.
Comment: @EthanBolker "Different orders of operations produce the same answer."- but doesn't the second image in my question (b) show the exact opposite? So i'm a little unsure, when brackets are set they tell me to apply these operations (inside the bracket from right to left) first and then the rest, thats why I dont understand that a(bc)=(ab)c is true for symmetrie operators.
Comment: I think you have commutativity and associativity confused, @iwab. Please revise their definitions.


How do I route traffic to website using a spcified network connection on Windows

Question: I want to route all traffic to a website over my wireless connection while the rest of the traffic using my lan.
What I tried was first finding out the IP address of the website I want to go to.
For example, lets say pandora.com. I found it resolves to 188.8.131.52. I have entered that entry into my hosts file. I then added that route using route add 220.127.116.11 mask 255.255.255.255 WirelessIP.
It doesn't seem to work however. Instead of using the IP address, is there a way that I can just say.. this URL to route over that connection?
Does anyone know of a program that I can install that will do this.. possibly some sort of proxy or a software load balancer that can do this?
Answer: What you did with the route add is exactly what you should have done, but I'm not quite sure what you did with the HOSTS file. You should not have needed to add anything to your HOSTS, so get rid of it.
The command you need to run to get the static route to work correctly is:
<code>route add 220.127.116.11 mask 255.255.255.255 x.x.x.x
</code>
where x.x.x.x is your wifi IP address. Add a -p to that command to make it persistant (i.e. you don't have to add it each time you restart).
I've done this multiple times and it's worked just fine.


using squeeze theorem to show limit

Question: I have a function $g(x)=x^4 \cos(2/x)$.
I have to use Squeeze Theorem to show $\lim\limits_{x\to0}g(x)=0$.
Usually all the questions I have done up till now on Squeeze theorem have provided me with 3 functions, 1 function is less than another. 1 function is the highest of them all. and 1 functino is in between the 2 functions. From there I have been able to prove the limit with squeeze theorem but in here I have no other function so what do I do here?
Comment: Your job is to find the upper and lower functions.  Noting that $\cos(x)$ is bounded between two values should help you get started with finding the appropriate functions.
Answer: You can use the fact that $\lim\limits_{x\to a}f(x) = 0$ iff $\lim\limits_{x\to a}|f(x)| = 0$, so in your case: $0\leq|f(x)| = x^4|\cos{\frac2x}|\leq x^4$ Since both $0$ and $x^4$ goes to zero with $x\to 0$ you obtain what you want. In the case you don't want to deal with the absolute value - just note that $-x^4\leq x^4\cos\frac2x\leq x^4$ since $-1\leq\cos \frac2x\leq1$ for all $x\neq 0$ as JavaMan has pointed in his comment.
Answer: Since $x^4$ is never negative, you can say that since $-1\le\cos(2/x)\le1$, it follows that $-x^4\le x^4\cos(2/x)\le x^4$.  There you have your three functions.

using squeeze theorem to show limit


MATLAB: solving 1st order hyperbolic equation in 2 spacial dimensions

Question: According to wikipedia, the equation
$$\psi_t + \mathbf{u} \cdot \nabla \psi =0$$
is hyperbolic. However, when I want to solve it in MATLAB using the pde toolbox (link), the general formula for a hyperbolic equation appears to be:
$$\psi_{tt} - \nabla \cdot (C\nabla\psi)+a\psi=f$$
Which is hyperbolic yes, but second order. I can't seem to find a way how to solve a simpler, 1st order problem. Maybe there is some way to transform it? I can't find anything in the toolbox documentation.
Answer: I don't think there is a way to use the toolbox (see here), but it should be simple to implement a finite difference method by hand:
Write
$$
\psi_t = -u_1 \psi_x - u_2 \psi_y,
$$
then discretize (I'll use the first-order forward difference in $t$, and the first-order centered difference in $x,y$)
$$
\frac{\psi(t + h) - \psi(t)}{h}\\
 = -u_1(x,y)\frac{\frac12\psi(x+k,y) - \frac12\psi(x-k,y)}{k} -u_2(x,y)\frac{\frac12\psi(x,y+k) - \frac12\psi(x,y-k)}{k}
$$
so
$$
\psi(t + h) = \\
 \psi(t) -\frac{h}{2k}\left(u_1(x,y)(\psi(x+k,y) - \psi(x-k,y)) +u_2(x,y)(\psi(x,y+k) - \psi(x,y-k))\right).
$$
Then your method is something like (assuming you are solving on $(t,x,y) \in [0,T] \times [0,L_x] \times [0,L_y]$)
psi(0,x,y) = initial_condition(x,y);
for t = 0 : h : T
    for x = 0 : k : L_x
        for y = 0 : k : L_y
            psi(t + h,x,y) = psi(t,x,y) ...
             - (h/(2*k))*(u_1(x,y)*(psi(t,x+k,y) - psi(t,x-k,y)) ...
             + u_2(x,y)*(psi(t,x,y+k) - psi(t,x,y-k)));
        end
    end
end

In practice, you will want psi to have integer indices, but this illustrates the main idea; you will also need to treat the boundary more carefully, notice phi(t,x,y-k) is going to give you problems for $y = 0$, etc. There is also some need to consider whether this scheme converges, etc. (this can depend on the choice of discretization, as well as the relative size of $h$ and $k$).
For more on all this, I recommend looking at Strikwerda's book. I know this probably seems like a lot to go through and learn just to solve a single PDE, but I can promise you this is a very useful general skillset to have if you think there are more PDEs in your future (in which case, you will quickly outgrow the Matlab toolbox).
Comment: In practice in MATLAB, you probably want to eschew the two inner loops over x and y in favor of fast difference operations like "diff" or "gradient". I implemented this idea with "gradient" with good results. With integer indicies, it looks something like [PSIX, PSIY] = gradient(psi(:,:,tt)); psi(:,:,tt + 1) = psi(:,:,tt) - (h/(2*k))*(u_1.*(PSIX) + u_2.*(PSIY));
Comment: Indeed. I usually vectorize $\psi$ and make the spatial derivative operator a matrix, so that there's only one loop (in $t$), and the update is just `phi{n + 1} = A*phi{n}`. But this requires using `kron` and tends to obfuscate what is actually happening. The loops are slow, but lay bare the details.
Comment: @BaronVT I found Strikwerda's book, but I'm not sure which section is relevant reading. Where would I look to find an improvement to the scheme I'm using atm?
Comment: I would recommend reading at least the first chapter. I have sort of written a crash course of the first half of that chapter, but reading a more detailed exposition will surely help you understand what you're doing (rather than just taking my word for it).


Entity Graph breaks Arquillian Unsatisfied dependencies

Question: using JBoss EAP7.1
I have tried to replace FetchType.EAGER on my Entity with an EntityGrap
<code> @Entity
@NamedQueries({
    @NamedQuery(name=Customer.FIND_ALL, query="SELECT DISTINCT c FROM Customer c"),
    @NamedQuery(name=Customer.DELETE_ALL, query="DELETE FROM Customer c")
})
@NamedEntityGraphs({
    @NamedEntityGraph(name=Customer.GRAPH_ALL, attributeNodes=
            {@NamedAttributeNode("addresses"), @NamedAttributeNode("communications")})
})
public class Customer implements Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 8179417957630809071L;

    public final static String FIND_ALL = "Customer.findAll";
    public final static String DELETE_ALL = "Customer.deleteAll";
    public final static String GRAPH_ALL = "Customer.graph.all";

    @Id
    @GeneratedValue
    private Long id;

    @Version
    private Timestamp version;

    @NotNull
    @Size(min=1, max=50)
    private String firstName;
    @NotNull
    @Size(min=2, max=100)
    private String lastName;
    @Enumerated(EnumType.STRING)
    private Gender gender;
    private Relationship relationship;
    private Date birthday;

    @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name="customer_id")
    private Set<Address> addresses;

    @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name="customer_id")
    private Set<Communication> communications;

@Stateless
public class CustomerDAOBean extends AbstractDAOBean<Long, Customer> implements CustomerDAO {

    @Override
    public List<Customer> findAll() {
        EntityGraph<?> entityGraph = em.createEntityGraph(Customer.GRAPH_ALL);
        TypedQuery<Customer> query = em.createNamedQuery(Customer.FIND_ALL, Customer.class);
        query.setHint("javax.persistence.loadgraph", entityGraph);
        return query.getResultList();
    }

}
</code>
My Arquillian Test has worked before with Eager Fetching
<code>@RunWith(Arquillian.class)
public class CustomerTest {
    @Deployment
    public static Archive<?> createTestArchive() {
        return ShrinkWrap.create(WebArchive.class, "test.war")
                .addClasses(AbstractDAO.class, AbstractDAOBean.class, CustomerDAO.class, CustomerDAOBean.class, Customer.class, Gender.class, Relationship.class, Address.class, Communication.class, CommunicationType.class, Kind.class)
                .addAsResource("META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

    }

    @Inject
    CustomerDAO customerDAO;

    @Before
    public void init() {
        customerDAO.deleteAll();
    }

    @Test
    public void testRegister() throws Exception {
        Customer customer = new Customer();
        customer.setFirstName("Hans");
        customer.setLastName("Rupp");
        customer.setGender(Gender.MALE);
        Address address = new Address();
        address.setCity("Stuttgart");
        address.setStreet("Fraubronnstr. 57a");
        address.setZip("70599");
        address.setCountry("Deutschland");
        customer.addAddress(address);
        Communication communication = new Communication();
        communication.setCommunicationType(CommunicationType.Email);
        communication.setValue("firstname.lastname@example.com");
        customer.addCommunication(communication);
        customerDAO.save(customer);
        List<Customer> customers = customerDAO.findAll();
        assertEquals(1, customers.size());
        Customer customerPersistent = customers.get(0);
        assertEquals(1, customerPersistent.getAddresses().size());
        assertEquals(1, customerPersistent.getCommunications().size());
    }
</code>
Now I get
<code>2018-04-05 10:34:37,209 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."test.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."test.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1978)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDAO with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject de.rupp.test.CustomerTest.customerDAO
  at de.rupp.test.CustomerTest.customerDAO(CustomerTest.java:0)

    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:362)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:284)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:137)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:158)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:501)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:61)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:59)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

2018-04-05 10:34:37,219 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 1) WFLYCTL0013: Operation ("deploy") fehlgeschlagen - Adresse: ([("deployment" => "test.war")]) - Fehlerbeschreibung: {"WFLYCTL0080: Fehlgeschlagene Dienste" => {"jboss.deployment.unit.\"test.war\".WeldStartService" => "Failed to start service
    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDAO with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject de.rupp.test.CustomerTest.customerDAO
  at de.rupp.test.CustomerTest.customerDAO(CustomerTest.java:0)
"}}
2018-04-05 10:34:37,219 ERROR [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0021: Deploy von Deployment "test.war" wurde mit folgender Fehlermeldung zurückgesetzt:
{"WFLYCTL0080: Fehlgeschlagene Dienste" => {"jboss.deployment.unit.\"test.war\".WeldStartService" => "Failed to start service
    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDAO with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject de.rupp.test.CustomerTest.customerDAO
  at de.rupp.test.CustomerTest.customerDAO(CustomerTest.java:0)
"}}
</code>
I have no idea which classes to add to the test archive. I have not added any, I have only modified the entity and the DAO
Many thanks for any ideas,
Hans
Answer: I have forgotten that I also have changed the CustomerDAO interface from @Local to @Remote.
Switching back to @Local the test works now.


Maximizing JInternalFrame in Java

Question: I am trying to get a JInternalFrame to be maximized when it is launched.  I have done web searches on this and have tried various code suggestions, but they do not seem to work properly on my machine, which is running Java 6 on Windows 7.
I have simplified the code below so that it will be easier to isolate the solution.
Can anyone show me how to change the code below so that the internal frame is maximized automatically when it is created?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class MaximizeInternalFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JLayeredPane desktop;
JInternalFrame internalFrame;

public MaximizeInternalFrame() {
    super("Test To Maximize Internal Frame");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Make the big window be indented 50 pixels from each edge of the screen.
    int inset = 50;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension screenMinus50 = new Dimension(screenSize.width - inset*2, screenSize.height - inset*2);

    this.setPreferredSize(screenMinus50);
    desktop = new JDesktopPane();
    setJMenuBar(createMenuBar());
    this.add(desktop, BorderLayout.CENTER);
    this.pack();
    this.setSize(screenMinus50);
    this.setLocationRelativeTo(null);
}
protected JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    //Set up the File menu.
    JMenu FileMenu = new JMenu("File");
    FileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(FileMenu);
    //Set up the first menu item.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(new OpenListener());
    FileMenu.add(menuItem);
    //Set up the second menu item.
    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    FileMenu.add(menuItem);

    return menuBar;
    }
class OpenListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // create internal frame
        internalFrame = new JInternalFrame("this internal frame needs to be maximized", true, true, true, true);
        desktop.add(internalFrame);
        internalFrame.setSize(internalFrame.getMaximumSize());
        internalFrame.pack();
        internalFrame.setVisible(true);
    }
}
public static void main(String args[]) {
    MaximizeInternalFrame myParentFrame = new MaximizeInternalFrame();
    myParentFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {if ("quit".equals(e.getActionCommand())){System.exit(0);}}
}

Comment: please learn java naming conventions and stick to them
Comment: [`InternalFrameCount`](http://stackoverflow.com/a/9422246/230513) is an example.
Answer: Add the following after internalFrame.setVisible(true):
try {
  internalFrame.setMaximum(true);
} catch (PropertyVetoException e) {
  // Vetoed by internalFrame
  // ... possibly add some handling for this case
}

You can also remove internalFrame.setSize(internalFrame.getMaximumSize());
Comment: FYI to others: setMaximum() must be called *after* the JInternalFrame has been added to a parent container. Calling it beforehand will throw an exception. Common sense (after all, how do you tell a window to fill its parent if it hasn't been given a parent yet), but can easily be overlooked. Not that *I* did that. ;)

Maximizing JInternalFrame in Java


Brackets calculations explained

Question: I am trying to solve how much is
$$(x^3-2x-1):(x+1)$$
Please include steps and explanations. Is there and easy way to calculate those?
My logic is that I have to:
$$(x+1)(? ?)=(x^3-2x-1)$$
but is there another easier way?
Comment: just use division algorithm
Comment: you Need polynomial log division
Comment: see here https://en.wikipedia.org/wiki/Polynomial_long_division
Answer: Check that $x=-1$ is indeed a solution to $x^3-2x-1$.
$$(x+1)(Ax^2+Bx+C)=x^3-2x-1$$
We can easily see that $A=1$ and $C=-1$
$$(x+1)(x^2+Bx-1)=x^3-2x-1$$
Now let $x=1$.
$$2(1+B-1)=1-2-1$$
$$B=-1$$
Edit:
In response to comment, compute quotient of $x^5-2x-1$ divided by $x+1$ using the same method. You might want to use long division or Horner's scheme.
check that $x=-1$ is a root. $$(x+1)(Ax^4+Bx^3+Cx^2+Dx+E)= (x^5-2x-1).$$ Clearly, $A=1$, $E=-1$.
$$(x+1)(x^4+Bx^3+Cx^2+Dx-1)= (x^5-2x-1).$$
Let $x=1$:
$$2(1+B+C+D-1)=-2$$
$$B+C+D=-1 \tag{1}$$
Let $x=2$:
$$3(16+8B+4C+2D-1)=32-4-1=27$$
$$15+8B+4C+2D=9 \tag{2}$$
Let $x=-2$:
$$(-1)(16-8B+4C-2D-1)=-32+4-1=-29$$
$$15-8B+4C-2D=29 \tag{3}$$
Adding equation $(2)$ and $(3)$:
$$30+8C=38$$
$$C=1\tag{4}$$
Substitute $(4)$ back into $(1)$:
$$B+D=-2 \tag{5}$$
Substitute $(4)$ back into $(2)$:
$$8B+2D=-10$$
$$4B+D=-5 \tag{6}$$
Solving $(5)$ and $(6)$:
$$B=D=-1$$
Comment: I have no idea where $$2(1+B−1)=1−2−1$$ came from... Can you please explain?
Comment: I substitute $x=1$ into the previous equation $(1+1)(1^2+B(1)-1)=1^3-2(1)-1$
Comment: Can you please show me same logic on $$(x+1)(x^5-2x-1)$$ Thank you
Comment: Do you mean compute the quotient of $x^5-2x-1$ divided by $x+1$?
Comment: Yes, sorry. I stand correct. Can you show me please
Comment: Edited the answer. You might want to use Horner's scheme for the latest question perhaps.
Answer: To divide by $x-a$, you can use Horner's scheme:
$$\begin{matrix}\text{coeff.}\\\text{of}&\\\\\\\\\times-1\quad\\\text{coeff.}\\\text{of}\end{matrix}\begin{array}{crrr}
x^3 &x^2&x&1\\
\downarrow&\downarrow&\downarrow&\downarrow\\
\color{blue}1&\color{blue}0&\color{blue}{-2}&\color{blue}{-1}\\
\downarrow&-1&1&1\\
\hline
\color{red}1&\color{red}{-1}&\color{red}{-1}&0\\
\uparrow&\uparrow&\uparrow \\
x^2&x&1
\end{array}$$
Thus, the quotient is equal to $\;x^2-x-1$.
Answer: $$\frac { x^{ 3 }-2x-1 }{ x+1 } =\left( a{ x }^{ 2 }+bx+c \right) \\ x^{ 3 }-2x-1=\left( x+1 \right) \left( a{ x }^{ 2 }+bx+c \right) =a{ x }^{ 3 }+\left( b+a \right) { x }^{ 2 }+\left( b+c \right) x+c\\ a=1\\ b+a=0\Rightarrow b=-1\\ b+c=-2\Rightarrow c=-1$$
Answer: let's do some transformations with $$x^{3}-2x-1=x^{3}-x-x-1=(x^{3}-x)-(x+1)=x(x^{2}-1)-(x+1)=x(x+1)(x-1)-(x+1)=(x+1)(x(x-1)-1)=(x+1)(x^{2}-x-1).$$
After division you get the answer: $x^{2}-x-1$

Brackets calculations explained


Deducing hidden die

Question: A dice deduction game uses a set of $N$ $(N-1)$-sided fair dice. For the $n$-th die in the set, if $n<N$, the face that shows $n$ pips is replaced with a blank. For $n=N$, all faces stays as it is.
To clarify, for $N=7$, the number of pips on each face of each dice is as follows:
<code>Die#1: X 2 3 4 5 6
Die#2: 1 X 3 4 5 6
Die#3: 1 2 X 4 5 6
Die#4: 1 2 3 X 5 6
Die#5: 1 2 3 4 X 6
Die#6: 1 2 3 4 5 X
Die#7: 1 2 3 4 5 6
</code>
Before the game begins, the game master hides one random die and rolls the rest of the dice on the table as the game begins. The goal of the game is to deduce which die is hidden, using this rules:

Player is only allowed to look at the dice when all dice have settled on their resting positions
Player is only allowed to look at the dice directly from above so that only the faces facing upwards can be seen
Player is free to move dice around as long as their orientations remain unchanged
Player is not allowed to pickup and inspect the dice (The point is, player is strictly prohibited from seeing any dice showing more than one face at any given time)
Player is free to reroll any subset of dice infinitely many times
Player is allowed to put mark on faces using marker as a way to
carry information for future rolls

Given the rules, is there a strategy for the player to always be able to deduce the hidden die in finite time?
Comment: To reroll dice infinitely many times (rule 5) in finite time, maybe it's suggesting a supertask?
Answer: One relatively naive way that somewhat abuses rule 5 would be to roll all of the available dice repeatedly some large number (say 1000) times. At each toss of all the dice, count the number of occurrences of each face value and add it to a running total for each value. For instance, if on some given toss of the dice, 3 of them show 1, and up till now you've seen some number $N_1$ of ones total, then update $N_1 = N_1 + 3$ (forgive the computer code notation). Once you've done all your tosses, and have all your final counts $(N_1,...,N_6)$, look at the count values relative to each other. If some single count $N_x$ is significantly higher than the others, then it was probably die #$x$ that got taken out. Otherwise, if all the counts are relatively even, it is probably die #7 missing.
This works asymptotically (as the number of re-rolls goes to $\infty$) since, if the die missing face $x$ is removed, then the remaining group of dice has more $x$ faces between them than any other value. And if the die with all its faces is removed, there is an equal number of each face value left in the group.
The success rate of your game will of course be a function of how many re-rolls you do, and the precise form of rule you use to decide whether one count is "significantly higher" than the others.
Edit: You could also further abuse rule 5 by instead taking each available die individually, rolling it some number of times where you'd expect to see every side at least once (say 30 times). Then decide for that single die which one you think it is (based on which face value(s) you saw zero times) and set it aside. After you do that for all available dice, 1 should be missing from the list of "identified" dice. This seems like it might be even faster than the first way I gave. Basically, rule 5 leaves you with a few different ways to go about it.
Of course neither of these methods gives success with certainty, but if you use good "decision rules" then there should be success with probability approaching 1 as the number of re-rolls increases.

Deducing hidden die


How to set namespace aware to false?

Question: I'm trying to parse some XML with EclipseLink MOXy, and it's failing on the line with the xsi attribute. If I remove this, it parses fine. However, I've got 100GiB of XML to wade through and changing the source files is not an option.
It's been suggested that if I can set XmlParser.setNamespaceAware(false) then it should work - but I've got no idea how to configure this, without breaking right into the guts of MOXy.
<record>

    <identifier>info:doi/10.1007/s10973-004-0435-2</identifier>
    <datestamp>2009-04-28</datestamp>
    <setSpec>J</setSpec>
    <setSpec>J:1007</setSpec>
    <setSpec>J:1007:2777</setSpec>

<metadata>
    <crossref xmlns="http://www.crossref.org/xschema/1.0"
        xsi:schemaLocation="http://www.crossref.org/xschema/1.0 http://www.crossref.org/schema/unixref1.0.xsd">
        <journal>
            <journal_metadata language="en">
[...]

The exception I get when the xsi: prefix is present is:
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[13,107]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?crossref&xsi:schemaLocation&xsi]

Comment: What is the original problem you are hitting?
Comment: @BlaiseDoughan updated. Really hope you can help :)
Comment: In your use case are you able to add the necessary `xmlns:xsi` declaration?  Just checking before we dive into alternate options.
Comment: I can't change the XML in any way, unfortunately.
Answer: There currently isn't an option in EclipseLink JAXB (MOXy) to tell it to ignore namespaces.  But there is an approach you can use by leveraging a StAX parser.
Demo
You can create a StAX XMLStreamReader on the XML input that is not namespace aware and then have MOXy unmarshal from that.
package forum13416681;

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
        StreamSource source = new StreamSource("src/forum13416681/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(source);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo root = (Foo) unmarshaller.unmarshal(xsr);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

Java Model (Foo)
package forum13416681;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Foo {

    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

Input (input.xml)
Below is a simplified version of the XML from your question.  Note that this XML is not properly namespace qualified since it is missing the namespace declaration for the xsi prefix.
<?xml version="1.0" encoding="UTF-8"?>
<foo xsi:schemaLocation="http://www.crossref.org/xschema/1.0 http://www.crossref.org/schema/unixref1.0.xsd">
    <bar>Hello World</bar>
</foo>

Output
Below is the output from running the demo code.
<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <bar>Hello World</bar>
</foo>

Answer: Rather than disabling namespace awareness altogether, you may be able to use a StAX-implementation-specific mechanism to declare the xsi prefix in advance, then parse with namespaces enabled.  For example, with Woodstox you can say:
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
import com.ctc.wstx.sr.BasicStreamReader;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("com.example");

        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource source = new StreamSource("input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        ((BasicStreamReader)xsr).getInputElementStack().addNsBinding(
               "xsi", "http://www.w3.org/2001/XMLSchema-instance");

and then create the unmarshaller and unmarshal the xsr as in Blaise's answer.  While this obviously ties you to one specific StAX implementation, it means that you don't have to modify your existing JAXB model classes if they expect the <crossref> element and its children to be in the http://www.crossref.org/xschema/1.0 namespace.

How to set namespace aware to false?


How to schedule concurrent.future.Threadpool.executor map() function in python

Question: I have one web scraping function which fetches data of 190 URL's. To complete it fast I used concurrent.future.Threadpool.executor. I am saving that data to SQL Server database. I have to do these all process repeatedly to every 3 mins from 9AM to 4PM. But when I use while loop or scheduler that concurrent future not works. No error and no output.
# required libraries
import request

urls = []

def data_fetched(url):
    # data fetching
    # operations on data
    # data saving to SQL server
    return ''

while True:
    with concurrent.future.ThreadPool.executor() as executor:
        executor.map(data_fetched, url)
    time.sleep(60)

I want to repeat all these things to every 3 mins, explained flow of code. Please help me how to schedule it.
start = dt.strptime("09:15:00", "%H:%M:%S")
end = dt.strptime("15:30:00", "%H:%M:%S")
# min_gap
min_gap = 3

# compute datetime interval
arr = [(start + timedelta(hours=min_gap*i/60)).strftime("%H:%M:%S")
       for i in range(int((end-start).total_seconds() / 60.0 / min_gap))]
while True:
    weekno = datetime.datetime.today().weekday()
    now = dt.now() # gets current datetime
    hour = str(now.hour) # gets current hour
    minute = str(now.minute) # gets current minute
    second = str(now.second)
    current_time = f"{hour}:{minute}:{second}" # combines current hour and minute

    # checks if current time is in the hours list
    if weekno < 5 and current_time in arr:
            print('data_loaded')
    else:  # 5 Sat, 6 Sun
        pass
    time.sleep(60)

So under these while loop I want to call that function using concurrent.futures.
Answer: You can create a seperate function and schedule it to execute the data_fetched(). I hope your urls variable contains the list of urls and not empty list.
from schedule import every, repeat, run_pending
import time
import request

urls = []

def data_fetched(url):
    # data fetching
    # operations on data
    # data saving to SQL server
    return ''

@repeat(every(3).minutes)
def execute_script():
    with concurrent.future.ThreadPool.executor() as executor:
        executor.map(data_fetched, urls)

while True:
    run_pending()
    time.sleep(1)

Comment: Thanks @Jamiu Shaibu, used for loop and ur suggested flow to schedule concurrent.futures. It's working as expected now.
Comment: You are welcome @varshapatil, I'm glad it worked for you.