Problema na função de procurar frases

Question: Não estou descobrindo o problema que está na minha função, ela faz o seguinte:

Eu recebo um vetor de char com o '\n' no final.
Depois eu percorro o vetor verificando:
Se encontrar no vetor o ponto de exclamação, ponto final, ou de interrogação, quer dizer que eu encontrei uma frase (frase foi definido como uma palavra que termina com um ponto uma exclamação ou uma interrogação), e guardo a posição do vetor;
Se eu encontrar um espaço, eu simplesmente guardo a posição;
Se eu encontro um dígito de 0 a 9, uma letra minúscula ou uma letra maiúscula, caso i = 0, parada recebe 0, senão, eu simplesmente guardo a posição de i;
Se eu encontrar um \n eu termino o laço dando um break;

Bom, a função é essa, mas ela está dando um laço, o i trava dependendo da frase, mas não estou entendendo o porque dele estar travando, não estou achando o erro da lógica.
Segue abaixo o código da função:
int contagemFrases(char* p, int r) // Declaração da função que conta o número de frases presentes no texto.
{
    int frases = 0, parada = 0, i;

    for(i = 0; i < r; i = parada + 1)
    {
        if(p[i] == 46 || p[i] == 33 || p[i] ==  63)
        {
            frases++;
            parada = i;
        }
        else if(p[i] == 32)
            parada = i;

        else if (p[i] >= 48 && p[i] <= 57 || p[i] >= 65 && p[i] <= 90 || p[i] >= 97 && p[i] <= 122)
        {
            if(i == 0)
                parada = 0;
            else
                parada = i;
        }
        else if(p[i] == '\n')
            break;
    }

    return frases;
}

Comment: Você que concorda que independente do (i == 0) a `parada` sempre vai receber `i`? Só uma observação, não é o problema
Comment: Mas o for esta percorrendo o vetor, então ele sempre guarda em parada a posição atual não??porque ela sempre esta guardando 1??
Comment: Pra que serve a `parada`?
Comment: Uma dica: o seu comentário do lado da função é redundante. Não precisa dele pra saber que tem uma declaração de função do lado :) Seria melhor fazer um comentário dizendo o que a função *faz*. O que significam os parâmetros de entrada? E o valor de saida? A função tem algum efeito colateral (como imprimir na tela ou mudar o valor de uma variável global)? Repare que falei "o que" e não "como", que é algo que o código C já consegue explicar sozinho. Tenho que confessar que não consegui entender ainda pra que serve a sua função...
Comment: Tem um exemplo de uma chamada de função onde o seu programa trava?
Answer: Eu não entendi muito bem para que serve o parada, mas seu erro está exatamente nessa variável em relação aos if/else.
Fiz uns testes aqui e o problema acontece quando você não cai em nenhum if/else proposto, por exemplo quando a frase contém vírgula, dessa forma você nunca atribui ao parada o novo valor de i.
Minha sugestão seria você trocar
for(i = 0; i < r; i = parada + 1)

Para
for(i = 0; i < r; i++)

Que pra mim é o que faz mais sentido, ou então criar um else para incrementar a parada em último caso, simplesmente assim:
...
else {
    parada++;
}

Problema na função de procurar frases


Eclipse duplication annotation @SurpressWarnings for PMD

