How to get hourly average for a timeseries in R for a specified date range?

Question: I have hourly data for multiple areas A,B,C over 2 years. I am looking to be able to get an hourly average over a specified date range for each area. Sorry, I read through:
How to make a great R reproducible example
but wasn't sure how to use dput() to properly represent the data I have. Please see below for my sample data from copying the output from dput(mydata):
structure(list(time = structure(c(1451606400, 1451610000, 1451613600,
1451617200, 1451620800, 1451624400, 1451628000, 1451631600, 1451635200,
1451638800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
    A = c(0.0173731172095063, 0.0175417882503753, 0.0175839560105925,
    0.017499620490158, 0.0173309494492891, 0.017668291531027,
    0.017836962571896, 0.017836962571896, 0.0182586401740685,
    0.0234452746807901), B = c(0.0173567013800694, 0.0173567013800694,
    0.0170744785934016, 0.0172155899867355, 0.0170744785934016,
    0.0172155899867355, 0.0172155899867355, 0.0172861456834025,
    0.0173567013800694, 0.0198261507634126), C = c(0.00791114205246669,
    0.00806936489351603, 0.00806936489351603, 0.00806936489351603,
    0.00806936489351603, 0.00822758773456536, 0.00854403341666403,
    0.00854403341666403, 0.00854403341666403, 0.012341381601848
    )), class = "data.frame", row.names = c(NA, 10L))

Basically I took the time data I had and created separate columns to represent the year, month, day and hour.
structure(list(Year = c("2016", "2016", "2016", "2016", "2016",
"2016", "2016", "2016", "2016", "2016"), Month = c("01", "01",
"01", "01", "01", "01", "01", "01", "01", "01"), Day = c("01",
"01", "01", "01", "01", "01", "01", "01", "01", "01"), hour = c("00",
"01", "02", "03", "04", "05", "06", "07", "08", "09"), timedata = structure(c(1451606400,
1451610000, 1451613600, 1451617200, 1451620800, 1451624400, 1451628000,
1451631600, 1451635200, 1451638800), class = c("POSIXct", "POSIXt"
), tzone = "UTC")), class = "data.frame", row.names = c(NA, 10L
))

I am looking to get the average values populated for 24 hours as follows based on a specified date range. The reason why I broke up the time into specific year, month, day, and hour columns was to do something like group_by(), but I have a couple issues.
I am looking to get the average for a specified date range (e.g. Jan to Mar without weekends).
The final output matrix I am expecting should be a matrix 25 x 4. Value x at hour 0:00 below would be the average of the values for weekday in Jan to Mar at hour 0:00 for area A.
time A B C
0:00 x
1:00
2:00
3:00

Thank you.
Answer: You can try this -
library(dplyr)
library(lubridate)

df %>%
  mutate(month = month(time),
         hour = hour(time)) %>%
  filter(format(time, '%u') %in% 1:5, month %in% 1:3) %>%
  group_by(hour) %>%
  summarise(across(A:C, mean, na.rm  =TRUE))

format(time, '%u') %in% 1:5 would keep only the weekdays (Mon-Fri), month %in% 1:3 would keep only the months Jan-Mar.


Combine class and method parameterized type

Question: I have an interface like this:
public interface BatchSynchronisedPool<R extends Runnable> {
    void execute(R runnable, Object batchIdentifier);
    public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier);
}

I want to infer an upper bound for Callable, but still want to be able to keep the <T> argument on the method:
public interface BatchSynchronisedPool<R extends Runnable, C extends Callable> {
    void execute(R runnable, Object batchIdentifier);
    public <T> Future<T> submit(C<T> callable, Object batchIdentifier);
}

Obviously that doesn't work, because the type of C I specify may only take a specific range of T arguments.  However, now you have the general just of what I am trying to do, is there a  possible solution, or am I stuck always having to submit a callable? (Or remove generics altogether and perform an unsafe cast)
Answer: I'm not 100% certain, but don't think that you can do what you're trying to do here.  Since C is not generic, you can't use C<T>.  Lots of code below, but tl;dr take option 3.  All that really changes in the end is how many BatchSynchronisedPool objects you'll need to create, which is not really a considerable overhead...

1. Keep the <T> generic type parameter on the method, submit a Callable<T> and perform a runtime check of the types, like your original solution, in the class that implements this interface.
public interface BatchSynchronisedPool<R extends Runnable> {
    void execute(R runnable, Object batchIdentifier);
    public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier);
}

public class MyBSP<R, C> implements BatchSynchronisedPool<R, C> {
    void execute(R runnable, Object batchIdentifier) { ... }
    public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier) {
        // Check types.
        if (!(callable instanceof MyDesiredCallableClass)) {
            throw new IllegalArgumentException("Types don't match.");
        }

        // Do work.
        T result = callable.call();

        ...
    }
}

public class MyUsageClass {
     public static void main(String[] args) {
         // Submit string.
         MyBSP<Runnable> bsp = new MyBSP<Runnable>();
         bsp.submit(new StringCallable(), someObject1);

         // Submit integer.
         bsp.submit(new IntegerCallable(), someObject2);
     }
}

2. Keep the <T> generic type parameter on the method, submit a C and perform a cast, like you've suggested, in the class that implements this interface.
public interface BatchSynchronisedPool<R extends Runnable, C extends Callable> {
    void execute(R runnable, Object batchIdentifier);
    public <T> Future<T> submit(Class<T> cls, C callable, Object batchIdentifier);
}

public class MyBSP<R, C> implements BatchSynchronisedPool<R, C> {
    void execute(R runnable, Object batchIdentifier) { ... }
    public <T> Future<T> submit(Class<T> cls, C callable, Object batchIdentifier) {
        // Do work... with a cast.
        T result = cls.cast(callable.call());

        ...
    }
}

public class MyUsageClass {
     public static void main(String[] args) {
         // Submit string.
         MyBSP<Runnable, Callable> bsp = new MyBSP<Runnable, Callable>();
         bsp.submit(new StringCallable(), someObject1);

         // Submit integer.
         bsp.submit(new IntegerCallable(), someObject2);
     }
}

3. Create a new BatchSynchronisedPool for each type T that you're trying to submit by specifying T as a generic type parameter for the class.  Then each time you want to call submit on a different type, you'd need to generate a new instance of BatchSynchronisedPool.
public interface BatchSynchronisedPool<T, R extends Runnable, C extends Callable<T>> {
    void execute(R runnable, Object batchIdentifier);
    public Future<T> submit(C callable, Object batchIdentifier);
}

public class MyBSP<T, R, C> implements BatchSynchronisedPool<T, R, C> {
    void execute(R runnable, Object batchIdentifier) { ... }
    public Future<T> submit(C callable, Object batchIdentifier) {
        // Do work.  Types are okay; no checking or casting needed!
        T result = callable.call();

        ...
    }
}

public class MyUsageClass {
     public static void main(String[] args) {
         // Submit string.
         MyBSP<String, Runnable, Callable<String>> stringBsp = new MyBSP<String, Runnable, Callable<String>>();
         stringBsp.submit(new StringCallable(), someObject1);

         // Submit integer.
         MyBSP<Integer, Runnable, Callable<Integer>> integerBsp = new MyBSP<Integer, Runnable, Callable<Integer>>();
         integerBsp.submit(new IntegerCallable(), someObject2);
     }
}

Comment: If you have the class at runtime as in (2), you can do a checked cast and avoid the warnings at `T result = (T) callable.call()` with `T result = cls.cast(callable.call())`
Comment: Good answer but I see a couple of mistakes in it: in **2.** and **3.** `MyBSP` is not properly implementing the interface as the `submit` method has different signatures. In **2.** the `submit` method in the inteface should have the `Class` parameter. In **3.**, since the class is also parametrized with `T`, I'd remove the `Class` parameter from the class method and I'd move it to a constructor. My 2 cents.
Comment: @AlonsoDominguez You are correct; sorry for those mistakes.  Let it be known that it is a *horrible* idea to edit this much code *in* StackOverflow.  Many, many copy/paste errors.


Is there a way to change the service account from "This account" to "Log in as" using Powershell?

Question: New to powershell but bear with me. I am trying to automate an install of Prosystem Fx Engagement, but need to install a SQL instance. I have already done this part, but I will also need to automate the changing of a services account within services.msc. It will need to be changed from "This account" to "Log in as Local System Account"
https://i.stack.imgur.com/iDrHb.jpg
The name of the service is MSSQL$PROFXENGAGEMENT, and the display name of the service is SQL Server (PROFXENGAGEMENT).
I don't really want to use the method below because the password is visible on the .ps file. I tried looking around but was not able to find anything. Is this even possible?
<code>$LocalSrv = Get-WmiObject Win32_service -filter "name='MSSQL$PROFXENGAGEMENT'"
$LocalSrv.Change($null,$null,$null,$null,$null,$false,"DOMAIN\administrator","PASSWORD")
</code>
Answer: As for the method you are saying you don't want to use for the account change activity. You don't have to and should not put plain text password in scripts.
You can prompt for the password, store that in a variable and use it. Yet, that means a human has to be there to address that, so, not very automated.
So, this means you need to set the credentials in a file or other store and call that from your script.
Meaning like what is described below. This of course has to be done in advance of any other use case that will need it.
using secure password with multiple users without prompt
<code>#saving credentials
Get-Credential | Export-CliXml -Path c:\credential.xml

#importing credentials to a variable
$Credential = Import-CliXml -Path c:\credential.xml
</code>
A YouTube Video on the topic:
Learn to securely use Passwords with PowerShell
You could also use the Windows Credential Store, and call it from there. Also shown in the above video.
Using Windows Credential Manager
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde
https://www.powershellgallery.com/packages/CredentialManager/1.0
https://www.experts-exchange.com/questions/29061982/Powershell-Using-credentials-stored-in-Credential-Manager.html
Comment: Thank you! I have stored my password into Credential Manager and it is passing through successfully! using ```$managedCred.username,$managedCred.GetNetworkCredential().Password ``` instead of strings
Comment: No worries. Glad to help. Be sure to mark as your accepted answer for the benefit of others that may be looking for similar needs.


All the solutions of $f'(x)=f(x+\pi/2)$

