Windows Server 2008 TCP connections limit

Question: I run my own game server made in .net 4.5 on a Windows Web Server 2008 VPS. I have done some research on a problem I have. My app doesn't accept clients, if there are already 1000 clients connected. This app is basically a one process running six Socket servers (each on diffrent port) and has no limitations to connected clients in its code. Here is some stats:
-------------------------------Server ID: 0 stats
[22-03-2014 14:37:39] Stats: Players online: 160
[22-03-2014 14:37:39] Stats: Connections: 164
-------------------------------Server ID: 1 stats
[22-03-2014 14:37:39] Stats: Players online: 515
[22-03-2014 14:37:39] Stats: Connections: 535
-------------------------------Server ID: 2 stats
[22-03-2014 14:37:39] Stats: Players online: 108
[22-03-2014 14:37:39] Stats: Connections: 111
-------------------------------Server ID: 3 stats
[22-03-2014 14:37:39] Stats: Players online: 59
[22-03-2014 14:37:39] Stats: Connections: 61
-------------------------------Server ID: 4 stats
[22-03-2014 14:37:39] Stats: Players online: 74
[22-03-2014 14:37:39] Stats: Connections: 75
-------------------------------Server ID: 5 stats
[22-03-2014 14:37:39] Stats: Players online: 52
[22-03-2014 14:37:39] Stats: Connections: 54
-----------------------------------Total stats
[22-03-2014 14:37:39] Stats: Total players: 968
[22-03-2014 14:37:39] Stats: Total connected: 1000

"Player" clients are logged-in players, and "connected clients/connections" is the amount of sockets being listened at a time (logged-in players + players logging in).
Is it a Windows Web Server 2008 limitation?
Answer: It appears to me that you might be in violation of the license agreement since you are using 2008 Web Edition:
From Here:

Windows Web Server 2008 is specifically designed to be used as a  single-purpose Web server . It is intended only for Internet accessible, front- end Web serving of Web pages, Web sites, and Web applications.

Anyway, the Web Edition of Server 2008 does indeed have a limit of 10 simultaneous half-open TCP connections, but no reasonable limit on active TCP connections, according to this Microsoft employee:

It depends on the edition, Web and Foundation editions have connection limits while Standard, Enterprise, and Datacenter do not.

But if you're consistently seeing the number of connections get stuck at exactly 1000, consistently, then I'm not yet convinced that half-open TCP connections are your problem.  Couldn't tell you more without scrutinizing your code.
But you should probably move off of Web Edition anyway, since you are using it for something other than running a web server. So the best answer I think you'll get given the information you've provided to us is no, there is no limit of active TCP connections that is anywhere near 1000 that is imposed either by the operating system or by the .NET framework. It is capable of handling many, many more than that.
Edit: I suppose you could try changing the registry key HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\EnableConnectionRateLimiting to 0, but that may or may not work because of your Web Edition of Windows Server, as well as opening you up to possible denial of service attacks (if you actually are having a problem with half-open TCP connections, which I'm not yet convinced that you are. Run a netstat and see if you have a bunch of connections that seem to be stuck in a SYN_SENT state.)
Comment: Technically my game server is a "Web application" since the game client is made in flash. But thank you, I will probably move to other Windows version to see if it fixes the problem.


Como melhorar a otimização do código em Python?

Question: Escrevi uma função que recebe uma lista de números positivos ou negativos e retorna a soma de todos os números que são divisíveis por números primos e o número primo em questão. Exemplo:
funcao = [12, 15] # resultado = [[2, 12], [3, 27], [5, 15]]
[2,12] = Apenas o 12 é divisível por 2, e 2 é primo
[3,27] = Os números 12 e 15 são divisíveis por 3, sua soma é 27, e 3 é primo
E assim por diante.
Meu código está funcionando mas ele está demorando muito para executar quando os números são muito grandes, como eu posso otimizá-lo?
def checkprimo(n):
for x in range(n):
    if x+1!=1 and x+1!=n and n%(x+1)==0: return False
return True
def sum_for_list(lst):
nReply = []
for x in range(len(lst)):
    nReply.append([y+1 for y in range(lst[x] if lst[x]>0 else lst[x]*-1) if lst[x]%(y+1)==0])
reply = sorted(set([y for x in nReply for y in x] ))
if 1 in reply: reply.remove(1)
chose = []
for i,x in enumerate(reply):
    chose.append([0,0])
    for y in lst:
        if y%x==0 and checkprimo(x)==True:
            chose[i]=[x,y+(chose[i][1])]
for x in range(chose.count([0,0])):
    chose.remove([0,0])
return chose

Answer: Não sei bem se é isso que você precisa, removi alguns loops desnecessários, e usei uma formula otimizada para detectar se é Primo.
Objetive esse resultado:
Para 12,15 = [[2, 12], [3, 27], [5, 15]]

Time elapsed (hh:mm:ss.ms) 0:00:00.391751

Para 141412,1000002,12202001 =
[[2, 1141414], [35353, 141412], [3, 1000002], [166667, 1000002], [7, 12202001], [1743143, 12202001]]

Time elapsed (hh:mm:ss.ms) 0:00:01.305071

import math
def is_prime(n):
  for i in range(2,int(math.sqrt(n))+1):
    if (n%i) == 0:
      return False
  return True

def sum_for_list(lst):
    new_reply = []
    for x in lst:
        for y in range(2,abs(x)):
            if abs(x)%y == 0:
                if is_prime(y):
                    added  = False
                    for idx,value in enumerate(new_reply):
                        if y == value[0]:
                            new_reply[idx] = [y, x + value[1]]
                            added = True
                            break
                    if not added: new_reply.append([y,x])
    return new_reply

from datetime import datetime
start_time = datetime.now()

lst = [12,15]
sum_for_list(lst)

print('Time elapsed (hh:mm:ss.ms) {}'.format(datetime.now() - start_time ))


É possível utilizar o cordova com o nodemon?

Question: Estou começando a estudar mais o cordova porém toda vez eu preciso utilizar o
cordova run browser
para subir o servidor e testar o app, com node eu uso o nodemon para deixar o servidor atualizando sem precisar reiniciar, alguém sabe alguma alternativa pra deixar o cordova rodando pra sempre, do mesmo jeito que o nodemon funciona?
Cheguei a testar o nodemon da seguinte maneira:
nodemon --exec cordova run browser, porém sem sucesso.
Answer: Encontrei uma solução viável, o Cordova Browser-Sync Plugin.
Com ele é possível editar o html do app sem precisar atualizar nada, ele faz um refresh no emulador que você esteja usando.


How to change the element of array in swift

Question: I'm having difficulty with this function:
func sort(source: Array<Int>!) -> Array<Int>! {
   source[0] = 1
    ......
    return source
}

An error happens:

Why can't I directly assign the value to the specific element in the array?
Comment: If you want to modify the array that was passed in, add the `inout` keyword: `func sort(inout source: [Int])` and call it like this `sort(source: &myArray)`.
Comment: Be aware that the code crashes if `source` is `nil` or empty.
Answer: The variable sort is immutable because it's a parameter. You need to create a mutable instance. Also, there's no reason to have the parameter and return value as implicitly unwrapped optionals with the ! operator.
func sort(source: Array<Int>) -> Array<Int> {
   var anotherSource = source // mutable version
   anotherSource[0] = 1
   ......
   return anotherSource
}

Comment: and the preferred way to write `Array` is `[Int]`
Comment: It's more common to write it as `[Int]`, yes. Although in some rare circumstances `Array` does make sense.
Comment: Generic never be nil?
Comment: If you leave off the `!` then it's never nil and it's much safer.
Comment: Thanks, my memory is in swift1.
Comment: Avoid implicitly unwrapped `Optional` and force-unwrapping `Optional` as much a possible. Using them is a good way to shoot yourself in the foot.


Getting a NoClassDefFoundError when trying to install a project with maven

Question: Trying to build an eclipse plugin. When I try to run mvn install I get this error message:
java.lang.NoClassDefFoundError: org/eclipse/xtext/common/types/DefaultCommonTypesRuntimeModule
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

Apparently a NoClassDefFoundError means that the class isn't available at runtime. This plugin doesn't actually run xtext code but it is built using an xtext language plugin. My guess would be that there would be a problem with getting that code at runtime? I maven installed the parent plugin and even linked it's jar locally through maven and this still pops up. Ideas?
Also heres the project's pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.fu_berlin.inf.archnemesis</groupId>
    <artifactId>archnemesis-pmd-rule</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Archnemesis PMD Plugin</name>
    <description>Archnemesis rule implementation for PMD</description>

    <dependencies>
        <dependency>
            <groupId>de.fu_berlin.inf.archnemesis</groupId>
            <artifactId>de.fu_berlin.inf.archnemesis</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd</artifactId>
            <version>5.0.5</version>
        </dependency>
        <dependency>
            <groupId>de.fu_berlin.inf.archnemesis</groupId>
            <artifactId>de.fu_berlin.inf.archnemesis-core-rule</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.xtend</groupId>
            <artifactId>org.eclipse.xtend.lib</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Comment: Please check if your `.m2` directory contains the jar or not and try to run mvn with `-U` command  `mvn install -U`
Answer: Try adding the following dependency to your pom.xml
<dependency>
    <groupId>org.eclipse.xtext</groupId>
    <artifactId>org.eclipse.xtext.common.types</artifactId>
    <version>2.8.4</version>
</dependency>


Como realizar TDD utilizando Hibernate

Question: Gostaria de saber como realizar o TDD usando Hibernate. Me informaram que este ORM armazena memória antes de gravar no possível, então queria saber como testar um dado que está em memória. Por exemplo: executo o comando para salvar e faço um teste para verificar se este dado esta salvo, é possível?
Answer: Não é possível utilizar somente o cache de primeiro nível do Hibernate para utilizar em casos de testes. A melhor abordagem para esse problema é configurar o Hibernate para que ele use algum banco de dados em memória.
Alguns banco de dados em memória que você pode utilizar são: HSQLDB, o Apache Derby ou o H2DB.
Basta criar um DataSource para algum destes BDs e utiliza-lo na configuração do Hibernate utilizada nos seus casos de testes.
Exemplo: http://www.java2s.com/Tutorial/Java/0350__Hibernate/HibernateandHSQL.htm
Comment: Esta é a estratégia adequada para a maioria das situações.
Answer: Você tem que entender o seguinte, o cache está atrelado ao PersistenceContext que é criado quando você cria um EntityManager.
Você pode usar sem problemas o JUnit com EntityManager para testar seu DAO. O que você deve fazer é:

Adicionar o HSQLDB como dependência ou algum outro banco em memória
Ter um persistence.xml somente para o teste, nesse caso, bastaria adicioná-lo dentro de um diretório test, no caso do maven, que automaticamente essa troca de contexto seria feita.
Crie um EntityManagerFactory estático:
private static EntityManagerFactory entityManagerFactory;
Criar um método estático anotado com @BeforeClass que criará o EntityManagerFactory:
@BeforeClass
public static void createPersistenceUnit() {
    entityManagerFactory = Persistence.createEntityManagerFactory("PU")
}
Crie um método estático anotado com @AfterClass para matar o EntityManagerFactory:
@AfterClass
public static void closePersistenceUnit() {
    entityManagerFactory.close();
}
Crie um método anotado com @Before para iniciar o EntityManager a cada teste:
@Before
public void beforeTest() {
    entityManager = getEntityManagerFactory().createEntityManager();
}
Crie um método com @After para finalizar o EntityManager após cada teste:
@After
public void finishTest() {
    entityManager.close();
}

Desse modo você vai ter um EntityManager para cada teste e seu cache de primeiro nível não será problema.
Esse post é meio velhinho, mas fala sobre isso: http://uaihebert.com/tdd-com-hsqldb-jpa-e-hibernate/
Answer: Acho que vocês esta indo pelo caminho de testar a tecnologia e não o domínio do negócio de seu problema.
Acho que você poderia tentar "mockar" o hibernate com um mockito por exemplo e não se preocupar tanto com a tecnologia e sim com as regras de negócio, afinal é pra isso que usamos o TDD... :-)
Comment: Assim você continua testando a tecnologia, a não ser que a regra de negócio é relacionada a ter o objeto em memória ou não...

