Jquery limit text in text field copy safe

Question: Here is a mind Boggling question i am having trouble with. I have a text field. This text field needs to accommodate U.S mobile numbers with 4 formats

XXX-XXX-XXXX
XXXXXXXXXX
X-XXX-XXX-XXXX
XXXXXXXXXXX

When the last character is entered it needs to do a check via ajax which i have no problem with.
I need to check if the textbox has the full mobile number in when typing, copying and pasting, deleting and every possible way to put numbers in that field.
i tried checking it with $("#id").keyup but that doesn't work when you paste something.
is there a way to check either via interval or something else if there is 10 or eleven characters in that field regardless of how it got there? [edit]
Here is a solution i found
<code>var checkMsisdnInterval = window.setInterval( function () {
    var msisdn = getdigits($("#newSessionMsisdn").val());

    if(msisdn.length == 10 && !checked) {
        doPreCheck(msisdn);
        checked = true;
    } else if(checked == true) {
        //do something here
    } else {
        //do something else
    }
}, (1000 * 60 * 0.1));

$("#newSessionMsisdn").keyup(function(){
    checked = false;
});

function getdigits (s) {
   return s.replace (/[^\d]/g, "");
}
</code>
it works like a bomb. Thanks for all the replies
Answer: Why not checking it in the ajax call, use the beforeSend method and check the length of the input field.
For your reference, http://api.jquery.com/jQuery.ajax/
Comment: that will not work. For example a person starts typing in the mobile number. When it get to the length it needs to be at it disables the text field and does an ajax call to see if the person is opted in without clicking buttons
Answer: Using jQuery, have you looked at the change event?
http://api.jquery.com/change/
Comment: Does the .change event work with text fields. I was under the impression it only works with select boxes
Comment: The .change event doesn't work with my text field or it takes 20 seconds to register.
$("#newSessionMsisdn").change(function(){checked = false;});


Find $\int x\times\sqrt{8-x^2} \,dx$

Question: $$\int x\times\sqrt{8-x^2} \,dx = \,?$$
I got to this:
$$\int\sqrt{8x^2-x^4} \,dx$$
or:
$$\int\frac{8x-x^3}{\sqrt{8+x^2}}\, dx$$
I don't know how to integrate neither. If possible, no $\sin$ \ $\cos$ \ etc.
Comment: Have you seen the method of substitution before, sometimes called $u$-substitution?
Answer: Make the substitution $u=8-x^2$, and you'll get an easy integration.
Answer: Or directly:
$$\int x\sqrt{8-x^2}\,dx=-\frac{1}{2}\int\sqrt{8-x^2}\,d(8-x^2)=-\frac{1}{2}\frac{2}{3}(8-x^2)^{3/2}+C$$

Find $\int x\times\sqrt{8-x^2} \,dx$


Bootstrap framework - Sub-theme template.php not having any effect

Question: I installed bootstrap framework for drupal and copied the sub-theme into my themes directory, renamed all the files accordingly and everything works fine, however any php code that i put in my sub-theme template.php file is not having any effect.
I tried adding google fonts, css files and some other functions to template.php but nothing works, even tho the code is valid.
I wonder what am i doing wrong? Should i be editing base theme template.php file, even tho it will get updated and deleted? Or is there some other reason why my sub-themes template.php has no effect?
Comment: Active theme's template.php always runs, as well as the active theme's base theme's template.php. However the active theme's template.php should be overriding base theme's template.php. Your case is not a default behavior, so it's probably something local to your instance of Drupal, whether it be a combination of either a module, a particular theme or custom code that you've written.
Answer: If you change in template.php then you have to clear the theme cache. Because the contents of the this file is cached in the database, so altering it will not be noticed by Drupal.
You can clear cache from here
Drupal 7: Administration > Configuration > Development > Performance (admin/config/development/performance)
For more information visit Clearing the theme cache
Comment: I cleared all caches after doing updates to template.php. I wonder if clearing theme cache is different from clearing all caches?
Comment: [`drupal_flush_all_caches()`](https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_flush_all_caches/7)
Answer: Don't edit the files in the bootstrap base theme - they will get overwritten when you update to a new version just as you said. Only edit and add files to the subtheme.
About the changes not showing up: Is it possible that you are using the wrong theme hooks or no hooks at all? Just using functions like drupal_add_css() or drupal_add_js() wont work in the template.php unless you call them from a theming hook like template_preprocess_page().
Here is a good tutorial to preprocess functions: Implementing Preprocess and Process Hooks
If you want to generally add a css file to theme (not just to certain templates as you could with the preprocess functions) however, I would suggest adding them in the .info file of your subtheme:
<code>#original bootstrap css files in your subtheme
stylesheets[all][] = css/style.css
stylesheets[all][] = css/jquery.cover.css

#custom sheet
stylesheets[all][] = css/YOURCSS.css
</code>
If you intend to use the less files bootstrap ships with hwever, there is no need to even do that: The bootstap subtheme supplies you with a number of less files you can compile into css for your theme using a programm such as crunch. The file you have to compile is the style.less file, which references all other less files. The output is a single style.css file, the one references above. You can either use the less files the subtheme provides or add new ones.


Best way to make an EntityFieldQuery query be case sensitive?

Question: The case-sensitivity of queries created using EntityFieldQuery rely on the underlying database's collation [*]. So if the collation for a particular field is 'utf8_general_ci', then all the queries created with EntityFieldQuery are case insensitive.
Given that it's not always possible to change the schema (say, if you are developing a module that acts on third party fields) how can you ensure case sensitivity when using EntityFieldQuery ?
My solution so far consists of using a tag and a hook_query_TAG_alter hook to cast the value to a BINARY value (and EFQ allows us to create our own operator for this). Here is an example:
function mymodule_query_case_sensitive_alter($query) {
  foreach ($conditions as $index => $condition) {
    if (is_array($condition) && $condition['operator'] == '=c') {
      $placeholder = ':db_condition_placeholder_' . $query->nextPlaceHolder();
      $conditions[$index]['field'] = 'CAST(' . $conditions[$index]['field'] . ' AS BINARY) = ' . $placeholder;
      $conditions[$index]['value'] = array($placeholder => $conditions[$index]['value']);
      $conditions[$index]['operator'] = NULL;
    }
  }
}

Then one could run:
$r = (new EntityFieldQuery())
  ->fieldCondition('field_my_field', 'value', 'ExaMple', '=c')
  ->addTag('case_sensitive')
  ->execute();

Based on this, my questions are:

It is quite cumbersome. Is there really no simpler way ?
Does such a tag already exist ?
This is limited to MySql field storage backend - how could it be adapted to work on other field storage backends ?

[*] I am assuming a MySql field storage backend here.
Comment: The slip was on the word 'engine', not 'storage'. The right word should have been 'backend' - Drupal allows different storage backends for fields, and these are not necessarily SQL based. See https://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_storage_info/7 for instance. (question edited accordingly)
Comment: I didn't want to be mean of course, I commented for precision's sake, and only after answering.
Comment: Comments are welcome, and now the question is clearer thanks to you :)
Answer: MySQL documentation shows you can simply define collation at a query level:
SELECT k COLLATE latin1_german2_ci AS k1;

 SELECT *
 FROM t1
 WHERE _latin1 'Müller' COLLATE latin1_german2_ci = k;

 SELECT *
 FROM t1
 WHERE k LIKE _latin1 'Müller' COLLATE latin1_german2_ci;

If collation is only thing that affects case sensitivity for you, that's the way to go, no need to cast anything.
Based on this, you can set the operator in the EntityFieldQuery to be 'COLLATE utf8_general_cs ='. For instance the following will work:
$r = (new EntityFieldQuery())
  ->fieldCondition('field_my_field', 'value', 'eXample', 'COLLATE utf8_general_cs =')
  ->execute();

This remains limited to MySQL field storage backend however.
Comment: @Alice I was just writing code like that, and couldn't save it due to your edit pending ;)
Comment: As a small addition: the uf8 general case sensitive collation is 'ut8_general_cs', however that is not always enabled (depending on version of MySql) so an alternative is 'utf8_bin'.
Comment: @AliceHeaton Corrected. Where was my mind?
Answer: Drupal 8 core patch documentation https://www.drupal.org/files/issues/entity-query-case-2068655-127_0.patch

Drupal considers LIKE case insensitive and the following is often used
to tell the database that case insensitive equivalence is desired:
@code
db_select('users')
->condition('name', db_like($name), 'LIKE')
@endcode
Use 'LIKE BINARY' instead of 'LIKE' for case sensitive queries.


Retrieve newly created Term tid

Question: I've created a PHP program outside Drupal that inserts Terms into my Drupal installation.
define('DRUPAL_DIR', __DIR__);

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();

require_once DRUPAL_DIR . '/core/includes/database.inc';
require_once DRUPAL_DIR . '/core/includes/schema.inc';

$term = \Drupal\taxonomy\Entity\Term::create([
      'vid' => 'subject',
      'langcode' => 'en',
      'name' => 'Test af Tag',
      'description' => [
        'value' => '
My description.
',
        'format' => 'full_html',
      ],
      'weight' => -1,
      'parent' => array(0),
])->save();

var_dump($term);

My question is - how do I retrieve the newly created Term tid?
The var_dump only gives a

string(9) "SAVED_NEW"

Why on earth doesn't it return the newly created Entity/Term?!?
I need it in case there will be a child Term later.
I've looked at www.drupal.org/documentation and api.drupal.org. But those pages just confuses me more :-(
Thank you very much
Answer:
Why on earth doesn't it return the newly created Entity/Term?!?

Why would it? You already have the term object, why do you need it to be chained in a return from a save method that can and does provide you with extra context??
Just use this:
$term = \Drupal\taxonomy\Entity\Term::create([
  'vid' => 'subject',
  'langcode' => 'en',
  'name' => 'Test af Tag',
  'description' => [
    'value' => '
My description.
',
    'format' => 'full_html',
  ],
  'weight' => -1,
  'parent' => array(0),
]);

$inserted_or_updated = $term->save();

$tid = $term->id();

Comment: Thank you very much - I learn a new important thing today. :-)

Retrieve newly created Term tid


Can't ping remote device via Bluetooth Low Energy using routable IPv6 address

Question: I have a simple CoAP Server on a device that implements the BLE Internet Protocol Support Profile that I wanted to test using Mozilla Firefox + Copper plug-in. So far I have managed to connect to my device, and ping it using its link local address. Unfortunately, Firefox cannot access link-local addresses, so in order to use that I have to configure my device with a routable address.
I have this config file... /etc/radvd.conf :
interface bt0
{
    AdvSendAdvert on;
    prefix 2001:db8::/64
    {
        AdvOnLink off;
        AdvAutonomous on;
        AdvRouterAddr on;
    };
};

Then I set an address in the subnet for the linux box, enabled forwarding and restarted radvd.
sudo ip address add 2001:db8::1/64 dev bt0
echo 1 | sudo tee /proc/sys/net/ipv6/conf/all/forwarding
sudo service radvd restart

This is the output from ifconfig bt0:
bt0       Link encap:UNSPEC  HWaddr 00-15-83-FF-FE-E8-EB-0B-00-00-00-00-00-00-00-00
          inet6 addr: fe80::215:83ff:fee8:eb0b/64 Scope:Link
          inet6 addr: 2001:db8::1/64 Scope:Global
          UP POINTOPOINT RUNNING MULTICAST  MTU:1280  Metric:1
          RX packets:111 errors:0 dropped:0 overruns:0 frame:0
          TX packets:116 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:8553 (8.5 KB)  TX bytes:10804 (10.8 KB)