Question: Consider the following equation (with $f \in C^{\infty}(\mathbb{R})$): $$f'(x)=f(x+\pi/2)$$
This equation is satisfied by $f(x) = A\cos(x) +B\sin(x)$, for any $A,B \in \mathbb{R}$.
Question: What are all the (other) solutions of this equation (if any)?
Comment: Duplicate of [How to solve differential equations of the form $f'(x) = f(x + a)$](https://math.stackexchange.com/questions/61818/how-to-solve-differential-equations-of-the-form-fx-fx-a)
Comment: general solution https://math.stackexchange.com/a/4379608/532409
Answer: The "Ansatz" $f(x):=e^{\lambda x}$ leads to the equation
$$\lambda=e^{\lambda\pi/2}$$
with the obvious solutions $\lambda=\pm i$. But there are (probably an infinity)  other complex solutions, one of them being
$$a\pm ib:=1.0214 \pm 4.86821 \, i$$
(found numerically). The functions
$$f(x):=e^{ax}\bigl(A\cos(bx)+B\sin(bx)\bigr)$$
are then new solutions of your delay-differential equation.
Comment: Yes! Your first equation is a particular case of the one solved [here](https://math.stackexchange.com/a/217262/84284).  It follows that $\lambda = -\frac{2}{\pi}W_k(-\frac{\pi}{2})$, with $W_k$ a branch of the [Lambert W-function](https://en.wikipedia.org/wiki/Lambert_W_function#Numerical_evaluation).
Comment: I was considering this way of solving it too using Fourier transformation, but I haven't found a proof that the solution need to be Fourier transformable. This means that I couldn't rule out the possibility of yet even more solutions to the equation.
Comment: @YvesDaoust: These are just the solutions that come immediately to mind. I'm sure the theory alluded to in my answer has a lot to say about this problem. E.g., one could choose $f$ arbitrarily in $[0,\pi/2]$, just making sure that the ends meet in a $C^\infty$ way, and then extend $f$ to all of ${\mathbb R}$ using the DDE.
Comment: Using an ansatz indeed allows you to find some solutions, but is not necessarily sufficient to exhaust them, is it ?
Comment: @ChristianBlatter: this is the interesting part of the question.
Answer: A heuristic comment. The ansatz above works because the delay operator $f(\cdot) \mapsto f(\cdot+a)$ can be formally written as $e^{aD}$, where $D = \frac{d}{dx}$. This is simply the operator-side manifestation of the Taylor's theorem.
So your equation can be formally written as $(D - e^{\frac{\pi}{2}D}) f = 0$. Considering that the eigenfunctions of $D$ are exponential functions, this operator diagonalizes over the space of exponentials:
$$(\lambda - e^{\frac{\pi}{2}\lambda}) e^{\lambda x} = 0.$$
So it suffices to hunt for values $\lambda$ for which $\lambda - e^{\frac{\pi}{2}\lambda} = 0$ holds. Of course, all this discussion is not so rigorous and the possibility of other form of solutions is still open.
(Though I humbly suspect that the span of the solutions of the form above is in some sense 'dense in $C^{\infty}$' so that any solution is a possibly infinite linear combination of the solutions of the form above.)
Comment: A similar argument: taking the Laplace transform leads to an expression that has for poles the solutions of the same equation.
Comment: @SebastienPalcoux, I am not claiming that this is the solution, precisely for the reason you mentioned: operator-theoretic approach does not work well in general. Hence it is heuristics. On the other hand, the situation is not that bad considering that $iD$ is a self-adjoint operator on $C_c^{\infty}(\mathbb{R})$ and hence the functional calculus allows $e^{aD}$ to be defined on a broader range of functions, say $L^2(\mathbb{R})$. Of course, for application to our problem this is still not enough. (Any solution of the form above does not lie in $L^2(\mathbb{R})$.)
Comment: For more details on "operator-side manifestation of the Taylor's theorem" I found  https://math.stackexchange.com/q/278365/84284
Comment: @YvesDaoust: the assumption $C^{\infty}$ is not strong enough for defining the delay operator with derivative:  https://mathoverflow.net/a/275319/34538

All the solutions of $f'(x)=f(x+\pi/2)$


Why block scope at global namespace is not allowed?

Question: I am curious as to why creating a block scope {} outside any function (in global scope) is not permitted.
I am able to do the following:
int main(){

  {
    int i = 4;
  }

    //other stuff

}

but not
{
    int i = 4;
}

int main(){
    //other stuff
}

This issue piqued my interest while implementing a compile time assert, for which I am creating a variable which is to be destroyed immediately after the assert logic is done (to free memory).
Comment: What do you expect to accomplish, by that?
Comment: Can't you do the same thing with a namespace (if you give it a unique name).
Comment: Are you aware of [`static_assert`](https://en.cppreference.com/w/cpp/language/static_assert)?
Comment: @Justin Yes, I am aware. I am implementing some core functionality/library myself in order to learn better.
Comment: @HolyBlackCat Won't the object persist in memory if I do that
Comment: @User10482 `static_assert` is built into the language. You can't properly reimplement it using other language constructs. *"Won't the object persist in memory if I do that"* It will (unless you don't use it and allow the compiler to optimize it away by giving it [internal linkage](https://en.cppreference.com/w/cpp/language/storage_duration), e.g. by making it `static`).
Comment: Thank you guys for the input. Why does it has to always "accomplish something" to be asked on this forum. I am trying to learn programming by myself and I try to do so by implementing ideas that I have. There might be a well established library/ in-built that does this, but I want to do it myself with my idea. This teaches me a lot that I would otherwise never have learned. Why is this a problem?
Comment: @User10482  -  the purpose of a programming language is to be a tool (or a set of tools) that can be used to direct a computational device to achieve something useful.   So any features added to a programming language need to achieve *something*.   It might make some task possible that would not be possible without that feature.   It might make it easier to achieve something that could be done in other ways.    But a feature of a programming language - which will take effort to implement - that doesn't achieve anything of use is pointless and irrelevant.
Comment: @Peter I agree with what you say. But again, I believe that the learning stage should be not be bounded. "Why is it so?" is an important question to ask, and it should be asked to understand why the standard is-- what it is today. Anyways, I won't bog down this question thread too much with my personal opinions. It is a bit disheartening to get curiosity down-voted just because someone thought it is not useful to them.
Comment: @User10482 - I'm not arguing against curiosity, in learning or anything else (and I'm also not one of your down-voters).  However, your question here is not a "Why is it so?" - it is a "Why isn't this done in that way?" question, for which a perfect valid response is the question "Why would you do it that way?".   If you get that question, the person is asking you to satisfy *their* curiosity.   If your curiosity has a place, so does theirs - but you treat their curiosity as suppressing yours.
Answer:
I am curious as to why creating a block scope {} outside any function (in global scope) is not permitted.

It would be pointless and misleading.  Variables declared outside any function are statically allocated so never go out of scope (except at the end of the program).

Edit: to make a variable exist transiently on program startup, you can do something like:
static auto throwaway_name = [] () { MyClass myVar; return 0; } ();

The only thing this allocates statically is an int (which might get optimised out, I'm not sure).
Comment: Yep, and to try to prevent that I was trying to use a scope block to separate it out from the ```statics```
Comment: @User10482 Added a rider to my answer, hope it helps.
Comment: Interesting use of lambda. Thanks!
Answer:
Why block scope at global namespace is not allowed?

Only declaration statements are allowed in the global namespace by the grammar. No block statements or expression statements.
Let's say that the language was changed to allow block statements in namespace scope. Think about what should the behaviour be. When are those statements executed? In what order?
P.S. main must return int.
Comment: Maybe behavior just like that of a block scope inside a function? The order can be after all globals, since things inside block scope are able to see outside, and not the other way. Thanks for pointing out the ```int``` for ```main```
Comment: @User10482 `Maybe behavior just like that of a block scope inside a function? ` A function is called like this: `name(arguments)`. How would the namespace scope be called? `The order can be after all globals` How about order of one block statement in namespace scope versus another block?
Comment: I meant behavior like that of a block scope inside a function (like the one inside ```main``` in the question). As for the order of two blocks, they are essentially independent of each other, so won't matter in what order they are processed
Comment: @User10482 Yes. The block scope of a function is executed when the function is called. When would the block scope in namespace scope be executed?
Comment: Is 'namespace scope' synonym for 'block scope'? I am thinking it won't be callable. It will execute once just like it does for global ```statics```. PS: I am no expert on this. Just putting out my ideas. This is a fun conversation!
Comment: @User10482 Namespace scope is where you can define free functions. For example the global namespace or a named namespace. You could try writing a proposal for the committee to see if something like this would be added to the language. You can already achieve almost the same by simply calling a function from initialisation of a global object. Though I can see that being guaranteed to be run after **all** global objects could be of some value in theory. Though I can't come up with an example use case.
Comment: >simply calling a function from initialization of a global object. You mean something like Mr. Sanders suggested [here](https://stackoverflow.com/a/57829559/11205473)
Comment: @User10482 Indeed. You can also use a named function if you so prefer, or you can write a constructor for the global object.
Comment: Thanks! Also, I found a nifty trick from ```Modern C++ Design``` by Alexandrescu. He uses ```sizeof()``` and embeds the object initialization inside it with the test expression for asserting. Apparently, ```sizeof()```discards the object after it is done evaluating the expression.
Comment: Although, I am having some trouble implementing it. Maybe it's just that the language has changed a bit since the book was written.
Comment: @User10482 I don't think the expression within `sizeof` is evaluated at all. Only the type of the expression matters. Templates within the expression would be instantiated though.
Comment: You are correct. I tried it and it suffers from similar issue. I think no  standalone expressions are allowed in the global namespace (which makes sense). Here is the code [PasteBin](https://pastebin.com/g5gRj6zS)
Answer:
6.3.3 [basic.scope.block] A name declared in a block (8.3) is local to that block; it has block
  scope. Its potential scope begins at its point of declaration (6.3.2)
  and ends at the end of its block. A variable declared at block scope
  is a local variable

You can't have a nameless block scope in the global namespace scope.
But you can introduce a namespace
namespace N {
  int i = 4;
}

Comment: I see. but that variable would persist in the namespace, whereas ```{}``` would have destroyed it on block exit.
Comment: @User10482 I think you're stuck. To my knowledge the best you can do is make the memory lost as small as possible. Make a `char` variable and initialize it with a function that returns a `char` and do the work in the function. All of the big stuff goes out of scope with the end of the function and the only residue is the `char`.
Comment: @user4581301 Might be so. Thanks! Also, I found a nifty trick from Modern C++ Design by Alexandrescu. He uses ```sizeof()``` and embeds the object initialization inside it with the test expression for asserting. Apparently, ```sizeof()``` discards the object after it is done evaluating the expression
Comment: @User10482 I think I'll be slipping that trick into my back pocket for a trial later.
Comment: @user4581301 Well that did not go so well. It suffers from the same issue. I think no expressions are allowed in global namespace (sensibly so). You can have a look at the code [PasteBin](https://pastebin.com/g5gRj6zS)


How to effectively and efficiently remove varnish from wood

Question: I have been restoring an old staircase. I started sanding it with fine sandpaper and then tried rougher paper. It seems that the varnish layer on the wood is melting when sanding over it and sticking to the sandpaper, rendering it useless in a matter of seconds. It is hard to peel the varnish off the paper.
The staircase has a lot of perpendicular surfaces and corners; these are hard to reach and the edges of the sandpaper gets cluttered immediately.
The current result is a staircase where the varnish is sanded in the middle of the surfaces and around the edges there is still a lot of varnish left.
I have used cellulose thinner twice, but that was not effective at all. What did I do wrong here?
My question is: how can I remove the varnish from the wood easily, especially in the corners?
Answer: I've had great results using Citristrip, (see below). I've used it indoors many times and it leaves no fumes. I've used it to strip multiple layers of paint and varnish from furniture.

There are many products to choose from but I know this one works. It just goes on with a brush. Good luck, you took on a big job redoing a staircase.
This product looks similar due to the fact that it is for indoors and is a gel, which is what you need for a staircase. Verity it with the store personnel. It is sold in your neck of the woods.
Comment: Thank you for the tip. I'm a Belgian resident, so I cannot find products named Citistrip or stripping gel in my store (it's in Dutch or French). Do you think the products in the link are the same type? https://www.brico.be/nl/search?text=afbijtmiddel
Comment: @Slowspark  Check my edited answer.
Answer: You might want to try a scraper, which will do particularly well with corners.
In the current era, a scraper with a reversible & replaceable tungsten carbide blade is probably your best bet unless you really enjoy sharpening/burnishing a steel scraper.
A scraper can do the whole job, or assist with those parts of the job that other methods do not quite do.
You might also test a small area with alcohol just in case your "varnish" is actually shellac, which will easily dissolve with alcohol.
Answer: Check out Diamabrush (https://diamabrush.com/). I used one in an angle grinder to strip 7 layers of sander clogging paint from 700sqft of pine flooring. It worked great.
Comment: Looks great for big surfaces, but I mentioned that I doing my staircase. A rotating blade won't get near the corners.


Re-wet Edge of Stain

Question: I'm staining a new shop (using Sikkens Cetol SRD) that's sided in T1-11 with cedar bats.
On the first day my partner got a little carried away and stained all of the walls from ground level up to about six feet.
Yesterday I tried staining up to (and slightly over) the previously stained portion and ended up with some pretty noticeable lap marks.
I'm hoping I can spread them out a bit with paint thinner or mineral spirits. Any advice in that area is also appreciated.
But my question is is there a good way to avoid new lap marks on the rest of the siding now that I no longer have a wet edge to work from? Would it make sense to brush some paint thinner or mineral spirits on the previously stained area before continuing the stain to the end of each section?
Answer: Unlike painting you cannot fix this by painting/staining over it seems. The only working solution is to remove stain somewhat uniformly from the overlapping areas using thinner. I'm not sure for how long after staining you can still use thinner. If that stops working, you can try paint/varnish strippers, which may remove some but not all staining because stain sinks into the wood fibers rather than forming a film like paint. Failing all that, you have to sand the wood to remove the marks.
Comment: I was going to disagree until I remembered this is t111 with the rough texture it would be tough to remove, you may be better off trying to blend some additional stain cut with the same solvent that the stain uses (usually mineral spirits).

Re-wet Edge of Stain


Why can't I hire more Metsuke in Shogun 2 Total War?

Question: In my single-player, Hard campaign game - I've found that Metsuke (the secret police of Shogun 2) agents are useful to boost income and catch enemy ninja. What confuses me is why I can't hire any more - the tool-tip indicates that it is based on the number of market-chain buildings I have, and I am sure I have more markets than Metsuke.
Also, it is not clear whether an upgrade to a market counts as an extra 'market-chain' building - I am guessing that it doesn't.
Comment: From browsing forums, it seem there is a hard-limit of 5 agents of any type,  regardless of how many chains you have. You can never have more than 5 Metsuke, 5 Ninjas, 5 Monks, etc. http://forums.totalwar.org/vb/showthread.php?134068-Where-to-build-markets-only-in-fertile-lands/page2
Answer: Asked 2 weeks ago and if no-one has further information will accept this as the answer:
From browsing forums it seem there is a hard-limit of 5 agents of any type, regardless of how many chains you have. You can never have more than 5 Metsuke, 5 Ninjas, 5 Monks, etc. http://forums.totalwar.org/vb/showthread.php?134068-Where-to-build-markets-only-in-fertile-lands/page2


Elixir and mongodb aggregation

Question: I have setup a mongodb with one collection that has this structure:
{ "_id" : "some_id", "city" : { "name" : "City1" }}

I've started a project with elixir 1.9.2 and phoenix and I've setup {:mongodb, "~> 0.5.1"}
I've already setup in phoenix some find queries already working, so my connection with mongodb from phoenix work just fine.
Edit:
Those are queries that already works for me:
def get_trip_list() do

{:ok, conn} = Mongo.start_link(url:"mongodb://localhost:27017/myaguila")

    cursor = Mongo.find(conn, "trip", %{})

    cursor
    |> Enum.to_list()
  end

def get_total_count() do
    {:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/myaguila")
    cursor = Mongo.count(conn, "trip", %{})
    {:ok, total} = cursor
    {:total_trips, total}
  end

But now I need to build a query with aggregation to count how many times appears each city and I really can't understand how to use the mongodb pipeline from phoenix
I write a query directly in mongo's shell (I try and worked) this way:
db.collection_name.aggregate([{$group: {_id : "$city", count: {$sum: 1}}}])

and bring me those results:

{ "_id" : { "name" : "City1" }, "count" : 212 }
{ "_id" : { "name" : "City2" }, "count" : 1200 }
{ "_id" : { "name" : "City3" }, "count" : 789 }
{ "_id" : { "name" : "City4" }, "count" : 540 }
{ "_id" : { "name" : "City5" }, "count" : 333 }

So I need to translate this mongo query to elixir language.
(Don't know much about it but I see I dont use Ecto)
That's the same expected result in elixir.
This is the library's documentation
https://hexdocs.pm/mongodb/readme.html#contributing
Thanks in advance.
Answer: This is the solution:
Mongo.aggregate(pid, "trip", [%{"$group" => %{ "_id" => "$city.name", "count" => %{ "$sum" => 1}}}])
I was missing the square brackets before the first level map.
Thanks to the interested ones.
Answer: Look :)
db.cities.aggregate([
    {
        $group: {
            _id: { name: "$city.name" },
             count: { $sum: 1 }
        }
    }
])

Result in mongodb query
{ "_id" : { "name" : "City1" }, "count" : 3 }
{ "_id" : { "name" : "City2" }, "count" : 2 }

Comment: Thanks, but I need the Phoenix's sintax. Up in the question I've already have the mongo query working.
Comment: There is no such thing as "Phoenix syntax" for queries. It's "Ecto syntax" since _Phoenix_ does not implement any DB interoperation functionality on its own.
Comment: @Aleksei Matiushkin thanks for clarify, but I tnink I neither use Ecto, Im using this documentation and examples to work it https://hexdocs.pm/mongodb/readme.html#contributing
Comment: Ah; ok, then there is _mongodb adapter syntax_. Phoenix known literally nothing about DB level.

Elixir and mongodb aggregation


Pycharm: show x/y coordinates with matplotlib automatically

Question: If I plot with ipython, I automatically see the x/y coordinates when I move with the mouse over the canvas (see bottom right in screenshot):
import matplotlib.pyplot as plt
import numpy as np

my_random = np.random.random(5)
plt.plot(my_random)
plt.show()

How can the same achieved with Pycharm (my plots appear in the SciView toolwindow)?
If not: is there perhaps an easy workaround for it? (and do I have more possibilities if the plot does not appear in the SciView toolwindow?)
Comment: https://matplotlib.org/2.0.1/examples/widgets/cursor.html ??
Comment: unfortunately, this does not seem to help
Comment: Not sure what the "SciView toolwindow" is, but probably it shows the plots as png images, which do not have any interactivity, so no coordinates can be shown.
Answer: using TkAgg it works:
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt

my_random = np.random.random(5)
plt.plot(my_random)
plt.show()


Wrong text encoding when parsing json data

Question: I am curling a website and writing it to .json file; this file is input to my java code which parses it using json library and the necessary data is written back in a CSV file which i later use to store it in a database.
As you know data coming from a website can be in different formats so i make sure that i read and write in UTF-8 format, still i get wrong output.
For example, Østerriksk becomes �sterriksk.
I am doing all this in Linux. I think there is some encoding problem because this same code runs fine in Windows but not in Unix/Linux.
I am quite sure my java code is proper but i am not able to find out what I'm doing wrong.
Comment: Should i change my approach to parse the json file from shell instead of having java program?if so please guide me in that..
Answer: What kind of IDE are you using, for example this can happen if you are using Eclipse IDE, and not set your default encoding to utf-8 in properties.
Answer: You're reading the data as ISO 8859-1 but the file is actually UTF-8. I think there's an argument (or setting) to the file reader that should solve that.
Also: curl isn't going to care about the encodings. It's really something in your Java code that's wrong.
Comment: Thanks a lot for the response.All i had to do was read in ISO 8859-1 and write it in utf-8...That worked properly..


Poisson equation, spectral method and Hermitian symmetry of DFT

Question: Let's say I have 2D rectangular grid $N \times M$ with the real values values $f_{nm}$.
I would like to solve 2D Poisson equation
$$\Delta u = f \,\,\, in \,\,\, [0,A] \times [0,B].$$
with periodic boundary conditions
\begin{equation}
\begin{aligned}
u(x,0) &= u(x,B), \quad x \in [0,A]\\
u(0,y) &= u(A,y), \quad y \in [0,B]\\
u_y(x,0) &= u_y(x,B), \quad x \in [0,A]\\
u_x(0,y) &= u_x(A,y), \quad y \in [0,B].\\
\end{aligned}
\end{equation}
using spectral method along the lines of
https://math.stackexchange.com/a/2583914/224869
and
https://stackoverflow.com/questions/42878802/solve-the-poisson-equation-using-fft-with-cuda
There are still issues I don't understand fully:

There is a claim that this is equivalent to solving linear system with the same boundary conditions using five point stencil finite difference scheme for Laplacian. Is there a proof that this is true? I am talking about equivalence up to numerical error for rectangular domain not just about the fact that it solves the same equation.
In the documentation of cufft, chapter multidimensional transforms, you can see there is some "group symmetry" so that when I know the transform
$$X_k = \sum_{n=0}^{N-1} x_n e^{-2\pi i k n/N}$$
for $k \in \{0, \cdots, N-1\}$ I know it any given $k\in \mathbb{N}$. How it comes that when doing transition to the continuous case, I have to use only those $k$ from the sequence, which are closest to $0$? What would fail if I used $k \in \{0, \cdots, N-1\}$? Moreover how to treat case of even $N$, where you have this "unbalanced harmonic"? Can you suggest some source where this transition between DFT and FFT is well covered?
I would like to use real to complex transform, which is more memory efficient as it uses roughly half of the data. Namely for
$$X_{kl} = \sum_{n=0}^{N-1} \sum_{m=0}^{M-1} x_{nm} e^{-2\pi i (k n/N + l m/M)}$$
it stores only $k \in \{0, \cdots, N-1\}$ and $l \in \{0, \cdots, \lfloor M/2\rfloor\}$. This relies on the Hermitan symmetry $X_{kl} = \overline{X}_{N-k M-l}$. Will I break the symmetry when I start dividing by the terms related to the second derivative in Poisson equation? I would like to operate on that array of $k \in \{0, \cdots, N-1\}$ and $l \in \{0, \cdots, \lfloor M/2\rfloor\}$ and later use so called complex to real transform, which use that hermitian symmetry of input data. I think this is related to the treatment of unbalanced harmonics.
Is it possible to change the method to compute Poisson equation with different type of boundary conditions? How the spectral method shall be modified when I need to prescribe zero Dirichlet conditions everywhere on the boundary? Is there a chance for generalization for any combination of Dirichlet and Neumann?
Comment: What is the domain? A rectangle? In how many dimensions? The purely theoretical way of solving that PDE is separation of variables, in this rectangular case.
Comment: @GiuseppeNegro : My use case is 2D rectangular domain.
Answer: I have asked this question some time ago and in the meantime I gained some insights into the problem. Thus I will try to address the questions I had and explain the relationships between continuous problem, discrete problem and FFT algorithm. I would also like to note that I have implemented this in this CUDA program.
First of all, because of the fact that we have just $N \times M$ values of the function $f$ at the gridpoints and we would like to solve continuous equation
$$ \Delta u = f,$$
we would need the mean we understand the objects $f$ and $u$. How $f$ will be defined outside the gridpoints and in which points of the rectangle $[0,A] \times [0,B]$ we actually place gridpoints. I will be using notation, where I identify $N$, $n$ with axis $x$ and $M$, $m$ with axis $y$.
We aim to construct continuous object $f$, which coincides with $f_{nm}$ at the grid points
$$f(n\frac{A}{N}, m \frac{B}{M}) = f_{nm}, \quad n \in \{0...N-1\}, m\in \{0 ... M-1\}.$$
Let's do FFT of $f_{nm}$ to obtain $F_{kl}$, now we have that
$$f_{mn} = \sum_{k=0}^{N-1} \sum_{l=0}^{M-1} F_{kl} e^{2\pi i (k n/N + l m/M)}$$
and thus
$$f(n\frac{A}{N}, m \frac{B}{M}) = \sum_{k=0}^{N-1} \sum_{l=0}^{M-1} F_{kl} e^{2\pi i (k n/N + l m/M)}$$
can be extended to the $f$ defined on the whole domain by the following formula
$$f(x, y) = \sum_{k=0}^{N-1} \sum_{l=0}^{M-1} F_{kl} e^{2\pi i (k x/A + l y/B)}.$$
Partial answer 2:
This definition of continuous object $f$ is possible but probably not the best one. If we subscribe to this definition, the function $f$ will be real at the gridpoints, but outside of them it will be complex function in general. So we use the trick based on the following identity that holds for the discrete IFFT
$$f_{mn} = \sum_{k=0}^{N-1} \sum_{l=0}^{M-1} F_{kl} e^{2\pi i (k n/N + l m/M)}=\sum_{k=-\lfloor \frac{N - 1}{2}\rfloor }^{\lfloor \frac{N}{2}\rfloor} \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor} F_{kl} e^{2\pi i (k n/N + l m/M)},$$
where we identify $F_{kl} = F_{k+M l+N}$. The identity holds because $e^{2\pi i} = e^{0} = 1$. For us however is more convenient to use the second sum in the identity to define continuous object $f$. When $N$ and $M$ will be odd, then the function
$$f(x, y) = \sum_{k=-\lfloor \frac{N - 1}{2}\rfloor }^{\lfloor \frac{N}{2}\rfloor} \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor} F_{kl} e^{2\pi i (k x/A + l y/B)}$$
will also be purely real and can also be represented as a expansion by means of trigonometric functions. This interpolation of our grid by the sum of trigonometric functions is actually the object we would like to use to solve our differential equation. Analogously we define the function $u$
$$u(x, y) = \sum_{k=-\lfloor \frac{N - 1}{2}\rfloor }^{\lfloor \frac{N}{2}\rfloor} \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor}  U_{kl} e^{2\pi i (k x/A + l y/B)},$$
and therefore its Laplace will have the form
$$\Delta u(x, y) = -(2 \pi)^2\sum_{k=-\lfloor \frac{N - 1}{2}\rfloor }^{\lfloor \frac{N}{2}\rfloor} \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor}  \left(\left(\frac{k}{A}\right)^2+\left(\frac{l}{B}\right)^2\right) U_{kl} e^{2\pi i (k x/A + l y/B)}.$$
From the theory about Poisson's equation with periodic boundary conditions we know that $f$ and $u$ have to have the zero mean. This can be achieved only by first ensuring that the input data $f_{nm}$ have zero mean, second by fixing $U_{00}=F_{00}=0$.
Partial answer 1: Note what is now our discrete problem. It is to find a values of $u_{mn}$ on the gridpoints such that the Laplace operator on the $u$ defined as the "Fourier extension" the gridpoint values will coincide with the "Fourier extension" of the object $f_{mn}$. We don't discretize the differential operator on the grid. We rather construct Fourier or trigonometric extension of the functions involved and understand the derivatives in the point sense at the gridpoints as the derivatives of interpolating functions $f$ and $u$. From the restriction of the Poisson equation to the gridpoints we know, that we can find $u_{nm}$ by IFFT of $U_{kl}$, where
$$F_{kl} =  -(2 \pi)^2  \left(\left(\frac{k}{A}\right)^2+\left(\frac{l}{B}\right)^2\right) U_{kl}.$$
From how we constructed $f$ and $u$ it is evident that the derivatives are understood as a point derivatives at the gridpoints of the interpolations by trigonometric functions. Thus they will not coincide with any finite difference scheme. The referenced article rather states that "when the problem is discretized by the finite difference (FD) method with a five-point stencil on an (n + 1) × (n + 1) equispaced grid, there is a FFT-based algorithm that computes the values of the solution... " I am not aware of such FFT-based algorithm, the referenced book has to be paid for, but for the algorithm I just have outlined the five-point stencil won't coincide with aforementioned definition of Laplacian on trigonometric extension. It does not exclude that it probably could be shown that these two definitions are in some sense close, e.g. by Taylor expansion.
Partial answer 2 - continue: There is also one more argument, that $k$ and $l$ up to $N-1$ and $M-1$ is not a good idea other that $f$ will be complex. The higher values of $|k|$ are there, the higher frequencies will be present in my interpolation of $f$. And the higher differences might be between point derivatives of $f$ and five-point stencil or whatever reasonable discretization of Laplace operator on my grid.
Partial answer 3: We have now the method how to solve this discrete problem by finding functions $f$, $u$ with the support on $[0,A] \times [0,B] $ with given values of $f$ on the gridpoints so that these two functions satisfy Poisson equation. It can be done for any $o_k \in \mathbb{N}$, $o_l \in \mathbb{N}$ and frequencies $k \in \{ o_k ... N - 1 - o_k\}$,  $l \in \{ o_l ... M - 1 - o_l\}$. We have seen that by setting $o_k=-\lfloor \frac{M-1}{2} \rfloor$ and $o_l=-\lfloor \frac{N-1}{2} \rfloor$ we obtain real $f$ in odd case. In even case however, there will still be the term correspondig to $k=N/2$
$$ \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor} F_{(N/2)l} e^{2\pi i (N/2 x/A + l y/B)}$$
which needs to be symmetrized to obtain real function $f$ in the way
$$  \frac{1}{2} \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor} F_{(-N/2)l} e^{2\pi i (-N/2 x/A + l y/B)} + \frac{1}{2} \sum_{l=-\lfloor\frac{M - 1}{2}\rfloor }^{\lfloor \frac{M}{2}\rfloor} F_{(N/2)l} e^{2\pi i (N/2 x/A + l y/B)}$$
In fact it would need to be symmetrized in $l$ as well. By doing so we have found real $f$ even in unbalanced case. And if moreover we use $k$ and $l$ that are centered around $0$ from the formula
$$F_{kl} =  -(2 \pi)^2  \left(\left(\frac{k}{A}\right)^2+\left(\frac{l}{B}\right)^2\right) U_{kl}$$
we also see that $U_{-kl}=\bar{U}_{kl}$ because of $F_{-kl}=\bar{F}_{kl}$ and because of the choice of frequencies I does not break hermitan symmetry.
Partial answer 4:
There is a theory regarding coercive operators, that for the problem with Dirichlet boundary conditions on the part of the boundary there is unique weak solution to the Poisson equation, for sources see this answer. It is natural to ask if we can use spectral methods to solve Poisson equation on square with Neumann or Dirichlet boundary conditions instead of periodic boundary conditions. I am not sure about general answer yet. It seems however that the simplicity of the spectral method stems from the fact, that functions in the Fourier decompositions are eigenfunctions of Laplace operator with periodic boundary conditions. Whenever we lose this, the method gets more ugly.
It is apparently possible to use something called odd or even extension to solve problem with homogeneous Dirichlet or homogeneous Neumann. Key observation here is that any sine function in FFT on $[0,2A]$ is zero at $0$, $A$ and $2A$ and any cosine function in FFT on $[0,2A]$ will have zero derivative at $0$, $A$ and $2A$. And if we construct odd function, it will have zero even components in Fourier transform and vice versa.
So let's say we would like to have homogeneous Dirichlet and we have
$$f(x,y), x \in [0,A], y\in [0, B]$$ and we extend it to $[0,2A] \times [0, 2B]$ such that
$$f(x,y) = \begin{cases}
f(x,y), & x \in [0,A], y \in [0,B],\\
-f(2A-x,y), & x \in [A,2A], y \in [0,B],\\
-f(x,2A-y), & x \in [0,A], y \in [B,2B],\\
f(2A-x,2A-y) & x \in [A,2A], y \in [B,2B].
\end{cases}$$
Now we solve the original problem $\Delta u = f$ with periodic boundary conditions on bigger domain $[0,2A] \times [0, 2B]$. The restriction of the solution to the $[0,A] \times [0, B]$ will be solution to the original problem with Dirichlet boundary conditions. See also Figure 3 of this paper. By solving four times bigger problem, we managed to use this type of spectral method to solve the following boundary value problems
\begin{equation}
\begin{aligned}
u(x,0) &= u(x,B) = 0, \quad x \in [0,A],\\
u(0,y) &= u(A,y)=0,  \quad y \in [0,B].
\end{aligned}
\end{equation}
or
\begin{equation}
\begin{aligned}
u(x,0) &= u(x,B) = 0, \quad x \in [0,A],\\
u_x(0,y) &= u_x(A,y) = 0, \quad y \in [0,B].\\
\end{aligned}
\end{equation}
or
\begin{equation}
\begin{aligned}
u_y(x,0) &= u_y(x,B)=0, \quad x \in [0,A],\\
u_x(0,y) &= u_x(A,y)=0, \quad y \in [0,B].\\
\end{aligned}
\end{equation}
Of course that raises further question if there is a way to extend the set of problems even further. And if if it is necessary to solve four times bigger problem to have solution with some kind of normal boundary conditions or if we can computationally exploit the symmetry of the problem.
Apparently when homogeneous Dirichlet conditions are imposed and I need other type of Dirichlet conditions, it is sufficient to compute Laplace equation solution on the square with those Dirichlet conditions and sum the two solutions. This way the method can be generalized for any type of Dirichlet boundary conditions.
Note also when doing the odd or even extension we have to pay attention how FFT and IFFT of sequence [0,n-1] is defined with respect to the domain [0,A]. The last point $n-1$ is mapped not to A but to $A-A/N$ so if we do naïve extension by simple mirroring we obtain lot of aliasing between FFT and FT frequencies. From the perspective of keeping radix-2 algorithm it might be much faster than using different than $2N$ grid.


Behat disable responsive features

Question: I'm currently a little stuck with behat + responsive features that appear / disappear at given break-points on the screen.
The first thing  would be to create tags to filter features for devices.
Like
@iphone or @mobile and so on.
This gets really messy if you are testing on multiple devices with multiple-screen-sizes / orientations.
We then would end up with:
@iphone5c_hor
@iphone5c_vert
@iphone5s_hor
@iphone5s_vert
@samsung..._vert
My second attempt was to tag features with a min-width tag e.g.  min-width:700 and to use the feature-context and the webdriver to determine if the current browser I'm on supports the feature I'm trying to execute.
This works up until this point:
<code>/**
 * @BeforeFeature
 *
 * @param \Behat\Behat\Hook\Scope\BeforeFeatureScope|BeforeScenarioScope $scope
 */
public function prepareForTheScenario(BeforeScenarioScope $scope)
{
 if (!$this->checkWindowWidth($scope)) {
 // TODO: skip scenario here
 }
}
</code>
Has anybody had any experience with these kind of dynamic tests, am I going down a completely wrong way, and If not how can I make that work?
Answer: I solved it myself by creating a Behat-Extension that supports my needs. (https://github.com/2bepublished/BehatResponsiveFeaturesExtension)
Answer: I had similar issues and had to set browser window size to get rid of my problem. I wonder if this helps you (check it please).
What I did was:
Just set the window size to something bigger so that the menu doesn't get squahed down when testing. You can change the screen size from 1024X768 to something else as you wish.
<code>#symfony/src/Site/CommonBundle/Features/Context/FeatureContext.php

/**
 * @BeforeStep
 */
public function beforeStep()
{
    $this->getSession()->resizeWindow(1024, 768, 'current');
}
</code>
Comment: Hi, sorry this is not an option for me as we have atleast 20 different versions of the interface based on the ui and the goal is to test them - so we can be sure that the interface works on the devices.
Comment: I case you need this featue have a look at this extension: https://github.com/2bepublished/BehatResponsiveFeaturesExtension

Behat disable responsive features


Swipe gesture vs prev/next buttons

Question: Let's say you are navigating through an image carousel and you're on a touch device.
Which navigation method do you prefer: prev/next buttons or swipe-left/right gesture?
I'm trying to convince my boss that in such cases a touch-detection library may not be necessary and navigation buttons require just a finger tap, but he insists that "a user wants to swipe."
Comment: Is it safe to assume by "touch device" you're referring to a modern smartphone or tablet (as opposed to a touch-screen kiosk)?
Comment: Yes, I'm referring to a typical smartphone or tablet.
Answer: An important factor to consider is the audience of the application (or website). It seems that typically, many younger audiences (or those familiar with touch devices) are used to swiping across carousels. If you look core applications of iOS (and I'm assuming Android as well), you'll find that carousels support swiping to switch to the next image. However, older audiences might be more accustomed to click events which hearken back to a very mouse oriented approach of user experiences.
If you're considering programming for touch, I would think it would be a good idea to at least support swiping (I would offer both methods so you hit both the desktop base as well as the touch/tablet). Consider what the hand is already doing on a mobile device while navigating a page. The user is already used to swiping up or down to view content vertically, that is quite ingrained into everything we do. Now we have a situation where we have content "scrolling" horizontally. Typically, a natural response would be to swipe the content across the screen, just as if you had multiple pages of paper on your desk, and you wanted to slide one across the desk.
This brings up a topic of User Interface design that is making a lot of headway in my opinion. We are seeing devices try to work like how we work with real objects. Again, think about scrolling on a tablet, you touch a part of the page and drag up (which would push content up, just like you would push paper upwards). Apple has moved in this direction with their laptops as well with something called Natural Scrolling (it took a bit of getting used to, but it makes sense if you think about it).
So, to sum up. If you're asking which I prefer, I would prefer the swipe capability, but both options should be present. Depending on which library you're going to use and how careful you are on storage space, touch detection shouldn't add a huge difference in application performance. And having both features present (both click and swipe) would reach to a broader audience and satisfy both sides.
Comment: However, this debate is a tangent off the original intent of the post. If you would like to post a question to debate the use of touch interfaces and the age demographic of those users, that would be appropriate.  The original intent of this answer was to state that with the varying experiences of UI, it would be a wise decision to include both types of scrolling patterns to reach a broader audience.
Comment: Why is it typical for "younger" audiences to understand that they can use a swipe gesture? You are perpetuating the myth that age = technical usage/understanding. May want to check this out: http://www.nngroup.com/articles/college-students-on-the-web/
Comment: Read the article, but I find that it doesn't go against what I have posted. It's probably safe to say that there are more young people (younger than 28) who use new technology (such as iPads or Galaxy Tabs) than older. So generally, the younger generation is more familiar with new user interfaces (such as swiping and carousels). As the article said, users skip over cumbersome actions. To someone who is generally familiar with swiping, finding and clicking a small button is more difficult than a simple swipe. **This doesn't perpetuate any myth, but points out a general observation on UX use.**
Comment: You know that the largest tablet demographic skews older, right? http://www.marketingcharts.com/wp/online/us-tablet-owner-demographics-as-of-september-2013-37557/attachment/pew-us-tablet-adoption-rates-in-sep-2013-oct2013/
Comment: Also, it is dangerous to assume that a younger generation is more familiar with new user interfaces. Just as the previous article stated, younger users (i.e., college aged) are frustrated when faced with new design patterns.
Comment: That is exactly the point I'm trying to make. The younger generation has seen swiping patterns and (I think) are already familiar with said design. That being said, a different approach for them might be to have to click buttons. "New" is relative to the user. And your demographic supports my point that *typically* older individuals don't use as much technology. If you look at the age brackets, you will find that they are not spread evenly (1 year, 11 years, 19 years, 14 years, +).
Answer: There's a lot to be said for the swipe idiom over buttons.  It might seem like the physical action of a tap is quicker and easier than a swipe, but that assumption ignores Fitts's Law, or its touchscreen equivalent: the smaller a target is, the longer it will take for the user to hit it.  You can do a swipe without looking, but to hit a button you have to mentally and physically position your finger, and this takes time and interrupts the flow of concentration.
Swiping is also a much richer interaction.  The user can discover it by accident, and if they start a swipe without meaning to they can easily cancel it by reversing the action.  Typically they can also "peek" at the next and previous items, even while looking at the current item.  And of course, it uses no screen real estate at all.
However, if your users can't be relied on to know about swiping, you have to provide an alternative-- if even 1% of your users can't use the app at all, that's a failure.
Swiping might also cause accessibility problems.  The action takes less motor control to perform, but also requires larger hand movements-- I wouldn't want to give detailed advice on this.
If you are aiming for a demographically broad audience, the latter two points suggest including both techniques, as Andrew suggests.  But if you can justify it, I'd say go with swiping alone; it's a more elegant solution and you can use 100% of the view for content.
Comment: If the action is always on the same position - as it is for the carousel - Fit's Law only applies for the first action.

Swipe gesture vs prev/next buttons


Deleting a value from an array in C++

Question: Hi I am trying to do something like this:
Say at first iteration I have
  A={2,3,5}

and then in the second iteration it becomes
  A={2,3}

and so on. I tried making the third element zero after my first iteration but then the algorithm is going wrong.
What is the way in C++ to delete an element in an array as we perform each iteration?
Comment: Why don't you use a `vector`?
Comment: For the best answer, we need to know what your algorithm does.
Comment: What is an array? If it is the values between 2 pointers `first` and `end`, then removing the last element is just `--end`.
Comment: It is a circuit partitioning algorithm. No the value which is being deleted is not always at the back. How is vector useful? Or do we need to use linked lists? Pl.s let me know. Thanks
Answer: The best (and most C++ - like) approach is to switch from an array to a resizable container, such as std::vector. This container is resized dynamically, letting you delete elements from the end or from any other place in the container. The container would then resize to the correct size.
// Create a vector
std::vector<int> mydata(10, -1);
for (int i = 0 ; i != 10 ; i++)
    mydata[i] = i;
// Process all elements
for (int i = 0 ; i != mydata.size() ; i++) {
    cout << mydata[i] << endl;
}
// Remove the last element
mydata.pop_back();
// Process all elements - now the last one is not there
for (int i = 0 ; i != mydata.size() ; i++) {
    cout << mydata[i] << endl;
}

Comment: `mydata.erase(mydata.end()-1)` is verbose. Why not use `mydata.pop_back()`?
Comment: Here is a [link to a demo on ideone](http://ideone.com/48KkE1).
Comment: deque may be better if removing many elements but +1 good answer.
Comment: @Rapptz You're right, I fixed the answer.
Answer: You can't delete elements in regular array in C++. If you are using List or vector then it is other thing, but with plain arrays you can only create new array, and copy values from old array without value which you want to "delete". And then say "new array" is "old array".
Answer: For C++ style: use std:vector and following that this.
For C style: use dynamic heap arrays i.e. new/delete/realloc
Answer: Picture an array as metal structure with slots.  The slots cannot be changed, they are fixed.
Fill the slots with books. This is like putting values in an array.
Remove the 3rd book from the 3rd slot.  Because it is a metal structure with fixed slots, none of the other books are adjusted or moved.  They are in their original positions.
To "remove" the empty slot, pull a book from the next slot and place into the empty slot.  Repeat until the "empty" slot is at the end or all the remaining books have been moved. This is how a std::vector will remove an item.
A linked list is more efficient at removing slots.  Think of a linked list as a bunch of containers that have a hook one one end and as string on the other.  You can tie the string only to one container's hook.  Following the string gives you the order of the containers.  To remove a container, untie the previous container's string and tie it to the hook of the next container, then untie the container's string.  You didn't have to copy or move any of the remaining elements; you just moved the string.
Unless you keep track of the removed container, it may get lost.  This situation is called a memory leak:  a lost item in memory.

Deleting a value from an array in C++


Run directly from the meter to house, no secondary panel?

Question: I want to run directly from the meter to house, no secondary panel:
Would the following portion of this answer here apply to my situation?

To feed a 100 ampere secondary panel 200 ft. from the main panel, you'll want to use 1 AWG copper or 2/0 aluminum conductors. This is large enough to safely carry 100 amperes, and to maintain power quality over the distance.
NOTES:
You'll want to feed the panel using 4 conductors (hot, hot, neutral, ground).
You'll have to isolate the grounded (neutral) conductors and bus bar in the secondary panel.
You'll have to install an adequate grounding electrode system at the outbuilding, and bond the grounding conductor to it.
Comment: no idea ... you did not say how far is the meter from the house
Comment: It's a solid answer, but is it the same question?  Not necessarily.  Now if you have a meter-main combination, with overcurrent protection and the first means of disconnect at the meter, the panel at the house really is a subpanel, and it does apply
Comment: Would this info help?  The electric company just ran new service from the street about 150ft to the pole on our property and installed a new meter on the pole. Now we are running service from that meter 160ft underground to the house.
Answer: Per the 2020 code (230.85), you are now REQUIRED to have a disconnect outside of the house at the meter.
Comment: Thanks for all the answers.


How do I use Quicktype Json C# generated classes to Deserialize a JSON file

Question: I have a very large Json file (cannot share)
Im using https://quicktype.io/csharp/ to generate a C# class from this json file
It gives me a class that looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Tem
    {
        [JsonProperty("Cal")]
        public Cal Cal_ { get; set; }
    }

    public partial class Cal
    {
        [JsonProperty("Cam")]
        public Camera[] Cam_ { get; set; }

        [JsonProperty("Dis")]
        public Dis[] Dis_ { get; set; }

        [JsonProperty("Eye")]
        public Eye[] Eye_ { get; set; }

        [JsonProperty("Ine")]
        public Ine[] Ine_ { get; set; }

        [JsonProperty("Metadata")]
        public Metadata Metadata_ { get; set; }

        [JsonProperty("Tem")]
        public Tem Tem_ { get; set; }
    }
    //...There are a lot more
}

Then it has some built internal classes
internal static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters =
        {
            ShutterConverter.Singleton,
            AssignedEyeConverter.Singleton,
            LocationConverter.Singleton,
            new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
        },
    };
}

Not sure what to do with these
I tried to deserialize it this way but it didn't work, all the values were null
 using (StreamReader r = new StreamReader(file path))
 {
         string json = r.ReadToEnd();

         QuickType.Cal test = Newtonsoft.Json.JsonConvert.DeserializeObject<QuickType.Cal>(json);

 }

I know that the file path is correct and the json string contains the json file. But I dont know how Im supposed to convert from Json.net to this Json generated class
Thanks,
Comment: Does Json2csharp.com work?
Comment: That didn't help. Im still getting Null for all the data in the class
Answer: As suggested above by Jeremy Thompson I switched to Json2csharp.com because the formatting is much simpler
Then I parse with Cal.RootObject test = Newtonsoft.Json.JsonConvert.DeserializeObject<Cal.RootObject>(json);


DNS Routing Fails for VPN Connections on Ubuntu 16.04 Out-of-the-Box

Question: I would like to report some odd behavior, and my solution. I am left extremely unsatisfied as it relies on custom scripts and non-standard packages. I would like the community's help in figuring out a better fix for this.

Problem
I heavily rely on OpenVPN for all of my servers. After I upgrading to Ubuntu 16.04.1 from 14.04.5, I stopped being able to access any websites and most services.
My OpenVPN servers continued to tunnel traffic just fine for Mac OS X, Windows, and Ubuntu 12.04/14.04 clients.
System Details
My out-of-the-box resolv.conf file looked like:
nameserver 188.8.131.52
nameserver 184.108.40.206

My routes indicated that I was routing through a router at 192.168.0.1 and was assigned the dynamic IP address of 192.168.0.3. Shown by the commands:
ip r
ip a

My traffic would function normally (internet, XMCP, IRC, etc...) until I connected to my OpenVPN Server via the terminal with the command
sudo openvpn ~/client.ovpn

Once connected, it will immediately stop all traffic.
A new adapter (tun0) was created for this connection. Shown by the command:
ip a

Connecting with Wireshark revealed dozens of DNS calls aimed at my original DNS servers, even though the configuration was set to tunnel all traffic through my OpenVPN servers.
My Solution
After a full week of reading many dozens of bug-reports, blog posts, and tutorials, I still had no success. But, this is a DNS issue and DNS is managed by resolv.
Finally, I stumbled upon this GitHub library and my problem was resolved.
masterkorp/openvpn-update-resolv-conf
To implement these scripts, I cloned the repo into my home directory with:
git clone https://github.com/masterkorp/openvpn-update-resolv-conf.git

I copied those scripts to /etc/openvpn with:
sudo chmod +x *.sh && sudo mv *.sh /etc/openvpn
Next, I installed openresolv, nscd, and unbound with:
sudo apt-get install openresolv nscd unbound

Then, I edited my OpenVPN Client Configuration file (*.ovpn or *.conf) and appended the following lines at the end of the configuration directives, but before the "ca" tag (your inline certs, if any):
script-security 2
up "/etc/openvpn/update-resolv-conf.sh /etc/openvpn/update-systemd-network.sh"
down "/etc/openvpn/update-resolv-conf.sh /etc/openvpn/update-systemd-network.sh"

This allowed me to connect successfully to the VPN and tunnel my traffic.
My resolv.conf file now looks like:
# Generated by resolvconf
nameserver 127.0.0.1
nameserver 127.0.1.1

Discussion and Need for Better Solution
It appears to me that Ubuntu's DNS records are not being properly updated. The purpose of the above code and the new entries is to rewrite a malfunctioning update process of the internal DNS server records. I have read some bug reports on Launchpad that suggest that 16.04 relies on some old network management update code that was written and working in 14.04, and was never properly migrated to the new 16.04 framework.
There must be a better way of handling this process. I don't like the idea that I need to use custom scripts to patch problematic OS behavior.
Anyone have any insight in this?
Comment: Yep. That's the push "dhcp-option DNS X.X.X.X". The output that was printed after running the "sudo openvpn --config [config file]" command indicated that command was getting pushed to the client. That did not cause Ubuntu to update it's DNS though.
Comment: Thought there was an option on openvpn server to push new dns server with IPs etc... to clients
Comment: @ok123jump the solution part should be moved into an answer to this question. :)
Answer: Set your DNS servers in /etc/resolv.conf  then try this in the terminal as root:
chattr +i /etc/resolv.conf
then reboot
Comment: That would lock the resolv.conf file with that set of DNS nameservers until you unlocked it and changed them. One may not want to be connected to the VPN all the time. This would require a reboot every time you wanted to get off your VPN and back to your regular traffic - or another VPN.


