How to get the number of unread messages and the last message for each user in a SQL query?

Question: I have a message table like this:
message_id | body         | from_user_id | to_user_id | is_read | sent_date
---------------------------------------------------------------------------
1          | hello        | 23           | 31         | false   | 2 min ago
---------------------------------------------------------------------------
2          | thank you    | 28           | 31         | true    | 4 min ago
---------------------------------------------------------------------------
3          | how are you? | 31           | 28         | false   | 1 min ago
---------------------------------------------------------------------------
4          | a-ha         | 29           | 31         | false   | 6 min ago

I want to get the number of unread messages and the last message for the current user (for example user_id 31)
desired result:
from_user_id | to_user_id | body         | sent_date | unread_message_count
-------------------------------------------------------------------
23           | 31         | hello        | 2 min ago | 1
-------------------------------------------------------------------
31           | 28         | how are you? | 1 min ago | 0
-------------------------------------------------------------------
29           | 31         | a-ha         | 6 min ago | 1

I used below code:
WITH ranked_messages AS (
    SELECT
         messages.from_user_id, messages.to_user_id,
         messages.body, messages.sent_date,
         ROW_NUMBER() OVER
         (PARTITION BY messages.from_user_id
         ORDER BY messages.sent_date DESC) AS message_rank
    FROM
         messages
    WHERE
         messages.to_user_id = 31 OR
         messages.from_user_id = 31
)
SELECT *
FROM ranked_messages
WHERE message_rank = 1;

but I want also unread_message_count per user.
for an example:
select
     count(*) as unread_message_count
from
     messages
where
     to_user_id = 31 and
     is_read = false and
     from_user_id = 23

This should be calculated for each user who sends messages to the user, and vice versa
How can I achieve this result?
Comment: Thought of using group by user_id?
Comment: This code does not work
And it gives Error about the group by
Comment: Oh, my bad, it's in MySQL. You'd have to adapt
Comment: `SELECT count(message_id) as unread_messages_count, body FROM messages WHERE to_user_id = 31 AND is_read = false GROUP BY to_user_id ORDER BY sent_date DESC  LIMIT 1` Order by and group by were reversed
Comment: your codes does not work, because you must add `body` to the group by. I had tried all these ways
But these solutions do not work. thank you man
Comment: `SELECT COUNT(b.message_id) as unread_messages_count, b.to_user_id, (SELECT a.body FROM messages a WHERE a.to_user_id = b.to_user_id ORDER BY a.sent_date DESC LIMIT 1) as last_message FROM messages b  WHERE b.is_read = false GROUP BY b.to_user_id` Okay, my final try.
Comment: Build the query that gets and groups the amount of messages for each user, then sub-query the body into the results
Answer: You can use window functions.  I think this is the logic:
WITH ranked_messages AS (
    SELECT m.from_user_id, m.to_user_id, m.body, m.sent_date,
           SUM(is_read::int) OVER (PARTITION BY from_user_id) as cnt
           ROW_NUMBER() OVER (PARTITION BY m.from_user_id ORDER BY m.sent_date DESC) AS message_rank
    FROM messages m
    WHERE 31 IN (m.to_user_id, m.from_user_id)
)
SELECT *
FROM ranked_messages
WHERE message_rank = 1;

Comment: This code has a number of errors.
When the errors are solved.
For the cnt, the value is zero
Answer: You could include the count as a nested query:
WITH ranked_messages AS (
    SELECT messages.from_user_id, messages.to_user_id, messages.body, messages.sent_date,
        select count(*) as unread_message_count from messages where (to_user_id = 31 or from_user_id = 31) and is_read = false,
        ROW_NUMBER() OVER (PARTITION BY messages.from_user_id ORDER BY messages.sent_date DESC) AS message_rank
    FROM messages
    WHERE messages.to_user_id = 31 OR messages.from_user_id = 31
)
SELECT * FROM ranked_messages WHERE message_rank = 1;

Comment: `(select count(*) as unread_message_count from messages where (to_user_id = 38 or from_user_id = 38) and is_seen = false)` calculates all unread_message_count. I want  unread_message_count per user


Parametric Circles Scrolling + Arrays

Question: I've been working on this problem for a while now, but can't seem to find a way around it.
I am trying to build a horizontal scroll wheel module (made up of "layers" array) with a "selector" layer in the middle of the screen that selects the layer within said array that landed on it, displays said layer's name and the page that is associated with it.
In other words, once "beige layer" lands on "selector", this.name is displayed below this.layer, "beige page".x=0
Right now the problem is that the wheel only moves onDrag, so you have to keep dragging it for it to move as opposed to onScroll. I've tried playing around with reading the x position of layers in the "layers" array relative to "selector" and fire off commands, and I also tried playing around with .parent properties for the pages, but it doesn't work the way it should.
Here's a framer link if that helps, http://share.framerjs.com/psbqx3dvqtz9/
Any help would be appreciated, guys.
Thanks in advance!
Alex
layerCount=8
circleCenterZ=0
circleCenterX= Screen.width / 2
circleRadius= 500
scrollSensitivity= 50
depthOfField= 25
divide= layerCount/Math.PI/1.6
allPosZ=[]
layers=[]
savX = 0
sX = 0
newCat=[]
colors=["green","red","blue","white","pink","purple","beige","orange","yellow","brown"]

select=new PageComponent
    x: 5
    y: 636
    backgroundColor: null
    borderWidth: 5
    clip:false
    scrollVertical:false

scroll = new ScrollComponent # invisible proxy ScrollComponent
    width: Screen.width, y:500
    scrollVertical: false
    height: 306

scroll.content.opacity = 0

colors.forEach (cat,i) ->
    posZ= allPosZ[i]= circleCenterZ+(circleRadius/2)*Math.cos(i/divide)
    posX= circleCenterX+(circleRadius/2)*Math.sin(i/divide)

    layer=layers[i]= new Layer
        width:109
        height:109
        parent:select.content
        y: select.height/5
        borderRadius: 100
        name: colors[i]
        midX: posX
        z: posZ

layers[0].onClick ->
    print this.name

maxDepth = Math.max.apply @, allPosZ
minDepth = Math.min.apply @, allPosZ

for layer,i in layers

    darken = Utils.modulate(layer.z,[maxDepth,minDepth],[0,1], true)
    layer.blur = Utils.modulate(layer.z,[maxDepth/3,minDepth],[0,depthOfField], true)

scroll.content.onDrag ->

    sX = (@.x + savX) / scrollSensitivity

    for layer,i in layers

        posZ= allPosZ[i]= circleCenterZ+(circleRadius/2)*Math.cos((i+sX)/divide)
        posX= circleCenterX+(circleRadius/2)*Math.sin((i+sX)/divide)

        layer.z = posZ
        layer.midX = posX
        darken = Utils.modulate(posZ,[maxDepth,minDepth],[0,1], true)
        layer.blur = Utils.modulate(posZ,[maxDepth/3,minDepth],[0,depthOfField], true)

scroll.content.onDragEnd ->
    savX = sX * scrollSensitivity
    @.x = 0

for i in [0...layerCount]
    category=newCat[i]=new Layer
        backgroundColor: colors[i]
        width: Screen.width
        x: Screen.width*i
        name: colors[i]+" "+"page"

Answer: I think what you're trying to do can be achieved by using onMove instead of onDrag. This will fire during dragging and animations, so you get the velocity of the scrolling as well.
Because you're scrolling in a circle, the scrolling should be infinite. This can be achieved by setting the x on every move to modulo the width of the scroll component. An example of this can be found here.
To apply this to your code you need a couple of changes:

Add a layer to the content of your proxy scroll component with a large width to increase the content size
Set the starting scroll offset to midway through the content
use onMove instead of onDrag
Modify the @x in onMove like this: @x = start + @x % scroll.width
Remove the onDragEnd code

This results in the following code:
layerCount=8
circleCenterZ=0
circleCenterX= Screen.width / 2
circleRadius= 500
scrollSensitivity= 50
depthOfField= 25
divide= layerCount/Math.PI/1.6
allPosZ=[]
layers=[]
savX = 0
sX = 0
newCat=[]
colors=["green","red","blue","white","pink","purple","beige","orange","yellow","brown"]

select=new PageComponent
    x: 5
    y: 636
    backgroundColor: null
    borderWidth: 5
    clip:false
    scrollVertical:false

scroll = new ScrollComponent # invisible proxy ScrollComponent
    width: Screen.width, y:500
    scrollVertical: false
    height: 306

scroll.content.opacity = 0
count = 40
l = new Layer
    width: count * scroll.width
    height: scroll.content.height
    parent: scroll.content
start = -count/2 * scroll.width
scroll.content.x = start

colors.forEach (cat,i) ->
    posZ= allPosZ[i]= circleCenterZ+(circleRadius/2)*Math.cos(i/divide)
    posX= circleCenterX+(circleRadius/2)*Math.sin(i/divide)

    layer=layers[i]= new Layer
        width:109
        height:109
        parent:select.content
        y: select.height/5
        borderRadius: 100
        name: colors[i]
        midX: posX
        z: posZ

layers[0].onClick ->
    print this.name

maxDepth = Math.max.apply @, allPosZ
minDepth = Math.min.apply @, allPosZ

for layer,i in layers

    darken = Utils.modulate(layer.z,[maxDepth,minDepth],[0,1], true)
    layer.blur = Utils.modulate(layer.z,[maxDepth/3,minDepth],[0,depthOfField], true)

scroll.onMove ->

    @x = start + @x % scroll.width

    sX = @x / scrollSensitivity

    for layer,i in layers

        posZ= allPosZ[i]= circleCenterZ+(circleRadius/2)*Math.cos((i+sX)/divide)
        posX= circleCenterX+(circleRadius/2)*Math.sin((i+sX)/divide)

        layer.z = posZ
        layer.midX = posX
        darken = Utils.modulate(posZ,[maxDepth,minDepth],[0,1], true)
        layer.blur = Utils.modulate(posZ,[maxDepth/3,minDepth],[0,depthOfField], true)

for i in [0...layerCount]
    category=newCat[i]=new Layer
        backgroundColor: colors[i]
        width: Screen.width
        x: Screen.width*i
        name: colors[i]+" "+"page"

Working example is here: http://share.framerjs.com/qc7jdyfyw7f6/

Parametric Circles Scrolling + Arrays


How do I make Laravel Elixir merge my files rather than overwrite them?

Question: There are two stylesheets within my application named Main.scss and Reset.css. I try to compile the Sass, then merge it with the other stylesheet (Reset.css) but I end up missing the styles from Main.scss. It was working perfectly well until I decided to add Reset.css.
Here's my gulpfile:
var elixir = require('laravel-elixir');
var assetsDir = 'public/Assets/Stylesheets/';
elixir.config.sourcemaps = false;

