JUnit Test error with embedded glassfish container

Question: I must admit this JUnit Test has been a nightmare till now, I have invested some days, searching the Internet but everything I tried did not work.
The Error I have is following:
SCHWERWIEGEND: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
java.lang.RuntimeException: Invalid resource : jdbc/sample__pm

The jdbc/sample__pm does not exist but the jdbc/sample in the persistance.xml does.
As I said I have already searched for this problem but I cound not find a solution to the problem.
I guess the problem is with the pom.xml, but don't know how to fix it.
Thanks for any help
the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maggioni</groupId>
<artifactId>SampleApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>4.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.2.0</version>
    </dependency>

</dependencies>
<build>
    <finalName>SampleApp</finalName>
</build>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

the source can also be found here
Answer: I had the same problem, and after losing lot's of time I got it right. I now can run my tests with my connection pool.
This problem is resulted by series of small maven code generation bugs in Netbeans.
Bellow are a serie of steps for you to check
1 - Your pom.xml must have the path , plugin and dependencies configured for glassfish-embedded-static-shell,  as follow:

Add in the properties at the top of your pom the path to your glassfish instalation folder(here is mine):

<properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <glassfish.embedded-static-shell.jar>C:/Program Files/glassfish-4.0/glassfish/lib/embedded/glassfish-embedded-static-shell.jar</glassfish.embedded-static-shell.jar>
  </properties>

Add/check the plugin definition bellow(substitute mysql per maven in your case):
        `
<build>
...
  <plugins>
    ...
     <plugin>
        <groupId>org.glassfish.embedded</groupId>
        <artifactId>maven-embedded-glassfish-plugin</artifactId>
        <version>4.0</version>

        <dependencies>
            <dependency>
                <groupId>org.glassfish.main.common</groupId>
                <artifactId>simple-glassfish-api</artifactId>
                <version>4.1</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-static-shell</artifactId>
                <version>4.1</version>
                <scope>system</scope>
                <systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.28</version>
            </dependency>
        </dependencies>
        <configuration>
            <app>target/${project.artifactId}-${project.version}</app>
            <port>8282</port>
            <contextRoot>${project.artifactId}</contextRoot>
            <foo>bar</foo>
        </configuration>

    </plugin>
  ...
  </plugins>
 ...
</build>

`
2 - Your glassfish-resources... it's silly, but the jndi-name of your jdbc resource must have the prefix java:app/ so, add it as sample bellow(just the resource, the connection pool can have any name):

*Just remembering the name of your jdbc-resource in glassfish will not have the prefix java:app, e.g.: my jdbc-resource in glassfish is just ShrewdPCPool
3 - Third and the silliest, when deploying the embedded glassfish, the auto generated script of Netbeans doen't include /src/main/resources/setup in the class path, so, the simple solution is to make a copy of your glassfish-resources.xml para /src/main/resources/META-INF/ or if you are patient enough change the script.
Dont forget to clean, and after build with dependencies before you run your tests!


How to create nested JSON objects dynamically?

Question: I want to create a JSON object words which further contains a list of
 objects s and e with different values as follows:
words = {
    "a1": { s: 1.5, e: 2 },
    "a2": { s: 2.1, e: 2.4 },
    "a3": { s: 2.5, e: 3 },
    "a4": { s: 3.5, e: 3.8 },
    "a5": { s: 3.9, e: 4.5 },
    "a6": { s: 4.8, e: 5.2 },
    "a7": { s: 5.3, e: 5.3 },
    "a8": { s: 6.5, e: 7 },
    "a9": { s: 7.2, e: 8 },
    "a10": { s: 8.1, e: 8.4 }
};

I need to add these values using a for loop. I have applied the
following solution for this:
var words = {};
var xyz = [1.5,2,2.1,2.4,2.5,3,3.5,3.8,3.9,4.5,4.8,5.2,5.3,5.3,6.5,7,7.2,8];
words = '{';
for (var i = 0; i <= 10; i++) {
    if (i == 10)
        var obj = '\"a' + i + '\"'+': { s:' + xyz[i] + ', e:' + xyz[i + 1] + '}';
    else
        var obj = '\"a' + i + '\": { s:' + xyz[i] + ', e:' + xyz[i + 1] + '},';

    words = words + obj;
}
words = words + '}';

$.parseJSON(words);

But this generates an error of:

Uncaught SyntaxError: Unexpected token s**

I am not able to create the object due to this.
Answer: You don't need to create a JSON string, then parse it with JS: it's far more easily done with this:
var words = {},
    xyz = [1.5,2,2.1,2.4,2.5,3,3.5,3.8,3.9,4.5,4.8,5.2,5.3,5.3,6.5,7,7.2,8];

for (var i = 0, l = xyz.length/2; i < l; i++) {
  words['a' + (i + 1)] = {
    s: xyz[2*i],
    e: xyz[2*i + 1]
  };
}

console.log(words);

Explanation: you go through your source arrays, taking each pair into an object literal and assign it to words object with bracket notation (so that value of 'a' + (i+1) is evaluated as a new key).
Answer: There's basically no reason to create a JSON string in a browser manually, instead create the object and then use JSON.stringify on it. (Most browsers have it by default, if not you can add a shim.) For instance:
var counter;
var words = {};
var xyz = [1.5,2,2.1,2.4,2.5,3,3.5,3.8,3.9,4.5,4.8,5.2,5.3,5.3,6.5,7,7.2,8];
var i;
counter = 0;
for (i = 0; i < xyz.length; i += 2) {
    ++counter;
    words["a" + counter] = {s: xyz[i], e: xyz[i + 1]};
}
console.log("words (before stringify):", words);
words = JSON.stringify(words);
console.log("words (after stringify):", words);

If you're going to create JSON manually, you need to learn and follow its rules. The keys must be in double quotes (so, "s": ... not s: ...), for instance.


TDD how to handle a change in a mocked object

Question: In writing unit tests, for each object that the unit interacts with, I am taking these steps (stolen from my understanding of JBrains' Integration Tests are a Scam):

Write a test in the unit to ensure it is sending the correct call and params to the collaborating object
Write a test in the unit that ensures it handles all possible responses from the collaborating object. These responses are all mocked so the unit is tested in isolation.
Write a test in the collaborating object to make sure it accepts the call and params.
Write tests to make sure each possible response is sent back.

My question comes around when I decide to refactor an object that has responses mocked in step 2.  If I change the way the object responds to a call, none of the tests that other objects have for that call will fail because they have all been mocked to match the old style. How do you keep mocks up to date with the objects they are mocking?  Is there a best practice for this?  Or have I completely misunderstood things and am doing it all wrong?
Answer: I do it this way.
Suppose I have to change the responses from interface method foo(). I gather all the collaboration tests that stub foo() in a list. I gather all the contract tests for method foo(), or if I don't have contract tests, I gather all the tests for all the current implementations of foo() in a list.
Now I create a version control branch, because it'll be messy for a while.
I @Ignore (JUnit speak) or otherwise disable the collaboration tests that stub foo() and start re-implementing and re-running them one by one. I get them all passing. I can do this without touching any production implementation of foo().
Now I re-implement the objects that implement foo() one by one with expected results that match the new return values from the stubs. Remember: stubs in collaboration tests correspond to expected results in contract tests.
At this point, all the collaboration tests now assume the new responses from foo() and the contract tests/implementation tests now expect the new responses from foo(), so It Should All Just Work.(TM)
Now integrate your branch and pour yourself some wine.
Answer: Revised:
This is a tradeoff. Ease of testing by isolating an object from its environment vs Confidence that it all works when all the pieces come together.

Aim for stable roles: Think in terms of client-oriented roles (rather than a bunch of methods). I've found roles (written in terms of client's needs / client-first / outside-in) are less volatile. Check if the role is a leaky abstraction betraying implementation details. Also watch for roles that are change-magnets (and come up with a mitigation plan).
If you have to make changes, see if you can 'lean on the compiler'. Things like changing a method signature will be flagged up nicely by the compiler. So use it.
If the compiler cannot help you in spotting the changes, be more diligent that usual to see if you haven't missed a spot (client usage).
Finally you fall back on acceptance tests to catch such issues - ensure that Object A and Collaborators B,C,D are playing by the same assumptions (protocol). If something manages to escape your dragnet, chances are high that at least one test should spot it.
Comment: First, exceptions thrown certainly represent part of the contract, but I don't like to add exceptions to a contract unless it's essential. Next, at most importantly, I don't like that your answer cites acceptance tests as the first and most significant way to solve this problem, since I generally treat them as the last and weakest line of defence. Diligence, we agree, gives us the best chance of success.
Comment: Falling back on acceptance tests should help, but not be necessary. I'd rather not try to rely on them, but I'm happy to have them as an emergency mistake detection system.
Comment: @J.B.Rainsberger - Yes. I was not implying that every interaction needs to be a high-level/system test. However some system test should be able to spot any anomalous change in the interaction. We've sparred before on this http://tech.groups.yahoo.com/group/testdrivendevelopment/message/32743 Not sure how I missed the last message on that thread. I would consider Exceptions thrown as part of the contract (you seem to imply that they are implementation details).. Could you elaborate on that? Apart from that, changes can be made safely, given an adequate amount of diligence to not miss any spots
Comment: @J.B.Rainsberger - yeah.. I can see how a reader can get that idea... Revised
Answer: First, it's definitely harder to get this level of coverage with integration tests, so I think unit tests are still superior. However, I think you have a point. It's hard to keep your objects' behavior in sync.
An answer to this is to have partial integration tests that have real services 1 level deep, but beyond that are mocks. For instance:
var sut = new SubjectUnderTest(new Service1(Mock.Of<Service1A>(), ...), ...);

This solves the problem of keeping behaviors in sync, but compounds the level of complexity because you now have to setup many more mocks.
You can solve this problem in a functional programming language using discriminated unions. For instance:
// discriminated union
type ResponseType
| Success
| Fail of string   // takes an argument of type string

// a function
let saveObject x =
    if x = "" then
        Fail "argument was empty"
    else
        // do something
        Success

let result = saveObject arg

// handle response types
match result with
| Success -> printf "success"
| Fail msg -> printf "Failure: %s" msg

