Custom validate exposed filter on a view

Question: I have a view with a single exposed taxonomy field that requires the user to click apply before any results are shown.
The options in the list appear like this

Please Select (All label overridden with form alter, also the default option)
option 1
option 2

if the user clicks apply when please select field is highlighted I would like to show an error message rather than all of the results I have tried the following.
Form:
$form['#validate'][] = 'MY_MODULE_claims_validate';

Function:
function MY_MODULE_forms_claims_validate(&$form, &$form_state) {
  if($form_state['values']['user_category'] == 'All') {
    // $form_state['values']['user_category'] = 0;
    form_set_error('user_category', 'please enter a value');
  }
}

It does set a form error message but also throws the follwoing warning, it also does this on the first page load which I don't want it to do either.
Notice: Undefined index: nid in views_handler_field_term_node_tid->pre_render()
It throws this error the first time the page is loaded which is a
Comment: Do you want to perform the validation or get rid off the warnings? Currently your question seems un clear.
Comment: The code above works I just need to work out why it's throwing that warning and also prevent the validation firing on the first page load.
Comment: I've tried using the same technique with several views and I always get the same error message. Notice: Undefined index: nid in views_handler_field_term_node_tid->pre_render()
Answer: Another approach is to use hook_form_FORM_ID_alter() like other answers suggest, but to put custom validation before the default validation callback of views. By this way you can stop to step over to the second validation callback if validation fails, otherwise validation continues to process.. Example:
function MY_THEME_OR_MODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  if ($form['#id'] == 'views-exposed-form-MY_VIEWS_NAME_MY_VIEWS_DISPLAY') {
    array_unshift($form['#validate'], 'my_custom_validation_callback');
  }
}

function my_custom_validation_callback($form, &$form_state) {
  if(/* CONDITION */) {
    form_set_error('user_category', 'please enter a value');
  }
}

Comment: Tried the above and it does validate the field but I still get the error message, it also tries to validate the form on first page load and throws the error and also my custom validate error.
Comment: @DanielHarper I think you need to show the views configuration and your form alteration and so on.. Which is related to this functionality... Because I don't think that error in your form alter function or validation callback, it is in somewhere else..
Comment: @DanielHarper The problem is in your views configuration.. I think you've added somekind of node field and when you execute the views it tries to load that field but you are not allowing it to be chosen, that's why you are getting this error.. Take a look at this page: http://www.whatwherewhenroslindale.com/history-view-google?field_historical_date_value[min][year]=&field_historical_date_value[max][year]=1913&=Search&op=Reset When you open it you get an error. And validation shows that End date must be chosen.. I think you need to provide default value to that field somehow..
Comment: You're right I have a taxonomy field which is not rendered but used to group the view for quicktabs, is there a way to manually set these values in the validate function or do I need to use a different views hook?
Comment: @DanielHarper I don't have a working project where I can test it and I don't know your view's configuration... With `hook_form_FORM_ID_alter` can set default values to views. Here is a simple tutorial (I don't this can help you:)): http://views-help.doc.logrus.com/help/views/alter-exposed-filter and here: http://venutip.com/content/setting-default-value-views-exposed-filters
Comment: I have updated the answer with the exact function I used which solved all my issues and gave me the desired behaviour, thanks for all the help.
Comment: @DanielHarper I am very glad if I could help you with this.
Answer: If you are using Drupal 7, I'd leverage the Form API's #states functionality and only allow the (APPLY) to appear when you've selected something in your exposed filter.
For a quick example, I just threw together a View that exposes the content type...

...and then a quick form_alter() along these lines...
function MODULE_OR_THEME_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id=='views_exposed_form') { // obviously you'll probably need to be more specific :)
    // drupal_set_message('
' . print_r($form, TRUE) . '
'); // uncomment to see what you have to work with
    $form['type']['#options']['All']='Please Select';
    $form['type']['#description']=t('You must select a type before continuing.');
    $form['submit']['#states']=array(
      'enabled' =>  array(':input[name="type"]' => array('!value' => 'All')), // enable this when we aren't selecting "All" for our type
      'visible' =>  array(':input[name="type"]' => array('!value' => 'All')), // show it when we aren't selecting "All" as well
      // 'disabled' => array(':input[name="type"]' => array('value'  => 'All')), // you don't really need this one or the next one but include them
      // 'hidden' =>   array(':input[name="type"]' => array('value'  => 'All')), // for completeness or if your site does need them (mine didn't)
    );
  }

}

which, together, produces on the first page load:

and when a type has been selected:

thus allowing your users to look at the View.
Obviously, for your View, you'll have to change the 'type' to your taxonomy field as well as the name= to the correct name, too, but the broad strokes are here.
Comment: Thanks for the answer @jimajamma although this would have worked I wanted to leave the button in tact as I thought it would be less confusing for users.
Answer: You can use hook_form_alter() to attach a custom #validate callback:
function my_module_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message($form_id);
  if ($form_id == 'my_exposed_form') {
    $form['#validate'][] = 'my_custom_validate';
  }
}

function my_custom_validate($form, &$form_state) {
  // Do custom validation here...
  // If there was a problem...
  form_set_error('my_taxonomy_exposed_field', t('Something bad happened!'));
}

Alternatively, you may want to overwrite the #validate handler, rather than appending your validate handler to it, if the default handler is causing problems.
Also, on your exposed filter, check out the different 'operator' settings as a way to help prevent all results from being displayed if nothing is selected.
Comment: I have updated my question to include a solution as above but it doesn't quite work as I needs, also explained above.


Differences in mean curvatures for permuted coordinates of a manifold

Question: Below is an example demonstrating the differences in mean curvatures and one other quantity, and below is my question. Please comment if you need me to explain in more detail or clarify notation, or if there are any mistakes.
Example 1
Consider the manifold with coordinates given by
$$(x,y,x+y, x^2).$$
At each point, there are two unit normal vectors
$$n_1 = \frac{\sqrt{3}}{3} [1, 1, -1, 0]^T$$
and
$$n_2 = \frac{\sqrt{3}}{3\sqrt{8x^2+3}} [4x,-2x,2x,-3].$$
The vector of mean curvatures in the directions of each of these normals is given by
$$c=\left[0, -\frac{2\sqrt{3}}{(8x^2+3)^{3/2}}\right]^T.$$
Further, if we let $q$ be the vector with components $q^r = \operatorname{Tr}{N (Dn_r) N^T}$ where $Dn_r$ is the Jacobian derivative of the vector $n_r$, $r=1,2$ and $N$ is the matrix with $r$-th row equal to $n_r$, then we get that $q=\vec{0}$.
Brownian motion on this manifold has drift $\mu =N^T(q+c)$, i.e.
$$\mu = \frac{1}{(8x^2+3)^2}[-8x, 4x, -4x, 6]^T.$$
Example 2
Now, permute the last two coordinates so that instead we have
$$(x,y,x^2,x+y).$$
Now, we can obtain unit normal vectors
$$n_1 = \frac{1}{\sqrt{4x^2+1}} [2x,0,-1,0]^T,$$
$$n_2 = \frac{1}{\sqrt{(4x^2+1)(8x^2+3)}}\left[1, 4x^2+1, 2x, -(4x^2+1)\right]^T,$$
mean curvature vector $c$,
$$c=\left[-\frac{1}{(4x^2+1)^{3/2}}, -\frac{2x(16x^2+5)}{(4x^2+1)^{3/2}(8x^2+3)^{3/2}}\right]^T$$
and now a non-zero $q$, as defined above,
$$q=\frac{1}{\sqrt{(4x^2+1)(8x^2+3)}}\left[\frac{1}{(4x^2+1)\sqrt{8x^2+3}}, -\frac{2x}{4x^2+1}\right]^T$$
and drift of a Brownian motion
$$\mu = \frac{1}{(8x^2+3)^2}[-8x, 4x, -4x, 6]^T,$$
again from $\mu=N^T(q+c)$.
My questions:

Why are the mean curvatures different for these two permutations of coordinates of, essentially, the same manifold? (From a probabilist perspective, I get the same SDE for Brownian motion on both manifolds--this might be a blasphemous viewpoint to differential geometers, so please excuse my ignorance!)

What is the geometric interpretation of $q^r = \operatorname{Tr}(N (D n_r)N^T)$ and why does it change depending on the permutation of coordinates (or rather on the order of orthonormalizing the rows of $Df$ where $f(x,y,z,w)=0$ defines the manifold, via component definitions e.g. $f^1(x,y,z,w)=x+y-z$ and and $f^2(x,y,z,w)=x^2-w$) for the first presentation.
Comment: Because the surface has codimension $2$, there is a *circle* of unit normal vectors at each point, thus a family of second fundamental forms parametrized by a circle. You are choosing random orthogonal pairs from that circle. Why didn't you choose the second $n_1$ and $n_2$ just by switching the last two coordinates? Up to an orientation issue, the mean curvature vector should be a well-defined vector, if you keep track of an ambient isometry such as you are using.
Comment: The manifold is 4D with coords (x, y, x+y, x2) or (x, y, x2, x+y). There are two unit norm vectors, n1 and n2.
The mean curvature vector c has components given by c=[c1,c2]T where c1 and c2 are expressions involving x and possibly other constants.
There is a vector q defined using the Jacobian derivative of the normal vectors. In Example 1, q=0 while in Example 2 q is non-zero with components given in the example.
The drift μ of Brownian motion on the manifold is given by μ=NT(q+c) where N is a matrix with row r equal to nr. The expressions for μ are given in the examples. Is this correct?
Comment: @AnonImus Yup that is the gist of the setup.
Answer: Here's the way I compute this as a differential geometer. I differentiate the parametrization, not the normal vector, to arrive at the second fundamental form. With the parametrization
$$g(x,y)=(x,y,x+y,x^2),$$
I get $g_x = (1,0,1,2x)$ and $g_y = (0,1,1,0)$ spanning the tangent space, with corresponding orthonormal normal vectors
$$N_1 = \frac1{\sqrt3}(1,1,-1,0) \quad\text{and}\quad N_2 = \frac1{\sqrt{9+24x^2}}(4x,-2x,2x,-3).$$ You and I have chosen the same normal vectors.
The corresponding second fundamental forms are
$$\text{II}_j = \begin{bmatrix} g_{xx}\cdot N_j & g_{xy}\cdot N_j \\ g_{yx}\cdot N_j & g_{yy}\cdot N_j\end{bmatrix},$$
and so the second-fundamental form is the vector-valued $2$-tensor
$$\text{II}_1\otimes N_1 + \text{II}_2\otimes N_2 = \begin{bmatrix} 0 & 0\\0 & 0\end{bmatrix} \otimes N_1 + \frac{-6}{\sqrt{9+24x^2}}\begin{bmatrix} 1 & 0\\0 & 0\end{bmatrix} \otimes N_2.$$
The mean-curvature vector will be
$$H=\text{tr}(\text{II}_1)N_1 + \text{tr}(\text{II}_2)N_2 = \frac{-6}{\sqrt{9+24x^2}}N_2 = \frac{-2}{3+8x^2}(4x,-2x,2x,-3).$$
This seems to agree with your result.
If I change the surface, as you did, by switching the final two coordinates, those two coordinates switch in everything I've computed and nothing else changes; I end up with $H'=\dfrac{-2}{3+8x^2}(4x,-2x,-3,2x)$. I continue not to understand what you've done. Why did you choose such arcane normal vectors now? Why didn't you just choose the corresponding normal vectors in the first case and compare the mean-curvature vectors with the two different orthonormal bases?
Comment: Insightful! The arcane choice of normal vectors was due to (perhaps too blindly!) following a recipe for Brownian motion simulations on a manifold. (Essentially, you have a Stratonoivch SDE $dX_t = P(X_t)\circ dW_t$ where $P(x)$ is the orthogonal projection onto $T_xM$. For codimension $1$, we nicely get the Ito drift as as $c(x)n(x)$ where $c$ is the mean curvature in the direction of the normal vector $n$. For higher codimension, I obtained $N^T(c+q)$ as the Ito drift, where $c$ is a mean-curvature vector now, showed that $q=0$ for hypersurfaces, and found choices where $q\neq 0$ and $q=0$.)
Answer: Question 1: The mean curvature measures the average deviation of the surface from its tangent planes, and it depends on the normal vectors and their derivatives. In Example 1, the normal vectors have a specific form that is different from Example 2, and this difference in normal vectors leads to different mean curvature vectors. Essentially, the way the surface deviates from its tangent planes is different for the two examples due to the different normal vectors.
Provided your probabilistic perspective, the SDE for Brownian motion only depends on the Riemannian metric of the manifold, which is the same for both examples. However, the drift term of the SDE, which depends on the mean curvature, is different for the two examples due to the difference in normal vectors and mean curvature vectors.
Comment: "However, the drift term of the SDE, which depends on the mean curvature, is different for the two examples due to the difference in normal vectors and mean curvature vectors." Actually my examples and computations in the OP show that the drift, $\mu$, is *the same for both choices* despite the normal vectors, mean curvatures and $q$ being different. Perhaps you skimmed the OP too quickly?


