AVPlayer streaming works and yet it doesn't

Question: Running Xcode 7.1.1 under El Capitan 10.11.2 an IOS 9.2 app
Trying to understand the minimum code I need to implement playback for a Video stream, and crafted this very simply piece here ... don't need the Observer strictly speaking, but it crept in so I left it.
static const NSString *ItemStatusContext;
// a class static

self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
[self.avPlayer addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];

self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
avPlayerLayer.frame = CGRectMake(128, 128, 512, 386);
[self.view.layer addSublayer: avPlayerLayer];
[self.avPlayer play];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {

if (context == &ItemStatusContext ) {
    AVPlayer *thePlayer = (AVPlayer *)object;
    if ([thePlayer status] == AVPlayerStatusFailed) {
        NSError *error = [self.avPlayer error];
        // Respond to error: for example, display an alert sheet.
        NSLog(@"error %@",error);
        return;
    }
    NSLog(@"player status %ld",(long)[thePlayer status]);
    // Deal with other status change if appropriate.
}
// Deal with other change notifications if appropriate.
//[super observeValueForKeyPath:keyPath ofObject:object
 //                      change:change context:context];
return;
}

It works, but... only on the demo stream provided by Apple, nothing else I give it plays ...
** TRIED **
Tried adding this code into the mix too, which also works with the Apple demo stream, but none of the others I have tried.
NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:url options:nil];

avPlayerItem = [[AVPlayerItem alloc] initWithAsset:avasset];
self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];

.......
** MORE UPDATES ** ... reworked observer since I wasn't getting useful info from it, now it tells me the Apple m3u8 is really to play; and "fails" on everything else I try...
So .... all these fail for example ...
 //self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://content.uplynk.com/209da4fef4b442f6b8a100d71a9f6a9a.m3u8"]];
//self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://content.jwplatform.com/manifests/vM7nH0Kl.m3u8"]];
//self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://walterebert.com/playground/video/hls/sintel-trailer.m3u8"]];

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {

if (object == self.avPlayer.currentItem && [keyPath isEqualToString:@"status"]) {

    if (avPlayer.currentItem.status == AVPlayerStatusFailed) {
        NSError *error = [self.avPlayer error];
        // Respond to error: for example, display an alert sheet.
        NSLog(@"AVPlayerStatusFailed error %@",error);
        return;
    }
    if (avPlayer.currentItem.status == AVPlayerStatusUnknown) {
        NSError *error = [self.avPlayer error];
        NSLog(@"AVPlayerStatusUnknown error %@",error);
    }
    if (avPlayer.currentItem.status  == AVPlayerStatusReadyToPlay) {
        NSLog(@"AVPlayerStatusReadyToPlay");
        [self.avPlayer play];
    }
    //[super observeValueForKeyPath:keyPath ofObject:object
             //              change:change context:&ItemStatusContext];
    // Deal with other status change if appropriate.
}
// Deal with other change notifications if appropriate.
//[super observeValueForKeyPath:keyPath ofObject:object
 //                      change:change context:context];

return;

}
Comment: Make sure the file path you're giving is correct.
Comment: Zachary, I have tried with some tutorial examples and youtube, none work. I am streaming, not downloading/playing from a file. That I have working.
Comment: Tried going thru an avasset too, that works with the Apple demo, but nothing else...
Comment: Updated observer code; now reporting accurately it seems, everything is un-streamable except the Apple demo.
Answer: Phew, recall something about this before; no excuses really. Managed to fix it by looking somewhere completely different; in info.plist which needs this key in it to play arbitrary streams.
 <key>NSAppTransportSecurity</key>
 <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
 </dict>

I know it is bit lax, but I leave the reader to do so some more research if they want to make their app more bullet proof that I care about right now :) Do this, use the observer code in the EDITED section and cut out the [self.avPlayer play] (line 8) in the main code your be in business.
Comment: Warning: As of 2018, if you allow arbitrary loads then be prepared to explain Apple why with justifications. If the justifications aren't sufficient to them then your app is subject to rejection. In short, DO NOT, allow arbitrary loads, unless absolutely necessary.


Send the email when specific node is created

Question: In one of my module, i have created a checkbox on user account page. If user has checked that checkbox at that time an email is sent to all the user when any specific node is created. Lets say when any article node is created then all the users who has checked the checkbox will be notified by the email when node is posted.
Now for the scenario given below:

Lets say if my site has 1000 users who has checked the checkbox/
Simultaneously 10 users are creating the node.
When only 10 users are creating the node simultaneously 1000 * 10 = 10000 emails will be sent at time.
I am not sure whether site will able to handle it or not.

So i want the suggestion that is there any way to queue email and send them after certain intervals like 1000 mails after an hour, 1000 after second hour 1000 after 3rd hour and so on. Or any other nice solution.
Comment: check this module http://drupal.org/project/queue_mail
Answer: I've never used it but the Queue Mail module purports to do exactly that:

Queues email sending from your site so that instead of being sent immediately it is sent on cron.
Useful for high traffic sites that can send a lot of emails on individual page requests.

It has the unusual properties of being both actively maintained, and bug report free at this time - so looks quite promising


href sms: || smsto: not working on android with html+cordova(phonegap)

Question: I'm building an android app using cordova and html(js/css etc..)
I'm trying to open the sms application when the user click on a link.
this is the html code:
Send Sms

while the tel: and mailto: schemes works, the sms: or smsto: not working.
INFO:
using the sms: scheme I get this error: No Activity found to handle Intent
E/Cordova(28360): Error sending sms sms:052xxxx808:android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }

And using the sms: scheme I get this error: ERR_UNKNOWN_URL_SCHEME
D/Cordova(27207): CordovaWebViewClient.onReceivedError: Error code=-10 Description=net::ERR_UNKNOWN_URL_SCHEME URL=smsto:052xxxx808

I'm using Nexus 5 with Kitkat 4.4.2 to test the app..
P.S: on Galaxy 4 the sms: scheme is working...(not KitKat)
UPDATE:
Clicking on a href link in the browser with the sms: scheme does work, So maybe I missing permission or something like that?
Comment: I can confirm I'm seeing the exact same thing and was just about to post the same when I found your report.  Have you tried using the Cordova SMS plugin instead the href? I was starting to go down that path.
Answer: I was able to get this working by creating an SMS Plugin that combined two previous answers here and here. The only catch is that you have to check for the intent to see if build version is KitKat to do differently than in the past.
Here's my plugin code:
public class SmsPlugin extends CordovaPlugin {
    public final String ACTION_SEND_SMS = "SendSMS";

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals(ACTION_SEND_SMS)) {
            try {
                String phoneNumber = args.getString(0);
                String message = args.getString(1);
                String method = args.getString(2);

                if (method.equalsIgnoreCase("INTENT")) {
                    invokeSMSIntent(phoneNumber, message);
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
                } else {
                    sendSMS(phoneNumber, message);
                }

                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
                return true;
            } catch (JSONException ex) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
            }
        }
        return false;
    }

    private void invokeSMSIntent(String phoneNumber, String message) {
        Intent intent;
        Activity activity = this.cordova.getActivity();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
        {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity);

            intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(phoneNumber)));
            intent.putExtra("sms_body", message);

            // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
            if (defaultSmsPackageName != null) {
                intent.setPackage(defaultSmsPackageName);
            }
        } else {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setType("vnd.android-dir/mms-sms");
            intent.putExtra("address", phoneNumber);
            intent.putExtra("sms_body", message);
        }

        activity.startActivity(intent);
    }

    private void sendSMS(String phoneNumber, String message) {
        SmsManager manager = SmsManager.getDefault();
        PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
        manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
    }
}

And here is how I invoke it from JavaScript:
var sms = function() {
    message: function (phnum, callback) {
        if (Ext.os.is.iOS) {
            cordova.exec(callback, function (err) {
                callback('The following error occurred: ' + err);
            }, "Sms", "send", [ {"recipients": [phnum]} ]);
        } else if (Ext.os.is.Android) {
            cordova.exec(callback, function (err) {
                callback('The following error occurred: ' + err);
            }, "SmsPlugin", "SendSMS", [phnum, "", "INTENT"] );
        } else {
            document.location.href = "sms:" + phnum
        }
    }
};
module.exports = sms;

Be sure to add this to you config.xml:
<feature name="SmsPlugin">
    <param name="android-package" value="my.plugin.SmsPlugin" />
</feature>


How to send ajax success to an external php page?

