Task of the guy who reads the score and sits next sound technician at movie

Question: Yesterday I was at a Lord of the Rings Live concert and I was wondering, what these two guys were doing. They were sitting next to the sound technician and reading the score and they were wearing headphones and had some kind of controller in their hands. Although they had a monitor like the conductor. Are they checking the speed? Or if everyone is right, or do they regulate the mix, especially of the solists? I don't know so I'm aksing you!
"A little hope ist left", Galadriel
Comment: I guess they can read the score and tell their colleague when a certain point in the music is reached when some audio/video/... setting has to be changed. It might be too difficult for one person to read a score and control other things at the same time.
Answer: Stage lighting would be my guess.
Comment: The lighting tech has their own console, and is usually controlled from a booth.
Comment: Stage light ist in a Room above as far as I know.
Answer: IF he's reading the score, probably the Sound Assistant or A2.  In theater you usually have a tech "on book" with the score to coordinate upcoming cues, sync with the conductor and most importantly let the Sound Engineer know when there are specific sound cues to mark. If there are sound effects that the orchestra isn't providing then they are cued from the console.
The controller he was using could be for the monitors, working the back of house sound which is what the A2 usually does. He also could be on the effects cues while the other engineer mixed the Front of House sound.
Edit* read the question again, realized it was two people AND a sound engineer. They could still be doing the Sound Assistant job together.
Comment: This sounds logical :D


Post hoc test with heteroscedasticity

Question: I ran an Anova using the car package.
y~A*B+C
A has 2 factor levels, B has 3 factor levels, and C has 2 factor levels (data from two data sets was included in the analysis. C is Experiment 1/2)
I found that there is a significant effect of A and B but no significant interaction. I now wanted to do post hoc comparisons among the levels of factor B separately for each level of factor A, which is of interest to me. I did another Anova for each level of factor A (y~B+C) and now want to do a post hoc comparison to see differences among the 3 levels of factor B under each condition. However, there is clear heteroscedasticity in the residuals, which I corrected for using the white.adjust argument in the Anova. Is there a post hoc test that can be used for heteroscedastic data in e.g. the emmeans package?
Should I rather use weighted regression (e.g. gls) to resolve the heteroscedasticity issue and then do e.g. tukey adjusted comparisons?
Any advice is much appreciated!
Answer: Yes. That is what you should do. emmeans() summarizes model results, so you need to hand it a model that you trust. See also the FAQ vignette in the emmeans package, which also discusses this matter.
Comment: Many thanks for your fast reply. I assume you mean that I should rather go with weighted regression to fix the heteroscedasticity issue, as I cannot include a heteroscedasticity "correction" in the post hoc test, as I did for my Anova model. I will try a weighted gls model. Maybe that does the trick. I know that the games-howell post hoc test can be used for data that is heteroscedastic. Are there other post hoc tests? (more out of curiosity)

Post hoc test with heteroscedasticity


Finding tight bound for a function

Question: Suppose we are given a rational function $f(n)=\frac{p(n)}{q(n)}$ and need to find g(n) that satisfies: $f(n) \in \Theta(g(n))$. Does g(n) need to be one of $n^c, log n, n log n$ or can it be, in this case $g(n)=f(n)=\frac{p(n)}{q(n)}?$
Answer: Certainly if $g(n)=f(n)$ you have $f(n) \in \Theta(g(n))$  It is true, but often it will miss the point.  The idea is to have $g(n)$ look like one of the things you cite to get an idea how it compares to other problems.
Answer: Of course $f(n) \in \Theta(f(n))$ by definition of $\Theta$, but usually what the question is asking for is a simple function $g$. In this case you would only need the most significant term from the top and the bottom each. The proof of that goes like this:
For any $p(n) = a(n)+b(n)$ and $q(n) = c(n) + d(n)$ where $b(n) \in o(a(n))$ and $d(n) \in o(c(n))$ and $a(n) \ne 0$ and $c(n) \ne 0$,
  $\frac{p(n)}{q(n)} = \frac{a(n)}{c(n)} (1+\frac{b(n)}{a(n)}) (1+\frac{d(n)}{c(n)})^{-1} \in \frac{a(n)}{c(n)} (1+\frac{b(n)}{a(n)}) (1-O(\frac{d(n)}{c(n)}))$
  $\in \frac{a(n)}{c(n)} (1+o(1)) (1+o(1)) = \frac{a(n)}{c(n)} (1+o(1)) = \Theta(\frac{a(n)}{c(n)})$
Comment: The method you outlined works when $p(n)$ is of higher degree than $q(n)$, in which case $g(n)$ is of the form $n^c$. However, when the converse is true, $g(n)$ is of the form $\frac{1}{n^c}$ which would mean $f(n) \in \Theta(\frac{1}{n^c})$ which is counter intuitive since it would mean that efficiency gets better as input size increases. Does the method you outlined work in this case? Thanks!
Comment: The proof works for any functions that satisfy the conditions I gave, including non-polynomials. The definition of $\Theta$ only requires the ratio tending to a non-zero constant, and so although algorithms never have complexity less than $\Theta(n)$ if $n$ is the input size, it is not contradictory. For example as $x \to \infty$, $\frac{1}{x}-\frac{1}{x+1} = \frac{1}{x^2+x} \in \Theta(\frac{1}{x^2})$. Also, $\frac{1}{x}-\frac{1}{x+1} = \frac{1}{x^2} - \frac{1}{x^3+x^2} \in \frac{1}{x^2} + \Theta(\frac{1}{x^3})$. You can get a free graphing software from http://www.padowan.dk/ to plot these.
Comment: Thanks. I already have graphing software and I used it to verify that my choice of g(n) did bound f(n).

Finding tight bound for a function


Ruby Range test case SyntaxError

Question: I'm running Ruby's Range test case but got an error in the line:
assert_equal([*"a".."z", "aa"], ("a"..).take(27))

The error message is:
test/ruby/test_range.rb:38: syntax error, unexpected ')'
...qual([*"a".."z", "aa"], ("a"..).take(27))
...                              ^

