align graphs in columwise by allowing the X-scale of each plot in same length using multiplot

Question: I am trying to plot five graphs (a,b,c,d,e) in a column order as shown below.
The x-scale for each plot is different and they are not in a vertical line  but graphs (c), (d) and (e) have almost similar x range values, that is 24.8.
When I plot these graphs in column order, I find graph labeled (a) and (b) are stretch and compressed respectively.
However, graphs labeled (c), (d) and (e) are having a same scale length.
I have drawn a red line vertically to show how the x-scale of each graph is aligned. (Please see the attached figure)

The gnuplot script that I used is given below.
#!/usr/bin/env gnuplot

#########################################################################################
#OUTPUT

#PNG
set terminal pngcairo size 600,550 enhanced dash #font "Arial-Bold,13" #fontscale 1.20
set output "zMulti-plot-LDP-lyoSystems.png"

#########################################################################################

set style line 1 lt 1 lw 1.5  lc rgb "red"
set style line 2 lt 3 lw 1.5  lc rgb "red"
set style line 3 lt 5 lw 1.5  lc rgb "red"

set style line 4 lt 1 lw 1.5  lc rgb "black"
set style line 5 lt 3 lw 1.5  lc rgb "black"
set style line 6 lt 5 lw 1.5  lc rgb "black"

set style line 7 lt 1 lw 1.5  lc rgb "green"
set style line 8 lt 3 lw 1.5  lc rgb "green"
set style line 9 lt 5 lw 1.5  lc rgb "green"

set style line 10 lt 1 lw 1.5  lc rgb "blue"
set style line 11 lt 3 lw 1.5  lc rgb "blue"
set style line 12 lt 5 lw 1.5  lc rgb "blue"

set style line 13 lt 1 lw 1.5  lc rgb "magenta"
set style line 14 lt 3 lw 1.5  lc rgb "magenta"
set style line 15 lt 5 lw 1.5  lc rgb "magenta"

set style line 16 lt 1 lw 1.5  lc rgb "#6495ED"
set style line 17 lt 3 lw 1.5  lc rgb "#6495ED"
set style line 18 lt 5 lw 1.5  lc rgb "#6495ED"

#########################################################################################
set macro
#ylabelFONT="font 'arial,16'"
labelFONT="font 'Arial,18'"
scaleFONT="font 'Arial-Bold,14'"
keyFONT="font 'Arial,10'"
#graph="using 1:2 with lines lw 1 "

#########################################################################################

xsize = 0.80
ysize = 0.22
xorigin = 0.002
yorigin = 0.02

#####################################################################
set xrange [-24.8:24.8]
set yrange [0:2.5]
set xtic auto       @scaleFONT    # set xtics automatically
#set ytic 0,0.2,0.4     @scaleFONT    # set ytics automatically
set ytic " " #0,0.2,0.4     @scaleFONT    # set ytics automatically
unset key

set size 1.0,1.0
set multiplot

# plot A
set ylabel ""
set label "(e)" at 0,0.60   @scaleFONT
set origin xorigin,yorigin
set size xsize,(ysize+0.015)

plot    "bcm25perRS-000-050ns_only_HEAD.dat"       using 1:2 title  '1-050ns'  w  l  ls  1  ,\
        "bcm25perRS-000-050ns_only_TAIL.dat"       using 1:2 title  '1-050ns'  w  l  ls  2  ,\
        "bcm25perRS-000-050ns_only_WATER.dat"      using 1:2 title  '1-050ns'  w  l  ls  3  ,\
        "bcm25perRS-051-100ns_only_HEAD.dat"       using 1:2 title  '51-100ns'  w  l  ls  4  ,\
        "bcm25perRS-051-100ns_only_TAIL.dat"       using 1:2 title  '51-100ns'  w  l  ls  5  ,\
        "bcm25perRS-051-100ns_only_WATER.dat"      using 1:2 title  '51-100ns'  w  l  ls  6  ,\
        "bcm25perRS-101-150ns_only_HEAD.dat"       using 1:2 title  '101-150ns'  w  l  ls  7  ,\
        "bcm25perRS-101-150ns_only_TAIL.dat"       using 1:2 title  '101-150ns'  w  l  ls  8  ,\
        "bcm25perRS-101-150ns_only_WATER.dat"      using 1:2 title  '101-150ns'  w  l  ls  9  ,\
        "bcm25perRS-151-200ns_only_HEAD.dat"       using 1:2 title  '151-200ns'  w  l  ls  10 ,\
        "bcm25perRS-151-200ns_only_TAIL.dat"       using 1:2 title  '151-200ns'  w  l  ls  11 ,\
        "bcm25perRS-151-200ns_only_WATER.dat"      using 1:2 title  '151-200ns'  w  l  ls  12 ,\
        "bcm25perRS-201-250ns_only_HEAD.dat"       using 1:2 title  '201-250ns'  w  l  ls  13 ,\
        "bcm25perRS-201-250ns_only_TAIL.dat"       using 1:2 title  '201-250ns'  w  l  ls  14 ,\
        "bcm25perRS-201-250ns_only_WATER.dat"      using 1:2 title  '201-250ns'  w  l  ls  15 ,\
        "bcm25perRS-251-300ns_only_HEAD.dat"       using 1:2 title  '251-300ns'  w  l  ls  16 ,\
        "bcm25perRS-251-300ns_only_TAIL.dat"       using 1:2 title  '251-300ns'  w  l  ls  17 ,\
        "bcm25perRS-251-300ns_only_WATER.dat"      using 1:2 title  '251-300ns'  w  l  ls  18

# plot B
set xrange [-24.8:24.8]
set ylabel ""
unset label
set label "(d)" at 0,0.60   @scaleFONT
#set format x ""
set origin xorigin,(yorigin+0.19)
set size xsize,ysize+0.02

plot    "bcm25perS-000-050ns_only_HEAD.dat"       using 1:2 title  '1-050ns'  w  l  ls  1  ,\
        "bcm25perS-000-050ns_only_TAIL.dat"       using 1:2 title  '1-050ns'  w  l  ls  2  ,\
        "bcm25perS-000-050ns_only_WATER.dat"      using 1:2 title  '1-050ns'  w  l  ls  3  ,\
        "bcm25perS-051-100ns_only_HEAD.dat"       using 1:2 title  '51-100ns'  w  l  ls  4  ,\
        "bcm25perS-051-100ns_only_TAIL.dat"       using 1:2 title  '51-100ns'  w  l  ls  5  ,\
        "bcm25perS-051-100ns_only_WATER.dat"      using 1:2 title  '51-100ns'  w  l  ls  6  ,\
        "bcm25perS-101-150ns_only_HEAD.dat"       using 1:2 title  '101-150ns'  w  l  ls  7  ,\
        "bcm25perS-101-150ns_only_TAIL.dat"       using 1:2 title  '101-150ns'  w  l  ls  8  ,\
        "bcm25perS-101-150ns_only_WATER.dat"      using 1:2 title  '101-150ns'  w  l  ls  9  ,\
        "bcm25perS-151-200ns_only_HEAD.dat"       using 1:2 title  '151-200ns'  w  l  ls  10 ,\
        "bcm25perS-151-200ns_only_TAIL.dat"       using 1:2 title  '151-200ns'  w  l  ls  11 ,\
        "bcm25perS-151-200ns_only_WATER.dat"      using 1:2 title  '151-200ns'  w  l  ls  12 ,\
        "bcm25perS-201-250ns_only_HEAD.dat"       using 1:2 title  '201-250ns'  w  l  ls  13 ,\
        "bcm25perS-201-250ns_only_TAIL.dat"       using 1:2 title  '201-250ns'  w  l  ls  14 ,\
        "bcm25perS-201-250ns_only_WATER.dat"      using 1:2 title  '201-250ns'  w  l  ls  15 ,\
        "bcm25perS-251-300ns_only_HEAD.dat"       using 1:2 title  '251-300ns'  w  l  ls  16 ,\
        "bcm25perS-251-300ns_only_TAIL.dat"       using 1:2 title  '251-300ns'  w  l  ls  17 ,\
        "bcm25perS-251-300ns_only_WATER.dat"      using 1:2 title  '251-300ns'  w  l  ls  18

# plot C
set xrange [-24.4:24.4]
set ylabel ""
unset label
set label "(c)" at 0,0.60   @scaleFONT
set origin xorigin,(yorigin+0.385)
set size xsize,ysize+0.02
plot    "bcm25perR-000-050ns_only_HEAD.dat"       using 1:2 title  '1-050ns'  w  l  ls  1  ,\
        "bcm25perR-000-050ns_only_TAIL.dat"       using 1:2 title  '1-050ns'  w  l  ls  2  ,\
        "bcm25perR-000-050ns_only_WATER.dat"      using 1:2 title  '1-050ns'  w  l  ls  3  ,\
        "bcm25perR-051-100ns_only_HEAD.dat"       using 1:2 title  '51-100ns'  w  l  ls  4  ,\
        "bcm25perR-051-100ns_only_TAIL.dat"       using 1:2 title  '51-100ns'  w  l  ls  5  ,\
        "bcm25perR-051-100ns_only_WATER.dat"      using 1:2 title  '51-100ns'  w  l  ls  6  ,\
        "bcm25perR-101-150ns_only_HEAD.dat"       using 1:2 title  '101-150ns'  w  l  ls  7  ,\
        "bcm25perR-101-150ns_only_TAIL.dat"       using 1:2 title  '101-150ns'  w  l  ls  8  ,\
        "bcm25perR-101-150ns_only_WATER.dat"      using 1:2 title  '101-150ns'  w  l  ls  9  ,\
        "bcm25perR-151-200ns_only_HEAD.dat"       using 1:2 title  '151-200ns'  w  l  ls  10 ,\
        "bcm25perR-151-200ns_only_TAIL.dat"       using 1:2 title  '151-200ns'  w  l  ls  11 ,\
        "bcm25perR-151-200ns_only_WATER.dat"      using 1:2 title  '151-200ns'  w  l  ls  12 ,\
        "bcm25perR-201-250ns_only_HEAD.dat"       using 1:2 title  '201-250ns'  w  l  ls  13 ,\
        "bcm25perR-201-250ns_only_TAIL.dat"       using 1:2 title  '201-250ns'  w  l  ls  14 ,\
        "bcm25perR-201-250ns_only_WATER.dat"      using 1:2 title  '201-250ns'  w  l  ls  15 ,\
        "bcm25perR-251-300ns_only_HEAD.dat"       using 1:2 title  '251-300ns'  w  l  ls  16 ,\
        "bcm25perR-251-300ns_only_TAIL.dat"       using 1:2 title  '251-300ns'  w  l  ls  17 ,\
        "bcm25perR-251-300ns_only_WATER.dat"      using 1:2 title  '251-300ns'  w  l  ls  18

# plot D
set xrange [-26.2:26.2]
set xtics auto
set ylabel ""
unset label
set label "(b)" at 0,0.60   @scaleFONT
set origin xorigin,(yorigin+0.58)
set size xsize,ysize+0.02