How to handle an exception thrown by superclass constructor in java?

Question: I am getting an error is "Unhandled exception type Exception".So i have an question that how we can handle this thrown exception in our subclass.
class A
{

  A() throws Exception
  {
       System.out.println ("Executing class A constructor");
       throw new IOException();
  }
}

public class B extends A
{
  B() //error
  {
       System.out.println ("Executing class B constructor");

  }

  public static void main ( String args[] )
  {
          try
          {
          A a = new B();
          }
          catch ( Exception e)
          {
                      System.out.println( e.getMessage() );
          }
  }
}

Answer: There is no way for the constructor of a subclass to handle an exception thrown by its superclass constructor.
The only thing you can do is to declare the subclass constructor as throwing the same (checked) exceptions as the super class constructor declares; e.g.
    B() throws Exception {
         System.out.println ("Executing class B constructor");
    }

That means that you cannot get the "Executing class B constructor" message to be printed if A() throws an exception.
If you think about it, this is a good thing.  If you were able to catch the exception from super(...) in the subclass constructor, you would have to deal with an object that may not have been fully initialized.  That is likely to be problematic.
Alternatively, if you do want to deal with the exceptions coming out of the superclass constructor, then hide the subclass constructor (i.e. make it private) and instead use a factory method to create the subclass instances.  That method can handle exceptions that emanate from the super or subclass constructor.  But note that unless the constructor does something vile (e.g. smuggling this out via the exception), you still won't see the reference to the partially initialized / broken object.

I guess this is just an example, but it is worth pointing out that it is a bad idea to declare a method as throwing Exception (or Throwable).  You should declare the method as throwing the actual exceptions that are thrown.
Answer: You need to also throw exception in subclass.
 B() throws Exception
 {
   System.out.println ("Executing class B constructor");
 }

Answer: Add throws clause to B.
B() throws Exception.
Constructor B will expect that the exception to be handled as it is a child of A.
Since it is a constructor you can not put it inside try catch. So the option is to throw it.


Stay on selected language when page refresh or move to another page

Question: I found a reference for multi language for my website without using php. I had 2 languages: english & bahasa. Default is english when I choose bahasa and i refresh the page, it's back to english again as I want it to stay on the selected language
I couldn't figure out anyway as I'm still learning about jQuery and others.
<code>changeLanguage("en");

$("#id_button").click(function(){
    changeLanguage("id");
});

$("#eng_button").click(function(){
    changeLanguage("en");
});
</code>
I expect that when I select bahasa dan when the page refresh or move to other pages, it will remain on my selected language.
Answer: You can use a cookie to store the language using Jquery
<code>  $.cookie('lang','en');
</code>
To retrieve the data
<code>  var current_language = $.cookie('lang');
</code>
For more reference please visit the link
https://github.com/jaaulde/cookies#With_jQuery
and also someone answered this
how to save data in a cookie using jquery


Probability of getting certain number of points

Question:
A one on one basketball tournament is happening in your community.  Each game has a point system.  3 points for a win, 1 point for a tie, and 0 points for a loss.  You have a 50% chance on winning, 40% chance of losing and a 10% of tying.  We will say all these events are independent.
 What's the probability by the end of 6 games you have at least 12 points.

 This is my work for this question.
Let X be the number of points after 6 games, then
 $ P(X \ge 12)=1-(3(\frac{1}{2})^6+1(\frac{1}{10})^6+0(\frac{4}{10})^6)=\frac{238281}{250000}$
 I am not confident in it though.  Can somebody explain how I am approaching this right or wrong..
Comment: Can you explain your reason to obtain that probability?
Comment: I am not sure if correct but basically at least usually you subtract from one and since 6 games are played .5 of the time your going to win so *$\frac{1}{2}^6$ and this argument holds for the other probabilites.  I multiplied each probability by total points being earned
Comment: I think you are confused between expectation and probability.
Comment: Should I not be including the point values in my calculation then?
Answer: The correct probability is $277/800$.  One way to calculate it is as follows.  First, list the possible $(W,T,L)$ triples with $W + T + L = 6$, $3W + 1T + 0L \ge 12$, and $W, T, L \ge 0$:
$$\{(3,3,0),(4,0,2),(4,1,1),(4,2,0),(5,0,1),(5,1,0),(6,0,0)\}$$
Now the desired probability is
$$\sum_{(W,T,L)} \binom{6}{W,T,L} 0.5^W 0.1^T 0.4^L = \frac{277}{800}.$$
Here, $$\binom{6}{W,T,L}=\frac{6!}{W!T!L!}$$ is a multinomial coefficient, which can also be written as $$\binom{6}{W}\binom{6-W}{T}\binom{6-W-T}{L}=\binom{6}{W}\binom{6-W}{T}\binom{L}{L}=\binom{6}{W}\binom{6-W}{T}.$$

An alternative approach uses recursion.  Let $w(p,g)$ denote the probability of at least $p$ points in the next $g$ games.  We want to compute $w(12,6)$.  By conditioning on the outcome of the next game, we obtain:
$$
w(p,n) =
\begin{cases}
1 &\text{if $p\le 0$ and $g=0$}\\
0 &\text{if $p>0$ and $g=0$}\\
0.5 w(p-3,g-1) + 0.1 w(p-1,g-1) + 0.4 w(p,g-1) &\text{otherwise}
\end{cases}
$$
The resulting values of $w$ are:
$$\begin{matrix}
p\backslash g &0 &1 &2 &3 &4 &5 &6 \\
\hline
0 &1.00000 &1.00000 &1.00000 &1.00000 &1.00000 &1.00000 &1.00000 \\
1 &0.00000 &0.60000 &0.84000 &0.93600 &0.97440 &0.98976 &0.99590 \\
2 &0.00000 &0.50000 &0.76000 &0.88800 &0.94880 &0.97696 &0.98976 \\
3 &0.00000 &0.50000 &0.75000 &0.87600 &0.93920 &0.97056 &0.98592 \\
4 &0.00000 &0.00000 &0.35000 &0.63500 &0.80960 &0.90496 &0.95392 \\
5 &0.00000 &0.00000 &0.25000 &0.51500 &0.71350 &0.84076 &0.91528 \\
6 &0.00000 &0.00000 &0.25000 &0.50000 &0.68950 &0.81675 &0.89606 \\
7 &0.00000 &0.00000 &0.00000 &0.20000 &0.44750 &0.65275 &0.79526 \\
8 &0.00000 &0.00000 &0.00000 &0.12500 &0.32750 &0.53250 &0.69866 \\
9 &0.00000 &0.00000 &0.00000 &0.12500 &0.31250 &0.50250 &0.66263 \\
10 &0.00000 &0.00000 &0.00000 &0.00000 &0.11250 &0.30000 &0.49663 \\
11 &0.00000 &0.00000 &0.00000 &0.00000 &0.06250 &0.20000 &0.37625 \\
12 &0.00000 &0.00000 &0.00000 &0.00000 &0.06250 &0.18750 &0.34625 \\
\end{matrix}
$$
In particular, $w(12,6)=277/800$.
Comment: I updated my answer just now with more detail.
Comment: I get what your saying until the "Now the desired..." are we plugging in the values above for W, T, L? Cause I tried that for the first one and got nothing close to what you got
Comment: I like your solutions, but I think you have the probabilities of losing and tying switched in your calculations.  It shoud be $P(loss) = 0.4$ and $P(tie) = 0.1$.
Comment: Yes, thanks for pointing it out.  I was fooled by the inconsistent ordering in the problem statement.  I corrected my answer just now, and it matches yours.
Answer: Here is an approach using a probability generating function.  Readers unfamiliar with generating functions may find many applicable resources in the answers to this question: How can I learn about generating functions?
A probability generating function for the total number of points in six rolls is
$$f(x) = (0.4 + 0.1 x + 0.5 x^3)^6$$
On expansion of the polynomial,
$$f(x) = 0.004096\, +0.006144 x+0.00384 x^2+0.032 x^3+ \\0.03864 x^4+0.019224 x^5+0.100801
   x^6+0.0966 x^7+ \\0.03603 x^8+0.166 x^9+0.120375 x^{10}+0.03 x^{11}+ \\0.1525
   x^{12}+0.075 x^{13}+0.009375 x^{14}+0.075 x^{15}+ \\0.01875 x^{16}+0.015625
   x^{18}$$
(This is easy if you use a computer algebra system or Wolfram Alpha, otherwise a bit tedious.)
The coefficient of $x^n$ in this polynomial is the probability of winning $n$ points.  So if we want the probability of winning $12$ or more points, we can just sum up the coefficients of $x^{12}$ through $x^{18}$:
$$0.1525+0.075+0.009375+0.075 +0.01875+0.015625 = \boxed{0.34625}$$


Skip non-alphabetical characters with BreakIterator

Question: My goal from this is to convert "<,Bold,>" to "< Bold>" (Without the space between '<' & 'B') using BreakIterator
Say String " This is a test." is my input
public static List<String> getWords(String text) {
    List<String> words = new ArrayList<String>();
    BreakIterator breakIterator = BreakIterator.getWordInstance();
    breakIterator.setText(text);
    int lastIndex = breakIterator.first();
    while (BreakIterator.DONE != lastIndex) {
        int firstIndex = lastIndex;
        lastIndex = breakIterator.next();
        if (lastIndex != BreakIterator.DONE) {
            String t = text.substring(firstIndex, lastIndex);
            words.add(t);
        }
    }
    return words;
}

