AppFabric Caching - Proper use of DataCacheFactory and DataCache

Question: I am looking for the most performant way to arrange usage of the datacache and datacache factory for AppFabric caching calls, for between 400 and 700 cache gets per page load (and barely any puts).  It seems that using a single static DataCacheFactory (or possibly a couple in a round-robin setup) is the way to go.
Do I call GetCache("cacheName") for every DataCache object request, or do I make one static at the time DataCache factory is initialized and use that for all calls?
Do I have to handle exceptions, check for fail codes and attempt retries?
Do I have to consider contention when more than one thread tries to use the cache store and wants the same item (by key)?
Is there some kind of documentation which properly explores the design and usage of this?

Some information I have gathered so far from the forum:
http://social.msdn.microsoft.com/Forums/en-AU/velocity/thread/98d4f00d-3a1b-4d7c-88ba-384d3d5da915
"Creating the factory involves connecting to the cluster and can take some time. But once you have the factory object and the cache that you want to work with, you can simply reuse those objects to do puts and gets into the cache, and you should see much faster performance."
http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/0c1d7ce2-4c1b-4c63-b525-5d8f98bb8a49
"Creating single DataCacheFactory (singleton) is more performing than creating multiple DataCacheFactory. you should not create DataCacheFactory for each call, it will have performance hit."
"Please try to encapsulate round-robin algorithm (having 3/4/5 factory instances) in your singleton and compare load-test results."
http://blogs.msdn.com/b/velocity/archive/2009/04/15/pushing-client-performance.aspx
"You can increase the number of clients to increase the cache throughput. But sometimes if you want to have smaller set of clients and increase throughput, a trick is to use multiple DataCacheFactory instances. The DataCacheFactory instance creates a connection to the servers (e..g if there are 3 servers, it will create 3 connections) and multiplexes all requests from the datacaches on to these connections. So if the put/get volume is very high, these TCP connections might be bottlenecked. So one way is to create multiple DataCacheFactory instances and then use the operations on them."

Here what is in use so far... the property is called and if the return value is not null an operation is performed.
private static DataCache Cache
{
    get
    {
        if (_cacheFactory == null)
        {
            lock (Sync)
            {
                if (_cacheFactory == null)
                {
                    try
                    {
                        _cacheFactory = new DataCacheFactory();
                    }
                    catch (DataCacheException ex)
                    {
                        if (_logger != null)
                        {
                            _logger.LogError(ex.Message, ex);
                        }
                    }
                }
            }
        }

        DataCache cache = null;

        if (_cacheFactory != null)
        {
            cache = _cacheFactory.GetCache(_cacheName);
        }

        return cache;
    }
}

See this question on Microsoft AppFabric forum: http://social.msdn.microsoft.com/Forums/en-AU/velocity/thread/e0a0c6fb-df4e-499f-a023-ba16afb6614f
Comment: There is an answer to this now in the forum. Check the link above.
Answer: Here is the answer from the forum post:

Hi. Sorry for the delayed response,
  but I want to say that these are great
  questions and will probably be useful
  to others.
There shouldn't be a need for more
  than one DataCacheFactory per thread
  unless you are requiring different
  configurations. For example, if you
  programmatically configure the
  DataCacheFactory with the
  DataCacheFactoryConfiguration class,
  then you might want to create one that
  has local cache enabled and another
  that does not. In this case, you would
  use different DataCacheFactory objects
  depending on the configuration you
  require for your scenario. But other
  than differences in configuration, you
  should not see a performance gain by
  creating multiple DataCacheFactories.
On the same subject, there is a
  MaxConnectionsToServer setting (either
  programmatic in
  DataCacheFactoryConfiguration or in
  the application configuration file as
  an attribute of the dataCacheClient
  element). This determines the number
  of chennels per DataCacheFactory that
  are opened to the cache cluster. If
  you have high throughput requirements
  and also available CPU/Network
  bandwidth, increasing this setting to
  3 or higher can increase throughput.
  We don't recommend increasing this
  without cause or to a value that is
  too high for your needs. You should
  change the value and then test your
  scenario to observe the results. We
  hope to have more official guidance on
  this in the future.
Once you have a DataCacheFactory, you
  do not need to call GetCache()
  multiple times to get multiple
  DataCache objects. Every call to
  GetCache() for the same cache on the
  same factory returns the same
  DataCache object. Also, once you have
  the DataCache object, you do not need
  to continue to call DataCacheFactory
  for it. Just store the DataCache
  object and continue to use it.
  However, do not let the
  DataCacheFactory object get disposed.
  The life of the DataCache object is
  tied to the DataCacheFactory object.
You should never have to worry about
  contention with Get requests. However,
  with Put/Add requests, there can be
  contention if multiple data cache
  clients are updating the same key at
  the same time. In this case, you will
  get an exception with an error code of
  ERRCA0017, RetryLater and a substatus
  of ES0005, KeyLatched. However, you
  can easily add exception handling and
  retry logic to attempt the update
  again when errors such as these occur.
  This can be done for RetryLater codes
  with various substatus values. For
  more information, see
  http://msdn.microsoft.com/en-us/library/ff637738.aspx.
  You can also use pessimistic locking
  by using the GetAndLock() and
  PutAndUnlock() APIs. If you use this
  method it is your responsibility to
  make sure that all cache clients use
  pessimistic locking. A Put() call will
  wipe out an object that was previously
  locked by GetAndLock().
I hope this helps. Like I said, we
  hope to get this type of guidance into
  some formal content soon. But it is
  better to share it here on the forum
  until then. Thanks!
Jason Roth
Comment: MaxConnectionsToServer is a powerful configuration setting. It provides parallelism without explicit coding of same. Be sure to implement logic to catch DataCacheExceptions and look for ErrorCode of RetryLater or Timeout for retries however, because conflicts may occur much more frequently.
Answer:
Do I call GetCache("cacheName") for
  every DataCache object request, or do
  I make one static at the time
  DataCache factory is initialized and
  use that for all calls?

I suppose really the answer should be; try it both ways and see if there's a difference, but one static DataCache seems to me to make more sense than a corresponding call to GetCache for every call to Get.
That 'Pushing Client Performance' article suggests that there's a sweet spot where the number of DataCacheFactory instances gets you maximum performance beyond which the memory overhead starts working against you - it's a shame they didn't give any guidelines (or even a rule of thumb) on where this spot might be.
I haven't come across any documentation on maximising performance - I think AppFabric is still too new for these guidelines to have been shaken out yet. I did have a look in the Contents for the Pro AppFabric book, but it seems much more concerned generally with the workflow (Dublin) side of AppFabric rather than the caching (Velocity) piece.
One thing I would say though: is there any possibility for you to cache 'chunkier' objects so you can make fewer calls to Get? Could you cache collections rather than individual objects and then unpack the collections on the client? 700 cache gets per page load seems to me to be a huge number!
Comment: With respect to pulling large numbers of items from the cache, have you had a look at the tagging and region functionality yet? If not have a read through this blog post on the subject: http://blogs.msdn.com/b/skaufman/archive/2010/04/22/tagging-objects-in-the-appfabric-cache.aspx
Comment: @Rohland Tags and regions might be a way to simplify some of the code by reducing the number of calls to cache.Get, but I'm not sure they'd offer a significant performance benefit as I think the code would still be pulling the same number of items from the cache per page i.e. I think you'd still be hitting the same bottlenecks.
Comment: Yes it is a huge number, but I'm adding a caching provider for a nasty system and cant really change its core caching requirements.  Thanks for your comments, still it highlights the fact there is not much good information out there for my specific problem.


Create a Routes path that loads a specific component based on condition

Question: I want to load distinct components for similar structured paths.
Means:

example.com/whatever-post-slug --> Load Post Component
  example.com/whatever-page-slug --> Load Page Component
  example.com/hello-i-am-a-string --> Load Post Component (because that
  slug belongs to a post)
  example.com/about-us --> Load Page Component (because that slug belongs to a page)