Question: I am trying to disable several arbitrary PMD warnings for my class.
How can I list several PMD rules to be ignored? I was not able to find with Google.
@SuppressWarnings("PMD.OnlyOneReturn")
@SuppressWarnings("PMD.ShortVariable")
public class MyClass {

it gives Eclipse compile time error:
Duplicate annotation @SurpressWarnings

This is compilable but ignored
@SuppressWarnings("PMD.OnlyOneReturn, PMD.ShortVariable")

This
@SuppressWarnings("PMD.OnlyOneReturn", "PMD.ShortVariable")

result is
Syntax error on token ,

Eclipse is configured to accept PMD type:
Unsupported @SuppressWarnings ( "PMD.DoNotCallSystemExit" )
Answer: You have to list them in an array.
Like this:
@SuppressWarnings({
    "PMD.OnlyOneReturn",
    "PMD.ShortVariable"  })

Answer: Found just now in Annotation Type SuppressWarnings.
This seems to work, note { and }, since it is String[]:
@SuppressWarnings({"PMD.OnlyOneReturn", "PMD.ShortVariable"})


I can't Create React App using 'npx create-react-app'

Question: I am trying to use the npx create-react app, but I have errors that are shown below:
npm ERR! code ENOENT
npm ERR! syscall lstat
npm ERR! path C:\Users\Administrator
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, lstat 'C:\Users\Administrator'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in: C:\Users\rtyuiop\AppData\Local\npm-cache\_logs\2023-10-10T23_20_24_197Z-debug-0.log

And the log file of the error is here...!
0 verbose cli C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
1 info using email@example.com
2 info using firstname.lastname@example.com
3 timing npm:load:whichnode Completed in 3ms
4 timing config:load:defaults Completed in 5ms
5 timing config:load:file:C:\Program Files\nodejs\node_modules\npm\npmrc Completed in 8ms
6 timing config:load:builtin Completed in 8ms
7 timing config:load:cli Completed in 2ms
8 timing config:load:env Completed in 1ms
9 timing config:load:file:D:\DEVOWFS\M204\TPs\.npmrc Completed in 0ms
10 timing config:load:project Completed in 2ms
11 timing config:load:file:C:\Users\rtyuiop\.npmrc Completed in 2ms
12 timing config:load:user Completed in 2ms
13 timing config:load:file:C:\Users\Administrator\AppData\Roaming\npm\etc\npmrc Completed in 1ms
14 timing config:load:global Completed in 1ms
15 timing config:load:setEnvs Completed in 1ms
16 timing config:load Completed in 23ms
17 timing npm:load:configload Completed in 23ms
18 timing config:load:flatten Completed in 5ms
19 timing npm:load:mkdirpcache Completed in 0ms
20 timing npm:load:mkdirplogs Completed in 0ms
21 verbose title npm exec create-react-app JeuDe
22 verbose argv "exec" "--" "create-react-app" "JeuDe"
23 timing npm:load:setTitle Completed in 2ms
24 timing npm:load:display Completed in 0ms
25 verbose logfile logs-max:10 dir:C:\Users\rtyuiop\AppData\Local\npm-cache\_logs\2023-10-10T23_20_24_197Z-
26 verbose logfile C:\Users\rtyuiop\AppData\Local\npm-cache\_logs\2023-10-10T23_20_24_197Z-debug-0.log
27 timing npm:load:logFile Completed in 16ms
28 timing npm:load:timers Completed in 0ms
29 timing npm:load:configScope Completed in 0ms
30 timing npm:load Completed in 99ms
31 timing arborist:ctor Completed in 2ms
32 silly logfile start cleaning logs, removing 2 files
33 silly logfile done cleaning log files
34 http fetch GET 200 https://registry.npmjs.org/create-react-app 1260ms (cache revalidated)
35 timing arborist:ctor Completed in 0ms
36 timing command:exec Completed in 1305ms
37 verbose stack Error: ENOENT: no such file or directory, lstat 'C:\Users\Administrator'
38 verbose cwd D:\DEVOWFS\M204\TPs
39 verbose Windows_NT 10.0.19045
40 verbose node v18.18.0
41 verbose npm  v9.8.1
42 error code ENOENT
43 error syscall lstat
44 error path C:\Users\Administrator
45 error errno -4058
46 error enoent ENOENT: no such file or directory, lstat 'C:\Users\Administrator'
47 error enoent This is related to npm not being able to find a file.
47 error enoent
48 verbose exit -4058
49 timing npm Completed in 2157ms
50 verbose code -4058
51 error A complete log of this run can be found in: C:\Users\rtyuiop\AppData\Local\npm-cache\_logs\2023-10-10T23_20_24_197Z-debug-0.log

How can I resolve this issue, knowing that I used
npm cache clean --force, npm install -g create-react-app@latest, npx create-react-app project_name --template all, npm init,
npm install create-react-app, npx create-react-app myapp, npm install -g create-react-app
but without success.
Comment: https://dev.to/ag2byte/create-react-app-is-officially-dead-h7o
Answer: You can do this:
npm i create-react-app@latest

then:
npx create-react-app my-app

Answer: try First:
npm cache clear --force

then:
npm install -g create-react-app

last:
npx create-react-app [name_app]


Displaying Webforms on Content Types in Page Manager/Panels

Question: The Webform module allows for a webform to be attached to each Content Type of the site.  I can "Enable webform functionality" on my Page and Blog Content types and don't need to use a separate "webform" content type, very cool!  This works great with Entity View Mode displays.

However, when using the Node Template in Page Manager, I can't get the Webform to render after adding the "Node being viewed" Webform element.

My goal is to use Page Manager to display every node, with the possibility of a unique webform attached to that node.
I brought this up at a meetup recently and was advised that it may be a bug.  But I didn't think to ask where!  So, if you think I should bring this up in an issue queue, where do you think that would be, Webform, ctools, or Panels?
Thanks,
Jim
Answer: Have you tried the following way:
Comment: Hi Sumit,  Yes.  That is exactly what I have tried.
Comment: Oh!! Its working fine for me. Can't say what is the issue for you.


Can it be wrong to use a series resistor in a 2x12 cabinet to have matching impedances?

Question: I made a wood frame for 2 speakers, intending to create my own 2x12 cabinet, but now i realised my speakers are 8Ohm and 4Ohm, they don't match, meaning i would get a weird value when matching them.
I thought about a series resistor with the 4Ohm to make it 8Ohm, then i can either wire them in series to make 16Ohm or parallel to make 4 Ohm.
What could possibly go wrong with this?

I know it must be a power resistor, i have one rated 200W (way more than any amp i will conenct there)
I am aware that the speaker connected to the resistor will probably have a volume loss compared to the other one.

What I'm asking is, can it damage the amp?
Thank you
Answer: First of all, if you have a choice (and why wouldn't you?), avoid an impedance mismatch. So my advice would be to get one more speaker, either 4 or 8 Ohms to get an impedance which you can match. Then you don't need to worry, and if your amp ever dies on you, you won't have that nagging feeling that you should have known better ...
Having said that, and if you really want to use those two speakers, then
I wouldn't use a resistor, because it's a shame to use amp power for heating your cabinet. I'd say it's probably most reasonable to connect the two speakers in series, which gives you 8+4 = 12 Ohms. Then you can either choose the 8 Ohms setting on your amp, or use the 16 Ohms setting, because almost all amps can handle a slight impedance mismatch. It depends on whether you have a tube or a solid state amp, and if you have a tube amp, it also depends on the model how sensitive it is to an impedance mismatch.
If you connect the speakers in parallel you'll get a resistance of 8*4/(8+4) = 2.67 Ohms, which is quite small, and which I wouldn't use, unless your amp has a 2 Ohms setting (I think I've seen this with some Mesa Boogies).
As a final note, it is important to realize that a speaker doesn't have a constant impedance anyway. If it is rated at 8 Ohms, then you will find frequencies at which its impedance is a bit smaller, and you'll find a wide range of frequencies where the impedance is considerably higher. That knowledge could be a motivation for not getting too neurotic about a small (!) mismatch.
Comment: Alright, that does it for me. I recognise that the inductive load that is the speaker is completely different from the resistive load that is the resistor. I just wanted to use what i have, but I guess I'll just get another speaker. The amp is an egnater tweaker 40. All tube, i reckon they are more sensitive and so this slight impedance mismatch can mean trouble, I won't risk it anyway, thank you
Comment: @AnteroDuarte: I think that's a good decision!
Answer: Don't.  "Impedance" means that for any sinoid voltage applied to the terminals, the effective current will not exceed the one derived by Ohm's law.  But it does not make any claim that the impedance will be constant over frequency, or that the phase of the current will be the same as that of the voltage.
Amplifier standardize on the output voltage: how much current a speaker draws is its own choice.
As a consequence, the only thing you put in series with a speaker is another speaker of the same type.  Or an audio crossover designed to work with this kind of speaker.  Anything else, even a different speaker with the same impedance rating, is asking for trouble.
As an extreme example, piezo tweeters are a capacitive load designed to be hooked up directly with the voltage source.  It is a pretty common mistake to hook them up with a branch of a frequency crossover designed for inductive tweeters.  You'd think you'd be doing them a favor by giving them less range to work with (inductive tweeters will die pretty soon if you don't spare them the low frequencies), but you actually build a resonant circuit that way that excels at letting the actually rather robust piezo tweeters go up in smoke.
Of course, things can work even putting different stuff in series.  But they are not likely to continue working when driven at the max of the spec, and the results may be worse than you thought for no discernible reason, both regarding frequency response and distortion as well as maximum specs.
Comment: I especially agree with the part about two radically different speakers driven in series will produce unpredictable sound.


Why is $\sigma(X_i) = \{ (X_i \in A) \ | \ A \in \mathbb{B}\}$

Question: Why is $$\sigma(X) = \{ (X \in A) \ | \ A \in \mathbb{B}\}$$
where $X$ is a random variable.
Shouldn't it be $$\sigma(X) = \sigma(\{ (X \in A) \ | \ A \in \mathbb{B}\} )$$
or is $\{ (X \in A) \ | \ A \in \mathbb{B}\}$ already a $\sigma$-algebra?
Comment: Indeed, $\{ (X \in A) \ | \ A \in \mathbb{B}\}$ is already a $\sigma$-algebra.
Answer: $\{(X\in A) | A\in \mathbb{B} \}$ is already a $\sigma$-algebra.
It is clear that it is the smallest such $\sigma$-algebra because it only contains the pre-images of $A\in \mathbb{B}$, which are necessary for X to be measurable.


django redis with django showing ulimit error

Question: Im trying to use redis on my django project and when i try to run thr server using 'redis-server' on the terminal i get the following errors
<code>2280:C 23 Nov 15:19:57.938 # oO0OoO0OoO0Oo Redis is starting
oO0OoO0OoO0Oo
2280:C 23 Nov 15:19:57.938 # Redis version=4.0.9, bits=64,
commit=00000000, modified=0, pid=2280, just started
2280:C 23 Nov 15:19:57.938 # Warning: no config file specified, using
the default config. In order to specify a config file use redis-server
/path/to/redis.conf
2280:M 23 Nov 15:19:57.939 # You requested maxclients of 10000
requiring at least 10032 max file descriptors.
2280:M 23 Nov 15:19:57.939 # Server can't set maximum open files to
10032 because of OS error: Operation not permitted.
2280:M 23 Nov 15:19:57.939 # Current maximum open files is 4096.
maxclients has been reduced to 4064 to compensate for low ulimit. If
you need higher maxclients increase 'ulimit -n'.
2280:M 23 Nov 15:19:57.939 # Creating Server TCP listening socket
*:6379: bind: Address already in use
</code>
I have tried all solutions including setting the file max size etc ,still error persists.Im using ubuntu 18.04 .Any other solutions?
Comment: Not a programming problem. Most likely just an other process listening on port 6379 or Redis is still running.
Comment: how to kill it? @KlausD.
Comment: This was when trying to start for the first time,the server was never run successfully  before @KlausD.
Comment: solved it @KlausD.
Answer: An instance of redis was already running! Killed it using :
<code>$ /etc/init.d/redis-server stop
</code>


create-react-app max_old_space_size not working

Question: Facing an issue while build or run my project with create-react-app. It throws js heap out of memory error.
> yarn build
yarn run v1.6.0
$ react-scripts build
Creating an optimized production build...

<--- Last few GCs --->

[8792:0000021D06B28B70]  1492086 ms: Mark-sweep 1401.7 (1716.2) -> 1401.7 (1716.2) MB, 1474.4 / 0.0 ms  allocation failure GC in old space requested
[8792:0000021D06B28B70]  1493542 ms: Mark-sweep 1401.7 (1716.2) -> 1401.5 (1658.7) MB, 1455.7 / 0.0 ms  last resort GC in old space requested
[8792:0000021D06B28B70]  1494983 ms: Mark-sweep 1401.5 (1658.7) -> 1401.5 (1636.7) MB, 1439.7 / 0.0 ms  last resort GC in old space requested

<--- JS stacktrace --->

After searching on internet, i found a few suggestions and have added the following lines to my package start and build lines.
"start": "node --max_old_space_size=4096 node_modules/.bin/react-scripts start",
"build": "node --max_old_space_size=4096 node_modules/.bin/react-scripts build"

Now when i run "yarn start" or "yarn build" i get the following error.
> yarn build
yarn run v1.6.0
$ node --max_old_space_size=4096 node_modules/.bin/react-scripts build
D:\My-App\node_modules\.bin\react-scripts:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
error Command failed with exit code 1.

Also I've removed my nodes_modules folder and reinstalled it again. But still the issue exists.
My development environment:
OS: Windows 10 Pro | Node version: v8.9.3 | npm version : v6.1.0 | yarn version: v1.6.0 | react-scripts version: 1.1.4
Comment: Since this is left un-answered for a long time therefore answering my own question.

Quick answer is `react-scripts --max_old_space_size=4096 start`

This issue was addressed in github issues of create react app [here][1].

  [1]: https://github.com/facebook/create-react-app/issues/4536
Answer: Just copy paste from the comments. This question deserves an answer, even if that answer is already there in the comments section with small letters.
The solution: react-scripts --max_old_space_size=4096 start
This issue was addressed in github issues of create react app here.
Credits to @bytecode


Comparing retention rates with differing sample sizes

Question: I'm looking to compare the retention rate of people who attended an event and those who did not. The list of those who did not (70) is significantly larger than those who have (28).
Here is an example of the data I'm looking to compare:
In April 7 people left - of those 3 attended the event and 4 did not.
I've calculated the retention rate of attended as (28-3)/28 = 89.29%
I've calculated the retention rate of non-attendees as (70-4)/70 = 94.29%
Due to the sample size, I don't think it's a proper comparison but I need a way to compare them.
Answer: Since you are comparing categorical variables (stayed or left) on two groups, you can use a Chi-square test of independence, which does not require equal sample sizes.
http://stattrek.com/chi-square-test/independence.aspx?tutorial=ap
Answer: I would comment but it seems I need reputation to do so.
What you are looking for is a test for equal proportions. Sample sizes need not be equal.
This webpage will be of assistance:
https://web.archive.org/web/20150216093850/https://onlinecourses.science.psu.edu/stat414/node/268
Edit: I am assuming you know how to use a test statistic once you have obtained it. Let me know if this is not the case.
Comment: I'm afraid im not quite understanding how this would work given the example.
Comment: Ok. I need some idea of your background in statistics. Is this homework for an introductory class, or have you taken/are taking one?


find $\int^6_0f(x)dx$ by definition , $f(x)=\left\lfloor\frac{x}{3}\right\rfloor$

Question:
Let $f(x)=\lfloor\frac{x}{3}\rfloor$,
Is $f(x)$ integrable at $[0,6]$?
If so, find $\int^6_0f(x)dx$ by definition.

The function is integratable because it is bounded and it has a finite number of points which are not continuous in $[0,6]$.
So first we need to divide the interval into even parts, I did that by $\frac{6-0}{n}=\frac{6}{n}=\Delta x$.
And then choose the integration points: let $x_k^* \in [0,6]$ and the points are $x_k^*=1+\frac{5n-1}{n}$.
Then calculate the summation:
$$S_n=\sum^n_{k=1}f(x_k^*)\cdot \Delta x = \sum^n_{k=1} \left\lfloor\frac{x_k^* }{3}\right\rfloor \cdot \frac{6}{n} = \sum^n_{k=1} \left\lfloor\frac{6-\frac{1}{n}}{3}\right\rfloor \cdot \frac{6}{n} = \sum^n_{k=1} \left\lfloor\frac{6n-1 }{3n}\right\rfloor \cdot \frac{6}{n}  $$
Now if I calculate the limit of $S_N$ as $n$ approaches infinity I get that the answer is $0$ which is wrong according to the book it is supposed to be $3$ but it only provides the final answer.
Can anyone give me any tips and hints on that? I find this topic complicated.
Hopefully my translations are understandable. Thank you.
EDIT - my answer is also wrong even if I use $\Delta x=\frac{b-a}{n}$ and $x^*=a+ \Delta x \cdot k$
Comment: $f(x)=\lfloor\frac{x}{3}\rfloor$ is discontinuous at multiples of $3$ such as $0,3,6$ so why not split the integral into the piecewise continuous parts and then add the (two) results together?
Comment: Your definition of $x^{*}_k$ is different to what you are using in the $S_n$. You are also summing over $k$, but $k$ is no where to be found in your sum. Are these typos?
Comment: @Henry so make a piecewise function where we assume the point $t$ is one time $0 \leq t <3$ , $3 \leq t <6$ and for the last $t=6$?
Comment: @NiveaNutella why different ? $6-\frac{1}{n}$=$1+\frac{5n-1}{n}$
Comment: @Adamrk Yes - and $\int^6_6 \cdots dx =0$
Answer: by your definition $\Delta x = \frac{b-a}{n} = \frac{6}{n}$, so $x_k = \frac{k \Delta x}{n}$. $k$ starts from $0$ to $n$. so the sum would be:
$$
S_n = \sum_{k=0}^n f(x_k)\Delta x = \sum_{k=0}^n \left\lfloor \frac{6k}{3n} \right\rfloor \frac{6}{n} = \sum_{k=0}^n \left\lfloor \frac{2k}{n} \right\rfloor \frac{6}{n}
$$
$2k/n$ is less than $1$ if $k<\lceil n/2 \rceil$, so $\lfloor \frac{2k}{n} \rfloor$ is zero. only nonzero elements are for $  \lceil n/2 \rceil \leq k \leq n$ and $\left\lfloor \frac{2k}{n} \right\rfloor$ is just 1. so the summation becomes :
$$
S_n = \sum_{k=\lceil n/2 \rceil}^n  \frac{6}{n} = \frac{6}{n} \sum_{k=\lceil n/2 \rceil}^n1 = \frac{6}{n} ( n - \left\lceil \frac{n}{2} \right\rceil).
$$
if you take limit for $n \rightarrow \infty$ , you get:
$$
\lim_{n \rightarrow \infty}S_n = \frac{6}{n} \left(\frac{n}{2}\right) = 3.
$$
you can find this value easily by linearity of integral:
$$
\int_0^6 \left\lfloor \frac{x}{3} \right\rfloor dx = \int_0^3 0 dx + \int_3^6 1 dx =\int_3^6 dx = 6-3 = 3.
$$
you didn't use correct formula for $x_k$ because there is no $k$ in your formula and hence in your sum!
Comment: Your welcome. $\left \lfloor \frac{x}{3} \right \rfloor$ for $x \in [0,6]$ is a piecewise constant function. its value is $0$ if $x \in [0,3)$. it is 1 if $x \in [3,6)$. and it is 2 for $x=6$. so yes it has three possible values but its value is $2$ for just one point. the value of one point doesn't affect value of integral. so what i did was actually dividing the sum for two values $0$ and $1$. and you need to just compute the sum for intreval that $f(x)$ is 1, that is $[3,6)$, or in summation it is $k=\{\lceil n/2 \rceil, \lceil n/2 \rceil + 1, \dots , n\}$.
Comment: yes as i said the value in $k=n$ is 2. but that's just 1 points in $\mathbb{R}$ if you take limit for $n \rightarrow \infty$ its value doesn't matter. you can also take $k \in \{ \lceil n/2 \rceil, \dots,n-1\}$ . the value of $ (n-1 - \lceil n/2 \rceil)/n$  is equal to $1/2$ in limit and you get same result.
Comment: Thank you for the answer! , can you please tell me why did you check when $\frac{2k}{n}$ is less than 1? as a user here stated we have 3 options because of the floor value and the possible values of the function are $0,1,2$ what about those values?
Comment: Thank you so much , but I noticed something you said  $  \lceil n/2 \rceil \leq k \leq n$ and $\left\lfloor \frac{2k}{n} \right\rfloor$ is just 1 but what if $k=n$? then the value is $2$ maybe you meant $<$ and not $\leq$?
Comment: Thank you! all clear now


LSE vs MLE - why use LSE when we have MLE?

Question: I know that with linear regressions Least Squares Estimate (LSE) and Maximum Likelihood Estimate (MLE) have differences, in that
(1) MLE does not use unbiased standard deviation(the denominator is n)
(2) While LSE requires homogenous E(residuals|X) & V(residuals|X), MLE does not,
(3) LSE and MLE get identical when the residuals of LSE follow N(0, sigma^2).
Considering that LSE requires further assumptions[(3)] to become MLE, it seems that MLE has more beneficial characteristics of being a statistical estimator.
My question is, then, Why do we have to use LSE when we have MLE, since the computation on both coefficients doesn't seem to differ in their difficulty?
Some colleagues of mine told me that deciding which to use differs by field and interests, but it seems to me that MLE serves better in nearly every aspect and at the same time computing MLE doesn't seem to be much more difficult. So, Why not MLE for every case rather than LSE?
Comment: For linear regression, the result is the same.
Answer: As an overarching framework, indeed MLE is much better.  It shows when you really want to use LSE, and when you want to use other estimators, such as LAE, Poisson regression, logistic regression, WLS, GLS, etc. It also leads naturally and seamlessly to Bayesian methods. All quantitative disciplines would be much better off to replace LSE with MLE.
These days, there is not much to motivate LSE other than "we've always done it this way." Back in the 19th century, computational considerations were important and justified LSE, but certainly not any more. Some like to appeal to the Gauss-Markov Theorem to justify LSE, but that theorem is nearly vacuous. It states that LSE is best among estimators that are unbiased and linear functions of the data, but since the class of linear, unbiased estimators is so limited, that theorem is akin to stating that LSE is the biggest fish in a tiny puddle.


Can ML or DL model automatically pick up "difference" feature?

Question: ML requires manual feature extraction whereas DL doesn't necessarily require feature engineering, since recent advanced models like transformers learn necessary features automatically during training - even the sequence of data.
However, I'm still not sure if it means that the model can automatically pick up the features that help understand the data pattern without any preprocessing whatsoever.
Difference is something very simple yet important when you do any sort of feature engineering. For example you have to take difference of price to get the price change, and you have to take difference of timestamps to get the duration of some logs.
This is very simple:

However, can ML models and DL models automatically "use" this feature without explicitly given?
Say I have some columns like timestamp and price.
Is it absolutely pointless to add duration or price_%change?
There are many ML and DL models but I want to know if there are models that are robust to these feature selections so that the model renders feature engineering useless.
Answer: It is just a linear function with parameters $\beta_1 = 1$ and $\beta_2 = -1$
$$
\beta_1 x_1 + \beta_2 x_2 = 1 \times x_1 + (-1) \times x_2 = x_1 - x_2
$$
Neural networks use such linear functions commonly. If you have a simple multilayer feed-forward network, just one of the layers would need to learn the parameters above to calculate the new feature to be used by the next layer. Of course, the network wouldn't learn exactly this, as it is unlikely the learned weights would be exactly $1$ and $-1$ and the result would be passed through the non-linear activation function, but it will be able to learn about the kind of relationship you are describing and use it to learn higher-level patterns.
That said, there are cases where engineering the features by hand makes life easier for the algorithm so it does not need to figure them out.


How to Call A Method after certain time has passed in C#

Question: I'm trying to call an method in this if statement after a certain time (15 seconds) has ended, but it doesn't seem to work at all not print out the text in the method. How would I go about doing this, I have tried researching on how the stopwatch works but it doesn't seem to be working. Thank your for your help.
I have tried this code
    Stopwatch watch = Stopwatch.StartNew();

    if (watch.Elapsed.TotalMilliseconds == 15)
    {
       Method();
    }

Comment: This appears to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Tell us why (you think) you need this and what is the ultimate goal you are trying to achieve?
Comment: A stopwatch is what is is- it stops time. It is not executing code magically. your watch.Elapsed.TotalMilliseconds is probably at 0.000001 or so - not 15. You might want to use Thread.Sleep() if you want to force your program to sleep a bit, else you need to look here: [Timer with callback](https://msdn.microsoft.com/en-us/library/ms149618(v=vs.110).aspx). It has some example at the bottom.
Answer: Task is what you need
Task.Delay(new TimeSpan(0, 0, 15)).ContinueWith(o => { Method(); });

More about Asynchronous programming here
Comment: This is what I've been looking for. The most elegant solution there.
Answer: What you are trying to do is to halt your execution for a short while, and then invoke the method.
The most trivial way to do so is:
Thread.Sleep(TimeSpan.FromSeconds(15));
Method();

But note that your thread is not doing anything during this time, so a better way would be to use the TPL, and async methods, but for your needs the code above will probably do :)


insert data from html table to mysql using php

Question: I am making a php application for saving data, it is using table inside form, I want to save data from table to mysql using PHP.
Parse html table using file_get_contents to php array
I am coming through this question but in this the DOM loading another file, but my table is in the same file.
can any one suggest me how can I do this?
Answer: There are 2 ways I can think of doing this:
1 - You could define the table as a variable, and echo it, inside a PHP block, so that you have the data for parsing:
<?php
// define the table with heredoc
$data = <<<HTML

        ...

HTML;

// print the table
echo $data;

// follow the instructions from your link here
parseTable($data);

2 - You could also grab the data using an output buffer, :
<?php
// start a buffer
ob_start();
?>

        ...

<?php
// get the table in a variable
$data = ob_get_contents();

// flush the buffer to output
ob_end_flush();

// follow the instructions from your link here
parseTable($data);


How do I keep alive a session on betfair in nodejs

Question: I've wrote a nodejs app for betfair but my session id is expiring and I can't figure out how to write the keep alive code. Here is documentation for it: https://api.developer.betfair.com/services/webapps/docs/display/1smk3cen4v3lu3yomq5qye0ni/Keep+Alive
Comment: Please share details about the error you receive and some code that you wrote.
Answer: I've found a solution. I converted the curl command on this site: http://curl.trillworks.com/#node.
This is my code:
<code>var request = require('request');

var appkey = xxxx;
var ssid = xxxx;

function keepAlive() {
    var options = {
        url: 'https://identitysso.betfair.com/api/keepAlive',
        headers: {
            'Accept': 'application/json',
            'X-Application': appkey,
            'X-Authentication': ssid
        }
    };

    function callback(error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
    }

    request(options, callback);
}
</code>


Domain Server through windows 7?

Question: is it possible to run a AD DS through a regular windows 7 workstation?
Answer: In a word, no.
Windows 7 does not come with the required libraries and services needed to deploy AD. Even if it did, it's a workstation operating system and should not be used to provide services to other computers.

Domain Server through windows 7?


With states, how to uncheck a disabled checkbox, but not check it when it becames enabled?

Question: I have a Form API checkbox that can be enabled and disabled by another ("parent") one:
<code>$form['my_checkbox']['#states'] = array(
  'enabled' => array(
    ':input[identifier="parent"]' => array('checked' => TRUE),
  ),
);
</code>
Works like a charm, but now I want it to get cleared if it is dizabled. So I made my code look like this:
<code>$form['my_checkbox']['#states'] = array(
  'enabled' => array(
    '[:input[identifier="parent"]' => array('checked' => TRUE),
  ),
  'unchecked' => array(
    ':input[identifier="self"]' => array('disabled' => TRUE),
  ),
);
</code>
Sadly, this also automatically checks that checkbox when it becomes enabled, and that's not what I want - I want to give my user an option, but not suggest him to check that field at all.
Can this "one way" action be achieved with <code>'#states'</code>? If so, how? Or is it always two way deal, and there is no option to uncheck on disabling without checking on enabling?
Note: I tried to use both disabled and enabled of self, and checked & unchecked of parent, with the same results.
I know I can achieve it with behavior, that's how I have it done now, but states api is there to avoid behavior's complexity and my use case seems pretty simple.
Comment: Legal reason: it is illegal in some jurisdictions to auto-check "I agree to ... " checkboxes for users. Not to mention it might be immoral to do so. And for clarity if someone does not agree to give me any data, I don't want to make it look like there is still checked some extra use of it.
Answer: Turns out that with current implementation of states it is not possible. When condition gets true, state is applied, and when it gets false, state gets removed. No way around it so far, not without hacking core, so it's more feasible to write custom behaviors instead.
Comment: If anyone wants to provide a better answer, or more elaborated answer saying essentially the same, and get a hat for stealing checkmark, feel free and encouraged to do it! Promise not to keep it for myself.
Answer: it's possible create states with unchecked.
See https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_states/7
[]'s
Comment: Sorry, but I already know this, and I don't see how this link can answer my question. Also there is not too much text to it to explain what you meant. Are you sure you're answering the specific case I asked about?


Problem embedding user_register_form in custom template

Question: I need to embed the user_register_form inside a template.
In my module i do:
....
$form=drupal_get_form("user_register_form");
unset($form["field_nickname"]);
unset($form["locale"]);
$output=theme("agent_new",array("element"=>array("form"=>$form, "var1"=>$var1, "var2"=>$var2)));

I specify "agent_new" in hook_theme and in agent_new.tpl.php:

   <?php print drupal_render($element["form"]["field_first_name"]); ?>

   <?php print drupal_render($element["form"]["field_surname"]); ?>

...
<?php print drupal_render_children($element["form"]); ?>

The problem is that  the <form> tag is not rendered and i need to insert it manually, i think this is not a good thing.
Where is the mistake? Do i need to directly theme the user_register_form ? Is it not possible to embed it in my theme?
Answer: You can try something like this:
In your .module file:
<?php

/**
 * Implements hook_form_alter().
 */
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
  // User Register
  if ($form_id == 'user_register_form') {
    unset($form["field_nickname"]);
    unset($form["locale"]);
  }
}

In your .tpl.php file:
<?php
if (!$logged_in):
  $elements = drupal_get_form('user_login_block');
  $rendered = drupal_render($elements);
  $output  = '<form action="' . $elements['#action'] . '" method="' . $elements['#method'] . '" id="' . $elements['#id'] . '" accept-charset="UTF-8">';
  // Your required fields here!
  $output .= $elements['name']['#children'];
  $output .= $elements['pass']['#children'];
  $output .= $elements['form_build_id']['#children'];
  $output .= $elements['form_id']['#children'];
  $output .= $elements['actions']['#children'];
  $output .= $elements['links']['#children'];
  $output .= '</form>';
  print $output;
endif;
?>


Why Rolle's theorem gives me wrong answer?

Question:
Find number of zeroes of $f(x) = 1 - x^{-2}$.

I assume that this function has two or more zeroes in the domain $ \mathbb{R} - \{0\}$.
Since $f^\prime (x) = \large{2\over x^3}$, therefore we can say      $f^\prime(x) \ne 0$ for all $x \in   \mathbb{R} - \{0\}$.
Therefore by Rolle's theorem we can say that our assumption is wrong (because if it was correct then for any two zeroes $a,b\in\mathbb{R} - \{0\}$, $f^\prime(c) = 0$ where $c \in [a, b]$)  and  $f(x)$ has one zero at most in $\mathbb{R} - \{0\}$.
What we deduced is incorrect given the fact that $f(\pm 1) = 0$ and $\{\pm 1\} \in \mathbb{R} - \{0\}$.
Here I followed a similar proof.

What is the error in my proof ?
Comment: Look at the conditions on Rolle's Theorem - the function has to be continuous on an interval $[a,b]$ and differentiable on $(a,b)$.
Comment: There could be one zero on each side of 0 (in fact, there is).
Comment: The function is not continuous. '
Comment: The hypothesis of Rolle's Theorem requires a closed interval [a,b] such that f is continuous on [a,b], differentiable on (a,b), and f(a) = f(b) = 0. What's your closed interval [a,b]?
Comment: @lulu The function is even everywhere differentiable (where defined), so it's obviously continuous (where defined). The fact it's not defined at $0$ doesn't make it not continuous.
Comment: @lulu Why it is not continuous ? I choose $\mathbb{R} - \{0\}$ as the domain.
Comment: @A---B: What's your closed interval [a,b]? If you don't have one, forget about using Rolle's Theorem.
Comment: Rolles' Theorem requires continuity on the interval between the zeroes.  Here's a simpler example:  take $g(x)=x+1$ for $x≤0$ and $g(x)=x-1$ for $x>0$.  Here $g'(x)=1$ wherever it is defined, but the function has two zeroes.
Comment: @quasi Take a look at http://math.stackexchange.com/questions/2186743/understanding-rolles-theorem?noredirect=1&lq=1. I took a similar approach.
Comment: @A---B: But I asked for a closed interval [a,b]. Look at the _hypothesis_ of Rolle's Theorem.
Comment: @quasi Look at the third line of the proof. The thing in the brackets. I do know the hypothesis for Rolle's theorem.
Comment: @A---B: If you can't satisfy the hypothesis of Rolle's Theorem, you can't apply the theorem.
Comment: Just to say, the function in that earlier question is $g(x)=x^3+x+1$ which is continuous on the whole of $\mathbb R$, hence if $g(a)=0=g(b)$ we know that $g$ is continuous on $[a,b]$.  That reasoning does not work for the function here, as it is not continuous at $0$.  You can apply your argument to show that $f(x)$ can't have two roots which are both greater than $0$ or both less than $0$.
Comment: @A---B: Show me the closed interval [a,b] for which the hypothesis of Rolle's Theorem is satisfied. If you can't, then that dramatizes your error -- you tried to apply a theorem without checking that the hypothesis was satisfied.
Comment: @lulu The function should be continous on its domain, right ? I took the domain as $\mathbb{R} - \{0\}$, and $\mathbb{R} - \{0\}$ does not have any $0$ to check continuity for.
Comment: @A---B: Sure, f is continuous (and differentiable) on its domain, but still, you don't have a closed interval [a,b] on which the hypothesis of Rolle's Theorem is satisfied. There's no getting away from that logical gap. Note: By definition, a closed interval [a,b] is an interval of $\mathbb{R}$.
Comment: Look up the statement of [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem).  If $f(a)=0=f(b)$ AND $f(x)$ is continuous on the closed interval $[a,b]$ and differentiable on the open interval $(a,b)$, then we can find $c\in (a,b)$ with $f'(c)=0$.  Your function is simply not continuous on $[-1,1]$.
Comment: @quasi Ok I get it, my proof is flawed because my function is not continous on $[-1, 1]$ where it has its zeroes. I still did not get why my function should be continous on a clsosed interval of $\mathbb{R}$ even if it is not defined there.
Comment: Because the hypothesis requires it, it's that simple. In particular, if $f$ is required to be continuous on $[a,b]$, it must be defined on _all_ points of $[a,b]$, where by $[a,b]$, we mean _all_ _real_ _numbers_ between $a$ and $b$ inclusive, not just those in the domain of $f$.
Comment: Discontinuities matter!   Go back to my (simpler) example of $g(x)$ which has derivative $1$ but which is discontinuous at $0$. The function is locally monotone increasing....meaning $b>a\implies g(b)>g(a)$ PROVIDED $g(x)$ is continuous on $[a,b]$.  The discontinuity at $0$ messes all that up, so there is no problem having $g(1)=0=g(-1)$.
Comment: @lulu, Quasi Yes, thanks for the answer it is a big relief to know my mistake. If you have time and you don't mind can you write an answer.
Comment: I'll defer to lulu on this -- lulu's comment "Look up the statement of Rolle's Theorem ..." is pretty much a complete answer.
Comment: @quasi Yes it pretty much is. Calculus is much more harder than I thought, it is easy to mess up completely on small things. :(
Answer: Rolle's theorem works on intervals, while $\mathbb R \setminus \{0\}$ is not an interval.
Comment: @A--B This answer addresses your question perfectly. The moment you say "by Rolle's theorem", you are assuming $\mathbb{R} \backslash \{0\}$ satisfy the hypotheses of Rolle's theorem (and therefore is an interval).
Comment: A---B: lulu already explained that -- check back in the comments of the main question (the comment which mentions $x^3 + x + 1$).
Comment: I don't understand upvotes on this answer. I never claimed $\mathbb{R} -\{0\}$ to be an interval, it is not even part of my proof to claim $\mathbb{R} -\{0\}$ as an interval.I think this answer does not answer my question, but what does I know.
Comment: Lulu and Quasi answered my question properly. though I did not downvote this answer and please don't take my comment as a offence to the answerer.


How to orchestrate well?

Question: I am starting a concerto, and one of the problems is about the orchestration. I have not studied orchestration, do not have an orchestration book, or any orchestral knowledge from a theoretical standpoint. I am not asking for a full book, only the basics of orchestration, as in, what do you want to aim for, what things should you avoid like the black plague, etc, and how do you balance between tuttis, only strings, only winds, etc,(in my case, tutti, only piano, only orchestra, and specific combinations). What are the absolute basics of orchestration?
Comment: I'm voting to close this question because any answers would need to be far too long to fit here.
Comment: The absolute basics are still too much for an SE answer I think. But there's a book that's public domain and available free online written by Rimsky-Korsakov on orchestration.
Comment: What you're asking is not unlike "I'm starting a marathon competition, how do I run?". With all due respect, I wouldn't even begin to think "I am starting a concerto" considering what is being asked. Even before that, I'd ask myself something like "Do I have enough knowledge and experience to begin *to think* I could be able to write a concerto?". That said, nobody says you shouldn't do it. But consider that even not "famed" composers began to write concertos after *years* (sometimes decades) of composition and orchestration. Rimsky-Korsakov is good advice, then keep studying and practicing.
Comment: @Gregory Don't be confused by published Opus numbers and actual studying experience. Rachmaninoff composed that first concerto after at least 8 years of conservatory (obviously including theory and harmony), of which at least 3 spent studying counterpoint and composition (which doesn't mean he didn't begin studying those subjects even before on his own). He obviously wrote a lot of piano, chamber and orchestral music before that point, just like any composer; the fact that all those works are not published (or are not catalogued *before* the concerto) is completely irrelevant.
Comment: @ToddWilcox I agree. I just wanted to point out that "Op. 1" does **not** mean "my first [orchestral] composition without any experience".
Comment: Seems you're trying to run marathons when all you've tried is walking downstairs! If you need to ask a question such as this, it's telling that you're nowhere near ready, and need to cut your teeth on far more basic music - maybe for several years. Sorry!
Comment: @musicamante Rachmaninoff was a,"famed" composer, and he wrote his first concerto as his opus 1, before any solo piano or chamber music. I know I am not Rachmaninoff(not anywhere near) but that means that it can be done, even without any experience.
Comment: @Gregory Very true. It's just a bit much to enumerate all of that extremely broad information on orchestration as one answer - people have written entire books on that subject, it wouldn't be possible to answer summarily. Do you have specific concerns about some part of orchestration? You could break this up into multiple questions...
Comment: There's not really a link between what Rachmaninov did and what is on-topic here at Music.SE.
Comment: @Gregory There are a lot of voices here rightly pointing out that a concerto is a rather ambitious "first step" without having orchestration knowledge. I just wanted to offer a word of encouragement that you shouldn't necessarily abandon it. As G.K. Chesterton said, "Anything worth doing is worth doing badly," and you can learn a lot from mistakes. Also, many major Hollywood soundtracks are created by one person hammering out the basics at the piano, and someone else doing the labor of orchestration. By all means, jot down your ideas. Then, maybe find someone knowledgeable to...
Comment: ... to do the orchestration and explain to you why they make the choices they do, or even to walk you through doing it yourself.
Answer: You need to study existing scores, and get a library of remembered sounds stored in your brain.  You have to know what an oboe and a clarinet sound like together, or those with flutes added.  You need to be able to imagine how bright a violin will sound at a certain note, and how loud a timpani sounds in a concert hall (as opposed to a sampled instrument on a DAW).
All instruments have a kind of timbre spectrum-- the quality of sound changes as you vary pitch and volume.
In short if you want to do composition, you need to do the things you haven't done.  Sorry, no shortcuts.
As a beginning orchestrator, if you aren't going through everything ever written by Mozart first, then I'd humbly say you're doing it wrong. :)
Comment: Well, maybe "everything ever written by Mozart" is a bit too much... Especially if we also consider *anything* he wrote, including [**those**](https://en.wikipedia.org/wiki/Mozart_and_scatology) letters he wrote to his cousin Maria... Or maybe not? ;-)
Comment: Tish I'm tea.  :P

How to orchestrate well?


How to detect inside a component if an external ADO Connection has connected or disconnected?

Question: I'm working on a component which works with a database, and the database connection is specified from the outside of this component via a TADOConnection property, visible in the Object Inspector. I assume that the outside user may intend to make use of the events of this ADO component. Yet, I also need to be able to detect when it connects or disconnects, and other various events. Can I get the events of the TADOConnection without overriding event handlers that an outside user may have assigned?
Comment: Interesting factoid:  The ADO connection can be dropped or disconnected sometimes without the TADOConnection.Connected property going false on you, at least for remote MS SQL Server ADO connections.   This happens to me when serious network faults are happening, or the SQL server is thrashing and overloaded.
Comment: By _detect_ you mean _get a notification or event_ on connect/disconnect?
Comment: Yes, as well as all the events, such as `OnCommitTransComplete` - I just don't want to interfere with users' implementation of these events.
Answer: There is no solution for this without heavy side effects and/or strict limitations.
You have to reroute the events, but ...
Outside users can modify events at design time (that could be handled in a way) but also at runtime and every approach will break.
As a (maybe) possible workaround (and it is a limitation) you can publish an own descent from TADOConnection and force outside users to use this, and implement multi cast events.
If you just want to get informed to Connect/Disconnect Events, you may call the protected method TCustomConnection.RegisterClient (unit Data.DB)


Xamarin Android getting image data from Gallery

Question: I am trying select an image from Gallery, which is stored in external. I then catch the data in OnActivityResult. I, then, want to get the byte array data of the selected image using these lines.
protected override async void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    ...
    var imageSelected = File.ReadAllBytes(data.Data.Path);
    ...
}

However, I got this following error:
System.IO.DirectoryNotFoundException: Could not find a part of the path "/external/images/media/149144".
/external/images/media/149144 is the value of data.Data.Path. Also, note that I have added READ_EXTERNAL_STORAGE permission in my androidmanifest
Any help would be greatly appreciated.
Comment: Can this by done with media picker plugin https://blog.xamarin.com/getting-started-with-the-media-plugin-for-xamarin/ ?
Comment: @Milen I have not tried that, but based on my code (using native Android approach), can you tell what I did wrong there?
Answer:
I am trying select an image from Gallery, which is stored in external. I then catch the data in OnActivityResult. I, then, want to get the byte array data of the selected image using these lines.

You are getting the ContentUrl from data.Data.Path, and File.ReadAllBytes can't find a file from a ContentUrl.
If you want to get the byte data from the result, you can create a Bitmap from the ContentUrl and convert the Bitmap directly to byte array like below:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    try
    {
        Bitmap bitmap = MediaStore.Images.Media.GetBitmap(this.ContentResolver, data.Data);
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Compress(Bitmap.CompressFormat.Jpeg,100, stream);
            byte[] array=stream.ToArray();
        }

    }
    catch (Java.IO.IOException e)
    {
           //Exception Handling
    }
}


How to unzip .rar file with Python and patoolib

Question: I need uncompress .rar file in Google Colab with Python3. First I tried to do localy in MacOS.
I have installed Patoolib package:
pip install patool

and unrar to unzip .rar files
brew install unrar

Then, In my python script I do:
import patoolib
patoolib.extract_archive("data_2/Peliculas.rar", outdir="/data_2")

and I get the following error :
PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),

I need to configure Patool to use unrar but there is no documentation available. Somebody knows how to solve this error?
Comment: Try checking this issue: https://github.com/wummel/patool/issues/19 it seems like a similar problem. It's on Windows though, the solution may not apply
Comment: I've seen this issue, no it can't be apply. He uses a .exe. I need the unrar path
Comment: I have never used patool but it looks like it needs some compression program that handles the archive format you need to be installed and in the expected path. Do you have 7zip or unrar installed in the system you are running the script?
Answer: My issue was solved by simply adding my Winrar directory to my Path (in system environment variables). Made the horrible mistake of assuming it was set up by default (because why wouldn't it, it's already on the contextual menus right?), but that was not the case.
Hope this helps whoever reads this


Multiple entity quickedit problem

Question: I am utilising the basic page content type. When viewing my basic page the layout consists of:

A view that outputs a block in the form of a header. Using the basic page fields "title" and "short description"
A block that outputs the body of the page.

When I view my page, the node/articles markup is rendered as follows:

...

...

Analysing the markup we can gather that:

They both have the same node id
The latter can be differentiated by the data-quickedit-entity-instance-id="1" tag
Only the 'quick-edit' button appears for the first article
On quick-edit click, both of the articles are trying to be manipulated by some form of js - there could be conflict here.

So the question is. How come the quick edit feature does not work for a node that is rendered twice on one page?
Has anyone got any reason why it is not working or can provide a solution for this?
Thanks in advance
Answer: According to #15 of this Drupal issue
having a duplicate node on the screen is enough to disable the contextual menu functionality. It's a bug.
Comment: It's a strange one. Just looking at the markup, you'd assume that quick-edit would see multiple instances of the same node. I suppose handling whatever happens after saving the edit to the instance is too advanced at the moment (e.g. updating an image, then having to update every other instance on the same page with that image) It'd be a lot of work.
Comment: I've hit this too, and it was quite confusing until I started rendering just one of multiple rendered instances.

Multiple entity quickedit problem


How to convert json objects with number as field key in Java?

Question: The server I am working with returns an json object which contains a list of objects, not just one.
{
"1":{"id":"1","value":"something"},
"2":{"id":"2","value":"some other thing"}
}

I want to convert this json object into an object array.
I know I can use Gson, and create a class like this:
public class Data {
    int id;
    String value;
}

and then use
Data data = new Gson().fromJson(response, Data.class);

But it's only for the objects inside the json object.
I don't know how to convert json object with number as keys.
Or alternatively I need to alter the server to response to something like this?:
{["id":"1","value":"something"],["id":"2","value":"some other thing"]}

But I don't want to change to server as I have to change all the client side codes.
Comment: That JSON sample looks like a Map. So how about u convert it to a map and then get the values() from that map? It looks not completely right but your Json string is not right I think:)
Comment: That is *not* a "list of objects". It's an object that contains other objects that happen to be using numbers as field names - or basically a `Map` at the most base level.
Answer: Your JSON looks really weird. If you can't change it, you have to deserialize it to Map. Example source code could looks like this:
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class GsonProgram {

    public static void main(String... args) throws Exception {
        Gson gson = new GsonBuilder().create();
        String json = "{\"1\":{\"id\":\"1\",\"value\":\"something\"},\"2\":{\"id\":\"2\",\"value\":\"some other thing\"}}";

        Type type = new TypeToken<HashMap<String, HashMap<String, String>>>() {}.getType();
        Map<String, Map<String, String>> map = gson.fromJson(json, type);
        for (Map<String, String> data : map.values()) {
            System.out.println(Data.fromMap(data));
        }
    }
}