You define a discriminated union called ResponseType that has a number of possible states, some of which can take arguments and other metadata. Every time you access a return value you have to deal with possible various conditions. If you were to add another failure type or success type, the compiler would give you warnings for each time you don't handle the new member.
This concept can go a long way toward handling the evolution of a program. C#, Java, Ruby and other languages use exceptions to communicate failure conditions. But these failure conditions are frequently not "exceptional" circumstances at all, which ends up leading to the situation you are dealing with.
I think functional languages are the closest to providing the best answer to your question. Frankly, I don't think there is a perfect answer, or even a good answer in many languages. But compile-time checking can go a long way
Comment: @KeremBaydoğan I work with deadlines. Understanding the code that I write helps me meet deadlines. Allowing that code to worsen over time threatens my ability to meet deadlines.
Comment: I disagree that keeping mocks and implementations in sync is hard. It can be tedious, but it's not really hard, in general. I treat this the way I treated TDD 12 years ago: a newly-discovered part of my job that people had never explained to me before.
Comment: @J.B.Rainsberger keeping mocks and implementations is really hard, if you are working with deadlines or developers who does not like unit tests.
Comment: @KeremBaydoğan If you work with programmers who do not like unit tests, then every automated testing technique you use will eventually fail. If you want to use automated tests to help you in your work, then you will find it difficult to share code with people who do not want to work that way.
Comment: @J.B.Rainsberger I am feeling the same as you about understanding the code that I'm working on. What I am trying to say we should not trust developers about keeping mock and real software components in synch. Every developer should write one mock object for every real object that they are maintaining and when they finish doing that they should write a unit test and compare every behaviour of the real object with the mock one. Because comment section is not large enough, I have posted an answer. It would be great if you take a look at it and tell if something is not right.
Comment: @KeremBaydoğan We should be able to trust them. If we can't trust them, then we are not working together, and the rules certainly change. :)
Answer: You should not trust human beings (even yourself) about keeping mock and real software components in synch.
I hear you ask?

Then what is your proposal?

My proposal is;

You should write mocks.
You should only write mocks for software components that you maintain.
If you maintain a software component with another developer, you and the other developer should maintain mock of that component together.
You should not mock someone else's component.
When you write a unit test for your component, you should write a separate unit test for the mock of that component. Let's call that MockSynchTest.
In MockSynchTest you should compare every behavior of the mock with the real component.
When you make changes to your component, you should run the MockSynchTest to see if you made your mock and component out-of-synch or not.
If you need the mock of a component that you do not maintain while testing your components, ask the developer of that component about the mock. If she can provide you with a well tested mock, good for her and lucky for you. If she can not, kindly ask her to follow this guideline and provide you a well tested mock.

This way if you accidentally make your mock out of synch, there will be a failing test case there to warn you.
This way you do not need to know the implementation details of the foreign component for mocking.
How-to-write-good-tests#dont-mock-type-you-dont-own
Comment: In a very low-trust environment, these rules might help, but I do not recommend these as general rules. I interpret these rules as a way to cope with working in an environment where we share code with people, but we do not work as a team. These rules seem to me to simply treat co-workers as if they were producing a product that we consume; and in the case, we should not live in the same code base.


Get argument from current URL in twig to generate a link with that argument

Question: I'm trying to get an argument from search page URL so that I can pass that argument to a link.
For example I have this URL for a search results page:
http://domain.com/site-search?search_api_fulltext=foo
And on that page I want to have a link that goes to:
http://domain.com/other-search?search_api_fulltext=foo
I've tried {{ url('<current>') }} but that only returns the domain and path, but not the arguments. I've been trying stuff like {{ app.request.requestUri }} but that returns nothing.
FWIW, I'm trying to include these links in a custom block that appears on the search results page.
Comment: app. is for symfony apps. You could preprocess this variable and pass it in.
Comment: I'm trying that with:
```function MYTHEME_preprocess_block(&$variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  $variables['current_path'] = $current_path;
}```
But its still not returning the arguments, just the page path.
Comment: Ah, I figured out the function:
```function rcorpta_preprocess_block(&$variables) {
  $current_path = \Drupal::request()->getRequestUri();
  $variables['current_path'] = $current_path;
}```
Answer: well if you are making a custom block then you are probably doing something like this ....
 $build = [];
 $build['#theme'] = 'your_template',
 return $build;

and then in you hook_theme().
 hook_theme() {
  return [
   'your_template' => [
     'render element' => 'children',
     'template' = 'your-templante-name',
    ],
  ];
 }

you don't need to pre prossess the block at all just make sure the block is not being cached aka add this to your block plugin.
see How to prevent a block from being cached?
public function getCacheMaxAge() {
  return 0;
}

then in your block function :
 $search_query = \Drupal::request()->get('search_api_fulltext');
 $build = [];
 $build['#theme'] = 'your_template',
 $build['#search_query'] =  $search_query,
 return $build;
....

 hook_theme() {
  return [
   'your_template' => [
     'render element' => 'children',
     'template' = 'your-templante-name',
     'variables' => [
       'search_query' => NULL,
     ],
    ],
  ];
 }

then in your twig template
{% if search_query %}
  do what you want here {{search_query}}
{% endif%}

Comment: Thanks. I'm going to try this later on. I did figure out how to do it in a way I'm not happy about where I used in my theme file:
```
function THEME_preprocess_block(&$variables) {
  $current_path = \Drupal::request()->getRequestUri();
  $variables['current_path'] = $current_path;
}
```

And then in my twig:
```{% set search_string = current_path|split('=')|last %}```

But I want to come back and try your suggestion as I want everything in the custom module and not part of the theme.


How does Drupal generate form ID for a form?

Question: Following is the code snippet I used to generate a the User Profile edit form:
print drupal_render(drupal_get_form('user_profile_form'));
However, I could not find the declaration of the 'user_profile_form' form id anywhere in the Drupal system. So I assumed that it is generated dynamically, but I am not sure.
So, If it is already defined, Where is it defined?
and if it is generated dynamically then how does Drupal generates these Form IDs.
Answer: user_profile_form is defined in user.pages.inc file in user module directory.
The id of the form is the function name which generate the structure of the form in Drupal System. When we call drupal_get_form, it call function name to build the form array.
There is a function drupal_prepare_form in form.inc in includes directory of the Drupal root.

drupal_prepare_form function prepares a structure of the form array
  and adds required elements, executes any hook_form_alter functions
  and optionally inserts a validation token to prevent tampering.

Code Snippets to generate form id:
if (!isset($form['#id'])) {
    $form['#id'] = drupal_html_id($form_id);
  }


MSChart changing my value

Question: I have an MS Chart.
My code is the following:
chart.Series[chartType].Points.DataBindXY(xValues, xCaption, yValues, yCaption);
chart.ChartAreas[0].AxisX.LabelStyle.Format = "CustomAxisXFormat";
chart.FormatNumber += new EventHandler<FormatNumberEventArgs>(chart_FormatNumber);

Then
private void chart_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.Format == "CustomAxisXFormat")
    {
        e.LocalizedValue = string.Format("{0:hh tt}", new DateTime(1, 1, 2, (int)e.Value, 0, 0).AddHours(-1));
    }
}

xValues and yValues are are arrays of ints.
The problem I am having is, if xValues = int[]{1,2,3}, when chart_FormatNumber handles the event, the values (e.Value) change to {2,3,4}.
So have to do a subtraction there to make it the correct value.
Can somebody tell me what is going on and/or how to stop MSChart from changing my values?
Comment: I tested it, and it works fine on my machine. In fact, if I remove the subctraction the labels are correctly shown. You should post a small but complete example to reproduce the issue (if it's too long for a single post, use http://pastebin.com/)
Comment: Thanks for attempting to reproduce this.  I was able to find the cause the my issue.  See my answer below.
Answer: Ok, after some head scratching, I figured out what was happening.  I was supplying my x parameters are int.  MS chart was adding +1 and -1 to the x axis range.  The x values I was providing was xValues = int[]{1,2,3}.  In chart_FormatNumber() I was getting {0,1,2,3,4} which was throwing me off.

MSChart changing my value


Estimating and Interpreting the ATT with Regression Adjustment and Marginal Effects

Question: I am beginning a project that will employ regression/covariate adjustment to estimate the average effect of treatment on the treated (ATT) and I realize that I have two questions concerning how one estimates the ATT in such a setting and how one interprets the regression output when using GLMs.
First, I am slightly confused on the specification of a desired treatment effect under a regression adjustment framework. For example, in alternative strategies, such as matching or weighting, syntax for executing these methods typically supports an explicit argument where one specifies the desired treatment effect: effect = ate, qoi = att, something along these lines. However, in a standard regression formula in R y ~ x1 + x2 + ..., data = data I do not know how to effectively specify the argument for the treatment effect that I want to estimate. By default, does regression adjustment estimate the ATE? If so, how does one modify this?
Second, after reading papers by Mood 2010, Hanmer and Kalkan 2012, and Norton and Dowd 2018, it is apparent to me that, when modeling non-continuous outcomes, regression coefficients, odds ratios, IRRs, hazard ratios, etc. may prove problematic in interpretation. One solution is to estimate average marginal effects (AMEs). This leads me to my second question. Suppose that I estimate the ATT for a treatment on a count outcome. I then estimate the AME. Can I effectively interpret this AME as the ATT, or are they fundamentally different quantities of interest?
Answer: The method to estimate representative treatment effects using regression is called g-computation and works with any outcome type as long as the effect measure can be specified as a contrast between means (e.g., a mean difference, a ratio between marginal probabilities, a ratio between marginal odds, etc.). Here's how this works:

Fit a model for the outcome. Ideally this is a flexible model that includes interactions between the treatment and covariates.
Generate predicted values from this model setting all units' treatment value to "treated"
Generate predicted values from this model setting all units' treatment value to "control"
Compute the mean of the predictions under treatment (2) and the mean of the predictions under control (3)
Compute a contrast between these two means.

This method of g-computation estimates the ATE. To estimate the ATT, steps 2 and 3 should be done using only the treated units. The control units are still used to fit the model in 1, but only the treated units are used to compute the predicted values.
To get standard errors, you can use bootstrapping or the delta method (the latter of which is exactly accurate when the outcome model is linear and the contrast is the difference in means but only an approximation otherwise).
In R, this is really easy using the marginaleffects package:
#Fit the outcome model
fit <- glm(Y ~ A * (X1 + X2 + X3), data = data)

#Generate predictions and contrast them
avg_comparisons(fit, variables = "A",
                newdata = subset(data, A == 1))

This works for any GLM, e.g., logistic regression, Poisson regression etc. To compute contrasts that aren't the difference in means/risk difference, just supply an argument to comparison and transform (e.g., to get the risk ratio/relative risk, you would set comparison = "lnratioavg", transform = "exp").
This quantity is related to an AME, though that term is a bit ambiguous because of the multiple meanings of the word "marginal". The word "marginal" in AME means the instantaneous rate of change when the predictor is changed by a tiny amount. For a binary predictor, we are not changing it by a tiny amount; we are going from 0 to 1 (or whatever values you have). So AME is not an accurate way to describe this contrast, though I often use it because it is very closely related in computation and concept to a true AME. Rather, this is a "contrast between the average adjusted predictions". Kind of a mouthful.
Comment: The ATT is not really an appropriate estimand for sequential treatments. There is no clear definition of "the treated" when units can receive treatment at various time points. So this is only for point treatments. For sequential treatments, you can estimate the ATE using either a variant of g-computation designed for longitudinal treatments (much more complicated than the method I demonstrated because you need a separate model for each time-varying covariate) or IPW, which is much simpler. Both are used to estimate the parameters of a MSM.
Comment: A quick follow up: Does this approach automatically account for the panel structure of my data (just realized I did not specify this in my question). Or do I need to pair g-computation with marginal structural models. I completely admit that this question may indicate ignorance on my part. I have been having difficulty contextualizing the relationships between g-methods. It is unclear to me if g-computation and IPW are MSMs or if g-computation and IPW are applied to MSMs.


What is right way to show folder usage in megabytes

Question: I'm using du -bs to count folder size.
du -bs /folder

Result is 96342835 in Bytes... so in MB is it (/1024/1024) 91,87MB.
OK, but when I use
du -hs /folder

Result is different. 99MB
Or
 du -s taxikondrat.cz/

100980 KBytes = 98,61MB
I tried different methods. In every I got different results. E.g.
find -type f -print0 | xargs -0 stat --print='%s\n' | awk '{total+=$1} END {print total}'

I got 96059200 bytes, so 91,60MB
ls -ARgo "$@" | awk '{q += $3} END {print q}'

96338739 bytes, so 91,87 MB. After rounding is same, but in bytes little different.
As a cherry on the cake is Google :)

