Fit distributions with glm

Question: I'm trying to fit different statistical distributions (Gamma, Poisson, normal, inverse Gaussian) to my data with a glm. An example could be like this:
data   <- rexp(500, 3)
model  <- glm(data~1, family=Gamma())
shape  <- 1 / gamma.dispersion(model)
rate   <- shape*model$coef[1]

model2 <- glm(data~1, family=poisson())
lambda <- unique(model2$fitted.values)

So, here are the questions:

If I want to select between all of them the best distribution fit with a glm, can I use the models AIC? Or should I use another method, like MSE? It's because I have really different results from one distribution to another.
I already know that packages fitdistrplus or MASS include specific functions to do this, but it's too slow for my data. If I use those functions to fit the data, mostly of the time I get as a result an exponential distribution. Is there a way to fit it with the glm families?

Update: The data comes from sales orders, but it is always grater than 0, that's why I can use the exponential or gamma distributions. (Only include the normal for the cases where there are so many orders that the negative part under the curve is almost 0).
Also, I need the distribution for simulate from it, a new series.
With the result showed by the AIC i can't make them compete to get the best fitting.
I would like a formal test, but i also need it to be really fast, so I'll settle for an approximation or something exploratory.
Comment: Where do these data come from? Why do you need to know what distribution they are (ie, what is your end goal)? What are the "really different results", & what is the impact of them? Do you need a formal test, or are you looking exploratatively for a better understanding?
Comment: Yes, I will have to compute it for regular intervals. I have almost 500000 records, and for each one I have to compute it's distribution. With the other calculations of the code I reach the half a second for each record, so this makes little less than 3 days. I was thinking on keeping the distribution for each record, and this way save time, but I will have to compute the calculation for half of them periodically.
Comment: Thanks for the clarification. Why would it need to be really fast? Are you going to code this & have it run automatically at regular intervals? If you just need to figure out what the distribution is, presumably that could be done once & then you could simulate however much you want later.
Comment: These data are "sales orders", are they *counts* (eg, 5 orders), or something like revenue (which can have fractional values)?
Comment: Yes, they are the number of items ordered for each time. Why?
Comment: Why would you fit both a Poisson and a gamma (say) to either continuous or to count data?
Answer: Since your data are counts, they can only come in whole numbers.  That is, you could get a count of $5$ orders or $6$ orders, but you could never get $5.5$ orders.  Thus, your data cannot be distributed as Gamma, normal or inverse Gaussian, as these are continuous distributions.  (It is always possible in some case that the fits will be good enough for your purposes, but your goal is to use the distribution to simulate new data, so we can just rule them out.)
For your case, the default first choice would be the Poisson distribution.  However, the Poisson is very restrictive, in the sense that the variance must equal the mean.  This can happen in the real world, but rarely does in practice and I highly doubt it would hold for sales.  A distribution that relaxes this restriction / generalizes the Poisson is the negative binomial.  This will allow you to estimate the mean and variance separately.
Thus, your first strategy for fitting these distributions and doing so quickly, is to only fit the negative binomial.  The primary function for getting these parameters is ?fitdistr in the MASS package.  Subsequently, you can use ?rnbinom for simulation.
If speed and size remain a problem, another possibility is to extract random subsamples of manageable size from your data and fit those.  Even if these were a small proportion of the total, the fitted parameters should be very close to the true parameters if the sample is large enough.
Comment: I think I can't use the subsamples, because the total data for each set is not really large. I will try only the negative binomial and check the results. Also, I tried the Poisson, that it was my first approach, but it fitted really bad the data. So my strategy was to add more distributions although they were for continuous data. Thanks a lot for the explanation.
Answer: It is quite interesting to hear using glm to replace the fitdistr.
But at least the code has some problems, which you may have found from the totally different AIC or likelihood.
The glm family distribution function will consider the link function by default.
So in your code, gamma() means Gamma(link = "inverse") by default. And a log link for the poisson distribution. You may try gamma(link ="identity") and compare with the poisson(link = "identity")
Comment: I already checked this, but I have one question. If I change the glm code like that, then, the parameters of the distributions have to be changed with the corresponding link function? Thanks!
Comment: @TomásM.Sorry for not understanding your exact meaning. But different link function will transform the underlying data first. So the result of fitting parameters are different and so are the AIC and llh.
Comment: Sorry for my bad english and not making me understanding. I was wondering if the parameters need to be changed in any way with the link function or they can be used to simulate a new dataset, similar to the fitted, as they are on the glm result.
Comment: @TomásM. Link function should be set as link ="identity". In this case, the parameter does not need transforming. Eg. lambda of Poisson, lambda from glm is the same as lambda from fitdistr and it can be used to simulate new distribution directly. But gamma distribution need to care about using the same pattern (shape/rate, or shape/scale). If the link function is not set to identity, some transformation has been done by glm by default, then the fitting parameters are not the same as fitdistr. This not only affects the value of parameter but also the standard error of the fitting.

Fit distributions with glm


prepared statement help, 63/63 parameters failing with number of parameters mismatch

Question: i'm having troubles with my prepared statement (mysqli), i'm doing my connection and everything's right until i try to bind parameters, here's the problem, i'ml trying to bind 63 values, and I did check many times i have the right amout of values and feel desperate because i' can't find the mistake leading me to the error :

mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

Here follow my code:
<code>$query = $db->prepare("INSERT INTO `T_TOUR_RES`(`Id`, `UpDate`, `NumVoie`, `Indice`, `Voie`, `Ville`, `CodePostal`, `LocNom`, `LocPrenom`, `LocNumVoie`, `LocIndice`, `LocVoie`, `LocCodePostal`, `LocVille`, `LocTel`, `PropNom`, `PropPrenom`, `PropNumVoie`, `PropIndice`, `PropVoie`, `PropVille`, `PropCodePostal`, `PropTel`, `EntRaisonSociale`, `EntDenominationCommerciale`, `EntTypeEntreprise`, `EntNumVoie`, `EntIndice`, `EntVoie`, `EntVille`, `EntCodePostal`, `EntTel`, `EntAPE`, `EntNAF`, `EntSIRET`, `IsLoc`, `IsProp`, `IsPro`, `IsAdmin`, `Done`, `DoneDate`, `DoneEq`, `IsIndiv`, `IsCollectif`, `NbOccupants`, `HasComposteur`, `IsPrincipal`, `IsSecondaire`, `IsGite`, `IsHote`, `IsRefus`, `IsWeb`, `HasPlace`, `IsDiffManip`, `IsPrecoPAV`, `LinkCollectif`, `IsBatimentCollectif`, `HaveBac`, `NbOM`, `NbTri`, `Comportement`, `Remarque`, `RecapBac`, `ID_TOUR`, `ID_SOURCE`) VALUES (NULL, NOW(),'?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')");

    if ($query === FALSE) {
        die ("Mysql Error: " . $db->error);
    } else {
        if ($query->bind_param("issssssisssssssissssssssissssssssiiiiissiiiiiiiiiiiiiiiiiisssii",$this->NumVoie, $this->Indice, $this->Voie, $this->Ville, $this->CodePostal, $this->LocNom, $this->LocPrenom, $this->LocNumVoie, $this->LocIndice, $this->LocVoie, $this->LocCodePostal, $this->LocVille, $this->LocTel, $this->PropNom, $this->PropPrenom, $this->PropNumVoie, $this->PropIndice, $this->PropVoie, $this->PropVille, $this->PropCodePostal, $this->PropTel, $this->EntRaisonSociale, $this->EntDenominationCommerciale, $this->EntTypeEntreprise, $this->EntNumVoie, $this->EntIndice, $this->EntVoie, $this->EntVille, $this->EntCodePostal, $this->EntTel, $this->EntAPE, $this->EntNAF, $this->EntSIRET, $this->IsLoc, $this->IsProp, $this->IsPro, $this->IsAdmin, $this->done, $this->DoneDate, $this->DoneEq, $this->IsIndiv, $this->IsCollectif, $this->NbOccupants, $this->HasComposteur, $this->IsPrincipal, $this->IsSecondaire, $this->IsGite, $this->IsHote, $this->IsRefus, $this->IsWeb, $this->HasPlace, $this->IsDiffManip, $this->IsPrecoPAV, $this->LinkCollectif, $this->IsBatimentCollectif, $this->HaveBac, $this->NbOM, $this->NbTri, $this->Comportement, $this->Remarque, $this->RecapBac, $this->ID_TOUR, $this->ID_SOURCE)) {

            if($query->execute()){}else{die ("Mysql Error: " . $db->error);}
        } else {
            die ("Mysql Error: " . $db->error);
        }
    }
