Out of Memory Issue - Mongo is getting killed as soon as I start my java application

Question: I have a standalone application where I am trying to create 5 threads for each thread I am creating a mongoclient and persisting the details in MongoDB and then closing the mongoclient.
When I start my mongo service it is running but as soon as I start my java application after some time my mongo service is getting killed and I can see the below error in syslog.
Out of memory: Kill process 12715 (mongod) score 433 or sacrifice child\\
kernel: [2946780.340246] Killed process 12715 (mongod) total-vm:6646800kB, anon-rss:6411432kB, file-rss:0kB

I am using Linux server it has 10GB RAM. As far as I know we can do thousands of transactions in MongoDB. And before I start I can see free memory after some time I am getting the above error.
Not sure exactly what to do, Do we need to set anything in linux server to handle multiple mongo requests? or do I need to increase the limit of mongo service any where?
Can any one please suggest me on this?
Thanks in Advance.
o/p of free -m before and after execution of my java application are as follows:
Before:
             total       used       free     shared    buffers     cached
Mem:         14032       7646       6385         54          4        153
-/+ buffers/cache:       7489       6542
Swap:            0          0          0

After:
             total       used       free     shared    buffers     cached
Mem:         14032      13845        186         54        106       4131
-/+ buffers/cache:       9607       4425
Swap:            0          0          0

The below are the details from mongod.conf
# mongod.conf

storage:
  dbPath: /xxx/xxx
  journal:
    enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 127.0.0.1,xx.xx.xx.xx

processManagement:
   fork: true
security:
  authorization: enabled

Output of cat /proc/<PId>/limits:
cat /proc/17694/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             112088               112088               processes
Max open files            1024                 4096                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       112088               112088               signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