getWords(String) returns <,Bold,>, ,This, ,is, ,a, ,test.
I've tried :
            String t = text.substring(firstIndex, lastIndex);
            if (t != "<" || t != ">" || t != "/" || t != ">") System.out.println("Char Not Skipped " + t); else System.out.println("Char Skipped" + t);
            //if (text.charAt (firstIndex - 1) == '<') t = "<" + t;
            //if (text.charAt (lastIndex + 1) == '>') t += ">";
            //if (text.charAt (lastIndex + 1) == '/' && text.charAt (lastIndex + 2) == '>') t += "/>";
            //System.out.println(t);
            words.add(t);

All that returns is Char Not Skipped.
Comment: I never used that. But `replaceAll(regex)` is a better option in this case.
Answer: I'm not sure if I got your question the right way.
If you want to delet all , in your string you could easily do:
    String s = "<,Bold,>, ,This, ,is, ,a, ,test";
    String newString = s.replace(",", "");
    System.out.println(newString);

The output would look like that:

 This is a test

If you just want to delet <, and ,> you could use:
   String s = "<,Bold,>, ,This, ,is, ,a, ,test";
   String newString =  (s.replace("<,", "<")).replace(",>", ">");
   System.out.println(newString);

Output of that would be

<Bold>, ,This, ,is, ,a, ,test
Comment: Using this I am getting  : ` This is a test`


asp.net, Silverlight, Visifire

Question: I am developing one chart application on .net framework by  using Silverlight and Visifire, Its a silverlight application and I am running it on localhost.
I have connected through SQL server database to my application and it is running fine, The main thing that i am looking for is that, when i change data inside the database, It should dynamically change my application's chart values.
What I tried is, I set the page refreshing parameter in my web page and its updating the values also, but whole application is re-appearing.
So, Is there any way to change the chart value parallely as I change my values in database.
Looking forward for response,
Thanks.
Comment: You can create a timer in your silverlight control that polls the database every x min's but this will make alot of calls to the db.
Answer: You can use "DispatcherTimer" in Silverlight application and call UpdateChart() on each tick event of the timer. Checkout the sample code below.
<code>public Page()
{
    System.Windows.Threading.DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 10); // 10 sec
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    UpdateChart();
}
</code>
Hope this helps!
Comment: thanks for response but the main thing is that what i'll write inside that update method.
Comment: You can write dataSeries.DataSource = myCollection;

asp.net, Silverlight, Visifire


How can I fix this CrashlyticsMissingDependencyException?

Question: I have been using the latest Crashlytics (Fabric integration) for a while. But recently I encountered the following crash error due to missing dependency although I didn't change anything about Crashlytics configurations.
any idea?
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     |  |
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     |  |
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     |  |
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .   \ |  | /
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .    \    /
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     \  /
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .      \/
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up,
    install an Android build tool and ask a team member to invite you to this app's organization.
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .      /\
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     /  \
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .    /    \
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .   / |  | \
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     |  |
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     |  |
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .     |  |
02-08 22:18:00.935  18887-18887/? E/Fabric﹕ .
02-08 22:18:00.935  18887-18887/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.staging, PID: 18887
    java.lang.RuntimeException: Unable to create application com.example.android.App: io.fabric.sdk.android.services.concurrency.UnmetDependencyException: com.crashlytics.android.CrashlyticsMissingDependencyException:
    This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up,
    install an Android build tool and ask a team member to invite you to this app's organization.
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4734)
            at android.app.ActivityThread.access$1600(ActivityThread.java:171)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5506)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

Comment: As mentioned in the answers below, we're working on a fix, but using 1.14.4 will work while we continue to get to the bottom of this.
Comment: I just try to uninstall and install the Fabrics for Android Studio couples time. AS need to restart couple time to get his sanity back.
Comment: use this link https://docs.fabric.io/android/crashlytics/build-tools.html#disabling-crashlytics-for-debug-builds
Answer: I have added the following codes before I actually installed Fabric/Crashlytics:
debug {
    ext.enableCrashlytics = false
}

Removing it before the first run with Crashlytics solved the problem. The problem no longer occurs after the first run.
Comment: @ThuyTrinh turned out it's because of dataBinding = true. Crashlytics has rolled out a fix for it: https://twittercommunity.com/t/fabric-gradle-plugin-1-21-0-add-support-for-android-databinding-true/57474
Comment: I'm getting the same issue with 1.20.1 and 2.5.2@aar. But I can't use this workaround on CI as the build workspace is reset for every build :(
Comment: I'm working with the engineers at Fabric. Different timezones suck and it took us almost 2 weeks already. Still no solution yet. But you can try a workaround on the CI: ./gradlew clean assemble , then ./gradlew assemble, the apk generated by the second command will work as normal.
Comment: Updated fabric plugin today and got this error. Workaround helps, but it not works on subsequent runs. So I have to comment this line for run.
Comment: Its not working for me in `com.crashlytics.sdk.android:crashlytics:2.6.8@aar`
Comment: @HieuRocker that's probably because the project was built on terminal and fabric.properties wasn't created?
Comment: @ThuyTrinh nope, fabric.properties is there
Comment: @HieuRocker did u manage to fix that? it seems the Fabric Gradle plugin didn't recognize the fabric.properties. building just worked fine on the IDE having the Fabric plugin installed. this really sucks and is haunting us on both terminal and CI server!
Answer: I had this commented out in gradle
apply plugin: 'io.fabric'

needed to uncomment it
Or if you don't have it, add it!
Answer: The only workaround for now, if you really need to publish your app, is to change the dynamic version numbers to static ones:
[...]

classpath 'io.fabric.tools:gradle:1.14.4'

[...]

compile('com.crashlytics.sdk.android:crashlytics:2.2.0@aar') {
        transitive = true
}

[...]

EDIT:
an updated version of the Fabric SDK has been published; you can get it by changing the line to this:
classpath 'io.fabric.tools:gradle:1.26.1'

Comment: Here are the versions available: https://s3.amazonaws.com/fabric-artifacts/public/io/fabric/tools/gradle/maven-metadata.xml
Answer: Seems like specifying the plugin version as:
classpath 'io.fabric.tools:gradle:1.+'

picks up 1.15.1, which has the problem.
Specifying major and minor to previous 1.14 seems to be stable:
classpath 'io.fabric.tools:gradle:1.14.+'

Answer: I had same problem after update plugin.
to solve need remove from AndroidManifest.xml:
<meta-data
    android:name="com.crashlytics.ApiKey"
    android:value="API_SECRET_KEY" />

and add to fabric.properties:
apiSecret=API_SECRET_KEY
apiKey=YOUR_SECRET_KEY

UPDATE:
Now, you have to use:
  <meta-data
      android:name="io.fabric.ApiKey"
      android:value="API_KEY" />

Comment: Their integration guidelines seemed outdated. Seemingly no longer need fabric.properties but a `````` in the AM file. The name was changed from "com.crashlytics.ApiKey" to "io.fabric.ApiKey".
Comment: Yes, you'r right. It's important. I updated my post. Thank you.
Answer: If you are using the disable function during debug as shown
 Crashlytics crashlyticsKit = new Crashlytics.Builder()
            .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
            .build();
    Fabric.with(this, crashlyticsKit);

What happens when you update the version of crashlytics is that
Fabric.with(this, crashlyticsKit);

is turned to
Fabric.with(this,new Crashlytics());

So make sure to change that back to crashlyticskit. If you are doing this correctly and the error still appears then make sure you have
debug {

      ext.enableCrashlytics = false
 }

under android {buildtypes{}}
Answer: Mike from Crashlytics here. We shipped an updated version - 1.15.2 - earlier today that includes a fix for this behavior. If you run:
./gradlew assemble --refresh-dependencies

that will pull in the latest version. You can also see more details on the fix here.
Comment: @MikeB After some time, again I'm getting CrashlyticsMissingDependencyException. I'm using:
dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.16.0'
    }