Defining a function (high interrupt) within or outside a macro C

Question: For starter, Im using the C18 compiler for my PIC. When I create the function like high_ISR without any calls in it, my compiler adds code to it, code I can only see in a Disassembly listing view. This makeing my high_ISR function have a cycle time of say 160 cycles (but it appears to be empty looking at the C code).
Instead if I define it as a macro, I will have it empty of "secret" code. Will I somehow have a problem with other functions cause removing code that my compiler secretly puts there, will it cope with that in other ways?
I mean, I want the ISRs to be fast.
Is there a way to make the ISR function fast when writing the function in a c file, could I somehow supress the "secret code".
See code examples below:
Proper creation of my high_ISR function in a .c file but with a long cycle time
#pragma interrupt highISR
void highISR(void)
{
    SomeFunction();
}

Macro created function for instance in my header file (say cycle time of 16 cycles)
#define FAST_INTERRUPT           \
    void highISR(void)           \
    {                            \
        SomeFunction();          \
    }

Just for clarification, If I use the macro, I implement it as:
#pragma interrupt higISR
    FAST_INTERRUPT

The First highISR declaration produces this:
    #pragma interrupt highISR
323: void highISR(void)
 12B12    CFDA     MOVFF 0xfda, 0xfe4
 12B14    FFE4     NOP
 12B16    CFE2     MOVFF 0xfe2, 0xfda
 12B18    FFDA     NOP
 12B1A    CFE9     MOVFF 0xfe9, 0xfe4
 12B1C    FFE4     NOP
 12B1E    CFEA     MOVFF 0xfea, 0xfe4
 12B20    FFE4     NOP
 12B22    CFD9     MOVFF 0xfd9, 0xfe4
 12B24    FFE4     NOP
 12B26    CFDA     MOVFF 0xfda, 0xfe4
 12B28    FFE4     NOP
 12B2A    C1FB     MOVFF 0x1fb, 0xfe4
 12B2C    FFE4     NOP
 12B2E    C1FC     MOVFF 0x1fc, 0xfe4
 12B30    FFE4     NOP
 12B32    C1FD     MOVFF 0x1fd, 0xfe4
 12B34    FFE4     NOP
 12B36    C1FE     MOVFF 0x1fe, 0xfe4
 12B38    FFE4     NOP
 12B3A    CFF6     MOVFF 0xff6, 0xfe4
 12B3C    FFE4     NOP
 12B3E    CFF7     MOVFF 0xff7, 0xfe4
 12B40    FFE4     NOP
 12B42    CFF8     MOVFF 0xff8, 0xfe4
 12B44    FFE4     NOP
 12B46    CFF5     MOVFF 0xff5, 0xfe4
 12B48    FFE4     NOP
 12B4A    CFF3     MOVFF 0xff3, 0xfe4
 12B4C    FFE4     NOP
 12B4E    CFF4     MOVFF 0xff4, 0xfe4
 12B50    FFE4     NOP
 12B52    CFFA     MOVFF 0xffa, 0xfe4
 12B54    FFE4     NOP
 12B56    CFFB     MOVFF 0xffb, 0xfe4
 12B58    FFE4     NOP
 12B5A    EE05     LFSR 0, 0x500
 12B5C    F000     NOP
 12B5E    0E12     MOVLW 0x12
 12B60    04E8     DECF 0xfe8, W, ACCESS
 12B62    E303     BNC 0x12b6a
 12B64    CFEE     MOVFF 0xfee, 0xfe4
 12B66    FFE4     NOP
 12B68    D7FB     BRA 0x12b60
 12B6A    52E6     MOVF 0xfe6, F, ACCESS
