Rsync hangs: expand file_list pointer array to N bytes, did move

Question: Rsync is going into "interruptible sleep" mode after transferring some files from a local folder to a NFS folder.
The folder I am trying to backup contains more than 180gb of data.
This is what rsync outputs before it hangs:
[sender] expand file_list pointer array to 524288 bytes, did move

I am running Ubuntu Server 14.04 LTS with rsync version 3.1.0 protocol version 31 and I am running rsync with these options:
/usr/bin/rsync -rHAXxvvut --numeric-ids --progress {SRC_FOLDER} {NFS_FOLDER}

Thanks for any hints
Comment: Adding the -v (verbose) option more than once can output more info. Try to add something similar to: `-vv`  and see the output.
Comment: I got already the verbose option and it's not outputting anything more than the message in my question.
Comment: Is your OS 64 bits?
Comment: @ringø yes it's 64 bit
Answer: Considering that the rsync you're using is an open-source software, it's quite easy to get access to related source code.
After downloading the main .tar.gz and applying the Ubuntu patch (rsync_3.1.0-2ubuntu0.4.diff.gz), you end up with exactly the code underlying the rsync you're using. Something like this:
$ mkdir rsync
$ cd rsync/
$ wget http://archive.ubuntu.com/ubuntu/pool/main/r/rsync/rsync_3.1.0.orig.tar.gz
$ wget http://archive.ubuntu.com/ubuntu/pool/main/r/rsync/rsync_3.1.0-2ubuntu0.4.diff.gz
$ gzip -d rsync_3.1.0-2ubuntu0.4.diff.gz
$ tar zxvf rsync_3.1.0.orig.tar.gz
$ cd rsync-3.1.0/
$ patch -p1 < ../rsync_3.1.0-2ubuntu0.4.diff

Now a simple grep can quickly tell us the context of your error message:
$ grep -r 'expand file_list pointer array to'
flist.c:        rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",

So you're lucky, as your error message is used in a single fragment of a single file. Namely: flist.c.
Let's take a look:

It's relatively easy to guess that the routine containing the error message (lines 325, 326, 327, 328) is named flist_expand and sounds like something needed to ensure that the whole file list (to rsync) can be held in a properly sized in-memory structure (aka: the more files you need to rsync, the more memory is required to handle rsync-computations, and as such a list is not known "in advance", it needs to be dynamically computed, by allocating proper chunks of memory to a "list" [more or less]).
So, I would bet that your problem relies NOT on the size of data you're rsync-ing, but on the number of files. I'd try splitting your rsync in multiple sub-rsyncs, by focusing on internal subfolders.
Actually, it would be nice to better investigate the:

line 328: (new_ptr == flist->files) ? " not" : "");
line 334: out_of_memory("flist_expand");

but this goes much beyond my initial goal :-)
Anyway, I would bet that checking your logs you would find some "out of memory" message… :-)
HTH!
Answer: Two remarks that may help

have always been satisfied with the -a option (from the man "archive mode; equals -rlptgoD (no -H,-A,-X)")
rsync may be waiting for NFS to give access to a file. It seems NFS is able to actually get rsync locked (probably when overwriting a certain file), and it'd be interesting to see which file was being accessed by rsync just before entering its "sleep". There is a command to see what files are currently opened by rsync lsof -ad3-999 -c rsync (from askubuntu)
Answer: Using rsync to transfer content to an NFS folder is likely to be very inefficient.  Think about what's involved when rsync wants to get a checksum of the remote file, or to modify a remote file in place.  You're far better to have rsync talk to an rsync process running on the file server.  If at all possible I'd change that first and then look to see if your current problem still exists.  I.e. use rsync over ssh or run an rsync daemon process, and leave NFS out of the picture entirely.
To find out what rsync is doing, strace might be useful:
strace -p <PID>

Or have strace start rsync like so:
strace rsync [rsync options] <src> <target>

To attach to a running process by PID may require root permissions, though that can be changed (by root).
Answer: I had the same problem and managed to get around the problem by doing the transfer remotely. For example, instead of doing rsync -avvxAX source/ destination/, I did rsync -avvxAX source/ root@localhost:destination/
rsync probably have specific optimizations for local transfers. Until we can figure out the exact cause of the problem, this workaround fixes the problem for me.
My system: Gentoo Linux, kernel 4.3.0, systemd, rsync 3.1.2 protocol version 31
Comment: Unfortunately I've only NFS or FTP access to the remote directory.
Answer: I ran across this problem just now, while setting up rsnapshot, which uses rsync.
I can now reliably repeat the problem when rsnapshot invokes rsync on a very large directory (47,616 directory entries) for the second repeat rsnapshot of the same directory.  The rsync was going from, and to, a local disk formatted using ext4.
The first time I saw it, the usual /usr/bin/rsync binary, stripped of symbols, "rsync  version 3.2.3  protocol version 31" that is in my Xubuntu "22.04.1 LTS (Jammy Jellyfish)" release was invoked, and I did not have it under strace.  So gdb backtraces were undecipherable to me.  Running strace on the three "hung" rsync processes (parent, sender,and receiver) showed all three processes, after printing the "expand file_list pointer array", were looping on select(2) system calls, with 60 second timeouts.
So I rebuilt that same 3.2.3 version of rsync from source, and repeated the same test (after I figured out how to reproduce this), and got the following three gdb stack backtraces:
============== Backtrace #1 ==============
#0  0x00007f69b109b74d in __GI___select (nfds=nfds@entry=5, readfds=readfds@entry=0x7ffcd48d9c90, writefds=writefds@entry=0x7ffcd48d9d90, exceptfds=exceptfds@entry=0x7ffcd48d9d10, timeout=timeout@entry=0x7ffcd48d9c60)
at ../sysdeps/unix/sysv/linux/select.c:69
#1  0x0000564b6ef0f544 in perform_io (needed=needed@entry=18, flags=flags@entry=2) at io.c:741
#2  0x0000564b6ef115f5 in write_buf (f=f@entry=4, buf=0x7ffcd48da019 "3dec1fdb7cfc0341_0", len=len@entry=18) at io.c:2125
#3  0x0000564b6eee9088 in send_file_entry (first_ndx=<optimized out>, ndx=<optimized out>, symlink_len=0, symlink_name=0x0, file=0x564b70627350,
    fname=0x7ffcd48d9fd0 ".config/_some_application__that_I_use_/Cache/3dec1fdb7cfc0341_0", f=4) at flist.c:565
#4  send_file_name (f=f@entry=4, flist=flist@entry=0x564b6f6ae160, fname=fname@entry=0x7ffcd48dc150 ".config/_some_application__that_I_use_/Cache/3dec1fdb7cfc0341_0", stp=stp@entry=0x0, flags=flags@entry=65540,
    filter_level=filter_level@entry=2) at flist.c:1604
#5  0x0000564b6eeea49d in send_directory (f=f@entry=4, flist=flist@entry=0x564b6f6ae160, fbuf=fbuf@entry=0x7ffcd48dc150 ".config/_some_application__that_I_use_/Cache/3dec1fdb7cfc0341_0", len=len@entry=72,
    flags=flags@entry=65540) at flist.c:1839
#6  0x0000564b6eeea9b6 in send1extra (f=f@entry=4, file=file@entry=0x564b6f454a50, flist=flist@entry=0x564b6f6ae160) at flist.c:1992
#7  0x0000564b6eeeb21f in send_extra_file_list (f=f@entry=4, at_least=at_least@entry=1000) at flist.c:2078
#8  0x0000564b6eef8add in send_files (f_in=f_in@entry=5, f_out=f_out@entry=4) at sender.c:245
#9  0x0000564b6ef02b3c in client_run (f_in=5, f_out=4, pid=pid@entry=2780890, argc=argc@entry=1, argv=argv@entry=0x564b6efe65b0) at main.c:1317
#10 0x0000564b6eee2227 in start_client (argv=<optimized out>, argc=1) at main.c:1580
#11 main (argc=<optimized out>, argv=<optimized out>) at main.c:1812

    ============== Backtrace #2 ==============

#0  0x00007f69b109b74d in __GI___select (nfds=nfds@entry=2, readfds=readfds@entry=0x7ffcd48d7cd0, writefds=writefds@entry=0x7ffcd48d7dd0, exceptfds=exceptfds@entry=0x7ffcd48d7d50, timeout=timeout@entry=0x7ffcd48d7ca0)
    at ../sysdeps/unix/sysv/linux/select.c:69
#1  0x0000564b6ef0f544 in perform_io (needed=76, flags=flags@entry=4) at io.c:741
#2  0x0000564b6ef1070a in send_msg (code=code@entry=MSG_INFO, buf=buf@entry=0x7ffcd48d87e0 "[generator] expand file_list pointer array to 524288 bytes, did move\n", len=len@entry=69, convert=<optimized out>) at io.c:966
#3  0x0000564b6ef05d63 in rwrite (code=<optimized out>, code@entry=FCLIENT, buf=buf@entry=0x7ffcd48d87e0 "[generator] expand file_list pointer array to 524288 bytes, did move\n", len=69, is_utf8=<optimized out>, is_utf8@entry=0) at log.c:339
#4  0x0000564b6ef063e5 in rprintf (code=code@entry=FCLIENT, format=format@entry=0x564b6ef3f368 "[%s] expand file_list pointer array to %s bytes, did%s move\n") at log.c:442
#5  0x0000564b6eee4445 in flist_expand (extra=<optimized out>, flist=0x564b6f41b3e0) at flist.c:309
#6  flist_expand (flist=0x564b6f41b3e0, extra=<optimized out>) at flist.c:287
#7  0x0000564b6eeec9bd in recv_file_list (f=3, dir_ndx=dir_ndx@entry=2960) at flist.c:2584
#8  0x0000564b6ef13243 in wait_for_receiver () at io.c:1699
#9  0x0000564b6ef0f934 in wait_for_receiver () at io.c:1677
#10 perform_io (needed=89, flags=flags@entry=4) at io.c:862
#11 0x0000564b6ef1070a in send_msg (code=code@entry=MSG_INFO, buf=buf@entry=0x7ffcd48da8d0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0 is uptodate\n", len=len@entry=82, convert=<optimized out>) at io.c:966
#12 0x0000564b6ef05d63 in rwrite (code=<optimized out>, code@entry=FCLIENT, buf=buf@entry=0x7ffcd48da8d0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0 is uptodate\n", len=82, is_utf8=<optimized out>, is_utf8@entry=0) at log.c:339
#13 0x0000564b6ef063e5 in rprintf (code=code@entry=FCLIENT, format=format@entry=0x564b6ef3fba5 "%s is uptodate\n") at log.c:442
#14 0x0000564b6eeee433 in set_file_attrs (fname=fname@entry=0x7ffcd48de1c0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0", file=file@entry=0x564b702ccae8, sxp=<optimized out>, sxp@entry=0x7ffcd48dc010,
    fnamecmp=fnamecmp@entry=0x0, flags=<optimized out>) at rsync.c:661
#15 0x0000564b6eef3b78 in recv_generator (fname=fname@entry=0x7ffcd48de1c0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0", file=file@entry=0x564b702ccae8, ndx=44459, itemizing=itemizing@entry=1, code=code@entry=FLOG,
    f_out=f_out@entry=1) at generator.c:1805
