OLS regression results: p-values > 0.10, how to proceed?

Question: In the Python statsmodels documentation there is an example with the goal:
We want to know whether literacy rates (Literacy column) in the 85 French departments (Departments) are associated with per capita wagers on the Royal Lottery (Lottery) in the 1820s. We need to control for the level of wealth (Wealth) in each department, and we also want to include a series of dummy variables on the right-hand side of our regression equation to control for unobserved heterogeneity due to regional effects (Region; N, E, S, W to 0 or 1). The model is estimated using ordinary least squares regression (OLS).
<code>OLS Regression Results
==============================================================================
Dep. Variable:                Lottery   R-squared:                       0.338
Model:                            OLS   Adj. R-squared:                  0.287
Method:                 Least Squares   F-statistic:                     6.636
Date:                Tue, 02 Feb 2021   Prob (F-statistic):           1.07e-05
Time:                        07:07:06   Log-Likelihood:                -375.30
No. Observations:                  85   AIC:                             764.6
Df Residuals:                      78   BIC:                             781.7
Df Model:                           6
Covariance Type:            nonrobust
===============================================================================
                  coef    std err          t      P>|t|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept      38.6517      9.456      4.087      0.000      19.826      57.478
Region[T.E]   -15.4278      9.727     -1.586      0.117     -34.793       3.938
Region[T.N]   -10.0170      9.260     -1.082      0.283     -28.453       8.419
Region[T.S]    -4.5483      7.279     -0.625      0.534     -19.039       9.943
Region[T.W]   -10.0913      7.196     -1.402      0.165     -24.418       4.235
Literacy       -0.1858      0.210     -0.886      0.378      -0.603       0.232
Wealth          0.4515      0.103      4.390      0.000       0.247       0.656
==============================================================================
Omnibus:                        3.049   Durbin-Watson:                   1.785
Prob(Omnibus):                  0.218   Jarque-Bera (JB):                2.694
Skew:                          -0.340   Prob(JB):                        0.260
Kurtosis:                       2.454   Cond. No.                         371.
==============================================================================

</code>
Prob (F-statistic), 1.07e-05, thus reject null hypothesis (H0: all coefficients are equal to zero), so there is statistically significant evidence that there is a relationship between dependent and independent variables together. But only Wealth has a p-value < 0.05.
Should the model be used as is? Or should all independent variables except Wealth be removed? What should be done based on the goal "We want to know whether literacy ... We need to control for the level of wealth (Wealth) in each department ..."?
Comment: You could also check how much standard errors change if you use heteroscedasticity robust standard errors (`model.fit(cov_type="HC3")` in statsmodels).
Comment: Plot the data and the fit before doing anything else. (Perhaps you already have.) The numerical summaries are nowhere near as powerful a way to 'see' the nature of the data.
Answer: Assuming that there are no problems with model assumptions, the model should be used as it is. Insignificant variables should not be removed. Removing them would invalidate any tests that are run within the reduced models. (Removing insignificant variables seems to be a common practice, but that doesn't make it better. Occasionally there are reasons such as removing variables that are potentially expensive to observe in the future when using the model for prediction, or that the number of observations is too small for fitting a full model with reasonable reliability, but I don't see such reasons here; even in such cases there are often better criteria than significance.)
Comment: stated it well.   In addition you can deal with collinearities (competing predictors with overlapping content) by forming specific contrasts and by running composite tests ("chunk tests").  For example you can test the combined effects of literacy and wealth after adjusting for region.  **However** you are currently assuming linearity for literacy and wealth, which is unlikely to be true.
Comment: +1 I wish that first sentence were bolded, because it is so important!
Comment: @svavil The fact that insignificant variables are insignificant  *can* be interpreted, and that's important (the main interest is in literacy, which is not significant). I'd be surprised if you disagreed with this.
Comment: @Alexis is correct that the first sentence is important, particularly this part: "Assuming that there are no problems with model assumptions". However, his comment is incomplete because it does not say that the model assumptions are only rarely free of problems. Perhaps omitting variables would make the model less problematical!
Comment: @MichaelLew I agree that "model assumptions are rarely free of problems", however all p-values rely on the model assumptions, meaning that removing variables based on p-values is not a suitable tool for dealing with model assumptions. Also not all potential problems with model assumptions invalidate the standard analysis shown here, but that's a different discussion.
Comment: Do you maybe want to stress that the model as is could be used for *prediction*, but the *interpretation* of the model should only rely on significant variables?
Comment: @MichaelLew she/her, thanks. Aside: omitting variables ≠ not making assumptions.
Answer: If you remove independent variables, even ones that are not statistically significant, it will change the coefficient for the wealth variable (and the other variables).  It will also reduce your adjusted R-squared value, which at .287 is already not great (but not bad either).  This means of course that 28.7% of the outcome variable is explained by the independent variables in the model.
Removing some of the independent variables could possibly make the p-value for the Wealth variable smaller, but only because Wealth may in some way correlate with those independent variables.  In other words, you want all those independent variables left in the equation because the regression then controls for them, and does not falsely ascribe their effect to Wealth. A classic example of this is a regression associating drinking with bad health.  But smoking is often associated with drinking.  When smoking is also included in the regression, drinking is no longer significantly associated with bad health.
You can actually remove some highly non-significant independent variables and see how the other variables' coefficients change.  Juggling them around in this way is part of the art of regression, but this requires good subject area knowledge.  It is also useful to see how well these independent variables correlate with each other. If they do, you will have multicollinearity which will weaken the predictive power of the variables that correlate with each other; in which case it's usually best to remove one of them from the regression. This transfers their effect on the dependent variable to the remaining independent variable(s), at least to the extent that they were correlated with each other.


alert if nothing happens on click

Question: essentially I am wondering if it is possible to write a function so that if any item inside a specific container is clicked and nothing happens, it will alert that the feature has not been made available yet.
explanation:
what I mean by nothing happens is that the DOM or site does not change. So if a javascript or jquery function is called on click, it would not alert. if the item is a link to another site, it would not alert. but if nothing happens on the click event of an item inside a specified container, then it would alert.
Comment: can somebody please explain the -1, I feel like this is a very useful question
Comment: It might be because there is no code attempt shown.
Comment: Why do you need to detect if a button does something? Why can't you just manually put in an alert box in the code for each button that is not implemented?
Comment: I could do that, and I am right now. it just sparked the inspiration for this question because I thought it would be cool if this was easily possible
Answer: The closest you could come would be to:

bind a click event handler to the container that would check if the event target was a link to another site and, if not, display the alert
make sure you stopPropagation() on all your event handlers.
Comment: it's more than just a link to another site...that would be easy...it is if there is any call to jquery or javascript as well
Comment: @RyanSaxe — That is handled in step 2 of the answer.
Comment: wouldn't that just prevent the other from happening, not detect if something does...
Comment: Yes, but since you won't want the alert to fire if your other event handler fires, that gives you the end result that you want.
Comment: could you give an example? I am still not sure how this will work
Answer: Like this?
jsFiddle
$('a').on('click', function() {
    if ($(this).attr('href') == '#') {
        alert('the feature has not been made available yet.');
        return false;
    } else {
        return true;
    }
});

Comment: I am just answering his question.
Comment: in order to answer my question this needed to work with jquery event calls as well just like opening a dialog as @TusharGupta has pointed out

alert if nothing happens on click


GCE kernel updates

Question: I'm running a Debian instance on GCE and want to prevent kernel updates as I don't want to have to reboot the instance unnecessarily.
I know that I can use sudo apt-mark hold package_name to stop kernel updates on the instance, but I don't know if it's safe to do this. For example, does using the above command on GCE instances have any unintended consequences, such as cascading dependencies, or causing issues with the parent server, for example when Google automatically moves an instance to a new zone for maintenance reasons. Thanks.
Answer: You shouldn't have a problem with pinning or holding package versions during a live migration event since your virtual machine's settings are independent of the host/hypervisor it runs on. The state of the machine is preserved during a live migration down to the OS state and the state of its installed packages.
This post goes into detail what happens from a technical perspective: http://googlecloudplatform.blogspot.com/2015/03/Google-Compute-Engine-uses-Live-Migration-technology-to-service-infrastructure-without-application-downtime.html
Comment: Thank you. And thanks also for the link to the Google blog post, I hadn't seen that.

GCE kernel updates


Field found by index and not by name

Question: Here is my problem, I have this query:
SQL = "SELECT *
       FROM Items, Genre, Artist
       WHERE Artist.ID = " & SelArtist & " AND
             Items.Artist = Artist.ID AND
             Items.Genre = Genre.ID"

And I need to get Items.Song from it.
If I go with the "index print" it all works out and print the correct field
Response.Write(Recordset.Fields(3).Value)

But if I choose to go with the field name
Response.Write(Recordset.Fields("Items.Song"))
then It gives me the following error:

ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested
  name or ordinal.

Which, if I'm not wrong means he can't simply find the field I'm searching for and often it is because the name is mispelled or simply the query is wrong. In my case I'm sure it's none of either cases so I really don't know what to do about it.
Thank you very much in advance
Comment: You can't prefix your column names, your record set is independent of your query, all it knows is that there is a column called "Song", it has no knowledge which table it came from. This is one reason why [you should not be using `SELECT *`](https://sqlblog.org/2009/10/10/bad-habits-to-kick-using-select-omitting-the-column-list). You have no control over ambiguous column names.
Answer: Try changing to:
Response.Write(Recordset.Fields("Song"))