Unfortunately something is still incorrectly configured. I can still ping my device using local link address ping6 fe80::215:83ff:fee8:eb0b%bt0, but when I want to ping it with its routable address, ping6 -I bt0 2001:db8::215:83ff:fee8:eb0b, I have no responses.
What could be the problem?
IS there anything else I need to configure?
EDIT
New radvd.conf
interface bt0
{
    abro fe80::215:83ff:fee8:eb0b/64 {
        AdvVersionHigh 10;
        AdvVersionLow 2;
        AdvValidLifeTime 2;
    };
    AdvSendAdvert on;
    prefix 2001:db8::/64
    {
        AdvOnLink off;
        AdvAutonomous on;
        AdvRouterAddr on;
    };
};

Comment: when I'm troubleshooting virtual networks I use 'route -n', 'ping I bt0' and tcpdump, 'ip link show' and iptables - usual gotchas include - the device is not up, firewall rules, NAT rules.
Answer: It appears that your radvd configuration is missing the ABRO options, required for your system to be recognized as a 6LoWPAN border router.
A minimal configuration would be:
interface bt0 {
    abro fe80::215:83ff:fee8:eb0b/64 {
        AdvVersionHigh 10;
        AdvVersionLow 2;
        AdvValidLifeTime 2;
    }
    #...everything else

Note that in this stanza you need to use the link-local address of the border router.
And of course your devices must be Bluetooth 4.1 (or higher). 4.0 devices will not work.
Comment: I have made and edit to show what my new radvd.conf looks like. I sill can't ping remote device. BLE dongle that is connected to my notebook via USB is 4.0, might this fact be a problem that I am not able to ping remote device using routable address but I can ping it using link local adress?
Comment: @AlBundy All the devices have to be 4.1 for a complete IPv6 implementation. See [RFC 7668](https://tools.ietf.org/html/rfc7668) for further details.


Webform Serial - Serial Numbers updating after editing submission

Question: I am using webform serial to generate tracking numbers for webform submission.
If I edit a submission the serial number auto increments to the next available number. How can I stop this from happening and have the number locked - even when I edit a submission?
Answer: I was going to suggest placing an issue on the project page-it seems like it would be a common issue-but you've already done so. If the maintainer doesn't respond, you could look at the code yourself to figure out why and create a patch for it.


How to reduce the final set of significant variables from logistic model?

Question: I have built a logistic model, which has 40 significant predictors, p value<0.0001.
I want to reduce them to say about 10 variables, so that it can be presented to business. How do i go about doing that?
Answer: See Harrell (2001), Regression Modeling Strategies, Ch. 5.4, pp 99 – 101.
Fit a linear model to the predicted values (on the logit scale), & use a backward stepwise procedure, stopping when the coefficient of determination falls below say 95%, or when you've reduced the predictors by as many as you wanted to. You've then got a straightforward measure of how much predictive power you've lost in the selection. (And if you're doing penalized regression the shrinkage will automatically be passed on to the approximate model.) Rough & ready inference can be based on a covariance matrix for the coefficient estimates derived from that of the full model.
All the same I'd still usually prefer presenting the most salient features of the full model & ignoring details. Plotting the relation of the most important predictors to the response at typical values of the others usually gets the idea across.
Answer: The following approach is computationally intensive, but it's intuitive and will achieve the goal that you're after.
First step is to split your dataset into three partitions: Train, Test, and Validate.
For the Train partition, follow these steps:
(1) For each one of the 40 predictors, build a one-variable model. Select the best one-variable model based on a statistical criteria (e.g., AUC).
(2) For each of the remaining 39 variables, add it to the best one-variable model to create 39 two-variable models. Select the best two-variable model based on the same  statistical criteria.
(3) For each of the remaining 38 variables...
..
..
(40)
At the end of this process, you will have 40 models: best one-variable model, best two-variable model, and so on.
Using each one of these models, score the Test partition (i.e., apply the same algorithm on the Test partition) and calculate the same statistical measure that you chose to select the best models in steps 1 thru 40.
Now you have 40 pairs of a statistical measure. Plot them on a line chart and find the inflection point (elbow/bend) in the curve. You will notice that the metric improves as you add more features to the model, but the improvement rate will decrease/diminish as you keep adding more features. Also, the gap between your Train and Test measures would widen as more features are added.
Your best (parsimonious) model is where you detect the bend in the curve.


Merge two Excel tables Based on matching data in Columns

Question: I've been working on a excel problem, that I need to find an answer for I'll explain it below.
I've Table01 with the Columns :

Group No
Name
Price

I've Table02 with the columns:

Group No
City
Code

I've merged two tables of Table01 & Table02 as shown in the Image03 , But without order.
But,as you see Group No Column is similar in both tables.
What I need is to get the matching rows of Table01 & 02 considering 'Group No' Column.
The Final result is to be seen as the final image.
Is there a way to do this with excel functions ?

Thank You!
Comment: convert both into tables and use match and index functions.
Answer: Put the table in the second image on Sheet2, columns D to F.
In Sheet1, cell D2 use the formula
<code>=iferror(vlookup($A2,Sheet2!$D$1:$F$100,column(A1),false),"")
</code>
copy across and down.
Edit: here is a picture. The data is in two sheets. On Sheet1, enter the formula into cell D2.   Then copy the formula across to F2 and then down as many rows as you need.
Comment: if your rows are greater than 100, then change the value 100 accordingly.
Comment: Thank You for the Answer But Can you please explain this a little more @teylyn
Comment: to which cell that I should write this function @ teylyn
Comment: Interesting. A downvote a year after the OP accepted the answer. Care to elaborate?
Comment: This was just to help out people how might come across a similar situation.
Answer: Teylyn's answer worked great for me, but I had to modify it a bit to get proper results. I want to provide an extended explanation for whoever would need it.
My setup was as follows:

Sheet1: full data of 2014
Sheet2: updated rows for 2015 in A1:D50,
sorted by first column
Sheet3: merged rows
My data does not have a header row

I put the following formula in cell A1 of Sheet3:
<code>=iferror(vlookup(Sheet1!A$1;Sheet2!$A$1:$D$50;column(A1);false);Sheet1!A1)
</code>
Read this as follows: Take the value of the first column in Sheet1 (old data). Look up in Sheet2 (updated rows). If present, output the value from the indicated column in Sheet2. On error, output the value for the current column of Sheet1.
Notes:

In my version of the formula, ";" is used as parameter separator instead of ",". That is because I am located in Europe and we use the "," as decimal separator. Change ";" back to "," if you live in a country where "." is the decimal separator.
A$1: means always take column 1 when copying the formula to a cell in a different column. $A$1 means: always take the exact cell A1, even when copying the formula to a different row or column.

After pasting the formula in A1, I extended the range to columns B, C, etc., until the full width of my table was reached. Because of the $-signs used, this gives the following formula's in cells B1, C1, etc.:
<code>=IFERROR(VLOOKUP('Sheet1'!$A1;'Sheet2'!$A$1:$D$50;COLUMN(B1);FALSE);'Sheet1'!B1)
=IFERROR(VLOOKUP('Sheet1'!$A1;'Sheet2'!$A$1:$D$50;COLUMN(C1);FALSE);'Sheet1'!C1)
</code>

and so forth. Note that the lookup is still done in the first column. This is because VLOOKUP needs the lookup data to be sorted on the column where the lookup is done. The output column is however the column where the formula is pasted.
Next, select a rectangle in Sheet 3 starting at A1 and having the size of the data in Sheet1 (same number of rows and columns). Press Ctrl-D to copy the formulas of the first row to all selected cells.
Cells A2, A3, etc. will get these formulas:
<code>=IFERROR(VLOOKUP('Sheet1'!$A2;'Sheet2'!$A$1:$D$50;COLUMN(A2);FALSE);'Sheet1'!A2)
=IFERROR(VLOOKUP('Sheet1'!$A3;'Sheet2'!$A$1:$D$50;COLUMN(A3);FALSE);'Sheet1'!A3)
</code>
Because of the use of $-signs, the lookup area is constant, but input data is used from the current row.


A monic coalgebra morphism whose underlying $\text{Set}$ morphism is not injective

Question: Let $F:\mathscr A\to \mathscr A$ be a functor. Consider the following category $\mathscr C$. The objects are arrows $A\to F(A)$. If $\alpha:A\to F(A)$ and $\beta:B\to F(B)$ are two objects, then a morphism from $\alpha$ to $\beta$ is an arrow $h:A\to B$ such that $F(h)\circ \alpha=\beta\circ h$.
Now let $F: Set\to Set$ be the functor that assigns to every set $S$ the subset of $S^3$ consisting of all triples in which at least two elements are the same. Let $A=\{a,b,c\}$ and let $\alpha$ act as follows: $a\mapsto (a,a,b);b\mapsto (a,a,a); c\mapsto(a,b,b)$. The problem is to find an object $\beta:B\to F(B)$ of $\mathscr C$ and a morphism $ \alpha\to\beta$ in $\mathscr C$ such that the morphism is monic but the underlying set function $A\to B$ is not injective.
I tried to play with some examples. For example, let $B=\{1\}$. Then there is only one possible $\beta:B\to F(B), 1\mapsto (1,1,1)$. And there is only one possible morphism $\alpha\to \beta$ that on the level of sets is defined as the constant function. This function is not injective. Then I tried to see if this morphism is monic. So let $\gamma: X\to F(X)$ be another object of $\mathscr C$ and consider two morphisms $\gamma\to \alpha$ whose underlying functions are $x,y:X\to A$. Suppose the morphisms with underlying functions $h\circ x$ and $h\circ y$ are equal. Need to check if it must be the case that $x=y$ or not. The first problem is that I don't know how to do this in the abstract setting where $X$ is just some unknown set. Secondly, I just picked $B$ and $\beta$ at random, but I don't understand what properties should $B$ and $\beta$ possess in order to find what I need.
Comment: I'm curious about where this came from
Comment: @BrianShin It's a problem from a class on category theory related issues.
Answer: I think this is false.
Let's go through the details. If I made a mistake in my argument, then perhaps that will show us how to prove it if it does turn out to be true.
Let $F(X)$ be the functor that sends a set $X$ to the subset of $X^3$ consisting of points with at least two coordinates equal.
Let's first answer the question of what a morphism from $(C,\gamma : C\to FC)$
to the object $(A,\alpha)$ of the question looks like.
By definition, a morphism is a map of sets $f:C\to A$ such that
$\alpha f = F(f)\gamma$.
Given such a morphism $f$, we can let $C_\bullet = f^{-1}(\bullet)$, where $\bullet \in A$, and the commutativity requirement says that
$\gamma(C_a)\subseteq C_a\times C_a\times C_b$, $\gamma(C_b)\subseteq C_a\times C_a\times C_a$, and $\gamma(C_c) \subseteq C_a\times C_b\times C_b$. Conversely, if we have a
decomposition of $C$ into three sets $C= C_a\sqcup C_b\sqcup C_c$ satisfying the compatibility with $\gamma$ condition, then defining
$f$ by $f(C_a)=a$, $f(C_b)=b$, $f(C_c)=c$ gives a morphism from $(C,\gamma)$ to $(A,\alpha)$.
Note that $f$ is determined by any two of the sets, and we also have certain restrictions on the sets.
For instance, $\pi_1\gamma(C) \subseteq C_a$, so if a morphism $f$ exists, and $C\ne \varnothing$, then $C_a\ne \varnothing$, and this also forces $C_b\ne\varnothing$, since $\pi_3\gamma(C_a)\subseteq C_b$.
Since two components of the points are equal, we can say a bit more about the images of the sets under $\gamma$, we must have
$\gamma(C_a) \subseteq \Delta(C_a)\times C_b$, where $\Delta$ is the diagonal map. (In other words, $\gamma$ sends things in $C_a$ to points with first two coordinate equal), and $\gamma(C_c)\subseteq C_a\times \Delta(C_b)$.
Now let's think a bit about morphisms $h$ from $(A,\alpha)$ to $(B,\beta)$.
Let $h_a,h_b,h_c$ be the images of $a$, $b$, and $c$ under $h$ respectively.
The condition that $\beta h = h^3\alpha$ applied to each of $a$, $b$, and $c$ gives us
that
$\beta h_a = (h_a,h_a,h_b)$, $\beta h_b = (h_a,h_a,h_a)$, and $\beta h_c = (h_a,h_b,h_b)$.
Let's think about the possibilities for $B$ if we want $h$ to fail to be injective on the level of sets, and yet still a monomorphism. Note that we can assume without loss of generality that $h$ is surjective on the level of sets, otherwise we can replace $B$ by the image of $h$, and the commutativity condition on $\beta$ forces $\beta$ to restrict to the image of $h$, and the natural inclusion of the image of $h$ into $B$ is a monomorphism, so $h$ was a monomorphism if and only if $h$ restricted to its image is a monomorphism.
Now, if $h$ identifies all of the points, $h_a=h_b=h_c$, $B=\{*\}$ is the terminal object, and $(A,\alpha)\to (B,\beta)$ will not be a monomorphism, if we can find any two parallel but not equal maps to $(A,\alpha)$. Let's produce such a pair.
Let $C=\{a,b,d,e\}$, with $\gamma(a) = (a,a,b)$, $\gamma(b)= (a,a,a)$, $\gamma(d) = (a,a,e)$, and $\gamma(e) = (a,a,d)$.
The idea here is that I'm thinking of $a$ and $b$ as permanent members of $C_a$ and $C_b$, but $d$ should be able to switch back and forth between the two, and I don't need anything to map to $C_c$.
Then we can define two parallel maps $(C,\gamma)\to (A,\alpha)$ by
$f_1(a)=f_2(a)=a$, $f_1(b)=f_2(b)=b$, $f_1(d)=f_2(e)=a$, and $f_2(d)=f_1(e)=b$, and you can check that these are in fact well defined maps.
Now let's suppose that $h$ identifies two points of $A$.
If $h_a\ne h_b$, and $h_c=h_a$ or $h_c=h_b$, then we run into an impossibility, since we would have to have $\beta h_c=\beta h_a$ or $\beta h_c=\beta h_b$, which doesn't happen in this case, since $h_a\ne h_b$.
So instead we must have $h_a=h_b$.
Let's take $B=\{0,1\}$, $\beta(0)=\beta(1)=(0,0,0)$, and
$h:A\to B$ defined by $h(a)=h(b)=0$, $h(c)=1$.
But if we apply $h$ to our maps $f_1$ and $f_2$ from before, we see that
$h\circ f_1 = h\circ f_2$ are both maps sending all of $a,b,d,e$ to $0$.
Thus this $h$ is not a monomorphism either.
We appear to have exhausted all of the possibilities for $h$ to be a monomorphism without being injective.
Perhaps someone will spot an error in this though.
Edit notes
In light of Alex Kruckman's excellent answer (+1), it seems very likely that the question has a typo, and is false as written, so the logic in my answer is probably correct.
Comment: Looks right to me! I suspect there was a typo in the question, as I've indicated in my answer.
Answer: Just to fix some terminology (and motivation): Let $F\colon \mathscr{A}\to \mathscr{A}$ be a functor. A pair $(A,\alpha)$, where $A\in \mathscr{A}$ and $\alpha$ is an arrow $A\to F(A)$, is called an $F$-coalgebra. If $(A,\alpha)$ and $(B,\beta)$ are $F$-coalgebras, an arrow $h\colon A\to B$ such that $F(h)\circ \alpha = \beta\circ h$ is called a morphism of $F$-coalgebras. So the category $\mathscr{C}$ defined in the question is the category of $F$-coalgebras. There is an obvious forgetful functor $U\colon \mathscr{C}\to \mathscr{A}$, and you are looking for an example of a monic morphism $h$ in $\mathscr{C}$ such that $U(h)$ is not monic in $\mathscr{A}$.
jgon's answer correctly shows that the question as written does not have a positive answer. I suspect there is a typo in the question - if we adjust the example so that $\alpha(b) = (a,\mathbf{b},a)$, then the question admits a positive answer.
To be precise, $A = \{a,b,c\}$ and $\alpha\colon A\to F(A)$ is defined by
\begin{align*}
\alpha(a) &= (a,b,b)\\
\alpha(b) &= (a,b,a)\\
\alpha(c) &= (a,a,b)
\end{align*}
Now the point is that for any $F$-coalgebra $(C,\gamma)$, there is at most one $F$-coalgebra morphism $h\colon (C,\gamma)\to (A,\alpha)$. Indeed, suppose $h\colon (C,\gamma)\to (A,\alpha)$ is an $F$-coalgebra morphism. Let $x\in C$, and let $\gamma(x) = (y_1,y_2,y_3)$. By definition of $F$, at least two of $y_1,y_2,y_3$ are equal. Suppose $y_1 = y_2 = y$. Then $\alpha(h(x)) = F(h)(\gamma(x)) = F(h)(y,y,y_3) = (h(y),h(y),h(y_3))$. The first two coordinates of this triple are equal, so we must have $h(x) = c$ (since the first two coordinates of $\alpha(a)$ and $\alpha(b)$ are not equal). Similarly, if $y_1 = y_3$, then we must have $h(x) = b$, and if $y_2 = y_3$, then we must have $h(x) = a$.
It follows that any $F$-coalgebra morphism $m\colon (A,\alpha)\to (B,\beta)$ is monic, since if $h_1,h_2\colon (C,\gamma)\to (A,\alpha)$ are two $F$-coalgebra morphisms such that $m\circ h_1 = m\circ h_2$, then $h_1 = h_2$ (simply because there is at most one $F$-coalgebra morphism $(C,\gamma)\to (A,\alpha)$).
In your question, you correctly note that we can take $(B,\beta)$ to be the terminal $F$-coalgebra. Then the unique $F$-coalgebra morphism $m\colon (A,\alpha)\to (B,\beta)$ is monic but not injective on underlying sets.


Push forward and Pull back maps

Question: I am taking a class called calculus on manifolds, we are learning about tensors, alternating tensors and  k forms in hope to arrive at stokes theorem in order to integrate k-forms over manifolds. We are using Munkres book on Analysis on manifolds.
I am currently preparing for my final exam and having trouble with some past exam questions. I only have my notes and the book to go by and ive spent most of my time working through the theorems to try work it out. The question is as follows
Let $dx, dy, dz$ be a basis of elementary 1-forms on $R^3$. Consider the 1 form $\omega(x,y,z)$ and 2-form $\eta(x,y,z)$ on $R^3$ defined as follows;
$$\omega(x,y,z)=3xydx+2zdy+5dz$$
$$\eta(x,y,z)=4xydx\wedge dy +2(x^2+1)dy\wedge dz$$
(a) Detemrine $\omega\wedge\eta$ and $\omega\wedge\star\omega$, with $\star:\Omega^l(R^3)\rightarrow\Omega^{3-l}(R^3)$, the hodge star operator.
(b) Let the map $\alpha:R^2\rightarrow R^3$ be given by the equation
$$\alpha(u,v)=(u+v,u-v,u^2+v^2)$$
Determine $d\omega, \alpha_*\omega, \alpha_*(d\omega)$ and $d(\alpha_*\omega)$. Which general relation does this illustrate.
End of question:
So first I will explain my understanding and attempts
(a) I think I have this one, using theorems of the properties of the wedge product I found
$$\omega\wedge\eta=xydx\wedge dy\wedge dz[6x^2-17]$$
I am having trouble with putting my understanding of the hodge operator into action on an explicit function. I have tried using the fact $\star dx=dy\wedge dz $ but I don't know if what I am doing is right. I came to the following answer;
$$3xydy\wedge dz+2zdx\wedge dz +5dx\wedge dy$$
For the final part of that question I got
$$(3xy)^2(dx\wedge dy\wedge dz)-4z^2dx\wedge dy\wedge dz-25dx\wedge dy\wedge dz$$
I am not sure if this is what the corrext answer is supposed to look like. Again, in the context I am not quite sure what this means.
(b) is where I am having most of my problems, I found the first part to it to be $$-xdx\wedge dy-2dy\wedge dz $$
Again, Im nots sure if this is what the answer is supposed to look like, very few examples were done in class It was mainly proving all of the theorems we were using so I am not certain if these are what my answers are supposed to look like.
The final three parts, I do not know where to begin. The $\alpha_*\omega$ was taught to us as a push forward maps as a linear map on tangent spaces where $(x;v)$ are elements of $T_x(A)\rightarrow T_{\alpha(x)}(B)$ defined as \
    Let $ A\in\mathbb{R}^k $ open, and $ B\in\mathbb{R}^n $ open. $ \alpha:A\rightarrow \mathbb{R}^n $ such that $ \alpha(A)\subset B$, Then $ \alpha^{\star}\Omega^l(B)\rightarrow \Omega^l(A) $
    \begin{equation}
 (\alpha^{\star}w)(x)((x;v_1),\ldots(x;v_l))=w(\alpha(x))(\alpha_{\star}(x;v_1),\ldots,(\alpha_{\star}(x;v_l))
 \end{equation}
If anyone could help me in applying this definition to get an answer Id appreciate it. Finding it quite difficult even just to formulate a question to ask. I have tried going to my lecturer to confirm my solutions and ask for guidance but he has not gotten back to me since.
Thank you in advance.
Comment: I don't entirely recall everything about the Hodge star, but I don't think you evaluated it correctly on the other terms. In particular, $\star dy = dz \wedge dx$ and $\star dz = dx \wedge dy$. I think you know how to do wedge multiplication. All that's left is to get everything in terms of $dx \wedge dy \wedge dz$ and then collect like terms. Your final answer should be one 3-form i.e. something like $f dx \wedge dy \wedge dz$.
Answer: I am not familiar with the Hodge star, but the other wedge product computation seems to be in order. As far as part $(b)$ you have a map $\alpha(u,v) $ and so $\alpha_* = \textbf{J}(d_{(u,v)} \alpha)$ which is just the differential of $\alpha$ which you know how to compute.
$$D_{(u,v)} \alpha = \begin{pmatrix} \frac{\partial x}{\partial u} &\frac{\partial x}{\partial v} \\\frac{\partial y}{\partial u} &\frac{\partial y}{\partial v} \\\frac{\partial z}{\partial u} &\frac{\partial z}{\partial v}\end{pmatrix}_{(u,v)}$$
where $\alpha(u,v) = (x,y,z)$. You shouldn't be writing $\alpha_*$ if you wish to do something with $\omega$ since $\alpha_*$ is a linear map on tangent vectors. Are you sure you shouldn't have the pullback instead i.e $\alpha^*$, then this will make sense.
Comment: Yes you are correct, I corrected myself in the last line but not the question. It asks to find $\alpha^*\omega$ If $alpha_*$ is a linear map on tangent vectors, what is the pullback? Does that map 1-forms back to the original set? And to confirm, the push forward map (superscript star) is the differential? That has cleared something up if its the case. In computing the pull back I found $$\alpha^{\star}\omega=\omega(u+v,u-v,u^2+v^2)(u+v,u-v,u^2+v^2;\begin{array}{cc}
 v_1+v_2\\
 v_1-v_2\\
 2uv_1+cvv_2
\end{array})$$
Comment: Use the properties of $\alpha^*$, namely for a general $n$-form, $$ \alpha^* \left(\sum_{I} a_{I} \ dx^{i_1} \wedge \cdots \wedge dx^{i_n} \right) = \sum_I (a_I \circ f ) \  d(x^{i_1} \circ \alpha) \wedge \cdots \wedge d(x^{i_n} \circ \alpha)$$ One key property outside linearity is that $\alpha^*(d \omega) = d(\alpha^* \omega)$.

Push forward and Pull back maps


Run a linux docker container on Windows Server 2022

Question: I have a Windows Server 2022 running on GCP. I've installed docker as described here: https://cloud.google.com/compute/docs/containers#docker_on_windows
Namely, I did this:
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Install-Package -Name docker -ProviderName DockerMsftProvider
Restart-Computer -Force

However, when I try to run a linux image, I get
PS C:\Windows\system32> docker run busybox ls
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
docker: no matching manifest for windows/amd64 10.0.20348 in the manifest list entries.
See 'docker run --help'.

PS C:\Windows\system32> docker run --rm --platform linux alpine ls
Unable to find image 'alpine:latest' locally
docker: Error response from daemon: unsupported os linux.
See 'docker run --help'.

What am I doing wrong?
Edit. I can't enable hyper-V on GCP. My processor is Intel(R) Xeon(R) CPU @ 2.80GHz   2.80 GHz should't it allow nested virtualization
Screenshot
Looks like I need to enable nested virtualization in gcp. For that, I need to set enableNestedVirtualization field to true. There are descriptions on how to do it with gcloud api, but no way I can get my customer to do that. Any way to do it either in the WebUI or the VPS itself?
Edit 2. Does this mean I can't run a linux docker container from Windows? Or is docker a special case?

L1 VMs have the following restrictions:
You must run Linux-based OSes; you can't use Windows Server images.

https://cloud.google.com/compute/docs/instances/nested-virtualization/overview
Comment: https://mountainss.wordpress.com/2022/05/28/running-ubuntu-and-debian-linux-distro-on-windows-server-2022-preview-with-wsl2/
Comment: thank you! I just edited my original question, looks like nested virtualization needs to be enabled before I can run hyper-v?
Answer: So, a few things about what you are trying to do:

Linux containers on Windows (either client 10/11 or Server) are not supported in production. You can run it for development/testing purposes.
Linux containers require WSL to work on Windows. Specifically, for Windows Server 2022, WSL2 is required. I blogged about this here: https://techcommunity.microsoft.com/t5/itops-talk-blog/using-wsl-2-on-windows-server-2022-to-run-linux-containers/ba-p/3624745
WSL 2 essentially runs a Linux VM and exposes the CLI and services. For that to work on any cloud, you need nested virtualization enabled.
Comment: Running older versions of Docker like 2.2
Comment: Is there a way to run Docker on Windows 11 without Docker Desktop?
Comment: Not that I know of. Can you please clarify the scenario? Why do you want to run containers without Docker Desktop on Windows? Is it because of the cost? In that case, you could look into https://mobyproject.org/. Moby is the open source project from which Docker builds on. It's free, and community led.


Understanding code in strlen implementation

Question: I have two questions regarding the implementation of strlen in string.h in glibc.

The implementation uses a magic number with 'holes'. I am not able to understand how this works. Can someone please help me understand this snippet:
size_t
strlen (const char *str)
{
   const char *char_ptr;
   const unsigned long int *longword_ptr;
   unsigned long int longword, himagic, lomagic;

   /* Handle the first few characters by reading one character at a time.
      Do this until CHAR_PTR is aligned on a longword boundary.  */
   for (char_ptr = str; ((unsigned long int) char_ptr
             & (sizeof (longword) - 1)) != 0;
        ++char_ptr)
     if (*char_ptr == '\0')
       return char_ptr - str;

   /* All these elucidatory comments refer to 4-byte longwords,
      but the theory applies equally well to 8-byte longwords.  */

   longword_ptr = (unsigned long int *) char_ptr;

   /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
      the "holes."  Note that there is a hole just to the left of
      each byte, with an extra at the end:

      bits:  01111110 11111110 11111110 11111111
      bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

      The 1-bits make sure that carries propagate to the next 0-bit.
      The 0-bits provide holes for carries to fall into.  */

    himagic = 0x80808080L;
       lomagic = 0x01010101L;
       if (sizeof (longword) > 4)
       {
           /* 64-bit version of the magic.  */
           /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
           himagic = ((himagic << 16) << 16) | himagic;
             lomagic = ((lomagic << 16) << 16) | lomagic;
         }
       if (sizeof (longword) > 8)
         abort ();

       /* Instead of the traditional loop which tests each character,
          we will test a longword at a time.  The tricky part is testing
          if *any of the four* bytes in the longword in question are zero.  */
       for (;;)
         {
           longword = *longword_ptr++;

           if (((longword - lomagic) & ~longword & himagic) != 0)
         {
           /* Which of the bytes was the zero?  If none of them were, it was
              a misfire; continue the search.  */

           const char *cp = (const char *) (longword_ptr - 1);

           if (cp[0] == 0)
             return cp - str;
           if (cp[1] == 0)
             return cp - str + 1;
           if (cp[2] == 0)
             return cp - str + 2;
           if (cp[3] == 0)
             return cp - str + 3;
           if (sizeof (longword) > 4)
             {
               if (cp[4] == 0)
             return cp - str + 4;
               if (cp[5] == 0)
             return cp - str + 5;
               if (cp[6] == 0)
             return cp - str + 6;
     if (cp[7] == 0)
      return cp - str + 7;
}}}

What is the magic number being used for?
Why not simply increment pointer until NULL character and return count? Is this approach faster? Why is it so?
Comment: On most architectures, glibc will use even faster functions. On modern Intel chips, for example, it uses SIMD extensions to vectorize the check.
Answer: This is used to look at 4 bytes (32 bits) or even 8 (64 bits) in one go, to check if one of them is zero (end of string), instead of checking each byte individually.
Here is one example to check for a null byte:
unsigned int v; // 32-bit word to check if any 8-bit byte in it is 0
bool hasZeroByte = ~((((v & 0x7F7F7F7F) + 0x7F7F7F7F) | v) | 0x7F7F7F7F);

For some more see Bit Twiddling Hacks.
The one used here (32-bit example):

There is yet a faster method — use hasless(v, 1), which is defined
  below; it works in 4 operations and requires no subsquent
  verification. It simplifies to
#define haszero(v) (((v) - 0x01010101UL) & ~(v) & 0x80808080UL)
The subexpression (v - 0x01010101UL), evaluates to a high bit set in
  any byte whenever the corresponding byte in v is zero or greater than
  0x80. The sub-expression ~v & 0x80808080UL evaluates to high bits set
  in bytes where the byte of v doesn't have its high bit set (so the
  byte was less than 0x80). Finally, by ANDing these two sub-expressions
  the result is the high bits set where the bytes in v were zero, since
  the high bits set due to a value greater than 0x80 in the first
  sub-expression are masked off by the second.

Looking at one byte at a time costs at least as much cpu cycles as looking at a full interger value (register wide). In this algorithm, full integers are checked to see if they contain a zero. If not, little instructions are used, and a jump can be made to the next full integer. If there is a zero byte inside, a further check is done to see at what exact position it was.
Comment: In addition, what the gcc `strlen` implementation does is optimize to take advantage of architectures where 8-byte integers are supported. Above you are limited to looking for a null in 4-bytes at a time. The `if (sizeof (longword) > 4)` comparison in `strlen` extends the comparison for an additional 4-bytes. The benefit either way is improved `strlen` performance for strings longer than approximately 32-chars. (above what you get with a *char-by-char check*). Good answer.


How is the action of a vector field on a function defined?

Question: Questions of definition:
1) How do we make sense of $X(f)$ with $X$ a vector field on $M$ and $f$ a smooth function on $M$?
One idea I have is that it is not actually $X(f)$ where we apply the vector field on $f$ but actually the canonically associated derivation. That is $X(f)$ is the following function $$ X(f):M\rightarrow \mathbb{R}:x\rightarrow X_x(f)$$
2) How do we make sense of $Xg(Y,Z)$ where $g$ is a riemanian metric and $X,Y,Z$ vector fields.
One idea is that $Xg(Y,Z)$ is the fonction defined as follows $$ Xg(Y,Z):M\rightarrow \mathbb{R}:x\rightarrow X_x\cdot g_x(X_x,Y_x)$$
Comment: A vector field IS a derivation of the algebra of smooth functions.
And yes, if $Y, Z$ are vector fields then $g(Y,Z)$ is a smooth function.
Comment: The way I saw it, a vector field is a function from $M$ to $TM$ and a derivation of the algebra of smooth functions is a function from $C^\infty(M)$ to $C^\infty(M)$. So they are not the same for me, but I have seen the bijection that associates a vector field to a derivation (as stated in my idea for the first question). If I follow your idea for the second question I should to be precise write $X(g(Y,Z))$ is the function sending $x$ to $X_x(g(X,Y))$ where $g(X,Y)$ is the function sending $x$ to $g_x(X_x,Y_x)$
Comment: I know this is a lot of detail of defenition and notations which, when you become more advanced, you drop. But I need everything to be stated clearly without any notations abuse at least once to see how everything fits together :)
Comment: Yes, this is correct.
Answer: You might be used to thinking of a vector field as a map from an open neighborhood $U$ of $M$ to $\mathbb{R}^n$.  For instance, if $M = \mathbb{R}^3$ (the US "Calc III" setting), we write
$$
    \mathbf{F}(x,y,z) = P(x,y,z) \mathbf{i} + Q(x,y,z) \mathbf{j} + R(x,y,z) \mathbf{k}