324:                    {
325:                        SomeFunction();
 12B6C    EC48     CALL 0x14090, 0
 12B6E    F0A0     NOP
327:                    }
 12B70    52E5     MOVF 0xfe5, F, ACCESS
 12B72    EE05     LFSR 0, 0x511
 12B74    F011     NOP
 12B76    0E12     MOVLW 0x12
 12B78    04E8     DECF 0xfe8, W, ACCESS
 12B7A    E303     BNC 0x12b82
 12B7C    CFE5     MOVFF 0xfe5, 0xfed
 12B7E    FFED     NOP
 12B80    D7FB     BRA 0x12b78
 12B82    CFE5     MOVFF 0xfe5, 0xffb
 12B84    FFFB     NOP
 12B86    CFE5     MOVFF 0xfe5, 0xffa
 12B88    FFFA     NOP
 12B8A    CFE5     MOVFF 0xfe5, 0xff4
 12B8C    FFF4     NOP
 12B8E    CFE5     MOVFF 0xfe5, 0xff3
 12B90    FFF3     NOP
 12B92    CFE5     MOVFF 0xfe5, 0xff5
 12B94    FFF5     NOP
 12B96    CFE5     MOVFF 0xfe5, 0xff8
 12B98    FFF8     NOP
 12B9A    CFE5     MOVFF 0xfe5, 0xff7
 12B9C    FFF7     NOP
 12B9E    CFE5     MOVFF 0xfe5, 0xff6
 12BA0    FFF6     NOP
 12BA2    CFE5     MOVFF 0xfe5, 0x1fe
 12BA4    F1FE     NOP
 12BA6    CFE5     MOVFF 0xfe5, 0x1fd
 12BA8    F1FD     NOP
 12BAA    CFE5     MOVFF 0xfe5, 0x1fc
 12BAC    F1FC     NOP
 12BAE    CFE5     MOVFF 0xfe5, 0x1fb
 12BB0    F1FB     NOP
 12BB2    CFE5     MOVFF 0xfe5, 0xfda
 12BB4    FFDA     NOP
 12BB6    CFE5     MOVFF 0xfe5, 0xfd9
 12BB8    FFD9     NOP
 12BBA    CFE5     MOVFF 0xfe5, 0xfea
 12BBC    FFEA     NOP
 12BBE    CFE5     MOVFF 0xfe5, 0xfe9
 12BC0    FFE9     NOP
 12BC2    CFE5     MOVFF 0xfe5, 0xfda
 12BC4    FFDA     NOP
 12BC6    0011     RETFIE 0x1

