Load nodes with a fields condition

Question: I have node type called online_users.
The node type has a field called field_website_id.
I would like to get the nodes that has a specific field_website_id.
I have try to make the following action but gets an error:
function get_list_of_users($site_id) {
$query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'users_online')
  ->propertyCondition('field_website_id', $site_id)
  ->execute();
  if (!empty($entities['node'])) {
    $node = node_load(array_shift(array_keys($entities['node'])));
  }
}

The error:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column
  'node.field_website_id' in 'where clause': SELECT node.nid AS
  entity_id, node.vid AS revision_id, node.type AS bundle, :entity_type
  AS entity_type FROM {node} node WHERE (node.type =
  :db_condition_placeholder_0) AND (node.field_website_id IS NULL ) ;
  Array ( [:db_condition_placeholder_0] => users_online [:entity_type]
  => node ) in EntityFieldQuery->execute() (line 1140 of /Applications/MAMP/htdocs/Drupal/includes/entity.inc).

What is the right way to do this?
Answer: To filter by fields you should use fieldCondition() method instead of propertyCondition()
function get_list_of_users($site_id) {
  $query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'users_online')
    ->fieldCondition('field_website_id', $site_id)
    ->execute();
  if (!empty($entities['node'])) {
    $node = node_load(array_shift(array_keys($entities['node'])));
  }
}

Comment: Does this function has cache problems? It seems that it bringing a strange results... Dubble and doesn't bring all of the results it supposed to bring (i had  compared with the db query)

Load nodes with a fields condition


How can I allow extensions to be made for my Android app?

Question: If I'm making an Android app (such as a messaging app), and I want the app to be opened up so that other developers can make extensions (i.e. connections to other messaging sites/protocols, additional features, custom UI), what options do I have available to me to do this?
The answer to this question makes it seem like if the other developer makes their own app, my app is able to pull data from it? Is this the direction I should be heading in?
I know such extensions are possible, because I've seen it with other apps, such as the file manager FX that has "add-ons" called "FX Plus" and "FX Root Access". So, basically, how can I also get add-ons/extensions for my app?
Comment: @Skynet is this an Android API that's available? so that's what I should be using?
Answer: Options I am aware of:
Intent API can provide some functionality (You can give developers ability to send commands and data to your app). For example you can accept some kind of XML and build your UI according to it. It's slow process, but you can create some hacks to speed it up. documentation
Content provider you can give other developers ability to work with data of your app. Databases, photos, videos. You can share your's and you can accept new ones.
documentation
AIDL Service never used it myself but sounds promising. documentation
Comment: Hmm... okay. Not enough info for me to really figure out what to do, but the documentation links will (at least hopefully) get me off in the direction I want to go. So thank you for the answer!


Non-trivial prime gaps

Question: A simple proof that there are prime gaps of size at least $n+1$ for every $n$ can be seen in the first answer to this question. I consider prime gaps of the form $n!, n!+1, \ldots, n!+n$ of length at least $n+1$ to be "trivial".
My question is:

Can it be proven that for every $n,$ there are prime gaps of length at least $n$ starting from
integer $q,$ i.e. $q, q+1, q+2,\ldots, q+n-1,$ such that $q$ is not
of the form $n! m,$ where $m$ is positive integer?

I'm not sure if the second answer to the linked question helps answer my question.
Comment: You overlook a crucial detail : The gap is the difference bewteen two consecutive primes. That all those numbers are composite proves only a lower bound for the particular gap. That does not prove that every even (the only possible odd gap is $1$) gap occurs although it is even conjectured that every even gap occurs infinite many often.
Comment: What we can (easily) show is that there is no limit for the gaps in the prime numbers.
Comment: @Peter thanks. I have amended my question. Is it better now?
Answer: There are no primes with prime gaps being $7$. If you mean the prime gap to be at least $n$, the answer is yes. Consider $n \geq 2$. Take the $n$ smallest distinct primes $p_1, p_2 \dots p_n$. Notice that $q = p_1p_2p_3 \dots p_n + 2$ is not divisible by $n!$ as $3 \nmid q$. However, notice that $\pi (n-1) = k < n$ and thus, we can claim that $q, q+1 \dots q + n - 1$ are all divisible by at least one of $p_1, p_2 \dots p_k$.
Answer: A generalisation of the argument might show that there are non-trivial prime gaps.
Suppose that $a_1, \ldots, a_n$ are pairwise relatively prime, and none of them are prime. Then, by the Chinese remainder theorem the system
$$x \equiv -1 \ (\text{mod} \ a_1) \quad \cdots \quad x \equiv -n \ (\text{mod} \ a_n)$$
has a solution. Notice that this means that the sequence $x + 1,\ldots, x+n$ are all composite, since $a_i | x+i$. It is left to show that this procedure can yield an $x$ which is not of the form $n! m$, that is $\exists k \leq n$ such that $k$ does not divide $x$. To achieve this, just choose $a_1$ to be a number $\leq n$.

Non-trivial prime gaps


Simple Powershell 7.0 Script

Question: I have been practicing Powershell for a few days. I am trying to create a script to pull all
of the files with the extension .ps1 from my desktop and copy them to a folder in my documents.
It is a simple script and only slightly useful but I am doing it as a exercise.
I have been trying to get this to work on and off for the last two days but it
seems to get hung up. I am not sure what I am doing wrong. I will post the few different
ways I have tried to get this to work. All of the scripts I see floating around for
this are much more complex and I am just trying to start small and build from there.
Sample 1:
$dir = "C:\Users\Me\Desktop\*"
$des = "C:\Users\Me\Documents\Scripts\"

Get-ChildItem -Path $dir -File |
    Where-Object -Property Extension -eq ".ps1" |
        Copy-Item -Destination $des

Error:
PS C:\Users\Me> Copy-Item: untitled:Untitled-2:6:9
Line |
   6 |          Copy-Item -Destination $des
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find file 'C:\Users\Me\Documents\Scripts\Update-Help.ps1'.

Sample 2:
$dir = "C:\Users\Me\Desktop\*"
$des = "C:\Users\Me\Documents\Scripts\"

Get-ChildItem -Path $dir -Recurse -Force |
    Where-Object {$_.Extension -eq '.ps1'} | #This seems to return what I need to copy.
        Copy-Item -Destination $des

Error:
PS C:\Users\Me>
    Directory: C:\Users\Me\Desktop

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          11/18/2020 10:40 PM            172 Update-Help.ps1

    Directory: C:\Users\Me\Desktop\Desktop\kon-bootUSB

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          10/30/2018  2:30 PM            351 auto.ps1

PS C:\Users\Me> Copy-Item: untitled:Untitled-2:11:9
Line |
  11 |          Copy-Item -Destination $des
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find file 'C:\Users\Me\Documents\Scripts\Update-Help.ps1'.

Copy-Item: untitled:Untitled-2:11:9
Line |
  11 |          Copy-Item -Destination $des
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find file 'C:\Users\Me\Documents\Scripts\auto.ps1'.

Any thoughts on what I am missing would be very helpful!
I have tried to move things around and resort to the help
pages for the commands.
Comment: Do you have files with colons in the names?
Comment: What is `untitled:Untitled-2:11:9`?
Comment: Does the destination exist?  `C:\Users\Me\Documents\Scripts`
Comment: @js2010, No I do not. They are named with a typical naming convection. Still getting the same error with the below solution. I am not sure if it is the script at this point. I have tried it may ways. I have made sure it is grabbing the files I need by just running the Get-ChildItem and it returns the files I want to copy but is having a hard time copying them for some reason. As far ad I know, my Copy-Item syntax seems correct. The -Path does not have to be included, correct? I just want to copy the output of the previous pipe and use that for the copy statement.
Comment: I have searched for that on my computer and it does not come back with anything. I have noticed it appends that to my .ps1 files though ":6:9"
Comment: I only see that if I grab it with script though.
Comment: Yes, it exists as a folder in my Documents directory. I copied the path from file explorer.
Comment: I can list the items in it with Get-ChildItem. That is true for both paths.
Answer: I'm not certain but the Where-Object could be causing problems, you can eliminate that and use the native -Filter parameter of Get-ChildItem.
Try
Get-ChildItem -Filter *.ps1 -Path $dir | Copy-Item -Destination $des

I would also eliminate the * component of your $dir variable
Comment: This is one of the earlier things I tried, then I added Where-Object and even tried Select-Object and am coming to the same blockade. Thank you for your help!

Simple Powershell 7.0 Script


Why is the ICC near 0 in this longitudinal data?

Question: I'm working with a toy data set to get a basic understanding of longitudinal data analysis and I'm having a difficult time with a conceptual understanding of the intraclass correlation (ICC) as a proportion of variance. As I understand it, one way to interpret the ICC in a random-intercept model is it's the proportion of original outcome variance attributable to the between-cluster mean differences over time. In mathematical terms, if the model is represented as
$$y_{ti} = \beta_{0} + U_{0i} + e_{ti}$$
Where $U_{0i}\sim\mathcal{N}(0, \tau^{2}_{U_{0}})$ and $e_{ti}\sim\mathcal{N}(0, \sigma^{2}_{e})$, then the ICC is defined as
$$\text{ICC} = \frac{Var(U_{0i})}{Var(U_{0i}) + Var(e_{ti})} = \frac{\tau^{2}_{U_{0}}}{\tau^{2}_{U_{0}} + \sigma^{2}_{e}}.$$
Here are a couple of figures from the data. Plot A is a histogram of the responses, with the solid red line representing the grand mean. Plot B is a spaghetti plot of the individual trajectory across $t = 3$ time points. I have color coded a few individuals to show different patterns in the response.

library(tidyverse)
library(ggthemes)
library(ggpubr)
library(nlme)

df <- structure(
  list(
    id = rep(1:89, each = 3),
    wave = rep(1:3, 89),
    piat = c(18, 35, 59, 18, 25, 28, 18, 23, 32, 18, 31, 50, 18, 33, 53, 18, 28,
             31, 17, 28, 28, 17, 29, 41, 28, 26, 26, 16, 20, 21, 18, 28, 42, 18,
             31, 57, 18, 41, 49, 18, 31, 52, 18, 33, 44, 18, 43, 46, 37, 28, 29,
             16, 27, 27, 18, 18, 26, 18, 38, 47, 18, 22, 26, 22, 27, 45, 14, 37,
             37, 18, 40, 35, 29, 31, 43, 17, 23, 21, 19, 36, 57, 4, 27, 28, 18,
             24, 45, 18, 44, 51, 18, 31, 51, 24, 39, 48, 18, 34, 29, 17, 36, 37,
             18, 21, 28, 16, 38, 47, 18, 40, 47, 22, 36, 34, 18, 41, 48, 19, 40,
             46, 18, 28, 36, 18, 28, 47, 20, 36, 45, 18, 28, 41, 16, 34, 43, 25,
             45, 49, 18, 24, 31, 17, 27, 29, 19, 32, 48, 43, 55, 61, 20, 24, 32,
             18, 46, 47, 18, 29, 40, 28, 56, 61, 18, 20, 40, 21, 30, 38, 21, 22,
             47, 18, 22, 21, 16, 21, 32, 29, 37, 48, 21, 32, 47, 19, 20, 26, 18,
             34, 41, 28, 30, 31, 21, 28, 43, 18, 25, 32, 20, 24, 39, 22, 30, 42,
             26, 47, 45, 22, 45, 40, 22, 41, 53, 18, 24, 40, 26, 28, 28, 26, 28,
             30, 18, 24, 26, 31, 41, 62, 17, 19, 28, 42, 29, 30, 28, 42, 50, 23,
             39, 55, 28, 47, 53, 26, 44, 32, 19, 35, 51, 18, 32, 40, 29, 47, 45,
             16, 20, 59, 22, 49, 64, 22, 23, 23, 23, 40, 45)
  ),
  row.names = c(NA, -267L),
  class = c("tbl_df", "tbl", "data.frame")
)

