Drupal & Wordpress User Integration

Question: I know I might be asking a really dumb and likely complex matter, but it's the only remotely easy option I have at the moment.
Due to what is set up on two systems by my predecessor, wordpress and drupal both appear as CMS's that I have to manage, the one problem area apparently always having been the ability to keep users accounts in order so that if a user created an account on one site, the admin would have to go through and manually connect that account to the other site.
So my question is, after looking through everything and apparently finding very little to resolve this issue, I am coming to ask whether or not there is an option for me to connect the two (for the time being, I don't expect it to take more than a few months) in just how pull, from a table, a username and password? And just as a heads up, I am converting the wordpress to drupal so this problem will be solved in the near future, I just need this as a temporary solution...
I'd greatly appreciate any help.
Comment: You'd have to use a hook like hook_user_insert and or hook_user_update to keep the data flowing to Wordpress.
Comment: + that is to say, in conjunction with a Wordpress API so you can communicate to the other system.
Comment: Can you 1) Import your users from WP to Drupal using the migrate module
2) discontinue allowing users to register on WP, and 3) Use the Services module to create a REST endpoint where WP can auth users against Drupal? If that works for you I can provide a more detailed answer
Comment: @longboardnode - yes, I can do all three. I just would like some option and it sounds like I could readily implement your solution. If you could provide more information, that would be great.
Answer: This is a rough sketch based on some other projects I've been involved in but the WP part is a bit uncertain. FWIW here it is:
1) install services, rest server and an auth module (like oauth or api_key) the on drupal and create an endpoint with resources for the user functions you need. Find a great tutorial by here. Essentially you should be able to push a php call with the json to your endpoint (Chrome's postman is a great tool to test this with) and get a 200 response if user was logged in or 401 unauthorized if not.
2) pause WP from processing user tasks on its side
3) export legacy WP users to csv and import users into Drupal using the user import module
4) Now you should be able to set ping your drupal from WP, get a reply back, set a session var if logged in and serve content on WP as if they logged in.
Granted, this is not an ideal setup but hopefully will carry you through the few months until you convert WP to Drupal.
Comment: Helpful answer. Could you pls clarify what happens when a new WP user is created, or a new Drupal user is created. Is a user going to be created in the 'other' CMS when created in the first one?

Drupal & Wordpress User Integration


SQL Server database connections being dropped intermittently

Question: Occasionally in one of our production environments (we have a dozen or so without different clients pointing to each) we get exceptions at different points in the process.   The code doesn't do anything fancy - just your basic inserts and updates.  We are however, performing multiple inserts and updates within a single connection across multiple methods.
We'll get exceptions like this:
System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)....

and this:
System.Transactions.TransactionManagerCommunicationException: Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool. ---> System.Runtime.InteropServices.COMException (0x8004D024): The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)
at System.Transactions.Oletx.IDtcProxyShimFactory.ReceiveTransaction(UInt32 propgationTokenSize, Byte[] propgationToken, IntPtr managedIdentifier, Guid& transactionIdentifier, OletxTransactionIsolationLevel& isolationLevel, ITransactionShim& transactionShim)
at System.Transactions.TransactionInterop.GetOletxTransactionFromTransmitterPropigationToken(Byte[] propagationToken)
--- End of inner exception stack trace ---
at System.Transactions.Oletx.OletxTransactionManager.ProxyException(COMException comException)

and this:
System.InvalidOperationException: Invalid attempt to call Read when reader is closed.
at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
at System.Data.SqlClient.SqlDataReader.Read()

I don't think we need to turn on MSDTC (again things work in every other environment).  Suggestions on what we can check code-wise or environment-wise?
Comment: Why the votes to close??  I think this is a code issue, not a database engine one.
Answer: What do your methods look like that are calling the Database commands?  It sounds like you have an open connection spanning across multiple blocks of logical code for a very long time.
I recommend calling SqlConnection.Open() right before you need the connection open, and SqlConnection.Close() when you are complete with the connection.  This should all be taking place in a try/catch block.
If you are doing the above method, that means the connection isn't timing out.  In which case, you need to look to the SQL Server error log to see if there are any errors being thrown that you need to look at.
Comment: thanks for the response.  We are performing multiple methods with database updates in each while keeping open the connection/transaction.  I assume the suggestion would be to go to MSDTC and I can understand the reason why, but it doesn't make sense why this is only occurring in one of our environments.
Comment: Why MSDTC??  I'd look directly towards the database for direct errors or something along those lines.  The timeout for your instance in that one environment could be shorter, causing it to happen faster.  Hard telling, but it looks like SQL Server is shutting down the connection before you want it to.
Comment: Yes, you're probably right.  There's probably something in the environment that's different - we did check the timeout setting and that looked right.  I'll check your answer since we'll probably find more info after we get the logs from production.


Blues E scale on guitar

Question: I am currently learning the guitar, particularly solo-ing, and teaching myself by figuring out the Blues scale in various keys. At the moment I'm looking at the key of E.
The way I understand it (may be incorrect) the Blues scale is based on this formula:
1 b3 4 b5 5 b7
That should mean the Blues scale in key of E becomes:
E Gb A Bb B Db
However, in a lot of places, not the least being the very highly-rated Guitarist's Reference app on Android, the scale is said to be:
E G A Bb B D
Being a newbie I'm struggling to find the reasoning behind this, and how it applies to other keys. Any pointers would be greatly appreciated.
Thanks.
Answer: That notation (1 b3 4 b5 5 b7) is used to relate a scale to the major (ionian) scale. It shows which scale degrees should be flattened or sharpened (and by how much) relative to the major scale.
So, you should start with E major scale, not E minor or E phrygian (natural notes from E to E: E F G A B C D). E major scale is of course 1=E 2=F# 3=G# 4=A 5=B 6=C# 7=D#. Because these are the notes that the famous W W H W W W H (W=whole, H=half, two frets and one fret on guitar respectively) formula produces when you start with E.
If you apply the formula 1 b3 4 b5 5 b7, you end up with E G A Bb B D, which is what you found. Because the flats in b3 and b7 cancels the sharps of G# and D#.
Comment: AHA!!! Correct formula, wrong starting point! Thank you, that cleared it up perfectly.
Answer: Surprisingly, this question hasn't spawned an answer regarding the other blues scale - the major blues. Notes constituting this are E, F#, G, G#, B, C#. In E major. Not quite as much used as the minor blues, as it essentially works only over major chords, whereas the minor blues works over major, seventh and minor chords.
Your translation of notes for the minor blues scale is skewed - they are in fact E, G, A, Bb, B, D, and can be played for just about any blues in E, but it's good to also be able to throw in those other notes previously mentioned in this answer.
Comment: It works over major chords AND seventh chords. It doesn't need the 7th to work over 7th chords (as long as you don't add a major 7th). It is often used over the I7 chord, and over the IV7 chord one could switch to the (minor) blues scale.
Comment: @MattL. - good point, although care needs to be taken with the 6th note (C# here) as it'll clash with that b7 - unless a 13th is intended.

Blues E scale on guitar


Collect submitted data in a cookie to prefill form on next visit

Question: I have a webform but it's very long and users are going to use it a lot of times so they requested to have sort of like a cookie storage that remembers submitted data on some fields. For example I have a country field country: text_field (user inputs something).
I want to collect that country input from that user so when the next time user visits this form it will already have that country input in place so the users dont need to re-type it. I thought about having a cookie to store this but it feel likes it should be done in a different way.
Suggestions?
Answer: The webform module has the options to store incomplete webforms. You can enable the option 'Show "save draft" button' (this can be found on the advanced fieldset of the webform config). I haven't completely tested it but my best guess is that it only works for authenticated users.
When the user saves the webform and comes back to the webform, the supplied values are filled and the user can continue with the webform. It's olso possible to make the webform a multistep webform. When a users is going to a next step, webform saves the previous step. This also allows the user to continue where it left off.
I'm currently using Webform 7.x-3.18 for the features described above. I'm using a multi step form. At each stap the submitted values are saved as a draft.
Answer: There could be two cases either registered user or anonymous.
If user registered then:

create a custom module let called "MYMODULE".
In MYMODULE.module file do the following:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  switch ($form_id) {
    case 'your_webform_id':
      if (user_is_logged_in()) {

        global $user;

        // Select data from table as you need.
        $query = db_select('webform_submissions', 'ws');
        $query->leftJoin('webform_submitted_data', 'wsd', 'body.entity_id = n.nid');
        $query->fields('ws', ['nid', 'uid'])
          ->fields('wsd', ['data']);
        $query->condition('ws.uid', $user->uid);

        $result = $query->execute();

        while ($record = $result->fetchAssoc()) {

          // Set your $form field #default attribute here.
        }
      }
      else {

        // If user not registered yet and have data in COOKIE.
        if (isset($_COOKIE['webform_id']['field_name'])) {

          // Set your $form field #default attribute here.
        }
      }
      break;
  }
}

/**
 * Call submit hook here.
 */
function MYMODULE_webform_id_submit($form, &$form_state) {

  if (!user_is_logged_in()) {
    $COOKIE['webform_id']['field_name'] = $form_state['values']['field_name']; // set your cookie here
  }
}

NOTE:- please note above code is just an example and not tested code. please modify as per your requirement. above code is just for explaining purpose.


How to finish "Activity A" since "Activity B"? (Finish an Activity since other)

Question: I'm trying to finish the Activity A since Activity B when it will finish too. The Activity B was launches since Actvity A.
So I've paused the Activity A. How can I do it?
Comment: check this http://stackoverflow.com/questions/7363013/android-app-doesnt-close/7363052#7363052
Answer: You may try using startActivityForResult() on the Activity A to invoke the Activity B.
And then, before calling finish() on the Activity B, you should call setResult() and call finish() on the Activity A in onActivityResult()
Example here
Comment: I have also seen people setup a broadcast and have all activities listen for it, when the receive the broadcast with their class specified they finish();
Comment: Thank you, you right!, the best to do it is using startACtivityForResult. It's was to easy.


Differences between lsmeans and difflsmeans

Question: I've created an lmer model.  The effects of one of my treatments, "Fertilizer" varies depending on whether I use lsmeans or difflsmeans from lmerTest.
Which should I trust, lsmeans or difflsmeans?
I have a split plot experiment.  Fertilizer is the whole plot, Harvest is the split plot.  Within each plot, there are 10 samples (quad).  Data collected over 4 years, which I have made ordinal.  The response is total biomass, which I have transformed.
> model <- lmer(logTotal ~ ordYear*Fertilizer*Harvest + (1|(Block:Fertilizer)) + (1|(Plot:Quad)), REML = FALSE, data = df)

> difflsmeans(model, test.eff="Fertilizer")
Differences of LSMEANS:
                              Estimate Standard Error   DF t-value Lower CI Upper CI p-value
Fertilizer none-recommended       -0.3         0.0868 16.0   -3.27  -0.4677  -0.0996   0.005 **
Fertilizer none-half              -0.2         0.0868 16.0   -2.19  -0.3742  -0.0060   0.044 *
Fertilizer none-manure            -0.1         0.0868 16.0   -0.84  -0.2572   0.1110   0.412
Fertilizer recommended-half        0.1         0.0868 16.0    1.08  -0.0905   0.2777   0.297
Fertilizer recommended-manure      0.2         0.0868 16.0    2.42   0.0264   0.3946   0.028 *
Fertilizer half-manure             0.1         0.0868 16.0    1.35  -0.0671   0.3011   0.197
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

> lsmeans(model, list(pairwise~Fertilizer))
NOTE: Results may be misleading due to involvement in interactions
$`lsmeans of Fertilizer`
 Fertilizer    lsmean         SE    df lower.CL upper.CL
 none        4.602075 0.07090356 21.33 4.454764 4.749387
 recommended 4.885723 0.07090356 21.33 4.738411 5.033034
 half        4.792150 0.07090356 21.33 4.644838 4.939462
 manure      4.675189 0.07090356 21.33 4.527877 4.822500