class Data {

    private int id;
    private String value;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Data [id=" + id + ", value=" + value + "]";
    }

    public static Data fromMap(Map<String, String> properties) {
        Data data = new Data();
        data.setId(new Integer(properties.get("id")));
        data.setValue(properties.get("value"));

        return data;
    }
}

Above program prints:
Data [id=2, value=some other thing]
Data [id=1, value=something]

Comment: Thanks, but my json object is way more complex which requires to construct a corresponding class. But I've found out a raw way to read json object which only picks up the fields I need. Thanks anyway.
Comment: @SandeepanNath, I do not understand your question. Does not accepted answer work for you? Have you tried to use `JsonParser` and `JsonElement` classes?
Comment: I mean how to read the first value `1` - {
This one -> "1"
 :{"id":"1","value":"something"},
"2":
                          {"id":"2","value":"some other thing"}
}
Comment: In my example I use `Map`: `Map> map = gson.fromJson(json, type);`. You will find `1` in key set of that map. Something like `map.keySet()`
Answer: Because this json object uses int as the field key that you cannot specify the field key name when deserialize it. Thus I need to extract the value set from the  set first:
JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(json).getAsJsonObject();
Set<Entry<String,JsonElement>> set = obj.entrySet();

Now "set" contains a set of , in my case is <1,{id:1,value:something}>.
Because the key is useless here, I only need the value set, so I iterate the set to extract the value set.
for (Entry<String,JsonElement> j : set) {
            JsonObject value = (JsonObject) j.getValue();
            System.out.println(value.get("id"));
            System.out.println(value.get("value"));
        }