Why would I do this?
One may think: hey but why don't you just define two prefixed different paths, which is what paths are there for?
The answer is: I am creating (by the way an open source) Angular WordPress theme and I want the routing to be something the user decides, without forcing any hardcoded structure.
In one of the multiple possibilities, Posts and Pages (if you don't know WordPress, just know those are two distinct content types which I want to use independent Modules and Components for) may share slugs at a root level.
And at a first glance, I cannot know if the slug belongs to a post or a page or something else (so UrlMatcher cannot help here).
Did I think of a solution?
Yes, of three.
First solution
I could create a Wrapper Component that will load for the catchall route, and then, inside this component do something like:
<whatever-a-component *ngIf="showComponentA()"><whatever-a-component>
<whatever-b-component *ngIf="showComponentB()"></whatever-b-component>

And let the Wrapper Component do all the logic.
This adds an extra intermediate Component into the game.
Second solution
Use a resolver for the catchall and then do all the logic in the resolver.
The problem is, I need to subscribe to an http communication to know what kind of content type I am dealing with, and since the Resolver resolve() method needs to return an Observable, it's not nice to subscribe in there.
Of course, it works, if I return a placeholder observable that waits for a while, like so:
// ... inside resolve()
// Force an observable return to allow suscriptions take their time
return Observable.create(observer => {
  setTimeout(() => {
    console.log("Resolver observable is done");
    observer.complete();
  }, 15000);
});

... or if I pipe my subscription with mergeMap() and return EMPTY when I get the results from my subscription.
And once I get my data back I can set new routes including the current path that must point to its specific component.
That does not seem a very clean approach to me.
Third solution
Just load a normal Dispatcher Component that will do all the checkings at OnInit() and then navigate to a "secret" component-specific url but using { skipLocationChange: true }, so the user will have the correct route and also the correct component will be loaded.
But again, this adds an extra intermediate component into the game.
I think this is the cleanest solution, since at the App Routing module I can do something like:
{
path: 'wp-angular/view-post/:id',
loadChildren: () => import('./view-post/view-post.module').then(m => m.ViewPostModule)
},
{
path: 'wp-angular/view-page/:id',
loadChildren: () => import('./view-page/view-page.module').then(m => m.ViewPageModule)
}

So, I will have those modules lazy loaded only if the user actually visits one of those two content types.
Also, that content type component will already be available if the user then visits a second content of the same type.
And the fact that I can use { skipLocationChange: true } will make it possible to keep the path as expected.
Plus, this allows displaying navigation loading feedback without the need to subscribe to the Router Events.
Question
What would you do and why?
Maybe I am missing some magic Angular feature that allows doing this in a straight-to-the-point way.
Answer: Once I had a similar problem and I resolved like this:
You can use a Module that handles what component should be load by providing the ROUTES of the RouterModule using the useFactory provider of Angular.
The code could be something like this:
// HandlerModule
@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    RouterModule
  ],
  providers: [
    {
      provide: ROUTES,
      useFactory: configHandlerRoutes,
      deps: [CustomService],
      multi: true
    }
  ]
})

export class HandlerModule {}

export function configHandlerRoutes(customService: CustomService) {
  let routes: Routes = [];
  if (customService.whatever()) {
    routes = [
      {
        path: '', component: AComp
      }
    ];
  } else {
    routes = [
      {
        path: '', component: BComp
      }
    ];
  }
  return routes;
}

Then in your AppRoutingModule the module of the path '' is going to be the HandlerModule:
// AppRoutingModule

 {
    path: '',
    loadChildren: () => import('app/handler/handler.module').then(mod => mod.HandlerModule)
}

After in the CustomService you have to update the Router.config when the value that provides the method .whatever() changes because the application will only load the component that had loaded the first time. This is because the function "configHandlerRoutes" use by the useFactory provider in the HandlerModule is only executed the first time we navigate to the "" path and after that, the Angular Router already know which component he has to load.
In conclusion in the CustomService you have to do:
  export class CustomService {
  private whateverValue: boolean;
  constructor(private router: Router) {
  }

  public whatever(): boolean {
    return this.whateverValue;
  }

  public setWhatever(value: boolean): void {
    const previous = this.whateverValue;
    this.whateverValue = value;
    if (previous === this.whateverValue) {
      return;
    }
    const i = this.router.config.findIndex(x => x.path === '');
    this.router.config.splice(i, 1);
    this.router.config.push(
      {path: '', loadChildren: () => import('app/handler/handler.module').then(mod => mod.HandlerModule)}
    );
  }
}

That's it. I used the "" path for the example but you can use any path you want.
Also, you can use the same approach if you want to load Modules instead of components.
If you want another reference here is an article where they use the same approach: https://email@example.com/angular-use-the-same-route-path-for-different-modules-or-components-11db75cac455
Comment: I need to know which path the user is trying to access. But I cannot use ActivatedRouteSnapshot inside configHandlerRoutes()
Comment: You can add the ActivatedRoute as a dependency of the provider:

`@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    RouterModule
  ],
  providers: [
    {
      provide: ROUTES,
      useFactory: configHandlerRoutes,
      deps: [CustomService, ActivatedRoute],
      multi: true
    }
  ]
})
`
After that you can use it in the configHandlerRoutes():

`export function configHandlerRoutes(customService: CustomService, route: ActivatedRoute) {
...
}
`


Showing a function of Bernoulli random variable is a martingale

Question: Let $X_{0},X_{1},\cdots$ be i.i.d Bernoulli random variables with parameter $p$ (i.e., $\mathbb {P}(X_{i}=1)=p$, $\mathbb {P}(X_{i}=0)=1-p$). Define $S_{n}=\sum_{i=1}^{n} X_{i}$ where $S_{0}=0$. Define
\begin{align*}
Z_{n}=\left(\frac {1-p}{p}\right)^{2S_{n}-n},\quad n=0,1,2,\cdots
\end{align*}
Let $\mathcal {F}_{n}=\sigma(X_{0},X_{1},\cdots,X_{n})$. I want to show that $Z_{n}$ is a martingale with respect to this filtration. When I check if $\mathbb {E}(Z_{n+1}\vert \mathcal {F}_{n})=Z_{n}$, I have the following:
\begin{align*}
\mathbb {E}(Z_{n+1}\vert \mathcal {F}_{n})=\mathbb {E}\left( \left(\frac {1-p}{p}\right)^{2S_{n+1}-n-1}\vert\mathcal {F}_{n}\right)=\mathbb {E}\left( \left(\frac {1-p}{p}\right)^{2S_{n}-n}\cdot \left(\frac {1-p}{p}\right)^{2X_{n+1}-1}\vert\mathcal {F}_{n}\right)=\left(\frac {1-p}{p}\right)^{2S_{n}-n}\mathbb {E}\left(\left(\frac {1-p}{p}\right)^{2X_{n+1}-1}\vert\mathcal {F}_{n}\right)=\left(\frac {1-p}{p}\right)^{2S_{n}-n}\mathbb {E}\left(\frac {1-p}{p}\right)^{2X_{n+1}-1}=\left(\frac {1-p}{p}\right)^{2S_{n}-n}\cdot\left(\frac {1-p}{p}\right)^{2p-1}
\end{align*}
I don't know how to show that $\left(\frac {1-p}{p}\right)^{2p-1}=1$ or is there anything wrong with my argument?
Answer: Your computation of $\Bbb{E} \left(\frac{1-p}{p} \right)^{2X_{n+1}-1}$ is wrong. Note that it is not equal to $\left(\frac{1-p}{p}\right)^{2\Bbb{E}[X_{n+1}]-1}$, which is what you have written. It is in fact equal to (using the formula $E[f(X)] = \sum_{k} P(X=k)f(k)$):
$$
\Bbb{E} \left(\frac{1-p}{p} \right)^{2X_{n+1}-1} = \left(\frac{1-p}{p}\right)^{2(0)-1} P(X_{n+1} = 0) + \left(\frac{1-p}{p}\right)^{2(1)-1} P(X_{n+1} = 1) \\
= \frac{p}{1-p}(1-p) + \frac{1-p}{p}(p) = p+1-p = 1
$$
and therefore the result of your computation is just $\left(\frac{1-p}{p}\right)^{2S_n - n}$, which shows one part of the martingale conditions. You still need to show that the random variable $Z_i$ has finite expectation, and is adapted to the required filtration. Both of these are easy exercises.

EDIT : To see a use of this martingale, let us imagine we are playing a simple game , where you are tossing coins that turn heads with probability $p$, and you are counting the number of successes, which is $S_n$. The above martingale provides great control on $S_n$. Furthermore, if we were , say losing /gaining a rupee for each time we won/lost (refer to Gambler's ruin) then the above martingale(or a variant) , along with stopping time results , will give us probabilties for various functions of the game (the most common being : if we start with $n$ dollars and wish to get to $N>n$ dollars before we go bankrupt while playing this game, what is the probability of that happening?)
Comment: @Jiexiong687691 Indeed, $0<p<1$ is the only way this martingale works. But I want to say that if $p=0$ and $p=1$, then there are obvious martingales involving $S_n$ (I mean, $S_n$ will be constant random variables in these two cases, so it is obvious that one can predict $S_n$).
Comment: Good answer! I was about to write the same. :)
Comment: @ProfessorVector Thank you. I did want to suggest it in a comment, to be fair, but I felt there was enough in the attempt to convince me that this user had tried. I still feel I should add a few more details, though. Thanks once again.
Comment: No, that's not just a hint, it's a full-fledged answer, so it would be *wrong* to put it into a comment. If anybody needs *still* more details, they can ask.
Comment: @ProfessorVector You are correct.
Comment: @TeresaLisbon Thanks a lot! For the other two, I just say that $Z_{n}\in \mathcal {F}_{n}$ since $S_{n}\in \mathcal {F}_{n}$. And is it true that $0<p<1$ in this problem? I mean if $p=0$, then $Z_{n}$ is not well-defined and if $p=1$, then it contradicts with the fact that $S_{0}=0$.


