Como organizar o código sem perder desempenho?

Question: Usar funções no lugar de colocar o código diretamente influencia muito no desempenho de um programa ? Por exemplo:
void minhafuncao(){
  printf("Funcao");
}
main(){
  minhafuncao();
}

no lugar de
main(){
  printf("Funcao");
}

Eu sei que macros tem uma performance maior do que funções, mas até o momento eu só vi macros com operações aritméticas. Tem como colocar blocos de código com vários comandos em uma macro ? Ou, se não, alguma outra forma de fazer que tenha um melhor desempenho ?
Comment: Ter o código numa função separada ou directamente dentro doutra função não influencia o desempenho de forma observável.
Comment: Na verdade, pmg, depende do caso. Funções que somente fazem, por exemplo, uma ou duas operações aritméticas e que são chamadas em loop aos montes podem consumir um percentual maior e notável de tempo.
Answer:
Usar funções no lugar de colocar o código diretamente influencia muito no desempenho de um programa ?

Não! Chamadas de função, ainda mais em C, são bastante rápidas. Tem muitas outras coisas que vão deixar o seu programa bem mais lento do que uma chamada de função:

Como você pode ver nessa imagem, um único acesso ao disco rígido gasta um tempo equivalente a milhões de chamadas de função. Um acesso a um computador remoto nem se fala então.
E mesmo se as chamadas de função fossem lentas, (o que elas não são) o benefício que elas oferecem em termos de abstração e organização do código é muito grande e não valeria a pena abrir mão disso em nome de "performance". Quando se trata de otimizar um programa, o que faz diferença é a performance dos "gargalos": se você tem um loop interno consumindo 90% do seu tempo de execução, dobrar a velocidade dele vai ter um efeito significativo. Por outro lado, se um trecho do seu código só executa umas centenas de vezes, mesmo se você torná-lo instantâneo você não vai economizar mais do que alguns milissegundos. Por isso, sempre meça a performance do seu programa antes de começar a otimizá-lo! É difícil prever de antemão quais partes do seu programa são os verdadeiros gargalos e qualquer otimização fora do gargalo é uma perda de tempo que só serve para deixar seu código mais complicados. Sem contar que algumas otimizações podem acabar deixando seu programa mais lento do que antes. A única forma de se proteger disso é sempre medir antes e depois de qualquer otimização que você fizer.

sei que macros tem uma performance maior do que funções

Nem sempre! Macros são uma forma manual de inlining e o uso excessivo dessa técnica aumenta bastante o tamanho do executável. Além de desperdiçar memória, isso pode aumentar a taxa de "cache miss", o que é bastante prejudicial à performance (um acesso à memória RAM exige centenas de ciclos).
Além disso, o verdadeiro ganho de performance do inlining são vem do custo da chamada de função mas sim das oportunidades que o otimizador passa a ter para especializar o código da subrotina para o local da chamada (alocação melhor de registradores, remoção de código morto, etc). Isso nem sempre acaba valendo a pena e por isso recomendo confiar no seu compilador ao invés de sair fazendo inlining na mão.
Comment: Pior, que as vezes esses "gargalos" no código as vezes se tornam necessários quando o trecho sem tal gargalo se mostra uma tarefa demasiadamente complicada. Ex: As vezes se torna melhor para o entendimento percorrer um array todo - com for e um if interno - do que tentar achar expressões complicadíssimas no for e satisfazer as mesmas condições sem percorrer todo array.
Comment: É verdade, Rafael, e é sempre importante levar em conta o custo de deixar o programa mais complicado. Mas pior ainda é fazer artimanhas de otimização em algo que nem é um gargalo :)
Answer: À princípio influencia a performance. Mas pode não influenciar por uma série de fatores.
Uma chamada à uma função, além das instruções de desvio e retorno do fluxo do programa para um outro endereço, pode existir a cópia de dos parâmetros, seja de memória para memória, seja através de registradores. Então tem custo, claro.
Se a função for simples e dependendo das opções usadas para compilar o programa a função será otimizada através de da chamada inline expansion. Ou seja, se for vantajoso, o compilador elimina a chamada da função e coloca o código dela no local onde era feita a chamada. Note que o compilador sabe que há casos que usar esta técnica produzirá resultado contrário.
Isso faz o mesmo que a macro faria mas de forma muito melhor. No início os compiladores não eram capazes de fazer isto e as macros traziam esta vantagem, apesar das desvantagens que este recurso possui. Hoje não existe mais grandes motivos para o uso da macro, apenas situações muito específicas exigem seu uso em código C e menos ainda em código C++ que tem ainda mais recursos melhores.
É possível fazer macros mais complexas que simples operações aritméticas mas não é recomendado o seu uso nos dias de hoje. Mas como já disse antes macros não entendem o contexto onde estão sendo executadas, isto pode causar problemas se cuidados não forem tomados na sua criação e/ou uso.
Exemplo:
#define exemplo() \
  do { \
    faz alguma coisa aqui \
  } while (0)

Hoje não tem mais razão para fazer isso. Hoje até mesmo em C, na maior parte do tempo o programador deve se preocupar com a legibilidade do código e não com a performance. O compilador fará o código ser o mais rápido possível na imensa maioria das situações que o código for bem escrito. Não é necessário ficar procurando as melhores formas de escrever pensando primariamente na performance. Quando a performance não for suficiente, aí deve-se pensar no que fazer para melhorar aquele trecho. raramente a macro será a solução para este caso.
Em quase 100% dos casos o compilador saberá se uma função deve ser "linearizada" para obter melhor desempenho. Se ele não fizer isto é porque ele entende que não haverá ganho e ele acerta mais que os programadores costumam fazer.
Me parece que alguns compiladores podem forçar fazer o inline, através de flag específica, mesmo quando não é recomendado. Quase sempre quando o programador quer ser mais esperto que o compilador, ele erra.
O seu exemplo é certo que será "linearizada", a não ser que o compilador seja instruído a não fazer. Mas uma pequena mudança que ainda deixará a função curta provavelmente impedirá que isto aconteça:
void minhafuncao(){
  for (int i = 0; i < 100; i++) printf("Funcao");
}

Provavelmente o tempo gasto dentro da função é grande o suficiente para o compilador entender que não haverá ganho. Mas outros fatores podem ser levados em consideração para fazer a avaliação.
Não vou entrar em detalhes do que é feito ou não porque não conheço profundamente e depende de implementação, ou seja, pode mudar em cada versão ou marca do compilador.
Um teste pode ser aplicado para ver se há diferença ou não:
#include <stdio.h>
#include <time.h>

int minhafuncao(){
    int x = 0;
    x++;
    return x;
}

