Compiling Executable with dask or joblib multiprocessing with cython results in errors

Question: I'm converting some serial processed python jobs to multiprocessing with dask or joblib. Sadly I need to work on windows.
When running from within IPython or from command line invoking the py-file with python everything is running fine.
When compiling an executable with cython, it is no longer running fine: Step by step more and more processes (unlimited and bigger than the number of requested processes) get startet and block my system.
It somehow feels like Multiprocessing Bomb - but of course I used if __name__=="__main__:" for having the control block - approved by fine running from python call at the command line.
My cython call is cython --embed --verbose --annotate THECODE.PY and I'm compiling with gcc -time -municode -DMS_WIN64 -mthreads -Wall -O -I"PATH_TO_\include" -L"PATH_TO_\libs"  THECODE.c -lpython36 -o THECODE resulting in a windows executable THECODE.exe.
With other (single processing) code that is running fine.
The problem seems to be the same for dask and joblib (what might mean, that dask works like or is based on joblib).
Any suggestions?
For those interested in a mcve: Just taking the first code from Multiprocessing Bomb and compiling it with my cython commands above will result in an executable blowing your system. (I just tried :-) )
I just found something interesting by adding one line to the code sample for showing the __name__:
import multiprocessing

def worker():
    """worker function"""
    print('Worker')
    return

print("-->" + __name__ + "<--")
if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker)
        jobs.append(p)
        p.start()

When running that piece of code with python it shows
__main__
__mp_main__
__mp_main__
__mp_main__
__mp_main__
__mp_main__

(other output supressed). Explaining that the if decision works.
When running the executable after cython and compilation is shows
__main__
__main__
__main__
__main__
__main__
__main__

