$x-y$ divides $x^n - y^n$ -- prove by mathematical induction

Question: I'm doing some prove by mathematical induction practice problems. I got to this last problem but I don't know how to approach it.
This is the question
For all positive integers $n$ and any distinct real numbers $x$ and $y$, $x - y$ divides $x^n - y^n$.
Comment: Are you familiar with Polynomial Long Division? You should be able to use that to prove this result via induction.
Comment: It is not clear to me how to use induction here.
Comment: Could you explain how to approach this problem. I have been doing prove by induction with equations.  I know it sounds stupid but I don't know how to write this into an equation.
Comment: Its clear to me now :-).
Comment: **Wrong close reason. Pls close this as [duplicate](https://math.stackexchange.com/q/188657).**
Answer: Using strong induction:
Base cases $n=1$ and $n=2$ hold trivially  $(x-y)\mid (x-y)$ and $(x-y)\mid (x^2-y^2)$ since $x^2-y^2=(x+y)(x-y)$
Assume for our induction hypothesis that the claim $(x-y)\mid (x^k-y^k)$ holds for all $k$ up to $n$ for some $n$.
Consider $n+1$
$x^{n+1}-y^{n+1}=(x+y-y)x^n-(y+x-x)y^n$
$=(x+y)(x^n-y^n)-yx^n+xy^n=(x+y)(x^n-y^n)-xy(x^{n-1}-y^{n-1})$
Each term of which is divisible by $x-y$ by our induction hypothesis, thus the hypothesis is validated and the claim is true for all positive integers $n$.
Comment: Excuse me, I understood that in strong induction the only thing that changes is the inductive hypothesis. Is it really necessary to evaluate two base cases?
Comment: @EduardoS. in the way I wrote my answer, yes, since we apply the inductive hypothesis both to the $x^n-y^n$ that appears *as well as the* $x^{n-1}-y^{n-1}$ *that appears* when trying to show that $x^{n+1}-y^{n+1}$ is divisible by $x-y$.  So, in the way the inductive argument I laid out above, we need to know the statement is true for *both* of the previous two values.
Comment: You can, of course, prove this a different way.  Andre's answer for instance succeeds at avoiding needing to apply the induction hypothesis more than once in his induction argument.  If I were to have reworded my induction argument above to a completely different one then yes we could have gotten away without showing the second base case.  But, if wishing to apply the inductive hypothesis to multiple expressions simultaneously with different indices (*like how I apply both to $x^n-y^n$ as well as to $x^{n-1}-y^{n-1}$*) then you need appropriate base cases shown to match.
Comment: My strong induction concept is: In the inductive hypothesis, you assume that property holds for all numbers less than a certain n. Suppose that you were able to demonstrate the inductive step, which, with the assumed information, will demonstrate that "If all the naturals preceding a natural number (n + 1) fulfill a property, then (n + 1) fulfills it").
Comment: But we did not know if what we assumed in the inductive hypothesis was true, we only assumed it. So here the base case comes into play, since we evaluate and obtain that for $n = 1$ it holds, so we have the first case that holds the property.(this is why base case exists)
Comment: Since we prove that "If we choose a number (n+1) and all its previous numbers holds the property, then (n+1) it holds it", then if we choose any number, it is enough to see if all its previous numbers holds the property, so that this number fulfills it. We could see that "all the numbers before n = 2" are only the base case and therefore n = 2 complies, then for n = 3 it complies and so on. "It is enough that the first piece of dominoes fall so that the others fall" So why do you show 2? This isn't necessary, according to my understanding of how strong induction works.  Thanks for you reading
Comment: I showed $1$ directly.  I showed $2$ directly.  From then on, I showed it holds for $n+1$ because it holds both for $n$ and for $n-1$...  that is to say, I did not manually show that it worked for $3$, but it followed from the inductive argument and because it was true for both $1$ and for $2$.  I could not show that it was true for $2$ because of the inductive argument with the way I worded it, since that would have needed knowledge that it worked for both $1$ and for $0$... but here we are ignoring $0$ and it doesn't work there anyways.
Comment: But as I explained to you, n = 2 should be deduced from the same inductive argument, like a domino.
Comment: In the way I worded my argument... it is not the weight of a single domino which causes the next domino to fall, but rather the weight of the two previous dominos together which cause the next to fall.
Comment: Yes, it is exactly what I said. We proved that n = 1 was true. Now why prove that n = 2 is true? The "2" domain will fall, since all its previous domains are reduced to one (n = 1). And we already proved that it was true. So I don't understand why you test case n = 2.
Comment: We know that 'domino' $1$ holds,was prove in the base case.

To see if 'domino' 2 holds, we see that all the ones before it (only n = 1) holds, but that is the base case, therefore n = 2 holds.

To see if dominos 3 holds, we see that all previous to it (n = 1, n = 2) holds, therefore n = 3 holds .

and so...
Comment: No no no... My argument... to show that domino $n+1$ falls requires both of the dominos $n$ and $n-1$ to fall.  If I wanted to show that domino $3$ falls, that requires both of the dominos $2$ and $1$ to have fallen, and the argument the way that I worded it does *not* show that domino $2$ falls just because domino $1$ fell... the argument I used only works to show the domino $n+1$ falls because both of the dominos $n$ and $n-1$ were known to have fallen.  In the case of $2$ we do *not* know that both of the previous *two* dominos had fallen because *there aren't two dominos before it to fall*
Comment: Do not confuse the phrase "The previous two cases held" with the phrase "All previous case/cases held"
Comment: I understand, your demo requires that the previous two comply. So why assume the entire range (k to n) instead of n and (n-1)? That is, would it not be clearer just to assume what you are going to use?
Comment: If you still don't get it... then how about thinking about the Fibonacci Sequence.  If all I told you about the sequence was that the first term was $f(0)=1$ and then I told you that to find $f(n+1)$ you can use $f(n+1)=f(n)+f(n-1)$... then that is *not enough information*... the Lucas Numbers also follow the same pattern.  To find the next fibonacci number or to find the next lucas number, we need to know the *previous two* numbers to add.
Comment: It can be reworded to very specifically refer to only $n$ and $n-1$... sure... but why bother when it is just as easily worded as strong induction and strong induction is used so prevalently already that it should be recognizable and easy to understand.
Comment: Okay, so I think I understood. The proof used the assumption that there were 2 previous cases for which they were true, however, for "2" there were not 2 previous cases with the property.
Comment: So in general, if the argument of our proof will assume(and uses them) $n$ previous terms that holds the property I will have to evaluate $n$ base cases.
Answer: Compute $(x-y) \sum_{k=0}^n x^k y^{n-k}$.
Answer: Hint:
$$x^{n+1}-y^{n+1}=x^{n+1}-x^ny+x^ny-y^{n+1}=x^n(x-y)+y(x^n-y^n).$$
Remark: You don't really want to say any distinct real numbers, for any non-zero real divides any real number, so there is nothing to prove. Integers is fine.
Comment: While this is indeed true, I've always wondered about intuition behind this. I would make an attempt to write x^(n+1) - y^(n+1) as something like (x^n-y^n) * Z to make use of the hypothesis but figuring out I also "need" (for some definition thereof) the (x-y) * W term is a different matter altogether...


How to express the condition in permutation formula.

Question: I have a very basic and conceptual doubt regarding the function of the permutation formula. I am going to ask them through some examples in which I applied them and could not proceed.
The first basic thing is that let us say I have four dice and I want two of my dice to show exactly $3$ and the rest can show any number except $3$.
Now my line of thought was that we fix two places where we fill in $3$ and the rest two places can be filled in $5$ ways each. the first die can show in $5$ ways, the second can also show $5$ ways, the third and fourth can show in $1$ ways each, so the total number of arrangement should be $5 \cdot 5 \cdot1 \cdot 1$. My first doubt is - Does this thing need arrangement and rearrangement or is this automatically arranged? If not then how do I write it using the permutation formula so that the direct result which comes out is automatically arranged and I don't need to look for ways to arrange it.
Answer: You need arrangement. Because two 3's can be on any two places. You can multiply your ways with C(4,2). Here r = 2 because you have two 3's.
So answer will be $C(4,2) * (5*5*1*1)$
In case you want to use permutation. Then,
Number of ways = $\frac{P(4,2) * (5 * 5 * 1 * 1)}{2}$
Dividing by 2 because 3 is repeating twice.


What does 'downward pressure on sample size' mean?

Question: In this article, there is this phrase 'downward pressure on sample size'.
This is the paragraph:

no     matter  the     approach to     collecting  this    new     data,   when attempting     to  create  scorecards  that    leverage    psychometric    data (or    any     other   type    of  data    which   must    be  newly   collected  from    applicants  rather  than    pulled  from    old application forms   or  secondary   sources) there  will    be  downward    pressure    on  sample  size.

What does that mean? Such a phrase exists in other articles: 1 2
Comment: Probably the just mean that it will be costly to collect data ..., so budgetary constraints is that "downward pressure"?
Comment: @BCLC Right, I got carried away. I have deleted my comment and expanded it into an answer.
Comment: @kjetilbhalvorsen Downward pressure on = more costly to increase?
Comment: @BenedictM.J.G. oh alright thanks. Finished the project for that class later that month but good to know :P Post as answer?
Answer: It simply means that there is pressure to keep the sample size as small as possible and most likely smaller than could be desired by someone whose sole interest was in maximizing information quality - be if for inference, prediction, or merely for descriptive purposes.
Many reasons can be imagined: social convention, monetary costs, ethical concerns (e.g., if subjects might suffer), time constraints, or avoiding precedents. It might simply be the case that the expected result is unwelcome and some party with influence over the research process is seeking to compromise the data.
The party under pressure to minimize sample size need not be a researcher, either; it could be an organizational unit, a social group, a company. Indeed, researchers might themselves apply pressure, e.g., pressuring a colleague to keep sample sizes in line with their field's conventions, possibly to avoid setting a costly precedent (costly in terms of money or lost opportunity to publish spurious results).


Converting UTF8 Data to SQL_ASCII

Question: I want to convert my query results from a database, which is SQL_ASCII database encoded, to UTF-8.
$result = $query->execute;
$converted_data = array();
foreach($result as $record){
    $converted_data[count($converted_data)] = drupal_convert_to_utf8($record->name, 'sql-ascii');
}

I used drupal_convert_to_utf8 but it returns blank all the time. I was confused if sql-ascii should be the second parameter of drupal_convert_to_utf8. The database encoding, where the query results is fetched, is SQL_ASCII in postgres.
UPDATE:
According to the official documentation, It