#16 0x0000564b6eef4c95 in generate_files (f_out=f_out@entry=1, local_name=local_name@entry=0x0) at generator.c:2318
#17 0x0000564b6ef01f7c in do_recv (f_in=<optimized out>, f_in@entry=0, f_out=f_out@entry=1, local_name=local_name@entry=0x0) at main.c:1106
#18 0x0000564b6ef026cc in do_server_recv (argv=<optimized out>, argc=<optimized out>, f_out=1, f_in=0) at main.c:1219
#19 start_server (f_in=f_in@entry=0, f_out=f_out@entry=1, argc=<optimized out>, argv=<optimized out>) at main.c:1253
#20 0x0000564b6ef0281b in child_main (argc=<optimized out>, argv=<optimized out>) at main.c:1226
#21 0x0000564b6ef232b9 in local_child (argc=2, argv=argv@entry=0x7ffcd48df490, f_in=f_in@entry=0x7ffcd48df3f0, f_out=f_out@entry=0x7ffcd48df3f4, child_main=child_main@entry=0x564b6ef02800 <child_main>) at pipe.c:166
#22 0x0000564b6eee21d2 in do_cmd (f_out_p=0x7ffcd48df3f4, f_in_p=0x7ffcd48df3f0, remote_argc=<optimized out>, remote_argv=<optimized out>, user=0x0, machine=<optimized out>, cmd=<optimized out>) at main.c:651
#23 start_client (argv=<optimized out>, argc=1) at main.c:1569
#24 main (argc=<optimized out>, argv=<optimized out>) at main.c:1812

        ============== Backtrace #3 ==============

#0  0x00007f69b109b74d in __GI___select (nfds=nfds@entry=2, readfds=readfds@entry=0x7ffcd48d7cd0, writefds=writefds@entry=0x7ffcd48d7dd0, exceptfds=exceptfds@entry=0x7ffcd48d7d50, timeout=timeout@entry=0x7ffcd48d7ca0)
    at ../sysdeps/unix/sysv/linux/select.c:69
#1  0x0000564b6ef0f544 in perform_io (needed=76, flags=flags@entry=4) at io.c:741
#2  0x0000564b6ef1070a in send_msg (code=code@entry=MSG_INFO, buf=buf@entry=0x7ffcd48d87e0 "[generator] expand file_list pointer array to 524288 bytes, did move\n", len=len@entry=69, convert=<optimized out>) at io.c:966
#3  0x0000564b6ef05d63 in rwrite (code=<optimized out>, code@entry=FCLIENT, buf=buf@entry=0x7ffcd48d87e0 "[generator] expand file_list pointer array to 524288 bytes, did move\n", len=69, is_utf8=<optimized out>, is_utf8@entry=0) at log.c:339
#4  0x0000564b6ef063e5 in rprintf (code=code@entry=FCLIENT, format=format@entry=0x564b6ef3f368 "[%s] expand file_list pointer array to %s bytes, did%s move\n") at log.c:442
#5  0x0000564b6eee4445 in flist_expand (extra=<optimized out>, flist=0x564b6f41b3e0) at flist.c:309
#6  flist_expand (flist=0x564b6f41b3e0, extra=<optimized out>) at flist.c:287
#7  0x0000564b6eeec9bd in recv_file_list (f=3, dir_ndx=dir_ndx@entry=2960) at flist.c:2584
#8  0x0000564b6ef13243 in wait_for_receiver () at io.c:1699
#9  0x0000564b6ef0f934 in wait_for_receiver () at io.c:1677
#10 perform_io (needed=89, flags=flags@entry=4) at io.c:862
#11 0x0000564b6ef1070a in send_msg (code=code@entry=MSG_INFO, buf=buf@entry=0x7ffcd48da8d0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0 is uptodate\n", len=len@entry=82, convert=<optimized out>) at io.c:966
#12 0x0000564b6ef05d63 in rwrite (code=<optimized out>, code@entry=FCLIENT, buf=buf@entry=0x7ffcd48da8d0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0 is uptodate\n", len=82, is_utf8=<optimized out>, is_utf8@entry=0) at log.c:339
#13 0x0000564b6ef063e5 in rprintf (code=code@entry=FCLIENT, format=format@entry=0x564b6ef3fba5 "%s is uptodate\n") at log.c:442
#14 0x0000564b6eeee433 in set_file_attrs (fname=fname@entry=0x7ffcd48de1c0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0", file=file@entry=0x564b702ccae8, sxp=<optimized out>, sxp@entry=0x7ffcd48dc010,
    fnamecmp=fnamecmp@entry=0x0, flags=<optimized out>) at rsync.c:661
#15 0x0000564b6eef3b78 in recv_generator (fname=fname@entry=0x7ffcd48de1c0 ".config/_some_application__that_I_use_/Cache/Cache_Data/7543a11227262604_0", file=file@entry=0x564b702ccae8, ndx=44459, itemizing=itemizing@entry=1, code=code@entry=FLOG,
    f_out=f_out@entry=1) at generator.c:1805
#16 0x0000564b6eef4c95 in generate_files (f_out=f_out@entry=1, local_name=local_name@entry=0x0) at generator.c:2318
#17 0x0000564b6ef01f7c in do_recv (f_in=<optimized out>, f_in@entry=0, f_out=f_out@entry=1, local_name=local_name@entry=0x0) at main.c:1106
#18 0x0000564b6ef026cc in do_server_recv (argv=<optimized out>, argc=<optimized out>, f_out=1, f_in=0) at main.c:1219
#19 start_server (f_in=f_in@entry=0, f_out=f_out@entry=1, argc=<optimized out>, argv=<optimized out>) at main.c:1253
#20 0x0000564b6ef0281b in child_main (argc=<optimized out>, argv=<optimized out>) at main.c:1226
#21 0x0000564b6ef232b9 in local_child (argc=2, argv=argv@entry=0x7ffcd48df490, f_in=f_in@entry=0x7ffcd48df3f0, f_out=f_out@entry=0x7ffcd48df3f4, child_main=child_main@entry=0x564b6ef02800 <child_main>) at pipe.c:166
#22 0x0000564b6eee21d2 in do_cmd (f_out_p=0x7ffcd48df3f4, f_in_p=0x7ffcd48df3f0, remote_argc=<optimized out>, remote_argv=<optimized out>, user=0x0, machine=<optimized out>, cmd=<optimized out>) at main.c:651
#23 start_client (argv=<optimized out>, argc=1) at main.c:1569
#24 main (argc=<optimized out>, argv=<optimized out>) at main.c:1812

===
Notice that the expected error message is being printed in Stack Backtrace #2, and that recv_file_list() and flist_expand() are on that stack.
These rsync instances were being invoked with, more or less, the following command line options, the way I had rsnapshot configured to invoke it:
rsync -av --delete-during --archive --verbose --one-file-system --hard-links --xattrs --sparse src_dir dest_dir

I will see if I can make further progress debugging this, and make another post here if I do.
Answer: I have a large directory with 41621 directory entries.
When doing an rsync copy of that directory to another local disk, as described above in my previous long comment a few days ago, the rsync process known within rsync as the "sender" reads that directory, about 819 entries at a time, and sends what it sees to another rsync process known within rsync as the "receiver".
It should take the sender about 50 getdents64() system calls to read 41621 directory entries, 819 entries at a time.
After examining each of the 819 directory entries returned to it in typically 2 or 3 getdents64() system calls, the sender then sends a nearly 64 kbyte compressed body of information about those directory entries to the receiver.
The sender sends this data by writing almost 64 kbytes at a time over a socket to the receiver.  The sender sends almost 65536 (2^16) bytes at a time over a local socket to the receiver, and the receiver reads almost 32768 bytes at a time.
Fairly often the receiver will handle the big write from the sender using 3 reads, the first two of which almost, but not quite, read what was just send, and a 3rd read for just the few final bytes.
The above processing of my big directory does not quite complete however.
After the sender has read the results of 45th and 46th getdents64() calls and sent them over to the receiver, the receiver fails to read most of that,
and instead only reads just a few bytes of the nearly 64k bytes just sent it.  Then the receiver, as usual, invokes pselect6() to see if there is more for it read from the sender, and the kernel does not respond to that pselect6(), until its 60 second timer expires ... indicating that there is nothing more for the receiver to read on that socket ... this despite the sender having just sent it another nearly 64k byte packet.
That leaves a bunch of unread data on that socket, and then when the sender is ready to send what it learned from its 47th getdent64() call on that large directory, it first, as usual, checks using pselect6() on its socket to the receiver whether or not there is space for it to write more, and that pselect6() also blocks until the (same duration) 60 second timeout, indicating to the sender that that socket is too full of existing data, and can't accept more until the task reading it reads some data from it.
We then end up with both the sender and the receiver tasks (as well as the third rsync task, known as the "generator", which doesn't appear to be involved in this hang) all looping on pselect6() 60 second timeouts, all being told there is nothing to read, and no room to send any more writes, on the various sockets they share.
The rsync message that (if the verbose option is set) appears just before this interruptible hang:
"[sender] expand file_list pointer array"
appears now to me to be incidental to this hang.  That same message appears several times in the long rsync run that I am using to recreate this problem.  It indicates that rsync has chosen to expand the size of its dynamically allocated "file_list pointer array", perhaps because it needed a bigger buffer, in this case, to handle this huge directory.  I see no indication that this message is a sign of the problem
causing the hang, that occurs a few getdents64() calls later in the session.
The above analysis comes from spending a few hours, over a couple of days, examining the 200 Mbyte 'strace' output from running the rsnapshot job that invoked this rsync task.  I've been expert in strace and the underlying ptrace system call in Unix/Linux, for over 40 years.
===
P.S. -- I've further studied the strace output, as well as the corresponding rsync source code (where 'len' is set, just before the read() system call, in the perform_io() routine in io.c), and so far as I can tell now, the FIRST sign of anything amiss is a failure of the pselect6() call by the recipient to return results showing that there is data available to be read on the recipient's fd == 0, which data was placed into the socket feeding that fd, immediately before, by the sender.
So my current working hypothesis is that this is a kernel bug.
That the kernel returns (at least as seen by ptrace) errno ERESTARTNOHAND to all three (sender, recipient and generator) rsync processes, when I send them a SIGINT, after they've hung, is another sign of a kernel "weirdness" that has long been noticed, but unresolved.  Perhaps this new suspicious pselect6() failure to report available data to read is related to that long standing ERESTARTNOHAND weirdness.  Perhaps not.
Years ago, I would have debugged this suspect kernel behaviour myself; but I no longer have that skill set, so will have to see if I can produce a quality C program that displays the problem (if my current working hypothesis holds up).
===
P.P.S. -- Aha - the 3rd rsync process, the one called the 'generator' ALSO stalled out on a pselect6() call, just like the 'receiver' process did.
And BOTH of them stalled out, hanging on a pselect6() they should not have hung on, just after doing the SECOND call to expand their buffer, and hence just after issuing their SECOND associated 'expand file_list pointer array' verbose message.
Note also that the original poster to this thread reported he was also backing up a huge directory ... he gave the same in total space taken by the files in that directory, but I would not be surprised, if as someone else noted above, it mattered more how many directory entries were in that directory.
P.P.P.S -- The following little shell script reproduces the problem.  Having double (or more I presume) verbose flags (-vv) on rsync in this case results in a hang, whereas just a single verbose (-v) flag succeeds fine.
#!/bin/sh

# build two big test directories
mkdir -p bigdirs/1 bigdirs/2 newdirs
cd ./bigdirs/1
seq 55555 | xargs touch
cd ../2
seq 55555 | xargs touch
cd ../..

# successfuly rsync the test, with single verbose rsync
echo invoking rsync with single verbose - should succeed quickly.
rsync -a -v bigdirs/ newdirs 1>single_verbose_fd1.out 2>single_verbose_fd2.out
ls -lt single_verbose_fd1.out single_verbose_fd2.out

# hang trying same rsync with double verbose
echo
echo invoking double verbose rsync and expecting it to hang
echo interrupt with control-C or such to end the hang
rsync -a -vv bigdirs/ newdirs 1>double_verbose_fd1.out 2>double_verbose_fd2.out

Well ... I went to file a bug against rsync for the above hang, and almost the first thing they said at https://rsync.samba.org/issues.html was "don't use more than one verbose flag".  It fills up the communication lines between the various rsync tasks.  I agree, it does.  That's why I wrote the above little test case showing a hang with two or more verbose flags ... I was seeing the communication sockets between the rsync tasks filling up with all the verbose messages and then blocking, causing the hang.
So I may still file a bug at https://rsync.samba.org/issues.html, but I might not get much sympathy from them .
--
U.V. [Ultimum Verbum, the last word]
My above test case demonstrates a hang using the rsync version 3.2.3 that is in the Ubuntu version 22.04.1 LTS (Jammy Jellyfish) that I am running.
However with the latest rsync release of 3.2.7, downloaded and rebuilt from https://rsync.samba.org, this test case no longer fails.  The hang (at least the one the above shell script demonstrates) has been fixed.
Thank-you, rsync :).
I will file no bug reports.


