Como listar arquivos em um diretório e usar em um projeto Angular2 + Webpack

Question: Olá, estou criando um projeto em Angular2 + Webpack e nesse projeto eu uso como biblioteca de design o @angular/material. Para que eu possa criar meus próprios ícones, usando SVG, eu estou extendendo o componente MatIcon como diz na documentação, e estou salvando meus SVG em um diretório acessível da aplicação. A estrutura do projeto está dessa forma:
- src
| - app
  | my-icon.component.ts
| - assets
  | - svg
    | - facebook.svg
    | - google.svg
    | - twitter.svg

Meu componente está da seguinte forma:
import { Component, Input } from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";
import { MatIconRegistry } from "@angular/material";

export const icons = ["google", "facebook", "twitter"];

@Component({
  selector: "my-icon",
  template: `<mat-icon [svgIcon]="icon"></mat-icon>`
})
export class MyIcon {
  @Input() icon: string;

  constructor(i: MatIconRegistry, s: DomSanitizer) {
    for (let icon of icons)
      i.addSvgIcon(icon, s.bypassSecurityTrustResourceUrl(`assets/svg/${icon}.svg`));
  }
}

Meu problema é que toda vez que crio um novo SVG que seria um icon eu sou obrigado a vir no meu componente e atualizar minha constante icons com o nome do SVG.
Existe alguma forma de eu conseguir ler todos os arquivos da pasta "assets/svg" e já inserir na minha minha constante? Eu até vi que é possível fazer usando o fs do NodeJS como pode ser visto nesse link mas quando tento acessar o fs de dentro do meu componente, não retorno nenhuma informação.
Como resolver?
Answer: Fiz uma pergunta semelhante há um tempo atrás, e baseado no que encontrei, você pode utilizar o Chokidar informando o diretório, o caractere coringa seguido da extensão.
let watcher = chokidar.watch('assets/svg/*.svg');

observe o evento add sempre que um arquivo SVG for criado, colado ou movido para o diretório assets/svg/ você pode executar uma ação:
watcher.on('add', (path, stats) => {
    icons.push(path);
});

Código do exemplo acima:
const chokidar = require('chokidar');
let icons = [];
let watcher = chokidar.watch('assets/svg/*.svg');
watcher.on('add', (path, stats) => {
    icons.push(path);
    show();
});
const show = _ => console.log(JSON.stringify(icons, null, 2));

Comment: excelente! se desse pra dar mais de 1 ponto eu dava! fantástico


Get default printer name from command line?

Question: Microsoft Windows XP comes with a VBS script to manage local and network printers from the command line:
To Get the default printer details from command line:
cscript C:\windows\system32\prnmngr.vbs -g

To Get the list of printers added to the system from Windows command line:
cscript C:\windows\system32\prnmngr.vbs -l

Is there any equivalent commands for Windows 7? I just need to get the default printer details and get the list of printers attached to the system.
Answer: In Windows 7 these same scripts are found in
C:\Windows\System32\Printing_Admin_Scripts\en-US

Answer: List of all printers names and shows default one (You can get more details read documentation)
wmic printer get name,default

If you want output to file use:
wmic printer get name,default > D:\catalog\file.txt

Availability
The wmic command is an external command that is available in the below Microsoft operating systems as wmic.exe.
Windows XP professional
Windows 2003
Windows Vista
Windows 7
Windows 8
Windows 10

Comment: This worked for me in Windows 10: `for /f "tokens=2 delims==" %A in ('wmic.exe printer WHERE default^=TRUE get name /VALUE') do set "default_printer=%~A"`
Comment: `wmic printer where default=true get name` lists only the default printer.
Answer: On Windows 10, the scripts are found in the same place as Windows 7.
Execute the following command to display the default printer.
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -g

Note if there is no default printer, the script will return nothing
Answer: i found mine in the sideXside folder...
C:\windows\winsxs\x86_microsoft-windows-p..inscripts.resources_31bf3856ad364e35_6.1.7600.16385_en-us_0e83b619ada3e7ed\
i ran the following:
cscript C:\windows\winsxs\x86_microsoft-windows-p..inscripts.resources_31bf3856ad364e35_6.1.7600.16385_en-us_0e83b619ada3e7ed\prnmngr.vbs -g
it worked.
Answer: To list active printer components on Windows 8.1:
c:>cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -l | findstr "Printer name"
Answer: To remove printer in Windows 8.1:
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -d -p "Printer name"