</code>
Hoping someone can explain me what i'm failing here !
Comment: You need to remove your single quotes around your placeholders - `'?'`. `bind_param()` does the quoting for you.
Comment: Are *any* of your variables not getting set?
Answer: You if you're using prepared statements you should not use single-quotes around your ?
<code>$query = $db->prepare("INSERT INTO `T_TOUR_RES`(`Id`, `UpDate`, `NumVoie`, `Indice`, `Voie`, `Ville`, `CodePostal`, `LocNom`, `LocPrenom`, `LocNumVoie`, `LocIndice`, `LocVoie`, `LocCodePostal`, `LocVille`, `LocTel`, `PropNom`, `PropPrenom`, `PropNumVoie`, `PropIndice`, `PropVoie`, `PropVille`, `PropCodePostal`, `PropTel`, `EntRaisonSociale`, `EntDenominationCommerciale`, `EntTypeEntreprise`, `EntNumVoie`, `EntIndice`, `EntVoie`, `EntVille`, `EntCodePostal`, `EntTel`, `EntAPE`, `EntNAF`, `EntSIRET`, `IsLoc`, `IsProp`, `IsPro`, `IsAdmin`, `Done`, `DoneDate`, `DoneEq`, `IsIndiv`, `IsCollectif`, `NbOccupants`, `HasComposteur`, `IsPrincipal`, `IsSecondaire`, `IsGite`, `IsHote`, `IsRefus`, `IsWeb`, `HasPlace`, `IsDiffManip`, `IsPrecoPAV`, `LinkCollectif`, `IsBatimentCollectif`, `HaveBac`, `NbOM`, `NbTri`, `Comportement`, `Remarque`, `RecapBac`, `ID_TOUR`, `ID_SOURCE`) VALUES (NULL, NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
</code>
Edit:
Next time maybe make 1 google search on the error:
Php mysqi bind_param Number of variables doesn't match number of parameters in prepared statement
Comment: My bad, might be the heat who stroke me too hard, i was having an other mistake i resolved with Copy/paste from pma, and i forgot to put the single stroke away when i replaced, thank you for the fast answer !


Probability of Normal Distribution with Unknown Mean

Question: I am still quite new to the whole idea of probability and statistics and am not sure how to do this question.
The random variable R, also normally distributed, has a standard deviation of 3.59 with unknown mean is unknown. Find the greatest possible value of $P(-3.74<R<5.82)$.
I tried to integrate the normal distribution function using Maclaurin's expansion but it just got very messy and I'm sure there is a better way of doing it.
Comment: Normal distribution have it mode in the same place that it mean and median, and it density function is symmetric respect this point.
Answer: Look at the shape of the normal probability density function. It's unimodal and symmetric. So the probability of coverage is maximized if the mean of the distribution, $\mu$, coincides with the middle of your interval, that is, $1.04$. This result is independent of the standard deviation, $\sigma$. However, the attained probability does depend on $\sigma$.
Answer: $$g(x)=\frac{1}{\sigma\sqrt{2\pi}}\, e^{-\frac{(x - \mu)^2}{2 \sigma^2}}$$
$$P(a<x<b)=\int_a^b{g(x)dx}=\int_a^b{\frac{1}{\sigma\sqrt{2\pi}}\, e^{-\frac{(x - \mu)^2}{2 \sigma^2}}}dx$$
Where $a=-3.74$, $b=5.82$ and $\sigma=3.59$
What you are interested in is finding $\mu$ that maximizes $P$
$$\frac{\partial P(a<x<b)}{\partial \mu} =0$$
EDIT
As Luis Mendo mentioned, since the Normal distribution is centralized and symmetric, the maximum would be in the middle of the interval regardless of the standard deviation.
$$\mu=\frac{a+b}{2}=\frac{3.74+5.82}{2}$$
Comment: You mean find $\mu$ right? and the derivative should also be w.r.t. $\mu$, we know the standard deviation, but not the mean.
Comment: Right, fixed it...
Comment: I thin you may be missing a $-$ so $\mu=\frac{a+b}{2}=\frac{-3.74+5.82}{2}$


Better approximation for Lambert W Function near zero

Question: I've looking for a polynomial approximation for Lambert W Function around zero.
I am aiming at the range of $0\leq x\leq e$, and if possible then even $-e\leq x\leq e$.
The asymptotic expansion (when limited to a finite number of iterations of course), yields a polynomial approximation for the range of $-1/e\leq x\leq1/e$.
I've found the following polynomial approximation for the range of $0\leq x\leq e$ in this answer:
$$W_0(z)\approx\ln(1+z)\frac{1+\frac{123}{40}z+\frac{21}{10}z^2}{1+\frac{143}{40}z+\frac{713}{240}z^2}$$
I've tested it to my satisfaction, but I would like ask a few questions here:

Can it be easily extended to the range of $-e\leq x\leq e$?
The author of that answers explains something about Padé approximant, and says that one might do a better approximation (which he/she did not bother to find due to the question in context). Would anyone be able to shed light on how to compute a better polynomial approximation?
Is there perhaps a different approach to my goal, unrelated of the answer above?

Thank you!
Comment: These are not polynomial approximations.  They are rational approximations to $W(x)/\ln(1+x)$.
Answer: The minimax rational approximation of $W(x)/\ln(1+x)$ on $[0,e]$ with numerator and denominator of degree $2$ is, according to Maple,
$$ \frac{0.0396202320 + 0.1961951280 x + 0.1702729841 x^2}{0.0396188863 + 0.2161222712 x + 0.2405866129 x^2}$$
which has maximum absolute error approximately $0.00003396612388$ on that interval.
You're not going to be able to get an approximation on $[-e, e]$ because $W(x)$ has a branch point at $-1/e$ and all its branches are complex for $x < -1/e$.  You could look for approximations on $[-1/e, e]$.   Thus the
minimax rational approximation of $W(x)/\ln(1+x)$ on $[-1/e, e]$ with numerator and denominator of degree $2$ is
$$\frac{0.0663274708 + 0.2304903265 x + 0.1716240158 x^2}{0.0663270883 + 0.2636065188 x + 0.2426002763 x^2} $$
with maximum absolute error approximately $0.00004705730323$ on that interval.
You can get better accuracy with higher-degree approximations.
Comment: Yes, on a smaller interval you should get a better approximation.  In this case, for degrees $2$ and $2$ Maple is telling me "error curve fails to oscillate sufficiently; try different degrees".  With numerator of degree $1$ and denominator $3$, it gives me
$${\frac { 0.276972170484786+ 0.332660424982433\,x}{ 0.277527230334148
+ 0.466289676939177\,x+ 0.00150785702317642\,{x}^{2}-
 0.000244712398927012\,{x}^{3}}}
$$
with maximum error $6.281026638 \times 10^{-6}$.
Comment: Thank you! Your last (second) approximation is for $x\in[-1/e,e]$?
Comment: Yes, that's what I said.
Comment: Awesome (and very useful) answer, thank you very much!!!
Comment: I would like to ask one more thing: since I am already handling the range $[-1/e,1/e]$ accurately, I technically need to handle only the range $[1/e,e]$. Will Maple generate a more accurate 2nd-degree approximation if I narrow the input range in your first example?
Comment: Thank you so much. I actually downloaded Maple already, but needed to learn how to use it. +10 (if I could).


How to Automatically Manage a LARGE Number of Views Pages Under ONE?

Question: I am developing a website for a school's Alumni Association that has been around since the 1800s. Every student has a Grad Class specified in the database:
e.g. /students/1947/John_Doe (indicates graduated in 1947; all of this is ready to go).
But now I want to create a page for every year (1890, 1891, ... 1970, 1971, etc.) and have each listed on that page:
e.g. /students/1947 (would list all students who graduated in 1947)
Using Contextual Filters, this is easy!! But the problem is that creating each individual page for each individual year would be extremely cumbersome. Do each of these pages needed to be created manually (with an identical Contextual Filter) or is there a way to automatically manage all of these graduation classes under a single view? If so, how would those pages be created?
Answer: All you have to do is to add a wildcard to the URL pattern of your view like <code>/students/%</code> and then use this as argument for the contextual filter.
Similar to how the default "Taxonomy term" view does it (screenshot attached):


How to prove that $\int_0^{\pi} \log(|\sin t|)\,\textrm{dt} \;\;\textrm{is integrable }$

Question: How to prove that $$\int_0^{\pi} \log(|\sin t|)\,\textrm{dt} \;\;\textrm{is integrable }$$
Any hints would be appreciated.
Answer: Hint: It is true that $$\sin{t} \ge \frac{2}{\pi} t$$ on the interval $[0, \pi/2]$; try drawing the graph, or noting that $\sin{t}$ is concave down and we have equality at the endpoints. Now show that
$$\int_0^{\pi/2} |\ln{\frac{2}{\pi} t}| dt < \infty$$
Do something similar for the right-half of the interval.


How can I import data from XML into a content type using the Migrate module?

Question: I have activated the Migrate, the Migrate Plus, and the Migrate Tools modules. I have then imported a migration file at /admin/config/development/configuration/single/import.
id: beetween_jobs
label: 'Migrate beetween jobs from XML file'
migration_group: default
source:
  plugin: xml
  data_fetcher_plugin: http
    # 'simple_xml' is configured to be able to use the 'http' fetcher.
    data_parser_plugin: simple_xml
    urls:
      - https://mywebsite/beetween.xml
    item_selector: /root/jobs
process:
  title: title
  field_diffusion: diffusion
  field_faculty : faculty
destination:
  plugin: 'entity:node'
  default_bundle: job
migration_dependencies:
  required: {  }
  optional: {  }

I could not find any official documentation, so I created that file basing on a tutorial.
On /admin/structure/migrate/manage/default/migrations, it doesn't list the migration I added. I just says:

Il n'y a pas encore de Entités migration.
Échec de connexion au serveur de base de données. Le serveur a renvoyé
le message suivant : No database connection configured for source
plugin variable.
Est-ce que le serveur de base de données fonctionne ?
Est-ce que la base de données existe et avez-vous saisi le bon nom de base ?
Avez vous saisi les bons nom d'utilisateur et mot de passe ?
Avez vous saisi le bon nom d'hôte de la base de données ?
Comment: For a start, I believe all of the keys under source need to be indented the same, i.e. they are all direct children of source.
Comment: While the second part of the error message talks about a database connection, I believe it can't connect to the migration source (which I believe is because it's indented improperly in your yml).
Comment: Pretty sure the migrate plus module has an examples folder that demonstrates some of this.
Answer: As Kevin suggested, inspired by the

migrate_plus.migration.wine_role_xml.yml

file from the migrate_plus module , i have managed to make it working as such:
id: beetween_jobs
label: 'Migrate beetween jobs from XML file'
migration_group: default
source:
  # We use the XML data parser plugin.
  plugin: url
  data_fetcher_plugin: http
  data_parser_plugin: xml
  # Normally, this is one or more fully-qualified URLs or file paths. Because
  # we can't hardcode your local URL, we provide a relative path here which
  # hook_install() will rewrite to a full URL for the current site.
  urls:
    - https://my_website/files/ressources_docs/beetween.xml
  # Visit the URL above (relative to your site root) and look at it. You can see
  # that <response> is the outer element, and each item we want to import is a
  # <position> element. The item_xpath value is the xpath to use to query the
  # desired elements.
  item_selector: /root/job
  # Under 'fields', we list the data items to be imported. The first level keys
  # are the source field names we want to populate (the names to be used as
  # sources in the process configuration below). For each field we're importing,
  # we provide a label (optional - this is for display in migration tools) and
  # an xpath for retrieving that value. It's important to note that this xpath
  # is relative to the elements retrieved by item_xpath.
  fields:
    -
      name: title
      label: 'title'
      selector: title
    -
      name: diffusion
      label: 'diffusion'
      selector: diffusion
  # Under 'ids', we identify source fields populated above which will uniquely
  # identify each imported item. The 'type' makes sure the migration map table
  # uses the proper schema type for stored the IDs.
  ids:
    title:
      type: string
process:
  # Note that the source field names here (machine_name and friendly_name) were
  # defined by the 'fields' configuration for the source plugin above.
  title: title
  field_diffusion: diffusion
destination:
  plugin: 'entity:node'
  default_bundle: job
migration_dependencies:
  required: {  }
  optional: {  }


IS there a replacement for 'taxonomy_node_get_terms_by_vocabulary' in Drupal 7?

Question: The closest I have come is taxonomy_term_load_multiple but the conditions argument will be deprecated in V8.  They now recommend EntityFieldQuery as a replacement.  I am new to coding and have not worked with setting up the conditions for the query and am having trouble finding documentation/tutorials.  If a simpler function exists, I would prefer to use it.  Answers addressing a function or how to use EntityFieldQuery are welcome.
Thanks
Answer: I don't think an EntityFieldQuery would work in this case as you'd be trying to reference two types of entity in the same query (which cannot be done currently). The easiest way would be to build up the query yourself:
function taxonomy_node_get_terms_by_vocabulary($node, $vid) {
  $terms = &drupal_static(__FUNCTION__, array());

  if (!isset($terms[$vid])) {
    $terms[$vid] = array();
  }

  if (!isset($terms[$vid][$nid])) {
    $query = db_select('taxonomy_term_data', 'td')
      ->fields('td', array('tid'))
      ->condition('td.vid', $vid)
      ->condition('i.nid', $node->nid)
      ->orderBy('td.weight');

    $query->join('taxonomy_index', 'i', 'i.tid = td.tid');

    $terms[$vid][$nid] = taxonomy_term_load_multiple($query->execute()->fetchCol());
  }

  return $terms[$vid][$nid];
}