How can I prevent water buildup in a pit for a trampoline?

Question: A friend of mine is looking in to purchasing a trampoline for her garden. So it doesn't change the look too much she wants it lowered in to the floor. I mentioned that it might collect water, especially if she bricks up the walls.
What should be done to prevent this?
Comment: Given the effort to dig that large a hole, it would be tempting to reduce the distance between the trampoline surface and the ground. Don't!
Answer: As long as she doesn't lower the frame of the trampoline itself, then she's fine there. Any kind of wall should be within the diameter of the trampoline frame or very close to it, nothing closer than the frame's inner diameter & nothing beyond the frame's outer diameter. Eliminating fall off & reducing overall fall height is good. But, many trampolines come with retention nets.
You're precisely right though, digging a pit for it will collect & hold water. She'll need to pump water out or install a catch basin that's piped for immediate drainage. If she can cut it into a downslope or hillside, then the pit must drain slightly downhill & out of a the backside or have an open trench outlet dug or a large pipe poked through any remaining hill.


Is integral operator hermitian?

Question: If v is an element of a vector space of complex functions and A is an operator such that
Av=$\int_{0}^{x}vdx$,
then is A hermitian?
Thanks for the help.
Comment: It is probably a good idea to tell us what is the complex vector space you work with and with which Hermitian product it is endowed. I can probably make a guess but your question have to be clear and not open to interpretation. Also, what have you tried?
Comment: This is actually a question in quantum mechanics, where the vector space is the set of all complex valued functions. I ended up with a double integral .
Answer: Consider the operator $A$ on $L^2([0,a])$ by
$$ Av(x) = \int_{0}^{x} v(t) \, dt. $$
We can easily check that $A$ is a bounded operator on $L^2([0,a])$. Then for any $u, v \in L^2([0,a])$, the computation
$$
(u, Av)_{L^2}
= \int_{0}^{a} u(x) \overline{Av(x)} \, dx
= \int_{0}^{a}\int_{0}^{x}  u(x) \overline{v(t)} \,dt dx
= \int_{0}^{a}\int_{t}^{a}  u(x) \overline{v(t)} \,dx dt
$$
shows that the adjoint $A^*$ of $A$ satisfies
$$ A^* u(x) = \int_{x}^{a} u(t) \, dt. \tag{*} $$
Therefore $A \neq A^*$. (If you are worried about interchanging the order of integration, notice that the computation is justified when $u$ and $v$ are bounded in view of Fubini's theorem, and then $\text{(*)}$ extends to all of $L^2([0,a])$ by density argument.)
Answer: No, it is not. Consider e.g. $u$ and $v$ the indicator functions of intervals $[a,b]$ and $[c,d]$ respectively, where $0 < a < b < c < d$.  Then $\langle u,\; A v\rangle   = 0$ but $\langle A u,\; v \rangle \ne 0$.

Is integral operator hermitian?


understand limits of integration

Question: can someone tell me how can I understand the integration limits in this triple integral and the reason behind it ?
assume that I can't draw this 3 dimension domain.
thank you !
enter image description here
Comment: You can't draw this domain, but you can write it with latex/mathjax, the triple integral included, so we don't need a link to an image...
Comment: What triple integral?
Comment: sorry now it is in the post
Comment: how can I use latex here ?
Answer: I'm not sure what you mean by "the reason behind it".
By looking at the definition of $\Omega$, the condition $x² \leq y \leq x$ tells you that $0 \leq x \leq 1$ (otherwise it is not possible that $x² \leq x$). After you have found the bounds for $x$ you can write the integrals using the bounds for $y$ and $z$ given:
$$\int_\Omega (x+2y) dxdydz = \int_0^1 \int_0^x \int_{x²}^x (x+2y) dydzdx$$
assuming that you are allowed to interchange the integral signs.
Edit: If the order of the integration has to be maintained, you have to adapt the inequalities a little bit: We have $0 \leq z \leq x$ and $0 \leq x \leq 1$ so it follows that $z$ can range from $0$ to $1$. $x² \leq y \leq x$ is equivalent to $y \leq x \leq \sqrt{y}$ and as $y \leq x$ the condition $0 \leq z \leq x$ changes to $0 \leq z \leq y$. So you get
$$\int_\Omega (x+2y) dxdydz = \int_0^1 \int_z^1 \int_y^{\sqrt{y}} (x+2y) dxdydz$$
Comment: hey, thank you for your comment, but it has to be in the original order
Comment: I edited the answer accordingly.
Comment: Thank you very much !

understand limits of integration


Need to identify the "tray is empty or not" using opencv

Question: I want to recognize the tray is empty or not in the given image using OpenCV in python.
below is what I have tried

detect the biggest rectangle and cropped by using the below code

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thresh = cv2.erode(thresh, kernel, iterations=4)
thresh = cv2.dilate(thresh, kernel, iterations=4)
cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Find the biggest contour
max_area = -1
max_c = 0
for i in range(len(cnts)):
    contour = cnts[i]
    area = cv2.contourArea(contour)
    if (area > max_area):
        max_area = area
        max_c = i
contour = cnts[max_c]

areas = [cv2.contourArea(c) for c in cnts]
max_index = np.argmax(areas)
cnt=cnts[max_index]

x,y,w,h = cv2.boundingRect(cnt)

crop_img = img[y:y+h, x:x+w]

and then trying to find black dots by getting contours after applying the canny edge detection and applying mask and noise removal methods

#Create empty mask and flood fill
mask = np.zeros(edges.shape)
#Create 3-channel alpha mask
mask_stack = np.dstack([mask]*3)

and again threshold the image and find contours.

It works for medium and large objects but when I put the small objects like coins, this method is not working because the tray has some scratches and dust too. This is my first project using OpenCV in python
please help to find the solution to achieve this requirement.
Input:

Output:

Empty Tray:
Comment: show both an empty **and** a non-empty tray
Comment: Hi @Christoph, I have added empty tray image in my question. Any objects will be placed in tray.
Answer: I recommend you to:

do camera calibration to make the lines in your image straight, and
align your camera well to your production line so you can simplify the image processing and make it more robust, and
use better illumination conditions if possible.

Given the images above, here is a brute force solution:

you can first extract the region of interest (ROI) through:
HSV segmentation followed by contour detection, then you can extract the rectangular shape of the tray using this SO answer.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np

# Ref: https://stackoverflow.com/questions/37177811
def crop_minAreaRect(img, rect):

    box = cv2.boxPoints(rect)
    box = np.int0(box)

    W = rect[1][0]
    H = rect[1][1]

    Xs = [i[0] for i in box]
    Ys = [i[1] for i in box]
    x1 = min(Xs)
    x2 = max(Xs)
    y1 = min(Ys)
    y2 = max(Ys)

    angle = rect[2]

    rotated = False

    if angle < -45:
        angle += 90
        rotated = True
    # Center of rectangle in source image
    center = ((x1+x2)/2,(y1+y2)/2)
    # Size of the upright rectangle bounding the rotated rectangle
    size = (x2-x1, y2-y1)
    M = cv2.getRotationMatrix2D((size[0]/2, size[1]/2), angle, 1.0)
    # Cropped upright rectangle
    cropped = cv2.getRectSubPix(img, size, center)
    cropped = cv2.warpAffine(cropped, M, size)
    croppedW = W if not rotated else H
    croppedH = H if not rotated else W
    # Final cropped & rotated rectangle
    croppedRotated = cv2.getRectSubPix(cropped, (int(croppedW),int(croppedH)), (size[0]/2, size[1]/2))
    return croppedRotated

def ROI(img):
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    lower_hsv = np.array([0, 0, 135]) # 0, 0, 135
    higher_hsv = np.array([180, 20, 240]) # 180, 20, 240

    mask = cv2.inRange(hsv, lower_hsv, higher_hsv)

    seg = cv2.bitwise_and(img, img, mask=mask)

    seg_gray = cv2.cvtColor(seg, cv2.COLOR_BGR2GRAY)

    k1 = 51
    seg_gauss = cv2.GaussianBlur(seg_gray, (k1, k1), 0)
    seg_th = cv2.threshold(seg_gauss, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

    contours, hierarchy = cv2.findContours(image=seg_th, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
    for i, cnt in enumerate(contours):
        if  cv2.contourArea(cnt)>1000000:
            x,y,w,h = cv2.boundingRect(cnt)
            rect = cv2.minAreaRect(cnt)

    roi = crop_minAreaRect(img, rect)
    return roi

As you reduced your problem to a specific ROI, you can do use edge detection, Morphology, and Filtering as follows:

def ImProc(roi):

    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

    k_smooth = 5
    gauss = cv2.GaussianBlur(gray, (k_smooth, k_smooth), 0)

    # # Canny
    edges = cv2.Canny(gauss,0,255)

    k = 31 #300
    kernel = np.ones((k,k),np.uint8)
    mph = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

    res = cv2.medianBlur(mph, 21)

    return res

I have applied this approach to the given images:
empty = cv2.imread("empty.jpg")
full = cv2.imread("full.jpg")

roi_empty = ROI(empty)
roi_full = ROI(full)

res_empty = ImProc(roi_empty)
res_full = ImProc(roi_full)

cv2.namedWindow("res_full", cv2.WINDOW_NORMAL)
cv2.imshow("res_full", res_full)

cv2.waitKey(0)

and I have got the following result for the full tray (rotated):

Note that this solution is subject to different parameters, to better control them and get stable results I advise you to consider the recommendations above.
Comment: Hi @Bilal. Thank you so much. After a week my sleepless night coming to an end. your suggestions bringing  out the almost required output. ***hsv range value is the key what i have missed***. My first ever opencv project is going to have a successful release, all credits to you.


Sorting a list of objects in python on basis of attribute's three different value?

Question: I have  a list of objects.Every object has an attribute "priority" whose value could be low,medium,high.I would need to sort the list of objects so that objects with priority "high" comes first in list and then objects with priority "medium" and lastly  objects with priority "low".I know I can do this using few for loops , but I want a pythonic solution that is fast and effective.sorry but i am a beginner in python.
Comment: http://wiki.python.org/moin/HowTo/Sorting
Answer: score = dict(high=0, medium=1, low=2)
mylist.sort(key=lambda x: score[x.priority])

Answer: Thanks a lot. Upvoted for the pythonic solution.
I used the sorted variant.
sorted(myList, key = lambda x : score[x.priority])


Install Firefox extension with automatic script

Question: I'm trying to install an extension for Firefox in Ubuntu, either globally or for a specific Ubuntu user or for a specific Firefox profile - doesn't matter which. I've tried the solutions posted in older threads but they don't work for the Firefox version (20.0) right now. I've tried various solutions and none of them have worked.
Does anyone know of a command/script that can do this?
Thanks!
Comment: What did you try?  Did you try the method outlined at [Installing extensions](https://developer.mozilla.org/en-US/docs/Installing_extensions)?
Comment: Yeah, I tried all of those, both global and user specific, and both as an .xpi or extracted. Firefox just seems to ignore what I do; the extension list does not show my extension.
Answer: I suspect you are using firefox for some scripts / bots or a resource hungry website and you have an old computer? I recommend installing RamRUSH as an add-on - with the number of frames below 8% it automatically optimizes memory and firefox is more stable
Personally, I also used the trick: about: config
browser.sessionstorage = 0;
browser.sessionstory = 0;
cpd.history.enabled = false;

This relieves the browser a bit and is more stable
Answer: Figured it out. The core reason why it didn't work was because my extension itself was faulty.
The solution to do it is to put the Firefox extension (an .xpi file) under /usr/share/mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/.


How to keep sentries and templars in the center of death balll?

Question: Couple of posts about sentries usages (Effective use of Sentry in Starcraft 2) mention it is good to keep sentries in the center of you army so guardian shield will cover bigger amount of units and sentries can FF opponent quicker.
Similar I can say about templars: in center they are more defended and can quicker cast storm or feedback.
But even though I keep sentries on the same short-key as other gateway units they tend to spread out asides.
With templars that is even worse: they are slower and I keep templars usually on a separate short-key. As a result they often either behind and come to the battle too late or they are unprotected at all...
How you deal with this to keep sentries in the center of the death-ball and templars close to it?
Thanks a lot for any advise!
P.S. I do realize that keeping casters in PvT is vulnerable to EMPs, but in my current level (top gold/medium platinum) I'm not EMPed too often and that is not a problem for me yet.
Answer: If your micro isn't up to par one way you can keep sentries close to the centre of the deathball is by giving them a move (follow) command on your collossi by right-clicking on them. Keep them in a separate hotkey numver from your army so you don't accidentally cause them to unfollow.
as soon as you cast GS right click on the collossi again and they will stay underneath it.
Templars are a different story cause they're so slow. Still, I recommend keeping them ona separate hotkey from the rest of your army and ordering them to follow the collossi. Or, order your ARMY to follow the templars when your not in combat and don't expect an attack.
Answer: first of all I am a Top Platinum Player, actually I won many many games in Platinum Rank 1 vs. Diamond but stopped playing.
Sentries
With Sentries in Deathball it correct what you say, keep them in the middle is good so you can force field well and shield everything. I never experienced this problems you encounter with sentries, I just placed them in the deathball and as soon as you start the fight you anyway activate more than 1 shield. Since it would be stupid to have one shield which covers all units. And of course you will do some moving with your melee army and your colossus. So as soon as the fight starts you try to hide colossus behind Stalkers which are killing the vikings or any other anti-air air-units. And your Zealots are rushing towards enemy land units, so you spread anyway a bit. So simply keep the number of sentries up. If you have 200/200 supply which is mostly army than you should have at least 7 sentries which should cover your deathball well.
Templars:
Templars are used for storming biological units, mostly used vs marines & marauders or Zerg units. To be honest I didn't use templars often and that's not good, but from watching streams you will see how they use them. Often they seperate a group of templars coming from behind or from the side in the fight. Of course you can keep some in your deathball, but don't expect the enemy to be that stupid to run into your ball.
Alternatively templars are used from a surprise position like dropped from ship or sitting on a cliff nearby and morphed right after. To catch the enemy army before he perfectly splits them to fight your deathball.
And also you will not get EMP'd often in early Diamond - at least happened really rarely to me.
Comment: Generally you will see high-level protoss activate two guardian shields.  It has the benefit of additional coverage and prevents you from losing GS if your single sentry dies.
Generally you should have zealots in front of sentries, but there is no better way than to select them all and move back.
Templars should be near the front for feedback
Comment: only 2? they simply hit the button fast, sometimes more than 2 but never less ;) I never said only 1
Comment: I meant exactly two.  Three is generally a waste.  Depending on the sentry count you could use a single GS, but in PvT especially you'll see two


The Medibuntu Project has come to an end - what do I do now?

Question: The Medibuntu Project has come to an end: what do I do now?

How do I find packages on my system which came from medibuntu? The previous question seems to list medibuntu packages explicitly - is there a way to query the package database by the source?
Do I need to remove them so that they are replace by maintained packages from other sources?
Which repository contains those packages now?
Comment: Related: as it is what to do about one of the packages, http://askubuntu.com/questions/356046/i-cant-install-libdvdread4-due-the-script-not-finding-the-medibuntu-repositorie
Comment: http://askubuntu.com/questions/85397/is-medibuntu-needed-or-even-desirable
Answer: By now almost all the medibuntu packages have been already superseded with packages from the main repos (restricted, universe, multiverse), so the task is to eliminate the traces of medibuntu.
You would probably be better off installing aptitude before hand (see below).
As root, do the following to forget medibuntu:
aptitude purge medibuntu-keyring
rm -f /etc/apt/sources.list.d/medibuntu.list
aptitude update

Now, to purge the packages which come from medibuntu, do this:
aptitude purge $(dpkg-query -W -f='${PackageSpec}\t${Maintainer}\n' '*' |\
  grep email@example.com |\
  cut -f1)

You might have to select an alternative dependency problem resolution (e.g., aptitude might suggest that you remove a 100 packages if you purge a medibuntu library libavutil; say no and it will offer to install the same library from an alternative source).
This means that you will probably be better off using aptitude than apt-get for this task because the former offers the alternatives for dependency problem resolution.  See also

Aptitude vs. apt-get: Which is the recommended (aka the "right") tool to use?
Is aptitude still considered superior to apt-get?
Comment: You'll either need to install aptitude first (`sudo apt-get install aptitude`) or use `apt-get`. Aptitude doesn't exist by default.
Comment: The 'cut' command is wrong as the delimiter is a space, use this instead: aptitude purge $(dpkg-query -W -f='${PackageSpec} ${Maintainer}\n' '*' |\ grep firstname.lastname@example.com |\ cut -f1 -d' ')
Comment: **aptitude is not installed by default** in Ubuntu. Can a user install a package (aptitude) if he/she has already a corrupted repository ? Will apt-get let him/her to install anything at all ?
Comment: OK, what NikTh is telling you that your answer should **install aptitude first** before you can start using it ;).
Comment: @tdk: I fixed the command differently.
Comment: The most important part of the question has not been answered: "Which repository contains those packages now?"
Comment: @Mephisto: By now almost all the medibuntu packages have been already superseded with packages from the main repos (restricted, universe, multiverse), so the task is to eliminate the traces of medibuntu
Comment: @NikTh: please ask this as a separate question.
Comment: why the downvote?
Comment: Now you edited your answer the downvote(mine) became an Upvote :-)


How to add menu and settings option to the customized title bar of android?

Question: My requirement is to increase the height of the title bar, however I am not sure if that is feasible. If it is how can I increase its height so I would have more space. If it is not, I would need to add a menu option (Hamburger Style) to my customized title bar.
custom_title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#77b48e"
    android:gravity="center_vertical"
    android:orientation="vertical" >

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="29dp"
            android:layout_height="28dp"
            android:maxWidth="1dp"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/title_bar_viewname"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_weight="0.07"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="10sp" />

    </LinearLayout>

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

</LinearLayout>

MainActivity.java
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    }