########## Making Figures -----------------------------------
g <- ggplot(df) +
  aes(x = piat) +
  geom_histogram(color = "grey", fill = "grey80") +
  coord_flip() +
  theme_bw()

rand_ids <- c(16, 86, 88)

rand_df <- df %>%
  filter(id %in% rand_ids)

p1 <- g +
  geom_vline(data = rand_df, aes(xintercept = piat, color = factor(id))) +
  ggthemes::scale_color_colorblind() +
  geom_vline(xintercept = mean(df$piat), color = "red", size = 2, alpha = 0.5) +
  scale_y_continuous(limits = c(0, 70))

p2 <- ggplot(df) +
  aes(x = wave, y = piat, group = id) +
  geom_point(alpha = 0.1) +
  geom_line(alpha = 0.1) +
  geom_point(data = rand_df, aes(color = factor(id)), size = 2) +
  geom_line(data = rand_df, aes(color = factor(id)), size = 1) +
  ggthemes::scale_color_colorblind()

ggarrange(p1, p2, labels = c("A", "B"), common.legend = TRUE)

When the random intercept model is run, we are estimating the grand mean over time term ($\beta_{0}$), the cluster-specific deviations from the grand mean (as measured by its variance, $\tau^{2}_{U_{0}}$), and the residual variance ($\sigma^{2}_{e}$). Here the random intercept variance is extremely small, and therefore the ICC estimate is essentially 0, and I'm having a hard time conceptualizing why this is the case.
########## Empty-Means Model -----------------------------------
empty_means <- lme(piat ~ 1, random = ~ 1|id, data = df)
summary(empty_means)

Linear mixed-effects model fit by REML
  Data: df
       AIC      BIC    logLik
  2084.744 2095.495 -1039.372

Random effects:
 Formula: ~1 | id
        (Intercept) Residual
StdDev: 0.001475647 11.91709

Fixed effects:  piat ~ 1
               Value Std.Error  DF  t-value p-value
(Intercept) 31.22472 0.7293139 178 42.81382       0

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max
-2.2845111 -0.8579881 -0.2705962  0.7783177  2.7502760

Number of Observations: 267
Number of Groups: 89

########## ICC by hand -----------------------------------
0.001475647^2/(0.001475647^2 + 11.91709^2)
[1] 1.533291e-08

Here are my questions:

Does the output imply that the estimates for the cluster-level means across time ($\beta_{0} + U_{0i}$) are all essentially at the grand mean for every cluster?
If so, why is this the case, since empirical cluster-level means have a wide range? Is this due to massive shrinkage towards the grand mean?
How would I go about reporting the ICC? Would it be better to add in the effect of time and talk about the variation between clusters once wave is accounted for?
Answer: I was really intrigued by this, so I searched around a bit. There is a technical explanation related to estimation of small variances in mixed models. To answer your first and second questions, the model is reporting near-zero random intercept variance due to a very small, calculated between-ID variance. As explained here nlme will report near-zero values and lme4 will report precisely zero values when the model finds very small proportions of variance in the random intercept.
You demonstrate that you do have non-zero variance among ID-clusters; however, recall that ICC can be defined as variance between clusters AND/OR average correlation within clusters. You only have three observations per ID and there does not seem to be clear differences in overall "levels" between clusters in this data. The deviations among clusters is likely being interpreted as less meaningful/systematic due to the small within-cluster sample as explained here.
I would answer affirmatively to your third question. If I run the following (or without random slopes for wave), the model gives interpretable estimates.
empty_means <- lmer(piat ~ wave + (1 + wave | id) , data = df)
summary(empty_means)

On a side note, I would suggest including a meaningful 0 value for your wave variable. If you use wave = rep(0:2, 89), you can interpret the fixed-model intercept as estimated value at the first wave.
Comment: I think that clears it up. Let me repeat it back to you in my own words to see if I understand correctly. The within-cluster variance is high because of the function of its a) sample size and b) uncorrelated responses. As shown in the spaghetti plot, relatively large initial values are no guarantee of large values at future time points, and therefore the within-cluster data are noisy to a point where the between-cluster differences that might exist gets drowned out. If the within-cluster responses were more correlated (e.g. large time 1 responses associated with large time 2 responses, etc.)
Comment: or there were more time points in general, then the within-cluster variance would likely be smaller and the between-cluster differences could be more easily parsed.
Comment: Yes, I think what you described must be the reason for the low variance. And because it is low enough to impede precise calculation, the linear mixed models have a built-in and necessary procedure of reporting it as near zero (for `nlme`) or precisely zero (for `lme4`). For the "under the hood" discussions, you can refer to those more detailed responses. I didn't mark this as a repeat question, because I think yours had more nuance in some areas. [This](https://rpubs.com/bbolker/6226) and [this](https://rpubs.com/bbolker/4187) from the package owner might work as a footnote reference.


Configurando Rotas AngulasJS com ASP.NET MVC

Question: Estou com problema ao realizar roteamento angularJS com asp.net mvc.
Configurei meu roteamento no app.js e quando clico nos meus links as páginas não estão sendo redirecionadas corretamente, única página que aparece corretamente é a Home.html, mas depois que eu clico nos links as mesmas não funcionam, muda a URL mas fica sempre cm o Home.html.

Minha Index Principal

<!DOCTYPE html>

    <title></title>
    <meta charset="utf-8" />

     Home  |
     Listar Autores  |
     Listar Editoras  |
     Listar Livros

Configurando o app Angular JS

(function () {
var app = angular.module("app", ["ngRoute"]);

app.config(function ($routeProvider) {

    $routeProvider
        .when("/", { controller: "homeController", templateUrl: "/AngularJS/view/Home/Home.html" })
        .when("/Livro", { controller: "livroController", templateUrl: "/AngularJS/view/Livro/Livro.html" })
        .when("/Autor", { controller: "autorController", templateUrl: "/AngularJS/view/Autor/Autor.html" })
        .when("/Editora", { controller: "editoraController", templateUrl: "/AngularJS/view/Editora/Editora.html" })
    .otherwise({ redirectTo: "/" });

});
}());

Controller Home AngularJS

(function () {
    angular.module("app").controller("homeController", function ($scope) {
        $scope.home = " Home ";
    });
}());

Controller Livro Ahgular JS

(function () {
    angular.module("app").controller("livroController", function ($scope) {
        $scope.livro = "Primeiro Livro";
    });
}());

HtmlPage  Home

    Pagina Inicial - {{home}}

Html Page Livro

    Livro Index - {{livro}}

Página inicial aberta corretamente

Ao clicar no Link Livro - Muda a url mas não abre a página Livro.html

Estrutura dos arquivos
Answer: O problema é que a pasta Views por padrão é especial, o Web.config localizado dentro dela modifica o comportamento de requisições para arquivos dentro dela e por isso ele retorna que não encontrou o arquivo.
Se você não vai utilizar de forma alguma os Controllers e Views do ASP.Net MVC você pode excluir o Web.config que está dentro desta pasta e ela vai funcionar como uma pasta qualquer, e neste caso eu diria para remover o _ViewStart.cshtml e pasta Shared também, ou a outra alternativa é usar uma pasta com um nome diferente para os seus templates do AngularJS.
Comment: Leandro, deu certo par carregar minha view home, fiz exatamente o que me pediu, só que só tem um porém, as rotas para as outras páginas não estão funcionando, conseguiria me ajudar ? vou atutalizar meus fontes
Comment: Arrumei a estrutura dos meus arquivos, e também meu app.js
Answer: Consegui resolver este meu problema da seguinte forma!
Ao invés
de :  Home
coloquei:  Home

     Home  |
     Listar Autores  |
     Listar Editoras  |
     Listar Livros


Método gravar não tem ação e vinculo de Dependente x Motorista não é feito

Question: Ao desenvolver uma aplicação simples onde o dependente é vinculado ao seu responsável que é um motorista, quando eu clico para criar o vinculo e gravar dependente nada acontece, apenas a seguinte mensagem é lançada sem interromper a aplicação.
out 28, 2017 3:19:15 PM com.sun.faces.renderkit.RenderKitUtils renderUnhandledMessages
INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=j_idt4:motorista[severity=(ERROR 2), summary=(j_idt4:motorista: 'Pessoa [nome=ROBSON GOMES JUNIOR, cpf=, rg=, nomeMae=, nomePai=, dataNascimento=, rua=, numeroCasa=, bairro=, cidade=, estado=, cep=]' must be a number consisting of one or more digits.), detail=(j_idt4:motorista: 'Pessoa [nome=ROBSON GOMES JUNIOR, cpf=, rg=, nomeMae=, nomePai=, dataNascimento=, rua=, numeroCasa=, bairro=, cidade=, estado=, cep=]' must be a number between -2147483648 and 2147483647 Example: 9346)]

Bean
package modelo;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import modeloDAO.DAO;

@ManagedBean
@ViewScoped
public class DependenteBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Dependente dependente = new Dependente();
    private Integer motoristaId;

    public void setMotoristaId(Integer motoristaId) {
        this.motoristaId = motoristaId;
    }

    public Integer getMotoristaId() {
        return motoristaId;
    }

    public Dependente getDependente() {
        return dependente;
    }

    public List<Motorista> getMotoristas() {
        return new DAO<Motorista>(Motorista.class).listaTodos();
    }

    public void gravarMotorista() {
        Motorista motorista = new DAO<Motorista>(Motorista.class).buscaPorId(this.motoristaId);
        this.dependente.adicionaMotorista(motorista);
    }

    public void gravar() {
        gravarMotorista();
        System.out.println("Gravando Dependente: " + this.dependente.toString());
        new DAO<Dependente>(Dependente.class).adiciona(this.dependente);
        this.dependente = new Dependente();

    }

}

XHTML
<h:body>

