HTTP_REFERER in htaccess not working in Internet Explorer ( IE)

Question: I have htaccess file with the following rule
RewriteEngine On
RewriteCond %{HTTP_REFERER}   !(inbox/view/\?mail_id).*  [NC]
RewriteRule ^ - [F,L]

This would ensure that the file link will only open if clicked on pages with 'inbox','view' or '?mail' in its url. It's working in major browsers except IE . IE8 and IE9 throws 403 Forbidden Access error.
Any response is appreciated.
Thanks
Comment: You should already _know_ that referer is absolutely unreliable … your IE you're testing with probably just send _none_, and so this house-of-cards "access protection system" you are trying to dilettantishly build here comes tumbling down.
Answer: I believe you should be using
RewriteEngine On
RewriteCond %{HTTP_REFERER}   !(inbox|view|\?mail_id).*  [NC]
RewriteRule ^ - [F,L]

And I suppose that the Forbidden you are receiving is from the [F] in your rule.
Comment: thanks but that is exactly my code you cited. the problem is specific to IE because other browsers abide by that condition.
Comment: This isn't very helful since this code is the same as OP's one.
Comment: No the code is different. The OP has `inbox/view/` with slashes where as this answer has `inbox|view|` with pipes.


crontab bash script execution - Raspberry Pi

Question: I have a bash script that I'm using to execute a python file with a specific version of Python (3.6). The Bash script is currently located on my desktop (/home/pi/Desktop/go.sh)
#!/bin/bash
python3.6 /home/pi/scriptDir/myScript.py