Pode ser que "mockar" o método que retorna o objeto da memória, lhe ajude a testar os outros métodos de negócio, dá uma olhadinha no mockito, pode ajudar alguns exemplos dele...
Comment: Pois é, mas eu queria salvar em objeto e testar uma validação do tipo, se aquele objeto já existe no banco. Mais para testar o método.
Comment: A regra seria tipo, se o objeto já existe quando consultado ao BD, ele não insere, coisas deste tipo.
Comment: Me parece algo como um update. Mas se não for este o caso, então poderia ser algo para um cache fora do hibernate... talvez...

Como realizar TDD utilizando Hibernate


Deciphering the notation of this slash chord

Question: I'm having a hard time "really" understanding the following slash chord:
Notes in  order:

Ab  Eb  Gb  -  Db  F  Gb  Bb

My logic is telling me it's a Gb maj7/13 / Ab, butI would like to give this chord a notation beginning with the root note "Ab".
The best I came up with (so far) is Ab 7/9/11/13 (no3).
I don't think this is correct. Can it be some kind of dominant sus chord (ex. 9sus4)At least it sounds like one. Dominant, sus, IV/V feel.
Is there a way to notate this chord NOT as a slash chord?
Answer: TL;DR: Ab13sus4
There are always quite a few different ways to notate a particular set of pitches as a chord symbol. And, yes, this can get fairly ambiguous once you have a large number of pitches. In the end, I think it is always important to: try to explain a chord in the simplest way possible; use some common-sense (so, if there is some easily discernible triadic element for instance, it would seem best to acknowledge it).
If you want exactly the same notes and same voicing every time, it is best to use actual notation on a stave. However, if you need to describe the pitches at any one point, for chord symbols above a part, or for analysis, for instance, you will just need to be as accurate as possible.
Personally, I like your Gb maj7/13/Ab idea. You can simplify this further: Gbmaj13 implies the major 7th and 9th (and you often miss out the 11th, which would be Cb, in a 13th chord). So this would mean Gbmaj13/Ab would work. However, I would be bothered by the fact that the root of the Gb chord is above the 13th (Eb), while this isn't incorrect, the Ab and Eb fifth at the bottom of the chord more strongly suggest some kind of Ab chord, than Gb chord (this is the common sense taking over, even if we end up with a longer chord description).
So, how about Ab13sus4? The 13th implies the 7th and 9th. Using tertian harmony, you don't really need to describe the Db as an 11th, as there is no 3rd. So it can be described as a sus4.
There are other ways to notate this chord. A couple of things to be aware of: if you ask for a 13th chord a player will often miss out the 11th, even though strict tertian harmony would suggest that a 13th implies an 11th; if you do ask for an 11th a player will usually miss out the major 3rd, as it creates a "nasty" minor 9th interval between the 3rd and 11th. So, I guess Ab13add11 would work okay. (Although I usually take an add11 to mean that you add an 11th without the 7th and 9th, here they are implied by the 13th anyway.)
Comment: Wow! Great explanation. Thank you Bob! Like both ideas, the 13sus4 and the 13add11. Personally, i would choose the 13sus4 because of the missing 3rd. Learning a lot from your answer. Pretty new to the jazz chord notation game.
Comment: Yes, I think the sus4 is the best notation for the same reason… It specifically asks for a Db *instead* of a C.
Comment: Excellent diagnosis, doctor! Yes, the sus bit is more accurate, as there is no 3rd,(assuming root = Ab) crucial to anything with a number 7 or more. Nice sounding chord, too.
Answer: For me, it is a very simple Eb minor/Ab, since in jazz performance the 7th and 9th of the minor chord are always available. This chord is a II/V7 which resolves most likely in a I, in this case Db.
Answer: Notes: (on the piano) Ab Eb Gb - Db F Gb Bb (in that order).
Gb Major 9 add 13/Ab.
Gb Major scale degrees 1,3,5,7,9 with added 13 lowest note being Ab.
From Gb Major scale: Gb, Bb, Db, F, Ab with added Eb, lowest note being Ab.
Gb being root is often doubled.
The notes of any chord can be played in any register as long as the slash note is the lowest.


ComboBox on selction

Question: i am trying to get the combo-box when a option is selected it does something like print some text but i dont know how to do it after hours of trying to figure it out.i know it has something to do with the event listener. New to java. Any help is appreciated.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComboBox;

public class Frame extends JFrame
{
    //Instance Variables.
     private static final int FRAME_WIDTH = 500;
     private static final int FRAME_HEIGHT = 400;

     public Frame()
     {
        Venue1();
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
     }

     private void Venue1()
     {
         //String [] Venues = {"Elvis","Gorillaz", "Imagine Dragons","Michael Jackson"};

         JComboBox DropMenu = new JComboBox();
         DropMenu.addItem("Elvis");
         DropMenu.addItem("Gorillaz");
         DropMenu.addItem("Imagine Dragons");
         DropMenu.addItem("Michael Jackson");
         DropMenu.setEditable(false);
         DropMenu.setVisible(true);

         JPanel panel = new JPanel();
         JLabel label = new JLabel("Pick a venue");

         add(panel);
         panel.add(label);
         panel.add(DropMenu);

        ActionListener listener = new listener();
        DropMenu.addActionListener(listener);

     }//End of Venue Method.
}//End of class.

//Button Listeners

class listener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {

    }
}

    enter code here

Answer:
To get selected value from your JComboBox is simple as this :

DropMenu.getSelectedValue();

To get selection when changing the selection :
MyItemListener actionListener = new MyItemListener();
DropMenu.addItemListener(actionListener);

class MyItemListener implements ItemListener {
    // This method is called only if a new item has been selected.
    public void itemStateChanged(ItemEvent evt) {
        JComboBox DropMenu = (JComboBox) evt.getSource();
        Object item = evt.getItem();
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            // Item was just selected
        } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
           // Item is no longer selected
        }
    }
}

Comment: Thanks for the reply :). if i want something like print text what parameters would i put in the if statement. something like if(SelctedValue == "Elvis")
Comment: i made the changes
Comment: @JordanOLoughlin `if(SelctedValue == "Elvis") ` is not how `String`s are compared in Java, you want to use something more like `if("Elvis".equals(selctedValue))`
Comment: i know that but for some reason == worked?if(cb.getSelectedItem() == "Gorillaz")
   {
    System.out.println("You have selected Gorillaz");
   }
Comment: @JordanOLoughlin you advise you to take a look on this link https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java
Answer: In your ActionListener, you need to determine if the source of the event is a JComboBox, if it is, you can cast it and get the selectdValue
class listener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() instanceof JComboBox) {
            JComboBox cb = (JComboBox)event.getSource();
            System.out.println(cb.getSelectedItem());
        }
    }
}

I strongly recommend having a look at:

How to Use Combo Boxes
How to Write an Action Listener

ComboBox on selction


default value is not setting for select using ng-init

