Estender o findAll com filtros personalisados

Question: Estou tentando estender o findAll do meu repositório JPA para fazer filtros personalizados.
Fiz algumas pesquisas e encontrei algumas ideias de uso com Predicados e afins porém não identifiquei algo que funcionasse de uma forma pratica.
Gostaria de saber se alguém já passou por algo parecido ou tem alguma ideia de como fazer isto sem que eu precise criar varias formas de pesquisa dentro do meu Repository.
Comment: Você precisa que todos seus repositórios apresentem o comportamento de `findAll` alterado? Um grupo de repositórios apenas? Se sim, você precisa de um `factory bean` customizado para os repositórios. Se for apenas um (ou um grupo pequeno), `property expressions` não resolve seu problema? Além disto, seu problema é no uso de predicados ou na fabricação de beans?
Comment: preciso para alguns repositorios somente, um pouco grandes por sinal para buscas personalizadas. Você tem algum exempo de factory bean ? só para eu ter uma ideia se estamos falando a mesma lingua. @BrunoCésar desde ja agradeço
Comment: Esta [reposta](http://stackoverflow.com/questions/27950246/27955142#27955142) é um exemplo de repositório customizado e um factory bean que cria repositórios com ele como implementação. Não entendi exatamente a sua necessidade, o quão "personalizada" e complexa, por que no data jpa tem muitas formas de definir consultas. Se você incluir o que precisa como resultado, talvez fique mais simples de ajudar.
Comment: @BrunoCésar muito bom brother, coloca como resposta para que eu possa marcar como resolvido
Comment: @BrunoCésar O que preciso seria algo do tipo, utilizar filtros para varios campos utilizando o findall como base entendeu
Comment: Otto, não entendi este último comentário. Você precisa de algo além da resposta que foi linkada (e incluída a baixo) ou é apenas isto?
Comment: @BrunoCésar vou fazer os testes para ver se atende e estarei avisando na sequencia ... desde ja agredeço
Answer: Uma das formas de modificar o comportamento padrão de um método um repositórios com o Spring Data JPA é criando uma outra implementação base para repositório. Na documentação é mostrado como adiciona comportamente customizado ao repositório.
Os passos a serem seguidos são mais ou menos estes:

criar uma interface customizada para os seus repositórios, podendo extender o de JpaRepository mesmo, algo assim:

@NoRepositoryBean
public interface CustomGenericRepository<E extends CustomAbstractEntity, PK extends Serializable> extends
        JpaRepository<E, PK>, JpaSpecificationExecutor<E> { }

Iremos extender também JpaSpecificationExecutor, por que nosso repositório customizado irá usar specifications e ela dá suporte a isto.

o segundo passo é implementarmos esta interface que acabamos de definir. Abaixo um exemplo que altera o comportamento de findAll, usando uma Specification que irá considerar na consulta apenas os registros não excluídos logicamente, algo assim:

public class CustomGenericRepositoryImpl<E extends CustomAbstractEntity, PK extends Serializable> extends
        SimpleJpaRepository<E, PK> implements CustomGenericRepository<E, PK> {

    private final EntityManager entityManager;

    public CustomGenericRepositoryImpl(final Class<T> domainClass, final EntityManager entityManager) {
        super(domainClass, entityManager);
        this.entityManager = entityManager;
    }

    @Override
    public List<E> findAll() {
        return super.findAll(this.isRemoved());
    }

    private Specification<E> isRemoved() {
        return new Specification<E>() {

            @Override
            public Predicate toPredicate(final Root<E> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
                return cb.isFalse(root.<Boolean> get("deleted"));
            }

        };

    }

}

como temos uma implementação diferente que usaremos em nossos repositórios, precisamos criar uma forma de instruir ao container spring como ele irá criar as instâncias para os repositórios. Então, criaremos um factory bean também customizado e que também extende de um que é JPA, o JpaRepositoryFactoryBean:

public class CustomGenericRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable> extends
        JpaRepositoryFactoryBean<T, S, ID> {

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(final EntityManager entityManager) {
        return new RepositoryFactory(entityManager);
    }

    private static class RepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

        public RepositoryFactory(final EntityManager entityManager) {
            super(entityManager);
        }

        protected Object getTargetRepository(final RepositoryMetadata metadata) {
            return new CustomGenericRepositoryImpl<T, I>((Class<T>) metadata.getDomainClass(), em);
        }

        @Override
        protected Class<?> getRepositoryBaseClass(final RepositoryMetadata metadata) {
            return CustomGenericRepositoryImpl.class;
        }

    }

}

por fim, precisamos falar para o spring data qual é a factory-class que irar criar nossos repositórios. Usando XML você pode usar assim:

<jpa:repositories base-package="com.brunocesar.custom.repository,com.brunocesar.custom.repository.support"
    factory-class="com.brunocesar.custom.repository.support.factory.CustomGenericRepositoryFactoryBean"
    transaction-manager-ref="transactionManagerCustom" entity-manager-factory-ref="entityManagerFactoryCustom" />

Usando Java, considerando uma classe só para a configuração dos repositórios, basta configurar o atributo factoryClass de @EnableJpaRepositories, algo assim:
@EnableJpaRepositories(factoryClass = "com.brunocesar.custom.repository.support.factory.CustomGenericRepositoryFactoryBean")
public class JPARepositoryConfig {

    // o resto da configuração continua a mesma =)

}

Para usar, basta que o seu repositório extenda a interface customizada que criamos, CustomGenericRepository, algo assim:
@Repository
public interface PessoaRepository extends CustomGenericRepository<Pessoa, Long> {

}

Answer: Implementa o JPARepository que voce pode personalizar suas consultas de acordo com os atributos do seu objeto exemplo abaixo :
public interface Empregado extends JpaRepository<Empregado , Integer> {
   //Atributo Endereco dentro de Empregado ele já implementa pra vc tudo!
   List<Empregado> findByEndereco(Endereco endereco );
}


Why there are 6 Base Address Registers (BARs) in PCIe endpoint?

Question: After some reading about the PCIe, I came around the PCI compatible configuration headers and after understanding the header there is Base address Register(BAR) field. Where there are total 6 BARs in each PCIe endpoint. Why there are 6 BARs and not just 2 (1 in case 32 bit address and 2 in case 64 bit). I mean what is the detailed functionality of all the BARs?
Answer: Use of the BARs varies by device. They are used either for device control operations (this is the usual case) or for data transfer to/from the device. The reason there are six slots is to allow devices to provide different regions for different purposes. Also, since the interface supports 64-bit address spaces, providing one of those requires a 64-bit BAR (which requires two contiguous 32-bit slots) to configure the base address.
One example of the BAR usage can be seen in the datasheet for the Intel i350 Ethernet Controller. See section 9.4.11. There are 3 BARs in this device: one for memory-mapped device configuration (and access to a FLASH device), one for device configuration via I/O address space, and one for configuring MSI-X interrupt vectors.
Other devices may have one BAR or five (or none if the device is a bridge). If you're running linux, try sudo lspci -vv on your system to observe the variations among devices.


Question about lagrange's theorem (group theory)

Question: Lagrange's theorem tells us:
$$\frac{\left|G\right|}{\left|H\right|}=\left[G:H\right]$$
(in group theory)
But it is not clear to me what is  the number represents,does it represent the size of subgroup $H$?
Comment: What isn't clear in https://en.wikipedia.org/wiki/Lagrange%27s_theorem_(group_theory)?
Comment: Which number are you about?
Comment: "the ratio ${\displaystyle |G|/|H|}$, as being the index"
the index is the number of subgroups of the group G?
Comment: You're assuming $\lvert G\rvert$ is finite.
Comment: It is the *index*, also sometimes denoted by $(G:H)$, to distinguish it from the field degree $[L:K]$.
Answer: $|G|$ and $|H|$ are the orders of the groups $G$ and $H$, respectively, i.e. the number of elements in each. $[G:H]$ is the index of $H$ in $G$, the number of cosets of $H$ in $G$, i.e. the number of unique elements in the set $\{gH:g\in G\}$.
Comment: Thank you! now it's clear


Comparar a quantidade de linhas de um list em uma ViewBag

Question: Preciso saber a quantidade de linhas(na view) que envio do meu controller num tipo list<> para uma viewbag na view.
Com isso habilito ou não a opção de criar novas imagens.
Controller:
    ViewBag.Imagens =             _supplierAdvertImageRepository.GetSupplierAdvertImage(obj.SupplierAdvertID,
                obj.SupplierID);

View
@{
    if (ViewBag.Imagens > 5)
    {

            @Html.ActionLink(
                "Inserir Imagem ",
                "CreateAdvertImage",
                "SupplierAdvert",
                new {
                    supplierId = Model.SupplierID,
                    SupplierAdvertID = Model.SupplierAdvertID},
                    new {
                        supplierId = Model.SupplierID,
                        SupplierAdvertID = Model.SupplierAdvertID,
                        @class = "btn btn-info"
                    }
            )

    }
    else
    {
        Limite de Imagens Atingido
            *

    }

porém não esta correto, alguém tem uma ideia melhor?
Comment: Sua pergunta está sem lógica, poderia [reformular](https://pt.stackoverflow.com/questions/462431/comparar-a-quantidade-de-linhas-de-um-list-em-uma-viewbag)?
Comment: Veja se agora tem lógica?
Answer: Faz assim:
var lista = _supplierAdvertImageRepository
    .GetSupplierAdvertImage(obj.SupplierAdvertID, obj.SupplierID);
ViewBag.Imagens = lista;
ViewBag.Quantidade = lista.Count();

Na View
@if (int.TryParse(ViewBag.Quantidade.ToString(), out int c) && c > 5)
{
}

Comment: Virgílio, não tinha pensado nesta solução bem mais simples, mas resolveu obrigado!


I want to create  tags in single row using jquery or java script not using css

Question: I need a help on creating a item list from
car
bus
cycle
aeroplane
to a single line(row) instead of (column) like this

car
bus
cycle
aeroplane

i do not want break on each list tag.
My fiddle
Comment: Unfortunately HTML is not rendered by javascript, its rendered by the browser and styled using css so no matter how you write it CSS is the only way to tell the browser what you want.
Comment: Use CSS to give each of your `` tags `float:left`, and they would aligned on one line.
Comment: Set `display: inline;` for `li`s
Comment: @JustinHerter is right. The only minor thing I'd mention is you can do it with JS (sort of) by using the `$.css` jquery function, but at that point you're still using CSS only you're applying it to the individual item as opposed to using a more encompassing CSS rule
Answer: This isn't a JavaScript question, but a CSS question.  In order to display list items on a single line, you need to set the li elements display setting to inline-block or inline.

.inline-list li {
  display: inline-block;
  /* border added to outline the fact they are inline */
  border: 1px solid black;
}

Planes

Trains

Automobiles

Comment: i am creating list dynamically by using java script .its purely related to JavaScript. for further see my fiddle
Comment: Adding the elements dynamically: purely JS. Getting the elements in a single line (as your question and title are asking): *mostly* CSS (there are ways to do this with JS, but it makes more sense to do it with CSS)
Comment: i do not want to use display inline in tag
Comment: If you want LI tag items to be placed on a single row/line, you need some type of inline/inline-block display setting on the LI tags
Answer: this should be a good starting point:
#grocery-list ul li {
    display: inline;
}
#grocery-list ul li:before{
    content:'\25CF';
    margin-right:0.5em;
}

