Upper Bound Question

Question: I am currently going through Apostol's Calculus Book. Apostol is explaining the upper bound axiom. In example 3, it is stated that the set does not contain a Maximum bur rather only bounded above. My Question is why is 0.999999999999999 not the upper bound making it also the Maximum element ?
Comment: $0.999999999999999 <1$
Comment: do you mean [0.999...](https://en.wikipedia.org/wiki/0.999...)? It equals to 1 exactly but it is not in the sequence set.
Comment: Also, for a proof, say $x$ is any number the set. Then $x<\frac{x+1}2<1$, showing that you can always find a bigger number in the set, so there can be no maximal number.
Comment: @auntyellow Yes that's exactly what I meant. I did not know what it would be equal to exactly one until Adrian and yourself pointed out now. But now that I know this I guess the example makes sense.
Answer: Well, for starters: $0.999999…<1$ as pointed out by @geetha290krm.
Now, the interval $I=[0,1)$ is the subset of $\mathbb{R}$: all $x \in \mathbb{R}$ such that $0\leq x <1$. Note that $1$ is an upper bound of the interval (a number that is larger that the biggest number of the set). For example: $2$, $\pi$, $2022$ and $10^{10}$ are all upper bounds of $I$ since each of those real numbers are less than $1$.
The supremum of an interval is the smallest upper bound of a (upper) bounded set. If the supremum of the set is in the set itself, then the supremum is called the maximum element of the set. For example, in the interval $J=[0,1]$ has $1$ as its maximum element; in $I$, the supremum is also $1$, but since $1 \notin I$ then $1$ is not the maximum element.
Also, $0.999999999999999$ is not an upper bound since $0.999999999999999<1$. You can put as many $9$'s as you like and it still wouldn't an upper bound of $I$, since $0.999999999999999…9<1$.
The only way is to put an infinite number of $9$'s, since $0.\overline{9}=1$.
Comment: The proof is not easy. Some answers from [here](https://math.stackexchange.com/questions/11/is-it-true-that-0-999999999-ldots-1) may not be strict. Please refer to [here](https://en.wikipedia.org/wiki/0.999...#Analytic_proofs) and [here](https://en.wikipedia.org/wiki/0.999...#Proofs_from_the_construction_of_the_real_numbers), and need some knowledge about real analysis.
Comment: Hi, Adrian, First thank you for the answer. However, I am still a bit confused because why can't 0.999 with infinite number of 9's be the upper bound. Furthermore I don't understand how 0.99 with infinite 9's would equal 1.

Upper Bound Question


power of a sets

Question: I need to figure out what is the power of the group of functions from R to R that for each x that is not from Q, it's f(x) belongs to {x,x+1}
<code>I can make a mapping for each function to be Q-->R * (R/Q)--> {x,x+1}
Does it ok?
</code>
Thanks
Answer: For each $x$ in $\mathbb{R}-\mathbb{Q}$, you have two possible values, so the cardinality is at least $2^{|\mathbb{R}-\mathbb{Q}|}$, which is the same as $2^{|\mathbb{R}|}$, also known as $\beth_2$, since $|\mathbb{R}-\mathbb{Q}|=|\mathbb{R}|$. On the other hand, the cardinality is also at most $\beth_2$, since that is the cardinality of the set of all functions $\mathbb{R}\to\mathbb{R}$.
Comment: Thank you for the comment!
Answer: Hint: Do you know the cardinality(power) of the set(group) of functions from $\mathbb{R}$ to $\mathbb{R}$ with no restriction?  Can you see how to argue that this must be the same?  Alternately, you should be able to follow through the argument of the power of the unrestricted functions and make it work the same.
Comment: Thank you for the comment!
Comment: @Nir: I updated the terminology as suggested by Arturo Magidin in another post of yours.

power of a sets


Java 8 stream - merge collections of objects sharing the same Id

Question: I have a collection of invoices :
class Invoice {
  int month;
  BigDecimal amount
}

I'd like to merge these invoices, so I get one invoice per month, and the amount is the sum of the invoices amount for this month.
For example:
invoice 1 : {month:1,amount:1000}
invoice 2 : {month:1,amount:300}
invoice 3 : {month:2,amount:2000}

Output:
invoice 1 : {month:1,amount:1300}
invoice 2 : {month:2,amount:2000}

How can I do this with java 8 streams?
EDIT : as my Invoice class was mutable and it was not a problem to modify them, I choosed Eugene's solution
Collection<Invoice>  invoices = list.collect(Collectors.toMap(Invoice::getMonth, Function.identity(), (left, right) -> {
                left.setAmount(left.getAmount().add(right.getAmount()));
                return left;
            })).values();

Answer: If you are OK returning a Collection it would look like this:
Collection<Invoice>  invoices = list.collect(Collectors.toMap(Invoice::getMonth, Function.identity(), (left, right) -> {
                left.setAmount(left.getAmount().add(right.getAmount()));
                return left;
            })).values();

If you really need a List:
 list.stream().collect(Collectors.collectingAndThen(Collectors.toMap(Invoice::getMonth, Function.identity(), (left, right) -> {
                left.setAmount(left.getAmount().add(right.getAmount()));
                return left;
            }), m -> new ArrayList<>(m.values())));

Both obviously assume that Invoice is mutable...
Comment: Hi! 2 comments... 1) Maybe it should be `list.stream().collect(...)` and 2) You are mutating the original `Invoice` elements, this doesn't look correct to me... You could solve it by adding a copy constructor and using it in the value mapper
Comment: or use `Collectors.collectingAndThen(Collectors.toMap(…), m->new ArrayList<>(m.values()))`…
Answer: If you could add the following copy constructor and merge method to your Invoice class:
public Invoice(Invoice another) {
    this.month = another.month;
    this.amount = another.amount;
}

public Invoice merge(Invoice another) {
    amount = amount.add(another.amount); // BigDecimal is immutable
    return this;
}

You could reduce as you want, as follows:
Collection<Invoice> result = list.stream()
    .collect(Collectors.toMap(
        Invoice::getMonth, // use month as key
        Invoice::new,      // use copy constructor => don't mutate original invoices
        Invoice::merge))   // merge invoices with same month
    .values();

I'm using Collectors.toMap to do the job, which has three arguments: a function that maps elements of the stream to keys, a function that maps elements of the stream to values and a merge function that is used to combine values when there are collisions on the keys.
Comment: If you need an (unmodifiable) List instead of collection, you can use `List.copyOf(result)`
Answer: Collection<Invoice> result = invoices.stream().collect(groupingBy(i -> i.month,
                collectingAndThen(
                    reducing((Invoice i1, Invoice i2) -> new Invoice(i1.month, i1.amount + i2.amount)),
                        Optional::get))).values();

Answer: You can do something like
    Map<Integer, Invoice> invoiceMap = invoices.stream()
            .collect(Collectors.groupingBy(                   // group invoices by month
                    invoice -> invoice.month
            ))
            .entrySet().stream()                              // once you have them grouped stream then again so...
            .collect(Collectors.toMap(
                    entry -> entry.getKey(),                  // we can mantain the key (month)
                    entry -> entry.getValue().stream()        // and streaming all month's invoices
                        .reduce((invoice, invoice2) ->        // add all the ammounts
                                new Invoice(invoice.month, invoice.amount.add(invoice2.amount)))
                            .orElse(new Invoice(entry.getKey(), new BigDecimal(0)))          // In case we don't have any invoice (unlikeable)
            ));

Answer: Here is the solution by My library: abacus-common
Stream.of(invoices)
      .groupBy2(Invoice::getMonth, Invoice::getAmount, BigDecimal::add)
      .map(e -> new Invoice(e.getKey(), e.getValue())) // Probably we should not modify original invoices. create new instances.
      .toList();

Answer: I think if your application do not support lambda than this might be a suitable answer eg (Android minSdkVersion=16 do not support lambda)
public static List<Invoice> mergeAmount(List<Invoice> invoiceList) {
 List<Invoice> newInvoiceList = new ArrayList<>();
  for(Invoice inv: invoiceList) {
    boolean isThere = false;
     for (Invoice inv1: newInvoiceList) {
      if (inv1.getAmount() == inv.getAmount()) {
         inv1.setAmount(inv1.getAmoount()+inv.getAmount());
         isThere = true;
         break;
       }
     }
    if (!isThere) {
        newInvoiceList.add(inv);
    }
 }
  return newInvoiceList;
}


Is it possible to extract fixed effects from the general FGLS fixed effects model?

Question: When I use pggls commmand for general FGLS estimators for panel data from R package plm model, fixef doesn't work. How to solve this problem?
fgls.fe <- pggls(Y ~ x1 + x2 + x3, data = df, model = "within")
summary(fgls.fe)

Error in UseMethod("fixef") :
  no applicable method for 'fixef' applied to an object of class "c('pggls', 'panelmodel')"

Comment: I'm voting to close this question as off-topic because it is not only just about r programming but also lacks essential information for example about what r package is used and does not offer reproducible example.
Comment: There could be a statistical question behind: Are there fixed effects in the general FGLS models and how are they calculated? (The R package questions have been addressed by an edit; a reproducible example does not seem necessary.)
Comment: This model has individual effects and doesn't have time effects. When I use plm(y~x1+x2+x3, data=df, model="within") , "fixef" shows individual fixed effects. As I think, "fixef" support only for plm model(or OLS model). Are there any functions to extract individual fixed effetsc for GLS model?
Answer: It is a blunder in the current version 2.2-3 of package plm:
Function fixef.pggls is not exported/registered.
This was fixed in a newer version of the package, available on CRAN.
(This question + answer should be moved to StackOverflow.)
Comment: it works! Thanks a lot!!


Wordpress Taxanomy get term_id from slug

Question: I know the slug and name of the Taxanomy that i want.
The Category is "portfolio_category" and the slug i'm trying to get the id is "elementary_school".
I want this because i need the id to get all the terms within elementary_school. which i've hardcoded like this. I don't want to hardcode that 24 instead i want to get the id dynamically from the slug in question.
<code>$terms = get_terms( 'portfolio_category', array(
        'orderby' => 'id',
        'parent' => '24',
    )
);
</code>
Answer: Is this what you're looking for?
<code>$parentTerm=get_term_by('slug','elementary_school','portfolio_category');
$terms=get_terms('portfolio_category',array(
    'orderby'=>'id',
    'parent'=>intval($parentTerm->term_id)
));
</code>


All modules except php-mysql are installed in phpinfo

Question: I executed all sort of yum install commands and installed all packages.
[root@myip php.d]#  rpm -qa | grep -i mysql
mysql-server-5.1.69-1.el6_4.x86_64
mysql-libs-5.1.69-1.el6_4.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-devel-5.1.69-1.el6_4.x86_64
mysql-5.1.69-1.el6_4.x86_64
php-mysql-5.3.3-22.el6.x86_64

