Further optimizing my Java code filled with for-loops

Question: I wrote a code that returns two prime numbers with their gap equal to a given long and having no other prime number between them.
These are the methods I've made so far:
public static long[] firstGap(int gap, long lLimit, long uLimit) {
    for(long i=lLimit; i<=uLimit; i++) {
        for(long j=i+1; j<=uLimit; j++) {
            if(isPrime(i) && isPrime(j) && j-i==gap && !repeatedIsPrime(i+1,j-1)) {
                return new long[]{i,j};
            }
        }
    }
return null;
}

For checking if a number is prime
public static boolean isPrime(long n) {
    for(int i=2;i<n;i++) {
        if(n%i==0) {
            return false;
        }
    }
return true;
}

For checking if there's a prime number between two numbers
public static boolean repeatedIsPrime(long x, long y) {
    for(long i=x; i<=y; i++) {
        if(isPrime(i)) {
            return true;
        }
    }
return false;
}

What I've done so far is having the loop index as int but then I'd have a lossy conversion, next is reducing the method invocations but I can't find a way to do that. The only improvement I've made so far is that I've removed some unnecessary storing and assigning of values, other than that I got nothing. So how do I further optimize my code?
Comment: You can make this program vastly faster by improving your algorithms first. Reducing method invocations isn't going to make this code faster.
Answer: You can use following code:
public static long[] firstGap(int gap, long lLimit, long uLimit) {
    for(long i=lLimit; i<=uLimit; i++) {
        if(isPrime(i)) {
            long j = gap + i;
            if (isPrime(j) && !repeatedIsPrime(i + 1, j - 1)) {
                return new long[]{i, j};
            }
        }
    }
    return null;
}