Field found by index and not by name


PHP & MySQL - How to check if an array is empty or not in a function

Question: I was wondering how can I check if an array is empty or not in a function
Here is part of my code.
if (!mysqli_query($dbc, $sql)) {
        trigger_error(mysqli_error($dbc));
        return;
} else {
    $t = array();
    while($row = mysqli_fetch_array($result)) {
        $t[] = $row[0];
    }
}

if($tr > 0){
    $ts = array_sum($t);
}

Comment: `$t[] = $row[0]` is going to give the same value to every element in the `$t` array. You need to put a counter in there.
Comment: `if (empty($array))` and it doesn't matter inside of function or not
Comment: @JMC Creative, exactly where?
Comment: @Col. Shrapnel, "not in a function" but "if an array is empty or not"
Comment: @tang, before the while statment you could put ` $i = 0 ` then inside the while loop, you would put ` $t[] = $row[$i]; $i++ `
Comment: still there is no sense. what's the difference if gonna check it ina function? functions bears the same code as the rest of the script
Comment: @Col. Shrapnel, i did not no that, and no need to be rude. -1 for you being you.
Comment: LOL what a language! Your mom should spank you for this, little rascal :)
Answer: Use the empty() function:
http://php.net/manual/en/function.empty.php
Comment: Not a function, but a language construct. Slightly different.
Answer: Loose equivalency checking returns false on an empty array
if(array()) // returns false

http://php.net/manual/en/types.comparisons.php
Answer: Use count() to count the number of elements in the array.
Answer: An easy method would be to use the count function.
For example:
if(count($sourceArray) != 0) {
    // Do exciting things here...
}

Answer: empty($t);

gives you true if $t is considered empty.
Answer: To check if array is empty:
if (sizeof($arr) == 0)
{
    //Empty array
}

To check whether an element exists in an array or not:
if (in_array($element, $arr))
{
     //Element found in the array
}


replacing file name and content : shell script

Question: I have written the code for replacing content but quite confused with replacing file name, both in the same script. I want my script to run this way:
suppose I give input : ./myscript.sh abc xyz

abc is my string to be replaced
xyz is the string to replace with

If some directory or any subdirectory or file in it has name abc, that should also change to xyz.
Here is my code:
filepath="/misc/home3/babitasandooja/fol1"
for file in $(grep -lR $1 $filepath)
do
sed  -i "s/$1/$2/g" $file
echo "Modified: " $file
done

Now How should I code to replace the filename as well.
I tried:
if( *$1*==$file )
then
rename $1 $2 $filepath
fi

and
find -iname $filepath -exec mv $1 $2 \;

