nginx and php-fpm waiting for server too long

Question: I am using docker to mount nginx and a PHP-FPM using Phalcon Framework and React as the front end
`upstream fastcgi_backend {
server bfy-%APP_VERSION_UID%_buildify-php-fpm_1:90%FPM_PORT_PREFIX%;
}

server {
# Port 80 will require Nginx to be started with root permissions
# Depending on how you install Nginx to use port 80 you will need
# to start the server with `sudo` ports about 1000 do not require
# root privileges
# listen      80;

listen        80;
server_name $hostname;

##########################
# In production require SSL
# listen 443 ssl default_server;

# ssl on;
# ssl_session_timeout  5m;
# ssl_protocols  SSLv2 SSLv3 TLSv1;
# ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers   on;

# These locations depend on where you store your certs
# ssl_certificate        /var/nginx/certs/default.cert;
# ssl_certificate_key    /var/nginx/certs/default.key;
##########################

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

# This is the folder that index.php is in
root /var/www/html/public;
index index.php index.html index.htm;

charset utf-8;
client_max_body_size 100M;
fastcgi_read_timeout 1800;

open_file_cache max=100;
output_buffers 5 256k;

set_real_ip_from 172.18.0.9;
real_ip_header X-Real-IP;

# Represents the root of the domain
# http://localhost:8000/[index.php]
location / {
    # Matches URLS `$_GET['_url']`
    try_files $uri $uri/ /index.php?_url=$uri&$args;
}

# When the HTTP request does not match the above
# and the file ends in .php
location ~ [^/]\.php(/|$) {
    # try_files $uri =404;

    # Ubuntu and PHP7.0-fpm in socket mode
    # This path is dependent on the version of PHP install
    fastcgi_pass  fastcgi_backend;

    # Alternatively you use PHP-FPM in TCP mode (Required on Windows)
    # You will need to configure FPM to listen on a standard port
    # https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/
    # fastcgi_pass  127.0.0.1:9000;

    fastcgi_index /index.php;

    include fastcgi_params;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    fastcgi_param PATH_INFO       $fastcgi_path_info;
    # fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    # and set php.ini cgi.fix_pathinfo=0

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_buffers 64 4k;
    fastcgi_busy_buffers_size 24k;
    fastcgi_buffer_size 16k;
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;
}

location ~ /\.ht {
    deny all;
}

location ~* /collections.json$ {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
}

location /products/ {
    location ~* .js$ {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }
}

location /collections/ {
    location ~* /products.json$ {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }
}

location ~* \.(js|json|css|png|jpg|jpeg|gif|ico)$ {
    expires       max;
    log_not_found off;
    access_log    off;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/xml+rss
    image/svg+xml;
gzip_vary on;
}

and this is the php-fpm config file
[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /run/php-fpm.pid

; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written into a
local file.
; Note: the default prefix is /var
error_log = /proc/self/fd/2

; syslog_ident is prepended to every message. If you have multiple FPM
; instances running on the same server, you can change the default value
; which must suit common needs.
; Default Value: php-fpm
;syslog.ident = php-fpm

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
;emergency_restart_threshold = 0

process_control_timeout = 0

daemonize = no

; Set open file descriptor rlimit for the master process.
; Default Value: system defined value
rlimit_files = 1024

; Set max core size rlimit for the master process.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
rlimit_core = 0

; Specify the event mechanism FPM will use. The following is available:
; - select     (any POSIX os)
; - poll       (any POSIX os)
; - epoll      (linux >= 2.5.44)
; - kqueue     (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
; - /dev/poll  (Solaris >= 7)
; - port       (Solaris >= 10)
; Default Value: not set (auto detection)
events.mechanism = epoll

[www]
; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/1

user = %FPM_USER%
group = %FPM_GROUP%

;listen.owner = root
;listen.group = root
;listen.mode = 0660
;listen.allowed_clients = 127.0.0.1
listen = 0.0.0.0:%FPM_PORT%

pm = dynamic
pm.max_children = 200
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 25
pm.max_requests = 500
pm.process_idle_timeout = 10s

clear_env = no

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes

; for debugging 1h
request_terminate_timeout = 3600

I am using it as headless e-commerce using Shopify APIs.
The Response from the API is fast but the page takes about 12 seconds to load when I check the requests I see almost all the times is spent waiting for the server
 even if I remove the API calls the page takes the same time to load
I want to improve the server response time so that the page does not take 10-12 seconds to load I tried reducing and increasing the pm.max_childern and other pm properties but it did not made a difference.
I am using FROM php:7.2-fpm AS phalcon_build.
Is there something wrong with the configuration that is causing the page to load slow?
Comment: While there's quite a few issues with your config, you haven't asked a question nor provided any information to support a diagnosis of why your site is slow.
Comment: Questions seeking installation, configuration or diagnostic help must include the desired end state, the specific problem or error, sufficient information about the configuration and environment to reproduce it, and attempted solutions. Questions without a clear problem statement are not useful to other readers and are unlikely to get good answers. See [ask]
Answer: You should install xhprof, this way you'll see where the time is spent.
Using PECL (https://pecl.php.net/package/xhprof)
pecl install xhprof

And use the xhprof_html and xhprof_lib from this project to show the result: https://github.com/longxinH/xhprof
Answer: Yes, there's stuff wrong with your config, but your primary issue is arising within the PHP execution (or any persistence layer such as a DB underneath  the PHP).
Check for session contention, unnecessary DNS lookups and add instrumentation to record the elapsed time whenever the execution thread leaves PHP (i.e. http calls, database calls, external scripts).


Let $S=\{(a,b,c):a,b,c\in\mathbb{R}\}$ such that $f(z)=x+ay+i(bx+cy)$ is analytic then $L(S)=$ The linear span of $S$ is isomorphic to

Question: Let $S=\{(a,b,c):a,b,c\in\mathbb{R}\}$ such that $f(z)=x+ay+i(bx+cy)$ is analytic then $L(S)=$ The linear span of $S$ is isomorphic to
$(a) \,\mathbb{C}(\mathbb{R})$
$(b)\, \mathbb{R}^2(\mathbb{R})$
$(c)\, \mathbb{R}(\mathbb{R})$
$(d) \,\mathbb{R}^3(\mathbb{R})$
Using $C-R$ equations, I got $a+b=0$ and $c=1$. So $S=\{(a,-a,1):a\in\mathbb{R}\}$
From this, can anybody please help.
Comment: What is meant by the notations ${\bf C}({\bf R})$ etc.?
Comment: $\mathbb{C}(\mathbb{R})$ denotes the vector space of complex numbers over the real field.
Answer: $(1,-1,1)$ and $(-1,1,1)$ belong to $S$. Any pint of $S$ can be written  as $(a,-a,1)=\lambda (1,-1,1)+\mu (-1,1,1)$ where $\lambda =\frac {1+a} 2$ and $\mu =\frac {1-a} 2$. Hence, the span of $S$ coincides with then span of $(1,-1,1)$ and $(-1,1,1)$ and span of $S$ is isomorphic to $\mathbb R^{2}$.


Is there any way that i can read from BigQuery using Dialogflow Chatbot

Question: I want to implement a function where I want to display the data in the dialogflow chatbot that is retrieved from BigQuery using a select statement.Is this possible.Kindly help
Comment: Use the webhooks (fulfilment request) for this. I used Cloud Run in Go to POC this month ago.
Comment: Is there any demo that I can refer to online im able to find only sources where we are just updating the data
Comment: I don't know good resources on this. I tested a lot and I found how to interact correctly. It's not very well documented and you need time and courage to achieve this! My first step was to implement a fulfillment function that printed the JSON content. From there, you know what you receive. Finally, perform your request and forward the result formatted as expected by your bot.
Answer: Based on what was mentioned in the comments sections, seemingly Dialogflow Fulfillment is exactly what you need here. When user types an expression, Dialogflow matches the intent and send webhook request based on the adjusted fulfillment function, the webhook service then performs the intended action like calling API services or affording some other business logic processes.
Dialogflow integration with Bigquery also requires applying fulfillment code to compose appropriate GCP Cloud function that will handle communication with Bigquery API service. Said this, you can use built-in Inline editor and write your fulfillment function, however it doesn't accept any other then Node.js programming language.
Saying more about implementation, I think you can follow codelabs tutorial which provides in detail the general workflow, assuming that you can inject your own code replacing the domestic addToBigQuery() function in Index.js from the example. For this purpose you can visit nodejs-bigquery Github repository containing a lot of useful code samples, in particular the generic query() function that might match your initial aim.


SyntaxError: Non-UTF-8 code starting with '\xe7'

Question: I made a program which contains chinese and russian words, but when I ran it, I had a problem with the encoding
In the code that I shared, a complete sentence with some Russian and Chinese characters is shown. With that variable assignment the SyntaxError arises. But when i write sentence=input(), when the user enters the same sentence no error appears.
sentence='n紙sнo頭q愛z語u買gлd娜xтgлj鳥u買gлcхd娜u買 рj鳥pщi魚d娜gлh園d娜gлn紙r無z語 рr無pщl電pщv書kмz語u買gлkмu買o頭d娜r無n紙r無d娜o頭pщh園z語gлh園d娜gлpщcхo頭z語gлu買kмwзd娜cхgлsнgлz語r無kмd娜u買o頭pщh園z語gлpщgлz語aчi魚d娜o頭z語xтgлv書z語u買gлd娜cхgлv書j鳥pщcхgлn紙z語h園d娜l電z語xтgлv書r無d娜pщr無gлo頭z語h園z語gлo頭kмn紙z語gлh園d娜gлpщn紙cхkмv書pщv書kмz語u買d娜xтgлd娜u買o頭r無d娜gлxтj鳥xтgлh園kмwзd娜r無xтz語xтgлo頭kмn紙z語xтgлh園d娜gлd娜xтo頭r無j鳥v書o頭j鳥r無pщxтgлh園d娜gлh園pщo頭z語xтgлxтd娜gлd娜u買v書j鳥d娜u買o頭r無pщgлh園kмv書v書kмz語u買pщr無kмz語xтgлh園d娜gлh園pщo頭z語xтgлd娜u買gлd娜xтo頭d娜gлo頭j鳥o頭z語r無kмpщcхgлpщn紙r無d娜u買h園d娜r無d娜l電z語xтgлpщgлj鳥o頭kмcхkмñсpщr無gлd娜xтo頭pщgлd娜xтo頭r無j鳥v書o頭j鳥r無pщgлr無d娜wзkмxтpщu買h園z語gлxтj鳥xтgлl電d娜o頭z語h園z語xтgлl電pщxтgлj鳥o頭kмcхkмñсpщh園z語xт'

SyntaxError: Non-UTF-8 code starting with '\xe5' in file hjs.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

How can I solve it?
Answer: First of all, welcome to Stack Overflow!
Second, you could solve your problem by using Python 3 or, for Python 2, following what is said in this answer.
But why?
Well, according to the aforementioned PEP 263,

Python will default to ASCII as standard encoding if no other encoding hints are given.

And in the PEP you can see the same thing that the mentioned answer says, to add the line # -*- coding: <encoding name> -*-
And why isn't Python 3 affected by this issue?
As said in here,

Since Python 3.0, the language's str type contains Unicode characters(...)

So there is no need for adding the coding magic comment.
For more on that the full unicode article linked above is a great reading, and as it is a classic in StackOverflow, please see this.
Comment: I tried with python 3.6 and it worked. With python 2.7 it still raises, so I think my experiments are sound. What is your python 3 version (obtained with _python3 --version_)?


Gradient without function, just testing and training data

Question: I am new to optimization in particular so I think what I am looking for is vocabulary to describe a particular area of problems. In many (all?) optimization algorithms you need to take the gradient of the objective function at various points in the algorithm.
Many examples I find assume you already know the objective function, but, I assume, in most interesting problems you only have inputs and outputs of the objective function (testing and training data set). So how do you go from just the results of the function to the gradient of a function? I guess you could use regression analysis or slope of data points, but what are the common techniques (pros and cons) in optimization?
Answer: I think you are confusing the true objective function with the learner's objective function. Often, in machine learning and data science, we approximate a function using a model by following the gradient of the loss function with respect to the parameters of the model.
For example, let us approximate a true objective function $t(x)$ using a model $f(x,\alpha)$, given a dataset of points $(x_i,y_i)$ (with $i \in \{0\ldots n\}$). We first define the loss function as the sum of the loss with respect to each training point:
$\qquad L(x) = \sum_{i=1}^n (t(x_i) - f(x_i,\alpha))^2 = \sum_{i=1}^n (y_i - f(x_i,\alpha))^2$
And then use an optimization procedure to minimize the loss. One way to solve this, is to use gradient descent, which would require you to follow the gradient with respect to the loss function in an online manner:
$\text{while not stop:}\\
 \quad\text{pick a parameter index i}\\
 \quad\alpha \leftarrow \alpha + \eta \cdot \nabla f(x_i,\alpha)
$
Now if we have a form of the model, we can calculate this in a straightforward way. For instance, if $f(x,\alpha)$ is a one-dimensional plane:
$
\qquad f(x,\alpha) = \alpha_1 \cdot x + \alpha_0\\
\qquad \nabla L(x,\alpha) = \nabla(y_i - f(x_i,\alpha))^2 = \nabla(y_i - \alpha_1 \cdot x + \alpha_0)^2 = \ldots
$
Plug in the second equation into the algorithm, and you're good to go. This is, of course, just one example. There exist many different formulations of loss functions and many different associated optimization procedures. Enumerating all of them would require an enumeration of the discipline and is simply too broad (although I can say many of the optimization procedure encoutered in machine learning are either linear or convex).
Note also, that we don't always optimize with respect to the loss immediatly. Often, other terms are introduced in the optimization problem such as regularization terms. Furthermore, in some cases we want to avoid working with the loss function directly alltogether, one famous example being support vector machines.
Comment: $t(x_i a)$ in the loss function above is an approximation derived from the model $f(x,a)$? The model would be something like regression, right? I think I am misunderstanding something, "approximate a function using a model by folowing the gradient of the loss function", but it looks like the loss function is using the approximated function, or are they different functions?
Comment: Sorry, I changed the definition while I was writing the post and switched them in the first formula. It should be clearer now. The loss function measure the mistake you are making at a certain time in the algorithm and is equal to the difference between the true value (as observed, $t(x_i) = y_i$) and the predicted value of the model in the current stage ($f(x,\alpha)$, $\alpha$ is the current guess for the parameters).


HybridAuth hide additional information form for twitter

Question: I'm trying to integrate hybridauth module for social login. Its working fine for Facebook.
But for twitter, after successful sign in, its asking E-mail, first name, last name & gender details.
If I uncheck required information, I won't get email to create user in back-end.
How can we do set up in twitter side to get these details?
Or
Is there any way to get these required informations with out showing the form?
My intention to successfully login into Drupal site after sign in using twitter, with out asking any addition informations.
After sign in I'm getting below form (Want to hide this form):

Drupal back end setting (If i hide, above form wont come but can't get email ID and other details):
Answer: The reason it prompts for that information is the Twitter API doesn't provide it; you can't retrieve user emails through the API; it only provides a single field for a user's real name (thus the prompt for the last name); and it doesn't collect information on a user's gender.


Browsing the html page from virtual directory works in IE and no working in Chrome and firefox

Question: localhost/rupeshwebsite/RJ.htm
this url works fine IE and
I am developing a windows application using C#. We are developing application as a generalised one.We are giving scripts  in a notepad file so that they can place in the script in their web site or html page it should work.
As a part of testing i created a separate project and put the script in html page  and created the virtual directory for that project named rupeshwebsite.
When i browse the html page from the virtual directory, in IE the  url works fine and displaying the  details in browser. but when i paste the same url in chrome or firefox it won't work. Inside the script we are calling Json Rest service method using $.getJSON Function.Why the url is not working in chrome or firefox. Instead if replaced with my PC ip address in the url it's working fine in chrome and firefox. What is the error. Here i am attaching my script code.

Answer: Have you tried to use the path ?
In Firefox this should work (should work in chrome, too):
file:///C:/your/path/to/rupeshwebsite/RJ.htm

Answer: On Google Chrome on right mouse click there is 'Inspect Elements' option which will open debug console, you can get some information about the error from there. And for Firefox you can use the Firebug add-on for the same purpose.


How to remove a plus when clicking on a phone number?

Question: How to remove a plus when clicking on a phone number?
Now I have a link with a phone number and when I click on it, the system itself substitutes a plus, how to get rid of it?
<a href={`tel:08001999`} /> // My number

When pressed, the phone opens a call with the following number:
+08001999

And how to do with this?
08001999

Answer: Just add the country code along with your number and the number should still work with the plus too.
E.g. if your country code is say, 91, then your anchor href would look like this:
<a href={`tel:9108001999`} />

And the phone would open a call with the following number which should work fine:
+9108001999


Android/ Java: trim cut the latitude for map view

Question: I have stored the latitude and longitude inside my database as a string, e.g 23.563987,120.761719
How can i trim them into two seperate integers so that it can fit into my code? thanks!
[and sorry that i'm new to java....]
My map code is as below:
GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getApplicationContext());
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.addMarker(new MarkerOptions().position(
            new LatLng(23.563987,120.761719)).title("Marker"));
    mMap.setMyLocationEnabled(true);

    CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(
            23.563987,120.761719));
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

    mMap.moveCamera(center);
    mMap.animateCamera(zoom);

Answer: You can split the strung and convert it back to doubles.
String locationString = *from DB*
String[] latLongStrings = locationString.split(",");
double lat = Double.parseDouble(latLongStrings[0]);
double long = Double.parseDouble(latLongStrings[1]);

However, you might want to adjust your DB schema to have two REAL columns instead of one TEXT
Comment: any pros and cons of having one text columns? other than having to cut the string? thanks!!
Comment: It's good form to use the right data types. It makes searching and managing the data easier.
Answer: Why would you want to?  Trimming to an integer would lose a lot of accuracy-  you could be off by as much as .5 degrees, or 35 miles in either direction.


How to enable refunds for paypal adaptive payments?

Question: I'm trying to make refunds for adaptive payments and I get following Error in response: 'NO_API_ACCESS_TO_RECEIVER'
On this page
https://developer.paypal.com/docs/classic/api/adaptive-payments/Refund_API_Operation/
in documentation exists link to  Grant API permission, but looks like it obsolete, and I can't find any other information how to enable refunds, has anyone resolved this issue?
Answer: The receiver must provide you (primary receiver) access to the refund API. The PayPal document clearly states the following:

Have made a Pay request for which payment you want to refund the
  payment, or you must have received the part of the payment you want to
  refund, and
Have permission to make a refund on behalf of the receiver, unless you
  are also the receiver; in other words, each receiver whose account is
  the source of the refund must have granted you third-party access
  permission to call the Refund API operation.


How to align a column right-adjusted

Question: I want to align a column in my table. In this case 'Title'
mysql> SELECT Titel, KuenstlerName from Bild;
+--------------------------+---------------+
| Title                    |         Artist|
+--------------------------+---------------+
| Drei Musikanten          | Picasso       |
| Buveuse assoupie         | Picasso       |
| Die Flöte des Pan        | Picasso       |
| Paolo als Harlekin       | Picasso       |
| Die Umarmung             | Picasso       |
| Sitzende Frau            | Picasso       |
| Sternennacht             | van Gogh      |
| Der Park                 | Klingt        |
| Liegender Hund im Schnee | Marc          |
| Hauptweg und Nebenwege   | Klee          |
| Jungfrau im Baum         | Klee          |
| Das gelbe Haus           | Klee          |
+--------------------------+---------------+

Is it possible to align the left column to the right, like this:
+--------------------------+---------------+
| Title                    | Artist        |
+--------------------------+---------------+
|           Drei Musikanten| Picasso       |
|          Buveuse assoupie| Picasso       |
|         Die Flöte des Pan| Picasso       |
+--------------------------+---------------+

Thanks for your help!
Comment: sql is a database. It handles data, it is not for handling the display of it. Yes, you can pad your string as seen below, but that changes the data (adds spaces) and not it's display. It also will only work in fixed-font scenarios.
Answer: If your Title length is 150 then query like this
 SELECT LPAD(Titel,150,' ') as Titel , KuenstlerName from Bild;

Check Manual for LPAD function.
Comment: Don't worry for that you accepted is more important for other user for future reference.
Answer: If Titel is 100 characters, then do this
SELECT
    LPAD(Titel, 100, ' '),
    ...

or
SELECT
    RIGHT(CONCAT(REPEAT(' ', 100), Titel), 100),
    ...

Comment: The second option worked for SQL on iSeries DB2. My need wasn't for right adjusting the field, but rather an input variable going into a query. Worked Wonderfully.  Allowed the optimizer to use the correct index.
Answer: I solved the task like this:
SELECT LPAD(Titel,(SELECT MAX(LENGTH(Titel)) FROM Bild),' ') AS Titel, CONCAT(Kuenstler.Vorname,' ',Kuenstler.Name) AS Kuenstler
FROM Kuenstler
INNER JOIN Bild ON Bild.Kuenstler = Kuenstler.KID;

How to align a column right-adjusted


Endpoint is weird Amazon Textract Python

Question: I'm trying to use textract in python. I got the code from this url: https://github.com/aws-samples/amazon-textract-code-samples/blob/c8f34ca25113100730e0f4db3f6f316b0cff44d6/python/02-detect-text-s3.py.
I only changed s3BucketName and documentName in the code. But when I ran the code, I got this error:
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://textract.USA.amazonaws.com/"

Should I alter the url manually? If so, how can i do that?
Answer: The endpoint URL depends on your AWS region; USA is not a valid AWS region.
You can set the region when creating the boto3 client:
textract = boto3.client('textract', region_name='us-west-1')

will use https://textract.us-west-1.amazonaws.com/ as the endpoint.
Alternatively, the region can come from the profile or environment; see the boto3 configuration docs for more details.


view all possible attributes of an objectClass for LDAP

Question: I'm scripting some ldap automation and have reached a slight hangup.  Basically, I want to check if an attribute being added to an entry actually exists within a given objectClass before I try to add it.
So far the best idea I have for this is just running a regex on the schema definition file for the attribute, but that wouldn't account for the schema file being edited after the config is initialized.
A second thought would be to just catch the error thrown if the attribute can't be added, but that seems less efficient since my next step would then be to add the attribute to the schema and rebuild the config.
Seems like there should be a simple ldapsearch command to do this but I can't figure out the syntax.
so far I've tried:
ldapsearch -x -b 'dc=MY_DOMAIN,dc=com' '(objectclass=mySCHEMA)'

but that just lists any ldap entries that have the mySCHEMA objectclass on them.
Thanks for the help, Cheers!
Answer: You're looking for the subschemaSubentry.
RFC 2252 Lightweight Directory Access Protocol (v3): Attribute Syntax Definitions

5.1.5. subschemaSubentry
The value of this attribute is the name of a subschema entry (or
     subentry if the server is based on X.500(93)) in which the server
     makes available attributes specifying the schema.
( 126.96.36.199 NAME 'subschemaSubentry'
  EQUALITY distinguishedNameMatch
  SYNTAX 188.8.131.52.4.1.1418.104.22.168.12 NO-USER-MODIFICATION
  SINGLE-VALUE USAGE directoryOperation )

You can find it like so:
$ ldapsearch -s base -b '' subschemaSubentry
dn:
subschemaSubentry: cn=Subschema

$ ldapsearch -s base -b cn=Subschema objectClasses

As a one line:
ldapsearch -s base -b $(ldapsearch -s base -b '' subschemaSubentry | sed '/dn:/d;/^$/d;s/subschemaSubentry: //' ) objectClasses

If you're scripting in bash and your version of ldapsearch supports it, -o ldif-wrap=no will mean that you don't have to parse ldif line wrapping.

cn=schema,cn=config, while handy, is usually unavailable under OpenLDAP due to access controls inheritted from cn=config.
Comment: ah yeah, using the openldap, but +1 for the thorough answer :)
Answer: This is what I use to show the schema of a specific objectClass, such as organizationalRole
$ ldapsearch -s base -b cn=Subschema objectClasses -LLL -o ldif-wrap=no |\
  sed -nr '/organizationalRole/ p' | sed -r 's/[$()]+/\n /g'

Answer: It has been a lot of time since I was working with LDAP, but I think that each LDAP server may expose the schema in a certain suffix.
I think in Openldap you can search in base "cn=schema, cn=config" to find the current schema.
Try something like ldapsearch -x -s sub -b "cn=schema,cn=config" '(objectclass=*)' to see what you get. (Haven't tested this command line, but you get the point...).
From a developer's perspective, I would expect that the correct schema is there, and handle the exception of objectclass violation as if it was any kind of error.
I think that altering the schema is not something that should be handled by the application that adds/deletes data but by the installation procedure of the software.
Comment: no dice.  I'm thinking I'll just have to catch the error when adding and the attribute isn't already there.  Seems to be working in the stuff I've written since posting the question.
Answer: Simple shell/awk script approaches won't work at all because of object class inheritance. You have to evaluate that to really find out in advance what the LDAP server would be doing with your add/modify request. (At least that's what I understand what you want to achieve.)
If you don't mind scripting in Python you could use python-ldap's module ldap.schema which I've implemented for the full schema support in web2ldap. Besides object class inheritance it also takes care of DIT content rules which is very important to get attribute lists right with MS AD.
Be warned: Implementing a general solution is not trivial! And depending on the LDAP server used you will find somewhat incomplete schema references and will have to implement fall-back handling here and there.
I only know exactly one LDAPv3 client implementing full LDAP schema support. ;-)
Answer: If you prefer a GUI solution, download an LDAP browser like the free open source cross-platform JXplorer. Once connected to the LDAP, it allows you to browse (and edit) all objects and their attributes.


Are barplots appropriate for showing means?

Question: My data are differences of two values. I try to show the data with points (two per "x-axis-value") and their mean as bars. See here:

I feel that bars could be misleading insofar as they imply a continuous range of values and not just the mean. On the other hand, bars are much more noticeable for comparative purposes.
Secondly, here is a plot with means shown as horizontal "lines". They are likely more appropriate for showing a mean but I find it hard to compare the means and place the indivdual points with respect to the means by eye.
Here is the second plot:

If I would use the first plot in a thesis, do you think this will be found faulty, so to say?
Comment: Two points and optionally a short line showing the mean seem reasonable. I wouldn't extend the line to zero as seems to arise in two or three cases.  On a different level, we can't speak for people reading your thesis, just for ourselves.
Comment: Do you have just 2 points in each case? If so, the mean is not adding much information. You could consider omitting it entirely.
Comment: The vertical lines connecting the points (for each X-axis value) are also a bit redundant, especially the bit connecting them to the X-axis when both values are >0
Comment: In addition to mkt's point, in some cases (green bar) the line extends to zero but probably there are not points there?
Comment: I don't know what these data are really, but imagine that whether the mean is positive or negative could be of interest.
Comment: Yes, I was thinking the same. Redundancy of means and the lines connecting the points are also no good.:/ thank you i definitely need to consider that
Comment: @NickCox u think the mean is somewhat meaningful in this graph?
Answer: I came up with a revision:
1.) The lines connecting the points are now substituted by a background color
(pairs of colors here are just options), (points are now bars)
2.) mean is left out


Fsyacc: an item with the same key has been added

Question: I'm starting to play with Fslex/Fsyacc. When trying to generate the parser using this input
Parser.fsy:
%{
open Ast
%}

// The start token becomes a parser function in the compiled code:
%start start

// These are the terminal tokens of the grammar along with the types of
// the data carried by each token:
%token <System.Int32> INT
%token <System.String> STRING
%token <System.String> ID
%token PLUS MINUS ASTER SLASH LT LT EQ GTE GT
%token LPAREN RPAREN LCURLY RCURLY LBRACKET RBRACKET COMMA
%token ARRAY IF THEN ELSE WHILE FOR TO DO LET IN END OF BREAK NIL FUNCTION VAR TYPE IMPORT PRIMITIVE
%token EOF

// This is the type of the data produced by a successful reduction of the 'start'
// symbol:
%type <Ast.Program> start

%%

// These are the rules of the grammar along with the F# code of the
// actions executed as rules are reduced.  In this case the actions
// produce data using F# data construction terms.
start: Prog { Program($1) }

Prog:
    | Expr EOF                  { $1 }

Expr:
    // literals
    | NIL                   { Ast.Nil ($1) }
      | INT                       { Ast.Integer($1) }
    | STRING                    { Ast.Str($1) }

    // arrays and records
    | ID LBRACKET Expr RBRACKET OF Expr { Ast.Array ($1, $3, $6) }
    | ID LCURLY AssignmentList RCURLY { Ast.Record ($1, $3) }

AssignmentList:
    | Assignment { [$1] }
    | Assignment COMMA AssignmentList {$1 :: $3 }

Assignment:
    | ID EQ Expr { Ast.Assignment ($1,$3) }

Ast.fs
namespace Ast
open System

type Integer =
    | Integer of Int32

and Str =
    | Str of string

and Nil =
    | None

and Id =
    | Id of string

and Array =
    | Array of Id * Expr * Expr

and Record =
    | Record of Id * (Assignment list)

and Assignment =
    | Assignment of Id * Expr

and Expr =
    | Nil
    | Integer
    | Str
    | Array
    | Record

and Program =
    | Program of Expr

Fsyacc reports the following error: "FSYACC: error FSY000: An item with the same key has already been added."
I believe the problem is in the production for AssignmentList, but can't find a way around...
Any tips will be appreciated
Answer: Hate answering my own questions, but the problem was here (line 15 of parser input file)
%token PLUS MINUS ASTER SLASH LT LT EQ GTE GT
Note the double definition (should have been LTE)
My vote goes for finding a way to improve the output of the Fslex/Fsyacc executables/msbuild tasks


Good way to filter list distincted by property and ordered by date

Question: I have very simple thing to do, i have list of persons like this:
[{
    name: John,
    date: 01-01-2018,
    attend: true
},
{
    name: Adam,
    date: 01-01-2018,
    attend: false
},
{
    name: Adam,
    date: 01-02-2018,
    attend: true
},
{
    name: JOHN,
    date: 01-02-2018,
    attend: false
}]

Result of this array should be: Adam (true), John (false)
So i need to return list of latest entries from users, in this case John first confirmed that he is attending, then he changed his mind and told that he is not attending so i'm returning his last entry (note that sometimes it's written JOHN and sometimes John but it is the same person, that is kind of tricky part)
My question what is best approach to filter out this kind of list, i was thinking applying "unique by property java stream" but first will need to order persons by date descending and names (to upper/lower case) and then i would need somehow to take latest entry.
Anyone have any good idea what is best approach to this ?
Comment: Tip: Format date strings using the standard ISO 8601 format: YYYY-MM-DD. These formats are designed expressly for data exchange, and avoid ambiguity, making them easy for machines to parse and easy for humans to read across cultures. These formats are used by default in the *java.time* classes when parsing/generating text.
Comment: Yea, format is already like that, but i made mistake when i wrote it, anyway it is not important in this case at all, but thank you for suggestion
Answer: You can use Collectors.toMap to do the same as:
List<Person> finalList = new ArrayList<>(people.stream()
        .collect(Collectors.toMap(a -> a.getName().toLowerCase(),  // name in lowercase as the key of the map (uniqueness)
                Function.identity(), // corresponding Person as value
                (person, person2) -> person.getDate().isAfter(person2.getDate()) ? person : person2)) // merge in case of same name based on which date is after the other
        .values()); // fetch the values

Note: The above assumes the minimal Person class to be
class Person {
    String name;
    java.time.LocalDate date;
    boolean attend;
    // getters and setters
}

Answer: You could use the toMap collector:
Collection<Person> values = source.stream()
                    .collect(toMap(e -> e.getName().toLowerCase(),
                            Function.identity(),
                            BinaryOperator.maxBy(Comparator.comparing(Person::getDate))))
                    .values();

see this answer for an explanation as to how toMap works
Answer: Despite all answers until now are functionally correct, please consider this option:
final Map<String, Boolean> lastAttendResults =
            people
                .stream()
                .collect(
                    groupingBy(
                        Person::getName, // Define what is the property you are using to group people.
                        () -> new TreeMap<String, Boolean>(String.CASE_INSENSITIVE_ORDER), // Supply a map implementation that ignore name case.
                        collectingAndThen(maxBy(Comparator.comparing(Person::getDate)), // Foreach list of grouped people, select that with last date.
                            o -> o.get().isAttend()))); // As maxBy returns an Optional<Person> and we are sure that it exists, just get the Person and if he attends.

This implementation is interesting because it let be evident the concept of collection grouping. Although, in depth, it also uses a map, seems to me that use or not a map is not programmer problem, I mean, what we are looking for is how to group people by name and then get the last entry.
If you prefer receive a List of Person instead of a Map of name and attend, you can use:
final List<Person> lastAttendResults =
        new ArrayList<>(people
            .stream()
            .collect(
                groupingBy(Person::getName, // Define what is the property you are using to group people.
                    () -> new TreeMap<String, Person>(String.CASE_INSENSITIVE_ORDER), // Supply a map implementation that ignore name case.
                    collectingAndThen(maxBy(Comparator.comparing(Person::getDate)), // Foreach list of grouped people, select that with last date.
                        Optional::get // As maxBy returns an Optional<Person> and we are sure that is exists, just get the Person.
                        ))).values());

Comment: The only answer where the grouping has no hidden dependency on the default locale - [`toLowerCase()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#toLowerCase()) uses the default locale, which may yield surprising results, while [`CASE_INSENSITIVE_ORDER`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#CASE_INSENSITIVE_ORDER) does not take locale into account. It's also easy to replace it with a [`Collator`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/Collator.html) if necessary.
Answer: A compact way without streams:
Map<String, User> map = new LinkedHashMap<>();
users.forEach(u -> map.merge(
        u.getName().toLowerCase(),
        u,
        BinaryOperator.maxBy(Comparator.comparing(Person::getDate))));