plot    "malto23per-000-050ns_only_HEAD.dat"       using 1:2 title  '1-050ns'  w  l  ls  1  ,\
        "malto23per-000-050ns_only_TAIL.dat"       using 1:2 title  '1-050ns'  w  l  ls  2  ,\
        "malto23per-000-050ns_only_WATER.dat"      using 1:2 title  '1-050ns'  w  l  ls  3  ,\
        "malto23per-051-100ns_only_HEAD.dat"       using 1:2 title  '51-100ns'  w  l  ls  4  ,\
        "malto23per-051-100ns_only_TAIL.dat"       using 1:2 title  '51-100ns'  w  l  ls  5  ,\
        "malto23per-051-100ns_only_WATER.dat"      using 1:2 title  '51-100ns'  w  l  ls  6  ,\
        "malto23per-101-150ns_only_HEAD.dat"       using 1:2 title  '101-150ns'  w  l  ls  7  ,\
        "malto23per-101-150ns_only_TAIL.dat"       using 1:2 title  '101-150ns'  w  l  ls  8  ,\
        "malto23per-101-150ns_only_WATER.dat"      using 1:2 title  '101-150ns'  w  l  ls  9  ,\
        "malto23per-151-200ns_only_HEAD.dat"       using 1:2 title  '151-200ns'  w  l  ls  10 ,\
        "malto23per-151-200ns_only_TAIL.dat"       using 1:2 title  '151-200ns'  w  l  ls  11 ,\
        "malto23per-151-200ns_only_WATER.dat"      using 1:2 title  '151-200ns'  w  l  ls  12 ,\
        "malto23per-201-250ns_only_HEAD.dat"       using 1:2 title  '201-250ns'  w  l  ls  13 ,\
        "malto23per-201-250ns_only_TAIL.dat"       using 1:2 title  '201-250ns'  w  l  ls  14 ,\
        "malto23per-201-250ns_only_WATER.dat"      using 1:2 title  '201-250ns'  w  l  ls  15 ,\
        "malto23per-251-300ns_only_HEAD.dat"       using 1:2 title  '251-300ns'  w  l  ls  16 ,\
        "malto23per-251-300ns_only_TAIL.dat"       using 1:2 title  '251-300ns'  w  l  ls  17 ,\
        "malto23per-251-300ns_only_WATER.dat"      using 1:2 title  '251-300ns'  w  l  ls  18

# plot E
set xrange [-20.0:20.0]
set xtics auto
set ylabel ""
unset label
set label "(a)" at 0,0.60   @scaleFONT
set origin xorigin,(yorigin+0.77)
set size xsize,ysize+0.02

plot    "malto12per-000-050ns_only_HEAD.dat"       using 1:2 title  '1-050ns'  w  l  ls  1  ,\
        "malto12per-000-050ns_only_TAIL.dat"       using 1:2 title  '1-050ns'  w  l  ls  2  ,\
    "malto12per-000-050ns_only_WATER.dat"      using 1:2 title  '1-050ns'  w  l  ls  3  ,\
    "malto12per-051-100ns_only_HEAD.dat"       using 1:2 title  '51-100ns'  w  l  ls  4  ,\
    "malto12per-051-100ns_only_TAIL.dat"       using 1:2 title  '51-100ns'  w  l  ls  5  ,\
    "malto12per-051-100ns_only_WATER.dat"      using 1:2 title  '51-100ns'  w  l  ls  6  ,\
    "malto12per-101-150ns_only_HEAD.dat"       using 1:2 title  '101-150ns'  w  l  ls  7  ,\
    "malto12per-101-150ns_only_TAIL.dat"       using 1:2 title  '101-150ns'  w  l  ls  8  ,\
    "malto12per-101-150ns_only_WATER.dat"      using 1:2 title  '101-150ns'  w  l  ls  9  ,\
    "malto12per-151-200ns_only_HEAD.dat"       using 1:2 title  '151-200ns'  w  l  ls  10 ,\
    "malto12per-151-200ns_only_TAIL.dat"       using 1:2 title  '151-200ns'  w  l  ls  11 ,\
    "malto12per-151-200ns_only_WATER.dat"      using 1:2 title  '151-200ns'  w  l  ls  12 ,\
    "malto12per-201-250ns_only_HEAD.dat"       using 1:2 title  '201-250ns'  w  l  ls  13 ,\
    "malto12per-201-250ns_only_TAIL.dat"       using 1:2 title  '201-250ns'  w  l  ls  14 ,\
    "malto12per-201-250ns_only_WATER.dat"      using 1:2 title  '201-250ns'  w  l  ls  15 ,\
    "malto12per-251-300ns_only_HEAD.dat"       using 1:2 title  '251-300ns'  w  l  ls  16 ,\
    "malto12per-251-300ns_only_TAIL.dat"       using 1:2 title  '251-300ns'  w  l  ls  17 ,\
    "malto12per-251-300ns_only_WATER.dat"      using 1:2 title  '251-300ns'  w  l  ls  18

# plot F
set size 0.3,0.5
set origin 0.76,0.55
set bmargin at screen 0
set key center center
set border 0
unset xlabel
unset ylabel
unset label
unset tics
set format x ""
set format y ""

set yrange [0:1]
plot 2 ls 1 t '1-50ns', \
     2 ls 4 t '51-100ns', \
     2 ls 7 t '101-150ns', \
     2 ls 10    t '151-200ns', \
     2 ls 13    t '201-250ns', \
     2 ls 16    t '251-300ns'

unset multiplot

Can someone have a look into this issue and help me to get a multiplot as I shown but let the scale of each plot is align in vertical order?
It is OK if the graph size is small or big.
But I want the scale to be in line vertically.
Appreciate all your help.
Thank you.
If I edit the set xrange [-26.2:26.2] to be same for all, than I get something below.. which I dont want. I only want as how I shown in the first picture.
Comment: Just glancing over it I see that you are setting the xranges differently for each subgraph. Just sticking to one (probably the largest, `[-26.2:26.2]` )should solve your problem, if I understand it correctly. Without data no way to check.
Comment: Dear #vaettchen, I have reedited my post with making the xrange to be same to all plots. I get a graph as shown. I don't want any "down shoulder" both the side of all the plots.
Answer: Basically you must adjust x-values of the plot size and origin such, that the widest plots keep their current size, and all others are shifted and squeezed according to their smaller ranges.
Since I don't have any data files and cannot test the whole script anyway, here is a sketch of what you need to do:
xsize(width) = 0.80 * width / width_max
ysize = 0.22
width_max = (2 * 26.2)
left_min = -26.2
xorigin(left) = 0.002 + (left - left_min) * xsize / width_max
yorigin = 0.02

set multiplot

# plot A
set xrange [-24.8:24.8]
set origin xorigin(-24.8),yorigin
set size xsize(2*24.8),(ysize+0.015)

plot ...

# plot B
set xrange [-24.8:24.8]
set origin xorigin(-24.8),(yorigin+0.19)
set size xsize(2*24.8),ysize+0.02

plot ...

# plot C
set xrange [-24.4:24.4]
set origin xorigin(-24.4),(yorigin+0.385)
set size xsize(2*24.4),ysize+0.02

plot ...

# plot D
set xrange [-26.2:26.2]
set origin xorigin(-26.2),(yorigin+0.58)
set size xsize(2*26.2),ysize+0.02

plot ...

# plot E
set xrange [-20.0:20.0]
set origin xorigin(-20),(yorigin+0.77)
set size xsize(2*20),ysize+0.02

plot ...

Comment: #Christoph thanks for your reply. It did not work. I get the same figure plots only. Here I give the link in dropbox where you can find file with name 'dataFile.tar'. This file contain all the data. The link is (https://www.dropbox.com/s/79w8pwbcspplc27/dataFile.tar?dl=0)
Comment: Dear #Christoph, I am able to get the graph as I wanted. There was a typo error in the script. Really thanks again.
Comment: You're welcome. I didn't have the time to dig deeper into your script with the data files. Is it a typo in my part, or in your adaption?


Inserir múltiplos registros php mysql

Question: Estou desenvolvendo um site de autos e quando o usuário  fazer o cadastro do veículo ele tem a opção de inserir várias imagens do veículo. Eu tenho o código de upload, mas queria saber como inserir mais de uma imagem no campo de imagens da tabala (Exemplo Banco Dados: imagem_veiculo : 01.jpg, 02.jpg,...) ou não sei se é melhor fazer a inserção de múltiplas linhas em uma tabela específica.
Se alguém puder dar exemplos.
if(isset($_POST['upload'])){

    //INFO IMAGEM
    $file       = $_FILES['img'];
    $numFile    = count(array_filter($file['name']));

    //PASTA
    $folder     = 'upload';

    //REQUISITOS
    $permite    = array('image/jpeg', 'image/png');
    $maxSize    = 1024 * 1024 * 10;

    //MENSAGENS
    $msg        = array();
    $errorMsg   = array(
        1 => 'O arquivo enviado excede o limite definido na diretiva upload_max_filesize do php.ini.',
        2 => 'O arquivo excede o limite definido em MAX_FILE_SIZE no formulário HTML.',
        3 => 'O upload do arquivo foi feito parcialmente.',
        4 => 'Nenhum arquivo foi enviado.',
        6 => 'Pasta temporária ausênte.',
        7 => 'Falha em escrever o arquivo em disco.',
        8 => 'Uma extensão do PHP interrompeu o upload do arquivo.'
    );

    if($numFile <= 0)
        echo 'Selecione uma imagem!';
    else{
        for($i = 0; $i < $numFile; $i++){
            $name   = $file['name'][$i];
            $type   = $file['type'][$i];
            $size   = $file['size'][$i];
            $error  = $file['error'][$i];
            $tmp    = $file['tmp_name'][$i];

            $extensao = @end(explode('.', $name));
            $novoNome = rand().".$extensao";

            if($error != 0)
                $msg[] = "$name : ".$errorMsg[$error];
            else if(!in_array($type, $permite))
                $msg[] = "$name :  Erro imagem não suportada!";
            else if($size > $maxSize)
                $msg[] = "$name :  Imagem ultrapassa limite de 10MB";
            else{

                if(move_uploaded_file($tmp, $folder."/".$novoNome))
                    $msg[] = "$name :  Upload realizado com sucesso!";
                else{
                    $msg[] = "$name :  Desculpe ocorreu um erro!";
                }

            }

        }

        $insert = "INSERT INTO upload
                    (imagens)
                    VALUES
                    ('{$novoNome}')";
        $exe_insert = mysql_query($insert, $con);

    }

}

Answer: O correto é você ter uma tabela de imagens, e colocar uma linha para cada imagem.
Nessa tabela você tem que ter o id que referencia o registro do veículo:
Exemplo:
Tabela de Veículos:
CREATE TABLE `test`.`veiculos`(
  `id` INT NOT NULL AUTO_INCREMENT,
  `marca` VARCHAR(50) DEFAULT '',
  `modelo` VARCHAR(100) DEFAULT '',
  `ano` YEAR,
  `datacadastro` DATETIME,
  `datamodificacao` DATETIME,
   PRIMARY KEY (`id`)
);

Tabela de Imagens
CREATE TABLE `test`.`imagens`(
  `id_veiculo` INT DEFAULT 0,
  `imagem` VARCHAR(255) DEFAULT ''
);

Já o INSERT você pode fazer em uma única query, por exemplo:
INSERT INTO imagens (id_veiculo, imagem) VALUES
(1, 'imagem1.jpg'),
(1, 'imagem2.jpg'),
(1, 'imagem3.jpg');

Comment: Isso acontece porque o seu `INSERT` esta fora do `FOR`, por esse motivo ele salva somente a ultima
Comment: Achei bem bacana sua ideia Roberto, mas o problema que tenho é que ele está salvando no banco apenas o nome da última imagem selecionada, mas na pasta de destino ele envia todas as imagens. Tem ideia do que pode ser?
Comment: Genial cara, funcionou direitinho muito obrigado
Answer: Ou também como amigo acima disse colocando:

create table images(
id int unsigned not null auto_increment,
id_veiculos int unsigned default null,
imagem blob not null,
primary key (id),
constraint fk_veiculos_id foreign key (id_veiculos) references veiculos(id)
);
Answer: Ola meu amigo veja estes link's:
http://blog.wieczorek.com.br/index.php/como-fazer-o-upload-de-arquivos-para-um-banco-de-dados-mysql-utilizando-o-php?blog=1
http://php.net/manual/pt_BR/features.file-upload.multiple.php
https://www.scriptbrasil.com.br/forum/topic/161933-mega-tutorial-upload-simples-e-multiplo/
Comment: Essa não é a dúvida dele. A dúvida dele é como ficaria armazenado no banco de dados o caminho da imagem.

