How to scale multidimensional time series data per group

Question: I am dealing with panel data and want to scale it in order to use it for some ML models:

id
year
A
B
C

1
2000
3,539,101
265.152
.0683649

1
2001
3,539.101
2,485.833
.0683649

1
2002
3,539.101
2,939.903
.0688288

1
2003
3,733.545
3,021.591
-.0257413

2
2000
3,960.184
9,418.228
.9781774

2
2001
3,960.184
9,418.228
.4855057

2
2002
3,960.184
9,880.249
.049056

2
2003
3,960.184
1,287.206
.2310434

3
2000
4,724.285
1,287.206
-.0373083

3
2001
4,724.285
1,582.817
.1202868

3
2002
4,724.285
1,279.348
-.1824576

3
2003
4,724.285
1,213.678
-.0513311

However, I'm not sure if I should scale per group (ID) or not. I'm using the following code:
features = df.columns[2:5].tolist()
df[features]=sklearn.preprocessing.minmax_scale(df[features], feature_range=(0, 1), axis=0, copy=False)

Or should I do something like this instead:
from sklearn.preprocessing import minmax_scale()
df[features]=df.groupby("id")[features].transform(lambda x: minmax_scale(x.astype(float)))

Or should I scale by year?
df[features]=df.groupby("year")[features].transform(lambda x: minmax_scale(x.astype(float)))

Answer: It really depends on what you want to do. The scaling in this case affects your data and the information it contains.
To be concrete, let's refer to the following example. It is a stylized version of your data. The dataset I am using is as follows:
    id  year   A            B
0    1  2000   0     0.000000
1    1  2001   1     1.000000
2    1  2002   2     2.000000
3    1  2003   3     3.000000
4    1  2004   4     4.000000
5    1  2005   5     5.000000
6    1  2006   6     6.000000
7    1  2007   7     7.000000
8    1  2008   8     8.000000
9    1  2009   9     9.000000
10   2  2000  10  1100.000000
11   2  2001   9  1050.000000
12   2  2002   8  1033.333333
13   2  2003   7  1025.000000
14   2  2004   6  1020.000000
15   2  2005   5  1016.666667
16   2  2006   4  1014.285714
17   2  2007   3  1012.500000
18   2  2008   2  1011.111111
19   2  2009   1  1010.000000

We have two groups, 10 years for each and two features: A and B.
This data is used to produce the following graph.

On the x-axis you read the years; on the y-axis you read the values of either feature A (first column) or feature B (second column). Colors represent the two IDs (violet is ID=1; yellow is ID=2).
The first row is the original data, not scaled:

feature A is increasing for ID=1 and it is decreasing for ID=2; they show the same scale.

feature B shows different dynamics for the two groups; still it is increasing for group 1 and decreasing for group 2. They are on different scales.

Consider row by row:

the first row is a simple plot of the data. You can reconcile it with the description above. Note that since B is on different scales for the two groups, you can not really notice the groups' dynamics across years.

the second row is a simple scaling obtained with your first proposal: df[features]=sklearn.preprocessing.minmax_scale(df[features], feature_range=(0, 1), axis=0, copy=False). Here, you can notice that the graphs are identical to those in the first rows. The only noticeable difference is the scale of the y-axis being in 0-1. This transformation preserves the original information both with respect to years and to groups.

the third row is the group-scaling (your second proposal: df[features]=df.groupby("id")[features].transform(lambda x: minmax_scale(x.astype(float)))). Here things change a lot. Basically, this transformation removes group specific scales, preserving year dynamics. Here you lose information relative to group different scales which may be relevant. E.g. is it important that group 2 has a value of the B feature that is 1000 times that of group 1?

the last one is obtained scaling by year (df[features]=df.groupby("year")[features].transform(lambda x: minmax_scale(x.astype(float)))). Here you lose information on year dynamics, but keep information of different scales. E.g. you are not longer able to see increasing/decreasing dynamics across years, but you see that the value of the A feature is bigger for group 1 till some point than the reverse is true. For feature B, instead, you simply loose increasing and decreasing patterns.

Which one is best?
This depends on the data, prior knowledge and goal of the analysis. In general, if you want simply to take all the features to comparable magnitude I would go for the "Vanilla feature scaling", which preserve basic information, but makes so that all the features are in the same range. This is also what ML algorithms require, typically.
However, you may be interested in yearly dynamics. In this case, scaling within groups could make the problem easier to solve.
Or it may happen that you know that your data is such that measurements across years use different scales, but there is no dynamic component you want to capture; then, scale within years.
Keep in mind that within-years and within-groups scaling affect the final information you are using.
One last consideration is: if you are unsure which one you should use and you are dealing with a purely prediction problem where you can check to goodness of final results, you can just make this decision part of the algorithm's tuning: try all of them and select the best one via cross-validation, for example.
Code used for the example
from sklearn.preprocessing import minmax_scale
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

A1 = np.arange(10)
B1 = np.arange(10)
ID1 = np.repeat(1, 10)
YEAR1 = np.arange(2000, 2010)

A2 = 10 - np.arange(10)
B2 = 1000 + 1 / np.arange(0.01, 0.11, 0.01)
ID2 = np.repeat(2, 10)
YEAR2 = np.arange(2000, 2010)

A = np.concatenate([A1, A2])
B = np.concatenate([B1, B2])
ID = np.concatenate([ID1, ID2])
YEAR = np.concatenate([YEAR1, YEAR2])

dat = pd.DataFrame({"id": ID, "year": YEAR, "A": A, "B": B})
features = ["A", "B"]

fig, ax = plt.subplots(nrows=4, ncols=2, sharex=True, figsize=(12, 12))

# Plain fig
ax[0, 0].scatter(dat.year, dat.A, c=dat.id)
ax[0, 1].scatter(dat.year, dat.B, c=dat.id)
ax[0, 0].set_title("No scale (A)")
ax[0, 1].set_title("No scale (B)")

# Scale entire feature
df = dat.copy()
df[features] = minmax_scale(df[features], feature_range=(0, 1), axis=0, copy=False)
ax[1, 0].scatter(df.year, df.A, c=df.id)
ax[1, 1].scatter(df.year, df.B, c=df.id)
ax[1, 0].set_title("Vanilla feature scaling (A)")
ax[1, 1].set_title("Vanilla feature scaling (B)")

# Scale by group (id)
df = dat.copy()
df[features] = df.groupby("id")[features].transform(
    lambda x: minmax_scale(x.astype(float))
)
ax[2, 0].scatter(df.year, df.A, c=df.id)
ax[2, 1].scatter(df.year, df.B, c=df.id)
ax[2, 0].set_title("By-ID feature scaling (A)")
ax[2, 1].set_title("By-ID feature scaling (B)")

# Scale by year
df = dat.copy()
df[features] = df.groupby("year")[features].transform(
    lambda x: minmax_scale(x.astype(float))
)
ax[3, 0].scatter(df.year, df.A, c=df.id)
ax[3, 1].scatter(df.year, df.B, c=df.id)
ax[3, 0].set_title("By-YEAR feature scaling (A)")
ax[3, 1].set_title("By-YEAR feature scaling (B)")

plt.show()


Enumeration in Codeigniter: Undefined property: ProjectStatus_Enum::$Open

Question: I created a class in Library Folder.
class ProjectStatus_Enum {

    const Open          =   1;
    const InProgress    =   2;
}

and below is the code in Controller.
class ProjectController extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library("ProjectStatus_Enum");
        echo $this->projectstatus_enum->Open;
    }
}

when this line executes...echo $this->projectstatus_enum->Open; I am getting below error..

Undefined property: ProjectStatus_Enum::$Open
Answer: Sample class in Library Folder.
class ProjectStatus_Enum {

    const Open          =   1;
    const InProgress    =   2;
}

Class variable will not be accessible directly.
class ProjectController extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library("ProjectStatus_Enum");
        $ProjectStatus = $this->projectstatus_enum;//=> Direct variable call will not work
        echo $ProjectStatus::Open;
    }
}

Answer: You have to the :: for constants (const) and -> for class properties. So in your case use
echo $this->projectstatus_enum::Open;

or
echo ProjectStatus_Enum::Open

Comment: You are missing few things. I just got the code working. Please check my post and Many thanks for the help.
Comment: Ah, yes it was not meant as a fully working example, more a push in the right direction, good that you got it working!


Clusters in the form of parallel rectangles

Question: I have a dataset with 3 features and a pile of quite flat rectangles. My task is perform clustering on this dataset using any clustering method but not hierarchical. I have scaled all features and do kmeans with 6 clusters and got bad result. Tried also to do MDS but I havent enough memory. PCA doesnt work too. It seems that I can use only one feature to separate the data. But I wonder if there is some clustering method which can handle this data properly?
Data

K-means results
Comment: Cluster on the vertical coordinate only.
Answer: Use the one variable only that matters to your problem.
Blindly using all features and scaling them is not the way to go.
Here, I would neither use clustering, nor scaling.
Probably each of the clusters corresponds to a well defined z value. Just use the z value with some rounding if necessary. At most, use some kernel density estimation on Z to find peaks and gaps.
Comment: thx for answer. Actually it is pretty clear to do it in the way you suggested. But what if we had 3 parallel 3 - dimensional subspaces in 4 - dimensional space or even higher dimension? In that example we cant simply plot data, and it would be hard to find good projection. Are there some common techniques for such problems?
Comment: If they are parallel in any way, project the data to the orthogonal vector.
Comment: But there are subspace and **correlation clustering** techniques that even work when you have hyperplanes that are *not* parallel.


Explanation of notation of dot above plus $\dotplus$ for matrices

Question: I am studying Y Kubota's article  "A note on holomorphic imbeddings of the classical Cartan domanis into the unit ball". There is a phrase in the article

Considering a skew-symmetric matrix $Z$ of order $q$ such that $$Z=\left(\begin{array}{cc} 0 & a_1\\
-a_1&0 \end{array}\right)\dotplus\ldots\dotplus \left(\begin{array}{cc} 0 & a_m\\
-a_m&0 \end{array}\right),$$ where q=2m and $a_1,\ldots, a_m\in{\mathbb{C}}$.

Clearly, the expressions on the right are $2\times 2$ matrices and the expression on left is a matrix of order $2m$, but what does the symbol $\dotplus$ mean.
I found similar notation here but it is for product of algebras. Can someone help me understand what it means, if it is a standard notation or what it means.
Comment: I would guess it means $$
I_1  \dotplus  \cdots  \dotplus I_n  = \left( {\begin{array}{*{20}c}
   {I_1 } &  \cdots  & 0  \\
    \vdots  &  \ddots  &  \vdots   \\
   0 &  \cdots  & {I_n }  \\
\end{array}} \right).
$$
Comment: Does he not define the symbol somewhere? I wouldn't say that this is standard notation.
Comment: It is not mentioned what it means, it is used without stating what it means, that made me thought it is standard notation.
Answer: The 1980 article "A Note on Holomorphic Imbeddings of the Classical Cartan Domains into the Unit Ball" by Yoshihisa Kubota uses $\dotplus$ and cites the 1944 article "On the Theory of Automorphic Functions of a Matrix Variable I-Geometrical Basis" by Loo-Keng Hua, who writes in Theorem 7:

Let $Z$ be a non-singular skew-symmetric matrix; then we have a unitary matrix $U$ such that $$UZU'=\begin{pmatrix}0&d_1\\-d_1&0\end{pmatrix}\dotplus\cdots\dotplus\begin{pmatrix}0&d_{n/2}\\-d_{n/2}&0\end{pmatrix}\text{,}$$ where $d_1^2,d_1^2,\cdots,d_{n/2}^2,d_{n/2}^2$ are the characteristic roots of $-Z\overline{Z}$.

For comparison, the spectral theory section of the English Wikipedia page for skew-symmetric matrix says

$$\Sigma = \begin{bmatrix}
  \begin{matrix}0 & \lambda_1 \\ -\lambda_1 & 0\end{matrix} &  0 & \cdots & 0 \\
  0 & \begin{matrix}0 & \lambda_2 \\ -\lambda_2 & 0\end{matrix} & & 0 \\
  \vdots & & \ddots & \vdots \\
  0 & 0 & \cdots & \begin{matrix}0 & \lambda_r\\ -\lambda_r & 0\end{matrix} \\
    & & & & \begin{matrix}0 \\ & \ddots \\ & & 0 \end{matrix}
\end{bmatrix}$$...every complex skew-symmetric matrix can be written in the form $A=U\Sigma U^{\mathrm{T}}$ where $U$ is unitary and $\Sigma$ has the block-diagonal form given above with $\lambda_k$ still real positive-definite.

With this context, I am fairly confident that $\dotplus$ as used by Kubota denotes the direct sum of matrices, which produces a block form as exemplified above and guessed by gary in a comment.


Why is $\det(\vec{A},\vec{B}) = |\vec{A} \times \vec{B}|$?