Comment: Is
[this](http://stackoverflow.com/questions/6226538/how-to-increase-the-size-of-the-title-bar-in-an-android-application) what you need ?

**Check 2nd Answer**
Answer:
Change your MainActivity.java like this:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main);

       actionBar = getActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(
                Color.parseColor("#373836"));
        actionBar.setBackgroundDrawable(colorDrawable);

    }

For an Eg: now your action bar will be shown like this:

Then if you need to add the setting menu items in the action bar.
Add this below code in MainActivity,java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
   switch (item.getItemId())
   {
     case R.id.search:
      //your code here
        return true;
     default:
        return super.onOptionsItemSelected(item);
   }
}

Then in res/menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >

    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_action_settings"
        android:showAsAction="always"
        android:title="text"/>

</menu>

Now you can see the settings icon in the action bar

Finally if you have to increase the Action bar Height:
In manifest:
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" > --->set a theme

...
</application>

Then in Styles.xml:
<resources>

</resources>

Now Action bar size is increased upto the size we given
in styles.
Answer: With the release of android L, a new view has been released: Toolbar. Toolbar gives you much more flexibility to style the action Bar. It is also available for lower android versions via the appCompat library.

In this release, Android introduces a new Toolbar widget. This is a generalization of the Action Bar pattern that gives you much more control and flexibility. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate it, and react to scroll events. You can also set it as your Activity's action bar, meaning that your standard options menu actions will be display within it.