The Second Function only produce this ->
322:      #pragma interrupt highISR
323:          FAST_INTERRUPT
 13742    CFDA     MOVFF 0xfda, 0xfe4
 13744    FFE4     NOP
 13746    CFE2     MOVFF 0xfe2, 0xfda
 13748    FFDA     NOP
 1374A    52E6     MOVF 0xfe6, F, ACCESS
 1374C    0105     MOVLB 0x5
 1374E    5140     MOVF 0x40, W, BANKED
 13750    1A77     XORWF 0xf77, F, ACCESS
 13752    96A4     BCF 0xfa4, 0x3, ACCESS
 13754    2877     INCF 0xf77, W, ACCESS
 13756    6078     CPFSLT 0xf78, ACCESS
 13758    86A4     BSF 0xfa4, 0x3, ACCESS
 1375A    7E8B     BTG 0xf8b, 0x7, ACCESS
 1375C    0011     RETFIE 0x1
 1375E    52E5     MOVF 0xfe5, F, ACCESS
 13760    CFE5     MOVFF 0xfe5, 0xfda
 13762    FFDA     NOP
 13764    0011     RETFIE 0x1

Further more clarification just to show that the functions them self is written the same:
For the first rutine ->
void SomeFunction(void)
{
    if(PIR3bits.TMR4IF)
    {
        PIR3bits.TMR4IF = 0;

        PR4 ^= _SoftPWM_Toggle;

        _asm
            INCF PR4,0,ACCESS
            CPFSLT TMR4,ACCESS
        _endasm

            PIR3bits.TMR4IF = 1;

        SOFT_PWM_PIN ^= 1;
    }
}