$$
In the modern setting, we associate $\mathbf{i}$ to $\frac{\partial}{\partial x}$, $\mathbf{j}$ to $\frac{\partial}{\partial y}$, and $\mathbf{k}$ to $\frac{\partial}{\partial z}$.  So the vector field above becomes
$$
    X = P\frac{\partial}{\partial x} + Q\frac{\partial}{\partial y} + R\frac{\partial}{\partial z}
$$
$X$ maps functions to functions by
$$
    X(f) = P\frac{\partial f}{\partial x} + Q\frac{\partial f}{\partial y} + R\frac{\partial f}{\partial z}
$$
and is a derivation by the product rule.  On the level of points, it is, as you guessed,
$$
   X(f)(x,y,z) = P(x,y,z) \frac{\partial f}{\partial x}(x,y,z) + Q(x,y,z) \frac{\partial f}{\partial y}(x,y,z) + R(x,y,z) \frac{\partial f}{\partial z}(x,y,z)
$$
For your second question, let's pass to $M = \mathbb{R}^n$, or, $U$ is a coordinate patch of $M$ with coordinates $x^1, \dots, x^n$.  Then given three vector fields $X$, $Y$, and $Z$ on $U$, we can write
\begin{align*}
   X &= P^i \frac{\partial}{\partial x^i} \\
   Y &= Q^i \frac{\partial}{\partial x^i} \\
   Z &= R^i \frac{\partial}{\partial x^i} \\