You can change the height of your action bar by using Toolbar and just setting the layout_height attribute to the height you want.
More info on how to use it:
http://android-developers.blogspot.be/2014/10/appcompat-v21-material-design-for-pre.html
Documentation:
http://developer.android.com/reference/android/widget/Toolbar.html


Discover PL/SQL package-level types using Oracle dictionary views

Question: Can I discover types declared in Oracle PL/SQL packages using dictionary views? I.e. when I declare this:
CREATE OR REPLACE PACKAGE my_types AS
  TYPE t_cursor_type IS REF CURSOR;
  TYPE t_table_type IS TABLE OF some_table%rowtype;
END my_types;

I'd like to formally discover t_cursor_typeand t_table_type. They don't seem to be listed in
SYS.ALL_TYPES

By "formally" I mean I'd like some "formal meta-data", i.e. querying USER_SOURCE won't do the trick.
Answer: Only from the 11.1 onward.
From the manual:
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17125/adfns_plscope.htm#ADFNS02204
http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams189.htm#REFRN10271

PL/Scope is a compiler-driven tool that collects data about identifiers in PL/SQL source code at program-unit compilation time and makes it available in static data dictionary views. The collected data includes information about identifier types, usages (declaration, definition, reference, call, assignment) and the location of each usage in the source code.

DEMO
CREATE OR REPLACE PACKAGE my_types AS
  TYPE t_cursor_type IS REF CURSOR;
  TYPE t_table_type IS TABLE OF employees%rowtype;
  type t_associative is table number index by  varchar2(20);
END my_types;

alter package  my_types compile plscope_settings='IDENTIFIERS:ALL' reuse settings;

select *
from   user_identifiers ui
where  ui.object_type = 'PACKAGE'
and    ui.usage = 'DECLARATION'
and    ui.usage_context_id = '1';

NAME                           SIGNATURE                        TYPE               OBJECT_NAME                    OBJECT_TYPE   USAGE         USAGE_ID       LINE        COL USAGE_CONTEXT_ID
------------------------------ -------------------------------- ------------------ ------------------------------ ------------- ----------- ---------- ---------- ---------- ----------------
T_ASSOCIATIVE                  9A18FE6BCB72110F39CED9E08B932ECB ASSOCIATIVE ARRAY  MY_TYPES                       PACKAGE       DECLARATION          4          4          8                1
T_TABLE_TYPE                   77067FE9732B492C166D38221DC3DF37 NESTED TABLE       MY_TYPES                       PACKAGE       DECLARATION          3          3          8                1
T_CURSOR_TYPE                  EDEC9260784B7721BC3F3DAB293F23DD REFCURSOR          MY_TYPES                       PACKAGE       DECLARATION          2          2          8                1

zep@dev>

Comment: Great find! 11g keeps coming up with features I have not looked through yet; our systems are still on 10g.
Answer: EDIT: From documentation found by Zep, statement only applies oracle versions prior to 11.1
The bad news for you:
Those types only exist upon execution of the package, so they would never be seen in the oracle dictionary. In other words, those types are not in the database, rather they are in the package.
The good new for you:
The package is in the data dictionary so we could run some sql to search procedures and packages that contain the keywords you care about; in this example 'TYPE':
SELECT *
FROM dba_source
WHERE type IN ('PROCEDURE','PACKAGE','PACKAGE BODY')
AND text LIKE '%TYPE%IS%';

From there define the owner you care about and the do some regex or other fancy code to pull the data you need.
Alternatively, if you want to see the types in the data dictionary you would have to create them outside the package and then reference that type from the package.
References:
Oracle® Database Reference 11g Release 1 (11.1) - ALL_TYPES
Comment: That's what I was afraid of. Unfortunately, parsing the source is not robust enough for me...
Comment: So I guess, what I'm trying to do here http://stackoverflow.com/questions/6410452/fetch-oracle-table-type-from-stored-procedure-using-jdbc (reading package-level table types) is not possible either?
Comment: @Lukas Eder - You will be at a loss then. Think of it in another way - when you "formally" discover values from SYS.ALL_TYPES, you are querying the database, just as you would with sql from user_source.
Comment: OK thank you for the bad news :) Do you happen to know the answer for my other question (from my earlier comment?)
Comment: @Lukas Eder - That's right, unless someone can come along and redefine what I understand about oracle, types in procedures and packages are created at run time and then subsequently destroyed once the process has finished. They only ever exist in memory (ram) and never end up in the data dictionary (hd).
Comment: @Lukas Eder - which question was that?
Comment: @Joel: This one: http://stackoverflow.com/questions/6410452/fetch-oracle-table-type-from-stored-procedure-using-jdbc
Answer: Complex solution that works without any special compilation flags
Here's a solution that I've devised for the jOOQ code generator in version 3.9 to discover PL/SQL RECORD types. It only discovers those types that are actually referenced:
SELECT x.type_owner, x.type_name, x.type_subname, a.*
FROM all_arguments a
JOIN (
  SELECT
    type_owner, type_name, type_subname,
    MIN(owner        ) KEEP (DENSE_RANK FIRST ORDER BY owner, package_name, subprogram_id, sequence) owner,
    MIN(package_name ) KEEP (DENSE_RANK FIRST ORDER BY owner, package_name, subprogram_id, sequence) package_name,
    MIN(subprogram_id) KEEP (DENSE_RANK FIRST ORDER BY owner, package_name, subprogram_id, sequence) subprogram_id,
    MIN(sequence     ) KEEP (DENSE_RANK FIRST ORDER BY owner, package_name, subprogram_id, sequence) sequence,
    MIN(next_sibling ) KEEP (DENSE_RANK FIRST ORDER BY owner, package_name, subprogram_id, sequence) next_sibling,
    MIN(data_level   ) KEEP (DENSE_RANK FIRST ORDER BY owner, package_name, subprogram_id, sequence) data_level
  FROM (
    SELECT
      LEAD(sequence, 1, sequence) OVER (
        PARTITION BY owner, package_name, subprogram_id, data_level
        ORDER BY sequence
      ) next_sibling,
      a.type_owner,
      a.type_name,
      a.type_subname,
      a.owner,
      a.package_name,
      a.subprogram_id,
      a.sequence,
      a.data_level,
      a.data_type
    FROM all_arguments a
  ) a
  WHERE data_type = 'PL/SQL RECORD'
  GROUP BY type_owner, type_name, type_subname
) x
ON (a.owner, a.package_name, a.subprogram_id)
= ((x.owner, x.package_name, x.subprogram_id))
AND a.sequence BETWEEN x.sequence AND x.next_sibling
AND a.data_level = x.data_level + 1
ORDER BY x.type_owner, x.type_name, x.type_subname, a.sequence
;