Inserir múltiplos registros php mysql


Having difficulties converting SQL query to Laravel builder

Question: I am trying to convert the following query to laravels using query builder but having some issue with group by clause and sum aggregated functions
<code>SELECT code, SUM(quantity) as quantity, SUM(sale_price) as price
FROM `orders` o
GROUP By code
</code>
I have tried this, this is what I have after so many desperate tries.
<code>$args['orders'] = DB::table('orders')
->havingRaw('sum(sale_price)')
->havingRaw('code')
->where([ 'shop_id' => $shop_id ])
->groupBy('code')
->get();
</code>
Answer: Try with
<code>$args['orders'] = DB::table('orders')
->where('shop_id', '=', $shop_id)
->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price'))
->groupBy('code')
->get();
</code>
Order by
<code>$args['orders'] = DB::table('orders')
->where('shop_id', '=', $shop_id)
->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price'))
->groupBy('code')
->orderBy('quantity')
->get();
</code>
Or try
<code>->orderByRaw('sum(quantity)')
</code>
Comment: How would I add order_by column in this?
Comment: Nah, order_by is actually a column which will be joined to another table.
Comment: Then simply use `tablename.column` inside of `orderBy()`
Comment: SELECT code, order_by, SUM(quantity) as quantity, SUM(sale_price) as price FROM `orders` o GROUP By code
Comment: Then simply add `->orderBy('order_by')` to the query before `get()`
Comment: Nah, I want to select this column
Comment: Fixed it, I was not putting it in groupby, final result:

        $args['orders'] = DB::table('orders')
        ->where([ 'shop_id' => $shop_id ])
        ->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price, order_by'))
        ->groupBy('code', 'order_by')
        ->get();

Thanks
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136856/discussion-between-eddythedove-and-waleed).


When do we train a neural network using full batch training?

Question: It is a trend in deep learning to train models using multi-batches, i.e., to show the model a subset of the entire dataset for each weight update. In some cases, as in continual learning, we see that it is possible to train the network on one datapoint at the time. It is quite rare, on the other hand, to see research papers, or state of the art models, that are trained on a whole dataset simultaneously, i.e., in full-batch training.
For a research project, it would be useful for me to come up with a list of applications/models where training a neural network in full batch training is preferable with respect to using multi-batches. An example I have found is the COIN and COIN++ papers, that train on full batch training in order to memorise datapoints.
Question:
Do you know other applications where performing full-batch training is preferable to use mini-batches? Which ones?
Comment: One practical problem is that neural networks usually need a lot of data, so in many cases with full-batch training, you would run out of memory very fast.
Answer: To be honest the COIN paper is an exceptional case. The dataset consists of only 24 images, which is smaller than even the minibatch size used in more traditional experiments.
People like to use mini-batches because they introduce noise which helps with generalization, which is often what the researcher is interested in. However, even if the research does not care for that, most of the time the datasets are too big to even fit at once into the memory of the GPU.
I guess in all scenarios where overfitting is desired, like compression, it might make sense to use full batch training. However, as soon as you want to compress a large number of images the COIN approach will probably fail. As you cannot load them into the memory, and you would need to train multiple networks. IMO
So in general I could only think about cases where the implicit neural representation is needed, it might make sense to use full-batch training
Comment: Thank you for the answer! Yes, I'm aware that COIN is an exceptional case, I was simply wondering whether there are other specific and exceptional cases in the literature similar to this one.


How to prep vocals for a gig?

Question: Okay I finally have the opportunity I have been looking for for a while, I'm going to be doing my first big vocal performance at a big gig (big in my bands opinion not in expert opinion) with my band. I have been up to the task in practice but when I think about the live performance I choke up... I was wondering, I am used to being behind the drum kit so I have very little experience of having fans right in my face.
How can I prepare for this experience? I am confident in my vocals, I just don't know how I am going to cope being the centre of attention on stage.
Is there any pre gig "rituals" of a sort that you stalwart singers can advise me on?
Other points I would like covering if possible:

How to condition my vocal chords to not be too dry before the gig?
I will be playing drums beforehand, how can I ensure I am not too tired to sing?
When is it too far when trying to have a stage presence? (bare in mind its a heavy concert)
How much should I engage the crowd?
Comment: Karaoke helped me get comfortable singing without a guitar to protect me.
Comment: Nathan - quick query as there appear to be two good but separate questions here. Are you asking how to practice (ie warm up) or how to get over nervousness?
Comment: @DrMayhem well both actually How to practice in a nervous environment (Pre-gig) and how to get over nervousness...
Comment: @ToddWilcox funny you should say that I've been doing karaoke for a while. as crazy as it sounds it actually does help!
Answer: The answer to most of this is relax, keep your throat muscles loose (yawning actually helps with this). An audience wants you to be good, so they're typically behind you, but if you act nervous, that'll come across in the 'feel' of the gig more than anything.
There's various techniques, keep moving your feet beforehand, to keep blood flowing.
Breathe deep, there's lots of material on this online, but the basic most important thing is to get used to really filling your lungs, which gives you a boost of oxygen and helps keep the nerves away. It's also required to actually sing and produce a reasonable amount of sound. (You do need to project when you sing, the microphone doesn't do it all for you).
Know your material well, make sure you can sing 'in your sleep', without having to refer back to song sheets etc. this takes the pressure off a lot.
Rehearse chatting to an audience, even if it's just a couple of lines that they might respond to. Have a go at "how're y'all doing?", "Are you ready!?!" this kind of statement. or my personal favourite (when having to retune) "I swear it was in tune when I bought it"

How to condition my vocal chords to not be too dry before the gig?

Drink some water, alcohol dries you out, so does coffee. Have some water handy on stage in-case your throat feels dry.

I will be playing drums beforehand, how can I ensure I am not too tired to sing?

I'm not sure I understand this question.
Long term you can work on your physical fitness to improve stamina.
Are you usually out of breath after a couple of songs? If so this may effect your singing, take a break if possible.

When is it too far when trying to have a stage presence? (bare in mind its a heavy concert)

You'll find your feet over time, you need to find out what works for you, but here's some general guidelines.
Match the gig, if there's 4 people sitting at a table, it's not worth trying to play it like you would to a packed festival. If there's a bit of a crush standing at the front, I'd say go for it, you can't be too big if there's a crowd who'll go along with it. Guage the situation you're in.
Don't actually insult your audience, or the promoters, sound tech, bar staff etc. This will not end well.

How much should I engage the crowd?

Play well, talk a little. But most importantly try to enjoy it the way they're enjoying it. You're a part of the same scene they are, after all.
Comment: As I say, most of this is guidelines, and you will find your own way through it. To be honest, just go for it and have fun. That's the best advice I can give.

How to prep vocals for a gig?


Defining invariants of varieties over fields

Question: Let $f$ be a real-valued function on the set of curves over $\overline{\mathbf{Q}}$. Assume that isomorphism curves give rise to the same value for $f$.
Let $K$ be a number field and let $X$ be a curve over $K$.
Define $f_K(X)$ to be the value of $f$ after base change to $\overline{\mathbf{Q}}$.
Now, a priori, this function $f_K$ is not well-defined. In fact, one has to choose an embedding $K\subset \overline{\mathbf{Q}}$.
I do think that this is independent of the choice of embedding under some mild (or maybe none?) hypthesis on $f$. But why exactly?
Is $f_K$ well-defined? Do isomorphism $K$-curves take the same value?
Let me say that one can ask the same question for any function on some class of varieties over $\overline{k}$, where $k$ is a field.
Answer: In general $f_K$ will depend on the embedding $K \hookrightarrow \overline{\mathbb{Q}}$.  Take for instance the real part of the $j$-invariant of an elliptic curve, and consider elliptic curves with $j$-invariants $\pm \sqrt{2}$.


Active Drives and Spare Drives (RAID10)

Question: I'm setting up software RAID on Ubuntu Server 10.04; specifically RAID10. It's asking for the number of active devices for the array and the number of spare devices.
I know RAID10 is two mirrored pairs; RAID0 for two sets of RAID1 arrays (correct me if I'm wrong).
I would just like to know if all the devices (I'm using 4) are considered active, or if I should be setting it as 2 active and 2 spare.
Comment: Keep in mind that a Linux software RAID10, isn't exactly a RAID10, it can be setup really weird in various configurations. http://en.wikipedia.org/wiki/Non-standard_RAID_levels#Linux_MD_RAID_10
Comment: Didn't know that... it does say that the "4-drive example is identical to a standard RAID-1+0 array". So does that mean software RAID is going to be an okay choice in this case?
Comment: The software RAID runs just fine, though if you are worried about high-performance, you may need a hardware RAID controller.  Do some testing after you have the software RAID setup to see if it meets your requirements.  I use the software RAID on many servers, but they aren't extremely critical.
Answer: All 4 are active.  Spares are referring to Hotspares, which take over in the event of a disk failure.
Answer: In a RAID configuration, a spare or hot spare is an extra drive that at array construction stores nothing. If at some time in the future, an active drive fails, md (or whatever RAID controller you are using) will automatically start restoring data from the active drives to one spare drive in order to replace the spare drive.
If you have a RAID 10 of five 1TB disks (four active and one spare), you will only have 2TB total capacity. This is because you have a stripe of 2TB, which is mirrored so your 3rd and 4th disks are used up, and the fifth disk does nothing until one of the four fails.
Wikipedia RAID#Hot Spares
Answer: Spares are usually on-line drives not currently part of the RAID set.  If one of the drives fails, the RAID set can automatically rebuild using a spare.
In your example, all are active.

Active Drives and Spare Drives (RAID10)


Which formula is correct for the variance of discrete uniform distribution?

Question: This video offers this formula for the variance of discrete uniform distrubution:

However, this Wiki page uses a different formula despite everything else is the same:
$$\textrm{var}(X) = \frac{(b−a+1)^2−1}{12}  = \frac{(b−a)(b−a+1)}{12}.$$
Can someone explain to me why we have this discrepancy?  Thank you.
Answer: Your last step is wrong: $x^{2}-y^{2}=(x-y)(x+y)$ so you get the same answer in the two cases.
Comment: @J.Doe There must be  typo. It is not true that $\frac{(b−a+1)^2−1}{12}  = \frac{(b−a)(b−a+1)}{12}$. Instead it should be  $\frac{(b−a+1)^2−1}{12}  = \frac{(b−a)(b−a+2)}{12}$.
Comment: Those are the formulas from Wikipedia and an MIT video.  They agree on almost everything except that last part.  I do not think the variance is $x^{2}-y^{2}$.
Comment: Oh nice, thank you so much!


QSortFilterProxyModel hiding QWidget

Question: I have a QStandardItemModel that accomodates data. In one of the columns, I would like to add some QWidgets (clickable pictures). After adding QSortFilterProxyModel for soring/filtering purpose, however, the QSortFilterProxyModel hides my desired QWidgets.
I've searched the Internet but could not find how to keep the QWidget and QSortFilterProxyModel simultaneously. It would be much appreciated if someone may guide me on this issue. Thanks.
A minimal example, using QPushButton as my desired QWidget:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Buttons(QWidget):
    def __init__(self):
        super().__init__()
        layout = QHBoxLayout(self)
        layout.addWidget(QPushButton('btn1'))
        layout.addWidget(QPushButton('btn2'))
        self.setLayout(layout)

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        tab = QTableView()
        sti = QStandardItemModel()
        if True:    # This shows the buttons in a cell
            tab.setModel(sti)
        else:       # This does not show the buttons
            proxy = QSortFilterProxyModel()
            proxy.setSourceModel(sti)
            tab.setModel(proxy)
        sti.appendRow([QStandardItem(str(i)) for i in range(5)])
        tab.setIndexWidget(sti.index(0, 0), QPushButton("hi"))
        sti.appendRow([])
        tab.setIndexWidget(sti.index(1, 2), Buttons())
        self.setCentralWidget(tab)

app = QApplication([])
window = MainWindow()
window.resize(800, 600)
window.show()
app.exec_()

Answer: Widgets added using seyIndexWidget are added to the view, not to the model. The index passed to the function is only a reference that the view uses to know where those widget will be placed, and that reference must be that of the actual model used in the view.
If you're using a proxy model in the view, you must give the index of the proxy, not that of the source.
tab.setIndexWidget(proxy.index(0, 0), QPushButton("hi"))

Or, better:
tab.setIndexWidget(tab.model().index(0, 0), QPushButton("hi"))

Note that this obviously means that you might face some inconsistency whenever the model is altered because of filtering or sorting, and that's another reason for which index widgets should only be used for static and simple models, and delegates are the preferred solution.

QSortFilterProxyModel hiding QWidget


Questions about page 108-109 of Janusz's book Algebraic Number Fields

Question: I'm reading Janusz's book Algebraic Number Fields (second edition) and I'm confused by the proofs presented on page 108-109. My questions are:

In the proof of lemma 4.2 on page 108, why does $f(\alpha^n)=f(\alpha)^n$ imply $f(\alpha) \leq 1$?
Second line on page 109 : he proved that the minimum value $m$ of $|z-\alpha|$ occurs at some $\alpha_0 \in \mathbb{C}$ by showing that the function $g(\beta)=|z-\beta|$ is continuous on the closed disk $B_{\varepsilon}=\{\beta \in \mathbb{C} \mid |\beta| \leq m+\varepsilon+|z|\}$. I can see the function is continuous. If he is using the extreme value theorem, then the extreme value theorem says that $\max_{\alpha \in B_{\varepsilon}} |z-\alpha|=|z-\alpha_{\varepsilon}|$ for some $\alpha_{\varepsilon} \in B_{\varepsilon}$. Why does this imply $m=\min_{\alpha \in \mathbb{C}}=|z-\alpha|=|z-\alpha_0|$ for some $\alpha_0 \in \mathbb{C}$?
7th line on paeg 109: he used the following formula $$z^n-\alpha^n=(z-\alpha)(z-\omega \alpha)\cdots (z-\omega^{n-1}\alpha)$$ What is this formula? How is it derived? Can anyone point me to a reference?

Thank you!
Comment: regarding 3., note if $\alpha$ is a root of $z^n-\alpha^n$ then so is $\omega^k\alpha$ given $\omega^n=1$
Comment: @J.W.Tanner. Thank you for your answer. Yeah I can verify that $\omega^i\alpha$ are the roots, but is there any derivation of this formula?
Comment: If $\omega^i\alpha$ is a root, then $z-\omega^i\alpha$ divides the polynomial, and the product of all those terms has degree $n$ so there can't be other factors of $z^n-\alpha^n$
Answer: (1) Actually it is $f(\alpha^n)=f(\alpha)^n$ together with $f(a)$ being bounded by $\sqrt 2$ that implies $|f(\alpha)|\leq1$. Otherwise, suppose you have $|f(\alpha)|>1$ for some $\alpha$, then $|f(\alpha^n)|=|f(\alpha)|^n\to\infty$ as $n\to\infty$, contradicting the boundedness.
(2) For notational simplicity let
$$A=\{\alpha\in\mathbb{C}:|z-\alpha|\leq m+\epsilon\}\\
B_\epsilon=\{\beta\in\mathbb{C}:|\beta|\leq m+\epsilon+|z|\}$$
Then $A\subset B$. Now you are asking why
$$m=\inf_{\alpha\in\mathbb{C}}|z-\alpha|=\inf_{\alpha\in A}|z-\alpha|=\min_{\beta\in B_\epsilon}|z-\beta|$$
First by the definition of $A$ we have $\alpha\notin A\implies |z-\alpha|>m+\epsilon$. So we can remove the compliment of $A$ from consideration. Hence
$$\inf_{\alpha\in\mathbb{C}}|z-\alpha|=\inf_{\alpha\in A}|z-\alpha|$$
And from the set inclusions $A\subset B_\epsilon\subset\mathbb{C}$ we have
$$\inf_{\alpha\in A}|z-\alpha|\geq\inf_{\beta\in B_\epsilon}|z-\beta|\geq\inf_{\alpha\in\mathbb{C}}|z-\alpha|$$
thus finishing the equation.
(3) You can see this as a generalization of the equation $x^n-1=(x-1)(x-\omega)\cdots(x-\omega^{n-1})$.


How do I approximate a timeseries with a sum of sin and cos functions?

Question: I have a time series and I am trying to approximate it using an equation of the type:
y = A + B*sin(2*pi*x/n) + C*cos(2*pi*x/m) + D*sin(2*pi*x/q) + ...
The questions is how to find the parameters A, B, C, D, n, m, q, etc... Finding these values manually is obviously a very cumbersome process. I thought this would be a relatively simple problem for FFT, but I am getting nowhere (never done it before!).
My code (in R) is this:
value.ft <- fft(out.df$VALUE)
value.terms <- 4

fft.value <- rep(0, length(value.ft))
fft.value[1:value.terms] <- value.ft[1:value.terms]

value.approx <- Re(fft(fft.value, inverse=TRUE)) / length(out.df$VALUE)

with the following data.frame (out.df) as input:
structure(list(out.df.SEC = c(0, 300, 600, 900, 3000, 3300, 3600,
3900, 4200, 4500, 6600, 6900, 7200, 7500, 7800, 8100, 10200,
10500, 10800, 11100, 11400, 13500, 13800, 14100, 14400, 14700,
15000, 17100, 17400, 17700, 18000, 18300, 18600, 20700, 21000,
21300, 21600, 21900, 22200, 24000, 24300, 24600, 24900, 25200,
25500, 27600, 27900, 28200, 28500, 28800, 29100, 31200, 31500,
31800, 32100, 32400, 32700, 34500, 34800, 35100, 35400, 35700,
36000, 38100, 38400, 38700, 39000, 39300, 39600, 41700, 42000,
42300, 42600, 42900, 43200, 45300, 45600, 45900, 46200, 46500,
48600, 48900, 49200, 49500, 49800, 50100, 52200, 52500, 52800,
53100, 53400, 53700, 55800, 56100, 56400, 56700, 57000, 59100,
59400, 59700, 60000, 60300, 60600, 62700, 63000, 63300, 63600,
63900, 64200, 66300, 66600, 66900, 67200, 67500, 69600, 69900,
70200, 70500, 70800, 71100, 73200, 73500, 73800, 74100, 74400,
74700, 76800, 77100, 77400, 77700, 78000, 78300, 80100, 80400,
80700, 81000, 81300, 81600), out.df.VALUE = c(4.858, 4.938, 4.724,
4.45, 3.70666666666667, 3.9, 3.83, 3.784, 3.704, 3.6775, 3.4375,
3.368, 3.394, 3.552, 3.728, 3.65, 3.46, 3.444, 3.392, 3.378,
3.444, 3.34, 3.328, 3.246, 3.192, 3.212, 3.23, 3.6475, 3.484,
3.418, 3.556, 3.778, 3.80666666666667, 4.278, 4.428, 4.62, 4.886,
4.872, 4.99, 6.7, 6.132, 5.318, 4.722, 4.556, 4.32, 4.4125, 4.442,
4.376, 4.43, 4.55, 4.58, 4.114, 4.026, 4.11, 3.994, 3.97, 4.13,
3.98, 4.006, 3.918, 3.75, 3.734, 3.652, 3.81, 3.814, 3.816, 3.674,
3.592, 3.78, 3.608, 3.446, 3.474, 3.564, 3.252, 3.17, 3.1, 3.044,
2.818, 2.868, 2.564, 2.65, 2.732, 2.76, 2.646, 2.608, 2.6075,
3.0975, 2.794, 2.962, 2.848, 2.276, 2.15, 2.326, 2.19, 2.396,
2.072, 2.758, 2.73666666666667, 2.66, 2.726, 2.556, 2.61, 2.964,
3.125, 3.014, 2.966, 2.794, 2.95, 2.91666666666667, 3.054, 3.148,
3.068, 2.97, 3.08, 2.9, 2.88, 2.904, 2.976, 3.148, 3.078, 3,
2.888, 2.914, 2.774, 2.708, 2.59666666666667, 3.008, 2.976, 3.152,
3.102, 2.954, 2.96, 2.895, 2.854, 2.832, 2.908, 2.886, 3.026)), class = "data.frame", row.names = c(NA, -138L))

The result looks like this, and I think it is not nearly as good as it could be:

Changing the variable value.terms does not improve the outcome, and tend to result in overfitting. Is FFT the right approach here, or am I overcomplicating things?
EDIT: just to clarify, the objective is not an exact reproduction of the data but an approximation, whose equation can be used for other calculations. In the example, if the peak around 30k seconds and the two troughs around 15k and 50k seconds were just a bit more pronounced it'd be enough.
Comment: fft should be fine. linear regression should also work (like fft with a fixed periodicity).  I suspect the issue is that your data is not periodic, you have a jump between the value at 80000 and the value at 0.
Answer: I find it odd to use time (your x variable) as explanation of y. Specially if your time-series is not vanishing after a while...
What I see more often about time-series is using past to predict present. I tried using one lag of your y variable to explain itself, also know as AR(1). Results seem better and I am sharing it here. I am not sure if this answers your question but I had fun doing it.
#Make your code more generic
dt = as.data.table(a)
setnames(dt, "out.df.VALUE", "y")
setnames(dt, "out.df.SEC", "x")

dt[, lagy := shift(y, type = "lag", n = 1)]

model <- lm(y ~ lagy, dt)

int <- coef(model)[[1]]
slope <- coef(model)[[2]]

dt[, prediction := int + slope * lagy]
dt[, error := y - prediction]
score <- dt[2:.N, mean(error^2)]

plot_ly(dt, x = ~x, y = ~y, type = 'scatter', mode = "markers") %>%
    add_trace(x = ~x, y = ~prediction, type = 'scatter', mode = "lines")

You can play with logs (improves prediction slightly), you can include more lags as explanatory variables etc.
Comment: The sin/cos fitting does help when there are diurnal or other cyclic variations to account for. That said, your model does seem to fit this particular data set pretty well. It seems like you have used some packages beyond what's in base R, however, so to help others reproduce this please edit the answer to specify what packages you used.
Comment: "*I find it odd to use time (your x variable) as explanation of y. Specially if your time-series is not vanishing after a while...*" Makes sense if you expect the series to be a bounded monotonic function of time, but let me add that there are other cases.


Issue with itextsharp 5.4 while converting XML to PDF

Question: I am using following code to convert and XML into PDF by using itextsharp 5.4
protected void Page_Load(object sender, EventArgs e)
{
            Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream("ExampleDoc.pdf", FileMode.Create));
            ITextHandler xmlHandler = new ITextHandler(document);
            xmlHandler.Parse("ExampleDoc.xml");
}

I am getting error in line ITextHandler xmlHandler = new ITextHandler(document);
Errro is: missing directive or assembly reference
I have come to know that ITextHandler in no longer supported in itextsharp 5.4. So what should I do instead of that? I did not get any solid answer for this anywhere on net. Please suggest.
OR, Should I convert xml to html and then that html to pdf?
Comment: I still don't understand what "convert XML to PDF" actually means. What would your final PDF look like? Do you want your literal XML in the PDF as just a string? Do you want a table? Please post an example of what you actually want it to look like and we might be able to help you more.
Comment: Looks like ITextHandler was removed in version 5.4, use SimpleXmlParser instead. Look at [this answer](http://stackoverflow.com/questions/8874979/why-is-my-itexthandler-not-working-im-trying-to-parse-xml-into-itextsharp-doc).
Answer: I think it's going to work with iTextSharp but I invite you to use DoddleReport generator
See it live!
DoddleReport generates tabular reports from any IEnumerable datasource and you can render reports to Excel, PDF, HTML, and CSV – fully pluggable of course


Define signal for "Simulate arbitrary signal" from database

Question: I managed to make some arbitrary signals manually, but I want to define them in a database and at the define signal part from the Arbitrary signal properties I would like to take the values from a DB.
I have never worked with DB in Labview, and I'm a labview noob. Do you know some tutorials or schemas for what I'm trying to do? Thanks.
Comment: The LabView database kit is >$1000... do you already have a DB set up to use?  Why a database and not something else?  http://sine.ni.com/nips/cds/view/p/lang/en/nid/209060
Answer: http://performancemicrowave.com/sql_LV.html
http://www.jeffreytravis.com/lost/labsql.html
Free alternatives to NI's DB kit.  Without knowing more about your DB, whether it is already set up or not, what type of DB it is, etc, it's hard to provide more help than this.


How to prove the properties of penalized likelihood estimator in Fan and Li (2001) paper

Question: I'm reading through Fan and Li (2001) Variable Selection via Nonconcave Penalized Likelihood and its Oracle Properties. On p. 1349 (near the bottom-right corner) they proposed three properties that a good penalized estimator should have:

Unbiasedness: The resulting estimator is nearly unbiased when the true unknown parameter is large to avoid unnecessary modeling bias.
Sparsity: The resulting estimator is a thresholding rule, which automatically sets small estimated coefficient to zero to reduce model complexity.
Continuity:The resulting estimator is continuous in data z to avoid instability in model prediction.

They then showed that the hard thresholding penalty function can result in estimators with those good properties, given some conditions satisfied. Specifically,

(1) A sufficient condition for unbiasedness is $p_\lambda'(\theta)=0$ for large $|z|$;
(2) A sufficient condition for sparsity is $\text{min}_{\theta \neq 0} \{|\theta| +  p_\lambda'(\theta)\} \gt 0$;
(3) A sufficient and necessary condition for continuity is that the minimum of the function $|\theta| +  p_\lambda'(\theta)$ is attained at 0.

My questions are:

For proving condition (2), they depicted a figure below. I am confused why $\theta + p_\lambda'(\theta)$ is a Parabola since it is equal to $\theta + 2(\lambda - \theta)I(\theta < \lambda)$. And moreover, how does the proof to condition (2) come from this figure?

Update: Figure 3 should be a general illustration about $\theta + p_\lambda'(\theta)$ against $\theta (>0)$, as pointed out by @Glen_b.

The last sentence of first paragraph in Page 3 concludes that "a penalty function satisfying the conditions of sparsity and continuity must be singular at the origin". Why is that?

Obviously I totally missed the points here. Could any of you walk me through the arguments made for proving conditions (2) and (3)? Thank you very much for the help.
Comment: It's possible I missed something, but I don't see anything in the paper that suggests the depicted curve is actually parabolic, nor that it specifically represents exactly the function you claim it does. You make a number of other assertions that are also not clear to me from the paper. In any case, you should justify each of your assertions of fact. How, for example do you know that the depicted figure specifically shows $\theta + 2(\lambda - \theta)I(\theta < \lambda)$, rather than it being intended more generally to represent any of a variety of possible $\theta + p_\lambda'(\theta)$?
Comment: @Glen_b, thanks for pointing out this. I think you're right. Figure 3 should be general case. But given this, I still have trouble to understand the conclusion "the minimum of the function $|\theta| + p_{\lambda}'(\theta)$ is positive". Could you shed light on this?
Answer:
A sufficient condition for sparsity is that $\min_{\theta \neq 0} [ |\theta| + p'_\lambda (|\theta|) ]$ is positive.

The reason for this condition is explained on p. 1350 of the paper, but it is only a brief outline, as the authors have chosen to omit most of the demonstration of the asserted results.  I will try to fill in the blanks of what they have omitted.

The mathematical problem being solved here is to find a value $\hat{\theta} > 0$ that minimises a particular objective function for a fixed value of $z > 0$.  The objective function specified in equation $(2.3)$ is:
$$\begin{equation} \begin{aligned}
F(z,\theta)
&\equiv \tfrac{1}{2} (z-\theta)^2 + p_\lambda(|\theta|) \\[6pt]
&= \tfrac{1}{2} z^2 - z \theta + \tfrac{1}{2} \theta^2 + p_\lambda(|\theta|) \\[6pt]
&= \tfrac{1}{2} \theta^2 - z \theta + p_\lambda(|\theta|) + \text{const.} \\[6pt]
\end{aligned} \end{equation}$$
Finding the minimising argument is done by ordinary calculus techniques.  To facilitate this analysis, we define the function $H_\lambda(\theta) \equiv |\theta| + p'_\lambda (|\theta|)$.  Using the chain rule, for all $\theta \neq 0$ the first derivative of this objective function is:
$$\begin{equation} \begin{aligned}
\quad \text{ } \text{ } F'(z,\theta)
&= \theta + \text{sgn}(\theta) \cdot p'_\lambda (|\theta|) -z \\[6pt]
&= \text{sgn}(\theta) \cdot |\theta| + \text{sgn}(\theta) \cdot p'_\lambda (|\theta|) -z \\[6pt]
&= \text{sgn}(\theta) \Big[ |\theta| + p'_\lambda (|\theta|) \Big] -z \\[6pt]
&= \text{sgn}(\theta) \cdot H_\lambda(\theta) -z. \\[6pt]
\end{aligned} \end{equation}$$
This gets us to the point where the derivative of the objective function depends on the function $H_\lambda(\theta)$, which is the function subject to the condition in question.
 To complete the examination of the minimisation problem, the authors look at what happens in different cases when the minimum of this function is positive, negative, or zero.  This discussion occurs on p. 1350 of the paper, and with the above form for the derivative of the objective function, it should now be easier to understand.
(As some other commentators have also noted, Figure 3 appears to me to be intended as a generic figure, rather than one corresponding to a specific form of probability function.)
Answer: For conclusion A sufficient condition for sparsity is $\text{min}_{\theta \neq 0} \{|\theta| +  p_\lambda'(\theta)\} \gt 0$, I think I got some thoughts.
Given the condition (2) $\text{min}_{\theta \neq 0} \{|\theta| +  p_\lambda'(\theta)\} \gt 0$, then we have two cases regarding $|z|$.

If $|z| \lt \text{min}_{\theta \neq 0} \{|\theta| +  p_\lambda'(\theta)\}$, then according to Figure 3, the first-order derivative is positive for $\theta > 0$, and negative for $\theta < 0$. That is, the objective function is monotonously decreasing in $(-\infty, 0)$, and increasing in $(0, +\infty)$. Therefore, the penalized estimator $\hat{\theta} = 0$ in this case. Note that the objective function could be nondifferentiable at $\theta = 0$. Note also that $|z| = |\hat{\theta}^{OLS}|$ in the orthonormal case. That means, when the least square estimates are small enough (within a range), then the penalized estimates would be shrunk to 0, which leads to sparsity.
If $|z| \gt \text{min}_{\theta \neq 0} \{|\theta| +  p_\lambda'(\theta)\}$, then according to Figure 3, there are two roots, say $r_1, r_2$, to the first-order equation. Thus, the objective function first increases untill $\theta = r_1$, then decreases until $\theta = r_2$, then increases again. That is, $\theta = r_2$ is the minimizer, thus the penalized estimator. Since $|z|$ is sufficiently large, we actually don't need to shrink the estimator since $\hat{\theta} = z$ is approximately unbiased if condition (1) is satisfied.

On the other hand, if the sufficient condition doesn't hold, we then cannot find a situation where the objective function can be monotonously decreasing in $(-\infty, 0)$ and increasing $(0, +\infty)$. That is, the penalized estimator will not be zero.


Get frame of View inside UIViewController with UINavigationController

Question: I have a view controller, which has a UINavigationController, and a UITabController attached.
I'd like to know if there's a way to get the available screen space? I'd really like to be to say CGPoint(0,0); is definitely in the top left corner, just below the UINavigationController and that self.view.frame.size.height is only the space I have left to draw in.
I've tried:
self.view.bounds and self.view.frame, but both think that the available space is 320x460, not 320x367 as I'd expect.
What would be the most correct way to do this, aside from hard-coding the number?
Comment: Where exactly are you calling this methods? Did you try in `viewDidAppear` already?
Comment: Sorry - I'm calling them in `viewDidLoad`. Yes - `viewWillAppear` works. Please could you answer the question with this so I can mark it?
Comment: So call in viewDidAppear and post what you get.
Comment: I tried `CGSize bounds = self.view.bounds.size;` in `viewDidAppear` and get the result I was expecting 320x367. Thank you,
Answer: If you try to log the bounds in viewDidAppear instead of viewDidLoad, you will get the expected result.


Classifying the singularities of a function

Question: I'm trying to classify the singular points of the following function but I'm having some troubles.
$$f(z) = \frac{z(z-1)^{1/3}\sin(1/z)}{(z^2+1)^3\sin(z)}$$
This is what I have so far:

$z=i, z=-i$ are poles of order 3 because of the third power in the denominator.
Because of the $\sin(z)$ we have simples poles for $z=n\pi, n \in Z$
I suspect that $0$ is also a singularity because of the $\sin(1/z)$ but I'm not sure.

What do you think ?
Comment: What does $(z-1)^{1/3}$ mean?
Comment: $\sqrt[3]{z-1} $ maybe that is more understandable, my apologies !
Comment: And what does *that* mean?
Comment: I don't see how it would help me here, since $(z^2+1)=(z-i)(z+i)$. Maybe I'm missing out something.
Comment: What is it that you don't see how it would help you?
Comment: I don't see how the $(z-a)^{1/3}$ is helpful here. Sometimes you can find a removable singularity using terms in the nominator but here no idea how or if it is the case.
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/141764/discussion-between-jose-carlos-santos-and-bsaoptima).
Answer: The first two point are correct.

$z=i, z=-i$ are poles of order 3 because of the third power in the denominator.
Because of the $\sin(z)$ we have simples poles for $z=n\pi, n \in Z$

Now recalling $sin(\frac{1}{z})= Imm(e^{\frac{1}{z}})$ by developing in series $sin(1/z)=\sum_{k, dispari}(i)^{k-1}(\frac{1}{z})^k(\frac{1}{k!})$ Intuitevely $sin(1/z)=\frac{1}{z}-\frac{1}{6z^3}+...$. Which is by definition an
essenzial singularity.
Comment: I think point 2 is wrong: the case $n=0$ is special, you can't just pull out the $\sin z$ alone, you've go to worry about the other parts of the formula.
Comment: I just copied what he wrote in the first two part, if you consider just $1/sin(z)$ then he is right. I think the aim of the exercise is just finding different type of singularities. Otherwise you are right.


Cronometro Progressivo Atualizando div

Question: Gostaria de saber como fazer um cronômetro progressivo, visível na tela e que a cada 60 segundos atualizasse uma div que o código buscou de um outro arquivo PHP.
Se possível, gostaria que hora minutos e segundos fossem imprimidos separadamente para melhor posicionamento na tela.
Abaixo segue o segundo arquivo que vou chamar de 60 em 60 segundos:
<?php

date_default_timezone_set('America/Sao_Paulo');

$d = date('d');
$m = date('m');
$y = date('Y');
$h = date('H');
$i = date('i');
$s = date('s');

#setando a primeira data  10/01/2008
$dia1 = mktime(22,45,00,04,04,2019);

#setando segunda data 10/02/2008
$dia2 = mktime($h,$i,$s,$m,$d,$y);

#armazenando o valor da subtracao das datas
$d3 = ($dia2-$dia1);

#usando o round para arrendondar os valores
#converter o tempo em dias
$dias = round(($d3/60/60/24));

#converter o tempo em horas
$hrs = round(($d3/60/60));

#converter o tempo em minutos
$mins = round(($d3/60));

#exibindo  minutos
echo '2Quilos: '.$mins."
";

Comment: Fabio, como está até o momento o que vc fez?
Comment: coloquei o codigo acima do arquivo que vai ser atualizado pelo cronometro. Essa parte do cronometro eu não sei fazer
Answer: cronometro.html
<!DOCTYPE HTML>

    <meta charset="UTF-8">
    <title>Index</title>

            Variável
            Última atualização
            Relógio

            hora
            minuto
            segundo

            Contando 60 segundos

cronometro.php
<?php

date_default_timezone_set('America/Sao_Paulo');

$d = date('d');
$m = date('m');
$y = date('Y');
$h = date('H');
$i = date('i');
$s = date('s');

#setando a primeira data  10/01/2008
$dia1 = mktime(22,45,00,04,04,2019);

#setando segunda data 10/02/2008
$dia2 = mktime($h,$i,$s,$m,$d,$y);

#armazenando o valor da subtracao das datas
$d3 = ($dia2-$dia1);

#usando o round para arrendondar os valores
#converter o tempo em dias
$dias = round(($d3/60/60/24));

#converter o tempo em horas
$hrs = round(($d3/60/60));

#converter o tempo em minutos
$mins = round(($d3/60));

#exibindo  minutos
$variavel = "2Quilos: ".$mins."
";
$last_update = $h.':'.$m.':'.$s;

$array = [
    'variavel' => $variavel
];

header('Content-type: application/json');
echo json_encode($array);

Comment: Parceiro é isso mesmo só o cronometro que não está rodando e queria o timer separado hora min e segundos
Comment: Editado para mostrar um relógio (cronômetro?!) e hora minutos e segundos em `divs` separadas no doc `html` usando `moment.js`.

Cronometro Progressivo Atualizando div


Can you use Twilio Test Credentials in the API Explorer

Question: Is it possible to use the Twilio supplied Test Credential in their API Explorer?
I tried and get an error returned. I'm new to using the API Explorer and Test Credentials so maybe I am missing something.
Answer: Twilio evangelist here.
You'd have to be able to specify both the Account Sid and AuthToken, and I don't believe the API Explorer lets you set the AuthToken.
I'm curious what your looking to achieve by using test credentials with the API explorer.


Binding Slider value to Textbox

Question: I'd like to dynamically create a Slider with its value bind to TextBox inside the DockPanel. When I'm trying to do so I cannot bind the slider value to the TextBox, and inside the TextBox I'm getting the following message: {Binding ElementName=slValue, Path=Value, UpdateSourceTrigger=PropertyChanged} instead of the Slider's value.
Here's the code I've written so far:
double minimum = 0.0;
double maximum = 100.0;
double defaultValue = 5.0;
DockPanel item = new DockPanel();
item.VerticalAlignment = VerticalAlignment.Center;
Slider slValue = new Slider()
{
     Minimum = minimum,
     Maximum = maximum,
     TickFrequency = 1.0,
     Value = defaultValue,
     IsSnapToTickEnabled = true,
     Name = "slValue",
     Width = 100
};
TextBox slValueTB = new TextBox()
{
     Text = "{Binding ElementName=slValue, Path=Value, UpdateSourceTrigger=PropertyChanged}",
     TextAlignment = TextAlignment.Right,
     Width = 40,
};
item.Children.Add(slValue);
item.Children.Add(slValueTB);

And here's the xml code I'm trying to recreate dynamically:
        <DockPanel VerticalAlignment="Center" Margin="10">
            <TextBox Text="{Binding ElementName=slValue, Path=Value, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" TextAlignment="Right" Width="40" />
            <Slider Minimum="0" Maximum="100" TickPlacement="BottomRight" TickFrequency="5" IsSnapToTickEnabled="True" Name="slValue" />
        </DockPanel>

Comment: `{Binding ...}` is a markup extension that only works in XAML, not in code behind. See [How to: Create a Binding in Code](http://msdn.microsoft.com/en-us/library/ms742863.aspx).
Comment: If you want to write code like that you are doing something fundamentally wrong, namely not using [data templates](http://msdn.microsoft.com/en-us/library/ms742521.aspx).
Comment: I've tried doing it the same way as in example above, but i still can't bind its value to the TextBox. Here's what I've managed to do so far:     Binding myBinding = new Binding("ValueChanged");
            myBinding.Source = slValue.Value;
            slValueTB.SetBinding(TextBox.TextProperty, myBinding);
Answer: It should work this way:
var b = new Binding();
b.Source = slValue;
b.Path = new PropertyPath("Value");
slValueTB.SetBinding(TextBox.TextProperty, b);

or shorter:
slValueTB.SetBinding(TextBox.TextProperty,
    new Binding
    {
        Source = slValue,
        Path = new PropertyPath("Value")
    });

or even shorter:
slValueTB.SetBinding(TextBox.TextProperty,
    new Binding("Value") { Source = slValue });

Answer: Here's a snippet how to set a binding in code behind. I hope this gets you started:
var b = new Binding();
b.Source = slValue;
b.Path = new PropertyPath("Value");
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
slValueTB.SetBinding(TextBox.TextProperty, b);

Remark: Using ElementName requires that the name is unique
Edit: Your path in the Binding-constructor ("ValueChanged") of your comment looks a bit strange. Have you tried "Value" instead? And the Source-Property should be the control.
Comment: see my update. But its pointing to the same things as Clemens wrote a bit later
Comment: I've tried it but it's still not working :/. Thank you for your help though.

Binding Slider value to Textbox


DX7 BATTERY CHECK = 4.3V instead of 3V even after replacing it with new compatible battery

Question: I bought my Yamanha DX7 with this problem.
The INTERNAL MEMORY presets had scrambled named and wouldn't play any sound.

Reading the different threads on the web, I figured this was due to the battery although the BATTERY CHECK showed 4.3V.

Before I attempted to change the battery, I first reloaded the INTERNAL MEMORY with sounds.
Loading sounds to Yamaha DX7
Hereafter the steps found on http://bobbyblues.recup.ch
Set your DX7

Button FUNCTION then button 8 then set MIDI CH=1 i.e. Use the data entry slider or button YES/NO
Button 8 again then set SYS INFO AVAIL with button YES
Button INTERNAL MEMORY PROTECT then set to OFF with button NO

Soundbank transfer to your DX7
Used a Macbook Pro + SysEx Librarian
with a Cahaya Midi USB Interface 5 Pin Din Plug Cable for PC/Mac

Run your SysEx transfers program on your computer and load or open (depending on the program you are using) the SysEx file you want to transfer
Choose "SysEx send" (or the appropriate option) on your SysEx transfers program
You must then see MIDI RECEIVED on your DX7/TX7 display (transfer is OK)

This operation restored my internal memory, but the next day the presets were gone again and the label were scrambled.
The battery still showing 4.3V
So I decided to attempt the battery change.
Battery change

I ordered a 3V - CR2032 solder pin for Yamaha DX7 found on eBay.

I followed the instruction found on the NEGATRON BLOG
On my first attempt, I soldered the battery to 2 wires instead of directly soldering to the board, in order to allow the operation to be easier the next time if needed.

I closed DX7 but I didn't do the BATTERY CHECK, so I don't know if showed correct.
I loaded the new presets (using MIDI/USB interface) and all looked fined. I turned ON/OFF the DX7 + un/plugged it and checked the INTERNAL MEMORY and the presets were there.
I thought I had resolved it but an hour later the presets were scrambled again. i.e. No sound.
The battery change didn't help. BATTERY CHECK showed 4.3V ??? instead of 3V.
I read further and the first comment of the blog page, Kevin described my issue. BATTERY CHECK showing 4.5V instead of 3V.
That's when I decided to give my soldering another go. Interestingly Kevin says:

the trace coming off of the positive terminal of the battery that was supposed to connect to the next component had a gap in it in this kind of shape: —||—

I had also noticed that and I had bridged the battery pin to the diode...
On my second attempt, I removed the mainboard again and this time, I added more solder to look like on the picture on the blog
Closed the DX7, powered it and BATTERY CHECK showed 3V!!! YEAH!
...BUT the next day, I switched on my DX7 to my disappointment, the internal memory is corrupt again and to my surprise BATTERY CHECK showed 4.3V ???
Why would the BATTERY CHECK show 3V after replacement and 4.3V the next day?
Any pointers of what may be happening?
UPDATE
A minute ago the BATTERY CHECK was showing 3V again.
I touched my wires and it fluctuated!
Looks like it might be my soldering or bad wires.
I removed the battery completely BATTERY CHECK showed 4.3V!
Wired it again and it's now showing 3V. Let's see if that keeps the memory overnight.
Comment: There *may* be a capacitor that helps the battery - they tend not to last as long as the DX7 has been around.
Comment: You just need more souls. Everyone knows the DX7 runs on human souls.
Comment: I **highly** suggest you edit out the whole story leading up to your question and just ask your question. We don't need to know the process for loading sounds.
Comment: I will reorganize the question, the history behind the question is important though
Comment: I skipped it and I still understand your question. To me it's just noise. You changed the battery but for some reason it's still not storing the presets correctly. That's pretty straightforward.
Comment: I guess other people will skip the noise too. Thank you Todd.
Comment: What happens if you never turn it off? It's possible there is something besides the battery that is messed up.
Comment: It could be. That is also my 2 cents. I have indeed turned OFF/ON and unplugged from wall socket too (See question) and presets are there for an hour or more. It's not that it's not storing the preset, it's rather that it's not keeping the presets after a period of time (don't know how long). This is actually known issue that you usually resolve by replacing the battery. I may have hit a corner case where something else indeed is happening. The fact that the battery check shows 4.3V instead of 3V is suspicious
Comment: @ToddWilcox I have rewired my battery. may be a bad cable. Voltage fluctuated when I touched the cables. Let's see if that lasts. Thx
Comment: Today the memory is still intact, but will wait a few days before I confirm. I have heard indeed that some people recap their DX7. Thank you for the other pointer.
Comment: For me it sounds there is a common misconception about the battery: I guess the original part is a rechargable one. So if the battery is worn out or disconnected, we see the voltage applied to charge it: 4.3V. If it is connected, the voltage drops to the battery voltage, which would slowly rise while the unit is powered on (battery charging) and sinks while the unit is switched off (battery used to keep up memory).
Comment: This also explains why old DX7 batteries sometimes look inflated (worn out battery charged to much) and why a replacement usually won't last long (non-rechargable battery gets charged!)
Answer: When the battery is not connected, DX7 BATTERY CHECK shows 4.3V
Check that you have wired/soldered properly the new battery, it should show 3V.
Also this comment in the blog helped a great deal

the trace coming off of the positive terminal of the battery that was supposed to connect to the next component had a gap in it in this kind of shape: —||—

Another lead is to check/replace the capacitor "that helps the battery" as suggested by @Tim in the comment section


Executable fails with weird exception

Question: I am using ILMerge and Quartz.NET in a C# .NET 4.0 Windows Service application. The app runs fine without using ILMerge, but now that we're nearing shipping release, I wanted to combine all DLLs into a single executable.
Problem is, that ILMerge seems to work fine, but when I run the combined executable, it throws this exception:

Unhandled Exception: Quartz.SchedulerException: ThreadPool type 'Quartz.Simpl.SimpleThreadPool' could not be instantiated. ---> System.InvalidCastException: Unable to cast object of type 'Quartz.Simpl.SimpleThreadPool' to type 'Quartz.Spi.IThreadPool'.
  at Quartz.Util.ObjectUtils.InstantiateType[T](Type type) in :line 0
  at Quartz.Impl.StdSchedulerFactory.Instantiate() in :line 0
  --- End of inner exception stack trace ---
  at Quartz.Impl.StdSchedulerFactory.Instantiate() in :line 0
  at Quartz.Impl.StdSchedulerFactory.GetScheduler() in :line 0

Does anyone have any idea why this is? I have been wasting over 4 hours already and I can't figure it out. If I don't combine with ILMerge, then everything runs fine (with the Quartz.dll and Common.Logging.dll in the same directory).
I'm sure someone must have tried packaging Quartz.net up like this before, any ideas?
Comment: First time I tried to utilize ILMerge, ran it, didn't work anymore. Figured it must have been ILMerge, tried the internalize flag, didn't change anything. Remove ILMerge, compiled normally (like I used to before trying this out), all works (if DLLs are in same directory).
Comment: Is this the first time you tried to combine it with ILMerge? Or did it work prior to recent changes?
Comment: One of the things that ILMerge doesn't handle is type loading from an external assembly (which might be the case based on a glancing over the stacktrace). Maybe look also into one of the alternatives found [here](http://chrisghardwick.blogspot.nl/2012/01/ilmerge-getting-started-merging-and.html)
Comment: @rene would that mean I should try to load these external assemblies (not sure which) at startup manually? could that solve the issue?
Comment: @rene what part of the stracktrace would tell you that an assembly is missing? It's complaining about an invalid cast?
Comment: Don't use ilmerge. Instead use Jeffrey Richter's solution see http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
Comment: sometimes ILmerge doesn't work.  You could try something like SmartAssembly, to see if you get better results.
Answer: You could try creating your own ISchedulerFactory and avoid using reflection to load all of your types.
The StdSchedulerFactory uses this code to creat a threadpool. It's where your error is happening and would be the place to start looking at making changes:
<code>        Type tpType = loadHelper.LoadType(cfg.GetStringProperty(PropertyThreadPoolType)) ?? typeof(SimpleThreadPool);

        try
        {
            tp = ObjectUtils.InstantiateType<IThreadPool>(tpType);
        }
        catch (Exception e)
        {
            initException = new SchedulerException("ThreadPool type '{0}' could not be instantiated.".FormatInvariant(tpType), e);
            throw initException;
        }
</code>
The ObjectUtils.InstantiateType method that is called is this one, and the last line is the one throwing your exception:
<code>    public static T InstantiateType<T>(Type type)
    {
        if (type == null)
        {
            throw new ArgumentNullException("type", "Cannot instantiate null");
        }
        ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
        if (ci == null)
        {
            throw new ArgumentException("Cannot instantiate type which has no empty constructor", type.Name);
        }
        return (T) ci.Invoke(new object[0]);
    }
</code>
Right after this section in the factory, datasources are loaded using the same pattern and then the jobs themselves are also loaded dynamically which means you'd also have to write your own JobFactory. Since Quartz.Net loads a bunch of bits and pieces dynamically at runtime going down this road means you might end up rewriting a fair amount of things.
Answer: Disclaimer: I don't know Quartz.NET at all, although I spent some time struggling with ILMerge. When I finally understood its limitations... I stopped using it.
ILMerge'd application tends to have problems with everything which contains the word "reflection".
I can guess (I've never used Quartz.NET) that some classes are resolved using reflection and driven by configuration files.
Class is not only identified by its name (with namespace) but also by assembly it is coming from (unfortunatelly it doesn't get displayed in exception message).
So, let's assume you had (before ILMerging) two assemblies A (for you Application) and Q (for Quartz.NET).
Assembly 'A' was referencing assembly 'Q' and was using a class 'Q:QClass' which was implementing 'Q:QIntf'.
After merging, those classes became 'A:QClass' and 'A:QIntf' (they were moved from assembly Q to A) and all the references in code has been replaced to use those (completely) new classes/interfaces, so "A:QClass" is implementing "A:QIntf" now.
But, it did not change any config files/embedded strings which may still reference "Q:QClass".
So when application is reading those not-updated config files it still loads "Q:QClass" (why it CAN find it is a different question, maybe you left assembly 'Q' in current folder or maybe it is in GAC - see 1).
Anyway, "Q:QClass" DOES NOT implement "A:QIntf", it still implements "Q:QIntf" even if they are binary identical - so you can't cast 'Q:QClass' to 'A:QIntf'.
The not-ideal-but-working solution is to "embed" assemblies instead of "merging" them. I wrote a open-source tool which does it (embedding instead of merging) but it is not related to this question. So if you decide to embed just ask me.

You can test it by removing (hiding, whatever works for you) every single instance of Q.dll on your PC. If I'm right, the exception should say now 'FileNotFound'.
Comment: I ended up not using ILMERGE, it just wouldn't work and seemed like it required a lot more work than the benefit of having just a file files less in deployment runs. I also tried embedding, loading the assemblies dynamically, which helped me understand a lot more of the behind the scenes stuff, but it just wouldn't work for the hell of it. I loaded the assemblies at the earliest point, but had lots of static classes accessing them and they needed them to be re-loaded, but even static constructor was too late, strangely. always failed. your answer had most background, so thanks!
Comment: What do you mean by 'loading the assemblies at the earliest point'? I have successfully used the method Jeffrey Richter described on his blog (see my earlier comment). It was very straight forward and the only to watch for is, you need to ensure that your  AssemblyResolve event is initialized before anything else, which in my case meant creating a new entry point that setup the AssemblyResolve event and then called the old entry point.
Comment: @RomanMittermayr: I'm also puzzled "earliest" point. Although static constructor is quite early, the earlies point is module initializer. Unfortunately, module initializer cannot be altered with C# but can be altered with some minor IL manipulation (see: https://github.com/Fody/ModuleInit). When you say "reloaded" I have a feeling that Quartz.NET is creating separate AppDomains. That might be, in fact, a problem if you can't access them and inject you AssemblyResolver into them. You can try https://libz.codeplex.com/ as well.

Executable fails with weird exception


How to find source IP Address?

Question: How to find the default Default IP Address if my I am connected to more than one network? I found one link

http://libtins.github.io/tutorial/sending/

But In "Sending network layer PDUs" It is written that
PacketSender sender;

IP pkt = IP("192.168.0.1") / TCP(22) / RawPDU("foo");
sender.send(pkt);
Note that no source address was specified in the IP constructor. This uses by default the address 0.0.0.0. However, when sending network layer PDUs, if the source address is 0.0.0.0, the PDU will perform a lookup on the routing table to find out which should be the source address and automatically sets it.
So, Is there any way to find the source address using c++?
Answer: Once you connect the socket, you can call getsockname on it to find out the local IP address.

How to find source IP Address?


How to load one certain js before all others?

Question: We are struggling around here with SVG in IE8. Amongst other approaches we'ld also like to try the SVG Web Toolkit. In it's quick start guide they say:

To use SVG Web, you must drop a script tag into your HTML page. This must be the first script tag on your page.

The JS is now loaded by a custom module. I tried
drupal_add_js($modulepath . '/libraries/svg.js', array(
   'group' => JS_LIBRARY,
   'weight' => -1000,
   'every_page' => TRUE));

When I vary these options it's sometimes higher, sometimes lower. The above code makes it the highest I can get it. But there still are quite a number of scripts loaded prior to it.
How can we ensure the svg.js to load first? Is there maybe a way to do it with drupal_add_js in our custom module?
Answer: Can you try with hook_js_alter
Answer: You can install the js_injector module in which you can choose the priority of your .js file


Any Javascript/Jquery Library To validate SQL statment?

Question: i have a textarea in my project where users can save SQL queries but one of the requirements is to check if this query is valid or not
EXAMPLE
if user entered something like :
SELECT ** FROM EMP

this should return false and an error message just saying invalid it doesn't have to give any reasons
mainly the queries will be just select statement
NOTICE
i DON'T want to use any server side at this point
Question
Does any one knows a javascript / jquery  library or plugin that have this functionality or any thing similar
i have Google this and it didn't show any thing
Thanks
Comment: Does the parser need to know about your database? i.e. Which tables/views you have?
Comment: It would be far better to let the database engine figure this out for you.  I wonder if there is a way to do a dry run of a query... see if the query compiles but not execute it...
Comment: Did you Google 'javascript sql parser'? I just found this: https://github.com/forward/sql-parser
Comment: No it doesn't have too ... i don't think javascript can do this
Answer: Here is an example fiddle using the JS SQL Parser:
http://jsfiddle.net/Hb6az/
The parser will throw an error if it hits something unexpected, so you have to wrap your checking code in a try {} catch(error) {} block.
Comment: Note from the description: "Currently it is only capable of parsing fairly basic SELECT queries"
Comment: it looks good but as @MichaelMior said it parse basic SELECT queries see http://jsfiddle.net/minagabriel/Hb6az/1/
Comment: Try it from your local machine, it looks like jsFiddle is having trouble with the external resource and SQLParser is not getting defined.
Comment: @Laurence - I tried installing JS SQL Parser from localhost WAMP server. I couldn't find working. Is there a better way to make it work?
Comment: @ronquiq I have not used this in a very long time but it was an NPM package, you'd need to install it with `npm` or `yarn`


Apache refused to include TLS1.2

Question: I was running openssl0.9.8 for a long time and suddenly my SSL gave me a red X.
From the company that I purchased the ssl, they told me that i need to update my server config to include tls1.2 and also to regenerate the certificate using SHA2.
Since apt-get update wasnt updating openssl, i manually compiled it and now im working with version 1.0.1j. Im able to use tls1.2 to connect to through the VPS, but even if i had included SSLProtocols -all in my apache configuration and restarted server, https://www.ssllabs.com/ssltest/ reports that my server is not using tls1.2.
I excluded sslv2 + v3 and changes were fine. Still not able to use tls1.2 although.
Any ideas? Apache version is 2.2, running debian 6, PHP5-FPM
Comment: Tried adding +TLSv1.2 to the config file SSLProtocols and got back: SSLProtocol: Illegal protocol 'TLSv1.2'
Comment: You need to make sure you are loading the new module which is in a different location from your old module. What do you have for your load module statement?
Comment: Don't keep using Debian 6. It's time to update your whole stack.
Comment: @StefanLasiewski I did with ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl - and it loads fine, even after reboot, but i guess that apache needs recompile in order to work with the new module. Is it true?
Comment: /usr/bin/openssl is the commandline utility for using openssl. The Ssl module for Apache is different. Look at the LoadModule pines on your httpd.conf file. You probably do not need to recompile Apache.
Comment: Don't replace /usr/bin/openssl with something else. It will break your system, as many things depend on it. Instead, install OpenSSL to another location like /usr/local and point Apache at that.
Answer: Debian 6 (Debian Squeeze) is an old Debian version and will not include newer utilities like OpenSSL 1.0.1 & above. Utilities like OpenSSL 0.9.8 will receive security updates until February 2016. Plan your migration to Debian stable now.
If you are not very experienced in compiling utilities like OpenSSL and replacing them in Apache, don't do it-- you are digging yourself into a rathole and you may corrupt your system.
Giles at unix.stackexchange.com says it clearly:

Debian squeeze is no longer current, so you can't expect recent
  versions of programs to be available for it. If you really need the
  openssl command line tool, you could recompile it, but do consider
  whether you really need that: there aren't that many new features.
  There are still security updates for squeeze, if that's what you're
  concerned about.

Apache refused to include TLS1.2


.map function not rendering react data

Question: I have a function that calls an API, and returns an array when console.logged. Use effect is running, and return the array normally but it never updates the front end. Have been working on this for a while, and haven't found a solution. Am I missing something?
useEffect(()=>{
        const fetchData = async (user) => {
            console.log(user)
            const { data, error } = await supabase
                .from('contacts')
                .select()
                .match({ user_id: user })
            return data;
        }
        const user = supabase.auth.user().id;
        fetchData(user).then((data)=> {
            // console.log(data)
            setContacts(data);
            console.log(contacts)
        }).catch(error=>{
            console.log(error)
        })
        // returns an array or objects. each object is a contact
    },[contacts])
    return(

All Contacts

                    Name
                    email
                    phone
                    notes

                {
                    contacts.map((contact, i) => {

                            {contact.prosepct_name}
                            {contact.prospect_email}
                            {contact.prospect_phone}
                            {contact.notes}

                    })
                }

Answer: On first glance - it looks like you need to wrap the inner part of your map function in a return.
 {
    contacts.map((contact, i) => {
      return (

          {contact.prosepct_name}
          {contact.prospect_email}
          {contact.prospect_phone}
          {contact.notes}

      )
    })
  }

Comment: Thank you. Not too sure how I missed that (facepalm)
Comment: No worries - we've all been there!

.map function not rendering react data


Limitador de caracteres no text-box do ASP.NET

Question: Estou querendo implementar um limitador de caracteres pra essa text-box, mas não faço ideia de como fazer isso. É uma pagina de inserção de posts em um portal e o titulo precisa ter um limite.

        @Html.LabelFor(m => m.Titulo)
        @Html.TextBoxFor(m => m.Titulo, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Titulo)

Como posso implementar um limitador?
@Html.TextBoxFor(m => m.Titulo, new { @class = "form-control" })

Comment: Isso é ASP.NET, não tem nada a ver com ASP Classic.
Answer: Você poderia utilizar desta forma:
@Html.TextBoxFor(m => m.Titulo, new { @maxlength="10", @class = "form-control" })


Where in the page generation pipeline can an HTML beautifier be placed?

Question: I need to view the HTML generated by Drupal and it is pretty hard. If I want to the final output to be put through an HTML beautifier before sending it off to the client where would I do that?
I am sure some of the page optimization utilities on the admin/config/development/performance or the advagg module are at the right place in the pipeline. What stage or APIs would I have to hook into to accomplish something this?
Are there any APIs which can be hooked into to accomplish this or even modules to this?
Answer: Yes, in advagg is a good example, you have to get the response content with $response->getContent() in a response subscriber http://cgit.drupalcode.org/advagg/tree/src/EventSubscriber/ResponseSubscriber.php?h=8.x-3.x
  public function onResponse(FilterResponseEvent $event) {
    // Only care about HTML responses.
    if (stripos($event->getResponse()->headers->get('Content-Type'), 'text/html') === FALSE) {
      return;
    }

    $response = $event->getResponse();
    $html_markup = $response->getContent();
    // modify html markup
    $response->setContent($html_markup);
  }

Comment: What is the appropriate stage in Drupal 7?
Comment: I see you've added a new tag for D7, don't know anything about this, but you can try to look in the D7 code of advagg


Finding a Basis for T?

Question: Let T : R$^3$→R$^3$ be projection onto the plane 2x$_1$ − x$_2$ + x$_3$ = 0.
Find a basis for R$^3$ such that T is represented by a diagonal matrix in this basis.
I understand that we have to find some basis which will result in some A(x), so that A(x) is diagonal. But my confusion comes with what we need A to be. Would we be trying to find a matrix A that has the diagonals as the coefficients of the plane?
Answer: I assume that by "projection" you mean "orthogonal projection", else the question cannot be solved as stated. As $T$ is the identity on the plane (which I will denote by $\pi$), we can choose any base of $\pi\subset\mathbb{R}^3$ to obtain the first two vectors. For the third vector, we take a non-zero vector orthogonal to $\pi$. Then the eigenvalues are $1,1$ and $0$.
For example, we can take the basis given by
$$\pmatrix{0\\1\\1},\pmatrix{1\\-1\\1},\pmatrix{2\\-1\\1}.$$

Finding a Basis for T?


Drop first packet with iptables

Question: Our servers are getting UDP-flood spoofed ip attacks. I think, we can solve this problem with iptables and i want to make a rule with iptables.
When a ip tried to send a udp packet, iptables will block this. And after this first packet, for 10 secs. other packets will be accept.
How can i make this with iptables?
Comment: Please, post your current iptables rules.
Comment: I haven't any iptables rule now..
Answer: Try
<code>iptables -A INPUT -p udp [--dport 12345] -m recent --name attack --set
iptables -A INPUT -p udp [--dport 12345] -m recent --name attack --rcheck --seconds 10 --hitcount 2 -j ACCEPT
iptables -A INPUT -p udp [--dport 12345] -j DROP
</code>
This should permit the second and subsequent packets from any given address in a rolling ten-second window; others will be DROPped.  As usual, getting them in the right place in your existing ruleset is both vitally important and your problem.  If you aren't restricting this by port, make very sure you don't lock out DNS responses (and other udp-based responses you may consider desirable).

Drop first packet with iptables


How can i update my component after delete methods in React?

Question: I use Sweetalert2 and react(tsx) and i don't know how to refresh my item list after delete methods.
Here is the code (i have a button that use this functions):
function DeleteCard(item: DataItem){
    Swal.fire({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      icon: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
      if (result.isConfirmed) {
        const response = await Api.delete('api/stuff/${item.Id}')
        Swal.fire(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        )
      }
    }).then(function(){
        window.location.reload(); // this is page reload, it is woking but not a good solutions
    })
}

I tried window.location.reload();, but not a good solution.
Comment: What do you mean by "it is not a good solution"?
Comment: I would like to refresh my list, not the all page
Comment: One method people normally use is to mock the deletion instead of rehydrating the data, you can just select the appropriate item and delete it from the page to avoid making another request for the data.
Answer: If you receive your items list from an api, instead of window.location.reload(), fetch the item list again.


TypeError: cannot do index indexing on  with these indexers [1] of

Question: So I am trying to make a trading algorithm that uses the MFI and volume. So far here is the code:
import pandas as pd
import numpy as np
import pandas_datareader as web
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style

    #get stock prices
    start = dt.datetime(2017, 1, 1)
    end = dt.datetime(2020, 1, 1)
    d = web.DataReader('AMD', 'yahoo', start, end)
    pd.set_option('display.max_rows', 1000)
    pd.set_option('display.max_columns', 1000)

    #MOVING AVERAGES
    d['20_sma'] = d['Adj Close'].rolling(window=20).mean()
    d['50_sma'] = d['Adj Close'].rolling(window=50).mean()
    d['20_va'] = d['Volume'].rolling(window=20).mean()

    #MONEY FLOW INDEX
    d['typical_price'] = (d['High'] + d['Low'] + d['Close'])/3
    d['raw_money_flow'] = d['typical_price']
    mf = d.raw_money_flow.diff(1)
    p = mf.copy()
    n = mf.copy()
    p[p<=0] = 0
    n[n>0] = 0
    pmf = p.rolling(window=14).mean()
    nmf = abs(n.rolling(window=14).mean())
    mfr = pmf / nmf
    d['mfi'] = 100 - (100 / (mfr +1))
    d['mfi'].dropna(inplace=True)

    #Rules
    #trends
    d['up_trend'] = np.where(d['20_sma'] > d['50_sma'], 1, 0)
    d['down_trend'] = np.where(d['20_sma'] < d['50_sma'], 1, 0)

    #mfi location
    d['mfi_70_overbought'] = np.where(d['mfi'] > 70, 1, 0)
    d['mfi_30_oversold'] = np.where(d['mfi'] < 30, 1, 0)

    #strength of volume
    d['high_volume'] = np.where(d['Volume']>=d['20_va']*1, 1, 0)

    buy_period_col = np.logical_and(d['up_trend']==1, d['mfi_30_oversold']==1)

    buy_period = np.where(buy_period_col, 1, 0)

    buy = np.logical_and(buy_period==1, d['high_volume']==1)
    d['buy'] = np.where(buy, 1, 0)

    sell_period_col = np.logical_and(d['down_trend']==1, d['mfi_70_overbought']==1)
    sell_period = np.where(sell_period_col, -1, 0)

    d['sell'] = np.where(sell_period==-1, -1, 0)

    d['trade'] = d['buy'] + d['sell']

    pos=0

    num=0
    percentchange = []
    prices = []
    #find values

    for i in d['buy']:
    if i == 1:
        x = d.loc[i, ['Adj Close']]

The code works fine up untill the find values block of code at the end. What I am trying to do is index out the Adj Close price for when the algo buys. Though I am getting the error:
    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-d19f86abf5d9> in <module>
     70 for i in d['buy']:
     71     if i == 1:
---> 72         x = d.loc[close]
     73         prices.append(x)
     74 #         if pos==0:

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1765
   1766             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1767             return self._getitem_axis(maybe_callable, axis=axis)
   1768
   1769     def _is_scalar_access(self, key: Tuple):

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1961
   1962         # fall thru to straight lookup
-> 1963         self._validate_key(key, axis)
   1964         return self._get_label(key, axis=axis)
   1965

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
   1828
   1829         if not is_list_like_indexer(key):
-> 1830             self._convert_scalar_indexer(key, axis)
   1831
   1832     def _is_scalar_access(self, key: Tuple) -> bool:

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_scalar_indexer(self, key, axis)
    738         ax = self.obj._get_axis(min(axis, self.ndim - 1))
    739         # a scalar
--> 740         return ax._convert_scalar_indexer(key, kind=self.name)
    741
    742     def _convert_slice_indexer(self, key: slice, axis: int):

~\Anaconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in _convert_scalar_indexer(self, key, kind)
    384             is_flt = is_float(key)
    385             if kind in ["loc"] and (is_int or is_flt):
--> 386                 self._invalid_indexer("index", key)
    387             elif kind in ["ix", "getitem"] and is_flt:
    388                 self._invalid_indexer("index", key)

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in _invalid_indexer(self, form, key)
   3075         """
   3076         raise TypeError(
-> 3077             f"cannot do {form} indexing on {type(self)} with these "
   3078             f"indexers [{key}] of {type(key)}"
   3079         )

TypeError: cannot do index indexing on <class 'pandas.core.indexes.datetimes.DatetimeIndex'> with these indexers [45.86000061035156] of <class 'float'>

Why am I getting this error and how can I fix it? Thank you
Comment: Can you please add your imports to the top of your script
Comment: Sorry about that, they have been added
Comment: For anyone coming from Google, in my case this error came up because I was trying to subtract two dates and one was not a datetimeIndex for some reason. Check the dtypes of any date columns you are manipulating.
Answer: You can use a simpler expression:
X = d['Adj Close'][d['buy']==1]

or
d.loc[d.index[d['buy']==1], ['Adj Close']]

in your example, you are trying to call a location by int value 1, but not a datetime64 type
Comment: I don't think you even need d.index in the .loc. I would instead write d.loc[d['buy'] == 1, 'Adj Close']
Answer: Above answer is correct and more efficient, but if still you want it inside a loop for some post-processing, this is how you can do it:
for i in d['buy'].index:
    if d.loc[i, 'buy'] == 1:
        x = d.loc[i, ['Adj Close']]