Comparação de dados em Arquivos [C]

Question: Eu desejo fazer um algoritmo que pegue um nome em um arquivo X e veja se este nome existe em outro arquivo Y, caso não exista ele escreve esse nome em um terceiro arquivo Z. (Basicamente um algoritmo que aponta os faltantes em uma lista de presença).
Tentei fazer isso no algoritmo abaixo mas não funcionou, alguém poderia apontar erros na estrutura lógica cujo o impedem de funcionar?
#include <stdio.h>
#include <string.h>

typedef struct tipo_nome{
    char nomePessoa[50];
} nome;

void main(){
    int i, k=0;
    nome nome1, nome2;
    FILE *file1, *file2, *file3;
    file1 = fopen("listaCompleta.txt", "r");
    file2 = fopen("listaPresenca.txt", "r");
    file3 = fopen("listaFaltantes.txt", "a+");

    do{
        fread(&nome1, sizeof(nome), 1, file1);
        do{
            fread(&nome2, sizeof(nome), 1, file2);
            if(strcmp(nome1.nomePessoa, nome2.nomePessoa)==0){
                k=1;
            }
        }while(!feof(file2) && k==0);

        if(k==0){
            fwrite(&nome1, sizeof(nome), 1, file3);
        }
        k=0;
    }while(!feof(file1));

}

Comment: Precisa mesmo ser em [tag:C]? Um shell script resolveria sua vida muito mais facilmente
Comment: Sim, infelizmente tem de ser em C
Comment: Consigo escrever aqui um algoritmo com eficiência muito baixa, mas que atende perfeitamente seu caso. Para otimizar, já estou colocando alternativas na resposta como ordenar os conjuntos de dados ou usar uma estrutura de busca eficiente, como um hash ou árvore, mas não me delongo demais no assunto pois o seu foco aqui não é velocidade.
Comment: Você também pediu um algoritmo, então estou tomando a liberdade de por a ideia, sem muito foco no C propriamente dito
Answer: Você deseja criar o seguinte conjunto C:

Para tal, precisa responder a duas perguntas:

Como identificar e pertencente a A?
Como identificar e não pertencente a B?

Como no nosso caso A é um arquivo, então e pertence a A caso ele seja o resultado da leitura do arquivo. Então com isso conseguimos responder à primeira pergunta.
Agora, como saber se e não pertence a B? A operação de não pertinência implica no seguinte:

Se B for um conjunto comum, isso significa que vou precisar sempre comparar com todos os seus elementos para me certificar de que realmente e não pertence a B. Mas não precisamos nos ater a conjuntos comuns, posso ter conjuntos ordenados, mapas de hash ou árvore com os nomes, todas essas alternativas permitem que sejam feitas otimizações na quantidade de comparações feitas. Uma estrutura de dados de busca eficiente reduzirá o número de operações realizadas.
Para o escopo desta resposta, não estou otimizando a quantidade de comparações. Também estou levando em conta que o arquivo que contém o conjunto B seja relativamente pequeno, com alguns megabytes no máximo.
Como o conjunto B é sempre consultado como um todo, e o conjunto A só tem de importância a geração do próximo elemento, na inicialização do meu algoritmo vou pré-carregar totalmente B. A inicialização do conjunto A, neste caso, vai ser simplesmente abrir o arquivo.
A ideia geral é mais ou menos a seguinte:
inicializa conjunto A
inicializa conjunto B
faça:
    pegue _e_ o próximo elemento de A
    se _e_ não pertence a B:
        adiciona _e_ em C
enquanto não chegou ao fim do conjunto A

A inicialização de A seria somente a abertura do arquivo. Se fôssemos tentar otimizar as comparações entre A e B, poderíamos usar alguma estrutura de dados em A como um vetor ordenado.
inicialização conjunto A:
    abre arquivo "listaCompleta"

A inicialização do conjunto B aqui é sua leitura completa. Como não sabemos sua dimensão total a priori, podemos usar uma lista encadeada, cujo nó contém uma estrutura nome e um ponteiro para o próximo elemento da lista ligada:
inicialização conjunto B:
    nodo_lista *conjuntoB = NULL
    abre arquivo "listaPresenca"
    faça:
        nodo_lista *novoElemento = malloc(sizeof(nodo_lista))
        novoElemento->next = conjuntoB
        lê do arquivo "listaPresenca" no endereço &(novoElemento->valor)
        se a leitura deu certo:
            conjuntoB = novoElemento
    enquanto não der fim do arquivo "listaPresenca"
    fecha arquivo "listaPresenca"

Pegar o próximo elemento é só dar um fread do jeito que você fez:
pegue _e_ o próximo elemento de A:
    nome _e_
    lê do arquivo "listaCompleta" no endereço &_e_

A pertinência de e em B está sendo tratada observando todo o conjunto B, então precisamos fazer uma iteração completa:
_e_ pertence a B?
    nodo_lista *elementoB = conjuntoB
    enquanto elementoB != NULL:
        se elementoB->valor é igual a _e_:
            retorna "_e_ pertence a B"
        elementoB = elementoB->next
    retorna "_e_ não pertence a B"

A comparação entre dois elementos do tipo nome é comparando o campo nomePessoa dos dois objetos usando strcmp:
_a_ é igual a _b_?
    retorna strcmp(_a_.nomePessoa, _b_.nomePessoa) == 0

Adicionar elemento e ao conjunto C pode ser colocar o elemento novo em uma lista ou então escrever direto no arquivo com fwrite. Se for usada a alternativa de se por em uma lista, depois de se completar a análise de todos os itens de A, é necessário escrever esses itens no arquivo.
Diferenças entre as nossas abordagens
Basicamente, estou preenchendo o conjunto B na memória de trabalho, enquanto você tenta manter o uso de memória externa (como HD). O problema na sua abordagem é que, antes de analisar novamente um item do conjunto A, seria necessário reposicionar a leitura do arquivo que representa o conjunto B para o começo novamente. Um simples fseek logo depois da leitura do primeiro arquivo, forçando o segundo arquivo a voltar para o começo, faria as vezes de retornar para o começo da iteração do conjunto B.
Como sua alternativa implica em uma quantidade quadrática de leituras à memória externa, eu não achei que fosse uma solução prática. Assim, usando uma quantidade linear de leituras à memória externa, preencho logo todo o conjunto B.
Comment: Muito obrigado pela explicação Jefferson, consegui analisar o problema melhor depois dela e montar um algoritmo um pouco diferente que resolveu meu problema
Answer: Segue uma possível solução para o seu problema:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define LINHA_MAX_TAM  (100)
#define NOME_MAX_TAM   (50)

typedef struct pessoa_s
{
    char nome[ NOME_MAX_TAM + 1 ];
} pessoa_t;

int carregar_pessoas( const char * arq, pessoa_t ** pp, int * qtd )
{
    FILE * pf = NULL;
    char linha[ LINHA_MAX_TAM + 1 ] = {0};
    pessoa_t * p = NULL;
    int n = 0;

    pf = fopen( arq, "r" );

    if(!pf)
        return -1;

    while( fgets( linha, LINHA_MAX_TAM, pf ) )
    {
        linha[ strcspn(linha, "\n") ] = 0;
        n++;
        p = realloc( p, n * sizeof(pessoa_t) );
        strncpy( p[n-1].nome, linha, NOME_MAX_TAM );
    }

    fclose(pf);

    *qtd = n;
    *pp = p;

    return 0;
}

int gravar_pessoas( const char * arq, pessoa_t * p, int qtd )
{
    FILE * pf = NULL;
    int i = 0;

    pf = fopen( arq, "w" );

    if(!pf)
        return -1;

    for( i = 0; i < qtd; i++ )
        fprintf( pf, "%s\n", p[i].nome );

    fclose( pf );

    return 0;
}

int processar_ausencias( pessoa_t * pc, int qtdc, pessoa_t * pp, int qtdp, pessoa_t ** pa, int * qtda )
{
    int i = 0;
    int j = 0;
    int presente = 0;
    pessoa_t * p = NULL;
    int n = 0;

    for( i = 0; i < qtdc; i++ )
    {
        presente = 0;

        for( j = 0; j < qtdp; j++ )
        {
            if( !strcmp( pc[i].nome, pp[j].nome ) )
            {
                presente = 1;
                break;
            }
        }

        if(!presente)
        {
            n++;
            p = realloc( p, n * sizeof(pessoa_t) );
            strncpy( p[n-1].nome, pc[i].nome, NOME_MAX_TAM );
        }
    }

    *pa = p;
    *qtda = n;

    return 0;
}

int main( void )
{
    pessoa_t * lst_completa = NULL;
    int qtd_completa = 0;

    pessoa_t * lst_presenca = NULL;
    int qtd_presenca = 0;

    pessoa_t * lst_ausencia = NULL;
    int qtd_ausencia = 0;

    carregar_pessoas( "listaCompleta.txt", &lst_completa, &qtd_completa );
    carregar_pessoas( "listaPresenca.txt", &lst_presenca, &qtd_presenca );

    processar_ausencias( lst_completa, qtd_completa, lst_presenca, qtd_presenca, &lst_ausencia, &qtd_ausencia );

    gravar_pessoas( "listaFaltantes.txt", lst_ausencia, qtd_ausencia );

    free(lst_ausencia);
    free(lst_completa);
    free(lst_presenca);

    return 0;
}

listaCompleta.txt
JOSE
JOAO
ANTONIO
FRANCISCO
LUIZ
CARLOS
PEDRO
PAULO
MANOEL
LUCAS

listaPresenca.txt
JOSE
JOAO
LUIZ
PEDRO
PAULO
MANOEL
LUCAS

listaFaltantes.txt
ANTONIO
FRANCISCO
CARLOS

Comparação de dados em Arquivos [C]


Can we alter Hilbert's axioms to have $\mathbb{Q}^3$ as a unique model?