Como fazer para estilizar a tag

Question: Como faço para estilizar a tag select, como aquela seta que fica no final do campo. As pseudo-classes :hover, :focus, funcionam com este elemento?
Comment: [Relacionado](http://pt.stackoverflow.com/q/46034/4808).
Answer: Experimente isso:

   <select>
      <option>Aqui a primeira opção</option>
      <option>E aqui a segunda opção</option>
   </select>

Resultado:

Exemplo no Fiddle com as Pseudo-Classes
As pseudo-classes no geral funcionam igual para a maioria dos elementos e sim funciona com o elemento .

Pseudo-classes dinâmicas controlam os estados dos elementos. Abaixo,
  vão alguns deles:

:hover – quando passamos o mouse em cima do elemento.
:active – quando ativamos o elemento. Por exemplo, quando clicamos em um link e não soltamos o botão do mouse. Nesse momento, estamos ativando a ação do elemento. Esse estado é ativado também quando navegamos pelos links pelo teclado utilizando o TAB. Este estado não há em todos os elementos.
:visited – quando o link é visitado.
:focus – quando um elemento recebe foco. Muito utilizado em campos de texto. Quando clicamos em cima um campo de texto para escrever, o
    elemento está ganhando foco.

Pseudo-classes estruturais servem para selecionarmos um elemento da
  estrutura do código. Existem várias, por exemplo:

:first-child – seleciona o primeiro filho de um outro elemento.
:last-child – seleciona o último filho de um elemento.
:root – representa um elemento que é a raiz do documento. No HTML 4, é sempre a tag HTML.
:nth-child() – permite que selecionemos qualquer elemento no meio de um grupo de elementos. Por exemplo, você pode selecionar linhas de uma tabela. Assim, podemos fazer uma tabela zebrada, sem a ajuda de javascript. Há variações dessa pseudo-classe para podermos pegar os elementos de baixo para cima (:nth-last-child) e assim por diante.
:lang() – seleciona elementos que tem o atributo lang com um valor específico.

Fonte CSS Select: http://bavotasan.com/2011/style-select-box-using-only-css/
Fonte Pseudo-Classes: http://tableless.com.br/pseudo-classes-css/
Comment: Uma boa alternativa. No final, as mudanças não estão sendo feito a própia cetinha, mas sim, ela está sendo trocado por outra imagem.
Comment: Eu uso isto ai em algumas de minhas aplicações, realmente foi a única maneira que encontrei.
Answer: A tag <select> não se inclui nos elementos disponíveis para estilizar. Nesse caso, as pseudo-classes :hover, :focus, etc. também não irão funcionar.
A melhor maneira para 'modificar' essa setinha (e por se, o resto do elemento), seria trocá-la com uma que você mesmo fabricou. Assim sendo, poderá visualizá-la por meio do JavaScript e/ou CSS.
Answer: Há alguns componentes nativos dos browser possuem comportamentos que não podem ser editados via CSS.
Se isto for realmente necessário, existe plugins em Javascript que trocam os componentes nativos para códigos HTML que podem ser formatados.
Neste link você pode escolher algum que lhe atenda.
Cuidado com essas personalizações, criar aplicativos dependentes do Javascript pode ser um tiro no pé. Procure sempre componentes que não impossibilite a utilização caso o Javascript esteja desabilitado.
Alguns componentes tambem fornecem suporte a WAI ARIA que são regras aplicadas ao HTML para aumentar a acessibilidade do seu código. Essas regras apesar de serem tratadas na maioria das vezes como recursos para deficientes visuais, são recursos excelentes para que os usuários consigam utilizar por exemplo dentro do carro utilizando o comando de voz ou auxilam robos de busca como o GoogleBot a identificar e indexar corretamente o tipo de conteúdo.
Outra coisa que deve ter em mente é o suporte para mobile, alguns destes componentes não ficam usuais em dispositivos móveis. Para contonar este problema, você pode optar por escolher componentes voltados para frameworks CSS, como o Twitter Bootstrap ou Fundation, ou mesmo o jQuery Mobile.

Como fazer para estilizar a tag


Determining all the maximal linear subspace of a quadric

Question: $\newcommand\P{\mathbb P} \newcommand\R{\mathbb R}$Is there an algorithm to parameterically determine all the maximal linear subspaces of a regular quadric hypersurface in some projective real space? I know how to compute the dimension of this maximal subspace, but I'm not sure how to find ALL subspaces with maximal dimensions. For a real one-sheeted hyperboloid in $\P^3(\R)$ I think I can easily get the family of lines parametrically (parameterized say by the points on the hyperboloid). But what about higher dimensional quadrics? For instance if I have a quadric defined by
$$x_0^2+x_1^2+x_2^2 - x_3^2 - x_4^2 - x_5^2 $$
in $\P^5(\R)$. What are all the maximal linear subspaces. I know the obvious ones e.g. the intersection of hyperplanes $x_0=x_3, x_1=x_4$ and $x_2=x_5$ will give me maximal dimensional linear subspace ($\dim= 2$) and permutations of these i.e. $x_0=y_0, x_1=y_1, x_2=y_2$ where $(y_0,y_1,y_2)$ is any permutation of $(x_3,x_4,x_5)$. But obviously there are more linear subspace with maximal dimension. Could they all be obtained via some orthogonal transformation of the ones I just obtained?. Can I already write these transformations parameterized by say the points in the quadric? Through any point $P$ of the quadric is it guaranteed that I can find a maximal linear space that passes it and are they finitely many? This was just an example, what if I increase the dimension (as long as the quadric remains regular)?
Answer: Over a field $k$ of characteristic not 2, quadrics in $\Bbb P^n_k$ exactly correspond to quadratic forms on $k^{n+1}$. Under this correspondence, maximal linear subspaces of the quadric correspond exactly to maximal isotropic subspaces of the quadratic form. As we're over $\Bbb R$, every quadratic form is equivalent by diagonalization to one with $r$ copies of $1$ on the diagonal, $s$ copies of $-1$ on the diagonal, and $t$ copies of $0$ on the diagonal. Maximal isotropic subspaces exist, and all maximal isotropic subspaces are of dimension $\min(r,s)+t$ (this is all standard linear algebra).
To find these subspaces, we pick a $q=\min(r,s)$-dimensional subspace embedded in the direct sum of the $1$ and $-1$ eigenspaces for our matrix which has a basis of the following form: $v_1\oplus w_1,\cdots,v_q\oplus w_q$ where the $v_i$ and $w_i$ are all mutually orthogonal and $|v_i|=|w_i|$. We also throw in the entire $0$-eigenspace. This will give us many choices, all of which can be obtained from each other by the action of the indefinite orthogonal group $O(r,s)$ acting on the direct sum of the $1$ and $-1$ eigenspaces.
There does exist a maximal linear subspace through every real point (though sometimes it's just a point, or your quadric has no real points). Why? If you have a point given by $[x_0:\cdots,x_n]$, the span of the vector $(x_0,\cdots,x_n)$ defines an isotropic subspace, which must be contained in a maximal isotropic subspace. There may be infinitely many such subspaces - consider the point $p=[0:0:1:0:0:1]$ in your example. Then the projectivization of the subspace $V_\theta = \operatorname{Span} \{ (0,0,1,0,0,1), (\cos(\theta),\sin(\theta),0,0,1,0), (-\sin(\theta),\cos(\theta),0,1,0,0)\}$ passes through $p$ for any $\theta\in\Bbb R$.
Comment: by maximal I meant that they have the maximum possible dimension for a linear space in the quadric. So for the specific example, by maximal I mean $\dim = 2$. So, for the example why would the isotropic subspace spanned by a point live in an isotropic subspace of dimension 3 (projectivization dim = 2)? Is this a known fact? For $p$ you showed this is ok. But I think I can do it for any other point.
Comment: Yes, that's what I'm telling you. The maximal linear subspaces in the quartic are exactly the projectivizations of the maximal isotropic subspaces for the quadratic form. Every isotropic subspace is contained in a maximal isotropic subspace. This is what being a maximal isotropic subspace means.
Answer: I am going to answer my question at least from the given example. That was an example of a nonsingular quadric of even dimension (the ambient space is odd-dimensional) such that the positive and negative signatures are equal. In the particular example we had a quadric $X$ defined by
$$x_0^2+x_1^2 + x_2^2 - x_3^2-x_4^2-x_5^2 = 0$$
All maximal linear subspaces of this quadric are of the form
$$\{\langle(\vec v_i : A\vec v_i) : i=0\dots 3\rangle : \vec v_1,\vec v_2, \vec v_3 \text{ spans } \mathbb R^3, A\in O(3)\}$$
I leave the proof of this as an exercise.
This also answers : Every point $P\in X$ lies in a maximal linear subspace of $X$. To get this, we write $P$ as $(\vec v: A\vec v)$ for some orthogonal $A\in O(3)$ (recall that the point is in the quadric iff the norm from the first three coordinates has the same as the norm of the last three coordinates iff there is an orthogonal matrix transforming the first three coordinates to the last three coordinates), were $\vec v$ cannot be the zero vector. Then we just extend $\vec v$ so that it spans $\mathbb R^3$ i.e. if we set $\vec v_1 := \vec v$ and find $\vec v_2,\vec v_3$ such that the $\vec v_i$s spans $\mathbb R^3$ then the maximal linear space
$$\langle (\vec v_i : A\vec v_i) : i=1,\dots, 3\rangle$$
lies in $X$ and $P$ is in it as well.
For the more sophisticated case when there are $0$ eigenvalues for the quadratic form or when the positive and negative signatures ($p$ and $q$ resp.) are unequal we deal with indefinite orthogonal group $O(p,q)$ as explained in KReisers answer. I could write a more explicit answer if necessary.


Is it possible to squash all changes from a branch in TFS into one commit?

Question: For personal projects I use Git for SCM, but at work we use TFS.  One thing that I like about Git is that it allows a person to easily squash all the changes made in a branch when pulling the changes back into the master branch.
The benefit of this is that if their is anything wrong with the changes they can easily be removed be reverting back to the version before the merge even if no tag was created.
Is this. or the equivalent possible in TFS?  Is this where shelving changes fits into the equation?
Thanks.
Answer: Performing a merge in TFS results in a single commit; all of the changes from the source branch get rolled up into one changeset pended against the target branch. So as far as I understand your question, the answer is "yes".


Using Datetimepicker using javascript alert message showing up blank

Question: I am using the bootstrap datetimepicker and trying to save the date
when the alert on the page pops up it is showing up as blank.
Please help, if more information is needed please let me know.
Both lines of code are on the same html.erb page
This is the script that I am using to set the date

  <input type='text' data-date-format="MM/DD/YYYY" class="form-control" />

script I am trying to use to store the data

initialize datetimepicker
   $(".datetimepicker1").datetimepicker({
   endDate: new Date,
   format: "yyyy-mm-dd",
   autoclose: true,
    minViewMode: 1,
  todayBtn: "linked"
   });

Comment:  This is what I am using to capture the data
Comment: how do you initialize datetimepicker ?
Comment: $(".datetimepicker1").datetimepicker({
  endDate: new Date,
  format: "yyyy-mm-dd",
  autoclose: true,
  minViewMode: 1,
  todayBtn: "linked"
});
Comment: you're trying to alert the date value on change ?
Comment: I want to store the data in javascript so that I can move it to a database is my final goal  isn't this jsfiddle pulling the data into a string?http://jsfiddle.net/HFuDg/313/
Comment: how do you want to save it to a database? via Ajax or form submit?
Answer: From whatever I could understand, you probably are looking for this!
$(document).ready(function (){
$('#myDate').datepicker({  format: 'yyyy-mm-dd'
});

$('#myDate').on('change', function() {
   var x = $('#myDate').val();
   //alert("value of date is "+ x);
  //do some ajax call and put/post the date like,
        $.ajax({
                type: 'post',
                url: 'http://yourUrl',
                data: {"date": x}, //in case String is to be sent!
                success: function(x) {
                //do some stuff on success, you can go with callback implementation too
                 console.log(x);
                },
                error: function(err){
                    console.log(err);
                }
       })
});
});

Comment: Okay, so pretty much I need to capture this information through Ajax and then I can use some other stuff to get it into my database? With javascript it would capture the data when the form submitted? Thank you for all your help.


Probability of a draw between at least two options in an election with n voters and m options

Question: I hope that this question has not been asked before, but i couldn't find an answer to it online or by myself. I came by that question during a project of mine:
If i have an election with n voters and m possible choices, and the choices are equally likely selected by each voter, what would be the probability of at least 2 options receiving the same amount of votes (which is higher than the amount of votes for the other options, hence a shared majority between two or more choices)?
I assumed this would be just combinatorics, but couldn't solve it by myself.
Any insights are greatly appreciated. Thanks in advance, and again sorry if this has been asked before.
Answer: Each candidate has probability $1/m$ of being voted for by any voter, a constant. However, each candidate's total vote count is not independent of the other candidates' vote counts, because each voter can only vote once. If a voter votes for candidate $A$, the vote cannot also be cast for candidate $B.$ This is a multinomial distribution, and the joint probability that each candidate $i$ will have $y_i$ votes is given by
\begin{align*}
p(y_1,y_2,\dots,y_m)
&=\frac{n!}{y_1!\,y_2!\cdots y_m!}\,(1/m)^{y_1}(1/m)^{y_2}\cdots(1/m)^{y_m}\\
&=\frac{n!}{y_1!\,y_2!\cdots y_m!}\,\frac{1}{m^n},
\end{align*}
since $\sum y_i=n.$ But if we assume that $n\gg m,$ we can examine each candidate's distribution as approximately binomial (and independent of the other candidates' distributions). This would enable us to use order statistics to answer the question. Under this approximation, a single candidate's distribution would be
$$f(y_i)=\binom{n}{y_i}(1/m)^{y_i}(1-1/m)^{n-y_i}.$$
Order statistics with discrete distributions are somewhat annoying - the analysis is quite different from continuous distributions. The wiki page has a good section on how to discover the distribution of the maximum order statistic. Following that procedure, we define:
\begin{align*}
p_1&=P(Y<y)\\
&=F(y)-f(y)\\
p_2&=P(Y=y)\\
&=f(y)\\
p_3&=P(Y>y)\\
&=1-F(y).
\end{align*}
Note that
$$F(y):=\sum_{x=0}^{y}\binom{n}{x}(1/m)^x(1-1/m)^{n-x}.$$
This has rather messy closed-form expression involving the regularized hypergeometric function:
$$F(y)=\left(\frac{m-1}{m}\right)^{\!\!n}\left(\!\left(\frac{m}{m-1}\right)^{\!\!n}-(m-1)^{-y-1}(y+1)!\binom{n}{y+1}\,_2\tilde{F}_1\left(1,-n+y+1;y+2;\frac{1}{1-m}\right)\!\right).$$
In any case, we compute the next three quantities regarding
$Y_{(m)}:=\max(Y_1,Y_2,\dots,Y_m)$ as
\begin{align*}
P(Y_{(m)}\le y)&=\sum_{j=0}^{n-m}\binom{n}{j}p_3^j(p_1+p_2)^{n-j}\\
P(Y_{(m)}<y)&=\sum_{j=0}^{n-m}\binom{n}{j}(p_2+p_3)^jp_1^{n-j}\\
P(Y_{(m)}=y)&=P(Y_{(n)}\le y)-P(Y_{(n)}<y)\\
&=\sum_{j=0}^{n-m}\binom{n}{j}p_3^j(p_1+p_2)^{n-j}-\sum_{j=0}^{n-m}\binom{n}{j}(p_2+p_3)^jp_1^{n-j}.
\end{align*}
To finish up, we note that the $(m-1)$th order statistic is going to be very similar - we can approximate it as the same probability as what we just calculated. We wind up with:
$$P=\left[\sum_{j=0}^{n-m}\left(\binom{n}{j}\left(p_3^j(p_1+p_2)^{n-j}-(p_2+p_3)^jp_1^{n-j}\right)\right)\right]^2.$$
Comment: Fair enough.  But when you make an approximation, it's a good idea to be explicit about the conditions under which it might apply and, if possible, to provide error estimates or bounds.
Comment: The reasons I don't believe your answer are (1) it shouldn't be a perfect square and (2) in any case $m\ge n \ge 2$ it yields the wrong value. For instance, with $m=n=2$ there is only one term in the sum where $j=n-j=0,$ whence the sum equals $0,$ so you are trying to tell us $P=0.$ But enumeration of the four possible voting patterns shows $P$ must be $1/2.$
Comment: I would start by recognizing the hypergeometric function as being both (a) an incomplete Beta integral and (b) a Binomial CDF.  The latter can be approximated with the Normal CDF.
Comment: @whuber No doubt you are right, but the special cases you mention aren't anywhere near my approximation assumptions. I have assumed that $n\gg m,$ and that the second-largest order statistic is distributed approximately the same as the maximum order statistic.
Comment: My stats chops aren't up to providing error estimates, I don't think. Do you have any ideas for bounding the error on my estimate?
Comment: @whuber: I have a couple newer questions: https://stats.stackexchange.com/questions/540162/uniformly-most-powerful-test-for-weibull-distribution and https://stats.stackexchange.com/questions/541296/binomial-distribution-likelihood-ratio-test-for-equality-of-several-proportions. I was dearly hoping you'd take a look at them, please?


Enviar valores ao banco de dados

Question: Podem me ajudar a enviar os valores dos campos para o banco de dados.
Para o visualizador, Estou utilizando o seguinte código:
Código da página:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

?>

                    <?php echo $pagetitle; ?>
                    <?php echo $breadcrumb; ?>

                                    <form method="get" action="<?= base_url()?>admin/usuarios">

<?php echo anchor('admin/notas/adicionar', ' '. 'Adicionar Nota', array('class' => 'btn btn-block btn-primary btn-flat')); ?>

                                    </form>

                                    <form action="<?= base_url()?>admin/notas/salvar" id="" method="post">
                                        <?php include 'notas.php';?>

                                                <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-primary btn-flat', 'content' => lang('actions_submit'))); ?>
                                                <?php echo form_button(array('type' => 'reset', 'class' => 'btn btn-warning btn-flat', 'content' => lang('actions_reset'))); ?>
                                                <?php echo anchor('admin/dashboard', lang('actions_cancel'), array('class' => 'btn btn-default btn-flat')); ?>

                                    </form>

Código php:
<?php

    $classActive = "";
    $divMenu = "";
    $divPanel = "";

    $this->db->order_by('id', 'asc');
    $this->db->where('usuario_id', $this->session->userdata('id'));
    $this->db->where('usuario_nome', $this->session->userdata('usuario_nome'));
    $nota = $this->db->get('nota')->result_array();
    $contador = 0;
    foreach ($nota as $row) {
        $classActive .= ($contador == 0) ? "active" : "inactive";
        //$divMenu = "
" . $row['titulo'] . "
";
        $divPanel .= "

                        <input type=\"text\" class=\"form-control\" rows=\"14\" name=\"id\" placeholder=\"Título\" value=\"" . $row['id'] . "\">
                        <input type=\"text\" class=\"form-control\" rows=\"14\" style=\"padding: 5px; border:0px; background-color: #fff6cc; font-size: 18px;\" name=\"titulo\" placeholder=\"Título\" value=\"" . $row['titulo'] . "\">

                        <textarea maxlength=\"60\" class=\"ruledpaper form-control\" rows=\"\" cols=\"\" style=\"padding: 5px; border:0px; min-height: 350px;\" name=\"nota\" value=\"" . $row['nota'] ."\" placeholder=\"Digite o texto...\">" . $row['nota'] . "</textarea>

";
        $contador++;
    }
?>

            <?php echo $divPanel; ?>

        <?php foreach ($nota as $row){?>

                <a href="#<?php echo $row['id'];?>" data-toggle="tab">  <?php echo $row['titulo'];?>

        <?php }?>

Controlador
 $id = $this->input->post('id');
 $data['id']                       =   $id;
 $data['titulo']                   =   $this->input->post('titulo');
 $data['nota']                     =   $this->input->post('nota');
 $data['usuario_nome']             =   $this->session->userdata('nome_usuario');
 $data['usuario_id']               =   $this->session->userdata('id');
 $data['dt_alteracao'] =  strtotime(date("d-m-Y H:i:s"));

 $this->db->where('id',$id);

 if($this->db->update('nota', $data))

Porém, está sendo enviado para o banco de dados, sempre o ultimo ID.
UPDATE `nota` SET `id` = '4', `titulo` = 'NOTA 4', `nota` = 'TEXTO NOTA 4',
  `usuario_nome` = 'Wagner Fillio', `usuario_id` = '1',
  `dt_alteracao` = 1482159904 WHERE `id` = '4'

Sendo que quero enviar somente o ID selecionado.
Comment: Tenta só "UPDATE `nota` SET `titulo` = 'NOTA 4', `nota` = 'TEXTO NOTA 4', `usuario_nome` = 'Wagner Fillio', `usuario_id` = '1', `dt_alteracao` = 1482159904 WHERE `id` = 4"
Comment: Valeu, fiz assim: `UPDATE `nota` SET `titulo` = 'NOTA 4', `nota` = 'TEXTO NOTA 4', `usuario_nome` = 'Wagner Fillio', `usuario_id` = '1', `dt_alteracao` = 1482160549 WHERE `id` = '4'`
Não vai!
Pega somente o ID 4, mesmo se eu passar o ID 2
Comment: O problema deve ser isto: "..SET id = '4',..." apaga esse "id = '4'.... Faz como disse em cima, sem tirar nem pôr nada... ASSIM: "UPDATE nota SET titulo = 'NOTA 4', nota = 'TEXTO NOTA 4', usuario_nome = 'Wagner Fillio', usuario_id =1, dt_alteracao = 1482159904 WHERE id = 4"
Comment: Foi mal, passei desta forma, atualizei o retorno
Comment: A tag form está antes do Include, não sei se existe problema nisto, mas tentei colocar o form aonde estão os campos, mas não passou.
Comment: Tentaste com essa query SQL que pus em cima?
Comment: Sim, da mesma forma e mesmo assim não vai.
Sempre atualiza o ID 4, sendo que estou passando outro ID no post.
Comment: Ha então é isso que está mal, faz: "UPDATE nota SET titulo = 'NOTA 4', nota = 'TEXTO NOTA 4', usuario_nome = 'Wagner Fillio', usuario_id =1, dt_alteracao = 1482159904 WHERE id = ID_QUERES_AQUI"... Essa ultima parte pões o id certo
Comment: Está assim:
Veja como está o controller:
`$id = $this->input->post('id');`
E veja como está o campo:
``
Comment: Sim, depois quando submetes esse form vais ter de agarrar esse id que vem do form e coloca-lo nessa query SQL
Comment: Mas isto já deveria funcionar assim, sendo que name='nome do campo' envia os valores do campo para o controlador.
Está enviando, porem sempre o ID 4, se tivesse 5 registro no banco de dados, estaria enviado o ID 5.
Answer: Faça o seguinte, remova o form da página e faça com que cada nota possua seu próprio form, para fazer isto adicione as tags do form dentro do foreach(), assim quando o usuário clicar para salvar a nota 1 irá salvar o form da nota 1, quando for salvar a nota 2 irá salvar o form da nota 2, desta forma que você criou, você esta dizendo ao código que tudo dentro daquele form é um nota só.
Caso você queira que o sistema salve todas as notas em um único form como está agora, dentro do nome dos inputs deve ser assim:
<input name="nota[]">
<input name="descricao[]">

Desta forma você utilizará um laço for() para realizar o salvamento das informações no banco de dados.
Abraço,

Enviar valores ao banco de dados


Autocorrelation function.

Question: Let $X = (X_t
: t ∈ Z)$ be a $MA(1)$-Process. Define the time series $Y = (Y_t
: t ∈ Z)$
with $Y_t = \mathbb 1_{(X_t > 0)}$. What is the Autocorrelation function of $Y_t$?
Anyone has any idea how to even start solving this?
There was a hint to show $P(U > 0,V > 0) = 1/
4 +
1
/2π\cdot \arcsin(\rho)$ where $U$ annd $V$ are both normally distributed
with mean $0$, same Variance $σ^2$ und correlation $\rho$. I have shown this equality but how does it help me further?
Comment: Are we assuming that the white noise is normally distributed?
Comment: yes it is normally distributed.
Answer: Let $t,s\in\mathbb Z$. Then the correlation of $Y_t$ with $Y_s$ is
$$\rho_{t,s}=\frac{\operatorname{Cov}(Y_t,Y_s)}{\sqrt{\operatorname{Var}(Y_t)\operatorname{Var}(Y_s)}} = \frac{\mathbb E[Y_tY_s]-\mathbb E[Y_t]\mathbb E[Y_s]}{\sqrt{\operatorname{Var}(Y_t)\operatorname{Var}(Y_s)}}. $$
Now
$$\mathbb E[Y_t]=\mathbb E\left[\mathbf 1_{(X_t>0)}\right] = \mathbb P(X_t>0), $$
$$\mathbb E[Y_tY_s] = \mathbb E\left[\mathbf 1_{(X_t>0)}\mathbf 1_{(X_s>0)}\right] =\mathbb P(X_t>0,X_s>0), $$
and
$$\operatorname{Var}(Y_t)=\mathbb E[Y_t^2]- \mathbb E[Y_t]^2 = \mathbb P(X_t>0)(1-\mathbb P(X_t>0)). $$
So
$$\rho_{t,s} = \frac{\mathbb P(X_t>0,X_s>0)-\mathbb P(X_t>0)\mathbb P(X_s>0)}{\mathbb P(X_t>0)(1-\mathbb P(X_t>0))\mathbb P(X_s>0)(1-\mathbb P(X_s>0))} .$$
Using the result from your hint it seems that you should be able to compute this quantity.
Comment: so the end result is $\rho_Y=\frac{2}{\pi}\arcsin(\rho_X)$. Thanks.
Comment: I am getting $-1/4+\frac{1}{2\pi} \arcsin(\rho)$ for the numerator. But what about the denominator?
Comment: I am not sure exactly how you are defining the MA process, but if $X_t=\mu+\varepsilon_t+\theta\varepsilon_{t-1}$ then $\mathbb P(X_t>0) = \mathbb P(\varepsilon_t + \theta\varepsilon_{t-1}>-\mu)$. The sum of independent normal random variables is again a normal random variable, so...
Comment: Oh sorry I have $\theta=0.5$ and $\mu=0$. So I get 1/2 for each of the probabilities in the denominator since it's half of the bell curve right? Sorry if this sounds like a stupid question but I have been solving problems all day long and my brain doesn't wanna think propertly anymore, lol.
Comment: In that case $X_t=\varepsilon_t +\frac12\varepsilon_{t-1}$, so $X_t\sim\mathcal N\left(0,\frac54\sigma^2\right)$ (assuming $\varepsilon_t\sim\mathcal N(0,\sigma^2)$).
Comment: I was just wondering if this was the correlation coefficient. As long as I remember autocorrelation function was $E[X_t X_{t+\tau}]$?
Comment: but I am still getting $1/2$ for each probability. Is that right?
Comment: A normal distribution with mean zero is symmetric, so yes, that would be correct.

Autocorrelation function.


ML: RELU versus ln(1+exp(X))

Question: I wasn't able to find this question in SE, possibly as I am new to ML:
When deciding on an activation function, why is RELU preferred over a function such as ln(1 + exp(x))?  The former has zero sensitivity for negative values and so one might get a "dead" neuron. With ln(1 + exp(x)) at least one wouldn't step into a fully dead area.
Does it have to do with being able to create simpler analytic derivatives for backprop or something?
Answer: There are undoubtedly many way to provide such a justification. The simplest is that a logic gate can be constructed from rectifiers (diodes), but not from sigmoidal functions. Read a bit about logic gates here. From that "coincidence" or if you wish "rediscovery of the wheel," when neurons are joined so that they can duplicate "logic gates" they are also joined in an "efficient" and "robust" fashion. A sigmoidal function is like a rectifier with a back current leak, it will not form a clean logic gate, but rather, a dirty inefficient one. Granted, I have used electronic jargon here. The neural net equivalent language may not be even that accessible.
Comment: OK this makes sense from a "completeness" and "ability to calculate anything" standpoint. I think I had read about this in one of the online books.  But my impression was that machine learning isn't about exact calculations so much as getting a "good enough" local minimum in a short enough time.  Is there maybe also the element here that a RELU is faster to calculate that an exponential and a logarithm? I did get the electronics analogy - nice one, thanks!
Comment: Machine learning is certainly not restricted to logical elements any more than, for example, human speech is. Humans likely took until the late 19$^{th}$ century to develop [Boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra#History). The human provided evolution of neural network elements has been more rapid but not dissimilar, and includes non-binary operations for those neurons, for example, convolution.
Answer: As you mentioned, RELU has the dying problem which we cannot ignore. However, some improved versions like Leaky RELU, Parametric RELU have been invented, and it's no longer been as a considerable disadvantage of RELU.
For more references, you can look at these sites:

Dying ReLU: Causes and Solutions (Leaky ReLU)

A Practical Guide to ReLU

ML: RELU versus ln(1+exp(X))


Ordenar um array dentro de um objeto, entrando em todas as hierarquias do mesmo

Question: Eu possuo um objeto que contem varios arrays dentro dele. E esses arrays contem outros objetos que contem um campo lpec_sequence que eu gostaria que a baixo de cada hierarquia, ele fosse ordenado por esse campo. E tambem gostaria de percorrer por cada sub_menu existente dentro dos objetos e ordena-los tambem.
{
  "layout": {
    "menu_events": [
      {
    "lpec_id": 90,
    "lpec_sequence": 1,
    "sub_menu": false
      },
      {
    "lpec_id": 125,
    "lpec_sequence": 2,
    "sub_menu": false
      },
      {
    "lpec_id": 124,
    "lpec_sequence": 3,
    "sub_menu": true,
    "sub_menu_items": [
      {
        "lpec_id": 96,
        "lpec_sequence": 5,
        "sub_menu": false
      },
      {
        "lpec_id": 97,
        "lpec_sequence": 6,
        "sub_menu": false
      },
      {
        "lpec_id": 98,
        "lpec_sequence": 7,
        "sub_menu": false
      },
      {
        "lpec_id": 99,
        "lpec_sequence": 8,
        "sub_menu": true,
        "sub_menu_items": [
          {
            "lpec_id": 100,
            "lpec_sequence": 1,
            "sub_menu": false
          },
          {
            "lpec_id": 101,
            "lpec_sequence": 2,
            "sub_menu": false
          },
          {
            "lpec_id": 102,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 103,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 104,
            "lpec_sequence": 5,
            "sub_menu": false
          },
          {
            "lpec_id": 105,
            "lpec_sequence": 6,
            "sub_menu": false
          },
          {
            "lpec_id": 106,
            "lpec_sequence": 7,
            "sub_menu": false
          }
        ]
      },
      {
        "lpec_id": 118,
        "lpec_sequence": 10,
        "sub_menu": true,
        "sub_menu_items": [
          {
            "lpec_id": 121,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 122,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 119,
            "lpec_sequence": 1,
            "sub_menu": false
          },
          {
            "lpec_id": 120,
            "lpec_sequence": 2,
            "sub_menu": false
          }
        ]
      },
      {
        "lpec_id": 92,
        "lpec_sequence": 1,
        "sub_menu": false
      },
      {
        "lpec_id": 93,
        "lpec_sequence": 2,
        "sub_menu": false
      },
      {
        "lpec_id": 94,
        "lpec_sequence": 3,
        "sub_menu": false
      },
      {
        "lpec_id": 95,
        "lpec_sequence": 4,
        "sub_menu": false
      },
      {
        "lpec_id": 107,
        "lpec_sequence": 9,
        "sub_menu": true,
        "sub_menu_items": [
          {
            "lpec_id": 109,
            "lpec_sequence": 2,
            "sub_menu": false
          },
          {
            "lpec_id": 110,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 111,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 112,
            "lpec_sequence": 5,
            "sub_menu": false
          },
          {
            "lpec_id": 114,
            "lpec_sequence": 7,
            "sub_menu": false
          },
          {
            "lpec_id": 113,
            "lpec_sequence": 6,
            "sub_menu": false
          },
          {
            "lpec_id": 115,
            "lpec_sequence": 8,
            "sub_menu": false
          },
          {
            "lpec_id": 116,
            "lpec_sequence": 9,
            "sub_menu": false
          },
          {
            "lpec_id": 117,
            "lpec_sequence": 10,
            "sub_menu": false
          },
          {
            "lpec_id": 108,
            "lpec_sequence": 1,
            "sub_menu": false
          }
        ]
      }
    ]
      },
      {
    "lpec_id": 123,
    "lpec_sequence": 2,
    "sub_menu": true,
    "sub_menu_items": [
      {
        "lpec_id": 91,
        "lpec_sequence": 1,
        "sub_menu": false
      }
    ]
      }
    ]
  }
}

Answer: Uma vez que o .sort() muda a array diretamente podes fazer assim:
// ordenar primeiro os sub menus
data.layout.menu_events.forEach(
  event => event.sub_menu_items && event.sub_menu_items.sort((a, b) => a.lpec_id - b.lpec_id)
);

// ordenar os menus com os sub menus já ordenados
data.layout.menu_events.sort(
  (a, b) => a.lpec_sequence - b.lpec_sequence
);

Exemplo:

var data = {
  "layout": {
    "menu_events": [{
        "lpec_id": 90,
        "lpec_sequence": 1,
        "sub_menu": false
      },
      {
        "lpec_id": 125,
        "lpec_sequence": 2,
        "sub_menu": false
      },
      {
        "lpec_id": 124,
        "lpec_sequence": 3,
        "sub_menu": true,
        "sub_menu_items": [{
            "lpec_id": 96,
            "lpec_sequence": 5,
            "sub_menu": false
          },
          {
            "lpec_id": 97,
            "lpec_sequence": 6,
            "sub_menu": false
          },
          {
            "lpec_id": 98,
            "lpec_sequence": 7,
            "sub_menu": false
          },
          {
            "lpec_id": 99,
            "lpec_sequence": 8,
            "sub_menu": true,
            "sub_menu_items": [{
                "lpec_id": 100,
                "lpec_sequence": 1,
                "sub_menu": false
              },
              {
                "lpec_id": 101,
                "lpec_sequence": 2,
                "sub_menu": false
              },
              {
                "lpec_id": 102,
                "lpec_sequence": 3,
                "sub_menu": false
              },
              {
                "lpec_id": 103,
                "lpec_sequence": 4,
                "sub_menu": false
              },
              {
                "lpec_id": 104,
                "lpec_sequence": 5,
                "sub_menu": false
              },
              {
                "lpec_id": 105,
                "lpec_sequence": 6,
                "sub_menu": false
              },
              {
                "lpec_id": 106,
                "lpec_sequence": 7,
                "sub_menu": false
              }
            ]
          },
          {
            "lpec_id": 118,
            "lpec_sequence": 10,
            "sub_menu": true,
            "sub_menu_items": [{
                "lpec_id": 121,
                "lpec_sequence": 3,
                "sub_menu": false
              },
              {
                "lpec_id": 122,
                "lpec_sequence": 4,
                "sub_menu": false
              },
              {
                "lpec_id": 119,
                "lpec_sequence": 1,
                "sub_menu": false
              },
              {
                "lpec_id": 120,
                "lpec_sequence": 2,
                "sub_menu": false
              }
            ]
          },
          {
            "lpec_id": 92,
            "lpec_sequence": 1,
            "sub_menu": false
          },
          {
            "lpec_id": 93,
            "lpec_sequence": 2,
            "sub_menu": false
          },
          {
            "lpec_id": 94,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 95,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 107,
            "lpec_sequence": 9,
            "sub_menu": true,
            "sub_menu_items": [{
                "lpec_id": 109,
                "lpec_sequence": 2,
                "sub_menu": false
              },
              {
                "lpec_id": 110,
                "lpec_sequence": 3,
                "sub_menu": false
              },
              {
                "lpec_id": 111,
                "lpec_sequence": 4,
                "sub_menu": false
              },
              {
                "lpec_id": 112,
                "lpec_sequence": 5,
                "sub_menu": false
              },
              {
                "lpec_id": 114,
                "lpec_sequence": 7,
                "sub_menu": false
              },
              {
                "lpec_id": 113,
                "lpec_sequence": 6,
                "sub_menu": false
              },
              {
                "lpec_id": 115,
                "lpec_sequence": 8,
                "sub_menu": false
              },
              {
                "lpec_id": 116,
                "lpec_sequence": 9,
                "sub_menu": false
              },
              {
                "lpec_id": 117,
                "lpec_sequence": 10,
                "sub_menu": false
              },
              {
                "lpec_id": 108,
                "lpec_sequence": 1,
                "sub_menu": false
              }
            ]
          }
        ]
      },
      {
        "lpec_id": 123,
        "lpec_sequence": 2,
        "sub_menu": true,
        "sub_menu_items": [{
          "lpec_id": 91,
          "lpec_sequence": 1,
          "sub_menu": false
        }]
      }
    ]
  }
}

function sortEfter(obj, key, subKey) {
  if (!obj[subKey]) return;
  obj[subKey] = obj[subKey].sort((a, b) => a[key] - b[key]);
  obj[subKey].forEach(el => sortEfter(el, key, subKey))
}

data.layout.menu_events.forEach(
  event => sortEfter(event, 'lpec_id', 'sub_menu_items')
);

data.layout.menu_events.sort(
  (a, b) => a.lpec_sequence - b.lpec_sequence
);

console.log(data.layout.menu_events);
Comment: Olá, obrigado pela resposta. Mas verifiquei que o objecto com o id `lpec_id: 107` e o `lpec_id: 118` não está ordenado. O que será que acontece ?
Comment: @Jonathan reparei agora que há 3 niveis de profundidade, alguns `sub_menu_items` têm outros `sub_menu_items` dentro. Isso é mesmo assim ou não?
Comment: Sim sim, nesse caso há 3 níveis, mas pode haver 2 ou até mais do que 3
Comment: @Jonathan ok, e os subniveis chamam-se sempre `sub_menu_items` ou há outros nomes?
Comment: Sempre os mesmos.
Comment: @Jonathan dá uma olhada agora.
Comment: Perfeito. Muito Obrigado


how to restore the calendar view created by the calendar module?

Question: When you install the calendar module a calendar view is automatically created. For some reason it seems that I deleted this view. Now I need to use the calendar module and I need this view.
The version of the module is: 7.x-3.4
I can't find a way to make that the calendar module recreate that view. This is what I did:
1) I disabled the calendar module
2) I uninstall the calendar module
3) I enabled the calendar module again
But the view is not created at all.
How can I have this view restored.
Comment: Clear cache and check the views listing page ?
Comment: Drupal 7.15 and Calendar 7.x-3.4
Comment: I'm following this tutorial: http://vimeo.com/26853285 **Is it outdated?**
Answer: it is very simple.
Go to Home » Administration » Structure » Views and add new view from template.
Yo could see in this section different calendar views.
Regards


