Want to run asp.net 4 website on Ubuntu. Is it this simple?

Question: Hello I'm a Windows guy and would like to build a small, portable, mini ITX web servers with free software.
My application is ready. It's ASP.NET 4.0 connected to MySql 5. It runs on my Windows machine no problem, now I want to put it in a Linux box. I have previously ran some php websites in Ubuntu since apt-get install is very nice and easy, but I must say I'm still a Linux n00b.
I have Ubuntu 10.10 64 server.
Would it be as simple as:

apt-get install apache2
apt-get install mysql5
apt-get mono something
copy-paste the website files, done.

Is it this simple? Or are there complexities involved in step 3?
Oh and I assume i'll have to replace my MySql Connector/NET (MySql.Data.dll inside the bin folder) with a Mono version, right?
Comment: Are these going to be something that you are distributing a lot of?  If so, you need to consider maintenance and software upgrades, which are more important then the initial install.
Comment: @devicenull: not really a lot, and upgrades won't be frequent (if any). And in case of an upgrade, most probably, it will be a "packaged" upgrade. It's not a huge application anyway.
Answer:
Is it this simple? Or are there
  complexities involved in step 3?

Ubuntu 10.10 contains mono 2.6.7 which doesn't support ASP.NET 4.0. You will have to build newer version of mono from source or tarballs using parallel mono environment. Try to look at these two articles which should guide you to set up newer version of mono on your Ubuntu.

Oh and I assume i'll have to replace
  my MySql Connector/NET (MySql.Data.dll
  inside the bin folder) with a Mono
  version, right?

Yes, I think there is a special version for mono.


lamp - is this virtual host creation process correct?

Question: I recently reinstalled ubuntu on my system and I cannot get virtual hosts to work properly.
What I'm trying to accomplish is typing 'test' on my browser and pointing to my local machine on the location /var/www/test
I have followed several tutorials on the subject, and after every one of them, w3m test outputs the contents of the index.html on the test directory as expected, but when I go to firefox/chrome and type 'test' in the address bar, the browser googles it and shows me results.
So I'm wondering if there is something wrong with my process.
What I've been doing so far is:

sudo apt-get install lamp-server^
sudo gedit /etc/hosts
append '127.0.0.1 test' to hosts file
save and exit
sudo gedit /etc/apache2/sites-available/test
add the following lines to test:
<VirtualHost *:80>
ServerName test
DocumentRoot /var/www/test
</virtualHost>

save and exit
sudo a2ensite test
restart apache (tried sudo service apache2 restart, reload, and also
rebooted the whole system)

Any clues as to why w3m shows me the correct file and browsers refuse to do so?
Please note that I have cleared Firefox and Chrome caches several times.
-------- Update -------
It looks like I had misconfigured my lamp server in the first place.
The above process worked just fine when I reinstalled lamp (sadly I had to reinstall OS too), with one slight modification.
In step 3, you do not have to append the name of the vhost to the hosts file, you have to append it to the line that starts with 127.0.0.1 localhost (usually the first line)
I cannot accept any of the answers, since if someone faces the same difficulty in the future, none of the answers will give the solution.
I will have to search the configuration files to see exactly what was wrong, and the. I will post it.
Answer: You might have a minor problem with your DNS resolution via /etc/hosts.
Since you appended 127.0.0.1   test to /etc/hosts, the DNS resolver has two entries which refer to the same DNS name, test and localhost.  Fixing this could go two directions.

If you want an alias, add test to the original localhost line, rather than adding a new line.  The result would be: 127.0.0.1    localhost    localhost.localdomain    test
If you want to have a dedicated IP address, use an alternate IP address on a new line.  Fortunately, all of 127.0.0.0/8 is loopback, so you can use any IP address in that range to create a secondary IP address for services: 127.0.0.2    test    test.localdomain
Comment: Yes, I already pointed it out to OP [via chat on AU](http://chat.stackexchange.com/transcript/201?m=7537346#7537346), but it appears he had done this correct and just [doesn't bother](http://chat.stackexchange.com/transcript/message/7545378#7545378) updating the Q here. +1 for very clear explanation for what I was trying to tell him.
Comment: @AnPel It's not about accepting answers and there **is** something relevant. You can't expect other people to work on your questions this way. If you don't fix mistakes *in the question* you know of (not the same as mistakes in the steps), this will result in answers you are not helped with. This answer is one of them and wouldn't have to be there if you would have updated your question with the exact line in the file. This discourages people like RobGibson, probably. Your Q is fine besides that, though.
Comment: @AnPel I agree on your last comment. But again: Copy/pasting/linking the whole `/etc/hosts` is reliable and relevant.
Answer: Your browser is too smart, and thinks that if you enter a single word into the address bar, that you meant to search, rather than browse to a site. Enter in a complete URL instead, for instance:
http://test/

or
http://test./

Comment: Sadly, this is not the case. 'test' googles for test, and http://test/ takes me to test.com
Comment: You need to fix your browser, then.
Comment: both chrome and firefox? I hope you are right since I've been breaking my head 2 days now but it feels like it's not a coincidence. Except each browser's preferences, where else should I start looking?
Comment: Browsers nowadays assume that anything you enter with an extension is meant as a search. You might also try `test.localdomain`, assuming `localdomain` is your local intranet domain name (default on most Linux distributions).
Comment: @Oldskool localhost.localdomain takes me to the localhost. test.localdomain does not work.


how to disconnect two notes

Question: I'm rewriting a sheet of music where the first two notes are separate, but lilypond connect them. How to disconnect those two 1/8 notes?
Thanks.
see exemple: http://lilybin.com/rexyws/1
Comment: One reason for disconnecting them is to set lyrics to each note, and use beaming as a slur joining notes together for a single syllable. This is common in setting plainchant on a five-line stave, for example.
Comment: Why do you want to remove the beam? That's going to make them harder to read (without affecting how they're played in most cases).
Comment: @AndrewLeach Hmmm — for irregular/unmetered music, I could probably sort of see the point. But it also gets used for standard metered vocal/choral music, too, and as a singer, I _hate_ it: it makes complex rapid rhythms impossible to follow, and IMHO adds nothing that's not better conveyed using standard notation with slurs for melisma. (And it doesn't seem to apply to the example in this question, which is in common time.)
Comment: @gidds as a singer, I hate vocal music that is beamed according to the meter instead of with the syllables.  It makes it much harder to know where each syllable begins and ends, and it leads people to introduce spurious phrasing as they try to treat the slurs as an instrumentalist would.  It's a matter of taste.  And it *was* the standard notation for several centuries.
Comment: @phoog Then I hope you never have to sing fast music filled with rhythms like this (genuine, AFAICR): | s s s q q q s q q q | q. q q q q q s q | q q s s s q q q s q | … (where q=quaver and s=semiquaver). As for phrasing, isn't that what phrasing slurs are for?
Comment: @gidds what's the text underlay there?  Anyway, after posting that comment, I realized that I should be more precise: my beaming preference actually depends on the style of the music.  I find metrically beamed 18th-century music vexing, and metrically beamed 20th-century music is perfectly normal.  It feels a bit like the same discomfort I have with reading British or Irish fiction in American spelling or vice versa.  As to slurs, I'm not talking about phrasing slurs but about articulation, for example "die mit Tränen säen" in the Brahms _Requiem._
Answer: There are lots of ways to customise the beaming in LilyPond:

if you just want a manual beam — try [ & ] or \noBeam
or maybe you want no beams at all — try \autoBeamOff
perhaps there is something else you want your beams to do programatically
— Have a look at the options on this page of the documentation:
https://lilypond.org/doc/v2.23/Documentation/notation/beams

how to disconnect two notes


Do I remove outliers to make the data normally distributed for ANOVA?

Question: I am trying to conduct a 3 x 2 mixed ANOVA (my measure is reaction time data) and my data is currently not normally distributed. I have read that ANOVA is robust to the violations of normality so I wouldn't need to transform my data.
However, wouldn't I need to identify outliers since it is reaction time data? How would I identify outliers if my data is not normally distributed?

How do I identify outliers for data that isn't normally distributed? Once identified, should I transform them or delete it? (since reaction time data is susceptible to outliers due to people falling asleep in experiments all the time)
Would I still need to transform my data for the ANOVA?
Comment: Normal distributions do have outliers. By removing them, you may make your data even less normal.
Comment: If you're willing to delete outliers--which can be a radical alteration of your dataset--then why not take the next logical step and just make your data into whatever values you like so that they look normal? ;-)
Comment: Do people not normally take logs of reaction times?
Comment: @justanotherlayman  I read  Bill Huber's comment as sarcasm.  I hope my answer has convinced you that deleting every apparent outlier only to make the data look like it is normally distributed is wrong.
Comment: @anony-mousse so why do people remove outliers if that's the case? i always thought that removing outliers improves normality and hence, improves the validity of the results? but i maybe wrong again...
Comment: @mdewey thank you for your straightforward answer. are there any references that show that it is acxeptable to do that? it would be greatly appreciated! :)
Comment: @whuber thank you for answering. i'm sure i can do that to make my data perfectly clean and squeaky so that i can publish in high ranking international journals. ;)
Comment: @justanotherlayman they do not "improve normality", but they can have an undesired high influence on any squared-error method, which is why robust statistics such as MAD and L-estimators exist.
Answer: Okay.  You are off to a good start.  Rupert Miller Jr. in his book Beyond ANOVA: Basics of Applied Statistics published by Chapman and Hall 1997 as a reprint of an earlier publication, points out in fairly simple language the assumptions in ANOVA and what happens when there are departures from the underlying assumption and this includes specific robustness properties. So it may be that if ANOVA is robust enough in your case, you don't need to worry about the non-normality and so do not need to transform the data.
But in your case, you know that people fall asleep sometimes while they should be reacting to a stimulus.  It would seem that the observer should be able to note when this happens and you can eliminate the cases when this occurs. If this is the case you do not need a statistical technique to identify and remove the outliers.  You then need to be satisfied that what remains provides an adequate sample size for the ANOVA. Also, you want to be convinced that the people you eliminate for sleeping would not have very different reaction times compared to the ones that remain.
Some comments about outliers: I have a lot of experience researching outliers and have also applied outlier detection methods when validating Department of Energy databases. I think it is important to distinguish between outlier detection and outlier rejection.  Outlier detection helps you identify when observations depart significantly from the bulk of the data.
I view outlier detection as being acceptable since it leads you to check for possible errors or other causes for the outlier. If the error can be confirmed, there is justification for removing it.
Detecting outliers in some situations can tell you a lot about possible problems with your experiment. But arbitrary removal is dangerous because you are forcing the data to conform to some preconceived notion as to what your data should look like.
As you become more comfortable with these subjects do look at some of the posts here about ANOVA, transformations and outliers.  Also Wikipedia can often be helpful as well as some elementary books and articles on outliers. Besides ANOVA, Miller's book deals with transformations, and one sample and two sample problems.
Comment: Thank you so much for your wonderful answer. I'm a little dubious about removing outliers as well, especially without a good justification. We can only assume that the participant is falling asleep but we can never really be sure as they are left alone in the room to do the experiment. Therefore, after reading your comment, is it acceptable if i do a transformation to my data to "correct" for outliers? Thanks again.
Comment: @justanotherlayman Is it fair to say that falling asleep doesn't negate the reaction time but just makes it unusually long?  In that case if you want to transform to normality a power transformation might be okay. You could try the Box-Cox approach.


Serving static files with express for non-root paths

