How to get taxonomy translated when creating a new node in a different language than the interfaces?

Question: Configuration:
I set up a multilingual site with Multilingual support: Enabled, with translation so I'm creating a new node for each language.
The taxonomy term is set up with the Translation mode: Translate, so I have a new term for each language.
Creating a node with default language first is working fine:
If I create a new node in my default language using node/add/page I correctly see the taxonomy terms in the site language, then after the node is created, using the Translate tab and select the Spanish language will work correctly and display the terms in Spanish.
Creating a node in a different language:
Now if I want to create a Spanish node first and I go to node/add/page I will see the site default language for the terms, so my tid will be in English instead of Spanish even if the language switcher is correctly set to Spanish.
Then when I'll edit this node, my terms will switch back to Spanish and loose the default term I selected while creating the node.
Partial solution:
A way to avoid this problem is to go to es/node/add/page instead and now the terms are correctly in Spanish.
This solution isn't very convenient, so what is the correct way to handle this efficiently and avoid saving a Spanish node with English terms ?
Answer: Language Selecting
There are number of ways Drupal distinguishes which language the current user is in (yourself for example).
The admin page at '/admin/settings/language/configure' lists 4 methods.

None.
Path prefix only.
Path prefix with language fallback.
Domain name only.

As you can see, to determine the language, Drupal either uses a 'path prefix', 'language fallback' and 'domain'.
The path prefix is simply something like '/de/about-us-page'. Drupal sets the language to German. When I say set, I actually mean it sets it for that particular page request. Then visiting '/about-us-page' would NOT display the language in German but your default language (maybe English).
For Language Fallback, Drupal check if the currently logged in user has a language set in their $user account. If not, it will look for a language from the headers sent from the browser. If in any case the language cannot be determined, the default language is used; again may be English.
The Domain method is simply inspecting the domain and checking if it has been mapped to a language. e.g. German to domain.de.
Conclusion
From this, you can see that Drupal is simply reverting back to the default language. You setting the 'language' within the node edit form, is simply mapping this node to that language.
Usually when adding content for other languages, I switch into the native language. This ensure my taxonomy terms are mapped correctly.
Essentially, this is not a problem but the way this system works.
Possible Fix
If all the terms you are using are common to all languages, you can modify the Taxonomy translation mode to 'Localize' instead of 'Translate'. This should allow all the terms to be saved correctly even if you use the edit page without prefix 'node/add/page'.
Just ensure you translate the terms using the String translation module.
Hope this help.
Amarjit
Comment: Thanks for the explanation, I'm still not confident on what's the best way to handle multilingual taxonomy. I know I have to switch my locale language to create a new node but this isn't convenient and the end user won't do it leaving me with a messy content... I'll test out the localize option but I'm not sure if this would work out for free tags.


Minimizing trigonometric expression with the hint of substitution

Question: I have asked this question earlier for minimization of this expression.
$$
\left[\cos ^{2}\left(\dfrac{\theta }{2}\right) +
\,\sqrt{\,{1 - \gamma}\,}\,\sin^{2}\left(\dfrac{\theta }{2}\right)\right] ^{2} + \dfrac{\gamma }{4}\,\sin^{2}\left(\theta\right)\quad
\mbox{where}\quad 0 \le \gamma \le 1.
$$
But then I am not sure if we can reach any relevant minimization (closed form) from the strategy mentioned here. What is the way to minize this with the following hint?
Hint is given in the first question itself.
Comment: Did you omit the hint?
Answer: Try starting with the half angle formulas for sine and cosine, so the entire expression will  be with respect to $\theta$. Then multiply everything out and convert the $\sin^2 \theta$ to $1 - \cos^2 \theta$. After simplifying, you can differentiate with respect to $\theta$ and find the critical values.
Comment: Trust me I did that. But it doesn't converge. The end expression has nothing to simplify but yet we can't find solution of it. If you check the linked question you can see we have been doing what you suggested.
Answer: You are minimizing$$\left(\frac{1+\cos\theta}2+\sqrt{1-\gamma}\,\frac{1-\cos\theta}2\right)^2+\frac\gamma4(1-\cos^2\theta).$$
After expansion, you get a quadratic trinomial in $\cos\theta$.

find the location of the minimum by cancelling the derivative; if $\cos\theta$ falls in $[-1,1]$ it corresponds to the global minimum.

otherwise, compute the trinomial for $\cos\theta=\pm1$ and keep the smallest value.


How do I merge a parent fork?

Question: I forked a project on github, since then the original owner made changes to it. How do I pull down those changes and merge them into my fork?
Thanks!
Answer: First set up a remote for the upstream repository, if you haven't already:
git remote add upstream git://github.com/...

Then fetch the remote contents and merge from it (assuming you're on the corresponding master branch of your fork):
git fetch
git merge upstream/master

Comment: Because 'upstream' was not my default remote, this is what worked for me (which I guess is compressing the fetch and merge steps given above): `git pull upstream master`
Comment: You rarely want to work in the upstream's master. Unless you need it, I would skip the u-master creation.
Comment: @AdamDymitruk: True. I don't think I've actually used it with GitHub in fact; I use it mostly when I have an upstream "template" repo of my own that I've "forked" into a number of child repos, from which I might want to make changes to the upstream template.
Comment: what does this mean? fatal: 'upstream' does not point to a commit
Comment: for some reason I had to pull it down, not just fetch... then it worked, thanks Will!
Comment: "fatal: 'upstream/master' does not point to a commit" This doesn't work.
Comment: I get the following error when I try to follow the instructions above
`merge: upstream/master - not something we can merge`
BTW, I should add that the upstream is the parent of the parent. i.e. the parent of my fork is already forked from the repo I'm trying to merge into my repo

How do I merge a parent fork?


How to store perl modules local to the app ala rails unpack gems or bundler?

Question: I have a number of modules that I would like to store in my local app dir (perhaps something like ./lib) and check into source control.  Ideally, this would include all of the module dependencies.
The end goal is to be able to deploy to a number of boxes (which only have a basic perl install) and ensure that the script has the required modules/versions.  CPAN is out as an option during deployment and I want to avoid maintaining a separate internal repo if possible.
For the deployment, all of the boxes will be the same os/arch.  Cross platform would be a nice to have as we have folks who develop on linux and osx.
Any ideas?
Thanks.
Answer: Use local::lib. The documentation is highly readable.
Stuffing your local::lib directory into a VCS is probably a good idea. If you're using pure Perl modules (no compiled XS code), then it should be working on all platforms.
If you need to use XS modules that are not binary compatible between your differing architectures, then you might just get away with adding two versions of the compiled modules into the same local::lib installation, since those modules are found based on the archname compile time setting that is stored in the perl binary (perl -V).


Unwanted pipelines triggers

Question: I have next structure of my repo in Azure's VisualStudio:
development ↴
            - Branch1
            - Branch2

I have a pipeline.yaml in the development branch:
trigger:
  - development

pool:
  vmImage: ubuntu-latest

variables:
  - name: DEPLOYMENT
    value: development

jobs:
  - job: Print evironment
    steps:
      - script: printenv | sort

I have a pipeline linked to that file in development branch. Also I have a policy to run that pipeline for pull requests.
And I have two problems with this config.

When I am pushing anything into ANY branch that has that pileline.yaml file - pipeline will be triggered for that branch. And that affects all the branches nested from development branch
When I am creating a pull request - it is triggering the build. But if I am pushing changes to the branch then two builds are triggering. One for the branch itself and one for pull request.

What am I doing wrong?
Comment: How did you create the structure of my repo? The `development` is the branch folder? Could you please share the structure of my repo with image?
Comment: I can not make any screenshot unfortunately. The "development" is a branch from which other branches are created and to which they supposed to be merged
Answer: So the problem was in trigger syntax
This is working as explained in the question:
trigger:
  - development

This is working as expected:
trigger:
  branches:
    include:
      - development

Unwanted pipelines triggers


git stash show -p returns fatal error

Question: When I run git stash list, I get the following:
stash@{0}: WIP on mybranch: 123456f Generic comment here.
stash@{1}: WIP on mybranch: 234567g Generic comment here.
...
stash@{n}: WIP on mybranch: 345678h Generic comment here.

This is expected behavior.  What mystifies me is git show's behavior.
When I run git stash show -p stash@{1} in my repo, I get the following error:
fatal: ambiguous argument 'stash@{1}': unknown revision or path not in working tree.
How can this be?  I'm running git stash show -p stash@{1} in a git repo.
Comment: That does seem impossible. It means `git rev-parse` is failing to translate `stash@{1}` into a Git object ID, and yet `git stash list` simply shows all the reflog entries for the `stash` ref, so if it shows `stash@{1}` then `stash@{1}` is a valid name for an object ID. (The "on the right branch" part is irrelevant: stashes are on *no* branch.)
Comment: What Git version shows this error and with what Git version was the repository created (cloned)?
Comment: `git version 1.8.0` for both showing the error and the repo it was created.
Answer: The shell was eating up my own curly braces.
git stash show -p stash@'{1'} works.
Ironically, it was the least upvoted answer @ Is it possible to preview stash contents in git? that answered my question.
Just weird because the error reported the command with curly braces.  You'd expect it to report it without them.
Comment: In fact, my shell does that as well, but the error message *does* show it without the curly braces: `fatal: ambiguous argument 'stash@2': ...` It seems impossible. What OS are you on (and why is your Git so old? :-) these may have the same answer...)

git stash show -p returns fatal error


Administering user accounts on an ftp server remotely via script, webservice or similar

Question: We want to be able to create FTP users and assign rights to specific folders and revoke them again remotely by means of an automated process (running a script, calling a webservice or similar).
What solutions can you suggest?
OS is undecided. Windows and Unix are both fine.
Comment: What OS?  Any other requirements for the FTP server?
Comment: Edited question to answer OS requirements.
Answer: I think Filezilla FTP Server has a remote administration tool.
Answer: I'd suggest pureftpd or proftpd with a mysql backend
Old tutorials but still relevant:

http://www.howtoforge.com/proftpd_mysql_virtual_hosting
http://www.howtoforge.com/virtual-hosting-with-proftpd-and-mysql-ubuntu-8.04

With this you could use any popular programming language to update the mysql database to your liking including quotas, access, etc.
Alternatively I would recommend glftpd

http://en.wikipedia.org/wiki/Glftpd

The setup for glftpd can be quite complicated but it supports a lot of scripting, including user management through ftp commands.


Why do two implementations of the Anderson-Darling test produce such different p-values?

Question: I want to check the normality assumption of my series
I am trying to figure out the way is computed the p-value of the Anderson Darling in R packages, goftest and nortest. According to the R help, once is computed from Table 4.9 in Stephens (1986) and the other with the Marsaglia routine. But I do not find out why are they so different, which is more accurate and when should I use them.
library(goftest)
library(nortest)