More details about the above technique can be found here.
Relatively easy (but incomplete) solution that depends on a special compilation flag
I've just discovered this extremely interesting website, which lists a query that uses the dictionary views mentioned in zep's answer here. Using the package from the question, use this query:
WITH plscope_hierarchy
        AS (SELECT line
                 , col
                 , name
                 , TYPE
                 , usage
                 , usage_id
                 , usage_context_id
              FROM all_identifiers
             WHERE     owner = USER
                   AND object_name = 'MY_TYPES'
                   AND object_type = 'PACKAGE')
SELECT    LPAD (' ', 3 * (LEVEL - 1))
       || TYPE
       || ' '
       || name
       || ' ('
       || usage
       || ')'
          identifier_hierarchy
  FROM plscope_hierarchy
START WITH usage_context_id = 0
CONNECT BY PRIOR usage_id = usage_context_id
ORDER SIBLINGS BY line, col;

Yielding this result
PACKAGE MY_TYPES (DECLARATION)
   REFCURSOR T_CURSOR_TYPE (DECLARATION)
   NESTED TABLE T_TABLE_TYPE (DECLARATION)

Unfortunately, the nested table type is not resolved any further.
Answer: It won't be available from all_types or all_objects but as an alternative you can query user_source for the name of the type, and it should show up.
Comment: Thanks. Unfortunately, that's not going to be precise enough for me. I'll update my question...
Answer: Nowadays (18c+ IIRC) there are all_plsq_* views that do the job. For record types see user_plsql_types and all_plsql_type_attrs.
Answer: Try to use collection as an argument of package function/procedurę and next see into dictionary user_arguments:
select *
  from user_arguments a
 where a.object_name = 'MYFUNCTIONNAME'
 order by a.sequence;


The exponential function and one-parameter subgroups

Question: In Geometrical Methods for Mathematical Physics, Schutz speaks of a left-invariant vector field $\bar{V}$ at the identity $e$ of a Lie group $G$.  He writes:

Unlike an arbitrary vector field, $\bar{V}$ is determined completely by $\bar{V}_e$, so we can denote the points of $G$ on this curve by $$g_{\bar{V}_e}(t)=\exp(t\bar{V}_e).$$  Because exponentiation has, by definition, the property $$\exp(t_2\bar{V})\exp(t_1\bar{V})|_e=\exp\left[(t_1+t_2)\bar{V}\right]|_e,$$ the points on these integral curves form a group ...

It is my understanding, however, that exponentiation does not necessarily have the sum-to-product property when dealing with vectors/matrices.  What is the deeper reason behind Schutz making this claim?
Comment: If I understand you the "sum-to-product" property you mean when does $e^{\mathbf{X}}e^{\mathbf{Y}} = e^{\mathbf{X} + \mathbf{Y}}$, well it happens when $[\mathbf{X},\mathbf{Y}] = 0$, and look at $[a\mathbf{X}, b\mathbf{X}]$ for scalars $a,b$.
Comment: @Nadiels Correct.  Please expand this and submit as an answer!
Answer: Well it is true that $\exp(X)\exp(Y)$ in general is not
$\exp(X+Y)$. In fact you have a very well known formula that is the Baker-Campbell-Hausdorff Formula that states
$$\exp\left(X\right)\exp\left(Y\right)=\exp\left(X+Y+\frac{1}{2}\left[X,Y\right]+\frac{1}{12}\left(\left[X,\left[X,Y\right]\right]+\left[Y,\left[Y,X\right]\right]\right)...\right)$$
As you can see since the brackets are linear and $\left[X,X\right]=0 $you have
$$\exp\left(t_1X\right)\exp\left(t_2X\right)=\exp\left(t_1X+t_2X+\frac{t_1t_2}{2}\left[X,X\right]+...\right)=\exp\left(t_1X+t_2X\right)$$
Another way of seeing the same thing in this particular case is just writing the series of the exponential and grouping $t_1,t_2$.


Find perfect finite group whose quotient by center equals the same quotient for two other groups and has both as a quotient

Question: Let $G_1$ and $G_2$ be two finite perfect groups such that $G_1 / Z(G_1) \cong G_2 / Z(G_2)$. Then there exists a finite perfect group $G$  and subgroups $Z_1, Z_2 \le Z(G)$ with
$$
 G / Z(G) \cong G_i / Z(G_i) \quad
 \mbox{and} \quad
 G / Z_i \cong G_i, \quad i = 1,2.
$$
I want to construct the group $G$. I tried to start from the direct product $G_1 \times G_2$ and factor out an appropriate normal subgroup, but everything I try does not give the desired properties. And also I have no idea how to incorporate the assumption that $G_1$ and $G_2$ are perfect. So any hints on this exercise?
This exercise is taken from the book Theory of finite groups by H. Kurzweil and B. Stellmacher, and appears there on page 36.
Comment: I think the pullback of $G_1$ and $G_2$ should work... I'll try to prove it and get back to you
Comment: The pullback itself might not be perfect, but its commutator subgroup is.
Answer: This is just an outline argument, not a full solution.
Let $X=  G_1/Z(G_1) \cong G_2/Z(G_2)$ and let $\rho_1$, $\rho_2$ be the associated epimorphisms $\rho_1:G_1 \to X$ and $\rho_2:G_2 \to X$.
Now let $H = \{ (g_1,g_2) \in G_1 \times G_2 \mid \rho_1(g_1) = \rho_2(g_2) \}$ and put $G = [H,H]$. Note that $G$ is perfect by 1.5.3 in the book, and $G$ projects onto both $G_1$ and $G_2$.
Comment: PS: $H$ is called fibre product of $(G_1,\rho_1)$ and $(G_2,\rho_2)$ and is the pullback mentioned in "HallaSurvivor"'s comment.
Answer: Here is a more elaborated sketch of the answer to this question.

Write $\bar G_i=G_i/Z(G_i)$ and let $\theta\colon \bar G_1\to\bar G_2$ be an isomorphism. Consider the following commutative diagram

Define
$$
  H = \{\phi_1\circ\rho_1=\varphi_2\circ\rho_2\}\leqslant G_1\times G_2.
$$

Observe that
$$
  H = \{(x_1,x_2)\mid \theta(\bar x_1)=\bar x_2\}.
$$

Note that
$$
  [(\bar x,\theta(x)),(\bar y,\theta(\bar y))]
        = ([\bar x,\bar y],\theta([\bar x,\bar y])).
$$

Put $Z=Z(G_1\times G_2)$. Then $H/Z$ (the graph of $\theta$) is perfect.

$G=H'$ is perfect.

$G\cap Z=Z(G)$.

The (restriction of the) projections $G\to G_i$ are epi.

Define $Z_i=\text{ker}(G\to G_i)$. Then $Z_i\subseteq Z(G)$.


Converting a python script to exe

Question: I am trying to convert a python file to exe, by following this website:https://datatofish.com/executable-pyinstaller/
I think I have done everything right, I have added the python to PATH and installed pyinstaller using "pip install pyinstaller" which I got the response "Succesfully installed". My script is named "draw.py" and it is on Desktop, so I change the directory and then run "pyinstaller --onefile draw.py" but I get the result "'pyinstaller' is not recognized as an internal or external command,operable program or batch file."
I am sure that I have installed the pyinstaller, I am also sure that I have added python to PATH, because it starts up when I type "python" in command prompt.
I dont know where the problem is, I did everything the website told me to do correctly, I am pretty sure that I have missed something even though I did everything correctly.
Answer: try auto-py-to-exe its more user friendly and you can install it with  pip install auto-py-to-exe and then restart terminal/cmd and type auto-py-to-exe and its GUI based so its super easy to use you just need python and a browser installed.
I hope this helps and btw it uses pyinstaller to generate exe so if your package is installed correctly then there should not be any problem
Comment: Thanks for your suggestion, I will try it out if I fail with my current plan. Thanks for your reply!
Answer: Are you sure to add pyinstaller to the environment variable? If yes, try "pyinstaller - F [fileName]. py".
Through your question, I learned that your shell did not read a program named pyinstaller. Have you carefully checked whether pyinstaller was added to the environment variable?
Comment: Yeah, thats something I forgot and thats also probably the reason of my problem. Thanks for your reply!

Converting a python script to exe


Why has the Perfect cuboid problem not been solved yet?