Novo Dependente

    <h:form>

        <fieldset>
            <legend>Dados do Dependente</legend>
            <h:panelGrid columns="2">
                <h:outputLabel value="Nome: " for="nome" />
                <h:inputText id="nome" value="#{dependenteBean.dependente.nome}" />

                <h:outputLabel value="Data de Nascimento: " for="dataNascimento" />
                <h:inputText id="dataNascimento" value="#{dependenteBean.dependente.dataNascimento}" />

                <h:outputLabel value="CPF: " for="cpf" />
                <h:inputText id="cpf" value="#{dependenteBean.dependente.cpf}" />

                <h:outputLabel value="RG: " for="rg" />
                <h:inputText id="rg" value="#{dependenteBean.dependente.rg}" />

                <h:outputLabel value="Nome da Mãe: " for="nomeMae" />
                <h:inputText id="nomeMae" value="#{dependenteBean.dependente.nomeMae}" />
                <h:outputLabel value="Nome da Pai: " for="nomePai" />
                <h:inputText id="nomePai" value="#{dependenteBean.dependente.nomePai}" />
                <h:outputLabel value="Nome da Rua: " for="rua" />
                <h:inputText id="rua" value="#{dependenteBean.dependente.rua}" />
                <h:outputLabel value="Numero da Casa ou Apartamento: " for="numeroCasa" />
                <h:inputText id="numeroCasa" value="#{dependenteBean.dependente.numeroCasa}" />
                <h:outputLabel value="Bairro: " for="bairro" />
                <h:inputText id="bairro" value="#{dependenteBean.dependente.bairro}" />
                <h:outputLabel value="Cidade: " for="cidade" />
                <h:inputText id="cidade" value="#{dependenteBean.dependente.cidade}" />
                <h:outputLabel value="Estado: " for="estado" />
                <h:inputText id="estado" value="#{dependenteBean.dependente.estado}" />
                <h:outputLabel value="CEP: " for="cep" />
                <h:inputText id="cep" value="#{dependenteBean.dependente.cep}" />
                <h:commandButton value="Cadastrar" action="#{dependenteBean.gravar}" />
            </h:panelGrid>
        </fieldset>

        <fieldset>
            <legend>Dados do Motorista</legend>
            <h:outputLabel value="Selecione Motorista" for="motorista" />
            <h:selectOneMenu value="#{dependenteBean.motoristaId}" id="motorista">
                <f:selectItems value="#{dependenteBean.motoristas}" var="motorista" itemLabel="#{motorista.nome}" itemValue="#{pessoa.motorista.id}" />
            </h:selectOneMenu>

            <h:commandButton value="Gravar Motorista" action="#{dependenteBean.gravarMotorista}" />
        </fieldset>
    </h:form>
</h:body>

Classe Dependente
package modelo;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "DEPENDENTE")
public class Dependente extends Pessoa {

    @ManyToOne(fetch = FetchType.EAGER) // MUITOS DEPENDENTES PARA 1 MOTORISTA
    private Motorista motorista;

    public Dependente() {
        super();
    }

    @Override
    public String getNome() {
        // TODO Auto-generated method stub
        return super.getNome();
    }

    @Override
    public void setNome(String nome) {
        // TODO Auto-generated method stub
        super.setNome(nome);
    }

    @Override
    public String getCpf() {
        // TODO Auto-generated method stub
        return super.getCpf();
    }

    @Override
    public void setCpf(String cpf) {
        // TODO Auto-generated method stub
        super.setCpf(cpf);
    }

    @Override
    public String getRg() {
        // TODO Auto-generated method stub
        return super.getRg();
    }

    @Override
    public void setRg(String rg) {
        // TODO Auto-generated method stub
        super.setRg(rg);
    }

    @Override
    public String getNomeMae() {
        // TODO Auto-generated method stub
        return super.getNomeMae();
    }

    @Override
    public void setNomeMae(String nomeMae) {
        // TODO Auto-generated method stub
        super.setNomeMae(nomeMae);
    }

    @Override
    public String getNomePai() {
        // TODO Auto-generated method stub
        return super.getNomePai();
    }

    @Override
    public void setNomePai(String nomePai) {
        // TODO Auto-generated method stub
        super.setNomePai(nomePai);
    }

    @Override
    public String getDataNascimento() {
        // TODO Auto-generated method stub
        return super.getDataNascimento();
    }

    @Override
    public void setDataNascimento(String dataNascimento) {
        // TODO Auto-generated method stub
        super.setDataNascimento(dataNascimento);
    }

    @Override
    public String getRua() {
        // TODO Auto-generated method stub
        return super.getRua();
    }

    @Override
    public void setRua(String rua) {
        // TODO Auto-generated method stub
        super.setRua(rua);
    }

    @Override
    public String getNumeroCasa() {
        // TODO Auto-generated method stub
        return super.getNumeroCasa();
    }

    @Override
    public void setNumeroCasa(String numeroCasa) {
        // TODO Auto-generated method stub
        super.setNumeroCasa(numeroCasa);
    }

    @Override
    public String getBairro() {
        // TODO Auto-generated method stub
        return super.getBairro();
    }

    @Override
    public void setBairro(String bairro) {
        // TODO Auto-generated method stub
        super.setBairro(bairro);
    }

    @Override
    public String getCidade() {
        // TODO Auto-generated method stub
        return super.getCidade();
    }

    @Override
    public void setCidade(String cidade) {
        // TODO Auto-generated method stub
        super.setCidade(cidade);
    }

    @Override
    public String getEstado() {
        // TODO Auto-generated method stub
        return super.getEstado();
    }

    @Override
    public void setEstado(String estado) {
        // TODO Auto-generated method stub
        super.setEstado(estado);
    }

    @Override
    public String getCep() {
        // TODO Auto-generated method stub
        return super.getCep();
    }

    @Override
    public void setCep(String cep) {
        // TODO Auto-generated method stub
        super.setCep(cep);
    }

    // ADICIONA MOTORISTA
    public void adicionaMotorista(Motorista motorista) {
        this.motorista = motorista;
    }

    public Motorista getMotorista() {
        return motorista;
    }

    @Override
    public String toString() {
        return "Dependente [motorista=" + motorista + ", getNome()=" + getNome() + ", getCpf()=" + getCpf() + ", getRg()=" + getRg() + ", getNomeMae()=" + getNomeMae() + ", getNomePai()=" + getNomePai() + ", getDataNascimento()=" + getDataNascimento() + ", getRua()=" + getRua() + ", getNumeroCasa()=" + getNumeroCasa() + ", getBairro()=" + getBairro() + ", getCidade()=" + getCidade() + ", getEstado()=" + getEstado() + ", getCep()=" + getCep() + ", getMotorista()=" + getMotorista() + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((motorista == null) ? 0 : motorista.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Dependente other = (Dependente) obj;
        if (motorista == null) {
            if (other.motorista != null)
                return false;
        } else if (!motorista.equals(other.motorista))
            return false;
        return true;
    }

}

Classe Motorista
package modelo;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "MOTORISTA")
public class Motorista extends Pessoa {

    private String cnhMotorista;
    private String validadeCnh;

    @OneToMany(mappedBy = "motorista")
    private List<Dependente> dependentes;

    @ManyToOne
    private Carro carro;

    @OneToMany(mappedBy = "motorista")
    private List<Viagem> viagens;

    public Motorista() {
        super();
    }

    public String getCnhMotorista() {
        return cnhMotorista;
    }

    public void setCnhMotorista(String cnhMotorista) {
        this.cnhMotorista = cnhMotorista;
    }

    public String getValidadeCnh() {
        return validadeCnh;
    }

    public void setValidadeCnh(String validadeCnh) {
        this.validadeCnh = validadeCnh;
    }

    @Override
    public String getNome() {
        // TODO Auto-generated method stub
        return super.getNome();
    }

    @Override
    public void setNome(String nome) {
        // TODO Auto-generated method stub
        super.setNome(nome);
    }

    @Override
    public String getCpf() {
        // TODO Auto-generated method stub
        return super.getCpf();
    }

    @Override
    public void setCpf(String cpf) {
        // TODO Auto-generated method stub
        super.setCpf(cpf);
    }

    @Override
    public String getRg() {
        // TODO Auto-generated method stub
        return super.getRg();
    }

    @Override
    public void setRg(String rg) {
        // TODO Auto-generated method stub
        super.setRg(rg);
    }

    @Override
    public String getNomeMae() {
        // TODO Auto-generated method stub
        return super.getNomeMae();
    }

    @Override
    public void setNomeMae(String nomeMae) {
        // TODO Auto-generated method stub
        super.setNomeMae(nomeMae);
    }

    @Override
    public String getNomePai() {
        // TODO Auto-generated method stub
        return super.getNomePai();
    }

    @Override
    public void setNomePai(String nomePai) {
        // TODO Auto-generated method stub
        super.setNomePai(nomePai);
    }

    @Override
    public String getDataNascimento() {
        // TODO Auto-generated method stub
        return super.getDataNascimento();
    }

    @Override
    public void setDataNascimento(String dataNascimento) {
        // TODO Auto-generated method stub
        super.setDataNascimento(dataNascimento);
    }

    @Override
    public String getRua() {
        // TODO Auto-generated method stub
        return super.getRua();
    }

    @Override
    public void setRua(String rua) {
        // TODO Auto-generated method stub
        super.setRua(rua);
    }

    @Override
    public String getNumeroCasa() {
        // TODO Auto-generated method stub
        return super.getNumeroCasa();
    }

    @Override
    public void setNumeroCasa(String numeroCasa) {
        // TODO Auto-generated method stub
        super.setNumeroCasa(numeroCasa);
    }

    @Override
    public String getBairro() {
        // TODO Auto-generated method stub
        return super.getBairro();
    }

    @Override
    public void setBairro(String bairro) {
        // TODO Auto-generated method stub
        super.setBairro(bairro);
    }

    @Override
    public String getCidade() {
        // TODO Auto-generated method stub
        return super.getCidade();
    }

    @Override
    public void setCidade(String cidade) {
        // TODO Auto-generated method stub
        super.setCidade(cidade);
    }

    @Override
    public String getEstado() {
        // TODO Auto-generated method stub
        return super.getEstado();
    }

    @Override
    public void setEstado(String estado) {
        // TODO Auto-generated method stub
        super.setEstado(estado);
    }

    @Override
    public String getCep() {
        // TODO Auto-generated method stub
        return super.getCep();
    }

    @Override
    public void setCep(String cep) {
        // TODO Auto-generated method stub
        super.setCep(cep);
    }

    public List<Dependente> getDependentes() {
        return dependentes;
    }

    public void setDependentes(List<Dependente> dependentes) {
        this.dependentes = dependentes;
    }

    public Carro getCarro() {
        return carro;
    }

    public void setCarro(Carro carro) {
        this.carro = carro;
    }