/*
 |--------------------------------------------------------------------------
 | Elixir Asset Management
 |--------------------------------------------------------------------------
 |
 | Elixir provides a clean, fluent API for defining some basic Gulp tasks
 | for your Laravel application. By default, we are compiling the Sass
 | file for our application, as well as publishing vendor resources.
 |
 */

elixir(function(mix) {
  mix.sass('Main.scss', assetsDir + 'Main.css')
    .styles('Reset.css', assetsDir + 'Main.css')
    .version('Assets/Stylesheets/Main.css');
});

How can I make Reset.css simply merge into the same file as Main.css without overwriting the content from the .sass call?
Here's the log from calling gulp, also:
[21:24:18] Using gulpfile ~/Code/gulpfile.js
[21:24:18] Starting 'default'...
[21:24:18] Starting 'sass'...

Fetching Sass Source Files...
   - resources/assets/sass/Main.scss

Saving To...
   - public/Assets/Stylesheets/Main.css

[21:24:20] Finished 'default' after 1.87 s
[21:24:20] gulp-notify: [Laravel Elixir] Sass Compiled!
[21:24:20] Finished 'sass' after 1.98 s
[21:24:20] Starting 'styles'...

Fetching Styles Source Files...
   - resources/assets/css/Reset.css

Saving To...
   - public/Assets/Stylesheets/Main.css

[21:24:20] gulp-notify: [Laravel Elixir] Stylesheets Merged!
[21:24:20] Finished 'styles' after 41 ms
[21:24:20] Starting 'version'...

Fetching Version Source Files...
   - public/Assets/Stylesheets/Main.css

Saving To...
   - public/build

[21:24:20] Finished 'version' after 45 ms

Answer: You are creating you Main.css with sass, but then immediately overwriting it with Reset.css. The styles method is there to concatenate different stylesheets, which you can specify as an array as the first argument.
So you would say
.styles(['Reset.css', 'Main.css'], assetsDir + 'Main.css')

In that case though, it will look for Main.css in the resources directory. You can overwrite were the .mix function saves your Main.css by giving a second argument.
So for example
mix.sass('Main.scss', 'resources/assets/css/Main.css')
   .style(['Main.css','Reset.css'], assetsDir + 'all.css']

Comment: Great! I've managed to merge `Main.css` and `Reset.css`, although I wanted `Reset.css` to be first, so I ended up with this code:

`elixir(function(mix) {
      mix.sass('Main.scss', 'resources/assets/css/Main.css')
        .styles([
          'Reset.css',
          'Main.css'
        ], cssDir + 'Main.css');
    });`


Multiply block matrices with uneven blocks

Question: Is there a way to multiply matrices which are divided into blocks of the form
\begin{bmatrix}
    A&B
\end{bmatrix}
For some $n\times n $ matrix A and $n \times 1$ matrix B with its transpose from the left? Without having to multiply every entry.
Answer: I assume you mean $\begin{bmatrix} A & B \end{bmatrix}^T\begin{bmatrix} A & B \end{bmatrix}$.
Then multiplication can be done using block matrices
$\begin{bmatrix} A & B \end{bmatrix}^T\begin{bmatrix} A & B \end{bmatrix}=\begin{bmatrix} A^T \\ B^T \end{bmatrix} \begin{bmatrix} A & B \end{bmatrix} =\begin{bmatrix} A^TA & A^TB  \\ B^TA & B^TB\end{bmatrix} $
Operation is possible if dimensions of block matrices are appropriate for multiplication operation. In this case they are appropriate:
$ A^TA:   \ \ $ $n \times n , n \times n$ result $n \times n  $
$ A^TB:   \ \ $ $n \times n , n \times 1$ result $n \times 1  $
$ B^TA:   \ \ $ $1 \times n , n \times n$ result $1 \times n  $
$ B^TB:   \ \ $ $1 \times n , n \times 1$ result $1 \times 1  $.


GKE can't tag cluster with Terraform config

Question: I want to create similar configuration as described here: https://cloud.google.com/compute/docs/vpc/special-configurations#natgateway
with Terraform but "no-ip" instances are in Kubernetes cluster. Problem is such when you do it manually with command simmilar to:

gcloud container clusters create container-name --tags=sometag

You can set up tags for whole instances started in this cluster but i can't see such possibility when im trying to do that same with Terraform:
https://www.terraform.io/docs/providers/google/r/container_cluster.html
Is there posibility to set tags with Terraform config? I was trying too to find out how to update them manually after all with "gcloud" command but i can't find such possibility too.
Answer: It looks like it's (mistakenly) not documented at the moment, but you should be able to use the tags field in the node_config field to achieve this.
It'd look something like this
resource "google_container_cluster" "my_cluster" {
  // other config goes here
  node_config {
    tags = [ "no-ip"]
  }
}

[EDIT]: Oops, my mistake, this is a recent feature and won't be available until the 0.10 beta releases. Good news is that should be very, very soon. And, even better, docs will be available as soon as 0.10 is released.
Comment: Thank you for reply. This is great news and hope it will be really very soon. They could add scopes to node configuration too (those are two things which are missing for my application - except whole deployments and whole ingress Kubernetes resources).
Comment: Those options still are not available in node_pool configuration https://github.com/terraform-providers/terraform-provider-google/blob/a25461f2cc6f2a58c58b43b67a55c14a5af8b055/google/resource_container_node_pool.go  so it won't help me.
Comment: If you open an issue at https://github.com/terraform-providers/terraform-provider-google/issues/new we can get support added for it!
Comment: Also, 0.10beta1 is out, so tags on entire cluster instances is out. :)
Comment: According to your advice i added new tickets:

https://github.com/terraform-providers/terraform-provider-google/issues/162


Precentage Calculation on two objects of different sizes that returns the same result