int main () {
    clock_t begin, end;
    double time_spent;
    int x;
    begin = clock();
    for (int i = 0; i < 300000000; i++);
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Tempo gasto %.2lf segundos.\n", time_spent);
    begin = clock();
    for (int i = 0; i < 300000000; i++) minhafuncao();
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Tempo gasto %.2lf segundos.\n", time_spent);
    begin = clock();
    for (int i = 0; i < 300000000; i++) x++;
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Tempo gasto %.2lf segundos.\n", time_spent);
    x++;
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
O ideone ou outro site do tipo não é o melhor ambiente para fazer o teste mas dá para ter uma ideia. Recomendo fazer o seu teste. O resultado pode ser diferente em condições diferentes.
Outro teste sem retornar um valor. Note como muda. Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Comment: Exatamente, é o que eu falei, tudo vai depender de como compila.
Comment: Nem acho que ele seja muito bom, só mostra como é fácil ver que depende até em coisas simples. Em complexas, o depende fica enorme. Tem outras otimizações que podem ocorrer aí. Pode ter até mesmo [*dead code elimination*](http://en.wikipedia.org/wiki/Dead_code_elimination). O que pode justificar tempos iguais. Não é o caso mas pode ser que uma otimização propicie outra, como um [*loop unrolling*](http://en.wikipedia.org/wiki/Loop_unrolling) ou [*constant folding*](http://en.wikipedia.org/wiki/Constant_folding), só para citar algumas otimizações óbvias.
Comment: gcc -Wall -ansi -O velocidade.c -o velocidade -> Na execução:
Tempo gasto 0.10 segundos.
Tempo gasto 0.10 segundos.
Tempo gasto 0.10 segundos.  Sem a flag -O (0.7, 0.83, 0.7)
Comment: No código por que minhafunção retorna um valor? Sem o retorno o tempo sem -O também é parecido.
Comment: Pra mostrar que qualquer coisa pode fazer diferença. Que não dá para falar de velocidade de coisas hipotéticas. Um pequeno detalhe muda tudo. Certamente ele quer saber que deve usar qualquer função ou não. E isto **depende**, mesmo uma simples mudança **pode** produzir um resultado diferente. E falo mais sobre isto aqui: http://pt.stackoverflow.com/a/56838/101 é bobagem generalizar comparação de performance, ela só pode ser feito de forma muito específica, em casos reais.
Comment: Muito bom código, realmente sempre ouvi falar de performance, e isso fica sucinto nesse pequeno código. Vou testar umas flags, outros compiladores, alguns statics, inlines etc. Mutio bom exemplo!
Comment: Vamos [continuar esta discussão no bate-papo](http://chat.stackexchange.com/rooms/22718/discussion-between-rafael-bluhm-and-bigown).
Comment: Ambas as respostas foram bastante esclarecedoras.


Custom PreAuthorize annotation in Spring Security

Question: I'm trying to understand Spring Security and I'm wondering about creating my own annotations with authorities I've created. I've got something like this:
@PreAuthorize("hasAuthority('STANDARD')")
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface StandardRole {
}

@PreAuthorize("hasAuthority('ADMIN')")
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AdminRole {
}

and actually it looks like:
@AdminRole
@StandardRole
@GetMapping(path = "user", produces = "application/json")
public ResponseEntity<User> getUser(@RequestParam String login) {
   ...
}

but only first annotation works, second one is ommited. I want to do something like @AllowRoles() annotation, for example @Allow({UserType.ADMIN, UserType.STANDARD}) or @Allow({UserType.ADMIN}).
How can I do this? Thanks.
Answer: It's a pity to force the door open.
I used jsr250 annotation in my SecurityConfig class:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 ...

@RolesAllowed in my controller:
@RolesAllowed({UserType.TYPE_1, UserType.TYPE_2})
@GetMapping(path = "user", produces = "application/json")
public ResponseEntity<User> getUser() {

Finally, at my CustomDetails implementing UserDetails:
    private static final String PREFIX = "ROLE_";

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(
                new SimpleGrantedAuthority(
                        PREFIX + user.getUserType()));
    }

I forgot about "ROLE_" prefix. Code is much cleaner.
Comment: Upvoted! Thanks for the right suggestion of using the `@RolesAllowed` annotation. Regarding the ROLE_ prefix, one can also decide to strip it from the authorities by overriding ` GrantedAuthorityDefaults` bean desribed here: https://stackoverflow.com/a/43964633/913093


File.cpp.o: multiple definition of OpenPose flags CMakeFiles/.../main.cpp.o: first defined here

Question: I have found this issue many times and I went through dozens of found answers here (on stackoverflow) and elsewhere (google search), but nothing worked. At first I've had all the code in one file and everything worked correctly. But now, I've needed to split the code into multiple files, create some classes and so on, so I've created following files:
stdafx.h
#pragma once

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>

// opencv
#include <opencv2/opencv.hpp>

// openpose
#include <openpose/flags.hpp>
#include <openpose/headers.hpp>

#include "driver-keypoints.h"

driver-keypoints.h
#pragma once

class DriverKeypoints
{
public:
    DriverKeypoints();

    static const int NOSE = 0;
    static const int NECK = 1;
    static const int R_SHOULDER = 2;
    static const int R_ELBOW = 3;
    static const int R_WRIST = 4;
    static const int L_SHOULDER = 5;
    static const int L_ELBOW = 6;
    static const int L_WRIST = 7;
    static const int MID_HIP = 8;
    static const int R_HIP = 9;
    static const int L_HIP = 12;

    int get_video_keypoints();

protected:
    void save_keypoints(const std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>>& datums_ptr,
                        std::string out_file, bool is_first);
    void configure_wrapper(op::Wrapper& op_wrapper);
};

driver-keypoints.cpp
#include "stdafx.h"

// Custom OpenPose flags
// TODO: here should not have been an absolute path
DEFINE_string(output_path, "/path/to/output/openpose/", "Path to save required keypoints.");
DEFINE_bool(no_display, false, "Enable to disable the visual display.");

void DriverKeypoints::save_keypoints(const std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>> &datums_ptr,
                                     std::string out_file, bool is_first) {
}

void DriverKeypoints::configure_wrapper(op::Wrapper &op_wrapper) {
}

int DriverKeypoints::get_video_keypoints() {
}

main.cpp
#include "stdafx.h"

int main(int argc, char** argv) {
  FLAGS_disable_multi_thread = true;

  FLAGS_net_resolution = "-1x192";
  FLAGS_number_people_max = 0;

  // Parsing command line flags
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  // run parse keypoints from video
  DriverKeypoints driver_keypoints;
  return driver_keypoints.get_video_keypoints();
}

CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(my_project)

set(CMAKE_CXX_STANDARD 14)

find_package(OpenCV REQUIRED)
find_package(CUDA REQUIRED)
find_package(Caffe REQUIRED)
find_package(gflags REQUIRED)

include_directories(
        OpenCV_INCLUDE_DIRS
        ${CUDA_INCLUDE_DIRS}
        ${CAFFE_INCLUDE_DIRS}
)

# TODO: here should not have been an absolute path
include(/home/path_to_openpose/openpose/cmake/Utils.cmake)

set(SOURCE_FILES main.cpp driver-keypoints.cpp)
add_executable(anomalies_analysis ${SOURCE_FILES})

target_link_libraries(
        anomalies_analysis
        ${OpenCV_LIBS}
        ${CUDA_LIBRARIES}
        openpose
        gflags
)

And I am getting following errors:
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.data+0x0): multiple definition of `fLI::FLAGS_logging_level'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.data+0x0): first defined here
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.data+0x4): multiple definition of `fLI::FLAGS_nologging_level'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.data+0x4): first defined here
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.bss+0x0): multiple definition of `fLB::FLAGS_disable_multi_thread'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.bss+0x0): first defined here
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.bss+0x1): multiple definition of `fLB::FLAGS_nodisable_multi_thread'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.bss+0x1): first defined here
...

There are many answers of type:

you should declare something as inline (but it is not possible)
you should define something only once (but I think I am doing it, it looks like the problem is in including openpose/flags.hpp, but I have #pragma once in stdafx.h, so is the problem here?)

Could you please help me? I am not experienced enough in C/C++ programming. Thank you very much.
Answer: I know this is late, but I had a similar issue and this thread popped up.
My code was working fine in one file, and then gave me similar errors to yours when I created a header file and #included it in multiple places. In my case, I had DEFINE_type() in my header file instead of DECLARE_type(), which caused issues since the variables were being defined more than once. I would double check that you are using the DECLARE_ methods instead of the DEFINE_ methods in your header file.


To construct a feature space, do I have to involve all the features?

Question: In pattern recognition and machine learning, a feature vector is an n-dimensional vector of numerical features that represent some object.
The vector space associated with these vectors is often called the feature space.
Consider the titanic dataset, Pclass can take on {1, 2, 3}, sex can take on {'male','female'} ...
is it reasonable to say the feature space of Pclass is the set {1, 2, 3}?
or I have to involve all the features in the dataset, such as Pclass, set, Name, ...
Answer: No, the feature space is not $\{1,2,3\}$.
You start out with observations, perhaps measuring people on height, weight, and age, trying to predict some outcome Y. Feature space is what you do with those observations. Perhaps you also want to consider the square root of height but want to exclude weight. Then your feature space is height, square root of height, and age. A linear regression would fit four parameters to this model: an intercept plus one for each of height, square root of height, and age. But you, by your decision, elect not to include weight as a feature. Whatever model you use never knows that weight was measured.
In other words, feature space is some transformation (perhaps the identity) of your data.
You can make very complicated feature spaces. That's where you get all of the interaction terms. If you know about PCA, regressing on the PCs means that the PCs form your feature space.
(If you don't know PCA, it's a way of capturing much variance in few variables by taking linear combinations of the original variables.)
I love this video by MathematicalMonk (Jeffrey Miller): https://youtube.com/watch?v=rVviNyIR-fI.
Comment: Thanks for your reply. What if I consider a reduced dataset which only contains 2 features "Pclass" and "sex"? what the feature space is now?
Comment: @JJJohn it still depends on what you want in your feature space. You may just want those two variables. Perhaps you want an interaction term, too.
Comment: Thanks for your kindness. Actually,  "what I want" is the question. in other words, "what do I need" in feature space to train a classifier that could be used to predict "Survived", given a dataset composed of 2 features "Pclass", "sex" and 1 label "Survived", what the feature space is in this case.
Comment: @JJJohn it still depends on you and the decisions you make about what you want to include in your model. Maybe you just want those two variables. Maybe you want their interaction, too. Maybe you want to exclude one. The decision is yours. Did you watch Monk's video, yet? As far as what you need in your model, that is part of your job as a statistician or data scientist: to figure that out!


How can I compare an answer key to answers in java?

Question: For this project I have to have the user input an answer key and compare it to answers that have been made. For example the answer key would be inputted by the user for each question and the answers would look something like the following: 1231231412, where true = 1, false = 2, a = 1, b = 2, c = 3, etc. I am wondering on how to compare each value to the respective one in the answer key and keep count of the correct ones. I am relatively new to coding in java. Thank you in advance.
Comment: You could produce a string of the same format for the correct answers and then compare them index by index.
Answer: Simply convert the solution into the same format and then compare it with this method.
Returns -1, if the input is not of equal length, otherwise the number of right ones.
<code>static int compareKeys(String userInput, String solutionKey){
    if(userInput.length()!=solutionKey.length()) return -1;
    int re=0;
    for (int i = 0; i < userInput.length(); i++) {
        if(userInput.charAt(i)==solutionKey.charAt(i)) re++;
    }
    return  re;
}
</code>


How can I set up a "many to many" relation with an entity

Question: How do I set up n:m database relation in a custom Drupal 8 module?
I want to create task entities that may be related to different users (a task may have multiple related users and a user may have different tasks).
Using the database directly I would create two tables(as the user table already exists), a tasks table and a task_users table which contains the relations (User IDs and Task ID)s.
How do provide something like this in Drupal 8?
I only find a way to handle 1:n relations in setting up a user reference field (entity_reference) as a base field definition.
<code>$fields['user'] = BaseFieldDefinition::create('entity_reference')
</code>
But how do I provide a relationship table?
Answer: You can set unlimit cardinality, some like this:
<code>->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)</code>
This create a intermediate table to reference two entities.
Check api documentation: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21BaseFieldDefinition.php/function/BaseFieldDefinition%3A%3AsetCardinality/8.6.x


Is there any way to find a key of song somehow mathematically/algorithmically?

Question: There are many scales with different keys for the same notes.
For example, with the notes C D E F G A B C,
we can have: C major, C Ionian, A natural minor, D Dorian, E Phrygian, F Lydian, G Mixolydian, A Aeolian, B Locrian.
I understand that music in these scales have different tonal centers and sound different. I can detect a key by ear or while looking at song notes in my DAW.
But I want to categorize bunch of MIDI files by scales and keys. So i need to find a way to do it without hearing music or viewing notes/sheets.
Maybe some methods are exist? For example statistics of most frequently used note, or note with which most of bars are started, most frequent base of chord etc?
Comment: Some pieces are polytonal (or at least bitonal), which throws the biggest wrench ever into determining which key a piece (or even a phrase) is--the music is simultaneously in two or more keys at the same time. Do we have methods good enough to detect polytonality?
Comment: There are algorithms that do this! A lot of DJ software can automatically detect which key a song is written in. However, this software isn't perfect and will occasionally misidentify a song's key. (The music used in dances is rarely unusual enough to trip these algorithms up, though, so in practice, it isn't a serious issue.) Unfortunately, I'm not sure how these algorithms work, I just know they exist.
Comment: I have over 50000 midi files scraped from free resources, the files will be used for machine learning purposes, but I do not plan detect key using NN, but detect a key using some hardcoded algorithm and pass it to NN as one of parameters
Comment: Possibly relevant [answer on Stack Overflow](https://stackoverflow.com/questions/3141927/algorithms-for-determining-the-key-of-an-audio-sample).
Comment: Is it really worth the effort? Between modulation, modal vs tonal approach, chords split over different tracks...you may get really poor results. How many files do you need to go through? Are they songs from well known interpreters or personal files?
Comment: If you're hoping the key would be the input to a NN, what is the output you hope for? Just asking out of interest.
Answer: There's no such thing as a 100% sure identification of what 'the key' of a song is if you aren't taking notation as your reference - sometimes different people hear the same song as being in different keys. But if you have MIDI files, you have most of the same information that someone listening to a song would do - i.e. you have all the notes - so yes, you can make a fairly good go at finding out the key.
Basically all you need to do is work out how you'd work out the key of a piece by ear, and then turn that into an algorithm. How can you find the key of a song by ear? Mentions some relevant information, and you may find other useful techniques for finding the key of the song by ear around this site and the web.
As well as the  Krumhansl-Schmuckler algorithm mentioned by Dom, if you're a coder (I see that you have a stack overflow account), this could be a great problem to solve using machine learning, assuming you have a suitable training set of MIDI files with the key already correctly identified. You'd probably want to distil down the data in the song - for example, you could do an analysis that worked out

how many times each note of the octave occurs in the piece
how many times each note is playing on the strong beats
the average midi velocity of each note

...and then pass that in as the inputs to a suitable neural network and training algorithm. You could also consider the algorithm that Dom mentioned when deciding how to slice up the data. You might only end up with a trained NN that generates similar answers to to the Krumhansl-Schmuckler algorithm though!
You might also want to consider how you're going to deal with pieces that identifiably change key over the course of their development.
One more thing - MIDI does have the concept of a Key Signature meta message. This might not often be present or reliable but it could be another data point.
Comment: @ToddWilcox definitely true. With some machine learning models it can be better to throw more data in to the inputs and let the training process work out what's useful. With some types of models you might even to be able to slap all the note data in - I can see some parallels between that approach and the usage of Convolutional Neural Networks as used to recognise patterns in image data.
Comment: There are lots of other possibly relevant data points to collect. For each note, what one or two or three notes or intervals preceded it? For example, if a file has more than one descending fifths to the same note, we could conclude there is a cadence there and the second note of the descending fifth is possibly the key note. That might help with modulations, especially if you add proximity to other similar intervals. So if half the piece has a few G-C intervals and the other half has D-G intervals and D major chords, you can likely conclude modulation between C and G major.
Comment: @topomorto yeah, I would throw a RNN at it, with (relatively) raw MIDI as input and a vector of probabilities of keys as output, and let it figure the rest out. Feature engineering is so last decade :) In fact I *am* tempted to try that, if I could find a suitable dataset.
Answer: Seeing the spectrum analysis and frequency of notes alone typically are not enough to figure out the key. You could sometimes get it right for simpler songs, but any type of blues or chromaticism will completely screw with it. Scales used also don't tell you much about the key especially when talking about modes. The reason why is the idea of a key has a tonal center and a typical harmony, but the harmony can be fluid (borrowing, Picardy thirds, ect) and there might be trips to other tonal centers along the way (secondary dominants, modulation, ect).
The other thing to note is midi data is not good for music theory. In this case, because you cannot tell the difference between enharmonic equivalents, you will lose important information that is useful for determining key. Musicxml files are much better at retaining useful music theory data.
One algorithm I know is the Krumhansl-Schmuckler key determination algorithm which is implemented by music21.  The Krumhansl-Schmuckler key determination algorithm is based on Bayes' theorem which is statistical determination and while not always accurate is very close.
Answer: over 95% of the midi files will be major and minor scale.  find the 7 most used notes, look for flatted 3rd.  pretty solid way to do it.
not perfect.  but, plenty good enough.
Comment: Right on.  Musicians, don't most molodies/songs simply finish/resolve on the note of the key?  That would probably be an 80% solution to the issue.
Comment: Yes, most of the time a piece will end on the tonic. But I think OP wants a foolproof method. Perhaps the algorithm could be weighted with that info.
Comment: also, keep in mind that the key can change during the piece.  called "modulation".  having a computer analyze music can often be useless.  just analyze the music yourself.  you shouldn't care about EVERY midi file out there.  Just the ones you're going to bother to play.  Which is ONE of the reasons I prefer pop.  Everybody likes it.  Not too complex.  Sounds good.
Answer: The problem of key identification isn't necessarily well defined. For music where "key" is important (or else the OP isn't relevant), the cadence in the final key generally determines the key. The final chord is the goal of previous chord progressions (unless the piece just wanders.) The Picardy Third adds a bit of confusion (or perhaps signals a more modal organization.)
Another problem is the question of what  a song consists of. Given a simple verse+chorus, the key of the verse and the chorus differ. Another type of song structure is the "rondo" type (like marches and polkas). "Beer Barrel Polks" starts in C (I) and has a trio ending in F (IV). So the term key really ought to apply to parts of a piece (which begs the definition of "part" for that matter.)
Answer: The Krumhansl algo is based on perception tests, and is especially suited to diatonic contexts (Ionian mode if you prefer).
There are actually two questions in your question. the first is determining the pitch-class set, the second is determining which of the (usually seven) modes is the best candidate.
For the first question you could use some recent stuff using Discrete Fourier Transform of the sets of notes, easy to extract from mid files, and look at the directions in the complex plane of the relevant Fourier coefficients (3d and 5th). I do not know if it is worth buying my book  Music through Fourier Space  but the subject is definitely tackled in Chap. 6 :)
The analysis of modes is trickier (not that the first question is easy…), I would try some statistics on the chords used (especially triads). Say if the notes used are CDEFGAB but the most frequents chords are DFA and ACE I suspect Dorian.
But as mentioned above, there will usually be some ambiguity which is of no little importance in the charm of the music.
Comment: Why would you synthesize music from the midi file and then fourier analyze it? That would be like doing something and then undoing it immediately. The midi file already has the pitches.
Answer: This may not be an answer in the strictest sense, but I think I can point you in the direction of a software resource published as a tool for answering questions like this by an organization whose mission statement is an academic investigation of the subject.
Check out "Sonic Visualizer" and the "Aubio" music annotator software packages published by Queen Mary School of Digital Music, a school located in London, England.
This is a link to their software downloads page
Sonic Visualizer is designed for plugin extensibility.  One of the extension interfaces, VAMP, is often used to implement plugins in Python.  Queen Mary has a curated set of VAMP plugins that they maintain as a bundle package.  There is a plugin in that package entitled "Key Detection" that you may want to give a try.
I can't speak much for whether or not this is going to fit your needs unfortunately--I've heard of this software enough times to think of it when I saw your question, but I haven't a whole lot of experience with using it for this task.
Last time I used it I did so for tempo-detection and song structure segmenting plugins.  It did a decent job at those tasks--not fully automatic, but enough of a head start to make it possible to achieve my goal in less time than a pure manual start would have taken.