\end{align*}
where the $P$'s, $Q$'s and $R$'s are functions, and we use the Einstein summation convention.  Also, $g$ can be decomposed as $g_{ij} dx^i \,dx^j$.  So
$$
    g(Y,Z) = g_{ij} Q^i R^j
$$
This is a sum of products of functions.  If we hit this with $X$, then, we have
\begin{align*}
    Xg(Y,Z) &= P^k\frac{\partial}{\partial x^k}\left(g_{ij} Q^i R^j\right)\\
    &= P^k\left(\frac{\partial g_{ij}}{\partial x^k}Q^i R^j
      + g_{ij} \frac{\partial Q^i}{\partial x^k} R^j
      + g_{ij} Q^i \frac{\partial R^j}{\partial x^k}\right)
\end{align*}
I don't think it's valid to think of $g$ evaluated at $x$, then paired with $Y_x$ and $Z_x$, then acted upon by $X_x$.  Because, as you can see from the above, the components of $g$ itself get differentiated.  The proper order is that the tensor $g$ gets paired with the vector fields $Y$ and $Z$, resulting in a function, which is acted upon by $X$.
Comment: You say that $X$ is a section of the tangent bundle $TM$ (In particular, $X$ is a function $M\to TM$), but then you say that $X$ takes smooth functions as inputs, and finally, that $X(f)$ takes points in $M$ as inputs. This is really confusing, and incorrect if taken literally, and doesn't address OP's confusion.
Comment: @YoTengoUnLCD: OK, good point.  I'll take out that part.
Comment: How does it make sense to rewrite $\mathbf{i}$ as $\partial / \partial x$?
Comment: @AlexandreH.Tremblay That is the isomorphism between vector fields as maps and vector fields as derivations.  If you think of $\mathbf{i}$ as movement in the direction of increasing $x$ coordinate, it gets associated to differentiating a function with respect to the $x$ coordinate.
Comment: Do the non-commutative properties roll-over. For instance, is the case that $ij+ji=0$ thus $(d/dx)(d/dy)+(d/dy)(d/dx)=0$?
Comment: @AlexandreH.Tremblay not really.  Most of the time $\partial/\partial x$ and $\partial\partial y$ commute.  The cross product on vectors is more related to rotation than translation.
Comment: Then are you not replacing $i$ with something that is not equivalent to itself?
Comment: The association is an isomorphism of vector spaces, but not of Lie algebras.   If you want to generalize the cross product, you instead send vector fields to differential 1-forms.  In $\mathbf{R}^3$, vector fields can also be associated to differential 2-forms.  The interior (wedge) product of two 1-forms associated to two vector fields is the 2-form associated to their cross product.  [Sorry if this is going too far off on a tangent.  Perhaps it would be better if you asked a new question.]
Answer: To get this answered, the short answer to both questions is yes. A vector field $X$ is a section of the tangent bundle $TM$, in particular $X:M\to TM$. So $X$ maps points to vectors, and vectors map functions to real numbers.
So when people say $Xf$, they actually mean $\tilde X(f)$ where $\tilde X$ is implicitly defined as follows: $\tilde X: C^\infty(M)\to C^\infty(M)$, $\, \tilde X(f)(p):=X(p)(f)$.
The second question follows easily from this.