The Second one in header as a macro ->
#define FAST_INTERRUPT \
    void highISR(void) \
    { \
        PR4 ^= _SoftPWM_Toggle; \
        \
        PIR3bits.TMR4IF = 0; \
        \
        _asm \
            INCF PR4,0,ACCESS \
            CPFSLT TMR4,ACCESS \
        _endasm \
        \
            PIR3bits.TMR4IF = 1; \
        \
        SOFT_PWM_PIN ^= 1; \
        \
        _asm \
            RETFIE 1 \
        _endasm \
    }

Comment: WHat makes you think that defining the ISR within a macro will make any difference at all ? Ultimately you'll get the same code. Note that you have left out `#pragma interrupt highISR` in the macro case, for obvious reasons.
Comment: Yes, but your second "macro" version does not have `#pragma interrupt highISR` so you're probably not saving/restoring context around the ISR correctly in that case - that's probably the missing "secret code" that you're getting hung up about.
Comment: Please post the disassembled code that your compiler added -- it might help in deciding how important it is.
Comment: When Im debugging my code within the ISR function, in the first case the compiler put some extra lines of code there not visable until I disassembly, thous making my ISR taking a longer time executing. In the second case when Dissassembly the code the compiler is forced not to put any "secret code" right there, thous speeding up my ISR function.
Comment: It does, sorry for being unclear about that.
Comment: OK - now you have `void interrupt` in the macro case but just `void` in the original case - you need to compare "like with like"
Comment: Im so sorry for confusing you. The "void interrupt" where never ment to be, the #pragma defines the interrupt.
Comment: So... just did Justin, as u can see.. doing the function not as a macro gives me slower code and the compiler put garbage in my rutine.
Answer: I don't think its garbage in your runtime. Remember that an interrupt can occur anywhere in your main line code. When you return from the interrupt, you would expect your code to pick up in the same state where it left off. What the compiler is adding is code to save that state at the start of the ISR and then restore the state at the end of the interrupt. If it does not do that, and you modify a register or memory location in the ISR that the main line code was using, then odds are good that the code is not going to execute as exepected when the ISR returns. By elminating the compiler generated code, you have turned over the resposibility to your ISR to save, and then restore, the state of the mainline code. If you can gurantee that you will be ok (an empty ISR would be an example, it never changes the sate). If you can not, weird code execution is going to happen when you return to the main line code from the ISR. If you are using a high level compiled language, its hard to know what it is using under the covers, so having the compiler generate the state saving code for you ISR is safer than trying to do it on your own.
I suggest you take a look in the MPLAB® C18 C Compiler User's Guide docs. There are aditional parameters to the #pragma interupt directive that allow you to somewhat customize what state information gets saved.