Requires the iconv, GNU recode or mbstring PHP extension.

What are they? Am I going to install/include it or what?
Comment: I do not think that you can change the encoding if SQL_ASCII is set in PostgreSQL (see [docs](https://www.postgresql.org/docs/9.4/static/multibyte.html)). Taking a database backup, deleting the database, and re-creating as UTF-8 seems to be the suggested method at http://stackoverflow.com/questions/5090858/how-do-you-change-the-character-encoding-of-a-postgres-database and http://stackoverflow.com/questions/380924/how-can-i-change-database-encoding-for-a-postgresql-database-using-sql-or-phppga

If the client encoding is SQL_ASCII, then maybe locale is funky in PHP/PDO?
Answer: Go to /admin/reports/status and take a look at your unicode library. If you don't have multibyte support, install it: http://php.net/manual/en/mbstring.installation.php
I.e. for ubuntu like systems it's sudo apt-get install php7.0-mbstring for PHP7.
EDIT:
You could try 'auto' instead of 'sql_ascii', and btw., you can skip the count() call and can just write $converted_data[] = drupal_...
END EDIT
Comment: Go to /admin/reports/status/php and search for "Multibyte Support", should be "enabled" - then we need to search for other problems.
Comment: I found "Unicode library PHP Mbstring Extension" in admin/reports. How will I know if I have to install it?
Comment: 'extension=php_mbstring.dll' in php.ini was already enabled. I tried finding php_mbstring.dll at php/ext folder in windows and it does exist. But my problem still occurs.

Converting UTF8 Data to SQL_ASCII


Point $G_1$ represents the centroid of $\triangle ABD$ and point $G_2$ represents the centroid of $\triangle ABC$ in the trapezoid $ABCD$ $(AB||CD)$

Question:
Point $G_1$ represents the centroid of $\triangle ABD$ and point $G_2$ represents the centroid of $\triangle ABC$ in the trapezoid $ABCD$ $(AB||CD)$. Show $G_1G_2 \parallel AB \parallel CD$ and find $G_1G_2$ if $CD=a$ $cm$.

$G_1$ is the centroid of $\triangle ABD$ iff $\vec{G_1A}+\vec{G_1B}+\vec{G_1D}=\vec{0}$, but this does not help here. I am trying to show $\vec{G_1G_2}=\dfrac{1}{3}\vec{DC}$. Can someone help me?
Comment: I have a geometric method.
Answer: Since you wanted to see how to do it with vectors, The centroid of the triangle with vertices $\vec u, \vec v, \vec w$ is given by their average: $\frac 13(\vec u + \vec v + \vec w)$.
Therefore (choosing any arbitrary point as the origin, and identifying all the points with the vector from the origin to it): $$G_1 = \frac{(A + B + C)}3,\quad G_2 = \frac{(A + B + D)}3$$
And therefore $$\vec{G_1G_2} = G_2 - G_1 = \frac D3 - \frac C3 = \frac13(D - C) = \frac13\vec{CD}$$
Answer: Let $M$ is the midpoint of $AB$.
$\vec{G_1G_2}=\vec{G_1D}+\vec{DC}+\vec{CG_2}=\dfrac{2}{3}\vec{MD}+\vec{DC}+\dfrac{2}{3}\vec{CM}=\dfrac{2}{3}(\vec{MD}+\vec{CM}) + \vec{DC}=\dfrac{2}{3}(\vec{MD}-\vec{MC})+\vec{DC}=\dfrac{2}{3}\vec{CD}+\vec{DC}=\vec{DC}-\dfrac{2}{3}\vec{DC}=\dfrac{1}{3}\vec{DC}$
$\Rightarrow G_1G_2 || AB || CD$ and $G_1G_2=\dfrac{1}{3}CD$
Comment: What is $M$? And why is $\vec{G_1D} = \frac23\vec{MD}$ and $\vec{CG_2} = \frac23\vec{CM}$?
Comment: @PaulSinclair, $M$ is the midpoint of $AB$. $\vec{G_1D}=\frac{2}{3}\vec{MD}$, because $G_1D=\frac{2}{3}MD$ and they lie on the same line.
Comment: My point is that this information has to be part of your solution. Your solution is wrong without it. And how do you know that $G_1D = \frac 23 MD$ and $G_1, M$ and $D$ are colinear? If this has already been proven, then you need to explicitly reference that. If it hasn't, then the proof needs to be part of your solution as well.
Comment: @PaulSinclair, that's obvious. $G_1$ represents the centroid of $\triangle ABD$; thus, $G_1$ lies on every median. $M$ is the midpoint of $AB$, so $DM$ is a median and $DG_1=\frac{2}{3}DM$ (the centroid divides the length of each median in $2:1$ ratio).
Comment: How do you know that the centroid lies on every median? How do you know that that the centroid divides the length of each median in a 2:1 ratio? If you drew a picture of this and showed it to someone intelligent but who has not studied the geometry of triangles in any depth, would they agree that these two things are "obvious"? As a rule of thumb, anything that you cannot prove in 5 lines no wider than your hand does not qualify as "obvious".
Comment: @PaulSinclair, where the medians intersect is the centroid. The centroid divides each median into two parts, which are always in the ratio $2:1$. This is a fundamental basic property of centroids. I'll stop here. You may want to read this article - https://brilliant.org/wiki/triangles-centroid/.
Comment: You've completely missed my point. I am not trying to figure out why you think they are true. if you look at my post, the formula I gave also quickly proves it. What I am trying to get you to understand is that *this information needs to be included in your solution*. When you write out mathematics, you need to be aware of your audience, not just yourself. You have been studying this closely. The people you are trying to communicate it to have not. Even those who are aware of these results may not have them at the top of their mind. It is your job to remind them.
Comment: Figure out your audience is. Then subtract off a *minimum* of 1 year from your estimate of the lowest education level among them. Your job is to persuade them that the theorem is true. So you need to explain it to them carefully and clearly. Any time you make use of a result beyond basic mathematics that *everyone* knows, you should make explicit reference of that result. This is why I stated that the centroid is the average of the vertices before I made use of it. I didn't just assume you would know (in fact, from your remarks in the OP, I doubted that you knew).
Comment: @PaulSinclair, I understand what you're saying. I didn't get your solution because I have only studied vectors determined by two points. I have an elementary knowledge of vectors. I know that the equality $\vec{OA}+\vec{OB}+\vec{OC}=3\vec{OG}$ is true for every point $O$ if we have $\triangle ABC$ with centroid $G$. Why don't you use vectors in expressing $G_1$ and $G_2$? Shouldn't we write $\vec{G_2}=\dfrac{\vec{A} + \vec{B} + \vec{C}}{3}$?
Comment: Or $G_2 = \dfrac{(A + B + C)}3$?
Comment: There are many notations for vectors. You will find that almost universally, when mathematicians talk about vectors, there is a particular point designated as the origin, and all vectors eminate from it. This identifies vectors with points and allows you to treat them identically. You will also discover that denoting vectors wth a $\to$ on top is something that is rarely done after the 2nd year in college, Denoting them by boldface (for example, $\mathbf v$) tends to fade out as a common practice soon after. Basically, I overestimated your exposure to vectors, partly out of laziness.
Comment: I admit I mixed up your definitions of $G_1$ and $G_2$, so it should have been $$G_1 = \frac {A + B + D}3\\G_2 = \frac {A+B+C}3$$ You can interpret this as $$\vec{OG_1} = \frac {\vec{OA} + \vec{OB} + \vec{OD}}3\\\vec{OG_2} = \frac {\vec{OA}+\vec{OB}+\vec{OC}}3$$as you wish.


System of 6 variables and 5 equations with parameters

Question: $$\begin{align*}x_1 + x_2 &= a\\y_1 + y_2 &= b\\z_1 + z_2 &= c\\x_1 + y_1 + z_1 &= 0\\x_2 &= z_2\end{align*}$$
Solve for $x_1, x_2, y_1, y_2, z_1, z_2$ in terms of $a, b$ and $c$.
Comment: You will still have one free parameter because you have one too few equations.  The usual substitution technique will get you there.  What have you tried?  Where are you stuck?
Answer: Construct it as a matrix and just Gauss it down.
Comment: I tried that, but the matrix being 5x6 (not square) makes it a lot more difficult
Comment: @Koy Yes this is something you will have to live with. You have 6 unknowns and only 5 equations, it is unsolvable (you always need the same number of equations as the number of variables you are trying to solve for). In other words, you will have variables which you can only express in terms of other variables, a dependency on one another. The solution is not a point, it is a line or a space. To solve it exactly you need to guess a solution or use numerical methods.
Answer: You can either solve it by brute force, substituting between the simpler equations first which will eventually lead you to a solution.
An alternative method, if you are familiar with Linear Algebra courses, is expressing the system via matrices in the form $Ax=b$, where $A$ is the matrix of coefficients of the variables, $x$ the $1\times n$ matrix of $n$ variables and $b$ the $1\times n$ right hand matrix of the equations. After that, you can apply the Gauss Elimination Method to yield your solution.
Comment: Could you demonstrate the solution using the matrix? Because I've tried that but I end up circling and getting the same rows over and over again, just as with the brute force solution I keep getting the same equations.
Comment: @Koy Hi ! As you can observe from the last equation of your system, $x_2 = z_2$, you will have a free parameter because there are one few too much equations. That should not make you confused though, you can proceed with the solutions and let the extra parameter be a parameter !


Probabilty Models and distribution techniques

Question: Coliform bacteria are distributed randomly and uniformly throughout river water at the average concentration of one per twenty cubic centimeters of water.
Part (c)
In testing for the concentration (average number per unit volume) of bacteria it is possible to determine cheaply whether a sample has any (i.e. 1 or more) bacteria present or not.
Suppose the average concentration of bacteria in a body of water is λ per cubic centimeter. If 10 independent water samples of 10 c.c. each are tested, let the random variable Y be the number of samples with no bacteria. Find P (Y = y).
$P(Y=y) =$ $10\choose y$$(e^{-10λ})^y(1-e^{-10λ})^{10-y}$
Part (d)
Suppose that of 10 samples, 3 had no bacteria. Find an estimate for the value of λ
So basically using the equation in part (c), to calculate for part d, I'm guessing i sub $y$ as 3, but how do i get λ?
Comment: Can anyone figure this out?
Answer: The probability that of the $10$ samples , contain no bacteria is $\frac{3}{10}= 0.3$.
Using Poisson approximation, this probability equals $e^{−10λ}$.
Then $e^{−10λ}= 0.3$.


How to solve multicollinearity problem in a linear regression?

Question: I tried to do a OLS linear regression and got the following error message the end of the output:
"2 The smallest eigenvalue is  1e-25. This might indicate that there are strong multicollinearity problems or that the design matrix is singular."
I thought regression was supposed to solve/account for multicollinearity. Should I do PCA before regression and try this again?
How do I solve this error message? Is it even significant?
Input Data:

EDIT: Took advice from commenter.
Comment: Better to ask fewer questions and to edit in your thoughts about the answers to your questions.
Answer: Unless your data has more than two genders, including a constant and both male and female as indicators will be collinear.
Let $m_i$ be an indicator for male and let $f_i$ be an indicator for female. Observe that for any individual $i$
$$m_i + f_i = 1$$
hence if you also include a constant, your data is collinear.
Some solutions for collinearity:

If you have indicators for various categories (eg. month of the year), you should leave an indicator for one of the categories out of the regression. Coefficients on the remaining indicators will then be an estimate of the categories' mean relative to the left out category.
Think through the logic of your problem. You can have some subtle stuff such as age, years of schooling, and a constant being collinear if everyone starts school at the same age and doesn't drop out.
If you're doing machine learning with tons of variables, you may want to explore regularization.

Some further ideas and examples are explored here.
Answer: First of all, you should to be sure that you have multicollinearity. Check correlations between variables and use the VIF factor.
Then, if you want to solve multicollinearity reducing number of variables with a transformation, you could use a multidimensional scaling using some distance that remove redundancies.
PCA is useful when you have continuous variables, but I'm not sure that was good when your data is binary, so maybe you need to use MDS with binary ED or CorEx.


HP ProCurve Port Mode Configuration Question Mark 2

Question: We have a ProCurve Switch 2810-48G (J9022A).
We need to disable auto negotiation on two ports and manually configure them to be full duplex gige ports.
From the web GUI, Configuration Tab, Port Configuration sub tab, I am only presented with the option to configure the port as Auto - 1000. I take this to mean, auto negotiate duplex, manually configure the speed to be gige.
From the CLI when I try to set 1000-full I get the following error: Value 1000-full is not applicable to port 39 (or whatever port I try) The exact commands I have entered are:
config
interface 39
speed-duplex 1000-full
BTW: speed-duplex auto-1000 works ( I also tried full-1000 and that did not work either)
How do I manually configure the port such that it is manually configured to use full duplex, 1000 mbs?
Answer: There is no half-duplex in GigE - so set them to 1000-auto (its the same as 1000-full), also being GigE ports they should Autoneg perfectly fine removing the need to hardcode them at 1000, the days of vendors ended with GigE, Auto negotiation is a requirement for using 1000BASE-T according to the specs - as GigE works in a Master/Slave setup and they have to negotiate that. Why are you having to hardcode their speeds ?
Comment: That is what I though, actually.  I managed, however, to convince myself that I was imagining things though:  A CCIE in  a data center where we are doing an installation asked us to lock the port to 1000/full duplex and disable auto negotiation *and* there was that 1000-full option in the documentation.  HP support got back to me and confirmed its AUTO ONLY for gige

Perhaps cisco gear has some proprietary mode that does not auto-neg anything?


Rails Nested form - validate how many files are uploaded on form for same model field

Question: I have a nested form with multiple file field for :data. As you see below,'n' number of ApplicationFile model records are created where n=@company_templates.size. So in browser, if n=2 I see 2 upload buttons but how do I validate 'not nil' (either on model or DB side) if user has uploaded a file for each upload file_field button. Many thanks.
 <%= f.fields_for :application_files, @company_templates.size{ApplicationFile.new}, :html => {:multipart => true}  do |my_file| %>
  <%= my_file.file_field :data, label: " ", class: 'btn btn-file' %><% end %>

In fact, I've implemented not-nil condition for:data on the model side, but it bypasses it.
validates_presence_of :data, :allow_nil => false, :message => " - Please upload all required files for this job."

Answer: Ok so in a way I imposed this on user on form side, with the following as the model side validation didnot succeed maybe because it's a nested form.
<%= f.file_field :data, required: true %>


javax.persistence.PersistenceException: [PersistenceUnit: NG] Error while reading JPA XML file: META-INF/SQLQueries.xml

Question: I have tried several suggested methods to resolve this java persistence exception but have not been successful yet. Any suggestions or help would be greatly appreciated.
Below are the details
SQLQueries.xml
-----------------------------

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/orm_2_0.xsd "
    version="2.0">

    <sql-query name="GET_NEXT_NUM_SP" callable="true">
        <return alias = "nextNumber" class="com.utility.numbergenerator.da.NumberDAOImpl">
        <return-property name="nextSeqNum" column="NEXT_SEQ_NUM" />
    </return>
        { ? = call Test_Proc }
    </sql-query>

</entity-mappings>

persistence.xml
-----------------------

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="NG" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <mapping-file>META-INF/SQLQueries.xml</mapping-file>
        <class> Several related classes </class>
        <properties> Followed by properties </properties>
    </persistence-unit>

Stack trace of the exception

-- Initializing database connection --
  javax.persistence.PersistenceException: [PersistenceUnit: NG] Error while reading JPA XML file: META-INF/SQLQueries.xml
      at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1205)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1004)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
      at com.go2uti.oneworld.common.hibernate.HibernateUtil.initializeJPA(HibernateUtil.java:68)
      at com.go2uti.oneworld.common.hibernate.HibernateUtil.(HibernateUtil.java:38)
      at com.go2uti.oneworld.utility.businessidgenerator.da.EntityManagerFactorySingleton.init(EntityManagerFactorySingleton.java:26)
      at com.go2uti.oneworld.utility.businessidgenerator.compatibility.AbstractPersistentTest.initializeContext(AbstractPersistentTest.java:38)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.hibernate.InvalidMappingException: Unable to read XML
      at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:101)
      at org.hibernate.cfg.Configuration.add(Configuration.java:513)
      at org.hibernate.cfg.Configuration.add(Configuration.java:509)
      at org.hibernate.cfg.Configuration.add(Configuration.java:716)
      at org.hibernate.cfg.Configuration.addResource(Configuration.java:801)
      at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:152)
      at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1192)
      ... 34 more