Question: Can we alter Hilbert's axioms to have $\mathbb{Q}^3$ as a unique model?
The critical axioms seem to be the congruence axioms IV.1 and IV.4, and presumably the line completeness axiom V.2.
But how are they to be modified?
IV.1 might be replaced by requiring that there are counter-examples (irrationality of $\sqrt{2}$) and appropriately relaxing "congruent" to "almost congruent" (= "arbitrarily close to congruence").
But what about line completeness then, since it might be possible to add irrational points to $\mathbb{Q}^3$ such that the modified axioms still hold?
Comment: Why would you want to?  Q^3 is not a very good model of geometry.
Comment: Which geometry? And what is a "good" model?
Comment: Not far at all - you would end up embedding in R^3 if you wanted to do calculus (useful calculus). It is generally accepted the real numbers(*) are the "best we can do". For instance, preservation of compact sets doesn't hold, intermediate value theorem fails, unfirm continuity on compact sets fails, zero derivitve but non constant function exists - let alone what would happen in Q^3; say bye-bye to stokes theorem, etc. (*) perhaps complex.
Comment: Physicists are in search for discrete space(-time). So I wondered why not start with a "somehow-discrete" space? How far do we get?
Comment: There is a mathematician who prefers to work with rational numbers when it comes to geometry. He has written a book about it. In case you are interested: http://web.maths.unsw.edu.au/~norman/
Comment: @Dejan. Oh, that guy... "A unique and revolutionary text", pretention pushed to a barely bearable point...
Comment: @arbautjc: I wouldn't know, haven't read it. Seems to contain some interesting ideas though.
Answer: In text of
'If A, B are two points on a line a, and if A′ is a point upon the same or another line a′ , then, upon a given side of A′ on the straight line a′ , we can always find a point B′ so that the segment AB is congruent to the segment A′B′'
it must be stated that a' is parallel or perpendicular to a, and the same one with angles. Archimedes axiom is still needed and the last one might be replaced with nearly opposite one:
A restriction of a set of points on a line with its order and congruence relations that would preserve the relations existing among the original elements as well as the fundamental properties of line order and congruence that follows from Axioms I-III and from V-1 is impossible.
Now we could take rational multiples of any segment on a line (Thet depends on similarity and maybe Pasch's axiom), and due to axiom of anti-completeness, no other segments are possible.


Python multiprocessing: How to shutdown a long running process that is a sequence of actions

Question: I want to use multiprocessing to run simultaneously several processes of a series of lengthy actions (e.g. clicking through programs simultaneously using computer vision). I want to implement a stop button in a GUI that would allow me to stop the processes midway - but I'm not sure how I can do something like that.
I know I can use a shutdown event to control the processes if there's a loop - however in my case the target function is not a loop. Here's some code where I basically just did an event check on every line of the target function:
(I deleted a large part of the tkinter code as it's not relevant, the start/stop functions are for their respective buttons in the GUI.)
from multiprocessing import Event, Process
from time import sleep

def start_proc(self):
    self.proc_stop_event.clear()
    for i in range(4):
        p = Process(target=long_sequential_tasks, args=(i,self.proc_stop_event))
        p.start()

def stop_proc(self):
    self.proc_stop_event.set()

#a series of lengthy, different and sequential tasks
def long_sequential_tasks(idx, event):
    if not event.is_set(): <---
        print(str(idx))
        sleep(10)
    else:
        break
    if not event.is_set(): <---
        print(str(idx * 2))
        sleep(10)
    else:
        break
    if not event.is_set(): <---
        print(str(idx * 3))
        sleep(10)
    else:
        break

Checking the event status every line is obviously not a solution, but I really don't know what else I can do. In the future I want to add queues and locks to synchronize the parallel processes, and apparently process.terminate can leave them hanging.
So if process.terminate is not an option, how can I interrupt a process that's carrying out a sequence of actions in a clean way? Or I can still terminate the processes but do a clean up on the hanging stuff?
Comment: I think `is_set()` is the appropriate way to handle this. What you're saying is you have a long series of unrelated things to process that you want to be able to arbitrarily stop, but you can't write these into a loop at all?
Comment: Yes that's right. The individual steps have to be accomplished in a particular order though.
Answer:
Q : how can I interrupt a process that's carrying out a sequence of actions in a clean way?

May implement a signaling/messaging metaplane, using nanomsg or ZeroMQ, that will smart-inform the process ( which is checking for presence of such a <NO-GO>-message, as an example in due places in between the phases of the pure-[SERIAL] workflow check-points )
and once a <NO-GO>-message arrives, the process may perform a graceful-cleanup and smart termination of its own run.
The add-on costs are minimal.
def long_sequential_tasks( idx ):
    #-----------------------------------------------------------------------
    # local
    # mock-up soft-authentcation proxy
    # for otherwise possible ( whitelisting & key-management & other tools available )
    #
    CONTROL_HASH_ID = "asdfhasrtbnastnve9vaergn9ervgnaeragiagiab - WHATEVER"
    LENofMASK = len( CONTROL_HASH_ID )
    #-----------------------------------------------------------------------
    import zmq
    aCtx = zmq.Context()
    aSUB = aCtx.socket( zmq.SUB )
    aSUB.setsockopt(    zmq.SUBSCRIBE, CONTROL_HASH_ID )
    aSUB.setsockopt(    zmq.LINGER, 0 )
    ...
    aSUB.connect( "<protocol>://<address-details>" ) # { ipc:// | tipc:// | norm:// | tcp:// | vmci:// | ...  }

    if (  0 == aSUB.poll( 0, zmq.POLLIN )
       or      aSUB.recv(    zmq.NOBLOCK )[:LENofMASK] != CONTROL_HASH_ID
           ):                    # <--- NO LEGAL EXPLICIT <NO-GO>-MESSAGE HERE
           # continue planned work-step
           ...

    if (  0 == aSUB.poll( 0, zmq.POLLIN )
       or      aSUB.recv(    zmq.NOBLOCK )[:LENofMASK] != CONTROL_HASH_ID
           ):                    # <--- NO LEGAL EXPLICIT <NO-GO>-MESSAGE HERE
           # continue planned work-step
           ...

The requested effect is achieved in a clean way.
Tkinter MVC-actor-based GUI and any other smart-"sources" of control-messages can live with this concept and initiate smart-signaling/messaging outside the world of the tk.mainloop()-scheduler and easily reach inter-platform integrated computing exo-systems ( using this for more than a decade for a remote GUI for a QuantFX prototyping with AI/ML + trading backend ecosystem ).

Another way is to implement the exit-plan via a use of a custom-defined signal-handler. This releaves the need to structure the sequence of steps after/before which the exit-plan is being checked in a rather rigid way. Signal handlers can "interrupt" the flow of computations and may (rather brutally) kill its own process (which needs to add some mitigation measures, if graceful terminations are a must), but represents an option, if loooong running workflow-steps are worth stopping upon some external logic, responsible for the exit-plan flagging a <NO-GO>-state.
Comment: Hi, yeah ok I will read into signal handling and ask a question on that if needed. I think you guys are right though without catching the kill signal there's no avoiding having event/socket checks at close to every line, I did think about having a dictionary or list of functions which I can loop around, but it's going to be quite unreadable. Thanks for your help guys.
Comment: Can you explain the difference between the OP `event.is_set()` approach?
Comment: This way one is free to integrate inter-process control-layer, independently of any form of a shared object ( better share nothing and set explicit control-loops which may include extra-domain inputs, that have nothing to do with pure python )
Comment: It's true that this way opens up more possibilities particularly in cases where you need to connect to other platforms. However it doesn't avoid the need to have the checks at regular intervals along the code
Comment: But your alternative - to catch and handle the terminate signal from process.terminate if i understand correctly - seems to be the only solution. Could you elaborate more on the part where the sequence before/after exit plan needs to be structured in a rigid way?
Comment: I think in all cases you are going to have to write in checking along the way. Normally you would do something like `while not event.is_set():` and then continue but since each function is separate, your option is to do something like increment an `i` and then send to a function that contains all functions where each step is the next `i` and you return each time to automate the checking, but in any case you have to stop and check all the time, it's just how you want to write that step in that changes.
Comment: @JasonTan StackOverflow encourages users not to creep one question into many other of further extending questions. Feel free to accept the best Answer provided to your Question and may you like so, post a new MCVE-formulated problem as a new Question for all the other questions you may have come to. This is how StackOverflow works.


Loading data using for loop in python

Question: I am trying to load data from multiple files. The structure of data file is given by:
np.double np.double......np.double
with spaces between each column
I made the proper np.dtype for these files. To load them into different variable, I wrote a for loop to get the path:
X = []
run = '001'
for i in range(0, 370, 10):
    X.extend(['Phi_(3)_' + str(i) + '/' + 'SRF_GUN2_PHI(3)' + str(i) + '.Scan.' + run])

And then I loaded them manually, like this:
Scan_1 = np.loadtxt(open(X[0]), dtype=Scan)
Scan_2 = np.loadtxt(open(X[1]), dtype=Scan)
Scan_3 = np.loadtxt(open(X[2]), dtype=Scan)
 . . . .
Scan_36 = np.loadtxt(open(X[35]), dtype=Scan)

Is there away to do a for loop for this. Keeping the structure of original files. Or (even better) to combine all these scan files into one big file keeping the format, also using a for loop or anything similar?
Comment: well 1-36 so write a for loop from 1-36 and put the scan files in an array or dictionary
Answer: Are you looking for something like this
Scans = [np.loadtxt(open(file), dtype=Scan) for file in X]

That would create an array of elements 0-36

Loading data using for loop in python


Investigate impact of one variable in the present of another

Question: In my project analysis, I am currently investigate the impact of the ambient temperature on vitamin D level. I am using parametric tests as I transformed the Vit D data to meet the assumption of normally distribution. I conducted regression and found that with increase temperature the Vit D level increases.
Now, I need to see who is affected more male or female? Mean, is there any impact of temperature to reflects different Vit D level between gender? Or males or females were more effected by weather in Vit D level?
What is the test for this or how to conduct it?
I would appreciate any help as I am beginner in statistics.
Answer: Your current model is $$ Y=\beta_0 + \beta_1 X_1 + \epsilon$$ where $X_1 $ = temperature.
Next you can add gender ($X_2$) and the interaction between gender and temperature ($X_1X_2$) in the model, i.e., $$ Y=\beta_0 + \beta_1 X_1 + \beta_2 X_2 +\beta_3 X_1X_2 +\epsilon$$
Next to check if $\beta_3$ is significantly differ from zero. If yes, the effects of temperature on Vitamin D are different between male and female; otherwise, the effects are the same.


looking for a code list for cil , as in what does 16, 17, 18 , 19 , 02 do

Question: I was wondering if there is a cil code list, I think 16 = false and 17 = true, but not 100% sure, also think -1 = 0 , but if anyone has a website to help with this that would be great.
16 = false
17 = true ?

Answer: You can find a complete listing of all opcodes in Partition III of ECMA-335, but as for the specific instructions you listed:
02 (0x02) = ldarg.0
16 (0x10) = starg.s
17 (0x11) = ldloc.s
18 (0x12) = ldloca.s
19 (0x13) = stloc.s

0x16 = ldc.i4.0 // could be a 0 or false or '\0' ... the exact type depends on how it's used.
0x17 = ldc.i4.1 // could be a 1 or true or '\u0001' ... again, the exact type depends on how it's used.
0x18 = ldc.i4.2
0x19 = ldc.i4.3

Comment: From the OPs mentioning of 16 = false and 17 = true, i am pretty sure he just omitted the 0x and his numbers are actually hex.
Comment: Yeah, the thought occurred to me after posting and I was in the process of updating when you commented :)
Comment: ok i was thinking aob when i said cil.. but the cil website helps.. thanks


Move term to a different vocabulary

Question: I want to move a term from one vocabulary to another.  In drupal 7 one can use taxonomy manager to do this but drupal 8 branch has not implemented MOVE yet and the module has not received any update in some time.
Taxonomy merge that is suggested in this thread has not even been ported in drupal 8 yet.
Thus the only way that is left to do such a task is programmatically.
How do you update programmatically the vocabulary or a term?
Answer: You should load existing term using term id and then update vid of term from new vocabulary vid then save it.

<code>
use Drupal\taxonomy\Entity\Term;

$term = Term::load($tid);
$term->vid->setValue('ID value');
$term->Save();
</code>
Answer: https://www.drupal.org/project/term_manager
You can use Term Manager to move Term to new Vocabulary

Move term to a different vocabulary


ion-nav-buttons full width

Question: I am trying to add search bar into header on one of the page.
<ion-view view-title="">
    <ion-nav-buttons side="left">

            <input type="search" placeholder="Search">

  </ion-nav-buttons>

  <ion-content>
    Search content
  </ion-content>
</ion-view>

The search bar does show up in the header as expected. But how can I get the search field to be 100% width across the header? I did try to apply:
.buttons-left, .left-buttons{
    width:100% !important;
}

It does work. But the problem is it affect all other pages as well. I tried to add class and id to ion-nav-buttons, but it doesn't shows up.
How can I apply custom width to ion-nav-buttons only on specific page?
Comment: You probably have some parent containers, at least ``. You could add id/class to them then set a width according to the selected class, like: `body.anyclassname ion-nav-buttons { width: 100%; }`. Then you add this class name to  only when you want it 100% large.
Comment: Any comments about my suggestion? Cheers.
Answer: I am not sure if that is the best place where to put a search field:
<ion-nav-buttons>

You can define a custom header for your specific view:
<ion-header-bar class="bar-positive">

        <input type="search" placeholder="Search" class="search-bar">

    <button class="button button-clear">Cancel</button>
</ion-header-bar>

and use the directive hide-nav-bar="true" in your view to tell the current view to hide the navbar:
<ion-view view-title="home" hide-nav-bar="true">
    ...
</ion-view>

This is your end result:
<ion-view view-title="home" hide-nav-bar="true">

  <ion-header-bar class="bar-positive">

      <input type="search" placeholder="Search" class="search-bar">

    <button class="button button-clear">
      Cancel
    </button>
  </ion-header-bar>

  <ion-content padding='true' scroll='false' has-footer='false'>

        HOME PAGE

  </ion-content>

</ion-view>

I've also used some styling to change the result:
.search-bar
{
  width:100% !important;
  background: inherit;
  height: inherit !important;
}

and this is how it looks in a Plunker.

ion-nav-buttons full width


Listar dados de 2 tabelas vinculadas à tabela pai

Question: TABELA PEDIDOS
---------------------------
| id_pedido | pedido_data |
---------------------------
|    1      | 2016-01-01  |
---------------------------

TABELA PEDIDO_ITENS
-------------------------------------
| id_item | id_pedido | valor_total |
-------------------------------------
|    1    |     1     |    10.00    |
-------------------------------------
|    2    |     1     |     5.00    |
-------------------------------------

TABELA PAGAMENTOS
-----------------------------------------------
| id_pgt | id_pedido | data_pgto | valor_pago |
-----------------------------------------------
|   1    |     1     | 2016-01-10|    2.00    |
-----------------------------------------------

Preciso agora fazer uma consulta que me apresente da seguinte forma:
-------------------------------------------------
| id_pedido |    data    |  Débito  |  Crédito  |
-------------------------------------------------
|     1     | 2016-01-01 |   15.00  |           |
-------------------------------------------------
|     1     | 2016-01-10 |          |    2.00   |
-------------------------------------------------

Fiz uma tentativa mas nao deu certo:
(SELECT p.id_pedido, p.pedido_data, SUM(pi.valor_total) AS vTotal FROM pedidos AS p
    INNER JOIN pedido_itens AS pi ON pi.id_pedido=p.id_pedido)
UNION ALL
(SELECT p.id_pedido, pg.data_pgto, pg.valor_pago AS vTotalPago FROM pedidos AS p
    INNER JOIN pagamentos AS pg ON pg.id_pedido=p.id_pedido)

Abaixo SQL das tabelas:
   --
   -- Estrutura da tabela `pagamentos`
   --

   CREATE TABLE IF NOT EXISTS `pagamentos` (
   `id_pgto` int(11) NOT NULL AUTO_INCREMENT,
   `id_pedido` int(11) NOT NULL,
   `forma_pgto` enum('1','2','3','4') DEFAULT NULL COMMENT '1=Dinheiro    2=Cheque 3=Cartao 4=Boleto',
   `data_pgto` date NOT NULL DEFAULT '0000-00-00',
   `valor_pago` decimal(10,2) NOT NULL,
   PRIMARY KEY (`id_pgto`),
   KEY `id_pedido` (`id_pedido`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

   --
   -- Extraindo dados da tabela `pagamentos`
   --

   INSERT INTO `pagamentos` (`id_pgto`, `id_pedido`, `forma_pgto`, `data_pgto`, `valor_pago`) VALUES
   (1, 36, '1', '2016-09-15', '10.00');

   -- --------------------------------------------------------

   --
   -- Estrutura da tabela `pedidos`
   --

   CREATE TABLE IF NOT EXISTS `pedidos` (
     `id_pedido` int(11) NOT NULL AUTO_INCREMENT,
     `pedido_data` date NOT NULL DEFAULT '0000-00-00',
     `pedido_cliente` int(11) NOT NULL,
     `pedido_status` enum('1','2','3') NOT NULL DEFAULT '1',
     PRIMARY KEY (`id_pedido`),
     KEY `id_cliente` (`pedido_cliente`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=37 ;

   --
   -- Extraindo dados da tabela `pedidos`
   --

   INSERT INTO `pedidos` (`id_pedido`, `pedido_data`, `pedido_cliente`,`pedido_status`) VALUES
   (36, '2016-09-13', 6, '1');

   -- --------------------------------------------------------

   --
   -- Estrutura da tabela `pedido_itens`
   --

   CREATE TABLE IF NOT EXISTS `pedido_itens` (
     `id_item` int(11) NOT NULL AUTO_INCREMENT,
     `id_pedido` int(11) NOT NULL,
     `id_produto` int(11) NOT NULL,
     `qtde` int(11) NOT NULL,
     `valor_unit` decimal(10,2) NOT NULL,
     `valor_total` decimal(10,2) NOT NULL,
     PRIMARY KEY (`id_item`),
     KEY `id_pedido` (`id_pedido`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=46 ;

   --
   -- Extraindo dados da tabela `pedido_itens`
   --

   INSERT INTO `pedido_itens` (`id_item`, `id_pedido`, `id_produto`, `qtde`,`valor_unit`, `valor_total`) VALUES
   (44, 36, 5, 2, '1.00', '10.00'),
   (45, 36, 5, 2, '1.00', '5.00');

Comment: Para facilitar, você pode criar essa estrutura no http://sqlfiddle.com/
Answer: Sua consulta está correta, só faltou incluir o group by na primeira consulta.  Segue a consulta:
SELECT p.id_pedido, p.pedido_data, SUM(pi.valor_total) AS vTotal
FROM pedidos AS p INNER JOIN
     pedido_itens AS pi ON pi.id_pedido=p.id_pedido
group by p.id_pedido, p.pedido_data
UNION ALL
SELECT p.id_pedido, pg.data_pgto, pg.valor_pago AS vTotalPago
FROM pedidos AS p INNER JOIN
     pagamentos AS pg ON pg.id_pedido=p.id_pedido

Só achei estranho que na criação das tabelas as colunas de auto-incremento estão sendo incluídas nos inserts.
Considerando que as colunas em branco serão sempre zero, você pode fazer o seguinte:
SELECT p.id_pedido, p.pedido_data, SUM(pi.valor_total) AS Debito, (p.id_pedido-p.id_pedido) as Credito
FROM pedidos AS p INNER JOIN
     pedido_itens AS pi ON pi.id_pedido=p.id_pedido
group by p.id_pedido, p.pedido_data,  (p.id_pedido-p.id_pedido)
UNION ALL
SELECT p.id_pedido, pg.data_pgto, 0 as Debito, pg.valor_pago AS Credito
FROM pedidos AS p INNER JOIN
     pagamentos AS pg ON pg.id_pedido=p.id_pedido

id_pedido   pedido_data Debito  Credito
1           2016-09-13   15.00     0.00
1           2016-09-15    0.00    10.00

Comment: Obrigado @Developer, aparentemente deu certo, só vou verificar os valores calculados com mais tempo.
Comment: Ok, caso tenha algum problema, é só postar.


Drupal go to with destination

Question: I have a page with two links visible to people, but on clicking  of either of two, user needs to log in to view, so in case user is not logged in, i am sending users to login page with drupal_goto('user'), i want that if the user clicked 1st link and is redirected to login page, after successful login, he should be redirected again to that 1st link
i tried with this code but did not work,
$query = array('destination' => 'node/id');
drupal_goto('user',$query);

Comment: @googletorp has provided an answer. If you are trying to create a link to the login form that would send the user back to the current page, see if  `drupal_get_destination` works better.
Answer: You need to use drupal_goto the same way you use url.
So what you need to do is this:
$query = array('destination' => 'node/id');
drupal_goto('user', array('query' => $query));

Drupal go to with destination


Query mostrar imagem

Question: Gostaria de saber se consigo mostrar uma imagem dependendo do nome.
 $qr = mysql_query($sql) or die(mysql_error());
 while($exibe = mysql_fetch_array($qr)){
 echo'

Nome: '.$exibe["Nome"].'

Morada:   '.$exibe["Morada"].'

Distrito: '.$exibe["Distrito"].'

Concelho: '.$exibe["Concelho"].'

Por exemplo:
Mostra-me o nome de uma equipa(Real madrid) e consigo com que ele me mostre o simbolo do mesmo sem mexer na base de dados.
Consigo fazer isso?
Comment: a imagem também está guardada na base de dados?
Answer: Por exemplo, vamos supor que esteja guardado em Nome a equipa/time:
echo'

Aí você salva a imagem como /imagens/Real Madrid.jpg.

A grafia tem que ser idêntica ao que está no DB, inclusive as maiúsculas e minúsculas.
  Num caso real de uso, o ideal é normalizar os nomes e tirar os espaços e acentos pelo PHP.

Query mostrar imagem


Java RowFilter on multiple columns

Question: I want to sort/filter a JTable on multiple columns, f.E.

Column1  Column2
   a        1
   b        2
   c        2

I want to sort this table by the Column1 = b and by the Column2 = 2.
MyTableModel model = new MyTableModel();
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);

...

private void newFilter() {
    RowFilter<MyTableModel, Object> rf = null;
    //If current expression doesn't parse, don't update.
    try {
        rf = RowFilter.regexFilter(filterText.getText(), 0);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}

But how to sort on more than one column?
Answer: you can create RowFilter.AndFilter and pass all filters that you need:
List<RowFilter<M, O>> listOfFilters = new ArrayList<>();
... add filters into list
RowFilter.andFilter(listOfFilters);

Comment: Solved it: Pattern.quote(filterValue)
Comment: It works perfect, except when I filter on a String containing "(" or ")" for Example: "Car (Porsche)" . How to deal with that?
Answer: Simply remove the parameter "0" from the RowFilter to sort all columns:
rf = RowFilter.regexFilter(filterText.getText());

Answer: I used 2 inputs JTextField (filterTextOras,filterTextClient), using simple RowFilter<MyTableModel, Object> if either textfields are empty and compoundRowFilter is both contain text.
protected void Filtru() {
        RowFilter<MyTableModel, Object> rfo = null;
        RowFilter<MyTableModel, Object> rfc = null;
        if(filterTextClient.getText().equals("")){
            if(filterTextOras.getText().equals("")){
                sorter.setRowFilter(null);
            } else {
                rfc = RowFilter.regexFilter(filterTextOras, 1);//column 1
                sorter.setRowFilter(rfc);
            }
        } else {
            if(filterTextOras.getText().equals("")){
                rfo = RowFilter.regexFilter(filterTextClient.getText(), 0);//column 0
                sorter.setRowFilter(rfo);
            }
            else{
                List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
                filters.add(RowFilter.regexFilter(filterTextClient.getText(), 0, 0));
                filters.add(RowFilter.regexFilter(filterTextOras.getText(), 1));
                RowFilter<Object,Object> compoundRowFilter = RowFilter.andFilter(filters);
                sorter.setRowFilter(compoundRowFilter);
            }
        }
    }

Java RowFilter on multiple columns


How do I resize image directly while uploading the image?

Question: How do I resize image directly while uploading the image?
Select image from Computer > Upload > Resize directly to dimension that I want.
Comment: https://www.drupal.org/project/manualcrop
Answer: Try Max Image Size module
On the module page it says:

This module will resize uploaded images to be below the set
  dimensions. It is not an image style or other presentation layer
  module. If you want to maintain the original image you should use
  Image Resize Filter or an image style. The use case for this module is
  sites where users may upload very large images, but you do not
  want/need to keep the original.
Answer: If you are looking for crop image, then use Imagefield Crop module you can do that. Use image upload with crop as your widget after installing this module. Some other image crop modules are also available.
Comment: If the original image is 1mb and the resize image is 100kb. The total uploaded images to website is 1mb + 100kb or 100kb only? I am looking the resize module that upload resize image only. @Abin


$L|F$ and $F|K$ unramifed imply $L|K$ unramified

Question: Let $K$ be a Henselian field with respect to a (non-Arch) valuation $v$ (not necessarily discrete). Let $L|K$ be an infinite algebraic extension and let $F|K$ be any subextension. Then $v$ extends uniquely to $F$ and $L$. Let $v_L$ be its extension to $L$.
Suppose that $F|K$ and $L|F$ are unramified, then can we show that $L|K$ is unramified?
(We say that a finite extension $L|K$ is unramified if $[L:K]=[l:k]$ and $l|k$ is separable, where $l$ (resp. $k$) denotes the residue field of $v_L$ and $v$. An algebraic extension $L|K$ is unramified, if all its finite subextensions are unramified.
If $L|K$ is finite, then we can indeed show this by computing all involved degrees. So far it is not clear that why we can pass from an algebraic extension to a finite one.)
Answer: If $L/F$ and $F/K$ are infinite and unramified then take an arbitrary finite subextension $A/K$, let $B$ be the normal closure of $A$ in $\overline{L}$, let $C=B\cap L$ and  $D=C\cap F$.
For simplicity assume that $CF = F(c)$ has a primitive element with $c\in C$ (the idea stays the same if there is no primitive element). The $F$-minimal polynomial of $c$ is in $D[x]$.
So $[CF:F]=[C:D]$.
$CF/F$ is unramified so $[CF:F]=[\kappa(CF):\kappa(F)]$.
We know that $[\kappa(C):\kappa(D)] \ge [\kappa(CF):\kappa(F)]$.
Whence $
[\kappa(C):\kappa(D)] = [C:D]$ and $C/D$ is unramified.
Since $D/K$ is unramified as well we get that $[C:K]=[C:D][D:K]=[\kappa(C):\kappa(D)][\kappa(D):\kappa(K)]=[\kappa(C):\kappa(K)]$ ie. $C/K$ is unramified which proves that $L/K$ is unramified.
Comment: @Tian Ok, I edited my answer
Comment: Thanks a lot for your wonderful argument! (Actually this question has confused me for quite a long time...)


Should I use PA speakers for a full band all the time?

Question: I've been playing guitar, drums, and singing for about 6 years but I have no experience outside of playing by myself with an amp and speaker. I want to get serious about starting a band and playing now that I have a real job and cash flowing in. I'm having trouble figuring out the best way to amplify the sound.
Ideally I want a full band: drums, 2 guitars, bass, and vocals. I want to be able to play live, practice, and record.
Should I just get a good PA system and plug amps from the instruments into it? Or should I use classic amp and speakers for each instrument?
What do I need to consider in terms of sound quality, mixing,and cost effectiveness?
What do you think the best option is and what problems might I have?
Please help! I don't want to buy $1000 worth of stuff and then find out I need another $1000 just to make it work or that it won't even sound good in a particular setting live, practice, recording. Help me be realistic cause I have no idea!
Comment: @user45266 Why do you think this isn't the right website to ask this question? We've had similar questions before, and it's pretty relevant to both music practice and performance. The question can be improved in different ways (as is, it's very subjective and needs more info imo, but it can be carved into something that is useful for everyone), but relevance is not one of the issues I see.
Comment: @VonHuffman - Depending on what sort of gig. Playing in all sorts of bands - from duos to big bands, 2/3 times a week for 50 odd yrs, there were not that many venues providing house pas with decent sound men. Those that did were sometimes woeful in the sound produced. It's advisable to bring your own pa and be in charge of your own sound/foldback etc.Occasional venues do provive pas - beat up, with signs reading 'do not touch'.I at least would have my own mic, instrument and amp - self contained, if you like.Singers used to turn up with 'where's my mic?' Answer was usually 'still in the shop.'
Comment: Surely this question is about Musical Practice. Methods,  modes and equipment we use when playing live gigs and rehearsing?
Comment: You're starting a band that you want to play live with, right? Who's going to be in that band? Usually, bands aren't just one guy buying all the gear - in fact, in my experience it's been 'bring your own gear' as a general rule. Anyway, this isn't the right website to ask this question on. Best of luck!
Comment: I guess I'll clarify: My initial objection was that to me, the post read a bit like a shopping-recommendation-type question, which we don't do. It also seems pretty subjective and broad (how do I set up my entire new band), so I thought it would be really hard to give a useful answer. However, now that the question's drawn some excellent in-depth answers, I might rethink my close vote - I guess it's not unanswerable. I would still like to see this edited...
Comment: Get the basics and then add more as you go. Start with vocals then work on adding other instruments, and then working on an in-ear monitoring setup.
Comment: I think you already have a lot of good answers, but I would make one simple suggestion: go watch some bands at the types of venues you intend on playing. Get there early enough to watch setup. Pay attention to who brings what, versus what the house owns. Use those observations to refine what the answers below are suggesting.
Answer: I went down a similar path to eventually starting a full band to accompany me on stage in paying gigs vs playing just for fun.  My band has bass, two guitars, drums and vocal mics for all four musicians.
What you would need for amplification of your instruments and vocals is highly dependent on the venue your band will be performing in.  For a small pub, coffee shop or restaurant with tile floors you won't need as much amplification as you would say on a large outdoor patio or outdoor amphitheatre or a larger indoor venue such as a large ballroom.
My band plays primarily in smaller indoor spaces and occasionally at small outdoor parties.  A small multi channel mixer driving PA speakers will handle multiple vocal mics, acoustic guitar, digital piano and electronic drums.  I run all vocals and acoustic guitar and Roland Electronic Drums through the PA.
Acoustic drum kit is often loud enough (sometimes too loud) to get by with little to no amplification.  If you need to mic certain parts of an acoustic drum kit you can use mics designed for that purpose on boom mic stands. Most of the venues I play in do not want the music super loud (and are acoustically lively) so an acoustic kit would be overpowering.  The Electronic Drums allow for the volume of the drums to be adjusted to fit the needs dictated by the venue and the other instruments don't have to be turned up to match the volume of the acoustic kit.
In my band, the electric guitar and electric bass run through their own individual amps.  A Bass Amp will cover a wide area (low frequencies being less directional) and electric guitar amp makes it easier to dial in certain electric guitar tones by having both a clean channel and one for distortion.  If we play a room that is L shaped and have to point one PA speaker down each of the two sections, I will usually mic the Bass and Guitar cabinets, or plug them into a channel on the mixer using a direct out from the amplifier.  Not all guitar and bass amps have that capability, but my musicians have amps that do.
The next consideration is whether to use a powered mixer with passive speakers or a passive mixer with powered speakers.  The trend seems to be going towards powered speakers with a non powered digital mixer.  For a four piece band you should get at least 8 channels - more if all your musicians will sing harmony or take turns singing lead.  Consider that an electronic drum kit will use two channels to run in stereo.  Same with a digital keyboard.  Microphones will need XLR inputs, so be sure your mixer has enough for the number of mics you will use, including if you mic any guitar cabs or portion of an acoustic drum kit.
I recommend a 12 channel non-powered mixer with powered speakers because you can expand your PA system to accommodate various size venues with varying sound reinforcement needs.  In other words, you can start with some smaller powered speakers for smaller venues, and later add higher wattage speakers to cover larger venues (and even run them all together in series).  The same non powered mixer can send a signal to a variety of different powered PA speakers.
You might also consider floor monitors for yourself and bandmates.  The PA speakers will direct sound towards the audience, but your musicians (particularly any vocalist) will need to hear the mix in detail, and a monitor facing the band will make it much easier.  Some of the line array PA systems ("PA on a stick") are designed to be placed behind the musicians to allow both the musicians and audience to hear the sound from the speakers.
The self contained PA systems such as the type Bose made popular (but now close to a dozen other manufacturers have a version of) are primarily designed for one or two musicians with vocals and don't have enough independent input channels for a four piece band with four vocal mics. But you can use a non-powered mixer to enable more inputs into those systems.  For a small venue a "PA on a stick" line array could potentially run your vocals and acoustic guitars, and your electric guitar and bass could play through their respective amps. The line array can serve as the monitor for the band and eliminate the need for separate floor wedges.
Another consideration is weight and and the ease of set up. The newer powered PA speakers are actually much lighter than many passive PA speakers.  The more components your system has, the more time you will spend setting up and breaking down.
That is my two cents worth based on my personal experience. You might visit some of the local music venues near you to see what other bands are using and interview them during their break to ask why they use what they do and if they would do anything different if they were to make a change.  You will also be able to experience first hand how their system sounds.
I really enjoy playing with a band and encourage you to give it a try yourself.  Good luck and have fun!
Answer: Rockin Cowboy has comprehensively covered a lot of points!
A couple more thoughts for you.
Consider what sort and size venues the band may be gigging at. There's a fair difference between a bar that can hold 50 people from an outdoor gig where there might be 300 or so. And the equipment will vary accordingly.
As already stated, guitarist and bassist ought to provide their own gear. As one who occasionally has to use others' gear, I prefer to lug my own anyway!
Keyboards? When playing keys, again, I prefer to be self-contained. I know my gear, and prefer to rely on it rather than someone else's.
Drums? Acoustic or electric. Drummer's job to buy, bring and set up. Usually no need to mic up acoustic, maybe kick drum, through pa. A lot of electronic players expect to go through pa though. They already have a bit of setting up to do!
Vox? Slightly thorny one. If there's a bandleader, s/he often owns the pa sometimes enough mics for all, sometimes singers provide their own. I know I prefer to sing through a mic that I know isn't used by anyone else!
Pa itself? I still prefer the old idea of mixer (or mixer/amp) into passive speakers. I used to use the foldback system itself on small gigs as the pa. Money-wise, if it's your band, and you get gigs, hire and fire, negotiate, etc., etc., then you're going to need a pa unless you only play gigs where that's provided. From experience (in UK) that's a mixed bag. That pa won't come cheap, so it might be worth considering an extra cut from the earnings to go towards it a little. After all, without it, some gigs won't be playable!
Rehearsal wise, no need for loud, so maybe one pa speaker, and maybe everyone goes through it. Or pay lots for rehearsal studio sessions! And don't forget somewhere needs to be found to store the gear, and transport it. Good luck!
Answer: Using low volumes on stage and micing/DI'ing your amps to a console gives your sound engineer more control on how your music sounds in the audience, which hopefully improves the quality. At the price of a higher cost (devices and engineer).
While you are not sure what to buy, rent. I might have been lucky, but in my experience renting a PA is pretty affordable. This would give you some time to learn what kind of gear you'd need for what kind of venues. You could even invoice the rentals separately.
I've played for years with rented PA-equipment with a friend behind the buttons until at some point via via we got a dedicated technician with his own gear.
Comment: Micing a guitar amp. is well preferrable - the speaker is part of the sound chain.
Answer: TL;DR: DON'T BOTHER TO BUY A PA SYSTEM!
All the other answers seem to cover all bases, but I think people forgot one particular detail: on the 1970s and early 80s, it was common for bands to own their PA systems (and lights, and stage assembly etc).
This is not the case any more!
Almost all venues that you'd be playing these days have their own PA in place.
Even for rehearsals, pretty much any city would have a rehearsal studio, so you don't need to bother acquiring a PA for the vocals. I'm from Brazil and even though we are an undeveloped country, almost all cities (excluding the reeeealy poor villages) have some sort of rehearsal space. I'm guessing you are in the USA, so your situation migh be a little better.
If everything else fails, and you are so unlucky that, for one particular date, you need to bring your own PA, you can always rent.
On a side note: $1000 for a complete PA? You will be spending more than that just for the mikes!
Edit: repeated words.
Comment: A "venue", maybe.  But gigging in bars, pubs, cafes, small outdoors parties - you need your own PA system for all of those.  And as a beginning band, those are where you're going to be playing most.  As for the cost of PA, you could *easily* get a powered mixer, a couple of speakers, a few AKG D-5 or Senn e835 mics, and all the cables you'd need, all new.  Second-hand, even better.
Comment: For those who gig in US, I can say that few of the restaurants, sports bars, pubs, private events or parties that I play in have their own PA.  Also in US - the industry standard Shure SM58 Mic (admittedly not the best but very popular) runs $99.00 US.  But your point is well taken in that by the time you add mics, mic cables, speaker cables, mic stands, speaker stands, extension cords, etc. the total cost will be much higher than just the mixer and speakers.
Answer: When I first started a band, I started with a mixer/amp and passive speakers, and three vocal  mics. guitar players had their own amps, keyboard players had their own set-ups and drums were not amplified. I started small. I started at the beginning. I started with what I could afford. I also started learning about being in a band and how to run a budget so that we could grow into something more. I budgeted for more sound equipment, more band equipment, a trailer to haul equipment, then a van. And it hasn't stopped yet. My point is this, it's a process where you start at the beginning according to what you can afford, and then you try to build and expand on it. There is time to learn what you need to learn but you must be willing to learn, otherwise you'll just get stuck and won't move forward, just like anything else in life. That's my experience anyway.


Defining an f-invariant measure

Question: Suppose I have a compact oriented manifold $M$ with an orientation preserving self-diffeomorphism $f$. I wish to define a volume form on $M$ which is invariant under $f$. Certainly, it is necessary that for any open $U\subset M$, its image $f(U)$ is not a proper subset of $U$. Is this sufficient? If not, what would be sufficient?
A natural thing to try is to start with a volume form on $M$ and then try to even it out by repeatedly averaging the form with its pullback by $f$. However, I don't know how to force convergence.
Comment: This is in general a pretty hard question. Try googling for "SRB measures" and "physical measures", there are lots of results and open questions.
Comment: Ah, thanks. Is there a specific counterexample?
Answer: This is a very tough question. I don't think that you are likely to find useful necessary and sufficient conditions for the existence of an invariant volume form, especially if you require your volume forms to be smooth instead of just, say, absolutely continuous. There is one pretty general theorem that comes to mind that seems related to your topological criterion.

Let $f\colon M\to M$ be a $C^k$-smooth map, with $k\geq 2$. Suppose that there exists a Riemannian metric on $M$ with respect to which $f$ is expanding. That is, there exists a $\chi>1$ such that $\|Df(v)\|\geq \chi\|v\|$ for all tangent vectors $v$ on $M$. Then $f$ has a unique absolutely continuous invariant measure $\mu$, and this measure is strictly positive. Moreover, $\mu$ is at least $C^{k-1}$-smooth. In particular, if $f$ is $C^\infty$, the measure $\mu$ is a smooth volume form.

I believe the theorem remains true when $k = 1$ as well, except that $\mu$ may no longer be strictly positive. I could be wrong about that though. The expanding condition definitely implies your topological condition, so in that way this is a weaker statement than what you are hoping for. Then again, I don't think the statement you are hoping for is actually true. For a reference to the above theorem, see the article The measures invariant under an expanding map by Richard Sacksteder.
A word about your idea for finding an invariant volume form: it can be pushed a little further. Suppose $\omega$ is the volume form you start with, normalized to give you a probability measure $\mu$. Let $\mu_n$ be the measure $$\mu_n = \frac{1}{n}\sum_{k=0}^{n-1} f^{k}_*\mu.$$ This is a probability measure for each $n\geq 1$, but, as you say, there is no guarantee that the $\mu_n$ converge to anything, much less a volume form. However, because the weak topology on the space of Radon probability measures on $M$ is compact, there is some subsequence $\mu_{n_k}$ that converges weakly to a probability measure $\nu$. This $\nu$ is easily seen to be invariant. Of course, it will usually not be a volume form.
One final comment as far as conditions on the existence of an invariant volume form go: one thing that must be true is that almost every point $x\in M$ must be recurrent. This is a consequence of the Poincaré recurrence theorem.
Edit: I missed that you are looking at a diffeomorphism. There are no expanding diffeomorphisms of compact manifolds, so the above theorem would not apply.
Answer: In 1932 Denjoy constructed examples of $C^1$ circle diffeomorphisms that are topologically semi-conjugate to an irrational rotation, hence uniquely ergodic, for which the unique invariant probability measure is supported on a Cantor set on the circle. With higher regularity this can't happen anymore, but even for real-analytic maps the measure can be singular w.r.t. Lebesgue measure, as shown by V.I.Arnold.
For torus maps the situation gets infinitely worse...
EDIT: I just realized that the Denjoy examples actually have an open set $U$ such that $f(U)$ is a proper subset of $U$. Anyway, the Arnold examples are honestly topologically conjugate to an irrational rotation, so for them such a set $U$ does not exist.

Defining an f-invariant measure


What is meant in the quotation of Terry Tao?

Question: Terrence Tao commented of internalizing [here: https://terrytao.wordpress.com/career-advice/does-one-have-to-be-a-genius-to-do-maths/]

"It is true that some mathematicians can be vastly more efficient than
  others at learning material, but I feel this is more due to experience
  and an efficient means of study than to any innate genius ability,
  though of course innate talent is still a contributing factor. For
  instance, among the graduate students I have advised, the first paper
  they read in a subject often takes a month or so to read (and they
  have a question on almost every page on the paper); but after a few
  years, they can get the gist of a new paper in the subject within a
  day, skimming past all the "standard" (or at least "plausible")
  portions of the argument and focusing on the key new ideas. The key,
  I think, is to find one or more efficient ways to internalise the
  subject – either by using formalism, or geometric intuition, or
  physical intuition, or some other analogy or heuristic. Each
  mathematician has their own different way of doing this. Ramanujan,
  for instance, apparently performed a tremendous number of numerical
  computations, and derived much of his intuition from the patterns he
  observed from those computations. The intuition wasn't always correct
  (for instance, he famously gave an incorrect formula for the n^th
  prime), but he did discover a number of amazing results this way, some
  of which took a long time to prove rigorously."

I could not make any sense of what's written in the bold letters (not the literal meaning, of the methods). How one does that ?
Comment: The title suggests you literally don't know what the quote means, the body of the question suggests you want advice achieving that state. They're two very different questions, which are you asking?
Comment: @pjs36 Both, (The first one because I'm stupid; the second because I don't want to be) but focusing on the latter.
Answer: Practise, practise and more practise. The more you study and the more questions you answer the more you'll get a feel for the subject you are looking at. What Terry is pointing out is that the way in which you get that intuition varies for different people, some people will end up 'seeing' their subject in a geometric way, e.g. viewing homology as the search for holes in mathematical objects and spaces, others will work towards physical intuitions e.g. when proving very abstract solutions to nonlinear PDE often ideas from physics like conservation of 'mass' and 'energy' type equations are used in an intuitive way.
The essence of all of these ways of gaining a deeper understanding of a subject is that you take the formal definitions, which might be very abstract indeed and by considering them in a geometric, physical or other intuitive light you gain a real understanding of what the definitions $mean$ rather than just what they say.
Comment: Sorry if I was unclear, I meant that you need to practise developing the intuition for a given area and part of this comes from solving a lot of problems and doing lots of reading into a subject to really try and develop an idea of what is happening. I agree that rote learning won't do it but as part of developing an intuition doing problems and proofs etc. is key.
Comment: How you gain deeper understanding in something ? Rote practice will not probably suffice.


SQL Oracle how to extract xml tag content in a blob column

Question: There is an xml that is in a column of type BLOB (in oracle) and I need to access a certain tag from that xml. Until then I can retrieve the column this way:
SELECT TRIM(UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(my_column_blob, 1024))) as tag_name_xml
FROM my_table

the return is like this:

Here I leave part of the xml content:

How do I return the value/content of the cUF or cCT tag?
I would like the help of people with more knowledge, please.
the proposed solutions are very elegant, but for my case what would it be like to access with this xml structure to access the contents of the cUF tag?
<?xml version="1.0" encoding="UTF-8"?>
<cteProc xmlns="http://www.example.com" version="3.00">
   <CTe>
      <infCte version="3.00">
         <ide>
            <cUF>15</cUF>
         </ide>
      </infCte>
   </CTe>
</cteProc>

Answer: You can first convert your BLOB data into CLOB type, and then extract the tags you need. Here are two functions that will, hopefully, solve your problem:
FUNCTION BLOB2CLOB(mBLOB IN BLOB)
    RETURN CLOB IS
BEGIN
    Declare
        mCLOB   CLOB;
        mDestOffSet     INTEGER;
        mSrcOffSet      INTEGER;
        mBLOBCSID       INTEGER;
        mLangCntx       INTEGER;
        mWarn           INTEGER;
    Begin
        mDestOffSet := 1;
        mSrcOffSet := 1;
        mBLOBCSID := 0;
        mLangCntx := 0;
        mWarn := 0;
        DBMS_LOB.CreateTemporary(mCLOB, TRUE);
        DBMS_LOB.ConvertToClob(mCLOB, mBLOB, DBMS_LOB.GetLength(mBLOB), mDestOffSet, mSrcOffSet, mBLOBCSID, mLangCntx, mWarn);
        RETURN(mCLOB);
    End;
END BLOB2CLOB;
--
FUNCTION Get_SOAP_PartCLOB(mCLOB IN CLOB, mTag IN VARCHAR2) RETURN CLOB
  IS
BEGIN
    Declare
        wrkCLOB         CLOB;
    myStart       NUMBER(10) := 0;
    myEnd         NUMBER(10) := 0;
    myLength      NUMBER(10) := 0;
    toErase       NUMBER(10) := 0;
    Begin
    DBMS_LOB.CreateTemporary(wrkCLOB, True);
    DBMS_LOB.COPY(wrkCLOB, mCLOB, DBMS_LOB.GETLENGTH(mCLOB));
    --
    myStart := DBMS_LOB.InStr(wrkCLOB, mTag, 1, 1) + Length(mTag) - 1;
    myEnd := DBMS_LOB.InStr(wrkCLOB, SubStr(mTag, 1, 1) || '/' || SubStr(mTag, 2));
    myLength := DBMS_LOB.GetLength(wrkCLOB);

    DBMS_LOB.ERASE(wrkCLOB, myStart, 1);
    toErase := myLength - myEnd + 1;

    DBMS_LOB.ERASE(wrkCLOB, toErase, myEnd);

    wrkCLOB := REPLACE(wrkCLOB, ' ', '');

        RETURN(wrkCLOB);
    End;
END Get_SOAP_PartCLOB;

You can use it like this:
SELECT
    Get_SOAP_PartCLOB(BLOB2CLOB(YOUR_BLOB_COL), 'your_tag_in_clob') as "TAG_COL_ALIAS"
FROM
    YOUR_TABLE

I use the second function Get_SOAP_PartCLOB() to get some pretty big B64 data out of the soap envelope response from certain web services. That's why the return type is CLOB. You can cast the returned value to something else or make your own adjustments to the function.
Answer: Use XMLTYPE and XMLTABLE:
SELECT x.c
FROM   table_name t
       CROSS JOIN XMLTABLE(
         '/a/b/c'
         PASSING XMLTYPE(t.value, 1)
         COLUMNS
           c VARCHAR2(200) PATH './text()'
       ) x;

Or XMLQUERY:
SELECT XMLQUERY('/a/b/c/text()' PASSING XMLTYPE(value, 1) RETURNING CONTENT) AS c
FROM   table_name

Which, for the sample data:
CREATE TABLE table_name (value BLOB);

INSERT INTO table_name (value)
VALUES ( UTL_RAW.CAST_TO_RAW( '<c>something</c>' ) );

Both output:

C

something

db<>fiddle here
Comment: I understand that xml data from the question are somewhere within some binaries  in the BLOB in which case this will not work without conversion to CLOB (or varchar ...). It is, offcourse, the ok solution for clean xml content.
Comment: @dr The question does not mention binary data within the `BLOB`, the OP only wants to extract certain tags. Even if there is binary data then it is usually stored as Base64 within the XML and that is still only text data and can retrieved in exactly the same manner as this answer (possibly changing `VARCHAR2` to `CLOB`) and then subsequently converted back to a `BLOB`.
Comment: I agree, if there are clean xml formated characters. It is just that pictures above showing some binaries at the begining of a row, before xml. There is also a possibility thrre are more of them in the BLOB, otherways I would expect the data to be stored in a CLOB type. Wouldn't you too?


How to calculate t-value for testing whether there is differences between two coefficients

Question: I have been given the coefficient values for beta_1Vienna = -0.31 with a standard error of 0.038 and beta_2Amsterdam = -0.27 with a standard error of 0.040. To calculate it I used the formula in the picture, which gave me a t-value of 20, but this would mean I should reject the null, while my professor says to accept it. Can anyone help me with it?
Calculation for t-statistic
 = 20
Hypothesis
Comment: @whuber as noted in my answer, the OP calculated SE(B1) - SE(B2) rather than SE(B1 - B2). So you have 0.04 / (0.002) = 20
Comment: Questions on homework/school work need to be tagged `self-study` so that you get the benefit of thinking deeply on the problem.
Comment: For the t value I obtain $(-0.31 - (-0.27)) / \sqrt{0.038^2 + (-0.040)^2} = -0.72.$  Where does  "20" come from??
Comment: Thank you for pointing that out, @Adam.  I was trying to lead the OP to recognize what the source of their difficulty is.  After all, most people upon reflecting on the meaning of the difference of SEs, and seeing an example where that is negative, will realize that subtracting one SE from another is inherently problematic.
Answer: I think it's funny, borderline downright evil, to include $SE(\hat{\beta}_1 - \hat{\beta}_2)$ without any further detail in an elementary class. The truth is that with these details only you actually don't have enough information. Cross reference the Welch's T-test for the most commonly accepted solution with non-equal variances. Perhaps you were told that the variances were equal? Or a similar sample size was drawn between either city? Or can we assume the samples are independent?
As for your solution, you probably calculated $SE(\hat{\beta}_1) - SE(\hat{\beta}_2)$. Variance (and SE) is not like multiplication, you can't just "pass it through" the parentheses. Your intuition also should tell you that's wrong. For instance, if the second SE is greater than the first, you would reject the one-sided hypothesis in the wrong direction if significant!
Visualize the normal curves parametrized by their variances and effects when working these kind of problems, update with details, and memorize the Welch's T-test formulation, and apply it here for reference.
Comment: Thanks but what if I had the sample size for each city? e.g., if Vienna n = 207, and Amsterdam n = 195. Would I then be able to "just" use Welch's T-test to calculate whether there is a significant difference between my coefficient?

And also does it matter whether my coefficients are logged or not?
Comment: @RasmusM the $N$ matters because the T-test has an even more complicated formula for the degrees of freedom due to Satterthwaite. Of course, with $N$ so large, you can just use the $Z$ test, so no the $N$ isn't needed in my mind with this large sample. Regarding whether the beta-s are logged, that's a completely different question
Comment: The standard errors are specified and are relatively close, indicating considerations of Welch *etc.* are at best tangential to the question.  Moreover, as it turns out, the DF in this question is immaterial.


How do I Reverse this CSS3 Vertical Dropdown Animation?

Question: I'm designing a vertical dropdown header that opens and closes (using an input check type) when clicked and I can't get this opening animation to reverse for the closing animation because the closing animation is just a scaling trick and not the same as the opening animation.
This is my project: https://codepen.io/anon/pen/xLmbXd?editors=1100
I want to change this closing scaling animation to the actual animation:
@keyframes hideAnimation {
  0% {
    -moz-transform: scaleY(0.5);
    -ms-transform: scaleY(0.5);
    -webkit-transform: scaleY(0.5);
    transform: scaleY(0.5);
  }
  60% {
    -moz-transform: scaleY(0.25);
    -ms-transform: scaleY(0.25);
    -webkit-transform: scaleY(0.25);
    transform: scaleY(0.25);
  }
  80% {
    -moz-transform: scaleY(0);
    -ms-transform: scaleY(0);
    -webkit-transform: scaleY(0);
    transform: scaleY(0);
  }
  100% {
    -moz-transform: scaleY(0);
    -ms-transform: scaleY(0);
    -webkit-transform: scaleY(0);
    transform: scaleY(0);
  }
}

All help is appreciated!
Answer: the issue is that max-height will animate and ease (in and out) based on your 9999px height, so it is going really fast to reach that distance.  Rather you need to use actual heights or set max-height: 165px on line 49 of your pen.
You could also simplify your css to this;

.dropdown {position: relative;}
.dropdown > .dropdown-menu {
  max-height: 0;
  overflow: hidden;
  list-style: none;
  padding: 0;
  margin: 0;
  transform: scaleY(0);
  transform-origin: 50% 0%;
  transition: transform 0s ease-out 0.6s,max-height 0.6s ease-out;
}

.dropdown > .dropdown-menu li a {
  display: block;
  color: #6f6f6f;
  background: #EEE;
  box-shadow: 0 1px 0 white inset, 0 -1px 0 #d5d5d5 inset;
  text-shadow: 0 -1px 0 rgba(255, 255, 255, 0.3);
  padding: 10px 10px;
}
.dropdown > .dropdown-menu li a:hover {
  background: #f6f6f6;
}
.dropdown > input[type="checkbox"] {
  opacity: 0;
  display: block;
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}
.dropdown > input[type="checkbox"]:checked ~ .dropdown-menu {
  max-height: 160px;
  display: block;
  transform: scaleY(1);
  transition: transform 0.6s ease-out,max-height 0.6s ease-out;
}

Dropdown Menu

      <input type="checkbox" />
      First Menu

Home

About Us

Services

Contact

      <input type="checkbox" />
      Second Menu

Home

About Us

Services

Contact

      <input type="checkbox" />
      Third Menu

Home

About Us

Services

Contact

Comment: when it opens to a `max-height` of 900px it effectively stops at the height of it's children, thus following the speed of your `transform: scale()`.  When you close it, it is animating from a size of 900px to 0px in 2s.  that moves fast.
Comment: maybe you should look at just using height or max-height for your transitions.  By using `overflow: hidden;` it effectively covers and hides the child elements.  By setting your `transition` in the 'checked' state it will use that transition, and when you 'uncheck' or close, it will use the unchecked transition.  All I did was add a delay to the `transform: scale(0);` so it does that after it is 'hidden'
Comment: I see. Is there a reason why the dropdown closes faster than it opens when I apply the non-scaling effect to both closing and opening the drop-down?  https://codepen.io/anon/pen/xLmbXd?editors=0100


fractional chromatic number

Question: A fractional chromatic number of a graph $G=(V,E)$, is $min~\sum_{I}y_{I}$, and for every vertex $v\in V$ we have $\sum_{\{I:v \in I\}}y_{I} \ge1$(the condition).
I'm just a little confused on notation.
Let's consider this graph
There are $3$ independent sets: $\{V_{3}\},\{V_{2}\},\{V_{1},V_{4}\}$
So, is a fractional chromatic number of this graph $\le 3$ (sum of all vertices in each independent graph)?
Am I supposed to verify every independent graph if the sum isn't more than $1$, to check the condition ? f.e. for the first set ($\{V_{3}\}$) the sum of all vertices is $1$, and $1 \le 1$, so the condition is met, similarly the next set ($\{V_{4}\}$, and for the last set ($\{V_{1},V_{4}\}$) the sum of all verticies is $\frac{1}{2}+\frac{1}{2}= 1$ and $1 \le 1$ so the condition is met for this and any other set, so indeed, $\chi^{*} \le 3$ ? Is it correct ?
Comment: First note that you have listed the 3 *maximally* independent sets. There are two more independent sets, namely $\{V_1\}$ and $\{V_4\}$.
Answer: There is a variable $y_I$ for each independent set $I$; note that $I$ need not be maximal. Let's call the variables $y_1, y_2, y_3, y_4$, and $y_{14}$. This gives the following optimization problem:
$$\textrm{minimize} \sum_I y_I\\
\begin{align}
\textrm{s.t.}\quad &y_1 + y_{14} &\geq 1\\
& y_2 &\geq 1\\
& y_3 &\geq 1\\
& y_4 + y_{14} &\geq 1\\
\end{align}$$
Clearly $y_2 = y_3 = 1$. For the remaining variables, we'll get the smallest sum when we let $y_{14}$ do all of the work: $y_1=y_4=0$, and $y_{14}=1$. The interpretation of this is that we have no reason to assign different colours to $v_1$ and $v_4$.
So the sum is $3$, and all variables are integer. This corresponds to a $3-$colouring of the graph, which should make sense. (Choose three colours for $\{v_1,v_4\}, v_2$, and $v_3$.) There is no benefit to a fractional colouring for this graph.

fractional chromatic number


Obter parametro da url em requisção GET usando REST

Question: Olá pessoal possuo uma dúvida. Quando eu faço a requisição:

GET /sacar/30

Como o valor "30" não esta vinculado a uma chave, então não da para usar o $_GET['chave'].
A maneira que achei foi fazer isso:
$valor = end(explode("/", $_SERVER['REQUEST_URI']));

Não é algo muito bonito, mas funciona.
Será que há uma forma melhor de fazer isso sem usar um Framework? Em CakePHP faço isso de boa, porém sem ele complicou o meio de campo.
Answer: O PHP não trabalha nativamente com esse formato de URL e com tantos packages por ai que fazem a função de routing, não vejo muito propósito em refazer a roda.
O que você pode fazer é utilizar algum componente que faça o routing da sua aplicação. Que tal usar um microframework já com suporte a routing, como o Silex ou Lumen?
Exemplo simples com Silex:
<?php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{name}', function($name) use($app) {
    return 'Hello '.$app->escape($name);
});

$app->run();

Exemplo com Lumen (arquivo app/Http/routes.php)
$app->get('/', function () {
    return 'Hello World';
});

$app->post('foo/bar', function () {
    return 'Hello World';
});

Comment: Bacana cara, não sei se vale a pena para o momento eu usar, mais curti a ideia e quando precisar de algo mais robusto já sei que existe. A aplicação que estou fazendo é simples e somente tem requisição GET, com uma única URL, é uma provinha pratica para um emprego. Valeu pelas dicas.


"Any meaning to the concept of 'Self Mutual Information?"

Question: **
"Any meaning to the concept of 'Self Mutual Information?"
**
A blog post entitled, "Entropy in machine learning" dated May 6, 2019 (https://amethix.com/entropy-in-machine-learning/) gave a very good explanation and summary of the concepts of Mutual Information, KL Divergence and their relationships to Entropy. It also contained many informative references as well as providing useful Python code supporting their explanations. But portions of the results from one set of code are puzzling to me.  The code calculated the mutual information between the features for a dataset describing cancer. It created an "MI" (Mutual Information) matrix by interating across all the features, thus covering all possible combinations of the features in the data set (see screenshot below for MI matrix resulting from the code).

I understand that the reciprocal pairs of features that are mirrored across the diagonal of the matrix, like "Insulin - Resistin" and "Resistin - Insulin", are redundant but, do the MI of pairs of "self-feature pairs" like "Insulin - Insulin" or "Resistin - Resistin" provide any useful information?
I look forward to any feedback or insights.
Answer: What you called Self Mutual Information is actually Entropy. Indeed entropy can be also called Self Information (wouldn't use "mutual" anymore): https://en.wikipedia.org/wiki/Information_content
$$
\mbox{MI}(X,X) = H(X) - H(X|X) = H(X) - \sum_x \sum_x p(x,x) \log{p(x|x)} = H(X) - \sum_x \sum_x p(x,x) \log{1} = H(X)
$$
Thus, on the diagonal of that table you will find the entropy of each variable:

High entropy: values are uniformly distributed
0 entropy: one single constant value
Comment: Perfect answer and reference - thanks!

“Any meaning to the concept of ‘Self Mutual Information?”


Software to test accuracy in vocally repeating notes after listening and identifying

Question: Background
Listening and identifying a note is a difficult task for many of us who are beginners to singing. Even when we singing, we are often not certain as to how close we were to the actual note. Experienced singers, on the other hand, can very easily tell, whether one has hit the right note, or not, and in the latter case, which was the note one actually sang.
I have been taking vocals classes, for some time, and my teacher stressed on the fact that it is very important to accurately register a note before trying to sing it. She advised me to do this exercise to improve - randomly play notes, one by one, on a keyboard, identify it and repeat it vocally.
What I need
I know there would be keyboard/piano softwares available, but I was wondering if there is something advanced available. I would like a software which could take my voice input and compare it with the actual sound of the note produced by it and tell me how accurate it was.
Note - I checked this question - Singing software. However I am not sure if a karaoke software is exactly what I need, because I am stressing on individual notes here, rather than a complete song. So asking a new question...
Comment: A simple tuner wouldn't work ?
Comment: Welcome to Music SE! Per the [FAQ], questions eliciting recommendations are off-topic here. If you have questions about a specific software, feel free to ask.
Answer: I've never tried it, but this app seems pretty popular for audio=>note analysis
http://www.seventhstring.com/xscribe/screenshots.html
Of course, the point of hitting a key and singing to it...
 is to tune your ear to recognizing the sound as well as your voice creating it.
good luck to ya.


Error:Circular reference between projects

Question: I am importing a Eclipse Project to Android Studio. But it occur error like below. How do resolve this type of error
Error:Circular reference between projects: :app -> :app

below is settings.gradle file
include ':app'

here is build.gradle file
apply plugin: 'com.android.application'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

android {
    buildToolsVersion "23.0.1"
    compileSdkVersion 23
    defaultConfig {
        targetSdkVersion 22
    }
    productFlavors {
    }
    sourceSets {
        main {
            manifest.srcFile 'app/src/main/AndroidManifest.xml'
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

Answer: Sol1:same name(:app) will not add in Android Studio.
sol2:if the :app name is not there in your Project,
Create new Project and import again, it will work
sol3:in settings.gradle file
      include ':app',':core'
      add like this.


Variável soma está acumulando o valor ao invés de iniciar em zero

Question: O algoritmo é para o cliente realizar pedidos em uma lanchonete, ele pede o nome do cliente - 1, em seguida a quantidade do pedido e depois o algoritmo pede para digitar o número do pedido, quando o pedido encerra o programa da o valor total do pedido e passa para próximo cliente - 2, quando o cliente – 2 encerra o pedido o algoritmo pega valor total do cliente 1 soma com do cliente 2 e da valor total dessa soma para cliente 2 em vez dele só da o valor puro para cliente 2
print('-' * 20)
print('{:^20}'.format('LANCHONETE'))
print('-' * 20)
print('''Cardapio
[1] - Esfirra R$ 3,50
[2] - Coxinha R$ 3,50
[3] - Hotdog R$ 4,00
[4] - Hamburgue R$ 7,00
[5] - Suco R$ 1,50''')
valor = soma = 0
while True:
    print('-' * 20)
    nome = str(input('Digite seu nome: '))
    quant = int(input('Digite a quantidade do pedido: '))
    print('-' * 20)
    for c in range(1, quant + 1):
        pedido = int(input(f'Digite o {c}° pedido: '))
        if pedido >= 1 and pedido <= 2:
            valor = 3.50
        if pedido == 3:
            valor = 4
        if pedido == 4:
            valor = 7
        if pedido == 5:
            valor = 1.50
        soma += valor
    print(f'O valor total do pedido é R${soma:.2f}')

Answer: Isso porque você iniciou o valor de soma fora do laço de repetição, então o valor irá se acumular durante as iterações. O que você precisa fazer é iniciar a soma em zero para cada novo cliente, dentro do laço de repetição.
Ao invés de:
soma = 0
while True:
    ...

Faça:
while True:
    soma = 0
    ...

Comment: Deu certo obrigado!
Comment: Não esqueça de avaliar a resposta


Optimal combination of biased samplers

Question: Suppose we are interested in the mean $\mu$ of a random variable $X$ but the only way to sample it is from known biased distributions $p_{\lambda}(x)$, such that $\left<{X}\right>_{\lambda} =\mu_{\lambda} \neq \mu$. However, the total average of all biased distributions is unbiased, i.e. $\left<\left<{X}\right>_{\lambda} \right> = \mu$. Suppose also that each biased distribution comes with an equal initial cost $C_0$ (expensive) and an extra cost for each subsample $C_1$ (cheap). Given an allotment of a total cost $C$, what is the optimal way to partition the resources between different distributions and within each of them to get the most precise estimate for this cost?
A simple discrete example of this problem would be having access to $N$ unfair coins, half of which only give tails and half of which only give heads, meaning that they are fair on average. If we have access to \$100 and the initial toss costs \$10, while subsequent tosses cost \$0.1, clearly the best strategy is to buy 10 coins and toss each one of them once. On the other hand, if we know that all coins are fair, the best strategy would be to toss a single coin as many times as we can. How can we formalise this problem mathematically in less obvious cases and in the continuous case? Is this possible?
Comment: Do you know the distribution of $\lambda$'s? If yes, maybe this can be thought as a Gaussian mixture?
Comment: In your example you know the distribution of the coins, I guess the general question you ask doesn't assume this? What do you know about the distribution?
Comment: I suppose that it would be asking too much to do that without any information. Let's suppose that one can get a reliable esimate of the lower moments of the distributions, e.g. one knows $\mu_{\lambda}$ and ${\sigma}^2_{\lambda}$ for each $\lambda$.
Comment: I don't necessarily know the distribution of the $\lambda$'s but I can sample from it, if this makes sense
Answer: I think I finally figured it out. First we define the parametric estimator:
$$\hat{\mu}_{M,N} = \frac{1}{M}\sum_i^M \hat{\mu}_{i;N}$$
Where $M$ is the total number of samplers we use and $N$ is the number of samples we draw from each one of them. Then we are interested in the variance of this parametric estimator:
$$\text{Var}(\hat{\mu}_{M,N}) = \frac{1}{M}\text{Var}(\mu_N) \equiv \frac{1}{M} \left(\left<\left<\mu^2\right>_{\lambda;N}\right> - \left<\left<\mu\right>_{\lambda;N}\right>^2\right)\\
= \frac{1}{M} \left(\left<\left<\mu^2\right>_{\lambda;N}\right> - \left<\left<\mu\right>^2_{\lambda;N}\right> + \left<\left<\mu\right>^2_{\lambda;N}\right> - \left<\left<\mu\right>_{\lambda;N}\right>^2\right)\\
=\frac{1}{M}\left( \left<\frac{\text{Var}_{\lambda}(X)^2}{N}\right> + \text{Var}(\mu_{\lambda})\right)$$
where $\left<\text{Var}_{\lambda}(X)^2\right>$ is the intra-sampler variance (which I am going to call $\sigma^2_{intra}$) and $\text{Var}(\mu_{\lambda})$ is the inter-sampler variance (which I am going to call $\sigma^2_{inter}$).
The cost function was defined as (note that here we include the first draw into the second term, unlike in the original question):
$$C = C_0 M + C_1 M N$$
Now we do constrained minimisation of the variance so that the cost is constant and $M$ and $N$ are bigger than one. I will skip the steps, but finally one obtains:
$$N_{opt} = 1 \vee \left( \left\lfloor\sqrt{\frac{\sigma^2_{intra} C_0}{\sigma^2_{inter} C_1}}\right\rfloor \wedge \left\lfloor\frac{C-C_0}{C_1}\right\rfloor \right)$$
In our coin example, if all coins are double sided, such that $\sigma^2_{intra}=0$, then the above equation reduces to $N_{opt}=1$, and in the other example $\sigma^2_{inter}=0$, which means that $N_{opt}$ obtains its maximum possible value given the constraint, as expected.

Optimal combination of biased samplers


How to encode U+FFFD in order to do a replace?

Question: In a text file I obtain from a source I do not control, and which text content changes periodically and I have to compensate by re-downloading and processing, the char U+FFFD occurs frequently (but not always) when what is meant is the single quote or ' character.
How can I encode a replace operation so as to replace the U+FFFD with an actual single-quote in C#. I envision something like:
string s = s.Replace("U+FFFD", "'");

Clearly I should use the overload that deals with char, but I am not sure how to encode the U+FFFD, or for that matter, the single quote!
Comment: Incidentally, this is a hallmark of bad charset wrangling (possibly on the server side).  [U+FFFD is the `REPLACEMENT CHARACTER`](http://www.fileformat.info/info/unicode/char/fffd/index.htm).
Comment: @Pacerier, if you read the whole question, the replacement character is coming across from a source not under my control -- I just have to deal with it when it arrives.  *I* am not using it.  I am trying to *deal* with it.
Comment: That is likely true, but nothing I can do anything about.  All I do is download it as a text file, and have to deal with what it looks like.
Comment: you're replacing string "U+FFFD" just like any other string e.g "foo"
Comment: You are using the char [`U+FFFD` for the wrong purpose](http://stackoverflow.com/a/28168981/632951). The "replacement" char is not meant to do these kind of replacements.
Comment: Just out of curiosity: have to tried to find out the actual binary representation of the single quote characters in the source file? I have a feeling that you (implicitly) interpret the source file as UTF-8 (or similar) while it's actually in a different character encoding. For instance, `\xB4` in ISO-8859-1 also looks like a single quote. I don't know how critical this process is, but relying on error recovery behaviour (in this case, decoding bytes that don't fit in UTF sequences by `U+FFFD`) doesn't sound like a good solution to me.
Answer: string s = s.Replace('\uFFFD','\'');

Comment: Thanks for your quick response!  Saved me a lot of drudge work this morning!


Finding the number of non-decreasing traversals of a grid

Question: I'm trying to find the number of non-decreasing traversals of an $n$ by $m$ grid ($n$ rows, $m$ columns) with $n<m$ from the bottom-left corner square to the top-right corner square. Here is an example for $m=8, n=4$:

Equivalently, I'm trying to find the number of non-decreasing (i.e. $k_1 = 1 \leq k_2 \leq ... \leq k_m = n$) sequences where $k_i \leq n$ and every number in $\{1, ..., n\}$ appears at least once.
This initially looked like a stars-and-bars problem with $\sum k_i = M$ but this construction doesn't quite work (I couldn't find a way to encode the "every number in $\{1, ..., n\}$ appears at least once" criterion).
Comment: Duplicate:  https://math.stackexchange.com/questions/2825417/paths-from-one-corner-to-another
Comment: @DavidG.Stork The link you sent considers rook moves and an $n$x$n$ grid, neither of which are the case here (the sequence interpretation makes this more clear).
Comment: Equivalent problem: https://math.stackexchange.com/questions/3966189/combinatorics-6-digits-decreasing-code/3966196#3966196
Comment: Also -- you can't always have $k_1,\dots, k_m$ covering every number $1,\dots,n$. What if $m<n$? A general method for this problem shouldn't need you to turn your head sideways.
Comment: The non-decreasing traversals of the grid problem *is* identical to the rook move problem as stated (where you cannot travers a line twice).  Check.
Comment: @DavidG.Stork As I understand it, this problem only allows for one-step bishop or one-step horizontal rook moves. I have added a picture for $m=8, n=4$ to make this more clear.
Comment: @jlammy Correct, this only makes sense for $m \leq n$; I've edited the question to reflect this
Comment: I'm confused about the diagonal moves. The nondecreasing sequence: $(a1,b1,b2,c2,\dots,h4)$ is different from $(a1,b1,c1,c2,\dots,h4)$, but both count for the same diagonal move. I assume they are supposed to be different?
Comment: The only allowed moves are diagonal move and right move? Then, you always have exactly $m-1$ diagonal moves (otherwise you will either not reach the top layer or move beyond it) and the rest of $n-1$ moves (you don't move anywhere from the last square) are right moves, therefore the answer is $\binom{n-1}{m-1}$.
Answer: Replace each diagonal step by an up-step, and you get a path by rook moves from $\langle 1,1\rangle$ to $\langle n-m+1,m\rangle$. This path consists of $m-1$ up-steps and $n-m$ right-steps, for a total of $n-1$ steps. The steps can be taken in any order, and the path is completely determined when we know which steps are up-steps, so there are $\binom{n-1}{m-1}$ possible paths. On your $8\times 4$ board, for instance, there are $\binom73=35$ such paths.
Note that the transformation from one of your paths to a rook path is reversible simply by replacing each up-step by a diagonal step, so it really does define a bijection between your paths on an $n\times m$ board on and rook paths on an $(n-m+1)\times m$ board.


How to know the real amount of RAM in a VPS

Question: I've been told that a VPS's RAM can be manipulated (oversold) by VPS providers. I know I can always buy a VPS from a reputable company, but, really, how can one make sure the allocated memory is the one we have paid for. I am particularly interested to know this in respect with systems virtualized by Xen.
Comment: I'm not sure this is a meaningful question.  Do you mean "*how can I be sure that my VPS provider hasn't oversold the memory in their hosting server, and that I can call on all my allocated memory at any time and guarantee to get it*"?
Answer: This is not a technical problem.  It is a problem of business relationships, and as such not (to my mind) particularly on-topic for ServerFault.
From inside a host you generally have no way of seeing what the hosting server is doing - that's the whole point of a VPS.  Even if the hosting technology provided such a conduit, they control the hosting server so you cannot guarantee that the data that come down that conduit are valid.
If you want to be sure that your VPS provider isn't overselling core memory, then you need them to contractually guarantee it, ensuring that the contract gives you some way of validating that they're meeting their contractual obligations.  In practice, if being sure that you control the underlying resources is that important to you, it may well be cheaper to colocate your own physical box.


Editing fields presented in a view

Question: I am looking for a module that allows fields to be edited that are presented<,
preferably by a view. Basically what I am trying to acheive is as follows:

I have a content type as Meter Readings
Meter reading prepopulates by entity reference_prepopulate to a parent project
  ie.Building.
Idea is that a building has a meter say Electricity meter that records
  electricity readings(by Meter Readings Content type) that is presented in a view

I want to add meter readings in the view itself and not let the user to go to another screen,
Any  module to do this? Any references would be of great help.
Comment: are you creating one node per meter reading or adding additional mete reading to the existing content?
Comment: @MohammedShameem adding meter readings on a monthly basis like for jan -december , fields should be added as per user choice like add on more entry type thing, adding readings to existing content
Comment: Let me know if the method I explained works for you.
Answer: One option is to use the field collection. and field collection views
This would automatically create the add edit and delete links to those who have permission to do so both in views as well as node view. It will popup a nice box to make the changes or add items.
I followed the following steps.

Added a content type called Meter Reading
Added a field collection called Reading with widget embedded made its value as unlimited.
Add a field to the field collection.
Added a content of the type Meter Reading without entering anything in the reading.

Now when you visit the newly created content you would get something like below image.

Here you will notice the add link this and when you click that you would get a popup something like in the below screenshot.

In the popup you can enter the readings.
This is what I get after entering couple of readings.

By the way you can add field collection with in field collection and it would work the same. So in your case instead creating a content type called Meter reading you can create a field collection called meter reading and then add a field collection with in that called readings and with in that add a field called Reading.
So your entire page would look like this
Answer: am looking around D7 options myself- sounds like editable fields would suit your need to - shame it is only alpha - http://drupal.org/project/editablefields
Comment: Editable fields doesnt work with field collections

Editing fields presented in a view


Use $X^{-1}Y$ instead of $(^T)^{−1}^T Y$ to calculate $\beta$ when $X$ is already a square matrix in the least square problem

Question: In the least squares problem $X\beta = Y$, the solution is $\hat{\beta} = (^T)^{−1}^TY$. I learned that two facts:

$^T$ is square matrix so that the definition of matrix inversion is satisfied.
$(^T)^{-1}$ is related to the variance of the estimated coefficient

But when $X$ is already a square matrix, can I just use this estimator $X^{-1}Y$ to calculate the parameter coefficient $\beta$?
Thanks to @Whuber So the short answer is if $X$ is invertible, then the basic matrix operations work.
Assume $X$ is a 2x2 matrix, how is the effect of the correlation between column 1 (X[,1]) and column 2 (X[,2]) affect the results $X^{-1}Y$?
I guess as long as the correlation is not too severe, the computation of $X^{-1}Y$ is OKay. But when the correlation is too close to 1, the $X^{-1}Y$ result depends on the numerical tolerance in the methods like QR or Cholesky decomposition in a specific softare. Is the thought process correct?
Comment: Hint: if you assume $X$ is invertible (which you seem to do), you can use basic matrix operations to simplify the formula for $\hat\beta.$
Comment: What does your edit mean?  You have agreed the two expressions are the same, so what could possibly "affect the results"?
Comment: @whuber Thanks much for the quick reply. So the short answer is if $X$ is invertible, then the basic matrix operations work. I added a follow-up question in the post.
Comment: @whuber good point, I updated the questions.
Comment: If $X$ is a square matrix, the resulting estimator of $\beta$ is not going to be very good as this is equivalent to estimating the mean of a vector by this vector.
Comment: @Xi'an is it possible to have an example to illustrate the idea? I am not clear about this point
Answer: If $X$ is invertible then $X^{-1} = (X^\top X)^{-1} X^\top$. You can check this by the definition:

$(X^\top X)^{-1} X^\top X = I$, so it is the left-inverse of $X$.
$X(X^\top X)^{-1} X^\top$ is the projection onto the column space of $X$, which is $I$ because $X$ is full rank. So it is a right-inverse as well.
Alternatively, you might recall that for full-rank matricies, every left-inverse is also a genuine inverse, so it sufficed to stop after the first step.


Convexity and solution of a quadratic program

Question: Consider the following quadratic program
$$\begin{array}{ll} \underset{x_1,x_2,x_3}{\text{minimize}} & x_1 x_2 +\frac{1}{2} x_1^2 + \frac32 x_2^2 + 2 x_3^2 + 2 x_1 + x_2 + 3x_3\\ \text{subject to} & x_1 + x_2 + x_3 = 1\\ & x_1 - x_2=0\\ & x_1, x_2, x_3 \geq 0\end{array}$$
First, I want to check if the objective function is convex. I did this by finding the eigenvalues and they were all positive so this is positive definite and is strictly convex.
Now, I am trying to show that $x^*=(\frac12, \frac12, 0)$ is an optimal solution to this problem by finding vectors $y$ and $s$ that satisfy the optimality conditions jointly with $x^*$. I think this should be done via Primal-Dual Interior-Point Method but I am not much familiar with this approach and am pretty confused.
Thanks for your help in advance!
Comment: What have you tried?
Comment: Do you mean $x_1x_2+ x_1^2+ (3/2x_2^2)+ 2x_2^2+ 2x_3^2+ 2x_1+ x_2+3x_1$ or $(x_1x_2+ x_1^2+ 3)/(2x_2^2+ 2x_2^2+ 2x_3^2+ 2x_1+ x_2+3x_1)$?  And should that last "$3x_1$" be "$3x_3$"?  If not, $2x_1+ 3x_1= 5x_1$!
Comment: @IgorRivin I don't know how to approach it tbh.
Comment: Yes, I just edited it, thanks for letting me know. @user247327
Answer: Well, first, from $x_1- x_2= 0$, $x_1= x_2$ of course so we can write the expression to be minimized as
$x_1^2+ x_1^2+ \frac{3}{2}x_1^2+ 2x_3^2+ 2x_1+ x_1+ 3x_3= \frac{7}{2}x_1^2+ 3x_1+ 3x_3$
And from $x_1+ x_2+ x_3= 2x_1+ x_3= 1$, $x_3= 1- 2x_1$
So we can write the expression as a function, $\frac{7}{2}x_1^2+ 3x_1+ 3- 6x_1= \frac{7}{2}x_1^2- 3x_1+ 3$, of the single variable $x_1$!
That will have an extremum where $7x_1- 3= 0$ so at $x_1= \frac{3}{7}$, $x_2= x_1= \frac{3}{7}$, and $x_3= 1- 2x_1= 1- \frac{6}{7}= \frac{1}{7}?$.
The second derivative is 7, positive, so that is a minimum.
(We could also have observed that the function in $x_1$ is a parabola opening upward.)
Comment: Please do not post duplicate answers.
Comment: @XanderHenderson Any idea on how to do it?
Answer: Let $f$ be the cost function and note that ${\partial f(x^*) \over \partial x} = (3,3,3)$. Let $g(x) = x_1+x_2+x_3-1$ and note that $g(x^*) = 0$ and ${\partial f(x^*) \over \partial x}  + \lambda {\partial g(x^*) \over \partial x} = 0$ with $\lambda = -1$. Hence we see that $x^*$ solves the convex problem
$\min \{ f(x) | g(x) = 0 \}$ and hence is a solution for the more restrictive problem in the question.


Rails Spree OffsitePayments Integrations work flow

Question: I'm a newbie in Spree, ActiveMerchant and payment flow...
That said, I am trying to integrate an OffsitePayment to spree, I created a custom gateway, added it to spree's payment methods on spree.rb config file and set it up on the admin interface, such that it is showing as an option to the buyer, and the purchase method is getting called. But what to actually do inside the purchase method? I suppose (and expect) that the integration helper has some properties I need to fill in (as I see in the docs for it), but once it is filled, how do I actually "submit" the payment? I don't even know if I should be looking for something spree related or OffsitePayments, or ActiveMerchant related... a bit lost.
class Spree::Gateway::PagSeguro < Spree::Gateway
    def provider_class
        OffsitePayments::Integrations::PagSeguro
    end

    def provider
        provider_class::Helper
    end

    def auto_capture?
        true
    end

    def purchase(amount, paym_source, gateway_options={})
        # What to do here?
        ActiveMerchant::Billing::Response.new(true, 'success', {}, {})
    end

end

Answer: Your purchase method needs to do whatever is required for the gateway to take the money, and then return the appropriate ActiveMerchant::Billing::Response based on the status so that Spree knows how to process it.