Deep diving into Rx.ReplaySubject: How to delay `next()`?

Question:
Ignoring Block: Either I'm wrong here, or the whiskey is starting to work. (I don't want to rule out that I'm going stupid either. Sorry for that.)

Expectation:
I would have expected ReplaySubject to return a single value every 2 seconds because I wait two seconds (each time) before I call next().
Result:
The result is that there is a wait of 2 seconds, but then all values are output simultaneously.
The code in question is here:
import { ReplaySubject } from 'rxjs';

export const rs$: ReplaySubject<number> = new ReplaySubject();

rs$.subscribe({
  next: (data) => console.log(data),
  error: (error) => console.warn(error),
  complete: () => console.log('ReplaySubject completed'),
});

const fakeAPIValuesOne: Array<number> = [7, 11, 13];

fakeAPIValuesOne.forEach(async (entry: number) => {
  await wait(2000);  // <--- Why does wait() not work?
  rs$.next(entry);
});

function wait(milliseconds: number) {
  return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

Question:
What am I doing fundamentally wrong here?
If you want to try it out: https://stackblitz.com/edit/rxjs-wpnqvq?file=index.ts
EDIT 1:
SetTimeout also has no effect. The following code does exactly the same as above:
fakeAPIValuesOne.forEach((value: number) => {
  setTimeout(() => {
    rs$.next(value);
  }, 2000);
});

I wonder how next() can override all the delays here?
EDIT 2
The problem is solved, correct answer marked, thank you! You need the following details to run root level awaits for your ts-files.
package.json
Please notice the type section:
{
  "name": "playground",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "start": "nodemon index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "rxjs": "^7.5.5",
    "ts-node": "^10.7.0",
    "typescript": "^4.8.0-dev.20220507"
  },
  "type": "module"
}

