Using Python String Formatting with Lists

Question: I construct a string s in Python 2.6.5 which will have a varying number of %s tokens, which match the number of entries in list x. I need to write out a formatted string. The following doesn't work, but indicates what I'm trying to do. In this example, there are three %s tokens and the list has three entries.
s = '%s BLAH %s FOO %s BAR'
x = ['1', '2', '3']
print s % (x)

I'd like the output string to be:
1 BLAH 2 FOO 3 BAR
Comment: related: [Formatted string literals in Python 3.6 with tuples](https://stackoverflow.com/q/38763895/10197418) in case you're looking for an f-string based solution.
Answer: You should take a look to the format method of python. You could then define your formatting string like this :
>>> s = '{0} BLAH BLAH {1} BLAH {2} BLAH BLIH BLEH'
>>> x = ['1', '2', '3']
>>> print s.format(*x)
'1 BLAH BLAH 2 BLAH 3 BLAH BLIH BLEH'

Comment: @SabreWolfy: In Python 2.7, you can omit the field numbers: `s = '{} BLAH {} BLAH BLAH {} BLAH BLAH BLAH'`
Comment: The OP's problem is not the method but the format of the parameters. `%` operator only unpacks tuples.
Comment: @SabreWolfy If you construct it precedurally then you might find it easier to name your placeholders and use a dict to format the resulting string: `print u'%(blah)d BLAHS %(foo)d FOOS …' % {'blah': 15, 'foo': 4}`.
Comment: @kotchwane : the * function is a special python keyword that transform an argument that is a list into a list of arguements :). In our case, *x will pass to the format method the 3 members of x.
Comment: What is the meaning, or function, of the asterisk (*) ?
Comment: This would make the construction of `s` more complex, as I would need to include incrementing numbers rather than simply `%s` for each position. `s` is built up in a number of different steps, so it's much easier to just include the `%s` token.
Comment: This answer is potentially confusing, as it makes it look like the numerical values of 0, 1 and 2 are somehow related to the number of times the word "BLAH" appears.
Comment: @SabreWolfy edit my answer to avoid repetitions and potential confusion
Answer: print s % tuple(x)

instead of
print s % (x)

Comment: `print s % (x)` is what OP wrote, I was just quoting him/her.
Comment: `(x)` is the same thing as `x`. Putting a single token in brackets has no meaning in Python. You usually put brackets in `foo = (bar, )` to make it easier to read but `foo = bar,` does exactly the same thing.
Comment: I use the `(x)` notation for clarity; it also avoids forgetting the brackets if you later add additional variables.
Comment: This is the best answer! It's much simpler and shorter than the `.format()` one, which  is undeservedly the most upvoted one!!
Comment: I was just providing a language tip, not criticizing your answer (in fact I +1'd it). You did not write `foo = (bar, )` either :)
Comment: How about if you want to source your variables from multiple lists?  tuple only takes a single list, and the formatter seems to only take a single tuple
Comment: Answered my own question: `print s % (tuple(x) + tuple(y))`
Answer: Following this resource page, if the length of x is varying, we can use:
', '.join(['%.2f']*len(x))

to create a place holder for each element from the list x. Here is the example:
x = [1/3.0, 1/6.0, 0.678]
s = ("elements in the list are ["+', '.join(['%.2f']*len(x))+"]") % tuple(x)
print s
>>> elements in the list are [0.33, 0.17, 0.68]

Answer: Here is a one liner. A little improvised answer using format with print() to iterate a list.
How about this (python 3.x):
sample_list = ['cat', 'dog', 'bunny', 'pig']
print("Your list of animals are: {}, {}, {} and {}".format(*sample_list))

Read the docs here on using format().
Comment: @kotchwane the asterisk "expands" the list as individual ordered arguments. f(\*x) == f(x[0], x[1], ..., x[n]). Similarly, two asterisks (\*\*) expands a dict to keyword arguments.
Comment: What is the meaning, or function, of the asterisk (*) ? Your link to the documentation does not seem to clarify that.
Answer: Since I just learned about this cool thing(indexing into lists from within a format string) I'm adding to this old question.
s = '{x[0]} BLAH {x[1]} FOO {x[2]} BAR'
x = ['1', '2', '3']
print (s.format (x=x))

Output:
1 BLAH 2 FOO 3 BAR

However, I still haven't figured out how to do slicing(inside of the format string '"{x[2:4]}".format...,) and would love to figure it out if anyone has an idea, however I suspect that you simply cannot do that.
Comment: i dont really have one ... i think i just thought it would be neat to be able to do
Comment: I am a little curious about your use case for slicing.
Comment: Note that you can also do: `'{0[0]} BLAH {0[1]} FOO {0[2]} BAR'.format(x)`. In other words, you do not need to name the argument.
Answer: This was a fun question! Another way to handle this for variable length lists is to build a function that takes full advantage of the .format method and list unpacking. In the following example I don't use any fancy formatting, but that can easily be changed to suit your needs.
list_1 = [1,2,3,4,5,6]
list_2 = [1,2,3,4,5,6,7,8]

# Create a function that can apply formatting to lists of any length:
def ListToFormattedString(alist):
    # Create a format spec for each item in the input `alist`.
    # E.g., each item will be right-adjusted, field width=3.
    format_list = ['{:>3}' for item in alist]

    # Now join the format specs into a single string:
    # E.g., '{:>3}, {:>3}, {:>3}' if the input list has 3 items.
    s = ','.join(format_list)

    # Now unpack the input list `alist` into the format string. Done!
    return s.format(*alist)

# Example output:
>>>ListToFormattedString(list_1)
'  1,  2,  3,  4,  5,  6'
>>>ListToFormattedString(list_2)
'  1,  2,  3,  4,  5,  6,  7,  8'

Answer: For just filling in an arbitrary list of values to a string, you could do the following, which is the same as @neobot's answer but a little more modern and succinct.
>>> l = range(5)
>>> " & ".join(["{}"]*len(l)).format(*l)
'0 & 1 & 2 & 3 & 4'

If what you are concatenating together is already some kind of structured data, I think it would be better to do something more like:
>>> data = {"blah": 1, "foo": 2, "bar": 3}
>>> " ".join([f"{k} {v}" for k, v in data.items()])
'blah 1 foo 2 bar 3'

Answer: x = ['1', '2', '3']
s = f"{x[0]} BLAH {x[1]} FOO {x[2]} BAR"
print(s)

The output is
1 BLAH 2 FOO 3 BAR


How do I maintain API versions?

Question: I have different REST End points in my Drupal 8 instance, some of them are exposed from REST Views and some are custom controllers. I have a situation where I want to maintain versioning with my end points.
I have replaced some fields in the new version and the users relying on the older API version must not be impacted by the change.
In the case of a controller, how can I manage different output for different API versions?
Also, in the case of REST Views, where the URL is directly exposed, how can new data be added to that?
For example, the /rest/taxonomy-list endpoint gives a certain set of terms as JSON output. In the new API version, I have to add/remove/replace one term which is present in same vocabulary.
Comment: In the case of a controller, you use the version number as part of the URL, for example /api/2/rest-endpoint-name and /api/3/rest-endpoint-name.
Comment: @kiamlaluno, change in URL can be done, but if I have a minor change in the output, do I have to create a new method for it?
Comment: I would use the new URL for the new versions, and leave the old URL for legacy. I would not create a different route for a minor change. To me, /api/3/rest-endpoint-name is totally different from /api/2/rest-endpoint-name, at the point that trying use the output of /api/3/rest-endpoint-name as it were the output of /api/2/rest-endpoint-name, I would get totally wrong results.
Comment: Consider my example for an instance where I'm listing all terms of vocabulary in v1 and I want 10 newly added terms only in v2. Can that be managed from Drupal?
Answer: One approach to version the API is to add a version number to the URL.
So, for example your Views REST display URL /rest/taxonomy-list would become /rest/v1/taxonomy-list or /rest/v2/taxonomy-list
You could duplicate the existing Views display to adjust the fields and new URL in a new REST display.
For custom REST API plugins, you can extend the existing RestResource and give it a new URL, e.g:
use Drupal\my_module\MyRestResource;

/**
 * Sample Rest Resource
 *
 * @RestResource(
 *   id = "my_rest_resource_v2",
 *   label = @Translation("My Rest Resource (v2)"),
 *   uri_paths = {
 *     "canonical" = "/api/rest/v2/endpoint"
 *   }
 * )
 */
class MyRestResourceV2 extends MyRestResource {

  // Extend and override functions here

}

Comment: It will work in case of Rest Plugins but in case of view URLs?
I don't want to duplicate a view for the change in a single field.

How do I maintain API versions?


Contar linhas do dataframe com string de acordo com a posição no texto Python

Question: Tenho um dataframe com uma coluna de texto, como a seguir:
import pandas as pd

df = pd.DataFrame([["1", "texto com PALAVRA frAse PARAGRAFO", True, "foo"],
                   ["2", "texto com palavra Paragrafo", False, "foo"],
                   ["3", "texto com Frase paragrafo", True, "foo"],
                   ["4", "texto com FRASE", True, "foo"],
                   ["5", "texto com frase", True, "foo"],
                   ["6", "frase", True, "foo"],
                   ["7", "texto", False, "foo"],
                   ["8", "texto com paRAgrafo", False, "foo"]],
                  columns=["id", "texto", "col1", "col2"])

Quero contar todas as linhas em que, por exemplo, "frase" ocorre antes de "palavra" ou "paragrafo"; isto é, considerando que o que aparece "primeiro" é mais relevante no contexto, se fossem conjuntos, desconsideraria as "interseções" e teria:

linhas com "palavra": 2

linhas com "frase": 4

linhas com "paragrafo": 1

Pode ter mais texto antes --- por exemplo, "texto" e "com" não interessam/interferem ---; e, claro, preciso desconsiderar maiúsculas e minúsculas na hora da contagem.
Com str.contains, não leva em conta a "posição":
df_filter = df[df['texto'].str.contains('frase',case=False)]
len(df_filter)

5

Como fazer então?
Answer: Você pode criar uma função que retorna True se um determinado alvo aparece antes de outros termos em um texto :
def veio_antes(texto, alvo, outros):
    texto = texto.lower()
    alvo = alvo.lower()

    if alvo in texto:
        texto_antes = texto.split(alvo)[0]

        for o in outros:
            if o.lower() in texto_antes:
                return False

        return True

    return False

Uma função lambda que passa os argumentos necessários para veio_antes pode ser mapeada para os elementos da coluna do seu dataframe que contem os textos de interesse e a série resultante pode ser sumada para indicar a quantidade total de vezes que um alvo aparece antes dos outros termos. Se você quiser fazer isso para todos os termos de uma lista, você pode fazer o seguinte:
alvos = ["palavra", "frase", "paragrafo"]

for alvo in alvos:
    outros = [a for a in alvos if a != alvo]
    veio_antes_map = lambda x: veio_antes(x, alvo, outros)
    print('linhas com "{}": {}'.format(alvo, sum(df["texto"].map(veio_antes_map))))

Saída:
linhas com "palavra": 2
linhas com "frase": 4
linhas com "paragrafo": 1

Answer: Bom pelo que entendi da sua pergunta você esta querendo verificar a ordem em que as palavras acontecem no texto, bom eu vou considerar que você tem as palavras quais quer verificar a ocorrência.
fiz um exemplo que pode ajudar você a criar a logica que se aplica melhor ao seu código:
frase = 'Frase para testar'