First, if isPrime(i) == false, other check isn't have any sense. Second, for(long j=i+1; j<=uLimit; j++) can be deleted, because you use only one j (when j-i==gap)
Answer:
Remove the inner loop: `if (isPrime(i) && isPrime(i+gap) && !repeatedIsPrime(i+2, i+gap-2) return new long[]{i, j}
Make sure lLimit and uLimit are odd and increment by 2 as no sense to check the even numbers
When checking for prime, run till square root of n
Can use HashMap<Long, Boolean> to make sure you only check the same number once - but will have to remove the smallest number as you move
Use smarter primality test, such as e.g. Fermat test https://en.wikipedia.org/wiki/Primality_test#Fermat_primality_test for faster isPrime(i)


Describe union of set

Question: Let S describe the set $\bigcup_{n=1}^\infty A_n$ where
$A_n = \{(x, y) \in \mathbb{R}^2 | y-x^{2n} \geq 0 \}$
I understand that S describes the set of points "inside of the parabola y = $x^2$, but am not sure how to prove this. Any ideas?
Comment: Wait. We both have done a mistake. $S$ is actually $A_1\cup\{(x,y)|x\in(-1,1),y\in(0,1)\}$
Comment: for each $x$ consider the intersection with the vertical line at $x$. For $|x|>1$ these intersections get smaller for bigger $n$, but the opposite holds for $|x|<1$.
Comment: prove $a\in A_n \Rightarrow a\in A_1$ for positive integer $n$
Comment: @Isura Manchanayake, but doesn't that only work for n = 1?
Comment: no. Draw the graph and see.
Comment: Okay, thx . . .
Answer: If you mean inside of the parabola $y=x^2$ is $\{(x,y):y\geq x^2\}$, it can be shown like the following.
Let $(x,y)\in \bigcup_{n=1}^\infty A_n$
$\Rightarrow \exists n_0: (x,y)\in A_{n_0}$
$ \Rightarrow \exists n_0: (x,y)\in \{(x, y) \in \mathbb{R}^2 | y-x^{2n_0} \geq 0 \}$
$\Rightarrow \exists n_0: (x,y)\in \{(x, y) \in \mathbb{R}^2 | y \geq  x^{2n_0}\}\subset \{(x, y) \in \mathbb{R}^2 | y \geq  x^{2}\}$ ....   ($n_0\geq1$)
$\Rightarrow (x,y)\in\{(x, y) \in \mathbb{R}^2 | y \geq  x^{2}\}$
Let $(x,y)\in\{(x, y) \in \mathbb{R}^2 | y \geq  x^{2}\}$
$\Rightarrow  (x,y)\in A_1\subset \bigcup_{n=1}^\infty A_n$
$ \Rightarrow  (x,y)\in \bigcup_{n=1}^\infty A_n$
So we have $\bigcup_{n=1}^\infty A_n=\{(x, y) \in \mathbb{R}^2 | y \geq  x^{2}\}$

Describe union of set


python 3 asyncio and MotorClient: how to use motor with multithreading and multiple event loops

Question: I am back with a question about asyncio. I find it very useful (especially due to the GIL with threads) and I am trying to boost performances of some pieces of code.
My application is doing the following:

1 Background daemon thread "A" receives events from connected clients and reacts by populating a SetQueue (that simply is an event queue that removes duplicate ids) and by doing some insertions in a DB. I get this daemon from another module (basically I control a callback from when an event is received). In my sample code below I substituted this with a thread I generate and that very simply just populates the queue with 20 items and mimics DB inserts before exiting.
1 Background daemon thread "B" is launched (loop_start) and he just loops over running until completion a coroutine that:

Fetches all the items in the queue (if not empty, otherwise it release the control for x seconds and then the coroutine is re-launched)
For each id in the queue it launches a chained coroutine that:

Creates and waits for a task that just fetches all relevant information for that id from the DB. I am using MotorClient that supports asyncio to do await in the task itself.
Uses an Pool of Processes executor to launch a process per id that uses the DB data to do some CPU intensive processing.

The main thread just initializes the db_client and takes loop_start and stop commands.

That is basically it.
Now I am trying to boost performance as much as possible.
My current issue is in using motor.motor_asyncio.AsyncioMotorClient() in this way:

It gets initialized in the main thread and there I want to create indexes
Thread "A" needs to perform DB insertions
Thread "B" needs to perform DB finds/reads

How can I do this? Motor states that it is meant for a single thread application where you use obviously a single event loop.
Here I found myself forced to have two events loops, one in thread "A" and one in thread "B". This is not optimal, but I didn't manage to use a single event loop with call_soon_threadsafe while keeping the same behavior...and I think performance wise I am still gaining much with two events loop that release control over the gil bound cpu core.
Should I use three different AsyncioMotorClient instances (one per thread) and use them as stated above? I failed with different errors while trying.
Here is my sample code that doesn't include just the the MotorClient initialization in Asynchro's __init__
import threading
import asyncio
import concurrent.futures
import functools
import os
import time
import logging
from random import randint
from queue import Queue

# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('{}.log'.format(__name__))
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(processName)s - %(threadName)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

class SetQueue(Queue):
    """Queue that avoids duplicate entries while keeping an order."""
    def _init(self, maxsize):
        self.maxsize = maxsize
        self.queue = set()

    def _put(self, item):
        if type(item) is not int:
            raise TypeError
        self.queue.add(item)

    def _get(self):
        # Get always all items in a thread-safe manner
        ret = self.queue.copy()
        self.queue.clear()
        return ret

class Asynchro:
    def __init__(self, event_queue):
        self.__daemon = None
        self.__daemon_terminate = False
        self.__queue = event_queue

    def fake_populate(self, size):
        t = threading.Thread(target=self.worker, args=(size,))
        t.daemon = True
        t.start()

    def worker(self, size):
        run = True
        populate_event_loop = asyncio.new_event_loop()
        asyncio.set_event_loop(populate_event_loop)
        cors = [self.worker_cor(i, populate_event_loop) for i in range(size)]
        done, pending = populate_event_loop.run_until_complete(asyncio.wait(cors))
        logger.debug('Finished to populate event queue with result done={}, pending={}.'.format(done, pending))
        while run:
            # Keep it alive to simulate something still alive (minor traffic)
            time.sleep(5)
            rand = randint(100, 200)
            populate_event_loop.run_until_complete(self.worker_cor(rand, populate_event_loop))
            if self.__daemon_terminate:
                logger.debug('Closed the populate_event_loop.')
                populate_event_loop.close()
                run = False

    async def worker_cor(self, i, loop):
        time.sleep(0.5)
        self.__queue.put(i)
        logger.debug('Wrote {} in the event queue that has now size {}.'.format(i, self.__queue.qsize()))
        # Launch fake DB Insertions
        #db_task = loop.create_task(self.fake_db_insert(i))
        db_data = await self.fake_db_insert(i)
        logger.info('Finished to populate with id {}'.format(i))
        return db_data

    @staticmethod
    async def fake_db_insert(item):
        # Fake some DB insert
        logger.debug('Starting fake db insertion with id {}'.format(item))
        st = randint(1, 101) / 100
        await asyncio.sleep(st)
        logger.debug('Finished db insertion with id {}, sleep {}'.format(item, st))
        return item

    def loop_start(self):
        logger.info('Starting the loop.')
        if self.__daemon is not None:
            raise Exception
        self.__daemon_terminate = False
        self.__daemon = threading.Thread(target=self.__daemon_main)
        self.__daemon.daemon = True
        self.__daemon.start()

    def loop_stop(self):
        logger.info('Stopping the loop.')
        if self.__daemon is None:
            raise Exception
        self.__daemon_terminate = True
        if threading.current_thread() != self.__daemon:
            self.__daemon.join()
            self.__daemon = None
            logger.debug('Stopped the loop and closed the event_loop.')

    def __daemon_main(self):
        logger.info('Background daemon started (inside __daemon_main).')
        event_loop = asyncio.new_event_loop()
        asyncio.set_event_loop(event_loop)
        run, rc = True, 0
        while run:
            logger.info('Inside \"while run\".')
            event_loop.run_until_complete(self.__cor_main())
            if self.__daemon_terminate:
                event_loop.close()
                run = False
                rc = 1
        return rc

    async def __cor_main(self):
        # If nothing in the queue release control for a bit
        if self.__queue.qsize() == 0:
            logger.info('Event queue is empty, going to sleep (inside __cor_main).')
            await asyncio.sleep(10)
            return
        # Extract all items from event queue
        items = self.__queue.get()
        # Run asynchronously DB extraction and processing on the ids (using pool of processes)
        with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
            cors = [self.__cor_process(item, executor) for item in items]
            logger.debug('Launching {} coroutines to elaborate queue items (inside __cor_main).'.format(len(items)))
            done, pending = await asyncio.wait(cors)
            logger.debug('Finished to execute __cor_main with result {}, pending {}'
                         .format([t.result() for t in done], pending))

    async def __cor_process(self, item, executor):
        # Extract corresponding DB data
        event_loop = asyncio.get_event_loop()
        db_task = event_loop.create_task(self.fake_db_access(item))
        db_data = await db_task
        # Heavy processing of data done in different processes
        logger.debug('Launching processes to elaborate db_data.')
        res = await event_loop.run_in_executor(executor, functools.partial(self.fake_processing, db_data, None))
        return res

    @staticmethod
    async def fake_db_access(item):
        # Fake some db access
        logger.debug('Starting fake db access with id {}'.format(item))
        st = randint(1, 301) / 100
        await asyncio.sleep(st)
        logger.debug('Finished db access with id {}, sleep {}'.format(item, st))
        return item

    @staticmethod
    def fake_processing(db_data, _):
        # fake some CPU processing
        logger.debug('Starting fake processing with data {}'.format(db_data))
        st = randint(1, 101) / 10
        time.sleep(st)
        logger.debug('Finished fake processing with data {}, sleep {}, process id {}'.format(db_data, st, os.getpid()))
        return db_data

def main():
    # Event queue
    queue = SetQueue()
    return Asynchro(event_queue=queue)

if __name__ == '__main__':
    a = main()
    a.fake_populate(20)
    time.sleep(5)
    a.loop_start()
    time.sleep(20)
    a.loop_stop()

Answer: What's the reason for running multiple event loops?
I suggest just using the single loop in main thread, it's a native mode for asyncio.
asyncio might run loop in non-main thread in very rare scenarios but it doesn't look like your case.
Comment: Thread "A" is generated by mqtt module (in the real code) and needs to be able to call coroutines (e.g., "worker_cor")...this requires an event loop in the thread.
At the same time, Thread "B" is a background daemon that checks the event queue...needs to send coroutines when the queue is not empty...another loop!The main thread would need, in the real code, to launch some db coroutine...a third loop! How could I use just one event loop with these threads? Could you show me by modifying the sample code what would be your solution (indeed one loop means no problem with motor)
Comment: No further explanations or details? I tried to exploit call_soon_threadsafe to reuse the same loop but I got several problems including:

"Exception in callback None()
handle:
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/events.py", line 125, in _run
    self._callback(*self._args)
TypeError: 'NoneType' object is not callable". I am pretty much stucked here.
Comment: Sorry, I still don't understand why you need extra loops. If your code executed inside a loop is blocking -- please don't do it but call `ran_in_executor`. If your library incapsulates loop creation internally -- this is a *very bad* smell. Keep everything inside the main loop -- and create asyncio tasks for long running jobs. That's it.


add top quatation mark in a string, in mql4

Question: in mql4,
string list[]= {"EUR","CHF","GBP","JPY","AUD","CAD","NZD","CHF"};
for(int i=0; i<ArraySize(list); i++) string primer=list[i];
 {

  for(i=0; i<ArraySize(list); i++)
 {
 string second =list[i];
 string currency = StringConcatenate(primer, second);
 }
 }

currency output is CHFCHF. how can i fix it to change "CHFCHF" instead of CHFCHF ?
i don't add top quotation mark in mql4. Thank you.
Comment: Is this your real code?  `primer` is no longer in scope when you try to use it.
Comment: I have a hard time understanding what you expect from the snippet.
Answer: if it is
string currency = StringConcatenate(primer, second);
output is CHFCHF.
if
string currency = StringConcatenate("\"",primer, second,"\"");

output is "CHFCHF" and that is main point to send order later.
thank you everybody


What causes this error when runing react-native run-ios?

Question: I am trying to get react-native-maps to work. I have followed these instructions react-native-maps but I am getting an error when running react-native run-ios, and dont know what is causing it.
package.json
<code>{
  "name": "mobile",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "i": "^0.3.6",
    "npm": "^5.8.0",
    "prop-types": "^15.6.1",
    "react": "16.3.1",
    "react-native": "0.55.1",
    "react-native-drawer": "^2.5.0",
    "react-native-elements": "^0.19.1",
    "react-native-maps": "^0.21.0",
    "react-native-material-ui": "^1.22.1",
    "react-native-router-flux": "^4.0.0-beta.28",
    "react-native-sidebar": "^0.3.0",
    "react-native-vector-icons": "^4.6.0",
    "react-redux": "^5.0.7",
    "redux": "^3.7.2"
  },
  "devDependencies": {
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.3",
    "react-test-renderer": "16.3.1",
    "eslint-config-rallycoding": "^3.2.0"
  },
  "jest": {
    "preset": "react-native"
  },
  "rnpm": {
    "assets": [
      "./assets/fonts"
    ]
  }
}
</code>
The error looks like this:

The following build commands failed:
CompileC
  build/Build/Intermediates.noindex/mobile.build/Debug-iphonesimulator/mobile.build/Objects-normal/x86_64/AppDelegate.o
  mobile/AppDelegate.m normal x86_64 objective-c
  com.apple.compilers.llvm.clang.1_0.compiler (1 failure)
Installing build/Build/Products/Debug-iphonesimulator/mobile.app An
  error was encountered processing the command (domain=IXErrorDomain,
  code=13): Failed to get bundle ID from
  /Users/danieltuttle/project/code/frontend/traveler-mobile/ios/build/Build/Products/Debug-iphonesimulator/mobile.app
  Print: Entry, ":CFBundleIdentifier", Does Not Exist
Command failed: /usr/libexec/PlistBuddy -c Print:CFBundleIdentifier
  build/Build/Products/Debug-iphonesimulator/mobile.app/Info.plist
  Print: Entry, ":CFBundleIdentifier", Does Not Exist

When I look at the mobile.app folder. I see that there is nothing there. No Info.plist. Nothing. Because this is the build folder, it seems like it should be created. Do I have to set it there?
Can sombody give a tip how to solve this? Thanks!
Comment: Make sure you have `CFBundleIdentifier` added in your info.plist
Comment: The CFBundleIdentifier key exists in info.plist
Comment: Does it have a string associated with the key?
Comment: I upated the question. Sorry I was looking at the wrong info.plist before.
Comment: At minimum, add the steps you took to install `react-native-maps` and your `package.json` file. That `CFBundleIdentifier` error can show up for a ton of different reasons, so just the error will result in people suggesting many valid possible solutions, but it's all just going to be shots in the dark.
Comment: I was susccessfully building and deploying the app to the local IOS simulator, it was when I began to install react-native-maps that this error began to show up. I was following the instructions in the link for iOS-cocoapods when this became and issue. So I have followed those instructions carefully. Of note is I did use react-native link ... But that was like a a couple weeks ago and have done a lot since then with no problem, plus I followed the instructions in that link to revert any damage done by that.
Answer: I realized the problem in the image there is a + sign next to the @import statement and next to [GMSServices] ... It says add this line. So I took the whole line. But the problem was the + sign. I took it away then it worked.


android redraw layout programmatically

Question: My layout sometimes does not position elements properly. I tried a lot of solutions but unique working one is rotate from portrait to landscape and then return from landscape to portrait. Then layout is automaticaly redrawed and fits perfectly. I set orientation|keyboard so content is not reloaded, only redrawed. How to redraw content programmaticaly like landscape-portrait does? Thank you.
EDIT: I tried following and has no effect
getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
getWindow().getDecorView().findViewById(android.R.id.content).requestLayout();

Activity has set main layout on onCreate setContentView(R.layout.main);
Layout "main" contains a TabHost and Admob.
Layout "view" contains a webview ("id:myWebview") that is shown on tab1.
And also tried to invalidate them! webview, layout main and layout view and crashes
Comment: What does "sometimes" means? Is there any reproducible way to get the error? Have you tried requestLayout to force to redraw the layout? Can you put some sample code to show what kind of and views are you using?
Comment: You could try calling `invalidate()` to force a view to redraw.
Answer: Did you try to invalidate() the root view?
findViewById(android.R.id.content).invalidate();

This should redraw the whole layout.
Comment: Would that cause `setContentView()` to be called?
Comment: in kotlin i called layout directly By Id i.e. `llHint.invalidate()` but it's not working i mean no clear whole layout which contain textview, edittext etc.
Answer: When screen orientation changes, Android recreates the current Activity.
Try calling measure(int, int) or requestLayout(). Otherwise there is no chance to programmatically relayout views.
Answer: Call requestLayout() only and read View class description for more informations.
Answer: If this is unsafe or bad practice please do correct me. I tried all these methods as well and out of desperation decided to just try calling my onCreate again and it worked. So for now I'll be using onCreate(null) to refresh my view when I update data. Again, please let me know if this is unsafe or a bad practice and if possible a working alternative.

android redraw layout programmatically


Código fonte não compila

Question: #include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

    float parede, litros;

    printf ("Calcular litros de tintas  por metros quadrados.\n");
    printf ("Digite a metragem da parede(em metros ao quadrado):");
    scanf("%f", parede);

    if (litros = parede*2/10)
        printf("sao necessarios (litros):");
        scanf("%f", litros);

system ("pause");
return 0;
}

Comment: Relacionado: [Qual o significado do operador "&" (e comercial) na linguagem C?](https://pt.stackoverflow.com/questions/125793/qual-o-significado-do-operador-e-comercial-na-linguagem-c)
Comment: Ignorando vários problemas dos quais grande parte já foi assinalado por respostas, porque `` se o programa está todo em C ?
Answer: No scanf, é necessário o uso do "&":
scanf("%f", &parede);

O uso do "if" também não é necessário, pois você está atribuindo um valor à variável "litros", e não criando uma condição:
litros=(parede*2)/10;

Com isso, no resultado final, o printf deverá mostrar:
printf("Sao necessarios (litros): %.2f", litros);

(O %.2f determinou a quantidade de casas decimais que o programa mostrará, que no caso são 2).
Com isso não há a necessidade do uso do "scanf" após esse printf pois o usuário não irá digitar mais nada, apenas o resultado será impresso.
Espero ter ajudado.
Answer: Existe alguns problemas em seu código, por exemplo
 scanf("%f", parede); é necessario alocar o valor da variavel com &parede)/
outro erro é i IF que não esta correto utiliza-lo já que não há condição no programa( ele vai receber valor, calcular conforme a formula e só), o correto é
litros = (parede*2)/10; litros irá receber o valor de parede*2 / 10
e para apresentar os valores( printf("sao necessarios (litros)%.2f:\n ", litros);
onde (%.2f): ira chamar a variavel float com 2 digitos após a casa decimal.
Answer: Tente usar :
 if (litros = parede*2)/(10){
    printf("sao necessarios (litros):");
}
    scanf("%f", &litros);

Nunca se esqueça de colocar o & quando estiver usando Scanf, caso queira imprimir só dois números dpois da vírgula, use %.2f quando for imprimir.
Uma boa dica é abandonar o devC++ (eu acho que você está usando, devido ao system pause ali em baixo), eu recomendo usar o Eclipse para C, e tem a vantagem de rodar no Linux também.

Código fonte não compila


Atribuir input a variável

Question: Estou fazendo um formulário simples de envio. Basicamente: O usuário insere o valor a ser sacado, preenche os dados e ao clicar no botão confirmar,  é exibido o resultado na tela.
O problema é que eu não estou conseguindo criar essa função de envio, para que o código só execute quando o usuário clicar em confirmar. Como proceder?
Segue código:

    Valor do saque

        <input id="textinput" name="textinput" value="" type="text" placeholder="R$ 00,00" class="form-control input-md" required="">

Comment: Como está fazendo o envio? Onde está o botão de enviar?
Comment: Esse envio vai para um servidor?
Esse envio é calculado por javascript?
Esse envio pode ser cancelado?
Por favor, explique melhor seu problema para que possamos te ajudar
Answer: Conforme entendi, vc pode fazer assim:
Com javascript
<button onclick="enviar()">Confirmar</button>

Ou usando jQuery:
<button id="confirmar">Confirmar</button>

Answer:
O comportamento do exemplo abaixo será o seguinte:
  - Ao clicar no botão confirmar:
    - Apresentará um aviso com o value do campo "valor";
    - Apresentará no escopo a mensagem do value do campo "valor";
    - Não ocorrerá redirecionamento na página, pois dentro do atributo está inserido onsubmit existe além da função "mostrarDados()", o "return false" que para a execução do submit.
HTML:

<form method="post" action="" onsubmit="mostrarDados(); return false;">
    Valor do saque

        <input id="valor" name="valor" type="text" placeholder="R$ 00,00" class="form-control input-md">

    <input type="submit" name="confirmar" value="Confirmar">
</form>

JAVASCRIPT:

Atribuir input a variável


Number of tries until Failure with n different independent Bernoulli experiment

Question: I have 3(n) coffee machines in an office. I have a historical log of these machines, and I know in the last ten days(t) how many times they failed to make a coffee.
For example:

machine 1: 10 failures in 100 tries
machine 2: 20 failures in 1000 tries
machine 3: 50 failure in 300 tries

I know that I can use the Expected Value of the Geometric distribution for a machine to calculate the mean number of tries before the first failure.
Question:
How many times does one person need to use coffee machines in this office until he gets the first failure?
How can we formulate this problem?
Comment: If I get it correctly, it depends on the probability that person chooses which machine to use. Does this person always use the same machine? Does each machine have a probability of 1/3 to be chosen?
Comment: After reading the answer of @Tim, I think his approach is the most straightforward approach with considering people uniformly choose the machines.

I think it is possible to take the number of tries each machine had into account and calculate the possibility of using that machine.
Answer: As noticed in the comment by @periwinkle, the result depends also on the probability of the machine by a user. I would assume that the machines are chosen uniformly at random, but any other probability distribution can be used instead. Recall the law of total probability
$$
P(A) = \sum_i P(B_i \cap A)
$$
The probability of a failure of $i$-th machine depends on the probability of picking this machine $\pi_i$ and the probability of failure $p_i$. The events are independent, so jointly the probability is $\pi_i p_i$, and the overall probability of failure is
$$
p = \sum_i \pi_i p_i
$$
That's the probability we would plug-in into the Geometric distribution to calculate the expected number of runs till failure $1/p$.
Let's verify this using simulation
<code>pi <- c(1/3, 1/3, 1/3)
p <- c(10/100, 20/1000, 50/300)

sim <- function(n) {
    for (i in 1:n) {
        # pick machine
        k <- sample.int(3, size=1, prob=pi)
        # simulate failure or no failure with probability p[k]
        if (runif(1) < p[k]) {
            return(i)
        }
    }
    warning("no success")
}
</code>
That gives us the following result that confirms the analytical solution:
<code>> set.seed(42)
> summary(replicate(50000, sim(1000)))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   1.00    3.00    7.00   10.44   14.00  116.0
> 1/sum(pi * p)
[1] 10.46512
</code>
Comment: @DSaad not sure what you mean by "number of tries" but if machines are choosen not uniformly at random but users prefer to use some machines more than others, then you change the $\pi_i$ probabilities to something else. On another hand, if you want to assume that the events are not independent (e.g. if user used the machine he is more likely to use it again) this would get *much* more complicated and more details would be needed to answer it.
Comment: @DSaad so just change $\pi_i$ to any values such that $\sum_i \pi_i = 1$.
Comment: Thanks a lot! Is it possible to take the number of tries each machine had into account and calculate the possibility of using that machine, instead of assuming that the machines are chosen uniformly at random?
Comment: I meant by "number of tries" that, for example, the chance of machine 1 being used is  100 / (100 + 1000 + 300).


Can time series be used to predict series which has a changing trend?

Question: Supposing I have a stock return series from 2000-2013.
Looking at the data, it has pattern of long trending bull market: 2001-2008, and 2010-2013, while it also has great reversal in 2008 financial crisis.  So if we denote the daily return series as $y_{t}$, there are two properties:
(1) $y_{t}$ has a non-zero mean part in the return series, denoted as $r_{t}$, which is something like a "macro factor".
(2) $r_{t}$ can be correlated with $r_{t-1}, r_{t-2},... $, during the good years, or negative correlated during bad years.
Typically what people would do using ARMA model, is firstly estimate $r_{t}$ as $r_{t} = (y_{t-1} + ... + y_{t-k})/N $,  and feed the series $y_t - r_t$ into ARMA identification toolbox to get the coefficients.
However, it really turns out that picking $k$ can be quite critical :  the $k$ value tells how much we believe the past trend, which is really hard to say. For example, at 2008 when the financial crisis happen, you have no reason to believe the market would continue as the previous good years; while at the good years, you would assume the global economy works well and you can use a long $k$ window.  Or maybe it is best not to make the model depend on choosing $k$.
So back to the question regarding time series modeling:  What people usually do if the time series has a non-zero mean, which is quite different at different time point $t$ ?
Comment: You can try taking the first difference, run an ARIMA, and inspect the ACF afterward to see if it worked. That should work better than demeaning the series using r*t*
Answer: Yes.  Box-Jenkins ignored deterministic variables like trend when they laid out their ARIMA approach.  Here is a blog post that speaks to your issue of identifying trends and searching for them as opposed to just leaning on ARIMA.
http://www.autobox.com/cms/index.php/blog/entry/ages-at-death-of-the-kings-of-england
Comment: I am surprised to see such a dataset (age of kings at death) analyzed using methods intended for time series with regular intervals, because it differs fundamentally from them: it consists of interdependent *events* rather than observations at a given sequence of times. Not only is there then no reason to suppose that standard time series methods will be much good (except by accident or luck), one would expect that methods of data analysis more appropriate for such data would perform better.
Comment: Agreed. It comes from Rob Hyndman's time series data library found here http://robjhyndman.com/tsdldata/misc/kings.dat Yes, I point that in the blog.  So, imagine it is just pizza sales and that pain all goes away.


calling 2 url with web client spring boot with in sequential order

Question: This question is web flux spring boot related. I have 2 web clients, one that makes a call to a url to get accounts then I send those accounts to an object mapper to get mapped to a POJO. Then I use second web client to make a call to second url with those accounts. Can anyone tell me how I can make them execute in sequence ?
In current scenario by the time I get response back from first web client and response goes to object mapper, second web client fires up and accounts in request body goes as null.
Any input would be appreciated.
thanks.
Answer: Cast the WebClient output to Mono<YouPojo> and use flatMap to call the next service you want.
    WebClient client1 = null;
    WebClient client2 = null;

    Mono<String> finalResult = client1.get()
            .uri("https://example.com/service-1")
            .retrieve()
            .bodyToMono(String.class)
            .flatMap(result -> client2.post()
                    .uri("https://example.com/service-2")
                    .body(BodyInserters.fromFormData("result", result))
                    .retrieve()
                    .bodyToMono(String.class));

As you see in the above, we have two clients client1 and client2 that we use them to call the corresponding services. You can combine the results of Monos with map or flatMap operations.
Comment: thanks, so I have another file that takes the accounts retrieved from first call and use it in request body for second call. According to your solution that will happen in BodyInserters? because request body has to match the format for 2nd web client post call. Accounts from first call will be just part of bigger POJO in request body.
Comment: Yeah, probably it can solve your issue. I used the `BodyInserters#fromFormData` to pass the result to `client2`.
Comment: so i think in faltMap with { } I can have something like: Pojo.setAccounts(request.getAccounts) and then send that sepcific pojo in requestbody. Thanks.
Comment: Yes, you can do whatever you want and then subscribe to the latest `Mono` to run the flow sequentially.
Answer: By default webclient is a non-blocking I/O. Considering you are using ObjectMapper to convert the response from the first call. You can use block() method to make it blocking call.
Example:
webClient.get().uri("http:/sdfsdf.com/sdsd").retrieve().bodyToMono(String.class).block();