Question: I'm not able to setting the default value for drop down using ng-options, I'm able getting default value in response but in drop down it's not setting.
in js
 $scope.personalDetails = [
            {
               'fname':'Muhammed',
                'lname':'Shanid',
                'email':'email@example.com',
                'check' : 'Y',
                'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            },
            {
                'fname':'John',
                'lname':'Abraham',
                'email':'firstname.lastname@example.com',
                'check' : 'N',
                'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            },
            {
                'fname':'raj',
                'lname':'komali',
                'email':'email@example.com',
                'check' : 'N',
                 'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            },
            {
                'fname':'Roy',
                'lname':'Mathew',
                'email':'email@example.com',
                'check' : 'N',
                 'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            }];

in html

        <select ng-model="personalDetail.selectedDpn" ng-init="personalDetail.selectedDpn = personalDetail.checkxDrpn[1].key" ng-options="option.selectVal for option in personalDetail.checkxDrpn track by option.key" value="personalDetail.checkxDrpn[1].key"></select>
          default value : {{personalDetail.checkxDrpn[1].key}}

Answer: You need to set the model variable as object,
 ng-init="personalDetail.selectedDpn= personalDetail.checkxDrpn[1]"

DEMO

var app = angular.module('demo', []);
app.controller("profileController", function($scope) {
 $scope.personalDetails = [
            {
               'fname':'Muhammed',
                'lname':'Shanid',
                'email':'firstname.lastname@example.com',
                'check' : 'Y',
                'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            },
            {
                'fname':'John',
                'lname':'Abraham',
                'email':'email@example.com',
                'check' : 'N',
                'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            },
            {
                'fname':'raj',
                'lname':'komali',
                'email':'firstname.lastname@example.com',
                'check' : 'N',
                 'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            },
            {
                'fname':'Roy',
                'lname':'Mathew',
                'email':'firstname.lastname@example.com',
                'check' : 'N',
                 'checkxDrpn' : [
                  { key: 'Y',selectVal: "Yes"},
                  { key: 'N',selectVal: "No"}
                ]
            }];

});
<!DOCTYPE html>

  <link rel="stylesheet" href="style.css">

             <select  ng-init="personalDetail.selectedDpn= personalDetail.checkxDrpn[1]" ng-options="value as value.selectVal for value in personalDetail.checkxDrpn track by value.key"
        ng-model="personalDetail.selectedDpn" ></select>

default value : {{personalDetail.checkxDrpn[1].selectVal}}


Help with solving this fraction

Question:
Solve for $h$:
  $$125=\pi \left (\dfrac{5}{\sqrt[3]{\pi}} \right )^2h$$

This is from an optimization problem, in which the volume is the constraint of $125$. I've done everything in the problem except finding this last dimension $h$. The answer is $h=\dfrac{5}{\sqrt[3]{\pi}}$, but I can't seem to get that. Can you show steps please? Thanks.
Comment: Convert all the radicals to exponents and then use exponent laws to simplify everything if that helps. Then divide both sides by the coefficient of h.
Answer: $$125 = \frac{25 \pi}{\pi^{2/3}}h = 25\pi^{1/3}h \to h = \frac{5}{\pi^{1/3}}.$$
Answer: Note that
$$\pi \left(\frac{5}{\sqrt[3]{\pi}}\right)^2 = \frac{25 \pi}{\sqrt[3]{\pi}^2} = 25 \sqrt[3]{\pi}$$

Help with solving this fraction


Como fazer uma função retorna o valor que está no arquivo (c++ e fstream)

Question: estou tentado aprende a usar a biblioteca fstream do c++, mas acabei tendo a dificuldade de usar em uma função fora da main(), quando eu rodo o código ele não retorna nada
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

string ler_arquivo(string arquivo){
  fstream ler_arquivo;
  string texto;

  ler_arquivo.open("texte.txt",fstream::in);

  if(ler_arquivo.is_open()){
    while(!ler_arquivo.eof()){
      getline(ler_arquivo, texto);
    }
  }else{
    texto = "erro na leitura do arquivo \n";
  }
  ler_arquivo.close();
  return texto;
}

int main() {
  fstream arquivo;  //recebe o aarquivo
  string linha;
  //vector<double> num;

  linha = ler_arquivo("texte.txt");

  cout << linha;
return 0;
}

pelo teste que fiz o código entra na função e no if e o loop do while normalmente, mas acaba retornando nada
(obs.: o arquivo tem algumas frases como exemplo, mas é só como afim de estuda de como funciona)
(obs.2: a string recebe tudo o que está no arquivo)
Comment: declare uma auxiliar fora do primeiro escopo  do `for` ...    `string auxiliar` abaixo do `getline`  atribua a ela o  **texto** `aux+=texto;`  em vez de `return texto;`  retorne como `return aux;`
Answer: string ler_arquivo(string arquivo){
  fstream ler_arquivo;
  string texto;

  ler_arquivo.open("texte.txt",fstream::in);

  if(ler_arquivo.is_open()){
    while(!ler_arquivo.eof()){
      getline(ler_arquivo, texto);
    }
  }else{
    texto = "erro na leitura do arquivo \n";
  }
  ler_arquivo.close();
  return texto;
}

Então ler_arquivo() deveria receber o nome do arquivo e retornar uma string. Mas a julgar pelo código que escreveu esperava retornar o conteúdo do arquivo todo? Em uma string só? Uma vez só?
E declarou
    fstream ler_arquivo;

dentro da função de mesmo nome. Confuso. E não usou arquivo em lugar nenhum. Então é melhor repensar.
O exemplo abaixo retorna a primeira linha do arquivo se ele for aberto com sucesso. Pode comparar com o modo que usou e talvez ajude.
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string ler_arquivo(string arquivo)
{
    ifstream entrada{arquivo};
    string texto = "erro na leitura do arquivo \n";
    if (not entrada.is_open()) return texto;
    std::getline(entrada, texto,'\n');
    return texto;
}

int main()
{
    auto linha = ler_arquivo("teste.txt");
    cout << linha << "\n";
    return 0;
}

Comment: [Como identificar o anti-padrão TOCTOU? Como evitá-lo/removê-lo?](https://pt.stackoverflow.com/questions/360272/como-identificar-o-anti-padr%c3%a3o-toctou-como-evit%c3%a1-lo-remov%c3%aa-lo/361869?r=SearchResults&s=1%7C55.5581#361869)
Comment: Eu não o negativei, mas tenho certeza do motivo dos negativos.  Aqui `if (not entrada.is_open())` jamais oriente a fazem isso, em C/C++ e linguagens de baixo nível quando faz esse teste e em seguida tenta ler/escrever um arquivo você embute no seu programa uma vulnerabilidade a ser explorada chamada TOCTOU que permite ao atacante de baixa credencial tomar o controle do OS.  Nesses casos em C emule o tratamento de erros com setjmp/longjmp e em C++ use um tratamento de erros. Em linguagens médio e alto nível explorar o TOCTOU apenas congela o thread onde foi inserida a vulnerabilidade.
Comment: Os comentários se estenderam e o excesso de ruído foi removido **(** após intervalo suficiente para notificação e leitura **)**, conforme sinalização da comunidade - foram mantidos os que tem relação direta com o conteúdo dos posts. (este aqui também será removido em algum tempo, é apenas um feedback sobre a remoção dos anteriores para evitar mal entendido). Para discurtir o funcionamento do site, critérios de voto, escopo e qualquer ponto que trate do funcionamento da comunidade podem usar o site [meta].


Java array is being rewrited :(

Question: I´ve got a problem with my java project. I need to delete element from array and that´s taken care of. But the problem is that even though it removes the element (which I checked in debugging), when I click on the button, as far as i know, it basically rewrites the array so the fact that i removed the element doesn´t even matter. Any suggestions?
public class Words {

    Random r = new Random();
    public String[] poleSlov = {"a","b","c","d","e","f","g","h","i"};

    public String chooseRandomWord() {
        int tmp = r.nextInt(poleSlov.length);
        String p = poleSlov[tmp];

        String[] result = new String[poleSlov.length - 1];
        for (int i = 0; i < result.length; i++) {
            result[i] = poleSlov[((i < tmp) ? i : i + 1)];
        }

        poleSlov = result.clone();

        return p;
    }
}

private void btnNoveSlovoActionPerformed(java.awt.event.ActionEvent evt) {

    Words s = new Words();
    labSlovo.setText(s.chooseRandomWord());

}

Answer: btnNoveSlovoActionPerformed() creates a new Words instance, having a new poleSlov array. Since s is a local variable, after btnNoveSlovoActionPerformed() returns, the object it refers to is no longer accessible.
One possible fix is to call chooseRandomWord() on the current instance :
private void btnNoveSlovoActionPerformed(java.awt.event.ActionEvent evt)
{
    labSlovo.setText(chooseRandomWord());
}

Comment: that´s what i know, but how to fix it? :D
Answer: This happens because the actual String poleSlov is always the same. When a new instance of Words is created, poleSlov is redeclared with the same contents. I'd advise thinking about how you can edit this String's contents.

Java array is being rewrited :(


Why my program is skipping the digit 8 and 9?

Question: I need some help.
I designed the program to give me the first digit and then skip the next digit, and so on. But every time it comes to the digit 8, the result is 10 instead of 8, so with the digit 9, it will be 11. This does not happen with any digit other than 8 and 9.
After many attempts, I noticed that the same thing is repeated when sum1 reaches 17. If you add 1 or another number, it will skip 18 and 19, and takes the value 20.
It seems to me as if the program is 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22.
#include <cs50.h>
#include <stdio.h>

int main(void)
{
  int count = 8 ;
  long number = 12345678 ;
  long the_remainder ;
  long sum1 = 0 ;

  for (int i = 0 ; i < (count / 2) ; i++)
  {
     the_remainder = number % 10 ;

     printf(" the_remainder is %lo\n", the_remainder);

     number = number / 100 ;

     sum1 = sum1 + the_remainder ;

     printf("the sum is : %lo\n", sum1);
  }
}

The result
Comment: duplicates: [What does the format specifiers "%02x " and "%3o " do in this code?](https://stackoverflow.com/q/58262817/995714), [Why do the conversion specifiers, %o and %x, work differently for printf() and scanf() in C?](https://stackoverflow.com/q/33216448/995714), [What is the following format specifier doing?](https://stackoverflow.com/q/66020521/995714)
Answer: The %o format specifier outputs in octal format, which is why the value 8 appears as 10.
You want to use %ld instead, which print in decimal.
Answer: You are using the format specifier %o which will print out octal (i.e. base-8) integers. There is obviously no digit 8 or 9 in base-8.
Use %d or %ld for base 10.
Answer: You should try %d instead of %o. Because %o will print octal integers and your expected answer is of base 10 (decimal).


MpAndroidChart - How to fill area between two lines on a CombinedChart?

Question: This answer to a similar question shows how to fill the area between two lines on an MpAndroidChart LineChart.
However, rather than using LineChart, I am using CombinedChart (as I also have bar data to show). But applying the above solution to a CombinedChart results in this exception:

java.lang.ClassCastException: org.example.MyLineLegendRenderer cannot
  be cast to com.github.mikephil.charting.renderer.CombinedChartRenderer
          at com.github.mikephil.charting.charts.CombinedChart.setData(CombinedChart.java:96)

So I have looked at CombinedChartRenderer but (unlike LineChartRenderer), it does not seem to contain any functionality for filling.
So how can I fill the space between two lines on a CombinedChart?
Answer: You can override the createRenderers() method in the CombinedChartRenderer class. This way you can use your own renderer for each and every type of graph that you use in the your CombinedChart .
It will be something like this :
public class MyCombinedChartRenderer extends CombinedChartRenderer {

    public MyCombinedChartRenderer(CombinedChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

    @Override
    public void createRenderers() {
        this.mRenderers.clear();
        CombinedChart chart = (CombinedChart)this.mChart.get();
        if (chart != null) {
            CombinedChart.DrawOrder[] orders = chart.getDrawOrder();
            CombinedChart.DrawOrder[] var3 = orders;
            int var4 = orders.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                CombinedChart.DrawOrder order = var3[var5];
                switch(order) {
                    case BAR:
                        if (chart.getBarData() != null) {
                            this.mRenderers.add(new BarChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case BUBBLE:
                        if (chart.getBubbleData() != null) {
                            this.mRenderers.add(new BubbleChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case LINE:
                        if (chart.getLineData() != null) {
                            // replace with you own Line chart renderer
                            //this.mRenderers.add(new LineChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                            this.mRenderers.add(new MyLineChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case CANDLE:
                        if (chart.getCandleData() != null) {
                            this.mRenderers.add(new CandleStickChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case SCATTER:
                        if (chart.getScatterData() != null) {
                            this.mRenderers.add(new ScatterChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                }
            }
        }
    }
}

In the above code, in the switch block when case LINE: is encountered, we replace the default renderer with our own version : MyLineChartRenderer.
Then finally in your Activity code, you can use this renderer with your CombinedChart :
CombinedChart combinedChart = (CombinedChart) findViewById(R.id.barChart);
combinedChart.setRenderer(new MyCombinedChartRenderer(combinedChart, combinedChart.getAnimator(), combinedChart.getViewPortHandler()));

Comment: Finally, to fill the area between two (horizontal bezier) lines, please see my answer to this other SO question: https://stackoverflow.com/a/50118570/1617737


Are Random Forests good at detecting interaction terms?

Question: How good random forests actually are at finding interactions between variables? Does adding interaction terms explicitly to the model increase the predictive power of random forests?
Comment: See also https://datascience.stackexchange.com/a/61279/55122
Comment: And to upgrade some Related questions to Linked ones:
https://stats.stackexchange.com/q/157665/232706
https://stats.stackexchange.com/q/146087/232706
https://stats.stackexchange.com/q/396923/232706
Answer: @gunes has already given the answer, that random forests do work well with interactions. It may help to give a practical example, though. For that, we will need some toy data
set.seed(2021)
n = 5000
x1 <- rnorm(2*n)
x2 <- runif(2*n)
interaction <- x1 * x2
pseudointeraction <- sample(interaction)
x3 <- rbeta(2*n, 2, 10)
epsilon <- rt(2*n, df = 1)

y <- 1*x1 + 2*x2 + 3*x3 + 10*interaction

example <- data.frame(x1 = x1, x2 = x2, x3 = x3, x4 = rbeta(2*n, 5, 5), x5 = runif(2*n,-10, 10),
                      interaction = interaction, pseudointeraction = pseudointeraction,
                      y = y)

Basically we have a y that is a linear function of x1, x2, x3 and the interaction x1*x2. In the dataset there is also a permutation of the interaction term and two more random variables, all of which bear no real information. We split this into a training and an equally sized testing dataset like so:
train <- example[1:n,]
test <- example[(n+1):(2*n),]

As the toy data have been modelled in a linear model, linear regression should be able to solve that puzzle easily:
> linear1 <- lm(y ~ . - pseudointeraction , data = example)
> summary(linear1)

Call:
lm(formula = y ~ . - pseudointeraction, data = example)

Residuals:
       Min         1Q     Median         3Q        Max
-1.071e-12 -3.400e-16  5.000e-17  4.700e-16  1.524e-12

Coefficients:
              Estimate Std. Error    t value Pr(>|t|)
(Intercept) -1.641e-14  9.004e-16 -1.823e+01  < 2e-16 ***
x1           1.000e+00  4.293e-16  2.329e+15  < 2e-16 ***
x2           2.000e+00  7.480e-16  2.674e+15  < 2e-16 ***
x3           3.000e+00  2.076e-15  1.445e+15  < 2e-16 ***
x4           3.798e-15  1.423e-15  2.669e+00  0.00762 **
x5           2.698e-18  3.725e-17  7.200e-02  0.94225
interaction  1.000e+01  7.415e-16  1.349e+16  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.145e-14 on 9993 degrees of freedom
Multiple R-squared:      1, Adjusted R-squared:      1
F-statistic: 1.647e+32 on 6 and 9993 DF,  p-value: < 2.2e-16

The linear modell is wrong in declaring x4 a significant predictor but as it was to be expected, $R^2$ is about 100%.
If we take the interaction term away or exchange it for the permuted interaction term, things get far worse as was to be expected:
> linear2 <- lm(y ~ . - interaction, data = example)
> summary(linear2)

Call:
lm(formula = y ~ . - interaction, data = example)

Residuals:
     Min       1Q   Median       3Q      Max
-17.4533  -1.2764   0.0621   1.2977  14.8616

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)
(Intercept)       -0.1791910  0.1214622  -1.475    0.140
x1                 6.0262464  0.0287402 209.680   <2e-16 ***
x2                 2.0022413  0.1009082  19.842   <2e-16 ***
x3                 3.0676424  0.2800356  10.954   <2e-16 ***
x4                 0.1791824  0.1919885   0.933    0.351
x5                 0.0005224  0.0050245   0.104    0.917
pseudointeraction  0.0121493  0.0496305   0.245    0.807
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.894 on 9993 degrees of freedom
Multiple R-squared:  0.816, Adjusted R-squared:  0.8159
F-statistic:  7385 on 6 and 9993 DF,  p-value: < 2.2e-16

$R^2$ dropped to 82%. So the interaction term is important in this model.
Let's grow a randomForest with the interaction term and one without it:
library(randomForest)
rf1 <- randomForest(y ~ . - interaction, data = example, mtry = 3)
rf2 <- randomForest(y ~ . - pseudointeraction, data = example, mtry = 3)

print(rf1)
print(rf2)

This will take some time to compute and the print results will claim that the random forest with an interaction term given explained 99.67% of the variance while the random forest with the interaction term explained 99.93% of the variance.
It is hard to compare within-sample $R^2$ to Out-of-bag-performance, so we predict our test data set with all four of our models we have so far:
linear1.on.test <- predict(linear1, data = test)
linear2.on.test <- predict(linear2, data = test)
rf1.on.test <- predict(rf1, data = test)
rf2.on.test <- predict(rf2, data = test)

In a next step we compute the absolute residuals and plot them as boxplots:
boxplot(linear1.on.test - test$y, linear2.on.test - test$y, rf1.on.test - test$y, rf2.on.test - test$y,
        ylim=c(-7, 7), xaxt="n", ylab = "residuals")
axis(1, at = 1:4, labels = c("linear with I", "linear w/o I", "rf w/o I", "rf with I"))

So the linear model with interaction term fits best to how the data was constructed and thus predicts best. The linear model without interaction term has no flexibility to adapt and has the worst predictive value of all models.
For the random forests we see quite similar results with and without the interaction term. Stating the interaction term explicitely appears to be helpfull but the model without it is flexible enough to give good predictions without that.
Real world interactions will rarely follow the rules of exact multiplication so the small advantage of computing the interaction term for the model may be even smaller for real world interactions.
All of this will be influenced by sample size, number of features, tuning parameters and so on but the short take away message is that whilst feature building is a good thing, random forest can detect and make use of interactions even if they are not stated in the call to randomForest.
Feel free to run this code with different set.seed values, compute RMSEs etc.
Answer: Yes, tree based methods are good at detecting interactions, but not always. For example, using $x_1\lessgtr0$ and $x_2\lessgtr0$ in subsequent levels of the tree would be equivalent to using $x_1x_2\lessgtr0$ on one level. That said, since you set a max depth hyperparameter in random forests, adding promising interactions will decrease your overall depth and paves the way for more performance via leaving the remaining levels for other features.


How would I go about converting a string with a two digit number to int without using if statements, character arrays, apstrings, or atoi?

Question: As the question says, it can't use if statements, character arrays, apstrings, or atois. We've only had 5 classes, so we haven't done anything too complicated and I'm completely lost. Thank you for your help!
Comment: you should at least mention the programming language, also tag it
Comment: Can you be more clear as to what language you're talking about and platform if appropriate.
Answer: If you know that the string always contains two characters that are digits, you can just get the character codes and convert to the digit value, then put them together to a number.
Example:
int number = (str[0] - '0') * 10 + (str[1] - '0');

Comment: Thanks for the reply, although I forgot to mention that it's for C++, not for C#.
Comment: The code to do that in C++ happens to be identical to C#, so you can accept this answer.
Comment: Code works fine, but isn't this an array? Or am I just being completely wrong here.
Comment: @jjesh: A string is just an array of characters, and the bracket syntax is just another way of accessing items using a pointer anyway. You can use `*str` and `*(str+1)` to access the first and second characters using the string pointer, but that is just a syntactic difference, the generated code is identical to the bracket syntax.
Answer: C++11 to the rescue!
try {
  int i = std::stoi(str);
} catch (std::exception const&) {
  // Invalid conversion
}

(Probably cheating if this is an exercise...but you only excluded atoi.)
Answer: const char* str = "97";

int number = (str[0] - '0') * 10 + (str[1] - '0');

Comment: Thank you, that seems to work. But isn't that an array?
Comment: in C, a string is a special kind of an array of characters, so anything you can do on an array, you can do on a string.
Answer: no if, no array, no atoi, but ugly:
int ConvertToInt(const string &str)
{
    return
        str == "99" ? 99 :
        str == "98" ? 98 :
        str == "97" ? 97 :
        str == "96" ? 96 :
        str == "95" ? 95 :
        str == "94" ? 94 :
        str == "93" ? 93 :
        str == "92" ? 92 :
        str == "91" ? 91 :
        str == "90" ? 90 :
        str == "89" ? 89 :
        str == "88" ? 88 :
        str == "87" ? 87 :
        str == "86" ? 86 :
        str == "85" ? 85 :
        str == "84" ? 84 :
        str == "83" ? 83 :
        str == "82" ? 82 :
        str == "81" ? 81 :
        str == "80" ? 80 :
        str == "79" ? 79 :
        str == "78" ? 78 :
        str == "77" ? 77 :
        str == "76" ? 76 :
        str == "75" ? 75 :
        str == "74" ? 74 :
        str == "73" ? 73 :
        str == "72" ? 72 :
        str == "71" ? 71 :
        str == "70" ? 70 :
        str == "69" ? 69 :
        str == "68" ? 68 :
        str == "67" ? 67 :
        str == "66" ? 66 :
        str == "65" ? 65 :
        str == "64" ? 64 :
        str == "63" ? 63 :
        str == "62" ? 62 :
        str == "61" ? 61 :
        str == "60" ? 60 :
        str == "59" ? 59 :
        str == "58" ? 58 :
        str == "57" ? 57 :
        str == "56" ? 56 :
        str == "55" ? 55 :
        str == "54" ? 54 :
        str == "53" ? 53 :
        str == "52" ? 52 :
        str == "51" ? 51 :
        str == "50" ? 50 :
        str == "49" ? 49 :
        str == "48" ? 48 :
        str == "47" ? 47 :
        str == "46" ? 46 :
        str == "45" ? 45 :
        str == "44" ? 44 :
        str == "43" ? 43 :
        str == "42" ? 42 :
        str == "41" ? 41 :
        str == "40" ? 40 :
        str == "39" ? 39 :
        str == "38" ? 38 :
        str == "37" ? 37 :
        str == "36" ? 36 :
        str == "35" ? 35 :
        str == "34" ? 34 :
        str == "33" ? 33 :
        str == "32" ? 32 :
        str == "31" ? 31 :
        str == "30" ? 30 :
        str == "29" ? 29 :
        str == "28" ? 28 :
        str == "27" ? 27 :
        str == "26" ? 26 :
        str == "25" ? 25 :
        str == "24" ? 24 :
        str == "23" ? 23 :
        str == "22" ? 22 :
        str == "21" ? 21 :
        str == "20" ? 20 :
        str == "19" ? 19 :
        str == "18" ? 18 :
        str == "17" ? 17 :
        str == "16" ? 16 :
        str == "15" ? 15 :
        str == "14" ? 14 :
        str == "13" ? 13 :
        str == "12" ? 12 :
        str == "11" ? 11 :
        str == "10" ? 10 :
        str == "09" ? 9 :
        str == "08" ? 8 :
        str == "07" ? 7 :
        str == "06" ? 6 :
        str == "05" ? 5 :
        str == "04" ? 4 :
        str == "03" ? 3 :
        str == "02" ? 2 :
        str == "01" ? 1 :
        str == "00" ? 0 : -1;
}

Comment: you can't use == like that with C/C++
Comment: if you use std::string, you can use == like that


mongodb aggregation project objectId with concat

Question: db.test.aggregate({
    $match : { "themType" : "SuperTest" , "mType" : { "$in" : [ 1 , 2]}}
},
{ $project : { "_id" : 1, "refTestId" : 1, "avatar" : { $concat : [$refTestId] }
  } });

and avatar returns me null, probably its because its objectId, is it possible in this query to make from this objectId string ?
Comment: I don't think we can help you fully if you can't fully state the problem. `$concat` is an operator for strings; it won't work with other types.
Comment: can you post a sample document from your collection?
Answer: From MongoDB 4.0 and newer, there is a $toString operator which returns the ObjectId value as a hexadecimal string:
db.test.aggregate([
    { "$match": {
        "themType": "SuperTest",
        "mType": { "$in" : [1 , 2] }
    } },
    { "$addFields": {
        "avatar": { "$toString": "$refTestId" }
    } }
])

or using $convert
db.test.aggregate([
    { "$match": {
        "themType": "SuperTest",
        "mType": { "$in" : [1 , 2] }
    } },
    { "$addFields": {
        "avatar": {
            "$convert": { "input": "$refTestId", "to": "string" }
        }
    } }
])

Answer: This isn't possible yet. WiP issue see: https://jira.mongodb.org/browse/SERVER-29512


Não consigo gerar o Histograma Animado pelo notebook do matplotlib

Question: Oi, não estou conseguindo obter o resultado final do notebook do matplotlib (estou tentando pelo colab), que seria um histograma animado:
https://matplotlib.org/3.5.0/gallery/animation/animated_histogram.html
Acontece o seguinte erro:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/init.py", line 196, in process
func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py", line 951, in _start
self._init_draw()
File "/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py", line 1743, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py", line 1766, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "", line 7, in animate
for count, rect in zip(n, bar_container.patches):
AttributeError: 'silent_list' object has no attribute 'patches'
Comment: Tu poderias fornecer maiores informações, para nos ajudar a tentar reproduzir o erro localmente e, então, poder te ajudar a encontrar a solução?
Answer: Há dois problemas em rodar o notebook que você mencionou no Google Colab:

A versão do matplotlib instalada no Colab.
O backend do matplotlib no Colab, que por padrão é inline.

A solução para esses problemas seria:

Atualizar a versão do matplotlib para >= 3.4: !pip install matplotlib>=3.4.0.
Utilizar o backend de widget: %matplotlib widget. Isso requer a instalação da biblioteca ipympl: !pip install ipympl.

Além disso, devido a uma particularidade do Google Colab, é necessário rodar as seguintes linhas de código para habilitar o ipympl:
from google.colab import output
output.enable_custom_widget_manager()

Feito isso, tudo deve funcionar. Segue o código completo do notebook: https://gist.github.com/giovaniceotto/6820dcc4181608dbbe25a85339deb7e9


Launch Intent from ListActivity entry - Android

Question: I have implemented a ListActivity, and would like to launch a different intent for each possible selection of the items in the list.
For example:

Clicking on About item should launch an intent with a new activity, and clicking on the Access Phone Gallery item should launch an intent opening the Phone Gallery (for selecting an image say).
I'd like to understand two things:

How can I determine which item was clicked, so that I can launch the correct intent?
How can I make the items in the list change style when they are selected? (say become highlighted so that the User Experience is better)

The following is the code of MyActivity (implementing the ListActivity):
public class MyActivity extends ListActivity implements OnItemClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

        ListView listView = (ListView) findViewById(android.R.id.list);

        listEntryClass listEntries[] = new listEntryClass[]
        {
            new listEntryClass( "About", "About" ),
            new listEntryClass( "Access Phone Gallery", "Access Gallery and Choose an Image" )
        };

        listEntryArrayAdapter adapter = new listEntryArrayAdapter(this, R.layout.list_entry_layout, listEntries);

        View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
        TextView headerValue = (TextView) header.findViewById(R.id.list_header);
        headerValue.setText( this.getString(R.string.headerPrefs) );

        listView.addHeaderView(header);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener( this );

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {

        Log.d(TAG, "Clicked an entry");
    }

}

I guess there should be something like a switch-case on the id of the clicked item at some place, but have no idea of how to do it.
Answer:  @Override
 public void onItemClick(AdapterView <? > parent, View view, int position, long id)
 {
     if (postion == 0) {
         startActivity(MyActivity.this, FirstActivity.class);
     } else if (possion == 1) {
         startActivity(MyActivity.this, SecondActivity.class);
     } else {
         startActivity(MyActivity.this, ThirdActivity.class);
     }
 }

Answer: How can I determine which item was clicked, so that I can launch the correct intent?
   @Override
    public void onItemClick(AdapterView <? > parent, View view, int position, long id) {

       // Here position denotes which row index you clicked.
      //If you clicked first item in ListView then row index will be zero
     // and so on...

       switch (position) {
       case 0:
           //First Item/row clicked
           break;
       case 1:
           //Second Item/row clicked
           break;
       }
   }

How can I make the items in the list change style when they are selected? (say become highlighted so that the User Experience is better)
As for setting the color, you need a StateListDrawable . You can set this on your list using the android:listSelector attribute, defining the drawable in XML like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/item_disabled" />
  <item android:state_pressed="true"
        android:drawable="@drawable/item_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/item_focused" />
</selector>

ListView xml will look like below.

<ListView android:id="@+id/list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:dividerHeight="0dip"
      android:divider="@null"
      android:listSelector="@drawable/list_selector"
      android:layout_gravity="center" />

Comment: so I should put a `switch-case` on the position value?
Comment: and the selector should go in the layout file containing the listview?
Comment: just two final remarks if you don't mind ;D: 1) If I want to access the content of the item that was selected (say the Text it contains since it is a TextView) I should use the `view` argument?and how? 2)in my ListView I was forced to use the attribute `android:id="@android:id/list"` and was not able to define my own id, why?will it change something?
Comment: If your class extends ListActivity then you have to use android:id="@android:id/list".If your class extend Activity then you can use whatever id you want like android:id="@+id/myList". and yes you will need view argument.
Comment: THks a lot again, please just help me with this last thing before accepting the answer: are there any default android drawable resources for highlighting the list entry when pressed (with buttons they become orange automatically...)? cause I don't know how to create these: `@drawable/item_pressed"` `@drawable/item_focused"` `@drawable/item_disabled"`
Comment: If you have the chance can you please help me on the question of the last comment?thks anyway ;D


R Creating binary variables for each factor of a categorical variable - For assessing the importance and information gain from each predictor

Question: I have a data frame that contains a categorical variable with 6 levels.
x$response <- data.frame(rep(1:6,10))
x

I would like to create 6 different binary columns based on the level. For Eg:
x$response1 <- 0
    x$response1[x[,1]=='1'] <- 1

x$response2 <- 0
    x$response2[x[,1]=='2'] <- 1

I can do it the above mentioned way, but need to repeat it 6 times. I would like to know if there is better way to do it. And what can i do, Say if i have 30 levels?
My ultimate goal is to assess the importance or information gain from each predictor. I am doing this to create a logit model for each response category and perform anova using different combinations of predictors.But, how can I assess information gain for each predictor and response category combination.
Comment: FYI, it is unnecessary to create dummy variables for lm in R, in case that's what you're going for.
Comment: @JohnMadden only if you cast `x` to factor in either the `data.frame` or `formula` objects.
Answer: x <- data.frame(response = factor(rep(1:6,10)))
model.matrix(~., x)

For bonus points:
Matrix::sparse.model.matrix(~., x)

As noted by John Madden in the comments, R does the former automatically inside most modeling functions, e.g. lm.  Note also that this is not the full matrix.  R automatically drops the first dummy variable to avoid the dummy variable trap.  You can turn this behavior off as follows (note that in this case the intercept term is dropped):
model.matrix( ~ 0 + ., x)
Matrix::sparse.model.matrix( ~ 0 + . , x)

For larger datasets, sparse.model.matrix is probably your best bet:
#100 factor levels
library(microbenchmark)
library(Matrix)
x <- rep(1:100,1000)
microbenchmark(
  mm = model.matrix(~0+factor(x)),
  smm = sparse.model.matrix(~0+factor(x)),
  o = outer(x, unique(x), `==`),
  m = sparseMatrix(1:length(x), match(x,unique(x))),
  times=25
)

sparseMatrix(1:length(x), match(x,unique(x))) is even faster, if that's what you need.
#10 levels
library(microbenchmark)
library(Matrix)
x <- rep(1:10,10000)
microbenchmark(
  mm = model.matrix(~0+factor(x)),
  smm = sparse.model.matrix(~0+factor(x)),
  o = outer(x, unique(x), `==`),
  sm = sparseMatrix(1:length(x), match(x,unique(x))),
  times=25
)

Answer: A more efficient version of Zach's approach
outer(x, sort(unique(x)), `==`)

Comment: Yields "Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing decreasing)) : undefined columns selected" for me
Comment: @Zach Change your code to `x <- rep(1:6, 10)` instead, or properly reference the data.frame variable with `x$response` instead.
Comment: I think `Matrix::sparse.model.matrix` is more efficient on larger datasets.
Comment: @Zach You have to include creating the factor as part of the benchmark. Not only does it call a sort unique, but it recodes a as a factor variable. Not necessary with my method. Factor sucks in R. Sparse matrix depends moreover on the number of factor variables. For fewer levels (most common data analysis case) the matrix is not that sparse.
Comment: Good point. I updated my timings.It looks like sparse model matrix wins if you have ~100 factor levels, but for 10, your function wins.  I came up with an even more optimized version too.


Estimating Poisson distribution parameter

Question: Assume events occur as a Poission process with parameter $\lambda$ in a fixed size interval.
Assume $Y$ is a random variable, $Y = \frac{\text{count of  intervals with 0 events}}{\text{total count of intervals}}$
Therefore we know that
$E(Y) = P(0 \text{ events occur})$
or $E(Y) = e^{-\lambda}$.
Now I observe a value of $Y$ from a single data point as $Y=y$. Can I estimate $\lambda$ as a function of $y$?
Comment: If you *know* P(0 events) (rather than observe a proportion of cases with 0 events) this is not an estimation problem at all -- you have a known strictly  monotonic function of the parameter so when you solve that to get $\lambda$, you know $\lambda$ exactly. It makes no sense to talk about this as estimation unless you have information from a sample rather than knowing the probability.
Comment: I don't see any estimator here: by definition, an estimator is a function of *data*, but you haven't exhibited any data at all.  What then could you possibly mean by "unbiased" and "estimator"??
Comment: I know J, and I would like to know if this is valid way to estimate lambda. If yes, could we comment on the quality of this estimation in anyway? Sorry if this is currently ambiguous, it has been a long time since I studied probability concepts.
Comment: @Glen_b I have improved the formulation of my question. You are right, I have an observation of proportion of cases with 0 events. How should I go about it?
Answer: Note that -- conditional on the total number of fixed-size intervals ($n$) -- the number of them with counts of 0 will be $\textit{binomial}(n,e^{-\lambda})$.
In that case, this looks like  a straightforward inference problem for which a simple maximum likelihood (ML) estimator for $e^{-\lambda}$ exists, and hence one for $\lambda$ is available (though you need the number of counts of 0 - let's call that $N_0$ - to be non-zero for it to make any sense, and even conditioning on $N_0>0$, it will be biased).
You might consider if you can find ways to modify it (/regularize it) to make a more suitable estimator. For example, it might help to get started along those lines if you consider ways (other than ML) to estimate the binomial parameter when the sample proportion is 0 (the ML estimate is 0, but given that the parameter is in (0,1) that's not much use).


creating unscrollble background image using html and css only

Question: i am trying to make a web page which background is fixed (meaning width is 100% and height does not scroll ) but the main div of the page which contain all the content of the page is scroll-able in y direction. you can see the effect here   http://btemplates.com/2016/blogger-template-topgames/demo/. is it possible to achieve this effect using html and css only ? if not then how it can be done with javascript?
Comment: Please post relevant code snippets.
Comment: Can you edit your question and supply the code please?
Answer: Yes, you can easily do that using a background image. If you inspect the page you can see how they did it.
CSS:
body {
   background: url('<<Your URL Here>>'), center top no-repeat fixed;
}

content-wrapper {
   width: 960px;
   margin: 46px auto 0px;
   padding: 0px;
}

HTML:

   <content-wrapper>
   </content-wrapper>

In the future, right-click the page and inspect the HTML and CSS and you should be able to figure most things out.


How to estimate network data transfer volume for a mysql database

Question: When planning to port my application (Java+Tomcat+MySql) to a cloud hosting, I've found that one important factor to consider is the volume of data transferred to/from the database, as it is limited by cloud providers. I haven't found a convenient way to estimate this volume based on usage statistics (e.g. number of queries and average row size), neither how to measure current utilization.
Is there any statistics build into MySql I could use or it is better to monitor this from the network perspective, monitoring the data transferred to/from the port used by MySql's.
Thanks in advance
Answer: If you want to directly measure the network traffic in and out of MySQL in your application, there are two status variables you can monitor:

Bytes_received : The number of bytes received from all clients
Bytes_sent : The number of bytes sent to all clients

GLOBAL LEVEL
You could fetch these for all sessions (past and present) with this query
SELECT * FROM information_schema.global_status
WHERE variable_name IN ('Bytes_received','Bytes_sent');

This will give you the number of each for the lifetime of the mysql instance.
SESSION LEVEL
You could fetch these for each session with
SELECT * FROM information_schema.session_status
WHERE variable_name IN ('Bytes_received','Bytes_sent');

This will give you the number of each for the lifetime of the DB session.
You could use GUIs to graph them over time. Since you mentioned Java and Tomcat, you may wish to create your own presentation. You would have to keep the previous values, get the current values, subtract them, and then plot or display averages as you wish.
Give it a Try !!!
CAVEAT
I discussed this in the DBA StackExchange a long time ago (MySQL : Does 'bytes_sent' and 'bytes_received' include mysqldump data?) Anything that has a thread will be counted in these two variables, including internals like that for MySQL Replication (via the IO Thread and SQL thread).
In light of this, make sure you do not have unnecessary DB Connections doing reads and that you do not monitor MySQL during a mysqldump or a reload of a mysqldump. Even monitoring software (such as Nagios, Zabbix, MONYog, EM7, etc) will generate Bytes_received traffic each time it runs SHOW GLOBAL VARIABLES;
SUGGESTION
Since everything connected to mysqld will affect the global Bytes_received and Bytes_sent status values, you may want to have your application collect the session-level Bytes_received and Bytes_sent just before running mysqli_close(). Then, you can generate a report of those numbers. You could then subtract those totals from the global counts to see how much housekeeping data is being requested.
Comment: Thanks @RolandoMySQLDB. It looks like having replication will make this stats pretty much useless in my case.
Comment: That is true for global status. You may want to think about having all user sessions collect its individual byte traffic into a common table and tally them on demand.
Comment: I am going to add my previous comment to my answer.
Answer: The easiest way would be to fire up the MySQL Workbench.  Though it can take some small effort to properly set up the server administration features, it presents graphs for network utilization and is an all-around excellent tool for managing MySQL instances.  A web-based solution to MySQL administration is phpMyAdmin, which also provides MySQL statistics.  However, be advised that phpMyAdmin has had several major security flaws in the past (allowing total takeover of a system, for example), so I strongly recommend that you use the MySQL Workbench instead.  (Tangentially: if you really want or need to use phpMyAdmin, make sure you have an authentication scheme in front of phpMyAdmin, so for malevolents to even see your phpMyAdmin login they must get through your first login layer.  My servers, for instance, require Basic authentication over HTTPS and check the provided username and password against PAM before allowing access to even the phpMyAdmin login page.)
If you're also interested in general statistics collection with lots of pretty graphs, Munin is a good solution, and it also includes a collection of MySQL monitoring plugins, though they must first be configured and there are some Perl dependencies to be installed to make the MySQL monitoring plugins work (Cache::Cache and DBD::MySQL, according to the comments left in the plugin scripts).
Finally, remember that being billed for MySQL traffic is only an issue if your application and database reside on different servers; localhost traffic not metered (or billed, therefore).


Polymer - Disable gray background on click

Question: I am learning Polymer. The app I'm building to learn Polymer uses the paper-item element. I've chosen the paper-item for its look. However, when I click the paper-item, the background changes to a dark gray. How do I remove this behavior? I want the background of the paper-item to always be white. Yet, cannot figure out how to set the background color when the paper-item is clicked or selected.
Thank you for your help and have a great holiday season!
Answer: I think it's the focused behavior that needs to be changed by redefining the mixin:
<template>

  <paper-item></paper-item>
</template>

If this doesn't work try --paper-item-selected instead of --paper-item-focused.
Answer: I think you looking for this :
paper-item:focus::before,
paper-item:focus::after {
    color: white;
    opacity: 0;
}

This rule override rules from "bower_components/paper-item/paper-item-shared-styles.html" :
:host(:focus):before, .paper-item:focus:before {
    @apply(--layout-fit);

    background: currentColor;
    content: '';
    opacity: var(--dark-divider-opacity);
    pointer-events: none;

    @apply(--paper-item-focused-before);
}

I don't know where is import the rule on "paper-item:focus::after" but in my Chrome Developper Console I see this and I need to override it to get the behavior you want.
Regards


In Julia is big"123" a macro, function, or something else?

Question: As a newcomer to Julia this month, Sept. 2018, I am just getting used to the initially unfamiliar "@" symbol for macros and "!" symbol for functions with mutable inputs.  Am I right to assume that these are merely stylistic symbols for humans to read, and that they do not really provide any information to the compiler?
I bring this up in the context of the following code that does not seem to match the style of a macro, a function, or anything else in Julia I am aware of.  I am specifically asking about big"1234" below:
julia> big"1234"  # big seems to be neither a macro or a function.
1234

julia> typeof(big"1234")
BigInt

julia> typeof(BigInt(1234))
BigInt

My question is: What is big in big"1234"?
Edit: I think I got my answer based on a comment at https://discourse.julialang.org/t/bigfloat-promotion-rules-and-constants-in-functions/14573/4
"Note that because decimal literals are converted to floating point numbers when parsed, BigFloat(2.1) may not yield what you expect. You may instead prefer to initialize constants from strings via parse, or using the big string literal.
julia> BigFloat(2.1)
2.100000000000000088817841970012523233890533447265625

julia> big"2.1"
2.099999999999999999999999999999999999999999999999999999999999999999999999999986"

Thus, based on the above comment, big in big"1234" is a "big string literal."
Edit 2:  The above is a start at the answer, but the accepted answer below is much more complete.
Comment: also, '@' is not a stylistic choice, afaik. The @ is spotted by the interpreter / compiler and that object is interpreted as a macro.  Also, I wouldn't call '!' a stylistic choice either, it's a strongly encouraged naming convention, since there is no other way to convey the important information of whether a function mutates its argument or not.
Comment: @TasosPapastylianou, Thanks for the clarifications.
Comment: https://docs.julialang.org/en/v1/manual/metaprogramming/#Non-Standard-String-Literals-1
Answer: These are Non-Standard String Literals. They tell the compiler that xyz"somestring" should be parsed via a macro function named @xyz_str.
The difference between BigFloat(2.1) and big"2.1" is that the former does convert the standard Float64 representation of the "numeric" literal 2.1 to  BigFloat but the latter parses the string "2.1" directly (without interpreting it as a numeric literal)  with the macro @big_str to compute the BigFloat representation.
You can also define your Non-Standard String Literals. LaTeXStrings.jl for example uses it to make it easier to type LaTeX equations.
Please take a look at: https://docs.julialang.org/en/v1/manual/metaprogramming/#Non-Standard-String-Literals-1
Comment: I am liking Julia better all the time.  Thanks for the great answer!


Multilanguage site in the same directory. Help .htacces

Question: In the document root I have the files:
<code>en.php
es.php
pt.php
...
</code>
How can I load the PHP file according to the language of the visitor without affecting the query URL? And that the URL is kept mysite.com/url-required
Comment: How is PHP supposed to know which language the visitor needs if you don't want to change the request url?
Comment: The keyword you want to be looking into is _content negotiation_.
Answer: The PHP can't really tell the language of the guest. It can use the IP address of the visitors from the requests and load the language files accordingly, but keep in mind that a lot of people are moving to different countries or travel temporary to different countries, thus they might wont be able to understand anything from the website. The same happens if someone is using tor browser or masking their IP address.
A bit more accurate method is to use js to determine the web browser's language or the system language, however these are also more like guess works, because it's also possible that someone has to use a PC on different language.
So the best if you provide a way for the user to be able to switch the language, and then store this information in a cookie on their PC, so it wont switch back as they look around on the website.


Confidence interval for measure that lies within [0, 1]

Question: I have some metrics that all lie within [0,1], and I have multiple measurements of each. For example, one measure is accuracy (for a machine learning application). Accuracy will always lie within [0,1], and given multiple rounds of N-fold cross validation, there will be multiple measurements of accuracy. So let's say I have 50 values of accuracy, how can I form a confidence interval around accuracy, such that the confidence interval does not extend above 1 or below 0? More generally, how can I compute a confidence interval around an average of multiple measurements of a variable that always lies in [0,1], but does not necessarily adhere to a uniform distribution (e.g., a [0,1] truncated normal distribution)?
Comment: Accuracy is just a binomial variable of getting the right answer or not, so [binomial methods](https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval) apply. However, you seem to be interested in assessing machine learning performance. [Accuracy, as well as other threshold-based metrics like sensitivity, specificity, and $F_1$ score, is surprisingly problematic.](https://stats.stackexchange.com/questions/312780/why-is-accuracy-not-the-best-measure-for-assessing-classification-models/312787#312787)
Comment: Thanks! The issue I'm having with using binomial methods is that I don't have the number of experiments/trials. I am given N measurements of accuracy (or pick other metric in [0,1]) and I would like to know the confidence interval of the average of these N measurements. Maybe I'm missing something from  the Wiki link?
Comment: $N$ measurements from something like cross validation?
Comment: Technically from N runs of (10-fold) cross validation.
Comment: If you don't have the number of measurements, does that mean that all you have is the average accuracy? Then you are in a bit of a quandary, because of course the CI from a single accuracy will be a lot wider than the CI of an average of 100 accuracies.
Comment: No, I don't have the number of measurements that produce each accuracy value. That is, for a given run of 10-fold CV, I have an accuracy measurement. So for N runs of 10-fold CV, I have N accuracy values.
Answer: More of a comment than an answer, but I need the space provided by Answer format.
If you have 50 values from an unknown distribution with support $[0,1],$
then you might consider a bootstrap confidence interval for the population mean.
<code># Fictitious data
set.seed(714)
x = rbeta(50, 4, 10)   # Pop. mean 4/14 = 0.2857
summary(x)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 0.1026  0.2131  0.2925  0.2948  0.3440  0.5638

# Quantile bootstrap
set.seed(2021)
a.re = replicate(3000, mean(sample(x, 50, rep=T)))
CI = quantile(a.re, c(.025,.975));  CI

     2.5%     97.5%
0.2636896 0.3252635   # contains pop. mean (this time & usually)

hist(a.re, prob=T, col="skyblue2", main="Bootrap Dist'n: Resampled Means")
 abline(v=CI, col="orange", lwd=2, lty="dotted")
</code>

There are many styles of 95% nonparametric bootstrap CIs, just be sure to choose
one that is constrained to give endpoints in $[0,1].$
Comment: This is a great suggestion. Thank you!


Pointer arithmetic with shared memory

Question: I don't understand what exactly happens in the following lines:

unsigned char *membershipChanged = (unsigned char *)sharedMemory;

float *clusters = (float *)(sharedMemory + blockDim.x);

I assume that in #1 sharedMemory is effectively renamed into membershipChanged, but why would you add the blockDim to the sharedMemorypointer. Where does this address point?
sharedMemory was created with  extern __shared__ char sharedMemory[];

The code I found in a CUDA kmeans implementation.
void find_nearest_cluster(int numCoords,
                          int numObjs,
                          int numClusters,
                          float *objects,           //  [numCoords][numObjs]
                          float *deviceClusters,    //  [numCoords][numClusters]
                          int *membership,          //  [numObjs]
                          int *intermediates)
{
extern __shared__ char sharedMemory[];

//  The type chosen for membershipChanged must be large enough to support
//  reductions! There are blockDim.x elements, one for each thread in the
//  block.
unsigned char *membershipChanged = (unsigned char *)sharedMemory;
float *clusters = (float *)(sharedMemory + blockDim.x);

membershipChanged[threadIdx.x] = 0;

//  BEWARE: We can overrun our shared memory here if there are too many
//  clusters or too many coordinates!
for (int i = threadIdx.x; i < numClusters; i += blockDim.x) {
    for (int j = 0; j < numCoords; j++) {
        clusters[numClusters * j + i] = deviceClusters[numClusters * j + i];
    }
}
.....

Answer: sharedMemory + blockDim.x points blockDim.x bytes away from the base of the shared memory region.
The reason you might do something like this is to suballocate in shared memory.  The launch site of the kernel which includes find_nearest_cluster dynamically allocates some amount of shared storage for the kernel.  The code implies that two logically different arrays reside in the shared storage pointed to by sharedMemory -- membershipChanged, and clusters.  The pointer arithmetic is simply a means to get a pointer to the second array.
Comment: No.  Shared memory is dynamically allocated at kernel launch with the following syntax:  kernel<<>>(args...).  A pointer to the dynamically allocated smem lives at the extern shared variable.
Comment: and `extern __shared__ char sharedMemory[];` is enough to tell cuda to allocate all available shared memory?

Pointer arithmetic with shared memory


Different definition of continuity

Question: Condition: $f:I\to\mathbb R$ is continuous. For any countable sequence of pairwise disjoint sub-intervals $(x_k, y_k)$ of $I$, we have $\forall\epsilon\exists\delta$ such that $$ \sum_{k} |y_{k} - x_{k}| < \delta$$
implies
$$\sum_{k} |f(y_{k}) - f(x_{k})| < \epsilon.$$
Is this condition a necessary or sufficient condition of absolution continuity? Note that the order of the logic identifiers has changed.

A function $f: I \to \mathbb{R}$ is absolutely continuous on an interval $I$ if for every $\epsilon > 0$ there is a $\delta > 0$ such that whenever a finite sequence of pairwise disjoint sub-intervals $(x_k, y_k)$ of $I$ satisfies
  $$ \sum_{k} |y_{k} - x_{k}| < \delta$$
  then
  $$\sum_{k} |f(y_{k}) - f(x_{k})| < \epsilon$$
Answer: Yes, they are equivalent. Suppose you choose $\delta$ according to the usual definition of absolute continuity with $\epsilon$ repalced by $\epsilon /2$. If $(a_k.b_k)$ is  a disjoint sequence of interval with total length less than  $\delta$ then $\sum\limits_{k=1}^{N} |f(b_k)-f(a_k)| < \epsilon /2$ for each $N$. Let $N \to \infty$ to complete the proof.
Comment: Many thanks for your teaching! So you proved that the definition "$\forall\epsilon\exists\delta(\forall \text{finite subintervals we have} (\sum|y_k-x_k|<\delta \Rightarrow \sum|f(y_k)-f(x_k)|<\epsilon))$" is equivalent to "$\forall\epsilon\exists\delta(\forall \text{countable subintervals we have} (\sum|y_k-x_k|<\delta \Rightarrow \sum|f(y_k)-f(x_k)|<\epsilon))$". However, my first condition means "$\forall \text{countable subintervals}(\forall\epsilon\exists\delta \text{we have} (\sum|y_k-x_k|<\delta \Rightarrow \sum|f(y_k)-f(x_k)|<\epsilon))$". Not sure my understanding is correct, though.
Answer: Given any function $f:I\to\mathbb R$ (not necessarily continuous), the condition:  For any countable sequence of pairwise disjoint sub-intervals $(x_k, y_k)$ of $I$, we have $\forall\epsilon\exists\delta$ such that $$ \sum_{k} |y_{k} - x_{k}| < \delta$$
implies
$$\sum_{k} |f(y_{k}) - f(x_{k})| < \epsilon$$
is trivially true.
Proof: Given any countable sequence of pairwise disjoint sub-intervals $(x_k, y_k)$ of $I$, just choose $\delta = \frac{1}{2} \sum_{k} |y_{k} - x_{k}|$.  Then the condition $ \sum_{k} |y_{k} - x_{k}| < \delta$ will be false and so the implication "$ \sum_{k} |y_{k} - x_{k}| < \delta$
implies
$\sum_{k} |f(y_{k}) - f(x_{k})| < \epsilon$" will be trivially true.
So this conditionis not sufficient for absolute continuity or even continuity.
Comment: Yes, I also saw this after you gave the detailed explanation in the other question

Different definition of continuity


how to extract a large SAS file?

Question: I have a large census data set with 20044425 observations and 35 variables. I am trying to get it to the work folder using the mylib statement. the specific variable I am interested in is a numeric variable titled DISTRICT. However, it is taking an extremely long time to extract it from my folder in the computer to the work folder in SAS
I tried to extract cases with the specific DISTRICT I am interested in using the following code
libname pop "\\Client\C$\Users\lakna\Desktop\SA data";
data mydata;
set pop.Sapop (where=(DISTRICT=3));
run;

However, it is still taking an extremely long time. It took more that 15 minutes of real time, and I was still not able to extract it to the work folder from the library.
Is there a more effecient way to extract a large file?
Comment: What is the libname statement for `pop`.  Please show the output from `options nocenter;
ods listing;
proc contents data=sashelp.class varnum;
run;
dm 'output';`  Some of the 34 other columns may be very wide character columns that can cause write times longer than expected.  The pop table may be on a network folder, or a SAS/Share libref; both require transport across your network.
Answer: Being on a file server, the contents of the data set is being transferred (by the libname engine) to your SAS session machine before the where clause can be applied.
Some different ways to get faster cuts is

move the entire Sapop data set to your local machine
get IT to install SAS on the file serving machine so that the share \Client\C$ is a local path
Obtain a smaller cut from your census data provider
Answer: To make this slightly faster, you can copy the data set over and then process it. When you use a COPY procedure it does not read all the data it moves it over in blocks so it should be very straightforward.

Copy the data over using PROC COPY
Filter the data set from WORK
Add a KEEP = to keep only the variables you need which will reduce the size fo the file.
If you're regularly working with this file consider adding an index on the commonly used variables to filter and/or using a COMPRESS option to reduce the size.
libname pop "\\Client\C$\Users\lakna\Desktop\SA data";

proc copy in=pop out=work;
select SAPOP;
run;

data mydata;
set sapop (where=(DISTRICT=3) KEEP = DISTRICT <list of variables>);
run;

Comment: If you're only extracting a handful of short variables, and you have set the global option `compress = yes` it is sometimes optimal to set an explicit `compress = no` on the `data` statement. The overhead of the row-level compression that SAS uses can make narrow datasets balloon in size.
Comment: I would question whether the additional proc copy is particularly worthwhile here, unless the storage area used for the `work` library is significantly faster than the one for the `pop` library, and the dataset you're copying is subsequently being read multiple times.
Comment: @user667489 in general, things in WORK library process faster, mostly because it's local. COPY moves it over in blocks, so overall, in my experience, this does speed up the processing significantly. Will it work in all cases, no, but for what the OP said here, I think it will.

how to extract a large SAS file?


Primitive Pythagorean Triples & (Semi-)Prime Numbers

Question: There seems to be no shortage of people finding patterns related to prime numbers on this forum. Alas, I felt the need to share my stumbling upon such an instance.
Take a list of primitive Pythagorean triples, let their sides be denoted by the common a , b , and c .
I have noticed that the difference ( a-b with a>b) between a and b is very, very often a prime number, which holds for large values. And, if the number is not prime, then it is semi-prime (only factors are 1, itself, and two primes).
3/4/5= 1       .... 1428/1475/2053= 47 .... 693/1924/2045= 1231
20/21/29=1     .... 1204/1653/2045= 449 .... 1281/1640/2081= 349
5/12/13= 7
8/15/17= 7
7/24/25= 17
12/35/37= 23
9/40/41= 31
Of course, there are those that fail (are semi-prime; I don't believe I've found any counterexamples).
For example: 819/1900/2069= 1081 (23*47).
I find it intriguing that it outputs so many primes. I am not sure which fields of mathematics this relates to, but I would love to have the input of others. Is this only an interesting phenomenon or a gold nugget?
Pythagorean Triples up to 2100 + link up to 10000: http://www.tsm-resources.com/alists/trip.html
My conjecture: : Every number generated in this way is a prime or semi-prime number. And: note that this only includes primitive triples.
Comment: Are you saying that you have found no case in which $a-b$ is neither prime nor semi-prime?
Comment: I think you probably need more data.  When you say "I don't believe I've found any counterexamples," what statement would an example counter?  What input would you hope to get on this topic?
Comment: Note that $1$ is not considered a prime nowadays, so the precise formulation of your conjecture would be that there are never more than two factors in the prime factorization of the difference. (I take it that you're interested in a) proving or disproving this conjecture and b) understanding why so many of the differences are primes.)
Comment: abiessu - You're right, I do; I haven't been able to find exhaustive lists of only promitive Pythagorean triples, and I'm not familiar enough with software development to create something that would generate them correctly.
Comment: saulspatz - Yes, but my resources are limited. I was hoping to throw it out the world and let it be scrutinized, in case I missed anything.
Comment: joriki - Yes, precisely. And, unfortunately, within mathematics, there some things that you cannot know, but I will this to not be one of them, as it would indeed greatly please me to understand why.
Answer: \begin{align}
4635^2 + 4292^2 &= 6317^2 & 4635 - 4292 &= 343 = 7^3 \\
3445^2 + 708^2 &= 3517^2 & 3445 - 708 &= 2737 = 7\cdot 17 \cdot 23\\
31005^2 + 28268^2 &= 41957^2 & 31005 - 28268 &= 2737 = 7\cdot 17 \cdot 23
\end{align}
The difference $a-b$ of the legs in a primitive Pythagorean triple can have arbitrarily many  prime factors (distinct or not). But to have many prime factors the difference must be large, and hence the legs must be large too, so one first encounters many cases with few prime factors.
How to find triples such that the difference has many prime factors:
Recall that one can parametrise the primitive Pythagorean triples by
$$(r^2-s^2, 2rs, r^2+s^2)$$
with $r$ and $s$ coprime and of different parity. Thus the difference "odd leg $-$ even leg" is
$$(r^2 - s^2) - 2rs = (r - s)^2 - 2s^2\,.$$
If $r$ and $s$ are coprime, then $r-s$ and $s$ are coprime too, and a prime $p$ can divide the difference of the legs of a primitive Pythagorean triple if and only if $2$ is a square modulo $p$, that is the case if and only if $p \equiv \pm 1 \pmod{8}$. Every such prime can be written in the form $x^2 - 2y^2$ with $x,y$ (positive) integers, e.g. $7 = 3^2 - 2\cdot 1^2$, $17 = 5^2 - 2\cdot 2^2$, $23 = 5^2 - 2\cdot 1^2$.
We can factor such a representation as $p = (x + y\sqrt{2})\cdot (x - y\sqrt{2})$, and the set
$$\mathbb{Z}[\sqrt{2}] = \{ x + y\sqrt{2} : x,y \in \mathbb{Z}\}$$
is a ring, in particular the product of two such numbers is again of that form: $$(x+y\sqrt{2})(u + v\sqrt{2}) = (xu + 2yv) + (xv + yu)\sqrt{2}\,.$$
And $(x-y\sqrt{2})(u-v\sqrt{2}) = (xu + 2yv) - (xv + yu)\sqrt{2}$, so
$$(x^2 - 2y^2)(u^2 - 2v^2) = (xu + 2yv)^2 - 2(xv + yu)^2\,.$$
Thus if you have distinct primes $p_1, \dotsc, p_r$ with representations $p_{\rho} = x_{\rho}^2 - 2y_{\rho}^2$, then
$$x + y\sqrt{2} = \prod_{\rho = 1}^r (x_{\rho} + y_{\rho}\sqrt{2})$$
gives us a pair of positive integers with
$$x^2 - 2y^2 = \prod_{\rho = 1}^r (x_{\rho}^2 - 2y_{\rho}^2) = \prod_{\rho = 1}^r p_{\rho}\,.$$
Since the $p_{\rho}$ are by assumption all distinct, $x$ and $y$ are coprime - if not all $p_{\rho}$ are distinct, one must choose the signs of $x_{\rho}$ and $y_{\rho}$ corresponding to the same prime in a compatible way to have $\gcd(x,y) = 1$, but that can always be done - and $x$ is odd. Then $r = \lvert x \rvert +\lvert y\rvert$ and $s = \lvert y\rvert$ produces a primitive Pythagorean triple such that the difference between the legs is $p_1\cdot \dotsc \cdot p_r$.
As the first example at the top shows, it is not necessary that all the primes are distinct, we can find primitive Pythagorean triples for which the difference between the legs is divisble by a power of some prime, and we can have the exponent as large as we please. The second and third example have the smallest possible difference between the legs such that the difference is divisible be three distinct primes. The second corresponds to $(3+\sqrt{2})(5+\sqrt{2})(5-2\sqrt{2})$ and the third to $(3+\sqrt{2})(5+\sqrt{2})(5+2\sqrt{2})$.
Comment: Although this isn't the answer I was hoping for, thank you!
Answer: $1320^2+1575^2=2055^2$
$1575-1320=255=3 \cdot5 \cdot17$
I believe there will be more numbers with many factors as they grow in size.
Comment: OP asks about primitive Pythagorean triples, your counterexample has $\gcd(1320, 1575, 2055)=15$
Comment: True, I just missed that out, thanks. I will check other cases.
Answer: Among the Pythagorean triples where $|A-B|\le100$, not all prime numbers are represented among primitive's differences. For example, there are no primitives where $|A-B|=2, 3, 5,  11, 13, 19 ...$    Here those that can be found with brute force and I have found way to generate all primitives where $|A-B|=X$ for any prime difference contained in the following list.  Let me know if you would like to know how.
$$\\diff=7\\
(5,12,13)\quad
(15,8,17)\quad
(55,48,73)\quad
(65,72,97)\quad
(297,304,425)\quad
(403,396,565)\quad
(1755,1748,2477)\quad
(2325,2332,3293)\quad
$$
$$
\\diff=17\\
(7,24,25)\quad
(45,28,53)\quad
(105,88,137)\quad
(207,224,305)\quad
(555,572,797)\quad
(1265,1248,1777)\quad
(3293,3276,4645)\quad
(7315,7332,10357)\quad
$$
$$
\\diff=23\\
(33,56,65)\quad
(35,12,37)\quad
(133,156,205)\quad
(275,252,373)\quad
(855,832,1193)\quad
(1525,1548,2173)\quad
(4905,4928,6953)\quad
$$
$$
\\diff=31
(9,40,41)\quad
(91,60,109)\quad
(171,140,221)\quad
(429,460,629)\quad
(893,924,1285)\quad
(2607,2576,3665)\quad
(5311,5280,7489)\quad
$$
$$
\\diff=41\\
(39,80,89)\quad
(77,36,85)\quad
(319,360,481)\quad
(377,336,505)\quad
(2001,1960,2801)\quad
(2059,2100,2941)\quad
$$
$$
\\diff=47\\
(63,16,65)\quad
(85,132,157)\quad
(225,272,353)\quad
(663,616,905)\quad
(1475,1428,2053)\quad
(3705,3752,5273)\quad
$$
$$
\\diff=71\\
(13,84,85)\quad
(231,160,281)\quad
(351,280,449)\quad
(1113,1184,1625)\quad
(1809,1880,2609)\quad
(6731,6660,9469)\quad
$$
$$
\\diff=73\\
(95,168,193)\quad
(117,44,125)\quad
(455,528,697)\quad
(817,744,1105)\quad
(2905,2832,4057)\quad
(4515,4588,6437)\quad
(99,20,101)\quad
(161,240,289)\quad
(341,420,541)\quad
(1219,1140,1669)\quad
(2263,2184,3145)\quad
(6837,6916,9725)\quad
$$
$$
\\diff=89\\
(51,140,149)\quad
(209,120,241)\quad
(629,540,829)\quad
(931,1020,1381)\quad
(3367,3456,4825)\quad
(5733,5644,8045)\quad
$$
$$
\\diff=97\\
(15,112,113)\quad
(325,228,397)\quad
(465,368,593)\quad
(1575,1672,2297)\quad
(2387,2484,3445)\quad
$$


Theorems for convergence for alternating sequences?

Question: I was just wondering whether there are any theorems that relates to convergence of an alternating sequence. For example, $(a_n) = (1, -1/2, 1/3, -1/4, 1/5, -1/6, \ldots)$. Are there any theorems I can use to show convergence/divergence when terms of a sequence changes sign? I know that $(a_n) \rightarrow a \implies (|a_n|) \rightarrow |a|$ but the converse is not true.
Comment: If an alternating _sequence_ converges, then, necessarily, the limit is $=0$.
Comment: @user40333  is it possible you're asking if the *sum* of this sequence converges?
Comment: Drop the minus signs, and ask if that positive series converges to zero.
Comment: Cool, are there any sufficient conditions for convergence of an alternating sequence?
Answer: Let me summarise what was mentioned in the comments.
If $\{a_n\}_{n=1}^{\infty}$ is a convergent alternating sequence, then it must converge to $0$. Now note that if $a_1 > 0$, then $a_n = (-1)^{n+1}b_n$ where $\{b_n\}_{n=1}^{\infty}$ is a strictly positive sequence and $a_n \to 0$ if and only if $b_n \to 0$.
Note: in your post you say $a_n \to a \Rightarrow |a_n| \to |a|$, but the converse is not true. That is correct, however, if $a = 0$, the converse is true (it is the only case where the converse is true).
So, if you're trying to determine whether a sequence converges, and you know it is alternating, then you know what it must converge to, and once you know that, you can just work with the corresponding positive sequence.


bayesglm (arm) versus MCMCpack

Question: Both bayesglm() (in the arm R package) and various functions in the MCMCpack package are aimed at doing Bayesian estimation of generalized linear models, but I'm not sure they're actually computing the same thing. The MCMCpack functions use Markov chain Monte Carlo to obtain a (dependent) sample from the joint posterior for the model parameters. bayesglm(), on the other hand, produces. I'm not sure what.
It looks like bayesglm() produces a point estimate, which would make it MAP (maximum a posteriori) estimation rather than a full Bayesian estimation, but there's a sim() function that looks like it can be used to get posterior draws.
Can someone explain the difference in intended use for the two? Can bayesglm() + sim() produce true posterior draws, or is it some sort of approximation?
Comment: You could also use 'rjags' and write the model out yourself if you want complete control.
Comment: I don't know the answer but if it helps you can see source of those functions just by typing their names: > bayesglm > sim Or you can ask directly to maintainers :
http://cran.r-project.org/web/packages/arm/arm.pdf
http://cran.r-project.org/web/packages/MCMCpack/MCMCpack.pdf
Answer: To see the full source code you need to download the arm package source from CRAN (it's a tarball).  A quick look at the sim function makes me think that arm is an approximate Bayes method as it seems to assume multivariate normality of the maximum likelihood estimates.  In models with a very non-quadratic log likelihood, such as the binary logistic model, this may be unlikely to be accurate enough.  I'd like to get some comments from others about this.  I have used MCMCpack with success; it provides an exact Bayesian solution for many models, given enough posterior draws and convergence of MCMC.

bayesglm (arm) versus MCMCpack