So, what is true? Why this differences, and what method is to most accurate count?
Comment: What is real-world problem you are trying to solve?
Answer: You are comparing apples and oranges.
Please note that ls -s will give the size of a file based on the length of the actual data in the file.
By default the du command will give a size based disk usage. Since disk space gets allocated in blocks, and the lengths of  files are hardly ever  integer multiples of the block size, you end up with a block that's only partially filled with data but which can't be used by another file.
In other words, if your file-system has a blocksize of 512 bytes, a file of 1 byte length will take up 512 bytes on disk, the same as a file of 511 bytes. A file of 513 bytes will take up 2 blocks and 1024 bytes of disk space.
The total sum du file disk usages will almost always be more than the sum of ls -s file file sizes, especially when you have many (small) files and/or very large block sizes.
Using the du -b switch will NOT count blocks, but behave like ls -s:

-b  equivalent to --apparent-size --block-size=1
--apparent-size
   print  apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal frag‐
                mentation, indirect blocks, and the like
  -h, --human-readable
                print sizes in human readable format (e.g., 1K 234M 2G)

du -sh will convert the total number of blocks to a human readable format,
du -bh will print the sum of apparent sizes, in human readable format.
In my Maildir, with many small files that differs quite a bit:
$ du -bs cur/
84088436    cur/
$ du -s cur/
91800   cur/
$ du -sh cur/
90M cur/
$ du -bh cur/
81M cur/

EDIT With regards to converting to human readable output and megabytes, please don't overlook the whole "controversy' of human readable in SI decimal prefixes versus binary multiples, powers of 1000 versus  powers of 1024.
See for background this page: https://www.gnu.org/software/coreutils/manual/html_node/Block-size.html on how GNU utilities such as ls and du display them.
The capital M in du -h output stands for 1,048,576 bytes.
Comment: This is only the tip of the iceberg (but already complicated enough). There are also such exotic things as filesystems where "tails" of files (i.e. the last couple of bytes that are too small to fill an entire block) can share a block. (E.g. Reiserfs.) In that case, the tail blocks may be counted multiple times toward each file, and how you define the size of a single file in blocks is non-obvious. Then, there are sparse files, where the content of the file is larger than its representation on disk. The same goes for filesystems supporting compression. On filesystems with de-duplication, a …
Comment: … block may be part of more than one file; again, how do you sensibly define "size in blocks" in that case?
Comment: **TL;DR:** To just output real size on MB: `du -sbm` (replace m for k for kilobytes).
Comment: Note that `du -b` will also report the "real" length of the file.
Comment: Indeed there are additional complexities but simplified the disk usage is not expected to be equal to the size of the data. - For `du` the block size is usually simply set to 1024 bytes by default at compile time, completely regardless of how the underlying file system is configured


Prove and draw this metric

Question: $$d(x,y) =    \left\{
\begin{array}{ll}
      |x_1-y_1| & \text{iff} &
      x_2=y_2 \\
      |x_1|+|x_2-y_2|+|y_1| & \text{iff} & x_2 \neq y_2 \\
\end{array}
\right.
$$
$a)$ Prove this is a metric
$b)$ Draw the unit sphere centered at $(0,0)$ with respect to this metric
$c)$ Draw the unit sphere centered at $(1,1)$ with respect to this metric
My attempt
$a)$ Two cases:
case $1$: $x_2=y_2$
$i)$ Show $d(x,y) = 0$ iff $x=y=0$
$$d(x,y)= |x_1 - y_1| = |0-0| = 0 \checkmark$$
$ii)$ Show $d(x,y)=d(y,x)$ for all $x,y$
$$d(x,y)=|x_1 - y_1|= |-y_1+x_1|=|(-1)(y_1-x_1)|=|-1||y_1-x_1|=d(y,x) \checkmark $$
$iii)$ Show $d(x,z) \leq d(x,y) + d(y,z)$
$$d(x,z)= |x_1 - z_1|=|x_1 - y_1 + y_1 -z_1| \leq |x_1 - y_1|+|y_1-z_1|=d(x,y)+d(y,z) \checkmark $$
case $2:x_2 \neq y_2$
$i)$
$$d(x,y)=|x_1|+|x_2-y_2|+|y_1|=0 \\\text{iff}: x=y=0$$
$ii)$
$$d(x,y)= |x_1|+|x_2-y_2|+|y_1|=|y_1|+|-y_2+x_2|+|x_1|=|y_1|+|-1||y_1-x_2|+|x_1|=d(y,x)$$
$iii)$
$$d(x,z)=|x_1|+|x_2-z_2|+|z_1|=...$$
Not sure how to do this one.....................
$b)$ This is a unit sphere so $d(x,y)=1$. Let $x=(0,0)$ if $y_2=0$ then $$d(x,y)=|x_1-y_1|=|0-y_1|=|y_1|=1$$
Therefore this is only possible for points $(0,1)$ and $(0,-1)$
If $y_2 \neq 0$ then:
$$d(x,y)=|x_1|+|x_2-y_2|+|y_1|=|0|+|0-y_2|+|y_1|=|-y_2|+|y_1|=|y_2|+|y_1|=1$$
$$|y_2|+|y_1|=1$$
I can choose points such as $(0,1),(0,-1),(.5,.5)$ and the graph looks like a diamond
$c)$ Let $x=(1,1)$. If $x_2=y_2=1$ Then:
$$|1-y_1|=1$$
$$|-y_1|=|y_1|=1$$
So possible points are $(1,1),(-1,1)$
If $y_2 \neq 1$:
$$|1|+|1-y_2|+|y_1|=1 $$
$$|1-y_2|+|y_1|=0$$
Impossible to add two positive values and get zero. So there are no other possible points??
Can anybody help me with proving $iii)$ and help me with possible things that I messed up on?
Comment: This is called the River Metric on $\mathbb R^2.$ Lines of the form $\mathbb R\times \{y\}$ are separated from each other by mountains. The river is $\{0\}\times \mathbb R.$ So to get from $(x_1,x_2)$ to $(y_1,y_2)$ when $x_2\ne y_2$  you have to travel on the line $\mathbb R\times \{x_1\}$ to the point $(0,x_1)$ on the  river,  and travel on the river to  ($0,x_2),$ and travel on $ \mathbb R\times \{x_2\}$ to $(x_2,y_2)$.
Answer: Property 1: The distance should be zero if $ x = y $ regardless if $ x = 0 $ or not. Also, notice that if $ x = y $ then necessarily $ x_2 = y_2 $, so the "Case 2" you gave is moot.
Property 3: You need several cases. One is if the three points are in a horizontal line, which you proved already. Then you need a case where two points are in a horizontal line, and finally you need the case where no points share $ y $ coordinate.
Part b): You should look at several cases, cases where $ y = 0 $ and where $ y \neq 0 $.
Comment: I am done now. Would you be able to help me with the parts that I messed up on?

Prove and draw this metric


Reconciling two definitions of 'uncorrelatedness'

Question: In this paper, the authors defined uncorrelatedness in the following way:
Let $\mathbf{X}=(X_1,...,X_n)$, and $\mathbf{Y}=(Y_1,...,Y_n)$, where $X_i\sim X$ and $Y_i\sim Y$.
$\mathbf{X},\mathbf{Y}$ are uncorrelated iff $(\mathbf{X}-\bar{X}\mathbf{1})'(\mathbf{Y}-\bar{Y}\mathbf{1})=0$, where $\bar{X}=1/n \sum X_i$ and $\mathbf{1}=(1,...,1)$.
My question is what is the link between this definition of uncorrelatedness and the one usually used (I think), that of (for vectors r.v.) covariance matrix -  a matrix where each entry $(i,j)$ is $\text{Corr}(X_i,X_j)$ - being an identity matrix?
Any help would be appreciated.
Comment: Looks like the thing they're computing is a multiple of a sample correlation. If those terms are zero, the off-diagonal elements of the sample correlation matrix will be 0.
Answer: The authors in the specific paper do not treat the case of statistical correlation and non-correlation, which is only defined in terms of expected values. They are interested in vectors of realizations of two random variables, and the conditions they define are with respect to specific vectors, not the r.v.'s in general. And they explicitly say so in the Introduction of the paper.
Their interest lies in providing geometric intuition and conceptual separation about the three concepts of linear dependence, orthogonality and uncorrelatedness, in a linear algebra context, not a statistical one. So their definitions are suited to this view of these concepts, rather than the statistical ones.
Note how much more strict is this condition for uncorrelatedness of vectors of numbers, compared to the conditions for statistical uncorrelatedness: the later requires only that the condition $(\mathbf{X}-\bar{X}\mathbf{1})'(\mathbf{Y}-\bar{Y}\mathbf{1})=0$ holds "on average"
Comment: I posted an answer there.
Comment: Thanks for your interest in this question Alecos. One doubt: Could you elaborate a bit further on the last part of your answer? How does the statistical definition, $E[(\mathbf{X}-E(\mathbf{X}))(\mathbf{Y}-E(\mathbf{Y}))']= I_n$ relate to $E[(\mathbf{X}-E(\mathbf{X}))'(\mathbf{Y}-E(\mathbf{Y}))]=0$? And what is the link with orthogonality in the econometric sense? I hope you don't mind. (Your profile said you're doing a macro phd, so I have to take advantage. :) )
Comment: I have the impression that you are confusing  $E[(\mathbf{X}-E(\mathbf{X}))(\mathbf{Y}-E(\mathbf{Y}))']$ with $E[(\mathbf{X}-E(\mathbf{X}))(\mathbf{X}-E(\mathbf{X}))']$. Moreover this (last) expression gives covariance of the elements of vector $\mathbf X$, not correlation, so it is not equal to the identity matrix -what we want for non-correlation is that it is diagonal.
Comment: Yes, you're right, my mistake...  The papers def. is the usual def of sample covariance = 0 (without the constant 1/(n-1)).

