Tratamento de query grafico via javascript

Question: Boa tarde, eu tenho uma query em meu sistema onde ela gera um grafico.

Na primeira legenda do grafico está como undefined, estava tentando refazer a minha query  só que não estava vindo com os mesmos valores.
Eu queria retirar a opção undefined que está em minha legenda deixar somente as três ultimas opções em meu grafico.
Queria saber se alguém pode me ajudar a fazer um tratamento via javascript, irei postar meu codigo abaixo.
Controller.cs

 [HttpPost]
        public async Task<JsonResult> GetProcessosByDeslocamentoPedidoMedico()
        {
            SetaTipos();
            var dados = string.Empty;

            if (_tipo == 2)
                dados = $"AND p.RegiaoId = {_dado}";
            else if (_tipo == 3)
                dados = $"AND p.UserId = {_dado}";

            var query = $@"SELECT Title, SUM(Count) AS Count
                        FROM (
                            SELECT
                            CASE WHEN EstadoId = EstadoIdAutor AND EstadoId = Meddoc1 AND EstadoId = Meddoc2 AND EstadoId = Meddoc3 THEN
                                 'Sem deslocamento'
                                 ELSE CASE WHEN EstadoId = EstadoIdAutor AND (EstadoId = Meddoc1 OR EstadoId = Meddoc2 OR EstadoId = Meddoc3) THEN
                                    'Fora de domicílio do autor'
                                    ELSE CASE WHEN EstadoId <> EstadoIdAutor OR (EstadoId = EstadoIdAutor AND (EstadoId <> Meddoc1 AND EstadoId <> Meddoc2 AND EstadoId <> Meddoc3)) THEN
                                        'Mais de um deslocamento'
                                    END
                                 END
                            END AS Title,
                            COUNT(*) AS Count
                            FROM jud_Processos
                            WHERE Status = 1 AND Trancado = 1 {dados}
                            GROUP BY EstadoId, EstadoIdAutor, Meddoc1, Meddoc2, Meddoc3
                        ) Sub
                        GROUP BY Title;";

            var total = await _graficosService.GetProcessos(query);

            return Json(total, JsonRequestBehavior.AllowGet);
        }

meu javascript

case "9":
  Post("GetProcessosByDeslocamentoPedidoMedico", "", text);
  break;

Eu queria retirar a opção undefined que está em minha legenda
Comment: as informações do `JavaScript` que você forneceu são insuficientes para uma resposta.
Comment: Então é somente isso que tem em js não tem como fazer isto pela query? eu fiz uma de cada usei um union all porem os resultados diferentes
Answer: Pode filtrar os resultados com uma query superior
SELECT TOP 3 * FROM (
SELECT Title, SUM(Count) AS Count
                        FROM (
                            SELECT
                            CASE WHEN EstadoId = EstadoIdAutor AND EstadoId = Meddoc1 AND EstadoId = Meddoc2 AND EstadoId = Meddoc3 THEN
                                 'Sem deslocamento'
                                 ELSE CASE WHEN EstadoId = EstadoIdAutor AND (EstadoId = Meddoc1 OR EstadoId = Meddoc2 OR EstadoId = Meddoc3) THEN
                                    'Fora de domicílio do autor'
                                    ELSE CASE WHEN EstadoId <> EstadoIdAutor OR (EstadoId = EstadoIdAutor AND (EstadoId <> Meddoc1 AND EstadoId <> Meddoc2 AND EstadoId <> Meddoc3)) THEN
                                        'Mais de um deslocamento'
                                    END
                                 END
                            END AS Title,
                            COUNT(*) AS Count
                            FROM jud_Processos
                            WHERE Status = 1 AND Trancado = 1 {dados}
                            GROUP BY EstadoId, EstadoIdAutor, Meddoc1, Meddoc2, Meddoc3
                        ) Sub
                        GROUP BY Title
) TB_AUX;

Comment: Funcionou cara, muito obrigado


Generic Typescript function to check for duplicates in an array

Question: I'm trying make a generic Typescript function to check if an array contains duplicates. For example:
interface Student {
  name: string;
  class: string;
};

const students: Student[] = [
  { name: 'John Smith', class: 'Science' },
  { name: 'Edward Ryan', class: 'Math' },
  { name: 'Jessica Li', class: 'Social Studies'},
  { name: 'John Smith', class: 'English'}
];

That is the data.
This is what I want to do with the data:
const registerStudents = async (students: Student[]): Promise<void> {

  checkDuplicate(students, existingState); //This is the function I want to build

  const response = await axios.post('/students/new', students)
  existingState.push(response); //pushes newly registers students to the existing state
};

Regarding the checkDuplicate(), I want to make it a generic function, but I'm struggling with the logic.
export const checkDuplicate = <T>(items: T[], existingState: T[]): void {
  //checks if the items have any duplicate names, in this case, it would be 'John Smith', and if so, throw an error

  //Also checks if items have any duplicate names with the existingState of the application, and if so, throw an error

  if (duplicate) {
    throw new Error('contains identical information')
  };
};

It's a little bit complex and I haven't been able to figure out the logic to work with typescript. Any advice on how I can implement this would be appreciated!
Comment: Does [this](https://tsplay.dev/wQKnvm) meet your needs?  If so, I could write up an answer.  If not, could you explain how you'd implement `checkDuplicate()` in pure JavaScript or if it were not generic?  Then I could maybe give it types.
Comment: Oh wow this is perfect, I need to take sometime to understand this code but it works perfectly.
Comment: Okay then I will write up an answer with an explanation when I get a chance.
Answer: One reasonable way to approach this is to have checkDuplicate() take a single array items of generic type T[], and another array keysToCheck of type K[], where K is a keylike type (or union of keylike types) and where T is a type with keys in K and whose values at those keys are strings.  That is, the call signature of checkDuplicate() should be
declare const checkDuplicate: <T extends Record<K, string>, K extends PropertyKey>(
    items: T[],
    keysToCheck: K[]
) => void;

This function should iterate over both items and keysToCheck, and if it finds an item where a property is the same string as the same property in a previous item, it should throw an error.
If you had such a function, you could write the version which accepts students and existingState, two arrays of Student objects, like this:
function checkDuplicateStudents(students: Student[], existingState: Student[]) {
    checkDuplicate([...students, ...existingState], ["name", "class"]);
}

where we are spreading the students and existingState arrays into a single array to pass as items to checkDuplicate(), and since we are checking Student we are passing ["name", "class"] as keysToCheck.

Here's a possible implementation of checkDuplicate():
const checkDuplicate = <T extends Record<K, string>, K extends PropertyKey>(
    items: T[],
    keysToCheck: K[]
): void => {
    const vals = {} as Record<K, Set<string>>;
    keysToCheck.forEach(key => vals[key] = new Set());
    for (let item of items) {
        for (let key of keysToCheck) {
            const val: string = item[key];
            const valSet: Set<string> = vals[key]
            if (valSet.has(val)) {
                throw new Error(
                    'contains identical information at key "' +
                    key + '" with value "' + val + '"');
            };
            valSet.add(val);
        }
    }
}

The way it works is that we create an object named vals with one key for each element key of keysToCheck.  Each element vals[key] is the Set of strings we have already seen for that key.  Every time we see a string-valued property val with key key in any item in the items array, we check whether the set in vals[key] has val.  If so, we've seen this value for this key before, so we throw an error.  If not, we add it to the set.
(Note that it's possible to replace Set<string> with a plain object of the form Record<string, true | undefined>, as shown in Mimicking sets in JavaScript? , but I'm using Set here for clarity.)

Okay, let's test it against your example:
checkDuplicateStudents(students, []);
// contains identical information at key "name" with value "John Smith"

Looks good.  It throws an error at runtime and properly identifies the duplicate data.
Playground link to code
Comment: Great answer and thanks for taking the time to explain your logic!


How can I detect whether I have CREATE TABLE permission?

Question: How can I programmatically detect whether the current user has CREATE TABLE permissions in the current database?
I'm writing a tool that will do some lengthy processing, and then create database tables to save the results. I only want to create the tables if the processing runs to completion. But if the user won't be able to create the tables, there's no point spending time processing. I'd like to detect the permission issue and fail fast.
This will be from a C# application, and I can't assume that any special libraries will be installed, which probably rules out SQL-DMO and SMO. If there's an easy way to check permissions with a T-SQL query/script, that would be ideal.
Answer: Why not create the table first, and if that fails, go no further?
That said, you can also use the following function HAS_PERMS_BY_NAME:
SELECT HAS_PERMS_BY_NAME(db_name(), 'DATABASE', 'CREATE TABLE');

More information here:
http://msdn.microsoft.com/en-us/library/ms189802.aspx
Comment: +1, this is a more tenable solution than use of PERMISSIONS function.


Unable to convert BufferedImage to image java

Question: Im trying to set a image to my image view, and tried converting the image from a buffered image. However, before compiling I get an error saying " Type Mismatch: Cannot convert writeableImage to image". Can anyone explain why this is so and how to resolve this problem? Thank you :)
  BufferedImage bufferedImage = ImageIO.read(file);
  Image image = SwingFXUtils.toFXImage(bufferedImage, null);
  myImageView.setImage(image);

Answer: You probably imported java.awt.Image instead of javafx.scene.image.Image.
Also, depending on context, it may be better to directly construct an Image via new Image(String url).
Comment: tried doing this and i get a awt image. I'm trying to work with both javafx.scene.image.Image and java.awt.Image at the same time
Comment: @DavidLie Not sure what you are talking about. You need to give a [mcve].
Comment: nvm! I just rewrote the whole thing without using java.awt.Image! Thank you!
Comment: @DavidLie If you do need to use types of the same name in the same file, just import one and use the fully qualified type name, e.g. `javafx.scene.image.Image image = SwingFXUtils.toFXImage(bufferedImage, null);`


Remove duplicate words and just print lines in which this occurs

Question: I have a challenge to look in a file if a sentence contains 2 identical consecutive words. If so, you print the word; otherwise, you don't print the sentence.
Example:
abc2 1 def2 3 abc2
F4
--------------
dea 123 123 zy45
12 12
abc cd abc cd
xyz%$@! xyz%$@! kk
xyzxyz
abc h h h h

After running the program the output will be:
dea 123 zy45
12
xyz%$@! kk
abc h h h
3

This is what I have so far:
sed '/\([^\([^ ]\+\)[ ]\+\1]\)/d' F4 >|tmp

I got this so far but this is only separating between the sentences that have the double word and sentences that don't.
Comment: Where is coming from the 3 in the bottom of the expected output?
Comment: In order to rewrite the line to get rid of one of the duplicate words, you need to use a `s///` command, not `d`.
Comment: You seem to have an extra `[` and `]` in your command, so I don't see how it does what you say it does.
Comment: You write `If so, you print the word;`. The subject and output is different, delete that word?
Answer: Your sed expression was quite accurate. However, it needed some mangling to make it work:
$ sed -nr 's/\b(\S+)\s+\1(\s|$)/\1/p' file
dea 123 zy45
12
xyz%$@! kk
abc h h h