Results are averaged over the levels of: ordYear, Harvest
Confidence level used: 0.95

$`pairwise differences of Fertilizer`
 contrast                estimate        SE    df t.ratio p.value
 none - recommended   -0.28364706 0.1002728 21.33  -2.829  0.0455
 none - half          -0.19007464 0.1002728 21.33  -1.896  0.2595
 none - manure        -0.07311311 0.1002728 21.33  -0.729  0.8843
 recommended - half    0.09357242 0.1002728 21.33   0.933  0.7875
 recommended - manure  0.21053396 0.1002728 21.33   2.100  0.1853
 half - manure         0.11696153 0.1002728 21.33   1.166  0.6537

Results are averaged over the levels of: ordYear, Harvest
P value adjustment: tukey method for a family of 4 means

Thank you
Comment: I'm surprised to see the same df for the lsmeans as for the pairwise differences. In a balanced split-plot design, the df for comparisons will be integers. So I wonder if something went wrong somewhere and if you re-do these analyses you might get different results.
Answer: These are different tests you're performing.
LSMEANS calculates least square means for the different groups and tests whether those means differ from 0. DIFFLSMEANS uses these means and can help you to compare group means (e.g. conditions). In your case, it makes more sense to compare conditions with each other, so DIFFLSMEANS would be the test to consider. However, keep in mind that these are post-hoc tests, so first look at the summary/anova table whether you main effect of fertilizer is significant.
Answer: This makes me wonder what difflsmeans is doing. By its name, it should be differences of least-squares means, so I don't understand the distinction @Danyou is making (but perhaps overlooked the second part of the lsmeans output for the pairwise differences?). But obviously these are not doing the same thing, the difflsmeans results being very much rounder and with different degrees of freedom.
I do know that in this example, lsmeanscomputes predictions at each combination of ordYear, Fertilizer, and Harvest (assuming all 3 are factors) based on the fixed-effects part of the model. Then it averages together the predictions for each Fertilizer level, giving equal weights to each combination of the other factors. I wonder if the data are unbalanced and if difflsmeans does not give equal weight. You might experiment with the lsmeans call with different weights arguments to see if you can reproduce the difflsmeans results. For example, weights = "prop" or weights = "outer" -- or even weights = "cells" which produces the ordinary marginal means of the data.
If you ask me, I'd trust lsmeans. But I'm biased because I'm its author.
PS -- Another thing I'll note is that the lsmeans results use a Tukey adjustment (this is the defailt for pairwise comparisons, but you can change to some other adjustment), and the difflsmeans intervals and tests appear to be unadjusted for multiplicity.


Calculating residual DTW distance for a subset of the alignment (Dynamic Time Warping)

Question: My question is: "How do I find the cost for each step taken using the dtw R package?" My work is below, so if you see an easier solution please point it out!
DTW is very effective at aligning my data. Often the supplied time series contain multiple relevant features which I later consider independently.
The normalized residual distance between the full time series is computed and returned automatically. I am interested in the normalized residual distance of only a subset of the curves.  In trying to manually calculate this "subset-distance" from the cost matrix to avoid the computation required to re-align the subset regions.
library(dtw)

mat = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.01, 0.02, 0.03, 0.04, 0.06,             0.09, 0.11, 0.13, 0.16, 0.18, 0.2, 0.22, 0.24, 0.24, 0.22, 0.22,             0.22, 0.22, 0.21, 0.2, 0.19, 0.2, 0.23, 0.29, 0.34, 0.41, 0.51,             0.62, 0.73, 0.82, 0.9, 0.95, 1, 1, 1, 0.92, 0.92, 0.89, 0.89,             0.84, 0.79, 0.7, 0.53, 0.37, 0.23, 0.17, 0.13, 0.11, 0.09, 0.08,             0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.08, 0.08, 0.08, 0.09, 0.1,             0.13, 0.15, 0.19, 0.22, 0.27, 0.29, 0.34, 0.35, 0.36, 0.35, 0.38,             0.37, 0.37, 0.32, 0.3, 0.26, 0.24, 0.21, 0.19, 0.17, 0.15, 0.14,             0.12, 0.1, 0.09, 0.09, 0.08, 0.08, 0.07, 0.07, 0.07, 0.07, 0.06,             0.06, 0.06, 0.05, 0.05, 0.05, 0.05, 0.04, 0.04, 0.04, 0.04, 0.04,             0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.03, 0.04, 0.04, 0.04,             0.03, 0.03, 0.03, 0.04, 0.04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.01, 0.01, 0.02,             0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.1, 0.12, 0.12, 0.13,             0.14, 0.15, 0.17, 0.19, 0.2, 0.21, 0.22, 0.24, 0.23, 0.24, 0.26,             0.3, 0.32, 0.33, 0.35, 0.39, 0.44, 0.49, 0.55, 0.61, 0.67, 0.71,             0.76, 0.83, 0.9, 0.97, 1, 0.99, 0.86, 0.68, 0.5, 0.41, 0.33,             0.28, 0.23, 0.2, 0.17, 0.15, 0.13, 0.12, 0.1, 0.1, 0.1, 0.11,             0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.13, 0.15, 0.17, 0.18, 0.2,             0.21, 0.24, 0.25, 0.28, 0.29, 0.32, 0.35, 0.36, 0.34, 0.32, 0.3,             0.3, 0.28, 0.26, 0.23, 0.22, 0.19, 0.17, 0.15, 0.14, 0.12, 0.1,             0.09, 0.09, 0.08, 0.08, 0.07, 0.07, 0.07, 0.06, 0.06, 0.05, 0.05,             0.05, 0.05, 0.05, 0.05, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04,             0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04), .Dim = c(149L, 2L))

.
tw = dtw(mat[,1], mat[,2], keep.internals = T)

EDIT The below calculations are wrong.
tw$distance
# 2.19
tw$normalizedDistance
# 0.007

# Manually Calculated
sum(tw$costMatrix[ cbind(tw$index1, tw$index2) ])
# 219
sum(tw$costMatrix[ cbind(tw$index1, tw$index2) ]) / sum(tw$M, tw$N)
# 0.74

Above are values for the entire curve, I am looking for the distance of individual regions.  In an attempt to get this I try to recalculate the distance from the localCostMatrix.  For the case of the step pattern having equal weights it works out.
tw = dtw(mat[,1], mat[,2], keep.internals = T, step.pattern = asymmetricP05)

tw$distance
# 1.42
tw$normalizedDistance
# 0.009
sum(tw$costMatrix[ cbind(tw$index1, tw$index2) ])
# 146
sum(tw$costMatrix[ cbind(tw$index1, tw$index2) ]) / sum(tw$M, tw$N)
# 0.49

The asymetric case does not work. I think my missing link lies in the formula for distance given in the reference below.  It depends on the distance of each aligned point but also a step-weight m_phi.  How can I retrieve m_phi?

http://cran.r-project.org/web/packages/dtw/vignettes/dtw.pdf
http://irit.fr/page-perso/Julien.Pinquier/Docs/TP_MABS/res/dtw-sakoe-chiba78.pdf
plot(tw, type="threeway")

Comment: this is a complex problem ..... what do you expected in the asymetric case ?
Comment: I don't understand why this came from Stack Overflow. It seems focused on the right R code for some tasks. If this really is a statistical question asking about methods, please rewrite it as such.
Answer: I asked the question above.  I had misunderstood tw$costMatrix above.  Rather than a pairwise cost it is the total accrued cost as we traversed the path. (tw$costMatrix is $d_{phi}$ in the equation below, and thus its a sum of $d$ at every k along the path $(X,Y)$)
$d$ below is tw$localCostMatrix and is the pairwise distance between every point. In the default case above $d$ is the Euclidean distance.

Target values:
tw = dtw(mat[,1], mat[,2], keep.internals = T, step.pattern = asymmetricP05)

tw$distance
# 1.418333

tw$normalizedDistance
# 0.009519016

Correct calculations:
d.phi = tw$costMatrix[ cbind(tw$index1, tw$index2) ]

tw$distance is the total accrued distance and thus, the last value along the path.
d.phi[length(d.phi)]
# 1.418333

tw$normalizedDistance is the total accrued distance independent of path length asymmetricP05 normalizes to the length of the query.
d.phi[length(d.phi)] / tw$M
# 0.009519016

With this we can find the pre-warping distance and post-warping distance of subsets of the alignment.
i = 0:80
i = 80:120 # Indices we want distance for

d.phi.sub = tw$costMatrix[ cbind(tw$index1[i], tw$index2[i]) ]

(d.phi.sub[length(d.phi.sub)] - d.phi.sub[1])
# i.1 = 0.4383333
# i.2 = 0.79

(d.phi.sub[length(d.phi.sub)] - d.phi.sub[1]) / length(i)
# i.1 = 0.005411523
# i.2 = 0.01926829

Outstanding questions: Finding $m_{phi}$ was unnecessary for my goal because the costMatrix accounts for it but it would still be interesting.  tw$costMatrix is the sum along the path - this can't be true for any arbitrary path, so how do values that fall away from the optimal warp path get filled?  How can we retrieve the path that generated them?
Thanks everyone.
Comment: Please edit your question, rather than extend it in an answer.
Comment: Hi Nick, my goal was to find the residual distance between subsets of the alignment and I think that was achieved above.  Though if you see any mistakes please let me know.  I'd rather run into them now than later.


Is it faster to draw a series images on a Canvas with WebAssembly?

Question: I want to ask if it's faster and more memory efficient to draw a series of images into a canvas using WebAssembly/WASM?
I'm asking this since I have tested to draw series of images (animation) both in the main UI and with web workers. For few canvases (1-5 canvases) the performance is bearable, however for more canvases (like 20-25) the scenario gets a little different, rendering becomes slow motion.
Here's the code in main UI:
const videoDecoder = new Worker("videoDecoder.js");
const canvas = document.querySelector("#canvas");
const offscreen = canvas.transferControlToOffscreen();
videoDecoder.postMessage({ action: 'init', canvas: offscreen }, [offscreen] );