ps xua | grep java results :::
  root     17142  0.4  3.1 5645648 453180 pts/0  Sl   Sep04   2:27 java -d64 -Xms1024m -Xmx2048m -server -cp bin:lib/* javaclassname -resourceDir /xxx/bin -dataDir /xxx/data
   root     17491 22.1 11.2 5653432 1610716 pts/0 Sl   Sep04 115:14 java -d64 -Xms1024m -Xmx2048m -server -cp bin:lib_new/* javaclassname -resourceDir /xxx/bin -dataDir /xxx/data -outConfigPath /xxx/xxx.xml -outCatBrandPath /xxx/xxx.xml -outCatAndSunCatPath /xxx/xxx.xml
   xxx      19223  3.5 22.1 20396560 3184668 ?    Sl   Aug28 398:11 /usr/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/elasticsearch-5.4.0 -cp /elasticsearch-5.4.0/lib/* org.elasticsearch.bootstrap.Elasticsearch
   root     21617  2.2 21.7 18549248 3124416 ?    Sl   Aug28 247:34 /usr/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-8.0.30/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/apache-tomcat-8.0.30/endorsed -classpath /opt/apache-tomcat-8.0.30/bin/bootstrap.jar:/opt/apache-tomcat-8.0.30/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-8.0.30 -Dcatalina.home=/opt/apache-tomcat-8.0.30 -Djava.io.tmpdir=/opt/apache-tomcat-8.0.30/temp org.apache.catalina.startup.Bootstrap start

swapon -s output :
Filename                                Type            Size    Used    Priority

Comment: It seems that your server is running out of memory and the kernel out-of-memory killer (OOM-killer) decides to terminate the mongodb process to safeguard the operating system. Please include some indicators like memory usage just before and just after launching your java application, output of `free -m` and/or `top`, and java memory sizing (-Xms, Xmx, etc.)
Comment: Also provide all the configuration from  mongod.conf
Comment: top command results are as follows :

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
17694 root      20   0  372624 182312   6484 S  24.1  1.3   0:02.12 mongod
17491 root      20   0 5320268 384832  20788 S   6.0  2.7   0:16.05 java
    1 root      20   0   33472   1360     60 S   0.0  0.0   0:39.40 init
    2 root      20   0       0      0      0 S   0.0  0.0   0:01.01 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:14.28 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
Comment: Hi John, I have added the mongod.conf to the question.
Comment: What is the output of `cat /proc/PID/limits` where PID is the process id of mongo? Could be a ulimit issue.
Comment: Hi Jason, output is updated in question. sorry for the format I am newbie to stackoverflow.
Comment: My guess: You java process is allocating too much memory (try a `ps xua | grep java` and look for the -Xmx parameter), and the kernel kills mongod.
Comment: Hi ThoriumBR, added the suggested command results in qstn.
Comment: Could any one please suggest me how to go for further in this issue?
Comment: Do you have any swap space? Please paste the output of the `swapon -s` command. It's likely that the sum of all services is allocating close to the 10GB system RAM which triggers the kernel OOM-killer. With swap configured the kernel can move less-used memory pages to disk and free up physical RAM to run your applications.
Comment: Hi Andre,  updated the output in question. Its printing nothing.Is it mandatory to create it?
Comment: Yes, you definitely should create a swap partition on that server.
Answer: It seems that the OS is running out of physical memory, and since your server doesn't seem to have any swap configured, the kernel's out-of-memory killer (OOM-killer) kicks in and terminates processes to free up some RAM (in this case it's mongodb, but could be any other).
You should create a swap partition on that server.
Comment: Hi Andre, Thanks for your suggestion. I created a swap partition but system goes down. Is it because of this swap partition?
Comment: Hi Andre,

My server is of 14GB. I created a swap partition of 4GB, Does this makes sense? pls advice...
Comment: Check if your swap partition is actually in use with `swapon -s`, it should show something. Check the following guide for instructions on how to configure swap on Linux: http://www.thegeekstuff.com/2010/08/how-to-add-swap-space/?utm_source=feedburner . If your swap is properly configured and in use, it's possible that it is too small, check the usage with `free -m` and if it's becoming full try increasing its size to the same amount as the server's RAM.
Comment: Thank you so much for your help Andre.. I will try to increase the size as and when required currently OOM-killer is not killing my process.


Precisely which rules disallow spinning sets and open-hand passes?

Question: I've looked at FIVB indoor volleyball rules and FIVB beach volleyball rules.
I can't figure out which rules sections numbers lead to someone calling fault when:

a hand-set has too much rotation
a serve is received with an open-hand pass

I want to emphasize my only interest is to learn which FIVB or AVP rules section numbers apply, directly or indirectly, to the above.
Answer: There is no particular rule for this; both those faults would just be considered as either a double touch

9.1.1 A player may not hit the ball two times consecutively

or a catch

9.2.2 The ball must not be caught or thrown

Those quotes from either the FIVB Beach Volleyball Rules 2015-2016 or the FIVB Indoor Rules 2015-2016; the wording and numbering is identical between the two rule books. The difference between the two rule sets comes in how strictly the rules are interpreted with the beach interpretations generally being much stricter.
Comment: The ref I asked yesterday said she considers these covered by 9.3.3, 9.3.4.  Your answer sounds valid too. I'm convinced this is an interpretation issue and is subjective.
Answer: I just saw this and thought I'd give you an answer...
Neither of the things you listed are faults for FIVB USAV indoor (AVP Beach may be different).  If a ref calls either of those for that reason "too much rotation" or "you set the first ball," It's a bad ref who doesn't know the rules.
The fault is in the contact.  If the ref can't describe what it was about the contact  that s/he didn't like (or describes something that happened after the contact e.g. spin), they made a poor call.
Comment: Welcome to Sports SE! Can you provide supporting resources to your answer?


How do I customize the Add button in jqGrid?

Question: I have a jqGrid and when I do a seach I get a small dialog. There is a Add button at the top which has a "+" sign to add rows for advanced searching. Is it possible to change this button text to a plain old school "Add" without hacking into js library. Is there any way by writing an extendion? Is so how?
<input type="button" value="+" title="Add rule" class="add-rule ui-add">

Answer: The method navGrid has some parameters which can be used to change the look of navigator buttons. I used additionally toppager: true option of jqGrid and cloneToTop: true option of navGrid for the case that one uses top pager. If one just create the navigator buttons with respect of the call
$grid.jqGrid('navGrid', '#pager', {
    cloneToTop: true,
    view: true,
    add: true,
    edit: true,
    addtext: 'Add',
    edittext: 'Edit',
    deltext: 'Delete',
    searchtext: 'Search',
    refreshtext: 'Reload',
    viewtext: 'View'
});

one will see the results like

I used rowNum: 2 for the tests only to reduce the height of the pictures.
If I correct understand you, you want to have pure text buttons. To remove the icons from the buttons one can execute the following additional lines
var pagerId = 'pager', gridId = $.jgrid.jqID($grid[0].id);
$('#' + pagerId).find('.navtable span.ui-icon').remove();
$('#' + gridId + '_toppager').find('.navtable span.ui-icon').remove();

The gridId will be set to 'list'. One need to use $.jgrid.jqID if the grid id could contain meta-characters. The results will be

Next step will be to fix right and left the padding or margin of the buttons to show the texts better:
.ui-jqgrid .ui-jqgrid-pager .navtable .ui-pg-div,
.ui-jqgrid .ui-jqgrid-toppager .navtable .ui-pg-div {
    padding-right: 3px;
    padding-left: 3px;
}

The results will be improved to the following

Now we need fix the position of the pager. We can do this with respect of
$('#' + pagerId + '_left').width($('#' + pagerId + ' .navtable').width());
$('#' + gridId + '_toppager_left').width($('#' + gridId + '_toppager .navtable').width());
$('#' + pagerId + '_center')[0].style.width='';
$('#' + gridId + '_toppager_center')[0].style.width='';

and have the following results

or remove additionally unused right part of the pager with
$('#' + pagerId + '_right').remove();
$('#' + gridId + '_toppager_right').remove();

which places the pager in the middle of the rest place on the pager:

In my opinion it's mostly the matter of taste which look is better. You can see the resulting demo here.
Alternatively you can do use icons over the text, but place the text under the icons. You can see
details in the demo which produces the following results:

UPDATED: To customize the "Add rule" or "Add group" buttons you can use afterRedraw option:
afterRedraw: function () {
    $('input.add-rule',this).val('Add new rule');
    $('input.add-group',this).val('Add new rule group');
}

The demo which use the option produce the searching dialog like the following:

(the button "Add group" exist if multipleGroup: true are used and the "Add rule" button exist if multipleSearch: true are used).
UPDATED 2: One can improve the visibility of the searching dialog if one would use jQuery UI Buttons:
afterRedraw: function () {
    $('input.add-rule',this).val('Add new rule').button();
    $('input.add-group',this).val('Add new group or rules').button();
    $('input.delete-rule',this).val('Delete rule').button();
    $('input.delete-group',this).val('Delete group').button();
}

The result dialog can be like the following
Comment: Thanks for your answer. My question was when a user click on the search button as shown in the figure, it brings a pop up box. The Add button for the search criteria is a + sign. I want to change this. How do I do this? Your answer doesn't explain this....
Comment: @chugh97: You should formulate your question more clear. The title of the question say nothing about Searching dialog. The "+" Button is the "Add" new row button. Your original question isn't complex. You can use [afterRedraw](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:singe_searching#options) option in the form `afterRedraw: function () {$('input.add-rule',this).val('Add new rule');`
Comment: Thanks for your answer!! I am sorry my question was not very well understood in first instance. I'll try next time....


Trying to change ASP .NET Label from code behind: The name "idLabel" does not exist in the current context?

Question: I have a ASP.NET project using C#. When I try to change a label (which is created in .aspx file) from the my code behind file (aspx.cs), the label does not get recognized in my code behind file.
I tried idLabel.Text = "example"; but an error pops up saying:

The name 'idLabel' does not exist in the current context.

Is there any way to fix this?
Comment: Post the aspx markup for the label.
Comment: check this one code file vs code behind https://www.c-sharpcorner.com/forums/difference-between-codebehind-and-code-file
Answer: We need more information.
if you just drop a asp.net label on the page, then your code should work.
however, if the label is inside of a gridview, repeater, listview etc? Then you have MANY rows, and thus a different approach is required on how to reference a given label, since if in a gridview or listview, then you have "many" rows, and thus which instance of the label (what row) are you trying to refer to?
This is why additional information is required here.
If you have this markup on the page, say like this:
<asp:Label ID="idLabel" runat="server" Text="Label"></asp:Label>

Then, yes your code should work.
however, if nested in a repeating type of control, then you will have "many" rows of data, and thus a different approach is required.


Conditions for $\frac{a_{n+1}}{a_n}$ to converge?

Question: Given a sequence $a_n \to 0$. I'm looking for a necessary and sufficient condition for $\frac{a_{n+1}}{a_n}$ to converge.
Comment: $\frac{a_{n+1}}{a_n}$ converges is a necessary and sufficient condition.
Answer: In general, the form $\lim _{n\rightarrow \infty}\frac{a_{n+1}}{a_n}$ will converge if the limit of this is $<1$. If the above holds, then write $a_{n+1}<a_n$ for every index $n$. This is the definition of a monotonically decreasing function.
A necessary condition would be that $a_n \neq 0.$ Monotonically decreasing would be a sufficient condition. Also, non-negativity of the sequence follows from $a_n\neq 0$ since $a_n=0\Rightarrow  a_{n+1}<0.$


How to seperate list of hours:minutes:seconds in python?

Question: Hello i would like to seperate a list that looks like this:
04:05:43.0
04:05:44.0
04:05:45.0
04:05:46.0
04:05:47.0
04:05:48.0
04:05:49.0
04:05:50.0
04:05:51.0
04:05:52.0
04:05:53.0
04:05:54.0
04:05:55.0
04:05:56.0
04:05:57.0
04:05:58.0
04:05:59.0
04:06:00.0
04:06:01.0
04:06:02.0
04:06:03.0
04:06:08.0

to 3 seperated lists with hours/minutes and seconds
Expected result:
hrs = [4, 4, 4, 4, 4, 4, ......]
mins = [5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,.... ]
secs = [1, 2, 3, 4, 5, 6,.....]

Comment: Simply use `your_string.split(":")` and index it with 0, 1, and 2
Answer: This code does exactly what you want!
list_ = [
    '04:05:43.0',
    '04:05:44.0',
    '04:05:45.0'
]

# create 3 different lists, seperating hrs mins and secs
hrs = []
mins = []
secs = []

# loop through the list and split the strings into 3 different lists
for i in list_:
    hrs.append(int(i.split(':')[0]))
    mins.append(int(i.split(':')[1]))
    secs.append(int(i.split(':')[2].split('.')[0]))

Answer: You can use zip and str.split in a generator expression:
# the output will be tuples
hrs, mins, secs = zip(*((int(float(x)) for x in s.split(':')) for s in data))

Or for lists instead of tuples:
hrs, mins, secs = map(list, zip(*((int(float(x)) for x in s.split(':'))
                                  for s in data)))

Output:
# hrs
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

# mins
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6]

# secs
[43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0, 1, 2, 3, 8]

Answer: It's easy when data is in fixed format
lst = [
'04:05:43.0',
'04:05:44.0',
'04:05:45.0',
'04:05:46.0',
'04:05:47.0',
'04:05:48.0',
'04:05:49.0',
'04:05:50.0',
'04:05:51.0',
'04:05:52.0',
'04:05:53.0',
'04:05:54.0',
'04:05:55.0',
'04:05:56.0',
'04:05:57.0',
'04:05:58.0',
'04:05:59.0',
'04:06:00.0',
'04:06:01.0',
'04:06:02.0',
'04:06:03.0',
'04:06:08.0',
]
hrs  = [int(i[0:2]) for i in lst]
mins = [int(i[3:5]) for i in lst]
secs = [int(i[6:8]) for i in lst]
print( "hrs =", hrs)
print( "min =", mins)
print( "secs =", secs)

Output:
hrs= [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
min= [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6]
secs= [43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0, 1, 2, 3, 8]


SQL - Adding values to new column in a table from another table

Question: I am learning to deal with databases and I came across a task to add new column to existing table and add values to that column using values from another table.
My tables are
table_1                          table_2
branch_code| book_id             branch_branch_code| branch_name
        100| 1                                  100| Carnegie
        101| 2                                  102| Reservoir
        102| 3                                  103| JOLIMONT

I need to add new column, book_returned_branch to table 1. Add branch name to the new column using branch_code. Branch code is similar to both the tables.
Below is my code and it throws an error: single-row subquery returns more than one row. Please advise!
UPDATE TABLE_1
    SET BOOK_RETURNED_BRANCH =
    (SELECT BRANCH_NAME FROM TABLE_2 A
    JOIN TABLE_2 B ON A.BRANCH_CODE = B.BRANCH_BRANCH_CODE
    WHERE BRANCH_CODE = (SELECT BRANCH_CODE FROM TABLE_2));

Comment: dbms name please
Comment: Oracle SQL Developer
Answer: You can do this using a correlated subquery:
UPDATE TABLE_1
    SET BOOK_RETURNED_BRANCH = (SELECT BRANCH_NAME
                                FROM TABLE_2 t2
                                WHERE table_1.BRANCH_CODE = t2.branch_branch_code
                               );

No JOIN is needed in the subquery.
Having said that, I strongly discourage you from doing this.  You should simply use JOIN when you query the table to get the branch name.
Comment: Can you suggest query with JOIN?
Comment: @PrajwalVenugopal . . . No, I can't.  Oracle doesn't support `JOIN` in `UPDATE` statements (well, except for subqueries).
Answer: For add a field to a existing table, you have to use alter table. Try the following code:
alter table table_1 add BOOK_RETURNED_BRANCH varchar(150);

update
    table_1
set
    BOOK_RETURNED_BRANCH =
    (
        select
            branch_name
        from
            table_2
        where
            table_1.branch_code = table_2.branch_branch_code
    );


Is it possible to make multi-layer autoencoder learn to completely repeat input?

Question: I'm currently playing with auto-encoders, so the question is more about research than practical implementation. I know that if I reduce capacity of auto-encoder by making hidden layer smaller, I'll get some kind of factorization of the input (adding noise is more effective, but let's forget about that for a while).
However I also read that, given enough neurons on hidden layer auto-encoder will just learn to repeat input, so I wanted to check that worst case (to see how it actually does it) by building a small simple MLP:

no noise at the input
number of layers: 3 (inputN=4, contextN=4, outputN=4)
activation: ReLU
weight init: XAVIER
input/output number of features: 4
hidden layer size: 4
regularization: L2 (weight decay), tried no-regularization
SGD with back-propagation, no momentum
learning rate: 0.01 (tried 0.1, 0.001, 0.0001)
library: deeplearning4j

If I pass a single (or several) sample over and over - I can see that only some of output values repeat input, others are zeros. So, the output is becoming sparse, meaning that for let's say [0.1, 0.3, 0.7, 0.2] - I get something like [0.1, 0.0, 0.7, 0.0]. Running more iterations doesn't improve it after some point.
I believe the sparsity of result is due to gradient vanishing, probably because some of the initial weights were set close or equal to zero by XAVIER. I tried other random initializations (including normal distribution), but it didn't help; Of course, I could try to set weights as they should be (and I did - it worked), but it wouldn't show anything interesting.
So, is there any example of multi-layer auto-encoder that just repeats its input completely? Was my guess about weight initialization right or is there any another underlying reason? Does varying samples and order play any significant role in this particular case?
P.S. If this problem is not typical for this architecture - I could provide some reproducible example (in case there is a problem with my code or underlying library).
Comment: An autoencoder learns a representation of the dataset, not single input neurons. If an autoencoder repeats it's input perfectly we can assume it overfit somewhere. You can make the network overfit by giving it a very small dataset. Or you can make the latent vector bigger than the input vector. However, if the latent vector is smaller the network cannot reproduce it's inputs perfectly for every input possible, but it can for a subset of the input space, if that subset is small enough.
Answer: Why not start with one layer, and get the output equals the input for one layer? Then, by induction, you can just stack those one on top of the other, and just pipeline through your trained identity layers.
Note that if you're using ReLU, then if you have any negative values in your input/output, then ReLU will not be able to do the identity operation you seek. So you'd probably want to make sure your input/output is strictly positive.
Gradient vanishing refers to a specific effect of backpropagated gradients being attenuated by passage through an activation function, like tanh or sigmoid. ReLU doesnt suffer from vanishing gradients too much, since the gradient is 1, for positive domain input.
So:

check your inputs/outputs are strictly positive
start with a single layer

Edit: based on your new information that it works with a single layer, but not with multiple layers, I reckon that ReLU is blocking your gradient backprop, probably because it has zero gradient for much of its domain.  Therefore, you might try using an activation function that has non-zero gradient almost everywhere, eg ELU or leaky ReLU. On the whole, I'd try leaky ReLU first, because:

it is technically an activation function (cf not using an activation function at all)
its piecewise linear (cf ELU)
it has gradient almost everywhere, so gradients should backprop ok
Comment: Thanks for the reply. 1) my values are strictly positive to avoid ReLU "saturation" as you could see from example 2) basically my problem is that I can do it with a single layer easily (because of no back-propagation there)  - I want to see same behaviour for back-propagated layers, not for stacked ones... or at least get some explanation why it doesn't work with backprop.
Comment: Hmmm. Interesting that works for one layer but not for multiple. Maybe whats happening is that ReLU is blocking your gradient backprop.  I'd be tempted to try 1. using an activaiton function that has non-zero gradient almost everywhere, eg ELU, or leaky ReLU; and/or 2. adding dropout (to cause the neurons to be turned on/off randomly, albeit the noise will mean the network takes longer to converge).
Comment: (I guess leaky relu might do what you want: piecewise linear, so gradients wont vanish horribly, has gradient almost everywhere, is technically an activation function (cf just not having an activation function at all...), and is quite commonly used)
Comment: after applying `LeakyRELU` I've got rid of zeros but they were replaced with small negative values  `[0.6623673,0.12019485,0.13492113,0.57845426] ==> [-5.88048E-4,0.12378041,-0.00475002,-0.0019856603]`
Comment: actually, maybe there is some problem with my new configuration/code. I tried to use only one layer and got same results. I think I should re-check it again...


Book for non parametric statistics

Question: What would be a good book for non-parametric statistics. Not just the introduction but advanced level. Also I am looking at something I can use for learn and not for reference.
In particular I am looking for a book that can contain basics behind non-parametric methods, non-parametric inference, methods to evaluate non parametrics, e.g., KS test, $t$ test, etc. , bootstrapping ....
Comment: Nonparametric statistics is a large field, but I'd guess *All of Nonparametric Statistics* by Larry Wassermann should be a reasonable starting point. I don't know if I would call it "advanced level" but he sketches the proofs of many of the results in the book. Topics include the bootstrap, smoothing techniques, density estimation, regression, and lots of other things. No nonparametric Bayes, however.
Comment: +1 on the Wassermann book. *Nonparametric Statistics with Applications to Science and Engineering* by  Kvam and Vidakovic is also quite hoppy. Not really mathematical but I enjoyed its "problem-oriented" approach.
Comment: I have checked that one out but its more like a reference book than learning material. No?
Comment: I disagree, it should be fine for learning from. If I'm remembering correctly, he wrote it for people who hadn't seen nonparametric methods before, such computer science students he teaches.
Comment: Are you looking for 'distribution free' stuff, or nonparametrics in the sense of "infinite parametric", whether it applies to distributions, relationships between variables or whatever else? For example, I can assume a linear relationship for $(Y|X=x)$ without a distributional assumption, or I can *assume* that $Y$ is normal and not assume the relationship with $X$ is anything but 'smooth'... both can be referred to as 'nonparametric', even though each is parametric in one aspect (if potentially infinite-parametric in another).
Comment: I am looking for distribution free stuff .
Comment: I think that expanding this answer to give a little more detail would make it more likely to attract good responses. For example, terms like "advanced" are nebulous and can be misinterpreted. :)
Comment: t tests don't qualify as non-parametric -- except in a special sense that they are often surprisingly robust.
Answer: I think of the Encyclopaedia Britannica of nonparametric statistics as being:

Hollander, M, & Wolfe, D (1999). Nonparametric Statistical Methods, 2nd Edition. Wiley.

I'm not sure if I would characterize this as introductory or advanced.  Many of the sections are a bit terse, in my opinion, and are written with a good deal of mathematical notation.  This will be intimidating / off-putting for people who have some math anxiety. On the other hand, it's not really deriving theorems, it's just using mathematical notation to express the ideas.  There are some problems included at the end of each section; you could definitely use the book to learn nonparametric statistics.
For a treatment that is much more introductory:

Higgins, J. (2003). Introduction to Modern Nonparametric Statistics. Duxbury.

will be much less intimidating, I think.  I have skimmed some portions of it, and it seems to be a gentle introduction for people who don't have a strong statistical background.  It is very clear, but does not have anything like the depth or coverage of Hollander & Wolfe.
Comment: I think @gung-ReinstateMonica was just writing figuratively and alluding to a source that is a reference, large and definitive.
Comment: In november 2013 the [third edition](http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470387378.html) of Hollander & Wolfe appeared and a corresponding GNU R package [NSM3](http://cran.r-project.org/web/packages/NSM3/index.html) is available.
Comment: I don't have a recommendation for a book that focuses primarily on the mathematical theory behind nonparametric statistics.
Comment: I find it confusing how you refer to the Encyclopaedia Britannica, when in fact the book does not come from the Encyclopaedia Britannica. In fact, the Encyclopaedia Britannica [has an entry on nonparametric statistics](https://academic.eb.com/levels/collegiate/article/statistics/108592#60722.toc).
Answer: You may want to check out All of Nonparametric Statistics, by Larry Wasserman. The title says it all ;)
The reviews for the book are outstanding. I cannot tell for myself, since I have not read it yet. But it seems to have all the theoretical background you are looking for, plus it has also a focus on applications, i.e. it well help you to put those techniques into use quickly.
Comment: Provide the name of book in the answer, not only the link, please! :)
Comment: Despite the title, this isn't a book that focuses on Wilcoxon-Mann-Whitney, Kruskal-Wallis and that bundle of nonparametric significance tests, which many readers of this thread may be looking for. As always, you can check out a book's aims and contents from material or Amazon or the publisher's website.
Answer: Nonparametric statistics is to a good extent a disjoint collection of tests and estimators.  Especially for tests, I suggest instead using semiparametric regression models as a unifying framework.  Commonly used tests such as the Wilcoxon and Kruskal-Wallis tests may be obtained as special cases of the proportional odds semiparametric ordinal logistic regression model.  A better rank-based approach than the Wilcoxon signed-rank statistic may be obtained likewise.  The regression framework easily extends to allow covariate adjustment and longitudinal data.
I've taken this approach in my introductory e-book BBR.
Answer: The best nonparametric statistics book is

Hollander, M.; Wolfe, D.A.; Chicken, E. Nonparametric statistical methods. Hoboken: Wiley, 2014. 848 p.

Lehmann's book is a classic, but it doesn't cover all the methods in Hollander et al. It has a balanced mix of teaching, practical examples, useful exact tables, and nifty proof sketches in the appendix.

Lehmann, E.L. Nonparametrics: statistical methods based on ranks. New York: Springer, 2006. 480 p.

The main focus of Hettmansperger's book is rank-based methods, leaning towards a more rigorous style. The books by Randles & Wolfe and Gibbons & Chakraborti have a somewhat similar approach:

Gibbons, J.D.; Chakraborti, S. Nonparametric statistical inference. Boca Raton: CRC Press, 2021. 694 p.
Hettmansperger, T.P. Statistical inference based on ranks. New York: Wiley, 1984. 380 p.
Randles, R.H.; Wolfe, D.A. Introduction to the theory of nonparametric statistics. New York: Wiley, 1979. 450 p.

Conover and Siegel & Castellan wrote great books with a more practical, applied treatment. They also describe a few techniques that aren't in the books I mentioned above.

Conover, W.J. Practical nonparametric statistics. New York: Wiley, 1999. 608 p.
Siegel, S.; Castellan Jr., N.J. Nonparametric statistics for the behavioral sciences. New York: McGraw-Hill, 1988. 399 p.

Book for non parametric statistics


Custom ListView with database

Question: Here's my android code :
public class Fragment1 extends SherlockFragment {
    ListView lista;
    ArrayList<String> items = new ArrayList<String>();
    private List<Listaadaptera> thelista = new ArrayList<Listaadaptera>();
    View rootView;
    ListView list;
    TextView title, price;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        MenuInflater blowup = this.getSherlockActivity()
                .getSupportMenuInflater();
        blowup.inflate(R.menu.menuxml, menu);
        return;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.pricelist, container, false);
        ActionBar ab = this.getSherlockActivity().getSupportActionBar();
        ab.setTitle("");
        title = (TextView) rootView.findViewById(R.id.title);
        price = (TextView) rootView.findViewById(R.id.details);

        list = (ListView) rootView.findViewById(R.id.list);
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
                this.getSherlockActivity(), R.array.phones_array,
                android.R.layout.simple_spinner_dropdown_item);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        ab.setListNavigationCallbacks(mSpinnerAdapter, null);
        StrictMode.enableDefaults();
        getData();
        return rootView;
}
getdata();
*//Connecting to the database...

        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json = jArray.getJSONObject(i);
                thelista.add(new Listaadaptera(json.getString("PhoneName"),
                        json.getString("PhonePrice"), R.drawable.lebanon,
                        R.drawable.rating));
            }

        } catch (Exception e) {
            Log.e("lag_tag", "ERROR PARSING DATA" + e.toString());
            Toast.makeText(getSherlockActivity(),
                    "Couldn't Connect To The Internet", Toast.LENGTH_LONG)
                    .show();
        }

    }

}

