Preferred JavaScript mode for emacs? Is it js2-mode from Yegge?

Question: There are a couple JavaScript modes out there:

js2-mode by Steve Yegge
javascript.el by Karl Landström.
espresso?
Something else?

Does anyone have a recommendation on which to use?

EDIT
2011 June 11 - This is sort of an old, outdated question at this point. FYI: Emacs v23 now includes a javascript mode, it's called js-mode and it is basically a renamed and updated espresso mode.  I've chosen to use the built-in mode.
Comment: I've been using a [js2-mode fork](https://github.com/mooz/js2-mode) that has decent community backing/support in fixing things. The fixes made me switch back to js2-mode using, instead of js-mode.
Answer: There's also espresso-mode.
A note from the espresso-mode homepage:

   Espresso has been incorporated into GNU Emacs starting with version 23.2 and has been renamed js-mode.
Comment: +1 espresso-mode is much better for navigating around functions (`C-M-a` and `C-M-e`) than js2-mode. The author also actively responds to issue requests unlike js2-mode
Comment: Good to know.  I was ready to pull the trigger on js2-mode but now I have to rethink it.
Comment: js-mode (formerly expresso) doesn't handle etags very well, even when used with exuberant-ctags. It chokes on namespaced functions. e.g. typing ```M-.``` on ```this.myFunction()``` would look for ```.myFunction``` instead of ```myFunction```. It also would only look at files open in other buffers instead of everything indexed in TAGS. Check out mooz's community fork of js2-mode, which is actively maintained. https://github.com/mooz/js2-mode/
Answer: I use js2-mode for JavaScript authoring, and it's excellent. It's not ideal for JavaScript-like languages, though (ActionScript), so it's good to have a simple mode around as a backup for those cases.
Comment: Conway, everyone else understood what mr unknown was saying. give him a break.
Comment: @Alex, you mean espresso-mode.  The js2-mode (by Steve Yegge) is _not_ being included into Emacs.  They included espresso-mode, and renamed it js-mode.
Comment: js2-mode already included into GNU Emacs tree, and will released as part of GNU Emacs 23.2
Comment: So JavaScript is not "JavaScript-like"? Do you mean to say "non-JavaScript languages that are similar to JavaScript"?


How to prove $\sup(f(x)^2) = (\sup(f(x)))^2$

