how to create a thin wrapper around array of strings C

Question: when using Julia calling C, it is very nice that Julia can provide a thin wrapper around existing data allocated by C, which makes the data exchange efficiently between C and Julia. And here is the famous online example in Julia embedding section:
double *existingArray = (double*)malloc(sizeof(double)*10);
jl_array_t *x = jl_ptr_to_array_1d(array_type, existingArray, 10, 0);

the question I have is: can Julia gurus provide an example illustrating how to create a thin wrapper around existing array of strings (already allocated on heap in C)
A follow-up question is: do we also need to take care of write barriers in some way? since the array of strings is basically an array of pointers,
Another question is: how Julia does gc handling when the underlying data is an array of strings: i.e. array of pointers, each pointer pointing to the starting address of a null-terminated string.
Many thanks!!!
Zhiyuan
Comment: Are you looking for unsafe_wrap and unsafe_string?  You can use GC.@preserve to prevent the C string array from being gc-ed.
Comment: Thanks for pointing this out.
Answer: It looks that Julia 0.5.2 does not complain the following approach :
suppose that in C/C++ you have :
char* pchar points to "A is s\0B is so\0C is som\0D is some\0", size : 34 = size_all
and
char** myStrArray has 4 pointers : pointing to the location of "A", "B", "C", D",
and the goal is to pass pchar and myStrArray back to Julia without copying so that in Julia somehow you have an array of string :
{ "A is s", "B is so", "C is som", "D is some" }
in the C/C++ portion :
jl_value_t* array_type_pointer = jl_apply_array_type(jl_voidpointer_type, 1);
jl_array_t* str_array = jl_ptr_to_array_1d( array_type_pointer, (void*)myStrArray, 4, 1 );
jl_set_nth_field(ret, 14, (jl_value_t*)str_array );

jl_value_t* array_type_uint8 = jl_apply_array_type(jl_uint8_type, 1);
jl_array_t* chr_array = jl_ptr_to_array_1d( array_type_uint8, (uint8_t*)pchar, size_all, 1 );
jl_set_nth_field(ret, 13, (jl_value_t*)chr_array );

Notice that in jl_ptr_to_array_1d call, the last param := 1, which means that Julia owns the buffer.
in Julia portion
you declare the following two :
StrVec::Array
StrVecPtr::Array

both of the above two belong to ret1, say.
julia> String(copy( ret1.StrVec ) )
"A is s\0B is so\0C is som\0D is some\0"

julia> ret1.StrVecPtr
4-element Array{Ptr{Void},1}:
 Ptr{Void} @0x000000000404aa00
 Ptr{Void} @0x000000000404aa07
 Ptr{Void} @0x000000000404aa0f
 Ptr{Void} @0x000000000404aa18

julia> unsafe_string( convert( Ptr{UInt8}, ret1.StrVecPtr[1] ) )
"A is s"

julia> unsafe_string( convert( Ptr{UInt8}, ret1.StrVecPtr[2] ) )
"B is so"

julia> unsafe_string( convert( Ptr{UInt8}, ret1.StrVecPtr[3] ) )
"C is som"

julia> unsafe_string( convert( Ptr{UInt8}, ret1.StrVecPtr[4] ) )
"D is some"

Remarks : the contents are contained in an array of UInt8, separated by '\0', contiguous in memory, making it easier to do gc after all.
If someone spots some errors, or have alternative ways, please post.
Many thanks.
Comment: I'm not sure I have understood your question exactly, but I thought my recent answer to an unrelated question may also be relevant here https://stackoverflow.com/a/50747086/4183191


Clearing a Collection of Collections - clear every item first, or just clear all at once?

Question: Suppose I have a Collection of some kind that itself contains Collections; e.g., Dictionary(Of String, List(Of MyClass)). If I want to clear the Collection entirely, does it make any sense to clear every individual child collection before clearing the parent collection, like so:
For Each ListDef As KeyValuePair(Of String, List(Of MyClass)) In MasterDictionary
    Dim ThisList As List(Of MyClass) = ListDef.Value
    ThisList.Clear()
Next

MasterDictionary.Clear()

Or does this really accomplish nothing more than simply:
MasterDictionary.Clear()

I'm asking if there is any reason whatsoever -- performance, safety, clarity, etc. -- to use the first method. I generally use the second method myself, as I'm assuming that it implicitly achieves what the first method does anyway. But we all know how dangerous it is to assume sometimes; hence this question.
EDIT: Today, in my own application, I have seen compelling evidence that the first method might be preferable in certain cases. In my application I have some global collections (Dictionaries containing Lists, as in the example code above) that are populated when the user clicks a button labelled "Load Data". When the user clicks "Unload Data" to start back at the beginning, formerly I was clearing these collections using the second method above.
The problem was that when the user clicked "Load Data" a second time, the application would suddenly become very slow. Eventually it would finish populating the collections again, but at a snail's pace. I couldn't figure out how to fix this problem until I finally tried the first method above. Now reloading and repopulating the collections works just as quickly as loading them the first time.
Based on the answers posted so far, it sounds like I must have "other code somewhere" referencing the child collections; however, it doesn't look to me like I do. The only places I reference these children are where I iterate over their parents; if the parents are cleared, then they should have no children that the code is aware of, correct?
Hopefully someone can help me to understand how either these references are still lingering around, or else what is going on that I am overlooking.
Comment: Based on the code samples you show in your question there is no reason that the first fragment performs any differently than the second fragment; in either case the top-level dictionary is cleared at the end, and nothing (allegedly) refers to any of the elements that were originally in it.