pricelist xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:background="@drawable/image_bg"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />
    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Smartphone Name"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/details"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Smartphone Price"
        android:textColor="#343434"
        android:textSize="10dip" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/rating" />

</RelativeLayout>

My goal in this code, is to get all the details registered in the database and detail them into this costume layout.
In the table i have the 2 fields, PhoneName and PhonePrice
How to make my application able to get all the details found in these fields and detail them in listview?
Your help will be appreciated a lot! Thanks :D
Answer: Try using this codes
         Cursor select = db.rawQuery("Select *from DBName", null);
         select.moveToFirst();
         int count = c.getCount();
         String[] pname= new String[count];
         String[] pprice= new String[count];
         c.moveToFirst();
         for(int i = 0;i < count;i++){
                pname[i] = c.getString(c.getColumnIndex("PhoneName"));
                pprice[i] = c.getString(c.getColumnIndex("PhonePrice"));
                c.moveToNext();
            }
         ListView list = (ListView) findViewById(R.id.menulist);
         list.setAdapter(new listadapter(this,pname,pprice));

"listadapter" should have 2 values for your PhoneName and Phone Address. And use holder so your list view will not encounter a problem like repeating value.
this codes work's for me.
Answer: Use an ArrayAdapter to map the values you get from the database to the row xml you will use to display the data in the ListView
Comment: I had my application working in the past with the standard listview, but i couldn't get my application working on a costume listview...
Comment: That was my old code :
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json = jArray.getJSONObject(i);

   String name = json.getString("PhoneName");
    items.add(name);
    ArrayList items = new ArrayList();
    thelista.setAdapter(mArrayAdapter);
Comment: I mean, the class were you are extending ListView
Comment: Also, may I know why are you extending ListView?
Comment: i posted it into the question. Any suggestion?
Comment: I do not understand. What do you want to achieve by extending ListView? Where is your class that extends ListView?

Custom ListView with database


Como duplicar esta função jQuery

Question: Como podem estar vendo nesse fiddle de exemplo, quando o primeiro link é clicado uma div aparece e ao pressionar o botão de voltar do navegador, ela fecha.
Gostaria que o segundo link funcionasse da mesma maneira, mostrando a div dois, mas quando tento duplicar a função um dos dois param de funcionar e eu não faço ideia do porquê (sou leigo em JS/Jq).
Podem me ajudar?
Link do fiddle: https://jsfiddle.net/d6yp3b92/
Código:

$(document).ready(function() {
  $('body').on('click touch', '#paginaUm', function(e) {
    $('.pagina.um').fadeIn();
  });
});