But still, how come in many econometric texts it seems as if they use the definition of orthogonality as interchangeable with uncorrelatedness? (maybe I'm just confusing things... I I have already asked something similar before, but I didn't understand some of the comments, nor did I get an answer. http://stats.stackexchange.com/questions/111391/interpreting-orthogonality )


Is this a melody or harmony?

Question: I was playing four 9th chords in a 4/4 time signature. All whole notes.
Then I shortened some of of the notes of each chord by a quarter or half note by playing them late. For example, in a Cmaj9 chord I would play the pitch 5th (G) 1 quarter note later and the 9th (D) a dotted half note later.
I used this technique for all chords to shorten some notes and play them later than the rest of the chord. I hope you get what I mean. To visualize it (x = rest; – = note):
D xxx–
B ––––
G x–––
E ––––
C ––––
Now, I was wondering if I am creating a melody by doing this.
AFAIK a melody is a pattern of single pitches that can be recognized as a unit.
For example, the 9th (D) is only 1 quarter note long and the movement from G to D is distinct. Since I've done this shortening technique for all 4 chords, there seems to be some melodic movement. Or is this still defined as a harmony?
I know it's maybe a hair splitting question but I would like to understand how those things are defined.
Thanks in advance.
Comment: If it draws attention as a dominating and identifiable musical element that people will hum or whistle after hearing it, it's certainly _melodic_ at least. The more people do that the more melodic it is. IMO it's possible to construct a line of single individual pitches that's so forgettable and unidentifiable that I wouldn't call it a melody. A copyright court might agree. But what "is" something... Maybe you could ask, would someone call this a melody and would other people agree it's a justifiable labeling. And would a copyright judge agree. :)
Comment: Thank you. I see, so an abstract definition would be maybe "melodic movement within a chord progression"?
_
At the end of the progression I have rising notes. Basically a 9th chord that starts with only 2 pitches, then a quarter note later the next pitch is played, quarter note later next pitch and quarter note later last pitch.
_
This makes it feel like a melody to me but all pitches are still in 9th chord, so hard for me to tell if the shortening of the note transforms them into a melody.
Comment: A simplified version of this question would be "Can a series of chords, through their voicing and range choices, 'create'/suggest a melody?" The answer is yes. "Do they always? If someone perceives one, would everyone?" Both too vague/subjective to be answerable. "What can I do to help ensure that a melody is heard?" A great question; besides voicing choices, the answer would have to do with execution and bringing out certain notes. Consider asking that one if you want, but mention which instrument you play!
Comment: What I would like to know is, if I shorten that start point of every single note of a chord, so that no note starts at the same time and there's perceivable distance between them, is that still a chord progression? A chord is multiple pitches simultaneously but what about pitches that sound at the same time but started at different times?
Answer: Basically it can be either or both. It can be interpreted as an arpeggio of a Cmaj9 chord or it can be interpreted as a melodic fragment like this:

In the end the context that you place it in will determine whether it sounds more like melody or harmony. If this is all that is happening the top notes will give the impression of melody. If this is done behind a vocal or melodic instrument then it will have more of a harmonic quality to it.
The only potential issue is if you are repeating the B note on beat 2. If you do that it will detract from the feeling of it being a melody because the G will be under the B and will not stand out as much. Are you playing each note only once or are you playing them on every beat? I am under the impression that each note is struck only once.
Comment: Thanks for your reply. Every chord is only played once for a duration of 1 bar. Currently there are no other instruments. I will add a bass and maybe a melody but this is actually when this question arose because I was wondering I already have "a melody in my chord progression".

Is this a melody or harmony?


View ignores unchecked "require this relationship"

Question: I am building a view in Drupal 8 that calls for two relationships:

Nodes referenced by the current node (relationship required)
A single node referenced by the above reference node (relationship not required)

Although that second relationship isn't required, my view is still filtering out all nodes where that second relationship is null. Is anyone else having this problem?
Answer: I was able to solve this by changing which field I wanted from the reference node. I was originally trying to get the path, but I switched it to "Link to Content." Still, it's odd that when trying to get just the path, views suddenly requires the relationship.


Multiplying multiple columns with the same single column in a Multi-index column Pandas

Question: I have this data frame
col = pd.MultiIndex.from_arrays([['Jan', 'Jan', 'Jan', 'Cost'],
                                ['WK1', 'WK2', 'WK3', 'Cost']])
data = pd.DataFrame(np.random.randn(4, 4), columns=col)
data

and I want to multiply all the column in Jan with cost so i tried it like this
data[['Jan']] * data[['Cost']]

but it fills my values with Nan. Is there a way i can do this?
Answer: I think you need remove [] for selecting DataFrame with mul:
print (data['one'].mul(data['two']))

Or:
print (data['one'] * data['two'])

Also is possible use function xs:
print (data.xs('one', axis=1, level=0) * data.xs('two', axis=1, level=0))

Answer: From what I can guess, you have trouble accessing columns using multi level index. Is this what you are trying to do?
data[('one','a')] * data[('two','b')]
Out[1065]:
0    0.528834
1   -0.287322
2   -0.255244
3    0.871213
dtype: float64

Answer: Hi guys I made a for cycle to answer my question
for col in data['Jan']:
 data[('Jan', col)] = data[('Jan',col)] * data[('Cost','Cost')]

Hope someone can give me a feedback in what i can improve


Understanding of an example of vector space

Question:
Let $L\supseteq K$ be a field such that the operations on $K$ are a restriction of the operations on $L$. Then $L$ is a vector space over $K$.

I didn't understand this example. How was $L$ a vector space over $K$? Can you explain clearly?
Comment: Is the first sentence supposed to say "such that the operations on $K$ are a restriction of the operations on $L$"?
Comment: @TannerSwett yes.
Answer: @CFalcon 's answer tells the abstract story. Here's an example you are familiar with that might help you understand.
The real numbers $\mathbb{R}$ are a subset of the complex numbers $\mathbb{C}$. In fact they are a subfield. Now suppose you temporarily forget that you can multiply complex numbers but remember that you can multiply a complex number by a real number. Then you can think of $\mathbb{C}$ as just $\mathbb{R}^2$ - a two dimensional real vector space. The vectors $(1,0) = 1$ and $(0,1)= i$ are a basis.
For a second somewhat more complicated example prompted by the comments, consider the field of rational numbers $\mathbb{Q}$ as a subfield of the real numbers $\mathbb{R}$. You can add real numbers and multiply them by rationals, so the real numbers are a vector space with scalars the field of rationals. What makes this example harder to understand is that the dimension of $\mathbb{R}$ as a vector space over $\mathbb{Q}$ is infinite - even uncountable. You don't want to try to think about a basis.
Comment: @Kahler If $K=\Bbb Q$ and $L=\Bbb R$. Then $K$ is not a vector space over $L$.
Comment: Because we have $2 \in \Bbb Q$. And if we take $\pi \in \Bbb R$, then $2\pi \notin \Bbb Q$.
Comment: in the my example, I think, $K$ should be a vector space over $L$ but not. only, I did not understand this statement.
Comment: @VikrantDesai Why?
Comment: @VikrantDesai Thanks.
Answer: The setting means that $K$ is a subfield of $(L,+,\times)$, therefore one can define an external multiplication by elements of $K$ on $L$ in the following manner:
$$\cdot\colon\begin{cases}K\times L&\rightarrow & L\\(\lambda,x) & \mapsto &\lambda\times K\end{cases}.$$
It is easy to check that $(L,+,\cdot)$ is a vector space over $K$, namely:

The couple $(L,+)$ is an abelian group.
The map $\cdot$ is compatible with the multiplication in $K$ i.e. one has:
$$\forall (\lambda,\mu)\in K^2,\forall x\in L,\lambda\cdot(\mu x)=(\lambda\mu)\cdot x.$$
The map $\cdot$ is distributive with respect to addition in $L$ i.e. one has:
$$\forall \lambda\in K,\forall (x,y)\in L^2,\lambda\cdot(x+y)=\lambda x+\lambda y.$$
The map $\cdot$ is distributive with respect to addition in $K$ i.e. one has:
$$\forall (\lambda,\mu)\in K^2,\forall x\in L,(\lambda+\mu)\cdot x=\lambda x+\mu x.$$
The identity element of $K$ is an identity element for $\cdot$ i.e. one has:
$$\forall x\in L,1_K\cdot x=x.$$


Understanding the proof of $\left ( A' \right )^{-1}=\left ( A^{-1} \right )^{'}$

Question: Assume that $A$ is an $n \times n$ square matrix and is nonsingular.
To prove that $\left ( A' \right )^{-1}=\left ( A^{-1} \right )^{'}$, it is mentioned in the solution of the lecture notes that $$A'\left ( A^{-1} \right )^{'}=\left ( A^{-1} \right )^{'}A'=I.$$
This is the part that I don't understand. How does showing the above help us prove that $\left ( A' \right )^{-1}=\left ( A^{-1} \right )^{'}$?
Comment: Uniqueness of inverse.
Comment: What does the $'$ in $A'$ stand for? Transposition?
Comment: @Fimpellizieri $A'$ is the transpose of matrix $A$.
Answer: If you have a matrix $N$ and you find that $MN = NM = I$ then this automatically implies that $M$ is the unique inverse to your matrix $N$. We denote this by $N^{-1} = M$.
So if you show that $A' \left(A^{-1}\right)' =\left(A^{-1}\right)'A' = I$ then you've shown that $\left(A^{-1}\right)'$ is the inverse to $A'$. But we denote the inverse to $A'$ as $\left(A'\right)^{-1}$. So this means that $\left(A'\right)^{-1} = \left(A^{-1}\right)'$.

If you still can't quite see it, try giving $A'$ a new name; like so. Let $N = A'$ and $M = (A^{-1})'$. Then you've showed $NM= MN = I$. So this means $N^{-1} = M$. That is $(A')^{-1} = M = (A^{-1})'$.
Comment: Right. Let $N = A'$ and $M = (A^{-1})'$ Then you've showed $NM= MN = I$. So this means $N^{-1} = M$. That is $(A')^{-1} = M = (A^{-1})'$.
Comment: Still not seeing it quite yet, but if $A' = N$ then is $\left ( A' \right )^{-1} = N^{-1} = M$? I'm trying it with new labels,
Comment: Ah I can see it now. Quite a sneaky trick!
Answer: The idea is that if $Y$ satifies $XY=YX=I$, then $Y=X^{-1}$ (by uniqueness, as pointed out by Zain Patel). In this case we have $X=A'$ and $Y={(A^{-1})}'$, therefore ${(A^{-1})}'=X^{-1}={(A')}^{-1}$.


Checkbox so user can decide whether image should be displayed or not

Question: I would like to add a checkbox yes / no whether the image field should be displayed or not in a node.
Answer: Mh, there are several ways.
One of this is to add a boolean field to the content type.
Then you can create a template for the node, or the field, and display it only if the value of the boolean field is "on".
What do you think?
Comment: sounds good to me.


Obtaining cdf from pdf when pdf is defined on limited region/support

Question: This is a very simple question, but I want to make sure I am doing it correctly.
I have the pdf from a Pareto distribution:
$$f(x) = 160 x^{-6}, \ \ 2 \leq x < \infty$$
and want to obtain the cdf
$$F(x) = \int_{- \infty}^x f(t) \mathrm{d}t$$
In this case, is it the same if I substitue the lower bound of the integral to $2$ since the pdf is specifically defined for $x \in [2, \infty)$ such that $F(x) = -32x^{-5} + 1$?
Comment: Yes, you are correct.
Comment: The confusion only arises because you failed to define what $f$ was to the left of $2$. If you correct the omission, the difficulty immediately disappears.
Comment: I have seen the problems defined this way almost all of the time. I was unsure if it always was implied that everything outside this interval was $0$
Answer: Let's write the calculus formally.
$$f(x) =  \begin{cases}
      160x^{-6} & x\ge2\\
      0 & x< 2
   \end{cases}