Here's my web worker:
onmessage = async function (e) {
    const blob = e.data;
    blob.arrayBuffer().then(arrayBuffer => {
        const uint8Array = new Uint8Array(arrayBuffer);
        for(;;;) {
           const offsetIdx = ...;
           const endIdx = ...;
           const jpegArray = uint8Array.slice(offsetIdx, endIdx);
           const blob = new Blob([jpegArray], {type: "image/jpeg"});
           drawImage(blob);
        }
    }
}

async function drawImage(blob) {
    const bmp = await createImageBitmap(blob);
    context.drawImage(bmp, 0, 0);
    bmp.close();
}

This works fine it can render the images to the browser from web worker, but as mentioned, at 1-5 canvases it is still smooth, but with more canvases the rendering in the browser gets very slow.
So I want to ask is it faster to draw a series images on a Canvas with WebAssembly? Or there will be no theoretical performance gain compared to doing it now with the  web worker approach?
Comment: webassembly has no access to DOM - a canvas is part of the DOM - so, no, it would not be possible
Comment: @jaromanda-x According to this https://stackoverflow.com/questions/42806037/modify-canvas-from-wasm and other blog articles I found online, its possible to "draw" to the canvas from WASM using shared memory.
Comment: well, try it and see for yourself
Comment: My concern is mainly in the performance aspect which before I try to "rewrite" my web worker into C++ for wasm I want to ask if anyone have experience how much performance gain vs doing it on Javascript alone--or there is none so I would like to know upfront if there's a point on taking up the task on doing  WASM at all.
Comment: Here's a demo: https://www.hellorust.com/demos/canvas/index.html
Comment: You can write in the ArrayBuffer from WASM, but you'd still have to `putImageData` from the main or Worker. From your code, the seemingly logical part that should be moved to WASM is the hidden part in `for(;;;) {
           const offsetIdx = ...;
           const endIdx = ...;`. The slowest part though is probably `drawImage()`. If you wish to pass that to WASM, you'd need to implement the Image decoder yourself. B.t.w. bypassing account's suspension by using an other account is quite a questionable practice...
Answer: WebAssembly may be faster for complex computation on a byte array, but the main JavaScript UI thread will still be doing the rendering of the ArrayBuffer to canvas.  As long as you're "reusing" ArrayBuffer instances by passing objects with the Transferable interface between main UI thread and WebWorker, you should be able to easily update the canvas performantly.
When rendering updates to multiple canvas objects, consider batching them within a requestAnimationFrame() function to minimize repainting of the pixels in the browser. This should significantly improve browser frame rate performance.
Comment: It all depends on how much computation is occurring.  I always recommend A/B testing and measurements for all performance comparisons. Theoretically, WASM should be faster for more complex algorithms. Your slow motion may result from multiple repaints on the main UI thread
Comment: So you mean, there's no significant performance gain from using Web Assembly vs web worker in this context?
Comment: In my case, all the canvases are "rendered" in the web worker: `const offscreen = canvas[i].transferControlToOffscreen();` so the code left in the main UI thread is the passing of these canvases to web workers, and of course the instantiation of web workers. So there's no `requestAnimationFrame` in the case of the receiving app which renders 20 canvases. Everything comes from the websocket as bytes and goes to the Video decoder workers.
Comment: In fact here's the complete code that renders the canvases: https://gist.github.com/questionaut/8d01108cd6152495762b021d0e93b285
Answer:
So I want to ask is it faster to draw a series images on a Canvas with WebAssembly?

WebAssembly can be faster than JavaScript, but it isn't always. It very much depends on the type of computation you are performing. Probably the greatest strength of WebAssembly is that the performance is predictable and constant. This blog post gives probably the most informative real-world performance results:
https://hacks.mozilla.org/2018/01/oxidizing-source-maps-with-rust-and-webassembly/

Or there will be no theoretical performance gain compared to doing it now with the web worker approach?

The two methods are in no way mutually exclusive! You can run WebAssembly within a Web Worker and use shared memory (similar to a SharedArrayBuffer) to allow cross-thread access of your image data. This blog post gives an example of how WebAssembly can be used to compute an algorithm across multiple worker threads:
https://blog.scottlogic.com/2019/07/15/multithreaded-webassembly.html
In response to your specifics, a video decoders (which are typically written in C++) are a very good candidate for WebAssembly. I'd suggest creating multiple web workers, each with a WebAssembly-powered decoder.


LaTex errors: unit of measure (pt inserted)

Question: I met this problem in LaTex. Although I know what is the error message meaning, I have no idea about how to solve this error in my task.
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{1.2}%
\pgfmathsetmacro{\MidNumber}{(0.8}%
\def\test#1{\ifdim#1pt>\MidNumber\textcolor{gray!70}{#1}\else{#1}\fi}
\newcommand{\ApplyGradient}[2]{\centering %
  \pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}%
  \edef\x{\noexpand\cellcolor{black!\PercentColor}}\x\test{#1}
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}p{1.1cm}<{\endcollectcell}}
\newcolumntype{S}{>{\collectcell\ApplyGradient}p{0.6cm}<{\endcollectcell}}
%
\begin{table}[!ht]
\centering
\small
% \renewcommand{\arraystretch}{0.9}
% \begin{adjustbox}{width=0.8\textwidth}
\begin{tabular}{>{\centering}p{1.3cm}|>{\centering}p{0.9cm}|R R R R R }
\multicolumn{2}{c}{testing in$\rightarrow$}& \multicolumn{1}{c} {$\ang{40}$}  & \multicolumn{1}{c} {$\ang{20}$}  & \multicolumn{1}{c} {$\ang{0}$}  & \multicolumn{1}{c} {$-\ang{20}$}  & \multicolumn{1}{c} {$-\ang{40}$} \\ \midrule
            & $\ang{40}$    & 0.52  & 0.44  & 0.27  & 0.10  & -0.01 \\
            & $\ang{20}$    & 0.47  & 0.49  & 0.35  & 0.19  & 0.07 \\
spider9     & $\ang{0}$     & 0.25  & 0.47  & 0.63  & 0.46  & 0.24 \\
            & $-\ang{20}$   & 0.05  & 0.17  & 0.35  & 0.55  & 0.54 \\
            & $-\ang{40}$   & 0.0   & 0.05  & 0.21  & 0.47  &  0.77 \\
\midrule
            & $\ang{40}$   & 0.75  & 0.69  & 0.40  & 0.17  & 0.04 \\
            & $\ang{20}$   & 0.72  & 0.82  & 0.59  & 0.30  & 0.10 \\
gecko7      & $\ang{0}$    & 0.31  & 0.65  & 1.06  & 0.96  & 0.56 \\
            & $-\ang{20}$  & 0.16  & 0.45  & 0.86  & 1.18  & 0.98 \\
            & $-\ang{40}$  & 0.03  & 0.15  & 0.40  & 0.74  & 0.96 \\
\midrule
            & $\ang{40}$   & 1.09  & 0.75  & 0.39  & 0.14  & 0.02 \\
            & $\ang{20}$   & 0.54  & 0.55  & 0.33  & 0.15  & 0.04 \\
babyA       & $\ang{0}$    & 0.29  & 0.58  & 0.88  & 0.74  & 0.42 \\
            & $-\ang{20}$  & 0.18  & 0.47  & 0.85  & 1.09  & 0.71 \\
            & $-\ang{40}$  & 0.02  & 0.11  & 0.33  & 0.65  & 0.91 \\
\bottomrule
\end{tabular}
%
\begin{tabular}{p{0.4cm} S}
& 0.0 \\
& 0.1 \\
& 0.2 \\
& 0.3 \\
& 0.4 \\
& 0.5 \\
& 0.6 \\
& 0.7 \\
& 0.8 \\
& 0.9 \\
& 1.0 \\
& 1.1 \\
& 1.2 \\
\end{tabular}
% \end{adjustbox}
\caption{Robustness of the top learned controllers for different directions in the real world.
The data shows the fitness that the top controllers of three robots in 5 directions (the second column) perform in another directions (the first row).
The fitness value in a cell is averaged over three controller and three repetitions for each controller.
The dark and light color represent the high and low fitness value respectively.}
\label{tab:robustness}
\end{table}

The table shows as the picture:

But I got a lot errors like:
Illegal unit of measure (pt inserted).

                   \protect
l.1545             & $\ang{40}$    & 0.52  &
                                             0.44  & 0.27  & 0.10  & -0.01 \
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)
! Illegal unit of measure (pt inserted).

                   \protect
l.1545 ...       & $\ang{40}$    & 0.52  & 0.44  &
                                                   0.27  & 0.10  & -0.01 \
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)
Could you give me hands to solve this problem? Thanks!
Answer: Here are some considerations:

While \MinNumber is defined to be 0, you have a number that is smaller than that in the table (-0.01).
If you're performing a test on dimensions (\ifdim <dimA><relation><dimB>) you need to make sure both <dimA> and <dimB> are dimensions. In your case you have
\ifdim#1pt>\MidNumber

and \MidNumber is clearly not a dimension. It is just a number (0.8). That's the main cause of the problem.
\ApplyGradient should only take a single argument, not two.
It's awkward to use booktabs together with vertical rules in a tabular. There's no real need for them; I've kept them in the output, but you don't need them.

\documentclass{article}

\usepackage[margin=1in]{geometry}% Just for this example
\usepackage[table]{xcolor}
\usepackage{booktabs,collcell,xfp}

\newcommand*{\ang}[2]{#1^\circ}
\newcommand*{\MinNumber}{-0.01}%
\newcommand*{\MaxNumber}{1.2}%
\newcommand*{\MidNumber}{0.8}%

\newcommand{\test}[2]{\ifdim#1pt>\MidNumber pt\textcolor{gray!70}{#1}\else #1\fi}

\newcommand{\ApplyGradient}[2]{\centering %
  \edef\x{\noexpand\cellcolor{black!\fpeval{100*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}}}\x\test{#1}
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}p{1.1cm}<{\endcollectcell}}
\newcolumntype{S}{>{\collectcell\ApplyGradient}p{0.6cm}<{\endcollectcell}}
%

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{
    >{\centering}p{1.3cm} |
    >{\centering}p{0.9cm} |
    *{5}{R}
  }
    \multicolumn{2}{c}{testing in$\rightarrow$} &
      \multicolumn{1}{c}{$\ang{40}$} &
      \multicolumn{1}{c}{$\ang{20}$} &
      \multicolumn{1}{c}{$\ang{0}$}  &
      \multicolumn{1}{c}{$-\ang{20}$} &
      \multicolumn{1}{c}{$-\ang{40}$} \\
    \midrule
                & $\ang{40}$  & 0.52 & 0.44 & 0.27 & 0.10 & -0.01 \\
                & $\ang{20}$  & 0.47 & 0.49 & 0.35 & 0.19 &  0.07 \\
    spider9     & $\ang{0}$   & 0.25 & 0.47 & 0.63 & 0.46 &  0.24 \\
                & $-\ang{20}$ & 0.05 & 0.17 & 0.35 & 0.55 &  0.54 \\
                & $-\ang{40}$ & 0.0  & 0.05 & 0.21 & 0.47 &  0.77 \\
    \midrule
                & $\ang{40}$  & 0.75 & 0.69 & 0.40 & 0.17 &  0.04 \\
                & $\ang{20}$  & 0.72 & 0.82 & 0.59 & 0.30 &  0.10 \\
    gecko7      & $\ang{0}$   & 0.31 & 0.65 & 1.06 & 0.96 &  0.56 \\
                & $-\ang{20}$ & 0.16 & 0.45 & 0.86 & 1.18 &  0.98 \\
                & $-\ang{40}$ & 0.03 & 0.15 & 0.40 & 0.74 &  0.96 \\
    \midrule
                & $\ang{40}$  & 1.09 & 0.75 & 0.39 & 0.14 &  0.02 \\
                & $\ang{20}$  & 0.54 & 0.55 & 0.33 & 0.15 &  0.04 \\
    babyA       & $\ang{0}$   & 0.29 & 0.58 & 0.88 & 0.74 &  0.42 \\
                & $-\ang{20}$ & 0.18 & 0.47 & 0.85 & 1.09 &  0.71 \\
                & $-\ang{40}$ & 0.02 & 0.11 & 0.33 & 0.65 &  0.91 \\
    \bottomrule
  \end{tabular}
  \hspace{4mm}
  \begin{tabular}{ S }
    0.0 \\ 0.1 \\ 0.2 \\ 0.3 \\ 0.4 \\ 0.5 \\
    0.6 \\ 0.7 \\ 0.8 \\ 0.9 \\ 1.0 \\ 1.1 \\
    1.2
  \end{tabular}

  \caption{Robustness of the top learned controllers for different directions in the real world.
    The data shows the fitness that the top controllers of three robots in~5 directions (the second column) perform in another directions (the first row).
    The fitness value in a cell is averaged over three controller and three repetitions for each controller.
    The dark and light color represent the high and low fitness value respectively.}
\end{table}

\end{document}

Comment: @GongjinLan: Arxiv might not have [`xfp`](//ctan.org/pkg/xfp) installed. You can revert back to your use of [`tikz`](//ctan.org/pkg/tikz) (or `pgfmath`) for performing the calculations and try again. Practice helps to solve problems, but ultimately you need to look at the source code to understand how things are put together. Spending lots of time digging through problems makes you better at solving them.
Comment: cool, Thanks a lot! How do you know so many details? Could you give me some ideas that I can fix some other latex problem like this by myself? I mean I want to learn to fix something like you rather than post it to ask help again next time.
Comment: one more issue, this is working without any errors in Overleaf. But when I submitted it to Arxiv. It has some problem with package xfp as: ```! LaTeX Error: File `xfp.sty' not found.```
Comment: nice, thanks a lot for your answer. I kind of solved this issues.


meta-analysis of p values

Question: I am a bit confused about doing the meta analysis with my data. Given z-scores I calculate p values in a way: p.values = 2*pnorm(-abs(z)). Then I would like to apply Fischer/or Stouffer methods to do the meta analysis. Those methods work with one-sided p-values, so I just need to divide my p.values by 2 (p.values/2) and perform combine.test(p.values/2) in R?
In this paper I found following:
After combining the P-values, if desired the resulting combined P can be again converted to a two-tailed test by multiplying it by two.
So, after I get the combined p values, i multiply them by two to get back to the two-tailed test.
Would it be a correct procedure?
EDIT:
I found this post, saying:
Specifically, if two-sided p-values are being analyzed, the two-sided p-value (${p}_i$/2) is used, or 1-${p}_i$ if left-tailed p-values are used.
but I do not understand what is ${p}_i$ in this case. Is ${p}_i$ a two-sided p-value?
Comment: Isn't this a non-standard and potentially misleading way to conduct meta-analysis?  I would expect to see an analysis in terms of effect size.
Answer: What is important here is that the $p$-values $p_1,\ldots,p_n$ are independent and constructed so that if the null $H_i$ is true, $p_i\sim U[0,1]$. They are not restricted to one-sided tests/p-values.
For example, for Fisher's test with test statistic
$$F=-2\sum_i\ln(p_i),$$
we obtain its null distribution ($\chi^2_{2n}$) as follows: let
$y=g(p_i):=-2\ln(p_i)$. Then, $p_i=g^{-1}(y)=e^{-\frac{1}{2}y}$
and the density of $-2\ln(p_i)$ is given by
   $$ f_{-2\ln(p_i)}(y)=f_{p_i}(g^{-1}(y))\left|\frac{\partial}{\partial y}g^{-1}(y)\right|.
    $$
Note
    $$\frac{\partial}{\partial
y}g^{-1}(y)=-\frac{1}{2}e^{-\frac{1}{2}y}$$
and
$$\left|\frac{\partial}{\partial y}g^{-1}(y)\right|=\frac{1}{2}e^{-\frac{1}{2}y}.$$
We have $f_{p_i}(g^{-1}(y))=1\;\forall\;g^{-1}(y)\in[0,1]$ (a standard uniform density). This implies
    $$f_{-2\ln(p_i)}(y)=\frac{1}{2}e^{-\frac{1}{2}y}.$$
 The density of a $\chi^2_R$ random variable is
    $$f_{\chi^2_R}(y)=\frac{1}{2^{R/2}\Gamma(R/2)}y^{\frac{R}{2}-1}e^{-\frac{y}{2}}.$$
    With $R=2$, we get $f_{\chi^2_2}(y)=\frac{1}{2\Gamma(1)}e^{-\frac{y}{2}}.$ Recall that $\Gamma(1)=\int_0^\infty t^{1-1}e^{-t}\;dt=1$. So,
    $$
f_{\chi^2_2}(y)=\frac{1}{2}e^{-\frac{y}{2}}.
$$
    We have shown that $f_{-2\ln(p_i)}(y)=f_{\chi^2_2}(y)$. The proof is
    complete since the sum of $n$ independent
    $\chi^2_R$ r.v.s is distributed as $\chi^2_{n\cdot R}$.
That and why $p$-values are uniform under the null is for example discussed here: Why are p-values uniformly distributed under the null hypothesis?
My conjecture for your mistaken claim that you may only use one-sided p-values might be addressed here: Fisher's method for combing p-values - what about the lower tail?
The link explains that only large values of the test statistic provides evidence against the meta-null that all nulls $H_i$ are true, because the way the statistic is constructed, small $p_i$ translate into a large test statistic $F$.
Comment: For the first question, yes. For the second claim, yes in the sense that if your observed test statistic is, say, in the right tail of the null distribution, it needs to be further out there if you conduct a two-sided test than if you conducted a right-tailed test. (If your test statistic has is standard normal under the null, a test statistic larger than 1.645 would suffice in the right-tailed case, where it would need to exceed 1.96 in the two-sided case, both at the 5% level.)
Comment: So, it means that I could also use two-sided p-values for Fisher or Stouffer, doesn't it? The only thing is that, if I combine two-sided p-values I am more strict regarding the hypothesis.
Comment: Essentially, you should do a right-tailed test if only large positive values of the test statistic provide evidence against the null.
Answer: You need to think carefully about your scientific question. If you use two-sided p-values then what do you want to happen if five are significant in one direction and five in the other? Do you want them to cancel out?
In fact you also need to be careful with one-sided p-values as well because different methods of combining them do, or do not, cancel them out. Try your proposed method with (0.0001, 0.0001, 0.9999, 0.9999) and see if it corrsesponds to your scientific question.

meta-analysis of p values


Change background colour of selected Items hides the ListView checkboxes

Question: I found the following code which does a good job of modifying the background colour of selected items. However, the CheckBox for that line disappears.
Originally the CheckBoxes were not in their own columns so I was shifting everything 20 to the right. However, that didn't fix the issue.
I've now created a column or the CheckBoxes, the selected line is highlighted but you still cannot see the CheckBoxes either under the colored bar, or, if shifted right, left of the bar.
Clicking where the CheckBox should be still puts a tick in but you only see the box when you move off the line.
Any ideas about getting the CheckBox back, I've looked through DrawListViewSubItemEventArgs and cannot see anything that will do it.
Private Sub lsvFileList2_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles lsvFileList2.DrawSubItem
    Dim NewBounds As Rectangle
    If e.Item.Selected = True Then
        NewBounds = e.Bounds
        NewBounds.X = e.Bounds.X + 20
        e.Graphics.FillRectangle(New SolidBrush(Color.Purple), NewBounds)
        TextRenderer.DrawText(e.Graphics, e.SubItem.Text, New Font(Me.Font, Nothing), New Point(e.Bounds.Left + 3, e.Bounds.Top + 2), Color.AntiqueWhite)
    Else
        e.DrawDefault = True
    End If
End Sub

Modified code after suggestions from Jimi:
Private Sub lsvFileList1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles lsvFileList1.DrawSubItem
    Dim NewBounds As Rectangle
    Dim PurpleBrush As SolidBrush
    Dim glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, VisualStyles.CheckBoxState.CheckedNormal)
    Dim p = New Point(e.Bounds.Location.X + 4, e.Bounds.Y + CInt((e.Bounds.Height - glyphSize.Height) / 2))

    Using PurpleBrush
        PurpleBrush = New SolidBrush(Color.Purple)
    End Using
    If e.Item.Selected = True Then
        NewBounds = e.Bounds
        NewBounds.X = e.Bounds.X + 20
        e.Graphics.FillRectangle(PurpleBrush, NewBounds)
        TextRenderer.DrawText(e.Graphics, e.SubItem.Text, New Font(Me.Font, Nothing), p, Color.AntiqueWhite, Color.Purple)
        If e.Bounds.Left = 0 Then
            If e.Item.Checked Then
                CheckBoxRenderer.DrawCheckBox(e.Graphics, p, VisualStyles.CheckBoxState.CheckedNormal)
            Else
                CheckBoxRenderer.DrawCheckBox(e.Graphics, p, VisualStyles.CheckBoxState.UncheckedNormal)
            End If
        End If
    Else
        e.DrawDefault = True
    End If