// geri butonunu yakalama
window.onhashchange = function(e) {
  var oldURL = e.oldURL.split('#')[1];
  var newURL = e.newURL.split('#')[1];

  if (oldURL == 'paginaUm') {
    $('.pagina.um').fadeOut();
    e.preventDefault();
    return false;
  }
  //console.log('old:'+oldURL+' new:'+newURL);
}
.pagina{position:fixed; display:none; top:0; left:0; width:100%; height:100%; background:gray; color:white; padding:20px;

Pagina 1

Popup 1

Pressione voltar para fechar.

Pagina 2

Popup 2

Pressione voltar para fechar.

Comment: Se cada `` abre o `` que está lá dentro então você fazer tudo com um evento de click se alterar um pouco as classes que tem.
Answer: Não sei dizer exatamente se essa é a melhor prática, mas se vc fizr um evento de click para cada btn vc consegue duplicar o código de boa
Veja o exemplo, teste no seu ambiente clicando no btn voltar que vai funcionar direitinho, ou aqui mesmo, ou nesse link https://codepen.io/hugocsl/pen/oJXpWW

$(document).ready(function() {
  $("#paginaUm").click(function(event){
    $('.pagina.um').fadeIn();
  });
  $("#paginaDois").click(function(event){
    $('.pagina.dois').fadeIn();
  });
});

// geri butonunu yakalama
window.onhashchange = function(e) {
  var oldURL = e.oldURL.split('#')[1];
  var newURL = e.newURL.split('#')[1];

  if (oldURL == 'paginaUm') {
    $('.pagina.um').fadeOut();
    e.preventDefault();
    return false;
  }
   if (oldURL == 'paginaDois') {
    $('.pagina.dois').fadeOut();
    e.preventDefault();
    return false;
  }

  //console.log('old:'+oldURL+' new:'+newURL);
}
.pagina{position:fixed; display:none; top:0; left:0; width:100%; height:100%; background:gray; color:white; padding:20px;

Pagina 1

Popup 1

Pressione voltar para fechar.

Pagina 2

Popup 2

Pressione voltar para fechar.

Comment: Verdade mesmo! Cada vez mais entendemos da linguagem mais conseguimos reduzir os códigos, mas o que importa é que funciona! rsrs
Comment: Essa cê respondeu, já foi suficiente. Essa outra dúvida foi a parte... vou abrir outra. Mas obrigadão por tudo!!!!
Comment: Tá ótimo! Agradeço bastante!
Comment: @ThiagoSoubra valeu jovem, do jeito que fiz tenho certeza que funciona, mas é que tem uns caras aqui no site muito fera mesmo, que as vezes resolvem esses problemas com 2 linhas de código rss. Mas esse jeito ai fica de fácil entendimento pra quem é mais leigo tipo a gente :D
Comment: vc sabe como fazer para usar esse sistema também por meio de um botão? porque além do popup ser fechado pelo botão de voltar, também há um botão que permite fechá-lo. Percebi que a coisa buga se eu simplesmente acionar a função de ocultar aquela div ao botão. Vi que o #link não sai do url, dai quando clico para abrir novamente (ou outro popup com o mesmo método), ele não abre, meio que precisa que o url esteja "limpo"... então eu acho que é preciso que seja possível realizar o mesmo processo que 'voltar' ao clicar nesse botão de fechar. Se não souber, posso abrir uma nova pergunta!
Comment: @ThiagoSoubra faz o seguinte, primeiro vc tem que coloca a classe `.fechar`, no seu caso é na tag `` que tem o texto "Pressione para fechar". Depois disso lá dentro do script vc inclui isso `$(document).ready(function () {
            $(".fechar").click(function (event) {
                $('[class^="pagina"]').fadeOut();
                document.location.href = String( document.location.href ).replace( /#paginaUm/, "" ) ;
                document.location.href = String( document.location.href ).replace( /#paginaDois/, "" );
            });
        }); ` testa ai que deve funcionar
Comment: amigo, dá certo, mas recarrega a página quando clicado :| seria possível fazer que não recarregasse?
Comment: @ThiagoSoubra aqui não está fazendo o refresh, atualizei o link, veja https://codepen.io/hugocsl/pen/oJXpWW qq coisa me fala
Comment: estranho, aqui atualiza. olha, gravei a tela nesse curto vídeo: https://drive.google.com/file/d/18OEMVRRC8sDko-0nj4HW-IRQmERNxfv1/view?usp=sharing
Comment: coloquei uma imagem na pagina pra ela ficar mais "pesada" e dá pra ver melhor o reload
Comment: @ThiagoSoubra é Jovem acho que não consegui dessa vez. Fiz uns testes aqui, atualizei novamente o link inclusive. E esse código é instavel, tem horas que até apaga a navbar no topo não sei pq. Vc pode desmarcar a resposta como aceita que não tem problema. Mas de qq forma te indico abrir outra pergunta, para alguém com mais conhecimento te ajudar nessa parte da URL.

Como duplicar esta função jQuery


Very basic intro to real analysis question

Question: In page 2  of baby Rudin,
Rudin wants to prove that $ \forall p  \in   \mathbb{Q} $ such that $ p>0 $ and $p^2 < 2$ there exists a $q \in \mathbb{Q}$ such that $q>p$ and $q^2<2$
In his proof, he starts with let $q=p-(p^2-2)/(p+2)$
and then says $q^2-2 = 2(p^2-2)/(p+2)^2$
and he goes on to finish the proof.
His proof makes sense, my question is how am I supposed to think in order to solve such problems?
I mean how do you know what to set q equal to? How did he think about this problem to get the expressions?
For example, right now I am trying to find $X$ such that $ q=p-X $ different than the one Rudin set. But I can't think of anything.
Thank you!!
Edit: Yes, I apologize I did make a mistake, it's been fixed now
Comment: Please, try to make the title of your question more informative. E.g., *Why does $a\le b$ imply $a+c\le b+c$?* is much more useful for other users than *A question about inequality.* From [How can I ask a good question?](http://meta.math.stackexchange.com/a/589/): *Make your title as descriptive as possible. In many cases one can actually phrase the title as the question, at least in such a way so as to be comprehensible to an expert reader.*
Comment: You have to understand that at that point in the book Rudin is merely interested in establishing the truth of the statement "$\{x \in \mathbb Q \mid x^2 < 2\}$ has no least upper bound in the rationals." The definition of $q$ in this case happens to be derived from Newton's method for computing nth roots, but the why and how of this isn't relevant to what Rudin is trying to teach at that point, so he simply chooses the shortest/cleanest argument that yields what he wants.
Comment: If you take $p=2$, then necessarily $q^{2}>2$?
Comment: and $p^2$ is alwasy greater than $0$ if $p>0$. Have you written the problem properly? I don't have Rudin atm to check it out
Comment: Presumably, you misquoted what you said Rudin wants to prove, because, as Lucien's example points out, what you wrote there is false.
Comment: You're right thanks for pointing this out
Comment: Yes I understand that it's not relevant, I was just curious..
Answer: Suppose we just wanted to find any real $q$ which satisfied that. Well, all we'd have to do would be to choose some $q\in (p,\sqrt{2})$ and so the obvious choice would be to take the center of this interval and so set $q=\dfrac{\sqrt{2}-p}{2}$. The problem with this is that because $p$ is rational and $\sqrt{2}$ is irrational, we find that $q$ must be irrational.
So, we have to be a little more clever. We still want $q$ to be in the above stated range, because those values satisfy $q>p$ and $p^2<2$, but how do we guarantee that $q$ is also rational? Well, we need to find a rational number in the range $(0,\sqrt{2}-p)$ which we can add to $p$. Let's call that value $r$ for now. Well $r$ is going to be dependent on $p$ so lets write $r$ as a function $r(p)$ which we want $r(p)<\sqrt{2}-p$ in the interval $0<p<\sqrt{2}$. We want $r(p)$ to take rational values for all rational $p$ so we should write it as a rational function $$r(p)=\dfrac{s_1(p)}{s_2(p)}$$ where $s_i$ is a polynomial in $p$. Now, there are many polynomials we could take to be $s_1$ and $s_2$ so lets put in some simplifying conditions. We want $r(p)<\sqrt{2}-p$ and we note that the RHS has a root at $p=\sqrt{2}$ so lets make $r(p)$ also have a root there. We want $r(\sqrt{2})=0$ which happens precisely when $s_1(\sqrt{2})=0$. Probably the most obvious choice for $s_1(p)$ then would be $s_1(p)=2-p^2$.
Now we need to try and find an $s_2(p)$ which makes $r(p)$ small enough. As we're trying to make $r(p)$ smaller than a linear function, let's make $r(p)$ grow like a linear function away from its singularities, so let's make the denominator linear and set $s_2(p)=ap+b$. So, we want $\frac{-b}{a}$ to be outside of the interval $(0,\sqrt{2})$ and we also want $s_2(p)$ to be large enough to satisfy the condition we impose on $r(p)$. At this point, many values of $a$ and $b$ will work and I can only assume Rudin chooses $a=1$ and $b=2$ to make the algebra a little neater later on.
Given this, we get $$r(p)=\dfrac{2-p^2}{p+2}$$ and after this we need to check that that this choice of $r(p)$ really satisfies the conditions, which it does.
I'll admit, this 'method' of finding a good $q$ involves a lot of trial and error, and some arbitrary choices, but this is because there are many possible $q$ that we could have used and the dependance of $q$ on $p$ just makes things that little bit more complicated. I can offer no advice on how to 'get a feel' for making these choices other than practice and trying to make 'reasonable' choices along the way.
Answer: There were two cases.One with q such that q>p and q^2<2 and second with q< p and q^2>2. I think this X was chosen (in Rudin) so as to use it again in the case two.You can take any other X say X =(p-q)/2 in 1st case and X=(q-p)/2 in second.In the book it was chosen so as to avoid taking two different X's for the two cases.
Answer: I'm  working through POMA too, and I had that same exact question when I read that section. Here's how I think you can derive it.
The easy way to create such a real $q$ is to have $q=p + \frac{\sqrt{2} -p}{k}$ for some $k>1$, but you want to avoid using $\sqrt{2}$ and irrational $k$ for $q$ to be rational. To get rid of the square root in the right term $$\frac{\sqrt{2} -p}{k}=\frac{2 -p^{2}}{k(\sqrt{2}+p)}$$
noting that $2+p >   \sqrt{2}+p$ you get$$0<\frac{2 -p^{2}}{k(2+p)}<\frac{2 -p^{2}}{k(\sqrt{2}+p)}$$
where the middle function may be used as a replacement term for rational $k>1$ and, we see this inequality works for $k=1$ as well, which gives$$p<p+\frac{2 -p^{2}}{k(2+p)}<p+\frac{2 -p^{2}}{k(\sqrt{2}+p)}\ \leq \sqrt{2}$$
satisfying all of the conditions for q for rational $k \geq 1$. The case $k=1$ gives Rudin's Formula.
Also, something interesting I noticed when trying to figure this out was that if you view q and p recursively you can use Newton's formula for $f(x)=x^{2}-2$, which gives $q= p-\frac{p^{2}-2}{2p}$: a solution fairly close to the other answer.
Comment: Thank you! This is the clearest answer for me even though Daniel Rust's answer is really good too!


counterexample of weak convergence for quantile function

Question: As we know, if a sequence of distribution functions $\{F_n\}$converge weakly to $F$, i.e. $\lim_{n \to \infty}F_n(x)=F(x)$ for all continuous points $x\in\mathbb{R}$ of $F$, then for the corresponding quantile functions, we have $\lim_{n\to\infty}Q_n(u)=Q(u)$ for all continuous points $u\in(0,1)$ of Q.
So my question is if $u$ is not a continuous point of Q, can we find an example that $Q_n(x)$ does not converge?
The discontinuous points (jumps) in $Q$ corresponds to flat points in $F$, but I don't know how to construct such a $\{F_n\}$.
Answer: Let $F_n(x)=0$ for $x\le 0$, $F_n(x)=1$ for $x\ge 3$, $F_n(x)=\frac12+\frac{(-1)^n}{n}$ for $1\le x\le 2$. Extend $F_n$ to  the intervals $(0,1)$ and $(1,2)$, for example affinely: as $F(x)=F(1)x$ for $0<x<1$ and $F_n(x)=(1-F_n(2))(x-2)+1$ for $2<x<3$.
As $n\to \infty$, the sequence $F_n$ converges uniformly. The median of the random variable described by $F_n$ is less than $1$ when $n$ is even, and greater than $2$ when $n$ is odd. Hence, the median does not have a limit as $n \to\infty$.


Angularjs filter and select all checkbox

Question: I have an array of items I am displaying with

the items have a check box that denotes "selected" or not.
how can I know which items are displayed by the filter when I need to do something like call a delete function that will delete all selected items ?
Items which have been selected (checked in the checkbox) and then hidden by filtering are still selected. I need a way to know which item is on screen at the moment.
Answer: You can use $filter to call a filter in your controller.
app.controller('MyCtrl', function($scope, $filter){
   var filter = $filter('filter');

   $scope.items = [/* your items here */]

   $scope.selectAllFilteredItems = function (){
      var filtered = filter($scope.items, $scope.search_text);

      angular.forEach(filtered, function(item) {
         item.selected = true;
      });
   };
});

You would then call selectAllFilteredItems() in an ng-click or anywhere else you needed to.
Comment: I was going to answer exactly the same =) Here's a simple fiddle http://jsfiddle.net/bmleite/syYxa/
Comment: Thx, but not sure I understand, where does search_text go ? and didn't you mean the forEach loop to be on filtered ?
Comment: the fiddle is great ! thx, but it lacks checking which items are actually selected,
Comment: Lol... thanks @MarkRajcok... I'm full of typos today apparently.
Answer: Not really super polished, but it works.
In your controller
var filter = $filter('filter');
$scope.item_ids = [];
$scope.selectAllFilteredItems = function (){
    var filtered = filter($scope.items, $scope.searchText);
    angular.forEach(filtered, function(item, key) {
        if($scope.item_ids.indexOf(item.id) == -1){
            $scope.item_ids.push(item.id);
            $scope.items[key].selected = true;
        }else{
            $scope.item_ids.splice($scope.item_ids.indexOf(item.id),1);
            $scope.items[key].selected = false;
        }
    });
};

Then in the table you have a checkbox to select all or unselect all.

        <input type="checkbox" ng-click="selectAllFilteredItems()">

        <input type="checkbox" ng-model="item.selected">

Comment: What about the case that after an selection one element will be deselected? How to handle the deselection of the Select All Button?


TDD for Embedded C : How to Design Tests for LED Driver?

Question: I m newbie to TDD and following "Test Driven Development for Embedded C edition 3".
Find a very interesting question brought by the author.
It's under Getting Started->Testing Your Way to Done->Put the knowledge to Work, P103:

Our hardware engineer informed us she could save $0.12/board
if the LEDs used inverted logic. Modify the LedDriver and its tests
to use inverted logic.
How could our tests or design be improved so that most of the
tests don't care about inverted logic?
Our company just completed the version of the board with inverted
LED logic. We find out that some of the previous version are still
in the field. How should the LedDriver and its tests be modified to
support both versions of the hardware? Conditional compilation is
not part of a correct answer. We want one binary.
The production board's silkscreen is wrong! LED 1 is labeled 16,
Led 2 is 15, and so on. How do you modify the tests and code to
work with the real hardware?

Is there any best practice I can follow to modify production code or test script in these cases?
B.R.
Answer: Generally, this sounds like a task for the programmer, not the tester.
If you write to a port with active high logic, then you'd use PORT |= MASK;, otherwise PORT &= ~MASK;. The main problem here is that we need different operators depending on polarity, so the naive solution would be:
if(active_high)
  PORT |= MASK;
else
  PORT &= ~MASK;

This produces overhead code though. The best solution is if we can generate MASK at compile time and always have the code do PORT = MASK; without destroying any other non-related port pins. Which in turn means that this needed to be considered as early on as during hardware pin routing!
Assuming the PORT = MASK; solution is possible, then you might do something like:
#define LED2 (1u << 2)  // active high
#define LED1 (0u << 1)  // active low
#define LED0 (1u << 0)  // active high

#define MASK (LED0 | LED1 | LED2)

Or if you will: #define POLARITY 1... #define LED2 (POLARITY << 2) ...

That being said, one of the most important skills in engineering is critical critical thinking/scepticism and thinking outside the box.
So if the hardware engineer says that they can save $0.12/board and your company makes millions of boards, give a thumb's up. If you sell hundred or so per year, and the fix will take you several days of work, then maybe tell them to get lost, because the programmer's salary for fixing it will cost many times more than any money "saved" during the lifetime of this product.
Similarly, "conditional compilation is not part of a correct answer. We want one binary" is a pre-mature requirement. If you screw up the silk screen on a prototype board, the best solution is to deliver one binary for that screwed-up board, until the point where you roll out the next PCB version and then you permanently change the firmware at that point in time. These things should generally be handled by version control, not by conditional compilation. Make a separate branch for the screwed up board, problem solved.
If that isn't possible, well... when designing the board, it is also possible to do tricks like sacrificing 4 GPIO pins, set them as inputs and tie each to Vdd or GND in order to form a binary number. The MCU can read the pins to tell the hardware version of the board and there's room for 16 revisions. Then you can have one binary that supports every single version.
Always question the requirements!
Comment: Thank you Lundin for elaborating details on the production code implementation. it's very helpful. May I ask if there is a batter way to write a test script independent of those hardware changes? here is the example code from the book [LedDriverTest.c](https://media.pragprog.com/titles/jgade/code/unity/LedDriver/LedDriverTest.c) you can see there are some magic numbers for LEDs output checking. I doubt that using magic number in test script is a smart way.


LXC 2.0 Ownership problems

Question: If I launch an Ubuntu 16.04 container and install for instance memcached on it, then almost all the processes /proc//* descriptors are owned by nobody.nogroup or 65334.65334
Where as if I would install memcached it on my host machine these would be owned by root.root
In effect I can't see which processes are bound to which ports using netstat, since even root get's permission denied to these files:
On my LXC container: https://pastebin.com/raw/FXp1bcwm
On my host system: https://pastebin.com/raw/LZjuN6iq
What can I do about this?
Comment: I think you have to add a user mapping for the container. Not really sure, but [there](https://insights.ubuntu.com/2017/06/15/custom-user-mappings-in-lxd-containers/) is some more information about it.
Answer: This happens because you are trying to install memcached as 'x' user however, that user ID might not exist or might be different.
A good script could be found here: https://gist.github.com/bloodearnest/ebf044476e70c4baee59c5000a10f4c8

LXC 2.0 Ownership problems


can the chromecast device be programmed also be used as a sender

Question: I wanted to know if it is possible to program one chromecast device to send video to another chromecast device acting like a receiver. I have browsed the API reference on the Google developer website and it doesn't seem to be supported, but also doesn't say it isn't supported.
Comment: Basicly against the UX theme of the device.
Comment: Thanks for the reply @LesVogel-GoogleTVDevRel. It just seems like a natural extension for the device. That would allow for the android/chrome devices to be the hub for all entertainment devices. I was picturing attaching a chromecast device to each media device and having an android tablet control it all. Go from cable to blu-ray to netflix to youtube all controlled by one device. It is a similar concept to what Microsoft is doing with the XBOX One by making it the entertainment hub for your living room.
Comment: @DMC3600

That can already be done though.  If you use the standard Cast action button, you are of course limited to working with a single Chromecast, but if you do a bit more manual coding, you can get a list of all Chromecasts, and connect to all of them simultaneously and control them each manually and individually.  You will need to build your own UI for selecting and controlling them, though.
Answer: This is not currently possible, and probably never will be.  Chromecast devices don't "source' video, they only transfer from wifi to an HDMI output.  it is possible to play the same stream on two different chromecast devices, but there is no way to synchronize them at the moment.  In theory this is something the devices could support.


Why are all my p-values so low in logistic regression model?

Question: I'm trying to make a regression model to explain a dependent variable that follows a binomial distribution - I have data on the number of successes and the number of trials for each observation. The proportion of successes to trials ranges from about 0.2 to 0.97. I have ~30 independent variables I am choosing from for my model, some of which are continuous, some are discrete, some are percentages.
In researching how best to model this situation, I thought it made sense to use logistic regression. I am working in MATLAB and have implemented this using a generalized linear model for a binomial distribution with a logit link function. From what I can tell the fact that my independent variables vary in data type is okay.
I have tried doing this for many different combinations of independent variables, and my results keep showing that the p-values for all of the independent variables are 0 (out to many decimal places). Note also that SSE is quite high.
Generalized Linear regression model:
    logit(FC) ~ 1 + DDP + SCHabs + Train + Exp_hab + Exp_ratio + Sch_tot
    Distribution = Binomial

Estimated Coefficients:
                Estimate         SE        tStat           pValue
               __________    __________    _______    ___________

(Intercept)       -0.4451     0.0060914    -73.071              0
DDP             0.0036999    8.0929e-05     45.718              0
SCHabs             1.7664      0.025142     70.259              0
Train          2.0887e-05    9.8255e-08     212.58              0
Exp_hab           -6.8407       0.25137    -27.214    4.4104e-163
Exp_ratio         -0.1891      0.016817    -11.245     2.4572e-29
Sch_tot        2.9644e-06    1.5103e-08     196.28              0

30 observations, 23 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 3.05e+05, p-value = 0
SSE = 2.74 e+09

EDIT: Here is the same output when I run the regression in R:
Call:
glm(formula = cbind(FC, NotFC) ~ DDP + SCHabs + Train + Exp_hab +
    Exp_ratio + Sch_tot, family = binomial, data = regdata)

Deviance Residuals:
     Min        1Q    Median        3Q       Max
-185.628   -28.537    -1.076    45.945   244.305

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.453e-01  6.089e-03  -73.13   <2e-16 ***
DDP          3.586e-03  8.056e-05   44.52   <2e-16 ***
SCHabs       1.741e+00  2.470e-02   70.49   <2e-16 ***
Train        2.091e-05  9.833e-08  212.65   <2e-16 ***
Exp_hab     -6.916e+00  2.511e-01  -27.54   <2e-16 ***
Exp_ratio   -1.761e-01  1.672e-02  -10.53   <2e-16 ***
Sch_tot      2.960e-06  1.508e-08  196.30   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 489866  on 29  degrees of freedom
Residual deviance: 184863  on 23  degrees of freedom
AIC: 185168

Number of Fisher Scoring iterations: 5

At first I thought maybe it was a multicollinearity problem, but the standard errors are low, none of the values in the correlation matrix are above 0.65, and a collintest did not flag any issues.
Next I thought maybe I have too many independent variables in my model, so I tried using just a single independent variable. I tried several of them, no improvement. I then tried using stepwise regression to minimize the SSE. This added in all of the variables I included in the data table, as well as many of the cross terms. Only 1 p value was insignificant, which still seems absurd.
Generalized Linear regression model:
FC ~ [Linear formula with 29 terms in 10 predictors]
Distribution = Binomial

Estimated Coefficients:
                          Estimate        SE           tStat        pValue
                         ___________    __________    ________    ___________

(Intercept)               6.7475       0.94871      7.1123     1.1413e-12
DDP                      0.77076       0.11393      6.7654     1.3296e-11
LWE                      -1.7078       0.05188     -32.919    1.1664e-237
MinBlocks              -0.053431      0.005388     -9.9167     3.5224e-23
STHabs                   -7.3881        1.0538     -7.0107     2.3707e-12
SCHabs                    5.4708        0.7251      7.5449     4.5264e-14
Train                -0.00074337    0.00012089     -6.1494       7.78e-10
Exp_hab                  -705.19         85.84     -8.2152     2.1178e-16
Exp_ratio                -4.3504       0.88336     -4.9249     8.4421e-07
Sch_tot              -8.5885e-07    1.4783e-06    -0.58097        0.56126
Nonfuncp                  122.31        2.6776      45.678              0
DDP:LWE                  0.13659     0.0097325      14.034     9.6492e-45
DDP:MinBlocks          0.0074521     0.0005404       13.79     2.9306e-43
DDP:STHabs              0.080665      0.030503      2.6445      0.0081805
DDP:SCHabs              -0.57123       0.11228     -5.0873     3.6318e-07
DDP:Train            -2.1825e-06    2.8467e-07     -7.6667     1.7648e-14
DDP:Exp_hab              -67.035        10.265     -6.5307     6.5451e-11
LWE:MinBlocks           0.037295      0.001902      19.608     1.3131e-85
LWE:STHabs               0.60362      0.085094      7.0936      1.307e-12
LWE:SCHabs                -11.87       0.77467     -15.322     5.4435e-53
LWE:Train             3.0939e-05     4.983e-06       6.209      5.333e-10
LWE:Exp_hab              -112.45        22.495     -4.9988     5.7686e-07
LWE:Sch_tot           4.8253e-06    1.1147e-07      43.289              0
MinBlocks:Exp_hab         1.1291        0.2784      4.0556     5.0011e-05
STHabs:Train          0.00060167    6.8549e-05      8.7772     1.6763e-18
STHabs:Exp_hab            724.38        83.805      8.6437      5.442e-18
STHabs:Nonfuncp          -357.31        8.4226     -42.423              0
Train:Exp_hab           0.051368     0.0083675      6.1389     8.3074e-10
Exp_hab:Exp_ratio          769.8        79.383      9.6974      3.094e-22

30 observations, 1 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 4.9e+05, p-value = 0
SSE = 637

Finally, I decided to simulate a meaningless independent variable by drawing from the normal distribution and adding it to the regression to see if it was also significant. I did this many times, and every time this meaningless variable also had a p-value of zero, which obviously can't be right.
What's going on here? What am I doing wrong?
EDIT:
Here are scatter plots of the key variables from the regression above. A few of the discrete variables, like DDP and LWE, have lots of zeros in them. Which are meaningful, real zeros. I thought about maybe coding them as binary variables instead. I also include a correlation matrix plot.

EDIT AGAIN:
Here is a public dropbox link to a .csv file with my data. Note the dependent variable I am modeling is "FCperHab" which is calculated by dividing "FC" by "Hab". For context, these are data on states in India. I am trying to explain the percentage of habitations in each state that have full access to drinking water.
https://www.dropbox.com/s/w5gzdznhuad59gp/regdata_CV.csv?dl=0
Comment: I can't reproduce your results with Stata. Nor it is easy to reconcile your results posted above with the Dropbox file. As you say, your response variable is `FCperHab` but your results above are for `FC`. In Stata, a fairly standard logit model identifies only `Train` as a significant predictor at conventional levels. I'd surmise a bizarre MATLAB problem in which you fed in the wrong variable and/or called up the wrong software. I don't use MATLAB routinely. If this really is about the details of MATLAB, it would be off-topic here.
Comment: multicollinearity would inflate p-values, not make them small.
Comment: can you show us some scatter plots for your data.
Comment: Without having any idea what the problem is here, I would suggest you try running the same analysis in R, SPSS, or whatever, and seeing what happens. And, as @MHH says, plot everything!
Comment: @MHH I have edited the post above to include some plots.
Comment: @Eoin Good idea. I'm not great with R, but just gave it a try. Repeated the first regression above and got the same results. So I think it's unlikely I'm just messing up the syntax. I added some plots above if that gives you any other ideas. Thanks.
Comment: Can you post your data?
Comment: @kjetilbhalvorsen I've just added a public dropbox link to a csv file with my data. Please let me know if there's a different preferred method to post it. Thanks.
Comment: NB Not all implementations of logit treat continuous fractional responses correctly. Note that your fraction is based on a binary response, but does not itself follow a binomial distribution.
Comment: @NickCox Thanks for your help. I'd love to see the Stata results. Note I edited the post to include results I ran in R as well - they are identical to my MATLAB results. The MATLAB regression does use both FC and Hab, the syntax just requires them separately. You'll see in the R regression the dependent variable includes both FC and notFC, which is successes and failures. Thoughts on why it would be different in Stata but not R? Is my understanding correct that a glm model with logit link function is the same as logit model?
Comment: Also, I didn't realize logit doesn't always treat fractional responses correctly. I will look into this. Do you think it's problematic that the data doesn't follow a binomial distribution? Thanks again.
Comment: Quick answer: Your more complicated model has, it seems, almost as many parameters as observations. In essence, you have a near perfect fit, but not helpfully. I'll follow with a more detailed response.
Comment: $Nick Cox: I think the problem is different, all the binomial n's are very high, and independence is probably questionable. Maybe a quasibinomial model? Also, without knowing what all the different variables represent, it is limited what we can contribute.
Comment: @kjetilbhalvorsen I don't disagree. My comment is just a first instalment and the comment on the more complicated model is just that, it is far too complicated, which is a fatal objection. Other issues do remain and I will post shortly.
Comment: Using a quaibinomial family gives this: ... (Dispersion parameter for quasibinomial family taken to be 11955.66)   ... and **all** significance disappears!
Answer: As pointed out in comments, the more complicated model has too many predictors to be taken seriously. I focus here on models with 6 predictors.
I have used MATLAB very occasionally in the past but not for any related purpose and am emphatically no expert. But on the face of it your MATLAB call does not feed a fractional response (outcome, dependent variable) to the function either directly or indirectly and it is amazing that it produces any output at all. If you are using the same naming conventions across different software, then FC is a count, not a binary response, and not a fit present for a logit function.
Here is the result of a calculation in Stata 14 of a logit model in which the response is treated as a continuous proportion:
. fracreg logit fcperhab ddp schabs train exp_hab  exp_ratio sch_tot

Iteration 0:   log pseudolikelihood = -19.110279
Iteration 1:   log pseudolikelihood =  -18.06008
Iteration 2:   log pseudolikelihood = -17.979059
Iteration 3:   log pseudolikelihood = -17.976617
Iteration 4:   log pseudolikelihood = -17.976616

Fractional logistic regression                  Number of obs     =         30
                                                Wald chi2(6)      =      21.06
                                                Prob > chi2       =     0.0018
Log pseudolikelihood = -17.976616               Pseudo R2         =     0.0865

------------------------------------------------------------------------------
             |               Robust
    fcperhab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         ddp |   .0033372   .0044366     0.75   0.452    -.0053583    .0120327
      schabs |   .0195366   .0162846     1.20   0.230    -.0123806    .0514538
       train |   .0000243   8.40e-06     2.90   0.004     7.85e-06    .0000408
     exp_hab |  -4.832934   10.20002    -0.47   0.636     -24.8246    15.15873
   exp_ratio |  -.0016535    .904611    -0.00   0.999    -1.774658    1.771351
     sch_tot |   1.15e-06   9.91e-07     1.16   0.248    -7.98e-07    3.09e-06
       _cons |  -.1102337   .3944965    -0.28   0.780    -.8834326    .6629653
------------------------------------------------------------------------------

For all that the results may seem disappointing, there is nothing pathological about the output. Non-Stata users can probably guess that _cons means the intercept. As context here are some summary statistics:
. su fcperhab ddp schabs train exp_hab  exp_ratio sch_tot

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
    fcperhab |         30    .6355612    .2472515   .2056616   .9978048
         ddp |         30    7.766667    19.39016          0         85
      schabs |         30        8.83    10.14478          0       40.2
       train |         30    18741.83    26769.19          0     108643
     exp_hab |         30    .0152065    .0195379          0   .1046035
-------------+---------------------------------------------------------
   exp_ratio |         30    .3794279    .2442145          0   .7318189
     sch_tot |         30    203229.1    410059.5        251    2198181

Stata users who have not yet upgraded to 14 [released 7 April 2015] may note that glm fcperhab ddp schabs train exp_hab  exp_ratio sch_tot, link(logit) f(binomial) vce(robust) gives the same calculation. But it is important here to spell out to Stata, in whatever version is used, that binomial is at best a convenient fiction here.  In fracreg that is automatic; otherwise, vce(robust) is that signal and if we omit it results are quite different, but still free of pathological very high or very low P-values:
. glm fcperhab ddp schabs train exp_hab  exp_ratio sch_tot, link(logit) f(binomial)
note: fcperhab has noninteger values

Iteration 0:   log likelihood = -13.058365
Iteration 1:   log likelihood = -12.952102
Iteration 2:   log likelihood = -12.950226
Iteration 3:   log likelihood = -12.950225
Iteration 4:   log likelihood = -12.950225

Generalized linear models                         No. of obs      =         30
Optimization     : ML                             Residual df     =         23
                                                  Scale parameter =          1
Deviance         =  5.476451096                   (1/df) Deviance =   .2381066
Pearson          =   4.86766495                   (1/df) Pearson  =   .2116376

Variance function: V(u) = u*(1-u/1)               [Binomial]
Link function    : g(u) = ln(u/(1-u))             [Logit]

                                                  AIC             =   1.330015
Log likelihood   = -12.95022455                   BIC             =  -72.75109

------------------------------------------------------------------------------
             |                 OIM
    fcperhab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         ddp |   .0033372    .023125     0.14   0.885    -.0419869    .0486613
      schabs |   .0195366   .0430982     0.45   0.650    -.0649343    .1040075
       train |   .0000243   .0000275     0.88   0.377    -.0000297    .0000783
     exp_hab |  -4.832937   27.53167    -0.18   0.861    -58.79402    49.12815
   exp_ratio |  -.0016538   1.961583    -0.00   0.999    -3.846287    3.842979
     sch_tot |   1.15e-06   2.69e-06     0.43   0.671    -4.13e-06    6.43e-06
       _cons |  -.1102337    .785408    -0.14   0.888    -1.649605    1.429138
------------------------------------------------------------------------------

There are many other scientific and statistical issues not clear without further discussion and analysis, and I will raise just two:

Some predictors appear to be absolute counts or amounts, and it's not all clear why absolute values are natural here.
Some of the predictors may need or benefit from transformation.


How do I get the domain name in Windows 7

Question: I've written a dll that runs an Excel add-in (some years ago).
I use this code to retrieve the domain name and that works fine in Windows XP, but it fails in Windows 7.
Only if I run as administrator does it work.
However I don't want to run as administrator because this code is part of an Excel add-in dll and Excel cannot find the user's files if running as admin.
MyReg:= TRegistry.Create;

MyReg.RootKey:= HKEY_LOCAL_MACHINE;
MyReg.OpenKey(RegKeyWin7,false);
NetworkID2:= lowercase(trim(MyReg.ReadString(RegValWin7)));
MyReg.CloseKey;

FreeAndNil(MyReg);

FNetworkOK:= (NetworkID2 = OKRes4);
//Temp check to pinpoint the problem.
if FNetWorkOK = false then ShowMessage('Error wrong domain: '+NetworkID2)
else ShowMessage('all ok');

How do I retrieve the domain name in Windows 7 using Delphi under normal privileges?
Answer: Use NetWkstaGetInfo in the Win32 API, requesting info via level value = 100.

Return information about the
  workstation environment, including
  platform-specific information, the
  name of the domain and the local
  computer, and information concerning
  the operating system. The bufptr
  parameter points to a WKSTA_INFO_100
  structure.
Comment: @Jeroen, Yea, I found this one: http://www.delphi-central.com/tutorials/delphi_user_list.aspx He! that's the same one you found, anyway good one because it works on the devel-machine. Lets see if it works in production.
Comment: Thanks, Checked it out, I need to be a authenticated user to access, so I think normal users have the privileges run this. Kind of hate to have to use these horrible Win_api functions, was hoping that was a Delphi wrapper function for that.
Comment: Yes, Win32 does it all but can be ugly.
Comment: @Johan: there are many Delphi examples for NetWkstaGetInfo: http://www.google.com/search?q=NetWkstaGetInfo+delphi, for instance this one http://www.delphi-central.com/tutorials/delphi_user_list.aspx
Comment: @Johan - glad to hear it, all the best


What is happening actually if "variable is declared but its value is never read"?

Question: I got the same warning a lot of times when I use this pattern below.
<code>function test() {
  let value: number = 0 // I get the warning here: value is declared but its value is never read

  value = 2

  return false
}
</code>
My question is that what is really happening in this case?
Comment: Nothing is happening. And that's the point - it's useless to have an unused variable. It's very likely a mistake. If you have a variable, you *probably meant to use it*.
Comment: You set `value` to `0`, and then immediately set it to `2`. The setting to `0` is very superfluous, which is what is being pointed out to you. In this particular code you're not even using the `value` variable for anything afterwards, so it may be completely superfluous.
Comment: @deceze Warning has nothing to do with being set to 0 and then to 2. It's just that neither of those values are ever used.
Comment: @Roberto Assuming you *did* use `value` afterwards, the warning would still hold for the useless first assignment. I'll assume the `value` *is* used somewhere afterwards, since this is obviously not complete code.
Comment: @deceze But it wouldn't, the warning has nothing to do with that. Try it.
Comment: @Roberto We don't even know what tool exactly is producing this warning. My IDE for instance *will* warn about the useless first assignment. I don't know if or what React stack would warn about this, but it surely would make sense there too.
Comment: @deceze It's tslint
Answer: That means you are not using that variable it is declared there and value is assigned to it but it is of no use. Better not to declare that variable.


order a list by the `snd` of a tuple within that list:

Question: I have the following list of tuples:
[("a",6),("b",1),("c",2),("d",4),("e",1),("f",1),("g",5),("h",3),("i",1),("j",2)]

but would like to order the tuples in the list by the snd element of the tuple. That way, I get an answer similar to:
[("b",1),("e",1),("f",1),("i",1),("c",2),("j",2),("h",3),("d",4),("g",5),("a",6)]

(i.e. the list is ordered by the second (snd) of each tuple.
Answer: sortBy (comparing snd)

where sortBy is in List and comparing is in Data.Ord.
Answer: As an alternative to jleedev's answer:
sortBy (compare `on` snd)

where sortBy is in Data.List and on is in Data.Function.
IMO this formulation is slightly nicer because compare is just the standard class method from Ord, and on is a more generally useful function that comparing. But there's not much in it.
Answer: Since Data.List 22.214.171.124 one can also use sortOn as follows: sortOn snd.


É possível saber a Coluna selecionada por GREATEST no MySQL?

Question: Hipoteticamente, tendo 8 colunas em uma tabela X, 4 dessas colunas armazenam uma média:
AVG(colunaN)

E as outras 4 armazenam a quantidade de elementos dessa média
COUNT(column)

Em uma query estou tentando pegar a maior média dessas 4, para isso utilizo a função:
GREATEST(coluna1,coluna2,coluna3,coluna4) AS maior_media

Como eu posso fazer para identificar qual coluna foi seleciona como maior em "GREATEST" para retornar o seu
COUNT(colunaSelecionadaPorGreatest)

Que na situação acima, está armazenada em uma das outras 4 colunas.
Comment: Não seria mais prático trabalhar com linhas e não colunas? Daí bastaria um GROUP BY /MAX.
Comment: bom, na arquitetura desse meu projeto da faculdade, eu defini que é melhor eu atualizar as médias e contagens em uma tabela separada, para evitar fazer esses cálculos para cada item que vou mostrar no sistema, então ele atualiza esses dados periodicamente utilizando cronjobs.
Comment: Então faça uma gambiarra utilizando CASE / WHEN.
Answer: Foi muito simples de resolver, encontrei o uso de variáveis no MySQL, dessa forma armazenei o resultado do "GREATEST" em uma:
SELECT
    @greatest := (GREATEST(coluna1,coluna2,coluna3,coluna4)) AS greatest_average,
    CASE @greatest
         WHEN coluna1 THEN count_coluna1
         WHEN coluna2 THEN count_coluna2
         WHEN coluna3 THEN count_coluna3
         WHEN coluna4 THEN count_coluna4
    END AS greatest_counter,
    CASE @greatest
         WHEN coluna1 THEN "Qualidade"
         WHEN coluna2 THEN "Velocidade"
         WHEN coluna3 THEN "Durabilidade"
         WHEN coluna4 THEN "Quantidade"
    END AS greatest_counter_type

E criei dois CASES, um para armazenar o valor do contador correspondente à coluna selecionada por "GREATEST", e o outro para armazenar o tipo selecionado.


jQuery animate() com comportamento indesejado

Question: Boa noite! Estou fazendo uma aplicação e preciso de utilizar uns efeitos de slide. Utilizei o animate do jquery para obter o resultado desejado.
Ficou até bom, porém enquanto a animação ocorre a altura da div aumenta/diminui para que todo o texto dentro caiba, segue o exemplo:

$('#btnprincipal').click(function (){
  $('#divTexto').animate({ width: 'toggle' }, 500);
});

<button id="btnprincipal">Aperte aqui</button>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultricies erat massa, sed posuere risus pharetra sed. Integer in pretium tortor. Sed vestibulum orci ut odio lacinia fermentum. Ut nec nulla sed ligula finibus volutpat vel nec sem. Nulla aliquam mauris vel tortor sodales, id finibus est varius. Duis suscipit ultrices dolor, vitae molestie ex fringilla vitae. Suspendisse et sapien eu orci egestas volutpat quis vitae mauris. Curabitur nisl risus, maximus ac tristique sed, consequat quis risus. Nunc vel venenatis mi, eu faucibus arcu. Nunc quis faucibus sem. Vestibulum dignissim commodo lectus, sit amet iaculis metus lacinia ac. Integer placerat faucibus metus et consequat. Aliquam sed volutpat justo, vel pharetra nunc.

O que posso fazer para previnir esse comportamento?
Comment: se difinir o `height` do div isso não deve acontecer, por exemplo `height: 200px`
Comment: Apaguei minha resposta, agora que vi a resposta aceita que entendi o propósito da pergunta :)
Comment: Sim mas eu não podia definir o height porque o texto era gerado de acordo com o que o usuario digitasse, nisso que travei. A resposta aceita deu a solução para o problema
Answer: Para que o overflow funcione como esperado, é necessário que a div tenha a altura definida. Porém, não pode definir uma altura estática, pois ela varia de acordo com o conteúdo (texto) e eventualmente de acordo com o tamanho do viewport.
A minha sugestão é definir apenas a altura enquanto a animação está a decorrer e remove-la assim que a animação termine. Para isso podemos utilizar os callbacks start e done.
Algo como o seguinte código:

$('#btnprincipal').click(function (){
  const divTexto = $('#divTexto');

  divTexto.animate(
    { width: 'toggle' },
    {
      duration: 500,
      start: () => {
        divTexto.css('height', divTexto.height());
      },
      done: () => {
        divTexto.css('height', 'auto');
      },
    },
  );
});

<button id="btnprincipal">Aperte aqui</button>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultricies erat massa, sed posuere risus pharetra sed. Integer in pretium tortor. Sed vestibulum orci ut odio lacinia fermentum. Ut nec nulla sed ligula finibus volutpat vel nec sem. Nulla aliquam mauris vel tortor sodales, id finibus est varius. Duis suscipit ultrices dolor, vitae molestie ex fringilla vitae. Suspendisse et sapien eu orci egestas volutpat quis vitae mauris. Curabitur nisl risus, maximus ac tristique sed, consequat quis risus. Nunc vel venenatis mi, eu faucibus arcu. Nunc quis faucibus sem. Vestibulum dignissim commodo lectus, sit amet iaculis metus lacinia ac. Integer placerat faucibus metus et consequat. Aliquam sed volutpat justo, vel pharetra nunc.

Comment: Muitíssimo obrigado! Não sabia como solucionar o problema de ter a minha altura variável, isso resolve meu problema


_default_ VirtualHost overlap on port 443, the first has precedence

Question: I have two ruby on rails 3  applications running on same server, (ubuntu 10.04), both with SSL.
Here is my apache config file:
<VirtualHost *:80>
ServerName example1.com
DocumentRoot /home/me/example1/production/current/public
</VirtualHost>
<VirtualHost *:443>
ServerName example1.com
DocumentRoot /home/me/example1/production/current/public
SSLEngine on
SSLCertificateFile /home/me/example1/production/shared/example1.crt
SSLCertificateKeyFile /home/me/example1/production/shared/example1.key
SSLCertificateChainFile /home/me/example1/production/shared/gd_bundle.crt
SSLProtocol -all +TLSv1 +SSLv3
SSLCipherSuite HIGH:MEDIUM:!aNULL:+SHA1:+MD5:+HIGH:+MEDIUM
</VirtualHost>

<VirtualHost *:80>
ServerName example2.com
DocumentRoot /home/me/example2/production/current/public
</VirtualHost>
<VirtualHost *:443>
ServerName example2.com
DocumentRoot /home/me/example2/production/current/public
SSLEngine on
SSLCertificateFile /home/me/example2/production/shared/iwanto.crt
SSLCertificateKeyFile /home/me/example2/production/shared/iwanto.key
SSLCertificateChainFile /home/me/example2/production/shared/gd_bundle.crt
SSLProtocol -all +TLSv1 +SSLv3
SSLCipherSuite HIGH:MEDIUM:!aNULL:+SHA1:+MD5:+HIGH:+MEDIUM
</VirtualHost>

Whats the issue:
On restarting my server it gives me some output like this:
 * Restarting web server apache2
 [Sun Jun 17 17:57:49 2012] [warn] _default_ VirtualHost overlap on port 443, the first has precedence
 ... waiting [Sun Jun 17 17:57:50 2012] [warn] _default_ VirtualHost overlap on port 443, the first has precedence

On googling why this issue is coming I got something like this:
You cannot use name based virtual hosts with SSL because the SSL handshake (when the browser accepts the secure Web server's certificate) occurs before the HTTP request, which identifies the appropriate name based virtual host. If you plan to use name-based virtual hosts, remember that they only work with your non-secure Web server.
But not able to figure out how to run two ssl application on same server.
Can any one help me?
Comment: You don't have any `_default_` vhosts in the config you've provided, so they're elsewhere.  What's the output of `apache2ctl -S`?  (Yes, it is possible to run multiple SSL name-based vhosts on different certificates, provided you don't have any need to support client browsers running Windows XP or any others that don't support TLS SNI.  Do you need to support Windows XP?)
Answer: Almost there!
Add this to ports.conf or http.conf and keep your above config.
<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.

    # !important below!
    NameVirtualHost *:443
    Listen 443
</IfModule>

Comment: This no longer works in Apache 2.4.7
Comment: The comment "# !important! needs to be removed or moved to another line. Otherwise, thank you very much for interpreting this inaccurate error message for me.
Comment: Thank you.   I found I needed to comment out Listen 443 because that's also being used in my individual conf.d/website.conf configs
Answer: It helped me executing " /usr/sbin/apachectl -S" also. This command exit shows TWO "ssl.conf" files on the same path. Move or delete the offender file and everything should work after.
Answer: You can add this to your apache config at /etc/apache2/ports.conf:
<IfModule mod_ssl.c>
    Listen 443
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        NameVirtualHost *:443
    </IfModule>
</IfModule>

(This works in both: apache 2.2 and 2.4)
Answer: Just find where the "Listen 443" is and paste above NameVirtualHost *:443
In my case was in ssl.conf file
If you restart the http server and you are getting the message "Listen 443 is deprecated" delete "Listen 443" and leave only NameVirtualHost *:443
Comment: Warning: NameVirtualHost is deprecated and has no effect in current versions of Apache. It may be helpful for very old versions of Apache though.


Dudeney's "Modern Puzzles" - number 38, The Despatch-Rider

Question: In Henry Ernest Dudeney's "Modern Puzzles" of 1926 (now out of print) we have:

The Despatch-Rider

If an army forty miles long advances forty miles while a despatch-rider gallops from the rear to the front, delivers a depatch to the commanding general, and returns to the rear, how far has he to travel?
My solution is that it seems clear that the rider goes twice as fast as the army, so he goes 80 miles one way and 2/3 of 40 miles the other, being an obvious 19 1/3 miles.
But Dudeney's solution is:
"The answer is the square root of twice the square of 40, added to 40. This is $96.568$ miles, or, roughly $96 \tfrac 1 2$ miles."
Anybody got any idea how he arrives at this?
Comment: Remember that the rider's *relative* speed with respect to the army differs for each leg because the army is advancing while he's riding.  The first leg is surely shorter than $80$ miles because the army hasn't advanced the full $40$ miles by the time the rider completes the first leg so your answer can't be right.
Comment: Why is it clear that the rider goes twice as fast as the army? If he does, then the army will have arrived at their destination by the time the rider has reached the front. If I'm interpreting the problem correctly, the army only reaches its destination once the rider has made it all the way back to the rear once more.
Comment: What I'd assume is that the dispatch finishes his task at the same time the last soldier in line has marched 40 miles. Let $y$ be such that  $y+40$ is the distance from the dispatch to his starting position at the time when he meets the general. From this momment, the time it takes the last guy to end his march is the same as the time it takes the dispatch to end his. Using this observation (and the observation that $40+2y=x\cdot40$ if the speed of the dispatch is $x$  miles per minute and the march proceeds at 1 mile per minute), I arrived at the author's figure ($40+40\sqrt2$).
Comment: @TheoBendit Thank you, that was my misunderstanding. I had misinterpreted the question such that I had assumed that the army had travelled 40 miles while the rider had merely travelled to the front. I had not twigged that the 40 miles travelled included the time he returned to the rear again.
Answer: Let $x_{0}$ be the length of the army in miles (in this problem, $x_{0}=40$). Assume the army is travelling along the $x$-axis, with the rear of the army starting at $0$, the front of the army starting at $x_{0}$. Assume without loss of generality that at time $1$, the despatch-rider has finished (so the front of the army has reached position $2x_{0}$ and the despatch-rider is at position $x_{0}$, the back of the army).
Let $t_{0}$ be the time at which the despatch-rider reached the front of the army (after which point he reverses direction), and let $m$ be the speed of the despatch-rider. Then we have the following facts:

the position of the front of the army at time $t$ is $x_{0} + x_{0}t$ for $0\le t \le 1$
position of the despatch-rider at time $t$ is $mt$ for $0\le t \le t_{0}$ and $-mt+b$ for $t_{0}\le t \le 1$, where $m$ is the constant speed of the despatch rider, and $b$ is a constant to be determined.

We can find $m, b, t_{0}$ in terms of known quantities. Then the distance travelled by the despatch-rider will be $m$ (as he travels at speed $m$ for time $1$).
From the second bullet point above, we must have
$$mt_{0} = -mt_{0}+b \Rightarrow 2mt_{0} - b = 0. \tag{1}$$
Also, since the despatch-rider is at position $x_{0}$ at time $1$, the second bullet point implies that
$$-m+b = x_{0}.\tag{2}$$
Adding $(1)$ and $(2)$ gives $(2t_{0}-1)m = x_{0}\Rightarrow m = \frac{x_{0}}{2t_{0} - 1}$.
So $(1)$ implies that $b = 2t_{0}m = \frac{2t_{0}x_{0}}{2t_{0}-1}$.
Now, the first bullet point above implies that $x_{0}+x_{0}t_{0} = -mt_{0}+b$ (i.e. at time $t_{0}$ the head of the army and the despatch-rider are at the same position). So we have
$$\begin{align}
x_{0} + x_{0}t_{0} &= -mt_{0}+b\\
\Rightarrow x_{0} + x_{0}t_{0} &= -\frac{x_{0}t_{0}}{2t_{0} - 1} + \frac{2t_{0}x_{0}}{2t_{0}-1} \\
\Rightarrow 1 + t_{0} &= \frac{t_{0}}{2t_{0} - 1}\\
\Rightarrow (2t_{0} - 1)(1 + t_{0}) &= t_{0} \\
\Rightarrow 2t_{0} + 2t_{0}^{2} - 1 - t_{0} &= t_{0} \\
\Rightarrow 2t_{0}^{2} &= 1 \\
\Rightarrow t_{0} &= \frac{\sqrt{2}}{2}.
\end{align}$$
So $m = \frac{x_{0}}{2t_{0} - 1} = \frac{x_{0}}{\sqrt{2}-1} = \left(\sqrt{2} + 1\right)x_{0}$.
Hence the distance travelled by the despatch-rider is $m = \left(\sqrt{2} + 1\right)x_{0}$. In your particular problem, since $x_{0}$ is $40$ miles, the total distance travelled by the despatch-rider is $\left(\sqrt{2} + 1\right)\times 40 \approx 96.57$ miles.
Answer: Alternative approach:
Assume that the army's length is $(40)$.
Assume that the rider's speed is $(r > 1)$ while the army's speed is $1$.
Assume that the first leg of the trip uses time $t_1.$
Assume that the second leg of the trip uses time $t_2.$
The distance traveled by the rider is $r(t_1 + t_2)$.
Further, from the problem's constraints,
$40 = (1) \times (t_1 + t_2) \implies (t_1 + t_2) = 40.$
Therefore, the problem is reduced to computing $r$.

During $t_1$, the speed at which the rider covers the $(40)$ is (in effect) $(r - 1)$.
During $t_2$, the speed at which the rider covers the $(40)$ is (in effect) $(r + 1)$.
Therefore, $t_1(r - 1) = 40 = t_2(r + 1).$
Therefore $40 = t_1 + t_2 = \displaystyle \frac{40}{r-1} + \frac{40}{r+1}.$
This implies that $1 = \displaystyle \frac{1}{r-1} + \frac{1}{r+1} = \frac{2r}{r^2 - 1}$.
Therefore, $\displaystyle r^2 - 2r - 1 = 0 \implies r = \left(\frac{1}{2}\right) \times \left[2 \pm \sqrt{8}\right] = 1 \pm \sqrt{2}$.
Since $r$ must be $> 0$, (in fact $r$ must be $> 1$), you have that $r = 1 + \sqrt{2}$.


Language select: how to set up

Question: In the current set up for every English page, there's a Spanish equivalent.  Each page has its own unique URL.
Previously we had it set up so that you could click "Spanish" within the English page and it would call the Spanish URL and visa-versa.
We've been asked to make it global -a button at the top of the site.
Is there a clean way to detect what the Spanish equivalent is of the current page with PHP and make the switch?
Answer: Locale module by default provides you a block, Language Switcher, that switches language of the site. It's a list of installed language.
If you like a fancy switcher, you can install this module:
Language Switcher Dropdown

Language Switcher Dropdown is a very simple module that exposes a new
  block, similar to the default Language Switcher block provided by
  Locale module. The new block allows site visitors to switch languages
  using a drop-down select list instead of using hyperlinks.
The module also integrates well with Language Icons module if
  installed.

Here's an example
Answer: I use i18n serie for multilingual websites and there is a block language which works on its own for this purpose. I'm not sure to understand your multilingual setup, if it is not using i18n, I wouldn't say if you can use it independantly from the i18n suite. More info here: https://drupal.org/node/1279698

Language select: how to set up


PEP eight in wxpython

Question: I use this code and (wx.SCRIPT, wx.ITALIC, wx.BOLD) are signed as pep8 warning. How can i Fix that?
font = wx.Font(9, wx.SCRIPT, wx.ITALIC, wx.BOLD, False, u'Consolas')
Comment: What warning? PEP8 has a lot of warnings associated with it.
Comment: I don't see anything obviously wrong with this line though. You probably don't have enough whitespace around this line if I had to guess.
Answer: when I pasted your code into pycharm, it said that it "can't find reference to wx.SCRIP, wx.ITALIC and wx.BOLD"
if that disturbs you, you can fix it as follows:
font = wx.Font(9, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_ITALIC, wx.FONTWEIGHT_BOLD, False, u'Consolas')

more on it: wxpython docs

PEP eight in wxpython