Question: In the multivariable calculus class the teacher showed us the formula of the cross product
$$ \vec{A} \times \vec{B} =\begin{vmatrix}\hat{\imath}& \hat{\jmath}& \hat{k} \\
a_1 & a_2 & a_3 \\b_1 & b_2 & b_3 \end{vmatrix}$$
And formula for determinant in two dimensions which can calculate the area of parallelogram in two dimensions by
$$\det(\vec{A},\vec{B}) =\begin{vmatrix}a_1 & a_2 \\b_1 & b_2 \\\end{vmatrix}$$
Then teacher talked about the area of a parallelogram also being equal to the length of $\vec{A} \times \vec{B}$, that is $|\vec{A} \times \vec{B}|$, but gave no proof. I wanted to check this, so I used  $a_3=0,b_3=0$ just to have the $3 \times 3$ in the form that could be compared to $\det(\vec{A},\vec{B})$ form. When I expand the calculation, I do end up with  $|\hat{k}(a_1b_2 - a_2b_1)|$, and that equals to $(a_1b_2 - a_2b_1)$ The two forms are equal. Is this reasoning correct?
Comment: This is not an answer. Here's a wikipedia link for the formula: http://en.wikipedia.org/wiki/Determinant#2-by-2_matrices
Comment: I can't resist pointing to Dick Palais's [question on MO](http://mathoverflow.net/questions/54122/does-this-formula-have-a-rigorous-meaning-or-is-it-merely-formal) (even if it is only tangentially related).
Comment: The most common way to find the cross product is to expand a determinant by minors where the determinant has rows of the unit vectors in the x, y, z directions. And i am guessing you know about determinants here. just expland both sides even in case of 3x3 determinants to see the proof yourself
Comment: Are you assuming that the area of the parallelogram is equal to the length of the cross-product? (If you point to the formula $|\mathbf a \times \mathbf b| = |\mathbf a| \cdot |\mathbf b| \cdot \sin \theta$ to prove this, then you still need to justify that this formula follows from the coordinate-based definition of cross-product you are working with.)
Comment: Also, note that the $\det(A,B)$ gives the signed area of the parallelogram. For the "real" area, you need to put an absolute value. (I see that you aren't that consistent with your use of absolute values.)
Comment: @Srivatsan, the teacher derived $det(\vec{A},\vec{B})$ from $|a||b|sin\theta$, he didnt make the determinant xproduct connection. Maybe I could make that connection, but I did not want to veer to much from the class material.
Comment: [Related](http://math.stackexchange.com/questions/61526)...
Answer: This post here gives an excellent explanation regarding the similarities between cross products and determinants
Answer: Anton and Rorres in their Elem Linear Algebra with Applications 9th Ed seem to use something similar to what I described in my question. Their wording is "the key to the proof is to use [cross prod theorem], however that theorem applies to vectors in 3-space whereas our vectors are in 2-space. To circumvent this "dimension problem" we shall view u and v as vectors in xy-plane of an xyz-coordinate system (figure below) in which case the vectors are described as $u = (u_1,u_2,0)$ and $v = (v_1,v_2,0)$".


Which is more common when spinning a jogwheel clockwise, going forward or backward in time?

Question: On a DJ controller, which is the more common default setting for jogwheels, say, on CDJs at clubs? Clockwise to move forward in time (toward the end of the song), or counterclockwise?
I learned clockwise as forward in time (like a clock), but I see the logic of the other way too: clockwise scrolls the waveform "to the right". And I'm starting to suspect the latter is more common, because I'm the only one saying, "Hey, this is backwards," whenever I touch someone else's controller :p
I know it's easily invertible in software, sometimes in hardware, and also I know there isn't a "right" way. But I just want to practice whichever way's more prevalent.
Comment: Anyone who has worked with vinyl would surely associate clockwise with going forward? That's the way the record spins.
Answer: @YourUncleBob is of course correct. Will accept the answer if posted.
Realized though that the reason I felt like my colleagues' settings were "backwards" was something totally unrelated, and kind of interesting and a good lesson for beginner DJs.
tl;dr - Yet another reason not to rely on visual cues!
It turns out I didn't have a different setting than my colleagues. It was a combination of relying on software visual cues, and using unfamiliar software, that made it seem so.
Basically, when I was learning on Native Instrument's Traktor, I found these training wheels called a "phase meter":

This orange bar leading to the right means, "The track you're mixing in is too far ahead of the other one. Slow it down."* Eventually I turned it off, but it managed to cement a bad association in me: "Right means counterclockwise."
* And if it seems like the orange bar is in any way related to the distance between the red and white lines on the beatgrid, it's not! It's relative to the beatgrid of the other deck.
Now, I work for a music company, so we have CDJs, DDJs, and Denons, and whenever I jumped on after a colleague, the jogwheels always seemed "backwards." But actually, it wasn't that. It was because at work we always used Algoriddim's djay Pro. Since I wasn't familiar with djay Pro and kind of nervous, naturally I looked for visual helpers to compensate. But that's where djay Pro's layout worked opposite my "Right means counterclockwise" instinct:

Above, I'm mixing in the bottom deck. It looks like it's to the right of the playing top deck. So my instinct was to go counterclockwise on the bottom deck. But of course, that was slowing it down when actually it needs to catch up to the top deck. Noticing it immediately, I'd go clockwise instead and wonder, "Why's it backwards?"
Don't rely on visual cues, even if you're nervous! Visual cues on unfamiliar software may work backwards to what your brain is used to.


How can I automatically restart a timer when time runs out?

Question: I am trying to have a countdown start and once it hits 0 automatically restart the timer back at 10. I feel like this should be pretty simple but I am having issues.
@IBOutlet weak var timerLabel: UILabel!

var defaultTime = 10
var timer = Timer()

func startTimerFunc() {
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)
}

@IBAction func startTimerBtn(_ sender: UIButton) {
    startTimerFunc()
}

func resetTimer() {
    timer.invalidate()
    defaultTime = 0
}

@objc func action() {

    defaultTime -= 1
    timerLabel.text = String(defaultTime)

    if timerLabel.text == "0" {
        resetTimer()
        startTimerFunc()
    }
}

Answer: The timer keeps running until you invalidate it, Here you just need to reset the defaultTime:
@objc func action() {
    if defaultTime == 0 {
        defaultTime = 10
    }
    defaultTime -= 1
    timerLabel.text = String(defaultTime)
}


Train Tensorflow Obejct Detection without bounding boxes annotations

Question: I managed to retrain the object detection module on my own dataset by adhering it to the PASCAL VOC format shown below.
This format is bounding box oriented and peeking into their TFRecords creation scripts, it does expect a good number of these groundtruth values to generate corresponding TFRecords.
The problem with bounding boxes, is that it gives you approximations and annotating rotated images can be rather challenging.
After looking around, I came across labelme which allows you to perform shape (point to point) annotations, as well instead of just bounding box. Below is a short version of the produced annotation along with the resulting image consisting of the resulting shape.
My questions are:

Concentrating on the contents of <polygon></polygon>, does the Object Detection API support point to point annotations?
If yes to 1, how do I go about creating the TFRecords for it? What other changes need to be made to accommodate this?

Pascal VOC Format
<annotation verified="no">
  <folder>VOC2012</folder>
  <filename>pic.jpg</filename>
  <source>
    <database>Unknown</database>
  </source>
  <size>
    <width>214</width>
    <height>300</height>
    <depth>3</depth>
  </size>
  <segmented>0</segmented>
  <object>
    <name>sample</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndbox>
      <xmin>32</xmin>
      <ymin>37</ymin>
      <xmax>180</xmax>
      <ymax>268</ymax>
    </bndbox>
  </object>
</annotation>

Snapshot of point-to-point annotation
Here's the full annotation file and the corresponding image
<annotation>
    <filename>ipad.jpg</filename>
    <folder>sample</folder>
    <source>
    <submittedBy>username</submittedBy>
    </source>
    <imagesize>
        <nrows>450</nrows>
        <ncols>800</ncols>
    </imagesize>
    <object>
        <name>ipad</name>
        <deleted>0</deleted><verified>0</verified><occluded>no</occluded>
        <attributes></attributes>
        <parts>
            <hasparts></hasparts>
            <ispartof></ispartof>
        </parts>
        <date>12-Jul-2017 19:20:22</date><id>0</id>
        <polygon>
            <username>anonymous</username>
            <pt><x>40</x><y>76</y></pt>
            <pt><x>435</x><y>11</y></pt>
            <pt><x>472</x><y>311</y></pt>
            <pt><x>94</x><y>418</y></pt>
        </polygon>
    </object>
    <object>
        <name>screen</name>
        <deleted>0</deleted>
        <verified>0</verified>
        <occluded>no</occluded>
        <attributes></attributes>
        <parts>
            <hasparts></hasparts>
            <ispartof></ispartof>
        </parts>
        <date>12-Jul-2017 19:20:48</date><id>1</id>
        <polygon>
            <username>anonymous</username>
            <pt><x>75</x><y>89</y></pt>
            <pt><x>118</x><y>397</y></pt>
            <pt><x>447</x><y>308</y></pt>
            <pt><x>421</x><y>30</y></pt>
        </polygon>
    </object>
</annotation>

Answer: The Tensorflow Object Detection API only performs Bounding Box annoations.
Comment: The current state only allows bounding boxes. When you have polygons or other shapes you must transfer them to boundings boxes before training. Facebook's [Multipathnet](https://github.com/facebookresearch/multipathnet) can be trained on polygons.
Comment: Thanks for the response. How would you recommend handling rotations then? And uh is point-to-point annotation training even feasible for object localization in ML?
Comment: How to convert polygons to bounding box format? Is there any known way to do it? Or maybe writing script to get the extreme corners of the polygon.
Comment: as Derek mentioned, the api use to only perform bounding box annotations, it now supports masks though using MASK RCNN, https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/instance_segmentation.md
Comment: This answer is wrong. You can pass images without bboxes to tf od by passing an empty list to the tfrecord producer


Is it possible to keep in container static array?

Question: I mean not keep pointer on array
std::vector<int*> vector;

but
std::vector<int[]> vector;

The problem is to keep such array in hash_map in order to compare not pointers when Insert there but when I try like this
std::hash_map<std::vector<BYTE>,std::string> xxx

I've got an error.
Comment: I have a hard time understanding your question, could you please rephrase it? Also note that a `vector` is exactly the same thing as a `vector`. Did you maybe mean a `vector` (where N is a compile-time constant)?
Comment: The hash map in C++ is called `unordered_map`, and this works fine for me: `std::unordered_map, std::string>`
Comment: `std::unordered_map, std::string>` is a possibility too, since the array is presumably statically-sized.
Comment: `vector v; v.push_back({1, 2, 3});` This even makes GCC segfault :)
Comment: even with unordered_map I've got an error `cannot convert from 'const std::vector<_Ty>' to 'size_t'`
Comment: @jrok: I don't see how that would even compile. In C++03 it's invalid syntax and in C++11 it shouldn't type check because you can't assign an initializer list (or anything else for that matter) to an array.
Comment: and if define `std::pair pair` then I've got an error on `pair.first=t` where `BYTE t[16]` --> `left operand must be l-value`
Comment: @Артём : That means it can't find a suitable hash function for `vector`, which implies a deficiency in your compiler's standard library. [Boost.Unordered](http://www.boost.org/libs/unordered/) comes with the correct specializations, however, so you may consider using that instead if you don't want to write the hash function yourself.
Comment: so in order to make it possible to make it works like I want I need to download BOOST library?
Comment: Not necessarily; you either need 1) a not-deficient standard library implementation, or 2) to write a hash functor for `std::vector<>` yourself, or 3) use the existing hash functor from Boost.
Comment: the problem was not in `hash_map` but in `std::pair`. It can't keep vector in it
Comment: Huh? [Of course you can](http://ideone.com/6I8hQ), why would you think otherwise..?
Comment: Yes...It is error in `table.insert(pair)`..
Comment: What type is `table`? What type is `pair`?
Answer: You cant do std::vector<int[]> vector. You have to specify the size of the array for it to compile like this std::vector<int[5]> vector.
However, this is a bad idea because you can`t assign arrays to other arrays, etc. and you will get all kinds of errors when you try to use vector.
Instead, use vector<vector<int>> vector or in C++11 use vector<std::array<int, 5>> vector.
Also, I don't know what implementation of hash_map you are using so I dont know if the above solutions will work in your case. (Also, C++11 has unordered_map, so that might be preferable)
Comment: What's C++11x? I know C++0x and C++11 :)
Comment: so put my vector into another vector?
Comment: @Артём: Yes, that might work, but I don't know if the key for hash_map can be a container or not.


Dificuldade para relacionar os municípios do SNIS com os do IBGE

Question: Estou tentando querendo montar uma base de dados que inclua a população atendida pelos serviço de saneamento e o PIB per per capita de cada município. O primeiro dado eu já consegui acessar pelo SNIS. O segunda dado fica disponível no IBGE e também já tenho ele em mãos.
Todavia, o sistema do IBGE não usa o código dos municípios. Já os dados do SNIS tem uma coluna com o código dos municípios, mas não é o mesmo número de outras bases de dados - contém apenas 6 dígitos. Me parece que a única alternativa seria agregar os dados pelo nome dos municípios, o que eu não sei fazer e poderia gerar infinitas incongruências.
Se alguém puder me ajudar, ficaria muito agradecido.
Comment: Pergunta, não seria melhor você atribuir um `id` para cada município, juntamente com o nome do mesmo, em uma tabela pai, e depois duas tabelas filhas, uma para atribuir o PIB ao id do município respectivo, e outra tabela para lançar a população atendida pelo saneamento básico, também atribuindo ao id do município?
Answer: Há vários municípios brasileiros que têm o mesmo nome, pensei que eram poucos, mas ao conferir a lista de todos eles, vi que são muitos os casos. Acho que o pior caso é Bom Jesus, nome de um município do Piauí, um do Rio Grande do Norte, um da Paraíba, um de Santa Catarina e um do Rio Grande do Sul. Há outros nomes que só divergem na acentuação, tal como Arapuá (MG) e Arapuã (PR).
Entretanto, um estado não pode ter dois municípios com um mesmo nome e nem dois municípios que só diferem na acentuação. Assim sendo se você puder obter o nome ou a sigla do estado de cada município e concatenar ao nome, cada nome de município será único.
Pode ser que haja diferenças referentes a maiúsculas e minúsculas, acentos e apóstrofos. Por exemplo, uma base pode te dar "Olho d'Água das Cunhãs (MA)" e a outra "OLHO DAGUA DAS CUNHAS (MA)". Neste caso você converte tudo para maiúsculas, tira os acentos e tira os apóstrofos para unificar o nome.
Assim sendo, se você puder obter das bases o nome, sigla ou código do estado junto com o nome do município, então basta relacionar-se os nomes dos municípios junto com os estados para você fazer esse mapeamento [solução 1].
Se você não consegue obter o nome, sigla ou código do estado diretamente, pode ser que ele seja parte do código do município. Por exemplo, os dois primeiros dígitos do código do município utilizado pelo IBGE nas bases que conheço são os mesmos dígitos do estado. Não sei como são os dados do SNIS que você está utilizando, mas acho que seja apenas o código do município do IBGE sem o último dígito, pois pelo menos é isso que eu encontrei ao procurar alguns arquivos para download no site do SNIS.
Desprezar-se o último dígito do código do IBGE é seguro, uma vez que este é apenas um dígito verificador. Pelo que verifiquei nas planilhas que achei no site do SNIS, é exatamente esse o caso. Por exemplo, o município de Americana tem o código do IBGE 3501608, e na planilha que achei em algum lugar do site do SNIS consta o código 350160. Nos dois casos, 35 é o código do estado de São Paulo. Neste caso, basta desconsiderar o último dígito do código do IBGE e os códigos vão coincidir. [solução 2]
Caso o seu código seja alguma coisa que não tenha similaridade com o código do IBGE e você não tem a informação do estado disponível de forma direta, então mesmo assim você deve tentar achar um padrão que a partir do código você descubra o estado. Se esse padrão não existir, então você dança, pois neste caso, não será capaz de saber quem é quem no caso de Bom Jesus, por exemplo. Entretanto, acho improvável que a base do SNIS que você está usando relacione o município sem informar absolutamente nada do estado em questão.
Answer: Amigo ontem tem essa coisa é no site sidra. Eh bem dificil achar o link dos dowloads de base de dados. Mas verifique se essa t3m tudo que voce precisa
Download sidra atualizado municipios


Setting delay time in javascript code

Question: I have the following code.
var el = document.getElementsByClassName('_4jy0 _4jy3 _517h _51sy _42ft');
    var objDiv = document.querySelector("._5tee .uiScrollableAreaWrap");
    var n = -1;
    while (true)
    {
        if (n == el.length)
        {
            break;
        }
        else
        {
            objDiv.scrollTop = objDiv.scrollHeight;
        }
         n = el.length;
         var el = document.getElementsByClassName('_4jy0 _4jy3 _517h _51sy _42ft');
    }

This code is about one div box with a scroll bar. When the code is being executed it goes down to the last item and then the box loads more items but the code stops cause the box needs some ms to load the other items.
So i want you to help me make one delay time with setTimeout or setInterval
and when it has no more items to load, the while loop will stop.
What would you suggest to use, setTimeout or setInterval???
And what should the code be like???
i only want the scroll down( objDiv.scrollTop = objDiv.scrollHeight; ) to run every 3ms (for example)
could you help me with that??
Comment: @torazaburo - yes, a space-delimited list of classes to search for is supporeted by getElementsByClassName: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
Comment: I would suggest putting a check between `if (n == el.length) { //checlk here; break; }`
Comment: It can't be done without a function, because Javascript will block until the script executes and the page will not get updated untill then. setTimeout or setInterval is the way to go, and they both need a function as the first argument. Another possible way is to reload the script, but I won't recomment doing it like that. If you want a delay in your while loop, it is possible but the page will not get updated. For that just use the Date object and compare to a previous date plus the millisecs delay.
Comment: @JuanGarcia could you help me with the function then? give me and example
Comment: @cyber_rookie to check what?? the timeout??
Comment: @PeterLillevold thanks for the comment! could anyone now suggest what can i do so the code to have a delay?!?!? :)
Comment: @SteliosM. - what have you tried so far? Please show due effort first, and ask questions when any issue arises, lest your question will be closed. Good to read before asking in SO: http://stackoverflow.com/help/how-to-ask
Comment: @PeterLillevold ok i will update in some hours with anything that i have tried until now!
Answer: function hereYouGo() {
var el = document.getElementsByClassName('_4jy0 _4jy3 _517h _51sy _42ft');
    var objDiv = document.querySelector("._5tee .uiScrollableAreaWrap");
    var n = -1;
    while (true)
    {
        if (n == el.length)
        {
            break;
        }
        else
        {
            objDiv.scrollTop = objDiv.scrollHeight;
        }
         n = el.length;
         var el = document.getElementsByClassName('_4jy0 _4jy3 _517h _51sy _42ft');
    }

    }
var myTimer = setInterval(hereYouGo, 1000);

try this code here im pretty sure that it will work.
Comment: thanks man it seems that it didnt have to be with "no function" at all :)

Setting delay time in javascript code


Exercice XV num 9 - Calculus Made Easy

