Getting an unsupported media type error while having a json object posted and content-type set to application/json

Question: I'm doing a small test projet, the goal is to log throught Keycloak API and get my access token.
The problem i'm facing is that i got a 415 error "unsupported media type" as the following :
HTTP error
I've tried content type header as

text/plain
application/x-www-form-urlencoded
application/json

Here is my code :
void MainWindow::on_realmButton_clicked()
{

    QNetworkRequest req{QUrl(QString("http://localhost:8080/realms/demo/protocol/openid-connect/token"))};

    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QJsonObject body;

    body["client_id"] = "demo-client";
    body["grant_type"] = "password";
    body["client_secret"] = "CLIENT_SECRET";
    body["scope"] = "openid";
    body["username"] = "user";
    body["password"] = "password";

    QJsonDocument document(body);
    QByteArray bytes = document.toJson();
    qDebug() << "JSON Object :" << bytes.toStdString().c_str();

    netReply = netManager->post(req, bytes);

    connect(netReply, &QNetworkReply::readyRead, this, &MainWindow::readData);
    connect(netReply, &QNetworkReply::finished, this, &MainWindow::finishReading);

}

Answer: Try something like. Edit: add this note for clarity. "The protocol/openid-connect/token endpoint expects form encoded body, not a JSON body."
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

QUrl body;

body.addQueryItem("client_id","demo-client");
.
.
.
networkManager->post(req, body.toString(QUrl::FullyEncoded).toUtf8());


Given $a$, $b$ are integers with $a > b$ and the two roots $\alpha$, $\beta$ of the equation $3x^2 + 3(a+b)x + 4ab = 0$ satisfy a relation

Question:
Given $a$, $b$ are integers with $a > b$ and the two roots $\alpha$, $\beta$ of the equation $3x^2 + 3(a+b)x + 4ab = 0$ satisfy the relation $$\alpha(\alpha + 1) + \beta(\beta + 1) = (\alpha + 1)(\beta + 1)$$, find all the pairs $(a, b)$ of two integers.

Greetings, I was doing the above question related to number theory. I could not complete the question.
Here's what I've done so far:

Note that $\alpha + \beta = -(a + b)$ and $\alpha \beta = \frac{4ab}{3}$.
  Now expanding $\alpha(\alpha + 1) + \beta(\beta + 1) = (\alpha + 1)(\beta + 1)$ gives us $\alpha^2 + \beta^2 - \alpha \beta = 1$. Thus, $(\alpha + \beta)^2 - 3\alpha \beta = 1$.
  Substituting and doing some calculations, we get $a - b = 1$.

Now I can't proceed further. I don't know if this problem has been posted earlier on MSE (I have checked though).
Any help would be appreciated.
Thank You
Comment: I think you're only supposed to find the form of the ordered pair $(a,b)=(b+1,b),b\in\Bbb Z$.
Answer: Now, we have $b=a-1$ and
$$3x^2+3(2a-1)x+4a(a-1)=0,$$ which gives
$$9(2a-1)^2-4\cdot3\cdot4a(a-1)\geq0.$$
Can you end it now?
Comment: I am afraid $a+b\ne1$.
Comment: @Yves Daoust Thank you! I fixed.
Answer: Using $b=a-1$, the equation is:
$$3x^2+3(2a-1)x+4a(a-1)=0$$
The discriminant
$$9+12a-12a^2$$
is positive only for the integers $a=0$ and $a=1$


Any flexible customizable theme for display configuration via panels?

Question: Recently, I have been trying to design pages using panels and mini-panels. I followed some tutorial online, but the method used was a bit questionable. Since I am a newbie at this, I crashed my system beyond all repair, lol, but I managed to export valuable configuration and reload it on a fresh install. So, I am happy enough with that...
I have been playing with changing colors under Bartik, but honestly, that won't be enough...
I am looking for a flexible theme that will allows me to easily design the layout of pages from scratch using panels (most preferably). Ideally, I should be able to configure colors and fonts easily and the basic template should be simple and elegant. I want to load my own pictures to customize it (i.e., no preloaded pictures or they should be made removable).
Basically, the site is about sharing tips. Users will be able to put comments and vote for posts made by an editor. Nothing crazy.
Does anyone have a good recommendation to make for Drupal 7? Is there any such thing as a newbie-proof template? Thanks.
Answer: I still very new to the Panels module and theming in general but I have really enjoyed the Zen base theme.
I think it would have been confusing as a 'first' theme, but if you've been playing with Bartik and other common Drupal themes I think you'll love Zen.
The support, documentation and standardization surrounding the project is very impressive.
I should note though you need to be comfortable designing with CSS.  It's very 'clean' out of the box.
Answer: If - as I read from your question - you prefer a solution that's highly configurable through the admin-interface as opposed to coded through the theme, Omega theme in combination with Omega Tools, Delta and Context modules might be what you are looking for.
Answer: After doing more reading and video watching, I got to understand that some specific themes have been created for the Panels Everywhere module. It does not really make sense to use panels without panels everywhere. I am using Precision.
The Omega theme and Zen theme suggested in other answers are indeed powerful themes, but they don't integrate with panels. These are based on blocks, regions and zones.


Is SeleniumBasic v220.127.116.11 still the latest version and is it compatible with Chrome/ChromeDriver Version 103.0.5060.53 (Official Build) (64-bit)?

Question: I'm using VBA in Microsoft Access 365 and want to use it for web automation (filling web forms and getting the resulting information using variables)
I'm not 100% sure, but it looks like SeleniumBasic is installing an outdated ChromeDriver.exe? (2.21.371459)
Error Screenshot

My current version of Chrome is: Version 103.0.5060.53 (Official Build) (64-bit)
I'm reading all sorts of information about IDE's etc, and it's all a bit overwhelming! :(
To test this theory, I went ahead and replaced the ChromeDriver.exe file in C:\Program Files (x86)\SeleniumWrapper with the newer, compatible version # 102.0.5005.61
The code now throws another error

but at least it's now showing the correct driver. The error information screenshot also says that "DevToolsActivePort file does not exist". What??? How do I set that up? As I said, overwhelming.
Comment: Did you make sure you downloaded the same driver version number as your current chrome version?
Comment: Yes, I have now… and it works. Next question: is there some sort of "web recorder " that can record your input and save it as script? I was using OpenTwebst for that, but it only works with IE… which makes it all but useless!
Comment: Not that I know of, you have to go into the HTML of the webpage and find your identifiers (Ids, Class Names, Names, Tags, and XPaths) on your own.
Comment: Sadly there is no web recorder that will be perfect for your use case. Primarily because of the language. There are recorders like Katalon but the export languages do not include VBA. Furthermore, using easy to translate export languages like python suffer from the fact that selenium basic has a lot less functionality "out-of-the-box" so you would end up having to figure out workarounds, if/where possible, using custom JavaScript and executeScript method of webdriver instance.
Comment: That said, automation with selenium basic takes no more than a couple of hours to get a reasonable grip on. Reading html and understanding css selectors/basic JavaScript will take additional time. These are very good tutorials: https://www.wiseowl.co.uk/vba-macros/videos/vba-scrape-websites/selenium-get-started/
Comment: Your two error screenshots are completely identical. I can confirm that SeleniumBasic v.18.104.22.168 from 2016 absolutely still works on Windows 10 here in late 2022.
Difficult to say why you're experiencing these problems when you're not sharing any code and more specific details about your setup. Very bad if you want help from others. Please read this: https://stackoverflow.com/help/how-to-ask
Answer: Try using the .Start before using the .Get. Yes, this vba version of selenium hasn't had regular (or any) updates in like 8 years so it's not perfect. You have to keep your drivers up to date etc. You'll have a much better experience if you use Selenium with Python or Java.
Dim sel As New Selenium.ChromeDriver
Dim url As String

sel.Start "chrome", "http://google.com"
sel.Get "/"

Comment: Yeah, I downloaded a version of Chromedriver that matches my Chrome browser, and it seems to work… for a few seconds, then the browser closes. I have read that this is caused because the driver goes "out of scope" when the procedure ends. The cure is to "Dim" the driver in the main header section, Right after "Option Explicit" I haven't tested this yet.

Is SeleniumBasic v2.0.9.0 still the latest version and is it compatible with Chrome/ChromeDriver Version 103.0.5060.53 (Official Build) (64-bit)?


Using Multiple Mappers for multiple output directories in Hadoop MapReduce

Question: I want to run two mappers that produce two different outputs in different directories.The output of the first mapper(Send as argument) should be send to the input of the second mapper.i have this code in the driver class
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class Export_Column_Mapping
{
    private static String[] Detail_output_column_array = new String[27];
    private static String[] Shop_output_column_array = new String[8];
    private static String details_output = null ;
    private static String Shop_output = null;

    public static void main(String[] args) throws Exception
    {

        String Output_filetype = args[3];
        String Input_column_number = args[4];
        String Output_column_number = args[5];

        Configuration Detailsconf = new Configuration(false);

        Detailsconf.setStrings("output_filetype",Output_filetype);
        Detailsconf.setStrings("Input_column_number",Input_column_number);
        Detailsconf.setStrings("Output_column_number",Output_column_number);

        Job Details = new Job(Detailsconf," Export_Column_Mapping");

        Details.setJarByClass(Export_Column_Mapping.class);
        Details.setJobName("DetailsFile_Job");

        Details.setMapperClass(DetailFile_Mapper.class);
        Details.setNumReduceTasks(0);

        Details.setInputFormatClass(TextInputFormat.class);
        Details.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.setInputPaths(Details, new Path(args[0]));
        FileOutputFormat.setOutputPath(Details, new Path(args[1]));

        if(Details.waitForCompletion(true))
        {

        Configuration Shopconf = new Configuration();

        Job Shop = new Job(Shopconf,"Export_Column_Mapping");
        Shop.setJarByClass(Export_Column_Mapping.class);
        Shop.setJobName("ShopFile_Job");

        Shop.setMapperClass(ShopFile_Mapper.class);
        Shop.setNumReduceTasks(0);

        Shop.setInputFormatClass(TextInputFormat.class);
        Shop.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.setInputPaths(Shop, new Path(args[1]));
        FileOutputFormat.setOutputPath(Shop, new Path(args[2]));

        MultipleOutputs.addNamedOutput(Shop, "text", TextOutputFormat.class,LongWritable.class, Text.class);
        System.exit(Shop.waitForCompletion(true) ? 0 : 1);
        }
    }

    public static class DetailFile_Mapper extends Mapper<LongWritable,Text,Text,Text>
    {
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
        {
            String str_Output_filetype = context.getConfiguration().get("output_filetype");

            String str_Input_column_number = context.getConfiguration().get("Input_column_number");
            String[] input_columns_number = str_Input_column_number.split(",");

            String str_Output_column_number= context.getConfiguration().get("Output_column_number");
            String[] output_columns_number = str_Output_column_number.split(",");

            String str_line = value.toString();
            String[] input_column_array = str_line.split(",");

            try
            {

                for(int i = 0;i<=input_column_array.length+1; i++)
                {
                    int int_outputcolumn = Integer.parseInt(output_columns_number[i]);
                    int int_inputcolumn = Integer.parseInt(input_columns_number[i]);

                    if((int_inputcolumn != 0) && (int_outputcolumn != 0) && output_columns_number.length == input_columns_number.length)
                    {

                        Detail_output_column_array[int_outputcolumn-1] = input_column_array[int_inputcolumn-1];

                        if(details_output != null)
                        {
                            details_output = details_output+"       "+ Detail_output_column_array[int_outputcolumn-1];
                            Shop_output = Shop_output+"     "+ Shop_output_column_array[int_outputcolumn-1];

                        }else
                        {
                            details_output = Detail_output_column_array[int_outputcolumn-1];
                            Shop_output =  Shop_output_column_array[int_outputcolumn-1];

                        }
                    }
                }

            }catch (Exception e)
            {

            }
            context.write(null,new Text(details_output));
        }
    }
    public static class ShopFile_Mapper extends Mapper<LongWritable,Text,Text,Text>
    {
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
        {
            try
            {

                for(int i = 0;i<=Shop_output_column_array.length; i++)
                {
                    Shop_output_column_array[0] = Detail_output_column_array[0];
                    Shop_output_column_array[1] = Detail_output_column_array[1];
                    Shop_output_column_array[2] = Detail_output_column_array[2];
                    Shop_output_column_array[3] = Detail_output_column_array[3];
                    Shop_output_column_array[4] = Detail_output_column_array[14];

                    if(details_output != null)
                    {
                        Shop_output = Shop_output+"     "+ Shop_output_column_array[i];

                    }else
                    {
                        Shop_output =  Shop_output_column_array[i-1];

                    }
                }
            }catch (Exception e){

            }
            context.write(null,new Text(Shop_output));
        }
    }

}