New store branch locations optimisation problem

Question: Imagine we manage a chain of stores. We already have a number $M$ of branches of this chain in a certain region. The CEO has decided we need to expand and should open $N$ new stores in this same region.
The question is: Where should we place these new stores such that the locations are optimal?
So we have the set of current branch locations $\vec{z}_{1...M}$ and are looking for the new locations: $\vec{x}_{1...N}$
The goal is have consistent cover of the city, such that any random citizen has the least walking distance to any chain.
Bonus: Each existing branch has a weight based on the current number of visitors.

Is there a name for this type of optimisation problem?
With what sort of algorithm could this be solved?

An idea I had:

Place the $M$ new points evenly and randomly distributed on the map, together with the existing points.
Per iteration perform a push/pull on the new points, similar to how charged particles repel one another. Effectively calculate a force on each new point from other new points and the old points.
Move each new point slightly based on the net force.
Repeat 2. and 3. untill there is no change.

This should be fairly robust but the outcome is strongly dependent on the initial guess and susceptible to local minima. The outcome would be hardly optimal.
Comment: Note that I was caught between asking this here and on StackOverflow. I decided to ask it here since it is a general problem, independent on code implementation.
Answer: This is the multi-source Weber problem, a particular example of a facility location problem. You can find lots of information on solution methods by searching these terms.


thinking-sphinx index minimum

Question: I have a Product & Price models, where:
class Product < AR::Base
  has_many :prices # there are several types of prices, e.g. for guests, users et.c.

I want to index and sort products by price values, belonging to this product and exact price_type.
ThinkingSphinx::Index.define :product, with: :active_record do
  indexes name
  indexes k1c
  indexes catalogue_code
  indexes created_at, sortable: true
  indexes prices(:value), as: :price, sortable: true # TODO

  has :category_id
  has :brand_id
  has :kind_cd
  has :price_id
end