Question: I have an ajax function that will generate a data grid.
prof.php
function ajaxFunction(gotid){
$.ajax({
  type: 'POST',
  url: "profile/ajax/order.php",
  data: {id:gotid},
  dataType: 'json',
  cache: false,
  success: function(result) {
  for( i = 0, l = result.length; i < l; i++ ) {
  var usId = result[i]['user_id'];
  var orId = result[i]['order_id'];
  var poId = result[i]['product_id'];
  var itStat = result[i]['freelancer_st'];
  var unitp = result[i]['unit_price'];
   .... the rest of the code

the function works fine as I generate the grid in a popup inside the prof.php, but i have changed my mind and I want to send the success in an external php page so the grid will be generated there in list.php
Is that possible? could you give me a lead ?
Appreciated.
Answer: You just call another ajax function within your success callback.
function ajaxFunction(gotid){
$.ajax({
  type: 'POST',
  url: "profile/ajax/order.php",
  data: {id:gotid},
  dataType: 'json',
  cache: false,
  success: function(result) {
  $.ajax({
     type: 'POST',
     url: 'list.php',
     data: { data: JSON.serialize( result ) }
     ... etc

Comment: it does not passes anything to the nested ajax, i have placed an alert right after the success: function(result) { but it alerts nothing and won't generate the table in list.php either


Prove hypothesis that $\sigma_1 \leq \sigma_2$in R

Question: My condition
$
X \sim \mathcal{N}(\mu_1,\,\sigma^{2}_1)\,.
     $ where mean = 6, sd = 2.5, n=16
$
Q \sim \mathcal{N}(\mu_2,\,\sigma^{2}_2)\,.
     $ where mean = 8, sd = 4, n=18
I need to prove $H:\sigma^{2}_1 <= \sigma^{2}_2 $ where $H_0$ is $:\sigma^{2}_1 >= \sigma^{2}_2$ with significance level: 0.05
My solution in R:
x_distr <- rnorm(n=16, mean=6, sd=2.5)
q_distr <- rnorm(n=18, mean=8.5, sd=4)

Now I don't know which test to choose, but I decided to use F-Test:
var.test(x_distr, q_distr,
     alternative = c("less"),
     conf.level = 0.95);

But I don't know how to confirm $H$ using test output. Could you help me?
Output :
F test to compare two variances

data:  x_distr and q_distr
F = 0.42046, num df = 15, denom df = 17, p-value = 0.0492
alternative hypothesis: true ratio of variances is less than 1
95 percent confidence interval:
 0.0000000 0.9957639
sample estimates:
ratio of variances
         0.4204604

Comment: Perhaps you could edit your post to show us the output? It is a bit difficult to offer an interpretation of something we cannot see.
Comment: @mdewey sorry. I have made edit
Answer: So the $p$-value is 0.0492 which if you had decided in advance to use the 0.05 level of significance would mean that you could reject the null hypothesis that the two variances were equal in favour of your alternative hypothesis that x has a smaller variance than q. The confidence interval from 0 to 0.996 suggests that (a) you do not have much information here as it is very wide (b) that in the long run 95% of the time the true ratio of the variances would lie between 0 and 0.996.


Strange "File does not exist" error

Question: The following URLs work well if I visit them with browsers.

mydomain.com/f123
mydomain.com/f123/
www.mydomain.com/f123
www.mydomain.com/f123/

All of them are redirected to www.mydomain.com/somfile.php?f=123 with RedirectRule in Apache main conf file, everything works perfectly with my browser, no error output in Apache log file either.  However Apache logs the following errors every minute:
File does not exist: /home/mydomain/www/f123

What does this error mean and why is it thrown?
Comment: What do you get if you request `http://220.127.116.11/f123` where `18.104.22.168` is your IP address?
Comment: It's also redirected to www.mydomain.com/somfile.php?f=123 without any error output. It's always working well no matter what I try with browser.

I also see the following errors:
File does not exist: /home/natmed/mydomain/f123, referer: http://www.mydomain.com/f123/
Answer: I know this sounds dumb but does the file really exist at that location (/home/mydomain/www/f123)? If you are using a masking technique like this and the file exists elsewhere then Apache won't actually find the file if it doesn't exist in the first place.
Additionally, I was thinking it may be permissions related. Perhaps it could have something to do with the executable permission, which is used to read directories and sometimes files in certain cases. Take a look at the permissions, make sure you can access as apache (user / group).
Usually path or permission 9 out of 10 times.
Comment: /home/mydomain/www/f123 doesn't exist, but /home/mydomain/www/somfile.php exists, the redirection works well. Basically, the website works perfectly for me and other several thousands visitors a day, but the Apache log suggests it doesn't work for some users. But I couldn't figure out how possibly the error was thrown out, because it's always working well when I test from my browser.
Comment: Right, thats why apache is complaining. Apache thinks that the literal file /home/mydomain/www/f123 exists, when it doesn't. It might be because of how your front-end users access the file as well. Take a look in apache's access logs for some clue.

Strange "File does not exist" error


Is it possible to organize template files?

Question: As I've gotten more familiar with Drupal, I've been utilizing template files more and more. However, the naming conventions for template files make them difficult to organize. Is it possible to organize template files into different directories within the primary templates directory? I'm using Drupal 7.
If it isn't possible, are there alternative methods to organize template files?
Thanks!
Answer:
Is it possible to organize template files into different directories within the primary templates directory?

Yes, absolutely. Drupal will recursively search the root and every subfolder in your theme for .tpl.php files.
Stick them in whatever structure suits you, Drupal will find them.


Weird CSS background image issue with Google Cloud Storage

Question: I have images stored in Google Cloud Storage and used in an Ionic / Angular app. The images are shown as a background of a div element. They're mostly showing ok but in some cases, the image is missing. And it's consistently specific images that are missing - not others - no matter how many times the pages are reloaded.
Right-clicking on the background image to get the background URL and then pasting that URL in the browser results in the image displaying fine.
The scss code contains the basics styling of the image and the background image URL is added inline within the HTML file.
item.scss
    offer {
    .card-image{
        background-color:lightgray;
        background-position:center;
        background-size:cover;
        height:10rem;
        width:100%;
    }
}

item.html
    <ion-card>

  <ion-item text-wrap>

{{ offer?.name }}

{{ offer?.provider }}

  </ion-item>

</ion-card>

And here's what it looks like rendered with the middle item's image absent...
Comment: What browser are you using for testing?
Comment: Chrome with the Dev console on on a mac
Comment: Sorry meant the developer inspector is open (prevents caching)
Comment: Did you try in other browsers? I apologize for the basic question, but whenever there are odd rendering problems, it's the equivalent of "Did you try turning it off and on again?"
Comment: Hey Aaron, it's a good point. Been trying it on different browsers and devices yes. So safari and chrome on iOS and also chrome in normal and incognito modes on osx. Also the web app being also an ionic app is being rendered within a system browser within the context of an actual iOS app... behaviour remains the same
Comment: Instead of copy/pasting, did you try right-clicking on the URL in the DOM or Styles explorers and click open in new tab?
Comment: It's now resolved. Looks like there was a problem with URL escaping :s
Answer: Ok, I think I see the problem. After a bit of head-scratching, it turns out that there was some error with escaping the URL of the background image. So any image with a space in its name was absent.
Solution: change the file names to avoid spaces or escape URL's. It's now working as expected!


Definir funções para valores iguais e valores em ordem crescente - Python

Question: Olá, defini uma função para leitura de 100 fichas de registros, contendo nome, sexo e idade, e agora necessito de uma função separada para mostrar na tela quantas pessoas são do sexo masculino e outra função separada para mostrar o vetor em ordem crescente de idade. Como poderia ser feito? Obrigado.
#Fazendo a ficha com nome sexo e idade
def leitura_registros():
    registros = []
    for x in range(100):
        fichaderegistros = {
        "Nome": input("Digite o nome: "),
        "Sexo": input("Digite o sexo: "),
        "Idade": int(input("Digite a idade: "))
        }
        registros.append(fichaderegistros)
    return registros

if __name__ == '__main__':
    # Faz a leitura dos registros
    registros = leitura_registros()

Answer: Para contar quantas são de um determinado sexo, a lógica é bem simples: percorrer a lista e quando encontrar um registro com o sexo desejado, adicionar um contador. Basicamente, ficaria assim:
def contar_por_sexo(registros, sexo):
    resultado = 0
    for registro in registros:
        if registro['Sexo'] == sexo:
            resultado += 1
    return resultado

qtd_homens = contar_por_sexo(registros, 'M')
print(f'Há {qtd_homens} na lista')

Já para ordenar por ordem crescente, basta utilizar a função nativa sorted:
def ordenar_por_idade(registros):
    return sorted(registros, key=lambda it: it['Idade'])

Comment: @JoãoMoreira basta percorrer a lista retornada por `ordenar_por_idade` e exibir com `print`. Deixo isso a seu cargo.
Answer: Não testei, mas acredito que é bem simples e deve funcionar:
#Fazendo a ficha com nome sexo e idade
registros = []
masculinos = []
idadesCrescente = []

def leitura_registros():

for x in range(100):
    fichaderegistros = {
    "Nome": input("Digite o nome: "),
    "Sexo": input("Digite o sexo F ou M: "),
    "Idade": int(input("Digite a idade: "))
    }
    registros.append(fichaderegistros)
    return registros

if __name__ == '__main__':
# Faz a leitura dos registros
    registros = leitura_registros()

def sexoMasculino(registros):

    for i in registros:
        if i == "M" or i == "m":
            masculinos.append(i)

def idadesCrescente(){
    cont = 0   # cont serve como uma chave, 0 fechada e 1 aberta
    for a in registros:
    if cont != 0: #Quando cont for zero, não entra nesse if
            idadesCrescentes.append(a)
            cont = 0 #Após armazenar o valor a chave é fechada

    if a == "M" or a == "m":
            cont = 1 # se as condições acima forem verdadeiras, significa que o termo seguinte será o valor de idade e a chave será liberada para o próximo loop.

    return idadesCrescentes.sort()
    }

Considerações:
A função sexoMasculino() irá fazer uma varredura pelo vetor registros e verificará em qual posição encontra-se o valor M, m, F ou f e assim armazenar em outro vetor, logo será armazenado somente o valores de sexo.
Para a função idadesCrescente(), será verificada se o termo de sexo foi encontrado e se sim, sabemos que o valor seguinte será a idade, logo a chave é liberada e no loop seguinte o if de idade será true e armazenará a idade. Após a função .sort fará a organização em ordem crescente dos valores.
Agora é só chamar os métodos onde quiser e sem precisar passar nenhum parâmetro. Qualquer coisa avisa ai
Comment: Sua função de ordenar pela idade não pareceu fazer muito sentido. Poderia explicá-la?


O que é e para que serve uma função anônima em R?

Question:
O que é uma função anônima? E por que pode ser chamada de função lambda?
Qual é a utilidade de uma função anônima na linguagem R?
Onde ela pode ser aplicada (por exemplo, pode ser aplicada em function operators)?
Comment: Acho que boa parte das respostas estão neste [link](http://adv-r.had.co.nz/Functional-programming.html).
Comment: Fica a dica aí pra quem quiser preparar um conteúdo a respeito disso em português : )
Answer: O que é função anônima em R?

Uma função anônima Anonymous Function (também conhecida como expressão lambda) é uma definição de função que
não está vinculada a um identificador. Ou seja, é uma função criada e
usada, mas nunca atribuída a uma variável. Ou seja, R não possui uma sintaxe especial para criar uma função nomeada: quando você cria uma função, usa o operador de atribuição regular para atribuir um nome. Se você optar por não atribuir um nome à função, receberá uma função anônima.

E por que pode ser chamada de função lambda?

Expressões lambda são funções sem nome dadas como valores constantes. Eles podem aparecer em qualquer lugar que qualquer outra constante, mas são tipicamente escritos como um parâmetro para alguma outra função.
A palavra "lambda" ou "expressões lambda" geralmente se
refere a funções anônimas. Então, nesse sentido, um lambda é um tipo
de função (função anônima), mas nem toda função é um lambda (ou seja,
funções nomeadas não são geralmente chamadas de lambdas).
Logo, funçôes anônimas em multas linguagens são tratadas como lambda.

Qual é a utilidade de uma função anônima na linguagem R?
Exemplo simples de função anônima em r:
# Sintaxe de função anônima
(function(x) x * 10)(10)

# equivalente (normal)
fun<-function(x) x * 10

fun(10)

Como a sintaxe acima indica, você está fazendo tudo de uma só vez: a
declaração e a chamada em uma declaração de linha. Portanto, apesar
de não ser transparente ao lê-lo, ele é autocontido e você o usa
porque não quer definir mais nenhuma outra função em algum outro
script atual (ou em um script externo): você está lidando com um
simples cálculo quando for necessário, e você provavelmente não o
usará em nenhum outro lugar do seu código, portanto, não vale a pena
lembrá-lo.


Help with inequality for real numbers: $||x+y|^t-|x|^t-|y|^t|\leq C(|x|^{t-1}|y|+|x||y|^{t-1})$

Question: I have a problem with proving the following inequality
$$||x+y|^t-|x|^t-|y|^t|\leq C(|x|^{t-1}|y|+|x||y|^{t-1})$$
where $t>1$ and $x,y$ are real numbers and $C$ is only dependent on $t$.
I tried to use the Hölder inequality $|a|^s|b|^{1-s}\leq s|a|+(1-s)|b|$, triangle inequality and about any other trick I know of, but have been getting nowhere.
The closest I got is
$$
LHS\leq (2^{t-1}+1)(|x|^t+|y|^t)+2^{t-1}(|x|^{t-1}|y|+|x||y|^{t-1})
$$
but I don't think I can improve it to be $\leq RHS$.
Answer: I am not sure that this is the most straightforward approach, or that there is any way of using known inequalities to get the result, but at least it seems to give a proof of the stated inequality.
Wlog we can assume that both $x$ and $y$ are different from zero. Indeed, if not, the stated result reduces to the elementary inequality $0 \leqslant 0$, which is obviously true. Start by distinguishing two cases: either both $x$ and $y$ have the same sign or not. Since $t > 1$, it is easy to see that
$$
|x+y|^t - |x|^t - |y|^t \geqslant 0
$$
holds in the first case, while the opposite inequality is true if $xy < 0$.
Case I ($\mathbf{xy > 0})$: In this case we want to show
$$
|x+y|^t-|x|^t-|y|^t \leqslant  C(|x|^{t-1}|y|+|x||y|^{t-1}).
$$
By symmetry we can further assume that $x \leqslant y$. Dividing both terms by $|y|^{t}$ and setting $z = \frac{x}{y}$ we are left to prove the following inequality
$$
|z+1|^t - |z|^t  - 1 \leqslant C ( |z|^{t-1} + |z| )
$$
under the constraint $0 \leqslant z \leqslant 1$. Basic calculus then gives that there exists a constant $C$ such that
$$
(1+z)^t \leqslant 1 + C z + C z^{t-1} + z^t
$$
holds. Indeed, the function $(1+z)^t$ is continuous with bounded derivative for $z \in [0,1]$ and $t >1$. Moreover, for $z = 0$ identity holds in the above relation. Therefore choosing $C$ sufficiently large we deduce
$$
(1+z)^t \leqslant 1 + C z
$$
for $z \in [0,1]$ which implies the desired estimate.
Case II ($\mathbf{xy < 0})$: Wlog we can assume $|x| \geqslant |y|$ and $x > 0$. Arguing as in the previous case, the desired inequality can be reduced to
$$
|z|^t + 1 \leqslant |z-1|^t + C|z|^{t-1} + C|z|
$$
for $z \in [0,1]$. Again this follows easily since the function $|z|^t + 1$ is continuous with bounded derivative and identity holds for $z = 0$.
P.S.: The first case in the argument above seems to give $C = 2^t-1$ as best possible constant. However, this does not appear to be optimal in general.


Under ExtJS 4 is there a way to load external components?

Question: I am trying to set up my ExtJS 4 project so I have three top level applications, for example,

/foo/app.js
   /bar/app.js
   /baz/app.js

Each 'top level' application is a separate ExtJS application, each with their own loaders. There are some cases were I will have general components that I want to share between all three applications, so I have /components top level directory.
If I have a component name say ComponentA,

/components/componenta.js

How would I go about getting ComponentA into all three applications, so it could referenced or extended by the individual application?
Answer: You can add this same loader into each of the applications app.js file.
Ext.Loader.setPath('Common', '../components/');

Then give your common components the same namespace when you define them and place them in the components top level folder.
componenta.js
Ext.define('Common.componenta', {
    extend: 'Ext.panel.Panel',
    title: 'Component A'
});

Then the Common namespace will be available for you to extend in each application
Ext.define('Foo.panel', {
    extend: 'Common.componenta',
    title: 'Foo Panel'
});

Comment: Upvoted.Here is a recent discussion on the Sencha forums that describe exactly this pattern in greater detail. http://www.sencha.com/forum/showthread.php?238496-How-to-extend-a-Ext.app.Application&p=875700
Comment: Thanks, that was a lot more straight forward then I thought it would be.


How does The Talkbox work?

Question: Beck started using this in the early 70's - they called it The Bag for a hot minute (as did Stevie Wonder) - Frampton's career came alive when he started using it - how does it work?
Answer: A talkbox is a small, self-contained amplifier and speaker assembly. There's a tiny little speaker in it and the speaker, instead of moving air in a room, moves the air in a plastic tube that it's sealed against. You run your guitar in to the talkbox and
the plastic tube you insert in your mouth. So your mouth becomes a resonant cavity for the guitar sounds coming out of the tiny speaker.
You aim your mouth at a microphone, play your guitar, and by changing the shape of your mouth and the size of your open mouth, you can make it act like a filter on your guitar signal. Your mouth becomes the equalizer with far more degrees of freedom than something like a wah might offer.
Traditionally you'd "play" the talkbox in to a normal vocal mic and it'd be amplified at the front of the house just like your vocals. Because you're not using a tube amp or anything like that you'd run your guitar in to a fuzz pedal (like Frampton did when he Came Alive) or some such thing to dirty it up a bit (although the tiny speaker had a pretty good break up all on its own).
Rocktron has modernized the talkbox a bit with their Banshee 2 and they let you continue to feed your guitar signal to your guitar amp while playing the talkbox into the vocal mic to route to the front of house. But the speaker-driving-a-plastic-tube-to-your-mouth design has remained largely unchanged since it's inception.
Answer: When you talk into it you are transmitting sound(changing air pressure) into a microphone(essentially). This is used to modulate(or control in some way) another signal such as a guitar. It's based on the same principles as a vocoder(and essentially is one).
Comment: A talkbox is not a vocoder, not even essentially. And it's not based on the same principals (unless you get really loose about "same"). See: http://en.wikipedia.org/wiki/Vocoder
Comment: I think they're taking issue with your phrase that a talkbox is basically a vocoder, which isn't really true. In your comments you're arguing that a vocoder and talkbox are based on the same principle, modulation, which is true. Understanding modulation will get you a lot further than just a talkbox and vocoder (chorus, flange, wah, phaser, tremolo), so I think your point wasn't particularly clear in your answer.
Comment: I'm also not so sure how useful the point is when explaining it to someone that doesn't even know the basics of the effect. Saying it's the same as FM radio may be true at an abstract mathematical level, but it's getting a little on the esoteric side in a discussion of guitar effects.
Comment: Effectively the same thing(mathematically the same modulation principle is taking place). A talkbox takes the input signal into the mouth, the mouth applies a modulation to the signal and it is output to a microphone. A vocoder takes a signal and modulates it electronically. So yes, they are based on the same principles, that of modulation.
Comment: I'm with @Ian C. on this. The vocorder results in a somewhat-similar sound, but how it works is entirely different. The vocoder tries to do what the talkbox is doing, but, well, it's comparing the original sound, as far as guitarists are concerned, with an interloper. Technically, the vocoder is more capable and can be controlled by more sources such as MIDI or control voltages, but I've never heard one come close to the sound of a cranked up talk box rattling the fillings of a guitarist.
Comment: It has nothing to do with how they sound but based on the physical and mathematical principles. You can have to things that work very differently sound similar and two things that work very similar sound completely different. For example, AM radio and FM radio, by your definition, would give two completely different results. Yet they are based on the same mathematical principles(almost identical actually). The only difference is one modulates the frequency of a carrier wave and the other modulates the amplitude.
Comment: Both have to do with modulation and all modulation effects have a simple mathematical representation. For me, I see them all as different colors of the same thing because they are all represented in the same mathematical way.  Now I can understand it will be difficult for you to understand this if you have trouble abstracting and see the mathematical nature of things but it doesn't mean your right. Now maybe I am generalizing a bit too much for this forum but the reason I did that is that if the original poster understands modulation(has experience with it in other areas) he should easily
Comment: Math is a **tool** to help us understand physical phenomenon, math itself does not **create** anything physical. Yes, the same mathematical tools can be used to create transfer functions that attempt to describe what is happening in a vocoder, a transistor, and a talkbox. The tools however are imperfect. They do not represent physical phenomenon perfectly and, more importantly, they fail to describe the fundamentally **huge** differences in the four physical object's implementation and use. (con't...)
Comment: (...con't) And really, that is what the OP is asking about. How is it *physically* implemented and used. So math is not the complete answer, though it could be some part of it. Your answer in particular is so vague it could be dropped in verbatim for any "How does effect X work?" It says nothing really. If you want to answer with math, I suggest you at least answer with the *specific* transfer function that describes the behaviour of the effect.
Comment: Um, Math and physics go hand and hand. Quantum Mechanics requires that physically observable "things" have corresponding hermitian operators. Mathematics isn't a "tool" unless you think "thinking" is a tool. Mathematics is a very precise descriptive language. I never once said math created things. But the concept of modulation is pretty accurately understood as a mathematical construction. Only people that do not have the mathematics background can be confused by that.
Answer: There are actually 2 basic types of "talkbox".  The original one is exactly as explained in the first post.  Basically, the amplified guitar signal is fed through a tube.  The tube goes into your mouth, vibrating any dental work you may have (just kidding).  When you move your mouth, the sound is changed, creating a vocal-type effect.  This effect is amplified if you stand in front of a microphone (which of course must be attached to a working sound system).
The second - more recent - type of talkbox still uses a tube to "modulate" the sound, but the amplified guitar tone does not go through the tube and into your mouth.  Therefore, it does not require a microphone.  Instead, it works more like a vocoder, where you speak into the tube and the guitar sound is changed accordingly.  I think that this is actually a vocoder that is being marketed as a talkbox, so the line does blur a bit here.

How does The Talkbox work?


Ignore or remove object with null in reduce

Question: I have the following in my js code:
// Format the links
const newLinks = Object.keys(this.props.mission.links).reduce((array, key) => {
  return [...array, {url: this.props.mission.links[key], name:key}]
}, []);

which creates my object like so:
{ url: "https://i.imgur.com/03gonKW.png", name: "mission_patch" }

but there are cases where the url is null how can I ignore that so it's not created in the object meaning if the url is null it should skip it.
Comment: how about ` return [...array, {url: this.props.mission.links[key] !== undefined ? this.props.mission.links[key] : ""  , name:key}] ` ?
Comment: Maybe this `return this.props.mission.links[key] ?
   [...array, {url: this.props.mission.links[key], name:key}] : array;`
Comment: exactly, use a `ternary op` OR `if()` to judge the `url`
Comment: Thanks @Hassan Imam that worked
Answer: The simplest solution would be to add a condition in your reducer:
const newLinks = Object.keys(this.props.mission.links).reduce((array, key) => {
  if(!this.props.mission.links[key]) {
    return array;
  }
  return [...array, {url: this.props.mission.links[key], name:key}]
}, []);

But, note that this case (in general) matches map better then reduce:
const links = this.props.mission.links;
const newLinks = Object.keys(links)
  .filter(key => !!links[key])
  .map(key => ({url: links[key], name: key}));

Answer: Need to do the following
return this.props.mission.links[key] ? [...array, {url: this.props.mission.links[key], name:key}] : array;

Answer: You can apply filter first to get only keys with not null values:
let filteredKeys = Object.keys(this.props.mission.links).filter((key) => !!this.props.mission.links[key])
And then apply your reduce, you can also chain these operations:
let result = Object.keys(this.props.mission.links).filter((key) => !!this.props.mission.links[key]).reduce((array, key) => { return [...array, {url: this.props.mission.links[key], name:key}] }, [])


Consigo preencher um DataGrid com um List?

Question: Como faço para preencher um DataGrid a partir de um List tipado?
Seria possível, gerar as colunas automaticamente?
Comment: Caique, para complementar a resposta, deixe um comentário. O autor decide se quer ou não incluir o conteúdo que você sugere.
Comment: Para complementar sua pergunta, edite a sua pergunta, não a resposta dada. Para adicionar uma resposta, dê a sua própria resposta.
Comment: Ah, ok. Desculpe.
Answer: Sim ItemSource do DataGrid (vi que o seu caso é WPF) você consegue passar qualquer enumeração (IList, ICollection, etc)
DataGrid1.ItemsSource = (new List<T>());

Aonde new List<T>() é a sua lista.
Para habilitar a criação automática de colunas, coloque True em AutoGenerateColumns no seu DataGrid do WPF.
Comment: Obrigado cara, ajudou bastante. Eu fiz como disse e quando executei, nada... percebi que havia três linhas no DataGrid, mas não tinha coluna alguma. Procurei alguma propriedade ou método que gerasse, achei o AutoGenerateColumns e deixei como true e funcionou! Obrigado :)
Comment: Ok, que bom que deu certo


Erro ao alterar registro na tabela

Question: Consigo alterar manualmente, porém no projeto da erro..
O Erro:
Cannot add or update a child row: a foreign key constraint fails (sistema.veiculo, CONSTRAINTveiculo_ibfk_1FOREIGN KEY (fk_idassociado) REFERENCESassociado(ID_ASSOCIADO`) ON DELETE CASCADE)

tela editar:
public class Edit_Veiculo extends javax.swing.JFrame {

     Conexao CONEXAO = new Conexao();
    DefaultListModel MODELO;
    int Enter = 0;

     String [] Codig;

    public Edit_Veiculo() {
        initComponents();

         //tela maximizada
        this.setExtendedState(MAXIMIZED_BOTH);
        Lista.setVisible(false);
        //listagem
        CONEXAO.conecta();
        MostraPesquisa();

        MODELO = new DefaultListModel();
        Lista.setModel(MODELO);
    }

 private void PesquisaPlacaMousePressed(java.awt.event.MouseEvent evt) {
        MostraPesquisa();
        Lista.setVisible(false);
    }

private void PesquisaPlacaActionPerformed(java.awt.event.ActionEvent evt) {
        Lista.setVisible(false);
        Enter = 1;
    }

    private void PesquisaPlacaKeyReleased(java.awt.event.KeyEvent evt) {
        if(Enter == 0)
        ListadePesquisa();
        else
        Enter = 0;
    }

    private void ListaMousePressed(java.awt.event.MouseEvent evt) {
        MostraPesquisa();
        Lista.setVisible(false);
    }

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
try{
        Associado associado = new Associado();
        Veiculo veiculo = new Veiculo();
        VeiculoDao vdao = new VeiculoDao();

        veiculo.setIdveiculo(Integer.parseInt(idveiculo.getText()));

        veiculo.setPlaca(placa.getText());
        veiculo.setRenavam(renavam.getText());
        veiculo.setMarca(marca.getText());
        veiculo.setModelo(modelo.getText());
        veiculo.setChassi(chassi.getText());
        veiculo.setAno(Integer.parseInt(ano.getText()));
        veiculo.setCor(cor.getText());

        veiculo.setAssociado(associado);

        vdao.alterarVeiculo(veiculo);
}catch (Exception ex){
     Logger.getLogger(Edit_Veiculo.class.getName()).log(Level.SEVERE, null, ex);
           JOptionPane.showMessageDialog(null, "Erro ao alterar." + ex);
}
    }

public void ListadePesquisa(){
 try {
            CONEXAO.executaSQL("SELECT * FROM veiculo where placa like '" + PesquisaPlaca.getText() + "%' ORDER BY placa");
            MODELO.removeAllElements();
            int v = 0;
            Codig = new String[4];
            while (CONEXAO.resultset.next() & v < 4) {
                MODELO.addElement(CONEXAO.resultset.getString("placa"));

                Codig[v] = CONEXAO.resultset.getString("idveiculo");
                v++;
            }
            if (v >= 1) {
                Lista.setVisible(true);
            } else {
                Lista.setVisible(false);
            }

            ResultadoPesquisa();
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao listar dados" + erro);
        }
}

 public void MostraPesquisa() {
        int Linha = Lista.getSelectedIndex();
        if (Linha >= 0) {
            CONEXAO.executaSQL("SELECT * FROM veiculo where idveiculo = "+Codig[Linha]+" ");
            ResultadoPesquisa();
        }

    }

 public void ResultadoPesquisa() {

        try {
            CONEXAO.resultset.first();
           //dados associado
            idveiculo.setText(CONEXAO.resultset.getString("idveiculo"));
            numcota.setText(CONEXAO.resultset.getString("fk_idassociado"));
            placa.setText(CONEXAO.resultset.getString("placa"));
            renavam.setText(CONEXAO.resultset.getString("renavam"));
            marca.setText(CONEXAO.resultset.getString("marca"));
            modelo.setText(CONEXAO.resultset.getString("modelo"));
            chassi.setText(CONEXAO.resultset.getString("chassi"));
            ano.setText(CONEXAO.resultset.getString("ano"));
            cor.setText(CONEXAO.resultset.getString("cor"));

        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao pesquisar." + erro);
        }
    }

}

tabelas:
CREATE TABLE `associado` (
  `ID_ASSOCIADO` int(255) NOT NULL,
  `NOME` varchar(50) NOT NULL,
  `CPF` varchar(14) NOT NULL,
  `RG` varchar(13) NOT NULL,
  `CELULAR` int(30) NOT NULL,
  `TELEFONE` int(30) NOT NULL,
  `DATA_NASC` varchar(10) NOT NULL,
  `ESTADO` char(2) NOT NULL,
  `CIDADE` varchar(50) NOT NULL,
  `CEP` int(9) NOT NULL,
  `ENDERECO` varchar(50) NOT NULL,
  `BAIRRO` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `veiculo` (
  `idveiculo` int(11) NOT NULL,
  `fk_idassociado` int(11) NOT NULL,
  `placa` varchar(7) NOT NULL,
  `renavam` varchar(11) NOT NULL,
  `marca` varchar(100) NOT NULL,
  `modelo` varchar(50) NOT NULL,
  `chassi` varchar(8) NOT NULL,
  `ano` int(4) NOT NULL,
  `cor` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `associado`
  ADD PRIMARY KEY (`ID_ASSOCIADO`);

ALTER TABLE `veiculo`
  ADD PRIMARY KEY (`idveiculo`),
  ADD KEY `fk_idassociado` (`fk_idassociado`);

 ALTER TABLE `veiculo`
      MODIFY `idveiculo` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;

ALTER TABLE `veiculo`
      ADD CONSTRAINT `veiculo_ibfk_1` FOREIGN KEY (`fk_idassociado`) REFERENCES `associado` (`ID_ASSOCIADO`)
      ON DELETE CASCADE;

método alterar:
public void alterarEngate(Engate eng) {
        Transaction tx = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            session.update(eng);
            session.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Alterado com sucesso!");
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

print da tela:
Comment: parece que o problema é no mapeamento da entidade na constraint veiculo_ibfk_1FOREIGN
Comment: Considerando `Associado associado = new Associado();`. E depois `veiculo.setAssociado(associado);` a impressão que passa é que o associado do objeto `veiculo` é 0. Pode conferir isso?
Comment: o problema está no código pois consigo alterar os registros manualmente..
Answer: Boa tarde,
Acho que vc poderia colocar mais do seu código. O método todo, por exemplo. Mas aparentemente vc está tentando salvar ou atualizar um registro do veículo com um registro de associado que não existe.
Primeiramente:
    veiculo.setAssociado(veiculo.getAssociado());

Não faz mt sentido o que está escrito no trecho acima. Vc está definindo o associado do veículo com o associado que o próprio veículo já possui. (?)
Voltando ao seu problema. Sugiro que vc debugue seu código, veja o ID do associado que está sendo setado no seu veículo. Com o ID, busque por ele no banco de dados para confirmar se ele realmente existe. Caso ele não exista, está resolvido o problema: A chave estrangeira (ID_ASSOCIADO) não existe na tabela de associados. Vc terá que fazer algum tratamento em relação à isso ou alterar sua lógica.
Mas tente colocar mais do seu código pro pessoal te ajudar melhor.
Comment: Obrigado pela ajuda mas verificar se o id existe na tabela pai foi a primeira coisa que fiz, e existe sim! vou atualizar a pergunta com os códigos..
Comment: Em que momento do código vc está definindo o associado? Pq olhando ali no código quando vc faz     veiculo.setAssociado(associado);' o valor de associado é vazio. Não foi atribuído nada à ele após a declaração Associado associado = new Associado();
Comment: Não preciso dessa linha? desculpe, sou iniciante..
Comment: Precisa sim, mas o seu objeto está vazio. Vc simplesmente criou ele. Tente colocar valores nele para que ele possa ser localizado no banco. Depois de Associado associado = new Associado(); coloque o valor do associado que está no banco e será buscado, por exemplo: associado.setID(1);
Comment: Consegui! Obrigado pela ajuda!!

Erro ao alterar registro na tabela


apache http 80 .htacess, https 443 .htaccess does not work

Question: When I run api.troop37bsa.org/user, I am able to access the page but when I run https://api.troop37bsa.org/user, I get a 404 error.  When I run api.troop37bsa.org/?url=user or the secure version I get the correct page.
I am using a .htaccess file to rewrite the url so that everything after the subdomain is considered a parameter string.
So my question is two fold.  How can I get https://api.troop37bsa.org/user  to work and also what can I do to reduce the size of my .conf file and reduce or merge my .htaccess file.
I have an Apache Server 2.4.18.
My .conf file is setup as follows:
<VirtualHost *:80>

        ServerAdmin firstname.lastname@example.com
        ServerName api.troop37bsa.org
        ServerAlias api.troop37bsa.org
        DocumentRoot /var/www/troop37bsa.org/api/public

        <Directory /var/www/troop37bsa.org/api/public>
                DirectoryIndex index.php index.html
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # Uncomment this directive is you want to see apache2's
                # default start page (in /apache2-default) when you go to /
                #RedirectMatch ^/$ /apache2-default/
        </Directory>
        AccessFileName .htaccess

        ErrorLog /var/www/troop37bsa.org/api/public/apache_error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

#RewriteEngine on
#RewriteCond %{SERVER_NAME} =api.troop37bsa.org
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

</VirtualHost>

<VirtualHost *:443>

        ServerAdmin firstname.lastname@example.com
        ServerName api.troop37bsa.org
        ServerAlias api.troop37bsa.org
        DocumentRoot /var/www/troop37bsa.org/api/public

        <Directory /var/www/troop37bsa.org/api/public>
                DirectoryIndex index.php index.html
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                allow from all
                # Uncomment this directive is you want to see apache2's
                # default start page (in /apache2-default) when you go to /
                #RedirectMatch ^/$ /apache2-default/
        </Directory>
        AccessFileName .htaccess
        ErrorLog /var/www/troop37bsa.org/api/public/apache_https_error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

In my subdomain folder (ie /var/www/troop37bsa.org/api/public) I have an .htaccess file
ReWriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.+)$ %2index.php?url=$1 [QSA,L]

I am also using a letsencrypt SSL certificate that otherwise seems to be working fine.
Thanks
Comment: Have you tried disabling .htaccess altogether with AllowOverride none (since you seem to have access to the virtualhost and not need it anyways) and just add a simple "FallBackResource /index.php" in the VirtualHost config for both virtualhosts? If you insist on using that set of rewrites the last condition avoids any normal uri to end up at index.php as you seem to want, that's why /user will give you a 404 if it does not exist.
Comment: @ezra-s Thank you.  That is what I was going for.  The main difficulty seems to have been a .conf file that I did not see before.  It was apparently generated by letsencrypt.  Anyway it was pointing to /publicl not publc.  I removed the l, and removed the virtual host  *443 and the entire site works as expected.
Comment: glad you solved it.
Answer: As I said in the comment section, It turns out that there was a conf file generated by letsencrypt, but it was pointing to the wrong directory publicl instead of public.  When I changed the directory name. the .htaccess file did its job.  I also removed the second *.443 virtualHost from my orginal conf to avoid further conflicts.
So others may not fall into the same problem the name of the conf was api.troop37bsa.org-le-ssl.conf.  This set up the look ups from the SSL certificate.
the api.troop37bsa.org-le-ssl.conf code is as follows:
<IfModule mod_ssl.c>
<VirtualHost *:443>

        ServerAdmin firstname.lastname@example.com
        ServerName api.troop37bsa.org
        ServerAlias api.troop37bsa.org
        DocumentRoot /var/www/troop37bsa.org/api/public

        <Directory /var/www/troop37bsa.org/api/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # Uncomment this directive is you want to see apache2's
                # default start page (in /apache2-default) when you go to /
                #RedirectMatch ^/$ /apache2-default/
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLCertificateFile /etc/letsencrypt/live/troop37bsa.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/troop37bsa.org/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

Thank you everyone for your help.


How to split the dataset for cross validation, learning curve, and final evaluation?

Question: What is an appropriate strategy for splitting the dataset?
I ask for feedback on the following approach (not on the individual parameters like test_size or n_iter, but if I used X, y, X_train, y_train, X_test, and y_test appropriately and if the sequence makes sense):
(extending this example from the scikit-learn documentation)
1. Load the dataset
from sklearn.datasets import load_digits
digits = load_digits()
X, y = digits.data, digits.target

2. Split into training and test set (e.g., 80/20)
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

3. Choose estimator
from sklearn.svm import SVC
estimator = SVC(kernel='linear')

4. Choose cross-validation iterator
from sklearn.cross_validation import ShuffleSplit
cv = ShuffleSplit(X_train.shape[0], n_iter=10, test_size=0.2, random_state=0)

5. Tune the hyperparameters
applying the cross-validation iterator on the training set
from sklearn.grid_search import GridSearchCV
import numpy as np
gammas = np.logspace(-6, -1, 10)
classifier = GridSearchCV(estimator=estimator, cv=cv, param_grid=dict(gamma=gammas))
classifier.fit(X_train, y_train)

6. Debug algorithm with learning curve
X_train is randomly split into a training and a test set 10 times (n_iter=10). Each point on the training-score curve is the average of 10 scores where the model was trained and evaluated on the first i training examples. Each point on the cross-validation score curve is the average of 10 scores where the model was trained on the first i training examples and evaluated on all examples of the test set.
from sklearn.learning_curve import learning_curve
title = 'Learning Curves (SVM, linear kernel, $\gamma=%.6f$)' %classifier.best_estimator_.gamma
estimator = SVC(kernel='linear', gamma=classifier.best_estimator_.gamma)
plot_learning_curve(estimator, title, X_train, y_train, cv=cv)
plt.show()

plot_learning_curve() can be found in the current dev version of scikit-learn (0.15-git).
7. Final evaluation on the test set
classifier.score(X_test, y_test)

7a. Test over-fitting in model selection with nested cross-validation (using the whole dataset)
from sklearn.cross_validation import cross_val_score
cross_val_score(classifier, X, y)

Additional question: Does it make sense to replace step 7 by nested cross-validation? Or should nested cv be seen as complementary to step 7
(the code seems to work with k-fold cross validation in scikit-learn, but not with shuffle & split. So cv needs to be changed above to make the code work)
8. Train final model on whole dataset
classifier.fit(X, y)

EDIT: I now agree with cbeleites that step 7a doesn't make much sense in this sequence. So I wouldn't adopt that.
Comment: I am almost certain that F1 is a new name for an old concept.  I think it is counterproductive to invent new names for old things.  More importantly, it is an improper scoring rule that will result in selection of the wrong features as well as adding a good deal of noise into the whole process.
Comment: ... in any case F1 shares the problems of accuracy @FrankHarrell alludes to: these  stem from counting fractions of test cases of hard classifcations. To get one of Frank's proper scoring rules, you'd need to switch to probabilistic output of the SVM, and then e.g. use Brier's score (mean squared error) instead of accuracy. I guess you could also derive a MSE-type version of F1. Such measures should indeed be better for the tuning step. For communicating the final performance, you may also need the typical ways (e.g. accuracy, F1) of expressing performance for your community.
Comment: @ta.ft: whether the approach is wrong or not depends on what you consider wrong: grid search on proportions has a serious risk of skimming variance unless you have ridiculously large numbers of independent cases. So, for many situations the claim that grid search yields the optimal model is wrong. However, if you do a proper nested validation, the outer validation gives an honest measure of the chosen "optimal" model's performance. So that is not wrong. You just don't have a guarantee that the grid search got the optimal model. As for literature, I'll update my answer.
Comment: Which accuracy scoring rule are you using?  If it is classification accuracy, such an improper scoring rule will undo much of the work you've gone to.
Comment: I used the default which is indeed classification accuracy. I know that, e.g., F1 would be more appropriate. But here I'm just interested if the splits are used OK.
Comment: The accuracy measure makes such a huge impact that other choices may not be relevant enough to spend time on.  And what is F1?
Comment: From the [scikit documentation](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html#sklearn.metrics.f1_score): "F1 score, also known as balanced F-score or F-measure. The F1 score can be interpreted as a weighted average of the precision and recall." I'm happy to change the scoring method in the above code, but I'd still be curious about the splitting.
Comment: Thanks Frank Harrell and @cbeleites. I never read that the F1-score is inappropriate for hyperparameter tuning. Then [this example](http://scikit-learn.org/dev/auto_examples/grid_search_digits.html#example-grid-search-digits-py) in the scikit-learn documentation using precision and recall for tuning is wrong?  Do you have a reference for correct scoring rules such as Brier's score for the tuning step?
Comment: @tobip, What is the utility of 8)? and in the 7) is the score will be calculated with the best estimator?
Comment: May I ask why a grid search over gamma is executed although you're using an SVM linear kernel?
Answer: I'm not sure what you want to do in step 7a. As I understand it right now, it doesn't make sense to me.
Here's how I understand your description: in step 7, you want to compare the hold-out performance with the results of a cross validation embracing steps 4 - 6. (so yes, that would be a nested setup).
The main points why I don't think this comparison makes much sense are:

This comparison cannot detect two of the main sources of overoptimistic validation results I encounter in practice:

data leaks (dependence) between training and test data which is caused by a hierarchical (aka clustered) data structure, and which is not accounted for in the splitting. In my field, we have typically multiple (sometimes thousands) of readings (= rows in the data matrix) of the same patient or biological replicate of an experiment. These are not independent, so the validation splitting needs to be done at patient level. However, such a data leak occurs, you'll have it both in the splitting for the hold out set and in the cross validation splitting. Hold-out wold then be just as optimistically biased as cross validation.
Preprocessing of the data done on the whole data matrix, where the calculations are not independent for each row but many/all rows are used to calculation parameters for the preprocessing. Typical examples would be e.g. a PCA projection before the "actual" classification.
Again, that would affect both your hold-out and the outer cross validation, so you cannot detect it.

For the data I work with, both errors can easily cause the fraction of misclassifications to be underestimated by an order of magnitude!
If you are restricted to this counted fraction of test cases type of performance, model comparisons need either extremely large numbers of test cases or ridiculously large differences in true performance. Comparing 2 classifiers with unlimited training data may be a good start for further reading.

However, comparing the model quality the inner cross validation claims for the "optimal" model and the outer cross validation or hold out validation does make sense: if the discrepancy is high, it is questionable whether your grid search optimization did work (you may have skimmed variance due to the high variance of the performance measure). This comparison is easier in that you can spot trouble if you have the inner estimate being ridiculously good compared to the other - if it isn't, you don't need to worry that much about your optimization. But in any case, if your outer (7) measurement of the performance is honest and sound, you at least have a useful estimate of the obtained model, whether it is optimal or not.
IMHO measuring the learning curve is yet a different problem. I'd probably deal with that separately, and I think you need to define more clearly what you need the learning curve for (do you need the learning curve for a data set of the given problem, data, and classification method or the learning curve for this data set of the given problem, data, and classification mehtod), and a bunch of further decisions (e.g. how to deal with the model complexity as function of the training sample size? Optimize all over again, use fixed hyperparameters, decide on function to fix hyperparameters depending on training set size?)
(My data usually has so few independent cases to get the measurement of the learning curve sufficiently precise to use it in practice - but you may be better of if your 1200 rows are actually independent)

update: What is "wrong" with the the scikit-learn example?
First of all, nothing is wrong with nested cross validation here. Nested validation is of utmost importance for data-driven optimization, and cross validation is a very powerful approaches (particularly if iterated/repeated).
Then, whether anything is wrong at all depends on your point of view: as long as you do an honest nested validation (keeping the outer test data strictly independent), the outer validation is a proper measure of the "optimal" model's performance. Nothing wrong with that.
But several things can and do go wrong with grid search of these proportion-type performance measures for hyperparameter tuning of SVM. Basically they mean that you may (probably?) cannont rely on the optimization. Nevertheless, as long as your outer split was done properly, even if the model is not the best possible, you have an honest estimate of the performance of the model you got.
I'll try to give intuitive explanations why the optimization may be in trouble:

Mathematically/statisticaly speaking, the problem with the proportions is that measured proportions $\hat p$ are subject to a huge variance due to finite test sample size $n$ (depending also on the true performance of the model, $p$):
$Var (\hat p) = \frac{p (1 - p)}{n}$
You need ridiculously huge numbers of cases (at least compared to the numbers of cases I can usually have) in order to achieve the needed precision (bias/variance sense) for estimating recall, precision (machine learning performance sense). This of course applies also to ratios you calculate from such proportions. Have a look at the confidence intervals for binomial proportions. They are shockingly large! Often larger than the true improvement in performance over the hyperparameter grid.
And statistically speaking, grid search is a massive multiple comparison problem: the more points of the grid you evaluate, the higher the risk of finding some combination of hyperparameters that accidentally looks very good for the train/test split you are evaluating. This is what I mean with skimming variance. The well known optimistic bias of the inner (optimization) validation is just a symptom of this variance skimming.
Intuitively, consider a hypothetical change of a hyperparameter, that slowly causes the model to deteriorate: one test case moves towards the decision boundary. The 'hard' proportion performance measures do not detect this until the case crosses the border and is on the wrong side. Then, however, they immediately assign a full error for an infinitely small change in the hyperparameter.
In order to do numerical optimization, you need the performance measure to be well behaved. That means: neither the jumpy (not continously differentiable) part of the proportion-type performance measure nor the fact that other than that jump, actually occuring changes are not detected are suitable for the optimization.
Proper scoring rules are defined in a way that is particularly suitable for optimization. They have their global maximum when the predicted probabilities match the true probabilities for each case to belong to the class in question.
For SVMs you have the additional problem that not only the performance measures but also the model reacts in this jumpy fashion: small changes of the hyperparameter will not change anything. The model changes only when the hyperparameters are changes enough to cause some case to either stop being support vector or to become support vector. Again, such models are hard to optimize.

Literature:

 Brown, L.; Cai, T. & DasGupta, A.: Interval Estimation for a Binomial Proportion, Statistical Science, 16, 101-133 (2001).
 Cawley, G. C. & Talbot, N. L. C.: On Over-fitting in Model Selection and Subsequent Selection Bias in Performance Evaluation, Journal of Machine Learning Research, 11, 2079-2107 (2010).
 Gneiting, T. & Raftery, A. E.: Strictly Proper Scoring Rules, Prediction, and Estimation, Journal of the American Statistical Association, 102, 359-378 (2007). DOI: 10.1198/016214506000001437
Brereton, R.: Chemometrics for pattern recognition, Wiley, (2009).
points out the jumpy behaviour of the SVM as function of the hyperparameters.

Update II: Skimming variance
what you can afford in terms of model comparison obviously depends on the number of independent cases.
Let's make some quick and dirty simulation about the risk of skimming variance here:
scikit.learn says that they have 1797 are in the digits data.

assume that 100 models are compared, e.g. a $10 \times 10$ grid for 2 parameters.
assume that both parameter (ranges) do not affect the models at all,
i.e., all models have the same true performance of, say, 97 % (typical performance for the digits data set).
Run $10^4$ simulations of "testing these models" with sample size = 1797 rows in the digits data set
p.true = 0.97 # hypothetical true performance for all models
n.models = 100 # 10 x 10 grid

n.rows = 1797 # rows in scikit digits data

sim.test <- replicate (expr= rbinom (n= nmodels, size= n.rows, prob= p.true),
                       n = 1e4)
sim.test <- colMaxs (sim.test) # take best model

hist (sim.test / n.rows,
      breaks = (round (p.true * n.rows) : n.rows) / n.rows + 1 / 2 / n.rows,
      col = "black", main = 'Distribution max. observed performance',
      xlab = "max. observed performance", ylab = "n runs")
abline (v = p.outer, col = "red")

Here's the distribution for the best observed performance:

The red line marks the true performance of all our hypothetical models. On average, we observe only 2/3 of the true error rate for the seemingly best of the 100 compared models (for the simulation we know that they all perform equally with 97% correct predictions).
This simulation is obviously very much simplified:

In addition to the test sample size variance there is at least the variance due to model instability, so we're underestimating the variance here
Tuning parameters affecting the model complexity will typically cover parameter sets where the models are unstable and thus have high variance.
For the UCI digits from the example, the original data base has ca. 11000 digits written by 44 persons. What if the data is clustered according to the person who wrote? (I.e. is it easier to recognize an 8 written by someone if you know how that person writes, say, a 3?) The effective sample size then may be as low as 44.
Tuning model hyperparameters may lead to correlation between the models (in fact, that would be considered well behaved from a numerical optimization perspective). It is difficult to predict the influence of that (and I suspect this is impossible without taking into account the actual type of classifier).

In general, however, both low number of independent test cases and high number of compared models increase the bias. Also, the Cawley and Talbot paper gives empirical observed behaviour.
Comment: @ta.ft: two approaches are a) incorporate as much external knowledge about your application and data into the modeling in order to drastically reduce the number of models that need to be compared (= decide hyperparameters instead of optimizing). It may be overall better to change to a classifier that has intrinsically meaningful hyperparameters, i.e. where you can know from application and data type what the hyperparameter should (approximately) be. b) compare the few remaining models by proper scoring rule. E.g. Briers score has far better variance properties for many classifiers.
Comment: You can also refuse to optimize at all (via decisions (a)). If you get a good-enough classifier and can argue that you have no chance to prove superiority of another classifier given the available sample size (e.g. do some demo McNemar calculations, look up necessary sample size for proportion comparisons for a hypothetic better classifier - there's a good chance that these will be ridiculously large even for ridiculously large hypothetical improvements), you can argue that optimization does not make any sense and just creates a risk of overfitting.
Comment: @RNA: The probability to be "lucky" in all folds is directly connected to the total number of cases (in all 10 folds), and typically only the average over all those folds is considered. I updated the answer with a simulation of a hypothetical picking of the best of 100 models (say, 2 hyperparameters with 10 steps each), which is already associated with a considerable bias for the example scenario (error rate too low by 1/3). Many people here rarely have a few thousand independent cases at hand - e.g. I rarely have even the 44 individuals who wrote digits for the full UCI digits data set.
Comment: @cbleites: If grid search might not be an appropriate method for finding the optimal model, what method should I choose then?
Comment: I don't agree with you on "skimming variance". If you have lots of points in the grid for hyperparameter optimization, a point may opportunistically get lucky in one fold of the CV; but if you have, let's say 10-fold CV, it is still unlikely a set of parameter will accidentally get lucky on all 10 folds of the CV.


Acessar variável através de exec

Question: Estava a tentar criar uma "shell" para poder comunicar com um programa em python durante a sua execução, até momento tenho o seguinte (este código é apenas um exemplo):
import sys, pygame
from threading import Thread
import math

pygame.init()

# INIT SCREEN
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("teste")

black = 0,0,0
white = 255,255,255

rgb = 255,50,120

running = True

clock = pygame.time.Clock()

timer = 0
angulo = 0
x = 0
global x_var
x_var = 2

def process_input():
    while True:
        c = input("Python Shell > ")
        exec(c)

t = Thread(target=process_input)
t.daemon = True
t.start()

while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False

    angulo = ( angulo % 360 ) + 0.1
    sin = math.sin(angulo) * 50
    y = int( sin + height / 2)

    x+= x_var

    pos = x,y

    rgb =  ( (rgb[0] % 255 ) +1 ) , ( (rgb[1] % 255 ) +1 ), ( (rgb[2] % 255 ) +1 )

    screen.set_at(pos, rgb)
    pygame.display.flip()

O meu objectivo seria alterar uma variável em tempo de execução, por exemplo alterar x_var de 2 para -2 mas não estou a ter sucesso. Sou um pouco novo com python, mas já vi algo semelhante em outras situações.
Coloquei a variável x_var como Global até por que não sei o alcance do comando exec. Penso que o facto de ter o comando process_input em outra thread poderá estar a afectar o acesso, porém preciso de ter sempre um loop em execução.
Answer: Há muitos, muitos senões com a forma como você está fazendo esse programa -
mas, isso faz parte das coisas que escrevemos quando estamos entendendo como programar.
O principal talvez seja que suas estruturas de controle não fazem parte de nenhuma função e estão "soltas" no código do módulo.
No entanto, essa dúvida pontual é fácil de responder: o exec  e o eval, por padrão tem seu código executado num dicionário de variáveis novo, sme nenhuma variável - mas ambos aceitam como parâmetros dicionários para serem usados como o namespace de variáveis globais e locais.
Como o seu intuito aí é alterar variáveis globais, basta passar o dicionário de variáveis globais, que em Python é retornado quando chamamos globals() como segundo parâmetro para o exec:
def process_input():
    while True:
        c = input("Python Shell > ")
        exec(c, globals())

Funciona assim:
In [1]: b = 10

In [2]: exec(input("teste: "), globals())
teste: b = 20

In [3]: b
Out[3]: 20

Se você mantiver e evoluir esse programa, eventualmente o código dentro do loop principal do pygame vai ficar numa função. Você pode então criar um objeto de namespace só para manter todas as variáveis que atualmente são locais, e passar o .__dict__ desse objeto como parâmetro de globais para o exec.

Acessar variável através de exec


Can someone explain this use of a primitive type in generics in Java?

Question: I'm using the following statement in my Java code and I was surprised to see that it actually compiles:
ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(url.toString(), byte[].class, params);

The signature for this method from the docs is:
ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)

I was under the impression that you cannot use primitives in generics in Java. If so, how is this working? Is this essentially syntactic sugar for something else that's going on under the hood? My (quite probably wrong) guess is that the compiler converts byte[] to Array and somehow works with that. I was wondering if someone could explain to me how and why this works.
Answer: I guess, it's because arrays are actually objects (referenced types) in Java, they are immediate subtypes of Object. So, generics work for them as for any Java reference type.
Comment: In other words, arrays of primitive values are not primitive types
Comment: Ah, cool! Thanks for the explanation - that makes sense. **edit:** If you're going to downvote this answer, at least have the courtesy to explain why.
Answer: That's about a "wrapper" for practical purposes. I mean, despite you're using primitive types (int or byte) you create an object (you know, new reserved word is to indicate you're instantiating an object).
Answer: I'm assuming you're using JDK 5 or greater. If so, it is cos' of Autoboxing kicking in, to convert the primitive byte[] into Byte[] internally.
Comment: Nothing to do with autoboxing.
Comment: I am affraid that is not true.
Comment: That is incorrect, arrays are not primitives (as explained by Victor Sorokin) so no autoboxing occurs.


Does the three chord trick always end a chord progression on the third chord?

Question: It seems almost a redundant question, but the meat of it is this:
If one is using the three chord trick, is it stylistically required by rules of theory and phrasing to end the progression on the third chord? While it is pleasing to the ear to complete the phrase and repeat after three chords, will extending the phrase cause major disharmony or dissonance? What concepts of music theory can be cited to support this?
Comment: I have heard TWO different definitions of "three chord trick." One (the more common one) is any song built around the chords I IV and V. The other (less common) is any cadence of 3 chords played in rapid succession, for example C-G-Am or F-G-Am. La Bamba / Twist and shout comply with both these definitions, AND end on the V (the "third" out of I-IV-V as expressed in the question. But a cadence of the I IV and V chords in succession in ANY of the six possible orders sounds good. Those ending in I sound the most "finished." Please define 3 chord trick a bit more to get the answer you want.
Answer: No.  In fact, ending a passage of music on the third chord (the dominant) is an imperfect cadence or half cadence.  As implied by the name, there is a "better sounding" perfect cadence (or authentic cadence), where you end on the first chord (the tonic).  To quote Wikipedia:

Because it sounds incomplete or "suspended", a half cadence is considered a weak cadence.

http://en.wikipedia.org/wiki/Cadence_(music)
The concept of finality is, of course, subjective; but as for dissonance, that is not present regardless of the order the chords are played.  The three chords (tonic, dominant, subdominant) are the main chords of the key and are so often used exactly because they don't clash with each other.
Comment: @ogerard Basically yes.  These three chords are basically the minimum when composing a song, allowing for simplicity without sounding like there's something missing.  The number of popular songs that use only three chords is impressive, if not as interesting as the ["four chord songs"](http://www.youtube.com/watch?v=5pidokakU4I).
Comment: so what the original poster means by "three chord trick" is sticking to degree I, IV and V when playing chords to a melody, or it is something more? That was the first time I heard this phrase.
Answer: In simple answer to your question - yes, quite often, obviously not always. The progression may well end on the 'third chord' as you call it (normally it's the V), in order to re-start the sequence, usually back at I.
The song itself, though, will not sound like it's finished if that end of sequence chord is left to sound at the end of the song. As Matthew states, that is an imperfect cadence, where the listener expects more - hence it's there at the end of the sequence, but normally, the song will end on the next chord after the V, which will be I - producing a perfect cadence. No prizes for guessing why it's so called, unless it fades out of course, when we'll never know!
Unless- of course - we're talking about the jazz sequence of ii - V - I, which inevitably ends on 'the third chord' as the OP calls it, which is rather different...


Why do equilateral triangles relate to cubics

Question: I found this question talking about the relation between an equilateral triangle and cubics with three distinct real roots.
Here's an image from the original post with an example:

What this post says about cubic curves is:
You can always construct an eq. triangle such that:

the triangle's inscribed circle's center lines up with the point of inflection on the curve
the function's min. and max. points line up with the inscribed circle's left- and rightmost points (not necessarily tangent to the sides)
and the triangle's vertices line up with the roots.
(as seen on the image)

You can use these facts to solve a cubic of this kind using with the triple angle cosine identity, but my question is not about that.
My question would be about how you can prove this is construction is always possible.
(1.) I started using the fact that cubic curves have point symmetry on the inflection point, thus the min. and max. points are equally distanced from the poi. on the x-axis.
This defines a circle.
(2.) Then, if we draw another circle with the same center but twice the radius, we can construct a specific eq. triangle in between the two circles such that the two are the inscribed and circumscribed circles of that triangle.
(3. ??) We have infinitely many ways of rotating this, but for some reason, one of them will line up such that the vertices are in line with the roots. I got stuck here.
Thanks for any help and please explain in a very simple manner.
Comment: Note that we can always draw an equilateral triangle whose vertices have any three $x$-coordinates we like, but (up to moving it up and down) that equilateral triangle is unique. So all the work here is in making sure that the critical points and inflection point line up with the other properties of the triangle.
Answer: Let's start with an equilateral triangle, such that one side is parallel to the $x$-axis. The center of the triangle is at $x_c$ along the $x$ axis. The circumscribed circle radius is $R$. The $y_c$ position does not matter, but I will use $y_c=0$ in my example. You can offset it by any value. Now let's rotate the triangle clockwise by an angle $\phi$. The vertex on top, originally at $(x_c,R)$ will have the new $x$ coordinate $x_c+R\sin\phi$. For the vertex on the left, originally at $(x_c-R\cos\frac\pi6,-R\sin\frac\pi6)$, the new $x$ coordinate will be $x_c-R\cos\left(\frac\pi6-\phi\right)$, and for the vertex on the right the new coordinate will be $x_c+ R\cos\left(\frac\pi6+\phi\right)$.
So now let's identify these coordinates with the roots of the cubic. We assume that they are distinct, and in increasing order $a,b,c$. We need to find the $x_c$ coordinate,$R$, and the angle $\phi$. Note that for the equilateral triangle $$x_c=\frac{a+b+c}3$$
The equation of the quadratic (up to a multiplicative factor) will be $$y=(x-a)(x-b)(x-c)=x^3-(a+b+c)x^2+(ab+bc+ca)x-abc$$
Let's calculate the first and second derivatives:
$$y'=3x^2-2(a+b+c)x+(ab+bc+ca)\\y''=6x-2(a+b+c)$$
Let's start with the inflection point, $y''=0$ corresponding to $$x_i=\frac{a+b+c}3=x_c$$
Since $y'$ is a quadratic, its roots are symmetric with respect to its vertex (at $x_i$), and the distance between the roots will be $$\frac{\sqrt{2^2(a+b+c)^2-4\cdot3\cdot(ab+bc+ca)}}3=\frac23\sqrt{a^2+b^2+c^2-ab-bc-ca}$$
This is the diameter of the inscribed circle, which also the radius of the circumscribed circle in an equilateral triangle.
$$R=\frac23\sqrt{a^2+b^2+c^2-ab-bc-ca}$$
So we need to find $\phi$. We know that the top vertex coordinate is $$b=x_c+R\sin\phi$$
so $$\sin\phi=\frac{b-x_c}R$$
Now let's check if indeed the other coordinates correspond to $a$ and $c$. I will do it for $a$, and $c$ can be done in a similar fashion.
$$x_c-R\cos\left(\frac\pi6-\phi\right)=x_c-R\left(\cos\frac\pi6\cos\phi-\sin\frac\pi6\sin\phi\right)\\
=x_c-R\left(\frac{\sqrt{3}}2\sqrt{1-\sin^2\phi}+\frac12\sin\phi\right)\\=x_c-\left(\frac{\sqrt{3}}2\sqrt{R^2-R^2\sin^2\phi}+\frac12R\sin\phi\right)\\=(*)$$
Now we use $$R\sin\phi=b-x_c\\x_c=\frac{a+b+c}3\\R^2=\frac49(a^2+b^2+c^2-ab-bc-ca)$$
to get $$(*)=\frac{a+b+c}3-\left(\frac{\sqrt 3}2\sqrt{\frac49(a^2+b^2+c^2-ab-bc-ca)-\frac{(2b-a-c)^2}9}+\frac12\frac{2b-a-c}3\right)\\=\frac16\left(2a+2b+2c-2b+a+c-\sqrt3\cdot\\\sqrt{4a^2+4b^2+4c^2-4ab-4ac-4bc-4b^2-a^2-c^2+4ab+4bc-2ac}\right)\\=\frac16\left(3a+3c-\sqrt3\sqrt{3a^2+3c^2-6ac}\right)\\=\frac16\left(3a+3c-3\sqrt{(a-c)^2}\right)$$
Since we assumed $c>a$, we then write $$(*)=\frac16(3a+3c-3(c-a))=a$$
Asn mentioned before, you can similarly show that $$x_c+R\cos\left(\frac\pi6+\phi\right)=c$$
Answer: Well, the roots of a cubic, with 3 real roots, are determined by the $\cos()$ of an angle, $\psi$ that is divided by 3.
So if one starts with
$\psi = \theta + 2k\pi$
then
$\dfrac{\psi}{3} = \dfrac{\theta}{3} + \dfrac{2\pi}{3} k$
The equilateral triangle comes from the $\dfrac{2\pi}{3}$ spacing between the angles for the solutions.
You might find Figure 2 and Section 2.4 of Nickalls' analytic geometry based solution of the cubic helpful.
If one allows $\psi$ to be a complex hyperbolic angle, then once can use $\cosh()$ to express the roots of the cubic, and go beyond the case of 3 real roots.
See this Geogebra demonstration and vary the slider for the $d$ coefficient of the cubic (or press the play button for that slider and let it vary automatically).  The left pane will show the value of the complex hyperbolic angle $\psi$ and the right pane will show its affect on the (real part of the) roots of the cubic.

That Geogebra demonstration is based off of this write up.


how to load android app from a web page link?

Question: I am trying to load an android application from html file when browsing the page with android browser.
I have tried like below
Click

But without any avail...
I am also trying to pass a video link to the app.
Any has any idea..
I have seen this kind of link can be done in apple ios.
Comment: By "load", do you mean "start an already installed on the device Application" or do you mean "install an Application from a link on a web site"?
Comment: Similar type of question asked before :

http://stackoverflow.com/questions/13042278/launch-android-application-from-a-browser-link?rq=1
Comment: Similar type of Question asked before :

http://stackoverflow.com/questions/13042278/launch-android-application-from-a-browser-link?rq=1
Comment: already installed app
Answer: And that's the Android way:
http://play.google.com/store/apps/details?id=<package_name>

e.g. https://play.google.com/store/apps/details?id=com.troubi.kingofmath
At an Android device the user will be asked whether he'd like to open this app in the browser or Google Play.
Comment: Thanks...what if I wanted to pass a link to the app? Ie..a video link
Comment: Then you just give him the youtube link? :}
Comment: Sorry for the dumb question but how do i do that ..https://play.google.com/store/apps/details?id=com.troubi.kingofmath:www.youtube.com/xyxz?
Comment: @AbulHasnat Nope, you have to provide a separate youtube link starting with www.youtube.com/something-here


Redirecting user links to their profile2 pages

Question: I'm using the profiles2 module for my user profiles.  I want to keep the user accounts hidden, and instead, use the profiles for public viewing.  I disabled user pictures in users, and added an image field to the profile entity.  When users post comments, the links point back to the user account.  How can I change that behavior so links point back to that user's profile page?  Also, how can I display the profile pics for these comments?  Essentially, I'm wanting the normal user functionality to occur with profiles instead of users.
Answer: This can be done with a combination of
hook_preprocess_username (make links go to profile path)
Something like:
function MYTHEME_preprocess_username(&$variables) {
  $account = $variables['account']
  if($profile = profile2_load_by_user($account, 'profile_type')){
    // This gets passed onto theme_username
    $variables['link_path'] = 'user/' . $variables['uid'].'/profile/path';
    // Possibly:
    // $type = $profile->type();
    // $path = profile2_page_get_base_path($type) . '/' . $account->uid
  }
}

and then the picture override in
hook_preprocess_user_picture (override user picture)
Something like:
function MYTHEME_preprocess_user_picture(&$variables) {

  $account = variables['account'];
  if($profile = profile2_load_by_user($account, 'profile_type')){
    // You could use a custom 'display' parameter, e.g image_style
    $image = field_view_field('profile2', $profile, 'field_image');
    $variables['user_picture'] = render($image);
  }

}

Comment: Perfect. Thanks David!  (note to other viewers: don't forget the '&' in the variables argument on the preprocess_username fx).  Also, profile2_page_get_base_path() expected an object.  I'll have to look at it later (or post if you know).
Comment: For some reason, the field_view_field call isn't working right.
Comment: To clarify, the field is populated, but the text 'Array' is rendered where the image should be.
Comment: @vintorg it needs `$variables['user_picture'] = render($image);` - answer updated
Comment: Answer also updated to use `$type = $profile->type();` for `profile2_page_get_base_path` object param
Comment: That did 'er.  One thing I am struggling with in Drupal is when something gets themed, rendered, etc.  I wish there was a comprehensive diagram that helped break that down.  Anyway, thanks again David!
Comment: I'm having problems with this, can you please clarify what you did to make this work? the preprocess doesn't seem to be working when I've pasted it in my template.php and replaced the name with my theme. Thanks :)
Comment: Fixed it! this is my code :  `function YOURTHEME_preprocess_username(&$vars)
{
   $account = $vars['account'];
   if ($profile = profile2_load_by_user($account, 'machine_name_of_your_profile'))
   {
      $type = $profile->type();
      $vars['link_path'] = profile2_page_get_base_path($type) . '/' . $account->uid;
   }
}`
, as taken from http://www.socialseomanagement.com/drupal-howto-redirecting-user-account-links-users-profile-page . I've had to clear my cache too.
Answer: Drilling down a bit, the picture there is generated inside comment.tpl.php by the user-picture.tpl.php template, inside of which it relies upon the $user_picture variable.
So, the easiest way might be to put something along these lines in your theme's template.php...
YOURTHEME_preprocess_user_picture(&$variables) {

  $account=$variables['account'];

  $variables['user_picture'] = // whatever you want it to be instead of the default

}

...with whatever logic you need based upon the user information passed in via the $account variable.
This would also have a possible added benefit of overriding this everywhere a user picture is similarly themed.
Comment: A small typo on the second line of the function, should be `$variables['account']`.
Comment: Excellent jimajamma.  Can I also modify the user links to point to the profile pages instead of user accounts from template.php?
Comment: good catch, fixed up.  thx!


Regex for sed to grab multiple lines or a better way?

Question: I'm creating a script that connects to a server and dumps the output to a tempfile. I want to use sed in the script to grab specific information from the temp file. The output would always have the 80 char dashed line then the information I want followed by the Disconnected statement.
I've gotten a regex working if it was just a single line the trouble is how do I group the newlines as well?
Regex
-\{80\}[\r\n]*\(.*\)[\r\n]\{4\}Disconnected

File
...
--------------------------------------------------------------------------------
The information that I want to get can be a single line or multiple lines.
Another line to grab.

And this should be caught as well.

Disconnected ...

Desired output
The information that I want to get can be a single line or multiple lines.
Another line to grab.

And this should be caught as well.

Answer: First use the '-n' flag to suppress automatic output. Next use sed addresses to quote the part you are interested at (from the dashes "---" till a line that has the word "Disconnected"). Finally print the pattern space (all the pattern space since you're interested in everything inside it).
~$ sed -n '/^---*/,/Disconnected/{p}' inputfile

Edited because of LF4 request of removing the line with dashes from the result.
With the "addresses" you quote individual pattern spaces. So you can do whatever you want with those individual pattern spaces. Including remove lines by regexp. In the example, the command removes lines formed by dashes from the pattern space yielding the output you're looking for:
~$ sed -n '/^---*/,/Disconnected/{/^---*/d;p}' inputfile

HTH
Comment: Thats nice and clean but it's also including the dashes and the disconnected line. I have to run lines=wc -l; sed -n '2;$lines {p}' in order to remove them is there a way to group with that sed command?
Comment: Notice that with sed addressed you get the individual pattern spaces. With them you can do whatever you want. See my edit.
Comment: Interesting thank you for explaining that I didn't know about sed addresses and how they worked I'll have to do reading about it.
Answer: sed can look for the pattern in multiple lines by concatenating them into which is called "hold space", something like this:
$ sed -n '1h;1!H;${;g;s/.*\(-\{80\}.*Disconnected\).*/\1/p;}' file

1h: copy the first line to hold space
1!H: from the second line, append to hold space
$: the last line
g: copy the hold space to pattern buffer
s/pattern/substitution/: search and substitute
\1: back reference to the group in pattern
p: print
Comment: This worked well, I modified it to grab just the text excluding the dashes and the Disconnect as well as the blank line before Disconnected and after the dashes.

sed -n '1h;1!H;${;g;s/.*\(-\{80\}\n\(.*\)\nDisconnected.*/\1/p;}' file
Comment: Don't forget to escape the round bracket with a backslash.
Answer: Easiest, but not very efficient way is

Use tr to remove all newlines.
tr '\n' ' '

Re-add newlines after the Disconnected with sed's \a command.
Parse that data using your sed command.


R fuse several JSON

Question: i'm having struggle to make a JSON from several different JSON in R. I'm making a request to an API while increasing the final parameter in function of the var "individus". For each request i receive a JSON and i only keep the "$top" part of it.
for (i in 1:individus){
    url_req <- paste("api_url", i, sep="")
    list_json1 <- fromJSON(url_req)

    if (missing(list_json1) == TRUE){
        inter_json <- list_json1$top
    }else{
        inter_json <- inter_json+list_json1$top
    }
}

My question is how can i fuse those data in a single json a little bit like that :
inter_json <- inter_json+list_json1$top

Answer: I think you might be confusing the concat-operators: + is used in javscript but not in R. Since the object returned by fromJSON is a list, you need to combine lists and could have a look here: how-to-combine-two-lists-in-r

R fuse several JSON


PySpark and Pandas - Read partitioned csv files from S3 skipping empty ones

Question: Using PySpark, I have some code that runs through a bunch of queries.
for index, query in enumerate(query_map):
  spark_dataframe.filter(
       query).write.csv('s3://OutputBucket/Csvs/Query_{}'.format(index)

I'm new to spark, but I understand that each partition is writing individual csv files to a directory called Query_[index]. Now I want to collect those files and put them into a pandas dataframe.
import boto3
import pandas
s3 = boto3.resource('s3')
my_bucket = s3.Bucket("OutputBucket")
#Get all csv names
csvs = [
    "s3://OutputBucket/Csvs/"+\
    str(i.key) for i in my_bucket.objects.filter(Prefix='Query/')]
to_concat = []
#Turn them into a dataframe
for csv in csvs:
    try:
        to_put_in.append(pandas.read_csv(csv))
    except pandas.errors.EmptyDataError:
        pass
#Join dataframe
my_big_dataframe = pandas.concat(to_concat)

The problem is that Pyspark writes a lot of empty files out. So my code spends a lot of time trying to read in an empty csv file only to throw an exception.
As I understand, the df_spark.toPandas() function defeats the purpose of spark since it puts it into driver memory and does not utilize the IO parallelization of each partition. It also defeats the purpose of spark to use coalesce. So writing to a bunch of csvs and then manually reading them in is not a terrible idea.
tl;dr
My question is if there is a way to skip those empty csv files that pyspark writes by either:

Perhaps boto3 can sort them first by size then just iterate until we fine and empty file?
Is there any way in PySpark to do it without defeating the point of pyspark?
Comment: Ia your end goal to concatenate all of the part files into one csv? You can do that from the command line, if that's an option. See [this answer](https://stackoverflow.com/questions/47828907/how-to-save-a-file-on-the-cluster/47932523#47932523).
Comment: So just save to hdfs  then concatenate from the head node?
Comment: That's usually the way I do it, but I don't know what your use case is.
Answer: I faced a similar problem few months ago. Used something like this
# get the number of non-empty partitions in dataframe df
numNonEmptyPartitions = (df.rdd.glom().map(lambda x: 1 if len(x)>0 else 0).
                                reduce(lambda x,y: x+y))

df = df.coalesce(numNonEmptyPartitions)

Now, you will have all non empty partitions.


Is Prediction the same as Compression?

Question: Just came across this transcript that states:

The principle is that prediction is the same thing as compression. And
what that means is that whenever you have a prediction algorithm, you
can also get a correspondingly good compression algorithm for data you
already have, and vice versa.

Even though the article gives some examples, I would like to know some answers from this community on the question: Is Prediction the same as Compression?
Comment: According to the Information Bottleneck Method, it is
Answer: Nontechnically, prediction (eg to predict) is the process of transforming an object.  The transformer often depends on summaries of information.  These summaries, or estimates, might be compression.
So, it seems that estimation might be compression, but prediction is not. I say might because you can describe a dataset by the dataset itself - that's still an estimate, but not a compression. Usually, estimates are compressions.
Technically, a prediction is an estimate of a random variable.  To get that estimate requires estimation.  I don't think though that this is the definition that the author is using.

Is Prediction the same as Compression?