    @Override
    public String toString() {
        return "Motorista [getCnhMotorista()=" + getCnhMotorista() + ", getValidadeCnh()=" + getValidadeCnh() + ", getNome()=" + getNome() + ", getCpf()=" + getCpf() + ", getRg()=" + getRg() + ", getNomeMae()=" + getNomeMae() + ", getNomePai()=" + getNomePai() + ", getDataNascimento()=" + getDataNascimento()   + ", getRua()=" + getRua() + ", getNumeroCasa()=" + getNumeroCasa() + ", getBairro()=" + getBairro() + ", getCidade()=" + getCidade() + ", getEstado()=" + getEstado() + ", getCep()=" + getCep()   + ", getDependentes()=" + getDependentes() + ", getCarro()=" + getCarro() + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((carro == null) ? 0 : carro.hashCode());
        result = prime * result + ((cnhMotorista == null) ? 0 : cnhMotorista.hashCode());
        result = prime * result + ((dependentes == null) ? 0 : dependentes.hashCode());
        result = prime * result + ((validadeCnh == null) ? 0 : validadeCnh.hashCode());
        result = prime * result + ((viagens == null) ? 0 : viagens.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Motorista other = (Motorista) obj;
        if (carro == null) {
            if (other.carro != null)
                return false;
        } else if (!carro.equals(other.carro))
            return false;
        if (cnhMotorista == null) {
            if (other.cnhMotorista != null)
                return false;
        } else if (!cnhMotorista.equals(other.cnhMotorista))
            return false;
        if (dependentes == null) {
            if (other.dependentes != null)
                return false;
        } else if (!dependentes.equals(other.dependentes))
            return false;
        if (validadeCnh == null) {
            if (other.validadeCnh != null)
                return false;
        } else if (!validadeCnh.equals(other.validadeCnh))
            return false;
        if (viagens == null) {
            if (other.viagens != null)
                return false;
        } else if (!viagens.equals(other.viagens))
            return false;
        return true;
    }

}

DAO
package modeloDAO;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;

public class DAO<T> {

    private final Class<T> classe;

    public DAO(Class<T> classe) {
        this.classe = classe;
    }

    public void adiciona(T t) {

        // consegue a entity manager
        EntityManager em = new JPAUtil().getEntityManager();

        // abre transacao
        em.getTransaction().begin();

        // persiste o objeto
        em.persist(t);

        // commita a transacao
        em.getTransaction().commit();

        // fecha a entity manager
        em.close();
    }

    public void remove(T t) {
        EntityManager em = new JPAUtil().getEntityManager();
        em.getTransaction().begin();

        em.remove(em.merge(t));

        em.getTransaction().commit();
        em.close();
    }

    public void atualiza(T t) {
        EntityManager em = new JPAUtil().getEntityManager();
        em.getTransaction().begin();

        em.merge(t);

        em.getTransaction().commit();
        em.close();
    }

    public List<T> listaTodos() {
        EntityManager em = new JPAUtil().getEntityManager();
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> lista = em.createQuery(query).getResultList();

        em.close();
        return lista;
    }

    public T buscaPorId(Integer id) {
        EntityManager em = new JPAUtil().getEntityManager();
        T instancia = em.find(classe, id);
        em.close();
        return instancia;
    }

    public int contaTodos() {
        EntityManager em = new JPAUtil().getEntityManager();
        long result = (Long) em.createQuery("select count(n) from livro n").getSingleResult();
        em.close();

        return (int) result;
    }

    public List<T> listaTodosPaginada(int firstResult, int maxResults) {
        EntityManager em = new JPAUtil().getEntityManager();
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> lista = em.createQuery(query).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();

        em.close();
        return lista;
    }

}

Comment: Coloca  o código do DAO.
Comment: Adicionado, vlw!
Answer: Olhando para o HTML do problema relatado, observei que no trecho:
<f:selectItems value="#{dependenteBean.motoristas}" var="motorista" itemLabel="#{motorista.nome}" itemValue="#{pessoa.motorista.id}" />

você faz uma referência a um objeto pessoa que não existe no escopo do código, mas sim motorista é declarado como apelido da variável.
O trecho acima deveria ser:
<f:selectItems value="#{dependenteBean.motoristas}" var="motorista" itemLabel="#{motorista.nome}" itemValue="#{motorista.id}" />

Desta forma, acredito que alterando o código para:
<h:body>

Novo Dependente

<h:form>

    <fieldset>
        <legend>Dados do Dependente</legend>
        <h:panelGrid columns="2">
            <h:outputLabel value="Nome: " for="nome" />
            <h:inputText id="nome" value="#{dependenteBean.dependente.nome}" />

            <h:outputLabel value="Data de Nascimento: " for="dataNascimento" />
            <h:inputText id="dataNascimento" value="#{dependenteBean.dependente.dataNascimento}" />

            <h:outputLabel value="CPF: " for="cpf" />
            <h:inputText id="cpf" value="#{dependenteBean.dependente.cpf}" />

            <h:outputLabel value="RG: " for="rg" />
            <h:inputText id="rg" value="#{dependenteBean.dependente.rg}" />

            <h:outputLabel value="Nome da Mãe: " for="nomeMae" />
            <h:inputText id="nomeMae" value="#{dependenteBean.dependente.nomeMae}" />
            <h:outputLabel value="Nome da Pai: " for="nomePai" />
            <h:inputText id="nomePai" value="#{dependenteBean.dependente.nomePai}" />
            <h:outputLabel value="Nome da Rua: " for="rua" />
            <h:inputText id="rua" value="#{dependenteBean.dependente.rua}" />
            <h:outputLabel value="Numero da Casa ou Apartamento: " for="numeroCasa" />
            <h:inputText id="numeroCasa" value="#{dependenteBean.dependente.numeroCasa}" />
            <h:outputLabel value="Bairro: " for="bairro" />
            <h:inputText id="bairro" value="#{dependenteBean.dependente.bairro}" />
            <h:outputLabel value="Cidade: " for="cidade" />
            <h:inputText id="cidade" value="#{dependenteBean.dependente.cidade}" />
            <h:outputLabel value="Estado: " for="estado" />
            <h:inputText id="estado" value="#{dependenteBean.dependente.estado}" />
            <h:outputLabel value="CEP: " for="cep" />
            <h:inputText id="cep" value="#{dependenteBean.dependente.cep}" />
            <h:commandButton value="Cadastrar" action="#{dependenteBean.gravar}" />
        </h:panelGrid>
    </fieldset>

    <fieldset>
        <legend>Dados do Motorista</legend>
        <h:outputLabel value="Selecione Motorista" for="motorista" />
        <h:selectOneMenu value="#{dependenteBean.motoristaId}" id="motorista">
            <f:selectItems value="#{dependenteBean.motoristas}" var="motorista" itemLabel="#{motorista.nome}" itemValue="#{motorista.id}" />
        </h:selectOneMenu>