Constructing group homomorphisms.

Question: Let $G$ and $H$ be groups, and suppose I want to construct a group homomorphism $\phi$ between them. From what I know, I just need to send each element $x \in G$ to an element $y \in H$ such that the order of $\phi(x)=y$ divides the order of $x$. Is that right?
I was a bit confused, because I was trying to construct a nontrivial homomorphism from $D_{6} = \{1, b, b^2, a, ba, b^2a\}$ to $\Bbb{Z}_2 = \{e, s\}$ , and I thought of sending $a \in D_6$ to $s \in \Bbb{Z}_2$, since the order of $s$ (obviously) divides that of $a$, and sending the rest of the elements to $e$. However, we have then have $\phi(ba)\phi(b) = (e)(e) = e$ and $\phi(bab) = \phi((ab^2)b)) = \phi(a) = s \not=e$. So $\phi$ cannot be a group homomorphism, right?
Am I doing something wrong here, or do I need to satisfy more conditions in order to construct a group homomorphism?
Answer: Your example is a good counterexample to the false claim "a map $\phi : G \to H$ that satisfies $\operatorname{order}_H(\phi(x))$ divides $\operatorname{order}_G(x)$ for all $x \in G$ is a homomorphism." It is true that any homomorphism satisfies this condition (i.e., it is a necessary condition) but it is not sufficient, as your example shows.
A homomorphism simply must satisfy $\phi(x)\phi(y) = \phi(xy)$ for any $x,y \in G$.
Here are some hints for constructing your nontrivial homomorphism:

The set of elements of $G$ that map to $e$ is a subgroup of $G$ known as the kernel of $\phi$.
So, if $x$ and $y$ are not in the kernel of $\phi$, then $xy$ must be in the kernel because $\phi(x)\phi(y)=s^2=e$.
Answer: No, your condition is necessary but not sufficient. e.g. any map from the Klein 4-group $\langle a,b:a^2=b^2=aba^{-1}b^{-1}=e\rangle$ to itself that fixes the identity satisfies your condition, but any group homomorphism is determined by where it sends $a$ and $b$.
Answer: To construct a homomorphism between (G,*) and (H,•)
For every pair of elements g1, g2 in G we have: f(g1 * g2) = f(g1) • f(g2)
for this to be true, you need to map the generetor in G to the generator in H and keep on.
For example; constructing a non trivial homoomorphism between Z/6Z and Z/3Z both additive:
map 1+ Z/6Z to go to 1 in Z/3Z,
 map 2+ Z/6Z to go to 2 in Z/3Z,
 map 3+ Z/6Z to go to 0 in Z/3Z,
 map 4+ Z/6Z to go to 1 in Z/3Z,
 map 5+ Z/6Z to go to 2 in Z/3Z,
 map 0+ Z/6Z to go to 0 in Z/3Z

Constructing group homomorphisms.


How do I remove text format information from the bottom of my text areas

Question: I have a content type which all of my users can create instances of.  The last text area has a section listing the particulars of the text format.  I would like to disable this area so that the test format does not explain itself, and all the user sees is a text area.
Answer: You can use the Simplify module for this.
It describes itself as follows:
Simplify allows particular fields to be hidden from the user interface. This helps to de-clutter forms and present a more user-friendly experience to content editors.


Drupal filters are not cacheable

Question: I have a Drupal 7 site with many custom filters. Everything works correctly, but not quick.
It looks as though the filter cache system does not work properly because the cache_filter table is always empty.
There is the issues in d.o. but it isn't replied.
Answer: The cache is used only when the input format is enabled to use it; this means that none of the input filters that are used by the input format disable the cache.
The code of check_markup(), which the function that is called when an input format needs to be applied to a text, contain the following code.
$cache = $cache && !empty($format->cache);
$cache_id = '';
if ($cache) {
  $cache_id = $format->format . ':' . $langcode . ':' . hash('sha256', $text);
  if ($cached = cache_get($cache_id, 'cache_filter')) {
    return $cached->data;
  }
}

$cache is a parameter passed to the function, and $format is the variable containing the value returned by filter_format_load(); if $format->cache is FALSE, the filter cache will not be used.
In the case of the body content of a node, the function that is used to sanitize the body content (and the content of other fields) is _text_sanitize(), which invokes check_markup() without to use the fourth parameter ($cache), which by default is FALSE. This means that, whatever the filter is set to use the cache or not, the cache will not be used.
function _text_sanitize($instance, $langcode, $item, $column) {
  // If the value uses a cacheable text format, text_field_load() precomputes
  // the sanitized string.
  if (isset($item["safe_$column"])) {
    return $item["safe_$column"];
  }
  return $instance['settings']['text_processing'] ? check_markup($item[$column], $item['format'], $langcode) : check_plain($item[$column]);
}

This is different from what done by user_comment_view() (the implementation of hook_comment_view() that is used to filter out the signature of the user who wrote the comment), which passes the fourth parameter to check_markup(), setting it to TRUE.
function user_comment_view($comment) {
  if (variable_get('user_signatures', 0) && !empty($comment->signature)) {
    // @todo This alters and replaces the original object value, so a
    //   hypothetical process of loading, viewing, and saving will hijack the
    //   stored data. Consider renaming to $comment->signature_safe or similar
    //   here and elsewhere in Drupal 8.
    $comment->signature = check_markup($comment->signature, $comment->signature_format, '', TRUE);
  }
  else {
    $comment->signature = '';
  }
}