Here is your updated fiddle
Answer: Disc style:

li{
  width: 80px;
  float: left;
}

Car

Bus

Cycle

Plane


What type of neural network can handle variable input and output sizes?

Question: I'm trying to use the approach described in this paper https://arxiv.org/abs/1712.01815 to make the algorithm learn a new game.
There is only one problem that does not directly fit into this approach. The game I am trying to learn has no fixed board size. So currently the input tensor has dimensions m*n*11, where m and n are the dimensions of the game board and can vary each time the game is played. So first of all I need a neural network able to make use of such varying input sizes.
The size of the output is also a function of the board size, as it has a vector with entries for every possible move on the board, and so the output vector will be bigger if the board size increases.
I have read about recurrent and recursive neural networks but they all seem to relate to NLP, and I'm not sure on how to translate that to my problem.
Any ideas on NN architectures able to handle my case would be welcome.
Comment: for varying input size there are some good answers here: https://stats.stackexchange.com/questions/388859/is-it-possible-to-give-variable-sized-images-as-input-to-convolutioal-neural-net

and here: https://ai.stackexchange.com/questions/2008/how-can-neural-networks-deal-with-varying-input-sizes

for varying output size that still remains a mystery for me.
Comment: Highly theoretical, fully convolutional network. For example YOLOv3 is capable process different images size(in therms that network will not crash).
Answer: What you need is Pointer Networks (https://arxiv.org/abs/1506.03134)
Here is a introduction quote from a post about it:

Pointer networks are a new neural architecture that learns pointers to positions in an input sequence. This is new because existing techniques need to have a fixed number of target classes, which isn't generally applicable— consider the Travelling Salesman Problem, in which the number of classes is equal to the number of inputs. An additional example would be sorting a variably sized sequence.
  - https://finbarr.ca/pointer-networks/

Its an attention based model.
Essentially a pointer network is used to predict pointers back to the input, meaning your output layer isn't actually fixed, but variable.
A use case where I have used them is for translating raw text into SQL queries.

Input: "HOW MANY CARS WERE SOLD IN US IN 1983"
Output: SELECT COUNT(Car_id) FROM Car_table WHERE (Country='US' AND
Year=='1983')

The issue with raw text such as this is that it will only make sense w.r.t to a specific table (in this case car table with a set of variables around car sales, similar to your different boards for board games). Meaning, that if the question cant be the only input. So the input that actually goes into the pointer network is a combination of -
Input -

Query
Metadata of the table (column names)
Token vocabulary for all categorical columns
Keywords from SQL syntax (SELECT, WHERE etc..)

All of these are appended together.
The output layer then simply points back to specific indexes of the input. It points to Country and Year (from column names in metadata), it points to US and 1983 (from tokens in vocabulary of categorical columns), it points to SELECT, WHERE etc from the SQL syntax component of the input.
The sequence of these indexes in the appended index is then used as the output of your computation graph, and optimized using a training dataset that exists as WIKISQL dataset.
Your case is quite similar, you need to pass the inputs, metadata of the game, and the stuff you need as part of your output as an appended index. Then the pointer network simply makes selections from the input (points to them).
Answer: You need to go back to a fixed input / output problem.
A common way to fix this issue when applying to images / time series... is to use sliding windows to downsize. Perhaps this can be applied to your game.
Comment: I could train a network with say a 4 by 4 game board, and then make separate predictions for every 4 by 4 piece of the playing board. The problem then is how to combine the results, and how co compensate for losing information compared to when the entire board is taken into account, as far away parts will more often than not have impact on each other.
Comment: average, majority voting, custom rules... I don't know your game
Answer: Fully convolutional neural network is able to do that. Parameters of conv layers are convolutional kernels. Convolutional kernel not so much care about input size(yes there are certain limitations related to stride, padding input and kernel size).
Typical use case is some convlayers followed by the maxpooling and repeated again and again to some point where are filters flattened and connected to dense layer. Dense layer is problem because he expect input at fixed size. If there is another conv2 layer, your output will be another feature map of appropriate size.
Example of such network could be YOLOv3. If you feed it for example with image 416x416x3 output can be for example 13x13xnumber of filters(I know YOLOv3 has more output layers but I will discuss only one because of simplicity). If you feed YOLOv3 with image 256x256x3, output will be feature map 6x6xnumber of filters.
So network don't crash and produce results. Will be results good? I don't know, maybe yes, maybe no. I never use it at such manner I always resize image to recommended size or retrain network.
Comment: Did you test yourself? I think TF/keras/... rise error of mismatch tensor size (some thing like this). Did you mean padding?


Compiling C# at Runtime

Question: I want to implement C# as the scripting language in my game.
My problem is, that my script will not compile if I want to use classes defined in the game core (exe).
The script looks like this:
using System;
using ConsoleApplication1;

class Script
{
private static void Call()
{
    Console.WriteLine("called");
}

public static void Init()
{
    Console.WriteLine("Script");
    Call();
    GameObject myO; // THIS IS WHAT I WANT TO GET WORKED,
 //IF THIS IS COMMENTED OUT, IT COMPILES FINE, GAMEOBJECT
 // IS DEFINED IN THE "ConsoleApplication1" NAMESPACE.
}
}

The script is compiled like in the MDX sample:
    static void Main(string[] args)
    {
        CodeDomProvider provider = new CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();
        //cp.CompilerOptions = "/target:library";
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.IncludeDebugInformation = false;
        cp.ReferencedAssemblies.Add("ConsoleApplication2.exe");
        cp.ReferencedAssemblies.Add("System.dll");
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, "script.cs");
        if (!cr.Errors.HasErrors)
        {
            Console.WriteLine("Success");
            cr.CompiledAssembly.GetType("Script").GetMethod("Init").Invoke(null, null);
        }
        Console.ReadLine();
    }

Is there any way to call functions or create objects defined in the "ConsoleApplication1" namespace via the script?
Comment: Did I miss the part where you state what compile time error you encounter?
Comment: Oh I´m so dumb... checked compiler error[0] and now it works, thanks
Comment: Is 'ConsoleApplication1.GameObject' defined in 'ConsoleApplication2.exe'?
Answer: This is a daily programming problem.  It's not working and you think it should be working.  So break it down.  Instead of working on a big problem, work on a smaller problem.  Just tackle trying to compile the script outside of your program.  Once you get that working, then you can try to compile it as a script from inside your program, knowing that you've got the basic problem of references and compiler issues sorted out.
Something like:

csc /reference:ConsoleApplication1.exe script.cs

From the looks of it, it might be as simple as changing the reference from ConsoleApplication2.exe to ConsoleApplication1.exe.

Compiling C# at Runtime


Get valid ssl certificates with EC2 and OVH

Question: I have an EC2 instance that I will use for some backend services in my mobile app. In order to get valid SSL certificates for this instance, here is what I did:

Setup EC2 + python backend services
Got an OVH domain name
routed my EC2 elastic IP adress with this domain name (both with Route 53 of AWS and in OVH)

Now, I'm trying to get a valid SSL certificate with AWS Certificate Manager, but my request is in pending validation for three days... Did I do something wrong? Thanks a lot for your help.
Comment: Monday was a holiday in the USA and there was a weekend before that.
Answer:
Does your domain really use the Route53 name servers? That has to be configured in your registrar (OVH?)

Did you create the required ACM validation TXT record in your Route53 Hosted Zone?

You won't be able to use an ACM-issued certificate in EC2 anyway, ACM certs can only be used with Application Load Balancers (ALB), CloudFront, and some other services. They can't be exported for use on EC2.