nodemon.json
Please consider the following config to avoid the error: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
{
  "execMap": {
    "ts": "node --loader ts-node/esm"
  }
}

Last but not least tsconfig.json
{
  "compilerOptions": {
    "module": "ESNext",
    "target": "ESNext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "isolatedModules": true,
    "noEmit": true,
    "strict": true,
    "lib": ["esnext", "DOM"]
  }
}

Comment: one day after: was definitely not within the peak: https://imgs.xkcd.com/comics/ballmer_peak.png
Answer: Following note is extracted from mozilla web docs here

Note: forEach expects a synchronous function.
forEach does not wait for promises. Make sure you are aware of the
implications while using promises (or async functions) as forEach
callback.

So you this issue isn't related to the ReplaySubject, you just cannot use forEach for this use-case.
Cheers
EDIT: Solved
import { ReplaySubject } from "rxjs";

export const rs$ = new ReplaySubject();

rs$.subscribe({
  next: (data) => console.log(data),
  error: (error) => console.warn(error),
  complete: () => console.log("ReplaySubject completed"),
});

const fakeAPIValuesOne = [7, 11, 13];

// That won't work:
// fakeAPIValuesOne.forEach(async (entry: number) => {
//   await wait(2000);
//   rs$.next(entry);
// });

// That will work
for (const element of fakeAPIValuesOne) {
  await wait(2000);
  rs$.next(element);
}