The idea is the one  you already implemented: match a given word with [^ ] and see if you match it again with \1. What I added is all of this to be replaced with \1 so the repeated block disappears.
Instead of [^ ] it is also useful to use \S and instead of [ ], \s. Note also the usage of \b as a word boundary to prevent false positives like fedorqui qui and the usage of \1(\s|$) to prevent other false positives like hello helloa (thanks WalterA for the examples!). Note the usage of \s|$ to match either a space or the end of the line; \b matches any not-word character, which makes it not useful for the case with xyz%$@! kk.
To prevent all lines to be printed, we use sed -n. This way, we just print (with p) those that go through the regular expression that was defined.
Note the usage of -r to get rid of all those escaping to capture groups. Without it, the command would be:
sed -n 's/\b\([^ ]\+\)[ ]\+\1/\1/p' file

Let's test it with a more comprehensive input:
$ cat a
abc2 1 def2 3 abc2
F4
--------------
dea 123 123 zy45
12 12
abc cd abc cd
xyz%$@! xyz%$@! kk
xyzxyz
fedorqui qui
hello helloa
abc h h h h
$ sed -nr 's/\b(\S+)\s+\1(\s|$)/\1/p' a
dea 123zy45
12
xyz%$@!kk
abc hh h

Comment: You are matching a substring like `fedorqui qui`.
Comment: @WalterA very good point! Added a word boundary to prevent so. Thanks.
Comment: Your change with `\b` also fixed `abc cd abc cd` that was truncated into `abcd abc cd`. But how about the second string as a substring of the first like `walter waltera` ?
Comment: @WalterA and this was another good point that needed bigger weapons to address.
Comment: And how about `%$@!xyz %$@!xyz kk`? You explained that `\b` won't work at the end of the line, so it will cause problems too at the start of a word.
Comment: @WalterA I will go through this tomorrow.
Answer: I was looking for a sed solution that seemed to be easy. perhaps in this case awk is better (F4 is the inputfile):
awk '{
        for (i=2; i<=NF; i++) {
           if ($(i-1)==$i) {
              $i="";
              printf("%s\n", $0);
              break;
           }
        }
     }' F4

I am not complete happy with this solution, since it will leave a double FieldSep in $0 after deleting the doubled word, but literally the OP did not see that a space or tab should be deleted too.


Confused about Two Sample Proportion Tests - am I performing this correctly?

Question: I am dealing with an advertising dataset that includes hundreds of advertisements, how many clicks each ad receives, whether the amount of clicks received is considered "high" or "low" (determined by a methodology I don't have access to), and whether each ad includes the logo of the advertiser.
I am currently running a Two Tailed T-Test, but would also like to run a Two Sample Proportion Test to determine whether ads with the logo have a higher proportion of "high" click ads than ads without the logo. However, I am a bit confused on the formula to calculate Z Score.
I would really appreciate if somebody could let me know if the below methodology is accurate.

Also, please let me know if there are better statistical tests to run for a dataset like this. Looking for any and all suggestions! Thank you!
Answer: I can't read the computations you posted. This seems to be a test whether two binomial proportions are significantly different. To compute the denominator, two approaches are in common use.
[P-values below are for two-sided tests, of $H_0: p_1=p_2$ against $H_a: p_1 \ne p_2.$]
(a) Pool together observations from both groups, and successes from both groups, use
the ratio to estimate standard error. Rationale: under $H_0: p_1 = p_2.$
This method is shown here.
(b) Find the two $\hat p_i$s separately, use them to get the total
variance of $\hat p_1 - \hat p_2,$ and use that to estimate the standard error. Rationale: Separate standard errors are required for the CI, so
the CI 'matches' the test.
Below is output from Minitab, which gives a choice whether to use (a) or (b). Output is for method (b).
Test and CI for Two Proportions

Sample    X    N  Sample p
1        18   31  0.580645
2       273  337  0.810089

Difference = p (1) - p (2)
Estimate for difference:  -0.229444
95% CI for difference:  (-0.408126, -0.0507618)
Test for difference = 0 (vs ≠ 0):
  Z = -2.52  P-Value = 0.012

Fisher's exact test: P-Value = 0.005

Method (a) gives $z = -3.01$ and P-value 0.003, so I suppose you did computations for (a). [Fisher's exact test is based on a hypergeometric distribution.]
The procedure prop.test in R gives the following results (because of reasonably large sample sizes I did not use a continuity correction):
 prop.test(c(18,273), c(31, 337), cor=F)

        2-sample test for equality of proportions
        without continuity correction

data:  c(18, 273) out of c(31, 337)
X-squared = 9.0325, df = 1, p-value = 0.002652
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.40812588 -0.05076184
sample estimates:
   prop 1    prop 2
0.5806452 0.8100890

The corresponding $|z| = \sqrt{9.0325}=3.0039,$ in close agreement with Minitab's output for (a).


Guitar head with bass cabinet

Question: This may sound silly, I am considering using a guitar head with a 2x12 bass cabinet, is this a doable setup? The head can be 16, 8 or 4 ohms.
Comment: I am thinking of using a guitar head, sorry.
Answer: That will work, as long as the impedance (the Ohms) of the cabinet is equal to or more than the impedance of the head. What will be different about it is that a bass cabinet is usually intended to reproduce the full frequency response that is fed to it and therefore will likely sound more fizzy and or boomy than a guitar cabinet would.
Also pay very careful attention to the impedances because I think many bass cabinets are made with lower impedances than guitar cabinets. This is because bass heads are often solid state which can more easily be designed with arbitrarily low output impedances.
Comment: @Tim Many older amps are 16 Ohms. I wouldn't try to measure an output or input impedance. Both should be rated by the manufacturer and clearly labeled. I don't even know how one would measure them, especially the output impedance.
Comment: A meter can measure DC resistance but not nominal load impedance, which is a specific point on a curve. Cabinet manufacturers also may tweak rated load impedances to take into account quirks of their designs if a cabinet is overly resonant at any one frequency. You would have to do a sine wave sweep with a tone generator and measure the current through the load to get the impedance curve and then you could get a typical nominal impedance from that, but who has all that equipment and know-how? I just don't use cabinets that have lost their impedance labels.
Comment: My Hartke head is 8ohms, Trace and Ashdown both 4 ohms *minimum*, and I use them mainly with 8 ohm cabs. I'd say that as long as one isn't driving any where near flat out, it won't be a problem. I guess most guitar amps will be either 4 or 8 ohms. An obvious to do is to measure both !
Comment: True about output, but input is straightforward with a meter. Some cabs have the magical disappearing impedance plate.
Comment: Oh. It's just that every speaker I've checked comes max.+/- 10% of the marked impedance. Near enough for jazz...
Comment: Thank you Tim & Todd for your answers, and the ancillary conversations were quite interesting and informative. Cheers!
Answer: Back in the '60s, the favourite guitar rig was the Fender bass combo, so it's not a new idea. It will work fine, given Todd's caveats, but you may find it lacks a bit of top end. That will be very subjective - guitar, you,  style, and actual cab chosen.
Comment: All but three of the 2x12 bass cabinets on Sweetwater's web site have tweeters. Of those three, two of them specifically list a frequency response up to 16 kHz, which is more than an octave higher than a typical guitar cabinet that tops around around 5 kHz. The last one actually might have a tweeter that is just not discussed in the details, it's not clear. It seems to really be meant as a powered sub for a bass rig that also has a top in the form of a 2x10 with tweeter or something like that. An old used cabinet from the 70s probably doesn't have a tweeter. Modern ones usually do.
Comment: Basses need the high end for articulation. Guitars need the high end rolled off to make distortion not sound like a swarm of angry bees literally inside your ears. Guitar amps are really specialized and aren't good for anything else. As you say, keyboard and bass amps are often quite similar and one can be used for the other in a pinch. PA cabinets are much closer to keyboard/bass/acoustic cabinets but usually have even wider and more even frequency response and higher power handling, except for the low end models.
Comment: Wouldn't it have too much top end? My bass cab definitely has a lot more top end than any of my guitar cabs. I mean my bass cab even has a tweeter and my guitar cabs definitely don't.
Comment: Probably not with 2x12". I put a bullet in a 15" cab, but that was as much for keys as bass. My 4x10" has no tweeter, (although I did build the 2x10" with switchable tweeter), and sounds balanced with either guitar or bass. Must try it with keys!
Comment: Interesting. I'd have though, theoretically, that a cab designed for guitar would have a higher range (register, tessitura?!) than a bass cab, given the main difference between the two instruments. But properly crossed over, it might not make much difference. I do know that p.a. cabs (with tweeters) are great for both keys and bass.

Guitar head with bass cabinet


Show limit does not exist

Question: Show that this limit does not exist,
z is Complex number:
$\lim_{z \to -1 } \frac{1}{z^3}\sin (\frac{z}{z+1})$
Comment: How far did you get? where did you get stuck?
Comment: I think i find out how to show that. $2 \pi k = z/(z+1)$ so i will have the first series $Z_k$, that will give me $sin(2\pi k) = 0$
Comment: You should put your described attempt into the question. As you can see from my answer, it's not a bad start at all. Also, use `\sin` to get "$\sin$".
Answer: You want to find a sequence for $z \to -1$ such that $2πk = \frac{z}{z+1} = 1 - \frac{1}{z+1}$. Can you now? You basically have to choose a sequence for $k$ so that the required $z$ tends to $-1$. So the original expression is zero on this sequence for $z \to -1$.
You also want another sequence for $z \to -1$ such that the original expression does not tend to zero. You can use the same trick to make $\sin(\frac{z}{z+1}) = 1$ on that sequence. Then the expression would not tend to zero.

Show limit does not exist


Google Cloud Run not scaling up despite large backlog and available instances

Question: I am seeing something similar to this post.  It looked like additional detail was needed to answer that question, so I'm re-asking with my details since those details weren't provided.
I am running a modified version of the Google Cloud Run image processing tutorial example.
I am inserting tasks into a task queue using this create tasks snippet.  The tasks from the queue get pushed to my cloud run instance.
The problem is it isn't scaling up and making it through my tasks in a timely manner.
My cloud run service configuration:

I have tried setting a minimum of both 0 and 50 instances
I have tried a maximum of 100 and 1000 instances
I have tried --concurrency=1 and 2, and 8
I have tried with --async and without --async

With 50 instances pre-allocated even with concurrency set to 1, I am typically seeing ~10 active container instances and ~40 idle container instances. I have ~30,000 tasks in the queue and it is getting through ~5 jobs/minute.
My tasks queue has the default settings.  My containers aren't using a lot of cpu, but they are using a lot of memory.
A process takes about a minute to complete.  I'm only running one process per container instance.  What additional parameters should be set to get higher throughput?

Edit - adding additional logs
I enabled the logs for the queue, I'm seeing some errors for some of the jobs.  The errors look like this:
{
insertId: "<my_id>"
jsonPayload: {
@type: "type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog"
attemptResponseLog: {
attemptDuration: "19.453155s"
dispatchCount: "1"
maxAttempts: 0
responseCount: "0"
retryTime: "2021-10-20T22:45:51.559121Z"
scheduleTime: "2021-10-20T16:42:20.848145Z"
status: "UNAVAILABLE"
targetAddress: "POST <my_url>"
targetType: "HTTP"
}
task: "<my_task>"
}
logName: "<my_log_name>"
receiveTimestamp: "2021-10-20T22:45:52.418715942Z"
resource: {
labels: {
location: "us-central1"
project_id: "<my_project>"
queue_id: "<my-queue>"
target_type: "HTTP"
}
type: "cloud_tasks_queue"
}
severity: "ERROR"
timestamp: "2021-10-20T22:45:51.459232147Z"
}

I don't see errors in the cloud run logs.

Edit - Additional Debug Information
I tried to take the queue out of the equation to determine if it is cloud run or the queue.  Instead I directly used curl to post to the url.  Some of the tasks ran successfully, for others I received an error.  In the below logs empty lines are successful:
upstream connect error or disconnect/reset before headers. reset reason: connection termination

upstream connect error or disconnect/reset before headers. reset reason: connection termination
upstream connect error or disconnect/reset before headers. reset reason: connection termination
upstream connect error or disconnect/reset before headers. reset reason: connection termination

upstream connect error or disconnect/reset before headers. reset reason: connection termination

This makes me think cloud run isn't handling all of the incoming requests.

Edit - task completion time test
I wanted to test if the time it takes to complete a task causes any issues with CloudRun and the Queue scaling up and keeping up with the tasks.
In place of the task I actually want completed I put a dummy task that just sleeps for n seconds and prints the task details to stdout (which I can read in the cloud run logs).
With n set to 0, 5, 10 seconds I see the number of instances scale up and it keeps up with the tasks being added to the queue.  With n set to 20 seconds or more I see that less CloudRun instances are instantiated and items accumulate in the task queue.  I see more errors with the Unavailable status in my logs.
According to this post:
Cloud Run offers a longer request timeout duration of up to 60 minutes
So it seems that long running tasks are expected.  Is this a Google bug or am I missing setting some parameter?
Answer:
I do not think this is a Cloud Run Service problem. I think this is an issue with how you have Tasks setup.

The dates in the log entry look odd. Take a look at the receiveTimestamp and the scheduleTime. The task is scheduled for six hours before the receive time. Do you have a timezone problem?

According to the documentation, if the response_time is not set then the task was not attempted. It looks like you are scheduling tasks incorrectly and the tasks never run.

Search for the text The status of a task attempt. in this link:
Types for Google Cloud Tasks
Comment: The task creation code I'm using has an `in_seconds` parameter, which defaults to `180`, I set it to `None` and still see the time difference you are mentioning and the issue.  That is the only time component I am using on the tasks end.  Is it possible my Cloud Run image needs its time set correctly or could it be something in the Google Cloud infrastructure?
Comment: Also I had the task in both instances running for several hours, before turning on the logs, this could account for a several hour time difference.
Comment: I do not know the source of your time issue, just that there might be a problem there. The task did not run according to the log message.
Comment: Still seeing the `unavailable`. Here are the timestamps within a few minutes of sending new tasks to the queue, makes me think it is not a time difference:
`attemptDuration: "5.018117s"`, `retryTime: "2021-10-25T16:23:16.719135Z"`, `scheduleTime: "2021-10-25T16:19:00.328353Z"`, `receiveTimestamp: "2021-10-25T16:23:18.207261176Z"`, `timestamp: "2021-10-25T16:23:16.619301336Z"`


How to send request body along with boundary delimiter with the --data option in CURL

Question: I am new to curl.
I am using curl to upload a file using shell script.
I have to set the boundary delimiters explicitly and make an HTTP request by passing few fields.
I have couple of questions:

Is it possible to explicitly set boundary delimiters using GET method?
If yes, how?
If not then is it okay to use something like this in a POST method (check the arguments to the --data)

curl --data "--4ebf00fbcf09&AB=MSG&tableView=simple&orient=UP&x-895892-n=1
&x-128754-o=2&x-974573-p=1&client=browser&d-984303-s=create&--4ebf00fbcf09--"
--header "Content-Type: multipart/form-data; boundary=4ebf00fbcf09"
www.xyz.com
Many Thanks!
Answer: Those delimiters are used for multipart formposts, and curl will handle the delimiters automatically for you if you just make sure to use --form instead of --data.
The difference between curl's -d and -F is also explained in the curl book in section aptly titled "-d vs -F".
The manual way
If you truly want to set the boundary yourself, you need to build the entire formpost yourself as RFC1867 dictates.
A shell script to send the two values "name=foo" and "value=42":
<code>#/bin/sh
boundary="------------------------2100d47bb8839948"

# generate the body
echo "$boundary" > post
echo 'Content-Disposition: form-data; name="name"' >> post
echo "" >> post
echo "foo" >> post
echo "$boundary" >> post
echo 'Content-Disposition: form-data; name="value"' >> post
echo "" >> post
echo "42" >> post

## final boundary uses two extra dashes
echo "$boundary--" >> post

curl -H "Content-Type: multipart/form-data; boundary=$boundary" localhost --data-binary @post
</code>
Comment: Hi, But is there a way to set the delimiter explicitly?


The PUT request shows "HTTP/1.0 406 Not Acceptable: Missing node type"

Question: I have been trying to update a node data from the PUT request using cURL. bit due to some reason its showing "HTTP/1.0 406 Not Acceptable: Missing node type", I think the request is done proper but the data is not in proper format. Below is my code.
<code>        $newSketchBoard = array(
          'title' => $title,
          'type' => 'rest_sketchboard_user',
          'field_sketchboarduser_user_id[und][0][value]'=>$data['uid'],
          'field_sketchboard_paintjob_id[und]' => $data['paintjobid'],
          'field_sketchboard_template_id[und][0][value]' => $data['templateid'],
          'field_sketchboard_user_color_id1[und]' => $data['colorid1'],
          'field_sketchboard_user_color_id2[und]' => $data['colorid2'],
          'field_sketchboard_user_color_id3[und]' => $data['colorid3'],
          'field_sketchboard_user_color_id4[und]' => $data['colorid4'],
        );
        $entity_id=$sketchboard_data['entity_id'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER,true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 600);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIE, "{$session_cookie}");
        curl_setopt($ch, CURLOPT_PUT, 1);
        curl_setopt($ch, CURLOPT_URL, REST_BASE_URL."/node/{$entity_id}");

        $putString = $newSketchBoard;
        // Put string into a temporary file
        $putData = tmpfile();
        // Write the string to the temporary file
        fwrite($putData, $putString);
        // Move back to the beginning of the file
        fseek($putData, 0);
        curl_setopt($ch, CURLOPT_INFILE, $putData); // file pointer
        curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $newSketchBoard);
        $result = curl_exec($ch);
        print_r($result);