How do I make mud?

Question: So I have been trying to make mud, but it just isn't working.  Could someone give me a quick guide to making mud?
ps: I noticed there is an existing question about farming with buckets, but it doesn't quite answer my question.
Comment: out of curiosity what was the problem with the farming question?
Comment: I'm not entirely sure.  It didn't quite make sense/work for me.  Of course, I tried it the day I started with DF, so there may just be a learning component.
Answer: Ok, so mud is created when water is placed on ground.  Technically any water on almost any ground will create mud, but that usually isn't sufficient.  Normally to use the mud, you need to either drain the water (or wait for it to evaporate) down to 1/7.
There are a couple ways you can create mud:

Flooding
Rain
Bucket Brigade

Flooding is largely straight forward.  Create a channel near the water source.  Make a Gate attached to a lever.  Open gate to allow the flow of water.  Close gate and allow water to flow out (or evaporate).
Rain is also fairly straight forward.  In Temperate climates or better regular rainfall will turn most areas into mud.  Simply dig a hole open to air, wait for the rain, and then construct a floor on top to prevent further flooding.
Bucket brigade was discussed in a previous question but I'll repost the answer here:

Dig a channel (for the moment let's assume 3x3).
Leave at least 1 ramp (your dwarfs need a place to dump the water from)
Create a Zone above the 3x3 area (since this is a channel this would be on the level you channeled from)
Size the Zone appropriately
Select Zone type Pit/Pond
Change from Pit (the default) to Pond using the capital P and then pressing f
Create a Zone above a source of water
Change the Zone Type to Water Source
Build buckets (you can do this at a carpenter shop).
Make sure you have dwarves with hauling professions

Your dwarves will dump the water into the pond at the ramp.  It is key to have a ramp or this won't work.  Water will slowly fill your 3x3 area until all tiles are at 1/7.  When any tile is at 1/7th you'll notice mud is created.
You can see the original bucket brigade answer for more information
Comment: I have tried the bucket farm method, and the flooding method, and I feel that flooding is much easier and faster for creating farm swaths of almost any size. Just be careful to control your flood.

Also, I believe that opening a tile to the sky makes that an "outside" tile forever, which would mean you could only grow "outside" plants on that tile.  This could be an advantage or disadvantage.
Comment: I have found that breaching the small ponds into a much larger room is the easiest method for creating a farm. If the room is large enough, you don't have to worry about dealing with excess water as it will spread out to far and evaporate.
Comment: @Poindexter that's what I started doing.  The problem with that is that sometimes you completely drain out a bigger pond on the surface and you lose your water supply AND everything floods

How do I make mud?


Is there any way to send and receive SMS through my Prolink PHS300 3g modem?

Question: Is there any way to send and receive SMS through my 3g modem. I'm using prolink phs300 modem.
Answer: You could try the Gnome Phone Manager - package gnome-phone-manager
http://live.gnome.org/PhoneManager


How to integrate juju-gui into openstack horizon dashboard?

Question: I saw this video:
https://www.openstack.org/summit/tokyo-2015/videos/presentation/canonical-the-juju-app-store-for-your-openstack
Which talks about juju plug-in for openstack horizon dashboard. The author of the talk told that everything we see there can be found on line. I searched for the juju horizon plug in on the Internet but failed to find anything.
Can anybody help me integrating juju gui into an openstack horizon dashboard?
Answer: Thanks for your interest in Juju in Horizon! We demonstrated this to gauge interest and the Horizon plugin relies on code that was not completed to production grade. If you're interested please send an email to the address at the end of the presentation and folks at Canonical will get back with you and know that there's more interest for this idea.


Do we have a list of NSURL Error Code Values given by Apple somewhere?

Question: Can someone post the value for the list of possible NSURLError codes.
I am getting -1004 as the error code value, I can see based on the description it is "Could not connect to the server", but is there any list of error number and information combination of NSURL?
Answer: Following is the list I collected by printing all the NSURL Error codes in debug window.
Hope this helps someone, but it is just for quick reference, it will be always better to print the actual error description to confirm
<code>ErrorCode   Enum Value
-1      NSURLErrorUnknown
-995    NSURLErrorBackgroundSessionRequiresSharedContainer
-996    NSURLErrorBackgroundSessionInUseByAnotherProcess
-997    NSURLErrorBackgroundSessionWasDisconnected
-999    NSURLErrorCancelled
-1000   NSURLErrorBadURL
-1001   NSURLErrorTimedOut
-1002   NSURLErrorUnsupportedURL
-1003   NSURLErrorCannotFindHost
-1004   NSURLErrorCannotConnectToHost
-1005   NSURLErrorNetworkConnectionLost
-1006   NSURLErrorDNSLookupFailed
-1007   NSURLErrorHTTPTooManyRedirects
-1008   NSURLErrorResourceUnavailable
-1009   NSURLErrorNotConnectedToInternet
-1010   NSURLErrorRedirectToNonExistentLocation
-1011   NSURLErrorBadServerResponse
-1012   NSURLErrorUserCancelledAuthentication
-1013   NSURLErrorUserAuthenticationRequired
-1014   NSURLErrorZeroByteResource
-1015   NSURLErrorCannotDecodeRawData
-1016   NSURLErrorCannotDecodeContentData
-1017   NSURLErrorCannotParseResponse
-1018   NSURLErrorInternationalRoamingOff
-1019   NSURLErrorCallIsActive
-1020   NSURLErrorDataNotAllowed
-1021   NSURLErrorRequestBodyStreamExhausted
-1022   NSURLErrorAppTransportSecurityRequiresSecureConnection
-1100   NSURLErrorFileDoesNotExist
-1101   NSURLErrorFileIsDirectory
-1102   NSURLErrorNoPermissionsToReadFile
-1103   NSURLErrorDataLengthExceedsMaximum
-1104   NSURLErrorFileOutsideSafeArea
-1200   NSURLErrorSecureConnectionFailed
-1201   NSURLErrorServerCertificateHasBadDate
-1202   NSURLErrorServerCertificateUntrusted
-1203   NSURLErrorServerCertificateHasUnknownRoot
-1204   NSURLErrorServerCertificateNotYetValid
-1205   NSURLErrorClientCertificateRejected
-1206   NSURLErrorClientCertificateRequired
-2000   NSURLErrorCannotLoadFromNetwork
-3000   NSURLErrorCannotCreateFile
-3001   NSURLErrorCannotOpenFile
-3002   NSURLErrorCannotCloseFile
-3003   NSURLErrorCannotWriteToFile
-3004   NSURLErrorCannotRemoveFile
-3005   NSURLErrorCannotMoveFile
-3006   NSURLErrorDownloadDecodingFailedMidStream
-3007   NSURLErrorDownloadDecodingFailedToComplete
</code>
Comment: https://fuchsia.googlesource.com/third_party/swift-corelibs-foundation/+/upstream/fuchsia_release/Foundation/NSURLError.swift ?
Answer: In Xcode, press Cmd-Shift-O (the letter) or select File -> Open Quickly...  and start typing nsurlerror in the text field. Once you see it in the list select NSURLError.h to view all error definitions in the Xcode editor.


How do you get Legendaries back?

Question: I sold off my Lugia, Heatran, and Regigigas to get Mewtwo, Palkia and Porygon. I want to get them back without trading. Can I get them back by beating the Elite Four again?
Comment: When you say "Sold Off," do you mean traded away?
Comment: @ShadowZ. So it seems, he got 3 other Pokemon for them.
Comment: Depends. Are you willing to erase your save and start a new game?
Answer: No, if you have already caught them previously. They can re-appear only if you defeat them.

How do you get Legendaries back?


Struts 2: return to calling page

Question: I'm using Struts 2.
I'd like to return from an Action to the page which invoked it.
Say I'm in page x.jsp, I invoke Visual action to change CSS preferences in the session; I want to return to x.jsp rather than to a fixed page (i.e. home.jsp)
Here's the relevant struts.xml fragment:

<action
   name="Visual"
   class="it.___.web.actions.VisualizationAction">
   <result name="home">/pages/home.jsp</result>
</action>

Of course my VisualizationAction.execute() returns home.
Is there any "magic" constant (like, say, INPUT_PAGE) that I may return to do the trick?
Must I use a more involved method (i.e. extracting the request page and forwarding to it)?
T.I.A.
Answer: You can use  a dynamic result in struts.xml. For instance:
<action
   name="Visual"
   class="it.___.web.actions.VisualizationAction">
   <result name="next">${next}</result>
</action>

Then in your action, you create a field called next. So to invoke the action you will pass the name of the page that you want to forward to next. The action then returns "next" and struts will know which page to go to.
There is a nicer explanation on this post: Stack Overflow
Answer: return INPUT;

will do the trick. INPUT constant is defined in Action interface itself. It indicates that action needs more input.
By calling page if you meant the page that took you to the action input page, then your input page will have to store HTTP header "Referer" in the request scope for the Action.
Comment: Thanks for your kind (and prompt) reply, nikhilbelsare.
I tried return INPUT; but I got a **No result defined for action it.___.web.actions.VisualizationAction and result input** error message (pretty much expected because I did not define "input" anywhere in my struts.xml).
What I really wanted to do was Struts 2 to automagically identify the referring page and forwarding me to it, since the action is invoked from many different pages, and I have no means of predicting the INPUT.
I guess I'm stuck with parsing the header...
Comment: Alternatively, you can include a hidden field in each of your pages that tells Action which page to redirect to. This way you can get finer control over things.
Answer: My solution would involve one interface and one interceptor.  You implement the following interface for all actions to which you are likely to want to redirect:
public interface TargetAware {
  public String getTarget();
  public void setTarget(String target);
}

The interceptor simply ensures that the target is set, if required:
public class SetTargetInterceptor extends MethodFilterInterceptor implements Interceptor {
   public String doIntercept(ActionInvocation invocation) {
      Object action = invocation.getAction();
      HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
      if (action instanceof TargetAware) {
         TargetAware targetAwareAction = (TargetAware) action;
         if (targetAwareAction.getTarget() == null)
            targetAwareAction.setTarget(getCurrentUri(request));
      }
      return invocation.invoke();
   }

   // I'm sure we can find a better implementation of this...
   private static String getCurrentUri(HttpServletRequest request) {
      String uri = request.getRequestURI();
      String queryString = request.getQueryString();
      if (queryString != null && !queryString.equals(""))
         uri += "?" + queryString;
      return uri;
   }

   public void init() { /* do nothing */ }
   public void destroy() { /* do nothing */ }
}

From then on, once these two bits are in place and your actions implement the TargetAware interface (if you expect to have to redirect to them), then you have access to a target parameter in your JSPs whenever you need it.  Pass that parameter on to your VisualizationAction (which might as well implement also the TargetAware interface!), and on SUCCESS, redirect as explained by Vincent Ramdhanie:
<action name="Visual" class="it.___.web.actions.VisualizationAction">
   <result type="redirect">
      <param name="location">${target}</param>
      <param name="parse">true</param>
   </result>
</action>

I did not try every single detail of this strategy.  In particular, beware of the notation surrounding the redirect result type (depending on your specific version of Struts2: 2.0.x and 2.1.x may differ on this...).
Answer: I prefer the way when you navigating users by particular actions.
http://domain.com/myAction.action
You could use some parameter as indicator, that you want to change current design:
i.e.
http://domain.com/myAction.action?changeDesign=silver_theme
So then, you write some struts 2 interceptor, which logic is to check the presence of such parameter 'changeDesign', and this interceptor will do nessesary work of changing design and will control workflow. With interceptor you decouple your actions from crosscutting logic.

Struts 2: return to calling page


How to filter with jquery for upper- and lowercase?

Question: I have a list with results contain Uppercases and Lowercases.
My problem is that I can't search in lowercases (type keywords in lowercase) It can't find any results.
This is the code for filtering a list using 1 input. I am using jquery for this:

    Filter by model:
    <input id="filtro" name="filtro" type="text" onkeyup="filter(this)" />

145GB 10K Ultra320 SCSI Pluggable Hard Drive

15 BL465c IB Cluster

05370-xx1 DL380 G3 cluster

AlphaServer GS320 Model 32

AMD Opteron 254 2.8GHz SC E BL25p Processor Kit

Answer: jsFiddle Demo
Usually what you want to do is set both the value you are searching for and the text you are searching in to upper case.
var value = $(element).val().toUpperCase();//value you are looking for

and later
if ($(this).text().toUpperCase().search(value) > -1)//text you are searching in


Z-index does not work

Question: I've been having some issues with the z-index property. Below you can see a example:

https://jsfiddle.net/cf5dhv39/1/
As you can see, I want that the div #button overlay the div #gray (like the div #trip does), but, no matter how much I increase the z-index in #button, the button doesn't overlay the #gray.
Comment: This is an issue of *stacking contexts*. This may be helpful: [Basics of the `z-index` property](http://stackoverflow.com/a/32515284/3597276).
Comment: Very useful Michael, thanks.
Sorry Gothdo, next time i'll do that ;)
Comment: Please post relevant CSS code *directly in the question*, not only on JSFiddle.
Answer: That is because #popup has smaller z-index than #gray and #button is child of #popup so it will always be behind #gray, but you can place #gray inside #popup

div{
  width: 100px;
  height: 100px;
  background: black;
  position: relative;
}

#popup{
  z-index: 100;
  width: 300px;
  height: 300px;
}

#button{
  top: 10px;
  left: 10px;
  z-index: 2;
  background: green;
}