Collection<User> result = map.values();

Or if you do need a List:
List<User> result = new ArrayList<>(map.values());

This code uses Map.merge, which puts the entry in the map if there's no entry with the same key (the lowercased user name), or, if the map already contains an entry for the key, it applies the merge function, which in this case chooses the User instance with the max date.


Obtain p-values from t-values

Question: I used Tobit regression and wanted to test if the coefficients are significant. The problem is that when I run the R code I get t-values instead of z-values. I found code on how to calculate p-values for z-values and I'm wondering if you can use the same code for the t-values that you use for the z-values to calculate the p-values.
This is the code for when you have z-values:
ctable <- coef(summary(m))
pvals <- 2 * pt(abs(ctable[, "z value"]), df.residual(m), lower.tail = FALSE)
cbind(ctable, pvals)

This is the code I used for the t-values:
ctable <- coef(summary(m))
pvals <- 2 * pt(abs(ctable[, "t value"]), df.residual(m), lower.tail = FALSE)
cbind(ctable, pvals)

                    Value  Std. Error   t value         pvals
(Intercept):1   209.559678 32.50165463  6.447662  3.327237e-10
(Intercept):2    4.184758  0.05227107 80.058783 7.628269e-246
read             2.697963  0.62024382  4.349843  1.737355e-05
math             5.914589  0.70502334  8.389210  8.842612e-16
proggeneral     -12.714341 12.36481457 -1.028268  3.044547e-01
progvocational  -46.143265 13.65615960 -3.378934  8.002457e-04