Question: Divide π into 3 parts such that the product of their sines may be a maximum or minimum.
What is the most intuitive way to solve it?
Comment: Do you mean:
$$
\text{Maximize} \qquad \sin{A}\sin{B}\sin{C} \qquad \text{subject to}~A+B+C=\pi
$$
?
Comment: Actually, it should probably be $A, B, C > 0$ since equality would allow one of them to equal $\pi$ and the others to equal $0$: an obvious minimum for $\sin A\cdot\sin B\cdot\sin C$.
Comment: @MattiP. I guess you'd also need $A,B,C \geq 0,$
Comment: @MattiP. I quoted the exercise. Your assumption looks about right.
Answer: Let $f:[0,\pi]\times[0,\pi]\to\mathbb{R}$, $f(x,y)=\sin x \sin y\sin(\pi-x-y)$.
You have $\frac{\partial f}{\partial x} =\cos x\sin y\sin(\pi-x-y)-\sin x \sin y \cos(\pi-x-y)$ and $\frac{\partial f}{\partial y} =\sin x\cos y\sin(\pi-x-y)-\sin x \sin y \cos(\pi-x-y)$. Set both equal to zero and you find, after dividing by $\cos x $ and $ \cos(\pi-x-y)$ the first and by $\cos y$ and $\cos(\pi-x-y)$ the second, $\tan x=\tan y=\tan(\pi-x-y)$.
Hence $x=y=\pi/3$.
Answer: I think it works best with Lagrange multipliers.  Let $f(A,B,C) =\sin(A)\sin(B)\sin(C)$ and let $g(A,B,C) = A+B+C = \pi$ be the constraint.  The system of equations $\nabla f =\lambda \nabla g$ is:
$$\cos(A)\sin(B)\sin(C) = \lambda$$
$$\sin(A)\cos(B)\sin(C) = \lambda$$
$$\sin(A)\sin(B)\cos(C) =\lambda$$
Subtract the first two equations and cancel $\sin(C)$ to get
$$\cos(A)\sin(B) - \sin(A)\cos(B) = 0$$
or $$\sin(A-B) = 0.$$
Assuming $0<A,B<\pi$ we must have $A=B$.
Similarly we have $B=C$.  So the solution is $3A=\pi$ or $A=B=C=\pi/3$.

Exercice XV num 9 - Calculus Made Easy


Power calculation for non-normal distribution

Question: I'm trying to do a power analysis for a research study aimed at characterizing between group differences in depression for 2 different populations. As I understand it, depression scores are a non-normal distribution in the general population.
For a P value <0.05, specified power of 0.8 and a difference of PHQ-9 scores between populations of 7 (scale is 0-27), what is the best/proper power analysis equation?
Are there any other variables I need given that PHQ-9 scores are a non-normal distribution (right skewed)?
Thank you so much!!
Comment: For PHQ-9 a 7-point difference is approximately that between the major depression class and the other depression class, and also between the other depression class and the no depression class. Think carefully if that's a reasonable magnitude of difference to look for between your populations. The distribution in the general population probably isn't relevant to your study.
Comment: Looking for a seven point difference on a 28 point scale seems rather optimistic. If you are not happy with assuming normality then you can proceed using simulation.
Comment: @mdewey do you think it is fair to assume normality given that this is a very simple project at the medical student level? I need to do a simple power calculation and I need to tell my PI how large my sub-study population should be.
Answer: From J Gen Intern Med 2001 Sep; 16(9): 606–613:

The mean PHQ-9 score was 17.1 (SD, 6.1) in the 41 patients diagnosed by the [medical health professional] as having major depression; 10.4 (SD, 5.4) in the 65 patients diagnosed as other depressive disorder; and 3.3 (SD, 3.8) in the 474 patients with no depressive disorder. The vast majority of patients (93%) with no depressive disorder had a PHQ-9 score less than 10, while most patients (88%) with major depression had scores of 10 or greater.

The distribution of PHQ-9 scores within a population thus depends on the prevalence of depressive-disorder classes in the population. As part of the power calculation you must specify values for some combination of depression-class prevalence and within-class distributions of scores. This would seem to be best handled by simulation.
It would be good if you had such estimates of score distributions from the particular populations with which you will be working, for example from a pilot study. If you do, then Frank Harrell's suggestion in another answer to base your power calculations and analysis on a two-sample Wilcoxon test would be a good way to proceed.
If you don't have your own information, the above quote gives you a starting point for those within-class distributions of scores to combine with your estimates of depression-class prevalence.
Unfortunately, within-disorder-class value distributions in the above quote seem to be non-normal. Thus you need to take some care in simulation. In particular, the no-disorder score SD of 3.8 is greater than the mean of 3.3, so a normal distribution would imply many impossible negative values.
You might generate a set of non-negative integer values for that class from a negative binomial distribution. For the R parameterization of the NegBinomial distribution, you could estimate a corresponding prob parameter from the ratio of the mean (3.3) to the standard deviation squared (3.8^2), or 0.2285319. The corresponding size parameter would be mean * prob/(1-prob), or 0.9775586. Then generate a random sample of N scores for that class with rnbinom(N, 0.9775586, 0.2285319). Those parameter estimates agree well with the above value of 93% of such patients having scores below 10.
The problem with a normal-distribution assumption about scores for that class of patients is illustrated here:

The solid line shows a normal distribution with mean 3.3 and SD 3.8, while the points show the probability masses for the corresponding negative binomial distribution from the prior paragraph.
You could take the same approach to simulating integer values for the two depression-disorder classes. A quick check suggests about 5% of scores above 27 with the above approach to negative-binomial simulation for the major-depression class. Thus negative binomial simulation for that class might better be based on counts below 27 (mean = 27-17.1 = 9.9, SD = 6.1), then subtracting the simulations from 27. That agrees well with 88% of those patients having scores of 10 or greater. Among all 3 classes you will get a very small fraction of simulations outside the range of 0 - 27. Those could be eliminated or just set to the corresponding limits.
Finally, as the scores aren't going to be normally distributed overall within each population, you shouldn't count on power calculations from the relatively simple formulas that apply to t-tests, which aren't appropriate here anyway. You should choose an analysis method suited to your type of integer-scale data. I made some suggestions here. Then run multiple analyses on your simulated data to estimate power as a function of sample size. You would also want to repeat that power analysis over ranges of assumptions about depression-class prevalence and score distributions to see how sensitive your power estimates are to those assumptions.
In response to comment:
Whether normal approximations will be adequate and, if so, what to assume about standard deviations depend on what you expect the compositions of your populations to be. Let's call them PopA and PopB.
Based on the cited literature, say that those with major depressive disorder (MDD) have mean scores of 17 with SD of 6, those with other depressive disorders (ODD) have mean scores of 10 with SD of 6, and those not depressed (ND) have mean scores of 3 with SD of 4. Then there is a difference of 7 between ND and ODD and a difference of 7 between ODD and MDD, consistent with the question.
If PopA is all MDD and PopB is all ODD, then a normality assumption for each group with SD of 6 in each group might do fine.
If PopA is all ND and PopB is all ODD, then you also have a mean difference of 7 between the PopA and PopB, but the normality assumption about the PopA/ND group is questionable.
If PopA is 50:50 ND:ODD and PopB is 50:50 ODD:MDD, then you still have a mean difference of 7 between PopA and PopB. But the within-Pop SD will be much larger than the 4-6 for homogeneous populations, and the within-Pop assumption of normality is tenuous.
That's why I said at the beginning of the answer:

The distribution of PHQ-9 scores within a population thus depends on the prevalence of depressive-disorder classes in the population. As part of the power calculation you must specify values for some combination of depression-class prevalence and within-class distributions of scores.
Comment: @Eoin I recognize that, but I think the main point here is to get the OP (and the OP's PI) to think explicitly about their assumptions about the populations they are trying to compare. If they had one population independently assessed as MDD and another as ODD, then assuming within-population normal distributions of PHQ-9 scores might be good enough. Otherwise...
Comment: Thanks for this detailed response -- this is obviously the ideal way to go for the most appropriate power calculation given my study. Unfortunately, I'm only a med student and much of the higher level statistics is over my head. It's also worth noting that my project is relatively simple, and I just need to generate a power calculation for my PI by Friday. In short -- is there a way to make some simple assumptions (i.e. assume normality, assume depression prevalence in my populations) in order to produce a power calculation, while noting that it was made with assumptions that may not hold?
Comment: @StevenPinkler the main non-normality problem comes from the non-depressed group, whose values are bunched up close to 0. If you are trying to compare a major-depression group against an "other depressive" group (difference about 7 units, SD about 6 within in each group, means of both groups pretty far from the limits of the scale) then you might get by with a normality assumption. But if there are many non-depressed in your populations I would worry. It all depends on just what you expect your 2 populations to contain.
Comment: @StevenPinkler I added to the answer to illustrate some scenarios. Whether a normality assumption is reasonable (and, if so, what you should assume about SDs for power calculations) depends heavily on your assumptions about the populations you are comparing. If your PI hasn't made those assumptions clear yet, get them clarified. Trying to do power calculations is an excellent way to expose and clarify potentially hidden assumptions.
Comment: This is all excellent, but it's worth pointing out that PHQ-9 scores aren't generally bimodally distributed in the general population, as you might inspect, because depression isn't a discrete present/absent thing. Instead, you see a heavy-tailed right-skewed distribution, with people increasingly likely to be diagnosed as "*depressed*" as their scores increase.
Comment: 100% agree, I just wanted to make sure nobody came away from this thinking of a bimodal distribution with two well-separated peaks for ND and MDD cases. Not that I'm accusing you of making that mistake!
Answer: If you have a representative sample of the scores you can compute the power or sample size for a Wilcoxon test.  These are functions of the entire frequency distribution of the scores, and the effect size is stated in terms of an odds ratio, which can be converted to a difference in means for interpretation.  Detailed examples with R code are in Chapter 7 of BBR.  Note that the Wilcoxon test is a special case of the proportional odds model, and this model can handle arbitrarily strong clumping at zero.


Programmatically get a form in Drupal 8

Question: The documentation states:

The argument passed to the getForm() method is the name of the class
  that defines your form and is an implementation of
  \Drupal\Core\Form\FormBuilderInterface.

How do I find out the name of the class that defines the website feedback form provided by the core contact module (the form id is: feedback_contact_message_form).
When I dpm() the form I can see that there is a class defined:
$input array(30)
  →'#attributes' => array(1)
    →'class' => array(3)
      string(29) "feedback-contact-message-form"
      string(20) "contact-message-form"
      string(12) "contact-form"

I tried various forms of this as the parameter to pass to:
\Drupal::formBuilder()->getForm();

but no luck. Thanks
Comment: Contact forms are entities, so I'd imagine they all come from `Drupal\contact\ContactFormEditForm`. Not sure there's an easy way to find that out unless you know it's an entity form though. The best way might be to look under the module's `src` folder for files whose name ends with `Form.php`. I think that's a safe convention to follow
Comment: Wouldn't Berdir's answer in my question help? http://drupal.stackexchange.com/questions/146617/how-to-render-a-contact-form-in-a-theme
Comment: Thanks, but I don't follow Berdir's answer. I've added my block plugin below my answer - adding the first two lines from your code sample. Where do I go from here? Thanks.
Answer: This renders within a block plugin:
class myModule extends BlockBase {
  public function build() {

    $default_form = \Drupal::config('contact.settings')->get('default_form');
    $entity = \Drupal::entityManager()->getStorage('contact_form')->load($default_form);

    $message = \Drupal::entityManager()
      ->getStorage('contact_message')
      ->create(array(
        'contact_form' => $entity->id(),
      ));

    $form = \Drupal::service('entity.form_builder')->getForm($message);

    return $form;
  }
}

Comment: I'm having the same issue. can we share notes and chat? All I want to do is just take a form from somwhere on the site and output it somewhere else inside a PHP enabled text field.
Comment: How I can submit this entity form programatically?
Comment: if the form needs to act on #after_build, for ajax operation, do we have any solution?

Programmatically get a form in Drupal 8


Cannot borrow variable as mutable because it is also borrowed as immutable while refactoring a parser

Question: To learn Rust, I began working on a simple parser. I got it working, but I used a ton of bad practices like using .clone() for saving data in my structs.
Today I started refactoring the code to use references to the object rather than cloning them. I got pretty far before getting stuck with these two errors:
error: cannot borrow 'm_list' as mutable because it is also borrowed as immutable [E0502]

and
error: cannot borrow 'h_list' as mutable because it is also borrowed as immutable [E0502]

After reading about the error, I'm very confused. Some say it's a bug in Rust's borrow checker, but I'm 99% sure that its a bug with my code.
Code where the error manifests:
mod utypes;

use std::env;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::error::Error;

use utypes::*;

fn main() {

    let args: Vec<_> = env::args().collect();

    let file = match File::open(&args[1]) {

        Ok(file) => file,
        Err(why) => panic!("IO Error on line: {}, cause: {}", line!(), Error::description(&why)),

    };
    let buffer = BufReader::new(file);

    let mut m_list: Vec<Materia> = Vec::new();
    let mut h_list: Vec<Hora> = Vec::new();
    let mut t_list: Vec<Turno> = Vec::new();

    for line in buffer.lines() {

        let l = line.unwrap();
        let spl_line: Vec<&str> = l.split(':').collect();

        if spl_line[0].starts_with('#') { continue; }

        match spl_line[0] {

            "mat" => { parse_mat(&mut m_list,spl_line) },
            "hra" => { parse_hra(&m_list,&mut h_list,spl_line) },
            "trn" => { parse_trn(&mut t_list,spl_line) },
            "tad" => { exec_tad(&h_list,&mut t_list,spl_line) },
            ""    => continue,
            _     => panic!("Unknown type identifier: {}.", spl_line[0]),

        }

    }

    for turno in t_list.iter() {

        println!("\nTurno: {}:\n", turno.nombre);
        for dia in turno.dias.iter() {

            print!("{:?}: ", dia.enum_id);
            for hora in dia.horas.iter() {

                print!("\n\t{} ", hora);

            }
            println!("");

        }

    }

    /*
    println!("\nDEBUG INFO:\n");
    println!("{:?}", m_list);
    println!("{:?}", h_list);
    println!("{:?}", t_list);
    */

}

fn get_trn( t_list: &Vec<Turno>, gid: u32 ) -> Option<usize> {

    for (i,trn) in t_list.iter().enumerate() {

        if trn.id == gid {

            return Some(i);

        }

    }

    None

}

fn get_hra ( h_list: &Vec<Hora>, gid: u32 ) -> Option<usize> {

    for (i,hra) in h_list.iter().enumerate() {

        if hra.id == gid {

            return Some(i);

        }

    }

    None

}

fn get_mat ( m_list: &Vec<Materia>, gid: u32 ) -> Option<usize> {

    for (i,mat) in m_list.iter().enumerate() {

        if mat.id == gid {

            return Some(i);

        }

    }

    None

}

fn parse_mat<'a> ( m_list: &'a mut Vec<Materia>, line: Vec<&str> ) {

    assert_eq!(4,line.len());

    let id: u32 = match line[1].parse::<u32>() {

        Ok(id)    => id,
        Err(_)    => panic!("Error parsing u32 at {}.", line!()),

    };

    m_list.push(Materia::new(id,line[2].to_string(),line[3].to_string()));

    }

fn parse_hra<'a> ( m_list: &'a Vec<Materia>, h_list: &mut Vec<Hora<'a>>, line: Vec<&str> ) {

    assert_eq!(5,line.len());

    let id: u32 = match line[1].parse::<u32>() {

        Ok(id)    => id,
        Err(_)    => panic!("Error parsing u32 at {}.", line!()),

    };
    let start: u32 = match line[2].parse::<u32>() {

        Ok(start) => start,
        Err(_)    => panic!("Error parsing u32 at {}.", line!()),

    };
    let durat: u32 = match line[3].parse::<u32>() {

        Ok(durat) => durat,
        Err(_)    => panic!("Error parsing u32 at {}.", line!()),

    };
    let matid: u32 = match line[4].parse::<u32>() {

        Ok(matid) => matid,
        Err(_)    => panic!("Error parsing u32 at {}.", line!()),

    };
    let mat_i: usize = match get_mat(m_list,matid) {

        Some(x) => x,
        None      => panic!("Error matid not found in m_list!")

    };

    h_list.push(Hora::new(id,start,durat,&m_list[mat_i]));

}

fn parse_trn<'a> ( t_list: &mut Vec<Turno<'a>>, line: Vec<&str> ) {

    assert_eq!(3,line.len());

    let id: u32 = match line[1].parse::<u32>() {

        Ok(id) => id,
        Err(_) => panic!("Error parsing u32 at {}.", line!()),

    };

    t_list.push(Turno::new(id,line[2].to_string()));

}

fn exec_tad<'a> ( h_list: &'a Vec<Hora<'a>>, t_list: &mut Vec<Turno<'a>>, line: Vec<&str> ) {

    assert_eq!(4,line.len());

    let hid: u32 = match line[2].parse::<u32>(){

        Ok(hid) => hid,
        Err(_)  => panic!("Error parsing u32 at {}.", line!()),

    };
    let tid: u32 = match line[3].parse::<u32>(){

        Ok(tid) => tid,
        Err(_)  => panic!("Error parsing u32 at {}.", line!()),

    };

    let hra_i: usize = match get_hra(h_list,hid) {

        Some(x) => x,
        None    => panic!("Error matid not found in m_list!")

    };
    let trn_i: usize = match get_trn(t_list,tid) {

        Some(x) => x,
        None    => panic!("Error matid not found in m_list!")

    };

    match line[1] {

        "Dom" => t_list[trn_i].addhra(Dias::Dom,&h_list[hra_i]),
        "Lun" => t_list[trn_i].addhra(Dias::Lun,&h_list[hra_i]),
        "Mar" => t_list[trn_i].addhra(Dias::Mar,&h_list[hra_i]),
        "Mie" => t_list[trn_i].addhra(Dias::Mie,&h_list[hra_i]),
        "Jue" => t_list[trn_i].addhra(Dias::Jue,&h_list[hra_i]),
        "Vie" => t_list[trn_i].addhra(Dias::Vie,&h_list[hra_i]),
        "Sab" => t_list[trn_i].addhra(Dias::Sab,&h_list[hra_i]),
        _     => panic!("Unknown day error!")

    }

}

And utypes.rs:
use std::fmt;

//Dias
#[derive(Debug)]
pub enum Dias {

    Dom,
    Lun,
    Mar,
    Mie,
    Jue,
    Vie,
    Sab,

}

//Materia

#[derive(Debug)]
pub struct Materia {

    pub id: u32,
    pub nombre: String,
    pub profesor: String,

}

impl Materia {

    pub fn new( i: u32, nom: String, prof: String ) -> Materia {

        Materia {

            id: i,
            nombre: nom,
            profesor: prof,

        }

    }
}

//Hora

#[derive(Debug,Clone)]
pub struct Hora<'a> {

    pub id: u32,
    pub comienzo: u32,
    pub duracion: u32,
    pub materia: &'a Materia,

}

impl<'a> Hora<'a> {

    pub fn new ( id: u32, com: u32, dur: u32, mat: &'a Materia ) -> Hora<'a> {

        Hora {

           id: id,
            comienzo: com,
            duracion: dur,
            materia: mat,

        }

    }

    pub fn fin ( &self ) -> u32 {

        self.comienzo + self.duracion

    }

    pub fn fmt_time ( tot: u32 ) -> String {

        let min = ( tot / 60 ) % 60;
        let hra = tot / 3600;

        format!("{:02}:{:02}", hra, min)

    }

}

impl<'a> fmt::Display for Hora<'a> {

    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {

        write!(f, "[{}-{}, {}]", Hora::fmt_time(self.comienzo), Hora::fmt_time(self.fin()), self.materia.nombre)

    }

}

//Dia

#[derive(Debug)]
pub struct Dia<'a> {

    pub enum_id: Dias,
    pub nombre: String,
    pub horas: Vec<&'a Hora<'a>>,

}

impl<'a> Dia<'a> {

    pub fn new( ei: Dias, nom: String ) -> Dia<'a> {

        Dia {

            enum_id: ei,
            nombre: nom,
            horas: Vec::new(),

        }

    }

    pub fn addhra( &mut self, hra: &'a Hora<'a> ){

        self.horas.push(hra);

    }

}

//Turno

#[derive(Debug)]
pub struct Turno<'a> {

    pub id: u32,
    pub nombre: String,
    pub dias: [Dia<'a>; 7],

}

impl<'a> Turno<'a> {

    pub fn new( i: u32, nom: String ) -> Turno<'a> {

        Turno {

            id: i,
            nombre: nom,
            dias: [

                    Dia::new(Dias::Dom,"Domingo"  .to_string()),
                    Dia::new(Dias::Lun,"Lunes"    .to_string()),
                    Dia::new(Dias::Mar,"Martes"   .to_string()),
                    Dia::new(Dias::Mie,"Miercoles".to_string()),
                    Dia::new(Dias::Jue,"Jueves"   .to_string()),
                    Dia::new(Dias::Vie,"Viernes"  .to_string()),
                    Dia::new(Dias::Sab,"Sabado"   .to_string())

                   ],

        }

    }

    pub fn addhra( &mut self, dia: Dias, hra: &'a Hora<'a> ) {

        match dia {

            Dias::Dom => self.dias[0].addhra(hra),
            Dias::Lun => self.dias[1].addhra(hra),
            Dias::Mar => self.dias[2].addhra(hra),
            Dias::Mie => self.dias[3].addhra(hra),
            Dias::Jue => self.dias[4].addhra(hra),
            Dias::Vie => self.dias[5].addhra(hra),
            Dias::Sab => self.dias[6].addhra(hra),

        }

    }

}

I think the mutable reference &mut m_list ends after the function parse_mat() returns so I should be able to, in another iteration of the for loop, call parse_hra() and pass &m_list without problems, right?
Full error:
src/main.rs:36:39: 36:45 error: cannot borrow `m_list` as mutable because it is also borrowed as immutable [E0502]
src/main.rs:36             "mat" => { parse_mat(&mut m_list,spl_line) },
                                                 ^~~~~~
