how we can convert a project written en c++ to dll file?

Question: I have a project written en c++ in which i use tesseract and opencv libraries.
the project contain one function which return an Int ant it has as parameter sting an int
<code>    int image_blanche(string nom_img,double k);
</code>
My question how can i convert it to dll file to use it in an other .net project
Comment: yes and it works without error
Comment: Change the build type to dll from the project properties and build.
Comment: By going to the project proprties menu, build option, and changing it from .EXE to .DLL.
Answer: Add a header file (e.g. "Defines.h") containing:
<code>#pragma once
#ifdef COREDLL_EXPORTS
#define COREDLL_API __declspec(dllexport)
#else
#define COREDLL_API __declspec(dllimport)
#endif
</code>
Your function should then be defined in a header like this:
<code>#include "Defines.h"
extern "C"
{
    COREDLL_API int image_blanche(char const* nom_img,double k);
};
</code>
...and implemented in a source file.
Compile that Project with COREDLL_EXPORTS defined as a DLL Project and use the dll in another project.
When building the DLL having COREDLL_EXPORTS defined you basically produce (=export) the symbols and place them in the DLL. When using the DLL in another project not having COREDLL_EXPORTS defined, you consuming/reading (=import) the symbols from the DLL.
Here is a good explanation of how you can then invoke the function from the DLL: Dynamic Invoke C++ DLL function in C#
Update: As MSalters correctly pointed out, using C-Style datatypes should be considered, as it plays friendly across DLL bounds. I changed the string parameter to use a character array instead.
Comment: Downvoted, as `extern "C"` is a particular bad idea for a function which takes a `std::string`. You absolutely want name mangling there. When the name mangling causes a linker error, it almost certainly prevented a runtime error in the interpretation of the `std::string` class,
Comment: what does it means extern "C"
Comment: extern "C" makes a function-name in C++ have 'C' linkage (compiler does not mangle the name).


Difference between inverse functions and inverse images

Question: Let $f: X \rightarrow Y$ be the function $f(x)=x^2$. Find $f^{-1}([-1,2])$.
Since the inverse function is not defined (since $f$ is not injective), how do I solve this?
Obviously, the soution is a set (a subset or $\mathbb{R}$), but since the "codomain" of $f$ is $[0,\infty)$, I don't see what values we are looking at... $f$ don't take on negative values, as far as I can see...
(Obviously, $\sqrt{-1}=i$, but this is not the answer...)
Answer: If $f:A\to B$ is a function and $B'\subseteq B$ then the inverse image of $B'$ under $f$ is defined as the set
$$f^{-1}(B')=\{x\in A:f(x)\in B'\}$$
In your case (assuming $X=Y=\mathbb{R}$),
$$f^{-1}([-1, 2]) = \{x \in \mathbb{R}: x^2 \in [-1, 2]\}$$
It's easy to see that this happens when $x\in [0, \sqrt 2]$. Notice that when we are talking about the inverse image of a set $B'$, it is not necessary for every element of $B'$ to be in the image of $f$ (i.e. $f$ doesn't have to be onto $B'$).
Comment: Thanks a lot! It was the "not need to be onto" part that confused me... from calculus-level and below I'm used to bijective functions (on the predefined domain).


Combining 2 drupal 7 sites - how to move modules?

Question: I am combining 2 drupal sites in to one, was not really my decision. What things I should be careful of and what pitfalls to avaoid?
List of things I will be combining

Content Types - Bundle copy module
View - Export functionality
Content Data - Feeds Importer
Blocks - Block Import Export module
Modules - I am going to install the standard modules from drupal projects and copy over any custom modules. How can I move the modules configurations?
Menus - How to move over the menus settings?
Users - Do not need to be migrated

There is not much taxonomy on the other site.
Anything else I am missing ?
Any suggestions on the sequence to do this?
Thanks.
Comment: Thanks @KrishnaMohan I updated the question. How do I move the modules specific settings though
Answer: I don't know for the menus, but for modules that doesn't implement any export functionality (either custom or contrib), take a look into the 'variables' table.
for how unpractical it may sounds, export the table data, keep only the 'module_*' entries of the module you need and insert the in the new site database, after you install the modules. You will have to change the sql from inserts to updates too.
yes, extremely unpractical, but i can't think to an automated routine for it.
also, probably you are forgetting to export users too (and custom roles).
Comment: Thanks, Users do not have to be migrated so can be skipped. Updating the question.


Install CoreOS on HP thin client

Question: I am trying to install CoreOS on HP Thin Client, but I am unable to format the flash drive.
i have created the USB boot as per https://github.com/jriguera/coreos-usb-creator - then i can login to the thin client, but when i try to install CoreOS, i get the following error
khine@coreos /tmp $ sudo su
bash-4.2# coreos-install -d /dev/sda -C stable -c cloud-config.yml
Checking availability of "local-file"
Fetching user-data from datasource of type "local-file"
Downloading the signature for http://stable.release.core-os.net/amd64-usr/766.3.0/coreos_production_image.bin.bz2...
2015-09-21 15:49:14 URL:http://stable.release.core-os.net/amd64-usr/766.3.0/coreos_production_image.bin.bz2.sig [543/543] -> "/tmp/coreos-install.kGAdApmypF/coreos_production_image.bin.bz2.sig" [1]
Downloading, writing and verifying coreos_production_image.bin.bz2...

bunzip2: I/O or other error, bailing out.  Possible reason follows.
bunzip2: No space left on device
    Input file = (stdin), output file = (stdout)
Cannot write to '-' (Broken pipe).
gpg: Signature made Wed Sep  2 04:32:09 2015 UTC using RSA key ID E5676EFC
gpg: key 93D2DCB4 marked as ultimately trusted
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: BAD signature from "CoreOS Buildbot (Offical Builds) <email@example.com>" [ultimate]
1: Download of coreos_production_image.bin.bz2 did not complete
141: Cannot expand coreos_production_image.bin.bz2 to /dev/sda
1: GPG signature verification failed for coreos_production_image.bin.bz2
/dev/sda: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/sda: calling ioctl to re-read partition table: Success
bash-4.2#

If i try to mount the drive, I get the following:
bash-4.2# mount /dev/sda /mnt
mount: /dev/sda is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/sda,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

the flash drive is 500Mb - any advise on how i can install coreOS on HP thin client t5720?
Comment: `bunzip2: No space left on device` - the downloaded bz2 file extracts to a 4.42Gb image. It's not going to fit on a 500Mb flash drive.
Answer: The answer is in your question:

the flash drive is 500Mb

while the error is

bunzip2: I/O or other error, bailing out.  Possible reason follows.
bunzip2: No space left on device

and your downloaded file http://stable.release.core-os.net/amd64-usr/766.3.0/coreos_production_image.bin.bz2 is 186 MB in compressed data and expanding to a 4,756,340,736 byte (4.5 GB) file.
Given that the largest available flash module for the t5720 is 1 GB (AH430AA according to the model's QuickSpecs), you only have the option of cracking the device open to see if the "flash module" is simply a USB storage stick or a PATA flash module. If it is, replacement with industry-standard parts should be smooth.
Comment: Keep in mind that CoreOS keeps two copies of the same `usr` partition so that it can pivot between them for updates.  In addition there is a root disk as well.  For a smaller version of this try the ISO image which does not have these partitions.  In that case you can just dd the image onto the drive.

Install CoreOS on HP thin client


How should I show that the Lie algebra $\mathfrak{sl}(4,\mathbb{C})$ is isomorphic to the Lie algebra $\mathfrak{so}(6,\mathbb{C})$?

Question: It is the Problem.3 of the "S.-T. Yau College Student Mathematics Contests 2022 Algebra and Number Theory"
The problem is

Give a direct proof that the Lie algebra $\mathfrak{sl}(4,\mathbb{C})$ is isomorphic to the Lie algebra $\mathfrak{so}(6,\mathbb{C})$.(You should construct a Lie algebra homomorphism and prove that it is an ismorphism; you should not use Dynkin diagrams or the classification theory of simple Lie algebras).
Comment: This amounts to considering for $\mathfrak{sl}(4, \Bbb C)$ the $6$-dimensional representation $\bigwedge^2 \Bbb C^4$, which comes equipped with a symmetric bilinear form.
Comment: For the record, that isomorphism also descends to (and conversely, can be constructed as coming from) an iso of the respective compact real forms. See https://math.stackexchange.com/q/570831/96384
Answer: As you surely suspect, there is not a "systematic" approach to these anomalous facts. :)
One example-documentation of many of these anomalous/sporadic isomorphisms/isogenies, etc. is at
https://www-users.cse.umn.edu/~garrett/m/v/sporadic_isogenies.pdf
That list/discussion is not exhaustive, because we could also do "quaternion" forms... not to mention maybe p-adic, etc. :)
Answer: I thought I'd give a pared down explanation with less Lie group focus and fewer (in my opinion) unneeded choices. The action of $\mathfrak{g} = \mathfrak{sl}_4$ on $V = \mathbb{C}^4$ naturally extends to an action on $\bigwedge^2V$ via:
$$ X \cdot (v\wedge w) = Xv \wedge w + v\wedge Xw.$$
To see where this comes from we only need to differentiate the diagonal group action $g\cdot(v\wedge w) = gv \wedge gw$. Now $\dim\bigwedge^2V = 6$ and it comes with a natural bilinear form $(a\wedge b, c\wedge d) := a\wedge b \wedge c\wedge d \in \bigwedge^4V \cong \mathbb{C}$. This last identification of $\bigwedge^4V \cong \mathbb{C}$ is a choice but it doesn't actually matter which choice we make as $\mathfrak{so}_n$ doesn't change if we rescale the bilinear form.
So now we have to show that $\mathfrak{sl}_4$ indeed acts as $\mathfrak{so}_6$ on this space. In other words, that it is skew for the bilinear form:
$$ (X\cdot\alpha,\beta) + (\alpha,X\cdot\beta) = 0$$
Take $\alpha = a \wedge b, \beta = c \wedge d$. Then the above reads:
$$ (Xa\wedge b \wedge c\wedge d) + (a\wedge Xb \wedge c\wedge d) + (a\wedge b \wedge Xc\wedge d) + (a\wedge b \wedge c\wedge Xd) = 0$$
Note this is just the natural action of $\mathfrak{sl}_4$ on $\bigwedge^4V$ the left hand side and that this equals zero for any $a,b,c,d$ is exactly the statement that the action on $\bigwedge^4V$ is trivial or equivalently that $\mathrm{tr} X =0$ which is exactly our definition of $X\in\mathfrak{sl}_4$.
For this last idea involving the trace we can find this by differentiating the  true definition of determinant:
$$ga\wedge gb \wedge gc\wedge gd = \det(g)a\wedge b \wedge c\wedge d$$
So to be precise we have found a map $\mathfrak{sl}_4 \to \mathfrak{so}_6$ and to see that this is an isomorphism we note that it is injective (not quite trivial but not hard) and both our Lie algebras have the same dimension. In fact as Torsten points out we only need to show that the map is not the zero map since both of them are simple Lie algebras.
Finally, note that this isomorphism descends to the real forms as well. For example:
$$ \mathfrak{su}(4) \cong \mathfrak{so}(6)$$
$$ \mathfrak{sl}(2,\mathbb{H}) \cong \mathfrak{so}(5,1)$$
$$ \mathfrak{su}(2,2) \cong \mathfrak{so}(4,2)$$
$$ \mathfrak{sl}(4,\mathbb{R}) \cong \mathfrak{so}(3,3)$$
$$ \mathfrak{su}(3,1) \cong \mathfrak{so}^*(6)$$
Comment: If the notation is supposed to agree with the one in Onishchik/Vinberg, I would have thought $\mathfrak{su}(2,2)$, being quasi-split, is $\simeq \mathfrak{so}(4,2)$. And $\mathfrak{so}(5,1)$, the one whose diagram has two black dots but no arrow, would be $\simeq \mathfrak{sl}_4(\mathbb H)$. The others are clear, O/V seem to call your very last $\mathfrak{u}^*_3(\mathbb H)$. https://books.google.de/books?id=l8nJCNiIQAAC&pg=PA229&redir_esc=y&hl=de#v=onepage&q&f=false
Comment: Still, this is my favourite answer to this question, although I upvoted the others as well. One more tiny remark: Once we have a map between the LAs in question, and if we already know they are both simple, we do not even have to check that it's injective, just that it's not identically zero, which might be a bit more on the trivial side of the spectrum (trivial $\leftrightarrow$ hard).
Comment: @TorstenSchoeneberg good catch I seem to have swapped those two around. The way I remember is that $\mathbb{P}(\mathbb{H}^2)\cong S^4 \subset \mathbb{P}(\mathbb{R}^{5,1})$
Answer: This is essentially an example of an accidental isomorphism, an isomorphism between a classical matrix Lie group and a spin group. (Equivalently, said isomorphisms at the lie algebra level, or complexified versions of these isomorphisms.) These are a special case of exceptional isomorphisms, which are isomorphisms between small objects in families of objects which do not generalize to the rest of the families (for example, only so many special orthogonal and special linear groups are isomorphic). These include finite simple groups especially. (Some use "accidental" and "exceptional" interchangeably or a little differently, but this is my preference to distinguish them.) While there is some general theory for determining these isomorphisms, such as Dynkin diagrams for complex lie algebras, there are typically more interesting (and mysterious) explanations lurking around that only work for small sets or low dimensions (an illustration of the Law of Small Numbers in action).