Answer: Computationally this is a correct calculation of a p value for a two sided test of whether there is significant evidence the parameter behind your t value is different from zero, assuming the t statistic does indeed have a t distribution under the null hypothesis.
I'm surprised the summary() method doesn't itself calculate this p value.  This makes me wonder whether the author has reason to doubt whether these statistics really do have t distributions and this is a valid test to use.  I would investigate this further if I were you.
I would also round your p values (and probably everything else in that table) to two decimal places to avoid giving a false sense of precision.

Obtain p-values from t-values


Reverse prediction in a time series

Question: We know using models like ARIMA we can do out of sample predictions for a Time Series. i.e. we can know what would be the value v at time t. Can we do the reverse of it, and find at what t will be v a certain value? Like for linear case, v = mt+c; one can find t = (v-c)/m. Is there a way to do this for nonlinear case?
Comment: Side note: consider also models that target time specifically, e.g. autoregressive conditional duration (ACD) models.
Answer: I don't think it is possible, because this assumes that the function $v=f(t)$ is one on one. But that isn't the case for many time series.
Consider a simple case where a time series looks like a sinusoidal function: One value of v will correspond to multiple values of t.
Comment: Yes, and multiple values are not invalid solution. Anyway, I was wondering if there is a way to do this. Right now I'm thinking of employing a root search kind of method to do this. Any other ideas will be great to know.