Comment: It is not recommended to call the block operation in reactive system.
Comment: I tried using .block() but got exception (cannot use .block()) so had to remove it
Comment: can you tell me what is the exception you are receiving ?
Comment: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-4


Calculating the confidence interval for the mean value from a sample

Question: The central limit theorem states that the means of many different samples with n>30 from a population that follows an arbitrary distribution are normally distributed.
If you now have the information from only one sample, how do you calculate the confidence interval, which states that with 95% the true mean value of the population is in this interval?
The normal distribution of the mean values is not available, since only one sample was taken, with only one mean value $\overline{x}$ and standard deviation s.
The estimated value for the mean is simply $\overline{x}$, the estimated value for the standard deviation is simply s, that is trivial. But we now want to have an interval with a confidence of, say, 95%.
Let's imagine the normal distribution that results from many samples. We don't have the results of many samples, but let's imagine this normal distribution. In the middle is $\mu_\overline{x}$ and to the left and right of it are the standard deviations, which of course we don't know either. Now we think of a range as being drawn in, which contains 95% of all sample means. Exactly this range we can also form around our determined sample mean $\overline{x}$ so that we can state: with 95% certainty the mean of all sample means (i.e. the population mean) lies in this area.
So what we need to calculate now is the 95% interval of the normal distribution, because we can then "put" this range over the sample mean x to get the limits of the interval. This is:
Lower limit = $\overline{x}$ - 1.96 * $\sigma_\overline{x}$
Upper limit = $\overline{x}$ + 1.96 * $\sigma_\overline{x}$
Whereas $\sigma_\overline{x}$ denotes the standard deviation of the normal distribution.
How do you get the $\sigma_\overline{x}$? I have read that it can be calculated by dividing the standard deviation s of the population by $\sqrt{n}$ . But since we don't have the sigma of the population, we divide the standard deviation s (of the sample) by $\sqrt{n}$. But why can we just use s here? Why can we use just the standard deviation of the sample to calculate the interval length of the normal distribution? A too high standard deviation of the sample makes the interval too large, a too low standard deviation makes the interval too small.
Comment: "The central limit theorem states that the means of many different samples with n>30 from a population that follows an arbitrary distribution are normally distributed." ... Well no, most of those things are not what the actual CLT says. You don't need many samples, the statement of the theorem has nothing to do with n>30, but instead with asymptotic behaviour of a standardized mean or sum, and the distribution cannot be completely arbitrary (there are some conditions). Just about any undergrad level book on mathematical statistics should cover a basic form of the theorem.
Comment: It's our best guess ("estimate" in statistical language) of the population value.
Comment: if the observations in your sample vary a lot, we are less certain where the true mean lies.
Answer: I think the sequence of your story is incorrect (the high level picture). From a high level perspective, you start first with a (random) sample from which you would like to draw inferences about a given population.
Still from a high level perspective, the CLT states that, given a sufficiently large sample size, the sampling distribution of the mean for a variable will approximate a normal distribution. It states also that as the sample size increases, the mean of the sampling distribution equals the population mean and the standard deviation equals $\frac{\sigma}{\sqrt{n}}$
Where $\sigma$ is the population standard deviation and n the sample size (of the sampling distribution).
You can find an explanation for $\frac{\sigma}{\sqrt{n}}$ here : https://stats.stackexchange.com/a/60486/321901
Most of the time, we do not know the population standard deviation $\sigma$ so we estimate it through the standard error (of the sampling distribution) $se = \frac{s}{\sqrt{n}}$ where s is the standard deviation of the sample (then t-test must be used instead of z-test... but it deserves more than a "high level" answer).
Answer: We could apply S there since this estimate of sigma (std err of population) is unbiased and consistent estimate.
Comment: How do you figure that capital $S$ is unbiased? By Jensen's inequality, taking the square root of an unbiased estimator introduces bias.
Comment: $s$ is not an unbiased estimate of $\sigma$. $s^2$ is unbiased for $\sigma^2$, and then Jensen's inequality says that $s$ is biased for $\sigma$.
Comment: I doubt that these authors can prove something that isn't true. @Dave has the key point here.
Comment: Yes, we use instead of sample value unbiased estimate of the sigma in all times. ***S*** is unbiased: S = n/(n-1) * s, where s is std err from sample.
Comment: Simple proof you could find in this book: Fetsje Bijma, Marianne Jonker, Aad van der Vaart, 2016 An Introduction to Mathematical Statistic, p 52


How to set parameters of basler using python, _genicam.AccessException: Node is not writable

Question: Hi everyone Is this correct way to set the basler camera parameters using basler?
Is any other way to access camera after calling camera.stratGrabbing() function?
I am getting error like :
return _genicam.IInteger_SetValue(self, Value, Verify)
_genicam.AccessException: Node is not writable. : AccessException thrown in node 'Height' while calling 'Height.SetValue()' (file 'integert.h', line 77)
import cv2
from pypylon import pylon

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
converter = pylon.ImageFormatConverter()
camera.Open()
camera.CenterX=False
camera.CenterY=False