But in phpinfo i can see all extensions like mssql , pgsql , mongo etc but not mysql
what should i do
PHP pacakges
php-common-5.3.3-22.el6.x86_64
php-pdo-5.3.3-22.el6.x86_64
php-pgsql-5.3.3-22.el6.x86_64
php-pecl-imagick-2.2.2-4.el6.x86_64
php-zts-5.3.3-22.el6.x86_64
php-process-5.3.3-22.el6.x86_64
php-ldap-5.3.3-22.el6.x86_64
php-suhosin-0.9.29-2.el6.x86_64
php-enchant-5.3.3-22.el6.x86_64
php-fpm-5.3.3-22.el6.x86_64
php-cli-5.3.3-22.el6.x86_64
php-pear-1.9.4-4.el6.noarch
php-odbc-5.3.3-22.el6.x86_64
php-pecl-memcached-1.0.0-1.el6.x86_64
php-pecl-xdebug-2.1.4-1.el6.x86_64
php-pecl-geoip-1.0.8-3.el6.x86_64
php-pecl-memcache-3.0.5-4.el6.x86_64
php-embedded-5.3.3-22.el6.x86_64
php-soap-5.3.3-22.el6.x86_64
php-snmp-5.3.3-22.el6.x86_64
php-bcmath-5.3.3-22.el6.x86_64
php-mbstring-5.3.3-22.el6.x86_64
php-imap-5.3.3-22.el6.x86_64
php-xmlrpc-5.3.3-22.el6.x86_64
php-gd-5.3.3-22.el6.x86_64
php-xml-5.3.3-22.el6.x86_64
php-recode-5.3.3-22.el6.x86_64
php-5.3.3-22.el6.x86_64
php-mssql-5.3.3-1.el6.x86_64
php-pecl-apc-3.1.9-2.el6.x86_64
php-pecl-mongo-1.4.0-1.el6.x86_64
php-tidy-5.3.3-22.el6.x86_64
php-mcrypt-5.3.3-1.el6.x86_64
php-dba-5.3.3-22.el6.x86_64
php-intl-5.3.3-22.el6.x86_64
php-pspell-5.3.3-22.el6.x86_64
php-php-gettext-1.0.11-3.el6.noarch
php-mysql-5.3.3-22.el6.x86_64

Comment: So what does `phpinfo()` look like?
Comment: every module is listed there except mysql
Comment: is `extension=m_mysql` uncommented in php.ini?
Comment: @NathanC . there is no extension=m_mysql in ini file. system says that all fil;es are in etc/php.d directory and all r loaded from there. i have same file on localhost and it works there. extensins are now in their separate ini files
Answer: Assuming you installed php-MySQL, and assuming you're using Apache, you might just need to restart Apache (if you haven't already).
I believe in centos, that's:
$ sudo /etc/init.d/httpd restart

Comment: i did that many times
Comment: Gotcha - Are my assumptions right about what you installed? (Are you building a LAMP stack? - can you list relevant installed packages?)
Comment: i have added php packes, what else u want to knopw


How to use different condition's in np.where

Question: I'm using the below code which generates a new column called Category but it is not generating the correct output which i'm looking for.
import pandas as pd
import numpy as np

Order = pd.read_excel (r"C:\Sample.xls", sheet_name='Orders')
Return = pd.read_excel (r"C:\Sample.xls", sheet_name='Returns')

Order['Category'] = np.where(Order['Sales']>10000, 'High',
                        np.where((Order['Sales']>5000) & (Order['Sales']<9999), 'Medium',
                          'Low'))
Order.merge(Return, on='Order ID', how='inner')\
    .groupby(['State','Category']).agg(Sales=("Sales", sum)).reset_index()

OUTPUT

REQUIRED OUTPUT
Comment: look into [`pandas.cut`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html)
Comment: and [`numpy.select`](https://numpy.org/doc/stable/reference/generated/numpy.select.html) instead of nested `np.where`?
Comment: It looks for first look good, can you be more specific with problem?
Comment: Is there a specific reason to use np.where() ? I can see you are already using pandas and you can use simpler way to filter dataframes.
Comment: @SAL requirement is to use only np.where().
Comment: @jezrael the **Category** from the **OUTPUT** is giving me only **LOW** but if you check my **REQUIRED OUTPUT** based on the condition it has been categorized.
Comment: What is `print (Order['Sales'].dtype)` ?
Comment: @jezrael it is **float64**
Comment: hmmm, is possible convert values of `OUTPUT` to text?
Answer: Go through your Sales column and add as much as condition (if condition) you want.
for i,e in enumerate(df['Sales']):
  if e < 30:
    df.at[i,'Category'] = 'low'


Geometric Distribution vs Exponential Distribution Curve & relationship

Question: The curve of Geometric Distribution is shaped according to parameter $\alpha$ using equation as
$P_{r}=\frac{ (1-\alpha) \alpha^{CW}} { 1- \alpha^{CW} }. \alpha^{-r}$. Where CW is a parameter (contention window size like 16). So probability for occurrence of r=0 to 15 is is represented as shown in figure for different values of $\alpha$. Here 'number starting from 0 are put at higher side so r='0' has highest probability and r='15' has lowest probability.

Question1. So my question is that exponential distribution also gives curve that matches shape of one of the curves of geometric distribution so how can we find value of $\alpha$ that give same curve as given by exponential distribution.
I have also read in Wikipedia that "The exponential distribution is the continuous analogue of the geometric distribution."  https://en.wikipedia.org/wiki/Geometric_distribution
Question 2. So how can I prove any relationship between them like when exponential distribution equals geometric distribution?.  It is some times also referred as "truncated geometric probability  distribution"
Answer: The geometric and exponential distributions are not the same, since they aren't even defined on the same domain. The geometric distribution lives on a discrete domain, the exponential distribution on a continuous domain. Consequently, you can't make the graphs of their density functions match perfectly. You can at most get them to look similar.
However, there is a reason why they look so similar: both are memoryless distributions, so they're both characterized by their median (at least approximately in the case of the geometric distribution, since its median is not unique in general). The best match between a geometric and exponential function should be when their medians match. The median of the exponential distribution with density function $f:[0,\infty)\to\mathbb R,~f(x)=\frac1\lambda\exp(-\lambda x)$ is $\frac{\ln2}{\lambda}$. Now calculate the median of your geometric distribution and choose $\alpha$ such that the medians match.
Comment: thanks for the reply. Since Geometric Distribution formula does not have $\lambda$, instead $\alpha$, so how can I match them theoretically. Any hints on this would help me. Thanks in advance.
Comment: You can still calculate the median, which will then depend on $\alpha$. Set that equal to $\frac{\ln2}{\lambda}$ and solve for $\alpha$.
Comment: I have used this formula for gettig probability of occurrence of variable 'r' with exponential distribution . It has same parameter 'CW' as in geometric distribution but $\lambda$ is not used here.                                                                                $v2=(double) pow(2,CW)-1$ ,
$u=0.0+ getvalue(Uniform(0.0,1.0))$ and
$r=ceil( log2(1+u*v2)$    Now how can I match exponential equation without $\lambda$ with geometric distribution with $\alpha$
Comment: Still the same: calculate their medians and set them to be equal. Then solve for $\alpha$.
Comment: The median of Geometric Distribution : $ \frac{-1}{log_{2}(1-p)} $ (from Wikipedia), should I put $p$ as probability of geometric distribution, but then it has $CW$ parameter. What value then it should have? .  Also when equating then $ \frac{-1}{log_{2}(1-p)} =\frac{\ln2}{\lambda}$ , $\lambda$ is not known. Please help, I cannot understand
Comment: The one on Wikipedia is with $CW=\infty$ and calls its parameter $p$ instead of $\alpha$. You have to calculate your medians manually according to the definition of a median: a number such that it's equally likely to get a number below or above this number. And yes, $\alpha$ will depend on $\lambda$. There are more than one exponential distribution, and the best $\alpha$ depends on which one you choose. You choose by picking some $\lambda>0$.
Comment: Thanks once again for giving time. Since i have CW=16 so should i put $\lambda=1/16$ and as $p=\alpha$ . Therefore, Should equation be  like this $\frac{-1}{log_{2}(1-\alpha)} =\ln2 * 16 $
Comment: $CW$ has nothing to do with $\lambda$. Your geometric and exponential distributions are different from those on Wikipedia. You have to calculate the median yourself, since the formula on Wikipedia doesn't hold for your version of the distributions.
Comment: This is my formula for exponential distribution $P(X=x)=\frac{r^x-r^{x-1}}{r^N-1},\,r>1$ and $P(X\le x)=\frac{r^x-1}{r^N-1} $.  It has no $\lambda$ so its median cant be  $\frac{\ln2}{\lambda}$.
Comment: My exponential distribution has this formula $P(X=x)=\frac{r^x-r^{x-1}}{r^N-1},\,r>1$ and $P(X\le x)=\frac{r^x-1}{r^N-1} $ and trunacted geometric probability distribution has this formula $P_{r}=\frac{ (1-\alpha) \alpha^{N}} { 1- \alpha^{N} }. \alpha^{-r}$. Where 'N' is number of element to choose from and 'r'=probbaility of picking a number r from N numbers. Now to solve for $\alpha$ i need median of both distributions equated. What would be the final equation to solve for $\alpha$ and get its value in range 0 to 1?


Find all degree solutions in the interval 0° ≤ θ < 360°

Question: Need help with a math problem. Any help is greatly appreciated.
Find all degree solutions in the interval $0° ≤ θ < 360°$. If rounding is necessary, round to the nearest tenth of a degree. Use your graphing calculator to verify the solution graphically. (Enter your answers as a comma-separated list.)
$5\sin^2 θ − 6 \cos 2θ = 0$
I used the following double angle formula to make the entire equation sin:
$1-2\sin^2θ=0$

$5\sin^2 θ-6(1-2\sin^2θ)=0$
$5\sin^2θ+12\sin^2θ-6=0$
$17\sin^2θ-6=0$
$17\sin^2θ=6$
$\sin^2θ=6/17$
$\sinθ=\pm \sqrt{6/17}$
After taking the inverse of $\sin$ I got
$θ = 36.5°, 130.3°$
Comment: You will get 2 solutions for $\sqrt{6/17}$ and 2 more solutions for $-\sqrt{6/17}$.
Comment: Your calculations are correct.
Comment: Correct except Two things 1) $1-2 \sin^2 \theta= \cos 2 \theta$ instead; and 2) See @AlexZom 's comment.
Answer: you could also do $$0 = 5 \sin^2 t - 6 \cos 2t =\frac 52\left(1-\cos 2t\right) - 6\cos 2t \to \cos 2t = \frac 5{17}  $$
solutions are $$2t = \pm \cos^{-1}\left(\frac5{17}\right) + 360^\circ k =\pm 72.8953^\circ+360^\circ  k$$ that is $$t = 36.4476, 143.552^\circ, 216.447^\circ,323.552^\circ $$