Here is my crontab entry, when I do crontab -l (note, I've deleted my other jobs)
* * * * * bash /home/pi/Desktop/go.sh # JOB_ID_3

When I run this file using the command line or from the GUI it executes properly.
When I have crontab do it, nothing happens.
Both my python file and the bash script are executable. chmod +x
Is there something obvious I'm missing?
**my python script does depend on other files in the same script directory, could that be the issue?
Comment: I added in > /home/pi/Desktop/clog.log to my cron, there was no inital output but when I used echo I can see that the actual bash script is running as I get a "start" and "finish" echo as expected. So this means the error is with the python script. So how do I debug/log the python script execution?
Answer: Here's what got it working for me. I was not using the full path to my python install. Unless you log the bash file there's no indication that you have an issue.
This is my bash file now. echo's were just to determine that I was indeed running the bash file.
#!/bin/bash
echo started
/home/pi/Python-3.6.0/python home/pi/myScriptFolder/myScript.py
echo finished

To break down the line executing the script:
/home/pi/Python-3.6.0/python - is where python 3.6.0 is installed on my Pi, it could be different for you. home/pi/myScriptFolder/myScript.py is the script I want to run.
And here is my cron statement:
*/15 * * * * bash /home/pi/Desktop/go.sh > /home/pi/Desktop/clog.log 2>&1 -q -f

Breaking down this line:
*/15 * * * * is the cron time, in this case every 15 mins. bash /home/pi/Desktop/go.sh specifies to run a bash file and the directory of that file. > /home/pi/Desktop/clog.log 2>&1 -q -f this last section creates a log file named clog.log so you can see what's going on.
The key here was not just logging the go.sh bash file execution, but adding the 2>&1 -q -f to the end of the log request. Before I did that there was no indication of a problem, afterwards I was getting the python files error returned into the log file.
Answer: Cron jobs are surprisingly tricky.  Aside from working directory ( which you'd have to set somewhere), you also need to handle environment setup ( $PATH, for example).
Start by redirecting your shell script's standard output and error to a log file so you can get feedback.
Comment: This is a poor abridged summary of the [Stack Overflow `cron` tag info page](/tags/cron/info) which has a good troubleshooting section.


Response style as an alternative explanation for latent mean differences under scalar invariance?

Question: I established scalar measurement invariance for a two-group CFA. The output from my final model showed that the latent mean in group 1 is lower than the latent mean in group 2. Can this difference in latent means also be explained by different response styles (e.g., disacquiescence response style)? Why (not)?
I know this is similar to this question, but somehow fail to connect the dots: In measurement invariance testing, how can one differentiate between differences in response styles and differences in latent means?
Answer: If all of the items are coded in the same direction, then you cannot distinguish between a positive (yea-saying) or negative (nay-saying) response style and a difference in means.
If the responses are heavily skewed in one direction you cannot distinguish between a more extreme response style and latent mean differences.
This is essentially an identification problem - you can add a latent response style variable, but that model will not be identified with the data that you have. When you have items coded in opposite directions you can add a response style factor (a class of multiple-trait multiple methods model; also called a method factor) and the response style latent variable will be identified.
You can never know (for example) women have fewer symptoms than men, or women report fewer symptoms than men - my spouse says I complain more than they do; I say I suffer more than they do. There is no way to obtain a unique (identified) solution to demonstrate who is right.
Comment: I largely agree with @JeremyMiles. However, there are ways to isolate "latent" response styles in surveys by using mixture distribution (latent class) models. See:
Comment: Eid, M., & Rauber, M. (2000). Detecting measurement invariance in organizational surveys. European Journal of Psychological Assessment, 16(1), 20–30. https://doi.org/10.1027/1015-57126.96.36.199
Comment: if you expect the same response style to manifest across many factors, then you could potentially partial that out with a factor for all such items.  Try searching for papers about modeling acquiescence bias with CFA.
Comment: Thank you! As a follow-up: (1) I often read that scalar invariance is a prerequisite for comparing latent means. Does your answer imply that scalar invariance is a necessary yet insufficient condition for meaningful group comparisons of latent means, as the groups could differ also in terms of another factor (let's say, the latent response style factor)? (2) Although I like the example in the final paragraph, I'm a bit confused by the words 'you can never know'. Does this also entail the MTMM scenario where you include the extra factor? I assume there you would?
Comment: You compare the latent means, and they are different. Why? Is that because of a real difference, or because of a response bias? You don't know. Correlation isn't causation.


Computation in a Quotient Ring

Question: I have $f(x)=x^3 +x -2 \in \mathbb{Q[x]}$ and R is the ring $\mathbb{Q[x]}/\langle f \rangle$.
$\alpha = x +\langle f \rangle$ is the image of $x$ in R. Note R is not a field as $f(x)=(x-1)(x^2 +x+2)$.
I need to calculate
$(\alpha ^2 - 2)^2$
and
$(\alpha ^2 - 2)^{-1}$ which is the inverse.
Answers needs to be given in the form of $a\alpha^2 +b\alpha+c$ where $a,b,c \in \mathbb{Q}$.
For the first one, I used the ideal addition and multiplication laws, and reached $(x^4 -4x^2 +4) + \langle f \rangle$ which is basically $(\alpha^2 -2)(\alpha^2 -2)$ but I cannot write it in the form it is asking in terms of $a\alpha^2 +b\alpha+c$. If someone can give me a hint, and just make sure I have got my final calculations correct, and possibly verify the inverse for me as well, I really appreciate their kind favor and time.
Many Thanks
Comment: Welcome to Mathematics Stack Exchange.  $x^3+x-2\equiv0\implies x^3\equiv-x+2\implies x^4\equiv-x^2+2x$
Comment: That looks right; and now that you can reduce $x^4$ and $x^3$, you can solve $(a\alpha^2+b\alpha+c)(\alpha^2-2)\equiv1$ for $a, b, $ and $c$
Comment: Hint: by the division algorithm $\, g = qf + r\equiv r\pmod{\!f},\,$ so every poly $\,g\,$ is equal to its remainder $\, r = (g\bmod f)\, $ in $\Bbb Q[x]/(f).\,$ You can compute inverses using the extended Euclidean alhorithm - just as for integers.
Comment: @J.W.Tanner Thanks for the hint, it made a break through. Now I reached $-5x^2 +2x +4 + \langle f \rangle$ which then I converted to $-5\alpha^2 +2\alpha +4$. Just to be sure I have done it right, is this the right way to convert the ideal back to the requested form?
Comment: I am stuck at finding the inverse, I am following an example which I cannot understand fully. Can someone help me make a break through?
Answer:
$\alpha^3+\alpha-2=0$, so $\alpha^3=-\alpha+2$ and $\alpha^4=-\alpha^2+2\alpha$,

so $\alpha^4-4\alpha^2+4$ simplifies to $-5\alpha^2+2\alpha+4$.

Now say $(a\alpha^2+b\alpha+c)(\alpha^2-2)=1$.

Thus $a\alpha^4+b\alpha^3+c\alpha^2-2a\alpha^2-2b\alpha-2c=1.$
Thus $a(-2\alpha^2+2\alpha)+b(-\alpha+2)+(c-2a)\alpha^2-2b\alpha-2c=1$.
Thus $(c-3a)\alpha^2+(2a-3b)\alpha+(2b-2c)=1.$
Therefore $c-3a=0, 2a-3b=0$, and $2b-2c=1$.
Can you take it from here?
Comment: Yes perfect, the extended euclidean algorithm is hell of time wasting... many thanks

Computation in a Quotient Ring


Problem with Upload Speed (ISP or my Fault?)

Question: i got a problem with my Upload on my Office.
My Office has 2 Servers and some Computers.
I get from my ISP 200Mbit Down and 100Mbit upload. It is an FTTH Connection (OpticalFiber)
enter image description here
i think it is capped around 300kbit per Upload connection? And i dont think it is a missconfiguration of a Server because all 2 Servs and local laptops have the same problem.
I already checked:

The Router (Fritz!Box 7590)
The Switch (it was an older Managed switch that i changed for testing purposes to an new unmanged switch)
All Cables (Cat7)

Internal Data transfers are great so i dont think it is the infrastructure itself.
i had iperf connection tests from my 2 servers.
Server1
Server2
and upload tests from my own laptop.
Lan Connection
Wifi Connection
what it can be?
is it my fault at this point ?
or did the ISP messed things up? (ticket is already open... but yeah they have the same infos and images...)
I would take any advices to check things!
MfG
Blade8895
I expect around 100Mbit of upload :D
Answer: Among the many limits to the performance of a TCP connection is:
Throughput <= EffectiveWindowSize / RoundTripTime
If all your upload connections get ~300 kbit/s, and if you run several at the same time and they still each get ~300 kbit/s*, then you should look into what your servers have configured for TCP window size, and what the round-trip-time happens to be to your destination(s). And then look to increase your window size.
*Or the 3-4 Mbit/s your iperf tests implied.  They also suggested some rather small congestion windows (Cwnd) - one of the four "windows" which make up "EffectiveWindowSize" in the formula above - that small a congestion window suggests some non-trivial packet losses somewhere along the way.  Something you could confirm via looking at the network statistics (eg netstat -s under *nix) and/or packet captures.


Parsing JSON with a Hive UDF in Scala

Question: I'm trying to write a Scala UDF for Hive which acts on a JSON array -- extending org.apache.hadoop.hive.ql.exec.UDF and and relying on play-json's play.api.libs.json.parse.
When attempting to call this from within Hive, I see java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonToken.id()I.
I'm not sure what the cause is here--some incompatibility with the jackson versions, and if so, how can I work around this?
The only component/version that I'm tied to is Hive 1.2.
Comment: Is your JSON dependency correct one? Is your UDF jar contains the JSON dependency with correct version?
Comment: The UDF is packaged (with sbt-assembly) with its dependencies (with the exception of Hadoop/Hive where I am using the ones provided). As a stand-alone function it tests/works fine.
Comment: whats the version of jackson core jar in the UDF fat jar?
Comment: I've tried a few...what should I be aiming for? The play-json version mainly dictates my options in that regard.
Comment: The problem could be your cluster is taking the old version of jackson jar which is causing issue or there are two different jars contain the same class name [JsonToken] , check this http://stackoverflow.com/questions/32142340/java-lang-nosuchmethoderror-when-i-try-to-parse-json-on-spark
Answer: Take a look at the JSON UDF's in Brickhouse ( http://github.com/klout/brickhouse ).  Brickhouse has UDF's to_json and from_json , as well as a convenience functions json_map and json_split to deal directly with maps and arrays.
In regards to your versioning problem, Brickhouse uses jackson under the covers, using version 1.8.8 (among others), and I haven't come across this particular versioning problem.
Answer: The guess that this is a Jackson incompatibility makes sense.
Hive 1.2 uses Jackson 1.9.2 but later versions are used by recent versions (i.e. last couple of years) of Play-JSON.
If reverting to an old enough version of Play-JSON doesn't make sense, then perhaps the simplest workaround would be to use a Scala JSON parsing library that doesn't depend on Jackson; Rapture JSON can be used with multiple backends and so might be a good choice.

Parsing JSON with a Hive UDF in Scala


How can I alter variables sent to a paragraph bundle template within a module?

Question: I need to preprocess the field values of a paragraph bundle to "send" additional variables
to the template (paragraphs-item--xxxx--full.tpl.php). I want to avoid putting too much php logic into the paragraph template.
I am searching for something similiar to the node_view hook for a paragraph item. Is there a fitting mechanism or maybee a module providing such an hook?
Answer: Drupal 7
I don't know if this will help you directly, but I found this code snippet on DropBucket and it helped me do something similar:
/**
 * Implements hook_preprocess_entity().
 */
function wwvs_slices_preprocess_entity(&$variables) {
  if ($variables['entity_type'] == 'paragraphs_item') {
    $edge = field_get_items('paragraphs_item', $variables['paragraphs_item'], 'field_edge');
    if ($edge && $edge[0]['value']) {
      $variables['classes_array'][] = 'slice--edge';
    }
  }
}

In my usecase I wanted to augment the paragraph entity with an ID using the url-safe version of the title field used in that paragraph. Here's my implementation:
/**
 * Implements hook_preprocess_entity().
 */
function foo_preprocess_entity(&$variables) {
  if ($variables['entity_type'] == 'paragraphs_item') {
    if(isset($variables['elements']['pp_title']['#items'][0]['value'])){
      $title = $variables['elements']['pp_title']['#items'][0]['value'];
      $title = strtolower(drupal_clean_css_identifier($title));
      $variables['attributes_array']['id'] = $title;
    }
  }
}

I hope this is helpful!
Answer: Drupal 8
In Drupal 8 you can use hook_preprocess_HOOK:
/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME/MYMODULE_preprocess_paragraph(&$variables) {

}

/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME/MYMODULE_preprocess_paragraph__TYPE(&$variables) {

}


Calendar set YEAR issue

Question: I tried the following piece of code:
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.YEAR, 0);
c1.set(Calendar.DAY_OF_YEAR, 1);
Date d1 = c1.getTime();

Calendar c2 = Calendar.getInstance();
c2.setTime(d1);
c2.set(Calendar.YEAR, 2001);
c2.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(c2.getTime().toString());

Calendar c3 = Calendar.getInstance();
c3.set(Calendar.YEAR, 2000);
c3.set(Calendar.DAY_OF_YEAR, 1);
Date d2 = c3.getTime();

Calendar c4 = Calendar.getInstance();
c4.setTime(d2);
c4.set(Calendar.YEAR, 2001);
c4.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(c4.getTime().toString());

The result is:
Wed Jan 01 23:47:00 CET 2001
Mon Jan 01 23:47:00 CET 2001

What is wrong? Shouldn't I use Calendar in this way for setting YEAR?
Answer: The year is relative to the era. By setting the year to something less or equal to 0 the calendar automatically corrects this by switching the era (from AD to BC or from BC to AD). This behaviour is better known from the other fields. E.g. if you set the month to something negative the year gets decremented accordingly.
Those corrections aren't made individually but rather they are made all at once, usually when you call getTime() to read out the resulting date.
Calendar c1 = Calendar.getInstance(); // August  16th, 2012 AD
c1.set(Calendar.YEAR, 0);             // August  16th,    0 AD
c1.set(Calendar.DAY_OF_YEAR, 1);      // January  1st,    0 AD
Date d1 = c1.getTime();               // January  1st,    1 BC (corrected)

Calendar c2 = Calendar.getInstance();
c2.setTime(d1);
c2.set(Calendar.YEAR, 2001);          // January  1st, 2001 BC
c2.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(c2.getTime());     // prints "Wed Jan 01 05:35:00 CET 2001"
                                      // because 01/01/2001 BC was a Wednesday

So instead of setting the year to 2001 you would have to set it to -2000 (because year 0 doesn't exist at all). Or you could explicitly set the era:
c2.set(Calendar.ERA, GregorianCalendar.AD);

Another way to solve this "bug" is by not reading out the time before the complete date is set:
Calendar c1 = Calendar.getInstance(); // August  16th, 2012 AD
c1.set(Calendar.YEAR, 0);             // August  16th,    0 AD
c1.set(Calendar.DAY_OF_YEAR, 1);      // January  1st,    0 AD
c1.set(Calendar.YEAR, 2001);          // January  1st, 2001 AD
System.out.println(c1.getTime());     // prints the expected date

To output the era of a date you can use "G" in the pattern of a SimpleDateFormat:
new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy G").format(c2.getTime())
// "Wed Jan 01 05:35:00 CET 2001 BC"

Answer: In both cases, immediately prior to printing your output, you set the year to 2001.  In both cases, the output reflected that year.  Were you expecting a different output?
Comment: Wednesday vs Monday.
Futhermore, if I set MILLISEC and SEC to 0, and compare the two dates, they won't result equal. Originally my purpose was to normalize the day part of the DateTime objects for comparing just the time part.
Comment: LOL!  What the...?  Yeah, that one got by me.  Looks to me like you've identified a bug.
Comment: The aberrant behavior seems to be related to the c1 year being set to 0 (or negative years, it turns out).  When I try it with positive years, I've only seen the correct Monday outcome.

Calendar set YEAR issue


Add column across rows condtionally

Question: with df like below
df <- data.frame(
  name = rep(c("A", "B", "C"),2),
  type = c("10", "10", "10","20", "20", "20"),
  val = c(1,2,3,4,5,6)
)
> df
  name type val
1    A   10   1
2    B   10   2
3    C   10   3
4    A   20   4
5    B   20   5
6    C   20   6
>

the expected output is
I need to add val of all records with name C to val of records with name A for the corresponding type with a new name AC. Need an output keeping name C and without it.
output1
  name type val
1    A   10   1
2    B   10   2
3    C   10   3
4    AC  10   4
5    A   20   4
6    B   20   5
7    C   20   6
8    AC  20   10

output2
  name type val
1    AC  10   4
2    B   10   2
4    AC  20   10
5    B   20   5
>

prefer dplyr based solution
Answer: Here is one way,
library(dplyr)

df %>%
  mutate(new = as.integer(name %in% c('A', 'C'))) %>%
  group_by(type, new) %>%
  summarise(name = paste0(name, collapse = ''), val = sum(val)) %>%
  ungroup() %>%
  select(-new)

# A tibble: 4 × 3
#    type  name   val
#  <fctr> <chr> <dbl>
#1     10     B     2
#2     10    AC     4
#3     20     B     5
#4     20    AC    10

To get the other output then,
df %>%
  mutate(new = as.integer(name %in% c('A', 'C'))) %>%
  group_by(type, new) %>%
  summarise(name = paste0(name, collapse = ''), val = sum(val)) %>%
  ungroup() %>%
  select(-new) %>%
  filter(nchar(name) > 1) %>%
  bind_rows( df) %>%
  arrange(val)

# A tibble: 8 × 3
#    type  name   val
#  <fctr> <chr> <dbl>
#1     10     A     1
#2     10     B     2
#3     10     C     3
#4     10    AC     4
#5     20     A     4
#6     20     B     5
#7     20     C     6
#8     20    AC    10

Comment: should have asked this earlier - updated the question - what if I need to keep `name`s `A` and `C`
Answer: Here is another (requires tidyr as well as dplyr)
df1 <- df %>% group_by(type) %>%
              summarise(AC=sum(val[name %in% c("A","C")]),B=val[name=="B"]) %>%
              gather(key=name,value=val,-type) %>%
              arrange(type)

Answer: Here is one option using data.table
library(data.table)
rbindlist(list(df, setDT(df)[, .(name = "AC", val = sum(val[as.character(name) %chin%
     c("A", "C")])) , .(type)][, names(df), with = FALSE]))[order(type, name)]
#    name type val
#1:    A   10   1
#2:    B   10   2
#3:    C   10   3
#4:   AC   10   4
#5:    A   20   4
#6:    B   20   5
#7:    C   20   6
#8:   AC   20  10

Or with dplyr
library(dplyr)
df %>%
   filter(name %in% c("A", "C")) %>%
   group_by(type) %>%
   summarise(name = 'AC', val = sum(val)) %>%
   full_join(df, ., on = 'type') %>%
   arrange(type, val)
#    name type val
#1    A   10   1
#2    B   10   2
#3    C   10   3
#4   AC   10   4
#5    A   20   4
#6    B   20   5
#7    C   20   6
#8   AC   20  10

Add column across rows condtionally


php href for user account registration in prestashop

Question: :)
I'm currently editing the customersignin.tpl of my theme and would like to add a 'sign up' link.
Those are the other href classes and href used in the .tpl file:
class="logout"
href="{$logout_url}"

class="account"
href="{$my_account_url}"

href="{$my_account_url}"

I have no idea what the required class (if needed) and href for the registry form are. I guessed around a little with things like
href="{$create_account_url}"

But ultimately, I haven't been able to hit it randomly.
Answer: To add a sign-up link you can have an idea from the sample code provided with classic theme.
Something like:

          {l s='No account? Create one here' d='Shop.Theme.Customeraccount'}

For PS 18.104.22.168, the authentication template is found in /themes/classic/templates/customer/authentication.tpl
https://github.com/PrestaShop/PrestaShop/blob/develop/themes/classic/templates/customer/authentication.tpl#L40
Comment: `
{l s='Sign up' d='Shop.Theme.Actions'}
`

This is, what I thought it should be based on the template you linked. However, once I click on it on my [website](https://giftify.cc/), it doesn't really link anywhere. - As you'll be able to see if you hover over the link. The other two ones actually link to the right page, whereas 'sign up' doesn't.
Comment: I solved the problem. It changed from %url_for_register to $urls.pages.register


Receiving function as another function´s parameter

Question: Let´s say I have 2 functions like these ones:
def list(n):
   l=[x for x in range(n)]
   return l
def square(l):
   l=list(map(lambda x:x**2,l))
   print(l)

The first one makes a list from all numbers in a given range which is "n" and the second one receives a list as a parameter and returns the squared values of this list.
However when I write:
square(list(20))

it raises the error "map object cannot be interpreted as an integer" and whenever I erase one of the functions above and run the other one it runs perfectly and I have no idea what mistake I made.
Answer: You redefined the standard function list()! Rename it to my_list() and clean the code accordingly.
As a side note, your function list() is doing exactly what list(range(n)) would do. Why do you need it at all? In fact, for most purposes (including your example), range(n) alone is sufficient.
Finally, you do not pass a function as a parameter. You pass the value generated by another function. It is not the same.
Comment: Ok, i didnt know you could redefine the list() function and  I am aware that list(range(n)) would be simplier but I was just trying to put an example to my problem. Thanks...


How to interpret Quadratic Terms

Question: I'm answering a practice exam questions, and having trouble with one on quadratic terms. Could someone give me a quick summery of
1) why they are sometimes included?
2) How to interpret them?
In regards to 2) specifically how would you interpret the sign of the coefficient?
3)If we drew a curve of fitted values, and the curve achieved its maximum value at a value of 20 years, how would I interpret that? Is it that values larger then 20 years are associated with a decline in the response variable?
Thanks
I think I understand these concepts, but would appreciate some reassurence
Answer: Lets consider an example (here I use Stata, but the logic works the same in any other package):
. sysuse nlsw88, clear
(NLSW, 1988 extract)