and more and more. Thus the workers call to the module are no longer masqueraded like an import and thus each workers tries to start five new ones in a recursive manner.
Comment: I think this might be worth reporting as a bug (https://github.com/cython/cython/issues).
Comment: @DavidW Good option - I'll do that exactly now. THX
Answer: When starting a new python-process multiprocessing-module uses spawn-method on Windows (this behavior can be also triggered on Linux by using mp.set_start_method('spawn').
Command-line arguments are passed to the interpreter in the new process, so the communication with the parent process can be established, for example:
 python -c "from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=5, pipe_handle=11)" --multiprocessing-fork

The problem with embeded cython modules (or with frozen (i.e. created with cx_Freeze, py2exe and similar) modules in general), that passing command line arguments to them corresponds more to
python my_script.py <arguments>

i.e. the command line aren't automatically processed by the interpeter, but needs to be handled in the script.
multiprocessing provides a function called multiprocessing.freeze_support(), which handles the command line arguments correctly and which can be used as shown in Bastian's answer:
if __name__ == '__main__':
    # needed for Cython, as it doesn't set `frozen`-attribute
    setattr(sys, 'frozen', True)
    # parse command line options and execute it if needed
    multiprocessing.freeze_support()

This solution works however only for Windows, as can be seen in the code:
def freeze_support(self):
    '''Check whether this is a fake forked process in a frozen executable.
    If so then run code specified by commandline and exit.
    '''
    if sys.platform == 'win32' and getattr(sys, 'frozen', False):
        from .spawn import freeze_support
        freeze_support()

There is a bug-report: multiprocessing freeze_support needed outside win32 which might/might not be fixed soon.
As explained in the above bug-report, it is not enough to set frozen attribute to True and to call freeze_support directly from the multiprocessing.spawn because than the semaphore tracker isn't handled correctly.
There are two options I see: either to patch your installation with a yet unreleased patch from the above bug report or to use the do-it-yourself approach presented bellow.

Here are an earlier version of this answer which is more "experimental" but offers more insights/details and proposes a solution in a somewhat Do-It-Yourself-style.
I'm on linux, so I use mp.set_start_method('spawn') to simulate the behavior of windows.
What happens in the spawn-mode? Let's add some sleeps, so we can investigate the processes:
#bomb.py
import multiprocessing as mp
import sys
import time

def worker():
    time.sleep(50)
    print('Worker')
    return

if __name__ == '__main__':
        print("Starting...")
        time.sleep(20)
        mp.set_start_method('spawn') ## use spawn!
        jobs = []
        for i in range(5):
            p = mp.Process(target=worker)
            jobs.append(p)
            p.start()

By using pgrep python we can see that at first there is only one-python process, then 7(!) different pids. We can see the command-line arguments via cat /proc/<pid>/cmdline. 5 of the new processes have command line
-c "from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=5, pipe_handle=11)" --multiprocessing-fork

and one:
-c "from multiprocessing.semaphore_tracker import main;main(4)"

That means, the parent process starts 6 new python interpreter instances and every newly started interpreter executes a code sent from the parent via the command line options, the information is shared via pipes. One of these 6 python-instances is a tracker, which observes the whole thing.
Ok, what happens if cythonized+embeded? The same as with the normal python, the only difference is that the bomb-executable is started instead of python. But differently as the python-interpreter, it doesn't execute/isn't aware of the command line arguments, so the main function runs over and over and over again.
There is an easy fix: let the bomb-exe to start the python interpreter
 ...
 if __name__ == '__main__':
    mp.set_executable(<PATH TO PYTHON>)
 ....

Now the bomb is no longer a multiprocessing bomb!
However, the goal is probably not to have a python-interpreter around, so we need to make our program aware of possible command lines:
import re
......
if __name__ == '__main__':
    if len(sys.argv)==3:  # should start in semaphore_tracker mode
        nr=list(map(int, re.findall(r'\d+',sys.argv[2])))
        sys.argv[1]='--multiprocessing-fork'   # this canary is needed for multiprocessing module to work
        from multiprocessing.semaphore_tracker import main;main(nr[0])

    elif len(sys.argv)>3: # should start in slave mode
        fd, pipe=map(int, re.findall(r'\d+',sys.argv[2]))
        print("I'm a slave!, fd=%d, pipe=%d"%(fd,pipe))
        sys.argv[1]='--multiprocessing-fork'   # this canary is needed for multiprocessing module to work
        from multiprocessing.spawn import spawn_main;
        spawn_main(tracker_fd=fd, pipe_handle=pipe)

    else: #main mode
        print("Starting...")
        mp.set_start_method('spawn')
        jobs = []
        for i in range(5):
            p = mp.Process(target=worker)
            jobs.append(p)
            p.start()

Now, our bomb doesn't need a stand-alone python-interpreter and stops after the workers are done. Please note the following:

The way it is decide, in which mode bomb should be started is not very error-safe, but I hope you get the gist
--multiprocessing-fork is just a canary, it doesn't do anything it only must be there, see here.

NB: The changed code can be also used with python, because after executing "from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=5, pipe_handle=11)" --multiprocessing-fork python changes the sys.argv so the code no longer sees the original command line and len(sys.argv) is 1.
Comment: Found a [solution](https://stackoverflow.com/questions/53497078/joblib-parallel-cython-hanging-forever) that works for me: I had to use `with multiprocessing.get_context("spawn").Pool() as pool: ...` in my app to force Loky-backend of the dependency to use `n_jobs=1`. No mp-bombs in my frozen app anymore :thumbsup:
Answer: I think based on the detail from the submitted bug report, I can offer the maybe most elegant solution over here
if __name__ == '__main__':
    if sys.argv[0][-4:] == '.exe':
        setattr(sys, 'frozen', True)
    multiprocessing.freeze_support()
    YOURMAINROUTINE()

The freeze_support()-call is needed on windows - see python multiprocessing documentation.
If running within python only with that line it is already fine.
But somehow cython is obviously not aware of some of those things (the docs tell it is tested with py2exe, PyInstaller and cx_Freeze). It could be alleviated by the setattr-call, which only may be used when compiling, thus the decision by file extension.
Comment: Tried this and, unfortunately, I still get multiple spawns of my frozen command line app using multiprocessing.Pool on Python 3.7 with joblib dependency. I verified, and `setattr(sys, 'frozen', True)` is executed (as a bomb, however).
Comment: This works only for windows see https://bugs.python.org/issue32146
Answer: Inspired by the answer (or the given ideas there) from ead, I found a very simple solution - or lets better call it workaround.
For me just changing the if clause to
if __name__ == '__main__':
    if len(sys.argv) == 1:
        main()
    else:
        sys.argv[1] = sys.argv[3]
        exec(sys.argv[2])

did it.
The reason why that works is (in my case):
When calling the original .py-file the worker's __name__ is set to __mp_main__ (but all processes are just the plain .py-file).
When running the (cython) compiled version the worker's name is not usable, but the workers get called different and thus we can identify them by more that one argument in argv. In my case worker's argv reads
['MYPROGRAMM.exe',
 '-c',
 'from multiprocessing.spawn import spawn_main;
       spawn_main(parent_pid=9316, pipe_handle =392)',
 '--multiprocessing-fork']

Thus in argv[2] the code for activation of the workers is found and gets executed with the upper commands.
Of course if you need arguments for your compiled file, you need a bigger effort, maybe parsing for the parent_pid in the call. But in my case, that would simply be overdone.
Comment: What is about my solution, which mimics the the right behavior of the python-interpreter? You will need to adjust it for windows (`parent_pid` instead of `fd` and `pipe_handle` instead of `pipe`), but I think that should set up the communication correctly
Comment: Your solution will only work for your minimal/similar examples. As soon as data must be shared between the master and the slaves your work around will break down...
Comment: @ead oh sad - you are right. I just tested. Do you know, why? What can I do further? (Okay, it is no longer a process-bomp, but non functional. :-( )
Comment: @ead your solution works - I just needed to understand it. I'm fine with that - but I think it could be done (working for me also with big worker functions) in a more readable (or understandable) fashion - I'll update my answer.
Answer: Since the solutions proposed didn't work for me, I am providing an additional answer with a workaround.
My frozen app also resulted in a multiprocessing bomb. I could solve it by

using Thread-based parallelism, instead of process-based multiprocessing and
within Joblib Parallel execution, using Parallel(n_jobs=4, prefer="threads"), as suggested by this answer (instead of the default prefer="multiprocessing")

I couldn't get multiprocessing.Pool to work in the frozen app (neither with prefer="threads" nor with prefer="multiprocessing"), but one can switch to thread-based multiprocessing by see docs:
# a dependency with joblib
from dep_with_joblib import BigJob
# multiprocessing wrapper for threaded.Thread
from multiprocessing.dummy import Pool as ThreadPool
# instead of
# from multiprocessing import Pool

# thread based parallelism,
# works if `Parallel(n_jobs=4, prefer="threads")` is used
# in joblib (e.g. inside big_job())
POOL = ThreadPool(processes=1)

# as far as I can tell,
# the following Process based Parallelism
# does _not_ work with frozen app/joblib atm
# POOL = Pool(processes=1)

class MainClass():
    def __init__(self):
        """Init ClusterGen"""
        return

    @staticmethod
    def run_big_job(big_job, data):
        """Run big_job on parallel thread"""
        big_job()
        return big_job

   def big_job_exec(self):
        """Big job execution"""

        bigjob = BigJob()
        big_job_input_data = ...
        # Start big_job on different thread
        async_result = POOL.apply_async(
            MainClass.run_big_job, (bigjob, big_job_input_data))
        # get results from clusterer
        bigjob_results = async_result.get()

More explicit example with Queue and threading.Thread:
import threading
import queue
# a dependency with joblib
from dep_with_joblib import BigJob

job_queue = queue.Queue()

def store_in_queue(f):
    def wrapper(*args):
        job_queue.put(f(*args))
    return wrapper

class MainClass():
    def __init__(self):
        """Init ClusterGen"""
        return

    @staticmethod
    @store_in_queue
    def run_big_job(big_job, data):
        """Run big_job on parallel thread"""
        big_job()
        return big_job

   def big_job_exec(self):
        """Big job execution"""

        bigjob = BigJob()
        big_job_input_data = ...
        # Start big_job on different thread
        t = threading.Thread(
            target=MainClass.run_big_job,
            args=(bigjob, big_job_input_data),
            group=None,
            name="example-bigjob",
        )
        t.start()
        # get results from big_job
        bigjob_results = job_queue.get()

in both of the examples above, bigjob() is run async on a different thread. The examples can be easily modified with multiple threads.
Why async? In my case BigJob() is a module from a dependency that uses Joblib.Parallel to improve speed, which wouldn't work when my app was frozen + I needed bigjob() to run async to prevent my GUI from crashing.


What is the relation between Kelvin-Helmholtz timescale and free-fall timescale?

Question: What is the actual difference between these two timescales? Both of these timescales define the time for collapse when there is only gravity. Also, what does it mean by saying Kelvin-Helmholtz timescale is smaller than free-fall timescale for high-mass protstars? When are these timescales important in star formation/evolution ?
Answer: The two timescales are different because they are relative to two different physical situations.
When you talk about the free-fall timescale, you have in mind a pressure-less cloud of dust/gas distributed in a more or less spherical shape with average density $\rho$ that is collapsing under its own gravitational pull. From dimensional analysis it can be concluded that the time it takes for the distribution to collapse completely is $t_{ff} \approx {1 \over \sqrt{G\rho}}$. Actually it is more like $t_{ff} \approx {1 \over 2}{1 \over \sqrt{G\rho}} \approx 35 \min \left(\rho \over g\ cm^{-3} \right)^{-{1 \over 2}}$
Kelvin-Helmholtz timescale comes up in a completely different situation. Imagine again a more or less spherical cloud. But this time it is supported by pressure, it is not collapsing in free fall. And the cloud is also hot, it has a certain temperature and radiates away energy with a luminosity $L$. If the cloud has no additional sources of energy (e.g. nuclear burning) then the radiated energy must be taken from the internal energy reservoir $E_i$. At this rate, it will take a time $t_{KH} \approx {E_i \over L}$ to drain the gas from all its energy. While loosing energy, the cloud shrinks, therefore after a time $t_{KH}$ it will be completely collapsed.
When talking about a real star, these two scenarios are just an idealization, because a real star will usually (i) be supported by pressure, (ii) produce energy through nuclear reactions. Yet, there are situations in which these two conditions do not apply and the two timescales may become important.
Example 1: during the formation of a star, a molecular cloud slowly collapses into itself. There is no nuclear burning yet, but the proto-star is supported by pressure, therefore the collapse will happen on a Kelvin-Helmholtz timescale.
Example 2: a massive star may end its life as a core collapse supernova. At the onset of collapse the star feels the pressure faltering and starts to collapse nearly in free-fall.
When you read that a star has a $t_{kh} < t_{ff}$ it just means that the star is extremely luminous, so that, if the nuclear reactions happened to instantly halt and stop producing energy (magically or by other means), the star would need to collapse in a very short time, to keep up with the radiated luminosity. The collapse would be so quick that, if the material of the star were in free fall, the star would be slower to collapse.
Comment: @ user:42349 So what does tkh<tff means in the context of star formation? What does it mean when we say a high-mass protostar has tkh<tff ? Does it mean that it's accreting mass such that it's tff is bigger ? Can you please clarify ?
Comment: @Rian I was talking about adult stars, not protostars. I think it would be unusual for a protostar to have tkh<tff, because since the protostar is not yet doing nuclear fusion, it evolves on a Kelvin-Helmholtz timescale. So tkh<tff would mean that the star is actually contracting faster than in free fall
Comment: @ Prallax But for high mass protostars tkh<tff
Answer: The freefall timescale, $\tau_{\rm ff}$, is exactly that. It assumes any collapse is unopposed by any pressure.
In general any contraction will be opposed by internal pressure and energy. However, that internal energy can be radiated away, allowing contraction to proceed. The Kelvin-Helmholz timescale, $\tau_{\rm KH}$, is the time required for an object to radiate away its heat energy via its intrinsic luminosity.
If we think about a contracting/collapsing object then that evolution will take place at a rate given by the larger of these two timescales$^1$.
For example, in a forming protostar, there are no nuclear reactions, the star contracts and heats up. $\tau_{\rm KH}$ might be something like 10 Myr for a 1 solar mass pre-main sequence star, while $\tau_{\rm ff}$, which is approximately $(G \rho)^{-1/2}$ where $\rho$ is the density, might be something like 1 day. In such circumstances the contraction occurs on the K-H timescale.
But in other situations the order of these timescales can be reversed. That is a consequence of the very steep dependence of luminosity on mass and temperature. Thus in a high mass star the denominator (the luminosity) of $\tau_{\rm KH}$ increases much more than the numerator (the internal energy) and hence the K-H timescale becomes much shorter. Towards the end of its life, $\tau_{\rm KH}$ can become shorter than $\tau_{\rm ff}$ in the core of the star. (Not for the entire star).
What this means, is that once nuclear reactions cease, the contraction proceeds on a free fall timescale - leading to "core collapse".
$^1$ There is a third timescale to be considered - the time taken for the nuclear fuel of the star to be consumed in providing its current luminosity. This is known as the "nuclear timescale", $\tau_{\rm nuc}$.
So now you have three timescales to consider, but the rule is the same - evolution takes place at a rate governed by the longer timescale. In a main sequence star, $\tau_{\rm nuc}$ is much longer than either of the other two timescales and so the star evolves slowly (e.g. $\tau_{\rm nuc}$ is about 10 billion years for a newly formed 1 solar mass star). As the source of nuclear fuel runs out, or if the luminosity increases, then $\tau_{\rm nuc}$ gets much shorter and eventually one of the other timescales can take over and govern to evolution.
Comment: @ ProfRob So what does tkh<tff means in the context of star formation? What does it mean when we say a high-mass protostar has tkh<tff ? Does it mean that it's accreting mass such that its density decreases and tff gets bigger ? Can you please clarify ?
Comment: @Rian I would not have thought that $t_{\rm KH}$ is less than $t_{\rm ff}$ during star formation. Could you provide a reference for where you have seen this and I can investigate. The implication would simply be that the contraction is proceeding on the free fall timescale in those circumstances.
Comment: Here is the link https://www.eas-journal.org/articles/eas/abs/2011/06/eas1151012/eas1151012.html
Comment: @Rian, it's paywalled so I can't see it. You will need to quite the relevant passage in your question.
Comment: "In the case of massive stars, the free-fall time is longer than the Kelvin–Helmholtz timescale, so that the massive stars in formation reach thermal equilibrium before the accretion is completed. This is why the history of the accretion rates for massive stars is so critical. "


Would a non-conducting body ever acquire a uniform static charge throughout it's volume?

Question: I'm studying electromagnetism and optics in first year and solving a lot of problems involving conveniently symmetrical conducting and non-conducting bodies having various uniformly distributed charges and I can't help wondering: Is there in fact a natural circumstance where a non-conducting body would acquire a uniform electric charge throughout it's volume? How might that come about?
Answer: An electret is a insulating material that has been imbued with a semipermanent volume charge distribution by bombarding it with charged particles.  I don't know how uniform the charge distribution generally is, but if it were desireable, I'm sure a clever engineer could arrange for an approximately constant volume charge distribution.
Beams of charged particles in particle accelerators are probably more Gaussian than uniform in charge distribution, but they do represent volumes with continuous charge density.
Although such systems are somewhat rare, they are very useful for learning the basic techniques which can be applied or generalized to analyze more complicated, realistic scenarios (as I suspect you are aware).
Comment: Thanks! I have heard of an electret, in the context of an electret microphone, but had not made the connection. That's cool.


If quarks change colours constantly how are proton and neutron stable relatively?

Question: Say for example a proton is composed of Up, Up and Down (valance) quarks and suppose the Down quark absorbs a gluon, Down quark must change colour in order to conserve colour charges but so must the other 2 Up quarks? Unless at any given point in time the 3 valance quarks are always exchanging gluons to each other simultaneously otherwise the strong force between them would not work as proton right?
Comment: Don't forget that the gluons also carry color charge. See [this anim](https://upload.wikimedia.org/wikipedia/commons/3/35/Nuclear_Force_anim_smaller.gif) (which was created by [manishearth](https://physics.stackexchange.com/users/7433/manishearth)), but bear in mind that's a highly simplified diagram, so it doesn't show the interchange of gluons between 2 gluons or a quark & a gluon, and it doesn't show the sea quarks, just the valence quarks.
Comment: @PM2Ring: I read that the sea quarks is invisible to outside so the effect is negligible but the valence quarks must add up to white colour.
Answer: Yes, all the quarks & gluons in the proton are continuously exchanging gluons with their neighbouring quarks & gluons, and the total color charge must always add up to white, due to color confinement.
However, as the anim below (from Wikipedia, courtesy of manishearth) shows, the color changes of 2 valence quarks don't need to be simultaneous. Eg, a blue quark can emit a blue-antired gluon which changes that quark's color to red, so the color remains balanced at all times.

I should mention that color charge is actually more complicated than diagrams like the one above imply. Please see the Wikipedia article on gluons for details. Also see Why are there eight gluons and not nine? by James Bottomley and John Baez.


Is mysql_real_escape_string is really safe to use?

Question: OK, I have one question before I start coding MySQL in my school project. Is mysql_real_escape_string is really safe to use? I've heard that it's still not really safe to use..So are there any tweaks which makes SQL query secure? I've used mysql_real_escape_string before many times, but not I am building a website for my school, so first thing I've to check is security.
Comment: I think what you really want to know is: "Is using `mysql_real_escape_string()` the only thing I should worry about with regard to security?"  Answer:  no, but it's a good start.
Comment: mysql_real_escape_string is quite old fashionned, php version is now 5.3 and above: use PDO !
Comment: I'd like to hear why it's not safe to use.
Comment: This might help: [importance of link identifier in mysql_real_escape_string()](http://stackoverflow.com/questions/1977886/importance-of-link-identifier-in-mysql-real-escape-string)
Comment: @tandu - no one said it's not safe to use, there are just better methods out there as hornetbzz said.
Answer: UPDATE: The answer below was to the best of my knowledge correct at the time of writing.  The fact is mysql_real_escape_string is not safe and never really was.  You should always use prepared statements instead.
As mysql_* has been removed completely as of PHP 7 the situation has become moot.  I've left my original answer for historical purposes below.

mysql_real_escape_string is safe to use if used properly (ie, everywhere you're inserting PHP variables into your queries), but as has been pointed out in the comments it's not the only thing you need to worry about. For example, HTML markup could be inserted into your DB and used for Cross Site Scripting attacks.
You might also want to consider prepared statements as an alternative to mysql_real_escape_string, as they will automatically escape input for you so there's no chance of accidentally forgetting to escape a parameter.
Comment: it's critical to ensure you use mysql_real_escape_string() and NOT the bad old mysql_escape_string() if you must stick to using the old mysql legacy db library in php
Comment: @Ray i know your comment is old but if you need to use the old legacy MySQL library in PHP for some wierd reason atleast use MySQL's native [Prepared SQL Statement Syntax (MYSQL 5.5+)](https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html) to get protection against SQL injections the `EXECUTE stmt1 USING @` approach is safe  .. But keep in mind using `CONCAT()` how the manual uses it can be prone again to SQL injections.


What is called an intentional thought?

Question: I'm a 16 old student and I'm getting weird and intrusive thoughts which are unwanted and unintentional. I got those thoughts about buddha which can be greater insult for Him. But none of them are intended or wanted. Do this collect bad kamma? I've begun to fear those thoughts as I'm someone who fear sinning more than anyone. Please help me, what should I do?
Answer: There's no sin in Buddhism. Buddha will not be offended if you're thinking some silly thoughts. Don't hurt yourself with this fear. No need to perpetuate those silly thoughts either, it won't do you any good. Focus on what's beneficial. Such as study of Dharma.
Karma is not watching you, it does not revenge or punish. Karma is the future you build with your choices, day after day after day.
Answer: In the teaching of Dependent Origination, intentional thought arises at the 4th condition called 'nama-rupa'. The unintentional thoughts that arise prior to this, at the 2nd condition called 'sankhara', are the product of ignorance (SN 12.2) and/or past becoming (MN 9).
Therefore, weird and intrusive thoughts ('hindrances'; 'asava') which are unwanted and unintentional are the inevitable results of past becoming (MN 9) and kamma (AN 10.61). Not wanting such thoughts cannot stop their arising because the arising of such thoughts follows the law of Dependent Origination; which is a fixed absolute law.
Your questions on this forum appear to show a conflict between internal personal desires and the desires of your mother making you make promises to the Buddha. If you live with your parents, you must generally obey most of their directives when their directives are reasonable. Buddhism clearly teaches Buddhist parents have a duty to "protect" their children from harmful things (DN 31).
However, being forced to make promises to the Buddha is not proper. Instead, whatever concerns your mother has about your actions, your mother should clearly explain why such actions may be harmful & dangerous for you. If your mother is unable to do this, making promises to the Buddha is unlikely to be effective. Therefore, it is natural for your mind to rebel against or reject anything forced upon it; including rejecting the Buddha.
Rejecting the Buddha does not really collect bad kamma. What collects bad kamma is the underlying reason why the Buddha is being rejected. For example, if the Buddha is rejected to justify engaging in drug taking or sexual misconduct, the collecting of bad kamma will be caused by the drug taking or sexual misconduct.
If you are a young person, it can be difficult to understand lust, let alone give it up; because nature creates lust to control the mind of teenagers/young adults. If lust did not control the minds of young adults, young adults would not have sex and make babies. For most of history, most girls were married at 16 years old and had children very soon after. But today, young people accumulate lust because the corporate society makes money from trading in addictions. Today, most young people believe sex is for entertainment rather than for reproduction. Of lust, the Buddha said:

When a person lives heedlessly, his craving grows like a creeping
vine. He runs now here    & now  there, as if looking for fruit:  a
monkey in the forest.

In summary, it is expected in a society with birth control and pornographic entertainment that young people fight against their parents and fight against the Buddha; unless their parents are immoral sexual liberals. In Buddhism, this is called "amataputtikabhaya" ("danger of not having parents").

What is called an intentional thought?


Repeating the contents of one cell in another, in "real time"

Question: How could I create two Input cells, such that when the first is evaluated, the contents of the first are copied to the second? So for example, there would be two empty cells - let's call them A and B - and when the user enters f[x_]:=x^2+4 in A, nothing happens.  When that same user evaluates cell A, then the contents of cell B are updated to be  f[x_]:=x^2+4.  There is a similar question here, but I don't need to be that low-level about things, and I also don't want automatic refreshing; only when cell A has been evaluated.
Comment: This may be useful for this: http://reference.wolfram.com/mathematica/ref/CellEvaluationFunction.html
Comment: Unfortunately I don't have time to figure it out, but this is what I'd try (not sure it's the best or even that it would work):  first identify cell B somehow, e.g. with a cell tag.  Then make the CellEvaluationFunction locate cell B (NotebookLocate), select it, and replace its contents e.g. using NotebookWrite (NotebookApply? not sure).  Hope this helps.
Comment: @Szabolcs: Ok, thanks, this could be a very good place to start.  DO you know how I would use such a function to update the contents of another cell? It can't be just the usual Dynamic, because I need cell B to be an input cell.
Comment: @Szabolcs: Your suggestion definitely helped, and it works when I enter it directly into the notebook.  However, if I use it as an argument to CellEvaluationFunction, it does not work.
Answer: Why not use CellEpilog? :
CellPrint /@
  {ExpressionCell[ , "Input",
    CellTags -> "CellA",
    Background -> LightGray,
    CellEpilog :> Module[{contA},

      contA = NotebookRead[Cells[CellTags -> "CellA"]] /.
        {Cell[b_, "Input", ___], ___} :> b;
      NotebookFind[EvaluationNotebook[], "CellB", Next, CellTags];
      SelectionMove[EvaluationNotebook[], All, CellContents];
      NotebookWrite[EvaluationNotebook[], contA]
      ]],
   ExpressionCell[, "Input", CellTags -> "CellB",
    Background -> LightOrange]
   };


Difficulty Understanding Conjugate points of a differential equation.

Question:
I was struggling to understand this footnote from the book by Gelfand. I couldnot understand how he takes the solution of the differential equation

 outside the interval $[a,b]$ and also how will the solution of $(26)$ with initial condition
$$h(a-\epsilon)=0$$ $$h'(a-\epsilon)=1$$
Cannot vanish on whole interval $[a,b]$
Comment: A conjugate point is a second/the next greater root of $h$?
Answer: $P,Q$ are defined outside $[a,b]$ by the implicit assumptions of the cited footmark. Also, if $P$ is non-zero on $[a,b]$, then it is also non-zero on some open neighborhood of that interval. A linear ODE has solutions that can be extended to the full intervals over which the coefficients are continuous and the leading coefficient is non-zero.
This all makes it possible to take the initial condition at $a−ϵ$ for a sufficiently small positive $ϵ$.
For the overall claim, the conjugate point is a continuous function of the initial point. If it is greater than $b$ for initial point $a$, then that same is true for initial points in a neighborhood of $a$, simply by continuity.
Comment: "_the conjugate point is a continuous function of the initial point_", i dont know about this can you shed some light about it.
Comment: This is the meaning of the first sentence: "The solution ... depends continuously on the initial conditions" and by the implicit function theorem, the root depends continuously on the solution, as it has to be a simple one (as all roots of a non-trivial solution of a 2nd order linear DE have to be).


Why doesn't a changed particle ever lose energy by interacting with others by radiation of virtual photons? Are all virtual photons exchanged?

Question: I've had it explained to me in a separate post that charged particles are constantly exchanging virtual particles with other charged particles and their energy is a steady state. How it is a surety that all of the virtual photons sent out by the charged particle will be exchanged with other charged particles? Are the particles sent in a directed manner toward other charged particles? How much do we know about this interaction/mechanism? Are some virtual particles just radiated away, leaving the charged particle with less energy?
Answer: Virtual particles are not real
It's in the name. You may draw Feynman diagrams where there are internal lines, and we call these internal lines virtual particles. They are not real. You will never detect a virtual particle. They are not really exchanged between the real charged particles. Virtual particles are just-so stories designed to explain Feynman diagrams - which are really pictorial representations of the calculation of probability amplitudes - in non-technical terms. Don't think that charged particles continually send out virtual particles (I've seen this in popular science accounts, too).
The idea that charged particles interact via "exchanging" virtual photons come from the fact that the Coulomb force may be obtained by the non-relativistic limit of this diagram:

There's an internal line, thus there is a "virtual particle exchanged", but this is not a real description of what is happening, it's a story. The full interaction between two charges in the quantum theory is far more complicated and only approximated by this diagramms, of which "exchange of photon" is a natural-language description which does not do its character as a pictorial representation of a term in a perturbative expansion of the interaction justice.
Comment: @Alfred Centauri: No, we don't as far as I can tell. I don't mean to imply that we know what is *really* happening (whatever that means when talking about QFT), I only want to say that we have no grounds to believe that the virtual particles are anything more than a convenient way to talk about QFT amplitudes in terms of Feynman graphs. And to those who suscribe to the "Shut up and calculate" approach, any talk about what's "really" happening is meaningless, anyway.
Comment: To be sure, to say that they aren't *really* exchanged or that it isn't a *real* description of what is happening possibly implies that we *know* the 'real' description of what's going on.  Do we?
Comment: Yes, we know. The Coulomb potential is what we know well. Retarded potential generalizes the Coulomb potential, but the meaning is the same - it is an interaction potential having nothing to do with emitting photons (no photons are emitted in this diagram). It is a "near" field.


What is $\mathbb{R}/\mathbb{Z}$ isomorphic to?

Question: Intuitively, I see how  is related to $\{e^{i\theta} : 0 \le \theta \le 2\pi \}$. I tired to use the first Isomorphism theory where  it states that the image of φ is isomorphic to the quotient group G / ker(φ). Now, I am so confused now since the kernel doesn't work out.  I am having a hard time to put everything together... How do I prove $\mathbb{R}/\mathbb{Z}$  is isomorphic to $e^{i\theta}$. Or if my intuition is wrong and its isomorphic to something else? I'd appreciate your help!
Comment: Perhaps try the map $\theta \mapsto e^{2\pi i \theta}$ instead of $\theta \mapsto e^{i\theta}$.
Answer: Let $f\colon\mathbb{R}\rightarrow S^1=\{e^{2i\pi\theta}\}$ be given by $f(t)=e^{2i\pi t}$ (show this is a homomorphism). The kernel of $f$ is $\{t\in\mathbb{R}\mid f(t)=1\}=\{t\in\mathbb{Z}\}$ and so $\ker f= \mathbb{Z}$. We also know that $f$ is clearly surjective and so by the first isomorphism theorem, we have $$\mathbb{R}/\mathbb{Z}=\mathbb{R}/{\ker f}\cong\mbox{Im }f=S^1.$$
Answer: Since a real number $x$  ca be uniquely writen $x=k+m$ where $(k,m) \in \mathbb Z \times [0,1[$, we can define the map $m=\mu(x)$ from $\mathbb R$  to $[0,1[$. Under the law: $a \bot b = \mu(a+b)$ we can see that $H=([0,1[,\bot)$  is a group. Since $\mu$ is a morphism and  $\text{Im} (\mu)=[0,1[$ we have ${\mathbb R}/{\mathbb Z} \sim H$


How to increase speed between changing photos in Photos/iPhoto?

Question: I am trying to view through the photos, but the process is painfully slow. How do you increase the speed of transition or remove the transition  altogether so that when you press arrow keys or do three finger swipe the change is instantaneous.
Answer: Turns out my question was not fully researched.
Swipe takes some transition time which I do not know how to modify but using arrow keys gives instantaneous transition.


Did the Ancient Egyptians understand the importance of the brain?

Question: I remember hearing recently that new research has suggested that the ancient Egyptians did not view the brain as a useless organ, and had a reason for disposing of it in the manner they did. I can't find references to this online, and am wondering if anyone on here knows about the validity or otherwise of this idea.
Comment: I cannot give you a source but I believe that they thought the brain was there to cool the body down. So fevers were the result of the brain not working properly thus adding cold compressed would kick start it again.
Comment: Source for the brain being important, please?
Answer: The reason to remove the brain wasn't because it was "useless", but because it was among the first tissues to decay. From http://si.edu/encyclopedia_si/nmnh/mummies.htm

The first step in the process was the removal of all internal parts that might decay rapidly. The brain was removed by carefully inserting special hooked instruments up through the nostrils in order to pull out bits of brain tissue.
Answer: Old Kingdom medical texts speak of the brain having hemispheres and being enclosed in a membrane; the ancient Egyptians also performed (successful) brain surgery
The brain makes mucous (so there was no need to preserve it on the basis of that function) the HEART is the seat of life, thinking, being...
I refer you to this Mummification Processes in relation to the Human Brain and Heart
Answer: The ancient Egyptians did not recognize it as the hub of a person's thoughts. They believed the heart did that job rather than the brain. They wrapped the heart carefully before putting it back in the mummy's body, as they believed the person would need it to get through the Underworld to the Afterlife.
Comment: Do you have a source for that information?
Comment: I have certainly read that various ancient scientists/philosophers did not see the brain as being connected to thought and one would wonder why given that head injury would affect cognition. However, perhaps head injury tended to result in death or they attributed the impairment in cognition to secondary effects of brain injury, like the cooling of the blood (as Aristotle thought) might be necessary for clear thinking/memory.


Should I ask the program coordinator regarding an offered PhD TAship for 11 months?

Question: I have been offered a Ph.D. position in the USA where the offer says 'the admission comes with an 11 month per year teaching assistantship that carries a $2,000/month stipend, health insurance, and a scholarship to cover your tuition. My question is why do they mention 11 months? Does that mean that I will not get payed for the 12th month? I am an international student so I am very confused about the offer. Should I ask the program coordinator regarding this?
Answer: It is likely that it means that duties will extend for up to 11 months. But most places like to spread the payouts over 12 months as it eases the bookkeeping process somewhat.
But yes, you can ask.
Likewise health insurance is most likely to extend over 12 months if the award continues.
In the US, however, every university sets its own policies to some extent, so you need to ask.
It may be that 11 months (rather than 9) is to satisfy the visa people. I don't know the regulations for that, however.


how to validate html code

Question: i want to check the mistake  of  our html code.
are you suggest me  some method in firefox to improve mark up code and solve old code mistake.
How to edit the html. who give me error list node by node like a ul is missing etc.
Answer: I think you need to look at the Web Developer Toolkit and Firebug plugins. Between them you can do just about anything you need to do in terms of debugging html and css.
Comment: The Web Developer is awesome!
Answer: Firefox addon:
HTML Validator
W3C:
W3C Validator
Answer: HI,
You should use firebug for this will help to developer to know about error or mistake.
and
http://validator.w3.org/ is also help to know mistakes and some software as dreamweaver and notepad++ will help.
Answer: Try going to http://validator.w3.org/
Answer: You're after this: http://validator.w3.org/.
Answer: Not sure what you're referring to specifically.  If you're talking about W3C validation you can access the validator tool here.
If something isn't displaying right you could install firebug (Firefox addon) to see what's happening, though this is more of a CSS issue than html.
Without knowing the specifics of your problem it is hard to give more specific advice than that.
Answer: If you're using Java on the server side, you can add the JTidy servlet to your web.xml to validate all HTML generated by your web application.

how to validate html code


What is the difference between "telecast" and "TV program"?

Question: Can I use the words "telecast" and "TV program" to have the same meaning?
Comment: Why the votes to close with no comments or advice posted? I think it's quite an interesting question.
Comment: You may not find it easy to establish any difference from dictionaries, so I don't entirely agree with the (currently ***four***) closevotes. As a general principle, this noun usage of ***a telecast*** refers to *some video content* (loosely, a "program") which is simultaneously broadcast across multiple "channels". A ***program*** is still a program both before and after it has been "broadcast" (or recorded on tape, disc, etc.) since this refers more to the *video content itself*, but a ***telecast*** specifically refers to the *mode of dissemination*.
Comment: If the dictionaries aren't clear, it would be helpful to know what definition the asker is looking at and what confuses them about it. @FumbleFingers
Comment: @ColleenV: I didn't even bother checking any dictionaries myself - but I've just looked now, to find the first definition for ***telecast*** says *[noun:] a television broadcast, [verb:] transmit by television*. So I stand by my comment. Particularly given the only answer posted here before you lot closed the question makes no mention of what I consider to be the key aspect of current usage (***simultaneous*** transmission via ***multiple*** channels, which usually only arises in the context of important ***live*** sports events, etc.).
Comment: So, if you were to answer the question with the same effort put into asking it, you could just say "No, you can't. " I could have chosen the "needs more detail" reason for closing it, but it's still not a great question even if you personally have read some interesting aspects into it. If it's edited to be more than a yes or no question,  I'll vote to reopen. @FumbleFingers
Comment: @ColleenV: Thank you. To be honest, apart from your earlier point (what definition the asker is looking at and what confuses them about it), I can't really see what additional detail we might reasonably expect here. And since we're *still* engaging with the question, I've just been to check the *full* OED listing for **telecast**. Again, nothing to give any hint as to why it's not an exact synonym for *broadcast [by television]* as a verb, and the noun definition simply says *The action or an act of broadcasting by television; a television broadcast or programme.* But I know they're different.
Answer: Yes and no...
Telecast can be both either a verb or a noun so, while I hope you wouldn't, you could say that you "were going to telecast a TV program" (Yes, I know it's a tautology).
But, in the OPs question we can assume that we're discussing the noun form of telecast and here's where it gets a bit tricky. Telecast goes back to the first days of television with the earliest example in the OED dating from 1937 and, by early usage and by dictionary definition, they have the same meaning and are interchangeable. However, I think it's fair to say that today (and I've just confirmed this by that very scientific method - a Google search) telecast is most commonly used in relation to the broadcast of live events.
Some recent examples (from British, US and Australian sources):

For the second consecutive event, viewership of NBCSN's telecast of a Verizon IndyCar Series race showed a significant viewership gain
Phillip Hughes funeral to be telecast live
Clint Eastwood's tasteless Caitlyn Jenner joke to be cut from awards telecast
The 21st Life Ok Screen Awards will telecast on Life Ok on Sunday 25th

What is the difference between “telecast” and “TV program”?


How to perform Gaussian process regression when  function being approximated changes over time?

Question: What are good strategies for performing Gaussian process regression when the function I am trying to approximate changes over time? The naive approach that springs to my mind is to only use the N most recent data points to perform the regression. What are better strategies?
Answer: You could try this method:
Predictive Active Set Selection Methods for Gaussian Processes

We propose an active set selection framework for Gaussian process classification for cases when the dataset is large enough to render its inference prohibitive. Our scheme consists of a two step alternating procedure of active set update rules and hyperparameter optimization based upon marginal likelihood maximization. The active set update rules rely on the ability of the predictive distributions of a Gaussian process classifier to estimate the relative contribution of a datapoint when being either included or removed from the model.
Answer: If you want a fixed budget algorithm, see for e.g.,

M. Lázaro-Gredilla, S. Van Vaerenbergh and I. Santamaría, "A Bayesian
  Approach to Tracking with Kernel Recursive Least-Squares", IEEE
  International Workshop on Machine Learning for Signal Processing (MLSP
  2011), Beijing, China, September, 2011.


I am an Indian citizen. Can I apply for UK visa without submitting passport since I have to travel to another country in that period?

Question: I am an Indian. I am planning a trip to UK and need to apply for a visitor visa. But since I have a Japan trip planned between now and my UK trip, I don't have enough time for a regular UK visa application which could take up to 14 working days. I know if the fast track option, but it is quite expensive. Is there a way to apply for UK visa without submitting my passport?
Answer: You can and this option is available only if you are not using the priority or super priority options. Refer to https://www.vfsglobal.co.uk/india/user_pay_services.html#keep-my-passport-when-applying
Answer: No, unless you have an alternative valid travel document that you can submit with the application. https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/549692/Visitor_Supporting_Documents_Guide_-_English_version.pdf
Comment: Not correct, see answer from Bharg below.


material preview displays objects in black although it has texture

Question: I'm following Lynda's tutorial and I've encountered something that I don't know how to fix.
some of my objects appear in black while I'm in material preview on the layout panel but has no problem on texture painting layout or render view.
im sure its something simple but i have no clow what to do
Answer: Try recalculating the normals, or inverting them! Or create a new picture, in the UV Editing window, and change the color from black to another!
Answer: The texture has its own color, the material has its own! When you switch display / shader modes, you can see, the only color, which you assigned to the material / texture. Recolor the black background, of the face your mesh, in the editor window, to a color identical to the material!


Default login credentials for phppgadmin

Question: Hey guys. I want a situation in which I could point to my phppgadmin url and it opens up without asking for login details i.e. how can I set up phpmyadmin to use a default login credential. Or how can I increase the amount of time before the session expires. These are straightforward with phpmyadmin. Thanks all
Answer: There is no built-in method like phpmyadmin's auth='config' option however http://sourceforge.net/projects/phppgadmin/forums/forum/115884/topic/2026840 might be able to help you.


Is it possible to stop monster respawns in Oblivion?

Question: Is it possible to stop monsters from respawning in Oblivion ? Is it also possible to prevent monsters from auto-leveling with your character?
Comment: There are two discrete questions in the body.
Answer: Sure it is, it just requires finding the right mod..
Mods affecting the spawn rates and leveled encounter lists are a dime a dozen, so I won't bother to recommend specifics. (Similarly, not having used any, I feel unqualified.) Browse reviews and enjoy.
Answer: Concerning the leveling, I can recommend Oscuro's Oblivion Overhaul.
Answer: Martigen's Monster Mod should fix your monster leveling problem. One of its features is:

Statically scaled levelling -- meet creatures and NPCs more powerful than you, and less powerful than you, regardless of your level as you level.


can I say that my variable is "approximately" normally distributed?

Question: I am studying a variable on 50000 observations, I have applied a cox box transformation to make it normal, but even with transformation the kolmogorov smirnof and the anderson darling test tells that my distribution is not normal,
given the qqplot that I have, and the number of observations can I say that my variable is approximately normal? I want to use parametric test but all the test rely on the normality assumption
Comment: Since it is a statistics related question, I think it would be better if you ask it on **www.stats.stackexchange.com** or **www.math.stackexchange.com**.
Answer: Your data is already binned. You should do a Chi-Squared test to see if the hypothesis that your data is normally distributed is correct. You can refer to this question to see how you can go about this.
You will set your baseline as the Normal distribution with the mean and variance from your dataset. Then you will test the difference in expected value between the Normal distribution and yours.
Answer: Many will tell you that normality tests are  overly sensitive, especially given that most statistical tests are robust to even gross departures from normality. If you are very concerned, conduct a parametric test anyway along with a nonparametric, and if the end results agree, stick with the parametric.
Comment: I have used a box-cox transformation to get a normal distribution
Comment: Yes, and that's fine. I just wouldn't worry too much if that transform fails a normality test.  Normality tests are overly sensitive.


How to measaure the similarity between two series?

Question: I'm confused about how to measure the similarity between two time series with the same length. For example, both time series are 2 hours in length and every 5 minutes a point. I really want to know which Distance Algorithm should I use. I have tried the Euclidean Distance but it didn't work well on this type of data. Should I find some point-to-point distance algorithms?
Comment: You can search the asked and now answered questions with the search bar up on this site. For example "distance time series" gives a variety of ideas, how to measure. Maybe this question on [stackoverflow](https://stackoverflow.com/questions/48497756/time-series-distance-metric) gives you more ideas how to define your distance. And if you are there now, you can find a lot of answered questions related, if you use the search too. I hope you find something that fits to your problem. If you have questions, that are not to find in this searches, you are welcome to ask again :)
Comment: It is not possible to answer your question, unless you show examples of the time series. To answer the question, we need to know the invarances needed, for example...warping, uniform scaling, offset, amplitude scaling, phase, occlusions, uncertainty and wandering baseline [a]. [a] https://www.cs.ucr.edu/~eamonn/Complexity-Invariant%20Distance%20Measure.pdf
[b] https://www.cs.unm.edu/~mueen/DTW.pdf
Answer: A classical approach for time series similarity computation is Dynamic Time Warping (DTW).
From your description, it may suit your use case:

In general, DTW is a method that calculates an optimal match between two given sequences (e.g. time series) with certain restriction and rules:

Every index from the first sequence must be matched with one or more indices from the other sequence, and vice versa
The first index from the first sequence must be matched with the first index from the other sequence (but it does not have to be its only match)
The last index from the first sequence must be matched with the last index from the other sequence (but it does not have to be its only match)
The mapping of the indices from the first sequence to indices from the other sequence must be monotonically increasing, and vice versa, i.e. if $j>i$ are indices from the first sequence, then there must not be two indices $l>k$ in the other sequence, such that index $i$ is matched with index $l$ and index $j$ is matched with index $k$, and vice versa

Some alternatives to DTW can be found in this literature review on time series dissimilarity measures.
Comment: Thanks for answering sincerely. But to be the best of my knowledge, DTW  belongs to Elastic Measure. It's  suitable for similarity measurement of sequence changes rather than morphology, e.g. it's often used in speech recognition. I only want to compare the vertical distance between two sequences without considering stretch or transformation of the sequences. It could be a point-to-point way of calculating or something. In addition, the time complexity of DTW is very high. Could you know some Lock-step Measures? Thank you.
Comment: In such a setting, a simple $L_1$ distance would be a good fit.
Comment: Yeah, I think so too. And I have already implemented $L_1$ distance (and $L_2$ distance) but the results are not so good. Since I was calculating directly on the raw data (not exactly, $z-score$ implemented), I am now studying whether I can use some time series representations to make the results better. Can you help me? Thank you.


Isolation problem in AC current sensing circuit

Question: I am working on an electronic project where I have to measure AC current. While searching, I often encountered a term called "isolation", especially when using the shunt resistor. What is this problem and how do I avoid it?
Comment: Here "Isolation" means much the same as "Insulation".  For safety, you would generally want your measurement circuit to be insulated from the 120/240 V AC  circuit you are measuring.
Answer: The best way to measure AC current is by means of an isolated current sensor. One popular and inexpensive component is the ACS712, which is a Hall effect transducer. For more details, see:
https://www.seeedstudio.com/blog/2020/02/15/acs712-current-sensor-features-how-it-works-arduino-guide/
Depending on the magnitude of current you are measuring, a standard current transformer (CT) may be what you need. Many of them are designed for high current up to thousands of amperes, with a standard output of 5A. You can wrap multiple turns on the primary to read smaller currents. And there are some that have a turns ratio of about 500:1 so a 5 ampere current will produce 10 mA, which can be used with a 100 ohm resistor to get 1 volt.
https://www.digikey.nl/en/product-highlight/a/amgis/ct-series-current-transformers
You can also use a high side current sensor like HV7800:
https://ww1.microchip.com/downloads/en/DeviceDoc/HV7800%20A062813.pdf
Answer: Using a shunt resistor generally requires that the sensing circuit is connected (with wires) to the AC circuit being measured. If this is high voltage (say 110 VAC or 220 VAC), this is likely dangerous; requires special care, and limits how the results can be delivered to a computer etc. If the sensing (readout) circuit was battery powered, or doesn't have exposed connections that a person could touch, it might be OK.
Alternatively, a clamp-type current meter can be used. This is basically a specialized transformer, and since the primary (the AC line) and secondary are isolated, the secondary can be connected to instruments or touched by a person without risk.


What does "Each night increases the danger" mean?

Question: In Knights of Pen and Paper, the DM will tell you: "Each night increases the danger" whenever you rest somewhere.  What does this mean?  Do enemies increase in strength the longer I play the game?  I'm at day 606 and it doesn't seem like anything has gotten harder (in terms of individual enemies I mean).
Answer: The longer you camp in a single place the higher the chance of an encounter.  The specific encounters do not get more difficult, they depend on the same enemies list for whatever area.
Answer: From what i can tell it either doesn't mean much about the actual night time. Most people find that the monsters (individually) don't get much harder at night.
Maybe it is just saying that as you progress through the game, there will be more
powerful monsters that you have to verse? Or maybe it is just talking about the difficulty of the game in general.


If a reverse shell target is an administrator does the attacker have admin?

Question: If a reverse shell target is an administrator does the attacker have admin?
I've been trying to find the answer to this but can only find general information on how they connect to a target and not whether they maintain admin.
Comment: Do you mean "If an admin runs the reverse shell, does the shell run as admin?"
Answer: A reverse shell is a normal program. It runs as the user it is executed from. E.g. if you convince a php application to execute a reverse shell, you may get the same user access as the webserver it is running on such as www-data.
With the same logic, if you convince a human user logged with administrator/root rights to run a reverse shell connecting back to your machine then yes, it will allow you to execute the administrator/root commands.


Change values from nominal to numeric

Question: I want to change the values of the class labels from nominal into numeric.
e.g if the values of a class are {iris-setosa,iris-virginica,iris-versicolor} i want to make them {0,1,2} so the instances will have as a value at the class label the form (0,0,1,2,0,1,1,2,0).
Any idea?
Answer: It's called Label Encoding
In python with the help of scikit-learn you can do following:

le = preprocessing.LabelEncoder() le.fit(["paris", "paris", "tokyo",
  "amsterdam"])
list(le.classes_)
le.transform(["tokyo", "tokyo", "paris"])
list(le.inverse_transform([2, 2, 1]))

Documentation for more info: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html
PS: Label Encoding might be responsible for unintentional ordering of possible values. I.E. model might think that nominal value with associated numerical value 2 is more important than the nominal value with associated numerical value 1. The solution is One Hot Encoding.

Change values from nominal to numeric


What is the best practices of using Docker for front end development on OS X and passthrough ENV from host to container

Question: I am looking for best practices about front-end developing on OSX with docker and I have found number of projects on github. Here they are:

docker-osx-dev
boot2docker-xhyve
coreos-xhyve
docker-unison
hodor

The fact is I need two-way syncing files from host system to virtual container and vice versa via mounted (synced) folder and IO performance should be like native one. Therefore I don't consider shared folders FS like vboxsf and vmhgfs. Also it's needed to have some build tools (gulp etc) with working wathcer within shared folder.
What do you think about xhyve (with NFS) instead of VirtualBox? Who tried the unison, what the performance docker provides with it?
At last I have a special task I want to run app.js via nodejs through host to container ENV if it is possible. In other words I have to add ENV variable for PATH to nodejs (within virtual container) to my ~/.bash_profile. Is there any chance to do passthrough NODE_PATH from host to container at all?
Thanks.
Comment: you have to realize your containers are fully self-contained. you can nfs mount host drives to your Linux guest then map them as volumes into the containers for live reload with `docker run -v`. you can pass env vars such as secret keys / dbms ip,user,passw,catalog with `docker run -e`
Answer: Not sure if "best practice" is asking for opinions (which is against SO policy), note that this also heavily depends on your tools chain.
I'm not a fan of boot2docker as it works to date (although it may improve and it may be the best approach in the long term as it is the official approach maintained by the docker team).
EDIT: boot2docker was discontinued and replaced by Docker Machine which does pretty much the same thing but in a more generic way, allowing you to manage Docker daemons locally, in LAN or in the cloud.
For Me, I'm on Windows, but I face the same (even more) difficulties as OSX devs. As I'm using Hyper-V, boot2docker (VirtualBox) can't run, so I have to roll my own. Also, last time I tried boot2docker - it ran TinyCoreLinux, which is another Linux distribution I'd have to learn while my focus is CoreOS in the cloud, so I'd rather just focus on CoreOS.
The target for setting up your dev is as follows:

Have ssh access with mounting rights to a docker host (either in VM or on LAN): this is CoreOS on Hyper-V for me.
Have a native docker client & export DOCKER_HOST=<ip or hostname here>
mount /mnt/from/host working directory into your docker host for live reload: this works through mount.cifs on CoreOS with a systemd unit for me.
Make dev.Dockerfile for your dev requirements, if you're a node developer, start from the node image, npm install gulp/browserify/.. whatever you need as a base image for your projects & docker build -f dev.Dockerfile -t my_dev_container .
docker run -it -v /mnt/from/host/:/src/app/ -e my_dev_container

You are now in a terminal with a fully isolated environment which can be put under source control & replicated between project members and has full live reload abilities.
Draw backs: if you rely on REPL or intelliSense from your IDE, you'll have to have an IDE that can use the remote server. Or you have to run your IDE within the dev container (cloud9 or use X server).
Of course if you live in a terminal and are fluent in vim, you are good to go.


Continuous function with Continous right derivative must be differentiable on $[0,T]$?

Question: I think a continuous function with continuous right derivative  must be differentiable on a closed bounded interval $[0,1]$ but I do not know how to prove it. If this is not true, can any one give me a counterexample?
Answer: This is true. One can prove something stronger with little extra effort:
If any one of the four Dini derivatives of a continuous function $f$ is continuous at a point then $f$ is differentiable at that point. Your hypothesis is that the upper and lower right derivatives are equal and continuous.
This is proved in Bruckner, Differentiation of Real Functions, Theorem 1.3, page 40.
Comment: Up to the right endpoint where differentiability could fail (e.g. shifting $g(x) = x \sin x$ to $f(x) = g(1-x)$).


How to search across all the fields?

Question: In Lucene, we can use TermQuery to search a text with a field. I am wondering how to search a keyword across a bunch of fields or all the searchable fields?
Answer: Another approach, which doesn't require to index anything more than what you already have, nor to combine different queries, is using the MultiFieldQueryParser.
You can provide a list of fields where you want to search on and your query, that's all.
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                Version.LUCENE_41,
                new String[]{"title", "content", "description"},
                new StandardAnalyzer(Version.LUCENE_41));

Query query = queryParser.parse("here goes your query");

This is how I would do it with the original lucene library written in Java. I'm not sure whether the MultiFieldQueryParser is available in lucene.net too.
Comment: MultiFieldQueryParser  is an elegant approach.. :)
Comment: Thanks, your answer is not bad either ;) +1
Comment: It is, at least in 4.8 (the latest as of this Comment), in Lucene.Net - it's in the Lucene.Net.QueryParser Project, under the Classic folder.
Answer: Two approaches
1) Index-time approach: Use a catch-all field. This is nothing but appending all the text from all the fields (total text from your input doc) and place that resulting huge text in a single field. You've to add an additional field while indexing to act as a catch-all field.
2) Search-time approach: Use a BooleanQuery to combine multiple queries, for example TermQuery instances. Those multiple queries can be formed to cover all the target fields.
Example check at the end of the article.
Use approach 2 if you know the target-field list at runtime. Otherwise, you've got to use the 1st approach.
Comment: Thanks for good big picture explanation. The first approach is what I use when storing different types of products in same index, for example Printers versus Monitors, where different fields exist for each doc but I want to use "full-text" search with a simple search string and bring back all products that match.
Answer: Another easy approach to search across all fields using "MultifieldQueryParser" is use IndexReader.FieldOption.ALL in your query.
Here is example in c#.
Directory directory = FSDirectory.Open(new DirectoryInfo(HostingEnvironment.MapPath(VirtualIndexPath)));

    //get analyzer
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);

    //get index reader and searcher
    IndexReader indexReader__1 = IndexReader.Open(directory, true);
    Searcher indexSearch = new IndexSearcher(indexReader__1);

    //add all possible fileds in multifieldqueryparser using indexreader getFieldNames method
    var queryParser = new MultiFieldQueryParser(Version.LUCENE_29, indexReader__1.GetFieldNames(IndexReader.FieldOption.ALL).ToArray(), analyzer);
    var query = queryParser.Parse(Criteria);
    TopDocs resultDocs = null;

    //perform search
    resultDocs = indexSearch.Search(query, indexReader__1.MaxDoc());
    var hits = resultDocs.scoreDocs;

click here to check out my pervious answer to same quesiton in vb.net
Comment: I don't see the point of using duck typing here (i.e. `dynamic` keyword). Maybe you wanted to use type interence with `var`?

How to search across all the fields?


Managing development between multiple SVN repositories

Question: I am working on a software development project that requires me to work in two different SVN repositories. The first repository is the client repository and the second repository is our internal development repository. I am trying to find the most effective way to manage development between the two repositories.
A little more information about the setup...
The client repository is the master. Multiple vendors work on development in separate branches in parallel. When a branch is complete, it is reintegrated into the trunk and all of the active branches merge up from the trunk. I may have 2-3 active branches in the client repository at a given time.
My development repository is the place where all of my team's development takes place. We don't use the client repository solely due to its lack of integration to our development tools for code review and task management. When a project starts, we export the source from the branch in the client repository and import it into our repository. This is where my question comes in...
What is the best way to manage development between the two repositories for a given branch? During the life of a project, there may be updates to our branch in the client repository that need to be pulled in to our repository. We also need to be able to send changes to our branch in the client repository for releases.
I've come up with two potential options thus far:

Use patches to manage the changes between branches. The downfall here is any tree structure changes won't be included.
Use svnrdump to replay the revisions between branches. The problem with this approach is I'm not sure if its the purpose of this tool and what would happen if a revision it replayed conflicted with changes we've made. The tool is primarily for migrating repositories.

SVN 1.7 is used for both servers.
Any thoughts or suggestions are appreciated.
Comment: Frankly, this is a case where Subversion just doesn't have a good workflow.
Answer: Join both repositories into one big "superrepo", using externals (master repo is empty, with real repo, mounted over externals definitions)
This way all transfers of revisions between repositories can be (TBT!) just ordinary merges


What are some thread safe techniques for storing data about a single user's request in a servlet?

Question: If a servlet is not thread safe, then does that mean that all objects created and referenced during the servlet lifecycle are not thread safe? Maybe I'm not quite getting this, but in Web applications you almost always want to account for data stored during the servlet lifecycle that is pertinent to a single user's request. What are some of the best practices for managing this data? Should I instantiate a processor (much like a front controller) for my servlet, and wrap it in a synchronized block? This seems like it would be a major performance issue.
Answer: Just don't assign request or session scoped data as instance variable of the servlet. That's all. Declare and assign them inside the method block and it will be threadsafe (read: it won't be shared among all requests/threads anymore). I've answered this in detail before here: In Java, How do I make sure my web application is thread safe?.
Answer: Instance variables are not thread safe. Local variables (declared within the methods) would be thread safe; each thread essentially has its own set of local variables.
See this JavaWorld article on writing thread-safe servlets for additional tips if you MUST rely on an instance variable.


Relocation to Mars

Question: One billion years into the future and the Sun has swollen in size and it is now not possible to live on Earth due to the heat. Mankind has relocated to Mars where the temperature is more favorable now than on Earth. My question is how much time has this gotten us before we can't live on Mars any longer because the Sun is dying ? The next step I guess will be to relocate to another Solar System ?
Comment: It would be easier to shield Earth from radiation IMO.
Answer: As it gets older, the core of the Sun starts to fill with Helium ash. This increases the average mass per particle and hence the core temperature must increase to maintain the pressure. This increases the nuclear reaction rate and the Sun becomes more luminous, at almost a constant surface temperature.
The habitable zone is controlled not only by the luminosity of the star, but also by the atmosphere of the planet. It's doubtful Mars would ever become "habitable" in that sense (without our intervention), but what I will assume is that you want the equilibrium temperature to be warmer than 263K, but say cooler than 303K (i.e. between -10 and 30 Celsius).
The details of finding the blackbody equilibrium temperature can be found here. The formula we need is
$$ T = \left(\frac {L_{\odot} (1-a)}{16\pi \sigma D^2}\right)^{1/4},$$
where $L_{\odot}$ is the luminosity of the Sun at any time, $D$ is the distance to the planet, $a$ is the albedo and $\sigma$ is the Stefan-Boltzmann constant. $T$ is in Kelvin.
I will assume that the average albedo of Mars is 0.25 (though it varies considerably with wavelength, depends on icecap coverage etc.) and that $D=2.27\times10^{11} m$.
We can then rearrange the formula above to give the luminosity of the Sun for a given equilibrium temperature.
$$L_{\odot} =21.3\ \pi \sigma D^2 T^4$$
This means that for $T>263 K$, then $L_{\odot}>9.35\times 10^{26}\ W$, but for $T<303\ K$, we require $L_{\odot}<1.65\times10^{27}$\ W$. That is the Sun's luminosity should be somewhere between 2.44 and 4.30 times its current luminosity.
The next step is to look at a stellar evolutionary model for a star like the Sun.You can generate one here. I find that the Sun will have a luminosity in this range from ages 8.9 billion (at the cool end) to 10.0 billion years (at the hot end).
Obviously you can play with the numbers (upper and lower temperature bound, albedo) to get different answers (the big assumption was to just use the equilibrium temperature, but an atmosphere could warm things up a bit), but you should be able to follow this prescription using whatever numbers you wish.
Incidentally, at the "cool end" of the Mars calculation - the Earth's equilibrium temperature would be 315K (42 Celsius).
Answer: The Sun is not dying when the Earth supposedly becomes uninhabitable, it is just getting hotter as it evolves on the main sequence (core hydrogen burning, the Sun has something like 5+ billion years before reaching the red giant stage of its evolution)). It will continue to get more luminous and at a guess if Mars could be made habitable it would remain so for millions, possibly another billion years. But then do you really think humanity (or rather our descendant civilization/s) if it survives for another billion years will need a planetary surface to survive?
Comment: Yes, I am aware of this also ,that it would be simpler for us just to move into an artificial habitat in space but don't you think that it would be nicer to live on a Planet ?
Answer:
One billion years into the future and the Sun has swollen in size and it is now not possible to live on Earth due to the heat.

The problem isn't so much the Sun swelling in size. The key problems are that the Sun's luminosity increases over time and that the Earth is covered with oceans. The Earth will become uninhabitable long before the Sun turns into a red giant. Ever increasing luminosity will result in an increased surface temperature, which in turn will result in increased atmospheric water vapor. Right now this isn't a problem because increased atmospheric water vapor means more clouds, which increases albedo, which counters the effect of increased luminosity.
There is another effect to consider, however. Water vapor is an extremely powerful greenhouse gas. At some point, increased surface temperature will increase atmospheric water vapor to the point where the increased greenhouse effect increases the surface temperature, which increases vaporization even more, and so on. A loop! And a loop of the very worst kind, a positive feedback loop.
When this will happen is subject to debate, perhaps as short as 500 million years (Goldblatt 2013), perhaps a billion years (Kasting 1988), or perhaps as long as 1.5 to 2 billion years (Leconte 2013) in the future.

Mankind has relocated to Mars where the temperature is more favorable now than on Earth.

Speculating what humanity will do or become 500 million years from now is a bit much. It's not science; nobody will live to see whether there speculations turn out to be true. But it is a bit fun. Why would we relocate to Mars? Why not relocate Earth instead (Korycansky 2001)? We might eventually have to worry about Mars getting in the way. In that case, simply move Mars, too!

References
Goldblatt, Colin, et al (2013), "Low simulated radiation limit for runaway greenhouse climates," Nature Geoscience, 6.8:661-667.
Kasting, James F. (1988), "Runaway and moist greenhouse atmospheres and the evolution of Earth and Venus," Icarus, 74.3:472-494.
Korycansky, D. G., Gregory Laughlin, and Fred C. Adams (2001), "Astronomical engineering: a strategy for modifying planetary orbits," Astrophysics and Space Science, 275.4:349-366.
Leconte, Jérémy, et al. (2013), "Increased insolation threshold for runaway greenhouse processes on Earth-like planets," Nature, 504.7479:268-271.
Comment: Yes, this would be so much better to just knock Mars out of the way and move the Earth into its orbit farther from the Sun. How would this be done though ?
Comment: @PeterU - Read the referenced article. The answer to your question is there.
Answer: If you  postulate some things that began as humans are still around in 1,000,000 years, I would also guess that way off topic of astronomy (but not for exploration) that "people" will be safely basically immortal in virtual worlds and have sought out the safest place to do their thinking and playing. Perhaps headed outside the galaxy or the plane of the galaxy to wherever the least junk is flying about.
Comment: +1 I doubt the outside of the galaxy though. Two things to consider here. One is the strong radiation from the center of the galaxy, which is shielded by the starts in the plane of rotation, so the plane is the safest place. The other is entropy. Self evolving systems can exist only by dissipating a concentrated energy from a low-entropy source. This is the reason life evolved on the Earth as the right balance between the Sun and space. The same principle on a larger scale would keep the super-intelligent creatures of the future somewhere inside the galaxy(s) in the best entropy balance.

Relocation to Mars


Predicting t+1 from a set of sequences

Question: Say I have have an experiment where I release a single rat into a maze and wait for it to reach the end. Say I also track this rat's position in the maze at various times. Let's do this $n$ times. Now, I have a dataset of $n$ variable-length movement profiles, since some of the animals finish faster, some take longer, etc. Is there a way I can analyze this data to then predict the next move $(x_{t+1}, y_{t+1})$ given an $(x_t, y_t)$? What techniques could I use and how would they work? I was thinking LSTM or RNNs, but unsure what techniques are used for problems like this.
Answer: Yes you can use LSTM or RNN,but from my point of view can be other possibilities.
If you want to use the recurrent neural network you should use a supervised training mode:

(many - to - many)
The input x1 is the move made by the rat at time1 and y1 is the move of the rat at time 2, x2 is the move of the rat at time 2 and y2 is the move of the rat at time3 and so on...
But for your kind of problem a recurrent netowork could not be the best choice. What i recommend you is to use a decision tree, or a random forest algorithm, since the possible outcomes of the rat are just 4: right, left, up, down, and you have a finite state of positions, "the maze". The best you can do is build a random forest algorithm where the features can be, for instance, the walls around the rat, the distances of these walls from it,the distance from the exit and the number of times the rat has passed through that state, and the labels are: right, left, up, down.
Remember that a machine learning model works better then a deep learning model if the data are not consistent and a random forest is a very powerful machine learning tool

Predicting t+1 from a set of sequences


Will a master's in math from the University of Vienna help or hurt my Ph.D. prospects?

Question: My end goal is to become a professor of either mathematics or philosophy. I haven't made up my mind which. Thus, I plan to get a master's degree in math, specializing in logic, in order to continue my education without committing to either discipline. More generally, I suppose my question is: is this a sensible plan, and is the University of Vienna a good place to carry it out?
Comment: I'm not sure why you think it might be otherwise. The university has a very rich history and has produced many Nobel prize winners.
Answer: Kurt Gödel is an alumnus of University of Vienna, and would be a pretty hard act to follow, but at the intersection of math and philosophy he has few peers.
I think your plan is perfectly reasonable, though if you don't speak and write German, you might want to address that. Maybe not essential today, I guess.
The Wikipedia entry on the university is informative, of course.
Comment: There's also the Vienna Circle! https://en.m.wikipedia.org/wiki/Vienna_Circle
Comment: That's at least three generations of researchers ago. Sorry, but this legacy really doesn't matter anymore for anything but the university's public image.


Updates break jquery modules

Question: I could need some help! (Newbie)
After updating a couple of modules on my local site, the jquery modules are behaving really weird or stopped working at all (Masonry, Chosen and Sidr). Masonry and Chosen don't work at all, Sidr just works on those pages where Masonry isn't on. Infinite Scroll stopped working, too and is replaced by the regular pagination. There are no error messages displayed or in the drupal report.
What should I do now?
(the site is running Drupal 7.28 on Acquia Dev Desktop php 5.2.38)
Comment: Clear the cache first, if that doesn't fix it check your browser console; there are likely javascript errors in there
Comment: Did you find the solution? Was it any of the answers below?
Answer:
Caches may be stale. Clear and re-test. Even disable your JS aggregation/minify, then have Drupal regenerate the aggregated JS (after testing with it off to see if that fixed the issue).
If you also use the jquery_update module, you may need to update your jQuery library.
Sometimes replacing the modules with their DEV versions help fix the issue, because sometimes there's small bugs that get fixed in DEV first before a new stable update is tagged and released.
Answer: It could be that you updated jQuery Update, and it provided a version of jQuery UI that was too new for those modules. If you have jQuery Update installed, then try switching the versions of jQuery UI and see if one fixes them.
Also, clear all the caches whenever you switch.

Updates break jquery modules


What is the fastest way to count set bits in UInt32

Question: What is the fastest way to count the number of set bits (i.e. count the number of 1s) in an UInt32 without the use of a look up table? Is there a way to count in O(1)?
Comment: Look at the answer of [this post](http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer).
Answer: The bit-twiddling hacks page has a number of options.
Of course, you could argue that iterating over all 32 possible bits is O(N) in that it's the same cost every time :)
For simplicity, I'd consider the lookup-table-per-byte approach, or Brian Kernighan's neat idea which iterates as many times as there are bits set, which I'd write as:
public static int CountBits(uint value)
{
    int count = 0;
    while (value != 0)
    {
        count++;
        value &= value - 1;
    }
    return count;
}

If you don't like the idea of populating a 256-entry lookup table, a lookup-per-nybble would still be pretty fast. Mind you, it's possible that 8 array lookups might be slower than 32 simple bit operations.
Of course, it's worth testing your app's real performance before going to particularly esoteric approaches... is this really a bottleneck for you?
Comment: This looks more readable, but I still wonder what is the usage of counting bits like this. If I had to guess, I'd say it's probably used in cryptography.
Comment: I was intrigued by the LUT approach. It seems to definitely be slower for population counts, particularly if the intrinsic is available, but for getting the indices of the set bits the LUT appears faster if some care is taken. I've explored this [on GitHub](https://github.com/gsuberland/DotNetBitPositionLookup) if you want to take a look.
Comment: @AlissonReinaldoSilva in my case, I have to calculate how many IP addresses there are within a given subnet mask:
`var power = IPAddress.Parse("255.255.240.0").GetAddressBytes().Select(b => b.InverseBits()).CountSetBits(); var addressesInNetwork = Math.Pow(2, power);`
Comment: @AlissonReinaldoSilva : One esoteric case I use this for is to check that a number is a positive power of 2; i.e. `var isPowerOf2 = (CountBits(input) == 1 && input>0);`
Answer: Is a duplicate of:
how-to-implement-bitcount-using-only-bitwise-operators
or
best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer
And there are many solutions for that problem. The one I use is:
    int NumberOfSetBits(int i)
    {
        i = i - ((i >> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
        return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
    }

Comment: I tested the performance of this answer, and the one from Jon Skeet. While that approach has a better readibility, this one is the fastest.
Comment: @raoul: Though this has the same performance for every case (12 arithmetic operations including one multiplication), Jon's version (actually Brian Kerninghan's) can be much faster if only a few bits are set (which is a typical case for enums).
Comment: Which one has better performance will depend on _the rest_ of your application code. Whenever you're comparing two relatively fast alternatives, real performance will really be dominated by other factors outside of the method body, such as how stressed is the CPU instruction / memory cache typically at the time this method is called, and how much branch mispredictions and cache-misses do you typically get as a result. Purely contextual. ...Anyway the point I was getting to is that less branching == more predictable performance - not always the fastest - but predictability is very valuable too.
Comment: (...cont. The classic opposite example that everyone is familiar with is when you get that sudden dip in performance because a full garbage collection kicked in.)
Answer: In .NET Core 3.0 the x86 popcnt intrinsic has been exposed, allowing you to perform a single-instruction population count calculation on a uint or uint64.
int setBits = System.Runtime.Intrinsics.X86.Popcnt.PopCount(value);

There is also a 64-bit version System.Runtime.Intrinsics.X86.Popcnt.X64.PopCount() that can be used on a ulong when running on a 64-bit CPU.
Comment: @Polynomial There is an even better solution since .NET Core 3.0: [BitOperations.PopCount](https://learn.microsoft.com/de-de/dotnet/api/system.numerics.bitoperations.popcount), which is available across all platforms and (hopefully) uses the intrinsic operation you mentioned.
Comment: @AlexNorcliffe That isn't possible with an intrinsic. The simplest approach is just to loop through all bits in the value and put the indices of the set bits into a list. You can speed this up further by computing the popcount ahead of time so you can use a preallocated array rather than a `List` object. The faster way is to do what John Skeet mentioned above and split the number into chunks so you can use a lookup table. I've investigated this [here](https://github.com/gsuberland/DotNetBitPositionLookup) and shown that it is generally faster than computing the positions each time.
Comment: this is great - thanks! I must admit I'm mystified why HashSet is performing fastest for me, even when using a the very latest reference source for BitArray in .NET Core from Feb 2019 that has optimisations using Span and aggressive inlining. In my test I have 5 million values, and 2.5 million secondary values. My goal is to essentially end up with a list of which values in the first are not in the second. They are contiguous integers so I expected a BitArray to win - can't quite wrap my head around how calling HasSet<>.Remove could be faster! Tho it does use more allocations obvs
Comment: @AlexNorcliffe I looked at the reference source for BitArray and each new instance creates a heap allocation (array of Int32 values large enough to hold the bits), extending the array (i.e. setting the Length property to a larger value) causes a new allocation and copy, shrinking the array causes the unused array elements and final-element bits to be cleared. Foreach loops over a BitArray will also allocate a new enumerator object on the heap, but the Get function should be fast: one aligned memory access, one division, one modulo, one variable shift left, one AND, one integer comparison.
Comment: @AlexNorcliffe Another potential option is to use BitVector32, which is effectively an encapsulation of bit extraction maths with some convenience methods, and since it's a struct it gets put on the stack rather than the heap. But the fastest performance is almost certainly always going to be doing the calculation manually in your own code, all inline, because you can avoid doing calls and branches (most calls to framework classes have parameter validation!) and keep things in a very tight loop for better performance.
Comment: thanks! Do you know if there is an intrinsic or defacto algorithm for returning the indices of the set bits, not just the counts?
Answer: A platform independent BitOperations.PopCount is available in Core 3.0 and higher.
Hardware intrinsics are used when available, otherwise it defaults to a software fallback. Currently, both X86/64 and ARM processors are supported.
Source
Full credit to @Mark who mentioned this in a comment to another answer.
Comment: Perfect! Thanks!
Comment: Also note Jeffs answer where this is now even better in .Net 7 :)
Answer: As of .NET 7 Int32 (and other integer types) have a static PopCount method so you can just call int.PopCount(int bitMask) without having to write your own algorithm or rely on System.Numerics and unsigned types with BitOperations.
https://learn.microsoft.com/en-us/dotnet/api/system.int32.popcount
Answer:
Here is the solution in java to get the set bits of a given number.

import java.util.*;

public class HelloWorld {

static int setBits(int n) {
    int count = 0;
    while(n != 0) {
        count+= ((n & 1) == 1) ? 1 : 0;
        n >>= 1;

    }
    return count;
}

 public static void main(String []args){
     Scanner sc = new Scanner(System.in);
     int n = sc.nextInt();
     System.out.println("Results: " + HelloWorld.setBits(n));
 }
}


What is the meaning of the sentence in bold? How to analyze its tense

Question:
<@1.1>It's only 4 hours flying time from Sydney, but a world
  away.<@1.2>What better place to rest than a country where the only
  place people hurry is on the football field and things are done in
  "Fiji time"?
<@2.1>Viti Lev-Great Fiji-is the largest island.<@2.2>Here you'll find
  the capital Suva and the international airport at Nadi Vatoa, on the
  other hand, is a tiny island in the farthest part of Hiji.<@2.3>Then
  there are 331 other islands, many of them with places to stay.
<@3.1>With less than a million people living on islands, you'll never
  feel crowded.<@3.2>And with a climate(气候)that changes only for five
  degrees between seasons, there's never a bad time to come.
<@4.1>From cities to villages, from mountains to beaches, from water
  sports to wooden artworks, Fiji can give you more adventures and
  special experiences than you could find almost any where in the world.
<@5.1>Whenever you come , wherever you go ,you're sure to see some
  unforgettable events , From war dances to religious (宗教的)
  songs.<@5.2>From market days to religious days.<@5.3>It's not just
  staged  for tourists; it's still a part of everyday life in Fiji And
  any one of us can enjoy Fiji's spirit by being part of the traditional
  (传统的) sharing of yaqona-a drink made from the root of a Fiji plant
<@6.1>So why not join us for the experience of a lifetime?
Answer: Literally, the phrase things are done in "Fiji time" means that [people who live there] do things in "Fiji time". This suggests that Fiji people do things in some unique way.
Fiji time refers to the time people spent in Fiji.
As the previous part of the phrase suggests, Fiji is the only place where "the only place people hurry is on the football field". This means that they don't rush things in life (except on the football field). Staying in Fiji must be very casual and easygoing.
What a good place to stay and enjoy your vacation!
Answer: Someplace time is a phrase used to show that a geographic location is using a different time from another geographic location

Greenwich Mean Time (GMT) (+0 UTC)
  Eastern Standard Time (EST) (+5 UTC)
  Daylight Savings Time (DST or BST)

that it is a particular time zone which uses a specified time to coordinate activities
Things are done in Fiji time is a colloquial expression meaning Fiji is metaphorically in its own time zone. It is used to contrast the rhythm and speed things happen in Fuji compared to the rest of the world.  The writer is saying that things happen at a more laid back, slower, and relaxed pace in Fiji versus Sydney (in the example), which is a large bustling city.
This is characterised by being

a world away (from Sydney)

where

the only place people hurry is on the football field

and answers the question

What better place to rest

This is in contrast to New York time

New York Minute

where time is at a premium in the city that never sleeps and people are constantly rushing around just to stay in place and not fall behind.