Comment: No problem, in case you haven't seen it take a look at [How to use EntityFieldQuery](http://drupal.org/node/1343708), it does a pretty good job of explaining things :)
Comment: Thanks, I'm new to coding SQL/PHP/Drupal.  I would have been stuck on EntityFieldQuery for some time before catching the flaw. Every example helps a lot!
Comment: If I want to read all terms of a vocabulary (instead of just those on a node, would I be correct to simply delete references to $NID, and delete lines -> condition ('i.nid...); and $query->join....  ?
Comment: You could do definitely, but in that case it would probably be better to use the built-in function [`taxonomy_get_tree()`](http://api.drupal.org/api/drupal/modules--taxonomy--taxonomy.module/function/taxonomy_get_tree/7). Using that function will give you the correct hierarchy of terms in the vocab, not just a flat list of all terms.
Comment: I saw that on, but couldn't determine of the array returned was flat or a tree and I was not certain how I would read it if it was multilevel.


Selenium is very slow , any alternative?

Question: URL - https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch
Using selenium I can extract data from the above URL but the process is very slow. Is there any way I can extract data using request library only?
I wanted the text shown in the image  to extract.
My code to extract data using selenium -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome('chromedriver',options=option)

driver.get('https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch')
time.sleep(5)

html_text2 = driver.page_source
soup2 = BeautifulSoup(html_text2,'lxml')

data1 = soup2.find("span" , "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").text.strip()
data2 = soup2.find("span" , "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)").text.strip()

wb = load_workbook('output.xlsx')
ws  = wb.active
fontstyle = Font(size = "16")
ws['B9'].value = f'{data1}  {data2}'
ws.cell(row = 9 , column = 2).font = fontstyle

wb.save("output.xlsx")

Comment: What have you tried or researched so far?
Comment: @cruisepandey No, he has shown his Selenium based code only. The phrase "using beautifulsoup only" is misleading since it can not get any data from the server. That was Selenium's part. I guess he wants to use requests or similar. But we will know once he supplies details about his attempts.
Comment: First I tried to use beautifulsoup with request library only but I was getting an error (nonetype) . Then I used selenium. But I think It can be extracted by beautiulsoup also but I don't know how
Comment: @KlausD. : He has already shown his efforts, see these two lines `data1 = soup2.find("span" , "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").text.strip()
data2 = soup2.find("span" , "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)").text.strip()` they are bs4
Comment: Please update the question with your attempt and the full error message!
Comment: @KlausD. :  I am not sure what you are talking about, but this is BS `html_text2 = driver.page_source
soup2 = BeautifulSoup(html_text2,'lxml')

data1 = soup2.find("span" , "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").text.strip()
data2 = soup2.find("span" , "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)").text.strip()`
Comment: there's no method called `find` in Selenium, it is available in BS4
Comment: @cruisepandey The code is completely unrelated to the problem, which is to replace Selenium not beautifulsoup. And BTW "this is BS" is a very unfortunate choice of words.
Comment: @AnimeshSingh : You initially mentioned you are looking for solution in BS4, now you have changed the entire heading/post description. Please be clearer about your post. folks here invest time and efforts to solve problem. Anyone can click on the edited link and can see what was originally posted.
Comment: Let me explain what I meant. We generally use a combination of two libraries to scrap data. One to extract full JSON of a webpage(request,urllib etc) and the other to scrape out the required data(generally beutifulsoup). Here while using selenium, extraction of json is becoming quite slow. I wanted a way to fast this process because it is a dynamic website and we cant't use request library to extract JSON.
Comment: Now I found a library called request-html. It works with dynamic websites as well .
Answer: You can use this css_selector :
div>span[data-reactid='31']

which has unique entry in HTMLDOM.
in Beautiful soup, we use select for css not find.
driver.get('https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch')
time.sleep(5)

html_text2 = driver.page_source
soup2 = BeautifulSoup(html_text2, 'lxml')

info = [i.text.strip() for i in soup2.select("div>span[data-reactid='31']")]
print(info)

Answer: You can simply replace chrome() with PhantomJS() in the webdriver line after installing latest phantomjs.
PhantomJS is a discontinued headless browser used for automating web page interaction.
You can try using urllib and post directly to the login link. You can use cookiejar to save cookies. You can even simply save cookie, after all, a cookie is simply a string in http header.
It is not always the selenium which is slow. sometimes we need to look at code we are using.
Comment: I used request_html for data scraping.
Answer: You can use beautifulsoup to get that data pretty fast.
That data is present inside a
 with attributes data-reactid = 30. You can select that
 and extract the data from it.
Here is the code:
import requests
from bs4 import BeautifulSoup

headers = {"User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"}

url = 'https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch&guccounter=1'
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')

d = soup.find('div', {'data-reactid': '30'})
print(list(d.stripped_strings))

['26.70', '-0.00 (-0.01%)', 'At close:  5:35PM CEST']


Como substituir determinadas letras duma frase?

Question: Eu quero substituir essas letras:
local words = {"v", "s ", "#@#", "s", "รง", "b", "mp", "t"}

Quando elas tivessem em uma frase seriam substituídas, ou pode ser uma tabela assim também:
local words = {
   ["v"] = "d",
   ["s"] = "k"
}

Supondo que tenho a frase Insira o texto, o resultado desejado seria infira o fexfo, substituindo o s e t por f.
Comment: Você pode [editar a pergunta](http://pt.stackoverflow.com/posts/66562/edit) e colocar um exemplo do que pretende fazer?
Comment: Digamos que esse é o texto : "Insira o texto", iria retornar isso "infira o fexfo"
Comment: Isso seria em que linguagem?
De qualquer maneira tens de percorrer a string como um array, e confirmares se é essa letra
Answer: Você pode utilizar a função string.gsub para fazer as substituições, o padrão %a representa todas as letras:
function substituirLetras(texto, traducoes)
    return string.gsub(texto, "%a", traducoes)
end

Para utilizá-la, faça:
-- chave: o valor a ser substituído
-- valor: o substituto
letras = {
   ["s"] = "f",
   ["t"] = "f",
}

texto = "Insira o texto"

resultado = substituirLetras(texto, letras)
print (resultado) -- Infira o fexfo

Ver demonstração
Comment: Isso mesmo cara muito obrigado
Comment: @JoaoGabriel Disponha. Se possível marque a resposta como aceita, clicando na marca ✔ abaixo da pontuação. =)


Angular5 - Error: Local workspace file ('angular.json') could not be found

Question: Este erro acontece quando eu vou iniciar a aplicação:

Local workspace file ('angular.json') could not be found. Error: Local
  workspace file ('angular.json') could not be found.
      at WorkspaceLoader._getProjectWorkspaceFilePath (/usr/lib/node_modules/@angular/cli/models/workspace-loader.js:37:19)
      at WorkspaceLoader.loadWorkspace (/usr/lib/node_modules/@angular/cli/models/workspace-loader.js:24:21)
      at ServeCommand._loadWorkspaceAndArchitect (/usr/lib/node_modules/@angular/cli/models/architect-command.js:195:32)
      at ServeCommand. (/usr/lib/node_modules/@angular/cli/models/architect-command.js:47:25)
      at Generator.next ()
      at /usr/lib/node_modules/@angular/cli/models/architect-command.js:7:71
      at new Promise ()
      at __awaiter (/usr/lib/node_modules/@angular/cli/models/architect-command.js:3:12)
      at ServeCommand.initialize (/usr/lib/node_modules/@angular/cli/models/architect-command.js:46:16)
      at Object. (/usr/lib/node_modules/@angular/cli/models/command-runner.js:87:23)

Configurações:
Angular CLI: 6.0.0
Node: 8.11.1
OS: linux x64
Angular: 4.3.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, tsc-wrapped
Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.0
@angular-devkit/build-angular     <error>
@angular-devkit/build-optimizer   <error>
@angular-devkit/core              0.6.0
@angular-devkit/schematics        0.6.0
@angular/cli                      6.0.0
@schematics/angular               0.6.0
@schematics/update                0.6.0
rxjs                              5.4.2
typescript                        2.4.2

Answer: Atualize para a próxima versão do angular CLI
npm uninstall -g @angular/cli
npm cache verify
npm install -g @angular/cli@next

e recrie o projeto com ng new project , pois parece um erro em um dos releases https://github.com/angular/angular-cli/releases , há algumas informações aqui https://alligator.io/angular/angular-6/ sobre o comando ng update


Probability with same number of heads and tails

Question: I have this question that I don't know where to begin, any help will be greatly appreciated.
Consider the following random experiment. Toss a coin until the same number of heads and tails have been observed. Take as random variable, X, the observed number of heads (or tails). Determine the first few values in the probability distribution and calculate
$\sum^{5}_{n=1} nP (X = n)$
I'm not sure exactly where to start, if someone could point me in the right direction that would be great! thanks
Answer: Since we don't differentiate between $H$ and $T$, denote the fist tossed coin $\alpha$, because it doesn't matter what you've tossed. Then you're either done w.p $\frac{1}{2}$ by tossing $\alpha^c$ (NOT alpha) or you return to the state $\alpha$ w.p. $\frac{1}{2}$. Hence you get $P(X=1)$ w.p. $\frac{1}{2}$. Can you hande from here?
Comment: do you need the same #H/T or the same #H/T in a row?
Comment: In this case if you toss $\alpha^c$ straight away, you are done; if you toss another $\alpha$, the only way to get $X=2$ is to toss two $\alpha^c$ in a row - otherwise you'll need one more $\alpha^c$. Hence in order to get $k=2$ you need $\alpha \alpha, \alpha^c, \alpha^c$. Things would be different but for the 'Toss a coin until the same number of heads and tails have been observed' condition.
Comment: P(X = 1) = 1/2
P(X = 2) = 1/4
P(X = 3) = 1/8 ..?

Am I on the right track? The limit will reach 0?
Comment: I'm sorry I'm not exactly sure what you're asking?

I'm pretty sure it's just the same number of H/T in general, doesn't have to be in a row


How can I calculate the frequency when my samples don't span a whole period?

Question: I am taking samples at 30Hz of a signal which is a slowly-varying sinewave.
The period of the sinewave is expected to drift slowly, but will always be somewhere between 0.5s and 2s.
I would like calculate a estimate of the period/frequency of the sinewave, from just the last 1s of data (i.e. the last 30 samples).
I've tried Discrete Fourier Transform, but from what I can tell it doesn't behave well when you have less than a whole cycle sampled.
A naive approach is just to measure the time since the amplitude was roughly where it is at the current sample (two times ago), but that will get fiddly if I happen to be exactly at a maxima with the current sample.
Are there any other approaches I should be trying?
Answer: The fitting to experimental data of the function :
$$y(x)=a+b\cos(\omega x)+c \sin(\omega x)$$
or
$$y(x)=a+\rho\sin(\omega x+\varphi)$$
can be carried out thanks to the a method based on integral equation :
https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales
(in French, presently no translation available) Nevertheless, the synopsis of equations is legible pages 35-36.
The method is convenient especially for data covering only a small part of period.
Also, a specific part of the paper deals with damped sinusoidal regression :
$$y(x)=\rho\sin(\omega x+\varphi)e^{\alpha x}$$ in English, pages 64-67.
A numerical example is included in the referenced paper :
The data is :
-1.983 , 0.936
-1.948 , 0.81
-1.837 , 0.716
-1.827 , 0.906
-1.663 , 0.247
-0.815 , -1.513
-0.778 , -1.901
-0.754 , -1.565
-0.518 , -1.896
0.322 , 0.051
0.418 , 0.021
0.781 , 1.069
0.931 , 0.862
1.51 , 0.183
1.607 , 0.311
Equations (12), (13) yield $S_k$ and $SS_k$ :

Equations (15), (16) yield $\omega_1, a_1, b_1, c_1$ :

Then, equations (18), (19) , (20) yield $\rho_1 , \phi_1 , \Phi_k , K_k , \theta_k$ :

Equations (21), (22) yield $\omega_2 , \phi_2$ , with $a_2=a_1$ and $b_2=\rho_2\cos(\phi_2) \: ; \: c_2=\rho_2\sin(\phi_2)$ :

Finally, with $\omega_3=\omega_2$ , equation (23) yields $a_3 , b_3 , c_3$ which are approximates of $\omega , a , b , c$ :
Comment: Thanks for your answer. I tried to work through the method in the document using Excel (and using the input data from page 23 of that document). However, when I feed the inputs (x) back through the formula with the coefficients resulting from the algorithm, they don't look anything like the y values of the original data. I don't suppose you have any fully worked examples for that algorithm?
Comment: @dbruning : a full worked example is given in the referenced paper, pages 21-32. The copy of this example is now added to my first answer. If you need more explanation I will be again available within about one month because now I am on the verge to go abroad.
Comment: I appreciate those additional workings - I was able to confirm my workings were correct up until stage 3. When you're back from your holiday, would you mind also posting the values for the final matrix multiplication to actually generate a3, b3 and c3? Also - if you happen to be holidaying in NZ, let me know and I'll buy you a beer.
Comment: Never mind the final values - when I implemented this in code, it worked out correctly, so must have been an Excel error on my part. Accepting this answer - thanks for your help!
Answer: Once again, JJacquelin came with a nice answer based on a direct solution of the problem.
As said, the problem of fitting (in the least square sense) a model such as $$y=a+b\cos(\omega x)+c \sin(\omega x)$$ based on $(x_i,y_i)$ data points is simple if $\omega$ is known; in such a case, the problem reduces to a multilinear regression and solves immediately. The problem is much more complex if $\omega$ is not known and would involve nonlinear regression which required "good" starting values.
So, let us assign a given value to $\omega$; perform the linear regression which gives $a(\omega)$, $b(\omega)$, $c(\omega)$ and $SSQ(\omega)$. Try with different values of $\omega$ (marching using a small fixed stepsize) until you identify a minimum of $SSQ(\omega)$. At this point, you are ready for the nonlinear regression.
For illustration purposes, I used the data given in page 23 of JJacquelin's book (I take the opportunity of this post to thank him from providing me an easy access to the data) and I varied $\omega$ from $0$ by steps of $0.1$. I reproduce below the sums of squares around the optimum

<code>1.50         3.8688
1.60         2.8146
1.70         1.7823
1.80         0.9409
1.90         0.4405
2.00         0.3261
2.10         0.5258
2.20         0.9282
2.30         1.4761
2.40         2.2159
2.50         3.2727
</code>

For $\omega=2$, we have $a=-0.3979$, $b=1.2831$, $c=-0.5736$. Starting the nonlinear regression  using these estimates, the convergence is really fast and the solution is $a=-0.3907$, $b= 1.2893$, $c=-0.5717$, $\omega=1.9813$ corresponding to a sum of squares equal to $0.3204$.
All of the above can easily be done using Excel.
Edit
If I repeat the same process using only the data points corresponding to $x_i<0$ (they then cover less than a period), the results are  "quite" similar : the preliminary step shows a minimum for $\omega =1.7$ and the nonlinear regression leads to $a=-0.3820$, $b= 1.0958$, $c=-1.0378$, $\omega=1.7430$.
Comment: Thankyou for your answer. I understand the concept, but I'm not sure how to "perform the linear regression", can you provide more information there , or even show more of the workings you used to get those results?
Comment: If $\omega$ if fixed, then the model write $y=a+b w+ c z$ with $w_i=\cos(\omega x_i)$ and $z_i=\sin(\omega x_i)$. So, $a,b,c$ are immediately obtained using a multilinear regression (define the new regressors) as well as the sum of squared errors. If you want, send me a sample of your data in a .TXT file (my e-mail address is in my profile); in such a way, you would probably better see (I hope).
Comment: Thanks for the offer, the other answer gave a great result without having to iterate, so I've accepted that. Good to know that your approach would also work.


Given an embedding $h:D^n \to S^n$, is the closure of the complement $S^n-h(D^n)$ homeomorphic to $D^n$?

Question: Given an embedding $h:D^n  \to S^n$, is the closure of the complement $S^n-h(D^n)$ homeomorphic to $D^n$?
What I know about this is:
$h$ restricts to an embedding $h:S^{n-1} \to S^n$, and the generalized Jordan's curve theorem implies that $S^n-h(S^{n-1})$ has two components, having common boundary $h(S^{n-1})$. In the above case, we also know that the closure of such one component is homeomorphic to $D^n$.
Comment: https://en.wikipedia.org/wiki/Alexander_horned_sphere
Comment: @MoisheKohan Is it a counterexample?
Comment: Yes, this is a counter example. More examples of this type can be found for instance in [Geometric topology of 3-manifolds](https://bookstore.ams.org/coll-40) by Bing.
Answer: The statement you are asking about holds for $n=2$ (Schoenflies theorem) and it fails in higher dimensions.
Schoenflies theorem for $S^2$ states that every Jordan curve $J$ in $S^2$ is "standard", i.e. there exists a homeomorphisms $S^2\to S^2$ sending $J$ to a round circle. From this, it follows that the closure of each component of $S^2-J$ is homeomorphic to the closed 2-disk.
However, it fails in higher dimensions. The first example was constructed by J.W.Alexander and is called Alexander horned sphere. It is a subset $\Sigma\subset S^3$ homeomorphic to $S^2$, such that the closure of one component $C_1$ of $S^3-\Sigma$ is homeomorphic to the closed 3-ball, while the closure of the other component ($C_2$) is not. In fact $C_2$ is not even homeomorphic to $R^3$ as it is not simply connected.


Amazon EC2 Linux AMI: What is the third column of yum list installed?

Question: Output of yum list installed, rightmost column:
Most of them say installed, some say @amzn-main, and some say @amzn-updates.
What is the meaning of this? It says tmux is @amzn-main, but I have been running it. So is it actually installed or not?
I'm trying to compile zsh 5.0.2, but its configure script is complaining about not finding ncurses. ncurses is listed as @amzn-updates. I have been looking around the system for its files without much luck, and sudo yum install ncurses gives me
Package ncurses-5.7-3.20090208.11.amzn1.x86_64 already installed and latest version

Answer: The third column is which repository the package is installed from. All packages listed are installed. However, sometimes if there is more than one version available from different repositories, it will list which repository. Sometimes using yum list installed there is an annoying installed instead of an actual repository.
Here are some ways of discovering the repository for a given installed package:
rpm -qi {packagename}

or
repoquery -i {packagename}

References:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/add-repositories.html
https://stackoverflow.com/questions/635869/can-yum-tell-me-which-of-my-repositories-provide-a-particular-package
Answer: I'm not familiar with EC2, but since it based on CentOS, I guess that it is the repo id. You can list all configured repositories by running:
yum repolist all

I'm trying to compile zsh 5.0.2, but its configure script is
  complaining about not finding ncurses

Sounds like it complains about the header files and libraries, so you probably want to install ncurses-devel:
sudo yum install ncurses-devel

Comment: Okay, but does it mean that it is actually installed? What repo do the packages marked `installed` come from?
Comment: Install `yum-utils` then run `repoquery -i ncurses`, it will tell you where `ncurses` come from.
Comment: Yeah it looks like the reason that `zsh`'s `configure` failed to find `ncurses` was that it's installed in the somewhat non-standard `/lib/` dir instead of `/usr/lib/` or wherever. Still scratching my head about this (maybe I just gotta put some symlinks in `/usr/lib`), but I really am already spending too much time trying to set up an environment which is really realistically a transient one anyway.
Comment: Don't want to ask the same question again. Hence the comment. What is the difference between `@amzn-main`, `@amzn-updates` and `installed`?


convert PHP(JSON) to ASP code

Question: Can someone convert below code to ASP format?
<?php

$data = '
[
  {
    "A": "test",
    "B": "test",
    "C": "test"
  },
  {
    "A": "test",
    "B": "test",
    "C": "test"
  }
]
';

print($_GET['callback'] .'('. $data .')');

while I was testing cross domain restriction workaround, this code works fine with PHP server (of course) , thing is I'd like to implement this under ASP environment.
I tried here http://www.me-u.com/php-asp/hosting/asp.php though.
Thanks in advance.
Comment: I corrected the upper code sample. I had made a small mistake there. ;-)
Answer: Like, once?
<%
Dim data

''// VBScript Strings cannot span multiple lines, we must use
''// concatenation ("&") and line continuation markers ("_")
''// (also, double quotes need to be escaped by *double* double quotes)
data = "" & _
  "[" & _
  "  {" & _
  "    ""A"": ""test""," & vbCrLf & _
  "    ""B"": ""test""," & vbCrLf & _
  "    ""C"": ""test"""  & vbCrLf & _
  "  }," & vbCrLf & _
  "  {" & vbCrLf & _
  "    ""A"": ""test""," & vbCrLf & _
  "    ""B"": ""test""," & vbCrLf & _
  "    ""C"": ""test""" & vbCrLf & _
  "  }" & vbCrLf & _
  "]"

Response.Write Request.QueryString("callback") & "(" & data & ")"
%>

The whole string concatenation business can be avoided if you are going to print out all of it anyway (e.g. there is no further use for it being in a variable):
<%
Response.Write Request.QueryString("callback") & "("
%>
[
  {
    "A": "test",
    "B": "test",
    "C": "test"
  },
  {
    "A": "test",
    "B": "test",
    "C": "test"
  }
]
<%
Response.Write ")"
%>

convert PHP(JSON) to ASP code


Como saber se path existe?

Question: Uma das classes do meu sistema é criada com as informações de um arquivo.
Ao iniciar o sistema, ele executa, inicialmente, as seguintes linhas:
public List<Estabelecimento> listaEstabelecimento(string ibge)
{
    List<Estabelecimento> listaestabelecimento = new List<Estabelecimento>();
    var caminho = HttpContext.Current.Server.MapPath("~/Content/txtCnes/" + ibge);
    //como saber se a variável 'caminho' é um diretório válido? caso não seja, como criá-lo?
    StreamReader file = new StreamReader($"{caminho}/lfces004.txt", Encoding.GetEncoding("iso-8859-1"));

Quero saber se a variável caminho é um diretório válido, e caso não seja, quero que o sistema crie ele.
Comment: @ItaloRodrigo Então realmente é o que eu suspeitava, não precisa saber se ele existe, precisa tentar criar e tratar se der erro, por isso eu respondi da forma adequada de fazer isto.
Comment: Italo até deixei da forma como você editou, mas, não retire os `namespaces` muitos não tem conhecimento aonde fica cada comando é importante sempre uma resposta completa ... ! blz! fiz daquela forma para demonstrar mais o que deveria acontecer, o mais simples possivel. de um contexto geral
Comment: Porque irei criar uns arquivos dentro do diretório
Comment: E por que quer saber se é válido? Precisa desta informação ou precisa de outra coisa e acha que ela será útil? Geralmente não precisa saber disto. É usar e boa. Se der erro você trata. Geralmente soluções que tentam saber se é válido é erro de programação, ainda que quase todo mundo cometa ele.
Comment: Por que criar o diretório no caso de sua inexistência?
Answer: Pelo código apresentado a forma correta de tratar isso é capturando a exceção gerada após tentar usar o caminho. Não fazendo isso pode incorrer em condição de corrida, portanto outras formas produzem códigos que funcionam na maioria das vezes, mas pode falhar, portanto é programação errada.
Quem me conhece aqui sabe que refuto o abuso de exceções, mas esse é um dos casos que a exceção é o melhor mecanismo e curiosamente os programadores se recusam usar.
Então ficaria assim:
public List<Estabelecimento> listaEstabelecimento(string ibge) {
    List<Estabelecimento> listaestabelecimento = new List<Estabelecimento>();
    var caminho = HttpContext.Current.Server.MapPath("~/Content/txtCnes/" + ibge);
    //como saber se a variável 'caminho' é um diretório válido? caso não seja, como criá-lo?
    try {
        var file = new StreamReader($"{caminho}/lfces004.txt", Encoding.GetEncoding("iso-8859-1"));
        // provavelmente terá algo para fazer aqui para fazer sentido
    } catch (DirectoryNotFoundException) {
         WriteLine("o caminho não existe");
         return null; // provavelmente, não vi todo o código
    }
}

Coloquei no GitHub para referência futura.
Se desejar pode tratar outras exceções que este código pode gerar conforme mostra a documentação. Se deseja outro comportamento tem que adaptar ou teria que ser explicitado na pergunta. A resposta foi dada ao que foi perguntado.
Note que não sei nem se deveria tentar capturar exceção para esse caso, se a pergunta fosse mais clara quanto ao contexto e objetivo eu poderia dar uma resposta mais precisa.
Se fosse o caso de criar um diretório apenas, como havia em outra resposta agora apagada, além de não precisar verificar alguma coisa nem precisaria tratar exceção para saber se o diretório existe ou não, porque se ele existe não dá erro, ele apenas usa o que já existe. Se não existe ele cria, só isto.
Houve uma contestação, agora apagada, sobre haver um problema de condição de corrida na geração do caminho antes de se tentar acessar o diretório, mas não importa, só o acesso gerará um problema, qualquer manipulação do texto do caminho não causa problema de condição de corrida porque o texto não é o recurso externo, o diretório é que é o recurso externo e naturalmente compartilhado. O que não é compartilhado nunca gera uma condição de corrida.
Por isso eu perguntei qual era o objetivo. Com a edição ficou claro que haveria condição de corrida. Se ele só quisesse saber se o diretório existe ou não e não fosse usá-lo diretamente então o Directory.Exists() seria adequado, por isto ele existe. O que não pode é pegar sua existência e usar esta condição para assumir uma ação que depende dela continuar igual. Isso é condição de corrida.
Eu entendo que mesmo programadores experientes costumam errar em condição de corrida porque ela é complicada e difícil de reproduzir quando não é tratada devidamente. Como é mais comum ela não correr do que ocorrer os programadores acham que não tem problema não tratar corretamente. Até que acontece e aí fica achanado que é bug do compilador, do sistema operacional ou algo assim. Recomendo reler ou fazer novas perguntas específicas sobre condição de corrida.

Como saber se path existe?


How to check if string (current URL) includes a few substrings

Question: I need to check if current URL includes a few substrings. My code works but I need a tip if it possible to make it better. I realize that duplicating code is not a good practice. I am working with Angular 6/Typescript/ECMAScript 6.
const currentUrl = this.location.path();
const displayTile = currentUrl.includes('/search-resources') || currentUrl.includes('/calendar') || currentUrl.includes('/yearbooks')? 1 : 2;

Answer: Your code is just fine, if you want to reduce the length of checks, have the substrings in an array. like:
var substrings = ['/search-resources', '/calendar', '/yearbooks']

Test your condition using:
const displayTile = substrings.some((e) => currentUrl.includes(e)) ? 1 : 2

Array.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function.
Comment: @NataliaGurgul: Please consider accepting answers if they solve your question.
Answer: jQuery has $.inArray
var names = ['search-resources', 'calendar'];
if ($.inArray('calendar', names) !== -1) {
    // do stuff
}

Comment: I don't use jQuery
Comment: @NataliaGurgul you can check this link for your better answer https://frontstuff.io/a-better-way-to-perform-multiple-comparisons-in-javascript


Number of Zeroes Inside Unit Circle

Question: Let $a, b, d$ be real numbers and $c$ a complex number and define the quartic polynomial
$$
z^4+z^3 (a+ i b)+z^2c+z (d-ib)+1
$$
I want to know sufficient and necessary conditions for when three of the roots are strictly inside the unit circle $|z| < 1$. By the fundamental theorem of algebra, the product of the roots is one, so there are at most three solutions strictly inside the unit circle. I'd also be happy if I could determine when there are three . Of course there exists an exact formula for determining the roots of a quartic polynomial, but it is extremely messy and doesn't give you the norm of the rooms.
Thanks!
Answer: I think you can try to use Rouchè Theorem in the unitary circle...
Answer: Let $A = a+ib$ and $B = d-ib$.
Given any $a,b,d$, the closed parametric curve
$$ \gamma(t) = - e^{2it} -A e^{it} - B e^{-it} - e^{-2it}, \ 0 \le t \le 2 \pi $$
gives those $c$ for which there is a root on the unit circle.  For $c$ outside the curve, by Rouché there are as many roots inside the unit circle as for $z^2$, namely two.  As you cross the curve (at a regular point), the number either increases or decreases by $1$.   If there are any $c$ that make the number $3$, they will form one or more of the regions into which the curve divides the complex plane.
Comment: Thanks for your help! Just a quick question, if $c$ is outside the curve, how can Rouché's theorem be applied? Thanks!
Comment: Rouché applies when $|c|$ is sufficiently large, as the term $c z^2$ dominates the others.  And then  the number of roots inside the circle stays constant in the unbounded component of the complement of the curve.
Comment: Thanks! Can I also use Rouché to argue that if my polynomial has three roots in the unit circle, then $|A|>|B|$? Because then the polynomial $z^3 A+z B$ has three roots inside the unit circle. Thus my sufficient and necessary conditions are that $c$ is inside the parametric curve you give and $|A|>|B|$?
Comment: No, Rouché gives a sufficient condition, not a necessary one.
Comment: Could I not use the symmetric version of  [Rouché](https://en.wikipedia.org/wiki/Rouch%C3%A9%27s_theorem#Symmetric_version) if $c$ is not large enough? Because based on my numerical trials in Mathematica, these are necessary and sufficient.
Comment: It might be the case here (where $\text{Im}(B) = - \text{Im}(A)$), but I don't see how it could come just from Rouché.
Comment: I think you're right, it doesn't come from Rouché, but I still think it's true. Suppose we have three roots less than one $z_1, z_2, z_3$.  Then from Vieta's formula, the product of the roots is 1 and furthermore $|A| = |z_1+z_2+z_3+(z_1z_2z_3)^{-1}|$ and $|B| = |z_1^{-1}+z_2^{-1}+z_3^{-1}+z_1 z_2 z_3|$. Now I just need to show that $|z_1+z_2+z_3+(z_1z_2z_3)^{-1}| >|z_1^{-1}+z_2^{-1}+z_3^{-1}+z_1 z_2 z_3| $ for any $z_{1}, z_{2}, z_{3}$ with norm smaller than one. Not quite sure how to do that though.

Number of Zeroes Inside Unit Circle


Read older documents - like `tail --follow` - but for a mongodb collection

Question: I am familiar with the MongoDB oplog and change-streams, like so:
  const changeStream = d.db(dbName).collection(dbCollName).watch();

  changeStream.on('change', next => {
    log.info('doc:', next.fullDocument);
  });

but my question is, is there a way to get the most recent 100 documents and then read for changes without missing and documents? Just like tail -f but for a mongodb capped collection?
Answer: As far as I know, this isn't supported by change-streams, so you'd need to write it manually. Something like the following should first fetch the last 100 documents from the database & log those documents before beginning to log the documents that the change stream sees.
  const conn = await mongodb.connect("mongodb://mongo:27017")
  const db = conn.db("test");
  const coll = db.collection(collName);
  // We want to start watching before doing the fetch of the last 100 docs
  const changeStream = coll.watch();
  let changeStreamDocs = [];
  let hasLoggedInitialDocs = false;
  // we need to make sure we don't log the same doc twice if we see if in the change stream & in the `find`
  const initialDocIds = {};
  function logChangeStreamDocs() {
    // We need to wait for `coll.find` to complete before we can start logging the change stream docs
    if (!hasLoggedInitialDocs) return;
    for (const doc of changeStreamDocs) {
      if (initialDocIds[doc._id]) continue;
      console.log(doc);
    }
    changeStreamDocs = [];
  }
  changeStream.on('change', next => {
    changeStreamDocs.push(next.fullDocument)
    logChangeStreamDocs();
  });
  // find the most recent 100 docs. _id: -1 is descending
  const docs = await coll.find({}, { sort: { _id: -1 }, limit: 100 }).toArray();
  for (const doc of docs) {
    console.log(doc);
    initialDocIds[doc._id] = true;
  }
  loggedInitialDocs = true;
  logChangeStreamDocs();

note: I don't have a replica set handy, so I haven't actually tested this. There's probably a few typos in there


Can't Access IPv6 Address on VM from Internet

Question: I'm not very familiar with VM staff (and Windows) but am having the following issue.
Host: CentOS 8
Guest: Windows 10
VM: KVM-QEMU
and
Host: Windows Server 2019 Standard
Guest: Windows 10
VM: Hyper-V
In both cases, I can ping6 to the guest Windows 10 from all my devices in our LAN including an IPv6 gateway but cannot from the Internet. Both host servers are accessible from the Internet through IPv6. traceroute6 reaches our gateway but doesn't proceed to the guests. The following is the sample IPv6 assignments:
CentOS host:
<code>br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.0.111  netmask 255.255.255.0  broadcast 192.168.0.255
    inet6 fe80::d6a2:25e4:256:711  prefixlen 64  scopeid 0x20<link>
    inet6 2001:XXX:YYY::1000:1  prefixlen 48  scopeid 0x0<global>
    ether 0c:c4:XX:XX:2a:cc  txqueuelen 1000  (Ethernet)
    RX packets 737819  bytes 3070877722 (2.8 GiB)
    RX errors 0  dropped 46045  overruns 0  frame 0
    TX packets 336852  bytes 29787637 (28.4 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet6 fe80::fc54:ff:feea:ea1b  prefixlen 64  scopeid 0x2
    ether fe:54:00:XX:XX:1b  txqueuelen 1000  (Ethernet)
    RX packets 19344  bytes 8897740 (8.4 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 300884  bytes 39466247 (37.6 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions
</code>
Windows 10 on KVM-QEMU is assigned to 2001:XXX:YYY::1000:2/48 using the above br0 (bridge).
Does anybody have an idea why this happens?
Both guest Windows can access any IPv6 hosts in the Internet and are on the gateway's NDP table.
Thank you.
Comment: You're supposed to subnet that!
Comment: IPv6 subnets are /64.
Comment: @ChristopherH Um, no, that is not equivalent to saying an IPv4 subnet is /24. This is a much more complex issue than can be discussed in a comment; you need to go read RFC 7421 (and possibly other documents).
Comment: Why is your prefix set to 48? How are you getting network connectivity to your VMs?
Comment: What is your actual network?
Comment: But you only have one LAN segment?
Comment: Because our ISP gave us /48 addresses. On CentOS, I use bridge as describe at https://www.cyberciti.biz/faq/how-to-install-kvm-on-centos-8-headless-server/
Comment: You mean like 2001:XXX:YYY::1000:2/128 for guest?
Comment: All right, I'll try that but why are other servers OK with /48?
Comment: Well, I cannot show you but our ISP is Comcast and we use Ethernet Dedicated Internet.
Comment: /48 v6 subnets are perfectly acceptable to use, so long as all services are on the same segment. However, once you do setup the /48 on a segment, you will need to re-do your IP addressing in order to break up the /48. And @MichaelHampton saying that "IPv6 subnets are /64" is the equivalent to "IPv4 subnets are /24" which is incorrect. A subnet is anything from a /0 (the entire IPv4 space from 0.0.0.0 to 255.255.255.255) all the way to a /32 (a single IP address).
Comment: I got an official message from Comcast that /48 can be used as a segment without subnetting /64.
Answer: Well, I could figure this out after all..
Enabling VM guest Windows 10's "Inbound Rules - Virtual Machine Monitoring (Echo Request - ICMPv6-In)" firewall rule solved this problem. This worked at least with Windows Server but I haven't checked with CentOS yet.


hook for disabling fields on add and edit node for a content type

Question: I have a content type called article where i have 6 fields.
But i need to display and hide this fields based on user group while add and edit node.
I am confused about which hook should i use.I am planning to use hook_page_alter().Please let me know wheather it is the correct hook i am using.Thanks a lot.
Answer: If I understand you right you'd be better off using the Field Permissions module:

The Field Permissions module allows site administrators to set field-level permissions to edit, view and create fields on any entity.

If you do code it yourself you're going to end up implementing most of that modules functionality, so even if you don't use it it's a great place to look for the right hooks to call etc.
Comment: Thanks for the suggestion.I need to filter fields based on organic group.does the  Field Permissions module support organic groups.


PHP and DOM to extract these specific data of a web page

Question: Using PHP and DOM how do I get the PLACE, ADDRESS, LOCALITY, REGION, POSTAL CODE and COUNTRY from the following code ( part of a web page ).
Since now I have developed a part of the code to get other content. This is the code so far.
$dochtml = new DOMDocument();
$dochtml->loadHTMLfile('');
$xpath = new DOMXpath($dochtml);

$descr = $xpath->query('//div[@class="description"]')->item(0);
    print_r($descr->nodeValue);

$abbr  = $dochtml->getElementsByTagName("abbr")->item(0);
    $title = $abbr->getAttribute("title");
    echo $title;

This is the rest of the code.

        PLACE

ADDRESS

            LOCALITY,
            REGION
            POSTAL CODE,
            COUNTRY

UPDATE
I have a tiny problem with the following, in the page there are a lot of  tags however the two tags I want with classes dtstart and dtend as below are the only that are inside the #eventDetailInfo. Unfortunately, not all have the second abbr tag with the class=dtend so it gets the first from the "related events". So my question is how do I restrict it only to this specific id?

Monday, July 16th, 2012

Monday, August 16th, 2012

Answer: From reading the DOMXPath documentation, my suggested solution is outlined below.
Get elements by class
$nodes = $xpath->query('//div[contains(@class, "street-address")]');

Get elements by ID
$node = $xpath->query('//div[@id="someid"]');

Solution
To extract your values you could use something like (working example):
<?php
$html = '

        PLACE

ADDRESS

            LOCALITY,
            REGION
            POSTAL CODE,
            COUNTRY

Monday, July 16th, 2012

Monday, August 16th, 2012

';

$document = new DOMDocument();
$document->loadHTML($html);
$xPath = new DOMXpath($document);

function extractNodeValue($query, $xPath, $attribute = null) {
    $node = $xPath->query("//{$query}")->item(0);
    if (!$node) {
        return null;
    }
    return $attribute ? $node->getAttribute($attribute) : $node->nodeValue;
}

$place = extractNodeValue('div[contains(@class, "fn")]/a', $xPath);
$address = extractNodeValue('div[contains(@class, "street-address")]',$xPath);
$locality = extractNodeValue('span[contains(@class, "locality")]',$xPath);
$region = extractNodeValue('span[contains(@class, "region")]', $xPath);
$postalCode = extractNodeValue('span[contains(@class, "postal-code")]', $xPath);
$countryName = extractNodeValue('span[contains(@class, "country-name")]', $xPath);
$start = extractNodeValue('div[@id="eventDetailInfo"]/div/div/abbr[contains(@class, "dtstart")]', $xPath, 'title');
$end = extractNodeValue('div[@id="eventDetailInfo"]/div/div/abbr[contains(@class, "dtend")]', $xPath, 'title');

var_dump($place, $address, $locality, $region, $postalCode, $countryName, $start, $end);

Output:
string(5) "PLACE" string(7) "ADDRESS" string(8) "LOCALITY" string(6) "REGION" string(11) "POSTAL CODE" string(7) "COUNTRY" string(19) "2012-07-16T21:00:00" string(19) "2012-08-16T21:00:00"

Comment: You have some space around `$place` because you have the `` tag inside, and you don't select it.
Comment: Thank you for this great answer. Please take a look at my edit, I have posted one more thing on how to restrict the results from a specific id.
Comment: @Kaoukkos not entirely sure what you meant, but check my recent edit
Comment: Thanks again! I wasn't very clear, but I need to take the `2012-07-16T21:00:00` instead of the `Monday, August 16th, 2012`
Comment: @Kaoukkos updated `extractNodeValue` function to allow optional returning of attributes.
Comment: Great, I have accepted your answer, before giving you the +100, please tell me why when I echo something that originally has `` tags in it's content, I get only plain text? I am referring to a `` (I did this myself).
Comment: @Kaoukkos uh, there is no `div` with that class in your example? Try turning error reporting on: `ini_set('display_errors', 1); error_reporting(E_ALL);` to see if in fact you're getting errors?
Answer: You were almost done with your code:
<?php

$dochtml = new DOMDocument();
$dochtml->loadHTML('

        PLACE

ADDRESS

            LOCALITY,
            REGION
            POSTAL CODE,
            COUNTRY

');

$xpath = new DOMXpath($dochtml);

$place       = $xpath->query('//div[@class="fn org"]/a')->item(0)->nodeValue;
$address     = $xpath->query('//div[@class="street-address"]')->item(0)->nodeValue;
$locality    = $xpath->query('//span[@class="locality"]')->item(0)->nodeValue;
$region      = $xpath->query('//span[@class="region"]')->item(0)->nodeValue;
$postalCode  = $xpath->query('//span[@class="postal-code"]')->item(0)->nodeValue;
$countryName = $xpath->query('//span[@class="country-name"]')->item(0)->nodeValue;

Live code available here.
Comment: Should check node exists before attempting to access `nodeValue`, else risk: `E_NOTICE Notice: Trying to get property of non-object in __FILE__ on line __LINE__`
Answer: Use PHPQuery or similar libraries if you know CSS selectors.
Comment: He's almost there with DOMXPath


Why is the square root in the forward process of the Diffusion Model?

Question: I am trying to make sense of the diffusion model (one of the videos I watched https://www.youtube.com/watch?v=HoKDTa5jHvg&t=706s). I came across this formula in the model:

This picture clearly explains the components of the formula. My question is the middle term - the mean. Why is the square root? At the very beginning, I thought it had something to do with the normal distribution. In Binominal Distribution,
mean =  =
variance = 2 =
SD =  = square root()
Where p is the probability of success and q = 1 - p.
It looks like this is not relevant. Then I think would it be related to the Root Mean Square? If yes, then why the X needs to be squared to start with?
Would anyone please help?
I have read this paper, https://arxiv.org/pdf/1503.03585.pdf. Perhaps I overlooked, I can't find the answer from it.
Comment: The Normal distribution is highly relevant, because a diffusion is a limit of a random walk and the Normal distribution arises in such limits by virtue of the Central Limit Theorem.  Good keywords for searches are "Brownian Motion" and "Random Walk."
Answer: The square root term in the diffusion function helps to ensure that the mean of the latent representation is the same as the mean of the real data distribution, even though the diffusion process adds noise to the latent representation.
This is important because it allows the diffusion model to learn the data distribution more effectively.
If we did not use the square root term, the mean of the latent representation would gradually shift away from the mean of the real data distribution as the diffusion process progressed. This would make it more difficult for the diffusion model to learn the data distribution.


Getting total amount of prime numbers

Question: I have a simple code with a simple objective. I'm still learning Java and what my code does is to count the total amount of prime numbers of a certain number (example: from 11 - 19 there are 4 prime numbers 11,13,17,19). So far What I've able to do is display the prime numbers (11,13,17,19) but I also want to display "4" as the amount of prime numbers on the range of 11-19. this is my code.
<code> public static void main(String args[]) {

 //get input till which prime number to be printed
  System.out.println("Enter the number till which prime number to be printed: ");
  int limit = 19;

  System.out.println("Printing prime number from 11 to " + limit);

  for(int number = 11; number<=limit; number++){

      if(isPrime(number))

      {
          System.out.println(number );
      }
  }
}

public static boolean isPrime(int number){
    for(int i=2; i<number; i++){
       if(number%i == 0){
           return false;
       }
    }
    return true;
}
</code>
}
Comment: Then you need to add some code which increments a counter every time you find a prime number.
Comment: you might be interested in this:
http://introcs.cs.princeton.edu/java/14array/PrimeSieve.java.html
Answer: <code>int count = 0;
for(int number = 11; number<=limit; number++){
  if(isPrime(number))
  {
      System.out.println(number);
      count++;
  }
}
System.out.println("Number of primes are :"+count);
</code>
Comment: uugh.. I actually did that similar to my last question buy it seems like I put on the wrong side of the code. I stated int counter = 0 inside the for statement, I really have much to learn and practice. Thank you kind sir.

Getting total amount of prime numbers


Binary linear programming with two-dimensional parameter

Question: I am trying to solve a linear programming problem like the following, where $x_i$ represents a binary decision to purchase or not product $i$, and $a_i$ is the utility of product $i$:
$$max \sum_{i=1}^n a_ix_i$$
However, I also have a $n\times n$ matrix $b$, where $b_{ij}$ represents the added (or diminished) overall utility of purchasing products $i$ and $j$ together. Given that all the constraints are one-dimensional (i.e. total cost and total number of products), how can I modify the objective function to better represent the model?
I thought of $max \sum_{i=1}^n x_i(a_i+\sum_{j=1}^n x_j b_{ij})$, but I'm pretty sure this breaks the linearity because of the product $x_i x_j$.
Note: as expected, $b$ is symmetrical.
Answer: You need to introduce a new set of binary variables $y_{ij}$ that take value $1$ if and only if items $i$ and $j$ are both selected.
So your objective function becomes
$$
\sum_{i=1}^n a_ix_i + \sum_{i=1}^n \sum_{j<i} b_{ij}y_{ij}
$$
Don't forget to define variables $y_{ij}$ in your constraints:
$$
y_{ij}\le x_i \\
y_{ij}\le x_j \\
$$
(If $y_{ij}$ takes value $1$, then $x_i$ and $x_j$ also have to take value $1$)


How do I add a custom `labels` for just one tick?

Question: I'm making a chart where the X axis will have '%'.
ggplot(
  data = cars,
  aes(
    x = speed,
    y = dist
  )
) +
  geom_point() +
  scale_x_continuous(
    labels = function(x) paste0(x,'%'),
  )

This produces a chart.

I only want either the first tick or last tick on the X axis to have the '%'. How do I do this?
Comment: If you make this a [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including a representative sample of the data, someone can most likely help out
Comment: @camille Updated
Answer: Just change your function to
labels = function(x) c(paste0(x[1] * 100, '%'), x[-1])

(note you may have to adjust your breaks and/or limits because in the updated example you posted, the first element of x is not plotted, so in that case you would need to do c(paste0(x[1:2] * 100, '%'), x[-(1:2)]))
Comment: Thanks. This changes only the first tick. That's great. How would I write this code so that only the final tick label is formatted? `function(x) c( x[1], paste0(x[-1]*100, '%') )` removes the '%' from the first tick, but none of the others.
Comment: For an answer to my question, see Sri Lakshimi's answer.
Answer: Format the function in labels to get '%' for the first tick label only
labels = function(x) c(paste0(x[1],'%'),x[-(1)]

To get '%' for last tick label only
labels = function(x) c(x[1:length(x)-1] , paste0(rev(x)[1],'%'))


Differential of complex-valued function

Question: If $f(z)=u(x,y)+iv(x,y),\, z=x+iy$, how is the differential $df$ of $f$ defined? I know that for a real-valued function $h(x,y)$, its differential $dh$ is defined as $dh=h_xdx+h_ydy$, but I am not sure what that is for $f(z)$. Thanks!
Answer: As $f=u+iv$, we have$$df=du+idv=u_xdx+u_ydy+i(v_xdx+v_ydy)=(u_x+iv_x)dx+(u_y+iv_y)dy.$$

Differential of complex-valued function


Is $0^\infty$ indeterminate?

Question: Is a constant raised to the power of infinity indeterminate? I am just curious. Say, for instance, is $0^\infty$ indeterminate? Or is it only 1 raised to the infinity that is?
Comment: If we type those expressions into Mathematica, however, it tells us that 0^infinity is 0 and 1^infinity is indeterminate.
Comment: All the answers here assume $0^{\infty}$ is $0^{+\infty}$. [But Wikipedia doesn't assume that.](https://en.wikipedia.org/wiki/Indeterminate_form#Expressions_that_are_not_indeterminate_forms) It claims that $0^{\infty}$ is an indeterminate form because $0^{+\infty}$ has the limiting value $0$, and $0^{-\infty}$ is equivalent to $1/0$, which, as talked about in the same place I linked, is "not commonly regarded as an indeterminate form because there is not an infinite range of values that $f/g$ could approach."
Comment: "Specifically, if $f$ approaches $1$ and $g$ approaches $0$, then $f$ and $g$ may be chosen so that $(1)$ $f/g$ approaches $+\infty$, $(2)$ $f/g$ approaches $−\infty$, or $(3)$ the limit fails to exist. In each case the absolute value $|f/g|$ approaches $+\infty$, and so the quotient $f/g$ must diverge, in the sense of the extended real numbers. (In the framework of the projectively extended real line, the limit is the unsigned infinity $\infty$ in all three cases.)"
Comment: "Similarly, any expression of the form $a/0$, with $a \neq 0$ (including $a = +\infty$ and $a = −\infty$), is not an indeterminate form since a quotient giving rise to such an expression will always diverge."
Answer: No, it is zero.
Consider the function $f(x,y) = x^y$ and consider any sequences $\{(x_0, y_0), (x_1, y_1), \ldots\}$ with $x_i \to 0$ and $y_i \to \infty$. It is easy to see that $f(x_n,y_n)$ converges to zero: let $\epsilon > 0$. For some $N$, $|x_i| < \epsilon$ and $y_i > 1$ for all $i \geq N$, so $|f(x_i,y_i)| < \epsilon$ for all $i\geq N$.
More generally, as $x\to c$ and $y\to \infty$, $x^y$ converges to 0 for $|c|<1$, diverges to infinity for $c>1$, oscillates without converging for $c \leq -1$, and is indeterminate when $c=1$.
Comment: Hm, why does wolfram seem to think this limit DNE? http://www.wolframalpha.com/input/?i=lim+x+-%3E0%2C+y-%3E+inf+of+x%5Ey
Comment: wolfram alpha tells you why with the caveat: depends on the path in complex space.
Comment: user7530 guessed that Skylion intended "positive reals" for $x$ and $y$.  Wolfram Alpha guessed that it means a limit for $x,y$ in the complex plane.  Only Skylion can tell us what she meant.
Comment: Because the question is ambiguous: Mathematica, as it implies, interprets it as a limit of the complex exponential. This is very different than user7530's interpretation as an operation on the extended real numbers, where the limit used to compute it is restricted to positive bases and real exponents.
Comment: Yes, I assumed that $x$ and $y$ are real, and $\infty$ is the positive real infinity. For complex numbers, it's also true that $x^y\to 0$ as $|x|\to 0$ and $y\to \infty$, where again here I mean the positive real infinity.
Comment: ', so' --> Is that because $a^b < a$ if $0 < a < 1$ and $b > 1$?
Answer: Since the question came to the front page again, let's do a complex example, to show that (at least this time) Mathematica is not crazy...
For $t>0$, let $f(t) = t, g(t) = i/t$.  Then
$$
\lim_{t\to 0+} f(t) = 0,\qquad \lim_{t\to 0+}g(t) = \infty
\\
f(t)^{g(t)} = \exp\frac{i\;\log t}{t}
$$
and $\mathrm{Re} \Big(f(t)^{g(t)}\Big)$ (pictured) does not converge as $t \to 0^+$.
Answer: Any constant raised to the power $\infty$ or $\infty$ raised to the power of any constant is undoubtedly always indeterminate. This is because exponentiation $x^y$ is a binary operation from $\mathbb{S} \times \mathbb{S} \to \mathbb{S}$, where $\mathbb{S}$ is a set such that $x, y, x^y \in \mathbb{S}$. To the best of my knowledge, $\infty$ doesn't belong to any known set of numbers, which itself means neither $x$ nor $y$ can ever be $\infty$. That is to say, both $x^\infty$ and $\infty^y$ are indeterminate. Therefore, it follows that both $0^\infty$ and $1^\infty$ are indeterminate.
EDIT: Some people in the comments section are saying $1^\infty =1 $ and $0^\infty = 0$ by taking the help of limits. $\displaystyle \lim_{x\to\infty} 1^x$ is not the same as $1^\infty$. The former gives an approximate value of $1^x$ when $x$ is very close to $\infty$, but $x$ is still defined. Obviously, $\displaystyle \lim_{x\to\infty} 1^x = 1$ but $1^\infty \neq 1$. Both of them have different meanings. Same applies to the case of $0^\infty$ too. The question does not mention anything about limits. So the answer assumes that exact values of $1^\infty$ and $0^\infty$ are being referred to, which are absolutely indeterminate.
Comment: I see your point but your conclusion is not necessarily true. You are correct in saying that $\infty$ is not a number but there are other interpretations of the expression $0^{\infty}$ and $1^{\infty}$. Aside from the answers given below we also view $x^y$ as the number of functions from a set of size $y$ to a set of size $x$. There are no functions from an infinite set to the empty set and there is only one function from an infinite set to a singleton set. In that interpretation $0^{\infty}=0$ and $1^{\infty}=1$.
Comment: @JohnDouma When different mathematical interpretations of the same thing are not in agreement, then it's best not to interpret them mathematically at all. Those things are left out to be analyzed by the philosophers, not mathematicians. There is no rigourous mathematical explanation for the indeterminate forms (although I have attempted to provide one), hence the name.
Comment: That's just it. The different interpretations agree. By your example, $\Sigma_{i=0}^{\infty}\text{something}$ would be indeterminate too.
Comment: (-1) This answer is very incorrect. An indeterminate form can be understood as an expression representing *all possible limits* of an operation performed on *functions*. That is, for some operation $*$, the indeterminate form $a*b$ denotes $\lim_x [f(x)*g(x)]$ where $\lim_x f(x)=a$ and $\lim_x g(x)=b$ and $f,g$ are real-valued functions of real variables. If $f\to0$, $g\to\infty$, then $f^g\to 0$, so we may write $0^\infty=0$. It does not matter that $\infty$ is not in the real numbers.
Comment: @JohnDouma Of course, it is indeterminate. The summation notation that you show is actually $\displaystyle \lim_{n\to\infty} \sum^n_{x=0} f(x)$ and not simply $\displaystyle \sum^\infty_{x=0} f(x)$. In the former case the sum is defined because $x\to\infty$ but $x\neq\infty$. In the latter case, the summation assumes that $x=\infty$ is the upper bound which cannot be true. Although the latter form is widely used, technically it's incorrect in its sense.
Comment: @Jam No it's not incorrect. You are considering the values when the limits are being taken. The question was about simply $1^\infty$ and $0^\infty$. $\displaystyle \lim_{x\to\infty} 1^x$ is not same as $1^\infty$. Same applies to the $0^\infty$ case too. When limits are taken, all indeterminate forms are always defined. Precisely stated, $\displaystyle \lim_{x\to\infty} 1^x \approx 1$ but $\neq 1$ and $\displaystyle \lim_{x\to\infty} 0^x \approx 0$ but $\neq 0$.
Comment: @MisterTux You are misunderstanding what indeterminate forms are and seem to be confusing *indeterminate* with *undefined*. Please read Sec 4.4 of [Stewart's calculus](https://www.google.co.uk/books/edition/Calculus_Early_Transcendentals/BCzLDwAAQBAJ) and [MathWorld: Indeterminate](https://mathworld.wolfram.com/Indeterminate.html). The forms $1^\infty$ and $\frac00$ are indeterminate since limits of these forms could approach any value. The forms $0^\infty=0$ and $\frac10=\pm\infty$ are *not* indeterminate since limits of those forms will always approach certain values. (1/2)
Comment: Expressions representing undefined arithmetic operations in the real numbers are *not* necessarily indeterminate forms, as the other answers on this page demonstrate. See [MSE Q554521](https://math.stackexchange.com/questions/554521/). You are also clearly misunderstanding what limits are. They *are* exact, not approximations. This is the same fallacy that makes people claim that $0.999\ldots$ is *approximately* equal to $1$ despite that they are exactly the same number. See [MSE Q11](https://math.stackexchange.com/questions/11/). (2/2)

Is $0^\infty$ indeterminate?


Find probability density of $\arctan(Y/X)$ if $(X, Y)$ is multivariate normal distributed, where $EX=EY=0$ and $EX^2=EY^2=2$ and $EXY=1$

Question: I calculated density function for (X, Y):
$\frac{1}{2\sqrt{3}\pi}e^{-\frac{x^2-xy+y^2}{3}}$
But if i try to write probability function via integral, it becomes to hard to integrate:
$$\int_{v > \frac{u}{\operatorname{tg}(t)}} e^{-\frac{u^2}{3}}e^{\frac{vu}{3}}e^{-\frac{v^2}{3}}$$
And after integration I'll need to differentiate it to find density...
Answer: It is tempting to utilize the polar transformation:
\begin{equation}
\begin{cases}
X = R\cos\Theta, \\
Y = R\sin\Theta.
\end{cases}
\end{equation}
Then $\arctan(Y/X) = \Theta$. To find the distribution of $\Theta$, it is standard to start with the joint distribution of $(R, \Theta)$. The Jacobian is
\begin{equation}
\frac{\partial(x, y)}{\partial(r, \theta)} = r
\end{equation}
is standard. Hence the joint density of $(R, \Theta)$ is
\begin{align}
f(r, \theta) = \frac{r}{2\sqrt{3}\pi}\exp\left(-\frac{1}{3}(r^2 - r^2\sin\theta\cos\theta)\right), \quad r > 0, \theta \in (0, 2\pi].
\end{align}
Can you take it from here?
Comment: Thanks! After that it's only need to integrate $f$ over r.
Comment: @A.Kat Correct!


How to integrate outlook 2013 calendar?

Question: I'm working in an organization where we have our own exchange server. I'm developing a performance system to our employees using erpal.
I need an integration between exchange and Drupal that allows the employees to use their outlook calendar and tasks as same as erpal calendar and task (I mean the synchronization).
I found some ways doing it by using import and export but I think these ways are for Individual person. I need an automatic synchronization for all employees who have an email in our exchange server and can login to the system.
Is there any setting or module I have do? Or any steps link I can follow?
Answer: Have a look for the following modules:

Click to Schedule

It has sync capabilities for Outlook, Google calendar or iCal for Mac. Meetings are automatically confirmed, added to your Outlook, Google Calendar, or iCal and confirmation emails are sent to both parties.

CiviCRM activities iCalendar feed

Users can access an iCal feed of their upcoming (future) Activities, suitable for subscription via Google Calendar, Microsoft Outlook, and/or similar software.
Comment: hi kenorb
first of all, thank you for your response.
I tried to use -click to schedule but it looks there is a limitation for employees number. + each employee should has an account in timetrade !!

currently am trying the civicrm activities.

How to integrate outlook 2013 calendar?


Where can i see the analytics of an Google App Script Deployment?

Question: I created a small app on top of a spreadsheet (with GAS and HTML, CSS) and I deployed it.
Users can access to it without having to enter in the spreadsheet.
It works really well but i'm not able to see even the basic analytics (for eg. the number of viewers)
Thanks
Answer: Go to the project overview page. In the Google Apps Script web IDE, on the lefmost sidepanel click on Overview.

Also, if you have starred your project, go to https://script.google.com/home/starred
Rather than "viewers" you will see "users". If you have set your web-app as execute as you by anyone even anonymous, you will see only one user, you, as this page show the users that exectuted the scripts like the doGet function and the server-side functions called through google.script.run.
Note: https://script.google.com keep execution logs for the last 7 days. If you need to keep the logs longer you have to use another place to keep these logs, i.e. Cloud Logging (requires a Google Cloud standard project), Google Sheets spreadsheet.
Resources

https://developers.google.com/apps-script/guides/logging

Related

Effective way to debug a Google Apps Script Web App
Comment: That's great. Only problem I can see the active "users" over the last 7 days. It is possible to increase this time frame?


Help with Law of Excluded Middle

Question: I've got a problem with the law of excluded middle, and have a homework question surrounding it.
I normally would never ask, and this is my first time, but I can't for the life of me find an example on my university's website with LEM in it.
Here is the question:
Prove the validity of the sequents below (using LEM)
$$
\vdash (p \to q) \lor (q \to r)
$$
Comment: if $q$ is true, then the left hand side of the disjunction is true.  if $q$ is false, the right hand side of the disjunction is true.  the "law of the excluded middle" assumes these are the only two truth values available to $q$
Comment: yoyo's comment provides the answer in words. Note the particular case:  $\vdash (p \to q) \lor (q \to p) $, which I find can easily confuse people about predicate logic or make them regard it as a meaningless manipulation of symbols.
Answer: We can show that the sequents: $(p \to q) \lor (q \to r)$ is a tautology.
$((p \to q) \lor (q \to r))$
$\iff ((\lnot p \lor q) \lor (\lnot q \lor r))$
$\iff (\lnot p \lor \color{blue}{\bf (q \lor \lnot q)} \lor r)$
$\iff (\lnot p \lor \color{blue}{\bf\text{TRUE}} \lor r)\qquad$ by $\color{blue}{\bf LEM}$ (Either $q$ is true, or else $\lnot q$ is true.)
$\equiv \text{TRUE}$
$\therefore$ The sequent is valid as a tautology.
For a more detailed discussion of the Law of Excluded Middle (LEM), see this Wiki article

Note, one can "build up" (derive) the desired expression from only an application of the law of the excluded middle:
Start with $q \lor \lnot q$.
Introduce the disjunct $\lnot p$, to get $\lnot p \lor (q \lor \lnot q)$.
Through associativity of disjunction, we can express that as $(\lnot p \lor q) \lor \lnot q$.
Introduce another disjunct $r$, to get $(\lnot p \lor q) \lor \lnot q \lor r$, and
Through associativity of disjunction, again, we can arrive at $(\lnot p \lor q) \lor (\lnot q \lor r)$.
Now we need only apply equivalences discussed above to arrive at the proposition: $$(p \to q) \lor (q \to r)$$
Comment: @amWhy, yes, but---how does that use excluded middle?
Comment: The only possible truth values for q include true or false.  If q is true, the entire statement is true, because (q(true) or notq(false)) evaluates true, and (q(false) or notq(true)) evaluates true. Hence, since a disjunctive proposition is true whenever one of the disjuncts is true, the entire proposition is true, regardless of the truth values of any of p q or r.
Comment: @amWhy, sorry, I didn't read your answer closely enough to see that "q or not q" in the middle of it. +1 for you, -7 for me.
Comment: Thank you, but it seems I'm using some strange simplified form of Propositional Logic where I have to write down each step, in a little neat list, with the line is affects. It's very annoying. Edit: I hate enter = submit. Anyway, I meant to finish with: I could do this logic alone, but the restrictions placed by my course make it more difficult than it has to be. It's a Formal Specification and Verification course. I did a year of pure mathematics, but switched to CS, where they make Maths a lot less fun >.>
Comment: @Gerry: no problem...you had me second guessing my understanding of the LEM, though! :)  No -7 for you!!!
Comment: @Clintonio: I hate that "enter" = submit as well!!  (in the comments)...I'm finally getting used to it, but it took awhile! Can you adapt the proof above to suit the requirements of your class?  Each $\iff$ denotes an equivalent expression to the expression immediately above it.  I'm assuming you're familiar with the equivalence of ($p\to q$) and  ($\lnot p \lor q$).  If not, have you used the equivalence $(p\to q$) and $\lnot(p \land \lnot q)$?  From the latter you can obtain the former by DeMorgan's.
Comment: The reasoning here shows that |=(p→q)∨(q→r) (semantic validity).  The author asked for ⊢(p→q)∨(q→r) (syntactically yielded formula).  In other words, he asked it to get shown as a theorem, instead of a tautology.  Though, a completeness theorem does hold for classical propositional logic, you'd need to state it here, and already have proved it for propositional logic, and then you'd have to use it in the proof.  How did this get so many up votes?
Comment: @Doug: I'm not clear what you mean.  The entire "theorem" can be constructed by starting from ($q \lor \lnot q$) alone (i.e., starting from the law of the excluded middle). A true statement remains true regardless of any extention to it through the use of additional disjuncts, whatever they may be.
Comment: A "theorem" in the context of formal logic usually means a well-formed formula which has gotten *derived* using rules of inference or axioms.  Theorems do not necessarily have any reference to "truth" or any other semantic concept.  In order to derive a theorem you can only use rules of inference or axioms of the system.  To show a tautology you may make a reference to truth values as you did, but for theorems of formal logic this only holds if treat the semantic concept of truth as a syntactic entity.
Comment: Thank you all for your help. Doug Spoonwood answered the question within the confines of Propositional Logic with the restricted ruleset we have to play with!
Comment: @amWhy Though the second argument you gave didn't come as what the original post of this question wanted, (given certain strings as abbreviations of wffs) it does qualify as a proof instead of just a semantic argument.  Do you see how your second proof doesn't make any appeal to truth or other semantic concepts necessarily?  That's along the lines of what I meant to say.
Comment: The post wasn't really clear about what was wanted: emphasizing PROPOSITIONAL logic and NOT predicate (which was all anyone used here: propositional logic). The only clue that you pointed out was the turnstile/double turnstile distinction, which I hadn't paid close enough attention to.  Additionally, we try, here, not to solve problems step by step, for students to "copy"...we try to lay out the reasoning, some justification, etc.. There are so many "names" for different rules of inference, we cannot hope to read a user's mind to guess which one s/he knows and stick with that.
Comment: Part of learning is learning to be able to translate an inference, name, definition, etc, to terms one is used to.  Conceptual understanding is the key. If one recognizes the inference at play, one should be able to identify the name for the rule that one has learned. At any rate, I appreciate your constructive criticism; I did not catch what system/approach the user was accustomed to/constrained to.  I do think, though, that asking why others would want to vote up my answer was a little over the top.
Answer: What rules of inference and/or axioms do you have to work with?  If you have a natural deduction system, a possible proof [in Polish notation, Cxy means (x->y), Axy means (x V y), Nx means ~x, Kxy means (x ^ y)] might go)
<code>1  ACpqNCpq form of the law of the excluded middle
2  | Cpq assumption
3  | ACpqCqr 2 alternation/disjunction-introduction
4  CCpqACpqCqr 2-3 Conditional Introduction
5  |    NCpq assumption
6  ||   q assumption
7  |||  Nr assumption
8  |||| p assumption
9  |||| q 6, repetition
10 |||  Cpq 8-9 conditional introduction
11 |||  KCpqNCpq 10, 5 conjunction introduction
12 ||   r 7-11 negation elimination
13 |    Cqr 6-12 conditional introduction
14 |    ACpqCqr 13 alternation introduction
15 CNCpqACpqCqr 5-14 conditional introduction
16 ACpqCqr 1, 4, 15 alternation elimination.
</code>
Comment: Thank you, this is the solution I wanted. (edit: Stupid enter key)

I also recommend learning LateX! Thank you again~

Edit 2: We only had the basic 10 axioms, plus LEM and Reductio Ad Absurdum (sp)? You did the trick only using the basics. Also taught me a few things that my course didn't. Such as line 2 to 3. I didn't realise I could introduce new variables in the introduction of a disjunction. If I did, I wouldn't have been so stuck ¬_¬ Then again, I refuse to spend £100 on a book.
Comment: Glad I could provide some help here!  I'll definitely have to work on learning LaTex.  Could you vote up my answer?

Help with Law of Excluded Middle


How to add custom plugin controls to CkEditor

Question: I have made custom plugin for ckeditor in drupal ckeditor module.
The page where I am displaying CKEDITOR is loaded through AJAX and I am doing a replace div to show the CKEDITOR.
the code for replaceDiv is:
function replaceDiv( div )
{
    if ( editor )
        editor.destroy();

    editor = CKEDITOR.replace( div ,
    {
        toolbar : 'Basic'/*'Basic'*/
    });
}

But, the problem is that in this way I am not getting my custom plugin displayed, I tried adding my control name in the toolbar definition in the array but it seems that I am not doing in the right way since it is not working!
Answer: In ckeditor.config.js you need something like this:
CKEDITOR.editorConfig = function(config) {
  config.extraPlugins = 'vicheck';
  CKEDITOR.plugins.addExternal('vicheck', '/sites/all/plugins/ckeditor/vicheck/');
  config.toolbar_DrupalBasic = [ [ 'Bold', 'Italic', '-', 'vicheck' ] ];
}

where vicheck is a plugin.
This works for me using an old CKEditor module. I don't know if the latest versions have any change.
Comment: using this syntax do i need to modify the replaceDiv() code?
Comment: b/c apparently its not working..
Comment: You have two separate question: add plugins into CKEditor, and use CKEditor on an Ajax page. Try do answer each first. My solution could solve the first problem, for the second I don't know, and I don't know the replaceDiv function. But it looks like you do not give the full config variable to your instanciation.
Comment: yeah thats the actual problem, that i have no idea about instantiation of the config variable, otherwise there is no problem in opening ckeditor through ajax, but my plugin wont show up


Working with Android camera

Question: I'm new to Android and I've started working on video streaming app (from device to pc).
According to what I seen so far, what is better:
1. Using the camera callbacks + encoding to jpeg
2. Using the MediaRecorder (i.e H264 or something else) - I've seen that there are many problems with this API.
tnks
Answer: I was working on the video streaming app and we tried both approaches - note that this was 2-3 years ago.
Option 1. is probably too slow to achieve good rates, we managed around 8-12 fps. Note that this was two years ago and phones were significantly slower.
Option 2. We choose this approach on Androd, it's doable, but there are issues with certain phones.

Working with Android camera


Significant predictor loses significance when second non-significant predictor is entered (log regression)

Question:
Possible Duplicate:
How can adding a 2nd IV make the 1st IV significant?

I am looking at the prediction of relapse by gender.
In a hierarchical log regression, with gender entered in first block, it is significant. When anxiety is entered in second block, both anxiety and gender are insignificant. Interaction terms are also insignificant.
What does this mean? It is clearly not a moderator or mediator effect because anxiety does not predict relapse (even when entered before gender). Is this simply an effect of multicollinearity between gender and anxiety? Because gender is more strongly related to anxiety than to relapse?
Answer: Yes I think it must be because gender and anxiety are correlated. Probably both have a small but statistically significant effect when entered separately but when together in the model the individual effect of either one is not significant.  the tests don't measure their combined effect.
Comment: Are gender and anxiety are correlated? What happens if you do the analysis separately by gender? What size N do you have? Are plots of the data helpful?