Comment: This is by design: the rendered fields themselves are cached, so it serves no purpose to cache them in `check_markup()`.
Answer: If cache_filter table is empty, but memcache or APC is enable, then it's great ;)
Or as kiamlaluno pointed out, one of your filter disables cache (e.g. PHP filter) so that the whole format that uses that filter is not cacheable.
Comment: I have default format "Filtered HTML" and four filters enabled:  1. Limit allowed HTML tags"
 2. Convert line breaks into HTML (i.e.  and )
3. Convert URLs into links
4. Correct faulty and chopped off HTML **Wich of them filters can disable cache?**
Comment: I was talking about cache because you told "many custom filters". With all the filters you list, that format is cacheable. But there is another reason, that cache_filter is empty because the whole content is cache elsewhere (cache_field for instance). Are you sure that the filter is processed again and again everytime you view your content?
Comment: You are right. Default filters are processed only after cache cleaning. And it's really, content is cached in __cache_field__.  It seems as __cache_filter__ hasn't ever been used. Can you explain me in which cases this table will be used?

Drupal filters are not cacheable


Why am I getting an access violation exception in a pointer operation?

Question: I'm using C++ Visual Studio .Net 4.0 on Windows 7.0 x64. This happens just on the first loop of the while statement.
int main()
{
  char *string = new char[11];

  string = "characters\0";

  toUppercase(string);

  return 0;
}

void toUppercase(char *stringPtr)
{
 while(*stringPtr != '\0')
 {
    if(*stringPtr >= 'a' && *stringPtr <= 'z')
    {
        *stringPtr = *stringPtr - 32; // this is the culprit
    }

    ++stringPtr;
 }
}

Comment: If you're using the .NET Framework, why in the world are you re-implementing a function to convert a string to uppercase?
Comment: Have you checked that the *stringPtr actually contains \0 at the end ?
Comment: Was it a read or write AV?  Is the string properly null-terminated?  How did you call toUppercase, exactly?
Answer: I suspect you're doing something like this:
toUppercase("test");

The problem is "test" is an array of const char, not char, so cannot be modified. However, due to a terribly stupid deprecated special conversion, a string literal can be treated as char* anyway.
(Your function also fails to test for toUppercase(0).)
Comment: if can you please state what can I do with this? Thanks.
Comment: @Lemuel: Do with what? If you're doing what my answer shows in your code, don't. You need to have a modifiable buffer: `char buff[] = "test"; toUppercase(buff);`
Answer: My guess would be that you are passing to that function a pointer to a string that resides in read-only memory. However, you would need to share the surrounding code -- i.e., how you actually call this method -- to pinpoint the problem.
EDIT: In view of the above, here's one way to get this working:
char* str = strdup("hello world");
toUppercase(str);

Answer: You can get #AV if memory isn't writeable:
toUppercase((char*)"str");


Laplace inverse of $(s+2)U(s)=0$ and $(s+1)U(s)=0.$

Question: I asked this question here and I was given an answer but with some steps unfolded Solve the following problem, $u'(t)+p(t)u(t)=0,\;\;u(0)=0,$ $p(t)=\begin{cases}2& 0\leq t< 1,\\1 &t\geq 1\end{cases}.$.
My question now is, how do I take the La
Taking the Laplace transform of both sides
$$L(u'(t))+L(p(t)u(t))=0,$$
$$sU(s)-u(0)+L(p(t)u(t))=0,$$
For $0\leq t<1$
$$sU(s)+2U(s)=0,$$
$$(s+2)U(s)=0.$$
For $t\geq 1$
$$sU(s)+U(s)=0,$$
$$(s+1)U(s)=0.$$
Can someone show me how to take the Laplace inverse of this to get $u(t)=e^{-2t}$ and $u(t)=e^{-t}.$
Comment: No. Write the function $p$ using step functions.
Answer: First for $0 \le t \le 1$
$$
sU(s)+2U(s)=u(0)\Rightarrow u(t) = u(0) e^{-2t}(\phi(t)-\phi(t-1))
$$
and with $u(1) = u(0)e^{-2}$ we proceed solving for $1 \le t $
$$
sU(s)+U(s) = u(1)\Rightarrow u(t) = u(1)e^{-(t-1)}\phi(t-1)
$$
finally
$$
u(t) = u(0) e^{-2t}(\phi(t)-\phi(t-1))+u(0)e^{-2}e^{-(t-1)}\phi(t-1)
$$
or
$$
u(t) = u(0)\left(e^{-2t}(\phi(t)-\phi(t-1))+e^{-t-1}\phi(t-1)\right)
$$
Here $\phi(t)$ is the Heavside unit step function.
Comment: @Isham  As long as $u(0) = 0$ the solution is $u(t) = 0$.
Comment: @Isham Remember that the time origin now is at $t = 1$ so the response is translated now.
Comment: @Isham you are welcome!
Comment: but $u(0)=0 $  so the solution is null ..and $u(1)=0$ because of $u(0)$ but also the step function $\phi(1)-\phi (0)=1-1=0$
Comment: @Isham The problem as formulated has as solution $u(t) = 0$ because any homogeneous DE with  null initial conditions has null solution. I wrote the exact response to initial conditions. If the system were submitted to a step function, then the formulation should be non-homogeneous as $u'(t)+p(t)u(t) = \phi(t)$ with $\phi(t)$ the Heavside unit step function.
Comment: I do agree, the problem is really weird ...and I think it has no solution..
Comment: +1 Cesareo for your answer  another question : shouldnt it be here $e^{-t}$ $$sU(s)+U(s) = u(1)\Rightarrow u(t) = u(1)e^{-(t-1)}\phi(t-1)$$
Comment: ok thanks for your answer...I got it now
Answer: Hint: Correct your formulas with step function:
$$(s+2)U(s)=\dfrac1s$$
and
$$(s+1)U(s)=\dfrac1s$$


Regex best practice: multiple patterns or single one with combined expression?

Question: Think of the following scenario:

Application receives a list of regex from server (HTTP GET returns a List with each item indicating a regular expression.);
User input text needs to be validate against these expressions;
Application runs on Android, so memory is an issue;
List of expressions is not frequently changed.

What would be better:

Cache several Pattern objects, each one containing a single regex returned from server;
Concatenate the regex - <code>(REGEX1)|(REGEX2)|(REGEX3)...|(REGEXN)</code> - and maintain a single object on memory? - refreshing it whenever a single regex is added or removed, which doesn't happens very often.
Comment: If user input is short, the method doesn't matter. If user input is long, don't concatenate the patterns (except if each of them is anchored to the start and end of the string). Whatever try to reduce the number of patterns.
Comment: Well I'm not very fluent with regex in Java (I'm mainly python Programmer), but there is serious question. What about parsing regex? Parsing short regex is much less complicated than parsing long one. On the other hand parsing all regex at once can create better state machine. If I could be honest - this question needs serious benchmark :)
Comment: `Application receives a list of regex from server` ... what does this mean?  Can you give us an example of your data pipeline?
Comment: An HTTP GET returns a List with each item indicating a regular expression.
Comment: What if input is huge?
Comment: And how many regex do you wanna use? If it's not more than thousands then there is no real benefit of keeping it in one string.
Comment: It would be definitely less than one hundred. I cannot control the complexity of the regex once it is up to the client input them. @Alex Baranowski having them all in one single regex would simplify the cache refresh process perhaps?
Comment: I'm mainly concerned about the number of objects I'll have to keep in memory in case I do not concatenate them in one single expression. Thanks for the contribution Alex
Answer: I don't imagine there is a way to answer this question without having a specific list of regex's and the list of input.  Because, each regex/input combination is going to result in a different amount of memory used.  Here is what my instincts tell me:

Evaluate the Regex's one at a time.  In the "OR" scenario, the regex must simultaneously evaluate all of the OR'd expressions, so that would take more RAM, I believe.
Order the Regex's in order of either: (a) Likelihood to match, so that you can abandon evaluating the rest of the regex's or (b) Early non-matching, so that the regex can be quickly discarded as never going to match (for example "^a" only requires evaluating the first character of the string where as "a" requires searching the whole string for an "a".)

Ultimately, only testing can really tell you what takes more time/memory, I'm afraid.


How correctly copy and modify an array in typescript?

Question: I'm trying to copy an array, replace some elements in it and then replace the original array with the copy containing the new elements.
Here's my code:
type Span = {
  start: number
  end: number
}

const handleSplitClick = (span: Span, tag: string) => {
    let copy = props.value.map(a => {return{...a}})
    const splitIndex = copy.findIndex(s => s.start === span.start && s.end === span.end)
    if (splitIndex != -1 ){
      const tagIndex = (copy[splitIndex] as any).tags.findIndex((t:string) => t == tag)

      if (tagIndex > -1){
        (copy[splitIndex] as any).tags.splice(tagIndex, 1)
        console.log(copy)
        console.log(props.value)
      }
    }
 }

Here's how props.value looks:

I'm trying to modify the tags array within props.value.
I tried to initialise the copy of the array in these ways:
//1
let copy = props.value.map(a => {return{...a}})

//2
let copy = [...props.value]

//3
let copy = props.value.map(a => Object.assign({}, a));

Regardless of what I do, when I try to remove the particular element from tags:
(copy[splitIndex] as any).tags.splice(tagIndex, 1)

Both the original props.value and copy get modified.
How can I correctly achieve this?
Thanks.
Comment: Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) *"Here's how props.value looks:"*  Please post code, error messages, markup, **data structures**, and other textual information **as text**, not just as a *picture* of text. Why: http://meta.stackoverflow.com/q/285551/157247
Answer: It looks like you want to create a new array where you've removed the matching tag from the tags array when its start and end match, and that you want to do this immutably (that is, without changing the original array or original element object, like a state update in React and similar). To do that, in map return the other elements unchanged, and return a new element with the updated tags for the one that matches:
const handleSplitClick = (span: Span, tag: string) => {
    // Use `map` to create the new array
    const copy = props.value.map((element) => {
        // Is this the element we want to update?
        if (element.start === span.start && element.end === span.end) {
            // Yes, return an updated copy with the first tag removed
            return {
                ...element,
                tags: element.tags.filter((t) => t !== tag),
            };
        }
        // No, just return the element unchanged
        return element;
    });
    // ...use or return `copy`...
};

Or if you prefer keeping things really terse:
const handleSplitClick2 = (span: Span, tag: string) => {
    // Use `map` to create the new array
    const copy = props.value.map((element) =>
        element.start === span.start && element.end === span.end
            ? {
                  ...element,
                  tags: element.tags.filter((t) => t !== tag),
              }
            : element
    );
    // ...use or return `copy`...
};

Note that in the vast majority of cases, you don't need or want type annotations on the callback parameters for map or filter; TypeScript will infer them correctly:
Live version with TypeScript commented out:

const props = {
    value: [
        {start: 0, end: 1, tags: ["a", "b", "c"]},
        {start: 1, end: 2, tags: ["a", "b", "c"]},
        //                             ^^^ The one we'll target
        {start: 2, end: 3, tags: ["b", "c"]},
    ]
}