Question: I'm try to work out an absolute value in relation to two percentage values.
I have an object (let's call it 'a') that is 75% of the width of it's containing, or parent object. This child object 'a' has a border that is 5% it's own width.
Using purely percentages only, how would I set a border on the parent object (let's call this 'b'), that is the same width as the inner container's border, but only via a mathematical percentage calculation?
In summary, I need a border on both objects, that is the same width on each in absolute terms, but which is calculated via percentages not absolute values. Is this even possible? The child object inside the parent object is 75% of the width of the parent, so I'm thinking it isn't possible.
I'm really, really stuck and want to bang my head against the wall.
Emily
Comment: You should draw a figure with boxes inside each other to show what dimensions you are talking about.  I can't understand if the borders are part of $a$ and $b$, for example.
Answer: I will assume you want the borders to be contained strictly inside the objects, which we shall think of as boxes. You can work out what size your border should be by supposing the parent has a width of 100 (with what ever units you want). The inner box will have width 75. The border of the inner box has width which is 5% of 75, so 75/20, so 3.75. These numbers translate straight to percentages in relation to the parent box, so the border of the inner box has width 3.75% of the width of the larger box.


Query criteria for time and date field individually past midnight

Question: I am currently trying to create query update that runs across 8 hours shift. The query was successful but it got stuck at the past midnight part.
I have 3 shifts:
Shift 1: 7:30am to 4:30pm
shift 2: 4.30pm to 11:30pm
shift 3: 11:30pm to 7:30am
Query criteria for shift 3 that i have attempted:
Date:-----------------------Time:
Date()----------------------Between #11:30:00 PM# And #11:59:59 PM#
Date()+1-------------------Between #12:00:00 AM# And #7:29:59 AM#
Results: it will only work for today [date()] and when it date() reach tomorrow, it will reset to next next day.
shift 1 and shift 2 was successful however the query did not run successfully for shift 3. Both date and time are separated in the table using =date() and =time(). May i know how should i set up the criteria for shift 3?
Answer: You can use and/or:
Shift 1: Where [TimeField] >= #07:30# And [TimeField] < #16:30#
Shift 2: Where [TimeField] >= #16:30# And [TimeField] < #23:30#
Shift 3: Where [TimeField] >= #23:30# Or  [TimeField] < #07:30#

To filter on a date:
Select *
From YourTable
Where [TimeField] >= #07:30# And [TimeField] < #16:30# And [DateField] = [SelectDate]

' For the other shifts:

Where [TimeField] >= #16:30# And [TimeField] < #23:30#And [DateField] = [SelectDate]

Where ([TimeField] >= #23:30# And [DateField] = [SelectDate]) Or  ([TimeField] < #07:30# And [DateField] = DateAdd("d", 1, [SelectDate]))

Comment: Hi, thank you for the answer. May i know where do i put this statement and what about the date when it pass 12 midnight?
Comment: Those are the WHERE clause for your query. Just add a filter for the date field if you need that.
Comment: The problem i face now is that the next day after 7:30:00am is a new shifts but the shift 3 will now be today's date before 7:30:00am + today's date after 11:30:00pm


Ajax append Data to select

Question: I need to add the data to the database and meanwhile load into a select options menu, I successfully added the data and when loading the data gets duplicated in the options menu
function manageRow(data) {
    var rows = '';
    $.each( data, function( i, o ) {
        rows += '<option>'+o.salary_wage+'</option>';
    });
    $(".selectpicker111").append(rows);
}

Comment: Try $(".selectpicker111>*").remove(); from the begin then append :)
Comment: Possibly a duplicate of http://stackoverflow.com/questions/740195/adding-options-to-a-select-using-jquery-javascript ?
Comment: Or do `.html()` instead of `.append()`;
Comment: @ArunaWarnasooriya this didnt work and clears everything
Comment: @itzmukeshy7 Thanks so much it worked
Answer: Try this ;)
$("selector").append(rows);

This will append options to existing options and you will get duplicate options.
There are 2 ways to get rid of duplicate options:
Option 1:
Remove existing options before adding options using empty and then you can add options using either append or html.
$("selector").empty();

Option 2:
Replace existing options with new options using html
$("selector").html(newOptions);

In your case:
$(".selectpicker111").html(rows);

Ajax append Data to select


Set of matrices to the power n^2 are linearly dependent

Question: We are asked to show that the set $S={I_n, A, A^2,...,A^{n^2}}$ is linearly dependent, given $A$ is a square $n$ by $n$ matrix.
I'm not quite sure where to start. We've been doing span, linear combinations, transformations, matrix multiplication, etc. I think A should be invertible, because we must show that:
$a_1 I_n+a_2 A+a_3 A^2+...+a_k A^{n^2}=0$
If the coefficient of the identity element isn't zero, then I can show A must be invertible, since
$a_2 A+a_3 A^2+...+a_k A^{n^2}=-a_1 I_n$
Then
$(x_1 I_n+x_2 A+...+x_k A^{n^2-1}) A= I_n$
No idea where to go from here.
Answer: The dimension of the space of $n\times n$ real matrices is only $n^2$, so you can't have a linearly independent set of size $n^2+1$.
Comment: I would expect that to happen if you took $A$ with random numbers from the reals, but I'm not 100% sure.
Comment: So is the main issue is the set size? I guess it would be possible to have a set that is independent if it was something like $A, A^2,... A^{n^2}$?


Ordenar Aniversariantes por Dia

Question: Galera, precisava de uma ajuda para exibir na tela os Aniversariantes do Mês ordenado pelo dia, no banco ele puxa as informações do Sênior então a data vem ex: 1970-10-30, no meu controller está assim:
public function index()

{
    $condicoes['order'] = array('Funcionario.data_nascimento' => 'ASC');
    $condicoes['conditions'] = array(
        'Funcionario.situacao_afastamento <>' => 7,
        "DATE_FORMAT(Funcionario.data_nascimento, '%m') = DATE_FORMAT(NOW(), '%m')"
    );
    $condicoes['limit'] = 59;
    $this->paginate = $condicoes;
    $this->set('aniversariantes', $this->paginate());
}

Nessa condição ele ordena primeiramente a data do ano :( e depois a data do mês.
Answer: Você pode fazer assim:
$condicoes['order'] = array('DAY(Funcionario.data_nascimento) => 'ASC')
Comment: Está usando algum `datatable` ?
Comment: Acabei fazendo na mão mesmo, obrigado pela ajuda. ficou assim:
[$aniversariantes = $this->Funcionario->query("select matricula, nome, data_nascimento, data_admissao, local_id  from 002_funcionarios WHERE situacao_afastamento <> 7 and extract(month from data_nascimento) = DATE_FORMAT(NOW(), '%m') order by extract(month from data_nascimento), extract(day from data_nascimento)");
  ]
Comment: Olá, então eu tentei isso uma hora mas ele não faz validação, se eu colocar dessa forma. :(
Comment: Como assim validação ?
Comment: Ele pula essa condição, e exibe os funcionários de forma aleatória.
Comment: Qual o tipo do campo `data_nascimento` ?
Comment: Campo do tipo DATE

Ordenar Aniversariantes por Dia


Date.getTime() gives two different values for same timestamp

Question: I have two variables. weekStartDate and startDate. they both hold essentially the same timestamp:
this.startDate  Date {Mon Mar 26 2012 00:00:00 GMT+0530 (IST)}
this.weekStartDate   Date {Mon Mar 26 2012 00:00:00 GMT+0530 (IST)}

the problem is: when I try to getTime(), they show a slightly different value:
this.startDate.getTime()    1332700200000
this.weekStartDate.getTime()    1332700200506

how can i fix this?
Comment: User `this.weekStartDate.setMilliseconds(0)`.
Comment: Can you show how you're instantiating them?
Answer: The difference is 506 milliseconds. The number of milliseconds isn't displayed when you call toString() on a Date object, so anything more precise than seconds will go unnoticed unless you compare the numeric value.
To reset the milliseconds to 0, use:
this.weekStartDate.setMilliseconds(0);

Comment: They aren't exactly the same. weekStartDate was based on the current date and time, correct? The milliseconds on that date object were never reset, so a difference of 506 milliseconds shows up.
Comment: yes. why? when the two timestamps are exactly the same? why is there a difference?
Comment: @amit They are not exactly the same — the problem is that you only see the difference in the numerical timestamp representation.
Comment: @JoelLundberg Yeah, that's what I said, wasn't it?
Comment: @Mathias Bynens I forgot to direct the comment at amit, and you posted your comment while I was writing mine. So yeah, that's what you said.
Answer: I think the milliseconds are different. Set the milliseconds of boths dates to zero then the dates will be the same:
this.weekStartDate.setMilliseconds(0);
this.startDate.setMilliseconds(0);

edit: damn i was to slow


Constructing 3-way ANOVA when design is not fully factorial

Question: I have conducted an experiment measuring individual sizes as a function of two categorical variables (A and B), each with three levels (1, 2, 3). The combination A3:B3 is a control group. This experiment was conducted across two replicates on separate individuals (i.e. no repeated measures). However, design constraints meant that these replicates were performed across three "blocks", as shown in the table below.

As you can see, the control group appears in all blocks, but the other treatments are split across them, so the design is not fully factorial. I would like to test whether there is an effect on size caused by each variable and their interaction, both relative to each other and the control across all blocks. However, I would also like to test if there is an effect of block within each group, e.g. are A1:B1 individuals different in size in block 1 compared to block 2? Visualising the data suggest that this could be the case.
Using ANOVA in R I have set up a 3-way linear model as follows:
model <- lm(Size ~ A*B*Block)
Anova(model, type="II")

However, for the interactions B:Block and A:B:Block this model returns zero degrees of freedom. I believe this is due to factor level combinations with zero observations, i.e. because not all combinations appear in all blocks. I'm also concerned that I do not explicitly want to test the effect of block as a stand-alone variable, again because not all treatment combinations appear in all blocks (and therefore I'd expect to see differences in sizes across the blocks).
I would like help on how best to construct this model, and also how to run appropriate post-hoc tests. Running TukeyHSD(x=model, conf.level=0.95) results in a large number of NA contrasts due to missing factor combinations. How can I better set up these tests so that only comparisons I am interested in are performed?
Answer: You are correct that since not all combination appear in your data-set you will not be able to test certain interactions and contrasts. I am afraid there is no way round this with the constraints you had.
Do you really want to add the three-way interaction to your model? It is up to you but higher-order interactions can be hard to interpret especially when you have missing cells in the design. I would have included the two-ways only. (A+B+Block)^2 should do it. Note that if you fit a model with just the main effects (A + B + Block) there is still one effect which cannot be estimated since A3 and B3 are aliased, they only ever occur together so cannot be separated. Moving on to including the two-ways we find more problems. There is only one estimable effect for the A B interaction because none of A2B3, A3B2 and A3B3 are separately estimable because of the way they co--occur. There are also problems with the interactions with Block
You should not try to remove the main effect of Block. Search for the concept of marginality for explanations of why or look at this Q&A Including the interaction but not the main effects in a model for some discussion.
Comment: The three-way tells you that the interaction between A and B differs between levels of Block which is not the level of detail you seem to want. Since quite a lot of the A by B interaction cannot be estimated since you only have five of the possible nine combinations it will only apply to that part of the A by B interaction.
Comment: What I'd like to know is whether there was variation within treatment combinations (A1:B1, A2:B1, and so on) across blocks. I'm not sure how to do this without including the 3-way interaction. Would your proposed design allow me to capture this effect?
Comment: Understood. Looking at the model you have suggested the B:Block interaction still has zero df (because of missing combinations in block 3), so this is one interaction that I cannot test?


apply a discount to product in Commerce

Question: I have a Drupal Commerce site and I'd like to apply a discount to the price where it is calculated. I have a table in the profile user with SKU | discount pairs and I'd like some way (maybe Rules?) to search the product on the table and apply the discount to the price.
Is there any way to do this in Drupal Commerce?
Answer: I use the Commerce Sale Price module with great success on my website. It is easy to configure. Just install and enable then you can set the discounts on the product pages.
Update: I think you could use Commerce Coupon to achieve your results. The coupons can be automatically applied based on a role. Sorry I did not understand your question fully before. Also, I have not used this module but the description appears to do what you want.
Comment: See updated answer...
Comment: Thank you for the answer! But the problem with this approach is that the discounts on my website are per-user and not per-product. One user can have discounts in product one with x% off, two with y% off and three with z% off but another may have discounts in five with t% off seven with w% off and ten with r% off. The context seems to be pretty difficult to use Commerce Sale Price.
Comment: Is it possible with Drupal Coupon to apply the discount for a certain product and user when Rules is computing the product price?
Comment: I would expect so but I couldn't be sure without trying. I know I have used a similar module for one site I developed that had Ubercart. Based on the description and my knowledge, I would be surprised if this module didn't do what you want.
Answer: I can't give solution for your the values which you saved in the user profile as I have never had that situation but I can give simple solution based on the requirement which I understood from your comment.
You can set several rules for discount based on no. of items in cart & user type.
Rules work on 3 things - Event, Condition & Action.
So your event will be - Completing the checkout process.
Condition - Total product quantity comparison & User has role(s).
Action - Multiple the unit price by some amount (Which you can set as per your discount model e.g. 0.95 for 5%, 0.92 for 8% etc).

apply a discount to product in Commerce


How to enable ART accelerator STM32F407VGT6

Question: I keep reading about the ART accelerator on the STM32F407VGT6 and zero wait states at 168MHz and it is mentioned briefly in the data sheet but I can't find anywhere how to enable it.
There is a prefetch option in the FLASH_ACR register but enabling it causes my board to stop
working. Not sure if this is the same thing. Is ART even something that needs to be enabled or is it enabled by default? I notice in the errata for this chip that the ART was not working in A version but has been "fixed" in the current Z rev.
Thanks
Comment: I am as confused about whether or not it is enabled by default either.  I have some examples for the stm32f4 discovery http://github.com/dwelch67/stm32f4d with it cranked up to 168Mhz and the caches and prefetch and such on.  If you change the FLASH_ACR make sure you have the right number of wait states depending on your processor speed or it will crash/hang...For example 5 wait states for 3.3Volts 168Hz...Or maybe if you are already running then do a read-modify-write to turn on the icache and prefetch.
Comment: I have it set for 5 wait states at 168mhz like it says to do and still with prefetch it hangs. What is very mysterious is it quotes performance figures for both enabled and disabled in the data sheet. Sounds like a question for STM tech support
Answer: This link has the method of configuring the ART for stm32F2 devices. The same registers are found in the STM32f4 discovery series. Check for the word ART configuration in the pdf.  I'm having the same problem and this is the best answer I could find...
Comment: Yeah I still find that confusing. I think ART is misleading as it is only 0 wait states at certain clock speeds and it needs 5 wait states if running at 168MHz
Answer: I read in that document: STMicroelectronics: AN3430 - Application Note of How to achieve the lowest current consumption with STM32F2xx
On page 17 (3.1.1) they say that enabling the buffer prefetch will consume additional 20mA (for 128-bit-line fetching).
Maybe at 168 MHz you power supply gets unstable??
Maybe you can try it with lower clock speed (120MHz), where the cpu is very power efficient.


Find the surface area of this function when rotated around the y axis. Stuck

Question: Say I have this equation:
$$y = \frac{1}{3} x^{\frac{3}{2}}$$ and the limits of the integration provided are $0 \leq x \leq 12$
So since we're rotating around the y axis, I'm going to change the limits of integration to use y.
$$0 \leq y \leq 8 \sqrt{3}$$
Also, arc length is:
$$\sqrt{1 + \frac{dy}{dx}^2}$$
and $\frac{dy}{dx} = \frac{1}{2} x^{\frac{1}{2}}$ and so $\frac{dy^2}{dx} = \frac{1}{4}x$
So I have this SA equation:
$$SA = 2 \pi \int_0^{8 \sqrt{3}} x \sqrt{1 + \frac{1}{4}x}dx$$
$$ = 2 \pi \int_0^{8 \sqrt{3}} x \sqrt{\frac{4 + x}{4}x}dx $$
taking out the $\sqrt{4}$ from the denominator:
$$ = \pi \int_0^{8 \sqrt{3}} x \sqrt{4 + x} $$
Then I get $u = x + 4$ and $ x = u - 4$
(this is odd... I guess this is another way to usub?)
new limits of integration are:
when $x = 0$, $u = 0$
when $x = 8 \sqrt{3}$, $u = 8 \sqrt{3} + 4$
$$ = \pi \int_0^{8 \sqrt{3} + 4} \sqrt{u} (u -4) du$$
$$ = \pi \int_0^{8 \sqrt{3} + 4} u^{\frac{3}{2}} - 4 \sqrt{u} du$$
$$ = \pi * \frac{2}{5}u^{\frac{5}{2}} - \frac{8}{3} u^{\frac{3}{2}} \biggr]_0^{8 \sqrt{3} + 4}$$
This comes out to be pretty ugly... did I make a mistake?
Comment: OP: Could you specify if you are integrating with respect to $x$ or $y$? The $\mathrm{d}x$ or $\mathrm{d}y$ is missing from your initial integrals. As an example: $S=\int_0^{12}x\sqrt(1 + \frac{1}{4}x)\mathrm{d}x$
Answer: Yes, you did make a mistake in the very beginning, when you kinda decided to switch to integration with respect to $y$ … but you didn't actually switch. Note that your integral
$$2\pi\int_0^{8\sqrt{3}}x\sqrt{1+\frac{1}{4}x}\,dx$$
is with respect to $x$, but you're using the $y$-values from $0\le y\le8\sqrt{3}$ as your limits of integration. That doesn't make sense — effectively you're saying that $x$ ranges from $0$ to $8\sqrt{3}$, which isn't true.
Once you decided that

I'm going to change the limits of integration to use $y$,

you should actually do that. From the given equation you express $x$ as a function of $y$, $x=\ldots\text{something in terms of }y\ldots$, and then the arclength will be $ds=\sqrt{1+\left(\frac{dx}{dy}\right)^2}$, which you will use for setting up an integral with respect to $dy$.


Pegar valor de 2 select , e exibir os dados de cada um

Question: Estou montando uma pagina onde a pessoa conseguira comparar 2 produtos.
Tenho 2 tabelas, categorias e produtos.
Os 1º comboBox, fazem um select na tabela de categorias. Ao escolher uma categoria, nisso, chama um arquivo que vê quais produtos pertencem aquela categoria.
Assim ao escolher 2 produtos no 2º combobox(sublist) e apertar o botão teria que ser realizado um select no banco, pegando os 2 id's de cada sublist e exibir os dados deles abaixo.
Basicamente funcionaria assim:
<combo01> ComboBox 01</combo>
<combo02> ComboBox 02 </combo>
<combo01> sublist 01</combo>
<combo02> sublist 02</combo> //o conteúdo das sublists altera de acordo com o que foi selecionado no comboBox
<btn>Comparar</btn> //ao clicar em comparar, faria um select no banco com os 2 id de cada sublist, e mostraria os dados abaixo.

Vou colocar o link dos códigos aqui, se alguém puder me ajudar a resolver este problema, ficaria muito agradecido
https://www.dropbox.com/sh/7eupvel35rli2qg/AADJBHRFFprKS1lQNJ06ZcKca?dl=0
Comment: Cara, o `ComboBox 01` e o `ComboBox 02` são para escolher categorias? porque se sim, tu vai poder comparar categorias de produtos diferentes, o ideal seria tu ter apenas um combobox de categoria, e depois que pegar o produto fazer dois selects para usar na comparação.
Comment: Boa tarde Erlon ,Desculpe responder agora , fiquei sem internet ontem. O problema de fazer 1 comboBox só , é que se a pessoa quiser fazer pesquisa de Marcas/categorias diferentes , não daria certo .
Comment: Tudo depende, de para que ele utilizara
Answer: Eu faria com ajax, passando os IDs dos produtos selecionados nas listas;
function buscaProduto(callback, id) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "endereco/funcao", //substitua pela página em php que acessa o banco e retorna os dados
        data: "{'cdProduto':'" + id + "'}", //vamos supor que você tenha uma função na página acima que espere um parametro com o nome cdProduto
        dataType: "json",
        success: function (json) {
            callback(json);
        },
        error: function (XMLHttpRequest, status, error) {
            console.log(XMLHttpRequest);
            console.log(status);
            console.log(error);
        }
    });
}