what is the degree of zero?

Question: What is the degree of zero? For example, x^2 has degree 2 and the number 5 can be represented as 5x^0 so the number 5 has order 0. But what would the degree of zero be? When multiplying, exponents are added, but when multiplying by 0 this is clearly not the case because 0*5x=0. If the exponents were added the degree would have to be one but this is not the case.
Comment: Various conventions for the degree of the zero polynomial have various advantages and disadvantages. For example, for nonzero polynomials, note that if $f$ divides $g$ then $\deg f \le \deg g$. Since every polynomial divides the zero polynomial, this suggests  setting $\deg 0 = +\infty$! But one has to check every statement about degrees case by case for any proposed dimension. For example, no choice for $\deg 0$ will make $\deg f' = (\deg f)-1$ hold for every polynomial $f$ (it fails either for $f=1$ or $f=0$).
Answer: One may define it to be $-\infty$ (Jacobson, BAI), so that $\deg(fg)=\deg f+\deg g$ always holds, using the convention that $n+(-\infty)=-\infty+n=-\infty+(-\infty)=-\infty$ for any natural number $n$. Constants are usually given zero degree.
Comment: One wonders why $-\infty$ was chosen, since $+\infty$ has the same property.
Answer: Others define that zero (or any other constant) has no degree at all.
Comment: Non-zero constant polynomials are generally assigned degree $0$.
Comment: Yes, I have seen that too, makes sense.

what is the degree of zero?


Decoding Aztec barcode

Question: I scanned an Aztec code and got the following result:
AQEpKCzOLQMf85T0bLRyJEZ3qukry/dsKwebnG2xESMj8JSSgmc=
I would like to know the encoded format and if possible the readable text to see what information is hidden behind the code!
Thanks.
Comment: Isn't it the data specified by the person who printed the barcode according to his own rules? Is there any information that the data applies to something in the barcode specification called Aztec? If there is such information, it would be better to analyze it based on it. If it is someone's own unique rule, it will be difficult to get advice and answers without the information of that rule.
Answer: Your scanned data is formatted in Base64. You can decode it to binary format in almost any programming language or tool like https://www.base64decode.org/
I tried to decode the value but, it isn't readable in any common character sets. You'll have to supply more information on to get any relevant answers. The author of barcode data would be your best bet (what is the usage of the code?)
Comment: This is used by the public transportation department as a throwaway one-way ticket in subways of Tehran. Probably contains a serialized token of some sort.
Comment: How could you see this information?

Decoding Aztec barcode


Is there a Hibernate annotation that can screen for invalid MySQL dates?

Question: I'm using Hibernate 4.0.1.Final and MySQL 5.1.  My domain object contains a java.util.Calendar object mapped to a DATETIME column ...
<code>@NotNull
@Column(name = "START_DATE")
private Calendar startTime;
</code>
Occassionally, I'll get an exception when the datetime value is out of the range of MySQL DATETIMEs ...
<code>org.hibernate.exception.DataException: Data truncation: Incorrect datetime value: '2099-01-01 00:00:00' for column 'START_DATE' at row 1
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:71)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy40.executeUpdate(Unknown Source)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2757)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3268)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:78)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:304)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:615)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:608)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:604)
    at com.myco.myproject.dao.EventsDaoImpl.saveEvent(EventsDaoImpl.java:64)
    at com.myco.myproject.services.EventServiceImpl.saveEvent(EventServiceImpl.java:64)
    at com.myco.myproject.services.EventServiceImpl.refreshEventsDb(EventServiceImpl.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy37.refreshEventsDb(Unknown Source)
    at com.myco.myproject.services.EventsServiceTest.refreshAllEvents(EventsServiceTest.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '2099-01-01 00:00:00' for column 'START_DATE' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2939)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
    at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    ... 62 more
</code>
Is there a way to add an annotation to my Hibernate Java column to screen for this exception?  I'm hoping that eventually I can add some logic to validate the domain object before attempting to insert it into the database.
Thanks, - Dave
Answer: Are you sure your column is DATETIME, not TIMESTAMP? According to documentation "2099-01-01 00:00:00" is valid date for DATETIME (upper limit is "9999-12-31 23:59:59") but not valid for TIMESTAMP (upper limit is "2038-01-19 03:14:07").
Comment: Ah, it was a TIMESTAMP column.  I switched my table's column type to DATETIME and the problem went away.  Thanks, -


How to add an image to a term in a HOOK_install() of a module

Question: I was able to add a taxonomy term in the .install file in the hook_install() function, like this:
use Drupal\taxonomy\Entity\Term;

class feature_basic_page_class{

  const feature_basic_page_terms = array('default', 'inverted');
  const feature_basic_page_vid = 'basic_page_layout';
}

function feature_basic_page_install(){
  $feature_basic_page_terms = feature_basic_page_class::feature_basic_page_terms;
  $feature_basic_page_vid = feature_basic_page_class::feature_basic_page_vid;
  foreach($feature_basic_page_terms as $feature_basic_page_term){
    $term = Term::create(array(
      'name' => $feature_basic_page_term,
      'description' => '',
      'parent' => array(0),
      'vid' => $feature_basic_page_vid
    ));
    $term->save();
  }
}

function feature_basic_page_uninstall(){
  $feature_basic_page_terms = feature_basic_page_class::feature_basic_page_terms;
  $feature_basic_page_vid = feature_basic_page_class::feature_basic_page_vid;
  $terms = array();
  foreach($feature_basic_page_terms as $feature_basic_page_term){
    if($terms_name = taxonomy_term_load_multiple_by_name($feature_basic_page_term, $feature_basic_page_vid)){
      $terms = array_merge($terms, $terms_name);
    }
  }
  foreach($terms as $term){
    $term->delete();
  }
}

I use a class because I tried a global variable and it was not working I don't know why...
Anyway, now I need to do the same thing for another module but this time the taxo term has an additional image field. I tried a few things but no success at this point:
use Drupal\taxonomy\Entity\Term;

class feature_carrousel_class{

  const feature_carrousel_terms = [
    array('name' => 'left', 'overlay' => 'images/BRAVA_Image_Forme.png'),
    array('name' => 'right', 'overlay' => 'images/BRAVA_Image_Forme.png')
  ];
  const feature_carrousel_vid = 'layout';
}

function feature_carrousel_install(){
  $feature_carrousel_terms = feature_carrousel_class::feature_carrousel_terms;
  $feature_carrousel_vid = feature_carrousel_class::feature_carrousel_vid;
  foreach($feature_carrousel_terms as $feature_carrousel_term){
    $term = Term::create(array(
      'name' => $feature_carrousel_term['name'],
      'field_overlay_image' => $feature_carrousel_term['overlay'],
      'description' => '',
      'parent' => array(0),
      'vid' => $feature_carrousel_vid
    ));
    $term->save();
  }
}

function feature_carrousel_uninstall(){
  $feature_carrousel_terms = feature_carrousel_class::feature_carrousel_terms;
  $feature_carrousel_vid = feature_carrousel_class::feature_carrousel_vid;
  $terms = array();
  foreach($feature_carrousel_terms as $feature_carrousel_term){
    if($terms_name = taxonomy_term_load_multiple_by_name($feature_carrousel_term['name'], $feature_carrousel_vid)){
      $terms = array_merge($terms, $terms_name);
    }
  }
  foreach($terms as $term){
    $term->delete();
  }
}

I get this error:
The website encountered an unexpected error. Please try again later.
Error: Call to a member function getFileUri() on null in Drupal\image\Plugin\Field\FieldType\ImageItem->preSave() (line 317 of core/modules/image/src/Plugin/Field/FieldType/ImageItem.php).
Drupal\image\Plugin\Field\FieldType\ImageItem->preSave() (Line: 244)
Drupal\Core\Field\FieldItemList->delegateMethod('preSave') (Line: 202)
Drupal\Core\Field\FieldItemList->preSave() (Line: 449)
Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod('preSave', Object) (Line: 406)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('presave', Object) (Line: 435)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 291)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object) (Line: 389)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 747)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 358)
Drupal\Core\Entity\Entity->save() (Line: 24)
feature_carrousel_install()
call_user_func_array('feature_carrousel_install', Array) (Line: 391)
This sound like a bad path for my image, but I don't know how to call my image correctly. My images are located at the root of my module in an images/ folder.
Answer: An image field is basically an entity reference field.
So what you do is you create a file entity first, save that, and then assign the file id or file entity object to your image field, plus alt/title if you want that.
http://realityloop.com/blog/2015/10/08/programmatically-attach-files-node-drupal-8 seems like a pretty good blog post about that. nodes or terms doesn't a difference here.
The critical part ( to make drupal answers moderators happy ;)):
use \Drupal\node\Entity\Node;

// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);

// Create node object with attached file.
$node = Node::create([
  'type'        => 'article',
  'title'       => 'Druplicon test',
  'field_image' => [
    'target_id' => $file->id(),
  ],
]);
$node->save();

That assumes fetching the file data from somewhere, there are also examples that create it with an already existing file.
Comment: You are my Drupal Super Star! It was indeed the other way. I put in my module images sub dir the images I need, then with the magic `$uri  = file_unmanaged_copy('public://source.jpg', 'public://destination.jpg', FILE_EXISTS_REPLACE);` I was able to achieve my goal. Thanks


when a button_field is presses, how do I access the node that owns the button?

Question: I have a button (button_field) on a content type which is rendered by a view.  The view will display a list of items and each will have a clickable button.  When the user clicks a button, I need write a rule that will set a node reference (attached to the use) to the node that was clicked.  the button_field provides several rules for detecting button clicks, but I have yet to find a rule action or rule token that gives me access to the fields on the 'node that was clicked'  How do I gain access to that node?
Edit =============
Link added.  I am using the actions that came with the module (no additional code added.) I was hoping when I used the 'button pressed' action, rules would give me accesses to entity:... so that I could use the fields on the entity (the one with the button that was pressed.)  However, this data option is not available.  Honestly, I could use flags to do this, but I thought that a flag was a bit more functionality than I needed.
Comment: If the button is a submit button, it depends what data is passed to the submit handler from that form. Presumably the node data is in the `$form_state` there.
Comment: I believe since you are clicking from a View and not the actual node, none of the node data is loaded, therefore none of that data is available to Rules. You could use it as a form submit with a custom module or have a link to the node instead and use the button on the node itself.
Comment: Could you link to the module that provides button_field? Or paste your code if it's your custom solution? Also, if it is a module, but triggers your own snippet of code, one in which you need to access node, please share that snippet.
Comment: There really is no custom code.  I am creating a rule using the UI which is based on a button click. Usually when you build a rule, the system gives you a 'triggering node' variable.  This does not exist when using button trigger.
Answer: I was trying to do something similar. It looks like as you can have button_field on any entity, you need to DEFINE what entity has button_field in your rules. It worked for me thinking like that.
What I mean is in your rules' conditions section after checking 'Clicked button comparison' with your_button_field_name if you check 'Entity is of type' Node and 'Content is of type' Your_content_type, then all of the fields of your content type including nid will be available in Actions section under entity. Check out Using Button Field