const handleSplitClick = (span/*: Span*/, tag/*: string*/) => {
    // Use `map` to create the new array
    const copy = props.value.map((element) => {
        // Is this the element we want to update?
        if (element.start === span.start && element.end === span.end) {
            // Yes, return an updated copy with the first tag removed
            return {
                ...element,
                tags: element.tags.filter((t) => t !== tag),
            };
        }
        // No, just return the element unchanged
        return element;
    });
    console.log(JSON.stringify(copy, null, 4));
};

handleSplitClick({start: 1, end: 2}, "b");
.as-console-wrapper {
    max-height: 100% !important;
}


What is the execution sequence of functions while rendering a form?

Question: I have a form in my website with id: sample_test_custom_form. I have used hook_form_alter in a custom module  to alter this form. I also have a preprocess function (mytheme_preprocess_sample_test_custom_form) to do a little preprocessing.
However the alter function and the preprocess function are a little dependent on each other.
So I ask, what is the execution sequence of these function? Which of them is executed first?
Answer: Here's a diagram of the form build workflow in Drupal 7

More info: https://drupal.org/node/165104
Comment: Preprocessing is part of the theme layer. It will happen after form_alter
Comment: It is quite informative but doesn't say anything about the *preprocessing* unless preprocessing is a part of form building. Is it?


Imagem não aparece 100%

Question: Estou tentando criar uma LandingPage mas a primeira imagem está sendo cortada
Estou usando este CSS:
.parte1 {
    min-height: 100%;
    max-height: 100%;
    padding-top: 50px;
    padding-bottom: 50px;
    text-align: center;
    color: #f8f8f8;
    background-image: url('Images/Fundo/image1.png');
    background-repeat: no-repeat;
    background-size: cover;
}

e esta div:

e ai a imagem aparece cortada assim:

O que pode estar errado?
Answer:
Segue a explicação no código:

.parte1 {
    min-height: 100%;
    max-height: 100%;
    padding-top: 50px;
    padding-bottom: 50px;
    text-align: center;
    color: #f8f8f8;
    background-image: url('https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66');
    background-repeat: no-repeat;
    background-size: cover;
}

.parte2 {
    min-height: 100%;
    max-height: 100%;
    padding-top: 50px;
    padding-bottom: 50px;
    text-align: center;
    color: #f8f8f8;
    background-image: url('https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66');
    background-repeat: no-repeat;
    background-size: contain;
}

.parte3 {
    height: 50px;
    padding-top: 50px;
    padding-bottom: 50px;
    text-align: center;
    color: #f8f8f8;
    background-image: url('https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66');
    background-repeat: no-repeat;
    background-size: cover;
}
O COVER ESTÁ FAZENDO O PAPEL DELE DE USAR A DIV COMO UMA MÁSCARA:

O CONTAIN, NO LUGAR DO COVER, GARANTE A PRESERVAÇÃO DA IMAGEM DENTRO DOS LIMITES DA DIV:

MAS SE VOCÊ CONTROLAR A ALTURA DA DIV(sabendo o tamanho máximo da imagem), O COVER NÃO SERÁ UM PROBLEMA:

Imagem não aparece 100%


Law of Large Numbers for whole distributions

Question: I'm aware of the Law(s) of Large Numbers, concerning the means. However, intuitively, I'd expect not just the mean, but also the observed relative frequencies (or the histogram, if we have a continuous distribution) to approach the theoretical PMF/PDF as the number of trials goes into infinity.