Question: Why hasn't   Perfect Cuboid  Problem  been solved yet, whereas (possibly) more nontrivial ones such as FLT and Sphere packing have been solved?
I understand that calling some problems more nontrivial may be naive and seemingly trivial problems can be deceptively tricky, as with the FLT. All the same, FLT and to a lesser extent, Sphere Packing, garnered lots of attention by successive generations of mathematicians, until someone decided to finish it off and succeeded.
But, AFAIK, the Perfect Cuboid (PC) problem hasn't generated this kind of attention, perhaps because Fermat didn't leave a note about it. Is that the reason for PC remaining unsolved? One of the standard references for PC ,  Unsolved Problems in Number Theory , suggests several numerical results (p.178), but of course nothing like a proof, much like the status of  FLT and Sphere Packing many decades ago.
Comment: Another reason is that it is not a particular interesting problem from a mathematical perspective, its just a random diophantine system. Its truth value has probably no implications.
Comment: There aren't many implications of Fermat's Last Theorem, either.
Comment: FLT is probably famous because of the little note, rather than its use. At the same time, the proof of FLT proved the Taniyama-Shimura conjecture. So it was useful
Comment: @Ganesh: I see the individual tallies (this is one of the reputation-dependent "privileges"). The two people who upvoted it must have retracted their upvotes.
Comment: In the newer edition of Guy's book, he deals with Euler brick and related problems in pp. 275–283. Google books [link](http://books.google.com/books?id=1AP2CEGxTkgC&pg=PA275).
Comment: I think it is much harder to tell people about that problem, so it is going to get less attention.
Comment: @solomoan: How many 'useful' theorems are number theorists after these days? It's in the journey, IMHO.
Comment: @Alex: there are a lot more number theorists compared to Fermat's time, they are probably after a MUCH wider range of problems.
Comment: Why the downvotes? Isn't the question clear?
Comment: @Ganesh: There are no downvotes; the tally is 0 up, 0 down.
Comment: @joriki , No, it went up to +2 votes about an hour ago before someone downvoted it twice.
Comment: @solomoan, Why do you say it could be undecidable?
Comment: @solomoa, I guess you meant independent of ZFC? What is the reason for supposing that?
Comment: No solution to a perfect cuboid please see below; https://math.stackexchange.com/questions/2662452/can-anyone-verify-or-discredit-my-proof-of-no-solution-to-the-perfect-cuboid-pro
Comment: No perfect cuboid exist please see below; https://math.stackexchange.com/questions/2662452/can-anyone-verify-or-discredit-my-proof-of-no-solution-to-the-perfect-cuboid-pro
Answer: If I were to take a guess, I'd suggest that the reason it hasn't been solved yet is because there's not any apparent practical application, and nobody's put a bounty on it that's large enough to make it worth anybody's trouble to solve.
Comment: Bounties and practical applications are not at all what motivate most mathematicians to attack a given problem.
Comment: Hey, I just said it was a guess.  Maybe I'm just being a bit cynical, but I sincerely think if a sizable bounty was put on this problem, I think it'd be solved (one way or the other) in under a year.
Comment: Since when do mathematicians, especially number theorists, care about practical applications?
Comment: Since the invention of RSA and elliptic curve cryptography.
Comment: RSA involves only elementary mathematics, and elliptic curve cryptography is one very small slice of mathematics. There are many more interesting topics in number theory, and most mathematicians--- even in number theory--- are not in it for the practical applications.
Answer: It's just not a particularly interesting problem. Apart from the romantic (and ridiculous) idea that Fermat had a secret proof of his conjecture that was lost to history, there's not much compelling about FLT aside from the fact that it's very easy to state. What makes it interesting is that Frey proved that given a nontrivial rational point on the curve $x^n + y^n = z^n$ with $n > 2$, he could construct a elliptic curve $E/\mathbb{Q}$ that isn't modular. That would be significant; it ties into Taniyama-Shimura, the Hasse-Weil conjecture, the Langlands program, and so on. Without it, FLT would just be another arbitrary Diophantine equation with mild historical interest, relegated to amateur and recreational math. The brick problem is not as elegant as FLT and doesn't seem to tie into anything more significant, so it's not a topic of ongoing research.
Comment: Indeed. For me, it's interesting because this is the kind of problem that seems to be easy -  until I hear about the failure of past attacks.
Comment: But any sort of Diophantine equation is hard to solve; this particular one just doesn't have any interesting connections with number theory, algebraic geometry, etc.
Comment: True, but it has geometric roots.
Comment: Not really. The geometry involved is trivial and not particularly compelling.
Comment: Agreed. I merely pointed out that the problem has geometric roots, unlike a random diphantine system.
Answer: It seems that the perfect cuboid problem is one approaching the difficulty of, if not more difficult than solving Fermat's Last Theorem which took Andrew Wiles many years of his career and the use of extrememly complex mathematics to do.
However, if the problem is approached logically there seems to be a good reason why one can't exist. I was never one to believe that a seemingly impossible problem should be tackled by using computer programs to try as many combinations as possible just to see if a solution could be found. To me that admits defeat and actually proves nothing unless a set of figures that proves the case is found which for a perfect cuboid hasn't
happend to date.
I believe there is an answer and it's been staring us in the face but been ignored. Take the three face diagonals of a cuboid with dimensions X, Y Z. The three face diagonals are given by:-
X^2 + Y^2 = D1^2;
  X^2 + Z^2 = D2^2;
  Y^2 + Z^2 = D3^2;
We need go no further because all we need do is look at Y^2 and think about symmetry.
Comment: And so the answer is....


Issues with make jQuery ajax call to a rest api

Question: I am trying to use jquery ajax to make a post request that returns some response but it does not seem to work properly. Sometimes it works after a long wait, other times it does not work at all.This is my code.
<code>
<script type="text/javascript">

const id =  $('#auth').val();
  $("#set").click(function(){
    $('.spinner-grow').show();

  $.post("https://ravesandboxapi.flutterwave.com/v2/gpx/transactions/escrow/settle",
  {
    id: id,
    secret_key: "FLWSECK-25*******************0628-X"
  },

  function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
    $('.spinner-grow').hide();
  });
});

</script>
</code>
what might be the issue here?
Comment: Open developer console (`F12`). What do you see in the Network tab when it "does not work at all"?
Comment: ````Failed to load resource: the server responded with a status of 404 (Not Found)```` but there is no problem with it working with postman or codepunker
Comment: Is there any `body` coming back with that error? For example, many endpoints return `404` if the entity you're trying to access does not exist; perhaps your `id` is incorrect. Or, it can mean that the URL is incorrect. And are you sure that the endpoint is a `POST`?
Comment: yes, i just saw this ````jquery.min.js:4 POST https://ravesandboxapi.flutterwave.com/v2/gpx/transactions/escrow/settle 400 (Bad Request)````
Comment: A `400 Bad Request` means that you've sent bad data - either it was malformed, or the data was wrong. Confirm that your "Request" body was what you expect.
Comment: Well i think my problem is with the way i am getting the id from the form ````const id =  $('#auth').val();````. I inserted the value directly in the code and it worked
Comment: Without HTML we can't really help with that unfortunately.
Answer: The following is a complete example of a AJAX post is handled via jQuery. Just sharing it for reference.
<code>    var url = "api/path/to/your/controller";
    var data = {};
    data.id = id;
    data.secret = 'your-secret';
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function (resultObject) {
            console.log(resultObject);
        },
        error: function (err) {
            console.log('An error occured');
        }
    });
</code>
If you are getting a 404 error, better check the path to the API. You can use a tool like Postman to validate your requests and then try implementing the same in code.
Also make sure that there is no CORS issue as from your code we cannot understand if your post is triggered from the same domain.
Comment: While this is helpful, it isn't an *answer*. Suggestions, requests for more information, guesses, etc, should be *comments*. If you need to include code alongside a comment, consider an external resouce (JSFiddle, etc).


Why does this Coq Definition fail? Coq Namespace error for Inductive Type

Question: I have the following Inductive Type and a test function:
Inductive parameter : Type :=
| Nop
| OneP : forall A, A -> parameter
| TwoP : forall (A : Type) (r : nat) (b : A), parameter
.
Check (TwoP nat 1 5).

Definition test (p : parameter) : option (nat * nat) :=
match p with
| TwoP nat x y => Some (x, y)
| _ => None
end.

The test function fails with the error:
The term "Some (x, y)" has type "option (Datatypes.nat * nat)"
while it is expected to have type "option (Datatypes.nat * Datatypes.nat)".
I don't understand why my definition does not work. Is there a difference between nat and Datataypes.nat ?
Any help would be appreciated. Thanks!
Answer: In Coq, it is not possible to test what a type is.  Consider the following program:
Definition is_nat (A : Type) : bool :=
  match A with
  | nat => true
  | _   => false
  end.