# demonstrate some feature access
new_width = camera.Width.GetValue() - camera.Width.GetInc()
if new_width >= camera.Width.GetMin():
    camera.Width.SetValue(new_width)

numberOfImagesToGrab = 100
camera.StartGrabbing()
camera.Open()
print("Max gain",camera.Gain.Max)
print("Min gain",camera.Gain.Min)
print("Max ExposureTime",camera.ExposureTime.Max)
print("Min ExposureTime",camera.ExposureTime.Min)
i=0
j=0
while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)

    if grabResult.GrabSucceeded():
        i+=1
        j+=1
        if i==700:
            print(i)
            #camera.Open()
            camera.Height.SetValue( 200)
            camera.Width.SetValue( 300)

            camera.Gain=5
            camera.ExposureTime=8000
            i=0
        if j==500:
            #camera.Open()
            print(j)

            camera.Height.SetValue(1088)
            camera.Width.SetValue( 2048)

            camera.Gain=20
            camera.ExposureTime=8000
            j=0
        # Access the image data.

        print("AMAR :",camera.IsOpen())
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.imshow("image",img)
        cv2.waitKey(1)

grabResult.Release()
camera.Close()

Comment: Does this answer your question? ["Node is not writable" exception after a camera crash](https://stackoverflow.com/questions/64660434/node-is-not-writable-exception-after-a-camera-crash)
Answer: # cam stop
cam.AcquisitionStop.Execute()
# grab unlock
cam.TLParamsLocked = False

cam.OffsetX = 0
cam.OffsetY = 0
cam.Width = cam.Width.Max // 4
cam.Height = cam.Height.Max // 4

# grab lock
cam.TLParamsLocked = True
# cam start
cam.AcquisitionStart.Execute()

With the lock it may help you
Comment: Still camera crashing
Comment: Yes, It may help you
Comment: The difference between Value = 540 and Min = 8 must be dividable without rest by Inc = 8. : OutOfRangeException thrown in node 'Width' while calling 'Width.SetValue()' (file 'integert.h', line 79)    I am getting error like this.
Answer: You cannot change camera's Width/Height parameters while camera is in grabbing state (after camera.StartGrabbing() ). Only RW parameters regarding AOI settings is OffsetX/OffsetY and some other model dependent ones (CenterX  /CenterY / MirrorX / MirrorY) since they do not require reallocating internal memory buffers inside camera.
Put any Width/Height changes before grabbing loop, and after camera.Open().
From your code I presume you want to make some kind of dynamic parameter changes while camera is grabbing. Please consider Sequencer function of Basler ace cameras. In the nutshell - camera can change some parameters autonomically, frame by frame, without any user commands. However still, it won't help you in your Width/Height case.
Further reading below:
https://docs.baslerweb.com/sequencer-%28usb-cameras%29
https://docs.baslerweb.com/sequencer-(gige-cameras)
Comment: Actually I tried and it working but it is problem with changing width and height we gonna follow some  rules for changing width and height
Comment: Ok, agreed. You can also try the TrySetValue() method combined with GetInc() to make it more generic for different cameras that have different value increments.
Comment: if you mind please suggest me how to change , I just tried  how to change camera parameters and I commented and after that I didn't look
Comment: https://pastebin.com/0arhd2Kp


Como evitar a reinicialização dos elementos da tela ao virar o celular?

Question: Tenho uma Activity com apenas um cronômetro basicamente, e, ao virar o celular, o cronometro é zerado.
Existe algum evento que faz com que a Activity seja reiniciada ao fazer isso? Gostaria de impedir esse comportamento.
Answer: O problema
Quando você muda a orientação do aparelho, o Android destrói e recria a Activity. O que está acontecendo é que você provavelmente está iniciando o seu cronômetro no onCreate. Como o onCreate está sendo chamado de novo, o estado o seu cronômetro está sendo reiniciado.
O que você deve fazer vai solucionar não apenas o problema da rotação mas o problema de quando sua Activity é destruída em segundo plano porque o sistema precisou de memória (você pode simular essa situação habilitando "Não manter atividades" nas configurações de desenvolvedor do aparelho)
Imagino que seu código seja mais ou menos assim:
public class CronometroActivity extends Activity  {
    // ...
    // campos da sua classe
    // ...
    private Cronometro meuCronometro;
    // ...
    // mais campos da sua classe
    // ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        // inicialização dos elementos da tela
        // ...

        this.meuCronometro = new Cronometro();
        atualizarCronometroDaTela();

    }

    // demais métodos

}

Talvez você não tenha uma classe Cronometro e sim outras variáveis mais simples para compor o estado do cronômetro, mas o princípio é o mesmo.
Solução
Você deverá sobrepor o método onSaveInstanceState da Activity, que é chamado quando a Activity é destruída pelo sistema. O sistema envia a esse método um Bundle, chamado aqui de estadoDeSaida, dentro do qual você colocará os dados que quer persistir entre reinicializações da tela. Como você talvez já sabe, um Bundle aceita dados dos tipos primitivos do Java, Strings, objetos Serializable e objetos Parcelable (dentre alguns outros).
Presumindo aqui que a classe Cronometro implementa Parcelable, o código ficaria assim:
@Override
public void onSaveInstanceState(Bundle estadoDeSaida) {
    super.onSaveInstanceState(estadoDeSaida);
    estadoDeSaida.putParcelable("chaveDoMeuCronometro", meuCronometro);
}

Após a recriação, o onCreate será chamado, e será passado para ele esse mesmo Bundle no parâmetro savedInstanceState, em vez de null que fica lá quando a Activity está iniciando normalmente. A partir dele, você deverá retirar os dados que havia colocado e recriar o seu cronômetro.
Nesse exemplo, ficaria assim:
@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    // inicialização dos elementos da tela
    // ...
    if (savedInstanceState) {
        this.meuCronometro = savedInstanceState.getParcelable("chaveDoMeuCronometro")
    } else {
        this.meuCronometro = new Cronometro();
    }

    atualizarCronometroDaTela();
}

Dessa forma, sua Activity está protegida da perda dessa informação e seu cronômetro não vai zerar.
Entretanto, no caso específico do cronômetro, talvez você precise de uma solução mais engenhosa para evitar a perda de segundos nos quais o cronômetro ficaria congelado, esperando a destruição e recriação. Essa solução é para o caso mais simples (e mais comum) no qual isso não é importante.
Comment: Falta acrescentar que a classe *Cronometro* tem de implementar *Parcelable*. No entanto a solução mais simples seria guardar o estado em milissegundos do cronómetro e o *time* actual. Ao ser recriada a Activity, recria-se o cronómetro com um estado igual à soma do estado gravado com a diferença entre o *time* actual e o *time* guardado, assim evita-se implementar Parceable e resolve-se o problema da paragem do cronómetro.
Comment: É claro que num exemplo simples como este pouca importância tem. Suponha que a Activity representa uma corrida de carros, além do cronómetro teria de passar a lista de carros. Tanto *Cronometro* como *Carro* teriam de implementar *Parcelable*. O que eu digo é que é melhor apenas passar o **estado da corrida**. Uma classe que implemente *Parcelable* e que represente esse **estado**, por ex: valor do cronómetro e posição dos carros. Pense em outras situações em que possa haver um número maior de classes envolvidas. Em relação a uma referência: não sei se existe.
Comment: Certo. Acho que entendi o conselho. Vou manter isso em mente. Obrigado pelo esclarecimento. :)
Comment: @ramaral Eu cheguei a dizer "presumindo que Cronometro implementa Parcelable". :) Não quis entrar em detalhes de como fazer isso porque seria assunto pra outra pergunta. E é muito interessante a sua ideia de solução. Simples o bastante, mesmo dando um bom resultado.
Comment: Tem razão, peço desculpa, não me apercebi dessa frase.
Comment: Sem problema. :)
Comment: Uma nota mais em relação à minha solução: não faz sentido passar classes com comportamento entre *activities*, o que deve ser passado é o seu estado para, a partir dele, recriar a nova instância.
Comment: Não sei se entendi. Do jeito que você falou, eu entendi que você estava passando dois _timestamps_, algo como dois longs.
Comment: Mas depois você falou como se estivesse deixando de recomendar a solução que deu. Ou entendi errado?
Comment: O que quero dizer é que não se deve passar o "objecto cronómetro" mas sim o seu **estado** (valor marcado no mostrador).
Comment: Na minha cabeça, `Cronometro` é um objeto que contém apenas os dados. De qualquer forma, possui algum recurso de referência sobre esse princípio que você mencionou?
Comment: Eu via *Cronometro* também com comportamento, tipo: `start()`, `stop()`, `reset()` etc. Por que passar(neste caso) a classe toda se só é necessário um valor?
Comment: Compreendo que não faz sentido passar objetos que sejam donos de recursos como _streams_ e conexões de rede, mas em casos como o do cronômetro que tenha esses métodos aí que você citou, não vejo mal. Talvez eu não esteja enxergando além desse exemplo, por isso pedi uma referência a esse princípio. Com outros exemplos talvez eu compreenda melhor.
Answer: Ótima solução Pablo Almeida, usei esta mesma, com um objeto Serializable, assim não precisei implementar nada do Parceable.
Mas tem que fazer o cast do objeto no retorno do onCreate...
@Override
public void onSaveInstanceState(Bundle estadoDeSaida) {
    super.onSaveInstanceState(estadoDeSaida);
    estadoDeSaida.putSerializable("chaveDoMeuCronometro", meuCronometro);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    // inicialização dos elementos da tela
    // ...
    if (savedInstanceState) {
        this.meuCronometro = (Cronometro) savedInstanceState.getSerializable("chaveDoMeuCronometro")
    } else {
        this.meuCronometro = new Cronometro();
    }

    atualizarCronometroDaTela();
}


Can Drush 9 be used with this core version?

Question: I have a Drupal 7 site that was not built with Composer. I have been using Drush 7 with it, but about a week ago, drush wd-show --tail stopped working, I'm not getting any output. I installed Drush 9, but I'm having difficulty configuring it.
I created an XML file, per the example., Instructions say to put aliases in $PROJECT/drush/sites/self.site.yml but I don't have a project root with a composer.json – can I get this to work without using Composer to build my site? Or is Drush 9 depending on the Drupal site using Composer?
I'm running the local version of the site in MAMP on my Mac. I created sites/drush/sites/self.site.yml – it contains:
local:
  root: /Users/pglatz/dev/wdocs/wdocs-code/www
  uri: 'https://www.whiskerdocs-local-qa.pglatz.com/'

The command I'm using is:
$ drush9 @local --alias-path=/Users/pglatz/dev/wdocs/wdocs-code/www/sites/drush/sites --root=/Users/pglatz/dev/wdocs/wdocs-code/www

When I run it, I get the error:

Command wd-show was not found. Pass --root or a @siteAlias in order to run Drupal-specific commands.

But I am passing --root and a site alias. Likely a dumb syntax error on my part, but I can't figure it out. What am I doing wrong? And how can I get Drush to be working properly again?
Answer: No. Drupal 7 requires Drush 8.

See https://www.drush.org/latest/install/#drupal-compatibility

Furthermore, Drush 9 can't be installed globally.

See https://www.drush.org/latest/install/
Comment: @Kojo – The docs you are linking to are outdated. See https://www.drush.org/latest/install/ for the latest. I probably should update the links in my answer as well.
Comment: it seems that this information is outdated ? https://docs.drush.org/en/7.x/install/#pick-a-version There is says that Drush 9 is compatible with D7/D8. Or are the docs mistaken probably ? Because indeed I could not get Drush 9 work with D7 ;-)
Comment: Oh thanks. I'm wondering why drush maintainers then don't redirect to the new docs instead of leaving wrong things around ;-)