Please help me resolve this issue.
Answer: Your META-INF/SQLQueries.xml file is based on JPA 2.0 XML mapping schema which is defined by http://java.sun.com/xml/ns/persistence/orm_2_0.xsd.
Since sql-query element is Hibernate specific you need to replace <entity-mappings> with <hibernate-mapping> as shown below:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <sql-query name="GET_NEXT_NUM_SP" callable="true">
        <return alias="nextNumber" class="com.utility.numbergenerator.da.NumberDAOImpl">
            <return-property name="nextSeqNum" column="NEXT_SEQ_NUM" />
        </return>
        { ? = call Test_Proc }
    </sql-query>

</hibernate-mapping>


How to manipulate large media files in Node.js in a non-blocking way

Question: I am currently creating a Node.js app that receives an audio/video stream, writes it progressively to the disk, then transcodes it with ffmpeg once the stream has ended and sends it somewhere else to be stored, deleting it locally.
Besides the fact that I can transcode the stream before writing it to streamline the entire thing (this feature is planned), what is the best way to handle these operations on potentially large files?
I am aware of spawing child processes (the method I'm currently using), but I'm not sure how they actually function, even after much reading. I'm not even sure using "spawn" is exactly what I want here (is "fork" a better option?).
Essentially, I want to know how to transcode -> upload -> delete the file without blocking Node.js so that multiple users can do the same thing simultaneously. Also, I am thinking of putting all 3 operations in a single bash script so that they happen synchronously in sequential order, is this fine?
Answer: the correct way to handle this is to use Streams - a built in class in NodeJS.
streams allows piping data gradually and doing work on 1 chunk at a time. this allows handling files larger than the computer's RAM or endless stream of data like in your case.
so instead of writing the data to the disk, you can process it on the fly, without blocking the thread (can transcode multiple streams simultaneously).
You haven't shared any code, but here is a general example.
const spawn = require('child_process').spawn;

const ffmpeg = spawn('ffmpeg -y -hide_banner -f mpegts -i pipe: -codec copy -f mpegts pipe:');
ffmpeg.stdout.on('data', chunk => {
    // upload a transcoded chunk
});

// if the source is also a stream you can pipe it directly like so
// sourceStream.pipe(ffmpeg.stdin);

function onAudioVideoChunkReceived(chunk) {
    // handle audio/video input to nodejs server
    ffmpeg.stdin.write(chunk)
}

Note the use of the special input/output pipe: of ffmpeg that allows pipe input and output through stdin and stdout
Comment: Thank you, but transcoding on-the-fly is less my concern for the moment than understanding if spawn holds up the event loop while--let's say--transcoding the file. Essentially, I want to know if it's possible for multiple users to connect to this Node.js server I made, where files are transcoded in a child process by using spawn, or will the Node.js process be held up (disregarding external limits, like memory available and such)?
Comment: it doesn't block the thread and you can run multiple pipe at the same time. edited the answer to reflect that.
obviously this scales only up to a point where your CPU/memory/network are saturated.
Comment: Thank you, that's exactly what I wanted to know!


Datomic pull expression

Question: I was reading about the Datomic pull syntax here:
http://docs.datomic.com/pull.html
And it explains under wildcard that a pattern like [*] will pull an entity and recursively pulls its entity attributes. Direct quote:

The wildcard specification * pulls all attributes of an entity, and recursively pulls any component attributes:

;; pattern
[*]

;; result
{:release/name "The Concert for Bangla Desh",
 :release/artists [{:db/id 17592186049854}],
 :release/country {:db/id 17592186045504},
 :release/gid #uuid "f3bdff34-9a85-4adc-a014-922eef9cdaa5",
 :release/day 20,
 :release/status "Official",
 :release/month 12,
 :release/artistCredit "George Harrison",
 :db/id 17592186072003,
 :release/year 1971,
 :release/media
 [{:db/id 17592186072004,
   :medium/format {:db/id 17592186045741},
   :medium/position 1,
   :medium/trackCount 2,
   :medium/tracks
   [{:db/id 17592186072005,
     :track/duration 376000,
     :track/name "George Harrison / Ravi Shankar Introduction",
     :track/position 1,
     :track/artists [{:db/id 17592186048829} {:db/id 17592186049854}]}
    {:db/id 17592186072006,
     :track/duration 979000,
     :track/name "Bangla Dhun",
     :track/position 2,
     :track/artists [{:db/id 17592186048829}]}]}
  ...
  ]}

However, when I try this myself I only get db/id values for ref types. I have to basically call out any ref type in a more verbose way in the pull expression, with something like this:
[* {:content/type [:db/ident] :content/locales [:db/ident] :content/groups [*]}]

And that's not even actually the end of it, because :content/groups itself has ref types inside of it. Did the behavior of wildcarding change at some point and the docs are not up to date? Or am I doing something wrong? Is there a better (more concise) way to go about saying, "just recursively pull it all in"?
Answer: The wildcard in a pull expression only populates component attributes. That is, attributes with :db/isComponent true in the schema.
Looking at the mbrainz schema, only :release/media and :medium/tracks are component attributes, so they would be the only ones populated with a [*] pull pattern.
If your data model includes hierarchal reference chains (i.e. :person/friend as a reference to any person, who may also have friends referenced), you can define recursive pull patterns and specify their recursive depth (or use ... for unbounded).
This topic is also discussed on the Datomic Google Group, and includes a link to an example recursive pull pattern.
Comment: so is there no easy way to "automatically" pull in refs for enum values? As an example, in a given component, I may have 4-5 enums in various places, and generally a `(pull ?c [*])` suffices, except for the enums which then create this really annoying and long pull expression. It almost (_almost_) makes me want to just stick strings in there and to heck with the enums.
Comment: from what I can tell, all enums have the same `:db/id`, so if a record got retracted that `:db/id` would also get retracted. So it seems like its desirable to have enum values be `:db/isComponent true` because I don't want to manually specify every single enum on a pull expression, but on the other hand they're by definition shared across entities, and so not suitable for `:db/isComponent`. Is that correct?
Comment: ok, is there any reason why I might choose to not use `:db/isComponent true`, if my intention is to get the whole thing pulled in each time?
Comment: So reading from here: http://blog.datomic.com/2013/06/component-entities.html it seems like components are to take care of sub-objects that don't exist on their own without the parent. Does it make sense to make an enum value have `:isComponent true`?
Comment: Correct - component entities should be things that only exist as part of their parent.
Comment: This discussion: https://groups.google.com/d/msg/datomic/KI-kbOsQQbU/2ZiLH8TmXdkJ provides additional details you might find helpful.

Datomic pull expression


Dynamically changing JPanel size

Question: How to dynamically change the size of a JPanel depending on its contents which is a dynamically changing Popup with various size possibilities.
Comment: Have you tried calling `relayout` or `repaint` after the change has occurs?
Answer: Call revalidate on the JPanel after the size has changed. This will force the layout manager to run again.
Answer: In the simplest scenario you'd set a new preferred size, invalidate, validate again and finally request a repaint (or simply call SwingUtilities.updateComponentTreeUI on the panel) but the final say about the panel's size (and position, too) rests with the layout manager of the parent container; the look and feel may play a role, too. An alternative way to set the size is to [re]set the sizeVariant property of the component (a kind of size hint) and then as above, with the same caveats. Thus the answer to your question is: it depends.
If you are a tester and you did not write that code I realize that doesn't help you much. In that case you need to talk to its developer.

Dynamically changing JPanel size


What characteristics of a piece contribute to defining its tonal centre?

Question: I have been wondering since a long time that what exactly in a piece defines the scale and the tonal center. Even if we are given a chord progression that is followed through the entirety of the song, that information is not sufficient to predict the scale right? There can be 'other factors' that govern which specific note of the 12 notes that we have would be the tonal center. I just want to know what all comes under that list of 'other factors' and in what way each one contributes.
While I still had this thought process going on, I came across this video.

Can it be concluded from this video that the tonal center of the piece is independent of the melody? So is the rhythm alone sufficient to determine the tonal center?
So, if suppose for a song I have one single track of raw vocals without ANY kind of instrumental accompaniment. Will such a piece not have any tonal center? Can accenting on particular syllables contribute in any way? Can multiple harmony tracks be added to help narrow down to one unambiguous tonal center?
And if the melody indeed doesn't play any part, can one compose a piece where the melody has keys from a particular scale, but tonal center is made to be a note not belonging to the scale by playing appropriate bass and chords?
Only to make the question clearer, I am taking an example, please do not restrict your answer to this specific example. Can one compose a piece with the melody having notes strictly from the C major scale, having the tonal center as B flat?
Can anyone please enlighten me or guide me in an way that would clear these queries? A really big thanks for all commenters in advance!
Answer:
Even if we are given a chord progression that is followed through the entirety of the song, that information is not sufficient to predict the scale right?

Actually, that is the typical way to know the tonal center, the tonic.
You basically look for cadences or harmony that gives a sensible dominant thereby implying a tonic. The use of cadences is pretty formulaic. That's intentional to make form clear. Even when phrases drift around with harmonic ambiguity it's pretty common for cadences to clear up things tonal at the end of those phrases.
Alternately, you could use rhythm to emphasize a center. Hold a particular tone for longer durations, target it for phrase endings, etc. to make it the focus. This is basically what happens in the video. I takes a key signature of no sharps, no flats and just switches chords. The chords are sustained so each one in turn takes on the focus.
Another thing that could be tried, and is hinted at in the video, it use root position chords for the tonal center chord, and inverted chords for all the other roots. The idea is root position chords are more stable than inverted chords, the tonic should be stable, so use stable inversions for the tonic only.
You could do something similar with dissonance too. Keep the tonic chord a simple triad so it will be consonant and stable, but allow various levels of dissonance on other chords to make them less stable. That can create a dynamic sense of the instable, dissonant chords "pushing" toward the stability of the tonic.
These things sort of mirror the traditional sense of tonic where the "unstable" dominant moves to the stable tonic. But instead of following the traditional harmony of V resolving to tonic I, you could have some other "unstable" chord complimenting I.
Answer: The tonal center of a piece cannot be independent of its melody, and rhythm alone cannot determine a tonal center. Drum cadences are made purely of rhythmic unpitched drum parts, and they are, predictably, atonal.
However, a monophonic, single track of a capella raw vocals with no accompaniment can - and often will - still have a tonal center. Take the first few measures of a melody-only rendition of "Joy to the World" that starts with C, for example - it starts with a descending C major scale with C notes lengthened, and then only notes in the C major scale appear afterwards. It's very hard to say that "Joy to the World" that starts with C is not in C major.
"Joy to the World" is arguably an abnormal case, but the single melody track will often narrow down the tonal center to a few possibilities at most. The tonal center must not clash with the notes in the melody, especially the most strongly emphasized ones. (The second part of my last sentence is important for those melodies with all 12 notes in the chromatic scale, such as the melody of "You Will Know Our Names" from Xenoblade Chronicles.) Even a single-note drone will yield that its most likely tonal center is that note.
Note that I implied above that the tonal center of a melody can often be ambiguous, but be restricted to a few possibilities. Multiple harmony tracks can indeed be added to help narrow down to one unambiguous tonal center. For example, the normally D-major regimented melody near the start of Edward Elgar's Pomp and Circumstance March No. 1 in D Major is reharmonized into B minor in "In the Whirlpool" from Dance Dance Revolution: Mario Mix. I bet you anything that reharmonizing that melody into D sharp minor or A flat major without changing any of the notes is going to be extremely hard - this is because those keys prominently use notes not in that melody such as D sharp and A flat.
You can compose a melody made of notes strictly from the C major scale and have the tonal center be B flat. In order for that tonal center to be convincing instead of giving listeners the impression that you're "playing outside" the chord progression, your choice of notes from the C major scale will probably be somewhat restricted. You'll probably avoid E and B (except possibly as non-accented passing tones and neighbour tones) because those notes are neither in the B flat major scale nor the B flat minor scales.
My example "melody made of notes strictly from the C major scale with a B flat tonal center" is F-G-F F-G-F F-G-F D-C-D, which can be harmonized with a chord progression of B♭ B♭ F7 B♭. Take notice that every note in my example melody can be found in both C major and B flat major, though.
It is still true, though, that a chord progression that a piece strictly uses throughout may not be sufficient to predict the scale - especially its tonic. Depending on where the chord progression starts and ends, the tonal center can be different. It's easier to interpret a chord progression of looped Am F C G as being in A minor and one of looped C G Am F as being in C major instead, especially if the former progression ends with an Am chord and the latter with a C chord. Do note that A minor and C major scales share most of, if not all, their notes.
A monophonic melody can easily have a key that includes all the notes in that melody, especially its most strongly emphasized (longest, often repeated, stressed at cadences) notes, and its tonal center will fit in that key. Accenting on particular syllables can easily change which notes are the most strongly emphasized and therefore which notes fix down the tonal center. The same applies for a chord progression - it can easily have a key that includes all the notes in all its chords, especially root notes of chords and contents of chords that end cadences, and its tonal center will also fit that. However, it is very hard to perhaps impossible to compose a piece where the melody has notes from a particular scale, but the tonal center is made to be a note not belonging to the scale, because it is very hard to impossible to find the appropriate bass and chords that both switch the tonal center to a note that is not in any major or minor scale with all the notes of the melody in it and do not give off the impression that you are "playing outside" and refusing to fit the chord progression. ("Playing outside" often, if not always, involves dissonant intervals between melody and chord notes.)


Google Distance Matrix need to return the value

Question: I have using google distance matrix api to getting the shortest distance of two geo points from the following way:

On this code I have get the shortest distance successfully. But I need to return the value when calling the method calculateDistances() instead of alert the value. How can i do in callback methods!.
Comment: Callback mean to be execute not for return values
Comment: how can i change this method to return method
Comment: Why do you thing you need to "return" the data? The DirectionsMatrix is asynchronous, you can't return anything from its callback function, you have to use the data in the callback function when it is available.  You haven't told use what you are trying to do with the returned data.
Comment: I need to return the data and store them in an array
Comment: I added an updated answer based on the comments here, just in case you still need one. Best of luck!
Answer: Distance Matrix service is asynchronous, for that reason, you need to pass a callback method to execute upon completion of the request, to process the results. But you could consider to convert async API to promises.
The following example demonstrates how to convert Distance Matrix service API to promises using jQuery:

var origin1 = new google.maps.LatLng(9.6667, 80.0000);
var destinationA = new google.maps.LatLng(9.6689, 80.0059);

function calculateDistances(origins,destinations) {
            var service = new google.maps.DistanceMatrixService();
            var d = $.Deferred();
            service.getDistanceMatrix(
                {
                    origins: origins,
                    destinations: destinations,
                    travelMode: google.maps.TravelMode.DRIVING,
                    unitSystem: google.maps.UnitSystem.METRIC,
                    avoidHighways: false,
                    avoidTolls: false
                },
                function(response, status){
                  if (status != google.maps.DistanceMatrixStatus.OK) {
                     d.reject(status);
                  } else {
                     d.resolve(response);
                  }
                });
            return d.promise();
}

        calculateDistances([origin1],[destinationA])
           .done(function(response){

                var origins = response.originAddresses;

                for (var i = 0; i < origins.length; i++) {
                    var results = response.rows[i].elements;
                    for (var j = 0; j < results.length; j++) {
                        //console.log(results[j].distance.text);
                        document.getElementById('result').innerHTML += results[j].distance.text;
                    }
                }

           })
           .fail(function(status){
              document.getElementById('result').innerHTML = 'An error occured. Status: ' + status;
           });

Answer: I would make an array as a global variable with the number of elements as well a global variable. Then, in the callback I would increment the number of elements and set the current array element as the distance.
Hope this answers your question. Best of luck!


Find SQL 2008 setup.exe for command line uninstall

Question: I'm writing a little .Net app that stores data in a named SQL instance. I need to be able to uninstall the instance programatically. It's easy enough to call setup.exe with the uninstall parameters (I figured them out in this post: https://stackoverflow.com/questions/16068490/how-do-i-remove-an-sql-2008-instance-from-command-prompt-w-o-removing-the-server), but the hard part is reliably finding setup.exe.
I've found copies of setup.exe in
C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\
C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\SQLServer2008R2\

and finally I found "setup100.exe" in
C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\SQLServer2008R2\x86

How do I know which one to use? So far I've found I have to use the one in:
C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\

but I'm still not sure why I found a setup.exe in that SQLServer2008R2 folder.
There are ways to query for the uninstall command but they're unreliable when you uninstalling a named instance... :(. Win32_Product works but it breaks things when it's called. Windows.Installer logs everything that was ever installed so if I've had a few versions of SQL installed (surprisingly common) I can't tell which to use.
Right now I'm just querying the DB for it's install location, using what I find in
"\100\Setup Bootstrap\setup.exe"

and hoping for the best :P.
Edit: Found another one :P
"C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Release\x86\setup100.exe"

Answer: You can look in the registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\Bootstrap

The key you want is BootstrapDir.
FWIW, I don't have either (setup100.exe or setup.exe) in the Setup Bootstrap folder, only in the SQLServer2008R2 subfolder. This is perhaps because I don't have SQL Server 2008 installed, only 2008 R2.
So what do you plan to do when the file you're after is not found (either because 2008 isn't already installed, or because the user deleted it in an ill-advised attempt to free up some space)?


C2372 Redefinition; different types of indirection

Question: I am trying to define/declare a int array varible in one of my other files.
Other_file.h
#pragma once
extern int* Test = new int[2]; // <- Here I declare(at least by my understanding)

Other_file.cpp
#include <iostream>
#include "Other file.h"

int* Test[2]; // <- Line that causes error...

Then the Main.cpp
#include <iostream>
#include "Other file.h"

int main() {
    std::cout << Test;
    return 0;
}

Each time I get this error: Error   C2372   'Test': redefinition; different types of indirection | Other File.cpp | line 4
FYI I use MSVC 2019.
Comment: As soon as you initialize, you go from declaration to definition and undo the `extern`.
Comment: Ok thanks, that is good to know
Comment: I should say mostly undo the `extern`.  You do have a definition rather than a declaration, but the external linkage remains.
Comment: [Here's a discussion of the above and a possible use-case](https://stackoverflow.com/questions/32872607/use-case-for-initialized-extern-variable).
Answer: extern int* Test = new int[2]; // <- Here I declare(at least by my understanding)

No, that's a definition. This would be the declaration:
extern int* Test; // note, no value

As well, the two declarations are incompatible, which is what the error is trying to tell you. int*[2] and int* are completely different.
Comment: Where did you add `Test[]`?
Comment: Great that worked just had to add ```Test[]``` instead of ```Test```. Thanks a lot!
Answer: This declaration
extern int* Test = new int[2];

also a definition of a pointer with external linkage because the pointer is initialized.
Then in the source file "Other_file.cpp" you redefined the name Test as an array of two elements of the type int *.
#include "Other file.h"

int* Test[2];

So the compiler issues an error.
In fact the above code snippet is equivalent to
extern int* Test = new int[2];
int* Test[2];

If you want to declare a pointer then in the header "Other file.h" declare it like
extern int* Test;

and then in the source file "Other_file.cpp" write
#include "Other file.h"

int *Test = new int[2];


Remove title from ScatterChart

Question: I would like to know how to be able to assign a name just to a serie of data (using a scatterchart), without getting the same serie's name as title of the chart. I would like to get just the series name as legend and NOT chart title at all.
I realized that autoTitleDeleted needs to have a value of 1.
So, consulting documentation I found that ´chartContainer´ is the class to implement the aforementioned option. Therefore, I import the class and apply it as next:
from openpyxl import Workbook
from openpyxl.chart import (
        ScatterChart,
        Reference,
        Series,
)
from openpyxl.chart.chartspace import ChartContainer

wb = Workbook()
ws = wb.active

rows = [
        ["Size", "Batch 1", "Batch 2"],
        [2, 40, 30],
        [3, 40, 25],
        [4, 50, 30],
        [5, 30, 25],
        [6, 25, 35],
        [7, 20, 40],
]

for row in rows:
    ws.append(row)

chart2 = ScatterChart()

chart2.x_axis.title = "Size"
chart2.y_axis.title = "Percentage"

xvalues = Reference(ws, min_col = 1, min_row = 2, max_row = 7)
values = Reference(ws, min_col = 3, min_row = 1, max_row = 7)
series1 = Series(values, xvalues)
chart2.series.append(series1)

chart2 = ChartContainer(autoTitleDeleted = 1)

ws.add_chart(chart2, "J10")

wb.save("Ex1.xlsx")

However, next error comes up:
`runfile('C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py', wdir='C:/Users/Administrador/Desktop/Pre-Try/Ex1')
Traceback (most recent call last):
File "", line 1, in
    runfile('C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py', wdir='C:/Users/Administrador/Desktop/Pre-Try/Ex1')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py", line 46, in
    wb.save("Ex1.xlsx")
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save
    save_workbook(self, filename)
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook
    writer.save()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 276, in save
    self.write_data()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 76, in write_data
    self._write_worksheets()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 219, in _write_worksheets
    self._write_drawing(ws._drawing)
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 142, in _write_drawing
    self._archive.writestr(drawing.path[1:], tostring(drawing._write()))
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 296, in _write
    self._rels.append(rel)
UnboundLocalError: local variable 'rel' referenced before assignment`
I do not really understand this error. If you could help would be grateful!
Comment: You need to include the complete traceback with the error, though I don't think it is related to what you're doing here.
Comment: Thank you for your help @CharlieClark. I have edited the question with traceback. Hope this could help to understand where the problem is.
Answer: chart2 = ChartContainer(autoTitleDeleted = 1)
This the problem: you must use openpyxl chart objects so that the library can manage the plumbing between objects: charts are very complicated objects and we try and hide some of the complexity Your idea is correct - to set the value to True – but that won't work like this because a ChartContainer doesn't know how to add itself to the XLSX package.
As a workaround it's probably easiest create a title with an empty string. Otherwise you could submit a PR that allows the mapping of the autoTitleDeleted attribute from chartContainers to charts and vice versa.
Comment: Hi @Charlie. Thank you for your answer. Nonetheless, the workaround you commented does not resolve the problem because the "title space" is still there (although without any word), so the plot area is smaller than it shoud be. Is there any other way of removing the title?
Comment: No, not without making changes to the library as I've described.
Comment: Thanks for your quick answer @Charile. Yes, I have read your other option. However, I am not sure whether I have enough knowledge and experience to do that..

Remove title from ScatterChart


Add two integer fields and assign as default to another field

Question: So i have a model with three fields (A, B, C) I want C to have a default value of A+B.
Anyway i can use a callable value for default or maybe aggregation within models.py?
How do i do this?
Comment: This is a great candidate for a `pre_save` signal, or even the `save` method
Answer: As karthkir said, this is the sort of thing you should be doing in the save method.
<code>class MyModel(models.Model):
    ...
    def save(self, *args, **kwargs):
        if not self.C:
            self.C = self.A + self.B
        return super(MyModel, self).save(*args, **kwargs)
</code>
Comment: Anyway we can do it dynamically? While user is inputing data it gets updated?
Comment: If you write some JavaScript, yes. Nothing to do with Django in any case.
Comment: Thanks, I think your suggestion would suffice for now. I'll get to the ui design after.


Edit Drupal Form in "popup" just like it is done in Drupal Views Edit form

Question: I am making replica of views module. We when navigate to view in admin/list/structure, we add new view. Here we type name of our view and content type. After clicking "save and continue", it is redirected to view edit page.
On the top, there is,
Title
title:None
And when we click on "None", a popup box will appear to edit title of the view and replaces "None" with the title given by us.
I need to have this edit in popup functionality. Can anyone please help doing it.
Answer: I think what you are looking for is the ctools modal functionality.
Without further digging into that - because I think the following tutorial is comprehensive, easy to understand and complete - here is a link that will help you: https://www.deeson.co.uk/labs/insert-form-pop-modal-ctools-and-drupal-7
Some important things you should not forget:

Add the ctools module as dependency.
Add the callback to the menu.
Include the ctools js files on the page where the link to the title popup is.

ctools modal helped me a lot! Give it a try!


Showing ObservableCollection item changes using CommunityToolkit.Mvvm in .NET MAUI

Question: I went through SO questions on this topic and thought I followed the examples carefully but I still don't see a UI update when a property of an item in a CollectionView gets updated.
First, I made sure the property in my model is decorated with ObservableProperty:

public partial class Student : ObservableObject
{
   public int Id { get; set; }

   public string Name { get; set; }

   [ObservableProperty]
   bool isRegistered;
}

I then use an ObservableCollection in my view model which is also decorated with ObservableProperty:

public partial class MyViewModel : ObservableObject
{
   public MyViewModel()
   {
      var student = new Student
      {
          Id = 123,
          Name = "John Doe",
          IsRegistered = false
      };
      Students.Add(student);
      student = new Student
      {
          Id = 234,
          Name = "Jane Doe",
          IsRegistered = false
      };
      Students.Add(student);
   }

   [ObservableProperty]
   ObservableCollection<Student> students = new();

   [RelayCommand]
   void UpdateRegistrations()
   {
      foreach(var item in Students)
         item.IsRegistered = true;
   }
}

Here's the XAML:

<ContentPage...>
   <Grid
      RowDefinitions="*,50">

      <CollectionView
         Grid.Row="0"
         ItemsSource="{Binding Students}">
         <CollectionView.ItemTemplate>
            <DataTemplate
               x:DataType="model:Student">
               <Grid
                  ColumnDefinitions="*,30">

                  <Image
                     Grid.Column="1"
                     Source="checkmark.png"
                     IsVisible="{Binding IsRegistered}" />
               </Grid>
            </DataTemplate>
         </CollectionView.ItemTemplate>
      </CollectionView>

      <Button
         Grid.Row="1"
         Text="Update Data"
         Command="{Binding UpdateRegistrationsCommand}" />

   </Grid>
</ContentPage>

I don't see the change in IsRegistered reflected in the UI after executing UpdateRegistrations(). What am I missing here? I'm under the impression that CommunityToolkit.Mvvm handles INotifyPropertyChanged logic. Do I need to handle the change manually?
Comment: Where is the relevant XAML?
Comment: Nothing fancy going on in the XAML but updated the original post with the XAML code.
Comment: If you default the value to true does it show?
Comment: If I set the default value to `true`, the `Image` shows. I also reversed the logic so that when the `Button` is tapped, I set `IsRegistered` to `false`. Again, the UI doesn't reflect the change.
Comment: What you've done should work; you are correct that ObservableObject/ObservableProperty should propagate that update to UI. Seems suspiciously like a bug with IsVisible. As a test, add another element that is affected by IsRegistered, but not involving IsVisible: ``. SIDE NOTE: Does `IsVisible={Binding IsRegistered}` even compile? I'm used to seeing double-quotes.
Comment: Sam, please read the comment I've added to the accepted answer. That answer has a partially incorrect description. Don't want you misled.
Answer:
ObservableCollection doesn't need the [ObservableProperty] attribute; it automatically invokes NotifyPropertyChanged and NotifyCollectionChanged when you call its methods like .Add(). That's kinda the whole reason we use ObservableCollection<T> instead of List<T> for MVVM.

In .NET MAUI (and Xamarin), View Model properties used for bindings must be a public non-static property, so we'll promote Students from a field to a read-only property (aka add { get; } ).

In the XAML, the DataTemplate bindings are using incorrect syntax: the Name binding is missing the Binding keyword, and both should be wrapped in double-quotes ("). These bugs should be invoking a compiler (syntax) error when XAMLC is enabled, so make sure you haven't accidentally disabled XAMLC.

public partial class MyViewModel : ObservableObject
{
   public MyViewModel()
   {
      Students.Add(new Student
      {
          Id = 123,
          Name = "John Doe",
          IsRegistered = false
      });

      Students.Add(new Student
      {
          Id = 234,
          Name = "Jane Doe",
          IsRegistered = false
      });
   }

   public ObservableCollection<Student> Students { get; } = new();

   [RelayCommand]
   void UpdateRegistrations()
   {
      foreach(var item in Students)
         item.IsRegistered = true;
   }
}

<ContentPage...>
   <Grid
      RowDefinitions="*,50">

      <CollectionView
         Grid.Row="0"
         ItemsSource="{Binding Students}">
         <CollectionView.ItemTemplate>
            <DataTemplate
               x:DataType="model:Student">
               <Grid
                  ColumnDefinitions="*,30">

                  <Image
                     Grid.Column="1"
                     Source="checkmark.png"
                     IsVisible="{Binding IsRegistered}" />
               </Grid>
            </DataTemplate>
         </CollectionView.ItemTemplate>
      </CollectionView>

      <Button
         Grid.Row="1"
         Text="Update Data"
         Command="{Binding UpdateRegistrationsCommand}" />

   </Grid>
</ContentPage>

Comment: "3" is the actual bug fix. 1 and 2 are moot. [ObservableProperty] does the equivalent. Also, 1's explanation is partially wrong. ObservableCollection DOES NOT call NotifyPropertyChanged when `.Add`. That add does not change the value of the property; the property still points to the same collection. The only special feature of ObservableCollection is its NotifyCollectionChanged. [In fact, if code never does Add/Remove/etc, instead REPLACING the entire collection whenever there is a change, then List works just as well. There are some use cases where replacement is better performance.]


Mostrar uma unica DIV com Ajax

Question: Estou fazendo uma requisição ajax para uma URL externa e quero mostrar apenas três elementos do site original na pagina que Eu desenvolvi.
Porém ao executar o código ele traz todos os elementos da pagina.
Da pagina em questão Eu quero mostrar apenas os elementos "Usuário (user)" "Senha (password)" e o botão "Ok", como Eu faço para mostrar apenas estes 3 elementos na pagina?
Obs.: Quero mostrar os elementos mesmo e que ao colocar usuário e senha e clicar em OK ele seja direcionado para a URL original, siga o fluxo normal de login.

    <title>Exemplo AJAX</title>

Exemplo Ajax

Answer: Você pode converter o retorno da requisição em html e então extrair os dados que você quiser. Por exemplo:

var $log = $("#log"),
  str = "hello, my name is jQuery.", /* Aqui seria o retorno da requisicao */
  html = $.parseHTML(str), /* Aqui converte o texto em HTML para o jquery poder extrair informacoes */
  nodeNames = [];

$log.append(html);

$.each(html, function(i, el) {
  nodeNames[i] = "
" + el.nodeName + "
";
});

$log.append("
Node Names:
");
$("

")
  .append(nodeNames.join(""))

Content:

Abaixo exemplo de como ficaria:
$(function () {
    $("#btn-ajax-jquery").click(function () {
        $.ajax({
            type: 'POST',
            url: "http://www.site.com.br",
            dataType: "html",
            beforeSend: function () {
                $("#resposta").html("Aguarde...");
            },
            success: function (data) {
                var html = $.parseHTML(data); /* Aqui converte o texto em HTML para o jquery poder extrair informacoes */

                /* Com a varivel html recebendo o conteudo da outra página, agora é só buscar os dados do html. Para isso vc pode usar o próprio Jquery */

                $("

").append(nodeNames.join(""));
            },
            error: function (err) {
                console.log("Error: " + err.status);
                console.log("Error Message: " + err.statusText);
            }
        });
    });
});

Mostrar uma unica DIV com Ajax


How do I report an Ansible command/shell task as changed in check mode?

Question: In my Ansible playbook I have this:
- name: "A: Check to see if we need to run task B"
  [... implementation omitted, not relevant ...]
  register: task_a_result
  check_mode: no  # even run in check mode
  changed_when: no  # this only reads/checks stuff

- name: "B: Write x to file"
  shell: "echo {{ my_var|quote }} > /path/to/file"
  when: task_a_result.stdout_lines[0].startswith('ABCDEF')
  changed_when: yes  # when run, it always changes the state

I do not want to run B in check mode (hence, no check_mode: no), but I want to report it as changed if it would have been run in non-check mode. I want this behaviour, because I do not want surprises when running in non-check mode. However, despite having changed_when: yes and having set a conditional myself, Ansible keeps showing the task as skipped and thus unchanged to me:

skipping: [myhost] => changed=false
  msg: skipped, running in check mode

(The above in check mode, and it reports 'changed' in regular non-check mode.)
I found this bug report that appears misinterpreted and closed inappropriately: 14950, but I cannot comment on that any longer.
Am I overlooking something basic? Other modules usually report a "would have changed" status perfectly fine in Ansible, but is this also possible for shell/command?
Using Ansible 2.7.12 and 2.8.2 yields the same results.
I am hoping to avoid nasty hacks in the command itself like:
- name: "B: Write x to file"
  shell: "echo {{ my_var|quote }} {{ '>' if task_a_result.stdout_lines[0].startswith('ABCDEF') else '' }} /path/to/file"
  when: task_a_result.stdout_lines[0].startswith('ABCDEF')
  changed_when: yes

And yes I know I can write to a file with the copy/template modules, but I can't seem to overwrite files with it, e.g. echo 1234 > /sys/module/zfs/parameters/zfs_arc_max, because copy/template would attempt to replace the file it seems and setting a kernel parameter wouldn't work like that. And no, this kernel module parameter is not exposed through sysctl on Linux.
Comment: Why does it matter?
Comment: @MichaelHampton Because in a check mode, I'd like to know if something would change if I run it in non-check mode. IOW, without Ansible, if you would run `cmd --dry-run` which outputs "nothing changed" and you would then do `cmd`, and it *would* change something, this would be a bug.
Comment: But you've set the play to always report changed even if nothing changed (which is redundant). It certainly looks like the actual result is irrelevant, doubly so in check mode. What is missing here?
Comment: @MichaelHampton `when: my_conditional_var` determines whether the task is going to run, and if it runs, it will change. I want to see that in check mode. Perhaps I could improve the wording in the Q? It's basically check mode reports "I'm not going to change anything" and in non-check mode it *will* change stuff. That's what I want to fix.
Comment: In the example you gave, it doesn't actually matter! You can just ignore it. Are you doing something else?
Comment: @MichaelHampton I don't quite get what seems to be lost from my head to my post then. :) Anyway, updated the question again (rev 2) with more explicit wording. Thanks.
Answer: I know this is kind of old, but I'm looking for the solution to this problem. I'm also tired of surprises with command module as OP.
What if we change the 'command' part so that it executes a different command in check mode. And we run the task always, even in check mode.
Note: Before you complain, I know this is a bad example. I know there is a 'creates' option in 'command' module. This is just to give an example.
This is my solution so far:
- name: Get some info
  stat: {path: /tmp/somefile}
  register: file_info

- name: Run the command conditionally
  command:
    "{{ 'true' if (file_info['stat']['exists'] or ansible_check_mode)
    else 'touch /tmp/somefile' }}"
  changed_when: not file_info['stat']['exists']
  check_mode: false

It works, but is bloated.
Please tell me if there are better solutions.
Comment: Yes, thanks, meanwhile I always add a `--dry-run` argument conditionally if the command executed supports that. If the command supports it, it's even better, because it could test for failures and reduce the level of surprise when run in non-check mode. However, I'm still disappointed I haven't found a way to report-changed-but-not-run as task in Ansible check mode.
Answer: I have a similar need. I would like to see if the command would be run so that I get no surprises when not run in check mode.
As a workaround I added a debug task reporting that there would be a change and used the when condition from the command task to set changed_when on the debug task, i.e.
- name: "Report pending change"
  debug:
    msg: "Change pending"
  changed_when: task_a_result.stdout_lines[0].startswith('ABCDEF')
  when: ansible_check_mode|bool


Series Equaling Euler–Mascheroni Constant

Question: Is there a known series equaling the Euler–Mascheroni Constant? And if there is this, wouldn't that imply that the Harmonic series plus this new series equal a series that is exactly $\ln(x)$? I have been trying to derive the series  using the infinite product representation of $\ln(x)$ and manipulate it to get a series which I then could subtract that from the Harmonics to get my series, but it lead no where.
Comment: Note that $\sum_{k = 1}^n {\frac{1}{k}}  \ne \ln n + \gamma$. You can find several representations [here](https://en.wikipedia.org/wiki/Euler%27s_constant). For series expansions of $\ln$, see [here](https://en.wikipedia.org/wiki/Logarithm#Power_series).
Answer: Yes, there is; e.g.
$$\gamma = \sum_{k=1}^\infty \left( \frac{1}{k} - \log \left(1 + \frac{1}{k}\right)\right). \tag{1}$$
But this is just a rephrasing of the definition $$\gamma = \lim_{n \to \infty} H_n - \log n, \tag{*}$$ the proof of which I leave as an exercise.  So you gain nothing by using this particular series representation.
There are other series though:
$$\gamma = \sum_{n=2}^\infty (-1)^n \frac{\zeta(n)}{n} \tag{2}$$ as well as
$$\gamma = 1 - \sum_{n=1}^\infty \sum_{k = 2^{n-1}}^{2^n - 1} \frac{n}{(2k+1)(2k+2)}. \tag{3}$$
Reference:  https://mathworld.wolfram.com/Euler-MascheroniConstant.html
Comment: The first series converges quicker than the limit definition, so in a way you do gain something.


Ionic: Make checkbox checked in event handler if specific criteria are met

Question: I have a form with checkboxes and for certain checkboxes the following applies:
When a checkbox is checked and gets clicked and some statement is true, then the checkbox should not get unchecked, but jump back to checked state.
This is the current html:
<ion-item>
    <ion-label stacked  [innerHTML]="htmlString"></ion-label>
    <ion-checkbox (click)="checkboxClick($event)"></ion-checkbox>
  </ion-item>

And this is the current .ts:
checkboxClick(e){
    var statement = true;

    if(statement){
    e.target.checked = true;
    }
  }

Here is a stackblitz showcasing the issue: https://stackblitz.com/edit/ionic-tcxd55?file=pages%2Fhome%2Fhome.ts
Comment: This is not the right stackblitz
Answer: Use (ionChange) instead of (click)
html
<ion-checkbox (ionChange)="checkboxClick($event)" checked></ion-checkbox>

ts
  checkboxClick(e){
    var statement = true;
    if(statement){
      e.checked = true;
    }
  }

Comment: For anyone looking the answer for ionic 5 --> it has been updated as "e.detail.checked"
Comment: Thanks a lot you save my life
Answer: Below worked for me
HTML:
<ion-item>
     <ion-label>Enable Face Plotting (More Processor intensive)</ion-label>
     <ion-checkbox color="danger" checked="{{facePlottingOption}}" (ionChange)="checkboxFacePlotting($event)">
     </ion-checkbox>
 </ion-item>

.TS file:
export class TabCapturePage {
facePlottingOption: boolean = false;

public checkboxFacePlotting(e) {
    if (e.currentTarget.checked) {
      console.log("=========FACE PLOTTING ENABLED");
      this.facePlottingOption = true;
    } else {
      console.log("=========FACE PLOTTING DISABLED");
      this.facePlottingOption = false;
    }
  }
}

Answer: I don't see how the stackblitz relates to your problem, but here I see that you mispelled "target":
  if(statement){
    e.target.checked = true;
    }

Comment: Sorry, updated the stackblitz to the right one. Yes, I did misspell, but changing that does not solve the issue.


Can't modify value passed by ref in other thread

Question: I was trying to demo to a collegue why you'd better pass const references to functions doing read only operations with the following code. To my surprise it prints "It's safe!", even if I'm changing the value of the passedBool while the other thread is sleeping.
I'm trying to find out if I made a typo somewhere, if the compiler optimizes the code and passes passedBool by copy to avoid some overhead or if starting another thread creates a local copy of passedBool.
class myClass
{
public:
  myClass(bool& iBool)
  {
    t = thread(&myClass::myMethod,this,iBool);
  }

  ~myClass()
  {
    t.join();
  }

private:
  thread t;

  void myMethod(bool& iBool)
  {
    this_thread::sleep_for(chrono::seconds(1));

    if(iBool)
      cout << "It's safe!" << endl;
    else
      cout << "It's NOT safe!!!" << endl;
  }
};

void main()
{
  bool passedBool = true;

  cout << "Passing true" << endl;
  myClass mmyClass(passedBool);

  cout << "Changing value for false" <<endl;
  passedBool = false;

  cout << "Expect \"It's NOT safe!!!\"" <<endl;
}

Answer:
The arguments to the thread function are moved or copied by value. If
  a reference argument needs to be passed to the thread function, it has
  to be wrapped (e.g. with std::ref or std::cref).

from here
Comment: it's interesting... when changing the thread initialization with the `std::cref()` wrapper and changing the arguments types for `const bool& passedBool` I'm reaching the "not safe" state, as if the initial passedBool was never cast as const
Comment: I think, const modifier here is to prevent child thread procedure to modify value. it's like calling func(const bool& val). func can't modify val, but caller can.


Encoding problem from database to javamail

Question: I have a small application which reads from a Oracle 9i database and sends the data via e-mail, using JavaMail. The database has NLS_CHARACTERSET = "WE8MSWIN1252", that's it, CP1252.
If I run the app without any parameter, it works fine and the e-mails are sent correctly. However, I've a requeriment that enforces me to run the app with the -Dfile-encoding=utf8 parameter, which results in the text being sent with corrupted characters.
I've tried to change the encoding of the data read from the database, with:
String textToSend = new String(textRead.getBytes("CP1252"), "UTF-8");

But it doesn't help. I've tried all the possible combinations with CP1252, windows-1252, ISO-8859-1 and UTF-8, but still had no luck.
Any ideas?

Update to clarify my problem: when I do the following:
Statement stat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stat.executeQuery("SELECT blah FROM blahblah ...");
ResultSet rs = stat.getResultSet();
String textRead = rs.getString("whatever");

I get textRead corrupted, because the database is CP1252 and the application is running in UTF-8. Another approach that I've tried but also failed:
InputStream is = rs.getBinaryStream("whatever");
Writer writer = new StringWriter();
char[] buffer = new char[1024];
Reader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
while ((n = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, n);
}
String textRead = writer.toString();

Comment: Can you post the snippets where you get the text *into* the `textRead` variable, and where you compose the `MimeMessage`?
Comment: I have a similar problem - any pointers appreciated - I have added a bounty to the question!
Comment: I'm sorry - I have been transferred to another project within my company, so I have no access to the database for further testing. Grouchal, given that you've offered a bounty, please feel free to edit the question with more information specific to your case.
Answer: Your driver should do the conversion automatically and since cp-1252 is a subset of UTF-8 you shouldn't lose information.
Can you try the following: get the String with ResultSet.getString, write the string to a file. Open the file with an editor with which you can specify UTF-8 character set (jEdit for example).
The file should contain UTF-8 data.
Comment: I'm sorry Vincent - I have been transferred to another project within my company, so I have no access to the database for further testing. I hope your advice helps Grouchal - the user who has offered the bounty.
Comment: It turns out that although our problem was similar the real problem was caused by other software so this hasn't helped with the problem. However it is a good answer to the original question.
Comment: I have retaken this issue and finally managed to solve it. You were right, the string was already UTF-8. What I was doing wrong was the javamail part: I was trying `myMimeMessage.setText(text);` instead of `myMimeMessage.setText(text, "utf-8");`.
Answer: You seem to get lost in charset space -- i understand this... :-)
This line
String textToSend = new String(textRead.getBytes("CP1252"), "UTF-8");

does not make much sense. You have already text, convert it to a "cp1252" encoded byte []. Then you tell the VM to treat the bytes as if they were "UTF-8" (which is a lie...).
In short: if you have  a String as in textRead, you don't have to convert it at all. If something goes wrong, either the text is already rotten (look at it in the debugger) or gets rotten in the API later on. Check this and come back with more detail? Where is the text that is wrong and where do you exactly read it from or write it to...
Comment: Thanks for your answer... I've updated the question to clarify my problem. The data is getting corrupted when I read them from the database, that's why I was trying to read them as if they where CP1252 and then convert them to UTF-8.
Answer: Your database data is in windows-1252.  So -- assuming it's being handed back verbatim by the JDBC driver -- when you try to convert it to a Java String, that's the charset you need to specify:
Statement stat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stat.executeQuery("SELECT blah FROM blahblah ...");
byte[] rawbytes = rs.getBytes("whatever");
String textRead = new String(rawbytes, "windows-1252");

Is part of the requirement that the data be mailed out as UTF-8?  If so, the UTF-8 part needs to occur on the output side, not the input side.  When you have String data in Java, it's stored internally as UTF-16.  So when you serialize it out to the MimeMessage, you again need to pick a charset:
mimebodypart.setText(textRead, "UTF-8");

Comment: That makes a lot of sense :) I've made so many changes trying to fix it, that I'm starting to write stupid code. Anyway, it's still not working :(. I've tried also with "CP1252" and "ISO-8859-1" (just in case), but the result was the same.
Comment: And no, I'm not required to use UTF-8 for the mails, they can be sent with any encoding. I just need the strings to be actual UTF-8 characters because of the app being run with -Dfile.encoding=utf-8, which corrupts them otherwise.
Comment: OK.  Can you get the actual byte data being returned by `rs.getBinaryStream()` (or `rs.getBytes()`, which might be easier)?  And does it help to set `NLS_LANG` in your program's environment?
Answer: I had the same problem:
Orace database using WE8MSWIN1252 charset, some VARCHAR2 column data/text containing the euro-sign (€) in it. Sending the text using JavaMail gave problems on the euro-sign.
Finally it works. Two important things you should check/do:

be sure to use the most recent Oracle JDBC driver for the Java version you use.
specify the charset (prefer: UTF-8) in JavaMail, e.g.MimeMessage.setSubject(String text, "UTF-8")MimeMessage.setText(String text, "UTF-8").That way the email text gets UTF-8 encoded.NOTE: Because RFC 821 restricts mail messages to 7-bit US-ASCII, 8-bit character or binary data needs to be encoded into a 7-bit format. The email header "Content-Transfer-Encoding" specifies the encoding used. For more information: http://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html
Answer: Can you do the conversion in the database?  Instead of:
SELECT blah FROM blahblah

Try
SELECT convert(blah, 'WE8MSWIN1252', 'UTF8') FROM blahblah


Maximum Likelihood Estimator for Exponential Smoothing

Question: I'm not a statistician, so I would love an easy to understand answer. Is there a maximum likelihood estimator that can be stated as an explicit function of the observed data for the models enumerated below? If yes, what are the functions? I assume there's a different function for each parameter, right?

Holt-Winter double exponential smoothing // for parameters a and b
Brown's double exponential smoothing // for parameters a and b
Peter Winters' triple exponential smoothing // for parameters a, b, and y.

I use the wikipedia definitions of these exponential smoothing models as defined in http://en.wikipedia.org/wiki/Exponential_smoothing#Double_exponential_smoothing
and
http://en.wikipedia.org/wiki/Exponential_smoothing#Triple_exponential_smoothing
If the answer is that there is no explicit function for the maximum likelihood estimators for these models, is there any other type of explicit function that can be used to estimate the parameters? I'm only looking for explicit functions of the observed data, not optimization programs.
Thanks so much to anyone who can help!
Answer: "Traditional" exponential smoothing models are heuristic models and does not have an underlying statistical theory. So there is no maximum likelihood estimator. However, seminal work published in the Journal of the American Statistical Association by Ord, Koehler & Snyder in 1997 provided a statistical basis for exponential smoothing using a state space framework and provided a maximum likelihood estimation procedure. This book on Exponential smoothing extends the original work into all the forms of exponential smoothing including the ones that you mention. This book provides a clear example of how to implement maximum likelihood estimation. Forecast package in R uses the state space exponential smoothing framework. This package also uses Maximum Likelihood for estimation.
Here is the article that shows the log likelihood function for exponential smoothing also called as ETS models.
Comment: thanks for the info. My follow up question is regarding the book you linked to in "Forecasting with Exponential Smoothing." You said it includes a clear example of how to implement maximum likelihood estimation. Is it something that I could then convert to an explicit equation that's just a simple function of the observed data?  And by simple function, I mean it only uses the basic mathematical operators like +-x/^log, and ln. It's an $80 book, so just want to make sure that I don't buy it if it's not going to give me what I'm looking for. Thnx!
Comment: the preview of the [book](http://www.exponentialsmoothing.net/preview) has the maximum likelihood estimation part freely available to you to make a decision on whether to purchase or not. In addition, I have updated the post with the reference article that shows the log likelihood estimator for exponential smoothing with state space models. This is the same log likelihood in the book. The open source code for ETS is also available in R, you can find out the code implementation there as well. You can make your own call whether to purchase the book or not
Comment: I read the article, and it states you have to minimize the L* equation (#9 in article) to obtain the parameter estimates. Then the procedure involves using an optimizer. So, I think the answer to my original question is that there is no explicit function of the observed data that can be used for parameter estimation. Please correct me if I'm wrong. Assuming I'm right, then is there any other less accurate methodology for parameter estimation that doesn't require an optimizer, and only uses as an explicit function of the observed data? Thnx!


Global Payments Hosted Payment Page Integration - sandbox and prod POSTs back to merchantUrl delays

Question: I'm running our Hosted Payment Page integration the same as the official js lib:
https://github.com/globalpayments/rxp-js/blob/master/examples/hpp/process-a-payment-embedded-autoload-callback.html
All is good except this bit is very slow for the response to come back to our side with the approved/failed transaction:
https://github.com/globalpayments/rxp-js/blob/9909985b96ab5ed945614affad5f3739827f956b/examples/hpp/process-a-payment-embedded-autoload-callback.html#L16
e.g. The form gets presented, you enter your card details and click submit (on HPP) and then the 3D secure shows, does it's thing but around 4 mins later the result come back within the answer line (the above link, line 16). I'm not sure why it's so slow. Sandbox and Production are the same.
I'm opening a support case anyway, but if anyone has any ideas.
Thanks,
Gavin.
Answer: Was the firewall on the box blocking https / port 443 from the public. Obviously the libs do something. All was instant when off.


Contextual bandits: Number of models to estimate

Question: I have recently read several papers on contextual bandits especially for the case of binary rewards. However, one very basic aspect is not entirely clear to me:
In some papers (e.g. here https://arxiv.org/pdf/1812.06227.pdf), it is explicitly stated, that for each arm $a_1,...a_K$ the expected reward given some context vector $x_t$ is estimated by a separate linear model, i.e. $E[r_{a,x_t}] = \mu(\theta_a^Tx_t)$ (with a logistic link function $\mu(.)$ when rewards are binary).
In other papers (e.g. here https://arxiv.org/pdf/1703.00048.pdf or here https://arxiv.org/pdf/1805.07458.pdf) it seems like there is only one parameter vector $\theta$ for all arms and the context $x_{t,a}$ contains arm-specific features as well. While in the former case we would estimate $K$ models (one per arm), in the latter case it would be only one model for all arms.
My questions are:
1) Do I understand correctly that both are valid approaches to the contextual bandit problem or did I misunderstand the model formulation? To me they appear to be conceptually quite different.
2) And if they are both valid approaches, is there any systematic comparison between the two?
Answer: Have a look at this: http://rob.schapire.net/papers/www10.pdf and this https://papers.nips.cc/paper/7491-a-smoothed-analysis-of-the-greedy-algorithm-for-the-linear-contextual-bandit-problem.pdf.
It describes the contextual bandit setting for what they call "disjoint linear models" or "multi-parameter setting" ( different for all ) and "Hybrid Linear Models" o "single-parameter setting" in which they have a common parameter ⋆, common to all arms.
The short answer is that the two models are equivalent to one another (up to a factor of k in the problem dimension). However the two settings ave different properties and can be used to model different problems. To cite the paper (https://papers.nips.cc/paper/7491-a-smoothed-analysis-of-the-greedy-algorithm-for-the-linear-contextual-bandit-problem.pdf.):
The single-parameter setting can model, for example, the choice of which of some subset of individuals should participate in a particular clinical trial.
The multi-parameter setting can model, for example, the risk of criminal recidivism amongst different individuals who come from different backgrounds, when observable features correlate differently to crime risk amongst different groups of individuals.
I hope this answer your question.
Answer: By having separate linear models for each action, you are not enabling generalization between actions. This can be a problem if actions are ordered in some sense. For example, if the task is spam classification and your actions are degrees of belief (A = {Very unlikely, unlikely, neutral, likely, very likely}).
It seems more common to enable this generalization by default, and have a single linear model. The contextual bandits chapter in [1] considers a single linear model for all actions in EXP4. I believe having separate linear models for each action is actually to make the analysis of confidence bound algorithms easier (see for example, https://arxiv.org/abs/1802.09127). I have also seen separate models for each action in the Bayesian DQN literature (https://arxiv.org/pdf/1802.04412.pdf).
To answer your second question, I am not aware of a study that contrasts both approaches. I would certainly expect that a single linear model would be better in general, since it contains separate linear models as a special case.
[1] Lattimore, T., & Szepesv\'ari, Csaba (2018). Bandit Algorithms.


How to divide/split formatter settings for Groovy and Java in eclipse?

Question: Eclipse use the same Code formatter for Groovy and Java.
But I need different space/tabs rules for Java and for Groovy (in Java tab=4spaces, in Groovy tab=2spaces, etc.)
Maybe someone knows trick for divide formatters for java and groovy in eclipse? some plugin? some special settings?..
Answer: Are you saying that your Java code and groovy code in the same project should be formatted differently?  If so, that's not supported since all the use cases we considered had Groovy code inheriting the formatting preferences of Java.
If, however, your Java and Groovy code are in separate projects, you can use project specific settings for the different projects.
Comment: Ok, now clear. But in my company use different code convention for java and groovy. I think main reason for that - groovy closer to script languages then to static..
and yes - we use groovy and java on the same project.. it happened :)


Printing Numbers in Scheme

Question: Im trying to do the following in the list below: Let say list one consists of (1234) and list 2 is (5678) I am trying to print it in the following way (15263748) This is what I have for now but I am not sure where to go after this. Of course right now the code below prints it like 12 34 it should be 1 3 2 4
   (define (arrange-list lst1 lst2)
  (append lst1 lst2))
 (arrange-list '(12) '(34))

Comment: Just to clarify: the input lists look like `(1234)` or `(1 2 3 4)`? they're different
Answer: This is a common procedure, usually known as interleave. Assuming that the input lists have the same length, we can write the following implementation from scratch, using explicit recursion:
(define (interleave lst1 lst2)
  (if (null? lst1)
      '()
      (cons (car lst1)
            (interleave lst2 (cdr lst1)))))

The trick is taking one element from one list, and then from the other until both lists are exhausted. A more idiomatic solution in Racket would be to use built-in procedures, like this:
(define (interleave lst1 lst2)
  (flatten (map cons lst1 lst2)))

Either way it works as expected:
(interleave '(1 2 3 4) '(5 6 7 8))
=> '(1 5 2 6 3 7 4 8)

Answer: If the lists aren't of equal length, this is my solution which works albeit is not as elegant as the one mentioned in the other answer.
Basically, we maintain a variable x while recursively calling the procedure that indicates which list needs processing currently. The first list is indicated by 1 and the second, 2.
(define (print-alt A B x)(cond
                ((and (null? A) (null? B)) '())
                 ((= 1 x) (cond
                           ((null? A) (print-alt A B 2))
                           (else (append (list (car A)) (print-alt (cdr A) B 2)))))
                (else (cond
                      ((null? B) (print-alt A B 1))
                      (else (append (list (car B)) (print-alt A (cdr B) 1)))))))

Here is the output:
(print-alt (list 1 2 3 4 5 6) (list 5 6 7 8 9 10 11 12 123) 1)
(1 5 2 6 3 7 4 8 5 9 6 10 11 12 123)

Printing Numbers in Scheme