If you try to run this, Coq tells you that the last branch is redundant, and rejects the definition. The issue is that nat is taken to be a variable name, not the nat data type from the standard library.  Therefore, the first branch matches every type A, and the last branch is redundant. In your example, the pattern nat ends up masking the data type nat, which is why you end up seeing the qualified name Datatypes.nat.
One way of solving this issue is to use a type of codes instead of Type. For instance:
Inductive type : Type :=
| Bool
| Nat.

Definition type_denote t : Type :=
  match t with
  | Bool => bool
  | Nat => nat
  end.

Coercion type_denote : type >-> Sortclass.

Inductive parameter : Type :=
| Nop
| OneP : forall (A : type), A -> parameter
| TwoP : forall (A : type) (r : nat) (b : A), parameter
.
Check (TwoP Nat 1 5).

Definition test (p : parameter) : option (nat * nat) :=
match p with
| TwoP Nat x y => Some (x, y)
| _ => None
end.

There are two issues with this solution. First, it requires you to anticipate all types that you will need in parameter, and add those in the definition of type. Second, it forces you to program with dependent types, which can be hard to manipulate. It might be possible to refactor your definitions to avoid the problem of type matching altogether, although there is no one-size-fits-all solution -- it depends on your application.
Comment: That was insightful, thanks.
The use case here is to split instructions into a command and a parameter part.
E.g a cmd would be JmpZero with the parameter TwoP register expr.
Do you maybe know of resources that try to do something similar?


Mailgun stats don't add up

Question: I'm trying to make sense of the mailgun stats returned from the API, and the numbers simply don't add up.
I'm making the following call
<code>$stats = $mg->get('example.com/stats/total', array(
    'event' => array('accepted', 'delivered', 'failed', 'stored'),
    'duration' => '1m',
    "resolution" => "month"
));
</code>
And get this result (re-formatted to JSON for easier reading)
<code>{
    'accepted' : {
        'incoming' : 200,
        'outgoing' : 458,
        'total' : 658
    },
    'delivered' : {
        'smtp' : 399,
        'http' : 0,
        'total' : 399
    },
    'failed' : {
        'temporary' : {
            'espblock' : 0
        },
        'permanent' : {
            'suppress-bounce' : 0,
            'suppress-unsubscribe' : 0,
            'suppress-complaint' : 0,
            'bounce' : 0,
            'total' : 0
        }
    },
    'stored' : {
        'total' : 0
    }
}
</code>
Now, what I don't understand is this: Shouldn't the sum of delivered messages + failed messages + stored messages be greater than or equal to the number of accepted outgoing messages? In this example they are quit significantly lower, and that doesn't seem to make sense to me.
I would appreciate any thoughts on this.
Answer: Normally, accepted outgoing messages are equal to the sum of delivered messages + bounces messages.
Try using the Date (something like start/end) rather than the duration/resolution.
That's what I used in my cron, and it worked. You can go to the dashboard of Mailgun to check and compare with your results
Comment: Thanks, but that doesn't change anything for me: Different time range, but I still get 771 outgoing accepted, 696 delivered, 2 temp fails, 1 permanent fail, leaving 72 completely disappearing. I might open a ticket with them ...
Comment: Almost 10% disappearing, it's a bit weird. I remember when I used my cron, I had only 0,25% of disappearing messages, with a last state of 'accepted'. The ticket might be the solution ;)
Comment: Thanks. I was thinking I might have misunderstood something that might be common knowledge ..

Mailgun stats don't add up


Flutter: save a generated PDF from preview page with a custom function

Question: I'm using the pdf package (link to pub.dev page) to create documents inside my app.
How can I save the document from the preview page?
I know about the built-in print  and share functions, but I need something else.
I need a function that does the following:

save the document to the device
upload it to Firebase Storage
delete the local file

I know how to do all of these steps, but I'm unable to access the Uint8List  bytes of the pdf document that is being previewed... I've tried to add a PdfPreviewAction, but I still can't access the bytes of the document from it
Answer: all you have to do is call the save method in the Document() object
import "package:pdf/widgets.dart" as w;
final pdf = w.Document();
Future<Uint8List> getPDFData() async {
return await pdf.save();
}

you can do all the operations using the getPDFData() method
for example:
return PdfPreview(
        build: (format) => getPdfData(),
        actions: [
          PdfPreviewAction(
              icon: Icon(Icons.print),
              onPressed: (context, fn, format) {
                //if you call fn(format), you'll get Future<UInt8List>
              }),

        ],
      );

Comment: Ok thanks, but I'd like to call this function from a `PdfPreviewAction`, which is the third icon button in the bottom bar. Is that possible?
Comment: what is PdfPreviewAction? is it your own class?. this library doesn't has a class named PdfPreviewAction in my knowledge.
Comment: It's part of the `printing` package. Here's the [documentation](https://pub.dev/documentation/printing/latest/printing/PdfPreview-class.html) for it
Comment: try this,  PdfPreviewAction(
                  icon: Icon(Icons.print),
                  onPressed: (context, fn, format) {
                    print(fn(format));
                  })
Comment: I'll try and let you know... Thanks


Unexpected output of .sort() method in python

Question: I'm getting a 'None' result when I'm trying to sort a list and returning it to itself, why is that?
performing simply '.sort()' of the list yield the correct output.
>>> letters=['b','d','a']

>>> letters=letters.sort()

>>> print(letters)

None
>>> letters=['b','d','a']

>>> letters.sort()

>>> print(letters)

['a', 'b', 'd']

Comment: `sort` is sorting the elements in-place. You are just overwritting `letters` with `None`
Comment: The second sentence is the answer for the question.
Answer: The .sort() method doesn't return anything. You might be looking for sorted():
letters = sorted(letters)

Comment: Alright- I found -->  The primary difference between the list sort() function and the sorted() function is that the sort() function will modify the list it is called on. The sorted() function will create a new list containing a sorted version of the list it is given.
Comment: Yep, that's correct. So you probably either want `letters = sorted(letters)` or `letters.sort()`.
Answer: yes that is confusing, but I guess it is to make clear, that the list is sorted instead of just returning a sorted copy of the list.
So instead of
letters=letters.sort()

Just write:
letters.sort()

The list is sorted in-place. I also think that it would have been better to return self instead of returning nothing in this case, as this would make chaning possible and in some cases you would not even have to assign the list to a variable (e.g. if you call a function and need to pass the list sorted, you could otherwise just make a copy in the function call and call sort on it).
Answer: In the first example, you're setting letters to the return value of letters.sort(). But letters.sort() just rearranges the values and does not actually return anything, so you're storing None.
In the second example, you are doing it correctly, because .sort() is a list method that rearranges the values in it.
Answer: You can use sorted for your goal.
In [1]: letters=['b','d','a']

In [2]: letters = sorted(letters)

In [3]: letters
Out[3]: ['a', 'b', 'd']

https://docs.python.org/3.7/library/functions.html#sorted
list.sort(key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).

list.sort return None
Why does "return list.sort()" return None, not the list?


Reduce PDF file by factor of two

Question:
Possible Duplicate:
Reduce filesize of a scanned PDF
How to reduce pdf filesize?

I have completed my thesis. I used LaTeX to write and the total output size of my PDF is ~100MB. It contains a lot of figures(jpg photos, tikz pictures, pdf images), hyperlinks, references etc.
My university requires to upload my thesis in a PDF file at maximum 50 MB. So I need to reduce my thesis-in size-without losing any quality. Is there a way to do so?
Comment: This is certainly not Ubuntu-specific, and has been asked before here: http://tex.stackexchange.com/questions/14429/pdftex-reduce-pdf-size-reduce-image-quality Who knew there was a whole stackexchange site dedicated to LaTeX?
Comment: @roadmr:Thank you very much for your comment. Actually what I need is a way to reduce the already built PDF. I believe it would be much easier at that point, than to try to understand how to built it smaller.
Answer: This requires some thought as to exactly what is going on here and what you are trying to do.
You are probably aware that there are essentially 2 types of PDF document, those where the text is ready rendered and those where it is not in which case what you actually have is a fancy image of the page.
So if it is of ready-rendered type (its an images essentially), you could get some mileage by converting to real images, reducing the definition of those images and converting back to pdf. the convert program will do this.
You should also (in 12.04 or greater) put pdf into synaptic. You'll get a slew of utilities including the following, some may be useful for what you need. Perhaps  pdfedit, pdftk, poppler-utils, pdfmod, qpdf,
What if your pdf is of the other font-based variety? Of itself I am not sure there is much you can do here. After all, information is or should be stored efficiently in such a situation, so its not clear what you could do without loosing any of it.

Reduce PDF file by factor of two