Sem a opção de arredondar pra zero em C#

Question: Eu olhei a documentação da microsoft e vi que a biblioteca Math tem MidpointRouding.ToZero para arredondar os numeros para inteiros, porem no meu visual studio nao existe esse metodo, apenas o .AwayFromZero e .ToEven, alguem ja se depararou com esse problema ou sabe como eu posso resolver para meus valores nao serem arredondados para maiores??
Comment: Edite a pergunta e adicione o código para que possamos ver a forma que você está implementando.
Comment: Por favor, esclareça seu problema ou forneça detalhes adicionais a fim de destacar exatamente o que você precisa. Da forma como está escrito atualmente é difícil dizer com exatidão o que você está perguntando.
Answer: Muito provavelmente você está consultando a página da Microsoft para a versão .NET 5/6 e você está usando o .NET Framework.
.NET 5/6

.NET Framework


Uncaught Error - Enviando email usando nodemailer e Angularjs

Question: tenho quebrado cabeça há algum tempo tentando criar uma pagina de contatos onde o usuário através da pagina consegue enviar um email. Sou nova com node e por isso tenho passado por alguns problemas. No momento não consigo que minha aplicação reconheça o nodemailer e estou tendo a seguinte mensagem de erro:

Uncaught Error: Module name "nodemailer" has not been loaded yet for context: _. Use require([])

Segue meu código para analise. Por favor me ajudem, já passei por vários tutoriais e fóruns mas não consigo resolver o problema.

//CONTROLLER

(function() {
 'use strict';
 var ContatoController = ['$scope', '$http', function($scope, $http){

  $scope.formulario = {};
  $scope.formulario.nome;
  $scope.formulario.email;
  $scope.formulario.assunto;
  $scope.formulario.mensagem;

  $scope.enviaEmail = function() {
   $http({
      method: 'POST',
      url: '/contact-form',
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      data: {
       contactNome: $scope.formulario.nome,
             contactEmail: $scope.formulario.email,
             contactAssunto: $scope.formulario.assunto,
             contactMensagem: $scope.formulario.mensagem
      }
    })
    .success(function(data, status, headers, config) {
           console.log("Mensagem enviada com sucesso!");
          }).
          error(function(data, status, headers, config) {
           console.log("Não deu certo.");
          });
  };
}];

angular.module('contactForm').controller('ContatoController', ContatoController);
})();

//CONTROLLER-SERVER

'use strict';

var nodemailer = require('nodemailer');

app.post('/enviaEmail', function(req, res) {
 var transporter = nodemailer.createTransport({
  host: 'mail.meudominio.com',
  port: '465',
  secure: true,
  auth: {
   user: 'email@example.com',
   pass: '****'
  }
 });

 exports.enviaEmail = function(req, res) {
  var data = req.body;

     transporter.enviaEmail({
         from: data.contactEmail,
         to: 'email@example.com',
         subject: data.contactAssunto + ' - Enviado por: ' + data.contactNome,
         text: data.contactMensagem
     });

     res.json(data);
 };
});

//ROUTES

'use strict';

 angular.module('contactForm')
  .exports = function(app) {

   var core = require('js/server/core.server.controller.js');
      app.route('/contact-form').post(core.enviaEmail);
  };
Comment: Qual a sua versão do `Node.js`? Você pode verificar a versão usando o comando `node -v` no seu `prompt`
Comment: A versão é v8.12.0.
Comment: Já tentou reinstalar seu `Node.js`?
Comment: A resposta ajudou a resolver o problema e pode sanar dúvidas similares de outros usuários? Caso positivo não esqueça de marcar como aceita. Pra fazer isso é só clicar no ✓ do lado esquerdo da mesma (abaixo do indicador de up e down votes).
Answer: Me parece que você está fazendo uma confusão entre as responsabilidades do Angular e do Node.js. O NodeMailer é um pacote que deve ser executado do lado do servidor, ou seja, pelo Node.js. O Angular deve requisitar ao Node.js que envie o e-mail, fazendo uso de uma rota preestabelecida.
O seu arquivo de rotas no Node.js ficará similar ao seguinte:
const controller = require('../controller/email.controller');
const express = require('express');

const router = express.Router();

router.post('/enviarEmail', controller.enviarEmail);

module.exports = router;

E o seu controller que está referenciado na rota será similar a:
const { createTransport } = require('nodemailer');
const { promisify } = require('util');

const configurar = () => {
  const { sendMail } = createTransport({
    host: 'mail.meudominio.com',
    port: '465',
    secure: true,
    auth: {
      user: 'email@example.com',
      pass: '****',
    }
  });

  return promisify(sendMail);
};

const enviarEmail = async (req, res) => {
  const { body: { contactEmail, contactAssunto, contactNome, contactMensagem } } = req;
  const enviar = configurar();

  const info = await enviar({
    from: contactEmail,
    to: 'firstname.lastname@example.com',
    subject: `${contactAssunto} - Enviado por: ${contactNome}`,
    text: contactMensagem,
  });

  res.json({ info, mensagem: 'E-mail enviado com sucesso.' });
};

module.exports = {
  enviarEmail,
}

Note que como seu código não é executável fiz apenas uma sugestão de implementação que não tem garantias de funcionamento, afinal não pude testá-lá. Mas note as alterações que deixam claro quais as responsabilidades esperadas do servidor.


DiffEQ with various quadratic terms

Question: I have the following DiffEQ and would like to solve it,
$$2F\left( \frac{{d}^{2}}{d{z}^{2}}F\right) -{\left( \frac{d}{dz}F\right) }^{2}+2F\left( \frac{{d}^{2}}{d{y}^{2}}F\right) -{\left( \frac{d}{dy}F\right) }^{2}+2F\left( \frac{{d}^{2}}{d{x}^{2}}F\right) -{\left( \frac{d}{dx}F\right) }^{2}=0$$
Any help would be appreciated, Thanks.
Comment: You could have just said $F\,\Delta F=\frac{1}{2}\|\nabla F\;\|^2$.
Answer: Note that constant $F$ works. Otherwise, assuming $F > 0,$ your condition is just
$$  \Delta \sqrt F = 0.      $$ So you want some positive harmonic function $G$ and then $F = G^2.$ Similar for $F < 0, \; F = - H^2, \; \; \Delta \sqrt {-F} = 0.$
From Liouville's theorem, if you want a solution on all of $\mathbb R^3$ then $G$ and $F$ are constant. So, if you start with a nonconstant harmonic $G,$ such as linear, along a finite set of surfaces $G$ actually becomes $0$ and everything goes sideways.
EDIT, Thursday: Looking again, it is not so bad when the function is $0,$ as long as we are squaring, so the gradient is also $0.$ So, a solution, and possibly all solutions, are some real constant $c$ and harmonic $W,$ then
$$  F = c W^2.   $$

DiffEQ with various quadratic terms


There exist $\beta \in$ Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$ such that $\phi'(f)=\beta \circ \phi(f) \circ \beta^{-1}$