x = iris$Sepal.Length

goftest::ad.test(x, "pnorm", mean = mean(x), sd = sd(x))
nortest::ad.test(x)

The result of goftest package:
    Anderson-Darling test of goodness-of-fit
    Null hypothesis: Normal distribution
    with parameters mean = 5.84333333333333, sd = 0.828066127977863

data:  x
An = 0.8892, p-value = 0.4208

and the nortest package:
    Anderson-Darling normality test

data:  x
A = 0.8892, p-value = 0.02251

Comment: I don't think that's the case: an approximation would not be so grossly bad.
Comment: Right: that means that internally it performs the parameter estimation and therefore knows how to cope with the fact that the parameters were estimated from the data.  My suspicion is that `goftest::ad.test` does not know that, which is why it gets the wrong p-value.  That also explains why it expects you to supply the parameters, for otherwise it has no way of knowing precisely which Normal distribution you specified beforehand.
Comment: Without consulting the manual pages, it strikes me that your use of `goftest` is likely incorrect.  I would expect it to base its p-value on a *prespecified* normal distribution.  By offering it a normal distribution whose parameters have been estimated from the data, you rig the test strongly in favor of finding a "good fit," which can explain the large p-value.  Might I therefore suggest reading the documentation for both tests closely?
Comment: Thank you for your reply, but according with the documentation `nortest::nortest::ad.test()` only has one parameter https://www.rdocumentation.org/packages/nortest/versions/1.0-4/topics/ad.test
Comment: Thank you, but I guess the key point is the way they are obtaining the statistic distribution, `nortest::ad.test` is using some kind of approximation with the tabl, whereas `goftest::ad.test` is computing the exact value.
Comment: Thank you, the thing is that apparently I did the same, as you can see the statistic value is the same, but in one case the test is accepted and the other reject the null hypothesis. I can not figured out which one should I trust.
Answer: The Anderson-Darling test is a good test--but it has to be correctly applied and, as in most distributional tests, there is a subtle pitfall.  Many analysts have fallen for it.

Distributional tests are Cinderella tests.  In the fairy tale, the prince's men searched for a mysterious princess by comparing the feet of young ladies to the glass slipper Cinderella had left behind as she fled the ball.  Any young lady whose foot fit the slipper would be a "significant" match to the slipper.  This worked because the slipper had a specific shape, length, and width and few feet in the realm would likely fit it.

Imagine a twist on this fairy tale in which Cinderella's ugly stepsisters got wind of the search.  Before the prince's men arrived, the stepsisters visited Ye Olde Paylesse Glasse Slipper Store and chose a slipper off the rack to match the width and length of the eldest sister's foot.  Because glass slippers are not malleable and this one was not specifically tailored to the foot, it was not perfect--but at least the shoe fit.
Returning to their home just as the prince's men arrived, the older sister distracted the men while the younger surreptitiously substituted the slipper she had just purchased for the one brought by the men.  When the men offered this slipper to the older sister, they were astonished that it fit her warty foot.  Nevertheless, because she had passed the test she was promptly brought before the prince.
I hope the prince, like you, was sufficiently sharp-eyed and clever to recognize that the ugly lady brought before him could not possibly be the same beautiful girl he had danced with at the ball.  He wondered, though, how the slipper could possibly fit her foot.

That is precisely what this question asks: how can the goftest version of the A-D test seem like the dataset is such a good match to the Normal distribution "slipper" while the nortest version indicates it is not?  The answer is that the goftest version was offered a distribution constructed to match the data while the nortest version anticipates that subterfuge and compensates accordingly, as did the prince.

The goftest version of the Anderson-Darling test asks you to provide the glass slipper in the form of "distributional parameters," which are perfect analogs of length and width.  This version compares your distribution to the data and reports how well it fits.  As applied in the question, though, the distributional parameters were tailored to the data: they were estimated from them.  In exactly the same way the ugly stepsister's foot turned out to be a good match to the slipper, so will these data appear to be a good match to a Normal distribution, even when they are (to any sufficiently sharp-eyed prince) obviously non-Normal.
The nortest version assumes you are going to cheat in this way and it automatically compensates for that.  It reports the same degree of fit--that's why the two A-D statistics of 0.8892 shown in the question are the same--but it assesses its goodness in a manner that compensates for the cheating.

It is possible to adjust the goftest version to produce a correct p-value.  One way is through simulation: by repeatedly offering samples from a Normal distribution to the test, we can discover the distribution of its test statistic (or, equivalently, the distribution of the p-values it assigns to that statistic).  Only p-values that are extremely low relative to this null distribution should count as evidence that the shoe fits.
Here is a histogram of 100,000 iterations of this simulation.

P-values are supposed to have a uniform distribution (when the null hypothesis is true).  The histogram would be almost level.  This distribution is decidedly non-uniform.
The vertical line is at $0.42,$ the p-value reported for the original data.  The values smaller than it are shown in red as "extreme."  Only $0.0223$ of them are this extreme.  (The first three decimal places of this number should be correct.)  This is the correct p-value for this test.  It is in close agreement with the value of $0.0251$ reported by nortest.  (In this case, because I performed such a large simulation, the simulated p-value should be considered more accurate than the computed p-value.)

The R code that produced the simulation and the figure is reproduced below, but with the simulation size set to 1,000 rather than 100,000 so that it will execute in less than one second.
library(ggplot2)
x = iris$Sepal.Length
p.value <- goftest::ad.test(x, "pnorm", mean = mean(x), sd = sd(x))$p.value
#
# Simulation study.
#
mu <- mean(x)
sigma <- sd(x)
sim <- replicate(1e3, {
  x.sim <- rnorm(length(x), mu, sigma)
  goftest::ad.test(x.sim, "pnorm", mean = mean(x.sim), sd = sd(x.sim))$p.value
})

p.value.corrected <- mean(sim <= p.value)
message("Corrected p-value is ", p.value.corrected,
        " +/- ", signif(2 * sd(sim <= p.value) / sqrt(length(sim)), 2))
#
# Display the p-value distribution.
#
X <- data.frame(p.value=sim, Status=ifelse(sim <= p.value, "Extreme", "Not extreme"))
ggplot(X, aes(p.value)) +
  xlab("goftest::ad.test p-value") + ylab("Count") +
  geom_histogram(aes(fill=Status), binwidth=0.05, boundary=0, alpha=0.65) +
  geom_vline(xintercept=p.value, color="#404040", size=1) +
  ggtitle("A-D 'p-values' for Simulated Normal Data",
          "`goftest` calculations")

Comment: (+1) Some readers may wonder whether it makes a difference simulating from other distributions consonant with the null hypothesis, rather than that particular one. It doesn't, for EDF-based GoF test statistics in general; provided that the estimated parameters are those of a location-scale family. (See https://stats.stackexchange.com/q/110272/17230 & https://stats.stackexchange.com/q/237779/17230).


How to Apply Lagrange multiplier well

Question: This problem was originally:

Sphere $S$: $x^2+y^2+z^2=50$
Point $P(0,5,5)$
Circle $C$ is an intersection of a plane that passes $P$, and sphere
  $S$.
The radius of the circle $C$ is $1$.

Find the maximum of area of the projection of $C$ on the $xy$ plane.

I want to apply lagrange multiplier on this problem. So I converted to:

Constraints: $$a^2+b^2+c^2=1$$ $$b+c=\frac 75$$
Problem: Find the minimum of $$c$$.

Where the vector $(a,b,c)$ is the normal of the plane that passes $P$.
Solution using Cauchy- Schwarz Inequality:
$$0 a+b=\frac 75 -c$$
$$a^2+b^2=1-c^2$$
$$(0^2+1^2)(a^2+b^2) \ge(0 a+b)^2$$
$$\therefore (1-c^2) \ge (\frac 75-c)^2$$
$$\therefore \frac 34 \le c \le \frac 45$$
How can I use lagrange multiplier method on this problem effectively? Thanks.
Comment: I do not know what the statement "$P(0,5,5)$" means  so I cannot assess whether or not your transformed problem is related to the original. However I observe the transformed problem can be solved without Lagrange multipliers since it is the same as minimizing $c$ subject to $a^2 + c^2 + (7/5-c)^2=1$, you can find the region of all $c$ for which the constraint can be satisfied.
Comment: @Michael Oops! Sorry, I think I've missed some constraints while transforming the problem.
Comment: @Michael You're right, when I use Cauchy- Schwarz inequality it can be solved easily.  (c<=4/5) but I my point was to find solutions that applies lagrange multiplier method here, so that I could use that method when I can't use Cauchy- Schwarz or ordinary methods because of some reasoms.
Answer: You've already got everything you need. You can either proceed with two multipliers: $$L(a,b,c;\lambda,\mu) = c - \lambda(a^2+b^2+c^2-1) - \mu\left(b+c-\frac75\right)$$ or incorporate the linear constraint right away: $$L(a,c;\lambda) = c-\lambda\left(a^2+\frac1{25}(7-5c)^2+c^2-1\right).$$ I'd do the latter since it immediately eliminates two variables from the calculations and gets you to a set of equations that you'd have arrived at anyway. After differentiating, besides the constraint equation we have $$-2\lambda a = 0 \\ \left(\frac{14}5-4c\right)\lambda+1 = 0.$$ From this we quickly find that $\lambda\ne0$, so $a=0$, which leaves a quadratic equation in $c$.
Comment: Thank you for the clear example of using Lagrange multiplier on problem solving

How to Apply Lagrange multiplier well


how can i separate the numbers multiple by 9 in 1 to 1000 numbers using javascript?

Question: Here I am trying to display multiples of 9 in one side and remaining in another side from 1 to 1000 numbers. but only 9 multiples are displaying how can i display remaining numbers on another side?