src/main.rs:37:35: 37:41 note: previous borrow of `m_list` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `m_list` until the borrow ends
src/main.rs:37             "hra" => { parse_hra(&m_list,&mut h_list,spl_line) },
                                             ^~~~~~
src/main.rs:71:2: 71:2 note: previous borrow ends here
src/main.rs:11 fn main() {
           ...
src/main.rs:71 }
           ^
src/main.rs:37:47: 37:53 error: cannot borrow `h_list` as mutable because it is also borrowed as immutable [E0502]
src/main.rs:37             "hra" => { parse_hra(&m_list,&mut h_list,spl_line) },
                                                         ^~~~~~
src/main.rs:39:34: 39:40 note: previous borrow of `h_list` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `h_list` until the borrow ends
src/main.rs:39             "tad" => { exec_tad(&h_list,&mut t_list,spl_line) },
                                            ^~~~~~
src/main.rs:71:2: 71:2 note: previous borrow ends here
src/main.rs:11 fn main() {
           ...
src/main.rs:71 }
           ^

I'm not a native speaker so sorry for any mistakes.
play-rust
Comment: You store references to the elements of `m_list` in the `h_list` using function `parse_hra`. Therefore `m_list` borrow ends when `h_list` go out of scope. Try to use `Rc` instead references.
Comment: Please include a complete example of the erroneous code, including stub methods for parse_hra/parse_mat/parse_trn/exec_tad & without removing main/uses
Answer: @aSpex comment is right.
To solve this problem you can replace the references inside Hora and Dia with the corresponding indices. That is
struct Hora {
    materia: usize; //index in m_list
}

struct Dia {
    horas: Vec<usize>; //indices in h_list
}

You can also create a struct with fields m_list, h_list and t_list, so they are keeped together.
See this for a comparison with the approach using Rc and RefCell.


Incorporate guitar amp in my bass rig?