Definition of limit in category theory - is $X$ a single object of $J$ or a subset of $J$?

Question:
Let $F : J → C$ be a diagram of type $J$ in a category $C$. A cone to
  $F$ is an object $N$ of $C$ together with a family $ψ_X : N → F(X)$ of
  morphisms indexed by the objects $X$ of $J$, such that for every
  morphism $f : X → Y$ in $J$, we have $F(f) \centerdot ψ_X = ψ_Y$. http://en.wikipedia.org/wiki/Limit_(category_theory)

So, is $X$ here referring to a single object of $J$ or several objects of $J$ grouped together? (In terms of set, is $X$ an element of $J$ or a subset of $J$?)
Comment: I think the question that you are asking pertains to identifying what $X$ is in the quoted portion. It is simply an object of the category $J$ and $f$ is merely a morphism in $hom_J(X,Y)$.
Comment: Perhaps one or two answers are missing which explain the same thing once again?
Answer: $\hskip 0.1in$
A diagram of type $J$ can be thought of as a copy of $J$ (formally, the image of the category $J$ under the functor $F$) sitting inside another category $C$. All of the objects and morphisms in $J$ are carried over to a relabelled diagram sitting inside $C$ (I omitted the arrows in purple and blue because I was lazy). The idea of a cone is that you can append an object $N$ to this diagram in the sense that you put the object in your sights and then attach a morphism $\psi_A:N\to FA$ for each $A\in{\rm Ob}(J)$, and as a result you will have a new commutative diagram (blue + light blue above).
Answer: The cone is indexed by all objects of $J$.
Before dealing with limits in general, you should understand products and their universal property. Of course all factors are involved.
Answer: There is a collection of $\psi_X$. One $\psi$ for each object in $J$. So if $J$ has 3 objects $a,b,c$ you have $\psi_a, \psi_b,\psi_c$. But, in general, you cannot choose the $\psi$ as you wish.  The various $\psi$ have to respect some constraints among themselves: if there is a $f:X \to Y$ then, even if you can freely choose $\psi_X$,  $\psi_Y$ is automatically determined by $\psi_Y = F(f) \centerdot \psi_X$.
So, to answer explicitly to the question in your title: each $X$ is a single object in $J$. The collection of $X$ is the collection of all objects in $J$


Debugging hook_node_access in failing test

Question: Related to this question, I have a functional test asserting that hook_node_access is doing what it should for different users.
While I can access edit and delete routes, I add an additional check for $node->access() and see what its result is:
$low_user->set('field_suppliers', $supplier->id());
$low_user->save();

// With the supplier set, now we should have access.
$this->drupalGet('/node/' . $offer->id() . '/edit');
$this->assertSession()->statusCodeEquals(200);

$this->drupalGet('/node/' . $offer->id() . '/delete');
$this->assertSession()->statusCodeEquals(200);

// Why is this required?
$offer->save();
$this->assertSame(TRUE, $offer->access('update', $low_user));
$this->assertSame(TRUE, $offer->access('delete', $low_user));

Everything passes except the last two statements unless I save the node. I assume this is due to the line:
AccessResult::allowedIf(!empty($user_has_suppliers_with_node))->cachePerUser()->addCacheableDependency($node);

I also tried using resetCache and invalidateTags against the node, neither worked. Only saving the node will the test pass - but this is not required in implementation.
Edit:
Swapping $offer->save() with \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions(); also works.
I suppose I should be simulating an admin logging in and updating that user account and hitting "Save" on the user form, but I also had trouble getting the edit array to do anything for an autocomplete field. That is the difference between what is implemented and how its being tested (user action vs code).
Comment: Did you try to invalidate the cache contexts?
Comment: This says cache contexts cannot be invalidated: https://drupal.stackexchange.com/a/252891/57
Comment: The access result is cached, for example with `AccessResult::allowed()->cachePerPermissions()`. `cachePerPermissions()` adds the *user.permissions* cache context.
Answer: This looks like the access result is stored in the static cache. Try to clear it before testing node access:
\Drupal::entityTypeManager()->getAccessControlHandler('node')->resetCache();