Question: I started the proof with this:
$$\sup(f(x)^2) = \sup(f(x)*f(x)) = \sup(f(x)) * \sup(f(x) = (\sup(f(x)))^2$$
But it doesn't feel right. Any ideas or comments?
Answer: Let $f:[-10,1] \to \mathbb{R}$ such that $f(x)=x$. Then $sup(f(x)^2)=100$ and $(sup(f(x)))^2=1$. They are not equal.
Comment: Thank you very much!, But what happens when $x \in \R^d$ and $f: \R^d -> \R$ ?
Comment: You can also find many counterexamples in the setup you gave. Notice that $f$ has no any special property like continuous.


Comunicação entre forms

Question: Tenho um form que possui um componente de edição de texto(RichTextBox) e crio um form de busca(Find/famoso CTRL + F). A minha dúvida é como faço para um form "conversar" com o outro.
Por exemplo, digitar algo para pesquisar no form de busca e marcar no form que possui o componente de edição de texto.
Pensei inicialmente em passar como parâmetro o primeiro form para a de busca, mas não me parece certo. É certo passar um window para outro como parâmetro? Caso a resposta seja não, como deve ser feito essa comunicação.
OBS.: lembrando que estou usando c# e WPF.
Comment: Se você passar a janela toda como parâmetro, vai criar um acoplamento entre os dois e isso não é bom do ponto de vista da manutenção. Eu só mexi com WPF uma vez então não lembro, mas se você pode passar parâmetros a mais, siga o estilo do Android e passe uma cópia dos dados como parâmetro.
Answer: Encontrei a resposta em um post no próprio stackoverflow.com, segue o link https://stackoverflow.com/questions/7901007/sending-event-between-2-wpf-windows
Aceito sugestão de outras soluções.
Agradeço pela atenção!

Comunicação entre forms


Based on beta function, show that $\int_{-\pi/2}^{\pi/2} (\cos{\theta})^{n-1} d{\theta} = β(n/2, 1/2)$

Question: I am trying to solve this problem but seems that I am stuck. In order to show that $\int_{-\pi/2}^{\pi/2} (\cos{\theta})^{n-1} d{\theta} = β(n/2, 1/2)$.
I understand that beta function follows β(a, b)= $\int_0^1 x^{a-1}(1-x)^{b-1} dx $, and it can also be expressed as β(a, b)= Γ(a)Γ(b)/Γ(a+b).
I have tried to solve the problem from both ways, I got Γ(1/2)=$\sqrt{\pi}$, and also tried the option of letting $\sqrt{x}=cos{\theta}$. But I can't process further to find the solution.
So please help, thank you.
Answer: Put $x=\cos \theta$ then $dx = -\sin \theta d \theta = -(1-x^2)^{\frac 12}d \theta$. However, before doing this we must ensure that the substitution is $1-1$ , so we use the fact that $\cos$ is an even function, to write $\int_{- \frac \pi 2}^{\frac \pi 2} (\cos \theta)^n d \theta = 2\int_{0}^{\frac \pi 2} (\cos \theta)^n d \theta $. Therefore, we get :
$$
\int_{0}^{\frac \pi 2} (\cos \theta)^n d \theta = \int_0^1 x^n(1-x^2)^{- \frac 12} dx
$$
now put $y = x^2$, so that $dy = 2xdx $ so $dx = \frac{dy}{2 \sqrt y}$. Now we get :
$$
\int_0^1 x^n(1-x^2)^{- \frac 12} dx = \frac 12\int_0^1 y^{\frac n2} (1-y)^{- \frac 12} y^{- \frac 12}dy\\ =
\frac 12\int_0^1 y^{\frac {n+1}2 - 1} (1-y)^{- \frac 12}dy = \frac{\beta(\frac{n+1}{2},\frac 12)}{2}
$$
this $2$ cancels out with the $2$ we got from the even break up at the start, leading to the final answer being $$
\int_{- \frac \pi 2}^{\frac \pi 2} (\cos \theta)^n d \theta = \beta\left(\frac{n+1}{2},\frac 12\right)
$$
Your problem is solved by changing $n$ to $n-1$ in this argument. Also, since you have apparently attempted this approach but failed, it is possible that some of the steps need to be elaborated further, so kindly let me know.


Rails App on Heroku Loads Javascript, but JS functions are not running

Question: I have a rails app deployed on heroku and am having some issues getting new javascript, written with jquery, working on the deployed version of the site.
I've tried many different ways to precompile the assets, and it appears they are actually precompiled when deployed to heroku. I don't however believe that precompiling is the issue. The strange thing is I can see the new Javascript functions loaded in the browser.
So for example I have one simple function that hides a button until another button is clicked, very simple and it works fine in development. On the deployed version of the site I am able to find that function in the application.js source file but the button it should be hiding is not hidden.
So it seems the javascript is loaded, but not working. Has anyone experienced this issue when deploying with Heroku?
Here is an example of some of the javascript that is not working, again written in jquery.
<code>$('#remove_sock_selection').click(function(){
  $('#sock_images > ul > li > div').removeClass('selected').removeClass('make-thumbnail').removeClass('make-selected');
  $('#sock_images > ul > li').removeClass('mx-0');
  $('#sock_images > ul').removeClass('make-centered');
  $('#sock_images').addClass('mt-5');

  $('#sock_image_selected').hide();
  $('#packaging_images').hide();
  $('#addAdditionalSock').show();
});
</code>
it's really just adding/ removing css classes based on when things are clicked.
Answer: Do you already reset the cache or reboot your production? could you send the js you added? Just double check your production system if already been reboot/reset cache of you rails. It is a common issue when a js file updated in the production.
or maybe try reset your browser cache. Hope this will help.
Comment: I just updated the question with sample js code. It's definitely not the browser cache I tried clearing that as well as using different computers. However how would I clear the cache on the production system? Is there a heroku command for that, I don't have any specific cache setup so it would be whatever is the default,.
Comment: have you already check the name of the button? also, you already include the javascript in the file where the js will be trigger? because sometimes maybe your development system is fully sync/clean while in production some of the file may differ.


How to conditionally enable onClick

Question: I'm creating a table by mapping my data , I'm trying to create a TableSortLable , but I only want to enable the users to sort by the first two columns .
          {
            Columns.map(c,index => {
              return (
                <TableCell key={c.key} component="th" padding="checkbox">
                  <TableSortLabel
                     active={props.brokersListOrderByColumn === c.key}
                     direction={props.brokersListSortOrder}
                     onClick={() => props.setOrderBy(c.key)}>
                     {c.label}
                  </TableSortLabel>
                </TableCell>
              )
            })
          }

I was thinking on using the onClick event, and to enable it only when its the first or second column . I tried something like:
   {(index === 1 || index ===2) && onClick={() => props.setOrderBy(c.key)}}>

Or conditionally setting the onClick value , but both didn't work .
I've also tried conditionally setting the TableSortOrder :
                <TableCell key={c.key} component="th" padding="checkbox">
                  {(index === 1 || index === 2) && <TableSortLabel
                     active={props.brokersListOrderByColumn === c.key}
                     direction={props.brokersListSortOrder}
                     onClick={() => props.setOrderBy(c.key)}>
                     {c.label}
                  </TableSortLabel>}
                  {(index !== 1 && index !== 2) && c.label}
                </TableCell>

But it complains c is undefined
Comment: Is `TableSortLable` a typo? (on line 1)
Answer: Syntax error. You should write parameters in brackets for functors. Like this
Columns.map((c, index) => {...

Otherwise engine will try to find (RHS) variable c (expected as function).
You didnt init such variable and get this

But it complains c is undefined .
Answer: Try to give something like
onClick={
    () => (index === 1 || index === 2) && props.setOrderBy(c.key)
}

Answer: Try onClick={(index === 1 || index === 2) ? () => props.setOrderBy(c.key) : null}
Answer: Maybe you can just add check inside handler?
onClick={() => {
   if((index === 1 || index ===2)) {
       props.setOrderBy(c.key)}
   }
}}

You can also extract array function for breivity:
const onClick = () => {
   if((index === 1 || index ===2)) {
       props.setOrderBy(c.key)}
   }
}

....
onClick={onClick}>

Comment: Pretty sure `index` isn't defined in the JavaScript context.

How to conditionally enable onClick


SVG Clip-path with "foreignObject" on iOS fails

Question: I am trying to place YouTube(iframe) within a custom shape made with SVG.
    <svg width="500" height="500" viewBox="0 0 500 500">
    <clippath id="rect">
     <rect x="0" y="0" width="500" height="500" rx="50" ry="50" />
    </clippath>

    <g clip-path="url(#rect)">
    <foreignObject x="0" y="0" width="100%" height="100%">

    </foreignObject></g></svg>

Another sample page that attempts same goal
(Please check it out with any browser except the one on iOS)
The issue is that the clipping does not work on any browser on iOS. If you open the above sample, you would notice the clipping is gone.
Any advice appreciated.
Thanks.
Comment: If you don't provide a `width` or `height` attribute to your ``, they will each default to 0.  So you will be clipping out everything.
Comment: Thanks, it was my typo. It works fine on almost all browsers. Why not on iOS??
Answer: Workaround: apply clip path to a HTML element within <foreignObject>
We need to tweak the clip path to be responsive for HTML elements like so:
  <clipPath id="clip" clipPathUnits="objectBoundingBox">
        <rect x="0" y="0" width="1" height="1" rx="0.1" ry="0.1" />
   </clipPath>

We need to add clipPathUnits="objectBoundingBox" and scale the element to a size of 1x1 units.
See also "Eric Meyer: Scaling SVG Clipping Paths for CSS Use"

<svg width="500" height="500" viewBox="0 0 500 500">
  <clipPath id="clip" clipPathUnits="objectBoundingBox">
        <rect x="0" y="0" width="1" height="1" rx="0.1" ry="0.1" />
    </clipPath>
    <foreignObject x="0" y="0" width="100%" height="100%">

    </foreignObject>
</svg>

The iframe is blocked in SO snippets - see this codepen
<foreignObject> elements can introduce quite a lot of cross-browser rendering issues.
So you should consider a HTML/CSS approach if your svg mostly contains HTML encapsuled in a <foreignObject>.

HTML clipped

HTML rounded corners - no clip path

<svg style="width:0;height:0; position:absolute;">
  <clipPath id="clip" clipPathUnits="objectBoundingBox">
        <rect x="0" y="0" width="1" height="1" rx="0.1" ry="0.1" />
    </clipPath>
</svg>
Answer: Does wrapping the <clippath> inside <defs> help?
https://stackoverflow.com/a/14389784/708381
<defs><clipPath></clipPath></defs>


SQL - link to another table based on various rules?

Question: I have the below table which relates to sales and other dimensions.
SALEID  Shop    Channel   Value     Products     Opted In?  New/Repeat
1        HOME    EMAIL    MEDIUM    CAR          OPTED IN   NEW
2        GARDEN  PHONE    MEDIUM    INSURANCE    OPTED IN   REPEAT

I then have another table that looks like the below..
Shop      Channel   Value   Products        Opted in?     New/Repeat    RuleID         IsActive
All       Email     Medium  Car,Insurance   Opted In        All           888               1
Garden    Phone     Medium  Insurance       Opted In       Repeat         834               0

I need to link sales from the first table to pull the RuleID from the 2nd table where IsActive = 1. The link can only be made where all the values in the bottom table match the values in the top table
My issue is the way the data is stored on the 2nd table doesn't make it the easiest to link back. For example, some columns will have "All" in them... instead of the name of the SHOP. And also, the products column will sometimes have comma separated values in them instead of just one item.
In the example, the first table should pull through the ruleID 888 as it does match all of the criteria.. but what would be the best way to handle the columns that display either "ALL" or comma separated values?
I did find a split string function to handle comma separated values online, however I still can't get my head around how to implement it and deal with the "Alls"
Any ideas on how to handle this? I can't seem to find any similar issues when I search online!
Comment: Edit - so I found a way to handle the comma separated values by using this logic on the join..

 and ',' + a.products + ',' like '%,' + b.products+ ',%'
Answer: If I understand you right, then you could just do it like this:
SELECT * FROM Table1 JOIN Table2 ON (Table1.[New/Repeat] = Table2.[New/Repeat]    OR Table2.ColumnName = 'ALL')
etc
Abstract example:
http://sqlfiddle.com/#!9/cb091e/5


ckeditor implementation in react-native

Question: I want to implement CKEditor in a react-native mobile app but I am unable to find an appropriate package. I tried below but this is 2 years ago package as other too.

https://www.npmjs.com/package/react-native-webview-ckeditor

but result is error.
react-native v. 0.63.
Please help in getting rid of this.
Answer: try this
https://github.com/vitalyliber/react-native-ckeditor
React Native CKEditor component with no native code for React Native apps built using WebView
Comment: Invariant Violation: requireNativeComponent: "RNCWebView" was not found in the UIManager. Having errors
Comment: After resolving all bugs, I set the value some value but nothing is displaying there. and console having  message like Webview loaded,
WebViewEditor: sending message `my message then`

but nothing is displaying
Comment: Complete code is here: https://github.com/vitalyliber/react-native-ckeditor/issues/5#issuecomment-725303596

ckeditor implementation in react-native


transforming aggregated data to individual level data

Question: Suppose I have a data set which has 2 columns:
  visit   purchase
  5       2
  7       3

and I want to transform it to 1 column(for logit regression analysis)
In following column "purchase", 1 mean 1 purchase and 0 means no purchase, the total numbers of observation in purchase would equal to the sum of visit

I have tried
df.expanded <- df[rep(row.names(df), pmax(df$Predators, 1)),]

from this question and successfully expanded the observation. However, I don't know how to transform the value under column "purchase" after I expanded the row, as it looks like this
purchase
2
2
2
2
2
3
3
3
3
3
3
3

As the number of observation is indeed equal to 12, however, it also copied the number of purchase.
The data set I am working on now is pretty big therefore it is quite impossible to do it manually.
New:
This is part of my original dataset
https://i.stack.imgur.com/DByGX.png
and in R, the data frame is named 'try6'
So I enter this in console:
expand_01 <- function(x) {
  rep(c(1,0),
      c(x[["installs"]],x[["reach"]]-x[["installs"]]))
}
unlist(apply(try6,1,expand_01))

But an error is listed as follow:
Error in x[["reach"]] - x[["installs"]] : non-numeric argument to binary operator
I don't understand because the error said the value under those columns are non numerical(? or I misunderstood), but there are only numbers under the two columns..
Thank you for your help!!
Classes 'tbl_df', 'tbl' and 'data.frame':   379 obs. of  7 variables:
 $ dow     : POSIXct, format: "2019-05-09" "2019-05-09" ...
 $ country : chr  "IT" "IT" "IT" "IT" ...
 $ adtype  : chr  "VID" "VID" "VID" "VID" ...
 $ age     : num  6 5 5 4 4 3 3 2 2 2 ...
 $ gender  : num  1 1 2 1 2 1 2 3 1 2 ...
 $ reach   : num  15 26 2 47 4 34 2 1 45 4 ...
 $ installs: num  0 0 0 0 0 1 0 0 0 0 ...

After I put
 try8 <- try6 %>% head() %>% select(reach,installs)

please refer to this picture: https://i.stack.imgur.com/IKggu.png
and then I put
 dput(try8)

and it shows
 structure(list(reach = c(15, 26, 2, 47, 4, 34), installs = c(0,
 0, 0, 0, 0, 1)), row.names = c(NA, -6L), class = c("tbl_df",
 "tbl", "data.frame"))

names(try6) is

[1] "dow"      "country"  "adtype"   "age"      "gender"   "reach"    "installs"

New picture for
  reach <- try6$reach
  installs <- try6$installs

  new <- rep(0, sum(reach))

  for(j in 1:length(installs)){
  new[(sum(reach[0:(j-1)])+1):(sum(reach[0:(j-1)])+installs[j])] <- 1
  }

Picture: https://i.stack.imgur.com/CXS22.png
And also sometimes when there are for example 4 instalss, but the new results give five 1 (like 5 observation instead of 4)
picture: https://i.stack.imgur.com/Yc7tD.png
a lot of thanks!
Comment: can you cut & paste either `str(try6)` or `dput(head(try6))` into your question?
Comment: Hello I just edited the question! Many thanks for helping me out:)
Comment: problem could be related to `tbl` vs `data.frame`
Comment: it's not `tbl`-related, apparently.  Can't go farther without a reproducible example: if you can make the error occur with a data set `try7 <- try6 %>% head() %>% select(reach,installs)` (assuming you're using tidyverse), then you should be able to `dput(try7)` and paste the results into your question so we can reproduce the problem.
Comment: what is `names(try6)` ?
Comment: Hello, thank you for your reply! I edited my question above, many thanks!!! ( I downloaded tidyverse)
Answer: You don't need to transform your data to analyze it; you can run binomial regression:
glm(cbind(purchase,visit-purchase) ~ x1 + x2 + x3 ...,
          family=binomial(link="logit"),
          data= ...)

This is statistically equivalent to logistic regression and much more efficient!
If you really need to expand to zeros and ones ...
dd <- read.table(header=TRUE,
text="
visit   purchase
  5       2
  7       3
")
## convert to tibble, just in case that makes a difference
dd <- tibble::as_tibble(dd)
expand_01 <- function(x) {
     rep(c(1,0),
         c(x[["purchase"]],x[["visit"]]-x[["purchase"]]))
}
unlist(apply(dd,1,expand_01))

Comment: Thank you for your answer! I am doing a homework and transforming the data set is part of it. Do you also happen to know how to transform it? many thanks for help me out!
Comment: thank you for helping me out again:) I tried to expand it and there is an error: object "dd" not found. The original data I am using now is a data frame in R (is it relevant?)  many thanks:))
Comment: `dd` is the name of the data frame I used. You should substitute the name of your data frame.
Comment: Thank you! you are amazing! It is just there is this Error in apply(try4, 1, expand_01) : dim(X) must have a positive length, I tried fro this site, https://stackoverflow.com/questions/28423275/dimx-must-have-a-positive-length-when-applying-function-in-data-frame/28423503   and used lapply but it is not working... I just started using R, so I am not very familiar with it, your help is much appreciated:) Many thanks!
Comment: can't really answer that without a reproducible example.  You could edit your question to include a [mcve] ...
Comment: Hello, I updated the question. I don't know if it makes sense. Thank you so much:)
Answer: Just using indexing in a for loop as an alternative... it ain't pretty but:
visit <- c(5,7)
buy <- c(2,3)

new <- rep(0, sum(visit))

for(j in 1:length(buy)){
  new[(sum(visit[0:(j-1)])+1):(sum(visit[0:(j-1)])+buy[j])] <- 1
}

Comment: Hi there! thank you for helping me out! I tried your formula but the thing is that for original buy which are equal to 0, the outcome of the formula gives two 1. I uploaded a picture in my question to explain it, thank you in advance:)
Comment: The pictures for your formula is at the bottom under 'New', many thanks!
Comment: If this doesn't achieve what you want then please describe why in the comments here


vb.net combobox different style to C# combobox

Question: When creating a combobox on the design surface of a VB.NET app in VS 2008 it is white and when creating it in C# it is gray.
How do I create a combobox in C# that looks just like the VB.NET one?  (Style wise)
alt text http://www.tonysoenen.com/images/combos.jpg
Comment: If it is gray it may be a sign that the control has been disabled.  when you run the app, can you still type in it while it is gray?
Comment: I can't recreate this. These are completely 'clean' comboboxes just dragged from Toolbox to the form?
Comment: yes, just cleanly dragged from toolbox.  In my VS one is white where you'd type the text in VB.NET and in C# it is gray
Comment: not sure what you mean by that?
Comment: I found what the problem is, the DropDownStyle property was set to DropDownList...  :) Mystery solved! Thanks for the help
Comment: That's not cleanly dragged from the toolbox then...
Answer: The different styles are because of the drop-down style of these controls. To get the same style as for VB.NET, you can simply change the drop-down style property to "dropDown" for the ComboBox in C#.
Answer: Have you been using any custom controls? If you mouse over the Toolbox entry before you drag the control to the form, does the tooltip in both cases say "Version 220.127.116.11 from Microsoft Corporation .NET component"?
Answer: You should check the all the properties of both forms and both comboboxes to make sure they are the same. For example, it appears that your C# form has a different background color than the VB form.