$\mathrm{SL}_4\mathbb{R}$ acts on $\Bbb R^4$, and consequently acts on $\Lambda^2\Bbb R^4$ (which is $6$-dimensional). This inherits an inner product from $\mathbb{R}^4$ by polarizing and linearizing Grammian determinants, i.e. extending
$$ \langle\alpha\wedge\beta,\gamma\wedge\delta\rangle=\det\left(\begin{bmatrix} | & | \\ \alpha & \beta \\ | & |\end{bmatrix}^T\begin{bmatrix}| & | \\ \gamma & \delta \\ | & |\end{bmatrix}\right)=\det\begin{bmatrix}\langle\alpha,\gamma\rangle & \langle\alpha,\delta\rangle \\ \langle\beta,\gamma\rangle & \langle\beta,\delta\rangle \end{bmatrix}, $$
but this is actually not what we're interested in! (It is, however, how you calculate angles between 2D planes in 4D, so I thought I'd mention it.) Instead, we can be interested in the symmetric bilinear form
$$ (\alpha\wedge\beta,\gamma\wedge\delta):=\alpha\wedge\beta\wedge\gamma\wedge\delta, $$
where we identify $\Lambda^4\mathbb{R}^4$ with $\mathbb{R}$ via $\lambda(e_1\wedge e_2\wedge e_3\wedge e_4)\leftrightarrow\lambda$. This is a pseudo-Euclidean inner product with signature $(3,3)$. Two canonical choices of 3D subspaces, in the bilinear form restricts to a positive- or negative-definite signature, are the eigenspaces $\Lambda^2_+$ and $\Lambda^2_-$ of the Hodge star operator $\star\substack{\curvearrowright\\~}\Lambda^2\mathbb{R}^4$.
To understand this, consider the oriented Grassmanian $\widetilde{\mathrm{Gr}}_2\mathrm{R}^4$ which paramatrizes the oriented 2D subspaces of $\mathbb{R}^4$. This may be identified with a subset of $\Lambda^2\mathbb{R}^4$: if an oriented 2D subspace has an ordered orthonormal basis $\{e_1,e_2\}$, it corresponds to $e_1\wedge e_2\in\Lambda^2\mathbb{R}^4$ (note this is well-defined: if it has another ordered orthonormal basis $\{f_1,f_2\}$, then $e_1\wedge e_2=f_1\wedge f_2$ within $\Lambda^2\mathbb{R}^4$). (Also note $\widetilde{\mathrm{Gr}}_2\mathbb{R}^4$ is a double cover of the unoriented Grassmanian $\mathrm{Gr}_2\mathbb{R}^4$ which may be identified with a subspace of projectivization $\mathbb{P}(\Lambda^2\mathbb{R}^4)$.) Taking orthogonal complements (of oriented subspaces) is an involution of $\widetilde{\mathrm{Gr}}_2\mathbb{R}^4$ which extends to a linear operator $\star$ on $\Lambda^2\mathbb{R}^4$ with eigenvalues $\pm1$ both of multiplicity $3$.
The $\pm1$-eigenspaces $\Lambda^2_{\pm}$ are spanned by elements of the form $e_i\wedge e_j+ e_k\wedge e_\ell$ with $ijk\ell$ an even or odd permutation of $1234$ respectively. Under the identification $\Lambda^2\mathbb{R}^4\cong\mathfrak{so}(4)$, where $\alpha\wedge\beta$ (with $\{\alpha,\beta\}$ orthonormal) is a right-angle rotation in the $\alpha\beta$-plane and vanishes on the complement, the $\pm1$-eigenspaces correspond to left/right isoclinic rotations, which in turn correspond to left- or right-multiplying $\mathbb{H}$ by unit quaternions (in $SO(4)$).
The aforementioned symmetric bilinear form restricts to a positive- or negative-definite form on $\Lambda^2_{\pm}$ respectively. Note $\Lambda_{\pm}^2$ are orthogonal with respect to the form (as well as the irrelevant Euclidean inner product mentioned earlier!), so the form has signature $(3,3)$. Now, $\mathrm{SL}_4\mathbb{R}$ respects this form, since
$$ (A\alpha\wedge B\beta,A\gamma\wedge A\delta)=A\alpha\wedge A\beta\wedge A\gamma\wedge A\delta=\det(A\alpha\,A\beta\,A\gamma\,A\delta)$$
$$ =(\det A)\det(\alpha\,\beta\,\gamma\,\delta)=(1)(\alpha\wedge\beta\wedge\gamma\wedge\delta)=(\alpha\wedge\beta,\gamma\wedge\delta). $$
Thus, we have a homomorphism $\mathrm{SL}_4\mathbb{R}\to\mathrm{SO}(3,3)$. We can check this is $2$-to-$1$ map with kernel $\{\pm I_4\}$, and the corresponding lie algebra homomorphism $\mathfrak{sl}_4\mathbb{R}\to\mathfrak{so}(3,3)$ is an isomorphism. Since
$$ \mathfrak{sl}_4\mathbb{C}=(\mathfrak{sl}_4\mathbb{R})\oplus(\mathfrak{sl}_4\mathbb{R})i, \qquad \mathfrak{so}_6\mathbb{C}=\mathfrak{so}(3,3)\oplus \mathfrak{so}(3,3)i, $$
we can extend the isomorphism to the complexifications, i.e. $\mathfrak{sl}_4\mathbb{C}\to\mathfrak{so}_6\mathbb{C}$.
Comment: A speedier way to see that the signature is $(3,3)$ is that there exist 3 dimensional null spaces e.g. $e_1 \wedge \mathbb{R}^4$ and the maximum dimension of a null space is the minimum $\min\{p,q\}$ of the signature $(p,q)$
Comment: One thing I would like to say as well is the Hodge star comes from a choice of inner product $\mathbb{R}^4$ but this is extra structure that you have imposed. $\mathfrak{sl}(4, \mathbb{R})$ has no such structure built in. Indeed this amounts to choosing maximal compact subalgebras and instead finding the exceptional isomorphism: $\mathfrak{so}(4)\cong \mathfrak{so}(3)\oplus\mathfrak{so}(3)$


How to organize data in a dataframe with pandas

Question: I have a Dataframe like
     used_at  common users                     pair of websites
0       2014          1364                   avito.ru and e1.ru
1       2014          1364                   e1.ru and avito.ru
2       2014          1716                 avito.ru and drom.ru
3       2014          1716                 drom.ru and avito.ru
4       2014          1602                 avito.ru and auto.ru
5       2014          1602                 auto.ru and avito.ru
6       2014           299           avito.ru and avtomarket.ru
7       2014           299           avtomarket.ru and avito.ru
8       2014           579                   avito.ru and am.ru
9       2014           579                   am.ru and avito.ru

for the years 2014 and 2015 where all strings are duplicated. I need to print my data like
         used_at  common users                     pair of websites
0       2014          1364                   avito.ru and e1.ru
1       2015          1208                   avito.ru and e1.ru
2       2014          1716                   avito.ru and drom.ru
3       2015          897                    avito.ru and drom.ru

I want to delete duplicate strings and instead print each pair to first output 2014 and then 2015.
I tried to do this:
all_common_users = df.groupby([df['used_at'].dt.year]).apply(f).reset_index(drop=True, level=1).reset_index()

common_users = all_common_users.groupby(all_common_users.index / 2).first().sort_values('pair of websites')

# keep only 'interesting' columns
common_users = common_users[['pair of websites','used_at','common users']]
common_users.columns = ['pair of websites','year','common users']

df = common_users[common_users.groupby('pair of websites')['common users']].sort_values('pair of websites', ascending=False).reset_index()

But it prints incorrectly. What did I do wrong?
Answer: You can use pandas.DataFrame.drop_duplicates:
df.drop_duplicates(subset=["used_at", "common users"])
   used_at  common users            pair of websites
0     2014          1364          avito.ru and e1.ru
2     2014          1716        avito.ru and drom.ru
4     2014          1602        avito.ru and auto.ru
6     2014           299  avito.ru and avtomarket.ru
8     2014           579          avito.ru and am.ru

with subset:

column label or sequence of labels, optional Only consider certain
  columns for identifying duplicates, by default use all of the columns
Comment: It delete duplicate strings. How can it sort by `pair of websites`. I try `sort_values('pair of websites', ascending=False).` but not all pairs were sorted. Some are out of order
Comment: It should work the way you did. Otherwise ask another question with what you have, you'd like and what you tried.
Comment: @user116873 if it answers the question, accept it, so others will know that it fixed your problem.


How can I calculate the probality of to get a number with 15 digits lenght repeat

