Change font size with JavaScript

Question: I have this code to change the font size of a paragraph with 3 buttons. I would like to know which method can I use to not repeat the same lines and make this code more compact.
window.addEventListener('load', inicio, false);

function inicio(){
var boton1 = document.getElementById('boton1');
var boton2 = document.getElementById('boton2');
var boton3 = document.getElementById('boton3');

boton1.addEventListener('click', fuente10, false);
boton2.addEventListener('click', fuente13, false);
boton3.addEventListener('click', fuente20, false);
}

function fuente10(){
var parrafo = document.getElementById('parrafo');
parrafo.style.fontSize='10px'
}

function fuente13(){
var parrafo = document.getElementById('parrafo');
parrafo.style.fontSize='13px'
}

function fuente20(){
var parrafo = document.getElementById('parrafo');
parrafo.style.fontSize='20px'
}

I was thinking in a for loop but I can't figure it out how to do it :/
Comment: Why don't you use jQuery? It's way too easier to achieve this using jQuery!
Comment: @RutwickGangurde - jQuery is not needed to do something this basic.
Comment: im just triying to fully understand javascript to get better at programing, just learning :)
Comment: Try: http://jsfiddle.net/userdude/tR9dV/
Comment: @JhonnatanGonzalezRodriguez thats the correct approach. You should know the fundamentals before playing with something build on top of it.
Answer: Try something like this
window.addEventListener('load', inicio, false);

function inicio(){
var boton1 = document.getElementById('boton1');
var boton2 = document.getElementById('boton2');
var boton3 = document.getElementById('boton3');

boton1.addEventListener('click', function() { setFont("10px");}, false);
boton2.addEventListener('click', function() { setFont("13px");}, false);
boton3.addEventListener('click', function() { setFont("20px");}, false);
}

function setFont(value){
   var parrafo = document.getElementById('parrafo');
   parrafo.style.fontSize= value;
}

Update
A different approch (not tested, but should work)
    window.addEventListener('load', inicio, false);
function inicio() {
    var fontDetails = {"boton1" : "10px" , "boton2" : "13px" , "boton3" : "20px"};
    var parrafo = document.getElementById('parrafo');
    var domElement  = "";
    var element  = "";

    for (element in fontDetails)
    {
        domElement = document.getElementById(element);
        domElement.addEventListener('click', function() { parrafo.style.fontSize= fontDetails[element]; }, false);
    }
}