If you have more complex structure, like nested json objects, you can have something like this:
for (Entry<String,JsonElement> j : locations) {
            JsonObject location = (JsonObject) j.getValue();
            JsonObject coordinate = (JsonObject) location.get("coordinates");
            JsonObject address = (JsonObject) location.get("address");
            System.out.println(location.get("location_id"));
            System.out.println(location.get("store_name"));
            System.out.println(coordinate.get("latitude"));
            System.out.println(coordinate.get("longitude"));
            System.out.println(address.get("street_number"));
            System.out.println(address.get("street_name"));
            System.out.println(address.get("suburb"));
        }

Hope it helps.
Comment: What/Where is JsonParser class?
Comment: @PranoyC it's from Jackson Java library


The CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Question: I have implemented a SQL statement that returns a Boolean value of true is the Username and Date exist in the Table. Below is my implementation. I had also included the current error that is displayed in the Logcat
public boolean flagExists(String Username, String Date) {
SQLiteDatabase db = getReadableDatabase();
String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + " ' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'";

// Add the String you are searching by here.
// Put it in an array to avoid an unrecognized token error
Cursor cursor = db.rawQuery(selectString, new String[]{Username,Date});

boolean hasObject = false;
if (cursor.moveToFirst()) {
    hasObject = true;

    //region if you had multiple records to check for, use this region.
    int count = 0;
    while (cursor.moveToNext()) {
        count++;
    }
    //here, count is records found
    Log.d(TAG, String.format("%d records found", count));

    //endregion

}
cursor.close();          // Dont forget to close your cursor
db.close();              //AND your Database!
return hasObject;

}
rocess: com.example.s210121629.myhealth, PID: 10953
    android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:86)
            at com.example.s210121629.myhealth.Database.DBHelper.getPerson(DBHelper.java:400)
            at com.example.s210121629.myhealth.dashboard.DashboardActivity.decisionAlgorithm(DashboardActivity.java:347)
            at com.example.s210121629.myhealth.dashboard.DashboardActivity.reloadlist(DashboardActivity.java:327)
            at com.example.s210121629.myhealth.dashboard.DashboardActivity$3.onFinish(DashboardActivity.java:252)
            at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
            at android.os.Handler.dispatchMessage(Handler.java:102)