Reverse prediction in a time series


Descobrir qual div com a mesma class é maior

Question: Eu gostaria de ter um código que percorresse todas as divs com uma certa class e que depois visse qual era a que tinha maior height e que definisse o tamanho delas todas igual. Existe forma de o fazer?
Sei como conseguir o height com: $(".img").height() mas ele assim só vai buscar o primeiro que apareceu.
UPDATE
Com a resposta do @Artur o Templário, eu consegui ver o height de cada uma, mas agora não estou conseguindo aplicar o height a todas.
Eis o código que fiz
var lista_imagens = $('.border_not');

lista_imagens.each(function (){
  var last = "";
  var height_img=$(this).height();
  if(height_img > last){
    last = height_img;
  }
});

Answer: Você pode fazer isso com poucas linhas de código usando JavaScript puro.
Veja um exemplo:

document.getElementById('bt').addEventListener('click', btClick);

function btClick() {
  const divs = [...document.querySelectorAll('div.classe')];
  let maximo = Math.max(...divs.map((e) => e.offsetHeight));

  for(div of divs) {
    div.setAttribute("style", `height:${maximo}px`);
  }
}
.classe {
  width: 50px;
  background-color: lightblue;
  margin: 5px;
  float: left;
}

.tam1 {
  height: 30px;
}

.tam2 {
  height: 70px;
}

.tam3 {
  height: 110px;
}

.tam-max {
  height: 150px;
}
<button id="bt"> Trocar tamanhos </button>

Comment: Pelo que vejo, isso vai buscar `heigths` p´re definidos certo? No que eu estou a trabalhar, eu tenho quadrados que levam uma imagem e texto mas o texto nalguns quadrados não cabem numa só linha e expandem.
Comment: Não, de onde você tirou isso?
Comment: Peço desculpa, eu não li bem a resposta mas também não a entendi bem
Answer: Percorrendo todas as divs e depois aplicando o maior height encontrado a todos os elementos da mesma coleção:

var els = $(".teste");

var heights = 0;
$.each(els, function(){

   var h = $(this).height();

   if(h > heights) heights = h;

});

els.css('height',heights+'px');

tinha 20px de altura

tenho 50px de altura

tinha 30px de altura

Comment: Dá para aceitar a resposta duas vezes?
Comment: @I_like_trains :D Obrigado!
Answer: Eu acho que você pode fazer isso simplesmente sem usar jQuery ou Javascript, hein!
Com display: flex você pode fazer as imagens ficarem do mesmo tamanho.
Veja:

.images{
  display: flex;

}

Veja como elas ficam sem o atributo definido acima:

Comment: Achei o melhor caminho, se a ideia é criar colunas com a mesma altura.
Answer: Você só precisa percorrer todas elas:
var lista_imagens = $('.img');

lista_imagens.each(function (){
    // aqui, seu this é a imagem da iteração corrente
});

Comment: Editei a pergunta


F#5 or Fx5 in La Campanella m. 128?

Question:
In MuseScore version 3.6.2 I see it being F##5 indeed, but the next note in line is also F##, but looking into how it looks like in Piano visualiser that first one must be a mistake.
Answer: It should be F#5; the score in the Question contains an error.
Here is the same measure in the Busoni edition of the piece. The note has a # sign in front of it.

(Image source: IMSLP, Grandes études de Paganini, S.141)
And again, from the Gárdonyi edition (1971)

(Image source: IMSLP, Grandes études de Paganini, S.141)
Comment: @ВалерийЗаподовников Yes. It goes all the way back to the first edition.
Comment: @ВалерийЗаподовников the alternative conclusion, that there are supposed to be 12 perfect octaves with one diminished octave in their midst, is not tenable.
Comment: Breitkopf & Härtel and Ernst Pauer both have the typo then?
Comment: It's indeed a logical conclusion - what else! But could there have been a cautionary natural sign included (cancelling one of the sharps before) . Almost the whole piece is filled with accidentals, let's make it a full set...
Comment: I would only note that in the Gárdonyi (Bärenreiter) urtext the correction doesn't even merit a comment in the Critical Notes at the back of the volume. (Where editions still differ is in the *quasi cadenza,* m.77.)
Comment: @DjinTonic Thank you. I had hoped to find some critical commentary (or absence thereof), but didn't have access.

F#5 or Fx5 in La Campanella m. 128?


Quotation marks and apostrophes in the same string in QUERY Google Sheets function

Question: I'm wondering how one might handle a query like this. Let's suppose I had the following text contained in Cell A2 of a spreadsheet:
Case Bakers' Flats 12" White Flour Tortillas 10/12ct

and needed to put the following formula into B2:
=QUERY(importrange("KEY", "DATA!A1:Z1000"), "select Col24 where (Col1 = '"&A2&"')")