You can also remove the window load event handler and function inicio and try self executing function like this
(function() { //your code here } )();

Comment: This works really good, its is not bad to make anonym functions to call the setFont function?
Comment: No issues AFAIK, we can call like that.
Comment: I have updated another approach, hope thats useful. The beauty of the new approach is that you can add new button and font size in the fontDetails objects without modifing anyother section.
Comment: Technically... For each anonymous function you're creating, there's excess memory required beyond the first created, which may cause memory leaks to occur. If there's multiple attachments for the same function, use a declared function instead.
Answer: Maybe this change can help you:
    window.addEventListener('load', inicio, false);

    var boton;

    function inicio()
    {
        modificar('boton1', 10);
        modificar('boton2', 13);
        modificar('boton3', 20);
    }

    function modificar(id, size)
    {
        boton = document.getElementById(id);
        boton.onclick=function(){fuente(size)};
        console.log(boton);
    }

    function fuente(size)
    {
        var parrafo = document.getElementById('parrafo');
        parrafo.style.fontSize=size+'px';
    }

Comment: This ddnt worked for me it seems like when the window load it aitomatically set fon size 20 to ell the paragraphs, and buttons doesnt work :/

Change font size with JavaScript


Null.point.exception ao executar método de outra classe

Question: Estou executando este código:
classe de execução
package aloiexecv2;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *
 * @author root
 */
public class test {

    private static Variaveis variaveis;
    private static Dados dados;
    private static Cadastrar cadastrar;

    public static void main(String[] args) throws FileNotFoundException, IOException {
        int opcao;
        System.out.println("\n----------CADASTRO DE ROTINAS DE INICIALIZAÇÃO----------");
        System.out.println("Neste script você pode adicionar comandos para inicializar rotinas (no formato: startx ou xcalic -c) ou");
        System.out.println("Pode acrescentar o atalho para um programa que será executado (no formato: /home/usuario/programa)");
        System.out.println("Escolha sua opção:");
        System.out.println("1) Cadastrar rotina ou programa");
        System.out.println("2) Excluir script");
        System.out.println("3) Pré-visualizar arquivo do incialização");
        System.out.println("4) Sair");
        opcao = Variaveis.getENTRADA().nextInt();
        if (opcao >= 1 && opcao <= 4) {
            **cadastrar.CadRotina(opcao);**

        } else {
            System.out.println("Opcao inválida! Digite novamente");
        }
    }

}

Metodo da Classe Cadastrar:
public void CadRotina(String arquivo, String texto, int opcao) throws FileNotFoundException, IOException {
    this.arquivo = arquivo;
    this.texto = texto;
    this.opcao = opcao;
    dados.gravarNoArquivo(arquivo, texto);
}
public void CadRotina(int opcao2) throws FileNotFoundException, IOException {
    this.opcao = opcao2;
    switch (opcao2) {
        case 1:
            if (arquivo == null) {
                System.out.println("\n----------INFORMANDO AS ROTINAS DE INICIALIZAÇÃO----------");
                System.out.println("Cadastre primeiramente o nome do arquivo que recebera as rotinas");
                this.arquivo = Variaveis.getENTRADA().nextLine();
                System.out.println("Agora, informe o caminho do script que devera ser executado automaticamente(Ex.: /home/scriptinternet: \n Ou adicione uma linha de comando (ex. xcalib - c)");
                this.texto = Variaveis.getENTRADA().nextLine();
                dados.gravarNoArquivo(arquivo, texto);

            }
    }
}

E ele apresenta o seguinte erro:

Exception in thread "main" java.lang.NullPointerException     at
  aloiexecv2.test.main(test.java:34)
  /root/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned:
  1 BUILD FAILED (total time: 3 seconds)**

A linha 34 refere-se ao método CadRotina(opcao). Aparentemente o java ta dizendo que o método está vazio, mas já testei a variável opção e ela esta sendo setada no Scanner.
Fiz uma sobrecarga de método para que pudesse utilizar o CadRotina tanto com a variavel opcao quanto para execução direta (gravar arquivo, texto).
Não entendo por quê não executa. Alguem pode me ajudar?
Comment: possível duplicata de https://pt.stackoverflow.com/questions/51604/problemas-ao-executar-classe-em-java
Comment: `cadastrar` não foi instanciado
Comment: Relacionada: [O que é a NullPointerException e quais são suas principais causas?](https://pt.stackoverflow.com/questions/63617/o-que-%C3%A9-a-nullpointerexception-e-quais-s%C3%A3o-suas-principais-causas)
Comment: Relacionada: [Programa para criação de script de inicialização java (dica)](https://pt.stackoverflow.com/q/217187/132)
Answer: 1) Use a referência ao invés do tipo:
opcao = Variaveis.getENTRADA().nextInt();

Troque por:
opcao = variaveis.getENTRADA().nextInt();

2) Estático não gera objeto:
private static Variaveis variaveis;
private static Dados dados;
private static Cadastrar cadastrar;

Troque por:
private Variaveis variaveis;
private Dados dados;
private Cadastrar cadastrar;


Next page by selecting radio buttons not with clicking next page

Question: what i want is this when i click on the radio buttons it takes me to the next page without me actually clicking on the next page.
At the moment i have to click on next page
Application form link: http://goo.gl/CNgJq
I have written this code but it doesn't seem to work:
(function ($, Drupal, window, document, undefined) {
    $(document).ready(function() {
        $('.form-radio').click(function() {
            $('.next-tab mover').click();
        });
    });
})(jQuery, Drupal, this, this.document);

Can anyone tell me what am I doing wrong with this code?
Answer: Test case: http://jsbin.com/acopuj/1/edit
$('.next-tab mover') // <---- that's wrong

should be:
$('.next-tab.mover') // the button has both classes, add '.' and remove 'space'

Answer: Looking at your page, you need to change your selector to $('.next-tab.mover'). This will find the element with both the next-tab and mover classes.
$('.form-radio').click(function() { $('.next-tab.mover').click(); });

Comment: It's not... I didn't get a notification that there was already an answer as I was typing mine
Answer: You could try this one
$('.form-radio').on('click', function() {
    $('.next-tab.mover').trigger('click);
});


Count of union two HashSet

Question: There are two HashSet sequences. I need to count the result of union.
It should look like this:
    sequence1.UnionWith(sequence2);
    return sequence1.Count;

But I need to ensure that the original sequences do not change.
I used a simple search, but it very slows down the program
    var newHashkSet = new HashSet<string>(sequence1);
    foreach (var s2 in sequence2.Where(s2 => newHashkSet.SingleOrDefault(x => x == s2) == null)) {
        newHashkSet.Add(s2);
    }
    return newHashkSet.Count();

Is there a more elegant solution?
Comment: What do you mean by _"the original sequences do not change"_? Hashsets are unordered by definition. See also [Does HashSet preserve insertion order?](http://stackoverflow.com/questions/657263/does-hashset-preserve-insertion-order).
Comment: I mean that after using   sequence1.UnionWith(sequence2) sequence1 is changing
Answer: Use an ImmutableHashSet.
https://msdn.microsoft.com/en-us/library/dn467171(v=vs.111).aspx
By definition, immutable hash sets do not change. When you form the union of two immutable hash sets, the result is a third immutable hash set, not the mutation of either of the inputs.
Answer: Another quick but not memory efficient way is that You can create a copy of hashSet and then do the union
var newset = new HashSet<string>(sequence1);
newset.UnionWith(sequence2);
netset.count

Count of union two HashSet


Redirect a node to it's custom view

Question: I've created a view "myview" for a custom content-type.
Now I want: Any node of that content-type to be automatically redirected to that view page.
For example, if a user types: http://my-drupal-site/node/4 and that node is of my desired content-type, it needs to be redirected to http://my-drupal-site/myview/4
What I can do:
What I can do now is writing in my theme's template_preprocess_node() function following lines:
function myCustomTheme_preprocess_node(&$variables) {
    $arg1 = (int) arg(1);
    $isArg1Int = (is_int($arg1) && $arg1) ? (true) : (false);

    if (isset($variables['node']->type) && $isArg1Int) {
        if ($variables['node']->type == "MY-DESIRED-CONTENT-TYPE") {
            // REDIRECT!
            drupal_goto('myview/' . $variables['node']->nid);
        }
    }
}

That's pure hard-code, and I don't like it. Is there some elegant solution, may be using URL aliases or something like that?
Answer: You can use Panels like Patrick suggests, but Panels requires a lot of resources and it's a bit of a waste to only use it for this simple case.
There are different approaches to this, one could be to react to the full view of the node:
/**
 * Implements hook_node_view().
 */
function hook_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'type' && $view_mode == 'full' && arg(0) == 'node') {
    drupal_goto('url/' . $node->nid);
  }
}

The if statement if true if the node is of the correct type, is being view in full view and the url is node/..., which should limit to node being view the standard way.
Another way of doing this, would be to use the redirect module or something similar to setup redirects for all nodes of the type to the view. A weakness to this approach could be path alias. But that way you would make a permanent redirect which is more SEO friendly.
Comment: @giga never ever use your theme for logic like this. If you want to avoide code you could use redirect, but then panels would be better since its code free. The price you pay here is performance (RAM needed by the server).
Comment: Thanks Googletorp for the comment, however, to me it seems similar to the 'template_preprocess_node' approach I mentioned in my question - & I'm looking for solution which does not involve coding this way. However, can you say performance related issues on this approach?
Answer: You can definitely do this in Panels by adding a variant to the default node display override and then a selection rule for "content type = my_content_type."
You may also be able to just use Page Manager, part of Ctools and a dependency of Panels, to set this up but I'm not sure how to do it off the top of my head.
Node One has an excellent screencast on learning Page Manager and Panels.
Comment: I don't have panels module installed. Shouldn't it'll be an overkill to install a module like that (I'm assuming it's a heavy-weight module capable of doing many other things) to redirect? I was thinking something like `Path` or Url Aliases
Comment: Use pathauto to set a special content type for nodes of that content type (e.g., node/content_type_x/%).  Of course, this approach is not very good for SEO if you have already launched your site, as it will break URLs, but you can mitigate this somewhat using the http://drupal.org/project/redirect module.
Comment: If you are using pathauto, etc. to set your URLs, have you tried just overriding the path in views?  For an example of this, look at the "Taxonomy term" view which is installed with views by default.
Comment: Thanks Patrick. That works in general. Say, I can have a path like 'node/%' (% is Node-ID) in my view for a custom content-type, but doing this results in: for OTHER content-types `node/%` shows "Page not found". Because, view for my custom content-type registers it's path in `node/%` :(

Redirect a node to it's custom view


How can I make individual nodes fieldable?

Question: One of the great things about the entity and field APIs is the ability to add fields to any entity and subsequently any bundle.
Is there a way, using the Field API or the Entity API, to make fieldable nodes, field instances attached to individual nodes instead of content types?
If there isn't, is there any work being done using other concepts that would achieve the same result? The closest I can think of is the Webform module, but I'm sure it's not intended to be a way to create content, and it doesn't take advantage of the field API.
Answer: You could use something like http://drupal.org/project/conditional_fields but that's all done from a front-end point of view, which means Drupal doesn't really know about your structure.
http://drupal.org/project/properties looks interesting and may suit depending on your needs.
I had a similar issue recently and decided to split up into three separate content types instead of just one.
Answer: It's hard to say no it's not possible, as I might be missing something. But based on the data stored for fields and instances of fields, I'd say the current setup doesn't support this.
The best solution I can think off, would be to create a special field, that would allow for values of different kinds, much like you can add several images using the same field. I'm not sure if doing something like that would be a good idea, it would certainly be complex and time consuming, and probably couldn't use the code for all the fields that already have been defined.
Answer: I did this by attaching entities to nodes on creation.

Create an entity type: I used contrib's Entity API as the framework for doing this.
Create a custom node content type.
Create an implementation of hook_node_insert() for your custom content type that creates a bundle for the entity type you created in step 1.

At this point, the node is technically fieldable: you can do any number of things, whether its create fields programmatically or implement some sort of UI to let users add their own fields.
There are two gotchas with this approach:

Since the node and entity type are two separate structures, you need to make sure the bundle you create is linked to the node: there's some promising work being done with the Relation module, but I opted for just adding a property to the entity bundle that stores the node ID (you could also store the bundle ID in the node if you wanted), then loading the bundle information on hook_node_load().
Versioning bundles is heavy wizardry I chose not to get into: there is no API for it, and while in theory if you created a record of every change you made to the bundle it should work, it might possibly instead awaken the Old Gods. I opted for turning off revision support on the nodes I made fieldable this way.


IE8 horizontal scrollbar problem

Question: I have an IE8 buggy horizontal scrollbar problem,
similar to this:
DIV with overflow:auto and a 100% wide table
(unfortunately the solution suggested there (with zoom=1) does not work here,
or I don't know how to apply)
the horizontal scrollbar should not appear
(it does not appear in FF or Chrome
but it appears in IE8)
the sample code, with CSS tables:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

the same problem, with absolute positioning:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

the "overflow-x: hidden" solution is not good, because it may hide some content;
padding-right may work, but that's too dirty hack (how many pixels? what happens in other browsers? what if the user magnifies the page?)
Comment: I would like a CSS-only solution (no javascript)
Comment: I posted another question, that generalizes this issue; please check out, and lets go over to there: http://stackoverflow.com/questions/5554137/general-container-ie8-horizontal-scrollbar-problem
Answer: Setting the display:inline-block; for the div that contains the red box

Comment: this is one of the possible solutions; `float: left` and `position: absolute` are also OK
Comment: Agree, he mentioned the issue with IE8, so i just try it there, if he needs compatibility with IE7/6,  it might not work, I didn't test the bug there so not really sure.
Comment: thanks for the incoming, with the floated solution did you need to clear the element, didn't you?
Comment: no, I did not have to clear it. there are two little problems with this solution: 1: I can not use `overflow-y: auto`, because it causes horizontal scrollbar in FF and Chrome; 2: in IE8 the vertical scrollbar goes "outside" of the box (in FF and Chrome it is inside; it's also inside in IE8 without this inline-block/float/position:absolute thing)
Answer: With your first example, if you move the width:30em from the inner most DIV to it's parent the horizontal scrollbar no longer appears in IE8 and still functions properly in other browsers:
HTML:

Content goes here.

Live Demo: http://jsfiddle.net/rev7J/
Comment: The same will work for your second example (the absolute positioned DIVs).
Comment: I did not tell the whole story: I try to make a general container, and the red box is just a sample content in it; so we should not poke with the red box. I posted another question, that generalizes this issue; please check out, and lets go over to there: http://stackoverflow.com/questions/5554137/general-container-ie8-horizontal-scrollbar-problem
Comment: @slobo, so are you able to mark one of the answers correct that suit your listed question then?
Answer: What you need to do is change the overflow value from "scroll" to "auto". Scroll gives both horizontal and vertical bars whether you need them or not. Also IE for some reason requires the innermost box to be slightly smaller than its outer boxes for this to work correctly. Finally that means if you want to change your background do so in an outer Div rather than inside an inner one.
See the solution to your inquiry below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

Comment: Following on from your above answer, you don't even have to set a width or height to the #innerMost DIV - just add some padding for styling: http://jsfiddle.net/RnHs2/ Seems to work well cross-browser.
Comment: I did not tell the whole story: I try to make a general container, that is vertically constrained, but can grow horizontally. I posted another question; please check out, and lets go over to there: http://stackoverflow.com/questions/5554137/general-container-ie8-horizontal-scrollbar-problem
Answer: add:
 style="max-height:none\9;"

IE8 horizontal scrollbar problem


Configure Cntlm to use no proxy if none are available

Question: I am behind a corporate firewall in the day, and on an open internet connection at night. I have configured cntlm to forward to my corporate proxy, but when I connect from an open connection, I must re-configure my proxy settings.
Is there a way to make cntlm fallback to using no proxy when there is none available?
# List of parent proxies to use. More proxies can be defined
# one per line in format <proxy_ip>:<proxy_port>
#
Proxy       10.73.55.44:80
# <~~ something here to fallback to a direct connection when no proxy is available

Answer: cntlm has this on it's roadmap, but I think I solved this problem in a simple way using  squid:
1. Install squid on the same machine as cntlm, on port 3129 (just change 3128 to 3129 in squid.conf)
2. Add "Proxy  localhost:3129" to the cntlm proxy list
That's it.
Hope this helps.
Comment: This works flawlessly.
Comment: Where is this on the roadmap? Or is it implemented by now?
Answer: This solution rewrites the relevant part of the cntlm.conf based on your current IP address: http://mirkoweber.blogspot.hu/2011/10/using-cntlm-in-changing-environment.html
#!/bin/sh
case "$2" in
    up|vpn-up)
          IP=`/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
          case $IP in
             172.*)
                logger "setNoProxy.sh -set NoProxy for enterprise net"
                NoProxy="NoProxy         localhost, 127.0.0.*, 10.*, 192.168.*,enterprise-intranet"
                ;;
               *)
                logger "setNoProxy.sh - set NoProxy for direct net"
                NoProxy="NoProxy *"
                ;;
            esac
         sudo sed "s/^NoProxy.*$/$NoProxy/g" -i /etc/cntlm.conf
         sudo service cntlm restart
         ;;
esac

Comment: Please edit your answer to describe or at least summarize the solution  here. External pages can disappear at any time, and we prefer not to rely on them.


AWS AppSync returns null on query where lambda function actually returns result

Question: I have a AppSync API that is linked to a lambda function as data source, the lambda function is calling another API to retrieve data from Database.
However after I setup the schema and resolver as well as the lambda function, it kept return me null result despite I have the actual result in my lambda function. (I printed it in logs so I can see it)
Here is my schema:
scalar AWSDateTime

type Event implements Node {
    appId: String!
    description: String
    eventName: String!
    id: String!
    nodeId: ID!
    ts: AWSDateTime!
}

input EventCondition {
    appId: String
    description: String
    eventName: String
    id: String
    ts: AWSDateTime
}

type EventsConnection {
    nodes: [Event!]!
    totalCount: Int!
}

enum EventsOrderBy {
    APP_ID_ASC
    APP_ID_DESC
    DESCRIPTION_ASC
    DESCRIPTION_DESC
    EVENT_NAME_ASC
    EVENT_NAME_DESC
    ID_ASC
    ID_DESC
    NATURAL
    PRIMARY_KEY_ASC
    PRIMARY_KEY_DESC
}

interface Node {
    nodeId: ID!
}

type Query implements Node {
    event(id: String!): Event
    eventByNodeId(nodeId: ID!): Event
    events(
        condition: EventCondition,
        first: Int,
        last: Int,
        offset: Int,
        orderBy: [EventsOrderBy!]
    ): EventsConnection
    node(nodeId: ID!): Node
    nodeId: ID!
    query: Query!
}

Here is my resolver request mapping for events, I hardcoded it so I can test:
{
    "version" : "2017-02-28",
    "operation": "Invoke",
    "payload": {
        "fieldName": "events",
        "parentTypeName": "query",
        "selectionSetGraphQL": "{\n nodes\n {\n  id\n  appId\n  eventName\n }\n}"
    }
}

Here is my response mapping template:
$util.toJson($context.result)

I expect it directly returns what Lambda Function return which is an object, but it is not. Here is what my lambda function have:
    const client = new GraphQLClient(eventsGraphQLURI, { headers: {} })

    client.request(request).then(
        (data) => {
            response = data;
            console.log(`Result: ${JSON.stringify(response)}`)
        })
        .catch((error) => {
            console.log(`Error Occurred: ${error}`)
    })

    return response;

When I check the logs, this is what the API return to my Lambda Function:
    {
        "events": {
            "nodes": [
                {
                    "id": "1e8601cf-a740-4146-a9b7-a38201a89eab",
                    "appId": "2fa39c1e-5b40-4a77-bab3-d22a79f051a7",
                    "eventName": "TemperatureDetection"
                },
                {
                    "id": "2645538",
                    "appId": "cc472578-2389-40ea-8525-a2b86fea23c4",
                    "eventName": "xxx"
                }
            ]
        }
    }

However when I run query on AppSync console, this is what it returns:
{
  "data": {
    "events": null
  }
}

Is there anything that I have done wrong so it will not recognized? When I go and check the logs of the AppSync, there is not any response.context log. All I have for response after the request in the log is as below image:

Deeply appreciates if anyone could help with this, I struggled for like two days on this. Thank you!
Answer: Okay totally overlook on my side. If anyone was wondering the same issue, check the lambda function, it needs to be async and also return the response in a callback.
Here are my new lambda function code that works:
exports.handler = async (event, context, callback) => {
    const client = new GraphQLClient(eventsGraphQLURI, { headers: {} })

    await client.request(request).then(
        (data) => {
            response = data;
            console.log(`Result: ${JSON.stringify(response)}`)
        })
        .catch((error) => {
            console.log(`Error Occurred: ${error}`)
    })

    callback(null, response);
}


Acrescentar X dias em uma data já predefinida no MySQL

Question: Preciso criar uma data limite, uma data de expiração de acesso
no caso estou utilizando pra captar o hora atual o
"criado" datetime DEFAULT CURRENT_TIMESTAMP,

Onde utilizo o CURRENT_TIMESTAMP pra conseguir da hora da gravação no MySQL, depois do criado tbm tem o "EXPIRA"
Como posso acrescentar dias pra que no expira tenha 30 dias a mais que o criado?
Ex2: Criou hoje ( 13/02/2017 ) e vai expirar amanhã(+1 dia) // minha questão é nesse +1 dia
Answer: Você pode usar a função DATE_ADD(criado,INTERVAL 10 DAY) vai adicionar 10dias em cima do valor do campo criado
Comment: Obrigado! funcionou


How to combine identical tables into one table?

Question: I have 100s of millions of unique rows spread across 12 tables in the same database.  They all have the same schema/columns.  Is there a relatively easy way to combine all of the separate tables into 1 table?
I've tried importing the tables into a single table, but given this is a HUGE size of files/rows, SQL Server is making me wait a long time as if I was importing from a flat file.  There has to be an easier/faster way, no?
Comment: Why bother? Why not just create a view that unions them, and reference the view from now on?
Comment: FYI: Importing from a flat file shouldn't be slow either.  I suspect that your configuration is a bit overloaded.
Comment: Why did you use 12 separate tables if the schema is the same?  Why do you need them all in one table?
Comment: Using a UNION will not perform well, UNLESS the tables are structured along an axis like time (monthly, quarterly, etc) and you can leverage specialized index types of your server to help.
Comment: Why it was designed like that? Do tables are located on the same drive? What is the version of the server? Maybe there is some partitioning? Above all, if you decide to do it, do in portions not in a single `INSERT` statement.
Comment: @Paul I assumed the underlying tables are indexed, and depending on the query patterns SQL Server should be able to eliminate them just as easily as it would do with a huge unpartitioned table that combines them all. Nothing is going to be ideal here I think.
Comment: Just wanted to add that if the OP ends up with a solution using UNION, if he or she isn't concerned with duplicates between the tables, UNION ALL will be faster than UNION.  In my experience, anyway.
Answer: You haven't given much info about your table structure, but you can probably just do a plain old insert from a select, like below.  The example would take all records that don't already exist Table2 and Table3, and insert them into Table1.  You could do this to merge everything from all your 12 tables into a single table.
<code>INSERT INTO Table1
SELECT * FROM Table2
WHERE SomeUniqueKey
NOT IN (SELECT SomeUniqueKey FROM Table1)
UNION
SELECT * FROM Table3
WHERE SomeUniqueKey
NOT IN (SELECT SomeUniqueKey FROM Table1)
--...
</code>
Comment: Jim if he's looking to move all records I don't the the `WHERE` and `NOT IN` clauses are necessary.
Comment: @Zane, as I mentioned in my post, this is inserting records from Table2 and Table3 into Table1, where the records don't already exist in Table1.  It seems pretty clear what the WHERE clause is for if you understand SQL.
Comment: I do understand SQL and I also unserstand the OP's question. He said he needs all of this information combined into one location therfore no where clause is not required as it would slow the query down considerably. He has 100's of millions of rcords in each of these tables you are really going to want to use a NOT IN statement? Where this 100 Million is NOT IN that 100 Million.
Comment: Would it be faster/more expedient to use an Insert Into function or to just use SQL Server's Import/export tool?  Does the Insert Into clause assume the same datatypes already in the table that is being imported into?
Answer: Do what Jim says, but first:
1) Drop (or disable) all indices in the destination table.
2) Insert rows from each table, one table at a time.
3) Commit the transaction after each table is appended, otherwise much disk space will be taken up in case of a possible rollback.
4) Renable or recreate the indices after you are done.
If there is a possibility of duplicate keys, you may need to retain an index on the key field and have a NOT EXISTS clause to hold back the duplicate records from being added.


Fixed point of a bounded map in compact metric space

Question: The following is an exercise from Linear Analysis by Bollobas.
Let $f:X\to X$, with $X$ a compact metric space. Suppose that for every $\epsilon>0$, there is a $\delta=\delta(\epsilon)$ such that if $d(x,f(x))<\delta$ then $f(B(x,\epsilon))\subset B(x,\epsilon)$. Let $x_0\in X$ and define $x_n=f(x_{n-1})$ for every $n\ge 1$. Show that if $d(x_n,x_{n+1})\to 0$ as $n\to \infty$ then the sequence converges to a fixed point of $f$.
My attempt: So compactness implies completeness, so this sequence has a limit $x$. On the other hand, for any $\epsilon>0$ there is $N$ so that whenever $k>N$, $d(x_k,x)<\epsilon$ and $d(x_k,f(x_{k}))<\delta\epsilon$.  Then since $x$ is in an $\epsilon$-ball around $x_k$ and $x_k$ satisfies the necessary condition, $f(x)$ must also be in the $\epsilon$-ball about $x_k$. Thus $d(x_k,f(x))<\epsilon$, and so we see that $x_k\to f(x)$ as well, and therefore we must have $x=f(x)$.
Did I do something wrong? I don't seem to have used the full strength of compactness anywhere...
Comment: The condition $d(x_n , x_{n+1})$ in a general metric space does not imply that $x_n$ is Cauchy. But in a compact metric space, there is a convergent subsequence...
Comment: How about this: Take this subsequence $(x_{i_j})$ that converges to some $x$. Then, for any $\epsilon$, there is some sufficiently large $j$ (so that $i_j$ is sufficiently large), so that $d(x_{i_j}, f(x_{i_j}))<\delta(\epsilon)$ AND $d(x_{i_j},x)<\epsilon$, so $f(x)$ is also inside an $\epsilon$-ball about $x_{i_j}$. So we see that $x_{i_j}$ converges also to $f(x)$ as needed.
Answer: My first thought is to say that on a compact space, every sequence has a convergent sub-sequence.
In which case $d(x_{n_{k}},x) <\epsilon$
d($f\circ f\circ ...f\circ (x_n),x_{n_k})<\epsilon$ for some number of repetitions.
What I haven't been able to prove to myself is, would it be possible for f(x) to step away from $x_{n_k}$ for a sequence of steps, and step back to it, and have each step be decreasing.
Answer: $X$ is compact, so given any such sequence $(x_i)$, we can take a convergent subsequence $(x_{i_j})$ that converges to some $x\in X$. Now, take any $\epsilon>0$. By convergence there is some $N$ such that whenever $l>N$, $d(x,x_{i_l})<\epsilon$. Now by hypothesis there is $M$ such that whenever $k>M$, $d(x_k,x_{k+1})<\delta(\epsilon)$. Hence there is some sufficiently large $L$ such that whenever $m>N$ and $i_m>M$, $f(x)$ is inside the $\epsilon$-ball about $x_{i_m}$. Thus $x_{i_j}$ converge to $f(x)$, and so $f(x)=x$.
Now, $x$ trivially satisfies $d(x,f(x))<\delta(\epsilon)$, so for any arbitrary $\epsilon$, there is $x_{i_j}$ such that $d(x_{i_j},x)<\epsilon$, and thus $f(x_{i_j})=x_{i_j+1}$ is inside the $\epsilon$-ball about $x$. In fact this continues to hold for all $f^n(x_{i_j})$, or equivalently, every $x_m$ with $m>i_j$. Hence the $x_i$ converge to $x$ as well.


How do I write an SQL query that gives me the amount of times a value is repeated in a table?

Question: So I have two tables which I have created:
 CREATE TABLE IF NOT EXISTS `Advertising_Campaign` (
 `CampaignID` VARCHAR(10) NOT NULL,
 `AdvertName` varchar(45) NOT NULL,
 `ProjectLead` VARCHAR(10) NULL,
 `CostEstimate` decimal NULL,
 `CampaignCost` decimal NULL,
 `EndDateEst` date NULL,
 `StartDate` date NULL,
 `EndDate` date NULL,
 `Theme` VARCHAR(45) NOT NULL,
 `AdvertType` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`CampaignID`))
  ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `staff_works_campaign` (
`CampaignID` VARCHAR(10) NOT NULL,
`StaffID` VARCHAR(10) NOT NULL,
`SalaryGrade` Integer NOT NULL,
`isSup` VARCHAR(3) NOT NULL,
PRIMARY KEY (`StaffID`, `CampaignID`),
CONSTRAINT `FK_StaffID3` FOREIGN KEY (`StaffID`) REFERENCES `Staff` (`StaffID`),
CONSTRAINT `FK_CampaignID2` FOREIGN KEY (`CampaignID`) REFERENCES `Advertising_Campaign` (`CampaignID`))
ENGINE = InnoDB;

which gives the tables:

Basically, I want to write a query that will return me a list of the advertising_campaign.AdvertName with more than 2 staff members working on them and a count of the number of staff members whose staff_works_campaign.SalaryGrade is greater than 2.
I have tried:
select a.advertname, count(*) as 'Greater Than 2'
from advertising_campaign a inner join staff_works_campaign
where staff_works_campaign.SalaryGrade > 2;

Which isn't exactly what I want, it returns:

I am a bit unsure at what this is returning exactly because I would thought it would have returned a count of 2(because of the fact that there are 2 entries with a SalaryGrade of 4 in the table), might be because of the way inner join works?
I am also a bit confused as to how to filter for 'more than 2 staff members', My idea is to see the amount of times the staff_works_campaign.CampaignID has appeared in the staff_works_campaign table to see how many staff members are apart of the same campaign.
I'm not sure how to structure it to count the amount of times campaignID is repeated and to return the names of the adverts that have a campaignID that has 2 or more staff members working on it.
So in this case I would want it to return a table with AdvertName of only those campaigns with two or more people working on them and a count of those staff members who have a salary grade greater than 2.
Comment: Sorry I thought it would be easier to read, I will edit it.
Answer: SELECT
    a.CampaignID
    ,a.AdvertName
    ,COUNT(DISTINCT s.StaffID) AS [Count of staff]
    ,SUM(
        --Use this to get a total of the staff who are
        --in a SalaryGrade greater than 2
        CASE WHEN s.SalaryGrade > 2
            THEN 1
            ELSE 0 --anyone who is under this level will be a 0 and not count
        END
    ) as [Count of staff above salary grade]
FROM
    advertising_campaign AS a
    INNER JOIN staff_works_campaign AS s
        --dont forget the join condition
        ON a.CampaignID = s.CampaignID
--Dont want a where here, we want to include ALL staff.
--WHERE
--  staff_works_campaign.SalaryGrade > 2
GROUP BY
    a.CampaignID
    ,a.AdvertName
HAVING
    --more than two members of staff working on the same campaign.
    COUNT(DISTINCT s.StaffID) > 2

Comment: The AdvertName does not have `UNIQUE` constraint so this query might give little bit surprising results.
Answer: You can do something like following if you want both conditions together,
2 people working on camp, whose salary_grade >2
SELECT AdvertName
FROM Advertising_Campaign
WHERE CampaignID IN
 (
   SELECT
     CampaignID
   FROM
     staff_works_campaign
   WHERE
     SalaryGrade > 2
   GROUP BY
     CampaignID
   HAVING
   COUNT(DISTINCT StaffID) >= 2
)

Comment: You don't need the COUNT(DISTINCT) as there can't be same StaffID for one CampaignID because of primary key definition: `PRIMARY KEY (StaffID, CampaignID)`
Comment: Thanks, this was the most simple and easy to understand answer with my current knowledge :).
Answer: Firstly you need a condition to join the two tables on. Secondly you can use a Group By and Having clause to put the filter on aggregation. Finally you need to count the number of staff with a salary grade > 2, which you can SUM a conditional for. Something like this:
select a.advertname, Sum(CASE WHEN c.SalaryGrade > 2 THEN 1 ELSE 0 END) as 'Greater Than 2'
from advertising_campaign a inner join staff_works_campaign c
    on a.CampaignId = c.CampaignId
Group By a.advertname Having count(*) >= 2;

Answer: What you have received as a result from your query is the count of all staff members across all campaigns that have a salary grade greater than 2. It returns "Star Wars 3" as the advert name simply because it's the first name it came across in all the results that the COUNT operates over. (Some other SQL technologies such as Microsoft SQL Server actually won't allow you to do this kind of query to avoid this confusion.)
In order to get the results to be split by the campaign, you have to use the GROUP BY clause as suggested in the other answers. This will tell SQL to calculate any aggregate functions (i.e. COUNT) over groups of records that all match for one or more fields. In your case, you want to group by the campaignID, since you want the COUNT to be calculated for each campaign individually. You could do this on the advert name as well, but better to do it on the ID in case you have two with the same name. Modifying your query to do that, we get:
select a.campaignID, count(*) as 'Greater Than 2'
from advertising_campaign a inner join staff_works_campaign
where staff_works_campaign.SalaryGrade > 2
group by a.campaignID;

This still isn't quite going to work though, because the salary grade condition is applied before the COUNT. We need to move that part out into a new query that wraps around this one. We also need to limit the campaigns down to those with two staff - thankfully, we don't need yet another outer query for that. The HAVING keyword allows a condition to be applied after a GROUP BY, so we can do:
select a.campaignID, count(*) as 'staff_amount'
from advertising_campaign a inner join staff_works_campaign
group by a.campaignID
having staff_amount > 2;

Now, adding the staff salary condition and another select from advertising_campaign to get the advert name in an outer query, we finally get:
select advertising_campaign.advertname
from advertising_campaign
inner join staff_works_campaign on advertising_campaign.campaignid = staff_works_campaign.campaignid
inner join
(
    select a.campaignID, count(*) as 'staff_amount'
    from advertising_campaign a inner join staff_works_campaign
    group by a.campaignID
    having staff_amount > 2
) large_campaigns on advertising_campaign.campaignid = large_campaigns.campaignid
where staff_works_campaign.salarygrade > 2


Best/optimized way to search/lookup text in a file in BASH scripts

Question: I have a file with hundreds of thousands records. All these records are unique, comma separated values. First column can be considered the key, and second column is the value of interest.
The file size would be 8 to 10 MBs. I have to lookup these value time to time in a script. Currently I am using below grep statement:

myvalue=$(grep $myvar filename | cut -d, -f2)

It works fine, but the real problem is multiple/sequential lookups to same file. I think it is not very optimized way as I have to lookup from same file multiple times (more than 100-200 times ) during my script run so each time it'll grep the entire file. I want some better/optimized way.
update
Important to note that it is sequential script, and all the values in $myvar are generated during runtime, so I can't have all the values available and do a combined lookup, it has to be one value lookup in each iteration
Comment: It's not at all optimized. Use a programming language that lets you index the data first, then use the index for your queries.
Comment: It's a shell script that I use to login multiple devices, so can't change that,
Comment: Why not? What does performing several hundred queries of multi-megabyte files have to do with logging into another machine?
Comment: As I mentioned, it is a shell script and the values are generated in real time, so I have to lookup these values during the script run,
Comment: If the values you are looking up are generated at real time, that does not affect your ability to index the original data before hand.
Comment: Please suggest how? Or what language I can use in that shell script?
Comment: Have a look at `mapfile` here https://www.computerhope.com/unix/bash/mapfile.htm
Comment: When you write: _I have to lookup these value time to time in a script._ Do you mean that _you have the run the scipt multiple times and each time you run it, it needs to do such a lookup_, or do you mean _you run the script ones, but you have to perfom that lookup multiple times during a single run_
Comment: the later; script runs once and and I have to perform multiple lookups during the single run,
Answer: On of the obvious things I'm thinking of is the limitation of grep results, which can be done with the -m switch:
Prompt>cat test.txt
a
a
b
a
b

Prompt>grep "a" test.txt
a
a
a

Prompt>grep -m 1 "a" test.txt
a

Comment: -m will definitely improve the situation, but still each time the file has to be looked up, I am looking for some alternate, like maybe loading the entire file once in some sort of dictionary, or array maybe,
Answer: If the file is constructed once and then referenced over and over without being changed in between, you need to use an associative array as a lookup table. That might get big and ugly in bash; consider perl instead.
However, you asked how to do it in bash.
$: eval "declare -A lookup=(
   $( sed -E 's/^([^,]+),([^,]+).*/  [\1]=\2/' filename )
   )"

Now all the values should be in the table lookup.
An associative array uses strings as its keys instead of integers, so this sets the keys and values as pairs in a table.
sed -E 's/^([^,]+),([^,]+).*/  [\1]=\2/'

takes the first and second fields of the comma-delimited file and reformats them into key/value assignments in bash syntax, like this:
declare -A lookup=(
   [a]=1
   [b]=2
   [c]=3 # ... and so on
)

The eval parses all that into the current environment for your use.
No more grep's. Just use "${lookup[$myvar]}".
If you just wanted to assign it for readability, then instead of the grep use
myvalue="${lookup[$myvar]}"

My local example in use:
$: cat x
a,1,lijhgf
b,2,;lsaoidj
c,3,;l'skd

$: echo "declare -A lookup=(
   $( sed -E 's/^([^,]+),([^,]+).*/  [\1]=\2/' x )
   )"
   declare -A lookup=(
     [a]=1
     [b]=2
     [c]=3
   )

$: eval "declare -A lookup=(
   $( sed -E 's/^([^,]+),([^,]+),.*/  [\1]=\2/' x )
   )"

$: echo "${lookup[b]}"
   2

Comment: Replace the `eval` with just `echo` and throw it to a file so you can check the syntax looks like the example I gave. `declare -A lookup=( [a]=1 [b]=2 [c]=3 )` - you should be able to say something like `echo ${lookup[b]}` and see the right value if you source the file.
Comment: Got it. Sorry, was being overly cautious. I had actually done that as well at one point, and one of the three `sed`'s here was like that, lol... I updated it here so they match. :) You're quite welcome. How's the benchmark? Saving lots of time?
Comment: It's possible in a *perl* script, which will have much finer tools for complex programming with advanced maths and efficient streaming of data, and large, fast, complex memory structures. YMMV, but for big projects, you might be surprised what you can do in bash (and I have), but different languages specialize in diffrent things. Bash makes quick composition on the command line possible, focusing on a scriptable *interface*. It *can* do lots, but other languages do some of those things better. For example, bash has no multidimensional arrays at all.
Comment: Yeah, that's correct,  the file doesn't change. Your solution sounds interesting, I'll check and do some benchmarking and let you know
Comment: I am seeing this error when trying to load the file in array `must use subscript when assigning associative array`
Comment: I did echo and values are not coming as your example, rather simply comma separated keys and values only... Something wrong with sed statement I think
Comment: aah, I think in my case the difference is the input file. it has only two columns, when I use contents of your file x, it works fine, but when I removed the third column, it prints same thing: keys,values as it was printing for my original file. So need to adjust the sed statement.
Comment: replaced the `,.*` with `.*`from the end of sed expression, and it is working fine now as I don't have second comma in my file :) thanks a lot for your support
Comment: yeah, its amazing improvement, my initial setup was like plain `while read` and then grep each line, which took about 5 minutes and only looked up 30K values, while using arrays while I load the file, it took about 18 seconds to load and then printing all the records (almost 200K) took less then 2 seconds.
Comment: As you mentioned in your first comment, is it possible to use Perl hash in a shell script?
Answer: First of all, lets look at your command:
myvalue=$(grep $myvar filename | cut -d, -f2)

You make use of 2 binaries which you load (grep and cut) to process the data. You should attempt to reduce this to a single binary. This will already help a lot:
myvalue=$(awk -F, -v var="$myvar" '$0~var { print $2; exit}' filename)

This will be much faster as:

it is a single library
stops reading the file from the moment the entry is found

If you need to do multiple lookups based on the key which is located in the first column, you can do the following in bash:
 while IFS= read -r; do
    declare -A z+="( $REPLY )"
 done < <(awk -F, '{print "["$1"]="$0}' lookupfile)

 echo ${z[$key]}

based on How do I populate a bash associative array with command output?


Using Extended Euclidean algorithm question

Question: Suppose we have $f=x^5-1$ and $g=x^2-1$ and I have found the gcd using the Euclidean algorithm but I'm trying to find a way of expressing this in the way $$af+bg=1$$ where $f, g \in Q[x] $. I know this is actually quite easy and I could do this before, but somehow I can't seem to find how to do it.
I have the following
$x^5-1=(x^2-1)(x^3*x)+(x-1) $
$x^3+x=(x-1)(x^2+x+2)+2 $
So gcd is 1 but how can I work backwards to find how to do the question.
Comment: $x^3+x=(x-1)(x^2+x+2)+2 $  was the wrong calculation, should have been $x^2-1=(x-1)(x+1)+(0) $
Answer: $$  \left(   x^{5}  - 1 \right)  $$
$$  \left(   x^{2}  - 1 \right)  $$
$$  \left(   x^{5}  - 1 \right)  =  \left(   x^{2}  - 1 \right)  \cdot \color{magenta}{  \left(   x^{3}  +  x  \right) } +  \left(   x  - 1 \right)  $$
$$  \left(   x^{2}  - 1 \right)  =  \left(   x  - 1 \right)  \cdot \color{magenta}{  \left(   x  + 1 \right) } +  \left( 0 \right)  $$
$$ \frac{ 0}{1} $$
$$ \frac{ 1}{0} $$
$$ \color{magenta}{  \left(   x^{3}  +  x  \right) }  \Longrightarrow  \Longrightarrow  \frac{  \left(   x^{3}  +  x  \right) }{ \left( 1  \right) } $$
$$ \color{magenta}{  \left(   x  + 1 \right) }  \Longrightarrow  \Longrightarrow  \frac{  \left(   x^{4}  +  x^{3}  +  x^{2}  +  x  + 1 \right) }{ \left(   x  + 1 \right) } $$
$$  \left(   x^{4}  +  x^{3}  +  x^{2}  +  x  + 1 \right)  \left( 1  \right)  -  \left(   x  + 1 \right)  \left(   x^{3}  +  x  \right)  =  \left( 1  \right)  $$
$$  \mbox{to confirm GCD} =   \color{blue}{  \left(   x  - 1 \right) }   $$
$$  \left(   x^{5}  - 1 \right)  =  \left(   x^{4}  +  x^{3}  +  x^{2}  +  x  + 1 \right)  \cdot \color{blue}{  \left(   x  - 1 \right) } +  \left( 0 \right)  $$
$$  \left(   x^{2}  - 1 \right)  =  \left(   x  + 1 \right)  \cdot \color{blue}{  \left(   x  - 1 \right) } +  \left( 0 \right)  $$
$$  \left(   x^{5}  - 1 \right)  \left( 1  \right)  -  \left(   x^{2}  - 1 \right)  \left(   x^{3}  +  x  \right)  = \color{blue}{ \left(   x  - 1 \right)  }$$
$$  \mbox{GCD} =   \color{blue}{  \left(   x  - 1 \right) }   $$


Notational question regarding differential equations

Question: I am frequently given differential equations of the form:
$$ \cdots = \dfrac{dy}{dx}  \text{ or } \cdots=y'.$$
However, I am sometimes given differential equations of the form:
$$a \, dy +b \, dx = c$$
I don't really understand what this notation means. For example:
$$(y^2+xy)\,dx +(3xy+x^2)\,dy =0$$
Is an equation that I have recently been given. Is this equivalent to:
$$\frac{y^2+xy}{3xy+x^2} = -\frac{dy}{dx} \text{?}$$
I apologize for the somewhat basic question, but I don't quite grasp this notation, and I would very much appreciate if this could be explained.
Comment: Yes, you interpreted the notation correctly: Just try to get the term $\frac{dy}{dx}$ from the given differential variables. In your case, you were able to get it. I too do not like that notation, it is unrigorous in nature (what does $dx$ mean rigorously?)
Comment: $Pdx+Qdy=0$ is ambiguous: it could mean $P+Q\frac{dy}{dx}=0$ or it could mean $P\frac{dx}{dy}+Q=0.$ I believe this ambiguity is considered one of the advantages, or even the main point, of writing the equation in "differential form".
Comment: Often what we find in Nature is not Functions but just Related Variables. Why must we force one variable to be independent and the other dependent, when they deserve to be treated equally?
Answer: Short answer: yes, it is equivalent, you are intepreting it right.
Explanation:
you can understand $dx$ as the continuous version of the discrete $\Delta x$, where $\Delta x = x-x_0$ and $dx = \lim\limits_{x \rightarrow x_0^{+}} {\Delta x}= \lim\limits_{x\rightarrow x_0^{+}} {(x-x_0)}$.
For example in the graphic attached you have the function $y=x^3$. You can approximate the slope of this function, taking two $x$ values and their images, by $ \dfrac{\Delta y}{\Delta x}$. If you do that for "very small differentials" (where small in the applied mathematics case depends on the physical context and it will always contain a small, ideally negligible, source of error), you retrieve the derivative definition: $y'= \lim\limits_{x\rightarrow x_0^{+}}{\dfrac{y-y_0}{x-x_0}} = \dfrac{dy}{dx}$, which in that case equals to $y'= 3x^2 $, or  $ dy = 3x^2 dx$.
Vaguely speaking, a way to interpret this intuitively is to think of $dx, dy$ as a "reminiscence" of the derivative's geometric meaningenter image description here
Answer: When a curve in the Cartesian plane is not the graph of a function it may be possible to describe it as $\{(f(t),g(t)\}$ where $f$ and $g$ are functions and  $t$ ranges over some subset of $\mathbb R.$
For example the  circle $C=\{(\cos t,\sin t): t \in \mathbb R\}.$ For $C$ we can write $y(t)dx(t)/dt+x(t)dy(t)/dt=0,$ which we abbreviate as $ydx+xdy=0.$
When $(\pm 1,0)\ne (x,y)\in C$ we can find an open set $U$ with $(x,y)\in U$, for which  $C\cap U$ is the graph of a function $h$ of the first co-ordinate, satisfying $h(x)+xh'(x)=0.$
Similarly when $(0,\pm 1_\ne (x,y)\in C$ we can find an open set $V$ with $(x,y)\in V,$ for which $C\cap V$ is the "reverse" of the graph of a function $j$ of the second co-ordinate, satisfying $yj'(y)+j(y)=0.$


When an equivalence is a monoidal equivalence?

Question: I want to understand the followings from "Tensor Categories" by Etingof, Gelaki, Nikshych, and Ostrik.

Remark 1.5.3 It is easy to show that if $F: C \to C'$ is an equivalence of monoidal categories, then there exists a monoidal equivalence $F^{-1}: C' \to C$ such that the functors $F \circ F^{-1}$ and $F^{-1} \circ F$ are isomorphic to the identity functor as monoidal functors.

As indicated in the answer to the question, not every equivalence is a monoidal equivalence.
So what I am missing? Does Remark 1.5.3 contain more information than just a equivalence so that I can prove it is a monoidal equivalence? Or I am confused by some definitions?
Thanks.
Answer: The authors define: "A monoidal functor F is said to be an equivalence of monoidal categories if it is an equivalence of ordinary categories." I.e. a monoidal equivalence F is an equivalence, and F is equipped with a monoidal structure J. Since F is an equivalence, it admits a quasi-inverse G such that FG and GF are isomorphic to the respective identity functors.
What is less obvious (to a beginner like myself) is that G also may equipped with its own monoidal structure.
Answer: The meaning of "equivalence" is context-sensitive, just as for "isomorphism". Here, it is clear that "equivalence of monoidal categories" means the same as "monoidal equivalence".
Comment: In that case, what is "easy to show" in Remark 1.5.3? It seems that it is a definition of "monoidal equivalence".
Comment: There is more than one possible definition. For instance, one might define monoidal equivalence to be "strong monoidal functor that is fully faithful and essentially surjective on objects".
Comment: @ZhenLin *bump* Is that definition of a monoidal equivalence distinct to the one (Definition 12) given in math.ucr.edu/home/baez/qg-winter2001/definitions.pdf ?


Sample size calculation with no reference

Question: I'm conducting a RCT to test if a new type of device ("test device") is quicker than the device used now ("control device"). The problem is though, that we don't now what the mean time for the control device is, and we have no way of testing it (no ressources for a pilot study before the RCT either).
We know, however, that the new test device will be quicker than the control device, since the control device operates with 5 steps, while the test device only uses the first 4 steps. We have a hypothesis, that the test device will have a mean time that is at least 20% quicker than the control device. However, I have no idea how to calculate a sample size for this study, since we don't know the control mean time?
The RCT will randomly allocate to test / control group 1:1.
Other factors will also play a role; human errors, which might "blur" the mean times, which is why we need to run the study over a longer time period, to sort of "eliminate" this blur. That is, no matter the number of participants, we would likely have to run the study for at least 6 months, to account for these daily/weekly/monthly "human" changes.
Do you have any ideas how to calculate this sample size?
I assume, the hypothesis should be tested using a two-sample t-test (TOST) - superiority trial, with the 20% as margin.
Answer: You need not only the expected difference in means but also the associated variances among measurements to estimate sample sizes. Say that you have transformed the time axis so that the average time per "control device" test is 1 unit. Then you want to document that the average time per "test device" test is no greater than 0.8 units on that same scale.
As this isn't a paired design, the sample sizes needed will depend on the variability of test times within each device type. If the variances of test times are similar between the two devices, what matters is the ratio of that difference of 0.2 transformed-time units to the standard deviation $\sigma$ of test times (in those time units) within a type of device. That ratio is the hypothesized "effect size" that you want to be able to detect, and is often represented as d.
That variability thus might be determined by the characteristics of whatever it is you're testing even more than the variability introduced by the test systems themselves. That's the variability due to the "other factors" that "blur" the results. You presumably have some information on that variability of test times among the test objects, based on your understanding of the subject matter.
This PubMed page has a link to a free copy of "Practical guide to sample size calculations: superiority trials" by L. Flight and S. A. Julious, Pharmaceutical Statistics, 15 (1). pp. 75-79.* You choose the risk trade-offs you are willing to take, in terms of the balance between false-positive (Type I) and false-negative (Type II) errors from the study. That combination of variance, effect size, and risk trade-offs determines the sample size.
If the underlying transformed-time values are normally distributed with the same variance $\sigma^2$, Equation 3 of that paper provides a simple value for the size of each group if there is 1:1 allocation, based on Type I error of 0.05 and Type II error of 0.1 (i.e., 90% power). Multiplied by 2 to get the total sample size, you find:
$$ N = \frac{42 \sigma^2}{d^2}, $$
where d and $\sigma^2$  are as above. The paper provides much detail for other scenarios, but that is probably a good guide to initial design. For your desired difference of 0.2 units in transformed-time units, that's equivalent to:
$$N=210 \sigma^4, $$
with $\sigma$ in transformed-time units that bring the mean "control device" test time to a value of 1.
The problem, as pointed out by Michael Chernick, is:

In practice you never know the true variance. What you do is guess at it. This can be done by looking at how the answer changes as the population variance would vary over a range of plausible values. You might find the plausible values through literature on similar studies or through your own pilot study or you can do a two stage adaptive design where the initial stage is used primarily to determine if you need additional data and if so how much.

If you have the resources to do a pilot study just on the "test device" or already have enough data on it to estimate the variance of measurements made by it you could use that as the basis for that guess. For a start you could assume the same variance for the "control device". Perhaps there is information in the literature about the variance with the established "control device."
As Chernick said, this type of problem is sometimes handled by an adaptive design. This answer provides links to related resources. In particular, if you use the first stage of the adaptive design only to estimate variances better, without evaluating treatment differences, there is little risk of inflating the false-positive rate by adjusting the total sample size for the second stage.
Finally, consider whether you really want to do a superiority test. If your "test device" is simpler or cheaper than the "control device," a non-inferiority test could support its preferred use with a much smaller sample size. This web page illustrates how equivalence, superiority, and non-inferiority tests require different sample sizes.

*The page also contains links to similar articles on equivalence and non-inferiority tests.
Comment: Which exact formula/calculation are you referring to? I think it it quite a jungle with all the different formulas, so I would appreciate hearing which you would go with.

However, have no idea what the variance would be - to give some estimates of this would be completely guessing. Therefore, I'm worrying that a sample size calculation would be based on so much guessing, that it might be just as plausible to say, like "we run this study for X months".

Good point - but we KNOW that the test will be quicker (simple logic, since it do less steps) - question is only how much quicker.
Comment: @user12541161 it's the variability of test times among the cases you allocate to the two tests that matters here, including the "other factors" that "blur" the results. I suspect that you have some basis for estimating that variability if you've done some testing already. With an adaptive design you start with such an informed guess and re-evaluate the variance at some point during data collection, to alter the sample size as needed. Think of it as running a pilot phase at the beginning of the study, in a way that you can use the data from the pilot phase for the overall calculation.
Comment: @user12541161 a couple of other thoughts. First, if the humans involved aren't blinded to the type of test then you might get into trouble. Second, this all assumes that the two types of tests will return the same results on any test object, with only the time to get the result different. As an extreme example, I could have an infinitely fast test device if I don't care about the quality of test results at all--just don't do a test! I fear that you might be in an intermediate state, where the "test device" is faster but maybe not of as high quality as the "control device."
Comment: Thanks, and very good point about the "pilot phase" in the beginning of the study!

Regarding your second comment: you are completely right. We don't know if the test device is as good as the control device. And this is of course also a part of the study. However, our main objective is the time difference, since this is the biggest concern at the moment.


AWS EC2 java program memorry map issue

Question: I have a java program where a file around 800MB is memory mapped via java.io.RandomAccessFile. I'm hosting it in an EC2 m5.8xlarge (32 CPUs, 128GB RAM) instance with JVM OPTS set to -Xms64g -Xmx64g. While starting the service, I met error:
 [thread 3606 also had an error]
 #
 # A fatal error has been detected by the Java Runtime Environment:
 #
 #  SIGBUS (0x7) at pc=0x00007f214f3c5e73, pid=3556, tid=3637
 #
 # JRE version: OpenJDK Runtime Environment Temurin-17.0.6+10 (17.0.6+10) (build 17.0.6+10)
 # Java VM: OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (17.0.6+10, mixed mode, sharing, tiered, compressed class ptrs, z gc, linux-amd64)
 # Problematic frame:
 # V  [libjvm.so+0x602e73]  Copy::fill_to_memory_atomic(void*, unsigned long, unsigned char)+0x103
 #
 # No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
 #
 # An error report file with more information is saved as:
 # /home/user/builds/current/hs_err_pid3556.log
 #
 # If you would like to submit a bug report, please visit:
 #   https://github.com/adoptium/adoptium-support/issues
 #
 /home/user/builds/current/start.sh: line 78:  3556 Aborted                 java ${JVM_OPTS} -cp 'lib/*' ${LAUNCH_CLASS} $@

And the hs_err_pid3556.log mentioned above gives me below, where sun.misc.Unsafe.setMemory went wrong when setting the block of memories to 0s:
Current thread (0x00007fcaf0274fd0):  JavaThread "ForkJoinPool-1-worker-7" daemon [_thread_in_vm, id=3199, stack(0x00007fcb15fda000,0x00007fcb160db000)]
Stack: [0x00007fcb15fda000,0x00007fcb160db000],  sp=0x00007fcb160d8ce8,  free space=1019k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V  [libjvm.so+0x602e73]  Copy::fill_to_memory_atomic(void*, unsigned long, unsigned char)+0x103
j  jdk.internal.misc.Unsafe.setMemory0(Ljava/lang/Object;JJB)V+0 firstname.lastname@example.com
j  jdk.internal.misc.Unsafe.setMemory(Ljava/lang/Object;JJB)V+25 email@example.com
j  jdk.internal.misc.Unsafe.setMemory(JJB)V+6 firstname.lastname@example.com
j  sun.misc.Unsafe.setMemory(JJB)V+7 email@example.com
j  example.com.buffer.MemoryMappedBuffer.set(JJB)V+58
j  example.com.buffer.Buffer.zeroed()Lexample/com/buffer/Buffer;+9
j  example.com.collections.BufferSupplierMapped.supplyBuffers(JJ)Lorg/apache/commons/lang3/tuple/Pair;+37
j  example.com.collections.ConcurrentOffheapLongObjMap$MapImpl.<init>(Ljava/lang/String;Lexample/com/collections/OffheapMapBufferSupplier;Lexample/com/collections/OffheapValueSerDe;JJF)V+65
j  example.com.collections.ConcurrentOffheapLongObjMap.<init>(Ljava/lang/String;JJLexample/com/collections/OffheapMapBufferSupplier;Lexample/com/collections/OffheapValueSerDe;F)V+64
j  example.com.collections.ConcurrentOffheapLongObjMap.<init>(Ljava/lang/String;JJLexample/com/collections/OffheapMapBufferSupplier;Lexample/com/collections/OffheapValueSerDe;)V+11
j  example.com.collections.OffheapMapUtil.readToMapped(Ljava/lang/String;Lexample/com/collections/OffheapValueSerDe;Ljava/lang/String;Ljava/lang/String;)Lexample/com/collections/ConcurrentOffheapLongObjMap;+99
j  example.com.index.job.WritableSiteIndex.lambda$snapshotLoad$21(Lorg/apache/commons/lang3/mutable/MutableObject;Lexample/com/model/Site;Ljava/lang/String;Ljava/lang/String;)V+20
j  example.com.index.job.WritableSiteIndex$$Lambda$362+0x0000000801044f58.run()V+16
j  example.com.thread.AsyncTaskList.lambda$add$0(Ljava/lang/String;Lexample/com/function/ThrowingRunnable;)Ljava/lang/Void;+19
j  example.com.thread.AsyncTaskList$$Lambda$223+0x0000000800e31428.call()Ljava/lang/Object;+8
j  example.com.thread.AsyncTaskList.lambda$execute$1(Ljava/util/concurrent/Callable;)Ljava/lang/Boolean;+1
j  example.com.thread.AsyncTaskList$$Lambda$230+0x0000000800e30400.get()Ljava/lang/Object;+4
j  example.com.thread.WorkerService$$Lambda$231+0x0000000800e38000.call()Ljava/lang/Object;+4
j  java.util.concurrent.ForkJoinTask$AdaptedCallable.exec()Z+5 firstname.lastname@example.com
j  java.util.concurrent.ForkJoinTask.doExec()I+10 firstname.lastname@example.com
j  java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Ljava/util/concurrent/ForkJoinTask;Ljava/util/concurrent/ForkJoinPool$WorkQueue;)V+13 email@example.com
j  java.util.concurrent.ForkJoinPool.scan(Ljava/util/concurrent/ForkJoinPool$WorkQueue;II)I+193 email@example.com
j  java.util.concurrent.ForkJoinPool.runWorker(Ljava/util/concurrent/ForkJoinPool$WorkQueue;)V+53 email@example.com
j  java.util.concurrent.ForkJoinWorkerThread.run()V+31 firstname.lastname@example.com
v  ~StubRoutines::call_stub
V  [libjvm.so+0x822715]  JavaCalls::call_helper(JavaValue*, methodHandle const&, JavaCallArguments*, JavaThread*)+0x315
V  [libjvm.so+0x823f0b]  JavaCalls::call_virtual(JavaValue*, Handle, Klass*, Symbol*, Symbol*, JavaThread*)+0x1cb
V  [libjvm.so+0x8eda53]  thread_entry(JavaThread*, JavaThread*)+0xa3
V  [libjvm.so+0xe5e974]  JavaThread::thread_main_inner()+0x184
V  [libjvm.so+0xe62020]  Thread::call_run()+0xc0
V  [libjvm.so+0xc187e1]  thread_native_entry(Thread*)+0xe1
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  jdk.internal.misc.Unsafe.setMemory0(Ljava/lang/Object;JJB)V+0 firstname.lastname@example.com
j  jdk.internal.misc.Unsafe.setMemory(Ljava/lang/Object;JJB)V+25 firstname.lastname@example.com
j  jdk.internal.misc.Unsafe.setMemory(JJB)V+6 firstname.lastname@example.com
j  sun.misc.Unsafe.setMemory(JJB)V+7 firstname.lastname@example.com
j  example.com.buffer.MemoryMappedBuffer.set(JJB)V+58
j  example.com.buffer.Buffer.zeroed()Lexample/com/buffer/Buffer;+9
j  example.com.collections.BufferSupplierMapped.supplyBuffers(JJ)Lorg/apache/commons/lang3/tuple/Pair;+37
j  example.com.collections.ConcurrentOffheapLongObjMap$MapImpl.<init>(Ljava/lang/String;Lexample/com/collections/OffheapMapBufferSupplier;Lexample/com/collections/OffheapValueSerDe;JJF)V+65
j  example.com.collections.ConcurrentOffheapLongObjMap.<init>(Ljava/lang/String;JJLexample/com/collections/OffheapMapBufferSupplier;Lexample/com/collections/OffheapValueSerDe;F)V+64
j  example.com.collections.ConcurrentOffheapLongObjMap.<init>(Ljava/lang/String;JJLexample/com/collections/OffheapMapBufferSupplier;Lexample/com/collections/OffheapValueSerDe;)V+11
j  example.com.collections.OffheapMapUtil.readToMapped(Ljava/lang/String;Lexample/com/collections/OffheapValueSerDe;Ljava/lang/String;Ljava/lang/String;)Lexample/com/collections/ConcurrentOffheapLongObjMap;+99
j  example.com.index.job.WritableSiteIndex.lambda$snapshotLoad$21(Lorg/apache/commons/lang3/mutable/MutableObject;Lexample/com/model/Site;Ljava/lang/String;Ljava/lang/String;)V+20
j  example.com.index.job.WritableSiteIndex$$Lambda$362+0x0000000801044f58.run()V+16
j  example.com.thread.AsyncTaskList.lambda$add$0(Ljava/lang/String;Lexample/com/function/ThrowingRunnable;)Ljava/lang/Void;+19
j  example.com.thread.AsyncTaskList$$Lambda$223+0x0000000800e31428.call()Ljava/lang/Object;+8
j  example.com.thread.AsyncTaskList.lambda$execute$1(Ljava/util/concurrent/Callable;)Ljava/lang/Boolean;+1
j  example.com.thread.AsyncTaskList$$Lambda$230+0x0000000800e30400.get()Ljava/lang/Object;+4
j  example.com.thread.WorkerService$$Lambda$231+0x0000000800e38000.call()Ljava/lang/Object;+4
j  java.util.concurrent.ForkJoinTask$AdaptedCallable.exec()Z+5 email@example.com
j  java.util.concurrent.ForkJoinTask.doExec()I+10 firstname.lastname@example.com
j  java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Ljava/util/concurrent/ForkJoinTask;Ljava/util/concurrent/ForkJoinPool$WorkQueue;)V+13 email@example.com
j  java.util.concurrent.ForkJoinPool.scan(Ljava/util/concurrent/ForkJoinPool$WorkQueue;II)I+193 firstname.lastname@example.com
j  java.util.concurrent.ForkJoinPool.runWorker(Ljava/util/concurrent/ForkJoinPool$WorkQueue;)V+53 email@example.com
j  java.util.concurrent.ForkJoinWorkerThread.run()V+31 firstname.lastname@example.com
v  ~StubRoutines::call_stub
siginfo: si_signo: 7 (SIGBUS), si_code: 2 (BUS_ADRERR), si_addr: 0x00007fc979848000

What's interesting is that there's no problem running the same program mem-mapping the same file in my laptop (Ubuntu with 64g ram). That that AWS has no problem loading a very similar but smaller (560MB vs 800MB) file. So I'm pretty sure the Java program is working as expected, and so is the integrity of the file to be mapped.
Comment: Does this happen when running on a different EC2 instance? (ie, it could be a memory problem on your particiular instance -- in my understanding, SIGBUS is a hardware failure).
Answer: The SIGBUS (Bus Error) you're encountering often indicates memory-related issues, particularly with memory alignment. In the given log, it seems to be happening in the Copy::fill_to_memory_atomic function, and the problematic frame is in Unsafe.setMemory0. This suggests that there might be an issue with the memory mapping or alignment.
Try to experiment with different JVM options. For example, you might try using the -XX:MaxDirectMemorySize option to limit the amount of direct buffer memory and check the ulimit settings on your EC2 instance also review how you are specifying the memory map size.
If the issue persists, consider reaching out to AWS support for assistance. They might be able to provide insights specific to your AWS environment.

AWS EC2 java program memorry map issue


Estimates and standard errors for non-linear combinations of regression coefficients

Question: I have the econometric model
$$
 y_t= (1-b_0)(c + b_1x1_t + b_2x2_t + ...) + b_0*y_{t-1}
$$
$$
=(1-b_0)c + (1-b_0)(b_1x1_t)+... + b_0*y_{t-1}
$$
I estimate the model in the OLS form
$$
y_t=\beta_0c +\beta_1 x_t + ... + \beta_2y_{t-1}
$$
where for example
$$
b_1=\beta_1/(1-\beta_2)
$$
how do I recover from the OLS estimates the $b$ variables of interest? I am currently using Stata
Answer: You can get nonlinear combinations of coefficients like this, along with the their standard errors via the delta method:
. sysuse auto
(1978 Automobile Data)

. reg price mpg weight i.foreign, coefl

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(3, 70)        =     23.29
       Model |   317252881         3   105750960   Prob > F        =    0.0000
    Residual |   317812515        70  4540178.78   R-squared       =    0.4996
-------------+----------------------------------   Adj R-squared   =    0.4781
       Total |   635065396        73  8699525.97   Root MSE        =    2130.8

------------------------------------------------------------------------------
       price |      Coef.  Legend
-------------+----------------------------------------------------------------
         mpg |    21.8536  _b[mpg]
      weight |   3.464706  _b[weight]
             |
     foreign |
    Foreign  |    3673.06  _b[1.foreign]
       _cons |  -5853.696  _b[_cons]
------------------------------------------------------------------------------

. nlcom (b:_b[mpg]/(1-_b[weight])) (c:_b[weight]/_b[1.foreign]), post

           b:  _b[mpg]/(1-_b[weight])
           c:  _b[weight]/_b[1.foreign]

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
           b |  -8.866617   28.39079    -0.31   0.755    -64.51155    46.77832
           c |   .0009433   .0001725     5.47   0.000     .0006052    .0012813
------------------------------------------------------------------------------

. test b=c

 ( 1)  b - c = 0

           chi2(  1) =    0.10
         Prob > chi2 =    0.7548

The coefl option in regress makes it easier to see what the coefficients are named by Stata. You can also add the nocons option, which I've neglected to do. The post option in nlcom stores the estimation vector and variance–covariance matrix, making it easy to do hypothesis tests on these functions of coefficients.
You should also think about patching up your standard errors by using a time-series command like newey that can deal with serially correlated errors. The standard OLS assumptions about the errors are unlikely to hold here.
Answer: I'm not an expert on STATA, but if you feed STATA a left hand side variable $y_t$ and the regressors, $x$ and $y_{t-1}$, and tell it to estimate parameters with OLS, the outputted parameters $\beta_0, \beta_1$ and $\beta_2$ will probably fit in the linear model in this way,
$$
y_t = \beta_0 + \beta_1 x + \beta_2 y_{t-1}.
$$
That is I don't think that STATA ouputs the term $\beta_0 c$ as the intercept unless you tell it explicitly to do so.
So given the above linear model it's easy to retrieve the $b_i$'s, since
$$
\beta_0 = \left(1-b_0\right)c  \quad\Leftrightarrow\quad c=\frac{\beta_0}{1-b_0}
$$
$$
\beta_1 = \left(1-b_0\right)b_1  \quad\Leftrightarrow\quad b_1=\frac{\beta_1}{1-b_0}
$$
and the $b_0$ estimate is simply the OLS estimate of $\beta_2$, which we can just plug into the two equations above.


Tabela de Frequência com duas variáveis

Question: Olá, estou com uma dúvida que não consigo encontrar a resposta. Eu tenho um conjunto de dados de um livro de Estatística que estou estudando Link para o dataset.
Quando importado ele aparece como na imagem.
.
O que eu gostaria de fazer é transformar e visualizar o conjunto de dados como aparece na imagem seguinte
.
Agradeço muito a qualquer um que possa me ajudar.
Comment: o que são esses números que você procura?
Comment: Uma visualização cruzada de frequências... é um modo conciso de vizualizar dados... aparece em todo lovro de estatística e eu queria saber fazer com o pandas
Comment: Desculpe, acho que eu não claro no meu comentário anterior. Você pode descrever detalhadamente como que esses números são preenchidos? De qual coluna eles vem? O que acontece quando o valor "Capital" se repete? é somado os números de cada coluna? é calculado a média?
Comment: ah sim me desculpe...estes números são as frequências ... por exemplo na coluna Grau de Instrução temos dados qualitativos, se fizermos a soma teremos 12 E Fundamental, 18 E Médio e 6 Ensino Superior... e estes dados estão relacionados à região... a tabela do livro que eu postei mostra por exemplo que dos casos de Ensino Fundamental, 4 pertencem à Capital... O que eu não sei é cruzar as informações destas duas colunas... muito obrigado pelo interesse em me ajudar Terry...
Answer: Para fazer isso você precisará usar o groupby pelas colunas Região de Procedência e Grau de Instrução, usar o comando size para pegar o tamanho de cada um desses grupos. Após isso, é possível remover os dados sobre Grau de instrução do index com o comando unstack para eles se  "transformarem" em colunas, desta forma:
df2 = df.groupby(['Região de Procedência', 'Grau de Instrução']).size().unstack(1)
df2.head()

Grau de Instrução   ensino fundamental  ensino médio    superior
Região de Procedência
capital             4                   5               2
interior            3                   7               2
outra               5                   6               2

Para calcular os valores totais, será necessário fazer a soma primeiramente de cada coluna com sum, e salvar esses dados no index "Total", e depois repetir a mesma função mas somando linha a linha com sum(axis= 1) para se criar uma nova coluna.
df2.loc['Total',:]= df2.sum(axis=0)
df2.loc[:,'Total'] = df2.sum(axis=1)
df2.head()

Grau de Instrução   ensino fundamental  ensino médio    superior    Total
Região de Procedência
capital             4.0                 5.0             2.0         11.0
interior            3.0                 7.0             2.0         12.0
outra               5.0                 6.0             2.0         13.0
Total               12.0                18.0            6.0         36.0

Tabela de Frequência com duas variáveis


Is $\sqrt{x^3}$ uniformly continuous?

Question: $f(x) = \sqrt{x^3}, x \in (2,3)$ and $ g(x) = x^3, x \in \Bbb R$.
I have showed that $g$ is not uniformly continuous, but unable to do the 1st one i.e. $f(x) = \sqrt{x^3}, x \in (2,3)$.
Need some help for that part!
Comment: Do you know differentiation / the mean value theorem? That will be helpful.
Answer: $f$ (same formula) is continuous on $[2,3]$ hence uniformly continuous on it (by compactness of $[2,3]$) and its subspaces (by definition uniform continuity inherits to subspaces).
Answer: Hint. Any function which is continuous  in a compact set is also uniformly continuous there and in any of its subsets.

Is $\sqrt{x^3}$ uniformly continuous?


Choosing an advanced group theory text: concerns

Question: In this question, An Introduction to the Theory of Groups by Rotman is recommended twice as a good second-course group theory text.  However, after reading the reviews here, and seeing this pdf of what seems to be corrections to Rotman, I am pretty concerned about the apparently many errors in the text.  While some errors and their corrections may be pretty self-evident, I would hate to constantly be worrying about developing some misconception as a result of some fact that isn't presented quite as it should be.

To those who have read An Introduction to the Theory of Groups by Rotman: do you feel the errors are a serious concern/disruption to the flow of the text, or is the book still pretty navigable?

Also recommended in the question linked to above was A Course in the Theory of Groups by Robinson.  I really like the look of this text, based on the table of contents and the group-theoretic concepts that really interest me.
The Robinson text is said (in the linked-to question) to 'move pretty quickly into deeper waters.'  My background consists of basic group theory, ring theory, field theory, a little Galois theory, linear algebra, topology, real analysis, and graph theory (I have seen a decent amount of material and have been self-studying for a few years now).

Do you think that I can reasonably wade into these deeper waters without going in over my head and drowning?  I realize this is a little subjective since no one knows my exact abilities, but I love group theory and am definitely willing to put some real effort into it.

Thanks in advance for the advice.
Comment: The "pdf of what seems to be corrections to Rotman" was elucidated by @Did in another thread to be some kind of exercise in nitpicking on mathematical language in a well-known book rather than the must-have corrections typically described as errata to a book. There's no official errata for this book of Rotman, although he surely has some (posted on his home page) for his more recent books. The two topmost reviews on Amazon do complain about actual errors though in this book; one of those reviews is written by [Adam Glesser](http://math.fullerton.edu/people/full-time-faculty/item/glesser-adam).
Answer: Personally I think Robinson would be a terrible book to learn with.  It is a great book, but it's a great reference book, or at most a good book for getting into research level group theory (mostly in the second half).  It is incredibly dense and offers very little in the way of helpful exposition.  As for the exercises, the difficulty runs from very easy to impossibly hard (which is not a bad thing).
Rotman is a good book.  I haven't noticed any errors that weren't obvious after first glance.  The exercises are a good level of difficulty for people just moving into advanced group theory - challenging enough to be fun, yet not so hard that you become exhausted and lose your motivation to continue.  The exposition is pretty good too.  I like it.  If you're into finite group theory, check out Isaacs' book by that name - it is the best I've read across the board.
EDIT: Also, I took a look at the error log you posted for Rotman, and the majority of the changes are very nitpicky, such as replacing "one says that" with "we say that" in definitions.
Comment: Thanks for the input.  After reading just the very pleasant preface of Isaacs, I love it already!  I may very well go with that, since it is finite group theory that I'm most interested in.
Comment: Wehrfritz's Second Course in Group Theory is a nice stepping stone to Robinson's textbook (which is a nice stepping stone to Robinson's other textbooks, which are fundamental classics in their areas). If you are interested in the theory of (finite or infinite) solvable groups, then Robinson is a good book. However, you'd probably want to have finished Rotman first. :-)
Answer: Books I love, approximately in the order I could understand them (ignoring publication dates; also you might notice groups are finite unless otherwise specified):

Rotman's Group Theory (actually I don't really like it anymore, but I loved it back in the day)
Hall's Theory of Groups (awesome, low key, some deep results)

Isaacs's Finite Group Theory
Alperin–Bell Groups and Representations (for GL and Sylow; short)
Wehrfritz's Second Course (for solvable ideas)
Suzuki's Group Theory (elementary, but covers some serious matrial)

Robinson's Theory of Groups (working up to infinite soluble groups and finiteness conditions)
Hans–Kurzweil Theory of Finite Groups (clean, crisp; was the best until Isaacs's; still has best description of the transfer homomorphism, but Isaacs gives a better description of transfer itelf)
Gorenstein's Finite Groups (classic; you can start it earlier, but the maturity level required is uneven; probably need Isaacs's CTFG first)
Aschbacher's Finite Group Theory (clean, crisp; if you understand a chapter, then its amazing, but some chapters you might not get, and there is not much there to help you)

Doerk–Hawkes Finite Soluble Groups (the first two background chapters are actually the first three volumes of Endliche Gruppe condensed)
Huppert's Endliche Gruppe
P. Hall's Collected Works (well, I only read the finite ones)

Also try to read Isaacs's Character Theory of Finite Groups as soon as you can. I suspect it should be fine after Rotman or Hall. After that, James–Liebeck's Representations and Characters of Groups continues the same path. You can get pretty far without character theory, but it's just silly not to bask in its awesomeness.
Wilson's Finite Simple Groups, Carter's Simple Groups of Lie Type, Holt–Plesken Perfect groups, Leedham-Greene–McKay Structure of Groups of Prime Power Order, and Malle–Testerman Linear Algebraic Groups and Finite Groups of Lie Type are amazing too, but you probably know if you need them.
At any rate, this should give you an idea of the wonderful things you can read. :-)
Comment: Hi Jack,I was curious how you read/worked through these. Did you choose a book and work through it in a linear way? Did you do the exercises? Do you have any advice on working through these/ approaching group theory texts in general? Thank you.
Answer: I have the perfect recommendation for a student at your level, Alex. I reviewed the amazing graduate text, Finite Group Theory by I. Martin Isaacs, for the MAA online reviews several years ago and I still think it's the best currently existing second exposure to group theory. It's an extremely challenging book and students shouldn't even think about reading it without a strong undergraduate background in algebra, such as year long honors course based on Artin or Herstein. You certainly seem to have more then enough background to handle,so I'm happily recommending it. It's beautifully written by one of the top researchers in the field and contains many topics you won't find in standard texts.
To quote from my original review:

Each chapter comes with a freight car full of substantial exercises, ranging in difficulty from trivial to research level, many of them defining aspects of group theory not covered in the text proper, such as the Frattini subgroup, elementary abelian groups, the quasiquaternion and generalized quaternion groups, extraspecial groups, supersolvable groups and much, much more — some of which are later used in the text proper. The book also has the one telling characteristic of a text written by an active researcher in the field — the material covered reaches much closer to the research frontier then is usual. This is particularly clear in the chapters on subnormality and transfer theory, which contain many fairly recent results.
The book is amazingly clean. I couldn't find a single error. But the very best thing Isaacs brings to this book is the same thing he brings to all his textbooks — his wonderful style. Definitions, theorems and associated results are presented in a remarkably well organized, coherent manner, all in the author's terrific lively prose. In this regard, the book reads at times less like a textbook and more like a novel on the great narrative of the story of the development of finite group theory over the last twelve decades. The running theme unifying all these results in the narrative is the great accomplishment of the classification of finite simple groups. The flowing, eclectic style certainly conveys the vast love of the author for his chosen specialty and his great desire to set others on the same path.

Get this book. You'll thank me later, I promise.
Comment: I don't think Isaacs *Finite Group Theory* gets all that hard until maybe halfway through the book.  Personally I read it as a 3rd year undergraduate.  (I had taken up to graduate algebra by that point but much of it was ring and field theory.  I recall I had forgotten what normalizers and centralizers were, for example.)  I think Isaacs exposition is so good that a motivated student who is willing to do exercises could at least make it through the first few chapters even with only an introductory background in group theory.
Comment: By the way, that is a terrific and accurate review, @Mathemagician1234 .
Comment: Thanks for the review!  Just the preface did give me a very positive impression, and I'm glad that you were able to further confirm that it's a great book.  My background is diverse I suppose, but perhaps shakier than you may think.  I was able to learn group theory and a little ring theory from an algebraist through a reading course, but all my other algebra was just self-taught.  I am, however, willing to really work at it, and I bet the style of the text, as you describe it, will help as well.
Comment: @Alex If I may make a suggestion? Get yourself first a copy of Issacs' ALGEBRA:A GRADUATE COURSE to strengthen your background first,read through it and work as many of the problems as you can. Not only is it a terrific advanced book on algebra, it'll make the transition to his group theory book seemless.
Comment: Thanks for the suggestion.  I may just do that.  It would be a little disappointing putting off the group theory, but I was planning on shoring up my algebra at some point, and I knew I'd need some module theory eventually, so working through that book now would only be a good thing.
Comment: @Alexander Thanks,I appreiciate the kind words.I miss doing these reviews-but I couldn't make my dues and had to drop out of the MAA. I hope I can return to doing it at some point in the future.
Comment: @AlexanderGruber: Thanks for the comment.  I think it is just as well that I do some more algebra now.  It was going to happen at some point anyway, and it certainly won't hurt my chances of doing well at more advanced group theory.


Trigger the 'add more' button with javascript, to add a set amount of field values

Question: Is there any way to add a certain amount of field values to a standard node add/edit form (when unlimited option has been chosen) using a select input?
At the moment I have this working using javascript by triggering click on the 'add another' button, waiting a bit using setInterval (as the button disappears), then looping as many times as I need to, so the new fields gradually appear. This sort of works, but is very very dirty. Is there any proper way to add a certain number of field values based on a select option, and doesn't involved waiting?
Answer: I had the very similar problem, but with a group of fields that all needed to be added in sync. Using setTimeout to trigger is problematic because of the guess work involved.
One of the ways to do this is to use the ajaxSuccess event to chain a list of requests. After searching for a solution to my problem, I ended up creating a small plugin called group-add-more. This can work with both Drupal 6 as well as 7.


From array to array of arrays

Question: I'd like to transform an array like the following:
const myArray = [ 'a', 'b', 'c', 'd', 'e', 'f' ];

to something like this:
const transformedArray = [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e', 'f' ] ];

The only way I see is to do it with a good old for loop, but is there a more elegant way to do it with Array.prototype functions ?
Answer: You could do something like this using reduce, Math.floor and %

const myArray = ['a', 'b', 'c', 'd', 'e', 'f']

const newArray = myArray.reduce((acc, a, i) => {
  const index = Math.floor(i/2);
  acc[index] = acc[index] || [];
  acc[index][i % 2] = a
  return acc
}, [])

console.log(newArray)

Math.floor(i/2) gets which of the inner array the item belongs to
i % 2 gets which postion of the inner array the item belongs to
Answer: You can use Array.from() to create an array of chunks:

const myArray = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ];

const chunk = n => arr =>
  Array.from({ length: Math.ceil(arr.length / n) }, (_, i) =>
    myArray.slice(i * n, (i + 1) * n)
  );

const log = arr => console.log(JSON.stringify(arr));

log(chunk(2)(myArray)); // [["a","b"],["c","d"],["e","f"],["g"]]
log(chunk(3)(myArray)); // [["a","b","c"],["d","e","f"],["g"]]
Answer: The solution below uses reduce to iterate over the items in the array. We use a modulo check on the index of the item to determine whether or not we need to start a new array.
The code adds in an object when it's inspecting an index which doesn't need to be formed into a new array. We later filter these objects out. There may be a more elegant way of doing this, but I feel it works out quite nicely.

const letters = [ 'a', 'b', 'c', 'd', 'e', 'f' ];

const splitIntoChunks = (arrayIn, size) => arrayIn.reduce(
  (prev, curr, i) => [
    ...prev,
    i % size === 0
       ? arrayIn.slice(i, i + size)
       : { '__remove': true }
  ],
  [],
).filter(x => !x['__remove'])

const result = splitIntoChunks(letters, 2)

console.dir(result)
Answer: One other solution that has been given to me which I find the most readable.

const myArray = [ 'a', 'b', 'c', 'd', 'e', 'f' ];
const transformedArray = Array(myArray.length / 2).fill().map((_, i) => [myArray[i * 2], myArray[i * 2 + 1]]);
console.log(transformedArray);

From array to array of arrays


Exclude depth from mmenu

Question: I'm using https://mmenujs.com and would like to bypass a depth of the menu structure when it's displayed as a mobile menu. For example, desktop menu is like this:
1. MENU
   a. SUBMENU
      • link v
      • link w
   b. SUBMENU
      • link x
      • link y
      • link z
2. MENU
   a. SUBMENU
      • link v
      • link w
   b. SUBMENU
      • link x
      • link y
      • link z

But on mobile (via mmenu.js) I'd like to just show it like this:
1. MENU
   • link v
   • link w
   • link x
   • link y
   • link z
2. MENU
   • link v
   • link w
   • link x
   • link y
   • link z

Basically using a nested ul with a depth of 3, but bypassing depth #2 so the user doesn't have to do an extra tap to get past the submenus to get to the actual links.
Thanks!

 $('#mobile-nav').mmenu();

MENU 1

SUBMENU 1A

link v

link w

SUBMENU 1B

link x

link y

link z

MENU 2

SUBMENU 2A

link v

link w

SUBMENU 2B

link x

link y

link z

Comment: added... should've clarified that I'm using https://mmenujs.com here. It takes the nested UL and turns it into a sliding menu where each submenu is on its own screen. Trying to lose the intermediate step so the user just goes from Main Menu > Links instead of Main Menu > Sub-Menu > Links
Answer: You can't completely exclude a level like you ask for without doing some serious JS or PHP prior to calling the mmenu plugin.
But there are a couple of classnames you could add to the UL to try and prevent the mmenu plugin from turning it into a panel:

"NoPanel" : prevents the UL from being turned in to a panel.
"NoListview" : prevents the UL from being turned in to a listview.
"mm-listview_inset" : makes the UL appear as a default UL (with bullets).

Exclude depth from mmenu