Comment: This is only required for testing. In a normal request your implementation should work without problems because the static cache doesn't persist between requests.
Comment: Yeah, this also does it. Is this required for testing, or is the implementation not quite right?
Comment: Yes, [`EntityAccessControlHandler::access()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityAccessControlHandler.php/function/EntityAccessControlHandler%3A%3Aaccess/9.3.x) tries to retrieve the access value from its static cache. The same issue could happen to any code that creates an entity and calls its `access()` method without first saving the entity.


WordPress removing post from Recent Post

Question: I would like for my code to some how remove the post which is displayed in the first query and show the 3 recent post after the one displayed above.
Top post:
<?php query_posts('posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post();?>

<?php the_title(); ?>

<a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail('indexsavedimage'); ?>

<?php the_excerpt(); ?>
<?php endwhile; endif; wp_reset_query();?>

Recent Post:

<?php query_posts('posts_per_page=3'); if (have_posts()) : while (have_posts()) : the_post();?>

<a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail('newsimages'); ?>

<a class="title" href="<?php the_permalink(); ?>">
<?php the_title(); ?>

<?php the_excerpt(); ?>

<?php the_category( ', ' ); ?>
<a href="<?php the_permalink(); ?>" >

<?php endwhile; endif; wp_reset_query();?>

Comment: So, you want to take the first top posts (3) and display the first one in a particular style, then again with the other 2 in a different style? Sorry, misread that.
Comment: @phatskat You see the top part that is very large and is being displayed at the bottom as well? I would like to remove that post from the bottom and display the three recent post after that post instead.
Answer: First, read up on WP_Query if you haven't and get your arguments ready - http://codex.wordpress.org/Class_Reference/WP_Query
An important argument is posts_per_page
<?php
$posts = WP_Query(array(
    'posts_per_page' => 4,
    # ...
));

# From WordPress's example, modified of course
$i = 0;
if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
                $the_query->the_post();

                if(!$i)
                {
                    // First style
                    echo "Some HTML for the first post,
";
                }
                else
                {
                    // Code for second style
                    echo "HTML for the rest!";
                }

                $i++;
        }
} else {
        // no posts found
}

EDIT: Sorry, mismisread? lol. Here you go
Comment: Perfect, Exactly what I was looking for.


Utilizar outra caixa de pesquisa sem ser o padrão do DataTables

Question: Olá, que saber se é possível eu utilizar uma caixa de pesquisa que eu faça na mão e acrescente da minha tabela que tem o DataTable sem afetar o plug-in. Por que um exemplo eu tava pensando fazer filtragem por um input de data. Agredeço desde já!
Answer: Consegui resolver o meu problema:
colocamos isso no style:
<code>    <style>
    .dataTables_filter {
        display: none;
    }
</style>
</code>
Meu js
<code> $("#filter-input").keyup(function() {
     var pesquisar = $(this).val();
     tabela.search(pesquisar).draw();
     });
  });
</code>
E nas configurações do DataTable tem deixar:
<code>"searching": true
</code>


Can I register Laravel Auth::routes() under a prefix group?

Question: Hi I'm working with Laravel 5.3 and I notice that the default Auth routes are registered using Auth::routes();. Is it possible to encapsulate these routes under a prefix group? Eg:
Route::group(['prefix' => 'admin'], function(){
    Auth::routes();
});

Comment: Yes, I have. I get a NotFoundHttpException
Answer: Yes You can use it but to make sure now you should prefix your url with admin
e.g
If previously you access your url like http://localhost/auth/login
Now you should prefix your url with admin before auth like below
http://localhost/admin/auth/login
Issue is in your middleware you use auth middleware
Go to App/http/Middleware/Authenicate.php and there go to handle mathod in a class.
There you see return redirect()->guest('auth/login'); please replace it with
return redirect()->guest('admin/auth/login')

Comment: I get this part. How do I make my `auth` middleware redirect me to `/auth/login`.
Comment: you prefix your auth/login routes in admin how can u get to /auth/login ?
Comment: I am using Laravel 5.3, it has no `Authenticate.php` in middleware's folder.
Comment: go to app/Exceptions/Handler.php there is a method unauthenticated
there you find a redirect()->guest('login'); replace it with return redirect()->guest('admin/auth/login')
Comment: for further explanation check that link http://laravel.io/forum/08-23-2016-laravel-53-auth-middleware-redirect-guests
Answer: Yes you can do it.
Auth::routes() is just a wrapper method around normal route registering.
You can check the source code here.
Answer: Yes, you can do it with no restrictions.
Comment: I am trying to access a route with uri `/admin
Comment: Route::group(['prefix' => 'admin'], function(){
 Auth::routes();
});

Route::group(['middleware' => 'auth:web'], function(){
 Route::group(['prefix' => 'admin'], function(){

     Route::get('/', 'AdminController@index');

 });
});
Comment: @TaeKwonDev
`Auth::routes();` haven't `/` route. You can try in as `/admin/login`
Comment: Solution for `/admin` route
`Route::group(['prefix' => 'admin'], function(){
    Auth::routes();

    Route::get('/', function (){
        return view('welcome');
    });
});`
Comment: please have a look at the code I posted in the answers section to see my actual problem.


Which are the correct sum of squares for repeated measures ? (balanced example)

Question: Below are three ways to fit a MANOVA model in R and to extract ANOVA tables from the fitted model. The design is balanced. All methods give different results.
The data are 5 groups of 4 individuals with three repeated measures shown in figure below

and simulated with the following R code:
R code:
library(ez)
library(nlme)
library(lattice)

##################################
### BALANCED DESGIN ##############
##################################
groups <- c("A","B","C","D","E")
n.groups <- length(groups)
n.individuals <- rep(4,n.groups) # four individuals per group
id <- unlist(lapply(1:n.groups, function(i) paste0(groups[i],1:n.individuals[i]))) # individual id
n <- length(id)
set.seed(666) # diabolic seed
dat <- data.frame(id=id,
           group=rep(groups, each=4),
           x=round(rnorm(n,2),1),
           y=round(rnorm(n,4),1),
           z=round(rnorm(n,6),1))
ldat <- melt(dat, id=c("id","group")) # data in long format
xtabs( ~ group+variable, data=ldat) # check design
# plot data:
xyplot(value ~ variable | group, groups=id, data=ldat,
       panel = function(...){
          panel.grid(h=-1,v=-3)
          panel.superpose(..., type = "o", lty=2, pch=16)
        }
)

#### 1st method : car package ####
mfit <- lm( cbind(x,y,z)~group, data=dat )
idata <- data.frame(variable=c("x","y","z"))
aov.carII <- Anova(mfit, idata=idata, idesign=~variable, type="II")
aov.carIII <- Anova(mfit, idata=idata, idesign=~variable, type="III")

#### 2nd method : ez package ####
aov.ezI <- ezANOVA(data = ldat,
               dv = value,
               wid = id,
               within = .(variable),
               between = group,
               type=1
)
aov.ezII <- ezANOVA(data = ldat,
                   dv = value,
                   wid = id,
                   within = .(variable),
                   between = group,
                   type=2
)
aov.ezIII <- ezANOVA(data = ldat,
                   dv = value,
                   wid = id,
                   within = .(variable),
                   between = group,
                   type=3
)

### 3rd method : generalized least-squares fitting  ###
gfit <- gls(value ~ group*variable, data=ldat,
            correlation=corSymm(form= ~ 1 | id),
            weights=varIdent(form = ~1 | variable))

Comparison of ANOVA tables:
With gls() there are two possible ANOVA tables:
>  two possible ANOVA tables with gls :
> anova(gfit, type="sequential")
Denom. DF: 45
numDF   F-value p-value
(Intercept)        1 1401.9971  <.0001
group              4    2.3793  0.0658
variable           2   79.5687  <.0001
group:variable     8    1.4759  0.1929
> anova(gfit, type="marginal") # differs from sequential except for interaction
Denom. DF: 45
numDF   F-value p-value
(Intercept)        1 23.527654  <.0001
group              4  1.383566  0.2548
variable           2 14.166517  <.0001
group:variable     8  1.475904  0.1929

The type I ANOVA with ez (type I is not available with car):
> #########################
> ### TYPE I STATISTICS ###
> #########################
> aov.ezI$ANOVA  # close to sequential anova(gfit)
Effect DFn DFd         F            p p<.05       ges
1          group   4  15  1.996615 1.467733e-01       0.1651121
2       variable   2  30 80.595661 8.605897e-13     * 0.7715479
3 group:variable   8  30  1.093567 3.945923e-01       0.1549055

> # car: no Type I available

The type II ANOVA:
> ##########################
> ### TYPE II STATISTICS ###
> ##########################
> aov.ezII$ANOVA # identical to Type I ez
Effect DFn DFd         F            p p<.05       ges
2          group   4  15  1.996615 1.467733e-01       0.1651121
3       variable   2  30 80.595661 8.605897e-13     * 0.7715479
4 group:variable   8  30  1.093567 3.945923e-01       0.1549055

> aov.carII # car is close to ez but differs except for the first factor

Type II Repeated Measures MANOVA Tests: Pillai test statistic
Df test stat approx F num Df den Df    Pr(>F)
(Intercept)     1   0.98072   763.01      1     15 2.810e-14 ***
group           4   0.34744     2.00      4     15    0.1468
variable        1   0.91386    74.26      2     14 3.519e-08 ***
group:variable  4   0.51812     1.31      8     30    0.2758
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The type III ANOVA :
> ##########################
> ### TYPE III STATISTICS ###
> ##########################
> aov.ezIII$ANOVA # identical to Type II except for the second factor
Effect DFn DFd         F            p p<.05       ges
2          group   4  15  1.996615 0.1467732928       0.1651121
3       variable   2  30 11.560705 0.0001895724     * 0.3263455
4 group:variable   8  30  1.093567 0.3945922933       0.1549055

> aov.carIII # identical to Type II except for the second factor and intercept

Type III Repeated Measures MANOVA Tests: Pillai test statistic
Df test stat approx F num Df den Df    Pr(>F)
(Intercept)     1   0.92734  191.436      1     15 6.038e-10 ***
group           4   0.34744    1.997      4     15 0.1467733
variable        1   0.65384   13.222      2     14 0.0005956 ***
group:variable  4   0.51812    1.311      8     30 0.2757596
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Here is in addition the SAS output which does not depend on the chosen type (tpes I, II and III yield the same table):
proc mixed data=ldat ;
class id group variable ;
model value = group variable group*variable / s htype=2 ;
repeated variable / subject=id type=un ;
run;quit;

I am particularly interested in Type II sum of squares. The different ANOVA tables are close from each other but there are some differences (the F value and/or degrees of freedom). I'd like to understand these differences.
For the ez package I don't even know whether the results are given under the sphericity assumption.
Answer: I'm afraid SAS gives "wrong" Type II test statistics.
I have learnt a lot of things.
Type II hypothesis
According to these slides by G. Monette and J.
Fox
the Type II test of an "effect" corresponds to a test for the nullity of a multivariate linear hypothesis about the parameters, say $H_0\colon\{L_{1\mid 2}\beta=0\}$ for a certain matrix $L_{1 \mid 2}$.
Actually $H_0$ is a ``conditional'' hypothesis (see the slides) and $L_{1\mid 2}$ depends on the asymptotic covariance matrix $V$ of $\hat\beta$.
For mixed models and/or models with a repeated structure the $L_{1\mid 2}$ matrix is obtained by estimating $V$,
but for classical linear models the $V$ matrix is proportional to ${(X'X)}^{-1}$ and there's no need to estimate it.
Classical linear model example
Consider for instance a classical two-way ANOVA model:
> library(car)
> dat <- Soils[-c(1,2),] # delete two rows of the Soils dataset
> xtabs(~Contour+Depth,data=dat) # the design is unbalanced
            Depth
Contour      0-10 10-30 30-60 60-90
  Depression    4     4     4     4
  Slope         4     4     4     4
  Top           2     4     4     4
> fit <- lm(pH~Contour*Depth, data=dat)

I have written a function which computes the $L_{1 \mid 2}$ matrix which is valid for any model with two crossed fixed factors facAand facB as long as the vcov()function returns the estimated asymptotic covariance matrix :
L12matrix <- function(fit, facA, facB, V=vcov(fit)){
  coefs <- names(coef(fit))
  facA.match <- str_detect(coefs,facA)
  L1 <- NULL
  p <- length(facA.match)
  for(i in 1:p){
    if(facA.match[i]){
      l <- rep(0,p)
      l[i] <- 1
      L1 <- rbind(L1,l)
    }
  }
  rownames(L1) <- coefs[facA.match]
  facAB.match <- facA.match & str_detect(coefs,facB)
  L2 <- NULL
  for(i in 1:p){
    if(facAB.match[i]){
      l <- rep(0,p)
      l[i] <- 1
      L2 <- rbind(L2,l)
    }
  }
  rownames(L2) <- coefs[facAB.match]
  L12 <- t(car:::ConjComp(t(L2),t(L1), V))
return(L12)
}

And indeed, with the linearHypothesis() function we get the same results as the Anova() function:
> L12 <- L12matrix(fit,"Contour","Depth")
> linearHypothesis(fit,L12)
Linear hypothesis test

Hypothesis: ...

Model 1: restricted model
Model 2: pH ~ Contour * Depth

  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     36 5.6592
2     34 5.3003  2   0.35891 1.1511 0.3283

> L12 <- L12matrix(fit,"Depth","Contour")
> linearHypothesis(fit,L12)
Linear hypothesis test

Hypothesis: ....

Model 1: restricted model
Model 2: pH ~ Contour * Depth

  Res.Df     RSS Df Sum of Sq      F    Pr(>F)
1     37 18.3119
2     34  5.3003  3    13.012 27.822 2.847e-09 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
> Anova(fit)
Anova Table (Type II tests)

Response: pH
               Sum Sq Df F value    Pr(>F)
Contour        0.3589  2  1.1511    0.3283
Depth         13.0116  3 27.8220 2.847e-09 ***
Contour:Depth  0.5268  6  0.5633    0.7564
Residuals      5.3003 34
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

A GLS example
Consider the simulated data and the fitted gls model of my previous post:
groups <- c("A","B","C","D","E")
n.groups <- length(groups)
n.individuals <- rep(4,n.groups) # four individuals per group
id <- unlist(lapply(1:n.groups, function(i) paste0(groups[i],1:n.individuals[i]))) # individual id
n <- length(id)
set.seed(666) # diabolic seed
dat <- data.frame(id=id,
                  group=rep(groups, each=4),
                  x=round(rnorm(n,2),1),
                  y=round(rnorm(n,4),1),
                  z=round(rnorm(n,6),1))
ldat <- melt(dat, id=c("id","group")) # data in long format

##
glsfit <- gls(value ~ group*variable, data=ldat,
            correlation=corSymm(form= ~ 1 | id),
            weights=varIdent(form = ~1 | variable))

The Type II test of the group factor is performed as follows:
> L12 <- L12matrix(glsfit, "group", "variable")
> linearHypothesis(glsfit, L12)
Linear hypothesis test

Hypothesis: ...

Model 1: restricted model
Model 2: value ~ group * variable

  Df  Chisq Pr(>Chisq)
1
2  4 9.5172    0.04939 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Here the linearHypothesis()function has returned the Wald $\chi^2$-test statistics for $H_0$, and
the  Wald $F$-test statistics is simply obtained by dividing by the degrees of freedom:
> 9.5172/4
[1] 2.3793

Unfortunately the Anova() function from the car package does not work for gls fitted models.
But here the Type II Wald $F$-test statistic is equal to the Type I Wald $F$-test statistic because the design is balanced, and this statistic is returned by the anova() function:
> anova(glsfit)
Denom. DF: 45
               numDF   F-value p-value
(Intercept)        1 1401.9971  <.0001
group              4    2.3793  0.0658
variable           2   79.5687  <.0001
group:variable     8    1.4759  0.1929

If the design were not balanced then the Type II Wald $F$-test statistic can be obtained by inverting the order of the factors:
gfit.reverse <- update(gfit, model = value ~ variable*group)
anova(gfit.reverse)

Note that the L12matrix(glsfit, ...) command only uses the fitted model glsfit to get the parameters names and to get the asymptotic covariance matrix from the default value of the V argument.
Thus we would get the same results by typing :
L12 <- L12matrix(fit, "group", "variable", V=vcov(glsfit))

The SAS bug ?
Now, what's the problem with SAS ?
For a classical linear model such as fit, no problem: it gives the same results as the Anova() function.
But for a model with a repeated structure, such as glsfit, the
SAS output is obtained as follows with the R tools introduced above:
> # SAS result :
> lmfit <- lm(value ~ group*variable, data=ldat)
> L12 <- L12matrix(glsfit, "group", "variable", V=vcov(lmfit))
> linearHypothesis(glsfit, L12)
Linear hypothesis test

Hypothesis: ...

Model 1: restricted model
Model 2: value ~ group * variable

  Df  Chisq Pr(>Chisq)
1
2  4 7.9864    0.09208 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Divide 7.9864 by 4 and you find the SAS output :

In other words, SAS gives the Wald $F$-test statistic for $H_0:\colon\{L_{1\mid 2}\beta=0\}$ but by taking the
$L_{1\mid 2}$ matrix obtained with
the covariance matrix of the classical linear model obtained by ignoring the repeated structure.
Conclusion
Is it a "bug" with SAS, something wrong ? That depends whether the SAS developers have an interpretation of their Type II table but it seems to be a naïve generalization of the classical linear model to more general linear models, whereas J. Fox and G. Monette provide in their slides a well interpretable general approach to Type II hypotheses (is it an old or  a recent approach ? I'd like to know).
Comment: Continuation here: http://stats.stackexchange.com/questions/53045/monette-foxs-conditional-hypotheses/
Answer: About the difference between Type II and Type III with the car package:
In spite of the balanced design there is a difference because Type III tests depend on the specified contrasts. With orthogonal constrats there's no difference:
> mfit <- update(mfit, contrasts=list(group=contr.sum))
> Anova(mfit, idata=idata, idesign=~variable, type="III")

Type III Repeated Measures MANOVA Tests: Pillai test statistic
               Df test stat approx F num Df den Df    Pr(>F)
(Intercept)     1   0.98072   763.01      1     15 2.810e-14 ***
group           4   0.34744     2.00      4     15    0.1468
variable        1   0.91386    74.26      2     14 3.519e-08 ***
group:variable  4   0.51812     1.31      8     30    0.2758
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Finally with this example we could also suspect that SAS provides the Pillai statistic for factor group and the Wald statistic for factor variable. Yet I don't really know what's exactly going on... I will edit my posts in case of further understanding.


Add content type to a taxonomy - Drupal 7

Question: Hello everyone I have content type named contact where I have many fields like name, email, phone, country... and a taxonomy named organization with fields name, acronym and contact. What I try to do is when I create a new term of this taxonomy I have the possibility to add contact. Many thanks in advance
Comment: can you explain it clear so that it will be easy to answer
Answer: if I understand it right, you're trying to reference a "contact"-node...
You can use the module "entity reference" (https://www.drupal.org/project/entityreference) for that.
Comment: Many thanks Devaraj and Laebs. I think "entity reference" give me a begin solution. Now I can liaise the term to the node but the problem is when I open the page of the node I don't have the term of toxonomy. Should I add it ? How can I see the relation between this 2 elements ?hope I'm clear. Cheers
Comment: You have to add an additional field (type = entity reference) to the taxonomy vocabulary. To do this, go to: admin -> structure -> taxonomy -> edit vocabulary -> fields
Comment: many thanks laebs I will try it and come back to you


Saving elapsed time for each forked process in bash

Question: I am running six process in parallel, each takes different time to complete. My goal is to simply record the time each of the process takes to complete.
Below is an example:
  1 #!/bin/bash
  2
  3  { time sleep 30  > /dev/null 2>&1 ; } 2>> testing_time &
  4  PID1=$!
  5 { time sleep 100  > /dev/null 2>&1 ; } 2>> testing_time &
  6  PID2=$!
  7
  8  wait $PID2
  9  echo "PID2 end" >> testing_time
 10
 11  wait $PID1
 12  echo "PID1 end" >> testing_time

The output I get is:
real    0m30.032s
user    0m0.001s
sys     0m0.003s

real    1m40.025s
user    0m0.001s
sys     0m0.003s
PID2 end
PID1 end

The problem is, when I have many processes, I don't really know which time corresponds to which process. I am hoping to get some output which shows the elapsed time (only real time is fine too) of the process with some comments indicating which process the time corresponds to.
Expected output:
PID1 starts
real    0m30.032s
user    0m0.001s
sys     0m0.003s
PID1 end

PID2 starts
real    1m40.025s
user    0m0.001s
sys     0m0.003s
PID2 end
PID1 end

Comment: Why use 1 file then?  You could send command 1 to testing_time1, command 2 to testing_time2, ...  The fact you send it to background and output in the same file makes it impossible to guaranty sequence.
Comment: When I am running many processes, it is easier to manage only one file containing all the times. The time itself does not have to be sequential, meaning PID1 first and then PID2 etc. It can be PID2 first and then PID1 reported (in the order of actual completion), as long as I know which PID corresponds to which time.
Comment: You could output to many files and `cat` them together at the end of your script.  This way you keep it simple and get the desired output in 1 file.
Comment: FYI, putting `>> testing_time` everywhere is much less efficient than just opening the output file once at the top of your script and leaving that FD open.
Answer: One reasonable approach is to set the TIMEFORMAT variable to include your command.
time_silent_cmd() {
  local TIMEFORMAT cmd

  printf -v cmd '%q ' "$@"
  TIMEFORMAT="%R used for ${cmd//%/%%}"
  time { "$@" >/dev/null 2>&1 ; }
}

{
  time_silent_cmd sleep 0.5 &
  time_silent_cmd sleep 0.3 &
  time_silent_cmd printf '%s\n' "Sample with a % sign"
} 2>testing_time
wait

leaves the testing_time file containing contents akin to:
0.000 used for printf %s\\n Sample\ with\ a\ %\ sign
0.305 used for sleep 0.3
0.504 used for sleep 0.5

...on which point -- if you copy-and-paste that printf command to a prompt, the quoting generated by %q is such that it evaluates to its literal contents.
Comment: if the command run each time is same, but run in different folders, then how can I have the folder name in the "testing_time" file indicating the command took XY amount of time to operate in A folder, B folder etc?
Comment: Sounds like instead of `${cmd//%/%%}` you want to include `${PWD//%/%%}` in the TIMEFORMAT.


Icinga 2 vs Nagios 4 vs Shinken 2 : is there Performance Benchmarking comparison available?

Question: I am not able to find out any link that displays the benchmarking report for the cores.
We have to monitor 50,000 hosts and 30 services per host

per minute host check : icmp
per 5 minute service checks : telnet and snmp

We have been using Nagios 3 along with OMD (Check MK) to monitor network of half the size, that is 25,000 hosts and 30 services per host. But now the monitoring infrastructure is coming short on performance and with the new releases in place describing a better scalability we would like to switch on a core that is more scalable in terms of check execution, per cpu core per server.
Comment: The search term that will help find this information is "dimensioning", fwiw
Answer: Benchmarks for monitoring environments always differ and numbers won't apply to your requirements. Although there's a rich community for each project on its own where you may ask others to share their experience.
Comparison charts look fancy but I doubt they will help with anything. Which is why we don't have them on icinga.org anymore - if you want to testdrive certain systems, fire up vagrant or docker images and assign them resources.
https://github.com/Icinga/icinga-vagrant
Reading your numbers in relation with Icinga 2 - I would build a distributed cluster system sharing the load inside a HA zone. Start with a lower number and then go further.
Icinga 2's configuration dsl lets you generate configuration using apply rules, loops, functions, etc so bringing up a test config is only a matter of minutes. Take the web2 dev vagrant box for example: https://github.com/Icinga/icingaweb2/blob/master/.puppet/profiles/icinga2_dev/files/conf.d/test-config.conf#L48
Answer: I think you should try Centreon also because it's a really good product. If you use Centreon Engine and Broker you can go up to 100 000 hosts and 500 000 services without any problem !
When I moved from Nagios to Centreon Engine we had about 28 of load (for 500 hosts and 1200 services) on our monitoring servers, now we are at 2 with Centreon Engine on the same server !
If you have any questions you can contact me.


Is there a meaningful ITrigger.ToString() method in Quartz?

Question: What I mean is if I have a trigger like:
ITrigger trigger = TriggerBuilder.Create()
   .WithSimpleSchedule(x => x
        .WithIntervalInSeconds(40)
        .RepeatForever())
   .Build();

I want to be able to get meaningful (maybe even localized) description of trigger's schedule. For example, I want to see "Repeat every 40 seconds." message when I call trigger.ToMeaningfulString(). I couldn't google something like that but I'm almost sure I'm not the first one who got this idea and, maybe, there's already an implementation. This will be useful in case if you want to develop some kind of jobs manager which will display trigger or job status/details. This is not a top priority task so I wouldn't want to invest a lot of effort in it if there's no existed implementation.
Answer: Not sure if it exists for simple schedules. If you build your schedule using cron expressions, however, you could use cron expression descriptor, which takes a cron expression and does exactly what you ask for:
var cronExpression = "40 * * * * ?";
ITrigger trigger = TriggerBuilder.Create()
        .WithCronSchedule(cronExpression)
        .Build();

var humanReadableString = ExpressionDescriptor.GetDescription(cronExpression);
// humanReadableString = "Every 40 seconds"

Available as NuGet package.
Comment: Thanks, I'll look into this later, but I think this could be exactly what I was looking for.


Does $\sum_{k=0}^{\infty} \frac{k^n}{k!}$ have a closed form solution?

Question:
Does $\sum_{k=0}^{\infty} \frac{k^n}{k!}$ have a closed form solution?

I am working on a greater question involving this infinite series and I have been struggling with thinking of ways to evaluate it. Currently, I am thinking an inductive argument may work, but even that seems futile.
Comment: The sum in your title is not an infinite series as specified in your question.  Perhaps you want the following? - $$\sum_{k=0}^\infty\frac{k^n}{k!}=eB_n\ ,$$where $B_n$ is the $n$th Bell number: [details](https://en.wikipedia.org/wiki/Dobi%C5%84ski%27s_formula)
Comment: This is indeed what I meant. I suppose I'm looking for more insight on the proof than the Wikipedia article provides
Answer: For fixed $n\in \mathbb{N}$, notice that each term in the sum has  $n$ copies of $k$ in the numerator where $n\geq k\geq 0$, but only $k$ numbers in the factorial product $k!=k\times (k-1)\times \ldots \times 1$ in the denominator where each of these positive numbers is $\leq k$. In other words, each term in the sum is $\geq 1$ (you can verify this by induction), so $$\sum_{k=1}^n \frac{k^n}{k!}\geq n,$$ and so the limit goes to $\infty$ as $n\to \infty$.
Comment: You explained it so simply, thank you. This is still a useful result, since the greater problem involves uncountably many things. I think this may push me further in one direction. Thanks again!
Comment: @Solve_for_T Glad I could help.
Comment: @BrevanEllefsen The question has been edited. The question originally had the sum running up to $n$, so $k\leq n$ was always true.
Comment: Ah, OK. My bad, and bad on the OP to change the question. Didn't check edit history.
Comment: That is my fault -- sorry! It is late and I should go to bed...
Comment: @Solve_for_T No worries.


Measure Theory - Building Intuition

Question: I'm new in this world, so I apologize if this question is missing information.
Let $\Omega = \{\text{all students in the classroom}\}$.
Let $X(\omega) = 0$, if $\omega$ male student, and $X(\omega) = 1$, if $\omega$ is a female student.
Then $\sigma(X)$ consists of 4 subsets. Which ones?
Comment: $\emptyset, X^{-1}(0), X^{-1}(1)$, and $\Omega$. The idea is that you start taking inverse image of all measurable sets of the image $\{0,1\}$. So, inverse image of $\emptyset\subset\{0,1\}$ is the $\emptyset\subset\Omega$. The inverse image of $\{0\}$ and $\{1\}$ are $X^{-1}(0)$ and $X^{-1}(1)$. Finally, the inverse image of $\{0,1\}$ is $X^{-1}(\{0,1\})=\Omega$.
Comment: In other words, it is: $\sigma(X) = \{ \text{male student, female student}, \emptyset, \Omega\}$ ?
Comment: Yes. That is the $\sigma$-algebra.
Comment: It is the $\sigma$-algebra generated by $X$, right?
Comment: Yes, the smallest $\sigma$-algebra that makes $X$ measurable.
Comment: Thank you! That was helpful!
Comment: Is the inverse of $\emptyset$ the $\emptyset$?
Comment: $X^{-1}(\varnothing)=\varnothing$ if that's what mean. Because there is no $\omega\in\Omega$ with $X(\omega)\in\varnothing$.
Comment: $X^{-1}(A)=\{x\in\Omega:\ X(x)\in A\}$. If $A$ is empty, then no $x\in\Omega$ satisfies $X(x)\in A$. Therefore $X^{-1}(\emptyset)=\emptyset$
Comment: Yes, that is what i mean! Thank you!
Comment: @gariban I think you should distinguish the $\emptyset^{\mathbb R}$ and $\emptyset^{\Omega}$. After all, complementation is relative: $(\emptyset^{\mathbb R})^c = \mathbb R$ but $(\emptyset^{\Omega})^c=\Omega$
Answer: If $X$ is a random variable then in general: $$\sigma(X)=\{\{X\in B\}\mid B\in\mathcal B\}$$
where $\mathcal B$ denotes the Borel $\sigma$-algebra on $\mathbb R$ and: $$\{X\in B\}:=X^{-1}(B)=\{\omega\in\Omega\mid X(\omega)\in B\}$$
In your case $X$ can only take values in $\{0,1\}$ and consequently the set $\{X\in B\}$ can only take $4$ forms:

$\varnothing$ if $B\cap\{0,1\}=\varnothing$
$\{\text{male students}\}$ if $B\cap\{0,1\}=\{0\}$
$\{\text{female students}\}$ if $B\cap\{0,1\}=\{1\}$
$\Omega=\{\text{all students}\}$ if $B\cap\{0,1\}=\{0,1\}$

So we end up with:$$\sigma(X)=\{\varnothing,\{\text{male students}\},\{\text{female students}\},\Omega\}$$
Answer: The $\sigma$-algebra generated by a random variable $X$ in the probability space $(\Omega, \mathscr F, \mathbb P)$ is all the preimages of $X$ for any Borel set. What does this mean?
For any Borel set $B$ (pretend I said 'any subset' for the sake of understanding but then accept that any subset will technically not work) in the real numbers, there exists a subset of the sample space $\Omega$ that is also in $\mathscr F$, i.e., its $\mathbb P$robability can be computed s.t. if the subset is inputted into $X$, $X$ will give an output that is all the values in the Range of $X$ that is in $B$.
This is given by
$$\sigma(X)=\{ X^{-1}(B) | B\in\mathscr B\}$$
where $X^{-1}(B)$ is the said subset for a given $B$. This is given by
$$X^{-1}(B)=\{\omega\in\Omega | X(\omega)\in B\}.$$
Note that
$$X^{-1}(B) \in \mathscr F$$
$$X^{-1}(B) \subseteq \Omega$$
$$B \in \mathscr B$$
$$B \subseteq \mathbb R$$
Here, $X$ is an indicator/characteristic function for females: If you input a female student, $X$ gives you 1 and 0 otherwise (I assume all students are male xor female). This is given by
$$X(\text{(any) female student in the classroom}) = 1$$
$$X(\text{(any) male student in the classroom}) = 0$$
On the other hand, an output of 1 lets you know you've got female, and 0 lets you know you've got male. This is given by
$$X^{-1}(\{1\}) = \{\omega \in \Omega | X(\omega) = 1 \} = \{\omega \in \Omega | \omega \text{ is a female student in the classroom} \} = \{\text{(all) female students in the classroom}\}$$
$$X^{-1}(\{0\}) = \{\omega \in \Omega | X(\omega) = 0 \} = \{\omega \in \Omega | \omega \text{ is a male student in the classroom} \} = \{\text{(all) male students in the classroom}\}$$
However, we need not be limited by singletons. We can use intervals, unions of disjoint intervals, etc. For example,
$$X^{-1}((0.5,1.5] \cup {2} = \{\omega \in \Omega | X(\omega) \in (0.5,1.5]  \cup {2} \} = \{\omega \in \Omega | X(\omega) = 1 \} = \{\omega \in \Omega | \omega \text{ is a female student in the classroom} \} = \{\text{female students in the classroom}\}$$
$X$ doesn't care about the numbers in $(0.5,1.5] \cup {2}$ besides $1$ because its range consists only $0$ and $1$, i.e. $Range(X) = \{0,1\}$. Other examples:
$$X^{-1}((\pi,2,000]) = \{\omega \in \Omega | X(\omega) \in (\pi,2,000] \} = \{\omega \in \Omega | X(\omega) \in \emptyset^{\mathbb R} \}$$
$$= \{\omega \in \Omega | \omega \text{ is neither male nor female student in the classroom} \} = \emptyset^{\Omega}$$
$$X^{-1}([0,10]) = \{\omega \in \Omega | X(\omega) \in [0,10] \} = \{\omega \in \Omega | X(\omega) \in \{0,1\} \}$$
$$= \{\omega \in \Omega | \omega \text{ is a male or female student in the classroom} \} = \Omega$$
$$X^{-1}([-1,1)) = \{\omega \in \Omega | X(\omega) \in [-1,1) \} = \{\omega \in \Omega | \omega \text{ is a male student in the classroom} \} = \{\text{male students in the classroom}\}$$
Now, we can use any Borel set $B$. The important thing is whether or not $B$ has any 0's or 1's. Here's what to do:
$$X^{-1}(B) = \{\omega \in \Omega | X(\omega) \in B \} = \{\omega \in \Omega | \omega \text{'s that satisfy the condition} \} = \text{subset of} \ \Omega \ \text{that is in} \ \mathscr F \}$$
In your case I guess $\Omega$ would be finite, and all its subsets are in $\mathscr F$. Anyway, 4 cases

$B$ has $1$ but not $0$

$$X^{-1}(B) = \{\omega \in \Omega | X(\omega) \in B \} = \{\omega \in \Omega | X(\omega) = 1 \} = \{\omega \in \Omega | \omega \text{ is a female student in the classroom} \} = \{\text{female students in the classroom}\}$$

$B$ has neither $1$ nor $0$

$$X^{-1}(B) = \{\omega \in \Omega | X(\omega) \in B \} = \{\omega \in \Omega | X(\omega) \in \emptyset^{\mathbb R} \}$$
$$= \{\omega \in \Omega | \omega \text{ is neither male nor female student  in the classroom} \} = \emptyset^{\Omega}$$

$B$ has both $1$ and $0$

$$X^{-1}(B) = \{\omega \in \Omega | X(\omega) \in B \} = \{\omega \in \Omega | X(\omega) \in \{0,1\} \}$$
$$= \{\omega \in \Omega | \omega \text{ is a male or female student in the classroom} \} = \Omega$$

$B$ has $0$ but not $1$

$$X^{-1}(B) = \{\omega \in \Omega | X(\omega) \in B \} = \{\omega \in \Omega | \omega \text{ is a male student in the classroom} \} = \{\text{male students in the classroom}\}$$
Thus,
$X^{-1}(B) = $

$\{\text{female students in the classroom}\}$ if $B$ has $1$ but not $0$
$\emptyset^{\Omega}$ if $B$ has neither $1$ nor $0$
$\Omega$ if $B$ has both $1$ and $0$
$\{\text{male students in the classroom}\}$ if $B$ has $0$ but not $1$

Since $B$ cannot have other forms according to whether or not $B$ has $0$ or $1$, the cases are exhaustive. Thus, we know that $X^{-1}(B)$ cannot take any other forms.
Therefore:
$$\sigma(X)=\{ X^{-1}(B) | B\in\mathscr B\} = \{\emptyset^{\Omega},\{\text{male students in the classroom}\},\{\text{female students in the classroom}\},\Omega\}$$
Comment: Nice answer. Thank you.
Comment: What does $\emptyset^\mathbb{R}$ and $\emptyset^\Omega$ mean?
Comment: @gariban They belong to different sets. See my [comment here](https://math.stackexchange.com/questions/2701637/measure-theory-building-intuition/2727489#comment5629552_2701637). $\emptyset^{\mathbb R}$ is in $\mathscr B(\mathbb R)$, so $(\emptyset^{\mathbb R})^c = \mathbb R \ \text{and not} \ \Omega$.

Measure Theory - Building Intuition


Cone projected tangent angle

Question: A cone has a slope of 45 degrees.
The cone is projected on a plane that is inclined to the axis of the cone by x degrees.
If x was 0, the projection would be 2 lines converging at 90(45 + 45) degrees to each other.
cone projection parallel to cone's axis
If x was 90 degrees, the projection would cover the infinite plane in all directions.
cone projection perpendicular to cone's axis
In fact, if x is anything greater than the cone's slope(45 degrees), the projection will completely cover the plane.
cone projection at roughly 80 degrees between projected plane and cone's axis
If x was 45 degrees, the projection would be a straight line because one side of the cone would be perfectly perpendicular to the projected plane.
cone projection with 45 degrees
If x is between 0 and 45, the projection will be 2 converging lines at a point representing the vertex of the cone.
cone projection around 20 degrees
Question 1:
If the cone is projected at an angle of x between 0 and 45 degrees, what expression represents the angle between these converging lines?
illustration of projected tangent lines from the cone and angle in question
Question 2:
If the cone's slope was represented by y degrees instead of being a constant 45, what expression would represent the angle between the converging lines for x between 0 and (90 - y) degrees?
Comment: Welcome to mse. What have you tried? Where are you stuck?
Comment: Andrei, I created a little web page to visualize the relationship between the angle x and the tangent projection angle and approximate the answers inefficiently. I used that to create the illustrations in my question.

An answer to the following question should help:
Given that a cone was cut at angle x, what would be the ratio of radii in the elliptical conic section? A cylindrical section should have ratio abs(cos x) but a cone is more complex.

If I could get properties of an elliptical conic section at any distance away from the cone's vertex, would help.
Answer: Let $\alpha$ be the semi-aperture of the cone and $\theta$ (named $x$ in the question) the angle its axis forms with a given plane (see figure below). The projection of the cone onto the plane, for some values of $\theta$, is an angle whose sides are tangent to the projections of all the circular sections of the cone on the plane (twice angle $\delta$ in the figure).
If $ABCD$ is one such circular section of radius $h\tan\alpha$, its projection $A'B'C'D'$ is an ellipse with semi-axes $a=O'C'=OC=h\tan\alpha$ and
$b=O'B'=OB\sin\theta=h\tan\alpha\sin\theta$, while the distance from (the projection of) the vertex to the centre of the ellipse is $y_0=VO'=h\cos\theta$.
It is then a straightforward exercise to find the slope of the tangents:
$$
\tan\delta={a\over\sqrt{y_0^2-b^2}}=
{\tan\alpha\over\sqrt{\cos^2\theta-\tan^2\alpha\sin^2\theta}}.
$$
This formula works as long as the expression inside the square root is not negative, that is for $0\le\theta\le90{°}\!-\alpha$. For $\theta=90°\!-\alpha$ a generatrix of the cone is perpendicular to the plane and
$\delta=90°$.
For $\alpha=45°$, in particular:
$$
\tan\delta
={1\over\sqrt{\cos^2\theta-\sin^2\theta}}
={1\over\sqrt{\cos2\theta}}.
$$
Comment: Thanks a lot!  That worked very well.

I tested that with my interactive visualization and the angles line up perfectly through various cone slopes and various projection angles.
Comment: Thank you for this solution @intelligenti pauca. I'm wondering whether it is possible to generalise a solution including the cases, where the angle between the cone axis and the projected plane is equal or greater than 90 degrees. Any thoughts ? thanks a lot !
Comment: @mysterium If $\theta=90°$ then the projection is the whole plane. The case $\theta>90°$ doesn't make sense: $\theta$ is the angle between the axis of the cone and the plane (i.e. with the projection of that axis on the plane) and cannot be obtuse.
Comment: Thank you for the reply @Intelligenti pauca. However, if one can imagine an ice-cream cone held upright with an extended arm, let's say at a geometry similar to the statue of liberty, and seen by their eyes, wouldn't the plane containing the eyes and the ice-cream cone axis (vertical in this case) make an obtuse angle ?  in addition, if one solves for tan  the above equation (going from a given projected half angle  to the original half angle  ), it seems that  can be defined for angles >90°. Or I'm mistaken somewhere ?
Comment: @mysterium In that case angle $\theta$ is the supplementary of your angle. Formally, you could use your angle as well, because $\cos(\pi-\theta)=-\cos\theta$ and the formula for $\tan\delta$ contains only $\cos^2\theta$.
Comment: ah, okay, many thanks again for the clarification @Intelligentipauca !
Answer: When axis is tilted by $\beta$ and considering reduction of projected length in the denominator (simple figure, not drawn) we have new vertical angle for $90^{\circ}$ vertex angle:
$$2 \tan^{-1}\sec \beta$$
For the general case (using $\alpha $ in place of $x$). Let the tan of vertical angle at cone ( base radius $r$, height $h$ ) vertex be $T$.
Considering projections comparing tan of semi-vertical angles
$$ \tan \alpha=\dfrac{r}{h}$$
when the axis of symmetry is tilted by $ \beta$
$$ \tan \alpha_1=\dfrac{r \cos \beta}{h}$$
Dividing
$$ \sec \beta= \dfrac{\tan \alpha_1}{\tan \alpha}$$
Expressing half angle tangent $t$ in terms of the full angle tangent $T$ which is the projected angle between generators
$$\dfrac{{\dfrac{\sqrt{1+T1^2}-1}{T1}}}{{\dfrac{\sqrt{1+T^2}-1}{T}}} = \sec \beta, $$
an implicit equation between $ \tan^{-1} T_1 $ and $\tan^{-1} T. $

Cone projected tangent angle