Answer: Change
Cursor cursor = db.rawQuery(selectString, new String[]{Username,Date});

with
Cursor cursor = db.rawQuery(selectString, null);

if you want to specify the selectionArgs parameter, your selectString has to contain a number of ?s equals to the length of the selectionArgs String[]
Comment: I get the following error after making the suggested changes            E/SQLiteLog﹕ (1) near "ANDFlagDate": syntax error
09-08 21:04:25.569  27790-27790/com.example.s210121629.myhealth E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.s210121629.myhealth, PID: 27790
    android.database.sqlite.SQLiteException: near "ANDFlagDate": syntax error (code 1): , while compiling: SELECT * FROM Flags WHERE Username = 'email@example.com'ANDFlagDate= '08-09-2015'
Comment: you have to leave spaces before and after the keyword AND
Comment: I had not included the space
Answer: Change
String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + " ' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'";

to
String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + "' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'";

You are adding an extra space to the Username variable.
Comment: I get the following error after making the suggested changes            E/SQLiteLog﹕ (1) near "ANDFlagDate": syntax error
09-08 21:04:25.569  27790-27790/com.example.s210121629.myhealth E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.s210121629.myhealth, PID: 27790
    android.database.sqlite.SQLiteException: near "ANDFlagDate": syntax error (code 1): , while compiling: SELECT * FROM Flags WHERE Username = 'firstname.lastname@example.com'ANDFlagDate= '08-09-2015'
            at
Comment: Are you sure you copied and pasted?
Comment: I had left not included a space after and before the AND as suggested by @Blackbelt