. reg wage c.tenure##c.tenure grade i.race

      Source |       SS           df       MS      Number of obs   =     2,229
-------------+----------------------------------   F(5, 2223)      =     66.51
       Model |  9640.89034         5  1928.17807   Prob > F        =    0.0000
    Residual |  64447.0774     2,223   28.991038   R-squared       =    0.1301
-------------+----------------------------------   Adj R-squared   =    0.1282
       Total |  74087.9678     2,228  33.2531274   Root MSE        =    5.3843

------------------------------------------------------------------------------
        wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      tenure |   .2773182   .0677307     4.09   0.000     .1444962    .4101402
             |
    c.tenure#|
    c.tenure |  -.0070752   .0036278    -1.95   0.051    -.0141894    .0000389
             |
       grade |   .6792721   .0461853    14.71   0.000     .5887013    .7698429
             |
        race |
      black  |  -.7517506   .2649033    -2.84   0.005    -1.271234   -.2322669
      other  |   .6315991    1.06455     0.59   0.553    -1.456017    2.719215
             |
       _cons |  -2.106807   .6357411    -3.31   0.001    -3.353516   -.8600988
------------------------------------------------------------------------------

Adding the quadratic term tenure$^2$ (c.tenure#c.tenure) to the model means that the effect of tenure changes when you get more tenure. When you have 0 years of tenure, the slope is such that your hourly wage would increase by 28 cents for an additional year of tenure if the slope would remain unchanged, which it doesn't. (Hourly wage is in dollars, so a .28 dollar change is a 28 cents change.) Each additional year of tenure reduces the slope by .7 cents. In this case the coefficient of the square term is negative, so the relationship is concave. It usually helps to see this relationship as a graph:
. qui margins, at(grade=12 race=1 tenure=(0/26))

. marginsplot

  Variables that uniquely identify margins: tenure

Initially you get a higher wage as you get more tenure, but the gain decreases and even becomes negative after say 20 years of tenure. We can be more precise about when this occurs:
. nlcom -_b[tenure]/(2*_b[c.tenure#c.tenure])

       _nl_1:  -_b[tenure]/(2*_b[c.tenure#c.tenure])

------------------------------------------------------------------------------
        wage |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _nl_1 |   19.59777   5.692054     3.44   0.001     8.441549    30.75399
------------------------------------------------------------------------------

notice the huge confidence interval, this is quite typical, so be careful about interpreting the position of the maximum.
Answer: 1) Adding quadratic terms allows for non-linearity (in a linear model). If you think that the relation between your target variable and a feature is possibly non-linear, you can add quadratic terms. (Or, you could consider log transformation.)
2) Significance of quadratic terms could signal that the relation is non-linear. The sign merely represents the type of non linearity. A positive quadratic term could suggest that your relation is exponential. A negative relation suggests that for low values of your feature, the relation might be positive, but for high values the relation becomes negative.
3) Correct. Apparently the fitted function is such that a maximum value of 20 can be predicted. After that the non-linear term dominates, if it's sign is negative.
Is this of any help?
Comment: When the coefficient for the squared term is positive, the relationship is convex, not exponential. When that coefficient is negative than the  relationship is concave. As to 3) it is not that the value 20 is the maximum predicted value, but that the maximum predicted value occurs at 20.

How to interpret Quadratic Terms


Put menus into separate file for better handling caching with expires

Question: I am trying to clear the page cache of drupal as micro invasive as possible by using Cache Expires and custom logic.
On each page, the menu is shown down to Level 2 in a footer and a hover area.
Therefore I did only rebuild all menu trees and remove pages from cache if an item on the second or first menu level has been changes.
Now we have got an additional menu for mobile devices which is load with each page and shows down to four menu levels.
Changing a menu on the first to fourth level would no lead to a clear of almost any pages.
To avoid this, it would be great to put all that menu stuff out of the page html and put in a single file, containing just the menu information in e.g. json format.
So if a menu changes I would only need to update the menu file and the changed node.
Are there any smart hacks out there or even modules dealing with that?
Answer: There are several ways to achieve this - maybe more.

Ajax call and include the menu via jquery / javascript from drupal at runtime
Ajax call and iclude the menu via jquery / javascript from a static generated file by cron
SSI or ESI include at proxy level of a static file generated by cron

At the moment my favorite is 2. Option as the First option has a major performance drawback because the menu has to be generated for each page visit
Number 2. and 3. inlcude the menu within under 10 milliseconds
but 3. would be perfect as it makes the least problems with SEO.
As I do not yet have nginx as reverse proxy available in production I stick at 2. as I could not get to work SSI and PHP with apache and php virtual() is not cached by the drupal page cache (in fact it breaks it)
There is a module which helps with SSI and ESI called https://www.drupal.org/project/esi


direct double quoted text can be inserted into sql database, but single quote to double quote converted string is inserted as empty into the database?

Question: I have a textbox where i can type double quoted words like: hello i am "steve" and i can successfully insert the string into my database after mysqli_real_escape_string
<textarea name="description"></textarea>

php below:
$text_data = $_POST['description']; // hello my name is "steve"
$final_text = mysqli_real_escape_string($this->conn,$text_data);

// the above without removing double quotes can be inserted into the db

but if it is single quotes and I convert to double quotes then it cannot be inserted.

$text_data = $_POST['description']; // hello my name is 'steve'
$final_text = str_replace("'",'"',$text_data);
$final_text = mysqli_real_escape_string($this->conn,$text_data);

so my questions are:

how come it works with double quotes? doesn't it needs to be removed or replaced with "/ something?
if the first case: double quotes work fine, then how come the second case when converted from single to double quotes cannot be inserted into the db?

Thanks a lot in advance
Comment: This question should be closed and deleted because there is no actual query shown and the reason could be anything. A some silly mistake most likely
Answer: MySQL treats single quote as a string END. In order to INSERT string with single quotes you have to ESCAPE it as \'Hello World\'
This should work seamlessly
$text_data = "hello my name is \'steve\'";
Comment: thanks for the quick answer! but I have replaced single quotes with double quotes already
Comment: have u print what value u derived after converting your string?
Comment: replacing is one way to do this but not the best practice :-)
Comment: yes i did replace it and it prints out as expected: hello my name is "steve". with the double  quoting steve
Comment: Is this irrelevant to the question or misleading in any sense?
Answer: A couple things..
First I would do some reading on the differences between the single quote and the double quote's behaviors.  Just so going forward you have a basis for the differences between the two.
Secondly lets look at the logic of your code:
If I replace the single quotes in your code like your code suggest your statement will look like this:
"hello my name is "steve""

No lets look closly at what happens between " and steve.
"hello my name is "  steve ""

The reason your query is failing, I believe is because steve is not quoted anymore.
Using prepared statement is really your best solution to the problem.
Hope that helps
UPDATED:
$text_data = "hello my name is 'steve'";
$final_text = str_replace("'",'\"',$text_data);

Comment: thx for your answer , I have updated my question. as you can see I am using $_POST['description'] instead of hard coding the string
Comment: You are saying that the string is not being inserted by the query.  What error are you getting?
Comment: But other strings that do not have quotes are being insereted by your query
Comment: yes other string are being inserted >< that's why im so confused
Comment: Both Farooq Khan and Poonam were trying to illustrate the concept of escaping characters.  But they were showing you how to do it using your typed string.  Since your values are coming from the $_POST you have to use the srt_replace to add the slashes along with the quote.
Comment: Seriously though, check out prepared statements.  Then these type of situations will be avoided.  Plus, you get the side bonus of better protection against malicious database attacks.
Comment: there is no query shown in the question so you cannot claim you know why its failing. all your conjectures about the junk of date being inserted are of no use
Comment: @Your Common Sense - For the sake of learning, am I wrong when it's stated that his query works with no quotes, but fails with quotes? My thinking was that If the query is not using prepared statements thus the quotes would have broken the query string when it was formed.  I see your point that assumptions were made and that there could be other logic that may have caused it to fail, but those options seem far more unlikely than what I suggested.  I suggested the prepared statements so no matter what data was presented it would not break a query string.  Again, I see your point about assumtion
Comment: he is using mysqli_real_escape_string so his query NEVER BLOODY FAILS, no matter what silly juggling with quotes were performed before. Either he is not using mysqli_real_escape_string, or his tales are not true at all. Either way you should have TESTED his improbable claims before trying to answer.


lilypond: How to display changed chord names only?