        <h:commandButton value="Gravar Motorista" action="#{dependenteBean.gravarMotorista}" />
    </fieldset>
</h:form>
</h:body>

a sua aplicação volte a funcionar.
Comment: Inclua a classe `Motorista` na sua pergunta. Muito estranho esse comportamento. Se você lê uma lista de motoristas e atribui cada item a uma variável motorista, então não deveria dar erro.
Comment: Weslley Tavares, obrigado pela ajuda! Mas não funcionar porque como motorista é uma especialização de pessoa assim como dependente, o código para de funcionar quando eu retiro o pessoa. do código e lança essa exceção.  javax.el.PropertyNotFoundException: /dependente.xhtml @68,66 itemValue="#{motorista.id}": Property 'id' not found on type modelo.Motorista
Comment: Pronto, editei lá.
Comment: O mais estranho Weslley é o sistema congelar e não me dá ação. Porque é o seguinte imagina uma classe dependente sendo instanciada e eu vinculando um motorista ao dependente criado, eu faço isso e depois gravo o dependente com o motorista vinculado. Ele não me dá mensagem de erro nem uma, não me diz se fez o vinculo e nem se gravou no banco. Em resumo, parece que eu estou clicando em button sem action.
Comment: @RobinhoGomesJunior na sua classe `Pessoa` qual parâmetro é a chave?
Comment: Chave primária? é Integer id.
Comment: A sua classe `Pessoa` é abstrata?
Comment: Sim, é abstrata.
Comment: Vamos [continuar esta discussão no chat](http://chat.stackexchange.com/rooms/67921/discussion-between-weslley-tavares-and-robinho-gomes-junior).


Gymnopedie no 1 key signature

Question: I have a handbell arrangement of Gymnopedie with Bb in the signature.  Normally you know that means key of Fmaj.   Is it Fmaj?
Comment: Does this answer your question? [What is this key signature used in Gymnopedie 1 - F major or D major?](https://music.stackexchange.com/questions/125286/what-is-this-key-signature-used-in-gymnopedie-1-f-major-or-d-major)
Comment: I wonder what the money's like arranging Satie for handbells.
Answer: The original is in D major. If your version is pitched a 3rd higher than this, it's in F major.
Or if it's at the original pitch, but with a D minor key signature of one flat, it may be copying a Faber edition which prefers to define the key by the final D minor chord rather than all the preceding D major ones!
Comment: There do seem to exist editions which write the piece at original pitch but with a key signature of one flat: https://music.stackexchange.com/questions/125286/what-is-this-key-signature-used-in-gymnopedie-1-f-major-or-d-major?rq=1
Comment: Well, one edition!   That  Faber edition disagrees with Satie's manuscript.   An error, or an editor who couldn't come to terms with a minor final chord, like a reverse tierce de picardie?  But thanks, I'll modify my answer.
Comment: Indeed! - I'm definitely not claiming it's sensible :) But it's another possibility when someone mentions a version with that key signature - if the original poster gives us an image, then we'll know for sure :)

Gymnopedie no 1 key signature


Can't redefine service defined as constant

Question: Here is my temp.js
angular.module('temp', [])
    .service('tempFactory', function() {
        this.a =10;
    });

and here is my temp.Spec.js
describe('temp', function() {
    var tempFactory;

    beforeEach(function() {
        var mockedTempFactory = {};

        module('temp', function($provide) {
            $provide.value('tempFactory', mockedTempFactory);
        });

        mockedTempFactory.a = 20;

        inject(function(_tempFactory_) {
            tempFactory = _tempFactory_;
        });
    });

it('console.log of a property', function() {
    console.log(tempFactory.a);
});

});
In console I get value of 20.
But if I define tempFactory like this:
angular.module('temp', [])
    .constant('tempFactory', {
        a: 10;
    });

In console I get value of 10.
Why can't I redefine tempFactory which was initially defined as constant, but can redefine tempFactory which was initially defined as service, value or factory?
Answer: Because when you create a service, you provide a constructor function to it
.service('tempFactory', function() {
    this.a = 10;
})

Angular then creates an instance of this function kinda like this:
var tempFactoryInstance = new tempFactory();
return tempFactoryInstance;

and returns back exactly that instance, so you could access the a property right away.
But the constant does not do anything, it just returnes what you've passed to it, so
.constant('tempFactory', function() {
    this.a = 10;
})

will just return a function back to you. If you want to be able to access the property a in this situation, then you have to "instantiate" it:
var instance = new tempFactory();
console.log(instance.a); // 10

See the plunker.
You can read more about the behavior of different provider in the documentation.
Comment: I've corrected my example, now it is object, not function. Anyway I do not understand why I can redefine service, factory and value using value, but can not redefine constant using value method. And I can redefine constant using constant method.
Comment: Ok, sorry, I misunderstood the question. I think the reason is in how these providers are created. If you look at the source code [here](https://github.com/angular/angular.js/blob/071be609275558f177cdb28d8f8891bd1805af1c/src/auto/injector.js#L687), you will see that `service` and `value` use `factory`, when `constant` is handled in a separate way.


Remove a dynamically added iframe

Question: Hi so I have a parent div that in any given time an iframe with an embed youtube video is added. This par works great but I want to remove the iframe once the video is done. I've tried using an EvenListener then tried a bind, both of which did not work.
function play_vid(id, par){
// id: youtube video id      par: id of parent div
var obj = {"video": {
    "value": ''
    }}

$(par).html(obj.video.value);

$('#ytb-player').bind('onStateChange', function(e) {
   if (e.data === 0) {
    $(par).remove('#ytb-player');
   }
});

}
Any help is appreciated!
Comment: You're almost there but you need to use YT API to achieve this with `onStateChange`, look [here](https://stackoverflow.com/questions/26036441/hide-iframe-when-youtube-video-ends)
Comment: Sorry my comment didn't go through ! Thx, it worked
Comment: Add answer to this question mentioning what worked for you and then accept that answer so people coming to seek help for related issue may find it useful.
Answer: Yep! Using the YT API works with the code above!

Remove a dynamically added iframe


O que pode causar a queda de desempenho do EF nesse cenário?

Question: Estive fazendo uma importação de dados para um projeto em ASP.NET MVC 5 com EF 6.1 e SQL Server 2008 onde a fonte de dados da importação era um arquivo txt.
As linhas de registro do arquivo não eram muito grandes mas contiam uma certa quantidade de informações para processar. Como por exemplo, na linha de registro havia os dados de um funcionário, um cargo na empresa, bairro, cidade etc. Tudo se tornando uma tabela em meu modelo.
Logo, linha por linha eu verificava a existência de cada bairro, cidade, cargo e todos os outros dados para obter a identificação dos que já estavam cadastrados ou então gravar os não existentes ainda.
Mais tarde optei por armazenar em cache. Simplesmente carreguei os dados em listas genéricas para a memória e antes de ir no banco de dados eu verificada essas informações na memória.
As outras informações não geravam um número grande de dados, porém o cadastro de funcionários sim. Eram mais de 27 mil registros.
Enfim, minha questão é sobre um problema de desempenho que tive ao fazer a importação utilizando o EF. Após os primeiros mil registros (um pouco mais) o desempenho da importação caia muito, cadastrando um funcionário a cada 1 ou 2 segundos.
Até que percebi que ao cancelar o processo de importação e começar novamente os primeiros mil registros eram importados em um tempo bom, mas depois dos mil registros a coisa voltava a ir lentamente.
Resolvi destruindo o meu contexto a cada mil registros e criando novamente. Dai então a importação teve uma boa performance desde o início até o fim.
Gostaria de saber de vocês que já passaram por algo semelhante, porque isso ocorre? Pode ser alguma configuração do Entity Framework? O meu está sem configurações.
Segue o trecho de código da importação para quem quiser analisar:
private List<Domain.int> Matriculas;
private List<Domain.CentroCusto> CacheCentrosCusto;
private List<Domain.Local> CacheLocais;
private List<Domain.Profissao> CacheProfissoes;
private List<Domain.Cidade> CacheCidades;
private List<Domain.Bairro> CacheBairros;

//Funcionarios
public ActionResult Funcionarios(string filePath)
{
    var fileInfo = new FileInfo(filePath);
    if (!file.Exists)
        throw new Exception("Arquivo não encontrado");

    Matriculas = contexto.Funcionarios
        .Select(x => x.Matricula)
        .ToList();

    CacheCentrosCusto = contexto.CentrosCusto.ToList();
    CacheLocais = contexto.Locais.ToList();
    CacheProfissoes = contexto.Profissoes.ToList();
    CacheCidades = contexto.Cidades.ToList();
    CacheBairros = contexto.Bairros.ToList();

    var file = new System.IO.StreamReader(filePath);
    try
    {
        var count = 0;
        string line = "";
        while ((line = file.ReadLine()) != null)
        {
            string[] campos = line.Split(';');

            int matricula;
            if (int.TryParse(campos[0].Trim(), out matricula))
            {
                if (Matriculas.Contains(matricula))
                    continue;
            }

            var funcionario = new Domain.Funcionario();

            funcionario.Matricula = matricula;
            // obtendo outros dados ...

            funcionario.CentroCustoId = GetCentroCustoId(campos[34].Trim());
            funcionario.LocalId = GetLocalId(campos[35].Trim());
            funcionario.ProfissaoId = GetProfissaoId(campos[36].Trim());

            //Cidade e Bairro
            funcionario.BairroId = null;

            var bai_desc = campos[11].Trim();
            if (!string.IsNullOrEmpty(bai_desc))
            {
                var cid_uf = "";
                var cid_desc = campos[12].Trim();

                // trabalho a string ...

                var cidade = new Domain.Cidade();
                cidade.Nome = cid_desc;
                cidade.Uf = cid_uf;

                var bairro = new Domain.Bairro();
                bairro.Nome = bai_desc;

                funcionario.BairroId = GetBairroId(bairro, cidade);
            }

            try
            {
                contexto.Funcionarios.Add(funcionario);
                contexto.SaveChanges();

                Matriculas.Add(matricula);

                count++;
                if (count == 1000)
                {
                    count = 0;
                    contexto.Dispose();
                    contexto = new DataAccess.Context();
                }
            }
            catch (DbEntityValidationException e) { ... }
            catch (Exception e) { ... }
        }
    }
    finally
    {
        file.Close();
    }

    return RedirectToAction("Index");
}

Meu contexto é criado no construtor da classe Controller e destruido no método Dispose.
Notem que há um contador e a cada mil registros eu destruo e crio novamente o meu contexto.
Um exemplo para os outros métodos presentes:
OBS: Todos os outros métodos contidos no código da importação possuem a mesma estrutura que este.

private int? GetProfissaoId(string descricao)
{
    int? profissao_id = null;
    if (!string.IsNullOrEmpty(descricao))
    {
        var profissaoTemp = CacheProfissoes
            .SingleOrDefault(x => x.Descricao.ToLower().Trim() == descricao.ToLower().Trim());

        if (profissaoTemp == null)
        {
            try
            {
                contexto.Profissoes.Add(profissao);
                contexto.SaveChanges();

                CacheProfissoes.Add(profissao);

                profissao_id = profissao.Id;
            }
            catch (DbEntityValidationException e) { ... }
            catch (Exception e) { ... }

            profissao_id = profissao.Id;
        }
        else
        {
            profissao_id = profissaoTemp.Id;
        }

    }
    return profissao_id;
}

Comment: Você poderia colocar um trecho desse código? como faz o SaveChanges?
Comment: Eu tenho algumas coisas que eu nesse pequeno código mudaria! o duro é testar
Answer: Este problema de performance é bem conhecido. Ao adicionar os objetos um por um, você força o Entity Framework a verificar os estados de todos os objetos anexados ao contexto, o que é péssimo para a performance.
Há algumas maneiras de resolver. Vou apresentá-las e você escolhe qual a melhor para o seu caso:
1. Desligar o AutoDetectChangesEnabled
Simplesmente faça o seguinte:
context.Configuration.AutoDetectChangesEnabled = false;

Isto deve acabar com a verificação dos dados entre todas as entidades anexadas ao contexto.
2. Diminua o batch para 100 registros
Lembre-se de que você está inserindo não apenas funcionários, mas cidades e bairros, o que torna a unidade de trabalho tendo até 3000 registros. Deixe o número de registros por batch mais curto e realize o SaveChanges() antes de fazer o Dispose(), apenas para garantir que nada será perdido:
if (count == 100)
{
    count = 0;
    contexto.SaveChanges();
    contexto.Dispose();
    contexto = new DataAccess.Context();
}

3. Use as Caches em separado; AddRange() a cada batch
Crie listas em separado para suas caches, insira nelas normalmente, e ao término de cada batch insira todos os elementos de uma vez:
context.Funcionarios.AddRange(CacheFuncionarios);
context.Cidades.AddRange(CacheCidades);
...
context.SaveChanges();

Você pode, inclusive, usar todas as alternativas apresentadas ao mesmo tempo, mas recomendo ir implementando uma por uma e verificando a performance. Cada caso pode fazer o código se comportar bem diferente de um para o outro.
Veja mais em http://weblog.west-wind.com/posts/2013/Dec/22/Entity-Framework-and-slow-bulk-INSERTs

UPDATE
É necessária uma atualização desta resposta porque novas alternativas apareceram para resolver o problema de bulk insert usando Entity Framework.
Uma delas é o EntityFramework.BulkInsert (que tem um pacote separado para a versão 5 e outro para a versão 4). Há outras versões para outros bancos de dados, como o Microsoft SQL Server Compact Edition.
A primeira parte resposta ainda é útil para outros tipos de melhora de performance, mas já não tão útil para o caso de bulk insert.


Naming conventions in matrix diagonalization $ D = S^{-1} \cdot A \cdot S $

Question: The process of matrix diagonalzation is often summarized as
$$ D = S^{-1} \cdot A \cdot S $$
I understand that we often choose the first letter of the alphabet for the original matrix $A$ and $D$ for the diagonalized matrix.
But where does the name of the matrix $S$ come from?
Comment: In some texts it's $P$ instead of $S$
Comment: In some texts it's $U$ instead of $S$, In some texts it's $V$ instead of $S$,....
Comment: In my school books it depended on the context: S is mostly used for Symmetric Matrix, U for unitary ones, V for anti symmetric, P for other.
Answer: I would think $S$ here stands for similarity transformation.


WCF nested Callback

Question: The backgound: I am trying to forward the server-side ApplyChangeFailed event that is fired by a Sync Services for ADO 1.0 DBServerSyncProvider to the client. All the code examples for Sync Services conflict resolution do not use WCF, and when the client connects to the server database directly, this problem does not exist. My DBServerSyncProvider is wrapped by a head-less WCF service, however, and I cannot show the user a dialog with the offending data for review.
So, the obvious solution seemed to be to convert the HTTP WCF service that Sync Services generated to TCP, make it a duplex connection, and define a callback handler on the client that receives the SyncConflict object and sets the Action property of the event.
When I did that, I got a runtime error (before the callback was attempted):

System.InvalidOperationException: This operation would deadlock because the
  reply cannot be received until the current Message completes processing. If
  you want to allow out-of-order message processing, specify ConcurrencyMode of
  Reentrant or Multiple on CallbackBehaviorAttribute.

So I did what the message suggested and decorated both the service and the callback behavior with the Multiple attribute. Then the runtime error went away, but the call results in a "deadlock" and never returns. What do I do to get around this? Is it not possible to have a WCF service that calls back the client before the original service call returns?
Edit: I think this could be the explanation of the issue, but I am still not sure what the correct solution should be.
Answer: After updating the ConcurrencyMode have you tried firing the callback in a seperate thread?
This answer to another question has some example code that starts another thread and passes through the callback, you might be able to modify that design for your purpose?
Comment: After thinking this through some more, I realized that the thread behind the link in my post has an excellent reasoning for this issue. The problem is not that the callback has to be made in a separate thread. Since it runs on the server, this is not relevant. The deadlock happens on the client. I will post the answer with my code.
Answer: By starting the sync agent in a separate thread on the client, the callback works just fine:
<code>    private int kickOffSyncInSeparateThread()
    {
        SyncRunner syncRunner = new SyncRunner();
        Thread syncThread = new Thread(
               new ThreadStart(syncRunner.RunSyncInThread));

        try
        {
            syncThread.Start();
        }
        catch (ThreadStateException ex)
        {
            Console.WriteLine(ex);
            return 1;
        }
        catch (ThreadInterruptedException ex)
        {
            Console.WriteLine(ex);
            return 2;
        }
        return 0;
    }
</code>
And this is my SyncRunner:
<code>class SyncRunner
{
    public void RunSyncInThread()
    {
        MysyncAgent = new MySyncAgent();

        syncAgent.addUserIdParameter("56623239-d855-de11-8e97-0016cfe25fa3");
        Microsoft.Synchronization.Data.SyncStatistics syncStats =
              syncAgent.Synchronize();
    }
}
</code>

WCF nested Callback


Example of random variable with a unique moment sequence but mgf DNE in a neighborhood of 0

Question: Do you have an example of a random variable $X$ with a unique moment sequence but whose mgf does not exist in a neighborhood of 0?
In other words, I'm looking for a counterexample to the converse of the statement: if $M_X(t)$ exists in a neighborhood of 0, then the moments of $X$ uniquely determine its distribution.
Comment: Correct -- this would be the only random variable with such moments.
Comment: *Every* random variable has a definite moment sequence.  By "unique moment sequence" do you mean this would be the only random variable with such moments?
Comment: See https://stats.stackexchange.com/questions/84219/whether-distributions-with-the-same-moments-are-identical  for non-unicity, but there the mgf do not exist in an open interval around zero
Answer: You will not find such an example, this is explained at Whether distributions with the same moments are identical. For more information some other posts:

Undefined MGF but all moments finite?

Existence of the moment generating function and variance

Moment Generating Function for Lognormal Random Variable


Get fid from file path

Question: I've to create a custom function to display images because I've to add values from custom fields.
I've added a function to template php, like this:
function mytheme_image($variables) {
}

The problem is that in $variables I haven't the fid of the file, but only the path. I have to load the file to get values from field, how should I do?
I'm looking for functions to get fid from path but I haven't found something..
Answer: @pvorb, you can also use EntityFieldQuery.  See the first comment on https://api.drupal.org/api/drupal/includes%21file.inc/function/file_load_multiple/7 for an example.  Something along the lines of:
$filename = 'myfirstpony.png';
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'file')
  ->propertyCondition('filename', $filename)
  ->execute();
// if you are guaranteed there is exactly one result, then:
$file_object = reset($result['file']);
$fid = $file_object->fid;
// but beware you might get 0 or many results

If you have more information -- such as the path (from which you can generate a URI) -- then change the query condition to something like ->propertyCondition('uri', 'public://path/to/myfirstpony.png').
Comment: Hello and welcome. Please remember: this is not a forum. Answer the question, not other answers or comments.
Comment: @molot, the original question was: "The problem is that in $variables I haven't the fid of the file, but only the path. I have to load the file to get values from field, how should I do?"
Comment: If you are addressing arrubiu's question, don't start your answer with "@pvorb, you can also" - that's misleading. If you are addressing pvorb's post, you shouldn't post it under arrubiu's question at all. Simple as that. You may comment @ someone else to tell him your answer solves their doubts, that's OK, but only answer to the question.
Comment: @molot, why are you being so pedantic?  pvorb asked on dman's answer how to answer arrubiu's original question but using EntityFieldQuery.  I did that.
Comment: @ElephantJim - I believe Molot is just trying to keep the q&a format clean and readable for future visitors. Sure no offence meant
Answer: Assuming you want to change the image field output - you might want to look at the theme_image_formatter function that you can override and add your variables to the $image var, that will be later sent to theme_image or theme_image_style.
Be aware, that theme_image is working with the paths, so it is possible that the function is not called from within the theme_image_formatter.
Comment: I've tried it, but using netbeans the system doesn't enter in the function while shows images.
Comment: so are the images coming through the image field and displayed as such?
Comment: Images are "file" field, with "media selector" as widget, and displayed as "rendered entity"
Answer: Suggestions about formatting are helpful, but to answer the actual question, the answer at https://drupal.org/comment/6493610#comment-6493610 looks good.
// Take a look at: file.inc::file_load_multiple
$files = file_load_multiple(array(), array('uri' => $uri));
$file = reset($files);
// If empty, $file will be false, otherwise will contain the required file

A two-liner instead of one, but should get the result we need.
Comment: `$conditions` is marked deprecated in `file_load_multiple()` so what to do instead?
Answer: If using the file_entity module, you can use file_uri_to_object as it can load an existing file object (if exists) or create a new one.
http://www.drupalcontrib.org/api/drupal/contributions!file_entity!file_entity.file_api.inc/function/file_uri_to_object/7
If not using this module, just copy this function from "Source" at the link above.
This is a core issue and should be fixed in D8 sometime: https://www.drupal.org/node/685818
Answer: This seems to be the way to do it In Drupal 8
$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);