def verificar_qual_palavra_vem_antes(frase, p1, p2):

    frase = frase.lower()

    frase_partida = frase.split()

    posição_palavra_um = frase_partida.index(p1)

    posição_palavra_dois = frase_partida.index(p2)

    if posição_palavra_um < posição_palavra_dois:
        return 'a palavra 1 veio primeiro'

    else:
        return 'a palavra 2 veio primeiro'

Explicando o código
O funcionamento dele é bem simples, foi criada uma função que faz essa verificação de posições com os parâmetros Frase - palavra 1 e palavra 2
a primeira coisa que a função faz e usar a função built-in do Python lower() que consistem em transformar toda a string em caixa baixa
logo após é utilizado o split para dividir todas as palavras em um lista
e assim por final verificar o endereço de cada palavra na lista
então se o endereço da palavra 1 for menor que endereço da palavra 2 ja tem a ordem que ocorre as palavras


How are the three sectors on an F1 track determined?

Question: I know that there are three sectors in each F1 track, coloured red, blue, and yellow. But how is the track divided up into these sectors? Are they all the same length (i.e. if a track is 3km then each sector is 1km)? Or is there some F1 regulation covering this?
Comment: aren't they actually purple, green and yellow?
Comment: Thats how the current sector is going. the red/blue/yellow are in the track previews.
Comment: @CGCampbell Purple, Green and Yellow is stands for timing for each sector. Red, Blue and Yellow stands for parts of each sector.
Answer: I think there is no straight science of 'all sectors should be the same length or the same time'.
Looking at the Canadian GP

Just giving it a quick look over, its not distance. The red sector is clearly shorter.
For times, looking at the times of FP1 of 2022: https://www.reddit.com/r/formula1/comments/veo0re/canadian_gp_2022fp1_sector_times/
Sector 1: 20s. 2:24s. 3:29s. So not time either.
My best bet is that is at the discretion of the track owners. There is rules that you should try to make the equal as much as possible, but also, and that would be the main rule, that sectors should be split where the sector change is in a 'neutral' place. Where there is not a part of 'its in the middle of a turn so the times change depending on what point of the turn you are.


Converting logarithm to decimal form

Question: I apologize if this is a poorly formatted question, but i really need some help here...
I am trying to solve the following problem: $4\ln^3$
When I input this into my calculator, I get $4.3944$. However, when i input it into mathway, I get $5.0136$, which is the correct answer. Here is a picture of it in mathway: mathway_img
I have spent the last $2$ hours trying to figure out how to properly convert this problem into decimal form, as well as why my calculator keeps giving me a different answer. But, since I am new to logarithms, I have not been able to figure out how to get the answer $5.0136$.
Could someone please tell me how mathway gets this answer? Also, why does my calculator give me a different answer than mathway? Am I inputting it incorrectly?
Comment: You're right: $4\ln 3$ is something completely different. But you've completely ignored part of the expression: what happened to the $11$ inside the logarithm and the $11$ in the denominator?
Comment: Given the image at the link, I find it hard to believe that your input to your calculator was actually $4\ln^3$, which should in fact not yield any numerical result; what did you actually input?
Comment: @BrianM.Scott  I put in 4ln(3) but i do not think that is the same as putting in 4ln^3. I have tried inputting it multiple ways but i still cannot seem to get the answer 5.0136.
Comment: What do you mean $4\ln ^3$?  What does that *mean*?  Did you mean $4 \ln(3)$ or $4\ln^3 (something)$ (if so what?).  The phrase $4\ln^3$ doesn't mean *anything*.
Comment: Why are you leaving out the $11$?  That is an *essential* part of the problem.  Are you assuming you can cancel things out from *within* the expression?  For example would you think $\frac {2\sqrt{7}}{7} =  {2\sqrt{{\ \ }}}$?  But $2\sqrt{\ \ }$ is meaningless.  So is $4\ln^3$ is meaningless.
Answer: The expression in question is
$$\frac{4\ln^3(11)}{11}\;.$$
In this context $\ln^3(11)$ means $(\ln 11)^3$, just as $\sin^2\theta$ normally means $(\sin\theta)^2$; this is approximately $2.3978953^3$, or about $13.787662$. Now multiply that by $4$ and divide by $11$ to get about $5.0136953$.
Comment: That explanation made perfect sense! Like i said before i am new to logarithms so i was unsure what the proper form of this problem was. Thank you!
Comment: @MoistCarrot: You're welcome!
Answer: Input this into your calculator instead:
$$\frac{4\times (\ln (11))^3}{11}.$$
Answer: You are most likely inputting it incorrectly into your calculator.
The answer you are getting is due to the following input:
$$4\cdot\ln(3)$$ which will give you 4.3944...
As others have noted already, try this instead.
$$\dfrac{4\cdot(\ln(11))^{3}}{11}$$
Using the parenthesis above might clean up some of the confusion especially if it's an older style calculator.

Converting logarithm to decimal form


CURL returning different responses

Question: My code is this:
<code>$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: application/json','Content-Length: ' . strlen($content), 'Cache-Control: no-cache'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);

if($result===FALSE)
{echo "cURL ERROR:".curl_error($ch);}

echo $result;
</code>
The problem is the fact that sometimes it works and sometimes it doesn't.
When it works I get a valid JSON response, like:
Working,
but when it doesn't, I get this:
Not Working
I should mention that I don't have access to the API server. Can anyone help me with any idea? Thank you!
Comment: You are either querying two different pages, or the API is returning two different results. The first page you are querying returns JSON. The second page you are querying returns HTML. As such, you'll have to parse them differently.
Comment: Thank you for your quick response.