An Example of a Non-continuous Injection on a Real Interval, that is Not Strictly Monotonic.

Question:
Theorem
Let $I$ be a real interval.
Let $f:I \to \mathbb R$ be an injective continuous real function.
Then $f$ is strictly monotone.

If the condition on continuity indeed is necessary (it most probably is but I would prefer to see it more clearly...), then we would be able to find such a function. Otherwise, I wouldn't be so convinced. I've seen the proof that uses the Intermediate Value Theorem and I do realize it requires continuity, obviously. But I don't know if we could do without it.
Comment: But that isn't my question?
Comment: You could also use the mean value theorem to show the converse to that statement
Answer: Consider $I = [0,2]$ and $f$ defined on $I$ via $f(x) := x$ when $x \leq 1$ and $f(x):= -x $ when $x > 1$.
Comment: Well, I really feel silly right now... Thank you.


Regex to extract all pair of words and their respectively 3 words after and 3 words before in Python

Question: Regex to extract all pair of words and their respectively 3 words after and 3 words before
This is my regex:
((?:[\S,]+\s+){0,3})((?:[\S,]+\s*){0,3})

This is the text:
Quadriband - UMTS - HSDPA - HSUPA - LTE - Wi-Fi Fotocamera da 8 Megapixel - Lettore MP3 Sistema Operativo iOS8 - GPS integrato

I only get these results:
Quadriband - UMTS  - HSDPA -
HSUPA - LTE  - Wi-Fi Fotocamera
da 8 Megapixel  - Lettore MP3
Sistema Operativo iOS8  - GPS integrato

But I want all combination especially the - Wi-Fi Fotocamera - da 8 Megapixel
How can I edit my regex to catch all possible 3 (also 0, 1 or 2 will be fine) words before and 3 (also 0, 1 or 2 will be fine) words after a space..
https://regex101.com/r/mP9lT9/1
Comment: actually you want every 6 consecutive words?
Comment: What's your expected output?
Comment: All possible 3 (also 0, 1 or 2 will be fine) words before and 3 (also 0, 1 or 2 will be fine) words after a space...
Answer: Seems that you want every 6 consecutive none whitespace sub string.So you don't need regex for this task you can use itertools.islice within a list comprehension :
>>> from itertools import islice
>>>
>>> [' '.join(islice(li,i,i+6)) for i in range(len(li))]
['Quadriband - UMTS - HSDPA -', '- UMTS - HSDPA - HSUPA', 'UMTS - HSDPA - HSUPA -', '- HSDPA - HSUPA - LTE', 'HSDPA - HSUPA - LTE -', '- HSUPA - LTE - Wi-Fi', 'HSUPA - LTE - Wi-Fi Fotocamera', '- LTE - Wi-Fi Fotocamera da', 'LTE - Wi-Fi Fotocamera da 8', '- Wi-Fi Fotocamera da 8 Megapixel', 'Wi-Fi Fotocamera da 8 Megapixel -', 'Fotocamera da 8 Megapixel - Lettore', 'da 8 Megapixel - Lettore MP3', '8 Megapixel - Lettore MP3 Sistema', 'Megapixel - Lettore MP3 Sistema Operativo', '- Lettore MP3 Sistema Operativo iOS8', 'Lettore MP3 Sistema Operativo iOS8 -', 'MP3 Sistema Operativo iOS8 - GPS', 'Sistema Operativo iOS8 - GPS integrato', 'Operativo iOS8 - GPS integrato', 'iOS8 - GPS integrato', '- GPS integrato', 'GPS integrato', 'integrato']

Also if you have a special condition for your words you can find the words with passing a specific regex to re.findall and use it as s.split.


Prove the existence of limit of certain sequences.

Question: Problem: Let $0<a_1<b_1$ and
$$a_{n+1}=\sqrt{a_n\cdot b_n},b_{n+1}=\frac{a_n+b_n}{2}.$$ Prove that $\{a_n\}$ and $\{b_n\}$ converge to some limit.
Attempt: By induction and AM-GM, I can show that $0<a_n<b_n$ for any $n\in\mathbb{N}$.I am thinking maybe I can use Cantor intersection theorem at some point? Provided that I can show that $a_n \rightarrow$ and $b_n\downarrow$. But I am not sure if this is the right way to think about it.
Answer: By induction on $n$ we have $a_n<a_{n+1}<b_{n+1}=(a_n+b_n)/2,$  so $0<b_{n+1}-a_{n+1}<(b_n-a_n)/2\leq 2^{-n}(b_1-a_1),$ so the increasing sequence $(a_n)_n$ and the decreasing sequence $(b_n)_n$ converge to the same value, which is called the Arithmetico- Geometric Mean of $a,b. $ (Or AG Mean for short).
Answer: This is arithmetic geometric mean.
All you need is $ a_n < a_{n+1} $ and $ b_{n+1} < b_{n} $. With that the sequences are monotonic and bounded and therefore converges.
https://en.wikipedia.org/wiki/Arithmetic%E2%80%93geometric_mean
Answer: By using $0<a_n<b_n$
$$b_{n+1}=\frac{a_n+b_n}{2}≦b_n・・・≦b_1 $$
$$a_{n+1}=\sqrt{a_n\cdot b_n}≦b_n・・・≦b_1$$


My code is not updating in the frontend

Question: I have created one content type template few days before
page--classroom_course.tpl.php

Now I'm trying to modify my code in that page but my changes are not showing in front end. Even I cleared cache everything but still the issue is same.
After that I removed that file and I again checked but it's not showing any error instead of that same output is showing.
I don't know how that is taking.
Comment: page--classroom-course.tpl.php change the name of the file like this.
Comment: no changes, still that page is showing.
Comment: Try with node--classroom-course.tpl.php
Comment: Do u have several copies of your site? Perhaps ur in the wrong server?
Comment: Are u in the correct theme folder? Which theme is enabled?
Comment: @jawaharj Clear Cache after placing the file :)
Answer: What is the internal path of your page ? (not path alias).
Based on the internal path, you can find out how to create the correct template.
Or even better:
This is the best solution and the most precise:
In the theme folder, you can modify template.php and implement the following hook:
function THEMENAME_preprocess_page(&$variables) {
  // If you have devel installed; if not, user var_dump or enable devel.
  kpr($variables['theme_hook_suggestions']);
  die();
}

Navigate to your page, after clear cache and you should see the theme hook suggestions and based on those, you can create your template.
E.q. If one of the suggestions is something like "page__page_name", then your template name will be "page--page-name.tpl.php".

My code is not updating in the frontend


Elasticsearch connection refused

Question: I have just installed ElasticSearch 7.1.1 on Debian 9 throw apt-get repository
VPS 4GB ram .. 1vcpu
service elasticsearch status

 elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2019-06-04 16:53:25 CEST; 4min 53s ago
     Docs: http://www.elastic.co
  Process: 3161 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=78)
 Main PID: 3161 (code=exited, status=78)

Jun 04 16:53:11 MONITOR-BACKUP systemd[1]: Started Elasticsearch.
Jun 04 16:53:11 MONITOR-BACKUP elasticsearch[3161]: OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
Jun 04 16:53:25 MONITOR-BACKUP systemd[1]: elasticsearch.service: Main process exited, code=exited, status=78/n/a
Jun 04 16:53:25 MONITOR-BACKUP systemd[1]: elasticsearch.service: Unit entered failed state.
Jun 04 16:53:25 MONITOR-BACKUP systemd[1]: elasticsearch.service: Failed with result 'exit-code'.

test curl
curl -X GET http://126.96.36.199:9200/
curl: (7) Failed to connect to 22.214.171.124 port 9200: Connection refused

enviroment vars
$PATH
-bash: /usr/share/elasticsearch/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: No such file or directory

$JAVA_HOME
-bash: /usr/share/elasticsearch/jdk: Is a directory

Comment: You got connection refused because elasticsearch is not running. You need to investigate why it stopped.
Comment: with "service elasticsearch start" the server start, with server started "netstat -tulpn" doesn't show opened ports! ... after that it go on error.. "service elasticsearch status" show FAILED
Comment: Then you need to find out why elasticsearch crashed. Check its logs.
Comment: thanks, solved "at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured" ... just encommented row cluster.initial_master_nodes: ["node-1"] inside /etc/elasticsearch/elasticsearch.yml
Answer: In my case, status=78, it was solved by keeping node.name and cluster.initial_master_nodes with a proper name.
 elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2019-08-07 19:55:30 EEST; 3s ago
     Docs: http://www.elastic.co
  Process: 2331 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=78)
 Main PID: 2331 (code=exited, status=78)