Question: A number with nineteen digits is generated randomically by a particular system and it's guaranteed that every number generated is unique (by the system provider).
If I chunk this number and get the last fifteen digits, from right to the left, for example:
0123456789012345678 -> 456789012345678
Assuming that this procedure will be repeat for undefined and unlimited times it's possible to verify if will be possible to get repeated number?  How would be the chance or the probability to get a repeated number in this new sub selection or subset?
How can I calculate this or explain mathematically this probability?
Thanks
Answer: If it extends for an unlimited time you are guaranteed to get a repeat.  You are guaranteed a repeat after $10^{15}+1$ trials.  In fact, the system will fail its promise to never repeat the whole number after $10^{19}+1$ trials.
The probability of a collision is a generalization of the birthday problem.  The exact calculations get messy because you have a limited inventory of $15$ digit numbers- $10^4$ of each.  But if we ignore that, we are drawing from $d=10^{15}$ possibilities with replacement and asking for the number of draws to achieve a probability $p$ of a collision, we get $$n=\sqrt{2d \ln \left(\frac 1{1-p}\right)}$$  For $d=10^{15},p=\frac 12$ this gives about $3.73 \cdot 10^7$
The naive approach is that having drawn $n$ numbers you have $\frac {n(n-1)}2$ pairs, and each pair has $10^{-15}$ chance of matching.  This leads to $n \approx \sqrt {2d}$ which is quite close.
Comment: Thanks Very much Ross, you rocks! But are you considering a combination with repetition, I mean Crep(m,p) = C(m+p-1,p) because I can arrange C digitis {0,1,2,3,4,5,6,7,8,9} in p ways (15)  (012345678901234),(011111111111111) and so on?
Comment: I could consider the d value as binomial(24, 15) or the result of C(24,15) ?
Comment: @Bera:  yes, we are looking at with repetition because  there are $10$ choices at each location.  We are looking at $10{15}$ possibilities.  I presume $24$ comes from $15+10-1$ but that considers order unimportant.  We want to consider 000000999999999 different from 999999999000000


How to extract the residual and null deviances from a glmmTMB object (to calculate D2, the deviance explained)?

Question: The context is about the use of a given model deviance (often referred to as "Residual deviance" in R) and that of its "Null deviance" to calculate D2, the deviance explained for models with non-normal error.
This metric (D2) is somehow an approximation of what a coefficient of determination (R2) is for a linear model, providing an approximate idea of the variation explained by your model, although it is probably better described as a value reflecting how close its fit is from being perfect when compared to a saturated model, see:
https://bookdown.org/egarpor/PM-UC3M/glm-deviance.html
See also p. 166-167 of Guisan & Zimmermann (2000) for the calculation of D2 and its related adjusted version:
https://www.wsl.ch/staff/niklaus.zimmermann/papers/ecomod135_147.pdf
With most packages, such as "stats" and its glm() function, "MASS" and glm.nb(), and "mgcv" and gam(), those two deviance-related values can easily be obtained with "deviance" and "null.deviance" (preceeded by a dollar symbol - see further below). The gam() function actually also provides D2 directly in its output.
For the glmmTMB package and its glmmTMB() function, I haven't been able to extract the residual deviance nor the null deviance to calculate D2.
Does anyone has an idea of how to achieve this?
Here's an example of what I'm looking for, using real count data from a sample of walleyes through a monitoring program. The idea is to extract the deviance-related information for a same model using each function/package  when fitting these with the same error structure (NB2, link=log) and see how they compare, especially when contrasted to glmmTMB (not being able to extract such information from this package).
Walleye catch curve example:
From the "descending limb of a catch curve" in fisheries, one can model the rate at which counts decrease with age to estimate the instantaneous mortality (Z: the absolute value of the age coefficient) on the log scale from a sample of randomly-captured fish. The age-frequencies data are as follow:
age<-seq(1,15,by=1)
count<-c(151,56,117,10,12,21,8,2,2,1,2,0,1,1,2)
walleye<-data.frame(age,count)

These data are over-dispersed (variance > mean) and as such, the Poisson family distribution is inadequate. Using the glm.nb() function of the "MASS" package allows to model the variance in "extra" as follow:
summary(m.walleye.nb2<-glm.nb(count~age,data=walleye))

Call:
glm.nb(formula = count ~ age, data = walleye, init.theta = 3.114212171,
    link = log)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-1.6522  -0.8761  -0.2121   0.5188   1.8561

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  5.21804    0.36336  14.361  < 2e-16 ***
age         -0.42792    0.05395  -7.931 2.17e-15 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for Negative Binomial(3.1142) family taken to be 1)

    Null deviance: 109.841  on 14  degrees of freedom
Residual deviance:  15.903  on 13  degrees of freedom
AIC: 95.102

Number of Fisher Scoring iterations: 1

              Theta:  3.11
          Std. Err.:  1.58

 2 x log-likelihood:  -89.102

Note that both the Null deviance (109.841) and the Residual deviance (15.903) are provided directly in the output and already tell me that the predicted values of the considered model adjust pretty well to the observed (count) data given the large difference between the Null and Residual deviances.
These two deviance-related values can also be directly extracted as follow:
m.walleye.nb2$null.deviance
[1] 109.8413

m.walleye.nb2$deviance
[1] 15.90327

Using first the hnp() function of the hnp package (Moral et al. 2017) I can tell that the Pearson residuals of my model are behaving as expected given the distributional assumptions of this Poisson extension, i.e. the negative binomial type II (nb2). This model is thus adequate (i.e., goodness-of-fit), but I'm interested too in obtaining a "calibration" metric to get an idea of how well the predictions adjust to the observed data. That's where D2 is useful, and more so from an "explanatory power" than "adequacy" perspective:
D2<-100*(1-m.walleye.nb2$deviance/m.walleye.nb2$null.deviance)
D2
[1] 85.52159

This quite high value is not unexpected, as counts are deacreasing with age (walleyes die as they age) and despite the high variation observed in these age-frequencies data, the .nb2 model is capturing most of the signal for the central tendency and its associated variance.
If I use the gam() function of the mgcv package, I can run the exact same model when not recoursing to a smoothing function s() and specifying the argument method="ML" as REML would otherwise be used by default:
library(mgcv)
summary(m.walleye.nb2.GAM<-gam(
count~age,family=nb(theta=NULL),method="ML",data=walleye))

Family: Negative Binomial(3.114)
Link function: log

Formula:
count ~ age

Parametric coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  5.21804    0.36336  14.361  < 2e-16 ***
age         -0.42792    0.05395  -7.931 2.17e-15 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =  0.774   Deviance explained = 85.5%
-ML = 44.551  Scale est. = 1         n = 15

Note that the parameter estimates are identical to those obtained with
glm.nb() of the "MASS" package. The Deviance explained provided in the output corresponds to the one that was calculated for the previous .nb2 model. If we calculate D2 by hand for this nb2.GAM model:
D2<-100*(1-m.walleye.nb2.GAM$deviance/m.walleye.nb2.GAM$null.deviance)
D2
[1] 85.52159

Identical.
Now, fitting the same model in glmmTMB using .NB2 instead of .nb2 to differentiate it from the one obtained with glm.nb(), we get:
library(glmmTMB)
summary(m.walleye.NB2<-glmmTMB(count~age,family=nbinom2,data=walleye))
 Family: nbinom2  ( log )
Formula:          count ~ age
Data: walleye

     AIC      BIC   logLik deviance df.resid
    95.1     97.2    -44.6     89.1       12

Dispersion parameter for nbinom2 family (): 3.11

Conditional model:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  5.21804    0.34947  14.931  < 2e-16 ***
age         -0.42792    0.05485  -7.802 6.09e-15 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The parameter estimates are again identical to those of .nb2 and .nb2.GAM and the deviance provided is of 89.1.
All models have the same log-likelihood:
logLik(m.walleye.nb2)
'log Lik.' -44.55119 (df=3)

logLik(m.walleye.nb2.GAM)
'log Lik.' -44.55119 (df=3)

logLik(m.walleye.NB2)
'log Lik.' -44.55119 (df=3)

As indicated by @Ben Bolker, the deviance reported in glmmTMB is:
deviance<-(-2*-44.55119)
deviance
[1] 89.10238

The "deviance" here is obviously the same for all three models (89.10238) and is used for instance in the calculation of the AIC = -2(log-likelihood) + 2K where K is the number of parameters (df=3 from above):
89.10238+(2*3)
[1] 95.10238

In the end, the more specific question would be:
How can I extract the deviance-related information (null and residual deviances) from a glmmTMB object for the computation of D2 in such a simple model (one predictor, fixed effect)?
Although not of prime importance, this metric nonetheless helps to provide an idea of how "good" is your model at explaining the variation in your data, knowing that a top-ranking, adequate model may have a low explanatory power while still being useful. As such, knowing this information is desirable to sometimes tone down a statement related to the model predictions. I guess that Simon Wood has not provided D2 in the gam() function output of his "mgcv" package for no reason.
Answer: This would be a comment, but I need to provide some code, so I'll include it as an answer.
This may be in essence a programming question, but there might be a statistics question as well.

With glmmTMB objects, you may need to fit a null model to determine the null deviance. What constitutes a null model may depend on your purposes.

You should be able to derive the deviance from the log likelihood.  The latter can be extracted with logLik(model).

The anova() function displays the deviance.

library(glmmTMB)

model = glmmTMB(count ~ mined + (1|site), family=poisson, data=Salamanders)

model.null = glmmTMB(count ~ 1+ (1|site), family=poisson, data=Salamanders)

logLik(model)

   ### 'log Lik.' -1104.849 (df=3)

logLik(model.null)

   ### 'log Lik.' -1120.773 (df=2)

anova(model, model.null)

   ###            Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)
   ### model.null  2 2245.6 2254.5 -1120.8   2241.6
   ### model       3 2215.7 2229.1 -1104.8   2209.7 31.847      1  1.668e-08 ***

