text:
Q: The Zig Zag Lemma in Cohomology I´m reading the Zig Zag lemma in Cohomology and i want to prove the exactness of cohomology sequence at $ H^k(A)$ and $H^k(B)$ :
A short exact sequence of cochain complexes $ 0 \to A \ \xrightarrow{i} \ B \ \xrightarrow{j} \ C \to 0$ gives rise to a long exact sequence in cohomology:
$ ... \ \xrightarrow{j^*} \ H^{k-1}(C) \ \xrightarrow{d^*} \ H^k(A) \ \xrightarrow{i^*} H^k(B) \ \xrightarrow{j^*} H^k(C) \ \xrightarrow{d^*} H^{k+1}(A) \ \xrightarrow{i^*} ...$
where $i^∗$ and $j^∗$ are the maps in cohomology induced from the cochain maps i and j,and $d^∗$ is the connecting homomorphism.
I think first i need to prove that $im(d^∗) = ker(i^∗)$ for exactness in $H^k(A)$ . Help please…..
I prove the exactness of $H^k(C)$:
First I prove that $im( j^*)\subseteq ker (d^*)$.  Let $[b]\in H^k(B) $ then $d^* j^* [b] = d^*[j(b)]$.
In the recipe above for $d^*$ , we can choose the element in $B^k$ that maps to $j(b)$ to be b. Then $db \in B^{k+1}$. Because b is a cocycle, $db=0$. Following the Zig-Zag diagram we see that since $i(0) = 0 = db$, we must have
$d^*[j(b)] = [0]$, so $j^*[b]\in ker(d^*)$. The other way, i.e.,
$ker(d^*) \subseteq im(j^*)$:  suppose $d^*[c] = [a]=0$, where
$[c] \in H^k(C) $, this means that $a=da´$ for some $ a´ \in A^k$.i calculate the $d^*$ again by the diagram and take an element $ b \in B^k$with $j(b) = c$ and $i(a) = db$. Then $b - i(a´)$ is a cocycle in $B^k$ that maps to c under j:
$d(b - i(a´)) = db-di(a´) = db - id(a´) = db - ia = 0$,
$j(b - i(a´)) = db-ji(a´) = j(b) = c$
Therefore, $ j^*[b - i(a´)]= [c]$.

A: First of all the proofs aren't very difficult and go through without any complications as well as your proof the exactness at $H^k(C)$ given.
For a proof of this I would recommend any standard-literature such as Neukirch, p. 24 (in German) or NSW p.26 using the Snake lemma.