$$
To get from $f(x)$ to $F(x)$, integrate:
$$F(x) = \int_{-\infty}^{x}f(t)dt.$$
But we can break up the integral into an integral from $-\infty$ to $2$ and another from $2$ to $x$.
$$ F(x) = \int \limits_{-\infty}^{x} f(t) \ dt = \int \limits_{-\infty}^2 0 \ dt + \int \limits_2^{x} 160t^{-6} \ dt = \int \limits_2^{x} 160t^{-6} \ dt.$$
Comment: For the answer to be completely accurate, you need to specify $x\ge 2.$  Obviously $F(x)=0$ for all $x \lt 2,$ but that differs from what your formula asserts.


Our domain is being appended onto emails addresses that are sent from our server, why?

Question: Our app sends out many types of email notifications. On some occasions, we see that the email addresses are getting our domain appended to them.
e.g. firstname.lastname@example.com turns into email@example.com
What is causing this?
The App is running on a LAMP stack. We're using Sendmail / PEAR's Mail module.
Comment: What happens if you send out a message from Sendmail directly via bash (`sendmail -t firstname.lastname@example.com`)?
Answer: Check your mail server's configuration for the hostname! Also, often a missing dot at the end of a name in your bind configuration might cause this!
Answer: If you do have a wildcard record for your domain (*.example.com above) it might be that a rule in mailer config is firing which appends the domain name when searching for it.
What does the output of
<code>dig -t any gmail.com.example.com
</code>
give? anything about a valid A record or MX record?


What does it mean in terms of regression if residuals are not white noise?

Question: I need help in answering this one, it is an exam question.
Comment: Are these supposed to be residuals from a fit of a time-series? That is often the context where the term "white noise" is used.
Answer: Strictly speaking, the residuals of a regression are not white noise. Since each residual is a function of the entire data set, the residuals are lightly correlated.
But ... there's correlation and there's correlation.
Residuals can fail to be "white noise" if:

The regression model was not correctly specified. e.g $Y=a + bX + cX^2$ should have been chosen instead of a $Y=a+bX$.
Additional covariates are needed
the covariates are correct but the variance is not constant.
The error structure was not normal to start with. e.g. if you fit a linear regression model to 0/1 count data, you will get weird residuals near the extremes. The solution, in this case, would be to fit a logistic model.

Bottom line: when the residuals fail to be white noise, a different model should be tried.
Answer: Short answer regarding time series regression: If they are not white noise (i.e. they are not normal, not have zero mean or serially autocorrelated), then your model is not fully adequate. Therefore, you should revise your model. Usually (but not always), this means that there is a significant autocorrelation (of some order) among the residuals so you should improve your model.


Read from a text file

Question: I created a project and I want to read an array of strings from a .txt which is static. What is the right way to do that?
I created myfile.txt and put it in Asset folder of my project. Is it better to create a new folder? And then how can I read that file?
Comment: This is probably the most google-able question in a while.
Answer: You can read the file in various ways. Look at StreamReader for reading
See the docs:
    using (StreamReader sr = new StreamReader("c:\\TestFile.txt"))
    {
        while (sr.Peek() >= 0)
        {
            Console.WriteLine(sr.ReadLine());
        }
    }

Note the need to escape a backslashes \ in path names if you don't use raw strings.
Comment: I'm not sure, but http://msdn.microsoft.com/en-us/library/windows/apps/hh454050%28v=vs.110%29.aspxmentions StreamReader.
Answer: The easiest way to do so is by using
file3 = await localFolder.GetFileAsync(path);
IList<String> readFile = await Windows.Storage.FileIO.ReadLinesAsync(file3);

where path represents the file path or file name (depending on the location of the file)
It does not save it in an array as you wish but you can have the same functionality of arrays by utilizing lists. If you do not know how to utilize lists then there is more reason to use this method as practice and help you learn lists

Read from a text file


Show that every large integer has a large prime-power factor