Question: I have a Hartke 30w bass amplifier with preamp-out and speaker-in jacks. It has a decent tone and plenty of bottom and head-room for the space (small theatre). But I also have a new Vox vt20x (20w) with a tube and lots of modeling and effects.
I want to use some of these sounds from the guitar amp with my bass (the flanger in particular is really nice). Now the vox doesn't have a speaker-out or a line-out, just a headphone-out which doesn't seem promising.
I've tried plugging the bass into the Hartke, then running the preamp-out into the Vox input and it does seem to give me more shape on the lows and more head-room. And I'm guessing that I'm not going to blow any circuits by doing this, but is there a better way to either beef-up the low-end from the Vox or get a signal out of it that I can feed to the bass speaker?
Edit: tried the same chaining setup again and there is still sound from the bass speaker. So I am getting some degree of splitting. There's also a line-out on the Hartke with similar result.
Update: I didn't try either of the suggestions, but went ahead to rehearsal with the preamp-out jury-rig. The result was too noisy for the space. So I reduced my gear to just the Hartke amp and an envelope-filter pedal. For future shows, I think the real solution is a smaller Bass Amp, like 15w, so I can drive the amp more into its sweet spot. With the 30w, I have my volume on 3, and any more would be too loud. More details in my facebook post.
I cannot judge between the 2 answers yet, but either would probably be less noisy than what I tried. But the real crux of the issue for this show, is that 30w + 20w is just too much amplification for the space.
Update (much later) I have bought a lot more pedals so I don't need the guitar amp's effects. Adding a JHS Little Black Box in the loop on the bass amp (new 75w 12") gives me much more control of the overall volume while keeping the preamp in the sweet spot. This setup worked really well in my last show (The Who's Tommy).
Comment: Indeed split the signal from your guitar. I use a Morley ABY.
Answer: When I've done things like using multiple amps for different sounds, I've done it by splitting the signal from the instrument so that each amp has its own dedicated input from the instrument (signal runs in parallel).
I'd recommend this approach especially when running a bass signal into a guitar amp. Guitar amps are not meant to reproduce low frequencies very well (and in fact the lows somewhat ruin the sound if overdriven). However, eq-ing out the low guitar tones will clean up the distorted sound being generated by the guitar amp.
Compression settings could differ between amps (quite common); and effects like reverb sound worse when pumped with a lot of low frequencies.
You can also individually adjust the loudness of each amp and mix the signals to taste. Overall, you end up with a sound that is far clearer than running the signal first into two amps in series.
Comment: Something like this: http://www.tonebone.com/re-bigshot-aby.htm

They retail at around $100 but that's the type of component you'll need (AB/Y Splitter). The page has some nice diagrams to show the possibilities.
Comment: Ok. Should I look for just a "signal splitter"? Or do I want a little mixer/preamp?
Comment: That seems like a very good suggestion. After researching, it appears that a polarity switch is a useful feature for using two amps (which the bigshot has, but many ABYs do not).
Answer: I would go one further to the comments and suggest you use a crossover. A Rolls SX21 would do the job, and use up very little space. Basically you want to remove the low frequencies from the signal you are sending to your second amp. You will also probably want to buffer each circuit (treble and bass) separately, and you don't want the ground of one amp seeing the ground of another, else you get an unpleasant hum.
Answer: I want to piggyback on what's been said already. The Rolls SX21 is an awesome, small and wonderful tool to split your bass signal. I am doing things a little different though I am splitting the input between a GK 250w 1X12 and a TC Electronic 250w 2X10. But, the thing that I like to do different is use the 2X10 for the lows (under 200hz) and the 1X12 for the highs (above 200hz). The large 12" speaker doesn't sound tinny or harsh. Because the speaker is large in tends to have smooth pleasant sounding highs. I HIGHLY recommend the Rolls SX21 as a means to have a great, smooth and tight bass sound.

Incorporate guitar amp in my bass rig?


Let $x \in \Bbb R$, $n \in \Bbb N$, show that $n \lfloor x \rfloor \leq \lfloor nx \rfloor \leq n \lfloor x \rfloor + (n-1)$

Question: Problem:
Let $x \in \Bbb R$, $n \in \Bbb N$, show that $n \lfloor x \rfloor \leq \lfloor nx \rfloor \leq n \lfloor x \rfloor + (n-1)$
I have one part of the inequality, namely that
since $ \lfloor x \rfloor \leq x$, then $n\lfloor x \rfloor \leq nx$, but $ n\lfloor x \rfloor \in \Bbb Z$, so $n \lfloor x \rfloor \leq \lfloor nx \rfloor$
The right side of the inequality I am not so sure about. Insights appreciated.
Answer: If $k \leq x <k+1$ then $nk \leq x <nk+n$. So $nx$ lies between $j$ and $j+1$ for some $j \in\{nk+0,nk+1,...,nk+n-1\}$. Can you now see the right hand inequality?


How do I unset the comment form in Drupal 7?

Question: In Drupal 7, the comment form appears on every node. I want to unset the comment form and display a link of "Add new comment" that will display the comment form on click.
Do you have any Idea?
Answer: Go to "Structure > Content types", "Content type > Comment settings", and uncheck "Show reply form on same page as comments."


Page navigation broken in IE11

Question: I have a page with 4 links and two of them don't work in IE. They're fine in Chrome & FF.
I've read that IE requires some text in the anchor tags, but I tried that and there's no difference.
This is my html:

Link 1

Link 2

Link 3

Link 4

The funny thing about this is that I have links 1 & 2 set as large boxes on a first row and links 3 & 4 are boxes that look very similar to the other two but are on another row.
I also have another page in my app with the same layout and I have the same problem on that page as well.
Can't figure out what's causing this. The two elements are completely dead. The hovers don't work, the links don't work, they're just sitting there with no actions happening at all.
Answer: You forgot to close
Replace <a href='link1' for  (do the same on following anchors)
Comment: If you disable CSS do they work? Wondering if there's any rule affecting them. Doesn't seem like a IE11 issue since the first two are working (although I can't test that on my end)
Comment: Cool! Glad you found it!
Comment: Thanks, but that was just a typo. I'm converting from Jade and just forgot those. Will edit the question now.
Comment: They DO work when css is disabled. Will search for the culprit now.
Comment: Found the culprit `z-index` Not sure why that breaks the linking, but there you go. Thanks @rafg

Page navigation broken in IE11


Function on $\mathbb{C}$ with all primes as zeros?

Question: According to the Weierstraß factorization theorem, an entire function with all primes as zeros would be (if I didn't mess up):
$$\tilde P(z) = \prod_{p \text{ is prime}} \left(1-\frac{z}{p}\right) \cdot e^{z/p} $$ for $z\in\mathbb{C}$.
Formally I can define the slightly different function
$$P(z) = \prod_{p \text{ is prime}} \left(1-\frac{z}{p}\right)$$
for $z\in\mathbb{C}$.
Question: For which $z\in\mathbb{C}$ does the latter converge apart from the obvious $z=0$ or $z$ is prime?
Thoughts:

Taking the $\log$ this translates to the question for which $z$ $$\sum_{p \text{ is prime}}\log(1-z/p)$$ converges. I am guessing that this is not the case because thinning the harmonic series out to just primes, $\sum 1/p$, does not make it convergent and since $\sum \log(1-1/n)$ is not convergent, thinning out to primes likely does not help either.
Yet for $\tilde P$ the $\log$ yields $$\sum_{p \text{ is prime}}(z/p+\log(1-z/p)),$$ which is, in absolute value, even larger but should converge because $\tilde P(z)$ converges. Hmm???
Comment: Note that $\sum\frac 1p$ does not converge...
Comment: Reflect on your point 1. This is why the Weierstrass factorisation theorem needs terms like $e^{z/p}$.
Comment: @abiessu: fixed, the **not** was missing.
Comment: What do you mean with "in absolute value, even larger"? – Note that $z/p+\log(1-z/p) \sim (z/p)^2$ for $p \to \infty$, and that makes the series (2) converge (uniformly on compact sets).
Comment: Shouldn't this converge for *all* $z$? It looks like a Cauchy sequence to me.
Comment: @MartinR thanks, that answers my 'Hmmm'. With 'in absolute value' I mean $|z/p|+|log(1-z/p)|$. I didn't know this nice $(z/p)^2$ estimate.
Comment: Also $f$ is entire with only simple zeros at each prime iff it is of the form $f(z)=e^{g(z)}\prod_p (1-z/p) e^{z/p} $ with $g$ entire.
Comment: If $z\ne0$ and $z\ne p$ the product diverges.
Answer: Remark: There are two slightly different definitions of convergence of an infinite product $\prod_{n=1}^\infty a_n$. One definition is that all factors $a_n$ are nonzero and that $\lim_{N \to \infty} \prod_{n=1}^N$ exists and it not zero.
The other definition allows that finitely many factors are zero, and  requires that for some $n_0$, $\lim_{N \to \infty} \prod_{n=n_0}^N$ exists and it not zero. Using this definition, the value of a convergent infinite product is zero if and only if one factor is zero.
With the first definition, both of your products are divergent if $z$ is a prime.
I'll use the second definition here, which has the advantage that the case "$z$ is a prime" does not need to be considered separately.

It follows e.g. from the Taylor series of $\log(1+w)$ that
$$
|\log(1+w) - w | < K |w|^2
$$
for $|w| < 1/2$ and some constant $K > 0$. Therefore, for $|z| < R$ and all primes $p > 2|z|$,
$$ \tag{*}
\left| \log\left(1-\frac{z}{p}\right) + \frac{z}{p}\right| < \frac{KR^2}{p^2} \, .
$$
This implies the (locally uniform) convergence of $\sum\limits_{p > 2|z| \text{ is prime}} \log\left(1-\frac{z}{p}\right) + \frac{z}{p}$ and thus the convergence of the infinite product $$\prod_{p \text{ is prime}} \left(1-\frac{z}{p}\right) \cdot e^{z/p}\, .$$
For non-zero $z$ is the series $\sum\limits_{p \text{ is prime}} \frac{z}{p}$ divergent, so that $(*)$ also implies the divergence of $\sum\limits_{p > 2|z| \text{ is prime}} \log\left(1-\frac{z}{p}\right)$ and consequently the divergence of
$$\prod_{p \text{ is prime}} \left(1-\frac{z}{p}\right)\, .$$


Why most of the books give Definite Integral Represents Area Under the curve

Question: According to my understanding The definition of Definite Integral is:
if $f(x)$ is a Continuous function in $[a \:\: b]$ and if $P$ is Partition of the Interval $[a \:\: b]$ Then
$$ \lim _{\lVert P \rVert \to 0}\sum_{k=1}^{n}f(c_k)\Delta x_k=\int_{a}^{b} f(x)dx$$ Definite Integral can be negative positive or zero and its value may or may not represent area under the curve.
For example $$\int_{0}^{\frac{3\pi}{2}} \cos x \:dx=-1$$
But the area of $f(x)=\cos x$ in $[0 \: \frac{3\pi}{2}]$ is
$$A=\int_{0}^{\frac{\pi}{2}}\cos x\:dx+\left|\int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}\cos x \:dx\right|=1+|-2|=3$$
Hence Area of a curve $f(x)$ is given by $$A=\int_{a}^{b} |f(x)|dx$$
so area always represents definite Integral of a Positive function.
But why books give the reverse statement that Definite Integral represents area under the curve.
Answer: I think a lot of calculus books use vague terminology to not confuse a frustrated student even more. The difference between the two things you mention is just a matter of what you are using the integral for. Typically, in a calculus course you are finding area or volume and so you use geometric area. However, sometimes you do line-integrals, where you just take the signed area i.e whatever # the integral gives. Hopefully this helps a bit. Maybe someone can share on this. I tried not to give paragraphs of explanation.


Firebase Cloud Functions raise time limit

Question: I have written a cloud function for Firebase that is a https callable and is supposed to delete all files inside a folder. It looks like so:
import * as functions from "firebase-functions";
const admin = require('firebase-admin');
admin.initializeApp();
const {Storage} = require("@google-cloud/storage");
const storage = new Storage({keyFilename: "myserviceaccount.json"});
const fileBucket = "myfilebucket";

    exports.deleteFolder = functions.https.onCall(async (data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError(
        "unauthenticated",
        "only authenticated users can add requests"
    );
  }

  const bucket = storage.bucket(fileBucket);
  return bucket.deleteFiles({
    prefix: `${data.folderName}`,
  }, function(err:any) {
    if (err) {
      console.log(err);
    } else {
      // eslint-disable-next-line max-len
      console.log(`All the Firebase Storage files in users/${data.folderName}/ have been deleted`);
    }
  });
});

That works fine. But my folders sometimes hold quite a lot documents, so the standard time limit of 60s that I get when deploying this function is sometimes not enough to delete all files inside the folder. So I would like to raise the time limit for computation to 540s witch is according to Cloud Functions the maximum computation time I can get. But I cannot find a possibility to do so.
How do I raise the time limit for a single https callable function?
Answer: As explained in the doc, you need to do as follows:
const runtimeOpts = {
        timeoutSeconds: 540
      }

exports.deleteFolder = functions
                        .runWith(runtimeOpts)
                        .https
                        .onCall(async (data, context) => {...});

Note that you can also configure this value via the Google Cloud Console (not the Firebase Console).
In the Google Cloud Console , select your Firebase project, select the "Cloud Functions" vertical menu item, click on the Function name, then click on the "Edit" Button. Then click on the "VARIABLEs, .... " section title as shown below and adjust the Timeout value in the corresponding field.


C - cast int from hex to decimal?

Question: I have an int where the value is supposed to represent a hex value but is stored without the 0x prefix. For example, int x is stored as 20 but is supposed to equal 32 because it is missing the 0x. How do I convert this so that the computer understands to interpret the int as hex rather than decimal?
EDIT: for example, I want to convert 50 to 0x50.
Comment: Number bases are just a way to write a number. x is the same if it is written `0x20` or `32` or `thirty-two` or `treinta y dos`... they are all just ways of saying the same conceptual number. So this only makes a difference when reading or writing a string. `printf("%x", x)` will print it in hex whereas `printf("%d", x)` will print in decimal...
Comment: Are you saying you want to convert a given number, say 50 (0x32), to a different number, say 80 (0x50), such that the *hex* digits of the new number match the *decimal* digits of the old number? (whew, mouthful)
Comment: Let's change your example and see if it makes sense.  "For example, int x is stored as 1F but is supposed to equal 31 becuase it is missing the 0x.  No, this doesn't make sense. Not at all. Please re-explain what you are trying to do.
Comment: So the user types `50` or you have the string `"50"` and you want to do `int x = some_function(that_string);` such that `x == 0x50`? (spoilers: some_function is `strtol`)
Comment: @williamsca97 When you say "int x is stored as 20 but is supposed to equal 32", You must be seeing this in debugger/IDE's watch window. Right click on that and choose mode as Decimal (works on Most IDEs). It will show you 32. Hex, Decimal, Octal, Binary are just way of representation. For CPU, it's all binary!
Comment: So the value can only represent 5/8 of all the numbers?  That's weird.
Comment: possible duplicate [of this](http://stackoverflow.com/questions/3464194/how-can-i-convert-an-integer-to-a-hexadecimal-string-in-c)
Comment: Possible duplicate of [How can I convert an integer to a hexadecimal string in C?](http://stackoverflow.com/questions/3464194/how-can-i-convert-an-integer-to-a-hexadecimal-string-in-c)
Comment: No, I want to convert `50` to `0x50` for example.
Comment: Simply add `0x` on front of it and it will get hex notation. If that isn't the answer you are looking for, you need to clarify the question and show just exactly in what format those values are and why you can't simply edit them.
Comment: Otherwise you can use this code, which doesn't make any sense either. `#include

#define MY_CODE_IS_CRAP(x) 0x##x

int main (void)
{
  printf("%d", MY_CODE_IS_CRAP(20));
}`
Answer: you can use as below. 'hex' is your number without 0x. hexStr will contain actual value.
// number in hex format without 0x
int hex = 20;
char hexStr[10];

// int to string
sprintf(hexStr,"0x%d",hex);

If your want to use it as int:
int number = (int)strtol(hexStr, NULL, 16);

Comment: OP have not mentioned that Int to String conversion is expected.

C - cast int from hex to decimal?


Irreducible polynomials with integer coefficients over Q

Question: Suppose p(x) is an irreducible polynomial over Q of degree n, with integer coefficients. If p(x) has two roots r1 and r2 satisfying r1r2 = 5, prove that n is even.
Attempt at solution:

Because the base field is Q, the field extension is separable, hence no roots of multiplicity > 1.
Because r1r2 = 5, I think we can show that if the degree of n is odd, then this will contradict the fact that p(x) has integer coefficients somehow, but I can't seem to do this.

Any help would be appreciated very much, thank you.
Answer: Claim. For every root $r$ of $p(x)$, $r' = 5/r$ is also a root of $p(x)$.
Proof: Let $g$ be an element of the Galois group that maps $r_1$ to $r$, i.e. $gr_1 = r$. Let $r' = gr_2$. Then $rr' = gr_1 \cdot gr_2 = g(r_1r_2) =g(5) = 5$. QED
Therefore, we can split all roots of $p(x)$ into pairs $(r, r')$ such that $rr' = 5$. Since $p(x)$ doesn't have multiple roots, $r\neq r'$ in each pair. Thus the number of roots is even. Therefore, the polynomial has even degree.
Comment: Presumably, 5 can be replaced with any rational number, and the same result obtains.
Comment: Yury - thank you so much for the quick response & concise solution. Appreciate it.
Answer: Hint $\ $ Since $\rm\,p(x)\,$ is irreducible the Galois group G acts transitively on its roots, so G contains the involution $\rm\:x\to 5/x.\:$ If the involution has no fixed points then it $\rm\color{#C00}{pairs}$ the roots so they are $\rm\color{#C00}{even}$ in number. Else, if $\,\alpha\,$ is a fixed point then $\rm\,\alpha\, = 5/\alpha\,$ so $\rm\:\,\alpha^2 = 5\,$ so $\,\ldots$
Comment: Thank you Bill - much appreciated.


I want to make a Pause/settings screen

Question: I've been working on a game for awhile now. I've encountered an issue that I can't seem to resolve.
I've written it such that when I press the letter "Q", the pause menu comes up. But my game doesn't freeze. I've tried the Thread.sleep(); method but the entire game freezes.
Game Code
package flappyBird;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.*;             // for Random function
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.Timer;

public class FlappyBird extends JFrame implements ActionListener, MouseListener, KeyListener
{

    GUI guis = new GUI();

    Font myFont = new Font("Arial", 1, 65);

    public static FlappyBird flappyBird;

    public final int WIDTH = 800, HEIGHT = 800;

    public Renderer renderer;

    public Rectangle bird;

    public ArrayList<Rectangle> columns;

    public int ticks, yMotion, score;

    public boolean gameOver, started;

    public Random rand;

    public FlappyBird()
    {
        JFrame jframe = new JFrame();
        Timer timer = new Timer(20, this);

        renderer = new Renderer();
        rand = new Random();

        jframe.add(renderer);
        jframe.setTitle("Flappy Block");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(WIDTH, HEIGHT);
        jframe.addMouseListener(this);
        jframe.addKeyListener(this);
        jframe.setResizable(false);
        jframe.setVisible(true);

        bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
        columns = new ArrayList<Rectangle>();

        addColumn(true);
        addColumn(true);
        addColumn(true);
        addColumn(true);

        timer.start();
    }

    public void addColumn(boolean start)
    {
        int space = 300;
        int width = 100;
        int height = 50 + rand.nextInt(300);

        if (start)
        {
            columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
            columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
        }
        else
        {
            columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
            columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
        }
    }

    public void paintColumn(Graphics g, Rectangle column)
    {
        g.setColor(Color.green.darker());
        g.fillRect(column.x, column.y, column.width, column.height);
    }

    public void jump()
    {

        if (gameOver)
        {
            bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
            columns.clear();
            yMotion = 0;
            score = 0;

            addColumn(true);
            addColumn(true);
            addColumn(true);
            addColumn(true);

            gameOver = false;
        }

        if (!started)
        {
            started = true;
        }
        else if (!gameOver)
        {
            if (yMotion > 0)
            {
                yMotion = 0;
            }

            yMotion -= 13;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        int speed = 10;

        ticks++;

              if (started)
        {
            for (int i = 0; i < columns.size(); i++)
            {
                Rectangle column = columns.get(i);

                column.x -= speed;
            }

            if (ticks % 2 == 0 && yMotion < 15)
            {
                yMotion += 2;
            }

            for (int i = 0; i < columns.size(); i++)
            {
                Rectangle column = columns.get(i);

                if (column.x + column.width < 0)
                {
                    columns.remove(column);

                    if (column.y == 0)
                    {
                        addColumn(false);
                    }
                }
            }

            bird.y += yMotion;

            for (Rectangle column : columns)
            {
                if (column.y == 0 && bird.x + bird.width / 2 > column.x + column.width / 2 - 10 && bird.x + bird.width / 2 < column.x + column.width / 2 + 10)
                {
                    score++;
                }

                if (column.intersects(bird))
                {
                    gameOver = true;

                    if (bird.x <= column.x)
                    {
                        bird.x = column.x - bird.width;

                    }
                    else
                    {
                        if (column.y != 0)
                        {
                            bird.y = column.y - bird.height;
                        }
                        else if (bird.y < column.height)
                        {
                            bird.y = column.height;
                        }
                    }
                }
            }

            if (bird.y > HEIGHT - 120 || bird.y < 0)
            {
                gameOver = true;
            }

            if (bird.y + yMotion >= HEIGHT - 120)
            {
                bird.y = HEIGHT - 120 - bird.height;
                gameOver = true;
            }

        }

        renderer.repaint();
    }

    public void repaint(Graphics g)
    {

        g.setColor(Color.cyan);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.orange);
        g.fillRect(0, HEIGHT - 120, WIDTH, 120);

        g.setColor(Color.green);
        g.fillRect(0, HEIGHT - 120, WIDTH, 20);

        g.setColor(Color.red);
        g.fillRect(bird.x, bird.y, bird.width, bird.height);

        for (Rectangle column : columns)
        {
            paintColumn(g, column);
        }

        g.setColor(Color.white);
        g.setFont(new Font("Arial", 1, 100));

        if (!started)
        {
            g.drawString("Click to start!", 75, HEIGHT / 2 - 50);

        }

        if (gameOver)
        {
            g.setFont(myFont);
            g.drawString("Final Score:", 5, HEIGHT / 5 - 65);
            g.drawString("Game Over!", 200, HEIGHT / 2 - 70);
            g.drawString(String.valueOf(score),  WIDTH / 2 - 25, 100);
            ;
        }

        if (!gameOver && started)
        {
            g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
        }
    }

    public void DrawMenu(Graphics g)
    {
        guis.setVisible(true);
    }

    public static void main(String[] args)
    {
        flappyBird = new FlappyBird();

    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
        jump();
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
          int keyCode = e.getKeyCode();

          if (keyCode == KeyEvent.VK_SPACE)
              jump();
          if (keyCode == KeyEvent.VK_ESCAPE)

                  guis.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
    }

    public void keyReleased(KeyEvent e) {}

    public void keyTyped(KeyEvent e) {}

        @Override
    public void mouseReleased(MouseEvent e)
    {
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
    }
}

Pause Menu Code
package flappyBird;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.awt.Color;

public class GUI {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public void guis() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI window = new GUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public GUI() {
        initialize();

            JFrame jframe = new JFrame();

            jframe.setTitle("Pause Menu");
            jframe.setResizable(false);

    }

    /**
     * Initialize the contents of the frame.
     */

    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(Color.GRAY);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setResizable(false);

        JButton btnContinue = new JButton("Continue");
        btnContinue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                frame.dispose();
            }

        });
        btnContinue.setBounds(146, 67, 113, 23);
        frame.getContentPane().add(btnContinue);

        JButton btnExit = new JButton("Exit Game");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (JOptionPane.showConfirmDialog(null, "Are you sure?", "Exit",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                  System.exit(0);
                } else {

                }
            }

        });
        btnExit.setBounds(146, 181, 113, 23);
        frame.getContentPane().add(btnExit);

        JButton btnShare = new JButton("Share");
        btnShare.setBounds(10, 101, 89, 23);
        frame.getContentPane().add(btnShare);

        JButton btnSaveHighscore = new JButton("Save High Score ");
        btnSaveHighscore.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnSaveHighscore.setBounds(137, 135, 132, 23);
        frame.getContentPane().add(btnSaveHighscore);

        JButton btnLoadHighScore = new JButton("Load High Score");
        btnLoadHighScore.setBounds(137, 101, 132, 23);
        frame.getContentPane().add(btnLoadHighScore);

        JButton btnNewButton = new JButton("Cheat Codes");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

                JOptionPane.showMessageDialog(null, "Press A A A B for 100 points instant.");

            }
             });
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        btnNewButton.setBounds(294, 101, 105, 23);
        frame.getContentPane().add(btnNewButton);
    }
    private static void addPopup(Component component, final JPopupMenu popup) {
        component.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }
            private void showMenu(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }

    public void setVisible(boolean b){

        guis();
    }

}

Answer: You will have to introduce a new boolean variable called "paused" or anything like this.
When you enter the menu, you have to set it to true, and after exiting the menu, you set it to false. I couldn't really find your code for this though.
In your actionPerformed you will have to check the paused-flag, and if it's set not increment the time / not repaint etc.
So it's basically
public void actionPerformed(ActionEvent e)
{
    if(!paused)
    ...
}

and somewhere else:
"when q-key pressed"
{
    paused = !paused; // toggle the Pause
    if(paused) DrawPauseMenu();
}

Comment: Do my gui work then if i do it that way? Just boot up eclipse and try my code first. Since i can get a pause menu to work. But i want it to be like if i press Q the game freezes, but i can use the GUI
Comment: You have to make sure what you "exclude" in your execution when, paused is set.
You must not ignore all events. Only ignore Jump-Events, or Timer Updates, so don't let the bird move.
Comment: Oh ok, but i've tryed what u wrote. I nothing happends. The only thing change i did to your code was  'if (paused) guis.setVisible(True);

so now it does the same as before, shows the gui but dont freezes the game  :/ I am new to coding tho
Comment: Hey, KillPinguin. Do have any answears for this?
Comment: @AndreasWefring the thing is that your code is very unstructured and I didn't spend too much time trying to understand it tbh.
Thus I did not write an extensive answer.

What you need to do is introduce a Pause-State (via the variable) and while in Pause-State wrap all Statements that would update the game with `if(!paused) { ... }`
You still have to listen to important events though. So don't filter out anything while paused, just updates to the bird, and still notice clicks on the UI and stuff...
Comment: Hey! I dont know why, but today it worked. Thanks alot!!

I want to make a Pause/settings screen


Algebraic points on an elliptic curve

Question: There is a book about rational points on elliptic curves. What about algebraic points?
Comment: What about them?  Silverman has written other books on elliptic curves, e.g. _The Arithmetic of Elliptic Curves_ and _Advanced Topics in the Arithmetic of Elliptic Curves._  What do you want to know?
Comment: Qiaochu Yuan, are they important to study and if so what is their basic theory.
Answer: It is quite common to study elliptic curves over $K$ or to ask about all $K$-points on an elliptic curve (or on an abelian variety), where $K$ is a subfield of the algebraic numbers; usually a number field (finite extension of $\mathbb{Q}$), but sometimes somewhat bigger (e.g., the extension given by adding all $p^n$th roots of unity for some fixed prime $p$). It is also common to work over finite fields, or over general global fields or over local fields. Most of the basic theory over number fields is very similar to that over $\mathbb{Q}$; e.g., the Mordell-Weil Theorem holds for any number field (the group of $K$-points on an elliptic curve defined over $K$ is finitely generated for any number field $K$). It's known that for any number field $K$, there is a finite list of possible torsion subgroups of the group of $K$-defined points of any elliptic curve defined over $K$ (that is, there is a version of Mazur's Theorem for each number field). There is also the version of integral points ($\mathcal{O}_K$-points, where $\mathcal{O}_K$ is the group of integers of $K$), etc.
In fact, most of Silverman's books mentioned by Qiaochu work over arbitrary fields, restricting on occasion to local fields or to global fields. Most of the theory is quite general, and yes, there is interest in understanding elliptic curves over number fields.

Algebraic points on an elliptic curve


Limit concurrent connections to Postfix server

Question: I'm having trouble limiting the number of concurrent connections from the same client to my Postfix server. Limiting the maximal number of concurrent processes, from the same or different clients, is done easily.
Here are the two parameters that I'm using on main.cf to do so:
default_process_limit = 50
smtpd_client_connection_count_limit = 25

When testing, I run 100 concurrent connections (threads) from the same client, but it limits at 50 smtpd processes instead of just 25. Here is how I'm counting the smtpd processes at the server:
ps -C smtpd | wc -l

Does anyone know why this happens?
Answer: If you want to change the maximal number of smtpd running you should make your changes in /etc/postfix/master.cf. You change - to 50 in the 7th column then restart postfix with postfix restart (or a distro specific substitute).
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       -       -       -       smtpd

change this to
smtp      inet  n       -       -       -       50      smtpd

smtpd_client_connection_count_limit limits how many connections an smtp client can make.
It does not say how many smtpd servers will run by default. (smtpd_client_connection_count_limit is by default half of the default process limit.)
Comment: That is the same as changing `default_process_limit`. What I want is `smtpd_client_connection_count_limit`
Comment: @Jay That is not correct. I also made some changes to clarify. In fact when you specify '-' it takes `default_process_limit`. You can override this per service by replace it with a number.
Comment: Yes. But you are still overriding `default_process_limit` and this is for the "global" number of concurrent processes. I want to limit the number of processes per client to avoid having the same client use all the processes defined in `default_process_limit` (maxproc)
Comment: This is a per service override and it will only affect smtpd. smtpd_client_connection_count_limit is only a safeguard maybe what you want is a policy daemon with rate limiting capabilities.
Comment: ok. So, are you telling me that if I change the `-` to 50 as you mentioned, I can have 2 different clients running 50 processes against the server (50 + 50 = 100 max processes)? Considering that `default_process_limit` is set to 100.
Comment: i realized that your question has the right settings in the first place. Have you reloaded it with `postfix reload` after making the changes?
Comment: Yes I did. reload and restart.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/20087/discussion-between-cstamas-and-jay).