Comment: Thanks for the feedback @Sal Mangiafico
Comment: The deviances obtained with the anova() function in the example you have provided are the ones reported too in a glmmTMB output (see Ben Bolker's answer) and can be used for instance to calculate the AIC of the related model. Here, the AIC of "model.null" would be -2logLik + 2 K = -2*(-1120.8)+2*2 = 2245.6. But is there a way to calculate the Residual deviance from the deviance of "model" and the Null deviance from the deviance of "model.null"? Otherwise, if one could extract the deviance residuals of each models, that could resolve the issue. See my comment to Ben Bolker.
Answer: Another general comment, from the details section of ?lme4::deviance.merMod:
Deviance and log-likelihood of GLMMs:

One must be careful when defining the deviance of a GLM. For
example, should the deviance be defined as minus twice the
log-likelihood or does it involve subtracting the deviance for a
saturated model? To distinguish these two possibilities we refer
to absolute deviance (minus twice the log-likelihood) and relative
deviance (relative to a saturated model, e.g. Section 2.3.1 in
McCullagh and Nelder 1989). With GLMMs however, there is an
additional complication involving the distinction between the
likelihood and the conditional likelihood. The latter is the
likelihood obtained by conditioning on the estimates of the
conditional modes of the spherical random effects coefficients,
whereas the likelihood itself (i.e. the unconditional likelihood)
involves integrating out these coefficients. The following table
summarizes how to extract the various types of deviance for a
'glmerMod' object:

                     conditional        unconditional
   relative   'deviance(object)'         NA in 'lme4'
   absolute  'object@resp$aic()'  '-2*logLik(object)'

library(lme4)
library(glmmTMB)
m1 <- glmmTMB(count~spp * mined + (1|site), data = Salamanders, family="nbinom2")
m2 <- glmer.nb(count~spp * mined + (1|site), data = Salamanders)

-2*logLik() is the same (up to a small numerical difference) for glmmTMB and lme4 (approx 1631.3)
deviance(m2) is 501.8 (NULL for m1)
m2@resp$aic() is 1584.3 (undefined for m1)

For what it's worth, the canonical definition of deviance is what's called "relative" deviance above: people (including me) are sometimes sloppy and call $-2 \log {\cal L}$ (i.e. the "absolute deviance" from above) a deviance, but I believe that's technically incorrect. If we were only interested in differences between deviances rather than ratios this distinction wouldn't matter ...
Since you're not dealing with GLMMs (no random effect in your example) this gets a little bit less messy, but you still have a problem. We can fit the null model via update(my_model, . ~ 1), but that only gets us the values of $-2 \log{\cal L}$ ("absolute") for the full and null models, not the deviances.
Comment: Tangentially, I am currently having a brain fart: I can't see why the sum of the squared deviance residuals [which is how the (relative) deviance is usually computed] is actually different from $-2 \log{\cal L}$ ...?
Comment: Thanks for the feedback @Ben Bolker. I've read that we can indeed use the sum of the squared deviance residuals to estimate the residual deviance, but unless I'm wrong, glmmTMB only allows to extract either the "response", "pearson", or "working" residuals. If I use: sum(residuals(m.walleye.nb2,type="deviance")^2) for the model obtained with glm.nb() of the MASS package, I get the exact same value regarding the residual deviance: [1] 15.90327. The same hold true for the .nb2.GAM from gam() of the mgcv package. Identical (correct) value. Being able to do so with glmmTMB would offer a solution.
Answer: Until a more optimal answer is provided, here's a solution based on the information that I've found in a "GLM blog" by Matthias Dörig:
The glmmTMB() function allows to extract the "response", "working" and "pearson" residuals of a considered model, but (to my knowledge) not the "deviance" residuals. This is unfortunate, as the "Residual deviance" of a GLM is simply the sum of the squared "deviance" residuals.
Using the same walleye data, I'll start by using the glm() function with family=poisson as the solution relies on transforming the "response" residuals into "deviance" residuals based on an error-specific function that was provided by Dr. Dörig for the Poisson case.
summary(m.walleye.p<-glm(count~age,family=poisson,data=walleye))

Call:
glm(formula = count ~ age, family = poisson, data = walleye)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-5.0251  -1.4546  -0.1779   0.8031   7.0199

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  5.45450    0.08303   65.70   <2e-16 ***
age         -0.47326    0.02485  -19.05   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 851.04  on 14  degrees of freedom
Residual deviance: 111.96  on 13  degrees of freedom
AIC: 169.06

Number of Fisher Scoring iterations: 5

When using glmmTMB() to model the same data in an identical manner, we aim to obtain a Residual deviance of 111.96. Not displaying the output, this is coded as follow, using .P instead of .p to differentiate it from the first GLM:
summary(m.walleye.P<-glmmTMB(count~age,family=poisson,data=walleye))

The "response" residuals correspond to the simplest form, i.e. the difference between the observed and predicted values:
observed<-walleye$count
predicted<-fitted(m.walleye.P)

resid<-observed-predicted

The "response" residuals obained (i.e., resid object) would be identical as those:
residuals_response<-residuals(m.walleye.P,type="response")
resid == residuals_response
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15
TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

The function to transform the "response" to "deviance" residuals for family=poisson is based on the "observed" and "predicted" objects. Note that the function needs to be modified according to the considered error structure, the Poisson family representing (I believe) the simplest case when analyzing count data:
Poisson.dev<-function(y, mu)
2*(y*log(ifelse(y == 0, 1, y/mu))-(y-mu))

residuals.deviance<- sqrt(Poisson.dev(observed,predicted))*ifelse(observed>predicted,1,-1)

The Residual deviance is finally obtained as:
sum(residuals.deviance^2)
[1] 111.9586

It's the correct value.
Here's a last check to make sure that we would get the same result for m.walleye.p from the "deviance" residuals (see this link) that can be directly extracted from a glm() object:
sum(residuals(m.walleye.p,type="deviance")^2)
[1] 111.9586

It's the same value as:
m.walleye.p$deviance
[1] 111.9586

And the Null deviance would be:
m.walleye.p$null.deviance
[1] 851.0357

So the deviance explained (D2) would be:
100*(1-m.walleye.p$deviance/m.walleye.p$null.deviance)
[1] 86.84443

Although this value is pretty high, this model is completely inadequate. The count data are over-dispersed and thus, the required equi-dispersion (variance = mean) for a Poisson model is not respected. To be adequate, about 95% of the residuals should be found within the hnp simulated envelope below if the distributional assumptions of the Poisson family were to be respected, which is clearly not the case here, as 80% are found outside:
library(hnp)
hnp(m.walleye.p,resid.type="pearson",how.many.out=TRUE,paint=TRUE)
Poisson model
Total points: 15
Points out of envelope: 12 ( 80 %)

So, in the end, contrasting model adequacy (hnp) and model approximated explanatory power (D2) is valuable for more reliable statistical inferences.
For glmmTMB, one need to also run the null counterpart of the model (i.e., ~ 1), as indicated by @Sal Mangiafico and @Ben Bolker, to obtain the Null deviance too and then, calculate D2 from these values.
Maybe at some point the glmmTMB package will allow to obtain these deviance-related metrics, but the solution here is a bit too long to apply and I'm afraid that the required functions for either the Generalized Poisson (family=genpois), mean-parameterized Conway-Maxwell-Poisson (family=compois) or esle such as family=nbinom1 may be too difficult to code under this proposed approach.


IOS Box2D - body follows a specific path based on an array of points with fluctuating speed

Question: i have a question about a body, which follows a specific path. First of all here the method to move the body to a target point:
const float destinationControl = 0.3f;
b2Vec2 targetPosition = path[counter];
b2Vec2 missilePosition = _physicalBody->GetPosition();
b2Vec2 diff = targetPosition - missilePosition;
float dist = diff.Length();

if (dist > 0)
{

    if(dist < destinationControl){
        ++counter;
        return;
    }

    // compute the aiming direction
    b2Vec2 direction = b2Vec2(diff.x / dist, diff.y / dist);

    // get the current missile velocity because we will apply a force to compensate this.
    b2Vec2 currentVelocity = _physicalBody->GetLinearVelocity();

    // the missile ideal velocity is the direction to the target multiplied by the max speed
    b2Vec2 desireVelocity = b2Vec2(direction.x * maxSpeed, direction.y * maxSpeed);

    // compensate the current missile velocity by the desired velocity, based on the control factor

    b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);

    // transform our velocity into an impulse (get rid of the time and mass factor)
    float temp = (_physicalBody->GetMass() / normalDelta);

    b2Vec2 finalForce = b2Vec2(finalVelocity.x * temp, finalVelocity.y * temp);

    _physicalBody->ApplyForce(finalForce, _physicalBody->GetWorldCenter());

}

The array path represent as the name implies a specific path which the body follow when the user trigger a touch event.
My problem is that the variable maxSpeed is not constant. So when the path is very short and the maxSpeed is very high the body moves over the current target point. With my method above the body return to the current target point and does not focus the next point.
At the moment i have a constant destinationControl which controls if the distance between the body and the current target point is short enough to focus the next target point.
The focus is to increase a counter which represent the index of the target points.
So my final question is: Can you imagine another possible solution, because mine is not very safe. Is there a way to figure out if the body moves over the current target point?
thank you in advance and sorry for my bad english.
Comment: Hey Alex, what happened to the other question you posted related to this, that I answered last week? I want to use the code I posted in that answer, but the question seems to be deleted or something?
Answer: I had a similar problem in a path following demonstration I was putting together(here is the project and here is the just the video).
I found the most workable solution was to use a combination of 2 functions.  The first detects if the entity is "close" to the next point they want to get to.
bool MovingEntity::IsNearTarget()
{
   Vec2 toTarget = GetTargetPos() - GetBody()->GetPosition();

   if(toTarget.LengthSquared() < GetMinSeekDistanceSquared())
   {
      return true;
   }
   return false;
}

This entity is using a "seek" operation to move from point to point.  If it is close to the point, the function returns true.  It uses the square of the distance to avoid square roots for length.
The second function is the path following function executed every update:
void MovingEntity::ExecuteNavigateToPoint()
{
   vector<Vec2>& path = GetPath();
   bool isNearTarget = IsNearTarget();
   bool isNearNavTarget = IsNearTarget(_navigatePos,5.0);

   /* If the state tick timer expires, it means we
    * have spent too long trying to reach the next waypoint
    * and we need to replan on how to get to it.
    */
   if(IsStateTickTimerExpired())
   {
      CCLOG("Tick Timer Expired!!!");
      ChangeState(ST_NAVIGATE_TO_POINT);
   }
   /* If we are close to the navigation target point,
    * just seek to it.
    */
   if(isNearNavTarget || path.size() == 0)
   {  // Must be really close...just seek to it.
      CommandSeek(_navigatePos);
   }
   else if(!IsNodePassable(GetTargetPos(),false))
   {
      ChangeState(ST_NAVIGATE_TO_POINT);
   }
   else
   {  // If we are near the target and there are more points
      // on the list, pop the next point and navigate to it.
      if(isNearTarget && path.size() > 0)
      {  // Still more points on the list.
         GetTargetPos() = path.back();
         path.pop_back();
         ResetStateTickTimer(2*TICKS_PER_SECOND);
         /* If we can't get past the current nodes, replan.
          */
         if(path.size() > 0 && !IsNodePassable(path.back(),false))
         {
            ChangeState(ST_NAVIGATE_TO_POINT);
         }
      }
      ApplyThrust();
      ApplyTurnTorque();
   }
}

This one is a bit harrier, but what it comes down to is checking for whether the entity has reached the final target (_navigatePos), reached the next path point, or unable to approach the target point for some reason, so it times out and replans the path to it.
This is part of a simulation of a ship flying through a moving asteroid field, so the scenery was changing and the ship could get "stuck" in certain situations, so it had to replan every now and then.
The code base for this is located on github.
Was this helpful?
Comment: Wow the project is amazing. How long does it take to put all these stuff together?
Comment: Thanks you.  I hope you find it useful.  There is a lot to explore and a lot I need to improve in future projects.  Feel free to share as needed.  Short answer:  About 3 months of 5:30am-6:30am (the slippers are for a cold floor) work and lots of coffee.  Long answer:  See previous posts on site (reused), plus 3 decades of learning to write and architect code.
Answer: What I think you might want to do is to compare your maxSpeed to the distance possible to move.  When moving farther than the distance available, set the misslePosition to the targetPosition and retrieve the next target position in the path, and subtract the distance already moved from maxSpeed.
if (maxSpeed > dist)
{
    maxSpeed -= dist;
    misslePosition = targetPosition;
    targetPosition = path[counter + 1];
}