Question: I'm using express to serve a react app (bootstrapped with Create React App). The project has the following file structure:
|--client/
|    |--build/
|    |    |--static/
|    |    |    |--main.css
|    |    |    |--main.js
|    |    |
|    |    |--index.html
|    |
|    |--public/
|    |--src/
|
|--server/
     |--index.js

The build folder contains all of the static files, and I want to serve them from a non-root url. The express server is configured:
app.use('/some_path', express.static(path.resolve(__dirname,'../client/build')));

In my index.html, I initially had .css and .js files with href='/static/main.css and src='/static/main.js which didn't work (404 errors) because I believe the server would try to look for the files under /some_path/static/ for the files and not find them.
When I tried changing the paths in index.html to href='/some_path/static/main.css, I get 200 statuses for all of the requested files (the files sent match the originals), but nothing is rendered on the page. Inspecting the html page shows this in the body:

Any idea what I'm doing wrong here? Is there something simple I'm missing or something that needs to be configured when using React Create App?
Comment: For `href='/static/main.css'` to work, you just want `app.use(express.static(path.resolve(__dirname,'../client/build')));`.  That would be the right way to serve your static files.
Comment: Yes, it works for `app.use(express.static(...))`, but I would like to configure it so that only requests going to specific routes are served the static files. It seems possible from what I've read but not working for me so far
Comment: Well, I've given you something that works and you've rejected it.  I do not understand your unstated requirements well enough to suggest something else.
Answer: https://expressjs.com/en/starter/static-files.html

the doc already said the following:

To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:

app.use('/static', express.static('public'))

for example:
have a path as following:
https://localhost:port/product/new
with all the .JS and .CSS file in folder "public", normally, Express will return an error that cannot load the .JS and .CSS file. We only need to include the following line:
app.use('/product', express.static('public'))


Consulta MYSQL muito lenta, mais de 25 segundos, como melhorar?

Question: Tenho uma consulta no mysql que está muito lenta , mais de 25 segundos.
A base é bem grande, algo em torno de 1.000 registros de imoveis (tabela osrs_properties) e 16.000 registros de fotos (tabela osrs_photos).
A consulta sem a foto leva meio segundo, mas com foto leva 25, como posso melhorar a consulta ou a tabela para aumentar a velocidade da consulta?
Abaixo a consulta:
SELECT p.ref,p.pro_name, p.id, p.pro_alias, p.precovenda, f.image as fotoimg, MIN(f.ordering) AS foto, p.city, p.bed_room, p.rooms, p.parking, p.square_feet
FROM osrs_properties p
LEFT JOIN (SELECT * FROM osrs_photos  ORDER BY ordering ASC) f ON f.pro_id = p.id
LEFT JOIN (SELECT category_id, pid FROM osrs_property_categories) c ON c.pid = p.id
WHERE p.id > 0
GROUP BY p.pro_name, p.id
ORDER BY p.pro_name, foto DESC
LIMIT 21

A consulta LEFT JOIN (SELECT * FROM osrs_photos  ORDER BY ordering ASC) f ON f.pro_id = p.id serve para eu pegar a foto com o menor ordering, ela que está deixando a consulta lenta
Comment: 1000 registros é praticamente insignificante. O seu problema com certeza são os arquivos de imagem no bd. O ideal é não tê-las no bd, e sim fora, controlando somente o endereço pelo banco. Provavelmente 1 imagem, vale os 1000 registros da sua outra tabelas! rs
Comment: Tenta trocar o `SELECT * FROM osrs_photos` por uma consulta que retorne apenas as colunas que você utilize na sua consulta. Veja se teve algum ganho.
Comment: @rbz Não seria uma opção ele guardar no banco porém de uma forma compactada?
Comment: @rbz é então, é que a consulta funciona assim, ele vai trazer todos os imoveis e 1 foto, e essa foto tem que ser a menor (a coluna ordering tem que ser a menor) fiz um teste usando ordering =1, daí vai super rapido, o problema é que tem registros que não tem ordering 1, o menor é 2 por exemplo
Comment: @VictorLaio O problema não é espaço, e sim processamento. Compactar provavelmente irá piorar, pois o banco terá mais 1 passo para abrir a imagem.
Comment: @VictorLaio como asism compactada? (trabalho ha pouco tmepo com mysql)
Comment: @rbz O processo de descompactação seria feito pelo backend pra nao sobrecarregar o banco, e já vi vários processos que podem diminuir o tamanho de uma imagem drásticamente. Nas empresas que eu trabalhei nunca recomendaram o armazenamento no servidor devido a segurança.
Comment: @Leandro, seria compactar a imagem ou usar processos no backend pra diminuir o tamanho da mesma antes de salvar no banco de dados pra não consumir tanto recurso. Tipo o que o WhatsApp faz com as imagens.
Comment: @VictorLaio pelo que entendi, vc disse para dimunuir o tamanho da imagem, isso? se for, não é necessario, o que esta demorando na verdade, é para encontrar a referencia dessa imagem no banco
Comment: @VictorLaio por que você não faz a pesquisa sem a imagem e depois que retornar vai no banco de novo (_async_) pra carregar a imagem? Acho que seja uma solução bem simples e deva resolver o problema
Comment: @LeandroMarzullo Você tentou usar um `SELECT TOP`, e buscar o primeiro registro encontrado, fazendo um `ORDER BY` por outro campo, já que você tem campo `NULL` em `ORDERING`? Esse join provavelmente está causando a lentidão `LEFT JOIN (SELECT * FROM osrs_photos  ORDER BY ordering ASC) f ON f.pro_id = p.id`
Comment: @rbz aproveitando seu comentário que 1000 registros é insignificante, a partir de qts registros mais ou menos, considero como grande (considerando uma tabela de 5 colunas)
Comment: @LeandroMarzullo Depende muito do seu servidor, do seu banco, dos índices, etc. Mas "basicamente" uma tabela somente com 5 colunas (varchar, int, pensando nos mais utilizados), pode colocar milhões e milhões! rs
Comment: show, obrigado pelos ensinamentos...rs
Answer: Ja tentou assim:
SELECT p.ref, p.pro_name, p.id, p.pro_alias, p.precovenda, f.image as fotoimg,
MIN(f.ordering) AS foto, p.city, p.bed_room, p.rooms, p.parking, p.square_feet
FROM osrs_properties p
LEFT JOIN osrs_photos f ON f.pro_id = p.id
LEFT JOIN osrs_property_categories c ON c.pid = p.id
WHERE p.id > 0
GROUP BY p.pro_name, p.id
ORDER BY p.pro_name, foto DESC
LIMIT 21

Todas as tuas subqueries fazem um tablescan nas tabelas osrs_photos e osrs_property_category.
Tenta da forma que mostrei pra ver se melhora seu desempenho.
Espero ter ajudado. :D
Comment: @LeandroMarzullo ele eliminou todas as subqueries que estava utilizando nos joins. Como a tabela já tem referência, você pode usar direto. Essas subqueries seriam necessárias ou por falta de referência, ou algo específico. Mas ainda o ideal não é armazenar imagens em bd. Procure aqui no SOpt que tem bastante sobre o assunto.
Comment: @rbz, valeu, é como se minha consulta fossem 3 consultas em uma e esse agora é uma só, isso? Valew!
Comment: Show, deu certo sim, levou menos de meio segundo, só pra eu entender, o que exatamente fez? Obrigado!
Comment: A diferença é que você colocando só a tabela, ele só irá fazer a referência direta. Se você coloca uma query, ele executa ela primeiro, depois faz a referência.


Dragging a widget down a long list so that scrolling is necessary with gwt-dnd

Question: I'm investigating using gwt-dnd to implement drag-and-drop reordering of a list of widgets.  The list might be longer than its visible area, and so I'd like the user to be able to do that drag-the-widget-near-the-bottom-and-the-list-auto-scrolls behavior that's pretty much standard.
Does gwt-dnd have this support built in anywhere?  If not, any ideas on implementing it?
Answer: You can try to do the following in case, you are using PickUpDragController and other classes which might have dragMove.
Subclass the PickUpDragController class and override dragMove as follows
<code>@Override
  public void dragMove()
  {
        DOM.scrollIntoView(context.selectedWidgets.get(0).getElement());
        super.dragMove();
  }
</code>
Answer: As it turns out, gwt-dnd has support for this automatically.  It requires that the dropArea be inside an AbsolutePanel dragBoundary, and that the dragBoundary is within a scroll panel.


Laravel validation required if field not equal to

Question: I have custom validation rule in my controller:
$this->validate($request, [
    'currency' => [
        'required',
        'numeric',
        'min:0',
        'max:7'
    ],
    'price' => [
        'nullable',
        "required_if:currency, !=, 0",
        'numeric',
        'min:1',
        'max:1000000'
    ],
], $messages);

Why work in required_if:currency, ==, 0 and not work in this required_if:currency, !=, 0 case?
In my case price field required only when currency field value not equal to 0
I tired also:
required_unless,currency,0
required_unless:currency,0

Comment: Why not try if currency greater than 0? i.e. `currency => gt:12`
Answer: I would suggest you use

required_unless:currency,0
  according to   [1]: https://laravel.com/docs/5.5/validation#rule-required-unless
Answer: required_if:currency, ==, 0 works because the currency value must be equal to any of the values that follow the value name (in this case currency). In other words, price is required in this case if currency is either == or 0.
So the == doesn't mean the usual "equals" in this case. It is just taken as a string value. That is also why required_if:currency, !=, 0 does not work as you expected it to.
To make the price field required only when the currency field value is not equal to 0, you could use required_unless:currency,0.
In other words, price is always required, unless currency is equal to 0.
Comment: Not working in my case too, but everyone suggests this format like you did.
Answer: You can use this:
Rule::requiredIf($request->get('currency') != 0)

Comment: That's right! using Model "Illuminate\Validation\Rule".


Manipulação de Excel em python

Question: Gostaria de atualizar o excel toda vez que o programa rodar, mas a biblioteca só deixa eu escrever um novo excel, e não atualizar ele.
Como posso fazer isso?
import requests
from bs4 import BeautifulSoup
import pandas as pd

page = requests.get("https://g1.globo.com/previsao-do-tempo/sp/sao-paulo.ghtml")

soup = BeautifulSoup(page.content, 'html.parser')

days = soup.find(class_ = "forecast-next-days__content")
today = soup.find(class_="columns medium-24 large-12 medium-centered")

nome = [today.find(class_="forecast-header__date").get_text()]