I had trouble with a printer not showing in devices/printers so unable to remove it, although it was there when I pressed file > print in firefox. It was also stopping me from using the printer name for the actual installation (it bugged me having to append _1 at the end of the default printer!"
Answer: Using a batch file (no VBScript files):
@echo off

::Get printer CLSID
for /f %%a in ('reg query HKCU\Printers\Defaults') do (set regkey=%%a)
::Get printer name from the previous CLSID

for /f "tokens=3*" %%a in ('reg query %regkey%') do (set printername=%%a %%b)
echo Printer name is: %printername%

I wish that could help you


Error - A SignInResponse message may only redirect within the current web application - MVC 2.0 application

Question: I have a situation where we have a MVC 2 application(I tried this with a basic MVC 2 app without any extra stuff, still same problem) and am using adfs 2 for authenticating my users.
So..
Now I get into my application and I get the below..
ID3206: A SignInResponse message may only redirect within the current web application: '/[app]' is not allowed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.IdentityModel.Protocols.FederationException: ID3206: A SignInResponse message may only redirect within the current web application: '/[app]' is not allowed.
I have read most blogs on this, and posted to one..
    <federatedAuthentication>
            <wsFederation passiveRedirectEnabled="true" issuer="https://auth.[domain]/adfs/ls/" realm="https://[development domain]/[app]/" requireHttps="true" />
            <cookieHandler requireSsl="true" />
          </federatedAuthentication>
<audienceUris>
    <add value="https://[development domain]/[app]/" />
  </audienceUris>

I have the trailing slash on the realm and audienceUris.
I have added what he suggested to Application_BeginRequest – I then copied code to [development domain] as that's where the certs are.. It just gets stuck in an infinite loop then.
I also have checked my Relying Party on the Geneva server.. The Identifiers and Endpoints(POST) are both https://[development domain]/[app]/ - again with the trailing slash

I think it's a problem with the fact it's a MVC application, I have created numerous Claims Aware website and got my claims etc on the default.aspx page. My thinking is that the routing that is involved with the MVC app is somehow posting it back wrong?
any help really apprecaited as Im looking at this for quiet a while now to no avail..
J
Comment: I have the same problem with MVC4.
Answer: I've been tearing my hair out on this one.  I too have the trailing slash specified in my configuration.  Turns out that, in my case, navigating to my app with a trailing slash in the browser like so:
http://localhost/myapp/
will work, whereas
http://localhost/myapp
will not.
If I can dig up some more reasons why this is the case, I will add some more background on why this is happening.
Answer: I override the RedirectToIdentityProvider on subclass of WSFederationAuthenticationModule. This happens only once before redirecting to the STS. You have to tell the config file to use this class FixedWSFederationAuthenticationModule instead of the defualt WSFederationAuthenticationModule
public class FixedWSFederationAuthenticationModule : WSFederationAuthenticationModule
{
    public override void RedirectToIdentityProvider(string uniqueId, string returnUrl, bool persist)
    {
        //This corrects WIF error ID3206 "A SignInResponse message may only redirect within the current web application:"
        //First Check if the request url doesn't end with a "/"
        if (!returnUrl.EndsWith("/"))
        {
            //Compare if Request Url +"/" is equal to the Realm, so only root access is corrected
            //https://localhost/AppName plus "/" is equal to https://localhost/AppName/
            //This is to avoid MVC urls
            if (String.Compare(System.Web.HttpContext.Current.Request.Url.AbsoluteUri + "/", base.Realm, StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                //Add the trailing slash
                returnUrl += "/";
            }
        }
        base.RedirectToIdentityProvider(uniqueId, returnUrl, persist);
    }
}

Comment: Here is the syntax for your web.config:
Comment: That code will fail if the request URL contains parameters and/or if the realm is not a subset of - or identical to - the request URL. Both of those scenarios are quite possible.
Comment: Is there some reason to subclass instead of putting `WSFederationAuthenticationModule_RedirectingToIdentityProvider`? `RedirectingToIdentityProviderEventArgs` allows you to mutate the `SignInRequestMessage` as far as I can tell.
Answer: This code takes care of that (put it in global.asax):
private void Application_BeginRequest(object sender, EventArgs e)
{
//  This corrects WIF error ID3206 "A SignInResponse message may only redirect within the current web application: '/NHP' is not allowed."
//  For whatever reason, accessing the site without a trailing slash causes this error.
if (String.Compare(Request.Path, Request.ApplicationPath, StringComparison.InvariantCultureIgnoreCase) == 0 && !(Request.Path.EndsWith("/")))
Response.Redirect(Request.Path + "/");
}

EDIT:
Another thing to check is the federationAuthentication/wsFederation element in your microsoft.identityModel in Web.config. Verify the issuer and the realm are correct.
Comment: With this approach I ended up with some strange endless redirects between the IP-STS and RP on SignIn
Comment: I think something else is going on then but I couldn't tell you what.
Comment: This worked for us and was the easiest to implement. Our situation was that we have several virtual applications within one web site, so each as a sign in page within it's own 'folder' and the above fixed the issue.
Answer: I'm using Forms authentication with WIF. The forms auth module redirects unauthorized requests to the correct controller and stores the originally requested URL in the ReturnUrl parameter, so I worked around this bug by overriding the GetReturnUrlFromResponse method.
///
/// Provides a workaround for a bug in the standard authentication module.
///
/// <remarks>
/// This class corrects WIF error ID3206 "A SignInResponse message may only
/// redirect within the current web application..."
/// WSFAM produces the error when the ReturnUrl is the root of the web application,
/// but doesn't have a trailing slash. For instance, "/app" is considered incorrect
/// by WSFAM whereas "/app/" is correct.
/// </remarks>
public class FixedWsFederationAuthenticationModule : System.IdentityModel.Services.WSFederationAuthenticationModule
{
    ///
    /// Extracts the URL of the page that was originally requested from
    /// the sign-in response.
    ///
    /// <returns>
    /// The URL of the page that was originally requested by the client.
    /// This is the URL (at the relying party) to which the client should
    /// be redirected following successful sign-in.
    /// </returns>
    /// <param name="request">
    /// The HTTP request that contains a form POST, which contains the
    /// WS-Federation sign-in response message.
    /// </param>
    protected override string GetReturnUrlFromResponse(HttpRequestBase request)
    {
        string returnUrl = base.GetReturnUrlFromResponse(request);

        // First Check if the request url doesn't end with a "/"
        if (!string.IsNullOrEmpty(returnUrl) && !returnUrl.EndsWith("/"))
        {
            // Compare if (return Url +"/") is equal to the Realm path,
            // so only root access is corrected.
            // /AppName plus "/" is equal to /AppName/
            // This is to avoid MVC urls.
            if (string.Compare(
                returnUrl + "/",
                new Uri(Realm).LocalPath,
                StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                // Add the trailing slash.
                returnUrl += "/";
            }
        }

        return returnUrl;
    }
}

To make use of this class, you need to register it in the web.config.
Add this element to the system.webServer/modules section, changing the appropriate parts:
<add name="WSFederationAuthenticationModule" type="YOUR_NAMESPACE.FixedWsFederationAuthenticationModule, YOUR_ASSEMBLY" preCondition="managedHandler" />

Answer: I had this issue when I added STS reference to my web application which by default runs under virtual server on dynamic port. I changed it to run it out of IIS (as with virtual web server, redirection to STS will not happen unless you run it out of IIS / IIS Express) and manually edited by web.config to change audience URIs under Microsoft.IdentityModel configuration.
When I Looked at the FederationMetadata.xml, it was still referring to the old location (with dynamic port). I refreshed my STS Reference by adding it again and it worked.


How can I implement a template-driven nested for-loop that is capable of returning a generator?

Question: I am attempting to implement a generator that provides subscripts over a multi-dimensional array. Specifically, I am using the generator implementation by CPP Chrono. Since the number of dimensions is driven by the size of an input shape array, I needed a recursive function to generate the nested for loop. So far, I have the logic working for an size-deducted nested for loop:
#include <iostream>
#include <array>

using Size_t = size_t;
using Int64_t = int;

/// @brief Nested for loop helper
/// @details Recursively generates NLoops nested for loops
template <size_t Index = 0, Size_t NLoops>
void nested_for_loop(const std::array<Size_t, NLoops>& shape, std::array<Int64_t, NLoops>& outSubscripts) {
    if constexpr (Index == NLoops) {
        // Base case: All nested loops have run.
        // Operation is performed here
        for (size_t i = 0; i < NLoops; ++i) {
            std::cout << outSubscripts[i] << ' ';
        }
        std::cout << std::endl;

    } else {
        // Recursive case: Loop from 0 to array[Index]
        for (size_t i = 0; i <= shape[Index]; ++i) {
            outSubscripts[Index] = i; // Store the current subscript
            nested_for_loop<Index + 1>(shape, outSubscripts); // Recursively call for the next loop
        }
    }
}

int main() {
    std::array<Size_t, 3> shape = {2, 30, 6};
    std::array<int, 3> outSubscripts{};

    Int64_t outIndex = 0;
    nested_for_loop(shape, outSubscripts);

    return 0;
}

This is capable of printing out the subscripts that I want. If this was just a regular old for-loop, I could just add a co_yield to where the operation logic is, update the function return type to generator<MyDesiredArrayType>, and call it a day. However, it doesn't seem possible to convert this recursive approach to a generator function.
Is there a better approach to unrolling this for loop so that I can properly yield from a coroutine? The only approach that I've been able to come up with is the one described in this answer. I'd rather not have to resort to something like this, because I don't think there is a constexpr approach to converting the linear loop index into the N desired subscripts, and I don't want to incur the runtime cost of the extra  additions/multiplications.
There is always the option for me to manually write out a series of if constexpr statements. I could add M conditional blocks, so I'd be supporting from 0 up to M nested for loops. However, this would be so tedious and restrictive that it's silly. There surely must be a better way!
Answer: You can turn your recursive function to that simple iterative function:
template <std::size_t N>
bool next_cartesian_product(const std::array<std::size_t, N>& shape,
                            std::array<std::size_t, N>& cur)
{
    for (std::size_t i = 0; i != N; ++i) {
        if (++cur[N - 1 - i] > shape[N - 1 - i]) {
            cur[N - 1 - i] = 0;
        } else {
            return true;
        }
    }
    return false;
}

Demo
Turning it into generator should be trivial now.


Pegar apenas alguns atributos de tags HTML

Question: Tenho vários inputs, vindo de um curl, mas eu preciso apenas do name e do value desses inputs:
<input type="hidden" autocomplete="off" name="timezone" value="180" id="u_0_9">

Eu tenho esse código:
preg_match_all('/name="(.*?)" value="(.*?)"/is', $response, $names);

Só que esse código me traz registros errados, às vezes o que preciso ele não me mostra, quero apenas nome e valor.
Lembrando nem todos os inputs estão assim, tem alguns que estão assim:
<input type="hidden" name="jazoest" value="2703" autocomplete="off">

Não possui id, ou outros atributos, mas o que eu quero é só nome e valor.
Alguma dica?
Comment: O que significa `registros errados`, tens um exemplo? Me parece que com os dois exemplos que tu deu está pegando: https://regex101.com/r/c0IvzV/1
Comment: e o teste do código também pega: https://gist.github.com/ogabrielsantos/f020c1728d8fd9b06aefa8797e5d8ab2
Comment: @GabrielSantos, sim ele estava pegando outras coisas além dos inputs, eu resolvi usando `array_combine`.
Answer: Como você mencionou o curl, estou assumindo que você tem uma string contendo todo o HTML. Nesse caso, uma alternativa é usar a extensão DOM:
$html = <<<HTML

<input type="hidden" autocomplete="off" name="timezone" value="180" id="u_0_9">
<input type="hidden" name="jazoest"
 autocomplete="off" value="2703">
<input type="radio" value="value" name="test" >

HTML;

$doc = DOMDocument::loadHTML($html);
// obter todos os elementos input
$inputs = $doc->getElementsByTagName('input');
foreach ($inputs as $input) {
    // obter o name e value
    echo $input->getAttribute('name'). '='. $input->getAttribute('value'), PHP_EOL;
}

A saída é:

timezone=180
jazoest=2703
test=value

Se quiser, você pode restringir o tipo de input fazendo algo como:
if ($input->getAttribute('type') == 'hidden') {
    // só pega os valores se o campo for type=hidden
}

Assim, você só pega os campos hidden, por exemplo. Outra alternativa para restringir a busca é usar DOMXPath:
$doc = DOMDocument::loadHTML($html);
$xpath = new DOMXPath($doc);
// obter somente input com type="hidden"
$entries = $xpath->query('//input[@type = "hidden"]');
foreach ($entries as $entry) {
    echo $entry->getAttribute('name'). '='. $entry->getAttribute('value'), PHP_EOL;
}

Neste caso, ele só busca por input's que possuem o type="hidden". A saída é:

timezone=180
jazoest=2703

Usando regex
A solução acima eu acho mais simples do que usar regex. Não que seja "errado" usar regex, só acho que neste caso eu acho mais adequado usar um parser de HTML (seja o DOMDocument, seja qualquer outro desta lista), já que ele consegue lidar com várias situações que são bem mais difíceis de tratar com regex.
Por exemplo, a regex que você usou: name="(.*?)" value="(.*?)". Ela só vai pegar o name e value se eles estiverem exatamente nesta ordem e com um espaço separando-os.
No teste que fiz acima, repare que eu propositalmente coloquei o atributo autocomplete entre o name e value no segundo input (e ainda coloquei uma quebra de linha no meio da tag, o que é permitido em HTML), e no terceiro input eu inverti e coloquei o value antes do name. Então esta regex só pegaria o primeiro input.
Claro que daria para arrumar isso em uma única regex gigante, mas há detalhes demais para se preocupar, caso queira uma expressão que considere todos os casos possíveis. Por exemplo, para ler as 3 tags que coloquei no exemplo anterior, pegando o name e value, ficaria mais ou menos assim:
$html = <<<HTML

<input type="hidden" autocomplete="off" name="timezone" value="180" id="u_0_9">
<input type="hidden" name="jazoest"
 autocomplete="off" value="2703">
<input type="radio" value="value" name="test" >

HTML;
$pos = 0;
// procura por uma tag input
while (preg_match('/<input\b([^>]*)>/i', $html, $match, PREG_OFFSET_CAPTURE, $pos)) {
    $conteudo = $match[1][0]; // conteúdo da tag input atual
    $pos_conteudo = 0;
    $type = '';
    $name = '';
    $value = '';
    // busca os atributos da tag input atual
    while ($pos_conteudo < strlen($conteudo) &&
           preg_match('/\b(type|name|value)\s*=\s*(?|"([^"]*)"|\'([^\']*)\'|([^\'">\s]*))/i',
                      $conteudo, $match_conteudo, PREG_OFFSET_CAPTURE, $pos_conteudo)) {
        $attr_name = strtolower($match_conteudo[1][0]);
        if ($attr_name == 'type') {
            $type = $match_conteudo[2][0];
        } else if ($attr_name == 'name') {
            $name = $match_conteudo[2][0];
        } else if ($attr_name == 'value') {
            $value = $match_conteudo[2][0];
        }
        // continua buscando os atributos de onde parou o anterior
        $pos_conteudo = strlen($attr_name) + $match_conteudo[1][1];
    }
    // a próxima chamada de preg_match começa depois do conteúdo já encontrado
    $pos = strlen($conteudo) + $match[1][1];
    echo $type. '='. $name. '='.$value, PHP_EOL;
}

A saída é:

hidden=timezone=180
hidden=jazoest=2703
radio=test=value

Novamente, você pode fazer if ($type == 'hidden') ou algo do tipo se quiser restringir algum tipo de input específico (ou mudar na própria regex acima, se quiser).
Basicamente, esta solução se aproveita do fato de preg_match poder receber como parâmetro a posição na qual começará a busca. E usando a opção PREG_OFFSET_CAPTURE, a posição em que o match é encontrado também é retornada no array de matches (assim eu consigo saber onde a tag foi encontrada, e posso continuar a busca a partir dali).
Note que eu não uso muito .*?, já que o ponto corresponde a qualquer caractere (e como você usou a opção s, isso faz com que o ponto também considere as quebras de linha). Apesar de .*? parecer uma solução simples e que "funciona", ela tem o seu preço.
Basicamente, como .* corresponde a quaisquer caracteres, ele pode ir além da tag atual (por exemplo, se alguma tag tiver name mas não tiver value). Ex:
$html = <<<HTML
<input type="hidden" name="semvalue">
<input type="hidden" value="abc">
HTML;

if (preg_match_all('/name="(.*?)" value="(.*?)"/is', $html, $matches)) {
    var_dump($matches);
}

Com a opção s, o ponto considera quebras de linha. Com isso, a regex procura por name=" e em seguida vai procurando por todos os caracteres da string (incluindo quebras de linha), até encontrar " value=". Por isso ela acaba "invadindo" outra tag, e o resultado é:
array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(49) "name="semvalue">
<input type="hidden" value="abc""
  }
  [1]=>
  array(1) {
    [0]=>
    string(30) "semvalue">
<input type="hidden"
  }
  [2]=>
  array(1) {
    [0]=>
    string(3) "abc"
  }
}

Outra coisa que o .*? pode fazer é ficar indo e voltando na string, verificando todas as possibilidades, tornando a regex mais ineficiente, principalmente quando você está em um trecho que não corresponde à expressão. Já usando [^"], pelo menos você garante que a regex vai parar quando encontrar o caractere ", o que já diminui bastante essa ineficiência, aleḿ de garantir que ela não vai "invadir" outras tags.
Esse é um detalhe importante: em regex é muito melhor quando você diz exatamente o que quer e o que não quer. No caso, eu não quero "qualquer coisa" (.*). Dentro de uma tag, por exemplo, o que eu quero na verdade é "qualquer coisa que não seja o fechamento da tag". Ou seja, qualquer caractere que não seja o >. Por isso eu usei:
<input\b([^>]*)>

O trecho [^>]* significa "zero ou mais caracteres que não sejam o >". Tudo que está entre [^ e ] é uma classe de caracteres negados, ou seja, tudo que for diferente do que está lá dentro. Como lá dentro só tem o >, então este trecho pega tudo que não for o fechamento da tag. Isso considera inclusive as quebras de linha, o que torna a opção s desnecessária. Mas ainda mantive a opção i, pois tags são case insensitive (ou seja, a regex pega tanto input quanto INPUT).
E como este trecho está entre parênteses, isso forma um grupo de captura, que eu posso recuperar com $match[1][0]. O $match[1] refere-se ao primeiro grupo de captura (já que é o primeiro par de parênteses da regex), e $match[1][0] contém a string que foi capturada pela regex. Já $match[1][1] contém a posição em que este match foi encontrado (esta informação só está disponível quando você passa a opção PREG_OFFSET_CAPTURE).
De qualquer forma, esta primeira regex serve para pegar todo o conteúdo de uma tag input. Depois eu uso outra regex para buscar os atributos da tag atual, por isso eu uso o conteúdo da tag encontrada, em vez do HTML inteiro. Isso restringe o espaço de busca e evita que a regex sem querer comece a olhar em outras tags (o que pode acontecer dependendo do caso, se eu usar .*, por exemplo).
O trecho (type|name|value) busca pela string "type", ou "name", ou "value" (o caractere | significa ou), e está dentro de parênteses para formar um grupo de captura, assim eu consigo obter a string que foi capturada. Em seguida temos zero ou mais espaços (\s*), sinal de igual, zero ou mais espaços, e depois temos 3 opções:

uma string delimitada por aspas duplas: "([^"]*)" ou
uma string delimitada por aspas simples: \'([^\']*)\' (pois é permitido ter name='nome') ou
zero ou mais caracteres que não sejam aspas simples, aspas duplas ou espaços
([^\'">\s]*) (já que podemos ter coisas como required ou name=nome)

Repare que os conteúdos também estão entre parênteses, o que forma outro grupo de captura. No caso dos valores entre aspas, eu deixei as próprias aspas fora do grupo de captura, assim eu garanto que só terei o valor de cada atributo.
Como há mais de uma possibilidade (entre aspas duplas, aspas simples ou sem
aspas), eu uso o (?|, que indica um branch reset, ou seja, o que for capturado será o grupo 2 (caso contrário, um seria o grupo 2, o outro seria o 3, etc, e eu teria que verificar qual está vazio e qual não está para saber qual foi capturado - com o branch reset, basta verificar o grupo 2).
Depois eu verifico qual o nome do atributo que foi capturado (type, name ou value) e pego o respectivo valor. Atualizo a posição para o próximo preg_match e continuo o loop, procurando pelo próximo atributo da tag. Quando o conteúdo da tag termina, eu imprimo o type, name e value encontrados (e eles estarão vazios caso não tenha sido encontrado) e volto para o while externo, que continua procurando pelo próximo input.

Até seria possível fazer uma única regex que reconhece ao mesmo tempo uma tag input e os seus respectivos atributos type, name e value, em qualquer ordem. Mas ficaria grande demais (seria uma junção das duas regex acima com mais algumas alternâncias para garantir que os atributos possam estar em qualquer ordem), mas sinceramente, ficaria complexa demais e na minha opinião já não começa a valer a pena.

Como você pode ver, é bem mais fácil usar um parser de HTML. A sua regex pode até funcionar para casos mais simples, mas complique um pouquinho o HTML e a complexidade da regex vai aumentando gradativamente, até se tornar impraticável.
Por exemplo, se tivermos value="<abc>", a regex do primeiro while já não funciona, pois ela pega tudo entre o <input e o primeiro > que encontrar. Aí teríamos que verificar se o > não está entre aspas, para ter certeza que ele é o fechamento da tag.
Outro caso é quando tem uma tag dentro de comentários:

A regex acima pega esta tag também, pois não colocamos a verificação para saber se ela está dentro de um comentário. Só lembrando que o comentário pode se estender por várias linhas e ter várias outras tags e conteúdos dentro dele, então verificar isso com uma regex não é nada trivial.
Já o parser de HTML cuida de todos esses casos especiais para você. A solução com DOMDocument, por exemplo, obtém corretamente os valores que têm > e ignora os comentários, sem precisar de nenhuma alteração no código. Então avalie se a regex é de fato a melhor solução para o seu caso. Pois quando se trata de parsing de HTML, nem sempre é.


How to sort a PHP array and using the same index for each element?

Question: I have this array:
numbers = array(
                 "1"=>2
                 "2"=>5
                 "3"=>1
                 "4"=>12

);

if I used sort(numbers) the array will become
numbers = array(
                 "1"=>1
                 "2"=>2
                 "3"=>5
                 "4"=>12

);

the indexes still in same places just sort the numbers
I want to move  the indexes also like the following:
numbers = array(
                 "3"=>1
                 "1"=>2
                 "2"=>5
                 "4"=>12

);

Answer: You should make use of the asort in this context.

This function sorts an array such that array indices maintain their
correlation with the array elements they are associated with. This is
used mainly when sorting associative arrays where the actual element
order is significant.

<?php
$numbers = array(1=>2,2=>5,3=>1,4=>12);
asort($numbers);
print_r($numbers);

OUTPUT :
Array
(
    [3] => 1
    [1] => 2
    [2] => 5
    [4] => 12
)


How do I get the database value of a text field with $content?

Question: I need to output a raw link from within a text field, without all the "field-" divs, in node.tpl. I've tried <?php print render($content['field_download_link']['LANGUAGE_NONE']['0']['value']); ?> but nothing displays.
Answer: The easiest way is to install and enable Devel module.

A suite of modules containing fun for module developers and themers
  ...

After that add to your page.tpl.php file this:
<?php dpm($content); ?>

You need to be logged in as administrator and you will see debugger on the page. Find the value you want to print and double-click on it, you will see the exact "path" to the value and just print it as you wrote in your question.
Comment: I did as you said but I'm not seeing where to double click. I enabled everything within Devel's configuration page but no field machine names are popping up anywhere.
Comment: try to double click anything form debugger (array, object, value...) and you'll see that the "path" will show...try to dpm some other variable, like $page...
Comment: Okay got it. Awesome module. So it's $...['content']['system_main']['nodes'][7533]['field_download_link']['#field_name'], is there something I can put in place of 7533 to have it affect all nodes? And how do I write it out exactly?
Comment: Insted of `$...` at the beggining, you write variable that you put in `dpm` and print that. For this I recommend you to override content type's tpl.php, not page.tpl.php (take a look at https://www.drupal.org/node/17565 to see how to do that), in that file you can use `` and find the value you want to print, this will be applied to all nodes of that content type. Simply, you can copy the `node.tpl.php` file from `/modules/node/` in your Drupal root to `templates` folder of your theme, and rename it to target content type as described on link that I wrote you above...
Comment: I placed  at the top of node--feed_item.tpl.php, then  where I want the field, but it just causes the site to load without any css. Definitely doing this wrong. Also can I use the modified node.tpl I was using before or does it have to be a copy of the system one?
Comment: look through Theming Documentation at https://www.drupal.org/documentation/theme and find out more about Drupal Debugging at http://randyfay.com/debugging_drupal


What is the difference between mono and stereo?

Question: I've always wondered what the difference between the two is, and sometimes (games, movie players, etc), you are given a choice of mono or stereo sound. I toggle both but don't hear any difference.
So, how do they actually differ?
Comment: There are 2 possibilities *why* you aren't spotting the difference immediately. One is that the speech centre track of a movie *is* mono, only the effects & music will be in stereo; The second is that either the source [some 'transfers' from DVD/BD are *very* poor] or your hardware is wrongly configured for stereo. There is a very distant third, that you are deaf in one ear, even if only at higher frequencies - but you'd presumably already know if that was the case.
Comment: If you listen to basically any music in headphones and switch between mono and stereo, the difference should be clear.
Answer: The difference is in the number of channels (signals) used. Mono uses one, stereo uses more than one.

In monaural sound one single channel is used. It can be reproduced through several speakers, but all speakers are still reproducing the same copy of the signal.
In stereophonic sound more channels are used (typically two). You can use two different channels and make one feed one speaker and the second channel feed a second speaker (which is the most common stereo setup). This is used to create directionality, perspective, space.

Here is an example using a two speaker setup.
Mono

Stereo

More technically, true stereo means sound recording and sound reproduction that uses stereographic projection to encode the relative positions of objects and events recorded.
In a common stereo setup of two channels: left and right, one channel is sent to the left speaker and the other channel is sent to the right speaker. Now, by controlling to which channel you send the signal you can control the position of the sound. You'll hear sounds coming from different directions depending on which speaker you send the signal to, or in which proportion (you can send just a little more to the right speaker, so the sound is positioned just a little bit to the right). Sounds with equal proportions on both speakers will appear to come from the center.
In other words, stereo opens the possibility of playing with sound localization.
Answer: Since we have two ears, on opposite sides of our head, we hear a particular sound from two directions.Each takes a slightly different time to arrive, so we have a good idea where the sound is coming from.To record this stereo sound, two mics(at least) are used.Actually, the best theoretical position for these two mics is at 180 degrees away from each other - just like our ears.
In a situation where, say, a band is playing, sound will come from lots of different directions, so several mics can be used, and the position in the mix - pan- can be moved using the recording desk. When you listen to something in stereo with headphones, you can imagine where the guitarist is playing, inside your head.Often, the vocals are dead centre. Obviously, the effect is similar using speakers, but it's easier to appreciate where things are in a mix using cans.
With mono, there is no spaciality. The sound in each ear is identical. It can be recorded with just one mic.
The concept came about when someone listened to recordings that were done with the mic in different places, and he realised that two mics would 'hear' two different mixes.
The best way to check if a recording is stereo is to hear applause. It's very obvious whether it's mono or not.
Surround sound takes this idea further. Basically it used a delay to make the sound appear to come from farther away, but with 7 points now, it's achieved using 7 different positions for the mics, which are amplified with 7 different amps and speakers, spread round the room.
Comment: Thanks for the input. I am trying to convey a very complex concept in a simplistic way.Two directions because of two ears. For one to pose the OP's question, one would appear not to be much enlightened. The two directions idea was to set the scene. In 1933, at the World Fair, Chicago, a dummy with mics for ears was used to convey the binaural, now stereo idea to the general public.Things have moved on in time, and I think that delay was used in earlier times, probably not currently.
Comment: Did anybody mention Haas effect yet?  ;-)
Answer: The most basic definition of mono or stereo is simply that with stereo you receive information in one ear that is somewhat different to information received by the other. With mono audio your ears are receiving 100% of the same information.
Early stereo pop singles are quite interestingly and idiosyncratically mixed. For example 'King Midas in Reverse' by the Hollies has the vocals on the intro mixed 100% in the right channel. If you isolate the left channel you can hear a pleasant instrumental version of the same track. Early producers working in stereo often placed the bass and drums, often relegated to a single track on a 4-track tape, to one channel. 'I Feel Free' by Cream has the roar of the band placed in one channel, and the vocals and tambourine overdubs placed in the other. By the end of the '60s it was more commonplace to place the drums and bass in the centre of the mix and then subtly pan the vocals, and less subtly pan the guitars, keyboards and other instruments, in a bid to reproduce the spatial layout of a band onstage. While mono is sometimes seen as the poor cousin of stereo, producers such as Joe Meek were able to commit powerful and lucid mixes to tape in mono.
There are some stereo recordings that pretty much require some form of electronic analysis to demonstrate that they are stereo. Classical performances captured by overhead microphones, no more than a couple of feet apart suspended over an orchestra the width of a concert stage, don't tend to have lively stereo mixes. The two microphones will pick up their subtly different locations with regards to certain instruments, but the effect isn't to produce a jarring or vivid stereo image. Having said that again some early stereo classical records were produced so that 'sparring' instruments were placed at opposite sides of the stereo image.
Finally, if OP isn't hearing any difference between switching from mono to stereo then he could be experiencing a hardware issue somewhere, and legitimately isn't being presented with different audio upon making the switch. Speaking personally, the rather desultory soundcard in my laptop isn't capable of recording incoming audio in stereo, but recording software doesn't know this and will happily record the same incoming audio to two separate audio tracks. I would be surprised if a soundcard could only output mono audio, but a cursory Google reveals people reporting this exact problem.
Answer: ADD-ON:
As we almost do only have 'Stereo-Sound-Systems' to our disposal I'll make a little add-on with a totally practical approach.
On a two-channel-system like stereo (L/R) MONO simply means that the signal of a mono-source like a mono-microphon is distributed with the same level to both channels/speakers.
When a listener at a certain distance is sitting exactly in the center between the two speakers, the mono signal will appear to be exactly in the middle of the plain between the two speakers. Headphones leave you no choice. You are always in the middle!
Panning your signal either to the left or the right just means you are changing its position in the L/R sound-field but the signal is still mono.
Anything that really took advantage of the stereo system will definitely NOT get better by being remastered back to MONO because you are limiting the sound-space to a single channel - which on a CD gets equally distributed to both channels. There is no MONO-CD. Only MONO-MUSIC on a STEREO-CD...
If the music was absolutely mono in the first place, remastering with modern equipment could definitely help to make the sound more transparent and clear! Sometimes by even producing some pseudo-stereo-effect out of the mono signal...
Answer: The experience of listening to music in mono from a speaker, eg. that of an old table radio, versus headphones is completely different. When a speaker is projecting into a room, the sound is reflected off all the surfaces in the room, so there is still a sense of space and presence. When a mono signal is piped into headphones, the sound all seems to be coming from the center of the skull (as Consumer Reports described it many years ago) an effect which I find drab and lifeless.


Using Normal Distributions to find Proportion

Question: The height of a randomly selected woman from a population is normal with $\mu=165cm$ and $\sigma=7cm$. The heights f the men in this population are normal with $\mu=178cm$ and $\sigma = 8cm$.
I am asked: What proportion of the men are shorter than half of the women?
Since the populations follow two normal distributions, and I am supposed to find the proportion of the men that are shorter than half of the women, I believe I have to find the normal distribution of men area under the curve that occurs before half of women. I think this equates to the probability of lying left of the $z$ score.
I know the formula for calculating the $z$ score is:
$z=\frac{\bar x-M}{\sigma}$
Where $\bar x$ is the mean, and $M$ is the measurement of height, and $\sigma$ is a standard deviation.
However, I am uncertain of what values I should use given to me to substitute into this equation. After I find the $z$ score, I have to use the standard normal distribution table to find the proportion of men that are shorter than half of women.
Sorry if my reasoning sounds confusing, I am very new to this topic and not sure if I am approaching the problem correctly/in the easiest possible way.
Answer: I would break this question up into two pieces.  First, what height are half of the women taller than?  Since the mean of the women distribution is $165$ and the median of a normal distribution is its mean, the answer is 165.
The second half of the question is: What proportion of men are less than the height we just calculated. i.e. What proportion of men are less than 165 cm.  That is just asking what is the probability that a normal distribution with mean 178 and standard deviation 8 is less than or equal to 165.  So you need to calculate:
$$P(X \leq 165)$$
where $X$ is the normal distribution for male heights.
Comment: Similar to the question I asked in the other answer to this question, how do we know which number corresponds to $\bar x$ and which corresponds to $M$ ?
Comment: @mnmarkets - Your formula is a bit odd.  Usually you would write: $z = \frac{x - \bar{x}}{\sigma}$.  There $x$ is the outcome you are asking the probability about (e.g. 165 above), $\bar{x}$ is the mean, and $\sigma$ is the standard deviation.
Comment: How do I know to use the standard deviation ($\sigma$)  of men rather than women? I know we are calculating the proportion of men, but is there a more in depth explanation?
Comment: @mnmakrets  They are asking about the distribution (proportion) of men.  The info that describes that is that its a normal distribution with the male mean and standard deviation.
Comment: @mnmakrets - There is nothing really more to it than that.
Answer: due to symmetry, half the women are shorter that 165, so you need to calculate $$p(M<165)=p\left(z<\frac{165-178}{8}\right)$$
Can you finish it?
Comment: How do we know to subtract the average height of men from the average height of women, not the other way round?
Comment: is it because we are calculating the proportion of MEN shorter relative to half of women, not the reverse?
Comment: the formula for the normal distribution is $z=\frac{x-\mu}{\sigma}$, where $x$ is the value of interest, in this case $165$, and $\mu$ is the mean of the distribution of men
Comment: yes to your second question!
Comment: Thank you! What specifically is the value of interest ( variable $x$ )? I can see that for this question it is the mean height of women, but for other questions involving normal distributions, how can I be certain which number corresponds to which variable in a question?
Comment: it's usually some value that the variable **could** be, apart from the mean, but it very much depends on the wording of the question...
Comment: Once I calculate $z$, I know that $p( z \lt -1.625)$. Using a normal distribution table such as the one in the link: http://www.pavementinteractive.org/wp-content/uploads/2007/08/Normal_table.gif   How do I determine which above horizontal value it corresponds to?
Comment: either -1.62 or -1.63 or interpolate (take the average)
Comment: Does the top horizontal row of numbers correspond to the standard deviation for men  ($\sigma = 8cm = 0.08 m$)? If so, will the proportion of men that are shorter than half of the women be equal to $ 0.0465$ ?
Comment: This is if using $z$ as $-1.62$


Unity Error CS0103: O nome 'newRotation' não existe no contexto atual

Question: Estou acompanhando a série do Nils Nightmare Unity Training day e estou na videoaula 2, mas na hora de compilar dá esse erro:  Error CS0103: O nome 'newRotation' não existe no contexto atual (CS0103) (Assembly-CSharp), linha 62.
O código é o do PlayerMovement.cs. Aqui está o código todo:
1
2 public class PlayerMovement : MonoBehaviour
3  {
4
5   public float speed = 6f;                      // Velocidade do Jogador
6
7   Vector3 movement;                            // Vetor responsavel pelo   movimento
8   Animator anim;                               // Responavel pela transicao da animacao
9   Rigidbody playerRigidbody;                   // Responsavel pela fisica do objeto
10  int floorMask;                               // Mascara de chao
11  float camRayLenght = 100f;                   // Inf para raycast
12
13  void Awake()
14  {
15      floorMask = LayerMask.GetMask ("Floor");
16
17      //Atribuir as referencias
18      anim = GetComponent <Animator> ();
19      playerRigidbody = GetComponent <Rigidbody> ();
20
21
22  }
23
24  void FixedUpdate()
25  {
26      float h = Input.GetAxisRaw ("Horizontal");
27      float v = Input.GetAxisRaw ("Vertical");
28
29      Move (h,v);
30      Turning ();
31      Animating (h, v);
32
33  }
34
35  // Movimento
36  void Move (float h, float v)
37  {
38      // Determina o movimento
39      movement.Set (h,0f,v);
40
41      // Normaliza o movimento
42      movement = movement.normalized * speed * Time.deltaTime;
43
44      // Efetua o movimento no personagem
45      playerRigidbody.MovePosition (transform.position + movement);
46
47  }
48
49  // Girar o jogador
50  void Turning ()
51  {
52      Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
53
54      RaycastHit floorHit;
55
56      if(Physics.Raycast(camRay,out floorHit,camRayLenght,floorMask))
57      {
58          Vector3 playerToMouse = floorHit.point - transform.position;
59          playerToMouse.y = 0f;
60
61          Quaternion nevRotation = Quaternion.LookRotation(playerToMouse);
62          playerRigidbody.MoveRotation(newRotation);
63
64
65
66      }
67
68  }
69
70  void Animating(float h, float v)
71  {
72      bool walking = h != 0f || v!=0f;
73      anim.SetBool("IsWalking", walking);
74
75  }
76
77 }

Answer: Tem um errinho na linha 61
61          Quaternion nevRotation = Quaternion.LookRotation(playerToMouse);
62          playerRigidbody.MoveRotation(newRotation);

Você escreveu nevRotation para o nome da variável, e quando você usa ela você usa newRotation troque aquele nev por new que vai resolver.


Probability of a sample mean higher than population mean

Question: I have solved this question but I am not sure if what I did is correct:

It is found that the population mean and standard deviation for the paper are 45.292 and 18.761 respectively.  Find the probability of a random sample of size 250 giving a sample mean at least as high as the one found in the sample above (47.488)

<code>The sample mean above = 47.488
simple size = 250
Sample size as high as the one found, therefor one tailed Z table (upper)

I calculated Z score as:

z_score = (xbar−mu)/(sigma/sqrt(n))
z_score = (47.488 - 45.292)/(18.761/sqrt(250))
z_score = 1.850744

</code>
With this value, I calculated the probability with upper tailed Z table to be 96.79%
I just want to check if my rationale behind the example is correct.
Thanks in advance
Comment: A question about probability is just that: a probability calculation.  The reference to "confidence level," although it is related, is irrelevant.  Ultimately, the correctness of your answer rests (at least in part) on the meaning of "it is found that:" the values 45.292 and 18.761 must therefore be *descriptive statistics.*  What kind of dataset was used, how large was it, and what formulas were used to produce these two numbers?  Most likely this is a textbook or exam question and the person who created it gave no thought to this issue, but in real problems it's a crucial one.
Comment: Hi Whuber, this is just an example in a book. We are assuming the 45.292 and 18.761 are the population parameters, and from there we are calculating the probability.
Answer: Your answer is correct.
This is the basic idea underlying the z-test (which is correct for a sample size equal to 250 and known variance).
For the central limit theorem, you have $\bar{x}|\mu,\sigma^2 \sim N(\mu, \sigma^2/N)$ for $N \rightarrow +\infty$
which means that (considering $N=250$ large enough)
$$
z(\bar{x})=(\bar{x}-\mu)/(\sigma/\sqrt{N})\sim N(0,1)
$$
you can calculate the probability $P(\bar{x}>47.488)=1-\Phi_Z(z(47.488))$ where $\Phi_Z$ is the standard normal cumulative distribution function.
Comment: Thank you so much Ping. I have also realised that I need to subtract [1 - 0.9679] to calculate P > 47.488, as you stated above. I really appreciate your answer. I just wanted to be sure. Best regards.
Comment: As an exercise you may want to compare with the cumulative of the T-distribution. Best wishes


How to apply Grammatical Evolution string to a solution

Question: I am learning Grammatical Evolution, but one thing that I can't seem to grasp is how to use the strings that are evolved from grammar into solving an actual problem. Is it converted into a neural network or converted into an equation, or something else? How does it receive inputs and print out outputs?
Answer: Grammatical Evolution (GE) makes distinction between genotype and phenotype (genotype–phenotype distinction), which means an evolved genotype is not a solution by itself, but it maps to a solution.
Mutations and crossover are performed over genotypes, but to evaluate the fitness a genotype should be first transformed into a phenotype. In Grammatical Evolution this means generation of a string conforming to the chosen grammar. This solution string then should be executed, and the result of the execution evaluated to estimate the fitness of the solution.
How to actually execute the generated solution?
It highly depends on the implementation of a GE system.
If it generates solutions in some real programming language, they should be compiled and/or executed with the corresponding toolchain, ran with some test input, and  the output evaluated to estimate the fitness.
If a GE system is able to execute a solution internally, no external toolchain is involved. It might be convenient to generate a syntax tree-like structure according to the grammar (instead of unstructured text), because it's quite easy to execute such a structure.
How to execute a syntax tree?
There exist an entire class of so called tree walk interpreters — not super performant, but reasonably simple in implementation. Usually such an interpreter first parses a source text and builds a syntax tree, then executes it; but in a GE system it is possible to directly generate a syntax tree, so no parsing is involved.
I can suggest "A tree-walk interpreter" chapter of a freely available book "Crafting interpreters" as a good example of constructing such an interpreter.


Grails - Spring Security UI Email for Username

Question: I'm using Grails 3.2.4 and am attempting to use the email property of my User class as the username for registration.
So far, I've managed to get Spring Security Core to use the email as the username using the configuration setting below:
grails.plugin.springsecurity.userLookup.usernamePropertyName='email'

However, the registration functionality doesn't seem to take this into account and won't let me register a new user using only an email and password.
I've made a few attempts at overriding the RegisterController but I continue to experience different errors regarding null usernames.
It seems like I must be missing something very simple. Any help / direction is greatly appreciated.
Comment: check constraints in User domain class for username attribute - if db already created previously and had been nullable false and you changed to true after - then a manual db upate is needed to alter table change column to be nullable. You are giving an oversight without any actual code or error messages - traces or anything for anyone to give you anything useful on
Comment: Thanks @vahid, No traces were needed for my question as I was asking how to make the registration function in spring-security-ui use the email as a username.
Answer: It appears that in version spring-security-ui-3.0.0.M2 the username property might not be override-able.
String paramNameToPropertyName(String paramName, String controllerName) {
    // Properties in the ACL classes and RegistrationCode aren't currently
    // configurable, and the PersistentLogin class is generated but its
    // properties also aren't configurable. Since this method is only by
    // the controllers to be able to hard-code param names and lookup the
    // actual domain class property name we can short-circuit the logic here.
    // Additionally there's no need to support nested properties (e.g.
    // 'aclClass.className' for AclObjectIdentity search) since those are
    // not used in GSP for classes with configurable properties.

    if (!paramName) {
        return paramName
    }

    Class<?> clazz = domainClassesByControllerName[controllerName]
    String name
    if (clazz) {
        String key = paramName
        if (paramName.endsWith('.id')) {
            key = paramName[0..-4]
        }
        name = classMappings[clazz][key]
    }
    name ?: paramName
}

PS I'm getting around this now by doing something like this in my user domain class:
static transients = ["migrating"]]
String username = null

public void setEmail(String email) {
    this.email = email
    this.username = email
}

Comment: Thank you! Exactly the information I was looking for.


Classification threshold selection for predictions on unseen data

Question: In binary classification, what is the optimum probability threshold to predict binary outcomes (0/1) on unseen data without knowing the actual outcome?
Let's assume that a random forest model has been trained on a training dataset using n-fold cross validation and the classification probability threshold is set to the value maximizing the F1 score.
Thus, we have a training probability threshold and a cross validation probability threshold.
Which one is to be used for predicting the class (0/1) of the unseen data when applying the model?
Answer: I have two remarks, which do not answer your question:
1- when you train a RF model you do not need to perform cross validation:
https://datascience.stackexchange.com/questions/6510/does-modeling-with-random-forests-requre-cross-validation
2- which threshold you choose has nothing to do with statistics, it is a decision you need to take depending on the problem at hand (this is a really nice answer):
Classification probability threshold
So I guess this second point helps me to rephrase your question: assuming that I want to maximize the F1 score also in the unseen data (test set), which threshold should I use, the one maximising the F1 score in the cross validation or in the training?
I think the one in the training. Because cross validation just helps you choose/validate the model. Also I do not expect the two thresholds should differ much in absence of overfitting.
Comment: Agree on the methodology, however this does not answer my question. I want to use cross-validation, it gives more robust results. Essentially my question is, after I have done all of the above, and get a new batch of data to score, what is the "best" threshold to apply in order to tranform scores into predictions? Could be the one from the independent test set...however, sometimes the test set is small and may not capture seasonality in data. Thanks for taking the time though.
Comment: You are right, sorry, I totally misunderstood the question. I edited my answer accordingly.


Normalized coordinate of point on 4-sided concave polygon

Question: Considering I have concave polygon made up of 4 points: P1, P2, P3, P4, and a point M which I already know is inside this polygon.
How would I go about determining its "normalized" position in an equivalent, non-deformed rectangle?
Here is an example with a grid which makes it easy to see "with the eyes".

I am trying to write an algorithm to calibrate a touch-screen which is projected on a possibly non-flat surface and I need to know what a point corresponds to on a rectangle.
Math is not exactly my strong, so I would love an analytical, ready-to-code solution to this math problem!
Answer: Well, that was hard, but I figured it out!
The answer by @Andrei was correct, but it only works for an affine transform, which consists of just scaling, translating, shearing and rotating. An affine transform means parallel lines remain parallel. This was not my case as you can see the quadrilateral is more akin to a perspective transform.
The key was that in the case of a non-affine transform, also known as a perspective transform, the transformation matrix $T$ is more complex. The terms $g$ and $h$ are non-zero. Also, the $x$ and $y$ coordinates of the resulting transformation needs to be divided by $z$:
$$p_r'=\begin{pmatrix}x_r\\y_r\\1\end{pmatrix}T=\begin{pmatrix}x'\\y'\\z'\end{pmatrix}$$
$$p_r=\begin{pmatrix}x\\y\\1\end{pmatrix}=\begin{pmatrix}x' \over z'\\y' \over z'\\1\end{pmatrix}$$
Where $p_r$ is the resulting point position after being transformed by the matrix (forward transform).
Now, what we want is to find the inverse transformation. The formula is quite similar
$$p_b'=p_r T^{-1}=\begin{pmatrix}x'\\y'\\z'\end{pmatrix}$$
$$p_b=\begin{pmatrix}x\\y\\1\end{pmatrix}=\begin{pmatrix}x' \over z'\\y' \over z'\\1\end{pmatrix}$$
Now the hard part is finding the coefficients for the transform matrix, since it has no zero element.
$$T=\begin{pmatrix}a_{11}& a_{12}& a_{13}\\a_{21}& a_{22}& a_{23}\\a_{31}& a_{32}& a_{33}\end{pmatrix}$$
Thanks to the paper linked below, I was able to find these coefficients rather easily (watch out, the interesting part has a few typos!):
https://www.ldv.ei.tum.de/fileadmin/w00bfa/www/content_uploads/Vorlesung_3.2_SpatialTransformations.pdf
Considering
$$P_1=\begin{pmatrix} x_1 \\y_1 \\0 \end{pmatrix}$$
$$P_2=\begin{pmatrix} x_2 \\y_2 \\0 \end{pmatrix}$$
$$P_3=\begin{pmatrix} x_3 \\y_3 \\0 \end{pmatrix}$$
$$P_4=\begin{pmatrix} x_4 \\y_4 \\0 \end{pmatrix}$$
are the 4 points of the quadrilateral in clockwise order (in the case of a "computer graphics" referential; in the case of a "mathematical" referential, the order would be anti-clockwise)
$$\Delta x_1=x_1 - x_2, \Delta x_2=x_3 - x_2, \Delta x_3=x_0 - x_1 + x_2 - x_3$$
$$\Delta y_1=y_1 - y_2, \Delta y_2=y_3 - y_2, \Delta y_3=y_0 - y_1 + y_2 - y_3$$
$$a_{13}={\begin{vmatrix} \Delta x_3& \Delta x_2\\ \Delta y_3& \Delta y_2 \end{vmatrix} \over \begin{vmatrix} \Delta x_1& \Delta x_2\\ \Delta y_1& \Delta y_2 \end{vmatrix}}$$
$$a_{23}={\begin{vmatrix} \Delta x_1& \Delta x_3\\ \Delta y_1& \Delta y_3 \end{vmatrix} \over \begin{vmatrix} \Delta x_1& \Delta x_2\\ \Delta y_1& \Delta y_2 \end{vmatrix}}$$
$$a_{11}=x_1-x_0+a_{13}x_1$$
$$a_{12}=y_1-y_0+a_{13}y_1$$
$$a_{21}=x_3-x_0+a_{23}x_3$$
$$a_{22}=y_3-y_0+a_{23}y_3$$
$$a_{31}=y_0$$
$$a_{32}=y_0$$
$$a_{33}=1$$
Then, it was just a matter of inversing this matrix and doing the operation I described above.
Here is a jsfiddle showing the process in action:
https://jsfiddle.net/floriansegginger/jm57pbzg/40/
Comment: Thanks for the nice jsfiddle. Can you add some explanation about what the third coordinate in the mapped point means? Also how could one determine if the point is outside or inside the distorted rectangle?
Comment: Oh and is this an error, or am I not understanding completely:
https://imgur.com/a/RtrlTvz

I expect the bottom right corner to map to [1/1], but the result is arount [0.4, 0.4]?!
Answer: You can write the transformation matrix from the final state (let's use a square of side $1$) to the stretched polygon as a 3x3 affine matrix $T$. You can read more about it on wikipedia. Look at the last image. The matrix has the form $$T=\begin{pmatrix}a& b& c\\d& e& f\\0& 0& 1\end{pmatrix}$$
For the coordinates you use a vector notation $$\begin{pmatrix}x\\y\\1\end{pmatrix}$$
Let's suppose that $P_1$ is the transform of $(0,0)$. Then $c=x_1$ and $f=y_1$. Then continue with $P_2$ as the transform of $(1,0)$. You get $$a+c=x_2\\d+f=y_2$$ or $$a=x_2-x_1\\d=y_2-y_1$$
You can work out the other matrix elements. Now we know that $$T\begin{pmatrix}x_r\\y_r\\1\end{pmatrix}=\begin{pmatrix}x_M\\y_M\\1\end{pmatrix}$$
You can now multiply on the left with $T^{-1}$ and you get $$\begin{pmatrix}x_r\\y_r\\1\end{pmatrix}=T^{-1}\begin{pmatrix}x_M\\y_M\\1\end{pmatrix}$$
Comment: Thanks for the answer! However, my transformation is not affine, as you can see from my drawing. I am looking for a more generic case. Could you help?
Comment: What do you mean by saying that it's not affine? If you start from a square, you stretch it differently, shear it, rotate it, and translate it, you get your quadrilateral. That's an affine transformation
Comment: You might need to change some of the zerros in $T$ to some finite values
Comment: I think the transformation is not affine because it's more akin to a perspective transform? It is not just sheared, rotated, and translated. Lines do not remain parallel.
Comment: You are right, that's why you should modify the transformation matrix. It's still a 3x3 matrix. In one of the figures in the article on Wikipedia you have the example of how the matrix would look like.
Comment: I will try that. Do the "z" coordinates of the resulting points remain at 1?
Comment: I think they should.
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/87030/discussion-between-florian-segginger-and-andrei).


Inherit from @android:style/Theme.Holo.Light in a minSdkVersion="4" application

Question: I currently have an Android application targeting version 11 of the SDK with a minimum version of 4. I would like to apply the Light version of the Holo theme if the app is running on Honeycomb. Following the instructions here I have tried adding the following to res/values-v11/themes.xml but my application will not compile because @android:style/Theme.Holo.Light does not exist in SDK 4
<code><resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    </style>
</resources>
</code>
Is is possible to get a reference to that theme without having to build separate 1.6-2.x and 3.x versions of my application?
Comment: Setting minimum SDK to 4 in the manifest doesn't mean you have to set the build target to 4 - you can leave that at 11.
Answer: As EboMike pointed out in the comments this was a problem with setting the build target to the Minimum SDK version instead of the Target SDK version.


Looping Through Futures - Should you check isDone first?

Question: I have a list of futures.
List<Future> futures = new ArrayList<Future>();

futures.add(task1());
futures.add(task2());

Is there a difference between these two approaches of getting the results?
// Option 1
for (Future future : futures) {
  results.add(future.get());
}

// Option 2
boolean incompleteFound;
do {
 incompleteFound = false;
 for (Future future : futures) {
    if (!future.isDone()) {
      incompleteFound = true;
    }
 }
} while (incompleteFound);

for (Future future : futures) {
  results.add(future.get());
}

I would assume in Option 1 while the get call is blocking, this wouldn't affect the processing of the futures in the background. Am I correct in that assumption?
Answer: Correct. Do the first one. Waiting for one future's result won't affect any of the others (nor even the one you're waiting on).
The second one is not only unnecessary but also super wasteful. Instead of yielding the CPU to other threads you now have a busy loop that'll spin the CPU consuming CPU cycles for no reason.
Answer: Yes, it won't affect, all the future tasks runs parallelly in background.Refer: https://javarevisited.blogspot.com/2015/01/how-to-use-future-and-futuretask-in-Java.html#axzz5rLieL6lf


How to share any variable/object between BDD scenarios with Cucumber and Java?

Question: Here the login API call is common among all the scenarios, so I tried to use "Background". But in our case after login API call we get Session ID and this session ID needed to be passed as a header to all the calls happens after the Login call. Also Login call can not happen multiple time for the same user, so the login call should only happen for the one time and next calls should use only use the output of the login call happened first time.
Is there any in built technique in Cucumber BDD to handle this situation? I just do not want to write the same Login steps in each of the scenarios.
Answer: You can use global variable example:
<code>public class Test{

public static final String NAME = "xpto"; // global}

System.out.println(Test.NAME);
</code>
With Ruby this is very easy:
We need add only @
<code>    # steps1.rb
Given /^a person exists by the name of John Galt$/ do
  @person = Person.new
  @person.name = 'John Galt'
end

# steps2.rb
Then /^his name should be John Galt$/ do
  @person.name.should == 'John Galt'
end
</code>
Comment: Actually am searching for a solution in Java only
Comment: The firat solution is Java.


When is innodb_lock_wait_timeout ignored?

Question: I'm having a weird case of the classic deadlock exception:

Caused by: java.sql.BatchUpdateException: Deadlock found when trying to get lock; try restarting transaction.

The code is identified and indeed, it's possible to get a deadlock. The thing is that I'm getting the exception immediately. I've set the innodb_lock_wait_timeout variable to 1 hour (3600). But still, the exception comes right away.
What exactly is happening?
Comment: Maybe MySQL's gotten smart enough to recognize circular deadlocks and not have to wait for the timeout to hit.
Comment: Let's hope not. :-) Is it possible to override the timeout at runtime ?
Answer: You are hitting a deadlock the lock_wait_timeout variable is how long a query will wait to acquire locks before timing out - not the same thing.
From the manual:
The timeout in seconds an InnoDB transaction may wait for a row lock before giving up. The default value is 50 seconds. A transaction that tries to access a row that is locked by another InnoDB transaction will hang for at most this many seconds before issuing the following error:
You'd see this if you were hitting the timeout.  You have a proper deadlock that needs fixing.
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Comment: is there any way to check effective value of lock_wait_timeout??
Comment: To see the current values, run `show global variables like '%wait_timeout';`
Answer: https://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html#sysvar_innodb_lock_wait_timeout

The lock wait timeout value does not apply to deadlocks when innodb_deadlock_detect is enabled (the default)


Exchange 2010 Client Access Priority

Question: I have two Exchange servers set up in a DAG, both with client access roles as well as database roles.  One system is a physical machine hosted internally with a lot of horsepower behind it, while the other is a VM at a remote site offering DR capabilities.
For some reason, most if not all of my Outlook client have a preference set to connect to the remote Exchange server as opposed to the local Exchange server, even though the local has priority on all the databases and significantly more resources available to it.
Is there a way to set the priority of which Exchange server is selected as a Client Access Server by the Outlook clients?
Answer: If you have two distinct CAS servers in 2 different sites, and you want the clients associated with a specific MailboxDatabase to connect to one over the other, make sure you set the RPCClientAccessServer attribute on that specific MailboxDatabase to the FQDN of the primary CAS server.
To set the RPCClientAccessServer attribute on a mailbox, issue the following command in EMS:
Set-MailboxDatabase -Identity "MyAwesomeDatabase" –RPCClientAccessServer cas-server01.domain.tld

To ensure connectivity when the database is failed over to the secondary mailbox server, set the AllowCrossSiteRPCClientAccess attribute on the DAG:
Set-DatabaseAvailabilityGroup -Identity "MySuperSweetDAG" -AllowCrossSiteRpcClientAccess:$true

The following article will probably give you some nice pointers: http://blogs.technet.com/b/exchange/archive/2012/05/30/rpc-client-access-cross-site-connectivity-changes.aspx

Exchange 2010 Client Access Priority


Yii2 Call to undefined method setuser

Question: I'm using Yii2 module dektrium/yii2-user for user sign-up/sign-in and get the aforementioned error while trying to register.
<code>PHP User Error – yii\base\ErrorException.
in C:\xampp\htdocs\hello\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\EsmtpTransport.php
</code>
How do I find out the reason for this?
Answer: You should be very careful when configuring mailer component of your app. The problem was a typo:
<code>'viewpath' => '@app/mailer',
</code>
instead of
<code>'viewPath' => '@app/mailer',
</code>
in /appName/config/web.php

Yii2 Call to undefined method setuser


Why does this spring oauth configuration never return 401 unauthorized?

Question: I followed the tutorial here to attempt to get started with spring Oauth. However, no pages return 401 unauthorized, even though I have not authenticated with the server (the configuration examples in the tutorial supposedly disable anonymous access. The only comment on the tutorial related to this issue has a single response saying the authorization has not been enabled, but I have @EnableAuthorizationServer on one of the configuration classes.
all config classes
Application:
@SpringBootApplication
@EnableTransactionManagement
@EnableAutoConfiguration(exclude = {RepositoryRestMvcAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Authorization Server:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    private static String REALM = "EXAMPLE_REALM";

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private UserApprovalHandler handler;

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
               .withClient("trusted-client")
               .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
               .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
               .scopes("read", "write", "trust")
               .secret("secret")
               .accessTokenValiditySeconds(300)//invalid after 5 minutes.
               .refreshTokenValiditySeconds(600);//refresh after 10 minutes.
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore).userApprovalHandler(handler)
                 .authenticationManager(authManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.realm(REALM + "/client");
    }
}

Resource Server (note that I changed the antMatchers to anyRequest for testing purposes):
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    private static final String RESOURCE_ID = "SPRING_REST_API";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID).stateless(false);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.anonymous().disable()
            .requestMatchers().anyRequest()
            .and().authorizeRequests()
            .anyRequest().access("hasRole('ADMIN')")
            .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
    }
}

"Security":
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private ClientDetailsService clientService;

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("javabycode").password("123456").roles("USER")
            .and()
            .withUser("admin").password("admin123").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .anonymous().disable()
            .authorizeRequests()
            .antMatchers("/oauth/token").permitAll();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    @Bean
    @Autowired
    public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
        TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
        handler.setTokenStore(tokenStore);
        handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientService));
        handler.setClientDetailsService(clientService);
        return handler;
    }

    @Bean
    @Autowired
    public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
        TokenApprovalStore store = new TokenApprovalStore();
        store.setTokenStore(tokenStore);
        return store;
    }
}