Where $files will be an array of file objects, keyed by the file id.

Get fid from file path


Difference between routing and forwarding table

Question: What is the difference between forwarding and routing tables?
Answer: Routing is the decision over which interface a packet is to be sent. This decision has to be made for locally created packets, too. Routing tables contain network addresses and the associated interface or nexthop. This refers to ip route and ip rule (in a Linux context).
Forwarding refers to packets which reach a system but are not destined for this system. Forwarding is a decision of the system: "I take the packet and try to get it towards its destination." Instead of just dropping it. "forwarding" is a common term in the context of packet filters. Linux' Netfilter has three chains in its base table: INPUT, OUTPUT, and FORWARD. This chain just makes the decision "Shall this packet be forwarded or dropped?" (if forwarding is generally enabled on the system; Linux again: /proc/sys/net/ipv4/ip_forward).
Comment: What of: cat /proc/sys/net/ipv4/ip_forward
vs cat /proc/sys/net/ipv4/conf/eth0/forwarding ?
Comment: for posterity: I'm not sure that this answer is correct. In fact, I think as you've presented it, it's almost flipped...it is the *forwarding table* that has the mapping from IP address to an associated interface or next hop.

For any readers, I recommend looking at techieb0y's answer, which matches my general understanding more closely.
Comment: @AlexK The question is not very precise. If the OP meant "difference between forwarding tables and routing tables" then I may have associated the question with the wrong subject (packet filter). Does not make my answer wrong – in that case I just have not answered this question (but a different one). Maybe the OP knew so little about the subject that he could not recognize that I misunderstood him and thus accepted my answer instead of commenting it.
Answer: On most systems -- those with a single network connection -- they will be the same. On a router, however, they will tend to be different.
The routing table holds all the routes a router learns from its peers and neighbors using a dynamic routing protocol such as OSPF or BGP. Each route gets sorted and evaluated, based on the parameters of the routing protocol in use, and the subset of routing table entries that are 'best' are put into the forwarding table. When the router receives a packet, it looks up the destination address in the forwarding table to decide which interface to sent the packet out via, and what the next-hop address should be. As new routes are learned, interfaces brought up or down, or policy changes made to the ranking scheme, the routing table is reevaluated and the forwarding table updated.
Routing tables are typically stored in RAM; forwarding tables on high-performance gear will be distributed across line cards (in modular systems), and is stored in higher-speed TCAM. There is typically a finite number of routes that can be held in TCAM; when the forwarding table exceeds TCAM capacity, packets may be dropped, or routed in software (which is significantly slower).
Answer: First of all, in my point of view, I think you should first clearly understand the distinction between forwarding and routing, which is often neglected. According to the book Computer Networks in page 240 by Lary L.Peterson, forwarding is a series of actions the router will take once receives a packet, including looking at its destination address, consulting a table, and sending the packet in a direction determined by that table. However, routing here refers to the process by which the forwarding tables are built.
Returning back to the original question, we can draw the conclusion that although we can keep a forwarding table and a routing table in the same structures, there are many reasons to separate them. A forwarding table usually needs to be structured to optimize the process of looking up an address when forwarding a packet, which holds more information such as MAC address. In contrast, the routing table usually focuses on calculating changes in topology, and the entries in routing table are usually more succinct, only including the IP of next hop.
Answer: Routing table contain all path to the different destination ( the same destination can have 2 or more different path)
but
Forwarding table only contains the best path to every destination ( only one path for every destination)
Comment: I don't think this answer adds anything to the answers already here.  I'm not sure it's right, either.


Complex Roots in a Complex Equation

Question:
Problem: The equation $$\frac{x}{x+1} + \frac{x}{x+2} = kx$$ has exactly two complex roots. Find all possible complex values for $k.$