Question: In German songbooks chordnames are only displayed if they have changed. Lilypond shows chordnames at the beginning of a new line even if the chordname has not changed. Is it possible to change this behaviour?
Comment: I didn't get the problem you're facing. I constructed a MWE [here](http://lilybin.com/90wz55/1) and the chord names are only displayed when there's a change. Anyway, I suspect the solution has to do with the `break-visibility` property.
Answer: I just have finished my document by typesetting using "lualatex". Up to now I did not find another solution to my problem by hiding some chords manually by using \once \override ChordNames.ChordName.color = #white.
Hope to find another solution, soon.
Comment: You can use `\omit ChordName` instead of that override (at least in 2.19).  But you're right, the default behavior for `\set chordChanges = ##t` is to repeat the chord at the beginning of a new line.


Is it possible to change GNU screen session name after created?

Question: [This continues my previous question How do I know I'm running inside a linux "screen" or not? ]
As title, can I?
I tried screen -S mysess but it creates a new screen instead of changing the name of current screen.
I also tried screen -X 'sessionname haha' , but no effect. It just display a inverse-color bar at terminal bottom saying
-X: unknown command 'sessionname haha'

Answer: Use Ctrl-a: to get to command mode for the session and then use the sessionname command
sessionname session1

EDIT:
Using your example above:
If you are detached from a session you can change it's session name with
screen -r 25278.pts-15.linux-ic37 -X sessionname YourSessionName

If you are attached to the session then you can change it's session name with
screen -d -r 25134.pts-3.linux-ic37 -X sessionname YourSessionName

Comment: @JunChen: you should have said that in your question then !
Comment: Iain, you're like Wikipedia with added mod privileges.  That's my cool-thing-I-did-not-know-but-will-use-often-now-I-do for the day, and it's not even 9am yet.  Thanks!
Comment: Thank. But I'd like it to be done programmably so to be run from a shell script, not interactively.
Comment: Thank you. I got it now. The single quotes in my command caused the problem. Anyway, I think I have made it clear from the beginning, because from my screenshot, you can see I tried to change the session name via command line.
Comment: And I found that, in order to change *current* session name, just typing ``screen -X sessionname haha`` does not always work. On second execution, it will inevitably fail with message like ``/var/run/uscreens/S-chj2/16218.pts-9.linux-ic37: No such file or directory`` --still a pending question.


Closing old, unanswered questions because they're old

Question: So this pops up in the review queue:

Layered navigation with 3-tier product structure

It's a well-formed, well-considered question (esp. once OP responded to my request for clarification). It could be closed based on the lack of access to the third-party module. That said, the underlying question ("how to add additional attribute values to layered nav") is generally answerable without access.
Recently I'm seeing a lot of old, unanswered questions in the close queue. I get that we are trying to improve the answer rate of the site, and I get that we are fighting against a flood of bad questions, but I wonder if we are going a bit far. Thoughts?
Comment: Hey come on, that question is not *that* badly written. :)
Comment: It is very well-written. I voted to keep it open. What confounds the issue I suppose is that it's Enterprise, so you're reducing the pool of possible respondents (layered nav is governed by Enterprise_Search in EE), and there is this third-party module involved. Perhaps a more general question about how it could be done on its own in CE...
Comment: Hm yeah, I don't really have access to a copy of EE or that extension anymore. I think when I was working on that project, we even abandoned that legacy product structure and re-structured it. So, do closed questions not count toward the answer rate? Also, I noticed that questions with at least one question cannot be deleted. Only way to not count these toward the rate is to close them, then?
Answer: You have to admit that there are a lot of old questions, most of them badly written, that will never get an answer here.
These should be closed (in my opinion).
Because we neglected these questions when they were posted, now, while doing a cleanup most probably there will be some collateral victims in the process.
I don't have a bullet proof solution for this, but since a closed question can be opened any time I don't think this is really harmful. I also opened a few closed questions when the OP requested or when someone had an answer for them.
Also, closing valid but old questions might refresh the OP's memory that he has an old question to which he may already have got an answer and he can answer it himself.
In conclusion, we might be going too far with closing questions but the damage is not that big compared to the benefits.
Comment: Solid reasoning. First beer/soda/water/wine at Imagine is on me.
Answer: Good point. I've been closing more and more questions and I try to do it for good reasons.
Couple of reasons why I flagged this one

over a year old
only 66 views so low Google value
Magento EE so no easy way to reproduce
Bit broad / vague. The chance of getting an answer was in my opinion low.
Answer: How about asking, as a comment, the OP if there has been any progress? Active users will be alerted and be given a chance to update the question (possibly even an answer).
Even though I wasn't alerted, I just came across benmarks' question by chance, which prompted me to clean up a lot of my old questions. However, I understand that many users are indifferent about answer rates, so it may be a lost cause.
Comment: I tried that a few times, but most of the users with rep below 50 (not regular users) never answer back.
Answer: I'd like to add that when I stumble upon a question that is unanswered and vague/low quality/unclear/old and maybe, maybe after placing a comment the OP will come back to improve/answer it himself, I have forgotten completely about this question if he doesn't. I think for most of these questions the OP won't come back.
Closing it when you stumble upon them is the best way in my opinion.
Comment: Hmm. Perhaps a meta-monitoring service might help. Who's game?
Comment: Interesting thought... I could even think of more implementations then the issue discussed here ;)
Answer: I asked a similar question earlier regarding closing my own question.
Ref: Delete or Vote to Close my own question
Though the questions are good in my opinion, i never found a solution of it and also didn't got the answer.
I was advised to be my own judge by @benmarks and i decided to keep them open hoping someone might stumble upon it sometime and can provide the solution to it.
I think closing a bad questions will be a good idea, but we should not delete the decent questions. We can make it come up the queue by either upvoting, editing or commenting on it.
Comment: Surely you mean we should _not_ delete the decent questions?
Comment: Yes i think so... it is best in community interest..


Are there popular microtonal scales for violins? Or any instrument that can easily get microtones?