I read that due to the changes in cluster coordination introduced in Elasticsearch 7.x (https://www.elastic.co/blog/a-new-era-for-cluster-coordination-in-elasticsearch), as the log shows we are forced to configure at least one of those mentioned parameters in production.
In this case it can be easily solved by adding this to your elasticsearch.yml:
node.name: node-1 (or preferred name)
cluster.initial_master_nodes: node-1 (or preferred name)
This is also mentioned in the breaking changes 7.0 documentation of Elasticsearch (https://www.elastic.co/guide/en/elasticsearch/reference/7.0/breaking-changes-7.0.html#_discovery_configuration_is_required_in_production).
Source: https://groups.google.com/forum/#!topic/wazuh/MTNx6RcqRcQ

Elasticsearch connection refused


Independent or dependent events for conditional probabilities

Question: From the attached image of a tree diagram, is it possible to obtain $P(A)$? I would like to get hint rather than the answer. I drew the diagram from the below scenario but I am having difficulty determining $P(A)$ and whether or not the events are independent. Thank you.
enter image description here
The event of picking a person with red hair is A.
The event of picking a female is B.
The probability of picking red hair and female is $.05$ and the probability of picking a female is $0.25$. Thank you.
Answer: What is the probability of picking a female with red hair? What is the probability of picking a male with red hair? Then add these two probabilities to get $P(A)$.
The events cannot be independent since $P(A|B)\neq P(A|B')$.
Comment: From image, probability of red hair and female is 0.05 and the probability of male is 0.75. For probability of male and red hair don't I need the probability of picking red hair? And how do I calculate for that?
Comment: @user2220115 you calculate it the same way, just using a different branch of the tree: probability of male and red hair is $0.75\times0.067$.
Answer: As you asked, here is your Hint:
Use the Law of total probability $P(A) = P(A\cap B) + P(A\cap B')$


Prove or disprove: The image of a ring homomorphism $\phi:R\to S$ is an ideal in $S$.

Question:
Prove or disprove: The image of a ring homomorphism $\phi:R\to S$ is an ideal in $S$.

I only see examples where they use the image of an ideal, but I don't think this is the case for my question.
At first I tried using the ideal test, and I had a feeling that it wasn't working out the way it should, so now I am trying to find a counterexample. Any ideas for a simple counterexample?
Answer: It's false: Take $R=\mathbb{Z}$, $S=\mathbb{Q}$ and $\phi$ to be the inclusion.
Comment: Or more generally, $R$ any integral domain that is not a field, $S$ its field of fractions, and $\phi$ to be again the inclusion.
Comment: Hello, may I ask what you mean by inclusion?
Comment: @numericalorange sure, take $n\in \mathbb{Z}$ to $n\in \mathbb{Q}$
Answer: Another example I like :
Take $R$ any ring and the polynomial ring $R[X]$. Now consider the morphism $f:R[X]\rightarrow R[X]$ which sends $X$ to $X^2$ (extend it).


Accelerating multi-label classification using NNs

Question: I am using Tensorflow for multi-label classification of Audio. The dataset I am using is made up of 10 different classes, and to each sample of audio correspond two labels. In other words, the number of overlapping sounds/classes per file is always 2. So the vector of labels "y" would be for example:
y = [0, 0, 0, 0, 0, 1, 0, 1, 0, 0]
I am using 3 convolutional layers , 1 FC layer and 1 output layer made up of 10 logistic units. And as cost function I am using the binary cross entropy for each unit and then averaging the result.
I don't know if this actually has any consequences but all my inputs are negative values.
For some reason(s), I guess mainly as a consequence of the initialization of the parameters (weights) of the network and the logistic units in the output layer, the network tends to predict mainly zeros. I guess after some training this trend will start to dissapear, however, I wonder if there is a smart way of doing something so the network does not predict those many zeros and predicts more ones, which I believe would make the cost higher and the gradient stronger (not 100% sure about this one though)  and therefore training would be faster.
I use Xavier initialization for the weights of all layers, as an example:
W2 = tf.get_variable("W", [5,5,24,48], initializer=tf.contrib.layers.xavier_initializer())

Maybe using tf.truncated_normal_initializer instead
could help?
Answer: I use sigmoid when there are an arbitrary number of possible labels. In your case, you know you have exactly two labels. I would instead use softmax and divide the true label by two, for example [0,...,0,.5,.5,0,...].
This will force your network to at least sum to 1, something you don't get with sigmoid.
During inference, multiply the output by two if you want it to remain more interpretable as individual probabilities.
Comment: Thanks for the comment @kbrose. But if I use softmax, am I not then modelling a probability distribution that would give me as prediction 0.5 and 0.5 when two classes are present, when in fact the probabilities of those two classes should be 1 and 1 ?
Comment: Softmax is usually used to force a probability distribution, but the only thing _really_ special about it is you force the output of a layer to sum to a specific value. In your case, you can just re-interpret it as forcing your network to make two decisions instead of one. As I suggested, you could always multiply output by 2 during inference so it appears more interpretable.
Comment: Okey, I will give it a try then. Thanks.
But using sigmoids should in theory also work right? Even if you are not forcing it to sum 1 in the outputs, it would end up doing that, ideally.
I am not 100% sure though about what you say about multiplying the output by 2 during inference. I've read (https://towardsdatascience.com/the-softmax-function-neural-net-outputs-as-probabilities-and-ensemble-classifiers-9bd94d75932) that the softmax does some sort of weighted average.
Are you sure that multiplying the output by two is a fair thing to do?
Comment: Yes, sigmoids should work in theory. Using just fully connected layers on images also work in theory, though, but no one does that except as a toy project on MNIST. A lot of my experience in this field has been that adding constraints to your model that best reflect the data-at-hand tends to give better results faster.
Comment: All softmax does is force all outputs across the given layer to sum to 1 in a way that gradients can still flow through pretty well. I don't really know how to answer "are you sure that ...". It's all just numbers that we're choosing to interpret in different ways. I feel confident in my ability to manipulate these numbers in ways that may make them slightly more interpretable. Can you say why you think doing such a multiplication would be invalid?
Comment: Perhaps the confusion is around "during inference". By that I mean don't multiply the output by 2 during _training_ (instead _divide_ your _labels_ by 2). But if you are for example showing these probabilities on some sort of user interface, they may expect to see values closer to 1. Using softmax in the ideal world where your classifier correctly identified both correct labels your max probability would be 0.5. So you could simply multiply the probabilities by 2 on a user interface so they match expectations.
Comment: Thanks for the comments @kbrose.  I understood what you meant with "during inference", I was just not sure that, since the Softmax function does a non-linear transformation of the input vector, then the output probabilities can be scaled by 2 and interpreted just like that. But I don't have any fundament to say that is invalid.
So then for example during inference, if I calculate the predicted True Positives as "tp = tf.count_nonzero(predicted * actual)" where "predicted = tf.round(2*tf.nn.softmax(Z5))" and "actual = 2*labels",  does that seem correct?
Comment: That doesn't seem incorrect, but it's still possible you won't get any positive guesses if you use `round` (e.g. `[.4, .4, .4, .,4, .4]`). Don't have a great solution on hand.
Comment: Yeah, that's true. I was doing that before with the logistic units, but with softmax knowing how many true values are (2) it might make more sense to take the two highest values instead of rounding.


Using OR with EntityFieldQuery

Question: I have never had the need to do this before today, but it doesn't seem like you can make OR queries with EntityFieldQuery, since db_or is used for select queries.
An example would get to all entities that has a date field where the value is null or after today.
Am I missing something or some trick or is this simply not supported?
Comment: This is old but high up in my google results - it should be noted that you can use the orConditionGroup for this in Drupal 8.
Comment: You can also split one query into two, run them and then join results.
Comment: The performance impact of this is pretty horrible if the queries or the amount of data are even remotely larger.
Answer: I have seen a solution of this problem. The idea is to use addTag() in query and implement hook_query_TAG_alter(), where you have good old SelectQuery object.
Comment: Since this is a community answer, and most of it consists of an external link, I feel like the code, or at least some of the contents of the article, should be included in this answer. Because links do die. [Meta StackExchange discussion on this topic](http://meta.stackexchange.com/a/8259)
Comment: I'd propose selecting this as the right answer. The blog post provides a method for adding OR conditionality to EntityFieldQueries. The only problem is that you actually build SQL dependency with that method, which is kind of against the whole point of EFQs, but at least gets the job done. Thanks for the good link @Michael.
Comment: The original article is rather long and the idea can be summarized as "use addTag() in query and implement hook_query_TAG_alter()". After that the question has been reduced to "How to use OR with SelectQuery object" which is known subject.
Comment: I would strongly recommend converting the EFQ to a regular select query in this scenario.  Sticking with the EFQ and using tag-based alterations to mess with what it produces is horrible by comparison.
Answer: You can sublass EntityFieldQuery and override some methods.
The conditions that are added to an object of class EntityFieldQuery (e.g. a property condition) are added to an array.
  public function propertyCondition($column, $value, $operator = NULL) {
    // The '!=' operator is deprecated in favour of the '<>' operator since the
    // latter is ANSI SQL compatible.
    if ($operator == '!=') {
      $operator = '<>';
    }
    $this->propertyConditions[] = array(
      'column' => $column,
      'value' => $value,
      'operator' => $operator,
    );
    return $this;
  }

When the query is built, that array is then used in a loop similar to the following one (the code is present in EntityFieldQuery::propertyQuery()):
foreach ($this->propertyConditions as $property_condition) {
  $this->addCondition($select_query, "$base_table." . $property_condition['column'], $property_condition);
}

$select_query contains the value returned from a call to db_select().
Answer: You can't I'm afraid, ORs are not natively supported by the EntityFieldQuery class.
One way round it might be to add a tag to the query with with ->addTag(), then implement hook_query_TAG_alter() to change the internal structure of the query manually for queries containing that tag.
Doing this you will be able to loop through the existing conditions and make the necessary alterations to add your OR logic. It's not a pretty way to do it though; you can find an example here.
Answer: No need to split queries into 2 and merge or anything like that. Just need to alter the query
Consider the scenario:
I had 2 entity types with machine names: tincan statements, and tincan_agents
5 entity reference fields on the entity
4 of them are regular entity reference fields and the 5th (tincan_object) is a multi-entity-type reference field, each reference field references a entities of type 'Agent'.
The tincan_object reference field can reference Agents and Activities (a third entity type). An Agent has a property object_type, which can either be Agent or Group.
I want to find any Statement that references one of several possible Agents, in any of the reference fields. We need an OR operator between the fieldConditions, but we also need to check the object_type of the multi-entity type reference field, and make sure it is one of two possibilities.
The code below represents the simplest possible , in our solution the query had many other conditions, fields, etc... so the code needed to not count on the order of conditions, or even if all of these fields were being queried.
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'tincan_statement');

    $all_agents = array(4,10); //entity_ids to search for
    $query->addTag('tincan_statement_get_agents');
    $query->fieldCondition('tincan_actor', 'target_id', $all_agents, 'IN');
    //need OR between fields conditions
    $query->fieldCondition('tincan_authority', 'target_id', $all_agents, 'IN');
//need OR between fields conditions
    $query->fieldCondition('tincan_instructor', 'target_id', $all_agents, 'IN');
//need OR between fields conditions
    $query->fieldCondition('tincan_team', 'target_id', $all_agents, 'IN');
//need OR between fields conditions
//but then nested in the OR structure we need an AND for two columns of the multientity type reference field tincan_object
    $query->fieldCondition('tincan_object', 'target_id', $all_agents, 'IN');
    $query->fieldCondition('tincan_object', 'object_type', array('Agent', 'Group'), 'IN');
    $results = $query->$execute();

Solution:
Notice in the above EntityFieldQuery
 $query->addTag('tincan_statement_get_agents');

This tags the query, allowing implementation of hook_query_TAG_alter()
/**
 * Implements hook_query_TAG_alter()
 * alters the query for finding agents with or without the related_agents flag
 * used for Statement API Get processor EntityFieldQuery
 */
function tincan_lrs_query_tincan_statement_get_agents_alter(QueryAlterableInterface $query) {
  //need to or the search for all the fields (actor, object, authority, instructor, team)
  // the object_type of the object field needs to be Agent OR Group

  $conditions =& $query->conditions();
  // dsm($conditions);  //dsm() is your friend! comes with devel module
  $agent_grouping_condition = db_or();
  $object_parameters = array();
  $x = 0;
  foreach ($conditions as $key => $condition) {
    if (is_numeric($key) && isset($condition['field']) && is_scalar($condition['field'])) {
      if ( (strpos($condition['field'], 'tincan_object_object_type') !== FALSE  ||
          strpos($condition['field'], 'tincan_object_target_id') !== FALSE ) && $condition['operator'] == 'IN') {
  //u
            unset($conditions[$key]);
            $object_parameters[$x]['field'] = $condition['field'];
            $object_parameters[$x]['value'] = $condition['value'];
            $object_parameters[$x]['operator'] = $condition['operator'];
            $x += 1;
          }

       if(strpos($condition['field'], 'tincan_actor_target_id') !== FALSE ||
          strpos($condition['field'], 'tincan_instructor_target_id') !== FALSE ||
          strpos($condition['field'], 'tincan_team_target_id') !== FALSE ||
          strpos($condition['field'], 'tincan_authority_target_id') !== FALSE ) {
            unset($conditions[$key]);
            $agent_grouping_condition->condition($condition['field'], $condition['value'], $condition['operator']);

      }
    }
  }

  // create new AND condition to nest in our OR condition set for the object parameters
  $object_condition = db_and();
  foreach($object_parameters as $key => $param) {
    $object_condition->condition($param['field'], $param['value'], $param['operator']);
  }

  $agent_grouping_condition->condition($object_condition);

  $query->condition($agent_grouping_condition);

  //By default EntityFieldQuery uses inner joins, change to left
  $tables =& $query->getTables();

  foreach($tables as $key => $table) {
    if (strpos($key, 'field_data_tincan_object') !== FALSE ||
        strpos($key, 'field_data_tincan_actor') !== FALSE ||
        strpos($key, 'field_data_tincan_authority') !== FALSE ||
        strpos($key, 'field_data_tincan_instructor') !== FALSE ||
        strpos($key, 'field_data_tincan_team') !== FALSE ) {
          if(!is_null($table['join type'])) {
            $tables[$key]['join type'] = 'LEFT';
          }
    }
  }

}

Answer: The OP wants to query for entities with date null OR bigger than x, I wanted to query for nodes with no language defined OR the user's language. addTag() is the best solution for adding an actual OR statement, but would be overkill in my case. My very simple OR can be accomplished by looking up the language property in an array using:
$query->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN');

Using OR with EntityFieldQuery


Spectrum of a multiplication operator in L^2

Question: I have following excercice:

Let $X=\mathbb{L}^2(\mathbb{R})$ and $D=\{f \in
 \mathbb{L}^2(\mathbb{R}) : \int |xf(x)|^2 < \infty \}$
$A:D\subset X \rightarrow X$ $\; \; Af(x):=xf(x)$,
    determine the
  spectrum of A

I have shown, that $\sigma (A) =\mathbb{R}$ and that $\sigma_p (A)=\emptyset$
Now Im trying to determine the continous spectrum.
Let $z \in \mathbb{R}$ and $f \in C_0^{\infty}(\mathbb{R})$, then $(A-z)f \in C_0^{\infty}(\mathbb{R})$ . Since these functions are dense in $\mathbb{L}^2(\mathbb{R}) $ it follows that Ran(A-z) is dense and therefore $\mathbb{R} \subset \sigma_c(A)$.
Is this correct?
kind regards
Comment: It's true that $C_0^\infty$ is dense in $L^2$, but you haven't shown that every $C_0^\infty$ function is in the range of $A-z$.  You should let $g \in C_0^\infty$ be arbitrary and construct an $f \in D(A)$ such that $(A-z)f=g$.
Comment: You are right, obviously., I wasnt able to construct the desired f, but i found an alternative proof.

Let $z\in \mathbb{R}$ and $B_n:= \{x \in \mathbb{R} : |x-z| > \frac{1}{n}\}$
and let g be an arbitrary test function. Now $g \in D(A)$ and hence $1_{B_n}g \in D(A)$ and $1_{B_n}g \in Ran(A-z)$ because
$g_n:=1_{B_n}(x)\frac{1}{x-z}g(x) \in D(A)$ and $(A-z)g_n=g(x)1_{B_n}$ also     $1_{B_n}g \rightarrow g $ pointwise almost surely and by dominated convergence and Riesz in $\mathbb{L}^2$
Since $\overline{Ran(A-z)}$ is closed it follows that g is in there and therefore the claim. better now?
Comment: Yes much better.  You can post it as an answer so that this question can be marked as resolved.
Comment: out of curiousity, do you know how to construct the f, so that $(A-z)f=g$?
Comment: Trick question, you can't in general.  $(A-z)f$ will necessarily vanish at $x=z$ and so any $g$ which doesn't do so cannot be in the range of $A-z$.  In particular $C^\infty_0$ is not contained in the range.
Answer: $A-\lambda I$ is invertible for all $\lambda\notin\mathbb{R}$. This is because
$$
         \frac{1}{x-\lambda }(A-\lambda I)f=f, \;\;\; f\in \mathcal{D}(A),
$$
and because $\frac{1}{x-\lambda }f \in \mathcal{D}(A)$ for all such $\lambda$, with
$$
                (A-\lambda I)\frac{1}{x-\lambda}f=f.
$$
Therefore $\sigma(A)\subseteq\mathbb{R}$, because $\frac{1}{x-\lambda}$ is a bounded multiplication operator on $L^2(\mathbb{R})$.
Every $\lambda\in\mathbb{R}$ is in the spectrum because $\mathcal{R}(A-\lambda I)\ne L^2$ for any such $\lambda$, which can be seen by observing that the following is not in $L^2$ for all $\lambda\in\mathbb{R}$:
$$
                       e^{-x^2}\frac{1}{x-\lambda}.
$$
Comment: If $\sigma (A) = \mathbb R$ then it fails to be compact. But we know that spectrum of an operator is necessarily compact. What am I missing here?
Comment: @AntonioClaire : This particular solution does not require anything sophisticated. The solution is straightforward starting from basic principles. So why lob a grenade when a bb gun is sufficient?
Comment: Didn't you show that the spectrum of $A$ is the entire real line. But what I know is that the spectrum of any bounded operator is always a compact set but $\mathbb R$ fails to be compact. Doesn't it give us a contradiction? It will be very helpful if you clarify my query. Thanks in advance.
Comment: @AntonioClaire : Your observation shows you that multiplication by $x$ on $L^2(\mathbb{R})$ is not bounded. And that is correct.
Comment: Oh I can see now. Since $\text {ess} \sup (x \mapsto x) = \infty.$
Comment: @AntonioClaire : That's it. :)
Answer: Let $z\in \mathbb{R}$ and $B_n:= \{x \in \mathbb{R} : |x-z| > \frac{1}{n}\}$
and let g be an arbitrary test function. Now $g \in D(A)$ and hence $1_{B_n}g \in D(A)$ and $1_{B_n}g \in Ran(A-z)$ because
$g_n:=1_{B_n}(x)\frac{1}{x-z}g(x) \in D(A)$ and $(A-z)g_n=g(x)1_{B_n}$ also     $1_{B_n}g \rightarrow g $ pointwise almost surely and by dominated convergence and Riesz in $\mathbb{L}^2$
Since $\overline{Ran(A-z)}$ is closed it follows that g is in there and therefore the claim.


Exercise in Tao Analysis Book

Question: I'm currently studying in the book of Analysis of Terry Tao, amazing  book by the way. In one exercise I'm not pretty sure about how can do it (I know that will be almost trivial but I'm stuck in it).
Definition: Let $\varepsilon >0$ and $x,y\in \mathbb{Q}$. We say that $y \text{ is } \,\,\varepsilon\text{ - close to } x \iff d(x,y)\le \varepsilon$; where $ d(x,y)$ is the distance of $y$ to $x$ define as $d(x,y)= \,\mid x-y\mid.$
Ex: Let $\varepsilon>0$. If $x$ and $y$ are $\varepsilon \text{ - close}$ to $x$, and $w$ is between $y$ and $z$, then $w$ is also $\varepsilon \text{ - close}$ to $x$.
So my question: is there a clever way to do it without divide the exercise by cases when $y\le w\le z$ and then when $z\le w\le y$? Sorry if I'm not put my attempt, everything I tried is completely wrong. Thanks in advance.
Comment: You can say "without loss of generality, we may assume that $x\leq y$". This basically says that "the same proof works if $y\leq x$"
Comment: @Prahlad Vaidyanathan; Without loss of generality we may assume that $x \le y$. Either $y\le w\le z$ or $z\le w\le y$. If  $y\le w\le z$, clearly $0 \le y-x \le w-x \le z-x $. Thus, $\vert w- x \vert = w-x$ and $\vert z-x \vert =z-x$. It follows that $\vert w- x \vert \le \vert z- x \vert \le \varepsilon $. On the hand, if $z\le w\le y$, either $w-x \ge 0$ or $\,w-x \le 0 $. If  $w-x \ge 0$, then $\vert w-x\vert \le \vert y-x \vert \le \varepsilon$. If $w-x \le 0$, then $0\le\vert w-x \vert = x-w\le \vert x-z \vert \le \varepsilon$. Hence in either case $d(x,w) \le \varepsilon$.
Answer: One characterization/definition/property of "between" is that $d(x,w) + d(w,y) = d(x,y)$.  The distances to $x$ and $y$ are bounded by the $xy$ distance.
Comment: But why not $d(x,w) + d(w,y) \ge d(x,y)$ since, using the definition of distance as the book says, $d(x,y)=\vert x-y\vert =\vert (x-w)+(w-y)\vert\, \le \,\vert x-w \vert +\vert w-y \vert = d(x,w)+d(w,y)$
Comment: The triangle *in*-equality does hold if its stronger special case, with equality, applies.  The equality has additional implications that the inequality does not, such as the conclusion of this exercise being true.
Answer: There is a typo in the original post, saying "if x and y are e-close to x", instead of "y and z" being e-close to x.
Claim: Let $\epsilon > 0$. If $y$ and $z$ are both $\epsilon$-close $x$, and $w$ is between $y$ and $z$ (i.e., $y \leq w\leq z$ or $z\leq w \leq y$), then $w$ is also $\epsilon$-close to $x$.
Pf. Suppose $y \leq w \leq z$. We need to show that $d(x, w) \leq \epsilon$. By definition it suffices to show that $|x - w| \leq \epsilon$, i.e. that, $-\epsilon \leq x - w \leq \epsilon$ or that $w \leq x + \epsilon$ and $x - \epsilon \leq w$. To prove the claim, we show that these last two inequalities hold.
Since $d(x, y) \leq \epsilon$, we have $-\epsilon \leq x - y \leq \epsilon$. From the last inequality we have that $x - \epsilon \leq y$, and since $y \leq w$, by transitivity, $x - \epsilon \leq w$, which shows that the second inequality holds.
On the other hand since $d(x, z) \leq \epsilon$, $-\epsilon \leq x - z \leq \epsilon$. It follows that $z \leq x + \epsilon$ and since $w \leq z$, by transitivity again, we conclude that $w \leq x + \epsilon$, which shows that the first inequality also holds and proves the claim for the case where $y \leq w \leq z$. The other case can be proved similarly.

Exercise in Tao Analysis Book


Is there any way we can lowerCase the string while using filter in haskell

Question: I have a function in Haskell that takes a string and, after removing specific characters from it, returns a list of the characters in the string. Is there any way I can have the output be lowercase?
See my function below:
getList s = filter (\w -> w `notElem` [",.?!-:;\"\'",]) (words s)

Comment: you're not filtering out for the chars in the specific list but for words equal to `",.?!-:;\"\'"` (after removing the spurious comma).  Check my answer below for a working solution with lowercase.
Answer: Use toLower :: Char -> Char from Data.Char:
lowerCase :: String -> String
lowerCase = map toLower

Just apply getList to lowerCase s instead of just s.
Comment: getList s = filter (\w -> w `notElem` [",.?!-:;\"\'",]) (words lowercase s)  like this
Comment: That should be `words (lowercase s)`, not `words lowercase s`. But yes.
Comment: getList s = filter (\w -> w `notElem` [",.?!-:;\"\'",]) words (lowercase s)
Comment: i get an error on this line "error: parse error on input ']'"
Comment: You added a comma in your list for no reason. Why are you writing new questions in comments? If you have a problem you can't solve on your own, ask another question elsewhere.
Comment: Thanks @AJFarmar for your help.
Answer: I don't think your function is doing what you intent
you can rewrite is as
getList = map (map toLower . filter (`notElem` ",.?!-:;'\"")) . words

Comment: Thanks buddy, its perfect


What is measure of a quadrilateral with an equalateral triangle within the shape?

Question: http://i.imgur.com/URCItTG.jpg

BEC is an equilateral triangle and angle ABC is 130, what is the measure of angle ADC.
The answer I was given is 50 degrees, however I am curious as to steps required to get that specific answer.
Answer: Angle A: $120^o$
Angle ABE: $130-60=70^o$  ($2x=60^o$ coz equilaterla triangle)
Angle DEB: $180-60=120^o$ (sum of angle in straight line is $180^o$)
Therefore Angle ADE: $360-70-120-120=50^o$ (sum of angles in a quad. is $360^o$)