Chamando a função acima para o produto 01:
buscaProduto( function (json) {
    carregaProduto($.parseJSON(json.d), divProduto01);
}, [id do produto selecionado na lista01]);

e para o produto 02:
buscaProduto( function (json) {
    carregaProduto($.parseJSON(json.d), divProduto02);
}, [id do produto selecionado na lista02]);

Exibindo os dados que retornaram do banco:
function carregaProduto(dados, div) {
   if (dados != null) {
      aprensenta os dados na div informada!
   } else {
      aprensenta mensagem de produto não encontrado!
   }
}

Você pode fazer uma nova função para executar as duas buscaProduto() no evento onClick do botão assim:
function btnClick() {
    buscaProduto( function (json) {
        carregaProduto($.parseJSON(json.d), $("#divProduto01");
    }, $("#tv_assinatura").val());

    buscaProduto( function (json) {
        carregaProduto($.parseJSON(json.d), $("#divProduto02"));
    }, $("#tv_assinatura02").val());
}

Obs: note que estou apenas mostrando uma maneira de fazer e não te dando um código pronto, para você copiar e colar no seu projeto.
Comment: boa tarde Dil_oliveira , onde eu colocaria esse script ajax na pagina ? Você poderia demostrar no meu codigo  , não manjo muito de ajax .Obrigado desde Já
Comment: Cara, arquivos com dados confidenciais como nome de usuário e senha, não devem ser disponibilizados publicamente, alguém com má intenção pode fazer um estrago. Sugiro trocar a senha imediatamente.
Comment: Vish , vlw cara , juro que não tinha reparado que as config para acessar o servidor estavam no arquivo , muito obrigado. Tentei chamar sua função , mas não esta dando certo , pelo que vi na net , seria chama-la no botão , assim :  ? . Mais uma vez , grato
Comment: Boa tarde Dill , Primeiramente,muito obrigado pela atenção em ajudar a resolver meu problema.Fiz o script php que pegara e exbira os dados,voce pode testa-lo aqui http://www.script-beta.esy.es/functions/call_pacotes.php. O problema é que , o botão que deveria comparar os dados,não esta executando nenhuma ação.Erro : Uncaught ReferenceError: carregaProduto is not defined(index):100 onclick. Vou colocar todo o codigo aqui , voce poderia me ajudar a ver onde estou errando ? Mais uma vez grato : https://www.dropbox.com/sh/7eupvel35rli2qg/AADJBHRFFprKS1lQNJ06ZcKca?dl=0


How to convert text box text from english to tamil using c# windows application

Question: In my windows application i wish to convert the text box text from english language to tamil language which tools may be required to done this  thanks in advance
Answer: If you want to support localization in your WinForm application the best and faster way to accomplish it is use resx files. You can find a lot of walkthrough and guides in MSDN pages.
If your text is static inside your textbox, you can use directly the tools in VS designer mode: set the Localizable property of your form to true, then select a language different from (Default) in Language property of your form; now if you select your textbox and put the localized string, this will be stored in a different resx file, autogenerated from Visual Studio to support the language you selected.
Once you build your application, satellite assemblies will be generated in subfoders (such as en-US or similar), remember to take these subfolders with your executable.
(If you're trying to translate a text the user put inside your textbox, I don't know how to help you)


How do I get an older asp.net project to load in VS2017?

Question: I have a few older asp.net projects. These mostly don't seem to open in VS2017. Instead they generate the following error in the output window:

C:\my\project\path.csproj : error  : Invalid URI: The format of the URI could not be determined.

This seems to affect most web projects: asp.net forms and Web API alike. It doesn't seem to affect any other project types. However, I do have at least one project that does load up in VS2017 just fine. I'm not seeing much difference between loading and non-loading projects.
Is there a way to make these projects load successfully in both VS2015 and VS2017? If so what do I need to change?
Answer: The problem is caused, at least in my case, by the MSBuildExtensionsPath32 setting. This is a setting to change the path of some of the files used by msbuild - it is especially common to see it used for web projects.
Not loading in VS2017:
<code>  <PropertyGroup>
    <MSBuildExtensionsPath32>.\webMsbuild\</MSBuildExtensionsPath32>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
</code>
Loading successfully:
<code>  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
</code>


What does "up to proportionality" mean exactly?

Question: Using the "site:"-specific Google search, I have searched both MO and math.SE for the word, "proportionality," and have noticed the phrase "up to proportionality" in many posts.
What does "up to proportionality" mean exactly? A link to its source would be nice.
Comment: For example, a certain constant is so (i.e. constant) "up to proportionalty" because its value may change if we change the units of measure involved.
Comment: "There is, up to proportionality, a unique triangle with all edges the same length." Something like that?
Comment: [Related question](https://math.stackexchange.com/questions/430543/how-do-i-understand-the-meaning-of-the-phrase-up-to-in-mathematics)
Answer: Depends on context. It usually means that the answer of a problem is only well-defined up to a constant. For example when you compute an eigenvector of matrix, the vector $v$ is equivalent with the vector $5\cdot v$, so one might write a sentence like "the eigenvector is $\dots$ up to proportionality".
Answer: "Up to" is usually used when two things only differ in superficial or removable ways relative to the focus of the discussion/proof/etc. Indefinite integrals are the same up to a constant. Lines are the same up to a choice of basis. You can find it on wikipedia's jargon page. Also it has its own page.
Comment: The jargon page link is very limited in what it says about "up to". I would link the page that wikipedia has [explicitly on this phrase](https://en.wikipedia.org/wiki/Up_to).


Guitar strings feel electrified

Question: When my electric guitar is connected to the amp it's fine but when I connect it to my mult fx Zoom g1on and then to the amp, the strings and the bridge feel electrified. Like there's friction when I move my fingers through the strings.
I understand that this might be a ground problem but the thing is the adapter of my Zoom g1on is 2 pronged so it dosen't have a ground prong.
My amp has 3 prongs though.
What do i do?
Comment: What's really fun is when you are playing with electrified guitar strings and your face touches the metal mic screen. Ouch!
Comment: At a minimum, go to a home improvement store and get an "electrical outlet tester" or "socket tester" (US version might look like https://www.homedepot.com/p/Commercial-Electric-Outlet-Tester-OT-112R/206029154 ). You should also try running the fx unit on batteries to potentially eliminate the fx unit power supply/wall connection as a problem source.
Comment: My first guess (speaking as an electrical engineer) is that the power adapter for the Zoom may have "leakage" (meaning the adapter should be replaced).  But one would think that if this is the case it would cause bad hum, so I'm not certain that's the problem.  It actually could be that the Zoom unit is "isolating" the guitar in some way, such that some other malfunction becomes apparent (though it's not clear what this malfunction might be).
Comment: I used to face a similar problem, it happened when I connected to the amp itself. I later found out that the power socket had earthing problems. So check if your multi fx has some kind of problem with electricity.
Comment: How do i do that at home?
Comment: No idea about how the testing might go, You may wait for more responses, edit the question to ask also about how to check for electricity problems in your Multi FX and/or can also try asking about checking electricity problems in it in a DIY Repair forum or a Physics/Electrical Engineering forum with the User Manual of you multifx attached (User manual is likely to have the the inner circuit of the multi fx which they may useful for them to find out where to check and how so that they can tell you the same).
Comment: This happened to me with my first amp, a Line 6 Spider II. Plugged directly into the amp, through pedals, humbuckers, single coil, at home, at college, friend's house - it didn't matter. There was this low, scratchy "tingle" through my picking hand. Since you traced it back to your multi fx, you could check to see if it's still under warranty and contact the manufacturer. Otherwise, you could try to repair it or find someone who can.
Comment: @JohnWu - the really fun bit is *holding* a mic, and *holding* guitar strings simultaneously. The electricity contracts muscles, meaning you'd like to let go, but it's difficult. Not many live to tell that tale.
Comment: yes my guitar has an annoying hum as well when connected through zoom. i thought that was normal for single coil pickups. Is it not then?
Comment: Stop taking that risk and return that pedal.
Answer: STOP PLAYING
You should not feel any electricity when playing guitar. There could be many reasons, including electrical equipment malfunction or incorrect electric installation in your room. There were accidents of electric shock from music equipment, including fatal ones.
Frequent causes are malfunction of the amplifier or ground mismatch between two electrical outlets. But it might be something else. If you don't know how to diagnose the issue safely, let a qualified electrician check it.
Comment: I actually worked a couple of years repairing guitar amplifiers (mostly, and other equipment). Basically every 5th amp had a slightly dangerous flaw, and every 30th or so a really dangerous flaw.
Answer: This could be a lethal problem. It's just not safe to continue.
Firstly, check the socket you usually use. Socket testers are very cheap, and worth having. When I was gigging several times a week, the first thing I would do is check the stage electrics. Several times, we had to use sockets which were not the convenient ones on stage - they were faulty - things like no earth are potentially a lethal problem.
If the socket is good, don't bother plugging in somewhere else. Take all the equipment - guitar, leads, fx and amp to someone who can check earth continuity etc. out properly.
If any mains plugs are not of the moulded type, open them up and check the connections - that was one big contributor to the deaths of several guitarists in the past: wires coming loose inside, and touching a wrong terminal. Didn't always blow a fuse, either.
Answer: Do not touch a microphone/stand or any grounded equipment or go anywhere wet - you may die.
Incidentally I had a similar sensation with my last mobile phone (a Huwawei). When it was plugged into the charger it would create a weird 'electric' feeling if I rubbed my fingers over the casing. I had it for the duration of my contract and I didn't die but I stopped touching it when it was plugged in and I made sure I didn't touch anything conductive.
Note: Working with any electrical gear it's always wise to carry a pen-sized voltage detector. These are cheap and may save you a shock or two. Make sure you follow the instructions.
Comment: @Lelouche Lamperouge the Detector lights up only above 110/220 Volt depending on where you are. Depending on your condition Voltages above 40 Volt may be dangerous so you are not safe. Get a cheap Volt-Meter and measure the Voltage both AC and DC. If it's above 40 throw out the amp. If it's below it may only grill other equipment.
Comment: @Lelouche Lamperouge - I don't consider myself qualified to answer this. May I suggest you make a scientific question based on this and submit it here ---https://electronics.stackexchange.com/ The people there are presumably dealing with this sort of thing as part of their jobs.   Simply ask what the sensation might be caused by. Is it a voltage effect or something else?. You should refer back to the question here and make your new one sufficiently different so that they aren't precise duplicates.
Comment: I have a similar voltage detector. And when i checked the strings with it, it didnt light up. so that means its ok??

Guitar strings feel electrified


Views: Create a block with terms marked (reference field) by the term viewing

Question: I am having two vocabularies, for example, styles and artists.
Each of the artists term has a term reference field to styles voc.
When one of the styles term page is open i want to show all terms from artists vocabulary marked with this term.
Can't figure out why my contextual filter with term id doesn't work. Could you please help me to achieve it?
Answer: 2 steps are needed to achieve this with Views:
Relationship:

Add a relationship
1st screen on the popup: Choose Taxonomy term: Styles
2nd Screen on the popup: Choose Require this relationship
Apply

Contextual Filter:

Add a contextual filter
1st screen on the popup: Choose Taxonomy term: Term ID
On the Relationship dropdown choose the relationship you configured above
Choose When the filter value is not available -> Provide default value -> Type dropdown = Taxonomy term ID from URL
Apply

And you're done.
Comment: Great! That works! My mistake was not setting Relationship proper - i choose taxonomy term using Styles instead of taxonomy term Styles. If there'll be no better answer in 7 hours i'll be able to award my bounty.
Comment: Glad to help :)
Answer: If you simply add a block in your page, it won't work, as blocks don't receive arguments. There are several ways to bypass that :

Use Panels and re-build your page in a taxonomy/term/% page
Use Views URL Path Arguments
Trick your contextual argument by using custom php code in which you'll search your tid by using current_path() that should return taxonomy/term/[%tid].
Comment: Thanks for your answer, Gregory. But i don't want to use such weighty module as Panels for one little block.
Views URL Path Arguments - is made for Nodes.
I was also not successful in tricking the argument by custom code(


Number of kernels in a renderscript file

Question: In renderscript kernels has to be defined in separate .rs files as follows:
float __attribute__((kernel)) foo_1(uint32_t x) {
}

float __attribute__((kernel)) foo_2(uint32_t x) {
}

Then it has to be used in the main java file as follows:
ScriptC_fileName tmp = new ScriptC_fileName(rs);

I was wondering which of the following is the best programming practice for performance and why.

Defining all kernels in the same .rs file.
Using separate .rs files for different kernels.
Answer: It's really a matter of preference and architecture.  Separation of concerns: if the RS functionality is not related then you likely don't want to keep it all in one .rs file.
When you create the ScriptC instance it is creating the Java side object as well as loading the native / platform specific code for the component.  So if you have everything in a single .rs file you could be loading content which is not used/required at that time.  So larger memory consumption and possible native/platform resource consumption.
Processing performance it (generally) should have no major impact, other than the larger resource usage I already described.  Of course, that will be hardware and implementation dependent.  If the hardware specifics take a processing performance impact because of more low level code being loaded (on a DSP or GPU) then that is a platform specific issue.
Comment: That's interesting. I use in my app one rs instance and several (10) ScriptC instances. All of them are created in the onCreate method of mainActivity and are later used more or less sequentially. That clearly has some benefits, since creation of these objects (particularly the rs itself) is time consuming. Now, since I have added more and more kernels, I noted that on some lower-resource devices (as e.g. LG Fino) this starts to create (memory) problems. Thus, while I will keep one rs instance, I will create some of the ScriptC instances on an as-needed basis and then destroy them again.


Why the name "kernel" in stats and ML?

Question: This has been asked on other SE sites in the context of operating systems and linear algebra, but the same question bugs me regarding kernel methods used in statistics and machine learning.
Often it is said that kernels, e.g. in kernel density estimation or SVMs, represent some kind of similarity, but I don't get where the name 'kernel' comes from and what is the symbolism of it.
So, what's the etymology of kernels, in the context of statistics and machine learning?
To be clear, I'm well aware what a kernel is and the main properties of it, I'm just curious about the name, as I don't see what it has to do with the seed or core as it's mostly defined in dictionaries. Or at least I can't see a deeper meaning than 'the essential part of the method'.
Answer: You can find some information on this matter by checking the website for earliest known uses for some of the words of mathematics maintained by mathematician Jeff Miller.  You can also find information on the etymology of the word "kernel" in standard dictionary sources.
Writing in French, Fredholm (1903) used the word "noyau" (core) and Hilbert (1904) adopted this term but wrote in German, yielding the German word "kern" (core).  These terms were used in the context of writing about integral equations in functional analysis.  Shortly after this, Bôcher (1909), writing in English, uses the term "kernel" to refer to the same objects.  The term then spreads through the English-language literature on functional analysis, Fourier analysis, and later, probability and statistics.
According to the above-linked dictionary, this word derives from Old-English and Proto-Germanic.  It can refer either to a seed, or to the core, center or essence of an object.  The linguistic similarity between the German "kern" and "kernel" appears to be due to similar historical derivations.  The word "kernel" is alleged to have derived from a hypothesised (reconstructed) Proto-Germanic word "kurną" (corn).  So, based on this history, it seems that etymologically, the word "kernel" refers to a seed, core or essence, and is based on the Anglicisation of an old German word for corn.
UPDATE: This answer has been edited heavily to reflect new information that was brought to my attention by users cbeleites and R.M.  I initially thought this may have been a recent Anglicisation of the word "kern", but the dictionary sources suggest that the word "kernel" in English is very old.  I am not a linguist, so I am merely setting out information from the above sources.
Comment: I copied and pasted from the Wikipedia listing.
Comment: My real point was I knew Bochner was a giant in kernels, but never heard of Bocher.. Bochner was the thesis advisor of Samuel Karlin, from whom I took a course in Total Positivity (totally positive kernels inducing variation diminishing transformations, and all that jazz).
Comment: I'm not sure whether kernel is an Anglicised version of Kern, at least not in the sense of something that happened only a bit more than 100 years ago. I always took it as a translation: https://www.merriam-webster.com/dictionary/kernel definition 1 is called "Kern" in German.   E.g. for an apple, the seeds = Kerne, whereas core = Kerngehäuse (literally kernel housing).
Comment: Never heard of Maxime Bôcher. Ar first, I thought maybe it was a typo for Salomon Bochner, who some years later was very big (a giant) in integral kernels, among other things.
Comment: That is my fault - I'm not sure how to type the accented o.  How did you do it?
Comment: Boy, don't I feel stupid!  Anyway, I've edited the post to add the accent to his name.
Comment: Edited - feel free to edit to fix spelling errors.
Comment: @cbeleites I agree. I think the sense of *Kern* here is less of "core" and more of "seed". In that sense, "kernel" is an accurate translation: "kernel of corn", "walnut kernel", "kernel of an idea". I don't know French all that well, but [Wiktionary](https://en.wiktionary.org/wiki/noyau) indicates *noyau* shares that sense; it's the stone (seed) of a stone fruit, e.g. a nut of an almond - the word literally comes from the Latin for nut. -- Incidentally, "nucleus" also comes from the Latin for nut, so "core" is not a bad translation. I just think "seed" makes more sense in this context.
Answer: A kernel is used a multitude of times in Machine Learning and statistics. A few examples are:

In Support Vector Machines a kernel is a function that maps the data to a higher-dimensional space where the problem becomes linearly solvable (watch this).
Kernel Density Estimation

Kernel Density Estimation is a non-parametric way to estimate the probability density function of a random variable.

In this context a kernel is simply a weighting function used in kernel density estimation. The last link also has a few more uses of the word kernel in statistics.

In Convolutional Neural Networks, a kernel is a small matrix that is used to perform the convolution between the image and itself. See here.

In all these examples a kernel is a mathematical function that is used for some sort of transformation on your data. The kernel essentially is the constant part of that transformation. Depending on the choice of kernel we use for each transformation we might get a different effect from it.
Comment: OP is looking for the etymology of kernels.
Comment: Two more examples I've run across: with regards to probability density functions, the "kernel" can refer to a function that is proportional to the density, i.e. for $\beta e^{-\beta x}$, $e^{-\beta x}$ could be considered the kernel. Second, in numerical analysis, the "kernel" can refer to a core function that needs to computed several times during an iterative algorithm.
Comment: Does the use of "kernal" to refer to the nullspace in linear algebra have any relation to any of these usages in machine learning?

Why the name "kernel" in stats and ML?


Conditional Poisson with Same Variable

Question: Let $T$ be the amount of time needed for two cars to show up at a stop sign. The average rate of car showing up is 1 car per 10 minutes. Given that $T>5$, what is the probability of $T>15$?
I'm trying to do this problem with conditional probability but am tempted to think that I should just calculate for $P(T>15)$ but then that doesn't take into account of the conditional probability. If I do Baye's rule: $P(T>15,T>5)/ P(T>5)$ then the $P(T>15,T>5)$ doesn't make much sense either (Probability of T>15 and T>5). Does the top $P(T>15,T>5)$ collapse into $P(T>15)$? Or should I parse the timeline?
Answer: The random variable $N$ that counts the number of cars is a Poisson process with parameter $\lambda=0.1$, i.e. the number of events in any interval of length $t$ is a Poisson variable with mean $\lambda t$. Therefore, the random variable $T$, which is the time difference between the passages of two cars, follows an exponential distribution with parameter $\lambda$, i.e.
$$ f_\lambda(x)=\lambda e^{-\lambda  x} \hspace{1mm} for \hspace{1mm} x>0. $$
As explained in the dedicated Wikipedia page, the variable $T$ has the memoryless property, meaning that 'the existence of one point existing in a finite interval does not affect the probability (distribution) of other points existing'. See https://en.wikipedia.org/wiki/Exponential_distribution.
Consequently, $$ P(T>15|T>5)=P(T>10) = \int_{10}^{+\infty} \lambda e^{-\lambda  x} dx =
e^{-10 \lambda}=e^{-1}$$

Conditional Poisson with Same Variable


String.intern() shows strange result, how does it work

Question: I know that String.intern() adds string to pool if it does not contain the object, but how to explain the results.
The code below:
public static void main(String[] args) {

        char[] abc = new char[]{'a','b','c'};
        String str = new String(abc);
        System.out.println(str == "abc");
        str.intern();
        System.out.println(str == "abc");
    }

output is :
false
false
However when code as below:
public static void main(String[] args) {

        char[] abc = new char[]{'a','b','c'};
        String str = new String(abc);
        str.intern();
        System.out.println(str == "abc");
    }

The output is:
true
What's the difference.
Answer: The difference is that when you use the String literal "abc" before the explicit call to intern, it is implicitly interned.
str.intern() doesn't store the instance referenced by str in the String pool if an equal String is already in the pool.
First snippet:
System.out.println(str == "abc"); // "abc" is interned
str.intern(); // str is not stored in the pool
System.out.println(str == "abc"); // returns false

Second snippet:
str.intern(); // str is stored in the pool
System.out.println(str == "abc"); // returns true

From the Javadoc of intern:

String java.lang.String.intern()
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned...
All literal strings and string-valued constant expressions are interned.
Comment: This difference has been used in [this answer](https://stackoverflow.com/a/44929935/2711488) to detect whether a string already exists in the pool resp. when a string literal is resolved to a string object at runtime.


Copying part of a struct using memcpy and offsetof

Question: I'd like to copy part of a struct starting from a certain element forward by combining the offsetof macro and memcpy, as shown below:
#include <stdio.h>
#include <string.h>
#include <stddef.h>

struct test {

  int x, y, z;
};

int main() {

  struct test a = { 1, 2, 3 };
  struct test b = { 4, 5, 6 };

  const size_t yOffset = offsetof(struct test, y);

  memcpy(&b + yOffset, &a + yOffset, sizeof(struct test) - yOffset);

  printf("%d ", b.x);
  printf("%d ", b.y);
  printf("%d", b.z);

  return 0;
}

I expected this to output 4 2 3 but it actually outputs 4 5 6 as if no copying had taken place. What did I get wrong?
Comment: Remember that for any pointer or array `p` and index `i`, the expression `*(p + i)` is equal to `p[i]`. From that follows that `&b + yOffset` is actually equal to `(&b)[yOffset]`. This means you're going way out of bounds of your structures. You must use "byte" pointers instead.
Comment: Regarding the "problem" and the solution you want, what is the *actual* problem? Why do you need to copy only part of the structures? Why do you think that is the best (or only) solution to your actual underlying problem?
Comment: @Someprogrammerdude I'm reading Axel-Tobias Schreiner's Object Oriented C, he does something like this for its vtable implementation but I couldn't get it to work. He actually shows the casting in his book but honestly I naively ignored it, thinking it was some relic of some ancient version of C. My bad.
Comment: Please explain your understanding/intention of each part of `memcpy(&b + yOffset, &a + yOffset, sizeof(struct test) - yOffset);`.
Comment: I think Some is implying that we are looking at a https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
Comment: You might need the offset to calculate length to copy. But you can simply use `&b.y` to get address of that member.
Comment: @Gerhardh it is in somewhat grey area...
Answer: You're doing pointer arithmetic on a pointer of wrong type, and were writing to some random memory on stack.
Since you want to calculate byte offsets, you must use a pointer to a character type. So for example
memcpy((char *)&b + yOffset,
       (const char *)&a + yOffset,
       sizeof(struct test) - yOffset);


Otimizando colunas de tabelas MySQL que recebem apenas dois valores diferentes

Question: Possuo uma tabela em MySQL com 30 colunas, sendo que 25 podem receber apenas um dos seguintes valores: Não ou Sim. Atualmente elas estão definidas como char(3).
Caso eu converta essas colunas de char para tinyint, recebendo os respectivos valores 0 ou 1, minhas queries se tornarão mais otimizadas? Enfim, qual seria o tipo de dados mais recomendado para esse caso?
Comment: https://pt.stackoverflow.com/questions/108842/bit1-versus-tinyint1-para-valores-booleanos
Answer:
Caso eu converta essas colunas de char para tinyint, recebendo os respectivos valores 0 ou 1, minhas queries se tornarão mais otimizadas?

As queries não serão mais otimizadas porque, de forma geral, o tipo não cria otimizações, ainda mais se a comparação é com  tipos numéricos diferentes sendo usados. Claro que pode haver um pequeno ganho geral, não pela query, mas porque ocupando menos espaço haverá mais dados no cache e a carga pode se tornar mais eficiente em alguns cenários. Não é a query que fica melhor, e o ganho depende de questões que você não tem controle direto.
Só pra deixar claro, a escolha do tipo certo pode influenciar outros fatores, como o citado acima, e pode até dar o resultado certo ou errado. Não importa para esta questão, mas o que influencia o funcionamento do banco de dados é extremamente complexo e nem sempre fácil de prever, por isso o certo é sempre medir e não confiar em receitas de bolo que dizem o que é mais rápido (claro que medir corretamente é bem difícil, quem não se compromete a aprender tudo com profundidade terá que se contentar com a sorte).

Enfim, qual seria o tipo de dados mais recomendado para esse caso?

Me parece ser esse mesmo, independente de otimização, procure usar o que for mais semanticamente adequado, em geral fará pouca diferença, mas de fato não parece ser interessante ter 3 caracteres para algo que só pode ser 2 valores com uma semântica booleana.
Pode tentar o tipo BIT também, em alguns casos pode dar um outro ganho, este parece ser um caso, mas para ter ganho é preciso saber usá-lo corretamente, o que não é tão simples e pode complicar a aplicação à toa se não precisa da otimização. Precisa colocar todos esses dados na mesma coluna, se usar uma coluna para cada dado o ganho não acontecerá.
Também poderia usar o tipo BOOL só para dar mais semântica, ele é só um apelido para TINYINT.
Comment: https://pt.stackoverflow.com/questions/108842/bit1-versus-tinyint1-para-valores-booleanos
Comment: Obrigado a todos pelas informações.


i have open pdf file in php and how can secure it ? no one can neither download nor print it

Question: I run PDF file through localhost in php there is a option in browser of printing the PDF and I dont want it to be print.
Here is my code:
<code>header('Content-type: application/pdf');
readfile('The C# 4.0 complete reference.pdf');
die();
</code>
kindly help me
Comment: It is very unclear what you want to do exactly. Where is the file stored?
Answer: try this code
<code>   <?php
    header("Content-type:application/pdf");

    // It will be called downloaded.pdf
    header("Content-Disposition:attachment;filename='downloaded.pdf'");

    // The PDF source is in original.pdf
    readfile("original.pdf");
    ?>
</code>
Comment: i have try it previously but in browser on left click you get an option of print and i dont want print option for my pdf


Spark Python CSV Write

Question: I am trying to save the below file in CSV format in Spark Python.
File:
44,8602,37.19
35,5368,65.89
2,3391,40.64
47,6694,14.98
29,680,13.08
   import csv
   import StringIo
   inputa =sc.textFile("file:///home/kumar/customer-orders.csv")
   def writerecord(line):
       output=StringIO.StringIO()
       writer=csv.DictWriter(output,fieldnames["a","b","c"],extrasaction='ignore',delimiter = ',')
       for record in line:
           writer.writerow(record)
       return [output.getvalue()]
    inputa.mapPartitions(writerecord).saveAsTextFile("file:////home/godffddf2888/resultcx12"")

I get the following error:
Error process()
File "/usr/hdp/220.127.116.11-3485/spark/python/lib/pyspark.zip/pyspark/worker.py", line 106, in process
  serializer.dump_stream(func(split_index, iterator), outfile)
       File "/usr/hdp/126.96.36.199-3485/spark/python/pyspark/rdd.py", line 2355, in pipeline_func
return func(split, prev_func(split, iterator))
File "/usr/hdp/220.127.116.11-3485/spark/python/pyspark/rdd.py", line 317, in func
    return f(iterator)
 File "<stdin>", line 5, in writerecord
 File "/usr/lib64/python2.7/csv.py", line 148, in writerow
  return self.writer.writerow(self._dict_to_list(rowdict))
 File "/usr/lib64/python2.7/csv.py", line 145, in _dict_to_list
  return [rowdict.get(key, self.restval) for key in self.fieldnames]
  AttributeError: 'unicode' object has no attribute 'get'

Answer: I hope that this code does what you want:
import csv
import io
from pyspark import SparkContext

sc = SparkContext()

inputa =sc.textFile("./customer-orders.csv")

def toCSVlines(data):
    return ','.join(str(i) for i in data)
# function useful for saving data

columns_name = [["a","b","c"]]
header = sc.parallelize(columns_name) # RDD with columns name

rdd = inputa.mapPartitions(lambda x: csv.reader(x)) # read the data of your input .csv file
print (rdd.collect())
# [['44', '8602', '37.19'], ['35', '5368', '65.89'], ['2', '3391', '40.64'], ['47', '6694', '14.98'], ['29', '680', '13.08']]

rdd_tot = header.union(rdd) # create a single RDD with column names and values

print (rdd_tot.collect())
# [['a', 'b', 'c'], ['44', '8602', '37.19'], ['35', '5368', '65.89'], ['2', '3391', '40.64'], ['47', '6694', '14.98'], ['29', '680', '13.08']]

rdd_tot = rdd_tot.map(lambda line: toCSVlines(line))
rdd_tot.coalesce(1).saveAsTextFile("/whereverYouWant")
# you will have something like this
# a,b,c
# 44,8602,37.19
# 35,5368,65.89
# 2,3391,40.64
# 47,6694,14.98
# 29,680,13.08

Answer: To read csv file into a dataframe (and add columns):
df = spark.read.csv(path='my_csv_file.csv',
                    schema=['cola', 'colb', 'colc'])

You can manipulate data on that dataframe, when you're done with it you can write it back in csv format:
df.write.csv(path='output_file.csv', mode='overwrite')

Answer: You can try used flatMap() instead of using mapPartitions().

Spark Python CSV Write


ZF2 + Doctrine 2 orm:validate-schema multiple databases

Question: I've 2 entity manager orm_default for my backend and orm_powerdns for an interface with PowerDns.
if I want to validate schemas, orm:validate-schema valids only orm_default. How to validate my other schema?
Comment: did you have any luck with the http://doctrine-orm.readthedocs.org/en/latest/reference/tools.html#database-schema-generation ?
Answer: I've searched and tried multiple things, but the easiest thing I've found that works is changing the Entity Manager in the following file manually, before running the command:

.\vendor\doctrine\doctrine-orm-module\src\DoctrineORMModule\Module.php

line 141:
from:
<code>$entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
</code>
to:
<code>$entityManager = $serviceLocator->get('your_em_name');
</code>
Both the validate and create commands worked for me after that.
Comment: This works for me. Did you find a better solution for this?


Using MyFaces CODI with JBoss 7

Question: I was using JBoss 7 with JSF 2.0 (Mojarra) and Weld as CDI. Everything that JBoss already provides. Also, I added Primefaces to the package. So by now, my project is:
JBoss 7 + JSF 2.0 + Primefaces + Weld
However, I was feeling that JSF ViewScope was missing on Weld (and on CDI), so I just added MyFaces CODI to my project. But I'm feeling that I'm mixing lots of stuff right now, and I don't know what is what. So I have a few questions:
1 - Adding MyFaces CODI to the project is only CDI, or I'm changing Mojarra also?
2 - Adding MyFaces CODI also invalidate Weld, or I'm using CODI for the ViewScope and Weld for the others (Request, Application, Session, etc)? Is this a good idea?
Hope you understand my question and help me =P
Answer:
Adding MyFaces CODI to the project is only CDI, or I'm changing Mojarra also?

You're not changing Mojarra. CODI is just an extension to CDI.

Adding MyFaces CODI also invalidate Weld, or I'm using CODI for the ViewScope and Weld for the others (Request, Application, Session, etc)?

You're not changing Weld. CODI is just an extension to CDI.
Comment: Balus, thanks. About Weld and CODI, weld will be dealing with all cdi, and CODI just adds some stuff to it, is that right?

Using MyFaces CODI with JBoss 7


Prove that $ T $ is a normal transformation

Question: Let $ V $ be an finite-dimensional inner product space over $ \mathbb{C} $.
Let $ T : V \to V $ be a linear transformation and assume that for every $ v $, $ \lVert Tv \rVert =\lVert T^*v\rVert$. I want to prove that $ T $ is a normal transformation.
My effort:
Suppose, to the contrary, that $ T $ is not normal and therefore there exists a $ \vec{u} $ such that $ TT^*(\vec{u}) \neq T^*T(\vec{u}) $.
$ \lVert Tv\rVert^2= \langle Tv, Tv \rangle = \langle v, T^*Tv\rangle$
$ \lVert T^*v \rVert^2=\langle T^*v, T^*v\rangle=\langle v, TT^*v\rangle$
which means $ \lVert Tv\rVert^2 - \lVert T^*v \rVert^2==\langle v, (T^*Tv
-TT^*v) \rangle$
I now want to show that $ T^*Tv- TT^*v \not \perp u $, but I don't really know how? Could you give me a $ \textbf{hint} $?
Comment: $A=T^* T- T T^*$ is self-adjoint, so choose $u$ to be an eigenvector of $A$ and not just any $u$.
Answer: From the last equation you have derived you get that the RHS inner product is zero for all $v\in V$.
The hint is the term "Polarisation" to finish your proof,
cf wikipedia or
math SE's "Polarization identity in Hilbert space"
if you'd want to stay on this site.
Furthermore, the post
If $(Tx \mathbin{|} x) = 0$ for all $x$ then $T = 0$
is a seamless sequel of your post.
Comment: @talopl Polarization Identity is a standard result. You can find it in Wikipedia.
Comment: My linear algebra course is not in English, and so I am not familiar with the term "Polarization" $ \textbf{ in English } $. Could you, please, direct me to a page where I could read about it?
Comment: As I see no reaction may I ask  @talopl if this is a satisfactory hint/answer ?
Comment: @Hanno Unfortunately, no. I tried to expand what I got using the Polarization Identity, and yet all I got were messy calculations.
Comment: @talopl I'd propose you *first* get yourself acquainted with the polarisation identity itself, e.g. by going through the given references/links, for which you asked in your first comment. *Secondly*, you will see that it directly applies to your reasoning above as
$0=\lVert Tv\rVert^2 - \lVert T^*v \rVert^2=\langle \,v,\, (T^*T
-TT^*)v\,\rangle$ for all $v\in V$.


Interpreting multivariate tests in repeated measures

Question: I have a within subjects design with 4 DVs and I ran a repeated measures analysis on SPSS. My DV is a physiological measurement and is not expected to be linear.
My multivariate Wilks lambda is significant at p = .004 and my Mauchly test approaches significance at p = .054. However, my univariate tests of within subjects is non-significant (uncorrected  p =.07 and greenhouse Geisser p = .082)

Given that my sphericity test is almost significant and given that I do not expect linearity in the data, is it reasonable to look at my multivariate test and ignore the non-significant univariate test?
Do I even have to look at univariate if the multivariate is significant? I.e., am I justified to look at my pairwise comparisons simply because the multivariate is significant?
Comment: I understand your distaste for SPSS and concerns about fishing for results, but that is why I am asking this question - to understand the output. If your opinion is that the multivariate results table offers no valuable input and I should only look at the corrected p values in the within subjects test as a test for significance, I would like to know your reason for thinking so.
Answer: Multivariate tests for repeated-measures data are seldom, at least in published results in the experimental psychology I regularly read. Nevertheless there are recommendation for these tests. Most notably (from the top of my head):
O'Brien, R. G., & Kaiser, M. K. (1985). MANOVA method for analyzing repeated measures designs: An extensive primer. Psychological Bulletin, 97(2), 316–333. doi:10.1037/0033-2909.97.2.316
Algina, J., & Keselman, H. J. (1997). Detecting repeated measures effects with univariate and multivariate statistics. Psychological Methods, 2(2), 208–218. doi:10.1037/1082-989X.2.2.208
Keselman, H. J., Algina, J., & Kowalchuk, R. K. (2001). The analysis of repeated measures designs: A review. British Journal of Mathematical and Statistical Psychology, 54(1), 1–20. doi:10.1348/000711001159357
(I specifically liked the last one, Keselman et al., 2001)
You might also want to have a look at the multivariate statistics book by Tabachnik and Fidell.
It would be surprising if you couldn't find the recommendation for using the multivariate and ignoring the other (unfortunately nonsignificant) tests in your case in these papers.
Answer: The difference between multivariate and repeated measures tests is that both "favour" different alternatives while both maintaining (approximately) their $\alpha$-level. For "spherical data" i.e. spherical covariance matrices, this difference is rather small. So all your sphericity test tells you is that both tests may differ.
If you introduce a new procedure of picking the smallest p-value, you effectively inflate the type I error. You have to specify which procedure to take prior to analysis or even data gathering.
As Henrik already wrote, for univariate repeated measures data it is best to take repeated measures analysis since they will work for sure, whereas the multivariate tools will fail e.g. if the data become high dimensional of if by bad luck the empirical covariance matrix becomes singular.


Add a IF ...ELSE statement to stored procedure to skip duplicate primary keys

Question: I have a try catch in my physical C# code that just skips this insert process when a error accrues and continues the loop to fill the database. However this is bad coding practice.
So I would like to add an IF statement to the stored procedure below that will just skip if a primary key is already there. My primary key is @id.
How can I go about this?
CREATE PROCEDURE InsertProc
    (
    @id int,
    @to nvarchar(100),
    @from nvarchar(100),
    @subject nvarchar(100),
    @date datetime
    )
AS
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date)
    VALUES (@id, @to, @from, @subject, @date)

Answer: CREATE PROCEDURE InsertProc
    (
    @id int,
    @to nvarchar(100),
    @from nvarchar(100),
    @subject nvarchar(100),
    @date datetime
    )
    AS
    IF NOT EXISTS (SELECT NULL FROM Emails_Log
                    WHERE Email_ID = @ID)
    BEGIN
        INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date)
            VALUES (@id, @to, @from, @subject, @date)
    END

If you actually want to update record if already exists and insert if not, the pattern is as follows:
CREATE PROCEDURE InsertProc
    (
       @id int,
       @to nvarchar(100),
       @from nvarchar(100),
       @subject nvarchar(100),
       @date datetime
    )
    AS
       UPDATE Emails_Log
          SET e_To = @to,
              e_From = @from,
              e_Subject = @subject,
              e_Date = @date
        WHERE Email_ID = @ID
       -- If there was no update it means that @ID does not exist,
       -- So we proceede with insert
       IF @@ROWCOUNT = 0
       BEGIN
          INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date)
               VALUES (@id, @to, @from, @subject, @date)
       END

Answer: CREATE PROCEDURE InsertProc
    (
    @id int,
    @to nvarchar(100),
    @from nvarchar(100),
    @subject nvarchar(100),
    @date datetime
    )
    AS
    IF EXISTS(SELECT * From Emails_Log Where Email_ID = @id)
    UPDATE Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date)
            SET e_To = @to, e_From = @from, e_Subject = @subject, e_Date = @date
            WHERE Email_ID = @id
    ELSE
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date)
            VALUES (@id, @to, @from, @subject, @date)

Another way to do this would be with MERGE command.
Look at this thread on SO to know more.
Comment: set ...? would ((at)id, (at)to, (at)from, (at)subject, (at)date) go in there?
Comment: Msg 102, Level 15, State 31, Procedure InsertUpdateProc, Line 11
Incorrect syntax near '('.  : I got this error
Answer: You can check for the existence of a record in table and
insert the record in one statement.
Answer: try this code
CREATE PROCEDURE InsertProc
(
@id int,
@to nvarchar(100),
@from nvarchar(100),
@subject nvarchar(100),
@date datetime
)
AS
IF NOT EXISTS (SELECT Email_ID From Emails_Log Where Email_ID = @id)
BEGIN
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date)
        VALUES (@id, @to, @from, @subject, @date)
END


Remoção de Símbolos em colunas do dataframe em Python

Question: Feito um web scraping em  Python , até aí ok, só que gerou uma tabela com ( ) e [ ], \n, -, queria um código função em python para  usar para limpar o  dataframe da figura abaixo, pode ser coluna a coluna. , só permanecendo os nomes.
Obrigado!
Tentei:
#dataFrame.replace(to_replace='[' , value = "")
#DataFrame.replace (to_replace = None, value = None, inplace = False, limit = None, regex = False, method = 'pad')

&
def remove_str(txt):
    list_remove = [' [',' ]',' \n',' -',' ]
    for t in list_remove:
        txt = txt.strip(t)
    return txt
tempdata = pd.DataFrame(columns=['Time','Value'])
data['titulo'] = data.apply(lambda row : remove_str(row['titulo']), axis=1)

Answer: Data Frame de teste
import pandas as pd
import re

titulo = ['[Cobra Kai]', '[Bridgerton]', '[Vikings]']
genero = ['[\nAction, Comedy, Drama]', '[\nDrama, Romance]','[\nAction,Adventure, Drama]']
ano = ['[(2018)]','[(2020)]','[(2013-2020)]']

df = pd.DataFrame({'Titulo': titulo, 'Genero': genero, 'Ano':ano})

Código
simbolos = '][\n)( '
pattern = "[" + simbolos + "]"

df = df.applymap((lambda x: re.sub(pattern, '', x)))

Criando o data frame de teste
definindo os símbolos que serão excluídos
Definindo um padrão
Aplicando a função re.sub em todo o data frame

Saída
      Titulo                 Genero           Ano
0   CobraKai    Action,Comedy,Drama          2018
1   Bridgerton  Drama,Romance                2020
2   Vikings     Action,Adventure,Drama  2013-2020

Comment: Muito bom , este código funciona nesta nova tabela! Porém quando uso no meu DataFrame, não funciona. Pode aplicar este código, mas ao invés de criar um novo, para o já existente definido como dataframe (tudo minúsculo)
criar simbolos = '][\n)( '
pattern Código = "[" + simbolos + "]"

df = df.applymap((lambda x: re.sub(pattern, '', x)))
Comment: Muito bom , este código funciona nesta nova tabela! Porém quando uso no meu DataFrame, não funciona. Pode aplicar este código, mas ao invés de criar um novo, para o já existente definido como dataframe (tudo minúsculo)
Data Frame
Ou seja usar dataframe ao invés de uma nova tabela abaixo:
titulo = ['[Cobra Kai]', '[Bridgerton]', '[Vikings]']
genero = ['[\nAction, Comedy, Drama]', '[\nDrama, Romance]','[\nAction,Adventure, Drama]']
ano = ['[(2018)]','[(2020)]','[(2013-2020)]']
Comment: @MDataScience, boa tarde! Eu não entendi muito bem a colocação. Ele funciona pra você ou não funcionou? Caso não tenha funcionado, poste na pergunta o seus dados reais, dessa forma poderei testar e te ajudar de uma maneira mais adequada. Abraço!
Comment: O seu código funciona, mas no meu código não funcionou, então nao funcionou no meu caso , na tabela existente. Sim o ideal seria testar no código direto , pois tenho tentando fazer de várias formas e entender por que esta dando erro.. Sou novo por aqui , posso colocar o código lá (é para estudo mesmo) só que fica sem formatação.
Comment: @MDataScience, bom dia! Coloque seus dados em alguma hospedagem na nuvem e disponibilize o link na pergunta, dessa forma posso analisar melhor o que está havendo. Abraço!


continuity of a function involving an infimum

Question: Let $q(x,y)$ be continuous function on $\mathbb{R}^2$ and $K$ be a compact set of $\mathbb{R}$. Then, define
$$
q_K(y) \triangleq \inf_{x \in K} q(x,y).
$$
Is $q_K$ a continous function?
Answer: First, note that the inf is actually attained, by a standard argument.  I'll give a proof in the language of IST.
Fix some standard $y$, and note that $q$ is uniformly continuous on $[y-1, y+1]\times K$.  Let $y \approx y'$.  We are to show $q_K(y)\approx q_K(y')$.
Suppose $q_K(y)=q(y,x)$ and $q_K(y')=q(y',x')$. I claim that $q(y',x) \approx q(y',x')$. For if not, $q(y',x) \gg q(y',x')$ (i.e. by a non-infinitesimal amount). Then $q(y,x) \approx q(y',x) \gg q(y',x') \approx q(y,x')$ (first $\approx$ by continuity, last $\approx$ by uniform continuity which is needed as $x'$ may not be standard). That's a contradiction to $x$ realising the inf.
Now $q_K(y') = q(y',x') \approx q(y',x) \approx q(y,x) = q_K(y)$ as required.


How to configure Mongodb connection within kubernetes to connect with my nodejs app

Question: I create an nodejs app and try to connect it with mongodb on kubernetes cluster. The nodejs and mongodb app are separate pods in my cluster.
mongodb and app are running when i display the status , i can connect me to the mongodb pods and add datas
NAME                                      READY   STATUS    RESTARTS   AGE
my-backend-core-test-5d7b78c9dc-dt4bg     1/1     Running   0          31m
my-frontend-test-6868f7c7dd-b2qtm         1/1     Running   0          40h
my-mongodb-test-7d55dbff74-2m6cm          1/1     Running   0          34m

But when i try to make the connection with this script:
const urlDB = "my-mongodb-service-test.my-test.svc.cluster.local:27017";
console.log("urlDB :: ", urlDB);

mongoose.connect('mongodb://'+urlDB+'/test', { useNewUrlParser: true }).then(() => {
    console.log("DB connected")
}).catch((err)=> {
    console.log("ERROR")
})

I have the following error on my nodejs app:
> email@example.com start /usr/src/app
> node ./src/app.js

urlDB ::  my-mongodb-service-test.my-test.svc.cluster.local:27017
ERROR

As explained on kubernetes i'm suppose to communicate between the differents pods using service-name.namespace.svc.cluster.local (my-mongodb-service-test.my-test.svc.cluster.local:27017)
mongo logs show me a different host, corresponding to my pod and not the service. How can i configure the host on my yaml file ?
mongodb logs :

2019-05-24T10:57:02.367+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=my-mongodb-test-7d55dbff74-2m6cm
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] db version v4.0.9
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] modules: none
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] build environment:
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] options: { net: { bindIp: "0.0.0.0" } }
2019-05-24T10:57:02.376+0000 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten]
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=485M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-05-24T10:57:03.521+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:521941][1:0x7f2d2eeb0a80], txn-recover: Main recovery loop: starting at 2/140416 to 3/256
2019-05-24T10:57:03.719+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:719280][1:0x7f2d2eeb0a80], txn-recover: Recovering log 2 through 3
2019-05-24T10:57:03.836+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:836203][1:0x7f2d2eeb0a80], txn-recover: Recovering log 3 through 3
2019-05-24T10:57:03.896+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:896185][1:0x7f2d2eeb0a80], txn-recover: Set global recovery timestamp: 0
2019-05-24T10:57:03.924+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.984+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-05-24T10:57:03.986+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