It would produce an error.
My question is: Is there any way to avoid tripping up the query when the string I am using contains any assortment of quotation marks and apostrophes?
Comment: Possible duplicate of [Google Sheets Query Language: escape apostrophe](http://stackoverflow.com/questions/34991998/google-sheets-query-language-escape-apostrophe)
Answer: Short answer

To escape single quote / apostrophe, embrace the string containing an apostrophe between double quotes (").
To escape double quotes, apply a double substitution, first to remove the double-quotes, then to add them again.

Explanation
Google Sheets QUERY built-in function automatically escape some characters by internally adding \ before single quotes but it's doesn't work when the cell value to be used as the source for the criteria includes double quotes. As a workaround, the the use of double substitution is proposed.
Example for single quote / apostrophe
Below table represents and spreadsheet range that contains

Column A: The data source
Cell B1:  The data value to be used in the criteria expression
Cell C1: The following formula =QUERY(A:A,"SELECT * WHERE A = """&B1&""" ")

+---+---------+-----+-----+
|   |    A    |  B  |  C  |
+---+---------+-----+-----+
| 1 | I'm     | I'm | I'm |
| 2 | You're  |     |     |
| 3 | It's    |     |     |
| 4 | I am    |     |     |
| 5 | You are |     |     |
| 6 | It is   |     |     |
+---+---------+-----+-----+

Example for single quote / apostrophe and double quotes

=SUBSTITUTE(
   QUERY(
     SUBSTITUTE(A:A,"""","''"),
     "SELECT * WHERE Col1 = """&SUBSTITUTE(B1,"""","''")&""""
    ),
    "''",""""
 )

Note that instead of using a the letter A as identifier of the data source column it's used Col1.
Reference
https://developers.google.com/chart/interactive/docs/querylanguage
Answer: =query('Book list'!A:F,"select B,C,D,E where F=""free"" and D="""&E1&"""") - because:

Here are the formats for each type of literal:
String literals should be enclosed in either single or double quotes. Examples: "fourteen", 'hello world', "It's raining".
as mentioned in the reference above.


Question about the conditions that satisfy the Alternating Series Test.

Question: The series $\sum_{n=1}^{\infty}(-1)^{n+1}u_n$ converges if all three of the following conditions are satisfied:

The $u_n$'s are all positive.
The positive $u_n$'s are (eventually) nonincreasing: $u_n\ge u_{n+1}$ for all $n\ge N$.
$u_n \to 0$

This is the definition of the Alternating Series Test from my textbook. I understand the second and third conditions but I do not really understand the significance of the first condition. Since this series is an alternating series, the signs switch from positive to negative so how can all $u_n$'s be positive? Could someone explain the significance of the first condition?
Comment: The sequence $(-1)^{n+1} u_n$ may switch signs, but the first condition is saying that $u_n$ itself must all be positive. That is, without $(-1)^{n+1}$.
Comment: The answer below is good in explaining the reason! It's because then it actually wouldn't necessarily be an alternating series.
Comment: I would prefer to say the terms alternate in sign, and their *absolute values* have limit $0$, and so on. Then series $\sum a_i$ where $a_i$ is negative for odd $n$, and positive for even $n$ don't need to feel discriminated against.
Comment: The **terms** $(-1)^{n+1}u_n$ of the series alternate in sign, since $u_n$ is positive and the $(-1)^{n+1}$ alternates in sign.
Comment: That makes more sense! But why is it required that $u_n$ be positive? My textbook doesn't explain this.
Answer: You're confusing the individual terms $a_n = (-1)^{n+1}u_n$ with the $u_n$ themselves. In fact, we only have that the series is alternating because the $u_n$ are all positive: that means that the $a_n$ change sign every time! As an example for why we need positivity, let's say we picked $u_n = (-1)^{n+1} \frac 1n$. Then our series becomes $$\sum_{n=1}^\infty (-1)^{2n+2} \frac 1n = \sum_{n=1}^\infty \frac 1n$$ By not restricting the $u_n$ to be positive, our sequence (the $a_n = \frac 1n$) no longer alternates signs.
Comment: I see, but if $u_n$ is negative, wouldn't the signs just shift one place to the right or left?
Comment: Yes, that's true. It's really only convenience that we say "all $u_n$ are positive" instead of "all positive OR all negative". But since we only care about convergence, and $-\sum (-1)^n u_n = \sum (-1)^{n+1} u_n$, one converges if and only if the other does - so we can just multiply it by $-1$ to make all the $u_n$ positive.
Comment: Thank you for explaining this so clearly!
Comment: Gladly!$\ \ \ $


Need help on how to compute the fundamental group of a space.

Question: I'm studying for an oral qualifying exam and going through various past exams I find on the interwebs, including this mock exam from the University of Bath (Wayback Machine). One of the questions seems like it should be rather straightforward, but it's giving me trouble. The question goes as follows:
Four small squares are cut out from a large square and discarded. The edges of what remains are then identified according to the labels in the figure. Find the fundamental group of the resulting space $X$. [You may wish to introduce more edges to help your calculations, but be careful not to make unauthorized identifications.]

What would be a 'standard' approach to such a question? Here is what I have thought of so far and where I'm stuck:

Using Van Kampen's: Intuitively, I'd like to set $U$ to be the interior of the large square and $V$ to be the enlargement of the boundary of the large square. Then $U \cap V \simeq S^1$, but I get tangled up when it comes to making the edge identifications and determining what to do with $U$, plus, after edge identifications, $U$ and $V$ interact, and so $U \cap V$ might not actually be $S^1$ as I've thought it was. Is Van Kampen's the way to go here?

Since the hint mentions adding edges, is there some way to view this as a simplicial complex and compute the fundamental group from there? If so, how would I go about computing the fundamental group? (I am only familiar with computing homology for simplicial complexes, where I explicitly construct the boundary operators and compute the kernel/image of each to find the quotient groups.)

Thank you all for your help/advice!

UPDATES:
New Approach #1: I'm going to try and approach this by using Proposition 1.26 in Hatcher. That is, I'll view $X$ as attaching a 2-cell to the boundary in the picture (as suggested in the comments). First, I've added a basepoint $x_0 \in X$ and edges from this basepoint to the points labeled $C$ in each of the squares, as shown in this picture:

After doing this, the boundary of the space turns into the following picture, and, by contracting the edge $k$ to a point, this space deformation retracts to the wedge of five circles. I've also listed the attaching map.

By the proposition, we should be able to compute $\pi_1(X) = \pi_1(\partial X)/N$ where $N = \langle e\gamma e^{-1}f \gamma f^{-1} g \gamma g^{-1} h \gamma h^{-1} k \gamma^{-1} k^{-1} \rangle$. Writing $\pi_1(\partial X) = \langle \gamma, e, f, g, h \rangle$ and noting that $k$ is now the identity element, we get $\pi_1(X) = \langle \gamma, e, f, g, h \rangle / \langle e\gamma e^{-1}f \gamma f^{-1} g \gamma g^{-1} h \gamma h^{-1} \gamma^{-1} \rangle$.
Comment: @Rachel: Instead of including your solution in the question, why not turn it into an answer? That way the question will no longer be on the unanswered list.
Comment: You can realize this space as a CW-complex by attaching one $2$-cell to a wedge sum of $5$ circles. The attaching map can be inferred from the diagram. This is one way to compute the fundamental group.
Comment: @AymanHourieh - I've added details to trying to implement the approach you suggested. I'd be very grateful if you had a chance to look it over and offer any suggestions, as I wasn't able to complete the problem. Thank you!
Comment: Try Van Kampen again, but with $U$ as the light gray region (ie all but the squares) and $V$ as a union of enlargements of each square.
Comment: @aes - In order to use Van Kampen, we need $U \cap V$ to be path-connected, but the way I see it, with the $U$ and $V$ you've suggested, $U \cap V$ becomes five disjoint copies of $S^1$, and so it is not path-connected. Is there something I'm missing here with the identifications?
Comment: @Rachel You're right. There is a version with disconnected intersection, or you can draw the extra edges you have, but that's equivalent to what you're currently doing. As to your current question, the 1-skeleton homotopy-equivalent to a wedge of circles.
Comment: @aes - Ah, I didn't know about the disconnected version. Thank you. Also, I see now how the shape deformation retracts to the wedge of five circles. I've finished writing the solution to the problem accordingly. Thank you again!
Answer: I'll give here another answer which I think is correct but I am not 100% sure. We will rely heavily on the first theorem at page 11 of Hatcher's Algebraic Topology, which basically allows us to do 2 things:

We can kill any contractible line without changing homotopy type
Instead of identifying two points we can just attach a $1$-cell at these two points.

Our first observation is that after identifying the edges, non of the vertices of the large square has been identifyied with eachother therefore we can collapse each edge of the edges $AB,BC,CD,DA$ to a point. The result space is Space $1$, where red points means identified  (see the picture).
Now, we get the homotopy equivalent Space $2$ by replacing identifications by $1$-cells.
At last, we pull the outside point to the center, to get Space $3$, where the red lines are to be thought above the shape.

Now for the tricky part: The disc, without the red lines, deformation retracts to the wedge of $4$ circles. To picture this, imagine a strong gravity well at the center of the disk pulling everything radially at the center of the disc. Evertyhing either falls to the boundaries of the 4 existing holes or at the center of the disc, including the attaching points, therefore the red lines now became circles.
Therefore we end up wit the wedge of 8 circles and the fundamental group is $F_8$.


Fitting a 3D surface to a dataset of points [R]

Question: I have been trying to fit a polynomial surface to a set of point with 3 coordinates.
Let the data be:
DATA <- with(mtcars, as.data.frame(cbind(1:32, wt,disp,mpg)))

I have been trying to draw a surface using:

plot3d from rgl package,
using rsm package,
scatterplot3d package.

For example:
library(scatterplot3d)
attach(mtcars)
DATA <- as.data.frame(cbind(1:32, wt,disp,mpg))
scatterplot3d(wt,disp,mpg, main="3D Scatterplot")
model <- loess(mpg ~wt + disp, data=DATA)
x <-range(DATA$wt)
x <- seq(x[1], x[2], length.out=50)
y <- range(DATA$disp)
y <- seq(y[1], y[2], length.out=50)
z <- outer(x,y,
       function(wt,disp)
         predict(model, data.frame(wt,disp)))
z
p <- persp(x,y,z, theta=30, phi=30,
       col="lightblue",expand = 0.5,shade = 0.2,
       xlab="wt", ylab="disp", zlab="mpg")

I have also tried using surf.ls function:
surf.ls(2,DATA[,2],DATA[,3],DATA[,4])

But what I got looks like this:
I don't really know how to transform it to a 3D plot and more importantly, how to get the formula for the best fit surface obtained.
I would really appreciate your help.
PS I have deleted my last post and included more details in this one.
Comment: Ah, yes, I have noticed that. How about the second option with surf.ls?
Comment: Do you know how else I could try to fit a surface?
Comment: What I want to get is a polynomial expression of the function. I have found many tutorials how to do it in Matlab, but none regarding R, unfortunately.
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95820/discussion-between-marco11-and-thetime).
Answer: Try this:
attach(mtcars)
DATA <- as.data.frame(cbind(1:32, wt,disp,mpg))

x_wt <- DATA$wt
y_disp <- DATA$disp
z_mpg <- DATA$mpg

fit <- lm(z_mpg ~ poly(x_wt, y_disp, degree = 2), data = DATA)

To plot with rsm, use the following:
library(rsm)
image(fit, y_disp ~ x_wt)
contour(fit, y_disp ~ x_wt)
persp(fit, y_disp ~ x_wt, zlab = "z_mpg")

To plot with ggplot, use the following:
## ggplot
# Use rsm package to create surface model.
library(rsm)
SurfMod <- contour(fit, y_disp ~ x_wt)

# extract list values from rsm Surface Model
Xvals <- SurfMod$`x_wt ~ y_disp`[1]
Yvals <- SurfMod$`x_wt ~ y_disp`[2]
Zvals <- SurfMod$`x_wt ~ y_disp`[3]

# Construct matrix with col and row names
SurfMatrix <- Zvals$z
colnames(SurfMatrix) <- Yvals$y
rownames(SurfMatrix) <- Xvals$x

# Convert matrix to data frame
library(reshape2)
SurfDF <- melt(SurfMatrix)

library(ggplot2)
gg <- ggplot(data = SurfDF) +
      geom_tile(data = SurfDF, aes(Var1, Var2,z = value, fill = value)) +
      stat_contour(data = SurfDF, aes(Var1, Var2, z = value, color = ..level..)) +
      scale_colour_gradient(low = "green", high = "red") +
      geom_point(data = DATA, aes(wt, disp, z = mpg, color = mpg)) +
      geom_text(data = DATA, aes(wt, disp,label=mpg),hjust=0, vjust=0) +
      scale_fill_continuous(name="mpg") +
      xlab("x_wt") +
      ylab("y_disp")
library(directlabels)
direct.label.ggplot(gg, "angled.endpoints")

To see all of the available direct.label methods, go to http://directlabels.r-forge.r-project.org/docs/index.html
Comment: Using the lm method for a 3D fit, how would one extract the formula of the fitted surface?
Answer: You can plot fitted surfaces with plotly as well:
library(plotly)

dat <- data.frame(ChickWeight) %>%
  mutate(Chick = as.numeric(Chick))

# polynomial (curvy) surfaces:
fit <- lm(weight ~ factor(Diet)*poly(Time, Chick, degree=2), data=dat)

dat$predicted3d <- predict(fit, data=dat)

# points....
p <- plot_ly(data = dat, x = ~Time, y = ~Chick, z = ~weight, color = ~Diet, type = "scatter3d", mode="markers", alpha=.95)
# surface 1
p <- add_trace(p, data = dat %>% filter(Diet == 1), x = ~Time, y = ~Chick, z = ~weight, color = ~Diet, type = "mesh3d", opacity=.95)
# surface 2
p <- add_trace(p, data = dat %>% filter(Diet == 2), x = ~Time, y = ~Chick, z = ~weight, color = ~Diet, type = "mesh3d", opacity=.95)
# surface 3
p <- add_trace(p, data = dat %>% filter(Diet == 3), x = ~Time, y = ~Chick, z = ~weight, color = ~Diet, type = "mesh3d", opacity=.95)
# surface 4
p <- add_trace(p, data = dat %>% filter(Diet == 4), x = ~Time, y = ~Chick, z = ~weight, color = ~Diet, type = "mesh3d", opacity=.95)

p


Does jmxremote affect tomcat performance?

Question: I'd like to enable jmxremote to monitor Apache Tomcat resources utilization with Munin, but I want to know if this will affect the servers performance, is so, how much will it be impacted.
Answer: We monitor CPU usage on our servers(via SNMP) and when we enabled jmxremote to collect stats we did not see a noticeable appreciation in usage(real machines).  It looks like someone actually grabbed stats from this and documented it:
https://community.oracle.com/blogs/emcmanus/2006/07/21/how-much-does-it-cost-monitor-app-jconsole
They basically say it can vary based on app but saw as high as 5-6% cpu usage increase in some cases.


Return number of documents based on words in a field's string

Question: How can I return the number of documents that have more than 2 elements in the "words" list with more than 3 words in "word_combination".
Is there a way to count the number of words in a string?
Example: return document if (the length of "words" > 2) AND ("words.word_combination" has more than 3 words)
I have many documents stored. One document's structure looks like this:
"_source" : {
"group_words" : [

  {
    "amount" : 1140,
    "words" : [
      {
        "relevance_score" : 56,
        "points" : 66461,
        "bits" : 100,
        "word_combination" : "cat dog"
      },
      {
        "relevance_score" : 84,
        "points" : 45202,
        "bits" : 990,
        "word_combination" : "cat dog elephant"
      },
      {
        "relevance_score" : 99,
        "points" : 30974,
        "bits" : 70,
        "word_combination" : "elephant cat mouse leopard"
      }
    ],
    "group" : "whatever"
  },
  {
    "amount" : 1320,
    "words" : [
      {
        "relevance_score" : 25,
        "points" : 53396,
        "bits" : 70,
        "word_combination" : "lion elephant"
      },
      {
        "relevance_score" : 66,
        "points" : 52166,
        "bits" : 20,
        "word_combination" : "lion mouse fish cat dog"
      },
      {
        "relevance_score" : 82,
        "points" : 49316,
        "bits" : 810,
        "word_combination" : "elephant cat mouse leopard dog lion"
      },
      {
        "relevance_score" : 87,
        "points" : 127705,
        "bits" : 290,
        "word_combination" : "elephant cat mouse leopard tiger lion"
      }
    ],
    "group" : "whatever"
  },
  {
    "amount" : 11260,
    "words" : [
      {
        "relevance_score" : 0,
        "points" : 37909,
        "bits" : 9000,
        "word_combination" : "elephant cat mouse leopard tiger lion monkey"
      },
      {
        "relevance_score" : 3,
        "points" : 35782,
        "bits" : 540,
        "word_combination" : "elephant"
      }
    ],
    "group" : "whatever"
  }
]

}
Answer: Regarding the number of elements in the words array, my advice is to store that number in an additional field words_count at indexing time.
  {
    "amount" : 1140,
    "words_count": 3,                           <--- add this
    "words" : [
      {
        "relevance_score" : 56,
        "points" : 66461,
        "bits" : 100,
        "word_combination" : "cat dog"
      },
      {
        "relevance_score" : 84,
        "points" : 45202,
        "bits" : 990,
        "word_combination" : "cat dog elephant"
      },
      {
        "relevance_score" : 99,
        "points" : 30974,
        "bits" : 70,
        "word_combination" : "elephant cat mouse leopard"
      }
    ],
    "group" : "whatever"
  },

Concerning the number of words (or tokens) in the word_combination field, there's a data type called token_count which exists exactly for this purpose. Simply define your mapping like this:
...
"word_combination": {
  "type": "text",
  "fields": {
    "count": {
      "type": "token_count",
      "analyzer": "standard"
    }
  }
}

Then in your query you can access word_combination.count which is going to contain the number of tokens (as analyzed by the specified analyzer) present in the word_combination field.
Comment: Thank you for your response. However, is there a way of doing this without adding the additional words_count field?


Erro ao mostrar o tempo passado de vários posts em PHP

Question: <?php
function timeAgo($time_ago){
$cur_time   = time();
$time_elapsed   = $cur_time - $time_ago;
$seconds    = $time_elapsed ;
$minutes    = round($time_elapsed / 60 );
$hours      = round($time_elapsed / 3600);
$days       = round($time_elapsed / 86400 );
$weeks      = round($time_elapsed / 604800);
$months     = round($time_elapsed / 2600640 );
$years      = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
    echo "$seconds segundos atrás";
}
//Minutes
else if($minutes <=60){
    if($minutes==1){
        echo "um minuto atrás";
    }
    else{
        echo "$minutes minutos atrás";
    }
}
//Hours
else if($hours <=24){
    if($hours==1){
        echo "uma hora atrás";
    }else{
        echo "$hours horas atrás";
    }
}
//Days
else if($days <= 7){
    if($days==1){
        echo "ontem";
    }else{
        echo "$days dias atrás";
    }
}
//Weeks
else if($weeks <= 4.3){
    if($weeks==1){
        echo "à uma semana";
    }else{
        echo "$weeks semanas atrás";
    }
}
//Months
else if($months <=12){
    if($months==1){
        echo "um mês atrás";
    }else{
        echo "$months meses atrás";
    }
}
//Years
else{
    if($years==1){
        echo "um ano atrás";
    }else{
        echo "$years anos atrás";
    }
}
}

?>
<?php
  $curenttime=$date_uploaded;
  $time_ago =strtotime($curenttime);
  echo timeAgo($time_ago);
?>

Sendo que $date_upload é igual a (dd-mm-aa hh:mm:ss) no banco de dados.
Esse é o código, quando eu puxo só um post ele funciona, mas quando puxo vários posts dá esse erro:

error: Cannot redeclare timeAgo() (previously declared in C:\xampp\htdocs\site\poster.php:520) in)
Comment: Parece que no arquivo `poster.php` tem uma função com o mesmo nome desta que vc postou (`timeAgo`), mas também pode ser problema com a inclusão de arquivos via `include`. Tente mudar o nome desta função, e se estiver usando `include`, tente mudar pra `include_once`.
Answer: O erro diz que existe duas ou mais vezes uma função com mesmo nome no seu arquivo ou na chamada de um include.
Para resolver o problema, sugiro que tenha pelo menos dois arquivos, um com o processamento normal do código e outro somente com funções um pequena biblioteca, dessa forma é evitada a colisão de nomes de função. Outra alternativa a partir do php5.3 é usar name espaces.
Existem algumas ferramentas que identificam duplicação de código e copy & paste como o php mess dectector, também a ajuda na identificação dessas colisões.
Exemplo - erro simulado.
Exemplo - solução com namespaces.
Comment: conseguir rsolver muito obrigado criei um arquivo da função chamado data.php e depois dei um include no topo da pagina include ('data.php'); depois eu apenas fiz um echo na pagina


Mixture of non-normals is normal?

Question: Does anyone have a simple example of a mixture of non-normal distributions that is normal?  To make life simple, pick specific $f_1(y)$ and $f_2(y)$, both with mean 0, such that $$(1/2)f_1(y) + (1/2)f_2(y) = \frac{\exp(-y^2/2)}{\sqrt{2\pi}}.$$
Such distributions would provide a neat counterexample to the commonly stated "fact" that normality of the regression residuals implies normality of the conditional distributions. Let $Y|X = 1 \sim f_1(y)$ and $Y|X =2 \sim f_2(y)$.  Further, let $X$ take the values 1 and 2 each with probability 1/2.  Then the marginal distribution of the true residuals is normal, while the conditional distributions of $Y$ are not.
(Comment: It is the marginal distribution of the residuals that is most often used to check the normality assumption.)
Comment: This is trivial, but a mixture of a half-normal and a negative half-normal is normal. Wouldn't really occur in practice but it's an example.
Comment: @Noah and I guess that would generalise to a mixture of any set of $n$ suitably truncated Normals being Normal.
Comment: @Noah, that example does not work. The residuals are differences from the mean.  So the mixture in your case will be the mixture of two half-normals shifted to a common mean of zero, resulting in a bimodal residual distribution.  That's why I said to make the two components have mean 0.
Comment: I'm not sure I understand the counterexample argument. If $X$ is included the model that you're fitting, then the model isn't linear and you can't fit it with a linear regression anyway (as the data are not Normally distributed around a mean that is a linear function of $X$). If $X$ is not included in the model, then there is no problem. $Y$ can then follow a Normal distribution around the remaining IVs in your model, and the residuals will converge to that distribution. Whether you could further model those residuals as a mixture of non-Normals is irrelevant to the regression.
Comment: Ruben van Bergen, of course my example is linear.  The conditional means of $Y$ are both zero, so that $E(Y| X=x) = \beta_0 + \beta_1 x$, where $\beta_0 = \beta_1 = 0$. The example can be easily adjusted to any values of $\beta_0, \beta_1$ by applying location shifts to the conditional distributions.  I also disagree about irrelevance. As I said, it is common statistical practice to conclude from the marginal normality of the residuals that the conditional distributions are normal. My counterexample shows that this practice is based on a faulty premise.
Answer: I can show you all examples, not just the simple ones.
Solution
Here they are, schematically:

The bottom panels show how the density function $f$ of a distribution $F$ is split into two parts vertically along a nearly arbitrary curve.  The cyan portion of the split is a fraction $\lambda$ of $f;$ the upper left plots its graph.  The remaining portion (gray) therefore is a fraction $1-\lambda$ of $f$ whose graph is plotted in the upper right.  This is how all mixtures arise.
(Notice that little is assumed about the density $f$ except that it exists.)

Details
The mixture distribution $F$ is Normal, which means there is a mean $\mu$ and variance $\sigma^2$ for which $F$ has a density function $f(z;\mu,\sigma).$  The details of $f$ don't matter!
Let $\lambda:\mathbb{R}\to[0,1]$ be any (measurable) non-negative function.  This means the following integrals involving $\lambda$ are defined and non-negative:
$$\pi_\lambda = \int_\mathbb{R} \lambda(z)f(z;\mu,\sigma)\,\mathrm{d}z \le \sup(\lambda)\,  \int_\mathbb{R}f(z;\mu,\sigma)\,\mathrm{d}z \le (1)(1)=1;$$
$$1-\pi_\lambda = 1 - \int_\mathbb{R} \lambda(z)f(z;\mu,\sigma)\,\mathrm{d}z = \int_\mathbb{R} (1-\lambda(z))_f(z;\mu,\sigma)\,\mathrm{d}z \le 1.$$
(The first inequality is an easy special case of Holder's Inequality.)
Define two distributions as
$$F_{\lambda}(x) = \frac{1}{\pi_\lambda}\int_{-\infty}^x \lambda(z)f(z;\mu,\sigma)\,\mathrm{d}z;$$
$$F_{1-\lambda}(x) = \frac{1}{1-\pi_\lambda}\int_{-\infty}^x (1-\lambda(z))f(z;\mu,\sigma)\,\mathrm{d}z.$$
It is straightforward to establish that these are distribution functions and, by construction,
$$F = \pi_\lambda F_\lambda + (1-\pi_\lambda) F_{1-\lambda}\tag{*}$$
exhibits the original normal distribution as a mixture of these two.
Conversely, whenever there exist differentiable functions with property $(*),$ then a version of $\lambda$ can be recovered via
$$\lambda(z) = \left\{\begin{aligned}\frac{F^\prime_\lambda(z)}{f(z;\mu,\sigma)} &\quad&f(z;\mu,\sigma)\ne 0\\
0 & &\text{otherwise}\end{aligned}\right.$$
and because $0 \le \pi_\lambda\le 1,$ the range of $\lambda$ is contained in $[0,1],$ QED.
Finally, it is possible for the component distributions to be Normal: for instance, when $\lambda$ is a constant function that will be the case.  That is the only possibility, though: see https://stats.stackexchange.com/a/429877/919 for the proof.

Application
As requested in comments, it would be of interest to choose $\lambda$ to meet a set of criteria, such as

Give the components equal weights, which means $$\frac{1}{2}=\pi_\lambda = \int \lambda(z) f(z)\,\mathrm{d}z.$$

Since these are intended to model errors in a regression setting (with $\mu=0,$ we would like each of the components also to have zero mean: $0 = E_{F_\lambda}[X].$ In light of (1), that is equivalent to $$0 = \int z\lambda(z) f(z)\,\mathrm{d}z.$$

Since regression errors are often assumed to be homoscedastic -- of equal variances -- we would like the variances of $F_\lambda$ and $F_{1-\lambda}$ to be equal.  Since they have means of zero, when $f$ is a Normal density, this is achieved when $$\sigma^2 = 2\int z^2\lambda(z) f(z)\,\mathrm{d}z.$$

Although there are many solutions to these equations, one simple (striking) solution is obtained by supposing $\lambda$ and $1-\lambda$ are both simple functions: that is, piecewise constant.  By making $\lambda$ symmetric around $0$ we can assure that (2) holds.  The simplest of such simple functions is zero except on some positive interval $[a,b]$ and its negative $[-b,-a],$ where it equals $1.$
Without any loss of generality take $\sigma^2=1,$ so that $f = \phi$ is the standard Normal density with the property $\phi^(z) = -z\phi(z).$ Using this fact we may compute
$$\int \lambda(z)\phi(z)\,\mathrm{d}z = 2 \int_a^b \phi(z)\,\mathrm{d}z = 2(\Phi(b)-\Phi(a))$$
(where $\Phi$ is the standard Normal distribution function) and
$$\begin{aligned}
\int z^2 \lambda(z)\phi(z)\,\mathrm{d}z &= 2 \int_a^b z^2\phi(z)\,\mathrm{d}z \\
&= 2(\Phi(b) - \Phi(a) + a\phi(b) - b\phi(b)).
\end{aligned}$$
This permits numerical solution of (1) and (3).  The work is streamlined by noting from (1) that, given $0 \le a\lt \Phi^{-1}(3/4),$
$$b = b(a) = \Phi^{-1}(\Phi(a) + 1/4).$$
That leaves us to solve (3) for $a \ge 0$.  Here is an R implementation to illustrate:
f <- function(a) {
  b <- qnorm(1/4 + q <- pnorm(a))
  pnorm(b) - q + a * dnorm(a) - b * dnorm(b) - 1/4
}
uniroot(f, c(0, qnorm(3/4)- 1e-6))$root -> a
qnorm(pnorm(a) + 1/4) -> b

This calculation gives $a \approx 0.508949$ and $b \approx 1.59466.$  Here are plots of the two component densities $f_\lambda$ and $f_{1-\lambda}:$

To illustrate the intended application, here are bivariate data with 150 responses at $X=0$ with errors distributed as $F_\lambda$ and 150 responses at $X=1$ with errors distributed as $F_{1-\lambda}.$  To the right is a quantile plot of the collected residuals.

Although separately neither group of residuals appears Normal, they are both centered at zero, have nearly the same variance, and collectively look perfectly Normal.

Remarks

The basic construction readily generalizes to mixtures with more than two components.

The example in the application can be extended, by using simple (indicator) functions supported on intervals $[a_i,b_i]$ with $0\le a_1 \lt b_1 \le a_2 \lt b_2 \cdots \lt b_k,$ to create component distributions that match the first $2k$ moments of the Normal distribution their mixture creates.  With sufficiently large $k,$ the component distributions will be difficult to discriminate even with largish datasets (at which point one might legitimately wonder whether their non-Normality matters at all).
Comment: Thanks for the edits! They illustrate the application perfectly. It is now perfectly clear that normality of the residuals does not imply normality of the conditional distributions.  Several folks should take note ...   (Aside:  This all came me after grading numerous regression projects where students would look at the residual q-q plot and conclude "approximately normal," even though $Y$ itself was highly discrete, with obviously non-normal conditionals for that reason.)
Comment: No problem!  I understand exactly where you're going with this.  You can view your latest criteria in terms of developing $\lambda$ in terms of an orthogonal basis of functions relative to the Normal measure (a problem closely related to the Hermite polynomials).Sticking with step functions, though, the simplest solution is to take $\lambda(z)$ to be the indicator of the interval $[0.50895, 1.5947]$ applied to $|z|.$ Thus, at one value of $X$ the absolute residuals will all be in this interval and at the other value of $X$ none of the absolute residuals will be in this interval.
Comment: Oh that is perfect. Delightful even. Can you give me the math forms of 0.50895 and 1.5947? Sorry for being dense if I am.
Comment: I apologize for being too brief.  I'll present these results in an edit to the answer.
Comment: I believe they do. I wonder what you mean by "continuous" components, though, because even when $\lambda$ is discontinuous, $F_\lambda$ is a continuous distribution.  I suspect what you have in mind might be the requirement that $f_\lambda$ is never zero. One way to achieve that is to express $F=cF+(1-c)F$ as a mixture of normals, $0\lt c\le1,$ and then take one of the components to be the mixture $cF_\lambda+(1-c)F/2.$ Thus, the only effective additional criterion is the symmetry one and that can be solved using a similar numerical technique (requiring at least four variables instead of two).
Comment: I worked out a solution: let $\lambda$ be supported on $(-\infty,-b)\cup(-a,0)\cup(a,b)$ where $a\approx 0.5576$ and $b\approx 1.4372.$ Although the component *densities* are not continuous, the component *distributions* are continuous and can be made everywhere positive by mixing in some amount of $F$ with them.
Comment: A nice extension that comes from my original motivation: Let $Y$ be binary and let $\beta_0 + \beta_1 X$ be a predictor. The residual is then $\epsilon = Y - \beta_0 -\beta_1 X$, and equal to  $1 - \beta_0 -\beta_1 X$ wp $p$ and $ - \beta_0 -\beta_1 X$ wp $1-p$. Assuming $Y$ has marginal probs 0.5 each, then by letting the distributions of  $1 - \beta_0 -\beta_1 X$  and $- \beta_0 -\beta_1 X$ be your components, then one has perfectly normal residuals even though $Y$ is binary.
Comment: Nice. Looking for a real simple example though, and especially one where the component means are equal (to 0, wlog).
Comment: It's easy to construct simple examples: use simple $\lambda.$  To make the means equal, why not focus on $\lambda$ that are symmetric around $\mu$?  That will guarantee all means equal $\mu.$  An intriguing example is obtained by taking $\lambda$ to be the indicator of an interval $[\mu-\Phi^{-1}(1/4)\sigma,\mu+\Phi^{-1}(3/4)\sigma],$ which by construction will make $\pi_\lambda=\pi_{1-\lambda}=1/2.$ In effect, one component of $F$ consists of the center of the Normal distribution and the other is its two tails. That about as extreme a division as one can contemplate.
Comment: Oh yes. Now I see it.  Half in, half out, both symmetric even. But now that I think about it, it would be even nicer in terms of "startle factor" for regression application if there was homoscedasticity, i.e., if the means *and* the variances are equal as well.
Comment: At the risk of wearing out my welcome, would it be possible to have the components  (i) be continuous, (ii) have means zero, (iii) have standard deviations 1.0, and (iv) be reflections of one another ($f_1(y) = f_2(-y)$)?  Do such animals even exist?
Answer: A very simple example from the Skew normal distribution with density
$$ 2\phi(x)\Phi(\alpha x) $$
Choose for the twocomponents $\alpha, -\alpha$ then
$$ \frac12 2 \phi(x) \Phi(-\alpha x) + \frac12 2 \phi(x) \Phi(\alpha x) $$
is the standard normal density $\phi(x)$, by using symmetry, since $\Phi(-\alpha x) = 1-\Phi(\alpha x)$, but unfortunately the two mixture components do not have equal mean.
A simple example with equal means is got by exploiting $1=\sin^2 x +\cos^2 x$ so simply define mixture components by
$$ \phi(x) = \sin^2(x) \phi(x) + \cos^2(x) \phi(x) $$ and both components have mean zero.
Comment: That's the kind of simple I was looking forward, but the component means are not equal, are they? They have the same sign as \$alpha$, right? It is most crucial for the regression application that the means of the components be equal (to 0, wlog).
Comment: Thanks. whuber has a good example that works to make the point, but I like the idea of finding simple continuous ones (with equal means and variances) for a more realistic counterexample.
Comment: They are not equal, unfortunately. I will try to find some simple example that have equal means, but ...

Mixture of non-normals is normal?


Can Mac App Store Sandboxed apps use CGEventPost?

Question: For an app I am working on I am using CGEventPost to post some key events and I noticed that it stopped working with sandboxing enabled, even I turned on the "Enable Access for Assistive Devices" system preference.
Is there an entitlement to allow the use of CGEventPost? Or, is there another way to post key events with apps submitted to the Mac App Store?
Answer:
Can Mac App Store Sandboxed apps use CGEventPost?

No.

Is there an entitlement for CGEventPost? Or is there any way to post key events with apps submitted to the Mac App Store?

No.
If you look at the developer forums, Apple has made it very clear that they will never enable an entitlement for this, because it allows a blatantly obvious way for any app to escape the sandbox.
If you have some more specific need that you can justify (not "post some key events" but "get iPhoto do to XXX, which as far as I know can only be done by posting event YYY at it"), file a bug report, and post a thread on the dev forums.
This will do two things:

Someone at Apple may suggest a workaround--possibly a private one that nobody else can tell you and/or one that isn't normally allowed on the App Store but will be accepted if you've been told to use it.
It increases the chances that Apple will invent some better way to do what you want in the future.
Answer: Old question, but accepted answer is wrong. There are apps in Mac App Store that are using CGEventPost to simulate events. For example apps "Remote Mouse" and "Mobile Mouse Server" do that. They can send input to any other app and based on symbols in those apps they are clearly using CGEventPost-function. I would like to know how are they doing it.
Comment: If you download both of your mentioned apps you will see they are not sandboxed. I am assuming that they submitted them to apple before Sandboxing was enforced and have just submitted bug fixes since then (which is allowed per Apple's sandboxing rules). So I am assuming that they are using CGEventPost - which is not allowed for any new app on the Mac App Store. I have emailed the creators though, just in case, to see what it is they are doing.
Comment: Sandboxing was not required until June 1, 2012. Any app submitted after that date must be sandboxed. Any app before that date can stay on the App Store in an un-sandboxed mode but only bug fixes can be submitted to Apple. They are not allowed to do any feature updates.
Comment: I found two new apps which have the sandbox entitlement: "Computer Mouse" (com.highsolutions.iPhoneMouse) and "Remmo" (com.applace.remmo).
Comment: Thanks for the clarification. So rules are not the same for all apps then.
Comment: Yeah and if I want to write similar app like Remote Mouse I cannot distribute it via Mac App Store and even more I cannot autorun it at system startup!? It is stupid.
Comment: @DimitarNestorov yeah... something going on and I'm not sure what. Seems like some app get accepted while they use this or similar API to simulate user input.
Comment: I know for sure that `CGEventTapPostEvent` works in sandboxed apps, but you need to be granted permission in System Settings > Privacy & Security > Accessibility.  CGRequestPostEventAccess asks the user for that permission.


Como inserir uma imagem no meu doc html para que sirva como imagem de fundo(background)

Question: Cumprimentos. Gostaria de saber como inserir uma imagem em meu documento html para que a mesma sirva como background no meu site. A imagem encontra_se no desktop do meu PC com o nome "backg.jpg". Agradecia.
Answer: Utilize CSS para atingir o resultado pertendido background-image: url(CAMINHO_DA_IMAGEM).
Encontre em baixo um exemplo de como utilizar CSS para inserir uma imagem de fundo em toda a pagina.

<!DOCTYPE html>

   <title>Teste de imagem de fundo</title>

Considere utilizar um servidor virtual, por exemplo XAMPP para criar seus projetos em um ambiente +/- parecido com um servidor web real.
Evite criar projetos diretamente do seu Ambiente de Trabalho (Desktop).
Answer: css
   body
    {
         background-image: url("caminho_da_imagem.extensao");
    }

Answer: Quando eu comecei tinha dificuldade para entender como referenciar imagens e arquivos, se você esta usando windows e a imagem esta na área de trabalho você vai fazer mais ou menos isso, c:\usuarios\seu-usuario\desktop\nome-imagem.jpg não faça isso!
Eu aconselho utilizar a imagem na mesma pasta do index.html, você pode fazer a seguinte estrutura:

nome-pasta-projeto/

index.html
estilo.css
img/

bg.jpg

E no css fazer o seguinte:
body
{
     background-image: url("img/bg.jpg");
}

Como pode ver no css nós chamamos o bg.jpg dentro da pasta img
Observe sempre aonde estão seus arquivos e terá muito menos confusão.


Signal-r installation issue

Question: I'm trying to install SIgnal-r library into my project using :
<code>Install-Package Microsoft.AspNet.SignalR
</code>
provided here:http://signalr.net/ but i am having this error

Can anyone please tell me how I can fix this problem
Answer: SignalR 2.0 doesn't work on .NET 4.0 projects. You need to install SignalR 1.1.3. See http://www.asp.net/signalr/overview/signalr-1x/getting-started/tutorial-getting-started-with-signalr for more details.

Signal-r installation issue


Smallest constant of Lipschitz retraction from bounded to continuous functions

Question: Let $B$ be the space of all bounded functions $f:[0,1]\to\mathbb R$
equipped with the supremum norm*. It contains $C$, the space of
continuous functions on $[0,1]$, as a subspace. An $L$-Lipschitz
retraction $R:B\to C$ is, by definition, a map such that
$$R(f)=f \qquad \forall f\in C$$ $$\|R(f)-R(g)\|\le L\|f-g\| \qquad
\forall f,g\in B$$

Question: Does there exist a $2$-Lipschitz retraction from $B$ onto $C$?

I expect the answer to be negative, but so far was unable to find a
suitable obstruction.

Motivation

It is known that there is a $20$-Lipschitz retraction; such a map
(pretty complicated) is constructed in Theorem 1.6 in Geometric
Nonlinear Functional Analysis by Benyamini and Lindenstrauss. The
constant $20$ could probably be lowered by tweaking their
construction, but I am not interested in that right now.
There is no $L$-Lipschitz retraction for $L<2$. (Proof below).
The constant $2$ is known to be smallest possible for Lipschitz
retraction from $\ell_\infty$ onto $c_0$ (Example 1.5 in the same
book).

Proof of item 2 above. For $n\in \mathbb N$, let $f_n\in C$ be a
function such that $f_n(x)=-1$ for $x\le \frac12-\frac1n$, $f_n(x)=1$
for $x\ge \frac12+\frac1n$, and $f_n$ is linear in between. Let
$f(x)=\frac12 \operatorname{sign}(x-1/2)$. Note that $\|f-f_n\|\le
1/2$ for all $n$. Here is an illustration: $f$ in red, $f_n$ in blue.

The function $R(f)$ must satisfy $\|R(f)-f_n\|\le L/2$ for all $n$.
Therefore, $R(f)(x)\ge 1-L/2$ for $x>1/2$ and $R(f)(x)\le -1+L/2$ for
$x<1/2$. Since $R(f)$ is continuous, it follows that $L\ge 2$.
Remark on item 3: a $2$-Lipschitz retraction from $\ell_\infty$ onto $c_0$ is obtained by mapping each $(x_n)\in \ell_\infty$ to $(x_n-\min(|x_n|,s)\operatorname{sign}x_n)$ where $s=\limsup|x_n|$. Since $s$ is a $1$-Lipschitz function on $\ell_\infty$, the resulting map is $2$-Lipschitz.

(*) That is, $\|f\|=\sup_{[0,1]}|f|$. Note that $B$ is different
from $L^\infty[0,1]$ because the functions that are equal a.e. are not
identified, nor is there any measurability requirement.
Comment: When $f$ is the step function in my post, $D(f)=1/2$, so $K(f,t)$ is the smallest $\lambda$ for which the upper $\lambda$-Lipschitz envelope of $f$ is within $1+t/2$ of $f$. Hence $K(f,t)$ blows up like $t^{-1}$  as $t\to 0$, not integrable. But this is not a problem for the proof, because it's not $K$ that gets integrated; rather, those upper envelopes get averaged.
Comment: Do you know if the function $K(f,t)$ defined in Lindestrauss book is integrable, for fixed $f$ and $t\in (0,1)$?
Answer: It seems that Nigel Kalton gave an answer to your question. He proved in [1], theorem 3.5,  that for every compact metric space $K$, $C(K)$ is a $2$-absolute Lipschitz retract.
[1]: Kalton, N. J. Extending Lipschitz maps into C(K)-spaces. Israel J. Math. 162 (2007), 275–315. Link to article

Added by OP: For completeness, I'll outline the steps of Kalton's proof. Given $f\in B$, Let $Lf$ be its lower semicontinuous regularization, defined by
$$Lf(x) = \liminf_{y\to x} f(y)$$
Clearly, the map $f\mapsto Lf$ is a $1$-Lipschitz map of $B$ to itself, fixing $C$. Same holds for the upper semicontinuous regularization, denoted $Uf$.
For any $g\in C$ we have $f\ge g-\|f-g\|$. Since the right hand side is continuous, $Lf\ge g-\|f-g\|$. Similarly, $Uf\le g+\|f-g\|$. It follows that $Uf-Lf\le 2\|f-g\|$. Taking the infimum over $g\in C$ yields
$$Uf-Lf\le 2\operatorname{dist} (f,C)$$
Hence, the pair of functions $F_u(f) = Uf-\operatorname{dist} (f,C)$ and $F_l(f) = Lf+\operatorname{dist} (f,C)$ satisfies

$F_u(f)\le F_l(f)$
$F_u(f)$ upper semicontinuous, $F_l(f)$  lower semicontinuous
The operators $F_u$ and $F_l$ are $2$-Lipschitz, and restrict to the identity on $C$.

Consider the set of pairs $(h_1,h_2)\in B\times B$  such that $h_1$ is usc, $h_2$ is lsc, and $h_1\le h_2$. The main step of the proof is a $1$-Lipschitz retraction of this set onto its diagonal $\{(h,h) : h\in C\}$. This can be done via an iterative process, since the Lipschitz constant $1$ is preserved under composition. For the purpose of this construction, Kalton  replaces a general compact set $K$ with the Cantor set, due to universality of the latter.


How to Convert UTC time to local time In Swift 3?

Question: Here , I want to convert the UTC time to Local time formate in swift 3, but i'am not able to convert it. Please help me for this
let StrDate = "2018-03-06T13:32:57 +05:30" I want to Convert this Date into local time format.
 func convertStringDateFormateUTC(strDate: String,
     strCurrentFormateType:String, strFormateType:String) -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = strCurrentFormateType
            var strNewdate = NSDate()
            strNewdate = dateFormatter.date(from: strDate)! as NSDate
            dateFormatter.dateFormat = strFormateType;
            dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
            return dateFormatter.string(from: strNewdate as Date)
        }

Comment: Possible duplicate of [Swift 3.0 : Convert server UTC time to local time and visa-versa](https://stackoverflow.com/questions/42803349/swift-3-0-convert-server-utc-time-to-local-time-and-visa-versa)
Answer: Use TimeZone.current to detect local timezone and apply it.
let formatter = DateFormatter()
// initially set the format based on your datepicker date
var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "UTC" }
//print(localTimeZoneAbbreviation)
formatter.locale = Locale.init(identifier: localTimeZoneAbbreviation)
// convert your string to date
let yourDate = formatter.date(from: strDate)
//then again set the date format which type of output you need

In your function  :
func convertStringDateFormateUTC(strDate: String,
                                 strCurrentFormateType:String, strFormateType:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = strCurrentFormateType

    var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "UTC" }
    dateFormatter.locale = Locale.init(identifier: localTimeZoneAbbreviation)

    var strNewdate = NSDate()
    strNewdate = dateFormatter.date(from: strDate)! as NSDate
    dateFormatter.dateFormat = strFormateType;
    dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
    return dateFormatter.string(from: strNewdate as Date)
}

Comment: When i calculate time it shows it shows 1 hour extra ex. 6:00:00 +5:30 UTC time then I get 12:30 but i think i get 11:30 . Is it Right or not ?
Comment: Its right.  6:00:00 +5:30 UTC means  12:30 because in UTC its adds  +5:30 hr


Long Comments e Long Strings

Question: Gostaria de uma explicação apropriada sobre o que são Long Comments e Long Strings, apesar de que possam ser pequenos recursos.
Answer: Long Comments (comentários longos) são comentários utilizando Long Format. Long Strings (strings longas) representam strings, mas também utilizando o Long Format.
Long Format
Long Format (formato longo) é um formato que diferencia entre as versões -5.1 e +5.2.
Versão 5.1
Nessa versão o formato inicia com a sequência [[ e termina com a sequência ]] relacionada (os mesmos pares podem se repetir dentro).
Versão 5.2
Nessa versão o formato recebe um nível de fechadura. O formato apenas inicia se o seu início é válido. O início começa com [ e conta os símbolos = até que apareça ] para finalizar ele mesmo. Caso houve qualquer caractere fora = e [ , então o formato não inicia.
O nível de fechadura será a quantia de assigns (=).
O fim do formato é semelhante ao início, só muda [ para ] e requere a mesma quantia de assigns.
Long Comment
Nada especial sobre seu formato, além de que o formato deve ser declarado depois de --.
Long String
O formato da string longa não interpreta o escape (\) , requere fim e tem duas ações de fundo:

Converte a sequência de bytes 0D 0A (carriage return e new line, \r\n) e o byte 0D (\r) para 0A (\n)

Ignora a primeira quebra de linha (0D 0A, 0A ou 0D) seguida pelo seu início

O interpretador da linguagem assume que os bytes do código alvo são códigos de caracteres, por isso qualquer futuro problema se relaciona à codificação de seus caracteres.

Long Comments e Long Strings


how i can create 3d input / 3d output Convolution model with keras?

Question: I have a bit question i couldnt solve.
I wanna implement CNN model with fully-connected MLP to my protein database which has 2589 proteins. Each protein has 1287 rows and 69 columns as input and and 1287 rows and 8 columns as output. Actually there was 1287x1 output, but i used one hot encoding for class labels to use crossentropy loss in my model.
Also i want
if we consider as image i have an 3d matrix ** X_train = (2589, 1287, 69) for input** and y_train =(2589, 1287, 8) output, i mean output is also matrix.
Below my codes of keras:
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation="relu", input_shape=(X_train.shape[1],X_train.shape[2])))
model.add(Conv2D(32, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense((8), activation="softmax"))

But I encountered with Error about Dense layer :
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (2589, 1287, 8)

Ok, i understand that Dense should take positive integer unit (explanation in Keras docs. ). But how i can implement matrix output to my model ?
I tried that:
model.add(Dense((1287,8), activation="softmax"))

and something else but i couldnt find any solution.
Thanks very much.
Answer: The Conv2D layer requires an input shape of (batch_size, height, width, channels). This means that each sample is a 3D array.
Your actual input is (2589, 1287, 8) meaning that each sample is of shape (1289, 8) - a 2D shape. Because of this, you should be using Conv1D instead of Conv2D.
Secondly you want an output of (2589, 1287, 8). Since each sample is of a 2D shape, it makes no sense to Flatten() the input - Flatten() would reduce the shape of each sample to 1D, and you want each sample to be 2D.
Finally depending on the padding of your Conv layers,the shape may change based on the kernel_size. Since you want to preserve the middle dimension of 1287, use padding='same' to keep the size the same.
from keras.models import Sequential
from keras.layers import Conv1D, Flatten, Dense
import numpy as np

X_train = np.random.rand(2589, 1287, 69)
y_train = np.random.rand(2589, 1287, 8)

model = Sequential()
model.add(Conv1D(64,
                 kernel_size=3,
                 activation="relu",
                 padding='same',
                 input_shape=(X_train.shape[1],X_train.shape[2])))
model.add(Conv1D(32,
                 kernel_size=3,
                 activation="relu",
                 padding='same'))
model.add(Dense((8), activation="softmax"))

model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(X_train, y_train)

Comment: Im too greateful man, thanks very much. Its well explicative comment. i thought  2d mean like matrix (2dim). Thanks very much again.
Comment: Hi, if this answer solved your problem could you please mark it as accepted by clicking the check mark on its side? Thanks.
Comment: can i ask a questiın too. What about Pooling layer ? When i use pooling layer, i get error about dimension size. I mean each input has (1287,69) and each class label (1287,8). But after the pooling layer, output shape will be reduced, how i can solve this problem ?

For example when i 2 convol layer and 2 pooling layer (with 2 size), i got this error : expected dense_2 to have shape (322, 8) but got array with shape (1287, 8)
Comment: Pooling layers will reduce the size of it's input. The error is caused when the pooling layer reduces the size but your y vector is still the original size. Use model.summary() to see the shapes at each layer on your model.