Progress: I was online today, and I saw this problem. Heres what I tried: $$(x+1)x+(x+2)x=kx(x+1)(x+2),$$ and I immediately got stuck :(. I'm quite new at complex numbers. Help? Thanks.
Answer: Hint: A polynomial equation of degree $n$ has exactly $n$ roots (counted with multiplicity). The simplified expression has degree at most $3$. Therefore, it either has a double root or is of degree $2$ with simple roots. Note that $x=0$ is a root.
Comment: Make sure not to include $x=-1$ and $x=-2$ as roots because they cannot happen in the original expression.
Answer: While @lhf has provided the right hint, some careful case work is needed, hence elaborating a bit.
First, one must note the denominators can't be zero, so the domain of $x$ does not contain $\{-1, -2\}$.  Given this, you can multiply by $(x+1)(x+2)$ to get your equation as
$$(x+1)x + (x+2)x = kx(x+1)(x+2)$$
$$\iff kx^3 + (3k-2)x^2+(2k-3)x=0$$
Clearly $x=0$ is a root.  If in all there are exactly two roots, and $a$ is the other root, then we could have the roots as $(0,0,a), (0, a, a), (0,a)$ (of course without considering ordering).
For $x=0$ to be a double root, it is evident $\color{red}{k=\frac32}$ must be true.  So this is one value for $k$, which is indeed a complex number. (You should verify that this value indeed gives the last root in the allowable domain.)
If $x=0$ is not a multiple root, then we have what remains after cancellation of $x$, viz. $kx^2+(3k-2)x+(2k-3)=0$ should have exactly one more root.  This can happen in two ways,:
EITHER
the quadratic $kx^2+(3k-2)x+(2k-3)=0$, has a double root.  Thus it's discriminant is zero, hence
$$(3k-2)^2-4k(2k-3)=0 \iff k^2+4=0$$
which gives $\color{red}{k=\pm2i}$, which are the only other possible values.  (Once again you should verify this leads to roots for the quadratic which are allowable in the domain.)
OR
we have essentially only one root, i.e. $\color{red}{k=0}$ so that what remains after cancellation is really $-2x-3=0 \implies x =-\frac32$.
Collecting all cases, the values possible are $\color{red}{k \in \{\frac32,2i, -2i, 0\}}$
Comment: Thanks for the reply. It took a whole day of thinking, but I actually solved it! You're answers are correct, by the way.
Comment: A very thorough, careful answer but apparently the first time through I did not **read** it carefully enough. All the solutions found are in agreement with the problem statement.
Answer: Good!
Yes, multiplying both sides by $(x+ 1)(x+ 2)$ "reduces" the equation to $(x+ 1)x+ (x+ 2)x= kx(x+ 1)(x+ 2)$.  Now, I would do the indicated multiplications to get $x^2+ x+ x^2+ 2x= kX*(x^2+ 3x+ 2)= kx^3+ 3kx^2+ 2kx$ which can be written as $kx^3+ (3k- 1)x^2+ (2k- 1)x= 0$.  The left side can be factored as $x(kx^2+ (3k- 1)x+ (2k- 1))= 0$.  One root is $x= 0$ and, if $x$ is not $0$, $kx^2+ (3k- 1)x+ (2k-1)= 0$ can be solved by the "quadratic formula".
Comment: You don't need to solve the equation to count the roots.
Comment: @lhf Please explain?

Complex Roots in a Complex Equation


Access the last entrys in the database with a java program

Question: I am currently writing a Java program.
Brief description of the component:
I have an "Entries" table.
This table has the following columns:
Date (which is entered automatically when the user makes a double entry)
Input (This is the double number from the user)
With 5 entries, for example, the program should now access the last 2 entries made by the user and reflect them in the program.
For example, the table looks like this:

Date --------- Entry
21.01.2022 --   500

01.03.2022   -- 551

04.05.2022    -- 629

30.06.2022   -- 701

15.07.2022  --  781

Then the program should give me the 701 and the 781.
What is the most sensible way to do this?
It makes no sense to use the following "SQL statement": Select where date 06/30/2022 because it is no longer useful when the user makes a new entry.
Please help!!
Comment: Does this answer your question? [SQL Server SELECT LAST N Rows](https://stackoverflow.com/questions/4193705/sql-server-select-last-n-rows)
Comment: @sorifiend, MS SQL Server (which is discussed in link you attached) is **not** Oracle (which is database the OP is using) so ... I wouldn't say that it answers the question. "SQL" tag is related to the language, not database vendor.
Comment: No this question is not helping. Thanks tho
Answer: You can use the following SQL statement to select the last two rows:
select * from Entries order by date desc limit 2;

Comment: This gives me the error: "ORA-00936: Expression missing".
I assume "Where" is missing. A select must always have SELECT, FROM, WHERE. In your example we only have Select and From ...?
Comment: Oh? Since when is WHERE obligatory? `select * from dual` works perfectly fine, @singhKirat. Problem with *this* code is in `limit` clause - Oracle doesn't support it. Which database version do you use?
Comment: I am using Oracle SQL Developer  Version 21.4.2.018 Build 018.1706.
Answer: select Entry
  from your_table
 order by date desc -- show most recent entries above in the results
 fetch first 2 rows only; -- show first 2 records only

You might be running on an old DB (version less than 12) so "fetch" might not be introduced.
Try this then
select *
  from (select Entry
          from your_table
         order by date desc)
 where rownum <= 2;

Answering to your question how to get a penultimate value
select *
  from (select Entry, row_number() over(order by date_col desc) rn
          from your_table)
 where rn = 2;

the "rn = 2" condition will get you not the last date but a date before the last. Setting it to 1 will get you the row with most recent date
Comment: This also gives me the error: "ORA-00936: Expression missing". I assume "Where" is missing. A select must always have SELECT, FROM, WHERE. In your example we only have Select and From ...?
Comment: @singhKirat Nope, WHERE clause is not required. It has to have SELECT (to know what to select) and FROM (to know where to select from)

What db version are you on? Another reason might be the column name. "date" is a reserved word and it would be better to rename it to, say, "date_col"
Comment: I am using Oracle SQL Developer  Version 21.4.2.018 Build 018.1706. Nope "date" was just an example, i acctually am using another name.
Comment: @singhKirat SQL Developer is just a tool for connecting to the DB and performing queries. Please perform "select version from v$instance;" in order to get database version
Comment: Could you please repeat the statement maybe there is a typo. It says "ORA-00942: table or view does not exist"
Comment: I have tried on my machine and it is correct. Such an error means you have no access to that object. Check updated answer please
Comment: SELECT * FROM v$version; works for me. It says: Oracle Database 12c Enterprise Edition Release 126.96.36.199.0 - 64bit Production
Comment: Your updatet answer also says: ORA-00936:  "missing expression".... I don't get it :(
Comment: @singhKirat Hmm, then I don't get it.. It works fine on my 12. Please post the real query you're trying to  execute
Comment: I've tried all the different answers here and that i found on google, but I'm getting a variety of error messages:

select * from TABLE_NAME order by date desc fetch next 2 rows only;
ORA-00936: "missing expression"

select * from TABLE_NAME order by date desc limit 2;
same error

select COLUMN_NAME from TABLE_NAME order by date desc fetch first 2 rows only;
Same Error

SELECT TOP 2 FROM TABLE_NAME ORDER BY Id DESC;
ORA-00923: "FROM keyword not found where expected"
Comment: @singhKirat What are the real column names you're trying to select and sort by?
Comment: So i am not trying to sort anything. I just want the User to make an entry. Then the program is supposed to look at the last to entries and compare them with the new entry. In my table "counter" i have 2 columns: "counter_date" and "counter_entry".
Comment: I kinda figured it out: SELECT * FROM (
SELECT * FROM table_name ORDER BY date DESC)
WHERE ROWNUM <= 2. This gives me the last two entries. But still i would like to have them seperated. When I type 1 instead of 2, i get the last entry but how can i get the penultimate entry
Comment: @singhKirat please check updated answer
Comment: Ok well everything works!! Thank you very very much @ekochergin
Comment: @singhKirat, no probs. Glad it helped. Please mark my answer as answer to your question. Thanks
Comment: Hey @ekochergin, i have an problem. I want to implement the final statement we had, in java and print it (console). When I try it gives me just a number:

So my Java code is this:
ConnectTodb daba = new ConnectTodb("password");
daba.connect();
System.out.println(db.getStatement().executeUpdate("SELECT * FROM (select entry, row_number() over(order by entrie_date desc) rn from entry) where rn = 1"));

When I do this, it prints the nuber 0...

When I do the same command (SELECT * FROM ... blablabla ....1) in my oracle sql Developer, it gives me the right number...
Comment: Hi @singhKirat It seems to be a java related issue. I can't help here as I have no java experience. It would be better to post another question


Why is the transpose related to the dual space?

Question: A matrix with $m$ rows and $n$ columns in the real numbers is a map from $M : \mathbb{R}^n \to \mathbb{R}^m$; the transpose of this matrix is then a map $M^T: \mathbb{R}^m \to \mathbb{R}^n$.
However, it seems like the transpose is related to the dual space, i.e., here $M^T: \mathbb{R}^{m*} \to \mathbb{R}^{n*}.$ Intuitively, why is it natural to associate the transpose with the dual space? It doesn't seem to follow with our intuition when dealing with matrices.
Comment: What do you mean by "it doesn't follow with our intuition"?
Comment: The transpose of a matrix is best associated with the concept of the adjoint, from functional analysis. Given a linear map $T : X \to Y$, where $X$ and $Y$ are normed linear spaces (or even topological vector spaces), the adjoint $T^*$ is a linear map from $Y^*$ to $X^*$, defined by $T(f) = f \circ T$, where $\circ$ is regular function composition. Once you filter that down to the matrix level, interpreting the elements of $\Bbb{R}^n$ as column vectors and the elements of the dual as row vectors, with matrix multiplication being their pairing, this just turns into the transpose of the matrix.
Comment: @BenGrossmann instead of mapping from the codomain to the domain, it maps from the dual of the codomain to the dual of the domain
Comment: Here's an [old answer of mine](https://math.stackexchange.com/questions/3306408/row-and-column-space-connection-in-adjoint-dot-product/3306463#3306463) that isn't exactly an answer to this question, but it might be of some use.
Comment: @TheoBendit could you elaborate a little more on how $T^*$, or in this case, $M^*$ would end up being the same as the transpose of the matrix? Right now, I have that $M^*(f) = fM$ where $fM$ denotes matrix multiplication. Which means, given a row vector $f$ of dimension $m$,  then I multiply it by the matrix $M$. Working with a small example (e.g., $m = 2$ and $n = 3$ and the elements $ 1, 2, 3, 4, 5, 6$ I'm struggling to turn this into a transpose.
Comment: @TheoBendit You make a strong argument, I just hate the conclusion :). I'd argue that the incentive is more about "convenience" than "aesthetics", but that doesn't make me feel any better about it
Comment: I understand now, thanks for clarifying
Comment: @a6623 Yes, I agree with you so far. I was about to explain further, but my explanation was a circular. Yes, the adjoint, from a functional analysis perspective, even with these conventions, is just right-multiplication by $M$. The transpose comes out when we turn this back into a map from $\Bbb{R}^m$ to $\Bbb{R}^n$ (rather than their duals). You need some way of taking row vectors and turning them into column vectors. The conventional way is with the transpose, but there's nothing in the explanation so far that mandates this method. So, I think the answer is, "it's convention".
Comment: This convention appears in Ben's (excellent) answer in the form of taking "standard" bases for $\Bbb{R}^m$ and $\Bbb{R}^n$ and their duals. But, the only solid reason to use these bases are aesthetic, which is once again the reason we preference the transpose.
Comment: @TheoBendit I appreciate the complement! I wouldn't say that aesthetics are the the *only* solid reason (though that is the primary reason I lean on for the purposes of my answer). Really, the standard dual basis is the [dual basis](https://en.wikipedia.org/wiki/Dual_basis) to the standard basis. The aesthetic niceness of the standard basis is a direct consequence of the definition of multiplication and the definition of a matrix relative to a pair of bases.
Comment: @BenGrossmann Yeah, I think I will stick by my admittedly bold statement. Even the idea of a dual basis is aesthetic; what would be wrong with a basis whose dot products came out to be $2\delta_{ij}$ instead? Or indeed any fixed invertible matrix? Don't get me wrong, the aesthetics are very persuasive. Everything else certainly *feels* artificial, in the same way that expressing ordered $n$-tuples as coordinates in terms of non-standard bases feels artificial, just due to the way we notate said $n$-tuples. But, we could step around this if we wanted.
Comment: [Another related post](https://math.stackexchange.com/a/1138700/81360)
Answer: Here's one perspective.
Elements of $\Bbb R^n$ are typically considered to be "column vectors". That is, elements of $\Bbb R^n$ are represented by or are thought of as $n \times 1$ matrices. That said, we can treat the elements of any vector space as column vectors if we represent vectors with their coordinate vectors relative to some basis: given a basis $\mathcal B = \{v_1,\dots,v_n\}$ of a vector space $V$, $(a_1,\dots,a_n)^T$ is the coordinate vector corresponding to the vector $v = a_1 v_1 + \cdots + a_n v_n$. For a linear map $\phi:V \to W$ and basis $\mathcal B_1, \mathcal B_2$ of $V,W$ respectively, the matrix representation of $\phi$ relative to the bases $\mathcal B_1$ and $\mathcal B_2$ is the matrix $[\phi]$ such that if $x$ is the coordinate vector of $v$, $[\phi]x$ is the coordinate vector of $\phi(v)$. Notationally, we can write
$$
[\phi]^{\mathcal B_1}_{\mathcal B_2} [v]_{\mathcal B_1} = [\phi(v)]_{\mathcal B_2}.
$$
When an $m \times n$ matrix $M$ is treated as a linear map, $M$ is the matrix relative to the standard bases $\mathcal B_1, \mathcal B_2$ of $\Bbb R^n,\Bbb R^m$ (i.e. $\mathcal B_1 = \{(1,0,\dots,0)^T,\dots,(0,\dots,0,1)^T\}$).
On the other hand, elements of $(\Bbb R^n)^*$ can naturally be thought of as row-vectors. Indeed, the matrix of a linear map from $\Bbb R^n$ to $\Bbb R$ is a $1 \times n$ matrix, which is to say a "row vector". As you noted, any map $M:\Bbb R^n \to \Bbb R^m$ induces a "dual map" (which I will call $M^*$) from $(\Bbb R^m)^*$ to $(\Bbb R^n)^*$. This map is defined by
$$
M^*(r) = rM, \qquad r \in \Bbb R^{1 \times m}.
$$
Now, what happens when we try to treat our row vectors as column vectors? That is, what is the matrix representation of $M^*$?
Note that $(\Bbb R^m)^*$ and $(\Bbb R^n)^*$ are $m$ and $n$ dimensional spaces respectively, so this matrix needs to be an $n \times m$ matrix, which as you note is a map from $\Bbb R^m$ to $\Bbb R^n$. To find a matrix representation, we first need a basis. The "natural" choice of basis for the vector space of row vectors is the "standard dual basis", where we take
$$
\mathcal B_1 = \{(1,0,\dots,0),\dots,(0,\dots,0,1)\} \subset \Bbb R^{1 \times m}
$$
and $\mathcal B_2$ similarly. The nice thing about this choice of basis is that for any row-vector $r \in \Bbb R^m$, the coordinate vector of $r$ is simply the column-vector $[r]_{\mathcal B_1} = r^T$.
As it turns out, the matrix $[M^*]^{\mathcal B_1}_{\mathcal B_2}$ relative to this basis is the transpose, $M^T$! That's why we think of the transpose as a representation of the dual map.

Here is a proof confirming that this is the case. Let $\{e_1^{(m)},\dots,e_m^{(m)}\}$ denote the canonical basis of $\Bbb R^n$, so that $\{e_1^{(m)T},\dots,e_m^{(m)T}\}$ is the canonical dual basis. To get the $j$th column of a matrix representation of a map, we can look at what that map does to the $j$th element of the basis. In this case, we have
$$
M^*(e_j^{(m)T}) = e_j^{(m)T}M = \pmatrix{m_{j1} & \cdots & m_{jn}} = m_{j1} e_1^{(n)T} + \cdots + m_{jn} e_n^{(n)T}.
$$
So, the matrix representation $[M^*]^{\mathcal B_1}_{\mathcal B_2}$ should take the coordinate vector corresponding to $e_j^{(m)T}$ (which is simply $e_j^{(m)}$) to the vector $y = \pmatrix{m_{j1} & \cdots & m_{jn}}^T$. This amounts to saying that the $j$th column of $[M^*]^{\mathcal B_1}_{\mathcal B_2}$ is equal to $y$, which is the $j$th row of $M$.
In other words, $[M^*]^{\mathcal B_1}_{\mathcal B_2}$ is the matrix whose columns are the rows of $M$, which is to say that $[M^*]^{\mathcal B_1}_{\mathcal B_2} = M^T$.


ValueError: Can't handle mix of multilabel-indicator and binary Issue with GridSearchCV and KerasClassifier

Question: I have this CNN code for the MNIST data that divides the dataset into training set and test set for only 2's and 7's. On running it the code it gives about 98% Accuracy on the test set.
So, to increase the Accuracy I tried using KerasClassifier from keras.wrappers.scikit_learn. Using the Classifier with GridSearchCV I was thinking to find the optimal parameters but on running the code 1st Iteration goes all fine but throws an error from the next Iteration.
Here is the code:
# This is the normal CNN model without GridSearch
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
batch_size = 128
num_classes = 2
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

#Only look at 3s and 8s
train_picks = np.logical_or(y_train==2,y_train==7)
test_picks = np.logical_or(y_test==2,y_test==7)

x_train = x_train[train_picks]
x_test = x_test[test_picks]
y_train = np.array(y_train[train_picks]==7,dtype=int)
y_test = np.array(y_test[test_picks]==7,dtype=int)

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(4, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(8, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(16, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# Improving the accuracy using GridSearch
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
def build_model(optimizer):
    print(optimizer,batch_size,epochs)
    model = Sequential()
    model.add(Conv2D(4, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
    model.add(Conv2D(8, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(16, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=optimizer,
                  metrics=['accuracy'])
    return model
model = KerasClassifier(build_fn = build_model)
parameters = {'batch_size': [128, 256],
              'epochs': [10, 20],
              'optimizer': ['rmsprop']}
grid_search = GridSearchCV(estimator = model,
                           param_grid = parameters,
                           scoring = 'accuracy',
                           cv = 10)
grid_search = grid_search.fit(x_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_

This is the Output of the code:
rmsprop 128 12
Epoch 1/10
11000/11000 [==============================] - 3s - loss: 0.1654 - acc: 0.9476
Epoch 2/10
11000/11000 [==============================] - 3s - loss: 0.0699 - acc: 0.9786
Epoch 3/10
11000/11000 [==============================] - 2s - loss: 0.0557 - acc: 0.9839
Epoch 4/10
11000/11000 [==============================] - 2s - loss: 0.0510 - acc: 0.9839
Epoch 5/10
11000/11000 [==============================] - 2s - loss: 0.0471 - acc: 0.9853
Epoch 6/10
11000/11000 [==============================] - 2s - loss: 0.0417 - acc: 0.9875
Epoch 7/10
11000/11000 [==============================] - 2s - loss: 0.0399 - acc: 0.9870
Epoch 8/10
11000/11000 [==============================] - 2s - loss: 0.0365 - acc: 0.9885
Epoch 9/10
11000/11000 [==============================] - 2s - loss: 0.0342 - acc: 0.9899
Epoch 10/10
11000/11000 [==============================] - 2s - loss: 0.0321 - acc: 0.9903
 768/1223 [=================>............] - ETA: 0sTraceback (most recent call last):

  File "<ipython-input-4-975b20661114>", line 30, in <module>
    grid_search = grid_search.fit(x_train, y_train)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_search.py", line 945, in fit
    return self._fit(X, y, groups, ParameterGrid(self.param_grid))

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_search.py", line 564, in _fit
    for parameters in parameter_iterable

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 608, in dispatch_one_batch
    self._dispatch(tasks)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 571, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 109, in apply_async
    result = ImmediateResult(func)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 326, in __init__
    self.results = batch()

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 131, in <listcomp>
    return [func(*args, **kwargs) for func, args, kwargs in self.items]

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py", line 260, in _fit_and_score
    test_score = _score(estimator, X_test, y_test, scorer)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py", line 288, in _score
    score = scorer(estimator, X_test, y_test)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/metrics/scorer.py", line 98, in __call__
    **self._kwargs)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 172, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)

  File "/home/thakkar_/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 82, in _check_targets
    "".format(type_true, type_pred))

ValueError: Can't handle mix of multilabel-indicator and binary

Please help!
Answer: The error seem to be in the way you parsing the dictionary parameters..
An example from here:
import numpy
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import SGD
# Function to create model, required for KerasClassifier
def create_model(learn_rate=0.01, momentum=0):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    optimizer = SGD(lr=learn_rate, momentum=momentum)
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=10, verbose=0)
# define the grid search parameters
learn_rate = [0.001, 0.01, 0.1, 0.2, 0.3]
momentum = [0.0, 0.2, 0.4, 0.6, 0.8, 0.9]
param_grid = dict(learn_rate=learn_rate, momentum=momentum)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

GridseachCV basically takes elements from the dictionary that matches with its input parameter, and train it.  You are parsing the complete dictionary, but batch_size, and epochs aren't parameter within the function...
# Improving the accuracy using GridSearch
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
def build_model(optimizer = 'adam'):
    model = Sequential()
    model.add(Conv2D(4, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
    model.add(Conv2D(8, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(16, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=optimizer,
                  metrics=['accuracy'])
    return model
model = KerasClassifier(build_fn = build_model)
parameters = {'batch_size': [128, 256],
              'epochs': [10, 20],
              'optimizer': ['rmsprop']}
grid_search = GridSearchCV(estimator = model,
                           param_grid = parameters,
                           scoring = 'accuracy',
                           cv = 10)
grid_search = grid_search.fit(x_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_

Maybe something like this would work.. have not tested it.
Comment: No @J.Down the batch_size and epochs were there to print. Their presence matters only in the param_grid. The problem is somewhere with the y_test and y_train.
Comment: @DhavalThakkar Yes. thats right. Can you please check the format of your `y_train` that you pass in GridSearchCV and the output of `KerasClassifier.predict(X_train)`?