#trip{
  top: 40px;
  left: 10px;
  background: blue;
  z-index: 1000;
}

#gray{
  width: 100%;
  height: 100%;
  position: fixed;
  background: gray;
  top: 0px;
  left: 0px;
  opacity: 0.5;
  z-index: 1;
}

Comment: i put gray inside popup and works fine here, thanks !

Z-index does not work


I left my dogs sitting on another players island on a server

Question: I recently left my dogs on another players island, sitting. Now, that player kicked me from the island and U need my dogs back ..is there a way to do it without commands or cheats?
Answer: I don't really think so, but you should ask the guy who kicked you out, or possibly just make some new dogs.


AngularJS after redirect from app to app, the default page is not loading

Question: I have 2 html files, one is index.html and another is project.html.
They have their own app.js and project.js, and share the same controllers.js.
I defined ng-app="app" and ng-app="project" seperately, however when I login and redirect to project.html, or I logout from project to index.html, the default page sometimes is not loading. I'm using ui-router, the default state seems not triggered occasionally. Anything wrong? Thanks.
Update
Seems it's nothing to do with redirection. Even I just enter the url (get) again and again, it stops loading default state once in a while. If I force refresh it works again.
Comment: I think you should declare as same app (ng-app="app") in different page, you can use route or template to render back
Comment: actually I tried that already, same. seems angular cached module and not init the module when redirect to the page. but sometimes it does reinit again.
Comment: This happens to Chrome only. Never happens on Safari. I guess chrome is 'too smart' to cache js object?
Answer: Turns out it's the Chrome extension 'AngularJS Batarang' causing all these problems.
Do not install it.
Comment: wow - thanks for sharing this.... was having a similar problem and was mind boggling, turning off Batarang got it working as expected.  Stoopid extensions...


Query System Stop Words in SQL Server

Question: How can I query the list of system stop words in SQL Server? I need to be able to do this without creating a new stop list that copies the system stop list.
Comment: This was way too hard to find. Google didn't even bring up this post when I searched for it. The "informational" warning uses the term noise word when they are encountered. I would add more keywords to your post to help drive searches to it. Add the words or phrases to catch "how do I query the default list of noise words in SQL Server." I was going to create this exact post to help others find it, but since it exists could you try to add text to drive searches to this post. I only found it by searching for fulltext_system_stopwords and that isn't much help. +1Q/+1A.
Answer: <code>SELECT  stopword
FROM    sys.fulltext_system_stopwords ssw
WHERE   language_id = 1033;
</code>
Comment: Thank you, this is exactly what I needed!

Query System Stop Words in SQL Server


Why is $\sum\left(\left\lfloor\frac{x}{p}\right\rfloor+\left\lfloor\frac{x}{p^2}\right\rfloor+\dots\right)\log p=\sum\frac{x}{p}\log p+O(x)$?

Question:
Why is $\sum\limits_{\substack{p:\text{prime}\\p\le x\\}}\left(\left\lfloor\frac{x}{p}\right\rfloor+\left\lfloor\frac{x}{p^2}\right\rfloor+\dots\right)\log p=\sum\limits_{\substack{p:\text{prime}\\p\le x\\}}\frac{x}{p}\log p+O(x)$ ?

It is from here Proposition $4.3$. That the left sum is equal to $\sum\limits_{n\le x}\log n$ is clear, follows from a previous result but the $2$nd equality, I don't get
Answer: We have $$\left\lfloor \frac{x}{p}\right\rfloor =\frac{x}{p}+O\left(1\right)
 $$ then $$\sum_{p\leq x}\left\lfloor \frac{x}{p}\right\rfloor \log\left(p\right)=\sum_{p\leq x}\frac{x}{p}\log\left(p\right)+O\left(\sum_{p\leq x}\log\left(p\right)\right)
 $$ and for each $l\geq2
 $ we have $$\sum_{p\leq x}\left\lfloor \frac{x}{p^{l}}\right\rfloor \log\left(p\right)\ll x\sum_{p\leq x}\frac{\log\left(p\right)}{p^{l}}\leq x\sum_{p\geq1}\frac{\log\left(p\right)}{p^{l}}\ll x
 $$ because the series $\sum_{p\geq1}\frac{\log\left(p\right)}{p^{l}}
 $ is convergent, then $$\sum_{p\leq x}\left(\left\lfloor \frac{x}{p}\right\rfloor +\left\lfloor \frac{x}{p^{2}}\right\rfloor +\dots\right)\log\left(p\right)=\sum_{p\leq x}\frac{x}{p}\log\left(p\right)+O\left(\sum_{p\leq x}\log\left(p\right)\right)+O\left(x\right)
 $$ but we know that $$\theta\left(x\right)=\sum_{p\leq x}\log\left(p\right)\ll x
 $$ then $$\sum_{p\leq x}\left(\left\lfloor \frac{x}{p}\right\rfloor +\left\lfloor \frac{x}{p^{2}}\right\rfloor +\dots\right)\log\left(p\right)=\sum_{p\leq x}\frac{x}{p}\log\left(p\right)+O\left(x\right).
 $$
Comment: Is $<<$ in your notation big-O, you used also some formulas from the script, thanks for taking your time, it looks fine.
Comment: @OBDA Yes, it's the Vinogradov's notation for big O.