If you're not planning to use ALB you may consider a LetsEncrypt certificate.
Hope that helps :)
Comment: @Abilys38 I don't know OVH, sorry. The easiest is to use *Application Load Balancer* in front of your EC2 and use ACM issued certificate. That's what I would do. Other than that LetsEncrypt certbot can be made to run on Amazon Linux 2, or you can spin up Ubuntu EC2 where certbot works out of the box.
Comment: Thanks for your answer. Thus, I need to rely on LetsEncrypt... The problem is I don't have a web hosting subscription in OVH, just a domain subscription. And setting up certificates from the shell is very tough in my instance, as I can't download Certbot :((
Comment: Conclusion: There is no easy way to get a official ssl certificate in EC2 ?


how do I add two numpy arrays correctly?

Question: np_mat = np.array([[1, 2], [3, 4], [5, 6]])
np_mat + np.array([10, 10])

I am confused what the difference between np.array([10, 10]) and np.array([[10, 10]]) is. In school I learnt that only matrices with the same dimensions can be added. When I use the shape method on np.array([10, 10]) it gives me (2,)...what does that mean? How is it possible to add np_mat and np.array([10, 10])? The dimensions don't look the same to me. What do I not understand?
Comment: Read up on [broadcasting](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html), which is used in numpy for arithmetic operations with different shaped arrays
Comment: np.array([10, 10]) is single dimenssional array and np.array([[10, 10]]) is an multidimensional array as @FlyingTeller tell read up broadcasting
Answer: You cannot add two arrays of different sizes.
But those two both have first dimension, length, equal to 2. (that is len(a) == len(b))
Shape (2,) means that the array is one-dimensional and the first dimension is of size 2.
np.array([[1, 2], [3, 4], [5, 6]]) has shape (3, 2) which means two-dimensional (3x2).
But you can add them since they are of different dimensions, and numpy coerces a number to an arbitrary array full of this same number.  This is called broadcasting in numpy.
I.e. your code gets equivalent results to:
np_mat = np.array([[1, 2], [3, 4], [5, 6]])
np_mat + 10

Or to:
np_mat = np.array([[1, 2], [3, 4], [5, 6]])
np_mat + np.array([[10, 10], [10, 10], [10, 10]])

Comment: See [here](https://numpy.org/devdocs/user/theory.broadcasting.html) for the theoretical explanation.
Comment: Many thanks @Arusekk for your comment. 1) What is the length of an array? I have always use the term length for sequences like strings or lists. But I have troubles imagining what the length of an array is. 2) So shape (2,) is the same as dimensions (2x1)? shape (2,1) does not exist? Sorry, I don't want to dig too deep into the theory. Maybe I have to rework the basics again, and then come back to your comment. Many thanks anyway!
Answer: It looks like numpy is bending the rules of mathematics here. Indeed, it sums the second matrix [10, 10] with each element of the first [[1, 2], [3, 4], [5, 6]].
This is called https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html. The shape of [10, 10] is (2, ) (that is, mathematically, 2) and that of [[1, 2], [3, 4], [5, 6]] is (3, 2) (that is, mathematically, 3 x 2). Therefore, from general broadcasting rules, you should get a result of shape (3, 2) (that is, mathematically, 3 x 2).

I am confused what the difference between np.array([10, 10]) and np.array([[10, 10]]) is.

The first is an array. The second is an array of arrays (in memory, it is in fact one single array, but this is not relevant here). You could think of the first as a column vector (a matrix of size 2 x 1) and the second as a line vector (a matrix of size 1 x 2). However, be warned that the distinction between line and column vectors is irrelevant in mathematics until you start interpreting vectors as matrices.
Comment: many thanks @aimery for your comment. I think I have to rework the theory again. I assumed arrays and matrices are synonyms. But obviously, it's not. I am familiar with matrices and the rules of matrix multiplication, and I have never heard of broadcasting before in the context of matrices. It seems, the arithmetic for numpy arrays works slightly differently. Many thanks for your contribution!


Reconstructing triangles given side lengths and centroid

Question: Suppose that the centroid of an unknown triangle $ABC$ is $(0,0)$.
Suppose also that 3 lengths $\alpha,\beta,\gamma$ are given, where $\alpha$ is the length of $BC$, $\beta$ is the length of $AC$, and $\gamma$ is the length of $AB$.
Question: How do I find all $ABC$ given this information?
I assume that $ABC$ is unique up to rotation and "flipping". (Sorry, I am not a geometer.) I am particular interested in the Cartesian coordinates of $A,B,C$. Reference would be most welcome. Thank you!
Comment: Without loss of generality, the side of the triangle *below* the centroid is parallel to the $x$-axis.  Since all $3$ sides are known, the [Law of Cosines](https://en.wikipedia.org/wiki/Law_of_cosines) gives you all $3$ angles.  Then, since the inner triangle formed by a median is known via *side-angle-side*, the length of the median is known.  This implies (again using the Law of Cosines) that the $6$ vertex angles formed by the medians are all known.  ...see next comment
Comment: Further, by the [Centroid of a Triangle](https://en.wikipedia.org/wiki/Centroid#Of_a_triangle) you know the distance from the centroid (i.e. $(0,0)$) to each vertex.  This implies that you know the (directional angle + length) of the centroid to each vertex.  This is **game over**.
Comment: I speculate that one reason that no formula has been *published* is because of the problem's ambiguity.  As you indicated, the triangle can be *rotated* as desired, without changing the location of the centroid or the lengths of the triangle's $3$ sides.  Further, even with my premise that the side *below* the centroid is parallel to the $x$-axis, that still begs the question: which of the $3$ sides will be placed *below* the centroid.
Comment: @user2661923 thanks for this. As stated, Ii am not a geometer. I am interested in the Cartesian coordinates of a suitable concrete triangle. Is this possible with your comments? If so, please make it a proper solution. Thanks.
Comment: On the one hand, my comments do serve as a road map that will permit you to derive the corresponding Cartesian coordinates.  On the other hand, you are absolutely right that my comments are nowhere close to a clear solution.  Unfortunately, there is a MathSE protocol that prohibits me from doing so, because your question is missing pertinent information.  See [this article](https://math.meta.stackexchange.com/questions/33190/how-to-avoid-downvotes-for-beginners-questions/33236#33236)
 for details.  ...see next comment
Comment: For what it's worth, I strenuously disagree with the protocol in this instance.  In fact, I consider it absurd that you should be required to provide any more information than you already have.  However, MathSE does not belong to me and there is a clear group of *strident* reviewers that I wish to avoid conflict with.  Any other MathSE reviewer is welcome to highjack my analysis and convert it into an *answer*.  Then, the other reviewer can deal with the opposition point of view.
Comment: @user2661923 Thanks. I was expecting this to be actually known and was hoping for a citable reference. Cheers.
Answer: The only issue I see is somewhat inadequate explanation of why you are interested and why you are stuck, but it seems far more likely to me to be someone with a real world problem they lack the mathematical understanding to handle, than a lazy student trying to get someone else to do their homework, so I'll give it a shot. If the drive-by downvoters want to get pissy about it, I don't care.
Since you are after cartesian coordinates, I'll take a more algebraic approach than user2661923. I'll also handle the ambiguity by demanding that $A$ lie on the positive $x$-axis, with $B$ above the axis and $C$ below. If we label the coordinates $A = (x_A, y_A)$, etc. The equation for the centroid is
$(A + B + C)/3 = (0,0)$ which gives
$$x_A + x_B + x_C = 0\\y_A + y_B + y_C = 0$$
But $y_A = 0$ by my condition on $A$, so $y_B + y_C = 0$, or $y_C = -y_B$. And from the $x$ equation, we have $x_C = -(x_A + x_B)$.
Now I get tired of subscripts, so let me relabel the points:
$$A = (a, 0),\quad B = (b, y),\quad C = (-(a+b), -y)$$
Thus we are left with three unknowns $a,b,y$ and the distance equations from them:
$$\begin{align}(b-(-(a+b))^2 + (y - (-y))^2 &= \alpha^2\\
(a-(-(a+b))^2 + y^2 &= \beta^2\\
(a - b)^2 + y^2 &= \gamma^2\end{align}$$
After simplifying,
$$\begin{align}a^2 + 4b^2 + 4ab + 4y^2 &= \alpha^2\\
4a^2 + b^2 + 4ab + y^2 &= \beta^2\\
a^2 + b^2 - 2ab + y^2 &= \gamma^2\end{align}$$
The equations can be multiplied through by constants and added together in various combinations to get rid of the $ab$ terms:
$$\begin{align}3a^2 + 6b^2 + 6y^2 &= \alpha^2 + 2\gamma^2\\
6a^2 + 3b^2 + 3y^2 &= \beta^2 + 2\gamma^2\end{align}$$
Subtracting the first of those two equations from twice the second gives
$$9a^2 = -\alpha^2 + 2\beta^2 + 2\gamma^2\\
a = \frac{\sqrt{-\alpha^2 + 2\beta^2 + 2\gamma^2}}3$$
since $A$ was chosen to be on the positive $x$-axis.
Since $a$ is now a known value, we can take a different course. If we subtract the last two distance equations, we get
$$3a^2 + 6ab = \beta^2 - \gamma^2\\b = \dfrac{\beta^2 - \gamma^2-3a^2}{6a}$$
And $$y = \sqrt{\gamma^2 -(a-b)^2}$$
Comment: Paul, wonderfully clear proof!


Getting the error "No active batch." when running any batch

Question: I am getting this error when running any batch on the site:

No active batch.

I thought this may have been a server issue, so I tried it locally, but I got the same error. I tried deleting all the records in the batch table, but I also got the same result.
The only thing I can think of is that perhaps the web server is down. Why would that effect batches that do not connect to their servers?
Comment: Do you have the secure pages module installed?
Comment: ha, yeah I am @Clive, I just found the problem and posted the answer. thanks for all of your help!
Answer: If you are using the Secure Pages module, you have apply the patches provided for that module.
As I am using the Secure Pages module, that fixed my problem.

Steps to install Secure Pages 7.x-dev

Set <code>$conf['https'] = TRUE;</code> in settings.php.
Apply these two patches:

#961508-21: Dual http/https session cookies interfere with drupal_valid_token()
#471970-13: DrupalWebTestCase->getAbsoluteUrl should use internal browser's base URL
Answer: I had the same issue. It was caused by the secure pages issue. So I went to secure pages settings page, and in the ignore list, I added my url (e.g. admin/structure/abc). It worked good. Hope it helps someone.
Answer: Update For Drupal 8, please, check this topic - https://www.drupal.org/project/drupal/issues/3055287#comment-13911732
removing logger.factory service from the batch fix the problem


Python: how to start an invisible python console from a python script under windows?

Question: I need to start a python console from a python script under windows.
Output of this console must be written on a textbox.
I'm using subprocess 'cause python console can be python 2 or 3, depends on choice of user, so I can't use code.InteractiveConsole.
Now, i start python with subprocess.Popen, stdin=PIPE, stdout and stderr are two file subclasses where write methods print on my textbox.
class output(file):
  def __init__(self):
    super(output,self).__init__("out","wb")
  def write(self,data):
    MyTextBox.Text = data
out=output()

All ok.
p=Popen("python -i",stdin=PIPE,stdout=out,stderr=err)
err is an other class, same as output, but an other file.
Ok, p is a subprocess, but stdout and stderr are None.
Sure, I wrong something, but I don't understand!
Maybe I must use 4 variables? out_write and out_read for stdout and err_write, err_read for stderr?
Help!
Comment: the code example is fairly unclear, could you please provide a [MCVE](http://stackoverflow.com/help/mcve)?
Comment: Sorry, I haven't posted all source, but I solved with answer. Now I'm able to grab output and error from my invisible python console. Thanks!
Answer: To hide the command prompt window, pass the appropriate flags:
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE
subprocess.call('whoami', startupinfo=si)


Asymptotic distribution of statistics with proportions

Question: I am taking a graduate course in mathematical statistics.
Here is what I feel must be a straightforward problem in application of standard tools in "asymptotics" (such as application of Slutsky's theorem, Cramer's theorem, and quadratic forms).
I am struggling to factorize the problem properly and end up running into cumbersome forbidding expression I am not able to develop. There must be a neat way to solve this problem.
I have little practice solving problems in statistics, any help would be very much welcome!

Suppose we have $n$ iid samples of a bivariate Bernoulli random variable $(x, y)$ where $x$ and $y$ take values 0 or 1 and are independent.
Define $\hat{p}_{ij}$ to be the proportion of the samples such that $(x= i, y = j)$. Also define $\hat{p}_{i*} := \hat{p}_{i0} + \hat{p}_{i1}$ (which is identified as the proportion of samples where $x = i$) and similarly $\hat{p}_{*j} := \hat{p}_{0j} + \hat{p}_{1j}$.
What is the asymptotic distribution of the following statistics:
$$
S(n) = \sum_{i=0}^{1} \sum_{j=0}^{1}\frac{n(\hat{p}_{ij} - \hat{p}_{i*}\hat{p}_{*j})^2}{\hat{p}_{i*}\hat{p}_{j*}}\,.
$$

Added later:
Following some comments, here is an elaboration on the definition of the quantities involved:
Let $n$ iid samples of the bivariate Bernoulli $\{(x, y)_k\}_{k=1}^n$, define $$N_{i,j} := \{\text{number of indices } k ~| ~(x= i, y = j)_k\}$$ with this definition at hand the proportion $\hat{p}_{ij}$ is defined to be
$$
\hat{p}_{ij} := \frac{N_{ij}}{n}\,.
$$
and the asymptotic of $S(n)$ is with respect to the limit $n \to \infty$.
Comment: What is $n$? I think the problem is in your notation. I suspect that you have a sample $X_i$ of bivariate Bernould random variables. Define all the quantities as a function of the $X_i$ and then try to derive asymptotics. Now given your formula it is difficult to see how your quantities depend on $n$ an the sum frankly does not make sense.
Comment: Thanks for pointing out, I have added explanation how I define proportion and where the $n$ comes from. I do believe, however, that the sum is well defined and makes sense.
Comment: Please add the `[self-study]` tag & read its
[wiki](http://stats.stackexchange.com/tags/self-study/info).
Answer: This will not be an answer, only the explanation of how I would approach this problem. Asymptotics is usually always about the sums, so it is better to express all the quantities in term of sums. The main sum has 4 summands, where each summand depends on $n$, so it is clear that we need to deal with each summand independently.
Assume that we have a sample $Z_i$, $i=1,...,n$ where $Z_i=(X_i,Y_i)$ and $X_i$ and $Y_i$ are independent Bernoulli random variables. Then we have
$$\hat{p}_{1*}=\frac{\sum_{i=1}^nX_i}{n},\;\; \hat{p}_{0*}=\frac{\sum_{i=1}^n(1-X_i)}{n}.$$
The definitions for $\hat{p}_{*1}$ and $\hat{p}_{*0}$ are respectively the same.
Now
$$\hat{p}_{11}=\frac{\sum_{i=1}^nX_iY_i}{n},\;\; \hat{p}_{00}=\frac{\sum_{i=1}^n (1-X_i)(1-Y_i)}{n},$$
and
$$\hat{p}_{01}=\frac{\sum_{i=1}^n(1-X_i)Y_i}{n},\;\; \hat{p}_{10}=\frac{\sum_{i=1}^n X_i(1-Y_i)}{n}.$$
By substituting these expressions into the initial sum and doing some algebra, the resulting expressions either will look as the selfnormalizing sums of independent random variables, which will give you asymptotic results immediately, or you will get some form of $\chi^2$ statistic for which again you can find some sort of asymptotic results. These are of course some sort of educated guesses.


Unix Shell Scripting to calculate

Question: I have following data ;
24692 -rw-rw-r--+ 1 da01 da01 25284427 Aug 31 09:06 collected_BOT.227031
24660 -rw-rw-r--+ 1 da01 da01 25248756 Aug 31 09:35 collected_BOT.227032
24748 -rw-rw-r--+ 1 da01 da01 25338868 Aug 31 10:03 collected_BOT.227033
24740 -rw-rw-r--+ 1 da01 da01 25331322 Aug 31 10:31 collected_BOT.227034

sample:
grep 1303 collected_BOT.227034 | more
1559254293,151840703,AJ1X,10178801756650692,VA,VB,0,0,2,2,1303,1,L1O,6797,129,1,3,601,0,GVW1,9110,551,17,000000,0001,000000,,6,4,,1,1,,0
1559254294,151840704,AJ2X,10178801756650693,VA,VB,0,0,2,2,1303,1,L2O,6797,203,1,3,601,0,GVW2,9110,552,17,000000,0001,000000,,6,4,,1,1,,0
1559254295,151840705,AJ3X,10178801756650694,VA,VB,0,0,2,2,1303,1,L3O,6797,664,1,3,601,0,GVW3,9110,552,17,000000,0001,000000,,6,4,,1,1,,0

$15 = duration
I just want to calculate the total amount $15 on file collected_BOT.227034 (only if $11=1303)
Answer: awk -F, '$11==1303{sum+=$15} END {print sum}' collected_BOT.227034

-F, field separator is ,
$11==1303 check if 11th field exactly matches the number 1303

If so, add the value of 15th field to sum variable (whose initial value is zero by default)

END {print sum} after processing all the lines of input file, print the value of sum variable

Edit:
Thanks @Mark Setchell for pointing out that $11==1303 can be used instead of $11 ~ /^1303$/
Also, use print sum + 0 if output is needed as '0' even when no lines match. Or an explicit BEGIN{sum=0} block
Comment: As `1303` is a number, I think you might be better off using `$11==1303` rather than pattern matching.
Comment: By the way, if you change `print sum` to `print sum + 0`, it will still work if the sum is zero, or there are no matching lines.
Comment: @MarkSetchell, I checked, it works even without `sum + 0`.. mine is `GNU Awk 4.1.3` does that make a difference?
Comment: I think the GNU version is more forgiving. I am using the older one shipped with Apple's OSX and it just prints an empty line at the end if no lines match.
Comment: @MarkSetchell, gotcha.. I was making the values sum to zero, if no lines match, the output is blank like you mentioned..
Answer: Great solution @sp asic.
No need to use regular expression for field $11 though:
awk -F, '$11=="1303" {sum+=$15} END {print sum}' collected_BOT.227034

(beware: use == and not =, because this last one will do nothing except do a (successful) assignment to field $11
Comment: Why the double quotes around a number?
Comment: Hi J. Chomel,

its work :) thank you very much.

explanations you provide easily understandable for me.
Comment: @GaidenSaga, if this worked, you should [accept](http://stackoverflow.com/help/accepted-answer) it as correct answer.

Unix Shell Scripting to calculate


Can not disable the default blocks available under block layout

Question: I installed Drupal 8.3 and I am facing the following issue: There is no disable region available under block layout, but the same is available under Drupal 8.2.6.
Is this an issue with Drupal core or with the theme I am using?
Comment: i think the issue is with the theme you are using. Change the theme to one if the default drupal theme to confirm.
Answer: Not an issue, please read release notes and under heading "Improved site building and administration:"
Removed the 'disabled' region from block administration.
Now there is an option to "Disable" block.


invertendo uma sequencia long em c

Question: Eu preciso inverter uma sequencia de numeros, por exemplo, se for inserido 1234, eu imprimo 4321. O código ta funcionando, mas pra entradas do tipo 0123 ou 1230 o zero simplesmente é "excluido", mas eu precisava que o zero também fosse apresentado na tela. Não consegui pensar ou achar uma solução pra isso, alguem pode me ajudar?
int main()
{
    long n;
    long inverso;

    scanf("%ld",&n);

    do
    {
        inverso=n%10;
        printf("%ld",inverso);
        n/=10;

    }while(n>0);
    printf("\n");
    return 0;
}

Comment: `n` precisa ser long? 0 a esquerda sempre será ignorado para long, isso é normal. 0123 é a mesma coisa que 123.
O mesmo acontece com 1230, inicialmente o 0 está lá, pois ele é 1.230, após inverter o mesmo acontece e ele passa a ser 321.
Comment: Para mim, com a entrada `1230` funcionou perfeitamente. Para `0123`, de fato, o 0 é ignorado pelo motivo explicado acima.
Comment: sim precisa ser long, essa é uma quetão do URI, numero 1984, pra quem conhece o URI...
Comment: Enunciado: https://www.urionlinejudge.com.br/judge/en/problems/view/1984
Comment: Se você pede como entrada um número não pode ter um 0 no início.
Answer: Utilize o especificador 0* para especificar o tamanho do campo a ser impresso com zeros à esquerda.
Por exemplo para um campo de 5 posições:
printf("%0*lld\n", 5, 123);

No seu caso você precisa determinar o tamanho do campo que pode ser feito, por exemplo, imprimindo o número original em uma string (sprintf) e utilizando a função strlen de .
Comment: Veja teste em: http://ideone.com/cZG26Q
Comment: Eu não entendi mto bem como vou aplicar isso no meu programa, mas pelo que eu entendi esse `printf` vai colocar um 0 nas saidas de toda e qualquer entrada,o que não deve acontecer.
Comment: Apenas se o número a ser impresso não alcançar o número de algarismos definido no primeiro parâmetro (no caso o 5).

invertendo uma sequencia long em c


How can I take into account the different sample sizes of the two groups that I am comparing with a t-test or analysis of variance?

Question: I am computing a p-value using the aov function in R in order to detect if the means of two groups are significantly different. However, these two groups that I am comparing have different sample sizes (for example the first one has 90 data points, while the second one only 15). I suppose that the p-value that is computed in this way is not really fair... is there a way to take into account this problem ? Is this under the name of the "effect size"?
I find on statistics websites that there is the so called "Cohen's d" or "Hedges' G" coefficients that could be calculated. If I understood well, if these coefficients are low, the effect due to this difference is low, otherwise it is high ?
I would like to check if I interpret in a good way what I found and, possibly, if there are other methods to take into account this problem (like coefficients that I can easily compute with R).
Thank you in advance.
Comment: The math in a function like that takes care of this.
Comment: "aov", "t.test" in R
Comment: Which function ? Do you mean aov ? @Dave
Comment: @Dave How ? Is there like an argument that I have to specify to compute some value which quantifies this effect or do you mean that it is included in the p-value result and I do not have to worry about this problem ?
Answer: This is easier to discuss for t tests (with only two samples) than
with one-factor ANOVAs (typically with more than two samples).
Whether you're using the pooled or the Welch t test, look at the formulas for the denominator of the t statistic. There you see that a difference between
sample sizes $n_1 \ne n_2$ is taken into account. [In the 'balanced case' where $n_1 = n_2$ the formulas for the pooled and Welch t statistics can be shown to be identical (even though the degrees of freedom may differ).]
Maybe a couple of simulations showing what happens to many tests for different scenarios will help convince you that the type one error probabilities are as promised (often fixed at 5%) and that type two error probabilities are reasonable. [With 100,000 iterations, error probabilities should
be accurate to about two places.]
Pooled t test. equal sample sizes, null hypothesis true:
Let $\mu_1 = \mu_2 = 50,$ $\sigma_1 = \sigma_2 = 2,$ $n_1 = n_2 = 10.$
set.seed(1234)
pv = replicate(10^5, t.test(rnorm(10,50,2),rnorm(10,50,2),
               var.eq=T)$p.value)
mean(pv <= .05)
[1] 0.04923     # aprx rejection rate 5%

Pooled t test. unequal sample sizes, null hypothesis true:
Now $n_1=8 \ne n_2 = 12.$
set.seed(1235)
pv = replicate(10^5, t.test(rnorm(8,50,2),rnorm(12,50,2),
               var.eq=T)$p.value)
mean(pv <= .05)
[1] 0.05117    # aprx rejection rate 5%

Pooled t test. equal sample sizes, null hypothesis false,
Now $\mu_1 = 52, \mu_2 = 49.$ Because the null hypothesis is false, we are now interested in the power of the pooled t test $(1$ minus probability of type two error).
 set.seed(1236)
 pv = replicate(10^5, t.test(rnorm(10,52,2),rnorm(10,49,2),
                var.eq=T)$p.value)
 mean(pv <= .05)
 [1] 0.887    # aprx power 89%

Pooled t test. unequal sample sizes, null hypothesis false,
Now $n_1 = 8 \ne \nu = 12.$ There are still twenty observations altogether, but the design is unbalanced. As expected, there is a slight decrease in power
because the unbalanced design is less efficient, but the
result is reasonable.
set.seed(1236)
pv = replicate(10^5, t.test(rnorm(8,52,2),rnorm(12,49,2),
                var.eq=T)$p.value)
mean(pv <= .05)
[1] 0.87566    # aprx power 88%

In all four scenarios, the pooled variance in the denominator of
the 'pooled t test' has made necessary changes in the t statistic.
These changes ensure good behavior of the pooled t test when $n_1 \ne n_2.$
Note: However, the pooled t test can behave badly when population variances are unequal---especially so for unbalanced designs.]
For example, if $H_0$ is true, and variances differ, the what
ought to be a test at level 5% can become a test at level 10% (or higher),
when the larger variance is matched with the smaller sample size.
set.seed(1237)
pv = replicate(10^5, t.test(rnorm(8,50,3),rnorm(12,50,1),
               var.eq=T)$p.value)
mean(pv <= .05)
[1] 0.10743     # nowhere near intended 5% Type I error

But a Welch 2-sample t test corrects for unequal variances and gives
satisfactory results.
set.seed(1238)  # WELCH
pv = replicate(10^5,  t.test(rnorm(8,50,3),rnorm(12,50,1))$p.value)
mean(pv <= .05)
[1] 0.0516   # nearly 5% signif level

Comment: Thank you @BruceET ! However, I did not get really much the meaning of the codes that you wrote , sorry :( so I do not know if you did it in the codes, but is it correct to say that the lower the p-value is, the higher is the difference between the two means of the two groups in t-test? Supposing that we have equal variances of the two groups and different size, since (as you explained very kindly) the different size of the groups is taken into account in the t.test , I would say yes...


Quadratic inequality with absolute values

Question: I've decided to study calculus on my own, and I've started working on "A First Course in Calculus" by Serge Lang, 5th edition. Now I'm just reading the chapter on preliminaries, and there is a section about inequalities. In the exercises on p.13, there is a problem which I can solve, but I'm not quite sure that I'm doing it right.
The problem is:
$|x^{2}-2|\leqslant 1$
I am solving it in the following way:
1) Suppose that $x^{2}-2 \geqslant 0$, then $|x^{2}-2|=x^{2}-2$
First, I find the values of x for which $x^{2}-2$ is non-negative: $x^{2}-2 \geqslant 0$, and obtain $x \geqslant \sqrt{2}$ or $x \leqslant -\sqrt{2}$
Second, I solve the inequality $x^{2}-2 \leqslant 1$ and obtain $-\sqrt{3} \leqslant x \leqslant \sqrt{3}$
The solution for non-negative values of $x^{2}-2$ is thus $-\sqrt{3}\leqslant x \leqslant -\sqrt{2}$ or $\sqrt{2} \leqslant x \leqslant \sqrt{3}$
2) Suppose that $x^{2}-2<0$, then $|x^{2}-2|=-(x^{2}-2)=2-x^{2}$
First, I find the values of x for which $x^{2}-2$ is negative: $x^{2}-2<0$, and obtain $-\sqrt{2} < x < \sqrt{2}$
Second, I solve the inequality $2-x^{2} \leqslant 1$ and obtain $x \leqslant -1$ or $x \geqslant 1$
The solution for negative values of $x^{2}-2<0$ is thus $-\sqrt{2} < x \leqslant -1$ or $1 \leqslant x < \sqrt{2}$
Combining the solutions for non-negative and negative values of $x^{2}-2<0$, I obtain the final answer:
$-\sqrt{3} \leqslant x \leqslant -1$ or $1 \leqslant x \leqslant \sqrt{3}$
This is the correct answer.
And here I go to the question itself: is it necessary to find the values of x for which $x^{2}-2<0$ and $x^{2}-2>0$, or can this step be skipped? If I just find the intersection of solutions of two inequalties, $x^{2}-2 \leqslant 1$ and $2-x^{2} \leqslant 1$, I still get the same answer. But I feel that skipping this step would be wrong, and that maybe it can lead to incorrect answers when I deal with more complex problems of this type.
I also have a second, though smaller, question. When we speak about the solution of an inequality, and we have several intervals, do we use the conjunction "or", or "and"? That is, is it
$-\sqrt{3} \leqslant x \leqslant -1$ or $1 \leqslant x \leqslant \sqrt{3}$
or
$-\sqrt{3} \leqslant x \leqslant -1$ and $1 \leqslant x \leqslant \sqrt{3}$?
Lang uses "or" in the "Answers" section, but it just seems to me that we speak about a set of solutions here, so maybe "and" would be more appropriate.
Answer: For the main question, the answer is yes. It is possible for the answer to change when you consider the condition (in general problems).
For the smaller question, the answer is OR. You can easily test this by trying out values.
Comment: So do I get it right that, methodologically, when I solve inequalities with absolute values, I always need to calculate the intervals of x for the non-negative and negative values of the expression?
Comment: If you use the definition of $|.|$ in the way you did above, yes. There are of course other ways of solving it, which may not require that step.  For e.g., we could work out the solution using $\iff (x^2-2)^2 \le 1 \iff (x-1)(x+1)(x-\sqrt3)(x+\sqrt3) \le 0$ which will give you the same solution by comparing intervals where the product can change signs. Which way you use depends on the problem and what you are comfortable with.
Comment: Thank you. But could you please explain why you use $(x^{2}-2)^{2}$ instead of $|x^{2}-2|$ in your answer?
Comment: Because another possible way of using absolute value is $|x| = \sqrt{x^2}$.  Hence $|x| \le 1 \iff x^2 \le = 1^2=1$.  In this problem, then you can get some nice factorisations going, of the form $a^2-b^2 = (a-b)(a+b)$.


Why does the exponential curve that shows up in the orchestra not overwhelm anything?

Question: So, I've noticed these ratios of range in the modern orchestra, which is primarily strings:

Soprano:Alto - Factor of 4 or 5 more soprano than alto
Tenor:Bass - About twice as much tenor as bass
Alto:Low range - About the same amount of Alto as Tenor + Bass

This clearly shows an exponential decay curve as the range gets lower, similar to this curve:

Thus the Violin:Viola:Cello:Double Bass ratio in an orchestra could easily be this:
60:15:8:6 (in fact, I often see ratios similar to this in say a recording of Beethoven's symphonies, but over time, every part of this ratio becomes bigger, yet the ratio in simplest form is almost the same as in Beethoven's symphonies)
Now in this question about a possible woodwind predecessor to an orchestra, Ceremonial flute choirs as a predecessor to the orchestra, how plausible is this? I mentioned in the comments and in the main body of the question these same ratios that I listed above and somebody mentioned that this exponential curve is not ideal, that it would lead to too little bass and too much soprano. Which is why I'm asking this question.
Why is the exponential curve a default for the orchestra if in an entirely woodwind ensemble, it would be unbalanced? What is it about the timbre of multiple string instruments that leads to this exponential curve? I know that the difference between 1 and 3 of the same string instruments is staggering(1 gives you a solo timbre that can be more nerve-wracking, almost painful at high pitches(in the case of the violin and viola), soulful(in the case of the cello and to some extent the high range of the double bass), or intensely deep(in the case of the double bass), 3 gives you a more orchestral-type sound)
But in small string ensembles, the ratio is almost 1:1 and is 1:1 in the case of the Trio and Sextet(that is if the Sextet does not include the Double Bass, which some do). Yet as it gets larger and approaches the 60:15:8:6 ratio of Beethoven symphonies, it goes from this 1:1 or almost 1:1 to an exponential curve.
Why is that? Why is it that the orchestra follows an exponential curve with pitch range and small ensembles have an almost 1:1 ratio for all the ranges?
Comment: The orchestra I see most often uses a linear pattern, eg 4 double basses, 6 cellos, 8 violas, 10 2nd violins, 12 1st violins.
Comment: Can you clarify: it appears that your words "range" and "decay" refer solely to the **number of instruments* in each group?    The final sound product is easily controlled by means of those cute things  called "pianissimo" and "fortissimo"
Comment: I am using them that way because 1) it makes sense to classify instruments by pitch range and 2) I'm asking why an exponential curve shows up, thus my mention of exponential decay.
Answer: It is mainly because increasing the number of instruments in a section does not actually make the section much louder, mostly it just makes it sound "denser". Any doubling of the number of unisono instruments effectively increases their volume by 3 dB. Note that dB is a logarithmic scale, corresponding to the ears' response. In other words, to effect a linear increase in perceived loudness, you need to increase the number of instruments exponentially.
Now, why would you want that in the first place? –I don't think it's only about loudness; the sound silkiness itself that you only get with upwards of a dozen violins is just important part of the aesthetic of symphonic music. But inseparable from that is that those violins serve also as the main melodic role in the orchestra, which does require a robust volume and to get that (with only wooden acoustic fiddles) the vast numbers are also needed – even if, again, increasing numbers is a very expensive way of increasing volume.
The volume-need of the violins is not to persist against the low strings, those could easily just play quieter (as indeed they often do in a chamber music setting), but against the other instrument families which are inherently louder, in particular brass and percussion. Concretely, those instruments are capable of actually transforming a sizeable amount of the power a human player can put in, into sound. That's the reason the bass wind instruments, despite their bigger size, aren't really louder than their treble counterparts. In some cases they are actually quieter, since low pitches need more energy to be perceived as same volume.
On the other hand, on string instruments, except perhaps double bass, you first hit the limitations of the instrument itself. Even at fff, you can't put in all the force into a violin that a trained player's muscles could, because that pressure would just squelch the string.
Comment: Sure, feel free to rollback the parts you don't agree with. It looks like the Stack Exchange search doesn't care how you spell the word, but more primitive ones might care, and people are more likely to search for 'aesthetic'.
Comment: Also, as an addendum, for approx. each 10 violins added in the orchestra you likely double the dynamic palette of the instrument. This is important to obtain solid nuances from ppp to fff, not only a forte and a piano sound, with some indefinite state in between both. This, also, is an important musical role to most late Romantic and early Modern musics, bringing the common orchestra up to 300+ musicians on stage.
Comment: @Glorfindel thanks for your edit, but would you care to explain why some people consider uncommon spellings such as _æsthetic_ or _naïve_ as worth "correcting"? Those are perfectly cromulent in English writing, they don't make it harder to read, and it should be obvious that they're chosen deliberately and not as a slip of hand. If they mark me out as a pretentious cutup then so be it, that's my personal choice, isn't it?
Answer: If you don't mind some equations, you might look at "Why do choirs work?" for a bit of mathematical analysis of a closely-related question.
The key observation in the question there is that the mean amplitude of N people playing the same note together, with random phase-shifts, is proportional to the square root of N; the perceived loudness is roughly the log of this, so about half of the log of N. That makes the cellos a little louder than the basses --- say louder by some increment Q; the violas are an increment 2Q louder than the basses; the violins are an increment 3Q louder than the basses. Of course, that assumes that a single violin, a single viola, a single cello, and a single bass all have the same basic amplitude, which I doubt is true, but I'm not a string player. But the main point is that you don't end up with the violins being twice as loud as the violas, etc. --- there's a linear growth in loudness due to the exponential growth in numbers [again, assuming the same single-voice loudness].
This also ignores two other potentially important factors --- the differing rates of attenuation of different frequencies in a concert hall, and the differing perception of loudness as a function of frequency. It also assumes (pretty correctly, I think) that the listener is the same distance from all four string sections (i.e., that the differences in distances are small compared to the overall distance). That's probably wrong in the first 10 rows, but right thereafter.
Answer: Just for adding another (non-acustical) argument to the given answers:
Smaller ensembles need at least one instrument of each part, this results in 1:1:1:1
Bigger ensembles simply  reflect and represent the statistical spread of preferential instruments in the normal population.
Comment: "represent the statistical spread...in the normal population" – Not really. There are definitely more cellists than violists, and more flautists than hornists.
Comment: I'm pluss-ing only for the first statement - after all, chamber music composers define the instrumentation for the piece they're writing.  There are sextets, octets, etc of various voicings.


Prime ideals in ring of continuous functions

Question: Let $R:=C(\mathbb R,\mathbb R)$ be the ring of continuous $\mathbb R$-valued functions on $\mathbb R$. Is every prime ideal in $R$ maximal?
Comment: This is rather catastrophically false.  See https://math.stackexchange.com/questions/2078755/finding-a-space-x-such-that-dim-cx-n for a general result which shows how badly this fails.
Comment: @EricWofsey Thank you for pointing that out.
Answer: No, because that would imply $J(R)$ is nil and $R/J(R)$ is von Neumann regular. (I am denoting the Jacobson radical of $R$ as $J(R)$.)
But it is easy to show in this ring that $J(R)$ is $\{0\}$ and $R$ is not von Neumann regular.  (In fact, this ring has only trivial idempotents, but there must be many idempotents in a von Neumann regular ring.)
You can read elsewhere about the connection of prime ideals in this ring to ultrafilters. This is pretty much the best description you can get for prime ideals in such a ring, and it shows that in general prime ideals in a ring like this do not have to be maximal.
Comment: What is $J(R)$?
Comment: @principal-ideal-domain The Jacobson radical of $R$.


How is addition and subtraction determined when using word embeddings like Word2Vec?

Question: This question is particularly in the context of the word embedding algorithm Word2Vec.
I've noticed that many examples that are given in the original paper and other blog posts say things like vec(king) - vec(man) + vec(woman) = vec(queen) or vec(Madrid) - vec(Spain) + vec(France), but how is it determined when to use addition and when to use subtraction?
Intuitively, one can deduce that subtracting the country name from the capital and adding another country name would provide information on the new country's capital, but I was wondering if there was a formal explanation on how exactly this process goes.
Answer: There is not really a formal explanation, there is only intuition, like you have, backed up by actual computations that confirm it.
It just turns out that if you create a word embedding, that these computations work.
So, hypothetically, if 'Madrid' is mapped to [2, 3, 4], and 'Spain' is embedded as the vector [1, 1, 1], then the difference between those, [1, 2, 3], is very close to the difference between the embeddings of 'France' and 'Paris'. So you could say that the direction of [1, 2, 3], in the vector space, corresponds to the 'is the capital of' relation.
More realistically, these vectors are in 300d, not 3d as in the example above.
Comment: Thank you for the answer. It is a bit of a disappointment that there is no formal definition. That makes it even more amazing that the authors were able to come up with that idea. :)
Comment: +1. Two other interesting tidbits: 1) you're training a model to predict and it's not very good, but it's coefficients are the near-magical embedding, and 2) it's based entirely on context and can lead to "synonyms" that aren't really synonyms but rather are used in the same context.
Comment: Yes, it's quite an amazing result. Imagine hoping to find such a relation and then actually being able to publish it..
Comment: See https://mc.ai/arithmetic-properties-of-word-embeddings/


How to keep script from swallowing all of stdin?

Question: I have a script that reads from a pipe in a loop and runs an expect script and a normal shell script in the loop. Both scripts run ssh to another server to grab a piece of data.  For example:
cat /tmp/file |
while read a b c d
do
   s=`expect-script server1 $b`
   c=`ssh $b normal-script`
   echo $s $c
done

Even though there are many lines in /tmp/file, the script quits after processing the first line. I suspect that the expect script is swallowing all of stdin so that when it returns, there is nothing left to read. How can I avoid this? I don't want any of the scripts I call to read from the stdin of the main script.
Comment: Ah I see. Check the solution of this problem in different in another stackexchange network http://unix.stackexchange.com/questions/107800/using-while-loop-to-ssh-to-multiple-servers
Comment: possible duplicate of ["while read line" returns early when calling ssh "mysql..."](http://serverfault.com/questions/513858/while-read-line-returns-early-when-calling-ssh-mysql)
Comment: The possible duplicate is similar, but not identical. I can't use the -n option of ssh, because the ssh is called by the Expect script, which requires reading from stdin. I will play around with the redirection solution to see if that works for me.
Comment: Yes, that solution helped me get it to work, especially Richard Hansen's comment. Thank you!
Comment: @masegaloeh, I suggest adding an answer with that link, which Justin can accept, so that this question doesn't remain marked as unanswered.
Answer: It's actually ssh that's slurping up stdin. Just add the -n option:
    c=$( ssh -n $b normal-script )

If you don't want to do that, you can have your shell while loop read from a different file descriptor, leaving stdin untouched:
while read -u3 a b c d
do
    s=$( expect-script server1 $b )
    c=$( ssh $b normal-script )
    echo $s $c
done 3< /tmp/file

(assuming bash/ksh/zsh for the read -u option)
Answer: cat /tmp/file |
while read a b c d
do
    {
        s=`expect-script server1 $b`
        c=`ssh $b normal-script`
        echo $s $c
    } < /dev/null
done

The { command... } syntax allows you to apply redirection or piping to a sequence of commands.
I'll also note that you don't need cat in your example. You could do this:
while read a b c d
do
    ...
done < /tmp/file

Answer: Also, generally speaking, if you don't want some command to use current STDIN, you provide it with different one using <.
E.g.
   s=`expect-script server1 $b < /dev/null`

would make "expect-script" read it's STDIN from /dev/null (which happens to just return EOF) instead of consuming your current one (from /tmp/file)
You can also use | to provide some separate STDIN just for that command, e.g.
   s=`cat /tmp/stdin_for_expect | expect-script server1 $b`
   c=`cat /tmp/stdin_for_ssh | ssh $b normal-script`


Is there a CPAN way to parse random command line options?

Question: Is there a good (ideally CPAN) way to process arbitrary command line options with Perl?
E.g., take a string "-a 1 -b 'z -x' -c -d 3 4" and generate a GetOpt::Long - like data structure:
 { a=>1, b=>"z -x", c=>1, d=>[3,4] }  # d=>"3 4" is acceptable

The caveat is that

the set of options is NOT known in advance; so we seemingly can't use GetOpt::Long as-is.
The values themselves can contain other options, so simply parsing the string for #\b+-(\S+)\b# pattern to find all the options in THAT string also seems to not be possible, and further complicated that some parameters are of =s type, some =s@, some of "-x a b c" type.
Moreover, even if we could do #2, does GetOptionsFromString support correct tokenizing that respects quoted values?

NOTE: Assume for the purpose of exercise that ALL arguments are "options", in other words, if you split up the string into (possibly-quoted) tokens, your structure is always
 "-opt1 arg1a [arg1b] -opt2 ....".

In other words, any word/token that starts with a dash is a new option, and all the subsequent words/tokens that do NOT start with a dash are values for that option.
Comment: I note that [Getopt::Long](http://search.cpan.org/perldoc?Getopt%3A%3ALong) says: _A special entry `GetOptionsFromString` can be used to parse options from an arbitrary string. ... The contents of the string are split into arguments using a call to [Text::ParseWords](http://search.cpan.org/perldoc?Text%3A%3AParseWords)::shellwords._ I'm not sure that the description of Text::ParseWords::shellwords is reassuring.
Comment: If you don't know the options in advance, how can you tell how to parse the line?  If `-b` is not intended to take an option at all, then `'z -x'` starts the non-option ('file name') arguments, and `-c` and `-d` are also non-options (unless you like working without POSIXLY_CORRECT, but that just makes matters even less determinate).  I really don't think that anyone can do it without knowing what's permitted.  Your `-d 3 4` becomes problematic; `-d -3 -4` even more so; are the negative numbers after `-d` to be treated as options or negative integers?
Comment: @JonathanLeffler - the assumption is that any arguments are option arguments, of course. Otherwise it's an unsolvable problem. The naive rule is that any "token/word" starting with a dash is a new option and all the following tokens not starting with dashes are values for that option.
Comment: I would just use Text::ParseWords and then iterate through the results
Comment: You can use `Getopt::Long`; you'd call your hypothetical `Getopt::Random` module to return you an option string to be passed to `Getopt::Long`. The trickiest bit will be getting `GetOptions` to parse into appropriate variables. Given that the set of options is random, the set of variables you need to collect the values from is similarly random.  So, you probably end up using refs to parts of a hash so that you can iterate over the keys of the hash to find the option names and over the corresponding value to pull the command line information. Non-trivial, but not actually impossible. I think!
Answer: A quick example using Text::Parsewords and a simple state machine.
#!/usr/bin/env perl

use strict;
use warnings;

use Text::ParseWords qw/shellwords/;

my $str = q{-a 1 -b 'z -x' -c -d 3 4};
my $data = parse($str);

use Data::Printer;
p $data;

sub parse {
  my $str = shift;

  my @tokens = shellwords $str;

  my %data;
  my @keys;
  my $key = '_unknown';
  foreach my $token (@tokens) {
    if ($token =~ s/^\-//) {
      $key = $token;
      push @keys, $key;
      next;
    }

    if ( ref $data{$key} ) {
      push @{ $data{$key} }, $token;
    } elsif (defined $data{$key}) {
      $data{$key} = [ $data{$key}, $token ];
    } else {
      $data{$key} = $token;
    }
  }

  foreach my $key (@keys) {
    next if defined $data{$key};
    $data{$key} = 1;
  }

  return \%data;
}

Comment: Yeah, we ended up doing something similar for now but without  Text::ParseWords. I was soooo hoping for a non-self-bicycling solution :)
Comment: yeah, see there's always odd stuff, mine doesn't handle `-c` correctly. Thats why rolling by hand is dangerous (as you well know!)
Comment: fixed but getting hack-y-er all the time :/
Answer: Before I knew about Getopt::Long and the wisdom of using it, I rolled my own command line options processor that would take arbitrary arguments and populate a global hashtable. The rules were
Switches with a single letter (-A .. -Z, -a .. -z)
-n               sets  $args{"n"} = 1
-nfoo            sets  $args{"n"} = "foo"

Switches with more than one letter
--foo            sets  $args{"foo"} = 1
--foo=bar        sets  $args{"foo"} = "bar"

The joy and the hurt of this approach was you could quickly experiment with new command line options, changing code at the point that the option would be used without having to edit the call to GetOptions or having to allocation another variable:
 ... line 980 ...
 if ($args{"do-experimental-thing"}) {
     # new code
     do_experimental_thing();
 } else {
     do_normal_thing();
 }

This was the first module I uploaded to CPAN and I've since removed it, but the BackPAN has a long memory.


Give React setState parameters variable value

Question: I want to create a function handleFalse() in react so it works something like:
state = {
  Java: false,
  Python: false
}

handleFalse(language) {
  this.setState({language}:true);
}

render() {
  return(
    <Modal onHide={this.handleFalse("show")}></Modal>
  )
}

where language would be one of Java or Python.
Answer: You can use a computed property like this:
handleTrue(language) {
  this.setState({ [language]: true });
}

You can reference an object property with a variable by using [].
This works when accessing too: this.state[language]
Comment: Change to `onHide={() => this.handleFalse("show")}`. The way it is currently you are calling the `handleFalse` function every render, which sets state, which causes a re-render, and then repeats infinitely.
Comment: Thanks, but now I get an error saying "Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops." Do you know why?
Comment: That's going to be unrelated to this code here, and most likely caused by where you are calling the function. Can you show how you are calling it?
Comment: `onHide={() => doSomething()}` is assigning `onHide` a function. `onHide={doSomething()}` is assigning `onHide` the value of whatever `doSomething` returns.


How to create dictionary from list

Question: Im new to python so I apologize in advance if this is a bit rudimentary, but given the list:
lst = ['user1', 25, 'user2', 10, 'user3', 54]

how would I create a dictionary that would be:
usr_dict = {'user1': 25, 'user2': 10, 'user3': 54]

Answer: This has already been answered here.
But here is a method that is a little easier to understand:
usr_dict = {}
for i in range(0, len(lst), 2):
    usr_dict[lst[i]] = lst[i + 1]

Comment: ah ok thank you for that. It was the range part I was having trouble with. Much appreciated jdaz
Answer: I think the best approach is a dict comprehension:
usr_dict = {x[::2]:x[1::2] for x in lst}

How to create dictionary from list


Correlation Matrix as Input for Lavaan

Question: Is it possible to use a correlation matrix as input for Lavaan? I have searched the web but was not able to find an answer. Many Thanks.
Comment: Welcome to stackoverflow. I'm afraid that we have too few information to help you. Could you please tell us more. What is Lavaan? is it a package in R is it in Rbase? What is your issue exactly? I would recommend these [guidelines](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to maximize your chances of getting help.
Answer: The lavaan package is used to conduct latent variable analysis. While lavaan does support using a variance / covariance matrix as input to the procedure (see pp. 30 - 31 of the Lavaan tutorial for an example), it does not support use of a correlation matrix as input.

Correlation Matrix as Input for Lavaan


Pinch Zoom with gallery

Question: I'm trying to combine the capability of doing image zoom/panning
inside of a Gallery view. The problem is controlling which touch
events are processed by each. In a horizontal drag on the image, the
ImageView needs to process the drag in order to pan, up until the edge
of the image is reached then the gallery view needs to process the
touch event so that it can swipe over to the next photo.
It seems that if I return false from the imageview's ontouchevent,
then I won't be notified of the panning-drag move events.
What I tried to do was return true from the imageview's ontouchevent
until the edge of the image was hit then return false. However I
believe that this doesn't cause the Gallery to begin processing these
events, as it missed the initial touchdown event.
My next thought is to somehow turn the containing activity into the
touch handler, and somehow stop the Gallery and ImageView from getting
touch events normally, and have the activity forward the touch event's
manually. Not sure if this is possible or if there is an example.
My last resort will be to simply not use the android.widget.Gallery,
which I want to avoid because I want the widget to "feel" the same as
other places on the phone, and I like code re-use. Unfortunately I
might have to do this, as I see all other apps that have this zoom/pan
capability don't seem to use the Gallery widget (Gallery3D etc).
My question is, what's the best way to design for this situation where
motionevents need to split between two views? Any ideas would be
greatly appreciated thank you.
Comment: its a tough task to explain here.. paste you code here so that anyone can help you to figure it out.. ,
Answer: please follow the links here.
http://jmsliu.com/138/android-infinite-loop-gallery.html

Pinch Zoom with gallery


Mouse click and enter keyboard disable after one click in React

Question: What I am trying to do: I want to make it so that when you click on a button with the mouse or with enter from the keyboard that you cannot click on it again afterwards. In other words, disable it after pressing the button one time.
Clicking for the second time with the mouse worked, but I can't get it to work with enter.
My code is as follows.
function HomeFood() {
    const [food, setFood] = useState(null);
    const [calories, setCalories] = useState('');
    const [disabled, setDisabled] = useState(false);

    async function foodData() {
        const result = await axios.get(`link`);
        console.log('DATA RESULTS:', result.data);
        setFood(result.data)
    }

    function handleChange(e) {
        setCalories(e.target.value);
    }

    function handleKeyPress(e) {
        if (e.key === 'Enter') {
            foodData()
        }
    }

    return (

                <input
                    type='number'
                    onChange={handleChange}
                    onKeyPress={handleKeyPress}
                />
                <button
                    onClick={() => {
                        foodData();
                        setDisabled(true);
                    }}
                    disabled={disabled}
                >
                    Click here
                </button>

            {food && <FoodNutrients mealsData={food}/>}

    );
}

I tried to use the disabled state in the onKeyPress function, but only get errors. I also tried putting it in the input of the onKeyPress, but again more errors.
Answer: Just like the onClick, invoke setDisabled in handleKeyPress:
function handleKeyPress(e) {
    if (e.key === 'Enter') {

       if (disabled) { // do nothing if already disabled
           return;
        }

        foodData();
        setDisabled(true);
    }
}

Comment: Yes this is the one! Now I also can't use enter or mouse when I have used one of the two. Thank you!
Answer: Add a second disabled state, i.e. inputDisabled and toggle it on the same way. This allows each to disabled independently of the other.
function HomeFood() {
  const [food, setFood] = useState(null);
  const [calories, setCalories] = useState('');
  const [disabled, setDisabled] = useState(false);
  const [inputDisabled, setInputDisabled] = useState(false); // <-- new state

  async function foodData() {
      const result = await axios.get(`link`);
      console.log('DATA RESULTS:', result.data);
      setFood(result.data)
  }

  function handleChange(e) {
      setCalories(e.target.value);
  }

  function handleKeyPress(e) {
      if (e.key === 'Enter') {
          foodData();
          setInputDisabled(true); // <-- set input disabled
      }
  }

  return (

              <input
                  type='number'
                  onChange={handleChange}
                  onKeyPress={handleKeyPress}
                  disabled={inputDisabled} // <-- attach disabled prop
              />
              <button
                  onClick={() => {
                      foodData();
                      setDisabled(true);
                  }}
                  disabled={disabled}
              >
                  Click here
              </button>

          {food && <FoodNutrients mealsData={food}/>}

  );
}

If you wanted them to be disabled together at the same time then just use the same disabled state and setter for both.
Comment: Omg how stupid of me, I didn't even think about trying another state. Thank you!


Does concatening $5^n$ and $31^n$ ever lead to a prime?

Question: If we concatenate the numbers $5^n$ and $31^n$ , where $n\ge 1$ is an integer, then we get the number $$a_n:=5^n\cdot 10^m+31^n$$ where $m$ is the number of digits in the decimal expansion of $31^n$

Is $a_n$ prime for some $n\ge 1$ ?

I checked upto $n=10^4$ without finding a prime.
Comment: @HagenvonEitzen, for $n=8$ the factorization (if I entered the correct number) turns out to be $106129\times3680670249329$, so it seems unlikely there's any simple rule for the remaining case(s).
Comment: For odd $n$, $a_n$ is a multiple of $3$. For $n\equiv 2\pmod 4$, $a_n$ is a multiple of $13$.
Comment: @BarryCipra You entered it correctly.
Comment: @HagenvonEitzen The statement about $n \equiv 2$ mod $4$ is false.  First counterexample is $n = 118$.
Answer: There doesn't seem to be any obvious reason it should not be prime.  Heuristically, the probability of a "random" integer $x$ being prime is approximately $1/\log(x)$, and $\log(a_n) \approx n \log (5\cdot 31)$.
Since the series $\sum_n 1/n$ diverges, we should expect infinitely many $a_n$ to be prime.  But this is not a proof.


Does ICC intend to stop the ICC Champions Trophy?

Question: I've heard that ICC Champions Trophy 2013 will be the last Champions Trophy. Is it true?
If yes, what is the reason behind it?
Comment: A related newer post (after the decision to stop the competition has been changed): [Why ICC decide to go with Champions Trophy 2017?](https://sports.stackexchange.com/q/16199)
Answer: Yes, ICC Champions Trophy scheduled to take place in England this year will be the last.
This was done mainly for two reasons:

There already is an ODI tournament, the World Cup, that features all major teams. Probably, the ICC felt that there is no need for another ODI global tournament.
There was no world tournament for Test matches. So it was logical that ICC scrap the Champions Trophy and create one for Test matches. In effect, ICC World Test Championship will replace Champions Trophy from 2017.

Source: ICC News - Cricinfo.


Are decision trees sensitive to log translations in feature space?

Question: This question was partially answered on Are decision trees sensitive to translations in feature space?, but no references were provided for "Gini impurity and entropy measures are translation invariant".
I couldn't find material relating to this topic, so does anyone know either:
a) Why Gini is translation invariant, or
b) Why the results of a decision tree would be otherwise insensitive to translations (e.g. log) in the feature space?
Thanks.
Answer: It depends on what algorithm is being used to build the tree. CART trees are invariant to scale changes so a log transform should not change the resulting tree. However, the values of the split rules will be changed to the log scale.
The reason for this is because the splitting process sorts each feature (numeric) and then checks midpoints between successive observations for impurity improvement based on splits at the interval point. The maximum across observations and features is chosen for that node and the process continues. This means that if you rescale any feature(s) as long as the relative ordering of feature values is maintained-which a log transform will maintain-the tree will be the same but the split values will be log-transformed.
Comment: Note that there are also tree induction algorithms that do not only rely on tests of binary splits at midpoints but also on tests of linear associations (e.g., the CTree algoirthm with default settings). Consequently, such algorithms are invariant against linear transformations but not nonlinear transformations (like logs).
Comment: Yes, thanks, I was referring to trees grown via CART. Do you have any information as to why CART trees are invariant to scale/ non-linear transformations?
Comment: @DataAnalyticsAnalyst will add a description of why CART trees are invariant to scale changes to my answer.
Comment: Wouldn't the only issues here be with in machine precision? That is, taking the log of a very small number could aid in sorting; I'm thinking similar to how we always take the log of the likelihood in maximum likelihood estimation.
Comment: @Mark White -> Not sure how taking a log transform would aid in sorting. Assuming all observations are positive the log transform preserves the ordering. Most sorting algorithms use a binary comparison with a partial ordering so the numeric magnitudes are (usually) not relevant. What did you have in mind?
Comment: This probably won't be very useful in 99.999% of applications, but imagine we have predictors that are incredibly small, e.g., 6e-40, or something. Calculations might be hard to do well at that precision, so taking the log would help with precision. For instance, the difference between 7e-40 and 6e-40 is so small, but the difference between the log(7e-40) and log(6e-40) is 0.1541507, which is easier for a computer to handle. Again, this is more of a theoretical comment on my part than something OP should worry about, but I was wondering when the log might make a difference.
Comment: @AchimZeileis yes very good point about split types that are non-necessarily univariate. Hopefully the first sentence of my answer "
It depends on what algorithm is being used to build the tree." caveats this enough that readers are not mislead.
Comment: It's not just splits in linear combinations. CTree selects in each leaf a variable for splitting by running an association test of y and each x_j before selecting the split point in the best x_j*. The default test in the R package `partykit` (or the older `party`) is for linear associations between y and x_j and hence not invariant to nonlinear transformations. Of course, the default can be changed, e.g., to a test of monotonic rank correlations our maximally selected statistics. But it's a common misconception among our users that transformations do not matter at all.


Thoughts on customizing the user profile page?

Question: I'm looking to really transform the user profile page for my drupal 7 site and community into something enganging and interative. Perhaps a friends block, list of social media links like fb, linked in, etc as well as to be able to see new posts and comments that that user has contributed to the site.
Certainly we can do better than the default drupal page these days!
What modules do you recommend to start putting together a recipe for a quality and informative user profile experience?
Also, does you have any links for tutorials that would be good resources? Perhaps even a cool profile page you have done as an example.
Answer: I think the two key modules for this would be Views and Panels. That should get you most of the way but, depending on what you want to display, you may need some other modules.


Monte Carlo Sampling is performing better then Bayesian Optimization

Question: So I am testing the Bayesian optimization library for determining where to sample next by quering a test function such as 2d Rosenbrock to better reconstruct that function using Gaussian Process regression. In other words I want to perform selective sampling of the function to reconstruct that function using Gaussian Process regression (GP) in least amount of selective sampling.
So in the literature Bayesian optimization is considered a best way to learn a function in fewer steps efficiently. So lets say I want to sample at 100 locations and then construct the estimation of Rosenbrock function using GP, performing pure random or monte carlo sampling gives better results then performing Bayesian optimization!
So I want to ask why is Bayesian optimization considered useful then?

Additional notes:
The reproducible code would be very huge beyond the scope of SE guidlines.
Based on the answer the additional information is following:

the a priori information is initially constructed by performing random sampling or MC sampling of the function and fitting the GP - which automatically tunes the hyperparameters in the Sklearn library.
The kernel that is fitted is Matern which is widely used for such cases.
In general if I am doing sampling such as 100 samples, then in case of pure MC sampling i am taking all 100 samples randomly and then fitting the GP and comparing with the Rosenbrok function to see the error and for Bayesian i am taking 50 samples randomly initially - fitting the GP to form the prior - 50 using Bayesian optimization and then re-fitting the GP and comparing with the Rosenbrok.
Answer: Unfortunately you have given no reproducible code for us to investigate what is leading you to the results you have found. You also state that you want to reconstruct the function which neither random sampling nor Monte Carlo will perform.
Bayesian active sampling and Gaussian processes more generally perform well in regression when the priors over function spaces encoded in the kernel place sufficient probability mass over the function which you are hoping to infer. Mismatches in prior information can indeed hinder performance. If I was to diagnose your results I would start there and ask is the kernel appropriate and what is happening to the hyper parameters during optimisation.
Bayesian optimisation however is used to find global maxima or minima. It is generally speaking used when the optimisation surface is assumed multimodal and it is desirable to both explore the space and exploit modes under a limited budget. Once again this is only useful when the kernel uses in BO incorporates some a priori knowledge of the space such as smoothness.
Comment: I have included additional details incase you would want to elaborate your answer.
Comment: @GENIVI-LEARNER Thanks for the additional information. I'm still not sure why you are doing BO as I believe you are looking to do active sampling without a bias towards optimisation (purely exploration with no exploitation). If you wanted to do a similar comparison that was more appropriate for BO, I would compare the minima (or maxima depending on the optimisation goal) of the random selection versus BO. Also, I would suggest confirming that the BO kernel and the fitted kernel are the same.
Comment: Equally, if it is that you simply want to do active sampling to minimize the residual error, I can give you some alternative acquisition functions which will not endeavor to optimize the space.

Happy to help if you are still having difficulties.
Comment: Yes correct. I am using EI as acquisition function. Please do suggest alternatives.
Comment: So if you want to simply explore the space then you could do something similar in nature to EI by setting the acquisition function to just the standard deviation of the GP. In essence acquire points based on where your GP has greatest uncertainty. What you will be doing is sampling points away from one another like draws from a determinental point process (DPP). If you work through the math you'll notice this process does not consider the y-values of the observations other than for optimizing the kernel. Hope it works better for you, let us know!


Are coproduct exact functors?

Question: Are coproducts left exact or right exact functors in general?
Let k be a commutative ring (unital assosiative).
Specifically in the category of k-algebras is the tensor exact.  (This is not the case in the category of k-modules, but then again the tensor is not a coproduct therein).
Comment: Colimit functors are right exact.
Answer: Let $A$ be a category with finite colimits. Denote by $\sqcup\colon A\times A\to A$ the functor which sends every pair $(a_1,a_2)$ of objects to their coproduct $a_1\sqcup a_2$. Then $\sqcup$ is left adjoint to the diagonal functor $\Delta\colon A\to A\times A$, then $\sqcup$ preserve all colimits and therefore is right exact.
But they are not left exact in general. For example, functor $\sqcup\colon\mathbf{Set}\times\mathbf{Set}\to\mathbf{Set}$ doesn't preserve terminal objects.
Comment: @CSA You are welcome.
Comment: Prefect!!! Thanks alot Oskar, I'll have to modify my proof a tiny bit but should be fine :)
Answer: To answer your question about commutative $k$-algebras:
If $A$ is a commutative $k$-algebra, then $A \otimes_k - : \mathsf{CAlg}(k) \to \mathsf{CAlg}(k)$ is right exact, and it is also left exact when $k$ is a field, or more generally when $A$ is flat over $k$ (i.e. as a module). But in general, the functor doesn't even have to preserve monomorphisms.

Are coproduct exact functors?


two alternatives for one song in a bookpart

Question: I am making a music book where each song is on its own page. The code for each song is:
title = ...
music = ...
\bookpart {
  \header {
    title = \title
  }
  \score {
    \music
    \layout { }
  }
}

I have a special case where I want to put two songs on one page, because they are actually the same song, only in two different melodies. It should look like this:

Maybe someone can help me achieve this output?

Here is the code I wrote for that special page:
title = "Es war eine Mutter"
\bookpart {
  \header {
    title = \title
    piece = "offical version"
  }

  % official version
  \score {
    \relative
    {
      \numericTimeSignature
      \time 3/4
      \repeat volta 2 { c'4 f f g a a f g g e f f }
    }
    \addlyrics {
      Es war ei -- ne Mut -- ter, die hat -- te vier Kin -- der:
    }
    \addlyrics {
      Den Früh -- ling, den Som -- mer, den Herbst und den Win -- ter.
    }
    \layout { }
  }

  % Jane's version
  \score {
    \relative
    {
      \numericTimeSignature
      \time 3/4
      \repeat volta 2 { c'4 f f a g g a e e g f f }
    }
    \addlyrics {
      Es war ei -- ne Mut -- ter, die hat -- te vier Kin -- der:
    }
    \addlyrics {
      Den Früh -- ling, den Som -- mer, den Herbst und den Win -- ter.
    }
  }
}

which gives this output:

This has two issues:

I don't want lyrics below the second version. But if I remove them, the notes will not align with the first version any more (they come closer together). But I want to keep the alignment.

I don't know how to set a different piece name for the second version. Since I am inside a \bookpart, I cannot use a \header block inside the second \score block to set a local piece name.

I also tried a different approach: Putting both melodies into the same score.
\bookpart {
  \header {
    title = \title
    piece = "official Version"
  }
  \score {
    <<
    % official version
    \new Staff
    \relative
    {
      \numericTimeSignature
      \time 3/4
      \repeat volta 2 { c'4 f f g a a f g g e f f }
    }
    \addlyrics {
      Es war ei -- ne Mut -- ter, die hat -- te vier Kin -- der:
    }
    \addlyrics {
      \override VerticalAxisGroup.nonstaff-unrelatedstaff-spacing.padding = #5
      Den Früh -- ling, den Som -- mer, den Herbst und den Win -- ter.
    }
    % Jane's version
    \new Staff
    \relative
    {
      \numericTimeSignature
      \time 3/4
      \repeat volta 2 { c'4 f f a g g a e e g f f }
    }
    >>
    \layout { }
  }
}

This gave me the following output:

which again has issues, namely:

There's the connection bar (or whatever it is called) between both melodies. I don't want that, since it suggests to play both melodies simultaneously.
I don't know how to set a piece name for the second version at all, since it is just the same \score.
Comment: I still ought to keep my mouth shut because it's irrelevant to this question as well... but the first note really needs to be a pickup.
Comment: I would question the desire to not print lyrics with the second melody. It's always hard to look at one part of the page for pitches and elsewhere for lyrics. This seems to be a well-known German children's song, so perhaps those familiar with it would know them by heart, but I certainly don't.
Comment: @AndyBonner There is no "elsewhere" to look - the images I attached contain all notes and all text of the song (except other stanzas). Since the second version is just below the lyrics, the amout of eye movement when playing either version should be roughly the same. I'd agree with you if I had a larger piece, but not for a one-liner. (Side note: I want to save precious space; the pages in my book are quite small an there should be as much space as possible for a picture. It's going to be a song book for a 3-year-old, and since he can't read, he will use pictures as identifiers for the songs.)
Answer: Different piece names for each score
\Score blocks can have their own \header block. Remove piece =  from the outer header block, and add a header block at the beginning of each \score:
\header { piece = "Appropriate Title" }

Removing lyrics, but retaining note alignment
To remove the lyrics from the alternate melody, add
\layout { \hide LyricText }

at the end of the corresponding \Score block.
Comment: Have you tried your code? As I said: "Since I am inside a \bookpart, I cannot use a \header block inside the second \score block to set a local piece name.". I tried that, but it gave a compile error. See https://lilypond.org/doc/v2.23/Documentation/notation/creating-titles-headers-and-footers.html ("\header blocks may be placed [...] Within a \bookpart block but outside all \score blocks within that bookpart."). Still +1 for the hidden lyrics.
Comment: @Kjara Yes, I ran the code without error and double-checked by running it again just now. The bullet point immediately after the one you quoted says that a \header may be placed "within a \score block". (I used the first full version of your code and modified that.)
Comment: I don't know what I did there. I got compilation errors whenever I put a header block inside the score block (that's why I interpreted the quoted text so that inside bookpart, it is forbidden to have any header block inside score), but I don't get them now. Never mind!