Question: Let $\phi, \phi' : \mathbb{Z}/3\mathbb{Z} \rightarrow$ Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$ 2 non-trivial homomorphisms. Show that there exist $\beta \in$ Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$ such that $\phi'(f)=\beta \circ \phi(f) \circ \beta^{-1}$ $\forall f \in \mathbb{Z}/3\mathbb{Z}$. We know that Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z}) \cong GL(2,\mathbb{F}_2) \cong S_3$ and that we must have $\phi([1]_3) = \sigma \in S_3$ such that $\sigma$ is a 3-cycle (if not we have a trivial homomorphism). Then i have problem to show that there exist $\beta \in$ Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$ such that $\phi'(f)=\beta \circ \phi(f) \circ \beta^{-1}$ $\forall f \in \mathbb{Z}/3\mathbb{Z}$. I don't really see what happens when we want to ''jump'' from $S_3$ to Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$. Thanks in advance
Answer: It was proven in that question that $\mathrm{Aut}(\Bbb Z / 2 \Bbb Z \times \Bbb Z / 2 \Bbb Z)$ is isomorphic to $\mathcal S_3$. Therefore, it is enough to prove the result for $\mathcal S_3$ instead of $\mathrm{Aut}(\Bbb Z / 2 \Bbb Z \times \Bbb Z / 2 \Bbb Z)$.
A non-trivial homomorphism $\phi \in \mathrm{Hom}(\Bbb Z / 3 \Bbb Z, \mathcal S_3)$ is fully defined by $\phi(1)$ as $\Bbb Z / 3 \Bbb Z$ is cyclic. And $\phi(1)$ is a 3-cycle $\sigma_\phi$as the 3-cycles are the only elements of order 3 in $\mathcal S_3$. In $\mathcal S_3$, there are only two 3-cycles. Namely $\sigma_1 = (1 \ 2 \ 3)$ and $\sigma_2 = (1 \ 3 \ 2)$. Therefore $\mathrm{Hom}(\Bbb Z / 3 \Bbb Z, \mathcal S_3)$ also has only two non-trivial elements $\phi_1,\phi_2$ with $\phi_1(1) = \sigma_1$ and $\phi_2(1) = \sigma_2$.
Now in $\mathcal S_n$, any two cycles of the same lengths are conjugate. Applying the result to our specific case, it exists $\beta \in \mathcal S_3$ such that $\sigma_2 = \beta \circ \sigma_1 \circ \beta^{-1}$. By the way $\beta = (2 \ 3)$ is such an element.
This conjugacy relation means that
$$\phi_2(1) = \beta \circ \phi_1(1) \circ \beta^{-1}$$ and implies
$$\phi_2(2) = \phi_2(1+1)=(\beta \circ \phi_1(1) \circ \beta^{-1})^2=\beta \circ (\phi_1(1))^2 \circ \beta^{-1} = \beta \circ \phi_1(2) \circ \beta^{-1}$$
As $$\phi_2(0) = \mathrm{Id}= \beta \circ \phi_1(0) \circ \beta^{-1}$$
we have proven the desired result.
Comment: if $\Phi$ is an isomorphism between $\mathrm{Aut}(\Bbb Z / 2 \Bbb Z \times \Bbb Z / 2 \Bbb Z)$ and $\mathcal S_3$, you can use it to move back and forth between those two groups.
Comment: Thank you very much, you clarified everything i wanted to know!
I just have 1 more question: If we wanted to construct a morphism $\mathbb{Z}/3\mathbb{Z} \rightarrow$ Aut$(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$, would it be enough to construct it between  $\mathbb{Z}/3\mathbb{Z}$ and $S_3$ or we have to ''dive'' from S_3 to Aut($(\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z})$ in this case? My question might be stupid, i'm sorry, but i just want to construct a right intuition on this. Thank you very much
Comment: Thank you very much! Have a nice day


Perl INC not updated when running with T flag

Question: I'm trying to modify PERLLIB in order to give access to the folder where a certain library is.
In order to do so I'm modifying the PERLLIB variable:
export PERLLIB=/some/folder

Now if I do:
perl
print "$_\n" for @INC;

The new folder is displayed. However, If I do
perl -wT
print "$_\n" for @INC;

The new folder is not displayed. Is there a way to modify INC variable with env variables with the T flag?
Answer: No. That would defy the point. The whole purpose of -T is to avoid letting the caller execute arbitrary code, and giving them control of @INC would allow just that.
$ cat script
#!/usr/bin/perl
use strict;
use warnings;
print "Hello World\n";

$ printf '%s' 'print "owned\n";' >strict.pm

$ PERL5LIB=. script
owned
Hello World

Since -T is usually used by setuid scripts, allowing the caller to modify @INC would allow the caller to execute arbitrary code as the script owner.
You can use either of the following (but the script's setuid and setgid bits will be ignored):
perl -I/some/folder -T script

perl -Mlib=/some/folder -T script

You could add the following to the script itself if it's a setuid/setgid script:
use lib '/some/folder';

Make sure to use an absolute path, or you'll effectively invalidate -T.
Answer: The perlsec man page has this to say on Taint mode and @INC.

When the taint mode (-T ) is in effect, the "." directory is removed from @INC, and the environment variables PERL5LIB and PERLLIB are ignored by Perl. You can still adjust @INC from outside the program by using the -I command line option as explained in perlrun. The two environment variables are ignored because they are obscured, and a user running a program could be unaware that they are set, whereas the -I option is clearly visible and therefore permitted.
Another way to modify @INC without modifying the program, is to use the lib pragma, e.g.:
 perl -Mlib=/foo program

The benefit of using -Mlib=/foo over -I/foo, is that the former will automagically remove any duplicated directories, while the latter will not.
Note that if a tainted string is added to @INC, the following problem will be reported:

Insecure dependency in require while running with -T switch
Comment: @ikegami: Yeah, I realised that as I posted it. I'll work up a documentation patch.
Comment: That first sentence is partially outdated. `.` is no longer in `@INC` to be removed.


CNAME to S3 not working

Question: I have set up a CNAME record to point to an S3 bucket.  It's been up for 4 days, and the results of a DIG Lookup show that it is in place:
;; QUESTION SECTION:
;railroad.odehengineers.com.    IN  A

;; ANSWER SECTION:
railroad.odehengineers.com. 3600 IN CNAME   railroad.odeh.s3.amazonaws.com.
railroad.odeh.s3.amazonaws.com. 60 IN   CNAME   s3-directional-w.amazonaws.com.
s3-directional-w.amazonaws.com. 3613 IN CNAME   s3-1-w.amazonaws.com.
s3-1-w.amazonaws.com.   60  IN  A   220.127.116.11

I created a test page test.html which should be visible from both:

http://railroad.odeh.s3.amazonaws.com/test.html
http://railroad.odehengineers.com/test.html

But only the first, direct URL works.  The other gives a 404.
Any thoughts?  Do I need to coordinate at all with Amazon?  Or am I just misunderstanding the purpose of a CNAME record?
Answer: http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
Comment: I read this briefly before creating the CNAME, but didn't notice the requirement that the S3 bucket must have the same name as the CNAME.  Thanks for pointing me there.
Answer: It sounds like you need to configure host headers for the site to account for both URL's. Also, it's probably not the best idea to create a CNAME that points to another CNAME.
Comment: What do you mean "configure host headers to account for both URLs"?
Comment: +1, it's a Host Headers (sometimes called Virtual Host) problem, it needs to be fixed on S3. Also, CNAME to a CNAMe isn't so bad, but piling them up will cause noticeable delays eventually.

CNAME to S3 not working


Showing $ \sum_{n=0}^{\infty} \frac{1}{(3n+1)(3n+2)}=\frac{\pi}{3\sqrt{3}}$

Question: I would like to show that:
$$ \sum_{n=0}^{\infty} \frac{1}{(3n+1)(3n+2)}=\frac{\pi}{3\sqrt{3}} $$
We have:
$$ \sum_{n=0}^{\infty} \frac{1}{(3n+1)(3n+2)}=\sum_{n=0}^{\infty} \frac{1}{3n+1}-\frac{1}{3n+2} $$
I wanted to use the fact that $$\arctan(\sqrt{3})=\frac{\pi}{3} $$ but $\arctan(x)$ can only be written as a power series when $ -1\leq x \leq1$...
Comment: Technically, $\arctan x$ can be written as a power series valid around any given point. It's just that the Maclaurin series - the one with the coefficients you want - only converges in $[-1,1]$. Also, it happens that your series is $L(1,\chi_2)$, where $\chi_2$ is the unique nontrivial character modulo 3.
Answer: Regularized the series:
$$ \begin{eqnarray}
   \sum_{n=0}^m \frac{1}{(3n+1)(3n+2)} &=& \sum_{n=0}^m \left( \frac{1}{3n+1} - \frac{1}{3n+2} \right) = \sum_{n=0}^m  \int_0^1 \left( x^{3n} - x^{3n+1} \right) \mathrm{d} x \\
    &=& \int_0^1 \left( \frac{(1-x^{3m+3}) (1-x)}{1-x^3} \right) \mathrm{d} x =
         \int_0^1  \frac{1-x^{3m+3}}{1+x + x^2} \mathrm{d} x
\end{eqnarray}
$$
Now we can take the limit by dominating convergence theorem:
$$
  \sum_{n=0}^\infty \frac{1}{(3n+1)(3n+2)} = \int_0^1 \frac{\mathrm{d} x}{1+x+x^2} = \left.\frac{2   \sqrt{3}}{3} \arctan\left(\frac{2x+1}{\sqrt{3}}\right)\right|_0^1 = \frac{\pi}{3 \sqrt{3}}
$$
Comment: Clever. The actual computations are probably not so terribly different from my idea.
Comment: Proceeding per your suggestion leads to more complicated integrals: $\sum_{n=0}^\infty \frac{1}{(3n+1)(3n+2)} = \int_0^1 \mathrm{d} x \int_0^x \mathrm{d} y \sum_{n=0}^\infty y^{3n} =  \int_0^1 \mathrm{d} x \int_0^x \frac{\mathrm{d} y }{1-y^3}$.
Comment: Um, yeah. The inner one leads to one integral like yours plus a logarithm, but the outer one will then take more work. You win.
Comment: Thank you for this nice answer! However woundn't it rather be: $$ \int_0^1  \frac{1-x^{3(m+1)}}{1+x + x^2} \mathrm{d} x $$ ?
Comment: Yes, you are correct. I will edit the post.
Answer: What do you get if you differentiate $$\sum_{n=0}^\infty \frac{x^{3n+2}}{(3n+1)(3n+2)}$$ twice?
Comment: Why do I like answering a question with a question?
Comment: Why not?  [And now the system makes me spoil the joke by forcing me to type more.]
Comment: Type \ \ \ \ \ \ between dollar signs.
Comment: Too bad about your spoiled joke. But to be serious, I think it is pedagogically sound. [I tried responding "Why?" ending with a bunch of non-breakable spaces, but the system saw through the subterfuge and rejected it.]
Comment: @AndréNicolas Use `\color{white}{\text{ and some extra space here }}` and no one will notice.
Answer: An important trick here is that sigma and integral signs can be changed around.
$$\int \sum^b_{n=a} f\left(n,x\right)\, dx = \sum^b_{n=a} \int f\left(n,x\right) \,dx$$
And this is because
$$\int \sum^b_{n=a} f(n,x)\, dx$$
$$\int f\left(a,x\right) + f((a+1),x) + f((a+2),x) + \dots +f\left((b-1),x\right) + f(b,x) $$
$$ = \int f(a,x)\,dx + \int f((a+1),x) \,dx + \dots + \int f((b-1),x)\, dx + \int f(b,x)\, dx$$
Therefore
$$\begin{align*}
   \sum_{n=0}^m \frac{1}{(3n+1)(3n+2)}
    =& \sum_{n=0}^m \left( \frac{1}{3n+1} - \frac{1}{3n+2} \right) \\
    =& \sum_{n=0}^m  \int_0^1 \left( x^{3n} - x^{3n+1} \right) \mathrm{d} x \\
    =& \int_0^1 \sum_{n=0}^m \left( x^{3n} - x^{3n+1} \right) \mathrm{d} x \\
    =& \int_0^1 \left( \frac{(1-x^{3m+3}) (1-x)}{1-x^3} \right) \mathrm{d} x =
         \int_0^1  \frac{1-x^{3m+3}}{1+x + x^2} \mathrm{d} x
\end{align*}
$$
Also because
$$ \sum_{n=0}^m \left( x^{3n} - x^{3n+1} \right) = \frac{(1-x^{3(m+1)})(1-x)}{1-x^3} $$
Now let us see how the final integral
$$\sum_{n=0}^\infty \frac{1}{(3n+1)(3n+2)} = \int_0^1 \frac{\mathrm{d} x}{1+x+x^2} $$
is evaluated.
$$ x^2+x+1 = \left(x+\frac{1}{2}\right)^2+ \left(\frac{\sqrt{3}}{2}\right)^2$$
therefore if you skip two steps of substitution and do it once
$$ x+\frac{1}{2} = \frac{\sqrt{3}}{2} \tan \theta$$
$$ dx = \frac{\sqrt{3}}{2} \sec^{2} \theta$$
$$
\begin{eqnarray}
\int \frac{dx}{1+x+x^2} =  \int \frac{ \frac{\sqrt{3}}{2} \sec^{2} \theta}{\frac{3}{4} \sec^{2} \theta} {\mathrm{d} \theta}
   &=& \frac{2}{\sqrt{3}} \theta \\
   &=& \frac{2}{\sqrt{3}} \tan^{-1} \left(\frac{2x+1}{\sqrt{3}}\right)
\end{eqnarray}
$$
$$ \Rightarrow \int_0^1  \frac{\mathrm{d} x}{1+x+x^2} = \frac{2\sqrt{3}}{3} \left( \tan^{-1} ( \frac{3}{\sqrt{3}} ) - \tan^{-1} ( \frac{1}{\sqrt{3}} ) \right)$$
$$ = \frac{2\sqrt{3}}{3} \left( \frac{\pi}{3} - \frac{\pi}{6} \right) = \frac{\pi}{3\sqrt{3}}$$
Answer: Since it seems to have gone unnoticed I would like to mention another common trick that can be used here, namely to study the function
$$f(z) = \frac{1}{(3z+1)(3z+2)} \pi\cot(\pi z)$$
which has the property that with $S$ being our sum,
$$2S = \sum_n \mathrm{Res}(f(z); z=n) = \sum_n \frac{1}{(3n+1)(3n+2)}.$$
We examine what happens when we integrate $f(z)$ along the rectangle
$$\Gamma =\pm (N+1/2) \pm i N$$
with $N$ a large positive integer.
There are no poles on this contour and seeing that $\frac{1}{(3z+1)(3z+2)}\in\Theta(1/N^2)$ on the contour, the integral $$\int_\Gamma f(z) dz$$ goes to zero as $N$ goes to infinity.
This implies that the sum of the residues at the poles of $f(z)$ is zero, giving (observe the two simple poles at $z=-1/3$ and $z=-2/3$)
$$2S + \frac{1}{3}\pi\cot(-\pi/3) - \frac{1}{3} \pi\cot(-2\pi/3) = 0$$
so that $$ S = \frac{\pi}{6} (\cot(-2\pi/3)-\cot(-\pi/3))
= \frac{\pi}{6} \frac{2}{\sqrt{3}} = \frac{\pi}{3\sqrt{3}}.$$
To convince yourself that the integral really does vanish consider the two lines $\Gamma_1$ which is $N+1/2\pm iN$ (right vertical) and $\Gamma_2$ which is $\pm N+1/2+iN$ (top horizontal).
We parameterize $\Gamma_1$ with $z=N+1/2+it$ so that
$$\left|\int_{\Gamma_1} f(z) dz \right|
= \left|\int_{-N}^N f(N+1/2+it) i dt\right|.$$
The fractional term attains its maximum at $t=0$ when we cross the real axis, giving an upper bound on the norm which is
$$\frac{1}{(3N+5/2)(3N+7/2)}.$$
For the norm of the trigonometric term we get
$$|\pi\cot(\pi (N+1/2) + \pi it)|
=\pi\left|\frac{e^{i\pi (N+1/2) - \pi t}+e^{-i\pi (N+1/2) + \pi t}}
{e^{i\pi (N+1/2) - \pi t}-e^{-i\pi (N+1/2) + \pi t}}\right|
\\ = \pi\left|\frac{i(-1)^N e^{- \pi t} - i(-1)^N e^{\pi t}}
{i(-1)^N e^{- \pi t} + i(-1)^N e^{\pi t}}\right|
= \pi|\tanh(\pi t)|.$$
Observe that with $t$ real $\tanh(\pi t)$ has no poles and its norm is bounded by one. Therefore the norm of the integral along $\Gamma_1$ is bounded by
$$2N \times \frac{1}{(3N+5/2)(3N+7/2)} \in \Theta(1/N)$$
and the integral vanishes as $N$ goes to infinity as claimed.
For $\Gamma_2$ we parameterize with $z = t + i N$ so that
$$\left|\int_{\Gamma_2} f(z) dz \right|
= \left|\int_{-(N+1/2)}^{N+1/2} f(t+iN) dt\right|.$$
The two factors of the fractional term are minimized when they cross the imaginary axis at $t=-1/3$ and $t=-2/3$, giving an upper bound on the norm which is
$$\frac{1}{3N\times 3N} = \frac{1}{9N^2}.$$
For the norm of the trigonometric term we get
$$|\pi\cot(\pi t + \pi i N)|
= \pi\left|\frac{e^{i\pi t - \pi N} + e^{-i\pi t + \pi N}}
{e^{i\pi t - \pi N} - e^{-i\pi t + \pi N}}\right|
\le \pi\left|\frac{e^{\pi N}+e^{-\pi N}}{e^{\pi N}-e^{-\pi N}}\right|
=\pi|\coth(\pi N)|.$$
There aren't any poles here either and this term is bounded above by $\pi\coth(\pi)$ because $N>1.$ This gives the following bound on the norm of the integral along $\Gamma_2:$
$$(2N+1) \times \frac{1}{9N^2} \times \pi\coth(\pi) \in \Theta(1/N)$$
and this integral also vanishes as $N$ goes to infinity as claimed.
The other two line segments can be bounded by the same technique.
Comment: I use the function $\pi\cot(\pi z)$ in [this answer](http://math.stackexchange.com/a/868179). However, avoiding contour integration made things simpler.
Answer: $\newcommand{\angles}[1]{\left\langle\, #1 \,\right\rangle}
 \newcommand{\braces}[1]{\left\lbrace\, #1 \,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\, #1 \,\right\rbrack}
 \newcommand{\dd}{{\rm d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\dsc}[1]{\displaystyle{\color{red}{#1}}}
 \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}
 \newcommand{\half}{{1 \over 2}}
 \newcommand{\ic}{{\rm i}}
 \newcommand{\imp}{\Longrightarrow}
 \newcommand{\Li}[1]{\,{\rm Li}_{#1}}
 \newcommand{\pars}[1]{\left(\, #1 \,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\vphantom{\large A}\,#2\,}\,}
 \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\, #1 \,\right\vert}$
\begin{align}&\color{#66f}{\large%
\sum_{n=0}^{\infty}\frac{1}{\pars{3n + 1}\pars{3n + 2}}}
=\frac{1}{9}\sum_{n=0}^{\infty}\frac{1}{\pars{n + 1/3}\pars{n + 2/3}}
=\frac{1}{3}\bracks{\Psi\pars{\frac{2}{3}} - \Psi\pars{\frac{1}{3}}}
\end{align}
where $\ds{\Psi}$ is the Digamma Function.
With Euler Reflection Formula:
\begin{align}&\color{#66f}{\large%
\sum_{n=0}^{\infty}\frac{1}{\pars{3n + 1}\pars{3n + 2}}}
=\frac{1}{3}\bracks{\pi\cot\pars{\pi\,\frac{1}{3}}}
=\color{#66f}{\large\frac{\pi}{3\root{3}}}
\end{align}
because $\ds{\cot\pars{\frac{\pi}{3}} = {1 \over \root{3}}}$.
Answer: There is another way to solve the problem. Note
$$ (3n+1)(3n+2)=9\left[(n+\frac{1}{2})^2+\left(\frac{i}{6}\right)^2\right] $$
and
and hence
\begin{eqnarray}
\sum_{n=0}^\infty\frac{1}{(3n+1)(3n+2)}&=&\frac19\sum_{n=0}^\infty\frac1{(n+\frac{1}{2})^2+\left(\frac{i}{6}\right)^2}\\
&=&\frac1{18}\sum_{n=-\infty}^\infty\frac1{(n+\frac{1}{2})^2+\left(\frac{i}{6}\right)^2}\\
&=&\frac{1}{18}\frac{\pi\sinh2\pi b}{b(\cosh2\pi b-\cos2\pi a)}\bigg|_{a=\frac{1}{2},b=\frac{i}{6}}\\
&=&\frac{\pi}{3\sqrt3}
\end{eqnarray}
by using the result from this.
Answer: $$
\begin{aligned}
\sum_{n=0}^{\infty} \frac{1}{(3 n+1)(3 n+2)}&=\sum_{n=0}^{\infty}\left(\frac{1}{3 n+1}-\frac{1}{3 n+2}\right) \\
& =\frac{1}{3} \sum_{n=0}^{\infty}\left(\frac{1}{n+\frac{1}{3}}-\frac{1}{n+\frac{2}{3}}\right) \\
& =\frac{1}{3} \lim _{N \rightarrow+\infty}\left(\sum_{n=0}^N \frac{1}{n+\frac{1}{3}}+\sum_{n=-N}^1 \frac{1}{n+\frac{1}{3}}\right)\\
\end{aligned}
$$
Using the result for $z\not \in \mathbb Z$,
$$
\pi \csc (\pi z)=\lim _{N \rightarrow \infty} \sum_{k=-N}^N \frac{(-1)^k}{k+z},
$$
we have
$$\boxed{\sum_{n=0}^{\infty} \frac{1}{(3 n+1)(3 n+2)} = \frac{\pi}{3} \cot \left(\frac{\pi}{3}\right)=\frac{\pi}{3 \sqrt{3}} }$$


Appropriate normality tests for small samples

Question: So far, I've been using the Shapiro-Wilk statistic in order to test normality assumptions in small samples.
Could you please recommend another technique?
Comment: The Wilk in Shapiro-Wilk refers to Martin B. Wilk. It's all too easy to write "Wilks" especially (a) if someone else said or wrote that and you're copying (b) you know about the work in statistics of Samuel S. Wilks, a quite different person (c) you get confused about terminal "s" in English, given its other uses for plurals (statistics, cats, dogs, ...) and possessives ('s), which is common even among those whose first language is English. I've edited this thread to the extent I can; I can't reach into comments.
Comment: Here are a couple of other questions of possible interest: [is-normality-testing-essentially-useless](http://stats.stackexchange.com/questions/2492/), for a discussion of the value of normality testing, & [what-if-residuals-are-normally-distributed-but-y-is-not](http://stats.stackexchange.com/questions/12262/), for a discussion / clarification of the sense in which normality is an assumption of a linear model.
Answer: The fBasics package in R (part of Rmetrics) includes several normality tests, covering many of the popular frequentist tests -- Kolmogorov-Smirnov, Shapiro-Wilk, Jarque–Bera, and D'Agostino -- along with a wrapper for the normality tests in the nortest package -- Anderson–Darling, Cramer–von Mises, Lilliefors (Kolmogorov-Smirnov), Pearson chi–square, and Shapiro–Francia.  The package documentation also provides all the important references.  Here is a demo that shows how to use the tests from nortest.
One approach, if you have the time, is to use more than one test and check for agreement.  The tests vary in a number of ways, so it isn't entirely straightforward to choose "the best".  What do other researchers in your field use?  This can vary and it may be best to stick with the accepted methods so that others will accept your work.  I frequently use the Jarque-Bera test, partly for that reason, and Anderson–Darling for comparison.
You can look at "Comparison of Tests for Univariate Normality" (Seier 2002) and "A comparison of various tests of normality" (Yazici; Yolacan 2007) for a comparison and discussion of the issues.
It's also trivial to test these methods for comparison in R, thanks to all the distribution functions.  Here's a simple example with simulated data (I won't print out the results to save space), although a more full exposition would be required:
<code>library(fBasics); library(ggplot2)
set.seed(1)

# normal distribution
x1 <- rnorm(1e+06)
x1.samp <- sample(x1, 200)
qplot(x1.samp, geom="histogram")
jbTest(x1.samp)
adTest(x1.samp)

# cauchy distribution
x2 <- rcauchy(1e+06)
x2.samp <- sample(x2, 200)
qplot(x2.samp, geom="histogram")
jbTest(x2.samp)
adTest(x2.samp)
</code>
Once you have the results from the various tests over different distributions, you can compare which were the most effective.  For instance, the p-value for the Jarque-Bera test above returned 0.276 for the normal distribution (accepting) and < 2.2e-16 for the cauchy (rejecting the null hypothesis).
Comment: Thanks Shane, great answer! Well, "the others" from my field often use SPSS, so they use Kolmogorov-Smirnov (if they check normality at all), though IMHO the Lilliefors' test is a better choice when the data is gathered from a sample (when parameters are unknown). I was taught that Shapiro-Wilk's is appropriate for small samples, and just wanted to get more info about "small samples normality tests"...  BTW, I use nortest in R! =)
Answer: For normality, actual Shapiro-Wilk has good power in fairly small samples.
The main competitor in studies that I have seen is the more general Anderson-Darling, which does fairly well, but I wouldn't say it was better. If you can clarify what alternatives interest you, possibly a better statistic would be more obvious. [edit: if you estimate parameters, the A-D test should be adjusted for that.]
[I strongly recommend against considering Jarque-Bera in small samples (which probably better known as Bowman-Shenton in statistical circles - they studied the small sample distribution). The asymptotic joint distribution of skewness and kurtosis is nothing like the small-sample distribution - in the same way a banana doesn't look much like an orange. It also has very low power against some interesting alternatives - for example it has low power to pick up a symmetric bimodal distribution that has kurtosis close to that of a normal distribution.]
Frequently people test goodness of fit for what turn out to be not-particularly-good reasons, or they're answering a question other than the one that they actually want to answer.
For example, you almost certainly already know your data aren't really normal (not exactly), so there's no point in trying to answer a question you know the answer to - and the hypothesis test doesn't actually answer it anyway.
Given you know you don't have exact normality already, your hypothesis test of normality is really giving you an answer to a question closer to "is my sample size large enough to pick up the amount of non-normality that I have", while the real question you're interested in answering is usually closer to "what is the impact of this non-normality on these other things I'm interested in?". The hypothesis test is measuring sample size, while the question you're interested in answering is not very dependent on sample size.
There are times when testing of normality makes some sense, but those situations almost never occur with small samples.
Why are you testing normality?
Comment: But normality is *never* satisfied. It's sometimes a reasonable description of the data, but they're not actually normal.While it's sensible to check for non-normality when you assume it, it's not particularly useful to test it (for the reasons I described above). I do a qq-plot, for example, but a hypothesis test answers the wrong question in this situation. t-tests and anova usually work reasonably well if the distributions aren't heavily skew. A better approach might be to use procedures that don't assume normality - perhaps resampling techniques.
Comment: Thanks for a great answer, and a great question afterwards. It's crucial to get an insight about the background of the problem. Well, so many times I've seen people doing t-test, Pearson's r or ANOVA without getting any idea about the shape of distribution (which is often heavy-skewed) - parametric techniques "need" satisfied normality assumption. In psychology (which is my field of interest), we often deal with small samples, therefore I need appropriate normality test.
Comment: Or you can use non-parametric tests, at cost of having less power. And nothing is absolutely satisfied in statistics, it's not solely a normality issue. However, bootstrapping or jackknifing are not a solution when introducing someone to t-test and/or ANOVA assumptions. I doubt that resampling techniques solve normality issues at all. One should check normality both graphically (density plot, boxplot, QQplot, histogram) and "numerically" (normality tests, skewness, kurtosis, etc.). What do you suggest? This is completely off topic, but how would you check, say, ANOVA normality assumptions?
Comment: @aL3xa I think the randomization approach is better appropriate given your research field; notwithstanding the fact that usual parametric tests provide good approximation to exact permutation tests, non-parametric tests also imply some kind of assumption (e.g. on shape of the distribution). I even wonder how we might really define what is a deviation from normality in small-sample study. I think you should ask for further discussion about this point in a separate question.
Answer: There is a whole Wikipedia category on normality tests including:

the Anderson-Darling test, popular amongst statisticians; and
the Jarque-Bera test, popular amongst econometricians.

I think A-D is probably the best of them.
Comment: I agree. I performed a quick test of the A-D test, Jarque-Bera, and Spiegelhalter's test (1983), under the null, with sample size 8, repeating 10,000 times. The A-D test maintains nominal rejection rate, and gives uniform pvals, while J-B test is terrible, Spiegelhalter is middling.
Comment: @shabbychef The Jarque-Bera test relies on asymptotic normality of sample skewness and kurtosis, [which doesn't work well even for n in the low 100s](http://stats.stackexchange.com/a/83426/22228). But to obtain the desired rejection rate you can adjust critical values eg based on simulation results, as in Section 4.1 of Thadewald, T, and H. Buning, 2004, *[Jarque-Bera test and its competitors for testing normality - A power comparison](http://www.econstor.eu/bitstream/10419/49919/1/668828234.pdf)*, Discussion Paper Economics 2004/9, School of Business and Economics, Free University of Berlin.
Answer: For completeness, econometricians also like the Kiefer and Salmon test from their 1983 paper in Economics Letters -- it sums 'normalized' expressions of skewness and kurtosis which is then chi-square distributed.  I have an old C++ version I wrote during grad school I could translate into R.
Edit: And here is recent paper by Bierens (re-)deriving Jarque-Bera and Kiefer-Salmon.
Edit 2: I looked over the old code, and it seems that it really is the same test between Jarque-Bera and Kiefer-Salmon.
Answer: In fact the Kiefer Salmon test and the Jarque Bera test are critically different as shown  in several places but most recently here -Moment Tests for Standardized Error Distributions: A Simple Robust Approach by Yi-Ting Chen. The Kiefer Salmon test by construction is robust in the face of ARCH type error structures unlike the standard Jarque Bera test. The paper by Yi-Ting Chen develops and discusses what I think are likely to be the best tests around at the moment.
Comment: Chen seems to focus on larger datasets, which makes sense because the fourth and sixth and higher moments involved in these tests are going to take some time to settle down to asymptotic levels.  But distributional tests are typically used for datasets smaller than 250 values (the minimum studied in this paper).  In fact, most of them become so powerful with larger amounts of data that they are little more than afterthoughts in such applications.  Or is there more going on here than I am seeing?
Answer: For sample sizes <30 subjects, Shapiro-Wilk is considered to have a robust power - Be careful, when adjusting the significance level of the test, since it may induce a type II error! [1]
Comment: In small samples goodness of fit tests are generally unable to reject normaility.
Comment: @MichaelChernick what happens in the specific case then? What is the reason behind a small small being "classified" as non-normal?
Answer: I read about the z-score intervals:

For small samples (n < 50), if absolute z-scores for either skewness
or kurtosis are larger than 1.96, which corresponds with an alpha
level 0.05, then reject the null hypothesis and conclude the
distribution of the sample is non-normal

(Hae-Young Kim, 2013.)


Altering the Title of node using Preprocess

Question: I am trying to alter the page title of history content type using this function
function mytheme_preprocess_page(&$variables) {
   if(isset($variables['node']) && $variables['node']->type == "your_content_type") {
   drupal_set_title("my_title");
 }
}

But you know what, I dont get anything in node array.
Answer: You could use hook_node_view, which also gives you more flexibility, (e.g. control what view mode / language you affect):
function MY_MODULE_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'your-node-type' && $view_mode == 'full') {
    drupal_set_title('your-title');
}

Instead of using a preprocess function, which is better for template changes, (for example, adding variables to it):

The main role of the preprocessors is to set up variables to be placed
  within the template (.tpl.php) files.

Preprocess and process functions


what are the Streaming APIs

Question: basically I want to understand both high level and also technical point of view as what constitutes a streaming API, there are all sorts of data available but I could not find a satisfactory explanation of streaming API, also how does it differ from general APIs (REST if applicable)
PS:I am not asking about multimedia streaming.
Comment: Compare this question with your other questions to see why i downvoted it.
Comment: because it is not specific and kind of broad question I suppose,
Answer: Kind of a vague question. I guess streaming usually means one of the following (or a combination)

downloading data for immediate consumption, rather than a whole file for storage, potentially with support for delivering partial data (lower quality, only relevant pieces etc), sometimes even without any storage at all in between producer and consumer
a persistent connection that continues to deliver new data as it becomes available, rather than having the client poll

A good example (for the first pattern) are streaming XML parsers (such as SAX). They allow you to handle XML data that is too big to fit into memory (which a DOM parser likes to do).
Answer: I just find another good answer here:
https://www.quora.com/What-is-meant-by-streaming-API
A streaming API differs from the normal REST API in the way that it leaves the HTTP connection open for as long as possible(i.e. "persistent connection"). It pushes data to the client as and when it's available and there is no need for the client to poll the requests to the server for newer data. This approach of maintaining a persistent connection reduces the network latency significantly when a server produces continous stream of data like say, today's social media channels. These APIs are mostly used to read/subscribe to data.

what are the Streaming APIs


What is the distribution of sample means of a Cauchy distribution?

Question: Typically when one takes random sample averages of a distribution (with sample size greater than 30) one obtains a normal distribution centering around the mean value. However, I heard that the Cauchy distribution has no mean value. What distribution does one obtain then when obtaining sample means of the Cauchy distribution?
Basically for a Cauchy distribution $\mu_x$ is undefined so what is $\mu_{\bar{x}}$ and what is the distribution of $\bar{x}$?
Comment: From the [Wikipedia page](https://en.wikipedia.org/wiki/Cauchy_distribution#Transformation_properties), it looks like the sample mean of i.i.d. Cauchy variables would have the same distribution as the samples themselves.
Answer: If $X_1, \ldots, X_n$ are i.i.d. Cauchy$(0, 1)$ then we can show that $\bar{X}$ is also Cauchy$(0, 1)$ using a characteristic function argument:
\begin{align}
\varphi_{\bar{X}}(t) &= \text{E} \left (e^{it \bar{X}} \right ) \\
&= \text{E} \left ( \prod_{j=1}^{n} e^{it X_j / n} \right ) \\
&= \prod_{j=1}^{n} \text{E} \left ( e^{it X_j / n} \right ) \\
&= \text{E} \left (e^{it X_1 / n} \right )^n \\
&= e^{- |t|}
\end{align}
which is the characteristic function of the standard Cauchy distribution. The proof for the more general Cauchy$(\mu, \sigma)$ case is basically identical.
Comment: To help out those who might have trouble connecting some of the details, the step from the second to third line uses independence, the next one uses "identically distributed", the  next one can be done in several ways, but the easiest is to see that the expectation inside the power is the same integral as that for a Cauchy cf but in $t/n$, so (if you already know the cf for a Cauchy) you get $[e^{-|t/n|}]^n$ and then bringing the $n$th power down the $n$ terms cancel.
Comment: I liked that the other answer also explained that this means it is a [stable distribution](https://en.wikipedia.org/wiki/Stable_distribution).
Answer:
Typically when one takes random sample averages of a distribution (with sample size greater than 30) one obtains a normal distribution centering around the mean value.

Not exactly. You're thinking of the central limit theorem, which states that given a sequence $X_n$ of IID random variables with finite variance (which itself implies a finite mean $μ$), the expression $\sqrt{n}[(X_1 + X_2 + \cdots + X_n)/n - μ]$ converges in distribution to a normal distribution as $n$ goes to infinity. There is no guarantee that the sample mean of any finite subset of the variables will be normally distributed.

However, I heard that the Cauchy distribution has no mean value. What distribution does one obtain then when obtaining sample means of the Cauchy distribution?

Like GeoMatt22 said, the sample means will be themselves Cauchy distributed. In other words, the Cauchy distribution is a stable distribution.
Notice that the central limit theorem doesn't apply to Cauchy distributed random variables because they don't have finite mean and variance.
Comment: You said: "*the sample mean of the first n elements convergences in distribution to a normal distribution as n goes to infinity*" ... not exactly. Under weaker conditions than you need for the CLT, the mean itself converges to the constant $\mu$ (by the weak law of large numbers). You have to standardize the mean to get convergence to a normal distribution.
Comment: My comment was intended to be a bit stronger than "sample mean is also Cauchy", because the sample mean will have *the same parameters*. That is, like for a normal distribution, the location parameter will be the same, but unlike the normal case, the *scale* parameter will also be the same (whereas for the normal case, the scale decreases as $1/\sqrt{N}$). At least, this is my interpretation of the first 2 transformation properties listed at my link.
Comment: @DilipSarwate Corrected. Don't forget that you can edit other people's answers.


How do I show using math symbols "get quotient without remainder"

Question: For example, for 10/3, I only want to get 3, not 3.333. Is there a math operation that does this?
Comment: Try $\lfloor 10/3 \rfloor=3$. This is the "floor" operation and gets rid of any decimal.
Comment: http://mathworld.wolfram.com/IntegerDivision.html
Answer: Use the floor function, which takes a rational number and outputs the quotient (hence subtracts the remainder from the rational number): $\lfloor x \rfloor$, where $x$ is a given fraction.
So $\lfloor 10/3 \rfloor = 3$, e.g.
Comment: I see. I don't actually follow sports in Israel, let alone in other countries. I just recognized the Packers symbols from watching a lot of That '70s Show and How I Met Your Mother. :-P
Comment: Go Vikes! ${}{}$
Comment: Today, @Asaf, it's the San Francisco 49'ers! First playoff game preceding the superbowl! ;-)
Comment: @amWhy: needs another UV +1
Answer: There are two methods to do this:
1. Use the floor function $\lfloor \frac{a}{b} \rfloor$
2. Subtract the remainder and then divide: $\frac{a - ({a \text{ mod } b})}{b}$


Deploy root/trusted publisher certificate file to windows machines

Question: I have a self-signed .cer certificate that I need to deploy to roughly a 1000 machines, running a variety of Windows versions from Windows XP and Server 2003 up to Windows 7 and Server 2008 R2.
SCCM is our normal deployment method, and have tried deploying these a few different ways and am bumping against the fact that either not all the required tools are available on all OS's or I'm just getting error messages on the automatic deployment that I don't see running them manually.
The most cross-platform friendly method that I've found is to grab a copy of CertUtil.exe and certadm.dll from a copy of XP SP3 that has the admin tools installed and drop that in the same folder as the .cer file, then use the following command lines in a batch file:
certutil.exe -addstore TrustedPublisher cert.cer

certutil.exe -addstore root cert.cer

This works fine manually running the batch file, but comes back with a variety of errors running it automatically.
Is there a better way to do this cross Windows platforms?
Answer: As you have that many machines it seems reasonable to assume you're use an AD domain, in which case you can push the certs out using Group Policy Objects. There are numerous articles describing the steps, such as this one from Technet.
Comment: Thanks, but although we do have group policies, for various reasons that wasn't an option for deploying this cert.
Comment: Why? That's really the best solution you could ask for.
Answer: Following clues from this prior question import certificate using command line in Win XP Home, and a fair bit of playing around, now have this consistently deploying across OS's.
Have extracted the following files from the 32-bit Win Server 2003 SP2 Admin Pack (KB340178) and saved together with the certificate in a folder on the packages source in SCCM:
certadm.dll
certcli.dll
certreq.exe
certutil.exe
cert.cer

Now ditching the batch file and creating two separate programs in SCCM with the following two command lines (both chained together into the one advert) it all seems to be working as an automated rollout across the OS's.
certutil.exe -addstore TrustedPublisher cert.cer
certutil.exe -addstore root cert.cer
Comment: Just be careful not to run this advertisement on computers which already have the certificates added. certutil.exe will prompt for confirmation before overwriting anything. This will result in the advertisement hanging in the background, and prevent any others from running.
Comment: @MichaelSteele CertUtil.exe in Win2k8r2 has a `-f` option to force an `-addStore`


How to Interpret this lagged model?

Question:
Just a Quick Overview there are 2 lagged variables, Unemploy.L1 - lagged for 1 month, Unemploy.L12 lagged for 12 months.
Overview of variables - umemployment (unemploy, in thousands) (date, month of data collection), personal consumption expenditures (pce in billions of dollars), median duration of unemployment (uempmed in weeks) and personal savings rate (psavert)
Can Anyone help me in Interpreting unemploy.L1(1 month Lag), Unemploy.L12(12 month lag) Variables wrt coefficients ?
Answer: I am not sure if I understood your question correctly.
If you are just looking for an interpretation of the output of such regression, it seems that the coefficients for both unemploy.L1 and unemploy.L12 are significant. This is given by the p-values for such coefficients: being "small" (less than 0.001) we can reject the null hypotheses of the coefficients being not significant.
As far as the sign and the magnitude of such coefficients is concerned, short-term unemployment seems to have a positive impact, ceteris paribus, on the response variable. An additional "unit" of unemployment (whatever that means in your case) increases the outcome by 1.06 on average. Instead, long-term unemployment has a small negative effect, but still significant.

How to Interpret this lagged model?


Laravel 8 with Ajax - 405 (Method Not Allowed)

Question: When I try type something in input it is throwing error "405 Method not allowed". How can I fix this?
My route:
Route::get('/search' , [HomeController::class, 'searchPost'])->name('search-posts');

My blade:

<input id="search"  name="search" autocomplete="off" class="form-control" placeholder=" " type="text" style="border-radius: 100rem;font-family:Arial, FontAwesome"  >

My ajax:
$('body').on('keyup', '#search',function(){
            var searchQuest = $(this).val();

            $.ajax({
                method:'POST',
                url: '{{ route("search-posts") }}',
                dataType: 'json',
                data:{
                    searchQuest: searchQuest,
                },
                success: function(res){
                    console.log(res);
                }
            });

        });

My controller:
public function searchPost(Request $request){

        $search_users = User::where('username','like', '%' . $request->get('searchQuest') . '%')->get();

        return json_encode($search_users);

    }

Comment: So `method:'POST'` is causing `Method not allowed` when you have `Route::get`?
Answer: In your Route:
Route::get...

In your AJAX:
 $.ajax({
          method:'POST',
          ...

The methods are differents, that's the error.
Comment: @232323quick2323 You also need to pass the csrf token from ajax.
Comment: I changed Route::post instead of Route::get but now got this error '419 unknown status'


TSQL passing 2 values array to stored procedure

Question: I'm using SQL Server 2012 and C#.
Imagine have something similar to a shopping cart and now need to create an order with the following items:
productA - 4 (qty)
productB - 1 (qty)
productC - 9 (qty)

In my C# code I have a list that looks like this:
id : "productA" , qty : "4"
id : "productB" , qty : "1"
id : "productV" , qty : "9"

Questions:

How can I pass the list of 2 values to the stored procedure?
How can I have the stored procedure run 3 while loops each one running 4 times, then once then 9 times in order to physically create one record x request?

Note: In my case I don't have a QTY column in the table, I need to specifically create one record x item on the order.
Comment: Have a look at [table-valued parameters](https://msdn.microsoft.com/en-us/library/bb675163%28v=vs.110%29.aspx) for passing the list and [recursive CTEs](https://technet.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx) or using a numbers table to generate multiple rows. FWIW, most designs would include a `Quantity` column rather than trying to group rows whenever a count is needed.
Comment: Possible post your table schema here.
Comment: I agree about the QTY but in this case it does not really work. I'll check the Table-Valued param....
Answer:
You can done this by Table Value Parameter in SQL.

Sql Authority
MSDN

You can done this by passing TVP as @table format
declare @table table(product varchar(10), qty int)

insert into @table
   select 'product1', 4 union
   select 'product2', 2

;WITH cte AS (
   SELECT product, qty FROM @table
   UNION ALL
   SELECT product, qty-1 FROM cte WHERE qty > 1
)
SELECT t.product, t.qty
FROM cte c
JOIN @table t ON c.product = t.product
ORDER BY 1

Reference for the CTE : Creating duplicate records for a given table row
Answer: To pass a table into the stored procedure use table-valued parameter.
At first create a type:
CREATE TYPE [dbo].[ProductsTableType] AS TABLE(
    [ID] [varchar](50) NOT NULL,
    [qty] [int] NOT NULL
)

Then use this type in the stored procedure. The @ParamProducts is a table and can be used in all queries where a table can be used.
CREATE PROCEDURE [dbo].[AddProducts]
    @ParamProducts ProductsTableType READONLY
AS
BEGIN
    ...
END

To actually insert required number of rows I would use a table of numbers , http://web.archive.org/web/20150411042510/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html
In my database I have a table called Numbers with a column Number that contains numbers from 1 to 100,000. Once you have such table it is trivial to get the set that you need.
DECLARE @T TABLE (ID varchar(50), qty int);

INSERT INTO @T (ID, qty) VALUES ('productA', 4);
INSERT INTO @T (ID, qty) VALUES ('productB', 1);
INSERT INTO @T (ID, qty) VALUES ('productV', 9);

SELECT *
FROM
    @T AS Products
    INNER JOIN dbo.Numbers ON Products.qty >= dbo.Numbers.Number
;

Result set
ID          qty Number
productA    4   1
productA    4   2
productA    4   3
productA    4   4
productB    1   1
productV    9   1
productV    9   2
productV    9   3
productV    9   4
productV    9   5
productV    9   6
productV    9   7
productV    9   8
productV    9   9

This is an example. In your case you would have this SELECT inside INSERT INTO YourFinalTable.