and
compile 'com.crashlytics.android:crashlytics:1.1.13'
Comment: I'm not using Fabric integration by only Crashlytics itself and I'm also getting CrashlyticsMissingDependencyException. How could I deal with it?
Comment: @bogumil can you include part of your build.gradle?
Comment: Hmm, very odd @bogumil. Does running refresh-dependencies help?
Comment: @MikeB It doesn't help. I sent also an email to support@ and I can deliver more details if necessary.
Comment: @MikeB, thanks for the great work you and the team do on Fabric! I just posted an answer below that you might be interested in reviewing. Maybe I had an unusual case, but then again, maybe it will help your team or someone else.
Comment: Thanks for sharing your additional answer @DavidM that should have happened during the upgrade flow, but glad you're all set. For reference, for others - see the steps here - https://dev.twitter.com/fabric/android/integrating
Comment: Thanks Ankit, please send me a note at suppor(at)fabric(dot)io, I'll need more information to help out.
Comment: @MikeB sent you from firstname.lastname@example.com
Comment: This bug is back, we are experiencing it in io.fabric.tools:gradle:1.19.2
Comment: An app I installed days ago worked fine, today I bootup the device and start the app and get this CrashlyticsMissingDependencyException. Crashlytics/Fabric is proving to be a PITA Mike.
Comment: Attempts to rebuild project yield  'Crashlytics Developer Tools error'.
Comment: @MikeB which channel do u guys announce latest release of Fabric for Gradle?
Comment: Release notes can always be found here: https://docs.fabric.io/android/changelog.html
Comment: I'm still seeing the issue on io.fabric.tools:gradle:1.20.1
Comment: It seems to be a combination of io.fabric.tools:gradle:1.20.1 and com.android.tools.build:gradle:1.5.0 with dataBinding enabled
Comment: @MikeBonnell I just registered to read the details of the fix, but the page is no longer public.
Comment: What page isn't available?
Answer: Fix for me
From official source
Disable Crashlytics for Debug Builds
If you don't need Crashlytics crash reporting or beta distribution for debug builds, you can safely speed up your debug-builds by disabling the plugin entirely with these two steps:
First, add this to your app's build.gradle:
android {
    buildTypes {
        debug {
          // Disable fabric build ID generation for debug builds
          ext.enableCrashlytics = false
          ...

Next, disable the Crashlytics kit at runtime. Otherwise, the Crashlytics kit will throw the following error:
com.crashlytics.android.core.CrashlyticsMissingDependencyException:

This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up`

You can disable the kit at runtime for debug builds only with the following code:
// Set up Crashlytics, disabled for debug builds
Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build();

// Initialize Fabric with the debug-disabled crashlytics.
Fabric.with(this, crashlyticsKit);

Answer: The issue also occurs if you accidentally include the Crashlytics BuildConfig - very easy to do with Android Studio/IntelliJ auto-imports.
I'd imported
import com.crashlytics.android.core.BuildConfig;
Instead of my own
import <package_name>.BuildConfig;
Answer: For me it was because of dataBinding = true. Upgrading the fabric gradle plugin to 1.21.0 fixed the issue: https://twittercommunity.com/t/fabric-gradle-plugin-1-21-0-add-support-for-android-databinding-true/57474
Comment: Great point - this is a more recent version of that error. Using 1.21.0 or 1.21.1 will help.
Answer: In my case, I was using fabric in a "CommonLib" module that was added as a dependency in all other modules (including app).
So, I had added apply plugin: 'io.fabric' after buildscript {} block. So, I placed two plugins together:
apply plugin: 'com.android.library'
apply plugin: 'io.fabric'

And problem got solved!
Answer: Make sure to add apply plugin: 'io.fabric' into your application project build.gradle. In my case, I had a common build.gradle with apply plugin: 'io.fabric'. Moving it to application project solved the problem.
Answer: In case this helps someone else, I had a similar issue when upgrading Crashlytics to Fabric. In my case, the plugin left 2 lines from Crashlytics that I needed to manually remove before it would work.
In the gradle file, under buildscript dependencies, I had to manually remove:
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.16.0'

Also, under dependencies, I had to manually remove:
compile 'com.crashlytics.android:crashlytics:1.1.13'

Answer: Check if crashlytics is disabled in build.gradle file
    debug {
        ext.enableCrashlytics = false
    }

Instead use
    debug {
        ext.enableCrashlytics = true
    }

Answer: May be I am late to reply.
But this can happen from one more reason apart from all the above answers
If you miss to add
apply plugin:'io.fabric'

This may seem strange but this will result in the same issue
This is by default added by fabric when we sign up and add code from the Fabric window Using IDE but accidentally it can be deleted.
Answer: Removing the application icon fixed Crashylitics, whaaat?
I had everything done based on the onboarding tutorial, using the latest versions, etc.
Just wasted an hour trying to figure this out. It turns out someone thought it was a good idea to read the package name of the application icon instead of context.packageName to get the application's resources given a context. It's done in this method:
io.fabric.sdk.android.services.common.CommonUtils#getResourcePackageName
This obviously blows up if you happen to use an icon that's not inside the APK, for example: android:icon="@android:drawable/sym_def_app_icon". It probably was a good idea at the time, wonder what weird bug they were trying to work around?!
Answer: <meta-data
    android:name="firebase_crashlytics_collection_enabled"
    android:value="false" />

Add that to in AndroidManifest.xml.
Comment: So your solution to fixing the problem is to disable Crashlytics completely?


Which octave does Cb4 belong to?

Question: How do you distinguish:

C tone in 4th octave - flat
Cb in 4th octave
?

Or said other way around. Is the order of operations: (C4)b or (Cb)${calculate octave}?
Answer: Scientific pitch notation is one way to determine specific notes. It goes from C>B, as in C3 is the note one semitone higher that B2, C4 one semitone higher than B3.
All notes which are around C4 come into the C4 category. As in C, C♭, C♮, C♯ and Cx (aka ♯♯).
Had the note in question, C4♭ been called a B, it would be B3. That same B, sharpened to B♯ would be called B3♯.The sign should go above, the octave number under it.
So the answer is C4♭. 246.94 Hz taken when A4=440Hz.
Comment: Unlike double flat, double sharp has its own sign rather than a repeated regular sharp sign.  Lower case x is an approximation to this, like using b for flat or # for sharp.
Comment: I had never heard of Cx meaning C double sharp before. That was a bit tricky to confirm.
Comment: @BitChaser I edited; the proper symbols are  ♭ ♮ ♯  (Unicode 1d12b, 266d, 266e, 266f, 1d12a). I'm not sure the double-flat and double-sharp will render correctly on all systems. A better ASCII/markdown-approximation on StackExchange might be to use `**x**` or `×`.
Comment: We can say just that - and we'd be wrong! Please read the answer very carefully.
Comment: @leftaroundabout - thanks, but what looks more like an asterix isn't as effective - in my opinion - as an 'x' - which after all, is exactly what we expect in the dots. I'm reverting! I dare say if it looked like the changed sign, it would still have fooled Bit Chaser!
Comment: Strange that it looks like an asterisk to you, but yeah, as I said I'm not surprised that  renders inconsistently across platforms. Still, I think plain ASCII `x` is most confusing (absent your "aka ♯♯" clarification). Some other alternatives to try: C×, C✕, C❌, C, C. Hm,  think C× (`C×`) is probably the best and should definitely render quite reliably.
Comment: @leftaroundabout - not had any negative feedback about it till now, and others use it and it's clear. Just think Bit Chaser hadn't met 'x' in any of its incarnations before...
Comment: `×` clearly has a totally different semantics. IMHO, we should produce the correct semantics first, before we have a look how it renders.
Comment: Thanks @Tim!
As a conclusion, can we say that C4b = Cb3 = B3?
Comment: I understand. Cb3 would mean Cb in the third octave. I had wrong understanding of the way how you could write notes in chromatic scale. So I thought you can write it as (using flats):
C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B, with the C flat from the next octave being Cb in the current one.
Comment: What's the reason we name our notes from A (the no accidental _minor_ key), but scientific pitch notation loops at _C_?
Comment: @ColeJohnson - there is a special place to ask questions..!
Answer: It's a flattened C4.   Sounding the same as B3, admittedly!
One of the many instances in music notation where 'spelling matters'.

Which octave does Cb4 belong to?


Does gradient descent assume updates of one layer/parameter at a time?

Question: I read the following in "Deep Learning", from Goodfellow et al (Chapter 8, page 313):

The gradient tells how to update each parameter, under the
  assumption that the other layers do not change. In practice, we update
  all of the layers simultaneously.
When we make the update, unexpected results can happen because many
  functions composed together are changed simultaneously, using updates
  that were computed under the assumption that the other functions
  remain constant.

How is that possible? Isn't gradient descent based on the fact that a function decreases fastest if we move in the direction of the negative gradient, and across all weights precisely at the same time?
Comment: @NeilG Not sure I follow, yes the gradient is comprised of partial derivatives, but it still gives the fastest direction of change if we were to move all variables (at the same time) in its direction, doesn't it? And either way, why would the gradient tell us how to update each parameter assuming that the other layers do *not* change? Just because _we can compute it_ by taking partial derivatives independently for each parameter, I don't see how that leads to the assumption that we are supposed to move the parameters one by one.
Comment: "why would the gradient tell us how to update each parameter assuming that the other layers do not change?" Because it *is* a partial derivative, so that's by definition.
Comment: Did you try a thought experiment where you consider what happens in a two-layer neural network as the weights are updated?
Comment: @NeilG Thanks - Yes and in e.g. SGD we don't update the weights until we are done computing the gradient against every parameter (i.e. we indeed update all of the layers simultaneously). What confuses me is the statement _"the gradient tells how to update each parameter under the **assumption** that the other layers do not change"_. Isn't that incorrect?
Comment: No, that is correct provided you're not using second order methods.  They're partial derivatives, right?
Comment: @NeilG No, absolutely not - the gradient is the direction of steepest descent. That is, it is the best way of updating the weights, *simultaneously*. Now yes, it so happens that this steepest descent direction is given by the vector of partial derivatives, but this is just a counterintuitive mathematical surprise, that the best way to update the weights *simultaneously* happens to be obtained by pretending we change them one at a time.  But at the end of the day it's *still* the direction of steepest descent. Without further context the quoted passage in Goodfellow really makes no sense.
Comment: I added an answer to clarify your confusion.
Comment: Suppose you want to minimize x1^2+x2^2+x3^2+..... Gradient gives direction towards the minimum, so take step in that direction. For more general situation, direction towards the minimum is instead given by Newton step.  "Gradient" step is a super cheap approximation to Newton step in a sense that it lies in the same halfplane, so you can still make progress
Answer: Gradient descent using first order methods makes the assumption of a locally symmetric paraboloid loss with known curvature (the learning rate). In other words, we are taking a second order Taylor approximation and fixing the second order terms as given.  Then, the only values that need to be evaluated are the first order coefficients, which is the vector of partial derivatives.
If instead you were using second order methods, then you are no longer assuming the curvature.  Yes, the function decreases fastest if we move in the direction of the negative gradient.  However, it's better to move more quickly in directions where the curvature is small (since the minimum is far in these directions), and more slowly when the curvature is large (to prevent overshooting the minimum in these directions).
Comment: Regarding: ""the fact that a function decreases fastest if we move in the direction of the negative gradient") is no longer true". Actually, the gradient does indeed give the steepest ascent direction in all cases *for sufficiently small step sizes*. The second order direction gives more descent for step sizes implied by the Hessian we are using.
Comment: @JohnMadden Thanks for the correcting me!  Yes, for tiny step sizes, you can just follow the gradient since it is fastest, but you'll never get anywhere with tiny step sizes.  I will clarify my answer.


How to install sqlcmd on windows core

Question: I have inherited a sick WSUS server running on MS Windows core (i.e. no desktop). The CPU is maxed out by the sqlserv.exe process. This appears to be the embedded version rather than a full featured install of MSSQLserver. Disabling the ASUS service causes the CPU usage to drop from a saturated 100% to a variable 1-2%. I have run the code to decline the superceded patches but this has not resolved the problem. I see that a regular maintenance schedule includes rebuilding the indexes in the database. To that end I have downloaded the SQL script and sqlcmd.
However when I try to to install MsSqlCmdLnUtils.msi (msiexec -i MsSqlCmdLnUtils.msi via powershell remote session) I get returned to the command prompt after a short pause but the program is not installed.
Can aynone advise how to get this installed?
Answer: The prompt returns because the powershell remote session can't show you the setup UI.
However, you can connect to Windows Server Core using RDP and start the installation from the command line you'll get after logging in.
On the other hand, I recommend you to install sqlcmd on your computer/management workstation and use it remotely: sqlcmd -S ComputerA\instanceB
More details about sqlcmd: Connect to the Database Engine
Comment: "...install sqlcmd on your computer..." but I was under the impression that the embedded SQL Server (compared with the standalone server) only supported local named pipes. I'll check netstat when I'm back on the server.
Comment: "...connect to Windows Server Core using RDP" while I get an RDP connection - that's all. No shell. No cursor. Just a black screen :(

How to install sqlcmd on windows core


Como ler números do teclado em C#?

Question: Eu sei que getchar() obtém caracteres do teclado por stdin, porém quero saber como introduzir números em uma variável.
Pois se uso getchar() e digito um 2, por exemplo, a variável torna-se 50, e obviamente não é o que eu quero.
Comment: C, C# ou C++? Tô confuso.
Comment: As respostas aqui, até agora, funcionam, para um exercício rápido, mas para uso geral é preciso um pouco mais para garantir que a entrada está correta.
Comment: Tem `stdin` em C#? Espera, tem a tag C, mas o título da pergunta é C#, espera, ali tem C++ também, mas que confusão!
Comment: Por favor quando for postar sua pergunta coloque de forma clara e objetiva com informações detalhadas sobre o que você necessita e qual ou quais linguagens você estará utilizando.Sua pergunta ficou muito complicada de se entender,e usou as tags ainda somente para complicar mais. Por isto vou dar -1 para sua pergunta.
Answer: Bem, você não especificou a linguagem, então vamos lá:
C
Para ler um número, vou supor que seja uma int, em C o seguinte código é usado:
int meuNumero; scanf("%d", &meuNumero);

C++
Para ler um número em C++ usamos a stream cin com o operador >>:
int meuNumero; cin >> meuNumero;

C#
Em C# temos que chamar o método Console.ReadLine para lermos a entrada e Convert.ToInt32 ou Int32.Parse para transformarmos para um número:
int meuNumero = Convert.ToInt32(Console.ReadLine())

Answer: Se for C# você pode usar o Console.ReadLine() mas não se esqueça que o ReadLine() retorna uma String, por isso, é preciso converter. Você pode usar o Convert.ToInt32(Console.ReadLine());.
Então fica:
int input = Convert.ToInt32(Console.ReadLine());.
Se você digitar uma String que não seja int, provavelmente você receberá uma exceção.
Se você estiver usando C, com a biblioteca stdio.h, você conseguirá pegar um int usando o famoso scanf().
Então fica:
int input;
scanf("%d", &input);

Por fim, se tiver usando C++, você pode sempre usar o namespace sdt.
Então fica:
int input;
std::cin >> input;

Espero ter ajudado.
Answer: Na linguagem C, use getch() para capturar o primeiro caractere digitado, exemplo:
char op;
op=getch(); //Lê a primeira tecla que for digitado no teclado, e grava na variável
printf("Caractere digitado: %c", op); //Printa na tela o caractere digitado

Se quiser gravar mais de um caractere em uma variável usa o scanf() como já exemplificado nas outras respostas.
Se você quiser receber apenas números e fazer cálculos com eles use o scanf() utilizando variáveis do tipo int para numeros inteiros e float para não inteiros, por exemplo:
//Fazendo cálculos com números inteiros
int a, b, result_int;
printf("Digite o primeiro valor: \n");
scanf("%d", &a);
printf("Digite o segundo valor: \n");
scanf("%d", &b);

result_int = a + b;
printf("a + b = %d\n", result_int); //Printa na tela o resultado da soma

//Fazendo cálculos com números não inteiros
float c, d, result_float;
printf("Digite o primeiro valor: \n");
scanf("%f", &c);
printf("Digite o segundo valor: \n");
scanf("%f", &d);

result_float = c + d;
printf("c + d = %f\n", result_float); //Printa na tela o resultado da soma
printf("c + d = %.2f\n", result_float); //Printa na tela o resultado da soma, limitando as casas depois da virgula para melhor visual

Compila o código para você entender melhor rsrs.

Como ler números do teclado em C#?


Should I decompose time series before applying DTW

Question: I have two time series which I want to compare by using the 'Dynamic Time Warping' technique. However, I am only interested in comparing the trend component of them (i.e. excluding the seasonal and random components from the comparison).
I wonder if it makes sense to perform an extra step in which I extract the trend component from the two time series before compare them together by using the dtw package in R, or I would get similar results by directly comparing the original time series via the dtw package.
Answer: Yes, you should. Otherwise DTW will attempt to explain vaiblity that exists only in the y-axis, by spurious warping of the x-axis. This is explained in [a]
See also  https://www.cs.ucr.edu/~eamonn/sdm01.pdf
[a] https://www.cs.ucr.edu/~eamonn/sdm01.pdf
Answer: You are right to question this. Indeed if we are only interested in the trend component it makes more sense to focus on that.
The reason is that random variation due to noise, seasonal trends, etc. will make the alignment task of the DTW more demanding and potentially result in misleading results. That is because the alignment of localised features might take precedence over the overall trends; the potential misalignment of peaks due to noise or external factors would induce substantial error accumulation.
A standard time-series decomposition technique like STL by Cleveland et al. (1990) STL: A Seasonal-Trend Decomposition Procedure Based on Loess  (available in forecast::stl) is fine to start with but do consider other algorithms too like Ensemble Empirical Mode Decomposition (EEMD) from Wu and Huang Empirical Mode Decomposition: a Noise-Assisted Data Analysis Method
 (available in Rlibeemd::eemd) or (X-13) Seasonal Extraction in ARIMA Time Series (SEATS) from Dagum and Bianconcini's (2016) "Seasonal Adjustment Based on ARIMA Model Decomposition: TRAMO-SEATS") (available in seasonal::seas).


Riemann hypothesis, random walks and Möbius function

Question: How to show that the Riemann hypothesis, random walks and the Möbius function are related or even equivalent?
I was reading the paper Randomness and Pseudorandomness by Avi Wigderson, but to me the most interesting thing wasn't discussed there.
Comment: This blog post I wrote a couple of years ago might be of interest to you: http://www.riemannhypothesis.info/2014/10/tossing-the-prime-coin/
Answer: The fact that the Riemann Hypothesis and the Mobius function are related has its origin in the formula $$\frac{1}{\zeta(s)} = \sum_{n=1}^\infty \mu(n)n^{-s}.$$
The relation to random walks is that the Riemann Hypothesis implies that the cumulative sum of the Mobius function $$ M(x) =\sum_{n\le x}\mu(n)$$ has random walk-like asymptotics $M(x)  = O(x^{1/2+\epsilon}).$ The naive guess is that if the Mobius function were a random walk, the order of growth would be $\sqrt{x\log\log(x)}$ (the law of iterated logarithm) so the Riemann hypothesis gets us fairly close to this.
The identity relating the zeta function and the Mobius function is straightforward and can be understood by a person without much background with little work. It has to do with Euler's identity $$ \zeta(s) = \prod_p\frac{1}{1-p^{-s}}.$$ You can find derivations of this identity in a lot of places. Basically you expand the factor in a geometric series and multiply out. The identity to the sum form $ \zeta(s) = \sum_{n=1}^\infty n^{-s}$ then follows from the fundamental theorem of arithmetic. Then if you flip over Euler's identity and multiply it out, thinking about which terms are present and whether they have plus and minus signs will lead you to the identity $$ \frac{1}{\zeta(s)} = \prod_p (1-p^{-s}) = \sum_{n=1}^\infty \mu(n) n^{-s}$$ A good for-the-layman treatment of Euler's identity as well as more advanced stuff and connections to randomness can be found in Prime Obsession by (the regrettably somewhat disgraced) Derbyshire.
But the latter fact relating the Mobius function to a random walk requires some more heavy lifting.
In brief, the $1/2$ power and the fact that the zeros have $s=1/2$ are intimately related. The origin is that cumulative functions $F(x) = \sum_{n\le x}f(n)$ are related to Dirichlet series $\tilde f (s)=\sum f(n)n^{-s}$ by a Mellin transform $$ \frac{\tilde f(s)}{s} = \int_1^\infty x^{-(s+1)}F(x)dx$$ which can then be inverted to $$ F(x) = \frac{1}{2\pi i}\int_{a-i\infty}^{a+i\infty}\frac{\tilde f(s)}{s}x^sds.$$
If you use Cauchy's theorem to do the transform integral, you will see that the poles of $\tilde f(s)$ with largest real part contribute the leading order terms in the asymptotic series for $F(x)$. Since here we have $\tilde f = \frac{1}{\zeta(s)}$ we see that, assuming the Riemann Hypothesis, the largest poles of the integrand (i.e. the zeros of the zeta function) have real part $s=1/2$ so contribute a leading term $\sim x^{1/2}$ with oscillations from the imaginary parts
This of course sweeps a lot of details under the rug, but gives a basic outline of the relationship.
To see demonstrations of these facts one can consult an introductory treatment of analytic number theory. (Notes by Hildebrand are available free online and are quite good.)
Answer: These three concepts are not equivalent as such, but there is an interesting connection.
We can think of a simple symmetric random walk on $\mathbb{Z}$ as a sequence of random variables $Z_{1},Z_{2},...$ where each $Z_{k}$ takes the value $+1$ or $-1$, each with probability $1/2$. We can then define $S_{n}=\sum_{k=1}^{n}Z_{k}$ to be the position after $n$ steps of the random walk. One can then ask about what can be said about $E(|S_{n}|)$, the expected distance of the walk from the origin after $n$ steps. As is noted in the article, $E(|S_{n}|)$ is "about $\sqrt{n}$". More precisely $$\lim_{n\rightarrow\infty}\frac{E(|S_{n}|)}{\sqrt{n}}=\sqrt{\frac{2}{\pi}}$$
In particular, we can see that $E(|S_{n}|)=O(n^{\epsilon+\frac{1}{2}})$ for any $\epsilon>0$.
How does this relate to the Riemann Hypothesis? Well there is a formulation of the Riemann Hypothesis which bares a striking resemblance to the above statement. The idea is outlined in the article in the following way. Instead of using random variables $Z_{k}$ to determine each step, use $\mu(k)$. Here $\mu$ is the Mobius function, which is defined by setting $\mu(k)=+1$ if $k$ is a product of an even number of distinct primes, $\mu(k)=-1$ if $k$ is a product of an odd number of distinct primes, and $\mu(k)=0$ otherwise. Again we can think of using $\mu$ to tell us how to walk along $\mathbb{Z}$.
Now define the Mertens function $M(n)=\sum_{k=1}^{n}\mu(k)$. This function tells us where we are after $n$ steps using this method. Note that, unlike with the random walk, this walk is deterministic so we are looking at $M(n)$ directly and not some expectation. So, as with the random walk, what can we say about $M(n)$? Well, as is hinted at in the article, it turns out that the Riemann Hypothesis is equivalent to the conjecture that $$M(n)=O(n^{\epsilon+\frac{1}{2}})$$ holds for all $\epsilon>0$. Compare this with what was said about the random walk.
Comment: Could you explain where the $\epsilon$ is coming from in your big Oh notation?
$$\lim_{n\to\infty}\frac{E(|S_n|)}{\sqrt{n}} = \sqrt{\frac{2}{\pi}}$$
gives us that there exists an $N$ such that for all $n> N$,
$$E(|S_n|) < 2\sqrt{\frac{2}{\pi}}\sqrt{n}$$
and so $E(|S_n|)=O(n^{\tfrac{1}{2}})$
Comment: @adfriedman This might somewhat depend on your definition of $O$. We write $f=O(g)$ to mean that there is some absolute positive constant $C$ such that $f(n)\leqslant C\cdot g(n)$ holds for all $n$. As you mention, it can be shown that $E(|S_n|)=O(n^{\frac{1}{2}})$. However, since for all $\epsilon>0$ we have $n^{\frac{1}{2}}\leqslant n^{\epsilon+\frac{1}{2}}$, we also have $E(|S_{n}|)=O(n^{\epsilon+\frac{1}{2}})$ for any $\epsilon>0$. I admit it seems strange to look at this weaker statement, but this is done to compare with the conjecture for $M(n)$.
Comment: I use the same definition, I guess I just tend to expect the best upper bound an author can produce unless otherwise stated, so I was concerned that I was missing something. Using it to draw a comparison makes sense.
Comment: @JonCC, can you please add this to your answer?
Comment: I like the description given in the introduction to this article: https://faculty.math.illinois.edu/~hildebr/ugresearch/randomness-spring2014report.pdf. It asserts that we can model $M(x)$ as a $\{-1,0,1\}$-weighted random walk on the integer line whose values at each step are encoded by the number of distinct prime factors of each $n \geq 2$ -- effectively, the values of $\lambda(n)$ on the squarefree integers, which correspond to a pseudo-random "digital coin flip". Nice!


How does the tonic solfa work in minor keys?

Question: I'm curious as to how it works it minor keys specifically melodic and harmonic minors. I'm assuming it is different than the regular Do-Rei-Mi.
Comment: Well what system  are you using, fixed do or movable do?
Comment: Lets start with Fixed
Answer: There are two ways to use the do-re-mi note names:

Absolute note names
Relative note names

If you use them as absolute note names, you just call C do, D re etcetera, and if you encounter alterations, use the naming suggested by Babu (although he suggested it in the context of relative note names).
If you want to use them as relative note names, Babu suggests to call the tonic Do. I don't like this, I prefer to call the tonic of major keys Do and the tonic of minor keys La. A few advantages this has:

There is no difference in note names between a key and its relative. The natural minor scale contains exactly the same notes as the major scale, it just starts from the sixth note of the major scale. (This means that if a piece switches between the two - which often happens - you don't need to think about naming.)
You need fewer accidentals then with Babu's method. You would need them only for the harmonic (...-mi-fa-si-la - I use "si" for raised sol, "ti" for the note below "do")  and melodic (...-mi-fi-si-la-sol-fa-mi) minor scales.
It results in a distinction between minor and major keys in the very naming scheme, while all keys are the same. This reflects how it is in singing, where it doesn't matter from which key you start (barring range issues), but it does matter which mode you use.
This system is easily extendable to church modes, you can just say Dorian starts on re, Phrygian on mi, Lydian on fa and Mixolydian on sol. (Aeolian on la and Ionian on do as discussed, and if you want to be really pedantic, Locrian on ti.)

The relative note names work because there are fixed intervals between the notes: do-re, re-mi, fa-sol, sol-la is one tone, mi-fa and ti-do a semitone. (Of course alterations lower or raise the altered note by a semitone. I'm leaving the distances including altered notes as an exercise for the reader.)
Comment: Some pieces of music tend to modulate between relative major and minor keys, some between parallel major and minor keys, some modulate among both kinds, and some don't modulate at all.  For those of the first type, making the minor tonic "la" would make sense, but for those in the second, using "do" for the common tonic would make more sense.  For pieces that modulate in both ways, I'd suggest following the notated key signature (since many such pieces use accidentals rather than notating all the modulations).
Answer: Solfege has names for altered notes. From fixed Do as C, we have:

    C# as Di,
    D# as Ri,
    F# as Fi,
    G# as Si, and
    A# as Li,

We also have:

    Db as Ra,
    Eb as Me (pronounced "May", not as the English word "Me"),
    Gb as Se,
    Ab as Le, and
    Bb as Te.

I'm told (through Wikipedia's Solfège Page) that there are names for double-sharp and double-flat notes, but I've never used them and cannot comment on their general acceptance.
In fixed Do, conceptually, it's a straightforward conversion; C# is Di, so you always sing Di when you see C#. It's a little trickier to sing than movable do (a.k.a. tonic sol-fa). In movable Do, you only alter the notes of the scale which are flatted compared to the parallel major. So a natural minor scale would be:

    Do-Re-Me-Fa-Sol-Le-Te-Do

A harmonic minor scale would be:

    Do-Re-Me-Fa-Sol-Le-Ti-Do

An ascending melodic minor scale would be:

    Do-Re-Me-Fa-Sol-La-Ti-Do

And descending would be the same as natural minor.


How to solve the huge HTTPRL error that fills down all of Cron \ Status report?

Question: Background
For some time now, and only in some occasions, when I run CRON (Status report), I see a huge error (About 170 rows) at the HTTPRL section (related to the HTTP Parallel Request & Threading Library module). It happens randomly - Sometimes it happens and sometimes it doesn't. When the error does not appear, the HHTPRL "section" in status report is all green. Here is a screenprint of it:

What I already tried
I've tried to play a bit with the HTTPRL preferences, but nothing really helped... For example, tried to change at "IP Address to send all self server requests to": from -1 to 1. Also tried to uncheck "Enable background callbacks", But IIRC nothings has changed.
The only changes I saw (When the error was apparent) were when I unchecked "Enable background callbacks", or otherwise, when I check-marked "Use httprl to handle drupal_http_request" --- When I ran Cron afterwards the error's text went from about 170 rows to 90 rows. But this data doesn't help me to understand Why the error was there on the first place and even worse - Why it appears randomly?
Bottom line
Is this a known problem? You might have an idea why this happens or a tip how to try to solve it?
Comment: That "IP address..." setting definitely needs to be -1 because you need it to use the hostname to hit the server rather than the IP.  My guess is that your server's just really slow and isn't responding fast enough to the test call in some cases.  Either that or there's some reason why the server's hostname isn't being correctly deduced.  Your error shows it's attempting to connect via IP which will never work.  Would be interesting to see if there's an error with it trying to connect via hostname.
Comment: This might be as simple as leaving that field blank, unless you're doing some interesting redirection of the base URL.
Comment: Looking at the code suggests simple logic to determine your server's IP address but the logic isn't handling extracting the IP for your online site. Do you know the IP? If so, what happens if you enter it?
Comment: That sounds consistent with this bug ticket: https://www.drupal.org/node/2299009 Perhaps try using the dev version of the module. The stable version hasn't been updated since Jan 2014.
Comment: Hi, leaving this field blank didn't help... Moreover, It returns automatically to be "-1" right after I run Cron\Status report.
Comment: When I type the shared-server's IP address in the browser's address bar, I get a typical "Oops the site was not found" from my hosting service... and yet, when I type to domain itsef the site works fine.
Answer: The solution (or at least work around?) could be what is described in Comment # 8 of Issue # 2450725 related to the HTTP Parallel Request & Threading Library module:

change "'timeout' => 7," to "'timeout' => 1,"
This timeout is used in lock_acquire() that probably locks Drupal for 7 seconds, but in httprl config httprp waits only 5 seconds for a response.

Comment # 17 in that issue also contains a corresponding patch, which looks like so:
diff --git a/httprl.install b/httprl.install
index d7e2963..e2d4f04 100644
--- a/httprl.install
+++ b/httprl.install
@@ -177,7 +177,7 @@ function httprl_install_http_test($mode, $blocking = FALSE) {
       // Setup options array.
       'options' => array(
         'blocking' => $blocking,
-        'timeout' => 7,
+        'timeout' => 1,
         'max_redirects' => 0,
         'headers' => $headers,
       ),

Answer: I faced the same issue.
Executing the following code did the trick:
variable_set('httprl_install_lock_time', 1);

I use devel/php. It can be done via drush as well:
drush php-eval "variable_set('httprl_install_lock_time', 1);"


Ler o arquivo de um input usando JavaScript

Question: Eu criei um  e gostaria de ler o conteúdo desse input antes de manda-lo para o servidor. Vi que posso fazer isso usando um filereader mas as informações que encontrei estavam meio confusas.
Como posso fazer essa leitura?
A leitura seria de um arquivo txt.
var file = document.getElementById("inputmarc");
var fr = new FileReader();

fr.readAsBinaryString(file.files[0]);
fr.onload = function(e) { alert(e); }

No HTML:
<input type="file" id="inputmarc">

Comment: Tem como colocar o código que tentou fazer? Aproveitando, pode colocar também um exemplo do que deseja fazer, tal como o formato do arquivo que será lido e qual o conteúdo que espera ler nesse arquivo.
Comment: Acho que ainda não posse editar uma pergunta, é um arquivo txt.`var file = document.getElementById("inputmarc");

        var fr = new FileReader();
        fr.readAsBinaryString(file.files[0]);
        fr.onload = function(e)
        {
            alert(e);
        }`
Comment: E o código HTML?
Comment: É só um
Answer: Veja esse exemplo se te ajuda

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />

    <title>FileAPI HTML5</title>

   Por favor selecione arquivo que será lido:
    <input type="file" id="txtfiletoread" />

Conteúdo do arquivo:

Answer: Utilizando o FileReader funciona somente em navegadores com HTML5.
<input type="file" id="fileinput" />

Fonte: Fonte do exemplo acima
Navegadores compatíveis: http://caniuse.com/#feat=filereader
Veja outras infos em:

https://www.w3.org/TR/FileAPI/#dfn-filereader
https://developer.mozilla.org/en-US/docs/Web/API/FileReader


Problema com Calculo de Gastos com PHP

Question: Estou tentando fazer um calculo de gastos no sistema porém não estou tendo sucesso.
O sistema funciona da seguinte forma, o Usuario faz um pedido envolvendo 1 ou mais produtos, esses pedidos ficam armazenados em uma tabela.
Cada Produto cadastrado no sistema fica armazenados em outra tabela, tendo os campos padrões, em estoque, custo unitario, ja enviados e pendentes.
O Usuário possuiu duas tabelas, uma para a Filial que ele gerencia e a outra é a conta dos usuarios.
O problema é, ter o valor de pedidos para cada filial. Ou seja, somar quantos itens do produto foram enviados para a filial e depois multiplicar pelo custo dele, e por fim, somar todo o resultado para exibir o gasto total da filial no momento.
Eu tentei usando while porém ele exibi várias vezes o mesmo produto em quantidades diferentes para a mesma filial.
LEGENDAS
filiais = tabela onde fica todas as filiais cadastradas;
papelaria_itens = tabela onde fica os produtos;
papelaria_solicitacoes = tabela onde fica os pedidos;
papelaria_solicitados = tabela onde fica os produtos solicitados no pedido;
qnt_enviada = Quantidade Enviada;
custo = Custo Unitario por produto;
chns = Código exclusivo para cada dado cadastrado no sistema, funciona como um número de protocolo.
PHP
estou usando o PDO para conexão
$procura_filial = $site->query("SELECT * FROM filiais ORDER BY codigo ASC");
while ($filiais = $procura_filial->fetch(PDO::FETCH_OBJ)) {
    $procura_solicitacoes = $site->query("SELECT * FROM papelaria_solicitacoes WHERE filial = '$filiais->chns'");
    if ($procura_solicitacoes->rowCount() <= 0) {
    }else {
        while($solicitacoes = $procura_solicitacoes->fetch(PDO::FETCH_OBJ)){
            $procura_solicitados = $site->query("SELECT * FROM papelaria_solicitados WHERE chns = '$solicitacoes->chns'");
            while ($solicitados = $procura_solicitados->fetch(PDO::FETCH_OBJ)) {
                $reprocura_solicitados = $site->query("SELECT * FROM papelaria_solicitados WHERE filial = '$solicitados->filial' and produtos = '$solicitados->produtos'");
                //SOMA QUANTIDADE ENVIADA DE CADA PRODUTO
                while($soma_qnt = $reprocura_solicitados->fetch(PDO::FETCH_OBJ)){
                    $val1 = $soma_qnt->qnt_enviada;
                    $total_cont += $val1;
                    $total_produto_env = $total_cont;
                }
            }
        }
    }
    echo "Filial: ".$soma_qnt->filial." Produto: ".$soma_qnt->produtos." Enviados: ".$total_produto_env."
";
}

Answer: Vejo que você está indo muito na tabela papelaria_solicitados ou seja está errado.
Você deveria tratar estes cálculos diretamente no banco de dados sem precisar fazer vários  Selects e varios whiles. Seu programa ficara muito lento assim.
Faça algo como:
SELECT filial,count(*) as total FROM papelaria_solicitados GROUP BY filial,produtos

E depois exibir somente em um while.
Espero ter ajudado,
Saudações,
Comment: Rodrigo, você poderia me explicar melhor sua sugestão, eu não entendi direito.
Comment: Qual a parte você não entendeu direito?

Problema com Calculo de Gastos com PHP


Login sequence using DataSet in Windows Form (VB.NET)

Question: I created a DataSet for my users tables called UserDataSet and added a SELECT query which filters by username and password called GetDataByAuthentication(Username,Password)
Now, I'm trying to retrieve the row's columns returned when I call UsersTableAdapter.GetDataByAuthentication but I can't figured out how. Here's my code:
Dim table As UsersDataSet.UsersDataTable = Me.TableAdapter.GetDataByAuthentication(_User.Username, _User.Password)

    If (table.Rows.Count > 0) Then

        Dim row as DataRow = table.Rows(0)

        _User.FirstName = // what here?
        _User.LastName = // what here?
        _User.Username = // what here?
        _User.Password = // what here?
        _User.IsAdmin = // what here?

        Return True
    End If

Answer: I guess...
Replace row definition for:
Dim row as UsersDataSet.UsersRow

and then:
_User.FirstName = row.FirstName ' Or whatever is the FirstName column.

and row will have the properties you're looking for...
Hope it helps!
Comment: I'm pretty sure it would be `UsersDataSet.UsersDataRow` but this is essentially correct.
Comment: @ConradFrix I promise you I thought the same, and before put the answer opened Visual Studio to ensure the right value here :) ... Table names are [TABLE]DataTable and rows are [TABLE]Row. Thank you for the advice!


WASD movement with ncurses (Not sure why it doesnt work)

Question: I wanted to create a program with what i had just learned in ncurses. With it the user should be able to draw with a # in the screen using WASD. this is the code i did:
#include<stdio.h>
#include<ncurses.h>

int main (void)
{
        initscr();
        noecho();
        int coord_x = 10;
        int coord_y = 10;
        char direccion;
        mvwprintw(stdscr,coord_y,coord_x,"#");
        while (1)
        {
                refresh();
                direccion = getchar();
                switch (direccion)
                {
                  case 'w':
                        coord_y -= 1;
                  case 's':
                        coord_y += 1;
                  case 'a':
                        coord_x -= 1;
                  case 'd':
                        coord_x += 1;
                  case 'q':
                        break;
                  default:
                        continue;
                }
                if (coord_x == -1 && coord_y == -1) { coord_y += 1;coord_x += 1;}
                mvwprintw(stdscr,coord_y,coord_x,"#");
                if (direccion == 'q') {break;}
        }
        endwin();
        return 0;
}

But i am not really sure why does the # wont move neither up or left. I think the problem is in this part:
direccion = getchar();
                switch (direccion)
                {
                  case 'w':
                        coord_y -= 1;
                  case 's':
                        coord_y += 1;
                  case 'a':
                        coord_x -= 1;
                  case 'd':
                        coord_x += 1;
                  case 'q':
                        break;
                  default:
                        continue;
                }

But i am really not sure, do you know why it doesnt work?
EDIT: Thanks to everybody, now i realised im just dumb and forgot how to use a switch
Answer: You need to add breaks after each assignment in your switch statement:
switch (direccion) {
case 'w':
    coord_y -= 1;
    break; /* <-- here */
case 's':
    coord_y += 1;
    break; /* <-- here */
case 'a':
    coord_x -= 1;
    break; /* <-- here */
case 'd':
    coord_x += 1;
    break; /* <-- here */
case 'q':
    break;
default:
    continue;
}

The default behavior of the case is to "fall through" to the next one, so if you press w it is going to execute all of the coord_x and coord_y assignments, not just the one you intend.
Comment: As an aside, you should also call `curs_set(0);` to hide the cursor. Looks better that way.


help on manipulating this algebraic expression

Question: So I have something like:
$\frac {k!}{(k-3)!3!}$
I'm going to add $\frac 12k(k-1)$ to this, and I want to obtain $\frac {(k+1)!}{(k-2)!3!}$ as the result. I'm having trouble with this since I need a $k+1$ in the numerator and a $k-2$ in the denominator. Any hints please? Thank you.
Comment: Note that what you add is $$\frac{k!}{(k-2)!2!}.$$ Find the common denominator, add.
Comment: What do you mean by "I'm going to do $\frac 1 2 k (k - 1)$ to this"?
Comment: Do you want to obtain $\frac{(k+1)!}{(k-2)!3!}$ from the original expression?
Comment: I meant I wanted to add, sorry about that typo
Comment: ^ That worked, that was a really smart way to think of it, thank you.
Answer: $\frac {k!}{(k-3)!3!}=\frac{k(k-1)(k-2)(k-3)}{6(k-3)}=\frac{k(k-1)(k-2)}{6}$
$\frac{k(k-1)(k-2)(k-3)}{6(k-3)}=\frac{k(k-1)(k-2)}{6}$
$\frac {k!}{(k-3)!3!}+\frac{1}{2}k(k-1)=\frac{k(k-1)(k-2)}{6}+\frac{1}{2}k(k-1)=\frac{k(k+1)(k-1)}{6}=\frac{(k+1)!}{3!}$.
On the other hand $\frac {(k+1)!}{(k-2)!3!}=\frac {(k+1)k(k-1)(k-2)!}{(k-2)!3!}=\frac{(k+1)!}{3!}$. So they are equal.


How to find what .NET method led caused the worker to stop

Question: My code has deadlocked. Here is the stack trace for the main thread:

[Managed to Native Transition]

WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetMessageW(ref System.Windows.Interop.MSG msg,
    System.Runtime.InteropServices.HandleRef hWnd, int uMsgFilterMin, int
    uMsgFilterMax) + 0x14 bytes
      WindowsBase.dll!System.Windows.Threading.Dispatcher.GetMessage(ref
    System.Windows.Interop.MSG msg, System.IntPtr hwnd, int minMessage,
    int maxMessage) + 0x80 bytes
      WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame
    frame) + 0x75 bytes
      WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame
    frame) + 0x49 bytes
      WindowsBase.dll!System.Windows.Threading.Dispatcher.Run() + 0x4b
    bytes
      PresentationFramework.dll!System.Windows.Application.RunDispatcher(object
    ignore) + 0x17 bytes
      PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window
    window) + 0x6f bytes
      PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window
    window) + 0x26 bytes
      PresentationFramework.dll!System.Windows.Application.Run() + 0x1b
    bytes     MainDashboard.exe!MainDashboard.App.Main() + 0x59 bytes C#
      [Native to Managed Transition]      [Managed to Native Transition]
      mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,
    System.Security.Policy.Evidence assemblySecurity, string[] args) +
    0x6b bytes
      Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    + 0x27 bytes      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object
    state) + 0x6f bytes
      mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state, bool preserveSyncCtx) + 0xa7 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state, bool preserveSyncCtx) + 0x16 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state) + 0x41 bytes
      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44
    bytes     [Native to Managed Transition]

How do I know what part of my code is responsible.  The application entry point is:

MainDashboard.exe!MainDashboard.App.Main() + 0x59 bytes C#

and that is the only line in this stacktrace that is from my code.
Trying to view the call stack for two other frames displays the following:

The current thread is not currently running code all the call stack is
  could not be obtained.

Here is the call stack of another worker thread:

[Managed to Native Transition]

System.dll!Microsoft.Win32.SystemEvents.WindowThreadProc() + 0xaf bytes
      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object
    state) + 0x6f bytes
      mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state, bool preserveSyncCtx) + 0xa7 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state, bool preserveSyncCtx) + 0x16 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state) + 0x41 bytes
      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44
    bytes     [Native to Managed Transition]

And here is the call stack of the last thread:

[Managed to Native Transition]

Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.WaitForThreadExit()
    + 0x93 bytes      Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunParkingWindowThread()
    + 0x253 bytes     mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object
    state) + 0x6f bytes
      mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state, bool preserveSyncCtx) + 0xa7 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state, bool preserveSyncCtx) + 0x16 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
    executionContext, System.Threading.ContextCallback callback, object
    state) + 0x41 bytes
      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44
    bytes     [Native to Managed Transition]

The app is hanged. there is a total of 49,000 records it needs to process. it stopped at 3,029. The app is not using any resources at this point. Task Manager is showing it using 0% CPU. The UI is responsive, but it is designed to be always responsive.
Also, this is not deterministic. I mean, if I restart the app, it will hang at a different point in the code, so there is not one record that has an issue that brings the whole thing down.
Comment: Some versions of Visual Studio have a concurrency visualizer which should be able to do what you want.
Comment: The fact that the processing has stopped does not mean that there's any deadlock. It may be something as simple as yours main-processing loop getting `break`ed improperly or some exception was thrown and some outer `try/catch` has jumped out of your main-processing loop and hence halted the process. If you had the debugger attached, check the Output for any `first-chance exception` notifications. Any NullReference, ArgumentOutOfRange, NotImplemented, InvalidOperation etc may be a sign of a bad errorhandling that simply halted your loop.
Comment: How about putting `try ... catch` in your threads and have them log the exceptions?
Comment: How does stack trace look for the other threads?
Comment: @kkokosa, I edited the question with the other call stacks.
Comment: No evidence for deadlock at all, everything looks normal.
Comment: @HansPassant, so where is it stopped? I know there is no info there for you to answer that, but why has the app stopped? What could the cause of something like that be? Or how would I go about trying to find out.
Comment: When you say that it's deadlocked, what do you mean? Is the UI not responding? Are the threads that should be doing something not doing anything? Does Task Manager show that your app is using 0% CPU?
Comment: @JimMischel, the app is hanged. there is a total of 49,000 records it needs to process. it stopped at 3,029. The app is not using any resources at this point. Task Manager is indeed showing it using 0% CPU. The UI is responsive, but it is designed to be always responsive.
Comment: @quetzalcoatl, you are absolutely right. Output says that a bunch of First Chance exceptions have been thrown and a bunch of threads have exited right after the first chance exceptions. How do I debug this further. The output does not have line numbers or any way to see what the exception was. Thanks!
Comment: So your worker threads are supposed to be processing items, but they aren't. Are they waiting on a concurrent queue of some type? Are you using synchronization primitives (locks, semaphores, events, etc.) to control access to shared state? That's the place to look.
Comment: @JimMischel, thanks, I added a couple of try catches towards the entry point of the application. Thee should grab any exceptions that are not handled, running again, lets see what I will catch :)
Answer: Since you now noticed 'first chance exceptions' in your 'Output' and they seem to kill your threads, there's one more thing other than just try/catch/log.
Use the debugging features of VS/CLR even further. Go to DEBUG menu, then EXCEPTIONS then find one (or all, but I prefer to do it one-by-one) of the exceptions you identified as thrown-and-killing-your-threads, i.e. InvalidOperationException and check the tick "thrown" or "unhandled" (depending on what you want and what version of VS you have).
Now, assuming you ticked "thrown", the IDE will automatically break/halt the program whenever it tried to throw exactly this one type of exception.
..and not only it will halt the program, it will also jump there as if you placed a breakpoint. You'll instantly get all the stacktraces, variables, code, etc you'd like to inspect.
But, on the other hand, if you app is spamming with this kind of exception with rate of thousands per second, just because someone was lazy and if/else'd some bad-arguments case and try/caught the exception instead, then trying to rely on this exception being "thrown" will .. well.. fail. In such case, you might want to try with "unhandled" tick instead (if you see it in the IDE at all), but of course it has its own limitations, too. Nevertheless, they are really precious tools if only you can attach the IDE to the failing process. Insta-break on the unknown line with problems!


Solve the differential equation $(2x^3y+4x^3-12xy^2+3y^2-xe^y+e^{2x})dy+(12x^2y+2xy^2+4x^3-4y^3+2ye^{2x}-e^y)dx=0.$

Question: Solve the differential equation $(2x^3y+4x^3-12xy^2+3y^2-xe^y+e^{2x})dy+(12x^2y+2xy^2+4x^3-4y^3+2ye^{2x}-e^y)dx=0.$
I tried solving the problem like this:

We assume $M=(12x^2y+2xy^2+4x^3-4y^3+2ye^{2x}-e^y)$ and $N=(2x^3y+4x^3-12xy^2+3y^2-xe^y+e^{2x}).$ Now, we observe, $\frac{\partial  M}{\partial y}=12x^2+4xy-12y^2+2e^{2x}-e^y$ and $\frac{\partial N}{\partial x}=6x^2y+12x^2-12y^2-e^y+2e^{2x}.$ We find, that if we considered $M=12x^2y+4x^3-4y^3+2ye^{2x}-e^y$ and $N=4x^3-12xy^2+3y^2-xe^y+e^{2x}$ then $\frac{\partial  M}{\partial y}=12x^2-12y^2+2e^{2x}-e^y=\frac{\partial  N}{\partial x}.$ So, we consider or rather write the given equation, as $(12x^2y+4x^3-4y^3+2ye^{2x}-e^y)dx+(4x^3-12xy^2+3y^2-xe^y+e^{2x})dy+2xy^2dx+2x^3ydy=0.$
Now, we consider the differential equation, $(12x^2y+4x^3-4y^3+2ye^{2x}-e^y)dx+(4x^3-12xy^2+3y^2-xe^y+e^{2x})dy=0.$ This equation is an exact differential equation. The solution is of this differential equation is $$\int (12x^2y+4x^3-4y^3+2ye^{2x}-e^y)dx+\int 3y^2dy=c_1\implies 4x^3y+x^4-4y^3x+ye^{2x}-e^yx+y^3=c_1.$$
Next we try to find the solution of the differential equation, $2xy^2dx+2x^3ydy=0.$ We write this differential equation as $\frac{dy}{dx}=\frac{2xy^2}{2x^3y}=\frac{y}{x^2}\implies \frac{dy}{y}=\frac{dx}{x^2}.$ On integrating, both sides of $\frac{dy}{y}=\frac{dx}{x^2},$ we obtain,
$\int\frac{dy}{y}=\int\frac{dx}{x^2}\implies \log y=-\frac 1x+c_2\implies \log y+\frac 1x=c_2.$
Now, we add the solutions, of both of the differential equation, $(12x^2y+4x^3-4y^3+2ye^{2x}-e^y)dx+(4x^3-12xy^2+3y^2-xe^y+e^{2x})dy=0$ and  $2xy^2dx+2x^3ydy=0,$ to get the solution of the given differential equation, $$(2x^3y+4x^3-12xy^2+3y^2-xe^y+e^{2x})dy+(12x^2y+2xy^2+4x^3-4y^3+2ye^{2x}-e^y)dx=0$$  i.e
$$4x^3y+x^4-4y^3x+ye^{2x}-e^yx+y^3+\log y+\frac 1x=c_1+c_2=c.$$ So, the solution of the given differential equation is $4x^3y+x^4-4y^3x+ye^{2x}-e^yx+y^3+\log y+\frac 1x=c.$

It seems, that when I approach the problem like this, we get a solution of the given equation. But is this a legit way? I don't understand, this this way of solving differential equations is justified. Finally, I want to know whether the solution, I calculated is valid or not?
Comment: I think that the incorrect step is where you did this : "Next we try to find the solution of the differential equation, $2xy^2dx+2x^3ydy=0.$" The addition of those two terms is assumed to be zero without justification.
Comment: @An_Elephant Yes, that's it. The justification, is what I am looking for.
Comment: An irrelevant picky point: In your second step, you made a sign error.
Answer: To see that your approach is not valid, let's make things a bit clearer. If you have the differential equation $(P_1+P_2)dx+(Q_1+Q_2)dy=0$, there's no reason that this should imply $P_1\,dx+Q_1\,dy=0$ and $P_2\,dx+Q_2\,dy=0$. The converse is true, yes, if we take the intersection of those solution sets. So you will get a possible solution, but far from the most general solution. You certainly cannot add respective solutions and expect to get a solution.
Just try a simple case: Can you solve $dx-c\,dy=0$ (for $c\ne 0$) by setting $dx=0$ and $c\,dy=0$? Of course not. (Note, in particular, that you completely lose track of the constant $c$.)


Limit Webform Submission to one per quarter per user

Question: I need to limit one webform submission per quarter. For each quarter, the user can only make one submission. I want to use one webform. Here are some examples of the date period and the due date. How can I achieve the desired outcome with Webforms?

Q1 (Oct. 1, 2019 – Dec. 30, 2019): due by Jan. 15, 2020
Q2 (Jan. 1, 2020 – Mar. 30, 2020): due by Apr. 15, 2020
Q3 (Apr. 1, 2020 – Jun. 30, 2020): due by Jul. 15, 2020
Answer: ok so here is how to do that.. you need to create a module and set Drupal cron to run once a day on your hosting provider
then you need your webform to have email unique then do this ....
<code>/**
 * Implements hook_cron().
 */
function YOURMODULE_cron() {
  $current_month = date('m');
  // Get first month of quarter
  $new_month = (3 * floor(($current_month - 1 ) / 3)) + 1;
  $new_month = substr('0' . $new_month, -2);
  $first_quarter_day_date = date('Y') . '-' . $new_month . '-01';
  $today = date('Y-m-d');
  // So this is the first day of the quarter.
  if ($first_quarter_day_date == $today) {
    // Use cache to make sure this runs once.
    $cid = 'clear_out_webform_date';
    $cached = \Drupal::cache()->get($cid);
    $run = TRUE;
    if (!empty($cached)) {
      if (!empty($cached->data['date'])) {
        if ($cached->data['date'] == $today) {
          $run = FALSE;
        }
      }
    }

    if ($run == TRUE) {
      // change this to your webform id
      $webform_id = 'contact';

      // Export all submissions to file.
      $webform = \Drupal\webform\Entity\Webform::load($webform_id);
      $submission_exporter = \Drupal::service('webform_submission.exporter');
      $export_options = $submission_exporter->getDefaultExportOptions();
      $export_options['delimiter'] = ';';
      $export_options['multiple_delimiter'] = ',';
      $submission_exporter->setWebform($webform);
      $submission_exporter->setExporter($export_options);
      $submission_exporter->generate();
      $file_path = $submission_exporter->getExportFilePath();
      // rename the file to something good.
      $filename = $today.'-export.csv';
      $uri = 'public://' . $filename;
      $fileContent = file_get_contents($file_path);
      $destination = $uri;
      // Save the file to be a file entity.
      $file = file_save_data(
        $fileContent,
        $destination,
        FILE_EXISTS_RENAME
      );
      if (is_object($file)) {
        // Probably email your self a reminder here.
        // ^^ file is now an entity so you can do something like.
        // load a know entity and append the file to a document field.
        // or just download it as in the public file dir.

        // Delete previous quarter submissions.
        $query = \Drupal::entityQuery('webform_submission')
          ->condition('webform_id', $webform_id);
        $result = $query->execute();
        if (!empty($result)) {
          $webform_submission_storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
          $submissions = $webform_submission_storage->loadMultiple($result);
          foreach ($submissions as $submission) {
            $submission->delete();
          }
        }
      }
      // set the cache to today so it wont run again.
      \Drupal::cache()->set($cid, ['date' => $today]);
    }
  }
}
</code>
have fun and good luck.


Auto click em botão com o Tampermonkey

Question: Tenho um botão em um site, que serve para agradecer, gostaria de saber como fazer um script para o Tampermonkey, e que quando a página seja carregada, ele execute essa função de click nesse botão, tendo como base o class thank_user.
Agradecer
Isso só deve ser executado em uma certa url.
Como eu poderia fazer isso?
Answer: Teste isso :
$('.thank_user').click();

A url pode ser configurada direto no tampermonkey na linha @Match.

Auto click em botão com o Tampermonkey


How to add internet privileges to LG web app?

Question: I am developing a web app for TV and I need to add internet privileges for the same. For samsung, in Tizen Studio it's quite straight forward, in IDE in config.xml we can add internet privileges. How do I do the same for LG Web OS IDE? How, where and which file should be created or modified so that my web app has internet privileges while running on tv?
Answer: There is no special permission required in webOS to access internet. For other configurations you can have a look at appinfo.json


Test whether two rank orders differ

Question: I have 20 human subjects doing a task under 2 different difficulty conditions. (So it's a repeated measures design)
Each person has a different performance in each of the task difficulty conditions, so I can rank-order them.
I want to test whether there are differences between the ranking of performaces between conditions.
I'm essentially looking for a test that would give me a significant result when there is no correlation between measures.
EDIT: To clarify, I want to see if the rank order 1 2 3 4 5 6 7 8 9 10 is different from the rank order 1 3 4 5 2 8 9 6 7 10
The Wilcoxon signed-rank test -and other rank-based statistics- won't do, because their null hypothesis (unless I'm mistaken) is that the median of the two ranks is equal. This allows to test overall differences in rank order (when the two conditions are ranked together). What I need is a test that will compare whether the order itself changes.
Can anybody point me to a test that has the null hipothesis that the rank orders are equal?
Thanks
Comment: @ Zach: Thanks. The null of the Wilcoxon signed-rank test  (unless I'm mistaken) is that the median of the two ranks is equal. This allows to test overall differences in rank order (when the two conditions are ranked together).
What I need is a test that will compare whether the order itself changes.  I.e., I want to see if the rank order 1 2 3 4 5 6 7 8 9 10 is different from the rank order 1 3 4 5 2 8 9 6 7 10
Answer: If I understood correctly, you want to test if a condition affects the score order of a population. I guess what you are looking for is the Friedman test or its generalization. On wikipedia : http://en.wikipedia.org/wiki/Friedman_test. I think the example with the judges and the wines looks very similar to your problem.
Answer: I think the wilcoxon signed-rank test will work in this situation.  The null hypothesis is that the 2 sets of ranks are equal.
Comment: The null here (unless I'm mistaken) is that the median of the two ranks is equal. I'm not interested in the mean of the rank for 2 conditions, but in the rank order itself
Comment: @elisa You might want to edit your question then.  What you're asking for is currently not clear to me.
Answer: So if I understand you correctly, is the question you are trying to answer if there is a difference in the way individuals change across the two tests relative to one another? If so your null hypothesis would be the response of individuals is the same. You could test this using the actual values instead of their ranks and running a linear mixed model with random slopes and compare that with the model without the random slope:
<code>lm1<-lme(performance ~ test, random = ~ test | individual, data = data)
lm2<- lme(performance ~ test, random = ~ 1 | individual, data = data)
anova(lm2,lm1)
</code>
If model lm1 gives a significant better fit it shows individuals differed in the way their performance changed across the two tests.

Test whether two rank orders differ