I'm testing from 2 different pages, but CURLOPT_URL ($url) is the same.
Though I'm testing using 2 different forms on 2 different pages, everything is the same: HTML code, js code (for Ajax), $content of CURLOPT_POSTFIELDS. The only difference is in the CURL $result.
Most of the cases when the errors occur are when adding a GET parameter to the URL of the submitted form (ex: https://page.com?param=xx).

NOTE: The error doesn't occur just for one of the URLs with the forms, but randomly on both. Does the Not working response look familiar to anybody?
Comment: Thinking that the GET param from https://page.com?param=xx might affect the URL, I also changed :
"url: "/wp-content/api/api.php""  in   "var origin_url = window.location.origin;
jQuery.ajax({
    type: "POST",
    url: origin_url+"/wp-content/api/api.php","
Answer: The issue was that I was using a minification plugin that after a while was breaking the jQuery ajax call.
I solved it by implementing an inline script for the ajax call, using just javascript (no jQuery library dependency).

CURL returning different responses


How can I overlay one image over another so that dark background is transparent?

Question: I have 2 images, test1.jpg and test2.jpg that are RGB images. They have been converted from a 2D numpy array so they are monochrome images. They have the same shape. When I use the paste function, I only see one of the images instead of both.
Here are the test1 and test2 jpgs:

.
This is what I get after doing test1.paste(test2) and test1.save('final.jpg'):

Why is it only showing test2?
Here is my code:
im1 = Image.open('test1.jpg')
im2 = Image.open('test2.jpg')
im1.paste(im2)
im1.save('final.jpg')

Comment: I thought that paste basically overlays both images. Is that wrong? I want both "arms" to be in the same image, and I thought that's what paste is for.
Comment: "This is what I get after doing test1.paste(test2) and test2.save('final.jpg')"

Are you saving test1 or test2?
The sample code does a different thing from your explanation. The sample should work.
Comment: Ohh, that makes sense. I think I'm misunderstanding what paste does. Is there a way/function for just combining the contents of the 2 images regardless of the background (I want both arms in one image, so basically just add the white stuff from the 1st image to the 2nd image in the exact same position).
Answer: You simply need to choose the lighter of your two images at each point with PIL Channel Operations:
from PIL import Image, ImageChops

im1 = Image.open('test1.jpeg')
im2 = Image.open('test2.jpeg')

# Choose lighter of the two images at each pixel location
combined = ImageChops.lighter(im1,im2)

Note that you could use paste() as you originally intended, but that it will paste all the black as well as the white pixels from image2 over image1. In order to avoid that, you would need to make a mask and only paste where image2 is non-zero. That might look like this:
im1 = Image.open('test1.jpeg')
im2 = Image.open('test2.jpeg')

# Make greyscale mask from image2
mask = im2.convert('L')
mask = mask.point(lambda i: 255 if i>0 else 0)

# Paste image2 into image1 only where image2 has non-black content
im1.paste(im2, mask=mask)

I just think the ImageChops.lighter() method is simpler.

Note that these two methods will give subtly different results. For example, if a pixel is 192 in image1 and 67 in image2, the ImageChops.lighter() method will result in 192, whereas the paste() method will see there is something in image2, and therefore give you the 67. Your choice!
Comment: Thank you! Both of these work nicely.
Answer: paste is the wrong tool for this job, because it completely replaces the original image with the image you're pasting.  It appears you wanted something closer to the blend function which produces a mix of both images.  Unfortunately this has the side effect of darkening the image, because white blended with black becomes a middle gray; you'll probably want to double the values to compensate.
final = Image.blend(test1, test2, 0.5)
final = Image.eval(final, lambda x: 2*x))

Answer: One solution would be to use openCV instead of PIL. OpenCV's imagery is identical to a 2D numpy array, which means you can instead just use basic matrix addition to produce a combined image given "black" pixels are just 0.
import cv2 as cv

im1 = cv.imread('test1.jpg')
im2 = cv.imread('test2.jpg')

im3 = im1 + im2
cv.imwrite('test3.jpg', im3)

The black pixels remain black, as you're adding 0 to 0, while the filled pixels have the filled values of the two images combined (in this case, 0 + a maximum of 255 given no overlap between the two images)
The resulting image:

In the case of overlapping non-zero pixels, you may want to apply a 255 ceiling to the arrays, or instead normalize the combined image by the new maximum if that is preferred for your use case.


Linear Regression: Extremely Imbalanced Categorical Features

Question: I am working on a Kaggle Housing Dataset (https://www.kaggle.com/c/house-prices-advanced-regression-techniques) to predict a house price using several regression techniques. For this project I am using R/Rstudio. However, I ran into a issue when dealing with categorical data. The problem is that most of the categorical features are extremely imbalanced. Here are some of the examples.
table(data$Condition2)

Artery  Feedr   Norm   PosA   PosN   RRAe   RRAn   RRNn
     5     13   2889      4      4      1      1      2

table(data$HouseStyle)

1.5Fin 1.5Unf 1Story 2.5Fin 2.5Unf 2Story SFoyer   SLvl
   314     19   1471      8     24    872     83    128

The issue that this creates is that when I train-test-split, one of the data can include classes of a categorical feature that is not included in the other dataset. For example, train data can include "RPAn" and "RPAe" from the Condition2 feature, but test set do not include them.
This result in another issue where I train a model and test, it will return an error where the model could not find the classes that it was trained on for the test set. The error looks like this.
lm_model <- lm(SalePrice ~ ., pre_train)
summary(lm_model)
lm_result <- predict(lm_model, newdata=validation)

Error in model.frame.default(Terms, newdata,
     na.action = na.action, xlev = object$xlevels) :
  factor RoofMatl has new levels Membran, Roll

What would be the right approach to tackle this problem? Are the only way to just delete the  categorical features that are extremely imbalanced(which ends up being majority of the categorical feature in the dataset)? If someone can answer this, that would be super helpful!
Comment: You should use your entire dataset for encoding, and then do your splits.  https://stackoverflow.com/a/55525814/2886575
Comment: @Him, that's the opposite of what your linked answer says!
Comment: "Whatever transformations you do to the train set should be done to the test set before you make predictions" They mean to include things such as one-hot encoding.  If you are mapping the vectors [groucho, 2], [harpo, 3] and [chico, 4] to [1,0,0,2], [0,1,0,3] and [0,0,1,4] respectively, then this is NOT a "learning" activity, it's just a "defining" activity, and you should do this to ALL of your data in the same way, both for your training, your "test" and any future predictions.
Comment: @Him I agree about it not being "learning" and therefore mostly OK, and this is discussed at greater length elsewhere (mostly the root questions are: what does your model try to do when training with constant-0 columns for test-only categories, and what happens to training-only categories?).  But your approach is the second block of code in the answer you linked, headed as "Incorrect", so your link doesn't support your suggestion.
Comment: @BenReiniger this is fair that my link does not support my assertion, but I would like to still stand by my assertion.  The reasoning that the answer I posted gives for not doing the encoding on the cat'd train/test set is that the test set contains things that weren't in training.  This assumes that prediction on the test set is the ultimate goal of the model.  This is naive, I think, unless you're doing toy activities such as Kaggle competitions - the ULTIMATE test will be real-world data, and that data may very well contain unforeseen values that you will need to deal with.
Comment: Attempting to, a priori, consider all possibilities for inputs is the correct thing to do.  Possibly this involves lumping things together as @Dimitri suggests.

This would also fit into a production workflow, which probably looks like "pick a model family, do test/train split, train on train, test on test, report these results and make decisions about model based on these results, then train the production model on ALL available data and put into production."

The test/train thing is for meta-modeling to inform you about the model family, it's not for predictive purposes.
Answer: If your intent is prediction and not inference, there are a few ways around this I suppose.
One approach is to NOT make predictions for observations which have new levels.  This is the approach common modelling libraries (like  tidymodels) will take.  It isn't a great approach in my opinion, but it is certainly one approach.
The other is to use domain knowledge to collapse some of the less prevalent groups into larger categories. Maybe it would make sense to group 2 Bed + Den units together with the 2 Bed units.  Maybe it wouldn't.  It's up to you.
Another solution would be to use a random effect rather than a fixed effect.  When new categories appear, random effects can naturally handle them, as I demonstrate below
library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from
#>   required_pkgs.model_spec parsnip
library(tidyverse)
library(lme4)
#> Loading required package: Matrix
#>
#> Attaching package: 'Matrix'
#> The following objects are masked from 'package:tidyr':
#>
#>     expand, pack, unpack

# Generate imbalanced data
set.seed(0)
N = 100
groups = sample(letters[1:3], size=N, replace = T, prob = c(0.95, 0.04, 0.01))
y = model.matrix(~groups-1) %*% c(2, 1, -1) + rnorm(N, 0, 0.24)
d = tibble(groups, y)

# Split up the data
splits = initial_split(d)
train_set = training(splits)
val_set = testing(splits)

#missing b!
train_set %>%
  count(groups)
#> # A tibble: 2 × 2
#>   groups     n
#>   <chr>  <int>
#> 1 a         73
#> 3 c          1

# has b!
val_set %>%
  count(groups)
#> # A tibble: 2 × 2
#>   groups     n
#>   <chr>  <int>
#> 1 a         25
#> 2 b          1

model = lmer(y~1 + (1|groups), data = train_set)

preds = predict(model, newdata=val_set, allow.new.levels = T)

Created on 2021-10-12 by the reprex package (v2.0.1)
Finally, if there are just one or two categories which don't appear frequently, you could just ignore them.  This approach becomes problematic however when you have many categories appearing a few times and a handful making up the majority of the observations.
All in all, my recommendation would be to use domain knowledge to lump categories together if possible.  If once you've done that and you can't lump categories together in a reasonable way, my recommendation would be to use a mixed effect model.


How to conduct meta-regression with a continuous variable measured on different scales across studies?

Question: I am conducting a meta-analysis on the efficacy of a specific type of psychotherapy for children and plan to use meta-regression to identify moderators.
The predictor I am interested in is parent psychopathology, but this variable is collected using different measures across studies. For example, Study 1 uses Questionnaire 1 (e.g., Beck Depression Inventory), Study 2 uses Questionnaire 2 (e.g., Depression Anxiety Stress Scales), and both questionnaires use different scales. What information would I need to extract and use for the predictor in the meta-regression from each study? The mean alone doesn't seem appropriate given that different measures are being used from study to study.
Thank you in advance for your input!
Answer: As a beginner in meta-analysis and here at CV, I write with caution.
So far I strongly doubt that mixing scales for a predictor in meta-regression models is valid. I understand that  different scales can be mixed if they are pooled as an effect size. E.g.: SMDs in BDI mixed with SMDs in DASS works, they can further be transformed to other ES.
However, a predictor variable is not necessarily a standardized effect size and different scales usually don't meet the same metric and item thresholds. I suspect this is the case in your baseline-pathology example.
Thinking of exceptions, I would argue that:

it's okay to mix different scales if they  can easily be expressed in a normed metric derived from the same reference population (hypothetically: mixing t-scores of BDI with t-scores of DASS from a representative parent sample).
Similarly, imagine the two scales had the same item anchors (say from 1="not at all" to 6="extremely"), same number of items, and arguably same item difficulties, plus evidence that the raw values behave in similarly.
Finally, effect size measures could predict other effect size measures, e.g.: mixed SMDs from BDI/DASS predicting 1-year relapse proportions.

I'll let you know if someone convinces me of the contrary.
Cheers


Visitor pattern, not all visitors have a reasonable implementations for all methods in the interface

Question: I have an interface Serializable. This class have methods for each Item subclass in my application. However not all classes that implements Serializable have a reasonable implementation of the serialize method. Some of the serializers should not be able to serialize all the different objects because of security constraints in my application.
How should I solve this? Should I serialize a message that says "Serializer can't serialize object because of security constraints." or should I throw an RuntimeException? Or are there other ways of "fixing" this?
Answer: You would violate one important principle with your logic: Interface Segregation Principle

The Interface Segregation Principle states that clients should not be
  forced to implement interfaces they don't use. Instead of one fat
  interface many small interfaces are preferred based on groups of
  methods, each one serving one submodule.

So, I would create two distinct base classes to achieve your requirement:

Item
SerializableItem

And your visitor would only rely on SerializableItem objects:
public void visit(SerializableItem sItem)

Of course, it is possible to gather only common behaviour (unrelated to serialization so) of both classes within a kind of AbstractItem, as long as the visitor doesn't deal with it.
Comment: @Laidback Andy "where you have two serializers that inherit from the same serialize interface". What I promote is to avoid the common inheritance. Instead, create two interfaces like `FullySerializable` and `PartiallySerializable`. `FullySerializable`interface EXTENDS `PartiallySerializable`. So you will end up with a `PartiallySerializable` declaring the common `method1` and `FullySerializable` adding `method2`. Likewize, you will have two types of `Item`: Item1 implementing the first interface (since partially serializable) and Item2 (since fully serializable) implementing the other.
Comment: But the problem is not that the items being forced to implement the Serializable interface. The problem is that some of the Visitor's (Serializers) does not have any reasonable way to serialize it.
Comment: @Laidback Andy Hence the fact to let visitor deal with `SerializableItem` because solely this type is supposed to implement a complete implementation of serialization. Your problem would thus disappear since `Item` objects (not supposing to correspond to a full serialization way) aren't used by visitor. For better understanding and to avoid confusion, replace `SerializableItem`by `ItemSubjectToAFullSerialization`
Comment: Sorry, but I do not understand how it solves what I am asking. Could you provide an example where you have two serializers that inherit from the same serialize interface. Let say the interface for being serializable have two methods, but only the first serializer is able to provide a decent implementation for both. The other is only able to provide an implementation for the first one.
Comment: Just one thing; Does it seem right that a `AdminJsonSerializer` inherits from `MobileJsonSerializer`. The mobile JSON serializer is more restrictive with the fields it serialize. Should they be two separate interfaces instead? And have distinctive method headers? Such as `toAdminJson(Item item)` and `toMobileJson(Item item)`.
Comment: @Laidback Andy: If there isn't a strict IS-A relationship, prefer creating two distinct hierarchies of interfaces. In your case, `AdminJsonSerializer` can be seen as an extension of `MobileJsonSerializer`since less restrictive, so extending it makes sense.
Answer: Throwing UnsupportedOperationException("Non serializable") is a first guess. Of course it would be awesome to discover that at compile time, but Visitor pattern was not designed for that.
Answer: A class that won't provide a reasonable implementation for Serializable shouldn't implement it. Anyway the question is quite unclear. Does the interface Serializable declare the accept() method in your setup? It shouldn't. If it is possible that unserializable items are a special subclass of item you could just not implement the visit(MyNotSerializableItem) in the Visitor.
Comment: It is some of the serializers that may not have a reasonable implementation.


Possible to use tags/buttons as exposed filters?

Question: I have a view that will be a grid of a dozen content types. I would like to have a tag-like button or text link above the view of content to allow the user to toggle / filter between what content type they wish to see. I know that exposed filters allows you to use a dropdown or auto-complete, but is this possible with using tags as buttons?
Answer: It sounds like you want to have something like this, what is provided by the Facet API module (Drupal 7) or Facets For Drupal 8.

Image copied from: https://www.webwash.net/drupal/tutorials/intro-search-api-part-2-faceted-search (explanation of Facet API combined with Search API).  It should however also work with the Drupal core search system. (I only used it with Search API)

The Facet API module allows site builders to easily create and manage
  faceted search interfaces. In addition to the UI components that come
  out of the box, themers and module developers can build their own
  widgets that can optionally be contributed back to Drupal.org. Facet
  API works with the core Search, Search API, and Apache Solr Search
  Integration modules (including Acquia Search) meaning that code and
  configuration can be reused as-is with the most popular search
  solutions available to Drupal. Refer to the documentation for more
  details.

Each facet will be exposed as a block, that you can place anywhere in your site from the block administration page.
Comment: Great, this is just what I'm looking for
Comment: Here is another good tutorial for setting up the Search API with Facets http://envisioninteractive.com/drupal/drupal-7-views-with-faceted-filters-without-apachesolr/
Answer: If you want an easier option than setting up the Search API, you could just use Views Better Exposed Filters. https://www.drupal.org/project/better_exposed_filters
Comment: The facet API module claims to support core search. So setting up search API is not required.
Comment: @Neograph734, you're right but currently its still under development for supporting the core search in D8. https://www.drupal.org/node/2877989#comment-12137409


Allow to change site logo without giving too many privileges

Question: I want to allow a certain role to change the site logo, but I don't want to give the 'administer site configuration' permission to that role.
At first I was hiding some form elements from the theme settings forms using hook_form_alter() and $element['#access'] = FALSE, leaving only the options related to uploading a new site logo; after that I noticed the user would need the 'administer site configuration' permission to access that form.
Do you have any idea?
Answer: You could implement hook_menu_alter() to change the permission associated with that page, and use other permissions to decide which form fields should be visible to the currently logged in user. In this case, instead of using $element['#access'] = FALSE, you should use $element['#access'] = user_access('your custom module permission').
In the case you are only interested to allow some users to change the site logo, then you can use your custom permission for the page containing that settings form, and then use the "administer site configuration" permission for the form fields only the site administrators should see ($element['#access'] = user_access('administer site configuration')).
function mymodule_menu_alter(&$items) {
  if (isset($items[$menu_path]) {
    // Use the following line if the access callback already used is not user_access().
    $items[$menu_path]['access callback'] = 'user_access';
    $items[$menu_path]['access arguments'] = array('your custom module permission');
  }
}

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == $form_to_alter) {
    $form[$element_id]['#access'] = user_access('administer site configuration');
  }
}

Comment: Thanks a lot. I finally changed 'access callback' instead of 'access arguments' because the theme settings form had a callback to function _system_themes_access().
Comment: You can change the access callback to `'user_access'`, and then change the access arguments. If the access callback is going to use `user_access()` for just a permission, it is better to change the access callback.
Comment: That was not the case: `return user_access('administer site configuration') && ($theme->status || $theme->name == variable_get('admin_theme', '0'));`


Laravel 4.2 throwing MethodNotAllowedHttpException

Question: I have this code which worked in previous versions, now doesn't work anymore. I'm doing simple user authourization like this:
Form:
 {{ Form::open(array(
        'url'       => 'login',
        'method'    => 'PUT',
        'class'     => 'pure-form pure-form-stacked'
  )) }}

                    {{ $errors->first('email') }}
                    {{ $errors->first('password') }}

                    {{ Form::text('email', Input::old('email'), array('placeholder' => 'user')) }}
                    {{ Form::password('password', array('placeholder' => 'password')) }}

                    {{ Form::submit('Log in', array('class'=>'button-primary')) }}

   {{ Form::close() }}

Routes:
Route::get('login', array('https', function(){
return View::make('back-end/login');
}));

Route::post('login', array('https', function(){
$userdata = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
);

if(Auth::attempt($userdata)){
        return Redirect::to('dashboard');
    } else {
        return Redirect::to('login');
    }
}));

Route::group(['before' => 'auth', 'prefix' => 'dashboard'], function(){
    Route::get('/', 'DashboardController@getDashboard');
    Route::resource('blog', 'BlogController');
    Route::get('logout', [
           'as'   => 'logout',
           'uses' => 'UserController@getLogout'
        ]);
});

Login page will load, but when i submit form i get in output MethodNotAllowedHttpException.
Comment: change the method from `put` to `post`.
Answer: Your login route is a POST, but your form is using PUT. Switch 'method'    => 'PUT', to 'method'    => 'POST', in your Form::open call (as you shouldn't be using PUT anyways there) and it should work.
Comment: What does `php artisan routes` output?
Comment: Try showing us the output of `php artisan routes` - there may be something subtle there which other eyes might be able to pick up.
Comment: I did that, but then i get "404 Not Found".
Comment: That all routes exist, which doesn't make sense, if credentials are wrong it should reload login page instead of throwing not found error.


How to find the key from chords

Question: This is a danish song, but that shoud not matter. My problem is and has always been, how to figure out the key when presented to chords like the following:
D D#- Em A7
Tyggegummikongen Bobbel
D D#- Em A7
sidder i sin gummipoppel
DG
synger tyggegummisange
A7 D
for en tyggegummislange

I have tried to find the key from this looking for scales containing the chords from the song. Is this the right aproch?
Answer: Short answer: it's in D!
Long answer: Your approach of taking a chord/pitch inventory and comparing it to keys/scales is one possible way. The problem with this route, however, is that chromatic pitches will confuse the system and possibly results in an incorrect final answer.
For music like this, the best method is just to recognize what chord sounds like a stable point of repose. In this case, everytime the D chord appears (especially at the end) it sounds like it could be the end of the song; it doesn't need another subsequent chord to reach a completion.
However, the D# chords in the first two lines are not stable at all; they need to resolve to the subsequent E chord, so D# is a terrible candidate for the key of this piece.
Otherwise, you'll notice that the progression A7--D happens three times throughout the piece. Look for this pattern of a "dominant seventh" chord resolving down a perfect fifth to a triad. In most cases, this will be a V7--I motion and the latter chord will be tonic. (But note that this progression is becoming more and more rare in popular music, so this method doesn't always work either!)

How to find the key from chords


PHP prevent including same file twice

Question: Hi I have following files:
login.php, index.php, header.php, footer.php, restrictaccess.php
restrictaccess.php has following code:
<?php
session_start();
if (!(isset($_SESSION['user']) && $_SESSION['user'] != ''))
if (!(isset($_SESSION['access']) && $_SESSION['access'] != ''))
{
header ("Location: login.php");
exit();
}

if($_SESSION['access']=="2" ) {
     echo 'You are logged on as Manager';

}
if($_SESSION['access']=="1" ) {
     echo 'You are logged on as Restricted user';
}
?>

restrictaccess.php, header.php and footer.php is included on every page accept login.php.
Both header and footer also includes restrictaccess.php; will this cause any problem ? Is there anyway so that such includes will only be loaded once ?
Currently when someone logon the message "You are loged on as Manager/Restricted user" is displayed thrice.
Your help will be much appreciated, thank you.
Comment: Use [`include_once`](http://php.net/manual/en/function.include-once.php)
Comment: Try using `include_once`
Answer: Try using require_once() to include your file. This method checks wether the file was already included.
PHP manual
Answer: UGLY method
Use require_once or include_once for restrictaccess.php
Better method
Change restrictaccess.php to only:
<?php
if (!(isset($_SESSION['user']) && $_SESSION['user'] != ''))
if (!(isset($_SESSION['access']) && $_SESSION['access'] != ''))
{
  header ("Location: login.php");
  exit();
}
?>

Then use session_start() at the top of every client page (the pages that user can browse to like index.php).
Use the code about "You are logged-in as ..." only in the header.php (or wherever it make sense to display.
UPDATE
You can include the above restrictaccess.php anywhere in you files BUT you have to make sure that there is no data sent to the client prior to the header ("Location: login.php"); bit (i.e. if you have any echo, print_r or var_dump... before the header ("Location: login.php"); then PHP will generate an error)
Comment: along with session_start() ; do I also include restrictaccess.php on all the pages ?
Comment: Almost (see my edit above), you can add it to any page you want to guarantee an authorized access. If you keep it as in my example, it is safe to include multiple time because no function/class is declared, and the only outcome is a redirection.

PHP prevent including same file twice


How to run OSSEC over TCP

Question: I've got ossec working fine with several clients/agents with the default UDP:1514. However, after adding tcp to the server's ossec.conf file, removing and re-adding the agents, and restarting ossec on all machines, the agent logs show they are unable to contact the server, presumably because they are trying to do so over UDP.
While the server doc for ossec.conf shows the protocol option where you can spec TCP or UDP, there is nothing like this in the agent doc for ossec.conf, so my question is how does one configure the agents to also use TCP?
Answer: I found by searching the ossec mailing list that this is not possible.

How to run OSSEC over TCP


Any derivation map is a linear combination of directional derivatives

Question:
Fix a point $\mathbf p\in \mathbb R^n$. Given $\mathbf v\in \mathbb R^n$, define the directional derivative $\nabla_{\mathbf v}$ of a function $f$ at the point $\mathbf p$ as
$$\nabla_{\mathbf v}f(\mathbf p)=\lim_{t\to 0}\frac{f(\mathbf p+t\mathbf v)-f(\mathbf p)}t$$
Note that this is a map $\nabla_{\mathbf v}:C^\infty(\mathbb R^n)\to C^\infty(\mathbb R^n)$ where $C^\infty(\mathbb R^n)$ is the set of all real valued smooth functions on $\mathbb R^n$.
Now, let us have a map $D:C^\infty(\mathbb R^n)\to C^\infty(\mathbb R^n)$ that satisfies the following properties-

$D(f+g)(\mathbf p)=D(f)(\mathbf p)+D(g)(\mathbf p)$
$D(cf)(\mathbf p)=cD(f)(\mathbf p)$
$D(fg)(\mathbf p)=fD(g)(\mathbf p)+gD(f)(\mathbf p)$

Such a map is called a derivation at $\mathbf p$. Prove that any such derivation maps can be written as a linear combination of $\{\nabla_{\mathbf e_i}\}_{i=1}^n$.

I have proved that the directional derivative map $\nabla_{\mathbf v}$ satisfies properties (1),(2) and (3). Using that, we can also very easily show that any linear combination of $\{\nabla_{\mathbf e_i}\}_{i=1}^n$ also satisfies properties (1),(2) and (3). As a part of a previous exercise (which may be of use in this part), I have proved
$$\nabla_{\mathbf {v}+\mathbf{w}}=\nabla_{\mathbf v}\\\nabla_{c\mathbf v}=c\nabla_{\mathbf v}$$
But, I have no idea how to show that any derivation is a linear combination of directional derivatives.
Comment: Suppose $v=v_1e_1+v_2e_2+\dots +v_ne_n$, then as you have shown $\nabla_v=v_1\nabla_{e_1}+v_2\nabla_{e_2}+\dots + v_n \nabla_{e_n}$. How can you get the coordinates $v_i$ using only $\nabla_v$, and the coordinate functions $x_i$?
Answer: Let $f\in C^\infty(\mathbf R^n)$, $p \in \mathbf R^n$, $D$ a derivation at $p$. By Taylor, we can write
$$ f(x) = f(p) + \sum_{i = 1}^n h_i(x) (x_i - p_i) $$
with $h_i \in C^\infty(\mathbf R^n)$ such that $h_i(p) = \nabla_{e_i}f(p)$. Applying $D$, we have, writing $\pi_i\colon \mathbf R^n \to \mathbf R$, $x \mapsto x_i$ for the projections:
\begin{align*}
  Df &= D(f(p)) + \sum_{i = 1}^n D\bigl(h_i(\pi_i - p_i)\bigr)\\
     &= \sum_{i = 1}^n \bigl(h_i(p) D(\pi_i) + Dh_i (\pi_i - p_i)(p)\bigr)\\
     &= \sum_{i = 1}^n D(\pi_i) \cdot \nabla_{e_i}f(p)
\end{align*}
Hence,
$$ D = \sum_{i = 1}^n D(\pi_i) \nabla_{e_i} f(p) $$


Prove that the number of primes that divide $B=\left \{ \sum_{i=1}^n b_ia_i^n : n\in \mathbb{N} \right\}$ is infinite

Question: I got this interesting question which says

Let $n\geq 2$ and let $a_1,a_2,\dots ,a_n$ be positive integers (not
necessarily distinct) such that $(a_1,a_2,\dots ,a_n)=1$ (where $()$
represents G.C.D.) and not all $a_i$'s are $1$.
Let $b_1,b_2,\dots ,b_n$ be integers (not necessarily distinct).
Consider the set \begin{equation*} B=\left \{ \sum_{i=1}^n b_ia_i^n :
 n\in \mathbb{N} \right\} \end{equation*} Now, let \begin{equation*}
 P=\{p: p \text{ is a prime such that }p|b \text{ for some } b\in B\}
\end{equation*} Show that the set $P$ is infinite.

I can intuitively understand that if we consider that only a finite number of primes divide
\begin{equation*}
B_{n_0}=\left \{ \sum_{i=1}^n b_ia_i^n : n\in \{1,2,\dots ,n_0\} \right\}
\end{equation*}
then because the $a_i$'s are coprime, chances are high that $B_{n_0+1}$ will introduce a new prime factor. This approach looks more concrete if we look at a special case in which all the $b_i$'s are equal.
But, this is just a vague idea. Is it possible to proceed with this (or with any other idea)?
Comment: @JohnOmielan as far as I understand your approach, you are using specific values of $a_i$ and $b_i$, but you need to prove what is asked for all choices of $a_i$ and $b_i$. Did I get you wrong? If yes, please elaborate your approach a little.
Comment: You're welcome for the welcome :-) . Regarding your question, I believe it's somewhat poorly worded. Part of what confused me is the use of $n$ initially as an apparently fixed value and then again in defining the set $B$ as the power of $a_i$. I believe that second usage was meant to be a different variable. Your question would be clearer and make more sense to me, plus at least to me it'll then be unambiguous as well, if you use something like $B = \left\{\sum_{i=1}^n b_ia_i^m : m \in \mathbb{N} \right\}$ instead.
Comment: @JohnOmielan :) That was a nice suggestion. I did the edit. Is that okay now?
Comment: Thanks for clarifying this, except you used $m$ as the upper limit for the summation instead of $n$. Note there are always $n$ values of $a_i$, so as my comment showed, that should remain the same. It's incorrect to use $m$ as the upper limit, especially if $m > n$, since then it doesn't make sense for any $a_i$ where $i \gt n$.
Comment: @JohnOmielan The idea of the question, as far as I ubderstand it, is that $n$ is not really fixed. The question seems to ask you to prove that no matter what the choice of $a_i$'s and $b_i$'s (subject to the given conditions), as $n$ goes to infinity, i.e., as more and more $a_i$'s and $b_i$'s are taken into consideration, newer primes start dividing the elements of $B$. Now, I feel like it was better with the $n$ instead of the $m$ :(
Comment: Yes, you're right in a way that $n$ is not fixed. However, the idea seems to be that for some $n \ge 2$, whatever it might be (i.e., it's not fixed over all cases, but just for each particular case), you take those $n$ elements of $a_i$ and $b_i$ to create a set of $B$, where each value is the sum of those $n$ elements with each $a_i$ to the $m$'th power. The problem with the original problem statement is that $n$ is initially fixed (you can't defined a variable length set of elements), but then in $B$ it's defined to be variable, i.e., $n \in \mathbb{N}$. This doesn't make sense to me.
Comment: But then why do you think the upper limit of the sum should not be $m$?
Comment: As I mentioned in my earlier comment, having $m$ as the upper limit doesn't make sense since, with $m \in \mathbb{N}$, there is no upper limit for it.  Thus, for any particular case for a specific $n$, when $m \gt n$, then $a_i$ for $i \gt n$ is not defined. I've tried to explain what my issues are & how to address this as best as I can. Unfortunately, I'm afraid I don't have the time or interest in trying to explain this to you any further, so please decide for yourself what you think the question actually means, how to best present it and then edit your question text accordingly.
Answer: For a fixed $n \geq 2$, the numbers $a_i^n, 1 \leq i \leq n$ are coprime (as $a_i$s are coprime ). Hence $B=\mathbb{Z}$ which has infinitely many primes.
Answer: At first note that $B$ is a collection of integers $b$ of the form $b=p_1^{i_1}p_2^{i_2} \cdots p_m^{i_m}$ because of unique prime factorizations of each $b \in B$, where $p_j,
 j=1,2,..., m$ are primes and $i_1, i_2, ..., i_m$ are positive integers.
So each $n \in \mathbb{N}$ corresponds to an element $b=p_1^{i_1}p_2^{i_2} \cdots p_n^{i_n} \in B$, say, depending on that fixed $n$. All these primes $p_1,p_2, ...,p_n \in P$ because $p_i ~|~b$ for all $i=1,2,...,n$. Since $\mathbb{N}$ being countably infinite, $P$ must be countably infinite at least.
Comment: I was not the one who downvoted, but I don't understand your approach. As far as I understand, if you go on adding elements, it may not add a new prime factor in all cases, e.g., suppose you have $\{3,5\}$ and you add $2$ to the set, then no new prime factor is introduced in $3+5=8=2^3$. Also, all infinite sets may not have infinite prime factors, e.g., consider $\{2^n:n\in \mathbb{N}\}$ which has only one prime factor, $2$. Also, in your question, there's no mention of any $a_i$. Can you please elaborate a little?
Comment: Can someone point-out the reason of downvoting ?
Comment: @SayanDutta, yes, that is a good point. I think, if we can show that $B$ contains infinitely many elements of $\mathbb{Z}$, then we are done. Let me think whether I can get it
Comment: @SayanDutta, fix $n=2$, then the general element in $B$ is $b=b_1a_1^2+b_2a_2^2$. Take $a_1=1$ and $a_2=2$ so that $(a_1,a_2)=1$. Also $b_1, b_2 \in \mathbb{Z}$. Take $b_2=0$, then we get $b=b_1$, where $b_1$ can be any integers. This says that $B \supset \mathbb{Z}$. Since the primes of $\mathbb{Z}$ has infinitely many primes, $P$ is infinite looking at my answer
Comment: that was a nice idea, but isn't it a little too strong? I mean, will that really be correct? For a given choice of $a_i$'s and $b_i$'s, will $B$ contain all elements of $\mathbb{Z}$? Of course, if I give you any $x\in \mathbb{Z}$, you can find $a_i$'s and $b_i$'s in $\mathbb{Z}$ such that $x$ can be written as the required sum. But, will that hold for **all possible choices** of $a_i$'s and $b_i$'s? I don't think so.
Comment: I don't think the question asks you to choose specific $a_i$'s and $b_i$'s such that the property holds. I think it asks you prove that no matter what their choices (that is given any choice of $a_i$'s and $b_i$'s subject to the given conditions), as $n$ goes to infinity, more and more prime factors start dividing elements of $B$.
Comment: @SayanDutta, i understand what you are saying. But is there anymore restrictions in the original question ? I mean, is that all you mentioned in the question ?
Comment: The question I posted is all that I got. And considering the source from which I got it, chances are very slim that the question missed something.


What does the less than symbol in front of LVM volume size mean?

Question: Input
lvs

Output
LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
home mainvg -wi-ao----  10.00g
root mainvg -wi-ao---- <40.00g
swap swapvg -wi-ao----   4.00g

What does the less than symbol (<) in the root volume size mean?
Answer: From man lvs:

--units r|R|h|H|b|B|s|S|k|K|m|M|g|G|t|T|p|P|e|E
                All sizes are output in these units: human-(r)eadable with '<' rounding indicator
Comment: Thank you.  I guess I need to remember to use the system man pages since [the one I looked at](https://linux.die.net/man/8/lvs) didn't match my system.


Operator using Fourier transform

Question: I have the following question:
Let $\epsilon >0$. We define the operator $M:= (1-\epsilon \Delta)^{-\frac 12}$, where  $\Delta$ refers to the seconde derivative.
Let $u \in H^2(\mathbb{T})$ (the Sobolev space of periodic functions
having $H^2$ regularity). We ask to prove the existence of  $v \in L^2(\mathbb{T})$ such that  $$Mu = u + \epsilon v$$ et $\|v\|_{L^2}= \mathcal{O}(1)$
In the case where the domain of definition is $\mathbb{R}$, we can give  $Mu$ the following sense:
$$Mu := \mathcal{F}^{-1}\big((1-\epsilon \xi^2)^{-\frac 12}\hat{u}\big).$$ But in our case, could we define $Mu$ in the sense of Fourier series? Namely,
$$Mu := \mathcal{F}^{-1}\big((1-\epsilon n^2)^{-\frac 12}\hat{u}(n)\big).$$
Thank you.
Comment: the Fourier series can be viewed as the discretization in the frequency domain of the function we would have obtained by  restricting the function to one period and taking its Fourier transform. so the answer is yes, we can define the operator as a multiplication of the Fourier series coefficients. and I  didn't get what is $v$
Comment: In fact, as in the Taylor's series, we write formally
$$(1-\epsilon \Delta)^{-\frac 12}= 1+ \frac 12 \epsilon \Delta + R(\epsilon, \Delta).$$
 I just want to prove that we can do this development on $Mu$.
Comment: so the result of Fourier inversion should be $L^2(\mathbb{T})$ when $\hat{u}(n) = \mathcal{O}(n^{-1-\epsilon})$
Comment: Yes. That's why we assume that $u \in H^2(\mathbb{T})$
Comment: and to estimate to norm of the residual $v$ I would go again to the Fourier domain, as $H^2(\mathbb{T})$ has a simple meaning there.
Comment: do you agree that if $\mathcal{F}^{-1}((1-\epsilon n^2)^{-1/2} \hat{u}(n))$ exists then it has to be $Mu$ ? and you know it exists and $\in L^2(\mathbb{T})$ because you supposed that $u \in H^2(\mathbb{T})$, CQFD ?
Comment: Well I just want to be sure.
Answer: Spectral Theory for selfadjoint operators gives you a way to view functions of a selfadjoint operator $A$ as multiplication operators on the spectrum. For example,
$$
           F(-\Delta)f = \mathcal{F}^{-1}\{F(-s^2)\mathcal{F}\{f\}\}.
$$
Specifically,
$$
        (I-\epsilon\Delta)^{-1}f=\mathcal{F}^{-1}\{(1+\epsilon s^2)^{-1}\mathcal{F}\{f\}\}.
$$
The reason for $-s^2$ instead of $s$ is that the Fourier transform is built for the operator $\frac{1}{i}\frac{d}{dt}$ so that $s^2$ is associated with $\frac{1}{i^2}\frac{d^2}{dt^2}$.
When you're working on the unit circle (or, equivalently, periodic functions on $[-\pi,\pi]$,) the spectrum is discrete. When you treat the spectrum $\sigma(\frac{1}{i}\frac{d}{d\theta})=\mathbb{Z}$ as a discrete space with counting measure, then $\frac{1}{i}\frac{d}{d\theta}$ is transformed to multiplication by $s$ on $\ell^2(\mathbb{Z})$.
The continuous Fourier transform confuses things because the underlying space and the spectral space are the same, and the spectral density measure is uniform. The discrete case better represents what is going on. The space is continuous, but the spectrum is discrete. The spectral density measure remains uniform, but that's to be expected for differentiation.  Other operators may require non-uniform spectral density measures.
Comment: @A.MONET : This is the multiplication form of the Spectral Theorem for selfadjoint linear operators on a Hilbert Space. Here's a PDF with a full-blown proof: http from Michael Taylor's website: //www.unc.edu/math/Faculty/met/specthm.pdf . Michael Taylor is a Professor of Mathematics at UNC. The first part of page 6 gives the first version of such a theorem, and follows up with the full version (with proof.) I'm afraid this is not a simple theorem for the general case.
Comment: Thank you very much. Would you please add a link fo a book where I could find more details about that ?
Comment: Thank you for the link. I'll check it. It seems to be "not difficult"

Operator using Fourier transform


Solving the recurrence with $a_0 = 0$ and $a_n = \left(\frac{n-2}{n}\right)a_{n-1} + 2$

Question: Can anyone here help me to solve this recurrence relation?

$$a_0 = 0,\quad a_n = \left(\frac{n-2}{n}\right)a_{n-1} + 2, \quad n > 0$$

I've tried lots of ways to solve it, unsuccessfully.
Thanks in advance.
Comment: Ways like what?
Comment: Have you tried calculating it in a spreadsheet?  That will give you a hypothesis that you can try to validate.
Comment: I've tried to expand just the fraction $\frac{n-2}{n}$ and then I devided all terms by the result ($\frac{2}{n(n-1)}$)... however I got a loop..
Answer: Hint: For $n>1$, we have
$$a_n=\frac{(n-1)(n-2)}{n(n-1)}a_{n-1}+2,$$
$$n(n-1)a_n=(n-1)(n-2)a_{n-1}+2n(n-1),$$
let $b_n=n(n-1)a_n$ and it is not hard to solve $b_n$ then get $a_n$.


changing the adress of the pointer through another pointer

Question: Heloo GUYS ..I am passing an alias to pointer in function and in that function i am assigning pointer to another variable .This is changing the address of the pointer passed in main funtion .i'll show u example of my code .This gives the output 40.
#include <iostream>
using namespace std;
void foo(int* &p){
    int z=40;
    p=&z;
}
int main(){
    int x=10;
    int *p=&x;
    foo(p);
    cout<<*p;
}

But when I try to do this all in one main function then the address of the the pointer doesnt change so as the ouput..this is my code
#include<iostream>
using namespace std;
int main()
    {
       int a=1, b=2, c=3;
       int *p, *q;
       p=&a;
       q=*&p;
       p=&c;
       cout<<*q<<endl;

    }

it gives the ouput 1 rather then the 3.THANKs..
Comment: Your first example has undefined behaviour.
Comment: `q=*&p` is the same as `q = p;`
Comment: That's because you aren't changing `q`.  You are changing `p`.
Comment: c++ doesn't time travel. just because you do `p=&c` doesn't change the fact that `q` ended up pointing at `a`'s value in the previous line.
Comment: If you want to do "the same", it would look more like this: `int* p = &a int*& q = p; q = &c cout << *p;`.
Answer: void foo(int* &p){
    int z=40;
    p=&z;
}

Here pis a reference to a pointer. Which means that when you modify it you will modify the referred pointer too.
When you call foo with
int main(){
    int x=10;
    int *p=&x;
    foo(p);
    cout<<*p;
}

You're saying that the reference named pin the function foo refers to the pointer named pin the function mainso whatever you do to p in foowill affect p in main.
#include<iostream>
using namespace std;
int main()
    {
       int a=1, b=2, c=3;
       int *p, *q;
       p=&a;
       q=*&p;
       p=&c;
       cout<<*q<<endl;

    }

Here you have no reference anywhere, you just have a pointer that you copy to another pointer. q=*&pis equivalent to q=p.
The key difference to note here is that the & operator in the first exemple means "reference" while the & operator in the second example means "address of".
If you want the closest equivalent to the first sample with the second one, try this:
#include<iostream>
using namespace std;
int main()
{
  int a=1, b=2, c=3;
  int *p, *&q = p;
  p=&a;
  q=*&p;
  p=&c;
  cout<<*q<<endl;

}

And in this last case you have both an & which means "reference" in int *p, *&q = p; and an & that means "address of" in q=*&p;
Answer: void foo(int* &p){
    int z=40;
    p=&z;
}

The above code is not safe because whoever called foo now has a pointer to a destroyed stack variable.

it gives the ouput 1 rather then the 3.THANKs..

Of course it does. Learn what a pointer is and how it works.
int a=1, b=2, c=3, *p, **q;
p=&a;
q=&p;
p=&c;
std::cout << **q << std::endl;

Answer: just thought you should know that in foo() you're assigning the pointer to the address of a variable with automatic storage duration, which means that what it points to when foo returns is actually undefined (and in a larger program this is a disaster waiting to happen).
In your second example you only assign q once. During an assignment, a reference is the object it's referring to. The reference dependency is not carried forward.
Answer: That's because you aren't changing q.  You are changing p.  When you do
q=*&p;

it is the same as
q = p;

So what you have done is assigned p to point to a.  Then you assign the value of p to q so now q points to a.  Then you assign the address of c to p.  Doing that does nothing to q and as such q still points to a.
You should also read Can a local variable's memory be accessed outside its scope? to see why your first example has undefined behavior.
Answer: Why do you expect to get the answer as 3?

q points to p then you changed where p points. p points to c but q will still point to where previous address of p.
unless you reassign q to point to p, it will still point to a.
Answer: Let me only consider your second example and I added a few comments and hopefully it will clarify why you are getting that output.
   int a=1, b=2, c=3;
   int *p, *q;
   p=&a; //P now points to the variable a
   q=*&p; // q now points to the value in pointer p
   p=&c;
   cout<<*q<<endl; //You are printing  value from pointer q

Please note you are not changing the address of a pointer, instead you are changing only the address  where  pointer points to.


Drupal 7 force active menu item

Question: I got a page where I link to in the menu bar. On this page there is a custom module. On this page there are items from anothor database where the user can click on. When they click on it they go to another page with more information. When they are on the second page there is no active button in the menu bar. On the second page I also have a custom module.
How can u force a button in the menu bar to have the .active class or in another way so that it haves the active lay-out button.
I use Drupal 7.
Comment: Have a look at the context module. It deals with setting active menu items based on conditions.
Answer: The Menu position module will solve your problem.

Often times site builders want certain types of content to appear in a specific position in the navigational menu. The simplest solution, adding all of that content individually to the menu system, has performance and usability issues. (Imagine hundreds of menu items added to one spot in the menu.)
This module allows for the creation of rules that will dynamically add the current page into the menu system at the requested spots.
Answer: You can use http://drupal.org/project/context to set an active menu.

Context allows you to manage contextual conditions and reactions for
  different portions of your site.

Or use the API http://api.drupal.org/api/drupal/includes!menu.inc/function/menu_tree_set_path/7

Drupal 7 force active menu item


Simulating a Coin toss

Question: I am trying to simulate a coin toss using R. For that I used both sample function and rbinom function.
But I am getting different results.
> set.seed(1)
> rbinom(10,1,0.5)
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> sample(c(0,1), 10, replace = TRUE)
 [1] 0 1 0 0 1 0 0 0 1 1

What may be the reason for that?
I thought for the same seed value, both sample function and rbinom should give the same result.
Comment: You have used the same seed, but the two procedures `rbinom` and `sample` use pseudorandom numbers in different ways.
Comment: You should mention your version of R because the default algorithm for `sample` has changed over time (IIRC most recently at 3.6.0).
Comment: A specific seed only reliably gives the same results when you call the exact same random functions in the exact same order (basically when you rerun the same code multiple times). Just because 2 functions output the same distribution of values for some sets of inputs doesn't mean what's happening behind the scenes is the same. This could even apply to functions that use the exact same high-level algorithm, since they could either use different lower-level random functions or the order they get their random values in might just not be the same.
Comment: Would this answer your question? [R Difference between rbinom and sample](https://stats.stackexchange.com/questions/339459/r-difference-between-rbinom-and-sample)
Comment: Related: https://stackoverflow.com/questions/64965398/when-and-why-are-random-number-generators-not-sampled-for-certain-functions-and/64967064#64967064
Answer: Somewhat related example: One way to generate 10 tosses of a coin
with probability $0.4$ of heads is to use rbinom:
set.seed(123); rbinom(10, 1, .4)
[1] 0 1 0 1 1 0 0 1 0 0

Another way is to use the binomial inverse CDF (quantile) function) qbinom to transform uniform
random numbers from runif get the desired
Bernoulli distribution.
set.seed(123); qbinom(runif(10), 1, .4)
[1] 0 1 0 1 1 0 0 1 0 0

This suggests that R uses qbinom with runif to
get rbinom--in this instance.
However, for success probabilities greater than $0.5,$
it seems that R uses a variant of this method, and results starting with the same seed differ.
set.seed(123);  rbinom(10, 1, .6)
 [1] 1 0 1 0 0 1 1 0 1 1
set.seed(123);  qbinom(runif(10), 1, .6)
 [1] 0 1 1 1 1 0 1 1 1 1

If you use the same seed and you access pseudorandom numbers (as with runif) in exactly the same order
to do exactly the same procedure, you will get the same
result.
Another example: Means of 100 samples of size 10 from
the population $\mathsf{Norm}(\mu = 50, \sigma = 7).$
set.seed(1234)
a = replicate(100, mean(rnorm(10, 50, 7)))
summary(a)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  44.64   48.41   49.69   49.81   51.37   55.41

set.seed(1234)
x = rnorm(1000, 50, 7)
MAT = matrix(x, byrow = T, nrow=100)
a = rowMeans(MAT)
summary(a)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  44.64   48.41   49.69   49.81   51.37   55.41

However, if we omit byrow=T in making the matrix,
R will use its default, which is to fill the matrix by columns, and we will get a different summary (except, of course, for the mean of means).
set.seed(1234)
x = rnorm(1000, 50, 7)
MAT = matrix(x, nrow=100)
a = rowMeans(MAT)
summary(a)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  42.80   48.47   49.80   49.81   51.20   55.27

Comment: For p > 0.5, the random numbers are drawn according to `size - rbinom(n, size, 1-p)` in case anyone wonders.
Answer: An historical note about sample is that it got recently modified for being biased in some extreme situations (as commented by Chris Haug). In earlier versions of R such as 3.4.4, still running on my ownlaptop, the outcome of the above would be the same as a cdf inversion (and as the complement of the standard Uniform draw):
> set.seed(1)
> rbinom(10,1,0.5)
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> sample(c(0,1), 10, replace = TRUE)
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> qbinom(runif(10),1,0.5) #inverse cdf
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> 1*(runif(10)>0.5) #complement!
 [1] 0 0 1 1 0 1 1 1 1 0

When checking the C code behind the R function rbinom, the adopted approach relies on a single Uniform when $np<30$:
/* inverse cdf logic for mean less than 30 */
repeat {
 ix = 0;
 f = qn;
 u = unif_rand();
 repeat {
 if (u < f)
     goto finis;
 if (ix > 110)
     break;
 u -= f;
 ix++;
 f *= (g / ix - r);

and a much more involved resolution otherwise ($np\ge 30$), resolution including an accept-reject step,
/*------- np = n*p >= 30 : ----- */
repeat {
  u = unif_rand() * p4;
  v = unif_rand();

hence a random number of Uniforms. On the other hand, sample (in a version of 2017!) uses a cascade of C functions:
if (replace) {
    int i, nc = 0;
    for (i = 0; i < n; i++) if(n * p[i] > 0.1) nc++;
    if (nc > 200)
    walker_ProbSampleReplace(n, p, INTEGER(x), k, INTEGER(y));
    else
    ProbSampleReplace(n, p, INTEGER(x), k, INTEGER(y));
} else
    ProbSampleNoReplace(n, p, INTEGER(x), k, INTEGER(y));

For instance, ProbSampleReplace is based on a single Uniform call:
/* compute the sample */
for (i = 0; i < nans; i++) {
  rU = unif_rand();
  for (j = 0; j < nm1; j++) {
      if (rU <= p[j])

and the other ones as well, which is not to say that they return the same outcome than rbinom!
Answer: I tried getting to the source code for both, but couldn't find it.
I did, however, find the references for the building of the two algorithms.  They do not use the same references, so it is reasonable that they do not generate them in a similar manner.  Indeed, sample() function briefly says it uses an easier way to handle random numbers, presumably through different generation.

Simulating a Coin toss


Using one component for parent and child routes

Question: I have created a lazy loaded module which has to load a component when user redirects to route But I am getting an error as stated below:
Error: Uncaught (in promise): Error: Cannot find primary outlet to load 'XYZComponent'

Here is my module.
import { XYZComponent } from './xyz-confirm'

const XYZConfirmRoute: Route[] = [
  {
    path: '', component: XYZComponent ,
    children: [
        { path: 'test', component: XYZComponent}
    ]
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    RouterModule.forChild(XYZConfirmRoute)
  ],
  declarations: [XYZComponent],
  exports: [RouterModule],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class XYZConfirmModule { }

As you can see above I want to load XYZComponent for both parent and child routes. is there any way to achieve this?.
Comment: but is it the right way to load same component for both parent and child routes?
Comment: same here: https://stackoverflow.com/questions/42167047/cannot-find-primary-outlet-to-load-xyzcomponent
Comment: the outlet isn't found, you should define outlet.
Answer: Can't you simply do this
  {
    path: 'test', component: XYZComponent
  },{
    path: '', component: XYZComponent
  }

NOTE You must define narrowed path on the top as I did test is on the top and then ''


programmatically get child nodes referencing parent by entity reference uuid, using loadByProperties & entityTypeManager or other way without views

Question: I tried this answer:
https://drupal.stackexchange.com/a/298084/1082
But I can't seem to use
'field_scientists' => $entity_reference_uuid
where, in my case, field_scientists is a field of type Entity Reference UUID - which is a field type provided by https://www.drupal.org/project/entity_reference_uuid module
What happens for me is that I get nothing back, my results array from that example is empty.
Looking to do this programmatically in my own custom module, rather than use a Drupal View.
If there's another way e.g. that uses the ->query method for querying the database I'd be happy to use that programmatic approach.
Thank you.
Update
An example of a uuid of a node in my case is BMSSYS0000051988.
This was set in a migration import from a field in a CSV.
My attempts are:
1.  based on the example in the other question: https://drupal.stackexchange.com/a/298084/1082
  $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
    'type' => 'bol',
    'field_parent_uuid' => $taxonkey,
  ]);

2. db query
$query = \Drupal::entityQuery('node')
->condition('status', NODE_PUBLISHED)
->condition('type', 'bol');
$and = $query->andConditionGroup();
$and->condition('field_parent_uuid',  $taxonkey);
$query->condition($and);
$result = $query->execute();

where $taxonkey is BMSSYS0000051988
both 1 and 2 attempts above return an empty array.
Update 2
here is my migration config that shows uuid being written to from a field in the csv. Is this legal? Should a developer's code change the uuid value or should uuid only be handled by Drupal core?
dependencies: {  }
id: bol_csv_import
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags: null
migration_group: defaultl: publicbins
label: 'Import bol species data'
source:
  plugin: csv
  path: ./data/taxonomy_species_with_unpacked_bold.csv
  delimiter: ','
  enclosure: '"'
  header_offset: 0
  ids:
    - uuid
  fields:
    -
      name: uuidl: publicbins
      label: 'NBN id'
    -
      name: parent_uuid
      label: 'NBN id of parent'
    -
      name: name
      label: Name
    -
      name: rank
      label: 'Taxonomic Rank'
    -
      name: taxon_key
      label: 'Taxon Key'
    -
      name: authority
      label: authority
    -
      name: specimenrecords
      label: specimenrecords
    -
      name: publicrecords
      label: publicrecords
    -
      name: publicbins
      label: publicbins
    -
      name: taxid
      label: 'NBN Taxon ID'
process:
  title: name
  uid:
    plugin: default_value
    default_value: 1
  uuid: uuid
  field_parent_uuid: parent_uuid
  field_taxon_key: taxon_key
  field_authority: authority
  field_rank: rank
  field_num_bold_barcode_specimens: specimenrecords
  field_num_bold_public_records: publicrecords
  field_num_bold_public_bins: publicbins
  field_bold_taxon_id: taxid
  type:
    plugin: default_value
    default_value: bol
destination:
  plugin: 'entity:node'
migration_dependencies: null

Update 3
I'll write a db query to solve this issue. Running a command line sql indicates this approach could work:
MariaDB [db]> select * from node where nid = 246;
+-----+------+------+------------------+----------+
| nid | vid  | type | uuid             | langcode |
+-----+------+------+------------------+----------+
| 246 |  248 | bol  | NBNORG0000041189 | en       |
+-----+------+------+------------------+----------+
1 row in set (0.001 sec)

Comment: `BMSSYS0000051988` is not a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier).
Comment: No, it's not that only Drupal core should manage UUIDs. One of the main selling points is that UUIDs are unique world wide, so you can migrate them from site to site and they remain unique. But I wouldn't use the UUID field for other data than UUIDs. Put the non-UUID value in its own field and use UUIDs to cross reference data items in the source and/or destination of the migration. Where you create the UUIDs doesn't matter.
Comment: +1 Thank you @4uk4 that could be helpful `uuid` was set as that in the migration.  When I do `$node->uuid();` then `BMSSYS0000051988` is what I get back. Do you know if `uuid` should be set by the developer's own code, or should it be only set by Drupal Core itself?
Comment: I've added my migration config to my question to show how uuid was written to with `BMSSYS0000051988`.  If this is "illegal" in that only Drupal core should manage and create/write `uuid`s, then that would promote my strategy b) to implement a workaround to avoid using the uuid in this way: https://drupal.stackexchange.com/questions/310697/programmatically-get-child-nodes-referencing-parent-by-entity-reference-uuid-us?noredirect=1#comment395034_310699
Comment: +1 agree. I would prefer that the migration would not have imported that field data into `uuid` and instead another ordinary field. However I need to work with what I have, so in my 'Update 3', I will write a db query.
Answer: In theory this should work, the field type sets the UUID as main property: EntityReferenceUuidItem:: mainPropertyName()
It might be that Drupal is confused and handling the field as a standard entity reference field because it extends the core field type.
To make clear which field property you are querying append it with a dot:
'field_scientists.target_uuid' => $entity_reference_uuid

Which should work for both, loadByProperties or an entity query:
->condition('field_scientists.target_uuid', $entity_reference_uuid)

The first is only a shortcut for the second using the same query engine.
Comment: `$uuid = $node->uuid();`
Comment: I will try this out, this morning and get back to you - thank you so far.
Comment: Unlucky I'm afraid. Array returned still empty.
Comment: On  basis that `condition` approach boils down to the same query engine, I would be inclined to not try that also, anticipating same issue instead do 2 things a) raise bug on https://www.drupal.org/project/entity_reference_uuid and b) implement a work around, create a new field in my content type which is a plain old entity reference (not with uuid), populate with a reference to a node and test the code again. If works, write script to populate rest of the nodes (there are 100K of them though). The populating will use the existing entity ref uuid field value to find the node for the new field.
Comment: Is there a way to reproduce the issue on a fresh install? I couldn't. For me it works with and without the appended property name.
Comment: b) as a work around I wouldn't generate extra data. I would write low level SQL instead. This is also a good test whether the UUID you are querying really exists. Low level this is a simple query on one table.
Comment: Noted thank you @4uk4 . My setup seems strange.  Looking at the entity reference uuid field in the node, it is a string, the value of which is from a field that was imported from a CSV. In edit mode display that field shows as the parent node title with its node id in brackets. At the moment, I'm puzzled how that string value can reference the parent node.Looking at the Entity Reference UUID module, I haven't found any config nor in the field that gives a clue. I don't know which field is designated as the UUID. Some missing pieces of the puzzle for me.
Comment: I think I need to work out how to get a node's uuid to help me understand.
Comment: +1 thank you yes, @4uk4 and example `uuid` in my case for that is `BMSSYS0000051988` . I've updated my question also, with turther information as to what I'm tryng. Thank you for all your help.
Comment: looks like it is a confusion of mine between the value of the data in an ordinary field and uuid. so your answer is valid and I agree should work. I will write up some further info later.


Como obter todas as possibilidades a partir de uma string incompleta

Question: Tenho uma string, por exemplo, com 5 caracteres mas necessito da mesma ter 10 caracteres no total, fiz o seguinte codigo para tentar obter os 5 caracteres restantes para obter um total de 10 caracteres:
<?php
$letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$numeros = '0123456789';
$tudo = $letras.$numeros;

$string = 'XXXXX';

for($i = 0; $i < strlen($tudo); $i++) {
    echo $string.$tudo[$i].PHP_EOL;
}

Retorno:
XXXXXA
...
XXXXX9

Gostaria que o retorno fosse, por exemplo, XXXXXAAAAA, XXXXXAAAAB e assim vai, entao fiz outro codigo para tentar obter a quantia exata de 10 caracteres:
<?php
$letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$numeros = '0123456789';
$tudo = $letras.$numeros;

$minimo = 10;
$string = 'XXXXX';

for($i = 0; $i < strlen($tudo); $i++) {
    $base = $string.$tudo[$i];
    for($j = 0; $j < strlen($tudo); $j++) {
        $base .= $tudo[$j];
    }
    echo $base.PHP_EOL;
}

Porem o codigo acima retornou o seguinte:
XXXXXXXAABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
...
XXXXXXX9ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

Nao entendi porque do retorno retornar a variavel $tudo ao inves de retornar o resultado mais logico sendo:
XXXXXAAAAA
XXXXXAAAAB
...
XXXXX9AAAA
XXXXX9AAAB

Nos codigos aqui escritos, usei echo linha por linha para observar o retorno de cada tentativa, mas no codigo final gostaria que retornasse uma array, por exemplo:
array(
'XXXXXAAAAA',
'XXXXXAAAAB',
...
)

Agradeco qualquer solucao e/ou dica desde ja!
Answer: Uma das várias possibilidade seria converter sua string de caracteres em uma instância da classe ArrayObject criar alguns iteradores com o método ArrayObject::getIterator() e os iterar em cadeia para compor o resultado.
O iterador é um marcador de posição dentro uma lista de objetos que vai se deslocando conforme é iterado. ArrayObject::current() retorna o valor onde o iterador está apontando e  ArrayObject::next() o faz apontar para o próximo elemento. Para saber se o iterador extrapolou a lista de itens subscritos teste com ArrayObject::valid()

A lógica de funcionamento é simples, primeiro são criados m iteradores indexados de 0 a n onde n = m-1 e m é quantidade de dígitos que devem ser explorados por foça bruta.
Depois é criada uma função que testa por convolução se os iteradores indexados de n a 1 são válidos ou seja se o iterador[n] se esgotou o iterador[n-1] avança uma posição e o iterador[n] é restaurado, se caso o iterador[n-1] se esgotou o iterador[n-2] avança uma posição e o iterador[n-1] é restaurado,.... se caso o iterador[1] se esgotou o iterador[0] avança uma posição e o iterador[1] é restaurado. Finalmente a função termina testando a validade do iterador[0] caso o mesmo tenha se esgotado é retornado false indicando que todas as combinação foram obtidas.
O código em si opera num loop onde as strings de combinação são montadas a cada iteração do loop, e a cada iteração do loop gerando uma nova combinação de caracteres.

<?php
$letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$numeros = '0123456789';
//Junta letras e números e os separa num array para então criar instância ArrayObject.
$tudo = new ArrayObject(str_split ($letras.$numeros));

$minimo = 5;            //Define o número de caracteres a serem iterados.
$string = 'XXXXX';      //Define o prefixo do resultado.
$iteradores = [];       //Inicializa o array contendo os iteradores.

//Inicializa e armazena o número especificado de iteradores.
for($i=1; $i<=$minimo; $i++){
   $iteradores[] = $tudo->getIterator();
}

//Função de validação aplicada aos iteradores.
function iterar($iter){
    //Do iterador[n](último) ao iterador[1](segundo iterador)...
    for($i=count($iter) - 1; $i>0; $i--){
        //...se o iterador se esgotou....
        if (!$iter[$i]->valid()) {
            $iter[$i]->seek(0);                   //...o renova.
            $iter[$i-1]->next();                  //...itera o seu antecessor.
        }
    }
    return $iter[0]->valid();                     //Retorna o estado do primeiro iterador, enquanto for válido ainda existem combinações a serem geradas.
};

//Enquanto houver combinações...
while(iterar($iteradores)){
    $str = $iteradores[0]->current();                //...obtém o primeiro caractere após o prefixo.
    //...Obtém os caracteres consecutivos da esquerda para direita...
    for($i=0; $i<count($iteradores); $i++){
        $str .= $iteradores[$i]->current();
    }
    $iteradores[count($iteradores) - 1]->next();     //Prepara a nova combinação.

    $base = $string.$str;
    echo $base.PHP_EOL;
}

Teste o código no ideone.com. PS: No modo free são permitidos apenas 5s de execução do código, então ele para abruptamente, copie teste no seu ambiente de desenvolvimento.
Comment: Incrivel, nunca tinha usado nem estudado sobre a classe `ArrayObject`. Com certeza irei me aprofundar mais sobre a mesma! Muito obrigado.


Interpretation of coin toss: odds of getting a tail in two coin flips

Question: Let's say someone asks me: What are the odds of getting a tail when a coin is flipped twice?
Assuming the coin is fair and tosses are independent, I see two ways of solving this problem:

If we tabulate the results, we know the possible outcomes are: TT, HT, TH and HH. Therefore, the probability of getting at least one tail is 0.75.

Using a more intuitive approach, when I flip a coin, the probability of getting a tail is 0.5, so I can expect to get a tail every 1/0.5 = 2 flips. So, if I flip 2 times, I expect 1 tail, so 100%.

I know right answer is 75%, but I can't find a way to properly explain why solution 2 is incorrect. Maybe there is a difference in interpretation?
Comment: Solution 2 is correct on its own, it just doesn't answer the question. Everything you said in your solution 2 is correct. It is just the answer to a different question. Note that your original question is slightly ambiguous and should probably be reworded as "at least a tail" to make it unambiguous and to make it clear why solution 2 is not an answer to this question.
Comment: The missing 25% comes from the extra T in TT.
Comment: The incorrect part of Solution 2 is "so 100%". The expectation being one does not imply that the probability is 100%.
Answer: These are answers to different questions.
$75\%$ or $0.75$ is the answer to the question "What is the probability of at least one Tail when tossing a fair coin twice".  You might calculate it from your equally likely TT, HT, TH and HH outcomes using an indicator variable as $$\frac14 \times 1 +  \frac14 \times 1 +  \frac14 \times 1 +  \frac14 \times 0 =\frac34.$$
$1$ is the answer to the question "What is the expected total number of Tails when tossing a fair coin twice". You might calculate it from your equally likely TT, HT, TH and HH outcomes using a counting variable as $$\frac14 \times 2 +  \frac14 \times 1 +  \frac14 \times 1 +  \frac14 \times 0 =1.$$
Comment: Upvoted for the first sentence, "These are answers to different questions." In probabilities it is extremely important to be careful and precise and specifics with words. That's important in all subfields of mathematics, but even more so in probabilities.
Comment: @Stef The question used to include *"Maybe there is a difference in interpretation or these are answers to different questions?"* before it was edited
Answer: Let $T_1$ and $T_2$ denote the respective events that each of the tosses come up tails.  The second statement is effectively asserting that:
$$\mathbb{P}(T_1 \cup T_2) = \mathbb{P}(T_1) + \mathbb{P}(T_2)
\quad \quad \quad \quad \quad \text{(Erroneous)}$$
or that:
$$\mathbb{P}(T_1 \cup T_2) = \frac{\mathbb{E}(T_1 + T_2)}{2} \quad \quad \quad \quad \quad \quad \text{(Erroneous)}$$
The correct rule is the addition rule of probability:
$$\mathbb{P}(T_1 \cup T_2) = \mathbb{P}(T_1) + \mathbb{P}(T_2) - \mathbb{P}(T_1 \cap T_2).$$
Comment: Thus, with $\mathbb{P}(T_1)  = 0.5$, $\mathbb{P}(T_2) = 0.5$, and  $\mathbb{P}(T_1 \cap T_2)= 0.25$ we get $\mathbb{P}(T_1 \cup T_2) = 0.5+0.5-025 = 0.75$, same as in (1)
Answer: The term "expectation" in statistics means the probability-weighted average over the whole possibility space. It does not mean "the result that will happen with 100% probability". For instance, if you roll a die, the expected value is 3.5, even though the probability of a single die having a value of 3.5 is zero. Going from "the expected number of tails is 1" to "The probability of getting a tail is 100%" is a complete non sequitur.
Comment: It *would* be sequitur in the particular case of a Bernoulli experiment, where expectation and probability of success are equal.
Comment: @Stef Right.  Unfortunately, the application in (2) is to a *Geometric* distribution.
Answer: In addition to the good answers already given, there is in this situation a simple trick you can use to compute directly the probability.
Solve the inverse problem - what is the probability of not getting any tails? This is $$P_1 = 0.5$$ for the first throw and $$P_2 = 0.5$$ for the second throw, therefore this event has the probability of $$P = P_1*P_2 = 0.5*0.5 = 0.25$$
Since $P$ is the probability of not getting tails, the probability of getting at least one tail is $$Q = 1-P = 0.75$$ as in your first case.


Lightbox2 doesn't appear in the page

Question: I want to add Lightbox2 from http://lokeshdhakar.com/projects/lightbox2/ to my page, but following instructions (adding a JavaScript, and a CSS file) doesn't work.
I thought I did all correctly, but when I click on the image, it doesn't show any popup.
What's wrong with it?
Comment: You're using a version of lightbox that requires jQuery 1.7, whereas Drupal comes with jQuery 1.4 by default. Try the dev version of [jQuery Update](http://drupal.org/project/jquery_update), or [jQuery Multi](http://drupal.org/project/jqmulti). Either of those should let you load the right version of jQuery into the page.
Comment: That seems an acceptable answer. `:)`
Answer: There's also a Lightbox2 Drupal Module that integrates with various other modules. And you don't need to update your Drupal's jQuery.
Here's the documentation since I don't really know how you're intending to use Lightbox2 on your website.

Lightbox2 doesn't appear in the page


"abline" doesn't work after "plot" when inside "with"

Question: I want to create a scatterplot and draw the regression line for a subset of a dataset. To give a reproducible example I'll use the CO2 dataset.
I tried this but the regression line doesn't appear for some reason
with(subset(CO2,Type=="Quebec"),plot(conc,uptake),abline(lm(uptake~conc)))

What is the correct way to give a command like this? Can I do it with a one-liner?
Answer: You need to provide both your lines of code as a single R expression. The abline() is being taken as a subsequent argument to with(), which is the ... argument. This is documented a a means to pass arguments on to future methods, but the end result is that it is effectively a black hole for this part of your code.
Two options, i) keep one line but wrap the expression in { and } and separate the two expressions with ;:
with(subset(CO2,Type=="Quebec"), {plot(conc,uptake); abline(lm(uptake~conc))})

Or spread the expression out over two lines, still wrapped in { and }:
with(subset(CO2,Type=="Quebec"),
     {plot(conc,uptake)
      abline(lm(uptake~conc))})

Edit: To be honest, if you are doing things like this you are missing out on the advantages of doing the subsetting via R's model formulae. I would have done this as follows:
plot(uptake ~ conc, data = CO2, subset = Type == "Quebec")
abline(lm(uptake ~ conc, data = CO2, subset = Type == "Quebec"), col = "red")

The with() is just causing you to obfuscate your code with braces and ;.
Comment: Awesome suggestion in your "edit".  That's by far the correct way.
Comment: It's just that I don't like to give more than once a set of several restrictions. It might be dangerous.
Answer: From ?with: with ... evaluates expr in a local environment created using data.  You're passing abline() via ....  You need to do something like this:
with(subset(CO2,Type=="Quebec"),{plot(conc,uptake);abline(lm(uptake~conc))})

Answer: Gavin and Joshua offer good solutions to your immediate problem; here's the equivalent plot using ggplot:
library(ggplot2)
qplot(conc, uptake, data = CO2[CO2$Type == "Quebec" , ]) + stat_smooth(method = "lm", se = FALSE)


How to use RSA_PRIVATE_KEY in nodejs for JWT?

Question: I'm following angular documentation https://blog.angular-university.io/angular-jwt-authentication/ in step 2 uses RSA_PRIVATE_KEY but it doesn't explain how to generate it, I have to install something to use
const RSA_PRIVATE_KEY = fs.readFileSync (' ./ demos / private.key ');?

Thanks
Answer: You need OpenSSL binary to generate the keys:
openssl genpkey -out private.pem -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096
openssl pkey -in private.pem -out public.pem -pubout

You're welcome ;)
Comment: Didn't you check already? It doesn't matter, you need openssl binary.
Comment: yes i checked but i got an error performing a request to the server  'error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
'
Comment: this is the error https://stackoverflow.com/questions/59721820/evp-decryptfinal-exbad-decrypt-error-on-nodejs


Noetherian local domain of dimension one

Question: While revising for my exam in Commutative algebra, I came across the following statement that:

Let $A$ be a Noetherian local domain of dimension one. Let $x,y \in A$ with $x \neq 0$ and $y \in \mathfrak{m}$. Then $ax = y^n$ for some $a \in A$ and $n \geq 1$.

But I don't really understand why? Does this have anything to do with that in a Noetherian local domain every non-zero ideal can be written as a power of the maximal ideal? If yes, why do we even need an $a \in A$?
Thanks in advance!
Comment: The condition can be reformulated as follows: $\bar y\in A/(x)$ is nilpotent. But $A/(x)$ is local Artinian, so...
Comment: The exact wording would be the following way: Let $x,y \in A$ with $x \neq 0$ and $y \in \mathfrak{m}$. Then $ax = y^n$ for some $a \in A$ and $n \geq 1$. @user26857
Edit: I have edited the question.
Comment: I know that if $(A, \mathfrak{m})$ is local Artin, then $\mathfrak{m}$ is the only prime ideal and it is nilpotent, i.e. every element in the maximal ideal is nilpotent, so the whole $A/(x)$ is nilpotent, i.e. the maximal ideal. Is that correct?
Comment: I don't get this question: do you have to show that for a given $x\ne0$ there are $a\in A$, $y\in\mathfrak m$ and $n\ge 1$ such that $ax=y^n$?
Comment: @MBCLA yes,completely correct.
Answer: If $A$ is a DVR, then every nonzero ideal in a DVR is a power of the maximal ideal $\mathfrak{m}$, and we have $x=ut^n$ for $t\in \mathfrak{m}$ being a uniformizer. However, if $A$ is not a DVR, then this is not true in general. Then
$\mathfrak{m}$ is not principal.
Comment: @DietrichBurde I don't think is true that every nonzero ideal is a power of the maximal ideal, unless you assume that $R$ is a DVR as it seems.
Comment: Yes, I knew the one with unit $u$. But since here it is given that $a$ is in the ring, not necessarily a unit, I was confused. So, it is correct that in the case above $a$ would have to be in the unit? Is it also possible, that it is not a unit?
Comment: Yes, $a$ can be arbitrary. Then let $x'=ax\in A$ and write $x'=y^n$.
Comment: Ahhhhh, of course :D Such a stupid question from me! Thanks a lot for your reply :)
Comment: @user26857 You are right, it is not given, that $A$ has to be a DVR. I assumed it automatically without looking at it closer. But how can one see this statement otherwise, if the property with power of maximal ideal is not given?
Comment: Ah, sorry, I had assumed DVR.