I get the error..

Error:org.apache.hadoop.mapreduce.lib.input.InvalidInputException:
  Input path does not exist:
  file:/home/Barath.B.Natarajan.ap/rules/text.txt

I want to run the jobs one by one can any one help me in this?...
Answer: There is something called jobcontrol with which you will be able to achieve it.
Suppose there are two jobs A and B
ControlledJob A= new ControlledJob(JobConf for A);
ControlledJob B= new ControlledJob(JobConf for B);
B.addDependingJob(A);

JobControl jControl = newJobControl("Name");
jControl.addJob(A);
jControl.addJob(B);
Thread runJControl = new Thread(jControl);
runJControl.start();
while (!jControl.allFinished()) {
code = jControl.getFailedJobList().size() == 0 ? 0 : 1;
Thread.sleep(1000);
}
System.exit(1);

Initialize code at the beginning like this:
int code =1;

Let the first job in your case be the first mapper with zero reducer and second job be the second mapper with zero reducer.The configuration should be such that the input path of B and output path of A should be same.
Comment: **code = jControl.getFailedJobList().size() == 0 ? 0 : 1;** can you explain what code?
Comment: I am getting this error while running this but i have set all the required arguments**Error:org.apache.hadoop.mapred.InvalidJobConfException: Output directory not set.**
Comment: When you use fileoutputformat.setoutputpath specify the conf value and path. When you textinputformat specify the job value and path. Make the changes....
Comment: Is this r8? **Details.setInputFormatClass(TextInputFormat.class);**
**FileOutputFormat.setOutputPath(Details,new Path(args[1]));**
Comment: It should be FileOutputFormat.setOutputPath(DetailsConf,new Path(args[1]))
Comment: It is showing error in the code-*The method setOutputPath(Job, Path) in the type FileOutputFormat is not applicable for the arguments (Configuration, Path)*
Comment: are you using old api or new api.
Comment: using new apis only for all for jobcontroller i am using this *org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
 org.apache.hadoop.mapreduce.lib.jobcontrol.JobControl;*
Comment: Its working fine after a slight modification,but it is searching for third mapper ,i want to stop it after 2 mappers completes


Where am I going wrong? Trying to print no of times number is added before returning single digit

Question: function AdditivePersistence(num) {
    var count = 0;
    while (num.toString().length > 1) {
        count++;
        var num = calcVal(num);

    }
    return count;

    function calcVal(str) {
        var sum = 0;
        var arr = str.toString().split("");
        for (var i of arr) {
            sum = sum + arr[i];
        }
        return parseInt(sum.toString());
    }
}

console.log(AdditivePersistence(2233));

calcValue function returns sum of the array numbers and while loop repeats until returned number length is 1.
Comment: When you iterate an array using a `for...of` loop, `i` is a value from the array not the index.  Also, in your `while` loop you have this: `var num ....` you're declaring a new variable instead of using the one passed to the function as a parameter.
Answer: When you use for...of, i is the digit and not the index of the digit. In addition you need to parse the digit.
  sum = sum + parseInt(i, 10); // i is the number, not the index

Since you used i as an index, the result of calcVal() was always a string of length > 2, which means an infinite loop in your case.
Fixed code:

function AdditivePersistence(num) {
  var count = 0;

  while (num.toString().length > 1) {
    count++;
    num = calcVal(num);
  }

  return count;

  function calcVal(str) {
    var sum = 0;
    var arr = str.toString().split("");

    for (var i of arr) {
      sum = sum + parseInt(i, 10); // i is the number, not the index
    }

    return sum;
  }
}

console.log(AdditivePersistence(2233));
Comment: That worked. Thanks for the corrections ! Why do we need to parseInt "i" in for..of when the array elements are already numbers?
Comment: They are not numbers because - `var arr = str.toString().split("");` returns an array of strings.


How to calculate hyperplane for SVM?

Question: I am using SVM for document categorization. For that I have training sets i.e. set of training vector with their category. For example I have following sample data(assumption):

   -1 1:1 2:1
   +1 2:1 4:1
   -1 3:1 5:1
   +1 1:1 6:1
Can any one say:
  1) How to plot the data points in vector space(Sample diagram for the given test data   will help me best)?
  2) How to calculatehyperplane using the given sample?.
  3) How to classify the new document using hyperlane for following data?
       2:1 4:1

 4)Whether the kernel function are used for generating hypherlane efficiently?

My sample data:
-1 3:1 11:1 14:1 19:1 39:1 42:1 55:1 64:1 67:1 73:1 75:1 76:1 80:1 83:1
-1 3:1 6:1 17:1 27:1 35:1 40:1 57:1 63:1 69:1 73:1 74:1 76:1 81:1 103:1
-1 4:1 6:1 15:1 21:1 35:1 40:1 57:1 63:1 67:1 73:1 74:1 77:1 80:1 83:1
-1 5:1 6:1 15:1 22:1 36:1 41:1 47:1 66:1 67:1 72:1 74:1 76:1 80:1 83:1
-1 2:1 6:1 16:1 22:1 36:1 40:1 54:1 63:1 67:1 73:1 75:1 76:1 80:1 83:1
-1 2:1 6:1 14:1 20:1 37:1 41:1 47:1 64:1 67:1 73:1 74:1 76:1 82:1 83:1
-1 1:1 6:1 14:1 22:1 36:1 42:1 49:1 64:1 67:1 72:1 74:1 77:1 80:1 83:1
-1 1:1 6:1 17:1 19:1 39:1 42:1 53:1 64:1 67:1 73:1 74:1 76:1 80:1 83:1
-1 2:1 6:1 18:1 20:1 37:1 42:1 48:1 64:1 71:1 73:1 74:1 76:1 81:1 83:1
+1 5:1 11:1 15:1 32:1 39:1 40:1 52:1 63:1 67:1 73:1 74:1 76:1 78:1 83:1
-1 5:1 16:1 30:1 35:1 41:1 64:1 67:1 73:1 74:1 76:1 80:1 83:1
-1 5:1 6:1 15:1 20:1 37:1 40:1 50:1 63:1 67:1 73:1 75:1 76:1 80:1 83:1
-1 5:1 7:1 16:1 29:1 39:1 40:1 48:1 63:1 67:1 73:1 74:1 76:1 78:1 83:1
-1 1:1 11:1 18:1 20:1 37:1 42:1 59:1 62:1 71:1 72:1 74:1 76:1 80:1 83:1
+1 5:1 18:1 19:1 39:1 40:1 63:1 67:1 73:1 74:1 76:1 80:1 83:1

My Sample model file content:
1.0 3:1.0 11:1.0 14:1.0 19:1.0 39:1.0 42:1.0 55:1.0 64:1.0 67:1.0 73:1.0 75:1.0 76:1.0 80:1.0 83:1.0
1.0 4:1.0 6:1.0 15:1.0 21:1.0 35:1.0 40:1.0 57:1.0 63:1.0 67:1.0 73:1.0 74:1.0 77:1.0 80:1.0 83:1.0
1.0 2:1.0 6:1.0 16:1.0 22:1.0 36:1.0 40:1.0 54:1.0 63:1.0 67:1.0 73:1.0 75:1.0 76:1.0 80:1.0 83:1.0
1.0 5:1.0 6:1.0 15:1.0 20:1.0 37:1.0 40:1.0 50:1.0 63:1.0 67:1.0 73:1.0 75:1.0 76:1.0 80:1.0 83:1.0
1.0 5:1.0 7:1.0 16:1.0 29:1.0 39:1.0 40:1.0 48:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 78:1.0 83:1.0
0.47854266714671057 2:1.0 11:1.0 18:1.0 20:1.0 37:1.0 40:1.0 54:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 80:1.0 83:1.0
1.0 2:1.0 6:1.0 15:1.0 19:1.0 39:1.0 40:1.0 52:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 82:1.0 83:1.0
1.0 3:1.0 6:1.0 14:1.0 22:1.0 36:1.0 40:1.0 54:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 80:1.0 83:1.0
1.0 5:1.0 8:1.0 14:1.0 19:1.0 39:1.0 40:1.0 50:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 82:1.0 83:1.0
0.26128743031116053 3:1.0 7:1.0 18:1.0 22:1.0 36:1.0 40:1.0 50:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 82:1.0 83:1.0
1.0 5:1.0 15:1.0 22:1.0 36:1.0 40:1.0 63:1.0 67:1.0 73:1.0 74:1.0 76:1.0 82:1.0 83:1.0
1.0 1:1.0 15:1.0 21:1.0 35:1.0 45:1.0 62:1.0 68:1.0 72:1.0 74:1.0 77:1.0 80:1.0 93:1.0
Answer: Since you have more than two (in this example you have five, to be precise) dimensions, plotting would hardly help. Finding the maximum-margin separating plane is exactly what SVM is for. Unless you intend to implement it yourself, I suggest that you run LIBSVM on your data and read the resulting model file, which will contain the support vector coordinates. I'm not sure how to get the separating hyperplane out of that, but even if you do, it'll only be a hyperplane in the kernel space, not in the one where your samples are. If you just want to do linear classification, it may be better to use LIBLINEAR instead -- it's input format is the same as that of LIBSVM.
The decoding (i.e. predicting the class for a new sample) can be done either by invoking the corresponding binary from the distribution.
Comment: Hi Qnan.. I have tested LIBSVM with some sample data given in site.. I got model created by LIBSVM trainer. I have updated my question with sample training data and model created.. Can you please highlight which one is hyperplane coordinate?
Comment: @vigneshkumarrathakumar see the edit.
Comment: I believe if you have just two classes, then after running LIBSVM will contain a column of weights `w` that specify the hyperplane. The classification then should be something like comparing the dot product of that vector with a feature vector of a new sample and comparing that to zero.

How to calculate hyperplane for SVM?


How can I solve a problem I don't have any idea what to do with it like this?