</code>
I also tried using the following code, but it's not working.
<code>$putString = json_encode($newSketchBoard);
$putString = http_build_query($newSketchBoard);
</code>
Answer: This ticket might be of some help.
http://drupal.org/node/1191786


Find the range of $(1/2)T^2$

Question: Let $V=M_{2\times 2}(\mathbb{R})$ be the vector space consisting of all $2 \times 2$ real matrices. Define a transformation $T : V → V$ by
$$
                 T(A)=AP-PA, \mbox{ where }
P = \begin{pmatrix}
0 & 1 \\
1 & 0 \\
\end{pmatrix}.
$$
The transformation $T^2: V → V$ is defined to be $T^2(A) = T(T(A))$, for each $A ∈ V$. Given $c ∈ \mathbb{R}$, the transformation $cT$ is defined to be $(cT)(A) = cT(A)$, for each $A ∈ V$.
Find the Range of $(1/2)T^2$.
This is a question my professor gave me for practice. There were some other questions about proving that $T$ is linear and stuff like that but I have no idea where to even begin with this.
(ps. I apologize if my formatting is not proper but this is the first time I have asked a question on this website).
Comment: A linear operator is determined by it values on a basis. A tour de force approach is to evaluate $(1/2)T^2$ on a basis and then extract a maximal linearly independent set from their images. Another way is to compute $T^2(A)= (AP-PA)P-P(AP-PA)=AP^2-PAP - PAP + P^2A$ Since $P^2=I$ then $T^2(A)=2PAP$. Therefore $(1/2)T^2(A)=PAP$. Observe that then $(1/2)T^2$ is the composition of left multiplication by $P$ and right multiplication by $P$. Since $P$ is invertible so are the sided multiplications. Therefore $(1/2)T^2$ is an isomorphism. The range is the whole space.
Comment: Bettybel's comment has a mistake, perhaps that is why it is hard to understand.
Comment: All: I deleted a subchain of comments as it got a bit too personal. Flags were also flying. There was also some collateral damage.
Comment: Bettybel's comment starts out fine, but then there are a few sign errors. I don't see a very clean way of doing this. We do see that $I$ and $P$ are both in $\mathrm{Ker}(T)$, and hence also in the kernel of $\dfrac12T^2$, but that doesn't really say very much about the range (other than that it is at most 2-dimensional). I would bail out and just do it the way in Zach Teitler's (+1) answer.
Comment: @Bettybel Sorry I don't quite understand your explanation. I get it up until the P^2 =I part but then you lose me at that point
Answer: Say $A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$. Then
$$
  T(A) = AP - PA = \begin{pmatrix} b & a \\ d & c \end{pmatrix} - \begin{pmatrix} c & d \\ a & b \end{pmatrix} = \begin{pmatrix} b-c & a-d \\ d-a & c-b \end{pmatrix} .
$$
Continuing,
$$
  T^2(A) = T(T(A)) = \begin{pmatrix} (a-d)-(d-a) & (b-c)-(c-b) \\ (c-b)-(b-c) & (d-a)-(a-d) \end{pmatrix} = 2 \begin{pmatrix} a-d & b-c \\ c-b & d-a \end{pmatrix} .
$$
So
$$
  \frac{1}{2} T^2(A) = \begin{pmatrix} a-d & b-c \\ c-b & d-a \end{pmatrix} .