mongodb yaml
apiVersion: v1
kind: Service
metadata:
  name: my-mongodb-service-test
  namespace: my-test
spec:
  selector:
    app: my-mongodb
    env: test
  ports:
  - port: 27017
    targetPort: 27017
    protocol: TCP

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-mongodb-test
  namespace: my-test
  labels:
    app: my-mongodb
    env: test
spec:
  selector:
    matchLabels:
      app: my-mongodb-test
  replicas: 1
  template:
    metadata:
      labels:
        app: my-mongodb-test
    spec:
      containers:
        - name: mongo
          image: mongo:4.0.9
          command:
            - mongod
            - "--bind_ip"
            - "0.0.0.0"
          imagePullPolicy: Always
          ports:
            - containerPort: 27017
              name: mongo
              hostPort: 27017
              protocol: TCP
          volumeMounts:
              - mountPath: /data/db
                name: mongodb-volume
      volumes:
        - name: mongodb-volume
          hostPath:
            path: /home/debian/mongodb

Comment: have you tried using this as the URL = "my-mongodb-service-test:27017";
Comment: Also add label in the metadata of the mongo service :
  labels:
    app: my-mongodb-service-test
Comment: could you please print the stack trace instead of "ERROR" ? It would give more idea about the issue.
Comment: No my-mongodb-service-test:27017 don't works, i try many solutions before to post. 2 possibilities to my problem the configuration of mongodb prevent the connection due to the wrong hostname see the log. how to configure on kubernetes i don't know.
Second, my pod mongo is not accessible from the nodejs pod, how to check that i don't know i just start on kubernetes
Comment: About the stack trace it's just err => true :( weird
Answer: Your service selector is mismatch with pod labels, service endpoints is empty (you can check this by kubectl describe svc/my-mongodb-service-test -n my-test), so kubernetes can not access pod by service.
Correct service selector is:
apiVersion: v1
kind: Service
metadata:
  name: my-mongodb-service-test
  namespace: my-test
spec:
  selector:
    app: my-mongodb
...

This should match pod labels specify by spec.template.metadata.labels in Deployment yaml.
Comment: Yes i just see my error(of beginer) on the dashboard, just restart nodejs and mongo pods now it's works
Comment: Excuse me.. I have a problem connecting to mongodb in kubernetes https://stackoverflow.com/questions/65870380/connect-to-mongodb-with-mongoose-both-in-kubernetes . Kindly assist