Probably Unrelated, but there is also:
@Configuration
@EnableWebMvc
@EnableWebSecurity
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

After reading through the full source code of the tutorial, I added
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        return new OAuth2MethodSecurityExpressionHandler();
    }
}

Finally, the build.gradle for the project since I have often had issues with specific versions of dependencies when working with spring:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
        classpath('com.h2database:h2:1.4.193')
        classpath('org.hibernate:hibernate-core:5.0.12.Final')
        classpath('org.springframework.boot:spring-boot-starter-security:1.5.2.RELEASE')
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
sourceCompatibility = 1.8
targetCompatibility = 1.8

String appVersion = "1.0.0"

war{
    baseName="cubscout-rest"
    version=appVersion
    doFirst {
        manifest {
            attributes("Implementation-Title": 'cubscout-rest', "Implementation-Version": appVersion, "Implementation-Timestamp": new Date())
        }
    }
}

repositories {
    jcenter()
}

dependencies {
    compile project(':core')

    compile 'org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.1.RELEASE'
    //compile 'org.springframework.integration:spring-integration-core:4.3.7.RELEASE'
    //compile 'org.springframework.batch:spring-batch-core:3.0.7.RELEASE'
    compile 'org.springframework.data:spring-data-jpa:1.11.0.RELEASE'
    compile 'org.springframework.data:spring-data-rest-webmvc:2.6.0.RELEASE'
    compile 'org.springframework.security:spring-security-web:4.2.1.RELEASE'
    //compile 'org.springframework.boot:spring-boot-starter-security:1.5.2.RELEASE'
    compile 'org.springframework.security.oauth:spring-security-oauth2:2.1.0.RELEASE'
    compile 'org.hibernate:hibernate-core:5.0.12.Final'
    compile 'com.h2database:h2:1.4.193'
    compile 'org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-all:1.10.19'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE'
}