Question: I'm interested in popular or used microtonal scales. I don't need a list of all possible ones, just those that are used.
If you could also please give the uses of such microtonalities to hear not just the names of them, I'de be very happy. Thank you!
I have come up and used in my works the following ones:
Comment: Welcome! Please read about [the topics that are covered here](https://music.stackexchange.com/help/on-topic). Although it's not spelled out on that page, questions for which the answer would mainly be a "list" of items are also discouraged. You mentioned you don't need an exhaustive list, but maybe it would be helpful to edit the question to ask more *about* such microtonal uses, rather than just for their names. Especially, it might be helpful to narrow the focus to certain music cultures or genres. It might be easier to discuss these in a context of practice than abstract theory.
Comment: Combined with your previous question, it seems that you're working with microtones, but there's probably a lot to be gained from addressing the "how" rather than "what do I call it." (For instance, I'm curious why you're including a standard-tuned piano playing in unison with the microtonal violin part, and how that works out...)
Comment: @AndyBonner, I'm very interested in the "how" too. I edited the question  to say that. I don't want to reinvent the wheel, so yes, you're right
Comment: @RedMermaid have you seen the [Xenharmonic Wiki](https://en.xen.wiki/w/Main_Page)?  The article "[Why use alternative tunings?](https://en.xen.wiki/w/Why_use_alternative_tunings%3F)" may be a helpful starting point.
Comment: There's the ever-popular 'I'm not very good yet' microtonal chromatic, but this might not be what you're after.
Comment: It's a good thing you didn't ask for a "list of all possible ones".  That list would be infinite.
Answer: I hope others can give a more detailed answer, but my advice would be to start by studying those cultures and genres that use microtonality. Middle Eastern music springs to my mind; especially music for the oud; this might be the closest to the Western concept of "scale," in which we conceive of a given scale degree as being a certain frequency, "always" or in the abstract.
The violin is also used in Carnatic music, with non-Western techniques and tunings. The concept of scale is more complicated in this and other genres, because it's not just a matter of static pitch, but of rules about how you raise, lower, and bend pitches in different contexts. For that matter, there are plenty of Western practices that encourage us to "bend" or alter pitches for contextual reasons, from the "blue note"(s) in blues to the way classical players narrow the gap between the leading tone and the tonic. In the history of classical music, the notion that all half-steps are equal distances ("equal temperament") is relatively new; for centuries, the frequencies would be adjusted or "tempered," and even instruments with fixed pitch like lute or harpsichord would be able to adjust the temperament (in the case of lute, with moveable frets). For a deep dive into how this system still affects piano tuners (at least, smart ones) today, see The Seventh Dragon: The Riddle of Equal Temperament by Anita Sullivan.
Scandinavian folk music also exhibits "altered" tunings; I think of the willow flute which plays the overtone series with its "out of tune" notes, and both Norwegian and Swedish fiddle traditions adjust certain pitches noticeably out of the standard scale (see this article).
It's debatable whether "microtonal" is the right word for these practices of temperament or altered intonation, though. In the case of pre-Romantic Western music's "temperaments," this was still a system built on the tetrachord and the familiar 12 semitones that we derive from it; it's not a matter of dividing the octave into more or fewer parts than 12 and identifying "other notes."
Comment: I like the term "[mesotonal](https://en.xen.wiki/w/Mesotonal)" (coined by [Kraig Grady](https://en.wikipedia.org/wiki/Kraig_Grady)) for tuning systems that are still in the neighborhood of 12 tones, but not 12-EDO or other common "Western" systems.
Comment: @Theodore `s/12 tones/diatonicism/`. Most folk genres don't use 12 tones at all – they can often be played well on a 12-edo instrument, but usually it's quite clear that the starting point is rather a Pythagorean or 5-limit tuning (just as in the history of mainstream European art music). They typically use, like, 8 or 9 tones; for example Norwegian is usually in E major or B major, Irish is usually in D major or G major or their relatives, etc.. The idea to modulate anywhere, and thus require temperament, is historically the exception.
Comment: @leftaroundabout Not sure how that relates to my comment.  I know all this.  I was only offering an alternate term for "microtonal" when we're just talking about altered intonations.
Comment: @Theodore it relates to the number 12 in your comment, which IMO misses the point. You could just as well say their in the neighbourhood of 31 or 34 tones or perhaps even 17 tones, or indeed any other number of tones if it's not EDO anyway – this is meaningless. What does characterize mesotonal tunings however is that they're in the neighbourhood of a diatonic scale.
Comment: @leftaroundabout I see what you mean now and I don't wish to argue about what seems to be a minor point.  I guess I should have said 7 instead of 12, though I would personally consider the range of 5 ~ 12 to be "in the neighborhood of 7" (especially when compared to, say, 53).  FWIW, Kraig Grady has referred to both his *12-tone* Centaur (7-limit) and Centaura (11-limit) just tuning systems as "mesotonal".
Answer: "Or any instrument that can easily get microtones?"
You didn't specify if you are open to synthesizers or not.
There are a few that allow very controlled specification of microtonal scales and of course, allow easier control to play the desired pitches.
Using a synthesizer with microtonal control would also allow you to provide an accompaniment with matching scales.
The Hydrasynth, for one, allows uploading up to 32 user defined microtonal scales.
Comment: Yes, I am open to them


Number of naturals which cannot be expressed in the form $xa+yb=1$ for coprime $a,b$

Question: I was playing around with a few numbers.I noticed the following:
Given two coprime naturals $a$ and $b$,we can express a lot of integers in the form $xa+yb=d$ for $x,y\ge0$ and $x$ and $y$ are integers.
However, there are some others which I could not express in the form I just described.For example, if $a=7$ and $b=5$, I could not express 1,2,3,4,6,8,9,11,13,16,18 and 23 as above.There might be more of them.That brought to my mind a question.
Given two coprime positive integers $(a,b),a>1,b>1$,is the number of natural numbers which cannot be expressed in the form above finite? Can we exactly calculate how many of them exist?.
Comment: Every $d\ge(a-1)(b-1)$ can be so expressed; exactly half of the non-negative integers less than $(a-1)(b-1)$ can be so expressed.
Comment: @xavierm02: $a$ and $b$ are required to be relatively prime.
Answer: This is a special case of the Frobenius coin problem, also sometimes called the postage-stamp problem (though that name also refers to another problem) or, more recently, the McNugget problem. Sylvester proved that every $d\ge(a-1)(b-1)$ can be so represented and that exactly half of the non-negative integers less than $(a-1)(b-1)$ have such representations. This web page gives a proof of the theorem.
Comment: Looking at the $a=7$, $b=5$ example, you'll see that $d$ can be expressed if and only if $23-d$ can't. This is where the "exactly half" in Brian's answer comes from.


Plot of hazard ratio for primary endpoint and continuous covariate

Question: I wonder if you could tell me the R code to use to obtain a graph similar to the one below. (from: https://www.nejm.org/doi/full/10.1056/NEJMoa1215740)

That is, I would like to graph the association between the hazard ratio of the primary endpoint (EVENT) VS a continuous covariate (VALUE).
My cox model is as follows:
cox<- coxph(Surv(TIME, EVENT) ~ VALUE, data = EBB)

Answer: require(rms)
dd <- datadist(mydata); options(datadist='dd')
f <- cph(Surv(time, event) ~ rcs(glucose, 4), data=mydata)
ggplot(Predict(f))

To have more control over the plot use the contrast function (full name contrast.rms) and plot its results as shown in the help file for contrast.rms.
Comment: Thank you very much.

Can I just ask you how you interpret the log relative hazard in relation to the hazard ratio? In the graph I don't get the hazard ratio but log relative hazard
Comment: Anti-log of log relative hazard (you can easily plot the anti-log using an argument to `Predict`) is a hazard ratio.   The HR is relative to the reference value for the covariate.  Default in `rms` for a reference value is the median for most purposes.
Comment: This is what I searching for! In the OP's plot the reference point is 100. Can I set the reference point to any values I want with `rms`? And the length of confidence interval should be 0 at the reference point. How can I get the confidence interval like that?
Answer: An alternative would be to visualize the marginal survival probabilities as predicted by the Cox model using the contsurvplot package. You could, for example, plot the survival probability at t as a function of VALUE like this:
library(contsurvplot)

# refit model with x=TRUE
cox <- coxph(Surv(TIME, EVENT) ~ VALUE, data = EBB)

plot_surv_at_t(time="TIME",
               status="EVENT",
               variable="VALUE",
               data=EBB,
               model=cox,
               t=5) # change to some t of interest

To remove the dependency on t you could also use a survival area plot or a survival contour plot, as described here
Comment: No it doesn't. If you want a smoothed HR as a function of a continuous variable you can, however, use the `smoothHR` package instead. Personally I prefer the use of survival probabilities though, as they are easier to interpret and have nicer statistical properties.
Comment: You can also do this with the `rms` package, i.e., `ggplot(Predict(f, time=5))`.
Comment: Yes you are right. In this case `plot_surv_at_t` is equal to your code. However it will produce different estimates if other covariates are included in the cox model. The `rms` package also does not support the other type of plots mentioned.
Comment: The OP's plot focuses on hazard ratio, not survival probabilities. Does this package support HR?


Docker API is always returning empty JSON even when there are images in the server

Question: When I do "docker images" in my server, there are multiple images coming up. The same when tried to access through an api "curl http://localhost:2355/images/json" returns []. Does anyone know why is this happening?
Comment: Can you provide a [mcve]?  Have you taken any steps at all to secure the Docker socket; it's very easy to use it to root the host?
Comment: What does `docker context ls` output?
Comment: Thanks guys for responding, docker was not correctly installed on the server. I reinstalled it and it is working fine.
Answer: write this command your server and look your api is expose?
enter image description here
if you getting response like this your api not open for request
if do you want get access you must change docker.service file
Comment: Thanks a bunch for contributing by answering questions! However, please don't upload images of code/data/errors. There are many reasons why, and you can read some of them [here](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). If you could edit your answer that would be great!


Is this a correct translation from english into symbolic logic?

Question: "You can fool some of the people all of the time, and you can fool all of the people some of the time, but you can't fool all of the people all of the time." (Abraham Lincoln)
Let

$P$ be "fooling some of the people all of the time",
$Q$ be "fooling all people some of the time",
$R$ be "fooling all of the people all of the time".

$(P \lor Q) \rightarrow \neg R$
Is this a correct translation in propositional logic?
Comment: I don't think this question is a duplicate of the linked posts. These posts are about a  translation of the same sentence to first-order logic, here the translation is required into propositional logic.
Comment: See also [this post](https://math.stackexchange.com/questions/3318210/predicate-logic-formalisation-of-the-quote-by-abraham-lincoln)
Comment: As well as [this one](https://math.stackexchange.com/questions/135297/first-order-logic)
Answer: No, a correct formalization of Lincoln's sentence in propositional logic is the following:
$$(P \lor Q) \land \lnot R$$
Indeed, from a logical point of view, "but" as the same meaning as "and".
Note that I translated the "and" between the first two propositions by an "or", because in this context the two propositions express an alternative.
By the way, propositional logic is not the best logic to formalize this kind of sentences. First-order logic and modal logic can express a more faithful formalization of Lincoln's sentence.


Drag and Drop plots and other available features of Core-Plot?

Question: i am new to core-plot frame work, i have few doubts about the feature of this frame work. May be my question very simple or unwanted, but still i am posting here for my clarification.

Its possible to drag and drop plot in core-plot framework.
Can i achieve smooth scrolling (like: UIScrollView) in core-plot?
If my graph size is beyond screen size means, how i can handle to scroll the graph to see fully?

If Its possible means kindly guide me to which Class or API provide this features, or any other things i have to follow. Thank you in advance....
Answer: Short answer, yes.
Set the allowsUserInteraction to YES for each plot space you want to respond to touch scrolling and pinch zoom events. Set allowPinchScaling to NO on the hosting view to keep the scrolling behavior and disable pinch gestures.
The xRange and yRange of the plot space determine the visible are of the plot area. Any plot data outside these ranges will be clipped and invisible. The plot ranges can be changed by the user (via scrolling and/or zooming) or directly in code by the app.
Be sure to look at the example apps included with Core Plot. Many of them include scrolling graphs.
Comment: what about each plot drag and drop?
Comment: The question is tagged iOS. What do you mean by drag and drop? iOS doesn't support drag and drop the way that term is usually used on the Mac.
Comment: if i create a plot, at run time can i touch a plot and reposition the plot to some other place with in the same plots pace?
Comment: Yes, that's what I meant by "scrolling" in my answer above.


how to uninstall homebrew using an arm based Mac?

Question: I installed homebrew today. And I did something wrong so that my homebrew can't work now. Since I use an arm based mac, I am hard to find solution to uninstall my homebrew which installed in /opt/homebrew/.
Answer: To uninstall Homebrew, run the uninstall script from the Homebrew/install repository.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"

Download the uninstall script and run /bin/bash uninstall.sh --help to view more uninstall options.
Answer: Make sure your homebrew path is correct and then run the script:
echo $HOMEBREW_PREFIX #=> /opt/homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

See: https://github.com/homebrew/install#uninstall-homebrew
Comment: `echo $HOMEBREW_PREFIX` returned the right value but I still got an error: `Failed to locate Homebrew!`. Fixed it by adding `arch -arm64` in front of the install command.


Exportar dados para planilha excel formatando a célula

Question: Estou exportando dados para o Excel, porém o formato da planilha criada não é o desejado.
Estou exportando o texto 000 porém o Excel entende como sendo número, e o valor exportado é 0.
O código que estou usando é o seguinte:
txt_exportar="000"
.range(a1).value=txt_exportar.
Outro exemplo:
txt_exportar="060"
.range(a1).value=txt_exportar.
O valor exportado para o Excel é 60.
Desde já agradeço a atenção.
Answer: Encontrei a solução.
Basta acrescentar a seguinte linha:
Range("A1").NumberFormat = "@"


How to determine if a -long- time series made of discrete observations is markovian?

Question: Let us assume I have a time series made of the following  observations:
ts = c(163,18,53,189, 243, 101,  150, 39, 60,96,36,76,71,67,56,3,72,96,15,19)
How can I determine if it respects the Markov chain properties and how can I get the markovian order? Is there a source where I can find a guide?
Any good python or R solutions? The data I am dealing with is made of about 500K observations.
Thanks
Comment: Not sure what the formal way is to do this, but you could try to predict $X_t$ given (1) $X_{t-1}$ and (2) the entire vector. If the quality of your predictor for (1) is the same as (2) then you have reason to believe your time series respects the Markov property.
Comment: @philbo_baggins do you mean by using the empirical transition matrix observed from the studied time series? Do you suggest to simulate the series by using a Montecarlo Approach where the simulated are generated by the empirical transition matrix?
Comment: @CamilleGontier the observations are discrete, they come from a set of status made of natural numbers in [1-247]. I have changed the sample posted because it was not accurate. It could be a subset but I have invented it. Anyway, out of 500K observations, it is plausible to have that sampled vector. :(
Comment: That's indeed a nice idea, but doing so for each of the 500k observations might be computationally challenging. Valerio, are your observations continuous or discrete ? Is your vector ts an example sampled from your 500k observations, or is this the set of possible values that your 500k observations can take ?
Answer: I think this paper should answer your question : https://www.econstor.eu/handle/10419/2673
The starting point of the procedure is indeed to estimate the transition matrix from your observations, for instance by using a Maximum Likelihood approach. Then, you have to check for these two markovian properties:

You have to verify that your process is stationary. This can be done by splitting your observations into sub-chunks, estimating the transition matrices for each of these subsets, and testing whether the estimates significantly differ.
You have to verify that your process is memory-less, i.e. that your estimates of the transition matrix do not vary if the values of previous states are taken into account.

These statistical tests are described in the paper (and are based on a chi-squared test).
Hope this helps !
Comment: Unfortunately, i have found that this type of solution is somewhat questioned [here](https://stats.stackexchange.com/a/250519/298613) :''( @Camille Gontier
Answer: The answer to this question can be found in the following paper: Markov Chain Monte Carlo for generating ranked textual data

Assume one starts with an empirical series made by N consecutive observations of integers numbers taken in a finite set called RANK. They represent a discrete time of the stochastic process under analysis.
Let us define the discrete-time stochastic process as $X=(X(t):t \in \mathbb{N})$ taking values in RANK and with $P$ the related probability law. $X$ is a Markov Chain of order one if one gets:
$$
P(X(t+1)=i_{t+1}\vert X(t)=i_t)=P(X(t+1)=i_{t+1}\vert X(t)=i_t,\dots,X(1)=i_1,X(0)=i_0)
$$
for each $t \in \mathbb{N}$ and $i_0,i_1,\dots,i_t,i_{t+1}\in\mathbf{RANK}$.
One can write a less heavy relationship and say that $X$ is of order one when the following simplification of the above relationship is true:
$$
P(X(t+1)=i_{t+1}\vert X(t)=i_t)=P(X(t+1)=i_{t+1}\vert X(t)=i_t,X(t-1)=i_{t-1})
$$
for each $t \in \mathbb{N}$ and $i_0,i_1,\dots,i_t,i_{t+1}\in\mathbf{RANK}$.
So, the transition probabilities of orders one and two appearing in this last equation have been empirically estimated by looking at the consecutive empirical observations.
Two steps has to be carried out. In the First step, one checks the validity of the last equation; namely one tests that the empirical transition probabilities estimated by the original sample are associated with a Markov chain of order one. In the Second step, one shows that the obtained Markov chain of order one is a statistically sounding representation of the original sample, hence leading to the final statement of the Markovianity of the data.
Steps

Run 1000 simulations for by using the order one and two transition matrices. This step originates 2000 samples from Markov chains. The lengths of the series calculated from the first- and second-order transition matrices are different, they may depend on the length of the original observed sample, the object of the study.
Then, the first- and second-order simulated series have been pairwise compared via the Kolmogorov–Smirnov (KS) test. Namely, the empirical distributions of each simulated series from the first-order transition matrix have to be compared with those of the simulated series coming from the second-order transition matrix. If the series can be considered as coming from the same distribution, one can conclude that there is no statistical difference between the first-order transition probabilities and the second-order ones. This outcome supports the validity of the above reported equation, so that the Markov chain obtained by using the empirical transition probabilities estimated from the original sample is of order one. Furthermore, one can also use a Wilcoxon-Mann–Whitney test to compare the simulated series based on transition probability of order one and two, between each other. In this way, one can check of the absence of compelling evidence that the simulated series data differ. So, one can conclude that the simulated series are obtained by the same data generating process. This finding strongly supports that the observed data are realisations of a first-order Markov chain.
One has to check that the Markov chain of order one, if the above is all true, with states space in RANK and whose transition probabilities are estimated from the available observations, can be seen as the data generating process of the original sample. In so doing, one provides additional evidence that the evolutive process related to the formation of the target distribution has a Markovian structure. One compare, for example, 1000 of the simulated series to the original one, namely to the one under study. That can be done with a Chi-Square test or again, with the KS test. In addition, the main statistical indicators from the simulated series can be calculated (mean, st. dev., Kurt. and Skew.). If their empirical distributions are centred on the same parameters from the original series, this is a further proof of markovianity of order one, since the simulated series are generated from transition probabilities of order one, estimated from the observed / studied series.


I need to add all of the items in a ListBox into a "total" variable

Question: Here is an example of the For loop that I'm trying to run where lstCost is my ListBox that is formed by selecting options from two other ListBoxes and calculating a total for those option to create an item total and totalCost is the variable where I'm trying to save the total of all list items.
    Dim totalCost As Integer
    For x As Integer = 0 To lstCost.Items.Count - 1
        totalCost += Val(lstCost.Items.Item(x))
    Next
    txtTotalCost.Text = totalCost.ToString

For some reason, I end up with a 0 as my total in my txtTotalCost TextBox. Can anybody think of any reason why?
Comment: What happens when you debug this?  What specifically goes wrong?
Comment: Turn on Option Strict.  `Val` always returns a Double not an integer
Comment: whats in the listbox? Value wise I mean..
Comment: If you know it's integers use totalcost += DirectCast(ListBox1.Items(x), Integer)
Comment: If its $0.25 you will be getting zero... change integer to double
Answer: Dim totalCost As Double = 0

For x As Integer = 0 To lstCost.Items.Count - 1

    totalCost += DirectCast(lstCost.Items.Item(x).ToString.trim,Double)

Next

txtTotalCost.Text = totalCost.ToString

depending on your values in listbox you can change it to Decimal if required


how should I add textfield ie jump pagination in views mini pagination of drupal

Question: I have created views of gallery by view. View has provided pagination option. I choose mini pager.
I want to implement Jump Pagination here. How should I add in view pagination?
Comment: I've update my answer with new module (Pretty Pagination). I hope it helps.
Comment: You could just override the theme function for this... https://api.drupal.org/api/views/theme!theme.inc/function/theme_views_mini_pager/7
Answer: Try the Pretty Pagination module . I think This is what you are looking for.

This module extends the default pagination into a more flexible format. It allows the user to jump to any specific page in the content just by entering the desired page number in an input box and hitting the ENTER key. The result is easier navigation, especially with sites where the content is spread over numerous pages.

Another module is
Pagerer module. Using this module you can give direct input of the page to go to through a jQuery UI Slider.

Pagerer is a module providing a collection of pager themes to enhance Drupal and Views standard pagers.
Features:

multi-pane pager

Views pager plugin

control whether to display links to pages, to items, or to item
ranges

direct input of the page to go to through an input widget

selection of the page to go to through a jQuery UI slider

selection of the page to go to through a client-side scrolling pager

links to progressively more distant pages (like +10, + 20, +100,
+200)

adaptive logic links

specify text to be used to render page separators (like a vertical
bar) and page breakers (like an ellipsis)

supports Views' AJAX enabled pager


Starting scales on piano

Question: I've been playing the piano for about 5 years so scales have become a kind of second nature to me. I'm currently playing 3 and 4 notes per tick at 92 for my scales. But this year I have had trouble with starting my scales, it feels at thought I am physically unable to start playing them even though I know I can. Can anyone help?
Comment: Are you asking about a psychological issue or a physical one?
Comment: It sounds like you practice scale drills, but it recently take time to get into the "flow." Can you add some detail about what you actually play for scale practice?
Answer: I've been drilled to practice scales - parallel and opposite movement, 2:2, 3:2, 4:3 and this by only activating each single finger.
I had years to find out by myself that there are some important additional movements:

Rolling the hand, especially the wrist
Following with the arms and shoulders, if needed thewhole body to the right and left side of the piano, like we do when we play arpeggios, skipping with the thumb from fourth to octave etc.
Answer: Do you mean you can't start psychologically or physically?
Since many scales start with the thumb, there are several movements which play the thumb.  First, you can pronate down to it.  The same muscles you use to play trills and tremolos are the ones that can play the thumb.  Just use forearm rotation from the elbow and the thumb plays without using the thumb's muscles.
You could be playing too far in.  The keys are heavier on the inside so without curling, try to play all fingers using in/out motions to play on the outside edges.
The thumb should have a forward shift to it so you don't fall off the keys.  Sitting at the piano is unnatural so you need this constant forward shift.  If you totally relaxed, your arms would fall to your sides. There is no such thing as relaxation at the piano so you need constant alternating movements to maintain "relaxation."  As you learned in HS Physics, every motion must have an equal and opposite motion.  The good news is you can train yourself to minimize the opposite motion.
Maybe you are pressing too hard into the keybed?  You can't move if you are pressing down. Let gravity play the notes and your hand will always be free from strain.
Maybe you are abducting your fingers in preparation to play the next notes.  Spreading the fingers out creates instant tension. Learn to play with all five fingers together. The arm will get the fingers where they need to be.
There are multiple finger groupings and rotations involved in scale playing.  Your arm can change directions several times in one ascending octave scale.   It could be that you've been doing it incorrectly for the past few years and the tension is just hardwired into your brain. If this is the case you'll have to re-learn how to play.
Are there twists in your wrists?  Sometimes when players try to get forearm rotation in their arm (from the elbow) they do it from the wrist.  More tension ensues.
Play from the arm, shoulder, elbow.  The arm places the fingers, the fingers don't drag the arm behind it.  That will result in twists, weak feeling fingers and fatigue.
It could be that you are trying to play from the flexor muscles.  Your fingers don't have muscles.  They are moved from the muscles of the forearm.  The flexors are not fast muscles at all, they are designed for gripping things.   And, they are certainly not designed for independence because they are all interconnected and using two muscles to move one bone will result in tension, one of the muscles will have to submit.  The arm is designed for speed though, ironically.  For example, you don't throw a ball from the fingers, nor the wrist, nor the elbow, nor the shoulder.  The throw starts from the feet, abs, hips and back, then transfers to the shoulder, elbow, then fingers.  The wrist is a conduit.
Because your fingers are different lengths and the black keys are higher, you need an up movement from the forearm.  You must strike each key from above it coming straight down.  Like walking up stairs, your ascending foot must raise higher than the next step.  If not, you'll trip.  Piano keys are the same because of the different finger lengths and the higher black keys. You can't play them from below them.

Starting scales on piano


Teamcity CI/incremental builds - Change log

Question: We are running TeamCity Enterprise 7.0.4 (build 21474)
We have teamcity CI/incremental builds running every 15 minutes or so.
The incremental builds identify the VCS activity, checkout the new/modified code, and run a build.
We need this CI build to trigger another teamcity project. The triggered project needs information about the VCS changes that were checked out and built by the CI project.
To be specific we need VCS author(s) user id(s), their email id(s), and the file(s) modified
Seems this info can be obtained via Change Log/Pending Changes from teamcity
Any pointers, how to pass this info to the triggered project
Thanks
Answer: You can try using snapshot dependencies. What is the VCS you are using ?
Answer: Solution depends on what you want:
* Show changes from first project in changes log of second project (TeamCity UI only)
  Set snapshot dependency.
* Changes list inside build step as data for some process (writing into changelog.txt or something similar)
  Use TeamCity Rest API to obtain such information.
  Use query "/app/rest/changes?locator=build:(id:BUILD_NUMER_HERE)" to get build changes.
  Build number could be obtained by empty artifact dependency or snapshot dependency.
  If there a dependency, you can address parameters of first build as %dep.btxxx.build.number%
More documentation:
About dependencies
About Rest Api
About parameters from dependencies


Probability that the sum of $k$ distinct integers selected from $1, 2, \dots, n$ is divisible by $n$

Question: Would you please help me solve Problem 7 of Section 4.5, "Combinatorial Number Theory," in An Introduction to the Theory of Numbers, Niven, Zuckerman, Montgomery, 5th ed., Wiley (New York), 1991:

Let $n$ and $k$ be positive, relatively-prime integers with $n > k$.  Prove that if $k$ distinct integers are selected at random from $1, 2, \dots, n$, the probability that their sum is divisible by $n$ is $1/n$.

The authors describe three methods for solving combinatorial problems: (1) the pigeonhole principle, (2) the one-to-one correspondence procedure, in which elements in a finite set or between two sets are paired off to determine the number of elements, and (3) the inclusion-exclusion principle.  I know that the denominator of the probability fraction is $n \choose k$, so I tried (without success) to use the second method to count the number of desired outcomes to be $(n - 1)!/((n - k)! k!)$.
Because $n$ and $k$ are relatively prime, I know that $n \choose k$ is divisible by $n$.  In that case, numerical evidence suggests that the sum in question is uniformly distributed modulo $n$.  If I can prove that conjecture, I am done.
I found similar problems with particular values for $n$ and $k$, but the solutions do not help me solve this general case.
Answer: Think of the numbers as integers modulo~$n$. Consider the map
$A=\{a_1,a_2,\ldots,a_k\}\mapsto\{a_1+1,a_2+1,\ldots,a_k+1\}$. This adds $k$ modulo $n$ to the sum of the elements of $A$.
Comment: Can you explain more? How does this solve the problem?
Answer: The exponential formula tells us that  the cycle index $Z(P_k)$ of the
unlabeled set operator
$$\def\textsc#1{\dosc#1\csod}
\def\dosc#1#2\csod{{\rm #1{\small #2}}}\textsc{SET}$$
on $k$ slots has OGF
$$Z(P_k) = [w^k]
\exp\left(\sum_{l\ge 1} (-1)^{l-1} a_l \frac{w^l}{l}\right).$$
The desired probability is given by
$${n\choose k}^{-1} \frac{1}{n}
\sum_{p=0}^{n-1}
\left. Z(P_k)
\left(\sum_{q=1}^n z^q\right)\right|_{z=\exp(2\pi ip/n)}.$$
This is
$${n\choose k}^{-1} \frac{1}{n}
\sum_{p=0}^{n-1} \left. [w^k]
\exp\left(\sum_{l\ge 1} (-1)^{l-1} \left(\sum_{q=1}^n z^{ql}\right)
\frac{w^l}{l}\right)\right|_{z=\exp(2\pi ip/n)}.$$
Evaluating the contribution for $p=0$ first we get
$${n\choose k}^{-1} \frac{1}{n}
[w^k] \exp\left(\sum_{l\ge 1} (-1)^{l-1} n
\frac{w^l}{l}\right)
\\ = {n\choose k}^{-1} \frac{1}{n}
[w^k] \exp\left(n \log(1+w)\right)
\\ = {n\choose k}^{-1} \frac{1}{n}
[w^k] (1+w)^n
= {n\choose k}^{-1} \frac{1}{n} {n\choose k}
= \frac{1}{n}.$$
It remains to evaluate the contribution  from $1\le p\le n-1.$ Now for
these $p$ if $l$ is a multiple of $m = n/\gcd(p, n)$ we have
$$\sum_{q=1}^n \exp(2\pi ip/n)^{ql} = n.$$
We get zero otherwise. This yields for the remaining terms without the
scalar in front
$$\sum_{p=1}^{n-1} [w^k]
\exp\left(\sum_{l\ge 1} (-1)^{ml-1} n
\frac{w^{ml}}{ml} \right)
= \sum_{p=1}^{n-1} [w^k]
\exp\left(-\frac{n}{m} \sum_{l\ge 1}
\frac{(-w)^{ml}}{l} \right)
\\ = \sum_{p=1}^{n-1} [w^k]
\exp\left(-\frac{n}{m}\log\frac{1}{1-(-w)^m}\right)
= \sum_{p=1}^{n-1} [w^k] (1-(-w)^{n/\gcd(p, n)})^{\gcd(p, n)}
\\ = \sum_{p=1}^{n-1} [w^k] (1+(-1)^{1+n/\gcd(p, n)}
w^{n/\gcd(p, n)})^{\gcd(p, n)}.$$
Using an Iverson bracket this becomes
$$\sum_{p=1}^{n-1} [[n/\gcd(p,n)|k]] \times
{\gcd(p, n)\choose k\gcd(p,n)/n}
(-1)^{k\gcd(p,n)/n+k}.$$
Putting it all together we thus obtain
$$\bbox[5px,border:2px solid #00A000]{
\frac{1}{n} + (-1)^k
{n\choose k}^{-1} \frac{1}{n}
\sum_{p=1}^{n-1} [[n/\gcd(p,n)|k]] \times
{\gcd(p, n)\choose k\gcd(p,n)/n}
(-1)^{k\gcd(p,n)/n}.}$$
Note that $n/\gcd(p,n)$ is  a divisor of $n$ that is  at least two (we
would need $p=n$  to get $n/\gcd(p,n) = 1$ but  $p\lt n$).  This means
when $\gcd(k, n) =  1$ the Iverson bracket fails for  all $p$ and only
the first term remains, which is what we wanted to prove.
As a sanity check  we evaluate the formula when $k=n.$  The sum of the
one subset is $n(n+1)/2$ so we should get for the probability one when
$n$ is odd and zero when it is even. We find
$$\frac{1}{n} + (-1)^n
{n\choose n}^{-1} \frac{1}{n}
\sum_{p=1}^{n-1}
{\gcd(p, n)\choose \gcd(p,n)}
(-1)^{\gcd(p,n)}
\\ = \frac{1}{n} + (-1)^n \frac{1}{n}
\sum_{p=1}^{n-1}
(-1)^{\gcd(p,n)}
=  (-1)^n \frac{1}{n}
\sum_{p=1}^{n}
(-1)^{\gcd(p,n)}
\\ = (-1)^n \frac{1}{n}
\sum_{d|n} \sum_{\gcd(p,n) = d} (-1)^d
= (-1)^n \frac{1}{n}
\sum_{d|n} (-1)^d \sum_{\gcd(q,n/d) = 1} 1
\\ = (-1)^n \frac{1}{n} \sum_{d|n} (-1)^d \varphi(n/d).$$
We  evaluate  this  using   formal  Dirichlet  series,  starting  from
$\sum_{d|n} \varphi(d) = n$ which yields
$$\sum_{n\ge 1} \frac{\varphi(n)}{n^s} = \frac{\zeta(s-1)}{\zeta(s)}.$$
Furthermore we have
$$\sum_{n\ge 1} \frac{(-1)^n}{n^s} =
-\left(1-\frac{2}{2^s}\right)\zeta(s).$$
This means that
$$\sum_{n\ge 1} \frac{1}{n^s} \sum_{d|n} (-1)^d \varphi(n/d)
= -\left(1-\frac{2}{2^s}\right) \zeta(s-1).$$
Now we have for $n$ even
$$- (-1)^n \frac{1}{n} [n^{-s}] \left(1-\frac{2}{2^s}\right) \zeta(s-1)
= - (-1)^n \frac{1}{n} (n - 2 [(n/2)^{-s}] \zeta(s-1))
\\ = - (-1)^n \frac{1}{n} (n - 2 \times n/2) = 0.$$
We obtain zero as required. On the other hand with $n$ odd we find
$$- (-1)^n \frac{1}{n} [n^{-s}] \left(1-\frac{2}{2^s}\right) \zeta(s-1)
= - (-1)^n \frac{1}{n} [n^{-s}] \zeta(s-1)
\\ = - (-1)^n \frac{1}{n} \times n = 1,$$
again as  required. This  concludes the sanity  check. and  indeed the
entire argument.
Remark. Some  of this  material is duplicate  where I  have not
found the links however.
Addendum. The sum can be simplified. Starting with
$$\sum_{p=1}^{n-1} [[n/\gcd(p, n)|k]]
\times {\gcd(p,n) \choose k \gcd(p,n)/n} (-1)^{k\gcd(p,n)/n}.$$
With the GCD being $d$ we get
$$\sum_{d|n \wedge d \lt n}
\sum_{\gcd(p,n) = d}
[[n/d|k]]
\times {d \choose kd/n} (-1)^{kd/n}.$$
where we retain $1\le p\le n-1$. This is
$$\sum_{d|n \wedge d \lt n}
\sum_{\gcd(q,n/d) = 1}
[[n/d|k]]
\times {d \choose kd/n} (-1)^{kd/n}
\\ = \sum_{f|k \wedge f\gt 1} \sum_{d|n \wedge d \lt n}
\sum_{\gcd(q,n/d) = 1, n/d=f}
{d \choose kd/n} (-1)^{kd/n}$$
Here we have $1\le qd\le n-1$ but since $d\lt n$ we may actually use
$1\le qd\le n$ to obtain (this is because $\gcd(n/d,n/d) = n/d \gt 1$)
$$\sum_{d|n \wedge d \lt n \wedge n/d|k}
\sum_{\gcd(q,n/d) = 1}
{d \choose kd/n} (-1)^{kd/n}
= \sum_{d|n \wedge d \lt n \wedge n/d|k}
\varphi(n/d) {d \choose kd/n} (-1)^{kd/n}
\\ = \sum_{d|n \wedge d \gt 1 \wedge d|k}
\varphi(d) {n/d \choose k/d} (-1)^{k/d}
= \sum_{d|\gcd(n,k) \wedge d \gt 1}
\varphi(d) {n/d \choose k/d} (-1)^{k/d}.$$
Merge this into the boxed formula to get
$$\frac{1}{n} +
(-1)^k {n\choose k}^{-1}
\frac{1}{n}
\sum_{d|\gcd(n,k) \wedge d \gt 1}
\varphi(d) {n/d \choose k/d} (-1)^{k/d}$$
or alternatively
$$\bbox[5px,border:2px solid #00A000]{
(-1)^k {n\choose k}^{-1}
\frac{1}{n}
\sum_{d|\gcd(n,k)}
\varphi(d) {n/d \choose k/d} (-1)^{k/d}.}$$


Regexp to search/replace only text, not in HTML attribute

Question: I'm using JavaScript to do some regular expression. Considering I'm working with well-formed source, and I want to remove any space before[,.] and keep only one space after [,.], except that [,.] is part of a number. Thus I use:
text = text.replace(/ *(,|\.) *([^ 0-9])/g, '$1 $2');

The problem is that this replaces also text in the html tag attributes. For example my text is (always wrapped with a tag):

Test,and test . Again  ...

Now it adds a space like this src="xyz. jpg" that is not expected. How can I rewrite my regular expression? What I want is

Test, and test. Again  ...

Thanks!
Comment: This isn't something Regex's are good at as HTML isn't a regular language.  There is too much scope/nesting/context.
Comment: Is that text accessibly through the DOM?
Comment: Yes, I think, even I haven't tried. I wanted to write it as a CKEditor plugin, that's why I said "well-formed" (well, I meant XHTML anyway). I have the source code, but I think I can get is as DOM elements.
Answer: You can use a lookahead to make sure the match isn't occurring inside a tag:
text = text.replace(/(?![^<>]*>) *([.,]) *([^ \d])/g, '$1 $2');

The usual warnings apply regarding CDATA sections, SGML comments, SCRIPT elements, and angle brackets in attribute values.  But I suspect your real problems will arise from the vagaries of "plain" text; HTML's not even in the same league. :D
Comment: I doesn't work. "Test,and" should become "Test, and". I was thinking of lookafter too, but I couldn't get it. Something like looking for "...> anything but < (text to find/replace)".

And I think the [^<>]* part above is not necessary.
Comment: There more asterisks in there when I tested it, but they disappeared.  Try it now.
Comment: I was using another solution. But this one is much better :) Thanks.
Comment: @AlanMoore can you kindly provide a regex to also take care of any Search pattern provided....
Answer: Do not try to rewrite your expression to do this. You won't succeed and will almost certainly forget about some corner cases. In the best case, this will lead to nasty bugs and in the worst case you will introduce security problems.
Instead, when you're already using JavaScript and have well-formed code, use a genuine XML parser to loop over the text nodes and only apply your regex to them.
Answer: If you can access that text through the DOM, you can do this:
function fixPunctuation(elem) {
    // check if parameter is a an ELEMENT_NODE
    if (!(elem instanceof Node) || elem.nodeType !== Node.ELEMENT_NODE) return;
    var children = elem.childNodes, node;
    // iterate the child nodes of the element node
    for (var i=0; children[i]; ++i) {
        node = children[i];
        // check the child's node type
        switch (node.nodeType) {
        case Node.ELEMENT_NODE:
            // call fixPunctuation if it's also an ELEMENT_NODE
            fixPunctuation(node);
            break;
        case Node.TEXT_NODE:
            // fix punctuation if it's a TEXT_NODE
            node.nodeValue = node.nodeValue.replace(/ *(,|\.) *([^ 0-9])/g, '$1 $2');
            break;
        }
    }
}

Now just pass the DOM node to that function like this:
fixPunctuation(document.body);
fixPunctuation(document.getElementById("foobar"));

Comment: You mis-spelt the function name `fixPunctuation` as `fixPunctutation` a few times ;)
Answer: Html is not a "regular language", therefore regex is not the optimal tool for parsing it.  You might be better suited to use a html parser like this one to get at the attribute and then apply regex to do something with the value.
Enjoy!
Comment: That's a Java HTML parser. He want to do this in JavaScript.
Answer: Don't parse regexHTML with HTMLregex. If you know your HTML is well-formed, use an HTML/XML parser. Otherwise, run it through Tidy first and then use an XML parser.
Comment: @Scytale - He's just being thorough; so long as we're on the subject, though, people shouldn't parse RegEx with HTML either! ;)
Comment: You probably mean "don't parse HTML with regex", not the other way around. ;)
Comment: @Scytale @Richard hahaha I didn't even see that. My bad - will fix :)
Answer: As stated above and many times before, HTML is not a regular language and thus cannot be parsed with regular expressions.
You will have to do this recursively; I'd suggest crawling the DOM object.
Try something like this...
function regexReplaceInnerText(curr_element) {
    if (curr_element.childNodes.length <= 0) { // termination case:
                                               // no children; this is a "leaf node"
        if (curr_element.nodeName == "#text" || curr_element.nodeType == 3) { // node is text; not an empty tag like

            if (curr_element.data.replace(/^\s*|\s*$/g, '') != "") { // node isn't just white space
                                                                     // (you can skip this check if you want)
                var text = curr_element.data;
                text = text.replace(/ *(,|\.) *([^ 0-9])/g, '$1 $2');
                curr_element.data = text;
            }
        }
    } else {
        // recursive case:
        // this isn't a leaf node, so we iterate over all children and recurse
        for (var i = 0; curr_element.childNodes[i]; i++) {
            regexReplaceInnerText(curr_element.childNodes[i]);
        }
    }
}
// then get the element whose children's text nodes you want to be regex'd
regexReplaceInnerText(document.getElementsByTagName("body")[0]);
// or if you don't want to do the whole document...
regexReplaceInnerText(document.getElementById("ElementToRegEx"));


Funções do PHP adaptadas ao MySQL

Question: Ao tentar otimizar algumas queries para não depender do PHP para fazer algo que poderia vir pronto do banco de dados, eu estava tentando adaptar as duas rotinas abaixo em forma de functions do MySQL:
function encode( $input, $index ) {

    $base    = strlen( $index );
    $input  += pow( $base, 4 );
    $output  = '';

    for( $i = floor( log( $input, $base ) ); $i >= 0; $i-- ) {

        $bcp     = bcpow( $base, $i );
        $start   = floor( $input / $bcp ) % $base;
        $output .= substr( $index, $start, 1 );
        $input   = $input - ( $start * $bcp );
    }

    return $output;
}

E:
function decode( $input, $index ) {

    $base   = strlen( $index );
    $output = 0;
    $length = strlen( $input ) - 1;

    for( $i = 0; $i <= $length; $i++ ) {

        $bcpow   =  bcpow( $base, $length - $i );

        $output += strpos( $index, substr( $input, $i, 1 ) ) * $bcpow;
    }

    $output -= pow( $base, 4 );

    $output = sprintf( '%F', $output );
    $output = substr( $output, 0, strpos( $output, '.' ) );

    return (int) $output;
}

São funções que codificam um integer em uma string e vice-versa, um  AlphaID, quase como o do Youtube.
Como até o presente momento eu nunca havia criado uma function do MySQL, li o manual (que não é exatamente amigável) e cheguei, pelo menos quanto a function de codificação, nisso:
DROP FUNCTION IF EXISTS ENCODE_ALPHAID;

DELIMITER $$

CREATE FUNCTION ENCODE_ALPHAID( input integer ) RETURNS CHAR( 6 ) DETERMINISTIC

BEGIN

    DECLARE output CHAR( 6 );

    DECLARE letters CHAR( 62 );
    DECLARE base TINYINT( 2 );

    DECLARE iterator TINYINT( 2 );
    DECLARE bcp CHAR( 9 );
    DECLARE start TINYINT( 2 );

    SET output   = '';

    SET letters  = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    SET base     = CHAR_LENGTH( letters );
    SET input    = input + POW( base, 4 );

    SET iterator = FLOOR( LOG( base, input ) );

    ENCODING: LOOP

        SET bcp      = POW( base, iterator );
        SET start    = FLOOR( input / bcp ) % base + 1;

        SET output   = CONCAT( output, SUBSTR( letters, start, 1 ) );

        SET input    = input - ( start * bcp );

        SET iterator = iterator-1;

        IF iterator < 0 THEN LEAVE ENCODING; END IF;

    END LOOP ENCODING;

    RETURN output;

END $$

DELIMITER ;

A rotina foi criada com sucesso mas tem alguns problemas:

A rotina original, em PHP, codifica corretamente até o valor máximo possível, descrito pela constante PHP_INT_MAX (2147483647), porém a adptação para o MySQL não. Em algum momento, que não pude depurar a fundo, a codificação aparentemente pára, e as queries não retornam dados.
Até onde a codificação funciona, ela sempre atua "um pra frente".Por exemplo, codficar o inteiro 2, ao invés de retornar o esperado baaac está retornando baaad

Já quanto à função de decodificação:
DROP FUNCTION IF EXISTS DECODE_ALPHAID;

DELIMITER $$

CREATE FUNCTION DECODE_ALPHAID( input integer ) RETURNS INTEGER( 10 ) DETERMINISTIC

BEGIN

    DECLARE output INT( 10 );

    DECLARE letters CHAR( 62 );
    DECLARE base TINYINT( 1 );
    DECLARE length TINYINT( 1 );

    DECLARE iterator TINYINT( 1 );
    DECLARE bcp CHAR( 9 );
    DECLARE dot CHAR( 1 );

    SET output   = 0;

    SET letters  = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    SET base     = CHAR_LENGTH( letters );
    SET length   = CHAR_LENGTH( input ) - 1;

    SET iterator = 0;
    SET dot = '.';

    ENCODING: LOOP

        SET bcp      = POW( base, length - iterator );

        SET output   = output + LOCATE( SUBSTR( input, iterator, 1 ), letters ) * bcp;

        SET iterator = iterator+1;

        IF iterator <= length THEN LEAVE ENCODING; END IF;

    END LOOP ENCODING;

    SET output = ( output - POW( base, 4 ) ) + 0.0;

    SET output = SUBSTR( output, 0, LOCATE( dot, output ) );

    RETURN output;

END $$

DELIMITER ;

Sequer decodifica e, acredito eu, seja pela forma a qual o autor original das funções em PHP operou os resultados, formatando o cálculo matemático para float, operando como string e convertendo de volta pra integer que me faltou conhecimento em adaptar corretamente.
Comment: Mesmo que a necessidade real do projeto seja ter uma representação textual única do ID do registro, fazendo diretamente pelo MySQL posso me valer de tipos maiores, como o BIGINT, e ter 9,223,372,036,854,775,807 combinações, uma para cada registro.
Comment: Faltou os caracteres: `~!@$*()-_+=|,.`, não seria isso o problema?
Comment: Não seria melhor usar o [próprio método do MySQL](http://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_encrypt) para isso? Ou [esse](http://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_encode).
Comment: Não sabia que haviam funções nativas para isso. Eu poderia então, nesse caso, fazer `ENCRYPT( \`c\`.\`cid\`, 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' )`, porque está retornando `NULL`. E usando AES_ENCRYPT, já que a outra está depreciada eu não consigo ver o que retorna pois aparece *Binary* na GUI que eu uso
Comment: @BrunoAugusto base64 nao serve pro que  vc quer (tem do Mysql 5.6 em diante)? Ou é pra compatibilizar com algo já existente?
Comment: Não, não é pra manter compatibilidade com nada, é apenas uma característica do projeto na qual uma determinada coisa, bem estrutura já, a qual múltiplos usuários podem ter múltiplas "cópias", precisam obrigatoriamente ter uma representação textual (letras e números apenas). O mais plausível pra mim até o momento que decidi portar essas funções, era um *AlphaID* sob a chave-primária
Answer: Para gravar você pode fazer o seguinte:
INSERT INTO tabela (id, content)
VALUES (1, AES_ENCRYPT('seu conteúdo aqui', 'senha123_hash'));

E depois recuperar assim:
SELECT CAST(AES_DECRYPT(content, 'senha123_hash') as CHAR) AS content_decrypted,
       content as content_encrypt
FROM tabela WHERE id=1;

Neste caso, de precisar gerar uma senha alpha_id você grava os dois, o conteúdo encriptado e a senha encriptada neste formato, e depois na consulta você utiliza a o hash alpha_id para buscar o conteúdo, exemplo:
INSERT INTO tabela (id_tst, content, alpha_id)
VALUES (6, AES_ENCRYPT('seu conteúdo aqui', SHA2(MD5('senha123_hash'),512)),SHA2(MD5('senha123_hash'),512));

SELECT CAST(AES_DECRYPT(content, SHA2(MD5('senha123_hash'),512)  ) as CHAR) AS content_decrypted,
       content as content_encrypt,
       alpha_id
FROM tabela WHERE alpha_id='ea921eccc6b836f894710943da3b5a9ff546cb775e4d7c9e00a892d313165967e8a7eb2ebe105c8f1a0fcc74e47a77a2e901c8b450baa8de5f4a5a13b83c4415';

Ou com 256:
 INSERT INTO tabela (id_tst, content, alpha_id)
 VALUES (6, AES_ENCRYPT('seu conteúdo aqui', SHA2(MD5('senha123_hash'),256)),SHA2(MD5('senha123_hash'),256));

 SELECT CAST(AES_DECRYPT(content, SHA2(MD5('senha123_hash'),256)  ) as CHAR) AS content_decrypted,
        content as content_encrypt,
        alpha_id
 FROM tabela WHERE alpha_id='069b8392f647410845de6b9d796b6c2e0a628194682bb2c3e5ec8b978e5787c9';

Se você não faz questão de encriptar o seu conteúdo no banco, também dá para gerar um alpha_id, apenas convertendo a saída para o hash:
SELECT content, CONCAT(CAST(content as CHAR), SHA2(id, 256)) AS alpha_id
FROM tabela WHERE id=1

Comment: Mas nesse caso eu estaria decodificando antes de exibir e a intenção solucionável pela "técnica" do *AlphaID* é justamente mostrar codiicado numa seqüência de apenas letras e números
Comment: Você pode codificar por um tipo de `hash` que deseja alpha numerico .
Comment: Experimentei o SHA1() e todas as combinações do SHA2() mas por serem mais para criptografia o comprimento ficou meio longo e exibir isso seria complicado. Com *AlphaID*, mesmo um número grande como um integer 32bits fica bem pequeno
Comment: MD5 é 32, mas você pode quebrar essa string no banco, é só limitar o varchar(tamanho)
Comment: Porque no final, o que te interessa é ter uma ID alpha_id certo, que traga um conteúdo secreto. e no lugar da senha, você coloca o conteúdo mesmo, ou a ID do registro + algumas palavras secretas.
Comment: Só que se que quebrar um hash aumenta consideravelmente o risco de colisão
Comment: quebra e concatena com a id. Nunca mais vai ter colisão.
Comment: exemplo: 6c2e0a628194682bb2c3e5ec8b978e5787c9**2**. sendo 2 a id

Funções do PHP adaptadas ao MySQL


redis lock acquiring not working - stays running and never acquires

Question: this is my code:
pool = aioredis.ConnectionPool.from_url("redis://localhost:6379")
redis = await aioredis.Redis(connection_pool=pool, ssl=False, ssl_cert_reqs="None")

async with redis.lock("hello_lock") as lock:
    print("hello")
    await redis.hset("hello", "1", "2")

the Context Manager never gets entered and it seems like the lock is not being acquired at all. The program stays running/stuck on this line:
async with redis.lock("hello_lock") as lock:
Acquiring the lock should be a very quick thing so i'm confused whats happening here. Any help would be appreciated.
Comment: my redis server is local and im testing with it. Im 100% sure there are no other clients connected
Comment: I made an answer with a possible solution
Comment: Your bug seem to be in the Redis server configuration, if you have other clients connected to Redis and they are executing long-running commands, the lock acquisition could be blocked
Comment: @Saxtheowl whats weird is that locking works with other libraries such as aioredlock. But not with aioredis built-in locking
Answer: It might be a problem with the Redis lock implementation, try the following commands to manually acquire and release a lock:
127.0.0.1:6379> SET my_lock "1" NX
OK
127.0.0.1:6379> GET my_lock
"1"

If that still dont work try another method of lock like RedLock or RLock