A type of set used in convergence in measure theory

Question: This is not a specific problem, but a general question. Often when we're showing convergence of functions (particularly pointwise) or even of sets in certain cases,  a set of the following form appears:
$$
\bigcap_{N \in \mathbb{N}} \bigcup_{n \geq N} E_{n}
$$
Often we will encounter situations where we want to show that something goes "wrong" on a set of this form, or a set of this form is measurable (which is obviously true if $E_{n}$ are measurable); I have a general question about a set of this form.
In what contexts does this show up? In my understanding, something is in this set if it "happens" infinitely often. This is a slightly probabilistic interpretation, but if some property holds for infinitely many sets, then something like this shows up. Is that a correct interpretation? I'd appreciate some elaboration.
Example:
If we have a sequence of functions $\{f_{n}\}$, where each $f_{n}$ is supported on a measurable set $E_{n}$, then
$\bigcap_{N} \bigcup_{n \geq N} E_{n} $
is precisely the set of points where the sequence $f_{n}$ is not eventually $0$. Thus, points $x$ at which arbitrarily many $f_{n}$ are non-zero (points in infinitely many supports $E_{n}$). Is this correct?
Answer: What you've written is called $\limsup E_n$ and $x \in \limsup E_n$ iff $x \in E_n$ for infinitely many $n$. This understanding already shows your example is correct.
There is a similar notion that $\liminf E_n = \bigcup_{j \geq 1} \bigcap_{n \geq j} E_n$. $x \in \liminf E_n$ iff there is an $N$ such that $x \in E_n$ whenever $n \geq N$ (i.e. "$x \in E_n$ eventually").
These sets occur naturally when considering convergence as you noted. In fact, $\liminf E_n$ is defined to capture definitions like that of convergence which require some behaviour to occur "eventually". Then $\limsup E_n$ is just the complement of $\liminf E_n$.
For example, in the case of convergence we require that for fixed $\varepsilon > 0$, your sequence lies within distance $\varepsilon$ of the limit "eventually". More precisely, if $E_n^\varepsilon = \{x: |f_n(x) - f(x)| < \varepsilon\}$ then
$$\liminf E_n^\varepsilon = \{x: \exists N \text{ such that } \forall n \geq N, |f_n(x) - f(x)| < \varepsilon\}$$
so that we can write
$$\{x: f_n(x) \to f(x)\} = \bigcap_{k \geq 1} \liminf E_n^{k^{-1}}.$$
Conversely, if we want to find $x$ such that $f_n(x) \not \to f(x)$ then there is an $\varepsilon > 0$ such that $|f_n(x) - f(x)| > \varepsilon$ infinitely often which is the same as saying $x \in \limsup (E_n^\varepsilon)^c$. Hence we can write
$$\{x: f_n(x) \not \to f(x)\} = \bigcup_{k \geq 1}  \limsup (E_n^{k^{-1}})^c.$$


How to prune a specific part of my repo's history to eliminate bloat

Question: I was trying to remove some sensitive info from some old commits on our company's Git repo using the techniques described on this GitHub help page. Using filter-branch, I was able to modify the repo's history to my liking.
Unfortunately, I made the mistake of doing a pull from origin and doing some further work on the repo. By doing this, I believe I've effectively merged the original 'tainted' repo (A) with my 'fixed' repo (B), since the number of commit objects has doubled from 3000 to 6000.

Now, I could run the filter-branch steps again and force-push to fix up what I have, but the repo is still 'bloated' to double its size.
I know roughly where the merge occurred, but not the precise commit. I would like to be able to identify and prove which commit is the culprit, and then permanently remove commit tree A. I have a few potential ideas about how it could be done...

modifying that specific commit that joins A with B and then running a prune to garbage-collect everything under it
by deleting that commit entirely from history and replicating it later, after a prune
rebasing to the last commit on the head of repo B and cherry-picking everything above it except the one where I merged with A (not sure if cherry-picking would pull the whole commit tree back in, though!)

I welcome all suggestions!
Answer:
modifying that specific commit that joins A with B

You literally cannot do this.  But you can do something that may be just as good, or sufficiently good: you can make a copy of that commit, but before committing the copy, make it refer only to the B-side parent, not the A-side parent and the old history you wanted to remove.
Once you have copied that commit, though, you must also copy its immediate children.  The new copies will be the same as the originals except that they refer to the copy, not the original.
Of course, having copied those children, you must now copy their children.  The new copies will refer to the other new copies.  This repeats all the way through time until you reach the most recent commits.
Basically, then, what you need to do is run git filter-branch again.  The filter this time is: When you reach the specific commit that joins A with B, make a copy that doesn't do that.  All other commits get copied "as is".  The filter-branch command knows to substitute in the new parents from the first change onward.  When copying earlier commits (those in side A, and those in side B that come before this mistake), the "copies" will be bit-for-bit identical to the originals, so filter-branch will wind up re-using the originals.
The end result will be as if you had changed that one specific commit, except that it and all its descendants will be new commits.  You can then clone this repository to a new clone that doesn't refer at all to the side-A commits, and they will simply not be copied through; or you can, as you suggested, prune them away (but this is surprisingly difficult as Git desperately wants to avoid losing work, i.e., commits).  In any case, once that is one, you must convince all users of the repository to abandon their previous clones and switch to this new re-shrunken repository.
The remaining issue at this point is how you convince Git to change the parentage of that one specific commit.  There are two easy(ish) ways to do this:

use a "parent filter": see the filter-branch documentation
use git replace to construct the replacement commit first, then use git filter-branch to do the repository copying using the replacement, then discard the replacement since it's now incorporated into the copied commits.

The latter is easier to get right, since if you goof it up you can simply remove the replacement.  However, if you understand all of this, the former is not that hard to get right either, for a single commit: just write a shell script fragment of the form:
[ $GIT_COMMIT = <hash> ] && echo "-p " || cat