$$
Before going on, I'd like to point out a slightly different approach suggested in the comments. Note that $P^2 = I$. Then
$$
  T^2(A) = T(A)P - PT(A) = (AP-PA)P - P(AP-PA) = AP^2 - PAP - PAP + P^2A = 2(A-PAP),
$$
so $\frac{1}{2} T^2(A) = A-PAP$ (not $\frac{1}{2}T^2(A)=PAP$). One can compute
$$
  A - PAP = \begin{pmatrix} a & b \\ c & d \end{pmatrix} - \begin{pmatrix} d & c \\ b & a \end{pmatrix}
$$
and get the same expression as before.
Now, having an expression for $\frac{1}{2} T^2$ is nice, but the question was to describe the range of this operator. What kind of description would you like? If you want equations for the range, some obvious ones pop out: set
$$
  \begin{pmatrix} a-d & b-c \\ c-b & d-a \end{pmatrix} = \begin{pmatrix} x & y \\ z & w \end{pmatrix},
$$
then it's clear that $x+w = 0$ and $y+z = 0$. It's clear that these equations are independent. You already know that $\frac{1}{2} T^2$ is linear (right?) so its range is a linear subspace in $M_{2\times 2}(\mathbb{R})$, which is $4$-dimensional. Since we've written $2$ independent equations, we know the range has dimension at most $2$. A priori, however, there could be additional equations.
To see that there are no other equations we could, for instance, write down $2$ independent elements of the range, such as $\begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}$ and $\begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix}$ (can you see which matrices $A$ give these as $\frac{1}{2}T^2(A)$?).
So the range is at most $2$-dimensional (because we have $2$ equations and a $4$-dimensional ambient space) and it is also at least $2$-dimensional (because we have these $2$ independent elements). We can conclude that the range is precisely $2$-dimensional, spanned by the two elements given above. And that gives you a second description of the range, if you need a basis for it rather than equations.
For example, having this basis makes it very easy to check that $\frac{1}{2} T^2$ is a projection onto its range.
Comment: This is very helpful but I'm not quite sure how you got the equations x+w =0 and y+z = 0
Comment: If $x=a-d$ and $w=d-a$, then $w=-x$, $x+w=0$. I'm using $x,y,z,w$ as the coordinates in $M_{2\times2}(\mathbb{R})$.

Find the range of $(1/2)T^2$


Executar PHP sem atualizar toda a página

Question: Meu código é esse:
<?php
$buscarusuario=$pdo->prepare("SELECT * FROM top5 WHERE status = 'ativo' ORDER BY colocacao ASC");
$buscarusuario->execute();

// Exibir com Fetch_Obj
$linha=$buscarusuario->fetchAll(PDO::FETCH_OBJ);
foreach ($linha as $listar) {

?>

      <?php echo $listar->colocacao; ?>

<?php echo $listar->cantor; ?>

<?php echo $listar->titulo; ?>

        <a href='extra/likes.php?id=<?php echo "".$listar->id.""; ?>' alt="Gostei">

<?php } ?>

    <meta charset="UTF-8">
    <title>Inserir dados PDO</title>

<?php
// Incluindo arquivo de conexão
include("conexao.php");
$pdo = conectar();
// Recuperando valores
$id = $_GET['id'];

// Realizando consulta
$atualizartop=$pdo->prepare("UPDATE top5 SET likes = likes +1 WHERE ID='$id'");
$atualizartop->bindValue(":id",$id);
$atualizartop->execute();
if($atualizartop->rowCount() > 0):
    echo "Obrigado por votar!";
else:
    echo "Desculpe, mas ocorreu algum erro.";
endif;

?>

Gostaria de saber se ao clicar no link para votar teria como retornar um "Obrigado por votar!" sem ter que atualizar para a página que tem a função php.
Se alguém souber como ajudar agradeço.
Comment: Para retornar essa mensagem, tem que ter a certeza de que a gravação foi correta. Para isso, tem que ter o script da gravação no início do programa, não no fim, onde recebe uma mensagem e em uma div receba essa mensagem.
Answer: Coloque um id na sua imagem com o valor like. Em um arquivo de extensão .js, vai esse código:
$("#like").click(function(){
//evento de click sobre a imagem

    var id = $(this).attr('id'); //aqui você pegao valor do atributo id
    $dados['id'] = id; //esse array será passado para o php só que por POST ao invés de GET

    $.ajax({

        url: "incrementaLike.php", //Aqui vai o nome do seu arquivo PHP
        type: "post",
        async: true,
        data: $dados,
        cache: false
     })
     .done(function(data){

        //neste o ponto, o código php já foi executado e voltou para aqui se tudo ocorreu sem erro
        console.log(data);
     })
    .fail(function(){

        //vem para cá se algum erro ocorreu
        console.log("Deu alguma errada no php");
    });
});

Você deve separar seu PHP em um arquivo separado e o caminho para este vai no atributo url.
No caso eu coloquei um arquivo chamado incrementaLike.php na mesma pasta do seu html e também dos arquivos js.
incrementaLike.php
<?php

include("conexao.php");
$pdo = conectar();
// Recuperando valores
$id = $_POST['id'];

// Realizando consulta
$atualizartop=$pdo->prepare("UPDATE top5 SET likes = likes +1 WHERE ID='$id'");
$atualizartop->bindValue(":id",$id);
$atualizartop->execute();
if($atualizartop->rowCount() > 0):
    echo "Obrigado por votar!";
else:
    echo "Desculpe, mas ocorreu algum erro.";
endif;

?>

Comment: Desculpe-me, acabei esquecendo, no caso você quer mostrar uma mensagem, você deve mostrar dentro do .done(), caso alguma coisa ocorra de errado, não em termos lógicos no PHP, a função vai para o .fail().
Comment: Olá Leandro. Agradeço sua resposta, mas eu infelizmente fiquei boiando aqui pois não manjo muito de javascript. Teria como você fazer um exemplo para que possa entender? No código eu quero somar o valor de uma coluna ao clicar na imagem "like". Queria entender como adaptar a essa função. Desde já obrigado.
Comment: Tudo bem, primeiro baixe esse arquivo https://code.jquery.com/jquery-3.1.0.min.js e inclua ele na sua página, é uma biblioteca chamada JQuery.
Comment: Já tenho ela no meu código. Pois algumas funções do site necessitam da mesma. O problema é que não entendo muito bem como funciona o javascript
Comment: Alterei a resposta, troquei só o get pelo post no seu PHP
Comment: Meu amigo você me deu uma luz enorme. Entendi como é pra funcionar o javascript agora. Porém quando cliquei na imagem não aconteceu nada. Coloquei o id="like" na imagem e retirei o  do link que estava utilizando para atualizar. O que pode ter ocorrido?
Comment: Se você tem um link que engloba sua imagem na hora que você clica ele executa o evento padrão do link ignorando o evento da imagem, boa sorte com as suas ideias!
Comment: Sempre lembre de finalizar a questão para ajudar a comunidade StackOverflow


Allow NestedScrollView to scroll completely inside a BottomSheet with a Custom Behaviour

Question: I have a bottom sheet with a child NestedScrollView. I want to scroll the entire NestedScrollView all the way to bottom without sliding the bottom sheet to up side.
If user wants the bottom sheet to go all the way up they have to use the anchor no other way.
Now when use start scrolling down, after the entire content is scrolled i want the bottom sheet to hide on next scroll action.
As default bottom sheet behavior doesn't allow that  I have created my own custom behavior and overidden the onNestedScroll() and onNestedPreScroll().
There are two issue I am facing:
1 : The Nestedscrollview scrolls partially and stops because with respect to bottom sheet it sees there is no more content to scroll. I have to slide up the sheet in order to see the rest content. Means I cannot see the last item inside the NestedScrollView without expanding the sheet.
2: How can I detect whether the NestedScrollChild is scrolling up or scrolling down. Because I want the BottomSheet to intercept events once the scrollview finished scrolling its content. I tried onNestedPreScroll but seems like it is not necessarily triggered every time a child is being scroll. Is attaching a listener to scrollview is the only solution?
Link to see the issue in GIF
https://photos.app.goo.gl/RPmDFEtR9TbGHrau6
Below is my layout and behavior class.
<?xml version="1.0" encoding="utf-8"?>

<com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
    />
</com.google.android.material.appbar.AppBarLayout>

  -->

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/bs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_sheet_background_shaded"
        android:elevation="10dp"
        app:behavior_hideable="false"
        app:behavior_peekHeight="35dp"
        app:behavior_fitToContents="false"
        app:layout_behavior="com.pyus13.bottomsheetsample.MyBottomSheetBehaviour">

    <ImageView
            android:id="@+id/anchor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="1dp"
            android:src="@drawable/bottom_sheet_anchor_holder"
            app:layout_constraintBottom_toTopOf="@+id/main_container"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <androidx.core.widget.NestedScrollView
            android:id="@+id/main_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:clipToPadding="false"
            android:elevation="5dp"
            android:gravity="center"
            android:paddingTop="20dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/anchor">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            <Button
                    android:id="@+id/change_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Title 1"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Title 2"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Page 3"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Page 4"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Page 5"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Change Title"/>

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

My Behavior class:
class MyBottomSheetBehaviour<V : View> @JvmOverloads constructor(
    context: Context? = null, attrs: AttributeSet? = null) : BottomSheetBehavior<V>(context, attrs) {

private var isScrollingDown = false

override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean {
    if (state == STATE_COLLAPSED || state == STATE_HALF_EXPANDED) {
        return false
    }
    return super.onInterceptTouchEvent(parent, child, event)
}

override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
    Log.d("Scroll", "OnNestedScroll EVent $child $target $axes $type")
    if (state == STATE_COLLAPSED || state == STATE_HALF_EXPANDED) {
        if (target.canScrollVertically(1) || target.canScrollVertically(-1)) {
            return false
        }
    }
    return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
}

override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout, child: V, target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
    isScrollingDown = dy > 0
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
}

}
I want a behavior like this if someone can help would really be appreciated.
Comment: Did you find a solution to this?
Comment: I have exactly the same problem. What did you do to fix it?
Comment: Did you find a solution for this?
Answer: I had nearly the same issue, though, the layout was a bit different and the behavior was to not intercept nested scroll, at all. Since there's still no answer, I think  it's worth to share my experience here.
So, my layout was the following:
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
    <LinearLayout
        android:id="@+id/bottomSheetLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:backgroundTint="@color/colorPrimary"
        android:orientation="vertical"
        app:layout_behavior=".view.FullNestedScrollBottomSheetBehavior"
        app:behavior_hideable="false"
        app:behavior_peekHeight="@dimen/bottom_sheet_min_height"
        >

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/anchorImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="-4dp"
            android:layout_gravity="center"
            android:paddingTop="4dp"
            android:src="@drawable/ic_baseline"
            android:tint="@android:color/white"
        />

        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout"
            app:tabTextColor="#b2ffffff"
            app:tabIconTint="@color/selector_status_tab"
            app:tabRippleColor="#42ffffff"
            app:tabSelectedTextColor="@android:color/white"
            app:tabIndicatorColor="@android:color/white"
            >
            ...
        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            />
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