Another possible solution would be to make smaller steps and perform a loop while totalDistanceCovered < maxSpeed, this might be a little easier to capture more edge cases, although more CPU time is used.
const float stepDistance(0.1f); //Some relatively small value depending on your units.
float totalDistance(0.0f);
while (totalDistance < maxSpeed)
{
    totalDistance += stepDistance;

    //Perform the same movement logic you already have.
}

--- My original answer below ---
You may need to set the definition of the _physicalBody to be a bullet if you are attempting to collide with a dynamic target.

By default, Box2D uses continuous collision detection (CCD) to prevent dynamic bodies from tunneling through static bodies. This is done by sweeping shapes from their old position to their new positions. The engine looks for new collisions during the sweep and computes the time of impact (TOI) for these collisions. Bodies are moved to their first TOI and then halted for the remainder of the time step.
Normally CCD is not used between dynamic bodies. This is done to keep performance reasonable. In some game scenarios you need dynamic bodies to use CCD. For example, you may want to shoot a high speed bullet at a stack of dynamic bricks. Without CCD, the bullet might tunnel through the bricks.

From box2d.org/manual
Comment: The _physicalBody does not collide with a another body when it follows the path. My problem is that when the body moves to a target point and the maxSpeed is to fast the body moves over the target point and return to the same target without focus the next (which is my wish)
Comment: Oh, I am sorry to have misunderstood the question.  I'll try to update my answer in a bit with something hopefully more helpful.
Comment: No problem my english is not very good, so it's understandable (:
Comment: Hey @BlackBird thanks for your update. I will check your suggestion and give you feedback (:


Showing a Group Can Be Totally Ordered

Question: If every finitely generated subgroup of a group $G$ is totally ordered, what approach should I take to prove that $G$ is also totally ordered?
Should I define a map that takes elements from the subgroups and make an ordering (I have no idea if this can even work, it was just an idea) or should I build a set that satisfies the four properties from this page in its second paragraph?
Additionally, for such properties, what does it mean when it says $PP\subset P$?
Comment: $PP\subset P$ means that the product of two positive elements is positive. In general, $AB=\{ab:a\in A,\ b\in B\}$.
Comment: Do you know about ultrafilters?
Comment: @bof I read the Wikipedia page of it just now. Is this an easy way to prove the theorem? Do you have any ideas where to begin?
Answer: We need to prove that for any two elements $a, b$, we have either $a\le b$ or $b\le a$.
But this already holds in the subgroup generated by $a, b$ by hypothesis.
Having $P=\{x:x\ge e\}$, with $x, y\in P$ we get
$$xy\ \ge\ xe=x\ \ge\ e$$
so $xy\in P$, proving $PP\subseteq P$.
Similarly, if $x\in P\cap P^{-1}$, then both $x$ and $x^{-1}$ are $\ge e$, thus $e=xx^{-1}\ge xe=x\ge e$ yielding $e=x$.
If $x\ge e$, then $g^{-1}xg\ge g^{-1}eg=e$.
Finally, $P\cup P^{-1}=G$ expresses that the ordering is total.
Comment: I suspect that the OP actually wants to prove that, if every finitely generated subgroup of $G$ admits an ordering compatible with the group structure, then so does $G$. In other words, no ordering relation is given, and the orderings of various subgroups can disagree with each other on the intersection of the subgroups. (That's a fairly standard application of the compactness theorem for propositional logic; hence bof's comment about ultrafilters.) In other words, I suspect the OP misstated the question so that a single relation on $G$ is already given.
Comment: If you know the compactness theorem, then you don't have to use ultrafilters. Ultrafilters could be used here instead of compactness, and they can also be used to prove the compactness theorem, but if you already have the compactness theorem then you might as well just use that.
Comment: You seem to be on the right track. I'd use a propositional variable $p_{x,y}$ for each pair of elements of the given group $G$, with the intended "meaning" that $x\leq y$ in the ordering that I'm trying to construct. Any valuation (also called truth assignment) to these variables determines a binary relation $\leq$ on $G$: $x\leq y$ iff the valuation makes $p_{x,y}$ true. You need to produce a set $S$ of sentences such that whenever a valuation satisfies $S$, the corresponding relation $\leq$ makes $G$ a totally ordered group. (continued in next comment)
Comment: So if $S$ is satisfiable then $G$ is totally orderable. By compactness, it suffices to show that each finite subset of $S$ is satisfiable, and that's where you use the assumption that each finitely generated subgroup of $G$ is totally orderable.
Comment: Wow! Such an expert answer and very clear understanding! Thanks so much for the guidance.
Comment: Moreover, it's enough to assume that every *cyclic* subgroup is totally ordered. Can you prove that?
Comment: Hmm.. That part was not clearly indicated in the question, but it indeed makes sense.
Comment: @AndreasBlass I do have to use the compactness theorem in my proof. Does that mean I have to use ultrafilters instead? I was just given only the definition of a totally ordered group and a total linear order on $G$.
Comment: @AndreasBlass I see, I was just looking at your slides you made for Set Theory and Ultrafilters! Okay, I will use the compactness theorem, but I am stuck on what I should use the propositional variables for. That is why I asked this question, because I need to introduce variables and I am not sure what they should represent...That is why I thought using those properties would be okay, and I can set $p_{x,y}$ to represent an inequality possibly...
Comment: @AndreasBlass Okay, I am very excited to take this approach! Thanks so much for taking the time and helping me, you made it very clear and understandable for me!

Showing a Group Can Be Totally Ordered


How can I change google_ads schema in dbt project?

Question: I have a dbt project and I'm using ad_reporting model.
all the sources are working good, except than google_ads.
and that's because I don't have google_ads schema, instead I have google_ads_us schema.
I tried to change it in main dbt_project.yml file:
vars:
    google_ads_schema: google_ads_us

but still when I'm running this command:
dbt run --select ad_reporting

I keep get this error:
Database Error in model stg_google_ads (models\stg_google_ads.sql)
SQL compilation error:
Object 'DATABASE.HISTORY_GOOGLE_ADS.GOOGLE_ADS__URL_AD_ADAPTER' does not exist or not
authorized.
compiled SQL at target\run\ad_reporting\models\stg_google_ads.sql

the HISTORY_ prefix is from profiles.yml file (SCHEMA).
Anyone know where else I need to change the schema?
Answer: For the package to find the specified vars it has defined, you will need to do four things, all specified in the docs:
1. Make sure that ad_reporting for google_ads is enabled -->
# inside your dbt_project.yml

vars:
  ad_reporting__google_ads_enabled: true

2. Also, make sure that you let dbt know where to look for the google_ads raw data:
# inside your dbt_project.yml

vars:
  google_ads_database: your_database_name  # in our case, `raw`
  google_ads_schema: your_schema_name      # in our case, `google_ads`

3. Define the schemas where dbt will build the models for both google_ads and google_ads_source -->
# inside your dbt_project.yml

models:
  google_ads:
    +schema: my_new_schema_name # leave blank for just the target_schema
  google_ads_source:
    +schema: my_new_schema_name # leave blank for just the target_schema

Since, as according to the docs:
"By default this package will build the Google Ads staging models within a schema titled (<target_schema> + _stg_google_ads) and the Google Ads final models with a schema titled (<target_schema> + _google_ads) in your target database."
4. Specify which API you're going to use (Google Adwords or Google Ads):
# inside your dbt_project.yml

vars:
    api_source: google_ads  # adwords by default and is case sensitive!

Comment: thank you for your answer. I did that, and now I get another error: dbt found two resources with the name "google_ads__url_ad_adapter". Since these resources have the same name,
  dbt will be unable to find the correct resource when ref("google_ads__url_ad_adapter") is used. To fix this,
  change the name of one of these resources:
  - model.google_ads.google_ads__url_ad_adapter (models\url_google_ads\google_ads__url_ad_adapter.sql)
  - model.google_ads.google_ads__url_ad_adapter (models\url_adwords\google_ads__url_ad_adapter.sql)
but adwords is the default choice. how can I change it?
Comment: Forgot to add that you also need to specify the API you're going to use (Google Adwords or Google Ads). Updated the answer accordingly :)


Upper/lower standard error makes sense?

Question: In the given Image you can see a plot with error bars of different length on each upper and lower side. The bars were generated by calculating the standard error once for all points above the mean value and once for those below.

As information on this "method" is very sparse I ask myself wether it is correct to use them. Or is it just the wrong term (lower/upper standard error) I used?
And finally what is the meaning of them?
Comment: I think this needs some more context, what is "this method"? Are errors assumed to be Normal? Symmetric? If not, the confidence interval doesn't need to be symmetric. As far as the meaning of them, do you mean standard error in general, or in this context?
Comment: "Standard errors" denote the (estimated) sampling variability of parameter estimates. What you have here are "prediction errors".
Comment: Also are these pointwise bands or simultaneous?
Answer: It's OK to call them error bars, but as they're asymmetric they are not representing the standard error so it's not correct to talk about 'lower/upper standard error'.
I assume the error bars here represent confidence intervals, though they might also be credible intervals if they were constructed using Bayesian methods.
Hard to be sure if you're not going to tell us where you got this graph from.
Comment: @severin, let me see if I understood correctly: you have, say, $n_{U_{\infty}}$ points for each value of $U_{\infty}$. For the sake of notation, let's fix $U_{\infty}=i$ (e.g., $U_{\infty}=i=2\ m/s$). From there, you calculated the mean value of the $n_i$ points, say, $\hat{\Omega}_{R}(i)$, and, in order to calculate the "upper standard error", from the total $n_i$ points you used only the points that where above $\hat{\Omega}_{R}(i)$, and repeated the same procedure for the "lower standard error" but taking only the points that where under $\hat{\Omega}_{R}(i)$?
Comment: @Nesp, correct. Someone else calculated the [arithmetic mean](https://en.wikipedia.org/wiki/Mean_value) $\bar{\Omega}(i)$ for each $i = U_\infty$, then took all $\Omega(i) > \bar{\Omega}(i)$ to calculate the upper error, using the formula for the [standard error](https://en.wikipedia.org/wiki/Standard_error). The same for the points below.
Answer: According to your updated question, the claim of @onestop is still valid: it's not ok to call them standard errors. Furthermore, the method seems strange and non-standard at all. What really was done in your case is to divide the population in two (values upper and lower than the mean) and calculate the standard error of THAT population, not of your real population and therefore, I find it personally strange to assign the length of the error bars in that way. Apparently the idea that was done here was taken from here. However, IMHO, the idea of dividing the sample and calculating an "upper and lower" standard deviation doesn't make much sense (or at least it botters me).
In physics (my area and apparently yours), however, it has been somewhat standard to show 68% confidence intervals for the sample median or the mean (depending on your choice of a location statistic; let's call this statistic $\bar{X}$ for the moment) in the following way for non-symmetric distributions (apparently emulating what would be a central credible interval): with your data points, you calculate $\bar{X}$ and then report an upper error bar of length $L_u$, where $L_u$ is calculated in order to satisfy $P(\bar{X}<\mu<\bar{X}+L_u)= 0.34$, where $\mu$ is the real (unknown) parameter. Then, for your lower error bar of length $L_l$, you repeat the same procedure but now downwards of the location statistic $\bar{X}$, i.e., $P(\bar{X}-L_l<\mu<\bar{X})= 0.34$. Of course, because the distribution of $\bar{X}$ is usually not known this is usually done with non-parametric methods (such as the Bootstrap or some variant of it).
As was also pointed out by @onestop, you can also obtain bayesian credible intervals, where you actually calculate the probability (density, in the continuous case) of your parameter given your data. Let's call this probability $p(x|D)$. The length of the lower error bar is now calculated in a more "natural way" (at least for me), in order to satisfy $P(\hat{x}-L_l<x<\hat{x}|D)=0.34$, and the length of the upper error bar is now calculated in order to satisfy $P(\hat{x}<x<\hat{x}+L_u|D)=0.34$, where $\hat{x}$ is your point estimate of the parameter (usually the median or even the mode).
All of the above, of course, makes sense only if your parameter is unimodal.
Comment: Thank you Néstor, I think your 34% method is what I was looking for. Is it correct to calculate e.g. $L_u$ by ordering all $N$ points by value and take the one $n = 0.34 \cdot N$?
Comment: Unless your sample size is high enough as to be representative of your population I wouldn't. I would always go with a bootstrap to be sure :-).
Comment: sample sizes vary from 30 to 250. So I use bootstrap to create e.g. 100 samples out of the original sample. Then I sort each sample by size and pick the one with the `index=round(0.341 * sampleSize)`. Unfortunately the result needs a very large number of bootstrappedsamples to be stable due to the rounding. Is it therefore valid to sum all samples and then pick the 34.1% ?
Comment: I may be wrong, but you can't just use percentiles in order to get confidence intervals. I think that what you are trying to do is to create the so-called distribution-free confidence intervals (although somewhat modified this time). I think you'll like this blogpost: http://probabilityandstats.wordpress.com/2010/02/22/confidence-intervals-for-percentiles as it has references and a very nice explaination of what you are trying to do ;-).
Comment: Thanks a lot for this answer. What happens with values equal to the mean?

Upper/lower standard error makes sense?


SQL Server stored procedure not working through codesmith

Question: I've got this script that is working properly and the output is correct when I execute the stored procedure. But its not working when I use it through codesmith, when I perform generate output the stored procedure will return an integer not a result.
Here is the part that is causing the problem:
SELECT *
INTO #UsersConversations
FROM Conversations c
WHERE c.pid IN
    (SELECT DISTINCT ConvID FROM ConversationUsers WHERE UserID = @UserId)

I'm using SQL Server 2008, CodeSmith v4
Comment: Side note: you don't need to use `DISTINCT` on a query being used inside an `IN()` expression. At best, it's redundant. At worst, the server may actually do the work to make the result set distinct before evaluating the `IN()` expression. (I'm fairly sure the optimizer is smart enough to remove it, but why take the chance?)
Comment: The distinct in my case has no effect what so sever. i added it in a desperate try to solve the issue. anyhow with or without the distinct the behaviour is just the same. i moved it anyhow.
Answer: I work for CodeSmith Tools. The following detailed thread will describe the issue you are running into and how to resolve it.
Comment: That was really helpful, great post , many thanks :)
It definitely helped me solve the issue..
Comment: link is no longer valid. I have an account and am logged in. Possibly the forums have been turned off ?
Comment: The community site has been removed as it was super out dated. Generator looks at the returned metadata without executing the sproc. You'll need to set FMT off and exec the sprocs with null parameter as shown here: http://stackoverflow.com/questions/21434827/no-fields-for-dynamic-sql-stored-procedure-in-ssrs-with-set-fmtonly Do you get a result?


LDA topic models for various and unknown domains

Question: We have a large collection (1-2M) of documents of various domains (politics, design, programming, etc.). And lets assume that we don't know the exact number of domains. And our goal is to build relevant topic models for these domains. For example I want to find N topics in each domain. For politics it could be: foreign policy, government, economic policy, etc. For design domain: web design, architecture, etc. And so on.
If I understand correctly, when using a LDA it is necessary to form a training set for specified domain (e.g. politics). But problem arises if there is no information about domain. How can I form a training set in that case? I was thinking about building index, extracting documents, that contain significant words (defined by TF-IDF). But it seems that it is not the best solution.
If instead of LDA we can use HDP and handle training set compiled with random documents. But there arises another problem - a huge vocabulary, which may not fit in the memory (we need much more words to describe several domains, than one).
So the problem is: what is the best way to build topic models (or one topic model?) of various (and unknown) domains? And them apply this models to new (unseen) documents.
Comment: Please unfold abbreviations "LDA", "TF-IDF", "HDP" in yout question. Not everybody knows these acronyms.
Comment: @ttnphns, I've added links to wikipedia.
Comment: @peppered what did you end up doing for this?
Answer: Hierarchical topic models are designed precisely for this task. These models infer topic hierarchies where high level topics (poltics, sports) have subtopics beneath them (foreign policy, baseball).
The original formulation is from this paper

Hierarchical Topic Models and
the Nested Chinese Restaurant Process

And this very recent formulation, which is able to infer more accurate hierarchical structures

Nested Hierarchical Dirichlet Processes
Comment: @Pepper: Yes, you could also fit topic models recursively as you say. That may or may not be more computationally tractable, I'm not sure, but I suspect the resulting topic hierarchies would be considerably inferior in quality. Inferring the hierarchy all at once, as with hLDA (or nHDPs), will provide the optimal global structure, whereas recursively fitting LDA models to construct a tree would be locally optimal, a sort of [greedy algorithm](http://en.wikipedia.org/wiki/Greedy_algorithm). I'm not expert however, so if this point is important you might make it a new question.
Comment: Thanks for your answer! But how can I form a training set (from a huge collection), which would cover all domains? Or it supposed to use all (about 2 millions) documents in original collection?
Comment: Yes, the idea is that it will discover the tree structure on it's own from the entire document collection.
Comment: If I understand correctly for big collections it should be used an online version of hLDA (or nHDP)?
Comment: @pepper, I don't think that's the case. What makes you think that? On-line inference is generally more difficult than batch processing because of the constraints on computing time.
Comment: I'm new in this area, so it was my supposition. If to be honest, I did not think that there is a big difference between online and batch processing.
Comment: Is there any way to apply hierarchical models to unseen documents?
Comment: @pepper, sure. You first perform inference on the data you have then if you get new documents you can use the parameter values you inferred on the training set to make an inference about new items.
Comment: for better understanding I have another question. What is the difference between hLDA and using LDA "recursively". Under the latest I understand, that we can use LDA to discover basic topics from whole corpus, and then use LDA on already discovered topics and open new topics within "parent" topic. And repeat this process recursively into some levels deep (like in hLDA). Or it is a bad idea?


Measures that coincide on every $(a,b)$ are equal

Question: From Bass, Real Analysis for Graduate Students:

Suppose $X$ is the set of real numbers, $\mathcal B$ the Borel $\sigma$-algebra and $m$ and $n$ are two measures on $(X,\mathcal B)$ such that $m((a,b))=n((a,b))<\infty$ whenever $-\infty<a<b<\infty$. Prove that $m(A)=n(A)$ whenever $A\in \mathcal B$.

I'm thinking the $\pi-\lambda$ theorem comes in handy in this situation. I'm therefore inclined to let $\mathcal C = \{(a,b), a<b\}$ and $\mathcal D = \{A\in \mathcal B, m(A)=n(A)\}$.
$C$ is obviously a $\pi$-system, and I expect $\mathcal D$ to be a Dynkin system (also referred to as $\lambda$-system).
It's easy to prove that $\mathbb R\in \mathcal D$ and $\mathcal D$ is closed under increasing union. Nonetheless, I'm running into some trouble when attempting to prove that $\mathcal D$ is stable under complement...
Indeed, given $A,B$ in $\mathcal D$ such that $A\subset B$ , the following holds:
$m(B) = m(B\setminus A) + m(A)$, hence $n(B) = m(B\setminus A)+n(A)$.
But similarly, $n(B) = n(B\setminus A) + n(A)$.
Hence $m(B\setminus A)+n(A) = n(B\setminus A) + n(A)$
The problem is, $1+\infty = 2+\infty $, yet $1\neq 2$. I cannot derive $m(B\setminus A) = n(B\setminus A)$ unless $n(A)<\infty$, which may not be true...
Note that I haven't used finitess of $n$ and $m$ on $(a,b)$ yet ...
Any help is much appreciated.
Comment: Since $\Bbb R$ is $\sigma$-finite, you can consider open intervals of the form $E_n=(-n,n)$ where $n\in\Bbb N$. Argue locally on each $E_n$ then use the fact that $\Bbb R=\bigcup E_n$.
Comment: @BigbearZzz thanks for your valuable comment. I've added an answer based on your hint
Answer: Following BigbearZzz's suggestion, let $\mathcal D_k = \{A\in \mathcal B, m(A\cap E_k) =  n(A\cap E_k)\}$.
I'm going to prove that $\mathcal D_k$ is a Dynkin system such that $\mathcal C\subset \mathcal D_k$.

$m(\mathbb R\cap E_k) = m(E_k)=m((-k,k))=n((-k,k))=n(\mathbb R\cap E_k)$ hence $\mathbb R\in \mathcal D_k$
Let $A,B\in \mathcal D_k$ such that $A\subset B$. Note that  $E_k\cap (B\setminus A) = (E_k\cap B)\setminus (E_k\cap A)$ and both $(E_k\cap B)$ and $(E_k\cap A)$ have finite measure(for both $m$ and $n$). Therefore, $$\begin{align}m(E_k\cap (B\setminus A)) &= m(E_k\cap B)-m(E_k\cap A)\\&=n(E_k\cap B)-n(E_k\cap A)\\&=n(E_k\cap (B\setminus A))\end{align}$$

Hence $B\setminus A\in \mathcal D_k$

If $(A_i)$ is an increasing sequence of elements of $\mathcal D_k$, $$\begin{align}m(E_k\cap (\cup_i A_i)) &= m(\cup_i(E_k\cap A_i))\\&= \lim_im(E_k\cap A_i)\\ &=\lim_i n(E_k\cap A_i)\\ &=n(E_k\cap (\cup_i A_i))  \end{align}$$

Hence $\cup_i A_i \in \mathcal D_k$

$\mathcal C \subset \mathcal D_k$ because $\mathcal C$ is closed under finite intersection.

$\pi-\lambda$ theorem yields $\sigma(\mathcal C)\subset \mathcal D_k$ that is to say $\mathcal B\subset \mathcal D_k$. This means that for any $A\in \mathcal B$, $m(A\cap E_k) = n(A\cap E_k)$.
To finish it off, given $A\in \mathcal B$, $$\begin{align} m(A)&=m(\cup_i (A\cap E_i))\\ &= \lim_i m(A\cap E_i) \\ &= \lim_i n(A\cap E_i) \\&= n(A) \end{align}$$


Non-awaited async methods run on the UI thread?

Question: I want to have a method (Let's call it M1) execute some async code in a loop (Let's call that second method M2). On every iteration - the UI should be updated with the result of M2.
In order to await M2, M1 needs to be async. But M1 should run on the UI thread (to avoid race conditions) and therefore it will be called without an await.
Am I correct in thinking that in this way, M1's updating of the UI will be on the UI thread?

(Extra: It seems OK to have an async void in this case. Is this correct?)
Comment: Yes, seems you are correct. Just try to do it.
Comment: Trying would be tricky because race conditions are usually unlikely in this case. I'm just being careful because someone _sometime_ will likely run into them.
Comment: @ispiro btw, if you want to try `Task.Delay(10)` is likely the easiest way to create truly async method.
Comment: @AlexeiLevenkov Thanks for your time. I'm now looking into this whole thing. I really don't understand what's the point in async if it doesn't run on another thread. Anyway, I'm reading up on that now. Thanks again.
Answer: Yes. (assuming you use Synchronization Context that returns to UI thread - i.e. one from WinForm/WPF).
Note that it also means you can't schedule CPU-intensive operations that way as it will run on UI thread.
Using void async is quite standard method of handling events in WinForms:
void async click_RunManyAsync(...)
{
   await M1();
}

void async M1()
{
     foreach (...)
     {
        var result = await M2();
        uiElement.Text = result;
     }
}

async Task<string> M2()
{
    // sync portion runs on UI thread
    // don't perform a lot of CPU-intensive work

    // off main thread, same synchronization context - so sync part will be on UI thread.
    var result = await SomeReallyAsyncMethod(...);

    // sync portion runs on UI thread
    // don't perform a lot of CPU-intensive work
}

Comment: @ispiro `await` *does not* change what thread synchronous part of the code runs - so anything up to `await SomeReallyAsyncMethod` call will be on original thread. Synchronization context decides where execution continue after asynchronous operation ends.
Comment: @isipro, No, `await` will not create a new thread (using the Syncronization Context provided by UI) unless you order to create it manually using `await Task.Run(... cpu-bound code...)`.
Comment: Thanks. But I'm not explicitly using any Synchronization Context and I'm using this in UWP - Windows Universal Apps. Is that still OK?
Comment: Yes, the principle is the same
Comment: I don't understand your example - Since M1 is awaited - it will _not_ run on the UI thread. Isn't that so?
Comment: @ispiro - It will use current context - if starting operation on UI thread than it will come back to UI thread, if one from thread pool - will continue on arbitrary  thread.
Comment: But won't the `await` create a new thread for M1 so that `uiElement` will be updated from that second thread?
Comment: @ispiro, the Synchronization Context is set by UI framework. It is guaranteed that M1 and M2 will both run on UI thread.
Comment: OK. So it seems I misunderstood the whole `await` thing. So a) I _don't_ need to remove the `await` from M1. b) Any long calculations done in an `async` method _will_ stop the UI (unless using `Task.Run`). Did I understand correctly now? [I now see that I've been reading comments by two people. And they seem contradictory.]
Comment: @ispiro sounds right (but I don't see Andrey's and my comments are contradicting each other - so there is likely something else :)) I'd recommend searching for something like "C# await new thread" on search engine of your choice to read more. I.e. http://stackoverflow.com/questions/13429294/how-different-is-await-async-from-threading, consider asking separate question if need more clarification as it would be better than long comment thread...