Question: Show that every large integer has a large prime-power factor
That is, if $P(n)$ designates the largest number $p^a$ which divides $n$, then $\lim_{n\to\infty}P(n)=\infty.$
Answer: $\rm\bf Guide$: Let $p^a$ be a prime power. There are only finitely many prime powers less than it, and every natural can be uniquely written as a product of prime powers. Use this to show that the number of naturals $n\in\mathbb{N}$ such that $P(n)=p^a$ is finite, no matter what $p^a$ is. Along these lines, for given $p^a$, what explicitly is the largest $n$ such that $P(n)=p^a$? What does this mean for $P(\cdot)$ for all $\cdot>n$? How does this show that $\lim\limits_{n\to\infty} P(n)=\infty$?
Answer: HINT Show that for every $M \in \mathbb N$, there exists $N = N(M) \in \mathbb N$ such that $$ P(n) \leq M \implies n \leq N .$$
Why does this statement prove that $P(n) \to \infty$ as $n \to \infty$?
Comment: What does your answer mean?
Comment: @Z_Fang It's just a hint. I can elaborate on the hint, but anon's answer already explains the idea nicely.
Comment: Actually it is a problem (Page 11) in Fundamentals of Number Theory, by William J. Leveque
Comment: How about considering the converse-negative proposition for $ P(n) \leq M \implies n \leq N $ ?
Comment: @Z_Fang: If you mean the [contrapositive](http://en.wikipedia.org/wiki/Contraposition), then that's also valid because it's logically equivalent.
Comment: @anon But he asked, "Why does this statement prove that $P(n) \to \infty$ as $n \to \infty$?" Could the contrapositive reveal that?
Comment: @Z_Fang Well, actually, there's not much to prove there. The first sentence is essentially the definition of "$P(n) \to \infty$ as $n \to \infty$".


find the number of ways to choose an ordered pair $(a, b)$ of numbers from the set {${1, 2, 3, ..., 10}$} such that $|a-b|\leqslant5$

Question: find the number of ways to choose an ordered pair $(a, b)$ of numbers from the set {${1, 2, 3, ..., 10}$} such that $|a-b|\leqslant5$
My Approach:
I wrote down all the solutions, choosing $a=10$, $a=9$ and so on. Adding up all those solutions gave me the answer $70$. I think I counted all of the solutions but the answer is $80$.
Answer: Cause we find: ordered pair (a,b)
Case 1:
Assume a = b then:
we have 10 pair ex: (1,1), (2,2), (3,3), ..., (10,10)
Case 2:
Assume a > b then:
$ 5 \ge a-b > 0 $
$ \Rightarrow b+5 \ge a \gt b $
<code>    Notice: For each b (from 1 to 5) we can get:
              5 value for a ( from b+1 to b+5)
            --> The number of way: 5 * 5 =25
     For each b ( from 6 to 10) we can get:
              10 - b value for a
            --> the number of way: 4 + 3 + 2 + 1 = 10
</code>
the number of way for case 2 is:
$ 5 \times 5 + 4 + 3 + 2 + 1 = 35$
Case 3:
Swap (a,b) from case 2:
<code> the number of ways: 35
</code>
So we counted: 10 + 35 +35 = 80 pair
`
Answer: Your answer is correct if the numbers cannot be repeated. You get $80$ if you assume that the same number can be picked twice. For $a \in (1, 5)$, you have $6$ choices each which is $a \leq b \leq a + 5$. That leads to $6 \times 5 \times 2 = 60$ solutions. Multiplication by $2$ is as they are ordered pairs.
Now for $a \in (6, 10)$, the number of choices reduce by $1$ for every next number. So for $6$, we have five choices ($6$ - $10$), for $7$, four choices and so on.
That leads to $2 \times (5 + 4 + 3 + 2 + 1) = 30$ solutions.
Now note that $a = b$ should be counted only once i.e. we counted $(1, 1), (2, 2)...(10, 10)$ twice when we multiplied by $2$.
So the answer is $60 + 30 - 10 = 80$. This is assuming that the same number can be picked twice.
Comment: so how do I assume that I can select 'repeat'? I specifically didn't make cases for those 10 solutions.
Comment: @ShlokJain it is not your mistake. The question should have been more clear. If you are faced with ambiguous situations like this in an exam, all you can do is to upfront and clearly mention what is the assumption that you have made for your solution.


Help needed to interpret a probability formula

Question: I am trying to implement this paper  (Skeleton-based shape classification using path similarity) in Python but have difficulties understanding formulas 5 and 7 on page 7.

$$p(sp'|c_i) = \sum_{sp \in G(c_i)} p(sp'|sp)p(sp|c_i) \tag 5$$ $$p(c_i|sp') = \frac{p(sp'|c_i)p(c_i)}{p(sp')} \tag 7$$

The goal is to find a shape (named "class") whose topology is the closest to a query shape.

In this example, the probability that the query shape belongs to class C should be the highest
More specifically I am trying to calculate the "posterior probability" that a query shape belongs to a given class (formula 7)
What I understand: This "posterior probability" is based on "path similarity", i.e how far/close the skeleton paths of the compared shapes ("query" and "class") are.

a skeleton path (sp) is the route along the edges of the skeleton that starts at one vertex of the polygonal shape and ends at another one.

the distance is the difference between the respective radii of the m equidistant maximum disks along 2 different skeleton paths

What I DO NOT understand: Path comparison.
Question:
Do I need to compare each path of the query shape against each path of a class shape (nested  for loop) ?
If not, what paths do I need to compare ?
Again, I believe understanding formula 7 is key to get this right
Here a fully annotated snippet in Python showcasing the example displayed on the first picture (query shape vs A/B/C classes):
https://trinket.io/python3/a0eb5843c0
Answer: Skimming over the research paper and your code, the prior probability settings are
$\quad p(A) = p(B) = p(C) = \frac{1}{3}$
Also, these probabilities are initialized,
$\quad \text{If } sp \in A \text{ then } p(sp \mid A) = \frac{1}{132}$
$\quad \text{If } sp \in B \text{ then } p(sp \mid B) = \frac{1}{182}$
$\quad \text{If } sp \in C \text{ then } p(sp \mid C) = \frac{1}{130}$
If the input shape has skeleton paths $Q$,
$\quad \text{If } sp' \in Q \text{ then } p(sp') = \displaystyle p(sp'  \mid A) \,p(A) +p(sp'   \mid B ) \,p(B) + p(sp'  \mid C)\, p(C)$
(see formula $\text{(8)}$ in XIANG BAI's paper)
The first thing to do is see how the initial state is defined (all the input probabilities are initialized) and how you can 'turn the crank' on the formulas in hand and calculate
$\quad p(A|Q), p(B|Q) \text{ and } p(C|Q) \quad - \text{ check that they add up to }1$
For every $sp' \in Q$ you have to calculate $p(sp' \mid sp)$ for every $sp \in A \cup B \cup C$.
Comment: Thank you very much for taking the time to have a look at the paper. I have modified and annotated the Python code based on your explanation but notice that p(A|Q) + p(B|Q) + p(C|Q) doesn't equal 1. Could it be because of the "alpha value" from formula 4 ? Also the final posterior probabilities seem odds, they almost have the same value for each class (60.587, 60.485, 60.928) despite the topological difference.


Enable "parent":[] in REST export view

Question: I have multi level taxonomy with 2 levels, but when I run REST export on it, "parent":[], always stays blank - no matter how many levels there is.
How can I expose relations within taxonomy via restful?
Answer: This is a bug in core, see https://www.drupal.org/node/2543726
In the meantime, you could write a new normalizer service that is only for terms.

Enable "parent":[] in REST export view


jquery loaded html div elements selector

Question: First sorry if its a stupid question but i ran into a problem what i cant really solve.
I am loading a html file with the load function, and my problem is if i load the html file with the load function, the selectors with jquery dont really work, only with css.
(i use codeigniter)
example
load the html
$('.get_posts').hide().load('stream/load_posts').fadeIn(800);

so this html (view) file contains the following
<?php if(isset($stream_posts)): ?>
<?php foreach($stream_posts as $sp): ?>
<div id="sp_<?php echo $sp->sid; ?>" class="row stream-posts">

        <?php echo $sp->first_name ." ". $sp->last_name; ?>

            <?php echo nl2br(auto_link($sp->text)); ?>

<?php endforeach; ?>
<?php endif; ?>

i use codeigniter so at this line <?php echo nl2br(auto_link($sp->text)); ?> Automatically turns URLs and email addresses contained in a string into links, so when i have an auto link i would like to add an oembed class to it, so i tried the following
$("p.stream-content a").addClass('oembed');

and its not working, tried with css with the same selector with difference by coloring the anchor tags red, in css it works, than i discovered, the jquery selector part its not working because i use load, if i uncomment load and show the results via php it adds the class fine with jquery too.
Could please someone give me a hint for this?
Thank you
Answer: Did you try adding the class on the callback of load()?
$('.get_posts').hide().load('stream/load_posts', function() {
  $("#stream-text a").addClass('oembed');
}).fadeIn(800);

Note that I have changed your $('p.stream-content... because there is no
 element with class="stream-content"
Comment: sorry those where my typos, but thank you works with the callback, thanks so much (will accept your answer have to wait one min)


How to generate data from this particular multivariate distribution?

Question: Let $(X_0,X_1)$ be a random vector distributed according to the CDF $F_{(X_0,X_1)}(x,y)= \min (F_{X_0}(x),F_{X_1}(y))$ where $F_{X_0}(x),F_{X_1}(x)$ are the CDFs of $X_0,X_1$ respectively. We do not necessarily have densities.
I know how to sample from a one-dimensional CDF using inverse transform sampling. I want to sample from $F_{(X_0,X_1)}$.
My idea:
Sample $x_0$ from $F_{X_0}$. Then sample $x_1$ from $F_{(X_0,X_1)}(x_0,\cdot)$ which is now univariate. Is this correct ? How to prove that $(x_0,x_1)$ is indeed sampled according to $F_{(X_0,X_1)}$ ?
EDIT: my proposal is wrong since $F_{(X_0,X_1)}(x_0,\cdot)$ is not a CDF.
Comment: If they are independent, you can just do separate inverse transforms.  If they are not independent, it is significantly harder, except in some narrow cases.
Answer: Big hint: For inverse transform sampling, you use a uniform random variable $U$ to generate a sample $X=F_{X}^{-1}(U)$. What happens if you use the same $U$ to generate both $X_0=F_{X_0}^{-1}(U)$ and $X_1=F_{X_1}^{-1}(U)$?


trying to add certificates to my post method in java

Question: Hi im trying to add a crt and a key file to my post method as im using ssl connection to connect to server in https protocol
I dont know how to add crt and key and my server password to my request .
And also i use those certificates in my postman in setting/Client Certificates area
<code>public static  GetAPI(String query_url) {

    int ResponseCode = -1;
    try {

        URL url = new URL(query_url);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        con.setConnectTimeout(5000);
        // con.setDoOutput(true);
        // con.setDoInput(true);
        ResponseCode = con.getResponseCode();
        InputStream in = new BufferedInputStream(con.getInputStream());
        String result = IOUtils.toString(in, "UTF-8");
        // System.out.println(con.getResponseCode());
        // System.out.println(result);
        // System.out.println("result after Reading JSON Response");
        // JSONObject myResponse = new JSONObject(result);
        // System.out.println("jsonrpc- "+myResponse.getString("jsonrpc"));

        in.close();
        con.disconnect();

        return

    } catch (Exception e) {
        System.out.println(e);

    }
</code>
Comment: i need to add a .crt , .key and server password
Answer: If I've understood your case correctly, I think you need to add server certificate to your jre.
Take a look at this post.
Comment: So i need to add the certs to my  machine env variables?
Comment: Not, they have to be added to your jre. Using keytool utility (shipped with jdk ), you must add your certificate to a file ($JAVA_HOME/lib/security/cacerts) called truststore.


is there possible to create pipeline from GitHub to AWS EKS deployments on Git actions?

Question: I want to create a cicd pipeline from github to aws eks.
Is there possible to create pipeline from GitHub to AWS EKS deployments on Git actions ?
Answer: Yes its possible you need to use some kind of CI/CD tool (Jenkins/Gitlab/AWS Native services) in between to automate this whole process.
Flow would be something like
Developer commit changes --> Trigger CI/CD pipeline --> Build Docker image --> Push it to ECR -- Deploy latest image to EKS using (Kubectl or Helm charts)
Please refer :
https://www.eksworkshop.com/intermediate/260_weave_flux/ this has example for end to end implementation.
https://www.weave.works/blog/gitops-with-github-actions-eks
https://aws.amazon.com/blogs/opensource/git-push-deploy-app-eks-gitkube/
Comment: I have configured flux for deployment, but now flux pod is get `CrashLoopBackOff` and restarting hence I can not do the deployment, any comments on this.
Comment: I have tried and above issue has been solved not trying to push images to ECR but getting `no basic auth credentials` in GitHub actions.


macvlan and RST packets

Question: I have netcat running as a listener on port 7001 this is to troubleshoot a network client.
I ran wireshark on the server to capture the packets.
If I connect using eth0, it connects fine. SYN <-> SYN/ACK
If I use eth0mv1 it fails like this: SYN <-> ACK/RST
eth0mv1 was built with:
ip link add link eth0 name eth0mv1 type macvlan
dhclient eth0mv1

how do I figure this out?
update: If I set up 100 of them, the first I use works, all the rest don't
Answer: by any chance, are all the interfaces being created with exactly the same MAC address? you should specify a separate MAC for each macvlan interface;
ip link add link eth0 address 00:11:22:33:44:55 eth0mv1 type macvlan
Comment: If it is called without specifying an address, a random one is generated.

macvlan and RST packets


Features Hook after successfull import

Question: I wrote a feature module that contains content types and test nodes. After importing those nodes, a certain cache has to be rebuild.
Both hooks I know (hook_install() and hook_enable()) are called before the import happens.
Is there any hook that is executed only once after the feature import is done?
Answer: It depends on how the nodes are imported really. You should be able to use hook_modules_enabled which is the last hook invoked when a module is installed and enabled. You might need to tweak your module weight to make sure it's invoked after the import of the nodes.
Comment: Thanks that worked - however the hook is called `modules_enabled`
Comment: @Ghrommey, thanks, I don't know how I managed to get the link right but the name wrong.
Answer: As of Features 7.x-2.x-something, there are new hooks available which should help here. There are now pre/post enable/disable/revert/rebuild hooks, see for example here.
In @jantimon's scenario, implementing hook_post_features_enable_feature($component) would do the trick.

Features Hook after successfull import


Generators of a quotient ring

Question: I have a polynomial ring $\mathcal{R} = \mathbb{Z}_3[x]/(x^2+x+2)$ and I am supposed to find a generator of the multiplicative group of $\mathcal{R}$.
Well, I know that the $\vert \mathcal{R} \vert = 9$, therefore the number of elements of the multiplicative group is $9-1 = 8$ and that the elements $f(x)\in$ $\mathcal R$ are $[f(x)]=\{g(x) \in \mathbb Z_3 \mid g(x) \equiv f(x)$ mod $(x^2+x+2) \}$. Though I do not know how to find the generator instead of brute-forcing. Is there a smart way to do it? Thank you in advance.
Answer: Write down explicitly:
$$\mathcal R^*=\left\{\,1,\,2,\,w,\,w+1,\,w+2,\,2w,\,2w+1,\,2w+2\,\right\}\;,\;\;\text{with}$$
$$w^2=-w-2=2w+1\pmod 3$$
Now, for example (all the operations are carried on modulo $\;3\;$ ):
$$\begin{align*}&w\\
&w^2=2w+1\\
&w^3=w(2w+1)=2w^2+w=2(2w+1)+w=w+2+w=2w+2\\
&w^4=w(w^3)=2w^2+2w=2(2w+1)+2w=w+2+2w=2=-1\end{align*}$$
Thus, we have already an element of order $\;8\;$ (why?) . You can now find all four elements of order 8 (how?)
Comment: @Ravi Of course, as the order of this cyclic group is even. Yet the OP seems to be a beginner in these matters...
Comment: You could probably skip the $w^3$ computation you know?
Answer: The ring is the splitting field of $x^9-x$; since
$$
x^9-x=x(x^8-1)=x(x-1)(x+1)(x^2+1)(x^4+1)
$$
and $x^4+1=(x^2+x+2)(x^2+2x+2)$, the complete factorization is
$$
x^9-x=x(x^8-1)=x(x-1)(x+1)(x^2+1)(x^2+x+2)(x^2+2x+2)
$$
Note that if $\alpha$ is a root of $x^2+1$, then $\alpha^4=1$, so it is not good for the purpose. Thus we remain with the roots of the other degree two factors: indeed the group is cyclic of order $8$, so it must have $\varphi(8)=4$ elements which are generators.

Generators of a quotient ring


What is the difference between Informative (IVM) and Relevance (RVM) vector machines

Question: I'm trying to understand if there is any specific difference between Informative IVMs and Relevance RVMs other than the terminology. I've not seen anything explicit.
When I'm reading about vector machines, it is easy to see the difference of IVM/RVMs from Support (SVM) vector machines [colloquially, for classification, the SVM finds those points (vectors) that define the DMZ (de-militatized zone ;-) between the categories, while the RVM finds those that are the 'middle' of the crowd, and an associated crowd 'size' (e.g. in gaussian globs)], but I don't see any special difference between I/R vector machines beyond a choice of terminology by their proponents.
Is there a difference?
Comment: The DMZ and crowd 'middle' visualisation is for a classification, whereby the we are, in essence, also visualising an underlying pdf (or combined pdf).
Answer: The RVM places an Automatic Relevance Determination (ARD) prior on the weights in a regularized regression/logistic regression setup. (The ARD prior is a just a weak gamma prior on the precision of a gaussian random variable). Marginalizing out the weights and maximizing the likelihood of the data with respect to the precision causes many of the precision parameters to become large, which would push the associated weights to zero. If you use feature vectors given by a design matrix, then this strategy selects a small set of examples that predict the target variable well.
The IVM strategy is fundamentally different from the RVM's strategy. The IVM is a Gaussian Process method that selects a small set of points from the training set using a greedy selection criterion (based on change in entropy of the posterior GP) and combines this strategy with standard GP regression/classification on the sparse set of points.
Unlike the SVM, for both the IVM and RVM there is not an obvious geometric interpretation of relevant or informative vectors. Basically, both of the algorithms find sparse (the SVM and IVM are dual sparse, but the RVM should probably be considered primal sparse) solutions for regression/classification problems but they use different approaches to do so.
Comment: Many thanks for that, it gives some extra clarification.
Answer: Some of the distinctions I have identified so far were hidden in (one of) the original papers by Neil Lawrence. There are two versions of "A Sparse Bayesian Compression Scheme - The Informative Vector Machine" [Kernel Workshop at NIPS 2001], one on the Microsoft research site, and one on Laurence's site.
In the MS version there is an extra sentence with the statement "the selected data points are close to the decision boundary, a characteristics shared with the SVM". So my original view that the IVM vectors represented the 'middle' was wrong.
The other point is that it is a compression scheme in the sense that it is looking for a 'sparse representation of the data set", so that the IVM also seeks to retain those vectors that provided the most information during the analysis, so they can be re-used, as a data set and any computation repeated.
This ability to reduce the size of the data set is useful when the computation is bigO(M^3)
The RVM does select the 'middle' vectors (and their weights) which act on the basis functions (e.g. Gaussians) (see Ch7.2 'Relevance Vector Machines' in Bishop's 'Pattern recognition and machine learning').
OK, so the explanation is a bit of a hand waving description and isn't fully complete, but hopefully it will help those who aren't as relaxed with compact matrix formulations. More feedback would still be welcome.
Comment: The 'compression scheme' point is from the fotnote in Lawrence's paper.
Comment: The second link (to Laurence's site) is no longer available. The paper can be found here http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.2.8516&rep=rep1&type=pdf


Rounding Text Field

Question: I have a text box titled NetAmt  and another box titled TotalGrossValue
I need the amount from NetAmt to populate the box TotalGrossValue and round up to the next $100.00.  So if the amount in NetAmt is $421.00, I need it to round to $500.00
I tried Math.cell(NetAmt/100)*100  but I am guessing the NetAmt is incorrect or needs more to the script.
Comment: Should work. You spelled ceil wrong. Change cell to ceil
Answer: You could use modulus (reminder) to get the rest, and check if the number already is rounded to 100 (rest equals 0), if not add 100 minus the rest.
Answer: In a simple way add 100 to your NetAmt value and substract the reminder from them. use the below code for better understanding.
<code>const TotalGrossValue =  NetAmt % 100  === 0 ? NetAmt : NetAmt + 100 - NetAmt % 100
</code>

Rounding Text Field


normal distribution; iron python

Question: I want the user to specify a range delta, sigma for the spread and the number of random values it should produce.
But what's the best way in Iron Python to produce random values with a normal distributon? I found a function in NumPy that does that, but to my understanding it doesn't work with Iron Python.
Answer: The random module has a function that will do this for you: random.gauss(mu, sigma) (or random.normalvariate(mu, sigma), although the former is supposedly slightly faster according to the docs). See also the IronPython docs here.
Comment: (+1) Might be worth mentioning `random.normalvariate()` for completeness.

normal distribution; iron python


Pycharm Can't install TensorFlow

Question: I cannot install tensorflow in pycharm on windows 10, though I have tried many different things:

went to settings > project interpreter and tried clicking the green plus button to install it, gave me the error: non-zero exit code (1) and told me to try installing via pip in the command line, which was successful, but I can't figure out how to make Pycharm use it when it's installed there
tried changing to a Conda environment, which still would not allow me to run tensorflow since when I input into the python command line: pip.main(['install', 'tensorflow']) it gave me another error and told me to update pip
updated pip then tried step 2 again, but now that I have pip 10.0.1, I get the error 'pip has no attribute main'. I tried reverted pip to 9.0.3 in the command line, but this won't change the version used in pycharm, which makes no sense to me. I reinstalled anaconda, as well as pip, and deleted and made a new project and yet it still says that it is using pip 10.0.1 which makes no sense to me

So in summary, I still can't install tensorflow, and I now have the wrong version of pip being used in Pycharm. I realize that there are many other posts about this issue but I'm pretty sure I've been to all of them and either didn't get an applicable answer or an answer that I understand.
Answer: you can try to follow the steps from "Pycharm anaconda import tensor flow library issue" for solve your issue ("You need to do these following steps:")
Comment: turns out I was confused as to what Anaconda actually is. I simply created the environment in pycharm with Anaconda, then went into the Anaconda terminal and installed tensorflow from there, thanks!
Answer: what worked for is this;

I installed TensorFlow on the command prompt as an administrator using this command pip install tensorflow
then I jumped back to my pycharm and clicked the red light bulb pop-up icon, it will have a few options when you click it, just select the one that says install tensor flow. This would not install in from scratch but basically, rebuild and update your pycharm workspace to note the newly installed tensorflow

Pycharm Can't install TensorFlow


How To Get Multiple HTML Page URL into PHP

Question: I am working on one assignment. I had to write code for sending the report in email.
When I open HTML page and click on the button then it sends in email.
BUT suppose I have 10 reports, and I want that when I click on send button it should send in email.
Because I have used
$html=file_get_contents("html_report.html");
in PHP code. It works for only html_report.html but I want it to work for rest 10 files.
For example : I have 1.html,2.html...10.html,I want to that whenever I open any HTML file and click on the button then it should send. So what should I write in
$html=file_get_contents("WHAT SHOULD I WRITE HERE");
so that it will work for 10 files.
any help will be appreciated.
Comment: Sounds like you need to use `file_get_contents` inside loop
Comment: Try researching first before asking a question. http://stackoverflow.com/a/11441389/2301271
Comment: @Roopendra I don't want a loop. I want URL of each page in **file_get_contents**.
Comment: Do you want to put content from all files in one email, or choose and send one of reports?
Comment: do You want to send 10 reports in a single email?
Comment: @MarkS No. I just want that whenever I send it should work for all 10 files.
Comment: @AwladLiton No. I just want that whenever I send it should work for all 10 files.
Answer: add this code on the page who's url you want to use:
<?php $_SESSION['url'] = $_SERVER['REQUEST_URI']; ?>

and then use this url from session
Comment: Can you explain it further?
Answer: you can use the array to loop all those html file.LIKE:
$reports = array("1.html", "2.html", "3.html", ...... "10.html");

then loop the $reports.
foreach ($reports as $report) {
   $html = file_get_contents("WHAT SHOULD I WRITE HERE");
   ........
}

Answer: use:
$_SERVER['REQUEST_URI'];

to get the current url.
Answer: You will have to get HTML in loop and then send email.
<?php
$arrTemplates = array('html_report1.html','html_report2.html','html_report3.html','html_report4.html','html_report10.html');
foreach($arrTemplates as $htmlFileName) {
    $html=file_get_contents($htmlFileName);
    // CODE FOR SEND EMAIL
}
?>

Hope this will work for you.
Answer: With For Loop
<?php
for ($x=1; $x<=10; $x++)
{
$html=file_get_contents("html_report".$x.".html");
// Send email
}
?>

With arrays
<?php
$reports=array("page1.html","page2.html","page3.html");
$arrlength=count($reports);
for($x=0;$x<$arrlength;$x++)
{
$html=file_get_contents($reports[$x]);
// send Email
}
?>

Answer: update
Say, you want to send report by mail. your first report is in report1.html file. you want to send this report. say, you are using a php file named sendReport.php  to sending email.
now, send a parameter in your file like this  :
yourhost.com/sendReport.php?file=report1.html

in your sendReport.php get file name like this:
$fileName = $_GET['file'];

then get report html like this:
$html = file_get_contents($fileName);

then send your mail with report which is in $html
for every report file just pass right report file name in your url like this: yourhost.com/sendReport.php?file=your_file_name.html
I think it makes sense!
EDIT:
You should send a parameter for file name in your url like this:
yoursite.com?file=your_html_file_name

then in your mail sending page get file name:
$fileName = $_GET['file'];

then get report html like this:
$html = file_get_contents($fileName);

then send your mail
Comment: Hi Awlad, Yes I want to do this.Can you explain me a bit further?
Comment: @Kalpesh Singh: i have updated my answer.
Comment: So I have to added **email.php?file=C:\xampp\htdocs\test\email report\report_ledger.html** in Button and added rest two function in email.php but getting error. **Undefined index: file in C:\xampp\htdocs\test\email report\email.php on line 6**
Comment: just pass the name email.php?file=report_ledger.html
Comment: No,It is not working. Getting same error.
Comment: Hi Awlad, Your solution is 100% right. I have add this in the link instead of the button and it works! Thanks man..You made my day.


Showing two statements regarding bases in a topology are equivalent

Question: I want to show that the following two statements are equivalent -
B1) For each pair $B_1, B_2 \in \beta$ and each $x \in B_1 \cap B_2$, there is $B_3 \in \beta$ such that $x \in B_3 \subseteq B_1 \cap B_2.$
B2) For each pair $B_1, B_2 \in \beta$ we have $B_1 \cap B_2 = \bigcup_{i \in I}B_i$, for some $B_i \in \beta, i \in I$.
From statement B1), we have that $\forall x \in B_1 \cap B_2$ there exists $B_x$ such that $x \in B_x \subseteq B_1 \cap B_2$
$\implies B_1 \cap B2 \subseteq \bigcup_{x \in B1 \cap B2}B_x \subseteq B_1 \cap B_2$
$\implies B_1 \cap B_2 = \bigcup_{x \in B1 \cap B2}B_x$
I.e. $\implies B_1 \cap B_2 = \bigcup_{i \in I}B_i$, for some $B_i \in \beta$, $i \in I$.
I haven't much experience of topology yet so I just wanted to check that this looks ok?
Answer: Looks good. I would add a bit more details, though
$$B_1\cap B_2=\bigcup\{\{x\}\mid x\in B_1\cap B_2\}\subseteq
\bigcup\{B_x\mid x\in B_1\cap B_2\}⊆B_1\cap B_2$$
Therefore $B_1\cap B_2=\bigcup_{x\in I}B_x$ with $I=B_1\cap B_2$.


Why is not the definition of probability distribution consistent with the definition of Binomial Distributions?

Question: Definition of Probability Distribution:

A probability distribution is a table or an equation that links each
  outcome of a statistical experiment with its probability of
  occurrence.

And,
Definition of Binomial Probability Distribution:

A binomial distribution is a specific probability distribution which
  is used to model the probability of obtaining one of two outcomes, a
  certain number of times (k), out of fixed number of trials (N) of a
  discrete random event.

Are there two definitions consistent with each other? I think no.
Why? Because, how is a Binomial Distribution acting like a table or equation that links each out come of the experiment with its probability of occurrence?
Comment: they are perfectly consistent. the [binomial probability distribution](https://en.wikipedia.org/wiki/Binomial_distribution) gives you the probability of the number of successes in a set of _n_ independent yes/no experiments, each one having identical probability of success _p_
Answer: You need to define what's an outcome. So, in case of Binomial distribution the outcome is "event occurred k times out of n trials". So you have a table with n+1 rows from 0 to n, each one representing one outcome, i.e. k occurrences of event. Each row will have a corresponding probability. Hence, the table of outcomes.
Answer: Imagine an experiment where you toss a coin $N$ times and count the number of heads that occurred. Imagine that the coin doesn't necessarily have to be fair and it can return heads with some probability $p$. Binomial distribution is a function parameterized by $p$ and $N$ that maps the number of heads that occurred, $k$, with probabilities. In here $p$ and $N$ are features of the experiment and $k$ is the outcome. So yes, it's consistent with the definition provided.


MuseScore - Copy and paste

Question: I have just started experimenting with MuseScore.  Nothing serious but it is proving to be a useful tool to explore and understand music.  I was entering some string parts and hit this odd problem. I had entered the cello part and wanted to copy it to the double bass.  So, I tried copy and paste.  It worked but the effect was not what I wanted.  It moved the notes up an octave so that the bass would play the same notes as the cello.  Of course, what I wanted was that it would be notated the same and hence the bass would play an octave below.  Is there a technique for this?
I can see the logic in what it did.  If I were to copy an oboe part to the clarinet then I would appreciate the transposition.  (I have not tried that yet.)
Answer: As you say, this is correct behaviour.  If you don't want a literal copy of the music, after the Copy/Paste leave the notes selected and call the Transpose function.  It will take just a couple of clicks to shift down an octave.
Comment: I had used transpose but I had not thought to try it on a single part.
Comment: In the case of an octave, you could also do shift+down arrow (I forgot the key combination but it is something like that)
Comment: Right after you paste, while the pasted notes are still selected, ctrl+down arrow will set the notes in the right octave, here.

MuseScore - Copy and paste


jQuery serialize inputs on form submit, but not with ajax

Question: Is there no way of submitting a serialized parameter without ajax?
I want to access, like, an json encoded parameter when I process the POST from the form, something like: $params =  json_decode($_GET['params']);
Any ideas, besides iterating each input and append it to a hidden one that will contain all parameters in an encoded form, ?
Update
I'm using codeigniter, so I would rather do something like
$search = json_decode($this->input->get('params'));
updateName($search['name']);
updateGender($search['gender']);

Than
updateName($this->input->get('name'));
updateGender($this->input->get('gender'));

Comment: Why bother? There's no shortage of server side libraries for processing application/x-www-form-urlencoded data (which **is** serialised, just not as JSON).
Comment: @Quentin I've updated my question, reasoning why I wan't to do so
Comment: it's pretty pointless to do that. you could use javascript to serialize and create a hidden input before the form submits. but the way you are proposing is wrong... cuz $search['name'] will throw an error if name doesnt exist while $this->input->get('name') won't
Answer: Not sure if I am missing something here but you can only do a json_decode() on a json object/array.
$this->input->get() will return the whole $_GET array AS A PHP ARRAY, so you dont need to do anything JSON'ick with it to be able to use it as an array.
so would this not do what you want?
$search = $this->input->get();
updateName($search['name']);
updateGender($search['gender']);

If you want to pass it through the XSS filter first just use
$search = $this->input->get(NULL, TRUE);


"Lifting" an algebraic structure on a codomain to the set of functions into it.

Question: Let $ S$ be any set and $G$ be a group. The set of functions from $S$ to $G$ is clearly also a group. The identity element is the constant function whose value is the identity of $G$, and the (group) inverse of any function $g$ is the function that maps $x \in S$ to the group inverse of $g(x)$ in $G$.
If we replaced "group" with ring (also commutative ring, ring with identity, and both), vector space, or algebra (assuming for the last two that a scalar field is given), the analogous statement is still true.
This clearly isn't the case for fields though. Assuming we want to make this work, the "zero function" would be the one that maps identically to zero. A function $f$, such that $f(x) = 0$ for at least one $x \in S$, but is not $0$ for at least one other $x \in S$, is not equal to the zero function, but also doesn't have a multiplicative inverse.
Are there other algebraic structures for which this relationship holds or fails in interesting ways? Is there a specific field of mathematics that studies this?
Answer: Perhaps a good (but restrictive) framework for this question is model theory. Since you are talking about algebraic structures, I will regard them as first order structures consisting of a set and operations and relations, although we might also want to include families of subsets.
So in this restricted context, consider $\mathcal{G}=(G,(f_{n,\alpha}),(r_{p,\gamma}))$ where the terms $f_{n,\alpha}$ are functions of arity $n$ where $\alpha$ ranges in an index set $F_n$ and the terms $r_{p,\gamma}$ are relations of arity $p$ where $\gamma$ ranges in an index set $R_p$.
All theorems of $\mathcal{G}$ which are positive and conjunctive, i.e. which can be written in the first order language (with equality) on
$\left\langle f_{n,\alpha},r_{p,\gamma} \right\rangle$ without use of the negation, disjunction and implication symbols, are valid in the following natural lifting of the structure on $G^S$:

for $n \in \mathbb{N}$, $\alpha \in F_n$ and $\varphi_1,...,\varphi_n: S \rightarrow G$, $f_{n,\alpha}(\varphi_1,...,\varphi_n)(s):=f_{n,\alpha}(\varphi_1(s),...,\varphi_n(s))$.
for $p\in \mathbb{N}$, $\alpha \in R_p$ and $\psi_1,...,\psi_p: S \rightarrow G$, define $r_{p,\gamma}[\psi_1,...,\psi_p]$ to be valid in $G^S$ if for all $s \in S$, the formula $r_{p,\gamma}[\psi_1(s),...,\psi_p(s)]$ is valid in $\mathcal{G}$.

To see that, procede by induction on lengths of formulas. As an example, if you want to prove that the conjunctive theorem $\forall x \exists y(r_2[f_2(y,x),f_1(y)] \wedge f_2(x)=y)$ of $\mathcal{G}$ is valid in $G^S$, then consider a function $\varphi: S \rightarrow G$, and then given $s \in S$, pick (with the axiom of choice) an elment $\psi(s) \in G$ with $r_2[f_2(\psi(s),\varphi(s),f_1(\psi(s))]$ and $f_2(\varphi(s))=\psi(s)$ and notice that $\exists y(r_2[f_2(y,\varphi),f_1(y)] \wedge f_2(\varphi)=y)$ is valid in $G^S$.
Notice that the higher order structure of $\mathcal{G}$ (for instance it being finite, a simple group, a Noetherian ring and so on) has no reason to be carried onto $G^S$, even if it can be stated in a seemingly non disjunctive way.
Moreover, this result is not optimal, since for instance the axiom $\exists x(r_{1,0}[x] \vee r_{1,1}[x])$ is carried to $G^S$ even if it has no reason to be equivalent to a conjunctive statement in $\mathcal{G}$. But it seems to be a good heuristic in the common cases.
For instance the axioms of groups and rings are positive and conjunctive, whereas the axioms of fields and partially ordered sets are not, and the disjunctive elements in the corresponding theories are where counterexamples can easily be constructed.
Since theories with only positive conjunctive statements are rather rare, there are methods in model theory to still be able to lift structure using functions.
The most common one is the ultrafilter method, where we take a quotient of $G^S$ defined using a ultrafilter on $S$, thereby killing many functions (and all but constant ones if the ultrafilter is principal).
An intermediate method is to quotient by killing less functions provided the model-theoretic structure is already tame enough that it can kill all remaining indeterminacies. This is the case if $\mathcal{G}$ is minimal (ex: $(\mathbb{N},x \mapsto n+1)$ and $(\mathbb{C},+,\times)$) then one can take a quotient using the filter of cofinite subsets of $S$. This is also the case if $\mathcal{G}$ is o-minimal (ex: $(\mathbb{R},+,\times,<)$): then one can use the filter of neighborhoods of $+\infty$ on $S$. I don't know any other important example.
Comment: Fascinating! I'll need some time to understand all of this though :)
Comment: If you are not familiar with model theory, you can replace the terms "theorems of $\mathcal{G}$" by "axioms of the type of structure (i.e. axioms for groups)" and you can discard the general statement about conjunctive formulas by looking only at the axioms: they are conjunctive if the symbols $\vee,\rightarrow,\leftarrow,\leftrightarrow$ don't appear.
Comment: Wait... this makes no sense, the negation should also be forbidden here! I'll edit accordingly.
Comment: When you mentioned "disjunctive element" for a field, did you mean something like every element is either 0 or a unit?
Comment: Yes,this is what I meant.
Answer: A nice intuitive way to look at this is to look at the smallest non-trivial case: the set of functions from a two element set into your algebraic structure $G$. Note that, imposing pointwise operations on this set gives the structure $G\times G$. The direct product of groups is a group. The direct product of fields is not a field. This is the heart of the issue and is worth pondering independently of any larger apparatus for understanding "why".

This all said, a convenient way to consider the issue is through the lens of universal algebra. In particular, this field defines a variety of algebra as a general means to define things like groups and rings. Loosely speaking, a variety consists of a set of operations along with some equalities between various expressions in these operations that must hold for all variables - it's a bit painful to write out a satisfying technical definition, but consider two examples:
First, groups form a variety because they can be defined as follows: Let $S$ be a set and $\cdot:S^2\rightarrow S$ be a binary operation, $^{-1}:S^1\rightarrow S$ be a unary operation, and $e:S^{0}\rightarrow S$ be a $0$-ary operation (i.e. a constant, since $S^0$ is a one element set). The tuple $(S,\cdot,^{-1},e)$ is a group if it satisfies
$$(x\cdot y)\cdot z = x\cdot (y\cdot z)$$
$$x\cdot x^{-1}=e$$
$$e\cdot x = x\cdot e = x$$
where all of these are quantized over all $x,y,z\in G$.
One can define rings and commutative rings similarly. With a bit of creativity, the set of vector spaces over a fixed field $F$ (or modules over a fixed ring) can also be expressed with an infinite set of operations and axioms, taking "multiplication by $c\in F$" to be a unary operation for each $c$. Essentially, the important fact is that these algebraic structures are defined solely by universally true axioms relating some constants - you can check that, so long as your algebraic structure is defined by these rules, we can impose the same structure on the set of functions into an example of that structure, since the rules will still hold pointwise (i.e. after evaluating the function anywhere). More generally, any direct product of members of a variety is still in that variety - the case of functions into a group is just one instance of that where all the factors are the same.
Fields are not a variety. At first glance, the usual formulation of a field does not obey the desired rules: how can we express the invertibility of all non-zero elements only by enforcing universal equalities between some compositions of functions? There is no provision to write "for all $x$ except $0$, we have...". The requirement that $0\neq 1$ is also problematic.
Delving a bit deeper, we see that this is actually impossible: the direct product of two fields is never a field, so it is not possible to define "field" in the manner of a variety. This is a fundamental difference between the concept of a field and the other concepts you list.
Comment: Thanks for this direct and insightful explanation! I am now a little disappointed in myself for missing the simple and rather obvious equivalence between sets of functions and Cartesian products of the codomain...