And I ended up with the following approach:

Forbid the BottomSheet to accept nested scroll:

class FullNestedScrollBottomSheetBehavior<V: View>(
  context: Context,
  attrs: AttributeSet?
): BottomSheetBehavior<V>(context, attrs) {
  override fun onStartNestedScroll(...) = false
}

Dynamically change the height of the parent of the nested scroll element (in my case it's a ViewPager which holds NestedScrollView and RecyclerView) according to the real height of the BottomSheet's visible part:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  super.onViewCreated(view, savedInstanceState)
  bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
    override fun onStateChanged(bottomSheet: View, newState: Int) {}
    override fun onSlide(bottomSheet: View, slideOffset: Float) {
      val height = view.height - bottomSheet.top - anchorImage.height + 4.dp - tabLayout.height
      val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height)
      viewPager.layoutParams = params
    }
  })
}

Since the onSlide() will not be called until you drag your BottomSheet, the above logic should be duplicated to something like onViewCreated():

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  view.post {
    val h = view.height - bottomSheetLayout.top - anchorImage.height + 4.dp - tabLayout.height
    val p = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, h)
    viewPager.layoutParams = p
  }
  // here goes the code from the second point
}

I think the provided solution may be a good tip to implement the exact behavior required in the question.


why does my scanner(system.in) run twice

Question: I am new to java, been self teaching for the last week. I cannot find the reason why the if else statement runs twice. here is the whole code, I know is simple but still trying to learn.
package tickets;

import java.util.Scanner;