End Sub

Comment: Add these Flags: `TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.PreserveGraphicsClipping` to TextRenderer. Then, you have to draw the glyph yourself (`CheckBoxRenderer.DrawCheckBox()`, considering the state of `e.Item.Checked`). Also, the new bounds only apply to the first SubItem, the others use normal bounds. -- You need to declare the SolidBrush with a `Using` statement: the ListView calls its drawing procedure constantly, you're leaking graphic resources.
Comment: The glyph has an offset. Based on: `Dim glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, VisualStyles.CheckBoxState.CheckedNormal)`, the drawing position is `Dim p = New Point(e.Bounds.Location.X + 4, e.Bounds.Y + CInt((e.Bounds.Height - glyphSize.Height) / 2))`
Comment: Thanks for the solution Jimi.  I decided that adding a first column just for the checkboxes was the best solution for various reasons, so have not used the flags that you suggested.  However, I have used your other suggestions including the glyphsize calculation which is less clunky than my own code (recently decided to finally move from VB6 to VB.Net so I'm still getting my head around the .net environment).  I've added the completed code to my original question for those interested.
Answer: That code will render the text under the CheckBoxes, the NewBounds Rectangle is never actually offset, so you'll also draw the new selection under it.
This is wrong: New Font(Me.Font, Nothing). You need to use the ListView Control's Font, not the Form's. If you want to use a new Font, you need to declare the Font object with a Using statement, otherwise you're leaking Graphics resource. Give that that the ListView draws its items very often, it's not good.
Same for the Brush, that's not the way to declare a disposable object with a Using statement. See the code posted here.
It's important that you define the TextFormatFlags to use when drawing in a Control that already uses this format, so you won't notice the difference.
It's also important to provide a Rectangle that defines the bounds of the Text, not a Point. Use a Point when you don't care where the Text is drawn. Usually, the Top/Left position.
The hard-coded offsets: glyphSize.Width + 7, e.Bounds.Location.X + 4 are just default values.
Private drawingFlags As TextFormatFlags = TextFormatFlags.Left Or
        TextFormatFlags.VerticalCenter Or TextFormatFlags.PreserveGraphicsClipping

Private Sub lsvFileList1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles lsvFileList1.DrawSubItem
    Dim itemBounds As Rectangle
    If e.Item.Selected Then
        If e.ColumnIndex = 0 Then
            itemBounds = New Rectangle(e.Bounds.Location, e.Bounds.Size)
            Dim glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, VisualStyles.CheckBoxState.CheckedNormal)
            itemBounds.X = e.Bounds.X + glyphSize.Width + 7
            Dim p = New Point(e.Bounds.Location.X + 4, e.Bounds.Y + CInt((e.Bounds.Height - glyphSize.Height) / 2))
            CheckBoxRenderer.DrawCheckBox(e.Graphics, p, If(e.Item.Checked, VisualStyles.CheckBoxState.CheckedNormal, VisualStyles.CheckBoxState.UncheckedNormal))
        Else
            itemBounds = e.Bounds
        End If
        Using brush As New SolidBrush(Color.Purple)
            e.Graphics.FillRectangle(brush, itemBounds)
        End Using
        TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.Item.Font, itemBounds, Color.AntiqueWhite, drawingFlags)
    Else
        e.DrawDefault = True
        e.DrawFocusRectangle(e.Bounds)
    End If
End Sub

Private Sub lsvFileList1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles lsvFileList1.DrawColumnHeader
    e.DrawDefault = True
End Sub


Formatação de Bloco de Texto em Latex

Question: Boa tarde! Estou tentando, sem sucesso, colocar esse bloco de texto nessa formatação da foto:

Como faço isso em Latex?
Desde já agradeço a ajuda.
PS.: Estou usando o TeXstudio para editar.
Comment: Você está usando algum pacote de configuração? Não sou um grande utilizador de Latex, então é difícil eu dar uma resposta, mas quando crio documento em Latex eu uso uma suíte de arquivos que já deixa tudo configurado de acordo com as normas ABNT. Caso seja essa sua intenção, acho que pode procurar por abntex.
Comment: Não estou. O artigo tem que seguir o padrão da universidade. Um ou outro elemento usa a ABNT, e é até tranquilo configurar. O problema é esse alinhamento que tá me dando nos nervos.
Answer: Deve haver uma maneira inteligente de se fazer isso, mas a solução que encontrei serviu para meu propósito.
Este é o produto que cheguei:
<code>\begin{flushleft}
\hspace*{<distância desejada, com a unidade definida ex: 8cm}
texto texto texto texto texto\\
\hspace*{<distância>}
texto texto texto texto texto\\
\hspace*{<distância>}
texto texto texto texto texto\\
\end{flushleft}
</code>
Aberto para soluções mais inteligentes.
Answer: Uma solução mais consistente seria:
<code>\thispagestyle{empty} % Numeração não deve aparecer na página de rosto.

\begin{center}
\textbf{\large TÍTULO:SUBTÍTULO}
\end{center}
\par
\vspace{7em}

\hspace*{8cm}\parbox{8.5cm}{{Artigo apresentado ao curso de graduação em ... da Universidade ... como requisito parcial para obtenção do título de ... em .... % ajuste a largura do \parbox de acordo com o tamanho de sua margem
\\[1em]
Orientador: x.
\\[1em]
Co-orientador: x }}

\par
\vspace{13em}
\begin{center}
\textbf{\large Brasília}\\
\textbf{\large Ano}
\end{center}
</code>

Formatação de Bloco de Texto em Latex


Questions about specific examples of ideals, evaluation morphism, adjunction of elements, and characteristics of rings

Question: $\newcommand{\Z}{\mathbb{Z}}$
$\newcommand{\C}{\mathbb{C}}$
$\newcommand{\R}{\mathbb{R}}$
$\newcommand{\s}{\sigma}$
$\newcommand{\Q}{\mathbb{Q}}$
$\newcommand{\F}{\mathbb{F}}$

 example 1:  $ R=\Z, I=n\Z$ with $f(r)=r+I$, ker f =I
 example 2:  Let $R=\R[x]$ and $\C$ be a ring. So there is an $i\in \C$ and a substitution homomorphism $\s=\s_{i}:\R[x] \rightarrow \C$ by $\s_{i}(A)=A(i)$. What is $I=ker \s$? Surely $x^{2}+1 \in I$, because $i^{2}+1=0$. It follows $(x^{2}+1)A \in I$ for every $A\in R$, so $(x^{2}+1)R\subset I$. But with $L=a+bx(a,b \in \R)$ it is : $\s(L)=a+bi\ne 0$. That gives us : $A=(x^{2}+1)Q+L$ with rest $L=a+bx$. It follows that $\tilde{\s}:R/I\rightarrow \C$ is a ringisomorphism. Especially $R/I = \R[x]/(x^{2}+1)\R[x]$ is a field, because $\C$ is a field.
 example 3:  $\Q[i]=\Q+\Q i$ (this is even a field)
 example 4:  $\Z[\frac{1}{2}]=\Z+\Z \frac{1}{2}+\Z(\frac{1}{2})^{2}+\cdots$
 example 5:  $a=2^{1/3}$, $\Z[a]=\Z+\Z a+ \Z a^{2}$
 example 6:  $Char(\F_{4}) = 2 $, because 1+1=0.
 example 7 : $Char(\F_{2} \times \F_{2})= 2$

Example 1 : The $ker$ is everything that is mapped to 0, is it correct that this is always an ideal of R?
Example 2:  I understand that $i^{2}+1=0$ in $\C$, but how can it be concluded from that, that $x^{2}+1 \in I$, because of the substitution homomorphism? How is the step(s??) from $\s(L) = a+bi$ to $A=(x^{2}+1)Q+L$ done?
Example 3: Apart from using the definition of a field, is there a way one can see immediately, that $\Q[i]$ is a field?
Example 4 and Example 5: Why isn't that the same as in Example 3?
Example 6: I think $\F _{4} = \{0,1,2,3\}$ but that is wrong according to this example...
Example 7: $\F _{2} \times \F_{2} = \{0,1\} \times \{0,1 \} = \{0,0\}, \{0,1\}, \{1,0\},\{1,1\}$
Characteristic is the littlest number so when the rest is 0  with r=mq+d, so when it is $\{0,0\}$; but why aren't there two characteristics even if it is the same ring (so the characteristic of this should be $2,2$ and not only 2?
Comment: I don't understand what you're saying in (2). Could you clean it up so I can read it?
Answer:
Yes, the kernel of a ring homomorphism is always an ideal. In fact:
Proposition. Let $R$ be a ring, and let $I$ be a subset of $R$. Then $I$ is an ideal of $R$ if and only if there exists a ring $S$ and a ring homomorphism $f\colon R\to S$ such that $I=\mathrm{ker}(f)$.
Proof. The projection map $\pi\colon R\to R/I$ is a ring homomorphism, and $\mathrm{ker}(\pi)=I$ (this is the example you have). Conversely, if $f\colon R\to S$ is a ring homomorphism, then $\mathrm{ker}(f)$ is nonempty (since $f(0)=0$); if $a,b\in\mathrm{ker}(f)$ then $f(a-b) = f(a)-f(b) = 0$, so $a-b\in I$; and if $a\in I$ and $r\in R$, then $f(ar) = f(a)f(r)=0f(r)=0$, $f(ra)=f(r)f(a)=f(r)0=0$, so $ar,ra\in I$. Thus, $I$ is an ideal. $\Box$
You can conclude that $x^2+1$ is in the kernel of the map by simply noting that $\sigma_i(x^2+1) = 0$. This amounts to plugging in $i$ for $x$ and verifying you get $0$. Now, assume that $p(x)$ is any polynomial in $\mathbb{R}[x]$. Using Long Division, we can divide $p(x)$ by $x^2+1$ with remainder; that is, we can find unique polynomials $q(x)$ and $r(x)$ such that
$$p(x) = q(x)(x^2+1) + r(x),\qquad r(x)=0\text{ or }\deg(r)\lt\deg(x^2+1).$$
Then we have that
$$\sigma_i(p(x)) = p(i) = q(i)(x^2+1) + r(i) = q(i)0 + r(i) = r(i),$$
so $p(x)\in\mathrm{ker}(\sigma_i)$ if and only if $r(x)\in\mathrm{ker}(\sigma_i)$. Now, since $\deg(x^2+1)=2$, $r(x)$ must be a polynomial of degree at most $1$, so we can write $r(x) = a+bx$ with $a,b\in\mathbb{R}$ (possibly zero). Under what conditions will $r(x)$ be in $\mathrm{ker}(\sigma_i)$? $r(x)\in\mathrm{ker}(\sigma_i)$ if and only if $\sigma_i(r(x))=0$, if and only if $r(i) = a+bi = 0$. But since $a$ and $b$ are real numbers, the only way for $a+ib$ to be equal to $0$ is if $a=b=0$. That is, $r(x)\in\mathrm{ker}(\sigma_i)$ if and only if $r(x)=0$. Going back to $p(x)$, we see that $p(x)\in\mathrm{ker}(\sigma_i)$ if and only if $p(x)$ can be written as $q(x)(x^2+1)$ for some polynomial $q(x)$, if and only if $p(x)$ is a multiple of $x^2+1$.
Apart from using the definition of "field"? Not really.
See below.
The difference between 3, 4, and 5, is that the element we are adding in 3 and 5 satisfies a monic polynomial; in 3, $i$ satisfies $x^2+1$ (that is, $i^2+1=0$); so any expression of the form $a_0 + a_1i + a_2i^2 + \cdots + a_ki^k$ can be rewritten using only constant and linear terms. In 5, $a$ satisfies $x^3-2$, so again, any expression of the form $b_0+b_1a+b_2a^2+\cdots +b_ka^k$ can be rewritten so that you only use constant, linear, and quadratic terms. But in 3, the element $\frac{1}{2}$ does not satisfy any monic polynomial with integer coefficients: it is impossible to express $\left(\frac{1}{2}\right)^n$ as a sum of integer multiples of smaller powers of $\frac{1}{2}$, so you do need all powers.
$\mathbb{F}_4$ is the field of 4 elements; $\mathbb{Z}_4$ is not a field. $\mathbb{F}_4 = \{0,1,\alpha,\alpha+1\}$, where $\alpha^2 +\alpha+1=0$.
$\mathbb{F}_2\times\mathbb{F}_2 = \Bigl\{ (0,0), (0,1), (1,0), (1,1)\Bigr\}$ with addition and multiplication componentwise. The characteristic is the smallest positive integer $k$ such that $kr = (0,0)$ for all $r$ in the ring. (Your definition made absolutely no sense to me). $k=1$ doesn't work, because $(1,1)\neq (0,0)$. But $2$ works, because $2(a,b) = (a,b)+(a,b) = (a+a,b+b) = (0,0)$ no matter what $a$ and $b$ are (since in $\mathbb{F}_2$, $r+r=0$ for all $r$).

P.S. You describe all these as "Examples". Examples of what? Surely you mean "statement"?
Comment: Dear Arturo. You are amazingly fast. I am posting my answer, even though your perfect answer is already here. I hope you don't mind. +1 from me.
Comment: @MattN. Of course I don't mind.
Comment: Thank you, Arturo Magidin.
Answer: @Example 1: Yes. Proof: Let $r \in R$ and $x,y \in \operatorname{ker}{f}$. Then $f(rx) = f(r) f(x) = 0$ and $f(xr) = f(x) f(r) = 0$ hence $rx \text{and} xr \in  \operatorname{ker}{f}$ and $f(x + y) = f(x) + f(y) = 0$ hence $x + y$ is also in $ \operatorname{ker}{f}$.
The kernel of any ring homomorphism is an ideal.
@Example 2: I've not seen the use of capital letters to denote polynomials. I'll stick to $p \in \mathbb{R}[x]$, hope you don't mind. You have $(x^2 + 1)$ evaluated for $x = i$ is $0$ hence $(x^2 + 1)p(x)=0$ for $x=i$.
@Example 3: What does it look like? Things in $\mathbb{Q}$ all have inverses and $i^2 \in \mathbb{Q}$. A thing in $\mathbb{Q}(i)$ looks like $a + ib$ for $a,b \in \mathbb{Q}$. So what is its inverse $(a + ib)^{-1}$? Well, $\frac{1}{a + ib} =\frac{a - ib}{(a + ib)(a -ib)} = \frac{a - ib}{a^2 + b^2} $ hence $1 = (a + ib) \frac{a - ib}{a^2 + b^2}$, so the inverse of $a + ib$ is $\frac{a - ib}{a^2 + b^2} $.
@Example 4: This cannot be the same as example $3$ because example $3$ is a field and this one isn't. To see why think about what an inverse of for example $1 + \frac12$ would look like: $(1 + \frac12)^{-1} = \frac{1}{1 + \frac12} = \frac23$.
@Example 5: Try to think about why this is not a field.
@Example 6: $\mathbb{Z}/4\mathbb{Z}$ is not a field because $2$ doesn't have an inverse. The field with $4$ elements is $(Z/2Z)[T]/(T^2+T+1)$, see here.
Note that this is an important construction: take a ring and quotient it by a polynomial to get a field of a given number of elements.
@Example 7: No the characteristic is defined to be the smalles $n$ such that $1 + 1 \dots + 1 = 0$ $(n \text{ times})$.
Hope this helps.
Comment: I don't understand your comment in 4 and 5; the original post had $\mathbb{Z}[\frac{1}{2}] = \mathbb{Z}+\mathbb{Z}(\frac{i}{2}) + \mathbb{Z}(\frac{i}{2})^2+\cdots$, I switched the $i$s to $1$s; of course, both the example of adding $\frac{1}{2}$ or adjoining $\frac{i}{2}$ leads to a similar expression...
Comment: @ArturoMagidin Yes thank you, that comment is actually misleading as $\mathbb{Z}[\frac{i}{2}]$ is not a field. I'm going to rewrite it.