Is my intuition wrong? Always or only for some degenerate cases (e.g. Cauchy)?
If not, is there a special name for that law?
Comment: If for a histogram, you are interested in $\mathbb P(a < X \le b)$ over some particular interval $(a,b]$, then this is a consequence of the Law of Large Numbers and can be shown using an indicator variable
Comment: @Henry law of large numbers directly applies [here](https://stats.stackexchange.com/questions/560742/why-are-rating-distributions-smoother-when-there-are-more-players) you mean?
Answer: While the law of large numbers is framed in terms of "means" this actually gives you a large amount of flexibility to show convergence of other types of quantities.  In particular, you can use indicator functions to get convergence results for the probabilities of any specified event.  To see how to do this, suppose we start with a sequence $X_1,X_2,X_3 ,... \sim \text{IID } F_X$ and note that the law of large numbers says that (in various probabilistic senses) we have the following convergence:
$$\frac{1}{n} \sum_{i=1}^n X_i \rightarrow \mathbb{E}(X)
\quad \quad \quad \quad \quad
\text{as } n \rightarrow \infty.$$
In the sections below I will show how you can use this basic result to show that the empirical CDF converges to the true CDF of the underlying distribution in certain useful senses.  This will also show you how the law of large numbers can be applied in a creative way to prove convergence results for other things that don't look like "means" of quantities (but actually are).

Pointwise convergence of the empirical CDF to the true CDF: In your question you are interested in the convergence of the empirical distribution function to the true distribution function $F_X$.  Let's start by looking at a particular point $x$ by examining the sequence of values $Y_1,Y_2,Y_3 ,...$ defined by $Y_i \equiv \mathbb{I}(X_i \leqslant x)$.  This latter sequence is also IID, so the law of large numbers says that (in various probabilistic senses) we have the following convergence:
$$\frac{1}{n} \sum_{i=1}^n Y_i \rightarrow \mathbb{E}(Y)
\quad \quad \quad \quad \quad
\text{as } n \rightarrow \infty.$$
Now, at the point $x$ the empirical distribution function for the sequence $\mathbf{X}$ and the true CDF for the distribution can be written respectively as:
$$\begin{align}
\hat{F}_n(x)
&\equiv \frac{1}{n} \sum_{i=1}^n \mathbb{I}(X_i \leqslant x)
= \frac{1}{n} \sum_{i=1}^n Y_i, \\[12pt]
F_X(x)
&\equiv \mathbb{P}(X_i \leqslant x)
= \mathbb{E}(Y). \\[6pt]
\end{align}$$
(The latter result follows from the fact that $\mathbb{E}(Y) = \mathbb{P}(Y=1)$ for any indicator variable $Y$.)  We can therefore re-frame the previous convergence statement from the law of large numbers to give the pointwise  convergence result:
$$\hat{F}_n(x) \rightarrow F_X(x)
\quad \quad \quad \quad \quad
\text{as } n \rightarrow \infty.$$
You can see that this demonstrates that the empirical CDF converges pointwise to the true CDF for IID data; this is a direct consequence of the law of large numbers.    Specifically, the weak law of large numbers establishes pointwise convergence in probability, and the strong law of large numbers establishes pointwise convergence almost surely.

Uniform convergence of the empirical CDF to the true CDF: To go further than the above result, you need to use the uniform law of large numbers—or some other similar theorem—to establish uniform convergence of the empirical CDF to the true CDF.  If you use the uniform law of large numbers then you can establish uniform convergence of the empirical CDF under some restrictive assumptions on the underlying CDF.  However, there is actually a stronger theorem called the Glivenko–Cantelli theorem that establishes uniform convergence of the empirical CDF to the true CDF (almost surely) for any IID sequence of data.  That is, the theorem proves that:
$$\sup_x | \hat{F}_n(x) - F_X(x) | \overset{\text{a.s}}{\rightarrow} 0
\quad \quad \quad \quad \quad
\text{as } n \rightarrow \infty.$$
If you would like to learn more about this part, it is worth having a look at the proofs of the uniform law of large numbers and the Glivenko–Cantelli theorem to see how each of them work to establish uniform convergence.  The former theorem is broader, but it comes with some restrictions on the input function.  The latter theorem applies specifically to the empirical CDF of IID data, but it establishes uniform convergence (almost surely) without any additional assumptions.
Comment: The argument of the Glivenko-Cantelli theorem applies quite broadly: the same proof shows that you can go from pointwise convergence of an empirical CDF to uniform convergence for non-IID sequences as well, and the idea ('bracketing') is behind some more general uniform LLN results
Comment: @ThomasLumley: Yes, indeed.  Here I am thinking of the original theorem from Glivenko/Cantelli (which I'm pretty sure was for IID only), but I take it you are looking at the class of theorems later established by other authors using the same general argument.  I think this hinges on whether you consider "the GC theorem" to be just the original theorem, or the class of theorems that came later, or the class of all theorems that can in principle be derived by this general method, etc.  (A bit like the CLT.)  But yes, you are right that this can be extended to the non-IID case.
Answer: You may be asking for the Glivenko-Cantelli theorem.
https://en.wikipedia.org/wiki/Glivenko%E2%80%93Cantelli_theorem
Note that this is about cumulative distribution functions, i.e., for data sets relative frequencies that observations are below any given value $x$, or by implication in any interval $[x_1,x_2]$. Histograms are not a standard basis for such a result, as there are many ways to set up a histogram (bar widths etc.), whereas the cdf is uniquely defined.
Comment: (+1) The Glivenko-Cantelli theorem is a result of uniform convergence hence is stronger than pointwise convergence of the empirical cdf.
Answer: As an alternative to Glivenko–Cantelli, you can look at Sanov's theorem, which uses the Kullback–Leibler divergence as the distance measure. For any set $A$ of frequency distributions, this theorem upper-bounds the probability that the observed frequency distribution $f$ (for $n$ IID instances of a random variable with distribution $q$ on an alphabet $X$) lies in $A$, as follows:
$$P[f\in A] \leq (n+1)^{\left|X\right|} 2^{-d_\mathrm{min} n},$$
where $d_\mathrm{min} = \min_{f\in A} D_\mathrm{KL}(f||q)$ is the minimum KL divergence between frequencies in $A$ and the true distribution $q$. To apply this to directly address the question asked, this result implies that if $\mathcal{B}^{D_\mathrm{KL}}_d(q)$ denotes the "ball" of frequency distributions with KL divergence $<d$ from the true distribution $q$, we can upper-bound the probability of getting a frequency distribution outside this ball as
$$P[f\notin \mathcal{B}^{D_\mathrm{KL}}_d(q)] \leq (n+1)^{\left|X\right|} 2^{-d n}.$$
This expression has the advantage of being an explicit bound for finite sample size, in case you need something more than an asymptotic convergence result.  (A weaker option is provided by the asymptotic equipartition property, but the set considered in that theorem potentially includes some number of frequency distributions that are "very far" from the true distribution.)
Answer: We can define an indicator function that gives whether an observation is inside or outside of an interval,  which will have a Bernoulli distribution, and for a sample of multiple observations we have a binomial distribution. We can then apply the CLT to show that the mean converges to the probability mass of the original PDF in the interval. The error in absolute terms converges uniformly to zero, but I don't think the error as a percentage of the expected probability does.


How can i convert it to laravel

Question: i have a simple question for u.
How can i convert it to laravel?
Native php
<form method="post">
<input type="button" name="button">
</form>
<?
if(isset($_POST["button"])){
    echo "its work";
}
?>

Answer: Holy Moly....

I highly recommend you to read the documentation.

routes/web.php
<?php
Route::get('form', 'TestController@form')->name('form');
Route::post('form', 'TestController@submit');

App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Request;

class TestController extends Controller
{

    public function form()
    {
        return view('form');
    }

    public function submit(Request $request)
    {
        $validator = $request->validate([
            // validation here
            // 'input' => 'rule'
        ]);

        dd($validator);
    }

}

resources/views/form.blade.php
<form method="post" action="{{ route('form') }}">
    {{ csrf_field() }}
    <input type="submit" name="button">
</form>

How can i convert it to laravel


Why was Mozart so good at writing melodies?

Question: I don't mean this in a "hurr durr IQ hurr durr GENIOUS" way. Do we know if Mozart specifically studied folk songs, lieder etc. over learning fugues and counterpoint?
Comment: Consider re-titling your question along the lines of "How was Mozart trained in melody writing?"
Comment: I would edit your first sentence to fit the writing style used here, but I'm not totally sure what it means- maybe that you want an explanation other than being "naturally gifted"?
Comment: Maybe he took the Emperor's advice & reduced the number of notes ;))
Comment: Interestingly, studying fugues and counterpoint is one way to gain mastery over many aspects of melody. It's not a distraction from melody writing at all.
Comment: Do you have any reason to believe that Mozart was better at writing melodies than his contemporaries?
Comment: Current hunch: he had practice. Mozart's first compositions were published before he turned 9 years old. He even composed an opera before he turned 14! YMMV when it comes to Mozart's earliest compositions (and, IMO, his first 24(!!) symphonies).
Comment: You're leading the witness here.
Answer: Mozart was trained by his father, as well as other composers. He also attended a conservatory, where he studied traditional composition. He did not "revolutionize" anything- most of his music relied on currently established compositional techniques, which I suppose answers your question- he most likely studied fugues and counterpoint.
https://www.britannica.com/biography/Wolfgang-Amadeus-Mozart
i got all of this information here. It's quite a googleable question, so if you want more information, take a look at the link :)


Run script that is saved inside an object as a string

Question: I have an object that contains a string which is actually a script.
forms = {
    "528": {
        "thankyou_page_url": "",
        "external_url": "http:\/\/example.com",
        "use_external_url": "on",
        "open_in_new_tab": "",
        "after_sent_script": "alert( 'test script' );"
    },
};

I want to run the alert function from after_sent_script, inside another function. Something like this:
jQuery(document).ready(function() {
    if (form.after_sent_script) {
        form.after_sent_script;
    }
});

This of course does not work.
How can it be done?
Comment: Did you try eval
Comment: You have to save it as a function, not a string http://jsfiddle.net/29sVC/1299/
Comment: Thanks Amit, that works! :)
Answer: You can use eval, but you can read up why you should be cautious using eval.

var forms = {
    "528": {
        "thankyou_page_url": "",
        "external_url": "http:\/\/example.com",
        "use_external_url": "on",
        "open_in_new_tab": "",
        "after_sent_script": "alert( 'test script' );"
    },
};

eval(forms['528']['after_sent_script']);
Answer: You can use eval function
jQuery(document).ready(function() {
    if (form.after_sent_script) {
        eval(form.after_sent_script);
    }
});


How to change the docker.socket group

Question: I am managing several compute hosts running Ubuntu 18.04 (with systemd) and Docker inside a mostly trusted network.
I have an auth server so rather than manually add users to the docker group, so that they can run docker commands, I made a group ldap-docker on the auth server and add my users to that. Then I added "group": "ldap-docker" to /etc/docker/daemon.json file, and removed the local "docker" group from the systems.
This works fine on several hosts, but on some of them docker.service won't start because /var/run/docker.sock is still owned by root:root rather than root:ldap-docker.  the docker.socket.service also reports a failure to start
$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied
$ sudo systemctl status docker.socket
● docker.socket - Docker Socket for the API
   Loaded: loaded (/lib/systemd/system/docker.socket; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2021-03-12 08:11:48 PST; 8h ago
   Listen: /var/run/docker.sock (Stream)

Mar 12 08:11:48 host.example.com systemd[1]: Starting Docker Socket for the API.
Mar 12 08:11:48 host.example.com systemd[1171]: docker.socket: Failed to resolve group docker: Connection refused
Mar 12 08:11:48 host.example.com systemd[1]: docker.socket: Control process exited, code=exited status=216
Mar 12 08:11:48 host.example.com systemd[1]: docker.socket: Failed with result 'exit-code'.
Mar 12 08:11:48 host.example.com systemd[1]: Failed to listen on Docker Socket for the API.

I can run sudo chgrp on the /var/run/docker.sock socket, but the docker service has already failed to start so that doesn't help.
How do you control the startup of the docker.sock.service? and why would my setup work okay on some machines but not others?
Answer: Okay so the problem was two-fold. One was that you (probably) have to change the dockerd group with --group, and also you have to change the docker.socket.service group as well.
The other part is that you need to modify the settings with a systemd override (sudo systemctl edit docker) rather than making changes in /lib/systemd/system/docker.service since that gets automatically restored to known good values on a reboot (couldn't find proof in the docs for that but it seemed to happen that way).

How to change the docker.socket group


Bad performance of ARIMA model on online buzz data, Any suggestions?

Question: I was wondering if the ARIMA model is constrained to predict online buzz data (time series data).
What I want to do: Use the past round 30 months data to predict next month; and I use Python
Here are my steps for prediction

My original data
The number of data is 972 (unit = days).

enter code here

I detect the outliers based on z scores and replace it with moving average.

I reset the time interval. The data was resampled and aggregated on a weekly basis.
The number of data is 137. (unit = weeks)

Examining the unit root by the Augmented Dickey-Fuller test (ADF test) and difference if needed

Run acf and pacf values,

Divide my data into train and test set and the output of ARIMA model

The predicted values are
[ 24.23091639]
[-14.00353408]
[  6.92498285]
[  6.87718253]
The actual values are
[[-145.        ]
[ 143.        ]
[  11.42857143]
[  41.90670554]]
It seems to have a poor performance on predicting but I have no idea how to make it more accurate. I have tried several ways including outliers detection, considering the seasonal effects (Too few observations to estimate starting parameters for seasonal ARMA).
I'd like to ask the experts in the community, what would you do if you had this dataset? Of course, it doesn't necessarily have to be limited to traditional time series forecasting models; machine learning methods can also be used
Answer:
You are not saying what you learned from your ACF and PACF plots. Did this lead you to choose a particular ARIMA model? Do consider using an established auto-ARIMA tool: Selecting ARIMA orders by ACF/PACF vs. by information criteria In Python, pmdarima should work reasonably well, since it is a port of the forecast and fable packages in R which IMO constitute the gold standard in automatic time series forecasting.

Your time series may well simply have a lot of residual variance, AKA "noise". You can't say that your ARIMA forecasts are "bad" just by comparing a holdout sample to forecasts - if there is a lot of noise, your ARIMA may do the best it can. See How to know that your machine learning problem is hopeless?

Finally, you may be surprised at the fact that your ARIMA forecasts vary less than the time series. This is absolutely as it should be. ARIMA, just like any other forecasting/prediction/ML model, tries to disentangle the systematic forecastable components of your data from the unsystematic and unforecastable ones. If there are little systematic dynamics, then your forecast will not vary much. See Canonical duplicate for "Why do predictions vary less than observations?" and links therein.

Per above, ARIMA may indeed be the best you can do. Alternatively, consider Exponential Smoothing. We have a collection of forecasting textbooks and resources here: Resources/books for project on forecasting models


Consulta ao banco de dados em C# mvc

Question: Como é a sintaxe ou como transformo uma query com vários joins para uma busca em minha aplicação em c# asp.net mvc?
Exemplo: Tenho um formulário com o seguinte controller
        public ActionResult Pesquisa(int id = 0)
    {

        var resultado = context.Graduacoes.Include(e => e.Cursos).Where(e => e.Id == id).ToList();

        ViewBag.Graduacoes = new SelectList(db.Graduacoes, "Id", "Nome");

      return View(resultado);
    }

Eu preciso que ao usuário selecionar na lista a graduação e clicar em 'Pesquisar' me retorne o nome e outras informações dos indivíduos que possuem aquela graduação. No banco eu faria assim:
select e.Id, c.Nome from Curso c
inner join Escolaridade e
on e.Id = NívelId

Models
    public class Graduacao
{
    public int Id { get; set; }

    [Required(ErrorMessage = "O Nome é obrigatório.")]
    [StringLength(50, ErrorMessage = "O nome pode ter no máximo 50 caracteres.")]
    public string Nome { get; set; }

    [Range(0, 20, ErrorMessage="O Nível deve ser um número positivo até 20.")]
    public int Nível { get; set; }

    public virtual ICollection<Curso> Cursos { get; set; }
}

    public class Curso
{
    public int Id { get; set; }

    [Required(ErrorMessage = "O Nome é obrigatório.")]
    [StringLength(50, ErrorMessage = "O Nome pode ter no máximo 50 caracteres.")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "A Instituição é obrigatória.")]
    [StringLength(50, ErrorMessage = "A Instituição pode ter no máximo 50 caracteres.")]
    public string Instituição { get; set; }

    [Required(ErrorMessage = "O Nível do curso é obrigatório.")]
    public int? NívelId { get; set; }
    public virtual Escolaridade Nível { get; set; }

View
@model IEnumerable<Competências.Models.Graduacao>

<title>Pesquisa por Graduação </title>

     @using (Html.BeginForm())
     {

        @Html.DropDownList("Graduacoes")

    <button type="submit" id="pesquisar" class="btn btn-primary btn-xs">Buscar</button>
     }

    @{
        if (Model != null)
        {
            foreach (var item in Model)
            {

        Nome

                      @item.Nome

            }
        }
    }

Eu queria aqui, ao escolher uma graduação por exemplo "Ensino Médio" me retornar o nome de todos os cursos cadastrados com ensino médio. Eu quero retornar mais coisas, mas resumi para isso.
Answer:
Aviso: essa resposta foi sendo desenvolvida a partir do que o autor da pergunta mencionou. Qualquer redundância ou detalhe esquisito que possa aparecer é pelas várias edições que esta resposta teve.

Estou supondo que tem um mapeamento assim na sua aplicação:
public class Graduacao
{
    [Key]
    public int GraduacaoId { get; set; }

    public virtual ICollection<Curso> Cursos { get; set; }
}

public class Curso
{
    [Key]
    public int CursoId { get; set; }
    public int PerfilId { get; set; }
    public int GraduacaoId { get; set; }

    public virtual Perfil Perfil { get; set; }
    public virtual Graduacao Graduacao { get; set; }
}

public class Perfil
{
    [Key]
    public int PerfilId { get; set; }

    public virtual ICollection<Curso> Cursos { get; set; }
}

Então, um select básico ficaria assim:
public ActionResult Index(int id)
{
    var resultados = context.Graduacoes
                        .Include(g => g.Cursos)
                        .Where(g => g.GraduacaoId == id).ToList();

    ViewBag.Graduacao = new SelectList(db.Graduacao, "Id", "Nome");

    return View(resultados);
}

Perfil deve ser carregado por carga preguiçosa, que é feita automaticamente pelo Entity Framework.

EDIT
Mude seu Controller para:
public ActionResult Index()
{
    ViewBag.Graduacao = new SelectList(db.Graduacao, "Id", "Nome");

    return View();
}

[HttpPost]
public ActionResult Pesquisa(int id)
{
    var resultados = context.Graduacoes
                        .Include(g => g.Cursos)
                        .Where(g => g.GraduacaoId == id).ToList();

    ViewBag.Graduacao = new SelectList(db.Graduacao, "Id", "Nome");

    return View("Index", resultados);
}

View:

     @using (Html.BeginForm("Pesquisa", "Graduacoes"))
     {

             @Html.DropDownList("Graduacoes")

         <button type="submit" id="pesquisar" class="btn btn-primary btn-xs">Buscar</button>
     }

Comment: Qual a forma correta de fazer essa view? Meu model está assim como vc descreveu e o fiz a consulta. Só que quando clico em "Pesquisar" retorna o erro: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Competências.Models.Graduacao]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Competências.Models.Curso]'.
Comment: Bom, o erro é claro. A primeira linha da *View* tem que ser `@model IEnumberable`, e não `@model IEnumberable`.
Comment: Desculpa, não era essa o erro. é: "There is no ViewData item of type 'IEnumerable' that has the key 'Graduacao'." apontando para meu dropdownlist que está "@Html.DropDownList("Graduacao")
Comment: O erro é porque tem uma declaração de `@Html.DropDownList` que não está sendo carregada corretamente. Experimente remover a declaração usando comentários `@* *@` e repita o teste.
Comment: Ele está carregando corretamente. Eu vejo as opções que eu quero na lista, escolho uma e ao pesquisar dá esse erro. Comentando a linha como vc pediu eu não visualizo o dropdownlist, só o botão pesquisar. E ao clicar nele, nada acontece.
Comment: Entendi. Na verdade é uma tela que lista os cursos baseado na graduação. Pode desfazer o comentário na DropDown. Vou melhorar a resposta.
Comment: Ta, estava usando duas actions com o mesmo nome e no seu exemplo vc colocou as duas numa só. ok, quando eu busco agora não dá erro, só não retorna nada. Eu tenho que inicializar o id não? (int id = 0)
Comment: Isso mesmo. O problema de inicializar o Id é que pode dar problema de ambiguidade no protótipo da `Action`. Melhor tratar o Id por dentro do método.
Comment: mas se eu não inicializar o id nem roda. Dá "The parameters dictionary contains a null entry for parameter 'id'" Deixei inicializado já. Tem algum problema na consulta. Não estou tendo retorno nenhum
Comment: Aí é só com debug.
Comment: Vou editar minha pergunta passando model, controller e view. Pode dar uma olhada? Já tentei de tudo e não consigo retornar nada com a pesquisa
Comment: @PFVictor Ok, fico aguardando você.
Comment: Pronto Cigano, editado!
Comment: Eu estava usando uma view só. No caso essa que vc passou vai ser a Index certo?
Comment: @PFVictor Continua sendo uma View só, mas usando duas `Actions` em `Controller` para a lógica.

Consulta ao banco de dados em C# mvc


When is $\Omega^1_{E/K} \otimes_E L \to \Omega^1_{L/K}$ injective?

Question: My question refers to an exercise from Bosch's "Commutative Algebra and Algebraic Geometry (page 355, Ex. 8.1.8).
Let $L/K$ be a field extension and $E$ an intermediate field. As we can interpret $E,L$ as $K$-algebras we obtain a short exact sequence
$$\Omega^1_{E/K} \otimes_E L \to \Omega^1_{L/K} \to \Omega^1_{L/E} \to 0$$
where we denote for a morphism $f: A \to B$ of $R$-algebras the $A$-module $\Omega^1_{B/A} $ of differential forms.
The aim is to verify that the left map
$$\Omega^1_{E/K} \otimes_E L \to \Omega^1_{L/K}$$
from the sequence is injective iff every $K$-derivation $E \to L$ admits an extension as a $K$-derivation $L \to L$.
I don't know how can I solve the problem.
My attempts: "$\Leftarrow$" is seems to work as following: Assume $d_E:E \to L$ is $K$-derivation. By assumption it extends to $d_L:L \to L$. Then $E \to L \to \Omega^1_{L/K}$ is also a $K$-derivation so factorizes over $\Omega^1_{E/K}$ by universal property of $\Omega^1_{E/K}$. But why does it imply that $\Omega^1_{E/K} \otimes_E L \to \Omega^1_{L/K}$ is already injective?
Other direction : no idea
Answer: You can use the fact that $L$ is an injective $L$-module (because $L$ is a field). This means that the functor $\operatorname{Hom}_L(.,L)$ is exact.
Let $N$ be the kernel of $\Omega^1_{E/K}\otimes_E L\to \Omega^1_{L/K}$. Apply the exact functor $\operatorname{Hom}_L(.,L)$ to get the exact sequence :
$$ 0\leftarrow\operatorname{Hom}_L(N,L)\leftarrow\operatorname{Hom}_L(\Omega^1_{E/K}\otimes_E L,L)\leftarrow\operatorname{Hom}_L(\Omega^1_{L/K},L)$$
Look at the map on the right, using extension of scalar adjunctions and universal properties, it is :
$$\operatorname{Der}_K(E,L)=\operatorname{Hom}_E(\Omega^1_{E/K},L)=\operatorname{Hom}_L(\Omega^1_{E/K}\otimes_E L,L)\leftarrow\operatorname{Hom}_L(\Omega^1_{L/K},L)=\operatorname{Der}_K(L,L)$$
And this map is simply the correspondence $D\mapsto D|_E$. Putting, all this together, we get an exact sequence :
$$0\leftarrow\operatorname{Hom}_L(N,L)\leftarrow\operatorname{Der}_K(E,L)\leftarrow\operatorname{Der}_L(L,L)$$
It follows that a $K$-derivation $E\to L$ extends to a $K$-derivation $L\to L$ iff its image in $\operatorname{Hom}_L(N,L)$ is zero. Hence, every $K$-derivation $E\to L$ extends iff $\operatorname{Der}_L(E,L)\to\operatorname{Hom}_L(N,L)$ is the zero-map, but this is possible iff $N=0$, in other words iff $\Omega^1_{E/K}\otimes_E L\to \Omega^1_{L/K}$ is injective.


Heroku rails 4 devise error

Question: I have a rails app that I am developing on heroku. My app works completely on my local machine, however on the heroku domain (myapp.herokuapp.com...) once I hit submit on my devise powered signup page I get redirected to a page "/users" (that doesn't even exist) and I get this error:
We're sorry, but something went wrong.

If you are the application owner check the logs for more information.

Of course the logs shows absolutely NOTHING beyond normal and is completely worthless.
I should be redirected to my index page with an notice for the new user to check their email for a confirmation link. The email is not sent nor am I redirected to the index page on heroku. However when I try this samething locally I get a confermation link and I am redirected like I am supposed to be.
There is no difference between the two (heroku and my localhost) code wise besides my database.yml, and gem file (switching between sqlite3 and not.)
Before setting up confirmable with devise, on my localhost I was able to go to the login page and login with my newly created password/username. However on heroku (before setting up confirmable with devise) I was STILL redirected to the /users page but this time I was able to manually type in the index login page URL and login with my password/user name that I just created.
Why is devise (or whatever) redirecting me to /users? I have been stuck on this issus for two weeks, please help. Or at least point me to the right direction on where I can get hits on what is causing this issue. Thank you in advance!
UPDATE
heroku run rake db:migrate

Works as it should. But I am still having the error. So the rake did nothing.
Comment: Have you run your migrations on heroku? `heroku run rake db:migrate`
Comment: This answer helped me figure it out: http://stackoverflow.com/questions/11601484/rails-error-in-production-mode
Comment: Yes, the rake seems to be working, but I am still getting the same errors.  So the db:migrate didn't fix anything
Answer: Did you set up email sending in Heroku? For example a Sendgrid addons.
And for the redirecting: How To: redirect to a specific page on successful sign_in, sign_out, and or sign_up in Devise
Comment: Yes I did, I am using mailtrap. Thanks for the other link. However I have tried after_inactive_sign_up_path_for in the past and I still am getting the same error. Thanks

Heroku rails 4 devise error


Using symmetry to evaluate integral

Question: I have this integral:
$$\int\int_D(x^3y^2+\ln(x^2+x+1)\sin(y^3))dA$$
Where $D: x^2+y^2 \le 1$
I was wondering what the best way to solve this using symmetry rather than using calculations? Any hints or tips would be appreciated!
Answer: The function  $$f(x,y) =(x^3y^2) +(\ln(x^2+x+1)\sin(y^3)) $$ The first part namely $f_1(x,y)=(x^3y^2)$ satisfies  $$f_1(x,y)=-f_1(-x,y)$$
The second part namely $$f_2(x,y) =  (\ln(x^2+x+1)\sin(y^3))$$ satisfies $$f_2(x,y)=-f_1(x,-y)$$
Since the region is symmetric with respect to both $x-axis$ and $y-axis$  the positive and negative contributions cancel each other and you get $$\int\int_D(x^3y^2+\ln(x^2+x+1)\sin(y^3))dA=0$$
Comment: Your first statement is not correct, right? $f(x,-y)\neq f(x,y)$ in general (note the first term doesn't change sign with $y$ but the second term does, since $y^2$ is even but $\sin y^3$ is odd). Is that really what you meant?
Comment: @MPW Thanks for the comment. Please check the new version of my solution.
Answer: We have that

$g_1(x,y):=f(x,y)=x^3y^2+\ln(x^2+x+1)\sin(y^3)$
$g_2(x,y):=f(-x,-y)=-x^3y^2-\ln(x^2-x+1)\sin(y^3)$
$g_3(x,y):=f(-x,y)=-x^3y^2+\ln(x^2-x+1)\sin(y^3)$
$g_4(x,y):=f(x,-y)=x^3y^2-\ln(x^2+x+1)\sin(y^3)$

then divide $D$ in $4$ sectors to obtain
$$\int\int_Df(x,y)dA=\\=\int\int_{D_1} g_1(x,y)dA+
\int\int_{D_2} g_2(x,y)dA+\int\int_{D_3} g_3(x,y)dA+\int\int_{D_4} g_4(x,y)dA$$
Comment: I see that we can use the union of all of these, but does this make it any easier because the double integral of each one is quite long
Comment: @JamesO'dare Note that the sum $g_1+g_2+g_3+g_4=0$.
Comment: Got it thank you!

Using symmetry to evaluate integral


How to save automatically the current logged in user to domain with Grails?

Question: I've a question about Grails (using version 2.3.7).
I would like to save the username of the current logged in user (I'm using the spring security core plugin) to all my domains, so the user can only see/edit his own data if he logs in. To filter the content by user it is necessary to save the username with the data to the database.
I didn't find an example how to do this in an easy and appropriate way.
It is not really necessary to add the username manually on every single Controller and in every method?? (save, edit etc.)
Thanks for any help, sbr
EDIT: What I need is something that is described here: http://www.javacodegeeks.com/2013/11/getting-started-with-method-security-in-grails-using-spring-security.html
So I extended my Domain-Class with "static belongsTo = [author: User]" and the User-Class with "static hasMany = [notes: Note]". But now when I want to save a new Domain I receive the error that the author cannot be null. So HOW to fill this property with the current logged in user?
Comment: I suggest you look at the ACL plugin. It's a lot of reading but it's designed to do exactly what you are looking to do. https://grails.org/plugin/spring-security-acl
Comment: thanks I already had a look on this plugin and I had the impression it is too big and mighty for my needs. Isn't it possible to define a default value with the user on the domain class?
Or if I really need the ACL plugin, does anyone know a tutorial for this issue?
Comment: If you really want to head down that road then this will help put you on the right path: http://stackoverflow.com/questions/6984031/grails-adding-custom-fields-columns-to-all-domain-objects-automatically
Comment: okay I dont't think that I want to do meta-programming.
I'm just wondering that there is no smaller solution. Do you know how to configure the ACL plugin for my needs?
Comment: I don't know how to configure the ACL plugin for your needs, but you could always use the suggestion in the link I provided you of extending a base class for all your domains which would include the username. If you like I can try and write up an example as an answer. Be aware though, this approach will prevent you from extending your classes any further. Since a class can only extend one class. Even then though, you will have to deal with filtering on your own. Which could be done using the hibernate filters plugin. I think.
Comment: what happened to the spring security core alone ?
Comment: how do you mean "spring security core alone"?
Comment: okay I think I have to use the ACL Plugin. But the only example application I found is the official here (http://grails-plugins.github.io/grails-spring-security-acl/docs/manual.111/guide/4.%20Sample%20Application.html). But I can't figure out what the username and password are to test it. And I think this example is to big for my needs.
Does anyone has a sample application or a tutorial? Thanks.
Answer:
I would like to save the username of the current logged in user (I'm using the spring security core plugin) to all my domains, so the user can only see/edit his own data if he logs in.

It sounds like you were considering adding the username to the domain classes owned by a user? This is not a good approach, you should instead add an association between User and these domain class.

But now when I want to save a new Domain I receive the error that the author cannot be null. So HOW to fill this property with the current logged in user?

class NoteService {
  SpringSecurityService springSecurityService

  Note saveNote(Note note) {
    User currentUser = springSecurityService.currentUser
    currentUser.addToNotes(note)
    note.save()
  }
}


How to display panels region title?

Question: How can I say Panels to show region title while rendering the page?
I have a panel region containing 4 node previews and I want to print a title before them. It would be like a "wrapping section" in my home page.
Is it possible?
Answer: You can create a template for your panel and add whatever div or wrapper you need there, by wrapping it around the content div. This post is a great resource:
https://www.drupal.org/node/1678810
In brief, for panel panes, your template file should be named:
panels-pane--[pane type]--[pane subtype].tpl.php
This post also shows you how to find your panel's machine name.
If you aren't familiar with templates, this is also a great resource:
https://www.drupal.org/documentation/theme
Comment: As my need was a title for the region and not for a single pane (I have a region with multiple node teasers in it), I started from your idea and I created a custom pane as ctools plugin with a configurable string as parameter. In this way I can place a region title mostly everywhere.
Comment: nice! feel free to share your final solution code for others to enjoy :)

How to display panels region title?


Harmonic functions in the half-plane

Question: Denote by $\mathbb{H}$ the upper half-plane
$$
\mathbb{H} := \left\{ x \in \mathbb{R}^n : x_n > 0\right\}.
$$
Suppose that $u \in C^2(\mathbb{H}) \cap C(\bar{\mathbb{H}})$ is a bounded harmonic function such that $u \leq 0$ on $\partial\mathbb{H} = \{ x_n = 0\}$. Is it possible to conclude that $u \leq 0$ in all of $\mathbb{H}$? I know this is the case for $n = 2$ but am unable to establish the general case $n \geq 3$.
Answer: That's not true in $n=2$. For example, $u(x,y)=y$ is harmonic in $\mathbb{R}^2$, and positive for $y > 0$, even though $u(x,0) \le 0$. And it's not true in $\mathbb{R}^n$ for the same reason.
By assuming that $u$ is bounded, then you get the Poisson integral representation of $u$ from its boundary function, and that will give you what you want.
Comment: @DisintegratingByParts Is there an approach that would also work for more general domains in $\mathbb{R}^n$?
Comment: Yes but $u(x,y) = y$ is unbounded in $\mathbb{H}$. I think the boundedness assumption will be necessary somewhere.
Comment: @ImNotThereRightNow : Yes, you'll need further assumptions if you want the conclusion that you hoped for. I believe my answer is correct for the question that you asked.
Comment: Thanks but I'm a little confused now. How does this work with regards to my question? I'm assuming that $u$ is bounded.
Comment: @ImNotThereRightNow_ : Ah, I missed your assumption of boundedness in the question. Boundedness will give a Poisson integral representation, which will give you what you want.
Comment: @MathUser_NotPrime : The approach I gave you works in $\mathbb{R}^n$.
Comment: @DisintegratingByParts Well in all of $\mathbb{R}^n$ we can just apply Liouville's theorem to see that the function is actually constant. I was wondering about domains $U\subsetneq \mathbb{R}^n$
Comment: @MathUser_NotPrime : You just need the assumption of boundedness in the half-space.
Comment: @DisintegratingByParts Thanks. I posted my question [here](https://math.stackexchange.com/questions/3026701/a-maximum-principle-for-bounded-functions-in-unbounded-domain) if you want to take a look!

Harmonic functions in the half-plane


Difference between char pointer and pointer to char?

Question: I want to know the difference between char pointer and pointer to char..?
Are they same? If no what is the difference?
Comment: I'd say "same".  If you're in C++ land, using a "const" qualifier, then there are subtle distinctions in your declaration for "pointer is const" vs. "what's pointed at is const".  But as far as your question: the two terms are equivalent.  IMHO..
Answer: I think there is no difference between a "pointer to char" and a "char pointer".
char *p;

Both are just different ways to designate a pointer to char type.
Answer: This question might make more sense if you backed it up with example code.  As far as I'm concerned they mean the same thing, but I only ever think of "char pointer".
What I think you might mean is this:
char c = 'A';
char *pc = &c;            // <-- "pointer to char"?
char *p = new char [50];  // <-- "char pointer"?

In fact, pc and p above are both char pointers.  They can both be used as arrays, although addressing anything other than the zeroth element of pc is going to cause problems.
Answer: char pointer and pointer to a char are same i.e. char *ptr;
but, if you mean pointer to char array then their is a difference.
pointer to char array is declared as char (*ptr)[n] which stores the address of an char array of size n.
Answer: There is no difference between the char pointer and pointer to char. Both are same. Normally text strings are represented in C by arrays of characters. Since arrays can be easily be manipulated by pointers it is often used.
Answer: The confusion arises when you replace char with double.  People colloquially use double pointer to mean pointer to pointer to some type, not pointer to double type.
Answer: There is no difference.
They are just written in a different way.
Comment: Why would they be written differently? Or you mean 'it's an alternative expression'?


$k$-algebra of differential operators with coefficients in $k(t)$, does ring have any zero divisors?

Question: Let $A = k(t)\langle {d\over{dt}}\rangle$ be the $k$-algebra of differential operators with coefficients in $k(t)$. Thus, an element of $A$ is an expression$$F = f_n {{d^n}\over{dt^n}} + f_{n - 1} {{d^{n - 1}}\over{dt^{n - 1}}} + \ldots + f_1 {d\over{dt}} + f_0, \quad f_i \in k(t), \quad i = 1, \ldots, n,$$thought of as an operator $k(t) \to k(t)$. The sum $F + G$ of differential operators $F$ and $G$, is defined by taking the sum of corresponding coefficients $f_k$ and $g_k$, respectively, in front of ${{d^k}\over{dt^k}}$. Multiplication in the ring $k(t)\langle {d\over{dt}}\rangle$ is defined as the composition of differential operators (it is easy to see that such a composition is a differential operator again).
One has a ring imbedding $k(t) \hookrightarrow A= k(t)\langle {d\over{dt}}\rangle$, where a function $f \in k(t)$ is identified with the multiplication operator $m_f : g \mapsto f \cdot g$. Note that for $f \in k(t)$, we have$${d\over{dt}} \circ m_f - m_f \circ {d\over{dt}} = m_{f'}, \quad \text{where }f' := {{df}\over{dt}}.$$So, the elements $f$ and ${d\over{dt}}$ do not commute in $A$, in general.
Question. Does the ring $A$ have any zero divisors?
Answer: First we will assume that $k$ has characteristic zero.
$A$ acts linearly on the ring $k[[t]][t^{-1}]$ of formal Laurent series with coefficients in $k$ (where we allow only finitely many negative powers). This ring is an infinite dimensional vector space over $k$. Each differential operator, being linear in this action, has a kernel. This kernel is always finite dimensional for nonzero operators (this is the part that fails in positive characteristic). Thus the kernel of the product of two nonzero operators is finite dimensional, and in particular the product can never be zero.
We can now use this to prove the result in characteristic $p$. It is sufficient to prove this for the prime subfield $\mathbb{F}_p$ because taking the tensor product with the full field gives us the result.
Let $k=\mathbb{Q}$, and let $P$ be the subring of $k(t)$ consisting of all quotients of polynomials with integer coefficients where the denominator is monic. The result holds for $A_P$, the subring we get when we take coefficients in $P$ instead of $k(t)$. The advantage is that $P$ admits a surjective homomorphism onto $\mathbb{F}_p(t)$, which can be realized by taking the tensor product $\mathbb{F}_p\otimes P$ over $\mathbb{Z}$. Similarly we have $\mathbb{F}_p\otimes A_P\simeq A_{\mathbb{F}_p}$ as rings, and since $p$ is prime and the kernel consists of all multiples of $p$ we have that $A_{\mathbb{F}_p}$ remains a domain.
Comment: Rational functions don't behave well under tensor product, I am afraid. But the approach is nice.
Comment: @darijgrinberg Do you know a reference on that I could look at? I thought I'd be able to escape it but I haven't done anything academic with math in two years and I'm getting rusty.
Comment: I suspect Etingof has done proofs like this, but I am not sure where exactly .
Comment: @darijgrinberg I meant on the pathological behavior of rational functions under tensor product.
Comment: Well, you won't get $1/(x-\pi) $ from $\mathbb Q \left (x\right) \otimes \mathbb R $. If I remember right, there is even a proof of hilbert 's Nullstellensatz based on this sort of pathologies.
Comment: @darijgrinberg Sure, of course, but taking the tensor product with $\mathbb F_p$ over $\mathbb Z$ should work fine. If $x$ and $y$ differ by a multiple of $p$, so do $1/x$ and $1/y$.
Comment: @darij But only in this special case of course because you're only inverting monic polynomials. In particular $p$ is still a prime element.