The only way to explain a performance difference on the second load is if something else is going on, but to determine that it'll be necessary to get more information about what your load and unload are actually doing.
Answer: In both cases, you're not handling any disposal issues of individual elements contained in the child collections.  In this case, the two should be identical.
The one place where there would be a difference is if other code has a reference to any of the individual child collections directly.  If you hold a reference to one of the List(Of MyClass) in another portion of code, the first method will clear out the elements of the list (and effect your other code).  The second method will not do this, since it will leave the List alone.
If no other references are held, then the second method just waits on the GC to cleanup the elements.  The individual references are going to be handled by the GC in both cases, so the performance differences will be negligible.  Personally, I'd use the second method, since I believe it is more clear (and much easier to write), and the effect will be less surprising in most cases, especially if you're using the list elsewhere.
Comment: It has been a *long* time since I've even looked at the code I'd based this question on. Looking back, I'm pretty sure I must have been doing what you guess here -- holding references to the child collections elsewhere -- without realizing it. Honestly, the original project was such a big mess; I was just starting out and had much to learn. I'm accepting this answer now for posterity because I think it represents the most sensible advice to give to another developer who might be in this scenario (I don't even *have* this code to look at anymore myself).
Answer: If there are no other references to the 'child' collections, then Clear() on the 'parent' collection will result in the child collections (and by extension, the items in them) being cleaned up - eventually. I'd say you're safe as long as none of the child collections' elements implement IDisposable, as that implies they have some resource locked, and not disposing of them properly could lead to issues down the line.
Answer: It depends how your code is otherwise used.
If you have other code somewhere referencing any of the List(Of MyClass) then, obviously, those lists will then be empty to the other code using the first approach, whereas in the first one that will not be the case.
If there are any performance differences, that I do not know, and I suppose the best way to find out is to try and profile it. One could think the first method uses a bit more CPU power initially, but possibly less during a GC round - but really it's a case where profiling would make sense :).


JMO geometry Problem.

Question: $ABCD$ is a parallelogram. $E$ intersects $AD$ as $AE:ED =1:3$ and $F$ intersects $AB$ as $AF:FB=7:1$. $CE$ and $DF$ meets at point $P$. $CP:PE$ =?
Answer: Let $DF\cap BC=\{K\}$, $AE=a$ and $BF=b$.
Thus, $AF=7b$, $ED=3a$ and $BC=4a$.
Now, since $\Delta KBF\sim \Delta DAB$, we obtain:
$$\frac{KB}{4a}=\frac{b}{7b},$$ which gives
$$KB=\frac{4a}{7}$$ and since $\Delta KPC\sim\Delta DPE$, we obtain:
$$\frac{PC}{PE}=\frac{4a+\frac{4a}{7}}{3a},$$ which gives
$$\frac{PC}{PE}=\frac{32}{21}.$$
Answer: Using vectors . . .

Regarding all points as vectors, place the origin at $A$.

Since $|AE|\,{:}\,|ED|=1:3$, it follows that $E=\frac{1}{4}D$.

Since $|AF|\,{:}\,|FB|=7:1$, it follows that $F=\frac{7}{8}B$.

Since $ABCD$ is a parallelogram, it follows that $C = B+D$.

Since $P$ is on the segment $DF$, it follows that
$$P = D+s(F-D)$$
for some real number $s$, with $0 < s < 1$, where $s$ is the ratio $|DP|\,{:}\,|DF|$.
\begin{align*}
\text{Then}\;\;P &= D+s(F-D)
\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\,
\\[4pt]
&=D+s({\small{\frac{7}{8}}}B - D)\\[4pt]
&=\left({\small{\frac{7}{8}}}s\right)B + (1-s)D\\[4pt]
\end{align*}

Since $P$ is on the segment $CE$, it follows that
$$P = C+t(E-C)$$
for some real number $t$, with $0 < t < 1$, where $t$ is the ratio $|CP|\,{:}\,|CE|$.
\begin{align*}
\text{Then}\;\;P &= C+t(E-C)\\[4pt]
&=(B+D) + t({\small{\frac{1}{4}}}D-(B+D)\bigr)\\[4pt]
&=(1-t)B+(1-{\small{\frac{3}{4}}}t)D\\[4pt]
\end{align*}
Equating the two expressions for $P$, we get
$$\left({\small{\frac{7}{8}}}s\right)B + (1-s)D = (1-t)B+(1-{\small{\frac{3}{4}}}t)D$$
Since $B,D$ are linearly independent, we get the two equations
\begin{align*}
{\small{\frac{7}{8}}}s &= 1-t\\[4pt]
1-s &= 1-{\small{\frac{3}{4}}}t
\end{align*}
Solving the above system yields $s = {\large{\frac{24}{53}}},\;t = {\large{\frac{32}{53}}}$.

Since $|CP|\,{:}\,|CE|=t={\large{\frac{32}{53}}}$, it follows that $|CP|\,{:}\,|PE|=32\,{:}\,21$.
Answer:
Without loss of generality, I assumed that parallelogram is a rectangle (to be honest, I couldn't find how to draw a parallelogram in Paint so I made this assumption. But it doesn't affect the answer). Then by similarity of $\Delta FAD$ and $\Delta FBG$, we have
$$\frac{|FB|}{|AF|} = \frac{1}{7} = \frac{|GB|}{|AD|}$$So, $|GB| = \frac{4a}{7}$. Then by similarity of $\Delta PED$ and $\Delta PCG$, we have
$$\frac{|CG|}{|AD|} = \frac{|CP|}{|PE|} = \frac{\frac{32a}{7}}{3a} = \frac{32}{21}$$

JMO geometry Problem.


X probability function

Question: 3 men and 3 women sits on chairs numbered 1-6.
let X be the lowest number which a woman sits on.
what is the probability function of X?
I know X=1,2,3,4 and all the combinations is 64, but Im not sure how to start counting all the options
Comment: Show your attempt or ideas...
Comment: It's pretty easy to split the problems into cases... how many ways are there for a woman to be 1st? 2nd (but not 1st)? 3rd (but not 1st or 2nd)? 4th (etc...)?
Comment: This is not a "homework done for you" website. Ask,what part of the question you don't understand
Comment: let say the first is woman so (1/6)*(3c2)?
Comment: Ordering like WMMWMW gives $\binom{6}{3}=20$ possibilities. Do you understand that? You could say the $3$ chairs meant for a woman are picked out of $6$ chairs in total.
Answer: We solve a more general problem. The solution is likely to be understood only after a number of more concrete problems of this type have been solved.
We have $n$ chairs, numbered $1$ to $n$ and select $k$ of them. Let $X$ be the number of the smallest chair selected. What is the probability that $X=i$?
The possible values of $X$ are $1$ to $n-k+1$.  There are $\binom{n}{k}$ equally likely ways to choose $k$ chairs. Now we count the number of favourables, that is, the number of ways to choose $k$ chairs so that the smallest is $i$.
We must choose $k-1$ chairs from the chairs numbered $i+1$ to $n$. There are $n-i$ such chairs, so the number of favourables is $\binom{n-i}{k-1}$.
Comment: so I think the solution for my problem is:
1- 0.5
2- 6/20
3- 3/20
4- 1/20
Comment: @opeer That is correct.
Answer: An order like e.g. WMMWMW means that the first chair is for a woman,
the second for a man, et cetera.
There are $\binom{6}{3}=20$ of such orderings, since you could say that $3$ chairs are picked out of $6$ to be the chairs of women.
$X=1$ corresponds with orderings starting with W and there are $\binom{5}{2}=10$ such orderings. This because from the other $5$ chairs $2$ are picked out to be the chairs of women again.
Can you take it from here?

X probability function


Como adicionar atributos a um elemento que foi criado com JavaScript?

Question: Criei um elemento com JavaScript:
 $("
").addClass("placeholder").appendTo(element);

Mas não consigo dar atributos a ele, como posso fazer isso? Tentei o seguinte código, mas não funciona:
$(document).ready(function(){
     $(".placeholder").addClass("fg-red");
 });

Se eu crio ele com HTML funciona porém quando eu crio com JavaScript a tentativa de adicionar atributos não funciona
Comment: Isso aqui está errado `addClass(".placeholder")`. Não precisa do ponto antes para indicar que trata-se de uma classe, a função `addClass` serve exclusivamente para isto.
Comment: Desculpe não é o código verdadeiro digitei por celular e nas pressas saiu errado e não vi, desculpa
Answer: No momento que anexa o novo objeto
, apenas adicione as classes que deseja:
Exemplo 1
obj = $("
");
obj.addClass("placeholder");
obj.addClass("fg-red");
obj.appendTo(element);

Exemplo 2
$("
").addClass("placeholder").addClass("fg-red").appendTo(element);

Exemplo 3
$("
").addClass("placeholder fg-red").appendTo(element);

Comment: Este exemplo não funciona, ele está dentro de uma função o que eu acredito que não tenha como usar variáveis de funções
Comment: Nesse caso vou marcar a pergunta como não clara o suficiente. E também não ficou claro o que quis dizer no comentário. `...está dentro de uma função o que eu acredito que não tenha como usar variáveis de funções `
Comment: A sua resposta está de acordo com a minha pergunta, mas apenas gostaria de saber como posso fazer isso já que a criação do elemento está dentro de uma função no meu caso
Comment: coloque isso na sua pergunta. Alias, isso já se torna numa  questão secundária. Abra outra pergunta específica.
Comment: http://pt.stackoverflow.com/posts/comments/238685?noredirect=1
Answer: Escrevi algo para você analisar, para manipular elementos criados dinamicamente utilize a função ON do Jquery (vide documentação)

            Criar Elemento

            Adicionar Atributo ao Elemento Dinamicamente

Answer: É porque seu objeto está sendo criado dinamicamente,  use:
$(document). on (function (){
      ... aqui o que você quer fazer, no caso o addClass

}

Comment: O ready só atribui os eventos ao término do carregamento da página, em nenhum outro momento
Comment: Trocar o `ready` pelo `on` não funcionou, nem com o elemento já criado
Comment: Com os elementos já criados o ready é necessário manter, agora para os criados dinamicamente vou pesquisar melhor


Who serves in the first game after a tiebreaker?

Question: I'm watching the Federer-Roddick match in Miami.
Roddick served at 5-6 in the first set.  He ended up serving out the tiebreak, 7 points to 4.  He then opened the second set on serve.
How do they determine which player serves in the first game of the next set after a tiebreaker?
Answer: According to the Tennis for Dummies cheatsheet:

When tennis players win six games each, they generally play a
  tiebreaker to decide the winner of the set. (Tennis rules governing
  tiebreakers differ for team tennis and for the Wimbledon Championship
  and may differ for local tournaments as well.) If you're playing a
  tiebreaker, follow these steps:

The player due to serve the 13th game serves the first point into her opponent's deuce court.
After the first serve, the serve goes over to the other player, who then serves the next two points, serving first into the ad court and
  then into the deuce court.
After the third point, the players alternate, serving two points each — always beginning on the ad court.
After the first six points are played, no matter what the score is, the players change ends (and continue to do so after every other
  six-point span) until one player wins at least seven points with a
  margin of two points.

The player who serves the last game of the set before the tiebreaker
  serves the first game of the new set following the tiebreaker.
Answer: The answer requires knowing only 1 piece of information: who served first in the set that went to the tiebreak? The other player will always serve first in the next set.
Why? Connect 2 simple facts.
Fact 1) The player that served first in this set will necessarily be the first to serve in the tiebreak.
Fact 2) The player that serves first in the tiebreak, irrespective of the tiebreaker outcome, will return (i.e. NOT serve) in the first game of the next set.†
Combining fact 1 and 2:  If you serve first in a set that goes to a tiebreak, your opponent will ALWAYS serve first in the next set.
KISS: Keep It Simple Stupid
A lot of other explanations make it so confusing. It's easy: The player that serves first in a set that goes to tiebreak, will never serve first in the next set. The outcome of the tiebreak has nothing to do with this. You only need to know who served first in the set that went to a tiebreak to know who will serve first in the next set. It is always the OTHER player, always.

† Some people ask why fact 2 is true, which is an entirely different question. But it is worth addressing.
A simple rule to remember is that Players can never serve in consecutive games. Starting at 0-0, once you get to 6-6, it is necessarily the turn of the player who served first in the set to then serve first in the tiebreak. And since the tiebreak is considered a service game in-and-of-itself, and is served first by the same player who served first in the set, the next game after the tiebreak is the first game of the next set. And this must be served by the other player.
Answer: The player who served the last game before the tie break will serve the first game of the next set.
From the ITF 2017 Rules of Tennis book:

The player whose turn it is to serve shall serve the first point of the tie-break game. (...)
The player/team whose turn it was to serve first in the tie-break game shall be the receiver in the first game of the following set.

An easy way remember is to think of the tie-breaker as an additional game in the set, where the "server" is the player who serves the first point. Therefore the "receiver" of the tie break should serve the next game.
Answer: The answers above are true specific to singles, and are true for doubles if you substitute 'team' for 'player".  The tiebreaker itself is scored as a game so the set with the tie-breaker ends at 7-6 - so whichever player or team starts the tiebreaker is considered to have served that game from a scoring perspective.  The player or team that serves the last game prior to the tiebreaker will serve first in the following set.
Interestingly once a set starts players on each team must alternate who serves, but at the beginning of a new set, the order resets, so you could have either the player that served the last game prior to the tiebreaker serve the first game after OR his  or her partner.


How can I best use dplyr to subset data and create relative frequency tables?

Question: I'm using the iris data set to learn how to use dplyr, and am trying to create a relative frequency table that looks like this:

Petal.Width
.1
.2
.3
.4
.5
.6
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8

Species

setosa
0.10
0.58
0.14
0.14
0.02
0.02
0
0
0
0
0
0
0
0
0

versicolor
0
0
0
0
0
0
0.14
0.06
0.10
0.26
0.14
0.02
0.20
0.04
0.06

I'm struggling to group the observations by species, and then produce relative frequencies on a species by species basis.
I'm guessing it'll have to be something using group_by, mutate, and count, but the closest thing I could find online was this:
my_data %>%
    group_by(Petal.Width,Species) %>%
    summarise(n = n()) %>%
    ungroup %>%
    mutate(total = sum(n), rel.freq = n / total)

This was still not quite what I was looking for as it is the total number of observations, not the number per species.
Any help is appreciated greatly!
Answer: You could do this in dplyr, but it's a one liner in base R:
t(apply(table(iris$Species, iris$Petal.Width), 1, function(x) x/sum(x)))
#>
#>              0.1  0.2  0.3  0.4  0.5  0.6    1  1.1 1.2  1.3  1.4  1.5  1.6
#>   setosa     0.1 0.58 0.14 0.14 0.02 0.02 0.00 0.00 0.0 0.00 0.00 0.00 0.00
#>   versicolor 0.0 0.00 0.00 0.00 0.00 0.00 0.14 0.06 0.1 0.26 0.14 0.20 0.06
#>   virginica  0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.02 0.04 0.02
#>
#>               1.7  1.8 1.9    2  2.1  2.2  2.3  2.4  2.5
#>   setosa     0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00
#>   versicolor 0.02 0.02 0.0 0.00 0.00 0.00 0.00 0.00 0.00
#>   virginica  0.02 0.22 0.1 0.12 0.12 0.06 0.16 0.06 0.06

Created on 2022-02-02 by the reprex package (v2.0.1)
Comment: `m = table(iris$Species, iris$Petal.Width); m/rowSums(m)`
Answer: Something like this?
Not sure about the "wide" format though; I'd be inclined to keep it as long (omit the pivot_wider step).
library(dplyr)
library(tidyr)

iris %>%
  count(Species, Petal.Width) %>%
  group_by(Species) %>%
  mutate(p = n/sum(n)) %>%
  ungroup() %>%
  select(-n) %>%
  pivot_wider(names_from = "Petal.Width", values_from = "p")

Result:
Species    `0.1` `0.2` `0.3` `0.4` `0.5` `0.6`   `1` `1.1` `1.2` `1.3` `1.4` `1.5` `1.6` `1.7` `1.8` `1.9`   `2` `2.1` `2.2` `2.3` `2.4` `2.5`
  <fct>      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 setosa       0.1  0.58  0.14  0.14  0.02  0.02 NA    NA     NA   NA    NA    NA    NA    NA    NA     NA   NA    NA    NA    NA    NA    NA
2 versicolor  NA   NA    NA    NA    NA    NA     0.14  0.06   0.1  0.26  0.14  0.2   0.06  0.02  0.02  NA   NA    NA    NA    NA    NA    NA
3 virginica   NA   NA    NA    NA    NA    NA    NA    NA     NA   NA     0.02  0.04  0.02  0.02  0.22   0.1  0.12  0.12  0.06  0.16  0.06  0.06

Comment: Thank you so much! This is pretty close to what I need!

For my own learning, what is going on here with the n? I'd like to understand what its doing.
Comment: `n` is the default name for the column generated by `count`.
Comment: How does the n automatically identify which observations are tied to individual species? Instead of dividing all setosa instances by 150 values, but by 50 (the 50 that are setosa)?
Comment: The `group_by` does that. Best way to understand the code is to run it by adding each line: start with the first 2 lines, up to but not including the `%>%`, then the first 3, first 4 and so on (always up to but not including the `%>%`. That way you will see the output at each stage.


Limit Question from Calc I

Question: Compute the following limit, or show that the limit does not exist
$$\lim_{x\to 0}x^{2}e^{\sin(\frac{1}{x})}$$
Answer: $$\left|x^2e^{\sin1/x}\right|\le x^2e\xrightarrow[x\to 0]{}0$$

Limit Question from Calc I


Como manter posição de List/Menu em linha

Question: Olá estou tentando deixar um List/Menu na mesma linha que a descrição de um determinado produto que apresento ao usuário mas como a linha usa um H1 a minha List/Menu acompanha o tamanho do texto, criei uma div nominada como direita para algumas tentativas sem sucesso.

<a href="produtos.php?dep=<?php echo $row_rsDepartamento['id_departamento']; ?>"><?php echo $row_rsDepartamento['descricao'];  ?> > <a href="produtos.php?dep=<?php echo $row_rsDepartamento['id_departamento']; ?>&sub=<?php echo $row_rs_Subdepartamento['id_subdepartamento']; ?>"><?php echo $row_rs_Subdepartamento['descricao']; ?>

            <form action="produtos.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
             <select name="filtro" id="filtro" onChange="this.form.submit();">
               <option value="0">Ordenar por:</option>
               <option value="1">Popularidade</option>
               <option value="2">A-Z</option>
               <option value="3">Z-A</option>
               <input name="dep" type="hidden" value="<?php echo $dep; ?>">
               <input name="sub" type="hidden" value="<?php echo $sub; ?>">
             </select>
            </form>

O exemplo pode ser visto aqui:
List/Menu
JSFiddle
Comment: Sua página deu : _"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"_
Comment: Olá @Wallace Maxters, acabei de editar a página, obrigado pelo comentário.
Comment: @adventistapr, tente sempre posta as suas duvidas com SSCCE (Short, Self Contained, Correct (Compilable), Example - http://sscce.org/).
Acomselho que utilize o http://jsfiddle.net/ ou http://codepen.io/ para montar os seus exemplos.
desta forma fica até mais simples para nós modificamos os mesmos.
Answer: tente fazer algo do genero:

Breadcrumbs

        <select name="filtro" id="filtro">
               <option value>Selecione um Filtro</option>
               <option value="1">Popularidade</option>
               <option value="2">A-Z</option>
               <option value="3">Z-A</option>
        </select>

Ordenar por:

Você pode acessar o exemplo completo em http://jsfiddle.net/70k6hx1h/1/


Doubt on Fundamental Limit reduction

Question: I've recently learned about the fundamental limit and it's proof by the squeeze theorem, but now, I have the following task.
$\lim_{x \to 0} \dfrac{\sin{3x}}{x}$
I've looked at my old notes on trigonometrical identities, But I haven't managed to reduce this to a form of the $3$ multiplying $x$ goes out of the $\sin$ function.
Any tips ?
Thanks in advance.
Comment: What happen if we set $$3x=y$$?
Comment: Then we have $\lim_x{\sin{y}}{x}$, but this squeeze theorem isn't only true when the sine argument is equals to function denominator ?
Answer: $$\lim_{x \to 0} \frac{\sin 3x}{x} =  \lim_{x \to 0} \frac 31\cdot \frac {\sin 3x}{3x} = 3\cdot 1 = 3$$
We're using the fact that $$\lim_{\alpha\to 0} \dfrac {\sin \alpha}{\alpha} = 1.$$ In this case, $\alpha = 3x$.
Note, alternatively, that the posted limit initially evaluates to an indeterminate form $\dfrac 00$. Using L'Hospital, we get $$\lim_{x \to 0} \frac{\sin 3x}{x} = \lim_{x\to 0} \frac{3\cos 3x}{1} = \frac {3\cdot 1}{1} = 3$$
Comment: This rule only states for what you shown with \alpha, the sine argument must be equals to it's denominator ? And how about when we can't separate the denominator in a multiplication which does not affect the result ? like sin(4x)/sin(3x).
Comment: Isn't it true that $$\dfrac{\sin(3x)}{x} = \frac{3}{3}\cdot \frac{\sin(3x)}{x} = \frac{3}1\cdot \frac{\sin(3x)}{(3x)}\quad?$$ I didn't do anything to the argument of $\sin$. What I did was multiply the given function by $\frac 33 = 1$, hence it doesn't change the function, it simply allows for a nice representation of the function.
Comment: You're welcome, aajjbb.
Answer: Other two methods
1st method:
$\lim_{x\to 0} \frac{sin 3x}{x}=\lim_{x\to 0} \frac{cos 3x}{1} \times 3$ [By L'Hospital's Rule]
$=3$.
2nd method:
for any $\epsilon >0$ $|\frac{sin 3x}{x} - 3|=|\frac{sin 3x - 3x}{x}|<\epsilon $ [As |sin x|<|x|] whenever $|x|<\delta$
So,$\lim_{x\to 0} \frac{sin 3x}{x}=3$

Doubt on Fundamental Limit reduction


Place block region in node template

Question: I am trying to place a block region inside a news content type but it is not rendering.
node--news.html.twig:

{{node.label}}

{{ date }}

    {{ content }}

      {{ page.article_sidebar }}

How can I get the block to render inside of a node?
Comment: Blocks are not within the scope of nodes. You basically want to construct a page with something like [page_manager](https://drupal.org/project/page_manager).
Comment: No, it wasn't, not like this. As @mradcliffe said, if you want use blocks and regions like that, you need  something like page_manager/panels/panelizer (the last is afaik not ported yet and the others are in progress). Display suite is also an option and allows you to use layouts in node templates.
Comment: This was definitely possible in Drupal 7. Possibly related, D8: http://drupal.stackexchange.com/questions/186246/how-to-print-core-block-or-views-block-in-node-html-twig-drupal-8
Comment: I should have clarified, in D7, you could do it with a preprocess function, create a variable and then render that in a node template. It looks like the same thing is possible with D8 with issue I linked to above.
Answer: This is probably the solution, "Making region content available to node templates in Drupal 8": http://atendesigngroup.com/blog/making-region-content-available-node-templates-drupal-8

Replace "THEME" with your theme name below:
/**
* Implements hook_preprocess_node() for NODE document templates.
*/
function THEME_preprocess_node(&$variables) {
  // Allowed view modes
  $view_mode = $variables['view_mode']; // Retrieve view mode
  $allowed_view_modes = ['full']; // Array of allowed view modes (for performance so as to not execute on unneeded nodes)

  // If view mode is in allowed view modes list, pass to THEME_add_regions_to_node()
  if(in_array($view_mode, $allowed_view_modes)) {
    // Allowed regions (for performance so as to not execute for unneeded region)
    $allowed_regions = ['primary_content'];
    THEME_add_regions_to_node($allowed_regions, $variables);
  }
}

/**
* THEME_add_regions_to_node
*/

function THEME_add_regions_to_node($allowed_regions, &$variables) {
  // Retrieve active theme
  $theme = \Drupal::theme()->getActiveTheme()->getName();

  // Retrieve theme regions
  $available_regions = system_region_list($theme, 'REGIONS_ALL');

  // Validate allowed regions with available regions
  $regions = array_intersect(array_keys($available_regions), $allowed_regions);

  // For each region
  foreach ($regions as $key => $region) {

    // Load region blocks
    $blocks = entity_load_multiple_by_properties('block', array('theme' => $theme, 'region' => $region));

    // Sort 'em
    uasort($blocks, 'Drupal\block\Entity\Block::sort');

    // Capture viewable blocks and their settings to $build
    $build = array();
    foreach ($blocks as $key => $block) {
      if ($block->access('view')) {
        $build[$key] = entity_view($block, 'block');
      }
    }

    // Add build to region
    $variables[$region] = $build;
  }
}

After clearing caches, I can now print content specified in Drupal's block layout using my node template files. For example, if I've specified primary_content in $allowed_regions, then I can access it via node--node_type.html.twig with:
{{ primary_content }}

Comment: There is an error in the call to `system_region_list()`: The second argument is not a string, but an integer. Then, since the function is declared as `system_region_list($theme, $show = REGIONS_ALL)`, there is no need to use the second argument, if you are going to use its default value.
Comment: This looks like a link only answer. Please include a relevant quote of it in your answer here ...
Comment: I done this add to my .theme and I can add drupal blocks into this custom region,but region template isn't applied or suggest.How do you do this ?
I posted about that few time ago.thanks
Comment: It worked for me (also considering kiamlaluno comment). THANK YOU

Place block region in node template


How to plot two curves from data in two different dataframes on the same graph with ggplot?

Question: I'm very new to R and I have to deal with quite big datasets from a previous work. During these previous studies, the person worked on excel and I have to adapt everything on R.
Especially, I did 2 simple linear regressions. To simplify, the first one represents Y as a function of X from one dataframe, let's call it My_Data_1, and the second one is Y' as a function of X, with Y' a variable in the dataframe My_Data_2. In other words, X,Y and Y' come from different dataframes (among many other variables).
I'd like to compare the two regressions by plotting them on a single graph using ggplot2.
However, I don't know how to procede because the dataframe import is done in the ggplot, such as:
ggplot(data = My_Data_1, aes(x=X, y=Y)) +
geom_point() + etc...
I tried to put only x in the ggplot() and to put Y and Y' in geom_point() but it doesn't solve anything: Y' is unknown in this case because only one dataframe is imported in ggplot.
I didn't find solution. One would be to create a new table but I'd like to know if there is another way to do so.
I hope it was clear enough... Thanks by advance for your help!
Comment: Try something like: `ggplot() + geom_point(data = My_Data_1, aes(x=X, y=Y)) + geom_point(data = My_Data_2, aes(x=X, y=Y))`
Comment: Can you post the code you've tried and sample data? Please edit the question with the code you ran (no *"etc..."*, please) and the output of `dput(My_Data_1)`. Or, if it is too big with the output of `dput(head(My_Data_1, 20))`. And the same for `My_Data_2`.
Answer: You can use multiple layers inside one plot. For example you could use two different geom_smooth() layers. Each layer can be build from different data.
For example your first layer could look like this:
geom_smooth(data= df1, aes(x=x, y=y),method ="lm", se = FALSE, color = "blue")

And your second layer could look like this:
geom_smooth(data= df2, aes(x=x, y=y),method ="lm", se = FALSE, color = "red")

Same goes for your geom_point() layer.
In the end you can piece it together with +.
  ggplot()+
  geom_smooth(data= df1, aes(x=x, y=y),method ="lm", se = FALSE, color = "blue") +
  geom_smooth(data= df2, aes(x=x, y=y),method ="lm", se = FALSE, color = "red") +
  labs(title= "Two Regression Lines", x = "x Value", y = "Y Value")


Propensity score: which treatment effect is easier to infer?

Question: I'm currently working on a study where the goal is to estimate the treatment effect of a binary exposure. I want to calculate the Average Treatment Effect (ATE), Average Treatment Effect in the Treated (ATT) and the Average Treatment Effect in the Controls (ATC). For the dataset, I have plotted histograms of the propensity scores for the treated and untreated groups. Now my question is: from this histogram, what could be said about which effect is 'easier' to infer? Say that for treated and untreated, the histogram shows very high propensity scores, would ATC then be easier to infer, since for every untreated sample we could also 'consider it as treated' because of its high propensity score?
Answer: The treatment effect for the group about which there is the most information about the causal effect (i.e., for the which the uncertainty of the causal effect is the smallest) is called the average treatment effect in the overlap (ATO), and is described in Li, Morgan, and Zaslavsky (2018). The traditional estimand (i.e., ATT, ATC, or ATE) for which you will have the most certainty is the one for which the covariate distribution of the relevant group is most similar to that of the ATO sample.
In general, the most certainty is in the area with the most overlap. If there are many control units with high propensity scores, then the ATT will have higher precision. If there are many treated units with low propensity scores, then the ATC will have higher precision. If both groups have a wide propensity score range and there is a lot of overlap, the ATE will have good precision. It sounds like in your case the ATT will be the most precise.
If you are using propensity score weighting, you should check the effective sample size resulting from the weights for each estimand. The WeightIt package makes it easy to estimate weights and assess their properties, including the effective sample size. The weights with the largest effective sample size are those that should yield the best precision in the effect estimates.

Li, F., Morgan, K. L., & Zaslavsky, A. M. (2018). Balancing Covariates via Propensity Score Weighting. Journal of the American Statistical Association, 113(521), 390–400. https://doi.org/10.1080/01621459.2016.1260466


MIDI controller

Question: I recently bought a second hand midi controller (Alesis Q49) and I' m trying to connect it to my p/c, so that I can play (not interested in recording). I've installed 'virtual piano', selected my midi in MIDI Device menu, but still cannot hear sound from my midi.
Can anyone help please ?
Comment: All on the same MIDI channel?
Comment: Do you have a sound card?
Comment: Hi Tom, I do have sound card for my guitar recordings, can I use it for my midi ?
Answer:
Make sure that you've connected 'OUT' on the controller to 'IN' on your computer and your cable is not broken.
Make sure that both devices are set to the same MIDI channel.
Make sure that your software is sending audio to the correct audio output, and the volume is turned up.
Answer: I use an old but still very useful free program for Windows called MIDI OX, which monitors and displays all sorts of MIDI messages going on between PC and controllers. http://www.midiox.com/
This software is extremely useful for figuring out what is going on. It'll show you if a device is connected or not, it'll show you events when pressing the keys or other buttons, and so on. Various filters allow to only see certain messages and not others, etc.
I'm sure there are other similar programs for Mac and Windows if you look. Google "MIDI monitor software" or something like that to find them.

MIDI controller


Extreme Value Theory and Floods

Question:
Let's start with an example problem case. Say we measure two variables that are non-normally distributed and correlated. For example, we look at various rivers and for every river we look at the maximum level of that river over a certain time-period. In addition, we also count how many months each river caused flooding. For the probability distribution of the maximum level of the river we can look to Extreme Value Theory which tells us that maximums are Gumbel distributed. How many times flooding occurred will be modeled according to a Beta distribution which just tells us the probability of flooding to occur as a function of how many times flooding vs non-flooding occurred.

Source: https://twiecki.io/blog/2018/05/03/copulas/
Does anyone know why the maximums are Gumbel distributed? Shouldn't their distribution depend on the data itself?
Does anyone know why the number of floods are modeled by a beta distribution? Again, shouldn't their distribution depend on the data itself (e.g. maybe normally distributed)?
Answer: You have two questions so I'll answer them in turn
(1) Why are the maxima Gumbel distributed?
Essentially, this comes from the extremal types theorem which is a limiting distribution for block maxima. The most common limits distribution is the CLT which states
$$ \frac{\bar{X} - \mu}{\sigma} \to N(0,1) \text{ as } n \to \infty$$.
Now suppose we have data index by say time, and want to analyse the largest possible values the data could take. This could be amount of flooding, measured by volume of water spilling out of a river bank every day. In the $k$th year, having $N$ observations, we denote the block maxima as $M_k = \max \{y_1, y_2, \ldots, y_N \}$. You can think of these as e.g. annual maxima. Then the extremal types theorem says, for suitable sequences of constants, $a_k$, $b_k$ that
$$P\left(\frac{M_k - a_k}{b_k} \leq x\right) \to G(x)$$ where $G$ has one of three forms.  The three forms are
Gumbel: $G(x) = \exp\{ -\exp(-x)\}$
Frechet: $G(x) = \exp(-x^{-\alpha}) \text{ with } (x, \alpha > 0)$
Weibull: $G(x) = \exp\{ - (-x)^{\alpha} \}\text{ with } (x<0, \alpha > 0)$
and for $x>0$ the Weibull form takes the value $1$.
Note that, like the CLT, this result hold regardless of the distribution of the $y$ values. Although, the particular form might depend on the $y$ values. For instance, if the data are positive (daily rainfall counts) the Frechet would be a sensible form, whereas the Weibull would not support this type of observation.

(2) Why are the number of floods modelled by a Beta distribution?
The article mentions that they are going to model this by modelling the probability of a flood. That is $P(\text{flood}) = \frac{\text{num. floods}}{\text{num. obs}} \in [0,1]$. The Beta distribution has support on $(0,1)$ which is well suited to probabilities as they lie in $[0,1]$. If we modelled the probability by a Normal distribution, we might end up making statement like $P(\text{flood}) = -0.3$ which is not a valid statement. The Beta distribution will always give a value in $(0,1)$ as the answer. Thus the statements are always, in some sense, valid.

Extreme Value Theory and Floods


Use `RootModel` as `BaseSettings`

Question: I want to use a following model for my settings:
<code>import pydantic
import pydantic_settings

class Clustered(pydantic.BaseModel):
    is_clustered: typing.Literal[True]
    server_name: str

class NonClustered(pydantic.BaseModel):
    is_clustered: typing.Literal[False] = False

# ???
Settings = pydantic_settings.RootSettings[Clustered | NonClustered]
</code>
But I cannot seem to figure out a way to do this, and multiple inheritance does not work:
<code>>>> class Settings(pydantic_settings.BaseSettings, pydantic.RootModel[NonClustered | Clustered]): pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 117, in __new__
    cls: type[BaseModel] = super().__new__(mcs, cls_name, bases, namespace, **kwargs)  # type: ignore
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen abc>", line 106, in __new__
  File "/usr/local/lib/python3.12/site-packages/pydantic/root_model.py", line 50, in __init_subclass__
    raise PydanticUserError(
pydantic.errors.PydanticUserError: `RootModel` does not support setting `model_config['extra']`
</code>
Answer: This is not possible yet, follow https://github.com/pydantic/pydantic-settings/issues/200 for updates.

Use `RootModel` as `BaseSettings`


JavaFX Custom TableCell Listening

Question: The TextFieldTableCell, for example, responds and calls commitEdit() when you hit the enter key.
If I use a custom node to display, then how can I listen for ways to confirm an edit? For example, I have a DateTimePicker which extends a VBox. I can't add a KeyListener to a VBox.
What can I do to watch for confirmation of in-table edits? Here is what I have attempted:
class TableCellDateTimePicker extends TableCell<CHILD, LocalDateTime> {

    public TableCellDateTimePicker() {
        super();
    }

    @Override
    public void startEdit() {
        super.startEdit();
        DateTimePicker picker = new DateTimePicker();
        picker.getDatePickerField().setOnKeyPressed((KeyEvent event) -> {
            System.out.println("PRESS");
            if (event.getCode() == KeyCode.ENTER) {
                commitEdit(picker.getValue());
            }
        });
        picker.setValue(getItem());
        setGraphic(picker);
    }

    @Override
    public void commitEdit(LocalDateTime newValue) {
        super.commitEdit(newValue);
        setText(newValue.toString());
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
    }

}

DateTimePicker:
/**
 * A custom DateTimePicker that lets a user select a LocalDateTime.
 *
 * @author Toni-Tran
 */
public class DateTimePicker extends VBox {

    @FXML
    private DatePicker datePicker;
    @FXML
    private Button showTimePickerBtn;

    private NumberPicker hourField;
    private NumberPicker minuteField;

    private ObjectProperty<LocalDateTime> currentLocalDateTimeValue;

    public DateTimePicker() {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource(
                ScreenPaths.DATE_TIME_PICKER));
        loader.setController(this);
        loader.setRoot(this);
        try {
            loader.load();
        } catch (IOException e) {
        }
    }

    @FXML
    private void initialize() {
        showTimePickerBtn.setOnAction(event -> this.showTimePicker());

        hourField = new NumberPicker();
        hourField.setBounds(0, 23);
        hourField.setWrapAround(true);

        minuteField = new NumberPicker();
        minuteField.setBounds(0, 59);
        minuteField.setWrapAround(true);

        currentLocalDateTimeValue = new SimpleObjectProperty<>();

        datePicker.valueProperty().addListener((ObservableValue<? extends LocalDate> observable,
                LocalDate oldValue, LocalDate newValue) -> {
                    LocalDateTime newLDT;
                    if (newValue != null) {
                        newLDT = LocalDateTime.of(newValue.getYear(),
                                newValue.getMonth(), newValue.getDayOfMonth(),
                                hourField.getValue(), minuteField.getValue());
                        currentLocalDateTimeValue.set(newLDT);
                    } else {
                        currentLocalDateTimeValue.set(null);
                    }
                });

        hourField.valueProperty().addListener((ObservableValue<? extends Number> observable,
                Number oldValue, Number newValue) -> {
                    if (currentLocalDateTimeValue.getValue() != null) {
                        LocalDateTime newLDT = LocalDateTime.of(currentLocalDateTimeValue.getValue().getYear(),
                                currentLocalDateTimeValue.getValue().getMonth(), currentLocalDateTimeValue.getValue().getDayOfMonth(),
                                hourField.getValue(), minuteField.getValue());
                        currentLocalDateTimeValue.set(newLDT);
                    } else {
                        currentLocalDateTimeValue.set(null);
                    }
                });

        minuteField.valueProperty().addListener((ObservableValue<? extends Number> observable,
                Number oldValue, Number newValue) -> {
                    if (currentLocalDateTimeValue.getValue() != null) {
                        LocalDateTime newLDT = LocalDateTime.of(currentLocalDateTimeValue.getValue().getYear(),
                                currentLocalDateTimeValue.getValue().getMonth(), currentLocalDateTimeValue.getValue().getDayOfMonth(),
                                hourField.getValue(), minuteField.getValue());
                        currentLocalDateTimeValue.set(newLDT);
                    } else {
                        currentLocalDateTimeValue.set(null);
                    }
                });
    }

    /**
     * Manually set a LocalDateTime value for this control.
     *
     * @param val The {@link LocalDateTime} value for this control.
     */
    public void setValue(LocalDateTime val) {
        if (val == null) {
            return;
        }
        LocalDate day = LocalDate.of(val.getYear(), val.getMonthValue(),
                val.getDayOfMonth());
        datePicker.setValue(day);
        hourField.setValue(val.getHour());
        minuteField.setValue(val.getMinute());
    }

    /**
     * Returns the currently set LocalDateTime.
     *
     * @return the {@link LocalDateTime} value of this control.
     */
    public LocalDateTime getValue() {
        return currentLocalDateTimeValue.getValue();
    }

    /**
     * Returns the Observable property of the DateTimePicker's current
     * LocalDateTime value.
     *
     * @return the Observable property of the DateTimePicker's current
     * LocalDateTime value.
     */
    public ObjectProperty<LocalDateTime> valueProperty() {
        return currentLocalDateTimeValue;
    }

    /**
     * This is triggered when the user wants to view the screen for setting a
     * certain time for their LocalDateTime.
     */
    public void showTimePicker() {
        HBox topContainer = new HBox();
        topContainer.getChildren().addAll(hourField, new Label(":"),
                minuteField);
        topContainer.setSpacing(3);
        DialogPopup.showNodeApplicationlModal(topContainer);
    }

}

Comment: Don't have time to test it out, but does `picker.setOnAction(event -> commitEdit(picker.getValue()));` work?
Comment: It doesn't have the option to use setOnAction. I have also tried using setOnMouseClicked instead. It doesn't respond to clicks at all.
Comment: Oh, wait... What is `DateTimePicker`? I misread `DatePicker`
Comment: Added the code snippet for my DateTimePicker. I actually tried adding a listener to the DTP's valueProperty, and it responded to changes. However, I'm not sure how to write commitEdit(), or updateItem(). Am I even supposed to override updateItem()?
Answer: Typically, an editable cell will display the default text when in non-editing mode and an editing control (your DateTimePicker) when in editing mode. Your custom cell implementation needs to handle the transition between those as well as making sure the text and value for the editing control are properly updated if the cell's item is updated.
So you should override updateItem(...) to update the value of the DateTimePicker if in editing mode (isEditing()) or the text if not. Override startEdit() to switch to show the DateTimePicker instead of the text, and cancelEdit() to switch back.
You might not need to override commitEdit() at all. If your column is bound to a property, the default commitEdit() should update that property (or you can do this in an onEditCommit handler registered with the column).
The tutorial has an example of a fully-implemented editable table cell: see listing 13.11 near the bottom.
However, you will need to invoke commitEdit(...) at the appropriate time. The semantics of this are a bit tricky: you probably want to commit the edit if the user presses Enter when any of the individual controls inside your DateTimePicker have focus, and perhaps when the focus moves away from the DateTimePicker entirely.
I would recommend creating an onAction property in your DateTimePicker class. This is pretty easy to do, you just have to call setEventHander(...) when the property changes. There's a nice example in Richard Bair's MoneyField (there's a lot of stuff in that link, just look at where he creates the onAction handler). You can then (internally to the DateTimePicker) check for action events (or perhaps Enter key presses) on the individual controls and call fireEvent(new ActionEvent(...)) when that happens. Then your table cell implementation can register an onAction with the DateTimePicker and call commitEdit(...).
Committing the edit on focus loss shouldn't be much harder. Maybe expose a ReadOnlyBooleanProperty hasFocus() in DateTimePicker:
private final ReadOnlyBooleanWrapper hasFocus = new ReadOnlyBooleanWrapper(this, "hasFocus");
hasFocus.bind(
     hourField.focusedProperty()
    .or(minuteField.focusedProperty())
    .or(datePicker.focusedProperty())
    .or(showTimePickerButton.focusedProperty()));

// ...
public ReadOnlyBooleanProperty hasFocusProperty() {
    return hasFocus.getReadOnlyProperty();
}

Then your cell implementation can observe that property and commit the edit if it changes to false. You may have to experiment with that a bit to get it to work satisfactorily.
Comment: Excellent resources. I will look into those. One last question however: I overrode the updateItem() method like so:
`protected void updateItem(LocalDateTime item, boolean empty) {
    if (item == null || empty) {
        setGraphic(null);
    }else{
        setGraphic(null);
        setText(item.toString());
    }
}`
 but whenever I rearrange the table columns, I end up with an added duplicate cell directly underneath it. Is my method wrong, or is this a JavaFX bug?
Comment: Set the text to null as well as the graphic when the cell is empty.
Comment: It seems that the JavaFX built in DatePicker automatically fires an event when you select a date. My custom TimePicker contains a TextField which also fires an event when you hit the enter key. Since these both reside in my DateTimePicker, it will "catch" those events and fire another event out to the TableCell that is listening to it. Where exactly does fireEvent() get picked up from? What is listening to it?

JavaFX Custom TableCell Listening


Theorem on primitive n-th root of unity

Question: My manual states the following theorem and its corollary. I don't understand the proof of the corollary:
Theorem Let $ζ$ be a primitive n-th root of unity and let $k$ be an integer, then $ζ^k$ has order $n/(n,k)$.
We put $\delta = GCD(n,k)$.
Corollary The primitive $n$-th roots of unity are
$ζ_{n,k} = \cos \frac{2\pi k}{n} + i \sin\frac{2\pi k}{n} = ζ_n^k$
where $(n,k)=1$ and $1\le k \lt n$
Proof. Indeed from $(ζ^k)^{n/\delta}$ = 1
it follows that $(ζ^k)$ is primitive if and only if $\delta = 1$
Now, I don't understand why has it to be $\delta = 1$, since $(ζ^n) = 1$ owing to primitivity, I should have
$(ζ^k)^{n/\delta}$ =  $(ζ^n)^{k/\delta}$ = 1
whatever delta. I'm not sure what I'm misinterpreting of the theorem (Is maybe $\zeta$ not primitive?).
Comment: Yes, the equality is true for every $\delta$ that divides $n$ but then $\zeta^k$ is not primitive anymore. For example, $(-1)$ is a 4th root of unity but it is not a primitive one since $(-1)^2=1$ and $2\lt 4$
Comment: What is $\delta$? Are you sure it's not "$\zeta^k$ has order $n/(n,k)$? Or it could be that the author defines $\delta:=(n,k)$ and says "$\zeta^k$ has order $n/\delta$"?
Comment: For reference, could you post a picture of the text in question and the name of the book in your post?
Comment: you're right! I made a mistake
Comment: your proof is more compelling than the one the manual gives, thanks
Comment: If $\delta\neq 1$, then $n/\delta\lt n$ which, along with $(\zeta^k)^{n/\delta}=1$ suggests that $\zeta^k$ is not a primitive $n$-th root of unity since it equals to $1$ for a smaller power than $n$
Comment: I don't understand where it deduced that $(\zeta^k)^{n/\delta}=1$. Isn't $(\zeta^k)^{n/\delta}=1$ true for every $\delta$ as I tried to show in the question?
Answer: The confusion may come from the notion of "being primitive". Though it is not mentioned in your question, a root of unity is primitive of a certain order. If $\zeta$ is a primitive $n$-th root of unity, then $\zeta^k$ is a primitive $n$-th root of unity if and only if $(n,k)=1$, and in general $\zeta^k$ is a primitive $\tfrac{n}{(n,k)}$-th root of unity.
For example, if $\zeta$ is a primitive sixth root of unity, then so is $\zeta^5=\zeta^{-1}$. Of course $\zeta^3=-1$ is not a primitive sixth root of unity; it is a primitive second root of unity. And indeed $\tfrac{6}{(6,3)}=2$.
So really your proof is fine, it just doesn't prove what you may think it proves; the identities
$$(\zeta^k)^{n/\delta}=(\zeta^n)^{k/\delta}=1^{k/\delta}=1,$$
shows that the order of $\zeta^k$ divides $\tfrac{n}{\delta}$. A simple argument shows that the order of $\zeta^k$ is precisely $\tfrac{n}{\delta}$, which shows that $\zeta^k$ is a primitive $\tfrac{n}{\delta}$-th root of unity. In particular this shows that $\zeta^k$ is not a primitive $n$-th root of unity if $\delta\neq1$!
Answer: The theorem says that if $\zeta$ is a primitive $n$-th root of unity, then the order of $\zeta^k$ is $n/(n,k) = n/\delta$.
But $\zeta^k$ is a primitive $n$-th root of unity iff its order is $n$, i.e. $n/\delta = n$ which is equivalent to $\delta = 1$.

Theorem on primitive n-th root of unity


Reading from a URL Connection Java

Question: I'm trying to read html code from a URL Connection. In one case the html file I'm trying to read includes 5 line breaks before the actual doc type declaration. In this case the input reader throws an exception for EOF.
URL pageUrl =
    new URL(
        "http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html"
    );

URLConnection getConn = pageUrl.openConnection();
getConn.connect();
DataInputStream dis = new DataInputStream(getConn.getInputStream());
//some read method here

Has anyone ran into a problem like this?
URL pageUrl = new URL("http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html");
URLConnection getConn = pageUrl.openConnection();
getConn.connect();
DataInputStream dis = new DataInputStream(getConn.getInputStream());
String urlData = "";
while ((urlData = dis.readUTF()) != null)
    System.out.println(urlData);

//exception thrown

java.io.EOFException
      at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
      at java.io.DataInputStream.readUTF(DataInputStream.java:572)
      at java.io.DataInputStream.readUTF(DataInputStream.java:547)

in the case of bufferedreader, it just responds null and doesn't continue
pageUrl = new URL("http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html");
URLConnection getConn = pageUrl.openConnection();
getConn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(getConn.getInputStream()));
String urlData = "";
while(true)
     urlData = br.readLine();
     System.out.println(urlData);

outputs null
Comment: Line breaks are not EOF. Perhaps post your reading code and the exception it is throwing?
Comment: I agree with the above comment from Brian R., without the stack trace it's hard to tell what the problem is.   Also, I'm not sure why you would need to use a DataInputStream to read HTML.  That is for reading Java primitive types (binary) mostly.   If you want to read line-by-line, BufferedReader is a better (non-deprecated) choice.
Answer: You're using DataInputStream to read data that wasn't encoded using DataOutputStream. Examine the documented behavior for your call to DataInputStream#readUtf(); it first reads two bytes to form a 16-bit integer, indicating the number of bytes that follow comprising the UTF-encoded string. The data you're reading from the HTTP server is not encoded in this format.
Instead, the HTTP server is sending headers encoded in ASCII, per RFC 2616 sections 6.1 and 2.2. You need to read the headers as text, and then determine how the message body (the "entity") is encoded.
Answer: This works fine:
package url;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

/**
 * UrlReader
 * @author Michael
 * @since 3/20/11
 */
public class UrlReader
{

    public static void main(String[] args)
    {
        UrlReader urlReader = new UrlReader();

        for (String url : args)
        {
            try
            {
                String contents = urlReader.readContents(url);
                System.out.printf("url: %s contents: %s\n", url, contents);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public String readContents(String address) throws IOException
    {
        StringBuilder contents = new StringBuilder(2048);
        BufferedReader br = null;

        try
        {
            URL url = new URL(address);
            br = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = "";
            while (line != null)
            {
                line = br.readLine();
                contents.append(line);
            }
        }
        finally
        {
            close(br);
        }

        return contents.toString();
    }

    private static void close(Reader br)
    {
        try
        {
            if (br != null)
            {
                br.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Answer: This:
public class Main {
    public static void main(String[] args)
        throws MalformedURLException, IOException
    {
        URL pageUrl = new URL("http://www.google.com");
        URLConnection getConn = pageUrl.openConnection();
        getConn.connect();
        BufferedReader dis = new BufferedReader(
                                 new InputStreamReader(
                                     getConn.getInputStream()));
        String myString;
        while ((myString = dis.readLine()) != null)
        {
            System.out.println(myString);
        }
    }
}

Works perfectly. The URL you are supplying, however, returns nothing.
Comment: The supplied URL yields a 301 response ("Moved Permanently").
Comment: Ok thanks everyone. I didn't notice the 301, but now I fixed it

Reading from a URL Connection Java


Validar email em memória JAVA

Question: No java, como faço pra verificar se o String que o usuário digitou possui o caractere "@" ou se termina com ".com" ou ".br" pra validar o email em memória ?
Comment: A pergunta linkada é com javascript, mas basta usar a classe `java.util.regex.Pattern` que dá para adaptar isso ao java facilmente.
Comment: Ainda sobre o uso de regex para validar emails, tem algumas coisas  [aqui](https://pt.stackoverflow.com/a/340617/112052), [aqui](https://pt.stackoverflow.com/a/327150/112052), [aqui](https://pt.stackoverflow.com/a/365489/112052) e [aqui](https://pt.stackoverflow.com/a/348859/112052) (este último link tem algumas opções no final, só não recomendo a última regex). Para o uso de regex especificamente em Java, sugiro [este tutorial](https://docs.oracle.com/javase/tutorial/essential/regex/)
Comment: Você pode usar a regex `\w+(@)\w+(.)\w+`, que verifica a existência de um "@" e um "." e caracteres entre elas.
Answer: Para saber se a string possui o caractere @, você pode utilizar o contains:
String teste = "abc@s";
if (teste.contains("@")) {
    System.out.println("existe o caractere @ na string");
} else {
    System.out.println("não existe o caractere @ na string");
}

Para saber se a string termina com .com ou .br você pode utilizar uma substring:
String teste = "teste.com";
if (teste.substring(teste.length() - 4, teste.length()).equals(".com")) {
     System.out.println("a string acaba com .com");
} else {
     System.out.println("a string nao acaba com .com");
}

if (teste.substring(teste.length() - 3, teste.length()).equals(".br")) {
    System.out.println("a string acaba com .br");
} else {
    System.out.println("a string não acaba com .br");
}

Comment: Em vez de `substring`, também é possível usar [`endsWith`](https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#endsWith-java.lang.String-)

Validar email em memória JAVA


Consulta de dados na Web no Excel

Question: trabalho em uma empresa e preciso monitorar a vida útil de toners das impressoras diariamente...
Para isso criei uma planilha de monitoramento, só que queria um sistema prático, tipo assim:
Quando eu inserir o endereço da web em uma célula da planilha, automaticamente do lado aparecer a página da web...
Isso é para evitar ir no navegador, olhar, e depois inserir a porcentagem.
 Mas eu queria tipo um frame dentro da planilha.Por exemplo: Eu coloco o endereço "google.com" na célula A7, aí automaticamente do lado aparecia a exibição da página. É possível
Comment: Siga [este Tutorial em inglês](https://stackoverflow.com/a/21859804/7690982), use o tradutor caso necessário.
Comment: Caso seu Excel seja maior do que o 2013, [ler isto](https://support.microsoft.com/en-us/help/2793374/cannot-insert-certain-scriptable-activex-controls-into-office-2013-doc), em que a Microsoft desabilitou esta opção por motivos de segurança. Mas se qainda quiser se arriscar, realize os passos do link.
Comment: Desse jeito eu já tentei porém não aparece a barra para inserir uma URL.
Eu queria inserir uma URL em alguma barra ou célula e automaticamente aparecer a visualização da página da WEB ao lado.
É possível?
Comment: Sim, ao invés de `Me.WebBrowser1.Navigate "http://www.google.com"`, você utiliza uma variável no lugar de google.com e insere os valores como Textbox no userform e usa algum botão para iniciar.
Comment: Não faço a mínima ideia de como fazer isto.
Me dá um exemplo por gentileza?
Answer: 1) Entrar no VBA

Habilite o modo Desenvolvedor
Abra o VBA ou aperte Alt+F11

2) Adicionar Userform
Na janela de Projetos, clique com o botão direito e Insira um Userform

3) Inserir Web Browser
Dentro do Userform, inserir o Browser.

Clique em Ferramentas e depois Controles adicionais...

Ou com o botão direito na Caixa de Ferramentas

Dentro de Controles Adicionais..., habilitar Microsoft Web Browser. Com isso a foto de um planeta será inserida na Caixa de Ferramentas

Inserir Web Browser dentro do Userform, clicando no planeta.

4)Inserir TextBox
Clique no botão da caixa de ferramentas da imagem e insira a TextBox

5)Inserir Botão
Clique no botão da imagem e insira um botão de comando.

6)Código

Clique para Exibir o código do Userform

Inserir o código.
Private Sub CommandButton1_Click()
Dim web_link As String

    web_link = TextBox1
    Me.WebBrowser1.Navigate web_link
End Sub

7)Resultado

O Userform pode ser melhorado, como por exemplo, abrindo uma janela que é redimensionado para a tela inteira. E para chamar o Userform, basta adicionar algum botão, para isto há vários tutoriais.

Obs.: Caso o Web Browser não funcione, leia este link para habilitá-lo. Lembre-se que o mesmo foi desabilitado do Excel 2013 ou maior, para melhorar a segurança.
Comment: Perfeito! Obrigado.

Consulta de dados na Web no Excel


Augmented Reality : Recognize hand written number?

Question: I am trying to find a solution for this AR app as the topic tells.
I want my app to recognize a hand-written number by the user.
The app will tell the user to write down for example number 24 on a paper and move the camera over the written number to see the 3d object.
This might be used for saving a Birthday, a wedding date .. etc
For accuracy, the app instructions will show the user a preview to tell please write the number 24 similar to this..
Although each hand writing will differ, but at least we do not get curly "2"-s or "4" with an open edge ..etc
So here we need AR to recognize the number, or be able to read the number according to approximation.
And the first question is: Is such a behavior doable or anyone familiar with a similar concept?
After searching similar apps, I found "Ink Hunter" apps for tatoo preview-s, although these apps use symbols not number, but we can think of a number as a symbol as well.
Also as this video: https://www.youtube.com/watch?v=9rXJcIE2Fcs shows, each user draws the symbol in a different way and still they get it working.
I am using Unity3d and Vuforia.
Vuforia offers free samples(unity3d packages) on the website, and there is one named "Text Recognition" , and here's the tutorial link: https://www.youtube.com/watch?v=W3MK6nC5FWE
But unfortunately couldn't make it work.
If someone has developed such a functionality using these sample projects from vuforia or have any ulternative method please I need you help :)
thanks in advance moghes
Comment: My suggestion is not concerning Vuforia or Unity. Have a look at  [Tesseract OCR](https://github.com/tesseract-ocr). It can also be used as an OpenCV module.
Comment: Thanks for the hint about the OCR, as I started searching topics with OCR, more tools were introduced. I found an another third party "Wikitude" which again supports unity3d integration. My go to tool is unity 3d since it publishes for almost every platfrom, is integrated with almost all technologies available.
Comment: In InkHunter we have written an openCV lib which recognizes robustly our "square smile".
You might able to use Vuforia User Defined Targets for tracking (based on surrounded objects) and OCR for text recognition(if you need to show different 3d-models for different texts).
Answer: Here's a tutorial our team created on text recognition using the Hololens and Vuforia with Unity: https://www.youtube.com/watch?v=WdMeHgD4fMY. In the first portion of the video, we show how to get text recognition working with just Vuforia and Unity - no Hololens required. For your application, just change the text to numbers.
I believe the biggest challenge you will have is the "hand-written" component. From our research, Vuforia prefers computer-generated, predefined font types.


Export hyper-v VM with differencing disk

Question: I've created a test domain using Hyper-V with differencing disk, which works fine, you save a lot of space.
Now I want to export this environment using the Hyper-V manager, and import it to another machine.
But during the export of all machines who use differencing disk, the process fails with the message 'export failed'. Nothing more.
Questions;
-Is exporting VM with differencing disk supported by Hyper-V?
-Is maybe possible to just copy the files and manually reconstruct the situation?
-Can I merge the harddisks, so they all have private harddisks again?
Answer: Starting in Windows Server 2012, you no longer need to export a virtual machine in order to be able to import it. But, the export functionality still exists and it can be an easy way to prepare virtual machines to be imported.
https://technet.microsoft.com/en-us/library/hh831535(v=ws.11).aspx
How about just copy all the files and import them on the target host?
Comment: That link doesn't tell anything about differencing disk. But coping them, and recreate the VM's manually does seem to work. Just make sure you also have the parent disk in place!


Como fazer uma pesquisa no google com vba?

Question: Estou com dificuldade para montar uma programação em VBA que faça uma pesquisa no google.
Não consigo entender a parte do HTML.
A ideia é identificar o internet explorer aberto, ir para a página do google e buscar a palavra "teste" na pesquisa.
Segue código:
Dim ie As SHDocVw.InternetExplorer
Dim objShell As Object
Dim objWindow As Object
Dim objItem As Object
Dim objCollection As Object

Sub teste()

Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
    If LCase(objItem.FullName Like "*Internet Explorer*") Then
        Set ie = objItem
    End If
Next objItem

ie.navigate ("https://www.google.com.br/?gfe_rd=cr&ei=-s-bVf3ZBcf5gASo1oHAAw&gws_rd=ssl")

Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop '** Wait til page loaded

Set objCollection = ie.document.getElementsByTagName("input")

i = 0

Do While i < objCollection.all.Length
    If objCollection.all(i).ID = "q" Then
        objCollection.all(i).Value = "teste"
        Exit Do
    End If
    i = i + 1
Loop

End Sub

Comment: Acho que acessar o internet explorer assim é arriscado e ineficiente. Além disso, acessar o conteúdo da página para realizar a buscar parece complicado demais, inutilmente. Porque não usar o Shell para abrir o endereço da pesquiza `https://www.google.com.br/search?q={busca}`? O windows vai abrir, automaticamente, a janela do navegador padrão, e uma aba nova se o navegador estiver aberto.
Comment: O que eu queria era entender a lógica do HTML, para usar em outros sites depois.
Comment: Minto, acho que fazer isso vai abrir o windows explorer, independente de qual é o navegador padrão. Ainda assim recomendo essa linha.
Comment: O que quer dizer com lógica do html? Você quer analizar a html da página do google?
Comment: Sim, não estou conseguindo fazer a busca dos campos pelo html.
Seria a mesma lógica para preencher formulários online, analisar o HTML para identificar o id do campo e usar o VBA para preenchê-lo.
Answer: Consegui entender o problema.
segue solução que encontrei:
Dim ie As SHDocVw.InternetExplorer
Dim objShell As Object
Dim objWindow As Object
Dim objItem As Object
Dim objCollection As Object

Sub teste()

Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
    If LCase(objItem.FullName Like "*Internet Explorer*") Then
        Set ie = objItem
    End If
Next objItem

ie.navigate ("https://www.google.com.br/?gfe_rd=cr&ei=-s-bVf3ZBcf5gASo1oHAAw&gws_rd=ssl")

Do While ie.Busy Or ie.READYSTATE <> 4
DoEvents
Loop '** Wait til page loaded

Set objCollection = ie.document.getElementsByTagName("input")

i = 0

Do While i < objCollection.Length
    If objCollection(i).Name = "q" Then
        objCollection(i).Value = "teste"
        Exit Do
    End If
    i = i + 1
Loop

End Sub

Answer: Na página do google, o ID não é "q".
O elemento input onde se digita a busca tem atributo name="q".
Novamente, acho que essa forma de lançar a busca no google é contra-indicada. Eu executaria um Shell com o endereço do googgle, inserindo a texto pesquizado.


incorporating extra predictors in a forecasting model

Question: I have a Machine Learning demand forecasting task for a healthcare service. I have some columns such as date, diagnosis, hospital etc and each row represents a particular time that this service was used. Now I want to make weekly forecasting models for the usage of this service, so I group-by the date column and get the counts for each week, creating this way a 'demand' column. Now I have only a DateTime and demand columns. How I could incorporate my other predictors in a forecasting regression model. Maybe I am missing something as I am new to time-series.
Answer: One standard method would be regression with ARIMA errors. Take a look at "The ARIMAX model muddle", which has examples in R.
Actually, depending on your predictors, you might not have any temporal dynamics left over after the regression part, so the errors might be modeled as white noise. This can happen in particular if you explicitly model seasonality.
Note that you will need to forecast the values of your predictors themselves in some way (unless you only use lagged values with lags at least as large as your forecasting horizon).


What's the right database for this? Mongo, SQL, Couch or something else?

Question: Let's say I've got a collection of 10 million documents that look something like this:
{
"_id": "33393y33y63i6y3i63y63636",
"Name": "Document23",
"CreatedAt": "5/23/2006",
"Tags": ["website", "shopping", "trust"],
"Keywords": ["hair accessories", "fashion", "hair gel"],
"ContactVia": ["email", "twitter", "phone"],
"Body": "Our website is dedicated to making hair products that are..."}

I would like to be able to query the database for an arbitrary number of, including 0 of, any of the 3 attributes of Tags, Keywords, and ContactVia. I need to be able to select via ANDS (this document includes BOTH attributes of X and Y) or ORs (this document includes attributes of X OR Y).
Example queries:

Give me the first 10 documents that have the tags website and
shopping, with the keywords matching "hair accessories or fashion"
and with a contact_via including "email".
Give me the second 20 documents that have the tags "website" or
"trust", matching the keywords "hair gel" or "hair accessories".
Give me the 50 documents that have the tag "website".

I also need to order these by either other fields in the documents
   (score-type) or created or updated dates. So there are basically four "ranges" that are queried regularly.
I started out SQL-based. Then, I moved to Mongo because it had support for Arrays and hashes (which I love). But, it doesn't support more than one range using indexes, so my Mongo database is slow..because it can't use indexes and has to scan 10 million documents.
Is there a better alternative. This is holding up moving this application into production (and the revenue that comes with it). Any thoughts as to the right database or alternative architectures would be greatly appreciated.
I'm in Ruby/Rails if that matters.
Comment: One you start having queries like *site in tags.. you might have to go with something like http://sphinxsearch.com/
Comment: I'm not sure why you need support for more than one range querying using an index.  If the index is used for several query clauses, are the returned results still so large that they can't be sorted in memory efficiently?  Is your data set large enough that you can't create multiple indexes corresponding to different queries?
Comment: is the set of tags overlapping with the set of keywords? or are they non-overlapping sets?  In other words, can a particular string be both a keyword and a tag?
Comment: I think there is no reason why you couldn't use an SQL DB. For alternative schemas see here: http://stackoverflow.com/questions/597016/what-tag-schemas-are-the-most-efficient-effective .. and here: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
Comment: A better question than "which database" is probably "which search product". For example, see [Fulltext search in NoSQL databases](http://stackoverflow.com/questions/5453872/full-text-search-in-nosql-databases). It depends if you want search engine features like [faceted search](http://en.wikipedia.org/wiki/Faceted_search), [stemming](http://en.wikipedia.org/wiki/Stemming), [stopwords](http://en.wikipedia.org/wiki/Stopwords), and [boolean operators](http://en.wikipedia.org/wiki/Web_search_query) .. or prefer to create something simpler yourself. Elasticsearch seems a popular open source option.
Comment: The tags, keywords & contact_via are overlapping.  They are completely separate attributes, but since we don't set the names, they could have the same name, yes.
Comment: I've been leaning heavily toward flipping to a search-based product, and leaving the data where it is. I tried hitting Cassandra or Couch and both of those were a big bag of fail.  I'll update as I'm testing ElasticSearch.
Answer: When needing to do multiple queries on arrays, we found the best solution, at least for us, was to go with ElasticSearch. We get this, plus some other bonuses.  And, we can reduce the index requirements for Mongo.. so it's a win/win.
Answer: My two cents are for MongoDB. Not only can your data be represented, saved, and loaded as raw Ruby hashes, but Mongo is modern and fast, and really, really easy to know. Here's all you need to do to start Mongo server:
mongod --dbpath /path/to/dir/w/dbs

Then to get the console , which is just a basic JavaScript console, just invoke mongo. And using it is just this simple:
require 'mongo'
db = Mongo::Connection.new['somedb']
db.stuff.find #=> []
db.stuff.insert({id: 'abcd', name: 'Swedish Chef', says: 'Bork bork bork!'})
db.stuff.find #=> [{id: 'abcd', name: 'Swedish Chef', says: 'Bork bork bork!'}]
db.stuff.update({id: 'abcd', {'$set' => {says: 'Bork bork bork!!!! (Bork)!'}}})
db.stuff.find #=> [{id: 'abcd', name: 'Swedish Chef', says: 'Bork bork bork!!!! (Bork)!'}]

Comment: We're currently in Mongo.  But, we can't query it without big scans, because you can only query a single range and use indexes.  It won't create an index that has multiple ranges on it (arrays, sort orders, etc).


Export massive MySQL database, from a shared level hosting account

Question: I need to export a huge database, with thousands of tables and millions of rows. I'm on shared hosting so I can't use command line exports, and I really have no easy way to get to this data other than through phpMyAdmin or php. cPanel's default "export database" tool doesn't work, it exports a blank database. phpMyAdmin's export as gzip or zip also gives a blank database.
I've been downloading this one SQL file for a few hours now, and it's only at 114 mb of approximately 2 gb of actual data.
What's the best way for me to (safely) export all my data? This is really a huge pain and no data can be lost, but it must be exported out. Are there any scripts or programs I can use?
Comment: @Tuga That's one of the reasons why I am changing my hosting provider, and you're being rather unnecessarily rude. You don't post answers requiring a "thank you" for each one, and your provided answer didn't help solve my problem at all.
Comment: Ask your host to export it for you.
Comment: @ceejayoz I'm not sure they would know how...
Comment: "I'm not sure they would know how" : change your host provider
Answer: Can you access the database directly? MySqlDump.exe will work remotely in a pinch.
Comment: That doesn't mean he can't access MySQL remotely.
Comment: he does not have commandline access
Comment: To his own PC? Most folks with that sort of setup don't have mysql behind a firewall, you can hit 3306 directly . . .
Comment: How do I obtain and operate MySqlDump.exe?
Comment: Download and install mysql locally. For instructions, see the documentation on mysql AB's site.
Comment: I've got it now on my laptop, which is running XP. How can I use this to connect remotely to the server and make a local backup?
Comment: I'd start by looking at the command line parameters, should be rather obvious . . .
Comment: "RTFM" isn't the proper way to provide help on sites like this...
Comment: @cyclone - you're partially right, but when something is very well documented, we do expect that you read the documentation. This is a site for professionals, after all. If you have a problem with doing a mysql dump, open a new question with specific details of what you've tried and why it's failing.
Answer: Take a look at :
http://www.siteground.com/tutorials/php-mysql/mysql_export.htm
You'll find a tutorial that will teach you how to export databases using phpmyadmin, installed by default on cpanel.

Also, cpanel has a backup feature that allows you to backup only databases
https://yourdomain.com:2083/frontend/x3/backup/wizard-backup-type.html?type=mysql

Alternatively, you can try to use the function below (stolen from http://davidwalsh.name/backup-mysql-database-php) :
backup_tables('localhost','username','password','blog');

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);

    //get all of the tables
    if($tables == '*')
    {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while($row = mysql_fetch_row($result))
        {
            $tables[] = $row[0];
        }
    }
    else
    {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }

    //cycle through
    foreach($tables as $table)
    {
        $result = mysql_query('SELECT * FROM '.$table);
        $num_fields = mysql_num_fields($result);

        $return.= 'DROP TABLE '.$table.';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        $return.= "\n\n".$row2[1].";\n\n";

        for ($i = 0; $i < $num_fields; $i++)
        {
            while($row = mysql_fetch_row($result))
            {
                $return.= 'INSERT INTO '.$table.' VALUES(';
                for($j=0; $j<$num_fields; $j++)
                {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                    if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                    if ($j<($num_fields-1)) { $return.= ','; }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n";
    }

    //save file
    $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
    fwrite($handle,$return);
    fclose($handle);
}

Of course, you'll need to provide database credentials to the function, as well as an array of tables you'd like to backup. If you provide a "*" or no tables, a complete database backup will run. The script does the rest!
Don't forget to add set_time_limit(0); to the php file containing the backup_tables() function.
GL!
Comment: As I stated in the question, the cpanel defaults didnt work. Your script breaks at 360 seconds in, the server wont let me run it for more than that.
Comment: Don't forget to add `set_time_limit(0);` to the php file containing the `backup_tables()` function.
Comment: The server is configured to halt script execution at 360 seconds regardless of PHP's execution time limits.
Answer: You could try to use mysqldumper.
It's a script that downloads a part of your DB, remembers where it stops, calls itself again and continues to download.
Here is the link: http://www.mysqldumper.net/


Ordering 2 symmetric matrices in the same fashion

Question: I have 2 symmetric matrices, one of them being a correlation matrix and the other one similar to a correlation matrix. Examples of these matrices are shown below:
Correlation Matrix (c):
    A    B    C    D
A   1   0.5  0.1  0.4
B   0.5   1   0.9  0.3
C   0.1  0.9   1   0.3
D   0.4  0.3  0.3   1

Other Matrix (z):
    A    B    C    D
A   3    2    2    2
B   2    3    3    2
C   2    3    3    2
D   2    2    2    3

I'm ordering the correlation matrix in descending order so I can look at the top-most correlation values, using the following code:
c = corrMatrixMin10.abs()
s = c.unstack()
so = s.sort_values(kind="quicksort")
pd.DataFrame(so[so.values!=1].sort_values(ascending=False))

My question is as follows:
When I arrange the correlation matrix c in a descending order, the correlation matrix itself loses its shape. How do I have the other matrix z in the exact same order?
For example: The intersection of columns A and B in the matrix c is 0.5. The intersection of columns A and B in the matrix z is 2. How can I still preserve this order to associate these 2 values after arranging the matrix c in a descending order?
Any help would be greatly appreciated. TIA.
The code to generate the 2 matrices is as follows:
c = pd.DataFrame([[1, 0.5,  0.1, 0.4],
                   [0.5,  1,  0.9, 0.3],
                   [ 0.1,  0.9,  1, 0.3],
                   [ 0.4, 0.3, 0.3,  1]],
                  columns=list('ABCD'))

z = pd.DataFrame([[3, 2,  2, 2],
                   [2,  3,  3, 2],
                   [ 2,  3,  3, 2],
                   [ 2, 2, 2,  3]],
                  columns=list('ABCD'))

Answer: You can use Series.reindex
c_series = c.unstack().drop([(x, x) for x in c]).sort_values(ascending=False)
z_series = z.unstack().reindex(c_series.index)


Configure jenkins with different build tupes

Question: I have a question about configuring jenkins.
I have two build types in my build gradle file for my android project. Release and debug.
I want configure jenkins with option where I can choose what I want to build - release or debug.
I can't find any useful article how i can do this in my android application.
Any help is highly appreciated!!
Comment: Why not just build both?
Comment: After I will add this, I want add build flavors for each build type. Testers need only debug application, with different flavors. And release app I should build only when we go to production =)
Comment: Then it seems you really want separate jenkins jobs for each.
Comment: @laalto maybe =) can you give me some link to read how i can configure it in different jobs?
Answer: Solution is very simple. I'm build my app using grade script

Add build parameter "Choice parameter" with any name(In my case name is "BUILD_TYPE")
Add choices, in my case they are "Debug" and "Release"
In section "Invoke gradle script", in "tasks" write this - "clean assemble$BUILD_TYPE"
Save changes

Done! Now you will choose debug or release, and Jenkins will provide you with different build types of your app


What should be the tone effect of lowering (neck) pickup?

Question: Exactly what should be effect of lowering the neck pickup on a semihollow jazz guitar? (besides of it lowers the volume)
I am using clean jazz tone with a very little reverb, and no other effect. I am lowering the neck pickup, and besides of the volume is lower a bit, what change should I hear?
Added info:

Instrument: Ibanez AM 93 or better: Ibanez AM 93 Lefty
Neck pickup: Factory Super 58 Custom, according to Ibanez specification.
Strings: D'Addario Chromes Flat Wound, Light, 12-52
Comment: What a strange comment. The question was not "What will be the effect?" instead it was "What should be the effect?". Does it makes sense now, or should I explain in detail why "What should be the effect" can not be answered by "just trying".
Comment: _I am lowering the neck pickup...what change should I hear?_ - If you don't know what's going to happen, why have you decided to lower it?
Answer: " experienced players know how great of a difference pickup height can make. They talk of pickup height in terms of finding a "sweet spot" where pickups sound their best and are most responsive and dynamic. Often, but not in any case always, this means lowering your guitars pickups. Guitar techs even apply a term, Stratitis, to the negative effects of having pickups too close to the strings"
From:
https://bgmb55.wordpress.com/2012/02/17/tuning-your-sound-balancing-your-pickups/
Comment: +1 for the stratitis mention- I wondered what caused that. No idea why someone downvoted this answer.
Comment: This should be a comment, not an answer.
Answer: If I remember correctly, lowering the neck pickup should produce a darker, less treble-y sound as you're getting less of the attack of the string.
As the other answer touched on, it's worth just doing a little bit of research into pickup height and what the effects are, as well as experimenting yourself seeing as it's easily reversible if you don't like the effect produced. Remember you can also lower or heighten just one side of the pickup, so you can effectively get a very treble-y sound from the higher strings if you heighten the pickup there and also a much darker tone from the lower strings if you lower that side of the pickup.
Answer: It would help to know what type of pickups you are using.  There are many types of humbuckers and single coils as well as active and passive pu's.  For instance humbuckers can be found with different magnets and windings each of which can dramatically affect the tone and volume.  The pickups on a Gibson es-175, which many talk about as a quintessential jazz guitar are already dark and somewhat subdued.  I have found that lowering them effects the volume more so than the tone.  I agree with the previous answers lower them your self or raise them, you can always turn them back to there previous height.  Just remember that raising them to high can cause the magnets to pull your stings downward.
Comment: You are completely right, thanks for warn me. I was going to add this information, but somehow I forgot it. So here it is, and I am also going to edit the question to add this information: Instrument: Ibanez AM 93 Neck pickup: Factory Super 58 Custom, according to Ibanez specification.
Answer: Raising pickup will make it sound more characteristic for position. So neck position will become more fat and bridge will have more treble. Lowering should do opposite. Set best for you.


What is the difference between S3 video storage and streaming?

Question: I'm hosting videos on aws S3 at the moment. I can place the s3 url into the src attribute of my  tags and everything works correctly and plays as though the video is being streamed to my site. These are not small videos either. Some are 1gb in size.
I can also immediately jump to the end of the video as though the entire file wasn't downloaded, but just the part I need.
Whenever I google info on streaming on demand video from aws I get answers that I need a service in front of s3 to do something like this. Is aws automatically doing this for me?
Comment: S3 acts like a web server, and it supports ranged gets. When you scrub through the video, the video player in your browser simply requests a new range of bytes from S3. The entire file is not downloaded. Also, see [here](https://stackoverflow.com/questions/45900015/how-does-html5-video-seeking-api-work).
Answer: S3 support partial GET requests. This allows clients to request only a specific part of the file. Most modern players (including HTML5) are able to utilize this feature to provide the experience you describe to the users.
Quoting from here:

HTTP range requests allow to send only a portion of an HTTP message
from a server to a client. Partial requests are useful for large media
or downloading files with pause and resume functions, for example.


Questions about R survey package

Question: I have to use the R "survey" package and want to be sure to understand it.
I simulated data where the sample comes from a simple random sampling process.
When I compare the result of the Pearson's chi-square test (chisq.test), and the results using svychisq, the p values slightly differ (though really very close), while I expected them to be the same. Am I missing something?
Besides, I want to compare some characteristics between two groups, from data coming from a sampling, with different sampling probabilities but the same probability inside each group (a control group where the sampling probability is 1/1000 for all controls, and an "exhaustive" case group : probability=1 for all cases). In that case, is there a real impact on the test, that justifies using the Rao-Scott chi-square test?
Comment: Interesting. I just posted this: https://stats.stackexchange.com/questions/464720/stratified-survey-calculations-by-hand-and-with-survey-package-dont-agree-simu
Answer: You would expect slight differences.  The Rao-Scott tests were developed (according to Alastair Scott) to give approximately correct inference for estimated population tables, so the code estimates the population table, calls chisq.test() on it, and rescales the statistic. Even for simple random sampling, this will introduce slight variations.
When comparing proportions in the two strata under case-control sampling there is no need to use the Rao-Scott test.  The test is for the 2-way association parameter in a loglinear model, and this association parameter is preserved by case-control sampling (for the same reason that logistic regression coefficients are).
[I don't think this has anything to do with the linked Stratified survey calculations by hand and with survey package don't agree. Simulation results

Questions about R survey package


Como resolver o erro java.lang.NoClassDefFoundError ao executar o rhinit() no R

Question: Estou integrando o Hadoop com R utilizando Rhipe e a minha configuração é a seguinte:

Ubuntu 14.
Hadoop 1.0.3
R 3.2.2
Rhipe 0.73.1

Ao iniciar no ambiente R o rhinit()   aparece a seguinte mensagem:

rhinit() Rhipe: Using Rhipe.jar file Initializing Rhipe v0.73 Error in
  .jnew("org/godhuli/rhipe/PersonalServer") :
  java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataInputStream

Já pesquisei bastante em outros foruns e até o momento ainda não obtive uma solução.
Conto com a ajuda dos senhores, desde já agradeço.
Comment: Parece estar relacionado ao Rhipe não conseguir encontrar o path do Hadoop. Segundo [este link](https://amalgjose.wordpress.com/2013/05/05/rhipe-installation/#comment-298), configurar algumas variáveis deveria resolver.
Answer: Esse problema é causado porque as seguintes variáveis de ambiente não estão definidas corretamente :PKG_CONFIG_PATH e LD_LIBRARY_PATH
Configurando o R

exporte PKG_CONFIG_PATH = / usr / local / lib
exporte LD_LIBRARY_PATH = / usr / local / lib

Também escreva estes comandos no console do  R:

Sys.setenv (HADOOP_HOME = "/ usr / local / hadoop /")
Sys.setenv (HADOOP_BIN = "/ usr / local / hadoop / bin")
Sys.setenv (HADOOP_CONF_DIR = "/ usr / local / hadoop / conf")