Why isn't "Saddle-Free Newton" descent algorithm used in practice?

Question: Recently I have read a paper by Yann Dauphin et al. Identifying and attacking the saddle point problem in high-dimensional non-convex optimization, where they introduce an interesting descent algorithm called Saddle-Free Newton, which seems to be exactly tailored for neural network optimization and shouldn't suffer from getting stuck at saddle points like first order methods as vanilla SGD.
The paper dates back into 2014, so it's nothing brand new, however, I haven't seen it being used "in the wild". Why is this method not being used? Is the Hessian computation too prohibitive for real world sized problems/networks? Is there even some open source implementation of this algorithm, possibly to be used with some of the major deep learning frameworks?
Update Feb 2019: there is an implementation available now: https://github.com/dave-fernandes/SaddleFreeOptimizer)
Comment: I found reference to this same paper in an Uber Deep-Neuroevolution Blog post. Link: https://eng.uber.com/deep-neuroevolution/ You might ask the author if they have any implementation online / shared via GitHub.
Comment: If I had to guess, my assumption would be that computing + inverting the Hessian is impractical when your model has millions of parameters.
Comment: Can you refine your question from "is there an implementation"? That seems to afford, yes/no answers &/or sounds like a software request (which is off topic here). Could your question be elaborated into something like, 'what difficulties explain why there don't seem to have been more implementations'?
Comment: Good question, I couldn't find anything. However, the pseudocode is very simple so you could give it a try yourself, in which case there are some useful implementation details in one of the authors' doctoral thesis (page 103, https://papyrus.bib.umontreal.ca/xmlui/bitstream/handle/1866/13710/Dauphin_Yann_2015_these.pdf?sequence=6)
Comment: here is an implementation for TensorFlow:
https://github.com/dave-fernandes/SaddleFreeOptimizer
Comment: @gung Done. (This is actually my first ever question on CV, interesting to see it going alive after so long time)
Comment: Reddit discussion: https://old.reddit.com/r/MachineLearning/comments/bbw51k/d_saddlefree_newton_method_for_sgd_and_other/ It seems saddles are not that bad from generalization perspective (?) - the SFN paper plots are for training set, for test set such minimization is told to often lead to over-fitting (?) Good recent paper about evolution of eigenspectrum: https://arxiv.org/pdf/1902.02366
Answer: Better optimization does not necessarily mean a better model. In the end what we care about is how well the model generalizes, and not necessarily how good the performance on the training set is. Fancier optimization techniques usually perform better and converge faster on the training set, but do not always generalize as well as basic algorithms. For example this paper shows that SGD can generalize better than ADAM optimizer. This can also be the case with some second order optimization algorithms.

[Edit] Removed the first point as it does not apply here. Thanks to bayerj for pointing this out.
Comment: While I agree with the second point, the first one is not valid here. The authors propose to do optimisation only in the Krylov subspace, which does not require quadratic complexity.


.NET MAUI Blazor On page navigation

Question: There seems to be very little on the subject.
How can you click a button or an  tag and navigate to a different part of the page?
The only way I thought was possible was using simple html anchor tags that point to an element Id, sadly this does not work.
I would like vanilla MAUI Blazor options but if a third party component works, that's fine too.
Comment: What does the page in ` navigate to a different part of the page` mean? A razor page or a contentpage? I created a sample and try to add a label in the mainpage, but the razor page will not show. The app seems freezed.
Comment: It sounds like you're experiencing a different issue. In answer to your question. It's a Maui blazor app, so razor. Navigate in this context means moving from one part of the page to another part of the page. I.e Instead of scrolling, pressing a button at the top of the page that scrolls to a section at the bottom of the page.
Answer: I have created a new sample to test and used the js function to scroll to the element id replace the scrolling.
In the index.html:

In the razor:
@inject IJSRuntime js

<button class="btn btn-primary" @onclick="ScrollTo">Goto The Element</button>

  .... a lot of elements

The bottom of the page.

@code {
      private void ScrollTo()
      {
            ScrollToElementId("ap");
      }
      private async void ScrollToElementId(string elementId)
      {
            await js.InvokeVoidAsync("scrollToElementId", elementId);
      }
}

.NET MAUI Blazor On page navigation


Symfony workflow::can passes through workflow guard event listener

Question: I don't now if a lot of you have already tested the symfony workflow component but I hope some of you have :)
So, I am using this component on two objects and I want the first object to update the second one depending on the transition it is applying.
To do so, I am using the workflow guard listener on my first object and try to do a workflow::apply on my second object (using the second workflow ...).
The problem is that, when I make a workflow::can, the event is dispatched and it tries to apply a new status on my second object ... This is quite not normal as I am just asking if I can apply some transition and not asking to actually apply it on my first object.
config
framework:
    workflows:
        request_for_operation:
            type: 'state_machine'
            marking_store:
                type: 'single_state'
                arguments:
                    - 'status'
            supports:
                - AppBundle\Entity\RequestForOperation
            places:
                - draft
                - pending_for_management
                - in_progress
                - finished
                - canceled
            transitions:
                request_for_operations:
                    from: draft
                    to:   pending_for_management
                start_rfop_management:
                    from: pending_for_management
                    to:   in_progress
                close:
                    from: in_progress
                    to:   finished
                cancel:
                    from: [pending_for_management, in_progress]
                    to:   canceled
        operation:
            type: 'state_machine'
            marking_store:
                type: 'single_state'
                arguments:
                    - 'status'
            supports:
                - AppBundle\Entity\Operation
            places:
                - draft
                - pending_for_management
                - in_progress
                - finished
                - canceled
            transitions:
                validate_operation:
                    from: draft
                    to:   pending_for_management
                start_tracking:
                    from: pending_for_management
                    to:   in_progress
                close:
                    from: in_progress
                    to:   finished
                cancel:
                    from: [pending_for_management, in_progress]
                    to:   canceled

Subscriber
class RequestForOperationListener implements EventSubscriberInterface
{
    public function __construct(
        OperationManager $operationManager,
        UserNotifier $userNotifier
    ) {
        $this->operationManager = $operationManager;
        $this->userNotifier = $userNotifier;
    }

    public static function getSubscribedEvents()
    {
        return [
            'workflow.request_for_operation.guard.request_for_operations' => ['onRequestForOperations'],
            'workflow.request_for_operation.guard.start_rfop_management' => ['onStartRfopManagement'],
            'workflow.request_for_operation.guard.close' => ['onClose'],
            'workflow.request_for_operation.guard.cancel' => ['onCancel'],
        ];
    }

    public function onRequestForOperations(GuardEvent $event)
    {
        /** @var RequestForOperation $rfop */
        $rfop = $event->getSubject();

        //get all the operations linked to the rfop
        $operations = $rfop->getOperations();
        foreach ($operations as $operation) {
            //set the status of the operation to 'pending_for_management'
            $this->operationManager->applyTransition($operation, 'validate_operation');
            //set the status of the sub-operations to 'pending_for_management'
            foreach ($operation->getChildren() as $subOperation) {
                $this->operationManager->applyTransition($subOperation, 'validate_operation');
            }

            //get the users (i.e: managers) linked to the operation and notify them (by mail or whatever)
            $this->notifyAssignedUsers($operation->getUsers(), $operation);
        }
    }
}

Comment: You are not supposed to apply something in a guard. The guard is just here to prevent using a transition based on some assertion
Comment: I took the time to give a closer look at the Workflow class and I noticed that there are much more events dispatched than the documentation talks about ...
Comment: We have: leave, transition, enter, announce (and guard)
Comment: the guard event is dispatch on the workflow::can function, the others seems to be call on the workflow::apply (so maybe I have to use another event for what I want to do).
Comment: Wat is your question exactly? Do you get an error message?
Answer: I think I encountered the same issue as you, and I have logged a bug with the Symfony project: https://github.com/symfony/symfony/issues/33105


E2K7: Forward email from mailbox (without AD user) to external email address and internal mailbox

Question: I should create a new mailbox e.g. firstname.lastname@example.com. Mails to firstname.lastname@example.com should be forwarded to firstname.lastname@example.com and firstname.lastname@example.com. How can I do this with Exchange Server 2007? The best would be without separate Active Directory user, but I'm afraid this is not possible.
I know how I can forward an existing mailbox to an email contact(external) or other internal mailbox. I also know how to forward from email@example.com to email@example.com with the help of a email contact. But how to forward to an internal and external email address without an AD user?
Or another way: I can add the email@example.com to firstname.lastname@example.com. So firstname.lastname@example.com would receive the email. But how can I forward emails from email@example.com to firstname.lastname@example.com?
Solution:

Create a mailbox with firstname.lastname@example.com.
Create an e-mail contact (firstname.lastname@example.com)
Create a distribution group with two members: My external e-mail contact (email@example.com) and my AD user (firstname.lastname@example.com).
Forward firstname.lastname@example.com to the group.

But now I have created an extra mailbox with AD user, which I wanted to avoid. Also I have now a group with a separate SMTP address, which I also don't need. And I don't know if the difference between Exchange and Active Directory is good (external contact, group not in AD).
Answer: It sounds to me like you need to create a Distribution Group, add email@example.com and email@example.com to this group, and configure forwarding on firstname.lastname@example.com to forward to this group.
Comment: I'm not following. Why do you have an internal Contact (is that even possible?). You need to create a Distribution Group as I described and add email@example.com and email@example.com to this group. Then configure the forwarding options on the firstname.lastname@example.com mailbox to forward to this group.
Comment: I created an email contact in E2K7. I also created a distribution group in E2K7 (in AD he didn't find any contact). I have an email contact with email@example.com. But how to forward this email contact to the group? Now I have two contacts (the internal and the external email), but there is no option to forward the mails ..
Comment: My solution looks currently like the following: I have created a mailbox with firstname.lastname@example.com. I also created a distribution group with two members: My e-mail contact (firstname.lastname@example.com) and my AD user / mailbox (firstname.lastname@example.com). Now I forward firstname.lastname@example.com to the group. But now I have created an extra mailbox with AD user, which I wanted to avoid. Also I have now a group with a separate SMTP address, which I also don't need. But hey, it's working!
Comment: OK I can't currently find the user nor the distribution group in the AD. When does the changes are visible in the AD?
Comment: If this is a default install of SBS 2008 then in ADUC look in the MyBusiness OU and expand it to find your users, group, and contacts.
Comment: I expanded it but I only find the e-mail contact... I thought the data from the Exchange will be also in the Active Directory, but I made something wrong I think. Nevertheless, the forwarding works (but I created a new mailbox).


Does index works for LIKE statement in MySQL?

Question: I have a table (id, title), where id is PK. For a query SELECT * FROM table WHERE title LIKE "%stackoverflow%", I tried indexing title and fulltext indexing title. I used EXPLAIN to check if index works, and both don't work.
I am told index doesn't work for LIKE "%...%". Is this the case?
Comment: Use `REGEXP` instead of `LIKE`
Answer: noup it wont work, however if your index is 'xxx%' it will work,
another thing is your MySQL Version is older than 5.6 your engine HAVE TO BE MyIsam or Aria, but it cannot be Innodb to have a text index


Enviar um textarea ao pressionar enter com JS?

Question: Eu to tendo problemas na minha aplicação porque eu possuo dois tipos de textarea na mesma página. Um cria o post e o outro cria o comentário desse post.
O seguinte código faz o envio do textarea da postagem:
document.onkeyup=function(e){
     if(e.which == 13){
        if(document.getElementById('textarea-post').value == ""){
        } else {
            document.enviaPost.submit();
        }
      }
 }

Formulário do código acima:
 <form action="posta.php" method="post" name="enviaPost">

          <input type="hidden" name="data" value="<?= date("Y/m/d") ?>" />
          <textarea  placeholder="Como você está se sentindo?" name="contpost" id="textarea-post" /></textarea>

          <input type="submit" value="Enviar" class="botao-enviar-post" />

O que eu gostaria de saber é se, se eu colocar outra keyup como essa pro textarea do comentário, irá causar algum conflito. E caso dê, gostaria de saber como dá pra fazer isso sem gerar um conflito.
Segundo textarea:
<form  action="init/add_coment.php" method="post" name="enviaComent">
  <input type="hidden" value="<?=$post['id']?>" name="id_post" />
  <textarea id="texto_coment" name="comentario" class="comentario-texto" placeholder="Escreva um comentário..."></textarea>
</form>

Answer: Lembre-se de adicionar a biblioteca JQuery em sua página

Este código vai enviar o formulário enviaPost
$('#textarea-post').keydown(function(event) {
        if (event.keyCode == 13) {
            this.form.submit();
        return false;
    }
});

Faça a mesma coisa no formulário enviaComent
$('#texto_coment').keydown(function(event) {
        if (event.keyCode == 13) {
            this.form.submit();
        return false;
    }
});

https://jsfiddle.net/1h3dr7nt/
Comment: Não funcionou Lucas :/
Comment: Adicionou o jquery?
O ideal é que ele esteja ao final do código html da página, antes de fechar o  para que o usuário não tenha que aguardar o carregamento dele para mostrar o conteúdo da página e logo abaixo do jquery você abrirá . abra o dev tools(ctrl+shift+i) vá na aba Console e se tiver algum erro poste aqui
Comment: Funcionou! Muito obrigado! Só movi a importação pro final da página e deu tudo certo.
Comment: Eu tive um problema aqui... Na minha página existe um form para cada post. Assim, com o código do script acima, ele somente o primeiro comentário está funcionando.. Os demais não estão :/ Como posso fazer pra identificar cada textarea e cada form?
Comment: Desculpe a demora, em todos os textarea que você colocar o id="textarea-post" ou id="texto_coment" vai funcionar com o exemplo acima.


Are Exchange Online (EWS) WebCredentials passed securely?

Question: I'm writing a simple .NET web app to use EWS to view some room calendars on our Exchange Online (Office 365) cloud site. I know how to create the Exchange service in my code, set the credentials to a new WebCredentials (with valid username/password), and autodiscover the service URL.
My question is: are the username/password pair sent securely, or clear text?
If this is not secure, how about if I explicitly set the service's URL to the https://outlook.office365.com/EWS/Exchange.asmx URI?
Comment: **How** are you connecting to EWS? Please show us your code.
Comment: `ExchangeService svc = new ExchangeService();
svc.Credentials = new WebCredentials(AuthEmailAddress, AuthEmailPassword);
svc.AutodiscoverUrl(AutoDiscoverEmailAddress);`
Answer: Instead of calling AutoDiscoverUrl(), you can set the url directly using exchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");.
Also, after calling AutoDiscoverUrl() you can also inspect the Url property of your ExchangeService object to see whether it resolves to a secure endpoint or to an endpoint that you trust (from a specific list in your config file for example).
To be absolutely sure the returned Url is safe and secure, you should verify that the returned certificate is from the organization you expect it to be and that the certificate is signed by a trusted authority. This process is explained here. The default implementation mentioned int he article also accepts self-signed certificates, you probably don't want to do that in your production code. You could pin the certificate to a specific fingerprint for example.
If you want to exclude self-signed certs, change the following code in the referenced sample to return false:
// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.

// Or when you know that the certificate is signed by a trusted root authority, return false.
return false;

To answer your question, the username/password is normally sent securely using NTLM or Kerberos. In the worst case they can be sent using basic authentication, but if you're connecting over SSL, than it shouldn't be easy to intercept the password as long as you validate the SSL certificates properly.
Comment: I have already set the URL explicitly, but your comment is good (thanks). I can't get to my code right now, but I will inspect the URL property tomorrow. That may be good enough. I'll also go down the certificate route tomorrow. Thanks again.
Comment: Thank you. You saved the day.


A generalization of a Puiseux field

Question: Based on this question I wonder:
(1) Is it possible, instead of taking just one variable $t$ and get $P:=\bigcup_{n\in \mathbb{N}}K((t^\frac1n))$, to take $m \geq 2$ variables $t_1,\ldots,t_m$ and get the ''$m$-Puiseux field", denote it by $P_m$.
(2) If (1) has a positive answer, is it true that if $K$ is an algebraically closed field of characteristic zero, then $P_m$ is also an algebraically closed field?
Remarks: Let $K$ be an algebraically closed field of characteristic zero.
(a) It was mentioned here that $P$ is isomorphic to the algebraic closure of $K((t))$ (= the field of formal power series over $K$), then perhaps $P_m$ is isomorphic to the algebraic closure of $K((t_1,\ldots,t_m))$, so $P_m$ is algebraically closed.
(b) Another way to show that $P_m$ is algebraically closed is by induction on $m$: When $m=1$ we know that $P$ is algebraically closed (by the above mentioned question). When $m=2$ we can view $P_2$ as the usual Puiseux field (in one variable $t_2$) over the algebraically closed field
$\bigcup_{n\in \mathbb{N}}K((t_1^\frac1n))$, hence $P_2$ is algebraically closed (by the above mentioned question), etc.

I really apologize if my questions and remarks are trivial (either have a known answer or are just non-sense); it is because I am not so familiar with Puiseux fields.
Comment: I have now found the following paper: http://jlms.oxfordjournals.org/content/s2-8/3/504.extract which probably answers my question? (I am not sure, since only its first page is available).
Answer: It seems that this article answers my question in the positive.
It proves that $P_m$ is an algebraic closure of $K((t_1,\ldots,t_m))$.

A generalization of a Puiseux field


why does num_boost_round in xgboost influence auc score? it is crazy

Question: I tried to do xgboost native cv function and found that the auc score depends on num_boost_round parameter of cv function. When num_boost_round increases auc score also increases! Why is that?
What is the optimal value of num_boost_round and why?
Comment: Using bayesean reasoning: since many, many people use XGboost every day, the prior that there is such a fundamental error is extremely low.  One person having issues with a model in XGboost is a small amount of evidence, so the posterior says there is very little chance there is an error in XGboost, and a very large chance there is something you just dont understand about the library, or your data.  Please be more skeptical of yourself, and don't throw around weighty accusations like "it seems like there is an error in XGboost", it's a good way to not be taken seriously.
Answer: I was confused because n_estimators parameter in python version of xgboost is just num_boost_round. First I trained model with low num_boost_round and than I increased it, so the number of trees boosted the auc.
The optimal value is the number of iteration cv function makes with early stopping enabled.
Comment: Was this intended as an answer to your question, or a bit of
new information to help potential answerers? Please only use the "Your Answer" field to provide
answers to the original question. If you want to add more information, use the gray [edit](https://stats.stackexchange.com/posts/276696/edit) below the question body.


Why the list is not being empty again in python

Question: I have the following code:
def my_function(x, l=[]):
    for i in range(x):
        l.append(i+i)
    print(l)

my_function(2)
my_function(3)

For the first function call it prints [0,2] which is ok, but then for the second call it prints [0,2,0,2,4]!!! Why? I expected it to be only [0,2,4] since my second argument (or the absence of it) is an empty list. Am I doing something wrong? Am I assuming something wrong? Any help will be greatly appreciated.
Comment: This is a consequence of using a mutable data type as a default argument.
Answer: It's printing that because the list object does not change between function calls. Try creating a new list object for each call like in the following modified code:
def my_function(x, l):
    for i in range(x):
        l.append(i+i)
    print(l)

my_function(2, list())
my_function(3, list())


Showing an $\ell^1$-norm minimization problem defined over $\mathbb{C}^n$ can be rewritten as a problem over $\mathbb{R}^d$

Question: Let $A\in\mathbb{C}^{m\times n}$, $\mathbf{b}\in\mathbb{C}^m$, and $\eta\geq 0$. Consider the problem
\begin{alignat}{2}
 & \min_{\mathbf{x}\in\mathbb{C}^n} \ \  && \|\mathbf{x}\|_1\\
& s.t. && \|A\mathbf{x} -\mathbf{b}\|_2 \leq \eta.
\end{alignat}
Show that it can be rewritten as a problem involving real variables:
\begin{alignat}{2}
 & \min_{\mathbf{x}\in\mathbb{R}^d} \ \  && \mathbf{c}^T\mathbf{x}\\
& s.t. && \mathbf{x}\in K\\
& && g(\mathbf{x}) \leq \eta
\end{alignat}
where $K$ is a convex cone and $g$ is a convex function. Find $K$ and $g$ explicitly.
It seems simple to see that, since the 2-norm is convex, we would be able to rewrite $g$ as such. I'm having trouble finding an explicit $K$ that satisfies this. Any help?
Thanks
Answer: I think this should work: just to avoid confusion, replace all the $x$ variables in the top optimization problem with $z=(z_1,\ldots,z_n)\in \mathbb{C}^n$ and we will use $x\in \mathbb{R}^d$ to denote the variable in the bottom problem. The objective in the top part can be written as
\begin{equation}
\min_{z\in \mathbb{C}^n} \sum_{i=1}^n \sqrt{\text{Re}(z_i)^2+\text{Im}(z_i)^2}
\end{equation}
Let $d=2n+1$, and for each $z$ as above, think of $x\in \mathbb{R}^d$ as $x=(\text{Re}(z_1),\text{Im}(z_1),\ldots,\text{Re}(z_n),\text{Im}(z_n),t)$. Let $c=(0,\ldots,0,1)$. Then, define your convex cone $K$ as
\begin{equation}
K=\{(y,t)\in \mathbb{R}^{2n}\times \mathbb{R}: t\geq \sum_{i=1}^n \sqrt{y_{2i-1}^2+y_{2i}^2}\}.
\end{equation}
Note that $K$ is precisely the epigraph of the convex function $f:\mathbb{R}^{2n}\to \mathbb{R}$ given by $f(y)=\sum_{i=1}^n \sqrt{y_{2i-1}^2+y_{2i}^2}$, so is convex. It is a cone as the inequality is invariant under scaling by nonnegative numbers. Note that for any $y\in \mathbb{R}^{2n}$, the set of vectors $x=(y,t)\in K$ is precisely those where $t$ satisfies the desired inequality, and the minimal $t$ with this property is precisely $t=\sum_{i=1}^n \sqrt{y_{2i-1}^2+y_{2i}^2}$.
It seems like you know how to take care of $g(x)\leq \eta$, namely by just rewriting the constraint in the top problem in terms of real and imaginary parts, and just ignoring the $t$ variable. The reason this is an equivalent problem is that for any $z\in \mathbb{C}^n$, we can form the corresponding vector $x=(y,t)$ satisfying the constraints as described above, where $y$ gives the real and imaginary parts of $z$ and $t$ is exactly $\sum_{i=1}^n \sqrt{\text{Re}(z_i)^2+\text{Im}(z_i)^2}$, and this has the same objective value in both problems, so the optimum in the top problem is at least the optimum in the bottom problem. Conversely, given any $x=(y,t)$ satisfying the constraints in the bottom problem, you can go backwards to form the appropriate $z$ from $y$, and the objective value in the bottom problem is only potentially larger than it is in the top problem (if $t> \sum_{i=1}^n \sqrt{y_{2i-1}^2+y_{2i}^2}$). Therefore, the optimal values are equal, and there is an explicit mapping between optimal solutions between problems.


How could we pass FileReader's result to parent's state in React?

Question: Hello and thank you for your time.
I am currently learning React.
I am trying to refactor a component to separate and put the state into a top level component, called App, and make the ImageUpload, which renders a file input and a canvas, a stateless component.
The code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import registerServiceWorker from './registerServiceWorker';

class ImageUpload extends React.Component {

    _handleImageChange(e) {
        console.log("Enter");
        e.preventDefault();

        let reader = new FileReader();
        let newFile = e.target.files[0];
        console.log("File event: " + newFile);
        this.props.onChange(newFile, reader);
        console.log("File event after: " + newFile);

        reader.readAsDataURL(newFile)
    }

    render() {
        let imagePreviewUrl = this.props.upload;
        console.log("URL IS: " + imagePreviewUrl);
        let $imagePreview = null;
        if (imagePreviewUrl) {
            $imagePreview = ();
        } else {
            $imagePreview = (

Por favor seleccione una imagen de su ordenador para visualizarla
);
        }

        return (

                <form>
                    <input className="fileInput"
                           type="file"
                           onChange={(e) => this._handleImageChange(e)}/>
                </form>

                    {$imagePreview}

        )
    }
}

class App extends React.Component {
    state = {file: '', upload: ''};

    setFileFromUrl = (newFile, reader) => {
        console.log("The image file in APP method is: " + newFile);
        console.log("reader is:  " + reader);
        this.setState({
            file: newFile,
            upload: reader.result
        })
        ;
        console.log("Changed image file " + (this.state.file));
        console.log("Changed READER TO " + this.state.upload);
    };

    render() {
        return (

                <ImageUpload onChange={this.setFileFromUrl} file={this.state.file} upload={this.state.upload}/>

        )
    }
}

ReactDOM.render(<App/>, document.getElementById("mainApp"));
registerServiceWorker();

HTML:

Suba una imagen desde el ordenador, para mostrarla:

The code is based on: https://codepen.io/hartzis/pen/VvNGZP
I have studied the official tutorial and this course: https://app.pluralsight.com/library/courses/react-js-getting-started/table-of-contents
The difficult I am facing is that I would like to be able to update App's state from ImageUpload child component. Currently I am able to update App's state's file. However, I do not know how to do the same with App's state's upload condition, which tells canvas whether to show a string asking for inputting and image or to show it.
Output:
First when we load the page the state is:
State
file:
""
upload:
""

Then when we upload the image:
State
file:
File{…}
upload:
null

Why is upload null?
Also log result is:
index.js:10 Enter
index.js:15 File event: [object File]
index.js:52 The image file in APP method is: [object File]
index.js:53 reader is:  [object FileReader]
index.js:59 Changed image file
index.js:60 Changed READER TO
index.js:17 File event after: [object File]
index.js:24 URL IS: null

So we see that File flows from ImageUpload to App's state through setFileFromUrl method, when an onChange is present in the ImageUpload.
Why is URL null?
We know that FileReader is present in setFileFromUrl, and it is supposed that we update App's state's upload in:
upload: reader.result

Also, with the Development Tools, I have tried to manually change it to true:
And the output in the canvas is that it tries to show something, but it only shows a default thumbnail:

Could you help me please, thank you!!!
Comment: Any luck yet at about this issue? I'm having a similar problem.
Answer: You forgot to use reader.onloadend, add it to wrap your onChange call :
_handleImageChange(e) {
    console.log("Enter");
    e.preventDefault();
    let reader = new FileReader();
    let newFile = e.target.files[0];
    reader.onloadend = () => {
        this.props.onChange(newFile, reader);
    }
    reader.readAsDataURL(newFile)
}

Working codepen : https://codepen.io/Dyo/pen/LQxEYQ?editors=0110
Also, don't log this.state just after setState it won't log updated values because setState is async.
Answer: With the react function component, one could also use the below solution

import React,{useState} from 'react';
import {Buffer} from 'buffer';

function FileUpload() {

  const [filedata,setFiledata] = useState({fileBuffer:'No file uploaded'});

  const uploadFile = async (event) =>{
    console.log(filedata);
    event.preventDefault();

    //getting file from user
    const fileName = event.target.files[0];
    console.log('You uploaded => ',fileName.name);
    const fileReader = new window.FileReader();
    //reading filecontent as buffer
    fileReader.readAsArrayBuffer(fileName);
    console.log(fileReader);

    fileReader.onloadend = () =>{
      console.log('fileRead check in progress...');
      // console.log(Buffer(fileReader.result));
      setFiledata({fileBuffer: Buffer(fileReader.result)});
      console.log('buffer is',Buffer(fileReader.result));
    }

  }

  const fileSubmit = (event) =>{
    event.preventDefault();
    console.log('File submitted');
    console.log('filedata is',filedata.fileBuffer);
  }

  return (

      <form onSubmit={fileSubmit}>
        <input type="file" onChange={uploadFile}></input> {/*onChange */}
        {/* {filedata.fileBuffer} */}
        <input type='submit' ></input> {/*onSubmit */}
      </form>

  );
}

export default FileUpload;

below solution using native Node.js modules


Is Node.js ready for business critical applications?

Question: I was just reading More proof that enterprises love Node.js and wondering:

Should enterprise consider using nodejs for business critical
  applications?

It seems cloud computing companies like Joyent are fully on to it. Even Microsoft have started supporting nodejs on their Azure platform. I would like to know how seriously should we consider nodejs for enterprise application? Is it ready for mission critical applications?
Answer: Hell Yeah!!.. Understanding what Node.js is very important tho. A lot of large companies are already making a move towards using Node.js in many different ways
Other Examples
Linkedin - http://venturebeat.com/2011/08/16/linkedin-node/
Yahoo - http://developer.yahoo.com/blogs/ydn/posts/2011/11/yahoo-announces-cocktails-%E2%80%93-shaken-not-stirred/
Ebay - http://www.ebaytechblog.com/2011/11/30/announcing-ql-io/
Comment: mission critical applications in the enterprise usually means financial applications. And financial applications in the enterprise usually means a lot of mission critical number crunching that can't be messed up. Node is not a language for that. Most web stuff unless all your business is online is not mission critical. But OK maybe it is solid for that. But need better proof before I would think it is OK handling my financials.
Comment: I think nowadays thats dependent on the developers building those applications more than a language. Node is not a language, its a framework which solves a problem in a particular way, if it fits your problem set, use it.  Thats the way it is with languages or framework, they are very good at solving certain problems and aren't good at others. I haven't used node for financial stuff neither have I read anything about it not being a good fit for it or that its a perfect fit for those use-cases. So I can neither dispute or agree with you here.
Comment: Well, see the answer below, I am not sure if that qualifies as completely financial tho.
Answer: Dow Jones is using it for quite a few products now: WSJ Mobile, WSJ Portfolio, Streaming Stories, DJ X, and a few smaller projects. Within this next year almost all our web apps and pages will be on node =) We are constantly beefing up our node stack.


How to get Variance from Gaussian distribution and Random Initialization

Question: I am following deeplearning.ai's videos on Coursera. Prof Ng mentions that specific random initialisations for the weights(for example, by Xavier or He initialisations) can help optimise learning.
Particularly he says that multiplying a standard normal distribution by $\sqrt{\frac{2}{n^{[l-1]}}}$ produces a normal distribution with variance $  \frac{2}{n}$. Is there a proof for this statement? More particularly how can I derive the variance for a normal distribution multiplied by some factor?
Some background:
I attempted to find the variance for c*f(x) assuming f(x) is Gaussian and c is a constant by following the steps mentioned in the following question(particularly the accepted answer):
https://math.stackexchange.com/questions/518281/how-to-derive-the-mean-and-variance-of-a-gaussian-random-variable
However, I found some discrepancies in the results. I came to understand that this is probably caused by the fact that c*f(x) is no longer a PDF and hence we can no longer use the traditional definition of E(x), V(x) to estimate the mean and variance. So how are these particular random initialisations which lead to particular variances for the normal distribution arrived at?
Edit:
$ f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{\frac{-(x-\mu)^2}{2\sigma^2}} $
So, c*f(x) =
$\frac{\sqrt{c^2}}{\sqrt{2\pi\sigma^2}}e^{\frac{-(x-\mu)^2}{2\sigma^2}} $
I guess this is the part I don't understand. From what I can see, the constant only modifies the other term of the above expression. So, how is the new expression a valid PDF? The value of $2\sigma^2$ in the denominator of the exponent remains unchanged. So, how does this affect the variance?
Edit:
The following is an answer I got from another question.
The expressions for the mean and standard deviation are true for any random variable:
$$
E(X+k) = EX+Ek = EX + k, \quad V(X+k)=VX+Vk=VX,
$$
$$
E(kX) = kEX, \quad V(kX) = k^2VX
$$
I am sure you can find proofs for these easily. But we still need to show that the transformed variables have normal distributions. We can do that as follows. Assume $k>0$. Then:
$$
P(kX<x)
= P(X<\frac xk)
= \int_{-\infty}^\frac xk \frac{1}{\sqrt{2\pi}\sigma}\exp \left(-\frac{(t-\mu)^2}{2\sigma^2}\right)dt
$$
$$
= \int_{-\infty}^x \frac{1}{\sqrt{2\pi}k\sigma}\exp \left(-\frac{(u -k\mu)^2}{2(k\sigma)^2}\right)du
$$
where we substituted $u=kt$. This is the CDF of $\mathcal N(k\mu, k^2\sigma^2)$, so $kX$ has that distribution. (Note that we rediscovered the mean and variance). The cases $kX$ for $k<0$, and $X+k$ can be shown similarly.
Comment: Thank you. Sure. Will make an edit to the original question.
Answer: There's a difference between the parameters of a distribution $F$ and a multiple of a random deviate drawn from $F$.
In your example, suppose we have $x\sim \mathcal{N}(0, 1^2)$. Then $y=cx$ for some $c>0$ has distribution $y\sim \mathcal{N}(0, c^2)$. For details, see Going from a normal distribution to a standard normal distribution with a change of variable
This is obviously not the same as $cf(x;0, \sigma^2)$; by inspection, we can see that $cf$ is not a valid distribution because it will only integrate to 1 in the special case of $c=1$.
The key detail here is that you're multiplying the standard normal deviate by $c$, not the standard normal distribution.
Comment: That makes sense. However, I think there may be a small mistake in your formula. If $ x $ ~ $ N(\mu, \sigma^2) $, then for $y = cx$, for some c>0, the distribution is $ ~N(c\mu, c^2\sigma^2) $.
Comment: I have added a proof I found to the original question. Could you check it and let me know. It seems to me the mean shifts. Thank you.
Comment: Im guessing the mean doesn't shift for a standard normal distribution as it will be zero. But for other normal distributions, the mean appears to shift.
Comment: I see now that the flaw was generalizing beyond the standard normal case. In the original question, the focus is on 0-mean normals, so the relationship holds.


Does the sequence $n_i = n_{i-1} + random(a, b)$ have a name?

Question: I'm interested in the sequence $n_i = n_{i-1} + random(a, b)$ or, in plain English, "the next number is equal to the previous number plus a random quantity between a and b (for example $a=-1, b=1$)".
I'd like to read more about these sequences but I don't know how they're called, if they have a name at all, and how to search for them.
Comment: The term "random walk" comes to mind, try searching for that.
Answer: There's two cases to consider: is $\operatorname{random}(a,b)$ a discrete random variable equal to one of the values $\{a, a+1, \dots,b\}$ with equal probability, or a continuous random variable equal to a uniformly chosen real number in $[a,b]$?
In the first case, see a previous question: Distribution of Sum of Discrete Uniform Random Variables.
In the second case, we can normalize, defining $n_i' = \frac{n_i - ai}{b-a}$, which will now satisfy the recurrence $n_i' = n_{i-1}' + \operatorname{random}(0,1)$. This is known as the Irwin–Hall distribution or just the "uniform sum distribution".