but any of then is not working correctly. What should I do? Which approach should I take?
Any help would be appreciated.
Thanks :)
Answer: #!/bin/bash
dir=$1 str=$2 rep=$3
while IFS= read -rd '' file; do
    sed  -i "s/$str/$rep/g" -- "$file"
    base=${file##*/} dir=${file%/*}
    [[ $base == *"$str"* ]] && mv "$file" "$dir/${base//$str/$rep}"
done < <(exec grep -ZFlR "$str" "$dir")

Usage:
bash script.sh dir string replacement

Note: rename would also rename the directory part.

grep -Z makes it produce null-delimited outputs. i.e. it produces output which is composed of filenames in which everything is separated by 0x00.
-d '' makes read read input delimited by 0x00; -r prevents
backslashes to be interpreted; and IFS= prevents word splitting with IFS.
IFS= read -rd '' makes read read input with delimiter of `0
base=${file##*/} removes the directory part. It's the same as base=$(basename "$file")
dir=${file%/*} removes the file part.
[[ $base == *"$str"* ]] checks if the filename has something that could be renamed. && makes the command that follows it execute if the previous returns zero (true) code. Think of it as a single link if statement.
"$dir/${base//$str/$rep}" forms the new filename. ${base//$str/$rep} replaces anything in $base that matches value of $str and replace it with value of $rep.
Comment: Thanks a lot.. It's working fine. Could you please explain it as I am new to shell scripting.
Comment: Thanks again...:) but if my directory is named as abc, its getting replaced by xyz. But if its abc.txt, then not. What change do I need to do ??
Comment: @user3542109 You can just use `rename` I suppose but that's going to be more complicated as you have to also manually create the directory. I think I misread your question so I'll just pass :) I hope it helped you anyhow.


Methods for imputation of missing values with spatially autocorrelated data

Question: I am looking at the spatial patterns of turnover in aquatic assemblages using gradient forest and generalized dissimilarity models in R. I have species and environmental data for more than 400 sites sampled from streams across the country. However, I also have many missing values in my predictor (environmental) variables, up to 30% for some variables, so removing the cases with incomplete data sets or replacing them by the mean will result in bias and loss of information. About the missingness pattern, my data is really an assembly of data collected by different county administrations, and the decisions about which variables to sample are made at the county level. For example, some counties have monitored total and dissolved nutrients but others have only routinely monitored total nutrients. The missingness pattern is thus affected by those decisions. The next matrix is an example of how my data would look like:
<code>     County V1  V2  V3  V4  V5  V6
 [1,] 10     52  6   35  294 48  25
 [2,] 10     22  7   41  53  42  NA
 [3,] 10     118 NA  55  82  59  NA
 [4,] 10     150 8   13  91  63  15
 [5,] 10     500 NA  NA  NA  102 9
 [6,] 9      58  7   NA  22  73  7
 [7,] 9      9   6.5 NA  38  152 17
 [8,] 9      9   7   NA  14  224 11
 [9,] 9      142 5.5 NA  57  64  11
[10,] 9      90  6   NA  102 66  NA
[11,] 6      30  7   9   NA  NA  11
[12,] 6      420 4.5 8   NA  NA  NA
[13,] 6      43  4.5 3.5 NA  NA  NA
[14,] 6      50  6.5 116 NA  NA  14
[15,] 6      10  NA  13  NA  NA  8
>
</code>
where "County" is the different county administrations that have provided data, and "V1 to V6" are the environmental variables.
In county 10 all variables are sampled but some are missing at random.
In county 9 variable V3 is not routinely monitored.
In county 6 variables V4, and V5 are not monitored, in addition there are missing values in the routinely measured variables due to, e.g. failure in the sampling device.
I cannot forget to mention that my data are also spatially autocorrelated at small spatial scales (<10 to 100km).
I would like to estimate those missing values but I don't know which method is the most appropriate to do so, and which R packages are recommended.
Answer: If you got all the missing data experts in a room, they probably wouldn't agree on which method is best, although they might agree that what is best depends very much on the precise goal and on what underlies your data.
While you are waiting for a missing data expert to answer, here is one amateur answer, although it draws on some experience with environmental data.
With your data you could try (a) to fill in missings spatially or you could try (b) to fill in using relationships between variables or you could try (c) a combination. In principle, using all the information you have sounds a good idea; in practice even (a) or (b) could be a lot of work and (c) could be a nightmare.
My own tendency would be to consider estimating missings as weighted combinations of neighbouring values, weights depending on distances between sampling points. Although it is not obvious from your listing, I guess you have a latitude and longitude or some equivalent somewhere. In principle that is a one-line equation for each variable; in practice it could still be a lot of work, dependent possibly on access to, and skills with, GIS software.
The set-up of counties seems, possibly, a red herring here. The streams don't know which county they are in. It's possible that being in a county is meaningful if there is something dependent on policy or efficiency, e.g. how lax or tough a county is in controlling pollution or other abuse of the rivers. Otherwise being in a county is just a proxy for spatial variation for other reasons (climate, soils, biota, geology, topography, land use, population pressure, etc., etc.) and distance-weighting will capture that as well as anything else.
You'd still need to compare anything you got with a run on the dataset trimmed of missings. The problem with imputing weighted averages is necessarily that you understate the real variability.
There are non-spatial methods (multiple imputation etc.) that people are more likely to recommend. I thought I'd speak up for a spatial interpolation method.
It is interesting sociologically, if immaterial statistically, that you talk about "the country" as if we knew what it was. That leads to a guess that you are from the United States.
Comment: Thanks a lot for your answer, Nick! It gave me some ideas and helped me google in the right direction. The spatial interpolation that you suggest, do you mean (co-)kriging? my guess is that it can be done in both GIS and R, but i have never used it before. Then I read a bit about multiple imputation, but I am not sure how to "average" the results from the imputations. Most examples use regression, etc. I will be using machine learning afterwards.
Comment: Kriging is good, but there are lots of ways to do it. Some kind of inverse distance-weighted interpolation is simpler to do and to explain.
Comment: Thanks a lot for your help. I will definitely try this.By the way, I am not from the USA, I am living in Sweden. Here, we have administrative divisions called "län" and some decisions are made at this level (also environmental). Maybe, "county" is not the best translation but I thought it was useful to understand the problem.


Rails add new and edit form in bootstrap modal

Question: I'm new with Ruby on Rails. Now I'm trying to make a simple ToDo List app with bootstrap.
Everything works but I tried to render my New task form to a modal window and it's crashed. Here is my _form partial code:

          <button type="button" class="close" data-dismiss="modal">×</button>

Modal Header

New Task

          <%= form_for @task, :html => {class:"form-horizontal"} do |f|%>

          <% end %>
          <%= link_to 'Back', tasks_path %>

          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

And here is my Index.html.erb:

Task List

        <%= button_to 'New Task', new_task_path, :class =>"btn btn-success pull-right", :method => "get", data: {'data-toggle' => "modal", 'data-targe' => "#myModal"} %>

    <% @tasks.each do |task| %>
            <div class="panel <%= task_status(task) %>">

                    <%= task.title %>

<%= task.dueDate %>

<%= task.body %>

        <% end %>

    <%= render "tasks/form" %>

So when trying to start my index page I get an error First argument in form cannot contain nil or be empty.
Whats wrong with my code? And how can I pass an existing task to the form for Edit action?
UPD:
Method for new task in task controller is:
class TasksController < ApplicationController
    def index
        @tasks=Task.all
    end

    def new
        @task=Task.new
    end

    def show
        @task=Task.find(params[:id])
    end

    def create
        @task=Task.new(task_params)
        if @task.save
            redirect_to @task
        else
            render 'new'
        end
    end

    private
        def task_params
            params.require(:task).permit(:title, :body, :creationDate, :dueDate)
        end
end

Comment: What does your task_controller says?, better, what's in your task_controller?
Comment: you need to add @task = Task.new in index action as well.
Comment: You're trying to access to your new task form within your index file, so the @task = Task.new must be on your index method.
Answer: The @task variable in your form_for can't be nil nor [].
You could "hack" the Rails behavior and explictly declare your form like this:
<%= form_for Task.new, :html => {class:"form-horizontal"} do |f| %>
...

But it wouldn't be the "correct" way to use form_for, nor to use Rails, because you wouldn't be following the MVC patterns, so, instance variables from controllers to the views, no directly in the view.
As I can't see what's in your method in the tasks_controller, maybe you've forgot to set the Task.new value to your @task variable, try setting it as:
class Task < ApplicationController

  def index
    @task = Task.new
    ...
  end
end

Hope it helps.
EDIT: You're trying to access to your new task form within your index file, so the @task = Task.new must be on your index method.
Comment: Yeap, my method now is
def new
  @task=Task.new
end
Answer: Error message specify that first argument ie @task is nil.
So change your controller code
def index
  @task = Task.new
  @tasks = Task.all
end

def edit
  @task = Task.find(params[:id])
end


Why this code does not compile?

Question: Perhaps I'm crazy, but this code fails to compile in Arduino IDE 1.0.5 & 1.0.6.
class Foobar {};

void myFunction(const Foobar& n) {
}

void setup() {
}

void loop() {
}

The compiler outputs the following errors:
Arduino: 1.0.6 (Windows 7), Board: "Arduino Mega 2560 or Mega ADK"
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega2560 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -IC:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\variants\mega C:\Users\Alvaro\AppData\Local\Temp\build5499093930419069947.tmp\Max7219Testing.cpp -o C:\Users\Alvaro\AppData\Local\Temp\build5499093930419069947.tmp\Max7219Testing.cpp.o

Max7219Testing:2: error: expected ',' or '...' before '&' token
Max7219Testing:2: error: ISO C++ forbids declaration of 'Foobar' with no type

What's wrong here?
Comment: Maybe your source file has a stray (control) character?  It compiles fine here: http://ideone.com/AuDEQL
Comment: Post the intermediate .cpp file found in the temporary directory.
Answer: The code compiles cleanly here:
http://ideone.com/AuDEQL
class Foobar {};

void myFunction(const Foobar& n) {
}

void setup() {
}

void loop() {
}

int main() {}

If the code is as you've posted, the cause is usually
1) An invisible character (usually a control character) is embedded on one of the previous lines where the error occurs, and the C++ parser is picking it up as a syntax error
or
2) The characters you are seeing in your source file are not ASCII, but some "fancified" version of the character (thus either Unicode or some other encoding).  This can happen if you copy and paste from an application that uses the alternate encoding to make the text "look good", for example, a word processor such as MS Word.
I would take the file and inspect it in a hex editor to ensure it doesn't have such characters.
Edit:  The other scenario, which was pointed out in the comments by Ignacio Vazquez-Abrams is
3) you could be compiling a different file than the one you have posted.  This seems to be the case for Arduino IDE.
Comment: Nah, it's the Arduino IDE adding extra crap that turns it into not-C++.
Comment: It's not an invisible character. Wait until the file I asked for is posted and you'll see what I mean.
Comment: Well, it is a variation of item 1)  :)
Comment: My first impression was that there was some invisible character. Then, I wrote the same code manually in another computer, with a different OS, with the same result.
Comment: @IgnacioVazquez-Abrams Edited the answer to add the Arduino IDE issue.
Answer: Oh my $DEITY!
Following Ignacio suggestions, I checked out the final assembled cpp file that Arduino generates and the build system tries to compile:
#line 1 "sketch_feb05b.ino"
#include "Arduino.h"
void myFunction(const Foobar& n);
void setup();
void loop();
#line 1
class Foobar {};

void myFunction(const Foobar& n) {
}

void setup() {
}

void loop() {
}

So the IDE generates the declarations of the functions and puts them in the beginning of the file, before the types are declared.
No invisible symbols after all but a terrible decision of Arduino authors :-(
I wonder whether putting the types in external header files would fix it.

Why this code does not compile?


Regression with linear transformed matrix

Question: Intuitively (formal explanation is also welcomed), what would happen if we use a linear transformed matrix in regression?
$\mathbf{XZ}$ instead of $\mathbf{X}$ in $Y = \mathbf{X}\beta +\varepsilon$. I think we can still get consistent estimators, but no more unbiased?
Comment: Suppose $Z$ is of full rank, so that you can find a $\gamma$ for which $Z\gamma=\beta$.  What is the difference between the models $Y=X\beta+\varepsilon$ and $y=XZ\gamma + \varepsilon$?
Answer: If your matrix is created randomly, then you can still expect estimators of the same accuracy. This property is used in popular methods such as random projections. Random projections are used to do dimensionality reduction, with very little actual cost.


Algorithm to find shortest path to net values across nodes

Question: I have an undirected graph $G = (V, E)$ with nodes $V$ and edges $E$.
Each node $v$ has an associated number $n_v \in \mathbf{Z}$
Let us define for any two nodes $v, w \in V$ connected by an edge $e \in E$, that to net node $v$ with node $w$ means that we set $n_v = 0$ and set $n_w = n_w + n_v$. We also define that the cost of such a netting operation is $|n_v|f(e) \geq 0$ where $f : E \rightarrow[0,\infty)$ is a function describing unit cost of netting between $v, w$.
As an example:

In the above image, the big numbers in each node $v$ represent $n_v$ and the little numbers along each vertex $e$ represent $f(e)$.
The Question
By only netting nodes, I wish to make node $Z$ (green) the only non-zero node. I also want this done at minimal cost.
Does anyone know of an algorithm that does this at minimal cost?
In the above example, we should net node $A$ with $C$ (with a cost of $|-2|\times1$) and $B$ with $C$ (with a cost of $|-3|\times1$). All nodes apart from $Z$ are then $0$ with a total cost of $5$.
Motivation
Using an application for motivation, imagine we are a charity distributing medicine across cities in a country with a viral outbreak. The situation is rapidly evolving as the virus spreads or as people get better, and so some cities may find they have a shortage of medicine while others have surplus medicine.
The situation may look like:

We need to move medicine from where there is a surplus to where there is a shortage. The medicine can be transported from one city to another via interconnecting roads. There is a transportation cost involved in transporting each unit of medicine. Equivalently, we can move people from one city's hospital to another (we assume the same cost of transportation as medicine for simplicity, i.e. the undirected graph case).
We wish to ensure that all cities have neither a surplus nor a shortage of medicine. Any surplus must be moved back to the depot. Any shortage must also be moved back to the depot (this represents the scenario when we need to create and distribute new medicine from the medicine depot).
Comment: First observations: if all $n_v$ are non-negative (which you state in the problem, even though your example has negative values as well) and all edge costs are non-negative as well, then you can suffice with a single iteration of Dijkstra's algorithm starting from $Z$. (Let all potential flow towards $Z$ along a shortest path.) Secondly, if there are negative cost cycles, then a minimum may not exist. Finally, if the graph is not connected, then a solution need not even exist. Apart from that, I've had no luck so far solving the general case.
Comment: Many thanks Josse - to address an immediate point I apologise that I made a confusing typo: it should read $n_v \in \mathbf{Z}$. I will respond to the rest of your comment when I get back home tonight :)
Comment: @JossevanDobbendeBruyn as for the existence of a minimum with negative cost cycles, consider the medicine transportation scenario outlined in the Motivation: I cannot see why the minimum cost of transporting medicine to fulfill medicine shortages should not exist. Please could you give an example.
Comment: Well, suppose that we have a cycle of length $k \geq 3$ where every edge has cost $-1$. Choose some $v_0 \in V$ and set $n_w = 0$ for all $w \neq v_0$ as well as $n_{v_0} = 1$. Now add a sink $Z$ outside of the cycle which is connected to every vertex along the cycle with edge cost $0$. Now we can move the one unit of potential around in cycles and keep the cost decreasing. Of course, if we require that edge costs are non-negative, then this situation cannot occur, and the general problem may or may not be a lot easier as a result.
Comment: @JossevanDobbendeBruyn, you are right: I should have also stated that edge costs are non-negative.
Comment: I think this is essentially a [transportation problem](http://www.me.utexas.edu/~jensen/models/network/net8.html), isn't it?
Comment: @Rahul just skimmed your link and it does look like it fits my problem - thanks! It's midnight now, so I will finish reading your link and get back to you in the morning :)
Answer: Seems to be solved immediately by transforming it into a min cost max flow problem (with multiple sources and sinks). First, calculate the sum of all the nodes' values --- this will be node Z's final value (the final value for all other nodes are by definition 0). For a node, consider the difference between their final value and their initial value -- if the initial value is larger, you need to move some of its content around, otherwise it will receive some things from other nodes. Thus, for the former case, it can be treated as one of the multiple sources, and the latter case is treated as one of the multiple sinks.
Each edge shall have infinite capacity and have a cost equal to $f(e)$. The cost of the min-cost max-flow of this graph is exactly the minimum cost required.
Note that this will work even if there are edges with negative value, but negative cycles will result in the algorithm working incorrectly.
EDIT: whoops, didn't read the comments.


Why is the textarea control inserting a line break into the control's value when wrapping?

Question: A line break is inserted into the textarea's value even though the user didn't insert a new line.
I'd like to continue to use text wrapping in the control and allow users to enter line breaks when desired. But the insertion of line breaks by the browser when wrapping text isn't acceptable.
This shouldn't be happening as the textarea is set to soft wrap.
<textarea type="text" id="Answers_Text" runat="server" class="answerTextBox" maxlength="256" wrap="soft"></textarea>

.answerTextBox {
    height: 60px;
    width: 580px;
    font-size: 14px;
    white-space: pre-wrap;
    word-break: normal;
}

If you type a sentence that fits the width of the textarea exactly, and then enter a whitespace and start typing a new sentence, the second sentence wraps correctly to the second row, and doesn't insert a line break. However, if you enter a single white space as the first character of the second sentence, a literal line break is inserted at the start of that sentence, replacing the soft line break that is usually done when wrapping text.
I've tried testing this in Chrome and Firefox, and this hard line break is only being inserted in Chrome. To my understanding, this should only be happening if I set the wrap property to 'hard'. Is there a setting I'm missing? Or is this expected behavior?
Comment: Well, the textarea is not a self closing element ([source](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea)).
Can you make a jsfiddle so we can see the problem?
Comment: Have you tried closing the textarea element?
`
`
Comment: It's a self-closing tag right now. As far as I know, this element allows self-closing. I did try changing to an explicit end tag, same results.
Comment: Ah, my apologies then. Here's the fiddle link with the proper ending tag:  [link](https://jsfiddle.net/0yd59r14/)
Comment: Thanks for the fiddle. I can confirm that the issue occurs in Chrome on Windows 8.1, but not in Firefox. Just as @Godot said.
I played around with css, but couldn't find a solution. I would say it is a weird behaviour of Chrome browser.
Answer: Close <textarea></textarea> tag!
<textarea type="text" id="Answers_Text" runat="server" class="answerTextBox" maxlength="256" wrap="soft" ></textarea>

Comment: Edited the question to close the tag. Issue still occurs.


Two random variables with covariance $0$ but dependent

Question: Let $(X,Y)$ a random vector uniformly distributed on ball of center $(0,0)$ and
radius $r$.
Prove that $Cov(X,Y)=0$, but $X$ and $Y$ aren't independent.
Any idea?
Answer: Hint 1: Symmetry.
Hint 2: Consider cross section of a disc.
Answer: For some $\theta$ uniformly distributed on $[0, 2\pi]$ and some $a$ uniformly distributed on $[0, r]$, we can say $X:=a\cos(\theta)$ and $Y:=a\sin(\theta)$ to get the restriction you described (in a 2D ball at the origin with radius r). Clearly $X$ and $Y$ are dependent since a measurement of one tells you about the other. Now you just have to show that $\text{cov}(X, Y) = 0$. It has to do with the orthogonality of $\sin$ and $\cos$.
Comment: Actually unless the measurement happens to be exactly $r$ (assuming it is a closed ball) I'm not so sure that their dependence is obvious. Are you sure they are dependent? By any chance did you not mean for $X$ and $Y$ to be able to take values in the interior?
Comment: $P(X > {r \over \sqrt{2}} | Y > {r \over \sqrt{2}}) = 0$ but $P(X > {r \over \sqrt{2}} | Y < {r \over \sqrt{2}}) > 0$ so they are dependent.
Comment: Ah, I figured something like that would be the case! Thanks


On Noteflight, I would like to change the staff (score) key signature. How do I do this without keeping absolute pitch? (It adds accidentals.)

Question: Here is what it looks like before:

The key signature change only adds accidentals to the 'affected" notes. Thus, even a staff/score transposition does not solve my problem:

If the score were long, I would have to change many notes by hand. (However, I am just trying to identify the piece, as a Scarlatti Sonata number, from the piano score to Tommasini's ballet Le Donne di buon umore.)
Comment: Shouldn't stuff like this be asked from customer support?
Comment: About the sonatas, have a look at the French wikipedia page on the ballet: https://fr.wikipedia.org/wiki/Le_donne_de_buon_umore
Comment: @musicamante Yes, I have saved all the YouTube videos (and hundreds of Scarlatti), however the orchestral listing has about 7 movements while this has thirteen.
Comment: @piiperiReinstateMonica I was looking for user-involved feedback for faster response. After cleaning it up, it appears to K. 474: https://www.youtube.com/watch?v=PQnI0AY8IMI
Answer: On Noteflight, the shift key plus either up arrow or down arrow will transpose the selected elements up or downwards respectively by a chromatic semitone. This can be used in conjunction with the key signature change to transpose the music to a new key with a new key signature.
The example passage is in the key of C major (or whatever key the no-sharps-or-flats signature happens to be representing here). When changing the key signature by adding 3 flats, you've changed the key signature by three semitones (coincidence) from C to Eb major. Your key signature is written in Eb, but your music is still sounding in C since the change key signature function doesn't move any of the notes in Noteflight. In order to get the correct notes in Eb, simply select the entire passage, then hold down the shift key and press the up arrow three times. You may need to use the z key as well for enharmonic respellings of notes (D#=Eb=Fbb, et cetera), especially for chromatic notes.

There may also be a specific transpose feature built into Noteflight that does what the question asks, which I leave as an exercise to the reader to go research.
Comment: There is indeed a 'Transpose' feature, in the 'Pitch' menu.  Keep the 'Change key signatures' box checked, and it will automatically move the notes AND update the key signature.
Comment: > *simply select the entire passage, then hold down the shift key and press the up arrow three times.*
The problem is, the passage is preserved so only the now-accidental notes are natural, as you can see. I had to move them individually; I don't know if there is a faster way. I assume that would shift the entire passage.
Comment: @MarkC I don't quite understand what you're describing; if you changed the key signature and then shifted the whole passage upwards the correct amount, that should have taken care of it. You might need to adjust a *few* notes to their correct enharmonic spellings with the "z" key, but it should be almost perfect by that point.
Comment: You can see the exact result in Black above. I saw no option to apply the new accidentals to the existing notes oh, so it leave them exactly as written pitch wise.
Comment: @MarkC No, in black above it is very clear that **you have not used the up-arrow to move the notes yet.** I'll prove it: the first note of the treble clef highlighted in orange is an E natural (in C major, no accidental is necessary). Now in the black image, you have changed the key signature to that of Eb, which is not supposed to change any of the notes - it just *spells* the exact same music in Eb major.
Comment: @MarkC Do you see that the first note of the treble staff in black is still an E natural? It hasn't changed. If you had moved the notes correctly, your note should now be a G since we want to be in the key of Eb. So now that you have the image in black, what you need to do is **highlight the entire passage again, then hold shift and hit the up arrow three times**. If you did this correctly, your first note in the treble staff is a G without any accidentals on it and almost all of the rest of the music will be correctly written in Eb without any accidentals.
Comment: (And on the off chance that this still isn't getting you to what you want, then either your arrow keys are broken or I must ask whether you truly understand key signatures and transpositions.) @MarkC
Comment: That was outrageously off-the-mark and hasty in extreme.
Comment: Hello, @MarkC. Did you end up solving your problem? I don't understand what you're saying.


Sempre apresenta um erro quando tento usar a expressão mysqli

Question: sei que a expressão mysql foi descontinuada, sendo assim, estou tentando usar a expressão mysqli, porém sempre que tento mostra-me na tela um erro de sintaxe!
exemplo:
$buscaDados = mysqli_query("SELECT * FROM usuario");

Gera este erro:

Warning: mysqli_query() expects at least 2 parameters

Como corrijo esse erro?
Answer: É necessario passar dois agumentos quando o 'modo procedural' é usado. O primeiro é a conexão e o segundo a consulta.
mysqli_query

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode =
MYSQLI_STORE_RESULT ] )

Seu código deve ficar assim:
$buscaDados = mysqli_query($conexao, "SELECT * FROM usuario");

Answer: Veja a assinatura do método no manual

mixed mysqli_query (mysqli $link, string $query [, int $resultmode = MYSQLI_STORE_RESULT ])

Isso quer dizer que você precisa passar a conexão (que é um valor do tipo resource) como primeiro parâmetro, e a query como segundo.
Na versão orientada a objeto é possível usar um parâmetro apenas:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$buscaDados = $mysqli->query("SELECT * FROM usuario");


How to show timer only for pages with time limit in SurveyJS?

Question: When I run a SurveyJS survey with showTimerPanel='top', the timer appears everywhere. Can I make it appear only on certain pages? Or at least the pages with time limit.
Answer: You can setup the timer panel and start/stop timer on starting the survey and on current page change. Here is the code and example in plunker.
function setupTimerPanel(survey) {
    const showTimer = survey.currentPage.maxTimeToFinish;
    survey.showTimerPanel = showTimer ? "top" : "none";
    if(showTimer) {
        survey.startTimer();
    } else {
        survey.stopTimer();
    }
}
survey.onCurrentPageChanged.add((sender, options) => {
   setupTimerPanel(survey);
});
survey.onStarted.add((sender, options) => {
    setupTimerPanel(survey);
});

Please note, if you don't have a started page, then you need to call setupTimerPanel(survey); for your first page after creating the Survey model.
Thank you,
Andrew


opacity value for my C# form isn't adjusting

Question: I wanted to make one of my forms opaque. I did this by changing the opacity value in the form between 0 and 100. I later wanted to change this value during run-time, so i wrote some code:
Opacity = 80;

However my form doesn't seem to change opacity. Does anyone know what i'm doing wrong.
Comment: I'm not sure enough to write as an answer, but I believe opacity is from 0 to 1, so you'd want 0.80, though I could be confusing myself with UWP.
Comment: @gilliduck, you are correct and you should post as an answer. Opacity works between 0 and 1. so a value of 0.8 is correct.
Comment: @HandbagCrab thanks for the confirmation.
Answer: The Form.Opacity property ranges from 0 to 1. So to achieve 80% opacity, you would write Opacity = 0.8
Comment: Thank you so much, kind of confusing how it works like that


Reload  with jQuery and PHP

Question: I have an index.php that updates every second user sessions:
[Index.php (before )]

[reload_info.post.php]
(sql query... // $row is the result)
$_SESSION['name'] = $row->name;
$_SESSION['mail'] = $row->mail;
$_SESSION['status'] = $row->stat;
$_SESSION['cf'] = $row->cf;

All functions of the site using any session are usually charged if there is any change in the database (MySQL). What I would like is to update the body of index.php () without reloading the page.
That is, if I open the index.php file and have nothing between the   and editing the file I put any text after this a second, carrying the text without having to press F5 or take a location.reload ().
Is it possible? Thank you. :)
Comment: What's the point in reloading the entire body?  Why not just the parts you need to change?  I guess I don't see the benefit in not reloading the page at this point.
Comment: Inside the  I just have to `div.container` and nothing more.
Answer: you want something like this?

in reload_info.post.php you should return something...
echo echo $_SESSION['id'];
Comment: replace() is a really function?!
Comment: No, that disappeared with all content (text only lorem ipsum) and didn't return anything...
Comment: will update, as i understand, but mixig the php and js is bad idea
Comment: Hm, I understood. Well, I appreciate your help!
Comment: Yeah! That's really what I want.
Answer: Perhaps you are after something like joconut: https://github.com/vdemedes/joconut

Reload  with jQuery and PHP


Undefined reference (but nm says the function exists)

Question: I have a library from a camera manufacturer, and in the demo code provided, a function called metadata_init() works fine, in my code though, I get an undefined reference error.
The make output from the demo code:
/opt/linaro-multilib-2013.09-gcc4.8/bin/arm-linux-gnueabihf-gcc -lm -g   -L"/home/aro/Downloads"  -o hicore  demoS2.c  -lpthread -lyuvlib  -lrt

and thats it, the build succeeds fine, I will have a working ./hicore application in there.
My project is a little bit more complicated, and I compile using eclipse.
The console output is:
11:33:23 **** Build of configuration Camera-R4-Debug for project Camera ****
make all
Building file: ../src/Camera.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Camera.d" -MT"src/Camera.o" -o "src/Camera.o" "../src/Camera.cpp"
../src/Camera.cpp: In function 'int metadata_construct_http_message(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*)':
../src/Camera.cpp:379:24: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
                        "</svg>\r\n";
                        ^
../src/Camera.cpp: In function 'int main()':
../src/Camera.cpp:426:97: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
  (void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
                                                                                ^
Finished building: ../src/Camera.cpp

Building file: ../src/CameraFrameGrabber.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraFrameGrabber.d" -MT"src/CameraFrameGrabber.o" -o "src/CameraFrameGrabber.o" "../src/CameraFrameGrabber.cpp"
Finished building: ../src/CameraFrameGrabber.cpp

Building file: ../src/CameraLogger.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraLogger.d" -MT"src/CameraLogger.o" -o "src/CameraLogger.o" "../src/CameraLogger.cpp"
Finished building: ../src/CameraLogger.cpp

Building file: ../src/CameraParameter.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraParameter.d" -MT"src/CameraParameter.o" -o "src/CameraParameter.o" "../src/CameraParameter.cpp"
Finished building: ../src/CameraParameter.cpp

Building target: Camera
Invoking: Cross G++ Linker
arm-linux-gnueabihf-g++ -L/opt/Camerasdk/R4/lib -L"/cameraBuilds/Wrapper/Camera-R4-Debug" -L"/opt/ExternalLibraries/curl/Camera/R4/lib" -L"/opt/ExternalLibraries/libxml2/Camera/R4/lib" -L"/opt/ExternalLibraries/OpenCV24/Camera/R4/lib" -L"/opt/DetectionModules/Camera/R4/lib" -L/home/aro/Downloads -o "Camera"  ./src/Camera.o ./src/CameraFrameGrabber.o ./src/CameraLogger.o ./src/CameraParameter.o   -lWrapper -lxml2 -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lyuvlib -lrt -llibjasper -llibjpeg -llibpng -llibtiff -lzlib -lcurl
./src/Camera.o: In function `main':
/home/aro/cameraBuilds/Camera/Camera-R4-Debug/../src/Camera.cpp:426: undefined reference to `metadata_init(char*, int, int (*)(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*))'
collect2: error: ld returned 1 exit status
make: *** [Camera] Error 1

11:33:24 Build Finished (took 1s.212ms)

The code itself is the very same thing, I just copied it over.
Metadata.h:
#ifndef _HIK_METADATA_H_
#define _HIK_METADATA_H_

const int max_http_body_len = 100 * 1024;

typedef enum
{
    CMD_ADD_TYPE = 1,
    CMD_OTHER,
} METADATA_CTRL_TYPE;

typedef struct
{
    int length;
    int fd;
    METADATA_CTRL_TYPE cmd_type;
} METADATA_HEADER;

typedef struct
{
    char option[128];
    int  share_socket;
} METADATA_ADD_CFG;

typedef enum
{
    HTTP_HEADER_TYPE = 1,
    HTTP_BODY_TYPE,
} METADATA_HTTP_MESSAGE_TYPE;

typedef struct
{
    char boundary[64];
    char http_content_type[64];
    char multipart_content_type[64];
} METADATA_MULTIPART_TYPE;

typedef int (*p_metadata_construct_http_msg_callback_f)(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len);

int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);

#endif

In Camera.cpp:
#include "Metadata.h"
int metadata_construct_http_message(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len)
    {
        // Removed for SO
        return 0;
    }

    int main()
    {
        (void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
    ...
    }

What causes this, and how can I debug this issue to narrow down how to fix it?
Comment: And where *is* `metadata_init` defined? Where is its implementation?
Comment: Also, I strongly suggest you take a closer look at those warnings. They are there for a reason.
Comment: It is in the libyuvlib.a provided by the camera manufacturer. I can find it with nm, its listed there, but I can't get it to work on my project.
Comment: Yeah, the warnings are all in the new demo code. I will remove them once the code is working.
Answer: It think the problem could be that metadata_init is a C function, but you use it from a C++ code.
In case of this, extern "C" must be used in the header file, like this:
#ifdef  __cplusplus
extern "C" {
#endif

// embed the whole contents of the header file here, I just put the function here for brevity

int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);

#ifdef  __cplusplus
}
#endif

It is because of name mangling rules. Names are mangled differently in C and C++. With extern "C", you tell the compiler that the names inside should be used with "C" mangling.
C++ has to do a complex name mangling compared to C, because it has to embed almost all signature information to a name (all parameters type), while in C, the mangled name usually is the same as the function name, or there is a _ prepended.
Comment: Yes, that did it. Thanks for the help.


How can I verify my linear program solutions?

Question: I started solving linear programs with the Simplex algorithm, however it is unclear to me how can I verify my solutions.
I have heard about geometrical solutions easy to check visually, but I'd rather do it just with algebra. How can I verify my linear program solutions?
For context, here is the particular problem I just solved I'd like to verify:

$$max \ (3x_1-x_2)$$
$$\begin{cases}
x_1-x_2 \le 3\\
\color{red}{2x_1\le x_2}\\
x_1+x_2\ge 12\\
x_2 \le 10\\
x_1,x_2 \ge 0
\end{cases}$$
Change it to
$$\begin{cases}
x_1-x_2 \le 3\\
\color{red}{2x_1 - x_2 \le 0}\\
x_1+x_2\ge 12\\
x_2 \le 10\\
x_1,x_2 \ge 0
\end{cases}$$
I transform it to the standard form...
$$\begin{cases}
x_1-x_2 + x_3 = 3\\
2x_1 - x_2 + 0 + x_4 = 0\\
x_1+x_2 + 0 + 0 - x_5 = 12\\
0 + x_2 + 0 + 0 + 0 + x_6 = 10\\
x_1,x_2 \ge 0
\end{cases}$$

$$\begin{bmatrix}
1 & -1 & 1 & 0 & 0 & 0 & 3 \\
2 & -1 & 0 & 1 & 0 & 0 & 0 \\
1 & 1 & 0 & 0 & -1 & 0 & 12 \\
0 & 1 & 0 & 0 & 0 & 1 & 10 \\
3 & -1 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}$$
The pivot column is the second one, and the pivot row is the fourth. So we got our pivot:
$$\begin{bmatrix}
1 & -1 & 1 & 0 & 0 & 0 & 3 \\
2 & -1 & 0 & 1 & 0 & 0 & 0 \\
1 & 1 & 0 & 0 & -1 & 0 & 12 \\
0 & [1] & 0 & 0 & 0 & 1 & 10 \\
3 & -1 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}$$
$$r_4 + r_1$$
$$r_4 + r_2$$
$$-r_4 + r_3$$
$$r_4 + r_5$$
$$\begin{bmatrix}
1 & 0 & 1 & 0 & 0 & 1 & 13 \\
2 & 0 & 0 & 1 & 0 & 1 & 10 \\
1 & 0 & 0 & 0 & -1 & -1 & 2 \\
0 & [1] & 0 & 0 & 0 & 1 & 10 \\
3 & 0 & 0 & 0 & 0 & 1 & 10
\end{bmatrix}$$
The objective row has no negative terms, so we're done. The basic feasible solution here is
$$(x_1, x_2, x_3, x_4, x_5, x_6) = (0,10,13,10,0,0)$$
Now we evaluate this solution with the objective function:
$$3x_1-x_2 = 3(0)-10 = -10$$
Comment: If you look at the $x_1 + x_2 \geq 12$ constraint, does your answer satisfy it?
Comment: @NigelOvermars: Good thing I'm seeking a way to verify those answers :D
Comment: It puzzles me though - I can't quite find the flaw in the procedure.
Comment: Do you know how the Simplex algorithm works geometrically?
Comment: @NigelOvermars: Somewhat - you kind of draw a polygon based on the constraints and inspect to see what would be the maximum and minimum values? However, I may be wrong, but doesn't this only work if you are using two variables? Three or more wouldn't be possible I think?
Comment: https://www.dropbox.com/s/wcbj3cj5blqcpbv/03%20SimplexMethod1%20Handouts2pp.pdf https://www.dropbox.com/s/3unno1uvx1rv5gt/04%20SimplexMethod2%20Handouts2pp.pdf
Comment: The parts about the M-method and two-phase method are most relevant for you, but it might be useful to look at the rest, since you do not quite understand how the Simplex algorithm works.
Answer: For verifying a solution, Wolfram Alpha might be useful (also free and fast) if you do not actually have Mathematica. I actually wrote the following up in Mathematica then pasted that into WA. You should be able to duplicate this for your other checks:
Maximize[{3*x1 - x2,
x1 - x2 <= 3 && 2 x1 <= x2 && x1 + x2 >= 12 && x2 <= 10 && x1 >= 0 &&
x2 >= 0}, {x1, x2}]

Note that Minimize works similarly.


Server's DNS cannot resolve a third party web service URL

Question: One of our projects encountered a strange issue today: the VPS server cannot reach a third party web service, because it cannot resolve its domain. When I SSH to the server and run dig, curl, traceroute, or ping to the web service's domain, I get the error Could not resolve host: %web.service.url%.
Interestingly, another server of the same hosting company located in the same region (Australia), has the exact same problem, so it must be a wider problem than just one particular server. Pinging the same service from local computer works fine, and other servers also do not have this issue. The guys from technical support weren't able to help us for several hours, their last message said:
"Your options are to contact the DNS provider you are using as the DNS servers are not providing any records for the domain."
I am not sure what is a DNS provider, and why they sound like we are using some custom one rather than something that they provide for their VPS servers. Could it be an issue between them and Australian ISP?
And most importantly, what are our options now, except moving everything to another hosting provider? Switching the third party web service is not an option at the moment.
EDIT:
Below is the result of the dig %web.service.url%, when run from the server:
; <<>> DiG 9.9.5-4.3ubuntu0.1-Ubuntu <<>> %web.service.url%
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 35626
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1500
;; QUESTION SECTION:
;%web.service.url%.        IN  A

;; Query time: 1074 msec
;; SERVER: %server.ip.address%#53(%server.ip.address%)
;; WHEN: Thu Jul 20 09:19:44 UTC 2017
;; MSG SIZE  rcvd: 51

Answer: You've blocked out all the important information needed to actually do some testing for you. If the VPS servers are using DNS servers provided by the VPS host, and it fails to return records, then the problem should be with the VPS host.
A simple work around is to change the DNS settings on the VPS servers so that you use another DNS server. I typically use Google's DNS servers: 220.127.116.11 and 184.108.40.206
Comment: How can I do this? Is this done via SSH (I do have root access to it, if it's important), or via DNS settings (A, CNAME records, etc.), or something else?
Comment: It's done on the server. How to do that is dependent on what operating system it is running. It's typically going to be in the network configuration. Yes, via SSH.
Comment: thank you very much for your help! If someone is like me, [this article here](https://www.computerbeginnersguides.com/blog/2014/04/29/set-a-static-dns-server-address-in-ubuntu-14-04-trusty-tahr/) has nice step-by-step instructions that worked on Ubuntu 14.10.
Comment: @parrker9 Keep in mind that different DNS servers might simply mean that there is an RFC violation that the software on the other servers plays with more gracefully. Changing the DNS servers is not always an option in places on your network and this problem may return in some fashion. In the future, please provide the name of the malfunctioning domain so that we can troubleshoot various scenarios.


Making an ecommerce photography competition website

Question: This has been bugging me for days, I just can't wrap my head around this situation/process and how to make it work in Drupal.
I'm making a photography competition website where a user who signs up and buys a role has the choice to upload 3(costs £4), 10(costs £6) or 20(costs £10) images but each image needs to be a part of a competition category, so say someone uploads 3 images, one is for black and white category (taxonomy term), one is for wide angle and one is for sport, each image needs to be tagged with the correct category, of which im using taxonomy terms. But how do i go about this?
Firstly limiting the user to said amount of photos? I was thinking they buy a role (there are 3 roles), role one allows to upload 3 images using the content type 3 images, role two allowed to upload 10 images on a node and the final content type is '20 images' and they can upload up to 20 images using that content type, each user though must be able to categorise each individual image using the taxonomy vocab already created called Photo Categories. How the hell do i tag each individual image so that they are categorised and ready for viewing/judging later on??
Should I do this on the user profile instead? So that when they buy their role, they can then upload the amount of photos that they are allowed to? This will still mean I have the problem of assigning each image to a taxonomy term.
Also, is purchasing a role the best way to go about this? Now that you know what I am after what are your thoughts? I know Drupal can do this 'out of the box', i'm just struggling to see what workflow the end user will go through to achieve all of this.
Answer: I would look at Node Limit. So the user buys roles and that gives them access to create more content.
Then create a content type for Entry with all the appropriate tag fields.
I like the purchasing the role structure. I wouldn't do this directly on the user
Comment: Correct, I would associate the nodes with the user via author rather than creating the images as a field on the user entity
Comment: So you wouldn't add the images directly on the user? Is that what you are saying? All the rest sounds feasible and like it could be managed well.
Answer: There are different ways to go about this, but this doesn't have to be that difficult.
I would go for a process, where the user creates the nodes during checkout. This is a simple approach, as he can only create the nodes during purchase time. It sounds like that could work well for your situation.
There is a module that does something like what you explain: https://www.drupal.org/project/commerce_node_checkout
You should also be able to set this up with Rules and fields.
You could create a field collection on the line item for buying images. The field could be unlimited values where each item would be an image that needs to be created. You would then attach fields on the field collection that you require for each image, (the image file, category, and other data about the image)
You could create a price rule that would count the number of items on the field collection and calculate the price based on that.
You could create another rule executed at checkout complete where you loop through each item in the field collection and create a node with the values supplied. Then it's just a matter of requiring the user to input terms or other meta data you want/need in the field collection.
Comment: I like this approach, but feel it's slightly too rigid for the end user who is competing in the competition. Actually perfect for another site I'm working on with a client where they run a directory and wish to charge for submissions.


"There is no index available for this selector" despite the fact I made one

Question: In my data, I have two fields that I want to use as an index together. They are sensorid (any string) and timestamp (yyyy-mm-dd hh:mm:ss).
So I made an index for these two using the Cloudant index generator. This was created successfully and it appears as a design document.
{
  "index": {
    "fields": [
      {
    "name": "sensorid",
    "type": "string"
      },
      {
    "name": "timestamp",
    "type": "string"
      }
    ]
  },
  "type": "text"
}

However, when I try to make the following query to find all documents with a timestamp newer than some value, I am told there is no index available for the selector:
{
  "selector": {
    "timestamp": {
      "$gt": "2015-10-13 16:00:00"
    }
  },
  "fields": [
    "_id",
    "_rev"
  ],
  "sort": [
    {
      "_id": "asc"
    }
  ]
}

What have I done wrong?
Answer: It seems to me like cloudant query only allows sorting on fields that are part of the selector.
Therefore your selector should include the _id field and look like:
   "selector":{
      "_id":{
         "$gt":0
      },
      "timestamp":{
         "$gt":"2015-10-13 16:00:00"
      }
   }

I hope this works for you!


The "sides" of a k-regular bipartite graph are equal?

Question: I was reviewing some lectures notes and noticed that in a proof of a theorem our lecturer stated that the "sides" of a k-regular bipartite graph are equal and that it is trivial to prove it. Anyway I've been trying to prove it but I'm not to sure about my answer.
Ok so this is my try with a proof by contradiction :
Let G(V=A $U$ B, E) be a k regular bipartite graph, Need to show that $|A|$ = $|B|$.
Let  $|A|$ = n and  $|B|$ = m. Let n $!=$  m. lets say n $>$ m ( could be other way around)
since the graph is k regular we need to "distribute" k*n edges from A to B, but since n > m
there will always be a vertex in B with a degree of at least k+1. contradiction
Not to sure about the this proof, sorry about the English
Thanks!
Comment: The proof is fine.
Answer: Your proof is fine, though one could simplify it (i.e. there is no need to do a proof by contradiction): As you write, the number of edges in the graph equals $nk$. It also equals $mk$. Hence $nk=mk$ and $(n-m)k=0$. We conclude that $n=m$ or $k=0$.
As a matter of fact, your proof also makes use of $k\ne 0$ (can you see why?).
Actually, reading your proof does not make this immediately apparent, which indictes that you may add a bit more detail to the point where you conclude that onenode must have degree $\ge k+1$.
But that is not simply a problem of the proof, in fact it is alreeady a problem of the claim: A $0$-regular grpah with an odd number of vertices is inde3ed bipartite! - For the corrected claim, however, the proof is quite fine .


Projective modules and nondegenerate pairings

Question: Let $M$ be a finitely-generated projective module over a commutative algebra $A$, defined over $\mathbb{R}$. Consider a nondegenerate bilinear pairing
$$
(*,*):M \times M \to A,
$$
where by nondegenerate we mean that, for all non-zero $m \in M$, there exists some $n \in M$, such that
$$
(m,n) \neq 0.
$$
Does there exist an isomorphism
$$
\phi:M \to M^*
$$
such that
$$
(m,n) = \phi(m)(n),
$$
for all $m,n \in M$?
A side question, is: is projectivity necessary for us to
establish such an isomorphism, or can it be established in a weaker setting?
Comment: Do you assume that your module is of finite type? Otherwise you have counter-examples with vector spaces of infinite dimension.
Comment: @Captain Lama: Yes, we are assuming that $M$ is finitely generated.
Answer: No; for instance, if $A=M=\mathbb{R}[x]$ and $(m,n)=xmn$ then $\phi: M\to M^*\cong M$ is the map $\phi(m)=xm$ which is not an isomorphism.
More generally, $\phi(m)(n)=(m,n)$ defines a homomorphism $M\to M^*$ for any bilinear form on a module $M$, and nondegeneracy just says that $\phi$ is injective.  In general there is no reason to think $\phi$ is surjective, though.
Comment: Any ring is an algebra, for instance over $\mathbb{Z}$.
Comment: The assumption is that $A$ is an algebra, not a general ring.
Comment: Can this problem, i.e. non-surjectivity, happen for algebras?
Comment: I am interested in algebras over fields.


Is it possible to play 2 wind instruments well?

Question: I am currently playing the tenor trombone and flute. I have been playing the trombone for quite a while and I have recently taken up the flute. I am finding it difficult to sustain a strong tone on both instruments simultaneously. I find it impossible to get a good tone on the flute immediately after having played the trombone.
So my question is is it possible for me to have two different embouchures that I can easily change between even if it means that a break of a few hours would be needed between playing each instrument.
Is there a particular way that I should go about practicing the trombone and flute to achieve this?
Many thanks in advance,
Conor
Comment: It depends rather heavily on your definition of "well."  There are plenty of jazz musicians who play multiple instruments.  Symphony orchestra musicians double: clarinet and bass clarinet;  flute/piccolo/altoflute; bassoon/contrabassoon,  and so on.  What do you want other than an exhortation to practice both instruments more? :-)
Comment: I've regularly switched (without trouble) between flute, recorder, and melodica from song-to-song during my church's 20 minute evening song service. Granted, the last two don't really have any sort of embouchure. I don't find flute embouchure all that tiring (it's a one-keyed Baroque flute, but I don't think that makes a difference), though I can certainly *imagine* that brass instruments would do a number on my lips. It might take me a while to cool down after playing one, but I don't see why I'd lose the ability to play flute.
Comment: Caleb, I also don't find the flute embouchure all that tiring provided I take a break from the trombone for a week. The problem is that the brass embouchure is very different to that of the flute so it is hard to do both. (I play an Irish flute with no keys which I think is quite similar to the Baroque flute.) Thanks
Comment: Look up Roland Kirk, Yusef Lateef and Eric Dolpy - you might have your answer. But note that flute is considered a woodwind - different category than trombone, which is brass - a horn - flutes were originally made from wood and the sound is not created in the manner as brass/horns.
Answer: Absolutely. JB Arban once said that people have the wrong idea about embouchure - that it's a fixed thing like a statue.  He said that embouchure is fluid - you need to do what's right and what sounds good.
I am not surprised that you have difficulties with flute after trombone; remember how each instrument works:

The flute is like a fickle bottle - you need smooth, round, concentrated columns of air moving through a precise aperture between smooth lips (here is the key).
Trombones need lips that buzz in order to make sound.

After playing trombone your lips are swollen (from additional blood, pressure, and constant vibration) and sensitive.  Swollen, sensitive, quivering, wrinkly lips are not a flute's best friend.
Advice: Play flute before you play trombone, and see if that makes a difference in your sound.  I think that in addition to improving your flute playing, you will notice an improvement in your trombone sound from concentrating on using so much air for the flute.  I found that flute playing really complemented my tuba playing.
Answer: Definitely.  There are lots of people who play all of the woodwinds (maybe not bassoon) very well; modern musicals almost exclusively call for this kind of doubling.  In fact, it's kind of unusual to find professional saxophonists or clarinetists that aren't at least competent at the other, though admittedly those instruments are remarkably similar.
And it's not uncommon to find people who play multiple brass instruments, especially the more closely related they are.  Trombone+euphonium is very common and can just about be expected of a pro, and trumpet+horn or trumpet+euphonium (or all 3) is pretty common too.  These doublings are far less commonly asked for in any kind of published music, but the instruments are fundamentally the same so it's really no surprise.
Crossing over between the woodwind and brass families is rarer, but still not unheard of.  The sound production mechanisms start to vary far more so there's less direct skill transfer.  In particular, brass players often struggle with flute because the total lack of resistance causes them to far overblow.  But it's nothing that good ol' practice can't overcome.
My advice for getting used to the switch is very simple: just keep practicing the new instrument, you have to give your body enough time to internalize the muscle memory and learn to make the change faster.  I started on trumpet and when I first started learning horn, one would totally ruin the embouchure for the other, but now I can switch between them as quickly as I can get them to my face.  I've never tried quick switching between brass and woodwinds but I don't imagine having too much of a problem.
Answer: Look up James Morrison.  The guy plays an incredible number of instruments, and all extremely well. He has a big band CD out where he plays EVERY instrument in the band except percussion. Piano, Upright bass, guitar, alto sax, tenor sax, bari, the entire trombone section and trumpets. I've even seen him play trumpet and trombone, trading back and forth, with one in each hand, playing each one-handed.
With reference to your comment about your lips being swollen, a little swelling is natural when playing, but should not persist more than a short while afterward if you are doing it correctly and not using too much pressure.


Get a FILE* from a bluetooth COM port on Windows

Question: A C library I use requires a FILE* opened to a COM port to work with it, but I have to set up the port (connection speed, etc) before passing it to a library. On Windows, this is done using SetCommState(HANDLE, DCB*).
Transformation between HANDLE and FILE* is a solved problem (How make FILE* from HANDLE in WinApi? and How do I get the file HANDLE from the fopen FILE structure?), but, surprisingly, it fails in different ways when applied to COM ports obtained via Bluetooth (RFCOMM/SPP).
If I first open a HANDLE and set COM port parameters, _open_osfhandle always fails for me:
HANDLE qc9200_handle = CreateFile(T("\\\\.\\COM9"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
/* this returns a valid value like 0x30 */
SetCommState(qc9200_handle, &qc9200_dcb); /* also succeeds */
int qc9200_fd = _open_osfhandle((intptr_t)qc9200_handle, _O_RDWR|_O_BINARY);
/* returns -1 and sets errno=EINVAL (22) */

I tried setting various combinations of flags from _O_APPEND (the only one which is both mentioned in the docs and makes sense with my application) to _O_RDWR|_O_BINARY (which I initially thought should have worked), but the errno is always EINVAL.
If I try to start with a FILE* or int file_descriptor to produce a HANDLE from it later, both _topen(path, _O_RDWR|_O_BINARY) and _tfopen(path, "r+b") fail right away and set errno=EACCES (13) no matter what access mode I request, even when I launch my application with Administrator rights. They don't fail immediately, though, it takes about a second, and I can see the light flashing on my USB dongle before the failure. Trying to open(COMPORT, "+<", "\\\\.\\COM9") the port from Perl also fails with "access denied".
Fishing for events with ProcessMonitor, surprisingly, only gives a bunch of RegQueryValue to HKLM\System\CurrentControlSet\Enum\BTHENUM_LOCALMFG\Device parameters\{Port Name, Authenticated, Encrypted} and one IRP_MJ_READ to %WINDIR%\system32\ucrtbased.dll while fopen is on the stack.
The COM port in question is obtained using ordinary Windows 7 Bluetooth settings, and every terminal program capable of working with COM ports does access it, too, even if not run as administrator. When I do same things to a virtual COM port (com0com), everything works as expected; the errors persist only when I'm using a Bluetooth COM port.
UPD: As GetFileType(HANDLE) reveals, the handle to a Bluetooth COM port is not a file-HANDLE, nor it is anything GetFileType knows about. That's why C runtime library refuses to return a file descriptor to the handle and that's probably why fopen refuses to open the port. I'll have to implement an fprintf-like function which accepts HANDLEs instead and #ifdef and use it on Windows.
Comment: Try messing with the flags of `_open_osfhandle`. Perhaps removing `_O_RDWR` and/or adding `_O_APPEND` will make a difference. There is some code in [legacy_ntp](https://github.com/dan-boa/legacy_ntp/blob/master/ports/winnt/ntpd/win32_io.c) which uses `_open_osfhandle` on serial ports, although it uses `_O_TEXT` rather than `_O_BINARY`. Once you have a valid file descriptor, you can use `_fdopen` to open a `FILE*` (but you knew that already).
Comment: @Ian, No matter what flags of `_open_osfhandle` I try, it's always -1 and EINVAL. It's like `_open_osfhandle` doesn't like my HANDLE. But why should `fopen` on COM port fail when `CreateFile` succeeds - that's a true mystery. And why do `open`/`fopen` always return EACCES when I clearly have the access to the port using whatever terminal emulator?
Answer: Thanks to a Russian language forum, I was able to solve this.
After opening a HANDLE to the virtual COM port, I called GetFileType() on it,
printf("handle=%d\nGetFileType=%lu\n", handle, GetFileType(handle));

Which resulted in
handle=36
GetFileType=0

with GetLastError returning 0.
GetFileType(HANDLE) == 0x0000 means FILE_TYPE_UNKNOWN.
It seems that CRT doesn't know how to properly produce FILE* from unknown-type HANDLES, so my only way to run a vfprintf-like function to write to the serial port would be to implement such a function for HANDLEs myself.
Given that it's a bad idea to use FILE* with serial ports because the standard library would issue seek() calls between reads and writes, and serial ports are unseekable, I had to drop FILE* completely and use plain int file descriptors on POSIX and HANDLEs on Windows.


How can I implement a custom display widget for an existing field?

Question: Basically I want to have my own widget as a choice in the widget drop down of an existing field type; and/or I want to be able to call field_view_field on a field and have it display the way I need, using info from the field itself and elsewhere.
I can probabaly do what I need with template_preprocess_field but I would prefer to hook the existing admin interface for selecting the output of the form.
I'm assuming that a solution is available that would allow the output of the field to be the way I configure it any place the field can be displayed, Views, etc.
Thanks in advance.
Answer: The Examples project on drupal.org has a field example, showing how to implement a widget.
Basically you need to implement hook_field_widget_info(), and for 'field types' include the field type your widget is for.


Open graph stories Facebook

Question: I am new in Facebook App. and I just discovered Open Graph stories. I'm wondering - where should I insert action and object code? What are the steps in approval procedure?
Answer: I'm still new to this topic but found out some stuff (not completely yet!).
Via developer.facebook.com you have your app. You can add objects / actions to the left of the screen. The approval depends on the rights your app requires. Are you using the open graph for web or mobile app? Eventually you'll need to create a screencast (video) of how Facebook works in your app to get approved.

Open graph stories Facebook


How to build a list of filterable options from a query result?

Question: I have a collection of products, each product will have:

price
attributes (for example: Size: xl, s, xxl; Color: yellow, red, blue; ....)
manufacturer
categories (a product can be in multiple categories)

This collection is indexed in ElasticSearch. Now when I query the list of all products in category A for example, I would also like to get:

the price range of all products (min - max)
the list of all possible options and values (Size, Color and the corresponding values xl, s, xxl, yellow, red, blue)
manufacturer (Nike, Addidas, ....)
categories

I will then use these info to build a filter which users can use to narrow down the result list (similar to filters seen on many other ecommerce sites such as Amazon, Overstock, Ebay,...)
I looked into facet search but not sure if that's what I want, I would love to find a way to retrieve all the information I need with the smallest amount of queries possible.
Answer: Facets are definitely the way to go when it comes to the style of Amazon category search. For retrieving all the facets in the smallest amount of queries as possible you can very easily do this, just include more. I used Sense chrome extension because it's really pretty and easy to use for search examples. For this example I'm placing a few product documents with name, price, manufacturer, category, color. As you can see we've searched on all these facets in a match all. You can do the query you like and even do global facets, or any number of facets( range, histograms ect..). It really all depends on what documents you want, and how the data is formed. A very exciting portion of this is the next big thing Aggregations! You can do awesome searches with aggregations. Say you have a timestamp and a location, you could have a aggregation on a color between a time in a specific location. qbox.io will have a blog post about some interesting things you can do with aggs in the coming days.
<code>DELETE /test_index

PUT /test_index

PUT /test_index/product/1
{
   "name":"prod1",
   "price":19.95,
   "manufacturer":"manu1",
   "category":["cat1","cat2"],
   "color":"red"
}

PUT /test_index/product/2
{
   "name":"prod2",
   "price":25,
   "manufacturer":"manu2",
   "category":["cat2","cat3"],
   "color":"yellow"
}

PUT /test_index/product/3
{
   "name":"prod3",
   "price":4.99,
   "manufacturer":"manu2",
   "category":["cat1","cat2","cat3"],
   "color":"yellow"
}

PUT /test_index/product/4
{
   "name":"prod4",
   "price":19.95,
   "manufacturer":"manu3",
   "category":["cat1","cat3"],
   "color":"blue"
}

POST /test_index/_search
{
   "query": {
       "match_all": {}
   },
   "facets" : {
       "price_stats" : {
           "statistical" : {
               "field" : "price"
           }
       },
       "color_terms" : {
           "terms": {
              "field": "color",
              "size": 10
           }
       },
       "manufacturer_terms" : {
           "terms": {
              "field": "manufacturer",
              "size": 10
           }
       },
       "category_terms" : {
           "terms": {
              "field": "category",
              "size": 10
           }
       }
   }
}`
</code>
Answer: If you're wanting to get ahead of the ElasticSearch game, why not check out the latest beta of ElasticSearch, found here which includes aggregations. Here is a nice blog article explaining aggregations and the differences it has with facets, and its endless possibilities.
ElasticSearch 1.0.0 can't be far away so don't worry too much about being part of a beta program.


JNA Equivalent for C Code

Question: I have a C code segment like this
struct test{
char *ptr;
}

What is the equivalent JNA Syntax?
Comment: You want to access test from Java via JNA?
Answer: It should be as straight forward as;
public class test extends Structure {
  String ptr;
}

More about char* mapping here and
struct mapping here.

JNA Equivalent for C Code


Piano technique: Repetitions and oscillations

Question: Not sure if I am using the right expression, so allow me to cite examples.
There are some piano movements that cause my hands (wrists) to cramp when I play it for too long. One are repeated chords. Think of the accompaniment of Schubert's Erlkönig. Or the beginning of the Waldstein Sonata.
The other are oscillations between notes, as in the first movement of Beethoven's Pathetique sonata.
What is the right technique to play these and how do I train this technique?
Comment: This is not a piano technique-oriented answer, but as an [answer to another question](http://music.stackexchange.com/questions/439/are-tools-to-help-improve-finger-dexterity-strength-and-speed-really-useful/449#449), I advised using a [Powerball](http://www.powerballs.com/) every now and then to strengthen wrists.
Answer: When it comes to improving the stamina of your forearms, the only solution is to practice.  It's the same deal as with running, or swimming; if you don't train to keep the muscles fit, they will get tired.  To train yourself, play until your wrists get fatigued (I don't mean a little bit tired, I mean around the point where you feel the need to shake them out).  Once they're fatigued, it's time to stop; if you keep going, you're opening yourself up playing sloppily and getting injured.  If you take it slow, your stamina will increase gradually.  If you hurt yourself, you'll have to stop to heal, which will set you back a ways.
As far as technique is concerned, when you're dealing with repetitive chords, do your best to separate your hands' up and down motion from their side-to-side motion.  The up and down should be taken care of by your wrists alone, and the side-to-side should be taken care of by the rest of your arm.  This does tire your wrist out more quickly, but it also makes it easier to play more precisely.  In Erlkonig especially, make sure that while your hand must stay in the same general shape to play an octave, do NOT tense it.  If you find yourself tensing up, it's time to slow down and practice playing without tensing.  Again, tensing the hands leads to tired hands, which leads to sloppy playing and injury.
For the "oscillations" (I don't know the proper term, either :P), make sure that you're using more of a wrist rotation than an up and down motion.  Again, make sure that you're not tensing your hand up.  And again, your stamina will increase gradually, but it will increase.
Good luck!