Question: if
$$f(x)=\frac{4^{x}}{4^{x}+2}$$
, then
$$f(\frac{1}{11})+f(\frac{2}{11})+f(\frac{3}{11})+\cdots+f(\frac{10}{11})=?$$
By the way, I haven't taken calculus yet.
Comment: Hint: $$f(1-x) = \frac{4^{1-x}}{4^{1-x}+2} = \frac{2}{4^x + 2}$$
Comment: What kind of an answer do you expect here?
Comment: The answer of the question, and any idea on how to deal with these problems.
Answer: We have that
$$f(x)=\frac{4^{x}}{4^{x}+2}=1-\frac{2}{4^{x}+2}=1-f(1-x)\implies f(x)+f(1-x)=1$$
Comment: Can you please explain more?
Comment: Another hint: $1/11 + 10/11 = 1$.
Comment: @OmarMohamedKhallaf It is a symmetry you can figure out by the graph which is true for any $f(x)=\frac{a^x}{a^x+\sqrt a}$ with $a>0$.
Comment: @John Now I understand that $f(\frac{1}{11})+f(1 - \frac{1}{11})=1$, and the answer to my question will be $5$, but how can I figure this by my own?
Comment: OK, I think I got it now. Thanks for everyone helped me.


Can I perform RFE on a Categorical variable with multiple values?

Question: I have a dataset with a variable called attributes and it is an "array" of strings.
E.g.
Row 1:  ['backlight', 'stopwatch', 'world_clock']

Row 2: ['backlight', 'world_clock']

After doing some tokenizing, we transformed this array into multiple boolean (categorical) variables (actual dataset had > 50 variables):
backlight | stopwatch | world_clock | ... | + 50 more of these 'attributes' variables
    T           T           T
    T           F           T

Because now we have more than > 50 variables, we want to perform Recursive Features Elimination Cross Validation (RFECV) in order find an optimum no. of variables and perform regression to predict another variable, price.
However, I am unsure if I can remove these newly "variables". From these previously asked questions:

Does it make sense to apply recursive feature elimination on one-hot encoded features?
Can I ignore coefficients for non-significant levels of factors in a linear model?

the answers say that it doesn't make sense to remove a level of a categorical variable and it makes sense to me. But, my categorical variable attributes is not exactly categorical, it's more like an array. In fact, in my opinion, each attribute is independent of the other and they can in fact exists as separate columns but I am not sure about that...
I have 2 questions actually ...

What is this kind of variables called? Is it categorical? I have tried searching with different terms but to no avail
To add upon the linked questions, is it okay to "drop" these tokenized boolean "variables" and perform RFE and regression? Does it affect the accuracy of my model?
Answer:
But, my categorical variable attributes is not exactly categorical,
it's more like an array. In fact, in my opinion, each attribute is
independent of the other and they can in fact exists as separate
columns but I am not sure about that ...

This is in a way true, and the usual argument that you cannot drop a level of a categorical variable (without changing its meaning) is not relevant here. Each of your attributes variables is a binary variable in itself. But, the usual arguments against doing automatic variable selection do apply, see Variable selection for predictive modeling really needed in 2016?.  Look instead into regularization, search this site.


Highchart - Launch Sweetalert2 when click element

Question: I want to launch a Sweetalert2 toast when i click in a element of Highchart serie. In Highchart documentation shows how can we show a simple JS alert clicking in a element. I want to show a Sweetalert2 toast.
Sweetalert2
 private toast = Swal.mixin({
    toast: true,
    position: "top-end",
    showConfirmButton: false,
    timer: 5000
  });

Highchart serie
events: {
     click: function (event) {
          this.toast.fire({
               icon: "info",
               title: element.name
           });
      },
},

The problem: When i click in element serie element return

Cannot read property 'fire' of undefined

I suppose that .fire() function lost when try to launch it through click function. So: How can i launch sweetalert2 when click on chart serie element?
Answer: In the series click callback function this refers to the clicked series, so this.toast is undefined. If you only need the outer this, use arrow function:
events: {
    click: () => {
        ...
    }
}

If you also need to refer to the clicked series, use IIFE:
events: {
  click: (function(outerThis) {
    return function() {
      console.log(this, outherThis)
    }
  })(this)
}

Live example: http://jsfiddle.net/BlackLabel/Lrt6ajn9/
API Reference: https://api.highcharts.com/highcharts/series.column.events.click


How do I stop a remote form from submitting?

Question: I have a form that can be used both remotely and normally.
= form_for @comment, html: { class: 'comment-form' }, remote: request.xhr? do |f|
  = f.text_area :body
  = f.submit

I want the form to submit only if the body textarea has content:
  $(document).on 'submit', '.comment-form', (e) ->
    text = $(this).find('#comment_body').val()
    false if text.length < 1

That code works when the form is not Ajax. But when it's remote, it fails and the form still submits. Why? I also tried:
if text.length < 1
  e.preventDefault()
  false

I'm using Rails 4 with Turbolinks.
Update: I tried binding the ajax:beforeSend event, it still submits:
  $(document).on 'page:load', '.comment-form', ->
    $(this).bind 'ajax:beforeSend', ->
      alert "hi"

I see no alert...
Answer: I'm using Rails 22.214.171.124 and as described at jquery-ujs documentation in Stoppable events section you only need to return a false value in the ajax:beforeSend event to stop the request and prevent the form from being send.
So this works perfectly for me:
$(document).on("ajax:beforeSend", "form#messages_form", function (e, data, status, xhr) {
    var len = $.trim($('#message_body').val()).length;

    if (len > 3) {
        console.log("Send form!");
        return true;
    } else {
        console.log("Stopping request...");
        return false;
    }
});

I hope it could be useful for somebody else.
Comment: I don't know which is your case exactly but in this case the question was how to prevent the form from being sent if something special happens. I had a similar case _the form was being *submitted twice*_ and it was because in a `link_to` I used `remote: true` and at the same time I had and ajax request triggered with one of the `classes` my link was using.
Comment: Works. But what is the thing that is causing the form to be submitted twice in the first place?
Comment: If the form is already in the DOM upon page load, `$("form#messages_form").on("ajax:beforeSend", function ...)` will suffice.
Answer: You could potentially implement a handler for the ajax:beforeSend event to prevent your form from being submitted. It looks like the submit stops if your event handler returns false.
Wiki: https://github.com/rails/jquery-ujs/wiki/ajax
Example implementation: Stop $.ajax on beforeSend
Answer: Maybe I didn't get your question, but shouldn't it be enough to add a
:required => true

to your text_area ?
Comment: `required: true` adds an HTML5 attribute that doesn't work on half the browsers. It's not dependable. At least not at this time.
Answer: This looks like a jquery_ujs issue. Try changing the event to:
 $('.comment-form').on 'submit', (e) ->

I'm facing the same issue and only this solved the problem.


Vector support in Snowflake warehouse

Question: I have a vector data in my input field and I want to store this data in snowflake. So, can we store vector data in snowflake?
Comment: Your question isn't really clear here @ctom - what do you mean by a vector ? do you mean an indexed sequence of values like a vector in Scala?
Comment: yes, can we store indexed value (vector value) in snowflake warehouse?. just like bigquery supports storage of vector data
Answer: You should be able store it as one of the semi-structured data types as in the documentation:
<code>create table demonstration1 (
    id integer,
    array1 array,
    variant1 variant,
    object1 object
    );

insert into demonstration1 (id, array1, variant1, object1)
  select
    1,
    array_construct(1, 2, 3),
    parse_json(' { "key1": "value1", "key2": "value2" } '),
    parse_json(' { "outer_key1": { "inner_key1A": "1a", "inner_key1B": "1b" }, '
              ||
               '   "outer_key2": { "inner_key2": 2 } } ')
    ;

insert into demonstration1 (id, array1, variant1, object1)
  select
    1,
    array_construct(1, 2, 3, null),
    parse_json(' { "key1": "value1", "key2": NULL } '),
    parse_json(' { "outer_key1": { "inner_key1A": "1a", "inner_key1B": NULL }, '
              ||
               '   "outer_key2": { "inner_key2": 2 } '
              ||
               ' } ')
  ;

</code>

Vector support in Snowflake warehouse


Componente DatePicker quebrando a tela na edição

Question: Criei o seguinte componente para selecionar datas no UnForm:
export default function DatePickerInput({ name, ...rest }) {
  const datepickerRef = useRef(null);
  const { fieldName, defaultValue = '', registerField } = useField(name);

  const [date, setDate] = useState(defaultValue || null);
  useEffect(() => {
    registerField({
      name: fieldName,
      ref: datepickerRef.current,
      path: 'props.selected',
    });
  }, [fieldName, registerField]);

  return (

      <UnInput>
        <ReactDatePicker
          ref={datepickerRef}
          selected={date}
          onChange={setDate}
          dateFormat="dd/MM/yyyy"
          placeholderText="dd/mm/aaaa"
          writable="true"
          {...rest}
        />
      </UnInput>

  );
}

Para salvar registros o componente está funcionando normalmente, carregando e salvando a data que eu selecionei. Quando vou editar um registro, ao tentar carregar a data no load inicial, a página é quebrada e é exibido o seguinte erro:
Unhandled Rejection (TypeError): Cannot assign to read only property 'selected' of object '#<Object>'