After rake ts:rebuild I get that
rake aborted!
NoMethodError: undefined method `type' for nil:NilClass
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/attribute/type.rb:64:in `type_from_database'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/attribute/type.rb:17:in `type'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/attribute.rb:4:in `type'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:30:in `sphinx_type'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:18:in `collection_type'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/sql_source.rb:96:in `block in append_presenter_to_attribute_array'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/sql_source.rb:93:in `each'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/sql_source.rb:93:in `append_presenter_to_attribute_array'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/sql_source.rb:132:in `prepare_for_render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/active_record/sql_source.rb:65:in `render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/riddle-1.5.10/lib/riddle/configuration/index.rb:29:in `block in render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/riddle-1.5.10/lib/riddle/configuration/index.rb:29:in `collect'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/riddle-1.5.10/lib/riddle/configuration/index.rb:29:in `render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/core/index.rb:53:in `render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/riddle-1.5.10/lib/riddle/configuration.rb:41:in `block in render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/riddle-1.5.10/lib/riddle/configuration.rb:41:in `collect'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/riddle-1.5.10/lib/riddle/configuration.rb:41:in `render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/configuration.rb:88:in `render'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/configuration.rb:94:in `block in render_to_file'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/configuration.rb:94:in `render_to_file'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/rake_interface.rb:24:in `index'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/gems/thinking-sphinx-3.1.0/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/bin/ruby_executable_hooks:15:in `eval'
/home/asiniy/.rvm/gems/ruby-2.1.0@zhivojoffice/bin/ruby_executable_hooks:15:in `'
Tasks: TOP => ts:rebuild => ts:index

Need I write a raw SQL query or there is another method?
UPD
Analogue sql query for get a one price (after that I want to order by price):
SELECT "prices".* FROM "prices" WHERE "prices"."product_id" = $1 AND "prices"."price_type_id" = 1 LIMIT 1

My prices model. Quantity of price_types are about 5 and changes very seldom.
class Price < ActiveRecord::Base
  validates :price_type, :value, :product, presence: true
  validates :price_type, uniqueness: { scope: :product }
  validates :value, numericality: { greater_than: 0.0 }

  belongs_to :price_type
  belongs_to :product
end

Comment: Show us your `prices` model, please.
Comment: @Daiku see below UPD section...

Else I little rewrite my question.
Comment: What's the full index definition?
Answer: There's a couple of things to note here.
Firstly: attributes are a better fit for sorting (they're sortable by their very nature)... but yes, if you want to sort by the minimum price, then you're going to need a SQL snippet for that:
has 'MIN(prices.value)', as: :price, type: :float

You will also need to ensure you're referring to the prices association either in a separate field or attribute, or explicitly in a call to join, to ensure the table is actually part of the joins in the generated SQL query:
join prices

However, the error you're seeing is unrelated to this - it's complaining that it can't determine the type of an attribute because the specified column doesn't exist. I'm pretty certain this is from your price_id attribute, which should probably go through the prices association:
has prices.id, as: :price_ids

Making this change has the added bonus of ensuring the SQL join exists for the prices table, and so you don't need the join prices line in your index definition.
Comment: And else one question. How can I specify type of price for indices? It seems I'll use both price & price type in some filterings.
Comment: I'm not sure what the cause of that error is - I'll need to see the full stack trace. As for searching by price type, do you want all Products with a specific price type? Or something else?
Comment: don't worry about first error. Solved by myself, just a typo. What about next question?
Comment: I found that: http://stackoverflow.com/questions/19157235/search-by-year-with-thinking-sphinx

Is there any better solution?
Comment: I'm going to need more detail on how you want to search across Products and/or Prices.
Comment: Perhaps it's best to post this as another question?
Comment: let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51080/discussion-between-asiniy-and-pat)

thinking-sphinx index minimum


bootstrap-typeahead requires 2 taps to select on ios7

Question: Using bootstrap-typeahead.js v2.3.2 to autocomplete on Safari iOS7 requires tapping twice to select an item from the list. The first time it hides the keyboard and marks the option as active, but the click doesn't go through until you tap it again.
Other browsers or phones or even older versions of iOS don't have this problem.
Answer: Found out that tapping an option causes the text field to 'blur' (so far so good), but for some reason (or bug) the original click event never gets fired. Only the mouseenter.
A quick fix was call the 'click' function from the 'blur' handler if something has been "mouseover-ed". This doesn't seem to cause any problems to other browsers that might call the 'click' function twice.
Just add the line with the comment below. This chunk would be lines 265-269:
<code>, blur: function (e) {
  this.focused = false
  if (!this.mousedover && this.shown) this.hide()
  else if (this.mousedover) this.click(e) // fix ios7 missing click on blur
}
</code>
Hope this helps somebody with the same problem or other libraries that also don't get a click after a blur on iOS7.


A function in [0, 1] that has a local maximum which is not a global maximum

Question: I am trying to come up with an example of a function $f:[0,1]\to\mathbb{R}$, that has a local maximum which is not a global maximum.
The picture in my mind looks like this:

what would be a function that has a local maximum which is not a global maximum?
Thank you
Comment: You could use standard polynomial interpolation techniques to define the particular polynomial you want.  Google "Lagrange Interpolation" if you want to do this.

I'd probably just choose something "easier" to write that works, such as $f(x) = x\sin(\frac{x}{2\pi})$.  While this is more complicated than an interpolating polynomial, it's easy to write down without having to figure out the particular polynomial to use.
Comment: Why not use a piecewise linear function? It's hard to get simpler than that.
Comment: What do you mean by "general equation"?
Comment: The function $x(x-1/2)^2$ might be another option...
Answer: $f(x)=(x-\frac{1}{4})^2(x-\frac{3}{4})^2$? That should have a local maximum at $x=0.5$
Edit: Oops, general equation. There's no way to parametrise something like this.
Comment: This works totally fine. Thank you so much!
Answer: Let $f(x) =0$ for $x\in [0,1),$ with $f(1) = 1.$ Then $f$ has a local maximum of $0$ at every $x\in [0,1),$ and a global maximum of $1$ at $1.$


Run some method at start of banch of methods (python)

Question: May be I'm doing wrong way, so tell me how to do it better.
I'm implementing one class for all instances of some object, say users. (looks like it is pattern 'Table Module' in M. Fowler's "Patterns of Enterprise Application Architecture"). I attached the simplified example of my implementation.
user argument may be of different kinds:

just int ID
dictionary with information about user. it has key id among other
tuple or list with ID as element with index 0

I want to determine right type of input variable user and get ID from it. At the same time I'd like to check if the input is correct at all.
I'm confused about calling __get_id at the beginning of all methods handling user information.
I sure python can propose better and lighter solution of this problem. Or maybe the whole approach is wrong and I should implement another one.
Thanks in advance.
class BadInputError (Exception):
    pass

class Users:
    def __init__(self):
        pass

    def __is_wrong_id(self, id):
        # code to check if `id` is wrong
        return False

    def __get_id(self, user):
        if isinstance(user, int):
            ID = user
        elif isinstance(user, tuple) or isinstance(user, list):
            ID = user[0]
        elif isinstance(user, dict) and user.has_key('id'):
            ID = user['id']
        else:
            raise BadInputError
        if self.__is_wrong_id(ID):
            raise BadInputError
        return ID

    def check_user(self, user):
        uID = self.__get_id(user)
        # ...

    def delete_user(self, user):
        uID = self.__get_id(user)
        # ...

    def assign_new_role_to(self, user):
        uID = self.__get_id(user)
        # ...

Answer: Here __get_id(self, user) make an attempt that would seem as overloading, however you're right - it's generally considered bad practice to check if a variable is an instance of a given class. In your definition you would only ever extract the first element of the tuple or list and hope that it corresponds to the ID.
Instead, you should strive to create wrappers of User that support the __get_id() method.
Comment: So expecting different inputs is wrong? I should use unified format of argument when calling methods, i.e. i should stop to expect `user` to be int or dict or list or tuple? And please explain more about wrappers, i don't quite understand this concept. Or better suggest a good read about it. `upd` Or I better google myself=)
Comment: Expecting different input interfaces is not a nice habit - you should create specialised functions to handle different types, which in turn call __get_id(user) to extract it out of the User object.

A wrapper is simply a class that takes an object, and either extends it by composition or provides some interface you would expect.

Python has some nice syntax for function wrappers - check out http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
Answer: It's hard to tell with this stripped down code, but the problem that I see here is none of the methods actually use self (other than to call ___get_id -- which doesn't use self).
Normally a class stores data and associated methods -- If you just have methods, then it should be a module.
Here, it makes sense for a "User" to know it's ID, so it might be worth creating a "User" class which uses the __get_id function to store an attribute (self.id). Then the Users class (which represents a collection of Users) could just check user.id.  e.g.:
class User(object):
    def __init__(self, initialize_data):
        self.__set_id(intialize_data)

    def __set_id(self, data):
        if isinstance(user, int):
            self.id = user
        elif isinstance(user, (tuple, list)):
            self.id = user[0]
        elif isinstance(user, dict) and user.has_key('id'):
            self.id = user['id']
        else:
            raise BadInputError

class Users(object):
    def __init__(self):
        self.users = []

    def delete_user(self, user):
        self.users = [u for u in self.users if u.id != user.id]

Note that this example implementation is nowhere near optimal -- It was meant to show how I would structure the code.  A bunch of improvements could probably be made to make it more efficient (i.e., maybe a User could be hashible based on the id in which case Users.users could be a set).


How to handle duplicate row with join

Question: I have a table bic_table.
-------------------------
KeyInstn  | SwiftBICCode
100369    |  BOFAUSV1
100369    |  MLCOUS33

keyInstn_table
-------------------------
KeyInstn  | country
100369    | USA
100370    | India

I am trying to join keyInstn_table with bic_table.
And I want to join both value as a comma separated.
How to get the result as
-------------------------
KeyInstn  | country  | SwiftBICCode
100369    | USA      | BOFAUSV1,MLCOUS33
100370    | India    | BOFH76HG
-------------------------

Comment: could you please provide sample of the data along with the desired output?
Comment: Use proper Markdown to format your question, not HTML tags. This code is very hard to read and not getting any better. You only seem to show *one* table, not two. `JOIN` means you have two tables you want to combine. Or did you mean *group* ?
Answer: If your database version is SQL Server 2017+ then you can use following:
SELECT a.keyInstn, country,STRING_AGG(SwiftBICCode, ', ') AS SwiftBICCode
FROM tablename a inner join keyInstn_table b on a.keyInstn=b.KeyInstn
GROUP BY a.keyInstn,country

Alternatively, you can use stuff() for lower versions of SQL Server
select u.keyInstn, country,
stuff(( select concat( ',', SwiftBICCode) from tablename y
        where y.keyInstn= u.keyInstn for xml path('')),1,1, '')
from tablename u inner join keyInstn_table b on u.keyInstn=b.KeyInstn
group by u.keyInstn,country

Comment: STUFF doesn't concatenate anything. It's the XML part that isn't visible here that combines the values by turning them into XML with "empty" tags, then removing the trailing comma with `STUFF`
Comment: I have to join a table keyInst also
Comment: Thanks @PanagiotisKanavos and Fahmi

How to handle duplicate row with join


Unique taxonomy term

Question: I need a specific vocabulary (A) that has unique terms. I don't want to do validation at form level because I want to be able to add term from everywhere.
I successful used hook_entity_bundle_field_info_alter() and addConstraint() to be sure one specific field is unique on another vocabulary (B). (Will this be deprecated? See https://www.drupal.org/node/2346347.)
For the vocabulary (A), there is no specific field, so the hook_entity_bundle_field_info_alter() is not working here, as this field name is not a "true" field.
I use hook_entity_presave(), but I'm not able to do a proper validation because this hook is called too late in the code, except if I use Exception, but that is not a good way.
How can I achieve what I am trying to do?
Answer: For the Drupal 8 you can use the contrib module.
It's called  Taxonomy unique .
It is a stable module with a good usage statistics
Answer: That hook wont' go away and its fine to use it. That warning was added a while ago, it's not possible to change it anymore in 8.x AFAIK. We hoped to do that before the stable release.
Name is a true field but it is a base field. You should be able to get the base field definitions from the entity type manager if it is not there, clone it and add your constraint.
Comment: Great news !
I read some things about what you said (cloning, etc) but can you be more precise ? Or maybe an example ? I don't know what hook to use / or what class to use / surdefine

Thanks
Comment: Look at Comment::bundleFieldDefinitions(), you have to load the base field definitions yourself but otherwise it's similar.
Comment: I looked about that... I want a specific vocabulary taxonomy term (vocabulary created trough UI) be unique. So where should I clone `$base_field_definitions` to `addConstraint` on it ? I tried to extends `Drupal\taxonomy\Entity\Term` class in my custom module but... Seems to have no effect :/
Comment: As I said, don't clone, that was just an example. Use the API of the entity field manager to get the base field definitions and then clone the field from those and put it in the bundle definitions. If the vocubulary name needs to be dynamic, you need to store some configuration somewhere about the name, for custom code, you could just hardcode it.
Comment: Duh ... I "see" what you say but... Drupal 8 make me feel like a dumb developper ! I don't understand where (in what way, hook, class, ...) and how use the Entity API :x (I can hardcode it to begin !).

Unique taxonomy term


Confidence two biased dice are the same?

Question: I have 2 biased dice (die 1 and die 2) and I would like to calculate the % confidence they are the same (or different), given $n_1$ rolls of the first die and $n_2$ rolls of the second.
Conceptually I'd expect that initially the confidence that they were the same (or different) would be $0$, and as $n_1$ and $n_2$ increase towards $∞$ the confidence would approach $100\%$ that they are the same (or different).
It's relatively trivial to generate a distribution curve of the probability of rolling a specific value on each die, but it's unclear to how how to compare 2 distribution curves (one from each die) to determine the confidence that they are the same or not.
Comment: That depends on what you mean by bias of the die.  How do you measure it?
Comment: In the absence of knowledge about *how* the dice are biased (how the two probability distributions deviate from a uniform "fair" density), a standard approach is the [Kolmogorov-Smirnov test](https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test).  Such nonparametric tests of *statistical significance* are typically less *powerful* than tests which use information about the possible distributions.
Answer: Let $p_{k}$ denote the parameters of the $k$-th die (a vector of probabilities corresponding to each side) and let $\hat{p}_{k,n}$ be its sample analogue (sample proportion). A possible measure of similarity between the dice is
$$
S(p_1,p_2):=1-\frac{d(p_1,p_2)}{\max_{p,q\in \Xi}d(p,q)},
$$
where $d(\cdot,\cdot)$ is a distance on $\mathbb{R}^6$ and $\Xi$ is the unit simplex. Note that $S(p,p)=1$ and $S(r,s)=0$ for $(r,s)=\operatorname{argmax}_{p,q\in S}d(p,q)$. Since $\hat{p}_{k,n}\to p_{k}$ a.s., the sample version $\hat{S}:=S(\hat{p}_{1,n_1},\hat{p}_{2,n_2})$ converges a.s. to $S(p_1,p_2)$.

Since $\hat{S}$ is random, obtaining a particular number doesn't provide much information  (even if the true parameters are the same, a particular realization of $\hat{S}$ can be close to $0$). A statistical way to assess similarity between two distributions would be testing the following hypothesis:
$$
H_0:p_1=p_2, \\
H_1:p_1\ne p_2.
$$
First, by the CLT,
$$
\sqrt{n}\left(\hat{q}_{k,n}-q_k\right)\xrightarrow{d}N(0,V_k),
$$
where $q_k= p_{k,1:5}$, $\hat{q}_{k,n}=\hat{p}_{k,n,1:5}$, and $V_k=\operatorname{diag}(q_k)-q_k q_k^{\top}$.
Assume that the sample sizes are $n_l$ and $m_l$ such that $n_l,m_l\to \infty$ and $m_l/n_l\to 1$ as $l\to\infty$, and let $r_l=(n_l+m_l) / 2$. Since $\hat{p}_{1,n_l}$ and $\hat{p}_{2,m_l}$ are independent,
$$
\sqrt{r_l}\left(\hat{q}_{1,n_l}-q_1\right)-\sqrt{r_l}\left(\hat{q}_{2,m_l}-q_2\right)\xrightarrow{d} N(0,V_1+V_2).
$$
Therefore, one may consider the following test statistic:
$$
T_l:=r_l(\hat{q}_{1,n_l}-\hat{q}_{2,m_l})^{\top}(V_1+V_2)^{-1}(\hat{q}_{1,n_l}-\hat{q}_{2,m_l}).
$$
Under $H_0$, $V_1=V_2$ and $T_l\xrightarrow{d}\chi_5^2$ (in practice, $V_k$ is replaced by any consistent estimator). Thus, one rejects $H_0$ when $T_l>\chi_{5,1-\alpha}^2$, where $\chi_{5,1-\alpha}^2$ is the $(1-\alpha)$-quantile of $\chi_5^2$.
Answer: I am thinking of two ways to approach the question. You could go down the path of considering rolling both at the same time and counting when the values match. I believe your PDF would be standard, with a center of 1/36. This would be a kind of binomial test with success meaning a match and a failure being not a match. You would want a one-tailed test to measure bias on the upper side (frequency above 1/36).
Alternatively, there is such a thing as a multinomial test, which would look at probabilities of die rolls for each dice. I believe this would be more difficult, unless you are concerned with specific values on each dice. From the way I interpret your question, it seems that you just care about the times that the dice output the same value (perhaps to catch two dice that add to 7 or 11).


Parsing XML with Perl using XML::LibXML::XPathContext

Question: I have a perl subroutine that is intended to receive a basic xml document and use xpath expressions to gather specific information from the document. Here it is
    sub parseXml {
    my ($rawXml) = @_;
    print "Raw XML resonse:\n" . $rawXml . "\n";

    # Set up the XPath search.
    my $xp = XML::LibXML::XPathContext->new;
    my $node = $xp->getContextNode;
    $xp->setContextNode($node);

    # Extract a list apiece of ids, whos, and whats.
    my @ids = $xp->findnodes('//object/void[@property="id"]/int');
    my @whos = $xp->findnodes('//object/void[@property="who"]/string');
    my @whats = $xp->findnodes('//object/void[@property="what"]/string');

    # Iterate over the arrays to print the data.
    my $it = each_array(@ids, @whos, @whats);
    while (my ($id, $who, $what) = $it->()) {
    print sprintf("%2d: ", $id->string_value) .
        $who->string_value . " -> '" .
        $what->string_value . "'\n";
    }
}

The problem is that when I run the script, I get an error that says:
XPathContext: lost current node at ... line 83
What have I done wrong? I am VERY new to perl.
Comment: Which one is line 83?
Comment: Seems my guess on `getContextNode` was right. ;)
Comment: It sure was.  getContextNode was just in there to try some other solution that didn't work.  Your solution, Jens, was right, as-is. Thanks again for your help.  Robin, I believe line 83 was my $xp = XML::LibXML::XPathContext->new;
Answer: You're not parsing the input XML at any point. Given $rawXml is a string, make sure to do so by using
my $dom = XML::LibXML->load_xml(string => $rawXml);
my $xp = XML::LibXML::XPathContext->new($dom);

My guess is line 83 is my $node = $xp->getContextNode;, which fails as the document is currently empty. You shouldn't need it anyway, together with the following line.
Comment: You are a blessing.  Thank you.  This is the answer and I will be happy to mark it as such.  Thanks again.  I hope this helps others out there as well.  And thanks to getting to me so quickly.  I appreciate it.
Comment: Alternatively, `$xpc->findnodes($xpath, $doc);`


How to debug inode usage

Question: I am a programmer for a popular website hosted by two web servers with apache. We're in the ~1000 alexa rank range to give you an idea. I'm not a server administrator.
Only one of them (always the same one) has been having a problem that started for the first time last week: every 2-3 days, it crawls to a complete halt timing out all HTTP connections and SSH connections as well. If you are already SSH'd in typing commands is either extremely slow (20-30 minutes before they appear) or doesn't work at all.
The only way to temporarily fix the problem that we found is rebooting the server.
I noticed this in our graphs:

Every peak corresponds to the server halting, and every drop corresponds to the reboot we were forced to do.
How can I further debug this? Is there a way to see what process is using the most inodes? What would you do?
Comment: Do any of the other monitors show anything peculiar? I don't find it likely that reaching 60% inode usage would bring your system to a screeching halt. Maybe the climb in inode usage is just a symptom of something else that might be going wrong.
Comment: @Ryan: I went over all our graphs, I found another "abnormal" one but it's inode related as well (I edited it in).
Comment: possible duplicate of [Determine Location of Inode Usage](http://serverfault.com/questions/38907/determine-location-of-inode-usage)
Comment: @Ryan: something else I just found is that the memory allocated for "slab_cache" is considerably higher on www01 (the problematic server) than www02: 4 GB vs 1.5 GB. The servers and their software is pretty much identical.
Comment: And if anyone else can answer this: Why would the inode usage drop after a reboot? Aren't inodes associated with persistent data that's written to disk?
Comment: Ryan, if it's dropping after a reboot, that may indicate some process releasing, say, open file handles upon termination.  I would look at process tables, run "lsof" to see if there's any abnormal usage, etc.  If this is one out of several identical boxes, I would be suspicious, as there might be an attacker.
Comment: Does your software perchance write a lot of data to `/tmp`?  Is `/tmp` perchance not its own filesystem (taking up space on `/` until reboot, when all the data in `/tmp` should get dumped)?  --- Also if you're not the server administrator perhaps you should have them come here and post more details.
Answer: The graphs you've embedded are showing the usage of in-memory inodes, not the one present on disk. The increase is very likely due to the fact that the number of open handles (files) on this system is increasing too. Maybe one of your processes is leaking handles, check the "lsof" output to verify that.

How to debug inode usage


Meaning of $LI \succeq \nabla^2 f(x) \succeq \mu I$ in optimization

Question: In these notes, it is said that a function is convex iff
$$\nabla^2 f(x) \succeq 0$$
In many literatures $\succeq$ is taken as element-wise greater or equal to. But in this case, $\succeq$ means positive semidefinite. Okay.
Now, on page 41 it says:
$$LI \succeq \nabla^2 f(x) \succeq \mu I$$
It seems that $\succeq$ has been overloaded without clarification.
Can someone clarifies what this means? Does this mean that the diagonal terms on the Hessian is element-wise greater than $\mu$ or less than $L$? What does it have to do with the eigenvalues of hessian?
Answer: If $A \succeq 0$ means positive semidefinitivity of $A$, one denotes by $A \succeq B$ the fact that $A - B \succeq 0$, or equivalently, that
$$ \def\<#1>{\left<#1\right>}\<Ax,x> \ge \<Bx,x> \qquad x \in X $$
($X$ is the space on which the operators $A$, $B$ act). Hence $LI \succeq f''(x) \succeq \mu I$ means that
$$ L\def\n#1{\left\|#1\right\|}\n u^2 \ge \<f''(x)u, u> \ge \mu \n u^2 , \qquad u \in X $$
In terms of spectral values, this holds exactly iff $\sigma\bigl(f''(x)\bigr) \subseteq [\mu, L]$, that is, all spectral values of the Hessian lie between $\mu$ and $L$.
Comment: do you have a reference text? thx


Fonts loaded by useFonts but useCallback unable to detect and hide splashscreen

Question: I am facing this issue that I have faced when trying to load custom fonts to react-native using expo workflow. This time the fonts have been loaded but the splashscreen is not hiding. I don't know much about how useCallback works but my assumption is its similar to useEffect only it used to listen to a variable state and run functions. I have followed the documentation on useFonts and still. Some forums says it would work on emulators but sometimes hang on real devices. Please take a look at my code and help me.
import { useFonts } from "expo-font";
import { useCallback } from "react";
import * as Splashscreen from "expo-splash-screen";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { Onboarding } from "./screens";
import { ActivityIndicator } from "react-native";

Splashscreen.preventAutoHideAsync();
const Stack = createStackNavigator();

export default function App() {
  const [fontsLoaded, fontError] = useFonts({
    fo_regular: require("./assets/fonts/regular.otf"),
    fo_bold: require("./assets/fonts/bold.otf"),
    fo_medium: require("./assets/fonts/medium.otf"),
    fo_light: require("./assets/fonts/light.otf"),
    fo_xtrabold: require("./assets/fonts/xtrabold.otf"),
  });
  console.log(fontsLoaded);

  const onLayoutRootView = useCallback(async () => {
    if (fontsLoaded || fontError) await Splashscreen.hideAsync();
  }, [fontsLoaded, fontError]);

  if (!fontsLoaded && !fontError)
    return (
      <ActivityIndicator size="small" color="black" animating={fontsLoaded} />
    );

  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Onboard"
          component={Onboarding}
          options={{ headerShown: false }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

So there is a console log of fontsLoaded, that is how I know the fonts are loaded but the useCallback is not handling it.
Comment: Does this solve your issue?
[How to load custom fonts with the useFonts hook in React Native](https://stackoverflow.com/a/77172404)
Answer: You don't want useCallback you want a useEffect
  useEffect(() => {
    if (fontsLoaded || fontError) Splashscreen.hideAsync();
  }, [fontsLoaded, fontError]);

useEffect is for undertaking an action when the values in the dependency array changes (in your case when either fontsLoaded or fontError have changed)
useCallback is for memoizing a function
Comment: Well noted.. In the useFonts documentation you see they used useCallback and I assume is to memoize. Can I still use useEffect to call the useCallback once the component is loaded
Comment: The code inside the `useEffect` will be called (as a function) when the component is loaded when either `fontsLoaded` or `fontError` changes. You can memoize it as `useCallback`, but then you would have to call that function its own `useEffect` (which would run when the function changes, which might defeat the purpose of having saved it as a `useCallback`)


How I write user lockout in ASP.NET to a log?

Question: I need to write to the application log when a user is locked out in ASP.NET Membership
Comment: Do you use a custom membership provider?
Comment: no, just the one built into ASP>NET
Comment: @ScottandtheDevTeam : Which stage you need to log? At the time of user get locked or when user try log in?
Comment: I only need to log after 5 unsuccessful attempts and they get locked out.
Answer: You can try something like this
<code>var user = Membership.GetUser(myLogin.UserName);
if(user != null)
{
     if (usrInfo.IsLockedOut)
     {
        //write log
     }
}
</code>


Maven, Proguard and JDK8

Question: I'm trying to contribute to the Parceler library but I'm having issues with maven android plugin, obfuscation and jdk8.
It is well known issue and I've tried to fix it by installing latest tool and specifying specific proguard.jar path. But looks like it is still failing. So looking if someone can lead me in the right direction.
Meanwhile I'm digging more.
UPD
Here is error that I have:
Caused by: java.io.IOException: Can't process class [com/oracle/net/Sdp$1.class] (Unsupported class version number [52.0] (maximum 51.0, Java 1.7))
[INFO]  at proguard.io.ClassReader.read(ClassReader.java:112)
[INFO]  at proguard.io.FilteredDataEntryReader.read(FilteredDataEntryReader.java:87)
[INFO]  at proguard.io.JarReader.read(JarReader.java:65)
[INFO]  at proguard.io.DirectoryPump.readFiles(DirectoryPump.java:65)
[INFO]  at proguard.io.DirectoryPump.pumpDataEntries(DirectoryPump.java:53)
[INFO]  at proguard.InputReader.readInput(InputReader.java:226)
[INFO]  ... 6 more
[INFO] Caused by: java.lang.UnsupportedOperationException: Unsupported class version number [52.0] (maximum 51.0, Java 1.7)
[INFO]  at proguard.classfile.util.ClassUtil.checkVersionNumbers(ClassUtil.java:140)
[INFO]  at proguard.classfile.io.LibraryClassReader.visitLibraryClass(LibraryClassReader.java:89)
[INFO]  at proguard.classfile.LibraryClass.accept(LibraryClass.java:301)
[INFO]  at proguard.io.ClassReader.read(ClassReader.java:86)
[INFO]  ... 11 more

Comment: It doesn't seem to use your updated version. But even if it did; what does it help when Android is not capable of using the newer class files anyway?
Comment: Thanks for comment. Proguard builds list of classes but some of them (the contracts) are identical for android and some are not used at all. I wonder how to check that maven plugin uses latest proguard
Comment: The android maven plugin invokes proguard via the Android SDK and as such you have to check for a working version there.
Comment: @ManfredMoser thanks, so maven plugin relies only on proguard in Android SDK
Comment: Check out the proguard mojo to be sure ... either the SDK directly or the dependencies from the SDK team.  See https://github.com/simpligility/android-maven-plugin/blob/master/src/main/java/com/simpligility/maven/plugins/android/phase04processclasses/ProguardMojo.java
Answer: Update ProGuard to latest version in Android SDK 'tools' folder from link http://proguard.sourceforge.net/downloads.html

Maven, Proguard and JDK8


JS or jQuery: How to automatically redirect to a url upon receiving a JSON object upon event listener

Question: Goal is to be able to redirect to another url based upon an intersection within a cross-domain iframe.
Desired process is as such…

A user is on example.com/apps/ - and fills in a form which is within a cross-domain iframe.
Upon submitting this form within the iframe a parent.postMessage() art the end of the load event which posts a JSON string back to the current page.
Based in the information relied back the user is then redirected from the 'parent' site to a new url (also within parent site - eg. example.com/apps/foobar)

I have got as far as step 2 insofar as I am managing to get receipt of the JSON object but am struggling to then get the page to redirect as desired.
Any pointers will be gratefully received - am not at home with the level of JS I'm afraid
jQuery(document).ready(function($) {

  window.addEventListener('message', function(event) {

    var meta = JSON.stringify(event.data)
    var data = JSON.parse(meta);

    console.log(data);

    // this is the json object received once the form is submitted in the iframe { "applicantid": "12345", "sector": "foobar" }
    // and this is the url I want the user to be automatically redirected to when the parent website received the json object
    // https://exampe.com/apps/sector/?applicantid=12345

    var applicantid  = data.applicantid; // 12345
    var sector       = data.sector;      // foobar

  if(applicantid !== undefined) {

    var url = 'https://exampe.com/apps/' + sector + '/?cand_id=' + applicantid + '/';
    $(location).attr('href',url);

   }
 });
});

(I am aware there a probably better ways to to do this - we are restricted to this (or a similar method) due to the organisational structure of the client)
Thanks in advance
Answer: window.location = 'https://url.com';

Refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
Comment: Thank you - No joy I'm afraid - I think the issue is my function is not recognising when the Json vars become available


Can this heuristic argument be useful to prove: Sum of digits of $a^b$ equals $ab$ conjecture?

Question: I've been trying to write a proof for the following conjecture (from this question):

Let $s\left(a^{b}\right)$ denote the sum of the digits of $a^{b}$ in base $10$. Then the only integer values $a$,$b>1$ that satisfy $s\left(a^{b}\right)=ab$ are $(2,2),(3,3),(3,6),(3,9)$ and $(3,27)$.

I found what I think is a heuristic argument, but i'm not sure if it can be useful in proving the conjecture.
Let $d\left(n\right)$ denote the number of digits of integer $n$ in base $10$:
$$d\left(n\right)=1+\left\lfloor \log_{10}n\right\rfloor $$
Let $s\left(n\right)$ denote the digit sum of integer $n$ in base $10$.
Now from the conjecture, take for example the case $a=2$. I've been looking at the following sum:
$$\sum_{n=1}^{b}\frac{s\left(2^{n}\right)}{\sum_{k=1}^{n}d\left(2^{k}\right)}$$

The plot of that sum for $1\leq b\leq20000$ looks like that:

And now the same plot, with in orange, $9\log b$:

The difference between the $2$ curves quickly converges to a value $c$, and we see that:
$$
\lim_{b\rightarrow\infty}\left(9\log b-\sum_{n=1}^{b}\frac{s\left(2^{n}\right)}{\sum_{k=1}^{n}d\left(2^{k}\right)}\right)=c\approx12.721\ldots
$$
From this, we can also conclude that:
$$
\frac{s\left(2^{b}\right)}{\sum_{k=1}^{b}d\left(2^{k}\right)}\sim9\log\left(\frac{b-1}{b}\right)\sim\frac{9}{b}
$$
And since:
$$
d\left(n\right)=1+\left\lfloor \log_{10}n\right\rfloor \approx1+\log_{10}n
$$
Then:
$$
\sum_{k=1}^{b}d\left(2^{k}\right)\approx\frac{b^{2}\log_{10}2}{2}
$$
And:
$$
s\left(2^{b}\right)\sim\left(\frac{9}{b}\right)\left(\frac{b^{2}\log_{10}2}{2}\right)\sim\left(\frac{9}{2}\right)b\log_{10}2s\left(2^{b}\right)\sim1.3546\times b
$$
The same applies to other values of $a$, so more generally:
$$
s\left(a^{b}\right)\sim\left(\frac{9}{2}\right)b\log_{10}a
$$
Looking at plots of $s(a^b)$ for each values of $a$ from $2$ to $8$, we can see this asymptotic relation seems to be very accurate.

Now I have $2$ questions:
1: Does the asymptotic relation above is correct, or is there some errors in my reasoning?
2: Since $a>\left(\frac{9}{2}\right)\log_{10}a$, does an asymptotic relation like that is enough to prove $s\left(a^{b}\right)<ab$, for sufficiently large $b$?
Any help or advice would be appreciated.
Comment: I don't know why you are interested in the sums in $$\sum_{n=1}^{b}\frac{s\left(2^{n}\right)}{\sum_{k=1}^{n}d\left(2^{k}\right)}$$  What you are really interested in is a particular $n$.
Answer: Your relation $$s\left(a^{b}\right)\sim\left(\frac{9}{2}\right)b\log_{10}a$$
is a very reasonable heuristic.  It assumes that the digits of $a^b$ are reasonably evenly distributed.  Unfortunately the upper bound is $9b\log_{10}a$ (even ignoring the $1$) and $9\log_{10}a \gt a$ unless $a=9$.  We cannot use this to set a hard upper limit on the $b$ that we would have to check for a given $a$.  You can say that examples with large $b$ are unlikely because they would require the digits of $a^b$ to be larger than expected


Multisite - Commerce reference between sites

Question: Setup
Main website: www.example.com
LMS website: lms.example.com

The Main website is public facing for visitors to browse the courses.
The LMS website is for the users/commerce to be managed and the actual Course material to be used/viewed.
Drupal Commerce manages the payments and Access modules manage the users access once purchased.

The 2 websites are setup as Drupal multi-sites:

On the same server
Separate databases
All modules/theme files are under the sites/all folder.

Question
How do I create a 'Buy' button on the main website that will take the visitor to the LMS website with that particular course added in their cart?
Note: there is only a hand full of courses so the link on the main website can be manually added for each course.
Comment: The subdomain is currently setup as a multisite. If this makes this impossible/very hard, then I could use Domain Access to create the subdomain LMS under the same Drupal install.
Answer: So I have discovered that I will need to create a Rule using the module Rules URL Argument that will take the URL /add-to-cart/[SKU] and add this to cart.
I can create a field on the main site for the SKU and this then outputs the link to the LMS website.
The source is here:
https://www.youtube.com/watch?v=6Rn7FLFytTU


python insert into mysql table errors

Question: to write python program to insert data in mysql table fb_web_active_group_members_user_hClustering_six
if cur.execute(is_exist_table_sql) == 0:
    create_sql  = '''CREATE TABLE fb_web_active_group_members_user_hClustering_six (id bigint not null primary key auto_increment,userID bigint,GroupName varchar(128),
                Friends int,NoProfilePhotos int,UploadPhotoSum int,NoPosts int,UploadVideoSum int,postCluster0 int,postCluster1 int,postCluster2 int,postCluster3 int,postCluster4 int,
                normal_Friends float,normal_NoProfilePhotos float,normal_UploadPhotoSum float,normal_NoPosts float,normal_UploadVideoSum float,normal_postCluster0 float,
                normal_postCluster1 float,normal_postCluster2 float,normal_postCluster3 float,normal_postCluster4 float,postCluster0_ratio float,postCluster1_ratio float,
                postCluster2_ratio float,postCluster3_ratio float,postCluster4_ratio float, cluster_label int)'''
    cur.execute(create_sql)

for item in user_level_cluster_list:
    insert_sql = '''INSERT INTO fb_web_active_group_members_user_hClustering_six (userID,GroupName,
                Friends,NoProfilePhotos,UploadPhotoSum,NoPosts,UploadVideoSum,postCluster0,postCluster1,postCluster2,postCluster3,postCluster4,
                normal_Friends,normal_NoProfilePhotos,normal_UploadPhotoSum,normal_NoPosts,normal_UploadVideoSum,normal_postCluster0,
                normal_postCluster1,normal_postCluster2,normal_postCluster3,normal_postCluster4,postCluster0_ratio,postCluster1_ratio,
                postCluster2_ratio,postCluster3_ratio,postCluster4_ratio, cluster_label) VALUES
                ({0},"{1}",{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27})'''.format(item["UserID"],
                                                                               item["GroupName"],item["Friends"],item["NoProfilePhotos"],item["UploadPhotoSum"],
                                                                               item["NoPosts"], item["UploadVideoSum"],item["postCluster0"],item["postCluster1"],item["postCluster2"],item["postCluster3"],
                                                                               item["postCluster4"],"%.5f"% item["normal_Friends"],"%.5f"%item["normal_NoProfilePhotos"],"%.5f"%item["normal_UploadPhotoSum"],"%.5f"%item["normal_NoPosts"],
                                                                                "%.5f" %item["normal_UploadVideoSum"],"%.5f"%item["normal_postCluster0"],"%.5f"%item["normal_postCluster1"],"%.5f"%item["normal_postCluster2"],
                                                                                "%.5f" %item["normal_postCluster3"],"%.5f"%item["normal_postCluster4"],"%.5f"%item["postCluster0_ratio"],"%.5f"%item["postCluster1_ratio"],
                                                                                "%.5f" %item["postCluster2_ratio"],"%.5f"%item["postCluster3_ratio"],"%.5f"%item["postCluster4_ratio"],item["cluster_label"])

    print(insert_sql)
    cur.execute(insert_sql)

but it errors:
Traceback (most recent call last):
    INSERT INTO fb_web_active_group_members_user_hClustering_six (userID,GroupName,Friends,NoProfilePhotos,UploadPhotoSum,NoPosts,UploadVideoSum,postCluster0,postCluster1,postCluster2,postCluster3,postCluster4,normal_Friends,normal_NoProfilePhotos,normal_UploadPhotoSum,normal_NoPosts,normal_UploadVideoSum,normal_postCluster0,normal_postCluster1,normal_postCluster2,normal_postCluster3,normal_postCluster4,postCluster0_ratio,postCluster1_ratio,postCluster2_ratio,postCluster3_ratio,postCluster4_ratio, cluster_label) VALUES (1,"Jazzmasters&Jaguars",964,195,5,339,0,119,14,73,20,36,0.19280,0.17016,0.00028,0.37184,0.00000,0.27483,0.09589,0.20055,0.18868,0.15000,0.45420,0.05344,0.27863,0.07634,0.13740,4.00000)

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',0.27483,0.09589,0.20055,0.18868,0.15000,0.45420,0.05344,0.27863' at line 6")

could you please tell me the reason and how to resolve it
Comment: Midway the query your commas are starting to get wierd: `{14},{15},{16},{17},{18}`
Answer: Please, please use the standard syntax presented in MySQL python docs.
I.e. the format of your query should be:
 query = ("INSERT INTO table"
           "(col1, col2, col3, col4)"
           "VALUES (%s, %s, %s, %s)")
 data = (value1, value2, value3, value4)
 cursor.execute(query, data)

Don't use the python {}-formats. Your current implementation (if it happened to work, which it luckily does not, probably due to those weird commas) would be vulnerable to SQL-injections.

python insert into mysql table errors


Apache 2.4 unusual logs

Question: since some days, i am getting some tries of hack attacks, but actually all seems fine.
but i saw some log i could not explain:
127.0.0.1:443 22.214.171.124 - - [09/Oct/2015:04:49:29 +0200] "GET / HTTP/1.1" 404 4857 "-" "-"
127.0.0.1:80 184.108.40.206 - - [09/Oct/2015:07:56:11 +0200] "-" 408 0 "-" "-"
127.0.0.1:443 184.108.40.206 - - [09/Oct/2015:10:35:04 +0200] "GET /admin/i18n/readme.txt HTTP/1.1" 404 5081 "-" "python-requests/2.8.0"

here is my log configuration:
# - Exeption
SetEnvIf Request_URI "\.jpg$|\.jpeg$|\.gif$|\.png$|\.ico|\.icon|\.css$|\.js$|piwik\.php$|frogglogin\.php" dontlog
SetEnvIf User-agent "(bot|baidu)" dontlog

CustomLog ${APACHE_LOG_DIR}/access.log vhost_combined env=!dontlog

how is it possible to thoose requests to reach 127.0.0.1 ?
what can i do to prevent 408 errors ?
should i panic to see thoose kind of attaks incomming ?

thanks
PS:
is this a good idea ?
<VirtualHost 127.0.0.1>
# [ Default restriction ]
<Directory />
    Order deny,allow
    Deny from all
    allow from 127.0.0.1
</Directory>
</VirtualHost>

Answer: There is no way that the public IP address will reach directly to your loopback address. It is the matter of NAT where probably apache translates those IP addresses to loopback because either you told apache to do that or some misconfiguration.
There is no panic for such thing, it is normal to be attacked by botnets and/or automated apps designed to attempt login pages and so on.
When this attemps are in progress I'd rather check with:
  netstat -an (or adding additional parameters)

To check the connections.
Comment: thank for the answer, i am behind a nat, but if i allowed loopback it is a mistake, how could it be done in apache ? i added a config is that a good idea ?
Comment: I don't think that is a good idea at all. I don't see any need of such configuration. Pls check netstat as I suggested and you'll see the public interfaces and check your network configuration.
Comment: i am sorry i need more time for testing, i will give information when i ll be on it !
Answer: This is how i get ride of this :
I added this configuration after all defined virtual host, to catch "else" request (the request that doesn't match any other virtual host).
The most important is the virtual position(last) and the command ServerAlias *
#---------------#
# [ LOCALHOST ] #
#---------------#
# Local host for other:
# ServerName localhost
# ServerName 88.xxx.xxx.xxx
# ServerName xxxxxxxx.net
# and more ...
# need to be at the last position
<VirtualHost *:80 *:443>
# [ Server Domain ]
ServerName localhost
ServerAlias *
# [ Server Root ]
DocumentRoot /var/www/home/
# [ Cancel trafic ]
RewriteCond %{REQUEST_URI} !errors\/hack\.htm
RewriteRule .*? - [END,R=406]
# [ Custom Log ]
CustomLog ${APACHE_LOG_DIR}/hack.log combined
</VirtualHost>

Apache 2.4 unusual logs