A: Tu=[Tu, L. W.: Introduction to Manifolds, 2nd ed., New York: Springer, 2011.]
Spivak=[Spivak, M.: A Comprehensive Introduction to Differential Geometry, Vol. 1, Berkeley: Publish or Perish, 1979.]
I use the notations in Tu, p.285, Theorem 25.6.
The exactness at $H^k(\mathcal{C})$ [$\text{ im }j^*=\ker d^*$] is proved in Tu, p.286, l.5--l.$-$4. $\ker i^*\subset \text{ im }d^*$ [one part of exactness at $H^k(\mathcal{A})$] is proved in Spivak, p.576, l.$-$7--l.$-$1.
Proof of $\text{ im }d^*\subset \ker i^*$ [the other part of exactness at $H^k(\mathcal{A})$]:
Let $c\in \mathcal{C}^{k-1}$ such that $dc=0$. Then
$\exists b\in \mathcal{B}^{k-1}: j(b)=c$ [since $j$ is surjective].
$db=i(a)$ [Tu, p.284, l.$-$2]
$d^*[c]=[a]$ [Tu, p.285, l.3].
$i^*[a]=[i(a)]$ [Tu \cite [p.283, (25.2)]
$=[db]=0$. This completes the proof.
For the proof of exactness at $H^k(\mathcal{B})$ and other related material about the zigzag lemma, see Example 6.157 in  hhttps://sites.google.com/view/lcwangpress/%E9%A6%96%E9%A0%81/papers/mathematical-methods.


text:
Q: How do I structure a JSON Schema to add a rule comparing two fields I have been studying docs and examples at https://rjsf-team.github.io/react-jsonschema-form/
My question is, suppose I have a form that asks MIN_AGE and MAX_AGE.
How do I set up a schema rule to ensure that MAX_AGE > MIN_AGE?

A: Unfortunately, JSON Schema doesn't define a way to make comparative assertions on JSON data.  There has been a lot of discussion on a potential $data keyword, but no decisions have been made on it for various reasons.
Happily, with the introduction of vocabularies with draft 2019-09, you can define your own keywords.  You'll also have to provide some logic to whatever implementation you use so that it can evaluate your new keywords.

redpajama_set_name: RedPajamaStackExchange


text:
Q: neo4j - create different user authentication i am new to neo4j.
Can anyone tell me how to create different users
(say user1 and user2) with different authentication for database access like user1 can write to database but user2 can only read from database .. i have read the documentation but could not figure it out...
    i am using neo4j 2.0 with rest api from jquery and cypher.
i would be great if the process is properly explained..
Thanks Alot

A: Neo4j does not use user authentication by default. There is a detailed section in the reference manual covering security topics.
Additionally there's a add-on authentication-extension.


text:
Q: How to set CloudFront Distribution Description? Terraform's docs doesn't say anything about any Description field. Neither Googling (which's is problematic in the first place, as Description is super-common word).
I tried adding a Description Tag, but it doesn't show up in the Description column of the CloudFront > Distributions page

A: You can add a description using comment
AWS console uses description ( console is misleading :D) however the cloudfront API uses comment to denote description. Terraform mapped API name comment into its HCL

comment (Optional) - Any comments you want to include about the distribution.

A: I believe you're looking for the comment attribute.


text:
Q: Setting multiple variables of different types to null in C# for memory management Say I have a 3D double array 3dArray, and a variable of custom type Person person1. Doing this:
3dArray = null;
person1 = null;

works but looks sloppy and doing this:
3dArray = person1 = null;

as throws an error as 3dArray and person1 can't be implicitly converted. Is there a better way to do this in one line?

A: No, there is not. C# has no special provision by which null propagates regardless of variable type. It is otherwise strong typed, so this assignment IS not legal (as the first assigment determines the variable type).

A: If you simply want it on one line,
3dArray = null; person1 = null; something_else = null;

Doesn't save anything from a typing perspective, but it is more vertically dense code.

A: You can do it but only if the types have some sort of relationship and you assign from specific to general. For example, this works:
    string s;
    object o;
    IEnumerable l;

    o = l = s = null;


text:
Q: why file contents not saving in 2D array I have a file example.txt who look like this
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

but i am unable to read and save file contents in my array.
Here is my code which i am trying for almost 1 hour
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<fstream>
using namespace std;
int main()
{
    int arr[10][5];
    int i=0,j=0;
    ifstream in("example.txt",ios::in);

    if(!in)
        {
            cout<<"File not Found";
        }
    else
    {
        for (i = 0; i < 10; i++)
        {
            for( j = 0;j<5;j++)
            {
                 in >> arr[i][j];
            }
        }

    }

    int x=0,y=0;
    for(x=0;x<10;x++)
    {
        for(y=0;y<5;y++)
        {
            cout<<arr[x][y];
        }
        cout<<endl;
    }

    system("pause");
    return 0;

}

Is anything i am doing wrong , cant find out the error!
I am getting these raw values


text:
Q: Burning Snow Leopard DMG on Ubuntu So I have a SL Copy of Mac OS X Snow Leopard.DMG and I am running Ubuntu 12.0.4. I tried burn the DMG to a DVD using CD/DVD Creator however when I try, it comes up with a message saying
'The size of the file is over 2 GiB. Files larger than 2 GiB are not supported by the ISO9660 standard in its first and second versions (the most widespread ones).
It is recommended to use the third version of the ISO9660 standard, which is supported by most operating systems, including Linux and all versions of Windows™.
However, Mac OS X cannot read images created with version 3 of the ISO9660 standard.'
How do I bypass this so my DVD is bootable on my iMac. By the way, I'm running Ubuntu on the iMac and it is my only partition. No other OS Installed

A: Since this is a dmg image i think you have to convert to an iso then you can burn the ISO in brasero. You can look dmg2img "DMG2IMG is an Apple's compressed dmg to standard (hfsplus) image disk file convert tool",to install check here https://askubuntu.com/a/38122/71679 or a java port of the earlier DMG2ISO version called DMGExtractor http://www.catacombae.org/dmgx.html. "dmg2iso has been superseded by dmg2img - it can handle files >4GB "
Since no experience with either i cant say if they will work or not but it may be a good starting point.My understanding is that brasero cant burn  .dmg files they need to be converted first and from what ive read dmg2img doesn't always work for everyone.


text:
Q: Visual Studio slow and unable to load dependencies I am using Visual Studio Professional 2022 64-bit (version 17.1.0).
I experience problems to load/unload projects (very slow) and to load dependencies (fails).
It also takes long time (around 10 minutes?) to build (fails to build due to dependencies). Also slow (many minutes) to cancel the build.

But if I start the application as administrator, the build will happen successfully within seconds!

How can this issue be fixed, other than starting it as administrator?
Thanks in advance!


text:
Q: How to extract data from Open Packaging Conventions(.opc) extension files through SSIS Hi
 Could you please let me give an idea how to read .OPC data:
example for data in the file looks like: f1=1 | f2=2 | f3=3 | f4=4
I wanted to be convert this into table(f1,f2,f3,f4) and values as (1,2,3,4).
Currently am using SSIS to extract the data
and read from flatfile connection manager column delimeter as {|} and row delimiter as {cr}{lf} into col1, col2,col3,col4 then identfying the value with"=" string supperator and getting the value.
Is there any betterway to extract from SSIS or any convertion to the source file into .csv/txt or any other format can be done before reading?
Thanks in advance.

A: I would use a Script transformation, because then you can do all the text parsing using .NET instead, this will probably be much easier than trying to do it using built-in SSIS functions and/or plain TSQL.
Alternatively, you could transform the file to .csv externally using a script in .NET, Perl or whatever, then load the .csv file. You can still use SSIS to run the external script, of course, so you still keep control of the overall process within your package.


text:
Q: How to find the exponent of 2 in an integer in Java? I want to find the power of 2 that any integer contains. Like  12 = 2*2*3 so answer should come as 2, 28 = 2*2*7 so answer should come as 2 and so on.
int powerOf2InNumber = (int)Math.floor(Math.log(number) / Math.log(2));
I tried the above code but in cases like 28,26,10 etc i'm getting wrong answer.

A: There is a handy built-in function,
int powersOf2 = Integer.numberOfTrailingZeros(number);

A: This should do the trick:
int check = 28;
int count = 0;
while(check % 2 == 0) {
    check /= 2;
    count++;
}

Check ends up as the other factor. I.e the 7 in 2 * 2 * 7. Count is your answer.

A: What I think you're asking is: the number of times 2 goes into a number?
int countPowerOfTwo(int number) {
  int count = 0;
  while (abs(number) > 0) {
    if (number % 2 != 0) {
      return count;
    }
    count++;
    number = number / 2;
  }
  return count;
}

A: The best way, imo, has already been provided using Integer.numberOfTrailingZeros.  It is taken from Hacker's Delight which is an excellent book and worth the price.  Another method is as follows:
int b = 32*75;
int powerOf2 = BitSet.valueOf(new long[]{b}).nextSetBit(0);
System.out.println(powerOf2);

Prints
5

Note: For completeness you weren't far off in your attempt and logarithms can be used along with some basic bit manipulation.  So you can do the following:
int number = 32*75;
int powerOf2 = (int)(Math.log(number & -number)/Math.log(2))


text:
Q: IE11 Metro/Modern showing menu bar Since the latest couple of upgrades IE11 Metro/Modern version permanently shows the menu bar at the bottom. Usually, clicking inside the page showing would make it disappear downwards, now it remains taking up valuable screen real estate.
Is this supposed to happen? If so, can I turn it off somewhere?

A: Not 100% that this is your issue - but try going to "Options" and turning off "Always show address bar and Tabs".


text:
Q: Is there an extension of the integers where the "sum of natural numbers" is rigorous? There's the well-known claim that
$$\sum_{n=1}^{\infty} n = -\frac{1}{12} \tag 1$$
Of course in this form, using the usual interpretation of the infinite sum as limit of finite sums, it's wrong, as the sum on the left hand side diverges. The $-1/12$ is obtained by using the series of the zeta function,
$$\zeta(s) = \sum_{n=1}^{\infty} n^{-s}$$
and then observing that formally evaluating it for $s=-1$ gives eq. (1).
Now let's consider another "sum" of a divergent series:
$$\sum_{n=0}^{\infty} 2^n = -1 \tag 2$$
Again, you can do an argument like the above: If we take the geometric series
$$\frac{1}{1-q} = \sum_{n=0}^{\infty} q^n$$
and insert $q=2$, we get the claimed identity.
However, eq. (2) can also be made rigorous by extending the integers to the $2$-adic numbers, where the left hand side indeed converges to $-1$.
My question therefore is:
Does there exist an extension of the integers that makes eq. (1) rigorous in the sense that in that extension the sum actually converges to the value $-1/12$?

A: The $2$-adic topology makes $\mathbb{Z}$ into a Hausdorff topological group (and in fact a topological ring). This is important: the fact that the topology is compatible with addition is used to prove several basic facts about convergent series, e.g. that changing a finite number of terms does not affect convergence.
Claim: There is no Hausdorff topological group structure on $\mathbb{Z}$ such that the sequence
$$
\tag{$\star$}
\sum_{n=1}^kn,\;\;\;k=1,2,\ldots
$$
is Cauchy.
Proof: Suppose such a topology exists. Choose an open neighborhood $U$ of $0$ such that $1\not\in U$, and a neighborhood $V$ of $0$ such that $V-V\subset U$. Here, $V-V=\{v_1-v_2:v_1,v_2\in V\}$. We assume $(\star)$ is Cauchy, so there is some $N>0$ such that
$$
\sum_{n=1}^{k_1}n-\sum_{n=1}^{k_2}n\in V
$$
whenever $k_1$, $k_2>N$. Now
$$
1=\left(\sum_{n=1}^{N+3}n-\sum_{n=1}^{N+2}n\right)-\left(\sum_{n=1}^{N+2}n-\sum_{n=1}^{N+1}n\right)\in V-V\subset U,
$$
which contradicts $1\not\in U$.


text:
Q: Notepad++ find and replace multiple regex expressions I found help regarding multiple search and replaces at once, but they usually cover only plain words search and replace. (Multiple word search and replace in notepad++)
I have data that can have three different styles.
(15:
(4:
(:
Infront of each of these is a random number of spaces with I am trying to replace with a single space. I have the regular expression search for each by individually.
Find what: *\(([0-9])([0-9])\:
Replace with: \(\1\2\:
Find what: *\(([0-9])\:
Replace with: \(\1\:
Find what: *\(\:
Replace with: \(\:
If I try to combine them into a single find and replace code, I try;
Find what: ( *\(([0-9])([0-9])\:)|( *\(([0-9])\:)|( *\(\:)
Replace with: (?1 \(\1\2\:)(?2 \(\1\:)(?3 \(\:)
However, this does not complete the three replaces with the desired results that occur when run individually.
What kind of adjustments do I need to make the replace code work? Cheers

A: You can use the following regex.
Find:  *(\(\d*:)
Replace:  \1

Live Demo


text:
Q: Getting "None" instead of list of values in python I am currently working on POC on Alibaba Cloud and not authorized to share the information. I am a beginner in python. Let me consider an example to address my issue.
I am getting none instead of list of values,
Example code:
NumSet={1,2,3,4,5,6,7,8,9,10}
NumList = list(NumSet).reverse()
print(NumList)

Output:
None
What I am missing?

A: list.reverse() reverses the list in-place and returns nothing (or returns None).
NumList = list(NumSet)  # convert to list first
NumList.reverse()  # reverse in-place

This is the correct way to do it.

A: list.reverse() doesn't return the list reversed. What it does is reversing the list in-place. Just change to this and it ill work
NumSet = {1,2,3,4,5,6,7,8,9,10}
NumList = list(NumSet)
NumList.reverse()
print(NumList)


text:
Q: How to execute "kubeadm init" v1.6.4 behind firewall Is it possible to run "kubeadm init" without Internet access?
When executing kubeadm init on isolated networks where the host is not allowed to make external connections, it fails on download of a stable version check of some sort, as it tries to retrieve https://storage.googleapis.com/kubernetes-release/release/stable-1.6.txt .
# kubeadm init
[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
unable to get URL "https://storage.googleapis.com/kubernetesrelease/release/stable-1.6.txt": Get https://storage.googleapis.com/kubernetes-release/release/stable-1.6.txt: dial tcp 216.58.204.80:443: i/o timeout

Why is this check needed? The contents of that URL seems to today be "v1.6.4", which is the version that is installed:
# kubeadm version
kubeadm version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.4", GitCommit:"d6f433224538d4f9ca2f7ae19b252e6fcb66a3ae", GitTreeState:"clean", BuildDate:"2017-05-19T18:33:17Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

This seems to be a behavior introduced after 1.6.0. I have looked at documentation, flags, configuration options but have not found a way to execute kubeadm init without this (not even with --skip-preflight-checks).

A: Resolved by using the following command:
kubeadm init --kubernetes-version=v1.6.4

(Note the "v" in the version number.)


text:
Q: What is a .git.dmp file and how can I use it? I just backed up my unfuddle account and it sent a zip file with my git repositories as a .git.dmp file.  Do I just remove the .dmp ending and use it like a normal .git repository?

A: This blog post has a good explanation of using Git and Unfuddle. In short, it says to:
mkdir reponame
cd reponame
git init
git fast-import < ../my-unfuddle-backup.git.dmp
git checkout master

A: Check if it is possibly a git bundle. If that's the case, you can simply clone it to a new directory:
git clone whatever.git.dmp whatever

A: if it is a git internal bundle there's also just:
git bundle verify filename.git.dmp

command. No need to try to clone it


text:
Q: Suggestions to solve an optimization problem that involves quadratic forms. Let $E$ and $S$ be symmetric matrices of $m\times m$ such that $E$ is positive semidefinite. We define $\mathbf{e}\in\mathbb{R}^{m}$ column vector such that $\mathbf{e}_{i}=1$ for $i=1,\ldots,m-1$ and $\mathbf{e}_{m}=0$.
We consider the following optimization problem
$$\left\{\begin{array}{ll}
{\displaystyle \inf_{w\in\mathbb{R}^{m}}} & {\displaystyle \left( \sqrt{w^{T}Ew}+\sqrt{w^{T}Sw} \right)^{2}  }\\
\mbox{subject to} & w^{T}Sw\geq 0,  \\
& \mathbf{e}^{T}w=1, \\
& w(m-1)\geq 0, \\
& w(m)=1.
\end{array}\right. \tag{*}$$
The question: I need to solve $(*)$, for this purpose I appeal to the community of this website to help me with any suggestions, this help can be a way to reformulate $(*)$ in such a way that the new problem is in a context in which has more information, another help would be with some suggestion about an algorithm to approximate the optimal value of $(*)$.
Remark: Note that (*) is very similar to a quadratic program, the problem is square roots.

A: The square roots do not pose a significant problem. The bigger issue is your constraint $x^\intercal S x \geq 0$.
If $S$ is positive semidefinite then the constraint is trivial, and if $S$ is negative semidefinite then the constraint would be better expressed as $Sx = 0$. If is is neither NSD nor PSD then you have yourself a non-convex quadratic program.
With the exception of single-constraint quadratic programming, there are no particularly easy ways to solve non-convex quadratic programs.


text:
Q: Invalid shape error plotting 2D image in python for MNIST Sign Language dataset having PIXEL VALUES AS COLUMNS I have a MNIST Sign Language dataset with pixel values as columns.
I get the error when I try to plot an image at one of the indexes as follows:
#Training dataset
dfr = pd.read_csv("sign_mnist_train.csv")
X_train_orig = dfr.iloc[:,1:]
Y_train_orig = dfr['label']

#Testing dataset
dfe = pd.read_csv("sign_mnist_test.csv")
X_test_orig = dfe.iloc[:,1:]
Y_test_orig = dfe['label']

#shapes of dataset
print(dfr.shape) #(27455, 785)
print(dfe.shape) #(7172, 785)

#Example of a picture
index = 1
plt.imshow(X_train_orig.iloc[index])

#TypeError: Invalid shape (784,) for image data

A: Looks like the image you are trying to plot is a flattened one corresponding to [B, N], where N is 1x28x28, and B is 27455 which is your image size (27455, 784). This is fine if you want to feed it to a Linear layer of 784 long vector. To plot this image you have to reshape it to correspond to [27455, 1, 28, 28]. You can try this out:
image = X_train_orig.iloc[index]
image = np.reshape(image.values, (28, 28))
plt.imshow(image)


text:
Q: If T is forall/exists-axiomatizable and M,N satisfies T, then there is exists/forall sentence $\psi$ so that If M sat $\psi$, then N sat $\psi$ Setting
Definition: A theory $\pmb{T}$ has a $\forall\exists$-axiomatization if it can be axiomatized by sentences of the form $$\forall v_1\ldots \forall v_n \exists w_1 \ldots \exists w_n ~~ \phi(\bar{v},\bar{w})$$ where $\phi$ is a quantifier free $\mathcal{L}$-formula.
I am doing a multi-step proof to show $T$ has $\forall\exists$-axiomatization.
Step one: suppose whenever $(\mathcal{M}_i : i \in \mathbb{I})$ is a chain of models of $\pmb{T}$, then I showed that: $$\mathcal{M} = \bigcup \mathcal{M}_i \models \pmb{T}.$$
Looking ahead: Now I am showing the converse also holds. Suppose whenever $(\mathcal{M}_i, i \in \mathbb{I})$ is a chain of models of T, then $\bigcup \mathcal{M}_i$ is a model of T. Let $\Gamma = \{ \phi : \phi \text{ is a $\forall\exists$-sentence and $\pmb{T} \models \phi$}\}$. Let $\mathcal{M} \models \Gamma$. I will show $\mathcal{M} \models T$.
Step two: Now I want to show that there is $\mathcal{N} \models \pmb{T}$ such that if $\psi$ is an $\exists\forall$-sentence and $\mathcal{M} \models \psi$, then $\mathcal{N} \models \psi$.
My Attempt at step two
We construct the theory $\pmb{T}^{*} = diag_{\forall}(\mathcal{M}) \cup \pmb{T}$, where $diag_{\forall}$ is the set of for all sentences satisfied by $\mathcal{M}$. If $\pmb{T}^{*}$ is satisfiable then let $\mathcal{N} \models \pmb{T}^{*}$.
Now we show $\pmb{T}^{*}$ is consistent. By compactness, it suffices show some finite subset of $\pmb{T}^{*}$ is satisfiable. Let $\pmb{T}^{*'} = diag_{\forall}' \cup \pmb{T}'$, where $diag_{\forall}'$ and $\pmb{T}'$ are finite subsets of $diag_{\forall}$ and $\pmb{T}$. Next let:
$$\gamma = \forall \bar{v} \exists \bar{w} \phi(\bar{v},\bar{w}) \in \pmb{T}', \quad \theta = \forall \bar{x} \rho(\bar{x}) \in diag_{\forall}'.$$
Since $\pmb{T}$ is consistent, we can assume $\mathcal{N} \models \pmb{T}$, therefore $\mathcal{N} \models \gamma(\bar{a},\bar{b})$ for some $\bar{a},\bar{b} \in \mathbb{N}$. Now we show for each $\theta \in diag_{\forall}'$:
$$\mathcal{N} \models \gamma \Rightarrow \mathcal{N} \models \theta.$$
So we have:
$$\mathcal{N} \models \gamma ~~\Rightarrow~~
\mathcal{N} \models \forall \bar{v} \exists \bar{w} \phi(\bar{v},\bar{w}) ~~\Rightarrow~~
\mathcal{N} \models \forall \bar{v} \phi(\bar{v},\bar{b}) ~~~ \text{for some } \bar{b} \in \mathbb{N} ~~\Rightarrow~~\\
\mathcal{N} \models \forall \bar{x} \rho(\bar{x}) ~~\Rightarrow~~
\mathcal{N} \models \theta.$$
Where we simply renamed $\phi$ as $\rho$ and $\bar{v}$ as $\bar{x}$. Thus each $\pmb{T}^{*'}$ is consistent so $\pmb{T}^{*}$ is consistent, so $\mathcal{N} \models \pmb{T}^{*}$.
Next we show if $\mathcal{N} \models \pmb{T}$ and $\mathcal{M} \models \psi$, then $\mathcal{N} \models \psi$. Let $\psi = \exists \bar{v} \forall \bar{w} \sigma(\bar{v},\bar{w})$ where $\sigma$ is quantifier free. Then:
$$\mathcal{M} \models \exists \bar{v} \forall \bar{w} \sigma(\bar{v},\bar{w}) ~~\Rightarrow~~
\mathcal{M} \models \forall \bar{w} \sigma(\bar{a},\bar{w}) \text{ for some } \bar{a} \in \mathbb{M}.$$
And since $\mathcal{N} \models diag_{\forall}(\mathcal{M})$, we have:
$$\mathcal{N} \models \forall \bar{w} \sigma (\bar{a},\bar{w}) ~~\Rightarrow~~
\mathcal{N} \models \exists \bar{v} \forall \bar{w} \sigma (\bar{v},\bar{w}) ~~\Rightarrow~~
\mathcal{N} \models \psi.$$
My Problem

*

*I let $\mathcal{N} \models \pmb{T}$ by assumption, using the fact that $\pmb{T}$ is consistent. Is this justified and proper in this context?

*When unraveling the implication of $\mathcal{N} \models \gamma$ , is it correct to evaluate it at some $\bar{b} \in \mathbb{N}$?

A: I know this is 2 years later, but I am working through the same problem, and see nothing wrong with taking James' approach, with some small clarifications by removing unnecessary conjunctions. Let $\mbox{Th}_{\exists\forall}(\mathcal{M})$ be the set $S$ that James defined.
We need to show that $T\cup\mbox{Th}_{\exists\forall}(\mathcal{M})$
  is consistent. This is false iff there exists a $\exists\forall$
  sentence $\exists\overline{x}\forall\overline{y}\phi(\overline{x},\overline{y})$
  in $\mbox{Th}_{\exists\forall}(\mathcal{M})$
  where $\phi$
  is quantifier free such that $T\models\forall\overline{x}\exists\overline{y}\neg\phi(\overline{x},\overline{y})$.
 But this implies $\forall\overline{x}\exists\overline{y}\neg\phi(\overline{x},\overline{y})\in\Gamma$
  and thus $\mathcal{M}\models\forall\overline{x}\exists\overline{y}\neg\phi(\overline{x},\overline{y})$
 . But $\forall\overline{x}\exists\overline{y}\neg\phi(\overline{x},\overline{y})$
  is the negation of $\exists\overline{x}\forall\overline{y}\phi(\overline{x},\overline{y})$
 , contradicting the fact that $\exists\overline{x}\forall\overline{y}\phi(\overline{x},\overline{y})\in\mbox{Th}(\mathcal{M})$
 . $\square$

A: Here is my attempt. Fix a theory $T$, let $\Gamma = \{\phi \ : \ \phi \in \Pi_2 \cap Cl(T)\}$, suppose $M\models \Gamma$. Then $T$ is consistent, as, otherwise $M\models \forall x \exists y (x\neq x)$.
Let $S = \Sigma_2 \cap Th(M)$. We want to show that $T \cup S$ is consistent. It suffices to show that $T \cup \Delta$ is consistent, for every finite $\Delta \subseteq S$. Write $$\Delta = \{\phi_1,\ldots,\phi_n\} = \{\exists \bar{x}\forall\bar{y}\psi_1(\bar{x},\bar{y}),\ldots, \exists \bar{x}\forall\bar{y}\psi_n(\bar{x},\bar{y})\}$$
Where $\psi_i$ are quantifier free. If $T\cup \Delta$ is consistent, we are done, otherwise
$$
T \models \bigwedge_{i\leq n} \phi_i \rightarrow \bot
$$
So $T \models \bigvee \neg \phi_i$, more explicitly $T\models \bigvee \forall \bar{x} \exists \bar{y} \neg \psi_i(\bar{x},\bar{y})$. Hence $T\models\forall \bar{x} \exists \bar{y} \bigvee \neg \psi_i(\bar{x},\bar{y})$. This last sentence is a $\Pi_2$ consequence of $T$, hence, it $M$ is a model for it. Therefore $M\models \forall \bar{x} \bigvee \neg \forall \bar{y} \psi_i$, so finally $M \models \neg \exists \bar{x} \bigwedge \forall \bar{y} \psi_i$.
However $M\models \bigwedge \phi_i$ so $M\models \bigwedge\exists \bar{x} \forall \bar{y} \psi_i(\bar{x},\bar{y})$,
Edit, the following is incorrect:
thus $M \models \exists\bar{x} \bigwedge \forall \bar{y}\psi_i(\bar{x},\bar{y})$, but this contradicts the last paragraph.
This establishes that $T \cup S$ is consistent.

So at this point I am not sure how to proceed. I will leave this here in case I want to edit it later.


text:
Q: How to check if this process has finished? I'm building an iPhone app in which I retrieve data from a .json file on a web site and parse it into a some UITextField view in the main view. I want to check when the data is retrieved so that I can then remove the UIActivityIndicator from the screen.
This is the snippet of code I want to check on when it's complete, it's in viewWillAppear:
 //if a connection is available... get and load the infor via .json
if (connection){

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        kLatestGigsURL];
        [self performSelectorOnMainThread:@selector(fetchedData:)
                               withObject:data waitUntilDone:YES];
    });
}

Is it possible to check if the data has been fetched? If so how do I do this?
Thanks for reading!

A: dataWithContentsOfURL: is a synchronous method, so once it returns you can say it is complete.
Also, I would suggest to you tweak your code to use GCD to perform the block on the main thread like this:
if (connection){

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:kLatestGigsURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            // Update your indicator
            [self fetchedData:data];
        });
    });

}


text:
Q: Google Maps JS API Geolocation Permission Denied I'm using the Geolocation function to attempt to find the user's device via HTML5 as provided here:
https://developers.google.com/maps/documentation/javascript/examples/map-geolocation
Everything works great if the user accepts the prompt, but I would like to provide an alternate set of LatLng coordinates if the user denies permission. Currently (including Google's example linked above) if the user denies permission, it renders a blank space where the map should be.
So if the user accepts, render map centered at their location, and if the user denies permission, render map centered at an alternate geopoint.

// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see a blank space instead of the map, this
// is probably because you have denied permission for location sharing.

var map;

function initialize() {
  var mapOptions = {
    zoom: 6
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Location found using HTML5.'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}

google.maps.event.addDomListener(window, 'load', initialize);


text:
Q: Multi-Layered Architecture: where I should implement the error logging \ handling? I'm currently refactoring a large subsystem with a multi layered architecture, and I'm struggling to design an effective error logging \ handling strategy.
Let's say that my architecture consists of the following three layers:

*

*Public Interface (I.E an MVC Controller)

*Domain Layer

*Data Access Layer

My source of confusion is where I should implement the error logging \ handling:

*

*The easiest solution would be to implement the logging at the top level (I.E the Public Interface \ MVC Controller). However this feels wrong because it means bubbling up the exception through the different layers, and then logging it; rather than logging the exception at it's source.

*Logging the exception at it's source is obviously the best solution because I have the most information. My problem with this is that I can't catch every exception at source without catching ALL exceptions, and in the domain / public interface layer, this will lead to catching exceptions that have already been caught, logged and re-thrown by the layer below.

*Another possible strategy is a mix of #1 and #2; whereby I catch specific exceptions at the layer they are most likely to be thrown (I.E Catching, logging and re-throwing SqlExceptions in the Data Access Layer) and then log any further uncaught exceptions at the top level. However this would also require me to catch and relog every exception at the top level, because I can't distinguish between errors that have already been logged \ handled vs those that have not.
Now, obviously this is an issue in most software applications, so there must be a standard solution to this problem that results in exceptions being caught at source, and logged once; however I just can't see how to do this myself.
Note, the title of this question is very similar to 'Logging exceptions in a multi tier application"', however the answers in that post are lacking detail and are not sufficient to answer my question.

A: To your questions:

The easiest solution would be to implement the logging at the top
  level

Having the exception bubble up to the top level is an absolutely correct and plausible approach. None of the higher layer methods tries to continue some process after the failure, which typically can't succeed. And a well-equipped exception contains all the information necessary for logging. And doing nothing about exceptions helps you keep your code clean and focussed on the main task instead of the failures.

Logging the exception at it's source is obviously the best solution
  because I have the most information.

That's half correct. Yes, most useful information is available there. But I'd recommend to put all this into the exception object (if it isn't already there) instead of immediately logging it. If you log at a low level, you still need to throw an exception up to tell your callers that you didn't complete your job. This ends up in multiple logs of the same event.
Exceptions
My main guideline is to catch and log exceptions at the top level only. And all the layers below should make sure that all necessary failure information gets transported up to the top level. Within a single-process application e.g. in Java, this mostly means not to try/catch or log at all outside the top level.
Sometimes, you want some context information included in the exception log that's not available in the original exception, e.g. the SQL statement and parameters that were executed when the exception was thrown. Then you can catch the original exception and re-throw a new one, containing the original one plus the context.
Of course, real life sometimes interferes:

*

*In Java, sometimes you have to catch an exception and wrap it into a different exception type just to obey some fixed method signatures. But if you re-throw an exception, make sure the re-thrown one contains all information needed for later logging.

*If you are crossing an inter-process border, often you technically can't transfer the full exception object including the stack trace. And of course the connection might get lost. So here's a point where a service should log exceptions and then try its best to transmit as much of the failure information as possible across the line to its client. The service must make sure the client gets a failure notice, either by receiving a failure response or by running into a timeout in case of a broken connection. This will typically result in the same failure to be logged twice, once inside the service (with more detail) and once in the client's top level.
Logging
I'm adding some sentences about logging in general, not only exception-logging.
Besides exceptional situations, you want important activities of your application to get recorded in the log as well. So, use a logging framework.
Be careful about log levels (reading logs where debug information and serious errors are not flagged with different accordingly is a pain!). Typical log levels are:

*

*ERROR: Some function failed irrecoverably. That doesn't necessarily mean your whole program crashed, but some task couldn't be completed. Typically, you have an exception object describing the failure.

*WARNING: Something strange happened, but didn't cause any task to fail (strange configuration detected, temporary connection breakdown causing some retries, etc.)

*INFO: You want to communicate some significant program action to the local system administrator (starting some service with its configuration and software version, importing data files into the database, users logging into the system, you get the idea...).

*DEBUG: Things you as the developer want to see when you are debugging some issue (but you'll never know in advance what you really need in case of this or that specific bug - if you can foresee it, you'll fix the bug). One thing that's always useful is to log activities on external interfaces.

In production, set the log level to INFO. The results should be useful to a system administrator so he knows what's going on. Expect him to call you for assistance or bug-fixing for every ERROR in the log and half of the WARNINGs.
Enable the DEBUG level only during real debugging sessions.
Group the log entries into appropriate categories (e.g. by the fully-qualified classname of the code that generates the entry), allowing you to switch on debug logs for specific parts of your program.

A: I'm bracing myself for the downvotes, but I'm going to go out on a limb and say I'm not sure I can agree with this.
Bubbling exceptions up, much less logging them again, is extra effort with little benefit. Catch the exception at source (yes, easiest), log it, but then don't re-throw the exception, just report "error" to the caller. "-1", null, empty string, some enum, whatever. The caller only needs to know that the call failed, almost never the gruesome details. And those will be in your log, right? In the rare case that the caller does need the details, go ahead and bubble up, but not as an automatic unthinking default.


text:
Q: How can i fix Android Studio Render Problem? I have tried to do a fresh windows installation clean hard drive partition and then update windows to latest version install all driver and download and install android studio latest version completely then check for android studio updates but when i create a new android studio project then i go to activity.xml file then i saw white layout in editor and an error says

Render problem
  Failed to load AppCompat ActionBar with unknown error.   Tip: Try to refresh the layout.

and another error says

Failed to instantiate one or more classes
  The following classes could not be instantiated:
  - android.support.v7.widget.ActionBarContainer (Open Class, Show Exception, Clear Cache)
  - android.support.v7.widget.ActionBarContextView (Open Class, Show Exception, Clear Cache)
  - android.support.v7.app.WindowDecorActionBar (Open Class, Show Exception, Clear Cache)
   Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE.  If this is an unexpected error you can also try to build the project, then manually refresh the layout.  Exception Details java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener Copy stack to clipboard

i have tried everything but only one thing was working before update is to change
    implementation com.android.support:appcompat-v7:28.0.0-rc02
to alpha or beta but now its not working.Is there any other way to solve this problem permanently.
please help


text:
Q: JSON - how to get value in pure JS I can't get any value from my JSON object.
I was trying:
console.log(players[0].player.info.position)

It return undefined.
What am i doing wrong?
Validator say that JSON in valid.
Here is JSON object:
{"players": [
{
  "player": {
    "info": {
      "position": "aaa",
      "shirtNum": 1,
      "positionInfo": "aaa"
    },
    "nationalTeam": {
      "isoCode": "aaa",
      "country": "aaa",
      "demonym": "aaa"
    },
    "age": "221",
    "name": {
      "first": "aaa",
      "last": "aaa"
    },
    "id": 111,
    "currentTeam": {
      "name": "aaa",
      "teamType": "aaa",
      "shortName": "aaa",
      "id": 21
    }
  }
}]}

A: Try JSON.parse(). JSON itself is a string.

obj = JSON.parse('{"players":[{"player":{"info":{"position":"aaa","shirtNum":1,"positionInfo":"aaa"},"nationalTeam":{"isoCode":"aaa","country":"aaa","demonym":"aaa"},"age":"221","name":{"first":"aaa","last":"aaa"},"id":111,"currentTeam":{"name":"aaa","teamType":"aaa","shortName":"aaa","id":21}}}]}');

console.log(obj['players'][0].player.info.position);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

A: Use JSON.parse with your json string:

obj = JSON.parse('{"players":[{"player":{"info":{"position":"aaa","shirtNum":1,"positionInfo":"aaa"},"nationalTeam":{"isoCode":"aaa","country":"aaa","demonym":"aaa"},"age":"221","name":{"first":"aaa","last":"aaa"},"id":111,"currentTeam":{"name":"aaa","teamType":"aaa","shortName":"aaa","id":21}}}]}');

console.log(obj['players'][0].player.info.position);

A: You probably have that json in a variable which I call theThing. You need to use theThing to gain access to your json.
var theThing =    JSON.parse('{"players":[{"player":{"info":{"position":"aaa","shirtNum":1,"positionInfo":"aaa"},"nationalTeam":{"isoCode":"aaa","country":"aaa","demonym":"aaa"},"age":"221","name":{"first":"aaa","last":"aaa"},"id":111,"currentTeam":{"name":"aaa","teamType":"aaa","shortName":"aaa","id":21}}}]}');
alert(theThing.players[0].player.info.position);


text:
Q: Compton effect on wavelength Mention the change in wave length of the photon after it collides with free electron??
Is the rule of particle can be applied here?

A: You are probably familiar with the relation $$c = \lambda f$$ where $c$ is the speed of light, $\lambda$ the wavelength and $f$ the frequency of a photon.
The general formula for Compton scattering is normally given as $$\lambda' = \lambda_C (1-\cos \alpha) + \lambda$$ where $\lambda'$ is the wavelength of the scattered photon, $\lambda$ the initial wavelength, $\lambda_C$ the Compton wavelength $\lambda_C = \frac{h}{m_e c}$ and $\alpha$ is the angle at which the photon scatters.
From this formula, we can see that the wavelength of a photon increases when it scatters at an electron. To visualize, the photon gives some of its energy to the electron. The energy of a photon is given as $$E = h f$$ or $E = \frac{hc}{\lambda}$ so if the wavelength increases, the energy of the photon decreases. To illustrate further, if you shoot blue light at a stationary electron, you end up with scattered red light.
For the case of electromagetic radiation (light!), you can convert between frequency and wavelength using $c = \lambda f$. Those formulas combined allow you to find the frequency and wavelength of a scattered photon.


text:
Q: Oracle won't open with certain user id I have an application that I run from the task scheduler.   It runs fine if I run it from my account, but not if I run it from a system account (this account has similar permissions as mine on the server (it is even an administrator), but has a non-expiring password.  When I run it from the system account it fails when opening Oracle.  My application does not throw an exception.  It just dies.  However, the event viewer has the following:
Application: M921.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
   at Oracle.DataAccess.Client.OracleException.get_Source()
   at M921.Program.Main(System.String[])

Faulting application name: M921.exe, version: 1.0.0.0, time stamp: 0x58c2d17b
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x037d2be3
Faulting process id: 0x564
Faulting application start time: 0x01d299ba8cb4ef63
Faulting application path: D:\ScriptingApplications\M921\M921.exe
Faulting module path: unknown
Report Id: cbb37f30-05ad-11e7-97a4-005056824753

If I examine the trace logs I find this:
<msg time='2017-03-10T10:11:23.998-05:00' org_id='oracle' comp_id='clients'
 msg_id='3079483318' type='INCIDENT_ERROR' level='1'
 host_id='PWN401AV1369' host_addr='::1' prob_key='oci 24550 [3221225477]'
 errid='15721' detail_path='C:\Users\scrptadmin\Oracle\oradiag_scrptadmin\diag\clients\user_scrptadmin\host_2384361277_82\trace\ora_3292_1232.trc'>
 <txt>Errors in file

C:\Users\scrptadmin\Oracle\oradiag_scrptadmin\diag\clients\user_scrptadmin\host_2384361277_82\trace\ora_3292_1232.trc  (incident=15721):
oci-24550 [3221225477] [Unhandled exception: Code=c0000005 Flags=0
] [] [] [] [] [] [] [] [] [] []
 </txt>
</msg>
<msg time='2017-03-10T10:11:24.186-05:00' org_id='oracle' comp_id='clients'
 msg_id='dbgexProcessError:1266:3370026720' type='TRACE' level='16'
 host_id='PWN401AV1369' host_addr='::1'>
 <txt>Incident details in: C:\Users\scrptadmin\Oracle\oradiag_scrptadmin\diag\clients\user_scrptadmin\host_2384361277_82\incident\incdir_15721\ora_3292_1232_i15721.trc
 </txt>
</msg>

I have checked all of the permissions that I can think of.  The Database credentials are oracle credentials (not windows credentials;  username and password are part of the connection string)
I am using version 12.1.0 oracle client
I am baffled.

A: Since you have not provided any source code, I am only guessing here. To connect using a privileged account (SYSDBA or SYSOPER), you need to add the DBA privilege attribute to you connection string:
//Connect scott/tiger as SYSDBA
con.ConnectionString = "User Id=scott;Password=tiger;" +
  "DBA Privilege=SYSDBA;Data Source=oracle;";

Oracle documentation link


text:
Q: Conditioning Stata dataset on past values of variables I have a problem in conditioning the dataset I have on Stata. Basically I want to condition the presence in the dataset -within a certain group- of an observation for which a certain action is performed (as indicated by a variable) on the past values of another variable. So let's suppose I have the following
obs |  id  | action1 | action2 | year
  1  |   1  |    1    |    0    | 2000
  2  |   1  |    0    |    1    | 2001
  3  |   1  |    0    |    1    | 2002
  4  |   1  |    0    |    1    | 2002
  5  |   1  |    0    |    1    | 2003
  6  |   2  |    1    |    0    | 2000
  7  |   2  |    1    |    0    | 2001
  8  |   2  |    0    |    1    | 2002
  9  |   2  |    0    |    1    | 2002
 10  |   2  |    0    |    1    | 2003
And for each group identified by 'id' I want to keep the observation only if action 1 is performed or if action1 has been performed no earlier than 2 years before action2 has been performed. In this simplified example only observation 4 should be deleted. Please note that the 2 actions are not mutually exclusive and they can be performed more than once within the same year therefore looking at 2 observations in the past does not necessarily means to look at 2 years in the past.
A solution which I am not able to implement by code would be:
gen act1year= action1 * year
then by(id) store the value of act1year when they're different from 0 somewhere (I am not able to implement this)
and then by(id) keep if action1=1 or if action2[_n]=1 and the range year[_n] to year[_n]-2  contains at least one of the values in the previously stored variable.
I know probably my suggestion is not the easiest way to go and still I am not able to implement it, unfortunately I cannot manage to find a code that help me doing this. Hope you can help me. Thanks
Francesco

A: The following assumes certain things.
clear
set more off

input ///
obs  id  action1  action2  year
1  1  1  0  2000
2  1  0  1  2001
3  1  0  1  2002
4  1  0  1  2003
5  2  1  0  2000
6  2  0  1  2001
7  2  1  0  2002
8  2  0  1  2003
end

list, sepby(id)

*-----

bysort id (year) : keep if action1 | (action1[_n-1] + action1[_n-2] > 0)

list, sepby(id)

What is between parenthesis evaluates to one or zero depending on whether the inequality is true or false, respectively. This fragment indicates if action 1 was taken in either of the previous two observations.
You need to decide what to do with the first two observations, as they can't be compared with exactly two previous observations (they don't exist). In the following example they are always kept, because comparing with a non-existant observation in this case implies adding missing values, which results in missing. A missing is considered a very large number in Stata.
You can also work with time-series operators (help tsvarlist, help xtset) and really respect the time variable. Here, I work with the previous two observations. That may or may not coincide with the previous two time points.
I think your two actions are mutually exclusive, but you are not explicit about it.


text:
Q: How do you use the EM algorithm to calculate MLEs for a latent variable formulation of a zero inflated Poisson model? The zero inflated Poisson regression model is defined for a sample $(y_1,\ldots,y_n)$ by
$$
Y_i = \begin{cases} 0 & \text{with probability} \ p_i+(1-p_i)e^{-\lambda_i}\\
k & \text{with probability} \ (1-p_i)e^{-\lambda_i} \lambda_{i}^{k}/k!
\end{cases}$$
and it further assumes that the parameters $\mathbf{\lambda} = (\lambda_1, \dots, \lambda_n)$ and $\textbf{p} = (p_1, \dots, p_n)$ satisfy
$$\eqalign{
\log(\mathbf{\lambda}) &= \textbf{B} \beta \\
\text{logit}(\textbf{p}) &= \log(\textbf{p}/(1-\textbf{p})) = \textbf{G} \mathbf{\gamma}.
}$$
The corresponding log likelihood of the zero-inflated Poisson regression model is
$$\eqalign{
L(\gamma,\mathbf{\beta}; \mathbf{y}) &= \sum_{y_i=0} \log(e^{G_i \gamma}+\exp(-e^{\textbf{B}_i \mathbf{\beta}})) +\sum_{y_i >0} (y_i \textbf{B}_i \mathbf{\beta}-e^{\textbf{B}_i \mathbf{\beta}})\\
&\quad -\sum_{i=1}^{n} \log(1+e^{G_{i} \gamma})-\sum_{y_i >0} \log(y_{i}!)}$$
Here, $\mathrm{B}$ and $\mathrm{G}$ are the design matrices. These matrices could be the same, depending on the features one desires to use for the two generating processes. They have the same number of rows, however.
Assuming that we could observe $Z_i = 1$ when $Y_i$ is from the perfect, zero state and $Z_i = 0$ when $Y_i$ is from the Poisson state the log-likelihood would be
$$L(\gamma,\mathbf{\beta}; \mathbf{y}, \mathbf{z}) = \sum_{i=1}^{n} \log(f(z_i|\mathbf{\gamma}))+\sum_{i=1}^{n} \log(f(y_i|z_i, \mathbf{\beta}))$$
$$ = \sum_{i=1}^{n} z_{i} (\textbf{G}_i \gamma-\log(1+e^{G_{i} \gamma}))+ -\sum_{i=1}^{n} (1-z_{i})\log(1+e^{G_{i} \gamma})+ \sum_{i=1}^{n} (1-z_i)[y_{i} \textbf{B}_i \beta-e^{\textbf{B}_i \beta} - \log(y_{i}!)]$$
The first two terms are the loss in a logistic regression to separate $z_i=0$ from $z_i=1$. The second term is a regression to the points generated by the Poisson process.
But aren't latent variables unobservable? The purpose is to maximize the first log-likelihood. But we have to introduce latent variables and derive a new log-likelihood. Then using the EM algorithm, we can maximize the second log-likelihood. But this assumes that we know that either $Z_i = 0$ or $Z_i = 1$?

A: The root of the difficulty you are having lies in the sentence:

Then using the EM algorithm, we can maximize the second
  log-likelihood.

As you have observed, you can't. Instead, what you maximize is the expected value of the second log likelihood (known as the "complete data log likelihood"), where the expected value is taken over the $z_i$.
This leads to an iterative procedure, where at the $k^{th}$ iteration you calculate the expected values of the $z_i$ given the parameter estimates from the $(k-1)^{th}$ iteration (this is known as the "E-step",) then substitute them into the complete data log likelihood (see EDIT below for why we can do this in this case), and maximize that with respect to the parameters to get the estimates for the current iteration (the "M-step".)
The complete-data log likelihood for the zero-inflated Poisson in the simplest case - two parameters, say $\lambda$ and $p$ - allows for substantial simplification when it comes to the M-step, and this carries over to some extent to your form.  I'll show you how that works in the simple case via some R code, so you can see the essence of it.  I won't simplify as much as possible, since that might cause a loss of clarity when you think of your problem:
# Generate data
# Lambda = 1,  p(zero) = 0.1
x <- rpois(10000,1)
x[1:1000] <- 0

# Sufficient statistic for the ZIP
sum.x <- sum(x)

# (Poor) starting values for parameter estimates
phat <- 0.5
lhat <- 2.0

zhat <- rep(0,length(x))
for (i in 1:100) {
  # zhat[x>0] <- 0 always, so no need to make the assignment at every iteration
  zhat[x==0] <- phat/(phat +  (1-phat)*exp(-lhat))

  lhat <- sum.x/sum(1-zhat) # in effect, removing E(# zeroes due to z=1)
  phat <- mean(zhat)

  cat("Iteration: ",i, "  lhat: ",lhat, "  phat: ", phat,"\n")
}

Iteration:  1   lhat:  1.443948   phat:  0.3792712
Iteration:  2   lhat:  1.300164   phat:  0.3106252
Iteration:  3   lhat:  1.225007   phat:  0.268331
...
Iteration:  99   lhat:  0.9883329   phat:  0.09311933
Iteration:  100   lhat:  0.9883194   phat:  0.09310694

In your case, at each step you'll do a weighted Poisson regression where the weights are 1-zhat to get the estimates of $\beta$ and therefore $\lambda_i$, and then maximize:
$\sum (\mathbb{E}z_i\log{p_i} + (1-\mathbb{E}z_i)\log{(1-p_i)})$
with respect to the coefficient vector of your matrix $\mathbf{G}$ to get the estimates of $p_i$.  The expected values $\mathbb{E}z_i = p_i/(p_i+(1-p_i)\exp{(-\lambda_i)})$, again calculated at each iteration.
If you want to do this for real data, as opposed to just understanding the algorithm, R packages already exist; here's an example http://www.ats.ucla.edu/stat/r/dae/zipoisson.htm using the pscl library.
EDIT: I should emphasize that what we are doing is maximizing the expected value of the complete-data log likelihood, NOT maximizing the complete-data log likelihood with the expected values of the missing data/latent variables plugged in.  As it happens, if the complete-data log likelihood is linear in the missing data, as it is here, the two approaches are the same, but otherwise, they aren't.


text:
Q: Search and sort documents based on weights assigned to tags I am looking for an alternative for Tag based search in mongodb which could perform well in a heavy traffic application (atleast 100 req/sec)
Below is an example of the docs.
Doc1
"unique_id": "cOxmb8",
"name":"Jon Snow",
"knows":"Nothing",
"height":173,
"tags": [
  "leader",
  "king in the north",
  "stark",
  "CastleBlack"]

Doc2
"unique_id": "qxbf0d",
"name":"Samwell Tarly",
"knows":"Everything",
"height":172,
"tags": [
  "insightful",
  "sam the slayer",
  "tarly",
  "CastleBlack"]

Collection has 900k docs. Backend api is in golang.
If I search for "CastleBlack insightful leader" both docs would match 2 tags. But I want "Samwell Tarly" to show up first. The results should be optimized for high traffic application.
I achieved this by assigning weights dynamically to the search string in my api and sorting them on the sum of the weights. Below is the pipeline I used in my aggregation.
CastleBlack := 3,
insightful := 2,
leader := 1

[{"$match":
    {"root_words":{"$all":["CastleBlack"]}},
    {"root_words":{"$all":["insightful"]}},
    {"root_words":{"$all":["leader"]}}
},
{"$project":
    {
    "unique_id":0,
    "name":1,
    "knows":1,
    "weight":
        {"$add":[
            {"$multiply":[{"$size":{"$setIntersection":["$root_words",["CastleBlack"]]}},3]},
            {"$multiply":[{"$size":{"$setIntersection":["$root_words",["insightful"]]}},2]},
            {"$multiply":[{"$size":{"$setIntersection":["$root_words",["leader"]]}},1]}
        ]}

    }
},
{"$sort":
    {
        "weight": -1,
    }

}
]

I used $all instead of $in because $in didn't perform well under load test(100 req/sec). Full text search is of no use in heavy traffic applications. Is there any better way to achieve this and possibly optimize and make the query faster?


text:
Q: Why does $\int_{-\infty}^\infty \operatorname{sech} x \, \mathrm{d}x =\pi\;$? According to Desmos,
$$\int_{-\infty}^\infty \operatorname{sech} x \, \mathrm{d}x = 3.14159265359$$
Why is $\pi$ here in this definite integral?

A: Note that $\operatorname{sech}(x) = \frac{2e^x}{e^{2x}+1}.$ In that case:
$$I= \int_{-\infty}^{\infty} \operatorname{sech}(x) dx = \int_{-\infty}^{\infty} \frac{2e^x}{e^{2x}+1} dx.$$
For the later, $u=e^x \implies du=e^xdx$,
$$I = 2\int_0^{\infty}  \frac{1}{u^2+1} du = 2 \left(\lim_{u \to \infty} \arctan(u)  - 0 \right) = \pi.$$

A: Just as you do it with trigonometric functions, in the case of hyperbolic functions, you can use the corresponding tangent half-angle substitution
$$x=2 \tanh ^{-1}(t)\implies dx=\frac{2}{1-t^2}\,dt$$
$$I=\int \text{sech}(x)\,dx=\int\frac{2}{t^2+1}\,dt=2 \tan ^{-1}(t)=2 \tan ^{-1}\left(\tanh \left(\frac{x}{2}\right)\right)$$ Similarly
$$J=\int \text{csch}(x)\,dx=\log \left(\tanh \left(\frac{x}{2}\right)\right)$$

A: *

*That's magic of Gudermannian function, a bridge (other than Jacobi elliptic functions) relates trigonometric to hyperbolic functions:

\begin{align}
  \operatorname{gd} x &= \int_0^x \operatorname{sech} t \, dt \\
  &= \tan^{-1} \sinh x \\
  \operatorname{gd}^{-1} t &= \int_0^t \operatorname{sec} x \, dx \\
  &= \tanh^{-1} \sin t \\
\end{align}

*

*Further coincidence:

\begin{align}
  \operatorname{gd} x &= \tan^{-1} \sinh x \\
  &= \sin^{-1} \tanh x \\
  \operatorname{gd}^{-1} t &= \tanh^{-1} \sin t \\
  &= \sinh^{-1} \tan t
\end{align}

*

*Domain for $t$ is always $\left( -\frac{\pi}{2},\frac{\pi}{2} \right)$


text:
Q: Solution verification for an equation of exponentially distributed random variables Given two i.i.d. random variables $X,Y$, such that $X\sim \exp(1),  Y \sim \exp(1)$. I am looking for the probability $\Phi$. However, the analytical solution that I have got does not match with my simulation. I am presenting it here with the hope that someone with rectifies my mistake.
$$\Phi =\mathbb{P}\left[P_v \geq A + \frac{B}{Y}\right] $$
$$
P_v=
\left\{
\begin{array}{ll}
 a\left(\frac{b}{1+\exp\left(-\bar \mu\frac{P_s X}{r^\alpha}+\varphi\right)}-1\right), & \text{if}\ \frac{P_s X}{r^\alpha}\geq P_a,\\
0, & \text{otherwise}.
\end{array}
\right.
$$

My solution
\begin{multline}
  \Phi = \mathbb{P}\left[ a\left(\frac{b}{1+\exp\left(-\bar \mu\frac{P_s X}{r^\alpha}+\varphi\right)}-1\right) \geq A  +  \frac{B}{Y}\right]\mathbb{P}\left[X \geq \frac{P_ar^\alpha}{P_s}\right] + {\mathbb{P}\left[0 \geq A + \frac{B}{Y}\right] \mathbb{P}\left[X \geq \frac{P_ar^\alpha}{P_s}\right]}
\end{multline}
Given that $\mathbb{P}[0>A+\frac{B}{Y}] = 0$
$$ \Phi = \mathbb{P}\left[a\left(\frac{b}{1+\exp\left(-\bar \mu\frac{P_s X}{r^\alpha}+\varphi\right)}-1\right) \geq A + \frac{B}{Y}\right] \exp\left(-\frac{P_ar^\alpha}{P_s}\right) $$
Now consider only the first part of the expression on the right side of the equation, and letting $c = \frac{\bar{\mu}P_s}{r^{\alpha}}$.
$$ \mathbb{P}\left[a\left(\frac{b}{1+\exp\left(-\bar \mu\frac{P_s X}{r^\alpha}+\varphi\right)}-1\right) \geq A + \frac{B}{Y}\right] = \mathbb{P}\left[\frac{ab}{1+\exp\left(-c X+\varphi\right)}-a \geq A + \frac{B}{Y}\right]$$
consider $D = A + a$, with mathematical manipulations and using the fact that $\mathbb{E}[Y] = \frac{1}{\lambda} = 1$ and $\mathbb{E}[e^{-sX}] = \frac{1}{1+s}$, we get:
$$ \Phi = \mathbb{P} \left[Y \geq \frac{1}{ab}\mathbb{E}_Y[DY + B]. \mathbb{E}_X [1+e^\varphi e^{-c X}] \right]  $$
$$\Phi = \exp\left(-\frac{D+B}{ab}\left(1 + \frac{e^\varphi}{1+c}\right)\right)\exp\left(-\frac{P_a r^\alpha}{P_s}\right) $$


text:
Q: Run spark program locally with intellij I tried to run a simple test code in intellij IDEA. Here is my code:
import org.apache.spark.sql.functions._
import org.apache.spark.{SparkConf}
import org.apache.spark.sql.{DataFrame, SparkSession}

object hbasetest {

  val spconf = new SparkConf()
  val spark = SparkSession.builder().master("local").config(spconf).getOrCreate()
  import  spark.implicits._

  def main(args : Array[String]) {
    val df = spark.read.parquet("file:///Users/cy/Documents/temp")
    df.show()
    spark.close()
  }
}

My dependencies list:
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.0</version>
<!--<scope>provided</scope>-->
</dependency>

<dependency>
  <groupId>org.apache.spark</groupId>
  <artifactId>spark-core_2.11</artifactId>
  <version>2.1.0</version>
  <!--<scope>provided</scope>-->
</dependency>

when I click with run button, it throw an exception:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.mapreduce.TaskID.<init>(Lorg/apache/hadoop/mapreduce/JobID;Lorg/apache/hadoop/mapreduce/TaskType;I)V

I checked this post, but situation don't change after making modification. Can I get some help with running local spark application in IDEA? THx.
Update: I can run this code with spark-submit. I hope to directly run it with run button in IDEA.

A: Are you using cloudera sandbox and running this application because in POM.xml i could see CDH dependencies '2.6.0-mr1-cdh5.5.0'.
If you are using cloudera please use the below dependency for your spark scala project because the 'spark-core_2.10' artifact version gets changed.
<dependencies>
  <dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.10.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>1.0.0-cdh5.1.0</version>
  </dependency>
</dependencies>

I used the below reference to run my spark application.
Reference: http://blog.cloudera.com/blog/2014/04/how-to-run-a-simple-apache-spark-app-in-cdh-5/

A: Here are the settings I use for Run/Debug configuration in IntelliJ:
*Main class:*
org.apache.spark.deploy.SparkSubmit

*VM Options:*
-cp <spark_dir>/conf/:<spark_dir>/jars/* -Xmx6g

*Program arguments:*
--master
local[*]
--conf
spark.driver.memory=6G
--class
com.company.MyAppMainClass
--num-executors
8
--executor-memory
6G
<project_dir>/target/scala-2.11/my-spark-app.jar
<my_spark_app_args_if_any>

spark-core and spark-sql jars are referred in my build.sbt as "provided" dependencies and their versions must match one of the Spark installed in spark_dir. I use Spark 2.0.2 at the moment with hadoop-aws jar version 2.7.2.

A: It may be late for the reply, but I just had the same issue. You can run with spark-submit, probably you already had related dependencies. My solution is:

*

*Change the related dependencies in Intellij Module Settings for your projects from provided to compile. You may only change part of them but you have to try. Brutal solution is to change all.

*If you have further exception after this step such as some dependencies are "too old", change the order of related dependencies in module settings.

A: I ran into this issue as well, and I also had an old cloudera hadoop reference in my code. (You have to click the 'edited' link in the original poster's link to see his original pom settings).
I could leave that reference in as long as I put this at the top of my dependencies (order matters!).  You should match it against your own hadoop cluster settings.
<dependency>
  <!-- THIS IS REQUIRED FOR LOCAL RUNNING IN INTELLIJ -->
  <!-- IT MUST REMAIN AT TOP OF DEPENDENCY LIST TO 'WIN' AGAINST OLD HADOOP CODE BROUGHT IN-->
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-mapreduce-client-core</artifactId>
  <version>2.6.0-cdh5.12.0</version>
  <scope>provided</scope>
</dependency>

Note that in 2018.1 version of Intellij, you can check Include dependiencies with "Provided" Scope which is a simple way to keep your pom scopes clean.


text:
Q: Excel Passing Wrong Date Value to VBA Function I have a worksheet with Date values. Excel interprets them correctly as Dates.
I have a VBA function that accepts a Date parameter.
When I call this function from the worksheet, sometimes it works correctly, and sometimes it doesn't. When I set a breakpoint in the function to examine the value passed in to the function from a cell that is incorrect, I discover that the wrong date is passed to the function--it's a day off from what should be passed to the function. The time value is correct.
This occurs in random places on the worksheet. In fact, in one cell that it didn't work correctly in yesterday, it now works correctly today.
The time portion of the value doesn't seem to make a difference. It can be 22:00 hours, 08:00 hours, it's still off by a day.
Does this make any sense to anyone? I am seriously perplexed.
Edit:
I didn't post any code because I don't find it to be a code issue. But here's the function signature:
Function timeToDecimal(time As Date, semanticTimeFormat As String) As Double

Setting a breakpoint on the first line of executable code in the function, I see that time is, for example, 1/1/1900 1:09:25 PM, when, on the worksheet it is clearly 1/2/1900  1:09:25 PM.
The issue isn't the VBA code I've written. The issue is Excel's passing of the data to the function. Is there some quirk I am unfamiliar with in the interface between Excel and VBA?

A: IT WOULD APPEAR! that for some reason there is a bug in excel where 1990 is incorrectly interpreted as a leap year - mentioned here: How to represent a DateTime in Excel in the accepted answer. and I tested on a sheet of my own, putting the dates from 01/01/1900 up to 31/12/1901 and then i used vba like this:
Sub test()

Dim str As String
Dim test As String
Dim test2 As String

test2 = ActiveCell.FormulaR1C1
test = ActiveCell.Value

While Not ActiveCell.FormulaR1C1 = ""
    str = ActiveCell.Value
    ActiveCell.Offset(0, 2).Value = str
    ActiveCell.Offset(1, 0).Activate

Wend

End Sub

and from 01/01/1900 - 01/03/1900 thats where dates were off. the dates as a result of vba were all behind a day from what was in the spreadsheet:
01/01/1900 0:00     31/12/1899
02/01/1900 0:00     01/01/1900 0:00
03/01/1900 0:00     01/02/1900 0:00
04/01/1900 0:00     01/03/1900 0:00
05/01/1900 0:00     01/04/1900 0:00
06/01/1900 0:00     01/05/1900 0:00
07/01/1900 0:00     01/06/1900 0:00
08/01/1900 0:00     01/07/1900 0:00
09/01/1900 0:00     01/08/1900 0:00
10/01/1900 0:00     01/09/1900 0:00
11/01/1900 0:00     01/10/1900 0:00
12/01/1900 0:00     01/11/1900 0:00
13/01/1900 0:00 01/12/1900 0:00

but  then starting 01/03/1990 the dates all lined up all the way to where i stopped at the end of 1901:
01/01/1901 0:00     01/01/1901 0:00
02/01/1901 0:00     01/02/1901 0:00
03/01/1901 0:00     01/03/1901 0:00
04/01/1901 0:00     01/04/1901 0:00
05/01/1901 0:00     01/05/1901 0:00
06/01/1901 0:00     01/06/1901 0:00
07/01/1901 0:00     01/07/1901 0:00
08/01/1901 0:00     01/08/1901 0:00
09/01/1901 0:00     01/09/1901 0:00
10/01/1901 0:00     01/10/1901 0:00
11/01/1901 0:00     01/11/1901 0:00
12/01/1901 0:00     01/12/1901 0:00

SO! you'll likely need a part of the function to check the date and from 31/12/1899 to 28/02/1900 you'll need to move the day up 1.
*you'll notice the second set of dates shown, the right hand side, is all formatted mm/dd/yyyy - i have no idea why excel did that....


text:
Q: Refactoring nasty rails controller I've been adding stuff a bit "blindly" to my controller and currently everything is working as it should, but the controller is gotten very messy. I'm using the gem rails_best_practices to tidy up my code, but it's currently not giving me any other fixes than moving some of the methods to the model from the controller.
Few questions

Is there other gems like rails_best_practices that I could use to figure out how to tidy up the controller?

and secondly

Any advice on what kind of changes  I should do here?

I'm doing some authorisation here for which I think I should use a gem to handle, but currently not using any authorisation gems.
Here's the mess
class OffersController < ApplicationController
  before_action :authenticate_user!
  before_action :offer_authorised, only: [:show]
  before_action :set_offer, only: [:accept, :reject, :send_signature_requests, :make_payment]
  before_action :is_authorised, only: [:accept, :reject]

  def create
    session[:return_to] ||= request.referer
    if !current_user.stripe_id?
      return redirect_to payment_path, alert: "Connect your bank accout before payments."
    end
    rental = Rental.find(offer_params[:rental_id])

    if rental && rental.user_id == current_user.id
      redirect_to(request.referrer, alert: "You cannot make an offer from your own property.") && return
    end

    if current_user.offers.accepted.any?
      redirect_to(request.referrer, alert: "You've already rented an apartment.") && return
    end

    if Offer.exists?(user_id: current_user.id, rental_id: offer_params[:rental_id])
      redirect_to(request.referrer, alert: "You can only make one offer at a time.") && return
    end

    @offer = current_user.offers.build(offer_params)
    @offer.landlord_id = rental.user.id
    if @offer.save
      redirect_to applications_path, notice: "Offer sent."
    else
      redirect_to request.referrer, flash: {error: @offer.errors.full_messages.join(", ")}
    end
  end

  def destroy
    @offer = Offer.find(params[:id])
    @offer.destroy
    redirect_to request.referrer, notice: "Offer deleted."
  end

  def accept
    if @offer.waiting?
      @offer.accepted!
      @offer.rental.update(active: !@offer.rental.active?)
      Offer.where(landlord_id: current_user.id, rental_id: @offer.rental.id).where.not(id: @offer.id).update_all(status: 2)
      @offer.rental.tours.destroy_all
      @offer.user.tours.destroy_all
      OfferMailer.with(offer: @offer, tenant: @offer.user_id, landlord: @offer.landlord_id).offer_accepted.deliver_now
      flash[:notice] = "Offer accepted."
    end
    redirect_to offer_accepted_path(@offer)
  end

  def reject
    if @offer.waiting?
      @offer.rejected!
      flash[:notice] = "Offer rejected."
    end
    redirect_to request.referrer
  end

  def update
    @offer = Offer.find(params[:id])
    if params[:offer][:due_date]
      @offer.update(due_date: params[:offer][:due_date])
    end
    if params[:offer][:rental_agreement]
      @offer.rental_agreement.purge
      @offer.rental_agreement.attach(params[:offer][:rental_agreement])
    end
    flash[:notice] = "Saved."
    redirect_to request.referrer
  end

  def show
    @offer = Offer.find(params[:id])
    @rental = @offer.rental_id ? Rental.find(@offer.rental_id) : nil
    @comments = Comment.where(offer_id: params[:id])
    @has_attachment_file_sent = Comment.where(user_id: @offer.landlord_id).any? { |obj| obj.attachment_file.attached? }
    respond_to do |format|
      format.html
      format.js { render layout: false }
    end
  end

  def pay_rent
    @offer = Offer.find(params[:id])
    @rental = @offer.rental_id ? Rental.find(@offer.rental_id) : nil
    if @rental.user_referral_reward_count > 0
      referral_rent_payment(@rental, @offer)
    else
      make_payment(@rental, @offer)
    end
    flash[:notice] = "Payment succeeded."
    redirect_to request.referrer
  end

  def send_signature_requests
    landlord_mail = User.where(id: @offer.landlord_id)[0].email
    tenant_mail = User.where(id: @offer.user_id)[0].email
    if  @offer.rental_agreement.attached?
      attachment_file = @offer.rental_agreement
      blob_path = rails_blob_path(attachment_file, disposition: "attachment", only_path: true)
      response = HTTParty.get("URL", body: {
        landlord_mail: landlord_mail,
        tenant_mail: tenant_mail,
        agreement_url: blob_path
      }.to_json,
      headers: {"Content-Type" => "application/json"})
      @offer.update(invitation_uuid: response.parsed_response["invitation_uuid"], document_uuid: response.parsed_response["document_uuid"])
      flash[:notice] = "Signature requests sent."
      redirect_to request.referrer
    else
      flash[:alert] = "Add aggreement."
      redirect_to request.referrer
    end
  rescue => e
    flash[:alert] = "Failed to send signature requests."
    redirect_to request.referrer
  end

  private

  def make_payment(rental, offer)
    unless offer.user_stripe_id.blank?
      payment_methods = Stripe::PaymentMethod.list(
        customer: offer.user_stripe_id,
        type: "sepa_debit"
      )
      payment_intent = Stripe::PaymentIntent.create({
        payment_method_types: ["sepa_debit"],
        payment_method: payment_methods.data[0].id,
        confirm: true,
        off_session: true,
        customer: offer.user_stripe_id,
        amount: offer.amount * 100,
        currency: "eur",
        application_fee_amount: offer.amount * 4,
        metadata: {"name" => User.where(id: offer.user_id).first.full_name, "offer" => offer.id},
        transfer_data: {
          destination: rental.user_merchant_id
        }
      })
    end
  rescue Stripe::CardError => e
    flash[:alert] = e.message
  end

  def referral_rent_payment(rental, offer)
    unless offer.user_stripe_id.blank?
      referrer = User.find_by(id: rental.user.id)
      customer = Stripe::Customer.retrieve(offer.user_stripe_id)
      charge = Stripe::Charge.create(
        customer: customer.id,
        amount: offer.amount * 100,
        description: "Rent for #{rental.listing_name}",
        currency: "eur",
        application_fee_amount: offer.amount * 2,
        metadata: {"name" => User.where(id: offer.user_id).first.full_name, "offer" => offer.id},
        transfer_data: {
          destination: rental.user_merchant_id
        }
      )
      referrer.update_attribute(:referral_reward_count, referrer.referral_reward_count -= 1)
    end
  rescue Stripe::CardError => e
    flash[:alert] = e.message
  end

  def offer_authorised
    unless Offer.exists?(["id = ? AND (landlord_id = ? OR user_id = ?) AND status = ?", params[:id], current_user.id, current_user.id, 1])
      redirect_to dashboard_path,
        alert: "You're not authorized."
    end
  end

  def set_offer
    @offer = Offer.find(params[:id])
  end

  def is_authorised
    redirect_to root_path, alert: "You're not authorized." unless current_user.id == @offer.rental.user_id
  end

  def offer_params
    params.require(:offer).permit(:amount, :rental_id, :status, :priority, :rental_agreement)
  end
end

A: Well you can try reek gem it helps to detect smells in code.


text:
Q: Save intermediate states of my Apache Spark pipeline I have a quite complex Apache PySpark pipeline which performs several transformations on a (very large) set of text files. The intended output of my pipeline are different stages of the pipeline. Which is the best way (i.e. more efficient but also more sparkling, in the sense of: more fitting the Spark programming model and style) to do this?
Right now, my code looks like the following:
# initialize the pipeline and perform the first set of transformations.
ctx = pyspark.SparkContext('local', 'MyPipeline')
rdd = ctx.textFile(...).map(...).map(...)

# first checkpoint: the `first_serialization` function serializes
# the data into properly formatted string.
rdd..map(first_serialization).saveAsTextFile("ckpt1")

# here, I have to read again from the previously saved checkpoint
# using a `first_deserialization` function that deserializes what has
# been serialized from the `firs_serialization` function. Then performs
# other transformations.
rdd = ctx.textFile("ckpt1").map(...).map(...)

and so on. I would like to get rid of the serialization methods and of the multiple save/read -- by the way, does it impact the efficiency? I assume yes.
Any hint?
Thanks in advance.

A: This seems obviously simple, because it is, but I would recommend writing the intermediate stages out while continuing to reuse the existing RDD (side bar: use datasets/dataframes instead of RDDs to get more performance) and continue processing, writing out intermediate results as you go.
There's no need to pay the penalty of reading from disk/network when you already have the data processed (ideally even cached!) for further usage.
Example using your own code:
# initialize the pipeline and perform the first set of transformations.
ctx = pyspark.SparkContext('local', 'MyPipeline')
rdd = ctx.textFile(...).map(...).map(...)

# first checkpoint: the `first_serialization` function serializes
# the data into properly formatted string.
string_rdd = rdd..map(first_serialization)
string_rdd.saveAsTextFile("ckpt1")

# reuse the existing RDD after writing out the intermediate results
rdd = rdd.map(...).map(...) # rdd here is the same variable we used to create the string_rdd results above. alternatively, you may want to use the string_rdd variable here instead of the original rdd variable.


text:
Q: Java Swing, making a gridlayout fill parent? I just recently started using Swing to create GUIs for programs, and it's been pretty fun to mess around with so far. However, I'm having an issue with a JPanel with the layout set to gridLayout. Right now it looks like this:

The grid on the right is a JPanel set to a GridLayout, with each cell being a bordered JLabel. The options on the left are also inside a JPanel, and the left JPanel and right JPanel are nested in a GridBagLayout set on a JFrame.
Essentially, my problem is that I want to "scale" the grid on the right so that each cell is a certain height and width. The grid itself will have a variable number of rows and columns, which are set when the program first starts up. Eventually, I plan to have the right JPanel in a JScrollPane (if that's how that works...), so I'm not really concerned about whether or not all of the grid shows up onScreen.
I tried setting the fill value for the gridLayout to "BOTH" and it gave me the following result:

This is closer to my intention, but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel. Additionally, I would want the JLabels to be the same height and width. However, I don't know exactly how to do that. I've been messing around with it for a while now, and I'm not sure if I'm just too much of a noob with Swing, or if I'm missing something in the documentation.
In the end, I'd like the grid cells to be a fixed height and width, no matter the number of cells, and no matter whether it goes offscreen or doesn't fill it.
(Also, I just thought, maybe it's not the best idea to code this and then shove it in a JScrollPane later and expect it to perform the same.... I guess I'll just see what happens.)

A:
but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel.

Check out Darryl's Stretch Icon which will allow the icon to resize to file the space available for the JLabel.


text:
Q: Angular Dynamic ROuting and Lazy loading I am trying to create my Angular routes dynamically from Database.
I somehow achieved it.
I followed AppIntializer for same.
App.module.ts
....
providers:[...{
        provide: APP_INITIALIZER,
        useFactory: AppInitializerFn,
        multi: true,
        deps: [AppConfigService,RouteConfigService]
      }...]

  export const AppInitializerFn = (
    appConfig: AppConfigService,
    routeConfigService:RouteConfigService) => {
    return () => {
      return appConfig.loadAppConfig().then(()=>{
        return  routeConfigService.configure();
      });
    };
  };

 configure() {
    appRoutes[1].children= [];
    return this.http.get('/assets/data/menu.json').toPromise()
    .then((data:Array<any>) => {

      data.forEach((x:any)=>{
        appRoutes[1].children.push({path:x.path,
          loadChildren: this.appConfig.getConfig().AppSpecificComponentURL+x.compPath
        });

      });

      var router: Router = this.injector.get(Router);
      router.resetConfig(appRoutes);
      console.log(appRoutes)
    });
  }

menu.json
 [
          { "path": "dashboard", "compPath":"dashboard/dashboard.module#DashboardModule","default":true},
          { "path": "customer", "compPath":"customer/customer.module#CustomerModule"},
          { "path": "employee", "compPath":"employee/employee.module#EmployeeModule"},
          { "path": "supplier", "compPath":"supplier/supplier.module#SupplierModule"}
    ]

Now the problem is Lazy loaded modules are not even compiling and so getting an error
"Cannot find module "./src/app/app-specific/employee/employee.module"
Any help appreciated.

A: I had find out a simple solution. It works for me. I'll Hope help somebody.

In pages.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';

import { DataService } from '@services/data-service.service';

import { DashboardComponent } from '../dashboard/dashboard.component';

import { ContactsComponent } from './contacts/contacts.component';
import { TaxDataComponent } from './tax-data/tax-data.component';
import { LocalsComponent } from './locals/locals.component';
import { ChildrenItem } from '@app/models/children-item.model';
import { AuthorisedLayoutComponent } from '@app/layout/authorised/authorised-layout/authorised-layout.component';
import { AddressesComponent } from './addresses/addresses.component';

const appRoutes: Routes = [
  {
    path: '',
    component: AuthorisedLayoutComponent,
    children: [ ],
  },
];

const components = {
  dashboardComponent: DashboardComponent,
  addressesComponent: AddressesComponent,
  contactsComponent: ContactsComponent,
  taxDataComponent: TaxDataComponent,
  localsComponent: LocalsComponent,
};

@NgModule({
  imports: [RouterModule.forChild(appRoutes)],
  exports: [RouterModule]
})
export class PagesRoutingModule {

  constructor(private dataService: DataService, private router: Router) {
    this.dataService.getMenu().subscribe(
      (menu: any) => {
        this.dataService.user.menu = menu;

        const customRoutes = [];

        const key = 'main';
        if (this.dataService && this.dataService.user && menu
          && menu.navigation && menu.navigation[key]) {

          menu.navigation[key].map((x: any) => {
            const el: any = {};
            el.path = x.path;
            if (x.children) {
              el.children = [];
              x.children.forEach((child: ChildrenItem) => {
                if (!components[child.component]) {
                  console.error(`Component: ${child.component} doesn't exists!`);
                }
                el.children.push({
                  path: child.path,
                  component: components[child.component]
                });
              });
            }
            if (x.pathMatch) {
              el.pathMatch = x.pathMatch;
            }
            if (x.component) {
              if (!components[x.component]) {
                console.error(`Component: ${x.component} doesn't exists!`);
              }
              el.component = components[x.component];
            }
            if (x.redirectTo) {
              el.redirectTo = x.redirectTo;
            }
            if (x.canActivateChild) {
              el.canActivateChild = [components[x.canActivateChild]];
            }
            customRoutes.push(el);
          });
        }

        customRoutes.forEach((x: any) => appRoutes[0].children.push(x));

        this.router.config.forEach((child: any) => {
          if (child.path === 'pages' && child._loadedConfig) {
            child._loadedConfig.routes.forEach((x: any) => {
              if (x.path === '') {
                x.children = customRoutes;
              }
            });
          }
        });

        RouterModule.forChild(appRoutes);
      }, error => console.log(error)
    );

  }
}

And the response from server is some like this:
{
    user: {
      name: 'crivero',
      roles: ['admin']
    },
    navigation: {
      main: [
        { path: 'dashboard', component: 'dashboardComponent' },
        { path: 'addresses-app', component: 'addressesComponent' },
        { path: 'contacts-app', component: 'contactsComponent' },
        { path: 'tax-data-app', component: 'taxDataComponent' },
        { path: 'locals-app', component: 'localsComponent' },
      ],
      basePlatform: [
        { path: 'contactos', pathMatch: 'full', redirectTo: 'contactos/list' },
        {
          path: 'contactos',
          canActivateChild: 'authGuardService',
          children: [
            {
              path: 'list',
              component: 'contactsListComponent',
              data: {}
            }, {
              path: 'new',
              component: 'contactsNewComponent',
              data: {}
            }, {
              path: ':id/edit',
              component: 'contactsEditComponent',
              data: {}
            },
          ],
          data: { roles: [] }
        },
      ],
    },
    menus: {
      main: [
        { name: 'Inicio', link: '/pages/dashboard' },
        { name: 'Direcciones', link: '/pages/addresses-app' },
        { name: 'Contactos', link: '/pages/contacts-app' },
        { name: 'Datos fiscales', link: '/pages/tax-data-app' },
        { name: 'Locales', link: '/pages/locals-app' },
      ],
      basePlatform: [
      ]
    }
}

data-service.service.ts

  getMenu() {
    return this.httpClient.get(config.baseUrl + config.apiGetMenu,
      { headers: this.getHeaders() }).pipe(
        map((response: NavigationMenu) => {
          // return response;
          return customMenuMock;
        }),
        catchError((error: Response) => {
          return throwError('Fail to get data from server');
        },
        ),
      );
  }

A: This is not the right way to configure Dynamic Routes. calling an http request from app.module.ts is like killing Angular;  You should first subscribe the dynamic menu in a service file.Then u will get a data like this
 [
          { "path": "dashboard", "compPath":"dashboard/dashboard.module#DashboardModule","default":true},
          { "path": "customer", "compPath":"customer/customer.module#CustomerModule"},
          { "path": "employee", "compPath":"employee/employee.module#EmployeeModule"},
          { "path": "supplier", "compPath":"supplier/supplier.module#SupplierModule"}
    ];
let x = selected Array;

let appRoutes = [];
 x.forEach(val => {
      appRoutes.push({
          path: val.path,
          loadChildren: val.compPath
      };
  });

then import like this - RouterModule.forRoot(AppRoutes) in app.module.ts
This is just a bare idea of the workflow. hopes you understand.
Also using promise in angular is not a best practise;


text:
Q: Getting out of memory exception when picking images Camera/Gallery I am picking Images through Camera/Gallery using the below code and save their paths in Sq Lite that's fine. Everything is working.
My problem is after pick image 40 or above, I am getting an out of memory exception.
Can some one help please?
chooseImageFromCamera:
try {
    String folder_main = "FOSImages";
    File f = new File(Environment.getExternalStorageDirectory(), folder_main);
    if (!f.exists()) {
        f.mkdirs();
    }
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f1 = new File(android.os.Environment.getExternalStorageDirectory(),
                "/" + folder_main + "/" + System.currentTimeMillis() + "_temp.jpg");
    fileName = System.currentTimeMillis() + "_temp.jpg";
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f1));
    startActivityForResult(intent, Constants.CAMERA_REQUEST_CODE);
} catch (Throwable throwable) {
    throwable.printStackTrace();
}

onCaptureImageResult:
try {
    File f = new File(Environment.getExternalStorageDirectory() + "/" + "FOSImages");
    for (File temp : f.listFiles()) {
        if (temp.getName().equals(fileName)) {
            f = temp;
            origionalPaths.add(f);
            if (imageType == 1) {
                stickerBeforeFileList.add(f);
                horizentalAdapter1.notifyDataSetChanged();
            } else {
                stickerAfterFileList.add(f);
                horizentalAdapter2.notifyDataSetChanged();
            }
        }
    }

} catch (Throwable throwable) {
    throwable.printStackTrace();
}

 chooseFromLibrary:
try {
    Intent intent = new Intent(
            Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    intent.setType("image/*");
    startActivityForResult(
            Intent.createChooser(intent, "Select File"),
            Constants.GALLARY_REQUEST_CODE);

} catch (Throwable throwable) {
    throwable.printStackTrace();
}

onSelectFromGalleryResult:
try {
    Uri selectedImageUri = data.getData();
    File shopImageFile1 = new
 File(Utilities.getRealPathFromURI(selectedImageUri, UberFormActivity.this));
    if (imageType == 1) {
        stickerBeforeFileList.add(carImageFile1);
        horizentalAdapter1.notifyDataSetChanged();
    } else {
        stickerAfterFileList.add(carImageFile2);
        horizentalAdapter2.notifyDataSetChanged();
    }
} catch (Throwable throwable) {
    throwable.printStackTrace();
}


text:
Q: Tracing a program with multiple forks in if/else block I'm trying to trace this program. I see that it outputs 4 times when I run it, but I don't understand why.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
    if (fork() == 0)
        fork();
    else
    {
        fork();
        fork();
        printf("%d\n", getpid());
    }
}

To my knowledge, the if/else forks my program, then if we're in the child, it forks again. If we're in the parent, it runs the code in the else block. I get confused when I try to trace after this point, though.

*

*Once the else statement forks again, aren't we in another child?

*When do the children stop being spawned?

A: After getting it explained for me, I understand it now.
The first fork will spawn a child (let's call it c1):
if (fork() == 0)

The return value of fork is 0 when you're in the child. So, c1 will execute the if statement block:
fork();

The child created here, c2 (as well as c1) will both die, as they aren't going to execute the else block.

Meanwhile, the parent process will execute the else block.
fork();

This creates another child of the original parent process (c3). c3 will execute the next fork in the else block.
fork();

Now, we'll have c4, too.

Meanwhile, the original parent process will still have a fork that hasn't run. This creates the final child process, c5.
At the end of the run, there will be 4 prints: The original parent process, c3, c4, and c5.

A: *

*fork in if is executed. There are two processes now child C1 and parent. Parent gets a non-zero number. Child gets 0.

*Parent enters else block as it got non-zero return from fork. Now again there are two processes child C2 and parent - after executing fork in else block.

*Parents fork again with C3.

*Child C2 forks again with C4.

*Note that C1 also forked but it will not do anything other than ending the if block. (We don't care about this).
So 4, printf execution - one by Parent, C2, C3 and C4.
The key one line Golden rule is:-

Both parent and child process start their execution right after the system call fork()

And each process ends when the control reaches the end of the function here it is main(). (Answering your last question).


text:
Q: Initialising std::shared_ptr> using braced-init I have the following shared_ptr to a map:
std::shared_ptr<std::map<double, std::string>>

and I would like to initialise it using braced-init. Is it possible?
I've tried:
std::string s1("temp");
std::shared_ptr<std::map<double, std::string>> foo = std::make_shared<std::map<double, std::string>>(1000.0, s1);

but that gives the following error when compiled using Xcode 6.3:
/usr/include/c++/v1/map:853:14: Candidate constructor not viable: no known conversion from 'double' to 'const key_compare' (aka 'const std::__1::less<double>') for 1st argument

I've tried other variations of the first parameter (1000.0) without success.
Can anyone help?

A: std::map has an initializer-list constructor:
map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());

We can create a map using this constructor quite easily:
std::map<double,std::string> m1{{1000.0, s1}};

To use it in make_shared, we need to specify which instantiation of initializer_list we're providing:
auto foo = std::make_shared<std::map<double,std::string>>
           (std::initializer_list<std::map<double,std::string>::value_type>{{1000.0, s1}});

That looks really clumsy; but if you need this regularly, you could tidy it up with aliases:
#include <string>
#include <map>
#include <memory>

std::string s1{"temp"};

using map_ds = std::map<double,std::string>;
using il_ds = std::initializer_list<map_ds::value_type>;

auto foo = std::make_shared<map_ds>(il_ds{{1000.0, s1}});

You might instead prefer to define a template function to wrap the call:
#include <string>
#include <map>
#include <memory>

template<class Key, class T>
std::shared_ptr<std::map<Key,T>>
make_shared_map(std::initializer_list<typename std::map<Key,T>::value_type> il)
{
    return std::make_shared<std::map<Key,T>>(il);
}

std::string s1{"temp"};
auto foo = make_shared_map<double,std::string>({{1000, s1}});

A: Your problem is that you didn't actually put any braces in your initializer.  I needed the following to get it to work:
auto foo = std::make_shared<std::map<double, std::string> >(
                         std::map<double, std::string>({{1000.0, s1}})
           );

The double std::map<double, std::string> bugs me.  It really ought to be able to work out one of them given the other ... but gcc 5.3.0 wouldn't play ball.
You will definitely need the double braces.  (Once to say you are initializing a map, once to delimit each entry.)

A: you can do it without std::make_shared:
std::shared_ptr<std::map<double,std::string>> ptr(new std::map<double,std::string>({{1000.0, "string"}}));

A: This is a refinement and generalization of the answer by Toby Speight.
template<class Container>
std::shared_ptr<Container>
make_shared_init_list(std::initializer_list<typename Container::value_type> il) {
    return std::make_shared<Container>(il);
}

Usage:
auto foo = make_shared_init_list<std::map<double,std::string>>({{1000, "hi"}});

The advantage is that it retains the usage of unique_ptr by accepting the map as the template parameter instead of the key and value types. It also works transparently with map, unordered_map, or any other container constructors that can accept initializer lists.

A: Change the key's type.
double is a bad type for keys because there is no operator== for it and different byte sequences can represent the same floating point value.

A: Someting similar to this should do it...
 std::string s1("temp");

 std::map<double, std::string> *m = new std::map<double, std::string>{{100., s1}};

 auto foo = std::shared_ptr<std::map<double, std::string>>(m);

or as a oneliner
auto foo2 = std::shared_ptr<std::map<double, std::string>>(new std::map<double, std::string>{{100., s1}});

(Sorry, first missed the requirement for initializer list)


text:
Q: multi-condition left join conditions in MS access Is there another way to write the following poorly performing in Access (Jet) SQL?
SELECT *
FROM
  Det left join
  (select * from Inv inner join Dep on Inv.SN=Dep.SN) as mass ON
    (Det.RecSN = mass.SelfID) and (Det.DDTime between mass.Start and mass.End)

eliminating the sub-select and doing a direct join does not work, giving a JOIN expression not supported Error in the following snippet
SELECT *
FROM
  Det left join
  (Inv inner join Dep on Inv.SN=Dep.SN) ON
    (Det.RecSN = Inv.SelfID) and (Det.DDTime between Dep.Start and Dep.End)

The following produces a result, but suppresses Det records that don't match up to one or the other of the inner tables
SELECT *
FROM
  Det inner join
  (Inv inner join Dep on Inv.SN=Dep.SN) ON
    (Det.RecSN = Inv.SelfID) and (Det.DDTime between Dep.Start and Dep.End)

adding another set of parentheses doesn't help other than helping Access highlight the join clause that's problematic
SELECT *
FROM
  Det left join
  (Inv inner join Dep on Inv.SN=Dep.SN) ON
    ((Det.RecSN = Inv.SelfID) and (Det.DDTime between Dep.Start and Dep.End))

dropping the between clause to a where condition results in no left join characteristics (DDTime isn't between NULL and NULL) and can lead to too many entries being held in memory while other joins are being performed to this whole mess, and can make it more logically difficult to add other where conditionals.
SELECT *
FROM
  Det left join
  (Inv inner join Dep on Inv.SN=Dep.SN) ON
    Det.RecSN = Inv.SelfID
WHERE
  Det.DDTime between Dep.Start and Dep.End

Adding a clause to make left join behavior re-emerge (but only for the portion in the ON clause) makes the world slow to a crawl, I think because Access doesn't know how to handle OR clauses without essentially running a union all in the background for each of the sides of the OR clause with the rest of the query.
SELECT *
FROM
  Det left join
  (Inv inner join Dep on Inv.SN=Dep.SN) ON
    Det.RecSN = Inv.SelfID
WHERE
  Det.DDTime between Dep.Start and Dep.End
  OR Dep.Start is NULL

My current work-around is to use the inner join method, then have another query that queries this one ("qCombo") and compares against the Det table for entries in Det, but not in qCombo using either a left join or where not exists. But this solution is just as inefficient (if not more so) than the sub-select method at top.
SELECT 'qCombo' as QueryName, *
FROM
  Det inner join
  (Inv inner join Dep on Inv.SN=Dep.SN) ON
    (Det.RecSN = Inv.SelfID) and (Det.DDTime between Dep.Start and Dep.End)

Select Det.*
FROM
  Det LEFT JOIN
  qCombo ON
    (Det.DDTime=qCombo.DDTime) and
    (Det.OtherFieldsInUniqueIdx=qCombo.OtherFieldsInUniqueIdx)
WHERE
  qCombo.QueryName IS NULL

Other possibilities?

A: Are the tables you are querying indexed?
If not, I would add an index to the "Inv.SN=Dep.SN"columns ,
" Det.RecSN = Inv.SelfID" and lastly the dates "Det.DDTime, Dep.Start".
NOTE: Be careful when adding indexes. Specially if you are working in your production database. Do your research first on creating indexes on transactional tables, to avoid locks.
If you already have these indexed, then how many records do you have on each table?
Suggestion: I would Separate the OR conditions in different queries and save into a temporary table, so that I can analyze the data sets and results. Ultimately, querying the temporary tables for the final join.
Rule of thumb:
query just one record Id, and see the performance.
Always check if index exists.
There may be various issues, and multiple approaches.