I also tried ('a'..) in irb and got a SyntaxError:
irb> ('a'..)
Traceback (most recent call last):
        1: from /home/sergioro/.rvm/rubies/ruby-2.5.3/bin/irb:11:in `'
SyntaxError ((irb):9: syntax error, unexpected ')')
('a'..)

My question is what is ('a'..)? This syntax is used several times in the test case but it is not described in Range's documentation.

Update:
Installing Ruby version 2.6.4 solved the error for ('a'..). But there is an error if the endless range is on the left, like in test_range.rb:536:
assert_operator(..'Z', :===, 'ANA')

The error is:
test_range.rb:536: syntax error, unexpected .., expecting ')'
    assert_operator(..'Z', :===, 'ANA')

Answer: ('a'..) is endless range introduced in ruby 2.6. You need to install Ruby 2.6  and run this test on ruby 2.6.
Comment: Endless range without begin seem not to be supported in 2.6: https://ruby-doc.org/core-2.6.4/Range.html#class-Range-label-Endless+Ranges. Note that you link tests to trunk ruby, not to 2.6
Comment: please add the line with left endless range
Comment: I included the new error in the question and accepted this answer for my original question.

Ruby Range test case SyntaxError


Git server side hooks commit update file

Question: I am trying to implement git hook commit on a server using these instructions: https://git-scm.com/book/be/v2/Customizing-Git-An-Example-Git-Enforced-Policy
I created file "update" (make it executable, put in hooks folder), and found that this file is not executed when I do git commit -m "my message" and git push - all these commands work perfectly, but 'update' file is not working (I tried with simple script "echo "Hello World" >> somefile.txt)
Where is the problem?
Comment: Have you put the hook into the remote (server) repository? And certainly, update hooks works only on the remote side, on receiving commits from a client, so `git commit` isn't involved at all.
Comment: Yes, I put hook "update" file on server, but it does not work when I "git commit"!
Comment: `git commit` is a completely _local_ operation. It doesn't involve any network transaction, only `git push`, `git fetch` and `git pull` which is essentially `git fetch` + `git merge` do.
Comment: What exactly are you trying to achieve with the hooks?
Comment: I want to implement that all commits begin with ticket numbers: "TicketNumber-1234"

I understand that "git commit" is local one. When I do "git push", my "update" script is not running at all.

My repo path is /path/test.git/hooks - there I put "update" file.
Comment: I found similar ticket http://stackoverflow.com/questions/4372306/git-push-over-http-not-activating-remote-hooks  = same case as I described above. But when I switched to SSH protocol, it does not work anyway.
Answer: As commented above, the update hook is a server side hook
It needs to be on the server you are pushing to, in the bare repo: repo.git/hook/update
That explains why a git commit (local operation) does not trigger the server side hook.
For the rest, see the follow-up question.


how to move an object from one array to another

Question: I understand that this topic has been done before but I wanted to bring it up again for a specific reason, I have a function designed to move an item from one array to another, removing the item from the array it was originally in, but whenever I test it, it doesnt seem to work
<code>-(void) moveOpperand: (NSMutableArray *) moveFrom :(NSMutableArray *) moveTo{
    NSString *opperandObject = [moveFrom lastObject];
    if (opperandObject) {
    [moveTo addObject:moveFrom.lastObject];
        [moveFrom removeLastObject];
    }
}
</code>
the method above is called in this method
<code>-(NSMutableArray *) giveHand: (NSMutableArray *) hand : (NSMutableArray *)refrenceDeck{

    for (int i=0; i<6; i++) {
        [self moveOpperand:refrenceDeck :hand];
    }
    return hand;
}
</code>
the error when testing seems to be located when I try to implement the currentHand method
<code>-(NSMutableArray *) currentHand{
    if (_currentHand == nil) {

        self.currentHand = [self.myDeck giveHand:self.currentHand :self.myDeck.currentDeck];

    }
    return _currentHand;

}
</code>
Comment: how, specifically, does it not work?
Comment: Please state the problem. Why does it 'seem' not to work?
Comment: As Dustin said what is your goal and how is they not fulfilling it?
Comment: my goal was to remove an object from the movefrom and add it to the moveto, however it doesnt seem to modify either of the NSMutableArrays passed to it
Comment: Could you show the code where you're calling this?
Comment: I tried your code in a very simple case and it seems fine.  E.g.: `NSMutableArray *fromArray = [NSMutableArray arrayWithObjects:@"The string", nil];
    NSMutableArray *toArray = [NSMutableArray arrayWithCapacity:1];
    [self moveOpperand:fromArray :toArray];`.
Comment: Are you passing in copies of the array?  As @RyanPoolos states, show your code.
Comment: After you've called the moveOpperand method, have you analyzed the contents of those arrays in the debugger?
Comment: after checking, it seems like the problem isnt with the first two methods, its with this currenthand method
Answer: If you're trying to do what I think here is a very very simple category that should do it for any object.
NSObject+Move.h
<code>#import <Foundation/Foundation.h>

@interface NSObject (Move)

/*
 Returns true if it sucessfully moves the object between arrays.
 */
- (BOOL)moveFromArray:(NSMutableArray *)arrayA toArray:(NSMutableArray *)arrayB;

@end
</code>
NSObject+Move.m
<code>#import "NSObject+Move.h"

@implementation NSObject (Move)

- (BOOL)moveFromArray:(NSMutableArray *)arrayA toArray:(NSMutableArray *)arrayB
{
    if ([arrayA containsObject:self]) {
        [arrayA removeObject:self];
        [arrayB addObject:self];

        return YES;
    } else {
        return NO;
    }
}

@end
</code>
Answer: It's possible that you aren't setting the <code>_currentHand</code> variable to nil at the appropriate time, thus you may not have the correct current hand which may make it 'seem' like an issue with your array swapping.


Placing T-shaped tetrominoes on a n × n board.

Question: You have an infinite stack of T-shaped tetrominoes (composed of four squares of side length 1), and an n × n board. You are allowed to place some tetrominoes on the board, possibly rotated, as long as no two tetrominoes overlap and no tetrominoes extend off the board. For which values of n can you cover the entire board?(source :Canadian Junior Mathematical Olympiad 2022 p2)
how I tried:

Since,  each tetrominoe have 4 squares, the total number of squares is even therefore n is even.

I found experimentally that 4 is the smallest value  of n. Therefore every multiple of 4 can also be a value of n(assume an 4 × 4 square to be a unit square and try to form squares with it.)

But I am not sure if every n is a multiple of 4 or there are some other forms, and by 'some other form' I mean if  n = 4k + 2.
Comment: I do not think this is possible for $n=6$.  Perhaps the reason for this can be extended
Answer: Color the board like a checkerboard.  Each T covers three squares of one color and one of the other.  An odd number of Ts then cannot cover and equal number of squares of each color, so the area must be a multiple of $8$.  This requires that the side of the board be a multiple of $4$.
Comment: Every T has odd number of black squares. Board has even number of black squares. Then number of T is even.
Comment: can you simplify this sentence further " equal number of squares of each color, so the area must be a multiple of 8".


Flutter Bottom Navigation Space Between Items

Question: I'm trying to make the bottom navbar as in the picture, but I'm having difficulty adjusting the spacing between the items. I used an image instead of an icon. And I gave the images width and height values. But when I wanted to reduce their whitespace, I couldn't achieve it. Can you help me with this?
image:

My Code:
bottomNavigationBar: !navBarHide
              ? SafeArea(
                  child: BottomNavigationBar(
                      onTap: (index) {
                        debugPrint(controller.tabIndex.toString());
                        controller.tabChange(index);
                        controller.update();
                        if (index == 0) {
                          Get.offAndToNamed('dashboard');
                        } else if (index == 1) {
                          Get.offAndToNamed('blank');
                        } else if (index == 2) {
                          Get.offAndToNamed('home');
                        } else if (index == 3) {
                          Get.offAndToNamed('blank');
                        } else if (index == 4) {
                          Get.offAndToNamed('menu');
                        }
                      },
                      unselectedLabelStyle: TextStyle(fontSize: 0),
                      unselectedFontSize: 0,
                      unselectedIconTheme: IconThemeData(
                        size: Get.width > 390 ? 24.sp : 24.sp,
                      ),
                      selectedFontSize: 0,
                      selectedIconTheme: IconThemeData(
                        size: Get.width > 390 ? 24.sm : 24.sm,
                      ),
                      selectedLabelStyle: TextStyle(fontSize: 0),
                      type: BottomNavigationBarType.fixed,
                      landscapeLayout:
                          BottomNavigationBarLandscapeLayout.spread,
                      unselectedItemColor: Colors.black,
                      selectedItemColor: Colors.orange,
                      showSelectedLabels: false,
                      showUnselectedLabels: false,
                      currentIndex: controller.tabIndex,
                      // tamamdır
                      items: [
                        _bottomNavbarItem(
                          AppAssets.card_icon,
                          '',
                        ),
                        _bottomNavbarItem(
                          AppAssets.key_icon,
                          '',
                        ),
                        _bottomNavbarItem(
                          AppAssets.home_icon,
                          '',
                        ),
                        _bottomNavbarItem(
                          AppAssets.doc_icon,
                          '',
                        ),
                        _bottomNavbarItem(
                          AppAssets.menu_icon,
                          '',
                        ),
                      ]),
                )
              : null,

Items Method:
  _bottomNavbarItem(String assetName, String label) {
return BottomNavigationBarItem(
  icon: Image.asset(
    assetName,
    width: 25.w,
    height: 22.h,
    fit: BoxFit.contain,
  ),
  activeIcon: Container(
    width: 25.w,
    height: 22.h,
    child: Image.asset(assetName),
  ),
  label: label,
);

}
}
Answer: By default if there are more than 3 items the type changes to floating. You can force it to use fixed type to place all elements equally spaced
type: BottomNavigationBarType.fixed,

Comment: The code you have written already exists in the code.
Comment: You can increase the height and width of the container which will increase the size of the icons..
Comment: I don't think this is the solution
Comment: You can either increase the size of the asset or increase the size of the container. Also if you are sure of the size you can also give `fit:BoxFit.fill`


Criar Base de Dados com FluentNHibernate

Question: É possível através do FluentNHibernate criar a base de dados assim como são criadas as tabelas? Tenho a seguinte classe:
public class Helper : IRepositorioHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory()
    {
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2012
            .ConnectionString(x => x
                .Server(".\\Servidor")
                .Database("BaseDados")
                .Username("sa")
                .Password("****"))
            .ShowSql())
            .Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
            .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
            .BuildSessionFactory();

        return _sessionFactory;
    }

    public static ISession OpenSession()
    {
        return SessionFactory().OpenSession();
    }

    public void CriaDatabase()
    {
        SessionFactory();
    }
 }

Com ela consigo criar sem problemas as tabelas mas queria saber se é possível também criar a base para facilitar ainda mais quando distribuir minha aplicação.
Answer: Obrigado pela ajuda mas não deu certo =(
Tentei o seguinte:
public class Helper : IRepositorioHelper
{
    private static ISessionFactory _sessionFactory;
    private static FluentConfiguration Config
    {
        get
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2012
                    .ConnectionString(x => x
                        .Server(".\\SQLExpress")
                        .Database("Alugo2")
                        .Username("sa")
                        .Password("sA@sqlserver"))
                        .ShowSql())
                    .Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));
        }
    }

    private static ISessionFactory SessionFactory(bool create = false)
    {
        if(create)
        {
            Config.ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false));
            //_sessionFactory = Config.BuildSessionFactory();
            Config.BuildConfiguration();
        }

        else
        {
            Config.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true));
            _sessionFactory = Config.BuildSessionFactory();
        }

        return _sessionFactory;
    }

    public static ISession OpenSession()
    {
        return SessionFactory().OpenSession();
    }

    public void CriaDatabase()
    {
        SessionFactory(true);
    }
}

Quando uso '_sessionFactory = Config.BuildSessionFactory();' gera o seguinte erro:
/*An invalid or incomplete configuration was used while creating a SessionFactory.    Check   PotentialReasons collection, and InnerException for more detail.
em FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
em Alugo.Repositorio.NHibernate.Helper.SessionFactory(Boolean create) na d:\Arquivos \Documentos\Visual Studio 2013\Projects\Fluent\Alugo.Repositorio.Nhibernate\Helper.cs:linha 38
em Alugo.Repositorio.NHibernate.Helper.CriaDatabase() na d:\Arquivos\Documentos\Visual Studio 2013\Projects\Fluent\Alugo.Repositorio.Nhibernate\Helper.cs:linha 58
em Alugo.WindowsForm.frmMain.frmMain_Load(Object sender, EventArgs e) na d:\Arquivos\Documentos\Visual Studio 2013\Projects\Fluent\Alugo.WindowsForm\frmMain.cs:linha 36*/

E quando uso 'Config.BuildConfiguration();' não dá erro nenhum mas também não gera o banco de dados
Answer: A solução seria usar o SchemaExport().Execute()
Teste o código semelhante a este a seguir:
public FluentConfiguration GetConfig()
{
    return Fluently.Configure()
        .Database(
              MySQLConfiguration.Standard.ConnectionString(
                    c => c.Server("...").Database("...").Username("...").Password("..."))
        )
        .Mappings(
              m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
        );
}

public void Export(bool script, bool export, bool justDrop)
{
    GetConfig()
         .ExposeConfiguration(
              c => new SchemaExport(c).Execute(script, export, justDrop))
         .BuildConfiguration();
}

Fonte: https://stackoverflow.com/a/7793133/194717


Inverse Laplace with formula table

Question: In my book I have to do the inverse Laplace transform of:
$$\displaystyle \frac{4 + 15 s}{s(2+5s)} = \frac{15}{5} \frac{\left(s + \frac{4}{15}\right)}{s\left(s + \frac{2}{5}\right)}$$
With a partial fraction division, I can find that one fraction is $\displaystyle\frac{2}{s}$ and the second $\displaystyle\frac{5}{5s+2}$ and I can find $e^{-\frac{2}{5}t} + 2$.
But the book states that I should get it directly by looking at the summary of usual Laplace transforms? I checked the formulas and did not find what I should be able to see.
Can someone help me to navigate the classical formula table given that equation?
Comment: The _inverse_ for $ \ \frac{2}{s} \ $ is not simply $ \ 2 \ $ .
Comment: It says $2$ in the book (time function). Would you care to expand?
Comment: Some tables give the inverse as $ \ 2 \ H(t) \ $ ; I'm taking it that if your answer wasn't acceptable, it might be because a "step function" at $ \ t  =  0 \ $ was intended.
Comment: My answer is correct, discussing my answer is NOT what I am asking. When it comes to differential equations, with or without time function the inverse laplace transform of a constant a is a/s.
Answer: Here. Formula  $14.$
Write $\frac{15 s+4}{s (5 s+2)}$
as $$\frac{15}{5} \frac{\left(s + \frac{4}{15}\right)}{s\left(s + \frac{2}{5}\right)}$$
$\alpha=\frac{4}{15};\;a=0;\;b=\frac{2}{5}$
$$3\cdot \frac{1}{\frac{2}{5}} \left[\frac{4}{15}e^0-\left(\frac{4}{15}-\frac{2}{5}\right)e^{-2/5t}\right]=\frac{15}{2}\left(\frac{4}{15}+\frac{2}{15}e^{-2/5t}\right)=2+e^{-2/5t}$$
Tbh I prefer your solution :)
Comment: awwwww thanks !

Inverse Laplace with formula table


Can you upload to S3 using a stream rather than a local file?

Question: I need to create a CSV and upload it to an S3 bucket. Since I'm creating the file on the fly, it would be better if I could write it directly to S3 bucket as it is being created rather than writing the whole file locally, and then uploading the file at the end.
Is there a way to do this? My project is in Python and I'm fairly new to the language. Here is what I tried so far:
import csv
import csv
import io
import boto
from boto.s3.key import Key

conn = boto.connect_s3()
bucket = conn.get_bucket('dev-vs')
k = Key(bucket)
k.key = 'foo/foobar'

fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(io.StringIO(), fieldnames=fieldnames)
k.set_contents_from_stream(writer.writeheader())

I received this error: BotoClientError: s3 does not support chunked transfer
UPDATE: I found a way to write directly to S3, but I can't find a way to clear the buffer without actually deleting the lines I already wrote. So, for example:
conn = boto.connect_s3()
bucket = conn.get_bucket('dev-vs')
k = Key(bucket)
k.key = 'foo/foobar'

testDict = [{
    "fieldA": "8",
    "fieldB": None,
    "fieldC": "888888888888"},
    {
    "fieldA": "9",
    "fieldB": None,
    "fieldC": "99999999999"}]

f = io.StringIO()
fieldnames = ['fieldA', 'fieldB', 'fieldC']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
k.set_contents_from_string(f.getvalue())

for row in testDict:
    writer.writerow(row)
    k.set_contents_from_string(f.getvalue())

f.close()

Writes 3 lines to the file, however I'm unable to release memory to write a big file. If I add:
f.seek(0)
f.truncate(0)

to the loop, then only the last line of the file is written. Is there any way to release resources without deleting lines from the file?
Comment: I was looking to write directly to be a little more efficient. Essentially if I write the file locally, and upload it, I'm adding uploading as an additional step, and cleaning up the local file. I don't mind having an incomplete file - I could have an incomplete file if I wrote it locally too. The system will be idempotent and either delete a file in an error state, or continue it.
Comment: Even if you could write to S3 like you want, I would not recommend it due to consistency challenges.   Why do you think it would be better to not write locally?   Would you want a partial S3 object if there was an exception or issue?   I presume not.
Answer: I did find a solution to my question, which I will post here in case anyone else is interested. I decided to do this as parts in a multipart upload. You can't stream to S3. There is also a package available that changes your streaming file over to a multipart upload which I used: Smart Open.
import smart_open
import io
import csv

testDict = [{
    "fieldA": "8",
    "fieldB": None,
    "fieldC": "888888888888"},
    {
    "fieldA": "9",
    "fieldB": None,
    "fieldC": "99999999999"}]

fieldnames = ['fieldA', 'fieldB', 'fieldC']
f = io.StringIO()
with smart_open.smart_open('s3://dev-test/bar/foo.csv', 'wb') as fout:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    fout.write(f.getvalue())

    for row in testDict:
        f.seek(0)
        f.truncate(0)
        writer.writerow(row)
        fout.write(f.getvalue())

f.close()

Comment: For Python 2, be sure to use `StringIO.StringIO()` instead of `io.StringIO()`, else you will receive an encoding error
Comment: @inquiring minds, that's a good answer. My use case is almost like yours, only the difference is rather than csv, I want to generate an XML. As I like to use templating options like Mako/ genshi for xml-generation, can u suggest me a way how to deal with it? (Generating and writing simultanously, rather than local write first)
Comment: Binary mode is not needed for StringIO, changing mode from 'wb' to 'w' fixed the issue for me.
Comment: Is there a way to do this using boto3 only? It looks like boto3 supports 'multipart uploads' with native methods: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/create_multipart_upload.html
Answer: According to docs it's possible
s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))

so we can use StringIO in ordinary way
Update: smart_open lib from @inquiring minds answer is better solution
Comment: I don't understand how to use this. Isn't /tmp/hello.txt a local file, which is what we're trying to avoid?
Comment: I tried to dig in the `boto` sources and I see it needs to calculate MD5 checksum for each file sent. This means that the stream should be 'seekable' at least. I have non-seekable stream as I read from mongodb and I cannot rewind the data flow easily. The recommended here `smart_open` allows to use streams, but it just uses an internal buffer and then use 'multipart upload' with `boto` too. Technically it is possible to use file-like streams, but be ready that it may require a lot of memory. The idea of stream - is to use low memory to upload (probably) endless data flow.
Comment: No, according to [this ticket](https://github.com/boto/boto3/issues/256), it is not supported.
The idea of using streams with S3 is to avoid using of static files when needed to upload huge files of some gigabytes. I am trying to solve this issue as well - i need to read a large data from mongodb and put to S3, I don't want to use files.
Comment: @EthanP [StringIO](https://docs.python.org/2/library/stringio.html) — Read and write strings as files. Use `StringIO` object instead of file
Comment: @baldr Hmmm. This trick works for me in the past.
By the way in the ticket mentioned  in your message I see another [useful](https://github.com/boto/boto3/issues/256#issuecomment-139609091) method. Unfortunately I don't work with amazon now and can't test it
Comment: @baldr However, looks like S3 in common [can](http://stackoverflow.com/q/8653146/4249707) work with this kind of files.
Comment: @el-ruso, yes, this is exactly how `smart_open` works. And it seems it is the only way to upload these files. You upload large file by smaller chunks. I'd not call it 'stream upload', just 'chunk upload'.
Answer: We were trying to upload file contents to s3 when it came through as an InMemoryUploadedFile object in a Django request.  We ended up doing the following because we didn't want to save the file locally.  Hope it helps:
@action(detail=False, methods=['post'])
def upload_document(self, request):
     document = request.data.get('image').file
     s3.upload_fileobj(document, BUCKET_NAME,
                                 DESIRED_NAME_OF_FILE_IN_S3,
                                 ExtraArgs={"ServerSideEncryption": "aws:kms"})

Comment: While this approach works, it does not imply streaming - as InMemoryUploadedFile keeps the whole file in RAM. In memory files are relatively small in size - and they're not generated on-the-fly.
Answer: To upload a stream to s3 You can use Boto3 Session
r = requests.get(download_url, stream=True)
session = boto3.Session(aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_SECRET_KEY)
s3 = session.resource("s3")
bucket = s3.Bucket(UPLOAD_BUCKET_NAME)
bucket.upload_fileobj(r.raw, key)

This code can be used to call a URL which force downloads a file to get it as a stream and then upload it to s3.
Comment: The session and method for obtaining the stream in this example aren't necessary. The point is that passing a stream handle to upload_fileobj works.
Answer: There's an interesting code solution mentioned in a GitHub smart_open issue (#82) that I've been meaning to try out.  Copy-pasting here for posterity... looks like boto3 is required:
csv_data = io.BytesIO()
writer = csv.writer(csv_data)
writer.writerows(my_data)

gz_stream = io.BytesIO()
with gzip.GzipFile(fileobj=gz_stream, mode="w") as gz:
    gz.write(csv_data.getvalue())
gz_stream.seek(0)

s3 = boto3.client('s3')
s3.upload_fileobj(gz_stream, bucket_name, key)

This specific example is streaming to a compressed S3 key/file, but it seems like the general approach -- using the boto3 S3 client's upload_fileobj() method in conjunction with a target stream, not a file -- should work.
Comment: can you explain what is my_data here ?? is it a list or dict??
Comment: According to this StackOverflow answer, `writer.writerows()` takes an iterable of iterables -- list of lists, array of arrays, etc -- as input: https://stackoverflow.com/a/33092057/165494
Answer: Here is a complete example using boto3
import boto3
import io

session = boto3.Session(
    aws_access_key_id="...",
    aws_secret_access_key="..."
)

s3 = session.resource("s3")

buff = io.BytesIO()

buff.write("test1\n".encode())
buff.write("test2\n".encode())

s3.Object(bucket, keypath).put(Body=buff.getvalue())

Comment: I downvoted because buff.getvalue() is clearly not a stream, but a `bytes` object https://docs.python.org/3/library/io.html#io.BytesIO.getvalue
Answer: There's a well supported library for doing just this:
pip install s3fs

s3fs is really trivial to use:
import s3fs

s3fs.S3FileSystem(anon=False)

with s3.open('mybucket/new-file', 'wb') as f:
    f.write(2*2**20 * b'a')
    f.write(2*2**20 * b'a')

Incidentally there's also something built into boto3 (backed by the AWS API) called MultiPartUpload.
This isn't factored as a python stream which might be an advantage for some people.  Instead you can start an upload and send parts one at a time.
Answer: To write a string to an S3 object, use:
s3.Object('my_bucket', 'my_file.txt').put('Hello there')

So convert the stream to string and you're there.
Comment: This only works if the size of the object is not too big for memory.


Prove that a function sequence involing recursion and integral is convergent

Question:
Let $f, g : [0, 1] \rightarrow \mathbb R$ be continuous functions. Define $x_n(t) = f(t) + \int_0^t x_{n-1}(s)ds,$ $0 \le t \le 1, n=1,2,3,...$, where $x_0(t)=g(t), 0 \le t \le 1$. I have to show that the sequence $(x_n)$ is uniformly convergent on $[0, 1]$ and its limit is independent of $g$.

What I have done:

Since $f, g$ continuous on $[0, 1]$ by extreme calue theorem they attain a maximum on that interval, which means $\exists F,G$ constants such that: $|f(x)| \le F, |g(x)| \le G, \forall x \in [0,1]$.
Doing some calculations based on the above formula for $x_n(t)$ I have:

$|x_0(t)| \le |g(t)| \le G$
$|x_1(t)| = |f(t) + \int_0^t x_{0}(s)ds| \le |f(t)| + \int_0^t |x_{0}(s)|ds \le F+Gt$
$|x_2(t)| \le F+Ft+G\frac{t^2}{2}$
$|x_3(t)| \le F+Ft+F\frac{t^2}{2}+G\frac{t^3}{2*3}$
So, $|x_n(t)| \le F \sum_{k=0}^{n-1} \frac{t^k}{k!}+G \frac{t^n}{n!}$, which can also be written as:  $|x_n(t)| \le Fe^t+G \frac{t^n}{n!}$, since $e^t=\sum_{k=0}^{\infty} \frac{t^k}{k!}$

Where do I go from here? How can I prove the $|x_n(t)-x(t)|<\epsilon$ relationship of the uniform convergence?
Answer: Since $C[0,1]$ is a complete metric space with respect to the sup norm, we can use the contraction mapping principle.
To spell it out, let $T: C[0,1] \to C[0,1]$ map $x(t) \mapsto f(t) + \int_0^t x(s) ds$. $T$ itself isn't a contraction mapping, but $T^2$ is.
Let's prove this. First, let's work out what $T^2$ does:
$$T^2(x)(t)  =  f(t) + \int_0^t ds f(s) + \int_0^t ds \int_0^s du \ x(u) \\
= f(t) + \int_0^t ds f(s) + \int_0^t du \  \int_u^t ds x(u) \\
= f(t) + \int_0^t ds f(s) + \int_0^t du \ (t-u) x(u) $$
Hence
$$|T^2(x_1)(t) - T^2(x_2)(t)| \leq \int_0^t du \ (t-u) |x_1(u) - x_2(u) | \\
\leq  \sup_{t \in [0,1]} |x_1(t) - x_2(t) | \int_0^t du \ (t-u) \\
\leq \sup_{t \in [0,1]} |x_1(t) - x_2(t) | \times \frac {t^2} 2.$$
So
$$\sup_{t \in [0,1]}|T^2(x_1)(t) - T^2(x_2)(t)| \leq  \frac 1 2 \sup_{t \in [0,1]} |x_1(t) - x_2(t) |$$
Thus $T^2$ is a contraction mapping.
By the contraction mapping theorem, $(x_0, x_2, x_4, x_6, \dots)$ and $(x_1, x_3, x_5, x_7, \dots)$ both converge uniformly; moreover, they converge to the same limit and this limit is independent of the choice of $g$. So the original sequence $(x_0, x_1, x_2, \dots)$ also converges uniformly to a limit that is independent of $g$.
Comment: No I don't know this stuff! Can it not be solved more analytically?
Comment: It probably can, but the method using the contraction mapping theorem takes $\leq $ 5 seconds if you're familiar with it and I don't know any other method.
Comment: Do you understand the statement of the contraction mapping theorem, as spelt out in wikipedia, https://en.wikipedia.org/wiki/Banach_fixed-point_theorem#Statement ?
Comment: I just read it - it guarantees that there is a fixed point in that mapping you just did and guess that's were the $x_n(t)$ sequence converges correct? But is it uniformly convergent there and the how do I know with this method that the limit is indepedent of $g$?
Comment: The metric space is $C[0,1]$ endowed with the sup norm. Convergence of a sequence of functions with respect to the sup norm is the same thing as uniform convergence. So that bit is fine.
Comment: Independence of $g$ follows from the fact the the limit of the sequence $\{ T^n (g) \} $ is the (unique) fixed point of $T$ regardless of the choice of $g$. Look at the bit in the wikipedia page where it says "start with an arbitrary $x_0$.
Comment: Does this help or is it still not clear?
Comment: Yes its pretty clear, your answer is impeccable! But, I would wait for someone to provide an analytical solution to this...
Comment: I strongly recommend learning this contraction mapping technique though. It is really worth it. The same technique is used to prove that (a large class of) differential equations have solutions, which is a pretty important result.
Comment: @KennyWong: Actually, the inequality $\Vert T(x)\Vert\le\Vert x\Vert$ isn't sufficient for $T$ to be a contraction mapping. You'll have to prove that $\Vert T(x)\Vert\le\delta\Vert x\Vert$ for some $\delta<1$.
Comment: Good point! I'll remove this.
Comment: @SergeiGolovan Could you please take a look at the new version? Please do let me know if you still think I'm making a mistake, because this is nice problem and I really would like to understand the solution technique.
Comment: @KennyWong: Nice idea, to consider $T^2$ and show that it contracts.
Answer: Define the following sequence of functions, ${F_n}$ and ${G_n}$ as follows:
 $$F_0(t) = f(t), \ \ F_n(t) = \int_0^tF_{n-1}(s)ds$$
 $$G_0(t) = g(t), \ \ G_n(t) = \int_0^tG_{n-1}(s)ds$$
Note then that:
$$x_0(t) = G_0(t)$$
$$x_1(t) = f(t) + \int_0^t g(s)ds = F_0(t) + \int_0^tG_0(s)ds = F_0(t) + G_1(t)$$
And proceeding, inductively if
$$x_{n-1}(t) = \sum_{k=0}^{n-2}F_k(t) +G_{n-1}(t)$$
then
$$x_n(t) = f(t) +\int_0^t x_{n-1}(s)ds = F_0(t) + \int_0^t [\sum_{k=0}^{n-2}F_k(s) +G_{n-1}(s)]ds \\ = F_0(t) + \sum_{k=0}^{n-2}\int_0^tF_k(s)ds + \int_0^t G_{n-1}(s)ds \\ = F_0(t) + \sum_{k=0}^{n-2} F_{k+1}(t) + G_n(t) \\ = F_0(t) +\sum_{k=1}^{n-1} F_{k}(t) + G_n(t)  $$
And thus for all $n \geq 1$
$$x_n(t) = \sum_{k=0}^{n-1}F_k(t) + G_n(t) $$
Let $M_1,M_2$ be the maximum values of $f,g$ on $[0,1]$ respectively.
Then
$$\left |F_0(t) \right | \leq M_1$$
$$|F_1(t)| =  |\int_0^tF_{0}(s)ds| \leq M_1t$$
Proceeding this way, we get
$$|F_n(t)| \leq M_1\frac{t^n}{n!} $$
and similarly
$$|G_n(t)| \leq M_2\frac{t^n}{n!} $$
So, $\sup_{t \in [0,1]}|G_n(t)| \leq M_2/n!$ and thus $G_n$ converges uniformly to $0$. Now define $$H_n(t) = \sum_{k=0}^{n-1}F_k(t)$$ Given $\epsilon > 0$, for all sufficiently large $m,n$ and $m \geq n$we have $$|H_m(t) -H_n(t)| =  |\sum_{k=n}^{m-1}F_k(t)| \leq \sum_{k=n}^{m-1}|F_k(t)| \leq M_1\sum_{k=n}^{m-1}\frac{t^k}{k!} \leq M_1\sum_{k=n}^{m-1}\frac{1}{k!} < \epsilon$$  for all $t \in [0,1]$. Thus $H_n$ converges uniformly, suppose, to $H$.
Recall that $x_n(t) = H_n(t) +G_n(t)$ being the sum of two uniformly convergent sequences of functions must also be uniformly convergent and the uniform limit must be
$$\lim_{n \to \infty }H_n + G_n = H + 0 =H$$ Note that $H_n$ is independent of $g$ and since $G_n$ converges to $0$,  $x_n$ converges uniformly to a limit independent of $g$.
Comment: I can't actually see how you proceed to get the $x_n(t)$ with your way: when I tried to calculate $|x_2(t)-f(t)|$, there was a $\int_{0}^{t}|f(s)|ds$ there in my way, which I couldn't get it to just $f(t)$. Can you show me the calculation for $x_2(t)$? Also, after you substitute $t=1$ to the big inequality you produced, there is a "$-$" missing which also, since it has to be there, gives you trouble with using the absolute value below. Can you check this?
Comment: By integrating $f(t) - Mt \leq x_1(t) \leq f(t) + Mt$, we get
$f(t)t - Mt^2/2 \leq \int_0^t x_1(s)ds \leq f(t)t + Mt^2/2$, that is,
$f(t) + f(t)t - Mt^2/2 \leq f(t)+ \int_0^tx_1(s)ds \leq f(t) + f(t)t +Mt^2/2$ , that is, $f(t) + f(t)t - Mt^2/2 \leq x_2(t) \leq f(t) + f(t)t +Mt^2/2$, and so on.
Comment: From  $-(e^t- \sum_{k=0}^{n-1} \frac{t^k}{k!})f(t) - M\frac{t^n}{n!} \leq x_n(t)- e^tf(t) \leq -( e^t -\sum_{k=0}^{n-1} \frac{t^k}{k!} )f(t) + M\frac{t^n}{n!}$, we get $-(e^t- \sum_{k=0}^{n-1} \frac{t^k}{k!})f(t) - M\frac{t^n}{n!} \leq x_n(t)- e^tf(t) \leq ( e^t -\sum_{k=0}^{n-1} \frac{t^k}{k!} )M_1 + M\frac{t^n}{n!}$, here we use $-f(t) \leq |f(t)| \leq M_1$, and then we put to use $t=1$.
Comment: Fantastic! So, in the end, the limit I understand (for $n \rightarrow \infty$ it goes to $0$) - doesn't that show that $x_n(t) \rightarrow e^tf(t)$? Also what is that about the supremum that should I take - can you explain that as well?
Comment: Again on the extreme left, we use $-f(t) \geq -M_1$, so $-(e^t- \sum_{k=0}^{n-1} \frac{t^k}{k!})f(t) = (e^t- \sum_{k=0}^{n-1} \frac{t^k}{k!})(-f(t) \geq   (e^t- \sum_{k=0}^{n-1} \frac{t^k}{k!})(-M_1) $ and then we proceed as described.
Comment: From $|x_n(t)- e^tf(t)|  \leq M_1|(e- \sum_{k=0}^{n-1} \frac{1}{k!})| + \frac{M}{n!} \forall t \in [0,1] $, we get $\sup_{t \in [0,1]} |x_n(t)- e^tf(t)| \leq M_1|(e- \sum_{k=0}^{n-1} \frac{1}{k!})| + \frac{M}{n!}  $. Taking the limit as $n \to \infty$, we get $\sup_{t \in [0,1]} |x_n(t)- e^tf(t)| \to 0$, and so, $x_n(t)$ converges uniformly to $e^tf(t)$ and that is independent of $g$.
Comment: Ah, that's what you meant! I just show that there is another way with the sup to define uniform convergence! Thanks a lot!
Comment: Andre, I still see the problem with the integral in your first comment (computing $x_2(t)$), I mean you have $\int_{0}^{t}f(s)ds$ and you just going to $f(t)t$ below - how that happens?
Comment: Yes you are right, I possibly meant to use $M_1$ instead of $f(t)$. But still the previous proof was messy and probably erroneous. I edited to give a different proof. See above.
Comment: Andre, well done! This is a so much better proof than the previous one - fully understandable and elegant I have to say.


How do I read this piano sheet music?

Question: Could anyone let me know how I'm supposed to play these two measures on a normal sized piano?  The bottom two staves are for piano (treble clef on top and bass clef on bottom).
Firstly, I'd like to ask about the half notes in the first measure of the top staff.  I'm really confused about the tie.  It looks like it is connecting them to some of the 16th notes that start on the bottom staff that I need to play before the duration of the half notes ends.  I'd probably still call myself a beginner at piano, and I'm really unsure of how go about playing this.  Do I lift the fingers holding down the half notes before 2 beats is up so that I can play the 16th notes?  Of do I simply ignore the 16th notes that overlap with the half notes?
Another thing that I'm not so sure of is the 16th notes.  Firstly, what hand should I use?  Should I try to use my left hand for the bottom staff and change hands when I reach the upper staff?   But I see that the notes on the bottom staff overlap with those on the top one.  I'm confused.  And my hands certaintly aren't wide enough when you add in the half notes at the bottom.
So, how would an experienced pianist play these measures?  Or would they simply not bother to play everything?  I'm sorry if this is a stupid question.  I'm a beginner and really have no idea.  Anyway, any help would be appreciated.
Comment: Hi,   Please post the name of the piece AND the particular edition shown here.  It might be interesting to see if there are any introductory comments from the composer,  or if a different edition has different markings.
Answer: This passage is actually playable without any pedals assuming your hands can span an octave each.
While I'd like to use the sostenuto pedal at the half notes with pause signs on them before playing any 16th notes afterward, lifting the necessary fingers on the half notes when you need to play the 16th notes is acceptable (if more difficult). I wouldn't ignore the 16th notes--they seem like a flourish that should be played in full.
If all your hands can do is span an octave (like mine; I can span a ninth, but I hit adjacent inner notes at this point), distributing notes between hands should be easy. The only 16th note your left hand can play is the lowest B flat in that run. The rest of the 16th notes need to be played with the right hand. Change which finger is holding down chord notes while still holding down those notes as necessary if you're not using the sostenuto pedal.
If your hands can't even span an octave, you either need the sostenuto pedal or you're out of luck and need to use the damper pedal or lift a note for too long. But if your right hand can't play the B♭sus4 chords in the upper staff of the beginning of your excerpt or an unarpeggiated version of the B♭maj7 chord at the end, I personally believe this piece is out of your reach.
Comment: I disagree. Even if you could somehow play the RH chord F-B-D with 2-4-5 so that you can play the D. with 1 and the A with 3, that leaves no fingers to play the F' and A' at the end of the measure. This passage cannot be played without sostenuto unless one can span the LH octave 5-3 to cover the D. and F with 2 and 1 leaving RH 4-5 for the
F' and A', or else having seven fingers on one's right hand.
Comment: @AR-solidarityforModstrike - I left unstated that you will have to silently swap which RH fingers hold down which notes in the middle of the 16th-note run. This is a common enough tactic in fingering notation (e.g. "5-4") that I thought I didn't need to say this.
Comment: @AR-solidarityforModstrike - On further inspection, it does look like that last A in the first measure can't be hit without sostenuto, letting go of the half-note F (I'd actually go with the latter), or having a hand size that can span a 10th (this is common enough in piano sheet music).
Answer: The sustain pedal is often assumed in piano music. In this case, the notation implies it. The half note at the bottom tied through the end of the piece implies holding the pedal throughout, and the half notes tied to nothing says to hold those too ("properly", they would be tied to whole notes, but then the ties would have to go through the music so it would get messy).
So use the sustain pedal through the entire six beats. Hit the chord, and then you can use both hands to play the run. Since your right hand is already right there to play the first part of it, I would start with D-F-A-Bb with the right hand, then the next D-F-A-Bb with the left, then D-F-A-D with the right, A-Bb with the left, and then the last chord with the right.
Comment: I'm not sure I agree, because I would want the 16th-note run NOT to be sustained.  I understand that this may be a matter of interpretation.  Wonder what the composer actually intended?
Comment: @CarlWitthoft why do you say you want the run not sustained?
Comment: Matt,  just because I'd like to hear it as a run rather than  a  "building chord" .  Purely personal taste.
Comment: You'll hear it as a run. This is very typical stuff.
Comment: Btw, those ties on the half notes should be connected to whole notes in the next bar.
Comment: I disagree with "holding the sustain pedal throughout". The run starts so low that if you hold them together with sustain pedal, it will muddy the sound. Also, even if you skip the first four notes of the run, the sustain pedal might still make the sound too thick (this depends on your taste and interpretation)
Answer: The tie connects to an implied semibreve of the same three notes in the last bar.
I would play the B♭ octave and the RH chord, catch it with the sustain pedal and do the entire B♭maj7 arpeggio with my RH.  Holding the B♭ octave in the LH throughout.
If a sostenuto pedal was available, you could use it to sustain the chord and leave the arpeggio clean.   A pretty effect.  But not, I suspect, what the composer would expect.

How do I read this piano sheet music?


How can I Display 'Items per page' in pager instead of the exposed form

Question: I want to show 'Items per page' bellow the pager, I found viewsxtras, but it's just a placeholder and does not contain any code.
I'm using drupal 7 and views 3.x.
How can I do this?
Answer:

In Drupal 7 under PAGER click on "Paged, xx items", then look at the Pager options window
Answer: I think you can make use of the count in the aggrevation and then display it custom text in the footer region in the views. I have done it once, but not sure how.
Answer: This will only work with views exposed form
In .module file -> hook_form_alter
<code>$pager_options = array(5, 10, 15, 20, 25);
$form['your-views-form-id'] = array(
  '#type' => 'select',
  '#options' => $pager_options,
  '#default_value' => 0,
  '#title' => t('Items per page'),
);

$pages = $pager_options[$form_state['input']['your-views-form-id']];
$form_state['view']->set_items_per_page($pages);
</code>
It will provide you a select box with the options you have given and you will be able select no. of items to display on result.


Computing C90 ellipse area for two dimensional time series data

Question: I am trying to determine the area of the ellipse which contains the true mean of $(X_n, Y_n)_{1\leq n \leq N}$ with a probability of 90%. The 90% confidence ellipse area, or C90 area. The sample consists of N = 3000 $X,Y$ coordinate pairs. The problem is that definitions I've come across differ in how they end up calculating the actual area of this ellipse. After assuming this and that about the sample ( distribution etc ), the main steps are as follows :

Determine the covariance matrix of the sample. In this case its a $2\times 2$ matrix

Determine the eigenvalues of this matrix $\lambda_1, \lambda_2$

The square roots of the eigenvalues correspond to the axes of the ellipse. Hence the area can be calculated as $C90 = \pi\chi^2\sqrt{\lambda_1\lambda_2}$, where $\chi^2=4.605$

Step 3 is the one I'm confused about. I've looked at a couple of sources and each seems to compute the area differently. Some sources also claim that this corresponds to the area of the prediction ellipse, not the confidence ellipse.
My question is whether this 3 step procedure to find the C90 area is correct, or is this the area of the prediction ellipse.
Edit : updated my definition of the C90 area.
Comment: What you are describing is not [a confidence ellipse](https://stats.stackexchange.com/q/217374/1352), which is an *algorithm* that contains the true parameter pair in 95% of cases if we replicate the experiment often enough. A confidence ellipse has no reason to contain a specific percentage of the observations, and will in fact shrink as we collect more and more data, so contain fewer and fewer of the observations - simply because it is about *parameters*, not *observations*. This is similar to the relationship between quantiles and a confidence interval in a single dimension.
Comment: It sounds like there are potentially two separate questions here. (1) Determining (in terms of semimajor and semiminor axes and the center) the smallest ellipse that covers 90% of a given set of two-dimensional data points. (2) Given the semimajor and semiminor axes of an ellipse, determining its area. Part (1) is hard, and I haven't found anything with a quick search. There is something on [covering *all* points](https://scicomp.stackexchange.com/q/40494/42540), but I don't see a "quantile" version. Part (2) is [easy](https://en.wikipedia.org/wiki/Ellipse#Area). Can you clarify?
Comment: I'm updating my definition of C90 area and editing the question. Thanks for the clarification
Comment: OK, thank you, that is helpful. The algorithm does *not* give you a confidence ellipse. You can tell because the confidence ellipse should shrink as the sample size $n$ increases (because the means are better and better estimated), but there is no influence of $n$ in your calculation (other than in the estimation of the covariance matrix, but that is just that: the *estimation* of parameters that will not tend to zero as $n$ grows). Let's see whether we can find something...
Comment: [It looks like we don't have anything here](https://stats.stackexchange.com/search?q=confidence+ellipse+for+the+mean), which is a bit surprising to me. I would assume this to be treated in standard statistical textbooks. [The documentation to R's `car::dataEllipse()` function](https://rdrr.io/cran/car/man/Ellipses.html), may be helpful, especially its references.
Comment: @Stephan On the contrary, see the last line of the code at https://stats.stackexchange.com/a/34468/919 as well as the explanation at https://stats.stackexchange.com/a/67429/919.
Answer: The principle behind the $4.605$ value is that the sum of the square of two normal distributed variables (also known as a $\chi^2(\nu = 2)$ variable) is 90% of the time below $4.605$. The area of that circle is $4.605 \pi$.

The term $\sqrt{\lambda_1\lambda_2}$ relates to a stretching of the variables into an elipse.

What your $C90$ value refers to depends on what the values $\lambda_1$ and $\lambda_2$ refer to. It seems like these $\lambda$ are estimates of the population variance and in that case the interval relates to a prediction interval. It can also be that the $\lambda$ relate to the variance of the estimate and in that case the interval relates to a confidence interval.
This is similar to the difference between the variance of the population and the variance of the estimate of the mean of the population (these two differ by a factor $\sqrt{n}$).
Comment: A small detail: When we create a confidence interval then we use the t-distribution instead of a z-distribution. For the multivariate case I actually don't know how this is done. But, when $n$ is large then the two methods are approximately the same.


Media Queries não funciona

Question: Estou desenvolvendo um site em WordPress que precisa ser responsivo e para tanto estou utilizando media queries:
<code><head>
...
<link rel="stylesheet" href="css/style.css"/>
<link rel="stylesheet" href="css/mobile.css" media="screen and (max-width: 768px)">
...
</head>
</code>
Ao testa-lo em dispositivos móveis porem ele não está chamando as css do arquivo mobile.css. O que devo fazer?
O site é este aqui
Answer: Tente usar o media query inline no seu mobile.css
Exemplo: Use-media-query-inline-style
<code>@media only screen and (max-device-width: 480px) { /* Change to whatever media query you require */
    .bg {
         background: url(background_highres.jpg);
    }
}
</code>

Media Queries não funciona


Ubuntu Server 18.04LTS Can't extend default LV?

Question: My recent install of Ubuntu Server 18.04 appears to have created an LV mounted on / that is only 4GB. However the volume group is 160GB as expected.
But I get an insufficient disk space message when I try and expand the LV even though the group appears to have free space:
tom@git:~$ sudo pvs
  PV         VG        Fmt  Attr PSize    PFree
  /dev/vda3  ubuntu-vg lvm2 a--  <159.00g <155.00g
tom@git:~$ sudo vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  ubuntu-vg   1   1   0 wz--n- <159.00g <155.00g
tom@git:~$ sudo lvs
  LV        VG        Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  ubuntu-lv ubuntu-vg -wi-ao---- 4.00g

tom@git:~$ sudo lvextend -L +10G /dev/ubuntu-vg/ubuntu-lv
  /etc/lvm/archive/.lvm_git_20778_346817101: write error failed: No space left on device

Any ideas here? My root filesystem is at 100%, the new installer obviously doesn't make use of all disk space for the primary LV by default.
Comment: This happened to me using the 18.04.1 live server ISO.
Answer: LVM creates metadata backup on every change. It can't back up the data to /etc/lvm/archive as / is full. Either delete some files and try again, or use the -A n switch to not back up this change.
Reference man lvm (emphasis mine):

-A|--autobackup {y|n}
Whether or not to metadata should be backed up automatically after a change.  You are strongly advised not to disable this!  See vgcfgbackup(8).
Comment: Thanks, using -A n has let me extend the LV. Life saver thanks!
Comment: Forgot to add that I needed to use the --resizefs option as well.
Comment: Using `-r, --resizefs` depends on your use-case, of course.


how to make categories section in mongoose schema design of product like electronics, fashion ,food, etc. help to build this type of model

Question: How to make this type of model?
Comment: Please don't post pictures of code or other text. Copy the text to your question and use the code format tool.
Comment: Please provide enough code so others can better understand or reproduce the problem.
Answer: you can do something like this:
<code>category: {
    type: String,
    enum: {
        //Your categories
        value: ['electronics', 'food', 'fashion'],
        message: '{Value} //Your error message'
}
</code>
enum is validator in mongoose you can check it on their docs.
message -- this will warn user if he enters any other category not from value array
Before saving data mongoose will check if the provided category is in the categories of value array and will only save the data if category is on  of them. U can add more categories in the array if you want.
Comment: Thanks to give the better answer of my question


Expected minimum value of weird coin/card problem?

Question: I am quite boggled by the following question:

You are given a deck of cards with integers from 1 to 100 and 2 boxes labelled H and T. Flip a fair coin 100 times, and place the $i^\text{th}$ card in the box labelled H if the $i^\text{th}$ flip is heads, and likewise in the box labelled T if the flip is tails. Find the expected minimum card in the box with the $100^\text{th}$ card.

I am unclear on two fronts:
1) How do we condition on which box has the 100th card? Is that just assigning a probability of $\frac12$ or begin given the 100th card to each box, and does it even matter? (My gut says no due to symmetry, but I'm not sure how to formally tie this down.)
2) How do we find the expected minimum card? I feel like standard conditional expectation gives us the average value of the card, but not the expected minimum.
Help is greatly appreciated!
Answer: Let $X$ denote the minimal number of card in the box with the $100^{\text{th}}$ card.
Let $A$ be the event that the $100^{\text{th}}$ flip of the coin is heads. Then for $k=1,\ldots,99$
$$
\mathbb P(X=k)=\mathbb P(A)\mathbb P(X=k\mid A)+\mathbb P(A^c)\mathbb P(X=k\mid A^c)=\frac12 P_1(k)+\frac12 P_2(k)$$
where $P_1(k)$ is the probability that the first $k-1$ cards are in box $T$ and $k^{\text{th}}$ card is in box $H$.
$$
P_1(k)=\mathbb P(\underbrace{T \ldots T}_{k-1} H\mid 100^{\text{th}}=H)=\frac1{2^k}.
$$
And $P_2(k)=P_1(k)$ due to the symmetry.
So
$$
\mathbb P(X=k)=\frac1{2^k},\quad k=1,\ldots,99.
$$
For $k=100$
$$\mathbb P(X=100)=\frac12\mathbb P(\underbrace{T\ldots T}_{99}\mid 100^{\text{th}}=H)+\frac12\mathbb P(\underbrace{H\ldots H}_{99}\mid 100^{\text{th}}=T)=\frac1{2^{99}}.$$
You need to find
$$
\mathbb E[X]=\sum_{k=1}^{99} k\cdot \frac1{2^k}+100\cdot \frac{1}{2^{99}}.
$$
Another way is to find for $k=1,\ldots,100$
$$
\mathbb P(X\geq k) = \frac12\mathbb P(\underbrace{T\ldots T}_{k-1}\mid 100^{\text{th}}=H)+\frac12\mathbb P(\underbrace{H\ldots H}_{k-1}\mid 100^{\text{th}}=T)=\frac1{2^{k-1}}
$$
and then
$$
\mathbb E[X]=\sum_{k=1}^{100}\mathbb P(X\geq k)=\sum_{k=1}^{100} \frac1{2^{k-1}} = \sum_{k=0}^{99} \frac1{2^k} = 2-\frac{1}{2^{99}}.
$$
Comment: The second way is particular is very nice :)
Comment: @Thomas Agreed!
Comment: Hi quick question - why is the expected value for the second part just the sum of P(X>=k)? Where does that come from?
Comment: Look at https://math.stackexchange.com/q/2934857/


Using ListBoxFor in ASP.NET MVC 2

Question: I am trying to update the Roles a specific group has in my application. The Group model I use in my view has an additional AllRoles IEnumerable attached to it, so that in my view I can do something like this:
<%: Html.ListBoxFor( model => model.aspnet_Roles, new MultiSelectList( Model.AllRoles, "RoleId", "RoleName" ), new { @class = "multiselect" } )%>

This generates a multiple select drop down as expected. However, coming form PHP, I noticed that the name of the select was without square brackets, maybe that is OK in ASP.NET but in PHP it is wrong.
Now, how do I go about updating the group after submiting the form, more precisely, how can I read the multiselct selected values. What I need is that based on the RoleIds that I receive to Add respective aspnet_Roles to my Group model.
Trying to read the received values using HttpContext.Request.Form["aspnet_Roles"] failed and is also ugly. Can I somehow use the model to fetch the needed data? Controller function:
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Edit( SYSGroups updatedGroup ) {}

Thanks
Answer: The selected ids will be sent as a collection:
[HttpPost]
public ActionResult Edit(string[] aspnet_Roles)
{
    // the aspnet_Roles array will contain the ids of the selected elements
    return View();
}

If the form contains other elements that need to be posted you could update your model:
public class SYSGroups
{
    public string[] Aspnet_Roles { get; set; }
    ... some other properties
}

and have your action method look like this:
[HttpPost]
public ActionResult Edit(SYSGroups updatedGroup)
{
    // updatedGroup.Aspnet_Roles will contain an array of all the RoleIds
    // selected in the multiselect list.
    return View();
}

Comment: Can I avoid using another parameter to the Controller function? Can I use the received aspnet_Group to read the collection?

public ActionResult Edit( SYSGroups updatedGroup ) {}
Comment: Yes, add the `Aspnet_Roles` array property to the `SYSGroups` model class as I've shown. The property you are binding to in the `Html.ListBoxFor` needs to be an array like `Html.ListBoxFor(model => model.aspnet_Roles, ...` - `aspnet_Roles` needs to be a string array.
Comment: Just adding that parameter to the Controller function is OK, doesn't make sense to add weight to my model.
Thx.

Using ListBoxFor in ASP.NET MVC 2


Finding all functions $f:\mathbb{R}\to\mathbb{R}$ such that $f\bigl(xf(x+y)\bigr)=f\bigl(yf(x)\bigr)+x^2$

Question:
Find all functions $f:\mathbb{R}\to\mathbb{R}$ such that:
$$f\bigl(xf(x+y)\bigr)=f\bigl(yf(x)\bigr)+x^2$$

I assume the only solution is $f(x)=x$ (because it satisfies the condition).
Also, I could prove that:
$f(0)=f\bigl(-xf(x)\bigr)+x^2$ and $f\bigl(xf(2x)\bigr)=f(0)+2x^2$. Then I don't know how to go any further.
Comment: Similar questions on AOPS, but not the same: [1](https://artofproblemsolving.com/community/c6h1571632p9653841), [2](https://artofproblemsolving.com/community/c6h1177767p5691045), [3](https://artofproblemsolving.com/community/c6h411407), [4](https://artofproblemsolving.com/community/c6h1178359), [5](https://artofproblemsolving.com/community/c6h1307158p6990522), [6](https://artofproblemsolving.com/community/c6h2030430p14309529).
Comment: Assuming $f(x)=0$, $P(x,0)$ implies $x=0$. Hence $f(x)=0\implies x=0$ but not necessarily $f(0)=0$.
Comment: For the record, $ f ( x ) = - x $ gives another solution.
Comment: Same question on AOPS: [1](https://artofproblemsolving.com/community/c6h355780p1932915), [2](https://artofproblemsolving.com/community/c6h2042411p14454849), [3](https://artofproblemsolving.com/community/c6h583851p3451928), [4](https://artofproblemsolving.com/community/c6h1509317p8951779).
Answer: It's straightforward to verify that the identity function and its opposite are both solutions of the functional equation
$$ f \bigl ( x f ( x + y ) \bigr ) = f \bigl ( y f ( x ) \bigr ) + x ^ 2 \text . \tag 0 \label 0 $$
Indeed, they are the only functions with this property, which we try to prove. Suppose $ f : \mathbb R \to \mathbb R $ satisfies \eqref{0} for all $ x , y \in \mathbb R $. If $ f ( 0 ) \ne 0 $, then substituting $ 0 $ for $ x $ and $ \frac y { f ( 0 ) } $ for $ y $ in \eqref{0}, we get $ f ( y ) = f ( 0 ) $ for all $ y \in \mathbb R $, which leads to a contradiction, for example by putting $ x = y = 1 $ in \eqref{0}, and thus we must have $ f ( 0 ) = 0 $. Setting $ y = 0 $ in \eqref{0}, we get
$$ f \bigl ( x f ( x ) \bigr ) = x ^ 2 \tag 1 \label 1 $$
for all $ x \in \mathbb R $. In particular, \eqref{1} shows that if $ f ( x ) = 0 $ for some $ x \in \mathbb R $, then $ x = 0 $. Now, assuming $ f ( x ) = f ( y ) $ for some $ x , y \in \mathbb R $, we can use \eqref{0} and \eqref{1} to see that
$$ x ^ 2 = f \bigl ( x f ( x ) \bigr ) = f \bigl ( x f ( y ) \bigr ) = f \bigl ( ( y - x ) f ( x ) \bigr ) + x ^ 2 \text . $$
Hence $ f \bigl ( ( y - x ) f ( x ) \bigr ) = 0 $, and thus $ ( y - x ) f ( x ) = 0 $. From $ y \ne x $ we get $ f ( y ) = f ( x ) = 0 $, and thus $ x = y = 0 $, which leads to contradiction. Therefore, we must have $ x = y $, and consequently, $ f $ is injective. substituting $ - x $ for $ x $ in \eqref{1}, comparing with \eqref{1} itself, and using injectivity of $ f $ as well as $ f ( 0 ) = 0 $, we conclude that $ f $ is odd. By repeatedly using this and \eqref{0}, we get
\begin{align*}
f \bigl ( x f ( y ) \bigr )
& = f \bigl ( ( y - x ) f ( x ) \bigr ) + x ^ 2 \\
& = - f \bigl ( ( x - y ) f ( x ) \bigr ) + x ^ 2 \\
& = - f \bigl ( y f ( x - y ) \bigr ) - ( x - y ) ^ 2 + x ^ 2 \\
& = f \bigl ( y f ( y - x ) \bigr ) - ( x - y ) ^ 2 + x ^ 2 \\
& = f \bigl ( - x f ( y ) \bigr ) + y ^ 2 - ( x - y ) ^ 2 + x ^ 2 \\
& = - f \bigl ( x f ( y ) \bigr ) + 2 x y \text ,
\end{align*}
which implies
$$ f \bigl ( x f ( y ) \bigr ) = x y \tag 2 \label 2 $$
for all $ x , y \in \mathbb R $. Interchanging $ x $ and $ y $ in \eqref{2}, comparing with \eqref{2} itself and using injectivity of $ f $, we get
$$ y f ( x ) = x f ( y ) $$
for all $ x , y \in \mathbb R $, which in particular for $ y = 1 $ shows that $ f ( x ) = f ( 1 ) x $ for all $ x \in \mathbb R $. This together with \eqref{1} implies $ f ( 1 ) \in \{ - 1 , 1 \} $, which proves what was desired.


What is stored on a LVM LogicalVolume

Question: I have setup a LXC container with LVM as backingstore. On the host the command lvs --units m outputs the following:
 LV               VG    Attr      LSize   Pool Origin Data%  Move Log Copy%  Convert
 vm               dvg   -wi-a---- 1000.00m

When I'm inside the container the command df -h -B M prints this:
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/dvg/vm         969M  407M      513M  45% /
none                  1M    0M        1M   0% /sys/fs/cgroup
none                396M    1M      396M   1% /run
none                  5M    1M        5M   1% /run/lock
none               1977M    0M     1977M   0% /run/shm
none                100M    0M      100M   0% /run/user

Both outputs are in Mebibytes (MiB).
Now the question arises what files/metadata reserve the 1000-969=31MiB? Is it possible to display them?
Thanks!
Answer: The lost space is due to guests filesystem metadata, rather then to host LVM settings. Are you using XFS as guest filesystem, right? XFS reserves ~32 MB of disk space for its internal working, so this number fits quite nicely with your data.
On the other side, If you issue lsblk inside your guest you will notice that block device size is exactly the same as the backing logical volumes. So, on the guest side, no space is lost due to LVM alone.
Comment: Thanks. That's it. But Filesystem is ext3 :)

What is stored on a LVM LogicalVolume


remove a field ("ID") from json file:

Question: Trying to remove a field and value ("ID") from json file:
Can anyone please help with this?
JSON:
[{
  "Rating": "1600",
  "ID": "16733310",
  "Name": "LARRATEGUI,MARTIN",
  "Expires": "1000.10.10"
},{
  "Rating": "1353",
  "ID": "16429901",
  "Name": "ADDABBO,ERIC M",
  "Expires": "1000.10.10"
}]

TRIED THIS:
<?php

$file = file_get_contents("uscf.json");
$file = str_replace('"ID": "[0-9][0-9][0-9][0-9]",','',$file);
$array = json_decode($file, true);

unset ($array.ID);
echo $array;
?>

Desired result
[{
  "Rating": "1600",
  "Name": "LARRATEGUI,MARTIN",
  "Expires": "1000.10.10"
},{
  "Rating": "1353",
  "Name": "ADDABBO,ERIC M",
  "Expires": "1000.10.10"
}]

Comment: You tagged `javascript`, but you `TRIED THIS` in PHP...? Which language?
Comment: What should the end result look like? You mention `"ID": "16733310"` in your question but your PHP code attempt looks like it wants to use a regular expression to remove all `ID` properties with 4 digit values
Comment: You are right. My mistake. Sorry. I'm a little tired.
Comment: The page has an error.
Comment: The desired result is that "ID": "16733310", and "ID": "16429901", should be gone. All 300 of them.
Comment: Right, so you want the same array or objects, just without **any** `ID` properties? Does it matter what the values are?
Comment: The ID is always 8 digits.
Comment: If you want to use a regular expression you have to use `preg_replace`, not `str_replace`.
Answer: It's practically always easier to deal with the data after it has been decoded, rather than trying to manipulate the JSON directly.
Decode the JSON from the file, remove the ID elements from all the arrays, then write the JSON of the updated array back to the file.
$array = json_decode(file_get_contents("uscf.json"), true);
foreach ($array as &$el) {
    unset($el['ID']);
}
file_put_contents("uscf.json", json_encode($array));

Answer: In the interest of not mutating original data, I'd map the original array to a new one with the ID keys removed via array_diff_key().
$array = json_decode(file_get_contents("uscf.json"), true);
$filter = ['ID' => true]; // only the key is important
$newArray = array_map(function($item) use ($filter) {
    return array_diff_key($item, $filter);
}, $array);

Demo ~ https://3v4l.org/3nvtA

remove a field ("ID") from json file:


Spring JMS Consumer Application

Question: I have created a JMS broker with Apache ActiveMQ and a simple application that enqueues messages ok.
I would like to create another simple application that dequeues those messages asynchronously using an MDP. Here's an example of what I have so far:
Consumer POJO
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;

public class MyMessageListener implements MessageListener {
private static final Logger LOG = Logger.getLogger(MyMessageListener.class);

  public void onMessage(Message message) {
   try {
    TextMessage msg = (TextMessage) message;
    LOG.info("Consumed message: " + msg.getText());
   } catch (JMSException e) {
    e.printStackTrace();
   }
  }
}

And here's the relevant bits of my Spring XML configuration file
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory"
p:brokerURL="tcp://localhost:61616 />
</bean>

<bean id="simpleMessageListener"
class="main.SimpleMessageListener">

<jms:listener-container
container-type="default"
connection-factory="connectionFactory"
acknowledge="auto">
<jms:listener destination="testqueue"
ref="simpleMessageListener" method="onMessage" />
</jms:listener-container>

Now presumably I need a main method, but given the listener calls the onMessage method asynchronously when a message arrives on the queue, I am not sure how to code this up:
public class App {
  public static void main( String[] args ) {
  // what do I need here?
  //MyMessageListener msglistener ?
  }
}

Thank-you for your help.
Answer: You need to start the Spring application context, e.g. instantiate a ClassPathXmlApplicationContext. See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-instantiation

Spring JMS Consumer Application


Normalizing skewness with the Power or Box-Cox Transformation

Question: Suppose I have a random sample drawn from an arbitrary strictly positive continuous distribution. Suppose moreover that I want to use the Box-Cox transform to zero out the skewness. Is there an explicit solution to the required value of the exponent lambda that can be calculated as a function of the skewness or any other sample statistic, or is it necessary to find it through numerical methods?
I have revised this question in light of Glen's comment to respect the allowable range of the Box-Cox
Comment: Box Cox only works on positive random variables (the parameter lambda might be -0.5, for example). So if you standardize your variable you won't be able to do Box-Cox directly on it.
Answer: According to this: https://medium.com/@kangeugine/box-cox-transformation-ba01df7da884 , $\lambda$ needs to be calibrated by cross-validation.
Comment: Welcome to [stats.se].
Thank you for your answer.
Can you please [edit] you answer to expand it, in order to include the main points of the link you provide? It will be more helpful both for people searching in this site and in case the link breaks. By the way, take the opportunity to take the [Tour], if you haven't done it already. See also some tips on
[answer], on
[formatting help](https://stats.stackexchange.com/help/formatting)
and on writing down equations using
[LaTeX / MathJax](https://math.meta.stackexchange.com/q/5020).
Comment: I agree that andrewH might no longer be needing an answer, however, other people searching the site might be interested in an answer, in particular one that explains how to obtain $\lambda$. I'll provide the full quote for reference:
```The problem with the Box-Cox Transformation is estimating lambda. This value will depend on the existing data, and should be considered when performing cross validation on out of sample datasets. Make sure to estimate lambda according to the training dataset.```
Comment: @Ertxiem-reinstateMonica -- thanks for the guidance. If it's OK I'll keep it in mind for future posts, and this particular question is very old and the questioner is unlikely to still be interested.


Pathauto not updating path portion of new path

Question: When using the following replacement pattern for all nodes (to get 'hackable' urls that respect the menu hierarchy):
<code>[node:menu-link:parents:join-path]/[node:title]
</code>
I then create a node with title 'Subnode' which is a child of the node 'Parent node'. This will give me a node with the alias /parent-node/subnode. Now when I move the node one level up and change its (menu)title, so that Subnode is now a 'toplevel' node. The [node:title] part gets changed but the [node:menu-link:parents:join-path] not.
The 'Generate automatic URL alias' checkbox is checked.
Now when choose 'Update URL Alias' from the dropdown list in the content pane (/admin/content) the url does get updated properly.
Why doesn't it get updated right away?
Answer: I'm running into the same problem, I tried to trace the code and it looks like that token replacement is never called. Not sure if its a bug in token or pathauto module.
Comment: If you have more relevant information regarding whats going wrong codewise we could open a bug. Therefore we would first need to identify the module at fault though.


when is a session created in ejabberd ? How does authentication work ?

Question: Im trying to understand the data flow of ejabberd and having a hard time till now.
First, these are the modules Im working on:
ejabberd_c2s, ejabberd_auth, ejabberd_sm, ejabberd_sm_mnesia.
I understand that ejabberd_c2s is a gen_fsm that is entry point for any client that wants to talk to any other client.
Dataflow: When a client connects to ejabberd_c2s, it sends some data, not sure exactly what but along the lines of {JID}. But not able to figure out how ejabberd_c2s authenticates? Where does it actually call the authentication module and if the authentication is successful, who creates a session for that user? is it ejabberd_c2s or ejabberd_auth ?
I understand that ejabberd_sm[_mnesia] are the api calls that will actually create a session for anyone.
References to code/ samples is highly appreciated.!
How exactly this whole thing works?
Thanks a lot!
Answer: Authentication is primarily made is wait_for_stream function of c2s: https://github.com/processone/ejabberd/blob/master/src/ejabberd_c2s.erl#L348
Session is created in wait_for_bind function of c2s:
https://github.com/processone/ejabberd/blob/master/src/ejabberd_c2s.erl#L1046
Comment: Thanks.
what does wait_for_auth do? https://github.com/processone/ejabberd/blob/master/src/ejabberd_c2s.erl#L574
also, there are many places where auth_module is used, for eg, in wait_for_auth, wait_for_sasl etc. how are they different from one in wait_for_stream ?
A
Comment: Depends on XMPP protocol version and type of authentication. Generally, it is not the one used.


How to connect Docker Swarm services to an ordinary Docker network?

Question: I have a single small VPS that has a variety of services on it, such as a WordPress installation and a few web apps. For some while I have run all the services on it as Docker containers. Since I have a variety of domains and subdomains pointing to this box, I use the frontend proxy Traefik to capture the web ports and then route them internally in a Docker network.
I start up Traefik like so:
#!/bin/bash

# Removes the restart policy from previous containers
CONTAINER_LABEL=traefik-instance
../../bin/remove-restart.sh $CONTAINER_LABEL

mkdir --parents /var/log/traefik
mkdir --parents /etc/letsencrypt-traefik

docker run \
    --label $CONTAINER_LABEL \
    --publish 80:80 \
    --publish 443:443 \
    --volume $PWD/traefik.toml:/etc/traefik/traefik.toml \
    --volume $PWD/rules:/etc/traefik/rules \
    --volume /etc/letsencrypt-traefik:/etc/letsencrypt-traefik \
    --volume /var/log/traefik:/log \
    --network dockernet \
    --detach \
    --restart always \
    traefik:1.6

This all works very nicely. I have recently discovered Docker Swarm, and would like to convert all of my containers to services, which will give me replication services, rolling updates and zero-downtime deployments. However, I would like to do the change piecemeal, so that Traefik can route to both Swarm services and ordinary (non-Swarm) containers.
So, to launch Traefik as a service, I am now doing the following. You'll notice I am using non-standard ports for the purposes of testing:
#!/bin/bash

# Using "traefik2" while I am experimenting with multiple services
mkdir --parents /var/log/traefik2
mkdir --parents /etc/letsencrypt-traefik

docker service create \
    --publish 8080:80 \
    --publish 8443:443 \
    --mount type=bind,source=$PWD/traefik.toml,target=/etc/traefik/traefik.toml \
    --mount type=bind,source=$PWD/rules,target=/etc/traefik/rules \
    --mount type=bind,source=/etc/letsencrypt-traefik,target=/etc/letsencrypt-traefik \
    --mount type=bind,source=/var/log/traefik2,target=/log \
    --network traefiknet \
    traefik:1.6

This works also, when pointed to a Swarm web service that appears on the same network.
So, I have two Docker networks (amongst the various defaults that Docker creates for itself) like so:
root@box:~/docker# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
1aa479f13faa        dockernet           bridge              local
k71hpg1n0lo9        traefiknet          overlay             swarm

This results in my having a working Traefik container that can see Docker containers, and a working Traefik service that can see Swarm services. However, they cannot see each other.
To try to fix this, I have tried to add the Docker network to the start-up of the Traefik Swarm service:
--network dockernet \

In other words, I want this service to connect to both the bridge (old) and the overlay (new) networks. Unfortunately I get this:

Error response from daemon: The network dockernet cannot be used with services. Only networks scoped to the swarm can be used, such as those created with the overlay driver.

Is there a way my new service can connect to the old network, or indeed is there a way my old containers can connect to the new network? I have tried searching for the error, but there do not seem to be many mentions of it at all; I wonder if this edge-case usage of Swarm has not been encountered by many folks yet.
(Of course, one solution is for me to convert all of my containers to services, but to avoid a big-bang change, I'd rather do it slowly if possible).
Trying attachable networks
I then deleted my services and tried this:
docker network rm traefiknet
docker network create driver=overlay --attachable traefiknet

I then recreated the Traefik service, and it starts up. It is evidently still working because it routes traffic to a service that has also joined the traefiknet overlay.
However, I have created a non-service container, and connected that exclusively to traefiknet, and the --network-alias I create with that cannot be seen by the service. Oddly, if I shell into this non-Swarm container it can ping the Swarm Traefik container, so the network works. (I have tried creating an Alpine shell service, connected to traefiknet, and from here I cannot ping either the container name of my non-Swarm container, nor its --network-alias).
Upgrading Docker
I have tried to upgrade Docker from 17.03.2-ce to 18.06.1-ce, because a phrase in the manual indicated that my old Docker version might be the cause of the problem:

Communicate between a container and a swarm service sets up communication between a standalone container and a swarm service, using an attachable overlay network. This is supported in Docker 17.06 and higher.

However, this also has not helped.
Answer: I believe I have a fix for this, though there are still some things I don't understand. To set the context for this answer, the following is how I now install a Docker non-Swarm container:
#!/bin/bash

# Save pwd and then change dir to the project root
STARTDIR=`pwd`
cd `dirname $0`/../..

# Removes the restart policy from previous containers
CONTAINER_LABEL=ilovephp-staging
NETWORK_ALIAS=${CONTAINER_LABEL}
./bin/remove-restart.sh $CONTAINER_LABEL

docker run \
    --network swarmnet \
    --network-alias ${NETWORK_ALIAS} \
    --env TUTORIAL_ENVIRONMENT_NAME=staging \
    --detach \
    --restart always \
    ilovephp:2018-08-19

# Go back to original dir
cd $STARTDIR

As you can see, as per my question update, I am now able to put a container on an attachable Swarm network.
I have discovered that the reason why this cannot be pinged from a Swarm service is that it is missing a --name. As soon as I add a --name, it is reachable. Interestingly though, if I try to ping the Swarm service or the Swarm container from the Docker container, it works:
root@server:~# docker exec -it loving_allen sh
/ # # *** Ping a specific Swarm container ***
/ # ping alpine-swarm.1.9llv2hvv5xnc1c8diuhfh5m09
PING alpine-swarm.1.9llv2hvv5xnc1c8diuhfh5m09 (10.0.1.23): 56 data bytes
64 bytes from 10.0.1.23: seq=0 ttl=64 time=0.516 ms
^C
--- alpine-swarm.1.9llv2hvv5xnc1c8diuhfh5m09 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.516/0.516/0.516 ms
/ # # *** Ping the Swarm service ***
/ # ping alpine-swarm
PING alpine-swarm (10.0.1.22): 56 data bytes
64 bytes from 10.0.1.22: seq=0 ttl=64 time=0.376 ms
^C
--- alpine-swarm ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.137/0.231/0.376 ms

Thus, my original plan was to convert my Traefik instance from a container to a Swarm service, but that won't work, because unnamed non-Swarm containers won't be reachable. My solution now is to convert all my systems from containers to services first, and then once that is all done, I can convert the Traefik instance last. That way I will always be connecting from container to service, and not the other way around.
(I cannot add --names since if the Docker host is restarted, it will try to recreate containers with the same name, and they will fail because the old containers will still have those clashing names. And I can't use --rm to fix that, because it's not compatible with --restart! More on that here).
I still don't understand why a Docker-generated container name is not pingable from a Swarm container, nor why a --network-alias does not help either. But, given that my solution only needs to be temporary until all my systems are Swarm services, it probably does not matter.


Call Skype numbers from Silverlight

Question: I have a Silverlight application in which I display some phone numbers. I want that numbers to be displayed as links and when the user clicks on the links to be redirected to skype. (The same as on html).
This is the equivalent in html (just to understand what i need):
 +11 11 111 11 11

In silverlight I tried with:
<HyperlinkButton Content="{Binding}" NavigateUri="{Binding StringFormat=callto:\{0\}}" />

but I get System.InvalidOperationException: Failed to navigate to callto:+11 11 111 11 11.
Does somebody knows a solution for this?
Answer: Can you try using Javascript to invoke that sort of anchor?  If you're able to do this via Javascript, try using the Eval function to invoke the Javascript from Silverlight:
HtmlPage.Window.Eval();
Answer: This may be a little late, but if you still want to keep it in Silverlight code, then this will work:
Public Class MyHyperLink : Inherits HyperlinkButton
Sub New(ByVal uri As String)
    MyBase.NavigateUri = New Uri(uri)
End Sub

Public Sub Execute()
    Application.Current.Host.Content.IsFullScreen = False
    MyBase.TargetName = "_blank"
    MyBase.OnClick()
End Sub

End Class
And to call, add the following code::
Dim nav As New MyHyperLink(URL)
nav.Execute()

Call Skype numbers from Silverlight


change of variable in an ODE equation

Question: I am having a hard time understand how to get the equation here (the one circled) using change of variable. It says it uses $t=\lambda x$, and then new equation is the one in circle, but I am not sure how. Also in the new equation, the $y''$, is it actually $y''(t)$? so the y in the new equation is a function of t, is it correct?
Answer: First, multiply the given DE by $x$.
Take $t = \lambda x$ , then $$\frac{dy}{dt} = \frac{dy}{dx} \frac{dx}{dt} = \frac{1}{\lambda} \frac{dy}{dx} $$.
Further $$ \frac{d^2y}{dt^2} = \frac{1}{\lambda} \frac{d^2y}{dx^2} \frac{dx}{dt} = \frac{1}{\lambda^2} \frac{d^2y}{dx^2}$$
Now by substituting $\lambda = \frac tx$ we get $xy' = t\frac{dy}{dt}$ and $x^2y'' = t^2\frac{d^2y}{dt^2}$ and $\lambda^2x^2y = t^2y$

change of variable in an ODE equation


Send an ID via the URL for Angular views

Question: I am using Angular to create my single page views but I am struggling to pass an ID from an sql select via the URL.
I have created my view like so:
var app = angular.module("myApp", ["ngRoute"]);

    app.config(function($routeProvider) {
        $routeProvider
        .when("/jobSingle", {
            templateUrl : "jobSingle.php"
        });
    });

Works great shows me the correct page. I am TESTING data so this is open to SQL injection but I am playing around with Angular so don't freak out - I am linking to the single view page from another view like so:
$sql = "SELECT * FROM jobs";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) { // Start while
        echo $row['title'];
        echo 'View Job';
        ?>

                <?php echo $row['title']; ?>

    <?php
    } // End while
} else {
    echo "0 results";
}

So where I have echo 'View Job'; it appears to be the correct url and it opens the correct view but when I GET the id form the URL and var dump it i get nothing...
$jobId = $_GET['id'];
var_dump($_GET);

$sql = "SELECT * FROM jobs WHERE id = '$jobId'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) { // Start while
        echo $row['title'];
    } // End while
} else {
    echo "0 results";
}

My var dump just simply shows....
array(0) { } 0 results

Aswell when I do a var_dump on the $_GET I get a NULL value returned... Its almost like the url is being ignore, well the Id is at least...
Is this even possible OR am I missing the point here? I am learning angular as I work with Php primarily I wanted to know if this was do able?
Answer: Href #jobSingle?id=... in your  tag sets <fragment> part of a URL. When you open a website your browser does not include <fragment> part of a URL in its request.
To see the difference just swap <query> and <fragment> parts in your ULR to fulfill correct URL scheme. For example: ?id=1#jobSingle.
Here is the common URL syntax to clarify what I'm talkign about:
<scheme>://<user>:<password>@<host>:<port>/<path>?<query>#<fragment>

Everything that goes after # mark in URL is treated like a fragment.
RFC 1738 and RFC 3986 contain full explanation for raised topic.
Comment: Ok so I understand and followd your advice so my URL now looks like `http://localhost/Angular/?job_id=3#/jobSingle` but still returning `NULL`
Comment: So you put this link in your browser, refresh your page and `var_dump($_GET)` shows you NULL? Are you sure?
Comment: Do you open this path directly or through a serverside router script?


How to make rendered product variations show in a grid rather than a list?

Question: I need to present some product variations rendered in a grid, without any cart options.
The idea is to have the user click a link to the product and present a page with the variations.
The easiest way I manage to do this is: in content->manage display I only show the field:Product Variations, with the format:Rendered Product, which gives me exactly what I want. Except it is displaying the rendered variations in a vertical list rather than the nice grid like it shows by default in the product list view.
Where does the presentation of this rendered product variation elements come from?
I'm using the default theme:commerce kickstart omega.
Answer: You can quite easily create a View display to list your products in a grid.

Add a new View ( Admin > Structure > Views )
Add a page display
Add relevant filters for entity type etc..
Set the style to "Grid" layout
Tinker from there with fields etc..

That should do it.


Map for passed arguments isn't working as expected

Question: I'm trying to figure out how to properly tell typescript that each part of an enum expects different props.
Here's what I have:
function setDrawerContent <T>(data: DrawerData<T>) {
  //
}

// I would populate this as more content is needed in the future
enum DrawerContent {
  CONTENT_A,
  CONTENT_B,
}

// This is what I would call `setDrawerContent` with
interface DrawerData<T> {
  title: string;
  content: DrawerContent,
  props: T
}

// template for Content A
interface ContentAData {
  username: string;
}
// template for Content B
interface ContentBData {
  age: number;
}

// trying to map those interfaces above here
interface DrawerDataMap {
  [DrawerContent.CONTENT_A]: ContentAData,
  [DrawerContent.CONTENT_B]: ContentBData,
}

setDrawerContent<DrawerDataMap>({
  title: 'Title for Content A',
  content: DrawerContent.CONTENT_A,
  props: { username: 'some-class' } // error (but there shouldn't be): 'username' does not exist in type 'DrawerDataMap'
})

What am I doing wrong here? I would expect typescript to not complain above, just as I would expect it to complain below:
setDrawerContent<DrawerDataMap>({
  title: 'Title for Content A',
  content: DrawerContent.CONTENT_A,
  props: { age: 'some-class' } // `username` is supposed to be passed, not `age`
})

Answer: One problem is that you are giving DrawerDataMap as the generic parameter of the setDrawerContent function. The function then expects an argument with a props property of type DrawerDataMap (replace T with DrawerDataMap everywhere basically).
If you want props to be of type ContentAData, you can call the function like this :
setDrawerContent<ContentAData>(...)

or even without specifying the type as it will automatically be inferred. But that is not what you want if I understood correctly, as you want ContentAData to be "linked" to CONTENT_A and same for ContentBData and CONTENT_B.
What you can do is create interfaces that represent the data to pass and create a union type, like so :
interface ContentAData {
  title: string;
  content: DrawerContent.CONTENT_A,
  props: { username: string }
}

interface ContentBData {
  title: string;
  content: DrawerContent.CONTENT_B,
  props: { age: number }
}

type DrawerData = ContentAData | ContentBData;

function setDrawerContent(data: DrawerData) {
  //
}

Here is a REPL.it : https://replit.com/@billuc/UnionTypes#index.ts
I added a version with a base interface at the end, since it might be closer to what you need.


Show function does not have limit using sequence.

Question: Consider the function g: $R$ $ \rightarrow $ $R$ defined by
$$
g(x) = \left\{\begin{aligned}
&4-.5x &&: x\,rational\\
&.5x &&: x\,irrational
\end{aligned}
\right.$$
Pick a point $b$ at which $g$ does not have a limit, and show that it does not have a limit there. Use sequences.
Now I know that it only has a limit at point $a=4$, which I already proved using the definition of limit for functions. So I know I can really pick any other point to prove it does not have a limit there.
But my question is how can go about it using sequences. I found a theorem:
$2.1 \, THEOREM$
Let f : $D$ $\rightarrow$ $R$ with $x_o$ an accumulation point of D. Then f has a limit at Q iff for each sequence {$x_n$} converging to $x_o$ with $x_n$ $\in$ D and $x_n$ $\neq$ $x_o$ for all n, the sequence (f($x_n$)) converges.
But this theorem proves that a function has a limit, how can I use it to show that a function does not have a limit? Also, I am guessing because I have the freedom to choose almost any point to prove this that it must make it easy to some degree. Thank you.
Comment: The theorem also proves that a function does not have a limit, because of the word "iff".
Comment: Namely, pick two subsequences that converge to different values. Then, the sequence does not have a limit.
Comment: Let $x_n$ be a sequence of irrational numbers converging to 0. Let $y_n$ be the sequence $y_{2n}=1/n$ and y_{2n+1}=x_n$. Try showing the sequence $g(y_n)$ does not converge
Comment: Ah, thank you everyone. I understand now.
Answer: We can pick for example $b=0$.
Let $x_n = \frac1n$, then $\lim_{n \rightarrow \infty}x_n = 0$.
Since $x_n \in \mathbb{Q}$, $f(x_n)=4-\frac{x_n}{2}$, $\lim_{n \rightarrow \infty}f(x_n)=4$
Let $y_n=\frac{\sqrt{2}}n$, then $\lim_{n \rightarrow \infty} y_n = 0$.
Since $y_n \in \mathbb{R} \setminus \mathbb{Q}$, $f(y_n)=\frac{y_n}{2}$, $\lim_{n \rightarrow \infty}f(y_n) =0 \neq \lim_{n \rightarrow\infty}f(x_n)$
$g$ does not have such limit at $0$.
Comment: Thank you very much, the example is very helpful.


Swift: How to assign a class's delegate to another class in current class?

Question: protocol TestDelegate {
    func toggleLeftPanel()
}
class A: UIViewController, TestDelegate {
    //...do sth.
    func toggleLeftPanel() {
        //do sth.
    }
    //...do sth.
}
class B: UIViewController {
    var delegate: TestDelegate?
    func onMenu() {
        delegate?.toggleLeftPanel()
    }
}
class C: UIViewController {
    func presentAction() {
        let b = b()
        b.delegate = A.self//Here will report an error

        let b = B()// I got the instance of B controller
        presentViewController(b, animated: true, completion: nil)
    }
}

Think that I am now in C UIViewController( this corresponding a screen in IPhone ), then I will go to the B UIViewController by click a button (presentAction).
But when I arrived at B UIViewController, I found that onMenu not work( toggleLeftPanel not work ), because delegate property of b is nil, so I decide to assign a class( instance? ) to it( b.delegate ) before present b UIViewController, but I got an error "cannot assign a value of type 'xxxController.Type' to a value of type'xxxDelegate?'".
How could I resolve this problem? Or I should implement the TestDelegate in class C again and assign b.delegate to itself?
Answer: let b = b()
b.delegate = A.self

Codes above are completely nonsense. First, there is no class name "b". It should be let b = B(). Second, you should assign an instance to delegate. Not a class. Something like b.delegate = self. Please read https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
You can also try codes below
protocol TestDelegate: class {
    func toggleLeftPanel()
}

class A: UIViewController, TestDelegate {
    //...do sth.
    func toggleLeftPanel() {
        //do sth.
    }
    //...do sth.
}

class B: UIViewController {
    weak var delegate: TestDelegate?
    func onMenu() {
        delegate?.toggleLeftPanel()
    }
}

class C: UIViewController {
    func presentAction() {

        let a = A()
        let b = B()
        b.delegate = a
        presentViewController(b, animated: true, completion: nil)
    }
}

Comment: Oh, yes, I made some typo. It should be `let b = B()`. I understand what you said, but what I think is: `class A` had already implement the delegate, could I use it agian? I want to assign an instance of `class A` to `b.delegate` in `func presenAction()` in `class C`, could this be done? (I don't want to let class C to follow the protocol, and then implement it, and then assign `self` to `b.delegate`)
Comment: I knew that make `class C` to conform `TestDelegate` and then implement it can reach the goal, but I just want to reuse `class A`, because it had already conformed `TestDelegate`, could this be done?
Comment: Ah, Now I see what you are trying to do. As long as you have a class A instance you can do it. I updated my answer.
Comment: Yes, that's it. I think too much. As I build App by Storyboard, a xxxViewController corresponding a Storyboard, I unexpectedly  don't know how to create an instance of xxxViewController just because it corresponding a Storyboard(UI). Thank you very much! I'm a beginner.