to use as your --parent-filter.
Comment: Thanks for the detailed answer! I shall look into this at work next week and see if can accept it.
Comment: I'm sure your answer will be useful to someone out there, although it turns out I didn't need to do anything too complex to solve my problem. I've explained the outcome in a new answer.
Comment: Ah, yes, your particular case allows you to repeat the original filtering. I should perhaps have noticed, since "remove sensitive data" tends to fall into this special case.
Answer: By shear luck, I believe that simply repeating my original actions has solved the problem, i.e. running filter-branch again on my repo has cleaned up the 'duplicate' commits.
Since my filtering process was simply to remove specific files from every commit, running the same filter again on my modified repo (B) has no effect (B' = B) whereas running it on the commits in repo A results in commits that are identical to B.
Since commit hashes are calculated by the contents of the changes and the hashes of its ancestors, and because the ancestors in A and B are now effectively identical, I end up with identical commit hashes on both sides of the tree... therefore the duplicates magically disappear! My new repo now contains just over 3000 commit object as before.
Further reading:
How does git assure that commit SHA keys for identical operations/data are still unique?
How to get the git commit count?


Display Youtube Views from Url in Label (How to?) VB

Question: Is it possible to fetch the views from an youtube url to a label in VB?
Searched for hours in google but there is nothing...
Thanks in advance
Comment: Well, like I said, we really need more info.  What are you trying to do, how are you trying to do it?  I've already suggested you could look at doing some screenscape, but that's not something I would do.  Ask a better question and get better answers
Comment: Need more info really.  However, you could use the google api https://developers.google.com/api-client-library/dotnet, or maybe screenscrape as a couple options
Comment: isn't there a way to do it without api?
Answer: First Create a ConsoleApplication
Module Module1

    Sub Main()
        Console.Title = "Youtube Fetch Views"
        Dim thread As New System.Threading.Thread(AddressOf FetchViews)
        thread.Start()
        Console.ReadKey()
    End Sub

    Sub FetchViews()
        Dim request As Net.HttpWebRequest = TryCast(Net.WebRequest.Create("https://www.youtube.com/watch?v=6MTXs8T7Omg"), Net.HttpWebRequest)
        Dim response As Net.HttpWebResponse = TryCast(request.GetResponse(), Net.HttpWebResponse)
        Dim dataStream As IO.Stream = response.GetResponseStream()
        Dim reader As New IO.StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        reader.Close()
        response.Close()
        Dim str As Text.RegularExpressions.Match = Text.RegularExpressions.Regex.Match(responseFromServer, "
(.*?)
")
        Dim views As String = Str.Groups(1).Value
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine(views)
    End Sub

End Module

Comment: Exactly a way i'm looking for but doesn't seems to work for me... is there also a way to use this in a form.vb intead of module.vb?
Comment: Yeah you can Edit the Code i hope you know how to do it if now write me an Email firstname.lastname@example.com and send me your Source Code so i can edit it to your needs.


gain score vs. residualized change score when analysing correlates of change (NO group comparison)

Question: Im interested in the relationship between change in so-called dynamic risk factors (e.g. low self-control) and recidivism, as well as the relationship between prison climate and change in dynamic risk factors. Thus, change is the IV in the first question, the DV in the second question. Dynamic risk is measured at two points of time.
In both situations, im wondering if i should use a simple difference score or a residualized change score (or using the posttest score while controlling for pretest score, respectively). I've read a bunch of articles on this topic (e.g. this or this or this) as well as some other posts in this forum (e.g. here or here), but i'm still lacking a convincing solution to my specific problem (mainly, because most literature on this topic focusses on adjusting for baseline differences in group comparisons).
Following most literature, i would generally prefer using the change score, but there is one important pitfall: dynamic risk at pretest is correlated with recidivism (otherwise, it wouldnt be a risk factor), and those with high scores at pretest show larger changes than those with low scores at pretest (to which extent this amounts to regression to the mean would be another nice topic). Therefore, those exhibiting more prosocial change are also recidivating more, something which is not really meaningful to interpret. On the other hand, controlling for the baseline seems somewhat wrong to me because the fact that people with high (negative) baseline scores are more amenable to change is not simply a statistical problem, but of theoretical and contentual interest.
Comment: I can't speak to your particular study but quite generally change scores cause more problems than they solve, due to measurement error, regression to the mean, and imperfectly transforming the variable before subtraction.
Comment: Change scores in general.  In many settings change cannot be interpreted without reference to the starting value, so the notion of analyzing change is problematic (as opposed to predicted the final value conditioning on the initial value).  Details are at http://biostat.mc.vanderbilt.edu/ManuscriptChecklist under "Inappropriate choice of measure of change".  Look at it this way: It is statistically invalid to analyze change without conditioning on the initial value anyway (b/c of bias or power loss) and it is complex to have the initial value in 2 places in the model.
Comment: Thanks for your reply. Do you mean simple difference scores, residualized change scores, or change scores in general?
Answer: I'm doing a similar type of study (measurement of dynamic risk factor over time and their effect on recidivism) and I think I have chosen my solution. If you z-score your IV before you run your analysis, your results will be far easier to interpret. I will be using a table where I present the difference in probability for recidivism at the mean change and then 1 std dev above and below the mean change. If you're trying to convert z-scores in SAS, here is a helpful link: http://www.ats.ucla.edu/stat/sas/faq/standard.htm
Comment: This does not answer the question.


How to test the effect of price increase on subscription revenue in a mobile app?

Question: I'm currently working on a mobile app that offers premium subscription plans. I'm interested in testing whether increasing the price of the subscription might result in a significant increase in revenue even though the number of subscriptions might drop. I have data on the number of subscriptions and revenue for two random samples of the population base of users. The first sample (test group) has a new, higher price, while the second sample (control group) has the original price.
The data available is:
Control group:

Size: 4704
Number of subscriptions: 78
Revenue: 1377.22

Test group:

Size: 4683
Number of subscriptions: 65
Revenue: 1604.85

Edit: prices are constant in each group.
How to test that the increase in revenue is statistically significant?
Comment: @Dave2e but the revenue is non-binomial
Comment: That's okay if the change in revenue is a simple proportionate increase between the two groups.  That would lead to a hypothesis of the form $H_0: r_0\mu_0 \ge r_1\mu_1$ where the $r_i$ are the *relative* per-subscriber revenues and the $\mu_i$ are the binomial expectations. That requires some subtlety to test properly, but permits use of the Binomial distribution. It becomes simple if you have so much information about the current mean $\mu_0$ that you can treat it as a known constant, because the hypothesis now concerns $\mu_1$ in a textbook fashion.
Comment: Is the price constant within each group?
Comment: You will need to use a proportions or binomial test for something like this.
Comment: @dimitriy yes the prices are constant in each group.
Answer: Since prices are the same within each group, the data you gave is a sufficient statistic for the analysis.
You can do this with regression or the traditional two-sample t-test:
<code>. input ///
> treated sample_size tot_revenue

       treated  sample_~e  tot_rev~e
  1. 0 4626 0
  2. 0 78   1377.22
  3. 1 4618 0
  4. 1 65   1604.85
  5. end

. generate avg_revenue = tot_revenue/sample_size

. list, clean noobs

    treated   sample~e   tot_re~e   avg_re~e
          0       4626          0          0
          0         78    1377.22   17.65667
          1       4618          0          0
          1         65    1604.85      24.69

. /* Weighted Regression Version With Heteroskedastic Errors */
. regress avg_revenue i.treated [fw=sample_size], robust

Linear regression                               Number of obs     =      9,387
                                                F(1, 9385)        =       0.87
                                                Prob > F          =     0.3509
                                                R-squared         =     0.0001
                                                Root MSE          =     2.5907

------------------------------------------------------------------------------
             |               Robust
 avg_revenue | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
   1.treated |   .0499207   .0535074     0.93   0.351    -.0549655    .1548068
       _cons |   .2927763   .0328779     8.90   0.000     .2283285    .3572242
------------------------------------------------------------------------------

. /* t-Test Equivalent */
. collapse (count) sample_size (mean) mean = avg_revenue (sd) sd = avg_revenue [fw=sample_size], by(treated)

. list, clean noobs

    treated   sample~e       mean         sd
          0       4704   .2927763   2.254954
          1       4683    .342697   2.888863

. ttesti 4683 .342697 2.888863 4704 .2927763 2.254954, unequal

Two-sample t test with unequal variances
------------------------------------------------------------------------------
         |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
---------+--------------------------------------------------------------------
       x |   4,683     .342697    .0422148    2.888863    .2599361    .4254579
       y |   4,704    .2927763    .0328779    2.254954    .2283202    .3572324
---------+--------------------------------------------------------------------
Combined |   9,387    .3176808    .0267389    2.590643    .2652667    .3700949
---------+--------------------------------------------------------------------
    diff |            .0499207    .0535074               -.0549663    .1548077
------------------------------------------------------------------------------
    diff = mean(x) - mean(y)                                      t =   0.9330
H0: diff = 0                     Satterthwaite's degrees of freedom =  8844.82

    Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
 Pr(T < t) = 0.8246         Pr(|T| > |t|) = 0.3509          Pr(T > t) = 0.1754
</code>
The estimated effect of seeing the higher subscription price is an additional .0499207 (or ~5 cents) of revenue per user (or 17%). This is the 1.treated coefficient in the regression output or the diff in the t-test output.
Unfortunately, that change is not statistically distinguishable from zero in this experiment since the p-value is 0.3509 (or half of that for the one-sided test, which makes sense here).
Having said that, you would need a test of about 67-85K users to detect a change that small, given how variable subscription revenue is.

Post Hoc Sample Size Calculation
<code>. power twomeans .342697 .2927763, alpha(0.05) power(0.8) sd1(2.888863) sd2(2.254954) onesided

Performing iteration ...

Estimated sample sizes for a two-sample means test
Satterthwaite's t test assuming unequal variances
H0: m2 = m1  versus  Ha: m2 < m1

Study parameters:

        alpha =    0.0500
        power =    0.8000
        delta =   -0.0499
           m1 =    0.3427
           m2 =    0.2928
          sd1 =    2.8889
          sd2 =    2.2550

Estimated sample sizes:

            N =    66,640
  N per group =    33,320

. power twomeans .342697 .2927763, alpha(0.05) power(0.8) sd1(2.888863) sd2(2.254954)

Performing iteration ...

Estimated sample sizes for a two-sample means test
Satterthwaite's t test assuming unequal variances
H0: m2 = m1  versus  Ha: m2 != m1

Study parameters:

        alpha =    0.0500
        power =    0.8000
        delta =   -0.0499
           m1 =    0.3427
           m2 =    0.2928
          sd1 =    2.8889
          sd2 =    2.2550

Estimated sample sizes:

            N =    84,602
  N per group =    42,301
</code>
Comment: Thanks for the detailed answer. May I ask how did you reach the conclusion that 67-85k users are needed? Is that number per group or total of both groups. Thanks.
Comment: I did a power calculation in my head for total sample size assuming 50-50 split. I will verify if I did the math right when I am near my computer again and add more detail.
Comment: @SinkingTitanic See above for the standard sample size output.
Comment: Can you please explain why you used `[fw=sample_size]` for the weights? If I understood correctly, that would assign weights to each observation as (1/sample size). In other words, observations with higher sample sizes will have less weight. Can you explain why did you chose that? Thanks a lot!
Comment: Moreover, please correct me if I am wrong, but the "Two-sample t test with unequal variances" or sometimes called "Welch's t-test" assumes normality. How did you check for that given that we have only two observations per group? Thanks.
Comment: You have 9,387 observations, with 4 distinct types. Frequency weights are weights that indicate the number of duplicated observations. They allow data to be stored more parsimoniously. The result of the command is the same as if you duplicated each observation however many times and then ran the command unweighted.
Comment: Thanks. What about the fact that t test assumes normality. When I expanded the dataset and tested for normality for example with shapiro's test, it failed. Any suggestions?
Comment: The means of the two populations being compared should follow normal distributions. This happens in large samples because of the central limit theorem, even when the distribution of observations in each group is non-normal, like you have here.
Comment: Thanks for the clarification, do you suggest any book or course to polish my statistical skills? Really appreciate your efforts.
Comment: I like the four books I recommended [here](https://stats.stackexchange.com/search?q=user%3A7071+kohavi). If you had to get just one, Georgiev would be my pick. This stuff is hard, I am still learning too.


Redirect users from authentication URLs to signup page

Question: I'm using Drupal 6.
I want to redirect users from /user/login, /user/register, /user to /sign-up if the user is the anonymous user. This page would show both login and register forms together.
The only page left would be /user/password, which form I don't want to have on /sign-up.
I'm comfortable with PHP, but I'm not sure about which hooks should I use to achieve this.
PS. I already know how to print those forms on my own. My doubt is just about the URLs.
Answer: There is no dedicated hook for things like this, but you can implement hook_init() and then do the redirect there. Something like this:
<code>function yourmodule_init() {
  global $user;
  if (in_array($_GET['q'], array('user', 'user/login', 'user/register') && !$user->uid) {
    drupal_goto('sign-up');
  }
}
</code>
Answer: The easy way, you can just enable Path module, and create URL aliases for the paths you want.
Go to admin/build/path/add and create the aliases. But this will keep the same page, if you want a more custom page, and you have already the form by your own, create a hook_menu, and place the form on it.
Comment: Enabling path and creating a URL alias will not redirect them, so this recomendation is not useful for me.
Comment: Can you please elaborate how I would use hook_menu to do this? Trying this: http://pastebin.com/uetJWxuJ does not work. Maybe the user/login path is protected?


Fastest Lap: Qualifying vs Race

Question: Fastest laps are classified as the fastest time set around a particular race track during a Grand Prix, however, due to the new fuel rules in F1 that banned mid-race refueling, some of the fastest laps are hard to break, even though the cars set qualifying times that are faster than these set times.
Take the Japanese GP at Suzuka for example, the recorded fastest lap around the circuit was a 1'31.540 by Kimi Raikkonen in 2005, but a faster 1'30.915 lap was done during qualifying by Mark Webber in 2013.
A more recent qualifying vs race time comparison was during this year's Monaco GP. Daniel Ricciardo got pole on a 1'13.622 while the recognized fastest lap was a 1'17.939 by Lewis Hamilton.
Why are Fastest Laps taken from the fastest race lap and not from the significantly quicker qualifying times?
Comment: The fact that only ten cars can possibly be on the track during Q3 is probably a big factor. Fewer opponents means fewer *slower* opponents and more space to open throttle in, and the remaining opponents are also faster than the average, all of which contributes to faster lap time overall.
Comment: True, but that doesn't apply to pre-2006 regulations, where the qualifying is done in a single timed session, or a one-shot qualifying. Although it's not just about having a Q3 to yourselves, but about the fastest times being clocked almost every time in qualifying.
Comment: Were all lap record times made in qualifying prior to 2006, then? You quote Suzuka qualifying only from 2013, for example.
Comment: If you read the linked entry and the question again, fastest laps are always recorded from the race and not from qualifying. The question was why are fastest laps taken from the race and not from the often faster qualifying times.
Comment: I phrased poorly. Are qualifying times consistently better than comparable race times? Getting better times in qualifying than the race itself, in recent years is eaiser, for reasons mentioned earlier. Are the qualifying times pre-2006 better than the corresponding race times (as these were consistent conditions and the factors above don't account for the difference)?
Comment: A couple significantly faster pole time vs race time were the 2001 Monaco GP (David Coulthard qualified pole on a 1:17.430 and also set fastest lap at 1:19.424) and 2002 French GP (Juan Pablo Montoya set a 1:11.985 pole lap and David Coulthard got fastest lap with 1:15.045)
Comment: So, "a couple" out of how many races? I think you are treating the exception as the rule.
Comment: The exception is still being considered when I asked the question, but the point remains, majority of the faster laps come from qualifying, especially in the current regulations. Counting how many qualifying pole times are faster than race times is just prolonging the discussion. What I want to know is why are the fastest race laps considered to be the "fastest laps". Is it to differentiate itself from potential "track records" at the specific circuit?
Answer: By comparing a qualifying lap time to a race lap time, you are not comparing like for like.
In qualifying, the teams turn up their engines to give maximum performance.  It is not possible to do this during a race as you would likely break the engine due to the excessive revs/power being produced for the full race distance of around 200 miles.
Also during qualifying the cars have fresh tyres (and the quickest tyres ie softest) as well as very low fuel loads.  This makes them as quick as possible for the lap or two that is required to set a qualifying time.
Fuel load is crucial, a fully laden car carries up to 100 KG of fuel at the start of the race, meaning an average lap penalty of about 3-4 seconds - every 3kg of fuel carried adds about a tenth of a second per lap, depending on the circuit.
So at the start of a race, when the tyres are in good condition, the cars are heavy and so cannot get near their times from qualifying.  As the fuel load comes down, the tyres are wearing out so once again lap times cannot compete with those in qualifying.
Most cars will change tyres once or twice per race, so the cars are usually fastest after their final pit-stops when the fuel load is relatively light and they have fresh tyres.  But even then, they will be carrying 20 Kg or so of fuel so times are still off what they were in qualifying.  Added to that the fact the engines are "turned down" to reduce the chance of a failure, you can see why qualifying lap times are not usually beaten during a race.
Finally, with the 2016 regulations, drivers can use their DRS (Drag Reduction System) whenever they like, but in the race they can only use this on the designated straight(s) when they are within 1 second of the car in front.  This can account for around 1 second per lap.
Comment: +1. It really does cone down to "apples and oranges" - through with much more subtlety than some would expect!
Comment: I guess it's just to put a distinction between fastest lap time over one lap vs fastest lap time over an entire race distance. Thinking about it, there are cars like the Toyota F1's of the mid 2000's that perform better in qualifying than in the actual race.
Answer: Official lap records have to be set during competition i.e. during the race. Laps set during qualifying sessions, practice sessions, private test days etc. don't count towards being the official lap record, but may often be referred to as the "Overall lap record", "Unofficial lap record", "Course record" and so on...
Take the Nurburgring Nordschleife as an example. The official lap record was set during the 1983 1000 km of Nürburgring, but in 2018, a modified Porsche 919 beat this time by over 1 minute. However, because this was not during competition, this is not the official lap record.

Fastest Lap: Qualifying vs Race


How do I bring up an interface on a netscreen router

Question: I took down an interface using
set interface ethernet0/0 phy link-down

with the intention of cycling it back up - but now I can;'t figure out how to bring it up.
Answer: Try:
unset interface ethernet0/0 phy link-down

Comment: So obvious... many thaks!


Two Macbooks each running Ableton 9.7.2, Midi

Question: I have ALeton Live 9 on two Macs each with their own sound card. I know that Link with midi Tempo sync, but will they Transmit midi notes from one to the other? For, example, I have a midi track running on one Mac and I want to send this midi to the other mac. And, I play a keyboard connected to one mac but want the other mac to pickup on this keyboard.
Any links to documents, youtube etc., will be appreciated as I can't seem to find any
Comment: I came across this https://help.ableton.com/hc/en-us/articles/209071169-Sending-and-receiving-MIDI-messages-using-a-virtual-MIDI-network. It sort of answers my question
Answer: Yes, you can route MIDI between two laptops running Live, I've done it before. The main thing to pay attention to is MIDI sync. Basically, you want to pick one thing (probably one of the laptops or maybe an external drum machine) that is the master MIDI clock source, set it to master, and set everything else to MIDI clock slave.
The way I've hooked it up in the past is with hardware MIDI ports on DIN connectors. I don't know a way to go USB to USB. As you have found, there are network ways to do it also. The DIN connector method is a bit old-school and unsophisticated, but it's also reliable and doesn't rely on wifi staying up or anything like that.
In Live, pretty much all of your settings will be in Preferences on the Link/MIDI tab (older versions might just call it the MIDI tab). Double-check the Sync in and out and Track in and out settings. Remote you might want to leave turned off unless you want to send start/stop messages which might require the remote to be on. If I recall correctly, the MIDI clock slave computer will not start transport when you hit play, and then it will start when you start transport on the clock master. You can change the tempo on the clock master and both tempos will follow the changes.
To send MIDI keyboard or sequence information back and forth, set the MIDI in and out settings on the individual track(s) as appropriate.
Note that we don't like to provide link answers here, since links often go dead and then the answer is useless. My answer doesn't go into all the details, and that's partly because my time to type this is limited and partly because I don't have everything memorized. I have found the Live reference manual to be quite well written, as manuals go. For some topics you do have to jump around a lot. I prefer the web version for that reason since you can follow the links through the manual.


Proof that the addition of a baseline to the REINFORCE algorithm reduces the variance

Question: A widely used variation of REINFORCE is to subtract a baseline value $b$ from the return $G_t$ to reduce the variance of gradient estimation, such that
\begin{align}
\nabla_\theta J(\theta) & \propto  \sum_s d(s|\pi_\theta) \sum_a (q_\pi(s,a)-b(s)) \nabla_\theta \pi(a|s,\theta) \\
\end{align}
I haven't found any proof that the baseline reduces the variance of the gradient estimation, is there one?
Answer: I do not know any mathematical proof but this explanation may help:
Let's say all of our rewards are positive. Using this formula you wrote above without baseline function boosts  the probability of all actions, because we are always multiplying  the log probabilities with some positive rewards. This causes to reduce variance and that's why we want to boost the probability of actions better than the average.


Como faço para calcular o média de tempo de execução de uma função?

Question: Para calcular o tempo de execução de uma função estou utilizando
inicio=time.time()
# código
fim=time.time()

Como faço para calcular o tempo repetidas vezes e depois calcular a média desse tempo?
Answer: Você pode colocar o trecho de código que você está medindo a execução dentro de um laço:
import time

REPETICOES = 10
tempos = []
for vezes in range(0, REPETICOES):
    inicio = time.time()
    # ... o código a medir entra aqui ... #
    fim = time.time()
    tempos.append(fim - inicio)

media = sum(tempos) / REPETICOES

Ou então usar o módulo timeit:
import timeit

REPETICOES = 10

# o seu código fica dentro de uma string mesmo
meu_codigo = """
j = 0
for i in range(0,999):
    j += i * j
"""

tempo = timeit.timeit(stmt=meu_codigo, number=REPETICOES)
print(tempo / REPETICOES)

Mas você pode perceber que em ambos, seja "manualmente" ou através da biblioteca, as implementações são bastante parecidas.
Answer: Uma forma interessante que o Python te permite fazer é criar um decorador que gerencie isso para você:
def time_statistics(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()

        if not hasattr(wrapper, 'times'):
            wrapper.times = []

        wrapper.times.append(end - start)
        return result

    return wrapper

Assim, na função que você quer analisar, basta fazer:
@time_statistics
def foo(a, b):
    return a + b

Efetuar as chamadas para testar o tempo:
for _ in range(100):
    foo(1, 2)

Depois analisar os resultados:
print('Tempo mínimo [s]: ', min(foo.times))
print('Tempo máximo [s]: ', max(foo.times))
print('Média de tempo [s]: ', sum(foo.times) / len(foo.times))

Veja funcionando no Repl.it.
Dica: eu utilizei uma lista simples para gerenciar os tempos, mas você pode facilmente abstrair isso numa estrutura mais complexa que já gerencie toda a lógica que necessita, seja calculando mínimos e máximo, calculando a média, desvio padrão, variância, seja gerando o gráfico.


SQL query Parent-child distinct

Question: I have a pair of SQL server tables.
P contains id and name.
PR contains id, interestrate, tiernumber, fromdate, todate and P.id. PR may contain many rows listed per p.id / tier. (tiers are a list of rates a product may have in any given date period.)
eg: Product 1 tier 1 starts 1/1/2008 to 1/1/2009 and has 6 rates shown 1 row per rate. Product 1 tier 2 starts 1/2/2009 etc etc etc
I need a view on this that shows the P.name and the PR.tiernumber and dates... BUT I want only one row to represent the tier.
This is easy:
SELECT DISTINCT P.ID, P.PRODUCTCODE, P.PRODUCTNAME, PR.TIERNO,
  PR.FROMDATE, PR.TODATE, PR.PRODUCTID
FROM dbo.PRODUCTRATE AS PR INNER JOIN dbo.PRODUCT AS P
  ON P.ID = PR.PRODUCTID
ORDER BY P.ID DESC

This gives me the exact right data... However: this disallows me to see the PR.ID as that would negate the distinct.
I need to limit the resultset because the user needs to just see just a list of tiers, I need to see the PR.ID displaying all of the data.
Any ideas?
Comment: What do you mean with "I need to see the PR.ID"? WHICH PR.ID, since there are many per row, you can only show one. Either you have a row per P.ID / Tier, or per PR.ID.
Comment: Sorry - I need to return to an application a unique ID. The user wants to see all of the product's tiers and then wants to be able to select one to be more fully populated on screen. I need to find the tier number of the item selected and then use that to fetch the rest of the data... but the application can only give me the tier number if it has the unique ID.
Answer: SELECT P.ID, P.ACUPRODUCTCODE, P.PRODUCTNAME, PR.TIERNO,
  PR.FROMDATE, PR.TODATE, PR.PRODUCTID, MIN(PR.ID)
FROM dbo.PRODUCTRATE AS PR INNER JOIN dbo.PRODUCT AS P
  ON P.ID = PR.PRODUCTID
GROUP BY P.ID, P.ACUPRODUCTCODE, P.PRODUCTNAME, PR.TIERNO,
  PR.FROMDATE, PR.TODATE, PR.PRODUCTID
ORDER BY P.ID DESC

Ought to do the job.  GROUP BY in place of DISTINCT, with a summary function (MIN) to get a particular value back for the PR.ID.
Comment: Yes this returns a particular PR.ID value, but it's not clear from the OP's question *which* PR.ID value he needs (it might not be the MIN).  Maybe he will clarify.
Comment: I thought MIN made a little more sense than SUM in this case.  ;-)
Comment: You sir, are a scholar and a gentleman. Just tried this and it works wonderfully. Thank you very much.
Comment: Bill, I just need one ID with this tiernumber, as then I fetch the data using this tiernumber.
Answer: It sounds like you want to accomplish two different things with the same query, which doesn't make sense. Either you want a list of product/tier/date information or you want a list of interest rates.
If you want to pick a particular PR.ID to go with your data then you need to decide on what the rule is for that - what determines which ID you want to get back?
Comment: Tom, thank you. Yes I am limited by the application. What I am doing is getting a view to restrict the options to the user so that they just see that a product has had 5 tiers. When they decide to view an historic tier, they select the one option and then I fetch all rates for that tier... I need to offer the application a unique ID so that it can give it back to me so I can run the next fetch.

SQL query Parent-child distinct


Simple function iff Lebesgue measurable and has finite range.

Question:
$\mathbf{Definition \ 1:}$ The outer Lebesgue measure $m^*(S)$ of a set $S\subset \mathbb{R}^n$ is
$$m^*(S):=\inf_{S \subset \cup_{i=1}^\infty B_i} \sum_{i=1}^\infty |B_i|$$
Where $B_i$ are boxes and $|B_i|$ denotes the usual elementary measure of the box $B_i$
$\mathbf{Definition \ 2:}$ A set $S\subset \mathbb{R}^n$ is Lebesgue measurable, if for every $\epsilon>0$, there exists an open set $U\subset \mathbb{R}^n$ containing $S$, such that $m^*(U\backslash S) < \epsilon$
$\mathbf{Problem:}$
Show a function $f:\mathbb{R}^n\rightarrow [0,\infty]$ is simple, if and only if it is Lebesgue measurable and takes on only finitely many values.

I am not sure how to go about the problem, and honestly have no clue where to start.
A complex valued simple function is a mapping $f:\mathbb{R}^n \rightarrow \mathbb{C}$, that is a finite linear combination of indicator functions of lebesgue measurable sets in $\mathbb{R}^n$. I.e. $f=\sum_{i=1}^n a_i 1_{S_i}$ where $a_i\in \mathbb{C}$ and $S_i$ are Lebesgue measurable in $\mathbb{R}^n$.
I apologize for the lack of work, and would appreciate any help. Thanks in advance!
Answer: Any sum of the type $\sum\limits_{i=1}^{n} a_i I_{S_i}$ with $S_i$'s measurable  can be expressed in the form $\sum\limits_{i=1}^{m} b_i I_{T_i}$ where $T_i$'s are disjoint and $T_i$'s measurable.
If $f=\sum\limits_{i=1}^{m} b_i I_{T_i}$ then $f$ takes only the values $b_1,...b_m$ and $0$ and it is Lebesgue measurable because inverse image of any Borel set $B$ is a union of those $T_i$'s for which $b_i \in B$.
Conversely, if $f$ has these properties and $b_1,b_2,...,b_n$ are the distinct values of $f$ then $f=\sum\limits_{i=1}^{m} b_i I_{T_i}$ where $T_i=f^{-1}(\{b_i\})$.
Answer: Suppose $f$ is lebesgue measurable and take finitely many value (i.e. $f(\mathbb R^n)=\{a_1,...,a_m\}$ is finite). Then $f^{-1}(\{a_i\})$ are measurable and $$f=\sum_{i=1}^m a_i\boldsymbol 1_{f^{-1}(\{a_i\})},$$
and thus is simple. The converse is straightforward.


Instantiating object with foreignkey attribute with the Django shell

Question: my question is quite simple :
In Django Shell how to instantiate an object which requires a foreign key attribute?
The models.py
class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

class Album(models.Model):
    artist = models.ForeignKey(Musician)
    name = models.CharField(max_length=100)
    release_date = models.DateField()
    num_stars = models.IntegerField()

In the django shell...
p = Person(
        first_name="Fred"
       ,last_name="Flintstone"
       ,instrument="guitar"
    )

p.save()

...

a = Album(
        artist = " "               #<------ WHAT DO I PUT HERE ? :)))))
       ,name="u rock "
       ,release_date ="today"
       ,num_stars="4"
     )
a.save()

Thank you!
Amadeus
Answer: When instantiating a model, ForeignKey fields need to be set to an instance of the appropriate related model. In your case:
p = Musician(
    first_name="Fred",
    last_name="Flintstone",
    instrument="guitar"
)
p.save()

import datetime
a = Album(
    artist=p,
    name="u rock",
    release_date=datetime.datetime.now().date(),
    num_stars=4
)
a.save()

Note that I've also fixed some of your typos, and used the correct data types for the release_date and num_stars attributes.


How dispersive term in KdV PDE causes smoothing

Question: In KdV equation,
$$u_t+u_{xxx}-6uu_x=0,$$
$uu_x$ is the nonlinear term which cause blow up, and $u_{xxx}$ is the dispersive term, I am wondering how the dispersive term smooth the solution, otherwise solitary wave solution won't exist.
Answer: Dispersion makes different spectral components travel at different speeds. This can be illustrated by the dispersion relation $\omega = -\kappa^3$ valid for small $|u| \ll 1$, which links the temporal frequency $\omega$ to the spatial frequency (wavenumber) $\kappa$. It follows that the phase velocity is a function of the wavenumber. Now consider a waveform with various frequencies inside. If the spectral frequencies separate, then high frequencies (fast oscillations) will separate from lower frequencies (slow oscillations). This 'filtering' makes the low-frequency waveform look smoother, as fast oscillations have been removed.


Show $\lim\limits_{x \to 0}\frac{e^x-1}{x} = 1$

Question: I have a homework problem which asks me to show
$$ \lim\limits_{x \to 0}\frac{e^x-1}{x} = 1 $$
Any help is appreciated.
Comment: For an exercise this basic it's more or less vital to know how $e$ is defined in your book.
Comment: I'm guessing you can't use l'Hospital, can you?
Comment: This is often considered to be the definition of $e$: the real number such that the aforementioned limit converged to $1$.
Answer: By the definition of the derivative and if we denote $f(x)=e^x$ then
$$\lim_{x\to0}\frac{e^x-1}{x}=\lim_{x\to0}\frac{f(x)-f(0)}{x-0}=f'(0)=e^0=1$$
Comment: But it may be circular to evaluate this limit by citing the derivative of $e^x$ at $x=0$...
Comment: @GEdgar: Not if you define $e^x$ as the solution of the IVP $y=y'$, $y(0)=1$. ;)
Answer: We have $e^x=1+x+\frac{x^2}{2}+O(x^3)$, so $$\frac{e^x-1}{x}=\frac{x+x^2/2+O(x^3)}{x}=1+x/2+O(x^2)$$
Taking limits as $x\to 0$, we get $1+0=1$.
Answer: By L'Hôpital's rule $$\lim\limits_{x \to 0}\frac{e^x-1}{x} = \lim\limits_{x\to0}\frac{e^x}{1} = 1$$
Comment: Don't you already need to know the derivative of $e^x$ to use L'Hopital here?
Comment: Yes.. pretty simple derivative
Comment: @OliverBel: but the limit you're supposed to calculate is precisely the value of the derivative at zero... This is the same mistake as using the rule to calculate the limit of $\sin(x)/x$ at $0$.
Comment: So? What's wrong with assuming we know the derivative? We choose $e$ to have that property.
Answer: You can use l'Hopitals rule. Notice that if we substitute directly we get $\frac{0}{0}$, an indeterminate form. So we can differentiate the numerator and denominator separately then take the limit. Then we have
$$\lim_{x\to 0} \frac{e^x-1}{x}=\lim_{x \to 0}\frac{e^x}{1}=e^0=1$$
Though I've doubt you've seen this yet, we don't even need that because we can use the Taylor series for $e^x$.
$$\lim_{x \to 0}\frac{e^x-1}{x}=\lim_{x\to 0} \frac{\sum_{n=0}^\infty\frac{x^n}{n!}-1 }{x}=\lim_{x \to 0} \frac{1+\sum_{n=1}^\infty \frac{x^n}{n}-1}{x}=\lim_{x \to 0}\frac{\sum_{n=1}^\infty \frac{x^n}{n!}}{x}=\lim_{x\to 0} \sum_{n=1}^\infty \frac{x^{n-1}}{n!}$$
But then we have
$$\lim_{x\to 0} \sum_{n=1}^\infty \frac{x^{n-1}}{n!}=\sum_{n=1}^\infty \frac{0^{n-1}}{n!}=1$$
Though seeing that last step with $0^0=1$ is a bit trickier. Not necessarily the way to do it but another way of thinking of it for those that know the Taylor Series.
EDIT: You wouldn't even need to use the fact that $0^0$ is $1$ as
$$
\lim_{x\to 0} \sum_{n=1}^\infty \frac{x^{n-1}}{n!}=\lim_{x\to 0} 1+\sum_{n=2}^\infty \frac{x^{n-1}}{n!}=1+ \sum_{n=2}^\infty \frac{0^{n-1}}{n!}=1
$$
Comment: @mathematics2x2life: but the limit is, by definition, the value of derivative! If someone asked how to calculate the volume of a $4$-ball of radius $R$, would you consider an answer that says that $\pi^2R^4/2$ because the volume of an $n$-ball is $\frac{\pi^{n/2}}{\Gamma(\frac{n}{2} + 1)}R^n$ acceptable? I wouldn't, similarly I don't think your answer is acceptable. They are equivalent, but it takes some work to show that they are equivalent -- including calculating the derivative.
Comment: I'm confused to as why my or vadim123's solutions using Taylor Series are so despised, especially since I also did it by l'Hopitals. If the down voters could elaborate so I can improve answers in the future that would be most helpful!
Comment: Haters gonna hate.  Some people *define* $e^x$ with Taylor series; then our solutions are spot on.
Comment: @vadim123: Your solution is correct under this assumption (so I'm not downvoting it, unlike this and Oliver Bel's), using L'Hospital rule is circular no matter how you define the function. Of course, it's OP's fault for not singling out a definition of $e^x$ he's using (for instance, if you define it as the solution of $y'=y$ with $y(0)=1$, this is trivial, and it makes Sami's answer *sort of* valid), but I can't think of any way to justify using L'Hospital rule for this. Can you?
Comment: @tomasz I fail to see your logic here. vadim123's solution is no different than my alternate solution to l'Hopitals other than he truncates the series after a few terms whereas I continue to work with it in its entirety. Moreover, I give the solution using l'Hopitals rule. If you don't allow solutions using l'Hopitals rule or Taylor series or using the idea that you may be taking the derivative of a certain function, I fail to see what kind of solution you wanted. It makes no sense to find fault with every possible approach saying they aren't rigorous without suggesting a method that might be.
Comment: @mathematics2x2life: it's hard to tell what a proper answer for this question would be, as OP did not define $e^x$, as I have said, but using L'Hospital is circular, as you are using the *entire* derivative to calculate the value of the derivative at zero, which is circular. Whether or not a solution using the "Taylor expansion" is correct would depend on whether you take it as the definition or the exponential function or as the Taylor expansion of $e^x$ defined otherwise. Circular logic is incorrect no matter how you look at it, though.
Comment: @mathematics2x2life: as for the correct solution, depending on your definition of $e^x$, vadim's solution may be correct, or Sami's might be, or one of the ones in the question this one is marked a duplicate of.
Comment: @tomasz To require a definition of $e^x$ to do a problem as simple as this seems overly pedantic. I highly doubt when you first learned l'Hopitals rule that questions had to be phrased as you are desribing. Moreover, you can view this as calculating a derivative or you can view it as a simple limit problem. The OP may not have specified much, but had he/she not been able to use the derivative concept, I'm sure the question would have been phrased in terms of $\epsilon-\delta$'s instead.
Comment: @tomasz Moreover, it doesn't matter how you define $e^x$ as you can show the possible characterizations of $e^x$ result in the same function. Choosing one over the other depends on its utility in the problem and preference. You can't say one isn't correct and have another be--they are equivalent!
Comment: Work or not they are equivalent! So no matter what definition you use, the answers remain the same as they are equivalent! Moreover, to demand that you require such a high level of mathematical knowledge to do so basic a problem, you ask too much of the student! You wouldn't tell a high schooler that his calculation $\int e^x dx= e^x+C$ is incorrect because he hasn't given his definition of $e^x$ and also because he didn't prove it using $\epsilon-\delta$'s.
Comment: I'm sad that the question is closed. I would to have loved to see your solution to see what you are asking for since you've only said what you *didn't* want to see versus what you think is appropriate.


How to Disinfect a Bansuri?

Question: I am seeing different ways on Internet to disinfect a Bansuri.
One person suggested submerging in water and then hold over an open flame (this doesn't sound like good idea, especially with the nylon bindings)
What is best way to disinfect Bansuri. How often to disinfect Bansuri?
I had a bad cough past month. Finally I started practicing again (past 2 to 3 days), and an hour ago I feel onset of a cough.
Answer: A wooden flute/Bansuri can be effectively cleaned externally using almond oil(wipe bansuri dry at the end), as it is said that almond oil absorbs itself in the wood easily, and penetrates deeper. If you also want to clean your tone holes (suggested), you should use cotton wool buds, as they are soft and absorb much. I use this method on my bansuri, and it works very well.

How to Disinfect a Bansuri?


C++ not detecting string in cases

Question: C++ beginner here. I've tried a ton of ways to try and fix this code but I think the way to correct it is far beyond what I know.
switch (num)
{
case 0: enemy == "Ogre";
    enetotal = "250";
    enehealth = "250";
    eneattack = "5";
    break;
case 1: enemy == "Goblin";
    enetotal = "75";
    enehealth = "75";
    eneattack = "10";
    break;
case 2: enemy == "Orc";
    enetotal = "100";
    enehealth = "100";
    eneattack = "25";
    break;
case 3: enemy == "Dark Elf";
    enetotal = "70";
    enehealth = "70";
    eneattack = "15";
    break;
case 4: enemy == "Lizardman";
    enetotal = "200";
    enehealth = "200";
    eneattack = "20";
    break;
}

cout << "You walk past a field when you see a " + enemy +", what do you do? \n";

Basically, what is supposed to happen is that, depending on a random case, it would choose a name and the health/attack for a certain enemy. It was working fine when I just had the enemy name, but once I added the attack and health it simply printed
You walk past a field when you see a, what do you do?

Something tells me cases can't handle that many lines, but I've tried many other things such as if statements and nothing seems to work. Can anyone help me fix this?
Comment: Also, have someone show you how to step through with a debugger.  That's a huge part of learning to program, and would have shown you where the error was right away
Answer: You are comparing the name
enemy == "Orc"

Instead of assigning
enemy = "Orc"

Answer: Your syntax is wrong: == is a test = is assignment.

C++ not detecting string in cases


Is there any template situation when it is more likely for doing nutmeg?

Question: What is the right situation for doing a nutmeg? When the defender stands on both feet, when one foot is closer to you than another? Or maybe it can be performed in any situation? I just want to know: is there any template situation when it is more likely for doing nutmeg?
Comment: When the cameras are on you?
Comment: Great humor bro :)
Answer: Well, this is pretty objective, but the right time for a nutmeg is when it's the easiest move to pull off considering the inherent risk involved with it (i.e. kicking the ball straight towards the defender). Ideally, the defender would be perfectly square to you with their feet spread wide and set back on their heels, perhaps right after you've done a nice hesitation move.
This would give you the best chance to thread the ball through their legs without just bashing it into their shins while they're on the run and potentially losing possession. Additionally, you don't want them to have time to react and block the ball, so it's best to preface the nutmeg with a stutter or fake pass that will make them hesitate. Mind you, a good defender will make it tough for you to ever have a good opportunity to ever pull it off - they'll be trying to move you towards the sideline or other defenders and will rarely square up.
I don't have a legit source for this other than playing for over a decade and plenty of trial and error trying to make my friends look like fools. And maybe being made the fool once or twice. But if you watch videos of it done successfully, you'll find the above generally holds true.


Getting total count of query in combination with pager

Question: My code:
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'news');
$query->pager(9);

$count = $query->count()->execute();

However my $count value is always 9 (because of the pager).
How can i get the total count of the query? (All results)
Comment: You need to perform a separate query for the count, or find a way to shoehorn [this](https://stackoverflow.com/questions/818567/mysql-pagination-without-double-querying) into it
Answer: // Use clone.
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'news');
$query->pager(9);
**$count_query = clone $query;**

$items = $query->execute();

$count = $count_query->count()->execute()

Answer: You can't reuse the same query object for these two different things. ->pager() permanently turns the query into a paged query, and ->count() permanently turns it into a count query. Trying to do both results in unpredicted behavior depending on execution order.
The code you pasted actually does return the total count, because count trumps pager if both are set when the query is first executed. But afterwards it's a count query and will always return a number:
// With 50 nodes in the database:

$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'news');
$query->pager(9);

$count = $query->count()->execute();
// $count is 50, the ->pager() is ignored.

$items = $query->execute();
// $items is 50, not an array. It's permanently a count query now.

Flipping the execution order:
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'news');
$query->pager(9);

$items = $query->execute();
// $items is an array with 9 nodes.

$count = $query->count()->execute();
// $count is 9.

Instead, you need to use two separate queries here:
$count = \Drupal::entityQuery('node');
  ->condition('status', 1);
  ->condition('type', 'news');
  ->count()
  ->execute();

$items = \Drupal::entityQuery('node');
  ->condition('status', 1);
  ->condition('type', 'news');
  ->pager(9)
  ->execute();

Comment: i for now fixed it with "global $pager_total_items;"
Comment: How does that fix it?
Comment: Hm, it's true; pager queries do still store their total counts in a global variable. I believe the numeric index in that array can be set in the second argument to pager(). Obviously terrible architecture in an OO context, but it does save an extra query.


Change css based on the page url

Question: I am using drupal 7. I wanted to change the colors of the block and everything for certain pages. The ThemeKey module works fine but i have to do all the block configurations again in this. This is tedious. Since i want to change only the css any other module will solve ma problem?
Comment: You are completely right about ThemeKey. While it does serve a specific purpose, it's usually best to keep everything in a single theme, if possible.
Answer: The body element will have a boatload of classes on it.  This does vary by theme, but typically there will be a class like
body.page-the-url-for-this-page

that you can use in your CSS.
Do a view source on various pages and take a look.  Or, use the Inspect Element feature of Firefox's Firebug or Google Chrome, or the Firefox Web Developer extension.
If the class isn't there, you can do something like
function thetheme_preprocess_html (&$variables)
{
  $variables['classes_array'][] = "page-" . str_replace("/", "-", request_uri());
}

You may also want to investigate the Context module, which can aid in section-specific features for a site.
Answer: This is a good approach, and very succinct. I think the following code works better, since 'replace' throws an error:
function thetheme_preprocess_html (&$variables)
{
  $variables['classes_array'][] = "page-" . str_replace("/", "-", request_uri());
}

Change css based on the page url


Powershell - Removing all duplicate entries

Question: I am trying to find a Powershell command line that will read in a text file remove all duplicated lines (2+) and retain none of the duplicated lines. I haven't been able to find an answer for my question anywhere on Stackoverflow nor anywhere else.  Every example I have found so far only shows removing one and/or many of the duplicated lines and retaining one.
Is this possible through Powershell 2.0?
PowerShell Command Example:
Get-Content "C:\Temp\OriginalFile.txt" | select  -unique| Out-File "C:\Temp\ResultFile.txt"

OriginalFile.txt
1
1
1
2
2
3
4

ResultFile.txt (Actual)
1
2
3
4

ResultsFile.txt (Desired)
3
4

Answer: PSv2:
$f = 'C:\Temp\OriginalFile.txt'

Get-Content $f | Group-Object | ? { $_.Count -eq 1 } | Select-Object -ExpandProperty Name

PSv3+ allows for a cleaner and more concise solution:
Get-Content $f | Group-Object | ? Count -eq 1 | % Name

For brevity, the commands use built-in aliases ? (for Where-Object) and % (for ForEach-Object).
Neither Select-Object -Unique nor Get-Unique seemingly allow restricting the output to singletons in the input (standard Unix utility uniq has such a feature built in: uniq -u), so a different approach is needed.
The above Group-Object-based solution may not be efficient, but it is convenient:

lines are grouped by identical content, yielding objects that represent each group.
? { $_.Count -eq 1 } the filters the groups down to those that have just 1 member, in effect weeding out all duplicate lines.
Select-Object -ExpandProperty Name then transforms the filtered group objects back to the input line they represent.
Comment: Thank you for the reply.  I got this working due to your response.
Comment: I did have to modify `Select-Object -Expand Name` to `Select-Object -ExpandProperty Name`
Comment: @packetloss: Good point: the full parameter name is `-ExpandProperty`, and in scripts one should definitely use the full parameter names (although `-Expand` would have worked too) - answer updated.


Setting batch_size in data_param for Caffe has no effect

Question: When I set 'batch_size' in deploy.prototxt file in Google deep dream bvlc_googlenet to lower GPU memory requirements, it has no effect on speed nor memory requirements. It's as if it was ignored. I know the file itself (deploy.prototxt) is being used because other changes are reflected in results so that's not the issue. I also tried to set batch_size on all related layers as well ("inception_4c/1x1", etc), again no difference.
This is how I'm setting it:
layer {
  name: "inception_4c/output"
  type: "Concat"
  bottom: "inception_4c/1x1"
  bottom: "inception_4c/3x3"
  bottom: "inception_4c/5x5"
  bottom: "inception_4c/pool_proj"
  top: "inception_4c/output"
  data_param {
    batch_size 1
  }
}

When I time the runtime of the script, it's the same with batch_size 1 and with batch_size 512, there is no difference.
What am I doing wrong?
Answer: data_param is a parameter of the input data layer. You can set batch_size only for the input and this value propagates through the network.
In the deploy.prototxt the batch size is set by the first 'input_dim' argument (third line), try changing this value and see if it has any effect on the memory consumption of your network.
The first few lines of the deploy.prototxt file should be interpreted as
input: "data"  # the "top" name of input layer
input_dim: 10  # 4th dimension - batch size
input_dim: 3   # 3rd dimension - number of channels
input_dim: 224 # 2nd dimension - height of input
input_dim: 224 # 1st dimension - width of input

Thus, you expect at your fist conv layer ("conv1/7x7_s2") a "bottom" named "data" with shape 10-by-3-by-224-by-224.
Comment: This changed "Memory required for data" output so it affects something. But the process is still running out of memory. Memory required is now only 50MB and the graphics card has capacity of 4GB, yet it's running out of memory "syncedmem.cpp:51] Check failed: error == cudaSuccess (2 vs. 0)  out of memory". Any ideas?
Comment: @Sergey I'm not familiar with the model/setting of the "deep dream" system. but it sounds like (a) are you sharing the GPU with other processes? (b) is it possible that memory allocations in GPU are not "free"d?
Comment: According to "nvidia-smi" command, the memory consumed by the python script in question goes all the way from 10MB (before) to 4GB (when running script). It's in a server environment.
Comment: @Sergey hoe many processes does the server run? is all the memory used by a single process?
Comment: @Sergey can you run the script line-by-line in debug and monitor the GPU mem at the same time?
Comment: It's running only one process that requires large amount of GPU memory. But I've just found that "Memory required for data" is the same for all different sizes of images. Real memory usage changes with different image sizes, but this quoted number is always only dependant on configuration of deploy.prototext. So even though "Memory required for data" is only 50MB, this number doesn't represent total memory consumption. https://github.com/BVLC/caffe/issues/2387 I'm trying to figure out how to calculate real memory usage.


Como calculo determinante de uma matriz quadrada em python?

Question: Meu arquivo está assim:
arq = open('matriz.txt', 'r')
texto = []
matriz = []
texto = arq.readlines()

for i in range(len(texto)):
    matriz.append(texto[i].split())

for i in range(len(texto)):
    print(matriz[i])

arq.close()

Comment: Poderia postar o conteúdo do arquivo de entrada `matriz.txt` também ?
Comment: seria um arquivo tipo assim 10#10#10
Answer: Você pode usar o pacote NumPy, que possui uma larga coleção de funções matemáticas capazes de trabalhar com matrizes multidimensionais.
Para se computar a determinante de uma matriz você pode usar a função numpy.linalg.det(), veja só:
import numpy

matriz = [ [1, 1, 1], [2, 2, 2], [3, 3, 3] ]

print( numpy.linalg.det( matriz ) )

Veja funcionando no Repl.it
Para ler a matriz a partir de um arquivo e calcular a sua determinante:
import numpy

matriz = []

with open('matriz.txt', 'r')  as f:
    for linha in f.readlines():
      matriz.append( [ float(i) for i in linha.split() ] )

determ = numpy.linalg.det( matriz )

print(matriz)
print(determ)

Veja funcionando no Repl.it.
Ou, simplesmente:
import numpy

with open('matriz.txt', 'r')  as f:
    matriz = [[ float(i) for i in linha.split()] for linha in f.readlines()]

determ = numpy.linalg.det( matriz )

print(matriz)
print(determ)

Veja funcionando no Repl.it.
Comment: Eu não posso usar o numpy infelizmente.


How to upgrade to Drupal 8 from 7 with clean install?

Question: I currently have a Drupal 7 site that I want to install Drupal 8, but do a clean install. This means wiping away everything and starting with a new Drupal 8 instance. I have seen many articles on how to upgrade where the content types, fields, code, and other parts are migrated. However, I just want to upgrade to Drupal 8 without any migration of the content type, files, databases, and remove old directories that are not needed for Drupal 8.
How can I upgrade to Drupal 8 without any migration for a clean instance?
Comment: The way the question is worded, it sounds like you just want to build a fresh Drupal 8 site. That wouldn't be an "upgrade" or migration, but starting from scratch. If that's the case, look into leymannx's comment, and get familiar with composer if you aren't already. If you want to upgrade with the UI without migrating with drush, please reword the question.
Comment: You don't. You just restart. On your local computer do `composer create-project drupal-composer/drupal-project:8.x-dev my-directory --no-interaction`, then `cd my-directory` and add modules etc. See https://github.com/drupal-composer/drupal-project
Comment: @leymannx your steps did the trick. I was able to get a new copy of drupal 8 installed using that composer command.
Answer: If I'm understanding your situation correctly in that you just want a new Drupal 8 website without any of the content from your existing Drupal 7 website then you would just simply install Drupal 8 from scratch, as if you didn't even have an old site.
Once the new Drupal 8 website is installed you can then create/re-create any content that you'd like.


Closed convex subset of finite dimensional Banach space is homeomorphic to a ball

Question: Let $(V, \|\cdot\|)$ be a finite dimensional Banach space, with $n:=\dim(V) \geq 2$, and consider a non-empty, closed and convex subset $A \subset V$. Suppose that there is no $(n-1)$-dimensional subspace of $V$ that contains $A$. Show that $A$ is homeomorphic to the unit ball in $V$.
I have seen similar results for compact convex subsets of the Eucilidan space $\mathbb{R}^n$, but I don't see how to prove this statement.
Answer: This is not true. Take $V=\mathbb R^2$ and $A=[0,\infty )\times [0,\infty )$. Then $A$ is closed and convex, but it's not homeomorphic to $\mathcal B:=\{(x,y)\mid x^2+y^2\leq 1\}$.
Comment: @userr777 In finite dimension, the unit ball is always convex, symmetric, nonempty and bounded. See my answer (and its remark) to this question https://math.stackexchange.com/questions/1495726/norm-induced-by-convex-open-symmetric-bounded-set-in-bbb-rn
Comment: I see now, thanks! Do you know maybe what additional condition is necessary in order to have such an homeomorphism?


R420 iDRAC7 not working when server off

Question: I have Dell R420 with iDRAC enterprise in my home lab however I don't have a dedicated management interface, it is using one of the onboard NICs for that.
However when I shutdown the system both onboard NICs go dark (i.e the LAN leds don't work anymore) and I can't connect to iDRAC or use WAL.
The iDrac IP is not responsive at that stage and etherping can't reach that system.
As I keep those in a garage and not on all the time, any help to resolve that would be greatly appreciated.
At the moment I reverted back to using smart-plug to control on/off but this is far from optimal.
Thanks in advance for any pointers
Answer: (Mandatory legal note - I work for Dell)
Haven't tested that personally but that's the behavior I would expect. The iDRAC is a separate computer entirely. Not sure about the R420 as that's quite old but newer iDRACs have their own ARM proc and memory. As long as the server is plugged in, the expansion card itself will remain on along with the card's corresponding interfaces.
That is not true of the other interfaces which are physically attached to the motherboard and are not independently powered. When you kill the server, those are going to die along with it.
Comment: This is not true for BMC's that just piggiback on the same port, at least with other systems (Supermirco). I havve a number of recent ones where the mnagement port is shared with Eth1 - but still working with the computer off. Just the same physical port.
Comment: NIC should veto complete power off and the BMC should remain available. Saying that - it may depend on the NIC (onboard, mezzanine card, PCI-e slot with PCI-e having least chance of working correctly), NIC FW and configuration, NIC brand (go for Dell) and possibly - PCI-e slot configuration. Try to keep WoL on on the shared NIC, enable wake by PCI-e in BIOS. Some multiport NICs may share correctly only on the first NIC port.
Comment: @TomTom - the iDRAC isn't like the BMC that the SuperMicro has - that I am familiar with. It is a completely separate PCIe card (along with several other electrical connections for various other protocols). You can use the lifecycle controller to force the iDRAC to use another port other then its own but it doesn't natively do that (and I've never actually seen someone use that feature ).
Comment: @Tomek - maybe? That sounds reasonable. I'll have to test it if I get some free time. Correct me if I'm wrong as this isn't something I'm as familiar with but WOL is managed by the BIOS plus it has to be supported as you mentioned by the NIC. I'm looking at an iDRAC right now (though it's a 15G R7525) and I'm not seeing anything in the BIOS that stands out to me as being related to setting this. I might be missing it though? I'm open to suggestion

R420 iDRAC7 not working when server off


ActiveMQ Artemis redelivery delay

Question: Setup:

We have a Spring Boot application that is reading messages from an ActiveMQ Artemis JMS queue.
The messages are processing in a JPA transaction.
When there is an exception triggering a rollback in JPA, it also triggers a JMS rollback in Artemis that is setup with redelivery delay.
Our app is running in multiple instances in parallel and this cause optimistic locking issues when processing multiple messages that share common data.

Issue: When X messages are processed in parallel and there is optimistic locking issue, then only 1 message goes through and all the others are re-scheduled with the delay. When redelivery happens, then same as before the X-1 messages will arrive at the same time since the delay is the same and cause the same issue with only one going through.
Question: Does anyone know a way to add variance to the redelivery delay time of ActiveMQ Artemis?
Note: I know that there is an option for that in ActiveMQ 5.x called collisionAvoidanceFactor, but it is missing from ActiveMQ Artemis.
Answer: As you note, there is no equivalent for collisionAvoidanceFactor in ActiveMQ Artemis. I know of no way to modify the redelivery delay in a similar manner. There is the redelivery-delay-multiplier, but that is enforced consistently across redeliveries and would not provide the variance that you're looking for.
You may consider using message grouping so that "messages that share common data" are consumed serially by the same consumer and therefore avoid the locking issues in the first place.
After looking at what it would take to implement this feature I opened ARTEMIS-2364. I'll be sending a pull-request soon so it will likely be in the next version of Artemis (i.e. 2.10).

ActiveMQ Artemis redelivery delay


Embedded jetty implementing HttpSessionListener

Question: I am trying to implementing HttpSessionListener interface with embedded jetty with proxy servlet, I have registered SessionListener, but it is not getting invoked at all, here is the code,
<code>   public class JettyProxy {

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        CustomProxyServlet customProxyServlet = new CustomProxyServlet();

        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8888);
        server.addConnector(connector);

        ConnectHandler proxy = new ConnectHandler();

        server.setHandler(proxy);

        ServletContextHandler context = new ServletContextHandler(proxy, "/",
                ServletContextHandler.SESSIONS);
        ServletHolder proxyServlet = new ServletHolder(customProxyServlet);

        context.addServlet(proxyServlet, "/*");

        if (context.getSessionHandler() == null) {
            System.out.println("Session handler is null");
        } else {
            System.out.println("Session handler is not null");
        }

        if (context.getSessionHandler().getSessionManager() == null) {
            System.out.println("Managaer it null");
        } else {
            System.out.println("Manager is not null");
        }

        context.getSessionHandler().addEventListener(new CustomSessionHandler());

        server.start();
        server.join();
    }

}
</code>
SessionHandler is not null, session creating events are not getting triggered, please help me, what is the procedure get session events?
Answer: you should have a SessionManager. i usually use :
 org.eclipse.jetty.server.session.HashSessionManager.HashSessionManager()
and
org.eclipse.jetty.server.session.SessionHandler.SessionHandler(SessionManager manager)
then you should set the handler for the context
<code>context.setHandler(sessionHandler);

sessionHandler.addEventListener("Your Session Listener");
</code>


If statement not working with gets() in Ruby

Question: When I ask the user for input like this:
puts "Enter num: "
num = gets()
if num == 44
  puts "Yeah"
end
unless num == 44
  puts "No"
end

the output is "No" when I enter 44 when prompted. I can't figure out what's going on, so please help.
Comment: What is your question?
Comment: You should accept the answer that helped you by clicking the big check mark beside the answer.
Comment: Hint: what does the `s` in `gets` mean?
Answer: You can simply do:
gets.to_i

which will essentially chomp the newline character and convert to integer.
Example:
2.1.2-perf :020 > num = gets.to_i
44
 => 44
2.1.2-perf :021 > if num == 44
2.1.2-perf :022?>       puts "Yeah"
2.1.2-perf :023?>   end
Yeah

Answer: The ouput is "No" because when you use gets actually the input is taken as string, so a mismatch occurs. So for your logic to work you should convert it into integer
So this is the solution
puts "Enter num: "
num = gets().to_i
if num == 44
    puts "Yeah"
end
unless num == 44
    puts "No"
end

Answer: gets() by itself included the newline character. Use gets.chomp to remove that extra character. Then, call .to_i to convert to an integer. So: gets.chomp.to_i.
Comment: If you use `.to_i`, you don't need `.chomp`
Comment: Yep, that worked! Thanks so much, I knew that it'd be a simple fix.