Se eu comento a linha path: 'props.selected', no useEfect() a tela não é quebrada, porém a data não é preenchida no componente.
Como fazer para funcionar ?
Comment: @RafaelTavares sim. Estou usando o  react-datepicker. Postei os códigos completos aqui nestes dois Gists: https://gist.github.com/raphaelpradoo/aa301509820d78b726ff8ac258e750a1 https://gist.github.com/raphaelpradoo/ed20a035777a37bdc5d370f2fef699c9
Comment: Não sei é isso, mas já tentou usar `writable="true"` com o valor `true` sem as aspas: `writable=true`? Já vi plugins que tratam `"true"` (entre aspas) como string e `true` (sem aspas) como booleano e causa diferenciação e comportamento incorreto quando é string.
Comment: Ele não aceita.
Comment: Sem um exemplo [reproduzível](https://pt.stackoverflow.com/help/minimal-reproducible-example) não é possível ajudar. Você não mostrou nem os `import`'s para alguém poder tentar reproduzir. Parece que está usando o [`react-datepicker`](https://github.com/Hacker0x01/react-datepicker/blob/master/docs/datepicker.md), mas nem isso posso afirmar, já que ele não tem uma prop `writable`.
Answer: Problema :
formRef.current.setFieldValue('birthday',value) isso tentará definir valor no caminho fornecido, no nosso caso, desde que path seja props.selected
E props.selected é uma propriedade somente leitura, portanto você não pode definir valor em props, daí o erro.
useEffect(() => {
    registerField({
      name: fieldName,
      ref: datepickerRef.current,
      path: 'props.selected', // <---- this is props, and it's readonly
      clearValue: (ref) => {
        ref.clear();
      },
    });
}, [fieldName, registerField]);

Solução :
Você pode remover o path e usar os métodosgetter e setter, nomeados comogetValue e setValue:
setValue : para definir o valor inicial ou o que passou de setFieldValue
getValue :  para obter valor ao enviar
useEffect(() => {
    registerField({
      name: fieldName,
      ref: datepickerRef.current,
      clearValue: ref => {
        ref.clear();
      },
      setValue: (e, v) => {
        setDate(new Date(v)); // <---- Setting up default value
      },
      getValue: () => {
        return datepickerRef.current.props.selected; // to get selected value from Date picker's props
        // OR
        return Date.toString(); // to get selected value from state it self
      }
    });
}, [fieldName, registerField]);

DEMO DE TRABALHO :
Comment: Because this is the StackOverflow in Portuguese. This community is exclusively for Lusophone users so questions and answers should be asked in Portuguese. If you want to continue communicating in English, you can use [StackOverflow EN](https://stackoverflow.com/).
Comment: Any reason for downvote?
Comment: Okay thanks man,for explaining, let me convert it into Portuguese,
Comment: Do you need help with that?
Comment: @AugustoVasques,I know answer is 100% correct but I think,getting downvotes just for a language, I've tried to convert it, will you please also check if its still understandable or you can update it, thanks for asking.
Comment: it's OK(está OK).
Comment: @AugustoVasques, Thanks man (obrigado cara) :)
Answer:
writable="true"

No JSX, para inserir um valor booleano, entre outros , deve-se inserí-lo entre chaves ex. writable={true},
Comment: Continua com o mesmo problema


Finding a curve that intersects with $V(X_{0} X_{1}^{3} + X_{1}^{4} − X_{2}^{4})$ under certain conditions.

Question: Let $D=V(X_{0} X_{1}^{3} + X_{1}^{4} − X_{2}^{4})\subset\mathbb{P}_{\mathbb{C}}^{2}$ and $C=V(X_{0} X_{1}^{2} + X_{1}^{3} − X_{2}^{3})\subset\mathbb{P}_{\mathbb{C}}^{2}$. I have got that $C\cap D=\{(0:1:1), (1:-1:0), (1:0:0)\}$ with $mult_{(0:1:1)}(C,D)=1$, $mult_{(1:-1:0)}(C,D)=3$ and $mult_{(1:0:0)}(C,D)=8$. In addition, $C$ and $D$ have only one singular point, $(1:0:0)$, and only one inflection point, $(1:-1:0)$.
Now, I have to find a quartic $Q$ such that $mult_{(1:-1:0)}(Q,D)=3$, $mult_{(1:0:0)}(Q,D)=8$, and $(0:1:1), (0:1:0), (0:0:1), (1:0:1)\in Q$.
I think I have to manipulate $C$ in some way to get a quartic that satisfies those conditions, but I do not know how. In the same exercise I am also asked to find all lines tangent to $C$ and $D$ that contain $(0:0:1)$. I do not know if it is of any help, but (if I am not wrong) the only two are $V(X_{0}+X_{1})$ and $V(X_{1})$ for both curves, in the points $(1:-1:0)$ and $(1:0:0)$ respectively.
Any hint would be appreciated.
Answer: Let us write $F=X_{0} X_{1}^{3} + X_{1}^{4} − X_{2}^{4}$, $G=X_{0} X_{1}^{2} + X_{1}^{3} − X_{2}^{3}$ (which are the equations of $D$ and $C$ respectively).
Any quartic of the form $Q=V(GL)$, where $L$ is a linear polynomial, satisfies
$$mult_{(0:1:1)}(Q,D)\ge mult_{(0:1:1)}(C,D)=1,$$
$$mult_{(1:-1:0)}(Q,D)\ge mult_{(1:-1:0)}(C,D)=3,$$
 $$mult_{(1:0:0)}(C,D)\ge mult_{(1:0:0)}(C,D)=8.$$
And the same works if you choose $Q=V(GL+\lambda F)$, where $\lambda \in \mathbb{C}$. It suffices then to choose $L,\lambda$ such that  $(0:1:0), (0:0:1), (1:0:1)\in Q$.
Since $F(0,1,0)=G(0,1,0)=1$, $F(0,0,1)=G(0,0,1)=-1$ and $F(1,0,1)=G(1,0,1)=-1$, you can take $\lambda=1$ and $L=X_1+X_2$.


How to delete last symbol from string in SQL Server. In case it is a loop

Question: I have a query
WHILE @@fetch_status = 0
BEGIN
   SET @message =' ,' + CASE WHEN @year < 10 THEN 'socyr0' ELSE 'socyr' END + CAST(@year AS varchar) + ' as '+ CAST(@sql AS varchar) + ','
   PRINT @message
   FETCH NEXT FROM SocioCursor INTO @year
   SET @sql = @sql + 1
END

and result is few strings like
socyr08 as 2006,
socyr09 as 2007,
socyr10 as 2008,
socyr11 as 2009,
socyr12 as 2010,

What should I change to get
socyr08 as 2006,
socyr09 as 2007,
socyr10 as 2008,
socyr11 as 2009,
socyr12 as 2010

Answer: I am not sure if you are expecting them in a single row of text or not. I think you could do it like below without a cursor.
Please adjust variable length and right function as required and remove char(13) if you need a single row without line breaks.
Sql-Fiddle Demo
declare @sql int = 2006, @message varchar(500)

select @message = isnull(@message + ',' + char(13), '') +
                  'socyr' + right(100 + year, 2) + ' as ' +
                  convert(varchar(4), @sql),
                  @sql = @sql +1
from T

select @message

| COLUMN_0     |
----------------
socyr01 as 2006,
socyr02 as 2007,
socyr09 as 2008,
socyr10 as 2009,
socyr11 as 2010

Answer: You need to create and print the @message variable outside the loop.
In the loop you simply add a piece to it, like this: @message = @message + ...
Then, before printing it, you need to trim the last comma character:
@message = SUBSTRING ( @message, 1, LEN (@message) - 1 )


Choosing Best Nonlinear Model

Question: I have some data and I want to fit it to a nonlinear model. With a linear model there is a way to select a model using forward or backward selection. What is a similar method for a nonlinear model in situations where prediction of the model on unseen data is of highest priority?
Comment: Voting to close as too broad.  This is essentially asking for us to recite the entirety of all literature on model selection.
Answer: This is a very broad question but...
Yes, forward and backward selection (FS, BS) are not specific to linear models. FS and BS can be applied to any modeling schema where you have more than 1 input feature to estimate a relationship from. I tend to prefer BS, because I'm not usually limited in computing time/power.
The caret package has done a lot of this work for you. It supports BS (implemented as Recursive Feature Elimination) for linear models, tree and forest models, naive Bayes, and other models you could train via caret.
Comment: This is all true, but BS is not a good method.
Comment: It's just generally very outclassed by ridge, lasso, glmenet etc.  I generally avoid methods that make binary decisions, they are very high variance and there are always better options.
Comment: Right, but not individual trees!  You dull the effect of the binary decisions by boosting with a really small learning rate.  So the high variance of the binary decision *is* harmful, and you generally need ways to dull its impact, backwards selection doesn't offer such an option.
Comment: Matthew - why? It really depends on the context. If you're driving strictly for model accuracy, for example, it seems like a reasonable tool to have in the toolbox.
Comment: I haven't found that to be generally true, but we might be working in different contexts. If you count tree based models in the category of "binary decision methods", then I'd disagree. We see tree-based models winning many competitions via xgboost.
Comment: Gotcha. As usual, the answer is "it depends". I'm fond of using backwards selection as a one of my feature importance tools, not necessarily for direct modeling.

Choosing Best Nonlinear Model


Solving this problem algebraically: $81^{\sin^{2}(x)} + 81^{\cos^{2}(x)} = 30 $

Question: I saw this problem on an instagram feed
$$81^{\sin^{2}(x)} + 81^{\cos^{2}(x)} = 30$$
solve for x
And I saw that most people had solutions where you plug in x = 30 degrees or $\frac{\pi}{6}$ and the like. I get that if you assume that the two numbers added are integers, you can sort of back-calculate and say that
$81^{\frac{1}{4}} = 3 $
and
$81^{\frac{3}{4}} = 27$
so that gets you an answer that works.
But I was trying to come up with a good algebraic way to solve it -- I thought of using logs, but I don't think you can really do that here. I did see on method that says
let $u = 81^{\cos^{2}(x)}$ and you'd get, since $\cos^{2}(x) = 1 - \sin^{2}(x)$
$81^{1-\cos^2{x}} = \frac{81}{81^{\cos^2{x}}}$
So we can substitute in
$\frac{81}{u} + u = 30$
$81 + u^2 = 30u$
$u^2 - 30u + 81 = 0$
Solve this by factoring $(u - 3)(u - 27) = 0$ so solutions are at 3 and 27
that means $ u = 81^{cos^{2}(x)} = 3 $ therefore $\cos(x) = \frac{\sqrt{3}}{2}$
And it all works. But now that I seem to have figured this out I'd be curious as to why it works; or rather, is there some other method that would make sense? Again I thought of logs or something, but I feel like this method was "cheating" somehow...
Comment: whatever you did in second  method is an standard approach. this question is meant to be solved like your second approach.
Comment: Basically every solution will look more or less that, although e.g. $v=9^{\cos2x}$ may be the most trigonometrically elegant as it reduces to $\cos2x=\pm\frac12$.
Comment: I think the key point is to realize (as you did) that the trigonometry part is fake, and to ignore it, which you did by substituting $u=81^{\cos^2 x}$.
Answer: This is a very fine method, indeed we have that by $u=81^{\cos^{2}(x)}\neq 0$ and using $\cos^2 x+\sin^2 x=1$
$$81^{\sin^{2}(x)} + 81^{\cos^{2}(x)} = 30 \iff \frac{81}u+u=30$$
that is the two equations are completely equivalent and since
$$\frac{81}u+u=30 \iff u=3 \;\lor\; u=27$$
we obtain

$u=81^{\cos^{2}(x)}=3 \implies \cos^2 x=\frac14$
$u=81^{\cos^{2}(x)}=27 \implies \cos^2 x=\frac34$

which are indeed the two solutions.


Get Json Data from WebMethod to Application

Question: I wanted to get Json data from web method.In here i wanted to get News & Speaker both data but here only working News only.(Unable to get Speaker )
Here is my Stored Procedure
ALTER procedure [dbo].[LoadDayEvents]
@Date date
as
begin
select News,Speaker from Eventstbl
where DateToBePublished = CONVERT(date, @Date)
end

Here is my Web Method
 [WebMethod, ScriptMethod]
public static string SelectEventDate(string date)
{
    string News= "";
    string Speaker = "";

    try
    {
        SqlCommand comld = new SqlCommand("LoadDayEvents", conDB);
        comld.CommandType = CommandType.StoredProcedure;
        comld .Parameters.Add("@Date", SqlDbType.Date);
        comld .Parameters["@Date"].Value = DateTime.Parse(date).Date;
        if (conDB.State == ConnectionState.Closed)
            conDB.Open();
        News = comld .ExecuteScalar().ToString();
        Speaker = comld .ExecuteScalar().ToString();  // Thisone is not working
    }
    catch (Exception ee) { }
    finally { conDB.Close(); }
    return News;
}

Here i use Ajax/Json to get it

Comment: Your code seems to be partial. Where is the variable Verse defined?
Comment: Verse? i already remove that.
Comment: @ajp I wanted to get Speaker name in web method.News is working
Answer: You could return custom class
 var newsAndSpeaker = new NewsAndApeaker() { News = News, Speaker = Speaker}; //or define a custom class that has these two properties
 return newsAndSpeaker;

On Javascript, you can access the two variables using
msg.d.News //news
msg.d.Speaker

Or, you could return a list of string
 var newsAndSpeaker = new List<string>() { News, Speaker}; //or define a custom class that has these two properties
 return newsAndSpeaker;

On Javascript, you can access the two variables as in like a array
msg.d[0] //mews
msg.d.[1] //speaker

If your question is why your second property is not getting populated, then title seems to be wrong
Comment: In the return part it shows the error "newsAndSpeaker" does not exists in the current context


Can neural networks figure out some unknown transform?

Question: I am wondering can neural networks figure out some unknown transform?
I have two vectors:
$x$  - the original/truth value
$x_{t}$ - the transformed version of $x$
One way to describe it would be
$x_t=Fx$,
where $F$ is the transfer matrix (Ex. Fourier matrix in Fourier transform).
The problem here is that $F$ is an unknown transform, and it is not a regular known transform (Fourier, wavelet, ....etc.). But I have many $x$ and $x_t$, so I'm wondering if $F$ can be solved/known even empirically through neural network training?
Input of the neural network would be $x_t$, through the adjustment via weights and bias inside the network, the output should be $x$. Of course, here the transform has to be assumed the same for each transformation operation.
My vector size is 1024, meaning there are 1024 elements in both $x$ and $x_t$
Anyone can give some pointer how to do it with neural network? Seems to me a natural fit for a neural network problem. If neural network can do the job, is there any code/example/readings/literature on this topic?
I am not asking how to data fitting, that's not what I am after. I am just wondering if this problem can be done via neural network.
Thanks sincerely.
Comment: @Batman Thank you for your reply. But I am wondering if I can do that through neural network.
Comment: You don't need a neural network to learn F. You can use least squares techniques among or other things.
Comment: This is unclear. Is $F(x)$ linear in $x$ (as the discrete Fourier and wavelet transform) ? In that case $F(x) = M x$ for some matrix $M \in \mathbb{R}^{m \times n}$ that can find from $n$ linearly independent examples. Otherwise, if $F(x)$ is non-linear but piecewise continuous, then you can approximate it with a neural network.
Comment: @ user1952009 I actually don't know $F$ is linear or not. But interesting you said it can be approximated by neural networks if its non linear.
Comment: Then what do you know on $F$ ?... What is your concrete problem ?
Comment: I don't know anything on $F$, but I have a lot of $x$ and $x_t$. That is why I think neural network can work in this case.
Comment: Neural networks with $\tanh$ activation function are smooth functions of the input and can approximate (at any precision on some compact) any smooth function and hence any continuous function. For discontinuous functions the approximation will be bad at the point of discontinuity.
Comment: Of course you know something on $F$.. Where do the $x_i,F(x_i)$ come from ?
Answer: Theres quite a large literature on function approximation ability of neural networks.
I'll mention 3 papers:

Cybenko, G. (1989) "Approximations by superpositions of sigmoidal functions", Mathematics of Control, Signals, and Systems, 2 (4), 303-314
Kolmogorov, A. N. (1957). On the representation of continuous functions of many variables by superposition of continuous functions of one variable and addition. Doklady Akademii Nauk SSSR, 144, 679-681; American Mathematical Society Translation, 28, 55-59 [1963]
Telgarsky, M. (2016). Benefits of depth in neural networks,   arXiv:1602.04485 [cs.LG].

The first 2 papers say that for any suitably nice function and a big enough neural net, you can approximate the function. The last paper says that with a deeper neural network, you can have more wiggles in your function and be able to represent it with the same number of nodes (in some sense). You can also see this CS Theory SE thread.
Of course, having an algorithm to achieve these function approximations from data is a different matter -- these are purely theoretical results.
You can look at some theoretical guarantees for function approximation (e.g. in mean square error as a number of samples) in
Barron, Andrew R. "Approximation and estimation bounds for artificial neural networks." Machine Learning 14.1 (1994): 115-133.
(The author has done other papers which are relevant as well)
Comment: I think the main idea is to approximate $\phi$ by $\sum_n \phi(\frac{n}{N}) e^{-\pi N^2 x^2}$


Record video inside of iOS app, immediately play and loop

Question: I'm looking for a way for my users to take a video (defaulting to front facing camera, but with the ability to switch) lasting 2 seconds, display that video immediately and have it loop indefinitely (no controls displayed).  Essentially mimicking a 2 second gif.  I would like to do this in app so they can see the video before posting, and potentially retake.  Any ideas?  I've found some functionality here: https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/tdef/MPMovieControlStyle  but it doesn't seem to address the entire problem set.
Comment: Can you make your question more specific? *How do I record video?* is too broad to be a useful question.
Comment: Caleb, where did you read "How do I record video?" in my question above?  It's very specific about what I would like to do.
Comment: There are several different frameworks for recording video in iOS, all of which provide control over which camera to use, video duration, etc. Likewise for playback. What you need to know to fulfill your list of specific requirements isn't clear, and "Any ideas?" is not a sufficiently detailed question.
Answer: Checkout this guide from Apple:
Using Video
Comment: This is more of a comment than an answer.
Comment: Well, in this case, the criteria is basically,"does it actually answer the question, or just give more information that leads the OP to their own answer.  Also of note in this case, link only answers are frowned upon since the original link may go away at any time.  Better is to at least summarize the salient points of the answer and include the link as a 'for more information see...'
Comment: Thanks for the info, I am new to SO, are there some guidelines that say what should be  a comment and what should be an answer?


Drupal testing with DrupalGetAJAX

Question: I'm working on writing some automated tests for an AJAX API, and getting strange results. If I directly view the page, I see the following output, sent by drupal_json_output:
<code>{"guid":"8e2a179e-c901-8744-05fc-e80de9500dd6","success":1}
</code>
My unit test hits this same URL. Here's the test function:
<code> public function testInit() {
    $response = $this->drupalGetAJAX('plugin/init', array('query' => array('guid' => $this->registered_user->uuid)));
    $this->assertEqual($response['guid'], $this->registered_user->uuid, 'GUID Loaded');
    $this->pass(json_encode($response));
  }
</code>
When I initiate the test run, it goes to the usual batch API progress page. Rather than moving directly to the test results page, I get "An error has occurred, please continue to the error page". Here's the output that follows:
<code>An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: /batch?id=64&op=do StatusText: OK ResponseText: {"guid":"a06f78ec-1368-f4c4-c9ac-a710b3e941b5","success":1}{"status":true,"percentage":"100","message":"Processed test 1 of 1 - \u003Cem class=\u0022placeholder\u0022\u003EPlugin\u003C\/em\u003E.\u003Cdiv class=\u0022simpletest-fail\u0022\u003EOverall results: 7 passes, 1 fail, 0 exceptions, and 1 debug message\u003C\/div\u003E\u003Cdiv class=\u0022item-list\u0022\u003E\u003Cul\u003E\u003Cli class=\u0022first last\u0022\u003E\u003Cdiv class=\u0022simpletest-fail\u0022\u003EPlugin: 7 passes, 1 fail, 0 exceptions, and 1 debug message\u003C\/div\u003E\u003C\/li\u003E\n\u003C\/ul\u003E\u003C\/div\u003E"}
</code>
This reads to me like some sort of error within the test runner, as you can see the response from the AJAX API module is there. It looks like it's showing my module's JSON output concatenated to the output of the batch call to the test runner.
The output for $response is always "1" when viewing the test results page. In the detailed test result page, the call is listed as follows, returning 1 byte:

GET
  http://platform.dev/plugin/init?guid=5b299683-49d8-7754-f9ca-3fb88e79ff0f
  returned 200 (1 byte).

For experimentation sake, I also ran the test suites for AJAX commands and the batch API. The same thing happens. My best guess is that this is a bug in Drupal 7.15.
Comment: Do you have any debug modules enabled. Try disabling one module at a time and check if it is due to a contrib module first. Also make sure there is no space before the <?php tag.
Answer: The issue is a known one, and a patch fixed the issue - http://drupal.org/files/drupal.simpletest-curl-cookiejar.33.patch

Drupal testing with DrupalGetAJAX


Variável JS para PHP ou direto no BD

Question: Bom dia. Sou um programador iniciante, e estou desenvolvendo um app web de vendas, que soma todos os preços dos produtos incluídos dinamicamente por javascript:

E joga nesta div:

            <?php echo "O total é R$ " ?>

Banco:
 <?php

     $connect = mysqli_connect("localhost", "root", "", "narguile");

     $number = count($_POST["input_quantidade_venda"]);

     $soma_venda = $_POST['soma_venda'];

     if($number > 0) {

         for($i=0; $i<$number; $i++) {

             if(trim($_POST["input_quantidade_venda"][$i] != '')) {

                 $sql = "INSERT INTO vendas(quantidade, total) VALUES('".mysqli_real_escape_string($connect, $_POST["input_quantidade_venda"][$i])."', '$soma_venda')";
            mysqli_query($connect, $sql);
             }
         }

         echo "Venda cadastrada!";
     }

     else {

         echo "Não rolou";
     }
 ?>

Mas preciso colocar o valor desse "soma_venda" no Banco de Dados, e sinceramente, não faço ideia de como. Se alguém puder me ajudar, eu agradeço.
Comment: É necessário utilizar *AJAX* ou um formulário para envio de dados.
Comment: @ValdeirPsr, estou utilizando. coloquei o código completo agora...
Comment: Possível duplicata de [Igualar variável php a uma variável javascript](https://pt.stackoverflow.com/questions/25136/igualar-vari%c3%a1vel-php-a-uma-vari%c3%a1vel-javascript)
Comment: Vc quer incluir no banco todas as quantidades e o total geral (não o total de cada produto)?
Answer: Crie um input hidden para receber o valor total e enviar junto no serialize.
Coloque-o após essa div:

<input type="hidden" name="soma_venda">

E na função que faz a soma, acrescente:
("[name=soma_venda]").val(total);

Ficando assim:
function somaTotal(){
   var total = 0;

   $(".input_preco_venda").each(function(){
      total += parseFloat($(this).text());
   });

   $(".soma_venda").text(total);
   $("[name=soma_venda]").val(total);
}

Pronto. O valor da soma total irá junto no serialize e será recebida no PHP na variável $soma_venda = $_POST['soma_venda'];.

Variável JS para PHP ou direto no BD


Ajax cache control

Question: I am having a problem with ajax requests in Internet Explorer and in Chrome - I cannot bust the cache.  Normal pages don't have the problem - it's just the ajax requests.
I know that one workaround is to append a random query string variable to the end of the URL.  However, I don't want to lose all the benefits of caching, I just want the browser to pick up the new file if the version on the server is different from the cached version.
I have tried manually setting the ajax POST header, to no avail:

xmlHttp.setRequestHeader("Cache-Control", "must-revalidate");

Adding this to my .htaccess file doesn't work either:

<FilesMatch "\.(js|css).*">
  Header set Cache-Control: "max-age=172800, public, must-revalidate"
</FilesMatch>

Any help would be greatly appreciated.
Thanks, Brian
Answer: What about creating/extending a component that emits your css/js includes with the latest versions appended to the links? This way it's not going to grab a fresh copy every time, and will only grab a new version when the server says there is one.
Comment: That's actually a fantastic idea.  My only issue is that I'm using the mod_concat apache module which requires the
Comment: Solved the issue by using this answer in combination with mod_rewrite to get around the restrictions imposed by mod_concat.  Thanks for the help.

Ajax cache control


Group by new column without adding it

Question: Is there a way to group a dataframe df by a series s without adding s to df first?
The reason I am asking is because
df.assign(bin=pd.cut(df[what],n)).groupby('bin')

seems to create a full copy and that occasionally requires too much memory.
Comment: yes, groupby allows you to group a helper series without assigning it to a dataframe.  please add some data to replicate the question so we can try and comeup with something
Answer: You can do with
df.groupby(pd.Serise(pd.cut(df[what],n),index=df.index))

More info
df.groupby(pd.Series(pd.cut(df.c1,3),index=df.index)).sum()
Out[156]:
              c1  c2  c3
c1
(0.994, 3.0]   6  13  13
(3.0, 5.0]     0   0   0
(5.0, 7.0]    13  10   7

df
Out[154]:
   c1  c2  c3
0   7   5   2
1   1   2   2
2   3   4   3
3   1   4   6
4   1   3   2
5   6   5   5

Group by new column without adding it


Cannot perform tests for multivariate normality. Is my data set too large?

Question: I'm examining the performance of quadratic and linear discriminant models at classification.
My dataset has 250,000 observations, 2 groups and 30 explanatory variables.
I thought it would be worth considering whether or not my dataset displays multivariate normality, since that is one of the assumptions of discriminant function analysis.
The steps i took were:
1: created a data frame of just the 30 explanatory variables
2: converted the data frame into a numeric matrix (explanatoryVariables)
3: I used the mshapiro.test(explanatoryVariables) function and received the following error:

Error: cannot allocate vector of size 465.7 Gb

I have also tried
hzTest(explanatoryVariables)
mardiaTest(explanatoryVariables)
roystonTest(explanatoryVariables)

but also receive the same error.
Would this be due to the fact that I have too much data? Would there be any other way to show multivariate normality like reducing sample size, and would that make a significant impact in any way on the validity of the test?
Regards,
Marvin
Answer: With such a large ratio of samples to variables you could randomly sample them to use substantially fewer and get likely similar results.
You can also do quantile-quantile (QQ) plots of pairs of variables, multivariate normality also implies pairwise normality.
But I'm not sure you should take statistical normality tests very seriously (as opposed to plots which you should take seriously). Almost all real world data will be found to be non-normal given enough samples, unless generated by a really simple random process. So what impact would knowing that your data is approximately normal but not exactly normal have on your analysis? Likely not much, the main point is "does it make a difference?" and to answer that you have to empirically evaluate whether your models work well and whether some form of transformation would make them work better, judged by some criterion such as how well they predict in cross-validation.


Issue while implementing json_exists using JPA Native query

Question: My Query looks like below,
@Query(value="SELECT * FROM Retention_Record retention where retention.entity=:entity and retention.category=:category and retention.record_type=:recordType and json_exists(retention.reference_data_id, '$?(@==$referenceDataId)' passing :referenceDataId as \"referenceDataId\")" , nativeQuery=true)

When i am getting below error while execution of this query
2020-07-23 16:12:11 [eventsTaskExecutor-5] [] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 29902, SQLState: 99
999
2020-07-23 16:12:11 [eventsTaskExecutor-5] [] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-29902: error in executing
ODCIIndexStart() routine
ORA-20000: Oracle Text error:
DRG-50900: text query parser error on line 1, column 10
DRG-50917: escape on at end of text query string

Can anyone please help.
Comment: Does it work in Oracle database?
Comment: yes... on oracle db it works.
Comment: Can you share that query as well?
Comment: Hi @MuhammadWaqasDilawar,  SELECT * FROM Retention_Record retention where retention.entity=? and retention.category=? and retention.record_type=? and json_exists(retention.reference_data_id, '$?(@==$referenceDataId)' passing ? as "referenceDataId" )
Comment: Also, this works, but the query in question does not.    ```@Query(value="SELECT * FROM Retention_Record retention where retention.entity=:entity and retention.category=:category and retention.record_type=:recordType and json_exists(retention.reference_data_id, '$?(@==50000021)' )" , nativeQuery=true)```
Answer: I fixed the issue. Problem was with the way i was storing the Json Object in database. I was storing the value in db as ["50000021","50000022"] which in fact is a valid json, and was trying to execute my query to provide result, which was resulting in error. When i stored data in this format, { "ReferenceDataId" : ["50000021","50000022"] } below query successfully executed. – Ritesh 1 min ago   Edit   Delete
@Query(value = "SELECT * FROM Retention_Record retention where retention.entity=:entity and retention.category=:category and retention.record_type=:recordType and json_exists(retention.reference_data_id, '$.ReferenceDataId?(@==$referenceDataId)' passing :referenceDataId as \"referenceDataId\")", nativeQuery = true)


What's the protocol when an opponent is injured and your team has posession?

Question: In football, what protocol should a player follow when an opponent collapses to the ground after a legitimate challenge or tackle?

Put the ball out of play so they can receive (potential) treatment?
Keep posession as the referee has the authority to pause the game anyway?

The problem with fair play is deception: it is very common for football players to feign injury and minutes later be fully revived so that the need to stop is minimal. In the rare case of real injury the difference in 30 seconds of receiving treatment for a muscle or bone injury will likely have no effect.
Answer: You'll be unlikely to find a quote for this, so I'll draw upon personal experience as a player and referee.
First, let's clarify the referee's role in stopping play for injuries.
Law 5 - The Referee, Section 3 - Powers and Duties:

The referee:
...

allows play to continue until the ball is out of play if a player is only slightly injured
stops play if a player is seriously injured and ensures that the player is removed from the field of play ...

So if the referee hasn't stopped play, they've decided that the player is only slightly injured.
Ettiquette here really depends what is happening when the injury occurs or is noticed. If the other team are in the middle of a promising attack, there is not really any expectation that they put the ball out of play. They've done nothing illegal - so it's not fair that they're punished for fair play, and even if the ball is returned to them, it's not going to restore the strong position they were in.
However, if the other team is simply knocking the ball around the back, or not really doing anything with it, the team of the injured player will generally expect that the ball will be put out of play. In return, the team of the injured player will give the ball back, and the other team will be effectively in the same position they were in before the ball was put out of play.


Diferença entre consulta com e sem Inner Join

Question: Qual a diferença entre fazer as duas consultas abaixo, uma com e a outra sem INNER JOIN? Qual a mais adequada?
SELECT p.Descricao as Produto, c.Descricao as Categoria FROM Produto p, Categoria  c
WHERE p.CategoriaId = c.CategoriaId

SELECT p.Descricao as Produto, c.Descricao as Categoria  FROM Produto p
INNER JOIN Categoria c ON p.CategoriaId = c.CategoriaId

Answer: De forma geral podemos dizer que é a sintaxe apenas. Uma sintaxe que deixa mais claro o que deseja fazer uma junção pode ser mais interessante pela legibilidade.
É possível que algum mecanismo de banco de dados faça alguma otimização em algum caso que ela não consegue em outro. Pode até ser uma deficiência dele não conseguir fazer nos dois casos, mas pode correr. Tem que ver como isso se comporta no seu SGDB.
E atente porque pode não ser igual em todas situações, alguns farão uma otimização em todas situações, mas não em todas. Pra isso existe o EXPLAIN na maioria dos bancos.


Statistical analysis (comparison) of time course experiments

Question: I have data sets that represent multiple measurements over time. The data sets come from biological experiments in which we measure something in N individual cells, with around 250 measurements over a time frame of around one minute. For each of the time points, we have summary statistics (mean, SD, N) that we can plot to create a curve; this curve will thus be the average curve of, say, 30 individual curves from individual cells, and error bars will be either the SD or SEM of those individual curves.
Now, we have several such "average" curves that represent, e.g. "treated" versus "non-treated" cells. We need to statistically evaluate whether the curves are significantly different.
What I have done so far are multiple t-tests ("one per row") between two curves - this gives me individual p values, one for each time point, which looks like a reasonable start. In principle it tells me from what time point in the measurement the curves are significantly different (if they are at all).
Is there a tried-and-true statistical method to get a single p-value to determine whether two such curves are significantly different? Can anyone let me know (in layman's terms) how to do it?
edit: For illustration, here is a typical result - we have means, SD and N for every time point, but show only a few SDs here as error bars to not totally obscure the "average" line. What we'd like to have is one value that describes the significance of the difference between two lines, e.g. of the blue and the red ones.
Comment: You could have a look at the tag [tag:functional-data-analysis].  Maybe you can show us some plots?
Answer:
Is there a tried-and-true statistical method to get a single p-value to determine whether two such curves are significantly different?

Unless you have some theoretical form for the change in signal over time, this seems to be an ideal application of generalized least squares regression. You model the individual observations as a flexible function of the combination of time and treatment, doing so in a way that takes the correlations of observations within individual cells into account. That is sometimes called the "growth curve model," a name that seems to describe your example data pretty well. Frank Harrell's course notes and book explain the approach with an extended example in Chapter 7.
Using 250 separate t-tests is both inefficient and potentially misleading. With a p < 0.05 cutoff for "significance" you would expect about a dozen false positives just by chance. Also, that doesn't directly model the time course or account for the fact that you have repeated measurements on individual cells.
You can accomplish what you want with a single generalized least squares model. Each observation is modeled, with one row per observation notated with time, treatment, a cell ID, and any other covariates you want to evaluate.
The time course is fit with just a few coefficients, for example via a regression spline, versus the 250 separate coefficients you implicitly use now. With fairly simple, non-wiggly curves as you show, you only need a few "knots" to break the time axis into time periods for this. Within each time period between a pair of knots, values are fit with a cubic polynomial. At each knot, the polynomials from the time periods on both sides are constrained to be continuous and smoothly joined. From the data you show you might even be able to fit a single low-order polynomial over the entire time range. The idea is the same whether you use a single polynomial or a spline: describe the time course with just a few coefficients whose values you estimate directly from the data.
To evaluate the effect of treatment, you include as predictors in the model both a term for treatment by itself and an interaction between treatment and time, including interactions with the spline/polynomial terms modeling time. The coefficient for treatment would determine whether there was any difference between treated and control at a reference time point, typically at time = 0. The interaction terms allow the shape and time course to differ between the treatments.
To account for the correlations of observations within individual cells over time, you specify an overall structure for those correlations that is incorporated into the modeling. The Harrell references show how to do that in the context of generalized least squares. Mixed models with "random effects" for cells might be used instead to this end, but I don't see any advantage for your application.
"To get a single p-value to determine whether two such curves are significantly different," you do a test that evaluates all of the coefficients associated with treatment to determines whether any are different from 0. If you work with the tools in Harrell's rms package and its Gls() interface to generalized least squares, that's readily done with that package's anova() function. If you have a particular interest in the differences at 50 seconds, you can get error estimates that include information from all of the observations, not just those taken at 50 seconds.
Comment: Thank you very much for the detailed answer - there is quite a bit I first have to digest before I can get into action. One thing I do not (yet) understand is how, from modeling the curves (that is easy in my case) I can get to a statistical evaluation. In particular, I have the feeling (correct me if I am totally wrong) that by modeling the curves I will lose the information from the repetitive measurements per time point (the error bars/ standard deviation and n). Disregarding this information cannot be correct...
Comment: @frofa if you use _all the individual observations_, annotated as I suggest, then the statistical model will incorporate information from all the observations. You don't lose anything, as all of the observations contribute to estimating the coefficients that define the curves.
Comment: Thanks again. We do, of course, have all individual observations, but as we already have the summary statistics, we would very much like to work with those instead. This would scale much more easily to a larger number of experiments, and also allow to use normalized data (as we do to create the average curves and error bars in my example).
Comment: @frofa using the summary statistics time point by time point discards all the information about trajectories over time within each cell. The data-storage and computational requirements to work with individual observations don't seem to be overwhelming here. Including the `time 0` observation as a covariate helps normalize, and you can display 0-1 "normalized" data after the statistical tests based on generalized least squares.
Comment: @frofa I suppose you could consider [weighted least squares](https://en.wikipedia.org/wiki/Weighted_least_squares) (WLS) while modeling treatment and time as suggested for generalized least squares. That's a "specialization of generalized least squares." You weight each summarized time-point value by its estimated variance. But, depending on the nature of the data, that's not only an inefficient use of the data but it can also be misleading, as it ignores the correlation structure within the data.


How can I clear cookie when server is unreachable

Question: I am using node.js + express server. And maintaining a session for the login users.
I want to know how does a logged user to logout when the server is unreachable?
I know In the normal approach, I can call req.logout() on a request object in the express server. And that may clear cookies in the browser and session data on the server.
But what if when the server is unreachable once the user logged in? How can they log out themselves in the browser ?
Comment: clear browser cookies?
Comment: @Saharsh yes It's browser cookies which stored the session id
Answer: Use the Javascript on the client side as shown here
Clearing all cookies with JavaScript
Otherwise, if these are session cookies, they will clear when the user close the browser.
Any other cookies should be cleared by the user manually using a browser.


how to get the value from disabled box in selenium testng framework?

Question: how to get the data from disabled textfield or nonedited box?i need to get the data from non edited box.For example, pagelength is 2, i need to read the data, but we cant change the data from the textfield.i need to read, and print it as 2  in testng
Thanks in advance
Comment: Please revise your question and write with more clear things. At this stage it does not provide clear that what actually you want to perform using selenium. Also you have not specified that you are using selenium IDE or Web driver?
Answer: You can get the value from disabled text field as we do fetch value from general text field.
store the text field in webelement and call a method getAttribute("value");

//value is one of the attribute associated with text field.

Here is working example:
<code>WebDriver driver = new FirefoxDriver();
        driver.get("http://www.w3schools.com/jsref/prop_text_disabled.asp");
        String value = driver.findElement(By.xpath("//*[@id='main']/div[2]/div[2]/input")).getAttribute("value");
        System.out.println("Disabled Test Field contains: "+value);
</code>
O/p would be-  Disabled Test Field conatins: Hello World!


In variational inference, is it important to have a conjugate prior to the likelihood?

Question: For example, if I define my prior distribution as Gaussian but I define my likelihood as multinomial, does this break any theoretical basis for variational inference?
Answer: No this breaks no theoretical basis for Bayesian inference, nor for Variational inference. However, it may make your computation more complicated. Depending on the form of variational inference this may be more or less of a concern. For example, in mean-field variational inference you will likely break the nice, closed form solutions you get when you choose the conjugate priors.
Comment: thank you! are you familiar with the Variational autoencoder or stochastic gradient Variational bayes? if so, do you know if it matters if one uses a multinomial likelihood with a gaussian prior?
Comment: I'm not familiar w/autoencoders, SGD is an optimization method and VB is a form of variational inference. Neither of these two preclude the use a multinomial or a Gaussian prior.


Padrão MVC Web com NodeJS

Question: Galera, não estou achando nada relacionado, é o seguinte:
Estou aprendendo o modelo MVC, usando o módulo Express (NodeJS), conexão com MySQL Ok e já tem até uma interaçãozinha entre as páginas na WEB, tudo beleza até agora, mas tem um problema. Não faço ideia de como exibir o resultado da consulta personalizado numa página HTML, o máximo que já consegui foi exibir com JSON transformando em String e setando na tela.
exports.exibirCarros = (req, res) => {
let query = 'SELECT * FROM carros';

let result = pool.query(query, (err, rows) => {
   if(err) console.log(err);

   let vetor = [];

  // vetor.push(JSON.stringify(rows));
   rows.map((todo) => vetor.push(todo.cor));
  //  console.log(vetor);
   return vetor;
});
console.log(result);

res.render('pages/showCars');

}
Dentro do escopo do 'pool.query()', eu consigo o array que eu quero exibindo a variável 'vetor' :
[ 'Vermelho', 'Preto', 'Vermelho', 'Verde', 'Vermelho', 'Preto' ]

Agora quando dou um 'console.log(result)', eu nem sei o que significa isso:
    <ref *1> Query {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _callback: [Function (anonymous)],
  _callSite: Error
      at Pool.query (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/mysql/lib/Pool.js:199:23)
      at exports.exibirCarros (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/controllers/carroController.js:14:23)
      at Layer.handle [as handle_request] (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/layer.js:95:5)
      at next (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/route.js:137:13)
      at Route.dispatch (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/route.js:112:3)
      at Layer.handle [as handle_request] (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/layer.js:95:5)
      at /home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:281:22
      at Function.process_params (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:335:12)
      at next (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:275:10)
      at Function.handle (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:174:3),
  _ended: false,
  _timeout: undefined,
  _timer: Timer { _object: [Circular *1], _timeout: null },
  sql: 'SELECT * FROM carros',
  values: undefined,
  typeCast: true,
  nestTables: false,
  _resultSet: null,
  _results: [],
  _fields: [],
  _index: 0,
  _loadError: null
}

Código-fonte
Comment: Você deve renderizar a página dentro do _callback_ de `pool.query`, que retorna um objeto porque a consulta SQL é executada assíncronamente
Comment: Fiz isso e ele me retornou um erro bem louco ->
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:494:11)
    at ServerResponse.header ...
Comment: E aí cara, não sei o que estava fazendo de errado, mas tentei de novo e deu certo quanto ao callback, vlw aí.
Answer: amigo, tudo bem?
Utilizando Node.js você não consegue exibir conteúdo dinâmico dentro de uma página HTML. Para fazer isso, existe um pacote chamado EJS, feito exatamente para o que você precisa. Se você já tiver um arquivo HTML, vai bastar apenas alterar a extensão para .ejs
1- Instale o EJS com: npm install ejs --save
2- Configure o servidor para reconhecer o ejs:
const app = require('express')();
const ejs = require('ejs');

Abaixo você informa para o express que quer usar o EJS
app.set('view engine', 'ejs');

Abaixo você informa onde as views estão
app.set('views', '/views');

3- Altera a extensão de .html para .ejs
Pronto! Agora você sua página já pode exibir conteúdo dinâmico em sua página.
Saiba mais como o EJS funciona no site do NPM: https://www.npmjs.com/package/ejs
Espero ter ajudado.
Comment: Opa, eu já imaginava isso. Aliás, já estou usando o .ejs , mas ainda não faço ideia de como recuperar o resultado de uma função assíncrona (pool.query) para o meu front. Vou dar uma pesquisada a mais sobre o EJS mesmo, vai que acho. Vlw pela dica mano.
Comment: E aí mano, blz? Andei pesquisando e era realmente disso que eu precisava, obg mesmo pelo norteamento.
Comment: Que bom que pude ajudar. =)

Padrão MVC Web com NodeJS


Sorting with a predefined list in R

Question: I am trying to sort this dataframe:
 Age  grade name
 21.0   6   John
 21.0   6   Paul
 22.8   4   Jake

With the help of a list of names:
Paul, Jake, John

What is the most simple way to do it?
Answer: Data generation:
txt <-
"Age  grade name
21.0   6   John
21.0   6   Paul
22.8   4   Jake "
df <- read.table(text=txt, header=T)

In order to support custom sorting you can convert column to factor with specified levels:
df$name <- factor(df$name, levels=c("Paul", "Jake", "John"))

Now ordering df[order(df$name),] gives:
   Age grade name
2 21.0     6 Paul
3 22.8     4 Jake
1 21.0     6 John

Comment: Should add a note to make it clear that `order` acts on the numerical order of the factors, not their alpha sorting order.

Sorting with a predefined list in R


Cross Domain Tracking in Google Universal Analytics with Javascript Links

Question: So I have a scenario where I am constantly getting a good number of self-referral traffic to my website. I investigated it further and believe it to be a case of an issue with my cross domain tracking (as I use a 3rd party shopping cart).
What I can't seem to figure out is what I have wrong and why it isn't working. My specific scenario looks like the following
MainSite.com Analytics Code

CheckoutSite.com Analytics Code

As for the setup options:

The following domains are added to the exclusion referral list in GA

mainsite.com
checkoutsite.com (the actual checkout is on a subdomain of kiosk.checkoutsite.com but this shouldn't make a difference AFAIK)

mainsite.com 301 redirects to www.mainsite.com
As far as I can tell the 3rd party checkout JS script is adding link decorators as seems to be required Edit: Have confirmed as per the comments below

The links to the checkout cart are Javascript based which result in a iframe popover. They look like the follwing:

However, I still appear to be getting self-referrals and am completely unable to figure out why. Is there something wrong with my code or my setup? Is there something else I should be looking for?
Any help would be hugely appreciated!
Comment: Not sure if it would make a difference but I don't think you need to add the source domain, mainsite.com, to the autolink parameters.
Answer: You should be good code wise. You can also exclude those domains as referrers:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#selfreferrals
As you go through the flow yourself do you see #_ga hash's being appended? If not you'll need to reverse engineer the javascript link clicks as the outgoing clicks aren't getting decorated:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#linkerparam
Comment: I have confirmed as per your suggestion that this is indeed the case. The iframe URL is:  https://kiosk.checkoutwebsite.com/46837?&_ga=1.39169251.1548018169.1423221060 which seems to be precisely what you were referring to I believe?


Poisson distribution marginal probability of sufficient statistic

Question: I am self studying a theoretical statistics course I found online.
There is a question to show that for $(X_1, ... X_n)$ i.i.d Poisson variables with parameter $\theta$, the statistic $T=\sum_{i=1}^N X_i$ is sufficient. Now I know there are many answers online to show this, which I (think I) understand, but my question is around finding the marginal distribution $\mathcal{P}_{\theta}(T=t)$ and not actually about showing sufficiency.
I specifically want to know if there is a way to do this by marginalizing over $x$ WITHOUT using the fact that
the poisson distribution of a sum of $n$ poisson variables with parameter $\theta$ is a distribution $Po(n\theta)$.
So I believe that:
\begin{align}
\mathcal{P}_{\theta}(X=x, T=t) &= \mathcal{P}_{\theta}(X=x)I\{T(x)=t\} \\
&= I\{T(x)=t\}\prod_{i=1}^n \frac{\theta^{x_i}e^{-\theta}}{x_i!} \\ &= I\{T(x)=t\}\theta^t e^{-n\theta}\prod_{i=1}^n \frac{1}{x_i!}
\end{align}
And have seen that
\begin{align}
\mathcal{P}_{\theta}(T=t) &= \frac{n^t\theta^t e^{-n\theta}}{t!}
\end{align}
In the lecture notes I am following, they do something similar with a Bernoulli distribution and they marginalise over the possible outcomes of $x$ by multipliying the corresponding Bernoulli joint distribution by $\begin{pmatrix}n \\ t \end{pmatrix}$ to get the marginal distribution of $\mathcal{P}_{\theta}(T=t)$. My understanding of this is that there are this many ways to get $t$ successes in $n$ trials. (Sorry if including this is confusing but it is the rationale for why I am posing this q).
Hence, I feel like in the Poisson case it has something to do with the fact that the number of arrangements of the set of $t$ objects containing $n$ distinct elements $a_i, a_2 ... a_n$ with $x_i$ copies of element $a_i$ (s.t $\sum_i^n x_i = t$) is $\frac{t!}{\prod_{i=1}^n x_i!}$.
So if the total number of possible arrangements of the sample space is $n^t$ (as there are $n$ choices for $t$ total objects) then I see that the probability of such an event, $\Omega$ is:
$$
\mathcal{P}_{\Omega} = \frac{t!}{n^t\prod_{i=1}^n x_i!}
$$
Now I see that multiplying $\mathcal{P}_{\theta}(X=x, T=t)$ by $\frac{1}{\mathcal{P}_{\Omega}}$ gives the desired outcome however I don't understand why the inverse, and I don't really understand why you would divide by $n^t$ anyway as this wasn't done in the Bernoulli example (i.e it was not divided by $2^t$).
Can someone explain if my reasoning about the arrangements is correct, and if so where my logic is failing around taking the inverse/dividing by $n^t$?
If my reasoning is incorrect is there a way to marginalize over $x$ without using/showing the fact that the poisson distribution of a sum of $n$ i.i.d variables from $Po(\theta)$ is a distribution $Po(n\theta)$?
Thanks!
Answer: Since
\begin{align}
\mathcal{P}_{\theta}(X=x, T(X)=t) = \mathbb I\{T(x)=t\}\theta^t e^{-n\theta}\prod_{i=1}^n \frac{1}{x_i!}
\end{align}
by marginalisation
\begin{align}
\mathcal{P}_{\theta}(T(X)=t) &= \sum_{x;\,T(x)=t}\theta^t e^{-n\theta}\prod_{i=1}^n \frac{1}{x_i!}\\
&= \theta^t e^{-n\theta}\sum_{x;\,\sum_i x_i=t}\,\prod_{i=1}^n \frac{1}{x_i!}\\
&= \theta^t e^{-n\theta}\sum_{x;\,\sum_i x_i=t}\,\frac{t!}{t!}\prod_{i=1}^n \frac{1^{x_i}}{x_i!}\\
&= \frac{1}{t!}\theta^t e^{-n\theta}\sum_{x;\,\sum_i x_i=t}\,{t \choose x_1 \cdots x_n}\prod_{i=1}^n 1^{x_i}\\
&= \frac{1}{t!}\theta^t e^{-n\theta}(1+\cdots+1)^t\\
&= \frac{1}{t!}\theta^t e^{-n\theta}n^t
\end{align}
Comment: Awesome thank you, just fyi for anyone else the lines from 4-5 use the [multinomial theorem](https://en.wikipedia.org/wiki/Multinomial_theorem)


Views complex sort

Question: I have a Drupal view which displays 10 items. I have 3 items which need to remain at the top of the list, which i have done by making them sticky. These need to be in a particular order. The the rest of the list then needs to be in alphabetical order. How can I do this?
Here are my current view settings:
<code>Title
Title: None
Format
Format: Unformatted list | Settings
Show: Fields | Settings
open

    Add

Fields
Content: thumb
Content: Title
Content: Body
Content: Link
open

    Add

Filter criteria

Content: Published (Yes)
Content: Type (= Mineral)
Content: Title (!= others)
open

    Add

Sort criteria
Content: Sticky (desc)
Content: Title (asc)
Block settings
Block name: minerals
Access: Permission | View published content

    Add

Header

    Add

Footer
Pager
Use pager: Display a specified number of items |  items
More link: No
Advanced

    Add

Contextual filters

    Add

Relationships

    Add

No results behavior
Exposed form
Exposed form style: Basic | Settings
Other
Machine Name: block
Comment: No comment
Use AJAX: No
Hide attachments in summary: No
Hide contextual links: No
Use aggregation: No
Query settings: Settings
Field Language: Current user's language
Caching: None
Link display: None
CSS class: None
Theme: Information
Block caching: Do not cache
</code>
Comment: Can you also mention how your result coming up now?
Comment: the 3 sticky nodes are at the top of the list, in alpha order and then the remainder of the list is alpha. I need the 3 sticky nodes to be in a different order. Can i apply  a sort to only them?
Answer: If you ask about how to show the sticky ones in a particular order, you can add anonther custom field (e.g. "position") to the content-type. Define a number for the position and add another filter criteria for this custom field.
The sort criteria for the position should be set before the other sort filters.

Views complex sort


Hints to solve: $(5\cdot 3^{m})+4=n^{2}$

Question: Consider all possible integers $n \geq 0$ such that $$(5\cdot 3^{m})+4=n^{2}$$ holds for some corresponding integer $m \geq 0$. Find the sum of all such $n$.
My effort:
$5\cdot 3^{m}$ means number end in $5$ , $+4$ means number end in $9$. So perfect square should have $9$ as last digit, so $n$ can be $3$ or $7$. Hence sum equal to $10$.
Please tell is this correct? this was hit and trial kind stuff, please provide method which not include hit and trial.
Answer: Hint: $ 5 \cdot 3^m = n^2 - 4 = (n-2)(n+2) $. What can you say about $ n-2 $ and $ n+2 $, given this equality?
Comment: @Ayushakj Because $n+2$ and $n-2$ differ by $4$, they can't both be multiples of $3$, which means that all the factors of $3$ have to be together.  That means either the factorization is $1$ and $5\cdot 3^m$ (in which case $m=0$), or $5$ and $3^m$, which implies $m=0$ or $2$.  When you said one should be $5$, I thought you meant one of the factors was equal to $5$, which is true in all of the solutions.
Comment: one should be $5$ at least.
Comment: Yes - now conclude.
Comment: @Ayushakj Yes, that gives two cases.
Comment: @Aaron got it !
Comment: but tell me one thing that why we should assume two equalities to be prime (n+2) and (n-2), to have factor of $5$ we can have $15$, $25$.... so on as the value of n+2 or n-2.


C++ winsock send arrays

Question: I'm trying to send arrays trough the net with winsock2. Now, i read microsoft disabled sending raw pointers, but you can still send un-edited binary data by casting the pointer to char*:
send(rsock, (char*)&counter, len, 0);

However, the problem is putting the data back in an array when it reaches the client. here, pass is the binary data. That's how I do fot integers, bools and doubles.
    recv(sock, pass, sizeof(int), 0);
    refresh =  (int((void*)&pass));
    recv(sock, pass, sizeof(bool[4800][254]), 0);
    **key = (bool)&pass;
    recv(sock, pass, sizeof(double[4800][254]), 0);
    **mil = (double)&pass;

Integers aren't arrays, while bool and doubles are stored in 2 dimensional arrays. Now, the compiler says this code works for int and bool but for doubles it says "'type cast' : cannot convert from 'char **' to 'double'"
 "invalid type conversion" even though I'm trying to put raw data in it. Have I done something wrong? Is there any other workaround to send arrays? Thanks in advance.
EDIT: also, I still haven't tried the code with another PC, so I highly doubt the conversion for ints and bools is done right.
Comment: Where on earth did you 'read [that] Microsoft disabled sending raw pointers'? You can send anything you like. Whether it *means anything* at the receiver is another question.
Comment: You misread, or misremembered. There's no match for 'Microsoft disabled sending raw pointers' on this entire site other than this question. Your problem is to do with *compilation errors,* not 'Microsoft disabled sending raw pointers'.
Comment: I didn't recall on what exact question I read it, but it was on stack overflow - and by the way, *send(rsock, &counter, len, 0);* with a double doesn't compile, I have to use *send(rsock, (char*)&counter, len, 0);*. There is also a multiplication symbol after char, but the website format doesn't show it.
Comment: Well considering I'm misremembering, have you got any clues for the send problem or for the cast problem? Thanks.
Comment: There is no 'send problem'. There is a cast problem, which is just C programming, and there is a problem with your `recv()` code: you're assuming that it fills the buffer. It isn't contracted to do that. You need to store the result of `recv()` into a variable and check it for -1, check it for 0, and otherwise use it as the number of bytes received; if that's enough for the next piece of code, OK, otherwise loop.
Answer: Microsoft didn't disable sending anything. The fact is that sending a pointer will simply be of no use to the remote peer. A pointer is simply a memory address, and it is useless to know the address if the information is not there.
The problem you are probably facing is that this array is too big to fit the send buffer, that by default can hold only 64KB.
Pay attention to the return values of send() and recv() to know how much data you actually read/sent in that transaction. It will not always be the same size you told the function to do, as it is often split in pieces smaller than 4KB. You will have to manage the transmission of this information in pieces to fill your entire array.
Comment: You are right, but the problem i'm talking about in this question is not that the pointer doesn't reach the client, I was looking for a way to pull the pointer back into the array, seen that the one i tried works for bools but not for doubles for some reason unknown to me.
Comment: @Alex The issue that you're seeing is one where you believe that you can turn pointers into data.  That is not the way it works.  What you do is send the *data* (not the pointer) over the socket, and then the receiver has to take that data and recreate the data structure(s) on that side.  Pretend you're trying to save data to a file, and then in another program, read the file and reconstitute the data.  How would you do that?  It's the same principle.
Answer:
Have I done something wrong?

Well...

by default, send and recv don't guarantee to return only after all the buffer you've supplied is either sent or received; they may return as soon as they've enqueued a bit more data for sending, or after receipt of a bit more data that you might be able to process... the buffer size supplied is just an upper limit to your request, not a minimum.  If you want to ensure recv doesn't return until the full buffer has been populated, add the MSG_WAITALL flag as a final parameter.  For send you must loop sending further parts of your output buffer.
check you return codes... send and recv tell you of errors and have pretty little numbers that give you clues as to the cause and resolution
"the compiler says this code works" - no it doesn't... it says your code requests something it's prepared to compile, full of casts that it isn't meant to try to verify, most of which will crash at runtime

Then there's this:
recv(sock, pass, sizeof(int), 0);
refresh =  (int((void*)&pass));
recv(sock, pass, sizeof(bool[4800][254]), 0);
**key = (bool)&pass;
recv(sock, pass, sizeof(double[4800][254]), 0);
**mil = (double)&pass;

I'm not even going to begin to say what's wrong with all that... let's just talk about what might work (might being discussed below):
template <typename T>
void get(int sock, T& t)
{
    if (recv(sock, (char*)&t, sizeof t, MSG_WAITALL) != sizeof t)
        throw std::runtime_error("error while reading data from socket");
}

int refresh;
get(refresh);

bool key[4800][254];
get(key);

double mil[4800][254];
get(mil);

If your sending and receiving systems, compilers, compiler flags, executables etc. differ in any way then this may not work anyway as:

up until C++03 compilers weren't required to use any particular type to store bool, so who knows if your sending and receiving side will match
big and little endian systems have different byte ordering which can break naive binary transfers like this
the size of int may vary

Ultimately, a more robust way to do this would be to use the boost serialisation library.
Comment: I understand your disappointing, but this wasn't supposed to be final code, i would have checcked the return as soon as the casts would have worked. Anyway, thanks, i'll follow your tips. Oh. And are you sure the boost serialization library would work woth socket?
Comment: @Alex: You're welcome.  Re boost serialisation and sockets - there's some answers/links in existing questions - e.g. [here](http://stackoverflow.com/questions/4087263/serialize-and-send-objects-by-tcp-using-boost).  Cheers.

C++ winsock send arrays