How to implement a Drupal Commerce multi-select membership donation form?

Question: Something like this using Drupal Commerce? You select from a select list multiple options of which everyone has a price right? And so it adds it as a total below in a field like a cart, but in a same page and then below you fill your details and continue?

Or not a chance with Commerce and have to go with some other alternatives? Note I'm looking for Drupal friendly solution, not a PHP programmer here.
Cheers everyone for help!
Answer: I know you are asking about the form specifically, but it isn't clear to me how you are actually managing your 'memberships'. Something like CiviCRM that has full membership management tools can be integrated with both Drupal and with Commerce. There is a module for the latter, but we have found it problematic and have instead done the Commerce to CiviCRM integration using Drupal Rules and the CiviCRM Entities modules.
Answer: Maybe you want to try/experiment with the Commerce Donate module. Excerpt from its project page:

provides a donation line item type along with a donation product and product display.
In addition to being able to create a donation product, this module also provides a donation checkout pane, allowing customers to add a donation to their order as part of the checkout process.

Note: refer to this tutorial/screencast also for a great introduction to this module.
Comment: When possible, please try to adapt your answers for the individual questions instead of posting the same answer to multiple questions.  If you can't tailor the answer for the question, then you are likely answering duplicate questions, so please flag as dups or vote to close as dups.  Thanks.