Answer: One of Spring Boot's autoconfigurations was interfering. changing
@SpringBootApplication
@EnableTransactionManagement
@EnableAutoConfiguration(exclude = {RepositoryRestMvcAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {

with
@SpringBootApplication
@EnableTransactionManagement
@EnableAutoConfiguration(exclude = {RepositoryRestMvcAutoConfiguration.class, OAuth2AutoConfiguration.class})
public class Application extends SpringBootServletInitializer {

allows my configurations to function properly.


Find zeros of a polynomial and apply them to an another equation

Question: If $a$ and $b$ are the zeros of quadratic polynomial $x^2-4x-5$, find the value of $\frac1{a^3} + \frac1{b^3}$.
Comment: Welcome to MSE. What is your question?
Answer: Another way to approach it without solving the corresponding equation. One has
\begin{align*}
\frac{1}{a^{3}} + \frac{1}{b^{3}} = \frac{a^{3}+b^{3}}{a^{3}b^{3}} = \frac{(a+b)(a^{2} - ab + b^{2})}{a^{3}b^{3}} = \frac{(a+b)[(a+b)^{2} - 3ab]}{a^{3}b^{3}}
\end{align*}
Since $a + b = 4$ and $ab = -5$, we conclude that
\begin{align*}
\frac{1}{a^{3}} + \frac{1}{b^{3}} = -\frac{4\times(4^{2} + 15)}{125} = -\frac{124}{125}
\end{align*}
Hopefully this helps.
Answer: We know that:
$$x^2-4x-5=(x-5)(x+1)$$
The roots are:
$$x=5 \vee x=-1$$
Now, we can compute the value of:
$$\frac{1}{a^3}+\frac{1}{b^3}=\frac{1}{5^3}-1=-\frac{124}{125}$$
Comment: HEY, thanks for the answer!!
Comment: @Skandabhairava: if this answer has been useful, maybe a $+1$ or a favourite answer?


Negation bar meaning?

Question: I know that the horizontal bar on top means it's a negation. But I've never encountered one over more than one term like this one:
$\overline{\bar{x} + \bar{y}x}(y + \overline{xy})$
Is that equivalent to:
($\neg{(\bar{x} + \bar{y}x))}(y + \overline{xy})$ (the 2 first terms are negated then they are multiplied by the two last terms)
or
$\neg{\bar{x}} + (\neg{\bar{y}x)}(y + \overline{xy})$ (the first two terms are negated, but only the second term is multiplied by the two last terms)
or just.. something else?
Thanks!
Answer: Your first intuition was correct, it is equivalent to
$$
\left(\neg\left(\overline{x}+\overline{y}x\right)\right)\left(y+\overline{xy}\right)=\neg\left(\neg x+\left(\neg y\right)x\right)\left(y+\neg\left(xy\right)\right)
$$
Answer: The first equivalence you have given was the correct one.
You can use the De-Morgan's Law to simplify it.

Negation bar meaning?


Not able to Select after using Expression Tree in Group By

Question: I have been referring this post to group by using expression tree. Here is my code:
String[] fields = { "DepartmentID", "SkillID" };
var groupLambda = GroupByExpression<Person>(fields);
var query = dbContext.People.GroupBy(groupLambda.Compile());
var queryResult = query.ToList();

Here is the method GroupByExpression which uses solution given in aforesaid post (Thanks Daniel!):
public static Expression<Func<TItem, object>> GroupByExpression<TItem>(string[] propertyNames)
{
var properties = propertyNames.Select(name => typeof(TItem).GetProperty(name)).ToArray();
var propertyTypes = properties.Select(p => p.PropertyType).ToArray();
var tupleTypeDefinition = typeof(Tuple).Assembly.GetType("System.Tuple`" + properties.Length);
var tupleType = tupleTypeDefinition.MakeGenericType(propertyTypes);
var constructor = tupleType.GetConstructor(propertyTypes);
var param = Expression.Parameter(typeof(TItem), "x");
var body = Expression.New(constructor, properties.Select(p => Expression.Property(param, p)));
var expr = Expression.Lambda<Func<TItem, object>>(body, param);
return expr;
}

I want to be able to identify fields in the group by keys with strong names in select part like  query.Select(x => new { x.Key.DepartmentID, x.Key.SkillID });
How do I do this?
Comment: I hope you read the note of `GenericTypeTea`! What you are doing isn't a `GroupBy` "serverside" but is a `GroupBy` "clientside", because by compiling the expression, you obtain a `Func<,>`
Answer: Now... I won't give you the solution to the question you asked, but I'll try to help you :-)
If you want to do dynamic queries, you should probably use DynamicLinq
With DynamicLinq you can do things like:
IQueryable query = context.YourTable;
var groups = query.GroupBy("new (Field1, Field2)");

I'm rereading your question...

I want to be able to identify fields in the group by keys with strong names in select part like  query.Select(x => new { x.Key.DepartmentID, x.Key.SkillID });

You can't. GroupBy in general will return a IGrouping<TKey, TSource>. TKey is dynamic (because you build it based on strings), so you can't "extract" it and pass it to the compiler, so you can't do the select with strong names.
There is a single exception: if you know the types and numbers of the GroupBy TKey then something can be done. So, you gave us:
String[] fields = { "DepartmentID", "SkillID" };

If you always have two int then you can cast your query with:
.Cast<IGrouping<Tuple<int, int>, Person>>()
.Select(x => new { x.Key.DepartmentID, x.Key.SkillID });

Note that, as I've written in a comment, your GroupBy will be executed client-side, and everything after the GroupBy will be executed client-side (where client-side == where your program is vs sql-side == where your sql server is)!
DynamicLinq will solve the problem of executing the query sql-side instead of client-side, but won't solve the problem of strong vs weak naming (after a DynamicLinq you can: A) use .Cast<>() method or B) return a dynamic object/IEnumerable<dynamic>)
Answer: The syntax you're using new { x.Key.DepartmentID, x.Key.SkillID } constructs an anonymous class at compile time. If you want to create an anonymous class at runtime, see here. However, that won't allow you to "identify fields in the group by keys with strong names". If you want to construct an anonymous class at runtime, but be able to use those names at compile time, I'm afraid that's impossible.


Fixing Autocorrelation in mini time series for a linear regression?

Question: I have data with features that looks like this
I am using ML regression in Sklearn to predict a final cost (in a separate df). Before fitting a linear regression I went to test the assumptions of linear regression and have a problem with autocorrelation. Running an autocorrelation test gave me this
As you can see the Durbin Watson is too low. Someone suggested to me since some of my variables may be affected by time, to use ARIMA models to fix it. So to test if any of my variables were non-stationary I used an ADFULLER test and got these results.
The P-value of 0 for all of the variables indicates that the variables are already stationary meaning time shouldn't be the issue. Does anyone have a suggestion to get rid of this autocorrelation in my data?
Comment: Quite confusing what you mean by autocorrelation. The autocorrelation is the concept in the time series.
Comment: The Durbin Watson statistic is a test for autocorrelation, mine shows positive, my data isn't completely a time series, even so, autocorrelation also is a concept in non- time-series datasets I just don't know how to fix it.
Comment: If it's not a time series, I don't know what's the meaning of order in your observations.
Comment: So to clarify, the % complete takes ~10 rows to get to 100% then a new 'job' begins. I filtered out a column that is JOB ID. so those first 5 rows you see are for one job, then it will move on to another, and another, for 8000 rows making a whole bunch of mini time series. Not one giant one.
Answer: The problem with traditional autocorrelation corrections (and the tests for autocorrelation you used) is that they assume equal distance between the observations for each job. Since this assumption is not met, the autocorrelation tests are wrong. Also autocorrelation tests on less than 30 observations have shaky validity. The bigger question is whether the project cost estimates are correlated with percent of project complete. If so, then figure out how to incorporate that into your models as predictors. If you have 8000 projects and don't want build separate models, maybe you can estimate normal under/overprediction of eventual project costs as a function of the percent stage of the project and the current estimate at that stage of the project.
Comment: Thanks John! I was quite surprised checking my inbox and seeing this blast from the past haha. But even now, your answer makes sense and I appreciate it!


undefined index while submitting the form

Question: I want to submit the form with empty checkbox but I want the value "no" if the checkbox is not checked.
if($Domain_Check=="yes")
{
    echo '

                Domain_Check   <input type="checkbox" name="Domain_Check1" value="yes" checked>

';
}
else {
    echo '

                Domain_Check   <input type="checkbox" name="Domain_Check1" value="yes">

';
}

Comment: please edit and update complete code.
Comment: actually this code is a part of php code. the domain check value is yes it will display the checked textbox otherwise it will display unchecked textbox
Comment: Duplicate. http://stackoverflow.com/questions/19893927/send-checkbox-value-in-php-form#answer-19894178
Comment: in the `else` part write `value="no"` instead of `value="yes"`
Comment: Only used form items get sent. If you want "no" to get passed you need to set defaults and then override them if the value is set.
Answer: Use a hidden input before the checkbox to set a default value with the same name:
<input type="hidden" name="Domain_Check1" value="no">
<input type="checkbox" name="Domain_Check1" value="yes">

The hidden input will be overridden if the checkbox is checked.


Prove that $ m_{a} \geq m_{g} \geq m_{h} $ using strict inequalities unless $ a = b $.

Question: $ m_{a} = \frac{1}{2} (a + b) $
$ m_{g} =  \sqrt{ab} $
$ \frac{1}{m_{h}} = \frac{1}{2}(\frac{1}{a} + \frac{1}{b})$
Attempted Solution:
I believe I have shown the first step, which is $ m_{a} \geq m_{g} $ but i'm stuck on showing $ m_{g} \geq m_{h} $
$ \frac{a + b}{2} > \sqrt{ab} $
$ (\frac{a + b}{2})^2 > (\sqrt{ab})^2 $
$\frac{a^2 + 2ab + b^2}{4} > ab$
$a^2 + 2ab + b^2 > 4ab $
$a^2 -2ab + b^2 > 0 $
$(a - b)^2 > 0 $
so if $a = b$ we have $(a -b)^2 \geq 0$
Therefore, $m_{a} \geq m_{g} $
Now, i'm not sure if that is correct or not, if not can you please guide me to the complete solution or if I am correct on this part, provide a solution for $m_{g} \geq m_{h} $
Answer: Hint: if you set $c=\frac{1}{a},d=\frac{1}{b}$, then
$$ m_h(a,b)\leq m_g(a,b) $$
is equivalent to:
$$ m_g(c,d) \leq m_a(c,d).$$
Comment: So, if I simply note this fact in the proof, then I can say that $m_{g} \geq m_{h}$ because $ (\frac{1}{a} - \frac{1}{b}) ^2 \geq 0 $ which directly follows from my solution of $m_{a} \geq m_{g}$ ?


Describe the interior of Cantor set

Question:
Describe the interior of Cantor set

I think the interior is empty because the cantor set of nowhere dense, but as I write it correctly?
Comment: @draks: W... What?
Comment: @draks: Both are sets of points. You may be looking for a "set of _isolated_ points" -- but the Cantor set is not such a set.
Comment: @draks: $[0,1]$ is a set of points too, and that set of points certainly has an interior.
Comment: well the cantor set contains no interval, so if the interior were not empty then....
Comment: Since the [Cantor set](http://en.wikipedia.org/wiki/Cantor_set) is a set of points, wouldn't *"What is the interior of a point?"* be an equivalent question?
Comment: @AndréCaldas A point doesn't have an interior and set of points wouldn't as well. You don't agree?
Comment: @HenningMakholm I thought more about something like $\{0,1\}$...
Comment: @HenningMakholm Fine, thanks.
Comment: Is the Cantor set a set of isolated points or a set of infinitesimal intervals?
Answer: The Cantor set is the intersection of the sets $F_i$, where
$F_1=[0,1/3] \cup [2/3,1]$,
$F_2=[0,1/9]\cup [2/9,1/3]\cup [2/3, 7/9]\cup [8/9,1]$,
and, in general:
if $F_{n}$ is defined and consists of the union of $2^n$ disjoint intervals of the form $[k/3^n, (k+1)/3^n]$, then $F_{n+1}$ is obtained by removing the "open middle third" of each of these intervals.
Note the total length of $F_n$ is $(2/3)^n$.
Suppose the non-empty open interval $I=(a,b)$ is contained in the Cantor set. Then for each $n$, $I$ is contained in $F_n$, and thus in one of the intervals defining $F_n$.  But then $0< b-a\le (2/3)^n$ for all $n$. This leads to a contradiction, as $b-a\gt0$ and $\lim\limits_{n\rightarrow\infty} (2/3)^n=0$.
So the Cantor set contains no non-empty open interval; thus the interior of the Cantor set is empty.
Comment: One could also show that no nonempty open interval is a subset by Cantor set by showing that every nonempty open interval contains a number whose ternary expansion contains a "1". Although this is a more convoluted way to prove the same fact that you did neatly, it is still pedagogically instructive.

Describe the interior of Cantor set


How can I override global PHP.INI settings on shared hosting?

Question: I have sharring hosting account on Linux Apache PHP with JustHost.
In my account exists a global php.ini that only admin of hosting company can access.
My problem is that when I run php script the warnings + errors printed on webpage.
So I create php.ini in my www folder and put 2 lines, but nothing happens:
display_errors = Off
display_startup_errors = Off

What should I do to override the global shared hosting php.ini settings?
Answer: Try putting these lines in your .htaccess file:
php_value display_errors 0
php_value display_startup_errors 0

Answer: Possible, your hosting provider restricts you from overriding default php.ini. Not always!


cannot validate certificate - doesn't contain any IP SAN

Question: I am currently in the process of installing ELK ( ElastricSearch, LogStash & Kibana) stack.
My ELK server IP address is 172.29.225.32.
Elastic Search config is ::
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 172.29.225.32
#
# Set a custom port for HTTP:
#
http.port: 9200

Then I generated my SSL config. I am using IP based connection :
vim /etc/pki/tls/openssl.cnf
```
[ v3_ca ]
subjectAltName = IP:172.29.225.32
```

Then I generated my certs.
openssl req -config /etc/pki/tls/openssl.cnf -x509 -days 3650 -batch -nodes -newkey rsa:2048 -keyout /etc/pki/tls/private/logstash-forwarder.key -out /etc/pki/tls/certs/logstash-forwarder.crt

I am using beats. So my beats config is ::
input {
  beats {
    port => 5044
    ssl => true
    ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder.crt"
    ssl_key => "/etc/pki/tls/private/logstash-forwarder.key"
  }
}

I then installed beats and configured it ::
vim  /etc/filebeat/filebeat.yml
```
output:

  ### Elasticsearch as output
  elasticsearch:
    hosts: ["172.29.225.32:9200"]
  tls:
    certificate_authorities: ["/etc/pki/tls/certs/logstash-forwarder.crt"]
  logstash:
    hosts: ["172.29.225.32:5044"]
```

When I start filebeat, I get the ERROR::
# systemctl status filebeat
● filebeat.service - filebeat
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-06-09 13:45:35 GMT; 5s ago
     Docs: https://www.elastic.co/guide/en/beats/filebeat/current/index.html
 Main PID: 27273 (filebeat)
   CGroup: /system.slice/filebeat.service
           └─27273 /usr/bin/filebeat -c /etc/filebeat/filebeat.yml

Jun 09 13:45:35 supportserver /usr/bin/filebeat[27273]: transport.go:125: SSL client failed to connect with: x509: cannot validate certificate for 172.29.225.32 because it doesn't contain any IP SANs
Jun 09 13:45:35 supportserver /usr/bin/filebeat[27273]: transport.go:125: SSL client failed to connect with: x509: cannot validate certificate for 172.29.225.32 because it doesn't contain any IP SANs
Jun 09 13:45:36 supportserver /usr/bin/filebeat[27273]: transport.go:125: SSL client failed to connect with: x509: cannot validate certificate for 172.29.225.32 because it doesn't contain any IP SANs
Jun 09 13:45:38 supportserver /usr/bin/filebeat[27273]: transport.go:125: SSL client failed to connect with: x509: cannot validate certificate for 172.29.225.32 because it doesn't contain any IP SANs

I search in the vast spaces of the internet of an alternative to generate the certs. What I ended up doing is :
curl -O https://raw.githubusercontent.com/driskell/log-courier/1.x/src/lc-tlscert/lc-tlscert.go
go build lc-tlscert.go

./lc-tlscert
Specify the Common Name for the certificate. The common name
can be anything, but is usually set to the server's primary
DNS name. Even if you plan to connect via IP address you
should specify the DNS name here.

Common name:

The next step is to add any additional DNS names and IP
addresses that clients may use to connect to the server. If
you plan to connect to the server via IP address and not DNS
then you must specify those IP addresses here.
When you are finished, just press enter.

DNS or IP address 1: 172.29.225.32
DNS or IP address 2:

How long should the certificate be valid for? A year (365
days) is usual but requires the certificate to be regenerated
within a year or the certificate will cease working.

Number of days: 365
Common name:
DNS SANs:
    None
IP SANs:
    172.29.225.32

The certificate can now be generated
Press any key to begin generating the self-signed certificate.

Successfully generated certificate
    Certificate: selfsigned.crt
    Private Key: selfsigned.key

Copy and paste the following into your Log Courier
configuration, adjusting paths as necessary:
    "transport": "tls",
    "ssl ca":    "path/to/selfsigned.crt",

Copy and paste the following into your LogStash configuration,
adjusting paths as necessary:
    ssl_certificate => "path/to/selfsigned.crt",
    ssl_key         => "path/to/selfsigned.key",

I copied these certs to the correct path and still am getting the same ERROR. Is there something that I have missed ?
When I try to connect using openssl I get :
# openssl s_client -showcerts -connect 172.29.225.32:9200
CONNECTED(00000003)
139677497968544:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:184:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 247 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
---

Any ideas ?
Answer: If I'm reading your configs right, the path events follow is roughly:
beats
    |-> elasticsearch 172.29.225.32:9200
    |-> logstash 172.29.225.32:5044
           |-> Points unknown.

Your openssl test was done against ElasticSearch, which as far as I can tell hasn't ever been configured for TLS. Unfortunately, the error messages filebeat is producing are not detailed enough to separate problems talking to Logstash from problems talking to Elasticsearch (port 9200). To test, I would remove one or the other from your filebeat config and see how that affects the errors; this is to isolate which component is generating the TLS errors.
I believe filebeat defaults to non-TLS for ElasticSearch  unless you explicitly tell it to use TLS.
The logstash output for filebeat also seems to default to non-TLS, but something in your config is either negotiating for it and failing, or is oddly expecting it when it shouldn't.
Having recently done a round of SAN-debugging, here is a useful tip for getting the SANs off of a certificate:
openssl s_client -connect 172.29.225.32:5044 | openssl x509 -text -noout

That will give you the SANs on the certificate, where straight up s_client generally doesn't.