function wait(milliseconds: number) {
  return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

Answer: I think it's worth mentioning that you shouldn't (in general) expect this to work.
Consider the following:
wait(2000);
wait(2000);
wait(2000);
console.log("Hello");

"Hello" will be printed to the console right away. It's not going to wait 6 seconds.
That doesn't change if you put them in a loop.
for (const n of [1,2,3]) {
  wait(2000);
}
console.log("Hello");

This will also print "Hello" right away.

If you don't use .then() or await, the standard behavior is that you will not wait. Some APIs will (of course) perform an await on your behalf, but the expected behavior is that it won't.
You need to write it like this:
await wait(2000);
await wait(2000);
await wait(2000);
console.log("Hello");

Now you will wait 6 seconds before printing "Hello" to the console.

Now look at this:
const createPromise = async (v) => {
   await wait(2000);
   console.log("Hello From createPromise: ", v);
}

createPromise(1);
createPromise(2);
createPromise(3);
console.log("Hello");

You'll notice this isn't really any different. This will print Hello right away, and then 2 seconds later all the promises will resolve and print
Hello
// 2 seconds wait
Hello From createPromise: 1
Hello From createPromise: 2
Hello From createPromise: 3

You'll notice that none of them wait for each other. That's the expected behavior of a promise. You need to use await or .then in order to actally wait for the result of a promise.
You need to write:
await createPromise(1);
await createPromise(2);
await createPromise(3);
console.log("Hello");

These two loops do the same thing (as expected, they don't wait)
for (const n of [1,2,3]) {
  createPromise(n);
}

[1,2,3].forEach(n => createPromise(n))

console.log("Hello");

Again, the first thing you'll see printed is "Hello" because you never wait for any of these promises to resolve.
You do await the inner wait(2000) promise, but you never await the outer createPromise() promises. So of course you won't wait for them :)
for (const n of [1,2,3]) {
  await createPromise(n);
}

Comment: thank you, yes you're right and I admit it wasn't one of my greatest moments


LibreOffice problems drawing hash marks in shape using Python

Question: I am trying to create a hash mark pattern inside a shape using LibreOffice 5 on Windows 10 using the Python 3.3 that came with LibreOffice. Two thirds of the code is similar to this post with additional questions about creating hash marks at the end of the code listing.
This is the Python code I tried.
import sys
print(sys.version)

import socket
import uno

# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()

# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )

# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()

# Create the shape
def create_shape(document, x, y, width, height, shapeType):
    shape = model.createInstance(shapeType)
    aPoint = uno.createUnoStruct("com.sun.star.awt.Point")
    aPoint.X, aPoint.Y = x, y
    aSize = uno.createUnoStruct("com.sun.star.awt.Size")
    aSize.Width, aSize.Height = width, height
    shape.setPosition(aPoint)
    shape.setSize(aSize)
    return shape

def formatShape(shape):
    shape.setPropertyValue("FillColor", int("FFFFFF", 16))  # blue
    shape.setPropertyValue("LineColor", int("000000", 16))  # black

    aHatch = uno.createUnoStruct("com.sun.star.drawing.Hatch")
    #HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")
    #aHatch.Style=HatchStyle.DOUBLE;
    aHatch.Color=0x00ff00
    aHatch.Distance=100
    aHatch.Angle=450

    shape.setPropertyValue("FillHatch", aHatch)
    shape.setPropertyValue("FillStyle", "FillStyle.DOUBLE")

shape = create_shape(model, 0, 0, 10000, 10000, "com.sun.star.drawing.RectangleShape")
formatShape(shape)

drawPage.add(shape)

This code should set a double crosshatch pattern inside a rectangle but no pattern shows ups inside the rectangle.
aHatch = uno.createUnoStruct("com.sun.star.drawing.Hatch")
#HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")
#aHatch.Style=HatchStyle.DOUBLE;
aHatch.Color=0x00ff00
aHatch.Distance=100
aHatch.Angle=450

shape.setPropertyValue("FillHatch", aHatch)
shape.setPropertyValue("FillStyle", "FillStyle.DOUBLE")

The line to set the hatch style pattern:
uno.RuntimeException: pyuno.getClass:

Fails with the following error
com.sun.star.drawing.HatchStyleis a ENUM, expected EXCEPTION,

Here are some links to Java and BASIC examples I used for reference.
Answer:
HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")

This fails because HatchStyle is an Enum, not a Struct.  To use the HatchStyle enum, follow one of the three ways in the python example from the Enum link.

shape.setPropertyValue("FillStyle", "FillStyle.DOUBLE")

It looks like you are confusing "FillStyle.HATCH" and "HatchStyle.DOUBLE" from the example.  This is what the code should be in Python:
from com.sun.star.drawing.FillStyle import HATCH
shape.setPropertyValue("FillStyle", HATCH)

This seems to be missing as well:
drawPage = model.getDrawPages().getByIndex(0)

Comment: I got horizontal blue hatch marks after adding these two lines:  from com.sun.star.drawing.FillStyle import HATCH
shape.setPropertyValue("FillStyle", HATCH)
Is there something I need to add or change here to modify the color, angle and style of the hatch? aHatch.Color=0x00ff00
aHatch.Distance=100
aHatch.Angle=450
shape.setPropertyValue("FillHatch", aHatch)
Comment: I tested the [Basic example](https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Structure_of_Drawings#Hatches).  On Ubuntu (LO 5.0.5), the example worked as advertised, and the hatch settings could be changed in any way.  However on Windows (LO 5.1.0 and AOO 4.1.2), although setting the `FillStyle` worked, setting `FillHatch` had no effect.  You may want to consider [reporting a bug](https://wiki.documentfoundation.org/QA/BugReport).
Comment: I am developing on a Windows 10 machine and will give the Python code a try on a Linux machine before reporting a bug.  I will also test the BASIC code on both Linux and Windows machines.


jQuery .text() length behavior

Question: I have my HTML coded as follows;

<span<%=Some Java Code%>

Also there is a Javascript code called after document is ready which has the following line;
var welcomeLen = $(".welcomeText span").text().length;

Now if I want to update my HTML code inside li as follows;

<%=Some Java Code%>

i.e. I want to add a new span element with class="firstNameWithEllipses"
The issue that I am facing is that the JS calculation for welcomeLen changes if I add the above HTML code.
I am not quite sure how the text().length works as it returns the following values for the 2 separate cases;
When rendered as
Hello, StudentFname87654 from Functional!

it returns 41
&
When rendered as
Hello, StudentFname87654 from Functional!

it returns 58
How do I ensure that the welcomeLen remains the same even if I add any HTML code inside the span ?
Answer: This is because your selector matches both span elements and returns the text from both. You could have added any element other than a child span and you would not have experienced this issue:
console.log($(".welcomeText span").text());
//Output: Hello, StudentFname87654 from Functional!StudentFname87654

Make your selector more specific. You could, for example, select only span elements that are direct children of an li that is a descendant of .welcomeText:
var welcomeLen = $(".welcomeText li > span").text().length;

Here's a working example.
Update
If you're interested in why this is the case, you can look at the jQuery source for the text method. Like most jQuery methods, it iterates over all of the elements in the matched set:
var ret = "";
jQuery.each( text || this, function(){ //Iterate over matched set
    jQuery.each( this.childNodes, function(){ //Iterate over children of current element
        if ( this.nodeType != 8 )
            ret += this.nodeType != 1 ?
                   this.nodeValue : //Append text to overall string
                   jQuery.fn.text( [ this ] ); //Get text of descendants
    });
});
return ret;

Comment: Thx a lot for that...I did not quite understand how it returns for both spans...like how that calculation happens....But anyways I am actually using the JS code across multiple HTMLs and the new span element is added only for 1 html...So how do I ensure that the existing thing would also work if I update the selector to what you have shown ?
Comment: The selector I have used will work for your original example too. It simply looks for a `span` element that is a child of an `li`, which is the case in both of your examples.
Comment: @testndtv - If you are interested in why it returns the text for both, I've updated my answer.
Comment: Thx a lot...So it is kind of considering the 2 spans seperately and adding for the final result...
Comment: Yeah that's exactly what it does. It loops over all the `span` elements matched by the selector (which is 2), gets the text of each and builds up a string containing the text from all of them.
Answer: var welcomeLen = $(".welcomeText span").not(".firstNameWithEllipses").text().length;

jQuery .text() length behavior


Inverse of a one-to-one and continuous function is continuous

Question: Show that if $f$ is continuous on an interval $[a,b]$ and one-to-one, then $f^{-1}$ is also continuous.
This is from Stephen Abbott's Understanding Analysis, Exercise 4.5.8.
I've read multiple proofs online but I can't seem to understand any.
What I've done so far:
$f$ is continuous, so it preserves connectedness and compactness which implies $f([a,b]) = [\alpha, \beta]$, for some $\alpha, \beta \in \mathbb{R}$.
$f$ is monotone, so we can assume WLOG that $f$ is strictly increasing and so $f^{-1}$ is strictly increasing. We know $f^{-1}$ exists since $f$ is one-to-one.
I've also done a proof which does not require $f$ to be continuous and I've read that continuity of $f$ is not required. Why is this the case? I'm looking for a proof which uses the continuity of $f$.
One more thing I tried:
Lets say we have $(y_n) \to c$, for some $c \in [\alpha, \beta]$, where $(y_n) \subset [\alpha, \beta]$. Lets assume $f^{-1} (y_n)$ does converge and to some $f^{-1} (d) \in [a,b]$, where $f^{-1} (d) \neq f^{-1} (c)$. By continuity of $f$, we have $(y_n) \to d$, where $d \neq c$, by injectiveness. This is a contradiction to our hypothesis. The issue with this proof is that we must first show that $f^{-1} (y_n)$ does indeed converge.
Thank you.
Comment: Why is $f$ monotone?
Comment: Continuous+ injective implies strictly monotone
Comment: It's enough to show $f^{-1}(y_n) \to f^{-1}(c)$ when the $y_n \to c$ are monotone.
Answer: Closed interval $[a, b]$ is compact, so every sequence has a converging subsequence. In our case the limit point is the same for all such subsequences and equal to $f^{-1}(c)$ (just like in your reasoning).
Now it's easy to prove (f.i. by contradiction) that thus the whole sequence converges to this limit.
Comment: Here is the complete proof, skip it if you want to think about it for yourself. We know that every subsequence of our sequence has a further subsequence that convergences to some numer $x$ (by compactness) same for every subsequence (by what I wrote earlier). So let's assume by contradiction, that our sequence doesn't converge to $x$. Then it has to have a subsequence $\{x_{n_k}\}$ and a number $t>0$ such that  $|x_{n_k}-x| > t$ for all $n_k$. But this subsequence has a further subsequence that converges to $x$ (by what I wrote at the beginning), thus the desired contradiction.
Comment: Being one 1-1 and monotone implies continuity. Just notice that the only discontinuity points would be jump discontinuities (by monotonicity), so the bijectivity would fail.
Comment: Although dropping the bijectivity assumtion you could think about constructing $f^{-1}$ defined only on the range of $f$, and it would be continuous too. Would you like to prove it?
Comment: Thank you for the reply. Yes, we're just interested in the range of f. I'm looking at what you said and thinking about it.
Comment: I mean consider f.i. $f(x) = x$ on $[0,1]$ and $f(x) = x+1$ on $(1,2]$. Then $f^{-1}$ would be a function defined only on $[0,1] \cup (2,3]$, but it would still be continuous. Are you interested in such cases as well?
Comment: No, not at the moment.
Comment: Since $f^{-1} (y_n) \in [a,b]$, we have that there is a subsequence $f^{-1} (y_{nk}) \to x'$. By continuity of $f$, we have that $(y_{nk}) \to f(x')$. Since we have $(y_n) \to c$, we have $c = f(x')$ and so $f^{-1} (y_{nk}) \to f^{-1} (c)$. Now I'm thinking on how to continue. Thank you for the hints.
Answer: I learned undergraduate analysis from Abbott as well, so I think the following argument should be something you can follow given you are in chapter 4 of Abbott. It just uses the fact that compact is the same as closed and bounded and the fact that continuous is equivalent to sequentially continuous. I cannot offer a remedy to your proof, but I can offer a proof that you might be able to understand (you mention you can't seem to understand any proofs that you have read) given your position in Abbott's book.
To show that $f^{-1}$ is continuous, we need to show that for any convergent sequence $(y_n)_{n\in\mathbb{N}}$ (converging to, say, $y$) in the domain of $f^{-1}$, we have that $f^{-1}(y_n)$ converges to $f^{-1}(y)$. This is by the sequential characterization of continuity that Abbott gives.
Since $f$ is continuous and its domain is compact, we have that the image of $f$ is also compact, hence closed. So, we have that $y = f(x)$ for some $x$, since $y$ is a limit point of the range of $f$.
Now, note that $(y_n)_{n\in\mathbb{N}}$ can be written as $\big(f(x_n)\big)_{n\in\mathbb{N}}$ for some $x_n$ in the domain of $f$. So we are trying to show that $x_n$ converges to $x$.
So, we have a sequence $(x_n)_{n\in\mathbb{N}}$ in $[a, b]$, which is a compact set. Thus, there is a convergent subsequence $(x_{n_k})_{k\in\mathbb{N}}$ of $(x_n)_{n\in\mathbb{N}}$, who converges to, say, $x'$.
Now, by the continuity of $f$, we have that $f(x_{n_k})$ converges to $f(x')$. However, we have that $\big(f(x_{n_k})\big)_{k\in\mathbb{N}}$ is a subsequence of the convergent sequence $\big(f(x_n)\big)_{n\in\mathbb{N}}$, hence has the same limit. That is, we have that $f(x') = f(x)$. Lastly, we use injectivity of $f$ to conclude the result.
I would suggest reconciling your proof with this proof. Since your proof idea is to use injectivity to conclude strictly increasing, you could probably modify the above proof to say that $f(x') = f(x)$ immediately from the fact that $f$ is strictly increasing. Anyway, it would be a good exercise to try and modify the above proof to fit with your idea.
Comment: Yes, this answer goes in a good direction, toward the general form of the question, which manifestly does not use monotonicity. Namely, $f:X\to Y$ a continuous bijection from compact $X$ to Hausdorff $Y$ has a continuous inverse. Proof: for $U$ open in $X$, its complement $U^c$ is closed, so is compact. The continuous image $f(U^c)$ of a compact is compact in $Y$. In Hausdorff spaces, compact implies closed. Thus, the complement $f(U)$ is open. Done. :)
Comment: Thank you. The main issue though still remains, how do we show the whole sequence $f^{-1} (y_n)$ converges? I was able to do it using Jkbb's method.
Comment: @SonnyJordan: I see your concern. You can conclude that $f^{-1}(y_n)$ converges by proving the fact that a if subsequence of a monotonically increasing sequence converges, then the whole sequence must converge to the same limit as that of the subsequence.


Scheme function for time signatures in LilyPond

Question: I would like to notate a piece proportionally in LilyPond in which different time signatures are layered polymetrically. To do this, I want to place the time signatures above the staves, or more precisely: above the bar lines. And I want to do this with a markup, because the space that the time signatures occupy before the first note (or rest) of a bar naturally remains if I simply move them vertically and horizontally with a \override command (e.g. with \override TimeSignature.extra-offset = #'(-2 . 8)). In short: the first note (or rest) of a bar is always set a little too far to the right than it should be according to a precise proportional notation.
So what I want to do is write a function in which I can insert the time signatures into the notes with a markup command. I'm thinking of something like this:
\version "2.25.2"

\relative c' {
  \override Score.TimeSignature. stencil = ##f
   c4 d e f

 \mark \markup {
    \override #'(baseline-skip . 1.5)
    \override #'(font-size . 4.5)
    \override #'(font-name . "Bodoni72 Book")
    \override #'(whiteout-style . outline)
    \override #'(whiteout . 1)
    \override #'(extra-offset . -2)
    \column {
      \raise #0.5 \scale #'(0.8 . 0.8) "4" % Numerator of the time signature
      \raise #-0.5 \scale #'(0.8 . 0.8) "8" % Denominator of the time signature
    }
  }
  c8 d e f
}

It should be possible to pass numerators and denominators as arguments to such a function.
I am currently learning Scheme, but my knowledge is not yet sufficient to write such a function. Maybe someone can help me?
Answer: First things first: Such a function does exist (at least, a more powerful such function does exist):
\markup\compound-meter #'(4 8)

But if you really wanted to do your own function you'd need to define either a markup command (drawing the actual markup) or a scheme function (returning a markup to be drawn later). The markup command will be resolved later, which allows it to consider properties passed along. Of course you could also do a music function returning the actual mark event. See here how this would be done:
tsMarkup =
#(define-scheme-function (num den) (number? number?)
   #{
     \markup {
      \override #'(baseline-skip . 1.5)
      \override #'(font-size . 4.5)
      \override #'(font-name . "Bodoni72 Book")
      \override #'(whiteout-style . outline)
      \override #'(whiteout . 1)
      \override #'(extra-offset . -2)
      \column {
        \raise #0.5 \scale #'(0.8 . 0.8) #(number->string num) % Numerator of the time signature
        \raise #-0.5 \scale #'(0.8 . 0.8) #(number->string den) % Denominator of the time signature
      }
     }
   #})

#(define-markup-command (timesig layout props num den) (number? number?)
   (interpret-markup
    layout props
    #{
      \markup {
       \override #'(baseline-skip . 1.5)
       \override #'(font-size . 4.5)
       \override #'(font-name . "Bodoni72 Book")
       \override #'(whiteout-style . outline)
       \override #'(whiteout . 1)
       \override #'(extra-offset . -2)
       \column {
         \raise #0.5 \scale #'(0.8 . 0.8) #(number->string num) % Numerator of the time signature
         \raise #-0.5 \scale #'(0.8 . 0.8) #(number->string den) % Denominator of the time signature
       }
      }
    #}))

tsMark =
#(define-music-function (num den) (number? number?)
   #{
     \mark\markup {
      \override #'(baseline-skip . 1.5)
      \override #'(font-size . 4.5)
      \override #'(font-name . "Bodoni72 Book")
      \override #'(whiteout-style . outline)
      \override #'(whiteout . 1)
      \override #'(extra-offset . -2)
      \column {
        \raise #0.5 \scale #'(0.8 . 0.8) #(number->string num) % Numerator of the time signature
        \raise #-0.5 \scale #'(0.8 . 0.8) #(number->string den) % Denominator of the time signature
      }
     }
   #})

\relative c' {
  \override Score.TimeSignature. stencil = ##f
  c4 d e f

  \time 4/8
  \mark\markup\compound-meter #'(4 8)
  c8 d e f
  \mark\markup\timesig #4 #8
  c8 d e f
  \mark \tsMarkup 4 8
  c8 d e f
  \tsMark 4 8
  c8 d e f
}

Generally instead of manually creating these marks it would be fairly simple though to include a custom Time signature engraver creating these marks for you (this might be a good example to understand how engravers work):
#(define (Top_time_signature_engraver context)
   (let* ((cause '()) (last-fraction #f))
     (make-engraver
      (listeners
       ((time-signature-event engraver event)
        (set! cause event)))
      ((process-music engraver)
       (let ((frac (ly:context-property context 'timeSignatureFraction)))
         (if (not (equal? frac last-fraction))
             (let ((grob (ly:engraver-make-grob engraver 'RehearsalMark cause)))
              (ly:grob-set-property! grob
                                     'text
                                     #{
                                       \markup {
                                        \override #'(baseline-skip . 1.5)
                                        \override #'(font-size . 4.5)
                                        \override #'(font-name . "Bodoni72 Book")
                                        \override #'(whiteout-style . outline)
                                        \override #'(whiteout . 1)
                                        \override #'(extra-offset . -2)
                                        \column {
                                          \raise #0.5 \scale #'(0.8 . 0.8) #(number->string (car frac)) % Numerator of the time signature
                                          \raise #-0.5 \scale #'(0.8 . 0.8) #(number->string (cdr frac)) % Denominator of the time signature
                                        }
                                       }
                                 #})))
         (set! last-fraction frac)
         (set! cause '()))))))

\layout {
  \context {
    \Staff
    \remove Time_signature_engraver
    \consists #Top_time_signature_engraver
  }
}

{ 4 4 4 4 \time 5/4 4 4 4 4 4 }

In simple words this engraver will always create a mark as soon as the time signature fraction changes. If a \time ... event exists it is provided as cause, which allows for things such as point and click.
This also allows you to specify the marks at whatever level you might want — Staff level:
\layout {
  \context {
    \Staff
    \remove Time_signature_engraver
    \consists #Top_time_signature_engraver
  }
}

<<
  \new StaffGroup <<
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 }
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 }
  >>
  \new StaffGroup <<
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 }
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 }
  >>
>>

Score level:
\layout {
  \context {
    \Staff
    \remove Time_signature_engraver
  }
  \context {
    \Score
    \consists #Top_time_signature_engraver
  }
}

<<
  \new StaffGroup <<
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8 }
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8}
  >>
  \new StaffGroup <<
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8}
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8}
  >>
>>

Staff group level:
\layout {
  \context {
    \Staff
    \remove Time_signature_engraver
  }
  \context {
    \StaffGroup
    \consists #Top_time_signature_engraver
  }
}

<<
  \new StaffGroup <<
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8 }
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8}
  >>
  \new StaffGroup <<
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8}
    \new Staff { 4 4 4 4 \time 5/4 4 4 4 4 4 \time 7/8 8 8 8 8 8 8 8}
  >>
>>


Help with 2 errors using DOMDocument for html parsing

Question: When I run the decorate_keyword() function below, I'm getting two errors that I'm trying to resolve. The first seems the easiest to resolve. I'm getting...

Warning: DOMXPath::query()
  [domxpath.query]: Invalid expression
  in
  C:\xampplite\htdocs\testsite\wp-content\plugins\myplugin\index.php
  on line 48

Which points to the $nodes line in the function and is apparently stemming from the $keyword variable when it contains an empty space. $keyword = "test" does not throw the error. What do I need to change to overcome this error?
function rseo_decorate_keyword($postarray) {
    $keyword = "test";
    $content = $postarray['post_content'];
    /*
     even though I can echo $content and get a string,
     I'm getting error: Empty string supplied in loadHTML() when I use this.
     so, I have to explicity set it to a string as below to test.
   */
    $content = "this is a test phrase";
    $d = new DOMDocument();
    $d->loadHTML($content);
    $x = new DOMXpath($d);
    $nodes = $x->query("//text()[contains(.,$keyword) and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6)]");
    if ($nodes && $nodes->length) {
        $node = $nodes->item(0);
        // Split just before the keyword
        $keynode = $node->splitText(strpos($node->textContent, $keyword));
        // Split after the keyword
        $node->nextSibling->splitText(strlen($keyword));
        // Replace keyword with keyword
        $replacement = $d->createElement('b', $keynode->textContent);
        $keynode->parentNode->replaceChild($replacement, $keynode);
    }
     $postarray['post_content'] = $d;
return $postarray;
}

Answer: The contains XPath function should have the second argument in quotes if it contains various characters, such as ;&,. The space is one of them.
$nodes = $x->query("//text()[contains(.,'$keyword') and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6)]");

For your mysql_real_escape_string error, for some reason an object is being passed rather than a string. I'm not familiar with Wordpress internals: you should make a separate question with the appropriate tags.
Comment: I figured it was something simple. THanks!


two soft questions about stochastical ordering

Question: I have two questions and I will be very happy to hear your comments:

a-) For two random variables $X$ and $Y$ let $X$ dominate $Y$, i.e. $X>_{ST}Y$. let $f$ be a positive function. Is it true that $f(X)$ stochastically dominate $f(Y)$, i.e. $f(X)>_{ST}f(Y)$?
b-) For two random variables $X$ and $Y$ which do not stochastically dominate each other, is there are function $f$ such that $f(X)$ stochastically dominate $f(Y)$?

Thanks in advance.
What I know:
For part a-) I know that if $f$ is non-decreasing, then the domination is preserved.
For part b-) I know that an $f$ function exists such that $E[f(X)]\geq E[f(Y)]$ but this is in expectation not in CDF.
Answer: a) No, the domination is not preserved. For example take $X=1$ (has a dirac measure on $1$) and $Y \sim U[1,2]$. Then $Y$ stochastically dominates $X$. But now let $f: [1,2] \to \mathbb R, f(x) = \frac{1}{x} > 0 $ then the exact opposite relation holds for $f(X)=X=1$ and $f(Y) \leq 1$.
b) A trivial example would be a constant function. Else, you could just construct a function appropriately operating on the set $\{t\in\mathbb R: F_X(t) \leq F_Y(t)\}$ (and leaving the rest constant), which is not empty because of your assumption.
edit: More detailed example for b)
Let $X \sim \frac{1}{2} (\delta_0 + \delta_2)$ and $Y \sim \delta_1$ ($\delta_x$ is denotes the dirac measure concentrated at $x$). Then $X, Y$ can't be stochastically ordered. But let $f(x) = 1_{\{x \geq 2\}}$, then you get a strict inequality of the distribution functions of $f(X), f(Y)$ for $t \in [0,1)$, i.e.
$$F_{f(Y)}(t) = 1 \;\forall t \in [0,1)$$, while
$$F_{f(X)}(t) = \frac{1}{2} \;\forall t \in [0,1)$$
Comment: thanks for the answer, yes it is also what i thought so. So for a constant function we have $\leq$ case. I was wondering an example where $F_X(y)<F_Y(y)$. Do you have in mind any example?
Comment: no problem! Edited the post to add a more detailed example for b).
Comment: Okay no need to restrict to $[0,1)$ at the end of the interval of interest we will eventually get $\leq$. Thx for the answer. I have another question about stochastical ordering. Please let me know if you have any idea: http://math.stackexchange.com/questions/1015343/how-to-show-that-a-closed-ball-doesnt-allow-stochastical-ordering


Eccentricity of a hyperbola given the angle between the x-axis and its asymptote

Question: I need to find the eccentricity of a hyperbola whose asymptote makes an angle $\alpha$ with the $x$-axis.
So, I take the case where the transverse axis will be horizontal, $i.e.$
$\frac{x^{2}}{a^{2}} - \frac{y^{2}}{b^{2}} = 1$
I note that $\tan(\alpha) = \frac{b}{a}$.
I need $\epsilon = \frac{a^2}{c}$, so I can get $a$ in terms of $\alpha$ and $b$, $i.e.$
$a^2 = \frac{b^2}{({tan\alpha})^2}$
by using the fact that the asymptote has equation $y=\frac{b}{a}x$  and ${tan\alpha} = \frac{b}{a}$
After this I get stuck.  I'm thinking that my solution must be in terms of $\alpha$ only, since I'm not given any information about $a$ or $b$.  And I know that $c^2 - a^2 = b^2$, but I'm not seeing the algebra necessary to properly substitute in so that the the eccentricity is represented only in terms of $\alpha$.
Am I on the right track, and, if so, where do I go from here?
I figure, once I have this case, I can easily make an analogous argument for a hyperbola with a vertical transverse axis.
Answer: So $\tan\alpha = \frac{b}{a}$ and $c^2 - a^2 = b^2$
So we have:
$c^2 = a^2 + b^2$
$c^2 = a^2 + (a^2)tan^2\alpha$
$c = \sqrt{a^2 + (a^2)tan^2\alpha}$
$c = a\sqrt{1 + tan^2\alpha}$
$c = a\sqrt{sec^2\alpha}$
$c = (a)(sec\alpha)$
$\frac{c}{a} = sec\alpha$
$\epsilon = sec\alpha$