public class tickets {
public static void main(String[] args) {
//program designed to ask how many visitors
//are in a party of people and work out
//the total cost of the entry tickets.
    double adult = 12.50;
    double consession = 9.90;
    double child = 6.25;
    double percentage = 0.80;

System.out.println("please enter the amount of adults");
Scanner adult1 = new Scanner (System.in);

//adding code that would give a percentage discount for
//4 adults or more
{
if ( adult1.nextInt() >= 4
{
double adult2 =( adult1.nextInt() * percentage);
            }else {
double adult2 = (adult * adult1.nextInt());

System.out.println("please enter the amount of consessions");
    Scanner consession1 = new Scanner (System.in);
            double consession2 = (consession *consession1.nextInt());

    System.out.println("please enter the amount of children");
    Scanner child1 = new Scanner (System.in);
            double child2 = (child * child1.nextInt());

    System.out.println( "total"+" " + (adult2 +consession2 + child2) );
    System.out.println("hope you enjoy your visit today!");
    //woop woop it works!!!!!!!!!!
        }

    }
}

}
Comment: I guess the issue is not the `if` running twice, but you calling `nextInt` twice in the code.
Comment: Please post the rest of your code, and please fix the indentation on the formatting of the code.
Comment: Also, can you define "runs twice"? No line of code in your above example will get executed twice. What behavior or error are you seeing?
Comment: Just to add, it also doesn't run further than this point in the program if the number entered is 4 or greater.
Comment: here is the whole code.
Answer: The reason why your program asked for two inputs was because adult1 is the name of your scanner and in your if statement the condition was if the user input is >= 4 then take an Integer input again from the user and multiply that with percentage and store it in adult2, instead this should be done as follows
public static void main(String[] args)
{

  double adult = 12.50;
  double consession = 9.90;
  double child = 6.25;
  double percentage = 0.80;
  double adult2 = 0.0 // you dont need to redeclare below

  System.out.println("please enter the amount of adults");
  Scanner adult1 = new Scanner (System.in);

    // remove this unneccessary bracket {
    int num = adult1.nextInt();
    if ( num >= 4)
    {
      adult2 =( num * percentage);
    }
    else
    {
      adult2 = (adult * num);
    }
    System.out.println("Adult2 is " + adult2);
}

Answer: Store the int from the scanner and use that value in your ifs and calculations. You're calling nextInt() more than once and each time you get another int.
After you enter the if or else you will wait for more input of the integer type stopping the program.


HtmlUnit find the presence of a form for login

Question: For doing web scraping I'm trying to figure out when I get a specific url if the loging form is present:
<code>  if(!page.getForms().isEmpty()){
     HtmlForm loginForm = page.getFormByName("Valid");
     if(loginForm != null){
      //No valid session so login
       loginForm.getInputByName("usercode").setValueAttribute("MyUser");
       loginForm.getInputByName("userpwd").setValueAttribute("mypwd");
</code>
But this doesn't work because I got an exception when the form is not present.
Is there a way of detecting without exception the presence of the form?
Comment: What exception do you get and in which line? What value does loginForm hold when the form is not present?
Comment: In fact I got the normal exception of a non found element. So at the end I put a try catch around my code.
Comment: That makes sense. That way you can get rid of the `if` statements.
Answer: Further to the comments. I close the question and stop looking for strange things to do in place of of catching the exception.


Midi controlled metronome?

Question: For piano practice (Roland RD 88), I would like a metronome I can control with MIDI foot pedal/ switches (Roland FC-300). It's most important to me to adjust the tempo with an expression pedal CC, and it would be nice to switch between time signatures with PC messages.
My first preference is to control the RD-88 metronome directly, but I can't figure that out. Secondly, would be to run something on my MacBook, thirdly on an iPad.
Anybody have any ideas? Free and simple is best ;).
Later tonight I might set up some basic "songs" on Drambo (a bell for the bar start, a click for each beat), and then try to connect midi learn and tempo and program change input all together. I would rather do something with the MacBook because all the cables are hooked up to that anyway. If I have to buy Logic, I can do that but I don't really want to (yet).
Comment: I wonder the close votes, what's the motivation ? This does not ask for recommendation, but for a way to do something.
Comment: @Tom: I don't see much to justify your expectation, that if a device has a midi-plug then the metronome should be accessible through it.
Comment: How fluent are you with programming ? The keyboard metronome will probably accept a midi clock. So a small computer program using for instance midio for python can transform a bunch of CCs into a midi clock
Comment: Very fluent in programming but would rather not do it after work. I might handroll it, but I would use supercollider I think.
Comment: You might edit to say more about your ultimate goal of how you're going to use it. If the point is simply that your performance changes tempos, but your plan of the tempos you want and the bar numbers between them is set in stone, then a simple static click track will do. If you need to accommodate improv breaks of variable lengths, or the tempo choices need to be flexible, that's another matter.
Comment: @guidot actually, after looking it up, it does not seem to have a midi in anyway.
Answer: "Drummer's Pulse" claims to do it online. It seems to require Chrome and WebMIDI.

When you search for it, follow the Cache and select text-only (to avoid endless clicking on buttons etc.) it claims:
MIDI

Link your MIDI hardware controllers to the following functions by pressing the buttons and following the instructions:
General
Start/Stop
Tap
Next Sound
BPM
BPM
BPM - 10
BPM + 10
BPM - 1
BPM + 1
BPM Announcement
Toggle BPM Announcement
Beats
Beats
Beats - 1
Beats + 1
Volumes
Master
Whole note
Quarter note
Eighth note
Sixteenth note
Triplet note
External
External (MIDI control change)
Sends the value of this fader to an external application (e.g. Toontrack ezDrummer which is capable to control the volume by MIDI). Notice: You have to install a MIDI loopback software (e.g. loopMIDI by Tobias Erichsen).

Port:   Channel: All 12345678910111213141516   #: 0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119

Sorry, but your browser doesn't support WebMIDI.

These browsers support Web MIDI natively:
https://caniuse.com/#feat=midi

There may be more. I tried as search term:
midi controlled metronom tempo signature

Midi controlled metronome?


What stat analises to use? In SPSS

Question: What analises to use? MANOVA? And how?
 I have a following design:
Two groups made the same task and the result I have in Reaction times ( dependent variable). I have three additional variables (independent) that might have influenced that result.
What statistic command to use? I tried GLM but I can't Choose my dependent variable when entering syntax.
Appropriate your time
Answer: It's hard to say, given what you described.
If I understand correctly, you have one dependent variable (reaction time) and four independent variables. That sounds like a "univariate GLM" (look up UNIANOVA in the Help). Categorical independent variables (like "group", in your case binary) would be the "factors" and the continuous independent variables the "covariates."
Something like this:
UNIANOVA <dependent variable> BY <factor(s)> WITH <covariate(s) / <other options>.

What stat analises to use? In SPSS


Cardinality of continuous functions $f:\Bbb R\to \Bbb R$

Question:
Cardinality of continuous functions $f:\Bbb R\to \Bbb R$.

We know that a continuous function $f:\Bbb R\to \Bbb R$ is completely determined by its values on $\Bbb Q$.
For each rational $q\in \Bbb Q,f(q)$ has $c(=\text{Card }\Bbb R)$ many choices.
Hence the number of such functions becomes $\mathfrak{q^c}= \Bbb N^c$ .
Is it right?Please check.
Answer: It is not correct. For starters, the expression $q^\mathfrak{c}$ (where $q\in\Bbb Q$) is meaningless; I suspect that you meant $\left|\Bbb Q^\mathfrak{c}\right|$, which is indeed $\left|\Bbb N^\mathfrak{c}\right|$. But note that you do need the cardinality bars.
However, you've got the multiplication principle backwards: you're making a choice amongst $\mathfrak{c}$ real numbers $|\Bbb Q|$ times, so you really want $\mathfrak{c}^{|\Bbb N|}$. Finally, $\mathfrak{c}=2^{|\Bbb N|}$, so
$$\mathfrak{c}^{|\Bbb N|}=\left(2^{|\Bbb N|}\right)^{|\Bbb N|}=2^{|\Bbb N|\cdot|\Bbb N|}=2^{|\Bbb N|}=\mathfrak{c}\;.$$
This actually shows only that there are at most $\mathfrak{c}$ continuous real-valued functions on $\Bbb R$, since it counts all functions from $\Bbb Q$ to $\Bbb R$, not just the ones that are restrictions of continuous functions from $\Bbb R$ to $\Bbb R$. To complete the argument you need to show that there are also at least that many; this is easy, since there are $\mathfrak{c}$ constant functions.
I've answered this in order to respond to the specific problems with your argument. The question itself has been dealt with in detail in the answers to this question.
Comment: One question?Why can't we conclude from the first para that there are exactly $\mathfrak c$ functions?Why do we state at most $\mathfrak c$ many functions?Since for each $q\in \Bbb Q\exists\mathfrak  c$ many choices so can't we conclude that number of such functions is exactly $\mathfrak c$ as above
Comment: Why do we need to use constant functions to guarantee at least $\mathfrak c$?Would be glad if u could help.
Comment: @Ben: You're welcome. \\ The functions that you're counting aren't necessarily continuous; there are $\mathfrak{c}$ of them, but it's conceivable that fewer than that many are continuous. \\ We don't have to use the constant functions to guarantee that there are at least $\mathfrak{c}$ continuous functions; any set of $\mathfrak{c}$ continuous functions would work. But they're one of the easiest ways to do so: they are very easily seen to be continuous, and it's immediately clear that there are $\mathfrak{c}$ of them.
Comment: @Ben: You're very welcome.


Delphi Twebbrowser Validar Recaptcha

Question: estou acessando um site para buscar algumas informações, estou usando o delphi com o componente twebbrower, porém tem uma validação com o recaptcha, alguém sabe me dizer como posso validar o recaptcha via programação?
Comment: O que você já tentou?
Comment: sou programador faz bastante tempo, mas nesse caso não sei por onde começar
Answer: A função do reCAPTCHA/CAPTCHA é exatamente essa. Impedir que a validação/acesso seja feito via programação.
CAPTCHA é uma das medidas para proteger áreas restritas, formulários e outras coisas de bots/robôs que tentam fazer o acesso, validação ou envio de informações.
Imagine um cadastro de usuário ou alguma pesquisa com campos básicos (Data início e fim, CPF e etc), 99% desses casos são recursos que podem ser acessados por qualquer um que faça uma requisição GET.
Sem a proteção de um CAPTCHA, seria "fácil" para qualquer "hacker" construir um bot que fique fazendo requisição GET, preenchendo os dados e submetendo as informações.
Dependendo da quantidade de bots que foi programado, pode haver um gargalho imenso de requisições sendo feitas e informações sendo gravadas no banco de dados. Causando uma grave sobrecarga nos servidores e de dados gravados no banco.
O melhor caminho é verificar se o serviço que está fazendo a pesquisa, fornece algum tipo de API ou WebService para desenvolvedores fazerem a consulta.
Se não existir nenhum tipo de API/WebService, porque não mostra e peça para o usuário preencher só CAPTCHA ou até mesmo todo o site dentro do seu sistema?
Fora isso, é tentar "burlar" a validação, fazendo parse/análise do HTML e possíveis imagens do CAPTCHA (Processamento de imagens), ou tentar achar algum componente/lib de alguém que já tenha feito isso. O que é algo que eu não recomendo nenhum pouco, porque além de estar quebrando uma regra de segurança do serviço, o trabalho e a complexidade vão ser grandes.
Answer: Atualmente existem serviços que fornecem a solução do captcha, normalmente cobram por cada solução.
Se você inspecionar a página vai ver que logo abaixo do reCaptcha existe uma tag chamada <textarea id="g-recaptcha-response" ...>. Nela é possível retornar essa solução fornecida pela API que resolve o Captcha, ou seja, basta enviar os dados do captcha para a API, receber a solução e adicionar neste campo, então é só avançar na página.
Um exemplo de um serviço é o Anti-Captcha. No site deste serviço você pode encontrar exemplos de como obter o captcha e fazer as requisições.

Delphi Twebbrowser Validar Recaptcha


What can I use in place of document.write in a function to procedurally generate html?

Question: So thanks to stack overflow, I've determined that document.write is clobbering my webpage/program every time it runs.  I often see the answer is to use innerhtml and document.getElementById, however, in the following case, I can't wrap my head around how it would be used below, seeing as how you have to assign an id for it to replace. I want the whole thing below to redraw in a single div.  All it does is take that array, spit it out vertically, and highlight the number that matches the variable "speed".  That variable will change when you rerun the speedcounter function, so that the number that is highlighted will also change.
Is there a better way to do this?
And thanks for looking.
function speedcounter() {

var spx;
var speed = 3;
var sp=["0","3","4","4","4","5","6","7","8"];

document.write('
Speed

');
document.write('<input type="button" onclick="addspeed();" value="+">
');

 for (spx=8; spx>=0; spx--) {
    if (spx == speed) {
      document.write('');
     }
    document.write(sp[spx]);
    document.write('
');
    }
 document.write ('<input type="button" onclick="remspeed();" value="-">');
 }

Comment: Just a side note, the `` tag has been deprecated for quite some time. Use CSS instead.
Comment: I'm not sure how to dynamically change the font color based on the current state of $spx, is that possible with CSS?  The idea was to have it happen as a result of the for loop.
Answer: Anything stopping you from using jquery?
Following link uses jquery to create/append dom elements.  How to create a DOM node as an object?
Comment: In my opinion jquery actually makes things more simple.  Behind the scenes it will handle a good deal of the javascript/cross browser "gotcha's".  It's quite likely you'll end up writing less code, and the code that you do write will be more readable.  Just my 2 cents though ;)
Comment: Thanks for the reply and the link as well.  I know nothing about jquery, so I'll have to try it sometime soon.  I'm not a professional though, so I was really trying to keep things as simple/vanilla as possible.
Comment: @KennethIto is absolutely right. Definitely look into learning jQuery. It will make your life a lot easier!
Answer: If you don't want to use jQuery for whatever reason, here's a javascript-only solution.
I created a div

and then use document.getElementById() and innerHTML to add HTML to it. I kept your code structure to illustrate how it would be done. I'm using += to add on to the existing HTML in the div. At the top of the function, I clear it. If you don't do that you'll get multiple copies of the output every time you call the function.
function speedcounter() {
document.getElementById('speed').innerHTML = "";
var spx;
var speed = 3;
var sp=["0","3","4","4","4","5","6","7","8"];

document.getElementById('speed').innerHTML += '
Speed

';
document.getElementById('speed').innerHTML += '<input type="button" onclick="addspeed();" value="+">
';

 for (spx=8; spx>=0; spx--) {
    if (spx == speed) {
        document.getElementById('speed').innerHTML += '' + sp[spx] + '
';
    } else {
        document.getElementById('speed').innerHTML += sp[spx] + "
";
    }
 }
 document.getElementById('speed').innerHTML += '<input type="button" onclick="remspeed();" value="-">';
}

speedcounter();

I also removed your font tag and replaced it with a span and inline CSS to set the color.
Demo
Comment: Thanks!  This is exactly what I was looking for.  I had thought that each time you called innerhtml you were replacing it completely, but I see that you've put a + sign to simply add to it.  That's where I was stuck.
Comment: Yeah, that's the addition assignment operator. `a = a + b` translates to `a += b`. So in this case, I'm taking the innerHTML and adding something to it, and then setting it back as the innerHTML. Just looks cleaner.
Answer: You can modify the innerHTML property of the body directly:
var body = document.getElementsByTagName('body')[0];
body.innerHTML += '
Speed

';

http://jsfiddle.net/YB8HU/
Just keep in mind that every time you modify the innerHTML, the browser will reprocess the layout, so consider assigning your HTML strings to a variable, and actually adding it to the body at the end of your script.
Comment: Thanks for your help!  What does the [0] indicate here?
Comment: It means "the first `body` tag found". Your HTML shouldn't have more than one, but since `getElementsByTagName` returns a `NodeList` (not a single `Node`), you have to use that.


use Nginx as proxy, ajax request get 405 every other request

Question: I'm using nginx to separate the static and dynamic content,
this is my nginx config:
<code>

    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    error_log  /usr/local/var/logs/nginx/error.log  notice;
    #error_log  logs/error.log  info;

    pid        /usr/local/var/logs/nginx/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr:$remote_port - [$request_method] [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

        access_log  /usr/local/var/logs/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        upstream tomcat_backend {
            server localhost:8090;
        }

        server {
            listen 9797;
            server_name localhost;
            server_name_in_redirect off;
            access_log /usr/local/var/logs/nginx/test.access.log main;
            error_log  /usr/local/var/logs/nginx/test-error.access.log  debug;
            location ~ \.(jpg|png|js|ico|html|gif|css|map)$ {
                root /path/to/static;
            }
            location ^~ /api/ {
                access_log /usr/local/var/logs/nginx/nginx-location.access.log main;
                error_log  /usr/local/var/logs/nginx/nginx-location-error.access.log  debug;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Request-Time $msec;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_pass http://tomcat_backend;
                #proxy_set_header X-Request-Id $request_id;
            }
            location / {
                root /path/to/static;
                index index.html;
                if (!-e $request_filename) {
                    rewrite ^/(.*)  /index.html last;
                }

            }
        }
        include servers/*;
    }

</code>
sometimes tomcat can get the request and response it correctly, sometimes nginx just return 405 directly, not event forward to tomcat
this is the console of chrome, it always follow that pattern, 405, 401, 405, 401..., it shows 401 is because I input wrong password in purpose, that mean the request has been forward to tomcat:

I found that if I use node.js as backend(just a proxy server), everything is ok, no 405 anymore.
Answer: OK, finally I found the reason.
there is another process is monitoring the same port.
but it's wired that two process are using the same port


beforeEach is being called for every test instead of just once

Question: I'm encountering a situation when I use the beforeEach async and I have a couple of tests in my spec.
I see that the beforeEach is getting called for each test that runs instead of only once before all the tests - what am I doing wrong?
Here's my code:
describe("desc1", function () {

   beforeEach((done) => {
   // some logic
        done();

    });

}
describe("desc2",() => {

    it("has a value", () => {
        expect().toBe('');
    });
    it("has a value", () => {
        expect().toBe('');
    });
    it("has a value", () => {
        expect().toBe('');
    });

});
});

});
I'm using Typescript as you can see and Chutzpa
Comment: beforeEach will get called for every test
Comment: In mocha there is before hook and beforeEach hook, so might be there will be something before hook in the typescript
Comment: Ok. So in my beforeEach, I have scripts which are loaded async. I can check to see if it's my first test and load them but I was just wandering if there's a way to call beforeEach once for the describe.
Comment: within describe just user before(done){ //what you want to do  done()} for each describe it will run only once for each describe before the test
Comment: It looks like "before" is not recognized. There's a beforeSuite: https://www.npmjs.com/package/jasmine-before-suite
Comment: Ok then use that beforeSuite.From documentation it is telling it will be called once
Answer: as the name says, "beforeEach" runs before-each-test, you should use "before"
Answer: you can use the before command like so
describe('desc1', () => {
  before(() => {
    cy.log('Logged Once Before All tests');
  });

  beforeEach(() => {
    cy.log('Logged Before Every test');
  })

  it("has a value", () => {
    expect().toBe('');
  });
  it("has a value", () => {
    expect().toBe('');
  });
  it("has a value", () => {
    expect().toBe('');
  });
});


How to make a link type of a news automatically?

Question: I made a content type what called news, and I have created a list(text) field type to grab the type of news and name it news_type and it's value is Sport, Health, Technology, Economic and others.
I want each of this type comes to a block as a link to all of that type news. Something like below:

and How to make each news type a link?
Some people say you should use View, How to use view for this?
Please describe step by step.
Comment: you can do by adding that content-type inside view.
Answer:
Create a View named "xyz" & select content of type "news"
Select page or block
Add fields what you want to show or if you need filtration then you can filter your content.
& save.
Comment: Thanks, How to make each news type a link?


Integrating indicator function

Question: I am trying to integrate $f(t,\tau)=I(t \leq \tau)$ with respect to $\tau$ where $\tau$ is random variable that follows some distribution$(eg; Uniform[1,5])$ and $t > 0$. Any help would be appreciated? I am reading some paper and they don't show all the steps.
Comment: I am not sure but what would be the result in this case?
Comment: Basically, I want to get the marginal function f(t) by integrating out tau.
Comment: Thanks for an answer. So basically the integral equals 1 - CDF.
Comment: I am afraid you mostly missed the point in my answer.
Answer: To compute$$\int_1^5 \mathbb I_{t\le \tau}\,\text{d}\tau/4$$one need break the domain of integration $(1,5)$ into the part where the indicator is zero, which is when $\tau<t$, namely
$$(1,5)\cap(-\infty,t)=\begin{cases}\varnothing &\text{when }t<1\\(1,\min\{5,t\}) &\text{when }t>1\end{cases}$$
and the part where the indicator is one, namely
$$(1,5)\cap(t,\infty)=\begin{cases}\varnothing &\text{when }t>5\\(\max\{1,t\},5) &\text{when }t<5\end{cases}$$
Hence
$$\int_1^5 \mathbb I_{t\le \tau}\,\text{d}\tau=\begin{cases}
\int_1^5\,\text{d}\tau &\text{when }t<1\\
\int_t^5 \,\text{d}\tau&\text{when }1<t<5\\
0 &\text{when }t>5\end{cases}$$
The extension to a non-uniform prior on $\tau$ is immediate.

Integrating indicator function


Java, running external programs

Question: I am trying to write a chatbot. I am still in my startings, but I do have one question.
Process proc = Runtime.getRuntime().exec("notepad.exe");

This actually makes it impossible to quit my program, unless I quit the notepad. Is it possible to quit the Java program before this notepad has ended?
Comment: possible duplicate of [What is Daemon thread in java](http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java)
Comment: Yes, but it's platform specific and very messy to achieve.
Answer: You can run an external program via a separate thread in your program and continue your program logic in your main thread, i.e. a multi-threaded application is a good solution.
Comment: Even if the program runs in another thread, I can't quit the program. Does this mean that, if I want to run a big memory-eating program, like games like Skyrim, it's running in the Java application, thus terminating the Java-app process, means terminating the external program?
Answer: First destroy the subprocess :
Process proc = Runtime.getRuntime().exec("notepad.exe");
proc.destroy();

If this doesn't work, get the process id of the process  and  run a different subprocess to run kill <pid>
then exit java with System.exit(0); or normally.

Java, running external programs


Help with derivation of Mean Field Variational Inference

Question: I am studying Variational Inference using Bishop's book: Pattern Recognition and Machine Learning. At the moment, I am struggling to understand the Lower Bound derivation for the Mean-Field Variational inference at page 465, equation 10.6.
Surely I am missing some basic algebraic detail.. but, I can't for the moment see how this $\int \prod_{i}q_{i}\{\ln{p(\boldsymbol{X},\boldsymbol{Z})}\}d\boldsymbol{Z}$ turns into $\int q_{j}\{ \int \ln{p(\boldsymbol{X},\boldsymbol{Z})} \prod_{i \neq j} q_{i}\,d \boldsymbol{Z_{i}}\} \, d\boldsymbol{Z_{j}}$. From where does the extra integral come from? I have some ideas, but I am not sure of those. So, I want to read some intuitive explanation (with math as well) on the subject.
Additionally, can someone gently expose the complete algebraic derivation of equation 10.6 including showing the constant values?
Comment: The book has been released https://www.microsoft.com/en-us/research/people/cmbishop/#!prml-book
Comment: "Additionally, can someone gently expose the complete algebraic derivation of equation 10.6 including showing the constant values?" Leaving that formula out of your question limits the list of someones to owners of the book. Could you add it?
Answer: The main issue is that the integrals involved are multivariate. A confusing thing about Bishop's notation is that, inside those integrals, $q_i$ should actually be $q_i(\mathbf{Z}_i)$.
So we want to maximize the bound
$$\mathcal{L}(q) = \mathbb{E}_{\mathbf{Z}\sim q}[\log p(\mathbf{X},\mathbf{Z})]-\mathbb{E}_{\mathbf{Z}\sim q}[\log q(\mathbf{Z})] $$
with respect to the $j$-th marginal of the distribution $q$.
Because of the mean-field assumption, $$\log q(\mathbf{Z})=  \log q_j(\mathbf{Z}_j) + \sum_{i \neq j}  \log q_i(\mathbf{Z}_i),$$
therefore the entropy term will be:
$$\mathbb{E}_{\mathbf{Z}\sim q}[\log q(\mathbf{Z})] =  \mathbb{E}_{\mathbf{Z}_j\sim q_j}[\log q(\mathbf{Z}_j)]+ \sum_{i \neq j}  \mathbb{E}_{\mathbf{Z}_i\sim q_i}[\log q(\mathbf{Z}_i)].$$
Now, for the first term of the sum, there is actually no extra integral, it's just that we're considering a multivariate integral. We'll use the mean field assumption to break the multivariate integral:
$$ \mathbb{E}_{\mathbf{Z}\sim q}[\log p(\mathbf{X},\mathbf{Z})] = \int \log p(\mathbf{X},\mathbf{Z}) q(\mathbf{Z})d\mathbf{Z}\\=\int \log p(\mathbf{X},\mathbf{Z}) \prod_i q_i(\mathbf{Z}_i)d\mathbf{Z}_i \\= \int  \left( \log p(\mathbf{X},\mathbf{Z})\prod_{i\neq j}q_i(\mathbf{Z}_i)d\mathbf{Z}_i\right) q_j(\mathbf{Z}_j)d\mathbf{Z}_j \\=
\int \left( \mathbb{E}_{i \neq j}[\log p(\mathbf{X},\mathbf{Z})]\right) q_j(\mathbf{Z}_j)d\mathbf{Z}_j,$$
using the notation that Bishop introduces in Formula (10.8):
$$\mathbb{E}_{i \neq j}[\log p(\mathbf{X},\mathbf{Z})] = \int \log p(\mathbf{X},\mathbf{Z})\prod_{i\neq j}q_i(\mathbf{Z}_i)d\mathbf{Z}_i.$$
Now, denoting
$$A = \int \exp(\mathbb{E}_{i \neq j}[\log p(\mathbf{X},\mathbf{Z})])d\mathbf{Z}_j,$$
we can write:
$$ \mathbb{E}_{\mathbf{Z}\sim q}[\log p(\mathbf{X},\mathbf{Z})] =\int  \mathbb{E}_{i \neq j}[\log (p(\mathbf{X},\mathbf{Z})A/A)]q_j(\mathbf{Z}_j)d\mathbf{Z}_j \\= \int  \mathbb{E}_{i \neq j}[\log (p(\mathbf{X},\mathbf{Z})/A)]q_j(\mathbf{Z}_j)d\mathbf{Z}_j +\log(A).$$
Note that, in Bishop's notations, we have exactly:
$$\tilde{p}(\mathbf{X},\mathbf{Z}_j) = \exp \mathbb{E}_{i \neq j}[\log (p(\mathbf{X},\mathbf{Z})/A)].$$
By combining everything, we end up with:
$$\mathcal{L}(q) = \int  \log \tilde{p}(\mathbf{X},\mathbf{Z}_j)q_j(\mathbf{Z}_j)d\mathbf{Z}_j + \mathbb{E}_{\mathbf{Z}_j\sim q_j}[\log q(\mathbf{Z}_j)]+ \sum_{i \neq j}  \mathbb{E}_{\mathbf{Z}_i\sim q_i}[\log q(\mathbf{Z}_i)] + \log(A).$$
where the two last terms are the "constants" that do not depend on $q_j$.


Integral of $(x^2 \sin (x))/(x^4 + 2 x ^2 - 1)$

Question: I recently had this integral on a test $$\int ^\pi _{-\pi} \frac{x^2 \sin (x)}{x^4 -2x^2 -1} dx$$
Mr professor claims this is actually equal to zero because the function is periodic and the top is odd, whereas the bottom is even. I graphed this out and it does seem that the function is symmetric about the origin, which means it's conceivable that all the contributions to the integral will cancel themselves out, but I'm not convinced. I plugged this into Mathematica and it says the integral doesn't converge.
I've got no idea how to go about solving this, and seeing how nasty the mathematica solution is, I'm not expecting any of you to do that either. My question is, can we really say anything about this integral without truly solving it?
Comment: The function is not periodic
Comment: The integrand in the title is not the same as the integrand in the body of the message.  However, even if we use the integrand in the title, the same problem arises:  we get an integral of the form $\int_u^v f(x)/(x-a) dx$ with $a\in (u,v)$, $f(a)\neq 0$.
Comment: I edited it, thanks
Answer: Let:
$f(x)=\frac{x^{2}sin(x)}{x^{4}-x^{2}-1}$.
Note that $f(-x)=\frac{-x^{2}sinx}{x^{4}-x^{2}-1}=-f(x)$.
Hence the function is odd.
And since the function passes through the origin, and the integral between $\pi$ and $-\pi$ is the signed area bounded by the curve and $x$ axis, the total area is $0$.
Comment: So an odd integrand always guarantees an even function will come out of the integral?
Comment: I'm not sure if that's true, but I was referring to the symmetry about the origin. Sketching the curve will reveal that integral between 0 and $\pi$ is the negative of the integral between $-\pi$ and 0. This is true of any odd function that passes through the origin.
Comment: I was thinking the same thing, but I wasn't sure if the area was equal to infinity. I mean, it's not true that $ \infty - \infty = 0$, right? As far as I know, you can't really tell if the area is infinity by looking at its graph.
Answer: The denominator is $0$ at $x=\pm  g$ where $g=\sqrt {(1+\sqrt 5)/2}$ ...(both of which lie in $(-\pi,\pi)$)... and at no other real $x$. The numerator is not $0$ at $x=\pm g.$ In a small nbhd of $ g$ the integrand behaves like $k/(x-g)$ with constant $k\ne 0$ so $\int_0^{\pi}f(x) dx$ does NOT exist. However if we let $A=\lim_{e\to 0+}(\int_0^{g-e}+\int_{g+e}^{\pi})f(x) dx$ and $B=\lim_{e\to 0^+}(\int_{-\pi}^{-g-e}+\int_{-g+e}^0)f(x) dx$ then $A$ and $B$ do exist. And $A+B=0$ because $f(-x)=-f(x)$ for $x\ne \pm g$. But this is not the  def'n of $\int_{-\pi}^{\pi}f(x) dx.$
Comment: I'm not sure I really understand what you're getting at. You say the integral with limits $[0, \pi]$ doesn't exist, but then you say it does exist if you define it differently. Your last sentence confuses me as well. If what you've shown isn't the definition of $\int ^\pi _{-\pi}$ what was the point of the rest of your answer?
Comment: It does not exist as stated. The rest was a digression.
Comment: So my professor was wrong? If I'm gonna bring it up with him, I want to make sure I'm not wrong.
Comment: Just ask him what happens in a nbhd of $x=\sqrt {(1+\sqrt 5)/2}$.


Closed set in topological product

Question: We know that $\Pi_{i=1}^{n} \overline{A_i}=\overline{A}$ where $A=\Pi_{i=1}^n A_i$
How we can see from this that $\Pi_{i=1}^n A_i$ i closed if and only if each $A_i$ is closed ?
thank you
Comment: Provided that all the $A_i$ are non-empty, or we have trivial counterexamples.
Answer: If $A$ is closed, then $\overline{A}=A$, so $\prod_{i=1}^n\overline{A_i}=\prod_{i=1}^nA_i$, so $\overline{A_i}=A_i$ for each $i$ (each component is closed).
If each $A_i$ is closed, then $\overline{A}=\prod_{i=1}^n\overline{A_i}=\prod_{i=1}^nA_i=A$, so $A$ is closed, too.
Answer: Consider the projection map onto each component. Projection maps are closed maps.
Comment: Projection maps are **open**, not closed. For instance, $$S=\{(x,y)\in\mathbb R^2\,:\, xy=1\}$$ is closed, but $\pi_x(S)=\pi_y(S)=\mathbb R^2\setminus\{0\}$
Comment: Can you explain me more?
Comment: If each component is closed, their closures are just themselves. So the product is closed and therefore, it is its own closure.
Comment: If each $A_i$ is closed then using the adherence the product i closed but how to do the other sens ? if the product is closed how to see that each $A_i$ is closed ? thank you
Comment: That is what I stated in the answer.
Comment: the other sens we do projection ?? i don't understand how ?
Comment: The projection of a closed set onto each of its components is closed.
Comment: we can't see it juste by using the adherence ?
Comment: Of course, not $\mathbb R^2\setminus\{0\}$, but $\mathbb R\setminus\{0\}$.
Comment: Well then, looks like I messed up my last topology homework

Closed set in topological product


How to disable unwanted services in vyos?

Question: I am using VYOS server. I used nmap to scan all the open ports.
<code>PORT      STATE    SERVICE
22/tcp    open     ssh
111/tcp   filtered rpcbind
1521/tcp  filtered oracle
1525/tcp  filtered orasrv
14520/tcp filtered unknown
</code>
According to nmap document, the filtered state is something where nmap is unable to decide whether port is open or not.
I used curl to connect to the port. I see SYN is coming but VYOS is not responding back?
How can I do any one of these?

Block these ports in vyos permanently.
Disable the services.
Comment: It seems to me this question is mostly about what `filtered` means in the output. I would consider that question to be on-topic here, so I am [voting](http://serverfault.com/review/close/253473) to leave the question open. I did a [search](http://serverfault.com/search?q=%5Bnmap%5D+filtered) for possible duplicates, but didn't find a good one.
Answer: The "filtered" state is just what it sounds like. As you noted, nmap sent a SYN but got no response. Since nmap apparently didn't receive an ICMP error (e.g. "no route to host"), it assumes the probe should have made it to its destination, but was dropped somewhere along the way. Of course, ICMP could have been filtered on its way back to nmap, which is why you can't really be certain unless you control the network.
When I see this on my network, I usually assume one of the following:

The destination host that I'm probing (e.g. your Vyatta system) has a packet filter explicitly dropping nmap's probes.
There is a firewall or similar device between me and the host I'm probing.

If this was a host on my network, I would take the "filtered" response to mean that the ports are most likely blocked already.
Comment: Yeah VM doesn't respond but still its open for SYN flood attack as VM also doesn't connection refuse.
Comment: Whether it refuses the connection (e.g. TCP RST) or just drops the packet, the result is the same. The purpose of a SYN flood is kernel resource starvation by making the kernel sit there with a bunch of half-open sessions. If the server refuses _or_ drops the initial SYN, that doesn't happen. Dropping the SYNs is usually safer, because an attacker with a spoofed IP can cause you to send the TCP RST somewhere else (similar to a [Smurf atttack](https://en.wikipedia.org/wiki/Smurf_attack), but without amplification).


Removing linear term from a PDE

Question: Consider the equation
$$
\partial_tu =\nabla \cdot [u^{m-1} \nabla u] + \lambda u.
$$
I want to consider a transformation that can remove this $\lambda u$ term into a PDE of the form $\partial_tv = D\nabla \cdot [v^{m-1} \nabla v]$ for some constant $D$. Is this possible? I've found that if I let $u = \exp(\lambda t)v$, then
$$
\partial_tv = \exp[\lambda(m-1)t]\nabla\cdot[v^{m-1}\nabla v],
$$
but the $\exp[\lambda(m-1)t]$ term remains. Is it possible to remove it with a further transformation of $v$ or $t$?
Answer: Was just missing something obvious: Setting $\tau = g(t)$ for some $\tau$ gives $\partial_tv = \partial_\tau v g'(t)$. So, $g'(t) = \exp[\lambda(m-1)t]$ would allow the exponential term to be removed. Thus, $g(t) = 1/[\lambda(m-1)]\exp[\lambda(m-1)t]$ or $t = 1/[\lambda(m-1)]\log[\lambda(m-1)\tau]$.
Also, replacing $\tau$ by $\tau - D/[\lambda(1-m)]$ will make simplify it so that $\tau=0$ when $t=0$.

Removing linear term from a PDE


How to clear Exchange mail queues using PowerShell for a specific user

Question: Recently, my organization were victim of a spam attack. Sadly, some users provided their email and password. The attacker set up a long queue of emails to be sent (from the victims account).
We had to delete all the message manually (from the queue viewer). It was very painful (more than 100 thousands emails...).
Is it possible to clear the email queue of a specific user with the PowerShell ? (using the Active Directory module)
Found this old post, but it look outdated and it delete all the queue...
Answer: You just have to add a Where-Object (abbreviated to ?) after Get-Message to select the messages you want to remove.
Get-ExchangeServer |
    ?{$_.IsHubTransportServer -eq $true} |
    Get-Queue |
    get-message |
    ? {$_.sender -eq 'email@example.com'} |
    Remove-Message -withNDR $false

Comment: Is it possible to add the parameter -server + -credential (im running the command in remote)?
Comment: You should probably connect using a separate command first. Then any future Exchange commands will be issued against that Exchange org.
Comment: Perfect ! Thank you a lot ! I am now ready for the next spam attack ! Watch out ! :)


Prove that $(x_n)\rightarrow 0$ if $(y_n)\rightarrow 0$

Question: I want to prove the following theorem:

Let $q$ be a real number such that $0 ≤ q < 1$. Let $(x_n)$ be a bounded sequence and let $(y_n)$ be the sequence defined by $∀n ∈ \mathbb{N}, y_n = x_n − qx_{n+1}$. If $y_n$ converges to $0$, then $x_n$ converges to $0$.

I can show that when $\lim x_n$ exists, $(x_n)\rightarrow 0$, but my trouble is showing that $\lim x_n$ in fact exists. My strategy is to show that $\limsup x_n = \liminf x_n$ so that I may conclude that $\lim x_n$ exists. Of course, when $q=0$, the result is immediate.
Here's what I have so far, when $0<q<1$,
\begin{align*}
\limsup y_n & = \limsup(x_n-qx_{n+1})\\
    & \leq \limsup(x_n) + \limsup(-(qx_{n+1})\\
& = \limsup(x_n) -\liminf(qx_{n+1})\\
& = \limsup(x_n) - q\liminf(x_{n+1})\\
& = \limsup(x_n) - q\liminf(x_n),
\end{align*}
and by similar reasoning I've been able to show
\begin{align*}
\liminf y_n \geq \liminf x_n -q\limsup x_n.
\end{align*}
You can assume that each line follows from a result I proved in a previous problem. Given what I have so far, is there a way to reach $\limsup x_n \leq \liminf x_n$ so that I may conclude $\limsup x_n = \liminf x_n$? What am I missing?
I want to use the fact that $y_n$ converges to $0$, but whenever I try using this fact with my inequalities, I just arrive at what we already know: $\liminf x_n \leq \limsup x_n$.
Answer: $0\le q<1$ then $-1<-q$ iff $-|x_{n+1}|\leq-q|x_{n+1}|$ iff $|x_n|-|x_{n+1}|\leq|x_n|-q|x_{n+1}|\leq|x_n-qx_{n+1}|=|y_n|\to0$
Therefore $|x_n|$ is a Cauchy's sequence. Suppose $|x_n|\to c$. Applying limit in $|x_n|-q|x_{n+1}|\leq|y_n|$ we obtain $(1-q)c=0$. As $1-q \neq0$, we get $c=0$, i.e., $|x_n|\to0$. Therefore $x_n\to0$.
Comment: Thanks for this. I follow what you're doing, but I haven't encountered Cauchy sequences yet. Could you provide some hint for me based on the work I've shown already?
Answer: Jaca's answer is fine but here's an argument following your line of thought in case you need it.
So, the fact that $(x_n)$ is a bounded sequences allows us to play with both $\limsup x_n$ and $\liminf x_n$ as numbers, what I want to say is that both limits are finite.
Remember that, since $\lim y_n=0$, we have both limits superior and inferior equal to zero.
We have $x_n=y_n+qx_{n+1}$, taking $\limsup$ on both sides we get
\begin{align}
  \limsup x_n &\leq \limsup y_n + q\limsup x_{n+1} \\
              &= \limsup y_n + q\limsup x_{n}
  .
\end{align}
So that
$$
  (1-q)\limsup x_n \leq 0
$$
Since $1-q>0$ (we're in the interesting case $0<q<1$) we must have $\limsup x_n\leq 0$. Now take $\liminf$ in the equation $x_n=y_n+qx_{n+1}$ so that
$$
  \liminf x_n \geq q\liminf x_n \quad\Rightarrow\quad (1-q)\liminf x_n\geq 0
  ,
$$
and similiarly, $1-q>0$ implies $\liminf x_n\geq 0$ and we obtain $\liminf x_n=\limsup x_n =0$.