Touch ID Crashing App

Question: When trying to use TouchID on my app, it hangs for a long time, and then will sometimes crash, and sometimes finally pass.  I have never tried anything with Touch ID, so it's all very much new to me.  My code is:
-(IBAction)touchID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *student = [defaults objectForKey:@"firstName"];
    NSString *usingTouch = [defaults objectForKey:@"useTouchID"];

    if ([usingTouch isEqualToString:@"Yes"]) {
        [passwordfield removeFromSuperview];
        LAContext *context = [[LAContext alloc] init];

        NSError *error = nil;
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                    localizedReason:@"Are you the device owner?"
                              reply:^(BOOL success, NSError *error) {

                                  if (error) {
                                      NSLog(@"%@", error);
                                      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                message:@"There was a problem verifying your identity."
                                                                                delegate:nil
                                                                            cancelButtonTitle:@"Ok"
                                                                            otherButtonTitles:nil];
                                      [alert show];
                                      return;
                                  }

                                  else if (success) {
                                      NSString *nope = @"Yes";
                                      [defaults setObject:nope forKey:@"firstName"];
                                      [defaults synchronize];
                                      [self updatetheview];

                                  } else {
                                      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                message:@"You are not the device owner."
                                                                                delegate:nil
                                                                            cancelButtonTitle:@"Ok"
                                                                            otherButtonTitles:nil];
                                      [alert show];
                                  }

                              }];

        } else {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Your device cannot authenticate using TouchID."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
            [alert show];

        }
        //Perform Touch ID Code
    }

}

The Console shows this over and over again before finally acting as you would expect it to immediately:

 Stack:(
    0   CoreFoundation                      0x000000018359cf60  + 148
    1   libobjc.A.dylib                     0x0000000198a47f80 objc_exception_throw + 56
    2   CoreFoundation                      0x000000018359ce90  + 0
    3   Foundation                          0x00000001845bb2d8  + 88
    4   Foundation                          0x0000000184441a1c  + 56
    5   Foundation                          0x000000018443d5dc  + 260
    6   UIKit                               0x0000000188c378c4  + 64
    7   UIKit                               0x0000000188b2aa78  + 240
    8   UIKit                               0x0000000188b2a32c  + 116
    9   UIKit                               0x0000000188b2a1b8  + 504
    10  UIKit                               0x0000000188e3c834  + 228
    11  UIKit                               0x0000000188b28bf8  + 412
    12  Fritch                              0x0000000100186f40 -[DirectoryViewController updatetheview] + 180
    13  Fritch                              0x0000000100186cac __34-[DirectoryViewController touchID]_block_invoke + 316
    14  LocalAuthentication                 0x000000018599ee28  + 72
    15  LocalAuthentication                 0x000000018599e7e4  + 256
    16  LocalAuthentication                 0x000000018599b004  + 324
    17  LocalAuthentication                 0x000000018599b168  + 128
    18  CoreFoundation                      0x00000001835a2a70  + 144
    19  CoreFoundation                      0x00000001834a04d4  + 284
    20  Foundation                          0x00000001845c5b4c  + 20
    21  Foundation                          0x00000001845c5940  + 796
    22  Foundation                          0x00000001845c79c8  + 292
    23  libxpc.dylib                        0x00000001994a0c80  + 28
    24  libxpc.dylib                        0x00000001994a0c24  + 40
    25  libdispatch.dylib                   0x000000010051dc68 _dispatch_client_callout + 16
    26  libdispatch.dylib                   0x000000010052a780 _dispatch_queue_drain + 1036
    27  libdispatch.dylib                   0x0000000100521958 _dispatch_queue_invoke + 464
    28  libdispatch.dylib                   0x000000010051dc68 _dispatch_client_callout + 16
    29  libdispatch.dylib                   0x000000010052cec8 _dispatch_root_queue_drain + 2344
    30  libdispatch.dylib                   0x000000010052c590 _dispatch_worker_thread3 + 132
    31  libsystem_pthread.dylib             0x000000019946d470 _pthread_wqthread + 1092
    32  libsystem_pthread.dylib             0x000000019946d020 start_wqthread + 4
)
Answer: The reply block of evaluatePolicy is not called on the main thread. You need to make sure all of your UI code in the reply block is done on the main thread.

Touch ID Crashing App


Finding symmetric probability distribution with some property

Question: Can someone help me find a probability distribution symmetric about the origin with the property that $P(X \geq 3Y) \neq 1/2$? I can't come up with anything for some reason. I've tried several p.d.fs of my own as well as well-known pdfs. $N(0, \sigma^2)$  and $U(-1, 1)$ don't work.
Answer: I suppose you are taking $\{X,Y\}$ to be i.i.d. Let $X$ take the values $0,+1,-1$ with probabilities $\frac 1 2 , \frac  1  4$ and $\frac  1 4$ respectively. Then $P(X \geq 3Y)=\frac  5 8$.
Answer: To complement @Kavi Rama Murthy's answer, perhaps (based on what you tried) what you might find useful is the proof of the fact that of $X$ and $Y$ are iid and $P(X = 3Y) = 0)$ (such as those with densities), then the probability you are looking for is going to be 1/2.
That follows from $P(X \geq 3Y) = P(-X \geq 3(-Y)) = P(X \leq 3Y)$ where the first equality follows from the independence and the symmetry. Now. the result follows from the facts that $X > 3Y, X < 3Y$ and $X = 3Y$ partition the space, and $P(X \geq 3Y) = P(X > 3Y) + P(X = 3Y)$


how to decide which distribution has the following variable

Question: David choose digits one by one, So that each digit from $\{0,\ldots,9\}$ has the same probability
to be choosen, and in each time the selection is independent.
let $X$ be the number of times that $0$ occurs between the first two ocurrences of $9$. (for example for the seq $13904509\ldots$, $X=2$)
what is the distribution of the $X$?
$1.$ Same as $Y-1$, when $Y$ is a geometric variable.
$2.$ Poisson
$3.$ Uniform
$4.$ None of the above
I eliminated uniform, because $X$ isn't bounded
I don't think it is option 1, because option 1 fit to the question about how many digits in general occur between the to occurrences of $9$.
can someone help me from here? I will be happy get any sort of time about how to solve this kind of questions.
Thanks.
Comment: Imagine you are watching the tosses as they occur. Call a $0$ a failure, and the second $9$ a success. So we are looking at the number of failures until the first success.
Comment: but in this way X will be a variable that counts the number of digits that occure bewtween the first "9" and the second "9" (?) and I am looking for the number of "0" between the two "9"
Comment: We are not interested in any digits other than $0$ or $9$. So a toss that results in one of those can be treated as a non-toss. Thus the problem is equivalent to counting the number of tails **before** the first head, when tossing a fair coin.
Comment: thanks, it's a new perspective for me. in this case what will be the parameter of Y? 0.5?
Comment: Yes, it is $1/2$. As for the perspective, let's look at a simpler problem where the idea will be clear. Toss a fair die repeatedly. What is the probability you will get a $1$ before you get a $6$? Slightly fancier: what is the probability you will get a $1$ or a $2$ before you get a $6$?
Answer: For every $n\geqslant0$, knowing that $X\geqslant n$, $X\geqslant n+1$ happens when one meets a (new) $0$ before meeting another $9$. Since $0$ and $9$ are equiprobable, this happens with probability $p=\frac12$. Hence, for every $n\geqslant0$, $P[X\geqslant n+1\mid X\geqslant n]=p$.
Can you recognize the distribution of $X$ from this computation?
Hints:

For every $n\geqslant0$, $P[X\geqslant n+1]=P[X\geqslant n+1\mid X\geqslant n]\cdot P[X\geqslant n]$.
For every $n\geqslant0$, $P[X=n]=P[X\geqslant n+1]-P[X\geqslant n]$.


Compare Function result to multiple values

Question: I'm redesigning some of my For-loops to run faster. Especially the ones that call functions to compare values with. I'm wondering if my basic Delphi knowledge is missing something really useful.
I have a lot of For-loops similar to this:
for i :=1 to 10000 do
  begin
    //---  Check if Condition for each Object (i) Property = Yes, Y or a Number  ---
    if (fGetPropValue(i,'Condition')='Yes') Or (fGetPropValue(i,'Condition')='Y') Or (StrToIntDef(fGetPropValue(i,'Condition'),0)>0)
    then
      // code...

  end;

In this case function gets called 3 times, if result is different than Yes or Y. If it's Yes, it's only called once and skips the rest of calls.
I can use single call and store value to variable, like this:
for i :=1 to 10000 do
begin
  //---  Get Condition for each Object (i) Property   ---
  vCondition:=fGetPropValue(i,'Condition');
  //---  Check Condition = Yes, Y or a number  ---
  if (vCondition='Yes') Or (vCondition='Y') Or (StrtoIntDef(vCondition,0)>0)
  then
    // code...

end;

This is good, but what in case where I need to compare more complex, like:
for i :=1 to 10000 do
  begin

    if (fGetPropValue(i,'Condition')='Yes') Or (fGetPropValue(i,'Condition')='Y') Or (StrtoIntDef(fGetPropValue(i,'Condition'),0)>0) And
       (fGetPropValue(i,'Enabled')='Yes') Or (fGetPropValue(i,'Enabled')='Y') Or (StrtoIntDef(fGetPropValue(i,'Enabled'),0)>0) And
       (fGetPropValue(i,'Visible')='Yes') Or (fGetPropValue(i,'Visible')='Y') Or (StrtoIntDef(fGetPropValue(i,'Visible'),0)>0) And
       (fGetPropValue(i,'AllowAccess')='Yes') Or (fGetPropValue(i,'AllowAccess')='Y') Or (StrtoIntDef(fGetPropValue(i,'AllowAccess'),0)>0)
    then
      // code...

  end;

In this case if I introduce 4 variable and get all 4 values, I eliminate the speedy option if first logical comparison is true - without 4 variable it will not execute anymore calls.
Any way to redesign complex comparison?
Answer: You need to extract this into a function that you can re-use:
function PropValueIsTrue(const PropValue: string): Boolean;
begin
  Result := (PropValue='Yes') or (PropValue='Y') or (StrtoIntDef(PropValue,0)>0);
end;

Then your code becomes:
if PropValueIsTrue(fGetPropValue(i,'Condition')) and
   PropValueIsTrue(fGetPropValue(i,'Enabled')) and
   PropValueIsTrue(fGetPropValue(i,'Visible')) and
   PropValueIsTrue(fGetPropValue(i,'AllowAccess')) then

Comment: Having seen quite a lot of your code recently, it's clear that this sort of re-factoring is not part of your toolkit. It's the most important of all the refactorings, and your code will be far better once you master it. You will benefit in many ways. Your code will be more readable and so easier to maintain. Your code will be less prone to copy/paste errors, what I refer to as *inheriting from the clipboard*. I recommend you read this: http://martinfowler.com/books/refactoring.html
Comment: Looking back over your code, it's clear that you can spot patterns that are amenable to extraction, but that you don't contemplate extracting. Your instinct is to retain all your code in one large do it all method. Once you get familiar with decomposing and extracting, you'll look back over your old code and wince!
Comment: They work right up until the point where you want to change behaviour, and then you pretty much have to review your entire code base. We've all been there! ;-)
Comment: Great! Didn't think about this approach.
Comment: You're right on your observation. 9 years of copy & paste of approaches 'that work'. Now, working on improving this. Thank you for the resource link.


Is there theory about complexity conservation of number?

Question: For any one-to-one correspondence $f : \mathbb{N} \rightarrow \mathbb{N}$, small numbers mostly corresponds to small numbers and big numbers mostly corresponds to big numbers, intuitively. And in fact, limit of the function $f$ diverges to $\infty$.
Similarly, for any one-to-one correspondence $f : \mathbb{N} \rightarrow \mathbb{Z}$, there is correspondence like "small to small, big to big", too. (It may be because $\mathbb{Z}$ is isomorphic to $\mathbb{N}$?) Small natural number mostly corresponds to integer that has small absolute value. Big natural number mostly corresponds to integer that has big absolute value. And limit of the function $|f|$ diverges to $\infty$.
It is able to think about $\mathbb{Q}$ similarly. Small natural number mostly corresponds to rational number that have low complexity of ratio.(Have small denominator and numerator when expressed in irreducible fraction form) Big natural number mostly corresponds to rational number that have high complexity of ratio.
It looks like that one-to-one correspondence conserves complexity of number. I think it may be able to measure absolute(?) complexity of number, from this complexity conservation.
What I wrote up to here is too abstract and just intuitive-based so I want to find some accurate theory or theorem about those subjects. Can you suggest where should I go?
Answer: I think one notion of "complexity" you might be looking for refers to ordered sets.
What you are actually saying, is that if we have a bijection between two ordinals, then $|f|\rightarrow\infty$, where in $|f|$ I refer to the ordinal representing the element $|f|$, and in $\infty$ I refer to the ordinal of the set $f$ is working on.
This claim is indeed true in general, for any linear order you may put on a set. Of course every linear order will induce different notion of "complexity", but I think this is basically the direction you should go at. If you can provide further details about things you've tried, or some examples, I can try to assist further.
Useful links -
https://en.wikipedia.org/wiki/Ordinal_number
https://en.wikipedia.org/wiki/Total_order


html tabindex property

Question: i have a form. which includes menubar, corporation icon, some links, and 10 input[type='text']. i have writen tabindex attribute only for 5 inputs. because i want to make so that only this five input can be focused. how i can do it?
Comment: -1? why? it was simple question or what else?
Answer: Approach 1: Harmful with relatively weak browser support (it depends on the HTML5 draft)
Set a negative tabindex value on every element you do not wish to get the focus.
This will render it impossible for some users (especially screen reader users who have a higher portion of non-mouse users among them) to access those elements of the page.
Approach 2: Equally harmful but with relatively high browser support.
Bind, using JavaScript, an onfocus event handler to every element that you don't want to receive the focus. Have it call the focus() method of the first form control.
Doing either of these is a user-hostile act. Don't do it.
Answer: tabindex sets the indexing order of items, but it does not set a limit on which elements can be focused.
You should be able to achieve what you want by giving the last input the following JavaScript:
<input onblur="$('#firstitem').focus();">

or alternatively, in the ready() block
$("#lastitem").blur(function() { $('#firstitem').focus(); })

keep in mind though that with that, you are taking away the user's ability to navigate the page using a keyboard.
Comment: yesss. greate. simple. but did not think. i know i will taking away the user's ability to navigate the page using keyboard. but only in this page. :-) i searched for it. thanks..

html tabindex property


query model.vo uma solução com jpql e subqueryes

Question: Estou com a seguinte query no MySQL querendo passar para JPQL e estou com dúvida na sintaxe:
SELECT a2.produto ,
       a2.descricao,
       a2.quantidade_recente,
       Soma
  from (SELECT p.codigoProduto,
               sum(p.quantidade_recente) as Soma
          from previsaovendas.produto  as p,
               previsaovendas.usuario u
         where u.codigo = p.codigo_filial
           and u.codigo_chefe = 3
         group by p.codigoProduto) as a1,
      (SELECT p1.codigoProduto as produto,
              p1.descricao,
              p1.quantidade_recente
         from previsaovendas.produto as p1,
              previsaovendas.usuario u
        where u.codigo = p1.codigo_filial
          and u.codigo_chefe = 3 ) as a2
 where a2.produto = a1.codigoProduto;

Veja o resultado na tela
A query em jpql que é :
String jpql1 = "select new br.com.previsao.model.vo.Filtro(a2.produto , a2.descricao, a2.quantidadeRecente, Soma)"
        + " (select p.codigoProduto, sum(p.quantidadeRecente) aS Soma  from Produto p where p.gerenteFilial.chefe.codigo =:codigo ) as a1,"
        + " (select p1.codigoProduto aS produto, p1.descricao, p1.quantidade_recente from Produto p1 where p1.gerenteFilial.chefe.codigo =:codigo ) as a2"
        + " where a2.produto = a1.codigoProduto";

 // metodo buscarPorPaginacao
 TypedQuery<Filtro> query = manager.createQuery(jpql1, Filtro.class);
 query.setParameter("codigo", 3L);

 List<Filtro> resultado = query.getResultList();

 for (Filtro prod : resultado) {
      System.out.println(" Impressão Filtro da Empresa: ");
      System.out.println(" Nome : " + prod.getCodigoProduto() +  " Filtro " + prod.getDescricao()+  " Quantidade " + prod.getQuantidade()+ " Total " + prod.getTotal());

 }

recebo o erro no stack :

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException:
  unexpected token: ( near line 1, column 99 [select new
  br.com.previsao.model.vo.Filtro(a2.produto , a2.descricao,
  a2.quantidadeRecente, Soma) (select p.codigoProduto,
  sum(p.quantidadeRecente) aS Soma  from br.com.previsao.model.Produto p
  where p.gerenteFilial.chefe.codigo =:codigo ) as a1, (select
  p1.codigoProduto aS produto, p1.descricao, p1.quantidade_recente from
  br.com.previsao.model.Produto p1 where p1.gerenteFilial.chefe.codigo
  =:codigo ) as a2 where a2.produto = a1.codigoProduto]
Comment: parece que a jpql não aceita subquerye
Answer: A consulta está incorreta, faltou uma vírgula bem no ponto onde o erro ocorre:
a2.descricao, a2.quantidadeRecente, Soma), (select p.
                                         ^

O JPQL no Hibernate aceita subqueries no SELECT, WHERE e no HAVING. Portanto, desde que a lógica da sua consulta esteja correta, é para funcionar.