var i;
for (i=1;i<=1000;i++){
if (i % 9 == 0)
{
document.writeln(i);
document.write("
");
//document.write(i);
}
/*else if (i % 9){
document.write(i);
document.write("
");
}*/
}
Comment: Do you want to show the non-multiples of 9 on the left side like a column? Please elaborate
Comment: What is other side? You mean in some sort of table structure?
Answer: You can create two
 elements, concatenate "
" + i + "
" to appropriate ul to index of ul returned by .querySelectorAll()

var ul = document.querySelectorAll("ul");
var i;
for (i = 1; i <= 1000; i++) {
  ul[i % 9 == 0 ? 0 : 1].innerHTML += "
" + i + "
";
}
ul {
  display: inline-block;
  position: absolute;
  width: 125px;
}

ul:nth-of-type(2) {
  left: 250px;
}

multiples of 9

not multiples of 9

Answer: Yes, you can do that by first adding two div in HTML, And then simply add the the value of i in div like if i % 9 == 0 then we add this value to first div else we add this value to second div using simple javascript code.
You can access first and second div by using document.getElementById("div_id");


É possível utilizar EcmaScript 6 sem Babel ou Webpack?

Question: Como nos velhos tempos, seria possível simplesmente incluir um arquivo EcmaScript 6 no navegador e usar suas funcionalidades sem a necessidade de "transpilar" estes códigos?
Answer: Com exceção do Internet Explorer, a maioria dos navegadores suportam as sintaxes do ES6, ES2016, ES2017 e ES2018.
Sim, é possível escrever o código diretamente sem transpilador. É recomendável? Isso é subjetivo.
Sites como W3schools e MDN costumam documentar quais funcionalidades são suportadas por cada navegador, mas mesmo esses sites não possuem documentação completa, no fim do dia cabe a você jugar o que deve ser feito.
Por experiência (leia-se, subjetivo), acredito que arrow function, async/await, for..of, spread operator, let/const são relativamente seguros para se usar diretamente em navegadores. import/export por outro lado não é suportado por nenhum navegador.
Comment: Seja bem-vindo ao StackOverflow. Obrigado pela sua resposta, no entanto me gerou uma dúvida. Estava pesquisando e ví que é possível utilizar import e export incluindo a tag script da seguinte forma: ``````. Isto ainda seria ES6?
Comment: Eles não são equivalentes. `import` e `export` são macros para importar módulos, utilizados principalmente no NodeJS, de forma similar ao `import` e `public` de outras linguagens de programação.
Comment: Encontrei neste site: https://jakearchibald.com/2017/es-modules-in-browsers/


How do I install the database drivers?

Question: I'm trying to use Drupal 9 framework, and I'm stuck when setting up the database: I get the infamous error Error: Class 'Drupal\pgsql\Driver\Database\pgsql\Connection' not found.
When looking at the Drupal Core module composer.json, I see that the module expects a drivers directory to exist under vendor/drupal:
"autoload": {
        "psr-4": {
            "Drupal\\Core\\": "lib/Drupal/Core",
            "Drupal\\Component\\": "lib/Drupal/Component",
            "Drupal\\Driver\\": "../drivers/lib/Drupal/Driver"
        },

Which seems to be confirmed by this issue:
https://www.drupal.org/node/3129492
Which clearly establishes that Database drivers are now provided by their own dedicated module.
The question is: where is this module and how can it be installed? I searched packagist for drupal/drivers but it doesn't exist.
Comment: Is this an install-time or post-install issue?
Comment: I'm starting from scratch.
Comment: I'm voting to close this question because the question does not include the precise steps that occur which lead to the error message.
Answer:
where is this module and how can it be installed?

It's not a single module containing all the drivers - each driver has its own module, already included with core.
For Postgres that's the pgsql module (for MySQL it's mysql). That's the module you need to enable, nothing else to download.
Comment: You can ignore that last entry for the autoloader (`"Drupal\\Driver\\": "../drivers/lib/Drupal/Driver"`) - it's just an optional location that Drupal will look for contributed drivers in. See https://www.drupal.org/node/3123251
Comment: I found them inside the Drupal core module but they are not at all located where the core module autoloader expect them. This is very strange. I'll update my question.
Comment: Reading again, it's not clear from the question what steps you've taken to get to where you're at - under normal conditions, the correct database module (with the driver) will be installed during the site install after you've selected the connection type. Are you doing anything non-typical for your install? Using a non-core profile? Installing via `drush site-install` with some exotic parameters? Anything like that?
Comment: I'm just requiring the core module, creating an entry point index.php that instanciates a custom kernel and that's it. I'm using the framework to create a custom CMS - as I always did up to Drupal 8 - instead of installing a pre-configured site with drush. The documentation of Drupal 9 is very lacking, even more so than Drupal 8 it seems, when it comes to build a CMS instead of configuring a pre built one.

How do I install the database drivers?


Let $A\in M_3(\mathbb{Q})$ so that $A^8=I_3$. Prove that $A^4=I_3$

Question: Let $A\in M_3(\mathbb{Q})$ so that $A^8=I_3$. Prove that $A^4=I_3$.
I tried to look at the solution provided by my book, but I can't understand it.
They say that $A$'s minimal polynomial divides the polynomial $P \in \mathbb{Q} [X] $, $P(x) =x^8-1=(x-1)(x+1)(x^2+1)(x^4+1)$(this is clear to me).
Suppose $A^4 \neq I_3$. Then among $A$' s characteristic polynomial's roots would be one of the roots of $x^4+1=0$(this is what I do not understand, why does this happen?). This of course is a contradiction since the degree of $A$' s minimal polynomial is $\le 3$.
Answer: The minimal polynomial of $A$ divides $(x-1)(x+1)(x^2+1)(x^4+1)$ and is of degree $\leq 3$ as $A \in M_3$. This forces it to be a divisor of $(x-1)(x+1)(x^2+1)$ (because $x^4+1$ is irreducible, and does not break into further factors, so it cannot be part of the factorization of $A$ by the degree constraint. This is similar to the fact that if $a|bc$ and $c$ is prime with $c \nmid a$ then $a | b$).
However, just note that $(x-1)(x+1)(x^2+1) = x^4 - 1$. Therefore, the minimal polynomial of $A$ divides $x^4-1$, which means that $A$ satisfies $x^4 - 1$ : that is, $A^4 = I$.

As for the book itself, I will expand. The characteristic polynomial $p_A$ of $A$ is of degree $3$. The book is saying that if $A^4 \neq I$ then because $A$ satisfies $p_A(A) = 0$, the characteristic polynomial $p_A$ does not divide $x^4 - 1$. Therefore, some root $\alpha$ of the characteristic polynomial is not a root of $x^4-1$.
Next, because the minimal polynomial has the same roots as the characteristic polynomial, this root $\alpha$ is a root of $x^8-1$(since $A^8 =I$, we get that the minimal polynomial divides $x^8-1$, so $\alpha$ is a root of $x^8-1$) which is not a root of $x^4 - 1$. Therefore, it must be a root of whatever is left , which is $x^4+1$. But then, because $x^4+1$ is irreducible, $\alpha$ cannot be a root of a polynomial of smaller degree than $x^4+1$, which is a contradiction because we assumed it was a root of $p_A$.
So yes, basically my explanation is better.

The point is, over the real field $x^4+1$ will factorize into two more polynomials of degree $2$, so the argument above will fail if $x^4+1$ reduces because then the minimal polynomial could be one of the degree $2$ factors.
Comment: I  am actually still editing, trying to clarify what the book was trying to say. It is slightly longer, so I think what I wrote is better than the book! Also, $A$ can be a real matrix, it seems.
Comment: Thank you for your help! Your solution is indeed better, but I am still curious what they wanted to say.
Comment: Thank you! This is way clearer than the solution in my book. I wonder, is $A$ necessarily a rational matrix? Can't it just be a real matrix?
Comment: Actually no, we are using the fact that the characteristic polynomial has rational coefficients here, because $x^4+1$ is not irreducible over the real numbers, it will factorize. The rationality of the entries is crucial, let me add that above.


Solution for missing std::wstring support in Android NDK?

Question: I have a game which uses std::wstring as its basic string type in thousand of places as well as doing operations with wchar_t and its functions: wcsicmp() wcslen() vsprintf(), etc.
The problem is wstring is not supported in R5c (latest ndk at the time of this writting).
I can't change the code to use std::string because of internationalization and I would be breaking the game engine which is used by many games ...
Which options do I have?
1 - Replace string and wstring with my own string classes
This would give me better platform independency, but it is ridiculous to reimplement the wheel.
I've already started with a COW implementation of strings. I need it to be COW because I use them as keys in hash_maps.
This is of course lots of work and error prone ... but it seems it is something I can do.
2 - Try to fix the NDK recompiling the STLPort with my own implementations of the wide char string functions of the C standart library (wcslen, mbstowcs ... )
This would be the preferable way ... but I have no idea how to do it :(
How do I replace a function (lets say wcslen) in the libstdc++.a or libstlport_static.a? (not sure where they are :()
And as well I'm not sure which functions I need to reimplement, I know wcslen is not working so I guess they should be all ...
3 - Do you have any other idea?
I can't wait for an official fix for this and I will have to go with option #1 if I can't realize how to do #2.
I've read somewhere that if you target 2.3 you can use wstrings, but I ought to target Android 2.1.
PS: Forgot to say I need to use STL of course, but no RTTI and I can live without exceptions.
Thanks in advance!
Comment: Does setting `APP_STL := stlport_static` in your Application.mk not work? It compiles `std::wstring test = L"test";` for me. (Haven't tried running anything though.)
Comment: @Martin Stone
stlport compiles perfectly but
`std::wstring test = L"test";
assert( test.size() != 1 );  // fails!!!!!`
tracking it wsclen fails too, it returns 1 (I guess they are doing some casting to char* only) further investigation showed me that stl_port is using many functions (don't know how many nor which ones exactly) of the standard C library ...
Comment: Would it be an option to simply convert all your wstrings to strings (using `setlocale()` + `wcsrtombs()`) at well-defined interface points in your program? That way you can keep everything the way it is internally and only add a little bit of modification when you need to output strings.
Comment: Sorry,wcsrtombs doesn't work, but I can create my implementation of a converter to utf8 if it were the case (actually I have already coded that), the problem is I have std::wstring in more than 1500 places where it is used, and I would need to review each one if I modify its behaviour. So what I need is to find a replacement for the C wide character functions in the NDK and recompile the STL (something I have no idea how to do) ... or the other solution is to write my own std::wstring class which mimics the std behaviour, as I said above.
Comment: @user548569: I might have misunderstood your question. Are you claiming that your platform does not _have_ `wchar_t` (and `std::wstring`) so that you cannot even compile your code?
Comment: Worst than that ... wchar_t and std::wstring are defined with wrong implementations. So I need to either recompile the NDK stl or to change every place where I used wstring and wchar_t for a utf8 implementation or my own wchar_t/wstring implementation ... I don't know how to do the first option because it would be the desired way ...
Comment: Did you get a resolution to this?  I am still seeing in NDK R7 and CrystalX too does not have wcsicmp methods.
Answer: Try out CrystaX's NDK.  It has had stl support long before the official google one.  The current version (r5), which is based off the of the official ndk r5, is still beta 3, but it does have wchar_t support.
http://www.crystax.net/android/ndk-r5.php
Comment: Please confirm that CrystaX NDK really has wcsicmp()?  I don't see it.
Answer: I'm suffering from the same problem as you, but my only other thought is to load the strings via the JNI (as jstring* in native land), then convert them to UTF characters as necessary. Take a look at the available JNI string functions here:
http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#string_operations
Comment: How is this a solution for hundreds of std::wstring and wcsicmp uses?
Answer: Qt provides an excellent copy-on-write, international-friendly string implementation, QString, that is LGPLed.
You could, in theory extract it from the Qt source and use it in your own project. You will find the QString implementation in src/corelib/tools/qstring.h and .cpp in a Qt source download. You would also need the QChar, QByteArray, QAtomic, and QNamespace includes/classes (all under the corelib folder,) and you should define QT_NO_STL_WCHAR when compiling. (For this I would compile by hand or using my own script/Makefile.) Not simple, but once you get it up and running your life will be a lot simpler. It's better than reinventing the wheel, because it comes with loads of convenience functions and features.
Rather than stripping out just QString, you could also just use the QtCore module as a whole. See the android-lighthouse project for a Qt port to Android. (Also, it might be better to get your sources from there than from the above "vanilla" link, regardless of what you do.)
Comment: How is changing thousands of places into Qt calls a good solution? There are plenty of i18n libraries to recommend, but that wasn't the question. (I love Qt, but this is simply an irrelevant answer.)
Comment: Thanks, I already gone with my own utf8 string implementation.
Comment: @user548569: Could you post your own implementation as an answer? It's OK to answer your own questions if you've figured out the best way!


How can I do select of all user referral id?

Question: How can I do select of all user referral?
| id    |            user_id |    friend_id |
|-------|--------------------|--------------|
| 1     |                1   |          2   |
| 2     |                2   |          3   |
| 3     |                3   |          4   |
| 4     |                10  |          15  |

So in the above instance user 1 would have a referral trail of 3 because the person they referred, referred someone and then the person they referred referred someone else...
It might be better to do it as a php function but I'm struggling to get my head around it?
Below is my CODE
public function has_child($referee_referees){
        $this->db->select("user_account_phone");
        $this->db->where('referee_phone_number', $referee_referees);

        $query = $this->db->get('account');

        if($query->num_rows() == 0) {

            return FALSE;

        }

        return TRUE;
    }

    public function get_referee_referee_details($user_account_phone){
            $this->db->select('user_account_phone');
            $this->db->where('referee_phone_number', $product_phone_number);
            $query = $this->db->get('account');

            foreach($query->result() as $rows){
                $child_ids[$rows->user_account_phone] = $rows->user_account_phone;
                $referee_referees = $rows->user_account_phone;

                if($this->has_child($referee_referees)){
                    $child_ids[$rows->user_account_phone] = $this->get_referee_referee_details($rows->user_account_phone);
                }

                echo "
";
                var_dump($child_ids);
                echo "
";

                return $child_ids;

            }

        // return $child_ids;
    }

Comment: Is that a database? Are you using MySQL? What have you tried so far?
Comment: Show your code what have you tried.
Comment: select * from referral
Comment: iam using MYSQL
Comment: you should define your code first before asking others to code for you.
Answer: You will have to write some code to iterate over results.
This code is very rough and doesn't work for users that have referred more than one person.
function get_referral_count($userId) {
    $count = 0;
    // Just an example, you should escape this query!
    while ($res = // SELECT * FROM user_referral where user_id = $userId;)
    {
        $userId = $friendId;
        $count++;
    }
    return $count;
}

Answer: Interesting question
i tried shortest code for you
This will work - (you can test it with different data)
select friend_id from demo where user_id =1 OR user_id IN(select friend_id from demo)
Comment: dont forget to change your table name on that ... its demo in my query ...you can change that
Answer: If you have the data in memory(which is problematic for a big table)
$users = array(
    array("id" => 1, "user_id" => 1 , "friend_id" => 2,),
    array("id" => 2, "user_id" => 2 , "friend_id" => 3,),
    array("id" => 3, "user_id" => 3 , "friend_id" => 4,),
    array("id" => 4, "user_id" => 10, "friend_id" => 15,),
);

function getRefferrals(&$users, $userId)
{
    $referrals = 1;

    foreach($users as $user) {
        if($user["user_id"] == $userId) {
            $referrals += getRefferrals($users, $user["friend_id"]);
        }
    }

    return $referrals;
}

$referrals = getRefferrals($users, 1) - 1;
var_dump($referrals);

If you do not have the data in memory you could check if the user has "friend_id" and if it exists in the table
Comment: tHANK YOU, IAM USING CODEIGNITER, IAM FINDING WAYS TO GET IT WORK OUT. WILL LET YOU KNOW WHEN IT DOES. YOU ARE SUCH A GREAT PERSON
Comment: Hello Katona.abel Thanks so much it works. Iam very grateful for your help


Arrays in ASP.NET Core's XML Configuration Provider

Question: The ASP.NET Core (2.1) application I'm writing uses two configuration sources: JSON and XML (using framework's standard AddJsonFile and AddXmlFile. The XML settings have higher priority (e.g. should overwrite matching settings from the JSON file).
So far everything works fine, up until a point I had to create an array in XML file. Is it even possible?
Answer: Reference Configuration in ASP.NET Core: Bind an array to a class

The Bind supports binding arrays to objects using array indices in configuration keys. Any array format that exposes a numeric key segment (:0, :1, … :{n}) is capable of array binding to a POCO class array.

I'll provide two examples of how to use arrays in XML config file
Using the following config.xml
<configuration>
  <tvshow>
    <metadata>
      <series>Dr. Who</series>
      <title>The Sun Makers</title>
      <airdate>11/26/1977</airdate>
      <episodes>4</episodes>
    </metadata>
    <actors name="0">Tom Baker</actors>
    <actors name="1">Louise Jameson</actors>
    <actors name="2">John Leeson</actors>
    <legal>(c)1977 BBC https://www.bbc.co.uk/programmes/b006q2x0</legal>
  </tvshow>
</configuration>

The matching POCOs would look like
public class TvShow {
    public Metadata Metadata { get; set; }
    public string[] Actors { get; set; } //<-- TAKE NOTE
    public string Legal { get; set; }
}

public class Metadata {
    public string Series { get; set; }
    public string Title { get; set; }
    public DateTime AirDate { get; set; }
    public int Episodes { get; set; }
}

Given that <actors> elements' names, the resulting path would be
tvshow:actors:0
tvshow:actors:1
tvshow:actors:2

Which follows the convention stated in the documents
So in the following unit test it would pass as expected.
[TestClass]
public class Configuration_Should {
    [TestMethod]
    public void Bind_Xml_Arrays() {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddXmlFile("config.xml", optional: true, reloadOnChange: true)
        .Build();

        var tvShow = config.GetSection("tvshow").Get<TvShow>();

        tvShow.Should().NotBeNull();

        tvShow.Actors.Should().HaveCount(3);
    }
}

In another example, suppose the config.xml was modified to the following
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <tvshow>
    <metadata>
      <series>Dr. Who</series>
      <title>The Sun Makers</title>
      <airdate>11/26/1977</airdate>
      <episodes>4</episodes>
    </metadata>
    <actors>
      <names name="0">Tom Baker</names>
      <names name="1">Louise Jameson</names>
      <names name="2">John Leeson</names>
    </actors>
    <legal>(c)1977 BBC https://www.bbc.co.uk/programmes/b006q2x0</legal>
  </tvshow>
</configuration>

Would refactor the affected POCOs to
public class TvShow {
    public Metadata Metadata { get; set; }
    public Actors Actors { get; set; }
    public string Legal { get; set; }
}

//...Metadata omitted for brevity

public class Actors {
    public string[] Names { get; set; }
}

Again, following convention, the path for the array elements would need to look like
tvshow:actors:names:0
tvshow:actors:names:1
tvshow:actors:names:2

Which also behaves as expected when tested
[TestClass]
public class Configuration_Should {
    [TestMethod]
    public void Bind_Xml_Arrays() {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddXmlFile("config.xml", optional: true, reloadOnChange: true)
        .Build();

        var tvShow = config.GetSection("tvshow").Get<TvShow>();

        tvShow.Should().NotBeNull();

        tvShow.Actors.Names.Should().HaveCount(3);
    }
}

This, along with the documentation should provide enough of a foundation for you to properly structure your XML configuration to bind to arrays.
Answer: You can achieve array behavior by adding multiple tags with duplicated name of an array including Name attribute with element's "index in array". For instance the xml equivalent of this json
{
  "Values": [
    "value1",
    "value2",
    "value3"
  ],
  "Users": [
    {
      "Login": "log1",
      "Pass": "pass1"
    },
    {
      "Login": "log2",
      "Pass": "pass2"
    }
  ]
}

is
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <Values name="0">value1</Values>
  <Values name="1">value2</Values>
  <Values name="2">value3</Values>
  <Users name="0">
    <Login>log1</Login>
    <Pass>pas1</Pass>
  </Users>
  <Users name="1">
    <Login>log2</Login>
    <Pass>pas2</Pass>
  </Users>
</configuration>

If you examine Data of respective ConfigurationProviders that parsed xml and json configuration files

you will see that xml provider contains extra entries with name suffix  contain values of name attributes but essentially both collections are equal.
And some simple tests confirm equality of both configurations
public class Configuration
{
    public List<string> Values { get; set; }

    public List<User> Users { get; set; }

}

public class User
{
    public string Login { get; set; }

    public string Pass { get; set; }
}

//this code returns the same result for every configuration
var config = new Configuration();
_configuration.Bind(config);

string value = _configuration.GetValue<string>("Values:0");
string login = _configuration.GetValue<string>("Users:0:Login");

But it's not a full asnwer
There is a serious drawback in this approach. Before posting this answer I tested how xml configuration works with third party libraries. And I tried to convert json config to xml for Serilog. This json
//..
"WriteTo": [
  { "Name": "Console" }
]
//..

converts to
//..
<WriteTo name="0">
  <Name>Console</Name>
</WriteTo>
//...

And at this point an exception arises

System.FormatException: 'A duplicate key 'WriteTo:0:Name' was found. Line 11, position 11.'

Remember that extra entries from xml ConfigurationProvider? Well, now everything crashes because of them. So the conclusion is you cannot convert json array to xml if its objects contain Name key.
Some good news
There is a pull request that allows to use tags with duplicated names without Name attribute. This should fix that drawback. Also there is code for XmlConfigurationProvider from the same guy that you can already use in your project.
Comment: @MichałDudak Well, that's really unfortunate. As I mentioned in my answer consider using custom `ConfigurationProvider` from github or you can roll your own implementation if you desperately need xml configuration to be used.


SOLVED: S3 Object Lock with Cloudformation

Question: Is ObjectLock with Cloudformation Templates possibly still not implemented or is there an error in this template?
I tried to apply the following stack:
<code>---
Resources:
 TestBucketBucketProd:
  Type: 'AWS::S3::Bucket'
  Properties:
   BucketName: testbucket-prod
   VersioningConfiguration:
    Status: Enabled
   AccessControl: Private
   Tags:
    - Key: org
     Value: prod
   PublicAccessBlockConfiguration:
    BlockPublicAcls: true
    BlockPublicPolicy: true
    IgnorePublicAcls: true
    RestrictPublicBuckets: true
   ObjectLockConfiguration:
    ObjectLockEnabled: true
    Rule:
     DefaultRetention:
      Mode: GOVERNANCE
      Days: 2
Outputs:
 TestBucketBucketName:
  Value: !Ref TestBucketBucketProd
  Description: The name of the TestBucket S3 bucket.
</code>
Applying the stack leads to a failure in creation:
Object Lock configuration cannot be enabled on existing buckets (Service: Amazon S3; Status Code: 409; Error Code: InvalidBucketState; Request ID: 0AF2GTWV4W9P4X1C; S3
The bucket did not exist before applying the stack.
Any ideas?
Answer: With help from AWS support, I got the solution.
I'll write it here, because I don't think, it is quite intuitive.
You need to hand over "ObjectLockEnabled" twice.

   ObjectLockEnabled: true
   ObjectLockConfiguration:
     ObjectLockEnabled: "Enabled"
     Rule:
       DefaultRetention:
       Mode: "GOVERNANCE"
       Days: 2

ChatGPT helped me with a solution for the CLI:

aws s3api create-bucket --bucket zentrada-dev-testbucket \
 --region eu-central-1 \
 --create-bucket-configuration \
 LocationConstraint=eu-central-1 --object-lock-enabled-for-bucket

aws s3api put-object-lock-configuration \
 --bucket zentrada--dev-testbucket --object-lock-configuration \
'{"ObjectLockEnabled":"Enabled","Rule":{"DefaultRetention":{"Mode":"GOVERNANCE","Days":1}}}'


What does laissez vibrer mean on a wind instrument?

Question: I'm a bassoonist and when I play in a wind band I often get given a trombone part.
Sometimes I have to transpose a score because it is in E♭. And sometimes I have to transpose passages that are too high for me to play. I won't be of a standard to play the solos in Rite of Spring anytime soon. So I find myself writing out a lot of scores in a different clef or octave.
Last week I got a score that appeared to have a note tied to the following rest, as appears in bar 49. This is the way I transcribed it but the bar in the original trombone part looked like this

My notation reference (Gould, Behind bars, 72) tells me that this is an "open tie" which is also indicated laissez vibrer.
Now, I understand what this means for a stringed instrument. It means "don't damp the string at the rest".
But my reed won't continue vibrating when I stop blowing, and I must assume something similar applies to trombones.
So clearly the notation doesn't really mean laissez vibrer: it's some sort of metaphor. Can anyone explain what it does mean?
Comment: I'd put my money on it being a mistake
Comment: What piece (title, composer) is this, please?
Comment: @Aaron It's a band arrangement by J.-P. Labaste of *Bad Romance* by Lady Gaga (2009).
Comment: @ToddWilcox Umm. If I look again at the bars before it, that seems very plausible. Thanks for supplying a clearly necessary dose of commonsense.
Comment: Not a string part surely, unless it's on a cello or bass (especially a pizzicato note) or maybe on an open string. It's more common in music for percussion instruments, harps, pianos, guitars etc.
Comment: Note, this isn't necessarily a direction of *laissez vibrer*; until proven guilty it's just a misplaced slur. Occam's razor says it's much more likely that a note got deleted or a slur got accidentally copy/pasted than that it's meaningful.
Comment: @Andy Bonner: That's true.
Comment: I don't understand the new title. Is there some reason to have removed the original one? It seemed very clear.
Comment: @Aaron Thank you for pointing that out. It was finger trouble on my part.
Comment: Thanks for adding the original. See the edit on my answer.
Comment: Pretty sure that's **not** an l.v. tie! I think that's a fall.
Comment: @ToddWilcox I'm not sure I believe it's a fall on a ♪ in an ensemble rhythm part at a tempo of =76. But that's why I asked the question, so I'm not going to argue.
Answer: Was the music in a jazz style?   What we have here, I think, is a 'fall'.  An instruction to fall off the pitch at the end of a note.
Here are some more 'jazz articulations' from http://www.timusic.net/debreved/jazz-notation/
Comment: No, not jazz: pop. But in any case the composer or arranger should not be using a tapered tie/slur arc for any of those.
Comment: Agreed. Now that I see the mark I don't think it's a tie at all.
Answer: I have no experience with reed instruments, but on wind instruments like a flute you can end a note two ways: 1. by stopping to blow / slightly opening your lips vs 2. by letting your tongue stop the air stream. If I came across an open tie I would interpret it as the former, which -- very modestly -- causes a tapering off of the note.
And looking at your original trombone part I think it means that you also let the note slide down in pitch. On a flute I'd do that by rolling the instrument in. No idea if there is an equivalent on a bassoon.
Comment: Yeah I think it's a fall which is not uncommon in brass parts.
Comment: @ToddWilcox Ah, is that what it's called.
Answer: I'll be "that guy" and go against the crowd here.  I've seen similar markings here and there which were deliberately created by the composer to stress that the played note should be held right up to the start of the rest.  This stops musicians (either inexperienced or lazy) from terminating the sound sooner than intended.
So it's not exactly "laissez vibrer" but more of an indicator of desired note length.
Comment: Why not just put a tenuto over the note?
Comment: This seems plausible.  Maybe what was meant was "if this were a different instrument, *laisser vibrer* would make literal sense; here, interpret it figuratively."
Answer: I'd like to thank all the people who answered or commented. The consensus appears to be it depends, so I can't reasonably accept one answer above the others.
The first issue, raised in an early comment, was Is this accidental or deliberate? I initially assumed it was deliberate, but that assumption was no doubt coloured by the fact that I use Dorico, which makes doing it by accident very hard. Since it's not a notation I ever need, even to read, it took me a while to find out what it was, and how to do it deliberately.
The comments reminded me that it is in fact easy to do, deliberately or accidentally, in other software: simply copy a bar with a pair of slurred notes and delete the second of the pair in the copy. Sibelius, MuseScore, and even old Encore will all let you do that. Sibelius will let you do it with tied notes, too, which looks better, though you might not like the resulting playback. I'm not familiar with Finale, which I know for a fact was used to prepare the trombone score, but I'm guessing it's similar.
And if it is deliberate, then the right interpretation might depend on the kind of music, or the composer/arranger's tendency to make the slur arc serve for things other than slurs: suggestions here included tenuto, > al niente and fall.
That would be unremarkable. Even when used conventionally, slurs have different meanings for wind (legato), strings (bow-stroke), and voice (syllable).
Answer: The answer is in the two bars before. As you can see the last two 8th notes are connected with a slur in both bars. The composer wants you to play that last B in m49 as if it legatoes into a next note, except it's a rest this time.
So yes, you might say it's a tenuto, but the intent is slightly different. I'd play it as a tenuto with a rather smooth ending, not too abrupt.
I don't find this ambiguous at all TBH and would notate it the same way.


select2 returns null on refresh

Question: I have different users on the same page when I click on the user name it shows the qualification of that user.
when the page loads select2 shows qualification of user1.
when I click on user2 it shows qualification of user2
but again when I click on user1, I get empty select2 for user1, whatever I do until I refresh the page again.
My data is coming from ajax request.
success: function (data) {

    var qualif = data.qualifs //returns value/1/2/3 single/multiple

    alert(qualif); //always returns some value

    if(qualif.length>0){

        $.each(qualif, function (index, value) {
            $(`.qual_id option[value=${value}]`).attr('selected', true);
        });
    }

    if(qualif.length<1){
       $("#qual_id option:selected").prop("selected", false);
    }
}

$('#qual_id').select2({
  width: 'resolve',
  placeholder: "QUALIFICATION",
  maximumSelectionLength: 2,

});

view:
<select name="qual_id[]" id="qual_id"   class="qual_id" multiple="multiple" >
    @foreach(Session::get('qualifs') as $qualif)
        <option value="{{@$qualif->id}}" > {{@$qualif->qual}}</option>
    @endforeach
</select>

what I want is if there is value in qualif variable, which is showing
  correctly in alert, it should also show up in select2 each time I click, right now it is showing once. and if there's no
  value in qualif/alert select2 should be empty.

I tried following different answers from StackOverflow but nothing worked.
i added this when document is ready, or beforesend function this didn't work also.
$('#qual_id').val(null).trigger('change');

Complete JS:
$(document).ready(function($)  {

$('body').on('click','.edit-btn',function(){

            $.ajax({
                type: "GET",
                url: "teachers/" + $(this).attr("value") + "/edit",
                dataType: 'json',

beforeSend: function(){

success: function (data) {

var qualif = data.qualifs //returns value/1/2/3 single/multiple

alert(qualif); //always returns some value

if(qualif.length>0){

$.each(qualif, function (index, value) {
$(`.qual_id option[value=${value}]`).attr('selected', true);
});
}

if(qualif.length<1){
$("#qual_id option:selected").prop("selected", false);
}
$('#qual_id').select2({
width: 'resolve',
placeholder: "QUALIFICATION",
maximumSelectionLength: 2,
}  });

    });});  });

Comment: try reinitializing select2() after changes
Comment: how, where in ajax???
Comment: can you add the detailed js code
Comment: @ab.in i added complete js please check
Comment: Put `$('#qual_id').select2()` inside Ajax success method or just after the Ajax function.
Comment: again it loads, qualification first time, but when i click again it clears everything in select2 and never shows qualifications again. until i refresh.
Comment: but alert is showing values.
Answer: I'm not sure how are you returning the data, but I've tried to replicate it below. To achieve the similar structure you'd have to have PHP output like $data[]['value'] and $data[]['text']

$(document).ready(function() {
    var qualif = []
    qualif.push({"value": 1, "text": "single"});
    qualif.push({"value": 2, "text": "double"});

    // If you need to populate them too.
    /** $.each(qualif, function (index, data) {
      $('#qual_id').append($('<option>', { value: data.value, text: data.text }, '<option/>'));
    });*/
    $('#qual_id').val(qualif.map(a => a.value)).trigger('change');

    $('#qual_id').select2({
        width: 'resolve',
        placeholder: "QUALIFICATION",
        maximumSelectionLength: 2,
    });

});
#qual_id {
  width: 200px
}
<select name="qual_id[]" id="qual_id"   class="qual_id" multiple="multiple" >
    <option value="" ></option>
    <option value="1">single</option>
    <option value="2">double</option>
</select>

    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet"/>

select2 returns null on refresh


What is the difference between the various log messaging severity levels?

Question: There are several types of log messaging severity levels - emergency, critical, error, alert, info, notice, warning, debug. What does each level mean? Example, emergency and critical sound similar, what's the difference? Also, info and notice sound similar.
Answer: The code is the best manual :)
<code>/**
 * Log message severity -- Emergency: system is unusable.
 */
define('WATCHDOG_EMERGENCY', 0);
/**
 * Log message severity -- Alert: action must be taken immediately.
 */
define('WATCHDOG_ALERT', 1);
/**
 * Log message severity -- Critical conditions.
 */
define('WATCHDOG_CRITICAL', 2);
/**
 * Log message severity -- Error conditions.
 */
define('WATCHDOG_ERROR', 3);
/**
 * Log message severity -- Warning conditions.
 */
define('WATCHDOG_WARNING', 4);
/**
 * Log message severity -- Normal but significant conditions.
 */
define('WATCHDOG_NOTICE', 5);
/**
 * Log message severity -- Informational messages.
 */
define('WATCHDOG_INFO', 6);
/**
 * Log message severity -- Debug-level messages.
 */
define('WATCHDOG_DEBUG', 7);
</code>
Answer: The levels are mostly arbitrary below alert, and just represent a general priority of "badness".
In theory, a minimum log level could be set up in core, so that you could enable logging on a live site, but only have certain levels logged (say, emergency, critical, error, and alert) and the rest ignored.  To my knowledge, this hasn't been implemented (I would love to be shown wrong about this).
The current usage has more to do with filtering on the recent log messages page (admin/reports/dblog).
Comment: The ability to turn off debug messages on any environment would be good.


Minor Key Confusion

Question: I find when playing in a minor key I always end up playing the major version of the V chord. (So in Em I always end up playing a "B major" chord.)
However, the third of this chord should be played as a minor when looking at the key and it seems like using an accidental as frequently as I do is not really in the spirit of accidentals.
Am I making a mistake in my music theory? Am I playing the key I think I am? Is this just a mode? Do I need to start playing more B minors?
Please bear in mind I am relatively new to music theory and would appreciate it if you treat me like an idiot. Thanks.
Comment: Does this answer your question? [Understanding minor key harmony](https://music.stackexchange.com/questions/16248/understanding-minor-key-harmony)
Comment: Three years on from asking this question I think the first comment on your link is the best answer. "This is music, you can do whatever you want."
Answer: Two points.
First:  the scale of a key or mode is a framework.  It doesn't instruct you NOT to use other notes.  A piece of music that used ONLY diatonic (in the scale) notes and chords would be very basic and possibly very boring (although there's also beauty in simplicity).
Second: in traditional harmony the dominant chord of both a major or minor key is normally major.  If we're writing the sort of music that deals in dominant > tonic relationships (and that's the bulk of Western music for the last few hundred years) the dominant chord NEEDS to tend toward the tonic chord.  A large part of that tension comes from it including the 'leading note', the note a semitone below the tonic.
We don't HAVE to use major dominant chords.  But we don't have to reject then either.   You're doing nothing 'wrong' by sharpening that 7th into a leading note.   Equally, you're doing nothing 'wrong' by investigating the sound of a minor dominant chord.
"Theory describes, it does not command".
Comment: "Theory describes, it does not command" - awesome. Who said that? You? If so, you are a indeed a musical sage. (And it's not only applicable to music - it's applicable to everything.)
Comment: I don't know if I can claim copyright!  But it constantly needs saying.  Here's the long version: "Show me a song, I'll find a theoretical justification for its chord sequence. It might be simply 'use the chords within the scale'. It might need 'secondary dominants' to explain some out-of-scale notes. If that fails, we might have to 'borrow' chords from a related key or mode. Or maybe we'll have to invoke 'planing' which can neatly allow any chord chromatically adjacent to a 'permitted' one. Whatever. But rest assured, whatever you show me, I'll find some 'theory' to explain it."
Answer: Although the D in the scale of B will be natural in the key signature, it will often be sharpened when leading to the key note E. Think of the melodic minor scale for E. D will be sharpened on the way up but natural on the way down.
So, if your chord is to resolve to the tonic E, the sharpened D will work nicely.  You might find that a B minor chord works well if it goes somewhere else next.
Comment: So I'm not using the key incorrectly? I was mainly confused about whether or not I was actually IN a minor key or something else entirely.Does melodic minor result in using accidental symbols on the way down?
Comment: I don't think that you are using it incorrectly.  Look at some minor key melodies and you will see the 6th and 7th notes are often sharpened.  If you insisted on your V chord being minor, you would sometimes get a nasty clash with the melody.  Accidental symbols on the way down will be less common unless they are just undoing a previous sharpening accidental earlier in the same bar.  Note that when I say a "sharpening accidental", this is not necessarily a sharp sign, it could be a natural cancelling a flat in the key signature.
Answer: Say we play in Em (relative major is G, one sharp). The V chord of Em should be Bm (B D F#). Although playing B major adds a sharp to D it is not considered as an "error" since it can naturaly lead to Em. It's clearer if you play B7 (B D# F# A) as it's the dominant chord of Em.
Sometimes the II chord of the major scale (Am in G) can also be played as a II7 to lead to the V chord. Even the I of the minor scale can be «dominant»-ized.
Try to play those progressions:

G Bm Em Em Am D7 G G (nothing special)
G B7 Em Em Am D7 G G (B7 leads to Em in a strong way)
G B7 Em E7 Am D7 G G (same with E7 to Am)
G B7 Em E7 A7 D7 G G (same with A7 to D7)
Answer: Yes, you are making a mistake in the theory you know. Each major key sig. has a relative minor to go with it - C- Am: G- Em; Bb- Gm etc. Whilst the majors adhere exactly to the # or b in their key sig., minors cannot and do not.
That's because the notes that make up the minor scales vary in their 6th and 7th notes. It's the way they are. Take A minor. 6th note can be F or F#, 7th can be G or G#. In your scenario, E minor can have C or C#, and D or D#.
The D/D# anomaly makes the chord based on B either B minor or B major respectively. Both are quite acceptable, and both are used. The B major, as you find by using it and it sounds good to you, uses the leading note of D# to sound more decisive going into E minor, but there's nothing in the theory that says we must use that, or we must use B minor. Whatever fits better is what we should use. Please don't use theory as a set of rules. It's more a set of guidelines. Usual adage - if it sounds good, it usually is - whether the 'rules' get broken or not!
Answer: One way to look at things is that, for music in a minor key, the chord on scale step 5 is minor (Gm in the key of Cm). In a cadence, scale step is usually raised to be a half step below the tonic (scale step 8). In non-cadential  passages, often the minor chord on the 5th step is used. For example, in a cycle of fifths, one might have (in the major): I,IV,vii0,iii,vi,ii,V,I etc. In a minor key, an ending cycle might look like: i,iv,VII,III,VI,ii0,V,i. However if the passage contains two cycles one often gets: i,iv,VII,III,VI,ii0,v,i,VII,III,VI,ii0,V,i. The "interior" part of the passage is just a string of chords, not a cadence. Of course, this treatment is optional and neither is incorrect; they are just different.
A similar example occurs in sequence decent by thirds (Pachelbel canon for one example). In a major key: I,V,vi,iii,IV,I6,ii6,V (where the descending bass turns around for the last two chords, not the only choice.) In minor keys the following is often found, i,v,VI,III,iv,i6,ii06,V in a cadential context; the last chord can be v if not meant cadentially.
As an aside: this sequence is very flexible. In a minor key, the chords on the scale step 5 can be either major or minor; the iii chord can be a I64 (not really cadential or arpeggiated); the ii0 chord can be iv; the ii chord can be IV; the ii or ii0 chords can be in root position; and lastly the final V (or v) chord can be a vii06 leading to a I (or i) chord which can give a bass line that descends an octave.
Comment: From the question, it's doubtful that the OP understands I64, and I don't understand viio6.
Comment: Just to explain as I don't know about this site's editor: I64 would be  tonic chord with the fifth scale step in the base: in C-major this would be G-C-E  or G-E-C reading upward. The symbol vii06 is a diminished 7th with base note on the second scale degree. (I've written a half-diminished seventh as I didn't know if special symbols would appear here). In C-major this would be D-F-A-B. Note that using this chord allows one to add chords to a descending scale that spans an octave.  While one may not always have 8 or 9 noted bass lines; shorter ones can be lifted from such a construction.
Comment: There's a reference in the following link.  https://dmitri.mycpanel.princeton.edu/files/pdfs/MUS106handouts.pdf

Minor Key Confusion


Trying to understand an assumption in the proof of Mann's Theorem

Question: I am trying to follow the reasoning in the proof of Mann's Theorem:
$$d(C) \ge \min(d(A)+d(B),1)$$
I am clear that we can assume that:

$d(A) + d(B) \le 1$
We only need to prove that for every $n \ge 1$, $\exists m \in [1,n]$ such that $C(n) - C(n-m) \ge (d(A) + d(B))m$
For any $C(n)$, we can assume $n \notin C$

Here's where I am not clear.
Using the proof from Gelfond & Linnik Elementary Methods in the Analytic Theory of Numbers, they assume that $C$ is what they term normal:

We shall describe the system of numbers $H\subset[0,n]$ as normal if, for any numbers $f\in[1,n];f'\in[1,n];f\notin(H);f'\notin{H}$, we have $f + f' - n \notin H$.  If the sequence $C$ possesses the property that segments of it in the segment $[0,n]$ form a normal system, the lemma is easily proved for $C(n)$.

Then, they state the following conclusion:

In fact, let $m$ be the least positive integer not belonging to $C$; then $m < n$, since $n \notin C$ by hypothesis.

What about the situation where $C(n) = n-1$.  In that case, wouldn't $m=n$.  So, is the assumption that $C(n) < n-1$?  How can we be sure that $m < n$?
Here's my understanding of Schnirelmann Density.

$A,B$ are infinite sequences of integers starting with $0$ with in sequential order such as $0, a_1, a_2, \cdots$ where $0 < a_1 < a_2 < \cdots$
Schnirelmann density is defined as:
$$d(A) = \inf\limits_{n}\frac{A(n)}{n}$$

where: $$A(n) = \sum\limits_{0<a_i\le{n}}{1}$$

So, it is clear that:  $$0 \le \frac{A(n)}{n} \le 1$$
$C = A+B$ where $+$ is the sumset.

Edit:  Added detail on normal since this may be part of the answer.
Answer: It looks like it is a typo and it should read $m \le n$.
Linnik and Gelfond say in their footnote that the proof is taken from A. Ya. Khinchin, Three Pearls of Number Theory (1952).  This book is now published as a Dover edition which I tracked down.  In A. Ya. Khinchin's version: $m \le n$.
If $m=n$ with $n \notin C$, their argument applies since:
$$C(n) - C(n-m) = C(n) - C(0) = n - 1$$
Let $a_1, a_2, \cdots a_r$ be the numbers in $A(n)$.
Let $b_1, b_2, \cdots b_s$ be the numbers in $B(n)$.
From the assumption that $n \notin C$, we can see that $a_1, a_2, \cdots, a_r, n-b_1, n-b_2, \cdots, n-b_s, n$ are all distinct in $[1,n]$ which gives us:
$$A(n) + B(n) \le n-1$$
So, it follows that:  $C(n) - C(n-m) = C(n) - C(0) \ge A(m) + B(m) \ge (d(A) + d(B))m$


Como debugar para saber qual procedure ou tabela estão sendo executados com Asp.Net

Question: Estou há 10 dias no novo trabalho, e estou tendo algumas dificuldades - o que é normal, dependendo da dificuldade.
Tenho uma Table dentro de um Repeater, que é alimentada por uma Procedure.
Como eu faço com o Debug, para encontrar a Procedure que está sendo executada através dessa linha?
<asp:HiddenField ID="hdfCdTipoUsuario" runat="server" Value='<%# Eval("CdTipoUsuario")%>' />

Esse é todo o repeater e a table:
<asp:Repeater ID="rptGerenciaProcessos" runat="server"
    onitemcommand="rptGerenciaProcessos_ItemCommand"
    onitemdatabound="rptGerenciaProcessos_ItemDataBound">
    <HeaderTemplate>

            Consultar processo
            Priorizar
            Priorizado
            Nº do processo
            Data de abertura
            Início da análise
            Término na análise
            Situação
            Grupo
            Cota
            Etapa
            Analista

    </HeaderTemplate>
    <ItemTemplate>

                <asp:Button ID="btnConsultarProcessos" OnClick="btnConsultarProcessos_Click" runat="server" Text="Consultar processo" CommandName="ConsultaProcessoGestor" CssClass="acessos" />

                <asp:HiddenField ID="hdfCdProcesso" runat="server" Value='<%# Eval("CdProcesso")%>'/>
                <asp:HiddenField ID="hdfCdAnalise" runat="server" Value='<%# Eval("CdAnalise")%>' />
                <asp:HiddenField ID="hdfCdUsuario" runat="server" Value='<%# Eval("CdUsuarioAn")%>' />
                <asp:HiddenField ID="hdfCdWorkFlowItem" runat="server" Value='<%# Eval("CdWorkFlowItem")%>' />
                <asp:HiddenField ID="hdfCdTipoUsuario" runat="server" Value='<%# Eval("CdTipoUsuario")%>' />
                <asp:LinkButton ID="lkbPriorizar" runat="server" CssClass="clickPriorizar">Priorizar</asp:LinkButton>

            <input type="checkbox" id="ckbPriorizado" <%# Eval("IcPriorizado")%> disabled>
            <asp:Label Text='<%# Eval("CdProcesso")%>'      runat="server" />
            <asp:Label Text='<%# Eval("DtCriacao")%>'       runat="server" />
            <asp:Label Text='<%# Eval("DtInicio")%>'        runat="server" />
            <asp:Label Text='<%# Eval("DtFim")%>'           runat="server" />
            <asp:Label Text='<%# Eval("NmWorkFlowItem")%>'  runat="server" />
            <asp:Label Text='<%# Eval("CdGrupo")%>'         runat="server" />
            <asp:Label Text='<%# Eval("CdCota")%>'          runat="server" />
            <asp:Label Text='<%# Eval("NmTipoStatus")%>'    runat="server" />
            <asp:Label Text='<%# Eval("NomeUsuario")%>'     runat="server" />

    </ItemTemplate>
    <FooterTemplate>

    </FooterTemplate>
</asp:Repeater>

A dificuldade está em saber como eu debugo a linha ...Eval("Meu_Campo")...
Answer: O conteúdo dinâmico do Repeater é carregado normalmente do seguinte modo:
rptGerenciaProcessos.DataSource = RetornaDados(parametro1, parametro2);
rptGerenciaProcessos.DataBind();

Para saber qual Procedure é executada para alimentar o Repeater, você deve percorrer o caminho ao inverso do que está definido da atribuição do DataSource.
Pode ser que o caminho fique mais complicado se essa atribuição não está explícita no code-behind de sua página (normalmente [nome_pagina].aspx.cs). Ele pode estar chamando uma função genérica de outro lugar para carregar os dados:
CarregaRepeater(rptGerenciaProcessos, RetornaDados(parametro1, parametro2));

Eu aconselho, a procurar por RetornaDados(parametro1, parametro2)s no código fonte da página. Se não achar procure por todas as ocorrências do rptGerenciaProcessos. Use o "Find All".
Sobre debugar o Eval("Meu_Campo"):
Esse trecho de código define que o valor na coluna "Meu_Campo" irá nesse campo da sua página, pra cada item do DataSource. Você pode sobrescrever esse comportamento através do evento OnItemDataBound:
void rptGerenciaProcessos_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        Processo item = (Processo)e.Item.DataItem;
        // Coloque um breakpoint aqui e analise a variável "item"
        if (Item.CdTipoUsuario == 5) {
            ((Label)e.Item.FindControl("hdfCdTipoUsuario")).Value = "50";
        }
    }
}

Answer: Quando você cia um página sua_pagina.aspx é criado o CodeBehind dela com  nome_sua_pagina.cs é ai que esta sendo carregado seu repeater.
Edite esta página .cs e encontre rptGerenciaProcessos.DataSource nele estará o conteúdo dos dados.
Pode ser carregado por um objeto ou pela execução de método.
No método que estará o nome da SP ou expressão SQl usada para a carga.


Organic Groups -- Load group from gid

Question: This seems like it should be a simple operation, but I must be completely blind because I can't find anything about it in the API docs at http://www.drupalcontrib.org/api/drupal/contributions!og!og.module/7.
I have a $gid. I need to check if it really is the gid of a valid group, and have access to the group information if it is.
I did find an old post somewhere saying to use og_load(), but that is out of date -- that function does not exist in the current API. Nor can I find a function with similar functionality.
There has to be a simple way to do this; it's hardly an outlier use-case. What am I missing?
Answer: You don't need to use group_load anymore! Use node_load instead. You gid is equivalent to nid.
Comment: With node_load .. It dosn't display if current user (subscribe or not) ..
Answer: you could use og_get_group() http://api.drupalhelp.net/api/og/og.module/function/og_get_group/7 API to get the og group. It Return a loaded group entity if exists or create a new one.
Comment: Actually, that's the 7.x 1.0 API. That function no longer exists in the 7.x 2.0 API.

Organic Groups -- Load group from gid


Custom textfield element, different $form_state value and element '#value' value

Question: I have a textfield element (named 'bus_station'), which holds a bus station name.
I am looking for a way to create a custom 'bus_station' element (possibly extending Textfield class), which would:

Still expect a text input in form of bus station name (and retain it during $form_state->setRebuild() and show it to user), but
In $form_value, after form submit, either provide a FALSE (custom code in Element definition doesn't recognize input as a station) or TRUE (it does recognize a station)? Or Entity ID (ID of a matched bus station) and/or NULL (no match)?
Answer: This can be done via Form API callback implementation:
Inside module/src/Element/ElementClass.php, in public function getInfo() {} define a callback, e.g. processMyElement under '#process' key:
public function getInfo() {
  $class = static::class;
  return [
    // [...] Some other definitions.
    '#process' => [
      [$class, 'processMyElement'],
    ],
  ];
}

And then perform two complementary operations.
(1) Process the user input and return a value which will be provided in the '#form_state' values in valueCallback() method:
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  $output = parent::valueCallback($element, $input, $form_state);
  if ($input == 'SOMETHING') {
    $output = 'SOMETHING_ELSE';
  }
  return $output;
}

(2) And vice versa, in the processMyElement() method return the original value:
public static function processMyElement(&$element, FormStateInterface $form_state, &$complete_form) {
  if ($element['#value'] == 'SOMETHING_ELSE') {
    $element['#value'] = 'SOMETHING';
  }
  return $element;
}

Usage example
I have used this to extend Drupal\Core\Render\Element\Textfield class in combination with JS Storage Complete. That gives me Autocomplete (without need for a callback URL). This autocomplete returns Entity ID obtained via entity load based on unique label inside valueCallback() method, but user is still presented with its text input (obtained by returning $entity->label() inside processMyElement() method).


What are RMSE SD and Rsquared SD metrics in resampling results using R package:caret?

Question: I've been doing predictive modelling with R package caret. When resampling regression models, I get the traditional RMSE and Rsquared metrics, but also RMSE SD and Rsquared SD, for which I haven't found explanation in the manuals or documentations. Please, could anyone enlighten me?
Reproducible code:
library(mlbench)
data(BostonHousing)
library(caret)

ctrl <- trainControl(method = "cv", number = 2)
lmFit <- train(medv ~ ., data = BostonHousing, method = "lm", trControl = ctrl)
print(lmFit)

outputs:

Linear Regression

506 samples
13 predictors

No pre-processing
Resampling: Cross-Validated (2 fold)

Summary of sample sizes: 254, 252

Resampling results

RMSE  Rsquared  RMSE SD  Rsquared SD
4.91  0.721     0.202    0.00304

Cheers
Maria
Answer: It is the standard deviation of the resamples:
> lmFit$resample
          RMSE  Rsquared Resample
    1 4.702857 0.7283872    Fold1
    2 5.266187 0.6838433    Fold2
> apply(lmFit$resample[, 1:2], 2, sd)
      RMSE   Rsquared
0.39833479 0.03149727
> lmFit
Linear Regression

506 samples
 13 predictors

No pre-processing
Resampling: Cross-Validated (2 fold)

Summary of sample sizes: 253, 253

Resampling results

  RMSE  Rsquared  RMSE SD  Rsquared SD
  4.98  0.706     0.398    0.0315

Max
Answer: If you use crossvalidation, it is very convenient to retrieve the metrics through the caret model objects. This is especially useful if you train several models as you can retrieve the MAE, RMSE and Rsquared all at once.
Let's say you have trained two models, lm and gbm, with 10-fold cross-validation by train(preProcess(method = "cv", number = 10).
Then you can retrieve the metrics by
models.list <- list(fit.lm, fit.gbm)

models.list %>%
  # get resamples results
  resamples %>% .$values %>%
  # only select numeric columns
  select_if(is.numeric) %>%
  # calculate standard deviation
  summarise_all(.funs = sd)

This returns the standard deviations for all metrics:
      lm~MAE    lm~RMSE lm~Rsquared    gbm~MAE   gbm~RMSE gbm~Rsquared
1 0.02168322 0.02877163  0.03492542 0.02834266 0.03550696   0.04176382

Of course, this is even more useful if you have more trained models in your model list.


NoClassDefError in mllib 1.1.0

Question: I am trying to run the twitter classifier - https://github.com/databricks/reference-apps. It uses Spark to analyse those feeds.
I've loaded the project on IntelliJ and one of the dependency org.apache.spark.mllib which shows up in the external libraries is not working properly.

I get a java.lang.NoClassDefFoundError on running it. But the dependency is already present.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/mllib/feature/HashingTF
  at com.databricks.apps.twitter_classifier.Utils$.<init>(Utils.scala:12)
  at com.databricks.apps.twitter_classifier.Utils$.<clinit>(Utils.scala)
  at com.databricks.apps.twitter_classifier.Collect$.main(Collect.scala:26)
  at com.databricks.apps.twitter_classifier.Collect.main(Collect.scala)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.mllib.feature.HashingTF
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 9 more

My build.sbt looks like this:
import AssemblyKeys._

name := "spark-twitter-lang-classifier"

version := "1.0"

scalaVersion := "2.10.4"

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.1.0" % "provided"

libraryDependencies += "org.apache.spark" %% "spark-mllib" % "1.1.0" % "provided"

libraryDependencies += "org.apache.spark" %% "spark-sql" % "1.1.0" % "provided"

libraryDependencies += "org.apache.spark" %% "spark-streaming" % "1.1.0" % "provided"

libraryDependencies += "org.apache.spark" %% "spark-streaming-twitter" % "1.1.0"

libraryDependencies += "com.google.code.gson" % "gson" % "2.3"

libraryDependencies += "org.twitter4j" % "twitter4j-core" % "3.0.3"

libraryDependencies += "commons-cli" % "commons-cli" % "1.2"

resolvers += "Akka Repository" at "http://repo.akka.io/releases/"

assemblySettings

mergeStrategy in assembly := {
  case m if m.toLowerCase.endsWith("manifest.mf")          => MergeStrategy.discard
  case m if m.toLowerCase.matches("meta-inf.*\\.sf$")      => MergeStrategy.discard
  case "log4j.properties"                                  => MergeStrategy.discard
  case m if m.toLowerCase.startsWith("meta-inf/services/") => MergeStrategy.filterDistinctLines
  case "reference.conf"                                    => MergeStrategy.concat
  case _                                                   => MergeStrategy.first
}

Answer: I think you have to change mllib dependency to scope runtime. In maven you can add runtime tag to define the scope of dependency to runtime.

NoClassDefError in mllib 1.1.0


Exercise on Banach space

Question: Let $exp:\mathcal L(E)\to \mathcal L(E)$ the exponential map, where $E$ is a Banach space. $exp(A)=e^A= \sum_{n=0}^\infty \frac{A^n}{n!}$ and $A\in\mathcal L(E)$. I proved using partial sum $s_n= \sum_{k=0}^n \frac{A^k}{k!}$, that $||exp-s_n||_{\infty,B_R}=o(1)$ for $n\to\infty$ so $exp$ is uniform limit of continuous
function so exp is continuous. Then the question is find the differential of the map exp: $Dexp(A)[H]$. I don't know how to proceed for this second point. Any ideas?
Note:($B_R=B(0,R)$) and $$Dexp(A)[H]= \ lim_{t\to0}\frac{e^{A+tH}-e^A}{t}$$
Answer: Borrowed from https://fr.wikipedia.org/wiki/Exponentielle_d%27une_matrice#Application_t_%E2%86%A6_etX :
$$\begin{align}D\exp(A)(H)&=\sum_{i,j\ge0}\frac{A^iHA^j}{(i+j+1)!}\\&=\sum_{i,j\ge0}\mathrm B(i+1,j+1)\frac{A^i}{i!}H\frac{A^j}{j!}\\&=\int_0^1e^{(1-t)A}H{\rm e}^{tA}\,dt\end{align}$$
where $\mathrm B$ is the beta function.
When $AH=HA,$ this boils down to $D\exp(A)(H)=H\exp(A).$
Comment: Yes, exactly the same. You can even replace $\mathcal L(E)$ by any Banach algebra.
Comment: we do not have matrix in Banach, so you are saying is the same

Exercise on Banach space


Can not run the the tflite model on Interpreter in android studio

Question: I am trying to run a TensorFlow-lite model on my App on a smartphone. First, I trained the model with numerical data using LSTM and build the model layer using TensorFlow.Keras. I used TensorFlow V2.x and saved the trained model on a server. After that, the model is downloaded to the internal memory of the smartphone by the App and loaded to the interpreter using "MappedByteBuffer". Until here everything is working correctly.
The problem is in the interpreter can not read and run the model.
I also added the required dependencies on the build.gradle.
The conversion code to tflite model in python:
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from tensorflow.keras import regularizers
#Create the network
model = Sequential()
model.add(LSTM(...... name = 'First_layer'))
model.add(Dropout(rate=Drop_out))
model.add(LSTM(...... name = 'Second_layer'))
model.add(Dropout(rate=Drop_out))

# compile model
model.compile(loss=keras.losses.mae,
optimizer=keras.optimizers.Adam(learning_rate=learning_rate), metrics=["mae"])

# fit model
model.fit(.......)
#save the model
tf.saved_model.save(model,'saved_model')
print("Model  type", model1.dtype)# Model type is float32 and size around 2MB

#Convert saved model into TFlite
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
tflite_model = converter.convert()

with open("Model.tflite, "wb") as f:
    f.write(tflite_model)
f.close()

I tried also other conversion way using Keras
# converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
# tflite_model = converter.convert()

After this step, the "Model.tflite" is converted and downloaded to the internal memory of the smartphone.
Android studio code:
  try {
        private Interpreter tflite = new Interpreter(loadModelFile());
        Log.d("Load_model", "Created a Tensorflow Lite of AutoAuth.");

    } catch (IOException e) {
        Log.e("Load_model", "IOException loading the tflite file");

    }

private MappedByteBuffer loadModelFile() throws IOException {
    String model_path = model_directory + model_name + ".tflite";
    Log.d(TAG, model_path);
    File file = new File(model_path);
    if(file!=null){
    FileInputStream inputStream = new FileInputStream(file);
    FileChannel fileChannel = inputStream.getChannel();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    }else{
        return null;
    }
}

The "loadModelFile()" function is working correctly because I checked it with another tflite model using MNIST dataset for image classification. The problem is only the interpreter.
This is also build.gradle's contents:
android {
aaptOptions {
    noCompress "tflite"
}
 }
  android {
     defaultConfig {
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
      }
    }

dependencies {
     implementation 'com.jakewharton:butterknife:8.8.1'
     implementation 'org.tensorflow:tensorflow-lite:0.1.2-nightly'
     annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     //noinspection GradleCompatible
     implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }

Whenever I run Android studio, I got one of the following errors:
1-

or
2-

I have gone through many resources and threads and read about saving trained models, TFlite conversion, and Interpreters.
I am trying for 5 days ago to solve this issue but have no hope. Can anyone give a solution for this?
Comment: I think nightly versions have been replaced with SNAPSHOT ones. So snapshot ones are the latest...use them.
Comment: Have you visualized the tflite file with netron.app to see if it is valid? Upload it somewhere and provide us a link to verify it.
Comment: No, I did not visualized it. Can you give a tutorial link about how to do that?
Comment: Just open netron.app in your browser and drag/drop it inside.
Comment: I finally fixed this problem by adding the following to app's dependencies:

implementation 'org.tensorflow:tensorflow-lite:2.5.0'
and  implementation 'org.tensorflow:tensorflow-lite:0.1.2-nightly'.

I practice this useful link: https://developer.android.com/codelabs/digit-classifier-tflite#2 to solve my problem.
Comment: I do not think you need both. Try to use the snapshot intead https://www.tensorflow.org/lite/guide/build_android#use_nightly_snapshots
Comment: @Farmaker Yes you are right, I can run the code correctly using only "implementation 'org.tensorflow:tensorflow-lite:2.5.0". But when using only the " implementation 'org.tensorflow:tensorflow-lite:0.1.2-nightly", the code gives the same error.
Answer: Referring to one of the most recent TfLite android app examples might help: Model Personalization App. This demo app uses transfer learning model instead of LSTM, but the overall workflow should be similar.
As Farmaker mentioned in the comment, try using SNAPSHOT in the gradle dependency:
implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'

To load the model properly, can you try:
protected MappedByteBuffer loadMappedFile(String filePath) throws IOException {
    AssetFileDescriptor fileDescriptor = assetManager.openFd(this.directoryName + "/" + filePath);

    FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileDescriptor.getStartOffset();
    long declaredLength = fileDescriptor.getDeclaredLength();
    return fileChannel.map(MapMode.READ_ONLY, startOffset, declaredLength);
  }

This snippet can also be found in the GitHub example link I posted above.
Answer: loadMappedFile have impl at tensorflow lite utils
import org.tensorflow.lite.support.common.FileUtil;

    MappedByteBuffer tfliteModel = FileUtil.loadMappedFile(activity, getModelPath());


How can I remove space between div and the page

Question: When the browser size make to small I get a space in right side between div and the browser window margin.How can I remove that space?
My screen shot is here
https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/538697_4098812043002_480328736_n.jpg
my coding
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="c.css" rel="stylesheet" type="text/css" />

             Start to bump !
            <input name="login_btn" type="submit" class="login_button" id="login_btn" value="Log in" />
            <input name="register_btn" type="submit" class="register_button" id="register_btn" value="Register" />

      Beta Version

 ***********START**********************************lorem ipsum  lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
 lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
  lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
   lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
    lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsumrem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum

            Copyright © LCB 2013, All rights reserved.

Comment: There's no space for me: http://codepen.io/hmartiro/pen/ntGAB. However, you should right click on the space and choose "Inspect element" in Chrome, which should lead you to what the cause is in your case.
Comment: It gets only when browser window size make to small. Make the browser window size small and see.
Comment: I think the problem is with your background image try to remove that image and try once....
Comment: please refresh browser after re-sizing and check.
Comment: When I remove backgeound image its working well. What is the reason?
Answer: In your css file try:
* {
    margin:0;
    padding:0;
  }

Answer: remove your topmargin, bottommargin, left and right margins on your body tag and simply put margin:0; in where your body style is in the header.
Answer: Use reset.css in your page.it reset all the css property in all the browswer.
Comment: Visit this site http://meyerweb.com/eric/tools/css/reset/ and copy the css data in a seperat css file and add the file to your pages
Answer: Always use a css reset. There are many, but I like to use this created by Eric Meyer:
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-weight: inherit;
    font-style: inherit;
    font-size: 100%;
    font-family: inherit;
    vertical-align: baseline;
}
/* remember to define focus styles! */
:focus {
    outline: 0;
}
body {
    line-height: 1;
    color: black;
    background: white;
}
ol, ul {
    list-style: none;
}
/* tables still need 'cellspacing="0"' in the markup */
table {
    border-collapse: separate;
    border-spacing: 0;
}
caption, th, td {
    text-align: left;
    font-weight: normal;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: "";
}
blockquote, q {
    quotes: "" "";
}

Oh, please! Do not use inline style!