tempmax = today.find(class_="forecast-today__temperature forecast-
today__temperature--max").get_text()
tempmin = today.find(class_="forecast-today__temperature forecast-
today__temperature--min").get_text()

tempmax = tempmax[0:4]
tempmin = tempmin[0:4]
temp2 = [tempmax]
temp2 +=[tempmin]

nome += [n.get_text() for n in days.select(".forecast-table .forecast-next-
days__item-label")]
temp2 += [d.get_text() for d in days.select(".forecast-table .forecast-next-days__item-value")]

temmax = temp2[0:18:2]
temmin = temp2[1:18:2]

tempo = pd.DataFrame({
   "nome": nome,
   "temperatura_max":temmax,
    "temperatura_min":temmin
   })

tempo.to_excel(r"C:\Users\nlsouza\Desktop\python5.xlsx")

Comment: Sua pergunta é sobre Pandas, você poderia editar sua pergunta e adicionar a tag [tag:pandas]? (e talvez remover a tag `request`, sua pergunta não parece ser sobre isso).. Quanto à pergunta, você não consegue criar um dataframe a partir da planilha existente, editar o dataframe e salvá-lo novamente usando o mesmo método que você usou?
Comment: [Esta pergunta do SOEn](https://stackoverflow.com/a/47738103/1284458) talvez te ajude...
Answer: Não é possível "atualizar um arquivo Excel"
Na verdade, o próprio Excel só escreve um novo arquivo de Excel -
é impossível atualizar dados em um arquvo do tipo usado no Excel - xlsx - "in place".  Primeiro por que quando se fala em manipulação de arquivos, não é possível inserir dados "no meio" de um arquivo, como se inserem linhas em uma planilha. E segundo porque o xlsx é um arquivo binário, zipado - então mesmo que você faça um append no arquivo, até é possível gerar de volta o arquivo XML com o conteúdo de sua planilha ao final do arquivo ZIP - só que esse conteúdo vai ser duplicado: seu .xlsx teria o dobro do tamanho necessário.
O que o Excel e quase todos os outros programas fazem (a exceção podem ser alguns editores de PDF, e dependendo da edição feita), é gravar um arquivo novo, e, na sequência, excluir o arquivo antigo e renomear o arquivo novo para ter o mesmo nome do antigo.
Pode parecer um "desperdício" e que "fica lento", mas na ponta do lápis não é o que acontece - um computador moderno pode escrever dados em disco a taxa de (pelo menos) dezenas de megabytes por segundo - e um arquivo de 100MB já seria grande demais para ter qualquer uso prático pelo Excel diretamente, por exemplo.
Se você está falando de milhões de registros, que começariam a ficar pesados nesse modelo, também tem que partir para armazenamento de dados em um banco de dados, e não em um arquivo monolítico como um .xlsx - ou mesmo um CSV, que apesar de ser incremental e permitir o processamento "lazy" de linhas, não permite índices e queries.
O que é possível fazer nesse caso é:
ler os dados do arquivo Excel anterior, e concatenar os dados novos ao final, em seguida gravar o arquivo de novo.
Ao final do seu programa dá pra fazer isso:

from pathlib import Path  # a pathlib facilita trabalhar com arquivos

import csv

caminho = Path(r"C:\Users\nlsouza\Desktop\python5.xlsx")

if not caminho.exists():
   # primeira execução - gravar conteúdo todo:
   tempo.to_excel(caminho)
else:
   dados_anteriores = pd.read_excel(caminho)
   novos_dados = pd.concat((dados_anteriores, tempo), axis=0, ignore_index=True)
   novos_dados.to_excel(caminho)

Usando arquivos CSV
Se você deseja acrescentar dados ao final da sua tabela, a cada vez que o programa é executado, arquivos CSV podem ajudar. Mas não sentido pra isso nos dados que você extrai - pelo visto você quer só substituir os dados anteriores, não é? Daí é melhor a técnica de renomear o arquivo Excel mesmo.
Arquivos do tipo CSV podem sim ser aumentados ao final sem serem re-escritos: eles são arquivos textuais, e não tem "dados de fechamento": a sintaxe permite que sejam acrescentadas novas linhas ao final.
O problema é que o Pandas não tem suporte a isso - o método .to_csv do Pandas sempre vai escrever o arquivo inteiro de uma vez - e apagar o arquivo anterior.
Então é preciso ou usar o módulo "csv" do Python, ou, se fosse um dataframe mais complexo, usar o Pandas pra gerar um CSV temporário e copiar os dados para o CSV original usando alguma outra forma.
Dá pra fazer isso no final do seu programa então:

from pathlib import Path
import csv

caminho = Path(r"C:\Users\nlsouza\Desktop\python5.xlsx")

if not caminho.exists():
   # primeira execução - gravar cabeçalhos:
   with caminho.open("wt") as arq:
       writer = csv.writer(arq, delimiter=";")
       writer.writerow(tempo.columns)

with caminho.open("at") as arq:
   writer = csv.writer(arq, delimiter=";")
   for index, row in tempo.iterrows():
       writer.writerow(str(value) for value in row.values)

A grande desvantagem de usar um CSV em relação a um arquivo Excel, é que no CSV todos os dados são tratados como strings, e vai do programa que abrir o CSV (seja o Excel ou o próprio Pandas), "adivinhar" o tipo de dado de cada célula.
Se forem só números e strings de texto, em geral funciona direto - se você tiver dados como Datas, pode dar mais trabalho.  (E, se você quer realmente concatenar esses dados, incluir a data de quando o programa foi executado parece ser uma boa ideia).

Manipulação de Excel em python


Basic Question about Newton's Method for Optimization

Question: This is a very basic question about Newton's method for optimization, but I cannot seem to find the answer in any of my searches.  If we are using Newton's method (or gradient descent), how do we find a maximum instead of a minimum? Do we just change the sign of the step size to positive instead of negative?
Answer: Newton's method for unconstrained optimization finds local extrema. Given a function $f(\mathbf{x}):\mathbb{R}^n \to \mathbb{R}$ which we wish to minimize, Newton's method works by finding a root of $\nabla f(\mathbf{x})$. Absent any other information about the behavior of $f$, if Newton's method converges for some arbitrary initial guess, we don't know whether we've found a local minimum or maximum (or saddle point). Writing the iteration as $$\mathbf{x}_{n+1} = \mathbf{x}_n - H^{-1}\nabla f(\mathbf{x}_n)$$ where $H$ is the Hessian, it sounds to me like you're asking whether we might find a maximum by instead computing $\mathbf{x}_{n+1} = \mathbf{x}_n + H^{-1}\nabla f(\mathbf{x}_n)$, where we have changed the sign of the second term. This will not get us anywhere, as it moves away from a stationary point. To use Newton's method in an unconstrained optimization problem to find a maximum, you would use it exactly the same way as if you sought a minimum, probably with a different initial guess. Wikipedia's entry on Newton's method in unconstrained optimization offers a succinct discussion of the concept.
Descent methods, on the other hand, work differently; gradient descent, for example, updates each iteration by moving along a direction such that $f(\mathbf{x})$ decreases. In this case, it makes sense to change the sign of the update if you are seeking a maximum instead.
Answer: Yes, that's exactly what you do. You can think of this sign change as causing you to perform gradient ascent instead of gradient descent (in the case of using a gradient method). Alternatively, you can think of flipping the sign in a gradient method as performing gradient descent in $-f$. By finding a minimum of $-f$ you find a maximum of $f$.
Similar reasoning holds for Newton's method (and various other methods as well).


Why does a neural network have the same output for every item in a batch?

Question: I am trying to train a small MLP in Pytorch.
Here is the code for the net:
class Net(nn.Module):

    def __init__(self):
        super(Net,self).__init__()

        self.ln_1 =  nn.Linear(600, 512)
        self.ln_2 = nn.Linear(512, 256)
        self.ln_3 = nn.Linear(256, 128)
        self.ln_4 = nn.Linear(128, 64)
        self.ln_5 = nn.Linear(64, 64)
        self.ln_6 = nn.Linear(64, 32)
        self.ln_7 = nn.Linear(32, 16)
        self.ln_8 = nn.Linear(16, 1)
        self.sig = nn.Sigmoid()
        self.relu = nn.ReLU()

    def forward(self, x):
        print(f"Input: {x}")
        x =  F.sigmoid(self.ln_1(x))
        print(f"After Layer 1:{x}")
        x = F.sigmoid(self.ln_2(x))
        print(f"After Layer 2:{x}")
        x = F.sigmoid(self.ln_3(x))
        x = F.sigmoid(self.ln_4(x))
        x = F.sigmoid(self.ln_5(x))
        x = F.sigmoid(self.ln_6(x))
        x = F.sigmoid(self.ln_7(x))
        print(f"After Layer 7:{x}")
        x = F.sigmoid(self.ln_8(x))
        print(f"After Layer 8 (out):{x}")
        output = x

        return output

The input dim = 12x600 looks like this:
tensor([[[-0.0013, -0.0038, -0.0044,  ...,  0.0002,  0.0128,  0.0198],
     [-0.0043, -0.0026, -0.0003,  ..., -0.0002,  0.0038,  0.0057],
     [ 0.0364,  0.0272,  0.0145,  ...,  0.0054,  0.0084,  0.0114],
     ...,
     [-0.0179, -0.0068,  0.0040,  ..., -0.0222, -0.0262, -0.0192],
     [-0.0059, -0.0049, -0.0024,  ..., -0.0403, -0.0379, -0.0358],
     [ 0.0007,  0.0017,  0.0024,  ..., -0.0024, -0.0036, -0.0040]]])

And the output 1x12:
    tensor([[[0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051],
         [0.0051]]], grad_fn=<SigmoidBackward0>)

The optimizer looks like this and I tried different lr values to no avail.
criterion = nn.MSELoss()
optimizer = optim.Adam(Net.parameters(), lr=0.0001)

Training loop is designed the following way:
for epoch in range(epochs):
    running_loss = 0.0

    inp = inputs
    feat = features

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    outputs = fin(inp)
    loss = criterion(outputs.float(), feat)

    loss.backward()
    optimizer.step()

I want to know why despite different values being input to the model I end up with the same output for all 12 inputs? Thank you.
Below is the example of layer outputs at different points:
Input: tensor([[-0.0036, -0.0060, -0.0065,  ...,  0.0006, -0.0021, -0.0043],
        [ 0.0061,  0.0047,  0.0054,  ...,  0.0002, -0.0016, -0.0028],
        [ 0.0012,  0.0028,  0.0037,  ..., -0.0062, -0.0059, -0.0067],
        ...,
        [-0.0002,  0.0010,  0.0005,  ..., -0.0011, -0.0011,  0.0002],
        [-0.0003, -0.0012, -0.0010,  ..., -0.0022, -0.0002,  0.0020],
        [ 0.0005, -0.0013, -0.0027,  ...,  0.0037,  0.0047,  0.0045]])
After Layer 1:tensor([[0.4986, 0.4950, 0.5072,  ..., 0.4992, 0.5060, 0.4963],
        [0.4986, 0.4958, 0.5067,  ..., 0.4999, 0.5050, 0.4980],
        [0.4995, 0.4963, 0.5060,  ..., 0.4994, 0.5058, 0.4973],
        ...,
        [0.4983, 0.4964, 0.5053,  ..., 0.4992, 0.5055, 0.4976],
        [0.4982, 0.4944, 0.5061,  ..., 0.4989, 0.5048, 0.4972],
        [0.4983, 0.4949, 0.5056,  ..., 0.4997, 0.5058, 0.4970]],
       grad_fn=<SigmoidBackward0>)
After Layer 2:tensor([[0.4716, 0.3847, 0.3724,  ..., 0.5870, 0.5455, 0.4221],
        [0.4716, 0.3848, 0.3725,  ..., 0.5867, 0.5460, 0.4221],
        [0.4715, 0.3848, 0.3724,  ..., 0.5868, 0.5460, 0.4222],
        ...,
        [0.4716, 0.3849, 0.3724,  ..., 0.5871, 0.5461, 0.4220],
        [0.4716, 0.3848, 0.3724,  ..., 0.5869, 0.5461, 0.4221],
        [0.4717, 0.3848, 0.3725,  ..., 0.5869, 0.5462, 0.4222]],
       grad_fn=<SigmoidBackward0>)
After Layer 7:tensor([[0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706],
        [0.4533, 0.4607, 0.5720, 0.4933, 0.4806, 0.5116, 0.6091, 0.4371, 0.5633,
         0.5099, 0.6671, 0.6205, 0.4186, 0.4554, 0.6118, 0.4706]],
       grad_fn=<SigmoidBackward0>)
After Layer 8 (out):tensor([[0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051],
        [0.5051]], grad_fn=<SigmoidBackward0>)

```

Comment: I replaced all of sigmoids aside from the last one with leaky ReLUs and now I am getting different outputs.
Comment: You can check each layers' outputs.
Comment: So I also tried inputting without a batch and got the same result for all of the 12 inputs
Comment: I meant are the outputs same after the first layer, second layer etc.?
Comment: Can you also show how you are passing the 12x600 tensor to the network, and the complete training loop if possible?
Comment: I just updated the question and added what you requested. It seems that after a few layers the inputs in the batch become the same. @gunes
Answer: I met the same issue as you. I tried to fine-tune a large language model with more than millions of parameters but it outputs exactly the same for each batch. Finally, I figured out that I used a too-large learning rate with 0.0001 for the Adam optimizer.
Regularly, we leverage a tiny learning rate during the fine-tuning stage but I forgot to adjust it. In your case, though you are not using a pretrained model, you still set a too-large learning rate. As you stated, you change sigmoid to relu so that you get different outputs. I agree that Sigmoid is not the best choice for the activation function. However, if you take a look at how Sigmoid and Relu are different from each other. You will find that ReLu or LeakyRelu nearly lost their gradients if the input value is smaller than zero. That is why even if you use a big learning rate with a small batch size, the training process is still stable. In other words, the design of ReLu plays the role of preventing training vulnerability.
But there may be a case when you cannot alter and need to fix to a certain kind of deep learning model architecture, the optimal way is to turn down the learning rate.


Object cannot access its private data. error : private in this context

Question: I'm a c++ newbie. I have a simple class which stores a person's name and age.
for some reasons an object of the class cannot access the private data. When I run the code below, I get the error: Test::name is private within this context.
Header File
namespace Testing {

    class Test {
        public:
            Test(); // initializes to 0
            Test(int age_, std::string name_);
        private:
            int age;
            std::string name;
    };
}

Implementation File:
#include <string>
#include "Test.h"

using namespace std;
using namespace Testing;

Test::Test(){
    age = 26;
    name = "George";
}

Test::Test(int age_, string name_){
    age = age_;
    name = name_;
}

Main
#include <iostream>
#include "Test.h"

using namespace std;
using namespace Testing;

int main(){

    Test test;
    cout << test.name << endl;

    return 0;
}

Comment: Your implementation is not in the Testing namespace.
Comment: @drescherjm: There's no need to do so. `using namespace Testing;` is completely sufficient to define the members as they are defined in the OP's code. The class name in member definitions is looked up using regular name lookup. It does find class `Testing::Test` because of the previous `using namespace Testing;`. And it does properly associate definitions with the declarations.
Comment: @AnT • hmm, thank you... I did not know that!  I learned something new today.
Comment: private and protected members are not accessed from outside the class while public you can. So you either make name public or use a getter / setter. Remember a class by default its members are private while a structs's are public by default.
Comment: Please format the code
Comment: @Eljay: That's irrelevant. There's no need to explicitly put the definitions into `Testring` namespace. The definitions in the OP's code are made correctly. When you are defining something that has been declared previously, there's no need to explicitly "reopen" the namespace.
Comment: @Eljay, how should I use namespace in the implementation
Comment: @G king: Everything is perfectly fine with the namespace in your code. There's no real need to change anything with regard to namespace. You can rewrite it differently, but it is matter of personal taste. The error in your code has nothing to do with namespaces.
Answer: You are attempting to access a private member of your class from main function. It is not the "object" that accesses the private data (and it is not clear what you meant by that), it is function main that attempts to access that data. Function main does not have access to private members of your class. hence the error.
Answer: A object cant access the private data directly.
Only the member functions of class can access it.
So try to write a member function(function inside a class) or make variable (age,name) to public like

class Test{
public:
Test(); // initializes to 0
Test(int age_, std::string name_);
int age;
std::string name;
};

Refer this for using private data member
or else create friend functions
Refer this for using friend functions
Comment: "Only the member functions of class can access it." Pressumably you mean the class in which members are declared? Strictly speaking, that isn't true.
Answer: Your problem is you try to access private data (name) so you need to use a public getter or make the member data public:
std::string Test::getName()const{
    return name;
}

And in main:
//cout << test.name<< endl;
std::cout << test.getName() << endl;


Where to put Laravel Methods that help the controller and model get data and process it?

Question: I am building a PHP Laravel v5.3 app that will function as a Notebook with Notes and Tags as well as Note revisions/versions which can be used to restore a Note record to an older version of the Note.
It is basically an EverNote Clone in PHP similar to the open source Project Paperwork but will not be using AngularJS.
Based on that description above, my project has these Models:

Notebook
Note
NoteVersion
NoteTag

I have these Controller Classes to process the incoming HTTP Requests to the server Notebooks, Notes, and NoteVersions.
I need to have a function that will query the database for 2 NoteVersion records and return them as part of a JSON response which will be used as part of a view to show a comparison of 2 Note Versdions so a user can see the difference between the 2 records.
So assuming I add a new Controller Method to build this view, if I don't want the COntroller Method to handle the whole process of query the Database and doing all the work, what type of File would I put a function like that in  instead?
Assume the function I want to build is like this:
get2NoteVersionsForComparison($currentNoteId, $oldNoteId);

In my controller method I want to call this method get2NoteVersionsForComparison($currentNoteId, $oldNoteId) and have it return a JSON response.
So where best would my get2NoteVersionsForComparison($currentNoteId, $oldNoteId) method live at?
A Repository, Model, ServioceProvider?
Answer: I like to create a new project or company-specific folder and create a class in that folder. So, for example, App\Acme\MyClassFile.php would be the location. Create classes that do everything else. Sort of like a repository, if you will. But where doesn't matter as long as you use proper namespacing.


How to use Matrix4.setLookAt to move camera through scene

Question: Trying to make a first person camera controller in webGL.  I'm attempting to do this using the Matrix4.setLookAt() function, but am uncertain how to calculate (and frankly a bit uncertain which parameters I need to be modifying and when) how to move around.  How I currently have implemented to look left and right seems to work alright initially, but once the value nears 1.0 or -1.0 for the g_eyeX values, it begins moving the camera away (in the negative x direction away) from the cube in the scene.  I can't find much documentation on how to utilize this function to move the "camera" around the scene, as most of it references three.js (which I'm trying to learn how this works and don't want to use a library for it).  Could anyone give some help or point me in the right direction?  It would be greatly appreciated
My code is listed below, and here's what the setLookAt function takes as arguments
Matrix4.setLookAt(eyeX, eyeY, eyeZ, atX, atY, atZ, upX, upY, upZ)
'eyeX, Y, Z' - Specify the position of the eye point
'atX, atY, atZ' - Specify the position of the look-at point
'upX, upY, upZ' - Specify the up direction in the scene
JS:
<code> // sceneWalker.js
    // modified from RotatingTriangle.js (c) 2012 matsuda
    // uses a non-indexed cube - 2 triangles per side - 36 vertices
    // Vertex shader program
    var VSHADER_SOURCE =
      'attribute vec4 a_Position;\n' +
      'uniform mat4 u_ViewMatrix;\n' +
      'uniform mat4 u_ModelMatrix;\n' +
      'uniform mat4 u_ProjMatrix;\n' +
      'void main() {\n' +
      '  gl_Position = u_ProjMatrix * u_ViewMatrix * u_ModelMatrix * a_Position;\n' +
      '}\n';

    // Fragment shader program
    var FSHADER_SOURCE =
      'void main() {\n' +
      '  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
      '}\n';

    // Rotation angle (degrees/second)
    var ANGLE_STEP = 0.0;
    var MOVE_AMOUNT = 0.0;
    var g_eyeX = 0.0, g_eyeY = 0.0, g_eyeZ = 0.25; // Eye position
    var g_curX = 0.0, g_curZ = -3.0;

    function main() {
      // Retrieve <canvas> element
      var canvas = document.getElementById('webgl');

      // Get the rendering context for WebGL
      var gl = getWebGLContext(canvas);
      if (!gl) {
        console.log('Failed to get the rendering context for WebGL');
        return;
      }

      // Initialize shaders
      if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
        console.log('Failed to intialize shaders.');
        return;
      }

      // Write the positions of vertices to a vertex shader
      var n = initVertexBuffers(gl);
      if (n < 0) {
        console.log('Failed to set the positions of the vertices');
        return;
      }

      // Specify the color for clearing <canvas>
      gl.clearColor(0.0, 0.0, 0.0, 1.0);

      // Get storage location of u_ViewMatrix
      var u_ViewMatrix = gl.getUniformLocation(gl.program, 'u_ViewMatrix');
      if (!u_ViewMatrix) {
        console.log('Failed to get the storage location of u_ViewMatrix');
        return;
      }

      var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
      if (!u_ModelMatrix) {
        console.log('Failed to get the storage location of u_ModelMatrix');
        return;
      }

      var u_ProjMatrix = gl.getUniformLocation(gl.program, 'u_ProjMatrix');
        if (!u_ModelMatrix) {
            console.log('Failed to get the storage location of u_ProjMatrix');
            return;
        }
      // Current rotation angle
      var currentAngle = 0.0;
      // Model matrix
      var modelMatrix = new Matrix4();
      var viewMatrix = new Matrix4();
      var projMatrix = new Matrix4();
      modelMatrix.setTranslate(0, 0, 100);
      viewMatrix.setLookAt(g_eyeX, g_eyeY, g_eyeZ, 0, 0, 0, 0, 1, 0);
      projMatrix.setPerspective(45, (canvas.width)/(canvas.height), 0.1, 10000000);
      gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
      gl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements);
      gl.uniformMatrix4fv(u_ProjMatrix, false, projMatrix.elements);

      document.onkeydown = function(ev){ keydown(ev, gl, n, u_ViewMatrix, viewMatrix); };
      // Start drawing
      var tick = function() {
        //currentAngle = animate(currentAngle);  // Update the rotation angle
        draw(gl, n, currentAngle, modelMatrix, viewMatrix, u_ModelMatrix, u_ViewMatrix);   // Draw the triangle
        requestAnimationFrame(tick, canvas); // Request that the browser calls tick
      };
      tick();
    }

    function keydown(ev, gl, n, u_ViewMatrix, viewMatrix) {
    console.log(ev.keyCode);
        if(ev.keyCode == 39) { // The right arrow key was pressed
          g_eyeX -= 0.01;
          console.log(g_eyeX);
        } else
        if (ev.keyCode == 37) { // The left arrow key was pressed
          g_eyeX += 0.01;
          console.log(g_eyeX);
        }
        if(ev.keyCode == 38){
            g_eyeY += 0.01;
        }
        if(ev.keyCode == 40){
            g_eyeY -= 0.01;
        }
        if(ev.keyCode == 68){
            g_curX -= 0.01;
        }
        if(ev.keyCode == 65){
            g_curX += 0.01;
        }
        if(ev.keyCode == 87){
            g_curZ += 0.01;
        }
        if(ev.keyCode == 83){
            g_curZ -= 0.01;
        }
        else { return; }
    }

    function initVertexBuffers(gl) {
      var vertices = new Float32Array ([
        -0.5, 0.5, 0.5,
        -0.5, -0.5, 0.5,
        0.5, -0.5, 0.5,
        -0.5, 0.5, 0.5,
        0.5, -0.5, 0.5,
        0.5, 0.5, 0.5,
        0.5, 0.5, 0.5,
        0.5, -0.5, 0.5,
        0.5, -0.5, -0.5,
        0.5, 0.5, 0.5,
        0.5, -0.5, -0.5,
        0.5, 0.5, -0.5,
        0.5, -0.5, 0.5,
        -0.5, -0.5, 0.5,
        -0.5, -0.5, -0.5,
        0.5, -0.5, 0.5,
        -0.5, -0.5, -0.5,
        0.5, -0.5, -0.5,
        0.5, 0.5, -0.5,
        -0.5, 0.5, -0.5,
        -0.5, 0.5, 0.5,
        0.5, 0.5, -0.5,
        -0.5, 0.5, 0.5,
        0.5, 0.5, 0.5,
        -0.5, -0.5, -0.5,
        -0.5, 0.5, -0.5,
        0.5, 0.5, -0.5,
        -0.5, -0.5, -0.5,
        0.5, 0.5, -0.5,
        0.5, -0.5, -0.5,
        -0.5, 0.5, -0.5,
        -0.5, -0.5, -0.5,
        -0.5, -0.5, 0.5,
        -0.5, 0.5, -0.5,
        -0.5, -0.5, 0.5,
        -0.5, 0.5, 0.5
      ]);
      var n = 36;   // The number of vertices

      // Create a buffer object
      var vertexBuffer = gl.createBuffer();
      if (!vertexBuffer) {
        console.log('Failed to create the buffer object');
        return -1;
      }

      // Bind the buffer object to target
      gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
      // Write date into the buffer object
      gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

      // Assign the buffer object to a_Position variable
      var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
      if(a_Position < 0) {
        console.log('Failed to get the storage location of a_Position');
        return -1;
      }
      gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 0, 0);

      // Enable the assignment to a_Position variable
      gl.enableVertexAttribArray(a_Position);

      return n;
    }

    function draw(gl, n, currentAngle, modelMatrix, viewMatrix, u_ModelMatrix, u_ViewMatrix) {
      // Set the rotation matrix
      modelMatrix.setRotate(currentAngle, 1, 1, 1);
      modelMatrix.setTranslate(g_curX, 0, g_curZ);

      viewMatrix.setLookAt(g_eyeX, g_eyeY, g_eyeZ, 0, 0, 0, 0, 1, 0);
      // Pass the rotation matrix to the vertex shader
      gl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements);
      gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);

      // Clear <canvas>
      gl.clear(gl.COLOR_BUFFER_BIT);

      // Draw the rectangle
      gl.drawArrays(gl.TRIANGLES, 0, n);
    }

    // Last time that this function was called
    var g_last = Date.now();
    function animate(angle) {
      // Calculate the elapsed time
      var now = Date.now();
      var elapsed = now - g_last;
      g_last = now;
      // Update the current rotation angle (adjusted by the elapsed time)
      var newAngle = angle + (ANGLE_STEP * elapsed) / 1000.0;
      return newAngle %= 360;
    }
</code>
HMTL:
<code><!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Cube</title>
  </head>

  <body onload="main()">
    <canvas id="webgl" width="400" height="400">
    Please use a browser that supports "canvas"
    </canvas>

    <script src="../lib/webgl-utils.js"></script>
    <script src="../lib/webgl-debug.js"></script>
    <script src="../lib/cuon-utils.js"></script>
    <script src="../lib/cuon-matrix.js"></script>
    <script src="sceneWalker.js"></script>
  </body>
</html>
</code>
Comment: [this answer might be helpful](http://stackoverflow.com/a/29362951/128511). It uses lookAt
Comment: Just a detail (in addition to the ones mentioned in answers):
>      modelMatrix.setRotate(currentAngle, 1, 1, 1);
>      modelMatrix.setTranslate(g_curX, 0, g_curZ);
The second statement above overwrites the first one.
Answer: setLookAt is usually implemented to take 3 vectors (or 9 individual values) as its arguments. The first argument is where the position of eye/camera/you is. The second argument is position along the direction you want to be looking at. The third argument is up axis. This is used to set the orientation of camera. This is needed because there are infinitely many orientations that all shares the same direction. Think of a plane, the direction of plane tells you which way its going but the orientation of plane indicates whether its flying normally or upside down.
The setLookAt usually returns a view matrix (or its inverse) which you then pass to the GPU.
So rather than the three 0s you have as the look position, you probably want to use the position of your model instead? Furthermore, note that your movement implementation currently moves you in absolute coordinates, not based on the current orientation of the camera. +/- 1.0 to eye_x may not actually make you move left in camera space.
Here, for reference, a simple pseudo FPSCamera implementation that should get you started. The main thing to note is that the camera should track its orientation via a forward, up and side vectors. These are in world coordinates and they are normalized to a length of one.
<code>/** @constructor */
function FPSCamera(){
    this.pos = [0.0, 0.0, 0.0];

    this.dir = [0.0, 0.0, -1.0]; // or forward
    this.up = [0.0, 1.0, 0.0];
    this.side = [1.0, 0.0, 0.0]; // or right
}

FPSCamera.prototype.forward = function(dist){
    this.pos[0] += this.dir[0] * dist;
    this.pos[1] += this.dir[1] * dist;
    this.pos[2] += this.dir[2] * dist;
};
// do the same for other 2 directions, strife and fly

// looks to left/right
FPSCamera.prototype.yaw = function(radians){
    var orientationChange = ORIENTATION.fromAxisAngle(this.up, radians);
    this.dir = VEC3.rotatedByOrientation(this.dir, orientationChange);
    this.side = VEC3.cross(this.dir, this.up);
    this.side = VEC3.normalize(this.side);
};

// same for pitch... except the rotation axis is this.side and you need to ensure the pitch is within +/- 90 degrees

FPSCamera.prototype.getViewMatrix = function(){
    // matrix can be extracted from the 3 direction vectors, but lets use lookAt here;
    return MAT4.lookAt(this.pos, VEC3.plus(this.pos, this.dir), this.up);
};
</code>
Comment: I'm curious about the solution to this as well since I'm pretty new to vanilla WebGL. Could you provide a jsfiddle with a simple example?
Comment: Its kinda annoying to put 3D stuff to jsfiddle because there is a lot of dependencies as I rather work with my abstractions rather than webgl API directly. I think what I'll do is put up a simple FPS camera implementation and that should get you started.


Cache warming in drupal7

Question: I have a drupal 7 website that uses Varnish for caching (through drupal's Varnish module).
 I want a cache warming mechanism that would automatically cache the latest version of the pages after the cache expiration time has passed.
How can i do this?
Comment: I don't actually understand what your question is. Also the varnish module doesn't provide any form of caching.
Answer: You could try the cache warmer module, which apparently does just that.
There are also a bunch of blog posts on the topic around the web.

Cache warming in drupal7


Map 2D points inside a closed curve to unit disk

Question: Hy everyone,
I have a set of 2D cartesian points (x,y coordinates) lying inside an arbitrary closed contour , something like this: arbitrary_closed_contour
by 'arbitrary' I mean that the closed curve does not resemble any circle or ellipse.
What I want to do know is to map each pair of x,y coordinates to unit circle, thus obtaining something like this: points mapped to unit circle
How to accomplish this? I must confess up to this point I'm clueless!
Thanks for any suggestion.
Update: (based on @bubba comments)
Some clarifications:

I want to map each pair of (x,y) coordinates to the unit disk, not the unit circle.
The points I want to map emanate all from the same origin of coordinates. They're also distributed along a set of rays radially distributed
Each of these rays have two extrema points ((xA,yA) and (xB,yB)) each of them located at a different distance from the origin. Constrain: when mapped each of these points should be located on r=1
Comment: You have tagged your question (conformal-geometry), but the text of your question doesn't require the map to be conformal. Please clarify this discrepancy.
Comment: Given that a ray starts at the origin and extends to infinity in some definite direction $\phi\in{\mathbb R}/(2\pi)$, what do you mean by the two "extrema points" $(xA,yA)$, $(xB,yB)$ on such a ray?
Comment: I suppose he means that these two points are the ones (among those on this particular given ray) that have minimum and maximum distance from the origin, respectively.
Comment: @msotaquira: Maybe we don't share the same concept of "ray". To me (and most other people, I think), a "ray" is a semi-infinite line. In other words, it starts at some fixed point (the origin, typically) and it shoots off to infinity in some direction. So, a ray isn't just an infinite line; it's a "half-line". Are your points $(xA,yA)$ and $(xB,yB)$ on the same ray, or are they merely on the same line? Saying it another way -- is the origin allowed to be in between $(xA,yA)$ and $(xB,yB)$?
Answer: You're really mapping points to the unit disk, not the unit circle.
It looks like the points are all aligned on a finite number of "rays" that emanate radially from the origin. If so, this makes things much easier.
On a given ray, you map points as follows:
Let $d$ be the distance from the origin to the outermost point on the ray. The mapping for points on this ray is then just $(x,y) \mapsto (x/d, y/d)$.
If you want a single mapping/deformation that applies uniformly to all points, regardless of what ray they lie on, then some sort of interpolation between rays would be needed. But I won't go into that unless you confirm that you need it.
Comment: My mistake: I'm really mapping to the unit disk, NOT the unit circle! And yes, the points are all aligned on a finite number of rays emanating from the same origin. The mapping you suggested _(x,y)↦(x/d,y/d)_ would work. However, what I want is one of the extrema points of the ray (say _(xA,yA)_ located at a distance _dA_ from the origin) be mapped to the point _r=-1_ within the disk, whereas the other extremum (say _(xB,yB)_ located at a distance _dB$\neq$dA_) be mapped to the point _r=1_ within the disk.

How to accomplish this? Thanks again!
Comment: Sorry if I was not clear. What I want is that both extrema points be mapped to _r=1_! See the update I just posted on my original question.
Answer: Your question reads like you're looking for an implementation of the transformation whose existence is stipulated by the Riemann mapping theorem; particularly as you tagged it conformal-geometry.
Literature
Unfortunately, these beasts are rather hard to compute even for simple shapes, with huge numerical issues. The best continuous solutions seem to be based on Schwartz-Christoffel mappings. There is (at least) a whole book on the subject, which might be useful to you:
Driscoll and Trefethen Schwarz-Christoffel Mapping, Cambridge press, 2002
Recent developments in discrete differential geometry provide discretized concepts of conformal maps. One that I have intimate knowledge of is described in this article:
Springborn, Schröder and Pinkall, Conformal Equivalence of Triangle Meshes, ACM Transactions on Graphics 27:3
Algorithm
So what I'd do is the following:

Triangulate both your input shape and the circle in an arbitrary way
Choose an arbitrary triangle as intermediate step
Map both triangulations onto the triangle using my own implementation of the algorithm from the paper referenced above
Use the projective interpolation scheme described in that paper to map points first onto the triangle and from there onto the circle

There are a lot of arbitrary choices in the above approach. There is an arbitrary choice of a triangle, and for both partial mappings there is a choice which vertices of the triangulation should correspond to the corners of the triangle. In the continuous theory, the choice of the triangle is arbitrary, as is the choice of one set of corner vertices. The second choice of corner vertices fixes a Möbius transformation, as the conformal map is unique only up to Möbius transformations.
In practice, however, these choices will have significant impact on numeric stability. In a first attempt, I'd try the following heuristics. From the polygonal outline of the triangulated input shape, choose those vertices where the interior angle is minimal, and map those onto the corners of the triangle. Sum up the edge lengths along the rim of the polygon between these points, and use these three lengths as the edge lengths of your triangle. Also use these lengths as arc lengths along the rim of the circle to choose the corresponding vertices there as well. Make the triangulation of your shapes reasonably regular. With a bit of luck, this will work well enough. If not, new ideas might be required.


How to compare two inputs and show a div

Question: I need to make a simple js. Nothing fancy - just show or hide an element only if the inputs have the same value. All before send the form.
<form>
 <input type="number" name="some1" id="some1">
 <input type="number" name="some2" id="some2">

The inputs are the same

 <input type="submit">
</form>

The result can be something like this.
if(#some1(value)==#some2(value)) {
  #showhide.show()
} else {
  #showhide.hide()
}

Answer: Your jquery should be like this:
if($('#some1').val() == $('#some2').val()) {
  $('#showhide').show();
} else {
  $('#showhide').hide();
}

Answer: Something like this?

$("#some1, #some2").on("keyup change", function(){
  let firstEl = $("#some1"),
      secondEl = $("#some2"),
      conditionalEl = $("#showhide");

  if (firstEl.val() == secondEl.val() ) {
    conditionalEl.show();
  } else {
    conditionalEl.hide();
  }
});
#showhide {
 display: none;
}

<form>
 <input type="number" name="some1" id="some1">
 <input type="number" name="some2" id="some2">

The inputs are the same

 <input type="submit">
</form>
Comment: Glad it helped -- but make sure you mark your question as answered.


How do these logical contradictions differ in status?

Question: Firstly contradiction 1: $s_1$="There exist distinct integers $a,b,c$ such that $a^n+b^n=c^n$ for some $n>2$"
With this contradiction, we first suppose that it is true.  Then we find that doing so introduces a contradiction, so we reject our supposition.
Next, contradiction 2: $s_2=$"This statement is unprovable". First we suppose that it is true.  Then we use the fact that, working under the assumption that $s_2$ is true, we can prove $s_2$ by virtue of $s_2\implies s_2$.  This act demonstrates that, under the supposition, $s_2$ is provable, contradicting $s_2$.  Therefore, the supposition of $s_2$ introduces a contradiction so we reject the supposition.
How, if any, do these two circumstances differ in status?
This question refers to being, provable/true within any reasonable system, not that that is material to the question.
I hold that all statements are first supposed, pending acceptance and therefore the liar paradox and similar statements are simply equally rejectable as untrue statements.  The only difference in status that I can see, is that $s_2$ doesn't need to lean upon complex number theorems before it can be rejected.
Comment: The second statement is not a contradiction. Its analogue in arithmetics (i.e. the Godel statement) is, in fact, true in the standard natural numbers (assuming the consistency of arithmetics) and unprovable.
Comment: @Apostolos I think it is true in the standard numbers independently of the natural standard numbers, because it is an illogical statement. Can you find a reasonable theory in which it does not have the properties you describe? Any statement which is **contradictory under supposition** is illogical.
Answer: To expand on my comment.
$
\def\eq{\leftrightarrow}
\def\box{\square}
\def\nn{\mathbb{N}}
$
You have probably been misled by popular accounts of the incompleteness theorems. There is no way to understand them without having a grasp of the distinction between the meta-system MS and the formal system $S$ under study. In what follows I assume that $S$ extends PA$^-$, but in fact completely analogous results hold for any system that interprets PA$^-$ (as described here). A key point is that MS, being used for foundations of mathematics, always has a model of PA which we shall call $\nn$.
In particular, the Godel sentence $G_S$ is constructed in MS such that $S \vdash G_S \eq \neg \box_S G_S$, where "$\box_S P$" denotes a specific kind of arithmetical sentence such that we have (as a theorem proven in MS) that ( $S \vdash P$ ) iff ( $\nn \vDash \box_S P$ ). Observe crucially that $\box_S$ is peculiar to $S$, and hence $G_S$ is crucially dependent on $S$. Change $S$ and you will get a different $G_S$. In particular, you cannot construct an 'absolute' Godel sentence $G$ such that $S \vdash G \eq \neg \box G$, where $\box$ is an absolute version of provability in any sense, simply because such does not exist, unless you want a contradiction.
Put another way, your reasoning shows that you cannot have absolute Godel sentences. And no proof of the incompleteness theorems ever have such a thing either.
Comment: By the way I'm not the downvoter of your question, but I suggest you learn the technical details behind basic proof theory and the incompleteness theorems, as layman accounts just can't make the cut. Some things are inherently incompressible and this is one of them.
Answer: Your second example has multiple errors, which are generating this confusion.
First of all, let's specify a fixed theory we're discussing provability over. (You say this is immaterial, but it actually makes a huge difference; see below.) For any recursively enumerable theory $T$ extending $PA$ in the language of arithmetic, we may prove basic facts about provability in $T$ (e.g. for each $\varphi$ such that $T\vdash \varphi$, we have $T\vdash "T\vdash\varphi"$), and we may write a sentence $G_T$ saying "I am $T$-unprovable." Now we can in fact prove $G_T$, from axioms beyond those of $T$. Namely, if we add to $T$ the axiom asserting that $T$ is consistent, the resulting theory $T'$ can prove $G_T$.
But this is no contradiction at all. And this is the crucial mistake you make in deriving a contradiction from your $s_2$: you prove $s_2$ in the original system expanded by $s_2$ itself as a new axiom. In the language I've used above, you've proved $G_T$ in $T+G_T$. And obviously this isn't a problem, since $G_T$ doesn't say anything about its provability from systems other than $T$! In fact - assuming $T$ is consistent - the sentence $G_T$ is true, but unprovable in $T$. (What is provable in $T$ is the sentence "If $T$ is consistent, then $G_T$ is true", but that's not the same as $G_T$.)

Now you might try to escape this by replacing $G_T$ with some sentence $G_\infty$ which asserts its own "absolute unprovability," that it is unprovable in any "nice" system. Well, the first problem we hit is: what is a "nice" system? One natural guess is that a nice system is one whose axioms are true. This would certainly let your contradiction go through (if $G_\infty$ is true, then $\{G_\infty\}$ is a nice system which proves $G_\infty$, contradiction!).
But here you run afoul of a more fundamental problem: how do you plan to express your sentence in the language of arithmetic (or whatever language you're working in)? Remember, this was the key innovation that Goedel introduced: using Goedel numbering to show that certain statements about sentences could be expressed in the language of arithmetic.
And special emphasis should be placed on the word "certain" in the previous sentence. It is tempting to be vague when we talk about expressing metamathematical statements in arithmetic, on the grounds that "Goedel showed us we can do that." But that's a big mistake. And in fact Tarski's undefinability theorem shows that you won't be able to express the predicate "nice system" inside your language. Similarly, Lob's theorem shows that if a theory $T$ were to prove its own soundness, then $T$ would be complete - so, combined with Goedel's theorem, this shows that no (appropriate) theory $T$ proves its own soundness: there will be lots of sentences $\varphi$ such that $T\cup\{"T\vdash\varphi", "\neg\varphi"\}$ is consistent (although of course that expanded theory will prove its own inconsistency, but that's possible!).
Comment: Is my proposition not then, equivalent to this? That as part of our metatheory, we should have rules about suppositions, which state that we do not permit illogical suppositions, namely suppositions whose properties are self-contradictory irrespective of the system into which they are inserted. It seems to me that the modified liar is a statement which explicitly extends the axioms of any theory, and does so independently of the theory into which it is supposed. We might equivalently write the sentence 'the Godel number of this sentence exceeds itself in all theories.'
Comment: @RobertFrost: No that does not work. You can only form statements in the meta-system in the **language** of the meta-system, and there it is simply meaningless (syntactically invalid) to say "unprovable" without also specifying in what system. And also the meta-system does not permit self-reference, so you cannot use the English word "this".


sequence of continuous functions converges to bounded measurable function on $[0,1]$

Question: Problem: If $f$ is a bounded and measurable function on $[0,1]$, show that there exists a sequence of continuous functions $f_n(x)$ on $[0,1]$ s.t. $\int\left|f_n(x)-f(x)\right|dx\to 0$ as $n\to\infty$.
I asked a similar question to this before, but did not get a good answer. Here is my proof for this problem, which is equivalent to show that $C([0,1])$ is dense in $L^1([0,1])$.
First, denote $\mathcal{S}$ to be the space of simple functions $s(x)$ on $[0,1]$ with measure $\mu\left(\{x: s(x)\neq 0\}\right)<\infty$. Need to show that $\mathcal{S}$ is dense in $L^1([0,1])$, i.e. for every $\varepsilon>0$, as $n\to\infty$,
$$\int_{[0,1]}|f-s_n|\to 0$$
Obviously, $\mathcal{S}\subset L^1\left([0,1]\right)$, since
$$\int_{[0,1]}s(x)d\mu\leq\sup_{x}s(x)\mu\{x: f(x)\neq 0\}<\infty$$
By simple approximation theorem, there exists simple $\{s_n\}\uparrow f$ such that $\{s_n\}\to f$ for measurable $f$. By monotonicity,
$$\int_{[0,1]}s_n\leq\int_{[0,1]}f<\infty$$
Hence $s_n\in L^1$, which implies $s_n\in\mathcal{S}$.
Since $|f-s_n|\leq|f|\in L^1$, by dominated convergence theorem,
$$\lim_{n\to\infty}\int_{[0,1]}|f-s_n|=\int_{[0,1]}\lim_{n\to\infty}|f-s_n|\to 0$$
Second, need to show that $C([0,1])$ is dense in $\mathcal{S}$. Let $s\in\mathcal{S}$. By Lusin's theorem, there exists $\{f_n\}\in C([0,1])$ s.t. $\mu\left(\{x:f_n(x)\neq s(x)\}\right)<\varepsilon$, and $|f_n|\leq\|s\|_\infty$. Hence
$$\int_{[0,1]}|f_n(x)-s(x)|=\int_{\{f_n(x)\neq s(x), x\in[0,1]\}}|f_n(x)-s(x)|+\int_{\{f_n(x)=s(x), x\in[0,1]\}}|f_n(x)-s(x)|=\int_{\{f_n(x)\neq s(x), x\in[0,1]\}}|f_n(x)-s(x)|\leq\int_{\{f_n(x)\neq s(x), x\in[0,1]\}}2\|s\|_\infty\leq 2\|s\|_\infty\varepsilon$$
Hence
$$\int_{[0,1]}|f_n(x)-f(x)|\leq\int_{[0,1]}|f_n(x)-s(x)|+\int_{[0,1]}|s(x)-f(x)|\leq\left(2\|s\|_\infty+1\right)\varepsilon$$
Let $\varepsilon\to 0$, we get the desired result. Hence $C([0,1])$ is dense in $L^1([0,1])$.
My questions:

Can anyone help me check whether my proof is valid? If not, how to improve that?

I'm thinking of new ways to proof this, without using the density property listed in many real analysis texts. I'm thinking of using Littlewood's principle/Lusin's theorem (different from my current approach), but have not come up with a matured argument.

Here are some other posts related to this problem, but not exactly the same, since I'm dealing with sequence of continuous functions in my case.

Continuous functions on $[0,1]$ is dense in $L^p[0,1]$ for $1\leq p< \infty$
Questions about Proof of Lusin's Theorem
Is every Lebesgue measurable function on $\mathbb{R}$ the pointwise limit of continuous functions?
(The selected answer from this post inspires me. Some comments under the answer are some new approaches, but not written down in details. I'd be happy if someone can use these approaches to proof my case.)
I'll be grateful if anyone can help me with the questions I listed above. Thank you.
Answer: Here I provide two methods regarding this problem:
Method 1:
Use Lusin's theorem and Tietze's extension theorem: The former one guarantees a closed set of $F\subset[0,1]$ such that $f(x)$ is continuous on $F$ and $$\mu\left([0,1]\setminus F\right)<\varepsilon$$
The latter one guarantees existence of continuous sequence of functions $f_n(x)$ on $[0,1]$ s.t. $$\mu\left(\{x:f(x)\neq f_n(x)\}\right)\leq\mu\left([0,1]\setminus F\right)<\varepsilon$$
and $f_n(x)$ has the same upper bound as $f(x)$, say $C$.
Hence as $\varepsilon\to 0$,
$$\int_{[0,1]}|f_n(x)-f(x)|\mu dx=\int_{\{x\in[0,1]: f_n(x)\neq f(x)\}}|f_n(x)-f(x)|\mu dx\leq 2C\varepsilon\to 0$$
Method 2:
For bounded and measurable function $f(x)$ on $[0,1]$, we can define sequence of simple functions bounded as $$g_n(x)=\frac{1}{n}\left(nx\right)$$
$$\sup_{x}|g_n(x)-f(x)|\leq\frac{1}{n}\to 0$$ as $n\to\infty$.
By Lusin's theorem, for each simple function $g_n(x)$ and all $\varepsilon>0$, $\exists f_n(x)$ continuous on $[0,1]$ and a closed set $F_n\subset[0,1]$ s.t.
$$f_n(x)=g_n(x)\text{ for all $x\in E_n$ and }\mu\left([0,1]\setminus E_n\right)<\frac{1}{n}$$
On $E_n\cap E_m$ where $n\neq m$,
$$|f_n(x)-g_n(x)|=|f_m(x)-g_m(x)|$$
Hence,
$$\mu\left(\{x\in[0,1]: |f_n(x)-f_m(x)|>\varepsilon\}\right)\leq\mu\left([0,1]\setminus(E_n\cap E_m)\right)\leq\frac{1}{m}+\frac{1}{n}\to 0$$
as $m,n\to\infty$, where De Morgan's laws are used in the inequality.
Hence $f_n(x)$ is Cauchy sequence on $[0,1]$.
Since Cauchy sequence $f_n(x)$ must converge in measure to some function $[0,1]$, (proof regarding this is omitted, can be found in real analysis texts) there exists subsequence $f_{n_j}\to f$ a.e. as $j\to \infty$ (classical result; can also be found in other texts). Extract this subsequence and re-index it as $k=n_j$, we get the desired result.


Easy explanation on primary decomposition of ideals.

Question: The primary decomposition of an ideal $(x^2, xy)$ is
$$(x^2, xy) = (x) \cap (x, y)^2$$
which can be found on these notes.

Could someone explain to me how this can be done?

Edited: My question is not restricted to the example I gave  $(x^2, xy)$,  one can use any other example to show me the process.
Comment: Related: http://math.stackexchange.com/questions/990898/primary-decomposition-of-x2-xy-xz-in-kx-y-z-where-k-is-a-field, http://math.stackexchange.com/questions/1688151/how-do-you-prove-the-ideal-i-x2-xy-has-infinitely-many-distinct-irredunda
Answer: Answer 1.In general, to see an illustration of primary decomposition:
The book "Monomial Ideals" by "Herzog-Hibi", section 1.3 (Primary decomposition and associated prime ideals) illustrate this. The book has:
"Theorem 1.3.1 in combination with Corollary 1.3.2 now says that each monomial ideal has a unique presentation as an irredundant intersection of irreducible (primary) monomial ideals.  The proof of Theorem 1.3.1 shows us how we can find such a presentation." Example 1.3.3 illustrates the procedure.
I add an image of Example 1.3.3 and highlight the first step:

So we have  $(x^2, xy) = (x^2, x)\cap (x^2,y)    =  (x) \cap (x^2, y)$

Answer 2. For your special example:
Let K be a field and let R = K[X,Y]. Let M=(X,Y). The book "Steps in Commutative Algebra" by "Sharp", shows (Example 4.27, page 74) that $I=(x^2, xy) = (x) \cap (x, y)^2=(x) \cap (x^2, y)$. So the primary decomposition of "answer 1" and the primary decomposition you want are both primary decompositions for $I$.
Comment: In this case the answer would be $$(x^2, xy) = (x^2,x) \cap (x^2, y)=(x)\cap(x^2,y)$$ (It is fine but) why do we need to replace $(x^2,y)$ by $(x,y)^2=(x^2,xy,y^2)$
Comment: 1- primary decomposition is not unique. 2-    we dont replace $(x^2,y)$ by $(x^2,xy,y^2)$, but we replace $(x)\cap (x^2,y)$ by $ (x)\cap (x^2,xy,y^2)$. 3- we dont *need* to replace. +  give a link for "which can be found all over this site"
Comment: There are even [more decompositions](http://math.stackexchange.com/questions/1688151/how-do-you-prove-the-ideal-i-x2-xy-has-infinitely-many-distinct-irredunda?rq=1)


Repeated ANOVA unbalanced data

Question: I did experiment on one group of rats, the group was tested during 8 days, this is the first stage. The same group was also tested again but this time the test phase was 7 days. I tried to do my analysis using ANOVA repeated measures but I had a problem with the days.. I want to know the difference in the 2 tests but the days are not the same number. I don't know how to handle it. If there is any way to do the analysis using repeated measures ANOVA.
I tried to use repeated measure anova within subjects effect. My first factor will be test with 2 levels (test 1 & 2),second factor is day with the level of ?? Here is the problem! if I write 8 there will be extra day in test 2 and the analysis cant carry!!
Comment: If there is any way to do the analysis without dropping the day 8? I heard about the r software? what do you think? Do I need another software if I would like to carry the analysis without dropping the day 8?
Answer: I would recommend looking into mixed-effects models (also called random-effects models, multilevel models). They don't require balanced data--i.e., they handle missing data on the dependent variable--so you wouldn't have to drop your Day 8.
You can do them in R, e.g. with the package lme4 or nlme. SPSS, SAS, Stata, Mplus can all run those models too. (Note that you need one observation per line, so you'll end up with several lines for each rat.) For your particular situation, it looks like you would have observations nested under rats, with two level-1 predictors (test, which has 2 levels, and day, which has 8 levels). In R, using lme4 and assuming you have a data frame df with variables dv (your dependent variable at each observation), test (which is your grouping factor), day (which is the testing day), and rat (which is the rat identifier), it could go like this:
require(lme4)
lmer(dv ~ test + day + (1|rat), data=df)

If you treat day as a categorical predictor (as is done in repeated-measures ANOVA), then you cannot include the interaction between test and day, because day #8 occurs for only one level of test. However, if you treat day as a continuous variable (as is typically done in multilevel modeling), then including the interaction would be possible, and would be done like this:
lmer(dv ~ test + day + test:day + (1|rat), data=df)

Comment: Thanks - really useful and easy to understand explanation.
Answer: Drop Day 8. Then you have a standard $n \times 2 \times 7$ repeated measures design that lets you test both the Stage and Day main effects and the Stage $\times$ Day interaction.

Repeated ANOVA unbalanced data


Allow SVG in Views Rewrite Results

Question: I am rewriting the results of a field in a View and depending on the value of a field, I want to display an SVG icon. I can use TWIG to do the conditional logic and that works great, but it won't let me use the SVG tag. There is help text below that says:
You may include Twig or the following allowed HTML tags:   <acronym> <address>
   <bdi> <bdo> <big>

   <col> <colgroup> <command>
   <dfn>

  <hgroup>

  <menu> <meter>
 <output>

 <progress> <q> <rp> <rt> <ruby> <s> <samp>

 <time>
 <tt>
 <var> <wbr>

So as you can see, SVG is not there. Is there a way to make it allowable?
Answer: You can get around it by doing the overriding via template file rather than views config.
If you would like to allow it on views config and as long as you feel comfortable that non of your admins will do any evil XSS using svg to seek higher admin power and take over the world site, you could patch https://github.com/drupal/core/blob/9.3.x/lib/Drupal/Component/Utility/Xss.php#L21.
Comment: This did indeed work. THANKS

Allow SVG in Views Rewrite Results


My C#.net windows service is not executing its startup code

Question: I want to create windows service and for that i create the process and installer program also.
It installed well and I was ready to start my next move,but after starting the service it doesn't show anything.
I used this reference.
http://tech.pro/tutorial/895/creating-a-simple-windows-service-in-csharp
This is my program.cs file.
namespace WindowsService
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }
        public Program()
        {
            this.ServiceName = "Battery Service";
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            Console.WriteLine("Hello World");

            Console.ReadLine();
        }

        protected override void OnStop()
        {
            base.OnStop();

        }
    }
}

And next one is my installer file,WindowsServiceInstaller.cs
namespace WindowsService
{
    [RunInstaller(true)]
    public class WindowsServiceInstaller:Installer
    {
        public WindowsServiceInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            var serviceInstaller = new ServiceInstaller();

            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;

            serviceInstaller.DisplayName = "Battery Service";
            serviceInstaller.StartType = ServiceStartMode.Manual;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "Battery Service";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}

Service is installed correctly as per the above reference link.
Service appears in the Administrative Tools/Services and i started it.
It also appears on the Task Manager. Though,not executing the code.

Help me.
Comment: How do you know it is not executing? I doubt there is a visible stdin/stdout assigned to services. Have you tried something like writing to a file?
Comment: Google "session 0 isolation" to learn why services cannot display info on your desktop.  Use a log file.
Comment: possible duplicate of [Console.WriteLine() inside a Windows Service?](http://stackoverflow.com/questions/8792978/console-writeline-inside-a-windows-service)
Comment: See https://stackoverflow.com/questions/8792978/console-writeline-inside-a-windows-service/63685682#63685682 on how to open a console window. Note that this only makes sense when run manually (not as windows service)
Answer: There is no interactive console for a Windows service. Try using Debug.WriteLine instead and monitoring with an app like DebugView
Answer: Have a look at this article from MSDN How to: Debug Windows Service Applications.
You basically have to create the service in Debug configuration, start it as a service, then attach the VS debugger to the service process. You will then be able to debug it normally.
Because you need to debug the service when it starts, the article also give tips on how you can delay the execution of OnStart() to give you time to attach the debugger.
Comment: I did that. It doesn't go in OnStart() method


How do I get the google cloud shell to recognize the Esc key correctly in vim

Question: The Esc key is heavily used in vim to get back to normal mode from insert, command or visual mode. When you press the Esc key inside vim on the cloud shell the cursor changes shape and vim blocks. If you continually press on Esc key or if you click on the cursor itself then vim works again.
I am in the habit of using the Esc key for changing back to normal in vim and so this makes using vim on the cloud shell very painful.
I do not have to use the Esc key (I can use Ctrl+c or Ctrl+[ and that works except that the banner prompt still claims vim is in INSERT mode when it has switched to NORMAL mode) but it is hard to change my habits.
vim cursor in INSERT mode just before pressing Esc key
vim cursor becomes hollowed out just after Esc is pressed but still in INSERT mode
NORMAL mode can be obtained by pressing Esc two or three more times and waiting
I have investigated remapping the Esc key inside vim but this messes up vim and I get strange behaviour (see warning not to change Esc key mapping in How to disable Esc and cursor keys in vim).
The backspace key does not work in vim either but that can be fixed inside .vimrc with:
<code>inoremap <bs> <left><del>
</code>
If there is no fix for the Esc key mapping then I will have to force myself to learn not to use the Esc key.
The best alternative to the Esc key on the cloud shell seems to be Alt+Space - as that invokes a return to NORMAL mode and switches off the --INSERT mode message on the bottom too.
Comment: The hollowing out of cursor is caused when you leave the console, console can be focused again by clicking on the console using mouse. I was facing the same issue, a chrome plugin was capturing the `Esc` key. So start the browser without any plugin and see which plugin is causing the problem. But what I can't understand is how you are getting back to normal mode *by pressing Esc two or three more times and waiting*.
Comment: Try starting vim using `vim -u NONE` and check if the problem persists. Also make sure that no browser plugin is causing it, try disabling all plugins.
Answer: You'll have to hit escape twice (quickly) to enter ESC mode.


Prove that if $M$ is a maximal ideal and $x \notin M$, then $x+M$ is a unit in $R/M$

Question: I want to prove that if $M$ is a maximal ideal and $R$ is a commutative ring and $x \notin M$, then $x+M$ is a unit in $R/M$.
I am thinking of letting $(x,M) = \{rx+b \mid r \in R \, \text{and} \, b \in M\} \subset R$ and showing that $(x,M)$ is an ideal such that $M \subset (x,M) \subset R$, but I not sure how to proceed with this proof.
Comment: Yes, fixed. Thanks.
Answer: Just follow your nose from where you started.
$(M, x)$ cannot be equal to $M$ (since $x$ isn't in it) and therefore $(M,x)=R$. Then there has to be $m+xr=1$ for some $r\in R$.
Mod $M$, that reads $xr\equiv 1\pmod{M}$: in other words, $x$ is a unit.
Answer: If $R$ is not a commutative unital ring, then the statement in the question is incorrect.
If it's not commutative then we can see that $M_2 (\mathbb{R})$ - the ring of $2\times 2$ matrices with real entries - is a simple ring, and therefore ${0}$ is a maximal ideal, however there are non-zero $2\times 2$ matrices which are not units, for example $\pmatrix{1&0\\0&0}$.
If $R$ is not unital then we can take $R=2\mathbb{Z}$, then $M=4\mathbb{Z}$ is a maximal ideal, however $2+M$ is non-zero in $R/M$ but $(2+M)^2=0$.
Finally if $R$ is a commutative unital ring then let $x+M\in R/M$ such that $x+M\ne M$.
Now the set $I=(x+M)R/M = \{(x+M)(y+M)\mid y\in R\}$ is an ideal of $R/M$, and $x+M\in I$ so $I$ is a non-trivial ideal.
Since $M$ is maximal, $R/M$ has only two ideals: ${0}$ and $R/M$ (by the correspondence theorem for the natural homomorphism $\phi : R\to R/M$ defined by $x\mapsto x+M$).
We've shown that $I\ne 0$ and therefore $I=R/M$, so $1+M\in I$ and therefore $x+M$ is a unit.


Dividir array com virgula?

Question: Recebo assim os dados
Array
(
    [0] => php
    [1] => python
    [2] => javascript
)

como faço para receber assim com vírgula?
Exemplo:
php, python, javascript

Comment: Ok vou visualizar
Comment: OI anderson nao e isso gostaria de receber os dados com virgula
Comment: Sim, basta ler as respostas: https://pt.stackoverflow.com/a/88240/5878
Comment: Na verdade quero que ele de um loop
Comment: Por quê se essa função faz exatamente o que você precisa?
Comment: @HemersonPrestes você não quer com as funções já pronta você quer com uma estrutura de repetição?
Answer: No seu caso acredito que a função implode do PHP já resolve:
string implode ( string $glue , array $pieces )

Exemplo:
<?php

   $array = array('php', 'python', 'javascript');
   $separado_por_virgula = implode(", ", $array);

   print $separado_por_virgula; // php, python, javascript

Comment: como que ficari aqui    print_r ($_POST['programacao']);
Comment: Estou enviando os dados via post
Comment: Então você recebe com virgulas e quer converter para array? é isso?
Comment: Por favor favoreça as perguntas e ou respostas existentes clicando no Botão "Sinaliza" e indicando uma pergunta como duplicata.

Dividir array com virgula?


Claim $\lim_{\delta \to 0} E\int_{|y| \le 1}\int_{\mathbb{R}^d}| u(x)-u(x+\delta y)|^2\rho(y) \,dx\,dy = 0$

Question:
Let $(\Omega,\mathcal{F},P)$ be a probability space, with $u \in L^2( \Omega \times\mathbb{R}^d)$. Let $\rho$ be the standard mollifier approximation to Dirac's delta dunction defined on $\mathbb{R}^d$ supported in $\{ y \,|\, |y|\le 1\} \subset \mathbb{R}^d$.
   I would like to show $$\lim_{\delta \to 0} E\int_{|y| \le 1}\int_{\mathbb{R}^d}| u(x)-u(x+\delta y)|^2\rho(y) \,dx\,dy = 0$$

I know $\int_{\mathbb{R}^d}| u(x)-u(x+\delta y)|^2 \to 0$ as $\delta \to 0$ uniformly using transalation continuity in $L^p$ , and thus we have $\lim_{\delta \to 0} \int_{|y| \le 1}\int_{\mathbb{R}^d}| u(x)-u(x+\delta y)|^2\rho(y) \,dx\,dy = 0$. After this how can I interchange the limit with expection? Maybe somehow Dominated convergence theorem can be used.
Comment: So that means $y\in \mathbb R^d,$ so $\rho$ is not supported in $[-1,1],$ but maybe in $\{ y\in\mathbb R^d : \|y\|\le 1\}. \qquad$
Comment: I take it you mean $\rho$ is a smooth approximation to Dirac's delta function, but you said it's "the standard" one. I didn't know there was one that's singled out to be called that. Do you mean a Gaussian density function?
Comment: oh .... Maybe you mean $\rho$ is supported on $|y| \le 1.$ So it would _not_ be a Gaussian density. $\qquad$
Comment: Yes sir $\rho$ is supported in $[-1,1]$, approximation to Delta function.
Comment: You have $x+\delta y\in\mathbb R^d.$ So either $\delta \in \mathbb R^d$ and $y \in \mathbb R$ or $\delta \in \mathbb R$ and $y \in \mathbb R^d.$ I had assumed the latter until you said $\rho$ is supported in $[-1,1].$ Maybe it doesn't matter to the bottom line, but it seems hazardous not to be explicit about that. $\qquad$
Comment: Yes $\delta \in \mathbb{R}$. Let me be more explicit.
Comment: Yes, sorry about these errors.
Answer: Using
$$|u(x)-u(x+\delta y)|^2 \leq 2|u(x)|^2 + 2|u(x+\delta y)|^2$$
we find
$$\int_{\mathbb{R}^d} |u(x)-u(x+\delta y)| \, dx \leq 2 \int_{\mathbb{R}^d} |u(x)|^2 \, dx + 2 \int_{\mathbb{R}^d} |u(x+\delta y)|^2 \, dx = 4 \int_{\mathbb{R}^d} |u(x)|^2 \, dx.$$
Thus,
$$\begin{align*} \int_{|y| \leq 1} \int_{\mathbb{R}^d} |u(x)-u(x+\delta y)|^2 \varrho(y) \, dx \, dy \leq 4 \left( \int_{\mathbb{R}^d} |u(x)|^2 \, dx\right) \int_{|y| \leq 1} \varrho(y) \,dy. \end{align*}$$
As $u \in L^2(\Omega \times \mathbb{R}^d)$ the right-hand side is an integrable dominating function (which does not depend on $\delta$). Since you have already shown that the left-hand side converges to $0$ as $\delta \to 0$ we can apply the dominated convergence theorem to conclude that
$$\mathbb{E} \int_{|y| \leq 1} \int_{\mathbb{R}^d} |u(x)-u(x+\delta y)|^2 \varrho(y) \, dx \, dy \xrightarrow[]{\delta \to 0} 0.$$


unable to access external jar class and its methods

Question: i have simple  " welcome message " in my java class, Converted this class into jar,
jar file imported in the android application.
Problem:
unable to access the java class in android. please find the error log below.
Core JAVA Sample
package com.pat.welcome;

public class Welcome {

    public void sendSMS(){
        System.out.println("Welcome to pat, you can reach me any time");
    }
}

MainActivity
package com.pat.sendonesms;

import android.os.Bundle;
import com.pat.welcome.*;

import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    private static final String MyLog = null;
    Welcome newwelcome;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v(MyLog, "Activity loaded, call welcome class");
        newwelcome.sendSMS();
        Log.v(MyLog, "Welcome class called");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Error:
11-20 01:22:48.007: I/dalvikvm(1111): Could not find method com.pat.welcome.Welcome.sendSMS, referenced from method com.pat.sendonesms.MainActivity.onCreate
11-20 01:22:48.007: W/dalvikvm(1111): VFY: unable to resolve virtual method 4616: Lcom/pat/welcome/Welcome;.sendSMS ()V
11-20 01:22:48.018: D/dalvikvm(1111): VFY: replacing opcode 0x6e at 0x0011
11-20 01:22:48.167: V/(1111): Activity loaded, call welcome class
11-20 01:22:48.167: D/AndroidRuntime(1111): Shutting down VM
11-20 01:22:48.167: W/dalvikvm(1111): threadid=1: thread exiting with uncaught exception (group=0x414c4700)

if i add jar as per @Halim: instruction, then i getting jar like the below screen shot
i did " add to build path" getting below error
Installation error: INSTALL_PARSE_FAILED_NO_CERTIFICATES
Please check logcat output for more details.
Launch canceled!

Comment: Importing is not enough. You will have to add it to the classpath.
Comment: @MartijnCourteaux : In that case it would give a NoClassDefFoundError
Comment: you are not initializing the Welcome instance any where. may be thats the problem. You have declared it but not initialized it. bw how did you add the jar file into your project ?
Comment: @sayed.jalil: That would only cause a NPE.
Comment: @ Martin, yes it would. Thanks for pointing it out. But still its a problem :) He must be adding the jar file wrong way. it needs adding to the build path i guess.
Answer: Have you included this jar into your Android Project? You can do so by right click the Project->Properties->Java Build Path->Libraries
Then you can Add yourJAR from there
Comment: @Bharanikumar. Please check Order and Export tab. Right click the Project->Properties->Java build path->Order and export->Mark the welcome.jar->Move the jar to top of the list...You may want to try to mark the Android Private Libraries as well.
Comment: @Bharanikumar. If you have updated to ADT17. Please check this http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17 And yes don't forget to clean and rebuild the project.
Answer: Try this:
Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.Mark all checkboxes.Click on Apply and clean the project.
This worked for me.Hope this helps.
Answer: To add a jar file properly into your project copy it the libs folder of your android application project. You need to add it to the build path as well. You can do that by right clicking the jar inside the libs folder and selecting 'add to build path'.
Answer: You can add a jar in eclipse by right clicking on the Project --> Build Path --> Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar.
Answer: Do this way

Right Click on Project.
Go to "Properties"
Click "Java Build Path" on left side
Click "Order and export" tab
Check all dependencies.


how to query all where number from greatest then add limit after the query

Question: Hi Everyone Hope you can help me with this. this is the sample table
ID     name      num
1      A         34
2      B         54
3      C         24
4      D         3
5      E         100

First, I want to select all from the greatest num. Then after selecting all add another query to limit the first query to 3. So the ouput should be.
ID     name      num
5      E         100
2      B         54
1      A         24

The problem is I don't how do something like query all from greatest to lowest then after that query add a limit. Since the greatest is on row "5" if I say SELECT * FROM table ORDER BY num DESC LIMIT 0,3 the query would just stop to the 3rd row and won't get the largest because the largest num is on row 5. Help!
Comment: Did you actually try that sql command? I am pretty sure it will give you the three rows with the largest num value. (You have a typo in your output, Row 1 has a num value of 34 as per your sample)
Comment: plz share your table screen or format your question
Comment: [SQLFiddle](http://sqlfiddle.com/#!2/7e4278/1/0)
Answer: Hope this helps:
SELECT *
FROM `table`
ORDER BY `table`.`num` DESC
LIMIT 0 , 3

Answer: this should work
 select * from test order by num desc limit 3

fiddle demo
Answer: Try to make your num column data type to a number data type such as int, bigint, double etc. You definitely cannot sort it by num if you are using varchar/String data type for your num column.